aboutsummaryrefslogtreecommitdiff
path: root/mysql/truncation/driver.cxx
blob: 5222ce3c88760af1f439ee4ecc23cb00a5023adf (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
// file      : mysql/truncation/driver.cxx
// copyright : Copyright (c) 2009-2015 Code Synthesis Tools CC
// license   : GNU GPL v2; see accompanying LICENSE file

// Test insufficient buffer/truncation handling.
//

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

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

#include <common/common.hxx>

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

using namespace std;
namespace mysql = odb::mysql;
using namespace mysql;

int
main (int argc, char* argv[])
{
  // The default pre-allocated buffer is 256 bytes long.
  //
  string long_str (300, 'c'); // This will get the buffer to 512
  string longer_str (1025, 'b');

  try
  {
    // Test basic operations.
    //
    {
      auto_ptr<database> db (create_specific_database<database> (argc, argv));

      // Run persist/load so that the initial bindings are established
      // (version == 0).
      //
      {
        object1 o (1);
        o.str_ = "test string";

        transaction t (db->begin ());
        db->persist (o);
        db->load (1, o);
        t.commit ();
      }

      {
        object2 o (2);
        o.str_ = "test string";

        transaction t (db->begin ());
        db->persist (o);
        db->load (2, o);
        t.commit ();
      }

      // Store/load the long string which should trigger buffer growth.
      //
      {
        object1 o (3);
        o.str_ = long_str;

        transaction t (db->begin ());
        db->persist (o);
        t.commit ();
      }

      {
        transaction t (db->begin ());
        auto_ptr<object2> o (db->load<object2> (3));
        assert (o->str_ == long_str);
        t.commit ();
      }

      // Store/load longer string.
      //
      {
        object1 o (3);
        o.str_ = longer_str;

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

      {
        transaction t (db->begin ());
        auto_ptr<object2> o (db->load<object2> (3));
        assert (o->str_ == longer_str);
        t.commit ();
      }
    }

    // Test query.
    //
    {
      typedef mysql::query<object1> query;
      typedef odb::result<object1> result;

      auto_ptr<database> db (create_specific_database<database> (argc, argv));

      // Run persist/query so that the initial bindings are established
      // (version == 0).
      //
      {
        object1 o (20);
        o.str_ = "test string";

        transaction t (db->begin ());
        db->persist (o);
        o.id_++;
        db->persist (o);
        o.id_++;
        db->persist (o);

        result r (db->query<object1> (query::id == 20));
        assert (r.begin ()->id_ == 20);
        t.commit ();
      }

      // Test buffer growth with cached result.
      //
      {
        object1 o;

        transaction t (db->begin ());

        result r (db->query<object1> (query::id >= 20));
        result::iterator i (r.begin ());

        o.id_ = i->id_;
        o.str_ = long_str;

        // This forces buffer growth in the middle of result iteration.
        //
        db->update (o);

        ++i;
        assert (i->str_ == "test string");

        o.id_ = i->id_;
        o.str_ = longer_str;
        db->update (o);

        ++i;
        assert (i->str_ == "test string");

        t.commit ();
      }
    }

    // Test containers.
    //
    {
      auto_ptr<database> db (create_specific_database<database> (argc, argv));

      // Use different connections to persist and load the object.
      //
      connection_ptr c1 (db->connection ());
      connection_ptr c2 (db->connection ());

      container o (1);
      o.vec_.push_back (string (513, 'x'));

      {
        transaction t (c1->begin ());
        db->persist (o);
        t.commit ();
      }

      {
        transaction t (c2->begin ());
        auto_ptr<container> p (db->load<container> (1));
        t.commit ();

        assert (p->vec_ == o.vec_);
      }
    }
  }
  catch (const odb::exception& e)
  {
    cerr << e.what () << endl;
    return 1;
  }
}