aboutsummaryrefslogtreecommitdiff
path: root/schema/custom/driver.cxx
blob: cc1f3ea4ff751f2e09407a8eee434f9b4c19c95c (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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
// file      : schema/custom/driver.cxx
// author    : Boris Kolpackov <boris@codesynthesis.com>
// copyright : not copyrighted - public domain

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

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

#include "database.hxx" // create_database

#include "employee.hxx"
#include "employee-odb.hxx"

using namespace std;
using namespace odb::core;

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

    // Create the database schema.
    //
#if defined(DATABASE_MYSQL)  || \
    defined(DATABASE_SQLITE) || \
    defined(DATABASE_MSSQL)
    {

      // Due to bugs in SQLite foreign key support for DDL statements,
      // we need to temporarily disable foreign keys.
      //
      connection_ptr c (db->connection ());

#ifdef DATABASE_SQLITE
      c->execute ("PRAGMA foreign_keys=OFF");
#endif

      transaction t (c->begin ());

      // Try to drop the tables if they exist and ignore the error
      // if they don't.
      //
      try
      {
        db->execute ("DROP TABLE EmployeeDegree");
        db->execute ("DROP TABLE Employee");
        db->execute ("DROP TABLE Employer");
      }
      catch (const odb::exception&)
      {
      }

      db->execute (
        "CREATE TABLE Employer ("
        "name VARCHAR (255) NOT NULL PRIMARY KEY)");

      db->execute (
        "CREATE TABLE Employee ("
        "ssn INTEGER NOT NULL PRIMARY KEY,"
        "first_name VARCHAR (255) NOT NULL,"
        "last_name VARCHAR (255) NOT NULL,"
        "employer VARCHAR (255) NOT NULL REFERENCES Employer (name))");

      db->execute (
        "CREATE TABLE EmployeeDegree ("
        "ssn INTEGER NOT NULL REFERENCES Employee (ssn),"
        "degree VARCHAR (255) NOT NULL)");

      t.commit ();

#ifdef DATABASE_SQLITE
      c->execute ("PRAGMA foreign_keys=ON");
#endif
    }
#elif defined(DATABASE_PGSQL)
    {
      // PostgreSQL-specific SQL.
      //
      transaction t (db->begin ());

      db->execute ("DROP TABLE IF EXISTS \"Employer\" CASCADE");
      db->execute ("DROP TABLE IF EXISTS \"Employee\" CASCADE");
      db->execute ("DROP TABLE IF EXISTS \"EmployeeDegree\" CASCADE");

      db->execute (
        "CREATE TABLE \"Employer\" ("
        "name VARCHAR (255) NOT NULL PRIMARY KEY)");

      db->execute (
        "CREATE TABLE \"Employee\" ("
        "ssn INTEGER NOT NULL PRIMARY KEY,"
        "first_name VARCHAR (255) NOT NULL,"
        "last_name VARCHAR (255) NOT NULL,"
        "employer VARCHAR (255) NOT NULL)");

      db->execute (
        "CREATE TABLE \"EmployeeDegree\" ("
        "ssn INTEGER NOT NULL,"
        "degree VARCHAR (255) NOT NULL)");

      db->execute (
        "ALTER TABLE \"Employee\" "
        "ADD FOREIGN KEY (employer) "
        "REFERENCES \"Employer\" "
        "INITIALLY DEFERRED");

      db->execute (
        "ALTER TABLE \"EmployeeDegree\" "
        "ADD FOREIGN KEY (ssn) "
        "REFERENCES \"Employee\" "
        "INITIALLY DEFERRED");

      t.commit ();
    }
#elif defined(DATABASE_ORACLE)
    {
      // Oracle-specific PL/SQL.
      //
      transaction t (db->begin ());

      db->execute ("BEGIN "
                   "  EXECUTE IMMEDIATE "
                   "    'DROP TABLE \"Employer\" CASCADE CONSTRAINTS';"
                   "  EXCEPTION "
                   "    WHEN OTHERS THEN "
                   "      IF SQLCODE != -942 THEN RAISE; END IF;"
                   "END;");

      db->execute ("BEGIN "
                   "  EXECUTE IMMEDIATE "
                   "    'DROP TABLE \"Employee\" CASCADE CONSTRAINTS';"
                   "  EXCEPTION "
                   "    WHEN OTHERS THEN "
                   "      IF SQLCODE != -942 THEN RAISE; END IF;"
                   "END;");

      db->execute ("BEGIN "
                   "  EXECUTE IMMEDIATE 'DROP TABLE \"EmployeeDegree\"';"
                   "  EXCEPTION "
                   "    WHEN OTHERS THEN "
                   "      IF SQLCODE != -942 THEN RAISE; END IF;"
                   "END;");

      db->execute (
        "CREATE TABLE \"Employer\" ("
        "\"name\" VARCHAR (255) PRIMARY KEY)");

      db->execute (
        "CREATE TABLE \"Employee\" ("
        "\"ssn\" NUMBER(10) PRIMARY KEY,"
        "\"first_name\" VARCHAR (255) NOT NULL,"
        "\"last_name\" VARCHAR (255) NOT NULL,"
        "\"employer\" VARCHAR (255) NOT NULL)");

      db->execute (
        "CREATE TABLE \"EmployeeDegree\" ("
        "\"ssn\" NUMBER(10) NOT NULL,"
        "\"degree\" VARCHAR (255) NOT NULL)");

      db->execute (
        "ALTER TABLE \"Employee\" "
        "ADD FOREIGN KEY (\"employer\") "
        "REFERENCES \"Employer\" "
        "INITIALLY DEFERRED");

      db->execute (
        "ALTER TABLE \"EmployeeDegree\" "
        "ADD FOREIGN KEY (\"ssn\") "
        "REFERENCES \"Employee\" "
        "INITIALLY DEFERRED");

      t.commit ();
    }
#else
#  error unknown database
#endif

    // Create a few persistent objects.
    //
    {
      shared_ptr<employer> st (new employer ("Simple Tech Ltd"));

      shared_ptr<employee> john (new employee (1, "John", "Doe", st));
      shared_ptr<employee> jane (new employee (2, "Jane", "Doe", st));

      john->degrees ().push_back ("BA");
      john->degrees ().push_back ("MSc");
      jane->degrees ().push_back ("Ph.D.");

      transaction t (db->begin ());

      db->persist (st);
      db->persist (john);
      db->persist (jane);

      t.commit ();
    }

    // Load employees with "Doe" as the last name and print what we've got.
    //
    {
      typedef odb::query<employee> query;
      typedef odb::result<employee> result;

      session s;
      transaction t (db->begin ());

      result r (db->query<employee> (query::name.last == "Doe"));

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

        for (degrees::iterator j (i->degrees ().begin ());
             j != i->degrees ().end ();
             ++j)
        {
          cout << "  degree: " << *j << endl;
        }

        cout << endl;
      }

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