From 89de275db2b77d0abf9fa1ec066ef11e262c88af Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Thu, 24 Jan 2013 15:10:22 +0200 Subject: Add support for mapping char[N] to CHAR/VARCHAR database types Also improve query support for arrays (decaying). --- common/makefile | 3 +- common/query/array/driver.cxx | 170 ++++++++++++ common/query/array/makefile | 116 ++++++++ common/query/array/test.hxx | 77 ++++++ common/query/array/test.std | 0 common/query/basics/driver.cxx | 607 +++++++++++++++++++++++++++++++++++++++++ common/query/basics/makefile | 115 ++++++++ common/query/basics/test.hxx | 84 ++++++ common/query/basics/test.std | 102 +++++++ common/query/driver.cxx | 607 ----------------------------------------- common/query/makefile | 115 -------- common/query/test.hxx | 84 ------ common/query/test.std | 102 ------- libcommon/common/common.hxx | 4 +- libcommon/common/common.txx | 2 +- mssql/types/driver.cxx | 47 ++++ mssql/types/test.hxx | 103 ++++++- mysql/types/driver.cxx | 28 ++ mysql/types/test.hxx | 47 +++- oracle/types/driver.cxx | 33 +++ oracle/types/test.hxx | 54 +++- pgsql/types/driver.cxx | 35 +++ pgsql/types/test.hxx | 48 +++- sqlite/types/driver.cxx | 34 +++ sqlite/types/test.hxx | 70 ++++- 25 files changed, 1737 insertions(+), 950 deletions(-) create mode 100644 common/query/array/driver.cxx create mode 100644 common/query/array/makefile create mode 100644 common/query/array/test.hxx create mode 100644 common/query/array/test.std create mode 100644 common/query/basics/driver.cxx create mode 100644 common/query/basics/makefile create mode 100644 common/query/basics/test.hxx create mode 100644 common/query/basics/test.std delete mode 100644 common/query/driver.cxx delete mode 100644 common/query/makefile delete mode 100644 common/query/test.hxx delete mode 100644 common/query/test.std diff --git a/common/makefile b/common/makefile index 9e6e319..eae7f7e 100644 --- a/common/makefile +++ b/common/makefile @@ -33,7 +33,8 @@ no-id \ optimistic \ pragma \ prepared \ -query \ +query/basics \ +query/array \ readonly \ relationship \ relationship-query \ 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 +#include // std::auto_ptr +#include +#include + +#include +#include + +#include // DATABASE_* +#include + +#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 db (create_database (argc, argv)); + + typedef odb::query 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 (query::s == "abc")) == 1); + assert (size (db->query (query::s == query::_val ("bcd"))) == 1); + assert (size (db->query ("s = " + query::_val ("bcd"))) == 1); + assert (size (db->query ("s = " + query::_ref ("bcd"))) == 1); + + { + char a[] = "bcd"; + char* ra = a; + assert (size (db->query (query::s == a)) == 1); + assert (size (db->query (query::s == query::_val (a))) == 1); + assert (size (db->query (query::s == query::_ref (ra))) == 1); + assert (size (db->query ("s = " + query::_val (a))) == 1); + assert (size (db->query ("s = " + query::_ref (a))) == 1); + } + + { + const char a[] = "bcd"; + const char* ra = a; + assert (size (db->query (query::s == a)) == 1); + assert (size (db->query (query::s == query::_val (a))) == 1); + assert (size (db->query (query::s == query::_ref (ra))) == 1); + assert (size (db->query ("s = " + query::_val (a))) == 1); + assert (size (db->query ("s = " + query::_ref (a))) == 1); + } + + { + const char* p = "cde"; + assert (size (db->query (query::s == p)) == 1); + assert (size (db->query (query::s == query::_val (p))) == 1); + assert (size (db->query (query::s == query::_ref (p))) == 1); + assert (size (db->query ("s = " + query::_val (p))) == 1); + assert (size (db->query ("s = " + query::_ref (p))) == 1); + } + + { + char a[] = "cde"; + char* p = a; + assert (size (db->query (query::s == p)) == 1); + assert (size (db->query (query::s == query::_val (p))) == 1); + assert (size (db->query (query::s == query::_ref (p))) == 1); + assert (size (db->query ("s = " + query::_val (p))) == 1); + assert (size (db->query ("s = " + query::_ref (p))) == 1); + } + + string s ("abc"); + //assert (size (db->query (query::s == s)) == 1); + assert (size (db->query (query::s == s.c_str ())) == 1); + //assert (size (db->query (query::s == query::_val (s))) == 1); + assert (size (db->query (query::s == query::_val (s.c_str ()))) == 1); + assert (size (db->query ("s = " + query::_val (s))) == 1); + assert (size (db->query ("s = " + query::_ref (s))) == 1); + + assert (size (db->query (query::s == query::s1)) == 3); + + // std::array + // +#ifdef ODB_CXX11 + array a; + strcpy (a.data (), "abc"); + + assert (size (db->query (query::a == a)) == 1); + assert (size (db->query (query::a == query::_val (a))) == 1); + assert (size (db->query (query::a == query::_ref (a))) == 1); + assert (size (db->query ("a = " + query::_val (a))) == 1); + assert (size (db->query ("a = " + query::_ref (a))) == 1); +#endif + + // char + // + assert (size (db->query (query::c == 'a')) == 1); + + char c ('b'); + assert (size (db->query (query::c == query::_val (c))) == 1); + assert (size (db->query (query::c == query::_ref (c))) == 1); + + assert (size (db->query ("c = " + query::_val ('c'))) == 1); + assert (size (db->query ("c = " + query::_ref (c))) == 1); + + assert (size (db->query (query::c == query::c1)) == 3); + + // buffer + // + assert (size (db->query (query::b == buf)) == 3); + assert (size (db->query (query::b == query::_val (buf))) == 3); + assert (size (db->query (query::b == query::_ref (buf))) == 3); + assert (size (db->query ("b = " + query::_val (buf))) == 3); + assert (size (db->query ("b = " + query::_ref (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 // HAVE_CXX11 + +#include // std::strncpy + +#ifdef HAVE_CXX11 +# include +#endif + +#include + +#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 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 diff --git a/common/query/basics/driver.cxx b/common/query/basics/driver.cxx new file mode 100644 index 0000000..686b4b6 --- /dev/null +++ b/common/query/basics/driver.cxx @@ -0,0 +1,607 @@ +// file : common/query/basics/driver.cxx +// copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +// Test basic query support. +// + +#include // std::auto_ptr +#include +#include + +#include +#include + +#include // DATABASE_XXX +#include + +#include "test.hxx" +#include "test-odb.hxx" + +using namespace std; +using namespace odb::core; + +void +print (result& r) +{ + for (result::iterator i (r.begin ()); i != r.end (); ++i) + { + auto_ptr o (i.load ()); + cout << *o << endl; + } + cout << endl; +} + +const char* names[] = { "John", "Jane", "Joe" }; +const char** names_end = names + sizeof (names)/sizeof (names[0]); + +const char* key_data[] = { "\x23\x03\x15", "\x13\x13\x54", "\x08\x62\x35" }; + +int +main (int argc, char* argv[]) +{ + buffer + key1 (key_data[0], key_data[0] + 3), + key2 (key_data[1], key_data[1] + 3), + key3 (key_data[2], key_data[2] + 3); + + try + { + auto_ptr db (create_database (argc, argv)); + + typedef odb::query query; + typedef odb::result result; + + // + // + { + person p1 (1, "John", "Doe", 30, true, key1); + person p2 (2, "Jane", "Doe", 29, true, key2); + person p3 (3, "Joe", "Dirt", 31, false, key3); + p3.middle_name_.reset (new string ("Squeaky")); + person p4 (4, "Johansen", "Johansen", 32, false); + p4.middle_name_.reset (new string ("J")); + + transaction t (db->begin ()); + db->persist (p1); + db->persist (p2); + db->persist (p3); + db->persist (p4); + t.commit (); + } + + // Compilation tests. + // + if (false) + { + string name; + unsigned short age; + + db->query ("age = " + query::_ref (age)); + db->query ("age = " + query::_val (age)); + + query age_q (query::_ref (age) + " = age"); + query name_q ("first = " + query::_val (name)); + query q (age_q + "AND" + name_q); + + db->query (q); + db->query (age_q + "OR" + + name_q + "OR" + + "age < " + query::_ref (age)); + + query q1 (query::_val (name)); + q1 += " = first"; + } + + // Select-all query. + // + cout << "test 001" << endl; + { + transaction t (db->begin ()); + result r (db->query ()); + + for (result::iterator i (r.begin ()); i != r.end (); ++i) + { + person p; + i.load (p); + cout << p << endl; + } + + t.commit (); + } + + // Select-all query with order by. + // + cout << "test 002" << endl; + { + transaction t (db->begin ()); + result r (db->query ("ORDER BY" + query::age)); + + for (result::iterator i (r.begin ()); i != r.end (); ++i) + { + person& p (*i); + + cout << p.first_name_; + + if (i->middle_name_.get () != 0) + cout << ' ' << *i->middle_name_; + + cout << ' ' << i->last_name_ << ' ' << i->age_ << + (i->married_ ? " married" : " single") << endl; + } + cout << endl; + + t.commit (); + } + + // String query. + // + cout << "test 003" << endl; + { + transaction t (db->begin ()); +#ifndef DATABASE_ORACLE + result r (db->query ("age >= 30 AND last = 'Doe'")); +#else + result r (db->query ("\"age\" >= 30 AND \"last\" = 'Doe'")); +#endif + print (r); + t.commit (); + } + + // Value binding. + // + cout << "test 004" << endl; + { + transaction t (db->begin ()); + + const char* name = "Doe"; + +#ifndef DATABASE_ORACLE + result r ( + db->query ( + "age >= " + query::_ref (30) + "AND" + + "last = " + query::_val (name))); +#else + result r ( + db->query ( + "\"age\" >= " + query::_ref (30) + "AND" + + "\"last\" = " + query::_val (name))); +#endif + + print (r); + t.commit (); + } + + // Reference binding. + // + cout << "test 005" << endl; + { + transaction t (db->begin ()); + + string name; + unsigned short age; + +#ifndef DATABASE_ORACLE + query q ("age >= " + query::_ref (age) + "AND" + + "last = " + query::_ref (name)); +#else + query q ("\"age\" >= " + query::_ref (age) + "AND" + + "\"last\" = " + query::_ref (name)); +#endif + + name = "Doe"; + age = 30; + result r (db->query (q)); + print (r); + + name = "Dirt"; + age = 31; + r = db->query (q); + print (r); + + t.commit (); + } + + // + // Language-embedded queries. + // + + // Compilation tests. + // + if (false) + { + string name; + unsigned short age; + + // Column operators. + // + query q1 (query::married); + db->query (query::married); + db->query (query::married == true); + + //db->query (query::age); + + db->query (query::age == 30); + db->query (query::age == age); + db->query (query::age == query::_val (30)); + db->query (query::age == query::_val (age)); + db->query (query::age == query::_ref (age)); + //db->query (query::age == "123"); + //db->query ("123" == query::age); + //db->query (query::age == query::_val ("123")); + //db->query (query::age == query::_ref (name)); + db->query (query::last_name == "Doe"); + db->query (query::last_name == name); + db->query (query::last_name == query::_val ("Doe")); + db->query (query::last_name == query::_val (name)); + db->query (query::last_name == query::_ref (name)); + //db->query (query::last_name == 30); + //db->query (query::last_name == query::_val (30)); + //db->query (query::last_name == query::_ref (age)); + + db->query (query::last_name.is_null ()); + db->query (query::last_name.is_not_null ()); + + db->query (query::first_name == query::last_name); + + db->query (query::first_name.in ("John", "Jane")); + db->query (query::first_name.in_range (names, names_end)); + + // Query operators. + // + db->query (query::age == 30 && query::last_name == "Doe"); + db->query (query::age == 30 || query::last_name == "Doe"); + db->query (!(query::age == 30 || query::last_name == "Doe")); + db->query ((query::last_name == "Doe") + "ORDER BY age"); + } + + // Test is_null/is_not_null. + // + cout << "test 006" << endl; + { + transaction t (db->begin ()); + result r (db->query (query::middle_name.is_null ())); + print (r); + r = db->query (query::middle_name.is_not_null ()); + print (r); + t.commit (); + } + + // Test boolean columns. + // + cout << "test 007" << endl; + { + transaction t (db->begin ()); + result r (db->query (query::married)); + print (r); + r = db->query (!query::married); + print (r); + t.commit (); + } + + // Test implicit by-value, explicit by-value, and by-reference. + // + cout << "test 008" << endl; + { + string name ("Dirt"); + + transaction t (db->begin ()); + result r (db->query (query::last_name == "Doe")); + print (r); + r = db->query (query::last_name == query::_val (name)); + print (r); + query q (query::last_name == query::_ref (name)); + name = "Doe"; + r = db->query (q); + print (r); + t.commit (); + } + + // Test column operators (==, !=, <, >, <=, >=). + // + cout << "test 009" << endl; + { + transaction t (db->begin ()); + + // == + // + result r (db->query (query::last_name == "Doe")); + print (r); + + // != + // + r = db->query (query::last_name != "Doe"); + print (r); + + // < + // + r = db->query (query::age < 31); + print (r); + + // > + // + r = db->query (query::age > 30); + print (r); + + // <= + // + r = db->query (query::age <= 30); + print (r); + + // >= + // + r = db->query (query::age >= 31); + print (r); + + t.commit (); + } + + // Test query operators (&&, ||, (), !, +). + // + cout << "test 010" << endl; + { + transaction t (db->begin ()); + + // && + // + result r (db->query ( + query::last_name == "Doe" && query::age == 29)); + print (r); + + // || + // + r = db->query (query::last_name == "Doe" || query::age == 31); + print (r); + + // () + // + r = db->query ( + (query::last_name != "Doe" || query::age == 29) && query::married); + print (r); + + // != + // + r = db->query (!(query::last_name == "Doe")); + print (r); + + // + + // + r = db->query ((query::last_name == "Doe") + + "ORDER BY" + query::age); + print (r); + + t.commit (); + } + + // Test in/in_range. + // + cout << "test 011" << endl; + { + transaction t (db->begin ()); + + result r (db->query (query::first_name.in ("John", "Jane"))); + print (r); + + r = db->query (query::first_name.in_range (names, names_end)); + print (r); + + t.commit (); + } + + // Test column-to-column comparison. + // + cout << "test 012" << endl; + { + transaction t (db->begin ()); + result r (db->query (query::first_name == query::last_name)); + print (r); + t.commit (); + } + + // Test value_traits::value_type != value_traits::query_type. + // + cout << "test 013" << endl; + { + transaction t (db->begin ()); + result r (db->query (query::middle_name == "Squeaky")); + print (r); + t.commit (); + } + + // Test that loading of the same object type during iteration does + // not invalidate the result. + // + cout << "test 014" << endl; + { + transaction t (db->begin ()); + result r (db->query (query::last_name == "Doe")); + + result::iterator i (r.begin ()); + assert (i != r.end ()); + ++i; + assert (i != r.end ()); + + { + auto_ptr joe (db->load (3)); + } + + { + person p (5, "Peter", "Peterson", 70, false, key3); + db->persist (p); + db->erase (p); + } + + // SQL Server does not support re-loading of an object with long data + // from a query result. + // +#ifndef DATABASE_MSSQL + assert (i->last_name_ == "Doe"); // Actual load. +#endif + + // Overwrite object image again. + // + auto_ptr joe (db->load (3)); + person p; + i.load (p); + assert (p.last_name_ == "Doe"); + + t.commit (); + } + + // Test uncached result. + // + cout << "test 015" << endl; + { + transaction t (db->begin ()); + result r (db->query (query::last_name == "Doe", false)); + print (r); + t.commit (); + } + + // Test BLOB column operations. + // + cout << "test 016" << endl; + { + transaction t (db->begin ()); + + result r; + result::iterator i; + + // Oracle does not support LOB comparisons. + // +#ifndef DATABASE_ORACLE + // == + // + r = db->query (query::public_key == key2); + + i = r.begin (); + assert (i != r.end ()); + + assert (*i->public_key_ == key2); + assert (++i == r.end ()); +#endif + + // is_null + // + r = db->query (query::public_key.is_null ()); + + i = r.begin (); + assert (i != r.end ()); + + assert (i->first_name_ == "Johansen" && i->last_name_ == "Johansen"); + assert (++i == r.end ()); + + // is_not_null + // + r = db->query (query::public_key.is_not_null ()); + + i = r.begin (); + assert (i != r.end ()); + + assert (i->first_name_ == "John" && i->last_name_ == "Doe"); + assert (++i != r.end ()); + + assert (i->first_name_ == "Jane" && i->last_name_ == "Doe"); + assert (++i != r.end ()); + + assert (i->first_name_ == "Joe" && i->last_name_ == "Dirt"); + assert (++i == r.end ()); + + t.commit (); + } + + // Test iterator::id(). + // + cout << "test 017" << endl; + { + transaction t (db->begin ()); + result r (db->query (query::last_name == "Dirt")); + + result::iterator i (r.begin ()); + assert (i != r.end ()); + assert (i.id () == 3); + + t.commit (); + } + + // Test empty result set. + // + cout << "test 018" << endl; + { + { + transaction t (db->begin ()); + result r (db->query (query::last_name == "None")); + assert (r.empty ()); + t.commit (); + } + + { + transaction t (db->begin ()); + result r (db->query (query::last_name == "None")); + assert (r.begin () == r.end ()); + t.commit (); + } + } + + // Test size() validity at the beginning/middle/end of result set. + // + cout << "test 019" << endl; +#if !defined(DATABASE_SQLITE) && \ + !defined(DATABASE_ORACLE) && \ + !defined(DATABASE_MSSQL) + { + { + transaction t (db->begin ()); + result r (db->query ()); + assert (r.size () == 4); + result::iterator i (r.begin ()); + assert (r.size () == 4); + ++i; + ++i; + ++i; + assert (r.size () == 4); + ++i; + assert (r.size () == 4); + } + + { + transaction t (db->begin ()); + result r (db->query (false)); + result::iterator i (r.begin ()); + ++i; + ++i; + r.cache (); // Cache in the middle. + assert (r.size () == 4); + ++i; + assert (r.size () == 4); + ++i; + assert (r.size () == 4); + } + + { + transaction t (db->begin ()); + result r (db->query (false)); + result::iterator i (r.begin ()); + ++i; + ++i; + ++i; + r.cache (); // Cache at the end. + assert (r.size () == 4); + } + + { + transaction t (db->begin ()); + result r (db->query (query::last_name == "None")); + assert (r.size () == 0); + for (result::iterator i (r.begin ()); i != r.end (); ++i) ; + assert (r.size () == 0); + } + } +#endif + } + catch (const odb::exception& e) + { + cerr << e.what () << endl; + return 1; + } +} diff --git a/common/query/basics/makefile b/common/query/basics/makefile new file mode 100644 index 0000000..0be5352 --- /dev/null +++ b/common/query/basics/makefile @@ -0,0 +1,115 @@ +# 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 + +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 --table-prefix t_query_basics_ +$(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/basics/test.hxx b/common/query/basics/test.hxx new file mode 100644 index 0000000..f572d75 --- /dev/null +++ b/common/query/basics/test.hxx @@ -0,0 +1,84 @@ +// file : common/query/basics/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 +#include +#include +#include + +#include +#include + +#ifdef ODB_COMPILER +# if defined(ODB_DATABASE_PGSQL) +# define BLOB_TYPE "BYTEA" +# elif defined(ODB_DATABASE_MSSQL) +# define BLOB_TYPE "VARBINARY(max)" +# else +# define BLOB_TYPE "BLOB" +# endif +#endif + +typedef std::vector buffer; +typedef odb::nullable nullable_buffer; + +#pragma db object +struct person +{ + person (unsigned long id, + const std::string& fn, + const std::string& ln, + unsigned short age, + bool married, + const nullable_buffer& public_key = nullable_buffer ()) + : id_ (id), + first_name_ (fn), + last_name_ (ln), + age_ (age), + married_ (married), + public_key_ (public_key) + { + } + + person () + { + } + + #pragma db id + unsigned long id_; + + #pragma db column ("first") + std::string first_name_; + + #pragma db column ("middle") null + std::auto_ptr middle_name_; + + #pragma db column ("last") + std::string last_name_; + + unsigned short age_; + bool married_; + + #pragma db column ("key") type(BLOB_TYPE) null + nullable_buffer public_key_; +}; + +inline std::ostream& +operator<< (std::ostream& os, const person& p) +{ + os << p.first_name_; + + if (p.middle_name_.get () != 0) + os << ' ' << *p.middle_name_; + + os << ' ' << p.last_name_ << ' ' << p.age_ << + (p.married_ ? " married" : " single"); + + return os; +} + +#endif // TEST_HXX diff --git a/common/query/basics/test.std b/common/query/basics/test.std new file mode 100644 index 0000000..0548968 --- /dev/null +++ b/common/query/basics/test.std @@ -0,0 +1,102 @@ +test 001 +John Doe 30 married +Jane Doe 29 married +Joe Squeaky Dirt 31 single +Johansen J Johansen 32 single +test 002 +Jane Doe 29 married +John Doe 30 married +Joe Squeaky Dirt 31 single +Johansen J Johansen 32 single + +test 003 +John Doe 30 married + +test 004 +John Doe 30 married + +test 005 +John Doe 30 married + +Joe Squeaky Dirt 31 single + +test 006 +John Doe 30 married +Jane Doe 29 married + +Joe Squeaky Dirt 31 single +Johansen J Johansen 32 single + +test 007 +John Doe 30 married +Jane Doe 29 married + +Joe Squeaky Dirt 31 single +Johansen J Johansen 32 single + +test 008 +John Doe 30 married +Jane Doe 29 married + +Joe Squeaky Dirt 31 single + +John Doe 30 married +Jane Doe 29 married + +test 009 +John Doe 30 married +Jane Doe 29 married + +Joe Squeaky Dirt 31 single +Johansen J Johansen 32 single + +John Doe 30 married +Jane Doe 29 married + +Joe Squeaky Dirt 31 single +Johansen J Johansen 32 single + +John Doe 30 married +Jane Doe 29 married + +Joe Squeaky Dirt 31 single +Johansen J Johansen 32 single + +test 010 +Jane Doe 29 married + +John Doe 30 married +Jane Doe 29 married +Joe Squeaky Dirt 31 single + +Jane Doe 29 married + +Joe Squeaky Dirt 31 single +Johansen J Johansen 32 single + +Jane Doe 29 married +John Doe 30 married + +test 011 +John Doe 30 married +Jane Doe 29 married + +John Doe 30 married +Jane Doe 29 married +Joe Squeaky Dirt 31 single + +test 012 +Johansen J Johansen 32 single + +test 013 +Joe Squeaky Dirt 31 single + +test 014 +test 015 +John Doe 30 married +Jane Doe 29 married + +test 016 +test 017 +test 018 +test 019 diff --git a/common/query/driver.cxx b/common/query/driver.cxx deleted file mode 100644 index bec61fb..0000000 --- a/common/query/driver.cxx +++ /dev/null @@ -1,607 +0,0 @@ -// file : common/query/driver.cxx -// copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC -// license : GNU GPL v2; see accompanying LICENSE file - -// Test query support. -// - -#include // std::auto_ptr -#include -#include - -#include -#include - -#include // DATABASE_XXX -#include - -#include "test.hxx" -#include "test-odb.hxx" - -using namespace std; -using namespace odb::core; - -void -print (result& r) -{ - for (result::iterator i (r.begin ()); i != r.end (); ++i) - { - auto_ptr o (i.load ()); - cout << *o << endl; - } - cout << endl; -} - -const char* names[] = { "John", "Jane", "Joe" }; -const char** names_end = names + sizeof (names)/sizeof (names[0]); - -const char* key_data[] = { "\x23\x03\x15", "\x13\x13\x54", "\x08\x62\x35" }; - -int -main (int argc, char* argv[]) -{ - buffer - key1 (key_data[0], key_data[0] + 3), - key2 (key_data[1], key_data[1] + 3), - key3 (key_data[2], key_data[2] + 3); - - try - { - auto_ptr db (create_database (argc, argv)); - - typedef odb::query query; - typedef odb::result result; - - // - // - { - person p1 (1, "John", "Doe", 30, true, key1); - person p2 (2, "Jane", "Doe", 29, true, key2); - person p3 (3, "Joe", "Dirt", 31, false, key3); - p3.middle_name_.reset (new string ("Squeaky")); - person p4 (4, "Johansen", "Johansen", 32, false); - p4.middle_name_.reset (new string ("J")); - - transaction t (db->begin ()); - db->persist (p1); - db->persist (p2); - db->persist (p3); - db->persist (p4); - t.commit (); - } - - // Compilation tests. - // - if (false) - { - string name; - unsigned short age; - - db->query ("age = " + query::_ref (age)); - db->query ("age = " + query::_val (age)); - - query age_q (query::_ref (age) + " = age"); - query name_q ("first = " + query::_val (name)); - query q (age_q + "AND" + name_q); - - db->query (q); - db->query (age_q + "OR" + - name_q + "OR" + - "age < " + query::_ref (age)); - - query q1 (query::_val (name)); - q1 += " = first"; - } - - // Select-all query. - // - cout << "test 001" << endl; - { - transaction t (db->begin ()); - result r (db->query ()); - - for (result::iterator i (r.begin ()); i != r.end (); ++i) - { - person p; - i.load (p); - cout << p << endl; - } - - t.commit (); - } - - // Select-all query with order by. - // - cout << "test 002" << endl; - { - transaction t (db->begin ()); - result r (db->query ("ORDER BY" + query::age)); - - for (result::iterator i (r.begin ()); i != r.end (); ++i) - { - person& p (*i); - - cout << p.first_name_; - - if (i->middle_name_.get () != 0) - cout << ' ' << *i->middle_name_; - - cout << ' ' << i->last_name_ << ' ' << i->age_ << - (i->married_ ? " married" : " single") << endl; - } - cout << endl; - - t.commit (); - } - - // String query. - // - cout << "test 003" << endl; - { - transaction t (db->begin ()); -#ifndef DATABASE_ORACLE - result r (db->query ("age >= 30 AND last = 'Doe'")); -#else - result r (db->query ("\"age\" >= 30 AND \"last\" = 'Doe'")); -#endif - print (r); - t.commit (); - } - - // Value binding. - // - cout << "test 004" << endl; - { - transaction t (db->begin ()); - - const char* name = "Doe"; - -#ifndef DATABASE_ORACLE - result r ( - db->query ( - "age >= " + query::_ref (30) + "AND" + - "last = " + query::_val (name))); -#else - result r ( - db->query ( - "\"age\" >= " + query::_ref (30) + "AND" + - "\"last\" = " + query::_val (name))); -#endif - - print (r); - t.commit (); - } - - // Reference binding. - // - cout << "test 005" << endl; - { - transaction t (db->begin ()); - - string name; - unsigned short age; - -#ifndef DATABASE_ORACLE - query q ("age >= " + query::_ref (age) + "AND" + - "last = " + query::_ref (name)); -#else - query q ("\"age\" >= " + query::_ref (age) + "AND" + - "\"last\" = " + query::_ref (name)); -#endif - - name = "Doe"; - age = 30; - result r (db->query (q)); - print (r); - - name = "Dirt"; - age = 31; - r = db->query (q); - print (r); - - t.commit (); - } - - // - // Language-embedded queries. - // - - // Compilation tests. - // - if (false) - { - string name; - unsigned short age; - - // Column operators. - // - query q1 (query::married); - db->query (query::married); - db->query (query::married == true); - - //db->query (query::age); - - db->query (query::age == 30); - db->query (query::age == age); - db->query (query::age == query::_val (30)); - db->query (query::age == query::_val (age)); - db->query (query::age == query::_ref (age)); - //db->query (query::age == "123"); - //db->query ("123" == query::age); - //db->query (query::age == query::_val ("123")); - //db->query (query::age == query::_ref (name)); - db->query (query::last_name == "Doe"); - db->query (query::last_name == name); - db->query (query::last_name == query::_val ("Doe")); - db->query (query::last_name == query::_val (name)); - db->query (query::last_name == query::_ref (name)); - //db->query (query::last_name == 30); - //db->query (query::last_name == query::_val (30)); - //db->query (query::last_name == query::_ref (age)); - - db->query (query::last_name.is_null ()); - db->query (query::last_name.is_not_null ()); - - db->query (query::first_name == query::last_name); - - db->query (query::first_name.in ("John", "Jane")); - db->query (query::first_name.in_range (names, names_end)); - - // Query operators. - // - db->query (query::age == 30 && query::last_name == "Doe"); - db->query (query::age == 30 || query::last_name == "Doe"); - db->query (!(query::age == 30 || query::last_name == "Doe")); - db->query ((query::last_name == "Doe") + "ORDER BY age"); - } - - // Test is_null/is_not_null. - // - cout << "test 006" << endl; - { - transaction t (db->begin ()); - result r (db->query (query::middle_name.is_null ())); - print (r); - r = db->query (query::middle_name.is_not_null ()); - print (r); - t.commit (); - } - - // Test boolean columns. - // - cout << "test 007" << endl; - { - transaction t (db->begin ()); - result r (db->query (query::married)); - print (r); - r = db->query (!query::married); - print (r); - t.commit (); - } - - // Test implicit by-value, explicit by-value, and by-reference. - // - cout << "test 008" << endl; - { - string name ("Dirt"); - - transaction t (db->begin ()); - result r (db->query (query::last_name == "Doe")); - print (r); - r = db->query (query::last_name == query::_val (name)); - print (r); - query q (query::last_name == query::_ref (name)); - name = "Doe"; - r = db->query (q); - print (r); - t.commit (); - } - - // Test column operators (==, !=, <, >, <=, >=). - // - cout << "test 009" << endl; - { - transaction t (db->begin ()); - - // == - // - result r (db->query (query::last_name == "Doe")); - print (r); - - // != - // - r = db->query (query::last_name != "Doe"); - print (r); - - // < - // - r = db->query (query::age < 31); - print (r); - - // > - // - r = db->query (query::age > 30); - print (r); - - // <= - // - r = db->query (query::age <= 30); - print (r); - - // >= - // - r = db->query (query::age >= 31); - print (r); - - t.commit (); - } - - // Test query operators (&&, ||, (), !, +). - // - cout << "test 010" << endl; - { - transaction t (db->begin ()); - - // && - // - result r (db->query ( - query::last_name == "Doe" && query::age == 29)); - print (r); - - // || - // - r = db->query (query::last_name == "Doe" || query::age == 31); - print (r); - - // () - // - r = db->query ( - (query::last_name != "Doe" || query::age == 29) && query::married); - print (r); - - // != - // - r = db->query (!(query::last_name == "Doe")); - print (r); - - // + - // - r = db->query ((query::last_name == "Doe") + - "ORDER BY" + query::age); - print (r); - - t.commit (); - } - - // Test in/in_range. - // - cout << "test 011" << endl; - { - transaction t (db->begin ()); - - result r (db->query (query::first_name.in ("John", "Jane"))); - print (r); - - r = db->query (query::first_name.in_range (names, names_end)); - print (r); - - t.commit (); - } - - // Test column-to-column comparison. - // - cout << "test 012" << endl; - { - transaction t (db->begin ()); - result r (db->query (query::first_name == query::last_name)); - print (r); - t.commit (); - } - - // Test value_traits::value_type != value_traits::query_type. - // - cout << "test 013" << endl; - { - transaction t (db->begin ()); - result r (db->query (query::middle_name == "Squeaky")); - print (r); - t.commit (); - } - - // Test that loading of the same object type during iteration does - // not invalidate the result. - // - cout << "test 014" << endl; - { - transaction t (db->begin ()); - result r (db->query (query::last_name == "Doe")); - - result::iterator i (r.begin ()); - assert (i != r.end ()); - ++i; - assert (i != r.end ()); - - { - auto_ptr joe (db->load (3)); - } - - { - person p (5, "Peter", "Peterson", 70, false, key3); - db->persist (p); - db->erase (p); - } - - // SQL Server does not support re-loading of an object with long data - // from a query result. - // -#ifndef DATABASE_MSSQL - assert (i->last_name_ == "Doe"); // Actual load. -#endif - - // Overwrite object image again. - // - auto_ptr joe (db->load (3)); - person p; - i.load (p); - assert (p.last_name_ == "Doe"); - - t.commit (); - } - - // Test uncached result. - // - cout << "test 015" << endl; - { - transaction t (db->begin ()); - result r (db->query (query::last_name == "Doe", false)); - print (r); - t.commit (); - } - - // Test BLOB column operations. - // - cout << "test 016" << endl; - { - transaction t (db->begin ()); - - result r; - result::iterator i; - - // Oracle does not support LOB comparisons. - // -#ifndef DATABASE_ORACLE - // == - // - r = db->query (query::public_key == key2); - - i = r.begin (); - assert (i != r.end ()); - - assert (*i->public_key_ == key2); - assert (++i == r.end ()); -#endif - - // is_null - // - r = db->query (query::public_key.is_null ()); - - i = r.begin (); - assert (i != r.end ()); - - assert (i->first_name_ == "Johansen" && i->last_name_ == "Johansen"); - assert (++i == r.end ()); - - // is_not_null - // - r = db->query (query::public_key.is_not_null ()); - - i = r.begin (); - assert (i != r.end ()); - - assert (i->first_name_ == "John" && i->last_name_ == "Doe"); - assert (++i != r.end ()); - - assert (i->first_name_ == "Jane" && i->last_name_ == "Doe"); - assert (++i != r.end ()); - - assert (i->first_name_ == "Joe" && i->last_name_ == "Dirt"); - assert (++i == r.end ()); - - t.commit (); - } - - // Test iterator::id(). - // - cout << "test 017" << endl; - { - transaction t (db->begin ()); - result r (db->query (query::last_name == "Dirt")); - - result::iterator i (r.begin ()); - assert (i != r.end ()); - assert (i.id () == 3); - - t.commit (); - } - - // Test empty result set. - // - cout << "test 018" << endl; - { - { - transaction t (db->begin ()); - result r (db->query (query::last_name == "None")); - assert (r.empty ()); - t.commit (); - } - - { - transaction t (db->begin ()); - result r (db->query (query::last_name == "None")); - assert (r.begin () == r.end ()); - t.commit (); - } - } - - // Test size() validity at the beginning/middle/end of result set. - // - cout << "test 019" << endl; -#if !defined(DATABASE_SQLITE) && \ - !defined(DATABASE_ORACLE) && \ - !defined(DATABASE_MSSQL) - { - { - transaction t (db->begin ()); - result r (db->query ()); - assert (r.size () == 4); - result::iterator i (r.begin ()); - assert (r.size () == 4); - ++i; - ++i; - ++i; - assert (r.size () == 4); - ++i; - assert (r.size () == 4); - } - - { - transaction t (db->begin ()); - result r (db->query (false)); - result::iterator i (r.begin ()); - ++i; - ++i; - r.cache (); // Cache in the middle. - assert (r.size () == 4); - ++i; - assert (r.size () == 4); - ++i; - assert (r.size () == 4); - } - - { - transaction t (db->begin ()); - result r (db->query (false)); - result::iterator i (r.begin ()); - ++i; - ++i; - ++i; - r.cache (); // Cache at the end. - assert (r.size () == 4); - } - - { - transaction t (db->begin ()); - result r (db->query (query::last_name == "None")); - assert (r.size () == 0); - for (result::iterator i (r.begin ()); i != r.end (); ++i) ; - assert (r.size () == 0); - } - } -#endif - } - catch (const odb::exception& e) - { - cerr << e.what () << endl; - return 1; - } -} diff --git a/common/query/makefile b/common/query/makefile deleted file mode 100644 index 6f25ba4..0000000 --- a/common/query/makefile +++ /dev/null @@ -1,115 +0,0 @@ -# file : common/query/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 --table-prefix t_query_ -$(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/test.hxx b/common/query/test.hxx deleted file mode 100644 index 948ba30..0000000 --- a/common/query/test.hxx +++ /dev/null @@ -1,84 +0,0 @@ -// file : common/query/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 -#include -#include -#include - -#include -#include - -#ifdef ODB_COMPILER -# if defined(ODB_DATABASE_PGSQL) -# define BLOB_TYPE "BYTEA" -# elif defined(ODB_DATABASE_MSSQL) -# define BLOB_TYPE "VARBINARY(max)" -# else -# define BLOB_TYPE "BLOB" -# endif -#endif - -typedef std::vector buffer; -typedef odb::nullable nullable_buffer; - -#pragma db object -struct person -{ - person (unsigned long id, - const std::string& fn, - const std::string& ln, - unsigned short age, - bool married, - const nullable_buffer& public_key = nullable_buffer ()) - : id_ (id), - first_name_ (fn), - last_name_ (ln), - age_ (age), - married_ (married), - public_key_ (public_key) - { - } - - person () - { - } - - #pragma db id - unsigned long id_; - - #pragma db column ("first") - std::string first_name_; - - #pragma db column ("middle") null - std::auto_ptr middle_name_; - - #pragma db column ("last") - std::string last_name_; - - unsigned short age_; - bool married_; - - #pragma db column ("key") type(BLOB_TYPE) null - nullable_buffer public_key_; -}; - -inline std::ostream& -operator<< (std::ostream& os, const person& p) -{ - os << p.first_name_; - - if (p.middle_name_.get () != 0) - os << ' ' << *p.middle_name_; - - os << ' ' << p.last_name_ << ' ' << p.age_ << - (p.married_ ? " married" : " single"); - - return os; -} - -#endif // TEST_HXX diff --git a/common/query/test.std b/common/query/test.std deleted file mode 100644 index 0548968..0000000 --- a/common/query/test.std +++ /dev/null @@ -1,102 +0,0 @@ -test 001 -John Doe 30 married -Jane Doe 29 married -Joe Squeaky Dirt 31 single -Johansen J Johansen 32 single -test 002 -Jane Doe 29 married -John Doe 30 married -Joe Squeaky Dirt 31 single -Johansen J Johansen 32 single - -test 003 -John Doe 30 married - -test 004 -John Doe 30 married - -test 005 -John Doe 30 married - -Joe Squeaky Dirt 31 single - -test 006 -John Doe 30 married -Jane Doe 29 married - -Joe Squeaky Dirt 31 single -Johansen J Johansen 32 single - -test 007 -John Doe 30 married -Jane Doe 29 married - -Joe Squeaky Dirt 31 single -Johansen J Johansen 32 single - -test 008 -John Doe 30 married -Jane Doe 29 married - -Joe Squeaky Dirt 31 single - -John Doe 30 married -Jane Doe 29 married - -test 009 -John Doe 30 married -Jane Doe 29 married - -Joe Squeaky Dirt 31 single -Johansen J Johansen 32 single - -John Doe 30 married -Jane Doe 29 married - -Joe Squeaky Dirt 31 single -Johansen J Johansen 32 single - -John Doe 30 married -Jane Doe 29 married - -Joe Squeaky Dirt 31 single -Johansen J Johansen 32 single - -test 010 -Jane Doe 29 married - -John Doe 30 married -Jane Doe 29 married -Joe Squeaky Dirt 31 single - -Jane Doe 29 married - -Joe Squeaky Dirt 31 single -Johansen J Johansen 32 single - -Jane Doe 29 married -John Doe 30 married - -test 011 -John Doe 30 married -Jane Doe 29 married - -John Doe 30 married -Jane Doe 29 married -Joe Squeaky Dirt 31 single - -test 012 -Johansen J Johansen 32 single - -test 013 -Joe Squeaky Dirt 31 single - -test 014 -test 015 -John Doe 30 married -Jane Doe 29 married - -test 016 -test 017 -test 018 -test 019 diff --git a/libcommon/common/common.hxx b/libcommon/common/common.hxx index 2b79a5a..0d56d7c 100644 --- a/libcommon/common/common.hxx +++ b/libcommon/common/common.hxx @@ -38,11 +38,11 @@ create_specific_database (int& argc, } // This function returns an accurate result only if the result iterator -// hasn't been advanced. +// hasn't been advanced and after the call the result is no longer valid. // template std::size_t -size (odb::result&); +size (odb::result); #include diff --git a/libcommon/common/common.txx b/libcommon/common/common.txx index 66ccac9..5c40520 100644 --- a/libcommon/common/common.txx +++ b/libcommon/common/common.txx @@ -11,7 +11,7 @@ size_available (); template std::size_t -size (odb::result& r) +size (odb::result r) { if (size_available ()) return r.size (); diff --git a/mssql/types/driver.cxx b/mssql/types/driver.cxx index a779079..6276bab 100644 --- a/mssql/types/driver.cxx +++ b/mssql/types/driver.cxx @@ -218,6 +218,53 @@ main (int argc, char* argv[]) } } + // Test char/wchar_t arrays. + // + { + char_array o1 (1, "", L""); + char_array o2 (2, "1234567890", L"12345678\x1FFF\xD7FF"); + char_array o3 (3, "1234567890123456", L"12345678901234\x1FFF\xD7FF"); + + { + transaction t (db->begin ()); + db->persist (o1); + db->persist (o2); + db->persist (o3); + t.commit (); + } + + // SQL Server returns padded values for CHAR(N)/NCHAR(N). + // + memcpy (o1.s2, " ", 16); + o1.s3[0] = o1.c1 = ' '; + memcpy (o2.s2, "1234567890 ", 16); + + memset (o1.ls2, ' ', 1025); + memset (o2.ls2 + 10, ' ', 1025 - 10); + + memcpy (o1.ws2, L" ", 16 * sizeof (wchar_t)); + o1.ws3[0] = o1.wc1 = L' '; + memcpy (o2.ws2, L"12345678\x1FFF\xD7FF ", 16 * sizeof (wchar_t)); + + for (size_t i (0); i < 257; ++i) + o1.lws2[i] = L' '; + + for (size_t i (10); i < 257; ++i) + o2.lws2[i] = L' '; + + { + transaction t (db->begin ()); + auto_ptr p1 (db->load (1)); + auto_ptr p2 (db->load (2)); + auto_ptr p3 (db->load (3)); + t.commit (); + + assert (o1 == *p1); + assert (o2 == *p2); + assert (o3 == *p3); + } + } + // Test optimistic concurrency using ROWVERSION. // { diff --git a/mssql/types/test.hxx b/mssql/types/test.hxx index 3e20e73..884db35 100644 --- a/mssql/types/test.hxx +++ b/mssql/types/test.hxx @@ -23,7 +23,8 @@ typedef struct _GUID #include #include #include // std::auto_ptr -#include // std::memcmp +#include // std::memcmp, std::strncpy, std::str[n]cmp +#include // std::wcsncpy, std::wcs[n]cmp #include @@ -369,6 +370,106 @@ struct long_cont } }; +// Test char/wchar_t arrays. +// +#pragma db object +struct char_array +{ + char_array () {} + char_array (unsigned long id, const char* s, const wchar_t* ws) + : id_ (id) + { + std::strncpy (s1, s, sizeof (s1)); + std::strncpy (s2, s, sizeof (s2)); + s3[0] = c1 = *s; + + std::wcsncpy (ws1, ws, sizeof (ws1) / sizeof(wchar_t)); + std::wcsncpy (ws2, ws, sizeof (ws2) / sizeof(wchar_t)); + ws3[0] = wc1 = *ws; + + if (std::strlen (s) == sizeof (s2)) + { + std::memset (ls1, '1', 1025); + ls1[1025] = '\0'; + std::memset (ls2, '2', 1025); + + for (std::size_t i (0); i < 257; ++i) + { + lws1[i] = L'1'; + lws2[i] = L'2'; + } + lws1[257] = L'\0'; + } + else + { + std::strcpy (ls1, s); + std::strcpy (ls2, s); + + std::wcscpy (lws1, ws); + std::wcscpy (lws2, ws); + } + } + + #pragma db id + unsigned long id_; + + // + // + char s1[17]; + + #pragma db type("CHAR(16)") + char s2[16]; + + char s3[1]; + char c1; + + // Long data. + // + char ls1[1026]; + + #pragma db type("CHAR(1025)") + char ls2[1025]; + + // + // + wchar_t ws1[17]; + + #pragma db type("NCHAR(16)") + wchar_t ws2[16]; + + wchar_t ws3[1]; + wchar_t wc1; + + // Long data. + // + wchar_t lws1[258]; + + #pragma db type("NCHAR(257)") + wchar_t lws2[257]; + + bool + operator== (const char_array& y) const + { + return id_ == y.id_ && + + std::strcmp (s1, y.s1) == 0 && + std::strncmp (s2, y.s2, sizeof (s2)) == 0 && + s3[0] == y.s3[0] && + c1 == y.c1 && + + std::strcmp (ls1, y.ls1) == 0 && + std::strncmp (ls2, y.ls2, sizeof (ls2)) == 0 && + + std::wcscmp (ws1, y.ws1) == 0 && + std::wcsncmp (ws2, y.ws2, sizeof (ws2) / sizeof (wchar_t)) == 0 && + ws3[0] == y.ws3[0] && + wc1 == y.wc1 && + + std::wcscmp (lws1, y.lws1) == 0 && + std::wcsncmp (lws2, y.lws2, sizeof (lws2) / sizeof (wchar_t)) == 0; + } +}; + // Test optimistic concurrency using ROWVERSION. // #pragma db object optimistic diff --git a/mysql/types/driver.cxx b/mysql/types/driver.cxx index d2f159c..0a7155e 100644 --- a/mysql/types/driver.cxx +++ b/mysql/types/driver.cxx @@ -101,6 +101,34 @@ main (int argc, char* argv[]) assert (o == *o1); } + + // Test char array. + // + { + char_array o1 (1, ""); + char_array o2 (2, "1234567890"); + char_array o3 (3, "1234567890123456"); + + { + transaction t (db->begin ()); + db->persist (o1); + db->persist (o2); + db->persist (o3); + t.commit (); + } + + { + transaction t (db->begin ()); + auto_ptr p1 (db->load (1)); + auto_ptr p2 (db->load (2)); + auto_ptr p3 (db->load (3)); + t.commit (); + + assert (o1 == *p1); + assert (o2 == *p2); + assert (o3 == *p3); + } + } } catch (const odb::exception& e) { diff --git a/mysql/types/test.hxx b/mysql/types/test.hxx index bf1ac9f..bc4c773 100644 --- a/mysql/types/test.hxx +++ b/mysql/types/test.hxx @@ -9,6 +9,7 @@ #include #include #include // std::auto_ptr +#include // std::strncpy, std::str[n]cmp #include @@ -87,14 +88,8 @@ enum color {red, green, blue}; #pragma db object struct object { - object (unsigned long id) - : id_ (id) - { - } - - object () - { - } + object () {} + object (unsigned long id): id_ (id) {} #pragma db id unsigned long id_; @@ -274,4 +269,40 @@ struct object } }; +// Test char array. +// +#pragma db object +struct char_array +{ + char_array () {} + char_array (unsigned long id, const char* s) + : id_ (id) + { + std::strncpy (s1, s, sizeof (s1)); + std::strncpy (s2, s, sizeof (s2)); + s3[0] = c1 = *s; + } + + #pragma db id + unsigned long id_; + + char s1[17]; + + #pragma db type("CHAR(16)") + char s2[16]; + + char s3[1]; + char c1; + + bool + operator== (const char_array& y) const + { + return id_ == y.id_ && + std::strcmp (s1, y.s1) == 0 && + std::strncmp (s2, y.s2, sizeof (s2)) == 0 && + s3[0] == y.s3[0] && + c1 == y.c1; + } +}; + #endif // TEST_HXX diff --git a/oracle/types/driver.cxx b/oracle/types/driver.cxx index be6251c..6370e44 100644 --- a/oracle/types/driver.cxx +++ b/oracle/types/driver.cxx @@ -272,6 +272,39 @@ main (int argc, char* argv[]) t.commit (); } + + // Test char array. + // + { + char_array o1 (1, ""); + char_array o2 (2, "1234567890"); + char_array o3 (3, "1234567890123456"); + + { + transaction t (db->begin ()); + db->persist (o1); + db->persist (o2); + db->persist (o3); + t.commit (); + } + + // Oracle returns padded values for CHAR(N) unless they are + // empty (represented as NULL). + // + memcpy (o2.s2, "1234567890 ", 16); + + { + transaction t (db->begin ()); + auto_ptr p1 (db->load (1)); + auto_ptr p2 (db->load (2)); + auto_ptr p3 (db->load (3)); + t.commit (); + + assert (o1 == *p1); + assert (o2 == *p2); + assert (o3 == *p3); + } + } } catch (const odb::exception& e) { diff --git a/oracle/types/test.hxx b/oracle/types/test.hxx index 0543598..51665c9 100644 --- a/oracle/types/test.hxx +++ b/oracle/types/test.hxx @@ -8,6 +8,7 @@ #include #include #include // std::auto_ptr +#include // std::strncpy, std::str[n]cmp #include @@ -101,14 +102,8 @@ typedef std::vector strings; #pragma db object struct object { - object (unsigned int id) - : id_ (id) - { - } - - object () - { - } + object () {} + object (unsigned long id): id_ (id) {} #pragma db id unsigned int id_; @@ -166,13 +161,13 @@ struct object #pragma db type ("CHAR(13)") std::string char_; - #pragma db type ("VARCHAR2(512)") + #pragma db type ("VARCHAR2(512)") null std::string varchar2_; #pragma db type ("NCHAR(8)") std::string nchar_; - #pragma db type ("NVARCHAR2(512)") + #pragma db type ("NVARCHAR2(512)") null std::string nvarchar2_; // Oracle treats empty and NULL VARCHAR2 the same. Test that we @@ -302,4 +297,43 @@ struct blob } }; +// Test char array. +// +#pragma db object +struct char_array +{ + char_array () {} + char_array (unsigned long id, const char* s) + : id_ (id) + { + std::strncpy (s1, s, sizeof (s1)); + std::strncpy (s2, s, sizeof (s2)); + s3[0] = c1 = *s; + } + + #pragma db id + unsigned long id_; + + char s1[17]; + + #pragma db type("CHAR(16)") null + char s2[16]; + + #pragma db null + char s3[1]; + + #pragma db null + char c1; + + bool + operator== (const char_array& y) const + { + return id_ == y.id_ && + std::strcmp (s1, y.s1) == 0 && + std::strncmp (s2, y.s2, sizeof (s2)) == 0 && + s3[0] == y.s3[0] && + c1 == y.c1; + } +}; + #endif // TEST_HXX diff --git a/pgsql/types/driver.cxx b/pgsql/types/driver.cxx index 8b969aa..d2e5e80 100644 --- a/pgsql/types/driver.cxx +++ b/pgsql/types/driver.cxx @@ -85,6 +85,41 @@ main (int argc, char* argv[]) assert (o == *o1); } + + // Test char array. + // + { + char_array o1 (1, ""); + char_array o2 (2, "1234567890"); + char_array o3 (3, "1234567890123456"); + + + { + transaction t (db->begin ()); + db->persist (o1); + db->persist (o2); + db->persist (o3); + t.commit (); + } + + // PostgreSQL returns padded values for CHAR(N). + // + memcpy (o1.s2, " ", 16); + o1.s3[0] = o1.c1 = ' '; + memcpy (o2.s2, "1234567890 ", 16); + + { + transaction t (db->begin ()); + auto_ptr p1 (db->load (1)); + auto_ptr p2 (db->load (2)); + auto_ptr p3 (db->load (3)); + t.commit (); + + assert (o1 == *p1); + assert (o2 == *p2); + assert (o3 == *p3); + } + } } catch (const odb::exception& e) { diff --git a/pgsql/types/test.hxx b/pgsql/types/test.hxx index 34f913f..3078576 100644 --- a/pgsql/types/test.hxx +++ b/pgsql/types/test.hxx @@ -9,7 +9,7 @@ #include #include #include // std::auto_ptr -#include // std::memcmp +#include // std::memcmp, std::strncpy, std::str[n]cmp #include // std::size_t #include @@ -79,14 +79,8 @@ enum color {red, green, blue}; #pragma db object struct object { - object (unsigned long id) - : id_ (id) - { - } - - object () - { - } + object () {} + object (unsigned long id): id_ (id) {} #pragma db id unsigned long id_; @@ -188,4 +182,40 @@ struct object } }; +// Test char array. +// +#pragma db object +struct char_array +{ + char_array () {} + char_array (unsigned long id, const char* s) + : id_ (id) + { + std::strncpy (s1, s, sizeof (s1)); + std::strncpy (s2, s, sizeof (s2)); + s3[0] = c1 = *s; + } + + #pragma db id + unsigned long id_; + + char s1[17]; + + #pragma db type("CHAR(16)") + char s2[16]; + + char s3[1]; + char c1; + + bool + operator== (const char_array& y) const + { + return id_ == y.id_ && + std::strcmp (s1, y.s1) == 0 && + std::strncmp (s2, y.s2, sizeof (s2)) == 0 && + s3[0] == y.s3[0] && + c1 == y.c1; + } +}; + #endif // TEST_HXX diff --git a/sqlite/types/driver.cxx b/sqlite/types/driver.cxx index 69e8473..4e619fb 100644 --- a/sqlite/types/driver.cxx +++ b/sqlite/types/driver.cxx @@ -69,6 +69,40 @@ main (int argc, char* argv[]) t.commit (); } #endif + + // Test char/wchar_t arrays + // + { +#ifndef _WIN32 + char_array o1 (1, ""); + char_array o2 (2, "1234567890"); + char_array o3 (3, "12345678901234567"); +#else + char_array o1 (1, "", L""); + char_array o2 (2, "1234567890", L"123456789\x00C8"); + char_array o3 (3, "12345678901234567", L"1234567890123456\x00C8"); +#endif + + { + transaction t (db->begin ()); + db->persist (o1); + db->persist (o2); + db->persist (o3); + t.commit (); + } + + { + transaction t (db->begin ()); + auto_ptr p1 (db->load (1)); + auto_ptr p2 (db->load (2)); + auto_ptr p3 (db->load (3)); + t.commit (); + + assert (o1 == *p1); + assert (o2 == *p2); + assert (o3 == *p3); + } + } } catch (const odb::exception& e) { diff --git a/sqlite/types/test.hxx b/sqlite/types/test.hxx index fdaa541..a19cd20 100644 --- a/sqlite/types/test.hxx +++ b/sqlite/types/test.hxx @@ -9,6 +9,11 @@ #include #include #include // std::auto_ptr +#include // std::strncpy, std::str[n]cmp + +#ifdef _WIN32 +# include // std::wcsncpy, std::wcs[n]cmp +#endif #include @@ -17,14 +22,8 @@ typedef std::auto_ptr string_ptr; #pragma db object struct object { - object (unsigned long id) - : id_ (id) - { - } - - object () - { - } + object () {} + object (unsigned long id): id_ (id) {} #pragma db id unsigned long id_; @@ -38,8 +37,7 @@ struct object #pragma db type("REAL") double real_; - #pragma db type("REAL") - double nan_; + double nan_; // Represented in SQLite as NULL. #pragma db type("TEXT") std::string text_; @@ -73,4 +71,56 @@ struct object } }; +// Test char/wchar_t arrays. +// +#pragma db object +struct char_array +{ + char_array () {} + char_array (unsigned long id + , const char* s +#ifdef _WIN32 + , const wchar_t* ws +#endif + ) + : id_ (id) + { + std::strncpy (s1, s, sizeof (s1)); + s2[0] = c1 = *s; + +#ifdef _WIN32 + std::wcsncpy (ws1, ws, sizeof (ws1) / 2); + ws2[0] = wc1 = *ws; +#endif + } + + #pragma db id + unsigned long id_; + + char s1[17]; + char s2[1]; + char c1; + +#ifdef _WIN32 + wchar_t ws1[17]; + wchar_t ws2[1]; + wchar_t wc1; +#endif + + bool + operator== (const char_array& y) const + { + return id_ == y.id_ + && std::strncmp (s1, y.s1, sizeof (s1)) == 0 + && s2[0] == y.s2[0] + && c1 == y.c1 +#ifdef _WIN32 + && std::wcsncmp (ws1, y.ws1, sizeof (ws1) / 2) == 0 + && ws2[0] == y.ws2[0] + && wc1 == y.wc1 +#endif + ; + } +}; + #endif // TEST_HXX -- cgit v1.1