blob: 11ffb1e232e5981f1083089133370c9b83836a51 [file] [log] [blame]
drhd1bf3512001-04-07 15:24:33 +00001/*
drhb19a2bc2001-09-16 00:13:26 +00002** 2001 September 15
drhd1bf3512001-04-07 15:24:33 +00003**
drhb19a2bc2001-09-16 00:13:26 +00004** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
drhd1bf3512001-04-07 15:24:33 +00006**
drhb19a2bc2001-09-16 00:13:26 +00007** May you do good and not evil.
8** May you find forgiveness for yourself and forgive others.
9** May you share freely, never taking more than you give.
drhd1bf3512001-04-07 15:24:33 +000010**
11*************************************************************************
drh05a82982006-03-19 13:00:25 +000012** Code for testing all sorts of SQLite interfaces. This code
drhd1bf3512001-04-07 15:24:33 +000013** is not included in the SQLite library. It is used for automated
14** testing of the SQLite library.
15**
drhf3a65f72007-08-22 20:18:21 +000016** $Id: test1.c,v 1.270 2007/08/22 20:18:22 drh Exp $
drhd1bf3512001-04-07 15:24:33 +000017*/
18#include "sqliteInt.h"
19#include "tcl.h"
20#include <stdlib.h>
21#include <string.h>
22
drhdddca282006-01-03 00:33:50 +000023/*
24** This is a copy of the first part of the SqliteDb structure in
25** tclsqlite.c. We need it here so that the get_sqlite_pointer routine
26** can extract the sqlite3* pointer from an existing Tcl SQLite
27** connection.
28*/
29struct SqliteDb {
30 sqlite3 *db;
31};
32
33/*
drha3152892007-05-05 11:48:52 +000034** Convert text generated by the "%p" conversion format back into
35** a pointer.
36*/
37static int testHexToInt(int h){
38 if( h>='0' && h<='9' ){
39 return h - '0';
40 }else if( h>='a' && h<='f' ){
41 return h - 'a' + 10;
42 }else{
43 assert( h>='A' && h<='F' );
44 return h - 'A' + 10;
45 }
46}
47void *sqlite3TextToPtr(const char *z){
48 void *p;
49 u64 v;
50 u32 v2;
51 if( z[0]=='0' && z[1]=='x' ){
52 z += 2;
53 }
54 v = 0;
55 while( *z ){
56 v = (v<<4) + testHexToInt(*z);
57 z++;
58 }
59 if( sizeof(p)==sizeof(v) ){
60 memcpy(&p, &v, sizeof(p));
61 }else{
62 assert( sizeof(p)==sizeof(v2) );
63 v2 = (u32)v;
64 memcpy(&p, &v2, sizeof(p));
65 }
66 return p;
67}
68
69
70/*
drhdddca282006-01-03 00:33:50 +000071** A TCL command that returns the address of the sqlite* pointer
72** for an sqlite connection instance. Bad things happen if the
73** input is not an sqlite connection.
74*/
75static int get_sqlite_pointer(
76 void * clientData,
77 Tcl_Interp *interp,
78 int objc,
79 Tcl_Obj *CONST objv[]
80){
81 struct SqliteDb *p;
82 Tcl_CmdInfo cmdInfo;
83 char zBuf[100];
84 if( objc!=2 ){
85 Tcl_WrongNumArgs(interp, 1, objv, "SQLITE-CONNECTION");
86 return TCL_ERROR;
87 }
88 if( !Tcl_GetCommandInfo(interp, Tcl_GetString(objv[1]), &cmdInfo) ){
89 Tcl_AppendResult(interp, "command not found: ",
90 Tcl_GetString(objv[1]), (char*)0);
91 return TCL_ERROR;
92 }
93 p = (struct SqliteDb*)cmdInfo.objClientData;
94 sprintf(zBuf, "%p", p->db);
95 if( strncmp(zBuf,"0x",2) ){
96 sprintf(zBuf, "0x%p", p->db);
97 }
98 Tcl_AppendResult(interp, zBuf, 0);
99 return TCL_OK;
100}
101
drhb62c3352006-11-23 09:39:16 +0000102/*
103** Decode a pointer to an sqlite3 object.
104*/
105static int getDbPointer(Tcl_Interp *interp, const char *zA, sqlite3 **ppDb){
106 struct SqliteDb *p;
107 Tcl_CmdInfo cmdInfo;
108 if( Tcl_GetCommandInfo(interp, zA, &cmdInfo) ){
109 p = (struct SqliteDb*)cmdInfo.objClientData;
110 *ppDb = p->db;
111 }else{
112 *ppDb = (sqlite3*)sqlite3TextToPtr(zA);
113 }
114 return TCL_OK;
115}
116
117
drh2e66f0b2005-04-28 17:18:48 +0000118const char *sqlite3TestErrorName(int rc){
danielk19776622cce2004-05-20 11:00:52 +0000119 const char *zName = 0;
drh4ac285a2006-09-15 07:28:50 +0000120 switch( rc & 0xff ){
danielk19776622cce2004-05-20 11:00:52 +0000121 case SQLITE_OK: zName = "SQLITE_OK"; break;
122 case SQLITE_ERROR: zName = "SQLITE_ERROR"; break;
danielk19776622cce2004-05-20 11:00:52 +0000123 case SQLITE_PERM: zName = "SQLITE_PERM"; break;
124 case SQLITE_ABORT: zName = "SQLITE_ABORT"; break;
125 case SQLITE_BUSY: zName = "SQLITE_BUSY"; break;
126 case SQLITE_LOCKED: zName = "SQLITE_LOCKED"; break;
127 case SQLITE_NOMEM: zName = "SQLITE_NOMEM"; break;
128 case SQLITE_READONLY: zName = "SQLITE_READONLY"; break;
129 case SQLITE_INTERRUPT: zName = "SQLITE_INTERRUPT"; break;
130 case SQLITE_IOERR: zName = "SQLITE_IOERR"; break;
131 case SQLITE_CORRUPT: zName = "SQLITE_CORRUPT"; break;
danielk19776622cce2004-05-20 11:00:52 +0000132 case SQLITE_FULL: zName = "SQLITE_FULL"; break;
133 case SQLITE_CANTOPEN: zName = "SQLITE_CANTOPEN"; break;
134 case SQLITE_PROTOCOL: zName = "SQLITE_PROTOCOL"; break;
135 case SQLITE_EMPTY: zName = "SQLITE_EMPTY"; break;
136 case SQLITE_SCHEMA: zName = "SQLITE_SCHEMA"; break;
danielk19776622cce2004-05-20 11:00:52 +0000137 case SQLITE_CONSTRAINT: zName = "SQLITE_CONSTRAINT"; break;
138 case SQLITE_MISMATCH: zName = "SQLITE_MISMATCH"; break;
139 case SQLITE_MISUSE: zName = "SQLITE_MISUSE"; break;
140 case SQLITE_NOLFS: zName = "SQLITE_NOLFS"; break;
141 case SQLITE_AUTH: zName = "SQLITE_AUTH"; break;
142 case SQLITE_FORMAT: zName = "SQLITE_FORMAT"; break;
143 case SQLITE_RANGE: zName = "SQLITE_RANGE"; break;
144 case SQLITE_ROW: zName = "SQLITE_ROW"; break;
145 case SQLITE_DONE: zName = "SQLITE_DONE"; break;
drhc60d0442004-09-30 13:43:13 +0000146 case SQLITE_NOTADB: zName = "SQLITE_NOTADB"; break;
danielk19776622cce2004-05-20 11:00:52 +0000147 default: zName = "SQLITE_Unknown"; break;
148 }
149 return zName;
150}
drh4f0c5872007-03-26 22:05:01 +0000151#define t1ErrorName sqlite3TestErrorName
danielk19776622cce2004-05-20 11:00:52 +0000152
drhd1bf3512001-04-07 15:24:33 +0000153/*
drhc60d0442004-09-30 13:43:13 +0000154** Convert an sqlite3_stmt* into an sqlite3*. This depends on the
155** fact that the sqlite3* is the first field in the Vdbe structure.
156*/
drh51942bc2005-06-12 22:01:42 +0000157#define StmtToDb(X) sqlite3_db_handle(X)
drhc60d0442004-09-30 13:43:13 +0000158
159/*
160** Check a return value to make sure it agrees with the results
161** from sqlite3_errcode.
162*/
163int sqlite3TestErrCode(Tcl_Interp *interp, sqlite3 *db, int rc){
164 if( rc!=SQLITE_MISUSE && rc!=SQLITE_OK && sqlite3_errcode(db)!=rc ){
165 char zBuf[200];
166 int r2 = sqlite3_errcode(db);
167 sprintf(zBuf, "error code %s (%d) does not match sqlite3_errcode %s (%d)",
drh4f0c5872007-03-26 22:05:01 +0000168 t1ErrorName(rc), rc, t1ErrorName(r2), r2);
drhc60d0442004-09-30 13:43:13 +0000169 Tcl_ResetResult(interp);
170 Tcl_AppendResult(interp, zBuf, 0);
171 return 1;
172 }
173 return 0;
174}
175
176/*
danielk197751e3d8e2004-05-20 01:12:34 +0000177** Decode a pointer to an sqlite3_stmt object.
178*/
179static int getStmtPointer(
180 Tcl_Interp *interp,
181 const char *zArg,
182 sqlite3_stmt **ppStmt
183){
drhfe63d1c2004-09-08 20:13:04 +0000184 *ppStmt = (sqlite3_stmt*)sqlite3TextToPtr(zArg);
danielk197751e3d8e2004-05-20 01:12:34 +0000185 return TCL_OK;
186}
187
188/*
danielk19779a1d0ab2004-06-01 14:09:28 +0000189** Decode a pointer to an sqlite3_stmt object.
190*/
191static int getFilePointer(
192 Tcl_Interp *interp,
193 const char *zArg,
danielk19771e536952007-08-16 10:09:01 +0000194 sqlite3_file **ppFile
danielk19779a1d0ab2004-06-01 14:09:28 +0000195){
danielk19771e536952007-08-16 10:09:01 +0000196 *ppFile = (sqlite3_file*)sqlite3TextToPtr(zArg);
danielk19779a1d0ab2004-06-01 14:09:28 +0000197 return TCL_OK;
198}
199
200/*
drh7d8085a2003-04-26 13:19:38 +0000201** Generate a text representation of a pointer that can be understood
202** by the getDbPointer and getVmPointer routines above.
203**
204** The problem is, on some machines (Solaris) if you do a printf with
205** "%p" you cannot turn around and do a scanf with the same "%p" and
206** get your pointer back. You have to prepend a "0x" before it will
207** work. Or at least that is what is reported to me (drh). But this
208** behavior varies from machine to machine. The solution used her is
209** to test the string right after it is generated to see if it can be
210** understood by scanf, and if not, try prepending an "0x" to see if
211** that helps. If nothing works, a fatal error is generated.
212*/
drh64b1bea2006-01-15 02:30:57 +0000213int sqlite3TestMakePointerStr(Tcl_Interp *interp, char *zPtr, void *p){
drhfe63d1c2004-09-08 20:13:04 +0000214 sqlite3_snprintf(100, zPtr, "%p", p);
drh7d8085a2003-04-26 13:19:38 +0000215 return TCL_OK;
216}
217
218/*
danielk19776f8a5032004-05-10 10:34:51 +0000219** The callback routine for sqlite3_exec_printf().
drhd1bf3512001-04-07 15:24:33 +0000220*/
221static int exec_printf_cb(void *pArg, int argc, char **argv, char **name){
222 Tcl_DString *str = (Tcl_DString*)pArg;
223 int i;
224
225 if( Tcl_DStringLength(str)==0 ){
226 for(i=0; i<argc; i++){
227 Tcl_DStringAppendElement(str, name[i] ? name[i] : "NULL");
228 }
229 }
230 for(i=0; i<argc; i++){
231 Tcl_DStringAppendElement(str, argv[i] ? argv[i] : "NULL");
232 }
233 return 0;
234}
235
236/*
drh538f5702007-04-13 02:14:30 +0000237** The I/O tracing callback.
238*/
239static FILE *iotrace_file = 0;
240static void io_trace_callback(const char *zFormat, ...){
241 va_list ap;
242 va_start(ap, zFormat);
243 vfprintf(iotrace_file, zFormat, ap);
244 va_end(ap);
245 fflush(iotrace_file);
246}
247
248/*
249** Usage: io_trace FILENAME
250**
251** Turn I/O tracing on or off. If FILENAME is not an empty string,
252** I/O tracing begins going into FILENAME. If FILENAME is an empty
253** string, I/O tracing is turned off.
254*/
255static int test_io_trace(
256 void *NotUsed,
257 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
258 int argc, /* Number of arguments */
259 char **argv /* Text of each argument */
260){
261 if( argc!=2 ){
262 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
263 " FILENAME\"", 0);
264 return TCL_ERROR;
265 }
266 if( iotrace_file ){
267 if( iotrace_file!=stdout && iotrace_file!=stderr ){
268 fclose(iotrace_file);
269 }
270 iotrace_file = 0;
271 sqlite3_io_trace = 0;
272 }
273 if( argv[1][0] ){
274 if( strcmp(argv[1],"stdout")==0 ){
275 iotrace_file = stdout;
276 }else if( strcmp(argv[1],"stderr")==0 ){
277 iotrace_file = stderr;
278 }else{
279 iotrace_file = fopen(argv[1], "w");
280 }
281 sqlite3_io_trace = io_trace_callback;
282 }
283 return SQLITE_OK;
284}
285
286
287/*
danielk19776f8a5032004-05-10 10:34:51 +0000288** Usage: sqlite3_exec_printf DB FORMAT STRING
drhd1bf3512001-04-07 15:24:33 +0000289**
danielk19776f8a5032004-05-10 10:34:51 +0000290** Invoke the sqlite3_exec_printf() interface using the open database
drhd1bf3512001-04-07 15:24:33 +0000291** DB. The SQL is the string FORMAT. The format string should contain
292** one %s or %q. STRING is the value inserted into %s or %q.
293*/
294static int test_exec_printf(
295 void *NotUsed,
296 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
297 int argc, /* Number of arguments */
298 char **argv /* Text of each argument */
299){
drh9bb575f2004-09-06 17:24:11 +0000300 sqlite3 *db;
drhd1bf3512001-04-07 15:24:33 +0000301 Tcl_DString str;
302 int rc;
303 char *zErr = 0;
drh1211de32004-07-26 12:24:22 +0000304 char *zSql;
drhd1bf3512001-04-07 15:24:33 +0000305 char zBuf[30];
306 if( argc!=4 ){
307 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
308 " DB FORMAT STRING", 0);
309 return TCL_ERROR;
310 }
drhb86ccfb2003-01-28 23:13:10 +0000311 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
drhd1bf3512001-04-07 15:24:33 +0000312 Tcl_DStringInit(&str);
drh1211de32004-07-26 12:24:22 +0000313 zSql = sqlite3_mprintf(argv[2], argv[3]);
314 rc = sqlite3_exec(db, zSql, exec_printf_cb, &str, &zErr);
315 sqlite3_free(zSql);
drhd1bf3512001-04-07 15:24:33 +0000316 sprintf(zBuf, "%d", rc);
317 Tcl_AppendElement(interp, zBuf);
318 Tcl_AppendElement(interp, rc==SQLITE_OK ? Tcl_DStringValue(&str) : zErr);
319 Tcl_DStringFree(&str);
danielk1977926aab22006-06-27 07:34:40 +0000320 if( zErr ) sqlite3_free(zErr);
drhc60d0442004-09-30 13:43:13 +0000321 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
drhd1bf3512001-04-07 15:24:33 +0000322 return TCL_OK;
323}
324
325/*
drh27641702007-08-22 02:56:42 +0000326** Usage: db_enter DB
327** db_leave DB
328**
329** Enter or leave the mutex on a database connection.
330*/
331static int db_enter(
332 void *NotUsed,
333 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
334 int argc, /* Number of arguments */
335 char **argv /* Text of each argument */
336){
337 sqlite3 *db;
338 if( argc!=2 ){
339 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
340 " DB", 0);
341 return TCL_ERROR;
342 }
343 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
344 sqlite3_mutex_enter(db->mutex);
345 return TCL_OK;
346}
347static int db_leave(
348 void *NotUsed,
349 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
350 int argc, /* Number of arguments */
351 char **argv /* Text of each argument */
352){
353 sqlite3 *db;
354 if( argc!=2 ){
355 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
356 " DB", 0);
357 return TCL_ERROR;
358 }
359 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
360 sqlite3_mutex_leave(db->mutex);
361 return TCL_OK;
362}
363
364/*
drhb62c3352006-11-23 09:39:16 +0000365** Usage: sqlite3_exec DB SQL
366**
367** Invoke the sqlite3_exec interface using the open database DB
368*/
369static int test_exec(
370 void *NotUsed,
371 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
372 int argc, /* Number of arguments */
373 char **argv /* Text of each argument */
374){
375 sqlite3 *db;
376 Tcl_DString str;
377 int rc;
378 char *zErr = 0;
drh4e5dd852007-05-15 03:56:49 +0000379 char *zSql;
380 int i, j;
drhb62c3352006-11-23 09:39:16 +0000381 char zBuf[30];
382 if( argc!=3 ){
383 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
384 " DB SQL", 0);
385 return TCL_ERROR;
386 }
387 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
388 Tcl_DStringInit(&str);
drh4e5dd852007-05-15 03:56:49 +0000389 zSql = sqlite3_mprintf("%s", argv[2]);
390 for(i=j=0; zSql[i];){
391 if( zSql[i]=='%' ){
392 zSql[j++] = (testHexToInt(zSql[i+1])<<4) + testHexToInt(zSql[i+2]);
393 i += 3;
394 }else{
395 zSql[j++] = zSql[i++];
396 }
397 }
398 zSql[j] = 0;
399 rc = sqlite3_exec(db, zSql, exec_printf_cb, &str, &zErr);
400 sqlite3_free(zSql);
drhb62c3352006-11-23 09:39:16 +0000401 sprintf(zBuf, "%d", rc);
402 Tcl_AppendElement(interp, zBuf);
403 Tcl_AppendElement(interp, rc==SQLITE_OK ? Tcl_DStringValue(&str) : zErr);
404 Tcl_DStringFree(&str);
405 if( zErr ) sqlite3_free(zErr);
406 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
407 return TCL_OK;
408}
409
410/*
411** Usage: sqlite3_exec_nr DB SQL
412**
413** Invoke the sqlite3_exec interface using the open database DB. Discard
414** all results
415*/
416static int test_exec_nr(
417 void *NotUsed,
418 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
419 int argc, /* Number of arguments */
420 char **argv /* Text of each argument */
421){
422 sqlite3 *db;
423 int rc;
424 char *zErr = 0;
425 if( argc!=3 ){
426 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
427 " DB SQL", 0);
428 return TCL_ERROR;
429 }
430 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
431 rc = sqlite3_exec(db, argv[2], 0, 0, &zErr);
432 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
433 return TCL_OK;
434}
435
436/*
danielk19776f8a5032004-05-10 10:34:51 +0000437** Usage: sqlite3_mprintf_z_test SEPARATOR ARG0 ARG1 ...
drhd93d8a82003-06-16 03:08:18 +0000438**
drh05a82982006-03-19 13:00:25 +0000439** Test the %z format of sqliteMPrintf(). Use multiple mprintf() calls to
drhd93d8a82003-06-16 03:08:18 +0000440** concatenate arg0 through argn using separator as the separator.
441** Return the result.
442*/
443static int test_mprintf_z(
444 void *NotUsed,
445 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
446 int argc, /* Number of arguments */
447 char **argv /* Text of each argument */
448){
449 char *zResult = 0;
450 int i;
451
452 for(i=2; i<argc; i++){
danielk19771e536952007-08-16 10:09:01 +0000453 zResult = sqlite3MPrintf(0, "%z%s%s", zResult, argv[1], argv[i]);
drhd93d8a82003-06-16 03:08:18 +0000454 }
455 Tcl_AppendResult(interp, zResult, 0);
drh17435752007-08-16 04:30:38 +0000456 sqlite3_free(zResult);
drhd93d8a82003-06-16 03:08:18 +0000457 return TCL_OK;
458}
459
460/*
drh05a82982006-03-19 13:00:25 +0000461** Usage: sqlite3_mprintf_n_test STRING
462**
463** Test the %n format of sqliteMPrintf(). Return the length of the
464** input string.
465*/
466static int test_mprintf_n(
467 void *NotUsed,
468 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
469 int argc, /* Number of arguments */
470 char **argv /* Text of each argument */
471){
472 char *zStr;
473 int n = 0;
danielk19771e536952007-08-16 10:09:01 +0000474 zStr = sqlite3MPrintf(0, "%s%n", argv[1], &n);
drh17435752007-08-16 04:30:38 +0000475 sqlite3_free(zStr);
drh05a82982006-03-19 13:00:25 +0000476 Tcl_SetObjResult(interp, Tcl_NewIntObj(n));
477 return TCL_OK;
478}
479
480/*
drh68853902007-05-07 11:24:30 +0000481** Usage: sqlite3_snprintf_int SIZE FORMAT INT
482**
483** Test the of sqlite3_snprintf() routine. SIZE is the size of the
484** output buffer in bytes. The maximum size is 100. FORMAT is the
485** format string. INT is a single integer argument. The FORMAT
486** string must require no more than this one integer argument. If
487** You pass in a format string that requires more than one argument,
488** bad things will happen.
489*/
490static int test_snprintf_int(
491 void *NotUsed,
492 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
493 int argc, /* Number of arguments */
494 char **argv /* Text of each argument */
495){
496 char zStr[100];
497 int n = atoi(argv[1]);
drh68853902007-05-07 11:24:30 +0000498 const char *zFormat = argv[2];
499 int a1 = atoi(argv[3]);
drhdaf276d2007-06-15 18:53:14 +0000500 if( n>sizeof(zStr) ) n = sizeof(zStr);
drh68853902007-05-07 11:24:30 +0000501 strcpy(zStr, "abcdefghijklmnopqrstuvwxyz");
502 sqlite3_snprintf(n, zStr, zFormat, a1);
503 Tcl_AppendResult(interp, zStr, 0);
504 return TCL_OK;
505}
506
507/*
danielk19776f8a5032004-05-10 10:34:51 +0000508** Usage: sqlite3_get_table_printf DB FORMAT STRING
drhd1bf3512001-04-07 15:24:33 +0000509**
danielk19776f8a5032004-05-10 10:34:51 +0000510** Invoke the sqlite3_get_table_printf() interface using the open database
drhd1bf3512001-04-07 15:24:33 +0000511** DB. The SQL is the string FORMAT. The format string should contain
512** one %s or %q. STRING is the value inserted into %s or %q.
513*/
514static int test_get_table_printf(
515 void *NotUsed,
516 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
517 int argc, /* Number of arguments */
518 char **argv /* Text of each argument */
519){
drh9bb575f2004-09-06 17:24:11 +0000520 sqlite3 *db;
drhd1bf3512001-04-07 15:24:33 +0000521 Tcl_DString str;
522 int rc;
523 char *zErr = 0;
524 int nRow, nCol;
525 char **aResult;
526 int i;
527 char zBuf[30];
drh1211de32004-07-26 12:24:22 +0000528 char *zSql;
drhd1bf3512001-04-07 15:24:33 +0000529 if( argc!=4 ){
530 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
531 " DB FORMAT STRING", 0);
532 return TCL_ERROR;
533 }
drhb86ccfb2003-01-28 23:13:10 +0000534 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
drhd1bf3512001-04-07 15:24:33 +0000535 Tcl_DStringInit(&str);
drh1211de32004-07-26 12:24:22 +0000536 zSql = sqlite3_mprintf(argv[2],argv[3]);
537 rc = sqlite3_get_table(db, zSql, &aResult, &nRow, &nCol, &zErr);
538 sqlite3_free(zSql);
drhd1bf3512001-04-07 15:24:33 +0000539 sprintf(zBuf, "%d", rc);
540 Tcl_AppendElement(interp, zBuf);
541 if( rc==SQLITE_OK ){
542 sprintf(zBuf, "%d", nRow);
543 Tcl_AppendElement(interp, zBuf);
544 sprintf(zBuf, "%d", nCol);
545 Tcl_AppendElement(interp, zBuf);
546 for(i=0; i<(nRow+1)*nCol; i++){
547 Tcl_AppendElement(interp, aResult[i] ? aResult[i] : "NULL");
548 }
549 }else{
550 Tcl_AppendElement(interp, zErr);
551 }
danielk19776f8a5032004-05-10 10:34:51 +0000552 sqlite3_free_table(aResult);
danielk1977926aab22006-06-27 07:34:40 +0000553 if( zErr ) sqlite3_free(zErr);
drhc60d0442004-09-30 13:43:13 +0000554 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
drhd1bf3512001-04-07 15:24:33 +0000555 return TCL_OK;
556}
557
drhaf9ff332002-01-16 21:00:27 +0000558
559/*
danielk19776f8a5032004-05-10 10:34:51 +0000560** Usage: sqlite3_last_insert_rowid DB
drhaf9ff332002-01-16 21:00:27 +0000561**
562** Returns the integer ROWID of the most recent insert.
563*/
564static int test_last_rowid(
565 void *NotUsed,
566 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
567 int argc, /* Number of arguments */
568 char **argv /* Text of each argument */
569){
drh9bb575f2004-09-06 17:24:11 +0000570 sqlite3 *db;
drhaf9ff332002-01-16 21:00:27 +0000571 char zBuf[30];
572
573 if( argc!=2 ){
574 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " DB\"", 0);
575 return TCL_ERROR;
576 }
drhb86ccfb2003-01-28 23:13:10 +0000577 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
danielk1977c572ef72004-05-27 09:28:41 +0000578 sprintf(zBuf, "%lld", sqlite3_last_insert_rowid(db));
drhaf9ff332002-01-16 21:00:27 +0000579 Tcl_AppendResult(interp, zBuf, 0);
580 return SQLITE_OK;
581}
582
drhd1bf3512001-04-07 15:24:33 +0000583/*
drh25d65432004-07-22 15:02:25 +0000584** Usage: sqlite3_key DB KEY
585**
586** Set the codec key.
587*/
588static int test_key(
589 void *NotUsed,
590 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
591 int argc, /* Number of arguments */
592 char **argv /* Text of each argument */
593){
drh9bb575f2004-09-06 17:24:11 +0000594 sqlite3 *db;
drh25d65432004-07-22 15:02:25 +0000595 const char *zKey;
596 int nKey;
597 if( argc!=3 ){
598 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
599 " FILENAME\"", 0);
600 return TCL_ERROR;
601 }
602 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
603 zKey = argv[2];
604 nKey = strlen(zKey);
605#ifdef SQLITE_HAS_CODEC
606 sqlite3_key(db, zKey, nKey);
607#endif
608 return TCL_OK;
609}
610
611/*
612** Usage: sqlite3_rekey DB KEY
613**
614** Change the codec key.
615*/
616static int test_rekey(
617 void *NotUsed,
618 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
619 int argc, /* Number of arguments */
620 char **argv /* Text of each argument */
621){
drh9bb575f2004-09-06 17:24:11 +0000622 sqlite3 *db;
drh25d65432004-07-22 15:02:25 +0000623 const char *zKey;
624 int nKey;
625 if( argc!=3 ){
626 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
627 " FILENAME\"", 0);
628 return TCL_ERROR;
629 }
630 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
631 zKey = argv[2];
632 nKey = strlen(zKey);
633#ifdef SQLITE_HAS_CODEC
634 sqlite3_rekey(db, zKey, nKey);
635#endif
636 return TCL_OK;
637}
638
639/*
danielk19776f8a5032004-05-10 10:34:51 +0000640** Usage: sqlite3_close DB
drhd1bf3512001-04-07 15:24:33 +0000641**
danielk19776f8a5032004-05-10 10:34:51 +0000642** Closes the database opened by sqlite3_open.
drhd1bf3512001-04-07 15:24:33 +0000643*/
644static int sqlite_test_close(
645 void *NotUsed,
646 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
647 int argc, /* Number of arguments */
648 char **argv /* Text of each argument */
649){
drh9bb575f2004-09-06 17:24:11 +0000650 sqlite3 *db;
danielk197796d81f92004-06-19 03:33:57 +0000651 int rc;
drhd1bf3512001-04-07 15:24:33 +0000652 if( argc!=2 ){
653 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
654 " FILENAME\"", 0);
655 return TCL_ERROR;
656 }
drhb86ccfb2003-01-28 23:13:10 +0000657 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
danielk197796d81f92004-06-19 03:33:57 +0000658 rc = sqlite3_close(db);
drh4f0c5872007-03-26 22:05:01 +0000659 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
drhd1bf3512001-04-07 15:24:33 +0000660 return TCL_OK;
661}
662
663/*
drhc22bd472002-05-10 13:14:07 +0000664** Implementation of the x_coalesce() function.
665** Return the first argument non-NULL argument.
666*/
drh4f0c5872007-03-26 22:05:01 +0000667static void t1_ifnullFunc(
668 sqlite3_context *context,
669 int argc,
670 sqlite3_value **argv
671){
drhc22bd472002-05-10 13:14:07 +0000672 int i;
673 for(i=0; i<argc; i++){
drh9c054832004-05-31 18:51:57 +0000674 if( SQLITE_NULL!=sqlite3_value_type(argv[i]) ){
drh9310ef22007-04-27 17:16:20 +0000675 int n = sqlite3_value_bytes(argv[i]);
drh03d847e2005-12-09 20:21:58 +0000676 sqlite3_result_text(context, (char*)sqlite3_value_text(argv[i]),
drh9310ef22007-04-27 17:16:20 +0000677 n, SQLITE_TRANSIENT);
drhc22bd472002-05-10 13:14:07 +0000678 break;
679 }
680 }
681}
682
683/*
drhf0313812006-09-04 15:53:53 +0000684** These are test functions. hex8() interprets its argument as
685** UTF8 and returns a hex encoding. hex16le() interprets its argument
686** as UTF16le and returns a hex encoding.
687*/
688static void hex8Func(sqlite3_context *p, int argc, sqlite3_value **argv){
689 const unsigned char *z;
690 int i;
691 char zBuf[200];
692 z = sqlite3_value_text(argv[0]);
693 for(i=0; i<sizeof(zBuf)/2 - 2 && z[i]; i++){
694 sprintf(&zBuf[i*2], "%02x", z[i]&0xff);
695 }
696 zBuf[i*2] = 0;
697 sqlite3_result_text(p, (char*)zBuf, -1, SQLITE_TRANSIENT);
698}
drhaf304692007-04-23 23:56:31 +0000699#ifndef SQLITE_OMIT_UTF16
drhf0313812006-09-04 15:53:53 +0000700static void hex16Func(sqlite3_context *p, int argc, sqlite3_value **argv){
701 const unsigned short int *z;
702 int i;
703 char zBuf[400];
704 z = sqlite3_value_text16(argv[0]);
705 for(i=0; i<sizeof(zBuf)/4 - 4 && z[i]; i++){
706 sprintf(&zBuf[i*4], "%04x", z[i]&0xff);
707 }
708 zBuf[i*4] = 0;
709 sqlite3_result_text(p, (char*)zBuf, -1, SQLITE_TRANSIENT);
710}
drhaf304692007-04-23 23:56:31 +0000711#endif
drhf0313812006-09-04 15:53:53 +0000712
713/*
drhd1d9fc32004-01-07 19:24:48 +0000714** A structure into which to accumulate text.
715*/
716struct dstr {
717 int nAlloc; /* Space allocated */
718 int nUsed; /* Space used */
719 char *z; /* The space */
720};
721
722/*
723** Append text to a dstr
724*/
725static void dstrAppend(struct dstr *p, const char *z, int divider){
726 int n = strlen(z);
727 if( p->nUsed + n + 2 > p->nAlloc ){
728 char *zNew;
729 p->nAlloc = p->nAlloc*2 + n + 200;
drh17435752007-08-16 04:30:38 +0000730 zNew = sqlite3_realloc(p->z, p->nAlloc);
drhd1d9fc32004-01-07 19:24:48 +0000731 if( zNew==0 ){
drh17435752007-08-16 04:30:38 +0000732 sqlite3_free(p->z);
drhd1d9fc32004-01-07 19:24:48 +0000733 memset(p, 0, sizeof(*p));
734 return;
735 }
736 p->z = zNew;
737 }
738 if( divider && p->nUsed>0 ){
739 p->z[p->nUsed++] = divider;
740 }
741 memcpy(&p->z[p->nUsed], z, n+1);
742 p->nUsed += n;
743}
744
745/*
danielk19774adee202004-05-08 08:23:19 +0000746** Invoked for each callback from sqlite3ExecFunc
drhd1d9fc32004-01-07 19:24:48 +0000747*/
748static int execFuncCallback(void *pData, int argc, char **argv, char **NotUsed){
749 struct dstr *p = (struct dstr*)pData;
750 int i;
751 for(i=0; i<argc; i++){
752 if( argv[i]==0 ){
753 dstrAppend(p, "NULL", ' ');
754 }else{
755 dstrAppend(p, argv[i], ' ');
756 }
757 }
758 return 0;
759}
760
761/*
danielk1977e35ee192004-06-26 09:50:11 +0000762** Implementation of the x_sqlite_exec() function. This function takes
drhc22bd472002-05-10 13:14:07 +0000763** a single argument and attempts to execute that argument as SQL code.
drh6cbe1f12002-07-01 00:31:36 +0000764** This is illegal and should set the SQLITE_MISUSE flag on the database.
drhd1d9fc32004-01-07 19:24:48 +0000765**
danielk19776f8a5032004-05-10 10:34:51 +0000766** 2004-Jan-07: We have changed this to make it legal to call sqlite3_exec()
drhd1d9fc32004-01-07 19:24:48 +0000767** from within a function call.
drhc22bd472002-05-10 13:14:07 +0000768**
769** This routine simulates the effect of having two threads attempt to
770** use the same database at the same time.
771*/
danielk197751ad0ec2004-05-24 12:39:02 +0000772static void sqlite3ExecFunc(
danielk19770ae8b832004-05-25 12:05:56 +0000773 sqlite3_context *context,
danielk197751ad0ec2004-05-24 12:39:02 +0000774 int argc,
775 sqlite3_value **argv
776){
drhd1d9fc32004-01-07 19:24:48 +0000777 struct dstr x;
778 memset(&x, 0, sizeof(x));
drh37527852006-03-16 16:19:56 +0000779 (void)sqlite3_exec((sqlite3*)sqlite3_user_data(context),
drh03d847e2005-12-09 20:21:58 +0000780 (char*)sqlite3_value_text(argv[0]),
drhd1d9fc32004-01-07 19:24:48 +0000781 execFuncCallback, &x, 0);
danielk1977d8123362004-06-12 09:25:12 +0000782 sqlite3_result_text(context, x.z, x.nUsed, SQLITE_TRANSIENT);
drh17435752007-08-16 04:30:38 +0000783 sqlite3_free(x.z);
drhc22bd472002-05-10 13:14:07 +0000784}
785
786/*
danielk1977d7263922007-02-05 14:21:47 +0000787** Implementation of tkt2213func(), a scalar function that takes exactly
788** one argument. It has two interesting features:
789**
790** * It calls sqlite3_value_text() 3 times on the argument sqlite3_value*.
791** If the three pointers returned are not the same an SQL error is raised.
792**
793** * Otherwise it returns a copy of the text representation of it's
794** argument in such a way as the VDBE representation is a Mem* cell
795** with the MEM_Term flag clear.
796**
797** Ticket #2213 can therefore be tested by evaluating the following
798** SQL expression:
799**
800** tkt2213func(tkt2213func('a string'));
801*/
802static void tkt2213Function(
803 sqlite3_context *context,
804 int argc,
805 sqlite3_value **argv
806){
807 int nText;
808 unsigned char const *zText1;
809 unsigned char const *zText2;
810 unsigned char const *zText3;
811
812 nText = sqlite3_value_bytes(argv[0]);
813 zText1 = sqlite3_value_text(argv[0]);
814 zText2 = sqlite3_value_text(argv[0]);
815 zText3 = sqlite3_value_text(argv[0]);
816
817 if( zText1!=zText2 || zText2!=zText3 ){
818 sqlite3_result_error(context, "tkt2213 is not fixed", -1);
819 }else{
820 char *zCopy = (char *)sqlite3_malloc(nText);
821 memcpy(zCopy, zText1, nText);
822 sqlite3_result_text(context, zCopy, nText, sqlite3_free);
823 }
824}
825
826/*
drh9310ef22007-04-27 17:16:20 +0000827** The following SQL function takes 4 arguments. The 2nd and
828** 4th argument must be one of these strings: 'text', 'text16',
829** or 'blob' corresponding to API functions
830**
831** sqlite3_value_text()
832** sqlite3_value_text16()
833** sqlite3_value_blob()
834**
835** The third argument is a string, either 'bytes' or 'bytes16' or 'noop',
836** corresponding to APIs:
837**
838** sqlite3_value_bytes()
839** sqlite3_value_bytes16()
840** noop
841**
842** The APIs designated by the 2nd through 4th arguments are applied
843** to the first argument in order. If the pointers returned by the
844** second and fourth are different, this routine returns 1. Otherwise,
845** this routine returns 0.
846**
847** This function is used to test to see when returned pointers from
848** the _text(), _text16() and _blob() APIs become invalidated.
849*/
850static void ptrChngFunction(
851 sqlite3_context *context,
852 int argc,
853 sqlite3_value **argv
854){
855 const void *p1, *p2;
856 const char *zCmd;
857 if( argc!=4 ) return;
858 zCmd = (const char*)sqlite3_value_text(argv[1]);
859 if( zCmd==0 ) return;
860 if( strcmp(zCmd,"text")==0 ){
861 p1 = (const void*)sqlite3_value_text(argv[0]);
862#ifndef SQLITE_OMIT_UTF16
863 }else if( strcmp(zCmd, "text16")==0 ){
864 p1 = (const void*)sqlite3_value_text16(argv[0]);
865#endif
866 }else if( strcmp(zCmd, "blob")==0 ){
867 p1 = (const void*)sqlite3_value_blob(argv[0]);
868 }else{
869 return;
870 }
871 zCmd = (const char*)sqlite3_value_text(argv[2]);
872 if( zCmd==0 ) return;
873 if( strcmp(zCmd,"bytes")==0 ){
874 sqlite3_value_bytes(argv[0]);
875#ifndef SQLITE_OMIT_UTF16
876 }else if( strcmp(zCmd, "bytes16")==0 ){
877 sqlite3_value_bytes16(argv[0]);
878#endif
879 }else if( strcmp(zCmd, "noop")==0 ){
880 /* do nothing */
881 }else{
882 return;
883 }
884 zCmd = (const char*)sqlite3_value_text(argv[3]);
885 if( zCmd==0 ) return;
886 if( strcmp(zCmd,"text")==0 ){
887 p2 = (const void*)sqlite3_value_text(argv[0]);
888#ifndef SQLITE_OMIT_UTF16
889 }else if( strcmp(zCmd, "text16")==0 ){
890 p2 = (const void*)sqlite3_value_text16(argv[0]);
891#endif
892 }else if( strcmp(zCmd, "blob")==0 ){
893 p2 = (const void*)sqlite3_value_blob(argv[0]);
894 }else{
895 return;
896 }
897 sqlite3_result_int(context, p1!=p2);
898}
899
900
901/*
drhc22bd472002-05-10 13:14:07 +0000902** Usage: sqlite_test_create_function DB
903**
danielk19776f8a5032004-05-10 10:34:51 +0000904** Call the sqlite3_create_function API on the given database in order
drhc22bd472002-05-10 13:14:07 +0000905** to create a function named "x_coalesce". This function does the same thing
906** as the "coalesce" function. This function also registers an SQL function
danielk1977e35ee192004-06-26 09:50:11 +0000907** named "x_sqlite_exec" that invokes sqlite3_exec(). Invoking sqlite3_exec()
drhc22bd472002-05-10 13:14:07 +0000908** in this way is illegal recursion and should raise an SQLITE_MISUSE error.
909** The effect is similar to trying to use the same database connection from
910** two threads at the same time.
911**
912** The original motivation for this routine was to be able to call the
danielk19776f8a5032004-05-10 10:34:51 +0000913** sqlite3_create_function function while a query is in progress in order
drhc22bd472002-05-10 13:14:07 +0000914** to test the SQLITE_MISUSE detection logic.
915*/
drhc2eef3b2002-08-31 18:53:06 +0000916static int test_create_function(
drhc22bd472002-05-10 13:14:07 +0000917 void *NotUsed,
918 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
919 int argc, /* Number of arguments */
920 char **argv /* Text of each argument */
921){
drhc60d0442004-09-30 13:43:13 +0000922 int rc;
drh9bb575f2004-09-06 17:24:11 +0000923 sqlite3 *db;
drh9bb575f2004-09-06 17:24:11 +0000924 extern void Md5_Register(sqlite3*);
danielk1977312d6b32004-06-29 13:18:23 +0000925
drhc22bd472002-05-10 13:14:07 +0000926 if( argc!=2 ){
927 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
danielk19774397de52005-01-12 12:44:03 +0000928 " DB\"", 0);
drhc22bd472002-05-10 13:14:07 +0000929 return TCL_ERROR;
930 }
drhb86ccfb2003-01-28 23:13:10 +0000931 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
drhc60d0442004-09-30 13:43:13 +0000932 rc = sqlite3_create_function(db, "x_coalesce", -1, SQLITE_ANY, 0,
drh4f0c5872007-03-26 22:05:01 +0000933 t1_ifnullFunc, 0, 0);
drh235a8182006-09-13 19:21:28 +0000934 if( rc==SQLITE_OK ){
935 rc = sqlite3_create_function(db, "hex8", 1, SQLITE_ANY, 0,
936 hex8Func, 0, 0);
937 }
drhaf304692007-04-23 23:56:31 +0000938#ifndef SQLITE_OMIT_UTF16
drh235a8182006-09-13 19:21:28 +0000939 if( rc==SQLITE_OK ){
940 rc = sqlite3_create_function(db, "hex16", 1, SQLITE_ANY, 0,
941 hex16Func, 0, 0);
942 }
drhaf304692007-04-23 23:56:31 +0000943#endif
danielk1977d7263922007-02-05 14:21:47 +0000944 if( rc==SQLITE_OK ){
945 rc = sqlite3_create_function(db, "tkt2213func", 1, SQLITE_ANY, 0,
946 tkt2213Function, 0, 0);
947 }
drh9310ef22007-04-27 17:16:20 +0000948 if( rc==SQLITE_OK ){
949 rc = sqlite3_create_function(db, "pointer_change", 4, SQLITE_ANY, 0,
950 ptrChngFunction, 0, 0);
951 }
danielk1977312d6b32004-06-29 13:18:23 +0000952
drh5436dc22004-11-14 04:04:17 +0000953#ifndef SQLITE_OMIT_UTF16
danielk1977312d6b32004-06-29 13:18:23 +0000954 /* Use the sqlite3_create_function16() API here. Mainly for fun, but also
955 ** because it is not tested anywhere else. */
drhc60d0442004-09-30 13:43:13 +0000956 if( rc==SQLITE_OK ){
danielk1977576ec6b2005-01-21 11:55:25 +0000957 sqlite3_value *pVal;
drhf3a65f72007-08-22 20:18:21 +0000958 sqlite3_mutex_enter(db->mutex);
959 pVal = sqlite3ValueNew(db);
drhb21c8cd2007-08-21 19:33:56 +0000960 sqlite3ValueSetStr(pVal, -1, "x_sqlite_exec", SQLITE_UTF8, SQLITE_STATIC);
drhf3a65f72007-08-22 20:18:21 +0000961 if( db->mallocFailed ){
962 rc = SQLITE_NOMEM;
963 }else{
964 rc = sqlite3_create_function16(db,
drhb21c8cd2007-08-21 19:33:56 +0000965 sqlite3ValueText(pVal, SQLITE_UTF16NATIVE),
drhc60d0442004-09-30 13:43:13 +0000966 1, SQLITE_UTF16, db, sqlite3ExecFunc, 0, 0);
drhf3a65f72007-08-22 20:18:21 +0000967 }
drhc60d0442004-09-30 13:43:13 +0000968 sqlite3ValueFree(pVal);
drhf3a65f72007-08-22 20:18:21 +0000969 sqlite3_mutex_leave(db->mutex);
drhc60d0442004-09-30 13:43:13 +0000970 }
drh5436dc22004-11-14 04:04:17 +0000971#endif
972
drhc60d0442004-09-30 13:43:13 +0000973 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
drh4f0c5872007-03-26 22:05:01 +0000974 Tcl_SetResult(interp, (char *)t1ErrorName(rc), 0);
drhc22bd472002-05-10 13:14:07 +0000975 return TCL_OK;
976}
977
978/*
979** Routines to implement the x_count() aggregate function.
drh90669c12006-01-20 15:45:36 +0000980**
981** x_count() counts the number of non-null arguments. But there are
982** some twists for testing purposes.
983**
984** If the argument to x_count() is 40 then a UTF-8 error is reported
985** on the step function. If x_count(41) is seen, then a UTF-16 error
986** is reported on the step function. If the total count is 42, then
987** a UTF-8 error is reported on the finalize function.
drhc22bd472002-05-10 13:14:07 +0000988*/
drh4f0c5872007-03-26 22:05:01 +0000989typedef struct t1CountCtx t1CountCtx;
990struct t1CountCtx {
drhc22bd472002-05-10 13:14:07 +0000991 int n;
992};
drh4f0c5872007-03-26 22:05:01 +0000993static void t1CountStep(
994 sqlite3_context *context,
995 int argc,
996 sqlite3_value **argv
997){
998 t1CountCtx *p;
drh4f26d6c2004-05-26 23:25:30 +0000999 p = sqlite3_aggregate_context(context, sizeof(*p));
drh9c054832004-05-31 18:51:57 +00001000 if( (argc==0 || SQLITE_NULL!=sqlite3_value_type(argv[0]) ) && p ){
drhc22bd472002-05-10 13:14:07 +00001001 p->n++;
1002 }
drh90669c12006-01-20 15:45:36 +00001003 if( argc>0 ){
1004 int v = sqlite3_value_int(argv[0]);
1005 if( v==40 ){
1006 sqlite3_result_error(context, "value of 40 handed to x_count", -1);
danielk1977a1686c92006-01-23 07:52:37 +00001007#ifndef SQLITE_OMIT_UTF16
drh90669c12006-01-20 15:45:36 +00001008 }else if( v==41 ){
1009 const char zUtf16ErrMsg[] = { 0, 0x61, 0, 0x62, 0, 0x63, 0, 0, 0};
1010 sqlite3_result_error16(context, &zUtf16ErrMsg[1-SQLITE_BIGENDIAN], -1);
danielk1977a1686c92006-01-23 07:52:37 +00001011#endif
drh90669c12006-01-20 15:45:36 +00001012 }
1013 }
drhc22bd472002-05-10 13:14:07 +00001014}
drh4f0c5872007-03-26 22:05:01 +00001015static void t1CountFinalize(sqlite3_context *context){
1016 t1CountCtx *p;
drh4f26d6c2004-05-26 23:25:30 +00001017 p = sqlite3_aggregate_context(context, sizeof(*p));
drh90669c12006-01-20 15:45:36 +00001018 if( p ){
1019 if( p->n==42 ){
1020 sqlite3_result_error(context, "x_count totals to 42", -1);
1021 }else{
1022 sqlite3_result_int(context, p ? p->n : 0);
1023 }
1024 }
drhc22bd472002-05-10 13:14:07 +00001025}
1026
1027/*
1028** Usage: sqlite_test_create_aggregate DB
1029**
danielk19776f8a5032004-05-10 10:34:51 +00001030** Call the sqlite3_create_function API on the given database in order
drhc22bd472002-05-10 13:14:07 +00001031** to create a function named "x_count". This function does the same thing
1032** as the "md5sum" function.
1033**
1034** The original motivation for this routine was to be able to call the
danielk19776f8a5032004-05-10 10:34:51 +00001035** sqlite3_create_aggregate function while a query is in progress in order
drh90669c12006-01-20 15:45:36 +00001036** to test the SQLITE_MISUSE detection logic. See misuse.test.
1037**
1038** This routine was later extended to test the use of sqlite3_result_error()
1039** within aggregate functions.
drhc22bd472002-05-10 13:14:07 +00001040*/
drhc2eef3b2002-08-31 18:53:06 +00001041static int test_create_aggregate(
drhc22bd472002-05-10 13:14:07 +00001042 void *NotUsed,
1043 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1044 int argc, /* Number of arguments */
1045 char **argv /* Text of each argument */
1046){
drh9bb575f2004-09-06 17:24:11 +00001047 sqlite3 *db;
drhc60d0442004-09-30 13:43:13 +00001048 int rc;
drhc22bd472002-05-10 13:14:07 +00001049 if( argc!=2 ){
1050 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1051 " FILENAME\"", 0);
1052 return TCL_ERROR;
1053 }
drhb86ccfb2003-01-28 23:13:10 +00001054 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
drhc60d0442004-09-30 13:43:13 +00001055 rc = sqlite3_create_function(db, "x_count", 0, SQLITE_UTF8, 0, 0,
drh4f0c5872007-03-26 22:05:01 +00001056 t1CountStep,t1CountFinalize);
drhc60d0442004-09-30 13:43:13 +00001057 if( rc==SQLITE_OK ){
1058 sqlite3_create_function(db, "x_count", 1, SQLITE_UTF8, 0, 0,
drh4f0c5872007-03-26 22:05:01 +00001059 t1CountStep,t1CountFinalize);
drhc60d0442004-09-30 13:43:13 +00001060 }
1061 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
drhc22bd472002-05-10 13:14:07 +00001062 return TCL_OK;
1063}
1064
1065
drh3c23a882007-01-09 14:01:13 +00001066/*
1067** Usage: printf TEXT
1068**
1069** Send output to printf. Use this rather than puts to merge the output
1070** in the correct sequence with debugging printfs inserted into C code.
1071** Puts uses a separate buffer and debugging statements will be out of
1072** sequence if it is used.
1073*/
1074static int test_printf(
1075 void *NotUsed,
1076 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1077 int argc, /* Number of arguments */
1078 char **argv /* Text of each argument */
1079){
1080 if( argc!=2 ){
1081 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1082 " TEXT\"", 0);
1083 return TCL_ERROR;
1084 }
1085 printf("%s\n", argv[1]);
1086 return TCL_OK;
1087}
1088
1089
drhc22bd472002-05-10 13:14:07 +00001090
1091/*
danielk19776f8a5032004-05-10 10:34:51 +00001092** Usage: sqlite3_mprintf_int FORMAT INTEGER INTEGER INTEGER
drhd1bf3512001-04-07 15:24:33 +00001093**
1094** Call mprintf with three integer arguments
1095*/
danielk19776f8a5032004-05-10 10:34:51 +00001096static int sqlite3_mprintf_int(
drhd1bf3512001-04-07 15:24:33 +00001097 void *NotUsed,
1098 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1099 int argc, /* Number of arguments */
1100 char **argv /* Text of each argument */
1101){
1102 int a[3], i;
1103 char *z;
1104 if( argc!=5 ){
1105 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1106 " FORMAT INT INT INT\"", 0);
1107 return TCL_ERROR;
1108 }
1109 for(i=2; i<5; i++){
1110 if( Tcl_GetInt(interp, argv[i], &a[i-2]) ) return TCL_ERROR;
1111 }
danielk19776f8a5032004-05-10 10:34:51 +00001112 z = sqlite3_mprintf(argv[1], a[0], a[1], a[2]);
drhd1bf3512001-04-07 15:24:33 +00001113 Tcl_AppendResult(interp, z, 0);
drh3f4fedb2004-05-31 19:34:33 +00001114 sqlite3_free(z);
drhd1bf3512001-04-07 15:24:33 +00001115 return TCL_OK;
1116}
1117
1118/*
drh9d213ef2004-06-30 04:02:11 +00001119** If zNum represents an integer that will fit in 64-bits, then set
1120** *pValue to that integer and return true. Otherwise return false.
1121*/
1122static int sqlite3GetInt64(const char *zNum, i64 *pValue){
1123 if( sqlite3FitsIn64Bits(zNum) ){
drhb6a9ece2007-06-26 00:37:27 +00001124 sqlite3Atoi64(zNum, pValue);
drh9d213ef2004-06-30 04:02:11 +00001125 return 1;
1126 }
1127 return 0;
1128}
1129
1130/*
drhe9707672004-06-25 01:10:48 +00001131** Usage: sqlite3_mprintf_int64 FORMAT INTEGER INTEGER INTEGER
1132**
1133** Call mprintf with three 64-bit integer arguments
1134*/
1135static int sqlite3_mprintf_int64(
1136 void *NotUsed,
1137 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1138 int argc, /* Number of arguments */
1139 char **argv /* Text of each argument */
1140){
1141 int i;
1142 sqlite_int64 a[3];
1143 char *z;
1144 if( argc!=5 ){
1145 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1146 " FORMAT INT INT INT\"", 0);
1147 return TCL_ERROR;
1148 }
1149 for(i=2; i<5; i++){
1150 if( !sqlite3GetInt64(argv[i], &a[i-2]) ){
1151 Tcl_AppendResult(interp, "argument is not a valid 64-bit integer", 0);
1152 return TCL_ERROR;
1153 }
1154 }
1155 z = sqlite3_mprintf(argv[1], a[0], a[1], a[2]);
1156 Tcl_AppendResult(interp, z, 0);
1157 sqlite3_free(z);
1158 return TCL_OK;
1159}
1160
1161/*
danielk19776f8a5032004-05-10 10:34:51 +00001162** Usage: sqlite3_mprintf_str FORMAT INTEGER INTEGER STRING
drhd1bf3512001-04-07 15:24:33 +00001163**
1164** Call mprintf with two integer arguments and one string argument
1165*/
danielk19776f8a5032004-05-10 10:34:51 +00001166static int sqlite3_mprintf_str(
drhd1bf3512001-04-07 15:24:33 +00001167 void *NotUsed,
1168 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1169 int argc, /* Number of arguments */
1170 char **argv /* Text of each argument */
1171){
1172 int a[3], i;
1173 char *z;
chwf220b242002-06-16 04:54:28 +00001174 if( argc<4 || argc>5 ){
drhd1bf3512001-04-07 15:24:33 +00001175 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
chwf220b242002-06-16 04:54:28 +00001176 " FORMAT INT INT ?STRING?\"", 0);
drhd1bf3512001-04-07 15:24:33 +00001177 return TCL_ERROR;
1178 }
1179 for(i=2; i<4; i++){
1180 if( Tcl_GetInt(interp, argv[i], &a[i-2]) ) return TCL_ERROR;
1181 }
danielk19776f8a5032004-05-10 10:34:51 +00001182 z = sqlite3_mprintf(argv[1], a[0], a[1], argc>4 ? argv[4] : NULL);
drhd1bf3512001-04-07 15:24:33 +00001183 Tcl_AppendResult(interp, z, 0);
drh3f4fedb2004-05-31 19:34:33 +00001184 sqlite3_free(z);
drhd1bf3512001-04-07 15:24:33 +00001185 return TCL_OK;
1186}
1187
1188/*
drhb3738b62007-03-31 15:02:49 +00001189** Usage: sqlite3_snprintf_str INTEGER FORMAT INTEGER INTEGER STRING
1190**
1191** Call mprintf with two integer arguments and one string argument
1192*/
1193static int sqlite3_snprintf_str(
1194 void *NotUsed,
1195 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1196 int argc, /* Number of arguments */
1197 char **argv /* Text of each argument */
1198){
1199 int a[3], i;
1200 int n;
1201 char *z;
1202 if( argc<5 || argc>6 ){
1203 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1204 " INT FORMAT INT INT ?STRING?\"", 0);
1205 return TCL_ERROR;
1206 }
1207 if( Tcl_GetInt(interp, argv[1], &n) ) return TCL_ERROR;
1208 if( n<0 ){
1209 Tcl_AppendResult(interp, "N must be non-negative", 0);
1210 return TCL_ERROR;
1211 }
1212 for(i=3; i<5; i++){
1213 if( Tcl_GetInt(interp, argv[i], &a[i-3]) ) return TCL_ERROR;
1214 }
1215 z = sqlite3_malloc( n+1 );
1216 sqlite3_snprintf(n, z, argv[2], a[0], a[1], argc>4 ? argv[5] : NULL);
1217 Tcl_AppendResult(interp, z, 0);
1218 sqlite3_free(z);
1219 return TCL_OK;
1220}
1221
1222/*
drh63782852005-08-30 19:30:59 +00001223** Usage: sqlite3_mprintf_double FORMAT INTEGER INTEGER DOUBLE
drhd1bf3512001-04-07 15:24:33 +00001224**
1225** Call mprintf with two integer arguments and one double argument
1226*/
danielk19776f8a5032004-05-10 10:34:51 +00001227static int sqlite3_mprintf_double(
drhd1bf3512001-04-07 15:24:33 +00001228 void *NotUsed,
1229 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1230 int argc, /* Number of arguments */
1231 char **argv /* Text of each argument */
1232){
1233 int a[3], i;
1234 double r;
1235 char *z;
1236 if( argc!=5 ){
1237 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
drh63782852005-08-30 19:30:59 +00001238 " FORMAT INT INT DOUBLE\"", 0);
drhd1bf3512001-04-07 15:24:33 +00001239 return TCL_ERROR;
1240 }
1241 for(i=2; i<4; i++){
1242 if( Tcl_GetInt(interp, argv[i], &a[i-2]) ) return TCL_ERROR;
1243 }
1244 if( Tcl_GetDouble(interp, argv[4], &r) ) return TCL_ERROR;
danielk19776f8a5032004-05-10 10:34:51 +00001245 z = sqlite3_mprintf(argv[1], a[0], a[1], r);
drhd1bf3512001-04-07 15:24:33 +00001246 Tcl_AppendResult(interp, z, 0);
drh3f4fedb2004-05-31 19:34:33 +00001247 sqlite3_free(z);
drhd1bf3512001-04-07 15:24:33 +00001248 return TCL_OK;
1249}
1250
1251/*
drh63782852005-08-30 19:30:59 +00001252** Usage: sqlite3_mprintf_scaled FORMAT DOUBLE DOUBLE
drhb621c232004-02-21 19:41:04 +00001253**
1254** Call mprintf with a single double argument which is the product of the
1255** two arguments given above. This is used to generate overflow and underflow
1256** doubles to test that they are converted properly.
1257*/
danielk19776f8a5032004-05-10 10:34:51 +00001258static int sqlite3_mprintf_scaled(
drhb621c232004-02-21 19:41:04 +00001259 void *NotUsed,
1260 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1261 int argc, /* Number of arguments */
1262 char **argv /* Text of each argument */
1263){
1264 int i;
1265 double r[2];
1266 char *z;
1267 if( argc!=4 ){
1268 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1269 " FORMAT DOUBLE DOUBLE\"", 0);
1270 return TCL_ERROR;
1271 }
1272 for(i=2; i<4; i++){
1273 if( Tcl_GetDouble(interp, argv[i], &r[i-2]) ) return TCL_ERROR;
1274 }
danielk19776f8a5032004-05-10 10:34:51 +00001275 z = sqlite3_mprintf(argv[1], r[0]*r[1]);
drhb621c232004-02-21 19:41:04 +00001276 Tcl_AppendResult(interp, z, 0);
drh3f4fedb2004-05-31 19:34:33 +00001277 sqlite3_free(z);
drhb621c232004-02-21 19:41:04 +00001278 return TCL_OK;
1279}
1280
1281/*
drhe29b1a02004-07-17 21:56:09 +00001282** Usage: sqlite3_mprintf_stronly FORMAT STRING
1283**
1284** Call mprintf with a single double argument which is the product of the
1285** two arguments given above. This is used to generate overflow and underflow
1286** doubles to test that they are converted properly.
1287*/
1288static int sqlite3_mprintf_stronly(
1289 void *NotUsed,
1290 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1291 int argc, /* Number of arguments */
1292 char **argv /* Text of each argument */
1293){
1294 char *z;
1295 if( argc!=3 ){
1296 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1297 " FORMAT STRING\"", 0);
1298 return TCL_ERROR;
1299 }
1300 z = sqlite3_mprintf(argv[1], argv[2]);
1301 Tcl_AppendResult(interp, z, 0);
1302 sqlite3_free(z);
1303 return TCL_OK;
1304}
1305
1306/*
drh63782852005-08-30 19:30:59 +00001307** Usage: sqlite3_mprintf_hexdouble FORMAT HEX
1308**
1309** Call mprintf with a single double argument which is derived from the
1310** hexadecimal encoding of an IEEE double.
1311*/
1312static int sqlite3_mprintf_hexdouble(
1313 void *NotUsed,
1314 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1315 int argc, /* Number of arguments */
1316 char **argv /* Text of each argument */
1317){
1318 char *z;
1319 double r;
1320 unsigned x1, x2;
1321 long long unsigned d;
1322 if( argc!=3 ){
1323 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1324 " FORMAT STRING\"", 0);
1325 return TCL_ERROR;
1326 }
1327 if( sscanf(argv[2], "%08x%08x", &x2, &x1)!=2 ){
1328 Tcl_AppendResult(interp, "2nd argument should be 16-characters of hex", 0);
1329 return TCL_ERROR;
1330 }
1331 d = x2;
1332 d = (d<<32) + x1;
1333 memcpy(&r, &d, sizeof(r));
1334 z = sqlite3_mprintf(argv[1], r);
1335 Tcl_AppendResult(interp, z, 0);
1336 sqlite3_free(z);
1337 return TCL_OK;
1338}
1339
1340/*
danielk197752622822006-01-09 09:59:49 +00001341** Usage: sqlite3_enable_shared_cache BOOLEAN
danielk1977aef0bf62005-12-30 16:28:01 +00001342**
1343*/
drh6f7adc82006-01-11 21:41:20 +00001344#if !defined(SQLITE_OMIT_SHARED_CACHE)
1345static int test_enable_shared(
danielk197752622822006-01-09 09:59:49 +00001346 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
danielk1977aef0bf62005-12-30 16:28:01 +00001347 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1348 int objc, /* Number of arguments */
1349 Tcl_Obj *CONST objv[] /* Command arguments */
1350){
1351 int rc;
1352 int enable;
danielk197752622822006-01-09 09:59:49 +00001353 int ret = 0;
drhe53831d2007-08-17 01:14:38 +00001354 extern int sqlite3SharedCacheEnabled;
danielk1977aef0bf62005-12-30 16:28:01 +00001355
1356 if( objc!=2 ){
1357 Tcl_WrongNumArgs(interp, 1, objv, "BOOLEAN");
1358 return TCL_ERROR;
1359 }
1360 if( Tcl_GetBooleanFromObj(interp, objv[1], &enable) ){
1361 return TCL_ERROR;
1362 }
drhe53831d2007-08-17 01:14:38 +00001363 ret = sqlite3SharedCacheEnabled;
drh6f7adc82006-01-11 21:41:20 +00001364 rc = sqlite3_enable_shared_cache(enable);
danielk1977aef0bf62005-12-30 16:28:01 +00001365 if( rc!=SQLITE_OK ){
1366 Tcl_SetResult(interp, (char *)sqlite3ErrStr(rc), TCL_STATIC);
1367 return TCL_ERROR;
1368 }
danielk197752622822006-01-09 09:59:49 +00001369 Tcl_SetObjResult(interp, Tcl_NewBooleanObj(ret));
danielk1977aef0bf62005-12-30 16:28:01 +00001370 return TCL_OK;
1371}
1372#endif
1373
drh16a9b832007-05-05 18:39:25 +00001374
1375
danielk1977aef0bf62005-12-30 16:28:01 +00001376/*
drh4ac285a2006-09-15 07:28:50 +00001377** Usage: sqlite3_extended_result_codes DB BOOLEAN
1378**
1379*/
1380static int test_extended_result_codes(
1381 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
1382 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1383 int objc, /* Number of arguments */
1384 Tcl_Obj *CONST objv[] /* Command arguments */
1385){
1386 int enable;
1387 sqlite3 *db;
1388
1389 if( objc!=3 ){
1390 Tcl_WrongNumArgs(interp, 1, objv, "DB BOOLEAN");
1391 return TCL_ERROR;
1392 }
1393 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
1394 if( Tcl_GetBooleanFromObj(interp, objv[2], &enable) ) return TCL_ERROR;
1395 sqlite3_extended_result_codes(db, enable);
1396 return TCL_OK;
1397}
1398
1399/*
danielk1977161fb792006-01-24 10:58:21 +00001400** Usage: sqlite3_libversion_number
1401**
1402*/
1403static int test_libversion_number(
1404 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
1405 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1406 int objc, /* Number of arguments */
1407 Tcl_Obj *CONST objv[] /* Command arguments */
1408){
1409 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_libversion_number()));
1410 return TCL_OK;
1411}
1412
1413/*
danielk1977deb802c2006-02-09 13:43:28 +00001414** Usage: sqlite3_table_column_metadata DB dbname tblname colname
1415**
1416*/
1417#ifdef SQLITE_ENABLE_COLUMN_METADATA
1418static int test_table_column_metadata(
1419 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
1420 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1421 int objc, /* Number of arguments */
1422 Tcl_Obj *CONST objv[] /* Command arguments */
1423){
1424 sqlite3 *db;
1425 const char *zDb;
1426 const char *zTbl;
1427 const char *zCol;
1428 int rc;
1429 Tcl_Obj *pRet;
1430
1431 const char *zDatatype;
1432 const char *zCollseq;
1433 int notnull;
1434 int primarykey;
1435 int autoincrement;
1436
1437 if( objc!=5 ){
1438 Tcl_WrongNumArgs(interp, 1, objv, "DB dbname tblname colname");
1439 return TCL_ERROR;
1440 }
1441 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
1442 zDb = Tcl_GetString(objv[2]);
1443 zTbl = Tcl_GetString(objv[3]);
1444 zCol = Tcl_GetString(objv[4]);
1445
1446 if( strlen(zDb)==0 ) zDb = 0;
1447
1448 rc = sqlite3_table_column_metadata(db, zDb, zTbl, zCol,
1449 &zDatatype, &zCollseq, &notnull, &primarykey, &autoincrement);
1450
1451 if( rc!=SQLITE_OK ){
1452 Tcl_AppendResult(interp, sqlite3_errmsg(db), 0);
1453 return TCL_ERROR;
1454 }
1455
1456 pRet = Tcl_NewObj();
1457 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zDatatype, -1));
1458 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zCollseq, -1));
1459 Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(notnull));
1460 Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(primarykey));
1461 Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(autoincrement));
1462 Tcl_SetObjResult(interp, pRet);
1463
1464 return TCL_OK;
1465}
1466#endif
1467
danielk1977dcbb5d32007-05-04 18:36:44 +00001468#ifndef SQLITE_OMIT_INCRBLOB
1469
1470/*
1471** sqlite3_blob_read CHANNEL OFFSET N
danielk1977a9808b32007-05-07 09:32:45 +00001472**
1473** This command is used to test the sqlite3_blob_read() in ways that
1474** the Tcl channel interface does not. The first argument should
1475** be the name of a valid channel created by the [incrblob] method
1476** of a database handle. This function calls sqlite3_blob_read()
1477** to read N bytes from offset OFFSET from the underlying SQLite
1478** blob handle.
1479**
1480** On success, a byte-array object containing the read data is
1481** returned. On failure, the interpreter result is set to the
1482** text representation of the returned error code (i.e. "SQLITE_NOMEM")
1483** and a Tcl exception is thrown.
danielk1977dcbb5d32007-05-04 18:36:44 +00001484*/
1485static int test_blob_read(
1486 ClientData clientData, /* Not used */
1487 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1488 int objc, /* Number of arguments */
1489 Tcl_Obj *CONST objv[] /* Command arguments */
1490){
1491 Tcl_Channel channel;
1492 ClientData instanceData;
1493 sqlite3_blob *pBlob;
1494 int notUsed;
1495 int nByte;
1496 int iOffset;
1497 unsigned char *zBuf;
1498 int rc;
1499
1500 if( objc!=4 ){
1501 Tcl_WrongNumArgs(interp, 1, objv, "CHANNEL OFFSET N");
1502 return TCL_ERROR;
1503 }
1504
1505 channel = Tcl_GetChannel(interp, Tcl_GetString(objv[1]), &notUsed);
1506 if( !channel
1507 || TCL_OK!=Tcl_GetIntFromObj(interp, objv[2], &iOffset)
1508 || TCL_OK!=Tcl_GetIntFromObj(interp, objv[3], &nByte)
1509 || nByte<0 || iOffset<0
1510 ){
1511 return TCL_ERROR;
1512 }
1513
1514 instanceData = Tcl_GetChannelInstanceData(channel);
1515 pBlob = *((sqlite3_blob **)instanceData);
1516
1517 zBuf = (unsigned char *)Tcl_Alloc(nByte);
1518 rc = sqlite3_blob_read(pBlob, zBuf, nByte, iOffset);
1519 if( rc==SQLITE_OK ){
1520 Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(zBuf, nByte));
1521 }else{
1522 Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_VOLATILE);
1523 }
1524 Tcl_Free((char *)zBuf);
1525
1526 return (rc==SQLITE_OK ? TCL_OK : TCL_ERROR);
1527}
1528
1529/*
1530** sqlite3_blob_write CHANNEL OFFSET DATA
danielk1977a9808b32007-05-07 09:32:45 +00001531**
1532** This command is used to test the sqlite3_blob_write() in ways that
1533** the Tcl channel interface does not. The first argument should
1534** be the name of a valid channel created by the [incrblob] method
1535** of a database handle. This function calls sqlite3_blob_write()
1536** to write the DATA byte-array to the underlying SQLite blob handle.
1537** at offset OFFSET.
1538**
1539** On success, an empty string is returned. On failure, the interpreter
1540** result is set to the text representation of the returned error code
1541** (i.e. "SQLITE_NOMEM") and a Tcl exception is thrown.
danielk1977dcbb5d32007-05-04 18:36:44 +00001542*/
1543static int test_blob_write(
1544 ClientData clientData, /* Not used */
1545 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1546 int objc, /* Number of arguments */
1547 Tcl_Obj *CONST objv[] /* Command arguments */
1548){
1549 Tcl_Channel channel;
1550 ClientData instanceData;
1551 sqlite3_blob *pBlob;
1552 int notUsed;
1553 int iOffset;
1554 int rc;
1555
1556 unsigned char *zBuf;
1557 int nBuf;
1558
1559 if( objc!=4 ){
1560 Tcl_WrongNumArgs(interp, 1, objv, "CHANNEL OFFSET DATA");
1561 return TCL_ERROR;
1562 }
1563
1564 channel = Tcl_GetChannel(interp, Tcl_GetString(objv[1]), &notUsed);
1565 if( !channel
1566 || TCL_OK!=Tcl_GetIntFromObj(interp, objv[2], &iOffset)
1567 || iOffset<0
1568 ){
1569 return TCL_ERROR;
1570 }
1571
1572 instanceData = Tcl_GetChannelInstanceData(channel);
1573 pBlob = *((sqlite3_blob **)instanceData);
1574
1575 zBuf = Tcl_GetByteArrayFromObj(objv[3], &nBuf);
1576 rc = sqlite3_blob_write(pBlob, zBuf, nBuf, iOffset);
1577 if( rc!=SQLITE_OK ){
1578 Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_VOLATILE);
1579 }
1580
1581 return (rc==SQLITE_OK ? TCL_OK : TCL_ERROR);
1582}
1583#endif
drhc2e87a32006-06-27 15:16:14 +00001584
danielk1977deb802c2006-02-09 13:43:28 +00001585/*
danielk1977a393c032007-05-07 14:58:53 +00001586** Usage: sqlite3_create_collation_v2 DB-HANDLE NAME CMP-PROC DEL-PROC
danielk1977a9808b32007-05-07 09:32:45 +00001587**
1588** This Tcl proc is used for testing the experimental
danielk1977a393c032007-05-07 14:58:53 +00001589** sqlite3_create_collation_v2() interface.
danielk1977a9808b32007-05-07 09:32:45 +00001590*/
1591struct TestCollationX {
1592 Tcl_Interp *interp;
1593 Tcl_Obj *pCmp;
1594 Tcl_Obj *pDel;
1595};
1596typedef struct TestCollationX TestCollationX;
1597static void testCreateCollationDel(void *pCtx){
1598 TestCollationX *p = (TestCollationX *)pCtx;
1599
1600 int rc = Tcl_EvalObjEx(p->interp, p->pDel, TCL_EVAL_DIRECT|TCL_EVAL_GLOBAL);
1601 if( rc!=TCL_OK ){
1602 Tcl_BackgroundError(p->interp);
1603 }
1604
1605 Tcl_DecrRefCount(p->pCmp);
1606 Tcl_DecrRefCount(p->pDel);
1607 sqlite3_free((void *)p);
1608}
1609static int testCreateCollationCmp(
1610 void *pCtx,
1611 int nLeft,
1612 const void *zLeft,
1613 int nRight,
1614 const void *zRight
1615){
1616 TestCollationX *p = (TestCollationX *)pCtx;
1617 Tcl_Obj *pScript = Tcl_DuplicateObj(p->pCmp);
1618 int iRes = 0;
1619
1620 Tcl_IncrRefCount(pScript);
1621 Tcl_ListObjAppendElement(0, pScript, Tcl_NewStringObj((char *)zLeft, nLeft));
1622 Tcl_ListObjAppendElement(0, pScript, Tcl_NewStringObj((char *)zRight,nRight));
1623
1624 if( TCL_OK!=Tcl_EvalObjEx(p->interp, pScript, TCL_EVAL_DIRECT|TCL_EVAL_GLOBAL)
1625 || TCL_OK!=Tcl_GetIntFromObj(p->interp, Tcl_GetObjResult(p->interp), &iRes)
1626 ){
1627 Tcl_BackgroundError(p->interp);
1628 }
1629 Tcl_DecrRefCount(pScript);
1630
1631 return iRes;
1632}
danielk1977a393c032007-05-07 14:58:53 +00001633static int test_create_collation_v2(
danielk1977a9808b32007-05-07 09:32:45 +00001634 ClientData clientData, /* Not used */
1635 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1636 int objc, /* Number of arguments */
1637 Tcl_Obj *CONST objv[] /* Command arguments */
1638){
1639 TestCollationX *p;
1640 sqlite3 *db;
1641
1642 if( objc!=5 ){
1643 Tcl_WrongNumArgs(interp, 1, objv, "DB-HANDLE NAME CMP-PROC DEL-PROC");
1644 return TCL_ERROR;
1645 }
1646 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
1647
1648 p = (TestCollationX *)sqlite3_malloc(sizeof(TestCollationX));
1649 p->pCmp = objv[3];
1650 p->pDel = objv[4];
1651 p->interp = interp;
1652 Tcl_IncrRefCount(p->pCmp);
1653 Tcl_IncrRefCount(p->pDel);
1654
danielk1977a393c032007-05-07 14:58:53 +00001655 sqlite3_create_collation_v2(db, Tcl_GetString(objv[2]), SQLITE_UTF8,
danielk1977a9808b32007-05-07 09:32:45 +00001656 (void *)p, testCreateCollationCmp, testCreateCollationDel
1657 );
1658 return TCL_OK;
1659}
1660
1661/*
danielk197769e777f2006-06-14 10:38:02 +00001662** Usage: sqlite3_load_extension DB-HANDLE FILE ?PROC?
1663*/
1664static int test_load_extension(
1665 ClientData clientData, /* Not used */
1666 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1667 int objc, /* Number of arguments */
1668 Tcl_Obj *CONST objv[] /* Command arguments */
1669){
1670 Tcl_CmdInfo cmdInfo;
1671 sqlite3 *db;
1672 int rc;
1673 char *zDb;
1674 char *zFile;
1675 char *zProc = 0;
1676 char *zErr = 0;
1677
1678 if( objc!=4 && objc!=3 ){
1679 Tcl_WrongNumArgs(interp, 1, objv, "DB-HANDLE FILE ?PROC?");
1680 return TCL_ERROR;
1681 }
1682 zDb = Tcl_GetString(objv[1]);
1683 zFile = Tcl_GetString(objv[2]);
1684 if( objc==4 ){
1685 zProc = Tcl_GetString(objv[3]);
1686 }
1687
1688 /* Extract the C database handle from the Tcl command name */
1689 if( !Tcl_GetCommandInfo(interp, zDb, &cmdInfo) ){
1690 Tcl_AppendResult(interp, "command not found: ", zDb, (char*)0);
1691 return TCL_ERROR;
1692 }
1693 db = ((struct SqliteDb*)cmdInfo.objClientData)->db;
1694 assert(db);
1695
1696 /* Call the underlying C function. If an error occurs, set rc to
1697 ** TCL_ERROR and load any error string into the interpreter. If no
1698 ** error occurs, set rc to TCL_OK.
1699 */
drhc2e87a32006-06-27 15:16:14 +00001700#ifdef SQLITE_OMIT_LOAD_EXTENSION
1701 rc = SQLITE_ERROR;
1702 zErr = sqlite3_mprintf("this build omits sqlite3_load_extension()");
1703#else
danielk197769e777f2006-06-14 10:38:02 +00001704 rc = sqlite3_load_extension(db, zFile, zProc, &zErr);
drhc2e87a32006-06-27 15:16:14 +00001705#endif
danielk197769e777f2006-06-14 10:38:02 +00001706 if( rc!=SQLITE_OK ){
1707 Tcl_SetResult(interp, zErr ? zErr : "", TCL_VOLATILE);
1708 rc = TCL_ERROR;
1709 }else{
1710 rc = TCL_OK;
1711 }
1712 sqlite3_free(zErr);
1713
1714 return rc;
1715}
1716
1717/*
drhc2e87a32006-06-27 15:16:14 +00001718** Usage: sqlite3_enable_load_extension DB-HANDLE ONOFF
1719*/
1720static int test_enable_load(
1721 ClientData clientData, /* Not used */
1722 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1723 int objc, /* Number of arguments */
1724 Tcl_Obj *CONST objv[] /* Command arguments */
1725){
1726 Tcl_CmdInfo cmdInfo;
1727 sqlite3 *db;
1728 char *zDb;
1729 int onoff;
1730
1731 if( objc!=3 ){
1732 Tcl_WrongNumArgs(interp, 1, objv, "DB-HANDLE ONOFF");
1733 return TCL_ERROR;
1734 }
1735 zDb = Tcl_GetString(objv[1]);
1736
1737 /* Extract the C database handle from the Tcl command name */
1738 if( !Tcl_GetCommandInfo(interp, zDb, &cmdInfo) ){
1739 Tcl_AppendResult(interp, "command not found: ", zDb, (char*)0);
1740 return TCL_ERROR;
1741 }
1742 db = ((struct SqliteDb*)cmdInfo.objClientData)->db;
1743 assert(db);
1744
1745 /* Get the onoff parameter */
1746 if( Tcl_GetBooleanFromObj(interp, objv[2], &onoff) ){
1747 return TCL_ERROR;
1748 }
1749
1750#ifdef SQLITE_OMIT_LOAD_EXTENSION
1751 Tcl_AppendResult(interp, "this build omits sqlite3_load_extension()");
1752 return TCL_ERROR;
1753#else
1754 sqlite3_enable_load_extension(db, onoff);
1755 return TCL_OK;
1756#endif
1757}
1758
1759/*
drh28b4e482002-03-11 02:06:13 +00001760** Usage: sqlite_abort
1761**
1762** Shutdown the process immediately. This is not a clean shutdown.
1763** This command is used to test the recoverability of a database in
1764** the event of a program crash.
1765*/
1766static int sqlite_abort(
1767 void *NotUsed,
1768 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1769 int argc, /* Number of arguments */
1770 char **argv /* Text of each argument */
1771){
1772 assert( interp==0 ); /* This will always fail */
1773 return TCL_OK;
1774}
1775
1776/*
drh6cbe1f12002-07-01 00:31:36 +00001777** The following routine is a user-defined SQL function whose purpose
1778** is to test the sqlite_set_result() API.
1779*/
danielk19770ae8b832004-05-25 12:05:56 +00001780static void testFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
drh6cbe1f12002-07-01 00:31:36 +00001781 while( argc>=2 ){
drh03d847e2005-12-09 20:21:58 +00001782 const char *zArg0 = (char*)sqlite3_value_text(argv[0]);
danielk19776d88bad2004-05-27 14:23:36 +00001783 if( zArg0 ){
1784 if( 0==sqlite3StrICmp(zArg0, "int") ){
1785 sqlite3_result_int(context, sqlite3_value_int(argv[1]));
1786 }else if( sqlite3StrICmp(zArg0,"int64")==0 ){
1787 sqlite3_result_int64(context, sqlite3_value_int64(argv[1]));
1788 }else if( sqlite3StrICmp(zArg0,"string")==0 ){
drh03d847e2005-12-09 20:21:58 +00001789 sqlite3_result_text(context, (char*)sqlite3_value_text(argv[1]), -1,
danielk1977d8123362004-06-12 09:25:12 +00001790 SQLITE_TRANSIENT);
danielk19776d88bad2004-05-27 14:23:36 +00001791 }else if( sqlite3StrICmp(zArg0,"double")==0 ){
1792 sqlite3_result_double(context, sqlite3_value_double(argv[1]));
1793 }else if( sqlite3StrICmp(zArg0,"null")==0 ){
1794 sqlite3_result_null(context);
1795 }else if( sqlite3StrICmp(zArg0,"value")==0 ){
1796 sqlite3_result_value(context, argv[sqlite3_value_int(argv[1])]);
1797 }else{
1798 goto error_out;
1799 }
drh6cbe1f12002-07-01 00:31:36 +00001800 }else{
danielk19776d88bad2004-05-27 14:23:36 +00001801 goto error_out;
drh6cbe1f12002-07-01 00:31:36 +00001802 }
1803 argc -= 2;
1804 argv += 2;
1805 }
danielk19776d88bad2004-05-27 14:23:36 +00001806 return;
1807
1808error_out:
1809 sqlite3_result_error(context,"first argument should be one of: "
1810 "int int64 string double null value", -1);
drh6cbe1f12002-07-01 00:31:36 +00001811}
1812
1813/*
1814** Usage: sqlite_register_test_function DB NAME
1815**
1816** Register the test SQL function on the database DB under the name NAME.
1817*/
drhc2eef3b2002-08-31 18:53:06 +00001818static int test_register_func(
drh6cbe1f12002-07-01 00:31:36 +00001819 void *NotUsed,
1820 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1821 int argc, /* Number of arguments */
1822 char **argv /* Text of each argument */
1823){
drh9bb575f2004-09-06 17:24:11 +00001824 sqlite3 *db;
drh6cbe1f12002-07-01 00:31:36 +00001825 int rc;
1826 if( argc!=3 ){
1827 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1828 " DB FUNCTION-NAME", 0);
1829 return TCL_ERROR;
1830 }
drhb86ccfb2003-01-28 23:13:10 +00001831 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
danielk1977f9d64d22004-06-19 08:18:07 +00001832 rc = sqlite3_create_function(db, argv[2], -1, SQLITE_UTF8, 0,
danielk1977d8123362004-06-12 09:25:12 +00001833 testFunc, 0, 0);
drh6cbe1f12002-07-01 00:31:36 +00001834 if( rc!=0 ){
danielk1977f20b21c2004-05-31 23:56:42 +00001835 Tcl_AppendResult(interp, sqlite3ErrStr(rc), 0);
drh6cbe1f12002-07-01 00:31:36 +00001836 return TCL_ERROR;
1837 }
drhc60d0442004-09-30 13:43:13 +00001838 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
drh6cbe1f12002-07-01 00:31:36 +00001839 return TCL_OK;
1840}
1841
1842/*
danielk1977106bb232004-05-21 10:08:53 +00001843** Usage: sqlite3_finalize STMT
drhb86ccfb2003-01-28 23:13:10 +00001844**
danielk1977106bb232004-05-21 10:08:53 +00001845** Finalize a statement handle.
drhb86ccfb2003-01-28 23:13:10 +00001846*/
1847static int test_finalize(
danielk1977106bb232004-05-21 10:08:53 +00001848 void * clientData,
1849 Tcl_Interp *interp,
1850 int objc,
1851 Tcl_Obj *CONST objv[]
drhb86ccfb2003-01-28 23:13:10 +00001852){
danielk1977106bb232004-05-21 10:08:53 +00001853 sqlite3_stmt *pStmt;
drhb86ccfb2003-01-28 23:13:10 +00001854 int rc;
drhdddb2f22007-01-03 23:37:28 +00001855 sqlite3 *db = 0;
danielk1977106bb232004-05-21 10:08:53 +00001856
1857 if( objc!=2 ){
1858 Tcl_AppendResult(interp, "wrong # args: should be \"",
1859 Tcl_GetStringFromObj(objv[0], 0), " <STMT>", 0);
drhb86ccfb2003-01-28 23:13:10 +00001860 return TCL_ERROR;
1861 }
danielk1977106bb232004-05-21 10:08:53 +00001862
1863 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
1864
danielk19774397de52005-01-12 12:44:03 +00001865 if( pStmt ){
1866 db = StmtToDb(pStmt);
1867 }
danielk1977fc57d7b2004-05-26 02:04:57 +00001868 rc = sqlite3_finalize(pStmt);
drh4f0c5872007-03-26 22:05:01 +00001869 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
danielk19774397de52005-01-12 12:44:03 +00001870 if( db && sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
danielk1977106bb232004-05-21 10:08:53 +00001871 return TCL_OK;
1872}
1873
1874/*
1875** Usage: sqlite3_reset STMT
1876**
danielk1977261919c2005-12-06 12:52:59 +00001877** Reset a statement handle.
danielk1977106bb232004-05-21 10:08:53 +00001878*/
1879static int test_reset(
1880 void * clientData,
1881 Tcl_Interp *interp,
1882 int objc,
1883 Tcl_Obj *CONST objv[]
1884){
1885 sqlite3_stmt *pStmt;
1886 int rc;
1887
1888 if( objc!=2 ){
1889 Tcl_AppendResult(interp, "wrong # args: should be \"",
1890 Tcl_GetStringFromObj(objv[0], 0), " <STMT>", 0);
1891 return TCL_ERROR;
1892 }
1893
1894 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
1895
danielk1977fc57d7b2004-05-26 02:04:57 +00001896 rc = sqlite3_reset(pStmt);
danielk1977261919c2005-12-06 12:52:59 +00001897 if( pStmt && sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ){
1898 return TCL_ERROR;
1899 }
drh4f0c5872007-03-26 22:05:01 +00001900 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
danielk1977261919c2005-12-06 12:52:59 +00001901/*
danielk1977106bb232004-05-21 10:08:53 +00001902 if( rc ){
drhb86ccfb2003-01-28 23:13:10 +00001903 return TCL_ERROR;
1904 }
danielk1977261919c2005-12-06 12:52:59 +00001905*/
drhb86ccfb2003-01-28 23:13:10 +00001906 return TCL_OK;
1907}
1908
drh5a387052003-01-11 14:19:51 +00001909/*
drhd89bd002005-01-22 03:03:54 +00001910** Usage: sqlite3_expired STMT
1911**
1912** Return TRUE if a recompilation of the statement is recommended.
1913*/
1914static int test_expired(
1915 void * clientData,
1916 Tcl_Interp *interp,
1917 int objc,
1918 Tcl_Obj *CONST objv[]
1919){
1920 sqlite3_stmt *pStmt;
1921 if( objc!=2 ){
1922 Tcl_AppendResult(interp, "wrong # args: should be \"",
1923 Tcl_GetStringFromObj(objv[0], 0), " <STMT>", 0);
1924 return TCL_ERROR;
1925 }
1926 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
1927 Tcl_SetObjResult(interp, Tcl_NewBooleanObj(sqlite3_expired(pStmt)));
1928 return TCL_OK;
1929}
1930
1931/*
drhf8db1bc2005-04-22 02:38:37 +00001932** Usage: sqlite3_transfer_bindings FROMSTMT TOSTMT
1933**
1934** Transfer all bindings from FROMSTMT over to TOSTMT
1935*/
1936static int test_transfer_bind(
1937 void * clientData,
1938 Tcl_Interp *interp,
1939 int objc,
1940 Tcl_Obj *CONST objv[]
1941){
1942 sqlite3_stmt *pStmt1, *pStmt2;
1943 if( objc!=3 ){
1944 Tcl_AppendResult(interp, "wrong # args: should be \"",
1945 Tcl_GetStringFromObj(objv[0], 0), " FROM-STMT TO-STMT", 0);
1946 return TCL_ERROR;
1947 }
1948 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt1)) return TCL_ERROR;
1949 if( getStmtPointer(interp, Tcl_GetString(objv[2]), &pStmt2)) return TCL_ERROR;
1950 Tcl_SetObjResult(interp,
1951 Tcl_NewIntObj(sqlite3_transfer_bindings(pStmt1,pStmt2)));
1952 return TCL_OK;
1953}
1954
1955/*
danielk1977fbcd5852004-06-15 02:44:18 +00001956** Usage: sqlite3_changes DB
drh50457892003-09-06 01:10:47 +00001957**
danielk1977fbcd5852004-06-15 02:44:18 +00001958** Return the number of changes made to the database by the last SQL
1959** execution.
drh50457892003-09-06 01:10:47 +00001960*/
danielk1977fbcd5852004-06-15 02:44:18 +00001961static int test_changes(
1962 void * clientData,
1963 Tcl_Interp *interp,
1964 int objc,
1965 Tcl_Obj *CONST objv[]
1966){
1967 sqlite3 *db;
1968 if( objc!=2 ){
1969 Tcl_AppendResult(interp, "wrong # args: should be \"",
1970 Tcl_GetString(objv[0]), " DB", 0);
1971 return TCL_ERROR;
1972 }
1973 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
1974 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_changes(db)));
1975 return TCL_OK;
1976}
drh50457892003-09-06 01:10:47 +00001977
1978/*
drh7c972de2003-09-06 22:18:07 +00001979** This is the "static_bind_value" that variables are bound to when
danielk19776f8a5032004-05-10 10:34:51 +00001980** the FLAG option of sqlite3_bind is "static"
drh50457892003-09-06 01:10:47 +00001981*/
drh7c972de2003-09-06 22:18:07 +00001982static char *sqlite_static_bind_value = 0;
drhf0313812006-09-04 15:53:53 +00001983static int sqlite_static_bind_nbyte = 0;
drh7c972de2003-09-06 22:18:07 +00001984
1985/*
danielk19776f8a5032004-05-10 10:34:51 +00001986** Usage: sqlite3_bind VM IDX VALUE FLAGS
drh7c972de2003-09-06 22:18:07 +00001987**
1988** Sets the value of the IDX-th occurance of "?" in the original SQL
1989** string. VALUE is the new value. If FLAGS=="null" then VALUE is
1990** ignored and the value is set to NULL. If FLAGS=="static" then
1991** the value is set to the value of a static variable named
1992** "sqlite_static_bind_value". If FLAGS=="normal" then a copy
drhbf8aa2a2005-12-02 02:44:05 +00001993** of the VALUE is made. If FLAGS=="blob10" then a VALUE is ignored
1994** an a 10-byte blob "abc\000xyz\000pq" is inserted.
drh7c972de2003-09-06 22:18:07 +00001995*/
1996static int test_bind(
drh50457892003-09-06 01:10:47 +00001997 void *NotUsed,
1998 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1999 int argc, /* Number of arguments */
2000 char **argv /* Text of each argument */
2001){
danielk1977fc57d7b2004-05-26 02:04:57 +00002002 sqlite3_stmt *pStmt;
drh50457892003-09-06 01:10:47 +00002003 int rc;
drh7c972de2003-09-06 22:18:07 +00002004 int idx;
2005 if( argc!=5 ){
drh50457892003-09-06 01:10:47 +00002006 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
drh7c972de2003-09-06 22:18:07 +00002007 " VM IDX VALUE (null|static|normal)\"", 0);
drh50457892003-09-06 01:10:47 +00002008 return TCL_ERROR;
2009 }
danielk1977fc57d7b2004-05-26 02:04:57 +00002010 if( getStmtPointer(interp, argv[1], &pStmt) ) return TCL_ERROR;
drh7c972de2003-09-06 22:18:07 +00002011 if( Tcl_GetInt(interp, argv[2], &idx) ) return TCL_ERROR;
2012 if( strcmp(argv[4],"null")==0 ){
danielk1977fc57d7b2004-05-26 02:04:57 +00002013 rc = sqlite3_bind_null(pStmt, idx);
drh7c972de2003-09-06 22:18:07 +00002014 }else if( strcmp(argv[4],"static")==0 ){
danielk1977fc57d7b2004-05-26 02:04:57 +00002015 rc = sqlite3_bind_text(pStmt, idx, sqlite_static_bind_value, -1, 0);
drhf0313812006-09-04 15:53:53 +00002016 }else if( strcmp(argv[4],"static-nbytes")==0 ){
2017 rc = sqlite3_bind_text(pStmt, idx, sqlite_static_bind_value,
2018 sqlite_static_bind_nbyte, 0);
drh7c972de2003-09-06 22:18:07 +00002019 }else if( strcmp(argv[4],"normal")==0 ){
danielk1977d8123362004-06-12 09:25:12 +00002020 rc = sqlite3_bind_text(pStmt, idx, argv[3], -1, SQLITE_TRANSIENT);
drhbf8aa2a2005-12-02 02:44:05 +00002021 }else if( strcmp(argv[4],"blob10")==0 ){
2022 rc = sqlite3_bind_text(pStmt, idx, "abc\000xyz\000pq", 10, SQLITE_STATIC);
drh7c972de2003-09-06 22:18:07 +00002023 }else{
2024 Tcl_AppendResult(interp, "4th argument should be "
2025 "\"null\" or \"static\" or \"normal\"", 0);
2026 return TCL_ERROR;
2027 }
drhc60d0442004-09-30 13:43:13 +00002028 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
drh50457892003-09-06 01:10:47 +00002029 if( rc ){
2030 char zBuf[50];
2031 sprintf(zBuf, "(%d) ", rc);
danielk1977f20b21c2004-05-31 23:56:42 +00002032 Tcl_AppendResult(interp, zBuf, sqlite3ErrStr(rc), 0);
drh50457892003-09-06 01:10:47 +00002033 return TCL_ERROR;
2034 }
2035 return TCL_OK;
2036}
2037
drh5436dc22004-11-14 04:04:17 +00002038#ifndef SQLITE_OMIT_UTF16
danielk19774e6af132004-06-10 14:01:08 +00002039/*
2040** Usage: add_test_collate <db ptr> <utf8> <utf16le> <utf16be>
2041**
2042** This function is used to test that SQLite selects the correct collation
2043** sequence callback when multiple versions (for different text encodings)
2044** are available.
2045**
2046** Calling this routine registers the collation sequence "test_collate"
2047** with database handle <db>. The second argument must be a list of three
2048** boolean values. If the first is true, then a version of test_collate is
2049** registered for UTF-8, if the second is true, a version is registered for
2050** UTF-16le, if the third is true, a UTF-16be version is available.
2051** Previous versions of test_collate are deleted.
2052**
2053** The collation sequence test_collate is implemented by calling the
2054** following TCL script:
2055**
2056** "test_collate <enc> <lhs> <rhs>"
2057**
2058** The <lhs> and <rhs> are the two values being compared, encoded in UTF-8.
2059** The <enc> parameter is the encoding of the collation function that
2060** SQLite selected to call. The TCL test script implements the
2061** "test_collate" proc.
2062**
2063** Note that this will only work with one intepreter at a time, as the
2064** interp pointer to use when evaluating the TCL script is stored in
2065** pTestCollateInterp.
2066*/
2067static Tcl_Interp* pTestCollateInterp;
2068static int test_collate_func(
2069 void *pCtx,
2070 int nA, const void *zA,
2071 int nB, const void *zB
2072){
2073 Tcl_Interp *i = pTestCollateInterp;
2074 int encin = (int)pCtx;
2075 int res;
drh4db38a72005-09-01 12:16:28 +00002076 int n;
danielk19774e6af132004-06-10 14:01:08 +00002077
2078 sqlite3_value *pVal;
2079 Tcl_Obj *pX;
2080
2081 pX = Tcl_NewStringObj("test_collate", -1);
2082 Tcl_IncrRefCount(pX);
2083
2084 switch( encin ){
2085 case SQLITE_UTF8:
2086 Tcl_ListObjAppendElement(i,pX,Tcl_NewStringObj("UTF-8",-1));
2087 break;
2088 case SQLITE_UTF16LE:
2089 Tcl_ListObjAppendElement(i,pX,Tcl_NewStringObj("UTF-16LE",-1));
2090 break;
2091 case SQLITE_UTF16BE:
2092 Tcl_ListObjAppendElement(i,pX,Tcl_NewStringObj("UTF-16BE",-1));
2093 break;
2094 default:
2095 assert(0);
2096 }
2097
danielk19771e536952007-08-16 10:09:01 +00002098 pVal = sqlite3ValueNew(0);
drhb21c8cd2007-08-21 19:33:56 +00002099 sqlite3ValueSetStr(pVal, nA, zA, encin, SQLITE_STATIC);
drh4db38a72005-09-01 12:16:28 +00002100 n = sqlite3_value_bytes(pVal);
drh03d847e2005-12-09 20:21:58 +00002101 Tcl_ListObjAppendElement(i,pX,
2102 Tcl_NewStringObj((char*)sqlite3_value_text(pVal),n));
drhb21c8cd2007-08-21 19:33:56 +00002103 sqlite3ValueSetStr(pVal, nB, zB, encin, SQLITE_STATIC);
drh4db38a72005-09-01 12:16:28 +00002104 n = sqlite3_value_bytes(pVal);
drh03d847e2005-12-09 20:21:58 +00002105 Tcl_ListObjAppendElement(i,pX,
2106 Tcl_NewStringObj((char*)sqlite3_value_text(pVal),n));
danielk19774e6af132004-06-10 14:01:08 +00002107 sqlite3ValueFree(pVal);
2108
2109 Tcl_EvalObjEx(i, pX, 0);
2110 Tcl_DecrRefCount(pX);
2111 Tcl_GetIntFromObj(i, Tcl_GetObjResult(i), &res);
2112 return res;
2113}
2114static int test_collate(
2115 void * clientData,
2116 Tcl_Interp *interp,
2117 int objc,
2118 Tcl_Obj *CONST objv[]
2119){
2120 sqlite3 *db;
2121 int val;
danielk1977312d6b32004-06-29 13:18:23 +00002122 sqlite3_value *pVal;
drhc60d0442004-09-30 13:43:13 +00002123 int rc;
danielk19774e6af132004-06-10 14:01:08 +00002124
2125 if( objc!=5 ) goto bad_args;
2126 pTestCollateInterp = interp;
2127 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
2128
2129 if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[2], &val) ) return TCL_ERROR;
drhc60d0442004-09-30 13:43:13 +00002130 rc = sqlite3_create_collation(db, "test_collate", SQLITE_UTF8,
2131 (void *)SQLITE_UTF8, val?test_collate_func:0);
2132 if( rc==SQLITE_OK ){
2133 if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[3], &val) ) return TCL_ERROR;
2134 rc = sqlite3_create_collation(db, "test_collate", SQLITE_UTF16LE,
2135 (void *)SQLITE_UTF16LE, val?test_collate_func:0);
2136 if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[4], &val) ) return TCL_ERROR;
danielk1977312d6b32004-06-29 13:18:23 +00002137
drh86f8c192007-08-22 00:39:19 +00002138#if 0
danielk19779a30cf62006-01-18 04:26:07 +00002139 if( sqlite3_iMallocFail>0 ){
2140 sqlite3_iMallocFail++;
2141 }
2142#endif
drhf3a65f72007-08-22 20:18:21 +00002143 sqlite3_mutex_enter(db->mutex);
2144 pVal = sqlite3ValueNew(db);
drhb21c8cd2007-08-21 19:33:56 +00002145 sqlite3ValueSetStr(pVal, -1, "test_collate", SQLITE_UTF8, SQLITE_STATIC);
drhf3a65f72007-08-22 20:18:21 +00002146 if( db->mallocFailed ){
2147 rc = SQLITE_NOMEM;
2148 }else{
2149 rc = sqlite3_create_collation16(db,
drhb21c8cd2007-08-21 19:33:56 +00002150 sqlite3ValueText(pVal, SQLITE_UTF16NATIVE), SQLITE_UTF16BE,
danielk19779a30cf62006-01-18 04:26:07 +00002151 (void *)SQLITE_UTF16BE, val?test_collate_func:0);
drhf3a65f72007-08-22 20:18:21 +00002152 }
drhc60d0442004-09-30 13:43:13 +00002153 sqlite3ValueFree(pVal);
drhf3a65f72007-08-22 20:18:21 +00002154 sqlite3_mutex_leave(db->mutex);
drhc60d0442004-09-30 13:43:13 +00002155 }
2156 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
danielk19779a30cf62006-01-18 04:26:07 +00002157
2158 if( rc!=SQLITE_OK ){
2159 Tcl_AppendResult(interp, sqlite3TestErrorName(rc), 0);
2160 return TCL_ERROR;
2161 }
danielk19774e6af132004-06-10 14:01:08 +00002162 return TCL_OK;
2163
2164bad_args:
2165 Tcl_AppendResult(interp, "wrong # args: should be \"",
2166 Tcl_GetStringFromObj(objv[0], 0), " <DB> <utf8> <utf16le> <utf16be>", 0);
2167 return TCL_ERROR;
2168}
2169
drh268803a2005-12-14 20:11:30 +00002170/*
2171** When the collation needed callback is invoked, record the name of
2172** the requested collating function here. The recorded name is linked
2173** to a TCL variable and used to make sure that the requested collation
2174** name is correct.
2175*/
2176static char zNeededCollation[200];
2177static char *pzNeededCollation = zNeededCollation;
2178
2179
2180/*
2181** Called when a collating sequence is needed. Registered using
2182** sqlite3_collation_needed16().
2183*/
danielk1977312d6b32004-06-29 13:18:23 +00002184static void test_collate_needed_cb(
2185 void *pCtx,
2186 sqlite3 *db,
2187 int eTextRep,
drh268803a2005-12-14 20:11:30 +00002188 const void *pName
danielk1977312d6b32004-06-29 13:18:23 +00002189){
danielk197714db2662006-01-09 16:12:04 +00002190 int enc = ENC(db);
drh268803a2005-12-14 20:11:30 +00002191 int i;
2192 char *z;
2193 for(z = (char*)pName, i=0; *z || z[1]; z++){
2194 if( *z ) zNeededCollation[i++] = *z;
2195 }
2196 zNeededCollation[i] = 0;
danielk1977312d6b32004-06-29 13:18:23 +00002197 sqlite3_create_collation(
danielk197714db2662006-01-09 16:12:04 +00002198 db, "test_collate", ENC(db), (void *)enc, test_collate_func);
danielk1977312d6b32004-06-29 13:18:23 +00002199}
2200
2201/*
2202** Usage: add_test_collate_needed DB
2203*/
2204static int test_collate_needed(
2205 void * clientData,
2206 Tcl_Interp *interp,
2207 int objc,
2208 Tcl_Obj *CONST objv[]
2209){
2210 sqlite3 *db;
drhc60d0442004-09-30 13:43:13 +00002211 int rc;
danielk1977312d6b32004-06-29 13:18:23 +00002212
2213 if( objc!=2 ) goto bad_args;
2214 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
drhc60d0442004-09-30 13:43:13 +00002215 rc = sqlite3_collation_needed16(db, 0, test_collate_needed_cb);
drh268803a2005-12-14 20:11:30 +00002216 zNeededCollation[0] = 0;
drhc60d0442004-09-30 13:43:13 +00002217 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
danielk1977312d6b32004-06-29 13:18:23 +00002218 return TCL_OK;
2219
2220bad_args:
2221 Tcl_WrongNumArgs(interp, 1, objv, "DB");
2222 return TCL_ERROR;
2223}
drh7d9bd4e2006-02-16 18:16:36 +00002224
2225/*
2226** tclcmd: add_alignment_test_collations DB
2227**
2228** Add two new collating sequences to the database DB
2229**
2230** utf16_aligned
2231** utf16_unaligned
2232**
2233** Both collating sequences use the same sort order as BINARY.
2234** The only difference is that the utf16_aligned collating
2235** sequence is declared with the SQLITE_UTF16_ALIGNED flag.
2236** Both collating functions increment the unaligned utf16 counter
2237** whenever they see a string that begins on an odd byte boundary.
2238*/
2239static int unaligned_string_counter = 0;
2240static int alignmentCollFunc(
2241 void *NotUsed,
2242 int nKey1, const void *pKey1,
2243 int nKey2, const void *pKey2
2244){
2245 int rc, n;
2246 n = nKey1<nKey2 ? nKey1 : nKey2;
2247 if( nKey1>0 && 1==(1&(int)pKey1) ) unaligned_string_counter++;
2248 if( nKey2>0 && 1==(1&(int)pKey2) ) unaligned_string_counter++;
2249 rc = memcmp(pKey1, pKey2, n);
2250 if( rc==0 ){
2251 rc = nKey1 - nKey2;
2252 }
2253 return rc;
2254}
2255static int add_alignment_test_collations(
2256 void * clientData,
2257 Tcl_Interp *interp,
2258 int objc,
2259 Tcl_Obj *CONST objv[]
2260){
2261 sqlite3 *db;
2262 if( objc>=2 ){
2263 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
2264 sqlite3_create_collation(db, "utf16_unaligned",
2265 SQLITE_UTF16,
2266 0, alignmentCollFunc);
2267 sqlite3_create_collation(db, "utf16_aligned",
2268 SQLITE_UTF16 | SQLITE_UTF16_ALIGNED,
2269 0, alignmentCollFunc);
2270 }
2271 return SQLITE_OK;
2272}
2273#endif /* !defined(SQLITE_OMIT_UTF16) */
danielk1977312d6b32004-06-29 13:18:23 +00002274
danielk1977c8e9a2d2004-06-25 12:08:46 +00002275/*
2276** Usage: add_test_function <db ptr> <utf8> <utf16le> <utf16be>
2277**
2278** This function is used to test that SQLite selects the correct user
2279** function callback when multiple versions (for different text encodings)
2280** are available.
2281**
2282** Calling this routine registers up to three versions of the user function
2283** "test_function" with database handle <db>. If the second argument is
2284** true, then a version of test_function is registered for UTF-8, if the
2285** third is true, a version is registered for UTF-16le, if the fourth is
2286** true, a UTF-16be version is available. Previous versions of
2287** test_function are deleted.
2288**
2289** The user function is implemented by calling the following TCL script:
2290**
2291** "test_function <enc> <arg>"
2292**
2293** Where <enc> is one of UTF-8, UTF-16LE or UTF16BE, and <arg> is the
2294** single argument passed to the SQL function. The value returned by
2295** the TCL script is used as the return value of the SQL function. It
2296** is passed to SQLite using UTF-16BE for a UTF-8 test_function(), UTF-8
2297** for a UTF-16LE test_function(), and UTF-16LE for an implementation that
2298** prefers UTF-16BE.
2299*/
drh5436dc22004-11-14 04:04:17 +00002300#ifndef SQLITE_OMIT_UTF16
danielk1977c8e9a2d2004-06-25 12:08:46 +00002301static void test_function_utf8(
2302 sqlite3_context *pCtx,
2303 int nArg,
2304 sqlite3_value **argv
2305){
2306 Tcl_Interp *interp;
2307 Tcl_Obj *pX;
2308 sqlite3_value *pVal;
2309 interp = (Tcl_Interp *)sqlite3_user_data(pCtx);
2310 pX = Tcl_NewStringObj("test_function", -1);
2311 Tcl_IncrRefCount(pX);
2312 Tcl_ListObjAppendElement(interp, pX, Tcl_NewStringObj("UTF-8", -1));
2313 Tcl_ListObjAppendElement(interp, pX,
drh03d847e2005-12-09 20:21:58 +00002314 Tcl_NewStringObj((char*)sqlite3_value_text(argv[0]), -1));
danielk1977c8e9a2d2004-06-25 12:08:46 +00002315 Tcl_EvalObjEx(interp, pX, 0);
2316 Tcl_DecrRefCount(pX);
2317 sqlite3_result_text(pCtx, Tcl_GetStringResult(interp), -1, SQLITE_TRANSIENT);
danielk19771e536952007-08-16 10:09:01 +00002318 pVal = sqlite3ValueNew(0);
drhb21c8cd2007-08-21 19:33:56 +00002319 sqlite3ValueSetStr(pVal, -1, Tcl_GetStringResult(interp),
danielk1977c8e9a2d2004-06-25 12:08:46 +00002320 SQLITE_UTF8, SQLITE_STATIC);
2321 sqlite3_result_text16be(pCtx, sqlite3_value_text16be(pVal),
2322 -1, SQLITE_TRANSIENT);
2323 sqlite3ValueFree(pVal);
2324}
2325static void test_function_utf16le(
2326 sqlite3_context *pCtx,
2327 int nArg,
2328 sqlite3_value **argv
2329){
2330 Tcl_Interp *interp;
2331 Tcl_Obj *pX;
2332 sqlite3_value *pVal;
2333 interp = (Tcl_Interp *)sqlite3_user_data(pCtx);
2334 pX = Tcl_NewStringObj("test_function", -1);
2335 Tcl_IncrRefCount(pX);
2336 Tcl_ListObjAppendElement(interp, pX, Tcl_NewStringObj("UTF-16LE", -1));
2337 Tcl_ListObjAppendElement(interp, pX,
drh03d847e2005-12-09 20:21:58 +00002338 Tcl_NewStringObj((char*)sqlite3_value_text(argv[0]), -1));
danielk1977c8e9a2d2004-06-25 12:08:46 +00002339 Tcl_EvalObjEx(interp, pX, 0);
2340 Tcl_DecrRefCount(pX);
danielk19771e536952007-08-16 10:09:01 +00002341 pVal = sqlite3ValueNew(0);
drhb21c8cd2007-08-21 19:33:56 +00002342 sqlite3ValueSetStr(pVal, -1, Tcl_GetStringResult(interp),
danielk1977c8e9a2d2004-06-25 12:08:46 +00002343 SQLITE_UTF8, SQLITE_STATIC);
drh03d847e2005-12-09 20:21:58 +00002344 sqlite3_result_text(pCtx,(char*)sqlite3_value_text(pVal),-1,SQLITE_TRANSIENT);
danielk1977c8e9a2d2004-06-25 12:08:46 +00002345 sqlite3ValueFree(pVal);
2346}
2347static void test_function_utf16be(
2348 sqlite3_context *pCtx,
2349 int nArg,
2350 sqlite3_value **argv
2351){
2352 Tcl_Interp *interp;
2353 Tcl_Obj *pX;
2354 sqlite3_value *pVal;
2355 interp = (Tcl_Interp *)sqlite3_user_data(pCtx);
2356 pX = Tcl_NewStringObj("test_function", -1);
2357 Tcl_IncrRefCount(pX);
2358 Tcl_ListObjAppendElement(interp, pX, Tcl_NewStringObj("UTF-16BE", -1));
2359 Tcl_ListObjAppendElement(interp, pX,
drh03d847e2005-12-09 20:21:58 +00002360 Tcl_NewStringObj((char*)sqlite3_value_text(argv[0]), -1));
danielk1977c8e9a2d2004-06-25 12:08:46 +00002361 Tcl_EvalObjEx(interp, pX, 0);
2362 Tcl_DecrRefCount(pX);
danielk19771e536952007-08-16 10:09:01 +00002363 pVal = sqlite3ValueNew(0);
drhb21c8cd2007-08-21 19:33:56 +00002364 sqlite3ValueSetStr(pVal, -1, Tcl_GetStringResult(interp),
danielk1977c8e9a2d2004-06-25 12:08:46 +00002365 SQLITE_UTF8, SQLITE_STATIC);
2366 sqlite3_result_text16le(pCtx, sqlite3_value_text16le(pVal),
2367 -1, SQLITE_TRANSIENT);
2368 sqlite3ValueFree(pVal);
2369}
drh5436dc22004-11-14 04:04:17 +00002370#endif /* SQLITE_OMIT_UTF16 */
danielk1977c8e9a2d2004-06-25 12:08:46 +00002371static int test_function(
2372 void * clientData,
2373 Tcl_Interp *interp,
2374 int objc,
2375 Tcl_Obj *CONST objv[]
2376){
drh5436dc22004-11-14 04:04:17 +00002377#ifndef SQLITE_OMIT_UTF16
danielk1977c8e9a2d2004-06-25 12:08:46 +00002378 sqlite3 *db;
2379 int val;
2380
2381 if( objc!=5 ) goto bad_args;
2382 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
2383
2384 if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[2], &val) ) return TCL_ERROR;
2385 if( val ){
2386 sqlite3_create_function(db, "test_function", 1, SQLITE_UTF8,
2387 interp, test_function_utf8, 0, 0);
2388 }
2389 if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[3], &val) ) return TCL_ERROR;
2390 if( val ){
2391 sqlite3_create_function(db, "test_function", 1, SQLITE_UTF16LE,
2392 interp, test_function_utf16le, 0, 0);
2393 }
2394 if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[4], &val) ) return TCL_ERROR;
2395 if( val ){
2396 sqlite3_create_function(db, "test_function", 1, SQLITE_UTF16BE,
2397 interp, test_function_utf16be, 0, 0);
2398 }
2399
2400 return TCL_OK;
2401bad_args:
2402 Tcl_AppendResult(interp, "wrong # args: should be \"",
2403 Tcl_GetStringFromObj(objv[0], 0), " <DB> <utf8> <utf16le> <utf16be>", 0);
drh5436dc22004-11-14 04:04:17 +00002404#endif /* SQLITE_OMIT_UTF16 */
danielk1977c8e9a2d2004-06-25 12:08:46 +00002405 return TCL_ERROR;
2406}
2407
danielk1977312d6b32004-06-29 13:18:23 +00002408/*
2409** Usage: test_errstr <err code>
2410**
2411** Test that the english language string equivalents for sqlite error codes
2412** are sane. The parameter is an integer representing an sqlite error code.
2413** The result is a list of two elements, the string representation of the
2414** error code and the english language explanation.
2415*/
2416static int test_errstr(
2417 void * clientData,
2418 Tcl_Interp *interp,
2419 int objc,
2420 Tcl_Obj *CONST objv[]
2421){
2422 char *zCode;
2423 int i;
2424 if( objc!=1 ){
2425 Tcl_WrongNumArgs(interp, 1, objv, "<error code>");
2426 }
2427
2428 zCode = Tcl_GetString(objv[1]);
2429 for(i=0; i<200; i++){
drh4f0c5872007-03-26 22:05:01 +00002430 if( 0==strcmp(t1ErrorName(i), zCode) ) break;
danielk1977312d6b32004-06-29 13:18:23 +00002431 }
2432 Tcl_SetResult(interp, (char *)sqlite3ErrStr(i), 0);
2433 return TCL_OK;
2434}
2435
drh50457892003-09-06 01:10:47 +00002436/*
drh99ee3602003-02-16 19:13:36 +00002437** Usage: breakpoint
2438**
2439** This routine exists for one purpose - to provide a place to put a
2440** breakpoint with GDB that can be triggered using TCL code. The use
2441** for this is when a particular test fails on (say) the 1485th iteration.
2442** In the TCL test script, we can add code like this:
2443**
2444** if {$i==1485} breakpoint
2445**
2446** Then run testfixture in the debugger and wait for the breakpoint to
2447** fire. Then additional breakpoints can be set to trace down the bug.
2448*/
2449static int test_breakpoint(
2450 void *NotUsed,
2451 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
2452 int argc, /* Number of arguments */
2453 char **argv /* Text of each argument */
2454){
2455 return TCL_OK; /* Do nothing */
2456}
2457
drh241db312004-06-22 12:46:53 +00002458/*
drhb026e052007-05-02 01:34:31 +00002459** Usage: sqlite3_bind_zeroblob STMT IDX N
2460**
2461** Test the sqlite3_bind_zeroblob interface. STMT is a prepared statement.
2462** IDX is the index of a wildcard in the prepared statement. This command
2463** binds a N-byte zero-filled BLOB to the wildcard.
2464*/
2465static int test_bind_zeroblob(
2466 void * clientData,
2467 Tcl_Interp *interp,
2468 int objc,
2469 Tcl_Obj *CONST objv[]
2470){
2471 sqlite3_stmt *pStmt;
2472 int idx;
2473 int n;
2474 int rc;
2475
2476 if( objc!=4 ){
2477 Tcl_AppendResult(interp, "wrong # args: should be \"",
2478 Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE", 0);
2479 return TCL_ERROR;
2480 }
2481
2482 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2483 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
2484 if( Tcl_GetIntFromObj(interp, objv[3], &n) ) return TCL_ERROR;
2485
2486 rc = sqlite3_bind_zeroblob(pStmt, idx, n);
2487 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
2488 if( rc!=SQLITE_OK ){
2489 return TCL_ERROR;
2490 }
2491
2492 return TCL_OK;
2493}
2494
2495/*
drh241db312004-06-22 12:46:53 +00002496** Usage: sqlite3_bind_int STMT N VALUE
2497**
2498** Test the sqlite3_bind_int interface. STMT is a prepared statement.
2499** N is the index of a wildcard in the prepared statement. This command
2500** binds a 32-bit integer VALUE to that wildcard.
2501*/
2502static int test_bind_int(
danielk197751e3d8e2004-05-20 01:12:34 +00002503 void * clientData,
2504 Tcl_Interp *interp,
2505 int objc,
2506 Tcl_Obj *CONST objv[]
2507){
2508 sqlite3_stmt *pStmt;
2509 int idx;
2510 int value;
2511 int rc;
2512
2513 if( objc!=4 ){
2514 Tcl_AppendResult(interp, "wrong # args: should be \"",
drh241db312004-06-22 12:46:53 +00002515 Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE", 0);
danielk197751e3d8e2004-05-20 01:12:34 +00002516 return TCL_ERROR;
2517 }
2518
2519 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2520 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
2521 if( Tcl_GetIntFromObj(interp, objv[3], &value) ) return TCL_ERROR;
2522
danielk1977c572ef72004-05-27 09:28:41 +00002523 rc = sqlite3_bind_int(pStmt, idx, value);
drhc60d0442004-09-30 13:43:13 +00002524 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
danielk197751e3d8e2004-05-20 01:12:34 +00002525 if( rc!=SQLITE_OK ){
2526 return TCL_ERROR;
2527 }
2528
2529 return TCL_OK;
2530}
2531
drh241db312004-06-22 12:46:53 +00002532
2533/*
2534** Usage: sqlite3_bind_int64 STMT N VALUE
2535**
2536** Test the sqlite3_bind_int64 interface. STMT is a prepared statement.
2537** N is the index of a wildcard in the prepared statement. This command
2538** binds a 64-bit integer VALUE to that wildcard.
2539*/
danielk197751e3d8e2004-05-20 01:12:34 +00002540static int test_bind_int64(
2541 void * clientData,
2542 Tcl_Interp *interp,
2543 int objc,
2544 Tcl_Obj *CONST objv[]
2545){
2546 sqlite3_stmt *pStmt;
2547 int idx;
2548 i64 value;
2549 int rc;
2550
2551 if( objc!=4 ){
2552 Tcl_AppendResult(interp, "wrong # args: should be \"",
drh241db312004-06-22 12:46:53 +00002553 Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE", 0);
danielk197751e3d8e2004-05-20 01:12:34 +00002554 return TCL_ERROR;
2555 }
2556
2557 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2558 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
2559 if( Tcl_GetWideIntFromObj(interp, objv[3], &value) ) return TCL_ERROR;
2560
2561 rc = sqlite3_bind_int64(pStmt, idx, value);
drhc60d0442004-09-30 13:43:13 +00002562 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
danielk197751e3d8e2004-05-20 01:12:34 +00002563 if( rc!=SQLITE_OK ){
2564 return TCL_ERROR;
2565 }
2566
2567 return TCL_OK;
2568}
2569
drh241db312004-06-22 12:46:53 +00002570
2571/*
2572** Usage: sqlite3_bind_double STMT N VALUE
2573**
2574** Test the sqlite3_bind_double interface. STMT is a prepared statement.
2575** N is the index of a wildcard in the prepared statement. This command
2576** binds a 64-bit integer VALUE to that wildcard.
2577*/
danielk197751e3d8e2004-05-20 01:12:34 +00002578static int test_bind_double(
2579 void * clientData,
2580 Tcl_Interp *interp,
2581 int objc,
2582 Tcl_Obj *CONST objv[]
2583){
2584 sqlite3_stmt *pStmt;
2585 int idx;
2586 double value;
2587 int rc;
2588
2589 if( objc!=4 ){
2590 Tcl_AppendResult(interp, "wrong # args: should be \"",
drh241db312004-06-22 12:46:53 +00002591 Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE", 0);
danielk197751e3d8e2004-05-20 01:12:34 +00002592 return TCL_ERROR;
2593 }
2594
2595 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2596 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
2597 if( Tcl_GetDoubleFromObj(interp, objv[3], &value) ) return TCL_ERROR;
2598
2599 rc = sqlite3_bind_double(pStmt, idx, value);
drhc60d0442004-09-30 13:43:13 +00002600 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
danielk197751e3d8e2004-05-20 01:12:34 +00002601 if( rc!=SQLITE_OK ){
2602 return TCL_ERROR;
2603 }
2604
2605 return TCL_OK;
2606}
2607
drh241db312004-06-22 12:46:53 +00002608/*
2609** Usage: sqlite3_bind_null STMT N
2610**
2611** Test the sqlite3_bind_null interface. STMT is a prepared statement.
2612** N is the index of a wildcard in the prepared statement. This command
2613** binds a NULL to the wildcard.
2614*/
danielk197751e3d8e2004-05-20 01:12:34 +00002615static int test_bind_null(
2616 void * clientData,
2617 Tcl_Interp *interp,
2618 int objc,
2619 Tcl_Obj *CONST objv[]
2620){
2621 sqlite3_stmt *pStmt;
2622 int idx;
2623 int rc;
2624
2625 if( objc!=3 ){
2626 Tcl_AppendResult(interp, "wrong # args: should be \"",
drh241db312004-06-22 12:46:53 +00002627 Tcl_GetStringFromObj(objv[0], 0), " STMT N", 0);
danielk197751e3d8e2004-05-20 01:12:34 +00002628 return TCL_ERROR;
2629 }
2630
2631 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2632 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
2633
2634 rc = sqlite3_bind_null(pStmt, idx);
drhc60d0442004-09-30 13:43:13 +00002635 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
danielk197751e3d8e2004-05-20 01:12:34 +00002636 if( rc!=SQLITE_OK ){
2637 return TCL_ERROR;
2638 }
2639
2640 return TCL_OK;
2641}
2642
drh241db312004-06-22 12:46:53 +00002643/*
2644** Usage: sqlite3_bind_text STMT N STRING BYTES
2645**
2646** Test the sqlite3_bind_text interface. STMT is a prepared statement.
2647** N is the index of a wildcard in the prepared statement. This command
2648** binds a UTF-8 string STRING to the wildcard. The string is BYTES bytes
2649** long.
2650*/
danielk197751e3d8e2004-05-20 01:12:34 +00002651static int test_bind_text(
2652 void * clientData,
2653 Tcl_Interp *interp,
2654 int objc,
2655 Tcl_Obj *CONST objv[]
2656){
2657 sqlite3_stmt *pStmt;
2658 int idx;
2659 int bytes;
2660 char *value;
2661 int rc;
2662
2663 if( objc!=5 ){
2664 Tcl_AppendResult(interp, "wrong # args: should be \"",
drh241db312004-06-22 12:46:53 +00002665 Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE BYTES", 0);
danielk197751e3d8e2004-05-20 01:12:34 +00002666 return TCL_ERROR;
2667 }
2668
2669 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2670 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
2671 value = Tcl_GetString(objv[3]);
2672 if( Tcl_GetIntFromObj(interp, objv[4], &bytes) ) return TCL_ERROR;
2673
danielk1977d8123362004-06-12 09:25:12 +00002674 rc = sqlite3_bind_text(pStmt, idx, value, bytes, SQLITE_TRANSIENT);
drhc60d0442004-09-30 13:43:13 +00002675 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
danielk197751e3d8e2004-05-20 01:12:34 +00002676 if( rc!=SQLITE_OK ){
drh473d1792005-06-06 17:54:55 +00002677 Tcl_AppendResult(interp, sqlite3TestErrorName(rc), 0);
danielk197751e3d8e2004-05-20 01:12:34 +00002678 return TCL_ERROR;
2679 }
2680
2681 return TCL_OK;
2682}
2683
drh241db312004-06-22 12:46:53 +00002684/*
danielk1977161fb792006-01-24 10:58:21 +00002685** Usage: sqlite3_bind_text16 ?-static? STMT N STRING BYTES
drh241db312004-06-22 12:46:53 +00002686**
2687** Test the sqlite3_bind_text16 interface. STMT is a prepared statement.
2688** N is the index of a wildcard in the prepared statement. This command
2689** binds a UTF-16 string STRING to the wildcard. The string is BYTES bytes
2690** long.
2691*/
danielk197751e3d8e2004-05-20 01:12:34 +00002692static int test_bind_text16(
2693 void * clientData,
2694 Tcl_Interp *interp,
2695 int objc,
2696 Tcl_Obj *CONST objv[]
2697){
drh5436dc22004-11-14 04:04:17 +00002698#ifndef SQLITE_OMIT_UTF16
danielk197751e3d8e2004-05-20 01:12:34 +00002699 sqlite3_stmt *pStmt;
2700 int idx;
2701 int bytes;
2702 char *value;
2703 int rc;
2704
danielk1977161fb792006-01-24 10:58:21 +00002705 void (*xDel)() = (objc==6?SQLITE_STATIC:SQLITE_TRANSIENT);
2706 Tcl_Obj *oStmt = objv[objc-4];
2707 Tcl_Obj *oN = objv[objc-3];
2708 Tcl_Obj *oString = objv[objc-2];
2709 Tcl_Obj *oBytes = objv[objc-1];
2710
2711 if( objc!=5 && objc!=6){
danielk197751e3d8e2004-05-20 01:12:34 +00002712 Tcl_AppendResult(interp, "wrong # args: should be \"",
drh241db312004-06-22 12:46:53 +00002713 Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE BYTES", 0);
danielk197751e3d8e2004-05-20 01:12:34 +00002714 return TCL_ERROR;
2715 }
2716
danielk1977161fb792006-01-24 10:58:21 +00002717 if( getStmtPointer(interp, Tcl_GetString(oStmt), &pStmt) ) return TCL_ERROR;
2718 if( Tcl_GetIntFromObj(interp, oN, &idx) ) return TCL_ERROR;
2719 value = (char*)Tcl_GetByteArrayFromObj(oString, 0);
2720 if( Tcl_GetIntFromObj(interp, oBytes, &bytes) ) return TCL_ERROR;
danielk197751e3d8e2004-05-20 01:12:34 +00002721
danielk1977161fb792006-01-24 10:58:21 +00002722 rc = sqlite3_bind_text16(pStmt, idx, (void *)value, bytes, xDel);
drhc60d0442004-09-30 13:43:13 +00002723 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
danielk197751e3d8e2004-05-20 01:12:34 +00002724 if( rc!=SQLITE_OK ){
2725 return TCL_ERROR;
2726 }
2727
drh5436dc22004-11-14 04:04:17 +00002728#endif /* SQLITE_OMIT_UTF16 */
danielk197751e3d8e2004-05-20 01:12:34 +00002729 return TCL_OK;
2730}
2731
drh241db312004-06-22 12:46:53 +00002732/*
danielk19775b159dc2007-05-17 16:34:43 +00002733** Usage: sqlite3_bind_blob ?-static? STMT N DATA BYTES
drh241db312004-06-22 12:46:53 +00002734**
2735** Test the sqlite3_bind_blob interface. STMT is a prepared statement.
2736** N is the index of a wildcard in the prepared statement. This command
2737** binds a BLOB to the wildcard. The BLOB is BYTES bytes in size.
2738*/
danielk197751e3d8e2004-05-20 01:12:34 +00002739static int test_bind_blob(
2740 void * clientData,
2741 Tcl_Interp *interp,
2742 int objc,
2743 Tcl_Obj *CONST objv[]
2744){
2745 sqlite3_stmt *pStmt;
2746 int idx;
2747 int bytes;
2748 char *value;
2749 int rc;
danielk19775b159dc2007-05-17 16:34:43 +00002750 sqlite3_destructor_type xDestructor = SQLITE_TRANSIENT;
danielk197751e3d8e2004-05-20 01:12:34 +00002751
danielk19775b159dc2007-05-17 16:34:43 +00002752 if( objc!=5 && objc!=6 ){
danielk197751e3d8e2004-05-20 01:12:34 +00002753 Tcl_AppendResult(interp, "wrong # args: should be \"",
drh241db312004-06-22 12:46:53 +00002754 Tcl_GetStringFromObj(objv[0], 0), " STMT N DATA BYTES", 0);
danielk197751e3d8e2004-05-20 01:12:34 +00002755 return TCL_ERROR;
2756 }
2757
danielk19775b159dc2007-05-17 16:34:43 +00002758 if( objc==6 ){
2759 xDestructor = SQLITE_STATIC;
2760 objv++;
2761 }
2762
danielk197751e3d8e2004-05-20 01:12:34 +00002763 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2764 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
2765 value = Tcl_GetString(objv[3]);
danielk197749e46432004-05-27 13:55:27 +00002766 if( Tcl_GetIntFromObj(interp, objv[4], &bytes) ) return TCL_ERROR;
danielk197751e3d8e2004-05-20 01:12:34 +00002767
danielk19775b159dc2007-05-17 16:34:43 +00002768 rc = sqlite3_bind_blob(pStmt, idx, value, bytes, xDestructor);
drhc60d0442004-09-30 13:43:13 +00002769 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
danielk197751e3d8e2004-05-20 01:12:34 +00002770 if( rc!=SQLITE_OK ){
2771 return TCL_ERROR;
2772 }
2773
2774 return TCL_OK;
2775}
2776
drh99ee3602003-02-16 19:13:36 +00002777/*
drh75f6a032004-07-15 14:15:00 +00002778** Usage: sqlite3_bind_parameter_count STMT
2779**
2780** Return the number of wildcards in the given statement.
2781*/
2782static int test_bind_parameter_count(
2783 void * clientData,
2784 Tcl_Interp *interp,
2785 int objc,
2786 Tcl_Obj *CONST objv[]
2787){
2788 sqlite3_stmt *pStmt;
2789
2790 if( objc!=2 ){
2791 Tcl_WrongNumArgs(interp, 1, objv, "STMT");
2792 return TCL_ERROR;
2793 }
2794 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2795 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_bind_parameter_count(pStmt)));
2796 return TCL_OK;
2797}
2798
2799/*
drh895d7472004-08-20 16:02:39 +00002800** Usage: sqlite3_bind_parameter_name STMT N
2801**
2802** Return the name of the Nth wildcard. The first wildcard is 1.
2803** An empty string is returned if N is out of range or if the wildcard
2804** is nameless.
2805*/
2806static int test_bind_parameter_name(
2807 void * clientData,
2808 Tcl_Interp *interp,
2809 int objc,
2810 Tcl_Obj *CONST objv[]
2811){
2812 sqlite3_stmt *pStmt;
2813 int i;
2814
2815 if( objc!=3 ){
2816 Tcl_WrongNumArgs(interp, 1, objv, "STMT N");
2817 return TCL_ERROR;
2818 }
2819 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2820 if( Tcl_GetIntFromObj(interp, objv[2], &i) ) return TCL_ERROR;
2821 Tcl_SetObjResult(interp,
2822 Tcl_NewStringObj(sqlite3_bind_parameter_name(pStmt,i),-1)
2823 );
2824 return TCL_OK;
2825}
2826
2827/*
drhfa6bc002004-09-07 16:19:52 +00002828** Usage: sqlite3_bind_parameter_index STMT NAME
2829**
2830** Return the index of the wildcard called NAME. Return 0 if there is
2831** no such wildcard.
2832*/
2833static int test_bind_parameter_index(
2834 void * clientData,
2835 Tcl_Interp *interp,
2836 int objc,
2837 Tcl_Obj *CONST objv[]
2838){
2839 sqlite3_stmt *pStmt;
2840
2841 if( objc!=3 ){
2842 Tcl_WrongNumArgs(interp, 1, objv, "STMT NAME");
2843 return TCL_ERROR;
2844 }
2845 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2846 Tcl_SetObjResult(interp,
2847 Tcl_NewIntObj(
2848 sqlite3_bind_parameter_index(pStmt,Tcl_GetString(objv[2]))
2849 )
2850 );
2851 return TCL_OK;
2852}
2853
2854/*
danielk1977600dd0b2005-01-20 01:14:23 +00002855** Usage: sqlite3_clear_bindings STMT
2856**
2857*/
danielk1977600dd0b2005-01-20 01:14:23 +00002858static int test_clear_bindings(
2859 void * clientData,
2860 Tcl_Interp *interp,
2861 int objc,
2862 Tcl_Obj *CONST objv[]
2863){
2864 sqlite3_stmt *pStmt;
2865
2866 if( objc!=2 ){
2867 Tcl_WrongNumArgs(interp, 1, objv, "STMT");
2868 return TCL_ERROR;
2869 }
2870 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2871 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_clear_bindings(pStmt)));
2872 return TCL_OK;
2873}
drhf9cb7f52006-06-27 20:06:44 +00002874
2875/*
2876** Usage: sqlite3_sleep MILLISECONDS
2877*/
2878static int test_sleep(
2879 void * clientData,
2880 Tcl_Interp *interp,
2881 int objc,
2882 Tcl_Obj *CONST objv[]
2883){
2884 int ms;
2885
2886 if( objc!=2 ){
2887 Tcl_WrongNumArgs(interp, 1, objv, "MILLISECONDS");
2888 return TCL_ERROR;
2889 }
2890 if( Tcl_GetIntFromObj(interp, objv[1], &ms) ){
2891 return TCL_ERROR;
2892 }
2893 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_sleep(ms)));
2894 return TCL_OK;
2895}
danielk1977600dd0b2005-01-20 01:14:23 +00002896
2897/*
danielk19776622cce2004-05-20 11:00:52 +00002898** Usage: sqlite3_errcode DB
2899**
2900** Return the string representation of the most recent sqlite3_* API
2901** error code. e.g. "SQLITE_ERROR".
2902*/
2903static int test_errcode(
2904 void * clientData,
2905 Tcl_Interp *interp,
2906 int objc,
2907 Tcl_Obj *CONST objv[]
2908){
2909 sqlite3 *db;
drh4ac285a2006-09-15 07:28:50 +00002910 int rc;
2911 char zBuf[30];
danielk19776622cce2004-05-20 11:00:52 +00002912
2913 if( objc!=2 ){
2914 Tcl_AppendResult(interp, "wrong # args: should be \"",
2915 Tcl_GetString(objv[0]), " DB", 0);
2916 return TCL_ERROR;
2917 }
2918 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
drh4ac285a2006-09-15 07:28:50 +00002919 rc = sqlite3_errcode(db);
2920 if( (rc&0xff)==rc ){
2921 zBuf[0] = 0;
2922 }else{
2923 sprintf(zBuf,"+%d", rc>>8);
2924 }
drh4f0c5872007-03-26 22:05:01 +00002925 Tcl_AppendResult(interp, (char *)t1ErrorName(rc), zBuf, 0);
danielk19776622cce2004-05-20 11:00:52 +00002926 return TCL_OK;
2927}
2928
2929/*
2930** Usage: test_errmsg DB
2931**
2932** Returns the UTF-8 representation of the error message string for the
2933** most recent sqlite3_* API call.
2934*/
2935static int test_errmsg(
2936 void * clientData,
2937 Tcl_Interp *interp,
2938 int objc,
2939 Tcl_Obj *CONST objv[]
2940){
drh9bb575f2004-09-06 17:24:11 +00002941 sqlite3 *db;
danielk19776622cce2004-05-20 11:00:52 +00002942 const char *zErr;
2943
2944 if( objc!=2 ){
2945 Tcl_AppendResult(interp, "wrong # args: should be \"",
2946 Tcl_GetString(objv[0]), " DB", 0);
2947 return TCL_ERROR;
2948 }
2949 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
2950
2951 zErr = sqlite3_errmsg(db);
2952 Tcl_SetObjResult(interp, Tcl_NewStringObj(zErr, -1));
2953 return TCL_OK;
2954}
2955
2956/*
2957** Usage: test_errmsg16 DB
2958**
2959** Returns the UTF-16 representation of the error message string for the
2960** most recent sqlite3_* API call. This is a byte array object at the TCL
2961** level, and it includes the 0x00 0x00 terminator bytes at the end of the
2962** UTF-16 string.
2963*/
2964static int test_errmsg16(
2965 void * clientData,
2966 Tcl_Interp *interp,
2967 int objc,
2968 Tcl_Obj *CONST objv[]
2969){
drh5436dc22004-11-14 04:04:17 +00002970#ifndef SQLITE_OMIT_UTF16
drh9bb575f2004-09-06 17:24:11 +00002971 sqlite3 *db;
danielk19776622cce2004-05-20 11:00:52 +00002972 const void *zErr;
danielk1977950f0542006-01-18 05:51:57 +00002973 int bytes = 0;
danielk19776622cce2004-05-20 11:00:52 +00002974
2975 if( objc!=2 ){
2976 Tcl_AppendResult(interp, "wrong # args: should be \"",
2977 Tcl_GetString(objv[0]), " DB", 0);
2978 return TCL_ERROR;
2979 }
2980 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
2981
2982 zErr = sqlite3_errmsg16(db);
danielk1977950f0542006-01-18 05:51:57 +00002983 if( zErr ){
drhee858132007-05-08 20:37:38 +00002984 bytes = sqlite3Utf16ByteLen(zErr, -1);
danielk1977950f0542006-01-18 05:51:57 +00002985 }
danielk19776622cce2004-05-20 11:00:52 +00002986 Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(zErr, bytes));
drh5436dc22004-11-14 04:04:17 +00002987#endif /* SQLITE_OMIT_UTF16 */
danielk19776622cce2004-05-20 11:00:52 +00002988 return TCL_OK;
2989}
2990
2991/*
2992** Usage: sqlite3_prepare DB sql bytes tailvar
2993**
2994** Compile up to <bytes> bytes of the supplied SQL string <sql> using
2995** database handle <DB>. The parameter <tailval> is the name of a global
2996** variable that is set to the unused portion of <sql> (if any). A
2997** STMT handle is returned.
2998*/
2999static int test_prepare(
3000 void * clientData,
3001 Tcl_Interp *interp,
3002 int objc,
3003 Tcl_Obj *CONST objv[]
3004){
3005 sqlite3 *db;
3006 const char *zSql;
3007 int bytes;
3008 const char *zTail = 0;
3009 sqlite3_stmt *pStmt = 0;
3010 char zBuf[50];
danielk19774ad17132004-05-21 01:47:26 +00003011 int rc;
danielk19776622cce2004-05-20 11:00:52 +00003012
3013 if( objc!=5 ){
3014 Tcl_AppendResult(interp, "wrong # args: should be \"",
3015 Tcl_GetString(objv[0]), " DB sql bytes tailvar", 0);
3016 return TCL_ERROR;
3017 }
3018 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
3019 zSql = Tcl_GetString(objv[2]);
3020 if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR;
3021
danielk19774ad17132004-05-21 01:47:26 +00003022 rc = sqlite3_prepare(db, zSql, bytes, &pStmt, &zTail);
drhc60d0442004-09-30 13:43:13 +00003023 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
danielk19776622cce2004-05-20 11:00:52 +00003024 if( zTail ){
3025 if( bytes>=0 ){
3026 bytes = bytes - (zTail-zSql);
3027 }
3028 Tcl_ObjSetVar2(interp, objv[4], 0, Tcl_NewStringObj(zTail, bytes), 0);
3029 }
danielk19774ad17132004-05-21 01:47:26 +00003030 if( rc!=SQLITE_OK ){
3031 assert( pStmt==0 );
3032 sprintf(zBuf, "(%d) ", rc);
3033 Tcl_AppendResult(interp, zBuf, sqlite3_errmsg(db), 0);
3034 return TCL_ERROR;
3035 }
danielk19776622cce2004-05-20 11:00:52 +00003036
danielk19774ad17132004-05-21 01:47:26 +00003037 if( pStmt ){
drh64b1bea2006-01-15 02:30:57 +00003038 if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR;
danielk19774ad17132004-05-21 01:47:26 +00003039 Tcl_AppendResult(interp, zBuf, 0);
3040 }
danielk19776622cce2004-05-20 11:00:52 +00003041 return TCL_OK;
3042}
3043
3044/*
drhb900aaf2006-11-09 00:24:53 +00003045** Usage: sqlite3_prepare_v2 DB sql bytes tailvar
3046**
3047** Compile up to <bytes> bytes of the supplied SQL string <sql> using
3048** database handle <DB>. The parameter <tailval> is the name of a global
3049** variable that is set to the unused portion of <sql> (if any). A
3050** STMT handle is returned.
3051*/
3052static int test_prepare_v2(
3053 void * clientData,
3054 Tcl_Interp *interp,
3055 int objc,
3056 Tcl_Obj *CONST objv[]
3057){
3058 sqlite3 *db;
3059 const char *zSql;
3060 int bytes;
3061 const char *zTail = 0;
3062 sqlite3_stmt *pStmt = 0;
3063 char zBuf[50];
3064 int rc;
3065
3066 if( objc!=5 ){
3067 Tcl_AppendResult(interp, "wrong # args: should be \"",
3068 Tcl_GetString(objv[0]), " DB sql bytes tailvar", 0);
3069 return TCL_ERROR;
3070 }
3071 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
3072 zSql = Tcl_GetString(objv[2]);
3073 if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR;
3074
3075 rc = sqlite3_prepare_v2(db, zSql, bytes, &pStmt, &zTail);
danielk19777e29e952007-04-19 11:09:01 +00003076 assert(rc==SQLITE_OK || pStmt==0);
drhb900aaf2006-11-09 00:24:53 +00003077 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
3078 if( zTail ){
3079 if( bytes>=0 ){
3080 bytes = bytes - (zTail-zSql);
3081 }
3082 Tcl_ObjSetVar2(interp, objv[4], 0, Tcl_NewStringObj(zTail, bytes), 0);
3083 }
3084 if( rc!=SQLITE_OK ){
3085 assert( pStmt==0 );
3086 sprintf(zBuf, "(%d) ", rc);
3087 Tcl_AppendResult(interp, zBuf, sqlite3_errmsg(db), 0);
3088 return TCL_ERROR;
3089 }
3090
3091 if( pStmt ){
3092 if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR;
3093 Tcl_AppendResult(interp, zBuf, 0);
3094 }
3095 return TCL_OK;
3096}
3097
3098/*
3099** Usage: sqlite3_prepare16 DB sql bytes tailvar
danielk19776622cce2004-05-20 11:00:52 +00003100**
3101** Compile up to <bytes> bytes of the supplied SQL string <sql> using
3102** database handle <DB>. The parameter <tailval> is the name of a global
3103** variable that is set to the unused portion of <sql> (if any). A
3104** STMT handle is returned.
3105*/
3106static int test_prepare16(
3107 void * clientData,
3108 Tcl_Interp *interp,
3109 int objc,
3110 Tcl_Obj *CONST objv[]
3111){
drh5436dc22004-11-14 04:04:17 +00003112#ifndef SQLITE_OMIT_UTF16
danielk19776622cce2004-05-20 11:00:52 +00003113 sqlite3 *db;
3114 const void *zSql;
3115 const void *zTail = 0;
3116 Tcl_Obj *pTail = 0;
3117 sqlite3_stmt *pStmt = 0;
drhc60d0442004-09-30 13:43:13 +00003118 char zBuf[50];
3119 int rc;
danielk19776622cce2004-05-20 11:00:52 +00003120 int bytes; /* The integer specified as arg 3 */
3121 int objlen; /* The byte-array length of arg 2 */
3122
3123 if( objc!=5 ){
3124 Tcl_AppendResult(interp, "wrong # args: should be \"",
3125 Tcl_GetString(objv[0]), " DB sql bytes tailvar", 0);
3126 return TCL_ERROR;
3127 }
3128 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
3129 zSql = Tcl_GetByteArrayFromObj(objv[2], &objlen);
3130 if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR;
3131
drhc60d0442004-09-30 13:43:13 +00003132 rc = sqlite3_prepare16(db, zSql, bytes, &pStmt, &zTail);
3133 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
3134 if( rc ){
danielk19776622cce2004-05-20 11:00:52 +00003135 return TCL_ERROR;
3136 }
3137
3138 if( zTail ){
3139 objlen = objlen - ((u8 *)zTail-(u8 *)zSql);
3140 }else{
3141 objlen = 0;
3142 }
3143 pTail = Tcl_NewByteArrayObj((u8 *)zTail, objlen);
3144 Tcl_IncrRefCount(pTail);
3145 Tcl_ObjSetVar2(interp, objv[4], 0, pTail, 0);
danielk19774ad17132004-05-21 01:47:26 +00003146 Tcl_DecrRefCount(pTail);
danielk19776622cce2004-05-20 11:00:52 +00003147
danielk19774ad17132004-05-21 01:47:26 +00003148 if( pStmt ){
drh64b1bea2006-01-15 02:30:57 +00003149 if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR;
danielk19774ad17132004-05-21 01:47:26 +00003150 }
danielk19776622cce2004-05-20 11:00:52 +00003151 Tcl_AppendResult(interp, zBuf, 0);
drh5436dc22004-11-14 04:04:17 +00003152#endif /* SQLITE_OMIT_UTF16 */
danielk19776622cce2004-05-20 11:00:52 +00003153 return TCL_OK;
3154}
3155
danielk19774ad17132004-05-21 01:47:26 +00003156/*
drhb900aaf2006-11-09 00:24:53 +00003157** Usage: sqlite3_prepare16_v2 DB sql bytes tailvar
3158**
3159** Compile up to <bytes> bytes of the supplied SQL string <sql> using
3160** database handle <DB>. The parameter <tailval> is the name of a global
3161** variable that is set to the unused portion of <sql> (if any). A
3162** STMT handle is returned.
3163*/
3164static int test_prepare16_v2(
3165 void * clientData,
3166 Tcl_Interp *interp,
3167 int objc,
3168 Tcl_Obj *CONST objv[]
3169){
3170#ifndef SQLITE_OMIT_UTF16
3171 sqlite3 *db;
3172 const void *zSql;
3173 const void *zTail = 0;
3174 Tcl_Obj *pTail = 0;
3175 sqlite3_stmt *pStmt = 0;
3176 char zBuf[50];
3177 int rc;
3178 int bytes; /* The integer specified as arg 3 */
3179 int objlen; /* The byte-array length of arg 2 */
3180
3181 if( objc!=5 ){
3182 Tcl_AppendResult(interp, "wrong # args: should be \"",
3183 Tcl_GetString(objv[0]), " DB sql bytes tailvar", 0);
3184 return TCL_ERROR;
3185 }
3186 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
3187 zSql = Tcl_GetByteArrayFromObj(objv[2], &objlen);
3188 if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR;
3189
3190 rc = sqlite3_prepare16_v2(db, zSql, bytes, &pStmt, &zTail);
3191 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
3192 if( rc ){
3193 return TCL_ERROR;
3194 }
3195
3196 if( zTail ){
3197 objlen = objlen - ((u8 *)zTail-(u8 *)zSql);
3198 }else{
3199 objlen = 0;
3200 }
3201 pTail = Tcl_NewByteArrayObj((u8 *)zTail, objlen);
3202 Tcl_IncrRefCount(pTail);
3203 Tcl_ObjSetVar2(interp, objv[4], 0, pTail, 0);
3204 Tcl_DecrRefCount(pTail);
3205
3206 if( pStmt ){
3207 if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR;
3208 }
3209 Tcl_AppendResult(interp, zBuf, 0);
3210#endif /* SQLITE_OMIT_UTF16 */
3211 return TCL_OK;
3212}
3213
3214/*
danielk19774ad17132004-05-21 01:47:26 +00003215** Usage: sqlite3_open filename ?options-list?
3216*/
3217static int test_open(
3218 void * clientData,
3219 Tcl_Interp *interp,
3220 int objc,
3221 Tcl_Obj *CONST objv[]
3222){
3223 const char *zFilename;
3224 sqlite3 *db;
3225 int rc;
3226 char zBuf[100];
3227
3228 if( objc!=3 && objc!=2 ){
3229 Tcl_AppendResult(interp, "wrong # args: should be \"",
3230 Tcl_GetString(objv[0]), " filename options-list", 0);
3231 return TCL_ERROR;
3232 }
3233
3234 zFilename = Tcl_GetString(objv[1]);
danielk19774f057f92004-06-08 00:02:33 +00003235 rc = sqlite3_open(zFilename, &db);
danielk19774ad17132004-05-21 01:47:26 +00003236
drh64b1bea2006-01-15 02:30:57 +00003237 if( sqlite3TestMakePointerStr(interp, zBuf, db) ) return TCL_ERROR;
danielk19774ad17132004-05-21 01:47:26 +00003238 Tcl_AppendResult(interp, zBuf, 0);
3239 return TCL_OK;
3240}
3241
3242/*
3243** Usage: sqlite3_open16 filename options
3244*/
3245static int test_open16(
3246 void * clientData,
3247 Tcl_Interp *interp,
3248 int objc,
3249 Tcl_Obj *CONST objv[]
3250){
drh5436dc22004-11-14 04:04:17 +00003251#ifndef SQLITE_OMIT_UTF16
danielk19774ad17132004-05-21 01:47:26 +00003252 const void *zFilename;
3253 sqlite3 *db;
3254 int rc;
3255 char zBuf[100];
3256
3257 if( objc!=3 ){
3258 Tcl_AppendResult(interp, "wrong # args: should be \"",
3259 Tcl_GetString(objv[0]), " filename options-list", 0);
3260 return TCL_ERROR;
3261 }
3262
3263 zFilename = Tcl_GetByteArrayFromObj(objv[1], 0);
danielk19774f057f92004-06-08 00:02:33 +00003264 rc = sqlite3_open16(zFilename, &db);
danielk19774ad17132004-05-21 01:47:26 +00003265
drh64b1bea2006-01-15 02:30:57 +00003266 if( sqlite3TestMakePointerStr(interp, zBuf, db) ) return TCL_ERROR;
danielk19774ad17132004-05-21 01:47:26 +00003267 Tcl_AppendResult(interp, zBuf, 0);
drh5436dc22004-11-14 04:04:17 +00003268#endif /* SQLITE_OMIT_UTF16 */
danielk19774ad17132004-05-21 01:47:26 +00003269 return TCL_OK;
3270}
drhd3d39e92004-05-20 22:16:29 +00003271
3272/*
danielk1977bc6ada42004-06-30 08:20:16 +00003273** Usage: sqlite3_complete16 <UTF-16 string>
3274**
3275** Return 1 if the supplied argument is a complete SQL statement, or zero
3276** otherwise.
3277*/
3278static int test_complete16(
3279 void * clientData,
3280 Tcl_Interp *interp,
3281 int objc,
3282 Tcl_Obj *CONST objv[]
3283){
drhccae6022005-02-26 17:31:26 +00003284#if !defined(SQLITE_OMIT_COMPLETE) && !defined(SQLITE_OMIT_UTF16)
danielk1977bc6ada42004-06-30 08:20:16 +00003285 char *zBuf;
3286
3287 if( objc!=2 ){
3288 Tcl_WrongNumArgs(interp, 1, objv, "<utf-16 sql>");
3289 return TCL_ERROR;
3290 }
3291
drh03d847e2005-12-09 20:21:58 +00003292 zBuf = (char*)Tcl_GetByteArrayFromObj(objv[1], 0);
danielk1977bc6ada42004-06-30 08:20:16 +00003293 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_complete16(zBuf)));
drhccae6022005-02-26 17:31:26 +00003294#endif /* SQLITE_OMIT_COMPLETE && SQLITE_OMIT_UTF16 */
danielk1977bc6ada42004-06-30 08:20:16 +00003295 return TCL_OK;
3296}
3297
3298/*
danielk1977106bb232004-05-21 10:08:53 +00003299** Usage: sqlite3_step STMT
3300**
3301** Advance the statement to the next row.
3302*/
danielk197717240fd2004-05-26 00:07:25 +00003303static int test_step(
danielk1977106bb232004-05-21 10:08:53 +00003304 void * clientData,
3305 Tcl_Interp *interp,
3306 int objc,
3307 Tcl_Obj *CONST objv[]
3308){
3309 sqlite3_stmt *pStmt;
3310 int rc;
3311
danielk1977e1cd9872004-05-22 10:33:04 +00003312 if( objc!=2 ){
danielk1977106bb232004-05-21 10:08:53 +00003313 Tcl_AppendResult(interp, "wrong # args: should be \"",
3314 Tcl_GetString(objv[0]), " STMT", 0);
3315 return TCL_ERROR;
3316 }
3317
3318 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
danielk197717240fd2004-05-26 00:07:25 +00003319 rc = sqlite3_step(pStmt);
danielk1977106bb232004-05-21 10:08:53 +00003320
danielk1977fbcd5852004-06-15 02:44:18 +00003321 /* if( rc!=SQLITE_DONE && rc!=SQLITE_ROW ) return TCL_ERROR; */
drh4f0c5872007-03-26 22:05:01 +00003322 Tcl_SetResult(interp, (char *)t1ErrorName(rc), 0);
danielk1977e1cd9872004-05-22 10:33:04 +00003323 return TCL_OK;
3324}
3325
3326/*
danielk197717240fd2004-05-26 00:07:25 +00003327** Usage: sqlite3_column_count STMT
3328**
3329** Return the number of columns returned by the sql statement STMT.
3330*/
3331static int test_column_count(
3332 void * clientData,
3333 Tcl_Interp *interp,
3334 int objc,
3335 Tcl_Obj *CONST objv[]
3336){
3337 sqlite3_stmt *pStmt;
3338
3339 if( objc!=2 ){
3340 Tcl_AppendResult(interp, "wrong # args: should be \"",
3341 Tcl_GetString(objv[0]), " STMT column", 0);
3342 return TCL_ERROR;
3343 }
3344
3345 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3346
3347 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_column_count(pStmt)));
3348 return TCL_OK;
3349}
3350
3351/*
danielk19773cf86062004-05-26 10:11:05 +00003352** Usage: sqlite3_column_type STMT column
3353**
3354** Return the type of the data in column 'column' of the current row.
3355*/
3356static int test_column_type(
3357 void * clientData,
3358 Tcl_Interp *interp,
3359 int objc,
3360 Tcl_Obj *CONST objv[]
3361){
3362 sqlite3_stmt *pStmt;
3363 int col;
3364 int tp;
3365
3366 if( objc!=3 ){
3367 Tcl_AppendResult(interp, "wrong # args: should be \"",
3368 Tcl_GetString(objv[0]), " STMT column", 0);
3369 return TCL_ERROR;
3370 }
3371
3372 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3373 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
3374
3375 tp = sqlite3_column_type(pStmt, col);
3376 switch( tp ){
drh9c054832004-05-31 18:51:57 +00003377 case SQLITE_INTEGER:
danielk19773cf86062004-05-26 10:11:05 +00003378 Tcl_SetResult(interp, "INTEGER", TCL_STATIC);
3379 break;
drh9c054832004-05-31 18:51:57 +00003380 case SQLITE_NULL:
danielk19773cf86062004-05-26 10:11:05 +00003381 Tcl_SetResult(interp, "NULL", TCL_STATIC);
3382 break;
drh9c054832004-05-31 18:51:57 +00003383 case SQLITE_FLOAT:
danielk19773cf86062004-05-26 10:11:05 +00003384 Tcl_SetResult(interp, "FLOAT", TCL_STATIC);
3385 break;
drh9c054832004-05-31 18:51:57 +00003386 case SQLITE_TEXT:
danielk19773cf86062004-05-26 10:11:05 +00003387 Tcl_SetResult(interp, "TEXT", TCL_STATIC);
3388 break;
drh9c054832004-05-31 18:51:57 +00003389 case SQLITE_BLOB:
danielk19773cf86062004-05-26 10:11:05 +00003390 Tcl_SetResult(interp, "BLOB", TCL_STATIC);
3391 break;
3392 default:
3393 assert(0);
3394 }
3395
3396 return TCL_OK;
3397}
3398
3399/*
danielk197704f2e682004-05-27 01:04:07 +00003400** Usage: sqlite3_column_int64 STMT column
danielk19773cf86062004-05-26 10:11:05 +00003401**
3402** Return the data in column 'column' of the current row cast as an
danielk197704f2e682004-05-27 01:04:07 +00003403** wide (64-bit) integer.
danielk19773cf86062004-05-26 10:11:05 +00003404*/
danielk197704f2e682004-05-27 01:04:07 +00003405static int test_column_int64(
danielk19773cf86062004-05-26 10:11:05 +00003406 void * clientData,
3407 Tcl_Interp *interp,
3408 int objc,
3409 Tcl_Obj *CONST objv[]
3410){
3411 sqlite3_stmt *pStmt;
3412 int col;
danielk197704f2e682004-05-27 01:04:07 +00003413 i64 iVal;
danielk19773cf86062004-05-26 10:11:05 +00003414
3415 if( objc!=3 ){
3416 Tcl_AppendResult(interp, "wrong # args: should be \"",
3417 Tcl_GetString(objv[0]), " STMT column", 0);
3418 return TCL_ERROR;
3419 }
3420
3421 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3422 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
3423
danielk197704f2e682004-05-27 01:04:07 +00003424 iVal = sqlite3_column_int64(pStmt, col);
3425 Tcl_SetObjResult(interp, Tcl_NewWideIntObj(iVal));
3426 return TCL_OK;
3427}
3428
3429/*
danielk1977ea61b2c2004-05-27 01:49:51 +00003430** Usage: sqlite3_column_blob STMT column
3431*/
3432static int test_column_blob(
3433 void * clientData,
3434 Tcl_Interp *interp,
3435 int objc,
3436 Tcl_Obj *CONST objv[]
3437){
3438 sqlite3_stmt *pStmt;
3439 int col;
3440
3441 int len;
danielk1977c572ef72004-05-27 09:28:41 +00003442 const void *pBlob;
danielk1977ea61b2c2004-05-27 01:49:51 +00003443
3444 if( objc!=3 ){
3445 Tcl_AppendResult(interp, "wrong # args: should be \"",
3446 Tcl_GetString(objv[0]), " STMT column", 0);
3447 return TCL_ERROR;
3448 }
3449
3450 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3451 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
3452
danielk1977ea61b2c2004-05-27 01:49:51 +00003453 len = sqlite3_column_bytes(pStmt, col);
drh9310ef22007-04-27 17:16:20 +00003454 pBlob = sqlite3_column_blob(pStmt, col);
danielk1977ea61b2c2004-05-27 01:49:51 +00003455 Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(pBlob, len));
3456 return TCL_OK;
3457}
3458
3459/*
danielk197704f2e682004-05-27 01:04:07 +00003460** Usage: sqlite3_column_double STMT column
3461**
3462** Return the data in column 'column' of the current row cast as a double.
3463*/
3464static int test_column_double(
3465 void * clientData,
3466 Tcl_Interp *interp,
3467 int objc,
3468 Tcl_Obj *CONST objv[]
3469){
3470 sqlite3_stmt *pStmt;
3471 int col;
3472 double rVal;
3473
3474 if( objc!=3 ){
3475 Tcl_AppendResult(interp, "wrong # args: should be \"",
3476 Tcl_GetString(objv[0]), " STMT column", 0);
3477 return TCL_ERROR;
3478 }
3479
3480 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3481 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
3482
3483 rVal = sqlite3_column_double(pStmt, col);
danielk1977c572ef72004-05-27 09:28:41 +00003484 Tcl_SetObjResult(interp, Tcl_NewDoubleObj(rVal));
danielk19773cf86062004-05-26 10:11:05 +00003485 return TCL_OK;
3486}
3487
3488/*
danielk197717240fd2004-05-26 00:07:25 +00003489** Usage: sqlite3_data_count STMT
3490**
3491** Return the number of columns returned by the sql statement STMT.
3492*/
3493static int test_data_count(
3494 void * clientData,
3495 Tcl_Interp *interp,
3496 int objc,
3497 Tcl_Obj *CONST objv[]
3498){
3499 sqlite3_stmt *pStmt;
3500
3501 if( objc!=2 ){
3502 Tcl_AppendResult(interp, "wrong # args: should be \"",
3503 Tcl_GetString(objv[0]), " STMT column", 0);
3504 return TCL_ERROR;
3505 }
3506
3507 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3508
3509 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_data_count(pStmt)));
3510 return TCL_OK;
3511}
3512
3513/*
danielk197704f2e682004-05-27 01:04:07 +00003514** Usage: sqlite3_column_text STMT column
3515**
3516** Usage: sqlite3_column_decltype STMT column
3517**
3518** Usage: sqlite3_column_name STMT column
3519*/
3520static int test_stmt_utf8(
drh241db312004-06-22 12:46:53 +00003521 void * clientData, /* Pointer to SQLite API function to be invoke */
danielk197704f2e682004-05-27 01:04:07 +00003522 Tcl_Interp *interp,
3523 int objc,
3524 Tcl_Obj *CONST objv[]
3525){
3526 sqlite3_stmt *pStmt;
3527 int col;
danielk1977c572ef72004-05-27 09:28:41 +00003528 const char *(*xFunc)(sqlite3_stmt*, int) = clientData;
danielk1977f93bbbe2004-05-27 10:30:52 +00003529 const char *zRet;
danielk197704f2e682004-05-27 01:04:07 +00003530
3531 if( objc!=3 ){
3532 Tcl_AppendResult(interp, "wrong # args: should be \"",
3533 Tcl_GetString(objv[0]), " STMT column", 0);
3534 return TCL_ERROR;
3535 }
3536
3537 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3538 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
danielk1977f93bbbe2004-05-27 10:30:52 +00003539 zRet = xFunc(pStmt, col);
3540 if( zRet ){
3541 Tcl_SetResult(interp, (char *)zRet, 0);
3542 }
danielk197704f2e682004-05-27 01:04:07 +00003543 return TCL_OK;
3544}
3545
danielk19776b456a22005-03-21 04:04:02 +00003546static int test_global_recover(
3547 void * clientData,
3548 Tcl_Interp *interp,
3549 int objc,
3550 Tcl_Obj *CONST objv[]
3551){
3552#ifndef SQLITE_OMIT_GLOBALRECOVER
3553 int rc;
3554 if( objc!=1 ){
3555 Tcl_WrongNumArgs(interp, 1, objv, "");
3556 return TCL_ERROR;
3557 }
3558 rc = sqlite3_global_recover();
drh4f0c5872007-03-26 22:05:01 +00003559 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
danielk19776b456a22005-03-21 04:04:02 +00003560#endif
3561 return TCL_OK;
3562}
3563
danielk197704f2e682004-05-27 01:04:07 +00003564/*
3565** Usage: sqlite3_column_text STMT column
3566**
3567** Usage: sqlite3_column_decltype STMT column
3568**
3569** Usage: sqlite3_column_name STMT column
3570*/
3571static int test_stmt_utf16(
drh241db312004-06-22 12:46:53 +00003572 void * clientData, /* Pointer to SQLite API function to be invoked */
danielk197704f2e682004-05-27 01:04:07 +00003573 Tcl_Interp *interp,
3574 int objc,
3575 Tcl_Obj *CONST objv[]
3576){
drh5436dc22004-11-14 04:04:17 +00003577#ifndef SQLITE_OMIT_UTF16
danielk197704f2e682004-05-27 01:04:07 +00003578 sqlite3_stmt *pStmt;
3579 int col;
3580 Tcl_Obj *pRet;
3581 const void *zName16;
danielk1977c572ef72004-05-27 09:28:41 +00003582 const void *(*xFunc)(sqlite3_stmt*, int) = clientData;
danielk197704f2e682004-05-27 01:04:07 +00003583
3584 if( objc!=3 ){
3585 Tcl_AppendResult(interp, "wrong # args: should be \"",
3586 Tcl_GetString(objv[0]), " STMT column", 0);
3587 return TCL_ERROR;
3588 }
3589
3590 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3591 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
3592
3593 zName16 = xFunc(pStmt, col);
danielk1977f93bbbe2004-05-27 10:30:52 +00003594 if( zName16 ){
drhee858132007-05-08 20:37:38 +00003595 pRet = Tcl_NewByteArrayObj(zName16, sqlite3Utf16ByteLen(zName16, -1)+2);
danielk1977f93bbbe2004-05-27 10:30:52 +00003596 Tcl_SetObjResult(interp, pRet);
3597 }
drh5436dc22004-11-14 04:04:17 +00003598#endif /* SQLITE_OMIT_UTF16 */
danielk197704f2e682004-05-27 01:04:07 +00003599
3600 return TCL_OK;
3601}
3602
3603/*
3604** Usage: sqlite3_column_int STMT column
3605**
3606** Usage: sqlite3_column_bytes STMT column
3607**
3608** Usage: sqlite3_column_bytes16 STMT column
3609**
3610*/
3611static int test_stmt_int(
drh241db312004-06-22 12:46:53 +00003612 void * clientData, /* Pointer to SQLite API function to be invoked */
danielk197704f2e682004-05-27 01:04:07 +00003613 Tcl_Interp *interp,
3614 int objc,
3615 Tcl_Obj *CONST objv[]
3616){
3617 sqlite3_stmt *pStmt;
3618 int col;
danielk1977c572ef72004-05-27 09:28:41 +00003619 int (*xFunc)(sqlite3_stmt*, int) = clientData;
danielk197704f2e682004-05-27 01:04:07 +00003620
3621 if( objc!=3 ){
3622 Tcl_AppendResult(interp, "wrong # args: should be \"",
3623 Tcl_GetString(objv[0]), " STMT column", 0);
3624 return TCL_ERROR;
3625 }
3626
3627 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3628 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
3629
3630 Tcl_SetObjResult(interp, Tcl_NewIntObj(xFunc(pStmt, col)));
3631 return TCL_OK;
3632}
3633
danielk197744ee5bf2005-05-27 09:41:12 +00003634#ifndef SQLITE_OMIT_DISKIO
danielk1977b4b47412007-08-17 15:53:36 +00003635#if 0
danielk19779a1d0ab2004-06-01 14:09:28 +00003636/*
3637** Usage: sqlite3OsOpenReadWrite <filename>
3638*/
3639static int test_sqlite3OsOpenReadWrite(
3640 void * clientData,
3641 Tcl_Interp *interp,
3642 int objc,
3643 Tcl_Obj *CONST objv[]
3644){
danielk19771e536952007-08-16 10:09:01 +00003645 sqlite3_file *pFile;
danielk19779a1d0ab2004-06-01 14:09:28 +00003646 int rc;
3647 int dummy;
3648 char zBuf[100];
danielk197704f2e682004-05-27 01:04:07 +00003649
danielk19779a1d0ab2004-06-01 14:09:28 +00003650 if( objc!=2 ){
3651 Tcl_AppendResult(interp, "wrong # args: should be \"",
3652 Tcl_GetString(objv[0]), " filename", 0);
3653 return TCL_ERROR;
3654 }
3655
drh66560ad2006-01-06 14:32:19 +00003656 rc = sqlite3OsOpenReadWrite(Tcl_GetString(objv[1]), &pFile, &dummy);
danielk19779a1d0ab2004-06-01 14:09:28 +00003657 if( rc!=SQLITE_OK ){
drh4f0c5872007-03-26 22:05:01 +00003658 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
danielk19779a1d0ab2004-06-01 14:09:28 +00003659 return TCL_ERROR;
3660 }
drh64b1bea2006-01-15 02:30:57 +00003661 sqlite3TestMakePointerStr(interp, zBuf, pFile);
danielk19779a1d0ab2004-06-01 14:09:28 +00003662 Tcl_SetResult(interp, zBuf, 0);
3663 return TCL_ERROR;
3664}
3665
3666/*
3667** Usage: sqlite3OsClose <file handle>
3668*/
3669static int test_sqlite3OsClose(
3670 void * clientData,
3671 Tcl_Interp *interp,
3672 int objc,
3673 Tcl_Obj *CONST objv[]
3674){
danielk19771e536952007-08-16 10:09:01 +00003675 sqlite3_file *pFile;
danielk19779a1d0ab2004-06-01 14:09:28 +00003676 int rc;
3677
3678 if( objc!=2 ){
3679 Tcl_AppendResult(interp, "wrong # args: should be \"",
3680 Tcl_GetString(objv[0]), " filehandle", 0);
3681 return TCL_ERROR;
3682 }
3683
3684 if( getFilePointer(interp, Tcl_GetString(objv[1]), &pFile) ){
3685 return TCL_ERROR;
3686 }
drh054889e2005-11-30 03:20:31 +00003687 rc = sqlite3OsClose(&pFile);
danielk19779a1d0ab2004-06-01 14:09:28 +00003688 if( rc!=SQLITE_OK ){
drh4f0c5872007-03-26 22:05:01 +00003689 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
danielk19779a1d0ab2004-06-01 14:09:28 +00003690 return TCL_ERROR;
3691 }
danielk19779a1d0ab2004-06-01 14:09:28 +00003692 return TCL_OK;
3693}
3694
3695/*
3696** Usage: sqlite3OsLock <file handle> <locktype>
3697*/
3698static int test_sqlite3OsLock(
3699 void * clientData,
3700 Tcl_Interp *interp,
3701 int objc,
3702 Tcl_Obj *CONST objv[]
3703){
danielk19771e536952007-08-16 10:09:01 +00003704 sqlite3_file * pFile;
danielk19779a1d0ab2004-06-01 14:09:28 +00003705 int rc;
3706
3707 if( objc!=3 ){
3708 Tcl_AppendResult(interp, "wrong # args: should be \"",
3709 Tcl_GetString(objv[0]),
3710 " filehandle (SHARED|RESERVED|PENDING|EXCLUSIVE)", 0);
3711 return TCL_ERROR;
3712 }
3713
3714 if( getFilePointer(interp, Tcl_GetString(objv[1]), &pFile) ){
3715 return TCL_ERROR;
3716 }
3717
3718 if( 0==strcmp("SHARED", Tcl_GetString(objv[2])) ){
drh054889e2005-11-30 03:20:31 +00003719 rc = sqlite3OsLock(pFile, SHARED_LOCK);
danielk19779a1d0ab2004-06-01 14:09:28 +00003720 }
3721 else if( 0==strcmp("RESERVED", Tcl_GetString(objv[2])) ){
drh054889e2005-11-30 03:20:31 +00003722 rc = sqlite3OsLock(pFile, RESERVED_LOCK);
danielk19779a1d0ab2004-06-01 14:09:28 +00003723 }
3724 else if( 0==strcmp("PENDING", Tcl_GetString(objv[2])) ){
drh054889e2005-11-30 03:20:31 +00003725 rc = sqlite3OsLock(pFile, PENDING_LOCK);
danielk19779a1d0ab2004-06-01 14:09:28 +00003726 }
3727 else if( 0==strcmp("EXCLUSIVE", Tcl_GetString(objv[2])) ){
drh054889e2005-11-30 03:20:31 +00003728 rc = sqlite3OsLock(pFile, EXCLUSIVE_LOCK);
danielk19779a1d0ab2004-06-01 14:09:28 +00003729 }else{
3730 Tcl_AppendResult(interp, "wrong # args: should be \"",
3731 Tcl_GetString(objv[0]),
3732 " filehandle (SHARED|RESERVED|PENDING|EXCLUSIVE)", 0);
3733 return TCL_ERROR;
3734 }
3735
3736 if( rc!=SQLITE_OK ){
drh4f0c5872007-03-26 22:05:01 +00003737 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
danielk19779a1d0ab2004-06-01 14:09:28 +00003738 return TCL_ERROR;
3739 }
3740 return TCL_OK;
3741}
3742
3743/*
3744** Usage: sqlite3OsUnlock <file handle>
3745*/
3746static int test_sqlite3OsUnlock(
3747 void * clientData,
3748 Tcl_Interp *interp,
3749 int objc,
3750 Tcl_Obj *CONST objv[]
3751){
danielk19771e536952007-08-16 10:09:01 +00003752 sqlite3_file * pFile;
danielk19779a1d0ab2004-06-01 14:09:28 +00003753 int rc;
3754
3755 if( objc!=2 ){
3756 Tcl_AppendResult(interp, "wrong # args: should be \"",
3757 Tcl_GetString(objv[0]), " filehandle", 0);
3758 return TCL_ERROR;
3759 }
3760
3761 if( getFilePointer(interp, Tcl_GetString(objv[1]), &pFile) ){
3762 return TCL_ERROR;
3763 }
drh054889e2005-11-30 03:20:31 +00003764 rc = sqlite3OsUnlock(pFile, NO_LOCK);
danielk19779a1d0ab2004-06-01 14:09:28 +00003765 if( rc!=SQLITE_OK ){
drh4f0c5872007-03-26 22:05:01 +00003766 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
danielk19779a1d0ab2004-06-01 14:09:28 +00003767 return TCL_ERROR;
3768 }
3769 return TCL_OK;
3770}
drhd3d39e92004-05-20 22:16:29 +00003771
drhab3f9fe2004-08-14 17:10:10 +00003772/*
3773** Usage: sqlite3OsTempFileName
3774*/
3775static int test_sqlite3OsTempFileName(
3776 void * clientData,
3777 Tcl_Interp *interp,
3778 int objc,
3779 Tcl_Obj *CONST objv[]
3780){
3781 char zFile[SQLITE_TEMPNAME_SIZE];
3782 int rc;
3783
drh66560ad2006-01-06 14:32:19 +00003784 rc = sqlite3OsTempFileName(zFile);
drhab3f9fe2004-08-14 17:10:10 +00003785 if( rc!=SQLITE_OK ){
drh4f0c5872007-03-26 22:05:01 +00003786 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
drhab3f9fe2004-08-14 17:10:10 +00003787 return TCL_ERROR;
3788 }
3789 Tcl_AppendResult(interp, zFile, 0);
3790 return TCL_OK;
3791}
danielk197744ee5bf2005-05-27 09:41:12 +00003792#endif
danielk1977b4b47412007-08-17 15:53:36 +00003793#endif
danielk1977d8123362004-06-12 09:25:12 +00003794
danielk19776622cce2004-05-20 11:00:52 +00003795/*
drhcacb2082005-01-11 15:28:33 +00003796** Usage: sqlite_set_magic DB MAGIC-NUMBER
3797**
3798** Set the db->magic value. This is used to test error recovery logic.
3799*/
3800static int sqlite_set_magic(
3801 void * clientData,
3802 Tcl_Interp *interp,
3803 int argc,
3804 char **argv
3805){
3806 sqlite3 *db;
3807 if( argc!=3 ){
3808 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
3809 " DB MAGIC", 0);
3810 return TCL_ERROR;
3811 }
3812 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
3813 if( strcmp(argv[2], "SQLITE_MAGIC_OPEN")==0 ){
3814 db->magic = SQLITE_MAGIC_OPEN;
3815 }else if( strcmp(argv[2], "SQLITE_MAGIC_CLOSED")==0 ){
3816 db->magic = SQLITE_MAGIC_CLOSED;
3817 }else if( strcmp(argv[2], "SQLITE_MAGIC_BUSY")==0 ){
3818 db->magic = SQLITE_MAGIC_BUSY;
3819 }else if( strcmp(argv[2], "SQLITE_MAGIC_ERROR")==0 ){
3820 db->magic = SQLITE_MAGIC_ERROR;
3821 }else if( Tcl_GetInt(interp, argv[2], &db->magic) ){
3822 return TCL_ERROR;
3823 }
3824 return TCL_OK;
3825}
3826
3827/*
drhc5cdca62005-01-11 16:54:14 +00003828** Usage: sqlite3_interrupt DB
3829**
3830** Trigger an interrupt on DB
3831*/
3832static int test_interrupt(
3833 void * clientData,
3834 Tcl_Interp *interp,
3835 int argc,
3836 char **argv
3837){
3838 sqlite3 *db;
3839 if( argc!=2 ){
3840 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " DB", 0);
3841 return TCL_ERROR;
3842 }
3843 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
3844 sqlite3_interrupt(db);
3845 return TCL_OK;
3846}
3847
drh79158e12005-09-06 21:40:45 +00003848static u8 *sqlite3_stack_baseline = 0;
3849
drhc5cdca62005-01-11 16:54:14 +00003850/*
drh79158e12005-09-06 21:40:45 +00003851** Fill the stack with a known bitpattern.
danielk1977600dd0b2005-01-20 01:14:23 +00003852*/
drh79158e12005-09-06 21:40:45 +00003853static void prepStack(void){
3854 int i;
3855 u32 bigBuf[65536];
3856 for(i=0; i<sizeof(bigBuf); i++) bigBuf[i] = 0xdeadbeef;
3857 sqlite3_stack_baseline = (u8*)&bigBuf[65536];
3858}
3859
3860/*
3861** Get the current stack depth. Used for debugging only.
3862*/
3863u64 sqlite3StackDepth(void){
3864 u8 x;
3865 return (u64)(sqlite3_stack_baseline - &x);
3866}
3867
3868/*
3869** Usage: sqlite3_stack_used DB SQL
3870**
3871** Try to measure the amount of stack space used by a call to sqlite3_exec
3872*/
3873static int test_stack_used(
danielk1977600dd0b2005-01-20 01:14:23 +00003874 void * clientData,
3875 Tcl_Interp *interp,
3876 int argc,
3877 char **argv
3878){
3879 sqlite3 *db;
drh79158e12005-09-06 21:40:45 +00003880 int i;
3881 if( argc!=3 ){
3882 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
3883 " DB SQL", 0);
danielk1977600dd0b2005-01-20 01:14:23 +00003884 return TCL_ERROR;
3885 }
drh79158e12005-09-06 21:40:45 +00003886 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
3887 prepStack();
drh37527852006-03-16 16:19:56 +00003888 (void)sqlite3_exec(db, argv[2], 0, 0, 0);
drh79158e12005-09-06 21:40:45 +00003889 for(i=65535; i>=0 && ((u32*)sqlite3_stack_baseline)[-i]==0xdeadbeef; i--){}
3890 Tcl_SetObjResult(interp, Tcl_NewIntObj(i*4));
danielk1977600dd0b2005-01-20 01:14:23 +00003891 return TCL_OK;
3892}
danielk1977600dd0b2005-01-20 01:14:23 +00003893
3894/*
danielk19779636c4e2005-01-25 04:27:54 +00003895** Usage: sqlite_delete_function DB function-name
3896**
3897** Delete the user function 'function-name' from database handle DB. It
3898** is assumed that the user function was created as UTF8, any number of
3899** arguments (the way the TCL interface does it).
3900*/
3901static int delete_function(
3902 void * clientData,
3903 Tcl_Interp *interp,
3904 int argc,
3905 char **argv
3906){
3907 int rc;
3908 sqlite3 *db;
3909 if( argc!=3 ){
3910 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
3911 " DB function-name", 0);
3912 return TCL_ERROR;
3913 }
3914 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
3915 rc = sqlite3_create_function(db, argv[2], -1, SQLITE_UTF8, 0, 0, 0, 0);
drh4f0c5872007-03-26 22:05:01 +00003916 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
danielk19779636c4e2005-01-25 04:27:54 +00003917 return TCL_OK;
3918}
3919
3920/*
3921** Usage: sqlite_delete_collation DB collation-name
3922**
3923** Delete the collation sequence 'collation-name' from database handle
3924** DB. It is assumed that the collation sequence was created as UTF8 (the
3925** way the TCL interface does it).
3926*/
3927static int delete_collation(
3928 void * clientData,
3929 Tcl_Interp *interp,
3930 int argc,
3931 char **argv
3932){
3933 int rc;
3934 sqlite3 *db;
3935 if( argc!=3 ){
3936 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
3937 " DB function-name", 0);
3938 return TCL_ERROR;
3939 }
3940 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
3941 rc = sqlite3_create_collation(db, argv[2], SQLITE_UTF8, 0, 0);
drh4f0c5872007-03-26 22:05:01 +00003942 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
danielk19779636c4e2005-01-25 04:27:54 +00003943 return TCL_OK;
3944}
3945
3946/*
drh3e1d8e62005-05-26 16:23:34 +00003947** Usage: sqlite3_get_autocommit DB
3948**
3949** Return true if the database DB is currently in auto-commit mode.
3950** Return false if not.
3951*/
3952static int get_autocommit(
3953 void * clientData,
3954 Tcl_Interp *interp,
3955 int argc,
3956 char **argv
3957){
3958 char zBuf[30];
3959 sqlite3 *db;
3960 if( argc!=2 ){
3961 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
3962 " DB", 0);
3963 return TCL_ERROR;
3964 }
3965 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
3966 sprintf(zBuf, "%d", sqlite3_get_autocommit(db));
3967 Tcl_AppendResult(interp, zBuf, 0);
3968 return TCL_OK;
3969}
3970
3971/*
drh30867652006-07-06 10:59:57 +00003972** Usage: sqlite3_busy_timeout DB MS
3973**
3974** Set the busy timeout. This is more easily done using the timeout
3975** method of the TCL interface. But we need a way to test the case
3976** where it returns SQLITE_MISUSE.
3977*/
3978static int test_busy_timeout(
3979 void * clientData,
3980 Tcl_Interp *interp,
3981 int argc,
3982 char **argv
3983){
3984 int rc, ms;
3985 sqlite3 *db;
3986 if( argc!=3 ){
3987 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
3988 " DB", 0);
3989 return TCL_ERROR;
3990 }
3991 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
3992 if( Tcl_GetInt(interp, argv[2], &ms) ) return TCL_ERROR;
3993 rc = sqlite3_busy_timeout(db, ms);
3994 Tcl_AppendResult(interp, sqlite3TestErrorName(rc), 0);
3995 return TCL_OK;
3996}
3997
3998/*
drh92febd92004-08-20 18:34:20 +00003999** Usage: tcl_variable_type VARIABLENAME
4000**
4001** Return the name of the internal representation for the
4002** value of the given variable.
4003*/
4004static int tcl_variable_type(
4005 void * clientData,
4006 Tcl_Interp *interp,
4007 int objc,
4008 Tcl_Obj *CONST objv[]
4009){
4010 Tcl_Obj *pVar;
4011 if( objc!=2 ){
4012 Tcl_WrongNumArgs(interp, 1, objv, "VARIABLE");
4013 return TCL_ERROR;
4014 }
4015 pVar = Tcl_GetVar2Ex(interp, Tcl_GetString(objv[1]), 0, TCL_LEAVE_ERR_MSG);
4016 if( pVar==0 ) return TCL_ERROR;
4017 if( pVar->typePtr ){
4018 Tcl_SetObjResult(interp, Tcl_NewStringObj(pVar->typePtr->name, -1));
4019 }
4020 return TCL_OK;
4021}
4022
4023/*
drh6aafc292006-01-05 15:50:06 +00004024** Usage: sqlite3_release_memory ?N?
4025**
4026** Attempt to release memory currently held but not actually required.
4027** The integer N is the number of bytes we are trying to release. The
4028** return value is the amount of memory actually released.
4029*/
4030static int test_release_memory(
4031 void * clientData,
4032 Tcl_Interp *interp,
4033 int objc,
4034 Tcl_Obj *CONST objv[]
4035){
drh6f7adc82006-01-11 21:41:20 +00004036#if defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) && !defined(SQLITE_OMIT_DISKIO)
drh6aafc292006-01-05 15:50:06 +00004037 int N;
4038 int amt;
4039 if( objc!=1 && objc!=2 ){
4040 Tcl_WrongNumArgs(interp, 1, objv, "?N?");
4041 return TCL_ERROR;
4042 }
4043 if( objc==2 ){
4044 if( Tcl_GetIntFromObj(interp, objv[1], &N) ) return TCL_ERROR;
4045 }else{
4046 N = -1;
4047 }
4048 amt = sqlite3_release_memory(N);
4049 Tcl_SetObjResult(interp, Tcl_NewIntObj(amt));
4050#endif
4051 return TCL_OK;
4052}
4053
4054/*
4055** Usage: sqlite3_soft_heap_limit ?N?
4056**
4057** Query or set the soft heap limit for the current thread. The
4058** limit is only changed if the N is present. The previous limit
4059** is returned.
4060*/
4061static int test_soft_heap_limit(
4062 void * clientData,
4063 Tcl_Interp *interp,
4064 int objc,
4065 Tcl_Obj *CONST objv[]
4066){
drh86f8c192007-08-22 00:39:19 +00004067 static int softHeapLimit = 0;
drh6aafc292006-01-05 15:50:06 +00004068 int amt;
4069 if( objc!=1 && objc!=2 ){
4070 Tcl_WrongNumArgs(interp, 1, objv, "?N?");
4071 return TCL_ERROR;
4072 }
drh86f8c192007-08-22 00:39:19 +00004073 amt = softHeapLimit;
drh6aafc292006-01-05 15:50:06 +00004074 if( objc==2 ){
4075 int N;
4076 if( Tcl_GetIntFromObj(interp, objv[1], &N) ) return TCL_ERROR;
4077 sqlite3_soft_heap_limit(N);
drh86f8c192007-08-22 00:39:19 +00004078 softHeapLimit = N;
drh6aafc292006-01-05 15:50:06 +00004079 }
4080 Tcl_SetObjResult(interp, Tcl_NewIntObj(amt));
drh6aafc292006-01-05 15:50:06 +00004081 return TCL_OK;
4082}
4083
4084/*
drhb4bc7052006-01-11 23:40:33 +00004085** Usage: sqlite3_clear_tsd_memdebug
4086**
4087** Clear all of the MEMDEBUG information out of thread-specific data.
4088** This will allow it to be deallocated.
4089*/
4090static int test_clear_tsd_memdebug(
4091 void * clientData,
4092 Tcl_Interp *interp,
4093 int objc,
4094 Tcl_Obj *CONST objv[]
4095){
drhb4bc7052006-01-11 23:40:33 +00004096 return TCL_OK;
4097}
4098
4099/*
4100** Usage: sqlite3_tsd_release
4101**
4102** Call sqlite3ReleaseThreadData.
4103*/
4104static int test_tsd_release(
4105 void * clientData,
4106 Tcl_Interp *interp,
4107 int objc,
4108 Tcl_Obj *CONST objv[]
4109){
drhb4bc7052006-01-11 23:40:33 +00004110 return TCL_OK;
4111}
4112
4113/*
4114** Usage: sqlite3_thread_cleanup
4115**
4116** Call the sqlite3_thread_cleanup API.
4117*/
4118static int test_thread_cleanup(
4119 void * clientData,
4120 Tcl_Interp *interp,
4121 int objc,
4122 Tcl_Obj *CONST objv[]
4123){
4124 sqlite3_thread_cleanup();
4125 return TCL_OK;
4126}
4127
4128
4129/*
drhc6ba55f2007-04-05 17:36:18 +00004130** Usage: sqlite3_pager_refcounts DB
4131**
drhf5345442007-04-09 12:45:02 +00004132** Return a list of numbers which are the PagerRefcount for all
4133** pagers on each database connection.
drhc6ba55f2007-04-05 17:36:18 +00004134*/
4135static int test_pager_refcounts(
4136 void * clientData,
4137 Tcl_Interp *interp,
4138 int objc,
4139 Tcl_Obj *CONST objv[]
4140){
4141 sqlite3 *db;
4142 int i;
4143 int v, *a;
4144 Tcl_Obj *pResult;
4145
4146 if( objc!=2 ){
4147 Tcl_AppendResult(interp, "wrong # args: should be \"",
drhf5345442007-04-09 12:45:02 +00004148 Tcl_GetStringFromObj(objv[0], 0), " DB", 0);
drhc6ba55f2007-04-05 17:36:18 +00004149 return TCL_ERROR;
4150 }
4151 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
4152 pResult = Tcl_NewObj();
4153 for(i=0; i<db->nDb; i++){
4154 if( db->aDb[i].pBt==0 ){
4155 v = -1;
4156 }else{
drh27641702007-08-22 02:56:42 +00004157 sqlite3_mutex_enter(db->mutex);
drhc6ba55f2007-04-05 17:36:18 +00004158 a = sqlite3PagerStats(sqlite3BtreePager(db->aDb[i].pBt));
4159 v = a[0];
drh27641702007-08-22 02:56:42 +00004160 sqlite3_mutex_leave(db->mutex);
drhc6ba55f2007-04-05 17:36:18 +00004161 }
4162 Tcl_ListObjAppendElement(0, pResult, Tcl_NewIntObj(v));
4163 }
4164 Tcl_SetObjResult(interp, pResult);
4165 return TCL_OK;
4166}
4167
4168
4169/*
drh80788d82006-09-02 14:50:23 +00004170** tclcmd: working_64bit_int
4171**
4172** Some TCL builds (ex: cygwin) do not support 64-bit integers. This
4173** leads to a number of test failures. The present command checks the
4174** TCL build to see whether or not it supports 64-bit integers. It
4175** returns TRUE if it does and FALSE if not.
4176**
4177** This command is used to warn users that their TCL build is defective
4178** and that the errors they are seeing in the test scripts might be
4179** a result of their defective TCL rather than problems in SQLite.
4180*/
4181static int working_64bit_int(
4182 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
4183 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
4184 int objc, /* Number of arguments */
4185 Tcl_Obj *CONST objv[] /* Command arguments */
4186){
4187 Tcl_Obj *pTestObj;
4188 int working = 0;
4189
4190 pTestObj = Tcl_NewWideIntObj(1000000*(i64)1234567890);
4191 working = strcmp(Tcl_GetString(pTestObj), "1234567890000000")==0;
4192 Tcl_DecrRefCount(pTestObj);
4193 Tcl_SetObjResult(interp, Tcl_NewBooleanObj(working));
4194 return TCL_OK;
4195}
4196
4197
4198/*
drhd1bf3512001-04-07 15:24:33 +00004199** Register commands with the TCL interpreter.
4200*/
4201int Sqlitetest1_Init(Tcl_Interp *interp){
danielk19776f8a5032004-05-10 10:34:51 +00004202 extern int sqlite3_search_count;
4203 extern int sqlite3_interrupt_count;
4204 extern int sqlite3_open_file_count;
drh6bf89572004-11-03 16:27:01 +00004205 extern int sqlite3_sort_count;
danielk19776f8a5032004-05-10 10:34:51 +00004206 extern int sqlite3_current_time;
drhae7e1512007-05-02 16:51:59 +00004207 extern int sqlite3_max_blobsize;
drh16a9b832007-05-05 18:39:25 +00004208 extern int sqlite3BtreeSharedCacheReport(void*,
4209 Tcl_Interp*,int,Tcl_Obj*CONST*);
drhc2eef3b2002-08-31 18:53:06 +00004210 static struct {
4211 char *zName;
4212 Tcl_CmdProc *xProc;
4213 } aCmd[] = {
drh27641702007-08-22 02:56:42 +00004214 { "db_enter", (Tcl_CmdProc*)db_enter },
4215 { "db_leave", (Tcl_CmdProc*)db_leave },
drhd3d39e92004-05-20 22:16:29 +00004216 { "sqlite3_mprintf_int", (Tcl_CmdProc*)sqlite3_mprintf_int },
drhe9707672004-06-25 01:10:48 +00004217 { "sqlite3_mprintf_int64", (Tcl_CmdProc*)sqlite3_mprintf_int64 },
drhd3d39e92004-05-20 22:16:29 +00004218 { "sqlite3_mprintf_str", (Tcl_CmdProc*)sqlite3_mprintf_str },
drhb3738b62007-03-31 15:02:49 +00004219 { "sqlite3_snprintf_str", (Tcl_CmdProc*)sqlite3_snprintf_str },
drhe29b1a02004-07-17 21:56:09 +00004220 { "sqlite3_mprintf_stronly", (Tcl_CmdProc*)sqlite3_mprintf_stronly},
drhd3d39e92004-05-20 22:16:29 +00004221 { "sqlite3_mprintf_double", (Tcl_CmdProc*)sqlite3_mprintf_double },
4222 { "sqlite3_mprintf_scaled", (Tcl_CmdProc*)sqlite3_mprintf_scaled },
drh63782852005-08-30 19:30:59 +00004223 { "sqlite3_mprintf_hexdouble", (Tcl_CmdProc*)sqlite3_mprintf_hexdouble},
drhd3d39e92004-05-20 22:16:29 +00004224 { "sqlite3_mprintf_z_test", (Tcl_CmdProc*)test_mprintf_z },
drh05a82982006-03-19 13:00:25 +00004225 { "sqlite3_mprintf_n_test", (Tcl_CmdProc*)test_mprintf_n },
drh68853902007-05-07 11:24:30 +00004226 { "sqlite3_snprintf_int", (Tcl_CmdProc*)test_snprintf_int },
drhd3d39e92004-05-20 22:16:29 +00004227 { "sqlite3_last_insert_rowid", (Tcl_CmdProc*)test_last_rowid },
4228 { "sqlite3_exec_printf", (Tcl_CmdProc*)test_exec_printf },
drhb62c3352006-11-23 09:39:16 +00004229 { "sqlite3_exec", (Tcl_CmdProc*)test_exec },
4230 { "sqlite3_exec_nr", (Tcl_CmdProc*)test_exec_nr },
drhd3d39e92004-05-20 22:16:29 +00004231 { "sqlite3_get_table_printf", (Tcl_CmdProc*)test_get_table_printf },
4232 { "sqlite3_close", (Tcl_CmdProc*)sqlite_test_close },
4233 { "sqlite3_create_function", (Tcl_CmdProc*)test_create_function },
4234 { "sqlite3_create_aggregate", (Tcl_CmdProc*)test_create_aggregate },
4235 { "sqlite_register_test_function", (Tcl_CmdProc*)test_register_func },
4236 { "sqlite_abort", (Tcl_CmdProc*)sqlite_abort },
drh25d65432004-07-22 15:02:25 +00004237 { "sqlite_bind", (Tcl_CmdProc*)test_bind },
4238 { "breakpoint", (Tcl_CmdProc*)test_breakpoint },
4239 { "sqlite3_key", (Tcl_CmdProc*)test_key },
4240 { "sqlite3_rekey", (Tcl_CmdProc*)test_rekey },
drhcacb2082005-01-11 15:28:33 +00004241 { "sqlite_set_magic", (Tcl_CmdProc*)sqlite_set_magic },
drhc5cdca62005-01-11 16:54:14 +00004242 { "sqlite3_interrupt", (Tcl_CmdProc*)test_interrupt },
drh3e1d8e62005-05-26 16:23:34 +00004243 { "sqlite_delete_function", (Tcl_CmdProc*)delete_function },
4244 { "sqlite_delete_collation", (Tcl_CmdProc*)delete_collation },
4245 { "sqlite3_get_autocommit", (Tcl_CmdProc*)get_autocommit },
drh79158e12005-09-06 21:40:45 +00004246 { "sqlite3_stack_used", (Tcl_CmdProc*)test_stack_used },
drh30867652006-07-06 10:59:57 +00004247 { "sqlite3_busy_timeout", (Tcl_CmdProc*)test_busy_timeout },
drh3c23a882007-01-09 14:01:13 +00004248 { "printf", (Tcl_CmdProc*)test_printf },
drh538f5702007-04-13 02:14:30 +00004249 { "sqlite3_io_trace", (Tcl_CmdProc*)test_io_trace },
drhc2eef3b2002-08-31 18:53:06 +00004250 };
danielk197751e3d8e2004-05-20 01:12:34 +00004251 static struct {
4252 char *zName;
4253 Tcl_ObjCmdProc *xProc;
danielk197704f2e682004-05-27 01:04:07 +00004254 void *clientData;
danielk197751e3d8e2004-05-20 01:12:34 +00004255 } aObjCmd[] = {
drhdddca282006-01-03 00:33:50 +00004256 { "sqlite3_connection_pointer", get_sqlite_pointer, 0 },
drh241db312004-06-22 12:46:53 +00004257 { "sqlite3_bind_int", test_bind_int, 0 },
drhb026e052007-05-02 01:34:31 +00004258 { "sqlite3_bind_zeroblob", test_bind_zeroblob, 0 },
drh241db312004-06-22 12:46:53 +00004259 { "sqlite3_bind_int64", test_bind_int64, 0 },
4260 { "sqlite3_bind_double", test_bind_double, 0 },
danielk197704f2e682004-05-27 01:04:07 +00004261 { "sqlite3_bind_null", test_bind_null ,0 },
4262 { "sqlite3_bind_text", test_bind_text ,0 },
4263 { "sqlite3_bind_text16", test_bind_text16 ,0 },
4264 { "sqlite3_bind_blob", test_bind_blob ,0 },
drh75f6a032004-07-15 14:15:00 +00004265 { "sqlite3_bind_parameter_count", test_bind_parameter_count, 0},
drh895d7472004-08-20 16:02:39 +00004266 { "sqlite3_bind_parameter_name", test_bind_parameter_name, 0},
drhfa6bc002004-09-07 16:19:52 +00004267 { "sqlite3_bind_parameter_index", test_bind_parameter_index, 0},
danielk1977600dd0b2005-01-20 01:14:23 +00004268 { "sqlite3_clear_bindings", test_clear_bindings, 0},
drhf9cb7f52006-06-27 20:06:44 +00004269 { "sqlite3_sleep", test_sleep, 0},
danielk197704f2e682004-05-27 01:04:07 +00004270 { "sqlite3_errcode", test_errcode ,0 },
4271 { "sqlite3_errmsg", test_errmsg ,0 },
4272 { "sqlite3_errmsg16", test_errmsg16 ,0 },
4273 { "sqlite3_open", test_open ,0 },
4274 { "sqlite3_open16", test_open16 ,0 },
danielk1977bc6ada42004-06-30 08:20:16 +00004275 { "sqlite3_complete16", test_complete16 ,0 },
danielk197704f2e682004-05-27 01:04:07 +00004276
4277 { "sqlite3_prepare", test_prepare ,0 },
4278 { "sqlite3_prepare16", test_prepare16 ,0 },
drhb900aaf2006-11-09 00:24:53 +00004279 { "sqlite3_prepare_v2", test_prepare_v2 ,0 },
4280 { "sqlite3_prepare16_v2", test_prepare16_v2 ,0 },
danielk197704f2e682004-05-27 01:04:07 +00004281 { "sqlite3_finalize", test_finalize ,0 },
4282 { "sqlite3_reset", test_reset ,0 },
drhd89bd002005-01-22 03:03:54 +00004283 { "sqlite3_expired", test_expired ,0 },
drhf8db1bc2005-04-22 02:38:37 +00004284 { "sqlite3_transfer_bindings", test_transfer_bind ,0 },
danielk1977fbcd5852004-06-15 02:44:18 +00004285 { "sqlite3_changes", test_changes ,0 },
4286 { "sqlite3_step", test_step ,0 },
danielk197704f2e682004-05-27 01:04:07 +00004287
drhb4bc7052006-01-11 23:40:33 +00004288 { "sqlite3_release_memory", test_release_memory, 0},
4289 { "sqlite3_soft_heap_limit", test_soft_heap_limit, 0},
4290 { "sqlite3_clear_tsd_memdebug", test_clear_tsd_memdebug, 0},
4291 { "sqlite3_tsd_release", test_tsd_release, 0},
4292 { "sqlite3_thread_cleanup", test_thread_cleanup, 0},
drhc6ba55f2007-04-05 17:36:18 +00004293 { "sqlite3_pager_refcounts", test_pager_refcounts, 0},
drh6aafc292006-01-05 15:50:06 +00004294
drhc2e87a32006-06-27 15:16:14 +00004295 { "sqlite3_load_extension", test_load_extension, 0},
4296 { "sqlite3_enable_load_extension", test_enable_load, 0},
drh4ac285a2006-09-15 07:28:50 +00004297 { "sqlite3_extended_result_codes", test_extended_result_codes, 0},
drhc2e87a32006-06-27 15:16:14 +00004298
danielk197704f2e682004-05-27 01:04:07 +00004299 /* sqlite3_column_*() API */
4300 { "sqlite3_column_count", test_column_count ,0 },
4301 { "sqlite3_data_count", test_data_count ,0 },
4302 { "sqlite3_column_type", test_column_type ,0 },
danielk1977ea61b2c2004-05-27 01:49:51 +00004303 { "sqlite3_column_blob", test_column_blob ,0 },
danielk197704f2e682004-05-27 01:04:07 +00004304 { "sqlite3_column_double", test_column_double ,0 },
4305 { "sqlite3_column_int64", test_column_int64 ,0 },
drh241db312004-06-22 12:46:53 +00004306 { "sqlite3_column_text", test_stmt_utf8, sqlite3_column_text },
4307 { "sqlite3_column_decltype", test_stmt_utf8, sqlite3_column_decltype },
4308 { "sqlite3_column_name", test_stmt_utf8, sqlite3_column_name },
drh6c626082004-11-14 21:56:29 +00004309 { "sqlite3_column_int", test_stmt_int, sqlite3_column_int },
4310 { "sqlite3_column_bytes", test_stmt_int, sqlite3_column_bytes },
danielk19774b1ae992006-02-10 03:06:10 +00004311#ifdef SQLITE_ENABLE_COLUMN_METADATA
danielk1977955de522006-02-10 02:27:42 +00004312{ "sqlite3_column_database_name", test_stmt_utf8, sqlite3_column_database_name},
4313{ "sqlite3_column_table_name", test_stmt_utf8, sqlite3_column_table_name},
4314{ "sqlite3_column_origin_name", test_stmt_utf8, sqlite3_column_origin_name},
danielk19774b1ae992006-02-10 03:06:10 +00004315#endif
danielk1977955de522006-02-10 02:27:42 +00004316
drh6c626082004-11-14 21:56:29 +00004317#ifndef SQLITE_OMIT_UTF16
4318 { "sqlite3_column_bytes16", test_stmt_int, sqlite3_column_bytes16 },
drh241db312004-06-22 12:46:53 +00004319 { "sqlite3_column_text16", test_stmt_utf16, sqlite3_column_text16 },
4320 { "sqlite3_column_decltype16", test_stmt_utf16, sqlite3_column_decltype16},
4321 { "sqlite3_column_name16", test_stmt_utf16, sqlite3_column_name16 },
drh7d9bd4e2006-02-16 18:16:36 +00004322 { "add_alignment_test_collations", add_alignment_test_collations, 0 },
danielk19774b1ae992006-02-10 03:06:10 +00004323#ifdef SQLITE_ENABLE_COLUMN_METADATA
danielk1977955de522006-02-10 02:27:42 +00004324{"sqlite3_column_database_name16",
4325 test_stmt_utf16, sqlite3_column_database_name16},
4326{"sqlite3_column_table_name16", test_stmt_utf16, sqlite3_column_table_name16},
4327{"sqlite3_column_origin_name16", test_stmt_utf16, sqlite3_column_origin_name16},
drh6c626082004-11-14 21:56:29 +00004328#endif
danielk19774b1ae992006-02-10 03:06:10 +00004329#endif
danielk1977a393c032007-05-07 14:58:53 +00004330 { "sqlite3_create_collation_v2", test_create_collation_v2, 0 },
danielk1977a9808b32007-05-07 09:32:45 +00004331 { "sqlite3_global_recover", test_global_recover, 0 },
4332 { "working_64bit_int", working_64bit_int, 0 },
danielk197704f2e682004-05-27 01:04:07 +00004333
danielk19779a1d0ab2004-06-01 14:09:28 +00004334 /* Functions from os.h */
danielk197744ee5bf2005-05-27 09:41:12 +00004335#ifndef SQLITE_OMIT_DISKIO
danielk1977b4b47412007-08-17 15:53:36 +00004336#if 0
danielk19779a1d0ab2004-06-01 14:09:28 +00004337 { "sqlite3OsOpenReadWrite",test_sqlite3OsOpenReadWrite, 0 },
4338 { "sqlite3OsClose", test_sqlite3OsClose, 0 },
4339 { "sqlite3OsLock", test_sqlite3OsLock, 0 },
drhab3f9fe2004-08-14 17:10:10 +00004340 { "sqlite3OsTempFileName", test_sqlite3OsTempFileName, 0 },
danielk1977312d6b32004-06-29 13:18:23 +00004341
4342 /* Custom test interfaces */
4343 { "sqlite3OsUnlock", test_sqlite3OsUnlock, 0 },
danielk197744ee5bf2005-05-27 09:41:12 +00004344#endif
danielk1977b4b47412007-08-17 15:53:36 +00004345#endif
drh5436dc22004-11-14 04:04:17 +00004346#ifndef SQLITE_OMIT_UTF16
danielk1977312d6b32004-06-29 13:18:23 +00004347 { "add_test_collate", test_collate, 0 },
4348 { "add_test_collate_needed", test_collate_needed, 0 },
4349 { "add_test_function", test_function, 0 },
drh5436dc22004-11-14 04:04:17 +00004350#endif
danielk1977312d6b32004-06-29 13:18:23 +00004351 { "sqlite3_test_errstr", test_errstr, 0 },
drh92febd92004-08-20 18:34:20 +00004352 { "tcl_variable_type", tcl_variable_type, 0 },
danielk1977aef0bf62005-12-30 16:28:01 +00004353#ifndef SQLITE_OMIT_SHARED_CACHE
drh6f7adc82006-01-11 21:41:20 +00004354 { "sqlite3_enable_shared_cache", test_enable_shared, 0 },
drh16a9b832007-05-05 18:39:25 +00004355 { "sqlite3_shared_cache_report", sqlite3BtreeSharedCacheReport, 0},
danielk1977aef0bf62005-12-30 16:28:01 +00004356#endif
danielk1977161fb792006-01-24 10:58:21 +00004357 { "sqlite3_libversion_number", test_libversion_number, 0 },
danielk1977deb802c2006-02-09 13:43:28 +00004358#ifdef SQLITE_ENABLE_COLUMN_METADATA
4359 { "sqlite3_table_column_metadata", test_table_column_metadata, 0 },
4360#endif
danielk1977dcbb5d32007-05-04 18:36:44 +00004361#ifndef SQLITE_OMIT_INCRBLOB
4362 { "sqlite3_blob_read", test_blob_read, 0 },
4363 { "sqlite3_blob_write", test_blob_write, 0 },
4364#endif
danielk197751e3d8e2004-05-20 01:12:34 +00004365 };
drh1398ad32005-01-19 23:24:50 +00004366 static int bitmask_size = sizeof(Bitmask)*8;
drhc2eef3b2002-08-31 18:53:06 +00004367 int i;
drh51147ba2005-07-23 22:59:55 +00004368 extern int sqlite3_where_trace;
drhb851b2c2005-03-10 14:11:12 +00004369 extern int sqlite3_sync_count, sqlite3_fullsync_count;
drhaf6df112005-06-07 02:12:30 +00004370 extern int sqlite3_opentemp_count;
drh55ef4d92005-08-14 01:20:37 +00004371 extern int sqlite3_like_count;
drhdd735212007-02-24 13:53:05 +00004372 extern int sqlite3_xferopt_count;
drh538f5702007-04-13 02:14:30 +00004373 extern int sqlite3_pager_readdb_count;
4374 extern int sqlite3_pager_writedb_count;
4375 extern int sqlite3_pager_writej_count;
4376 extern int sqlite3_pager_pgfree_count;
drhd677b3d2007-08-20 22:48:41 +00004377#if OS_UNIX && defined(SQLITE_TEST) && SQLITE_THREADSAFE
drh029b44b2006-01-15 00:13:15 +00004378 extern int threadsOverrideEachOthersLocks;
4379#endif
drhc0929982005-09-05 19:08:29 +00004380#if OS_WIN
4381 extern int sqlite3_os_type;
4382#endif
drh8b3d9902005-08-19 00:14:42 +00004383#ifdef SQLITE_DEBUG
drh73be5012007-08-08 12:11:21 +00004384 extern int sqlite3_os_trace;
drh8b3d9902005-08-19 00:14:42 +00004385 extern int sqlite3_vdbe_addop_trace;
drh549c8b62005-09-19 13:15:23 +00004386#endif
4387#ifdef SQLITE_TEST
4388 extern char sqlite3_query_plan[];
drh9042f392005-07-15 23:24:23 +00004389 static char *query_plan = sqlite3_query_plan;
drh48083ce2005-09-19 12:37:27 +00004390#endif
drhc2eef3b2002-08-31 18:53:06 +00004391
4392 for(i=0; i<sizeof(aCmd)/sizeof(aCmd[0]); i++){
4393 Tcl_CreateCommand(interp, aCmd[i].zName, aCmd[i].xProc, 0, 0);
4394 }
danielk197751e3d8e2004-05-20 01:12:34 +00004395 for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){
danielk1977c572ef72004-05-27 09:28:41 +00004396 Tcl_CreateObjCommand(interp, aObjCmd[i].zName,
4397 aObjCmd[i].xProc, aObjCmd[i].clientData, 0);
danielk197751e3d8e2004-05-20 01:12:34 +00004398 }
danielk19776490beb2004-05-11 06:17:21 +00004399 Tcl_LinkVar(interp, "sqlite_search_count",
danielk19776f8a5032004-05-10 10:34:51 +00004400 (char*)&sqlite3_search_count, TCL_LINK_INT);
drh6bf89572004-11-03 16:27:01 +00004401 Tcl_LinkVar(interp, "sqlite_sort_count",
4402 (char*)&sqlite3_sort_count, TCL_LINK_INT);
drhae7e1512007-05-02 16:51:59 +00004403 Tcl_LinkVar(interp, "sqlite3_max_blobsize",
4404 (char*)&sqlite3_max_blobsize, TCL_LINK_INT);
drh55ef4d92005-08-14 01:20:37 +00004405 Tcl_LinkVar(interp, "sqlite_like_count",
4406 (char*)&sqlite3_like_count, TCL_LINK_INT);
danielk19776490beb2004-05-11 06:17:21 +00004407 Tcl_LinkVar(interp, "sqlite_interrupt_count",
danielk19776f8a5032004-05-10 10:34:51 +00004408 (char*)&sqlite3_interrupt_count, TCL_LINK_INT);
danielk19776490beb2004-05-11 06:17:21 +00004409 Tcl_LinkVar(interp, "sqlite_open_file_count",
danielk19776f8a5032004-05-10 10:34:51 +00004410 (char*)&sqlite3_open_file_count, TCL_LINK_INT);
danielk19776490beb2004-05-11 06:17:21 +00004411 Tcl_LinkVar(interp, "sqlite_current_time",
danielk19776f8a5032004-05-10 10:34:51 +00004412 (char*)&sqlite3_current_time, TCL_LINK_INT);
drhdd735212007-02-24 13:53:05 +00004413 Tcl_LinkVar(interp, "sqlite3_xferopt_count",
4414 (char*)&sqlite3_xferopt_count, TCL_LINK_INT);
drh538f5702007-04-13 02:14:30 +00004415 Tcl_LinkVar(interp, "sqlite3_pager_readdb_count",
4416 (char*)&sqlite3_pager_readdb_count, TCL_LINK_INT);
4417 Tcl_LinkVar(interp, "sqlite3_pager_writedb_count",
4418 (char*)&sqlite3_pager_writedb_count, TCL_LINK_INT);
4419 Tcl_LinkVar(interp, "sqlite3_pager_writej_count",
4420 (char*)&sqlite3_pager_writej_count, TCL_LINK_INT);
4421 Tcl_LinkVar(interp, "sqlite3_pager_pgfree_count",
4422 (char*)&sqlite3_pager_pgfree_count, TCL_LINK_INT);
danielk19774b2688a2006-06-20 11:01:07 +00004423#ifndef SQLITE_OMIT_UTF16
drh7d9bd4e2006-02-16 18:16:36 +00004424 Tcl_LinkVar(interp, "unaligned_string_counter",
4425 (char*)&unaligned_string_counter, TCL_LINK_INT);
danielk19774b2688a2006-06-20 11:01:07 +00004426#endif
drhd677b3d2007-08-20 22:48:41 +00004427#if OS_UNIX && defined(SQLITE_TEST) && SQLITE_THREADSAFE
drh029b44b2006-01-15 00:13:15 +00004428 Tcl_LinkVar(interp, "threadsOverrideEachOthersLocks",
4429 (char*)&threadsOverrideEachOthersLocks, TCL_LINK_INT);
4430#endif
drh268803a2005-12-14 20:11:30 +00004431#ifndef SQLITE_OMIT_UTF16
4432 Tcl_LinkVar(interp, "sqlite_last_needed_collation",
4433 (char*)&pzNeededCollation, TCL_LINK_STRING|TCL_LINK_READ_ONLY);
4434#endif
drhc0929982005-09-05 19:08:29 +00004435#if OS_WIN
4436 Tcl_LinkVar(interp, "sqlite_os_type",
4437 (char*)&sqlite3_os_type, TCL_LINK_INT);
4438#endif
drh549c8b62005-09-19 13:15:23 +00004439#ifdef SQLITE_TEST
4440 Tcl_LinkVar(interp, "sqlite_query_plan",
4441 (char*)&query_plan, TCL_LINK_STRING|TCL_LINK_READ_ONLY);
4442#endif
drh8b3d9902005-08-19 00:14:42 +00004443#ifdef SQLITE_DEBUG
4444 Tcl_LinkVar(interp, "sqlite_addop_trace",
4445 (char*)&sqlite3_vdbe_addop_trace, TCL_LINK_INT);
drh48083ce2005-09-19 12:37:27 +00004446 Tcl_LinkVar(interp, "sqlite_where_trace",
4447 (char*)&sqlite3_where_trace, TCL_LINK_INT);
drh73be5012007-08-08 12:11:21 +00004448 Tcl_LinkVar(interp, "sqlite_os_trace",
4449 (char*)&sqlite3_os_trace, TCL_LINK_INT);
drh8b3d9902005-08-19 00:14:42 +00004450#endif
danielk1977cbe21be2005-06-07 07:58:48 +00004451#ifndef SQLITE_OMIT_DISKIO
drhaf6df112005-06-07 02:12:30 +00004452 Tcl_LinkVar(interp, "sqlite_opentemp_count",
4453 (char*)&sqlite3_opentemp_count, TCL_LINK_INT);
danielk1977cbe21be2005-06-07 07:58:48 +00004454#endif
drh7c972de2003-09-06 22:18:07 +00004455 Tcl_LinkVar(interp, "sqlite_static_bind_value",
4456 (char*)&sqlite_static_bind_value, TCL_LINK_STRING);
drhf0313812006-09-04 15:53:53 +00004457 Tcl_LinkVar(interp, "sqlite_static_bind_nbyte",
4458 (char*)&sqlite_static_bind_nbyte, TCL_LINK_INT);
drhab3f9fe2004-08-14 17:10:10 +00004459 Tcl_LinkVar(interp, "sqlite_temp_directory",
drheffd02b2004-08-29 23:42:13 +00004460 (char*)&sqlite3_temp_directory, TCL_LINK_STRING);
drh1398ad32005-01-19 23:24:50 +00004461 Tcl_LinkVar(interp, "bitmask_size",
4462 (char*)&bitmask_size, TCL_LINK_INT|TCL_LINK_READ_ONLY);
drh748f7632005-03-11 04:41:39 +00004463#if OS_UNIX
drhb851b2c2005-03-10 14:11:12 +00004464 Tcl_LinkVar(interp, "sqlite_sync_count",
4465 (char*)&sqlite3_sync_count, TCL_LINK_INT);
4466 Tcl_LinkVar(interp, "sqlite_fullsync_count",
4467 (char*)&sqlite3_fullsync_count, TCL_LINK_INT);
drh748f7632005-03-11 04:41:39 +00004468#endif /* OS_UNIX */
danielk1977b82e7ed2006-01-11 14:09:31 +00004469
drhd1bf3512001-04-07 15:24:33 +00004470 return TCL_OK;
4471}