From d062cc21ae1dc530a21017dfbf841e557d64c6c3 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Thu, 25 Apr 2013 07:35:45 +0200 Subject: Add support for schema version table --- odb/sqlite/database.cxx | 92 +++++++++++++++++++++++++++++++++++++++++++++++++ odb/sqlite/database.hxx | 8 ++++- 2 files changed, 99 insertions(+), 1 deletion(-) (limited to 'odb') diff --git a/odb/sqlite/database.cxx b/odb/sqlite/database.cxx index e8e5c77..d5399f7 100644 --- a/odb/sqlite/database.cxx +++ b/odb/sqlite/database.cxx @@ -6,11 +6,16 @@ # include // WideCharToMultiByte #endif +#include + +#include #include #include #include #include +#include +#include #include #include @@ -177,5 +182,92 @@ namespace odb connection_ptr c (factory_->connect ()); return c.release (); } + + const database::schema_version_info& database:: + load_schema_version (const string& name) const + { + schema_version_info& svi (schema_version_map_[name]); + + // Construct the SELECT statement text. + // + string text ("SELECT \"version\", \"migration\" FROM "); + + if (!svi.version_table.empty ()) + text += svi.version_table; // Already quoted. + else if (!schema_version_table_.empty ()) + text += schema_version_table_; // Already quoted. + else + text += "\"schema_version\""; + + text += " WHERE \"name\" = ?"; + + // Bind parameters and results. + // + size_t psize[1] = {name.size ()}; + bind pbind[1] = {{bind::text, + const_cast (name.c_str ()), + &psize[0], + 0, 0, 0}}; + binding param (pbind, 1); + param.version++; + + long long migration; + bool rnull[2]; + bind rbind[2] = {{bind::integer, &svi.version, 0, 0, &rnull[0], 0}, + {bind::integer, &migration, 0, 0, &rnull[1], 0}}; + binding result (rbind, 2); + result.version++; + + // If we are not in transaction, SQLite will start an implicit one + // which suits us just fine. + // + connection_ptr cp; + if (!transaction::has_current ()) + cp = factory_->connect (); + + sqlite::connection& c ( + cp != 0 ? *cp : transaction::current ().connection ()); + + try + { + select_statement st (c, text, param, result); + st.execute (); + auto_result ar (st); + + switch (st.fetch ()) + { + case select_statement::success: + { + svi.migration = migration != 0; + assert (st.fetch () == select_statement::no_data); + break; + } + case select_statement::no_data: + { + svi.version = 0; // No schema. + break; + } + case select_statement::truncated: + { + assert (false); + break; + } + } + } + catch (const database_exception& e) + { + // Try to detect the case where there is no version table. SQLite + // doesn't have an extended error code for this so we have to use + // the error text. + // + if (e.error () == SQLITE_ERROR && + e.message ().compare (0, 14, "no such table:") == 0) + svi.version = 0; // No schema. + else + throw; + } + + return svi; + } } } diff --git a/odb/sqlite/database.hxx b/odb/sqlite/database.hxx index ee758e5..0471b64 100644 --- a/odb/sqlite/database.hxx +++ b/odb/sqlite/database.hxx @@ -349,9 +349,15 @@ namespace odb using odb::database::tracer; - public: + // Database schema version. + // + protected: + virtual const schema_version_info& + load_schema_version (const std::string& schema_name) const; + // Database id constant (useful for meta-programming). // + public: static const odb::database_id database_id = id_sqlite; public: -- cgit v1.1