blob: 6f54ffc2f2eee432b7e9c31d136f2aa50d5a6a23 [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**
drh86f8c192007-08-22 00:39:19 +000016** $Id: test1.c,v 1.268 2007/08/22 00:39:21 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/*
drhb62c3352006-11-23 09:39:16 +0000326** Usage: sqlite3_exec DB SQL
327**
328** Invoke the sqlite3_exec interface using the open database DB
329*/
330static int test_exec(
331 void *NotUsed,
332 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
333 int argc, /* Number of arguments */
334 char **argv /* Text of each argument */
335){
336 sqlite3 *db;
337 Tcl_DString str;
338 int rc;
339 char *zErr = 0;
drh4e5dd852007-05-15 03:56:49 +0000340 char *zSql;
341 int i, j;
drhb62c3352006-11-23 09:39:16 +0000342 char zBuf[30];
343 if( argc!=3 ){
344 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
345 " DB SQL", 0);
346 return TCL_ERROR;
347 }
348 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
349 Tcl_DStringInit(&str);
drh4e5dd852007-05-15 03:56:49 +0000350 zSql = sqlite3_mprintf("%s", argv[2]);
351 for(i=j=0; zSql[i];){
352 if( zSql[i]=='%' ){
353 zSql[j++] = (testHexToInt(zSql[i+1])<<4) + testHexToInt(zSql[i+2]);
354 i += 3;
355 }else{
356 zSql[j++] = zSql[i++];
357 }
358 }
359 zSql[j] = 0;
360 rc = sqlite3_exec(db, zSql, exec_printf_cb, &str, &zErr);
361 sqlite3_free(zSql);
drhb62c3352006-11-23 09:39:16 +0000362 sprintf(zBuf, "%d", rc);
363 Tcl_AppendElement(interp, zBuf);
364 Tcl_AppendElement(interp, rc==SQLITE_OK ? Tcl_DStringValue(&str) : zErr);
365 Tcl_DStringFree(&str);
366 if( zErr ) sqlite3_free(zErr);
367 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
368 return TCL_OK;
369}
370
371/*
372** Usage: sqlite3_exec_nr DB SQL
373**
374** Invoke the sqlite3_exec interface using the open database DB. Discard
375** all results
376*/
377static int test_exec_nr(
378 void *NotUsed,
379 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
380 int argc, /* Number of arguments */
381 char **argv /* Text of each argument */
382){
383 sqlite3 *db;
384 int rc;
385 char *zErr = 0;
386 if( argc!=3 ){
387 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
388 " DB SQL", 0);
389 return TCL_ERROR;
390 }
391 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
392 rc = sqlite3_exec(db, argv[2], 0, 0, &zErr);
393 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
394 return TCL_OK;
395}
396
397/*
danielk19776f8a5032004-05-10 10:34:51 +0000398** Usage: sqlite3_mprintf_z_test SEPARATOR ARG0 ARG1 ...
drhd93d8a82003-06-16 03:08:18 +0000399**
drh05a82982006-03-19 13:00:25 +0000400** Test the %z format of sqliteMPrintf(). Use multiple mprintf() calls to
drhd93d8a82003-06-16 03:08:18 +0000401** concatenate arg0 through argn using separator as the separator.
402** Return the result.
403*/
404static int test_mprintf_z(
405 void *NotUsed,
406 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
407 int argc, /* Number of arguments */
408 char **argv /* Text of each argument */
409){
410 char *zResult = 0;
411 int i;
412
413 for(i=2; i<argc; i++){
danielk19771e536952007-08-16 10:09:01 +0000414 zResult = sqlite3MPrintf(0, "%z%s%s", zResult, argv[1], argv[i]);
drhd93d8a82003-06-16 03:08:18 +0000415 }
416 Tcl_AppendResult(interp, zResult, 0);
drh17435752007-08-16 04:30:38 +0000417 sqlite3_free(zResult);
drhd93d8a82003-06-16 03:08:18 +0000418 return TCL_OK;
419}
420
421/*
drh05a82982006-03-19 13:00:25 +0000422** Usage: sqlite3_mprintf_n_test STRING
423**
424** Test the %n format of sqliteMPrintf(). Return the length of the
425** input string.
426*/
427static int test_mprintf_n(
428 void *NotUsed,
429 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
430 int argc, /* Number of arguments */
431 char **argv /* Text of each argument */
432){
433 char *zStr;
434 int n = 0;
danielk19771e536952007-08-16 10:09:01 +0000435 zStr = sqlite3MPrintf(0, "%s%n", argv[1], &n);
drh17435752007-08-16 04:30:38 +0000436 sqlite3_free(zStr);
drh05a82982006-03-19 13:00:25 +0000437 Tcl_SetObjResult(interp, Tcl_NewIntObj(n));
438 return TCL_OK;
439}
440
441/*
drh68853902007-05-07 11:24:30 +0000442** Usage: sqlite3_snprintf_int SIZE FORMAT INT
443**
444** Test the of sqlite3_snprintf() routine. SIZE is the size of the
445** output buffer in bytes. The maximum size is 100. FORMAT is the
446** format string. INT is a single integer argument. The FORMAT
447** string must require no more than this one integer argument. If
448** You pass in a format string that requires more than one argument,
449** bad things will happen.
450*/
451static int test_snprintf_int(
452 void *NotUsed,
453 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
454 int argc, /* Number of arguments */
455 char **argv /* Text of each argument */
456){
457 char zStr[100];
458 int n = atoi(argv[1]);
drh68853902007-05-07 11:24:30 +0000459 const char *zFormat = argv[2];
460 int a1 = atoi(argv[3]);
drhdaf276d2007-06-15 18:53:14 +0000461 if( n>sizeof(zStr) ) n = sizeof(zStr);
drh68853902007-05-07 11:24:30 +0000462 strcpy(zStr, "abcdefghijklmnopqrstuvwxyz");
463 sqlite3_snprintf(n, zStr, zFormat, a1);
464 Tcl_AppendResult(interp, zStr, 0);
465 return TCL_OK;
466}
467
468/*
danielk19776f8a5032004-05-10 10:34:51 +0000469** Usage: sqlite3_get_table_printf DB FORMAT STRING
drhd1bf3512001-04-07 15:24:33 +0000470**
danielk19776f8a5032004-05-10 10:34:51 +0000471** Invoke the sqlite3_get_table_printf() interface using the open database
drhd1bf3512001-04-07 15:24:33 +0000472** DB. The SQL is the string FORMAT. The format string should contain
473** one %s or %q. STRING is the value inserted into %s or %q.
474*/
475static int test_get_table_printf(
476 void *NotUsed,
477 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
478 int argc, /* Number of arguments */
479 char **argv /* Text of each argument */
480){
drh9bb575f2004-09-06 17:24:11 +0000481 sqlite3 *db;
drhd1bf3512001-04-07 15:24:33 +0000482 Tcl_DString str;
483 int rc;
484 char *zErr = 0;
485 int nRow, nCol;
486 char **aResult;
487 int i;
488 char zBuf[30];
drh1211de32004-07-26 12:24:22 +0000489 char *zSql;
drhd1bf3512001-04-07 15:24:33 +0000490 if( argc!=4 ){
491 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
492 " DB FORMAT STRING", 0);
493 return TCL_ERROR;
494 }
drhb86ccfb2003-01-28 23:13:10 +0000495 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
drhd1bf3512001-04-07 15:24:33 +0000496 Tcl_DStringInit(&str);
drh1211de32004-07-26 12:24:22 +0000497 zSql = sqlite3_mprintf(argv[2],argv[3]);
498 rc = sqlite3_get_table(db, zSql, &aResult, &nRow, &nCol, &zErr);
499 sqlite3_free(zSql);
drhd1bf3512001-04-07 15:24:33 +0000500 sprintf(zBuf, "%d", rc);
501 Tcl_AppendElement(interp, zBuf);
502 if( rc==SQLITE_OK ){
503 sprintf(zBuf, "%d", nRow);
504 Tcl_AppendElement(interp, zBuf);
505 sprintf(zBuf, "%d", nCol);
506 Tcl_AppendElement(interp, zBuf);
507 for(i=0; i<(nRow+1)*nCol; i++){
508 Tcl_AppendElement(interp, aResult[i] ? aResult[i] : "NULL");
509 }
510 }else{
511 Tcl_AppendElement(interp, zErr);
512 }
danielk19776f8a5032004-05-10 10:34:51 +0000513 sqlite3_free_table(aResult);
danielk1977926aab22006-06-27 07:34:40 +0000514 if( zErr ) sqlite3_free(zErr);
drhc60d0442004-09-30 13:43:13 +0000515 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
drhd1bf3512001-04-07 15:24:33 +0000516 return TCL_OK;
517}
518
drhaf9ff332002-01-16 21:00:27 +0000519
520/*
danielk19776f8a5032004-05-10 10:34:51 +0000521** Usage: sqlite3_last_insert_rowid DB
drhaf9ff332002-01-16 21:00:27 +0000522**
523** Returns the integer ROWID of the most recent insert.
524*/
525static int test_last_rowid(
526 void *NotUsed,
527 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
528 int argc, /* Number of arguments */
529 char **argv /* Text of each argument */
530){
drh9bb575f2004-09-06 17:24:11 +0000531 sqlite3 *db;
drhaf9ff332002-01-16 21:00:27 +0000532 char zBuf[30];
533
534 if( argc!=2 ){
535 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " DB\"", 0);
536 return TCL_ERROR;
537 }
drhb86ccfb2003-01-28 23:13:10 +0000538 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
danielk1977c572ef72004-05-27 09:28:41 +0000539 sprintf(zBuf, "%lld", sqlite3_last_insert_rowid(db));
drhaf9ff332002-01-16 21:00:27 +0000540 Tcl_AppendResult(interp, zBuf, 0);
541 return SQLITE_OK;
542}
543
drhd1bf3512001-04-07 15:24:33 +0000544/*
drh25d65432004-07-22 15:02:25 +0000545** Usage: sqlite3_key DB KEY
546**
547** Set the codec key.
548*/
549static int test_key(
550 void *NotUsed,
551 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
552 int argc, /* Number of arguments */
553 char **argv /* Text of each argument */
554){
drh9bb575f2004-09-06 17:24:11 +0000555 sqlite3 *db;
drh25d65432004-07-22 15:02:25 +0000556 const char *zKey;
557 int nKey;
558 if( argc!=3 ){
559 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
560 " FILENAME\"", 0);
561 return TCL_ERROR;
562 }
563 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
564 zKey = argv[2];
565 nKey = strlen(zKey);
566#ifdef SQLITE_HAS_CODEC
567 sqlite3_key(db, zKey, nKey);
568#endif
569 return TCL_OK;
570}
571
572/*
573** Usage: sqlite3_rekey DB KEY
574**
575** Change the codec key.
576*/
577static int test_rekey(
578 void *NotUsed,
579 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
580 int argc, /* Number of arguments */
581 char **argv /* Text of each argument */
582){
drh9bb575f2004-09-06 17:24:11 +0000583 sqlite3 *db;
drh25d65432004-07-22 15:02:25 +0000584 const char *zKey;
585 int nKey;
586 if( argc!=3 ){
587 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
588 " FILENAME\"", 0);
589 return TCL_ERROR;
590 }
591 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
592 zKey = argv[2];
593 nKey = strlen(zKey);
594#ifdef SQLITE_HAS_CODEC
595 sqlite3_rekey(db, zKey, nKey);
596#endif
597 return TCL_OK;
598}
599
600/*
danielk19776f8a5032004-05-10 10:34:51 +0000601** Usage: sqlite3_close DB
drhd1bf3512001-04-07 15:24:33 +0000602**
danielk19776f8a5032004-05-10 10:34:51 +0000603** Closes the database opened by sqlite3_open.
drhd1bf3512001-04-07 15:24:33 +0000604*/
605static int sqlite_test_close(
606 void *NotUsed,
607 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
608 int argc, /* Number of arguments */
609 char **argv /* Text of each argument */
610){
drh9bb575f2004-09-06 17:24:11 +0000611 sqlite3 *db;
danielk197796d81f92004-06-19 03:33:57 +0000612 int rc;
drhd1bf3512001-04-07 15:24:33 +0000613 if( argc!=2 ){
614 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
615 " FILENAME\"", 0);
616 return TCL_ERROR;
617 }
drhb86ccfb2003-01-28 23:13:10 +0000618 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
danielk197796d81f92004-06-19 03:33:57 +0000619 rc = sqlite3_close(db);
drh4f0c5872007-03-26 22:05:01 +0000620 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
drhd1bf3512001-04-07 15:24:33 +0000621 return TCL_OK;
622}
623
624/*
drhc22bd472002-05-10 13:14:07 +0000625** Implementation of the x_coalesce() function.
626** Return the first argument non-NULL argument.
627*/
drh4f0c5872007-03-26 22:05:01 +0000628static void t1_ifnullFunc(
629 sqlite3_context *context,
630 int argc,
631 sqlite3_value **argv
632){
drhc22bd472002-05-10 13:14:07 +0000633 int i;
634 for(i=0; i<argc; i++){
drh9c054832004-05-31 18:51:57 +0000635 if( SQLITE_NULL!=sqlite3_value_type(argv[i]) ){
drh9310ef22007-04-27 17:16:20 +0000636 int n = sqlite3_value_bytes(argv[i]);
drh03d847e2005-12-09 20:21:58 +0000637 sqlite3_result_text(context, (char*)sqlite3_value_text(argv[i]),
drh9310ef22007-04-27 17:16:20 +0000638 n, SQLITE_TRANSIENT);
drhc22bd472002-05-10 13:14:07 +0000639 break;
640 }
641 }
642}
643
644/*
drhf0313812006-09-04 15:53:53 +0000645** These are test functions. hex8() interprets its argument as
646** UTF8 and returns a hex encoding. hex16le() interprets its argument
647** as UTF16le and returns a hex encoding.
648*/
649static void hex8Func(sqlite3_context *p, int argc, sqlite3_value **argv){
650 const unsigned char *z;
651 int i;
652 char zBuf[200];
653 z = sqlite3_value_text(argv[0]);
654 for(i=0; i<sizeof(zBuf)/2 - 2 && z[i]; i++){
655 sprintf(&zBuf[i*2], "%02x", z[i]&0xff);
656 }
657 zBuf[i*2] = 0;
658 sqlite3_result_text(p, (char*)zBuf, -1, SQLITE_TRANSIENT);
659}
drhaf304692007-04-23 23:56:31 +0000660#ifndef SQLITE_OMIT_UTF16
drhf0313812006-09-04 15:53:53 +0000661static void hex16Func(sqlite3_context *p, int argc, sqlite3_value **argv){
662 const unsigned short int *z;
663 int i;
664 char zBuf[400];
665 z = sqlite3_value_text16(argv[0]);
666 for(i=0; i<sizeof(zBuf)/4 - 4 && z[i]; i++){
667 sprintf(&zBuf[i*4], "%04x", z[i]&0xff);
668 }
669 zBuf[i*4] = 0;
670 sqlite3_result_text(p, (char*)zBuf, -1, SQLITE_TRANSIENT);
671}
drhaf304692007-04-23 23:56:31 +0000672#endif
drhf0313812006-09-04 15:53:53 +0000673
674/*
drhd1d9fc32004-01-07 19:24:48 +0000675** A structure into which to accumulate text.
676*/
677struct dstr {
678 int nAlloc; /* Space allocated */
679 int nUsed; /* Space used */
680 char *z; /* The space */
681};
682
683/*
684** Append text to a dstr
685*/
686static void dstrAppend(struct dstr *p, const char *z, int divider){
687 int n = strlen(z);
688 if( p->nUsed + n + 2 > p->nAlloc ){
689 char *zNew;
690 p->nAlloc = p->nAlloc*2 + n + 200;
drh17435752007-08-16 04:30:38 +0000691 zNew = sqlite3_realloc(p->z, p->nAlloc);
drhd1d9fc32004-01-07 19:24:48 +0000692 if( zNew==0 ){
drh17435752007-08-16 04:30:38 +0000693 sqlite3_free(p->z);
drhd1d9fc32004-01-07 19:24:48 +0000694 memset(p, 0, sizeof(*p));
695 return;
696 }
697 p->z = zNew;
698 }
699 if( divider && p->nUsed>0 ){
700 p->z[p->nUsed++] = divider;
701 }
702 memcpy(&p->z[p->nUsed], z, n+1);
703 p->nUsed += n;
704}
705
706/*
danielk19774adee202004-05-08 08:23:19 +0000707** Invoked for each callback from sqlite3ExecFunc
drhd1d9fc32004-01-07 19:24:48 +0000708*/
709static int execFuncCallback(void *pData, int argc, char **argv, char **NotUsed){
710 struct dstr *p = (struct dstr*)pData;
711 int i;
712 for(i=0; i<argc; i++){
713 if( argv[i]==0 ){
714 dstrAppend(p, "NULL", ' ');
715 }else{
716 dstrAppend(p, argv[i], ' ');
717 }
718 }
719 return 0;
720}
721
722/*
danielk1977e35ee192004-06-26 09:50:11 +0000723** Implementation of the x_sqlite_exec() function. This function takes
drhc22bd472002-05-10 13:14:07 +0000724** a single argument and attempts to execute that argument as SQL code.
drh6cbe1f12002-07-01 00:31:36 +0000725** This is illegal and should set the SQLITE_MISUSE flag on the database.
drhd1d9fc32004-01-07 19:24:48 +0000726**
danielk19776f8a5032004-05-10 10:34:51 +0000727** 2004-Jan-07: We have changed this to make it legal to call sqlite3_exec()
drhd1d9fc32004-01-07 19:24:48 +0000728** from within a function call.
drhc22bd472002-05-10 13:14:07 +0000729**
730** This routine simulates the effect of having two threads attempt to
731** use the same database at the same time.
732*/
danielk197751ad0ec2004-05-24 12:39:02 +0000733static void sqlite3ExecFunc(
danielk19770ae8b832004-05-25 12:05:56 +0000734 sqlite3_context *context,
danielk197751ad0ec2004-05-24 12:39:02 +0000735 int argc,
736 sqlite3_value **argv
737){
drhd1d9fc32004-01-07 19:24:48 +0000738 struct dstr x;
739 memset(&x, 0, sizeof(x));
drh37527852006-03-16 16:19:56 +0000740 (void)sqlite3_exec((sqlite3*)sqlite3_user_data(context),
drh03d847e2005-12-09 20:21:58 +0000741 (char*)sqlite3_value_text(argv[0]),
drhd1d9fc32004-01-07 19:24:48 +0000742 execFuncCallback, &x, 0);
danielk1977d8123362004-06-12 09:25:12 +0000743 sqlite3_result_text(context, x.z, x.nUsed, SQLITE_TRANSIENT);
drh17435752007-08-16 04:30:38 +0000744 sqlite3_free(x.z);
drhc22bd472002-05-10 13:14:07 +0000745}
746
747/*
danielk1977d7263922007-02-05 14:21:47 +0000748** Implementation of tkt2213func(), a scalar function that takes exactly
749** one argument. It has two interesting features:
750**
751** * It calls sqlite3_value_text() 3 times on the argument sqlite3_value*.
752** If the three pointers returned are not the same an SQL error is raised.
753**
754** * Otherwise it returns a copy of the text representation of it's
755** argument in such a way as the VDBE representation is a Mem* cell
756** with the MEM_Term flag clear.
757**
758** Ticket #2213 can therefore be tested by evaluating the following
759** SQL expression:
760**
761** tkt2213func(tkt2213func('a string'));
762*/
763static void tkt2213Function(
764 sqlite3_context *context,
765 int argc,
766 sqlite3_value **argv
767){
768 int nText;
769 unsigned char const *zText1;
770 unsigned char const *zText2;
771 unsigned char const *zText3;
772
773 nText = sqlite3_value_bytes(argv[0]);
774 zText1 = sqlite3_value_text(argv[0]);
775 zText2 = sqlite3_value_text(argv[0]);
776 zText3 = sqlite3_value_text(argv[0]);
777
778 if( zText1!=zText2 || zText2!=zText3 ){
779 sqlite3_result_error(context, "tkt2213 is not fixed", -1);
780 }else{
781 char *zCopy = (char *)sqlite3_malloc(nText);
782 memcpy(zCopy, zText1, nText);
783 sqlite3_result_text(context, zCopy, nText, sqlite3_free);
784 }
785}
786
787/*
drh9310ef22007-04-27 17:16:20 +0000788** The following SQL function takes 4 arguments. The 2nd and
789** 4th argument must be one of these strings: 'text', 'text16',
790** or 'blob' corresponding to API functions
791**
792** sqlite3_value_text()
793** sqlite3_value_text16()
794** sqlite3_value_blob()
795**
796** The third argument is a string, either 'bytes' or 'bytes16' or 'noop',
797** corresponding to APIs:
798**
799** sqlite3_value_bytes()
800** sqlite3_value_bytes16()
801** noop
802**
803** The APIs designated by the 2nd through 4th arguments are applied
804** to the first argument in order. If the pointers returned by the
805** second and fourth are different, this routine returns 1. Otherwise,
806** this routine returns 0.
807**
808** This function is used to test to see when returned pointers from
809** the _text(), _text16() and _blob() APIs become invalidated.
810*/
811static void ptrChngFunction(
812 sqlite3_context *context,
813 int argc,
814 sqlite3_value **argv
815){
816 const void *p1, *p2;
817 const char *zCmd;
818 if( argc!=4 ) return;
819 zCmd = (const char*)sqlite3_value_text(argv[1]);
820 if( zCmd==0 ) return;
821 if( strcmp(zCmd,"text")==0 ){
822 p1 = (const void*)sqlite3_value_text(argv[0]);
823#ifndef SQLITE_OMIT_UTF16
824 }else if( strcmp(zCmd, "text16")==0 ){
825 p1 = (const void*)sqlite3_value_text16(argv[0]);
826#endif
827 }else if( strcmp(zCmd, "blob")==0 ){
828 p1 = (const void*)sqlite3_value_blob(argv[0]);
829 }else{
830 return;
831 }
832 zCmd = (const char*)sqlite3_value_text(argv[2]);
833 if( zCmd==0 ) return;
834 if( strcmp(zCmd,"bytes")==0 ){
835 sqlite3_value_bytes(argv[0]);
836#ifndef SQLITE_OMIT_UTF16
837 }else if( strcmp(zCmd, "bytes16")==0 ){
838 sqlite3_value_bytes16(argv[0]);
839#endif
840 }else if( strcmp(zCmd, "noop")==0 ){
841 /* do nothing */
842 }else{
843 return;
844 }
845 zCmd = (const char*)sqlite3_value_text(argv[3]);
846 if( zCmd==0 ) return;
847 if( strcmp(zCmd,"text")==0 ){
848 p2 = (const void*)sqlite3_value_text(argv[0]);
849#ifndef SQLITE_OMIT_UTF16
850 }else if( strcmp(zCmd, "text16")==0 ){
851 p2 = (const void*)sqlite3_value_text16(argv[0]);
852#endif
853 }else if( strcmp(zCmd, "blob")==0 ){
854 p2 = (const void*)sqlite3_value_blob(argv[0]);
855 }else{
856 return;
857 }
858 sqlite3_result_int(context, p1!=p2);
859}
860
861
862/*
drhc22bd472002-05-10 13:14:07 +0000863** Usage: sqlite_test_create_function DB
864**
danielk19776f8a5032004-05-10 10:34:51 +0000865** Call the sqlite3_create_function API on the given database in order
drhc22bd472002-05-10 13:14:07 +0000866** to create a function named "x_coalesce". This function does the same thing
867** as the "coalesce" function. This function also registers an SQL function
danielk1977e35ee192004-06-26 09:50:11 +0000868** named "x_sqlite_exec" that invokes sqlite3_exec(). Invoking sqlite3_exec()
drhc22bd472002-05-10 13:14:07 +0000869** in this way is illegal recursion and should raise an SQLITE_MISUSE error.
870** The effect is similar to trying to use the same database connection from
871** two threads at the same time.
872**
873** The original motivation for this routine was to be able to call the
danielk19776f8a5032004-05-10 10:34:51 +0000874** sqlite3_create_function function while a query is in progress in order
drhc22bd472002-05-10 13:14:07 +0000875** to test the SQLITE_MISUSE detection logic.
876*/
drhc2eef3b2002-08-31 18:53:06 +0000877static int test_create_function(
drhc22bd472002-05-10 13:14:07 +0000878 void *NotUsed,
879 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
880 int argc, /* Number of arguments */
881 char **argv /* Text of each argument */
882){
drhc60d0442004-09-30 13:43:13 +0000883 int rc;
drh9bb575f2004-09-06 17:24:11 +0000884 sqlite3 *db;
drh9bb575f2004-09-06 17:24:11 +0000885 extern void Md5_Register(sqlite3*);
danielk1977312d6b32004-06-29 13:18:23 +0000886
drhc22bd472002-05-10 13:14:07 +0000887 if( argc!=2 ){
888 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
danielk19774397de52005-01-12 12:44:03 +0000889 " DB\"", 0);
drhc22bd472002-05-10 13:14:07 +0000890 return TCL_ERROR;
891 }
drhb86ccfb2003-01-28 23:13:10 +0000892 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
drhc60d0442004-09-30 13:43:13 +0000893 rc = sqlite3_create_function(db, "x_coalesce", -1, SQLITE_ANY, 0,
drh4f0c5872007-03-26 22:05:01 +0000894 t1_ifnullFunc, 0, 0);
drh235a8182006-09-13 19:21:28 +0000895 if( rc==SQLITE_OK ){
896 rc = sqlite3_create_function(db, "hex8", 1, SQLITE_ANY, 0,
897 hex8Func, 0, 0);
898 }
drhaf304692007-04-23 23:56:31 +0000899#ifndef SQLITE_OMIT_UTF16
drh235a8182006-09-13 19:21:28 +0000900 if( rc==SQLITE_OK ){
901 rc = sqlite3_create_function(db, "hex16", 1, SQLITE_ANY, 0,
902 hex16Func, 0, 0);
903 }
drhaf304692007-04-23 23:56:31 +0000904#endif
danielk1977d7263922007-02-05 14:21:47 +0000905 if( rc==SQLITE_OK ){
906 rc = sqlite3_create_function(db, "tkt2213func", 1, SQLITE_ANY, 0,
907 tkt2213Function, 0, 0);
908 }
drh9310ef22007-04-27 17:16:20 +0000909 if( rc==SQLITE_OK ){
910 rc = sqlite3_create_function(db, "pointer_change", 4, SQLITE_ANY, 0,
911 ptrChngFunction, 0, 0);
912 }
danielk1977312d6b32004-06-29 13:18:23 +0000913
drh5436dc22004-11-14 04:04:17 +0000914#ifndef SQLITE_OMIT_UTF16
danielk1977312d6b32004-06-29 13:18:23 +0000915 /* Use the sqlite3_create_function16() API here. Mainly for fun, but also
916 ** because it is not tested anywhere else. */
drhc60d0442004-09-30 13:43:13 +0000917 if( rc==SQLITE_OK ){
danielk1977576ec6b2005-01-21 11:55:25 +0000918 sqlite3_value *pVal;
drhb21c8cd2007-08-21 19:33:56 +0000919 pVal = sqlite3ValueNew(db);
920 sqlite3ValueSetStr(pVal, -1, "x_sqlite_exec", SQLITE_UTF8, SQLITE_STATIC);
drhc60d0442004-09-30 13:43:13 +0000921 rc = sqlite3_create_function16(db,
drhb21c8cd2007-08-21 19:33:56 +0000922 sqlite3ValueText(pVal, SQLITE_UTF16NATIVE),
drhc60d0442004-09-30 13:43:13 +0000923 1, SQLITE_UTF16, db, sqlite3ExecFunc, 0, 0);
924 sqlite3ValueFree(pVal);
925 }
drh5436dc22004-11-14 04:04:17 +0000926#endif
927
drhc60d0442004-09-30 13:43:13 +0000928 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
drh4f0c5872007-03-26 22:05:01 +0000929 Tcl_SetResult(interp, (char *)t1ErrorName(rc), 0);
drhc22bd472002-05-10 13:14:07 +0000930 return TCL_OK;
931}
932
933/*
934** Routines to implement the x_count() aggregate function.
drh90669c12006-01-20 15:45:36 +0000935**
936** x_count() counts the number of non-null arguments. But there are
937** some twists for testing purposes.
938**
939** If the argument to x_count() is 40 then a UTF-8 error is reported
940** on the step function. If x_count(41) is seen, then a UTF-16 error
941** is reported on the step function. If the total count is 42, then
942** a UTF-8 error is reported on the finalize function.
drhc22bd472002-05-10 13:14:07 +0000943*/
drh4f0c5872007-03-26 22:05:01 +0000944typedef struct t1CountCtx t1CountCtx;
945struct t1CountCtx {
drhc22bd472002-05-10 13:14:07 +0000946 int n;
947};
drh4f0c5872007-03-26 22:05:01 +0000948static void t1CountStep(
949 sqlite3_context *context,
950 int argc,
951 sqlite3_value **argv
952){
953 t1CountCtx *p;
drh4f26d6c2004-05-26 23:25:30 +0000954 p = sqlite3_aggregate_context(context, sizeof(*p));
drh9c054832004-05-31 18:51:57 +0000955 if( (argc==0 || SQLITE_NULL!=sqlite3_value_type(argv[0]) ) && p ){
drhc22bd472002-05-10 13:14:07 +0000956 p->n++;
957 }
drh90669c12006-01-20 15:45:36 +0000958 if( argc>0 ){
959 int v = sqlite3_value_int(argv[0]);
960 if( v==40 ){
961 sqlite3_result_error(context, "value of 40 handed to x_count", -1);
danielk1977a1686c92006-01-23 07:52:37 +0000962#ifndef SQLITE_OMIT_UTF16
drh90669c12006-01-20 15:45:36 +0000963 }else if( v==41 ){
964 const char zUtf16ErrMsg[] = { 0, 0x61, 0, 0x62, 0, 0x63, 0, 0, 0};
965 sqlite3_result_error16(context, &zUtf16ErrMsg[1-SQLITE_BIGENDIAN], -1);
danielk1977a1686c92006-01-23 07:52:37 +0000966#endif
drh90669c12006-01-20 15:45:36 +0000967 }
968 }
drhc22bd472002-05-10 13:14:07 +0000969}
drh4f0c5872007-03-26 22:05:01 +0000970static void t1CountFinalize(sqlite3_context *context){
971 t1CountCtx *p;
drh4f26d6c2004-05-26 23:25:30 +0000972 p = sqlite3_aggregate_context(context, sizeof(*p));
drh90669c12006-01-20 15:45:36 +0000973 if( p ){
974 if( p->n==42 ){
975 sqlite3_result_error(context, "x_count totals to 42", -1);
976 }else{
977 sqlite3_result_int(context, p ? p->n : 0);
978 }
979 }
drhc22bd472002-05-10 13:14:07 +0000980}
981
982/*
983** Usage: sqlite_test_create_aggregate DB
984**
danielk19776f8a5032004-05-10 10:34:51 +0000985** Call the sqlite3_create_function API on the given database in order
drhc22bd472002-05-10 13:14:07 +0000986** to create a function named "x_count". This function does the same thing
987** as the "md5sum" function.
988**
989** The original motivation for this routine was to be able to call the
danielk19776f8a5032004-05-10 10:34:51 +0000990** sqlite3_create_aggregate function while a query is in progress in order
drh90669c12006-01-20 15:45:36 +0000991** to test the SQLITE_MISUSE detection logic. See misuse.test.
992**
993** This routine was later extended to test the use of sqlite3_result_error()
994** within aggregate functions.
drhc22bd472002-05-10 13:14:07 +0000995*/
drhc2eef3b2002-08-31 18:53:06 +0000996static int test_create_aggregate(
drhc22bd472002-05-10 13:14:07 +0000997 void *NotUsed,
998 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
999 int argc, /* Number of arguments */
1000 char **argv /* Text of each argument */
1001){
drh9bb575f2004-09-06 17:24:11 +00001002 sqlite3 *db;
drhc60d0442004-09-30 13:43:13 +00001003 int rc;
drhc22bd472002-05-10 13:14:07 +00001004 if( argc!=2 ){
1005 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1006 " FILENAME\"", 0);
1007 return TCL_ERROR;
1008 }
drhb86ccfb2003-01-28 23:13:10 +00001009 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
drhc60d0442004-09-30 13:43:13 +00001010 rc = sqlite3_create_function(db, "x_count", 0, SQLITE_UTF8, 0, 0,
drh4f0c5872007-03-26 22:05:01 +00001011 t1CountStep,t1CountFinalize);
drhc60d0442004-09-30 13:43:13 +00001012 if( rc==SQLITE_OK ){
1013 sqlite3_create_function(db, "x_count", 1, SQLITE_UTF8, 0, 0,
drh4f0c5872007-03-26 22:05:01 +00001014 t1CountStep,t1CountFinalize);
drhc60d0442004-09-30 13:43:13 +00001015 }
1016 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
drhc22bd472002-05-10 13:14:07 +00001017 return TCL_OK;
1018}
1019
1020
drh3c23a882007-01-09 14:01:13 +00001021/*
1022** Usage: printf TEXT
1023**
1024** Send output to printf. Use this rather than puts to merge the output
1025** in the correct sequence with debugging printfs inserted into C code.
1026** Puts uses a separate buffer and debugging statements will be out of
1027** sequence if it is used.
1028*/
1029static int test_printf(
1030 void *NotUsed,
1031 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1032 int argc, /* Number of arguments */
1033 char **argv /* Text of each argument */
1034){
1035 if( argc!=2 ){
1036 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1037 " TEXT\"", 0);
1038 return TCL_ERROR;
1039 }
1040 printf("%s\n", argv[1]);
1041 return TCL_OK;
1042}
1043
1044
drhc22bd472002-05-10 13:14:07 +00001045
1046/*
danielk19776f8a5032004-05-10 10:34:51 +00001047** Usage: sqlite3_mprintf_int FORMAT INTEGER INTEGER INTEGER
drhd1bf3512001-04-07 15:24:33 +00001048**
1049** Call mprintf with three integer arguments
1050*/
danielk19776f8a5032004-05-10 10:34:51 +00001051static int sqlite3_mprintf_int(
drhd1bf3512001-04-07 15:24:33 +00001052 void *NotUsed,
1053 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1054 int argc, /* Number of arguments */
1055 char **argv /* Text of each argument */
1056){
1057 int a[3], i;
1058 char *z;
1059 if( argc!=5 ){
1060 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1061 " FORMAT INT INT INT\"", 0);
1062 return TCL_ERROR;
1063 }
1064 for(i=2; i<5; i++){
1065 if( Tcl_GetInt(interp, argv[i], &a[i-2]) ) return TCL_ERROR;
1066 }
danielk19776f8a5032004-05-10 10:34:51 +00001067 z = sqlite3_mprintf(argv[1], a[0], a[1], a[2]);
drhd1bf3512001-04-07 15:24:33 +00001068 Tcl_AppendResult(interp, z, 0);
drh3f4fedb2004-05-31 19:34:33 +00001069 sqlite3_free(z);
drhd1bf3512001-04-07 15:24:33 +00001070 return TCL_OK;
1071}
1072
1073/*
drh9d213ef2004-06-30 04:02:11 +00001074** If zNum represents an integer that will fit in 64-bits, then set
1075** *pValue to that integer and return true. Otherwise return false.
1076*/
1077static int sqlite3GetInt64(const char *zNum, i64 *pValue){
1078 if( sqlite3FitsIn64Bits(zNum) ){
drhb6a9ece2007-06-26 00:37:27 +00001079 sqlite3Atoi64(zNum, pValue);
drh9d213ef2004-06-30 04:02:11 +00001080 return 1;
1081 }
1082 return 0;
1083}
1084
1085/*
drhe9707672004-06-25 01:10:48 +00001086** Usage: sqlite3_mprintf_int64 FORMAT INTEGER INTEGER INTEGER
1087**
1088** Call mprintf with three 64-bit integer arguments
1089*/
1090static int sqlite3_mprintf_int64(
1091 void *NotUsed,
1092 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1093 int argc, /* Number of arguments */
1094 char **argv /* Text of each argument */
1095){
1096 int i;
1097 sqlite_int64 a[3];
1098 char *z;
1099 if( argc!=5 ){
1100 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1101 " FORMAT INT INT INT\"", 0);
1102 return TCL_ERROR;
1103 }
1104 for(i=2; i<5; i++){
1105 if( !sqlite3GetInt64(argv[i], &a[i-2]) ){
1106 Tcl_AppendResult(interp, "argument is not a valid 64-bit integer", 0);
1107 return TCL_ERROR;
1108 }
1109 }
1110 z = sqlite3_mprintf(argv[1], a[0], a[1], a[2]);
1111 Tcl_AppendResult(interp, z, 0);
1112 sqlite3_free(z);
1113 return TCL_OK;
1114}
1115
1116/*
danielk19776f8a5032004-05-10 10:34:51 +00001117** Usage: sqlite3_mprintf_str FORMAT INTEGER INTEGER STRING
drhd1bf3512001-04-07 15:24:33 +00001118**
1119** Call mprintf with two integer arguments and one string argument
1120*/
danielk19776f8a5032004-05-10 10:34:51 +00001121static int sqlite3_mprintf_str(
drhd1bf3512001-04-07 15:24:33 +00001122 void *NotUsed,
1123 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1124 int argc, /* Number of arguments */
1125 char **argv /* Text of each argument */
1126){
1127 int a[3], i;
1128 char *z;
chwf220b242002-06-16 04:54:28 +00001129 if( argc<4 || argc>5 ){
drhd1bf3512001-04-07 15:24:33 +00001130 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
chwf220b242002-06-16 04:54:28 +00001131 " FORMAT INT INT ?STRING?\"", 0);
drhd1bf3512001-04-07 15:24:33 +00001132 return TCL_ERROR;
1133 }
1134 for(i=2; i<4; i++){
1135 if( Tcl_GetInt(interp, argv[i], &a[i-2]) ) return TCL_ERROR;
1136 }
danielk19776f8a5032004-05-10 10:34:51 +00001137 z = sqlite3_mprintf(argv[1], a[0], a[1], argc>4 ? argv[4] : NULL);
drhd1bf3512001-04-07 15:24:33 +00001138 Tcl_AppendResult(interp, z, 0);
drh3f4fedb2004-05-31 19:34:33 +00001139 sqlite3_free(z);
drhd1bf3512001-04-07 15:24:33 +00001140 return TCL_OK;
1141}
1142
1143/*
drhb3738b62007-03-31 15:02:49 +00001144** Usage: sqlite3_snprintf_str INTEGER FORMAT INTEGER INTEGER STRING
1145**
1146** Call mprintf with two integer arguments and one string argument
1147*/
1148static int sqlite3_snprintf_str(
1149 void *NotUsed,
1150 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1151 int argc, /* Number of arguments */
1152 char **argv /* Text of each argument */
1153){
1154 int a[3], i;
1155 int n;
1156 char *z;
1157 if( argc<5 || argc>6 ){
1158 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1159 " INT FORMAT INT INT ?STRING?\"", 0);
1160 return TCL_ERROR;
1161 }
1162 if( Tcl_GetInt(interp, argv[1], &n) ) return TCL_ERROR;
1163 if( n<0 ){
1164 Tcl_AppendResult(interp, "N must be non-negative", 0);
1165 return TCL_ERROR;
1166 }
1167 for(i=3; i<5; i++){
1168 if( Tcl_GetInt(interp, argv[i], &a[i-3]) ) return TCL_ERROR;
1169 }
1170 z = sqlite3_malloc( n+1 );
1171 sqlite3_snprintf(n, z, argv[2], a[0], a[1], argc>4 ? argv[5] : NULL);
1172 Tcl_AppendResult(interp, z, 0);
1173 sqlite3_free(z);
1174 return TCL_OK;
1175}
1176
1177/*
drh63782852005-08-30 19:30:59 +00001178** Usage: sqlite3_mprintf_double FORMAT INTEGER INTEGER DOUBLE
drhd1bf3512001-04-07 15:24:33 +00001179**
1180** Call mprintf with two integer arguments and one double argument
1181*/
danielk19776f8a5032004-05-10 10:34:51 +00001182static int sqlite3_mprintf_double(
drhd1bf3512001-04-07 15:24:33 +00001183 void *NotUsed,
1184 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1185 int argc, /* Number of arguments */
1186 char **argv /* Text of each argument */
1187){
1188 int a[3], i;
1189 double r;
1190 char *z;
1191 if( argc!=5 ){
1192 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
drh63782852005-08-30 19:30:59 +00001193 " FORMAT INT INT DOUBLE\"", 0);
drhd1bf3512001-04-07 15:24:33 +00001194 return TCL_ERROR;
1195 }
1196 for(i=2; i<4; i++){
1197 if( Tcl_GetInt(interp, argv[i], &a[i-2]) ) return TCL_ERROR;
1198 }
1199 if( Tcl_GetDouble(interp, argv[4], &r) ) return TCL_ERROR;
danielk19776f8a5032004-05-10 10:34:51 +00001200 z = sqlite3_mprintf(argv[1], a[0], a[1], r);
drhd1bf3512001-04-07 15:24:33 +00001201 Tcl_AppendResult(interp, z, 0);
drh3f4fedb2004-05-31 19:34:33 +00001202 sqlite3_free(z);
drhd1bf3512001-04-07 15:24:33 +00001203 return TCL_OK;
1204}
1205
1206/*
drh63782852005-08-30 19:30:59 +00001207** Usage: sqlite3_mprintf_scaled FORMAT DOUBLE DOUBLE
drhb621c232004-02-21 19:41:04 +00001208**
1209** Call mprintf with a single double argument which is the product of the
1210** two arguments given above. This is used to generate overflow and underflow
1211** doubles to test that they are converted properly.
1212*/
danielk19776f8a5032004-05-10 10:34:51 +00001213static int sqlite3_mprintf_scaled(
drhb621c232004-02-21 19:41:04 +00001214 void *NotUsed,
1215 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1216 int argc, /* Number of arguments */
1217 char **argv /* Text of each argument */
1218){
1219 int i;
1220 double r[2];
1221 char *z;
1222 if( argc!=4 ){
1223 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1224 " FORMAT DOUBLE DOUBLE\"", 0);
1225 return TCL_ERROR;
1226 }
1227 for(i=2; i<4; i++){
1228 if( Tcl_GetDouble(interp, argv[i], &r[i-2]) ) return TCL_ERROR;
1229 }
danielk19776f8a5032004-05-10 10:34:51 +00001230 z = sqlite3_mprintf(argv[1], r[0]*r[1]);
drhb621c232004-02-21 19:41:04 +00001231 Tcl_AppendResult(interp, z, 0);
drh3f4fedb2004-05-31 19:34:33 +00001232 sqlite3_free(z);
drhb621c232004-02-21 19:41:04 +00001233 return TCL_OK;
1234}
1235
1236/*
drhe29b1a02004-07-17 21:56:09 +00001237** Usage: sqlite3_mprintf_stronly FORMAT STRING
1238**
1239** Call mprintf with a single double argument which is the product of the
1240** two arguments given above. This is used to generate overflow and underflow
1241** doubles to test that they are converted properly.
1242*/
1243static int sqlite3_mprintf_stronly(
1244 void *NotUsed,
1245 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1246 int argc, /* Number of arguments */
1247 char **argv /* Text of each argument */
1248){
1249 char *z;
1250 if( argc!=3 ){
1251 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1252 " FORMAT STRING\"", 0);
1253 return TCL_ERROR;
1254 }
1255 z = sqlite3_mprintf(argv[1], argv[2]);
1256 Tcl_AppendResult(interp, z, 0);
1257 sqlite3_free(z);
1258 return TCL_OK;
1259}
1260
1261/*
drh63782852005-08-30 19:30:59 +00001262** Usage: sqlite3_mprintf_hexdouble FORMAT HEX
1263**
1264** Call mprintf with a single double argument which is derived from the
1265** hexadecimal encoding of an IEEE double.
1266*/
1267static int sqlite3_mprintf_hexdouble(
1268 void *NotUsed,
1269 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1270 int argc, /* Number of arguments */
1271 char **argv /* Text of each argument */
1272){
1273 char *z;
1274 double r;
1275 unsigned x1, x2;
1276 long long unsigned d;
1277 if( argc!=3 ){
1278 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1279 " FORMAT STRING\"", 0);
1280 return TCL_ERROR;
1281 }
1282 if( sscanf(argv[2], "%08x%08x", &x2, &x1)!=2 ){
1283 Tcl_AppendResult(interp, "2nd argument should be 16-characters of hex", 0);
1284 return TCL_ERROR;
1285 }
1286 d = x2;
1287 d = (d<<32) + x1;
1288 memcpy(&r, &d, sizeof(r));
1289 z = sqlite3_mprintf(argv[1], r);
1290 Tcl_AppendResult(interp, z, 0);
1291 sqlite3_free(z);
1292 return TCL_OK;
1293}
1294
1295/*
danielk197752622822006-01-09 09:59:49 +00001296** Usage: sqlite3_enable_shared_cache BOOLEAN
danielk1977aef0bf62005-12-30 16:28:01 +00001297**
1298*/
drh6f7adc82006-01-11 21:41:20 +00001299#if !defined(SQLITE_OMIT_SHARED_CACHE)
1300static int test_enable_shared(
danielk197752622822006-01-09 09:59:49 +00001301 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
danielk1977aef0bf62005-12-30 16:28:01 +00001302 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1303 int objc, /* Number of arguments */
1304 Tcl_Obj *CONST objv[] /* Command arguments */
1305){
1306 int rc;
1307 int enable;
danielk197752622822006-01-09 09:59:49 +00001308 int ret = 0;
drhe53831d2007-08-17 01:14:38 +00001309 extern int sqlite3SharedCacheEnabled;
danielk1977aef0bf62005-12-30 16:28:01 +00001310
1311 if( objc!=2 ){
1312 Tcl_WrongNumArgs(interp, 1, objv, "BOOLEAN");
1313 return TCL_ERROR;
1314 }
1315 if( Tcl_GetBooleanFromObj(interp, objv[1], &enable) ){
1316 return TCL_ERROR;
1317 }
drhe53831d2007-08-17 01:14:38 +00001318 ret = sqlite3SharedCacheEnabled;
drh6f7adc82006-01-11 21:41:20 +00001319 rc = sqlite3_enable_shared_cache(enable);
danielk1977aef0bf62005-12-30 16:28:01 +00001320 if( rc!=SQLITE_OK ){
1321 Tcl_SetResult(interp, (char *)sqlite3ErrStr(rc), TCL_STATIC);
1322 return TCL_ERROR;
1323 }
danielk197752622822006-01-09 09:59:49 +00001324 Tcl_SetObjResult(interp, Tcl_NewBooleanObj(ret));
danielk1977aef0bf62005-12-30 16:28:01 +00001325 return TCL_OK;
1326}
1327#endif
1328
drh16a9b832007-05-05 18:39:25 +00001329
1330
danielk1977aef0bf62005-12-30 16:28:01 +00001331/*
drh4ac285a2006-09-15 07:28:50 +00001332** Usage: sqlite3_extended_result_codes DB BOOLEAN
1333**
1334*/
1335static int test_extended_result_codes(
1336 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
1337 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1338 int objc, /* Number of arguments */
1339 Tcl_Obj *CONST objv[] /* Command arguments */
1340){
1341 int enable;
1342 sqlite3 *db;
1343
1344 if( objc!=3 ){
1345 Tcl_WrongNumArgs(interp, 1, objv, "DB BOOLEAN");
1346 return TCL_ERROR;
1347 }
1348 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
1349 if( Tcl_GetBooleanFromObj(interp, objv[2], &enable) ) return TCL_ERROR;
1350 sqlite3_extended_result_codes(db, enable);
1351 return TCL_OK;
1352}
1353
1354/*
danielk1977161fb792006-01-24 10:58:21 +00001355** Usage: sqlite3_libversion_number
1356**
1357*/
1358static int test_libversion_number(
1359 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
1360 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1361 int objc, /* Number of arguments */
1362 Tcl_Obj *CONST objv[] /* Command arguments */
1363){
1364 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_libversion_number()));
1365 return TCL_OK;
1366}
1367
1368/*
danielk1977deb802c2006-02-09 13:43:28 +00001369** Usage: sqlite3_table_column_metadata DB dbname tblname colname
1370**
1371*/
1372#ifdef SQLITE_ENABLE_COLUMN_METADATA
1373static int test_table_column_metadata(
1374 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
1375 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1376 int objc, /* Number of arguments */
1377 Tcl_Obj *CONST objv[] /* Command arguments */
1378){
1379 sqlite3 *db;
1380 const char *zDb;
1381 const char *zTbl;
1382 const char *zCol;
1383 int rc;
1384 Tcl_Obj *pRet;
1385
1386 const char *zDatatype;
1387 const char *zCollseq;
1388 int notnull;
1389 int primarykey;
1390 int autoincrement;
1391
1392 if( objc!=5 ){
1393 Tcl_WrongNumArgs(interp, 1, objv, "DB dbname tblname colname");
1394 return TCL_ERROR;
1395 }
1396 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
1397 zDb = Tcl_GetString(objv[2]);
1398 zTbl = Tcl_GetString(objv[3]);
1399 zCol = Tcl_GetString(objv[4]);
1400
1401 if( strlen(zDb)==0 ) zDb = 0;
1402
1403 rc = sqlite3_table_column_metadata(db, zDb, zTbl, zCol,
1404 &zDatatype, &zCollseq, &notnull, &primarykey, &autoincrement);
1405
1406 if( rc!=SQLITE_OK ){
1407 Tcl_AppendResult(interp, sqlite3_errmsg(db), 0);
1408 return TCL_ERROR;
1409 }
1410
1411 pRet = Tcl_NewObj();
1412 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zDatatype, -1));
1413 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zCollseq, -1));
1414 Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(notnull));
1415 Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(primarykey));
1416 Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(autoincrement));
1417 Tcl_SetObjResult(interp, pRet);
1418
1419 return TCL_OK;
1420}
1421#endif
1422
danielk1977dcbb5d32007-05-04 18:36:44 +00001423#ifndef SQLITE_OMIT_INCRBLOB
1424
1425/*
1426** sqlite3_blob_read CHANNEL OFFSET N
danielk1977a9808b32007-05-07 09:32:45 +00001427**
1428** This command is used to test the sqlite3_blob_read() in ways that
1429** the Tcl channel interface does not. The first argument should
1430** be the name of a valid channel created by the [incrblob] method
1431** of a database handle. This function calls sqlite3_blob_read()
1432** to read N bytes from offset OFFSET from the underlying SQLite
1433** blob handle.
1434**
1435** On success, a byte-array object containing the read data is
1436** returned. On failure, the interpreter result is set to the
1437** text representation of the returned error code (i.e. "SQLITE_NOMEM")
1438** and a Tcl exception is thrown.
danielk1977dcbb5d32007-05-04 18:36:44 +00001439*/
1440static int test_blob_read(
1441 ClientData clientData, /* Not used */
1442 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1443 int objc, /* Number of arguments */
1444 Tcl_Obj *CONST objv[] /* Command arguments */
1445){
1446 Tcl_Channel channel;
1447 ClientData instanceData;
1448 sqlite3_blob *pBlob;
1449 int notUsed;
1450 int nByte;
1451 int iOffset;
1452 unsigned char *zBuf;
1453 int rc;
1454
1455 if( objc!=4 ){
1456 Tcl_WrongNumArgs(interp, 1, objv, "CHANNEL OFFSET N");
1457 return TCL_ERROR;
1458 }
1459
1460 channel = Tcl_GetChannel(interp, Tcl_GetString(objv[1]), &notUsed);
1461 if( !channel
1462 || TCL_OK!=Tcl_GetIntFromObj(interp, objv[2], &iOffset)
1463 || TCL_OK!=Tcl_GetIntFromObj(interp, objv[3], &nByte)
1464 || nByte<0 || iOffset<0
1465 ){
1466 return TCL_ERROR;
1467 }
1468
1469 instanceData = Tcl_GetChannelInstanceData(channel);
1470 pBlob = *((sqlite3_blob **)instanceData);
1471
1472 zBuf = (unsigned char *)Tcl_Alloc(nByte);
1473 rc = sqlite3_blob_read(pBlob, zBuf, nByte, iOffset);
1474 if( rc==SQLITE_OK ){
1475 Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(zBuf, nByte));
1476 }else{
1477 Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_VOLATILE);
1478 }
1479 Tcl_Free((char *)zBuf);
1480
1481 return (rc==SQLITE_OK ? TCL_OK : TCL_ERROR);
1482}
1483
1484/*
1485** sqlite3_blob_write CHANNEL OFFSET DATA
danielk1977a9808b32007-05-07 09:32:45 +00001486**
1487** This command is used to test the sqlite3_blob_write() in ways that
1488** the Tcl channel interface does not. The first argument should
1489** be the name of a valid channel created by the [incrblob] method
1490** of a database handle. This function calls sqlite3_blob_write()
1491** to write the DATA byte-array to the underlying SQLite blob handle.
1492** at offset OFFSET.
1493**
1494** On success, an empty string is returned. On failure, the interpreter
1495** result is set to the text representation of the returned error code
1496** (i.e. "SQLITE_NOMEM") and a Tcl exception is thrown.
danielk1977dcbb5d32007-05-04 18:36:44 +00001497*/
1498static int test_blob_write(
1499 ClientData clientData, /* Not used */
1500 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1501 int objc, /* Number of arguments */
1502 Tcl_Obj *CONST objv[] /* Command arguments */
1503){
1504 Tcl_Channel channel;
1505 ClientData instanceData;
1506 sqlite3_blob *pBlob;
1507 int notUsed;
1508 int iOffset;
1509 int rc;
1510
1511 unsigned char *zBuf;
1512 int nBuf;
1513
1514 if( objc!=4 ){
1515 Tcl_WrongNumArgs(interp, 1, objv, "CHANNEL OFFSET DATA");
1516 return TCL_ERROR;
1517 }
1518
1519 channel = Tcl_GetChannel(interp, Tcl_GetString(objv[1]), &notUsed);
1520 if( !channel
1521 || TCL_OK!=Tcl_GetIntFromObj(interp, objv[2], &iOffset)
1522 || iOffset<0
1523 ){
1524 return TCL_ERROR;
1525 }
1526
1527 instanceData = Tcl_GetChannelInstanceData(channel);
1528 pBlob = *((sqlite3_blob **)instanceData);
1529
1530 zBuf = Tcl_GetByteArrayFromObj(objv[3], &nBuf);
1531 rc = sqlite3_blob_write(pBlob, zBuf, nBuf, iOffset);
1532 if( rc!=SQLITE_OK ){
1533 Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_VOLATILE);
1534 }
1535
1536 return (rc==SQLITE_OK ? TCL_OK : TCL_ERROR);
1537}
1538#endif
drhc2e87a32006-06-27 15:16:14 +00001539
danielk1977deb802c2006-02-09 13:43:28 +00001540/*
danielk1977a393c032007-05-07 14:58:53 +00001541** Usage: sqlite3_create_collation_v2 DB-HANDLE NAME CMP-PROC DEL-PROC
danielk1977a9808b32007-05-07 09:32:45 +00001542**
1543** This Tcl proc is used for testing the experimental
danielk1977a393c032007-05-07 14:58:53 +00001544** sqlite3_create_collation_v2() interface.
danielk1977a9808b32007-05-07 09:32:45 +00001545*/
1546struct TestCollationX {
1547 Tcl_Interp *interp;
1548 Tcl_Obj *pCmp;
1549 Tcl_Obj *pDel;
1550};
1551typedef struct TestCollationX TestCollationX;
1552static void testCreateCollationDel(void *pCtx){
1553 TestCollationX *p = (TestCollationX *)pCtx;
1554
1555 int rc = Tcl_EvalObjEx(p->interp, p->pDel, TCL_EVAL_DIRECT|TCL_EVAL_GLOBAL);
1556 if( rc!=TCL_OK ){
1557 Tcl_BackgroundError(p->interp);
1558 }
1559
1560 Tcl_DecrRefCount(p->pCmp);
1561 Tcl_DecrRefCount(p->pDel);
1562 sqlite3_free((void *)p);
1563}
1564static int testCreateCollationCmp(
1565 void *pCtx,
1566 int nLeft,
1567 const void *zLeft,
1568 int nRight,
1569 const void *zRight
1570){
1571 TestCollationX *p = (TestCollationX *)pCtx;
1572 Tcl_Obj *pScript = Tcl_DuplicateObj(p->pCmp);
1573 int iRes = 0;
1574
1575 Tcl_IncrRefCount(pScript);
1576 Tcl_ListObjAppendElement(0, pScript, Tcl_NewStringObj((char *)zLeft, nLeft));
1577 Tcl_ListObjAppendElement(0, pScript, Tcl_NewStringObj((char *)zRight,nRight));
1578
1579 if( TCL_OK!=Tcl_EvalObjEx(p->interp, pScript, TCL_EVAL_DIRECT|TCL_EVAL_GLOBAL)
1580 || TCL_OK!=Tcl_GetIntFromObj(p->interp, Tcl_GetObjResult(p->interp), &iRes)
1581 ){
1582 Tcl_BackgroundError(p->interp);
1583 }
1584 Tcl_DecrRefCount(pScript);
1585
1586 return iRes;
1587}
danielk1977a393c032007-05-07 14:58:53 +00001588static int test_create_collation_v2(
danielk1977a9808b32007-05-07 09:32:45 +00001589 ClientData clientData, /* Not used */
1590 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1591 int objc, /* Number of arguments */
1592 Tcl_Obj *CONST objv[] /* Command arguments */
1593){
1594 TestCollationX *p;
1595 sqlite3 *db;
1596
1597 if( objc!=5 ){
1598 Tcl_WrongNumArgs(interp, 1, objv, "DB-HANDLE NAME CMP-PROC DEL-PROC");
1599 return TCL_ERROR;
1600 }
1601 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
1602
1603 p = (TestCollationX *)sqlite3_malloc(sizeof(TestCollationX));
1604 p->pCmp = objv[3];
1605 p->pDel = objv[4];
1606 p->interp = interp;
1607 Tcl_IncrRefCount(p->pCmp);
1608 Tcl_IncrRefCount(p->pDel);
1609
danielk1977a393c032007-05-07 14:58:53 +00001610 sqlite3_create_collation_v2(db, Tcl_GetString(objv[2]), SQLITE_UTF8,
danielk1977a9808b32007-05-07 09:32:45 +00001611 (void *)p, testCreateCollationCmp, testCreateCollationDel
1612 );
1613 return TCL_OK;
1614}
1615
1616/*
danielk197769e777f2006-06-14 10:38:02 +00001617** Usage: sqlite3_load_extension DB-HANDLE FILE ?PROC?
1618*/
1619static int test_load_extension(
1620 ClientData clientData, /* Not used */
1621 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1622 int objc, /* Number of arguments */
1623 Tcl_Obj *CONST objv[] /* Command arguments */
1624){
1625 Tcl_CmdInfo cmdInfo;
1626 sqlite3 *db;
1627 int rc;
1628 char *zDb;
1629 char *zFile;
1630 char *zProc = 0;
1631 char *zErr = 0;
1632
1633 if( objc!=4 && objc!=3 ){
1634 Tcl_WrongNumArgs(interp, 1, objv, "DB-HANDLE FILE ?PROC?");
1635 return TCL_ERROR;
1636 }
1637 zDb = Tcl_GetString(objv[1]);
1638 zFile = Tcl_GetString(objv[2]);
1639 if( objc==4 ){
1640 zProc = Tcl_GetString(objv[3]);
1641 }
1642
1643 /* Extract the C database handle from the Tcl command name */
1644 if( !Tcl_GetCommandInfo(interp, zDb, &cmdInfo) ){
1645 Tcl_AppendResult(interp, "command not found: ", zDb, (char*)0);
1646 return TCL_ERROR;
1647 }
1648 db = ((struct SqliteDb*)cmdInfo.objClientData)->db;
1649 assert(db);
1650
1651 /* Call the underlying C function. If an error occurs, set rc to
1652 ** TCL_ERROR and load any error string into the interpreter. If no
1653 ** error occurs, set rc to TCL_OK.
1654 */
drhc2e87a32006-06-27 15:16:14 +00001655#ifdef SQLITE_OMIT_LOAD_EXTENSION
1656 rc = SQLITE_ERROR;
1657 zErr = sqlite3_mprintf("this build omits sqlite3_load_extension()");
1658#else
danielk197769e777f2006-06-14 10:38:02 +00001659 rc = sqlite3_load_extension(db, zFile, zProc, &zErr);
drhc2e87a32006-06-27 15:16:14 +00001660#endif
danielk197769e777f2006-06-14 10:38:02 +00001661 if( rc!=SQLITE_OK ){
1662 Tcl_SetResult(interp, zErr ? zErr : "", TCL_VOLATILE);
1663 rc = TCL_ERROR;
1664 }else{
1665 rc = TCL_OK;
1666 }
1667 sqlite3_free(zErr);
1668
1669 return rc;
1670}
1671
1672/*
drhc2e87a32006-06-27 15:16:14 +00001673** Usage: sqlite3_enable_load_extension DB-HANDLE ONOFF
1674*/
1675static int test_enable_load(
1676 ClientData clientData, /* Not used */
1677 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1678 int objc, /* Number of arguments */
1679 Tcl_Obj *CONST objv[] /* Command arguments */
1680){
1681 Tcl_CmdInfo cmdInfo;
1682 sqlite3 *db;
1683 char *zDb;
1684 int onoff;
1685
1686 if( objc!=3 ){
1687 Tcl_WrongNumArgs(interp, 1, objv, "DB-HANDLE ONOFF");
1688 return TCL_ERROR;
1689 }
1690 zDb = Tcl_GetString(objv[1]);
1691
1692 /* Extract the C database handle from the Tcl command name */
1693 if( !Tcl_GetCommandInfo(interp, zDb, &cmdInfo) ){
1694 Tcl_AppendResult(interp, "command not found: ", zDb, (char*)0);
1695 return TCL_ERROR;
1696 }
1697 db = ((struct SqliteDb*)cmdInfo.objClientData)->db;
1698 assert(db);
1699
1700 /* Get the onoff parameter */
1701 if( Tcl_GetBooleanFromObj(interp, objv[2], &onoff) ){
1702 return TCL_ERROR;
1703 }
1704
1705#ifdef SQLITE_OMIT_LOAD_EXTENSION
1706 Tcl_AppendResult(interp, "this build omits sqlite3_load_extension()");
1707 return TCL_ERROR;
1708#else
1709 sqlite3_enable_load_extension(db, onoff);
1710 return TCL_OK;
1711#endif
1712}
1713
1714/*
drh28b4e482002-03-11 02:06:13 +00001715** Usage: sqlite_abort
1716**
1717** Shutdown the process immediately. This is not a clean shutdown.
1718** This command is used to test the recoverability of a database in
1719** the event of a program crash.
1720*/
1721static int sqlite_abort(
1722 void *NotUsed,
1723 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1724 int argc, /* Number of arguments */
1725 char **argv /* Text of each argument */
1726){
1727 assert( interp==0 ); /* This will always fail */
1728 return TCL_OK;
1729}
1730
1731/*
drh6cbe1f12002-07-01 00:31:36 +00001732** The following routine is a user-defined SQL function whose purpose
1733** is to test the sqlite_set_result() API.
1734*/
danielk19770ae8b832004-05-25 12:05:56 +00001735static void testFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
drh6cbe1f12002-07-01 00:31:36 +00001736 while( argc>=2 ){
drh03d847e2005-12-09 20:21:58 +00001737 const char *zArg0 = (char*)sqlite3_value_text(argv[0]);
danielk19776d88bad2004-05-27 14:23:36 +00001738 if( zArg0 ){
1739 if( 0==sqlite3StrICmp(zArg0, "int") ){
1740 sqlite3_result_int(context, sqlite3_value_int(argv[1]));
1741 }else if( sqlite3StrICmp(zArg0,"int64")==0 ){
1742 sqlite3_result_int64(context, sqlite3_value_int64(argv[1]));
1743 }else if( sqlite3StrICmp(zArg0,"string")==0 ){
drh03d847e2005-12-09 20:21:58 +00001744 sqlite3_result_text(context, (char*)sqlite3_value_text(argv[1]), -1,
danielk1977d8123362004-06-12 09:25:12 +00001745 SQLITE_TRANSIENT);
danielk19776d88bad2004-05-27 14:23:36 +00001746 }else if( sqlite3StrICmp(zArg0,"double")==0 ){
1747 sqlite3_result_double(context, sqlite3_value_double(argv[1]));
1748 }else if( sqlite3StrICmp(zArg0,"null")==0 ){
1749 sqlite3_result_null(context);
1750 }else if( sqlite3StrICmp(zArg0,"value")==0 ){
1751 sqlite3_result_value(context, argv[sqlite3_value_int(argv[1])]);
1752 }else{
1753 goto error_out;
1754 }
drh6cbe1f12002-07-01 00:31:36 +00001755 }else{
danielk19776d88bad2004-05-27 14:23:36 +00001756 goto error_out;
drh6cbe1f12002-07-01 00:31:36 +00001757 }
1758 argc -= 2;
1759 argv += 2;
1760 }
danielk19776d88bad2004-05-27 14:23:36 +00001761 return;
1762
1763error_out:
1764 sqlite3_result_error(context,"first argument should be one of: "
1765 "int int64 string double null value", -1);
drh6cbe1f12002-07-01 00:31:36 +00001766}
1767
1768/*
1769** Usage: sqlite_register_test_function DB NAME
1770**
1771** Register the test SQL function on the database DB under the name NAME.
1772*/
drhc2eef3b2002-08-31 18:53:06 +00001773static int test_register_func(
drh6cbe1f12002-07-01 00:31:36 +00001774 void *NotUsed,
1775 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1776 int argc, /* Number of arguments */
1777 char **argv /* Text of each argument */
1778){
drh9bb575f2004-09-06 17:24:11 +00001779 sqlite3 *db;
drh6cbe1f12002-07-01 00:31:36 +00001780 int rc;
1781 if( argc!=3 ){
1782 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1783 " DB FUNCTION-NAME", 0);
1784 return TCL_ERROR;
1785 }
drhb86ccfb2003-01-28 23:13:10 +00001786 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
danielk1977f9d64d22004-06-19 08:18:07 +00001787 rc = sqlite3_create_function(db, argv[2], -1, SQLITE_UTF8, 0,
danielk1977d8123362004-06-12 09:25:12 +00001788 testFunc, 0, 0);
drh6cbe1f12002-07-01 00:31:36 +00001789 if( rc!=0 ){
danielk1977f20b21c2004-05-31 23:56:42 +00001790 Tcl_AppendResult(interp, sqlite3ErrStr(rc), 0);
drh6cbe1f12002-07-01 00:31:36 +00001791 return TCL_ERROR;
1792 }
drhc60d0442004-09-30 13:43:13 +00001793 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
drh6cbe1f12002-07-01 00:31:36 +00001794 return TCL_OK;
1795}
1796
1797/*
danielk1977106bb232004-05-21 10:08:53 +00001798** Usage: sqlite3_finalize STMT
drhb86ccfb2003-01-28 23:13:10 +00001799**
danielk1977106bb232004-05-21 10:08:53 +00001800** Finalize a statement handle.
drhb86ccfb2003-01-28 23:13:10 +00001801*/
1802static int test_finalize(
danielk1977106bb232004-05-21 10:08:53 +00001803 void * clientData,
1804 Tcl_Interp *interp,
1805 int objc,
1806 Tcl_Obj *CONST objv[]
drhb86ccfb2003-01-28 23:13:10 +00001807){
danielk1977106bb232004-05-21 10:08:53 +00001808 sqlite3_stmt *pStmt;
drhb86ccfb2003-01-28 23:13:10 +00001809 int rc;
drhdddb2f22007-01-03 23:37:28 +00001810 sqlite3 *db = 0;
danielk1977106bb232004-05-21 10:08:53 +00001811
1812 if( objc!=2 ){
1813 Tcl_AppendResult(interp, "wrong # args: should be \"",
1814 Tcl_GetStringFromObj(objv[0], 0), " <STMT>", 0);
drhb86ccfb2003-01-28 23:13:10 +00001815 return TCL_ERROR;
1816 }
danielk1977106bb232004-05-21 10:08:53 +00001817
1818 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
1819
danielk19774397de52005-01-12 12:44:03 +00001820 if( pStmt ){
1821 db = StmtToDb(pStmt);
1822 }
danielk1977fc57d7b2004-05-26 02:04:57 +00001823 rc = sqlite3_finalize(pStmt);
drh4f0c5872007-03-26 22:05:01 +00001824 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
danielk19774397de52005-01-12 12:44:03 +00001825 if( db && sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
danielk1977106bb232004-05-21 10:08:53 +00001826 return TCL_OK;
1827}
1828
1829/*
1830** Usage: sqlite3_reset STMT
1831**
danielk1977261919c2005-12-06 12:52:59 +00001832** Reset a statement handle.
danielk1977106bb232004-05-21 10:08:53 +00001833*/
1834static int test_reset(
1835 void * clientData,
1836 Tcl_Interp *interp,
1837 int objc,
1838 Tcl_Obj *CONST objv[]
1839){
1840 sqlite3_stmt *pStmt;
1841 int rc;
1842
1843 if( objc!=2 ){
1844 Tcl_AppendResult(interp, "wrong # args: should be \"",
1845 Tcl_GetStringFromObj(objv[0], 0), " <STMT>", 0);
1846 return TCL_ERROR;
1847 }
1848
1849 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
1850
danielk1977fc57d7b2004-05-26 02:04:57 +00001851 rc = sqlite3_reset(pStmt);
danielk1977261919c2005-12-06 12:52:59 +00001852 if( pStmt && sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ){
1853 return TCL_ERROR;
1854 }
drh4f0c5872007-03-26 22:05:01 +00001855 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
danielk1977261919c2005-12-06 12:52:59 +00001856/*
danielk1977106bb232004-05-21 10:08:53 +00001857 if( rc ){
drhb86ccfb2003-01-28 23:13:10 +00001858 return TCL_ERROR;
1859 }
danielk1977261919c2005-12-06 12:52:59 +00001860*/
drhb86ccfb2003-01-28 23:13:10 +00001861 return TCL_OK;
1862}
1863
drh5a387052003-01-11 14:19:51 +00001864/*
drhd89bd002005-01-22 03:03:54 +00001865** Usage: sqlite3_expired STMT
1866**
1867** Return TRUE if a recompilation of the statement is recommended.
1868*/
1869static int test_expired(
1870 void * clientData,
1871 Tcl_Interp *interp,
1872 int objc,
1873 Tcl_Obj *CONST objv[]
1874){
1875 sqlite3_stmt *pStmt;
1876 if( objc!=2 ){
1877 Tcl_AppendResult(interp, "wrong # args: should be \"",
1878 Tcl_GetStringFromObj(objv[0], 0), " <STMT>", 0);
1879 return TCL_ERROR;
1880 }
1881 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
1882 Tcl_SetObjResult(interp, Tcl_NewBooleanObj(sqlite3_expired(pStmt)));
1883 return TCL_OK;
1884}
1885
1886/*
drhf8db1bc2005-04-22 02:38:37 +00001887** Usage: sqlite3_transfer_bindings FROMSTMT TOSTMT
1888**
1889** Transfer all bindings from FROMSTMT over to TOSTMT
1890*/
1891static int test_transfer_bind(
1892 void * clientData,
1893 Tcl_Interp *interp,
1894 int objc,
1895 Tcl_Obj *CONST objv[]
1896){
1897 sqlite3_stmt *pStmt1, *pStmt2;
1898 if( objc!=3 ){
1899 Tcl_AppendResult(interp, "wrong # args: should be \"",
1900 Tcl_GetStringFromObj(objv[0], 0), " FROM-STMT TO-STMT", 0);
1901 return TCL_ERROR;
1902 }
1903 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt1)) return TCL_ERROR;
1904 if( getStmtPointer(interp, Tcl_GetString(objv[2]), &pStmt2)) return TCL_ERROR;
1905 Tcl_SetObjResult(interp,
1906 Tcl_NewIntObj(sqlite3_transfer_bindings(pStmt1,pStmt2)));
1907 return TCL_OK;
1908}
1909
1910/*
danielk1977fbcd5852004-06-15 02:44:18 +00001911** Usage: sqlite3_changes DB
drh50457892003-09-06 01:10:47 +00001912**
danielk1977fbcd5852004-06-15 02:44:18 +00001913** Return the number of changes made to the database by the last SQL
1914** execution.
drh50457892003-09-06 01:10:47 +00001915*/
danielk1977fbcd5852004-06-15 02:44:18 +00001916static int test_changes(
1917 void * clientData,
1918 Tcl_Interp *interp,
1919 int objc,
1920 Tcl_Obj *CONST objv[]
1921){
1922 sqlite3 *db;
1923 if( objc!=2 ){
1924 Tcl_AppendResult(interp, "wrong # args: should be \"",
1925 Tcl_GetString(objv[0]), " DB", 0);
1926 return TCL_ERROR;
1927 }
1928 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
1929 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_changes(db)));
1930 return TCL_OK;
1931}
drh50457892003-09-06 01:10:47 +00001932
1933/*
drh7c972de2003-09-06 22:18:07 +00001934** This is the "static_bind_value" that variables are bound to when
danielk19776f8a5032004-05-10 10:34:51 +00001935** the FLAG option of sqlite3_bind is "static"
drh50457892003-09-06 01:10:47 +00001936*/
drh7c972de2003-09-06 22:18:07 +00001937static char *sqlite_static_bind_value = 0;
drhf0313812006-09-04 15:53:53 +00001938static int sqlite_static_bind_nbyte = 0;
drh7c972de2003-09-06 22:18:07 +00001939
1940/*
danielk19776f8a5032004-05-10 10:34:51 +00001941** Usage: sqlite3_bind VM IDX VALUE FLAGS
drh7c972de2003-09-06 22:18:07 +00001942**
1943** Sets the value of the IDX-th occurance of "?" in the original SQL
1944** string. VALUE is the new value. If FLAGS=="null" then VALUE is
1945** ignored and the value is set to NULL. If FLAGS=="static" then
1946** the value is set to the value of a static variable named
1947** "sqlite_static_bind_value". If FLAGS=="normal" then a copy
drhbf8aa2a2005-12-02 02:44:05 +00001948** of the VALUE is made. If FLAGS=="blob10" then a VALUE is ignored
1949** an a 10-byte blob "abc\000xyz\000pq" is inserted.
drh7c972de2003-09-06 22:18:07 +00001950*/
1951static int test_bind(
drh50457892003-09-06 01:10:47 +00001952 void *NotUsed,
1953 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1954 int argc, /* Number of arguments */
1955 char **argv /* Text of each argument */
1956){
danielk1977fc57d7b2004-05-26 02:04:57 +00001957 sqlite3_stmt *pStmt;
drh50457892003-09-06 01:10:47 +00001958 int rc;
drh7c972de2003-09-06 22:18:07 +00001959 int idx;
1960 if( argc!=5 ){
drh50457892003-09-06 01:10:47 +00001961 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
drh7c972de2003-09-06 22:18:07 +00001962 " VM IDX VALUE (null|static|normal)\"", 0);
drh50457892003-09-06 01:10:47 +00001963 return TCL_ERROR;
1964 }
danielk1977fc57d7b2004-05-26 02:04:57 +00001965 if( getStmtPointer(interp, argv[1], &pStmt) ) return TCL_ERROR;
drh7c972de2003-09-06 22:18:07 +00001966 if( Tcl_GetInt(interp, argv[2], &idx) ) return TCL_ERROR;
1967 if( strcmp(argv[4],"null")==0 ){
danielk1977fc57d7b2004-05-26 02:04:57 +00001968 rc = sqlite3_bind_null(pStmt, idx);
drh7c972de2003-09-06 22:18:07 +00001969 }else if( strcmp(argv[4],"static")==0 ){
danielk1977fc57d7b2004-05-26 02:04:57 +00001970 rc = sqlite3_bind_text(pStmt, idx, sqlite_static_bind_value, -1, 0);
drhf0313812006-09-04 15:53:53 +00001971 }else if( strcmp(argv[4],"static-nbytes")==0 ){
1972 rc = sqlite3_bind_text(pStmt, idx, sqlite_static_bind_value,
1973 sqlite_static_bind_nbyte, 0);
drh7c972de2003-09-06 22:18:07 +00001974 }else if( strcmp(argv[4],"normal")==0 ){
danielk1977d8123362004-06-12 09:25:12 +00001975 rc = sqlite3_bind_text(pStmt, idx, argv[3], -1, SQLITE_TRANSIENT);
drhbf8aa2a2005-12-02 02:44:05 +00001976 }else if( strcmp(argv[4],"blob10")==0 ){
1977 rc = sqlite3_bind_text(pStmt, idx, "abc\000xyz\000pq", 10, SQLITE_STATIC);
drh7c972de2003-09-06 22:18:07 +00001978 }else{
1979 Tcl_AppendResult(interp, "4th argument should be "
1980 "\"null\" or \"static\" or \"normal\"", 0);
1981 return TCL_ERROR;
1982 }
drhc60d0442004-09-30 13:43:13 +00001983 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
drh50457892003-09-06 01:10:47 +00001984 if( rc ){
1985 char zBuf[50];
1986 sprintf(zBuf, "(%d) ", rc);
danielk1977f20b21c2004-05-31 23:56:42 +00001987 Tcl_AppendResult(interp, zBuf, sqlite3ErrStr(rc), 0);
drh50457892003-09-06 01:10:47 +00001988 return TCL_ERROR;
1989 }
1990 return TCL_OK;
1991}
1992
drh5436dc22004-11-14 04:04:17 +00001993#ifndef SQLITE_OMIT_UTF16
danielk19774e6af132004-06-10 14:01:08 +00001994/*
1995** Usage: add_test_collate <db ptr> <utf8> <utf16le> <utf16be>
1996**
1997** This function is used to test that SQLite selects the correct collation
1998** sequence callback when multiple versions (for different text encodings)
1999** are available.
2000**
2001** Calling this routine registers the collation sequence "test_collate"
2002** with database handle <db>. The second argument must be a list of three
2003** boolean values. If the first is true, then a version of test_collate is
2004** registered for UTF-8, if the second is true, a version is registered for
2005** UTF-16le, if the third is true, a UTF-16be version is available.
2006** Previous versions of test_collate are deleted.
2007**
2008** The collation sequence test_collate is implemented by calling the
2009** following TCL script:
2010**
2011** "test_collate <enc> <lhs> <rhs>"
2012**
2013** The <lhs> and <rhs> are the two values being compared, encoded in UTF-8.
2014** The <enc> parameter is the encoding of the collation function that
2015** SQLite selected to call. The TCL test script implements the
2016** "test_collate" proc.
2017**
2018** Note that this will only work with one intepreter at a time, as the
2019** interp pointer to use when evaluating the TCL script is stored in
2020** pTestCollateInterp.
2021*/
2022static Tcl_Interp* pTestCollateInterp;
2023static int test_collate_func(
2024 void *pCtx,
2025 int nA, const void *zA,
2026 int nB, const void *zB
2027){
2028 Tcl_Interp *i = pTestCollateInterp;
2029 int encin = (int)pCtx;
2030 int res;
drh4db38a72005-09-01 12:16:28 +00002031 int n;
danielk19774e6af132004-06-10 14:01:08 +00002032
2033 sqlite3_value *pVal;
2034 Tcl_Obj *pX;
2035
2036 pX = Tcl_NewStringObj("test_collate", -1);
2037 Tcl_IncrRefCount(pX);
2038
2039 switch( encin ){
2040 case SQLITE_UTF8:
2041 Tcl_ListObjAppendElement(i,pX,Tcl_NewStringObj("UTF-8",-1));
2042 break;
2043 case SQLITE_UTF16LE:
2044 Tcl_ListObjAppendElement(i,pX,Tcl_NewStringObj("UTF-16LE",-1));
2045 break;
2046 case SQLITE_UTF16BE:
2047 Tcl_ListObjAppendElement(i,pX,Tcl_NewStringObj("UTF-16BE",-1));
2048 break;
2049 default:
2050 assert(0);
2051 }
2052
danielk19771e536952007-08-16 10:09:01 +00002053 pVal = sqlite3ValueNew(0);
drhb21c8cd2007-08-21 19:33:56 +00002054 sqlite3ValueSetStr(pVal, nA, zA, encin, SQLITE_STATIC);
drh4db38a72005-09-01 12:16:28 +00002055 n = sqlite3_value_bytes(pVal);
drh03d847e2005-12-09 20:21:58 +00002056 Tcl_ListObjAppendElement(i,pX,
2057 Tcl_NewStringObj((char*)sqlite3_value_text(pVal),n));
drhb21c8cd2007-08-21 19:33:56 +00002058 sqlite3ValueSetStr(pVal, nB, zB, encin, SQLITE_STATIC);
drh4db38a72005-09-01 12:16:28 +00002059 n = sqlite3_value_bytes(pVal);
drh03d847e2005-12-09 20:21:58 +00002060 Tcl_ListObjAppendElement(i,pX,
2061 Tcl_NewStringObj((char*)sqlite3_value_text(pVal),n));
danielk19774e6af132004-06-10 14:01:08 +00002062 sqlite3ValueFree(pVal);
2063
2064 Tcl_EvalObjEx(i, pX, 0);
2065 Tcl_DecrRefCount(pX);
2066 Tcl_GetIntFromObj(i, Tcl_GetObjResult(i), &res);
2067 return res;
2068}
2069static int test_collate(
2070 void * clientData,
2071 Tcl_Interp *interp,
2072 int objc,
2073 Tcl_Obj *CONST objv[]
2074){
2075 sqlite3 *db;
2076 int val;
danielk1977312d6b32004-06-29 13:18:23 +00002077 sqlite3_value *pVal;
drhc60d0442004-09-30 13:43:13 +00002078 int rc;
danielk19774e6af132004-06-10 14:01:08 +00002079
2080 if( objc!=5 ) goto bad_args;
2081 pTestCollateInterp = interp;
2082 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
2083
2084 if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[2], &val) ) return TCL_ERROR;
drhc60d0442004-09-30 13:43:13 +00002085 rc = sqlite3_create_collation(db, "test_collate", SQLITE_UTF8,
2086 (void *)SQLITE_UTF8, val?test_collate_func:0);
2087 if( rc==SQLITE_OK ){
2088 if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[3], &val) ) return TCL_ERROR;
2089 rc = sqlite3_create_collation(db, "test_collate", SQLITE_UTF16LE,
2090 (void *)SQLITE_UTF16LE, val?test_collate_func:0);
2091 if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[4], &val) ) return TCL_ERROR;
danielk1977312d6b32004-06-29 13:18:23 +00002092
drh86f8c192007-08-22 00:39:19 +00002093#if 0
danielk19779a30cf62006-01-18 04:26:07 +00002094 if( sqlite3_iMallocFail>0 ){
2095 sqlite3_iMallocFail++;
2096 }
2097#endif
danielk19771e536952007-08-16 10:09:01 +00002098 pVal = sqlite3ValueNew(0);
drhb21c8cd2007-08-21 19:33:56 +00002099 sqlite3ValueSetStr(pVal, -1, "test_collate", SQLITE_UTF8, SQLITE_STATIC);
danielk19779a30cf62006-01-18 04:26:07 +00002100 rc = sqlite3_create_collation16(db,
drhb21c8cd2007-08-21 19:33:56 +00002101 sqlite3ValueText(pVal, SQLITE_UTF16NATIVE), SQLITE_UTF16BE,
danielk19779a30cf62006-01-18 04:26:07 +00002102 (void *)SQLITE_UTF16BE, val?test_collate_func:0);
drhc60d0442004-09-30 13:43:13 +00002103 sqlite3ValueFree(pVal);
2104 }
2105 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
danielk19779a30cf62006-01-18 04:26:07 +00002106
2107 if( rc!=SQLITE_OK ){
2108 Tcl_AppendResult(interp, sqlite3TestErrorName(rc), 0);
2109 return TCL_ERROR;
2110 }
danielk19774e6af132004-06-10 14:01:08 +00002111 return TCL_OK;
2112
2113bad_args:
2114 Tcl_AppendResult(interp, "wrong # args: should be \"",
2115 Tcl_GetStringFromObj(objv[0], 0), " <DB> <utf8> <utf16le> <utf16be>", 0);
2116 return TCL_ERROR;
2117}
2118
drh268803a2005-12-14 20:11:30 +00002119/*
2120** When the collation needed callback is invoked, record the name of
2121** the requested collating function here. The recorded name is linked
2122** to a TCL variable and used to make sure that the requested collation
2123** name is correct.
2124*/
2125static char zNeededCollation[200];
2126static char *pzNeededCollation = zNeededCollation;
2127
2128
2129/*
2130** Called when a collating sequence is needed. Registered using
2131** sqlite3_collation_needed16().
2132*/
danielk1977312d6b32004-06-29 13:18:23 +00002133static void test_collate_needed_cb(
2134 void *pCtx,
2135 sqlite3 *db,
2136 int eTextRep,
drh268803a2005-12-14 20:11:30 +00002137 const void *pName
danielk1977312d6b32004-06-29 13:18:23 +00002138){
danielk197714db2662006-01-09 16:12:04 +00002139 int enc = ENC(db);
drh268803a2005-12-14 20:11:30 +00002140 int i;
2141 char *z;
2142 for(z = (char*)pName, i=0; *z || z[1]; z++){
2143 if( *z ) zNeededCollation[i++] = *z;
2144 }
2145 zNeededCollation[i] = 0;
danielk1977312d6b32004-06-29 13:18:23 +00002146 sqlite3_create_collation(
danielk197714db2662006-01-09 16:12:04 +00002147 db, "test_collate", ENC(db), (void *)enc, test_collate_func);
danielk1977312d6b32004-06-29 13:18:23 +00002148}
2149
2150/*
2151** Usage: add_test_collate_needed DB
2152*/
2153static int test_collate_needed(
2154 void * clientData,
2155 Tcl_Interp *interp,
2156 int objc,
2157 Tcl_Obj *CONST objv[]
2158){
2159 sqlite3 *db;
drhc60d0442004-09-30 13:43:13 +00002160 int rc;
danielk1977312d6b32004-06-29 13:18:23 +00002161
2162 if( objc!=2 ) goto bad_args;
2163 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
drhc60d0442004-09-30 13:43:13 +00002164 rc = sqlite3_collation_needed16(db, 0, test_collate_needed_cb);
drh268803a2005-12-14 20:11:30 +00002165 zNeededCollation[0] = 0;
drhc60d0442004-09-30 13:43:13 +00002166 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
danielk1977312d6b32004-06-29 13:18:23 +00002167 return TCL_OK;
2168
2169bad_args:
2170 Tcl_WrongNumArgs(interp, 1, objv, "DB");
2171 return TCL_ERROR;
2172}
drh7d9bd4e2006-02-16 18:16:36 +00002173
2174/*
2175** tclcmd: add_alignment_test_collations DB
2176**
2177** Add two new collating sequences to the database DB
2178**
2179** utf16_aligned
2180** utf16_unaligned
2181**
2182** Both collating sequences use the same sort order as BINARY.
2183** The only difference is that the utf16_aligned collating
2184** sequence is declared with the SQLITE_UTF16_ALIGNED flag.
2185** Both collating functions increment the unaligned utf16 counter
2186** whenever they see a string that begins on an odd byte boundary.
2187*/
2188static int unaligned_string_counter = 0;
2189static int alignmentCollFunc(
2190 void *NotUsed,
2191 int nKey1, const void *pKey1,
2192 int nKey2, const void *pKey2
2193){
2194 int rc, n;
2195 n = nKey1<nKey2 ? nKey1 : nKey2;
2196 if( nKey1>0 && 1==(1&(int)pKey1) ) unaligned_string_counter++;
2197 if( nKey2>0 && 1==(1&(int)pKey2) ) unaligned_string_counter++;
2198 rc = memcmp(pKey1, pKey2, n);
2199 if( rc==0 ){
2200 rc = nKey1 - nKey2;
2201 }
2202 return rc;
2203}
2204static int add_alignment_test_collations(
2205 void * clientData,
2206 Tcl_Interp *interp,
2207 int objc,
2208 Tcl_Obj *CONST objv[]
2209){
2210 sqlite3 *db;
2211 if( objc>=2 ){
2212 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
2213 sqlite3_create_collation(db, "utf16_unaligned",
2214 SQLITE_UTF16,
2215 0, alignmentCollFunc);
2216 sqlite3_create_collation(db, "utf16_aligned",
2217 SQLITE_UTF16 | SQLITE_UTF16_ALIGNED,
2218 0, alignmentCollFunc);
2219 }
2220 return SQLITE_OK;
2221}
2222#endif /* !defined(SQLITE_OMIT_UTF16) */
danielk1977312d6b32004-06-29 13:18:23 +00002223
danielk1977c8e9a2d2004-06-25 12:08:46 +00002224/*
2225** Usage: add_test_function <db ptr> <utf8> <utf16le> <utf16be>
2226**
2227** This function is used to test that SQLite selects the correct user
2228** function callback when multiple versions (for different text encodings)
2229** are available.
2230**
2231** Calling this routine registers up to three versions of the user function
2232** "test_function" with database handle <db>. If the second argument is
2233** true, then a version of test_function is registered for UTF-8, if the
2234** third is true, a version is registered for UTF-16le, if the fourth is
2235** true, a UTF-16be version is available. Previous versions of
2236** test_function are deleted.
2237**
2238** The user function is implemented by calling the following TCL script:
2239**
2240** "test_function <enc> <arg>"
2241**
2242** Where <enc> is one of UTF-8, UTF-16LE or UTF16BE, and <arg> is the
2243** single argument passed to the SQL function. The value returned by
2244** the TCL script is used as the return value of the SQL function. It
2245** is passed to SQLite using UTF-16BE for a UTF-8 test_function(), UTF-8
2246** for a UTF-16LE test_function(), and UTF-16LE for an implementation that
2247** prefers UTF-16BE.
2248*/
drh5436dc22004-11-14 04:04:17 +00002249#ifndef SQLITE_OMIT_UTF16
danielk1977c8e9a2d2004-06-25 12:08:46 +00002250static void test_function_utf8(
2251 sqlite3_context *pCtx,
2252 int nArg,
2253 sqlite3_value **argv
2254){
2255 Tcl_Interp *interp;
2256 Tcl_Obj *pX;
2257 sqlite3_value *pVal;
2258 interp = (Tcl_Interp *)sqlite3_user_data(pCtx);
2259 pX = Tcl_NewStringObj("test_function", -1);
2260 Tcl_IncrRefCount(pX);
2261 Tcl_ListObjAppendElement(interp, pX, Tcl_NewStringObj("UTF-8", -1));
2262 Tcl_ListObjAppendElement(interp, pX,
drh03d847e2005-12-09 20:21:58 +00002263 Tcl_NewStringObj((char*)sqlite3_value_text(argv[0]), -1));
danielk1977c8e9a2d2004-06-25 12:08:46 +00002264 Tcl_EvalObjEx(interp, pX, 0);
2265 Tcl_DecrRefCount(pX);
2266 sqlite3_result_text(pCtx, Tcl_GetStringResult(interp), -1, SQLITE_TRANSIENT);
danielk19771e536952007-08-16 10:09:01 +00002267 pVal = sqlite3ValueNew(0);
drhb21c8cd2007-08-21 19:33:56 +00002268 sqlite3ValueSetStr(pVal, -1, Tcl_GetStringResult(interp),
danielk1977c8e9a2d2004-06-25 12:08:46 +00002269 SQLITE_UTF8, SQLITE_STATIC);
2270 sqlite3_result_text16be(pCtx, sqlite3_value_text16be(pVal),
2271 -1, SQLITE_TRANSIENT);
2272 sqlite3ValueFree(pVal);
2273}
2274static void test_function_utf16le(
2275 sqlite3_context *pCtx,
2276 int nArg,
2277 sqlite3_value **argv
2278){
2279 Tcl_Interp *interp;
2280 Tcl_Obj *pX;
2281 sqlite3_value *pVal;
2282 interp = (Tcl_Interp *)sqlite3_user_data(pCtx);
2283 pX = Tcl_NewStringObj("test_function", -1);
2284 Tcl_IncrRefCount(pX);
2285 Tcl_ListObjAppendElement(interp, pX, Tcl_NewStringObj("UTF-16LE", -1));
2286 Tcl_ListObjAppendElement(interp, pX,
drh03d847e2005-12-09 20:21:58 +00002287 Tcl_NewStringObj((char*)sqlite3_value_text(argv[0]), -1));
danielk1977c8e9a2d2004-06-25 12:08:46 +00002288 Tcl_EvalObjEx(interp, pX, 0);
2289 Tcl_DecrRefCount(pX);
danielk19771e536952007-08-16 10:09:01 +00002290 pVal = sqlite3ValueNew(0);
drhb21c8cd2007-08-21 19:33:56 +00002291 sqlite3ValueSetStr(pVal, -1, Tcl_GetStringResult(interp),
danielk1977c8e9a2d2004-06-25 12:08:46 +00002292 SQLITE_UTF8, SQLITE_STATIC);
drh03d847e2005-12-09 20:21:58 +00002293 sqlite3_result_text(pCtx,(char*)sqlite3_value_text(pVal),-1,SQLITE_TRANSIENT);
danielk1977c8e9a2d2004-06-25 12:08:46 +00002294 sqlite3ValueFree(pVal);
2295}
2296static void test_function_utf16be(
2297 sqlite3_context *pCtx,
2298 int nArg,
2299 sqlite3_value **argv
2300){
2301 Tcl_Interp *interp;
2302 Tcl_Obj *pX;
2303 sqlite3_value *pVal;
2304 interp = (Tcl_Interp *)sqlite3_user_data(pCtx);
2305 pX = Tcl_NewStringObj("test_function", -1);
2306 Tcl_IncrRefCount(pX);
2307 Tcl_ListObjAppendElement(interp, pX, Tcl_NewStringObj("UTF-16BE", -1));
2308 Tcl_ListObjAppendElement(interp, pX,
drh03d847e2005-12-09 20:21:58 +00002309 Tcl_NewStringObj((char*)sqlite3_value_text(argv[0]), -1));
danielk1977c8e9a2d2004-06-25 12:08:46 +00002310 Tcl_EvalObjEx(interp, pX, 0);
2311 Tcl_DecrRefCount(pX);
danielk19771e536952007-08-16 10:09:01 +00002312 pVal = sqlite3ValueNew(0);
drhb21c8cd2007-08-21 19:33:56 +00002313 sqlite3ValueSetStr(pVal, -1, Tcl_GetStringResult(interp),
danielk1977c8e9a2d2004-06-25 12:08:46 +00002314 SQLITE_UTF8, SQLITE_STATIC);
2315 sqlite3_result_text16le(pCtx, sqlite3_value_text16le(pVal),
2316 -1, SQLITE_TRANSIENT);
2317 sqlite3ValueFree(pVal);
2318}
drh5436dc22004-11-14 04:04:17 +00002319#endif /* SQLITE_OMIT_UTF16 */
danielk1977c8e9a2d2004-06-25 12:08:46 +00002320static int test_function(
2321 void * clientData,
2322 Tcl_Interp *interp,
2323 int objc,
2324 Tcl_Obj *CONST objv[]
2325){
drh5436dc22004-11-14 04:04:17 +00002326#ifndef SQLITE_OMIT_UTF16
danielk1977c8e9a2d2004-06-25 12:08:46 +00002327 sqlite3 *db;
2328 int val;
2329
2330 if( objc!=5 ) goto bad_args;
2331 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
2332
2333 if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[2], &val) ) return TCL_ERROR;
2334 if( val ){
2335 sqlite3_create_function(db, "test_function", 1, SQLITE_UTF8,
2336 interp, test_function_utf8, 0, 0);
2337 }
2338 if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[3], &val) ) return TCL_ERROR;
2339 if( val ){
2340 sqlite3_create_function(db, "test_function", 1, SQLITE_UTF16LE,
2341 interp, test_function_utf16le, 0, 0);
2342 }
2343 if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[4], &val) ) return TCL_ERROR;
2344 if( val ){
2345 sqlite3_create_function(db, "test_function", 1, SQLITE_UTF16BE,
2346 interp, test_function_utf16be, 0, 0);
2347 }
2348
2349 return TCL_OK;
2350bad_args:
2351 Tcl_AppendResult(interp, "wrong # args: should be \"",
2352 Tcl_GetStringFromObj(objv[0], 0), " <DB> <utf8> <utf16le> <utf16be>", 0);
drh5436dc22004-11-14 04:04:17 +00002353#endif /* SQLITE_OMIT_UTF16 */
danielk1977c8e9a2d2004-06-25 12:08:46 +00002354 return TCL_ERROR;
2355}
2356
danielk1977312d6b32004-06-29 13:18:23 +00002357/*
2358** Usage: test_errstr <err code>
2359**
2360** Test that the english language string equivalents for sqlite error codes
2361** are sane. The parameter is an integer representing an sqlite error code.
2362** The result is a list of two elements, the string representation of the
2363** error code and the english language explanation.
2364*/
2365static int test_errstr(
2366 void * clientData,
2367 Tcl_Interp *interp,
2368 int objc,
2369 Tcl_Obj *CONST objv[]
2370){
2371 char *zCode;
2372 int i;
2373 if( objc!=1 ){
2374 Tcl_WrongNumArgs(interp, 1, objv, "<error code>");
2375 }
2376
2377 zCode = Tcl_GetString(objv[1]);
2378 for(i=0; i<200; i++){
drh4f0c5872007-03-26 22:05:01 +00002379 if( 0==strcmp(t1ErrorName(i), zCode) ) break;
danielk1977312d6b32004-06-29 13:18:23 +00002380 }
2381 Tcl_SetResult(interp, (char *)sqlite3ErrStr(i), 0);
2382 return TCL_OK;
2383}
2384
drh50457892003-09-06 01:10:47 +00002385/*
drh99ee3602003-02-16 19:13:36 +00002386** Usage: breakpoint
2387**
2388** This routine exists for one purpose - to provide a place to put a
2389** breakpoint with GDB that can be triggered using TCL code. The use
2390** for this is when a particular test fails on (say) the 1485th iteration.
2391** In the TCL test script, we can add code like this:
2392**
2393** if {$i==1485} breakpoint
2394**
2395** Then run testfixture in the debugger and wait for the breakpoint to
2396** fire. Then additional breakpoints can be set to trace down the bug.
2397*/
2398static int test_breakpoint(
2399 void *NotUsed,
2400 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
2401 int argc, /* Number of arguments */
2402 char **argv /* Text of each argument */
2403){
2404 return TCL_OK; /* Do nothing */
2405}
2406
drh241db312004-06-22 12:46:53 +00002407/*
drhb026e052007-05-02 01:34:31 +00002408** Usage: sqlite3_bind_zeroblob STMT IDX N
2409**
2410** Test the sqlite3_bind_zeroblob interface. STMT is a prepared statement.
2411** IDX is the index of a wildcard in the prepared statement. This command
2412** binds a N-byte zero-filled BLOB to the wildcard.
2413*/
2414static int test_bind_zeroblob(
2415 void * clientData,
2416 Tcl_Interp *interp,
2417 int objc,
2418 Tcl_Obj *CONST objv[]
2419){
2420 sqlite3_stmt *pStmt;
2421 int idx;
2422 int n;
2423 int rc;
2424
2425 if( objc!=4 ){
2426 Tcl_AppendResult(interp, "wrong # args: should be \"",
2427 Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE", 0);
2428 return TCL_ERROR;
2429 }
2430
2431 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2432 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
2433 if( Tcl_GetIntFromObj(interp, objv[3], &n) ) return TCL_ERROR;
2434
2435 rc = sqlite3_bind_zeroblob(pStmt, idx, n);
2436 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
2437 if( rc!=SQLITE_OK ){
2438 return TCL_ERROR;
2439 }
2440
2441 return TCL_OK;
2442}
2443
2444/*
drh241db312004-06-22 12:46:53 +00002445** Usage: sqlite3_bind_int STMT N VALUE
2446**
2447** Test the sqlite3_bind_int interface. STMT is a prepared statement.
2448** N is the index of a wildcard in the prepared statement. This command
2449** binds a 32-bit integer VALUE to that wildcard.
2450*/
2451static int test_bind_int(
danielk197751e3d8e2004-05-20 01:12:34 +00002452 void * clientData,
2453 Tcl_Interp *interp,
2454 int objc,
2455 Tcl_Obj *CONST objv[]
2456){
2457 sqlite3_stmt *pStmt;
2458 int idx;
2459 int value;
2460 int rc;
2461
2462 if( objc!=4 ){
2463 Tcl_AppendResult(interp, "wrong # args: should be \"",
drh241db312004-06-22 12:46:53 +00002464 Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE", 0);
danielk197751e3d8e2004-05-20 01:12:34 +00002465 return TCL_ERROR;
2466 }
2467
2468 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2469 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
2470 if( Tcl_GetIntFromObj(interp, objv[3], &value) ) return TCL_ERROR;
2471
danielk1977c572ef72004-05-27 09:28:41 +00002472 rc = sqlite3_bind_int(pStmt, idx, value);
drhc60d0442004-09-30 13:43:13 +00002473 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
danielk197751e3d8e2004-05-20 01:12:34 +00002474 if( rc!=SQLITE_OK ){
2475 return TCL_ERROR;
2476 }
2477
2478 return TCL_OK;
2479}
2480
drh241db312004-06-22 12:46:53 +00002481
2482/*
2483** Usage: sqlite3_bind_int64 STMT N VALUE
2484**
2485** Test the sqlite3_bind_int64 interface. STMT is a prepared statement.
2486** N is the index of a wildcard in the prepared statement. This command
2487** binds a 64-bit integer VALUE to that wildcard.
2488*/
danielk197751e3d8e2004-05-20 01:12:34 +00002489static int test_bind_int64(
2490 void * clientData,
2491 Tcl_Interp *interp,
2492 int objc,
2493 Tcl_Obj *CONST objv[]
2494){
2495 sqlite3_stmt *pStmt;
2496 int idx;
2497 i64 value;
2498 int rc;
2499
2500 if( objc!=4 ){
2501 Tcl_AppendResult(interp, "wrong # args: should be \"",
drh241db312004-06-22 12:46:53 +00002502 Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE", 0);
danielk197751e3d8e2004-05-20 01:12:34 +00002503 return TCL_ERROR;
2504 }
2505
2506 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2507 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
2508 if( Tcl_GetWideIntFromObj(interp, objv[3], &value) ) return TCL_ERROR;
2509
2510 rc = sqlite3_bind_int64(pStmt, idx, value);
drhc60d0442004-09-30 13:43:13 +00002511 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
danielk197751e3d8e2004-05-20 01:12:34 +00002512 if( rc!=SQLITE_OK ){
2513 return TCL_ERROR;
2514 }
2515
2516 return TCL_OK;
2517}
2518
drh241db312004-06-22 12:46:53 +00002519
2520/*
2521** Usage: sqlite3_bind_double STMT N VALUE
2522**
2523** Test the sqlite3_bind_double interface. STMT is a prepared statement.
2524** N is the index of a wildcard in the prepared statement. This command
2525** binds a 64-bit integer VALUE to that wildcard.
2526*/
danielk197751e3d8e2004-05-20 01:12:34 +00002527static int test_bind_double(
2528 void * clientData,
2529 Tcl_Interp *interp,
2530 int objc,
2531 Tcl_Obj *CONST objv[]
2532){
2533 sqlite3_stmt *pStmt;
2534 int idx;
2535 double value;
2536 int rc;
2537
2538 if( objc!=4 ){
2539 Tcl_AppendResult(interp, "wrong # args: should be \"",
drh241db312004-06-22 12:46:53 +00002540 Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE", 0);
danielk197751e3d8e2004-05-20 01:12:34 +00002541 return TCL_ERROR;
2542 }
2543
2544 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2545 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
2546 if( Tcl_GetDoubleFromObj(interp, objv[3], &value) ) return TCL_ERROR;
2547
2548 rc = sqlite3_bind_double(pStmt, idx, value);
drhc60d0442004-09-30 13:43:13 +00002549 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
danielk197751e3d8e2004-05-20 01:12:34 +00002550 if( rc!=SQLITE_OK ){
2551 return TCL_ERROR;
2552 }
2553
2554 return TCL_OK;
2555}
2556
drh241db312004-06-22 12:46:53 +00002557/*
2558** Usage: sqlite3_bind_null STMT N
2559**
2560** Test the sqlite3_bind_null interface. STMT is a prepared statement.
2561** N is the index of a wildcard in the prepared statement. This command
2562** binds a NULL to the wildcard.
2563*/
danielk197751e3d8e2004-05-20 01:12:34 +00002564static int test_bind_null(
2565 void * clientData,
2566 Tcl_Interp *interp,
2567 int objc,
2568 Tcl_Obj *CONST objv[]
2569){
2570 sqlite3_stmt *pStmt;
2571 int idx;
2572 int rc;
2573
2574 if( objc!=3 ){
2575 Tcl_AppendResult(interp, "wrong # args: should be \"",
drh241db312004-06-22 12:46:53 +00002576 Tcl_GetStringFromObj(objv[0], 0), " STMT N", 0);
danielk197751e3d8e2004-05-20 01:12:34 +00002577 return TCL_ERROR;
2578 }
2579
2580 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2581 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
2582
2583 rc = sqlite3_bind_null(pStmt, idx);
drhc60d0442004-09-30 13:43:13 +00002584 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
danielk197751e3d8e2004-05-20 01:12:34 +00002585 if( rc!=SQLITE_OK ){
2586 return TCL_ERROR;
2587 }
2588
2589 return TCL_OK;
2590}
2591
drh241db312004-06-22 12:46:53 +00002592/*
2593** Usage: sqlite3_bind_text STMT N STRING BYTES
2594**
2595** Test the sqlite3_bind_text interface. STMT is a prepared statement.
2596** N is the index of a wildcard in the prepared statement. This command
2597** binds a UTF-8 string STRING to the wildcard. The string is BYTES bytes
2598** long.
2599*/
danielk197751e3d8e2004-05-20 01:12:34 +00002600static int test_bind_text(
2601 void * clientData,
2602 Tcl_Interp *interp,
2603 int objc,
2604 Tcl_Obj *CONST objv[]
2605){
2606 sqlite3_stmt *pStmt;
2607 int idx;
2608 int bytes;
2609 char *value;
2610 int rc;
2611
2612 if( objc!=5 ){
2613 Tcl_AppendResult(interp, "wrong # args: should be \"",
drh241db312004-06-22 12:46:53 +00002614 Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE BYTES", 0);
danielk197751e3d8e2004-05-20 01:12:34 +00002615 return TCL_ERROR;
2616 }
2617
2618 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2619 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
2620 value = Tcl_GetString(objv[3]);
2621 if( Tcl_GetIntFromObj(interp, objv[4], &bytes) ) return TCL_ERROR;
2622
danielk1977d8123362004-06-12 09:25:12 +00002623 rc = sqlite3_bind_text(pStmt, idx, value, bytes, SQLITE_TRANSIENT);
drhc60d0442004-09-30 13:43:13 +00002624 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
danielk197751e3d8e2004-05-20 01:12:34 +00002625 if( rc!=SQLITE_OK ){
drh473d1792005-06-06 17:54:55 +00002626 Tcl_AppendResult(interp, sqlite3TestErrorName(rc), 0);
danielk197751e3d8e2004-05-20 01:12:34 +00002627 return TCL_ERROR;
2628 }
2629
2630 return TCL_OK;
2631}
2632
drh241db312004-06-22 12:46:53 +00002633/*
danielk1977161fb792006-01-24 10:58:21 +00002634** Usage: sqlite3_bind_text16 ?-static? STMT N STRING BYTES
drh241db312004-06-22 12:46:53 +00002635**
2636** Test the sqlite3_bind_text16 interface. STMT is a prepared statement.
2637** N is the index of a wildcard in the prepared statement. This command
2638** binds a UTF-16 string STRING to the wildcard. The string is BYTES bytes
2639** long.
2640*/
danielk197751e3d8e2004-05-20 01:12:34 +00002641static int test_bind_text16(
2642 void * clientData,
2643 Tcl_Interp *interp,
2644 int objc,
2645 Tcl_Obj *CONST objv[]
2646){
drh5436dc22004-11-14 04:04:17 +00002647#ifndef SQLITE_OMIT_UTF16
danielk197751e3d8e2004-05-20 01:12:34 +00002648 sqlite3_stmt *pStmt;
2649 int idx;
2650 int bytes;
2651 char *value;
2652 int rc;
2653
danielk1977161fb792006-01-24 10:58:21 +00002654 void (*xDel)() = (objc==6?SQLITE_STATIC:SQLITE_TRANSIENT);
2655 Tcl_Obj *oStmt = objv[objc-4];
2656 Tcl_Obj *oN = objv[objc-3];
2657 Tcl_Obj *oString = objv[objc-2];
2658 Tcl_Obj *oBytes = objv[objc-1];
2659
2660 if( objc!=5 && objc!=6){
danielk197751e3d8e2004-05-20 01:12:34 +00002661 Tcl_AppendResult(interp, "wrong # args: should be \"",
drh241db312004-06-22 12:46:53 +00002662 Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE BYTES", 0);
danielk197751e3d8e2004-05-20 01:12:34 +00002663 return TCL_ERROR;
2664 }
2665
danielk1977161fb792006-01-24 10:58:21 +00002666 if( getStmtPointer(interp, Tcl_GetString(oStmt), &pStmt) ) return TCL_ERROR;
2667 if( Tcl_GetIntFromObj(interp, oN, &idx) ) return TCL_ERROR;
2668 value = (char*)Tcl_GetByteArrayFromObj(oString, 0);
2669 if( Tcl_GetIntFromObj(interp, oBytes, &bytes) ) return TCL_ERROR;
danielk197751e3d8e2004-05-20 01:12:34 +00002670
danielk1977161fb792006-01-24 10:58:21 +00002671 rc = sqlite3_bind_text16(pStmt, idx, (void *)value, bytes, xDel);
drhc60d0442004-09-30 13:43:13 +00002672 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
danielk197751e3d8e2004-05-20 01:12:34 +00002673 if( rc!=SQLITE_OK ){
2674 return TCL_ERROR;
2675 }
2676
drh5436dc22004-11-14 04:04:17 +00002677#endif /* SQLITE_OMIT_UTF16 */
danielk197751e3d8e2004-05-20 01:12:34 +00002678 return TCL_OK;
2679}
2680
drh241db312004-06-22 12:46:53 +00002681/*
danielk19775b159dc2007-05-17 16:34:43 +00002682** Usage: sqlite3_bind_blob ?-static? STMT N DATA BYTES
drh241db312004-06-22 12:46:53 +00002683**
2684** Test the sqlite3_bind_blob interface. STMT is a prepared statement.
2685** N is the index of a wildcard in the prepared statement. This command
2686** binds a BLOB to the wildcard. The BLOB is BYTES bytes in size.
2687*/
danielk197751e3d8e2004-05-20 01:12:34 +00002688static int test_bind_blob(
2689 void * clientData,
2690 Tcl_Interp *interp,
2691 int objc,
2692 Tcl_Obj *CONST objv[]
2693){
2694 sqlite3_stmt *pStmt;
2695 int idx;
2696 int bytes;
2697 char *value;
2698 int rc;
danielk19775b159dc2007-05-17 16:34:43 +00002699 sqlite3_destructor_type xDestructor = SQLITE_TRANSIENT;
danielk197751e3d8e2004-05-20 01:12:34 +00002700
danielk19775b159dc2007-05-17 16:34:43 +00002701 if( objc!=5 && objc!=6 ){
danielk197751e3d8e2004-05-20 01:12:34 +00002702 Tcl_AppendResult(interp, "wrong # args: should be \"",
drh241db312004-06-22 12:46:53 +00002703 Tcl_GetStringFromObj(objv[0], 0), " STMT N DATA BYTES", 0);
danielk197751e3d8e2004-05-20 01:12:34 +00002704 return TCL_ERROR;
2705 }
2706
danielk19775b159dc2007-05-17 16:34:43 +00002707 if( objc==6 ){
2708 xDestructor = SQLITE_STATIC;
2709 objv++;
2710 }
2711
danielk197751e3d8e2004-05-20 01:12:34 +00002712 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2713 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
2714 value = Tcl_GetString(objv[3]);
danielk197749e46432004-05-27 13:55:27 +00002715 if( Tcl_GetIntFromObj(interp, objv[4], &bytes) ) return TCL_ERROR;
danielk197751e3d8e2004-05-20 01:12:34 +00002716
danielk19775b159dc2007-05-17 16:34:43 +00002717 rc = sqlite3_bind_blob(pStmt, idx, value, bytes, xDestructor);
drhc60d0442004-09-30 13:43:13 +00002718 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
danielk197751e3d8e2004-05-20 01:12:34 +00002719 if( rc!=SQLITE_OK ){
2720 return TCL_ERROR;
2721 }
2722
2723 return TCL_OK;
2724}
2725
drh99ee3602003-02-16 19:13:36 +00002726/*
drh75f6a032004-07-15 14:15:00 +00002727** Usage: sqlite3_bind_parameter_count STMT
2728**
2729** Return the number of wildcards in the given statement.
2730*/
2731static int test_bind_parameter_count(
2732 void * clientData,
2733 Tcl_Interp *interp,
2734 int objc,
2735 Tcl_Obj *CONST objv[]
2736){
2737 sqlite3_stmt *pStmt;
2738
2739 if( objc!=2 ){
2740 Tcl_WrongNumArgs(interp, 1, objv, "STMT");
2741 return TCL_ERROR;
2742 }
2743 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2744 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_bind_parameter_count(pStmt)));
2745 return TCL_OK;
2746}
2747
2748/*
drh895d7472004-08-20 16:02:39 +00002749** Usage: sqlite3_bind_parameter_name STMT N
2750**
2751** Return the name of the Nth wildcard. The first wildcard is 1.
2752** An empty string is returned if N is out of range or if the wildcard
2753** is nameless.
2754*/
2755static int test_bind_parameter_name(
2756 void * clientData,
2757 Tcl_Interp *interp,
2758 int objc,
2759 Tcl_Obj *CONST objv[]
2760){
2761 sqlite3_stmt *pStmt;
2762 int i;
2763
2764 if( objc!=3 ){
2765 Tcl_WrongNumArgs(interp, 1, objv, "STMT N");
2766 return TCL_ERROR;
2767 }
2768 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2769 if( Tcl_GetIntFromObj(interp, objv[2], &i) ) return TCL_ERROR;
2770 Tcl_SetObjResult(interp,
2771 Tcl_NewStringObj(sqlite3_bind_parameter_name(pStmt,i),-1)
2772 );
2773 return TCL_OK;
2774}
2775
2776/*
drhfa6bc002004-09-07 16:19:52 +00002777** Usage: sqlite3_bind_parameter_index STMT NAME
2778**
2779** Return the index of the wildcard called NAME. Return 0 if there is
2780** no such wildcard.
2781*/
2782static int test_bind_parameter_index(
2783 void * clientData,
2784 Tcl_Interp *interp,
2785 int objc,
2786 Tcl_Obj *CONST objv[]
2787){
2788 sqlite3_stmt *pStmt;
2789
2790 if( objc!=3 ){
2791 Tcl_WrongNumArgs(interp, 1, objv, "STMT NAME");
2792 return TCL_ERROR;
2793 }
2794 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2795 Tcl_SetObjResult(interp,
2796 Tcl_NewIntObj(
2797 sqlite3_bind_parameter_index(pStmt,Tcl_GetString(objv[2]))
2798 )
2799 );
2800 return TCL_OK;
2801}
2802
2803/*
danielk1977600dd0b2005-01-20 01:14:23 +00002804** Usage: sqlite3_clear_bindings STMT
2805**
2806*/
danielk1977600dd0b2005-01-20 01:14:23 +00002807static int test_clear_bindings(
2808 void * clientData,
2809 Tcl_Interp *interp,
2810 int objc,
2811 Tcl_Obj *CONST objv[]
2812){
2813 sqlite3_stmt *pStmt;
2814
2815 if( objc!=2 ){
2816 Tcl_WrongNumArgs(interp, 1, objv, "STMT");
2817 return TCL_ERROR;
2818 }
2819 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2820 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_clear_bindings(pStmt)));
2821 return TCL_OK;
2822}
drhf9cb7f52006-06-27 20:06:44 +00002823
2824/*
2825** Usage: sqlite3_sleep MILLISECONDS
2826*/
2827static int test_sleep(
2828 void * clientData,
2829 Tcl_Interp *interp,
2830 int objc,
2831 Tcl_Obj *CONST objv[]
2832){
2833 int ms;
2834
2835 if( objc!=2 ){
2836 Tcl_WrongNumArgs(interp, 1, objv, "MILLISECONDS");
2837 return TCL_ERROR;
2838 }
2839 if( Tcl_GetIntFromObj(interp, objv[1], &ms) ){
2840 return TCL_ERROR;
2841 }
2842 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_sleep(ms)));
2843 return TCL_OK;
2844}
danielk1977600dd0b2005-01-20 01:14:23 +00002845
2846/*
danielk19776622cce2004-05-20 11:00:52 +00002847** Usage: sqlite3_errcode DB
2848**
2849** Return the string representation of the most recent sqlite3_* API
2850** error code. e.g. "SQLITE_ERROR".
2851*/
2852static int test_errcode(
2853 void * clientData,
2854 Tcl_Interp *interp,
2855 int objc,
2856 Tcl_Obj *CONST objv[]
2857){
2858 sqlite3 *db;
drh4ac285a2006-09-15 07:28:50 +00002859 int rc;
2860 char zBuf[30];
danielk19776622cce2004-05-20 11:00:52 +00002861
2862 if( objc!=2 ){
2863 Tcl_AppendResult(interp, "wrong # args: should be \"",
2864 Tcl_GetString(objv[0]), " DB", 0);
2865 return TCL_ERROR;
2866 }
2867 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
drh4ac285a2006-09-15 07:28:50 +00002868 rc = sqlite3_errcode(db);
2869 if( (rc&0xff)==rc ){
2870 zBuf[0] = 0;
2871 }else{
2872 sprintf(zBuf,"+%d", rc>>8);
2873 }
drh4f0c5872007-03-26 22:05:01 +00002874 Tcl_AppendResult(interp, (char *)t1ErrorName(rc), zBuf, 0);
danielk19776622cce2004-05-20 11:00:52 +00002875 return TCL_OK;
2876}
2877
2878/*
2879** Usage: test_errmsg DB
2880**
2881** Returns the UTF-8 representation of the error message string for the
2882** most recent sqlite3_* API call.
2883*/
2884static int test_errmsg(
2885 void * clientData,
2886 Tcl_Interp *interp,
2887 int objc,
2888 Tcl_Obj *CONST objv[]
2889){
drh9bb575f2004-09-06 17:24:11 +00002890 sqlite3 *db;
danielk19776622cce2004-05-20 11:00:52 +00002891 const char *zErr;
2892
2893 if( objc!=2 ){
2894 Tcl_AppendResult(interp, "wrong # args: should be \"",
2895 Tcl_GetString(objv[0]), " DB", 0);
2896 return TCL_ERROR;
2897 }
2898 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
2899
2900 zErr = sqlite3_errmsg(db);
2901 Tcl_SetObjResult(interp, Tcl_NewStringObj(zErr, -1));
2902 return TCL_OK;
2903}
2904
2905/*
2906** Usage: test_errmsg16 DB
2907**
2908** Returns the UTF-16 representation of the error message string for the
2909** most recent sqlite3_* API call. This is a byte array object at the TCL
2910** level, and it includes the 0x00 0x00 terminator bytes at the end of the
2911** UTF-16 string.
2912*/
2913static int test_errmsg16(
2914 void * clientData,
2915 Tcl_Interp *interp,
2916 int objc,
2917 Tcl_Obj *CONST objv[]
2918){
drh5436dc22004-11-14 04:04:17 +00002919#ifndef SQLITE_OMIT_UTF16
drh9bb575f2004-09-06 17:24:11 +00002920 sqlite3 *db;
danielk19776622cce2004-05-20 11:00:52 +00002921 const void *zErr;
danielk1977950f0542006-01-18 05:51:57 +00002922 int bytes = 0;
danielk19776622cce2004-05-20 11:00:52 +00002923
2924 if( objc!=2 ){
2925 Tcl_AppendResult(interp, "wrong # args: should be \"",
2926 Tcl_GetString(objv[0]), " DB", 0);
2927 return TCL_ERROR;
2928 }
2929 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
2930
2931 zErr = sqlite3_errmsg16(db);
danielk1977950f0542006-01-18 05:51:57 +00002932 if( zErr ){
drhee858132007-05-08 20:37:38 +00002933 bytes = sqlite3Utf16ByteLen(zErr, -1);
danielk1977950f0542006-01-18 05:51:57 +00002934 }
danielk19776622cce2004-05-20 11:00:52 +00002935 Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(zErr, bytes));
drh5436dc22004-11-14 04:04:17 +00002936#endif /* SQLITE_OMIT_UTF16 */
danielk19776622cce2004-05-20 11:00:52 +00002937 return TCL_OK;
2938}
2939
2940/*
2941** Usage: sqlite3_prepare DB sql bytes tailvar
2942**
2943** Compile up to <bytes> bytes of the supplied SQL string <sql> using
2944** database handle <DB>. The parameter <tailval> is the name of a global
2945** variable that is set to the unused portion of <sql> (if any). A
2946** STMT handle is returned.
2947*/
2948static int test_prepare(
2949 void * clientData,
2950 Tcl_Interp *interp,
2951 int objc,
2952 Tcl_Obj *CONST objv[]
2953){
2954 sqlite3 *db;
2955 const char *zSql;
2956 int bytes;
2957 const char *zTail = 0;
2958 sqlite3_stmt *pStmt = 0;
2959 char zBuf[50];
danielk19774ad17132004-05-21 01:47:26 +00002960 int rc;
danielk19776622cce2004-05-20 11:00:52 +00002961
2962 if( objc!=5 ){
2963 Tcl_AppendResult(interp, "wrong # args: should be \"",
2964 Tcl_GetString(objv[0]), " DB sql bytes tailvar", 0);
2965 return TCL_ERROR;
2966 }
2967 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
2968 zSql = Tcl_GetString(objv[2]);
2969 if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR;
2970
danielk19774ad17132004-05-21 01:47:26 +00002971 rc = sqlite3_prepare(db, zSql, bytes, &pStmt, &zTail);
drhc60d0442004-09-30 13:43:13 +00002972 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
danielk19776622cce2004-05-20 11:00:52 +00002973 if( zTail ){
2974 if( bytes>=0 ){
2975 bytes = bytes - (zTail-zSql);
2976 }
2977 Tcl_ObjSetVar2(interp, objv[4], 0, Tcl_NewStringObj(zTail, bytes), 0);
2978 }
danielk19774ad17132004-05-21 01:47:26 +00002979 if( rc!=SQLITE_OK ){
2980 assert( pStmt==0 );
2981 sprintf(zBuf, "(%d) ", rc);
2982 Tcl_AppendResult(interp, zBuf, sqlite3_errmsg(db), 0);
2983 return TCL_ERROR;
2984 }
danielk19776622cce2004-05-20 11:00:52 +00002985
danielk19774ad17132004-05-21 01:47:26 +00002986 if( pStmt ){
drh64b1bea2006-01-15 02:30:57 +00002987 if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR;
danielk19774ad17132004-05-21 01:47:26 +00002988 Tcl_AppendResult(interp, zBuf, 0);
2989 }
danielk19776622cce2004-05-20 11:00:52 +00002990 return TCL_OK;
2991}
2992
2993/*
drhb900aaf2006-11-09 00:24:53 +00002994** Usage: sqlite3_prepare_v2 DB sql bytes tailvar
2995**
2996** Compile up to <bytes> bytes of the supplied SQL string <sql> using
2997** database handle <DB>. The parameter <tailval> is the name of a global
2998** variable that is set to the unused portion of <sql> (if any). A
2999** STMT handle is returned.
3000*/
3001static int test_prepare_v2(
3002 void * clientData,
3003 Tcl_Interp *interp,
3004 int objc,
3005 Tcl_Obj *CONST objv[]
3006){
3007 sqlite3 *db;
3008 const char *zSql;
3009 int bytes;
3010 const char *zTail = 0;
3011 sqlite3_stmt *pStmt = 0;
3012 char zBuf[50];
3013 int rc;
3014
3015 if( objc!=5 ){
3016 Tcl_AppendResult(interp, "wrong # args: should be \"",
3017 Tcl_GetString(objv[0]), " DB sql bytes tailvar", 0);
3018 return TCL_ERROR;
3019 }
3020 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
3021 zSql = Tcl_GetString(objv[2]);
3022 if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR;
3023
3024 rc = sqlite3_prepare_v2(db, zSql, bytes, &pStmt, &zTail);
danielk19777e29e952007-04-19 11:09:01 +00003025 assert(rc==SQLITE_OK || pStmt==0);
drhb900aaf2006-11-09 00:24:53 +00003026 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
3027 if( zTail ){
3028 if( bytes>=0 ){
3029 bytes = bytes - (zTail-zSql);
3030 }
3031 Tcl_ObjSetVar2(interp, objv[4], 0, Tcl_NewStringObj(zTail, bytes), 0);
3032 }
3033 if( rc!=SQLITE_OK ){
3034 assert( pStmt==0 );
3035 sprintf(zBuf, "(%d) ", rc);
3036 Tcl_AppendResult(interp, zBuf, sqlite3_errmsg(db), 0);
3037 return TCL_ERROR;
3038 }
3039
3040 if( pStmt ){
3041 if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR;
3042 Tcl_AppendResult(interp, zBuf, 0);
3043 }
3044 return TCL_OK;
3045}
3046
3047/*
3048** Usage: sqlite3_prepare16 DB sql bytes tailvar
danielk19776622cce2004-05-20 11:00:52 +00003049**
3050** Compile up to <bytes> bytes of the supplied SQL string <sql> using
3051** database handle <DB>. The parameter <tailval> is the name of a global
3052** variable that is set to the unused portion of <sql> (if any). A
3053** STMT handle is returned.
3054*/
3055static int test_prepare16(
3056 void * clientData,
3057 Tcl_Interp *interp,
3058 int objc,
3059 Tcl_Obj *CONST objv[]
3060){
drh5436dc22004-11-14 04:04:17 +00003061#ifndef SQLITE_OMIT_UTF16
danielk19776622cce2004-05-20 11:00:52 +00003062 sqlite3 *db;
3063 const void *zSql;
3064 const void *zTail = 0;
3065 Tcl_Obj *pTail = 0;
3066 sqlite3_stmt *pStmt = 0;
drhc60d0442004-09-30 13:43:13 +00003067 char zBuf[50];
3068 int rc;
danielk19776622cce2004-05-20 11:00:52 +00003069 int bytes; /* The integer specified as arg 3 */
3070 int objlen; /* The byte-array length of arg 2 */
3071
3072 if( objc!=5 ){
3073 Tcl_AppendResult(interp, "wrong # args: should be \"",
3074 Tcl_GetString(objv[0]), " DB sql bytes tailvar", 0);
3075 return TCL_ERROR;
3076 }
3077 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
3078 zSql = Tcl_GetByteArrayFromObj(objv[2], &objlen);
3079 if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR;
3080
drhc60d0442004-09-30 13:43:13 +00003081 rc = sqlite3_prepare16(db, zSql, bytes, &pStmt, &zTail);
3082 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
3083 if( rc ){
danielk19776622cce2004-05-20 11:00:52 +00003084 return TCL_ERROR;
3085 }
3086
3087 if( zTail ){
3088 objlen = objlen - ((u8 *)zTail-(u8 *)zSql);
3089 }else{
3090 objlen = 0;
3091 }
3092 pTail = Tcl_NewByteArrayObj((u8 *)zTail, objlen);
3093 Tcl_IncrRefCount(pTail);
3094 Tcl_ObjSetVar2(interp, objv[4], 0, pTail, 0);
danielk19774ad17132004-05-21 01:47:26 +00003095 Tcl_DecrRefCount(pTail);
danielk19776622cce2004-05-20 11:00:52 +00003096
danielk19774ad17132004-05-21 01:47:26 +00003097 if( pStmt ){
drh64b1bea2006-01-15 02:30:57 +00003098 if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR;
danielk19774ad17132004-05-21 01:47:26 +00003099 }
danielk19776622cce2004-05-20 11:00:52 +00003100 Tcl_AppendResult(interp, zBuf, 0);
drh5436dc22004-11-14 04:04:17 +00003101#endif /* SQLITE_OMIT_UTF16 */
danielk19776622cce2004-05-20 11:00:52 +00003102 return TCL_OK;
3103}
3104
danielk19774ad17132004-05-21 01:47:26 +00003105/*
drhb900aaf2006-11-09 00:24:53 +00003106** Usage: sqlite3_prepare16_v2 DB sql bytes tailvar
3107**
3108** Compile up to <bytes> bytes of the supplied SQL string <sql> using
3109** database handle <DB>. The parameter <tailval> is the name of a global
3110** variable that is set to the unused portion of <sql> (if any). A
3111** STMT handle is returned.
3112*/
3113static int test_prepare16_v2(
3114 void * clientData,
3115 Tcl_Interp *interp,
3116 int objc,
3117 Tcl_Obj *CONST objv[]
3118){
3119#ifndef SQLITE_OMIT_UTF16
3120 sqlite3 *db;
3121 const void *zSql;
3122 const void *zTail = 0;
3123 Tcl_Obj *pTail = 0;
3124 sqlite3_stmt *pStmt = 0;
3125 char zBuf[50];
3126 int rc;
3127 int bytes; /* The integer specified as arg 3 */
3128 int objlen; /* The byte-array length of arg 2 */
3129
3130 if( objc!=5 ){
3131 Tcl_AppendResult(interp, "wrong # args: should be \"",
3132 Tcl_GetString(objv[0]), " DB sql bytes tailvar", 0);
3133 return TCL_ERROR;
3134 }
3135 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
3136 zSql = Tcl_GetByteArrayFromObj(objv[2], &objlen);
3137 if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR;
3138
3139 rc = sqlite3_prepare16_v2(db, zSql, bytes, &pStmt, &zTail);
3140 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
3141 if( rc ){
3142 return TCL_ERROR;
3143 }
3144
3145 if( zTail ){
3146 objlen = objlen - ((u8 *)zTail-(u8 *)zSql);
3147 }else{
3148 objlen = 0;
3149 }
3150 pTail = Tcl_NewByteArrayObj((u8 *)zTail, objlen);
3151 Tcl_IncrRefCount(pTail);
3152 Tcl_ObjSetVar2(interp, objv[4], 0, pTail, 0);
3153 Tcl_DecrRefCount(pTail);
3154
3155 if( pStmt ){
3156 if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR;
3157 }
3158 Tcl_AppendResult(interp, zBuf, 0);
3159#endif /* SQLITE_OMIT_UTF16 */
3160 return TCL_OK;
3161}
3162
3163/*
danielk19774ad17132004-05-21 01:47:26 +00003164** Usage: sqlite3_open filename ?options-list?
3165*/
3166static int test_open(
3167 void * clientData,
3168 Tcl_Interp *interp,
3169 int objc,
3170 Tcl_Obj *CONST objv[]
3171){
3172 const char *zFilename;
3173 sqlite3 *db;
3174 int rc;
3175 char zBuf[100];
3176
3177 if( objc!=3 && objc!=2 ){
3178 Tcl_AppendResult(interp, "wrong # args: should be \"",
3179 Tcl_GetString(objv[0]), " filename options-list", 0);
3180 return TCL_ERROR;
3181 }
3182
3183 zFilename = Tcl_GetString(objv[1]);
danielk19774f057f92004-06-08 00:02:33 +00003184 rc = sqlite3_open(zFilename, &db);
danielk19774ad17132004-05-21 01:47:26 +00003185
drh64b1bea2006-01-15 02:30:57 +00003186 if( sqlite3TestMakePointerStr(interp, zBuf, db) ) return TCL_ERROR;
danielk19774ad17132004-05-21 01:47:26 +00003187 Tcl_AppendResult(interp, zBuf, 0);
3188 return TCL_OK;
3189}
3190
3191/*
3192** Usage: sqlite3_open16 filename options
3193*/
3194static int test_open16(
3195 void * clientData,
3196 Tcl_Interp *interp,
3197 int objc,
3198 Tcl_Obj *CONST objv[]
3199){
drh5436dc22004-11-14 04:04:17 +00003200#ifndef SQLITE_OMIT_UTF16
danielk19774ad17132004-05-21 01:47:26 +00003201 const void *zFilename;
3202 sqlite3 *db;
3203 int rc;
3204 char zBuf[100];
3205
3206 if( objc!=3 ){
3207 Tcl_AppendResult(interp, "wrong # args: should be \"",
3208 Tcl_GetString(objv[0]), " filename options-list", 0);
3209 return TCL_ERROR;
3210 }
3211
3212 zFilename = Tcl_GetByteArrayFromObj(objv[1], 0);
danielk19774f057f92004-06-08 00:02:33 +00003213 rc = sqlite3_open16(zFilename, &db);
danielk19774ad17132004-05-21 01:47:26 +00003214
drh64b1bea2006-01-15 02:30:57 +00003215 if( sqlite3TestMakePointerStr(interp, zBuf, db) ) return TCL_ERROR;
danielk19774ad17132004-05-21 01:47:26 +00003216 Tcl_AppendResult(interp, zBuf, 0);
drh5436dc22004-11-14 04:04:17 +00003217#endif /* SQLITE_OMIT_UTF16 */
danielk19774ad17132004-05-21 01:47:26 +00003218 return TCL_OK;
3219}
drhd3d39e92004-05-20 22:16:29 +00003220
3221/*
danielk1977bc6ada42004-06-30 08:20:16 +00003222** Usage: sqlite3_complete16 <UTF-16 string>
3223**
3224** Return 1 if the supplied argument is a complete SQL statement, or zero
3225** otherwise.
3226*/
3227static int test_complete16(
3228 void * clientData,
3229 Tcl_Interp *interp,
3230 int objc,
3231 Tcl_Obj *CONST objv[]
3232){
drhccae6022005-02-26 17:31:26 +00003233#if !defined(SQLITE_OMIT_COMPLETE) && !defined(SQLITE_OMIT_UTF16)
danielk1977bc6ada42004-06-30 08:20:16 +00003234 char *zBuf;
3235
3236 if( objc!=2 ){
3237 Tcl_WrongNumArgs(interp, 1, objv, "<utf-16 sql>");
3238 return TCL_ERROR;
3239 }
3240
drh03d847e2005-12-09 20:21:58 +00003241 zBuf = (char*)Tcl_GetByteArrayFromObj(objv[1], 0);
danielk1977bc6ada42004-06-30 08:20:16 +00003242 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_complete16(zBuf)));
drhccae6022005-02-26 17:31:26 +00003243#endif /* SQLITE_OMIT_COMPLETE && SQLITE_OMIT_UTF16 */
danielk1977bc6ada42004-06-30 08:20:16 +00003244 return TCL_OK;
3245}
3246
3247/*
danielk1977106bb232004-05-21 10:08:53 +00003248** Usage: sqlite3_step STMT
3249**
3250** Advance the statement to the next row.
3251*/
danielk197717240fd2004-05-26 00:07:25 +00003252static int test_step(
danielk1977106bb232004-05-21 10:08:53 +00003253 void * clientData,
3254 Tcl_Interp *interp,
3255 int objc,
3256 Tcl_Obj *CONST objv[]
3257){
3258 sqlite3_stmt *pStmt;
3259 int rc;
3260
danielk1977e1cd9872004-05-22 10:33:04 +00003261 if( objc!=2 ){
danielk1977106bb232004-05-21 10:08:53 +00003262 Tcl_AppendResult(interp, "wrong # args: should be \"",
3263 Tcl_GetString(objv[0]), " STMT", 0);
3264 return TCL_ERROR;
3265 }
3266
3267 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
danielk197717240fd2004-05-26 00:07:25 +00003268 rc = sqlite3_step(pStmt);
danielk1977106bb232004-05-21 10:08:53 +00003269
danielk1977fbcd5852004-06-15 02:44:18 +00003270 /* if( rc!=SQLITE_DONE && rc!=SQLITE_ROW ) return TCL_ERROR; */
drh4f0c5872007-03-26 22:05:01 +00003271 Tcl_SetResult(interp, (char *)t1ErrorName(rc), 0);
danielk1977e1cd9872004-05-22 10:33:04 +00003272 return TCL_OK;
3273}
3274
3275/*
danielk197717240fd2004-05-26 00:07:25 +00003276** Usage: sqlite3_column_count STMT
3277**
3278** Return the number of columns returned by the sql statement STMT.
3279*/
3280static int test_column_count(
3281 void * clientData,
3282 Tcl_Interp *interp,
3283 int objc,
3284 Tcl_Obj *CONST objv[]
3285){
3286 sqlite3_stmt *pStmt;
3287
3288 if( objc!=2 ){
3289 Tcl_AppendResult(interp, "wrong # args: should be \"",
3290 Tcl_GetString(objv[0]), " STMT column", 0);
3291 return TCL_ERROR;
3292 }
3293
3294 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3295
3296 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_column_count(pStmt)));
3297 return TCL_OK;
3298}
3299
3300/*
danielk19773cf86062004-05-26 10:11:05 +00003301** Usage: sqlite3_column_type STMT column
3302**
3303** Return the type of the data in column 'column' of the current row.
3304*/
3305static int test_column_type(
3306 void * clientData,
3307 Tcl_Interp *interp,
3308 int objc,
3309 Tcl_Obj *CONST objv[]
3310){
3311 sqlite3_stmt *pStmt;
3312 int col;
3313 int tp;
3314
3315 if( objc!=3 ){
3316 Tcl_AppendResult(interp, "wrong # args: should be \"",
3317 Tcl_GetString(objv[0]), " STMT column", 0);
3318 return TCL_ERROR;
3319 }
3320
3321 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3322 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
3323
3324 tp = sqlite3_column_type(pStmt, col);
3325 switch( tp ){
drh9c054832004-05-31 18:51:57 +00003326 case SQLITE_INTEGER:
danielk19773cf86062004-05-26 10:11:05 +00003327 Tcl_SetResult(interp, "INTEGER", TCL_STATIC);
3328 break;
drh9c054832004-05-31 18:51:57 +00003329 case SQLITE_NULL:
danielk19773cf86062004-05-26 10:11:05 +00003330 Tcl_SetResult(interp, "NULL", TCL_STATIC);
3331 break;
drh9c054832004-05-31 18:51:57 +00003332 case SQLITE_FLOAT:
danielk19773cf86062004-05-26 10:11:05 +00003333 Tcl_SetResult(interp, "FLOAT", TCL_STATIC);
3334 break;
drh9c054832004-05-31 18:51:57 +00003335 case SQLITE_TEXT:
danielk19773cf86062004-05-26 10:11:05 +00003336 Tcl_SetResult(interp, "TEXT", TCL_STATIC);
3337 break;
drh9c054832004-05-31 18:51:57 +00003338 case SQLITE_BLOB:
danielk19773cf86062004-05-26 10:11:05 +00003339 Tcl_SetResult(interp, "BLOB", TCL_STATIC);
3340 break;
3341 default:
3342 assert(0);
3343 }
3344
3345 return TCL_OK;
3346}
3347
3348/*
danielk197704f2e682004-05-27 01:04:07 +00003349** Usage: sqlite3_column_int64 STMT column
danielk19773cf86062004-05-26 10:11:05 +00003350**
3351** Return the data in column 'column' of the current row cast as an
danielk197704f2e682004-05-27 01:04:07 +00003352** wide (64-bit) integer.
danielk19773cf86062004-05-26 10:11:05 +00003353*/
danielk197704f2e682004-05-27 01:04:07 +00003354static int test_column_int64(
danielk19773cf86062004-05-26 10:11:05 +00003355 void * clientData,
3356 Tcl_Interp *interp,
3357 int objc,
3358 Tcl_Obj *CONST objv[]
3359){
3360 sqlite3_stmt *pStmt;
3361 int col;
danielk197704f2e682004-05-27 01:04:07 +00003362 i64 iVal;
danielk19773cf86062004-05-26 10:11:05 +00003363
3364 if( objc!=3 ){
3365 Tcl_AppendResult(interp, "wrong # args: should be \"",
3366 Tcl_GetString(objv[0]), " STMT column", 0);
3367 return TCL_ERROR;
3368 }
3369
3370 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3371 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
3372
danielk197704f2e682004-05-27 01:04:07 +00003373 iVal = sqlite3_column_int64(pStmt, col);
3374 Tcl_SetObjResult(interp, Tcl_NewWideIntObj(iVal));
3375 return TCL_OK;
3376}
3377
3378/*
danielk1977ea61b2c2004-05-27 01:49:51 +00003379** Usage: sqlite3_column_blob STMT column
3380*/
3381static int test_column_blob(
3382 void * clientData,
3383 Tcl_Interp *interp,
3384 int objc,
3385 Tcl_Obj *CONST objv[]
3386){
3387 sqlite3_stmt *pStmt;
3388 int col;
3389
3390 int len;
danielk1977c572ef72004-05-27 09:28:41 +00003391 const void *pBlob;
danielk1977ea61b2c2004-05-27 01:49:51 +00003392
3393 if( objc!=3 ){
3394 Tcl_AppendResult(interp, "wrong # args: should be \"",
3395 Tcl_GetString(objv[0]), " STMT column", 0);
3396 return TCL_ERROR;
3397 }
3398
3399 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3400 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
3401
danielk1977ea61b2c2004-05-27 01:49:51 +00003402 len = sqlite3_column_bytes(pStmt, col);
drh9310ef22007-04-27 17:16:20 +00003403 pBlob = sqlite3_column_blob(pStmt, col);
danielk1977ea61b2c2004-05-27 01:49:51 +00003404 Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(pBlob, len));
3405 return TCL_OK;
3406}
3407
3408/*
danielk197704f2e682004-05-27 01:04:07 +00003409** Usage: sqlite3_column_double STMT column
3410**
3411** Return the data in column 'column' of the current row cast as a double.
3412*/
3413static int test_column_double(
3414 void * clientData,
3415 Tcl_Interp *interp,
3416 int objc,
3417 Tcl_Obj *CONST objv[]
3418){
3419 sqlite3_stmt *pStmt;
3420 int col;
3421 double rVal;
3422
3423 if( objc!=3 ){
3424 Tcl_AppendResult(interp, "wrong # args: should be \"",
3425 Tcl_GetString(objv[0]), " STMT column", 0);
3426 return TCL_ERROR;
3427 }
3428
3429 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3430 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
3431
3432 rVal = sqlite3_column_double(pStmt, col);
danielk1977c572ef72004-05-27 09:28:41 +00003433 Tcl_SetObjResult(interp, Tcl_NewDoubleObj(rVal));
danielk19773cf86062004-05-26 10:11:05 +00003434 return TCL_OK;
3435}
3436
3437/*
danielk197717240fd2004-05-26 00:07:25 +00003438** Usage: sqlite3_data_count STMT
3439**
3440** Return the number of columns returned by the sql statement STMT.
3441*/
3442static int test_data_count(
3443 void * clientData,
3444 Tcl_Interp *interp,
3445 int objc,
3446 Tcl_Obj *CONST objv[]
3447){
3448 sqlite3_stmt *pStmt;
3449
3450 if( objc!=2 ){
3451 Tcl_AppendResult(interp, "wrong # args: should be \"",
3452 Tcl_GetString(objv[0]), " STMT column", 0);
3453 return TCL_ERROR;
3454 }
3455
3456 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3457
3458 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_data_count(pStmt)));
3459 return TCL_OK;
3460}
3461
3462/*
danielk197704f2e682004-05-27 01:04:07 +00003463** Usage: sqlite3_column_text STMT column
3464**
3465** Usage: sqlite3_column_decltype STMT column
3466**
3467** Usage: sqlite3_column_name STMT column
3468*/
3469static int test_stmt_utf8(
drh241db312004-06-22 12:46:53 +00003470 void * clientData, /* Pointer to SQLite API function to be invoke */
danielk197704f2e682004-05-27 01:04:07 +00003471 Tcl_Interp *interp,
3472 int objc,
3473 Tcl_Obj *CONST objv[]
3474){
3475 sqlite3_stmt *pStmt;
3476 int col;
danielk1977c572ef72004-05-27 09:28:41 +00003477 const char *(*xFunc)(sqlite3_stmt*, int) = clientData;
danielk1977f93bbbe2004-05-27 10:30:52 +00003478 const char *zRet;
danielk197704f2e682004-05-27 01:04:07 +00003479
3480 if( objc!=3 ){
3481 Tcl_AppendResult(interp, "wrong # args: should be \"",
3482 Tcl_GetString(objv[0]), " STMT column", 0);
3483 return TCL_ERROR;
3484 }
3485
3486 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3487 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
danielk1977f93bbbe2004-05-27 10:30:52 +00003488 zRet = xFunc(pStmt, col);
3489 if( zRet ){
3490 Tcl_SetResult(interp, (char *)zRet, 0);
3491 }
danielk197704f2e682004-05-27 01:04:07 +00003492 return TCL_OK;
3493}
3494
danielk19776b456a22005-03-21 04:04:02 +00003495static int test_global_recover(
3496 void * clientData,
3497 Tcl_Interp *interp,
3498 int objc,
3499 Tcl_Obj *CONST objv[]
3500){
3501#ifndef SQLITE_OMIT_GLOBALRECOVER
3502 int rc;
3503 if( objc!=1 ){
3504 Tcl_WrongNumArgs(interp, 1, objv, "");
3505 return TCL_ERROR;
3506 }
3507 rc = sqlite3_global_recover();
drh4f0c5872007-03-26 22:05:01 +00003508 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
danielk19776b456a22005-03-21 04:04:02 +00003509#endif
3510 return TCL_OK;
3511}
3512
danielk197704f2e682004-05-27 01:04:07 +00003513/*
3514** 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_utf16(
drh241db312004-06-22 12:46:53 +00003521 void * clientData, /* Pointer to SQLite API function to be invoked */
danielk197704f2e682004-05-27 01:04:07 +00003522 Tcl_Interp *interp,
3523 int objc,
3524 Tcl_Obj *CONST objv[]
3525){
drh5436dc22004-11-14 04:04:17 +00003526#ifndef SQLITE_OMIT_UTF16
danielk197704f2e682004-05-27 01:04:07 +00003527 sqlite3_stmt *pStmt;
3528 int col;
3529 Tcl_Obj *pRet;
3530 const void *zName16;
danielk1977c572ef72004-05-27 09:28:41 +00003531 const void *(*xFunc)(sqlite3_stmt*, int) = clientData;
danielk197704f2e682004-05-27 01:04:07 +00003532
3533 if( objc!=3 ){
3534 Tcl_AppendResult(interp, "wrong # args: should be \"",
3535 Tcl_GetString(objv[0]), " STMT column", 0);
3536 return TCL_ERROR;
3537 }
3538
3539 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3540 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
3541
3542 zName16 = xFunc(pStmt, col);
danielk1977f93bbbe2004-05-27 10:30:52 +00003543 if( zName16 ){
drhee858132007-05-08 20:37:38 +00003544 pRet = Tcl_NewByteArrayObj(zName16, sqlite3Utf16ByteLen(zName16, -1)+2);
danielk1977f93bbbe2004-05-27 10:30:52 +00003545 Tcl_SetObjResult(interp, pRet);
3546 }
drh5436dc22004-11-14 04:04:17 +00003547#endif /* SQLITE_OMIT_UTF16 */
danielk197704f2e682004-05-27 01:04:07 +00003548
3549 return TCL_OK;
3550}
3551
3552/*
3553** Usage: sqlite3_column_int STMT column
3554**
3555** Usage: sqlite3_column_bytes STMT column
3556**
3557** Usage: sqlite3_column_bytes16 STMT column
3558**
3559*/
3560static int test_stmt_int(
drh241db312004-06-22 12:46:53 +00003561 void * clientData, /* Pointer to SQLite API function to be invoked */
danielk197704f2e682004-05-27 01:04:07 +00003562 Tcl_Interp *interp,
3563 int objc,
3564 Tcl_Obj *CONST objv[]
3565){
3566 sqlite3_stmt *pStmt;
3567 int col;
danielk1977c572ef72004-05-27 09:28:41 +00003568 int (*xFunc)(sqlite3_stmt*, int) = clientData;
danielk197704f2e682004-05-27 01:04:07 +00003569
3570 if( objc!=3 ){
3571 Tcl_AppendResult(interp, "wrong # args: should be \"",
3572 Tcl_GetString(objv[0]), " STMT column", 0);
3573 return TCL_ERROR;
3574 }
3575
3576 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3577 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
3578
3579 Tcl_SetObjResult(interp, Tcl_NewIntObj(xFunc(pStmt, col)));
3580 return TCL_OK;
3581}
3582
danielk197744ee5bf2005-05-27 09:41:12 +00003583#ifndef SQLITE_OMIT_DISKIO
danielk1977b4b47412007-08-17 15:53:36 +00003584#if 0
danielk19779a1d0ab2004-06-01 14:09:28 +00003585/*
3586** Usage: sqlite3OsOpenReadWrite <filename>
3587*/
3588static int test_sqlite3OsOpenReadWrite(
3589 void * clientData,
3590 Tcl_Interp *interp,
3591 int objc,
3592 Tcl_Obj *CONST objv[]
3593){
danielk19771e536952007-08-16 10:09:01 +00003594 sqlite3_file *pFile;
danielk19779a1d0ab2004-06-01 14:09:28 +00003595 int rc;
3596 int dummy;
3597 char zBuf[100];
danielk197704f2e682004-05-27 01:04:07 +00003598
danielk19779a1d0ab2004-06-01 14:09:28 +00003599 if( objc!=2 ){
3600 Tcl_AppendResult(interp, "wrong # args: should be \"",
3601 Tcl_GetString(objv[0]), " filename", 0);
3602 return TCL_ERROR;
3603 }
3604
drh66560ad2006-01-06 14:32:19 +00003605 rc = sqlite3OsOpenReadWrite(Tcl_GetString(objv[1]), &pFile, &dummy);
danielk19779a1d0ab2004-06-01 14:09:28 +00003606 if( rc!=SQLITE_OK ){
drh4f0c5872007-03-26 22:05:01 +00003607 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
danielk19779a1d0ab2004-06-01 14:09:28 +00003608 return TCL_ERROR;
3609 }
drh64b1bea2006-01-15 02:30:57 +00003610 sqlite3TestMakePointerStr(interp, zBuf, pFile);
danielk19779a1d0ab2004-06-01 14:09:28 +00003611 Tcl_SetResult(interp, zBuf, 0);
3612 return TCL_ERROR;
3613}
3614
3615/*
3616** Usage: sqlite3OsClose <file handle>
3617*/
3618static int test_sqlite3OsClose(
3619 void * clientData,
3620 Tcl_Interp *interp,
3621 int objc,
3622 Tcl_Obj *CONST objv[]
3623){
danielk19771e536952007-08-16 10:09:01 +00003624 sqlite3_file *pFile;
danielk19779a1d0ab2004-06-01 14:09:28 +00003625 int rc;
3626
3627 if( objc!=2 ){
3628 Tcl_AppendResult(interp, "wrong # args: should be \"",
3629 Tcl_GetString(objv[0]), " filehandle", 0);
3630 return TCL_ERROR;
3631 }
3632
3633 if( getFilePointer(interp, Tcl_GetString(objv[1]), &pFile) ){
3634 return TCL_ERROR;
3635 }
drh054889e2005-11-30 03:20:31 +00003636 rc = sqlite3OsClose(&pFile);
danielk19779a1d0ab2004-06-01 14:09:28 +00003637 if( rc!=SQLITE_OK ){
drh4f0c5872007-03-26 22:05:01 +00003638 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
danielk19779a1d0ab2004-06-01 14:09:28 +00003639 return TCL_ERROR;
3640 }
danielk19779a1d0ab2004-06-01 14:09:28 +00003641 return TCL_OK;
3642}
3643
3644/*
3645** Usage: sqlite3OsLock <file handle> <locktype>
3646*/
3647static int test_sqlite3OsLock(
3648 void * clientData,
3649 Tcl_Interp *interp,
3650 int objc,
3651 Tcl_Obj *CONST objv[]
3652){
danielk19771e536952007-08-16 10:09:01 +00003653 sqlite3_file * pFile;
danielk19779a1d0ab2004-06-01 14:09:28 +00003654 int rc;
3655
3656 if( objc!=3 ){
3657 Tcl_AppendResult(interp, "wrong # args: should be \"",
3658 Tcl_GetString(objv[0]),
3659 " filehandle (SHARED|RESERVED|PENDING|EXCLUSIVE)", 0);
3660 return TCL_ERROR;
3661 }
3662
3663 if( getFilePointer(interp, Tcl_GetString(objv[1]), &pFile) ){
3664 return TCL_ERROR;
3665 }
3666
3667 if( 0==strcmp("SHARED", Tcl_GetString(objv[2])) ){
drh054889e2005-11-30 03:20:31 +00003668 rc = sqlite3OsLock(pFile, SHARED_LOCK);
danielk19779a1d0ab2004-06-01 14:09:28 +00003669 }
3670 else if( 0==strcmp("RESERVED", Tcl_GetString(objv[2])) ){
drh054889e2005-11-30 03:20:31 +00003671 rc = sqlite3OsLock(pFile, RESERVED_LOCK);
danielk19779a1d0ab2004-06-01 14:09:28 +00003672 }
3673 else if( 0==strcmp("PENDING", Tcl_GetString(objv[2])) ){
drh054889e2005-11-30 03:20:31 +00003674 rc = sqlite3OsLock(pFile, PENDING_LOCK);
danielk19779a1d0ab2004-06-01 14:09:28 +00003675 }
3676 else if( 0==strcmp("EXCLUSIVE", Tcl_GetString(objv[2])) ){
drh054889e2005-11-30 03:20:31 +00003677 rc = sqlite3OsLock(pFile, EXCLUSIVE_LOCK);
danielk19779a1d0ab2004-06-01 14:09:28 +00003678 }else{
3679 Tcl_AppendResult(interp, "wrong # args: should be \"",
3680 Tcl_GetString(objv[0]),
3681 " filehandle (SHARED|RESERVED|PENDING|EXCLUSIVE)", 0);
3682 return TCL_ERROR;
3683 }
3684
3685 if( rc!=SQLITE_OK ){
drh4f0c5872007-03-26 22:05:01 +00003686 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
danielk19779a1d0ab2004-06-01 14:09:28 +00003687 return TCL_ERROR;
3688 }
3689 return TCL_OK;
3690}
3691
3692/*
3693** Usage: sqlite3OsUnlock <file handle>
3694*/
3695static int test_sqlite3OsUnlock(
3696 void * clientData,
3697 Tcl_Interp *interp,
3698 int objc,
3699 Tcl_Obj *CONST objv[]
3700){
danielk19771e536952007-08-16 10:09:01 +00003701 sqlite3_file * pFile;
danielk19779a1d0ab2004-06-01 14:09:28 +00003702 int rc;
3703
3704 if( objc!=2 ){
3705 Tcl_AppendResult(interp, "wrong # args: should be \"",
3706 Tcl_GetString(objv[0]), " filehandle", 0);
3707 return TCL_ERROR;
3708 }
3709
3710 if( getFilePointer(interp, Tcl_GetString(objv[1]), &pFile) ){
3711 return TCL_ERROR;
3712 }
drh054889e2005-11-30 03:20:31 +00003713 rc = sqlite3OsUnlock(pFile, NO_LOCK);
danielk19779a1d0ab2004-06-01 14:09:28 +00003714 if( rc!=SQLITE_OK ){
drh4f0c5872007-03-26 22:05:01 +00003715 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
danielk19779a1d0ab2004-06-01 14:09:28 +00003716 return TCL_ERROR;
3717 }
3718 return TCL_OK;
3719}
drhd3d39e92004-05-20 22:16:29 +00003720
drhab3f9fe2004-08-14 17:10:10 +00003721/*
3722** Usage: sqlite3OsTempFileName
3723*/
3724static int test_sqlite3OsTempFileName(
3725 void * clientData,
3726 Tcl_Interp *interp,
3727 int objc,
3728 Tcl_Obj *CONST objv[]
3729){
3730 char zFile[SQLITE_TEMPNAME_SIZE];
3731 int rc;
3732
drh66560ad2006-01-06 14:32:19 +00003733 rc = sqlite3OsTempFileName(zFile);
drhab3f9fe2004-08-14 17:10:10 +00003734 if( rc!=SQLITE_OK ){
drh4f0c5872007-03-26 22:05:01 +00003735 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
drhab3f9fe2004-08-14 17:10:10 +00003736 return TCL_ERROR;
3737 }
3738 Tcl_AppendResult(interp, zFile, 0);
3739 return TCL_OK;
3740}
danielk197744ee5bf2005-05-27 09:41:12 +00003741#endif
danielk1977b4b47412007-08-17 15:53:36 +00003742#endif
danielk1977d8123362004-06-12 09:25:12 +00003743
danielk19776622cce2004-05-20 11:00:52 +00003744/*
drhcacb2082005-01-11 15:28:33 +00003745** Usage: sqlite_set_magic DB MAGIC-NUMBER
3746**
3747** Set the db->magic value. This is used to test error recovery logic.
3748*/
3749static int sqlite_set_magic(
3750 void * clientData,
3751 Tcl_Interp *interp,
3752 int argc,
3753 char **argv
3754){
3755 sqlite3 *db;
3756 if( argc!=3 ){
3757 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
3758 " DB MAGIC", 0);
3759 return TCL_ERROR;
3760 }
3761 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
3762 if( strcmp(argv[2], "SQLITE_MAGIC_OPEN")==0 ){
3763 db->magic = SQLITE_MAGIC_OPEN;
3764 }else if( strcmp(argv[2], "SQLITE_MAGIC_CLOSED")==0 ){
3765 db->magic = SQLITE_MAGIC_CLOSED;
3766 }else if( strcmp(argv[2], "SQLITE_MAGIC_BUSY")==0 ){
3767 db->magic = SQLITE_MAGIC_BUSY;
3768 }else if( strcmp(argv[2], "SQLITE_MAGIC_ERROR")==0 ){
3769 db->magic = SQLITE_MAGIC_ERROR;
3770 }else if( Tcl_GetInt(interp, argv[2], &db->magic) ){
3771 return TCL_ERROR;
3772 }
3773 return TCL_OK;
3774}
3775
3776/*
drhc5cdca62005-01-11 16:54:14 +00003777** Usage: sqlite3_interrupt DB
3778**
3779** Trigger an interrupt on DB
3780*/
3781static int test_interrupt(
3782 void * clientData,
3783 Tcl_Interp *interp,
3784 int argc,
3785 char **argv
3786){
3787 sqlite3 *db;
3788 if( argc!=2 ){
3789 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " DB", 0);
3790 return TCL_ERROR;
3791 }
3792 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
3793 sqlite3_interrupt(db);
3794 return TCL_OK;
3795}
3796
drh79158e12005-09-06 21:40:45 +00003797static u8 *sqlite3_stack_baseline = 0;
3798
drhc5cdca62005-01-11 16:54:14 +00003799/*
drh79158e12005-09-06 21:40:45 +00003800** Fill the stack with a known bitpattern.
danielk1977600dd0b2005-01-20 01:14:23 +00003801*/
drh79158e12005-09-06 21:40:45 +00003802static void prepStack(void){
3803 int i;
3804 u32 bigBuf[65536];
3805 for(i=0; i<sizeof(bigBuf); i++) bigBuf[i] = 0xdeadbeef;
3806 sqlite3_stack_baseline = (u8*)&bigBuf[65536];
3807}
3808
3809/*
3810** Get the current stack depth. Used for debugging only.
3811*/
3812u64 sqlite3StackDepth(void){
3813 u8 x;
3814 return (u64)(sqlite3_stack_baseline - &x);
3815}
3816
3817/*
3818** Usage: sqlite3_stack_used DB SQL
3819**
3820** Try to measure the amount of stack space used by a call to sqlite3_exec
3821*/
3822static int test_stack_used(
danielk1977600dd0b2005-01-20 01:14:23 +00003823 void * clientData,
3824 Tcl_Interp *interp,
3825 int argc,
3826 char **argv
3827){
3828 sqlite3 *db;
drh79158e12005-09-06 21:40:45 +00003829 int i;
3830 if( argc!=3 ){
3831 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
3832 " DB SQL", 0);
danielk1977600dd0b2005-01-20 01:14:23 +00003833 return TCL_ERROR;
3834 }
drh79158e12005-09-06 21:40:45 +00003835 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
3836 prepStack();
drh37527852006-03-16 16:19:56 +00003837 (void)sqlite3_exec(db, argv[2], 0, 0, 0);
drh79158e12005-09-06 21:40:45 +00003838 for(i=65535; i>=0 && ((u32*)sqlite3_stack_baseline)[-i]==0xdeadbeef; i--){}
3839 Tcl_SetObjResult(interp, Tcl_NewIntObj(i*4));
danielk1977600dd0b2005-01-20 01:14:23 +00003840 return TCL_OK;
3841}
danielk1977600dd0b2005-01-20 01:14:23 +00003842
3843/*
danielk19779636c4e2005-01-25 04:27:54 +00003844** Usage: sqlite_delete_function DB function-name
3845**
3846** Delete the user function 'function-name' from database handle DB. It
3847** is assumed that the user function was created as UTF8, any number of
3848** arguments (the way the TCL interface does it).
3849*/
3850static int delete_function(
3851 void * clientData,
3852 Tcl_Interp *interp,
3853 int argc,
3854 char **argv
3855){
3856 int rc;
3857 sqlite3 *db;
3858 if( argc!=3 ){
3859 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
3860 " DB function-name", 0);
3861 return TCL_ERROR;
3862 }
3863 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
3864 rc = sqlite3_create_function(db, argv[2], -1, SQLITE_UTF8, 0, 0, 0, 0);
drh4f0c5872007-03-26 22:05:01 +00003865 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
danielk19779636c4e2005-01-25 04:27:54 +00003866 return TCL_OK;
3867}
3868
3869/*
3870** Usage: sqlite_delete_collation DB collation-name
3871**
3872** Delete the collation sequence 'collation-name' from database handle
3873** DB. It is assumed that the collation sequence was created as UTF8 (the
3874** way the TCL interface does it).
3875*/
3876static int delete_collation(
3877 void * clientData,
3878 Tcl_Interp *interp,
3879 int argc,
3880 char **argv
3881){
3882 int rc;
3883 sqlite3 *db;
3884 if( argc!=3 ){
3885 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
3886 " DB function-name", 0);
3887 return TCL_ERROR;
3888 }
3889 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
3890 rc = sqlite3_create_collation(db, argv[2], SQLITE_UTF8, 0, 0);
drh4f0c5872007-03-26 22:05:01 +00003891 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
danielk19779636c4e2005-01-25 04:27:54 +00003892 return TCL_OK;
3893}
3894
3895/*
drh3e1d8e62005-05-26 16:23:34 +00003896** Usage: sqlite3_get_autocommit DB
3897**
3898** Return true if the database DB is currently in auto-commit mode.
3899** Return false if not.
3900*/
3901static int get_autocommit(
3902 void * clientData,
3903 Tcl_Interp *interp,
3904 int argc,
3905 char **argv
3906){
3907 char zBuf[30];
3908 sqlite3 *db;
3909 if( argc!=2 ){
3910 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
3911 " DB", 0);
3912 return TCL_ERROR;
3913 }
3914 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
3915 sprintf(zBuf, "%d", sqlite3_get_autocommit(db));
3916 Tcl_AppendResult(interp, zBuf, 0);
3917 return TCL_OK;
3918}
3919
3920/*
drh30867652006-07-06 10:59:57 +00003921** Usage: sqlite3_busy_timeout DB MS
3922**
3923** Set the busy timeout. This is more easily done using the timeout
3924** method of the TCL interface. But we need a way to test the case
3925** where it returns SQLITE_MISUSE.
3926*/
3927static int test_busy_timeout(
3928 void * clientData,
3929 Tcl_Interp *interp,
3930 int argc,
3931 char **argv
3932){
3933 int rc, ms;
3934 sqlite3 *db;
3935 if( argc!=3 ){
3936 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
3937 " DB", 0);
3938 return TCL_ERROR;
3939 }
3940 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
3941 if( Tcl_GetInt(interp, argv[2], &ms) ) return TCL_ERROR;
3942 rc = sqlite3_busy_timeout(db, ms);
3943 Tcl_AppendResult(interp, sqlite3TestErrorName(rc), 0);
3944 return TCL_OK;
3945}
3946
3947/*
drh92febd92004-08-20 18:34:20 +00003948** Usage: tcl_variable_type VARIABLENAME
3949**
3950** Return the name of the internal representation for the
3951** value of the given variable.
3952*/
3953static int tcl_variable_type(
3954 void * clientData,
3955 Tcl_Interp *interp,
3956 int objc,
3957 Tcl_Obj *CONST objv[]
3958){
3959 Tcl_Obj *pVar;
3960 if( objc!=2 ){
3961 Tcl_WrongNumArgs(interp, 1, objv, "VARIABLE");
3962 return TCL_ERROR;
3963 }
3964 pVar = Tcl_GetVar2Ex(interp, Tcl_GetString(objv[1]), 0, TCL_LEAVE_ERR_MSG);
3965 if( pVar==0 ) return TCL_ERROR;
3966 if( pVar->typePtr ){
3967 Tcl_SetObjResult(interp, Tcl_NewStringObj(pVar->typePtr->name, -1));
3968 }
3969 return TCL_OK;
3970}
3971
3972/*
drh6aafc292006-01-05 15:50:06 +00003973** Usage: sqlite3_release_memory ?N?
3974**
3975** Attempt to release memory currently held but not actually required.
3976** The integer N is the number of bytes we are trying to release. The
3977** return value is the amount of memory actually released.
3978*/
3979static int test_release_memory(
3980 void * clientData,
3981 Tcl_Interp *interp,
3982 int objc,
3983 Tcl_Obj *CONST objv[]
3984){
drh6f7adc82006-01-11 21:41:20 +00003985#if defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) && !defined(SQLITE_OMIT_DISKIO)
drh6aafc292006-01-05 15:50:06 +00003986 int N;
3987 int amt;
3988 if( objc!=1 && objc!=2 ){
3989 Tcl_WrongNumArgs(interp, 1, objv, "?N?");
3990 return TCL_ERROR;
3991 }
3992 if( objc==2 ){
3993 if( Tcl_GetIntFromObj(interp, objv[1], &N) ) return TCL_ERROR;
3994 }else{
3995 N = -1;
3996 }
3997 amt = sqlite3_release_memory(N);
3998 Tcl_SetObjResult(interp, Tcl_NewIntObj(amt));
3999#endif
4000 return TCL_OK;
4001}
4002
4003/*
4004** Usage: sqlite3_soft_heap_limit ?N?
4005**
4006** Query or set the soft heap limit for the current thread. The
4007** limit is only changed if the N is present. The previous limit
4008** is returned.
4009*/
4010static int test_soft_heap_limit(
4011 void * clientData,
4012 Tcl_Interp *interp,
4013 int objc,
4014 Tcl_Obj *CONST objv[]
4015){
drh86f8c192007-08-22 00:39:19 +00004016 static int softHeapLimit = 0;
drh6aafc292006-01-05 15:50:06 +00004017 int amt;
4018 if( objc!=1 && objc!=2 ){
4019 Tcl_WrongNumArgs(interp, 1, objv, "?N?");
4020 return TCL_ERROR;
4021 }
drh86f8c192007-08-22 00:39:19 +00004022 amt = softHeapLimit;
drh6aafc292006-01-05 15:50:06 +00004023 if( objc==2 ){
4024 int N;
4025 if( Tcl_GetIntFromObj(interp, objv[1], &N) ) return TCL_ERROR;
4026 sqlite3_soft_heap_limit(N);
drh86f8c192007-08-22 00:39:19 +00004027 softHeapLimit = N;
drh6aafc292006-01-05 15:50:06 +00004028 }
4029 Tcl_SetObjResult(interp, Tcl_NewIntObj(amt));
drh6aafc292006-01-05 15:50:06 +00004030 return TCL_OK;
4031}
4032
4033/*
drhb4bc7052006-01-11 23:40:33 +00004034** Usage: sqlite3_clear_tsd_memdebug
4035**
4036** Clear all of the MEMDEBUG information out of thread-specific data.
4037** This will allow it to be deallocated.
4038*/
4039static int test_clear_tsd_memdebug(
4040 void * clientData,
4041 Tcl_Interp *interp,
4042 int objc,
4043 Tcl_Obj *CONST objv[]
4044){
drhb4bc7052006-01-11 23:40:33 +00004045 return TCL_OK;
4046}
4047
4048/*
4049** Usage: sqlite3_tsd_release
4050**
4051** Call sqlite3ReleaseThreadData.
4052*/
4053static int test_tsd_release(
4054 void * clientData,
4055 Tcl_Interp *interp,
4056 int objc,
4057 Tcl_Obj *CONST objv[]
4058){
drhb4bc7052006-01-11 23:40:33 +00004059 return TCL_OK;
4060}
4061
4062/*
4063** Usage: sqlite3_thread_cleanup
4064**
4065** Call the sqlite3_thread_cleanup API.
4066*/
4067static int test_thread_cleanup(
4068 void * clientData,
4069 Tcl_Interp *interp,
4070 int objc,
4071 Tcl_Obj *CONST objv[]
4072){
4073 sqlite3_thread_cleanup();
4074 return TCL_OK;
4075}
4076
4077
4078/*
drhc6ba55f2007-04-05 17:36:18 +00004079** Usage: sqlite3_pager_refcounts DB
4080**
drhf5345442007-04-09 12:45:02 +00004081** Return a list of numbers which are the PagerRefcount for all
4082** pagers on each database connection.
drhc6ba55f2007-04-05 17:36:18 +00004083*/
4084static int test_pager_refcounts(
4085 void * clientData,
4086 Tcl_Interp *interp,
4087 int objc,
4088 Tcl_Obj *CONST objv[]
4089){
4090 sqlite3 *db;
4091 int i;
4092 int v, *a;
4093 Tcl_Obj *pResult;
4094
4095 if( objc!=2 ){
4096 Tcl_AppendResult(interp, "wrong # args: should be \"",
drhf5345442007-04-09 12:45:02 +00004097 Tcl_GetStringFromObj(objv[0], 0), " DB", 0);
drhc6ba55f2007-04-05 17:36:18 +00004098 return TCL_ERROR;
4099 }
4100 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
4101 pResult = Tcl_NewObj();
4102 for(i=0; i<db->nDb; i++){
4103 if( db->aDb[i].pBt==0 ){
4104 v = -1;
4105 }else{
4106 a = sqlite3PagerStats(sqlite3BtreePager(db->aDb[i].pBt));
4107 v = a[0];
4108 }
4109 Tcl_ListObjAppendElement(0, pResult, Tcl_NewIntObj(v));
4110 }
4111 Tcl_SetObjResult(interp, pResult);
4112 return TCL_OK;
4113}
4114
4115
4116/*
drh80788d82006-09-02 14:50:23 +00004117** tclcmd: working_64bit_int
4118**
4119** Some TCL builds (ex: cygwin) do not support 64-bit integers. This
4120** leads to a number of test failures. The present command checks the
4121** TCL build to see whether or not it supports 64-bit integers. It
4122** returns TRUE if it does and FALSE if not.
4123**
4124** This command is used to warn users that their TCL build is defective
4125** and that the errors they are seeing in the test scripts might be
4126** a result of their defective TCL rather than problems in SQLite.
4127*/
4128static int working_64bit_int(
4129 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
4130 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
4131 int objc, /* Number of arguments */
4132 Tcl_Obj *CONST objv[] /* Command arguments */
4133){
4134 Tcl_Obj *pTestObj;
4135 int working = 0;
4136
4137 pTestObj = Tcl_NewWideIntObj(1000000*(i64)1234567890);
4138 working = strcmp(Tcl_GetString(pTestObj), "1234567890000000")==0;
4139 Tcl_DecrRefCount(pTestObj);
4140 Tcl_SetObjResult(interp, Tcl_NewBooleanObj(working));
4141 return TCL_OK;
4142}
4143
4144
4145/*
drhd1bf3512001-04-07 15:24:33 +00004146** Register commands with the TCL interpreter.
4147*/
4148int Sqlitetest1_Init(Tcl_Interp *interp){
danielk19776f8a5032004-05-10 10:34:51 +00004149 extern int sqlite3_search_count;
4150 extern int sqlite3_interrupt_count;
4151 extern int sqlite3_open_file_count;
drh6bf89572004-11-03 16:27:01 +00004152 extern int sqlite3_sort_count;
danielk19776f8a5032004-05-10 10:34:51 +00004153 extern int sqlite3_current_time;
drhae7e1512007-05-02 16:51:59 +00004154 extern int sqlite3_max_blobsize;
drh16a9b832007-05-05 18:39:25 +00004155 extern int sqlite3BtreeSharedCacheReport(void*,
4156 Tcl_Interp*,int,Tcl_Obj*CONST*);
drhc2eef3b2002-08-31 18:53:06 +00004157 static struct {
4158 char *zName;
4159 Tcl_CmdProc *xProc;
4160 } aCmd[] = {
drhd3d39e92004-05-20 22:16:29 +00004161 { "sqlite3_mprintf_int", (Tcl_CmdProc*)sqlite3_mprintf_int },
drhe9707672004-06-25 01:10:48 +00004162 { "sqlite3_mprintf_int64", (Tcl_CmdProc*)sqlite3_mprintf_int64 },
drhd3d39e92004-05-20 22:16:29 +00004163 { "sqlite3_mprintf_str", (Tcl_CmdProc*)sqlite3_mprintf_str },
drhb3738b62007-03-31 15:02:49 +00004164 { "sqlite3_snprintf_str", (Tcl_CmdProc*)sqlite3_snprintf_str },
drhe29b1a02004-07-17 21:56:09 +00004165 { "sqlite3_mprintf_stronly", (Tcl_CmdProc*)sqlite3_mprintf_stronly},
drhd3d39e92004-05-20 22:16:29 +00004166 { "sqlite3_mprintf_double", (Tcl_CmdProc*)sqlite3_mprintf_double },
4167 { "sqlite3_mprintf_scaled", (Tcl_CmdProc*)sqlite3_mprintf_scaled },
drh63782852005-08-30 19:30:59 +00004168 { "sqlite3_mprintf_hexdouble", (Tcl_CmdProc*)sqlite3_mprintf_hexdouble},
drhd3d39e92004-05-20 22:16:29 +00004169 { "sqlite3_mprintf_z_test", (Tcl_CmdProc*)test_mprintf_z },
drh05a82982006-03-19 13:00:25 +00004170 { "sqlite3_mprintf_n_test", (Tcl_CmdProc*)test_mprintf_n },
drh68853902007-05-07 11:24:30 +00004171 { "sqlite3_snprintf_int", (Tcl_CmdProc*)test_snprintf_int },
drhd3d39e92004-05-20 22:16:29 +00004172 { "sqlite3_last_insert_rowid", (Tcl_CmdProc*)test_last_rowid },
4173 { "sqlite3_exec_printf", (Tcl_CmdProc*)test_exec_printf },
drhb62c3352006-11-23 09:39:16 +00004174 { "sqlite3_exec", (Tcl_CmdProc*)test_exec },
4175 { "sqlite3_exec_nr", (Tcl_CmdProc*)test_exec_nr },
drhd3d39e92004-05-20 22:16:29 +00004176 { "sqlite3_get_table_printf", (Tcl_CmdProc*)test_get_table_printf },
4177 { "sqlite3_close", (Tcl_CmdProc*)sqlite_test_close },
4178 { "sqlite3_create_function", (Tcl_CmdProc*)test_create_function },
4179 { "sqlite3_create_aggregate", (Tcl_CmdProc*)test_create_aggregate },
4180 { "sqlite_register_test_function", (Tcl_CmdProc*)test_register_func },
4181 { "sqlite_abort", (Tcl_CmdProc*)sqlite_abort },
drh25d65432004-07-22 15:02:25 +00004182 { "sqlite_bind", (Tcl_CmdProc*)test_bind },
4183 { "breakpoint", (Tcl_CmdProc*)test_breakpoint },
4184 { "sqlite3_key", (Tcl_CmdProc*)test_key },
4185 { "sqlite3_rekey", (Tcl_CmdProc*)test_rekey },
drhcacb2082005-01-11 15:28:33 +00004186 { "sqlite_set_magic", (Tcl_CmdProc*)sqlite_set_magic },
drhc5cdca62005-01-11 16:54:14 +00004187 { "sqlite3_interrupt", (Tcl_CmdProc*)test_interrupt },
drh3e1d8e62005-05-26 16:23:34 +00004188 { "sqlite_delete_function", (Tcl_CmdProc*)delete_function },
4189 { "sqlite_delete_collation", (Tcl_CmdProc*)delete_collation },
4190 { "sqlite3_get_autocommit", (Tcl_CmdProc*)get_autocommit },
drh79158e12005-09-06 21:40:45 +00004191 { "sqlite3_stack_used", (Tcl_CmdProc*)test_stack_used },
drh30867652006-07-06 10:59:57 +00004192 { "sqlite3_busy_timeout", (Tcl_CmdProc*)test_busy_timeout },
drh3c23a882007-01-09 14:01:13 +00004193 { "printf", (Tcl_CmdProc*)test_printf },
drh538f5702007-04-13 02:14:30 +00004194 { "sqlite3_io_trace", (Tcl_CmdProc*)test_io_trace },
drhc2eef3b2002-08-31 18:53:06 +00004195 };
danielk197751e3d8e2004-05-20 01:12:34 +00004196 static struct {
4197 char *zName;
4198 Tcl_ObjCmdProc *xProc;
danielk197704f2e682004-05-27 01:04:07 +00004199 void *clientData;
danielk197751e3d8e2004-05-20 01:12:34 +00004200 } aObjCmd[] = {
drhdddca282006-01-03 00:33:50 +00004201 { "sqlite3_connection_pointer", get_sqlite_pointer, 0 },
drh241db312004-06-22 12:46:53 +00004202 { "sqlite3_bind_int", test_bind_int, 0 },
drhb026e052007-05-02 01:34:31 +00004203 { "sqlite3_bind_zeroblob", test_bind_zeroblob, 0 },
drh241db312004-06-22 12:46:53 +00004204 { "sqlite3_bind_int64", test_bind_int64, 0 },
4205 { "sqlite3_bind_double", test_bind_double, 0 },
danielk197704f2e682004-05-27 01:04:07 +00004206 { "sqlite3_bind_null", test_bind_null ,0 },
4207 { "sqlite3_bind_text", test_bind_text ,0 },
4208 { "sqlite3_bind_text16", test_bind_text16 ,0 },
4209 { "sqlite3_bind_blob", test_bind_blob ,0 },
drh75f6a032004-07-15 14:15:00 +00004210 { "sqlite3_bind_parameter_count", test_bind_parameter_count, 0},
drh895d7472004-08-20 16:02:39 +00004211 { "sqlite3_bind_parameter_name", test_bind_parameter_name, 0},
drhfa6bc002004-09-07 16:19:52 +00004212 { "sqlite3_bind_parameter_index", test_bind_parameter_index, 0},
danielk1977600dd0b2005-01-20 01:14:23 +00004213 { "sqlite3_clear_bindings", test_clear_bindings, 0},
drhf9cb7f52006-06-27 20:06:44 +00004214 { "sqlite3_sleep", test_sleep, 0},
danielk197704f2e682004-05-27 01:04:07 +00004215 { "sqlite3_errcode", test_errcode ,0 },
4216 { "sqlite3_errmsg", test_errmsg ,0 },
4217 { "sqlite3_errmsg16", test_errmsg16 ,0 },
4218 { "sqlite3_open", test_open ,0 },
4219 { "sqlite3_open16", test_open16 ,0 },
danielk1977bc6ada42004-06-30 08:20:16 +00004220 { "sqlite3_complete16", test_complete16 ,0 },
danielk197704f2e682004-05-27 01:04:07 +00004221
4222 { "sqlite3_prepare", test_prepare ,0 },
4223 { "sqlite3_prepare16", test_prepare16 ,0 },
drhb900aaf2006-11-09 00:24:53 +00004224 { "sqlite3_prepare_v2", test_prepare_v2 ,0 },
4225 { "sqlite3_prepare16_v2", test_prepare16_v2 ,0 },
danielk197704f2e682004-05-27 01:04:07 +00004226 { "sqlite3_finalize", test_finalize ,0 },
4227 { "sqlite3_reset", test_reset ,0 },
drhd89bd002005-01-22 03:03:54 +00004228 { "sqlite3_expired", test_expired ,0 },
drhf8db1bc2005-04-22 02:38:37 +00004229 { "sqlite3_transfer_bindings", test_transfer_bind ,0 },
danielk1977fbcd5852004-06-15 02:44:18 +00004230 { "sqlite3_changes", test_changes ,0 },
4231 { "sqlite3_step", test_step ,0 },
danielk197704f2e682004-05-27 01:04:07 +00004232
drhb4bc7052006-01-11 23:40:33 +00004233 { "sqlite3_release_memory", test_release_memory, 0},
4234 { "sqlite3_soft_heap_limit", test_soft_heap_limit, 0},
4235 { "sqlite3_clear_tsd_memdebug", test_clear_tsd_memdebug, 0},
4236 { "sqlite3_tsd_release", test_tsd_release, 0},
4237 { "sqlite3_thread_cleanup", test_thread_cleanup, 0},
drhc6ba55f2007-04-05 17:36:18 +00004238 { "sqlite3_pager_refcounts", test_pager_refcounts, 0},
drh6aafc292006-01-05 15:50:06 +00004239
drhc2e87a32006-06-27 15:16:14 +00004240 { "sqlite3_load_extension", test_load_extension, 0},
4241 { "sqlite3_enable_load_extension", test_enable_load, 0},
drh4ac285a2006-09-15 07:28:50 +00004242 { "sqlite3_extended_result_codes", test_extended_result_codes, 0},
drhc2e87a32006-06-27 15:16:14 +00004243
danielk197704f2e682004-05-27 01:04:07 +00004244 /* sqlite3_column_*() API */
4245 { "sqlite3_column_count", test_column_count ,0 },
4246 { "sqlite3_data_count", test_data_count ,0 },
4247 { "sqlite3_column_type", test_column_type ,0 },
danielk1977ea61b2c2004-05-27 01:49:51 +00004248 { "sqlite3_column_blob", test_column_blob ,0 },
danielk197704f2e682004-05-27 01:04:07 +00004249 { "sqlite3_column_double", test_column_double ,0 },
4250 { "sqlite3_column_int64", test_column_int64 ,0 },
drh241db312004-06-22 12:46:53 +00004251 { "sqlite3_column_text", test_stmt_utf8, sqlite3_column_text },
4252 { "sqlite3_column_decltype", test_stmt_utf8, sqlite3_column_decltype },
4253 { "sqlite3_column_name", test_stmt_utf8, sqlite3_column_name },
drh6c626082004-11-14 21:56:29 +00004254 { "sqlite3_column_int", test_stmt_int, sqlite3_column_int },
4255 { "sqlite3_column_bytes", test_stmt_int, sqlite3_column_bytes },
danielk19774b1ae992006-02-10 03:06:10 +00004256#ifdef SQLITE_ENABLE_COLUMN_METADATA
danielk1977955de522006-02-10 02:27:42 +00004257{ "sqlite3_column_database_name", test_stmt_utf8, sqlite3_column_database_name},
4258{ "sqlite3_column_table_name", test_stmt_utf8, sqlite3_column_table_name},
4259{ "sqlite3_column_origin_name", test_stmt_utf8, sqlite3_column_origin_name},
danielk19774b1ae992006-02-10 03:06:10 +00004260#endif
danielk1977955de522006-02-10 02:27:42 +00004261
drh6c626082004-11-14 21:56:29 +00004262#ifndef SQLITE_OMIT_UTF16
4263 { "sqlite3_column_bytes16", test_stmt_int, sqlite3_column_bytes16 },
drh241db312004-06-22 12:46:53 +00004264 { "sqlite3_column_text16", test_stmt_utf16, sqlite3_column_text16 },
4265 { "sqlite3_column_decltype16", test_stmt_utf16, sqlite3_column_decltype16},
4266 { "sqlite3_column_name16", test_stmt_utf16, sqlite3_column_name16 },
drh7d9bd4e2006-02-16 18:16:36 +00004267 { "add_alignment_test_collations", add_alignment_test_collations, 0 },
danielk19774b1ae992006-02-10 03:06:10 +00004268#ifdef SQLITE_ENABLE_COLUMN_METADATA
danielk1977955de522006-02-10 02:27:42 +00004269{"sqlite3_column_database_name16",
4270 test_stmt_utf16, sqlite3_column_database_name16},
4271{"sqlite3_column_table_name16", test_stmt_utf16, sqlite3_column_table_name16},
4272{"sqlite3_column_origin_name16", test_stmt_utf16, sqlite3_column_origin_name16},
drh6c626082004-11-14 21:56:29 +00004273#endif
danielk19774b1ae992006-02-10 03:06:10 +00004274#endif
danielk1977a393c032007-05-07 14:58:53 +00004275 { "sqlite3_create_collation_v2", test_create_collation_v2, 0 },
danielk1977a9808b32007-05-07 09:32:45 +00004276 { "sqlite3_global_recover", test_global_recover, 0 },
4277 { "working_64bit_int", working_64bit_int, 0 },
danielk197704f2e682004-05-27 01:04:07 +00004278
danielk19779a1d0ab2004-06-01 14:09:28 +00004279 /* Functions from os.h */
danielk197744ee5bf2005-05-27 09:41:12 +00004280#ifndef SQLITE_OMIT_DISKIO
danielk1977b4b47412007-08-17 15:53:36 +00004281#if 0
danielk19779a1d0ab2004-06-01 14:09:28 +00004282 { "sqlite3OsOpenReadWrite",test_sqlite3OsOpenReadWrite, 0 },
4283 { "sqlite3OsClose", test_sqlite3OsClose, 0 },
4284 { "sqlite3OsLock", test_sqlite3OsLock, 0 },
drhab3f9fe2004-08-14 17:10:10 +00004285 { "sqlite3OsTempFileName", test_sqlite3OsTempFileName, 0 },
danielk1977312d6b32004-06-29 13:18:23 +00004286
4287 /* Custom test interfaces */
4288 { "sqlite3OsUnlock", test_sqlite3OsUnlock, 0 },
danielk197744ee5bf2005-05-27 09:41:12 +00004289#endif
danielk1977b4b47412007-08-17 15:53:36 +00004290#endif
drh5436dc22004-11-14 04:04:17 +00004291#ifndef SQLITE_OMIT_UTF16
danielk1977312d6b32004-06-29 13:18:23 +00004292 { "add_test_collate", test_collate, 0 },
4293 { "add_test_collate_needed", test_collate_needed, 0 },
4294 { "add_test_function", test_function, 0 },
drh5436dc22004-11-14 04:04:17 +00004295#endif
danielk1977312d6b32004-06-29 13:18:23 +00004296 { "sqlite3_test_errstr", test_errstr, 0 },
drh92febd92004-08-20 18:34:20 +00004297 { "tcl_variable_type", tcl_variable_type, 0 },
danielk1977aef0bf62005-12-30 16:28:01 +00004298#ifndef SQLITE_OMIT_SHARED_CACHE
drh6f7adc82006-01-11 21:41:20 +00004299 { "sqlite3_enable_shared_cache", test_enable_shared, 0 },
drh16a9b832007-05-05 18:39:25 +00004300 { "sqlite3_shared_cache_report", sqlite3BtreeSharedCacheReport, 0},
danielk1977aef0bf62005-12-30 16:28:01 +00004301#endif
danielk1977161fb792006-01-24 10:58:21 +00004302 { "sqlite3_libversion_number", test_libversion_number, 0 },
danielk1977deb802c2006-02-09 13:43:28 +00004303#ifdef SQLITE_ENABLE_COLUMN_METADATA
4304 { "sqlite3_table_column_metadata", test_table_column_metadata, 0 },
4305#endif
danielk1977dcbb5d32007-05-04 18:36:44 +00004306#ifndef SQLITE_OMIT_INCRBLOB
4307 { "sqlite3_blob_read", test_blob_read, 0 },
4308 { "sqlite3_blob_write", test_blob_write, 0 },
4309#endif
danielk197751e3d8e2004-05-20 01:12:34 +00004310 };
drh1398ad32005-01-19 23:24:50 +00004311 static int bitmask_size = sizeof(Bitmask)*8;
drhc2eef3b2002-08-31 18:53:06 +00004312 int i;
drh51147ba2005-07-23 22:59:55 +00004313 extern int sqlite3_where_trace;
drhb851b2c2005-03-10 14:11:12 +00004314 extern int sqlite3_sync_count, sqlite3_fullsync_count;
drhaf6df112005-06-07 02:12:30 +00004315 extern int sqlite3_opentemp_count;
drh55ef4d92005-08-14 01:20:37 +00004316 extern int sqlite3_like_count;
drhdd735212007-02-24 13:53:05 +00004317 extern int sqlite3_xferopt_count;
drh538f5702007-04-13 02:14:30 +00004318 extern int sqlite3_pager_readdb_count;
4319 extern int sqlite3_pager_writedb_count;
4320 extern int sqlite3_pager_writej_count;
4321 extern int sqlite3_pager_pgfree_count;
drhd677b3d2007-08-20 22:48:41 +00004322#if OS_UNIX && defined(SQLITE_TEST) && SQLITE_THREADSAFE
drh029b44b2006-01-15 00:13:15 +00004323 extern int threadsOverrideEachOthersLocks;
4324#endif
drhc0929982005-09-05 19:08:29 +00004325#if OS_WIN
4326 extern int sqlite3_os_type;
4327#endif
drh8b3d9902005-08-19 00:14:42 +00004328#ifdef SQLITE_DEBUG
drh73be5012007-08-08 12:11:21 +00004329 extern int sqlite3_os_trace;
drh8b3d9902005-08-19 00:14:42 +00004330 extern int sqlite3_vdbe_addop_trace;
drh549c8b62005-09-19 13:15:23 +00004331#endif
4332#ifdef SQLITE_TEST
4333 extern char sqlite3_query_plan[];
drh9042f392005-07-15 23:24:23 +00004334 static char *query_plan = sqlite3_query_plan;
drh48083ce2005-09-19 12:37:27 +00004335#endif
drhc2eef3b2002-08-31 18:53:06 +00004336
4337 for(i=0; i<sizeof(aCmd)/sizeof(aCmd[0]); i++){
4338 Tcl_CreateCommand(interp, aCmd[i].zName, aCmd[i].xProc, 0, 0);
4339 }
danielk197751e3d8e2004-05-20 01:12:34 +00004340 for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){
danielk1977c572ef72004-05-27 09:28:41 +00004341 Tcl_CreateObjCommand(interp, aObjCmd[i].zName,
4342 aObjCmd[i].xProc, aObjCmd[i].clientData, 0);
danielk197751e3d8e2004-05-20 01:12:34 +00004343 }
danielk19776490beb2004-05-11 06:17:21 +00004344 Tcl_LinkVar(interp, "sqlite_search_count",
danielk19776f8a5032004-05-10 10:34:51 +00004345 (char*)&sqlite3_search_count, TCL_LINK_INT);
drh6bf89572004-11-03 16:27:01 +00004346 Tcl_LinkVar(interp, "sqlite_sort_count",
4347 (char*)&sqlite3_sort_count, TCL_LINK_INT);
drhae7e1512007-05-02 16:51:59 +00004348 Tcl_LinkVar(interp, "sqlite3_max_blobsize",
4349 (char*)&sqlite3_max_blobsize, TCL_LINK_INT);
drh55ef4d92005-08-14 01:20:37 +00004350 Tcl_LinkVar(interp, "sqlite_like_count",
4351 (char*)&sqlite3_like_count, TCL_LINK_INT);
danielk19776490beb2004-05-11 06:17:21 +00004352 Tcl_LinkVar(interp, "sqlite_interrupt_count",
danielk19776f8a5032004-05-10 10:34:51 +00004353 (char*)&sqlite3_interrupt_count, TCL_LINK_INT);
danielk19776490beb2004-05-11 06:17:21 +00004354 Tcl_LinkVar(interp, "sqlite_open_file_count",
danielk19776f8a5032004-05-10 10:34:51 +00004355 (char*)&sqlite3_open_file_count, TCL_LINK_INT);
danielk19776490beb2004-05-11 06:17:21 +00004356 Tcl_LinkVar(interp, "sqlite_current_time",
danielk19776f8a5032004-05-10 10:34:51 +00004357 (char*)&sqlite3_current_time, TCL_LINK_INT);
drhdd735212007-02-24 13:53:05 +00004358 Tcl_LinkVar(interp, "sqlite3_xferopt_count",
4359 (char*)&sqlite3_xferopt_count, TCL_LINK_INT);
drh538f5702007-04-13 02:14:30 +00004360 Tcl_LinkVar(interp, "sqlite3_pager_readdb_count",
4361 (char*)&sqlite3_pager_readdb_count, TCL_LINK_INT);
4362 Tcl_LinkVar(interp, "sqlite3_pager_writedb_count",
4363 (char*)&sqlite3_pager_writedb_count, TCL_LINK_INT);
4364 Tcl_LinkVar(interp, "sqlite3_pager_writej_count",
4365 (char*)&sqlite3_pager_writej_count, TCL_LINK_INT);
4366 Tcl_LinkVar(interp, "sqlite3_pager_pgfree_count",
4367 (char*)&sqlite3_pager_pgfree_count, TCL_LINK_INT);
danielk19774b2688a2006-06-20 11:01:07 +00004368#ifndef SQLITE_OMIT_UTF16
drh7d9bd4e2006-02-16 18:16:36 +00004369 Tcl_LinkVar(interp, "unaligned_string_counter",
4370 (char*)&unaligned_string_counter, TCL_LINK_INT);
danielk19774b2688a2006-06-20 11:01:07 +00004371#endif
drhd677b3d2007-08-20 22:48:41 +00004372#if OS_UNIX && defined(SQLITE_TEST) && SQLITE_THREADSAFE
drh029b44b2006-01-15 00:13:15 +00004373 Tcl_LinkVar(interp, "threadsOverrideEachOthersLocks",
4374 (char*)&threadsOverrideEachOthersLocks, TCL_LINK_INT);
4375#endif
drh268803a2005-12-14 20:11:30 +00004376#ifndef SQLITE_OMIT_UTF16
4377 Tcl_LinkVar(interp, "sqlite_last_needed_collation",
4378 (char*)&pzNeededCollation, TCL_LINK_STRING|TCL_LINK_READ_ONLY);
4379#endif
drhc0929982005-09-05 19:08:29 +00004380#if OS_WIN
4381 Tcl_LinkVar(interp, "sqlite_os_type",
4382 (char*)&sqlite3_os_type, TCL_LINK_INT);
4383#endif
drh549c8b62005-09-19 13:15:23 +00004384#ifdef SQLITE_TEST
4385 Tcl_LinkVar(interp, "sqlite_query_plan",
4386 (char*)&query_plan, TCL_LINK_STRING|TCL_LINK_READ_ONLY);
4387#endif
drh8b3d9902005-08-19 00:14:42 +00004388#ifdef SQLITE_DEBUG
4389 Tcl_LinkVar(interp, "sqlite_addop_trace",
4390 (char*)&sqlite3_vdbe_addop_trace, TCL_LINK_INT);
drh48083ce2005-09-19 12:37:27 +00004391 Tcl_LinkVar(interp, "sqlite_where_trace",
4392 (char*)&sqlite3_where_trace, TCL_LINK_INT);
drh73be5012007-08-08 12:11:21 +00004393 Tcl_LinkVar(interp, "sqlite_os_trace",
4394 (char*)&sqlite3_os_trace, TCL_LINK_INT);
drh8b3d9902005-08-19 00:14:42 +00004395#endif
danielk1977cbe21be2005-06-07 07:58:48 +00004396#ifndef SQLITE_OMIT_DISKIO
drhaf6df112005-06-07 02:12:30 +00004397 Tcl_LinkVar(interp, "sqlite_opentemp_count",
4398 (char*)&sqlite3_opentemp_count, TCL_LINK_INT);
danielk1977cbe21be2005-06-07 07:58:48 +00004399#endif
drh7c972de2003-09-06 22:18:07 +00004400 Tcl_LinkVar(interp, "sqlite_static_bind_value",
4401 (char*)&sqlite_static_bind_value, TCL_LINK_STRING);
drhf0313812006-09-04 15:53:53 +00004402 Tcl_LinkVar(interp, "sqlite_static_bind_nbyte",
4403 (char*)&sqlite_static_bind_nbyte, TCL_LINK_INT);
drhab3f9fe2004-08-14 17:10:10 +00004404 Tcl_LinkVar(interp, "sqlite_temp_directory",
drheffd02b2004-08-29 23:42:13 +00004405 (char*)&sqlite3_temp_directory, TCL_LINK_STRING);
drh1398ad32005-01-19 23:24:50 +00004406 Tcl_LinkVar(interp, "bitmask_size",
4407 (char*)&bitmask_size, TCL_LINK_INT|TCL_LINK_READ_ONLY);
drh748f7632005-03-11 04:41:39 +00004408#if OS_UNIX
drhb851b2c2005-03-10 14:11:12 +00004409 Tcl_LinkVar(interp, "sqlite_sync_count",
4410 (char*)&sqlite3_sync_count, TCL_LINK_INT);
4411 Tcl_LinkVar(interp, "sqlite_fullsync_count",
4412 (char*)&sqlite3_fullsync_count, TCL_LINK_INT);
drh748f7632005-03-11 04:41:39 +00004413#endif /* OS_UNIX */
danielk1977b82e7ed2006-01-11 14:09:31 +00004414
drhd1bf3512001-04-07 15:24:33 +00004415 return TCL_OK;
4416}