From 8bf752fc2354af4e98a43e208d1eb18c8a71e48b Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Mon, 5 Sep 2011 10:36:33 +0200 Subject: Support for views; native part --- common/makefile | 1 + common/view/driver.cxx | 185 +++++++++++++++++++++++++++++++++++++++++++++++++ common/view/makefile | 109 +++++++++++++++++++++++++++++ common/view/test.hxx | 114 ++++++++++++++++++++++++++++++ common/view/test.std | 0 5 files changed, 409 insertions(+) create mode 100644 common/view/driver.cxx create mode 100644 common/view/makefile create mode 100644 common/view/test.hxx create mode 100644 common/view/test.std diff --git a/common/makefile b/common/makefile index b3af876..a2f4962 100644 --- a/common/makefile +++ b/common/makefile @@ -23,6 +23,7 @@ query \ relationship \ schema \ template \ +view \ wrapper thread_tests := threads diff --git a/common/view/driver.cxx b/common/view/driver.cxx new file mode 100644 index 0000000..89ef304 --- /dev/null +++ b/common/view/driver.cxx @@ -0,0 +1,185 @@ +// file : common/view/driver.cxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +// Test views. +// + +#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)); + + // + // + { + country* ca (new country ("CA", "Canada")); + country* za (new country ("ZA", "South Africa")); + country* us (new country ("US", "United States")); + country* se (new country ("SE", "Sweden")); + + person p1 (1, "John", "Doe", 30, ca); + person p2 (2, "Jane", "Doe", 29, za); + person p3 (3, "Joe", "Dirt", 31, us); + person p4 (4, "Johansen", "Johansen", 32, se); + + transaction t (db->begin ()); + db->persist (ca); + db->persist (za); + db->persist (us); + db->persist (se); + + db->persist (p1); + db->persist (p2); + db->persist (p3); + db->persist (p4); + t.commit (); + } + + // view1 + // + { + typedef odb::query query; + typedef odb::result result; + + { + transaction t (db->begin ()); + + { + result r (db->query ()); + assert (size (r) == 4); + } + + { + result r (db->query ("ORDER BY age")); + assert (size (r) == 4); + } + + { + result r (db->query ("age < 31 ORDER BY age")); + result::iterator i (r.begin ()); + + assert (i != r.end ()); + assert (i->first == "Jane" && i->last == "Doe" && i->age == 29); + + assert (++i != r.end ()); + view1 v; + i.load (v); + assert (v.first == "John" && v.last == "Doe" && v.age == 30); + } + + t.commit (); + } + } + + // view2 + // + { + typedef odb::query query; + typedef odb::result result; + + { + transaction t (db->begin ()); + + { + result r (db->query ()); + result::iterator i (r.begin ()); + assert (i != r.end ()); + assert (i->count == 4); + } + + { + result r (db->query ("age < 31")); + result::iterator i (r.begin ()); + assert (i != r.end ()); + assert (i->count == 2); + } + + t.commit (); + } + } + + // view3 + // + { + typedef odb::query query; + typedef odb::result result; + + { + transaction t (db->begin ()); + + { + result r (db->query ()); + + size_t count (0); + for (result::iterator i (r.begin ()); i != r.end (); ++i) + { + if (i->last == "Doe") + assert (i->count == 2); + else if (i->last == "Dirt" || + i->last == "Johansen") + assert (i->count == 1); + else + assert (false); + + count++; + } + + assert (count == 3); + } + + t.commit (); + } + } + + // view4 + // + { + typedef odb::query query; + typedef odb::result result; + + { + transaction t (db->begin ()); + + { + result r (db->query ("age > 30 ORDER BY age")); + + result::iterator i (r.begin ()); + + assert (i != r.end ()); + assert (i->first == "Joe" && i->last == "Dirt" && + i->location == "United States"); + + assert (++i != r.end ()); + assert (i->first == "Johansen" && i->last == "Johansen" && + i->location == "Sweden"); + } + + t.commit (); + } + } + } + catch (const odb::exception& e) + { + cerr << e.what () << endl; + return 1; + } +} diff --git a/common/view/makefile b/common/view/makefile new file mode 100644 index 0000000..098baeb --- /dev/null +++ b/common/view/makefile @@ -0,0 +1,109 @@ +# file : common/view/makefile +# author : Boris Kolpackov +# copyright : Copyright (c) 2009-2011 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 --table-prefix common_view_ +$(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)) +$(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)) + +# 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/automake.make) + +$(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/view/test.hxx b/common/view/test.hxx new file mode 100644 index 0000000..9ceed60 --- /dev/null +++ b/common/view/test.hxx @@ -0,0 +1,114 @@ +// file : common/view/test.hxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef TEST_HXX +#define TEST_HXX + +#include +#include +#include // std::size_t +#include + +#include + +#pragma db object +struct country +{ + country (const std::string& c, std::string const& n) + : code (c), name (n) + { + } + + country () + { + } + + #pragma db id + std::string code; // ISO 2-letter country code. + + std::string name; +}; + +#pragma db object +struct person +{ + person (unsigned long i, + const std::string& fn, + const std::string& ln, + unsigned short a, + country* l) + : id (i), first_name (fn), last_name (ln), age (a), location (l) + { + } + + ~person () + { + delete location; + } + + person () + { + } + + #pragma db id + unsigned long id; + + #pragma db column ("first") + std::string first_name; + + #pragma db column ("last") + std::string last_name; + + unsigned short age; + + #pragma db not_null + country* location; +}; + +// General view with a custom query. +// +#pragma db view query("SELECT first, last, age FROM common_view_person") +struct view1 +{ + std::string first; + std::string last; + unsigned short age; +}; + +// Count view. +// +#pragma db view query("SELECT count(id) FROM common_view_person") +struct view2 +{ + std::size_t count; +}; + +// Aggregate view. +// +#pragma db view query("SELECT last, count(last) " \ + "FROM common_view_person " \ + "GROUP BY last") +struct view3 +{ + std::string last; + std::size_t count; + unsigned short age; +}; + +// JOIN view. +// +#pragma db view query("SELECT first, last, common_view_country.name " \ + "FROM common_view_person " \ + "LEFT JOIN common_view_country " \ + "ON common_view_person.location = " \ + "common_view_country.code") +struct view4 +{ + std::string first; + std::string last; + std::string location; +}; + +#endif // TEST_HXX diff --git a/common/view/test.std b/common/view/test.std new file mode 100644 index 0000000..e69de29 -- cgit v1.1