summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Sommerseth <dazo@users.sourceforge.net>2008-12-07 21:25:58 +0100
committerDavid Sommerseth <dazo@users.sourceforge.net>2008-12-07 21:25:58 +0100
commit0180c2a6b2163bd2f703aeeb27931b96dc44a11e (patch)
treee3e910f3d910ab542e2b89655d077ff3802e8a98
parent90da20df8560d301d34d65d63d35ca3bc822bd53 (diff)
downloadeurephia-0180c2a6b2163bd2f703aeeb27931b96dc44a11e.tar.gz
eurephia-0180c2a6b2163bd2f703aeeb27931b96dc44a11e.tar.xz
eurephia-0180c2a6b2163bd2f703aeeb27931b96dc44a11e.zip
Extended sqlite.[ch] with sqlite_query_mapped(...)
This functions does almost the same as sqlite_query(...), it takes an SQL statement without the WHERE clause. The where clause is built up based on the fields sent in via the eDBfieldMap struct, containing the fields and values to look for.
-rw-r--r--database/sqlite/sqlite.c33
-rw-r--r--database/sqlite/sqlite.h3
2 files changed, 35 insertions, 1 deletions
diff --git a/database/sqlite/sqlite.c b/database/sqlite/sqlite.c
index dda59ba..7d240a9 100644
--- a/database/sqlite/sqlite.c
+++ b/database/sqlite/sqlite.c
@@ -22,11 +22,14 @@
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
+#include <assert.h>
#include <sqlite3.h>
#include <eurephia_log.h>
#include <eurephia_nullsafe.h>
#include <eurephia_directions.h>
+#include <eurephia_admin_struct.h>
+#include <eurephiadb_mapping.h>
#define SQLITE_C
#include "./sqlite.h"
@@ -237,6 +240,36 @@ dbresult *sqlite_query(eurephiaCTX *ctx, char *fmt, ... ) {
return glob_results;
}
+dbresult *sqlite_query_mapped(eurephiaCTX *ctx, char *sql_start, eDBfieldMap *whmap) {
+ eDBfieldMap *ptr = NULL;
+ char where[8195], *sqlp = NULL;
+ int first;
+
+ assert((ctx != NULL) && (sql_start != NULL) && (whmap != NULL) );
+
+ // Prepare where clause
+ memset(&where, 0, 8195);
+ strcat(where, " (");
+ first = 1;
+
+ // Loop through all fields in the field map, and add properly
+ // formed SQL for a WHERE clause
+ for(ptr = whmap; ptr != NULL; ptr = ptr->next) {
+ // Only add those fields which is not NULL
+ if( (ptr->value != NULL) ) {
+ sqlp = sqlite3_mprintf("%s%s='%q'", (first != 1 ? " AND " : ""),
+ ptr->field_name, ptr->value);
+ strncat(where, sqlp, (8192 - strlen_nullsafe(where)));
+ sqlite3_free(sqlp);
+ first = 0;
+ }
+ }
+ strcat(where, ")"); // End this where clause
+
+ // Send the SQL query to the database and return the result
+ return sqlite_query(ctx, "%s WHERE %s", sql_start, where);
+}
+
// Simple line-by-line result dumper
void sqlite_dump_result(FILE *dmp, dbresult *res) {
_sqlite_tuples *row = NULL, *field = NULL;
diff --git a/database/sqlite/sqlite.h b/database/sqlite/sqlite.h
index 8d221fd..2898c53 100644
--- a/database/sqlite/sqlite.h
+++ b/database/sqlite/sqlite.h
@@ -22,7 +22,7 @@
# define SQLITE_H_
#include <stdarg.h>
-
+#include <eurephiadb_mapping.h>
typedef struct __sqlite_header {
unsigned int fieldid;
@@ -64,6 +64,7 @@ typedef struct __sqlite_dbresult {
#define sqlite_free_results(r) { _sqlite_free_results(r); r = NULL; }
void _sqlite_free_results(dbresult *);
dbresult *sqlite_query(eurephiaCTX *ctx, const char *, ...);
+dbresult *sqlite_query_mapped(eurephiaCTX *, char *, eDBfieldMap *);
char *sqlite_get_value(dbresult *res, int, int);
void sqlite_dump_result(FILE *, dbresult *);
int sqlite_get_numtuples(dbresult *);