diff options
Diffstat (limited to 'database/sqlite/sqlite.c')
| -rw-r--r-- | database/sqlite/sqlite.c | 33 |
1 files changed, 33 insertions, 0 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; |
