diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2011-08-21 16:27:34 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2011-08-21 16:27:34 +0200 |
commit | e84fa8e3b8defe24a00e14ba046e8e71b438ce0b (patch) | |
tree | 2d17fab73f0bbb1a96f365e38ecbd63e033af36f /odb/mysql/connection.cxx | |
parent | 47d344f31f7a1a980d807c142e0a36b09db3b1f3 (diff) |
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.
Diffstat (limited to 'odb/mysql/connection.cxx')
-rw-r--r-- | odb/mysql/connection.cxx | 42 |
1 files changed, 41 insertions, 1 deletions
diff --git a/odb/mysql/connection.cxx b/odb/mysql/connection.cxx index 3f4a664..fe4996f 100644 --- a/odb/mysql/connection.cxx +++ b/odb/mysql/connection.cxx @@ -8,6 +8,7 @@ #include <odb/mysql/database.hxx> #include <odb/mysql/connection.hxx> +#include <odb/mysql/transaction.hxx> #include <odb/mysql/statement.hxx> #include <odb/mysql/error.hxx> #include <odb/mysql/exceptions.hxx> @@ -21,7 +22,8 @@ namespace odb { connection:: connection (database_type& db) - : db_ (db), + : odb::connection (db), + db_ (db), failed_ (false), handle_ (&mysql_), active_ (0), @@ -72,6 +74,44 @@ namespace odb mysql_close (handle_); } + transaction_impl* connection:: + begin () + { + if (transaction::has_current ()) + throw already_in_transaction (); + + return new transaction_impl (connection_ptr (inc_ref (this))); + } + + unsigned long long connection:: + execute (const char* s, std::size_t n) + { + clear (); + + if (mysql_real_query (handle_, s, static_cast<unsigned long> (n))) + translate_error (*this); + + // Get the affected row count, if any. If the statement has a result + // set (e.g., SELECT), we first need to call mysql_store_result(). + // + unsigned long long r (0); + + if (mysql_field_count (handle_) == 0) + r = static_cast<unsigned long long> (mysql_affected_rows (handle_)); + else + { + if (MYSQL_RES* rs = mysql_store_result (handle_)) + { + r = static_cast<unsigned long long> (mysql_num_rows (rs)); + mysql_free_result (rs); + } + else + translate_error (*this); + } + + return r; + } + bool connection:: ping () { |