diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2012-01-26 12:43:17 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2012-01-26 12:43:17 +0200 |
commit | d3905b172b7d3cd1df933f57072aee652d10c186 (patch) | |
tree | a0c65d355d29285dce239b0d2328c168118e3646 /common/schema/test.hxx | |
parent | 32b8b828dc0e8ffea918c8cf9f07ba213ef63724 (diff) |
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.
Diffstat (limited to 'common/schema/test.hxx')
-rw-r--r-- | common/schema/test.hxx | 85 |
1 files changed, 82 insertions, 3 deletions
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 <string> +#include <vector> + #include <odb/core.hxx> // 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<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_object_1") +struct table_view +{ + #pragma db column(DB_SCHEMA."schema_object_1"."str") + std::string str; +}; + #endif // TEST_HXX |