summaryrefslogtreecommitdiff
path: root/src/libstrongswan/plugins/sqlite/sqlite_database.c
diff options
context:
space:
mode:
authorRene Mayrhofer <rene@mayrhofer.eu.org>2008-10-29 11:11:01 +0000
committerRene Mayrhofer <rene@mayrhofer.eu.org>2008-10-29 11:11:01 +0000
commit8b80ab5a6950ce6515f477624794defd7531642a (patch)
treeaa8303f3806c5615fbeafc4dc82febe3cd7c24dc /src/libstrongswan/plugins/sqlite/sqlite_database.c
parentdb67c87db3c9089ea8d2e14f617bf3d9e2af261f (diff)
downloadvyos-strongswan-8b80ab5a6950ce6515f477624794defd7531642a.tar.gz
vyos-strongswan-8b80ab5a6950ce6515f477624794defd7531642a.zip
[svn-upgrade] Integrating new upstream version, strongswan (4.2.8)
Diffstat (limited to 'src/libstrongswan/plugins/sqlite/sqlite_database.c')
-rw-r--r--src/libstrongswan/plugins/sqlite/sqlite_database.c43
1 files changed, 37 insertions, 6 deletions
diff --git a/src/libstrongswan/plugins/sqlite/sqlite_database.c b/src/libstrongswan/plugins/sqlite/sqlite_database.c
index d5a03894d..c8d9e5beb 100644
--- a/src/libstrongswan/plugins/sqlite/sqlite_database.c
+++ b/src/libstrongswan/plugins/sqlite/sqlite_database.c
@@ -12,12 +12,13 @@
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
- * $Id: sqlite_database.c 3911 2008-05-07 14:41:13Z martin $
+ * $Id: sqlite_database.c 4268 2008-08-21 11:58:58Z andreas $
*/
#include "sqlite_database.h"
#include <sqlite3.h>
+#include <unistd.h>
#include <library.h>
#include <debug.h>
#include <utils/mutex.h>
@@ -131,8 +132,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 +142,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 +162,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 +223,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 +238,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);
@@ -274,6 +283,25 @@ static int execute(private_sqlite_database_t *this, int *rowid, char *sql, ...)
}
/**
+ * Implementation of database_t.get_driver
+ */
+static db_driver_t get_driver(private_sqlite_database_t *this)
+{
+ return DB_SQLITE;
+}
+
+/**
+ * Busy handler implementation
+ */
+static int busy_handler(private_sqlite_database_t *this, int count)
+{
+ /* add a backoff time, quadratically increasing with every try */
+ usleep(count * count * 1000);
+ /* always retry */
+ return 1;
+}
+
+/**
* Implementation of database_t.destroy
*/
static void destroy(private_sqlite_database_t *this)
@@ -304,9 +332,10 @@ sqlite_database_t *sqlite_database_create(char *uri)
this->public.db.query = (enumerator_t* (*)(database_t *this, char *sql, ...))query;
this->public.db.execute = (int (*)(database_t *this, int *rowid, char *sql, ...))execute;
+ this->public.db.get_driver = (db_driver_t(*)(database_t*))get_driver;
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)
{
@@ -316,6 +345,8 @@ sqlite_database_t *sqlite_database_create(char *uri)
return NULL;
}
+ sqlite3_busy_handler(this->db, (void*)busy_handler, this);
+
return &this->public;
}