diff options
author | Michael Shepanski <michael@codesynthesis.com> | 2014-11-05 14:23:54 +1100 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2014-11-10 16:48:59 +0200 |
commit | 64b27b86025d160e49bf617143d80671ccb1e0e4 (patch) | |
tree | 1abc0df10d797d0d845e0590c1cb9a077e4a42af /common | |
parent | dee5f10d1d75eee43596ad1dfb47ecec9e0b1307 (diff) |
Implement {query,execute}_{one,value}() shortcut functions
Useful in situations where the query is know to return at most one element
(*_one) or exactly one element (*_value).
Diffstat (limited to 'common')
-rw-r--r-- | common/makefile | 1 | ||||
-rw-r--r-- | common/prepared/driver.cxx | 40 | ||||
-rw-r--r-- | common/prepared/test.hxx | 4 | ||||
-rw-r--r-- | common/query/one/driver.cxx | 204 | ||||
-rw-r--r-- | common/query/one/makefile | 118 | ||||
-rw-r--r-- | common/query/one/test.hxx | 27 | ||||
-rw-r--r-- | common/query/one/test.std | 0 | ||||
-rw-r--r-- | common/view/driver.cxx | 19 |
8 files changed, 411 insertions, 2 deletions
diff --git a/common/makefile b/common/makefile index 0fe8ffe..aa195ba 100644 --- a/common/makefile +++ b/common/makefile @@ -38,6 +38,7 @@ pragma \ prepared \ query/basics \ query/array \ +query/one \ readonly \ relationship/basics \ relationship/on-delete \ diff --git a/common/prepared/driver.cxx b/common/prepared/driver.cxx index dfbdf13..ee690a1 100644 --- a/common/prepared/driver.cxx +++ b/common/prepared/driver.cxx @@ -411,6 +411,46 @@ main (int argc, char* argv[]) t.commit (); } + + // Test execute_one() and execute_value(). + // + { + transaction t (db->begin ()); + + person p ("John Doe", 23); + db->persist (p); + + prep_query pq1 ( + db->prepare_query<person> ("query-1", query::id == p.id_)); + prep_query pq0 ( + db->prepare_query<person> ("query-0", query::id == p.id_ + 1)); + + { + auto_ptr<person> p (pq1.execute_one ()); + assert (p.get () != 0 && p->name_ == "John Doe"); + } + + { + auto_ptr<person> p (pq0.execute_one ()); + assert (p.get () == 0); + } + + { + person p; + assert (pq1.execute_one (p) && p.name_ == "John Doe"); + } + + { + person p ("", 0); + assert (!pq0.execute_one (p) && + p.id_ == 0 && p.name_.empty () && p.age_ == 0); + } + + { + person p (pq1.execute_value ()); + assert (p.name_ == "John Doe"); + } + } } catch (const odb::exception& e) { diff --git a/common/prepared/test.hxx b/common/prepared/test.hxx index c6e2c1e..571f4b6 100644 --- a/common/prepared/test.hxx +++ b/common/prepared/test.hxx @@ -12,9 +12,9 @@ #pragma db object struct person { - person () {} + person (): id_ (0) {} person (const std::string& name, unsigned short age) - : name_ (name), age_ (age) {} + : id_ (0), name_ (name), age_ (age) {} #pragma db id auto unsigned long id_; diff --git a/common/query/one/driver.cxx b/common/query/one/driver.cxx new file mode 100644 index 0000000..e6b2b67 --- /dev/null +++ b/common/query/one/driver.cxx @@ -0,0 +1,204 @@ +// file : common/query/one/driver.cxx +// copyright : Copyright (c) 2009-2014 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +// Test query one support. +// +// We assume that other tests in common/query/ exercise a variety of +// different kinds of queries. Here we are concerned with what is +// specific to query_one() and query_value(). +// + +#include <memory> // std::auto_ptr +#include <cassert> +#include <iostream> + +#include <odb/database.hxx> +#include <odb/transaction.hxx> + +#include <common/common.hxx> + +#include "test.hxx" +#include "test-odb.hxx" + +using namespace std; +using namespace odb::core; + +int +main (int argc, char* argv[]) +{ + try + { + auto_ptr<database> db (create_database (argc, argv)); + odb::database_id db_id (db->id ()); + + transaction t (db->begin ()); + + // query_one() + // + { + auto_ptr<object> o (db->query_one<object> ()); + assert (o.get () == 0); + } + + { + object o (4); + assert (!db->query_one<object> (o) && o.id_ == 4 && o.str_.empty ()); + } + + /* + { + object o (db->query_value<object> ()); + assert (false); + } + */ + + object o (1); + o.str_ = "value 1"; + db->persist (o); + + { + auto_ptr<object> o (db->query_one<object> ()); + assert (o.get () != 0 && o->str_ == "value 1"); + } + + { + object o; + assert (db->query_one<object> (o) && o.str_ == "value 1"); + } + + { + object o (db->query_value<object> ()); + assert (o.str_ == "value 1"); + } + + // query_one(const char*) + // + const char* q1_c (db_id == odb::id_oracle ? "\"id\" = 1" : "id = 1"); + const char* q0_c (db_id == odb::id_oracle ? "\"id\" = 2" : "id = 2"); + + { + auto_ptr<object> o (db->query_one<object> (q1_c)); + assert (o.get () != 0 && o->str_ == "value 1"); + } + + { + auto_ptr<object> o (db->query_one<object> (q0_c)); + assert (o.get () == 0); + } + + { + object o; + assert (db->query_one<object> (q1_c, o) && o.str_ == "value 1"); + } + + { + object o (4); + assert (!db->query_one<object> (q0_c, o) && + o.id_ == 4 && o.str_.empty ()); + } + + { + object o (db->query_value<object> (q1_c)); + assert (o.str_ == "value 1"); + } + + // query_one(std::string) + // + string q1_s (q1_c); + string q0_s (q0_c); + + { + auto_ptr<object> o (db->query_one<object> (q1_s)); + assert (o.get () != 0 && o->str_ == "value 1"); + } + + { + auto_ptr<object> o (db->query_one<object> (q0_s)); + assert (o.get () == 0); + } + + { + object o; + assert (db->query_one<object> (q1_s, o) && o.str_ == "value 1"); + } + + { + object o (4); + assert (!db->query_one<object> (q0_s, o) && + o.id_ == 4 && o.str_.empty ()); + } + + { + object o (db->query_value<object> (q1_s)); + assert (o.str_ == "value 1"); + } + + // query_one(odb::query) + // + typedef odb::query<object> query; + + query q1 (query::id == 1); + query q0 (query::id == 2); + + { + auto_ptr<object> o (db->query_one<object> (q1)); + assert (o.get () != 0 && o->str_ == "value 1"); + } + + { + auto_ptr<object> o (db->query_one<object> (q0)); + assert (o.get () == 0); + } + + { + object o; + assert (db->query_one<object> (q1, o) && o.str_ == "value 1"); + } + + { + object o (4); + assert (!db->query_one<object> (q0, o) && o.id_ == 4 && o.str_.empty ()); + } + + { + object o (db->query_value<object> (q1)); + assert (o.str_ == "value 1"); + } + + // Assertion on more than one element. + // + { + object o (2); + o.str_ = "value 2"; + db->persist (o); + } + + /* + { + auto_ptr<object> o (db->query_one<object> ()); + assert (false); + } + */ + + /* + { + object o; + db->query_one<object> (o); + assert (false); + } + */ + + /* + { + object o (db->query_value<object> ()); + assert (false); + } + */ + } + catch (const odb::exception& e) + { + cerr << e.what () << endl; + return 1; + } +} diff --git a/common/query/one/makefile b/common/query/one/makefile new file mode 100644 index 0000000..2fd441b --- /dev/null +++ b/common/query/one/makefile @@ -0,0 +1,118 @@ +# file : common/query/one/makefile +# copyright : Copyright (c) 2009-2013 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 +genf := $(call odb-gen,$(odb_hdr)) +gen := $(addprefix $(out_base)/,$(genf)) +cxx_obj := $(addprefix $(out_base)/,$(cxx_tun:.cxx=.o)) $(filter %.o,$(gen:.cxx=.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 + +# 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) + +$(gen): $(odb) +$(gen): odb := $(odb) +$(gen) $(dist): export odb_options += --generate-schema --generate-query \ +--generate-prepared --table-prefix t_query_one_ +$(gen): cpp_options := -I$(src_base) +$(gen): $(common.l.cpp-options) + +ifneq ($(db_id),common) +$(gen): odb_options += --database $(db_id) +else +$(gen): odb_options += --multi-database dynamic +endif + +$(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): sources := $(cxx_tun) +$(dist): headers := $(odb_hdr) +$(dist): data_dist := test.std +$(dist): export name := $(name) +$(dist): export extra_dist := $(data_dist) $(call vc8projs,$(name)) \ +$(call vc9projs,$(name)) $(call vc10projs,$(name)) $(call vc11projs,$(name)) \ +$(call vc12projs,$(name)) +$(dist): + $(call dist-data,$(sources) $(headers) $(data_dist)) + $(call meta-automake,../../template/Makefile.am) + $(call meta-vc8projs,../../template/template,$(name)) + $(call meta-vc9projs,../../template/template,$(name)) + $(call meta-vc10projs,../../template/template,$(name)) + $(call meta-vc11projs,../../template/template,$(name)) + $(call meta-vc12projs,../../template/template,$(name)) + +# Test. +# +ifneq ($(db_id),common) +$(eval $(call test-rule)) +else +$(foreach d,$(databases),$(eval $(call test-rule,$d))) +endif + +# Clean. +# +$(clean): \ + $(driver).o.clean \ + $(addsuffix .cxx.clean,$(cxx_obj)) \ + $(addsuffix .cxx.clean,$(cxx_od)) \ + $(addsuffix .hxx.clean,$(filter %.cxx,$(gen))) + $(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/vc8proj.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/vc12proj.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/one/test.hxx b/common/query/one/test.hxx new file mode 100644 index 0000000..1a9c35e --- /dev/null +++ b/common/query/one/test.hxx @@ -0,0 +1,27 @@ +// file : common/query/one/test.hxx +// copyright : Copyright (c) 2009-2014 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef TEST_HXX +#define TEST_HXX + +#include <string> + +#pragma db object +struct object +{ + object (unsigned long id) + : id_ (id) + { + } + + object () + { + } + + #pragma db id + unsigned long id_; + std::string str_; +}; + +#endif // TEST_HXX diff --git a/common/query/one/test.std b/common/query/one/test.std new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/common/query/one/test.std diff --git a/common/view/driver.cxx b/common/view/driver.cxx index 7ef8272..28c3ba0 100644 --- a/common/view/driver.cxx +++ b/common/view/driver.cxx @@ -76,6 +76,25 @@ view2_test (const auto_ptr<database>& db) assert (i->count == 2); } + { + auto_ptr<V> v (db->query_one<V> ()); + assert (v->count == 4); + } + + { + auto_ptr<V> v; + if (db->id () != odb::id_oracle) + v.reset (db->query_one<V> ("age < 31")); + else + v.reset (db->query_one<V> ("\"age\" < 31")); + assert (v->count == 2); + } + + { + auto_ptr<V> v (db->query_one<V> (query::age < 31)); + assert (v->count == 2); + } + t.commit (); } |