forked from Shmuma/sqlite3-mt4-wrapper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsqlite3_wrapper.c
210 lines (160 loc) · 4.21 KB
/
sqlite3_wrapper.c
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
206
207
208
209
210
#include "sqlite3.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define WINVER 0x0501
#include <windef.h>
#include <windows.h>
#include <shlobj.h>
#include <shlwapi.h>
#define APPDATA_PATHS 1
static char buf[1024];
struct query_result {
sqlite3 *s;
sqlite3_stmt *stmt;
};
/* We assume that given file name is relative to MT installation directory. We
* obtain this path prefix by query process's module name. */
static char* build_db_fname (const char* db)
{
#if APPDATA_PATHS
TCHAR buf[MAX_PATH];
HRESULT res;
res = SHGetFolderPathAndSubDir (0, CSIDL_APPDATA, NULL, 0, "MT-Sqlite", buf);
if (res != S_OK) {
SHGetFolderPath (0, CSIDL_APPDATA, NULL, 0, buf);
PathAppend (buf, "MT-Sqlite");
CreateDirectory (buf, NULL);
}
PathAppend (buf, db);
return strdup (buf);
#else
unsigned int len, s;
char* res;
len = GetModuleFileNameA (NULL, buf, sizeof (buf));
if (len <= 0)
return NULL;
while (len > 0 && buf[len-1] != '\\')
len--;
if (!len)
return NULL;
buf[len-1] = 0;
s = len + 1 + strlen (db);
res = malloc (s);
if (!res)
return NULL;
snprintf (res, s, "%s\\%s", buf, db);
return res;
#endif
}
int __stdcall sqlite_exec (const char *db, const char *sql)
{
sqlite3 *s;
int res;
char* name = build_db_fname (db);
if (!name)
return -1;
res = sqlite3_open (name, &s);
free (name);
if (res != SQLITE_OK)
return res;
res = sqlite3_exec (s, sql, NULL, NULL, NULL);
if (res != SQLITE_OK) {
sqlite3_close (s);
return res;
}
sqlite3_close (s);
return SQLITE_OK;
}
/*
* return 1 if table exists in database, 0 oterwise. -ERROR returned on error.
*/
int __stdcall sqlite_table_exists (const char *db, const char *table)
{
sqlite3 *s;
sqlite3_stmt *stmt;
char buf[256];
int res, exists;
char *name = build_db_fname (db);
if (!name)
return -1;
res = sqlite3_open (name, &s);
free (name);
if (res != SQLITE_OK)
return -res;
sprintf (buf, "select count(*) from sqlite_master where type='table' and name='%s'", table);
res = sqlite3_prepare (s, buf, sizeof (buf), &stmt, NULL);
if (res != SQLITE_OK) {
sqlite3_close (s);
return -res;
}
res = sqlite3_step (stmt);
if (res != SQLITE_ROW) {
sqlite3_finalize (stmt);
sqlite3_close (s);
return -res;
}
exists = sqlite3_column_int (stmt, 0);
sqlite3_finalize (stmt);
sqlite3_close (s);
return exists > 0 ? 1 : 0;
}
/*
* Perform query and pack results in internal structure. Routine returns amount of data fetched and
* integer handle which can be used to sqlite_get_data. On error, return -SQLITE_ERROR.
*/
int __stdcall sqlite_query (const char *db, const char *sql, int* cols)
{
sqlite3 *s;
sqlite3_stmt *stmt;
int res;
struct query_result *result;
char* name = build_db_fname (db);
if (!name)
return -1;
res = sqlite3_open (name, &s);
free (name);
if (res != SQLITE_OK)
return 0;
res = sqlite3_prepare (s, sql, strlen (sql), &stmt, NULL);
if (res != SQLITE_OK) {
sqlite3_close (s);
return 0;
}
result = (struct query_result*)malloc (sizeof (struct query_result));
result->s = s;
result->stmt = stmt;
*cols = sqlite3_column_count (stmt);
return (int)result;
}
/*
* Return 1 if next row fetched, 0 if end of resultset reached
*/
int __stdcall sqlite_next_row (int handle)
{
struct query_result *res = (struct query_result*)handle;
int ret;
if (!res)
return 0;
ret = sqlite3_step (res->stmt);
return ret == SQLITE_ROW ? 1 : 0;
}
const char* __stdcall sqlite_get_col (int handle, int col)
{
struct query_result *data = (struct query_result*)handle;
if (!data)
return NULL;
return sqlite3_column_text (data->stmt, col);
}
int __stdcall sqlite_free_query (int handle)
{
struct query_result *data = (struct query_result*)handle;
if (!data)
return 0;
if (data->stmt)
sqlite3_finalize (data->stmt);
if (data->s)
sqlite3_close (data->s);
free (data);
return 1;
}