aboutsummaryrefslogtreecommitdiff
path: root/common
diff options
context:
space:
mode:
Diffstat (limited to 'common')
-rw-r--r--common/lifecycle/driver.cxx117
-rw-r--r--common/lifecycle/makefile82
-rw-r--r--common/lifecycle/test.hxx29
-rw-r--r--common/lifecycle/test.std0
-rw-r--r--common/makefile3
-rw-r--r--common/schema/driver.cxx3
-rw-r--r--common/schema/test.hxx3
-rw-r--r--common/template/driver.cxx3
-rw-r--r--common/template/test.hxx1
9 files changed, 239 insertions, 2 deletions
diff --git a/common/lifecycle/driver.cxx b/common/lifecycle/driver.cxx
new file mode 100644
index 0000000..4b43129
--- /dev/null
+++ b/common/lifecycle/driver.cxx
@@ -0,0 +1,117 @@
+// file : common/lifecycle/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 object state transistions.
+//
+
+#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;
+
+using odb::shared_ptr;
+
+int
+main (int argc, char* argv[])
+{
+ try
+ {
+ auto_ptr<database> db (create_database (argc, argv));
+
+ // transient
+ //
+ try
+ {
+ transaction t (db->begin_transaction ());
+ auto_ptr<object> o (db->load<object> (1));
+ assert (false);
+ t.commit ();
+ }
+ catch (const object_not_persistent&)
+ {
+ }
+
+ // persistent
+ //
+ {
+ object o (1);
+ o.str_ = "value 1";
+
+ transaction t (db->begin_transaction ());
+ db->persist (o);
+ t.commit ();
+
+ try
+ {
+ transaction t (db->begin_transaction ());
+ db->persist (o);
+ assert (false);
+ t.commit ();
+ }
+ catch (const object_already_persistent&)
+ {
+ }
+ }
+
+ {
+ transaction t (db->begin_transaction ());
+ auto_ptr<object> o (db->load<object> (1));
+ assert (o->str_ == "value 1");
+ t.commit ();
+ }
+
+ // modified
+ //
+ {
+ transaction t (db->begin_transaction ());
+ auto_ptr<object> o (db->load<object> (1));
+ o->str_ = "value 2";
+ db->store (*o);
+ t.commit ();
+ }
+
+ {
+ transaction t (db->begin_transaction ());
+ auto_ptr<object> o (db->load<object> (1));
+ assert (o->str_ == "value 2");
+ t.commit ();
+ }
+
+ // transient
+ //
+ {
+ transaction t (db->begin_transaction ());
+ auto_ptr<object> o (db->load<object> (1));
+ db->erase (*o);
+ t.commit ();
+ }
+
+ try
+ {
+ transaction t (db->begin_transaction ());
+ auto_ptr<object> o (db->load<object> (1));
+ assert (false);
+ t.commit ();
+ }
+ catch (const object_not_persistent&)
+ {
+ }
+ }
+ catch (const odb::exception& e)
+ {
+ cerr << e.what () << endl;
+ return 1;
+ }
+}
diff --git a/common/lifecycle/makefile b/common/lifecycle/makefile
new file mode 100644
index 0000000..21278fc
--- /dev/null
+++ b/common/lifecycle/makefile
@@ -0,0 +1,82 @@
+# file : common/lifecycle/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/lifecycle/test.hxx b/common/lifecycle/test.hxx
new file mode 100644
index 0000000..268678c
--- /dev/null
+++ b/common/lifecycle/test.hxx
@@ -0,0 +1,29 @@
+// file : common/lifecycle/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 (unsigned long id)
+ : id_ (id)
+ {
+ }
+
+ object ()
+ {
+ }
+
+ #pragma odb id
+ unsigned long id_;
+ std::string str_;
+};
+
+#endif // TEST_HXX
diff --git a/common/lifecycle/test.std b/common/lifecycle/test.std
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/common/lifecycle/test.std
diff --git a/common/makefile b/common/makefile
index b81fd58..021c648 100644
--- a/common/makefile
+++ b/common/makefile
@@ -7,7 +7,8 @@ include $(dir $(lastword $(MAKEFILE_LIST)))../build/bootstrap.make
tests := \
schema \
-template
+template \
+lifecycle
default := $(out_base)/
test := $(out_base)/.test
diff --git a/common/schema/driver.cxx b/common/schema/driver.cxx
index 4d27e81..1cc6be9 100644
--- a/common/schema/driver.cxx
+++ b/common/schema/driver.cxx
@@ -3,6 +3,9 @@
// copyright : Copyright (c) 2009-2010 Code Synthesis Tools CC
// license : GNU GPL v2; see accompanying LICENSE file
+// Test database schema creation.
+//
+
#include <memory> // std::auto_ptr
#include <cassert>
#include <iostream>
diff --git a/common/schema/test.hxx b/common/schema/test.hxx
index 26a8b41..14c5113 100644
--- a/common/schema/test.hxx
+++ b/common/schema/test.hxx
@@ -71,6 +71,9 @@ struct type
#pragma odb type ("INTEGER UNSIGNED")
bool m1;
+
+ #pragma odb transient
+ char* m2;
};
#endif // TEST_HXX
diff --git a/common/template/driver.cxx b/common/template/driver.cxx
index 0361c13..0aaa228 100644
--- a/common/template/driver.cxx
+++ b/common/template/driver.cxx
@@ -3,6 +3,9 @@
// copyright : Copyright (c) 2009-2010 Code Synthesis Tools CC
// license : GNU GPL v2; see accompanying LICENSE file
+// PLACE TEST DESCRIPTION HERE
+//
+
#include <memory> // std::auto_ptr
#include <cassert>
#include <iostream>
diff --git a/common/template/test.hxx b/common/template/test.hxx
index 3891a75..cb25360 100644
--- a/common/template/test.hxx
+++ b/common/template/test.hxx
@@ -6,7 +6,6 @@
#ifndef TEST_HXX
#define TEST_HXX
-#include <string>
#include <odb/core.hxx>
#pragma odb object