aboutsummaryrefslogtreecommitdiff
path: root/common/schema/namespace
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2013-02-07 17:52:49 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2013-02-07 17:52:49 +0200
commit7dbf58f9486fb3b3a021bbfab9df03af5a8f0fb3 (patch)
treebdf0897bd03cb578d932257a70ad98314c7e7821 /common/schema/namespace
parent14cc151f37a089784f8db162bb6f419ec8d1aecb (diff)
Use multi-pass table creation in MySQL
This deals with table creation order and circular dependencies. Unfortunately, there doesn't seem to be a way in MySQL to drop a foreign key only if it exists without resorting to stored procedures.
Diffstat (limited to 'common/schema/namespace')
-rw-r--r--common/schema/namespace/driver.cxx112
-rw-r--r--common/schema/namespace/makefile115
-rw-r--r--common/schema/namespace/test.hxx157
-rw-r--r--common/schema/namespace/test.std0
4 files changed, 384 insertions, 0 deletions
diff --git a/common/schema/namespace/driver.cxx b/common/schema/namespace/driver.cxx
new file mode 100644
index 0000000..d74461c
--- /dev/null
+++ b/common/schema/namespace/driver.cxx
@@ -0,0 +1,112 @@
+// file : common/schema/namespace/driver.cxx
+// copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC
+// license : GNU GPL v2; see accompanying LICENSE file
+
+// Test database schemas (aka database namespaces).
+//
+
+#include <memory> // std::auto_ptr
+#include <cassert>
+#include <iostream>
+
+#include <odb/database.hxx>
+#include <odb/transaction.hxx>
+
+#include <common/common.hxx>
+
+#include "test.hxx"
+#include "test-odb.hxx"
+
+using namespace std;
+using namespace odb::core;
+
+int
+main (int argc, char* argv[])
+{
+ try
+ {
+ auto_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 ());
+ auto_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/common/schema/namespace/makefile b/common/schema/namespace/makefile
new file mode 100644
index 0000000..e15ee10
--- /dev/null
+++ b/common/schema/namespace/makefile
@@ -0,0 +1,115 @@
+# file : common/schema/namespace/makefile
+# copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC
+# license : GNU GPL v2; see accompanying LICENSE file
+
+include $(dir $(lastword $(MAKEFILE_LIST)))../../../build/bootstrap.make
+
+cxx_tun := driver.cxx
+odb_hdr := test.hxx
+cxx_obj := $(addprefix $(out_base)/,$(cxx_tun:.cxx=.o) $(odb_hdr:.hxx=-odb.o))
+cxx_od := $(cxx_obj:.o=.o.d)
+
+common.l := $(out_root)/libcommon/common/common.l
+common.l.cpp-options := $(out_root)/libcommon/common/common.l.cpp-options
+
+driver := $(out_base)/driver
+dist := $(out_base)/.dist
+test := $(out_base)/.test
+clean := $(out_base)/.clean
+
+# Import.
+#
+$(call import,\
+ $(scf_root)/import/odb/stub.make,\
+ odb: odb,odb-rules: odb_rules)
+
+# Build.
+#
+$(driver): $(cxx_obj) $(common.l)
+$(cxx_obj) $(cxx_od): cpp_options := -I$(out_base) -I$(src_base)
+$(cxx_obj) $(cxx_od): $(common.l.cpp-options)
+
+genf := $(addprefix $(odb_hdr:.hxx=-odb),.hxx .ixx .cxx) $(odb_hdr:.hxx=.sql)
+gen := $(addprefix $(out_base)/,$(genf))
+
+$(gen): $(odb)
+$(gen): odb := $(odb)
+$(gen) $(dist): export odb_options += --database $(db_id) --generate-schema \
+--generate-query --table-prefix schema_ns_
+$(gen): cpp_options := -I$(src_base)
+$(gen): $(common.l.cpp-options)
+
+$(call include-dep,$(cxx_od),$(cxx_obj),$(gen))
+
+# Alias for default target.
+#
+$(out_base)/: $(driver)
+
+# Dist
+#
+name := $(subst /,-,$(subst $(src_root)/common/,,$(src_base)))
+
+$(dist): db_id := @database@
+$(dist): sources := $(cxx_tun)
+$(dist): headers := $(odb_hdr)
+$(dist): data_dist := test.std
+$(dist): export name := $(name)
+$(dist): export extra_dist := $(data_dist) $(call vc9projs,$(name)) \
+$(call vc10projs,$(name)) $(call vc11projs,$(name))
+$(dist):
+ $(call dist-data,$(sources) $(headers) $(data_dist))
+ $(call meta-automake,../../template/Makefile.am)
+ $(call meta-vc9projs,../../template/template,$(name))
+ $(call meta-vc10projs,../../template/template,$(name))
+ $(call meta-vc11projs,../../template/template,$(name))
+
+# Test.
+#
+$(test): $(driver) $(src_base)/test.std
+ $(call schema)
+ $(call message,test $<,$< --options-file $(dcf_root)/db.options \
+>$(out_base)/test.out)
+ $(call message,,diff -u $(src_base)/test.std $(out_base)/test.out)
+ $(call message,,rm -f $(out_base)/test.out)
+
+# Clean.
+#
+$(clean): \
+ $(driver).o.clean \
+ $(addsuffix .cxx.clean,$(cxx_obj)) \
+ $(addsuffix .cxx.clean,$(cxx_od)) \
+ $(addprefix $(out_base)/,$(odb_hdr:.hxx=-odb.cxx.hxx.clean))
+ $(call message,,rm -f $(out_base)/test.out)
+
+# Generated .gitignore.
+#
+ifeq ($(out_base),$(src_base))
+$(driver): | $(out_base)/.gitignore
+
+$(out_base)/.gitignore: files := driver $(genf)
+$(clean): $(out_base)/.gitignore.clean
+
+$(call include,$(bld_root)/git/gitignore.make)
+endif
+
+# How to.
+#
+$(call include,$(bld_root)/dist.make)
+$(call include,$(bld_root)/meta/vc9proj.make)
+$(call include,$(bld_root)/meta/vc10proj.make)
+$(call include,$(bld_root)/meta/vc11proj.make)
+$(call include,$(bld_root)/meta/automake.make)
+
+$(call include,$(bld_root)/cxx/standard.make) # cxx_standard
+ifdef cxx_standard
+$(gen): odb_options += --std $(cxx_standard)
+$(call include,$(odb_rules))
+endif
+
+$(call include,$(bld_root)/cxx/cxx-d.make)
+$(call include,$(bld_root)/cxx/cxx-o.make)
+$(call include,$(bld_root)/cxx/o-e.make)
+
+# Dependencies.
+#
+$(call import,$(src_root)/libcommon/makefile)
diff --git a/common/schema/namespace/test.hxx b/common/schema/namespace/test.hxx
new file mode 100644
index 0000000..44d5c60
--- /dev/null
+++ b/common/schema/namespace/test.hxx
@@ -0,0 +1,157 @@
+// file : common/schema/namespace/test.hxx
+// copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC
+// 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"
+#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
diff --git a/common/schema/namespace/test.std b/common/schema/namespace/test.std
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/common/schema/namespace/test.std