diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/sqlite.h.in | 3 | ||||
-rw-r--r-- | src/sqliteInt.h | 3 | ||||
-rw-r--r-- | src/tclsqlite.c | 8 | ||||
-rw-r--r-- | src/test1.c | 283 |
4 files changed, 293 insertions, 4 deletions
diff --git a/src/sqlite.h.in b/src/sqlite.h.in index 425c293c8..b606dae83 100644 --- a/src/sqlite.h.in +++ b/src/sqlite.h.in @@ -24,7 +24,7 @@ ** This header file defines the interface that the sqlite library ** presents to client programs. ** -** @(#) $Id: sqlite.h.in,v 1.12 2001/04/06 16:13:43 drh Exp $ +** @(#) $Id: sqlite.h.in,v 1.13 2001/04/07 15:24:33 drh Exp $ */ #ifndef _SQLITE_H_ #define _SQLITE_H_ @@ -342,5 +342,4 @@ int sqlite_get_table_vprintf( va_list ap /* Arguments to the format string */ ); - #endif /* _SQLITE_H_ */ diff --git a/src/sqliteInt.h b/src/sqliteInt.h index 04de02079..c78ba2174 100644 --- a/src/sqliteInt.h +++ b/src/sqliteInt.h @@ -23,7 +23,7 @@ ************************************************************************* ** Internal interface definitions for SQLite. ** -** @(#) $Id: sqliteInt.h,v 1.39 2001/04/04 21:10:19 drh Exp $ +** @(#) $Id: sqliteInt.h,v 1.40 2001/04/07 15:24:33 drh Exp $ */ #include "sqlite.h" #include "dbbe.h" @@ -432,3 +432,4 @@ char *sqliteDbbeNameToFile(const char*,const char*,const char*); void sqliteBeginTransaction(Parse*); void sqliteCommitTransaction(Parse*); void sqliteRollbackTransaction(Parse*); +char *sqlite_mprintf(const char *, ...); diff --git a/src/tclsqlite.c b/src/tclsqlite.c index 57df83b86..42c8eada5 100644 --- a/src/tclsqlite.c +++ b/src/tclsqlite.c @@ -23,7 +23,7 @@ ************************************************************************* ** A TCL Interface to SQLite ** -** $Id: tclsqlite.c,v 1.16 2001/04/06 16:13:43 drh Exp $ +** $Id: tclsqlite.c,v 1.17 2001/04/07 15:24:33 drh Exp $ */ #ifndef NO_TCL /* Omit this whole file if TCL is unavailable */ @@ -507,6 +507,12 @@ int TCLSH_MAIN(int argc, char **argv){ Tcl_FindExecutable(argv[0]); interp = Tcl_CreateInterp(); Sqlite_Init(interp); +#ifdef SQLITE_TEST1 + { + extern int Sqlitetest1_Init(Tcl_Interp*); + Sqlitetest1_Init(interp); + } +#endif if( argc>=2 ){ int i; Tcl_SetVar(interp,"argv0",argv[1],TCL_GLOBAL_ONLY); diff --git a/src/test1.c b/src/test1.c new file mode 100644 index 000000000..e976da0d8 --- /dev/null +++ b/src/test1.c @@ -0,0 +1,283 @@ +/* +** Copyright (c) 2001 D. Richard Hipp +** +** This program is free software; you can redistribute it and/or +** modify it under the terms of the GNU General Public +** License as published by the Free Software Foundation; either +** version 2 of the License, or (at your option) any later version. +** +** This program is distributed in the hope that it will be useful, +** but WITHOUT ANY WARRANTY; without even the implied warranty of +** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +** General Public License for more details. +** +** You should have received a copy of the GNU General Public +** License along with this library; if not, write to the +** Free Software Foundation, Inc., 59 Temple Place - Suite 330, +** Boston, MA 02111-1307, USA. +** +** Author contact information: +** drh@hwaci.com +** http://www.hwaci.com/drh/ +** +************************************************************************* +** Code for testing the printf() interface to SQLite. This code +** is not included in the SQLite library. It is used for automated +** testing of the SQLite library. +** +** $Id: test1.c,v 1.1 2001/04/07 15:24:33 drh Exp $ +*/ +#include "sqliteInt.h" +#include "tcl.h" +#include <stdlib.h> +#include <string.h> + +/* +** Usage: sqlite_open filename +** +** Returns: The name of an open database. +*/ +static int sqlite_test_open( + void *NotUsed, + Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ + int argc, /* Number of arguments */ + char **argv /* Text of each argument */ +){ + sqlite *db; + char *zErr = 0; + char zBuf[100]; + if( argc!=2 ){ + Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], + " FILENAME\"", 0); + return TCL_ERROR; + } + db = sqlite_open(argv[1], 0666, &zErr); + if( db==0 ){ + Tcl_AppendResult(interp, zErr, 0); + free(zErr); + return TCL_ERROR; + } + sprintf(zBuf,"%d",(int)db); + Tcl_AppendResult(interp, zBuf, 0); + return TCL_OK; +} + +/* +** The callback routine for sqlite_exec_printf(). +*/ +static int exec_printf_cb(void *pArg, int argc, char **argv, char **name){ + Tcl_DString *str = (Tcl_DString*)pArg; + int i; + + if( Tcl_DStringLength(str)==0 ){ + for(i=0; i<argc; i++){ + Tcl_DStringAppendElement(str, name[i] ? name[i] : "NULL"); + } + } + for(i=0; i<argc; i++){ + Tcl_DStringAppendElement(str, argv[i] ? argv[i] : "NULL"); + } + return 0; +} + +/* +** Usage: sqlite_exec_printf DB FORMAT STRING +** +** Invoke the sqlite_exec_printf() interface using the open database +** DB. The SQL is the string FORMAT. The format string should contain +** one %s or %q. STRING is the value inserted into %s or %q. +*/ +static int test_exec_printf( + void *NotUsed, + Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ + int argc, /* Number of arguments */ + char **argv /* Text of each argument */ +){ + sqlite *db; + Tcl_DString str; + int rc; + char *zErr = 0; + char zBuf[30]; + if( argc!=4 ){ + Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], + " DB FORMAT STRING", 0); + return TCL_ERROR; + } + db = (sqlite*)atoi(argv[1]); + Tcl_DStringInit(&str); + rc = sqlite_exec_printf(db, argv[2], exec_printf_cb, &str, &zErr, argv[3]); + sprintf(zBuf, "%d", rc); + Tcl_AppendElement(interp, zBuf); + Tcl_AppendElement(interp, rc==SQLITE_OK ? Tcl_DStringValue(&str) : zErr); + Tcl_DStringFree(&str); + if( zErr ) free(zErr); + return TCL_OK; +} + +/* +** Usage: sqlite_get_table_printf DB FORMAT STRING +** +** Invoke the sqlite_get_table_printf() interface using the open database +** DB. The SQL is the string FORMAT. The format string should contain +** one %s or %q. STRING is the value inserted into %s or %q. +*/ +static int test_get_table_printf( + void *NotUsed, + Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ + int argc, /* Number of arguments */ + char **argv /* Text of each argument */ +){ + sqlite *db; + Tcl_DString str; + int rc; + char *zErr = 0; + int nRow, nCol; + char **aResult; + int i; + char zBuf[30]; + if( argc!=4 ){ + Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], + " DB FORMAT STRING", 0); + return TCL_ERROR; + } + db = (sqlite*)atoi(argv[1]); + Tcl_DStringInit(&str); + rc = sqlite_get_table_printf(db, argv[2], &aResult, &nRow, &nCol, + &zErr, argv[3]); + sprintf(zBuf, "%d", rc); + Tcl_AppendElement(interp, zBuf); + if( rc==SQLITE_OK ){ + sprintf(zBuf, "%d", nRow); + Tcl_AppendElement(interp, zBuf); + sprintf(zBuf, "%d", nCol); + Tcl_AppendElement(interp, zBuf); + for(i=0; i<(nRow+1)*nCol; i++){ + Tcl_AppendElement(interp, aResult[i] ? aResult[i] : "NULL"); + } + }else{ + Tcl_AppendElement(interp, zErr); + } + sqlite_free_table(aResult); + if( zErr ) free(zErr); + return TCL_OK; +} + +/* +** Usage: sqlite_close DB +** +** Closes the database opened by sqlite_open. +*/ +static int sqlite_test_close( + void *NotUsed, + Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ + int argc, /* Number of arguments */ + char **argv /* Text of each argument */ +){ + sqlite *db; + if( argc!=2 ){ + Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], + " FILENAME\"", 0); + return TCL_ERROR; + } + db = (sqlite*)atoi(argv[1]); + sqlite_close(db); + return TCL_OK; +} + +/* +** Usage: sqlite_mprintf_int FORMAT INTEGER INTEGER INTEGER +** +** Call mprintf with three integer arguments +*/ +static int sqlite_mprintf_int( + void *NotUsed, + Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ + int argc, /* Number of arguments */ + char **argv /* Text of each argument */ +){ + int a[3], i; + char *z; + if( argc!=5 ){ + Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], + " FORMAT INT INT INT\"", 0); + return TCL_ERROR; + } + for(i=2; i<5; i++){ + if( Tcl_GetInt(interp, argv[i], &a[i-2]) ) return TCL_ERROR; + } + z = sqlite_mprintf(argv[1], a[0], a[1], a[2]); + Tcl_AppendResult(interp, z, 0); + sqliteFree(z); + return TCL_OK; +} + +/* +** Usage: sqlite_mprintf_str FORMAT INTEGER INTEGER STRING +** +** Call mprintf with two integer arguments and one string argument +*/ +static int sqlite_mprintf_str( + void *NotUsed, + Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ + int argc, /* Number of arguments */ + char **argv /* Text of each argument */ +){ + int a[3], i; + char *z; + if( argc!=5 ){ + Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], + " FORMAT INT INT STRING\"", 0); + return TCL_ERROR; + } + for(i=2; i<4; i++){ + if( Tcl_GetInt(interp, argv[i], &a[i-2]) ) return TCL_ERROR; + } + z = sqlite_mprintf(argv[1], a[0], a[1], argv[4]); + Tcl_AppendResult(interp, z, 0); + sqliteFree(z); + return TCL_OK; +} + +/* +** Usage: sqlite_mprintf_str FORMAT INTEGER INTEGER DOUBLE +** +** Call mprintf with two integer arguments and one double argument +*/ +static int sqlite_mprintf_double( + void *NotUsed, + Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ + int argc, /* Number of arguments */ + char **argv /* Text of each argument */ +){ + int a[3], i; + double r; + char *z; + if( argc!=5 ){ + Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], + " FORMAT INT INT STRING\"", 0); + return TCL_ERROR; + } + for(i=2; i<4; i++){ + if( Tcl_GetInt(interp, argv[i], &a[i-2]) ) return TCL_ERROR; + } + if( Tcl_GetDouble(interp, argv[4], &r) ) return TCL_ERROR; + z = sqlite_mprintf(argv[1], a[0], a[1], r); + Tcl_AppendResult(interp, z, 0); + sqliteFree(z); + return TCL_OK; +} + +/* +** Register commands with the TCL interpreter. +*/ +int Sqlitetest1_Init(Tcl_Interp *interp){ + Tcl_CreateCommand(interp, "sqlite_mprintf_int", sqlite_mprintf_int, 0, 0); + Tcl_CreateCommand(interp, "sqlite_mprintf_str", sqlite_mprintf_str, 0, 0); + Tcl_CreateCommand(interp, "sqlite_mprintf_double", sqlite_mprintf_double,0,0); + Tcl_CreateCommand(interp, "sqlite_open", sqlite_test_open, 0, 0); + Tcl_CreateCommand(interp, "sqlite_exec_printf", test_exec_printf, 0, 0); + Tcl_CreateCommand(interp, "sqlite_get_table_printf", test_get_table_printf, + 0, 0); + Tcl_CreateCommand(interp, "sqlite_close", sqlite_test_close, 0, 0); + return TCL_OK; +} |