aboutsummaryrefslogtreecommitdiff
path: root/query/driver.cxx
blob: fbd148d9741a790ea659a40f8e0b756c73a86de0 (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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// file      : query/driver.cxx
// copyright : not copyrighted - public domain

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

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

#include "database.hxx" // create_database

#include "person.hxx"
#include "person-odb.hxx"

using namespace std;

using odb::database;
using odb::transaction;

typedef odb::query<person> query;
typedef odb::result<person> result;

static void
print (result& r)
{
  for (result::iterator i (r.begin ()); i != r.end (); ++i)
  {
    cout << i->first () << " ";

    if (!i->middle ().null ())
      cout << i->middle ().get () << " ";

    cout << i->last () << " " << i->age () << endl;
  }

  cout << endl;
}

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

    {
      typedef vector<person> people;

      people p;

      p.push_back (person ("John", "Doe", 21));
      p.push_back (person ("John", "Smith", 22));
      p.push_back (person ("Jack", "Johnson", 31));
      p.push_back (person ("John", "JJ", "Jackson", 32));
      p.push_back (person ("Jane", "JD", "Doe", 23));
      p.push_back (person ("Jane", "JS", "Smith", 24));

      transaction t (db->begin ());

      for (people::iterator i (p.begin ()); i != p.end (); ++i)
        db->persist (*i);

      t.commit ();
    }

    // A simple query and result handling.
    //
    {
      transaction t (db->begin ());

      result r (db->query<person> (query::age < 30));

      for (result::iterator i (r.begin ()); i != r.end (); ++i)
      {
        cout << i->first () << " " << i->last () << " " << i->age () << endl;
      }

      // Alternatively we can get a dynamically-allocated object.
      //
      /*
      for (result::iterator i (r.begin ()); i != r.end (); ++i)
      {
        auto_ptr<person> p (i.load ());
        cout << p->first () << " " << p->last () << " " << p->age () << endl;
      }
      */

      // Or we can load the state into an existing object.
      //
      /*
      person p ("", "", 0);

      for (result::iterator i (r.begin ()); i != r.end (); ++i)
      {
        i.load (p);
        cout << p.first () << " " << p.last () << " " << p.age () << endl;
      }
      */

      cout << endl;

      t.commit ();
    }

    // Query that shows how to combine expressions with &&, ||, and !
    // as well as use paranthesis to control evaluation order.
    //
    {
      transaction t (db->begin ());

      result r (
        db->query<person> (
          (query::first == "John" || query::first == "Jane") &&
          !(query::last != "Doe" || query::age > 30)));

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

    // Query that shows how to use by-reference parameter binding.
    //
    {
      transaction t (db->begin ());

      unsigned short lower, upper;

      query q (query::age >= query::_ref (lower) &&
               query::age < query::_ref (upper));

      for (unsigned short band (0); band < 10; ++band)
      {
        lower = band * 10;
        upper = lower + 10;

        result r (db->query<person> (q));

        if (!r.empty ())
        {
          cout << lower << '-' << (upper - 1) << ':' << endl;
          print (r);
        }
      }

      t.commit ();
    }

    // Query that shows how to use the in() function.
    //
    {
      transaction t (db->begin ());

      result r (
        db->query<person> (
          query::last.in ("Smith", "Johnson", "Jockson")));

      print (r);

      t.commit ();
    }

    // The same query but using the in_range() function.
    //
    {
      vector<string> names;

      names.push_back ("Smith");
      names.push_back ("Johnson");
      names.push_back ("Jockson");

      transaction t (db->begin ());

      result r (
        db->query<person> (
          query::last.in_range (names.begin (), names.end ())));

      print (r);

      t.commit ();
    }

    // Query that shows how to test for NULL values using the
    // is_null()/is_not_null() functions.
    //
    {
      transaction t (db->begin ());
      result r (db->query<person> (query::middle.is_not_null ()));
      print (r);
      t.commit ();
    }

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