From 5a14aa4978cca949f3d07561ba35f8fd3af76ab2 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Tue, 1 Mar 2011 11:56:33 +0200 Subject: Add support for embedded database schemas New options: --schema-format, --default-schema. New example: schema/embedded. --- schema/embedded/README | 51 +++++++++++++++++++ schema/embedded/database.hxx | 46 +++++++++++++++++ schema/embedded/driver.cxx | 97 +++++++++++++++++++++++++++++++++++ schema/embedded/makefile | 117 +++++++++++++++++++++++++++++++++++++++++++ schema/embedded/person.hxx | 60 ++++++++++++++++++++++ 5 files changed, 371 insertions(+) create mode 100644 schema/embedded/README create mode 100644 schema/embedded/database.hxx create mode 100644 schema/embedded/driver.cxx create mode 100644 schema/embedded/makefile create mode 100644 schema/embedded/person.hxx (limited to 'schema') diff --git a/schema/embedded/README b/schema/embedded/README new file mode 100644 index 0000000..d353bd0 --- /dev/null +++ b/schema/embedded/README @@ -0,0 +1,51 @@ +This example shows how to generate and use a database schema that is embedded +into the application rather than stored as a separate SQL file. + +The example consists of the following files: + +person.hxx + Header file defining the 'person' persistent class. + +person-odb.hxx +person-odb.ixx +person-odb.cxx + These files contain the database support code as well as the embedded + database schema for the person.hxx header. They are generated by the ODB + compiler from person.hxx using the following command line: + + odb -d --generate-schema --schema-format embedded \ + --generate-query person.hxx + + Where stands for the database system we are using, for example, + 'mysql'. + + The --generate-schema option requests the generation of the database schema. + The --schema-format option is used to instruct the ODB compiler to embed the + schema into the generated C++ files. + +database.hxx + Contains the create_database() function which instantiates the concrete + database class corresponding to the database system we are using. + +driver.cxx + Driver for the example. It includes the person.hxx and person-odb.hxx + headers to gain access to the persistent classes and their database support + code. It also includes database.hxx for the create_database() function + declaration. + + In main() the driver first calls create_database() to obtain the database + instance. It then uses the ODB schema catalog to create the database + schema. During this step the generated code issues a number of SQL + statements that drop and create necessary database tables, etc. + + After the database schema is ready, the driver persists a number of 'person' + objects, performs a database query, and prints the information about the + returned objects. + +To run the driver, using MySQL as an example, we can execute the following +command: + +./driver --user odb_test --database odb_test + +Here we use 'odb_test' as the database login and also 'odb_test' as the +database name. diff --git a/schema/embedded/database.hxx b/schema/embedded/database.hxx new file mode 100644 index 0000000..7483f52 --- /dev/null +++ b/schema/embedded/database.hxx @@ -0,0 +1,46 @@ +// file : schema/embedded/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::core; + + if (argc > 1 && argv[1] == string ("--help")) + { + cerr << "Usage: " << argv[0] << " [options]" << endl + << "Options:" << endl; + +#if defined(DATABASE_MYSQL) + odb::mysql::database::print_usage (cerr); +#endif + + exit (0); + } + +#if defined(DATABASE_MYSQL) + return auto_ptr (new odb::mysql::database (argc, argv)); +#endif +} + +#endif // DATABASE_HXX diff --git a/schema/embedded/driver.cxx b/schema/embedded/driver.cxx new file mode 100644 index 0000000..aeba3ee --- /dev/null +++ b/schema/embedded/driver.cxx @@ -0,0 +1,97 @@ +// file : schema/embedded/driver.cxx +// author : Boris Kolpackov +// copyright : not copyrighted - public domain + +#include // std::auto_ptr +#include + +#include +#include +#include + +#include "database.hxx" // create_database + +#include "person.hxx" +#include "person-odb.hxx" + +using namespace std; +using namespace odb::core; + +int +main (int argc, char* argv[]) +{ + try + { + typedef odb::query query; + typedef odb::result result; + + auto_ptr db (create_database (argc, argv)); + + // Create the database schema. + // + { + transaction t (db->begin ()); + schema_catalog::create_schema (*db); + t.commit (); + } + + // The following alternative version only creates the schema if it + // hasn't already been created. To detect the existence of the schema + // this version tries to query the database for a person object. If + // the corresponding table does not exist, then an exceptions will be + // thrown in which case we proceed to creating the schema. + // + /* + { + transaction t (db->begin ()); + + try + { + db->query (false); + } + catch (const odb::exception& e) + { + schema_catalog::create_schema (*db); + } + + t.commit (); + } + */ + + // Create a few persistent person objects. + // + { + person john ("John", "Doe", 33); + person jane ("Jane", "Doe", 32); + person joe ("Joe", "Dirt", 30); + + transaction t (db->begin ()); + + db->persist (john); + db->persist (jane); + db->persist (joe); + + t.commit (); + } + + // Print those over 30. + // + { + transaction t (db->begin ()); + + result r (db->query (query::age > 30)); + + for (result::iterator i (r.begin ()); i != r.end (); ++i) + { + cout << i->first () << " " << i->last () << endl; + } + + t.commit (); + } + } + catch (const odb::exception& e) + { + cerr << e.what () << endl; + return 1; + } +} diff --git a/schema/embedded/makefile b/schema/embedded/makefile new file mode 100644 index 0000000..639505e --- /dev/null +++ b/schema/embedded/makefile @@ -0,0 +1,117 @@ +# file : schema/embedded/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 := 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) +gen := $(addprefix $(out_base)/,$(genf)) + +$(gen): $(odb) +$(gen): odb := $(odb) +$(gen) $(dist): export odb_options += --database $(db_id) --generate-query \ +--generate-schema --schema-format embedded + +$(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 := $(subst /,-,$(subst $(src_root)/,,$(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 := README $(call vc9projs,$(name)) \ +$(call vc10projs,$(name)) +$(dist): + $(call dist-data,$(sources) $(headers) README database.hxx) + $(call meta-automake,../../template/Makefile.am) + $(call meta-vc9projs,../../template/template,$(name)) + $(call meta-vc10projs,../../template/template,$(name)) + +# Test. +# +$(test): $(driver) + $(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/schema/embedded/person.hxx b/schema/embedded/person.hxx new file mode 100644 index 0000000..a3d44b9 --- /dev/null +++ b/schema/embedded/person.hxx @@ -0,0 +1,60 @@ +// file : schema/embedded/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_; + } + + void + age (unsigned short age) + { + age_ = age; + } + +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