xref: /sqlite-3.40.0/src/test1.c (revision e807febb)
1d1bf3512Sdrh /*
2b19a2bc6Sdrh ** 2001 September 15
3d1bf3512Sdrh **
4b19a2bc6Sdrh ** The author disclaims copyright to this source code.  In place of
5b19a2bc6Sdrh ** a legal notice, here is a blessing:
6d1bf3512Sdrh **
7b19a2bc6Sdrh **    May you do good and not evil.
8b19a2bc6Sdrh **    May you find forgiveness for yourself and forgive others.
9b19a2bc6Sdrh **    May you share freely, never taking more than you give.
10d1bf3512Sdrh **
11d1bf3512Sdrh *************************************************************************
12d1bf3512Sdrh ** Code for testing the printf() interface to SQLite.  This code
13d1bf3512Sdrh ** is not included in the SQLite library.  It is used for automated
14d1bf3512Sdrh ** testing of the SQLite library.
15d1bf3512Sdrh **
16*e807febbSdrh ** $Id: test1.c,v 1.31 2004/01/06 00:44:25 drh Exp $
17d1bf3512Sdrh */
18d1bf3512Sdrh #include "sqliteInt.h"
19d1bf3512Sdrh #include "tcl.h"
2094e92032Sdrh #include "os.h"
21d1bf3512Sdrh #include <stdlib.h>
22d1bf3512Sdrh #include <string.h>
23d1bf3512Sdrh 
2494e92032Sdrh #if OS_WIN
2594e92032Sdrh # define PTR_FMT "%x"
2694e92032Sdrh #else
2794e92032Sdrh # define PTR_FMT "%p"
2894e92032Sdrh #endif
2994e92032Sdrh 
30d1bf3512Sdrh /*
31b86ccfb2Sdrh ** Decode a pointer to an sqlite object.
32b86ccfb2Sdrh */
3390f405e3Sdrh static int getDbPointer(Tcl_Interp *interp, const char *zA, sqlite **ppDb){
3490f405e3Sdrh   if( sscanf(zA, PTR_FMT, (void**)ppDb)!=1 &&
3590f405e3Sdrh       (zA[0]!='0' || zA[1]!='x' || sscanf(&zA[2], PTR_FMT, (void**)ppDb)!=1)
3690f405e3Sdrh   ){
3790f405e3Sdrh     Tcl_AppendResult(interp, "\"", zA, "\" is not a valid pointer value", 0);
38b86ccfb2Sdrh     return TCL_ERROR;
39b86ccfb2Sdrh   }
40b86ccfb2Sdrh   return TCL_OK;
41b86ccfb2Sdrh }
42b86ccfb2Sdrh 
43b86ccfb2Sdrh /*
44b86ccfb2Sdrh ** Decode a pointer to an sqlite_vm object.
45b86ccfb2Sdrh */
46b86ccfb2Sdrh static int getVmPointer(Tcl_Interp *interp, const char *zArg, sqlite_vm **ppVm){
4794e92032Sdrh   if( sscanf(zArg, PTR_FMT, (void**)ppVm)!=1 ){
48b86ccfb2Sdrh     Tcl_AppendResult(interp, "\"", zArg, "\" is not a valid pointer value", 0);
49b86ccfb2Sdrh     return TCL_ERROR;
50b86ccfb2Sdrh   }
51b86ccfb2Sdrh   return TCL_OK;
52b86ccfb2Sdrh }
53b86ccfb2Sdrh 
54b86ccfb2Sdrh /*
557d8085a8Sdrh ** Generate a text representation of a pointer that can be understood
567d8085a8Sdrh ** by the getDbPointer and getVmPointer routines above.
577d8085a8Sdrh **
587d8085a8Sdrh ** The problem is, on some machines (Solaris) if you do a printf with
597d8085a8Sdrh ** "%p" you cannot turn around and do a scanf with the same "%p" and
607d8085a8Sdrh ** get your pointer back.  You have to prepend a "0x" before it will
617d8085a8Sdrh ** work.  Or at least that is what is reported to me (drh).  But this
627d8085a8Sdrh ** behavior varies from machine to machine.  The solution used her is
637d8085a8Sdrh ** to test the string right after it is generated to see if it can be
647d8085a8Sdrh ** understood by scanf, and if not, try prepending an "0x" to see if
657d8085a8Sdrh ** that helps.  If nothing works, a fatal error is generated.
667d8085a8Sdrh */
677d8085a8Sdrh static int makePointerStr(Tcl_Interp *interp, char *zPtr, void *p){
687d8085a8Sdrh   void *p2;
697d8085a8Sdrh   sprintf(zPtr, PTR_FMT, p);
707d8085a8Sdrh   if( sscanf(zPtr, PTR_FMT, &p2)!=1 || p2!=p ){
717d8085a8Sdrh     sprintf(zPtr, "0x" PTR_FMT, p);
727d8085a8Sdrh     if( sscanf(zPtr, PTR_FMT, &p2)!=1 || p2!=p ){
737d8085a8Sdrh       Tcl_AppendResult(interp, "unable to convert a pointer to a string "
747d8085a8Sdrh          "in the file " __FILE__ " in function makePointerStr().  Please "
757d8085a8Sdrh          "report this problem to the SQLite mailing list or as a new but "
767d8085a8Sdrh          "report.  Please provide detailed information about how you compiled "
777d8085a8Sdrh          "SQLite and what computer you are running on.", 0);
787d8085a8Sdrh       return TCL_ERROR;
797d8085a8Sdrh     }
807d8085a8Sdrh   }
817d8085a8Sdrh   return TCL_OK;
827d8085a8Sdrh }
837d8085a8Sdrh 
847d8085a8Sdrh /*
85d1bf3512Sdrh ** Usage:   sqlite_open filename
86d1bf3512Sdrh **
87d1bf3512Sdrh ** Returns:  The name of an open database.
88d1bf3512Sdrh */
89d1bf3512Sdrh static int sqlite_test_open(
90d1bf3512Sdrh   void *NotUsed,
91d1bf3512Sdrh   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
92d1bf3512Sdrh   int argc,              /* Number of arguments */
93d1bf3512Sdrh   char **argv            /* Text of each argument */
94d1bf3512Sdrh ){
95d1bf3512Sdrh   sqlite *db;
96d1bf3512Sdrh   char *zErr = 0;
97d1bf3512Sdrh   char zBuf[100];
98d1bf3512Sdrh   if( argc!=2 ){
99d1bf3512Sdrh     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
100d1bf3512Sdrh        " FILENAME\"", 0);
101d1bf3512Sdrh     return TCL_ERROR;
102d1bf3512Sdrh   }
103d1bf3512Sdrh   db = sqlite_open(argv[1], 0666, &zErr);
104d1bf3512Sdrh   if( db==0 ){
105d1bf3512Sdrh     Tcl_AppendResult(interp, zErr, 0);
106d1bf3512Sdrh     free(zErr);
107d1bf3512Sdrh     return TCL_ERROR;
108d1bf3512Sdrh   }
1097d8085a8Sdrh   if( makePointerStr(interp, zBuf, db) ) return TCL_ERROR;
110d1bf3512Sdrh   Tcl_AppendResult(interp, zBuf, 0);
111d1bf3512Sdrh   return TCL_OK;
112d1bf3512Sdrh }
113d1bf3512Sdrh 
114d1bf3512Sdrh /*
115d1bf3512Sdrh ** The callback routine for sqlite_exec_printf().
116d1bf3512Sdrh */
117d1bf3512Sdrh static int exec_printf_cb(void *pArg, int argc, char **argv, char **name){
118d1bf3512Sdrh   Tcl_DString *str = (Tcl_DString*)pArg;
119d1bf3512Sdrh   int i;
120d1bf3512Sdrh 
121d1bf3512Sdrh   if( Tcl_DStringLength(str)==0 ){
122d1bf3512Sdrh     for(i=0; i<argc; i++){
123d1bf3512Sdrh       Tcl_DStringAppendElement(str, name[i] ? name[i] : "NULL");
124d1bf3512Sdrh     }
125d1bf3512Sdrh   }
126d1bf3512Sdrh   for(i=0; i<argc; i++){
127d1bf3512Sdrh     Tcl_DStringAppendElement(str, argv[i] ? argv[i] : "NULL");
128d1bf3512Sdrh   }
129d1bf3512Sdrh   return 0;
130d1bf3512Sdrh }
131d1bf3512Sdrh 
132d1bf3512Sdrh /*
133d1bf3512Sdrh ** Usage:  sqlite_exec_printf  DB  FORMAT  STRING
134d1bf3512Sdrh **
135d1bf3512Sdrh ** Invoke the sqlite_exec_printf() interface using the open database
136d1bf3512Sdrh ** DB.  The SQL is the string FORMAT.  The format string should contain
137d1bf3512Sdrh ** one %s or %q.  STRING is the value inserted into %s or %q.
138d1bf3512Sdrh */
139d1bf3512Sdrh static int test_exec_printf(
140d1bf3512Sdrh   void *NotUsed,
141d1bf3512Sdrh   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
142d1bf3512Sdrh   int argc,              /* Number of arguments */
143d1bf3512Sdrh   char **argv            /* Text of each argument */
144d1bf3512Sdrh ){
145d1bf3512Sdrh   sqlite *db;
146d1bf3512Sdrh   Tcl_DString str;
147d1bf3512Sdrh   int rc;
148d1bf3512Sdrh   char *zErr = 0;
149d1bf3512Sdrh   char zBuf[30];
150d1bf3512Sdrh   if( argc!=4 ){
151d1bf3512Sdrh     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
152d1bf3512Sdrh        " DB FORMAT STRING", 0);
153d1bf3512Sdrh     return TCL_ERROR;
154d1bf3512Sdrh   }
155b86ccfb2Sdrh   if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
156d1bf3512Sdrh   Tcl_DStringInit(&str);
157d1bf3512Sdrh   rc = sqlite_exec_printf(db, argv[2], exec_printf_cb, &str, &zErr, argv[3]);
158d1bf3512Sdrh   sprintf(zBuf, "%d", rc);
159d1bf3512Sdrh   Tcl_AppendElement(interp, zBuf);
160d1bf3512Sdrh   Tcl_AppendElement(interp, rc==SQLITE_OK ? Tcl_DStringValue(&str) : zErr);
161d1bf3512Sdrh   Tcl_DStringFree(&str);
162d1bf3512Sdrh   if( zErr ) free(zErr);
163d1bf3512Sdrh   return TCL_OK;
164d1bf3512Sdrh }
165d1bf3512Sdrh 
166d1bf3512Sdrh /*
167d93d8a81Sdrh ** Usage:  sqlite_mprintf_z_test  SEPARATOR  ARG0  ARG1 ...
168d93d8a81Sdrh **
169d93d8a81Sdrh ** Test the %z format of mprintf().  Use multiple mprintf() calls to
170d93d8a81Sdrh ** concatenate arg0 through argn using separator as the separator.
171d93d8a81Sdrh ** Return the result.
172d93d8a81Sdrh */
173d93d8a81Sdrh static int test_mprintf_z(
174d93d8a81Sdrh   void *NotUsed,
175d93d8a81Sdrh   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
176d93d8a81Sdrh   int argc,              /* Number of arguments */
177d93d8a81Sdrh   char **argv            /* Text of each argument */
178d93d8a81Sdrh ){
179d93d8a81Sdrh   char *zResult = 0;
180d93d8a81Sdrh   int i;
181d93d8a81Sdrh 
182d93d8a81Sdrh   for(i=2; i<argc; i++){
183d93d8a81Sdrh     zResult = sqlite_mprintf("%z%s%s", zResult, argv[1], argv[i]);
184d93d8a81Sdrh   }
185d93d8a81Sdrh   Tcl_AppendResult(interp, zResult, 0);
186d93d8a81Sdrh   sqlite_freemem(zResult);
187d93d8a81Sdrh   return TCL_OK;
188d93d8a81Sdrh }
189d93d8a81Sdrh 
190d93d8a81Sdrh /*
191d1bf3512Sdrh ** Usage:  sqlite_get_table_printf  DB  FORMAT  STRING
192d1bf3512Sdrh **
193d1bf3512Sdrh ** Invoke the sqlite_get_table_printf() interface using the open database
194d1bf3512Sdrh ** DB.  The SQL is the string FORMAT.  The format string should contain
195d1bf3512Sdrh ** one %s or %q.  STRING is the value inserted into %s or %q.
196d1bf3512Sdrh */
197d1bf3512Sdrh static int test_get_table_printf(
198d1bf3512Sdrh   void *NotUsed,
199d1bf3512Sdrh   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
200d1bf3512Sdrh   int argc,              /* Number of arguments */
201d1bf3512Sdrh   char **argv            /* Text of each argument */
202d1bf3512Sdrh ){
203d1bf3512Sdrh   sqlite *db;
204d1bf3512Sdrh   Tcl_DString str;
205d1bf3512Sdrh   int rc;
206d1bf3512Sdrh   char *zErr = 0;
207d1bf3512Sdrh   int nRow, nCol;
208d1bf3512Sdrh   char **aResult;
209d1bf3512Sdrh   int i;
210d1bf3512Sdrh   char zBuf[30];
211d1bf3512Sdrh   if( argc!=4 ){
212d1bf3512Sdrh     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
213d1bf3512Sdrh        " DB FORMAT STRING", 0);
214d1bf3512Sdrh     return TCL_ERROR;
215d1bf3512Sdrh   }
216b86ccfb2Sdrh   if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
217d1bf3512Sdrh   Tcl_DStringInit(&str);
218d1bf3512Sdrh   rc = sqlite_get_table_printf(db, argv[2], &aResult, &nRow, &nCol,
219d1bf3512Sdrh                &zErr, argv[3]);
220d1bf3512Sdrh   sprintf(zBuf, "%d", rc);
221d1bf3512Sdrh   Tcl_AppendElement(interp, zBuf);
222d1bf3512Sdrh   if( rc==SQLITE_OK ){
223d1bf3512Sdrh     sprintf(zBuf, "%d", nRow);
224d1bf3512Sdrh     Tcl_AppendElement(interp, zBuf);
225d1bf3512Sdrh     sprintf(zBuf, "%d", nCol);
226d1bf3512Sdrh     Tcl_AppendElement(interp, zBuf);
227d1bf3512Sdrh     for(i=0; i<(nRow+1)*nCol; i++){
228d1bf3512Sdrh       Tcl_AppendElement(interp, aResult[i] ? aResult[i] : "NULL");
229d1bf3512Sdrh     }
230d1bf3512Sdrh   }else{
231d1bf3512Sdrh     Tcl_AppendElement(interp, zErr);
232d1bf3512Sdrh   }
233d1bf3512Sdrh   sqlite_free_table(aResult);
234d1bf3512Sdrh   if( zErr ) free(zErr);
235d1bf3512Sdrh   return TCL_OK;
236d1bf3512Sdrh }
237d1bf3512Sdrh 
238af9ff33aSdrh 
239af9ff33aSdrh /*
240af9ff33aSdrh ** Usage:  sqlite_last_insert_rowid DB
241af9ff33aSdrh **
242af9ff33aSdrh ** Returns the integer ROWID of the most recent insert.
243af9ff33aSdrh */
244af9ff33aSdrh static int test_last_rowid(
245af9ff33aSdrh   void *NotUsed,
246af9ff33aSdrh   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
247af9ff33aSdrh   int argc,              /* Number of arguments */
248af9ff33aSdrh   char **argv            /* Text of each argument */
249af9ff33aSdrh ){
250af9ff33aSdrh   sqlite *db;
251af9ff33aSdrh   char zBuf[30];
252af9ff33aSdrh 
253af9ff33aSdrh   if( argc!=2 ){
254af9ff33aSdrh     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " DB\"", 0);
255af9ff33aSdrh     return TCL_ERROR;
256af9ff33aSdrh   }
257b86ccfb2Sdrh   if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
258af9ff33aSdrh   sprintf(zBuf, "%d", sqlite_last_insert_rowid(db));
259af9ff33aSdrh   Tcl_AppendResult(interp, zBuf, 0);
260af9ff33aSdrh   return SQLITE_OK;
261af9ff33aSdrh }
262af9ff33aSdrh 
263d1bf3512Sdrh /*
264d1bf3512Sdrh ** Usage:  sqlite_close DB
265d1bf3512Sdrh **
266d1bf3512Sdrh ** Closes the database opened by sqlite_open.
267d1bf3512Sdrh */
268d1bf3512Sdrh static int sqlite_test_close(
269d1bf3512Sdrh   void *NotUsed,
270d1bf3512Sdrh   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
271d1bf3512Sdrh   int argc,              /* Number of arguments */
272d1bf3512Sdrh   char **argv            /* Text of each argument */
273d1bf3512Sdrh ){
274d1bf3512Sdrh   sqlite *db;
275d1bf3512Sdrh   if( argc!=2 ){
276d1bf3512Sdrh     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
277d1bf3512Sdrh        " FILENAME\"", 0);
278d1bf3512Sdrh     return TCL_ERROR;
279d1bf3512Sdrh   }
280b86ccfb2Sdrh   if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
281d1bf3512Sdrh   sqlite_close(db);
282d1bf3512Sdrh   return TCL_OK;
283d1bf3512Sdrh }
284d1bf3512Sdrh 
285d1bf3512Sdrh /*
286c22bd47dSdrh ** Implementation of the x_coalesce() function.
287c22bd47dSdrh ** Return the first argument non-NULL argument.
288c22bd47dSdrh */
289c22bd47dSdrh static void ifnullFunc(sqlite_func *context, int argc, const char **argv){
290c22bd47dSdrh   int i;
291c22bd47dSdrh   for(i=0; i<argc; i++){
292c22bd47dSdrh     if( argv[i] ){
293c22bd47dSdrh       sqlite_set_result_string(context, argv[i], -1);
294c22bd47dSdrh       break;
295c22bd47dSdrh     }
296c22bd47dSdrh   }
297c22bd47dSdrh }
298c22bd47dSdrh 
299c22bd47dSdrh /*
300c22bd47dSdrh ** Implementation of the x_sqlite_exec() function.  This function takes
301c22bd47dSdrh ** a single argument and attempts to execute that argument as SQL code.
3026cbe1f1bSdrh ** This is illegal and should set the SQLITE_MISUSE flag on the database.
303c22bd47dSdrh **
304c22bd47dSdrh ** This routine simulates the effect of having two threads attempt to
305c22bd47dSdrh ** use the same database at the same time.
306c22bd47dSdrh */
307c22bd47dSdrh static void sqliteExecFunc(sqlite_func *context, int argc, const char **argv){
308c22bd47dSdrh   sqlite_exec((sqlite*)sqlite_user_data(context), argv[0], 0, 0, 0);
309c22bd47dSdrh }
310c22bd47dSdrh 
311c22bd47dSdrh /*
312c22bd47dSdrh ** Usage:  sqlite_test_create_function DB
313c22bd47dSdrh **
314c22bd47dSdrh ** Call the sqlite_create_function API on the given database in order
315c22bd47dSdrh ** to create a function named "x_coalesce".  This function does the same thing
316c22bd47dSdrh ** as the "coalesce" function.  This function also registers an SQL function
317c22bd47dSdrh ** named "x_sqlite_exec" that invokes sqlite_exec().  Invoking sqlite_exec()
318c22bd47dSdrh ** in this way is illegal recursion and should raise an SQLITE_MISUSE error.
319c22bd47dSdrh ** The effect is similar to trying to use the same database connection from
320c22bd47dSdrh ** two threads at the same time.
321c22bd47dSdrh **
322c22bd47dSdrh ** The original motivation for this routine was to be able to call the
323c22bd47dSdrh ** sqlite_create_function function while a query is in progress in order
324c22bd47dSdrh ** to test the SQLITE_MISUSE detection logic.
325c22bd47dSdrh */
326c2eef3b3Sdrh static int test_create_function(
327c22bd47dSdrh   void *NotUsed,
328c22bd47dSdrh   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
329c22bd47dSdrh   int argc,              /* Number of arguments */
330c22bd47dSdrh   char **argv            /* Text of each argument */
331c22bd47dSdrh ){
332c22bd47dSdrh   sqlite *db;
333c22bd47dSdrh   extern void Md5_Register(sqlite*);
334c22bd47dSdrh   if( argc!=2 ){
335c22bd47dSdrh     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
336c22bd47dSdrh        " FILENAME\"", 0);
337c22bd47dSdrh     return TCL_ERROR;
338c22bd47dSdrh   }
339b86ccfb2Sdrh   if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
340c22bd47dSdrh   sqlite_create_function(db, "x_coalesce", -1, ifnullFunc, 0);
341c22bd47dSdrh   sqlite_create_function(db, "x_sqlite_exec", 1, sqliteExecFunc, db);
342c22bd47dSdrh   return TCL_OK;
343c22bd47dSdrh }
344c22bd47dSdrh 
345c22bd47dSdrh /*
346c22bd47dSdrh ** Routines to implement the x_count() aggregate function.
347c22bd47dSdrh */
348c22bd47dSdrh typedef struct CountCtx CountCtx;
349c22bd47dSdrh struct CountCtx {
350c22bd47dSdrh   int n;
351c22bd47dSdrh };
352c22bd47dSdrh static void countStep(sqlite_func *context, int argc, const char **argv){
353c22bd47dSdrh   CountCtx *p;
354c22bd47dSdrh   p = sqlite_aggregate_context(context, sizeof(*p));
355c22bd47dSdrh   if( (argc==0 || argv[0]) && p ){
356c22bd47dSdrh     p->n++;
357c22bd47dSdrh   }
358c22bd47dSdrh }
359c22bd47dSdrh static void countFinalize(sqlite_func *context){
360c22bd47dSdrh   CountCtx *p;
361c22bd47dSdrh   p = sqlite_aggregate_context(context, sizeof(*p));
362c22bd47dSdrh   sqlite_set_result_int(context, p ? p->n : 0);
363c22bd47dSdrh }
364c22bd47dSdrh 
365c22bd47dSdrh /*
366c22bd47dSdrh ** Usage:  sqlite_test_create_aggregate DB
367c22bd47dSdrh **
368c22bd47dSdrh ** Call the sqlite_create_function API on the given database in order
369c22bd47dSdrh ** to create a function named "x_count".  This function does the same thing
370c22bd47dSdrh ** as the "md5sum" function.
371c22bd47dSdrh **
372c22bd47dSdrh ** The original motivation for this routine was to be able to call the
373c22bd47dSdrh ** sqlite_create_aggregate function while a query is in progress in order
374c22bd47dSdrh ** to test the SQLITE_MISUSE detection logic.
375c22bd47dSdrh */
376c2eef3b3Sdrh static int test_create_aggregate(
377c22bd47dSdrh   void *NotUsed,
378c22bd47dSdrh   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
379c22bd47dSdrh   int argc,              /* Number of arguments */
380c22bd47dSdrh   char **argv            /* Text of each argument */
381c22bd47dSdrh ){
382c22bd47dSdrh   sqlite *db;
383c22bd47dSdrh   if( argc!=2 ){
384c22bd47dSdrh     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
385c22bd47dSdrh        " FILENAME\"", 0);
386c22bd47dSdrh     return TCL_ERROR;
387c22bd47dSdrh   }
388b86ccfb2Sdrh   if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
389c22bd47dSdrh   sqlite_create_aggregate(db, "x_count", 0, countStep, countFinalize, 0);
390c22bd47dSdrh   sqlite_create_aggregate(db, "x_count", 1, countStep, countFinalize, 0);
391c22bd47dSdrh   return TCL_OK;
392c22bd47dSdrh }
393c22bd47dSdrh 
394c22bd47dSdrh 
395c22bd47dSdrh 
396c22bd47dSdrh /*
397d1bf3512Sdrh ** Usage:  sqlite_mprintf_int FORMAT INTEGER INTEGER INTEGER
398d1bf3512Sdrh **
399d1bf3512Sdrh ** Call mprintf with three integer arguments
400d1bf3512Sdrh */
401d1bf3512Sdrh static int sqlite_mprintf_int(
402d1bf3512Sdrh   void *NotUsed,
403d1bf3512Sdrh   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
404d1bf3512Sdrh   int argc,              /* Number of arguments */
405d1bf3512Sdrh   char **argv            /* Text of each argument */
406d1bf3512Sdrh ){
407d1bf3512Sdrh   int a[3], i;
408d1bf3512Sdrh   char *z;
409d1bf3512Sdrh   if( argc!=5 ){
410d1bf3512Sdrh     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
411d1bf3512Sdrh        " FORMAT INT INT INT\"", 0);
412d1bf3512Sdrh     return TCL_ERROR;
413d1bf3512Sdrh   }
414d1bf3512Sdrh   for(i=2; i<5; i++){
415d1bf3512Sdrh     if( Tcl_GetInt(interp, argv[i], &a[i-2]) ) return TCL_ERROR;
416d1bf3512Sdrh   }
417d1bf3512Sdrh   z = sqlite_mprintf(argv[1], a[0], a[1], a[2]);
418d1bf3512Sdrh   Tcl_AppendResult(interp, z, 0);
419fa173a76Sdrh   sqlite_freemem(z);
420d1bf3512Sdrh   return TCL_OK;
421d1bf3512Sdrh }
422d1bf3512Sdrh 
423d1bf3512Sdrh /*
424d1bf3512Sdrh ** Usage:  sqlite_mprintf_str FORMAT INTEGER INTEGER STRING
425d1bf3512Sdrh **
426d1bf3512Sdrh ** Call mprintf with two integer arguments and one string argument
427d1bf3512Sdrh */
428d1bf3512Sdrh static int sqlite_mprintf_str(
429d1bf3512Sdrh   void *NotUsed,
430d1bf3512Sdrh   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
431d1bf3512Sdrh   int argc,              /* Number of arguments */
432d1bf3512Sdrh   char **argv            /* Text of each argument */
433d1bf3512Sdrh ){
434d1bf3512Sdrh   int a[3], i;
435d1bf3512Sdrh   char *z;
436f220b24fSchw   if( argc<4 || argc>5 ){
437d1bf3512Sdrh     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
438f220b24fSchw        " FORMAT INT INT ?STRING?\"", 0);
439d1bf3512Sdrh     return TCL_ERROR;
440d1bf3512Sdrh   }
441d1bf3512Sdrh   for(i=2; i<4; i++){
442d1bf3512Sdrh     if( Tcl_GetInt(interp, argv[i], &a[i-2]) ) return TCL_ERROR;
443d1bf3512Sdrh   }
444f220b24fSchw   z = sqlite_mprintf(argv[1], a[0], a[1], argc>4 ? argv[4] : NULL);
445d1bf3512Sdrh   Tcl_AppendResult(interp, z, 0);
446fa173a76Sdrh   sqlite_freemem(z);
447d1bf3512Sdrh   return TCL_OK;
448d1bf3512Sdrh }
449d1bf3512Sdrh 
450d1bf3512Sdrh /*
451d1bf3512Sdrh ** Usage:  sqlite_mprintf_str FORMAT INTEGER INTEGER DOUBLE
452d1bf3512Sdrh **
453d1bf3512Sdrh ** Call mprintf with two integer arguments and one double argument
454d1bf3512Sdrh */
455d1bf3512Sdrh static int sqlite_mprintf_double(
456d1bf3512Sdrh   void *NotUsed,
457d1bf3512Sdrh   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
458d1bf3512Sdrh   int argc,              /* Number of arguments */
459d1bf3512Sdrh   char **argv            /* Text of each argument */
460d1bf3512Sdrh ){
461d1bf3512Sdrh   int a[3], i;
462d1bf3512Sdrh   double r;
463d1bf3512Sdrh   char *z;
464d1bf3512Sdrh   if( argc!=5 ){
465d1bf3512Sdrh     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
466d1bf3512Sdrh        " FORMAT INT INT STRING\"", 0);
467d1bf3512Sdrh     return TCL_ERROR;
468d1bf3512Sdrh   }
469d1bf3512Sdrh   for(i=2; i<4; i++){
470d1bf3512Sdrh     if( Tcl_GetInt(interp, argv[i], &a[i-2]) ) return TCL_ERROR;
471d1bf3512Sdrh   }
472d1bf3512Sdrh   if( Tcl_GetDouble(interp, argv[4], &r) ) return TCL_ERROR;
473d1bf3512Sdrh   z = sqlite_mprintf(argv[1], a[0], a[1], r);
474d1bf3512Sdrh   Tcl_AppendResult(interp, z, 0);
475fa173a76Sdrh   sqlite_freemem(z);
476d1bf3512Sdrh   return TCL_OK;
477d1bf3512Sdrh }
478d1bf3512Sdrh 
479d1bf3512Sdrh /*
480daffd0e5Sdrh ** Usage: sqlite_malloc_fail N
481daffd0e5Sdrh **
482afa4a020Sdrh ** Rig sqliteMalloc() to fail on the N-th call.  Turn off this mechanism
483daffd0e5Sdrh ** and reset the sqlite_malloc_failed variable is N==0.
484daffd0e5Sdrh */
485daffd0e5Sdrh #ifdef MEMORY_DEBUG
486daffd0e5Sdrh static int sqlite_malloc_fail(
487daffd0e5Sdrh   void *NotUsed,
488daffd0e5Sdrh   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
489daffd0e5Sdrh   int argc,              /* Number of arguments */
490daffd0e5Sdrh   char **argv            /* Text of each argument */
491daffd0e5Sdrh ){
492daffd0e5Sdrh   int n;
493daffd0e5Sdrh   if( argc!=2 ){
494daffd0e5Sdrh     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " N\"", 0);
495daffd0e5Sdrh     return TCL_ERROR;
496daffd0e5Sdrh   }
497daffd0e5Sdrh   if( Tcl_GetInt(interp, argv[1], &n) ) return TCL_ERROR;
498daffd0e5Sdrh   sqlite_iMallocFail = n;
499daffd0e5Sdrh   sqlite_malloc_failed = 0;
500daffd0e5Sdrh   return TCL_OK;
501daffd0e5Sdrh }
502daffd0e5Sdrh #endif
503daffd0e5Sdrh 
504daffd0e5Sdrh /*
505daffd0e5Sdrh ** Usage: sqlite_malloc_stat
506daffd0e5Sdrh **
507daffd0e5Sdrh ** Return the number of prior calls to sqliteMalloc() and sqliteFree().
508daffd0e5Sdrh */
509daffd0e5Sdrh #ifdef MEMORY_DEBUG
510daffd0e5Sdrh static int sqlite_malloc_stat(
511daffd0e5Sdrh   void *NotUsed,
512daffd0e5Sdrh   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
513daffd0e5Sdrh   int argc,              /* Number of arguments */
514daffd0e5Sdrh   char **argv            /* Text of each argument */
515daffd0e5Sdrh ){
516daffd0e5Sdrh   char zBuf[200];
517daffd0e5Sdrh   sprintf(zBuf, "%d %d %d", sqlite_nMalloc, sqlite_nFree, sqlite_iMallocFail);
518daffd0e5Sdrh   Tcl_AppendResult(interp, zBuf, 0);
519daffd0e5Sdrh   return TCL_OK;
520daffd0e5Sdrh }
521daffd0e5Sdrh #endif
522daffd0e5Sdrh 
523daffd0e5Sdrh /*
52428b4e489Sdrh ** Usage:  sqlite_abort
52528b4e489Sdrh **
52628b4e489Sdrh ** Shutdown the process immediately.  This is not a clean shutdown.
52728b4e489Sdrh ** This command is used to test the recoverability of a database in
52828b4e489Sdrh ** the event of a program crash.
52928b4e489Sdrh */
53028b4e489Sdrh static int sqlite_abort(
53128b4e489Sdrh   void *NotUsed,
53228b4e489Sdrh   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
53328b4e489Sdrh   int argc,              /* Number of arguments */
53428b4e489Sdrh   char **argv            /* Text of each argument */
53528b4e489Sdrh ){
53628b4e489Sdrh   assert( interp==0 );   /* This will always fail */
53728b4e489Sdrh   return TCL_OK;
53828b4e489Sdrh }
53928b4e489Sdrh 
54028b4e489Sdrh /*
5416cbe1f1bSdrh ** The following routine is a user-defined SQL function whose purpose
5426cbe1f1bSdrh ** is to test the sqlite_set_result() API.
5436cbe1f1bSdrh */
5446cbe1f1bSdrh static void testFunc(sqlite_func *context, int argc, const char **argv){
5456cbe1f1bSdrh   while( argc>=2 ){
5466cbe1f1bSdrh     if( argv[0]==0 ){
5476cbe1f1bSdrh       sqlite_set_result_error(context, "first argument to test function "
5486cbe1f1bSdrh          "may not be NULL", -1);
5496cbe1f1bSdrh     }else if( sqliteStrICmp(argv[0],"string")==0 ){
5506cbe1f1bSdrh       sqlite_set_result_string(context, argv[1], -1);
5516cbe1f1bSdrh     }else if( argv[1]==0 ){
5526cbe1f1bSdrh       sqlite_set_result_error(context, "2nd argument may not be NULL if the "
5536cbe1f1bSdrh          "first argument is not \"string\"", -1);
5546cbe1f1bSdrh     }else if( sqliteStrICmp(argv[0],"int")==0 ){
5556cbe1f1bSdrh       sqlite_set_result_int(context, atoi(argv[1]));
5566cbe1f1bSdrh     }else if( sqliteStrICmp(argv[0],"double")==0 ){
55793a5c6bdSdrh       sqlite_set_result_double(context, sqliteAtoF(argv[1]));
5586cbe1f1bSdrh     }else{
5596cbe1f1bSdrh       sqlite_set_result_error(context,"first argument should be one of: "
5606cbe1f1bSdrh           "string int double", -1);
5616cbe1f1bSdrh     }
5626cbe1f1bSdrh     argc -= 2;
5636cbe1f1bSdrh     argv += 2;
5646cbe1f1bSdrh   }
5656cbe1f1bSdrh }
5666cbe1f1bSdrh 
5676cbe1f1bSdrh /*
5686cbe1f1bSdrh ** Usage:   sqlite_register_test_function  DB  NAME
5696cbe1f1bSdrh **
5706cbe1f1bSdrh ** Register the test SQL function on the database DB under the name NAME.
5716cbe1f1bSdrh */
572c2eef3b3Sdrh static int test_register_func(
5736cbe1f1bSdrh   void *NotUsed,
5746cbe1f1bSdrh   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
5756cbe1f1bSdrh   int argc,              /* Number of arguments */
5766cbe1f1bSdrh   char **argv            /* Text of each argument */
5776cbe1f1bSdrh ){
5786cbe1f1bSdrh   sqlite *db;
5796cbe1f1bSdrh   int rc;
5806cbe1f1bSdrh   if( argc!=3 ){
5816cbe1f1bSdrh     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
5826cbe1f1bSdrh        " DB FUNCTION-NAME", 0);
5836cbe1f1bSdrh     return TCL_ERROR;
5846cbe1f1bSdrh   }
585b86ccfb2Sdrh   if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
5866cbe1f1bSdrh   rc = sqlite_create_function(db, argv[2], -1, testFunc, 0);
5876cbe1f1bSdrh   if( rc!=0 ){
5886cbe1f1bSdrh     Tcl_AppendResult(interp, sqlite_error_string(rc), 0);
5896cbe1f1bSdrh     return TCL_ERROR;
5906cbe1f1bSdrh   }
5916cbe1f1bSdrh   return TCL_OK;
5926cbe1f1bSdrh }
5936cbe1f1bSdrh 
5946cbe1f1bSdrh /*
5955a38705eSdrh ** This SQLite callback records the datatype of all columns.
5965a38705eSdrh **
5975a38705eSdrh ** The pArg argument is really a pointer to a TCL interpreter.  The
5985a38705eSdrh ** column names are inserted as the result of this interpreter.
5995a38705eSdrh **
6005a38705eSdrh ** This routine returns non-zero which causes the query to abort.
6015a38705eSdrh */
6025a38705eSdrh static int rememberDataTypes(void *pArg, int nCol, char **argv, char **colv){
6035a38705eSdrh   int i;
6045a38705eSdrh   Tcl_Interp *interp = (Tcl_Interp*)pArg;
6055a38705eSdrh   Tcl_Obj *pList, *pElem;
6065a38705eSdrh   if( colv[nCol+1]==0 ){
6075a38705eSdrh     return 1;
6085a38705eSdrh   }
6095a38705eSdrh   pList = Tcl_NewObj();
6105a38705eSdrh   for(i=0; i<nCol; i++){
6115a38705eSdrh     pElem = Tcl_NewStringObj(colv[i+nCol] ? colv[i+nCol] : "NULL", -1);
6125a38705eSdrh     Tcl_ListObjAppendElement(interp, pList, pElem);
6135a38705eSdrh   }
6145a38705eSdrh   Tcl_SetObjResult(interp, pList);
6155a38705eSdrh   return 1;
6165a38705eSdrh }
6175a38705eSdrh 
6185a38705eSdrh /*
6195a38705eSdrh ** Invoke an SQL statement but ignore all the data in the result.  Instead,
6205a38705eSdrh ** return a list that consists of the datatypes of the various columns.
6215a38705eSdrh **
6225a38705eSdrh ** This only works if "PRAGMA show_datatypes=on" has been executed against
6235a38705eSdrh ** the database connection.
6245a38705eSdrh */
6255a38705eSdrh static int sqlite_datatypes(
6265a38705eSdrh   void *NotUsed,
6275a38705eSdrh   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
6285a38705eSdrh   int argc,              /* Number of arguments */
6295a38705eSdrh   char **argv            /* Text of each argument */
6305a38705eSdrh ){
6315a38705eSdrh   sqlite *db;
6325a38705eSdrh   int rc;
6335a38705eSdrh   if( argc!=3 ){
6345a38705eSdrh     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
6355a38705eSdrh        " DB SQL", 0);
6365a38705eSdrh     return TCL_ERROR;
6375a38705eSdrh   }
638b86ccfb2Sdrh   if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
6395a38705eSdrh   rc = sqlite_exec(db, argv[2], rememberDataTypes, interp, 0);
6405a38705eSdrh   if( rc!=0 && rc!=SQLITE_ABORT ){
6415a38705eSdrh     Tcl_AppendResult(interp, sqlite_error_string(rc), 0);
6425a38705eSdrh     return TCL_ERROR;
6435a38705eSdrh   }
6445a38705eSdrh   return TCL_OK;
6455a38705eSdrh }
6465a38705eSdrh 
647b86ccfb2Sdrh /*
648073e5a77Sdrh ** Usage:  sqlite_compile  DB  SQL  ?TAILVAR?
649b86ccfb2Sdrh **
650b86ccfb2Sdrh ** Attempt to compile an SQL statement.  Return a pointer to the virtual
651b86ccfb2Sdrh ** machine used to execute that statement.  Unprocessed SQL is written
652b86ccfb2Sdrh ** into TAILVAR.
653b86ccfb2Sdrh */
654b86ccfb2Sdrh static int test_compile(
655b86ccfb2Sdrh   void *NotUsed,
656b86ccfb2Sdrh   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
657b86ccfb2Sdrh   int argc,              /* Number of arguments */
658b86ccfb2Sdrh   char **argv            /* Text of each argument */
659b86ccfb2Sdrh ){
660b86ccfb2Sdrh   sqlite *db;
661b86ccfb2Sdrh   sqlite_vm *vm;
662b86ccfb2Sdrh   int rc;
663b86ccfb2Sdrh   char *zErr = 0;
664b86ccfb2Sdrh   const char *zTail;
665b86ccfb2Sdrh   char zBuf[50];
666073e5a77Sdrh   if( argc!=3 && argc!=4 ){
667b86ccfb2Sdrh     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
668b86ccfb2Sdrh        " DB SQL TAILVAR", 0);
669b86ccfb2Sdrh     return TCL_ERROR;
670b86ccfb2Sdrh   }
671b86ccfb2Sdrh   if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
672073e5a77Sdrh   rc = sqlite_compile(db, argv[2], argc==4 ? &zTail : 0, &vm, &zErr);
673073e5a77Sdrh   if( argc==4 ) Tcl_SetVar(interp, argv[3], zTail, 0);
674b86ccfb2Sdrh   if( rc ){
675b86ccfb2Sdrh     assert( vm==0 );
676b86ccfb2Sdrh     sprintf(zBuf, "(%d) ", rc);
677b86ccfb2Sdrh     Tcl_AppendResult(interp, zBuf, zErr, 0);
678b86ccfb2Sdrh     sqlite_freemem(zErr);
679b86ccfb2Sdrh     return TCL_ERROR;
680b86ccfb2Sdrh   }
681326dce74Sdrh   if( vm ){
6827d8085a8Sdrh     if( makePointerStr(interp, zBuf, vm) ) return TCL_ERROR;
683b86ccfb2Sdrh     Tcl_AppendResult(interp, zBuf, 0);
684326dce74Sdrh   }
685b86ccfb2Sdrh   return TCL_OK;
686b86ccfb2Sdrh }
687b86ccfb2Sdrh 
688b86ccfb2Sdrh /*
689073e5a77Sdrh ** Usage:  sqlite_step  VM  ?NVAR?  ?VALUEVAR?  ?COLNAMEVAR?
690b86ccfb2Sdrh **
691b86ccfb2Sdrh ** Step a virtual machine.  Return a the result code as a string.
692b86ccfb2Sdrh ** Column results are written into three variables.
693b86ccfb2Sdrh */
694b86ccfb2Sdrh static int test_step(
695b86ccfb2Sdrh   void *NotUsed,
696b86ccfb2Sdrh   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
697b86ccfb2Sdrh   int argc,              /* Number of arguments */
698b86ccfb2Sdrh   char **argv            /* Text of each argument */
699b86ccfb2Sdrh ){
700b86ccfb2Sdrh   sqlite_vm *vm;
701b86ccfb2Sdrh   int rc, i;
7023a84069dSdrh   const char **azValue = 0;
7033a84069dSdrh   const char **azColName = 0;
7043a84069dSdrh   int N = 0;
705b86ccfb2Sdrh   char *zRc;
706b86ccfb2Sdrh   char zBuf[50];
707073e5a77Sdrh   if( argc<2 || argc>5 ){
708b86ccfb2Sdrh     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
709b86ccfb2Sdrh        " VM NVAR VALUEVAR COLNAMEVAR", 0);
710b86ccfb2Sdrh     return TCL_ERROR;
711b86ccfb2Sdrh   }
712b86ccfb2Sdrh   if( getVmPointer(interp, argv[1], &vm) ) return TCL_ERROR;
713073e5a77Sdrh   rc = sqlite_step(vm, argc>=3?&N:0, argc>=4?&azValue:0, argc==5?&azColName:0);
714073e5a77Sdrh   if( argc>=3 ){
715b86ccfb2Sdrh     sprintf(zBuf, "%d", N);
716b86ccfb2Sdrh     Tcl_SetVar(interp, argv[2], zBuf, 0);
717073e5a77Sdrh   }
718073e5a77Sdrh   if( argc>=4 ){
719b86ccfb2Sdrh     Tcl_SetVar(interp, argv[3], "", 0);
7203a84069dSdrh     if( azValue ){
721b86ccfb2Sdrh       for(i=0; i<N; i++){
722b86ccfb2Sdrh         Tcl_SetVar(interp, argv[3], azValue[i] ? azValue[i] : "",
723b86ccfb2Sdrh             TCL_APPEND_VALUE | TCL_LIST_ELEMENT);
724b86ccfb2Sdrh       }
725b86ccfb2Sdrh     }
726073e5a77Sdrh   }
727073e5a77Sdrh   if( argc==5 ){
728b86ccfb2Sdrh     Tcl_SetVar(interp, argv[4], "", 0);
7293a84069dSdrh     if( azColName ){
730b86ccfb2Sdrh       for(i=0; i<N*2; i++){
731326dce74Sdrh         Tcl_SetVar(interp, argv[4], azColName[i] ? azColName[i] : "",
732b86ccfb2Sdrh             TCL_APPEND_VALUE | TCL_LIST_ELEMENT);
733b86ccfb2Sdrh       }
734b86ccfb2Sdrh     }
735073e5a77Sdrh   }
736b86ccfb2Sdrh   switch( rc ){
737b86ccfb2Sdrh     case SQLITE_DONE:   zRc = "SQLITE_DONE";    break;
738326dce74Sdrh     case SQLITE_BUSY:   zRc = "SQLITE_BUSY";    break;
739326dce74Sdrh     case SQLITE_ROW:    zRc = "SQLITE_ROW";     break;
740326dce74Sdrh     case SQLITE_ERROR:  zRc = "SQLITE_ERROR";   break;
741b86ccfb2Sdrh     case SQLITE_MISUSE: zRc = "SQLITE_MISUSE";  break;
742b86ccfb2Sdrh     default:            zRc = "unknown";        break;
743b86ccfb2Sdrh   }
744b86ccfb2Sdrh   Tcl_AppendResult(interp, zRc, 0);
745b86ccfb2Sdrh   return TCL_OK;
746b86ccfb2Sdrh }
747b86ccfb2Sdrh 
748b86ccfb2Sdrh /*
749b86ccfb2Sdrh ** Usage:  sqlite_finalize  VM
750b86ccfb2Sdrh **
751b86ccfb2Sdrh ** Shutdown a virtual machine.
752b86ccfb2Sdrh */
753b86ccfb2Sdrh static int test_finalize(
754b86ccfb2Sdrh   void *NotUsed,
755b86ccfb2Sdrh   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
756b86ccfb2Sdrh   int argc,              /* Number of arguments */
757b86ccfb2Sdrh   char **argv            /* Text of each argument */
758b86ccfb2Sdrh ){
759b86ccfb2Sdrh   sqlite_vm *vm;
760b86ccfb2Sdrh   int rc;
761b86ccfb2Sdrh   char *zErrMsg = 0;
762b86ccfb2Sdrh   if( argc!=2 ){
763b86ccfb2Sdrh     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
764326dce74Sdrh        " VM\"", 0);
765b86ccfb2Sdrh     return TCL_ERROR;
766b86ccfb2Sdrh   }
767b86ccfb2Sdrh   if( getVmPointer(interp, argv[1], &vm) ) return TCL_ERROR;
768b86ccfb2Sdrh   rc = sqlite_finalize(vm, &zErrMsg);
769b86ccfb2Sdrh   if( rc ){
770b86ccfb2Sdrh     char zBuf[50];
771b86ccfb2Sdrh     sprintf(zBuf, "(%d) ", rc);
772b86ccfb2Sdrh     Tcl_AppendResult(interp, zBuf, zErrMsg, 0);
773b86ccfb2Sdrh     sqlite_freemem(zErrMsg);
774b86ccfb2Sdrh     return TCL_ERROR;
775b86ccfb2Sdrh   }
776b86ccfb2Sdrh   return TCL_OK;
777b86ccfb2Sdrh }
778b86ccfb2Sdrh 
7795a38705eSdrh /*
78050457896Sdrh ** Usage:  sqlite_reset   VM
78150457896Sdrh **
78250457896Sdrh ** Reset a virtual machine and prepare it to be run again.
78350457896Sdrh */
78450457896Sdrh static int test_reset(
78550457896Sdrh   void *NotUsed,
78650457896Sdrh   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
78750457896Sdrh   int argc,              /* Number of arguments */
78850457896Sdrh   char **argv            /* Text of each argument */
78950457896Sdrh ){
79050457896Sdrh   sqlite_vm *vm;
79150457896Sdrh   int rc;
79250457896Sdrh   char *zErrMsg = 0;
79350457896Sdrh   if( argc!=2 ){
79450457896Sdrh     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
79550457896Sdrh        " VM\"", 0);
79650457896Sdrh     return TCL_ERROR;
79750457896Sdrh   }
79850457896Sdrh   if( getVmPointer(interp, argv[1], &vm) ) return TCL_ERROR;
79950457896Sdrh   rc = sqlite_reset(vm, &zErrMsg);
80050457896Sdrh   if( rc ){
80150457896Sdrh     char zBuf[50];
80250457896Sdrh     sprintf(zBuf, "(%d) ", rc);
80350457896Sdrh     Tcl_AppendResult(interp, zBuf, zErrMsg, 0);
80450457896Sdrh     sqlite_freemem(zErrMsg);
80550457896Sdrh     return TCL_ERROR;
80650457896Sdrh   }
80750457896Sdrh   return TCL_OK;
80850457896Sdrh }
80950457896Sdrh 
81050457896Sdrh /*
8117c972decSdrh ** This is the "static_bind_value" that variables are bound to when
8127c972decSdrh ** the FLAG option of sqlite_bind is "static"
81350457896Sdrh */
8147c972decSdrh static char *sqlite_static_bind_value = 0;
8157c972decSdrh 
8167c972decSdrh /*
8177c972decSdrh ** Usage:  sqlite_bind  VM  IDX  VALUE  FLAGS
8187c972decSdrh **
8197c972decSdrh ** Sets the value of the IDX-th occurance of "?" in the original SQL
8207c972decSdrh ** string.  VALUE is the new value.  If FLAGS=="null" then VALUE is
8217c972decSdrh ** ignored and the value is set to NULL.  If FLAGS=="static" then
8227c972decSdrh ** the value is set to the value of a static variable named
8237c972decSdrh ** "sqlite_static_bind_value".  If FLAGS=="normal" then a copy
8247c972decSdrh ** of the VALUE is made.
8257c972decSdrh */
8267c972decSdrh static int test_bind(
82750457896Sdrh   void *NotUsed,
82850457896Sdrh   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
82950457896Sdrh   int argc,              /* Number of arguments */
83050457896Sdrh   char **argv            /* Text of each argument */
83150457896Sdrh ){
83250457896Sdrh   sqlite_vm *vm;
83350457896Sdrh   int rc;
8347c972decSdrh   int idx;
8357c972decSdrh   if( argc!=5 ){
83650457896Sdrh     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
8377c972decSdrh        " VM IDX VALUE (null|static|normal)\"", 0);
83850457896Sdrh     return TCL_ERROR;
83950457896Sdrh   }
84050457896Sdrh   if( getVmPointer(interp, argv[1], &vm) ) return TCL_ERROR;
8417c972decSdrh   if( Tcl_GetInt(interp, argv[2], &idx) ) return TCL_ERROR;
8427c972decSdrh   if( strcmp(argv[4],"null")==0 ){
8437c972decSdrh     rc = sqlite_bind(vm, idx, 0, 0, 0);
8447c972decSdrh   }else if( strcmp(argv[4],"static")==0 ){
8457c972decSdrh     rc = sqlite_bind(vm, idx, sqlite_static_bind_value, -1, 0);
8467c972decSdrh   }else if( strcmp(argv[4],"normal")==0 ){
8477c972decSdrh     rc = sqlite_bind(vm, idx, argv[3], -1, 1);
8487c972decSdrh   }else{
8497c972decSdrh     Tcl_AppendResult(interp, "4th argument should be "
8507c972decSdrh         "\"null\" or \"static\" or \"normal\"", 0);
8517c972decSdrh     return TCL_ERROR;
8527c972decSdrh   }
85350457896Sdrh   if( rc ){
85450457896Sdrh     char zBuf[50];
85550457896Sdrh     sprintf(zBuf, "(%d) ", rc);
85650457896Sdrh     Tcl_AppendResult(interp, zBuf, sqlite_error_string(rc), 0);
85750457896Sdrh     return TCL_ERROR;
85850457896Sdrh   }
85950457896Sdrh   return TCL_OK;
86050457896Sdrh }
86150457896Sdrh 
86250457896Sdrh /*
86399ee3600Sdrh ** Usage:    breakpoint
86499ee3600Sdrh **
86599ee3600Sdrh ** This routine exists for one purpose - to provide a place to put a
86699ee3600Sdrh ** breakpoint with GDB that can be triggered using TCL code.  The use
86799ee3600Sdrh ** for this is when a particular test fails on (say) the 1485th iteration.
86899ee3600Sdrh ** In the TCL test script, we can add code like this:
86999ee3600Sdrh **
87099ee3600Sdrh **     if {$i==1485} breakpoint
87199ee3600Sdrh **
87299ee3600Sdrh ** Then run testfixture in the debugger and wait for the breakpoint to
87399ee3600Sdrh ** fire.  Then additional breakpoints can be set to trace down the bug.
87499ee3600Sdrh */
87599ee3600Sdrh static int test_breakpoint(
87699ee3600Sdrh   void *NotUsed,
87799ee3600Sdrh   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
87899ee3600Sdrh   int argc,              /* Number of arguments */
87999ee3600Sdrh   char **argv            /* Text of each argument */
88099ee3600Sdrh ){
88199ee3600Sdrh   return TCL_OK;         /* Do nothing */
88299ee3600Sdrh }
88399ee3600Sdrh 
88499ee3600Sdrh /*
885d1bf3512Sdrh ** Register commands with the TCL interpreter.
886d1bf3512Sdrh */
887d1bf3512Sdrh int Sqlitetest1_Init(Tcl_Interp *interp){
888487ab3caSdrh   extern int sqlite_search_count;
88994e92032Sdrh   extern int sqlite_open_file_count;
890*e807febbSdrh   extern int sqlite_current_time;
891c2eef3b3Sdrh   static struct {
892c2eef3b3Sdrh      char *zName;
893c2eef3b3Sdrh      Tcl_CmdProc *xProc;
894c2eef3b3Sdrh   } aCmd[] = {
895c2eef3b3Sdrh      { "sqlite_mprintf_int",             (Tcl_CmdProc*)sqlite_mprintf_int    },
896c2eef3b3Sdrh      { "sqlite_mprintf_str",             (Tcl_CmdProc*)sqlite_mprintf_str    },
897c2eef3b3Sdrh      { "sqlite_mprintf_double",          (Tcl_CmdProc*)sqlite_mprintf_double },
898d93d8a81Sdrh      { "sqlite_mprintf_z_test",          (Tcl_CmdProc*)test_mprintf_z        },
899c2eef3b3Sdrh      { "sqlite_open",                    (Tcl_CmdProc*)sqlite_test_open      },
900c2eef3b3Sdrh      { "sqlite_last_insert_rowid",       (Tcl_CmdProc*)test_last_rowid       },
901c2eef3b3Sdrh      { "sqlite_exec_printf",             (Tcl_CmdProc*)test_exec_printf      },
902c2eef3b3Sdrh      { "sqlite_get_table_printf",        (Tcl_CmdProc*)test_get_table_printf },
903c2eef3b3Sdrh      { "sqlite_close",                   (Tcl_CmdProc*)sqlite_test_close     },
904c2eef3b3Sdrh      { "sqlite_create_function",         (Tcl_CmdProc*)test_create_function  },
905c2eef3b3Sdrh      { "sqlite_create_aggregate",        (Tcl_CmdProc*)test_create_aggregate },
906c2eef3b3Sdrh      { "sqlite_register_test_function",  (Tcl_CmdProc*)test_register_func    },
907c2eef3b3Sdrh      { "sqlite_abort",                   (Tcl_CmdProc*)sqlite_abort          },
9085a38705eSdrh      { "sqlite_datatypes",               (Tcl_CmdProc*)sqlite_datatypes      },
909c2eef3b3Sdrh #ifdef MEMORY_DEBUG
910c2eef3b3Sdrh      { "sqlite_malloc_fail",             (Tcl_CmdProc*)sqlite_malloc_fail    },
911c2eef3b3Sdrh      { "sqlite_malloc_stat",             (Tcl_CmdProc*)sqlite_malloc_stat    },
912c2eef3b3Sdrh #endif
913b86ccfb2Sdrh      { "sqlite_compile",                 (Tcl_CmdProc*)test_compile          },
914b86ccfb2Sdrh      { "sqlite_step",                    (Tcl_CmdProc*)test_step             },
915b86ccfb2Sdrh      { "sqlite_finalize",                (Tcl_CmdProc*)test_finalize         },
9167c972decSdrh      { "sqlite_bind",                    (Tcl_CmdProc*)test_bind             },
91750457896Sdrh      { "sqlite_reset",                   (Tcl_CmdProc*)test_reset            },
91899ee3600Sdrh      { "breakpoint",                     (Tcl_CmdProc*)test_breakpoint       },
919c2eef3b3Sdrh   };
920c2eef3b3Sdrh   int i;
921c2eef3b3Sdrh 
922c2eef3b3Sdrh   for(i=0; i<sizeof(aCmd)/sizeof(aCmd[0]); i++){
923c2eef3b3Sdrh     Tcl_CreateCommand(interp, aCmd[i].zName, aCmd[i].xProc, 0, 0);
924c2eef3b3Sdrh   }
925487ab3caSdrh   Tcl_LinkVar(interp, "sqlite_search_count",
926487ab3caSdrh       (char*)&sqlite_search_count, TCL_LINK_INT);
92794e92032Sdrh   Tcl_LinkVar(interp, "sqlite_open_file_count",
92894e92032Sdrh       (char*)&sqlite_open_file_count, TCL_LINK_INT);
929*e807febbSdrh   Tcl_LinkVar(interp, "sqlite_current_time",
930*e807febbSdrh       (char*)&sqlite_current_time, TCL_LINK_INT);
9317c972decSdrh   Tcl_LinkVar(interp, "sqlite_static_bind_value",
9327c972decSdrh       (char*)&sqlite_static_bind_value, TCL_LINK_STRING);
933d1bf3512Sdrh   return TCL_OK;
934d1bf3512Sdrh }
935