From 01c9e7c9cc51ad1795de6c556d196d46b931402b Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Thu, 23 Sep 2010 11:04:36 +0200 Subject: Add query example --- makefile | 2 +- query/database.hxx | 46 ++++++++++++++ query/driver.cxx | 183 +++++++++++++++++++++++++++++++++++++++++++++++++++++ query/makefile | 117 ++++++++++++++++++++++++++++++++++ query/person.hxx | 61 ++++++++++++++++++ 5 files changed, 408 insertions(+), 1 deletion(-) create mode 100644 query/database.hxx create mode 100644 query/driver.cxx create mode 100644 query/makefile create mode 100644 query/person.hxx diff --git a/makefile b/makefile index b252863..c226005 100644 --- a/makefile +++ b/makefile @@ -5,7 +5,7 @@ include $(dir $(lastword $(MAKEFILE_LIST)))build/bootstrap.make -dirs := hello mapping template +dirs := hello query mapping template dist_dirs := $(filter-out template,$(dirs)) default := $(out_base)/ diff --git a/query/database.hxx b/query/database.hxx new file mode 100644 index 0000000..aad361a --- /dev/null +++ b/query/database.hxx @@ -0,0 +1,46 @@ +// file : query/database.hxx +// author : Boris Kolpackov +// copyright : not copyrighted - public domain + +// +// Create concrete database instance based on the DATABASE_* macros. +// + +#ifndef DATABASE_HXX +#define DATABASE_HXX + +#include +#include // std::auto_ptr +#include // std::exit +#include + +#include + +#if defined(DATABASE_MYSQL) +# include +#endif + +inline std::auto_ptr +create_database (int& argc, char* argv[]) +{ + using namespace std; + using namespace odb; + + if (argc > 1 && argv[1] == string ("--help")) + { + cerr << "Usage: " << argv[0] << " [options]" << endl + << "Options:" << endl; + +#if defined(DATABASE_MYSQL) + mysql::database::print_usage (cerr); +#endif + + exit (0); + } + +#if defined(DATABASE_MYSQL) + return auto_ptr (new mysql::database (argc, argv)); +#endif +} + +#endif // DATABASE_HXX diff --git a/query/driver.cxx b/query/driver.cxx new file mode 100644 index 0000000..15f6129 --- /dev/null +++ b/query/driver.cxx @@ -0,0 +1,183 @@ +// file : query/driver.cxx +// author : Boris Kolpackov +// copyright : not copyrighted - public domain + +#include +#include // std::auto_ptr +#include + +#include +#include + +#include "database.hxx" // create_database + +#include "person.hxx" +#include "person-odb.hxx" + +using namespace std; + +using odb::database; +using odb::transaction; + +typedef odb::query query; +typedef odb::result result; + +static void +print (result& r) +{ + for (result::iterator i (r.begin ()); i != r.end (); ++i) + { + cout << i->first () << " " << i->last () << " " << i->age () << endl; + } + + cout << endl; +} + +int +main (int argc, char* argv[]) +{ + try + { + auto_ptr db (create_database (argc, argv)); + + { + typedef vector people; + + people p; + + p.push_back (person ("John", "Doe", 21)); + p.push_back (person ("John", "Smith", 22)); + p.push_back (person ("Jack", "Johnson", 31)); + p.push_back (person ("John", "Jackson", 32)); + p.push_back (person ("Jane", "Doe", 23)); + p.push_back (person ("Jane", "Smith", 24)); + + transaction t (db->begin_transaction ()); + + for (people::iterator i (p.begin ()); i != p.end (); ++i) + db->persist (*i); + + t.commit (); + } + + // A simple query and result handling. + // + { + transaction t (db->begin_transaction ()); + + result r (db->query (query::age < 30)); + + for (result::iterator i (r.begin ()); i != r.end (); ++i) + { + cout << i->first () << " " << i->last () << " " << i->age () << endl; + } + + // Alternatively we can get a dynamically-allocated object. + // + /* + for (result::iterator i (r.begin ()); i != r.end (); ++i) + { + auto_ptr p (i.load ()); + cout << p->first () << " " << p->last () << " " << p->age () << endl; + } + */ + + // Or we can load the state into an existing object. + // + /* + person p ("", "", 0); + + for (result::iterator i (r.begin ()); i != r.end (); ++i) + { + i.load (p); + cout << p.first () << " " << p.last () << " " << p.age () << endl; + } + */ + + cout << endl; + + t.commit (); + } + + // Query that shows how to combine expressions with &&, ||, and ! + // as well as use paranthesis to control evaluation order. + // + { + transaction t (db->begin_transaction ()); + + result r ( + db->query ( + (query::first == "John" || query::first == "Jane") && + !(query::last != "Doe" || query::age > 30))); + + print (r); + t.commit (); + } + + // Query that shows how to use by-reference parameter binding. + // + { + transaction t (db->begin_transaction ()); + + unsigned short lower, upper; + + query q (query::age >= query::_ref (lower) && + query::age < query::_ref (upper)); + + for (unsigned short band (0); band < 10; ++band) + { + lower = band * 10; + upper = lower + 10; + + result r (db->query (q)); + + if (!r.empty ()) + { + cout << lower << '-' << (upper - 1) << ':' << endl; + print (r); + } + } + + t.commit (); + } + + // Query that shows how to use the in() function. + // + { + transaction t (db->begin_transaction ()); + + result r ( + db->query ( + query::last.in ("Smith", "Johnson", "Jockson"))); + + print (r); + + t.commit (); + } + + // The same query but using the in_range() function. + // + { + vector names; + + names.push_back ("Smith"); + names.push_back ("Johnson"); + names.push_back ("Jockson"); + + transaction t (db->begin_transaction ()); + + result r ( + db->query ( + query::last.in_range (names.begin (), names.end ()))); + + print (r); + + t.commit (); + } + } + catch (const odb::exception& e) + { + cerr << e.what () << endl; + return 1; + } +} diff --git a/query/makefile b/query/makefile new file mode 100644 index 0000000..1445a00 --- /dev/null +++ b/query/makefile @@ -0,0 +1,117 @@ +# file : query/makefile +# author : Boris Kolpackov +# 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 := person.hxx +cxx_obj := $(addprefix $(out_base)/,$(cxx_tun:.cxx=.o) $(odb_hdr:.hxx=-odb.o)) +cxx_od := $(cxx_obj:.o=.o.d) + +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) + +$(call import,\ + $(scf_root)/import/libodb/stub.make,\ + l: odb.l,cpp-options: odb.l.cpp-options) + +ifdef db_id +$(call import,\ + $(scf_root)/import/libodb-$(db_id)/stub.make,\ + l: odb_db.l,cpp-options: odb_db.l.cpp-options) +endif + +ifeq ($(odb_db.l.cpp-options),) +odb_db.l.cpp-options := $(out_base)/.unbuildable +endif + +# Build. +# +$(driver): $(cxx_obj) $(odb_db.l) $(odb.l) +$(cxx_obj) $(cxx_od): cpp_options := -I$(out_base) +$(cxx_obj) $(cxx_od): $(odb.l.cpp-options) $(odb_db.l.cpp-options) + +ifeq ($(db_id),mysql) +$(cxx_obj) $(cxx_od): cpp_options += -DDATABASE_MYSQL +endif + +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 +$(gen): cpp_options := -I$(out_base) +$(gen): $(odb.l.cpp-options) + +$(call include-dep,$(cxx_od),$(cxx_obj),$(gen)) + +# Alias for default target. +# +$(out_base)/: $(driver) + +# Dist +# +name := $(notdir $(src_base)) + +$(dist): db_id := @database@ +$(dist): sources := $(cxx_tun) +$(dist): headers := $(odb_hdr) +$(dist): export name := $(name) +$(dist): export odb_header_stem := $(basename $(odb_hdr)) +$(dist): export extra_dist := $(call vc9projs,$(name)) $(call vc10projs,$(name)) +$(dist): + $(call dist-data,$(sources) $(headers) database.hxx) + $(call meta-automake,../template/Makefile.am) + $(call meta-vc9projs,../template/template,$(name)) + $(call meta-vc10projs,../template/template,$(name)) + +# Test. +# +$(test): schema := $(src_base)/$(basename $(odb_hdr)).sql +$(test): $(driver) + $(call message,sql $$1,$(dcf_root)/db-driver $$1,$(schema)) + $(call message,test $<,$< --options-file $(dcf_root)/db.options) + +# 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,$(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) + diff --git a/query/person.hxx b/query/person.hxx new file mode 100644 index 0000000..ad2adc3 --- /dev/null +++ b/query/person.hxx @@ -0,0 +1,61 @@ +// file : query/person.hxx +// author : Boris Kolpackov +// copyright : not copyrighted - public domain + +#ifndef PERSON_HXX +#define PERSON_HXX + +#include + +#include + +#pragma db object +class person +{ +public: + person (const std::string& first, + const std::string& last, + unsigned short age) + : first_ (first), last_ (last), age_ (age) + { + } + + const std::string& + first () const + { + return first_; + } + + const std::string& + last () const + { + return last_; + } + + unsigned short + age () const + { + return age_; + } + +public: + unsigned long + id () const + { + return id_; + } + +private: + friend class odb::access; + + person () {} + + #pragma db id auto + unsigned long id_; + + std::string first_; + std::string last_; + unsigned short age_; +}; + +#endif // PERSON_HXX -- cgit v1.1