diff options
Diffstat (limited to 'odb-tests/common/schema')
-rw-r--r-- | odb-tests/common/schema/embedded/basics/buildfile | 42 | ||||
-rw-r--r-- | odb-tests/common/schema/embedded/basics/driver.cxx | 59 | ||||
-rw-r--r-- | odb-tests/common/schema/embedded/basics/test.hxx | 23 | ||||
-rw-r--r-- | odb-tests/common/schema/embedded/basics/testscript | 49 | ||||
-rw-r--r-- | odb-tests/common/schema/embedded/order/.gitignore | 6 | ||||
-rw-r--r-- | odb-tests/common/schema/embedded/order/buildfile | 48 | ||||
-rw-r--r-- | odb-tests/common/schema/embedded/order/driver.cxx | 65 | ||||
-rw-r--r-- | odb-tests/common/schema/embedded/order/test1.hxx | 23 | ||||
-rw-r--r-- | odb-tests/common/schema/embedded/order/test2.hxx | 17 | ||||
-rw-r--r-- | odb-tests/common/schema/embedded/order/testscript | 49 | ||||
-rw-r--r-- | odb-tests/common/schema/namespace/buildfile | 41 | ||||
-rw-r--r-- | odb-tests/common/schema/namespace/driver.cxx | 113 | ||||
-rw-r--r-- | odb-tests/common/schema/namespace/test.hxx | 158 | ||||
-rw-r--r-- | odb-tests/common/schema/namespace/testscript | 53 |
14 files changed, 746 insertions, 0 deletions
diff --git a/odb-tests/common/schema/embedded/basics/buildfile b/odb-tests/common/schema/embedded/basics/buildfile new file mode 100644 index 0000000..0cfe85e --- /dev/null +++ b/odb-tests/common/schema/embedded/basics/buildfile @@ -0,0 +1,42 @@ +# file : common/schema/embedded/basics/buildfile +# license : GNU GPL v2; see accompanying LICENSE file + +import libodb = libodb%lib{odb} + +libs = + +for db: $databases + import libs += libodb-$db%lib{odb-$db} + +import libs += lib{common} + +exe{driver}: {hxx cxx}{* -*-odb -*-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} + +for db: $databases +{ + exe{driver}: {hxx ixx cxx}{test-odb-$db}: include = $multi + <{hxx ixx cxx}{test-odb-$db}>: 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 schema_embd_bscs_ \ + --generate-schema \ + --schema-format embedded \ + --schema-name test + +cxx.poptions =+ "-I$out_base" "-I$src_base" + +# Testscript's run-time prerequisites. +# +exe{driver}: ../../../../alias{database-client}: include = adhoc diff --git a/odb-tests/common/schema/embedded/basics/driver.cxx b/odb-tests/common/schema/embedded/basics/driver.cxx new file mode 100644 index 0000000..7ba2bce --- /dev/null +++ b/odb-tests/common/schema/embedded/basics/driver.cxx @@ -0,0 +1,59 @@ +// file : common/schema/embedded/basics/driver.cxx +// license : GNU GPL v2; see accompanying LICENSE file + +// Test basic embedded schema functionality. +// + +#include <memory> // std::unique_ptr +#include <iostream> + +#include <odb/database.hxx> +#include <odb/transaction.hxx> +#include <odb/schema-catalog.hxx> + +#include <libcommon/common.hxx> + +#include "test.hxx" +#include "test-odb.hxx" + +#undef NDEBUG +#include <cassert> + +using namespace std; +using namespace odb::core; + +int +main (int argc, char* argv[]) +{ + try + { + unique_ptr<database> db (create_database (argc, argv, false)); + + // Create the database schema. + // + { + connection_ptr c (db->connection ()); + + // Temporarily disable foreign key constraints for SQLite. + // + if (db->id () == odb::id_sqlite) + c->execute ("PRAGMA foreign_keys=OFF"); + + assert (schema_catalog::exists (*db, "test")); + assert (!schema_catalog::exists (*db, "test1")); + assert (!schema_catalog::exists (*db, "")); + + transaction t (c->begin ()); + schema_catalog::create_schema (*db, "test"); + t.commit (); + + if (db->id () == odb::id_sqlite) + c->execute ("PRAGMA foreign_keys=ON"); + } + } + catch (const odb::exception& e) + { + cerr << e.what () << endl; + return 1; + } +} diff --git a/odb-tests/common/schema/embedded/basics/test.hxx b/odb-tests/common/schema/embedded/basics/test.hxx new file mode 100644 index 0000000..43331f9 --- /dev/null +++ b/odb-tests/common/schema/embedded/basics/test.hxx @@ -0,0 +1,23 @@ +// file : common/schema/embedded/basics/test.hxx +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef TEST_HXX +#define TEST_HXX + +#include <string> +#include <vector> + +#include <odb/core.hxx> + +#pragma db object +struct object +{ + #pragma db auto id + unsigned long id; + + std::string str; + + std::vector<int> nums; +}; + +#endif // TEST_HXX diff --git a/odb-tests/common/schema/embedded/basics/testscript b/odb-tests/common/schema/embedded/basics/testscript new file mode 100644 index 0000000..6affa13 --- /dev/null +++ b/odb-tests/common/schema/embedded/basics/testscript @@ -0,0 +1,49 @@ +# file : common/schema/embedded/basics/testscript +# license : GNU GPL v2; see accompanying LICENSE file + +.include ../../../../database-options.testscript + +: mysql +: +if $mysql +{ + .include ../../../../mysql.testscript + + $* +} + +: sqlite +: +if $sqlite +{ + .include ../../../../sqlite.testscript + + $* +} + +: pgsql +: +if $pgsql +{ + .include ../../../../pgsql.testscript + + $* +} + +: oracle +: +if $oracle +{ + .include ../../../../oracle.testscript + + $* +} + +: mssql +: +if $mssql +{ + .include ../../../../mssql.testscript + + $* +} diff --git a/odb-tests/common/schema/embedded/order/.gitignore b/odb-tests/common/schema/embedded/order/.gitignore new file mode 100644 index 0000000..5d39d39 --- /dev/null +++ b/odb-tests/common/schema/embedded/order/.gitignore @@ -0,0 +1,6 @@ +# ODB-generated files. +# +test1-odb.?xx +test1-odb-*.?xx +test2-odb.?xx +test2-odb-*.?xx diff --git a/odb-tests/common/schema/embedded/order/buildfile b/odb-tests/common/schema/embedded/order/buildfile new file mode 100644 index 0000000..b2fac0b --- /dev/null +++ b/odb-tests/common/schema/embedded/order/buildfile @@ -0,0 +1,48 @@ +# file : common/schema/embedded/order/buildfile +# license : GNU GPL v2; see accompanying LICENSE file + +import libodb = libodb%lib{odb} + +libs = + +for db: $databases + import libs += libodb-$db%lib{odb-$db} + +import libs += lib{common} + +hdrs = test1 test2 + +exe{driver}: {hxx cxx}{* -*-odb -*-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 + +for h: $hdrs +{ + exe{driver}: {hxx ixx cxx}{$h-odb} + + <{hxx ixx cxx}{$h-odb}>: hxx{$h} libue{test-meta} + + for db: $databases + { + exe{driver}: {hxx ixx cxx}{$h-odb-$db}: include = $multi + <{hxx ixx cxx}{$h-odb-$db}>: hxx{$h} 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 schema_embd_ordr_ \ + --generate-schema \ + --schema-format embedded + +cxx.poptions =+ "-I$out_base" "-I$src_base" + +# Testscript's run-time prerequisites. +# +exe{driver}: ../../../../alias{database-client}: include = adhoc diff --git a/odb-tests/common/schema/embedded/order/driver.cxx b/odb-tests/common/schema/embedded/order/driver.cxx new file mode 100644 index 0000000..fde5e96 --- /dev/null +++ b/odb-tests/common/schema/embedded/order/driver.cxx @@ -0,0 +1,65 @@ +// file : common/schema/embedded/order/driver.cxx +// license : GNU GPL v2; see accompanying LICENSE file + +// Test statement execution order in embedded schemas. +// + +#include <memory> // std::unique_ptr +#include <iostream> + +#include <odb/database.hxx> +#include <odb/transaction.hxx> +#include <odb/schema-catalog.hxx> + +#include <libcommon/common.hxx> + +#include "test1.hxx" +#include "test2.hxx" + +#include "test1-odb.hxx" +#include "test2-odb.hxx" + +#undef NDEBUG +#include <cassert> + +using namespace std; +using namespace odb::core; + +int +main (int argc, char* argv[]) +{ + try + { + unique_ptr<database> db (create_database (argc, argv, false)); + odb::database_id db_id (db->id ()); + + // Create the database schema. + // + { + connection_ptr c (db->connection ()); + + // Temporarily disable foreign key constraints for MySQL and SQLite. + // For MySQL we can actually create the tables in any order. It is + // dropping them that's the problem (there is no IF EXISTS). + // + if (db_id == odb::id_mysql) + c->execute ("SET FOREIGN_KEY_CHECKS=0"); + else if (db_id == odb::id_sqlite) + c->execute ("PRAGMA foreign_keys=OFF"); + + transaction t (c->begin ()); + schema_catalog::create_schema (*db); + t.commit (); + + if (db_id == odb::id_mysql) + c->execute ("SET FOREIGN_KEY_CHECKS=1"); + else if (db_id == odb::id_sqlite) + c->execute ("PRAGMA foreign_keys=ON"); + } + } + catch (const odb::exception& e) + { + cerr << e.what () << endl; + return 1; + } +} diff --git a/odb-tests/common/schema/embedded/order/test1.hxx b/odb-tests/common/schema/embedded/order/test1.hxx new file mode 100644 index 0000000..b35074b --- /dev/null +++ b/odb-tests/common/schema/embedded/order/test1.hxx @@ -0,0 +1,23 @@ +// file : common/schema/embedded/order/test1.hxx +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef TEST1_HXX +#define TEST1_HXX + +#include <string> + +#include <odb/core.hxx> + +#pragma db object polymorphic +struct base +{ + virtual + ~base () {} + + #pragma db auto id + unsigned long id; + + std::string str; +}; + +#endif // TEST1_HXX diff --git a/odb-tests/common/schema/embedded/order/test2.hxx b/odb-tests/common/schema/embedded/order/test2.hxx new file mode 100644 index 0000000..b4e6d20 --- /dev/null +++ b/odb-tests/common/schema/embedded/order/test2.hxx @@ -0,0 +1,17 @@ +// file : common/schema/embedded/order/test2.hxx +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef TEST2_HXX +#define TEST2_HXX + +#include <odb/core.hxx> + +#include "test1.hxx" + +#pragma db object +struct derived: base +{ + int num; +}; + +#endif // TEST2_HXX diff --git a/odb-tests/common/schema/embedded/order/testscript b/odb-tests/common/schema/embedded/order/testscript new file mode 100644 index 0000000..131b3fa --- /dev/null +++ b/odb-tests/common/schema/embedded/order/testscript @@ -0,0 +1,49 @@ +# file : common/schema/embedded/order/testscript +# license : GNU GPL v2; see accompanying LICENSE file + +.include ../../../../database-options.testscript + +: mysql +: +if $mysql +{ + .include ../../../../mysql.testscript + + $* +} + +: sqlite +: +if $sqlite +{ + .include ../../../../sqlite.testscript + + $* +} + +: pgsql +: +if $pgsql +{ + .include ../../../../pgsql.testscript + + $* +} + +: oracle +: +if $oracle +{ + .include ../../../../oracle.testscript + + $* +} + +: mssql +: +if $mssql +{ + .include ../../../../mssql.testscript + + $* +} diff --git a/odb-tests/common/schema/namespace/buildfile b/odb-tests/common/schema/namespace/buildfile new file mode 100644 index 0000000..fa496d7 --- /dev/null +++ b/odb-tests/common/schema/namespace/buildfile @@ -0,0 +1,41 @@ +# file : common/schema/namespace/buildfile +# license : GNU GPL v2; see accompanying LICENSE file + +import libodb = libodb%lib{odb} + +libs = + +for db: $databases + import libs += libodb-$db%lib{odb-$db} + +import libs += lib{common} + +exe{driver}: {hxx cxx}{* -*-odb -*-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} + +for db: $databases +{ + exe{driver}: {hxx ixx cxx}{test-odb-$db}: include = $multi + <{hxx ixx cxx}{test-odb-$db}>: 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 schema_ns_ \ + --generate-schema \ + --generate-query + +cxx.poptions =+ "-I$out_base" "-I$src_base" + +# Testscript's run-time prerequisites. +# +exe{driver}: ../../../alias{database-client}: include = adhoc diff --git a/odb-tests/common/schema/namespace/driver.cxx b/odb-tests/common/schema/namespace/driver.cxx new file mode 100644 index 0000000..25515f2 --- /dev/null +++ b/odb-tests/common/schema/namespace/driver.cxx @@ -0,0 +1,113 @@ +// file : common/schema/namespace/driver.cxx +// license : GNU GPL v2; see accompanying LICENSE file + +// Test database schemas (aka database namespaces). +// + +#include <memory> // std::unique_ptr +#include <iostream> + +#include <odb/database.hxx> +#include <odb/transaction.hxx> + +#include <libcommon/common.hxx> + +#include "test.hxx" +#include "test-odb.hxx" + +#undef NDEBUG +#include <cassert> + +using namespace std; +using namespace odb::core; + +int +main (int argc, char* argv[]) +{ + try + { + unique_ptr<database> db (create_database (argc, argv)); + + // Test database schema (aka database namespace). + // + using ns::object2; + + object2 o2; + o2.id = "aaa"; + o2.nums.push_back (1); + o2.nums.push_back (2); + o2.nums.push_back (3); + o2.obj1 = new object1; + o2.obj1->str = "aaa"; + + { + transaction t (db->begin ()); + db->persist (o2.obj1); + db->persist (o2); + t.commit (); + } + + { + transaction t (db->begin ()); + unique_ptr<object2> p2 (db->load<object2> ("aaa")); + t.commit (); + + assert (o2 == *p2); + } + + { + typedef odb::query<object2> query; + typedef odb::result<object2> result; + + transaction t (db->begin ()); + + { + result r (db->query<object2> (query::id == "aaa")); + assert (size (r) == 1); + } + + { + result r (db->query<object2> (query::obj1->str == "aaa")); + assert (size (r) == 1); + } + + t.commit (); + } + + { + typedef odb::query<object_view> query; + typedef odb::result<object_view> result; + + transaction t (db->begin ()); + + result r (db->query<object_view> (query::object2::id == "aaa")); + + result::iterator i (r.begin ()); + assert (i != r.end ()); + assert (i->id2 == "aaa" && i->str == "aaa"); + assert (++i == r.end ()); + + t.commit (); + } + + { + typedef odb::result<table_view> result; + + transaction t (db->begin ()); + + result r (db->query<table_view> ()); + + result::iterator i (r.begin ()); + assert (i != r.end ()); + assert (i->str == "aaa"); + assert (++i == r.end ()); + + t.commit (); + } + } + catch (const odb::exception& e) + { + cerr << e.what () << endl; + return 1; + } +} diff --git a/odb-tests/common/schema/namespace/test.hxx b/odb-tests/common/schema/namespace/test.hxx new file mode 100644 index 0000000..0b1844a --- /dev/null +++ b/odb-tests/common/schema/namespace/test.hxx @@ -0,0 +1,158 @@ +// file : common/schema/namespace/test.hxx +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef TEST_HXX +#define TEST_HXX + +#include <string> +#include <vector> + +#include <odb/core.hxx> + +// Table names. +// +#pragma db object table("TABLE_EXPLICIT") +struct table_explicit +{ + #pragma db id + unsigned long id_; +}; + +#pragma db object +struct table_implicit +{ + #pragma db id + unsigned long id_; +}; + +// Column names. +// +#pragma db object +struct column +{ + #pragma db id + int m1; + + #pragma db column("foo") + int m2; + + int m_m3; + int _m4; + int m5_; + int m_; + int m__; +}; + +// Column types. +// +#pragma db object +struct type +{ + #pragma db id + std::string id; + + // Test default C++ to DB type mapping. + // + bool b; + char c; + signed char sc; + unsigned char uc; + short s; + unsigned short us; + int i; + unsigned int ui; + long l; + unsigned long ul; + long long ll; + unsigned long long ull; + float f; + double d; + std::string str; + + #pragma db type("INTEGER") + bool m1; + + #pragma db transient + char* m2; +}; + +// Test database schema (aka database namespace). +// +#ifdef ODB_COMPILER +#if defined (ODB_DATABASE_MYSQL) +//# define DB_SCHEMA "odb_test" +# define DB_SCHEMA "" +#elif defined (ODB_DATABASE_SQLITE) +# define DB_SCHEMA "main" +#elif defined (ODB_DATABASE_PGSQL) +# define DB_SCHEMA "public" +#elif defined (ODB_DATABASE_ORACLE) +//# define DB_SCHEMA "ODB_TEST" +# define DB_SCHEMA "" +#elif defined(ODB_DATABASE_MSSQL) +# define DB_SCHEMA "dbo" +#elif defined(ODB_DATABASE_COMMON) +# define DB_SCHEMA "dummy" +#else +# error unknown database +#endif +#endif + +namespace ns {typedef int my_int;} // Original. + +#pragma db object table(DB_SCHEMA."object_1") +struct object1 +{ + #pragma db id auto + unsigned long id; + + #pragma db column("str") + std::string str; +}; + +inline bool +operator== (const object1& x, const object1& y) +{ + return x.id == y.id && x.str == y.str; +} + +#pragma db namespace schema(DB_SCHEMA) +namespace ns // Extension. +{ + #pragma db object + struct object2 + { + object2 (): obj1 (0) {} + ~object2 () {delete obj1;} + + #pragma db id + std::string id; + + std::vector<unsigned int> nums; + object1* obj1; + }; + + inline bool + operator== (const object2& x, const object2& y) + { + return x.id == y.id && x.nums == y.nums && *x.obj1 == *y.obj1; + } +} + +#pragma db view object(object1) object(ns::object2) +struct object_view +{ + #pragma db column(ns::object2::id) + std::string id2; + + std::string str; +}; + +#pragma db view table(DB_SCHEMA."schema_ns_object_1") +struct table_view +{ + #pragma db column(DB_SCHEMA."schema_ns_object_1"."str") + std::string str; +}; + +#endif // TEST_HXX diff --git a/odb-tests/common/schema/namespace/testscript b/odb-tests/common/schema/namespace/testscript new file mode 100644 index 0000000..a2edd8d --- /dev/null +++ b/odb-tests/common/schema/namespace/testscript @@ -0,0 +1,53 @@ +# file : common/schema/namespace/testscript +# license : GNU GPL v2; see accompanying LICENSE file + +.include ../../../database-options.testscript + +: mysql +: +if $mysql +{ + .include ../../../mysql.testscript + + $create_schema; + $* +} + +: sqlite +: +if $sqlite +{ + .include ../../../sqlite.testscript + + $* +} + +: pgsql +: +if $pgsql +{ + .include ../../../pgsql.testscript + + $create_schema; + $* +} + +: oracle +: +if $oracle +{ + .include ../../../oracle.testscript + + $create_schema; + $* +} + +: mssql +: +if $mssql +{ + .include ../../../mssql.testscript + + $create_schema; + $* +} |