summaryrefslogtreecommitdiffstats
path: root/friendfinder/util/sqlite_helper.c
diff options
context:
space:
mode:
authorPatrick Hornecker2009-12-21 16:07:36 +0100
committerPatrick Hornecker2009-12-21 16:07:36 +0100
commit3cefadd098bb72a087406ecc09c1c04f1dca0ab5 (patch)
treebbec3ea006ac2abc6bdf52f00ca2b970f27274d9 /friendfinder/util/sqlite_helper.c
parentfew changes (diff)
downloadfriendfinder-3cefadd098bb72a087406ecc09c1c04f1dca0ab5.tar.gz
friendfinder-3cefadd098bb72a087406ecc09c1c04f1dca0ab5.tar.xz
friendfinder-3cefadd098bb72a087406ecc09c1c04f1dca0ab5.zip
file locations reorderd, map in gui...build problems on local machine
Diffstat (limited to 'friendfinder/util/sqlite_helper.c')
-rw-r--r--friendfinder/util/sqlite_helper.c205
1 files changed, 205 insertions, 0 deletions
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 <stdio.h>
+#include <stdarg.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "util/sqlite_helper.h"
+
+#ifdef WINCE
+#include <Evil.h>
+#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;
+}
+