aboutsummaryrefslogtreecommitdiff
path: root/common/schema/driver.cxx
blob: eb1358ef387154072e1ee7c17a369582dd54f2ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// file      : common/schema/driver.cxx
// author    : Boris Kolpackov <boris@codesynthesis.com>
// copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC
// license   : GNU GPL v2; see accompanying LICENSE file

// Test various aspects of database schema.
//

#include <memory>   // std::auto_ptr
#include <cassert>
#include <iostream>

#include <odb/database.hxx>
#include <odb/transaction.hxx>

#include <common/common.hxx>

#include "test.hxx"
#include "test-odb.hxx"

using namespace std;
using namespace odb::core;

int
main (int argc, char* argv[])
{
  try
  {
    auto_ptr<database> db (create_database (argc, argv));

    // Test database schema (aka database namespace).
    //
    using ns::object2;

    object2 o2;
    o2.id = "aaa";
    o2.nums.push_back (1);
    o2.nums.push_back (2);
    o2.nums.push_back (3);
    o2.obj1 = new object1;
    o2.obj1->str = "aaa";

    {
      transaction t (db->begin ());
      db->persist (o2.obj1);
      db->persist (o2);
      t.commit ();
    }

    {
      transaction t (db->begin ());
      auto_ptr<object2> p2 (db->load<object2> ("aaa"));
      t.commit ();

      assert (o2 == *p2);
    }

    {
      typedef odb::query<object2> query;
      typedef odb::result<object2> result;

      transaction t (db->begin ());

      {
        result r (db->query<object2> (query::id == "aaa"));
        assert (size (r) == 1);
      }

      {
        result r (db->query<object2> (query::obj1->str == "aaa"));
        assert (size (r) == 1);
      }

      t.commit ();
    }

    {
      typedef odb::query<object_view> query;
      typedef odb::result<object_view> result;

      transaction t (db->begin ());

      result r (db->query<object_view> (query::object2::id == "aaa"));

      result::iterator i (r.begin ());
      assert (i != r.end ());
      assert (i->id2 == "aaa" && i->str == "aaa");
      assert (++i == r.end ());

      t.commit ();
    }

    {
      typedef odb::query<table_view> query;
      typedef odb::result<table_view> result;

      transaction t (db->begin ());

      result r (db->query<table_view> ());

      result::iterator i (r.begin ());
      assert (i != r.end ());
      assert (i->str == "aaa");
      assert (++i == r.end ());

      t.commit ();
    }
  }
  catch (const odb::exception& e)
  {
    cerr << e.what () << endl;
    return 1;
  }
}