summaryrefslogtreecommitdiffstats
path: root/friendfinder/util/sqlite_helper.c
blob: 528d2f0ba04bbef540350c8f35c824c08e60810a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
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;
}