summaryrefslogtreecommitdiff
path: root/odb-tests/sqlite/stream
diff options
context:
space:
mode:
Diffstat (limited to 'odb-tests/sqlite/stream')
-rw-r--r--odb-tests/sqlite/stream/buildfile34
-rw-r--r--odb-tests/sqlite/stream/driver.cxx281
-rw-r--r--odb-tests/sqlite/stream/test.hxx37
-rw-r--r--odb-tests/sqlite/stream/testscript9
4 files changed, 361 insertions, 0 deletions
diff --git a/odb-tests/sqlite/stream/buildfile b/odb-tests/sqlite/stream/buildfile
new file mode 100644
index 0000000..e5baeb5
--- /dev/null
+++ b/odb-tests/sqlite/stream/buildfile
@@ -0,0 +1,34 @@
+# file : sqlite/stream/buildfile
+# license : GNU GPL v2; see accompanying LICENSE file
+
+if ($build.meta_operation != 'dist')
+{
+ assert ($sqlite) "sqlite should be configured for this test"
+ assert (!$multi) "multi-database mode is not supported by this test"
+}
+
+import libodb = libodb%lib{odb}
+import libodb_sqlite = libodb-sqlite%lib{odb-sqlite}
+import libs = lib{common}
+
+exe{driver}: {hxx cxx}{* -*-odb} {hxx ixx cxx}{test-odb} testscript
+
+# Introduce the metadata library target to make sure the libodb and
+# libodb-sqlite libraries are resolved for the odb_compile ad hoc rule (see
+# build/root.build for details).
+#
+libue{test-meta}: $libodb $libodb_sqlite
+
+<{hxx ixx cxx}{test-odb}>: hxx{test} libue{test-meta}
+
+exe{driver}: libue{test-meta} $libs
+
+# Specify the ODB custom options to be used by the odb_compile ad hoc rule
+# (see build/root.build for details).
+#
+odb_options = --table-prefix sqlitex_stream_ \
+ --generate-schema \
+ --default-database common \
+ --generate-query
+
+cxx.poptions =+ "-I$out_base" "-I$src_base"
diff --git a/odb-tests/sqlite/stream/driver.cxx b/odb-tests/sqlite/stream/driver.cxx
new file mode 100644
index 0000000..86522ec
--- /dev/null
+++ b/odb-tests/sqlite/stream/driver.cxx
@@ -0,0 +1,281 @@
+// file : sqlite/stream/driver.cxx
+// license : GNU GPL v2; see accompanying LICENSE file
+
+// Test SQLite BLOB/TEXT incremental I/O.
+//
+
+#include <vector>
+#include <memory> // std::unique_ptr
+#include <iostream>
+
+#include <odb/sqlite/database.hxx>
+#include <odb/sqlite/transaction.hxx>
+
+#include <odb/sqlite/text-stream.hxx>
+#include <odb/sqlite/blob-stream.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;
+
+template <typename S>
+void
+print (const S&)
+{
+ /*
+ cerr << s.db () << '.'
+ << s.table () << '.'
+ << s.column () << '#'
+ << s.rowid () << endl;
+ */
+}
+
+int
+main (int argc, char* argv[])
+{
+ try
+ {
+ unique_ptr<database> db (create_specific_database<database> (argc, argv));
+
+ string txt (1024 * 1024, 't');
+ vector<char> 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<blob>::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 ());
+ unique_ptr<object> p (db->load<object> (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<char*> (t.c_str ()), t.size ());
+ assert (t == txt);
+
+ blob_stream bs (p->b, false);
+ vector<char> b (bs.size (), '\0');
+ bs.read (b.data (), b.size ());
+ assert (b == blb);
+
+ for (vector<blob>::iterator i (p->bv.begin ()); i != p->bv.end (); ++i)
+ {
+ blob_stream bs (*i, false);
+ vector<char> 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<blob>::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 ());
+ unique_ptr<object> p (db->load<object> (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<char*> (t.c_str ()), t.size ());
+ assert (t == txt);
+
+ blob_stream bs (p->b, false);
+ vector<char> b (bs.size (), '\0');
+ bs.read (b.data (), b.size ());
+ assert (b == blb);
+
+ for (vector<blob>::iterator i (p->bv.begin ()); i != p->bv.end (); ++i)
+ {
+ blob_stream bs (*i, false);
+ vector<char> b (bs.size (), '\0');
+ bs.read (b.data (), b.size ());
+ assert (b == blb);
+ }
+
+ {
+ blob_stream bs (*p->nb, false);
+ vector<char> 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<object> query;
+ transaction tx (db->begin ());
+
+ {
+ object o1 (db->query_value<object> (query::t == txt));
+
+ blob_stream bs (o1.b, false);
+ vector<char> b (bs.size (), '\0');
+ bs.read (b.data (), b.size ());
+ assert (b == blb);
+ }
+
+ {
+ object o1 (db->query_value<object> (query::b == blb));
+
+ text_stream ts (o1.t, false);
+ string t (ts.size (), '*');
+ ts.read (const_cast<char*> (t.c_str ()), t.size ());
+ assert (t == txt);
+ }
+
+ tx.commit ();
+ }
+
+ // Test view.
+ //
+ {
+ typedef sqlite::query<view> query;
+ transaction tx (db->begin ());
+
+ view v (db->query_value<view> (query::t == txt));
+
+ blob_stream bs (v.b, false);
+ vector<char> 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;
+ }
+}
diff --git a/odb-tests/sqlite/stream/test.hxx b/odb-tests/sqlite/stream/test.hxx
new file mode 100644
index 0000000..9189a87
--- /dev/null
+++ b/odb-tests/sqlite/stream/test.hxx
@@ -0,0 +1,37 @@
+// file : sqlite/stream/test.hxx
+// license : GNU GPL v2; see accompanying LICENSE file
+
+#ifndef TEST_HXX
+#define TEST_HXX
+
+#include <vector>
+
+#include <odb/core.hxx>
+#include <odb/nullable.hxx>
+
+#include <odb/sqlite/text.hxx>
+#include <odb/sqlite/blob.hxx>
+
+#pragma db object
+struct object
+{
+ #pragma db id auto
+ unsigned long id;
+
+ odb::sqlite::text t;
+
+ #pragma db column("_123foo_bar")
+ odb::sqlite::blob b;
+
+ std::vector<odb::sqlite::blob> bv;
+
+ odb::nullable<odb::sqlite::blob> nb;
+};
+
+#pragma db view object(object)
+struct view
+{
+ odb::sqlite::blob b;
+};
+
+#endif // TEST_HXX
diff --git a/odb-tests/sqlite/stream/testscript b/odb-tests/sqlite/stream/testscript
new file mode 100644
index 0000000..2b9f019
--- /dev/null
+++ b/odb-tests/sqlite/stream/testscript
@@ -0,0 +1,9 @@
+# file : sqlite/stream/testscript
+# license : GNU GPL v2; see accompanying LICENSE file
+
+.include ../../database-options.testscript
+.include ../../sqlite.testscript
+
+: basics
+:
+$*