sqlite plugin requires libsqlite3 => 3.3.1 to share connections
authorMartin Willi <martin@strongswan.org>
Mon, 30 Jun 2008 11:06:18 +0000 (11:06 -0000)
committerMartin Willi <martin@strongswan.org>
Mon, 30 Jun 2008 11:06:18 +0000 (11:06 -0000)
use recursive locking if libsqlite3 < 3.5.0

configure.in
src/libstrongswan/plugins/sqlite/sqlite_database.c

index 1d4b4f9..1ce53ed 100644 (file)
@@ -684,6 +684,15 @@ if test x$sqlite = xtrue; then
                        void *test = sqlite3_prepare_v2;
                ], 
                [AC_MSG_RESULT([yes])]; AC_DEFINE_UNQUOTED(HAVE_SQLITE3_PREPARE_V2, 1), [AC_MSG_RESULT([no])])
+       AC_MSG_CHECKING([sqlite3.h version >= 3.3.1])
+       AC_TRY_COMPILE(
+               [#include <sqlite3.h>],
+               [
+                       #if SQLITE_VERSION_NUMBER < 3003001
+                               #error bad sqlite
+                       #endif
+               ], 
+               [AC_MSG_RESULT([yes])], [AC_MSG_RESULT([no]); AC_MSG_ERROR([SQLite version >= 3.3.1 required!])])
 fi
 
 if test x$openssl = xtrue; then
index 99f86a8..65aa254 100644 (file)
@@ -131,8 +131,8 @@ typedef struct {
        int count;
        /** column types */
        db_type_t *columns;
-       /** reference to db connection */
-       sqlite3 *db;
+       /** back reference to parent */
+       private_sqlite_database_t *database;
 } sqlite_enumerator_t;
 
 /**
@@ -141,6 +141,9 @@ typedef struct {
 static void sqlite_enumerator_destroy(sqlite_enumerator_t *this)
 {
        sqlite3_finalize(this->stmt);
+#if SQLITE_VERSION_NUMBER < 3005000
+       this->database->mutex->unlock(this->database->mutex);
+#endif
        free(this->columns);
        free(this);
 }
@@ -158,7 +161,8 @@ static bool sqlite_enumerator_enumerate(sqlite_enumerator_t *this, ...)
                case SQLITE_ROW:
                        break;
                default:
-                       DBG1("stepping sqlite statement failed: %s", sqlite3_errmsg(this->db));
+                       DBG1("stepping sqlite statement failed: %s",
+                                sqlite3_errmsg(this->database->db));
                        /* fall */
                case SQLITE_DONE:
                        return FALSE;
@@ -218,6 +222,10 @@ static enumerator_t* query(private_sqlite_database_t *this, char *sql, ...)
        sqlite_enumerator_t *enumerator = NULL;
        int i;
        
+#if SQLITE_VERSION_NUMBER < 3005000
+       /* sqlite connections prior to 3.5 may be used by a single thread only, */
+       this->mutex->lock(this->mutex);
+#endif
        
        va_start(args, sql);
        stmt = run(this, sql, &args);
@@ -229,7 +237,7 @@ static enumerator_t* query(private_sqlite_database_t *this, char *sql, ...)
                enumerator->stmt = stmt;
                enumerator->count = sqlite3_column_count(stmt);
                enumerator->columns = malloc(sizeof(db_type_t) * enumerator->count);
-               enumerator->db = this->db;
+               enumerator->database = this;
                for (i = 0; i < enumerator->count; i++)
                {
                        enumerator->columns[i] = va_arg(args, db_type_t);
@@ -306,7 +314,7 @@ sqlite_database_t *sqlite_database_create(char *uri)
        this->public.db.execute = (int (*)(database_t *this, int *rowid, char *sql, ...))execute;
        this->public.db.destroy = (void(*)(database_t*))destroy;
        
-       this->mutex = mutex_create(MUTEX_DEFAULT);
+       this->mutex = mutex_create(MUTEX_RECURSIVE);
        
        if (sqlite3_open(file, &this->db) != SQLITE_OK)
        {