summaryrefslogtreecommitdiff
path: root/odb-tests/common/schema/namespace
diff options
context:
space:
mode:
Diffstat (limited to 'odb-tests/common/schema/namespace')
-rw-r--r--odb-tests/common/schema/namespace/buildfile41
-rw-r--r--odb-tests/common/schema/namespace/driver.cxx113
-rw-r--r--odb-tests/common/schema/namespace/test.hxx158
-rw-r--r--odb-tests/common/schema/namespace/testscript33
4 files changed, 345 insertions, 0 deletions
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..0fabe6e
--- /dev/null
+++ b/odb-tests/common/schema/namespace/testscript
@@ -0,0 +1,33 @@
+# 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;
+ $*
+}