aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile.in4
-rw-r--r--README.sqlite12
-rw-r--r--auto.def1
-rw-r--r--jim-sqlite.c283
4 files changed, 6 insertions, 294 deletions
diff --git a/Makefile.in b/Makefile.in
index 37b01db..8ea97cb 100644
--- a/Makefile.in
+++ b/Makefile.in
@@ -157,10 +157,6 @@ tclprefix.so: jim-tclprefix.c
$(CC) $(CFLAGS) $(SHOBJ_CFLAGS) -c -o jim-tclprefix.o $> $^
$(CC) $(CFLAGS) $(LDFLAGS) $(SHOBJ_LDFLAGS) -o $@ jim-tclprefix.o $(SH_LIBJIM) @LDLIBS_tclprefix@
-sqlite.so: jim-sqlite.c
- $(CC) $(CFLAGS) $(SHOBJ_CFLAGS) -c -o jim-sqlite.o $> $^
- $(CC) $(CFLAGS) $(LDFLAGS) $(SHOBJ_LDFLAGS) -o $@ jim-sqlite.o $(SH_LIBJIM) @LDLIBS_sqlite@
-
sqlite3.so: jim-sqlite3.c
$(CC) $(CFLAGS) $(SHOBJ_CFLAGS) -c -o jim-sqlite3.o $> $^
$(CC) $(CFLAGS) $(LDFLAGS) $(SHOBJ_LDFLAGS) -o $@ jim-sqlite3.o $(SH_LIBJIM) @LDLIBS_sqlite3@
diff --git a/README.sqlite b/README.sqlite
index cc4761d..dde7de5 100644
--- a/README.sqlite
+++ b/README.sqlite
@@ -17,12 +17,12 @@ Basic usage
The Sqlite extension exports an Object Based interface for databases. In order
to open a database use:
- set f [sqlite.open dbname]
+ set f [sqlite3.open dbname]
-The [sqlite.open] command returns a db handle, that is a command name that
+The [sqlite3.open] command returns a db handle, that is a command name that
can be used to perform operations on the database. A real example:
- . set db [sqlite.open test.db]
+ . set db [sqlite3.open test.db]
sqlite.handle0
. $db query "SELECT * from tbl1"
{one hello! two 10} {one goodbye two 20}
@@ -101,7 +101,7 @@ For default this extension will use the empty string, but it is possible
to specify a different string for the NULL value.
In the above example there were two rows in the 'tbl1' table. Now
-we can add usign the "sqlite" command line client another one with
+we can add using the "sqlite" command line client another one with
a NULL value:
sqlite> INSERT INTO tbl1 VALUES(NULL,30);
@@ -163,8 +163,8 @@ In-memory DBs are used just like regular databases, just the name used to
open the database is :memory:. That's an example that does not use the
filesystem at all to create and work with the db.
- package require sqlite
- set db [sqlite.open :memory:]
+ package require sqlite3
+ set db [sqlite3.open :memory:]
$db query {CREATE TABLE plays (id, author, title)}
$db query {INSERT INTO plays (id, author, title) VALUES (1, 'Goethe', 'Faust');}
$db query {INSERT INTO plays (id, author, title) VALUES (2, 'Shakespeare', 'Hamlet');}
diff --git a/auto.def b/auto.def
index 5025930..2c62136 100644
--- a/auto.def
+++ b/auto.def
@@ -46,7 +46,6 @@ options {
rlprompt - Tcl wrapper around the readline extension
mk - Interface to Metakit
tclprefix - Support for the tcl::prefix command
- sqlite - Interface to sqlite
sqlite3 - Interface to sqlite3
win32 - Interface to win32
}
diff --git a/jim-sqlite.c b/jim-sqlite.c
deleted file mode 100644
index f37e943..0000000
--- a/jim-sqlite.c
+++ /dev/null
@@ -1,283 +0,0 @@
-/*
- * Jim - Sqlite bindings
- *
- * Copyright 2005 Salvatore Sanfilippo <antirez@invece.org>
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above
- * copyright notice, this list of conditions and the following
- * disclaimer in the documentation and/or other materials
- * provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE JIM TCL PROJECT ``AS IS'' AND ANY
- * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
- * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
- * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
- * JIM TCL PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
- * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
- * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
- * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- * The views and conclusions contained in the software and documentation
- * are those of the authors and should not be interpreted as representing
- * official policies, either expressed or implied, of the Jim Tcl Project.
- */
-
-#include <stdio.h>
-#include <string.h>
-#include <sqlite.h>
-
-#include <jim.h>
-
-typedef struct JimSqliteHandle
-{
- sqlite *db;
-} JimSqliteHandle;
-
-static void JimSqliteDelProc(Jim_Interp *interp, void *privData)
-{
- JimSqliteHandle *sh = privData;
-
- JIM_NOTUSED(interp);
-
- sqlite_close(sh->db);
- Jim_Free(sh);
-}
-
-static char *JimSqliteQuoteString(const char *str, int len, int *newLenPtr)
-{
- int i, newLen, c = 0;
- const char *s;
- char *d, *buf;
-
- for (i = 0; i < len; i++)
- if (str[i] == '\'')
- c++;
- newLen = len + c;
- s = str;
- d = buf = Jim_Alloc(newLen);
- while (len--) {
- if (*s == '\'')
- *d++ = '\'';
- *d++ = *s++;
- }
- *newLenPtr = newLen;
- return buf;
-}
-
-static Jim_Obj *JimSqliteFormatQuery(Jim_Interp *interp, Jim_Obj *fmtObjPtr,
- int objc, Jim_Obj *const *objv)
-{
- const char *fmt;
- int fmtLen;
- Jim_Obj *resObjPtr;
-
- fmt = Jim_GetString(fmtObjPtr, &fmtLen);
- resObjPtr = Jim_NewStringObj(interp, "", 0);
- while (fmtLen) {
- const char *p = fmt;
- char spec[2];
-
- while (*fmt != '%' && fmtLen) {
- fmt++;
- fmtLen--;
- }
- Jim_AppendString(interp, resObjPtr, p, fmt - p);
- if (fmtLen == 0)
- break;
- fmt++;
- fmtLen--; /* skip '%' */
- if (*fmt != '%') {
- if (objc == 0) {
- Jim_FreeNewObj(interp, resObjPtr);
- Jim_SetResultString(interp, "not enough arguments for all format specifiers", -1);
- return NULL;
- }
- else {
- objc--;
- }
- }
- switch (*fmt) {
- case 's':
- {
- const char *str;
- char *quoted;
- int len, newLen;
-
- str = Jim_GetString(objv[0], &len);
- quoted = JimSqliteQuoteString(str, len, &newLen);
- Jim_AppendString(interp, resObjPtr, quoted, newLen);
- Jim_Free(quoted);
- }
- objv++;
- break;
- case '%':
- Jim_AppendString(interp, resObjPtr, "%", 1);
- break;
- default:
- spec[1] = *fmt;
- spec[2] = '\0';
- Jim_FreeNewObj(interp, resObjPtr);
- Jim_SetResultFormatted(interp,
- "bad field specifier \"%s\", only %%s and %%%% are valid", spec);
- return NULL;
- }
- fmt++;
- fmtLen--;
- }
- return resObjPtr;
-}
-
-/* Calls to [sqlite.open] create commands that are implemented by this
- * C command. */
-static int JimSqliteHandlerCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
-{
- JimSqliteHandle *sh = Jim_CmdPrivData(interp);
- int option;
- static const char * const options[] = {
- "close", "query", "lastid", "changes", NULL
- };
- enum
- { OPT_CLOSE, OPT_QUERY, OPT_LASTID, OPT_CHANGES };
-
- if (argc < 2) {
- Jim_WrongNumArgs(interp, 1, argv, "method ?args ...?");
- return JIM_ERR;
- }
- if (Jim_GetEnum(interp, argv[1], options, &option, "Sqlite method", JIM_ERRMSG) != JIM_OK)
- return JIM_ERR;
- /* CLOSE */
- if (option == OPT_CLOSE) {
- if (argc != 2) {
- Jim_WrongNumArgs(interp, 2, argv, "");
- return JIM_ERR;
- }
- Jim_DeleteCommand(interp, Jim_String(argv[0]));
- return JIM_OK;
- }
- else if (option == OPT_QUERY) {
- /* QUERY */
- Jim_Obj *objPtr, *rowsListPtr;
- sqlite_vm *vm;
- char *errMsg;
- const char *query, *tail, **values, **names;
- int columns, rows;
- char *nullstr;
-
- if (argc >= 4 && Jim_CompareStringImmediate(interp, argv[2], "-null")) {
- nullstr = Jim_StrDup(Jim_String(argv[3]));
- argv += 2;
- argc -= 2;
- }
- else {
- nullstr = Jim_StrDup("");
- }
- if (argc < 3) {
- Jim_WrongNumArgs(interp, 2, argv, "query ?args?");
- Jim_Free(nullstr);
- return JIM_ERR;
- }
- objPtr = JimSqliteFormatQuery(interp, argv[2], argc - 3, argv + 3);
- if (objPtr == NULL) {
- Jim_Free(nullstr);
- return JIM_ERR;
- }
- query = Jim_String(objPtr);
- Jim_IncrRefCount(objPtr);
- /* Compile the query into VM code */
- if (sqlite_compile(sh->db, query, &tail, &vm, &errMsg) != SQLITE_OK) {
- Jim_DecrRefCount(interp, objPtr);
- Jim_SetResultString(interp, errMsg, -1);
- sqlite_freemem(errMsg);
- Jim_Free(nullstr);
- return JIM_ERR;
- }
- Jim_DecrRefCount(interp, objPtr); /* query no longer needed. */
- /* Build a list of rows (that are lists in turn) */
- rowsListPtr = Jim_NewListObj(interp, NULL, 0);
- Jim_IncrRefCount(rowsListPtr);
- rows = 0;
- while (sqlite_step(vm, &columns, &values, &names) == SQLITE_ROW) {
- int i;
-
- objPtr = Jim_NewListObj(interp, NULL, 0);
- for (i = 0; i < columns; i++) {
- Jim_ListAppendElement(interp, objPtr, Jim_NewStringObj(interp, names[i], -1));
- Jim_ListAppendElement(interp, objPtr,
- Jim_NewStringObj(interp, values[i] != NULL ? values[i] : nullstr, -1));
- }
- Jim_ListAppendElement(interp, rowsListPtr, objPtr);
- rows++;
- }
- /* Finalize */
- Jim_Free(nullstr);
- if (sqlite_finalize(vm, &errMsg) != SQLITE_OK) {
- Jim_DecrRefCount(interp, rowsListPtr);
- Jim_SetResultString(interp, errMsg, -1);
- sqlite_freemem(errMsg);
- return JIM_ERR;
- }
- Jim_SetResult(interp, rowsListPtr);
- Jim_DecrRefCount(interp, rowsListPtr);
- }
- else if (option == OPT_LASTID) {
- if (argc != 2) {
- Jim_WrongNumArgs(interp, 2, argv, "");
- return JIM_ERR;
- }
- Jim_SetResult(interp, Jim_NewIntObj(interp, sqlite_last_insert_rowid(sh->db)));
- return JIM_OK;
- }
- else if (option == OPT_CHANGES) {
- if (argc != 2) {
- Jim_WrongNumArgs(interp, 2, argv, "");
- return JIM_ERR;
- }
- Jim_SetResult(interp, Jim_NewIntObj(interp, sqlite_changes(sh->db)));
- return JIM_OK;
- }
- return JIM_OK;
-}
-
-static int JimSqliteOpenCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
-{
- sqlite *db;
- JimSqliteHandle *sh;
- char buf[60], *errMsg;
-
- if (argc != 2) {
- Jim_WrongNumArgs(interp, 1, argv, "dbname");
- return JIM_ERR;
- }
- db = sqlite_open(Jim_String(argv[1]), 0, &errMsg);
- if (db == NULL) {
- Jim_SetResultString(interp, errMsg, -1);
- sqlite_freemem(errMsg);
- return JIM_ERR;
- }
- /* Create the file command */
- sh = Jim_Alloc(sizeof(*sh));
- sh->db = db;
- snprintf(buf, sizeof(buf), "sqlite.handle%ld", Jim_GetId(interp));
- Jim_CreateCommand(interp, buf, JimSqliteHandlerCommand, sh, JimSqliteDelProc);
- Jim_SetResultString(interp, buf, -1);
- return JIM_OK;
-}
-
-int Jim_sqliteInit(Jim_Interp *interp)
-{
- if (Jim_PackageProvide(interp, "sqlite", "1.0", JIM_ERRMSG))
- return JIM_ERR;
-
- Jim_CreateCommand(interp, "sqlite.open", JimSqliteOpenCommand, NULL, NULL);
- return JIM_OK;
-}