From 5a69eecefb4f5d29d7ab76f81e69ab27d85586cf Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Mon, 22 Aug 2011 10:47:30 +0200 Subject: Enable foreign key constraints checking in SQLite Due to bugs in SQLite DDL foreign key support, we have to temporarily disable foreign keys when re-creating the schema. New manual section: 12.5.3, "Foreign Key Constraints". --- odb/sqlite/connection.cxx | 10 ++++++++++ odb/sqlite/database.cxx | 9 +++++++-- odb/sqlite/database.hxx | 9 +++++++++ 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/odb/sqlite/connection.cxx b/odb/sqlite/connection.cxx index d90c301..ffb70a7 100644 --- a/odb/sqlite/connection.cxx +++ b/odb/sqlite/connection.cxx @@ -66,6 +66,16 @@ namespace odb translate_error (e, *this); } + // Enable/disable foreign key constraints. + // + simple_statement st ( + *this, + db.foreign_keys () + ? "PRAGMA foreign_keys=ON" + : "PRAGMA foreign_keys=OFF", + db.foreign_keys () ? 22 : 23); + st.execute (); + statement_cache_.reset (new statement_cache_type (*this)); } diff --git a/odb/sqlite/database.cxx b/odb/sqlite/database.cxx index e756608..6ce4621 100644 --- a/odb/sqlite/database.cxx +++ b/odb/sqlite/database.cxx @@ -27,8 +27,12 @@ namespace odb database:: database (const string& name, int flags, + bool foreign_keys, auto_ptr factory) - : name_ (name), flags_ (flags), factory_ (factory) + : name_ (name), + flags_ (flags), + foreign_keys_ (foreign_keys), + factory_ (factory) { if (factory_.get () == 0) factory_.reset (new connection_pool_factory ()); @@ -41,8 +45,9 @@ namespace odb char* argv[], bool erase, int flags, + bool foreign_keys, std::auto_ptr factory) - : flags_ (flags), factory_ (factory) + : flags_ (flags), foreign_keys_ (foreign_keys), factory_ (factory) { using namespace details; diff --git a/odb/sqlite/database.hxx b/odb/sqlite/database.hxx index 0eadbbf..13c44e6 100644 --- a/odb/sqlite/database.hxx +++ b/odb/sqlite/database.hxx @@ -35,6 +35,7 @@ namespace odb public: database (const std::string& name, int flags = SQLITE_OPEN_READWRITE, + bool foreign_keys = true, std::auto_ptr = std::auto_ptr (0)); @@ -56,6 +57,7 @@ namespace odb char* argv[], bool erase = false, int flags = SQLITE_OPEN_READWRITE, + bool foreign_keys = true, std::auto_ptr = std::auto_ptr (0)); @@ -76,6 +78,12 @@ namespace odb return flags_; } + bool + foreign_keys () const + { + return foreign_keys_; + } + // Transactions. // public: @@ -103,6 +111,7 @@ namespace odb private: std::string name_; int flags_; + bool foreign_keys_; std::auto_ptr factory_; }; } -- cgit v1.1