From d3905b172b7d3cd1df933f57072aee652d10c186 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Thu, 26 Jan 2012 12:43:17 +0200 Subject: Implement support for database schema New pragma qualifier: namespace. New pragma specifier: schema. The table specifier was extended to accept a schema prefix. New option: --default- schema. The common/schema test was extended to cover the new functionality. --- common/schema/driver.cxx | 76 +++++++++++++++++++++++++++++++++++++++++-- common/schema/makefile | 2 +- common/schema/test.hxx | 85 ++++++++++++++++++++++++++++++++++++++++++++++-- common/schema/test.std | 1 - 4 files changed, 156 insertions(+), 8 deletions(-) (limited to 'common') diff --git a/common/schema/driver.cxx b/common/schema/driver.cxx index b53464c..eb1358e 100644 --- a/common/schema/driver.cxx +++ b/common/schema/driver.cxx @@ -3,7 +3,7 @@ // copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC // license : GNU GPL v2; see accompanying LICENSE file -// Test database schema creation. +// Test various aspects of database schema. // #include // std::auto_ptr @@ -28,11 +28,81 @@ main (int argc, char* argv[]) { auto_ptr db (create_database (argc, argv)); + // Test database schema (aka database namespace). // - // - cout << "test 001" << endl; + 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 ()); + auto_ptr p2 (db->load ("aaa")); + t.commit (); + + assert (o2 == *p2); + } + + { + typedef odb::query query; + typedef odb::result result; + + transaction t (db->begin ()); + + { + result r (db->query (query::id == "aaa")); + assert (size (r) == 1); + } + + { + result r (db->query (query::obj1->str == "aaa")); + assert (size (r) == 1); + } + + t.commit (); + } + + { + typedef odb::query query; + typedef odb::result result; + + transaction t (db->begin ()); + + result r (db->query (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::query query; + typedef odb::result result; + transaction t (db->begin ()); + + result r (db->query ()); + + result::iterator i (r.begin ()); + assert (i != r.end ()); + assert (i->str == "aaa"); + assert (++i == r.end ()); + t.commit (); } } diff --git a/common/schema/makefile b/common/schema/makefile index bc04d16..043e8a1 100644 --- a/common/schema/makefile +++ b/common/schema/makefile @@ -36,7 +36,7 @@ gen := $(addprefix $(out_base)/,$(genf)) $(gen): $(odb) $(gen): odb := $(odb) $(gen) $(dist): export odb_options += --database $(db_id) --generate-schema \ ---table-prefix schema_ +--generate-query --table-prefix schema_ $(gen): cpp_options := -I$(src_base) $(gen): $(common.l.cpp-options) diff --git a/common/schema/test.hxx b/common/schema/test.hxx index 45ffe9a..6c1bbd4 100644 --- a/common/schema/test.hxx +++ b/common/schema/test.hxx @@ -7,11 +7,13 @@ #define TEST_HXX #include +#include + #include // Table names. // -#pragma db object table ("TABLE_EXPLICIT") +#pragma db object table("TABLE_EXPLICIT") struct table_explicit { #pragma db id @@ -33,7 +35,7 @@ struct column #pragma db id int m1; - #pragma db column ("foo") + #pragma db column("foo") int m2; int m_m3; @@ -69,11 +71,88 @@ struct type double d; std::string str; - #pragma db type ("INTEGER") + #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" +#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 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_object_1") +struct table_view +{ + #pragma db column(DB_SCHEMA."schema_object_1"."str") + std::string str; +}; + #endif // TEST_HXX diff --git a/common/schema/test.std b/common/schema/test.std index af8d8e7..e69de29 100644 --- a/common/schema/test.std +++ b/common/schema/test.std @@ -1 +0,0 @@ -test 001 -- cgit v1.1