aboutsummaryrefslogtreecommitdiff
path: root/common
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2010-08-19 11:25:20 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2010-08-19 11:25:20 +0200
commitb59214c08549043cb5812c025159c49d6f9efe01 (patch)
tree3d586457497b96391c292af33ffcfe9a25a33701 /common
parentbc95a07e6ecd388b4ef485f218aa1211d36e0e53 (diff)
Test automatic id assignment
Diffstat (limited to 'common')
-rw-r--r--common/auto/driver.cxx69
-rw-r--r--common/auto/makefile82
-rw-r--r--common/auto/test.hxx33
-rw-r--r--common/auto/test.std0
-rw-r--r--common/makefile1
5 files changed, 185 insertions, 0 deletions
diff --git a/common/auto/driver.cxx b/common/auto/driver.cxx
new file mode 100644
index 0000000..423254e
--- /dev/null
+++ b/common/auto/driver.cxx
@@ -0,0 +1,69 @@
+// file : common/auto/driver.cxx
+// author : Boris Kolpackov <boris@codesynthesis.com>
+// copyright : Copyright (c) 2009-2010 Code Synthesis Tools CC
+// license : GNU GPL v2; see accompanying LICENSE file
+
+// Test automatic id assignment.
+//
+
+#include <memory> // std::auto_ptr
+#include <cassert>
+#include <iostream>
+
+#include <odb/database.hxx>
+#include <odb/transaction.hxx>
+
+#include <common.hxx>
+
+#include "test.hxx"
+#include "test-odb.hxx"
+
+using namespace std;
+using namespace odb;
+
+int
+main (int argc, char* argv[])
+{
+ try
+ {
+ auto_ptr<database> db (create_database (argc, argv));
+
+ unsigned long id1, id2, id3;
+ {
+ object o1 ("one");
+ object o2 ("two");
+ object o3 ("three");
+
+ transaction t (db->begin_transaction ());
+ db->persist (o1);
+ db->persist (o2);
+ db->persist (o3);
+ t.commit ();
+
+ id1 = o1.id_;
+ id2 = o2.id_;
+ id3 = o3.id_;
+
+ assert (id1 != id2);
+ assert (id1 != id3);
+ assert (id2 != id3);
+ }
+
+ {
+ transaction t (db->begin_transaction ());
+ auto_ptr<object> o1 (db->load<object> (id1));
+ auto_ptr<object> o2 (db->load<object> (id2));
+ auto_ptr<object> o3 (db->load<object> (id3));
+ t.commit ();
+
+ assert (o1->id_ == id1 && o1->str_ == "one");
+ assert (o2->id_ == id2 && o2->str_ == "two");
+ assert (o3->id_ == id3 && o3->str_ == "three");
+ }
+ }
+ catch (const odb::exception& e)
+ {
+ cerr << e.what () << endl;
+ return 1;
+ }
+}
diff --git a/common/auto/makefile b/common/auto/makefile
new file mode 100644
index 0000000..31c7efb
--- /dev/null
+++ b/common/auto/makefile
@@ -0,0 +1,82 @@
+# file : common/auto/makefile
+# author : Boris Kolpackov <boris@codesynthesis.com>
+# copyright : Copyright (c) 2009-2010 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.l
+common.l.cpp-options := $(out_root)/libcommon/common.l.cpp-options
+
+driver := $(out_base)/driver
+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)
+$(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): odb_options += --database $(db_id) --generate-schema
+$(gen): cpp_options := -I$(out_base)
+$(gen): $(common.l.cpp-options)
+
+$(call include-dep,$(cxx_od),$(cxx_obj),$(gen))
+
+# Alias for default target.
+#
+$(out_base)/: $(driver)
+
+# Test.
+#
+$(test): $(driver) $(src_base)/test.std
+ $(call message,sql $$1,$(dcf_root)/db-driver $$1, $(src_base)/test.sql)
+ $(call message,test $<,$< --options-file $(dcf_root)/db.options \
+| diff -u $(src_base)/test.std -)
+
+# 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))
+
+# 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,$(odb_rules))
+$(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/auto/test.hxx b/common/auto/test.hxx
new file mode 100644
index 0000000..c23027e
--- /dev/null
+++ b/common/auto/test.hxx
@@ -0,0 +1,33 @@
+// file : common/auto/test.hxx
+// author : Boris Kolpackov <boris@codesynthesis.com>
+// copyright : Copyright (c) 2009-2010 Code Synthesis Tools CC
+// license : GNU GPL v2; see accompanying LICENSE file
+
+#ifndef TEST_HXX
+#define TEST_HXX
+
+#include <string>
+
+#include <odb/core.hxx>
+
+#pragma odb object
+struct object
+{
+ object (const std::string& str)
+ : id_ (1), str_ (str)
+ {
+ }
+
+ #pragma odb auto id
+ unsigned long id_;
+ std::string str_;
+
+private:
+ object ()
+ {
+ }
+
+ friend class odb::access;
+};
+
+#endif // TEST_HXX
diff --git a/common/auto/test.std b/common/auto/test.std
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/common/auto/test.std
diff --git a/common/makefile b/common/makefile
index 87488be..1e56ecb 100644
--- a/common/makefile
+++ b/common/makefile
@@ -6,6 +6,7 @@
include $(dir $(lastword $(MAKEFILE_LIST)))../build/bootstrap.make
tests := \
+auto \
schema \
template \
lifecycle \