diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2021-06-14 10:01:20 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2021-06-14 10:01:20 +0200 |
commit | d776aad4d4abb8eabd240621eb3d13b59a5b17d8 (patch) | |
tree | dc0ad5955eab72ba96452423aebfc6b7ad110540 /sqlite/attach | |
parent | 1357765fd5d2bca38193f9be6f38618aef0b3ab0 (diff) |
Add test for SQLite ATTACH DATABASE support
Diffstat (limited to 'sqlite/attach')
-rw-r--r-- | sqlite/attach/driver.cxx | 107 | ||||
-rw-r--r-- | sqlite/attach/makefile | 108 | ||||
-rw-r--r-- | sqlite/attach/test.hxx | 32 | ||||
-rw-r--r-- | sqlite/attach/test.std | 0 |
4 files changed, 247 insertions, 0 deletions
diff --git a/sqlite/attach/driver.cxx b/sqlite/attach/driver.cxx new file mode 100644 index 0000000..8d0672f --- /dev/null +++ b/sqlite/attach/driver.cxx @@ -0,0 +1,107 @@ +// file : sqlite/attach/driver.cxx +// license : GNU GPL v2; see accompanying LICENSE file + +// Test attached database support. +// + +#include <memory> // std::auto_ptr +#include <cassert> +#include <iostream> + +#include <odb/schema-catalog.hxx> + +#include <odb/sqlite/database.hxx> +#include <odb/sqlite/connection.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> mdb (create_specific_database<database> (argc, argv)); + + { + object o ("one"); + + connection_ptr c (mdb->connection ()); + + database adb (c, ":memory:", "adb"); + + // Create schema similar to create_database(). + // + { + c->execute ("PRAGMA foreign_keys=OFF"); + + transaction t (c->begin ()); + odb::schema_catalog::create_schema (adb); + t.commit (); + + c->execute ("PRAGMA foreign_keys=ON"); + } + + { + transaction t (c->begin ()); + mdb->persist (o); + adb.persist (o); + t.commit (); + } + + { + transaction t (c->begin ()); + auto_ptr<object> p (adb.load<object> (o.id)); + t.commit (); + + assert (p->str == o.str); + } + + { + o.str = "two"; + + transaction t (c->begin ()); + adb.update (o); + t.commit (); + } + + { + typedef sqlite::query<object> query; + + transaction t (c->begin ()); + auto_ptr<object> p (adb.query_one<object> (query::str == "two")); + t.commit (); + + assert (p->str == o.str); + } + + { + transaction t (c->begin ()); + adb.erase (o); + t.commit (); + } + + { + transaction t (c->begin ()); + auto_ptr<object> p (mdb->load<object> (o.id)); + t.commit (); + + assert (p.get () != 0); + } + + adb.detach (); + } + } + catch (const odb::exception& e) + { + cerr << e.what () << endl; + return 1; + } +} diff --git a/sqlite/attach/makefile b/sqlite/attach/makefile new file mode 100644 index 0000000..1ae325d --- /dev/null +++ b/sqlite/attach/makefile @@ -0,0 +1,108 @@ +# file : sqlite/attach/makefile +# 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_attach_ \ +--schema main +$(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/attach/test.hxx b/sqlite/attach/test.hxx new file mode 100644 index 0000000..b3b8f63 --- /dev/null +++ b/sqlite/attach/test.hxx @@ -0,0 +1,32 @@ +// file : sqlite/attach/test.hxx +// 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& s): str (s) {} + + #pragma db id auto + unsigned long id; + + #pragma db unique + std::string str; + +private: + object () {} + + friend class odb::access; +}; + +#endif // TEST_HXX diff --git a/sqlite/attach/test.std b/sqlite/attach/test.std new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/sqlite/attach/test.std |