aboutsummaryrefslogtreecommitdiff
path: root/odb/sqlite/connection.cxx
diff options
context:
space:
mode:
Diffstat (limited to 'odb/sqlite/connection.cxx')
-rw-r--r--odb/sqlite/connection.cxx110
1 files changed, 102 insertions, 8 deletions
diff --git a/odb/sqlite/connection.cxx b/odb/sqlite/connection.cxx
index ab17c7c..0445163 100644
--- a/odb/sqlite/connection.cxx
+++ b/odb/sqlite/connection.cxx
@@ -1,5 +1,4 @@
// file : odb/sqlite/connection.cxx
-// copyright : Copyright (c) 2005-2019 Code Synthesis Tools CC
// license : GNU GPL v2; see accompanying LICENSE file
#include <new> // std::bad_alloc
@@ -26,11 +25,16 @@ odb_sqlite_connection_unlock_callback (void**, int);
namespace odb
{
+ using namespace details;
+
namespace sqlite
{
connection::
- connection (connection_factory& cf, int extra_flags)
+ connection (connection_factory& cf,
+ int extra_flags,
+ statement_translator* st)
: odb::connection (cf),
+ statement_translator_ (st),
unlock_cond_ (unlock_mutex_),
active_objects_ (0)
{
@@ -82,9 +86,12 @@ namespace odb
}
connection::
- connection (connection_factory& cf, sqlite3* handle)
+ connection (connection_factory& cf,
+ sqlite3* handle,
+ statement_translator* st)
: odb::connection (cf),
handle_ (handle),
+ statement_translator_ (st),
unlock_cond_ (unlock_mutex_),
active_objects_ (0)
{
@@ -106,6 +113,31 @@ namespace odb
db.foreign_keys () ? 22 : 23);
st.execute ();
+ // String lengths include '\0', as per the SQLite manual suggestion.
+ //
+ begin_.reset (new (shared) generic_statement (*this, "BEGIN", 6));
+ commit_.reset (new (shared) generic_statement (*this, "COMMIT", 7));
+ rollback_.reset (new (shared) generic_statement (*this, "ROLLBACK", 9));
+
+ // Create statement cache.
+ //
+ statement_cache_.reset (new statement_cache_type (*this));
+ }
+
+ connection::
+ connection (attached_connection_factory& cf, statement_translator* st)
+ : odb::connection (cf),
+ handle_ (0),
+ statement_translator_ (st),
+ unlock_cond_ (unlock_mutex_),
+ active_objects_ (0)
+ {
+ // Copy some things over from the main connection.
+ //
+ connection& main (*cf.main_connection_);
+
+ tracer_ = main.tracer_;
+
// Create statement cache.
//
statement_cache_.reset (new statement_cache_type (*this));
@@ -120,6 +152,44 @@ namespace odb
clear_prepared_map ();
}
+ generic_statement& connection::
+ begin_statement ()
+ {
+ return static_cast<generic_statement&> (*begin_);
+ }
+
+ generic_statement& connection::
+ begin_immediate_statement ()
+ {
+ if (!begin_immediate_)
+ begin_immediate_.reset (
+ new (shared) generic_statement (*this, "BEGIN IMMEDIATE", 16));
+
+ return static_cast<generic_statement&> (*begin_immediate_);
+ }
+
+ generic_statement& connection::
+ begin_exclusive_statement ()
+ {
+ if (!begin_exclusive_)
+ begin_exclusive_.reset (
+ new (shared) generic_statement (*this, "BEGIN EXCLUSIVE", 16));
+
+ return static_cast<generic_statement&> (*begin_exclusive_);
+ }
+
+ generic_statement& connection::
+ commit_statement ()
+ {
+ return static_cast<generic_statement&> (*commit_);
+ }
+
+ generic_statement& connection::
+ rollback_statement ()
+ {
+ return static_cast<generic_statement&> (*rollback_);
+ }
+
transaction_impl* connection::
begin ()
{
@@ -168,7 +238,7 @@ namespace odb
// unlock_notify() returns SQLITE_OK or SQLITE_LOCKED (deadlock).
//
- int e (sqlite3_unlock_notify (handle_,
+ int e (sqlite3_unlock_notify (handle (),
&odb_sqlite_connection_unlock_callback,
this));
if (e == SQLITE_LOCKED)
@@ -186,11 +256,21 @@ namespace odb
void connection::
clear ()
{
- // The current first active_object will remove itself from the list
- // and make the second object (if any) the new first.
+ invalidate_results ();
+
+ // The current first active_object may remove itself from the list and
+ // make the second object (if any) the new first.
//
- while (active_objects_ != 0)
- active_objects_->clear ();
+ for (active_object** pp (&active_objects_); *pp != 0; )
+ {
+ active_object* p (*pp);
+ p->clear ();
+
+ // Move to the next object if this one decided to stay on the list.
+ //
+ if (*pp == p)
+ pp = &p->next_;
+ }
}
// connection_factory
@@ -206,6 +286,20 @@ namespace odb
odb::connection_factory::db_ = &db;
db_ = &db;
}
+
+ void connection_factory::
+ attach_database (const connection_ptr& conn,
+ const std::string& name,
+ const std::string& schema)
+ {
+ conn->execute ("ATTACH DATABASE '" + name + "' AS \"" + schema + '"');
+ }
+
+ void connection_factory::
+ detach_database (const connection_ptr& conn, const std::string& schema)
+ {
+ conn->execute ("DETACH DATABASE \"" + schema + '"');
+ }
}
}