diff options
-rw-r--r-- | common/makefile | 3 | ||||
-rw-r--r-- | common/query/driver.cxx | 162 | ||||
-rw-r--r-- | common/query/makefile | 82 | ||||
-rw-r--r-- | common/query/test.hxx | 43 | ||||
-rw-r--r-- | common/query/test.std | 15 |
5 files changed, 304 insertions, 1 deletions
diff --git a/common/makefile b/common/makefile index 021c648..ad3b0a4 100644 --- a/common/makefile +++ b/common/makefile @@ -8,7 +8,8 @@ include $(dir $(lastword $(MAKEFILE_LIST)))../build/bootstrap.make tests := \ schema \ template \ -lifecycle +lifecycle \ +query default := $(out_base)/ test := $(out_base)/.test diff --git a/common/query/driver.cxx b/common/query/driver.cxx new file mode 100644 index 0000000..214dd7b --- /dev/null +++ b/common/query/driver.cxx @@ -0,0 +1,162 @@ +// file : common/query/driver.cxx +// author : Boris Kolpackov <boris@codesynthesis.com> +// copyright : Copyright (c) 2009-2010 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +// Test query support. +// + +#include <memory> // std::auto_ptr +#include <cassert> +#include <iostream> + +#include <odb/database.hxx> +#include <odb/transaction.hxx> + +#include <common.hxx> + +#include "test.hxx" +#include "test-odb.hxx" + +using namespace std; +using namespace odb; + +using odb::shared_ptr; + +void +print (result<person>& r) +{ + for (result<person>::iterator i (r.begin ()); i != r.end (); ++i) + { + auto_ptr<person> o (*i); + cout << *o << endl; + } +} + +int +main (int argc, char* argv[]) +{ + try + { + auto_ptr<database> db (create_database (argc, argv)); + + // + // + { + person p1 (1, "John", "Doe", 30); + person p2 (2, "Jane", "Doe", 29); + person p3 (3, "Joe", "Dirt", 31); + + transaction t (db->begin_transaction ()); + db->persist (p1); + db->persist (p2); + db->persist (p3); + t.commit (); + } + + // Compilation tests. + // + if (false) + { + typedef odb::query<person> query; + + 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_name = " + query::_val (name)); + query q (age_q + "AND" + name_q); + + db->query (q); + db->query<person> (age_q + "OR" + + name_q + "OR" + + "age < " + query::_ref (age)); + } + + // Select-all query. + // + cout << "test 001" << endl; + { + transaction t (db->begin_transaction ()); + result<person> r (db->query<person> ()); + print (r); + t.commit (); + } + + // Select-all query with order by. + // + cout << "test 002" << endl; + { + transaction t (db->begin_transaction ()); + result<person> r (db->query<person> ("ORDER BY age")); + print (r); + t.commit (); + } + + // String query. + // + cout << "test 003" << endl; + { + transaction t (db->begin_transaction ()); + result<person> r (db->query<person> ("age >= 30 AND last_name = 'Doe'")); + print (r); + t.commit (); + } + + typedef odb::query<person> query; + + // Value binding. + // + cout << "test 004" << endl; + { + transaction t (db->begin_transaction ()); + + const char* name = "Doe"; + + result<person> r ( + db->query<person> ( + "age >= " + query::_ref (30) + "AND" + + "last_name = " + query::_val (name))); + + print (r); + t.commit (); + } + + // Reference binding. + // + cout << "test 005" << endl; + { + transaction t (db->begin_transaction ()); + + string name; + unsigned short age; + + query q ("age >= " + query::_ref (age) + "AND" + + "last_name = " + query::_ref (name)); + + { + name = "Doe"; + age = 30; + result<person> r (db->query<person> (q)); + print (r); + } + + { + name = "Dirt"; + age = 31; + result<person> r (db->query<person> (q)); + print (r); + } + + t.commit (); + } + } + catch (const odb::exception& e) + { + cerr << e.what () << endl; + return 1; + } +} diff --git a/common/query/makefile b/common/query/makefile new file mode 100644 index 0000000..276b948 --- /dev/null +++ b/common/query/makefile @@ -0,0 +1,82 @@ +# file : common/query/makefile +# author : Boris Kolpackov <boris@codesynthesis.com> +# copyright : Copyright (c) 2009-2010 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.l +common.l.cpp-options := $(out_root)/libcommon/common.l.cpp-options + +driver := $(out_base)/driver +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) +$(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): odb_options += --database $(db_id) --generate-schema +$(gen): cpp_options := -I$(out_base) +$(gen): $(common.l.cpp-options) + +$(call include-dep,$(cxx_od),$(cxx_obj),$(gen)) + +# Alias for default target. +# +$(out_base)/: $(driver) + +# Test. +# +$(test): $(driver) $(src_base)/test.std + $(call message,sql $$1,$(dcf_root)/db-driver $$1, $(src_base)/test.sql) + $(call message,test $<,$< --options-file $(dcf_root)/db.options \ +| diff -u $(src_base)/test.std -) + +# 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)) + +# 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,$(odb_rules)) +$(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 new file mode 100644 index 0000000..a923458 --- /dev/null +++ b/common/query/test.hxx @@ -0,0 +1,43 @@ +// file : common/query/test.hxx +// author : Boris Kolpackov <boris@codesynthesis.com> +// copyright : Copyright (c) 2009-2010 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef TEST_HXX +#define TEST_HXX + +#include <string> +#include <iostream> + +#include <odb/core.hxx> + +#pragma odb object +struct person +{ + person (unsigned long id, + const std::string& fn, + const std::string& ln, + unsigned short age) + : id_ (id), first_name_ (fn), last_name_ (ln), age_ (age) + { + } + + person () + { + } + + #pragma odb id + unsigned long id_; + + std::string first_name_; + std::string last_name_; + unsigned short age_; +}; + +inline std::ostream& +operator<< (std::ostream& os, const person& p) +{ + return os << p.first_name_ << ' ' << p.last_name_ << ' ' << p.age_; +} + +#endif // TEST_HXX diff --git a/common/query/test.std b/common/query/test.std new file mode 100644 index 0000000..0235af1 --- /dev/null +++ b/common/query/test.std @@ -0,0 +1,15 @@ +test 001 +John Doe 30 +Jane Doe 29 +Joe Dirt 31 +test 002 +Jane Doe 29 +John Doe 30 +Joe Dirt 31 +test 003 +John Doe 30 +test 004 +John Doe 30 +test 005 +John Doe 30 +Joe Dirt 31 |