aboutsummaryrefslogtreecommitdiff
path: root/sqlite/attach/driver.cxx
blob: 25d279f0acc3f048c9bea4b91f7ac10aa9c1b8f4 (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
// file      : sqlite/attach/driver.cxx
// license   : GNU GPL v2; see accompanying LICENSE file

// Test attached database support.
//

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

#include <odb/schema-catalog.hxx>

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

#include <libcommon/common.hxx>

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

#undef NDEBUG
#include <cassert>

using namespace std;
namespace sqlite = odb::sqlite;
using namespace sqlite;

int
main (int argc, char* argv[])
{
  try
  {
    unique_ptr<database> mdb (create_specific_database<database> (argc, argv));

    {
      object o ("one");

      connection_ptr c (mdb->connection ());

      database adb (c, ":memory:", "adb");

      // Create schema similar to create_database().
      //
      {
        c->execute ("PRAGMA foreign_keys=OFF");

        transaction t (c->begin ());
        odb::schema_catalog::create_schema (adb);
        t.commit ();

        c->execute ("PRAGMA foreign_keys=ON");
      }

      {
        transaction t (c->begin ());
        mdb->persist (o);
        adb.persist (o);
        t.commit ();
      }

      {
        transaction t (c->begin ());
        unique_ptr<object> p (adb.load<object> (o.id));
        t.commit ();

        assert (p->str == o.str);
      }

      {
        o.str = "two";

        transaction t (c->begin ());
        adb.update (o);
        t.commit ();
      }

      {
        typedef sqlite::query<object> query;

        transaction t (c->begin ());
        unique_ptr<object> p (adb.query_one<object> (query::str == "two"));
        t.commit ();

        assert (p->str == o.str);
      }

      {
        transaction t (c->begin ());
        adb.erase (o);
        t.commit ();
      }

      {
        transaction t (c->begin ());
        unique_ptr<object> p (mdb->load<object> (o.id));
        t.commit ();

        assert (p.get () != 0);
      }

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