diff options
Diffstat (limited to 'common/query/basics')
-rw-r--r-- | common/query/basics/driver.cxx | 607 | ||||
-rw-r--r-- | common/query/basics/makefile | 115 | ||||
-rw-r--r-- | common/query/basics/test.hxx | 84 | ||||
-rw-r--r-- | common/query/basics/test.std | 102 |
4 files changed, 908 insertions, 0 deletions
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 <memory> // std::auto_ptr +#include <cassert> +#include <iostream> + +#include <odb/database.hxx> +#include <odb/transaction.hxx> + +#include <common/config.hxx> // DATABASE_XXX +#include <common/common.hxx> + +#include "test.hxx" +#include "test-odb.hxx" + +using namespace std; +using namespace odb::core; + +void +print (result<person>& r) +{ + for (result<person>::iterator i (r.begin ()); i != r.end (); ++i) + { + auto_ptr<person> 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<database> db (create_database (argc, argv)); + + typedef odb::query<person> query; + typedef odb::result<person> 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<person> ("age = " + query::_ref (age)); + db->query<person> ("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<person> (q); + db->query<person> (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<person> ()); + + 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<person> ("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<person> ("age >= 30 AND last = 'Doe'")); +#else + result r (db->query<person> ("\"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<person> ( + "age >= " + query::_ref (30) + "AND" + + "last = " + query::_val (name))); +#else + result r ( + db->query<person> ( + "\"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<person> (q)); + print (r); + + name = "Dirt"; + age = 31; + r = db->query<person> (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<person> (query::married); + db->query<person> (query::married == true); + + //db->query<person> (query::age); + + db->query<person> (query::age == 30); + db->query<person> (query::age == age); + db->query<person> (query::age == query::_val (30)); + db->query<person> (query::age == query::_val (age)); + db->query<person> (query::age == query::_ref (age)); + //db->query<person> (query::age == "123"); + //db->query<person> ("123" == query::age); + //db->query<person> (query::age == query::_val ("123")); + //db->query<person> (query::age == query::_ref (name)); + db->query<person> (query::last_name == "Doe"); + db->query<person> (query::last_name == name); + db->query<person> (query::last_name == query::_val ("Doe")); + db->query<person> (query::last_name == query::_val (name)); + db->query<person> (query::last_name == query::_ref (name)); + //db->query<person> (query::last_name == 30); + //db->query<person> (query::last_name == query::_val (30)); + //db->query<person> (query::last_name == query::_ref (age)); + + db->query<person> (query::last_name.is_null ()); + db->query<person> (query::last_name.is_not_null ()); + + db->query<person> (query::first_name == query::last_name); + + db->query<person> (query::first_name.in ("John", "Jane")); + db->query<person> (query::first_name.in_range (names, names_end)); + + // Query operators. + // + db->query<person> (query::age == 30 && query::last_name == "Doe"); + db->query<person> (query::age == 30 || query::last_name == "Doe"); + db->query<person> (!(query::age == 30 || query::last_name == "Doe")); + db->query<person> ((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<person> (query::middle_name.is_null ())); + print (r); + r = db->query<person> (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<person> (query::married)); + print (r); + r = db->query<person> (!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<person> (query::last_name == "Doe")); + print (r); + r = db->query<person> (query::last_name == query::_val (name)); + print (r); + query q (query::last_name == query::_ref (name)); + name = "Doe"; + r = db->query<person> (q); + print (r); + t.commit (); + } + + // Test column operators (==, !=, <, >, <=, >=). + // + cout << "test 009" << endl; + { + transaction t (db->begin ()); + + // == + // + result r (db->query<person> (query::last_name == "Doe")); + print (r); + + // != + // + r = db->query<person> (query::last_name != "Doe"); + print (r); + + // < + // + r = db->query<person> (query::age < 31); + print (r); + + // > + // + r = db->query<person> (query::age > 30); + print (r); + + // <= + // + r = db->query<person> (query::age <= 30); + print (r); + + // >= + // + r = db->query<person> (query::age >= 31); + print (r); + + t.commit (); + } + + // Test query operators (&&, ||, (), !, +). + // + cout << "test 010" << endl; + { + transaction t (db->begin ()); + + // && + // + result r (db->query<person> ( + query::last_name == "Doe" && query::age == 29)); + print (r); + + // || + // + r = db->query<person> (query::last_name == "Doe" || query::age == 31); + print (r); + + // () + // + r = db->query<person> ( + (query::last_name != "Doe" || query::age == 29) && query::married); + print (r); + + // != + // + r = db->query<person> (!(query::last_name == "Doe")); + print (r); + + // + + // + r = db->query<person> ((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<person> (query::first_name.in ("John", "Jane"))); + print (r); + + r = db->query<person> (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<person> (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<person> (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<person> (query::last_name == "Doe")); + + result::iterator i (r.begin ()); + assert (i != r.end ()); + ++i; + assert (i != r.end ()); + + { + auto_ptr<person> joe (db->load<person> (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<person> joe (db->load<person> (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<person> (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<person> (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<person> (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<person> (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<person> (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<person> (query::last_name == "None")); + assert (r.empty ()); + t.commit (); + } + + { + transaction t (db->begin ()); + result r (db->query<person> (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<person> ()); + 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<person> (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<person> (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<person> (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 <string> +#include <vector> +#include <memory> +#include <iostream> + +#include <odb/core.hxx> +#include <odb/nullable.hxx> + +#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<char> buffer; +typedef odb::nullable<buffer> 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<std::string> 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 |