From 3cefadd098bb72a087406ecc09c1c04f1dca0ab5 Mon Sep 17 00:00:00 2001 From: Patrick Hornecker Date: Mon, 21 Dec 2009 16:07:36 +0100 Subject: file locations reorderd, map in gui...build problems on local machine --- friendfinder/util/sqlite_helper.c | 205 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 205 insertions(+) create mode 100644 friendfinder/util/sqlite_helper.c (limited to 'friendfinder/util/sqlite_helper.c') diff --git a/friendfinder/util/sqlite_helper.c b/friendfinder/util/sqlite_helper.c new file mode 100644 index 0000000..528d2f0 --- /dev/null +++ b/friendfinder/util/sqlite_helper.c @@ -0,0 +1,205 @@ + +#include +#include +#include +#include + +#include "util/sqlite_helper.h" + +#ifdef WINCE +#include +#endif + + +int sql_result_step(struct sql_result *r) +{ + int ret; + if(r->stmt == NULL) + return -1; + + ret = sqlite3_step(r->stmt); + if(ret != SQLITE_ROW) + { + sqlite3_finalize(r->stmt); + r->stmt = NULL; + return -1; + } + return 0; +} + +void sql_result_destroy(struct sql_result *res) +{ + sqlite3_finalize(res->stmt); + free(res); +} + +static struct sql_result * +sql_result_create(sqlite3 *db, sqlite3_stmt *stmt) +{ + struct sql_result *result = malloc(sizeof(struct sql_result)); + result->stmt = stmt; + result->columnCount = sqlite3_column_count(stmt); + + /* + result->cols = (char **)malloc(result->columnCount * sizeof(char *)); + result->columnTypes = (int *)malloc(result->columnCount * sizeof(int)); + for(i = 0; i < result->columnCount; i++) + { + result->cols[i] = (char *)sqlite3_column_name(stmt, i); + result->columnTypes[i] = sqlite3_column_type(stmt, i); + } + */ + return result; +} + +static struct sql_result *_sql_query(sqlite3 *db, char *sql, int *ret) +{ + const char *tail; + sqlite3_stmt *stmt; + + *ret = sqlite3_prepare_v2(db, sql, -1, &stmt, &tail); + *ret = sqlite3_step(stmt); + switch(*ret) + { + case SQLITE_DONE: + sqlite3_finalize(stmt); + return NULL; + case SQLITE_ROW: + return sql_result_create(db, stmt); + default: + printf("sql_query(%i): %s\n%s \n", *ret, sql, sqlite3_errmsg(db)); + return NULL; + } +} + +static int sql_query_smpl(sqlite3 *db, char *sql) +{ + int ret; + // char *errmsg; + + const char *tail; + sqlite3_stmt *stmt; + + ret = sqlite3_prepare_v2(db, sql, -1, &stmt, &tail); + if(ret != 0) + printf("ret %i\n", ret); + ret = sqlite3_step(stmt); + switch(ret) + { + case SQLITE_DONE: + sqlite3_finalize(stmt); + return sqlite3_last_insert_rowid(db); + case SQLITE_ROW: + printf("not a simple query %s\nreturned row", sql); + return 0; + default: + sqlite3_finalize(stmt); + printf("sql_query(%i): %s\n%s \n", ret, sql, sqlite3_errmsg(db)); + return -1; + } + + /* + ret = sqlite3_exec(osm->db, sql, NULL, NULL, &errmsg); + if(ret != SQLITE_OK) + { + printf("osm_query_smpl(%i): %s\n", ret, sql); + printf("err mesg %s\n", errmsg); + return -1; + } + + return 0; + */ +} + +#ifdef WINCE +static inline void unix2win_path(char *str) +{ + while(*str) + { + if(*str == '/') + *str = '\\'; + str++; + } +} + +char *wince_full_path(char *file) +{ + char pwd[100]; + char dirbuff[512]; + getcwd(pwd, 100); + sprintf(dirbuff, "%s/%s", pwd, file); + unix2win_path(dirbuff); + return strdup(dirbuff); +} +#endif + + +sqlite3 *sql_open(char *filename) +{ + sqlite3 *db; + char *file; +#ifdef WINCE + file = wince_full_path(filename); +#else + file = filename; +#endif + if(sqlite3_open(file, &db)) + { + fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db)); +#ifdef WINCE + free(file); +#endif + return NULL; + } +#ifdef WINCE + free(file); +#endif + return db; +} + +int sql_exec(sqlite3 *db, char *msgfmt, ...) +{ + char sql[512]; + va_list args; + int ret; + + va_start(args, msgfmt); + ret = vsprintf(sql, msgfmt, args); + va_end(args); + if(ret < 0) + return ret; + + return sql_query_smpl(db, sql); +} + + +struct sql_result *sql_query(sqlite3 *db, int *error, char *msgfmt, ...) +{ + char sql[512]; + va_list args; + int ret; + struct sql_result *res; + + va_start(args, msgfmt); + ret = vsprintf(sql, msgfmt, args); + va_end(args); + + fprintf(stdout, "%s\n", sql); + + if(ret < 0) + { + if(error) + *error = ret; + return NULL; + } + + res = _sql_query(db, sql, &ret); + if(res == NULL) + { + if(error) + *error = ret; + return NULL; + } + return res; +} + -- cgit v1.2.3-55-g7522