diff options
Diffstat (limited to 'mysql/truncation/driver.cxx')
-rw-r--r-- | mysql/truncation/driver.cxx | 165 |
1 files changed, 114 insertions, 51 deletions
diff --git a/mysql/truncation/driver.cxx b/mysql/truncation/driver.cxx index 46350d6..76985f0 100644 --- a/mysql/truncation/driver.cxx +++ b/mysql/truncation/driver.cxx @@ -24,72 +24,135 @@ using namespace odb; int main (int argc, char* argv[]) { + // The default pre-allocated buffer is 512 bytes long. + // + string long_str (640, 'c'); // This will get the buffer to 1024 + string longer_str (1025, 'b'); + try { - auto_ptr<database> db (create_database (argc, argv)); - - // The default pre-allocated buffer is 512 bytes long. + // Test basic operations. // - string long_str (640, 'c'); // This will get the buffer to 1024 - string longer_str (1025, 'b'); + { + auto_ptr<database> db (create_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_transaction ()); + db->persist (o); + db->load (1, o); + t.commit (); + } + + { + object2 o (2); + o.str_ = "test string"; + + transaction t (db->begin_transaction ()); + 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_transaction ()); + db->persist (o); + t.commit (); + } + + { + transaction t (db->begin_transaction ()); + 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_transaction ()); + db->store (o); + t.commit (); + } + + { + transaction t (db->begin_transaction ()); + auto_ptr<object2> o (db->load<object2> (3)); + assert (o->str_ == longer_str); + t.commit (); + } + } - // Run persist/load so that the initial bindings are established - // (version == 0). + // Test query. // { - object1 o (1); - o.str_ = "test string"; + typedef odb::query<object1> query; + typedef odb::result<object1> result; - transaction t (db->begin_transaction ()); - db->persist (o); - db->load (1, o); - t.commit (); - } + auto_ptr<database> db (create_database (argc, argv)); - { - object2 o (2); - o.str_ = "test string"; + // Run persist/query so that the initial bindings are established + // (version == 0). + // + { + object1 o (20); + o.str_ = "test string"; - transaction t (db->begin_transaction ()); - db->persist (o); - db->load (2, o); - t.commit (); - } + transaction t (db->begin_transaction ()); + db->persist (o); + o.id_++; + db->persist (o); + o.id_++; + db->persist (o); - // Store/load the long string which should trigger buffer growth. - // - { - object1 o (3); - o.str_ = long_str; + result r (db->query<object1> (query::id == 20)); + assert (r.begin ()->id_ == 20); + t.commit (); + } - transaction t (db->begin_transaction ()); - db->persist (o); - t.commit (); - } + // Test buffer growth with cached result. + // + { + object1 o; - { - transaction t (db->begin_transaction ()); - auto_ptr<object2> o (db->load<object2> (3)); - assert (o->str_ == long_str); - t.commit (); - } + transaction t (db->begin_transaction ()); - // Store/load longer string. - // - { - object1 o (3); - o.str_ = longer_str; + result r (db->query<object1> (query::id >= 20)); + r.cache (); + result::iterator i (r.begin ()); - transaction t (db->begin_transaction ()); - db->store (o); - t.commit (); - } + o.id_ = i->id_; + o.str_ = long_str; - { - transaction t (db->begin_transaction ()); - auto_ptr<object2> o (db->load<object2> (3)); - assert (o->str_ == longer_str); - t.commit (); + // This forces buffer growth in the middle of result iteration. + // + db->store (o); + + ++i; + assert (i->str_ == "test string"); + + o.id_ = i->id_; + o.str_ = longer_str; + db->store (o); + + ++i; + assert (i->str_ == "test string"); + + t.commit (); + } } } catch (const odb::exception& e) |