aboutsummaryrefslogtreecommitdiff
path: root/common/query
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2013-01-24 15:10:22 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2013-01-24 15:10:22 +0200
commit89de275db2b77d0abf9fa1ec066ef11e262c88af (patch)
tree06cb2272c5e791946dc58421ed3f2d03b7c8122e /common/query
parent539c92147e8d58c49f350c4070051a6ddf6b2354 (diff)
Add support for mapping char[N] to CHAR/VARCHAR database types
Also improve query support for arrays (decaying).
Diffstat (limited to 'common/query')
-rw-r--r--common/query/array/driver.cxx170
-rw-r--r--common/query/array/makefile116
-rw-r--r--common/query/array/test.hxx77
-rw-r--r--common/query/array/test.std0
-rw-r--r--common/query/basics/driver.cxx (renamed from common/query/driver.cxx)4
-rw-r--r--common/query/basics/makefile (renamed from common/query/makefile)14
-rw-r--r--common/query/basics/test.hxx (renamed from common/query/test.hxx)2
-rw-r--r--common/query/basics/test.std (renamed from common/query/test.std)0
8 files changed, 373 insertions, 10 deletions
diff --git a/common/query/array/driver.cxx b/common/query/array/driver.cxx
new file mode 100644
index 0000000..2bda0a9
--- /dev/null
+++ b/common/query/array/driver.cxx
@@ -0,0 +1,170 @@
+// file : common/query/array/driver.cxx
+// copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC
+// license : GNU GPL v2; see accompanying LICENSE file
+
+// Test query support for C arrays.
+//
+
+#include <string>
+#include <memory> // std::auto_ptr
+#include <cassert>
+#include <iostream>
+
+#include <odb/database.hxx>
+#include <odb/transaction.hxx>
+
+#include <common/config.hxx> // DATABASE_*
+#include <common/common.hxx>
+
+#include "test.hxx"
+#include "test-odb.hxx"
+
+using namespace std;
+using namespace odb::core;
+
+#if defined(DATABASE_MYSQL)
+const odb::mysql::database_type_id bt = odb::mysql::id_blob;
+const odb::mysql::database_type_id st = odb::mysql::id_string;
+#elif defined(DATABASE_SQLITE)
+const odb::sqlite::database_type_id bt = odb::sqlite::id_blob;
+const odb::sqlite::database_type_id st = odb::sqlite::id_text;
+#elif defined(DATABASE_PGSQL)
+const odb::pgsql::database_type_id bt = odb::pgsql::id_bytea;
+const odb::pgsql::database_type_id st = odb::pgsql::id_string;
+#elif defined(DATABASE_ORACLE)
+const odb::oracle::database_type_id bt = odb::oracle::id_raw;
+const odb::oracle::database_type_id st = odb::oracle::id_string;
+#elif defined(DATABASE_MSSQL)
+const odb::mssql::database_type_id bt = odb::mssql::id_binary;
+const odb::mssql::database_type_id st = odb::mssql::id_string;
+#else
+# error unknown database
+#endif
+
+int
+main (int argc, char* argv[])
+{
+ try
+ {
+ auto_ptr<database> db (create_database (argc, argv));
+
+ typedef odb::query<object> query;
+
+ const char buf[16] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6};
+
+ //
+ //
+ {
+ object o1 (1, "abc", buf);
+ object o2 (2, "bcd", buf);
+ object o3 (3, "cde", buf);
+
+ transaction t (db->begin ());
+ db->persist (o1);
+ db->persist (o2);
+ db->persist (o3);
+ t.commit ();
+ }
+
+ {
+ transaction t (db->begin ());
+
+ // string
+ //
+ assert (size (db->query<object> (query::s == "abc")) == 1);
+ assert (size (db->query<object> (query::s == query::_val ("bcd"))) == 1);
+ assert (size (db->query<object> ("s = " + query::_val ("bcd"))) == 1);
+ assert (size (db->query<object> ("s = " + query::_ref ("bcd"))) == 1);
+
+ {
+ char a[] = "bcd";
+ char* ra = a;
+ assert (size (db->query<object> (query::s == a)) == 1);
+ assert (size (db->query<object> (query::s == query::_val (a))) == 1);
+ assert (size (db->query<object> (query::s == query::_ref (ra))) == 1);
+ assert (size (db->query<object> ("s = " + query::_val (a))) == 1);
+ assert (size (db->query<object> ("s = " + query::_ref (a))) == 1);
+ }
+
+ {
+ const char a[] = "bcd";
+ const char* ra = a;
+ assert (size (db->query<object> (query::s == a)) == 1);
+ assert (size (db->query<object> (query::s == query::_val (a))) == 1);
+ assert (size (db->query<object> (query::s == query::_ref (ra))) == 1);
+ assert (size (db->query<object> ("s = " + query::_val (a))) == 1);
+ assert (size (db->query<object> ("s = " + query::_ref (a))) == 1);
+ }
+
+ {
+ const char* p = "cde";
+ assert (size (db->query<object> (query::s == p)) == 1);
+ assert (size (db->query<object> (query::s == query::_val (p))) == 1);
+ assert (size (db->query<object> (query::s == query::_ref (p))) == 1);
+ assert (size (db->query<object> ("s = " + query::_val (p))) == 1);
+ assert (size (db->query<object> ("s = " + query::_ref (p))) == 1);
+ }
+
+ {
+ char a[] = "cde";
+ char* p = a;
+ assert (size (db->query<object> (query::s == p)) == 1);
+ assert (size (db->query<object> (query::s == query::_val (p))) == 1);
+ assert (size (db->query<object> (query::s == query::_ref (p))) == 1);
+ assert (size (db->query<object> ("s = " + query::_val (p))) == 1);
+ assert (size (db->query<object> ("s = " + query::_ref (p))) == 1);
+ }
+
+ string s ("abc");
+ //assert (size (db->query<object> (query::s == s)) == 1);
+ assert (size (db->query<object> (query::s == s.c_str ())) == 1);
+ //assert (size (db->query<object> (query::s == query::_val (s))) == 1);
+ assert (size (db->query<object> (query::s == query::_val (s.c_str ()))) == 1);
+ assert (size (db->query<object> ("s = " + query::_val (s))) == 1);
+ assert (size (db->query<object> ("s = " + query::_ref (s))) == 1);
+
+ assert (size (db->query<object> (query::s == query::s1)) == 3);
+
+ // std::array
+ //
+#ifdef ODB_CXX11
+ array<char, 17> a;
+ strcpy (a.data (), "abc");
+
+ assert (size (db->query<object> (query::a == a)) == 1);
+ assert (size (db->query<object> (query::a == query::_val (a))) == 1);
+ assert (size (db->query<object> (query::a == query::_ref (a))) == 1);
+ assert (size (db->query<object> ("a = " + query::_val<st> (a))) == 1);
+ assert (size (db->query<object> ("a = " + query::_ref<st> (a))) == 1);
+#endif
+
+ // char
+ //
+ assert (size (db->query<object> (query::c == 'a')) == 1);
+
+ char c ('b');
+ assert (size (db->query<object> (query::c == query::_val (c))) == 1);
+ assert (size (db->query<object> (query::c == query::_ref (c))) == 1);
+
+ assert (size (db->query<object> ("c = " + query::_val ('c'))) == 1);
+ assert (size (db->query<object> ("c = " + query::_ref (c))) == 1);
+
+ assert (size (db->query<object> (query::c == query::c1)) == 3);
+
+ // buffer
+ //
+ assert (size (db->query<object> (query::b == buf)) == 3);
+ assert (size (db->query<object> (query::b == query::_val (buf))) == 3);
+ assert (size (db->query<object> (query::b == query::_ref (buf))) == 3);
+ assert (size (db->query<object> ("b = " + query::_val<bt> (buf))) == 3);
+ assert (size (db->query<object> ("b = " + query::_ref<bt> (buf))) == 3);
+
+ t.commit ();
+ }
+ }
+ catch (const odb::exception& e)
+ {
+ cerr << e.what () << endl;
+ return 1;
+ }
+}
diff --git a/common/query/array/makefile b/common/query/array/makefile
new file mode 100644
index 0000000..817828f
--- /dev/null
+++ b/common/query/array/makefile
@@ -0,0 +1,116 @@
+# file : common/query/array/makefile
+# copyright : Copyright (c) 2009-2012 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/common.l
+common.l.cpp-options := $(out_root)/libcommon/common/common.l.cpp-options
+
+driver := $(out_base)/driver
+dist := $(out_base)/.dist
+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) -I$(src_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) $(dist): export odb_options += --database $(db_id) --generate-schema \
+--generate-query --generate-prepared --sql-name-case oracle:upper \
+--table-prefix t_query_array_
+$(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
+#
+name := $(subst /,-,$(subst $(src_root)/common/,,$(src_base)))
+
+$(dist): db_id := @database@
+$(dist): sources := $(cxx_tun)
+$(dist): headers := $(odb_hdr)
+$(dist): data_dist := test.std
+$(dist): export name := $(name)
+$(dist): export extra_dist := $(data_dist) $(call vc9projs,$(name)) \
+$(call vc10projs,$(name)) $(call vc11projs,$(name))
+$(dist):
+ $(call dist-data,$(sources) $(headers) $(data_dist))
+ $(call meta-automake,../../template/Makefile.am)
+ $(call meta-vc9projs,../../template/template,$(name))
+ $(call meta-vc10projs,../../template/template,$(name))
+ $(call meta-vc11projs,../../template/template,$(name))
+
+# Test.
+#
+$(test): $(driver) $(src_base)/test.std
+ $(call schema)
+ $(call message,test $<,$< --options-file $(dcf_root)/db.options \
+>$(out_base)/test.out)
+ $(call message,,diff -u $(src_base)/test.std $(out_base)/test.out)
+ $(call message,,rm -f $(out_base)/test.out)
+
+# 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))
+ $(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/vc9proj.make)
+$(call include,$(bld_root)/meta/vc10proj.make)
+$(call include,$(bld_root)/meta/vc11proj.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/common/query/array/test.hxx b/common/query/array/test.hxx
new file mode 100644
index 0000000..32ee413
--- /dev/null
+++ b/common/query/array/test.hxx
@@ -0,0 +1,77 @@
+// file : common/query/array/test.hxx
+// copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC
+// license : GNU GPL v2; see accompanying LICENSE file
+
+#ifndef TEST_HXX
+#define TEST_HXX
+
+#include <common/config.hxx> // HAVE_CXX11
+
+#include <cstring> // std::strncpy
+
+#ifdef HAVE_CXX11
+# include <array>
+#endif
+
+#include <odb/core.hxx>
+
+#pragma db object
+struct object
+{
+ object () {}
+ object (unsigned long id, const char* s, const char* b)
+ : id_ (id)
+ {
+ std::strncpy (s_, s, sizeof (s_));
+ std::strncpy (s1_, s, sizeof (s1_));
+#ifdef HAVE_CXX11
+ std::strncpy (a_.data (), s, a_.size ());
+#endif
+ c_ = c1_ = *s;
+ std::memcpy (b_, b, sizeof (b_));
+ }
+
+ #pragma db id
+ unsigned long id_;
+
+ char s_[17];
+ char s1_[17];
+
+#ifdef HAVE_CXX11
+#ifdef ODB_COMPILER
+# if defined(ODB_DATABASE_MYSQL) || \
+ defined(ODB_DATABASE_PGSQL) || \
+ defined(ODB_DATABASE_ORACLE) || \
+ defined(ODB_DATABASE_MSSQL)
+# pragma db type("VARCHAR(16)")
+# elif defined(ODB_DATABASE_SQLITE)
+# pragma db type("TEXT")
+# else
+# error unknown database
+# endif
+#endif
+ std::array<char, 17> a_;
+#endif
+
+ char c_;
+ char c1_;
+
+#ifdef ODB_COMPILER
+# if defined(ODB_DATABASE_MYSQL)
+# pragma db type("BINARY(16)")
+# elif defined(ODB_DATABASE_SQLITE)
+# pragma db type("BLOB")
+# elif defined(ODB_DATABASE_PGSQL)
+# pragma db type("BYTEA")
+# elif defined(ODB_DATABASE_ORACLE)
+# pragma db type("RAW(16)")
+# elif defined(ODB_DATABASE_MSSQL)
+# pragma db type("BINARY(16)")
+# else
+# error unknown database
+# endif
+#endif
+ char b_[16];
+};
+
+#endif // TEST_HXX
diff --git a/common/query/array/test.std b/common/query/array/test.std
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/common/query/array/test.std
diff --git a/common/query/driver.cxx b/common/query/basics/driver.cxx
index bec61fb..686b4b6 100644
--- a/common/query/driver.cxx
+++ b/common/query/basics/driver.cxx
@@ -1,8 +1,8 @@
-// file : common/query/driver.cxx
+// file : common/query/basics/driver.cxx
// copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC
// license : GNU GPL v2; see accompanying LICENSE file
-// Test query support.
+// Test basic query support.
//
#include <memory> // std::auto_ptr
diff --git a/common/query/makefile b/common/query/basics/makefile
index 6f25ba4..0be5352 100644
--- a/common/query/makefile
+++ b/common/query/basics/makefile
@@ -1,8 +1,8 @@
-# file : common/query/makefile
+# file : common/query/basics/makefile
# copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC
# license : GNU GPL v2; see accompanying LICENSE file
-include $(dir $(lastword $(MAKEFILE_LIST)))../../build/bootstrap.make
+include $(dir $(lastword $(MAKEFILE_LIST)))../../../build/bootstrap.make
cxx_tun := driver.cxx
odb_hdr := test.hxx
@@ -35,7 +35,7 @@ gen := $(addprefix $(out_base)/,$(genf))
$(gen): $(odb)
$(gen): odb := $(odb)
$(gen) $(dist): export odb_options += --database $(db_id) --generate-schema \
---generate-query --generate-prepared --table-prefix t_query_
+--generate-query --generate-prepared --table-prefix t_query_basics_
$(gen): cpp_options := -I$(src_base)
$(gen): $(common.l.cpp-options)
@@ -58,10 +58,10 @@ $(dist): export extra_dist := $(data_dist) $(call vc9projs,$(name)) \
$(call vc10projs,$(name)) $(call vc11projs,$(name))
$(dist):
$(call dist-data,$(sources) $(headers) $(data_dist))
- $(call meta-automake,../template/Makefile.am)
- $(call meta-vc9projs,../template/template,$(name))
- $(call meta-vc10projs,../template/template,$(name))
- $(call meta-vc11projs,../template/template,$(name))
+ $(call meta-automake,../../template/Makefile.am)
+ $(call meta-vc9projs,../../template/template,$(name))
+ $(call meta-vc10projs,../../template/template,$(name))
+ $(call meta-vc11projs,../../template/template,$(name))
# Test.
#
diff --git a/common/query/test.hxx b/common/query/basics/test.hxx
index 948ba30..f572d75 100644
--- a/common/query/test.hxx
+++ b/common/query/basics/test.hxx
@@ -1,4 +1,4 @@
-// file : common/query/test.hxx
+// file : common/query/basics/test.hxx
// copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC
// license : GNU GPL v2; see accompanying LICENSE file
diff --git a/common/query/test.std b/common/query/basics/test.std
index 0548968..0548968 100644
--- a/common/query/test.std
+++ b/common/query/basics/test.std