diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2015-07-15 18:43:03 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2015-07-15 18:43:03 +0200 |
commit | 0a2b28b4034a6b44f7bb7a126935d9e58cc0c1f9 (patch) | |
tree | 65ecf66a742cc7f80f784b996fcb7a3e6ca14253 /sqlite/stream | |
parent | 0d65234bc5c6742721c00360a0e3117d51d89c5f (diff) |
Implement SQLite incremental BLOB/TEXT I/O
Diffstat (limited to 'sqlite/stream')
-rw-r--r-- | sqlite/stream/driver.cxx | 280 | ||||
-rw-r--r-- | sqlite/stream/makefile | 108 | ||||
-rw-r--r-- | sqlite/stream/test.hxx | 38 | ||||
-rw-r--r-- | sqlite/stream/test.std | 0 |
4 files changed, 426 insertions, 0 deletions
diff --git a/sqlite/stream/driver.cxx b/sqlite/stream/driver.cxx new file mode 100644 index 0000000..5f708ba --- /dev/null +++ b/sqlite/stream/driver.cxx @@ -0,0 +1,280 @@ +// file : sqlite/stream/driver.cxx +// copyright : Copyright (c) 2009-2015 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +// Test SQLite BLOB/TEXT incremental I/O. +// + +#include <vector> +#include <memory> // std::auto_ptr +#include <cassert> +#include <iostream> + +#include <odb/sqlite/database.hxx> +#include <odb/sqlite/transaction.hxx> + +#include <odb/sqlite/text-stream.hxx> +#include <odb/sqlite/blob-stream.hxx> + +#include <common/common.hxx> + +#include "test.hxx" +#include "test-odb.hxx" + +using namespace std; +namespace sqlite = odb::sqlite; +using namespace sqlite; + +template <typename S> +void +print (const S&) +{ + /* + cerr << s.db () << '.' + << s.table () << '.' + << s.column () << '#' + << s.rowid () << endl; + */ +} + +int +main (int argc, char* argv[]) +{ + try + { + auto_ptr<database> db (create_specific_database<database> (argc, argv)); + + string txt (1024 * 1024, 't'); + vector<char> blb (1024 * 1024, 'b'); + + object o; + + { + transaction tx (db->begin ()); + + o.t.size (txt.size ()); + o.b.size (blb.size ()); + o.bv.push_back (blob (blb.size ())); + o.bv.push_back (blob (blb.size ())); + + db->persist (o); + + print (o.t); + print (o.b); + print (o.bv[0]); + print (o.bv[1]); + + blob_stream bs (o.b, true); + bs.write (blb.data (), blb.size ()); + + text_stream ts (o.t, true); + ts.write (txt.data (), txt.size ()); + + for (vector<blob>::iterator i (o.bv.begin ()); i != o.bv.end (); ++i) + { + blob_stream bs (*i, true); + bs.write (blb.data (), blb.size ()); + } + + tx.commit (); + } + + { + transaction tx (db->begin ()); + auto_ptr<object> p (db->load<object> (o.id)); + + print (p->t); + print (p->b); + print (p->bv[0]); + print (p->bv[1]); + + text_stream ts (p->t, false); + string t (ts.size (), '*'); + ts.read (const_cast<char*> (t.c_str ()), t.size ()); + assert (t == txt); + + blob_stream bs (p->b, false); + vector<char> b (bs.size (), '\0'); + bs.read (b.data (), b.size ()); + assert (b == blb); + + for (vector<blob>::iterator i (p->bv.begin ()); i != p->bv.end (); ++i) + { + blob_stream bs (*i, false); + vector<char> b (bs.size (), '\0'); + bs.read (b.data (), b.size ()); + assert (b == blb); + } + + assert (p->nb.null ()); + + tx.commit (); + } + + txt.resize (txt.size () + 1, 't'); + txt[0] = 'A'; + txt[txt.size () - 1] = 'Z'; + + blb.resize (blb.size () - 1); + blb.front () = 'A'; + blb.back () = 'Z'; + + { + transaction tx (db->begin ()); + + o.t.clear (); + o.t.size (txt.size ()); + + o.b.clear (); + o.b.size (blb.size ()); + + o.bv[0].clear (); + o.bv[0].size (blb.size ()); + + o.bv[1].clear (); + o.bv[1].size (blb.size ()); + + o.nb = blob (blb.size ()); + + db->update (o); + + print (o.t); + print (o.b); + print (o.bv[0]); + print (o.bv[1]); + print (*o.nb); + + { + text_stream ts (o.t, true); + ts.write (txt.data (), txt.size ()); + } + + { + blob_stream bs (o.b, true); + bs.write (blb.data (), blb.size ()); + } + + for (vector<blob>::iterator i (o.bv.begin ()); i != o.bv.end (); ++i) + { + blob_stream bs (*i, true); + bs.write (blb.data (), blb.size ()); + } + + { + blob_stream bs (*o.nb, true); + bs.write (blb.data (), blb.size ()); + } + + tx.commit (); + } + + { + transaction tx (db->begin ()); + auto_ptr<object> p (db->load<object> (o.id)); + + print (p->t); + print (p->b); + print (p->bv[0]); + print (p->bv[1]); + print (*p->nb); + + text_stream ts (p->t, false); + string t (ts.size (), '*'); + ts.read (const_cast<char*> (t.c_str ()), t.size ()); + assert (t == txt); + + blob_stream bs (p->b, false); + vector<char> b (bs.size (), '\0'); + bs.read (b.data (), b.size ()); + assert (b == blb); + + for (vector<blob>::iterator i (p->bv.begin ()); i != p->bv.end (); ++i) + { + blob_stream bs (*i, false); + vector<char> b (bs.size (), '\0'); + bs.read (b.data (), b.size ()); + assert (b == blb); + } + + { + blob_stream bs (*p->nb, false); + vector<char> b (bs.size (), '\0'); + bs.read (b.data (), b.size ()); + assert (b == blb); + } + + tx.commit (); + } + + // Test query. + // + + txt.resize (32); + blb.resize (64); + + { + transaction tx (db->begin ()); + + o.t.size (txt.size ()); + o.b.size (blb.size ()); + o.bv.clear (); + o.nb.reset (); + + db->update (o); + + text_stream ts (o.t, true); + ts.write (txt.data (), txt.size ()); + + blob_stream bs (o.b, true); + bs.write (blb.data (), blb.size ()); + + tx.commit (); + } + + { + typedef sqlite::query<object> query; + transaction tx (db->begin ()); + + { + object o1 (db->query_value<object> (query::t == txt)); + + blob_stream bs (o1.b, false); + vector<char> b (bs.size (), '\0'); + bs.read (b.data (), b.size ()); + assert (b == blb); + } + + { + object o1 (db->query_value<object> (query::b == blb)); + + text_stream ts (o1.t, false); + string t (ts.size (), '*'); + ts.read (const_cast<char*> (t.c_str ()), t.size ()); + assert (t == txt); + } + + tx.commit (); + } + + // Test view. + // + { + typedef sqlite::query<view> query; + transaction tx (db->begin ()); + + view v (db->query_value<view> (query::t == txt)); + + blob_stream bs (v.b, false); + vector<char> b (bs.size (), '\0'); + bs.read (b.data (), b.size ()); + assert (b == blb); + + tx.commit (); + } + } + catch (const odb::exception& e) + { + cerr << e.what () << endl; + return 1; + } +} diff --git a/sqlite/stream/makefile b/sqlite/stream/makefile new file mode 100644 index 0000000..5f63159 --- /dev/null +++ b/sqlite/stream/makefile @@ -0,0 +1,108 @@ +# file : sqlite/stream/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_stream_ +$(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/stream/test.hxx b/sqlite/stream/test.hxx new file mode 100644 index 0000000..32c29e3 --- /dev/null +++ b/sqlite/stream/test.hxx @@ -0,0 +1,38 @@ +// file : sqlite/stream/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 <vector> + +#include <odb/core.hxx> +#include <odb/nullable.hxx> + +#include <odb/sqlite/text.hxx> +#include <odb/sqlite/blob.hxx> + +#pragma db object +struct object +{ + #pragma db id auto + unsigned long id; + + odb::sqlite::text t; + + #pragma db column("_123foo_bar") + odb::sqlite::blob b; + + std::vector<odb::sqlite::blob> bv; + + odb::nullable<odb::sqlite::blob> nb; +}; + +#pragma db view object(object) +struct view +{ + odb::sqlite::blob b; +}; + +#endif // TEST_HXX diff --git a/sqlite/stream/test.std b/sqlite/stream/test.std new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/sqlite/stream/test.std |