aboutsummaryrefslogtreecommitdiff
path: root/odb/sqlite/connection.cxx
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2011-03-21 17:24:35 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2011-03-21 17:24:35 +0200
commitdac72baef46897b80fc98632cef182fb266a5d60 (patch)
treea90f40a5fac59456c6fecf3d31a008c5a061b955 /odb/sqlite/connection.cxx
parent3af997a875e439e71754fddb67fd60de9f60307b (diff)
Add base SQLite database classes
Diffstat (limited to 'odb/sqlite/connection.cxx')
-rw-r--r--odb/sqlite/connection.cxx59
1 files changed, 59 insertions, 0 deletions
diff --git a/odb/sqlite/connection.cxx b/odb/sqlite/connection.cxx
new file mode 100644
index 0000000..37f5e20
--- /dev/null
+++ b/odb/sqlite/connection.cxx
@@ -0,0 +1,59 @@
+// file : odb/sqlite/connection.cxx
+// author : Boris Kolpackov <boris@codesynthesis.com>
+// copyright : Copyright (c) 2005-2011 Code Synthesis Tools CC
+// license : GNU GPL v2; see accompanying LICENSE file
+
+#include <new> // std::bad_alloc
+#include <string>
+#include <cassert>
+
+#include <odb/sqlite/database.hxx>
+#include <odb/sqlite/connection.hxx>
+#include <odb/sqlite/statement-cache.hxx>
+#include <odb/sqlite/error.hxx>
+
+using namespace std;
+
+namespace odb
+{
+ namespace sqlite
+ {
+ connection::
+ connection (database_type& db)
+ : db_ (db)
+ {
+ int f (db.flags ());
+ const string& n (db.name ());
+
+ // If we are opening a temporary database, then add the create flag.
+ //
+ if (n.empty () || n == ":memory:")
+ f |= SQLITE_OPEN_CREATE;
+
+ // A connection can only be used by a single thread at a time. So
+ // disable locking in SQLite unless explicitly requested.
+ //
+ if ((f & SQLITE_OPEN_FULLMUTEX) == 0)
+ f |= SQLITE_OPEN_NOMUTEX;
+
+ if (int e = sqlite3_open_v2 (n.c_str (), &handle_, f, 0))
+ {
+ if (handle_ == 0)
+ throw bad_alloc ();
+
+ translate_error (e, *this);
+ }
+
+ statement_cache_.reset (new statement_cache_type (*this));
+ }
+
+ connection::
+ ~connection ()
+ {
+ statement_cache_.reset (); // Free prepared statements.
+
+ if (sqlite3_close (handle_) == SQLITE_BUSY)
+ assert (false); // Connection has outstanding prepared statements.
+ }
+ }
+}