aboutsummaryrefslogtreecommitdiff
path: root/common/query/driver.cxx
blob: 9a8300a131f2b7cbecd9bc6628610772d2a196f4 (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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// file      : common/query/driver.cxx
// author    : Boris Kolpackov <boris@codesynthesis.com>
// copyright : Copyright (c) 2009-2010 Code Synthesis Tools CC
// license   : GNU GPL v2; see accompanying LICENSE file

// Test query support.
//

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

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

#include <common.hxx>

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

using namespace std;
using namespace odb;

using odb::shared_ptr;

void
print (result<person>& r)
{
  for (result<person>::iterator i (r.begin ()); i != r.end (); ++i)
  {
    auto_ptr<person> o (*i);
    cout << *o << endl;
  }
}

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

    //
    //
    {
      person p1 (1, "John", "Doe", 30);
      person p2 (2, "Jane", "Doe", 29);
      person p3 (3, "Joe", "Dirt", 31);

      transaction t (db->begin_transaction ());
      db->persist (p1);
      db->persist (p2);
      db->persist (p3);
      t.commit ();
    }

    // Compilation tests.
    //
    if (false)
    {
      typedef odb::query<person> query;

      string name;
      unsigned short age;

      db->query<person> ("age = " + query::_ref (age));
      db->query<person> ("age = " + query::_val (age));

      query age_q (query::_ref (age) + " = age");
      query name_q ("first_name = " + query::_val (name));
      query q (age_q + "AND" + name_q);

      db->query (q);
      db->query<person> (age_q + "OR" +
                         name_q + "OR" +
                         "age < " + query::_ref (age));
    }

    // Select-all query.
    //
    cout << "test 001" << endl;
    {
      transaction t (db->begin_transaction ());
      result<person> r (db->query<person> ());

      for (result<person>::iterator i (r.begin ()); i != r.end (); ++i)
      {
        person p;
        i.load (p);
        cout << p << endl;
      }

      t.commit ();
    }

    // Select-all query with order by.
    //
    cout << "test 002" << endl;
    {
      transaction t (db->begin_transaction ());
      result<person> r (db->query<person> ("ORDER BY age"));
      print (r);
      t.commit ();
    }

    // String query.
    //
    cout << "test 003" << endl;
    {
      transaction t (db->begin_transaction ());
      result<person> r (db->query<person> ("age >= 30 AND last_name = 'Doe'"));
      print (r);
      t.commit ();
    }

    typedef odb::query<person> query;

    // Value binding.
    //
    cout << "test 004" << endl;
    {
      transaction t (db->begin_transaction ());

      const char* name = "Doe";

      result<person> r (
        db->query<person> (
          "age >= " + query::_ref (30) + "AND" +
          "last_name = " + query::_val (name)));

      print (r);
      t.commit ();
    }

    // Reference binding.
    //
    cout << "test 005" << endl;
    {
      transaction t (db->begin_transaction ());

      string name;
      unsigned short age;

      query q ("age >= " + query::_ref (age) + "AND" +
               "last_name = " + query::_ref (name));

      name = "Doe";
      age = 30;
      result<person> r (db->query<person> (q));
      print (r);

      name = "Dirt";
      age = 31;
      r = db->query<person> (q);
      print (r);

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