From 275f4e494cacd403d28b01f8e3356a6669ef8e13 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Wed, 2 Oct 2013 08:29:06 +0200 Subject: Add support for SQLite 3.3.6 That's what is still shipped with RHEL5. --- m4/libsqlite.m4 | 4 +- odb/sqlite/connection.cxx | 14 +++++- odb/sqlite/database.hxx | 9 ++++ odb/sqlite/statement.cxx | 109 +++++++++++++++++++++++++++++++++++++++++++++- odb/sqlite/statement.hxx | 14 ++++-- 5 files changed, 141 insertions(+), 9 deletions(-) diff --git a/m4/libsqlite.m4 b/m4/libsqlite.m4 index a75062c..4bbe9a1 100644 --- a/m4/libsqlite.m4 +++ b/m4/libsqlite.m4 @@ -24,9 +24,9 @@ int main () { sqlite3* handle; - sqlite3_open_v2 ("", &handle, 0, 0); + sqlite3_open ("", &handle); sqlite3_stmt* stmt; - sqlite3_prepare_v2 (handle, "", 0, &stmt, 0); + sqlite3_prepare (handle, "", 0, &stmt, 0); sqlite3_finalize (stmt); sqlite3_close (handle); } diff --git a/odb/sqlite/connection.cxx b/odb/sqlite/connection.cxx index f4e7259..188d59f 100644 --- a/odb/sqlite/connection.cxx +++ b/odb/sqlite/connection.cxx @@ -51,12 +51,22 @@ namespace odb f |= SQLITE_OPEN_NOMUTEX; #endif - const string& vfs (db.vfs ()); - sqlite3* h (0); + + // sqlite3_open_v2() was only addedin SQLite 3.5.0. + // +#if SQLITE_VERSION_NUMBER >= 3005000 + const string& vfs (db.vfs ()); int e ( sqlite3_open_v2 ( n.c_str (), &h, f, (vfs.empty () ? 0 : vfs.c_str ()))); +#else + // Readonly opening not supported in SQLite earlier than 3.5.0. + // + assert ((f & SQLITE_OPEN_READONLY) == 0); + int e (sqlite3_open (n.c_str (), &h)); +#endif + handle_.reset (h); if (e != SQLITE_OK) diff --git a/odb/sqlite/database.hxx b/odb/sqlite/database.hxx index 6039f23..fd4a736 100644 --- a/odb/sqlite/database.hxx +++ b/odb/sqlite/database.hxx @@ -27,6 +27,15 @@ #include +// We use the sqlite3_open_v2() flags in our interface. Define them +// for SQLite earlier that 3.5.0. +// +#if SQLITE_VERSION_NUMBER < 3005000 +# define SQLITE_OPEN_READONLY 0x00000001 +# define SQLITE_OPEN_READWRITE 0x00000002 +# define SQLITE_OPEN_CREATE 0x00000004 +#endif + namespace odb { namespace sqlite diff --git a/odb/sqlite/statement.cxx b/odb/sqlite/statement.cxx index f20d8da..62103fc 100644 --- a/odb/sqlite/statement.cxx +++ b/odb/sqlite/statement.cxx @@ -88,6 +88,11 @@ namespace odb int e; sqlite3_stmt* stmt (0); + + // sqlite3_prepare_v2() is only available since SQLite 3.3.9 + // but is buggy until 3.3.11. + // +#if SQLITE_VERSION_NUMBER >= 3003011 while ((e = sqlite3_prepare_v2 (conn_.handle (), text, static_cast (text_size), @@ -96,11 +101,23 @@ namespace odb { conn_.wait (); } +#else + e = sqlite3_prepare (conn_.handle (), + text, + static_cast (text_size), + &stmt, + 0); +#endif if (e != SQLITE_OK) translate_error (e, conn_); stmt_.reset (stmt); + +#if SQLITE_VERSION_NUMBER < 3005003 + text_.assign (text, text_size); +#endif + active_ = false; prev_ = 0; @@ -118,7 +135,13 @@ namespace odb const char* statement:: text () const { + // sqlite3_sql() is only available since 3.5.3. + // +#if SQLITE_VERSION_NUMBER >= 3005003 return sqlite3_sql (stmt_); +#else + return text_.c_str (); +#endif } void statement:: @@ -148,7 +171,16 @@ namespace odb case bind::integer: { long long v (*static_cast (b.buffer)); - e = sqlite3_bind_int64 (stmt_, c, static_cast (v)); + + e = sqlite3_bind_int64 (stmt_, + c, + // Prior to 3.5.0, sqlite3_int64 was called sqlite_int64. +#if SQLITE_VERSION_NUMBER >= 3005000 + static_cast (v) +#else + static_cast (v) +#endif + ); break; } case bind::real: @@ -355,10 +387,42 @@ namespace odb for (; e == SQLITE_ROW; e = sqlite3_step (stmt_)) r++; + // sqlite3_step() will return a detailed error code only if we used + // sqlite3_prepare_v2(). Otherwise, sqlite3_reset() returns the + // error. + // +#if SQLITE_VERSION_NUMBER >= 3003011 sqlite3_reset (stmt_); if (e != SQLITE_DONE) translate_error (e, conn_); +#else + e = sqlite3_reset (stmt_); + + if (e != SQLITE_OK) + { + // If the schema has changed, try to re-prepare and re-execute the + // statement. That's what newer versions of SQLite do automatically. + // + if (e == SQLITE_SCHEMA) + { + sqlite3_stmt* stmt (0); + e = sqlite3_prepare (h, + text_.c_str (), + static_cast (text_.size () + 1), + &stmt, + 0); + + if (e != SQLITE_OK) + translate_error (e, conn_); + + stmt_.reset (stmt); + return execute (); // Try again by recursively calling ourselves. + } + else + translate_error (e, conn_); + } +#endif if (!result_set_) r = static_cast (sqlite3_changes (h)); @@ -479,10 +543,21 @@ namespace odb if (e != SQLITE_ROW) { - reset (); done_ = true; + // sqlite3_step() will return a detailed error code only if we used + // sqlite3_prepare_v2(). Otherwise, sqlite3_reset() returns the + // error. + // +#if SQLITE_VERSION_NUMBER >= 3003011 + reset (); + if (e != SQLITE_DONE) +#else + e = reset (); + + if (e != SQLITE_OK) +#endif translate_error (e, conn_); } } @@ -564,9 +639,19 @@ namespace odb e = sqlite3_step (stmt_); #endif + // sqlite3_step() will return a detailed error code only if we used + // sqlite3_prepare_v2(). Otherwise, sqlite3_reset() returns the + // error. + // +#if SQLITE_VERSION_NUMBER >= 3003011 sqlite3_reset (stmt_); if (e != SQLITE_DONE) +#else + e = sqlite3_reset (stmt_); + + if (e != SQLITE_OK) +#endif { // SQLITE_CONSTRAINT error code covers more than just a duplicate // primary key. Unfortunately, there is nothing more precise that @@ -645,9 +730,19 @@ namespace odb e = sqlite3_step (stmt_); #endif + // sqlite3_step() will return a detailed error code only if we used + // sqlite3_prepare_v2(). Otherwise, sqlite3_reset() returns the + // error. + // +#if SQLITE_VERSION_NUMBER >= 3003011 sqlite3_reset (stmt_); if (e != SQLITE_DONE) +#else + e = sqlite3_reset (stmt_); + + if (e != SQLITE_OK) +#endif translate_error (e, conn_); return static_cast (sqlite3_changes (h)); @@ -707,9 +802,19 @@ namespace odb e = sqlite3_step (stmt_); #endif + // sqlite3_step() will return a detailed error code only if we used + // sqlite3_prepare_v2(). Otherwise, sqlite3_reset() returns the + // error. + // +#if SQLITE_VERSION_NUMBER >= 3003011 sqlite3_reset (stmt_); if (e != SQLITE_DONE) +#else + e = sqlite3_reset (stmt_); + + if (e != SQLITE_OK) +#endif translate_error (e, conn_); return static_cast (sqlite3_changes (h)); diff --git a/odb/sqlite/statement.hxx b/odb/sqlite/statement.hxx index 05fa9bd..7d7f021 100644 --- a/odb/sqlite/statement.hxx +++ b/odb/sqlite/statement.hxx @@ -131,15 +131,19 @@ namespace odb } } - void + int reset () { + int r (SQLITE_OK); + if (active_) { - sqlite3_reset (stmt_); + r = sqlite3_reset (stmt_); list_remove (); active_ = false; } + + return r; } protected: @@ -148,6 +152,10 @@ namespace odb connection_type& conn_; auto_handle stmt_; +#if SQLITE_VERSION_NUMBER < 3005003 + std::string text_; +#endif + bool active_; private: @@ -160,7 +168,7 @@ namespace odb // Doubly-linked list of active statements. // - private: + protected: void list_add () { -- cgit v1.1