diff options
-rw-r--r-- | makefile | 34 | ||||
-rw-r--r-- | odb/mysql/connection-factory.cxx | 37 | ||||
-rw-r--r-- | odb/mysql/connection-factory.hxx | 54 | ||||
-rw-r--r-- | odb/mysql/connection.cxx | 44 | ||||
-rw-r--r-- | odb/mysql/connection.hxx | 44 | ||||
-rw-r--r-- | odb/mysql/database.cxx | 174 | ||||
-rw-r--r-- | odb/mysql/database.hxx | 160 | ||||
-rw-r--r-- | odb/mysql/database.ixx | 16 | ||||
-rw-r--r-- | odb/mysql/exceptions.cxx | 38 | ||||
-rw-r--r-- | odb/mysql/exceptions.hxx | 54 | ||||
-rw-r--r-- | odb/mysql/forward.hxx | 20 | ||||
-rw-r--r-- | odb/mysql/makefile | 68 | ||||
-rw-r--r-- | odb/mysql/transaction-impl.cxx | 60 | ||||
-rw-r--r-- | odb/mysql/transaction-impl.hxx | 50 | ||||
-rw-r--r-- | odb/mysql/transaction-impl.ixx | 16 | ||||
-rw-r--r-- | odb/mysql/transaction.cxx | 26 | ||||
-rw-r--r-- | odb/mysql/transaction.hxx | 53 | ||||
-rw-r--r-- | odb/mysql/transaction.ixx | 41 |
18 files changed, 989 insertions, 0 deletions
diff --git a/makefile b/makefile new file mode 100644 index 0000000..ea14d69 --- /dev/null +++ b/makefile @@ -0,0 +1,34 @@ +# file : makefile +# author : Boris Kolpackov <boris@codesynthesis.com> +# 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 + +default := $(out_base)/ +test := $(out_base)/.test +install := $(out_base)/.install +clean := $(out_base)/.clean + +# Build. +# +$(default): $(out_base)/odb/mysql/ #$(out_base)/tests/ + +# Test. +# +$(test): $(out_base)/tests/.test + +# Install. +# +$(install): $(out_base)/odb/mysql/.install + $(call install-data,$(src_base)/LICENSE,$(install_doc_dir)/libodb-mysql/LICENSE) + $(call install-data,$(src_base)/README,$(install_doc_dir)/libodb-mysql/README) + +# Clean. +# +$(clean): $(out_base)/odb/mysql/.clean #$(out_base)/tests/.clean + +$(call include,$(bld_root)/install.make) + +$(call import,$(src_base)/odb/mysql/makefile) +#$(call import,$(src_base)/tests/makefile) diff --git a/odb/mysql/connection-factory.cxx b/odb/mysql/connection-factory.cxx new file mode 100644 index 0000000..64fdc57 --- /dev/null +++ b/odb/mysql/connection-factory.cxx @@ -0,0 +1,37 @@ +// file : odb/mysql/connection-factory.cxx +// author : Boris Kolpackov <boris@codesynthesis.com> +// copyright : Copyright (c) 2009-2010 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#include <odb/mysql/connection-factory.hxx> + +namespace odb +{ + namespace mysql + { + // + // connection_factory + // + + connection_factory:: + ~connection_factory () + { + } + + // + // new_connection_factory + // + + shared_ptr<connection> new_connection_factory:: + connect () + { + return shared_ptr<connection> (new (shared) connection (*db_)); + } + + void new_connection_factory:: + database (database_type& db) + { + db_ = &db; + } + } +} diff --git a/odb/mysql/connection-factory.hxx b/odb/mysql/connection-factory.hxx new file mode 100644 index 0000000..e316f18 --- /dev/null +++ b/odb/mysql/connection-factory.hxx @@ -0,0 +1,54 @@ +// file : odb/mysql/connection-factory.hxx +// author : Boris Kolpackov <boris@codesynthesis.com> +// copyright : Copyright (c) 2009-2010 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef ODB_MYSQL_CONNECTION_FACTORY_HXX +#define ODB_MYSQL_CONNECTION_FACTORY_HXX + +#include <odb/shared-ptr.hxx> + +#include <odb/mysql/forward.hxx> +#include <odb/mysql/connection.hxx> + +namespace odb +{ + namespace mysql + { + class connection_factory + { + public: + virtual shared_ptr<connection> + connect () = 0; + + public: + typedef mysql::database database_type; + + virtual void + database (database_type&) = 0; + + virtual + ~connection_factory (); + }; + + class new_connection_factory: public connection_factory + { + public: + new_connection_factory () + : db_ (0) + { + } + + virtual shared_ptr<connection> + connect (); + + virtual void + database (database_type&); + + private: + database_type* db_; + }; + } +} + +#endif // ODB_MYSQL_CONNECTION_FACTORY_HXX diff --git a/odb/mysql/connection.cxx b/odb/mysql/connection.cxx new file mode 100644 index 0000000..9edb814 --- /dev/null +++ b/odb/mysql/connection.cxx @@ -0,0 +1,44 @@ +// file : odb/mysql/connection.cxx +// author : Boris Kolpackov <boris@codesynthesis.com> +// copyright : Copyright (c) 2005-2010 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#include <new> // std::bad_alloc + +#include <odb/mysql/database.hxx> +#include <odb/mysql/connection.hxx> +#include <odb/mysql/exceptions.hxx> + +namespace odb +{ + namespace mysql + { + connection:: + connection (database& db) + : handle_ (&mysql_) + { + if (mysql_init (handle_) == 0) + throw std::bad_alloc (); + + if (mysql_real_connect (handle_, + db.host (), + db.user (), + db.passwd (), + db.db (), + db.port (), + db.socket (), + db.client_flags ()) == 0) + { + database_exception e (handle_); + mysql_close (handle_); + throw e; + } + } + + connection:: + ~connection () + { + mysql_close (handle_); + } + } +} diff --git a/odb/mysql/connection.hxx b/odb/mysql/connection.hxx new file mode 100644 index 0000000..e0f44a3 --- /dev/null +++ b/odb/mysql/connection.hxx @@ -0,0 +1,44 @@ +// file : odb/mysql/connection.hxx +// author : Boris Kolpackov <boris@codesynthesis.com> +// copyright : Copyright (c) 2005-2010 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef ODB_MYSQL_CONNECTION_HXX +#define ODB_MYSQL_CONNECTION_HXX + +#include <mysql/mysql.h> + +#include <odb/forward.hxx> +#include <odb/shared-ptr.hxx> + +namespace odb +{ + namespace mysql + { + class connection: public shared_base + { + public: + connection (database&); + + virtual + ~connection (); + + public: + MYSQL* + handle () + { + return handle_; + } + + private: + connection (const connection&); + connection& operator= (const connection&); + + private: + MYSQL mysql_; + MYSQL* handle_; + }; + } +} + +#endif // ODB_MYSQL_CONNECTION_HXX diff --git a/odb/mysql/database.cxx b/odb/mysql/database.cxx new file mode 100644 index 0000000..7902bcf --- /dev/null +++ b/odb/mysql/database.cxx @@ -0,0 +1,174 @@ +// file : odb/mysql/database.cxx +// author : Boris Kolpackov <boris@codesynthesis.com> +// copyright : Copyright (c) 2009-2010 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#include <odb/session.hxx> +#include <odb/transaction.hxx> + +#include <odb/mysql/database.hxx> +#include <odb/mysql/connection-factory.hxx> + +using namespace std; + +namespace odb +{ + namespace mysql + { + database:: + ~database () + { + } + + database:: + database (const char* user, + const char* passwd, + const char* db, + const char* host, + unsigned int port, + const char* socket, + unsigned long client_flags, + auto_ptr<connection_factory> factory) + : user_ (user ? user : ""), + passwd_str_ (passwd ? passwd : ""), + passwd_ (passwd ? passwd_str_.c_str () : 0), + db_ (db ? db : ""), + host_ (host ? host : ""), + port_ (port), + socket_str_ (socket ? socket : ""), + socket_ (socket ? socket_str_.c_str () : 0), + client_flags_ (client_flags), + factory_ (factory) + { + if (factory_.get () == 0) + factory_.reset (new new_connection_factory ()); + + factory_->database (*this); + } + + database:: + database (const string& user, + const string& passwd, + const string& db, + const string& host, + unsigned int port, + const string* socket, + unsigned long client_flags, + auto_ptr<connection_factory> factory) + : user_ (user), + passwd_str_ (passwd), + passwd_ (passwd_str_.c_str ()), + db_ (db), + host_ (host), + port_ (port), + socket_str_ (socket ? *socket : ""), + socket_ (socket ? socket_str_.c_str () : 0), + client_flags_ (client_flags), + factory_ (factory) + { + if (factory_.get () == 0) + factory_.reset (new new_connection_factory ()); + + factory_->database (*this); + } + + database:: + database (const string& user, + const string* passwd, + const string& db, + const string& host, + unsigned int port, + const string* socket, + unsigned long client_flags, + auto_ptr<connection_factory> factory) + : user_ (user), + passwd_str_ (passwd ? *passwd : ""), + passwd_ (passwd ? passwd_str_.c_str () : 0), + db_ (db), + host_ (host), + port_ (port), + socket_str_ (socket ? *socket : ""), + socket_ (socket ? socket_str_.c_str () : 0), + client_flags_ (client_flags), + factory_ (factory) + { + if (factory_.get () == 0) + factory_.reset (new new_connection_factory ()); + + factory_->database (*this); + } + + database:: + database (const string& user, + const string& passwd, + const string& db, + const string& host, + unsigned int port, + const string& socket, + unsigned long client_flags, + auto_ptr<connection_factory> factory) + : user_ (user), + passwd_str_ (passwd), + passwd_ (passwd_str_.c_str ()), + db_ (db), + host_ (host), + port_ (port), + socket_str_ (socket), + socket_ (socket_str_.c_str ()), + client_flags_ (client_flags), + factory_ (factory) + { + if (factory_.get () == 0) + factory_.reset (new new_connection_factory ()); + + factory_->database (*this); + } + + database:: + database (const string& user, + const string* passwd, + const string& db, + const string& host, + unsigned int port, + const string& socket, + unsigned long client_flags, + auto_ptr<connection_factory> factory) + : user_ (user), + passwd_str_ (passwd ? *passwd : ""), + passwd_ (passwd ? passwd_str_.c_str () : 0), + db_ (db), + host_ (host), + port_ (port), + socket_str_ (socket), + socket_ (socket_str_.c_str ()), + client_flags_ (client_flags), + factory_ (factory) + { + if (factory_.get () == 0) + factory_.reset (new new_connection_factory ()); + + factory_->database (*this); + } + + transaction_impl* database:: + begin_transaction () + { + if (odb::transaction::has_current ()) + throw already_in_transaction (); + + if (session::has_current ()) + return new transaction_impl (*this, session::current ()); + else + return new transaction_impl (*this); + } + + transaction_impl* database:: + begin_transaction (session& s) + { + if (odb::transaction::has_current ()) + throw already_in_transaction (); + + return new transaction_impl (*this, s); + } + } +} diff --git a/odb/mysql/database.hxx b/odb/mysql/database.hxx new file mode 100644 index 0000000..be939a7 --- /dev/null +++ b/odb/mysql/database.hxx @@ -0,0 +1,160 @@ +// file : odb/mysql/database.hxx +// author : Boris Kolpackov <boris@codesynthesis.com> +// copyright : Copyright (c) 2009-2010 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef ODB_MYSQL_DATABASE_HXX +#define ODB_MYSQL_DATABASE_HXX + +#include <string> +#include <memory> // std::auto_ptr + +#include <mysql/mysql.h> + +#include <odb/shared-ptr.hxx> +#include <odb/database.hxx> + +#include <odb/mysql/forward.hxx> +#include <odb/mysql/connection.hxx> +#include <odb/mysql/connection-factory.hxx> +#include <odb/mysql/transaction-impl.hxx> + +namespace odb +{ + namespace mysql + { + class database: public odb::database + { + public: + typedef mysql::connection connection_type; + + public: + // In MySQL NULL and empty string are treated as the same value + // for all the arguments except passwd and socket. + // + database (const char* user, + const char* passwd, + const char* db, + const char* host = 0, + unsigned int port = 0, + const char* socket = 0, + unsigned long client_flags = 0, + std::auto_ptr<connection_factory> = + std::auto_ptr<connection_factory> (0)); + + database (const std::string& user, + const std::string& passwd, + const std::string& db, + const std::string& host = "", + unsigned int port = 0, + const std::string* socket = 0, + unsigned long client_flags = 0, + std::auto_ptr<connection_factory> = + std::auto_ptr<connection_factory> (0)); + + database (const std::string& user, + const std::string* passwd, + const std::string& db, + const std::string& host = "", + unsigned int port = 0, + const std::string* socket = 0, + unsigned long client_flags = 0, + std::auto_ptr<connection_factory> = + std::auto_ptr<connection_factory> (0)); + + database (const std::string& user, + const std::string& passwd, + const std::string& db, + const std::string& host, + unsigned int port, + const std::string& socket, + unsigned long client_flags = 0, + std::auto_ptr<connection_factory> = + std::auto_ptr<connection_factory> (0)); + + database (const std::string& user, + const std::string* passwd, + const std::string& db, + const std::string& host, + unsigned int port, + const std::string& socket, + unsigned long client_flags = 0, + std::auto_ptr<connection_factory> = + std::auto_ptr<connection_factory> (0)); + + public: + const char* + user () const + { + return user_.c_str (); + } + + const char* + passwd () const + { + return passwd_; + } + + const char* + db () const + { + return db_.c_str (); + } + + const char* + host () const + { + return host_.c_str (); + } + + unsigned int + port () const + { + return port_; + } + + const char* + socket () const + { + return socket_; + } + + unsigned long + client_flags () const + { + return client_flags_; + } + + public: + virtual transaction_impl* + begin_transaction (); + + virtual transaction_impl* + begin_transaction (session&); + + public: + shared_ptr<connection_type> + connection (); + + public: + virtual + ~database (); + + private: + const std::string user_; + const std::string passwd_str_; + const char* passwd_; + const std::string db_; + const std::string host_; + unsigned int port_; + const std::string socket_str_; + const char* socket_; + unsigned long client_flags_; + std::auto_ptr<connection_factory> factory_; + }; + } +} + +#include <odb/mysql/database.ixx> + +#endif // ODB_MYSQL_DATABASE_HXX diff --git a/odb/mysql/database.ixx b/odb/mysql/database.ixx new file mode 100644 index 0000000..8d927b5 --- /dev/null +++ b/odb/mysql/database.ixx @@ -0,0 +1,16 @@ +// file : odb/mysql/database.ixx +// author : Boris Kolpackov <boris@codesynthesis.com> +// copyright : Copyright (c) 2009-2010 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +namespace odb +{ + namespace mysql + { + inline shared_ptr<database::connection_type> database:: + connection () + { + return factory_->connect (); + } + } +} diff --git a/odb/mysql/exceptions.cxx b/odb/mysql/exceptions.cxx new file mode 100644 index 0000000..4ee80fb --- /dev/null +++ b/odb/mysql/exceptions.cxx @@ -0,0 +1,38 @@ +// file : odb/mysql/exceptions.cxx +// author : Boris Kolpackov <boris@codesynthesis.com> +// copyright : Copyright (c) 2005-2010 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#include <sstream> + +#include <odb/mysql/exceptions.hxx> + +using namespace std; + +namespace odb +{ + namespace mysql + { + database_exception:: + ~database_exception () throw () + { + } + + database_exception:: + database_exception (MYSQL* h) + : error_ (mysql_errno (h)), + sqlstate_ (mysql_sqlstate (h)), + message_ (mysql_error (h)) + { + ostringstream ostr; + ostr << error_ << " (" << sqlstate_ << "): " << message_; + what_ = ostr.str (); + } + + char const* database_exception:: + what () const throw () + { + return what_.c_str (); + } + } +} diff --git a/odb/mysql/exceptions.hxx b/odb/mysql/exceptions.hxx new file mode 100644 index 0000000..a4e729b --- /dev/null +++ b/odb/mysql/exceptions.hxx @@ -0,0 +1,54 @@ +// file : odb/mysql/exceptions.hxx +// author : Boris Kolpackov <boris@codesynthesis.com> +// copyright : Copyright (c) 2005-2010 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef ODB_MYSQL_EXCEPTIONS_HXX +#define ODB_MYSQL_EXCEPTIONS_HXX + +#include <string> + +#include <mysql/mysql.h> + +#include <odb/exceptions.hxx> + +namespace odb +{ + namespace mysql + { + struct database_exception: odb::database_exception + { + database_exception (MYSQL*); + ~database_exception () throw (); + + unsigned int + error () const + { + return error_; + } + + const std::string& + sqlstate () const + { + return sqlstate_; + } + + const std::string& + message () const + { + return message_; + } + + virtual char const* + what () const throw (); + + private: + unsigned int error_; + std::string sqlstate_; + std::string message_; + std::string what_; + }; + } +} + +#endif // ODB_MYSQL_EXCEPTIONS_HXX diff --git a/odb/mysql/forward.hxx b/odb/mysql/forward.hxx new file mode 100644 index 0000000..41baa1c --- /dev/null +++ b/odb/mysql/forward.hxx @@ -0,0 +1,20 @@ +// file : odb/mysql/forward.hxx +// author : Boris Kolpackov <boris@codesynthesis.com> +// copyright : Copyright (c) 2005-2010 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef ODB_MYSQL_FORWARD_HXX +#define ODB_MYSQL_FORWARD_HXX + +namespace odb +{ + namespace mysql + { + class database; + class connection; + class connection_factory; + class transaction; + } +} + +#endif // ODB_MYSQL_FORWARD_HXX diff --git a/odb/mysql/makefile b/odb/mysql/makefile new file mode 100644 index 0000000..ce2204e --- /dev/null +++ b/odb/mysql/makefile @@ -0,0 +1,68 @@ +# file : odb/mysql/makefile +# author : Boris Kolpackov <boris@codesynthesis.com> +# 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 := \ +connection.cxx \ +connection-factory.cxx \ +database.cxx \ +exceptions.cxx \ +transaction.cxx \ +transaction-impl.cxx + +cxx_obj := $(addprefix $(out_base)/,$(cxx_tun:.cxx=.o)) +cxx_od := $(cxx_obj:.o=.o.d) + +odb_mysql.l := $(out_base)/odb-mysql.l +odb_mysql.l.cpp-options := $(out_base)/odb-mysql.l.cpp-options + +default := $(out_base)/ +install := $(out_base)/.install +clean := $(out_base)/.clean + +# Import. +# +$(call import,\ + $(scf_root)/import/libodb/stub.make,\ + l: odb.l,cpp-options: odb.l.cpp-options) + +$(call import,\ + $(scf_root)/import/libmysqlclient/stub.make,\ + l: mysql.l,cpp-options: mysql.l.cpp-options) + +# Build. +# +$(odb_mysql.l): $(cxx_obj) $(odb.l) $(mysql.l) +$(odb_mysql.l.cpp-options): value := -I$(src_root) +$(odb_mysql.l.cpp-options): $(odb.l.cpp-options) $(mysql.l.cpp-options) + +$(cxx_obj) $(cxx_od): $(odb_mysql.l.cpp-options) + +$(call include-dep,$(cxx_od)) + +# Convenience alias for default target. +# +$(out_base)/: $(odb_mysql.l) + +# Install. +# +$(install): $(odb_mysql.l) + $(call install-lib,$<,$(install_lib_dir)/$(ld_lib_prefix)odb-mysql$(ld_lib_suffix)) + $(call install-dir,$(src_base),$(install_inc_dir)/odb/mysql,\ +'(' -name '*.hxx' -o -name '*.ixx' -o -name '*.txx' ')') + +# Clean. +# +$(clean): $(odb_mysql.l).o.clean \ + $(odb_mysql.l.cpp-options).clean \ + $(addsuffix .cxx.clean,$(cxx_obj)) \ + $(addsuffix .cxx.clean,$(cxx_od)) + +# How to. +# +$(call include,$(bld_root)/cxx/o-l.make) +$(call include,$(bld_root)/cxx/cxx-o.make) +$(call include,$(bld_root)/cxx/cxx-d.make) diff --git a/odb/mysql/transaction-impl.cxx b/odb/mysql/transaction-impl.cxx new file mode 100644 index 0000000..fde19de --- /dev/null +++ b/odb/mysql/transaction-impl.cxx @@ -0,0 +1,60 @@ +// file : odb/mysql/transaction-impl.cxx +// author : Boris Kolpackov <boris@codesynthesis.com> +// copyright : Copyright (c) 2009-2010 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#include <mysql/mysql.h> + +#include <odb/mysql/database.hxx> +#include <odb/mysql/connection.hxx> +#include <odb/mysql/exceptions.hxx> +#include <odb/mysql/transaction-impl.hxx> + +namespace odb +{ + namespace mysql + { + transaction_impl:: + transaction_impl (database_type& db) + : odb::transaction_impl (db), connection_ (db.connection ()) + { + MYSQL* h (connection_->handle ()); + + if (mysql_real_query (h, "begin", 5) != 0) + throw database_exception (h); + } + + transaction_impl:: + transaction_impl (database_type& db, session_type& s) + : odb::transaction_impl (db, s), connection_ (db.connection ()) + { + MYSQL* h (connection_->handle ()); + + if (mysql_real_query (h, "begin", 5) != 0) + throw database_exception (h); + } + + transaction_impl:: + ~transaction_impl () + { + } + + void transaction_impl:: + commit () + { + MYSQL* h (connection_->handle ()); + + if (mysql_real_query (h, "commit", 6) != 0) + throw database_exception (h); + } + + void transaction_impl:: + rollback () + { + MYSQL* h (connection_->handle ()); + + if (mysql_real_query (h, "rollback", 8) != 0) + throw database_exception (h); + } + } +} diff --git a/odb/mysql/transaction-impl.hxx b/odb/mysql/transaction-impl.hxx new file mode 100644 index 0000000..1e611b8 --- /dev/null +++ b/odb/mysql/transaction-impl.hxx @@ -0,0 +1,50 @@ +// file : odb/mysql/transaction-impl.hxx +// author : Boris Kolpackov <boris@codesynthesis.com> +// copyright : Copyright (c) 2009-2010 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef ODB_MYSQL_TRANSACTION_IMPL_HXX +#define ODB_MYSQL_TRANSACTION_IMPL_HXX + +#include <odb/shared-ptr.hxx> +#include <odb/transaction.hxx> + +#include <odb/mysql/forward.hxx> + +namespace odb +{ + namespace mysql + { + class transaction_impl: public odb::transaction_impl + { + protected: + friend class database; + friend class transaction; + + typedef mysql::database database_type; + typedef mysql::connection connection_type; + + transaction_impl (database_type&); + transaction_impl (database_type&, session_type&); + + virtual + ~transaction_impl (); + + virtual void + commit (); + + virtual void + rollback (); + + connection_type& + connection (); + + private: + shared_ptr<connection_type> connection_; + }; + } +} + +#include <odb/mysql/transaction-impl.ixx> + +#endif // ODB_MYSQL_TRANSACTION_IMPL_HXX diff --git a/odb/mysql/transaction-impl.ixx b/odb/mysql/transaction-impl.ixx new file mode 100644 index 0000000..5f022d7 --- /dev/null +++ b/odb/mysql/transaction-impl.ixx @@ -0,0 +1,16 @@ +// file : odb/mysql/transaction-impl.ixx +// author : Boris Kolpackov <boris@codesynthesis.com> +// copyright : Copyright (c) 2009-2010 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +namespace odb +{ + namespace mysql + { + inline transaction_impl::connection_type& transaction_impl:: + connection () + { + return *connection_; + } + } +} diff --git a/odb/mysql/transaction.cxx b/odb/mysql/transaction.cxx new file mode 100644 index 0000000..ac8cd04 --- /dev/null +++ b/odb/mysql/transaction.cxx @@ -0,0 +1,26 @@ +// file : odb/mysql/transaction.cxx +// author : Boris Kolpackov <boris@codesynthesis.com> +// copyright : Copyright (c) 2009-2010 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#include <odb/mysql/transaction.hxx> + +namespace odb +{ + namespace mysql + { + transaction& transaction:: + current () + { + // While the impl type can be of the concrete type, the transaction + // object can be created as either odb:: or odb::mysql:: type. To + // work around that we are going to hard-cast one two the other + // relying on the fact that they have the same representation and + // no virtual functions. The former is checked in the tests. + // + odb::transaction& b (odb::transaction::current ()); + dynamic_cast<transaction_impl&> (b.implementation ()); + return reinterpret_cast<transaction&> (b); + } + } +} diff --git a/odb/mysql/transaction.hxx b/odb/mysql/transaction.hxx new file mode 100644 index 0000000..ebb62b3 --- /dev/null +++ b/odb/mysql/transaction.hxx @@ -0,0 +1,53 @@ +// file : odb/mysql/transaction.hxx +// author : Boris Kolpackov <boris@codesynthesis.com> +// copyright : Copyright (c) 2009-2010 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef ODB_MYSQL_TRANSACTION_HXX +#define ODB_MYSQL_TRANSACTION_HXX + +#include <odb/transaction.hxx> + +#include <odb/mysql/forward.hxx> + +namespace odb +{ + namespace mysql + { + class transaction_impl; + + class transaction: public odb::transaction + { + public: + typedef mysql::database database_type; + typedef mysql::connection connection_type; + + explicit + transaction (transaction_impl*); + + // Return the database this transaction is on. + // + database_type& + database (); + + // Return the underlying database connection for this transaction. + // + connection_type& + connection (); + + // Return current transaction or throw if there is no transaction + // in effect. + // + static transaction& + current (); + + public: + transaction_impl& + implementation (); + }; + } +} + +#include <odb/mysql/transaction.ixx> + +#endif // ODB_MYSQL_TRANSACTION_HXX diff --git a/odb/mysql/transaction.ixx b/odb/mysql/transaction.ixx new file mode 100644 index 0000000..d4f7cc8 --- /dev/null +++ b/odb/mysql/transaction.ixx @@ -0,0 +1,41 @@ +// file : odb/mysql/transaction.ixx +// author : Boris Kolpackov <boris@codesynthesis.com> +// copyright : Copyright (c) 2009-2010 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#include <odb/mysql/database.hxx> +#include <odb/mysql/transaction-impl.hxx> + +namespace odb +{ + namespace mysql + { + inline transaction:: + transaction (transaction_impl* impl) + : odb::transaction (impl) + { + } + + inline transaction::database_type& transaction:: + database () + { + return static_cast<database_type&> (odb::transaction::database ()); + } + + inline transaction::connection_type& transaction:: + connection () + { + return implementation ().connection (); + } + + inline transaction_impl& transaction:: + implementation () + { + // We can use static_cast here since we have an instance of + // mysql::transaction. + // + return static_cast<transaction_impl&> ( + odb::transaction::implementation ()); + } + } +} |