From 0a2b28b4034a6b44f7bb7a126935d9e58cc0c1f9 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Wed, 15 Jul 2015 18:43:03 +0200 Subject: Implement SQLite incremental BLOB/TEXT I/O --- sqlite/stream/driver.cxx | 280 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 280 insertions(+) create mode 100644 sqlite/stream/driver.cxx (limited to 'sqlite/stream/driver.cxx') diff --git a/sqlite/stream/driver.cxx b/sqlite/stream/driver.cxx new file mode 100644 index 0000000..5f708ba --- /dev/null +++ b/sqlite/stream/driver.cxx @@ -0,0 +1,280 @@ +// file : sqlite/stream/driver.cxx +// copyright : Copyright (c) 2009-2015 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +// Test SQLite BLOB/TEXT incremental I/O. +// + +#include +#include // std::auto_ptr +#include +#include + +#include +#include + +#include +#include + +#include + +#include "test.hxx" +#include "test-odb.hxx" + +using namespace std; +namespace sqlite = odb::sqlite; +using namespace sqlite; + +template +void +print (const S&) +{ + /* + cerr << s.db () << '.' + << s.table () << '.' + << s.column () << '#' + << s.rowid () << endl; + */ +} + +int +main (int argc, char* argv[]) +{ + try + { + auto_ptr db (create_specific_database (argc, argv)); + + string txt (1024 * 1024, 't'); + vector blb (1024 * 1024, 'b'); + + object o; + + { + transaction tx (db->begin ()); + + o.t.size (txt.size ()); + o.b.size (blb.size ()); + o.bv.push_back (blob (blb.size ())); + o.bv.push_back (blob (blb.size ())); + + db->persist (o); + + print (o.t); + print (o.b); + print (o.bv[0]); + print (o.bv[1]); + + blob_stream bs (o.b, true); + bs.write (blb.data (), blb.size ()); + + text_stream ts (o.t, true); + ts.write (txt.data (), txt.size ()); + + for (vector::iterator i (o.bv.begin ()); i != o.bv.end (); ++i) + { + blob_stream bs (*i, true); + bs.write (blb.data (), blb.size ()); + } + + tx.commit (); + } + + { + transaction tx (db->begin ()); + auto_ptr p (db->load (o.id)); + + print (p->t); + print (p->b); + print (p->bv[0]); + print (p->bv[1]); + + text_stream ts (p->t, false); + string t (ts.size (), '*'); + ts.read (const_cast (t.c_str ()), t.size ()); + assert (t == txt); + + blob_stream bs (p->b, false); + vector b (bs.size (), '\0'); + bs.read (b.data (), b.size ()); + assert (b == blb); + + for (vector::iterator i (p->bv.begin ()); i != p->bv.end (); ++i) + { + blob_stream bs (*i, false); + vector b (bs.size (), '\0'); + bs.read (b.data (), b.size ()); + assert (b == blb); + } + + assert (p->nb.null ()); + + tx.commit (); + } + + txt.resize (txt.size () + 1, 't'); + txt[0] = 'A'; + txt[txt.size () - 1] = 'Z'; + + blb.resize (blb.size () - 1); + blb.front () = 'A'; + blb.back () = 'Z'; + + { + transaction tx (db->begin ()); + + o.t.clear (); + o.t.size (txt.size ()); + + o.b.clear (); + o.b.size (blb.size ()); + + o.bv[0].clear (); + o.bv[0].size (blb.size ()); + + o.bv[1].clear (); + o.bv[1].size (blb.size ()); + + o.nb = blob (blb.size ()); + + db->update (o); + + print (o.t); + print (o.b); + print (o.bv[0]); + print (o.bv[1]); + print (*o.nb); + + { + text_stream ts (o.t, true); + ts.write (txt.data (), txt.size ()); + } + + { + blob_stream bs (o.b, true); + bs.write (blb.data (), blb.size ()); + } + + for (vector::iterator i (o.bv.begin ()); i != o.bv.end (); ++i) + { + blob_stream bs (*i, true); + bs.write (blb.data (), blb.size ()); + } + + { + blob_stream bs (*o.nb, true); + bs.write (blb.data (), blb.size ()); + } + + tx.commit (); + } + + { + transaction tx (db->begin ()); + auto_ptr p (db->load (o.id)); + + print (p->t); + print (p->b); + print (p->bv[0]); + print (p->bv[1]); + print (*p->nb); + + text_stream ts (p->t, false); + string t (ts.size (), '*'); + ts.read (const_cast (t.c_str ()), t.size ()); + assert (t == txt); + + blob_stream bs (p->b, false); + vector b (bs.size (), '\0'); + bs.read (b.data (), b.size ()); + assert (b == blb); + + for (vector::iterator i (p->bv.begin ()); i != p->bv.end (); ++i) + { + blob_stream bs (*i, false); + vector b (bs.size (), '\0'); + bs.read (b.data (), b.size ()); + assert (b == blb); + } + + { + blob_stream bs (*p->nb, false); + vector b (bs.size (), '\0'); + bs.read (b.data (), b.size ()); + assert (b == blb); + } + + tx.commit (); + } + + // Test query. + // + + txt.resize (32); + blb.resize (64); + + { + transaction tx (db->begin ()); + + o.t.size (txt.size ()); + o.b.size (blb.size ()); + o.bv.clear (); + o.nb.reset (); + + db->update (o); + + text_stream ts (o.t, true); + ts.write (txt.data (), txt.size ()); + + blob_stream bs (o.b, true); + bs.write (blb.data (), blb.size ()); + + tx.commit (); + } + + { + typedef sqlite::query query; + transaction tx (db->begin ()); + + { + object o1 (db->query_value (query::t == txt)); + + blob_stream bs (o1.b, false); + vector b (bs.size (), '\0'); + bs.read (b.data (), b.size ()); + assert (b == blb); + } + + { + object o1 (db->query_value (query::b == blb)); + + text_stream ts (o1.t, false); + string t (ts.size (), '*'); + ts.read (const_cast (t.c_str ()), t.size ()); + assert (t == txt); + } + + tx.commit (); + } + + // Test view. + // + { + typedef sqlite::query query; + transaction tx (db->begin ()); + + view v (db->query_value (query::t == txt)); + + blob_stream bs (v.b, false); + vector b (bs.size (), '\0'); + bs.read (b.data (), b.size ()); + assert (b == blb); + + tx.commit (); + } + } + catch (const odb::exception& e) + { + cerr << e.what () << endl; + return 1; + } +} -- cgit v1.1