2 * Jim - Sqlite bindings
4 * Copyright 2005 Salvatore Sanfilippo <antirez@invece.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials
15 * provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE JIM TCL PROJECT ``AS IS'' AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
19 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21 * JIM TCL PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
22 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 * The views and conclusions contained in the software and documentation
31 * are those of the authors and should not be interpreted as representing
32 * official policies, either expressed or implied, of the Jim Tcl Project.
40 #include "jimautoconf.h"
42 typedef struct JimSqliteHandle
47 static void JimSqliteDelProc(Jim_Interp
*interp
, void *privData
)
49 JimSqliteHandle
*sh
= privData
;
57 static char *JimSqliteQuoteString(const char *str
, int len
, int *newLenPtr
)
63 for (i
= 0; i
< len
; i
++)
68 d
= buf
= Jim_Alloc(newLen
);
78 static Jim_Obj
*JimSqliteFormatQuery(Jim_Interp
*interp
, Jim_Obj
*fmtObjPtr
,
79 int objc
, Jim_Obj
*const *objv
)
85 fmt
= Jim_GetString(fmtObjPtr
, &fmtLen
);
86 resObjPtr
= Jim_NewStringObj(interp
, "", 0);
91 while (*fmt
!= '%' && fmtLen
) {
95 Jim_AppendString(interp
, resObjPtr
, p
, fmt
- p
);
99 fmtLen
--; /* skip '%' */
102 Jim_FreeNewObj(interp
, resObjPtr
);
103 Jim_SetResultString(interp
, "not enough arguments for all format specifiers", -1);
117 str
= Jim_GetString(objv
[0], &len
);
118 quoted
= JimSqliteQuoteString(str
, len
, &newLen
);
119 Jim_AppendString(interp
, resObjPtr
, quoted
, newLen
);
125 Jim_AppendString(interp
, resObjPtr
, "%", 1);
130 Jim_FreeNewObj(interp
, resObjPtr
);
131 Jim_SetResultFormatted(interp
,
132 "bad field specifier \"%s\", only %%s and %%%% are valid", spec
);
141 /* Calls to [sqlite.open] create commands that are implemented by this
143 static int JimSqliteHandlerCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
145 JimSqliteHandle
*sh
= Jim_CmdPrivData(interp
);
147 static const char * const options
[] = {
148 "close", "query", "lastid", "changes", NULL
151 { OPT_CLOSE
, OPT_QUERY
, OPT_LASTID
, OPT_CHANGES
};
154 Jim_WrongNumArgs(interp
, 1, argv
, "method ?args ...?");
157 if (Jim_GetEnum(interp
, argv
[1], options
, &option
, "Sqlite method", JIM_ERRMSG
) != JIM_OK
)
160 if (option
== OPT_CLOSE
) {
162 Jim_WrongNumArgs(interp
, 2, argv
, "");
165 Jim_DeleteCommand(interp
, Jim_String(argv
[0]));
168 else if (option
== OPT_QUERY
) {
170 Jim_Obj
*objPtr
, *rowsListPtr
;
173 const char *query
, *tail
, **values
, **names
;
177 if (argc
>= 4 && Jim_CompareStringImmediate(interp
, argv
[2], "-null")) {
178 nullstr
= Jim_StrDup(Jim_String(argv
[3]));
183 nullstr
= Jim_StrDup("");
186 Jim_WrongNumArgs(interp
, 2, argv
, "query ?args?");
190 objPtr
= JimSqliteFormatQuery(interp
, argv
[2], argc
- 3, argv
+ 3);
191 if (objPtr
== NULL
) {
195 query
= Jim_String(objPtr
);
196 Jim_IncrRefCount(objPtr
);
197 /* Compile the query into VM code */
198 if (sqlite_compile(sh
->db
, query
, &tail
, &vm
, &errMsg
) != SQLITE_OK
) {
199 Jim_DecrRefCount(interp
, objPtr
);
200 Jim_SetResultString(interp
, errMsg
, -1);
201 sqlite_freemem(errMsg
);
205 Jim_DecrRefCount(interp
, objPtr
); /* query no longer needed. */
206 /* Build a list of rows (that are lists in turn) */
207 rowsListPtr
= Jim_NewListObj(interp
, NULL
, 0);
208 Jim_IncrRefCount(rowsListPtr
);
210 while (sqlite_step(vm
, &columns
, &values
, &names
) == SQLITE_ROW
) {
213 objPtr
= Jim_NewListObj(interp
, NULL
, 0);
214 for (i
= 0; i
< columns
; i
++) {
215 Jim_ListAppendElement(interp
, objPtr
, Jim_NewStringObj(interp
, names
[i
], -1));
216 Jim_ListAppendElement(interp
, objPtr
,
217 Jim_NewStringObj(interp
, values
[i
] != NULL
? values
[i
] : nullstr
, -1));
219 Jim_ListAppendElement(interp
, rowsListPtr
, objPtr
);
224 if (sqlite_finalize(vm
, &errMsg
) != SQLITE_OK
) {
225 Jim_DecrRefCount(interp
, rowsListPtr
);
226 Jim_SetResultString(interp
, errMsg
, -1);
227 sqlite_freemem(errMsg
);
230 Jim_SetResult(interp
, rowsListPtr
);
231 Jim_DecrRefCount(interp
, rowsListPtr
);
233 else if (option
== OPT_LASTID
) {
235 Jim_WrongNumArgs(interp
, 2, argv
, "");
238 Jim_SetResult(interp
, Jim_NewIntObj(interp
, sqlite_last_insert_rowid(sh
->db
)));
241 else if (option
== OPT_CHANGES
) {
243 Jim_WrongNumArgs(interp
, 2, argv
, "");
246 Jim_SetResult(interp
, Jim_NewIntObj(interp
, sqlite_changes(sh
->db
)));
252 static int JimSqliteOpenCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
256 char buf
[60], *errMsg
;
259 Jim_WrongNumArgs(interp
, 1, argv
, "dbname");
262 db
= sqlite_open(Jim_String(argv
[1]), 0, &errMsg
);
264 Jim_SetResultString(interp
, errMsg
, -1);
265 sqlite_freemem(errMsg
);
268 /* Create the file command */
269 sh
= Jim_Alloc(sizeof(*sh
));
271 snprintf(buf
, sizeof(buf
), "sqlite.handle%ld", Jim_GetId(interp
));
272 Jim_CreateCommand(interp
, buf
, JimSqliteHandlerCommand
, sh
, JimSqliteDelProc
);
273 Jim_SetResultString(interp
, buf
, -1);
277 int Jim_sqliteInit(Jim_Interp
*interp
)
279 if (Jim_PackageProvide(interp
, "sqlite", "1.0", JIM_ERRMSG
))
282 Jim_CreateCommand(interp
, "sqlite.open", JimSqliteOpenCommand
, NULL
, NULL
);