diff options
Diffstat (limited to 'odb-tests/sqlite')
35 files changed, 1709 insertions, 0 deletions
diff --git a/odb-tests/sqlite/attach/buildfile b/odb-tests/sqlite/attach/buildfile new file mode 100644 index 0000000..959f050 --- /dev/null +++ b/odb-tests/sqlite/attach/buildfile @@ -0,0 +1,32 @@ +# file : sqlite/attach/buildfile +# license : GNU GPL v2; see accompanying LICENSE file + +assert ($sqlite && !$multi || $build.meta_operation == 'dist') \ +"sqlite should be configured via config.odb_tests.database variable as a single database" + +import libodb = libodb%lib{odb} + +import libs = 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 library is +# resolved for the odb_compile ad hoc rule (see build/root.build for details). +# +libue{test-meta}: $libodb + +<{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_attach_ \ + --generate-schema \ + --default-database common \ + --generate-query \ + --schema main + +cxx.poptions =+ "-I$out_base" "-I$src_base" diff --git a/odb-tests/sqlite/attach/driver.cxx b/odb-tests/sqlite/attach/driver.cxx new file mode 100644 index 0000000..25d279f --- /dev/null +++ b/odb-tests/sqlite/attach/driver.cxx @@ -0,0 +1,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; + } +} diff --git a/odb-tests/sqlite/attach/test.hxx b/odb-tests/sqlite/attach/test.hxx new file mode 100644 index 0000000..b3b8f63 --- /dev/null +++ b/odb-tests/sqlite/attach/test.hxx @@ -0,0 +1,32 @@ +// file : sqlite/attach/test.hxx +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef TEST_HXX +#define TEST_HXX + +#include <odb/core.hxx> +#include <odb/nullable.hxx> + +#include <string> + +#include <odb/core.hxx> + +#pragma db object +struct object +{ + explicit + object (const std::string& s): str (s) {} + + #pragma db id auto + unsigned long id; + + #pragma db unique + std::string str; + +private: + object () {} + + friend class odb::access; +}; + +#endif // TEST_HXX diff --git a/odb-tests/sqlite/attach/testscript b/odb-tests/sqlite/attach/testscript new file mode 100644 index 0000000..db5089d --- /dev/null +++ b/odb-tests/sqlite/attach/testscript @@ -0,0 +1,9 @@ +# file : sqlite/attach/testscript +# license : GNU GPL v2; see accompanying LICENSE file + +.include ../../database-options.testscript +.include ../../sqlite.testscript + +: basics +: +$* diff --git a/odb-tests/sqlite/auto/buildfile b/odb-tests/sqlite/auto/buildfile new file mode 100644 index 0000000..2c70b7c --- /dev/null +++ b/odb-tests/sqlite/auto/buildfile @@ -0,0 +1,32 @@ +# file : sqlite/auto/buildfile +# license : GNU GPL v2; see accompanying LICENSE file + +assert ($sqlite && !$multi || $build.meta_operation == 'dist') \ +"sqlite should be configured via config.odb_tests.database variable as a single database" + +import libodb = libodb%lib{odb} + +import libs = 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 library is +# resolved for the odb_compile ad hoc rule (see build/root.build for details). +# +libue{test-meta}: $libodb + +<{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_auto_ \ + --generate-schema \ + --default-database common \ + --generate-query \ + --sqlite-lax-auto-id + +cxx.poptions =+ "-I$out_base" "-I$src_base" diff --git a/odb-tests/sqlite/auto/driver.cxx b/odb-tests/sqlite/auto/driver.cxx new file mode 100644 index 0000000..bcd42c1 --- /dev/null +++ b/odb-tests/sqlite/auto/driver.cxx @@ -0,0 +1,76 @@ +// file : sqlite/auto/driver.cxx +// license : GNU GPL v2; see accompanying LICENSE file + +// Test manual/automatic id assignment. +// + +#include <memory> // std::unique_ptr +#include <iostream> + +#include <odb/sqlite/database.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> db (create_specific_database<database> (argc, argv)); + + // object + // + { + unsigned long id1, id2, id3; + { + object o1 (1, "one"); + object o2 ("two"); + object o3 (3, "three"); + + transaction t (db->begin ()); + db->persist (o1); + db->persist (o2); + db->persist (o3); + t.commit (); + + id1 = *o1.id_; + id2 = *o2.id_; + id3 = *o3.id_; + + assert (id1 == 1); + assert (id3 == 3); + + assert (id2 != id1); + assert (id2 != id3); + } + + { + transaction t (db->begin ()); + unique_ptr<object> o1 (db->load<object> (id1)); + unique_ptr<object> o2 (db->load<object> (id2)); + unique_ptr<object> o3 (db->load<object> (id3)); + t.commit (); + + assert (*o1->id_ == id1 && o1->str_ == "one"); + assert (*o2->id_ == id2 && o2->str_ == "two"); + assert (*o3->id_ == id3 && o3->str_ == "three"); + } + } + } + catch (const odb::exception& e) + { + cerr << e.what () << endl; + return 1; + } +} diff --git a/odb-tests/sqlite/auto/test.hxx b/odb-tests/sqlite/auto/test.hxx new file mode 100644 index 0000000..9905182 --- /dev/null +++ b/odb-tests/sqlite/auto/test.hxx @@ -0,0 +1,32 @@ +// file : sqlite/auto/test.hxx +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef TEST_HXX +#define TEST_HXX + +#include <odb/core.hxx> +#include <odb/nullable.hxx> + +#include <string> + +#include <odb/core.hxx> + +#pragma db object +struct object +{ + explicit + object (const std::string& str): str_ (str) {} + object (unsigned long id, const std::string& str): id_ (id), str_ (str) {} + + #pragma db auto id + odb::nullable<unsigned long> id_; + + std::string str_; + +private: + object () {} + + friend class odb::access; +}; + +#endif // TEST_HXX diff --git a/odb-tests/sqlite/auto/testscript b/odb-tests/sqlite/auto/testscript new file mode 100644 index 0000000..a5a7da5 --- /dev/null +++ b/odb-tests/sqlite/auto/testscript @@ -0,0 +1,9 @@ +# file : sqlite/auto/testscript +# license : GNU GPL v2; see accompanying LICENSE file + +.include ../../database-options.testscript +.include ../../sqlite.testscript + +: basics +: +$* diff --git a/odb-tests/sqlite/custom/buildfile b/odb-tests/sqlite/custom/buildfile new file mode 100644 index 0000000..3325762 --- /dev/null +++ b/odb-tests/sqlite/custom/buildfile @@ -0,0 +1,31 @@ +# file : sqlite/custom/buildfile +# license : GNU GPL v2; see accompanying LICENSE file + +assert ($sqlite && !$multi || $build.meta_operation == 'dist') \ +"sqlite should be configured via config.odb_tests.database variable as a single database" + +import libodb = libodb%lib{odb} + +import libs = 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 library is +# resolved for the odb_compile ad hoc rule (see build/root.build for details). +# +libue{test-meta}: $libodb + +<{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_custom_ \ + --generate-schema \ + --default-database common \ + --generate-query + +cxx.poptions =+ "-I$out_base" "-I$src_base" diff --git a/odb-tests/sqlite/custom/driver.cxx b/odb-tests/sqlite/custom/driver.cxx new file mode 100644 index 0000000..0627708 --- /dev/null +++ b/odb-tests/sqlite/custom/driver.cxx @@ -0,0 +1,78 @@ +// file : sqlite/custom/driver.cxx +// license : GNU GPL v2; see accompanying LICENSE file + +// Test custom database type mapping in SQLite. +// + +#include <memory> // std::unique_ptr +#include <iostream> + +#include <odb/sqlite/database.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> db (create_specific_database<database> (argc, argv)); + + object o (1); + o.nv.push_back ("123"); // INTEGER + o.nv.push_back ("1.23"); // REAL + o.nv.push_back ("abc"); // TEXT + + // Persist. + // + { + transaction t (db->begin ()); + db->persist (o); + t.commit (); + } + + // Load. + // + { + transaction t (db->begin ()); + unique_ptr<object> o1 (db->load<object> (1)); + t.commit (); + + assert (o == *o1); + } + + // Update. + // + o.nv[1] += "4"; + + { + transaction t (db->begin ()); + db->update (o); + t.commit (); + } + + { + transaction t (db->begin ()); + unique_ptr<object> o1 (db->load<object> (1)); + t.commit (); + + assert (o == *o1); + } + } + catch (const odb::exception& e) + { + cerr << e.what () << endl; + return 1; + } +} diff --git a/odb-tests/sqlite/custom/test.hxx b/odb-tests/sqlite/custom/test.hxx new file mode 100644 index 0000000..54a2ba3 --- /dev/null +++ b/odb-tests/sqlite/custom/test.hxx @@ -0,0 +1,39 @@ +// file : sqlite/custom/test.hxx +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef TEST_HXX +#define TEST_HXX + +#include <string> +#include <vector> + +#include <odb/core.hxx> + +// Map NUMERIC SQLite type to std::string (or any other type that +// provides the value_traits<?, id_string> specialization). By +// default ODB treats NUMERIC as REAL. Note also that we don't +// need to specify to/from conversions since SQLite will convert +// implicitly. +// +#pragma db map type("NUMERIC") as("TEXT") + +#pragma db object +struct object +{ + object () {} + object (unsigned long id_): id (id_) {} + + #pragma db id + unsigned long id; + + #pragma db value_type("NUMERIC") + std::vector<std::string> nv; + + bool + operator== (const object& y) const + { + return id == y.id && nv == y.nv; + } +}; + +#endif // TEST_HXX diff --git a/odb-tests/sqlite/custom/testscript b/odb-tests/sqlite/custom/testscript new file mode 100644 index 0000000..53561ae --- /dev/null +++ b/odb-tests/sqlite/custom/testscript @@ -0,0 +1,9 @@ +# file : sqlite/custom/testscript +# license : GNU GPL v2; see accompanying LICENSE file + +.include ../../database-options.testscript +.include ../../sqlite.testscript + +: basics +: +$* diff --git a/odb-tests/sqlite/database/buildfile b/odb-tests/sqlite/database/buildfile new file mode 100644 index 0000000..e74ab1a --- /dev/null +++ b/odb-tests/sqlite/database/buildfile @@ -0,0 +1,11 @@ +# file : sqlite/database/buildfile +# license : GNU GPL v2; see accompanying LICENSE file + +assert ($sqlite && !$multi || $build.meta_operation == 'dist') \ +"sqlite should be configured via config.odb_tests.database variable as a single database" + +import libs = libodb-sqlite%lib{odb-sqlite} + +exe{driver}: {hxx cxx}{*} $libs testscript + +cxx.poptions =+ "-I$out_base" "-I$src_base" diff --git a/odb-tests/sqlite/database/driver.cxx b/odb-tests/sqlite/database/driver.cxx new file mode 100644 index 0000000..5e16653 --- /dev/null +++ b/odb-tests/sqlite/database/driver.cxx @@ -0,0 +1,40 @@ +// file : sqlite/database/driver.cxx +// license : GNU GPL v2; see accompanying LICENSE file + +// Test that database constructors are unambiguous and some other things. +// + +#include <odb/sqlite/database.hxx> + +#undef NDEBUG +#include <cassert> + +namespace sqlite = odb::sqlite; +using namespace sqlite; + +int +main (int argc, char* argv[]) +{ + // Test UTF-16 to UTF-8 conversion. + // +#ifdef _WIN32 + { + database d (L"t\x00C8st"); + assert (d.name () == "t\xC3\x88st"); + } +#endif + + // This code should not execute. + // + if (argc != 0) + return 0; + + { + database d1 ("db1"); + } + + { + database d1 (argc, argv); + database d2 (argc, argv, false); + } +} diff --git a/odb-tests/sqlite/database/testscript b/odb-tests/sqlite/database/testscript new file mode 100644 index 0000000..b9c0f5f --- /dev/null +++ b/odb-tests/sqlite/database/testscript @@ -0,0 +1,6 @@ +# file : sqlite/database/testscript +# license : GNU GPL v2; see accompanying LICENSE file + +: basics +: +$* diff --git a/odb-tests/sqlite/native/buildfile b/odb-tests/sqlite/native/buildfile new file mode 100644 index 0000000..49aa05b --- /dev/null +++ b/odb-tests/sqlite/native/buildfile @@ -0,0 +1,12 @@ +# file : sqlite/native/buildfile +# license : GNU GPL v2; see accompanying LICENSE file + +assert ($sqlite && !$multi || $build.meta_operation == 'dist') \ +"sqlite should be configured via config.odb_tests.database variable as a single database" + +import libs = libodb-sqlite%lib{odb-sqlite} +import libs += lib{common} + +exe{driver}: {hxx cxx}{*} $libs testscript + +cxx.poptions =+ "-I$out_base" "-I$src_base" diff --git a/odb-tests/sqlite/native/driver.cxx b/odb-tests/sqlite/native/driver.cxx new file mode 100644 index 0000000..c87aa5d --- /dev/null +++ b/odb-tests/sqlite/native/driver.cxx @@ -0,0 +1,74 @@ +// file : sqlite/native/driver.cxx +// license : GNU GPL v2; see accompanying LICENSE file + +// Test native SQL execution. +// + +#include <memory> // std::unique_ptr +#include <iostream> + +#include <odb/sqlite/database.hxx> +#include <odb/sqlite/transaction.hxx> + +#include <libcommon/common.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> db ( + create_specific_database<database> (argc, argv, false)); + + // Create the database schema. + // + { + transaction t (db->begin ()); + + db->execute ("DROP TABLE IF EXISTS sqlitex_native_test"); + db->execute ("CREATE TABLE sqlitex_native_test (n INTEGER PRIMARY KEY)"); + + t.commit (); + } + + // Insert a few rows. + // + { + transaction t (db->begin ()); + + assert ( + db->execute ("INSERT INTO sqlitex_native_test (n) VALUES (1)") == 1); + + assert ( + db->execute ("INSERT INTO sqlitex_native_test (n) VALUES (2)") == 1); + + t.commit (); + } + + // Select a few rows. + // + { + transaction t (db->begin ()); + + assert ( + db->execute ("SELECT n FROM sqlitex_native_test WHERE n < 3") == 2); + + assert ( + db->execute ("SELECT n FROM sqlitex_native_test WHERE n > 3") == 0); + + t.commit (); + } + } + catch (const odb::exception& e) + { + cerr << e.what () << endl; + return 1; + } +} diff --git a/odb-tests/sqlite/native/testscript b/odb-tests/sqlite/native/testscript new file mode 100644 index 0000000..16f05e0 --- /dev/null +++ b/odb-tests/sqlite/native/testscript @@ -0,0 +1,9 @@ +# file : sqlite/native/testscript +# license : GNU GPL v2; see accompanying LICENSE file + +.include ../../database-options.testscript +.include ../../sqlite.testscript + +: basics +: +$* diff --git a/odb-tests/sqlite/stream/buildfile b/odb-tests/sqlite/stream/buildfile new file mode 100644 index 0000000..fb8f605 --- /dev/null +++ b/odb-tests/sqlite/stream/buildfile @@ -0,0 +1,31 @@ +# file : sqlite/stream/buildfile +# license : GNU GPL v2; see accompanying LICENSE file + +assert ($sqlite && !$multi || $build.meta_operation == 'dist') \ +"sqlite should be configured via config.odb_tests.database variable as a single database" + +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 +: +$* diff --git a/odb-tests/sqlite/transaction/buildfile b/odb-tests/sqlite/transaction/buildfile new file mode 100644 index 0000000..e29092d --- /dev/null +++ b/odb-tests/sqlite/transaction/buildfile @@ -0,0 +1,30 @@ +# file : sqlite/transaction/buildfile +# license : GNU GPL v2; see accompanying LICENSE file + +assert ($sqlite && !$multi || $build.meta_operation == 'dist') \ +"sqlite should be configured via config.odb_tests.database variable as a single database" + +import libodb = libodb%lib{odb} + +import libs = 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 library is +# resolved for the odb_compile ad hoc rule (see build/root.build for details). +# +libue{test-meta}: $libodb + +<{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_transaction_ \ + --generate-schema \ + --default-database common + +cxx.poptions =+ "-I$out_base" "-I$src_base" diff --git a/odb-tests/sqlite/transaction/driver.cxx b/odb-tests/sqlite/transaction/driver.cxx new file mode 100644 index 0000000..80a0804 --- /dev/null +++ b/odb-tests/sqlite/transaction/driver.cxx @@ -0,0 +1,61 @@ +// file : sqlite/transaction/driver.cxx +// license : GNU GPL v2; see accompanying LICENSE file + +// Test esoteric SQLite transaction semantics aspects. +// + +#include <memory> // std::unique_ptr +#include <iostream> + +#include <odb/sqlite/database.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> db (create_specific_database<database> (argc, argv)); + + // In SQLite, when a commit fails because of the deferred foreign + // key constraint violation, the transaction is not automatically + // rolled back. Make sure we compensate for that. + // + try + { + object o; + o.p = odb::lazy_ptr<object1> (*db, 0); + + transaction t (db->begin ()); + db->persist(o); + t.commit (); + } + catch (const odb::exception&) + { + } + + // Make sure we can start a new transaction. + // + { + transaction t (db->begin ()); + t.commit (); + } + } + catch (const odb::exception& e) + { + cerr << e.what () << endl; + return 1; + } +} diff --git a/odb-tests/sqlite/transaction/test.hxx b/odb-tests/sqlite/transaction/test.hxx new file mode 100644 index 0000000..bfb981b --- /dev/null +++ b/odb-tests/sqlite/transaction/test.hxx @@ -0,0 +1,26 @@ +// file : sqlite/transaction/test.hxx +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef TEST_HXX +#define TEST_HXX + +#include <odb/core.hxx> +#include <odb/lazy-ptr.hxx> + +#pragma db object +struct object1 +{ + #pragma db id + unsigned long id_; +}; + +#pragma db object +struct object +{ + #pragma db id auto + unsigned long id_; + + odb::lazy_ptr<object1> p; +}; + +#endif // TEST_HXX diff --git a/odb-tests/sqlite/transaction/testscript b/odb-tests/sqlite/transaction/testscript new file mode 100644 index 0000000..c3388cb --- /dev/null +++ b/odb-tests/sqlite/transaction/testscript @@ -0,0 +1,9 @@ +# file : sqlite/transaction/testscript +# license : GNU GPL v2; see accompanying LICENSE file + +.include ../../database-options.testscript +.include ../../sqlite.testscript + +: basics +: +$* diff --git a/odb-tests/sqlite/truncation/buildfile b/odb-tests/sqlite/truncation/buildfile new file mode 100644 index 0000000..9a014e6 --- /dev/null +++ b/odb-tests/sqlite/truncation/buildfile @@ -0,0 +1,31 @@ +# file : sqlite/truncation/buildfile +# license : GNU GPL v2; see accompanying LICENSE file + +assert ($sqlite && !$multi || $build.meta_operation == 'dist') \ +"sqlite should be configured via config.odb_tests.database variable as a single database" + +import libodb = libodb%lib{odb} + +import libs = 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 library is +# resolved for the odb_compile ad hoc rule (see build/root.build for details). +# +libue{test-meta}: $libodb + +<{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_truncation_ \ + --generate-schema \ + --default-database common \ + --generate-query + +cxx.poptions =+ "-I$out_base" "-I$src_base" diff --git a/odb-tests/sqlite/truncation/driver.cxx b/odb-tests/sqlite/truncation/driver.cxx new file mode 100644 index 0000000..a2f6d66 --- /dev/null +++ b/odb-tests/sqlite/truncation/driver.cxx @@ -0,0 +1,163 @@ +// file : sqlite/truncation/driver.cxx +// license : GNU GPL v2; see accompanying LICENSE file + +// Test insufficient buffer/truncation handling. +// + +#include <memory> // std::unique_ptr +#include <iostream> + +#include <odb/sqlite/database.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[]) +{ + // 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 + { + // Test basic operations. + // + { + unique_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 ()); + unique_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 ()); + unique_ptr<object2> o (db->load<object2> (3)); + assert (o->str_ == longer_str); + t.commit (); + } + } + + // Test query. + // + { + unique_ptr<database> db (create_specific_database<database> (argc, argv)); + + typedef sqlite::query<object1> query; + typedef odb::result<object1> result; + + // 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 (); + } + } + } + catch (const odb::exception& e) + { + cerr << e.what () << endl; + return 1; + } +} diff --git a/odb-tests/sqlite/truncation/test.hxx b/odb-tests/sqlite/truncation/test.hxx new file mode 100644 index 0000000..8ca44ea --- /dev/null +++ b/odb-tests/sqlite/truncation/test.hxx @@ -0,0 +1,46 @@ +// file : sqlite/truncation/test.hxx +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef TEST_HXX +#define TEST_HXX + +#include <string> +#include <odb/core.hxx> + +#pragma db object table ("test") +struct object1 +{ + object1 (unsigned long id) + : id_ (id) + { + } + + object1 () + { + } + + #pragma db id + unsigned long id_; + + std::string str_; +}; + +#pragma db object table ("test") +struct object2 +{ + object2 (unsigned long id) + : id_ (id) + { + } + + object2 () + { + } + + #pragma db id + unsigned long id_; + + std::string str_; +}; + +#endif // TEST_HXX diff --git a/odb-tests/sqlite/truncation/testscript b/odb-tests/sqlite/truncation/testscript new file mode 100644 index 0000000..97e04f7 --- /dev/null +++ b/odb-tests/sqlite/truncation/testscript @@ -0,0 +1,9 @@ +# file : sqlite/truncation/testscript +# license : GNU GPL v2; see accompanying LICENSE file + +.include ../../database-options.testscript +.include ../../sqlite.testscript + +: basics +: +$* diff --git a/odb-tests/sqlite/types/buildfile b/odb-tests/sqlite/types/buildfile new file mode 100644 index 0000000..1bf719d --- /dev/null +++ b/odb-tests/sqlite/types/buildfile @@ -0,0 +1,32 @@ +# file : sqlite/types/buildfile +# license : GNU GPL v2; see accompanying LICENSE file + +assert ($sqlite && !$multi || $build.meta_operation == 'dist') \ +"sqlite should be configured via config.odb_tests.database variable as a single database" + +import libodb = libodb%lib{odb} + +import libs = 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 library is +# resolved for the odb_compile ad hoc rule (see build/root.build for details). +# +libue{test-meta}: $libodb + +<{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_types_ \ + --generate-schema \ + --default-database common \ + --generate-query \ + --cxx-prologue '#include "traits.hxx"' + +cxx.poptions =+ "-I$out_base" "-I$src_base" diff --git a/odb-tests/sqlite/types/driver.cxx b/odb-tests/sqlite/types/driver.cxx new file mode 100644 index 0000000..b444d94 --- /dev/null +++ b/odb-tests/sqlite/types/driver.cxx @@ -0,0 +1,113 @@ +// file : sqlite/types/driver.cxx +// license : GNU GPL v2; see accompanying LICENSE file + +// Test SQLite type conversion. +// + +#include <limits> // std::numeric_limits +#include <memory> // std::unique_ptr +#include <iostream> + +#include <odb/sqlite/database.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> db (create_specific_database<database> (argc, argv)); + + object o (1); + + o.bool_ = true; + o.integer_ = -123456; + o.real_ = 1.123; + o.nan_ = numeric_limits<double>::quiet_NaN (); + + string long_str (2040, 'l'); + + o.text_ = long_str; +#ifdef _WIN32 + o.wtext_ = L"t\x00C8st string"; +#endif + o.blob_.assign (long_str.c_str (), long_str.c_str () + long_str.size ()); + + { + transaction t (db->begin ()); + db->persist (o); + t.commit (); + } + + { + transaction t (db->begin ()); + unique_ptr<object> o1 (db->load<object> (1)); + t.commit (); + + assert (o == *o1); + } + +#ifdef _WIN32 + { + typedef sqlite::query<object> query; + typedef odb::result<object> result; + + transaction t (db->begin ()); + result r (db->query<object> (query::wtext == L"t\x00C8st string")); + assert (!r.empty ()); + t.commit (); + } +#endif + + // Test char/wchar_t arrays + // + { +#ifndef _WIN32 + char_array o1 (1, ""); + char_array o2 (2, "1234567890"); + char_array o3 (3, "12345678901234567"); +#else + char_array o1 (1, "", L""); + char_array o2 (2, "1234567890", L"123456789\x00C8"); + char_array o3 (3, "12345678901234567", L"1234567890123456\x00C8"); +#endif + + { + transaction t (db->begin ()); + db->persist (o1); + db->persist (o2); + db->persist (o3); + t.commit (); + } + + { + transaction t (db->begin ()); + unique_ptr<char_array> p1 (db->load<char_array> (1)); + unique_ptr<char_array> p2 (db->load<char_array> (2)); + unique_ptr<char_array> p3 (db->load<char_array> (3)); + t.commit (); + + assert (o1 == *p1); + assert (o2 == *p2); + assert (o3 == *p3); + } + } + } + catch (const odb::exception& e) + { + cerr << e.what () << endl; + return 1; + } +} diff --git a/odb-tests/sqlite/types/test.hxx b/odb-tests/sqlite/types/test.hxx new file mode 100644 index 0000000..fe0b274 --- /dev/null +++ b/odb-tests/sqlite/types/test.hxx @@ -0,0 +1,125 @@ +// file : sqlite/types/test.hxx +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef TEST_HXX +#define TEST_HXX + +#include <set> +#include <string> +#include <vector> +#include <memory> // std::unique_ptr +#include <cstring> // std::memcpy, std::str[n]cmp, std::strlen + +#ifdef _WIN32 +# include <cwchar> // std::wcslen, std::wcs[n]cmp +#endif + +#include <odb/core.hxx> + +typedef std::unique_ptr<std::string> string_ptr; + +#pragma db object +struct object +{ + object () {} + object (unsigned long id): id_ (id) {} + + #pragma db id + unsigned long id_; + + #pragma db type("BOOL") + bool bool_; + + #pragma db type("INTEGER") + int integer_; + + #pragma db type("REAL") + double real_; + + double nan_; // Represented in SQLite as NULL. + + #pragma db type("TEXT") + std::string text_; + +#ifdef _WIN32 + std::wstring wtext_; +#endif + + #pragma db type("BLOB") + std::vector<char> blob_; + + // Test NULL value. + // + #pragma db type("TEXT") null + string_ptr null_; + + bool + operator== (const object& y) const + { + return id_ == y.id_ + && bool_ == y.bool_ + && integer_ == y.integer_ + && real_ == y.real_ + && nan_ != nan_ + && text_ == y.text_ +#ifdef _WIN32 + && wtext_ == y.wtext_ +#endif + && blob_ == y.blob_ + && ((null_.get () == 0 && y.null_.get () == 0) || *null_ == *y.null_); + } +}; + +// Test char/wchar_t arrays. +// +#pragma db object +struct char_array +{ + char_array () {} + char_array (unsigned long id + , const char* s +#ifdef _WIN32 + , const wchar_t* ws +#endif + ) + : id_ (id) + { + std::memcpy (s1, s, std::strlen (s) + 1); // VC++ strncpy deprecation. + s2[0] = c1 = *s; + +#ifdef _WIN32 + std::memcpy (ws1, ws, (std::wcslen (ws) + 1) * sizeof (wchar_t)); + ws2[0] = wc1 = *ws; +#endif + } + + #pragma db id + unsigned long id_; + + char s1[17]; + char s2[1]; + char c1; + +#ifdef _WIN32 + wchar_t ws1[17]; + wchar_t ws2[1]; + wchar_t wc1; +#endif + + bool + operator== (const char_array& y) const + { + return id_ == y.id_ + && std::strncmp (s1, y.s1, sizeof (s1)) == 0 + && s2[0] == y.s2[0] + && c1 == y.c1 +#ifdef _WIN32 + && std::wcsncmp (ws1, y.ws1, sizeof (ws1) / 2) == 0 + && ws2[0] == y.ws2[0] + && wc1 == y.wc1 +#endif + ; + } +}; + +#endif // TEST_HXX diff --git a/odb-tests/sqlite/types/testscript b/odb-tests/sqlite/types/testscript new file mode 100644 index 0000000..5e50d32 --- /dev/null +++ b/odb-tests/sqlite/types/testscript @@ -0,0 +1,9 @@ +# file : sqlite/types/testscript +# license : GNU GPL v2; see accompanying LICENSE file + +.include ../../database-options.testscript +.include ../../sqlite.testscript + +: basics +: +$* diff --git a/odb-tests/sqlite/types/traits.hxx b/odb-tests/sqlite/types/traits.hxx new file mode 100644 index 0000000..9483f58 --- /dev/null +++ b/odb-tests/sqlite/types/traits.hxx @@ -0,0 +1,57 @@ +// file : sqlite/types/traits.hxx +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef TRAITS_HXX +#define TRAITS_HXX + +#include <cstring> // std::memcpy, std::memset + +#include <odb/sqlite/traits.hxx> + +#include "test.hxx" // string_ptr + +namespace odb +{ + namespace sqlite + { + template <> + class value_traits<string_ptr, id_text> + { + public: + typedef string_ptr value_type; + typedef std::string query_type; + typedef details::buffer image_type; + + static void + set_value (string_ptr& v, + const details::buffer& b, + std::size_t n, + bool is_null) + { + v.reset (is_null ? 0 : new std::string (b.data (), n)); + } + + static void + set_image (details::buffer& b, + std::size_t& n, + bool& is_null, + const string_ptr& v) + { + is_null = v.get () == 0; + + if (!is_null) + { + n = v->size (); + + if (n > b.capacity ()) + b.capacity (n); + + if (n != 0) + std::memcpy (b.data (), v->c_str (), n); + } + } + }; + } +} + +#endif // TRAITS_HXX |