From bde4f5c3bddb437c5238bcb6dd2311bc57600ef1 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Sun, 21 Aug 2011 16:27:34 +0200 Subject: Add odb::connection class This abstract class represents a connection to the database. One can use it to start a transaction or to execute a native statement out of a transaction. Before we had concrete connection classes in the database runtime libraries (e.g., odb::mysql::connection). Now these classes derive from odb::connection. --- odb/tracer/connection.cxx | 37 ++++++++++++++++++++++++++++++++++ odb/tracer/connection.hxx | 44 +++++++++++++++++++++++++++++++++++++++++ odb/tracer/database.cxx | 25 ++++++++++++++--------- odb/tracer/database.hxx | 17 +++++++++++----- odb/tracer/forward.hxx | 35 ++++++++++++++++++++++++++++++++ odb/tracer/makefile | 7 ++++++- odb/tracer/transaction-impl.cxx | 7 +++++-- odb/tracer/transaction-impl.hxx | 10 +++++----- 8 files changed, 160 insertions(+), 22 deletions(-) create mode 100644 odb/tracer/connection.cxx create mode 100644 odb/tracer/connection.hxx create mode 100644 odb/tracer/forward.hxx diff --git a/odb/tracer/connection.cxx b/odb/tracer/connection.cxx new file mode 100644 index 0000000..516e36a --- /dev/null +++ b/odb/tracer/connection.cxx @@ -0,0 +1,37 @@ +// file : odb/tracer/connection.cxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#include + +#include +#include + +namespace odb +{ + namespace tracer + { + connection:: + connection (database_type& db) + : odb::connection (db) + { + } + + unsigned long long connection:: + execute (const char*, std::size_t) + { + return 0; + } + + transaction_impl* connection:: + begin () + { + if (odb::transaction::has_current ()) + throw already_in_transaction (); + + return new transaction_impl ( + connection_ptr (inc_ref (this))); + } + } +} diff --git a/odb/tracer/connection.hxx b/odb/tracer/connection.hxx new file mode 100644 index 0000000..743bf92 --- /dev/null +++ b/odb/tracer/connection.hxx @@ -0,0 +1,44 @@ +// file : odb/tracer/connection.hxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef ODB_TRACER_CONNECTION_HXX +#define ODB_TRACER_CONNECTION_HXX + +#include + +#include +#include + +#include +#include + +namespace odb +{ + namespace tracer + { + class database; + + class connection; + typedef details::shared_ptr connection_ptr; + + class LIBODB_TRACER_EXPORT connection: public odb::connection + { + public: + typedef tracer::database database_type; + + connection (database_type&); + + virtual unsigned long long + execute (const char* statement, std::size_t length); + + virtual transaction_impl* + begin (); + }; + } +} + +#include + +#endif // ODB_TRACER_CONNECTION_HXX diff --git a/odb/tracer/database.cxx b/odb/tracer/database.cxx index 1359a26..90cfdfd 100644 --- a/odb/tracer/database.cxx +++ b/odb/tracer/database.cxx @@ -3,8 +3,8 @@ // copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC // license : GNU GPL v2; see accompanying LICENSE file -#include #include +#include namespace odb { @@ -15,19 +15,26 @@ namespace odb { } - unsigned long long database:: - execute (const char*, std::size_t) + transaction_impl* database:: + begin () { - return 0; + return connection ()->begin (); } - transaction_impl* database:: - begin () + inline connection_ptr database:: + connection () { - if (odb::transaction::has_current ()) - throw already_in_transaction (); + // Go through the virtual connection_() function instead of + // directly to allow overriding. + // + return connection_ptr ( + static_cast (connection_ ())); + } - return new transaction_impl (*this); + odb::connection* database:: + connection_ () + { + return new (details::shared) connection_type (*this); } } } diff --git a/odb/tracer/database.hxx b/odb/tracer/database.hxx index 7210681..2468583 100644 --- a/odb/tracer/database.hxx +++ b/odb/tracer/database.hxx @@ -9,8 +9,9 @@ #include #include -#include +#include +#include #include namespace odb @@ -20,14 +21,20 @@ namespace odb class LIBODB_TRACER_EXPORT database: public odb::database { public: + typedef tracer::connection connection_type; + virtual ~database (); - virtual unsigned long long - execute (const char* statement, std::size_t length); - - virtual transaction_impl* + transaction_impl* begin (); + + connection_ptr + connection (); + + protected: + virtual odb::connection* + connection_ (); }; } } diff --git a/odb/tracer/forward.hxx b/odb/tracer/forward.hxx new file mode 100644 index 0000000..61f0eb7 --- /dev/null +++ b/odb/tracer/forward.hxx @@ -0,0 +1,35 @@ +// file : odb/tracer/forward.hxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2005-2011 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef ODB_TRACER_FORWARD_HXX +#define ODB_TRACER_FORWARD_HXX + +#include + +#include + +namespace odb +{ + namespace tracer + { + class database; + class connection; + typedef details::shared_ptr connection_ptr; + class transaction; + } + + namespace details + { + template <> + struct counter_type + { + typedef shared_base counter; + }; + } +} + +#include + +#endif // ODB_TRACER_FORWARD_HXX diff --git a/odb/tracer/makefile b/odb/tracer/makefile index d7b4611..335e681 100644 --- a/odb/tracer/makefile +++ b/odb/tracer/makefile @@ -5,7 +5,12 @@ include $(dir $(lastword $(MAKEFILE_LIST)))../../build/bootstrap.make -cxx := exceptions.cxx database.cxx transaction.cxx transaction-impl.cxx +cxx := \ +exceptions.cxx \ +database.cxx \ +connection.cxx \ +transaction.cxx \ +transaction-impl.cxx cxx_tun := $(cxx) cxx_obj := $(addprefix $(out_base)/,$(cxx_tun:.cxx=.o)) diff --git a/odb/tracer/transaction-impl.cxx b/odb/tracer/transaction-impl.cxx index f8b90af..2069568 100644 --- a/odb/tracer/transaction-impl.cxx +++ b/odb/tracer/transaction-impl.cxx @@ -6,6 +6,7 @@ #include #include +#include #include using std::cout; @@ -16,8 +17,10 @@ namespace odb namespace tracer { transaction_impl:: - transaction_impl (database_type& db) - : odb::transaction_impl (db), finalized_ (false) + transaction_impl (connection_ptr c) + : odb::transaction_impl (c->database (), *c), + finalized_ (false), + connection_ (c) { cout << "begin transaction" << endl; } diff --git a/odb/tracer/transaction-impl.hxx b/odb/tracer/transaction-impl.hxx index a4d2b40..f926183 100644 --- a/odb/tracer/transaction-impl.hxx +++ b/odb/tracer/transaction-impl.hxx @@ -9,25 +9,24 @@ #include #include +#include +#include #include namespace odb { namespace tracer { - class database; - class transaction; - class LIBODB_TRACER_EXPORT transaction_impl: public odb::transaction_impl { protected: - friend class database; + friend class connection; friend class transaction; typedef tracer::database database_type; - transaction_impl (database_type&); + transaction_impl (connection_ptr); virtual ~transaction_impl (); @@ -40,6 +39,7 @@ namespace odb private: bool finalized_; + connection_ptr connection_; }; } } -- cgit v1.1