Missing parens in non-utf8 version of utf8_strlen
[jimtcl.git] / jim-sqlite.c
blob122b548430b8f873b48f4862d2c9fa1f2c6ab529
2 /* Jim - Sqlite bindings
3 * Copyright 2005 Salvatore Sanfilippo <antirez@invece.org>
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * A copy of the license is also included in the source distribution
12 * of Jim, as a TXT file name called LICENSE.
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
21 #include <stdio.h>
22 #include <string.h>
23 #include <sqlite.h>
25 #include "jim.h"
26 #include "jimautoconf.h"
28 typedef struct JimSqliteHandle
30 sqlite *db;
31 } JimSqliteHandle;
33 static void JimSqliteDelProc(Jim_Interp *interp, void *privData)
35 JimSqliteHandle *sh = privData;
37 JIM_NOTUSED(interp);
39 sqlite_close(sh->db);
40 Jim_Free(sh);
43 static char *JimSqliteQuoteString(const char *str, int len, int *newLenPtr)
45 int i, newLen, c = 0;
46 const char *s;
47 char *d, *buf;
49 for (i = 0; i < len; i++)
50 if (str[i] == '\'')
51 c++;
52 newLen = len + c;
53 s = str;
54 d = buf = Jim_Alloc(newLen);
55 while (len--) {
56 if (*s == '\'')
57 *d++ = '\'';
58 *d++ = *s++;
60 *newLenPtr = newLen;
61 return buf;
64 static Jim_Obj *JimSqliteFormatQuery(Jim_Interp *interp, Jim_Obj *fmtObjPtr,
65 int objc, Jim_Obj *const *objv)
67 const char *fmt;
68 int fmtLen;
69 Jim_Obj *resObjPtr;
71 fmt = Jim_GetString(fmtObjPtr, &fmtLen);
72 resObjPtr = Jim_NewStringObj(interp, "", 0);
73 while (fmtLen) {
74 const char *p = fmt;
75 char spec[2];
77 while (*fmt != '%' && fmtLen) {
78 fmt++;
79 fmtLen--;
81 Jim_AppendString(interp, resObjPtr, p, fmt - p);
82 if (fmtLen == 0)
83 break;
84 fmt++;
85 fmtLen--; /* skip '%' */
86 if (*fmt != '%') {
87 if (objc == 0) {
88 Jim_FreeNewObj(interp, resObjPtr);
89 Jim_SetResultString(interp, "not enough arguments for all format specifiers", -1);
90 return NULL;
92 else {
93 objc--;
96 switch (*fmt) {
97 case 's':
99 const char *str;
100 char *quoted;
101 int len, newLen;
103 str = Jim_GetString(objv[0], &len);
104 quoted = JimSqliteQuoteString(str, len, &newLen);
105 Jim_AppendString(interp, resObjPtr, quoted, newLen);
106 Jim_Free(quoted);
108 objv++;
109 break;
110 case '%':
111 Jim_AppendString(interp, resObjPtr, "%", 1);
112 break;
113 default:
114 spec[1] = *fmt;
115 spec[2] = '\0';
116 Jim_FreeNewObj(interp, resObjPtr);
117 Jim_SetResultFormatted(interp,
118 "bad field specifier \"%s\", only %%s and %%%% are valid", spec);
119 return NULL;
121 fmt++;
122 fmtLen--;
124 return resObjPtr;
127 /* Calls to [sqlite.open] create commands that are implemented by this
128 * C command. */
129 static int JimSqliteHandlerCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
131 JimSqliteHandle *sh = Jim_CmdPrivData(interp);
132 int option;
133 static const char * const options[] = {
134 "close", "query", "lastid", "changes", NULL
136 enum
137 { OPT_CLOSE, OPT_QUERY, OPT_LASTID, OPT_CHANGES };
139 if (argc < 2) {
140 Jim_WrongNumArgs(interp, 1, argv, "method ?args ...?");
141 return JIM_ERR;
143 if (Jim_GetEnum(interp, argv[1], options, &option, "Sqlite method", JIM_ERRMSG) != JIM_OK)
144 return JIM_ERR;
145 /* CLOSE */
146 if (option == OPT_CLOSE) {
147 if (argc != 2) {
148 Jim_WrongNumArgs(interp, 2, argv, "");
149 return JIM_ERR;
151 Jim_DeleteCommand(interp, Jim_String(argv[0]));
152 return JIM_OK;
154 else if (option == OPT_QUERY) {
155 /* QUERY */
156 Jim_Obj *objPtr, *rowsListPtr;
157 sqlite_vm *vm;
158 char *errMsg;
159 const char *query, *tail, **values, **names;
160 int columns, rows;
161 char *nullstr;
163 if (argc >= 4 && Jim_CompareStringImmediate(interp, argv[2], "-null")) {
164 nullstr = Jim_StrDup(Jim_String(argv[3]));
165 argv += 2;
166 argc -= 2;
168 else {
169 nullstr = Jim_StrDup("");
171 if (argc < 3) {
172 Jim_WrongNumArgs(interp, 2, argv, "query ?args?");
173 Jim_Free(nullstr);
174 return JIM_ERR;
176 objPtr = JimSqliteFormatQuery(interp, argv[2], argc - 3, argv + 3);
177 if (objPtr == NULL) {
178 Jim_Free(nullstr);
179 return JIM_ERR;
181 query = Jim_String(objPtr);
182 Jim_IncrRefCount(objPtr);
183 /* Compile the query into VM code */
184 if (sqlite_compile(sh->db, query, &tail, &vm, &errMsg) != SQLITE_OK) {
185 Jim_DecrRefCount(interp, objPtr);
186 Jim_SetResultString(interp, errMsg, -1);
187 sqlite_freemem(errMsg);
188 Jim_Free(nullstr);
189 return JIM_ERR;
191 Jim_DecrRefCount(interp, objPtr); /* query no longer needed. */
192 /* Build a list of rows (that are lists in turn) */
193 rowsListPtr = Jim_NewListObj(interp, NULL, 0);
194 Jim_IncrRefCount(rowsListPtr);
195 rows = 0;
196 while (sqlite_step(vm, &columns, &values, &names) == SQLITE_ROW) {
197 int i;
199 objPtr = Jim_NewListObj(interp, NULL, 0);
200 for (i = 0; i < columns; i++) {
201 Jim_ListAppendElement(interp, objPtr, Jim_NewStringObj(interp, names[i], -1));
202 Jim_ListAppendElement(interp, objPtr,
203 Jim_NewStringObj(interp, values[i] != NULL ? values[i] : nullstr, -1));
205 Jim_ListAppendElement(interp, rowsListPtr, objPtr);
206 rows++;
208 /* Finalize */
209 Jim_Free(nullstr);
210 if (sqlite_finalize(vm, &errMsg) != SQLITE_OK) {
211 Jim_DecrRefCount(interp, rowsListPtr);
212 Jim_SetResultString(interp, errMsg, -1);
213 sqlite_freemem(errMsg);
214 return JIM_ERR;
216 Jim_SetResult(interp, rowsListPtr);
217 Jim_DecrRefCount(interp, rowsListPtr);
219 else if (option == OPT_LASTID) {
220 if (argc != 2) {
221 Jim_WrongNumArgs(interp, 2, argv, "");
222 return JIM_ERR;
224 Jim_SetResult(interp, Jim_NewIntObj(interp, sqlite_last_insert_rowid(sh->db)));
225 return JIM_OK;
227 else if (option == OPT_CHANGES) {
228 if (argc != 2) {
229 Jim_WrongNumArgs(interp, 2, argv, "");
230 return JIM_ERR;
232 Jim_SetResult(interp, Jim_NewIntObj(interp, sqlite_changes(sh->db)));
233 return JIM_OK;
235 return JIM_OK;
238 static int JimSqliteOpenCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
240 sqlite *db;
241 JimSqliteHandle *sh;
242 char buf[60], *errMsg;
244 if (argc != 2) {
245 Jim_WrongNumArgs(interp, 1, argv, "dbname");
246 return JIM_ERR;
248 db = sqlite_open(Jim_String(argv[1]), 0, &errMsg);
249 if (db == NULL) {
250 Jim_SetResultString(interp, errMsg, -1);
251 sqlite_freemem(errMsg);
252 return JIM_ERR;
254 /* Create the file command */
255 sh = Jim_Alloc(sizeof(*sh));
256 sh->db = db;
257 snprintf(buf, sizeof(buf), "sqlite.handle%ld", Jim_GetId(interp));
258 Jim_CreateCommand(interp, buf, JimSqliteHandlerCommand, sh, JimSqliteDelProc);
259 Jim_SetResultString(interp, buf, -1);
260 return JIM_OK;
263 int Jim_sqliteInit(Jim_Interp *interp)
265 if (Jim_PackageProvide(interp, "sqlite", "1.0", JIM_ERRMSG))
266 return JIM_ERR;
268 Jim_CreateCommand(interp, "sqlite.open", JimSqliteOpenCommand, NULL, NULL);
269 return JIM_OK;