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 | ee570de4bd013fd2a4351d33d11539621f00bf57 (patch) | |
tree | 48a7857b2e5fcf799ae4773cbd61252b76ec7512 /odb/pgsql/connection.cxx | |
parent | 6e3ac760696d4ec43138b1aba82426582c767072 (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/pgsql/connection.cxx')
-rw-r--r-- | odb/pgsql/connection.cxx | 46 |
1 files changed, 44 insertions, 2 deletions
diff --git a/odb/pgsql/connection.cxx b/odb/pgsql/connection.cxx index 36a5713..808cdbb 100644 --- a/odb/pgsql/connection.cxx +++ b/odb/pgsql/connection.cxx @@ -4,16 +4,19 @@ // license : GNU GPL v2; see accompanying LICENSE file #include <new> // std::bad_alloc -#include <cstring> // std::strcmp #include <string> +#include <cstring> // std::strcmp +#include <cstdlib> // std::atol #include <libpq-fe.h> #include <odb/pgsql/database.hxx> #include <odb/pgsql/connection.hxx> +#include <odb/pgsql/transaction.hxx> #include <odb/pgsql/error.hxx> #include <odb/pgsql/exceptions.hxx> #include <odb/pgsql/statement-cache.hxx> +#include <odb/pgsql/result-ptr.hxx> using namespace std; @@ -28,7 +31,8 @@ namespace odb { connection:: connection (database_type& db) - : db_ (db), + : odb::connection (db), + db_ (db), handle_ (0), statement_cache_ (new statement_cache_type (*this)) { @@ -65,5 +69,43 @@ namespace odb PQfinish (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) + { + // The string may not be '\0'-terminated. + // + string str (s, n); + + result_ptr r (PQexec (handle_, str.c_str ())); + PGresult* h (r.get ()); + + unsigned long long count (0); + + if (!is_good_result (h)) + translate_error (*this, h); + else if (PGRES_TUPLES_OK == PQresultStatus (h)) + count = static_cast<unsigned long long> (PQntuples (h)); + else + { + const char* s (PQcmdTuples (h)); + + if (s[0] != '\0' && s[1] == '\0') + count = static_cast<unsigned long long> (s[0] - '0'); + else + count = static_cast<unsigned long long> (atol (s)); + } + + return count; + } } } |