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 ------- 13 files changed, 1273 insertions(+), 909 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 (limited to 'common') 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 -- cgit v1.1