From 64b27b86025d160e49bf617143d80671ccb1e0e4 Mon Sep 17 00:00:00 2001 From: Michael Shepanski Date: Wed, 5 Nov 2014 14:23:54 +1100 Subject: 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). --- common/query/one/driver.cxx | 204 ++++++++++++++++++++++++++++++++++++++++++++ common/query/one/makefile | 118 +++++++++++++++++++++++++ common/query/one/test.hxx | 27 ++++++ common/query/one/test.std | 0 4 files changed, 349 insertions(+) create mode 100644 common/query/one/driver.cxx create mode 100644 common/query/one/makefile create mode 100644 common/query/one/test.hxx create mode 100644 common/query/one/test.std (limited to 'common/query/one') 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 // std::auto_ptr +#include +#include + +#include +#include + +#include + +#include "test.hxx" +#include "test-odb.hxx" + +using namespace std; +using namespace odb::core; + +int +main (int argc, char* argv[]) +{ + try + { + auto_ptr db (create_database (argc, argv)); + odb::database_id db_id (db->id ()); + + transaction t (db->begin ()); + + // query_one() + // + { + auto_ptr o (db->query_one ()); + assert (o.get () == 0); + } + + { + object o (4); + assert (!db->query_one (o) && o.id_ == 4 && o.str_.empty ()); + } + + /* + { + object o (db->query_value ()); + assert (false); + } + */ + + object o (1); + o.str_ = "value 1"; + db->persist (o); + + { + auto_ptr o (db->query_one ()); + assert (o.get () != 0 && o->str_ == "value 1"); + } + + { + object o; + assert (db->query_one (o) && o.str_ == "value 1"); + } + + { + object o (db->query_value ()); + 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 o (db->query_one (q1_c)); + assert (o.get () != 0 && o->str_ == "value 1"); + } + + { + auto_ptr o (db->query_one (q0_c)); + assert (o.get () == 0); + } + + { + object o; + assert (db->query_one (q1_c, o) && o.str_ == "value 1"); + } + + { + object o (4); + assert (!db->query_one (q0_c, o) && + o.id_ == 4 && o.str_.empty ()); + } + + { + object o (db->query_value (q1_c)); + assert (o.str_ == "value 1"); + } + + // query_one(std::string) + // + string q1_s (q1_c); + string q0_s (q0_c); + + { + auto_ptr o (db->query_one (q1_s)); + assert (o.get () != 0 && o->str_ == "value 1"); + } + + { + auto_ptr o (db->query_one (q0_s)); + assert (o.get () == 0); + } + + { + object o; + assert (db->query_one (q1_s, o) && o.str_ == "value 1"); + } + + { + object o (4); + assert (!db->query_one (q0_s, o) && + o.id_ == 4 && o.str_.empty ()); + } + + { + object o (db->query_value (q1_s)); + assert (o.str_ == "value 1"); + } + + // query_one(odb::query) + // + typedef odb::query query; + + query q1 (query::id == 1); + query q0 (query::id == 2); + + { + auto_ptr o (db->query_one (q1)); + assert (o.get () != 0 && o->str_ == "value 1"); + } + + { + auto_ptr o (db->query_one (q0)); + assert (o.get () == 0); + } + + { + object o; + assert (db->query_one (q1, o) && o.str_ == "value 1"); + } + + { + object o (4); + assert (!db->query_one (q0, o) && o.id_ == 4 && o.str_.empty ()); + } + + { + object o (db->query_value (q1)); + assert (o.str_ == "value 1"); + } + + // Assertion on more than one element. + // + { + object o (2); + o.str_ = "value 2"; + db->persist (o); + } + + /* + { + auto_ptr o (db->query_one ()); + assert (false); + } + */ + + /* + { + object o; + db->query_one (o); + assert (false); + } + */ + + /* + { + object o (db->query_value ()); + 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 + +#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 -- cgit v1.1