diff options
-rw-r--r-- | CommonLibs/Makefile.am | 6 | ||||
-rw-r--r-- | CommonLibs/sqlite3util.cpp | 147 | ||||
-rw-r--r-- | CommonLibs/sqlite3util.h | 29 |
3 files changed, 180 insertions, 2 deletions
diff --git a/CommonLibs/Makefile.am b/CommonLibs/Makefile.am index 52c965c..6bba671 100644 --- a/CommonLibs/Makefile.am +++ b/CommonLibs/Makefile.am @@ -33,7 +33,8 @@ libcommon_la_SOURCES = \ Timeval.cpp \ Logger.cpp \ URLEncode.cpp \ - Configuration.cpp + Configuration.cpp \ + sqlite3util.cpp noinst_PROGRAMS = \ BitVectorTest \ @@ -58,7 +59,8 @@ noinst_HEADERS = \ URLEncode.h \ Configuration.h \ F16.h \ - Logger.h + Logger.h \ + sqlite3util.h BitVectorTest_SOURCES = BitVectorTest.cpp BitVectorTest_LDADD = libcommon.la diff --git a/CommonLibs/sqlite3util.cpp b/CommonLibs/sqlite3util.cpp new file mode 100644 index 0000000..b898445 --- /dev/null +++ b/CommonLibs/sqlite3util.cpp @@ -0,0 +1,147 @@ +/* +* Written by David A. Burgess, Kestrel Signal Processing, Inc., 2010 +* The author disclaims copyright to this source code. +*/ + + +#include "sqlite3.h" +#include "sqlite3util.h" + +#include <string.h> +#include <unistd.h> +#include <stdio.h> + + +// Wrappers to sqlite operations. +// These will eventually get moved to commonlibs. + +int sqlite3_prepare_statement(sqlite3* DB, sqlite3_stmt **stmt, const char* query) +{ + int prc = sqlite3_prepare_v2(DB,query,strlen(query),stmt,NULL); + if (prc) { + fprintf(stderr,"sqlite3_prepare_v2 failed for \"%s\": %s\n",query,sqlite3_errmsg(DB)); + sqlite3_finalize(*stmt); + } + return prc; +} + +int sqlite3_run_query(sqlite3* DB, sqlite3_stmt *stmt) +{ + int src = SQLITE_BUSY; + while (src==SQLITE_BUSY) { + src = sqlite3_step(stmt); + if (src==SQLITE_BUSY) { + usleep(100000); + } + } + if ((src!=SQLITE_DONE) && (src!=SQLITE_ROW)) { + fprintf(stderr,"sqlite3_run_query failed: %s: %s\n", sqlite3_sql(stmt), sqlite3_errmsg(DB)); + } + return src; +} + + +bool sqlite3_exists(sqlite3* DB, const char *tableName, + const char* keyName, const char* keyData) +{ + size_t stringSize = 100 + strlen(tableName) + strlen(keyName) + strlen(keyData); + char query[stringSize]; + sprintf(query,"SELECT * FROM %s WHERE %s == \"%s\"",tableName,keyName,keyData); + // Prepare the statement. + sqlite3_stmt *stmt; + if (sqlite3_prepare_statement(DB,&stmt,query)) return false; + // Read the result. + int src = sqlite3_run_query(DB,stmt); + sqlite3_finalize(stmt); + // Anything there? + return (src == SQLITE_ROW); +} + + + +bool sqlite3_single_lookup(sqlite3* DB, const char *tableName, + const char* keyName, const char* keyData, + const char* valueName, unsigned &valueData) +{ + size_t stringSize = 100 + strlen(valueName) + strlen(tableName) + strlen(keyName) + strlen(keyData); + char query[stringSize]; + sprintf(query,"SELECT %s FROM %s WHERE %s == \"%s\"",valueName,tableName,keyName,keyData); + // Prepare the statement. + sqlite3_stmt *stmt; + if (sqlite3_prepare_statement(DB,&stmt,query)) return false; + // Read the result. + int src = sqlite3_run_query(DB,stmt); + bool retVal = false; + if (src == SQLITE_ROW) { + valueData = (unsigned)sqlite3_column_int64(stmt,0); + retVal = true; + } + sqlite3_finalize(stmt); + return retVal; +} + + +// This function returns an allocated string that must be free'd by the caller. +bool sqlite3_single_lookup(sqlite3* DB, const char* tableName, + const char* keyName, const char* keyData, + const char* valueName, char* &valueData) +{ + valueData=NULL; + size_t stringSize = 100 + strlen(valueName) + strlen(tableName) + strlen(keyName) + strlen(keyData); + char query[stringSize]; + sprintf(query,"SELECT %s FROM %s WHERE %s == \"%s\"",valueName,tableName,keyName,keyData); + // Prepare the statement. + sqlite3_stmt *stmt; + if (sqlite3_prepare_statement(DB,&stmt,query)) return false; + // Read the result. + int src = sqlite3_run_query(DB,stmt); + bool retVal = false; + if (src == SQLITE_ROW) { + const char* ptr = (const char*)sqlite3_column_text(stmt,0); + if (ptr) valueData = strdup(ptr); + retVal = true; + } + sqlite3_finalize(stmt); + return retVal; +} + + +// This function returns an allocated string that must be free'd by tha caller. +bool sqlite3_single_lookup(sqlite3* DB, const char* tableName, + const char* keyName, unsigned keyData, + const char* valueName, char* &valueData) +{ + valueData=NULL; + size_t stringSize = 100 + strlen(valueName) + strlen(tableName) + strlen(keyName) + 20; + char query[stringSize]; + sprintf(query,"SELECT %s FROM %s WHERE %s == %u",valueName,tableName,keyName,keyData); + // Prepare the statement. + sqlite3_stmt *stmt; + if (sqlite3_prepare_statement(DB,&stmt,query)) return false; + // Read the result. + int src = sqlite3_run_query(DB,stmt); + bool retVal = false; + if (src == SQLITE_ROW) { + const char* ptr = (const char*)sqlite3_column_text(stmt,0); + if (ptr) valueData = strdup(ptr); + retVal = true; + } + sqlite3_finalize(stmt); + return retVal; +} + + + + +bool sqlite3_command(sqlite3* DB, const char* query) +{ + // Prepare the statement. + sqlite3_stmt *stmt; + if (sqlite3_prepare_statement(DB,&stmt,query)) return false; + // Run the query. + int src = sqlite3_run_query(DB,stmt); + return src==SQLITE_DONE; +} + + + diff --git a/CommonLibs/sqlite3util.h b/CommonLibs/sqlite3util.h new file mode 100644 index 0000000..f2b3aa7 --- /dev/null +++ b/CommonLibs/sqlite3util.h @@ -0,0 +1,29 @@ +#ifndef SQLITE3UTIL_H +#define SQLITE3UTIL_H + +#include <sqlite3.h> + +int sqlite3_prepare_statement(sqlite3* DB, sqlite3_stmt **stmt, const char* query); + +int sqlite3_run_query(sqlite3* DB, sqlite3_stmt *stmt); + +bool sqlite3_single_lookup(sqlite3* DB, const char *tableName, + const char* keyName, const char* keyData, + const char* valueName, unsigned &valueData); + +bool sqlite3_single_lookup(sqlite3* DB, const char* tableName, + const char* keyName, const char* keyData, + const char* valueName, char* &valueData); + +// This function returns an allocated string that must be free'd by the caller. +bool sqlite3_single_lookup(sqlite3* DB, const char* tableName, + const char* keyName, unsigned keyData, + const char* valueName, char* &valueData); + +bool sqlite3_exists(sqlite3* DB, const char* tableName, + const char* keyName, const char* keyData); + +/** Run a query, ignoring the result; return true on success. */ +bool sqlite3_command(sqlite3* DB, const char* query); + +#endif |