aboutsummaryrefslogtreecommitdiff
path: root/sqlite
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2016-10-26 16:41:51 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2016-10-26 16:41:51 +0200
commitc11a0e411d62db6f2ad4153579af254081a0e6f0 (patch)
treeac1a9e835fb87e13c959f356c5fdb650c1a3b95b /sqlite
parent6368220dda22d34f9ed50896622e6112668d0aba (diff)
Implement support for mixed auto/manual id assignment in SQLite
Now one can do: #pragma db id auto odb::nullable<int64_t> id; And then set the id to NULL to get auto-assignment or to the actual value to use a manual id.
Diffstat (limited to 'sqlite')
-rw-r--r--sqlite/auto/driver.cxx75
-rw-r--r--sqlite/auto/makefile109
-rw-r--r--sqlite/auto/test.hxx33
-rw-r--r--sqlite/auto/test.std0
-rw-r--r--sqlite/makefile1
5 files changed, 218 insertions, 0 deletions
diff --git a/sqlite/auto/driver.cxx b/sqlite/auto/driver.cxx
new file mode 100644
index 0000000..4841e03
--- /dev/null
+++ b/sqlite/auto/driver.cxx
@@ -0,0 +1,75 @@
+// file : sqlite/auto/driver.cxx
+// copyright : Copyright (c) 2009-2015 Code Synthesis Tools CC
+// license : GNU GPL v2; see accompanying LICENSE file
+
+// Test manual/automatic id assignment.
+//
+
+#include <memory> // std::auto_ptr
+#include <cassert>
+#include <iostream>
+
+#include <odb/sqlite/database.hxx>
+#include <odb/sqlite/transaction.hxx>
+
+#include <common/common.hxx>
+
+#include "test.hxx"
+#include "test-odb.hxx"
+
+using namespace std;
+namespace sqlite = odb::sqlite;
+using namespace sqlite;
+
+int
+main (int argc, char* argv[])
+{
+ try
+ {
+ auto_ptr<database> db (create_specific_database<database> (argc, argv));
+
+ // object
+ //
+ {
+ unsigned long id1, id2, id3;
+ {
+ object o1 (1, "one");
+ object o2 ("two");
+ object o3 (3, "three");
+
+ transaction t (db->begin ());
+ db->persist (o1);
+ db->persist (o2);
+ db->persist (o3);
+ t.commit ();
+
+ id1 = *o1.id_;
+ id2 = *o2.id_;
+ id3 = *o3.id_;
+
+ assert (id1 == 1);
+ assert (id3 == 3);
+
+ assert (id2 != id1);
+ assert (id2 != id3);
+ }
+
+ {
+ transaction t (db->begin ());
+ 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/sqlite/auto/makefile b/sqlite/auto/makefile
new file mode 100644
index 0000000..502743e
--- /dev/null
+++ b/sqlite/auto/makefile
@@ -0,0 +1,109 @@
+# file : sqlite/auto/makefile
+# copyright : Copyright (c) 2009-2015 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
+genf := $(call odb-gen,$(odb_hdr))
+gen := $(addprefix $(out_base)/,$(genf))
+cxx_obj := $(addprefix $(out_base)/,$(cxx_tun:.cxx=.o)) $(filter %.o,$(gen:.cxx=.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
+
+# 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)
+
+$(gen): $(odb)
+$(gen): odb := $(odb)
+$(gen) $(dist): export odb_options += --database sqlite --default-database \
+common --generate-query --generate-schema --table-prefix sqlitex_auto_ \
+--sqlite-lax-auto-id
+$(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
+#
+$(dist): sources := $(cxx_tun)
+$(dist): headers := $(odb_hdr)
+$(dist): data_dist := test.std
+$(dist): export name := $(subst /,-,$(subst $(src_root)/sqlite/,,$(src_base)))
+$(dist): export extra_dist := $(data_dist) \
+$(name)-vc8.vcproj $(name)-vc9.vcproj \
+$(name)-vc10.vcxproj $(name)-vc10.vcxproj.filters \
+$(name)-vc11.vcxproj $(name)-vc11.vcxproj.filters \
+$(name)-vc12.vcxproj $(name)-vc12.vcxproj.filters
+$(dist):
+ $(call dist-data,$(sources) $(headers) $(data_dist))
+ $(call meta-automake,../template/Makefile.am)
+ $(call meta-vc8proj,../template/template-vc8.vcproj,$(name)-vc8.vcproj)
+ $(call meta-vc9proj,../template/template-vc9.vcproj,$(name)-vc9.vcproj)
+ $(call meta-vc10proj,../template/template-vc10.vcxproj,$(name)-vc10.vcxproj)
+ $(call meta-vc11proj,../template/template-vc11.vcxproj,$(name)-vc11.vcxproj)
+ $(call meta-vc12proj,../template/template-vc12.vcxproj,$(name)-vc12.vcxproj)
+
+# Test.
+#
+$(eval $(call test-schemaless-rule))
+
+# Clean.
+#
+$(clean): \
+ $(driver).o.clean \
+ $(addsuffix .cxx.clean,$(cxx_obj)) \
+ $(addsuffix .cxx.clean,$(cxx_od)) \
+ $(addsuffix .hxx.clean,$(filter %.cxx,$(gen)))
+ $(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/vc8proj.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/vc12proj.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/sqlite/auto/test.hxx b/sqlite/auto/test.hxx
new file mode 100644
index 0000000..1ee8dbe
--- /dev/null
+++ b/sqlite/auto/test.hxx
@@ -0,0 +1,33 @@
+// file : sqlite/auto/test.hxx
+// copyright : Copyright (c) 2009-2015 Code Synthesis Tools CC
+// license : GNU GPL v2; see accompanying LICENSE file
+
+#ifndef TEST_HXX
+#define TEST_HXX
+
+#include <odb/core.hxx>
+#include <odb/nullable.hxx>
+
+#include <string>
+
+#include <odb/core.hxx>
+
+#pragma db object
+struct object
+{
+ explicit
+ object (const std::string& str): str_ (str) {}
+ object (unsigned long id, const std::string& str): id_ (id), str_ (str) {}
+
+ #pragma db auto id
+ odb::nullable<unsigned long> id_;
+
+ std::string str_;
+
+private:
+ object () {}
+
+ friend class odb::access;
+};
+
+#endif // TEST_HXX
diff --git a/sqlite/auto/test.std b/sqlite/auto/test.std
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/sqlite/auto/test.std
diff --git a/sqlite/makefile b/sqlite/makefile
index 0515772..1cd7f14 100644
--- a/sqlite/makefile
+++ b/sqlite/makefile
@@ -6,6 +6,7 @@ include $(dir $(lastword $(MAKEFILE_LIST)))../build/bootstrap.make
tests := \
template \
+auto \
custom \
database \
native \