blob: 64a6b8fb0ff93d8e9dcd94c8c78c6d31d9758177 [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.
drhd1bf3512001-04-07 15:24:33 +000015*/
16#include "sqliteInt.h"
17#include "tcl.h"
18#include <stdlib.h>
19#include <string.h>
20
drhdddca282006-01-03 00:33:50 +000021/*
22** This is a copy of the first part of the SqliteDb structure in
23** tclsqlite.c. We need it here so that the get_sqlite_pointer routine
24** can extract the sqlite3* pointer from an existing Tcl SQLite
25** connection.
26*/
27struct SqliteDb {
28 sqlite3 *db;
29};
30
31/*
drha3152892007-05-05 11:48:52 +000032** Convert text generated by the "%p" conversion format back into
33** a pointer.
34*/
35static int testHexToInt(int h){
36 if( h>='0' && h<='9' ){
37 return h - '0';
38 }else if( h>='a' && h<='f' ){
39 return h - 'a' + 10;
40 }else{
41 assert( h>='A' && h<='F' );
42 return h - 'A' + 10;
43 }
44}
drhe8f52c52008-07-12 14:52:20 +000045void *sqlite3TestTextToPtr(const char *z){
drha3152892007-05-05 11:48:52 +000046 void *p;
47 u64 v;
48 u32 v2;
49 if( z[0]=='0' && z[1]=='x' ){
50 z += 2;
51 }
52 v = 0;
53 while( *z ){
54 v = (v<<4) + testHexToInt(*z);
55 z++;
56 }
57 if( sizeof(p)==sizeof(v) ){
58 memcpy(&p, &v, sizeof(p));
59 }else{
60 assert( sizeof(p)==sizeof(v2) );
61 v2 = (u32)v;
62 memcpy(&p, &v2, sizeof(p));
63 }
64 return p;
65}
66
67
68/*
drhdddca282006-01-03 00:33:50 +000069** A TCL command that returns the address of the sqlite* pointer
70** for an sqlite connection instance. Bad things happen if the
71** input is not an sqlite connection.
72*/
73static int get_sqlite_pointer(
74 void * clientData,
75 Tcl_Interp *interp,
76 int objc,
77 Tcl_Obj *CONST objv[]
78){
79 struct SqliteDb *p;
80 Tcl_CmdInfo cmdInfo;
81 char zBuf[100];
82 if( objc!=2 ){
83 Tcl_WrongNumArgs(interp, 1, objv, "SQLITE-CONNECTION");
84 return TCL_ERROR;
85 }
86 if( !Tcl_GetCommandInfo(interp, Tcl_GetString(objv[1]), &cmdInfo) ){
87 Tcl_AppendResult(interp, "command not found: ",
88 Tcl_GetString(objv[1]), (char*)0);
89 return TCL_ERROR;
90 }
91 p = (struct SqliteDb*)cmdInfo.objClientData;
92 sprintf(zBuf, "%p", p->db);
93 if( strncmp(zBuf,"0x",2) ){
94 sprintf(zBuf, "0x%p", p->db);
95 }
96 Tcl_AppendResult(interp, zBuf, 0);
97 return TCL_OK;
98}
99
drhb62c3352006-11-23 09:39:16 +0000100/*
101** Decode a pointer to an sqlite3 object.
102*/
drh24b58dd2008-07-07 14:50:14 +0000103int getDbPointer(Tcl_Interp *interp, const char *zA, sqlite3 **ppDb){
drhb62c3352006-11-23 09:39:16 +0000104 struct SqliteDb *p;
105 Tcl_CmdInfo cmdInfo;
106 if( Tcl_GetCommandInfo(interp, zA, &cmdInfo) ){
107 p = (struct SqliteDb*)cmdInfo.objClientData;
108 *ppDb = p->db;
109 }else{
drhe8f52c52008-07-12 14:52:20 +0000110 *ppDb = (sqlite3*)sqlite3TestTextToPtr(zA);
drhb62c3352006-11-23 09:39:16 +0000111 }
112 return TCL_OK;
113}
114
115
drh2e66f0b2005-04-28 17:18:48 +0000116const char *sqlite3TestErrorName(int rc){
danielk19776622cce2004-05-20 11:00:52 +0000117 const char *zName = 0;
drh99dfe5e2008-10-30 15:03:15 +0000118 switch( rc ){
119 case SQLITE_OK: zName = "SQLITE_OK"; break;
120 case SQLITE_ERROR: zName = "SQLITE_ERROR"; break;
121 case SQLITE_INTERNAL: zName = "SQLITE_INTERNAL"; break;
122 case SQLITE_PERM: zName = "SQLITE_PERM"; break;
123 case SQLITE_ABORT: zName = "SQLITE_ABORT"; break;
124 case SQLITE_BUSY: zName = "SQLITE_BUSY"; break;
125 case SQLITE_LOCKED: zName = "SQLITE_LOCKED"; break;
danielk1977404ca072009-03-16 13:19:36 +0000126 case SQLITE_LOCKED_SHAREDCACHE: zName = "SQLITE_LOCKED_SHAREDCACHE";break;
drh99dfe5e2008-10-30 15:03:15 +0000127 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;
132 case SQLITE_NOTFOUND: zName = "SQLITE_NOTFOUND"; break;
133 case SQLITE_FULL: zName = "SQLITE_FULL"; break;
134 case SQLITE_CANTOPEN: zName = "SQLITE_CANTOPEN"; break;
135 case SQLITE_PROTOCOL: zName = "SQLITE_PROTOCOL"; break;
136 case SQLITE_EMPTY: zName = "SQLITE_EMPTY"; break;
137 case SQLITE_SCHEMA: zName = "SQLITE_SCHEMA"; break;
138 case SQLITE_TOOBIG: zName = "SQLITE_TOOBIG"; break;
139 case SQLITE_CONSTRAINT: zName = "SQLITE_CONSTRAINT"; break;
140 case SQLITE_MISMATCH: zName = "SQLITE_MISMATCH"; break;
141 case SQLITE_MISUSE: zName = "SQLITE_MISUSE"; break;
142 case SQLITE_NOLFS: zName = "SQLITE_NOLFS"; break;
143 case SQLITE_AUTH: zName = "SQLITE_AUTH"; break;
144 case SQLITE_FORMAT: zName = "SQLITE_FORMAT"; break;
145 case SQLITE_RANGE: zName = "SQLITE_RANGE"; break;
146 case SQLITE_NOTADB: zName = "SQLITE_NOTADB"; break;
147 case SQLITE_ROW: zName = "SQLITE_ROW"; break;
148 case SQLITE_DONE: zName = "SQLITE_DONE"; break;
149 case SQLITE_IOERR_READ: zName = "SQLITE_IOERR_READ"; break;
150 case SQLITE_IOERR_SHORT_READ: zName = "SQLITE_IOERR_SHORT_READ"; break;
151 case SQLITE_IOERR_WRITE: zName = "SQLITE_IOERR_WRITE"; break;
152 case SQLITE_IOERR_FSYNC: zName = "SQLITE_IOERR_FSYNC"; break;
153 case SQLITE_IOERR_DIR_FSYNC: zName = "SQLITE_IOERR_DIR_FSYNC"; break;
154 case SQLITE_IOERR_TRUNCATE: zName = "SQLITE_IOERR_TRUNCATE"; break;
155 case SQLITE_IOERR_FSTAT: zName = "SQLITE_IOERR_FSTAT"; break;
156 case SQLITE_IOERR_UNLOCK: zName = "SQLITE_IOERR_UNLOCK"; break;
157 case SQLITE_IOERR_RDLOCK: zName = "SQLITE_IOERR_RDLOCK"; break;
158 case SQLITE_IOERR_DELETE: zName = "SQLITE_IOERR_DELETE"; break;
159 case SQLITE_IOERR_BLOCKED: zName = "SQLITE_IOERR_BLOCKED"; break;
160 case SQLITE_IOERR_NOMEM: zName = "SQLITE_IOERR_NOMEM"; break;
161 case SQLITE_IOERR_ACCESS: zName = "SQLITE_IOERR_ACCESS"; break;
162 case SQLITE_IOERR_CHECKRESERVEDLOCK:
163 zName = "SQLITE_IOERR_CHECKRESERVEDLOCK"; break;
164 case SQLITE_IOERR_LOCK: zName = "SQLITE_IOERR_LOCK"; break;
165 default: zName = "SQLITE_Unknown"; break;
danielk19776622cce2004-05-20 11:00:52 +0000166 }
167 return zName;
168}
drh4f0c5872007-03-26 22:05:01 +0000169#define t1ErrorName sqlite3TestErrorName
danielk19776622cce2004-05-20 11:00:52 +0000170
drhd1bf3512001-04-07 15:24:33 +0000171/*
drhc60d0442004-09-30 13:43:13 +0000172** Convert an sqlite3_stmt* into an sqlite3*. This depends on the
173** fact that the sqlite3* is the first field in the Vdbe structure.
174*/
drh51942bc2005-06-12 22:01:42 +0000175#define StmtToDb(X) sqlite3_db_handle(X)
drhc60d0442004-09-30 13:43:13 +0000176
177/*
178** Check a return value to make sure it agrees with the results
179** from sqlite3_errcode.
180*/
181int sqlite3TestErrCode(Tcl_Interp *interp, sqlite3 *db, int rc){
drhb8613ab2009-01-19 17:40:12 +0000182 if( sqlite3_threadsafe()==0 && rc!=SQLITE_MISUSE && rc!=SQLITE_OK
183 && sqlite3_errcode(db)!=rc ){
drhc60d0442004-09-30 13:43:13 +0000184 char zBuf[200];
185 int r2 = sqlite3_errcode(db);
186 sprintf(zBuf, "error code %s (%d) does not match sqlite3_errcode %s (%d)",
drh4f0c5872007-03-26 22:05:01 +0000187 t1ErrorName(rc), rc, t1ErrorName(r2), r2);
drhc60d0442004-09-30 13:43:13 +0000188 Tcl_ResetResult(interp);
189 Tcl_AppendResult(interp, zBuf, 0);
190 return 1;
191 }
192 return 0;
193}
194
195/*
danielk197751e3d8e2004-05-20 01:12:34 +0000196** Decode a pointer to an sqlite3_stmt object.
197*/
198static int getStmtPointer(
199 Tcl_Interp *interp,
200 const char *zArg,
201 sqlite3_stmt **ppStmt
202){
drhe8f52c52008-07-12 14:52:20 +0000203 *ppStmt = (sqlite3_stmt*)sqlite3TestTextToPtr(zArg);
danielk197751e3d8e2004-05-20 01:12:34 +0000204 return TCL_OK;
205}
206
207/*
drh7d8085a2003-04-26 13:19:38 +0000208** Generate a text representation of a pointer that can be understood
209** by the getDbPointer and getVmPointer routines above.
210**
211** The problem is, on some machines (Solaris) if you do a printf with
212** "%p" you cannot turn around and do a scanf with the same "%p" and
213** get your pointer back. You have to prepend a "0x" before it will
214** work. Or at least that is what is reported to me (drh). But this
215** behavior varies from machine to machine. The solution used her is
216** to test the string right after it is generated to see if it can be
217** understood by scanf, and if not, try prepending an "0x" to see if
218** that helps. If nothing works, a fatal error is generated.
219*/
drh64b1bea2006-01-15 02:30:57 +0000220int sqlite3TestMakePointerStr(Tcl_Interp *interp, char *zPtr, void *p){
drhfe63d1c2004-09-08 20:13:04 +0000221 sqlite3_snprintf(100, zPtr, "%p", p);
drh7d8085a2003-04-26 13:19:38 +0000222 return TCL_OK;
223}
224
225/*
danielk19776f8a5032004-05-10 10:34:51 +0000226** The callback routine for sqlite3_exec_printf().
drhd1bf3512001-04-07 15:24:33 +0000227*/
228static int exec_printf_cb(void *pArg, int argc, char **argv, char **name){
229 Tcl_DString *str = (Tcl_DString*)pArg;
230 int i;
231
232 if( Tcl_DStringLength(str)==0 ){
233 for(i=0; i<argc; i++){
234 Tcl_DStringAppendElement(str, name[i] ? name[i] : "NULL");
235 }
236 }
237 for(i=0; i<argc; i++){
238 Tcl_DStringAppendElement(str, argv[i] ? argv[i] : "NULL");
239 }
240 return 0;
241}
242
243/*
drh538f5702007-04-13 02:14:30 +0000244** The I/O tracing callback.
245*/
shaneafdd23a2008-05-29 02:57:47 +0000246#if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE)
drh538f5702007-04-13 02:14:30 +0000247static FILE *iotrace_file = 0;
248static void io_trace_callback(const char *zFormat, ...){
249 va_list ap;
250 va_start(ap, zFormat);
251 vfprintf(iotrace_file, zFormat, ap);
252 va_end(ap);
253 fflush(iotrace_file);
254}
shaneafdd23a2008-05-29 02:57:47 +0000255#endif
drh538f5702007-04-13 02:14:30 +0000256
257/*
258** Usage: io_trace FILENAME
259**
260** Turn I/O tracing on or off. If FILENAME is not an empty string,
261** I/O tracing begins going into FILENAME. If FILENAME is an empty
262** string, I/O tracing is turned off.
263*/
264static int test_io_trace(
265 void *NotUsed,
266 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
267 int argc, /* Number of arguments */
268 char **argv /* Text of each argument */
269){
danielk1977286d2f42008-05-05 11:33:47 +0000270#if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE)
drh538f5702007-04-13 02:14:30 +0000271 if( argc!=2 ){
272 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
273 " FILENAME\"", 0);
274 return TCL_ERROR;
275 }
276 if( iotrace_file ){
277 if( iotrace_file!=stdout && iotrace_file!=stderr ){
278 fclose(iotrace_file);
279 }
280 iotrace_file = 0;
mlcreech3a00f902008-03-04 17:45:01 +0000281 sqlite3IoTrace = 0;
drh538f5702007-04-13 02:14:30 +0000282 }
283 if( argv[1][0] ){
284 if( strcmp(argv[1],"stdout")==0 ){
285 iotrace_file = stdout;
286 }else if( strcmp(argv[1],"stderr")==0 ){
287 iotrace_file = stderr;
288 }else{
289 iotrace_file = fopen(argv[1], "w");
290 }
mlcreech3a00f902008-03-04 17:45:01 +0000291 sqlite3IoTrace = io_trace_callback;
drh538f5702007-04-13 02:14:30 +0000292 }
danielk1977286d2f42008-05-05 11:33:47 +0000293#endif
294 return TCL_OK;
drh538f5702007-04-13 02:14:30 +0000295}
296
297
298/*
danielk19776f8a5032004-05-10 10:34:51 +0000299** Usage: sqlite3_exec_printf DB FORMAT STRING
drhd1bf3512001-04-07 15:24:33 +0000300**
danielk19776f8a5032004-05-10 10:34:51 +0000301** Invoke the sqlite3_exec_printf() interface using the open database
drhd1bf3512001-04-07 15:24:33 +0000302** DB. The SQL is the string FORMAT. The format string should contain
303** one %s or %q. STRING is the value inserted into %s or %q.
304*/
305static int test_exec_printf(
306 void *NotUsed,
307 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
308 int argc, /* Number of arguments */
309 char **argv /* Text of each argument */
310){
drh9bb575f2004-09-06 17:24:11 +0000311 sqlite3 *db;
drhd1bf3512001-04-07 15:24:33 +0000312 Tcl_DString str;
313 int rc;
314 char *zErr = 0;
drh1211de32004-07-26 12:24:22 +0000315 char *zSql;
drhd1bf3512001-04-07 15:24:33 +0000316 char zBuf[30];
317 if( argc!=4 ){
318 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
319 " DB FORMAT STRING", 0);
320 return TCL_ERROR;
321 }
drhb86ccfb2003-01-28 23:13:10 +0000322 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
drhd1bf3512001-04-07 15:24:33 +0000323 Tcl_DStringInit(&str);
drh1211de32004-07-26 12:24:22 +0000324 zSql = sqlite3_mprintf(argv[2], argv[3]);
325 rc = sqlite3_exec(db, zSql, exec_printf_cb, &str, &zErr);
326 sqlite3_free(zSql);
drhd1bf3512001-04-07 15:24:33 +0000327 sprintf(zBuf, "%d", rc);
328 Tcl_AppendElement(interp, zBuf);
329 Tcl_AppendElement(interp, rc==SQLITE_OK ? Tcl_DStringValue(&str) : zErr);
330 Tcl_DStringFree(&str);
danielk1977926aab22006-06-27 07:34:40 +0000331 if( zErr ) sqlite3_free(zErr);
drhc60d0442004-09-30 13:43:13 +0000332 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
drhd1bf3512001-04-07 15:24:33 +0000333 return TCL_OK;
334}
335
336/*
drh5bd98ae2009-01-07 18:24:03 +0000337** Usage: sqlite3_exec_hex DB HEX
338**
339** Invoke the sqlite3_exec() on a string that is obtained by translating
340** HEX into ASCII. Most characters are translated as is. %HH becomes
341** a hex character.
342*/
343static int test_exec_hex(
344 void *NotUsed,
345 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
346 int argc, /* Number of arguments */
347 char **argv /* Text of each argument */
348){
349 sqlite3 *db;
350 Tcl_DString str;
351 int rc, i, j;
352 char *zErr = 0;
353 char *zHex;
354 char zSql[500];
355 char zBuf[30];
356 if( argc!=3 ){
357 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
358 " DB HEX", 0);
359 return TCL_ERROR;
360 }
361 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
362 zHex = argv[2];
363 for(i=j=0; i<sizeof(zSql) && zHex[j]; i++, j++){
364 if( zHex[j]=='%' && zHex[j+2] && zHex[j+2] ){
365 zSql[i] = (testHexToInt(zHex[j+1])<<4) + testHexToInt(zHex[j+2]);
366 j += 2;
367 }else{
368 zSql[i] = zHex[j];
369 }
370 }
371 zSql[i] = 0;
372 Tcl_DStringInit(&str);
373 rc = sqlite3_exec(db, zSql, exec_printf_cb, &str, &zErr);
374 sprintf(zBuf, "%d", rc);
375 Tcl_AppendElement(interp, zBuf);
376 Tcl_AppendElement(interp, rc==SQLITE_OK ? Tcl_DStringValue(&str) : zErr);
377 Tcl_DStringFree(&str);
378 if( zErr ) sqlite3_free(zErr);
379 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
380 return TCL_OK;
381}
382
383/*
drh27641702007-08-22 02:56:42 +0000384** Usage: db_enter DB
385** db_leave DB
386**
387** Enter or leave the mutex on a database connection.
388*/
389static int db_enter(
390 void *NotUsed,
391 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
392 int argc, /* Number of arguments */
393 char **argv /* Text of each argument */
394){
395 sqlite3 *db;
396 if( argc!=2 ){
397 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
398 " DB", 0);
399 return TCL_ERROR;
400 }
401 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
402 sqlite3_mutex_enter(db->mutex);
403 return TCL_OK;
404}
405static int db_leave(
406 void *NotUsed,
407 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
408 int argc, /* Number of arguments */
409 char **argv /* Text of each argument */
410){
411 sqlite3 *db;
412 if( argc!=2 ){
413 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
414 " DB", 0);
415 return TCL_ERROR;
416 }
417 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
418 sqlite3_mutex_leave(db->mutex);
419 return TCL_OK;
420}
421
422/*
drhb62c3352006-11-23 09:39:16 +0000423** Usage: sqlite3_exec DB SQL
424**
425** Invoke the sqlite3_exec interface using the open database DB
426*/
427static int test_exec(
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 sqlite3 *db;
434 Tcl_DString str;
435 int rc;
436 char *zErr = 0;
drh4e5dd852007-05-15 03:56:49 +0000437 char *zSql;
438 int i, j;
drhb62c3352006-11-23 09:39:16 +0000439 char zBuf[30];
440 if( argc!=3 ){
441 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
442 " DB SQL", 0);
443 return TCL_ERROR;
444 }
445 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
446 Tcl_DStringInit(&str);
drh4e5dd852007-05-15 03:56:49 +0000447 zSql = sqlite3_mprintf("%s", argv[2]);
448 for(i=j=0; zSql[i];){
449 if( zSql[i]=='%' ){
450 zSql[j++] = (testHexToInt(zSql[i+1])<<4) + testHexToInt(zSql[i+2]);
451 i += 3;
452 }else{
453 zSql[j++] = zSql[i++];
454 }
455 }
456 zSql[j] = 0;
457 rc = sqlite3_exec(db, zSql, exec_printf_cb, &str, &zErr);
458 sqlite3_free(zSql);
drhb62c3352006-11-23 09:39:16 +0000459 sprintf(zBuf, "%d", rc);
460 Tcl_AppendElement(interp, zBuf);
461 Tcl_AppendElement(interp, rc==SQLITE_OK ? Tcl_DStringValue(&str) : zErr);
462 Tcl_DStringFree(&str);
463 if( zErr ) sqlite3_free(zErr);
464 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
465 return TCL_OK;
466}
467
468/*
469** Usage: sqlite3_exec_nr DB SQL
470**
471** Invoke the sqlite3_exec interface using the open database DB. Discard
472** all results
473*/
474static int test_exec_nr(
475 void *NotUsed,
476 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
477 int argc, /* Number of arguments */
478 char **argv /* Text of each argument */
479){
480 sqlite3 *db;
481 int rc;
482 char *zErr = 0;
483 if( argc!=3 ){
484 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
485 " DB SQL", 0);
486 return TCL_ERROR;
487 }
488 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
489 rc = sqlite3_exec(db, argv[2], 0, 0, &zErr);
490 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
491 return TCL_OK;
492}
493
494/*
danielk19776f8a5032004-05-10 10:34:51 +0000495** Usage: sqlite3_mprintf_z_test SEPARATOR ARG0 ARG1 ...
drhd93d8a82003-06-16 03:08:18 +0000496**
drhbc6160b2009-04-08 15:45:31 +0000497** Test the %z format of sqlite_mprintf(). Use multiple mprintf() calls to
drhd93d8a82003-06-16 03:08:18 +0000498** concatenate arg0 through argn using separator as the separator.
499** Return the result.
500*/
501static int test_mprintf_z(
502 void *NotUsed,
503 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
504 int argc, /* Number of arguments */
505 char **argv /* Text of each argument */
506){
507 char *zResult = 0;
508 int i;
509
danielk1977ca0c8972007-09-01 09:02:53 +0000510 for(i=2; i<argc && (i==2 || zResult); i++){
drhbc6160b2009-04-08 15:45:31 +0000511 zResult = sqlite3_mprintf("%z%s%s", zResult, argv[1], argv[i]);
drhd93d8a82003-06-16 03:08:18 +0000512 }
513 Tcl_AppendResult(interp, zResult, 0);
drh17435752007-08-16 04:30:38 +0000514 sqlite3_free(zResult);
drhd93d8a82003-06-16 03:08:18 +0000515 return TCL_OK;
516}
517
518/*
drh05a82982006-03-19 13:00:25 +0000519** Usage: sqlite3_mprintf_n_test STRING
520**
drhbc6160b2009-04-08 15:45:31 +0000521** Test the %n format of sqlite_mprintf(). Return the length of the
drh05a82982006-03-19 13:00:25 +0000522** input string.
523*/
524static int test_mprintf_n(
525 void *NotUsed,
526 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
527 int argc, /* Number of arguments */
528 char **argv /* Text of each argument */
529){
530 char *zStr;
531 int n = 0;
drhbc6160b2009-04-08 15:45:31 +0000532 zStr = sqlite3_mprintf("%s%n", argv[1], &n);
drh17435752007-08-16 04:30:38 +0000533 sqlite3_free(zStr);
drh05a82982006-03-19 13:00:25 +0000534 Tcl_SetObjResult(interp, Tcl_NewIntObj(n));
535 return TCL_OK;
536}
537
538/*
drh68853902007-05-07 11:24:30 +0000539** Usage: sqlite3_snprintf_int SIZE FORMAT INT
540**
541** Test the of sqlite3_snprintf() routine. SIZE is the size of the
542** output buffer in bytes. The maximum size is 100. FORMAT is the
543** format string. INT is a single integer argument. The FORMAT
544** string must require no more than this one integer argument. If
545** You pass in a format string that requires more than one argument,
546** bad things will happen.
547*/
548static int test_snprintf_int(
549 void *NotUsed,
550 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
551 int argc, /* Number of arguments */
552 char **argv /* Text of each argument */
553){
554 char zStr[100];
555 int n = atoi(argv[1]);
drh68853902007-05-07 11:24:30 +0000556 const char *zFormat = argv[2];
557 int a1 = atoi(argv[3]);
drhdaf276d2007-06-15 18:53:14 +0000558 if( n>sizeof(zStr) ) n = sizeof(zStr);
drh76945742008-02-19 18:29:07 +0000559 sqlite3_snprintf(sizeof(zStr), zStr, "abcdefghijklmnopqrstuvwxyz");
drh68853902007-05-07 11:24:30 +0000560 sqlite3_snprintf(n, zStr, zFormat, a1);
561 Tcl_AppendResult(interp, zStr, 0);
562 return TCL_OK;
563}
564
shane8225f5a2008-07-31 02:05:04 +0000565#ifndef SQLITE_OMIT_GET_TABLE
566
drh68853902007-05-07 11:24:30 +0000567/*
drhd2b3e232008-01-23 14:51:49 +0000568** Usage: sqlite3_get_table_printf DB FORMAT STRING ?--no-counts?
drhd1bf3512001-04-07 15:24:33 +0000569**
danielk19776f8a5032004-05-10 10:34:51 +0000570** Invoke the sqlite3_get_table_printf() interface using the open database
drhd1bf3512001-04-07 15:24:33 +0000571** DB. The SQL is the string FORMAT. The format string should contain
572** one %s or %q. STRING is the value inserted into %s or %q.
573*/
574static int test_get_table_printf(
575 void *NotUsed,
576 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
577 int argc, /* Number of arguments */
578 char **argv /* Text of each argument */
579){
drh9bb575f2004-09-06 17:24:11 +0000580 sqlite3 *db;
drhd1bf3512001-04-07 15:24:33 +0000581 Tcl_DString str;
582 int rc;
583 char *zErr = 0;
584 int nRow, nCol;
585 char **aResult;
586 int i;
587 char zBuf[30];
drh1211de32004-07-26 12:24:22 +0000588 char *zSql;
drhd2b3e232008-01-23 14:51:49 +0000589 int resCount = -1;
590 if( argc==5 ){
591 if( Tcl_GetInt(interp, argv[4], &resCount) ) return TCL_ERROR;
592 }
593 if( argc!=4 && argc!=5 ){
drhd1bf3512001-04-07 15:24:33 +0000594 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
drhd2b3e232008-01-23 14:51:49 +0000595 " DB FORMAT STRING ?COUNT?", 0);
drhd1bf3512001-04-07 15:24:33 +0000596 return TCL_ERROR;
597 }
drhb86ccfb2003-01-28 23:13:10 +0000598 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
drhd1bf3512001-04-07 15:24:33 +0000599 Tcl_DStringInit(&str);
drh1211de32004-07-26 12:24:22 +0000600 zSql = sqlite3_mprintf(argv[2],argv[3]);
drhd2b3e232008-01-23 14:51:49 +0000601 if( argc==5 ){
602 rc = sqlite3_get_table(db, zSql, &aResult, 0, 0, &zErr);
603 }else{
604 rc = sqlite3_get_table(db, zSql, &aResult, &nRow, &nCol, &zErr);
605 resCount = (nRow+1)*nCol;
606 }
drh1211de32004-07-26 12:24:22 +0000607 sqlite3_free(zSql);
drhd1bf3512001-04-07 15:24:33 +0000608 sprintf(zBuf, "%d", rc);
609 Tcl_AppendElement(interp, zBuf);
610 if( rc==SQLITE_OK ){
drhd2b3e232008-01-23 14:51:49 +0000611 if( argc==4 ){
612 sprintf(zBuf, "%d", nRow);
613 Tcl_AppendElement(interp, zBuf);
614 sprintf(zBuf, "%d", nCol);
615 Tcl_AppendElement(interp, zBuf);
616 }
617 for(i=0; i<resCount; i++){
drhd1bf3512001-04-07 15:24:33 +0000618 Tcl_AppendElement(interp, aResult[i] ? aResult[i] : "NULL");
619 }
620 }else{
621 Tcl_AppendElement(interp, zErr);
622 }
danielk19776f8a5032004-05-10 10:34:51 +0000623 sqlite3_free_table(aResult);
danielk1977926aab22006-06-27 07:34:40 +0000624 if( zErr ) sqlite3_free(zErr);
drhc60d0442004-09-30 13:43:13 +0000625 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
drhd1bf3512001-04-07 15:24:33 +0000626 return TCL_OK;
627}
628
shane8225f5a2008-07-31 02:05:04 +0000629#endif /* SQLITE_OMIT_GET_TABLE */
630
drhaf9ff332002-01-16 21:00:27 +0000631
632/*
danielk19776f8a5032004-05-10 10:34:51 +0000633** Usage: sqlite3_last_insert_rowid DB
drhaf9ff332002-01-16 21:00:27 +0000634**
635** Returns the integer ROWID of the most recent insert.
636*/
637static int test_last_rowid(
638 void *NotUsed,
639 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
640 int argc, /* Number of arguments */
641 char **argv /* Text of each argument */
642){
drh9bb575f2004-09-06 17:24:11 +0000643 sqlite3 *db;
drhaf9ff332002-01-16 21:00:27 +0000644 char zBuf[30];
645
646 if( argc!=2 ){
647 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " DB\"", 0);
648 return TCL_ERROR;
649 }
drhb86ccfb2003-01-28 23:13:10 +0000650 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
danielk1977c572ef72004-05-27 09:28:41 +0000651 sprintf(zBuf, "%lld", sqlite3_last_insert_rowid(db));
drhaf9ff332002-01-16 21:00:27 +0000652 Tcl_AppendResult(interp, zBuf, 0);
653 return SQLITE_OK;
654}
655
drhd1bf3512001-04-07 15:24:33 +0000656/*
drh25d65432004-07-22 15:02:25 +0000657** Usage: sqlite3_key DB KEY
658**
659** Set the codec key.
660*/
661static int test_key(
662 void *NotUsed,
663 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
664 int argc, /* Number of arguments */
665 char **argv /* Text of each argument */
666){
drh9bb575f2004-09-06 17:24:11 +0000667 sqlite3 *db;
drh25d65432004-07-22 15:02:25 +0000668 const char *zKey;
669 int nKey;
670 if( argc!=3 ){
671 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
672 " FILENAME\"", 0);
673 return TCL_ERROR;
674 }
675 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
676 zKey = argv[2];
677 nKey = strlen(zKey);
678#ifdef SQLITE_HAS_CODEC
679 sqlite3_key(db, zKey, nKey);
680#endif
681 return TCL_OK;
682}
683
684/*
685** Usage: sqlite3_rekey DB KEY
686**
687** Change the codec key.
688*/
689static int test_rekey(
690 void *NotUsed,
691 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
692 int argc, /* Number of arguments */
693 char **argv /* Text of each argument */
694){
drh9bb575f2004-09-06 17:24:11 +0000695 sqlite3 *db;
drh25d65432004-07-22 15:02:25 +0000696 const char *zKey;
697 int nKey;
698 if( argc!=3 ){
699 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
700 " FILENAME\"", 0);
701 return TCL_ERROR;
702 }
703 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
704 zKey = argv[2];
705 nKey = strlen(zKey);
706#ifdef SQLITE_HAS_CODEC
707 sqlite3_rekey(db, zKey, nKey);
708#endif
709 return TCL_OK;
710}
711
712/*
danielk19776f8a5032004-05-10 10:34:51 +0000713** Usage: sqlite3_close DB
drhd1bf3512001-04-07 15:24:33 +0000714**
danielk19776f8a5032004-05-10 10:34:51 +0000715** Closes the database opened by sqlite3_open.
drhd1bf3512001-04-07 15:24:33 +0000716*/
717static int sqlite_test_close(
718 void *NotUsed,
719 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
720 int argc, /* Number of arguments */
721 char **argv /* Text of each argument */
722){
drh9bb575f2004-09-06 17:24:11 +0000723 sqlite3 *db;
danielk197796d81f92004-06-19 03:33:57 +0000724 int rc;
drhd1bf3512001-04-07 15:24:33 +0000725 if( argc!=2 ){
726 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
727 " FILENAME\"", 0);
728 return TCL_ERROR;
729 }
drhb86ccfb2003-01-28 23:13:10 +0000730 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
danielk197796d81f92004-06-19 03:33:57 +0000731 rc = sqlite3_close(db);
drh4f0c5872007-03-26 22:05:01 +0000732 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
drhd1bf3512001-04-07 15:24:33 +0000733 return TCL_OK;
734}
735
736/*
drhc22bd472002-05-10 13:14:07 +0000737** Implementation of the x_coalesce() function.
738** Return the first argument non-NULL argument.
739*/
drh4f0c5872007-03-26 22:05:01 +0000740static void t1_ifnullFunc(
741 sqlite3_context *context,
742 int argc,
743 sqlite3_value **argv
744){
drhc22bd472002-05-10 13:14:07 +0000745 int i;
746 for(i=0; i<argc; i++){
drh9c054832004-05-31 18:51:57 +0000747 if( SQLITE_NULL!=sqlite3_value_type(argv[i]) ){
drh9310ef22007-04-27 17:16:20 +0000748 int n = sqlite3_value_bytes(argv[i]);
drh03d847e2005-12-09 20:21:58 +0000749 sqlite3_result_text(context, (char*)sqlite3_value_text(argv[i]),
drh9310ef22007-04-27 17:16:20 +0000750 n, SQLITE_TRANSIENT);
drhc22bd472002-05-10 13:14:07 +0000751 break;
752 }
753 }
754}
755
756/*
drhf0313812006-09-04 15:53:53 +0000757** These are test functions. hex8() interprets its argument as
758** UTF8 and returns a hex encoding. hex16le() interprets its argument
759** as UTF16le and returns a hex encoding.
760*/
761static void hex8Func(sqlite3_context *p, int argc, sqlite3_value **argv){
762 const unsigned char *z;
763 int i;
764 char zBuf[200];
765 z = sqlite3_value_text(argv[0]);
766 for(i=0; i<sizeof(zBuf)/2 - 2 && z[i]; i++){
767 sprintf(&zBuf[i*2], "%02x", z[i]&0xff);
768 }
769 zBuf[i*2] = 0;
770 sqlite3_result_text(p, (char*)zBuf, -1, SQLITE_TRANSIENT);
771}
drhaf304692007-04-23 23:56:31 +0000772#ifndef SQLITE_OMIT_UTF16
drhf0313812006-09-04 15:53:53 +0000773static void hex16Func(sqlite3_context *p, int argc, sqlite3_value **argv){
774 const unsigned short int *z;
775 int i;
776 char zBuf[400];
777 z = sqlite3_value_text16(argv[0]);
778 for(i=0; i<sizeof(zBuf)/4 - 4 && z[i]; i++){
779 sprintf(&zBuf[i*4], "%04x", z[i]&0xff);
780 }
781 zBuf[i*4] = 0;
782 sqlite3_result_text(p, (char*)zBuf, -1, SQLITE_TRANSIENT);
783}
drhaf304692007-04-23 23:56:31 +0000784#endif
drhf0313812006-09-04 15:53:53 +0000785
786/*
drhd1d9fc32004-01-07 19:24:48 +0000787** A structure into which to accumulate text.
788*/
789struct dstr {
790 int nAlloc; /* Space allocated */
791 int nUsed; /* Space used */
792 char *z; /* The space */
793};
794
795/*
796** Append text to a dstr
797*/
798static void dstrAppend(struct dstr *p, const char *z, int divider){
799 int n = strlen(z);
800 if( p->nUsed + n + 2 > p->nAlloc ){
801 char *zNew;
802 p->nAlloc = p->nAlloc*2 + n + 200;
drh17435752007-08-16 04:30:38 +0000803 zNew = sqlite3_realloc(p->z, p->nAlloc);
drhd1d9fc32004-01-07 19:24:48 +0000804 if( zNew==0 ){
drh17435752007-08-16 04:30:38 +0000805 sqlite3_free(p->z);
drhd1d9fc32004-01-07 19:24:48 +0000806 memset(p, 0, sizeof(*p));
807 return;
808 }
809 p->z = zNew;
810 }
811 if( divider && p->nUsed>0 ){
812 p->z[p->nUsed++] = divider;
813 }
814 memcpy(&p->z[p->nUsed], z, n+1);
815 p->nUsed += n;
816}
817
818/*
danielk19774adee202004-05-08 08:23:19 +0000819** Invoked for each callback from sqlite3ExecFunc
drhd1d9fc32004-01-07 19:24:48 +0000820*/
821static int execFuncCallback(void *pData, int argc, char **argv, char **NotUsed){
822 struct dstr *p = (struct dstr*)pData;
823 int i;
824 for(i=0; i<argc; i++){
825 if( argv[i]==0 ){
826 dstrAppend(p, "NULL", ' ');
827 }else{
828 dstrAppend(p, argv[i], ' ');
829 }
830 }
831 return 0;
832}
833
834/*
danielk1977e35ee192004-06-26 09:50:11 +0000835** Implementation of the x_sqlite_exec() function. This function takes
drhc22bd472002-05-10 13:14:07 +0000836** a single argument and attempts to execute that argument as SQL code.
drh6cbe1f12002-07-01 00:31:36 +0000837** This is illegal and should set the SQLITE_MISUSE flag on the database.
drhd1d9fc32004-01-07 19:24:48 +0000838**
danielk19776f8a5032004-05-10 10:34:51 +0000839** 2004-Jan-07: We have changed this to make it legal to call sqlite3_exec()
drhd1d9fc32004-01-07 19:24:48 +0000840** from within a function call.
drhc22bd472002-05-10 13:14:07 +0000841**
842** This routine simulates the effect of having two threads attempt to
843** use the same database at the same time.
844*/
danielk197751ad0ec2004-05-24 12:39:02 +0000845static void sqlite3ExecFunc(
danielk19770ae8b832004-05-25 12:05:56 +0000846 sqlite3_context *context,
danielk197751ad0ec2004-05-24 12:39:02 +0000847 int argc,
848 sqlite3_value **argv
849){
drhd1d9fc32004-01-07 19:24:48 +0000850 struct dstr x;
851 memset(&x, 0, sizeof(x));
drh37527852006-03-16 16:19:56 +0000852 (void)sqlite3_exec((sqlite3*)sqlite3_user_data(context),
drh03d847e2005-12-09 20:21:58 +0000853 (char*)sqlite3_value_text(argv[0]),
drhd1d9fc32004-01-07 19:24:48 +0000854 execFuncCallback, &x, 0);
danielk1977d8123362004-06-12 09:25:12 +0000855 sqlite3_result_text(context, x.z, x.nUsed, SQLITE_TRANSIENT);
drh17435752007-08-16 04:30:38 +0000856 sqlite3_free(x.z);
drhc22bd472002-05-10 13:14:07 +0000857}
858
859/*
danielk1977d7263922007-02-05 14:21:47 +0000860** Implementation of tkt2213func(), a scalar function that takes exactly
861** one argument. It has two interesting features:
862**
863** * It calls sqlite3_value_text() 3 times on the argument sqlite3_value*.
864** If the three pointers returned are not the same an SQL error is raised.
865**
drh85b623f2007-12-13 21:54:09 +0000866** * Otherwise it returns a copy of the text representation of its
danielk1977d7263922007-02-05 14:21:47 +0000867** argument in such a way as the VDBE representation is a Mem* cell
868** with the MEM_Term flag clear.
869**
870** Ticket #2213 can therefore be tested by evaluating the following
871** SQL expression:
872**
873** tkt2213func(tkt2213func('a string'));
874*/
875static void tkt2213Function(
876 sqlite3_context *context,
877 int argc,
878 sqlite3_value **argv
879){
880 int nText;
881 unsigned char const *zText1;
882 unsigned char const *zText2;
883 unsigned char const *zText3;
884
885 nText = sqlite3_value_bytes(argv[0]);
886 zText1 = sqlite3_value_text(argv[0]);
887 zText2 = sqlite3_value_text(argv[0]);
888 zText3 = sqlite3_value_text(argv[0]);
889
890 if( zText1!=zText2 || zText2!=zText3 ){
891 sqlite3_result_error(context, "tkt2213 is not fixed", -1);
892 }else{
893 char *zCopy = (char *)sqlite3_malloc(nText);
894 memcpy(zCopy, zText1, nText);
895 sqlite3_result_text(context, zCopy, nText, sqlite3_free);
896 }
897}
898
899/*
drh9310ef22007-04-27 17:16:20 +0000900** The following SQL function takes 4 arguments. The 2nd and
901** 4th argument must be one of these strings: 'text', 'text16',
902** or 'blob' corresponding to API functions
903**
904** sqlite3_value_text()
905** sqlite3_value_text16()
906** sqlite3_value_blob()
907**
908** The third argument is a string, either 'bytes' or 'bytes16' or 'noop',
909** corresponding to APIs:
910**
911** sqlite3_value_bytes()
912** sqlite3_value_bytes16()
913** noop
914**
915** The APIs designated by the 2nd through 4th arguments are applied
916** to the first argument in order. If the pointers returned by the
917** second and fourth are different, this routine returns 1. Otherwise,
918** this routine returns 0.
919**
920** This function is used to test to see when returned pointers from
921** the _text(), _text16() and _blob() APIs become invalidated.
922*/
923static void ptrChngFunction(
924 sqlite3_context *context,
925 int argc,
926 sqlite3_value **argv
927){
928 const void *p1, *p2;
929 const char *zCmd;
930 if( argc!=4 ) return;
931 zCmd = (const char*)sqlite3_value_text(argv[1]);
932 if( zCmd==0 ) return;
933 if( strcmp(zCmd,"text")==0 ){
934 p1 = (const void*)sqlite3_value_text(argv[0]);
935#ifndef SQLITE_OMIT_UTF16
936 }else if( strcmp(zCmd, "text16")==0 ){
937 p1 = (const void*)sqlite3_value_text16(argv[0]);
938#endif
939 }else if( strcmp(zCmd, "blob")==0 ){
940 p1 = (const void*)sqlite3_value_blob(argv[0]);
941 }else{
942 return;
943 }
944 zCmd = (const char*)sqlite3_value_text(argv[2]);
945 if( zCmd==0 ) return;
946 if( strcmp(zCmd,"bytes")==0 ){
947 sqlite3_value_bytes(argv[0]);
948#ifndef SQLITE_OMIT_UTF16
949 }else if( strcmp(zCmd, "bytes16")==0 ){
950 sqlite3_value_bytes16(argv[0]);
951#endif
952 }else if( strcmp(zCmd, "noop")==0 ){
953 /* do nothing */
954 }else{
955 return;
956 }
957 zCmd = (const char*)sqlite3_value_text(argv[3]);
958 if( zCmd==0 ) return;
959 if( strcmp(zCmd,"text")==0 ){
960 p2 = (const void*)sqlite3_value_text(argv[0]);
961#ifndef SQLITE_OMIT_UTF16
962 }else if( strcmp(zCmd, "text16")==0 ){
963 p2 = (const void*)sqlite3_value_text16(argv[0]);
964#endif
965 }else if( strcmp(zCmd, "blob")==0 ){
966 p2 = (const void*)sqlite3_value_blob(argv[0]);
967 }else{
968 return;
969 }
970 sqlite3_result_int(context, p1!=p2);
971}
972
973
974/*
drhc22bd472002-05-10 13:14:07 +0000975** Usage: sqlite_test_create_function DB
976**
danielk19776f8a5032004-05-10 10:34:51 +0000977** Call the sqlite3_create_function API on the given database in order
drhc22bd472002-05-10 13:14:07 +0000978** to create a function named "x_coalesce". This function does the same thing
979** as the "coalesce" function. This function also registers an SQL function
danielk1977e35ee192004-06-26 09:50:11 +0000980** named "x_sqlite_exec" that invokes sqlite3_exec(). Invoking sqlite3_exec()
drhc22bd472002-05-10 13:14:07 +0000981** in this way is illegal recursion and should raise an SQLITE_MISUSE error.
982** The effect is similar to trying to use the same database connection from
983** two threads at the same time.
984**
985** The original motivation for this routine was to be able to call the
danielk19776f8a5032004-05-10 10:34:51 +0000986** sqlite3_create_function function while a query is in progress in order
drhc22bd472002-05-10 13:14:07 +0000987** to test the SQLITE_MISUSE detection logic.
988*/
drhc2eef3b2002-08-31 18:53:06 +0000989static int test_create_function(
drhc22bd472002-05-10 13:14:07 +0000990 void *NotUsed,
991 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
992 int argc, /* Number of arguments */
993 char **argv /* Text of each argument */
994){
drhc60d0442004-09-30 13:43:13 +0000995 int rc;
drh9bb575f2004-09-06 17:24:11 +0000996 sqlite3 *db;
danielk1977312d6b32004-06-29 13:18:23 +0000997
drhc22bd472002-05-10 13:14:07 +0000998 if( argc!=2 ){
999 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
danielk19774397de52005-01-12 12:44:03 +00001000 " DB\"", 0);
drhc22bd472002-05-10 13:14:07 +00001001 return TCL_ERROR;
1002 }
drhb86ccfb2003-01-28 23:13:10 +00001003 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
drhc60d0442004-09-30 13:43:13 +00001004 rc = sqlite3_create_function(db, "x_coalesce", -1, SQLITE_ANY, 0,
drh4f0c5872007-03-26 22:05:01 +00001005 t1_ifnullFunc, 0, 0);
drh235a8182006-09-13 19:21:28 +00001006 if( rc==SQLITE_OK ){
1007 rc = sqlite3_create_function(db, "hex8", 1, SQLITE_ANY, 0,
1008 hex8Func, 0, 0);
1009 }
drhaf304692007-04-23 23:56:31 +00001010#ifndef SQLITE_OMIT_UTF16
drh235a8182006-09-13 19:21:28 +00001011 if( rc==SQLITE_OK ){
1012 rc = sqlite3_create_function(db, "hex16", 1, SQLITE_ANY, 0,
1013 hex16Func, 0, 0);
1014 }
drhaf304692007-04-23 23:56:31 +00001015#endif
danielk1977d7263922007-02-05 14:21:47 +00001016 if( rc==SQLITE_OK ){
1017 rc = sqlite3_create_function(db, "tkt2213func", 1, SQLITE_ANY, 0,
1018 tkt2213Function, 0, 0);
1019 }
drh9310ef22007-04-27 17:16:20 +00001020 if( rc==SQLITE_OK ){
1021 rc = sqlite3_create_function(db, "pointer_change", 4, SQLITE_ANY, 0,
1022 ptrChngFunction, 0, 0);
1023 }
danielk1977312d6b32004-06-29 13:18:23 +00001024
drh5436dc22004-11-14 04:04:17 +00001025#ifndef SQLITE_OMIT_UTF16
danielk1977312d6b32004-06-29 13:18:23 +00001026 /* Use the sqlite3_create_function16() API here. Mainly for fun, but also
1027 ** because it is not tested anywhere else. */
drhc60d0442004-09-30 13:43:13 +00001028 if( rc==SQLITE_OK ){
drheee4c8c2008-02-18 22:24:57 +00001029 const void *zUtf16;
danielk1977576ec6b2005-01-21 11:55:25 +00001030 sqlite3_value *pVal;
drhf3a65f72007-08-22 20:18:21 +00001031 sqlite3_mutex_enter(db->mutex);
1032 pVal = sqlite3ValueNew(db);
drhb21c8cd2007-08-21 19:33:56 +00001033 sqlite3ValueSetStr(pVal, -1, "x_sqlite_exec", SQLITE_UTF8, SQLITE_STATIC);
danielk1977a7a8e142008-02-13 18:25:27 +00001034 zUtf16 = sqlite3ValueText(pVal, SQLITE_UTF16NATIVE);
drhf3a65f72007-08-22 20:18:21 +00001035 if( db->mallocFailed ){
1036 rc = SQLITE_NOMEM;
1037 }else{
danielk1977a7a8e142008-02-13 18:25:27 +00001038 rc = sqlite3_create_function16(db, zUtf16,
1039 1, SQLITE_UTF16, db, sqlite3ExecFunc, 0, 0);
drhf3a65f72007-08-22 20:18:21 +00001040 }
drhc60d0442004-09-30 13:43:13 +00001041 sqlite3ValueFree(pVal);
drhf3a65f72007-08-22 20:18:21 +00001042 sqlite3_mutex_leave(db->mutex);
drhc60d0442004-09-30 13:43:13 +00001043 }
drh5436dc22004-11-14 04:04:17 +00001044#endif
1045
drhc60d0442004-09-30 13:43:13 +00001046 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
drh4f0c5872007-03-26 22:05:01 +00001047 Tcl_SetResult(interp, (char *)t1ErrorName(rc), 0);
drhc22bd472002-05-10 13:14:07 +00001048 return TCL_OK;
1049}
1050
1051/*
1052** Routines to implement the x_count() aggregate function.
drh90669c12006-01-20 15:45:36 +00001053**
1054** x_count() counts the number of non-null arguments. But there are
1055** some twists for testing purposes.
1056**
1057** If the argument to x_count() is 40 then a UTF-8 error is reported
1058** on the step function. If x_count(41) is seen, then a UTF-16 error
1059** is reported on the step function. If the total count is 42, then
1060** a UTF-8 error is reported on the finalize function.
drhc22bd472002-05-10 13:14:07 +00001061*/
drh4f0c5872007-03-26 22:05:01 +00001062typedef struct t1CountCtx t1CountCtx;
1063struct t1CountCtx {
drhc22bd472002-05-10 13:14:07 +00001064 int n;
1065};
drh4f0c5872007-03-26 22:05:01 +00001066static void t1CountStep(
1067 sqlite3_context *context,
1068 int argc,
1069 sqlite3_value **argv
1070){
1071 t1CountCtx *p;
drh4f26d6c2004-05-26 23:25:30 +00001072 p = sqlite3_aggregate_context(context, sizeof(*p));
drh9c054832004-05-31 18:51:57 +00001073 if( (argc==0 || SQLITE_NULL!=sqlite3_value_type(argv[0]) ) && p ){
drhc22bd472002-05-10 13:14:07 +00001074 p->n++;
1075 }
drh90669c12006-01-20 15:45:36 +00001076 if( argc>0 ){
1077 int v = sqlite3_value_int(argv[0]);
1078 if( v==40 ){
1079 sqlite3_result_error(context, "value of 40 handed to x_count", -1);
danielk1977a1686c92006-01-23 07:52:37 +00001080#ifndef SQLITE_OMIT_UTF16
drh90669c12006-01-20 15:45:36 +00001081 }else if( v==41 ){
1082 const char zUtf16ErrMsg[] = { 0, 0x61, 0, 0x62, 0, 0x63, 0, 0, 0};
1083 sqlite3_result_error16(context, &zUtf16ErrMsg[1-SQLITE_BIGENDIAN], -1);
danielk1977a1686c92006-01-23 07:52:37 +00001084#endif
drh90669c12006-01-20 15:45:36 +00001085 }
1086 }
drhc22bd472002-05-10 13:14:07 +00001087}
drh4f0c5872007-03-26 22:05:01 +00001088static void t1CountFinalize(sqlite3_context *context){
1089 t1CountCtx *p;
drh4f26d6c2004-05-26 23:25:30 +00001090 p = sqlite3_aggregate_context(context, sizeof(*p));
drh90669c12006-01-20 15:45:36 +00001091 if( p ){
1092 if( p->n==42 ){
1093 sqlite3_result_error(context, "x_count totals to 42", -1);
1094 }else{
1095 sqlite3_result_int(context, p ? p->n : 0);
1096 }
1097 }
drhc22bd472002-05-10 13:14:07 +00001098}
1099
danielk1977c5512882009-08-10 04:37:49 +00001100#ifndef SQLITE_OMIT_DEPRECATED
danielk1977fa18bec2007-09-03 11:04:22 +00001101static void legacyCountStep(
1102 sqlite3_context *context,
1103 int argc,
1104 sqlite3_value **argv
1105){
1106 /* no-op */
1107}
shaneeec556d2008-10-12 00:27:53 +00001108
danielk1977fa18bec2007-09-03 11:04:22 +00001109static void legacyCountFinalize(sqlite3_context *context){
1110 sqlite3_result_int(context, sqlite3_aggregate_count(context));
1111}
shaneeec556d2008-10-12 00:27:53 +00001112#endif
danielk1977fa18bec2007-09-03 11:04:22 +00001113
drhc22bd472002-05-10 13:14:07 +00001114/*
danielk1977fa18bec2007-09-03 11:04:22 +00001115** Usage: sqlite3_create_aggregate DB
drhc22bd472002-05-10 13:14:07 +00001116**
danielk19776f8a5032004-05-10 10:34:51 +00001117** Call the sqlite3_create_function API on the given database in order
danielk1977fa18bec2007-09-03 11:04:22 +00001118** to create a function named "x_count". This function is similar
1119** to the built-in count() function, with a few special quirks
1120** for testing the sqlite3_result_error() APIs.
drhc22bd472002-05-10 13:14:07 +00001121**
1122** The original motivation for this routine was to be able to call the
danielk19776f8a5032004-05-10 10:34:51 +00001123** sqlite3_create_aggregate function while a query is in progress in order
drh90669c12006-01-20 15:45:36 +00001124** to test the SQLITE_MISUSE detection logic. See misuse.test.
1125**
1126** This routine was later extended to test the use of sqlite3_result_error()
1127** within aggregate functions.
danielk1977fa18bec2007-09-03 11:04:22 +00001128**
1129** Later: It is now also extended to register the aggregate function
1130** "legacy_count()" with the supplied database handle. This is used
1131** to test the deprecated sqlite3_aggregate_count() API.
drhc22bd472002-05-10 13:14:07 +00001132*/
drhc2eef3b2002-08-31 18:53:06 +00001133static int test_create_aggregate(
drhc22bd472002-05-10 13:14:07 +00001134 void *NotUsed,
1135 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1136 int argc, /* Number of arguments */
1137 char **argv /* Text of each argument */
1138){
drh9bb575f2004-09-06 17:24:11 +00001139 sqlite3 *db;
drhc60d0442004-09-30 13:43:13 +00001140 int rc;
drhc22bd472002-05-10 13:14:07 +00001141 if( argc!=2 ){
1142 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1143 " FILENAME\"", 0);
1144 return TCL_ERROR;
1145 }
drhb86ccfb2003-01-28 23:13:10 +00001146 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
drhc60d0442004-09-30 13:43:13 +00001147 rc = sqlite3_create_function(db, "x_count", 0, SQLITE_UTF8, 0, 0,
drh4f0c5872007-03-26 22:05:01 +00001148 t1CountStep,t1CountFinalize);
drhc60d0442004-09-30 13:43:13 +00001149 if( rc==SQLITE_OK ){
danielk1977fa18bec2007-09-03 11:04:22 +00001150 rc = sqlite3_create_function(db, "x_count", 1, SQLITE_UTF8, 0, 0,
drh4f0c5872007-03-26 22:05:01 +00001151 t1CountStep,t1CountFinalize);
drhc60d0442004-09-30 13:43:13 +00001152 }
shaneeec556d2008-10-12 00:27:53 +00001153#ifndef SQLITE_OMIT_DEPRECATED
danielk1977fa18bec2007-09-03 11:04:22 +00001154 if( rc==SQLITE_OK ){
1155 rc = sqlite3_create_function(db, "legacy_count", 0, SQLITE_ANY, 0, 0,
1156 legacyCountStep, legacyCountFinalize
1157 );
1158 }
shaneeec556d2008-10-12 00:27:53 +00001159#endif
drhc60d0442004-09-30 13:43:13 +00001160 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
danielk1977fa18bec2007-09-03 11:04:22 +00001161 Tcl_SetResult(interp, (char *)t1ErrorName(rc), 0);
drhc22bd472002-05-10 13:14:07 +00001162 return TCL_OK;
1163}
1164
1165
drh3c23a882007-01-09 14:01:13 +00001166/*
1167** Usage: printf TEXT
1168**
1169** Send output to printf. Use this rather than puts to merge the output
1170** in the correct sequence with debugging printfs inserted into C code.
1171** Puts uses a separate buffer and debugging statements will be out of
1172** sequence if it is used.
1173*/
1174static int test_printf(
1175 void *NotUsed,
1176 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1177 int argc, /* Number of arguments */
1178 char **argv /* Text of each argument */
1179){
1180 if( argc!=2 ){
1181 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1182 " TEXT\"", 0);
1183 return TCL_ERROR;
1184 }
1185 printf("%s\n", argv[1]);
1186 return TCL_OK;
1187}
1188
1189
drhc22bd472002-05-10 13:14:07 +00001190
1191/*
danielk19776f8a5032004-05-10 10:34:51 +00001192** Usage: sqlite3_mprintf_int FORMAT INTEGER INTEGER INTEGER
drhd1bf3512001-04-07 15:24:33 +00001193**
1194** Call mprintf with three integer arguments
1195*/
danielk19776f8a5032004-05-10 10:34:51 +00001196static int sqlite3_mprintf_int(
drhd1bf3512001-04-07 15:24:33 +00001197 void *NotUsed,
1198 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1199 int argc, /* Number of arguments */
1200 char **argv /* Text of each argument */
1201){
1202 int a[3], i;
1203 char *z;
1204 if( argc!=5 ){
1205 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1206 " FORMAT INT INT INT\"", 0);
1207 return TCL_ERROR;
1208 }
1209 for(i=2; i<5; i++){
1210 if( Tcl_GetInt(interp, argv[i], &a[i-2]) ) return TCL_ERROR;
1211 }
danielk19776f8a5032004-05-10 10:34:51 +00001212 z = sqlite3_mprintf(argv[1], a[0], a[1], a[2]);
drhd1bf3512001-04-07 15:24:33 +00001213 Tcl_AppendResult(interp, z, 0);
drh3f4fedb2004-05-31 19:34:33 +00001214 sqlite3_free(z);
drhd1bf3512001-04-07 15:24:33 +00001215 return TCL_OK;
1216}
1217
1218/*
drhe9707672004-06-25 01:10:48 +00001219** Usage: sqlite3_mprintf_int64 FORMAT INTEGER INTEGER INTEGER
1220**
1221** Call mprintf with three 64-bit integer arguments
1222*/
1223static int sqlite3_mprintf_int64(
1224 void *NotUsed,
1225 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1226 int argc, /* Number of arguments */
1227 char **argv /* Text of each argument */
1228){
1229 int i;
1230 sqlite_int64 a[3];
1231 char *z;
1232 if( argc!=5 ){
1233 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1234 " FORMAT INT INT INT\"", 0);
1235 return TCL_ERROR;
1236 }
1237 for(i=2; i<5; i++){
drhdee0e402009-05-03 20:23:53 +00001238 if( !sqlite3Atoi64(argv[i], &a[i-2]) ){
drhe9707672004-06-25 01:10:48 +00001239 Tcl_AppendResult(interp, "argument is not a valid 64-bit integer", 0);
1240 return TCL_ERROR;
1241 }
1242 }
1243 z = sqlite3_mprintf(argv[1], a[0], a[1], a[2]);
1244 Tcl_AppendResult(interp, z, 0);
1245 sqlite3_free(z);
1246 return TCL_OK;
1247}
1248
1249/*
drhc5cad1e2009-02-01 00:21:09 +00001250** Usage: sqlite3_mprintf_long FORMAT INTEGER INTEGER INTEGER
1251**
1252** Call mprintf with three long integer arguments. This might be the
1253** same as sqlite3_mprintf_int or sqlite3_mprintf_int64, depending on
1254** platform.
1255*/
1256static int sqlite3_mprintf_long(
1257 void *NotUsed,
1258 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1259 int argc, /* Number of arguments */
1260 char **argv /* Text of each argument */
1261){
1262 int i;
1263 long int a[3];
1264 int b[3];
1265 char *z;
1266 if( argc!=5 ){
1267 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1268 " FORMAT INT INT INT\"", 0);
1269 return TCL_ERROR;
1270 }
1271 for(i=2; i<5; i++){
1272 if( Tcl_GetInt(interp, argv[i], &b[i-2]) ) return TCL_ERROR;
1273 a[i-2] = (long int)b[i-2];
drh7ed0cae2009-02-03 16:25:47 +00001274 a[i-2] &= (((u64)1)<<(sizeof(int)*8))-1;
drhc5cad1e2009-02-01 00:21:09 +00001275 }
1276 z = sqlite3_mprintf(argv[1], a[0], a[1], a[2]);
1277 Tcl_AppendResult(interp, z, 0);
1278 sqlite3_free(z);
1279 return TCL_OK;
1280}
1281
1282/*
danielk19776f8a5032004-05-10 10:34:51 +00001283** Usage: sqlite3_mprintf_str FORMAT INTEGER INTEGER STRING
drhd1bf3512001-04-07 15:24:33 +00001284**
1285** Call mprintf with two integer arguments and one string argument
1286*/
danielk19776f8a5032004-05-10 10:34:51 +00001287static int sqlite3_mprintf_str(
drhd1bf3512001-04-07 15:24:33 +00001288 void *NotUsed,
1289 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1290 int argc, /* Number of arguments */
1291 char **argv /* Text of each argument */
1292){
1293 int a[3], i;
1294 char *z;
chwf220b242002-06-16 04:54:28 +00001295 if( argc<4 || argc>5 ){
drhd1bf3512001-04-07 15:24:33 +00001296 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
chwf220b242002-06-16 04:54:28 +00001297 " FORMAT INT INT ?STRING?\"", 0);
drhd1bf3512001-04-07 15:24:33 +00001298 return TCL_ERROR;
1299 }
1300 for(i=2; i<4; i++){
1301 if( Tcl_GetInt(interp, argv[i], &a[i-2]) ) return TCL_ERROR;
1302 }
danielk19776f8a5032004-05-10 10:34:51 +00001303 z = sqlite3_mprintf(argv[1], a[0], a[1], argc>4 ? argv[4] : NULL);
drhd1bf3512001-04-07 15:24:33 +00001304 Tcl_AppendResult(interp, z, 0);
drh3f4fedb2004-05-31 19:34:33 +00001305 sqlite3_free(z);
drhd1bf3512001-04-07 15:24:33 +00001306 return TCL_OK;
1307}
1308
1309/*
drhb3738b62007-03-31 15:02:49 +00001310** Usage: sqlite3_snprintf_str INTEGER FORMAT INTEGER INTEGER STRING
1311**
1312** Call mprintf with two integer arguments and one string argument
1313*/
1314static int sqlite3_snprintf_str(
1315 void *NotUsed,
1316 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1317 int argc, /* Number of arguments */
1318 char **argv /* Text of each argument */
1319){
1320 int a[3], i;
1321 int n;
1322 char *z;
1323 if( argc<5 || argc>6 ){
1324 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1325 " INT FORMAT INT INT ?STRING?\"", 0);
1326 return TCL_ERROR;
1327 }
1328 if( Tcl_GetInt(interp, argv[1], &n) ) return TCL_ERROR;
1329 if( n<0 ){
1330 Tcl_AppendResult(interp, "N must be non-negative", 0);
1331 return TCL_ERROR;
1332 }
1333 for(i=3; i<5; i++){
1334 if( Tcl_GetInt(interp, argv[i], &a[i-3]) ) return TCL_ERROR;
1335 }
1336 z = sqlite3_malloc( n+1 );
1337 sqlite3_snprintf(n, z, argv[2], a[0], a[1], argc>4 ? argv[5] : NULL);
1338 Tcl_AppendResult(interp, z, 0);
1339 sqlite3_free(z);
1340 return TCL_OK;
1341}
1342
1343/*
drh63782852005-08-30 19:30:59 +00001344** Usage: sqlite3_mprintf_double FORMAT INTEGER INTEGER DOUBLE
drhd1bf3512001-04-07 15:24:33 +00001345**
1346** Call mprintf with two integer arguments and one double argument
1347*/
danielk19776f8a5032004-05-10 10:34:51 +00001348static int sqlite3_mprintf_double(
drhd1bf3512001-04-07 15:24:33 +00001349 void *NotUsed,
1350 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1351 int argc, /* Number of arguments */
1352 char **argv /* Text of each argument */
1353){
1354 int a[3], i;
1355 double r;
1356 char *z;
1357 if( argc!=5 ){
1358 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
drh63782852005-08-30 19:30:59 +00001359 " FORMAT INT INT DOUBLE\"", 0);
drhd1bf3512001-04-07 15:24:33 +00001360 return TCL_ERROR;
1361 }
1362 for(i=2; i<4; i++){
1363 if( Tcl_GetInt(interp, argv[i], &a[i-2]) ) return TCL_ERROR;
1364 }
1365 if( Tcl_GetDouble(interp, argv[4], &r) ) return TCL_ERROR;
danielk19776f8a5032004-05-10 10:34:51 +00001366 z = sqlite3_mprintf(argv[1], a[0], a[1], r);
drhd1bf3512001-04-07 15:24:33 +00001367 Tcl_AppendResult(interp, z, 0);
drh3f4fedb2004-05-31 19:34:33 +00001368 sqlite3_free(z);
drhd1bf3512001-04-07 15:24:33 +00001369 return TCL_OK;
1370}
1371
1372/*
drh63782852005-08-30 19:30:59 +00001373** Usage: sqlite3_mprintf_scaled FORMAT DOUBLE DOUBLE
drhb621c232004-02-21 19:41:04 +00001374**
1375** Call mprintf with a single double argument which is the product of the
1376** two arguments given above. This is used to generate overflow and underflow
1377** doubles to test that they are converted properly.
1378*/
danielk19776f8a5032004-05-10 10:34:51 +00001379static int sqlite3_mprintf_scaled(
drhb621c232004-02-21 19:41:04 +00001380 void *NotUsed,
1381 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1382 int argc, /* Number of arguments */
1383 char **argv /* Text of each argument */
1384){
1385 int i;
1386 double r[2];
1387 char *z;
1388 if( argc!=4 ){
1389 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1390 " FORMAT DOUBLE DOUBLE\"", 0);
1391 return TCL_ERROR;
1392 }
1393 for(i=2; i<4; i++){
1394 if( Tcl_GetDouble(interp, argv[i], &r[i-2]) ) return TCL_ERROR;
1395 }
danielk19776f8a5032004-05-10 10:34:51 +00001396 z = sqlite3_mprintf(argv[1], r[0]*r[1]);
drhb621c232004-02-21 19:41:04 +00001397 Tcl_AppendResult(interp, z, 0);
drh3f4fedb2004-05-31 19:34:33 +00001398 sqlite3_free(z);
drhb621c232004-02-21 19:41:04 +00001399 return TCL_OK;
1400}
1401
1402/*
drhe29b1a02004-07-17 21:56:09 +00001403** Usage: sqlite3_mprintf_stronly FORMAT STRING
1404**
1405** Call mprintf with a single double argument which is the product of the
1406** two arguments given above. This is used to generate overflow and underflow
1407** doubles to test that they are converted properly.
1408*/
1409static int sqlite3_mprintf_stronly(
1410 void *NotUsed,
1411 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1412 int argc, /* Number of arguments */
1413 char **argv /* Text of each argument */
1414){
1415 char *z;
1416 if( argc!=3 ){
1417 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1418 " FORMAT STRING\"", 0);
1419 return TCL_ERROR;
1420 }
1421 z = sqlite3_mprintf(argv[1], argv[2]);
1422 Tcl_AppendResult(interp, z, 0);
1423 sqlite3_free(z);
1424 return TCL_OK;
1425}
1426
1427/*
drh63782852005-08-30 19:30:59 +00001428** Usage: sqlite3_mprintf_hexdouble FORMAT HEX
1429**
1430** Call mprintf with a single double argument which is derived from the
1431** hexadecimal encoding of an IEEE double.
1432*/
1433static int sqlite3_mprintf_hexdouble(
1434 void *NotUsed,
1435 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1436 int argc, /* Number of arguments */
1437 char **argv /* Text of each argument */
1438){
1439 char *z;
1440 double r;
shane5e73db32008-07-08 03:04:58 +00001441 unsigned int x1, x2;
1442 sqlite_uint64 d;
drh63782852005-08-30 19:30:59 +00001443 if( argc!=3 ){
1444 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1445 " FORMAT STRING\"", 0);
1446 return TCL_ERROR;
1447 }
1448 if( sscanf(argv[2], "%08x%08x", &x2, &x1)!=2 ){
1449 Tcl_AppendResult(interp, "2nd argument should be 16-characters of hex", 0);
1450 return TCL_ERROR;
1451 }
1452 d = x2;
1453 d = (d<<32) + x1;
1454 memcpy(&r, &d, sizeof(r));
1455 z = sqlite3_mprintf(argv[1], r);
1456 Tcl_AppendResult(interp, z, 0);
1457 sqlite3_free(z);
1458 return TCL_OK;
1459}
1460
1461/*
danielk1977c1def3e2008-08-30 13:25:10 +00001462** Usage: sqlite3_enable_shared_cache ?BOOLEAN?
danielk1977aef0bf62005-12-30 16:28:01 +00001463**
1464*/
drh6f7adc82006-01-11 21:41:20 +00001465#if !defined(SQLITE_OMIT_SHARED_CACHE)
1466static int test_enable_shared(
danielk197752622822006-01-09 09:59:49 +00001467 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
danielk1977aef0bf62005-12-30 16:28:01 +00001468 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1469 int objc, /* Number of arguments */
1470 Tcl_Obj *CONST objv[] /* Command arguments */
1471){
1472 int rc;
1473 int enable;
danielk197752622822006-01-09 09:59:49 +00001474 int ret = 0;
danielk1977aef0bf62005-12-30 16:28:01 +00001475
danielk1977c1def3e2008-08-30 13:25:10 +00001476 if( objc!=2 && objc!=1 ){
1477 Tcl_WrongNumArgs(interp, 1, objv, "?BOOLEAN?");
danielk1977aef0bf62005-12-30 16:28:01 +00001478 return TCL_ERROR;
1479 }
danielk1977502b4e02008-09-02 14:07:24 +00001480 ret = sqlite3GlobalConfig.sharedCacheEnabled;
danielk1977c1def3e2008-08-30 13:25:10 +00001481
1482 if( objc==2 ){
1483 if( Tcl_GetBooleanFromObj(interp, objv[1], &enable) ){
1484 return TCL_ERROR;
1485 }
1486 rc = sqlite3_enable_shared_cache(enable);
1487 if( rc!=SQLITE_OK ){
1488 Tcl_SetResult(interp, (char *)sqlite3ErrStr(rc), TCL_STATIC);
1489 return TCL_ERROR;
1490 }
danielk1977aef0bf62005-12-30 16:28:01 +00001491 }
danielk197752622822006-01-09 09:59:49 +00001492 Tcl_SetObjResult(interp, Tcl_NewBooleanObj(ret));
danielk1977aef0bf62005-12-30 16:28:01 +00001493 return TCL_OK;
1494}
1495#endif
1496
drh16a9b832007-05-05 18:39:25 +00001497
1498
danielk1977aef0bf62005-12-30 16:28:01 +00001499/*
drh4ac285a2006-09-15 07:28:50 +00001500** Usage: sqlite3_extended_result_codes DB BOOLEAN
1501**
1502*/
1503static int test_extended_result_codes(
1504 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
1505 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1506 int objc, /* Number of arguments */
1507 Tcl_Obj *CONST objv[] /* Command arguments */
1508){
1509 int enable;
1510 sqlite3 *db;
1511
1512 if( objc!=3 ){
1513 Tcl_WrongNumArgs(interp, 1, objv, "DB BOOLEAN");
1514 return TCL_ERROR;
1515 }
1516 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
1517 if( Tcl_GetBooleanFromObj(interp, objv[2], &enable) ) return TCL_ERROR;
1518 sqlite3_extended_result_codes(db, enable);
1519 return TCL_OK;
1520}
1521
1522/*
danielk1977161fb792006-01-24 10:58:21 +00001523** Usage: sqlite3_libversion_number
1524**
1525*/
1526static int test_libversion_number(
1527 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
1528 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1529 int objc, /* Number of arguments */
1530 Tcl_Obj *CONST objv[] /* Command arguments */
1531){
1532 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_libversion_number()));
1533 return TCL_OK;
1534}
1535
1536/*
danielk1977deb802c2006-02-09 13:43:28 +00001537** Usage: sqlite3_table_column_metadata DB dbname tblname colname
1538**
1539*/
1540#ifdef SQLITE_ENABLE_COLUMN_METADATA
1541static int test_table_column_metadata(
1542 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
1543 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1544 int objc, /* Number of arguments */
1545 Tcl_Obj *CONST objv[] /* Command arguments */
1546){
1547 sqlite3 *db;
1548 const char *zDb;
1549 const char *zTbl;
1550 const char *zCol;
1551 int rc;
1552 Tcl_Obj *pRet;
1553
1554 const char *zDatatype;
1555 const char *zCollseq;
1556 int notnull;
1557 int primarykey;
1558 int autoincrement;
1559
1560 if( objc!=5 ){
1561 Tcl_WrongNumArgs(interp, 1, objv, "DB dbname tblname colname");
1562 return TCL_ERROR;
1563 }
1564 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
1565 zDb = Tcl_GetString(objv[2]);
1566 zTbl = Tcl_GetString(objv[3]);
1567 zCol = Tcl_GetString(objv[4]);
1568
1569 if( strlen(zDb)==0 ) zDb = 0;
1570
1571 rc = sqlite3_table_column_metadata(db, zDb, zTbl, zCol,
1572 &zDatatype, &zCollseq, &notnull, &primarykey, &autoincrement);
1573
1574 if( rc!=SQLITE_OK ){
1575 Tcl_AppendResult(interp, sqlite3_errmsg(db), 0);
1576 return TCL_ERROR;
1577 }
1578
1579 pRet = Tcl_NewObj();
1580 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zDatatype, -1));
1581 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zCollseq, -1));
1582 Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(notnull));
1583 Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(primarykey));
1584 Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(autoincrement));
1585 Tcl_SetObjResult(interp, pRet);
1586
1587 return TCL_OK;
1588}
1589#endif
1590
danielk1977dcbb5d32007-05-04 18:36:44 +00001591#ifndef SQLITE_OMIT_INCRBLOB
1592
1593/*
1594** sqlite3_blob_read CHANNEL OFFSET N
danielk1977a9808b32007-05-07 09:32:45 +00001595**
1596** This command is used to test the sqlite3_blob_read() in ways that
1597** the Tcl channel interface does not. The first argument should
1598** be the name of a valid channel created by the [incrblob] method
1599** of a database handle. This function calls sqlite3_blob_read()
1600** to read N bytes from offset OFFSET from the underlying SQLite
1601** blob handle.
1602**
1603** On success, a byte-array object containing the read data is
1604** returned. On failure, the interpreter result is set to the
1605** text representation of the returned error code (i.e. "SQLITE_NOMEM")
1606** and a Tcl exception is thrown.
danielk1977dcbb5d32007-05-04 18:36:44 +00001607*/
1608static int test_blob_read(
1609 ClientData clientData, /* Not used */
1610 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1611 int objc, /* Number of arguments */
1612 Tcl_Obj *CONST objv[] /* Command arguments */
1613){
1614 Tcl_Channel channel;
1615 ClientData instanceData;
1616 sqlite3_blob *pBlob;
1617 int notUsed;
1618 int nByte;
1619 int iOffset;
1620 unsigned char *zBuf;
1621 int rc;
1622
1623 if( objc!=4 ){
1624 Tcl_WrongNumArgs(interp, 1, objv, "CHANNEL OFFSET N");
1625 return TCL_ERROR;
1626 }
1627
1628 channel = Tcl_GetChannel(interp, Tcl_GetString(objv[1]), &notUsed);
1629 if( !channel
1630 || TCL_OK!=Tcl_GetIntFromObj(interp, objv[2], &iOffset)
1631 || TCL_OK!=Tcl_GetIntFromObj(interp, objv[3], &nByte)
1632 || nByte<0 || iOffset<0
1633 ){
1634 return TCL_ERROR;
1635 }
1636
1637 instanceData = Tcl_GetChannelInstanceData(channel);
1638 pBlob = *((sqlite3_blob **)instanceData);
1639
1640 zBuf = (unsigned char *)Tcl_Alloc(nByte);
1641 rc = sqlite3_blob_read(pBlob, zBuf, nByte, iOffset);
1642 if( rc==SQLITE_OK ){
1643 Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(zBuf, nByte));
1644 }else{
1645 Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_VOLATILE);
1646 }
1647 Tcl_Free((char *)zBuf);
1648
1649 return (rc==SQLITE_OK ? TCL_OK : TCL_ERROR);
1650}
1651
1652/*
danielk1977a8b30182008-10-02 14:49:01 +00001653** sqlite3_blob_write CHANNEL OFFSET DATA ?NDATA?
danielk1977a9808b32007-05-07 09:32:45 +00001654**
1655** This command is used to test the sqlite3_blob_write() in ways that
1656** the Tcl channel interface does not. The first argument should
1657** be the name of a valid channel created by the [incrblob] method
1658** of a database handle. This function calls sqlite3_blob_write()
1659** to write the DATA byte-array to the underlying SQLite blob handle.
1660** at offset OFFSET.
1661**
1662** On success, an empty string is returned. On failure, the interpreter
1663** result is set to the text representation of the returned error code
1664** (i.e. "SQLITE_NOMEM") and a Tcl exception is thrown.
danielk1977dcbb5d32007-05-04 18:36:44 +00001665*/
1666static int test_blob_write(
1667 ClientData clientData, /* Not used */
1668 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1669 int objc, /* Number of arguments */
1670 Tcl_Obj *CONST objv[] /* Command arguments */
1671){
1672 Tcl_Channel channel;
1673 ClientData instanceData;
1674 sqlite3_blob *pBlob;
1675 int notUsed;
1676 int iOffset;
1677 int rc;
1678
1679 unsigned char *zBuf;
1680 int nBuf;
1681
danielk1977a8b30182008-10-02 14:49:01 +00001682 if( objc!=4 && objc!=5 ){
1683 Tcl_WrongNumArgs(interp, 1, objv, "CHANNEL OFFSET DATA ?NDATA?");
danielk1977dcbb5d32007-05-04 18:36:44 +00001684 return TCL_ERROR;
1685 }
1686
1687 channel = Tcl_GetChannel(interp, Tcl_GetString(objv[1]), &notUsed);
danielk1977a8b30182008-10-02 14:49:01 +00001688 if( !channel || TCL_OK!=Tcl_GetIntFromObj(interp, objv[2], &iOffset) ){
danielk1977dcbb5d32007-05-04 18:36:44 +00001689 return TCL_ERROR;
1690 }
1691
1692 instanceData = Tcl_GetChannelInstanceData(channel);
1693 pBlob = *((sqlite3_blob **)instanceData);
1694
1695 zBuf = Tcl_GetByteArrayFromObj(objv[3], &nBuf);
danielk1977a8b30182008-10-02 14:49:01 +00001696 if( objc==5 && Tcl_GetIntFromObj(interp, objv[4], &nBuf) ){
1697 return TCL_ERROR;
1698 }
danielk1977dcbb5d32007-05-04 18:36:44 +00001699 rc = sqlite3_blob_write(pBlob, zBuf, nBuf, iOffset);
1700 if( rc!=SQLITE_OK ){
1701 Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_VOLATILE);
1702 }
1703
1704 return (rc==SQLITE_OK ? TCL_OK : TCL_ERROR);
1705}
1706#endif
drhc2e87a32006-06-27 15:16:14 +00001707
danielk1977deb802c2006-02-09 13:43:28 +00001708/*
danielk1977a393c032007-05-07 14:58:53 +00001709** Usage: sqlite3_create_collation_v2 DB-HANDLE NAME CMP-PROC DEL-PROC
danielk1977a9808b32007-05-07 09:32:45 +00001710**
1711** This Tcl proc is used for testing the experimental
danielk1977a393c032007-05-07 14:58:53 +00001712** sqlite3_create_collation_v2() interface.
danielk1977a9808b32007-05-07 09:32:45 +00001713*/
1714struct TestCollationX {
1715 Tcl_Interp *interp;
1716 Tcl_Obj *pCmp;
1717 Tcl_Obj *pDel;
1718};
1719typedef struct TestCollationX TestCollationX;
1720static void testCreateCollationDel(void *pCtx){
1721 TestCollationX *p = (TestCollationX *)pCtx;
1722
1723 int rc = Tcl_EvalObjEx(p->interp, p->pDel, TCL_EVAL_DIRECT|TCL_EVAL_GLOBAL);
1724 if( rc!=TCL_OK ){
1725 Tcl_BackgroundError(p->interp);
1726 }
1727
1728 Tcl_DecrRefCount(p->pCmp);
1729 Tcl_DecrRefCount(p->pDel);
1730 sqlite3_free((void *)p);
1731}
1732static int testCreateCollationCmp(
1733 void *pCtx,
1734 int nLeft,
1735 const void *zLeft,
1736 int nRight,
1737 const void *zRight
1738){
1739 TestCollationX *p = (TestCollationX *)pCtx;
1740 Tcl_Obj *pScript = Tcl_DuplicateObj(p->pCmp);
1741 int iRes = 0;
1742
1743 Tcl_IncrRefCount(pScript);
1744 Tcl_ListObjAppendElement(0, pScript, Tcl_NewStringObj((char *)zLeft, nLeft));
1745 Tcl_ListObjAppendElement(0, pScript, Tcl_NewStringObj((char *)zRight,nRight));
1746
1747 if( TCL_OK!=Tcl_EvalObjEx(p->interp, pScript, TCL_EVAL_DIRECT|TCL_EVAL_GLOBAL)
1748 || TCL_OK!=Tcl_GetIntFromObj(p->interp, Tcl_GetObjResult(p->interp), &iRes)
1749 ){
1750 Tcl_BackgroundError(p->interp);
1751 }
1752 Tcl_DecrRefCount(pScript);
1753
1754 return iRes;
1755}
danielk1977a393c032007-05-07 14:58:53 +00001756static int test_create_collation_v2(
danielk1977a9808b32007-05-07 09:32:45 +00001757 ClientData clientData, /* Not used */
1758 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1759 int objc, /* Number of arguments */
1760 Tcl_Obj *CONST objv[] /* Command arguments */
1761){
1762 TestCollationX *p;
1763 sqlite3 *db;
drhd55d57e2008-07-07 17:53:07 +00001764 int rc;
danielk1977a9808b32007-05-07 09:32:45 +00001765
1766 if( objc!=5 ){
1767 Tcl_WrongNumArgs(interp, 1, objv, "DB-HANDLE NAME CMP-PROC DEL-PROC");
1768 return TCL_ERROR;
1769 }
1770 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
1771
1772 p = (TestCollationX *)sqlite3_malloc(sizeof(TestCollationX));
1773 p->pCmp = objv[3];
1774 p->pDel = objv[4];
1775 p->interp = interp;
1776 Tcl_IncrRefCount(p->pCmp);
1777 Tcl_IncrRefCount(p->pDel);
1778
drhd55d57e2008-07-07 17:53:07 +00001779 rc = sqlite3_create_collation_v2(db, Tcl_GetString(objv[2]), 16,
1780 (void *)p, testCreateCollationCmp, testCreateCollationDel
1781 );
1782 if( rc!=SQLITE_MISUSE ){
1783 Tcl_AppendResult(interp, "sqlite3_create_collate_v2() failed to detect "
1784 "an invalid encoding", (char*)0);
1785 return TCL_ERROR;
1786 }
1787 rc = sqlite3_create_collation_v2(db, Tcl_GetString(objv[2]), SQLITE_UTF8,
danielk1977a9808b32007-05-07 09:32:45 +00001788 (void *)p, testCreateCollationCmp, testCreateCollationDel
1789 );
1790 return TCL_OK;
1791}
1792
1793/*
danielk197769e777f2006-06-14 10:38:02 +00001794** Usage: sqlite3_load_extension DB-HANDLE FILE ?PROC?
1795*/
1796static int test_load_extension(
1797 ClientData clientData, /* Not used */
1798 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1799 int objc, /* Number of arguments */
1800 Tcl_Obj *CONST objv[] /* Command arguments */
1801){
1802 Tcl_CmdInfo cmdInfo;
1803 sqlite3 *db;
1804 int rc;
1805 char *zDb;
1806 char *zFile;
1807 char *zProc = 0;
1808 char *zErr = 0;
1809
1810 if( objc!=4 && objc!=3 ){
1811 Tcl_WrongNumArgs(interp, 1, objv, "DB-HANDLE FILE ?PROC?");
1812 return TCL_ERROR;
1813 }
1814 zDb = Tcl_GetString(objv[1]);
1815 zFile = Tcl_GetString(objv[2]);
1816 if( objc==4 ){
1817 zProc = Tcl_GetString(objv[3]);
1818 }
1819
1820 /* Extract the C database handle from the Tcl command name */
1821 if( !Tcl_GetCommandInfo(interp, zDb, &cmdInfo) ){
1822 Tcl_AppendResult(interp, "command not found: ", zDb, (char*)0);
1823 return TCL_ERROR;
1824 }
1825 db = ((struct SqliteDb*)cmdInfo.objClientData)->db;
1826 assert(db);
1827
1828 /* Call the underlying C function. If an error occurs, set rc to
1829 ** TCL_ERROR and load any error string into the interpreter. If no
1830 ** error occurs, set rc to TCL_OK.
1831 */
drhc2e87a32006-06-27 15:16:14 +00001832#ifdef SQLITE_OMIT_LOAD_EXTENSION
1833 rc = SQLITE_ERROR;
1834 zErr = sqlite3_mprintf("this build omits sqlite3_load_extension()");
1835#else
danielk197769e777f2006-06-14 10:38:02 +00001836 rc = sqlite3_load_extension(db, zFile, zProc, &zErr);
drhc2e87a32006-06-27 15:16:14 +00001837#endif
danielk197769e777f2006-06-14 10:38:02 +00001838 if( rc!=SQLITE_OK ){
1839 Tcl_SetResult(interp, zErr ? zErr : "", TCL_VOLATILE);
1840 rc = TCL_ERROR;
1841 }else{
1842 rc = TCL_OK;
1843 }
1844 sqlite3_free(zErr);
1845
1846 return rc;
1847}
1848
1849/*
drhc2e87a32006-06-27 15:16:14 +00001850** Usage: sqlite3_enable_load_extension DB-HANDLE ONOFF
1851*/
1852static int test_enable_load(
1853 ClientData clientData, /* Not used */
1854 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1855 int objc, /* Number of arguments */
1856 Tcl_Obj *CONST objv[] /* Command arguments */
1857){
1858 Tcl_CmdInfo cmdInfo;
1859 sqlite3 *db;
1860 char *zDb;
1861 int onoff;
1862
1863 if( objc!=3 ){
1864 Tcl_WrongNumArgs(interp, 1, objv, "DB-HANDLE ONOFF");
1865 return TCL_ERROR;
1866 }
1867 zDb = Tcl_GetString(objv[1]);
1868
1869 /* Extract the C database handle from the Tcl command name */
1870 if( !Tcl_GetCommandInfo(interp, zDb, &cmdInfo) ){
1871 Tcl_AppendResult(interp, "command not found: ", zDb, (char*)0);
1872 return TCL_ERROR;
1873 }
1874 db = ((struct SqliteDb*)cmdInfo.objClientData)->db;
1875 assert(db);
1876
1877 /* Get the onoff parameter */
1878 if( Tcl_GetBooleanFromObj(interp, objv[2], &onoff) ){
1879 return TCL_ERROR;
1880 }
1881
1882#ifdef SQLITE_OMIT_LOAD_EXTENSION
1883 Tcl_AppendResult(interp, "this build omits sqlite3_load_extension()");
1884 return TCL_ERROR;
1885#else
1886 sqlite3_enable_load_extension(db, onoff);
1887 return TCL_OK;
1888#endif
1889}
1890
1891/*
drh28b4e482002-03-11 02:06:13 +00001892** Usage: sqlite_abort
1893**
1894** Shutdown the process immediately. This is not a clean shutdown.
1895** This command is used to test the recoverability of a database in
1896** the event of a program crash.
1897*/
1898static int sqlite_abort(
1899 void *NotUsed,
1900 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1901 int argc, /* Number of arguments */
1902 char **argv /* Text of each argument */
1903){
1904 assert( interp==0 ); /* This will always fail */
1905 return TCL_OK;
1906}
1907
1908/*
drh6cbe1f12002-07-01 00:31:36 +00001909** The following routine is a user-defined SQL function whose purpose
1910** is to test the sqlite_set_result() API.
1911*/
danielk19770ae8b832004-05-25 12:05:56 +00001912static void testFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
drh6cbe1f12002-07-01 00:31:36 +00001913 while( argc>=2 ){
drh03d847e2005-12-09 20:21:58 +00001914 const char *zArg0 = (char*)sqlite3_value_text(argv[0]);
danielk19776d88bad2004-05-27 14:23:36 +00001915 if( zArg0 ){
1916 if( 0==sqlite3StrICmp(zArg0, "int") ){
1917 sqlite3_result_int(context, sqlite3_value_int(argv[1]));
1918 }else if( sqlite3StrICmp(zArg0,"int64")==0 ){
1919 sqlite3_result_int64(context, sqlite3_value_int64(argv[1]));
1920 }else if( sqlite3StrICmp(zArg0,"string")==0 ){
drh03d847e2005-12-09 20:21:58 +00001921 sqlite3_result_text(context, (char*)sqlite3_value_text(argv[1]), -1,
danielk1977d8123362004-06-12 09:25:12 +00001922 SQLITE_TRANSIENT);
danielk19776d88bad2004-05-27 14:23:36 +00001923 }else if( sqlite3StrICmp(zArg0,"double")==0 ){
1924 sqlite3_result_double(context, sqlite3_value_double(argv[1]));
1925 }else if( sqlite3StrICmp(zArg0,"null")==0 ){
1926 sqlite3_result_null(context);
1927 }else if( sqlite3StrICmp(zArg0,"value")==0 ){
1928 sqlite3_result_value(context, argv[sqlite3_value_int(argv[1])]);
1929 }else{
1930 goto error_out;
1931 }
drh6cbe1f12002-07-01 00:31:36 +00001932 }else{
danielk19776d88bad2004-05-27 14:23:36 +00001933 goto error_out;
drh6cbe1f12002-07-01 00:31:36 +00001934 }
1935 argc -= 2;
1936 argv += 2;
1937 }
danielk19776d88bad2004-05-27 14:23:36 +00001938 return;
1939
1940error_out:
1941 sqlite3_result_error(context,"first argument should be one of: "
1942 "int int64 string double null value", -1);
drh6cbe1f12002-07-01 00:31:36 +00001943}
1944
1945/*
1946** Usage: sqlite_register_test_function DB NAME
1947**
1948** Register the test SQL function on the database DB under the name NAME.
1949*/
drhc2eef3b2002-08-31 18:53:06 +00001950static int test_register_func(
drh6cbe1f12002-07-01 00:31:36 +00001951 void *NotUsed,
1952 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1953 int argc, /* Number of arguments */
1954 char **argv /* Text of each argument */
1955){
drh9bb575f2004-09-06 17:24:11 +00001956 sqlite3 *db;
drh6cbe1f12002-07-01 00:31:36 +00001957 int rc;
1958 if( argc!=3 ){
1959 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1960 " DB FUNCTION-NAME", 0);
1961 return TCL_ERROR;
1962 }
drhb86ccfb2003-01-28 23:13:10 +00001963 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
danielk1977f9d64d22004-06-19 08:18:07 +00001964 rc = sqlite3_create_function(db, argv[2], -1, SQLITE_UTF8, 0,
danielk1977d8123362004-06-12 09:25:12 +00001965 testFunc, 0, 0);
drh6cbe1f12002-07-01 00:31:36 +00001966 if( rc!=0 ){
danielk1977f20b21c2004-05-31 23:56:42 +00001967 Tcl_AppendResult(interp, sqlite3ErrStr(rc), 0);
drh6cbe1f12002-07-01 00:31:36 +00001968 return TCL_ERROR;
1969 }
drhc60d0442004-09-30 13:43:13 +00001970 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
drh6cbe1f12002-07-01 00:31:36 +00001971 return TCL_OK;
1972}
1973
1974/*
danielk1977106bb232004-05-21 10:08:53 +00001975** Usage: sqlite3_finalize STMT
drhb86ccfb2003-01-28 23:13:10 +00001976**
danielk1977106bb232004-05-21 10:08:53 +00001977** Finalize a statement handle.
drhb86ccfb2003-01-28 23:13:10 +00001978*/
1979static int test_finalize(
danielk1977106bb232004-05-21 10:08:53 +00001980 void * clientData,
1981 Tcl_Interp *interp,
1982 int objc,
1983 Tcl_Obj *CONST objv[]
drhb86ccfb2003-01-28 23:13:10 +00001984){
danielk1977106bb232004-05-21 10:08:53 +00001985 sqlite3_stmt *pStmt;
drhb86ccfb2003-01-28 23:13:10 +00001986 int rc;
drhdddb2f22007-01-03 23:37:28 +00001987 sqlite3 *db = 0;
danielk1977106bb232004-05-21 10:08:53 +00001988
1989 if( objc!=2 ){
1990 Tcl_AppendResult(interp, "wrong # args: should be \"",
1991 Tcl_GetStringFromObj(objv[0], 0), " <STMT>", 0);
drhb86ccfb2003-01-28 23:13:10 +00001992 return TCL_ERROR;
1993 }
danielk1977106bb232004-05-21 10:08:53 +00001994
1995 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
1996
danielk19774397de52005-01-12 12:44:03 +00001997 if( pStmt ){
1998 db = StmtToDb(pStmt);
1999 }
danielk1977fc57d7b2004-05-26 02:04:57 +00002000 rc = sqlite3_finalize(pStmt);
drh4f0c5872007-03-26 22:05:01 +00002001 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
danielk19774397de52005-01-12 12:44:03 +00002002 if( db && sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
danielk1977106bb232004-05-21 10:08:53 +00002003 return TCL_OK;
2004}
2005
2006/*
drhd1d38482008-10-07 23:46:38 +00002007** Usage: sqlite3_stmt_status STMT CODE RESETFLAG
2008**
2009** Get the value of a status counter from a statement.
2010*/
2011static int test_stmt_status(
2012 void * clientData,
2013 Tcl_Interp *interp,
2014 int objc,
2015 Tcl_Obj *CONST objv[]
2016){
2017 int iValue;
2018 int i, op, resetFlag;
2019 const char *zOpName;
2020 sqlite3_stmt *pStmt;
2021
2022 static const struct {
2023 const char *zName;
2024 int op;
2025 } aOp[] = {
2026 { "SQLITE_STMTSTATUS_FULLSCAN_STEP", SQLITE_STMTSTATUS_FULLSCAN_STEP },
2027 { "SQLITE_STMTSTATUS_SORT", SQLITE_STMTSTATUS_SORT },
drha21a64d2010-04-06 22:33:55 +00002028 { "SQLITE_STMTSTATUS_AUTOINDEX", SQLITE_STMTSTATUS_AUTOINDEX },
drhd1d38482008-10-07 23:46:38 +00002029 };
2030 if( objc!=4 ){
2031 Tcl_WrongNumArgs(interp, 1, objv, "STMT PARAMETER RESETFLAG");
2032 return TCL_ERROR;
2033 }
2034 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2035 zOpName = Tcl_GetString(objv[2]);
2036 for(i=0; i<ArraySize(aOp); i++){
2037 if( strcmp(aOp[i].zName, zOpName)==0 ){
2038 op = aOp[i].op;
2039 break;
2040 }
2041 }
2042 if( i>=ArraySize(aOp) ){
2043 if( Tcl_GetIntFromObj(interp, objv[2], &op) ) return TCL_ERROR;
2044 }
2045 if( Tcl_GetBooleanFromObj(interp, objv[3], &resetFlag) ) return TCL_ERROR;
2046 iValue = sqlite3_stmt_status(pStmt, op, resetFlag);
2047 Tcl_SetObjResult(interp, Tcl_NewIntObj(iValue));
2048 return TCL_OK;
2049}
2050
2051/*
drhbb5a9c32008-06-19 02:52:25 +00002052** Usage: sqlite3_next_stmt DB STMT
2053**
2054** Return the next statment in sequence after STMT.
2055*/
2056static int test_next_stmt(
2057 void * clientData,
2058 Tcl_Interp *interp,
2059 int objc,
2060 Tcl_Obj *CONST objv[]
2061){
2062 sqlite3_stmt *pStmt;
2063 sqlite3 *db = 0;
2064 char zBuf[50];
2065
2066 if( objc!=3 ){
2067 Tcl_AppendResult(interp, "wrong # args: should be \"",
2068 Tcl_GetStringFromObj(objv[0], 0), " DB STMT", 0);
2069 return TCL_ERROR;
2070 }
2071
2072 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
2073 if( getStmtPointer(interp, Tcl_GetString(objv[2]), &pStmt) ) return TCL_ERROR;
2074 pStmt = sqlite3_next_stmt(db, pStmt);
2075 if( pStmt ){
2076 if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR;
2077 Tcl_AppendResult(interp, zBuf, 0);
2078 }
2079 return TCL_OK;
2080}
2081
2082
2083/*
danielk1977106bb232004-05-21 10:08:53 +00002084** Usage: sqlite3_reset STMT
2085**
danielk1977261919c2005-12-06 12:52:59 +00002086** Reset a statement handle.
danielk1977106bb232004-05-21 10:08:53 +00002087*/
2088static int test_reset(
2089 void * clientData,
2090 Tcl_Interp *interp,
2091 int objc,
2092 Tcl_Obj *CONST objv[]
2093){
2094 sqlite3_stmt *pStmt;
2095 int rc;
2096
2097 if( objc!=2 ){
2098 Tcl_AppendResult(interp, "wrong # args: should be \"",
2099 Tcl_GetStringFromObj(objv[0], 0), " <STMT>", 0);
2100 return TCL_ERROR;
2101 }
2102
2103 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2104
danielk1977fc57d7b2004-05-26 02:04:57 +00002105 rc = sqlite3_reset(pStmt);
danielk1977261919c2005-12-06 12:52:59 +00002106 if( pStmt && sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ){
2107 return TCL_ERROR;
2108 }
drh4f0c5872007-03-26 22:05:01 +00002109 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
danielk1977261919c2005-12-06 12:52:59 +00002110/*
danielk1977106bb232004-05-21 10:08:53 +00002111 if( rc ){
drhb86ccfb2003-01-28 23:13:10 +00002112 return TCL_ERROR;
2113 }
danielk1977261919c2005-12-06 12:52:59 +00002114*/
drhb86ccfb2003-01-28 23:13:10 +00002115 return TCL_OK;
2116}
2117
drh5a387052003-01-11 14:19:51 +00002118/*
drhd89bd002005-01-22 03:03:54 +00002119** Usage: sqlite3_expired STMT
2120**
2121** Return TRUE if a recompilation of the statement is recommended.
2122*/
2123static int test_expired(
2124 void * clientData,
2125 Tcl_Interp *interp,
2126 int objc,
2127 Tcl_Obj *CONST objv[]
2128){
shaneeec556d2008-10-12 00:27:53 +00002129#ifndef SQLITE_OMIT_DEPRECATED
drhd89bd002005-01-22 03:03:54 +00002130 sqlite3_stmt *pStmt;
2131 if( objc!=2 ){
2132 Tcl_AppendResult(interp, "wrong # args: should be \"",
2133 Tcl_GetStringFromObj(objv[0], 0), " <STMT>", 0);
2134 return TCL_ERROR;
2135 }
2136 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2137 Tcl_SetObjResult(interp, Tcl_NewBooleanObj(sqlite3_expired(pStmt)));
shaneeec556d2008-10-12 00:27:53 +00002138#endif
drhd89bd002005-01-22 03:03:54 +00002139 return TCL_OK;
2140}
2141
2142/*
drhf8db1bc2005-04-22 02:38:37 +00002143** Usage: sqlite3_transfer_bindings FROMSTMT TOSTMT
2144**
2145** Transfer all bindings from FROMSTMT over to TOSTMT
2146*/
2147static int test_transfer_bind(
2148 void * clientData,
2149 Tcl_Interp *interp,
2150 int objc,
2151 Tcl_Obj *CONST objv[]
2152){
shaneeec556d2008-10-12 00:27:53 +00002153#ifndef SQLITE_OMIT_DEPRECATED
drhf8db1bc2005-04-22 02:38:37 +00002154 sqlite3_stmt *pStmt1, *pStmt2;
2155 if( objc!=3 ){
2156 Tcl_AppendResult(interp, "wrong # args: should be \"",
2157 Tcl_GetStringFromObj(objv[0], 0), " FROM-STMT TO-STMT", 0);
2158 return TCL_ERROR;
2159 }
2160 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt1)) return TCL_ERROR;
2161 if( getStmtPointer(interp, Tcl_GetString(objv[2]), &pStmt2)) return TCL_ERROR;
2162 Tcl_SetObjResult(interp,
2163 Tcl_NewIntObj(sqlite3_transfer_bindings(pStmt1,pStmt2)));
shaneeec556d2008-10-12 00:27:53 +00002164#endif
drhf8db1bc2005-04-22 02:38:37 +00002165 return TCL_OK;
2166}
2167
2168/*
danielk1977fbcd5852004-06-15 02:44:18 +00002169** Usage: sqlite3_changes DB
drh50457892003-09-06 01:10:47 +00002170**
danielk1977fbcd5852004-06-15 02:44:18 +00002171** Return the number of changes made to the database by the last SQL
2172** execution.
drh50457892003-09-06 01:10:47 +00002173*/
danielk1977fbcd5852004-06-15 02:44:18 +00002174static int test_changes(
2175 void * clientData,
2176 Tcl_Interp *interp,
2177 int objc,
2178 Tcl_Obj *CONST objv[]
2179){
2180 sqlite3 *db;
2181 if( objc!=2 ){
2182 Tcl_AppendResult(interp, "wrong # args: should be \"",
2183 Tcl_GetString(objv[0]), " DB", 0);
2184 return TCL_ERROR;
2185 }
2186 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
2187 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_changes(db)));
2188 return TCL_OK;
2189}
drh50457892003-09-06 01:10:47 +00002190
2191/*
drh7c972de2003-09-06 22:18:07 +00002192** This is the "static_bind_value" that variables are bound to when
danielk19776f8a5032004-05-10 10:34:51 +00002193** the FLAG option of sqlite3_bind is "static"
drh50457892003-09-06 01:10:47 +00002194*/
drh7c972de2003-09-06 22:18:07 +00002195static char *sqlite_static_bind_value = 0;
drhf0313812006-09-04 15:53:53 +00002196static int sqlite_static_bind_nbyte = 0;
drh7c972de2003-09-06 22:18:07 +00002197
2198/*
danielk19776f8a5032004-05-10 10:34:51 +00002199** Usage: sqlite3_bind VM IDX VALUE FLAGS
drh7c972de2003-09-06 22:18:07 +00002200**
2201** Sets the value of the IDX-th occurance of "?" in the original SQL
2202** string. VALUE is the new value. If FLAGS=="null" then VALUE is
2203** ignored and the value is set to NULL. If FLAGS=="static" then
2204** the value is set to the value of a static variable named
2205** "sqlite_static_bind_value". If FLAGS=="normal" then a copy
drhbf8aa2a2005-12-02 02:44:05 +00002206** of the VALUE is made. If FLAGS=="blob10" then a VALUE is ignored
2207** an a 10-byte blob "abc\000xyz\000pq" is inserted.
drh7c972de2003-09-06 22:18:07 +00002208*/
2209static int test_bind(
drh50457892003-09-06 01:10:47 +00002210 void *NotUsed,
2211 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
2212 int argc, /* Number of arguments */
2213 char **argv /* Text of each argument */
2214){
danielk1977fc57d7b2004-05-26 02:04:57 +00002215 sqlite3_stmt *pStmt;
drh50457892003-09-06 01:10:47 +00002216 int rc;
drh7c972de2003-09-06 22:18:07 +00002217 int idx;
2218 if( argc!=5 ){
drh50457892003-09-06 01:10:47 +00002219 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
drh7c972de2003-09-06 22:18:07 +00002220 " VM IDX VALUE (null|static|normal)\"", 0);
drh50457892003-09-06 01:10:47 +00002221 return TCL_ERROR;
2222 }
danielk1977fc57d7b2004-05-26 02:04:57 +00002223 if( getStmtPointer(interp, argv[1], &pStmt) ) return TCL_ERROR;
drh7c972de2003-09-06 22:18:07 +00002224 if( Tcl_GetInt(interp, argv[2], &idx) ) return TCL_ERROR;
2225 if( strcmp(argv[4],"null")==0 ){
danielk1977fc57d7b2004-05-26 02:04:57 +00002226 rc = sqlite3_bind_null(pStmt, idx);
drh7c972de2003-09-06 22:18:07 +00002227 }else if( strcmp(argv[4],"static")==0 ){
danielk1977fc57d7b2004-05-26 02:04:57 +00002228 rc = sqlite3_bind_text(pStmt, idx, sqlite_static_bind_value, -1, 0);
drhf0313812006-09-04 15:53:53 +00002229 }else if( strcmp(argv[4],"static-nbytes")==0 ){
2230 rc = sqlite3_bind_text(pStmt, idx, sqlite_static_bind_value,
2231 sqlite_static_bind_nbyte, 0);
drh7c972de2003-09-06 22:18:07 +00002232 }else if( strcmp(argv[4],"normal")==0 ){
danielk1977d8123362004-06-12 09:25:12 +00002233 rc = sqlite3_bind_text(pStmt, idx, argv[3], -1, SQLITE_TRANSIENT);
drhbf8aa2a2005-12-02 02:44:05 +00002234 }else if( strcmp(argv[4],"blob10")==0 ){
2235 rc = sqlite3_bind_text(pStmt, idx, "abc\000xyz\000pq", 10, SQLITE_STATIC);
drh7c972de2003-09-06 22:18:07 +00002236 }else{
2237 Tcl_AppendResult(interp, "4th argument should be "
2238 "\"null\" or \"static\" or \"normal\"", 0);
2239 return TCL_ERROR;
2240 }
drhc60d0442004-09-30 13:43:13 +00002241 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
drh50457892003-09-06 01:10:47 +00002242 if( rc ){
2243 char zBuf[50];
2244 sprintf(zBuf, "(%d) ", rc);
danielk1977f20b21c2004-05-31 23:56:42 +00002245 Tcl_AppendResult(interp, zBuf, sqlite3ErrStr(rc), 0);
drh50457892003-09-06 01:10:47 +00002246 return TCL_ERROR;
2247 }
2248 return TCL_OK;
2249}
2250
drh5436dc22004-11-14 04:04:17 +00002251#ifndef SQLITE_OMIT_UTF16
danielk19774e6af132004-06-10 14:01:08 +00002252/*
2253** Usage: add_test_collate <db ptr> <utf8> <utf16le> <utf16be>
2254**
2255** This function is used to test that SQLite selects the correct collation
2256** sequence callback when multiple versions (for different text encodings)
2257** are available.
2258**
2259** Calling this routine registers the collation sequence "test_collate"
2260** with database handle <db>. The second argument must be a list of three
2261** boolean values. If the first is true, then a version of test_collate is
2262** registered for UTF-8, if the second is true, a version is registered for
2263** UTF-16le, if the third is true, a UTF-16be version is available.
2264** Previous versions of test_collate are deleted.
2265**
2266** The collation sequence test_collate is implemented by calling the
2267** following TCL script:
2268**
2269** "test_collate <enc> <lhs> <rhs>"
2270**
2271** The <lhs> and <rhs> are the two values being compared, encoded in UTF-8.
2272** The <enc> parameter is the encoding of the collation function that
2273** SQLite selected to call. The TCL test script implements the
2274** "test_collate" proc.
2275**
2276** Note that this will only work with one intepreter at a time, as the
2277** interp pointer to use when evaluating the TCL script is stored in
2278** pTestCollateInterp.
2279*/
2280static Tcl_Interp* pTestCollateInterp;
2281static int test_collate_func(
2282 void *pCtx,
2283 int nA, const void *zA,
2284 int nB, const void *zB
2285){
2286 Tcl_Interp *i = pTestCollateInterp;
2287 int encin = (int)pCtx;
2288 int res;
drh4db38a72005-09-01 12:16:28 +00002289 int n;
danielk19774e6af132004-06-10 14:01:08 +00002290
2291 sqlite3_value *pVal;
2292 Tcl_Obj *pX;
2293
2294 pX = Tcl_NewStringObj("test_collate", -1);
2295 Tcl_IncrRefCount(pX);
2296
2297 switch( encin ){
2298 case SQLITE_UTF8:
2299 Tcl_ListObjAppendElement(i,pX,Tcl_NewStringObj("UTF-8",-1));
2300 break;
2301 case SQLITE_UTF16LE:
2302 Tcl_ListObjAppendElement(i,pX,Tcl_NewStringObj("UTF-16LE",-1));
2303 break;
2304 case SQLITE_UTF16BE:
2305 Tcl_ListObjAppendElement(i,pX,Tcl_NewStringObj("UTF-16BE",-1));
2306 break;
2307 default:
2308 assert(0);
2309 }
2310
dan02fa4692009-08-17 17:06:58 +00002311 sqlite3BeginBenignMalloc();
danielk19771e536952007-08-16 10:09:01 +00002312 pVal = sqlite3ValueNew(0);
dan02fa4692009-08-17 17:06:58 +00002313 if( pVal ){
2314 sqlite3ValueSetStr(pVal, nA, zA, encin, SQLITE_STATIC);
2315 n = sqlite3_value_bytes(pVal);
2316 Tcl_ListObjAppendElement(i,pX,
2317 Tcl_NewStringObj((char*)sqlite3_value_text(pVal),n));
2318 sqlite3ValueSetStr(pVal, nB, zB, encin, SQLITE_STATIC);
2319 n = sqlite3_value_bytes(pVal);
2320 Tcl_ListObjAppendElement(i,pX,
2321 Tcl_NewStringObj((char*)sqlite3_value_text(pVal),n));
2322 sqlite3ValueFree(pVal);
2323 }
2324 sqlite3EndBenignMalloc();
danielk19774e6af132004-06-10 14:01:08 +00002325
2326 Tcl_EvalObjEx(i, pX, 0);
2327 Tcl_DecrRefCount(pX);
2328 Tcl_GetIntFromObj(i, Tcl_GetObjResult(i), &res);
2329 return res;
2330}
2331static int test_collate(
2332 void * clientData,
2333 Tcl_Interp *interp,
2334 int objc,
2335 Tcl_Obj *CONST objv[]
2336){
2337 sqlite3 *db;
2338 int val;
danielk1977312d6b32004-06-29 13:18:23 +00002339 sqlite3_value *pVal;
drhc60d0442004-09-30 13:43:13 +00002340 int rc;
danielk19774e6af132004-06-10 14:01:08 +00002341
2342 if( objc!=5 ) goto bad_args;
2343 pTestCollateInterp = interp;
2344 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
2345
2346 if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[2], &val) ) return TCL_ERROR;
drhc60d0442004-09-30 13:43:13 +00002347 rc = sqlite3_create_collation(db, "test_collate", SQLITE_UTF8,
2348 (void *)SQLITE_UTF8, val?test_collate_func:0);
2349 if( rc==SQLITE_OK ){
drheee4c8c2008-02-18 22:24:57 +00002350 const void *zUtf16;
drhc60d0442004-09-30 13:43:13 +00002351 if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[3], &val) ) return TCL_ERROR;
2352 rc = sqlite3_create_collation(db, "test_collate", SQLITE_UTF16LE,
2353 (void *)SQLITE_UTF16LE, val?test_collate_func:0);
2354 if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[4], &val) ) return TCL_ERROR;
danielk1977312d6b32004-06-29 13:18:23 +00002355
drh86f8c192007-08-22 00:39:19 +00002356#if 0
danielk19779a30cf62006-01-18 04:26:07 +00002357 if( sqlite3_iMallocFail>0 ){
2358 sqlite3_iMallocFail++;
2359 }
2360#endif
drhf3a65f72007-08-22 20:18:21 +00002361 sqlite3_mutex_enter(db->mutex);
2362 pVal = sqlite3ValueNew(db);
drhb21c8cd2007-08-21 19:33:56 +00002363 sqlite3ValueSetStr(pVal, -1, "test_collate", SQLITE_UTF8, SQLITE_STATIC);
danielk1977a7a8e142008-02-13 18:25:27 +00002364 zUtf16 = sqlite3ValueText(pVal, SQLITE_UTF16NATIVE);
drhf3a65f72007-08-22 20:18:21 +00002365 if( db->mallocFailed ){
2366 rc = SQLITE_NOMEM;
2367 }else{
danielk1977a7a8e142008-02-13 18:25:27 +00002368 rc = sqlite3_create_collation16(db, zUtf16, SQLITE_UTF16BE,
danielk19779a30cf62006-01-18 04:26:07 +00002369 (void *)SQLITE_UTF16BE, val?test_collate_func:0);
drhf3a65f72007-08-22 20:18:21 +00002370 }
drhc60d0442004-09-30 13:43:13 +00002371 sqlite3ValueFree(pVal);
drhf3a65f72007-08-22 20:18:21 +00002372 sqlite3_mutex_leave(db->mutex);
drhc60d0442004-09-30 13:43:13 +00002373 }
2374 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
danielk19779a30cf62006-01-18 04:26:07 +00002375
2376 if( rc!=SQLITE_OK ){
2377 Tcl_AppendResult(interp, sqlite3TestErrorName(rc), 0);
2378 return TCL_ERROR;
2379 }
danielk19774e6af132004-06-10 14:01:08 +00002380 return TCL_OK;
2381
2382bad_args:
2383 Tcl_AppendResult(interp, "wrong # args: should be \"",
2384 Tcl_GetStringFromObj(objv[0], 0), " <DB> <utf8> <utf16le> <utf16be>", 0);
2385 return TCL_ERROR;
2386}
2387
drh268803a2005-12-14 20:11:30 +00002388/*
2389** When the collation needed callback is invoked, record the name of
2390** the requested collating function here. The recorded name is linked
2391** to a TCL variable and used to make sure that the requested collation
2392** name is correct.
2393*/
2394static char zNeededCollation[200];
2395static char *pzNeededCollation = zNeededCollation;
2396
2397
2398/*
2399** Called when a collating sequence is needed. Registered using
2400** sqlite3_collation_needed16().
2401*/
danielk1977312d6b32004-06-29 13:18:23 +00002402static void test_collate_needed_cb(
2403 void *pCtx,
2404 sqlite3 *db,
2405 int eTextRep,
drh268803a2005-12-14 20:11:30 +00002406 const void *pName
danielk1977312d6b32004-06-29 13:18:23 +00002407){
danielk197714db2662006-01-09 16:12:04 +00002408 int enc = ENC(db);
drh268803a2005-12-14 20:11:30 +00002409 int i;
2410 char *z;
2411 for(z = (char*)pName, i=0; *z || z[1]; z++){
2412 if( *z ) zNeededCollation[i++] = *z;
2413 }
2414 zNeededCollation[i] = 0;
danielk1977312d6b32004-06-29 13:18:23 +00002415 sqlite3_create_collation(
danielk197714db2662006-01-09 16:12:04 +00002416 db, "test_collate", ENC(db), (void *)enc, test_collate_func);
danielk1977312d6b32004-06-29 13:18:23 +00002417}
2418
2419/*
2420** Usage: add_test_collate_needed DB
2421*/
2422static int test_collate_needed(
2423 void * clientData,
2424 Tcl_Interp *interp,
2425 int objc,
2426 Tcl_Obj *CONST objv[]
2427){
2428 sqlite3 *db;
drhc60d0442004-09-30 13:43:13 +00002429 int rc;
danielk1977312d6b32004-06-29 13:18:23 +00002430
2431 if( objc!=2 ) goto bad_args;
2432 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
drhc60d0442004-09-30 13:43:13 +00002433 rc = sqlite3_collation_needed16(db, 0, test_collate_needed_cb);
drh268803a2005-12-14 20:11:30 +00002434 zNeededCollation[0] = 0;
drhc60d0442004-09-30 13:43:13 +00002435 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
danielk1977312d6b32004-06-29 13:18:23 +00002436 return TCL_OK;
2437
2438bad_args:
2439 Tcl_WrongNumArgs(interp, 1, objv, "DB");
2440 return TCL_ERROR;
2441}
drh7d9bd4e2006-02-16 18:16:36 +00002442
2443/*
2444** tclcmd: add_alignment_test_collations DB
2445**
2446** Add two new collating sequences to the database DB
2447**
2448** utf16_aligned
2449** utf16_unaligned
2450**
2451** Both collating sequences use the same sort order as BINARY.
2452** The only difference is that the utf16_aligned collating
2453** sequence is declared with the SQLITE_UTF16_ALIGNED flag.
2454** Both collating functions increment the unaligned utf16 counter
2455** whenever they see a string that begins on an odd byte boundary.
2456*/
2457static int unaligned_string_counter = 0;
2458static int alignmentCollFunc(
2459 void *NotUsed,
2460 int nKey1, const void *pKey1,
2461 int nKey2, const void *pKey2
2462){
2463 int rc, n;
2464 n = nKey1<nKey2 ? nKey1 : nKey2;
2465 if( nKey1>0 && 1==(1&(int)pKey1) ) unaligned_string_counter++;
2466 if( nKey2>0 && 1==(1&(int)pKey2) ) unaligned_string_counter++;
2467 rc = memcmp(pKey1, pKey2, n);
2468 if( rc==0 ){
2469 rc = nKey1 - nKey2;
2470 }
2471 return rc;
2472}
2473static int add_alignment_test_collations(
2474 void * clientData,
2475 Tcl_Interp *interp,
2476 int objc,
2477 Tcl_Obj *CONST objv[]
2478){
2479 sqlite3 *db;
2480 if( objc>=2 ){
2481 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
danielk1977ebb32932009-04-28 15:35:38 +00002482 sqlite3_create_collation(db, "utf16_unaligned", SQLITE_UTF16,
drh7d9bd4e2006-02-16 18:16:36 +00002483 0, alignmentCollFunc);
danielk1977ebb32932009-04-28 15:35:38 +00002484 sqlite3_create_collation(db, "utf16_aligned", SQLITE_UTF16_ALIGNED,
drh7d9bd4e2006-02-16 18:16:36 +00002485 0, alignmentCollFunc);
2486 }
2487 return SQLITE_OK;
2488}
2489#endif /* !defined(SQLITE_OMIT_UTF16) */
danielk1977312d6b32004-06-29 13:18:23 +00002490
danielk1977c8e9a2d2004-06-25 12:08:46 +00002491/*
2492** Usage: add_test_function <db ptr> <utf8> <utf16le> <utf16be>
2493**
2494** This function is used to test that SQLite selects the correct user
2495** function callback when multiple versions (for different text encodings)
2496** are available.
2497**
2498** Calling this routine registers up to three versions of the user function
2499** "test_function" with database handle <db>. If the second argument is
2500** true, then a version of test_function is registered for UTF-8, if the
2501** third is true, a version is registered for UTF-16le, if the fourth is
2502** true, a UTF-16be version is available. Previous versions of
2503** test_function are deleted.
2504**
2505** The user function is implemented by calling the following TCL script:
2506**
2507** "test_function <enc> <arg>"
2508**
2509** Where <enc> is one of UTF-8, UTF-16LE or UTF16BE, and <arg> is the
2510** single argument passed to the SQL function. The value returned by
2511** the TCL script is used as the return value of the SQL function. It
2512** is passed to SQLite using UTF-16BE for a UTF-8 test_function(), UTF-8
2513** for a UTF-16LE test_function(), and UTF-16LE for an implementation that
2514** prefers UTF-16BE.
2515*/
drh5436dc22004-11-14 04:04:17 +00002516#ifndef SQLITE_OMIT_UTF16
danielk1977c8e9a2d2004-06-25 12:08:46 +00002517static void test_function_utf8(
2518 sqlite3_context *pCtx,
2519 int nArg,
2520 sqlite3_value **argv
2521){
2522 Tcl_Interp *interp;
2523 Tcl_Obj *pX;
2524 sqlite3_value *pVal;
2525 interp = (Tcl_Interp *)sqlite3_user_data(pCtx);
2526 pX = Tcl_NewStringObj("test_function", -1);
2527 Tcl_IncrRefCount(pX);
2528 Tcl_ListObjAppendElement(interp, pX, Tcl_NewStringObj("UTF-8", -1));
2529 Tcl_ListObjAppendElement(interp, pX,
drh03d847e2005-12-09 20:21:58 +00002530 Tcl_NewStringObj((char*)sqlite3_value_text(argv[0]), -1));
danielk1977c8e9a2d2004-06-25 12:08:46 +00002531 Tcl_EvalObjEx(interp, pX, 0);
2532 Tcl_DecrRefCount(pX);
2533 sqlite3_result_text(pCtx, Tcl_GetStringResult(interp), -1, SQLITE_TRANSIENT);
danielk19771e536952007-08-16 10:09:01 +00002534 pVal = sqlite3ValueNew(0);
drhb21c8cd2007-08-21 19:33:56 +00002535 sqlite3ValueSetStr(pVal, -1, Tcl_GetStringResult(interp),
danielk1977c8e9a2d2004-06-25 12:08:46 +00002536 SQLITE_UTF8, SQLITE_STATIC);
2537 sqlite3_result_text16be(pCtx, sqlite3_value_text16be(pVal),
2538 -1, SQLITE_TRANSIENT);
2539 sqlite3ValueFree(pVal);
2540}
2541static void test_function_utf16le(
2542 sqlite3_context *pCtx,
2543 int nArg,
2544 sqlite3_value **argv
2545){
2546 Tcl_Interp *interp;
2547 Tcl_Obj *pX;
2548 sqlite3_value *pVal;
2549 interp = (Tcl_Interp *)sqlite3_user_data(pCtx);
2550 pX = Tcl_NewStringObj("test_function", -1);
2551 Tcl_IncrRefCount(pX);
2552 Tcl_ListObjAppendElement(interp, pX, Tcl_NewStringObj("UTF-16LE", -1));
2553 Tcl_ListObjAppendElement(interp, pX,
drh03d847e2005-12-09 20:21:58 +00002554 Tcl_NewStringObj((char*)sqlite3_value_text(argv[0]), -1));
danielk1977c8e9a2d2004-06-25 12:08:46 +00002555 Tcl_EvalObjEx(interp, pX, 0);
2556 Tcl_DecrRefCount(pX);
danielk19771e536952007-08-16 10:09:01 +00002557 pVal = sqlite3ValueNew(0);
drhb21c8cd2007-08-21 19:33:56 +00002558 sqlite3ValueSetStr(pVal, -1, Tcl_GetStringResult(interp),
danielk1977c8e9a2d2004-06-25 12:08:46 +00002559 SQLITE_UTF8, SQLITE_STATIC);
drh03d847e2005-12-09 20:21:58 +00002560 sqlite3_result_text(pCtx,(char*)sqlite3_value_text(pVal),-1,SQLITE_TRANSIENT);
danielk1977c8e9a2d2004-06-25 12:08:46 +00002561 sqlite3ValueFree(pVal);
2562}
2563static void test_function_utf16be(
2564 sqlite3_context *pCtx,
2565 int nArg,
2566 sqlite3_value **argv
2567){
2568 Tcl_Interp *interp;
2569 Tcl_Obj *pX;
2570 sqlite3_value *pVal;
2571 interp = (Tcl_Interp *)sqlite3_user_data(pCtx);
2572 pX = Tcl_NewStringObj("test_function", -1);
2573 Tcl_IncrRefCount(pX);
2574 Tcl_ListObjAppendElement(interp, pX, Tcl_NewStringObj("UTF-16BE", -1));
2575 Tcl_ListObjAppendElement(interp, pX,
drh03d847e2005-12-09 20:21:58 +00002576 Tcl_NewStringObj((char*)sqlite3_value_text(argv[0]), -1));
danielk1977c8e9a2d2004-06-25 12:08:46 +00002577 Tcl_EvalObjEx(interp, pX, 0);
2578 Tcl_DecrRefCount(pX);
danielk19771e536952007-08-16 10:09:01 +00002579 pVal = sqlite3ValueNew(0);
drhb21c8cd2007-08-21 19:33:56 +00002580 sqlite3ValueSetStr(pVal, -1, Tcl_GetStringResult(interp),
danielk1977c8e9a2d2004-06-25 12:08:46 +00002581 SQLITE_UTF8, SQLITE_STATIC);
drhde4fcfd2008-01-19 23:50:26 +00002582 sqlite3_result_text16(pCtx, sqlite3_value_text16le(pVal),
2583 -1, SQLITE_TRANSIENT);
2584 sqlite3_result_text16be(pCtx, sqlite3_value_text16le(pVal),
2585 -1, SQLITE_TRANSIENT);
danielk1977c8e9a2d2004-06-25 12:08:46 +00002586 sqlite3_result_text16le(pCtx, sqlite3_value_text16le(pVal),
2587 -1, SQLITE_TRANSIENT);
2588 sqlite3ValueFree(pVal);
2589}
drh5436dc22004-11-14 04:04:17 +00002590#endif /* SQLITE_OMIT_UTF16 */
danielk1977c8e9a2d2004-06-25 12:08:46 +00002591static int test_function(
2592 void * clientData,
2593 Tcl_Interp *interp,
2594 int objc,
2595 Tcl_Obj *CONST objv[]
2596){
drh5436dc22004-11-14 04:04:17 +00002597#ifndef SQLITE_OMIT_UTF16
danielk1977c8e9a2d2004-06-25 12:08:46 +00002598 sqlite3 *db;
2599 int val;
2600
2601 if( objc!=5 ) goto bad_args;
2602 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
2603
2604 if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[2], &val) ) return TCL_ERROR;
2605 if( val ){
2606 sqlite3_create_function(db, "test_function", 1, SQLITE_UTF8,
2607 interp, test_function_utf8, 0, 0);
2608 }
2609 if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[3], &val) ) return TCL_ERROR;
2610 if( val ){
2611 sqlite3_create_function(db, "test_function", 1, SQLITE_UTF16LE,
2612 interp, test_function_utf16le, 0, 0);
2613 }
2614 if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[4], &val) ) return TCL_ERROR;
2615 if( val ){
2616 sqlite3_create_function(db, "test_function", 1, SQLITE_UTF16BE,
2617 interp, test_function_utf16be, 0, 0);
2618 }
2619
2620 return TCL_OK;
2621bad_args:
2622 Tcl_AppendResult(interp, "wrong # args: should be \"",
2623 Tcl_GetStringFromObj(objv[0], 0), " <DB> <utf8> <utf16le> <utf16be>", 0);
drh5436dc22004-11-14 04:04:17 +00002624#endif /* SQLITE_OMIT_UTF16 */
danielk1977c8e9a2d2004-06-25 12:08:46 +00002625 return TCL_ERROR;
2626}
2627
danielk1977312d6b32004-06-29 13:18:23 +00002628/*
2629** Usage: test_errstr <err code>
2630**
2631** Test that the english language string equivalents for sqlite error codes
2632** are sane. The parameter is an integer representing an sqlite error code.
2633** The result is a list of two elements, the string representation of the
2634** error code and the english language explanation.
2635*/
2636static int test_errstr(
2637 void * clientData,
2638 Tcl_Interp *interp,
2639 int objc,
2640 Tcl_Obj *CONST objv[]
2641){
2642 char *zCode;
2643 int i;
2644 if( objc!=1 ){
2645 Tcl_WrongNumArgs(interp, 1, objv, "<error code>");
2646 }
2647
2648 zCode = Tcl_GetString(objv[1]);
2649 for(i=0; i<200; i++){
drh4f0c5872007-03-26 22:05:01 +00002650 if( 0==strcmp(t1ErrorName(i), zCode) ) break;
danielk1977312d6b32004-06-29 13:18:23 +00002651 }
2652 Tcl_SetResult(interp, (char *)sqlite3ErrStr(i), 0);
2653 return TCL_OK;
2654}
2655
drh50457892003-09-06 01:10:47 +00002656/*
drh99ee3602003-02-16 19:13:36 +00002657** Usage: breakpoint
2658**
2659** This routine exists for one purpose - to provide a place to put a
2660** breakpoint with GDB that can be triggered using TCL code. The use
2661** for this is when a particular test fails on (say) the 1485th iteration.
2662** In the TCL test script, we can add code like this:
2663**
2664** if {$i==1485} breakpoint
2665**
2666** Then run testfixture in the debugger and wait for the breakpoint to
2667** fire. Then additional breakpoints can be set to trace down the bug.
2668*/
2669static int test_breakpoint(
2670 void *NotUsed,
2671 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
2672 int argc, /* Number of arguments */
2673 char **argv /* Text of each argument */
2674){
2675 return TCL_OK; /* Do nothing */
2676}
2677
drh241db312004-06-22 12:46:53 +00002678/*
drhb026e052007-05-02 01:34:31 +00002679** Usage: sqlite3_bind_zeroblob STMT IDX N
2680**
2681** Test the sqlite3_bind_zeroblob interface. STMT is a prepared statement.
2682** IDX is the index of a wildcard in the prepared statement. This command
2683** binds a N-byte zero-filled BLOB to the wildcard.
2684*/
2685static int test_bind_zeroblob(
2686 void * clientData,
2687 Tcl_Interp *interp,
2688 int objc,
2689 Tcl_Obj *CONST objv[]
2690){
2691 sqlite3_stmt *pStmt;
2692 int idx;
2693 int n;
2694 int rc;
2695
2696 if( objc!=4 ){
danielk197728c66302007-09-01 11:04:26 +00002697 Tcl_WrongNumArgs(interp, 1, objv, "STMT IDX N");
drhb026e052007-05-02 01:34:31 +00002698 return TCL_ERROR;
2699 }
2700
2701 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2702 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
2703 if( Tcl_GetIntFromObj(interp, objv[3], &n) ) return TCL_ERROR;
2704
2705 rc = sqlite3_bind_zeroblob(pStmt, idx, n);
2706 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
2707 if( rc!=SQLITE_OK ){
2708 return TCL_ERROR;
2709 }
2710
2711 return TCL_OK;
2712}
2713
2714/*
drh241db312004-06-22 12:46:53 +00002715** Usage: sqlite3_bind_int STMT N VALUE
2716**
2717** Test the sqlite3_bind_int interface. STMT is a prepared statement.
2718** N is the index of a wildcard in the prepared statement. This command
2719** binds a 32-bit integer VALUE to that wildcard.
2720*/
2721static int test_bind_int(
danielk197751e3d8e2004-05-20 01:12:34 +00002722 void * clientData,
2723 Tcl_Interp *interp,
2724 int objc,
2725 Tcl_Obj *CONST objv[]
2726){
2727 sqlite3_stmt *pStmt;
2728 int idx;
2729 int value;
2730 int rc;
2731
2732 if( objc!=4 ){
2733 Tcl_AppendResult(interp, "wrong # args: should be \"",
drh241db312004-06-22 12:46:53 +00002734 Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE", 0);
danielk197751e3d8e2004-05-20 01:12:34 +00002735 return TCL_ERROR;
2736 }
2737
2738 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2739 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
2740 if( Tcl_GetIntFromObj(interp, objv[3], &value) ) return TCL_ERROR;
2741
danielk1977c572ef72004-05-27 09:28:41 +00002742 rc = sqlite3_bind_int(pStmt, idx, value);
drhc60d0442004-09-30 13:43:13 +00002743 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
danielk197751e3d8e2004-05-20 01:12:34 +00002744 if( rc!=SQLITE_OK ){
2745 return TCL_ERROR;
2746 }
2747
2748 return TCL_OK;
2749}
2750
drh241db312004-06-22 12:46:53 +00002751
2752/*
2753** Usage: sqlite3_bind_int64 STMT N VALUE
2754**
2755** Test the sqlite3_bind_int64 interface. STMT is a prepared statement.
2756** N is the index of a wildcard in the prepared statement. This command
2757** binds a 64-bit integer VALUE to that wildcard.
2758*/
danielk197751e3d8e2004-05-20 01:12:34 +00002759static int test_bind_int64(
2760 void * clientData,
2761 Tcl_Interp *interp,
2762 int objc,
2763 Tcl_Obj *CONST objv[]
2764){
2765 sqlite3_stmt *pStmt;
2766 int idx;
2767 i64 value;
2768 int rc;
2769
2770 if( objc!=4 ){
2771 Tcl_AppendResult(interp, "wrong # args: should be \"",
drh241db312004-06-22 12:46:53 +00002772 Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE", 0);
danielk197751e3d8e2004-05-20 01:12:34 +00002773 return TCL_ERROR;
2774 }
2775
2776 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2777 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
2778 if( Tcl_GetWideIntFromObj(interp, objv[3], &value) ) return TCL_ERROR;
2779
2780 rc = sqlite3_bind_int64(pStmt, idx, value);
drhc60d0442004-09-30 13:43:13 +00002781 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
danielk197751e3d8e2004-05-20 01:12:34 +00002782 if( rc!=SQLITE_OK ){
2783 return TCL_ERROR;
2784 }
2785
2786 return TCL_OK;
2787}
2788
drh241db312004-06-22 12:46:53 +00002789
2790/*
2791** Usage: sqlite3_bind_double STMT N VALUE
2792**
2793** Test the sqlite3_bind_double interface. STMT is a prepared statement.
2794** N is the index of a wildcard in the prepared statement. This command
2795** binds a 64-bit integer VALUE to that wildcard.
2796*/
danielk197751e3d8e2004-05-20 01:12:34 +00002797static int test_bind_double(
2798 void * clientData,
2799 Tcl_Interp *interp,
2800 int objc,
2801 Tcl_Obj *CONST objv[]
2802){
2803 sqlite3_stmt *pStmt;
2804 int idx;
2805 double value;
2806 int rc;
drha06f17f2008-05-11 11:07:06 +00002807 const char *zVal;
2808 int i;
2809 static const struct {
2810 const char *zName; /* Name of the special floating point value */
2811 unsigned int iUpper; /* Upper 32 bits */
2812 unsigned int iLower; /* Lower 32 bits */
2813 } aSpecialFp[] = {
2814 { "NaN", 0x7fffffff, 0xffffffff },
2815 { "SNaN", 0x7ff7ffff, 0xffffffff },
2816 { "-NaN", 0xffffffff, 0xffffffff },
2817 { "-SNaN", 0xfff7ffff, 0xffffffff },
2818 { "+Inf", 0x7ff00000, 0x00000000 },
2819 { "-Inf", 0xfff00000, 0x00000000 },
2820 { "Epsilon", 0x00000000, 0x00000001 },
2821 { "-Epsilon", 0x80000000, 0x00000001 },
2822 { "NaN0", 0x7ff80000, 0x00000000 },
2823 { "-NaN0", 0xfff80000, 0x00000000 },
2824 };
danielk197751e3d8e2004-05-20 01:12:34 +00002825
2826 if( objc!=4 ){
2827 Tcl_AppendResult(interp, "wrong # args: should be \"",
drh241db312004-06-22 12:46:53 +00002828 Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE", 0);
danielk197751e3d8e2004-05-20 01:12:34 +00002829 return TCL_ERROR;
2830 }
2831
2832 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2833 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
danielk197751e3d8e2004-05-20 01:12:34 +00002834
drh394f07e2008-04-28 15:23:02 +00002835 /* Intercept the string "NaN" and generate a NaN value for it.
2836 ** All other strings are passed through to Tcl_GetDoubleFromObj().
2837 ** Tcl_GetDoubleFromObj() should understand "NaN" but some versions
2838 ** contain a bug.
2839 */
drha06f17f2008-05-11 11:07:06 +00002840 zVal = Tcl_GetString(objv[3]);
2841 for(i=0; i<sizeof(aSpecialFp)/sizeof(aSpecialFp[0]); i++){
2842 if( strcmp(aSpecialFp[i].zName, zVal)==0 ){
2843 sqlite3_uint64 x;
2844 x = aSpecialFp[i].iUpper;
2845 x <<= 32;
2846 x |= aSpecialFp[i].iLower;
drh0a667332008-05-11 17:22:01 +00002847 assert( sizeof(value)==8 );
2848 assert( sizeof(x)==8 );
2849 memcpy(&value, &x, 8);
drha06f17f2008-05-11 11:07:06 +00002850 break;
2851 }
2852 }
2853 if( i>=sizeof(aSpecialFp)/sizeof(aSpecialFp[0]) &&
2854 Tcl_GetDoubleFromObj(interp, objv[3], &value) ){
drh394f07e2008-04-28 15:23:02 +00002855 return TCL_ERROR;
2856 }
danielk197751e3d8e2004-05-20 01:12:34 +00002857 rc = sqlite3_bind_double(pStmt, idx, value);
drhc60d0442004-09-30 13:43:13 +00002858 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
danielk197751e3d8e2004-05-20 01:12:34 +00002859 if( rc!=SQLITE_OK ){
2860 return TCL_ERROR;
2861 }
2862
2863 return TCL_OK;
2864}
2865
drh241db312004-06-22 12:46:53 +00002866/*
2867** Usage: sqlite3_bind_null STMT N
2868**
2869** Test the sqlite3_bind_null interface. STMT is a prepared statement.
2870** N is the index of a wildcard in the prepared statement. This command
2871** binds a NULL to the wildcard.
2872*/
danielk197751e3d8e2004-05-20 01:12:34 +00002873static int test_bind_null(
2874 void * clientData,
2875 Tcl_Interp *interp,
2876 int objc,
2877 Tcl_Obj *CONST objv[]
2878){
2879 sqlite3_stmt *pStmt;
2880 int idx;
2881 int rc;
2882
2883 if( objc!=3 ){
2884 Tcl_AppendResult(interp, "wrong # args: should be \"",
drh241db312004-06-22 12:46:53 +00002885 Tcl_GetStringFromObj(objv[0], 0), " STMT N", 0);
danielk197751e3d8e2004-05-20 01:12:34 +00002886 return TCL_ERROR;
2887 }
2888
2889 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2890 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
2891
2892 rc = sqlite3_bind_null(pStmt, idx);
drhc60d0442004-09-30 13:43:13 +00002893 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
danielk197751e3d8e2004-05-20 01:12:34 +00002894 if( rc!=SQLITE_OK ){
2895 return TCL_ERROR;
2896 }
2897
2898 return TCL_OK;
2899}
2900
drh241db312004-06-22 12:46:53 +00002901/*
2902** Usage: sqlite3_bind_text STMT N STRING BYTES
2903**
2904** Test the sqlite3_bind_text interface. STMT is a prepared statement.
2905** N is the index of a wildcard in the prepared statement. This command
2906** binds a UTF-8 string STRING to the wildcard. The string is BYTES bytes
2907** long.
2908*/
danielk197751e3d8e2004-05-20 01:12:34 +00002909static int test_bind_text(
2910 void * clientData,
2911 Tcl_Interp *interp,
2912 int objc,
2913 Tcl_Obj *CONST objv[]
2914){
2915 sqlite3_stmt *pStmt;
2916 int idx;
2917 int bytes;
2918 char *value;
2919 int rc;
2920
2921 if( objc!=5 ){
2922 Tcl_AppendResult(interp, "wrong # args: should be \"",
drh241db312004-06-22 12:46:53 +00002923 Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE BYTES", 0);
danielk197751e3d8e2004-05-20 01:12:34 +00002924 return TCL_ERROR;
2925 }
2926
2927 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2928 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
drh10dfbbb2008-04-16 12:58:53 +00002929 value = (char*)Tcl_GetByteArrayFromObj(objv[3], &bytes);
danielk197751e3d8e2004-05-20 01:12:34 +00002930 if( Tcl_GetIntFromObj(interp, objv[4], &bytes) ) return TCL_ERROR;
2931
danielk1977d8123362004-06-12 09:25:12 +00002932 rc = sqlite3_bind_text(pStmt, idx, value, bytes, SQLITE_TRANSIENT);
drhc60d0442004-09-30 13:43:13 +00002933 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
danielk197751e3d8e2004-05-20 01:12:34 +00002934 if( rc!=SQLITE_OK ){
drh473d1792005-06-06 17:54:55 +00002935 Tcl_AppendResult(interp, sqlite3TestErrorName(rc), 0);
danielk197751e3d8e2004-05-20 01:12:34 +00002936 return TCL_ERROR;
2937 }
2938
2939 return TCL_OK;
2940}
2941
drh241db312004-06-22 12:46:53 +00002942/*
danielk1977161fb792006-01-24 10:58:21 +00002943** Usage: sqlite3_bind_text16 ?-static? STMT N STRING BYTES
drh241db312004-06-22 12:46:53 +00002944**
2945** Test the sqlite3_bind_text16 interface. STMT is a prepared statement.
2946** N is the index of a wildcard in the prepared statement. This command
2947** binds a UTF-16 string STRING to the wildcard. The string is BYTES bytes
2948** long.
2949*/
danielk197751e3d8e2004-05-20 01:12:34 +00002950static int test_bind_text16(
2951 void * clientData,
2952 Tcl_Interp *interp,
2953 int objc,
2954 Tcl_Obj *CONST objv[]
2955){
drh5436dc22004-11-14 04:04:17 +00002956#ifndef SQLITE_OMIT_UTF16
danielk197751e3d8e2004-05-20 01:12:34 +00002957 sqlite3_stmt *pStmt;
2958 int idx;
2959 int bytes;
2960 char *value;
2961 int rc;
2962
danielk1977161fb792006-01-24 10:58:21 +00002963 void (*xDel)() = (objc==6?SQLITE_STATIC:SQLITE_TRANSIENT);
2964 Tcl_Obj *oStmt = objv[objc-4];
2965 Tcl_Obj *oN = objv[objc-3];
2966 Tcl_Obj *oString = objv[objc-2];
2967 Tcl_Obj *oBytes = objv[objc-1];
2968
2969 if( objc!=5 && objc!=6){
danielk197751e3d8e2004-05-20 01:12:34 +00002970 Tcl_AppendResult(interp, "wrong # args: should be \"",
drh241db312004-06-22 12:46:53 +00002971 Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE BYTES", 0);
danielk197751e3d8e2004-05-20 01:12:34 +00002972 return TCL_ERROR;
2973 }
2974
danielk1977161fb792006-01-24 10:58:21 +00002975 if( getStmtPointer(interp, Tcl_GetString(oStmt), &pStmt) ) return TCL_ERROR;
2976 if( Tcl_GetIntFromObj(interp, oN, &idx) ) return TCL_ERROR;
2977 value = (char*)Tcl_GetByteArrayFromObj(oString, 0);
2978 if( Tcl_GetIntFromObj(interp, oBytes, &bytes) ) return TCL_ERROR;
danielk197751e3d8e2004-05-20 01:12:34 +00002979
danielk1977161fb792006-01-24 10:58:21 +00002980 rc = sqlite3_bind_text16(pStmt, idx, (void *)value, bytes, xDel);
drhc60d0442004-09-30 13:43:13 +00002981 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
danielk197751e3d8e2004-05-20 01:12:34 +00002982 if( rc!=SQLITE_OK ){
drhddff9ae2008-07-08 15:26:49 +00002983 Tcl_AppendResult(interp, sqlite3TestErrorName(rc), 0);
danielk197751e3d8e2004-05-20 01:12:34 +00002984 return TCL_ERROR;
2985 }
2986
drh5436dc22004-11-14 04:04:17 +00002987#endif /* SQLITE_OMIT_UTF16 */
danielk197751e3d8e2004-05-20 01:12:34 +00002988 return TCL_OK;
2989}
2990
drh241db312004-06-22 12:46:53 +00002991/*
danielk19775b159dc2007-05-17 16:34:43 +00002992** Usage: sqlite3_bind_blob ?-static? STMT N DATA BYTES
drh241db312004-06-22 12:46:53 +00002993**
2994** Test the sqlite3_bind_blob interface. STMT is a prepared statement.
2995** N is the index of a wildcard in the prepared statement. This command
2996** binds a BLOB to the wildcard. The BLOB is BYTES bytes in size.
2997*/
danielk197751e3d8e2004-05-20 01:12:34 +00002998static int test_bind_blob(
2999 void * clientData,
3000 Tcl_Interp *interp,
3001 int objc,
3002 Tcl_Obj *CONST objv[]
3003){
3004 sqlite3_stmt *pStmt;
3005 int idx;
3006 int bytes;
3007 char *value;
3008 int rc;
danielk19775b159dc2007-05-17 16:34:43 +00003009 sqlite3_destructor_type xDestructor = SQLITE_TRANSIENT;
danielk197751e3d8e2004-05-20 01:12:34 +00003010
danielk19775b159dc2007-05-17 16:34:43 +00003011 if( objc!=5 && objc!=6 ){
danielk197751e3d8e2004-05-20 01:12:34 +00003012 Tcl_AppendResult(interp, "wrong # args: should be \"",
drh241db312004-06-22 12:46:53 +00003013 Tcl_GetStringFromObj(objv[0], 0), " STMT N DATA BYTES", 0);
danielk197751e3d8e2004-05-20 01:12:34 +00003014 return TCL_ERROR;
3015 }
3016
danielk19775b159dc2007-05-17 16:34:43 +00003017 if( objc==6 ){
3018 xDestructor = SQLITE_STATIC;
3019 objv++;
3020 }
3021
danielk197751e3d8e2004-05-20 01:12:34 +00003022 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3023 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
3024 value = Tcl_GetString(objv[3]);
danielk197749e46432004-05-27 13:55:27 +00003025 if( Tcl_GetIntFromObj(interp, objv[4], &bytes) ) return TCL_ERROR;
danielk197751e3d8e2004-05-20 01:12:34 +00003026
danielk19775b159dc2007-05-17 16:34:43 +00003027 rc = sqlite3_bind_blob(pStmt, idx, value, bytes, xDestructor);
drhc60d0442004-09-30 13:43:13 +00003028 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
danielk197751e3d8e2004-05-20 01:12:34 +00003029 if( rc!=SQLITE_OK ){
3030 return TCL_ERROR;
3031 }
3032
3033 return TCL_OK;
3034}
3035
drh99ee3602003-02-16 19:13:36 +00003036/*
drh75f6a032004-07-15 14:15:00 +00003037** Usage: sqlite3_bind_parameter_count STMT
3038**
3039** Return the number of wildcards in the given statement.
3040*/
3041static int test_bind_parameter_count(
3042 void * clientData,
3043 Tcl_Interp *interp,
3044 int objc,
3045 Tcl_Obj *CONST objv[]
3046){
3047 sqlite3_stmt *pStmt;
3048
3049 if( objc!=2 ){
3050 Tcl_WrongNumArgs(interp, 1, objv, "STMT");
3051 return TCL_ERROR;
3052 }
3053 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3054 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_bind_parameter_count(pStmt)));
3055 return TCL_OK;
3056}
3057
3058/*
drh895d7472004-08-20 16:02:39 +00003059** Usage: sqlite3_bind_parameter_name STMT N
3060**
3061** Return the name of the Nth wildcard. The first wildcard is 1.
3062** An empty string is returned if N is out of range or if the wildcard
3063** is nameless.
3064*/
3065static int test_bind_parameter_name(
3066 void * clientData,
3067 Tcl_Interp *interp,
3068 int objc,
3069 Tcl_Obj *CONST objv[]
3070){
3071 sqlite3_stmt *pStmt;
3072 int i;
3073
3074 if( objc!=3 ){
3075 Tcl_WrongNumArgs(interp, 1, objv, "STMT N");
3076 return TCL_ERROR;
3077 }
3078 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3079 if( Tcl_GetIntFromObj(interp, objv[2], &i) ) return TCL_ERROR;
3080 Tcl_SetObjResult(interp,
3081 Tcl_NewStringObj(sqlite3_bind_parameter_name(pStmt,i),-1)
3082 );
3083 return TCL_OK;
3084}
3085
3086/*
drhfa6bc002004-09-07 16:19:52 +00003087** Usage: sqlite3_bind_parameter_index STMT NAME
3088**
3089** Return the index of the wildcard called NAME. Return 0 if there is
3090** no such wildcard.
3091*/
3092static int test_bind_parameter_index(
3093 void * clientData,
3094 Tcl_Interp *interp,
3095 int objc,
3096 Tcl_Obj *CONST objv[]
3097){
3098 sqlite3_stmt *pStmt;
3099
3100 if( objc!=3 ){
3101 Tcl_WrongNumArgs(interp, 1, objv, "STMT NAME");
3102 return TCL_ERROR;
3103 }
3104 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3105 Tcl_SetObjResult(interp,
3106 Tcl_NewIntObj(
3107 sqlite3_bind_parameter_index(pStmt,Tcl_GetString(objv[2]))
3108 )
3109 );
3110 return TCL_OK;
3111}
3112
3113/*
danielk1977600dd0b2005-01-20 01:14:23 +00003114** Usage: sqlite3_clear_bindings STMT
3115**
3116*/
danielk1977600dd0b2005-01-20 01:14:23 +00003117static int test_clear_bindings(
3118 void * clientData,
3119 Tcl_Interp *interp,
3120 int objc,
3121 Tcl_Obj *CONST objv[]
3122){
3123 sqlite3_stmt *pStmt;
3124
3125 if( objc!=2 ){
3126 Tcl_WrongNumArgs(interp, 1, objv, "STMT");
3127 return TCL_ERROR;
3128 }
3129 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3130 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_clear_bindings(pStmt)));
3131 return TCL_OK;
3132}
drhf9cb7f52006-06-27 20:06:44 +00003133
3134/*
3135** Usage: sqlite3_sleep MILLISECONDS
3136*/
3137static int test_sleep(
3138 void * clientData,
3139 Tcl_Interp *interp,
3140 int objc,
3141 Tcl_Obj *CONST objv[]
3142){
3143 int ms;
3144
3145 if( objc!=2 ){
3146 Tcl_WrongNumArgs(interp, 1, objv, "MILLISECONDS");
3147 return TCL_ERROR;
3148 }
3149 if( Tcl_GetIntFromObj(interp, objv[1], &ms) ){
3150 return TCL_ERROR;
3151 }
3152 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_sleep(ms)));
3153 return TCL_OK;
3154}
danielk1977600dd0b2005-01-20 01:14:23 +00003155
3156/*
drh99dfe5e2008-10-30 15:03:15 +00003157** Usage: sqlite3_extended_errcode DB
3158**
3159** Return the string representation of the most recent sqlite3_* API
3160** error code. e.g. "SQLITE_ERROR".
3161*/
3162static int test_ex_errcode(
3163 void * clientData,
3164 Tcl_Interp *interp,
3165 int objc,
3166 Tcl_Obj *CONST objv[]
3167){
3168 sqlite3 *db;
3169 int rc;
3170
3171 if( objc!=2 ){
3172 Tcl_AppendResult(interp, "wrong # args: should be \"",
3173 Tcl_GetString(objv[0]), " DB", 0);
3174 return TCL_ERROR;
3175 }
3176 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
3177 rc = sqlite3_extended_errcode(db);
3178 Tcl_AppendResult(interp, (char *)t1ErrorName(rc), 0);
3179 return TCL_OK;
3180}
3181
3182
3183/*
danielk19776622cce2004-05-20 11:00:52 +00003184** Usage: sqlite3_errcode DB
3185**
3186** Return the string representation of the most recent sqlite3_* API
3187** error code. e.g. "SQLITE_ERROR".
3188*/
3189static int test_errcode(
3190 void * clientData,
3191 Tcl_Interp *interp,
3192 int objc,
3193 Tcl_Obj *CONST objv[]
3194){
3195 sqlite3 *db;
drh4ac285a2006-09-15 07:28:50 +00003196 int rc;
danielk19776622cce2004-05-20 11:00:52 +00003197
3198 if( objc!=2 ){
3199 Tcl_AppendResult(interp, "wrong # args: should be \"",
3200 Tcl_GetString(objv[0]), " DB", 0);
3201 return TCL_ERROR;
3202 }
3203 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
drh4ac285a2006-09-15 07:28:50 +00003204 rc = sqlite3_errcode(db);
drh99dfe5e2008-10-30 15:03:15 +00003205 Tcl_AppendResult(interp, (char *)t1ErrorName(rc), 0);
danielk19776622cce2004-05-20 11:00:52 +00003206 return TCL_OK;
3207}
3208
3209/*
danielk197704103022009-02-03 16:51:24 +00003210** Usage: sqlite3_errmsg DB
danielk19776622cce2004-05-20 11:00:52 +00003211**
3212** Returns the UTF-8 representation of the error message string for the
3213** most recent sqlite3_* API call.
3214*/
3215static int test_errmsg(
3216 void * clientData,
3217 Tcl_Interp *interp,
3218 int objc,
3219 Tcl_Obj *CONST objv[]
3220){
drh9bb575f2004-09-06 17:24:11 +00003221 sqlite3 *db;
danielk19776622cce2004-05-20 11:00:52 +00003222 const char *zErr;
3223
3224 if( objc!=2 ){
3225 Tcl_AppendResult(interp, "wrong # args: should be \"",
3226 Tcl_GetString(objv[0]), " DB", 0);
3227 return TCL_ERROR;
3228 }
3229 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
3230
3231 zErr = sqlite3_errmsg(db);
3232 Tcl_SetObjResult(interp, Tcl_NewStringObj(zErr, -1));
3233 return TCL_OK;
3234}
3235
3236/*
3237** Usage: test_errmsg16 DB
3238**
3239** Returns the UTF-16 representation of the error message string for the
3240** most recent sqlite3_* API call. This is a byte array object at the TCL
3241** level, and it includes the 0x00 0x00 terminator bytes at the end of the
3242** UTF-16 string.
3243*/
3244static int test_errmsg16(
3245 void * clientData,
3246 Tcl_Interp *interp,
3247 int objc,
3248 Tcl_Obj *CONST objv[]
3249){
drh5436dc22004-11-14 04:04:17 +00003250#ifndef SQLITE_OMIT_UTF16
drh9bb575f2004-09-06 17:24:11 +00003251 sqlite3 *db;
danielk19776622cce2004-05-20 11:00:52 +00003252 const void *zErr;
drhaed382f2009-04-01 18:40:32 +00003253 const char *z;
danielk1977950f0542006-01-18 05:51:57 +00003254 int bytes = 0;
danielk19776622cce2004-05-20 11:00:52 +00003255
3256 if( objc!=2 ){
3257 Tcl_AppendResult(interp, "wrong # args: should be \"",
3258 Tcl_GetString(objv[0]), " DB", 0);
3259 return TCL_ERROR;
3260 }
3261 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
3262
3263 zErr = sqlite3_errmsg16(db);
danielk1977950f0542006-01-18 05:51:57 +00003264 if( zErr ){
drhaed382f2009-04-01 18:40:32 +00003265 z = zErr;
3266 for(bytes=0; z[bytes] || z[bytes+1]; bytes+=2){}
danielk1977950f0542006-01-18 05:51:57 +00003267 }
danielk19776622cce2004-05-20 11:00:52 +00003268 Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(zErr, bytes));
drh5436dc22004-11-14 04:04:17 +00003269#endif /* SQLITE_OMIT_UTF16 */
danielk19776622cce2004-05-20 11:00:52 +00003270 return TCL_OK;
3271}
3272
3273/*
drh1c767f02009-01-09 02:49:31 +00003274** Usage: sqlite3_prepare DB sql bytes ?tailvar?
danielk19776622cce2004-05-20 11:00:52 +00003275**
3276** Compile up to <bytes> bytes of the supplied SQL string <sql> using
3277** database handle <DB>. The parameter <tailval> is the name of a global
3278** variable that is set to the unused portion of <sql> (if any). A
3279** STMT handle is returned.
3280*/
3281static int test_prepare(
3282 void * clientData,
3283 Tcl_Interp *interp,
3284 int objc,
3285 Tcl_Obj *CONST objv[]
3286){
3287 sqlite3 *db;
3288 const char *zSql;
3289 int bytes;
3290 const char *zTail = 0;
3291 sqlite3_stmt *pStmt = 0;
3292 char zBuf[50];
danielk19774ad17132004-05-21 01:47:26 +00003293 int rc;
danielk19776622cce2004-05-20 11:00:52 +00003294
drh1c767f02009-01-09 02:49:31 +00003295 if( objc!=5 && objc!=4 ){
danielk19776622cce2004-05-20 11:00:52 +00003296 Tcl_AppendResult(interp, "wrong # args: should be \"",
drh1c767f02009-01-09 02:49:31 +00003297 Tcl_GetString(objv[0]), " DB sql bytes ?tailvar?", 0);
danielk19776622cce2004-05-20 11:00:52 +00003298 return TCL_ERROR;
3299 }
3300 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
3301 zSql = Tcl_GetString(objv[2]);
3302 if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR;
3303
drh1c767f02009-01-09 02:49:31 +00003304 rc = sqlite3_prepare(db, zSql, bytes, &pStmt, objc>=5 ? &zTail : 0);
dan937d0de2009-10-15 18:35:38 +00003305 Tcl_ResetResult(interp);
drhc60d0442004-09-30 13:43:13 +00003306 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
drh1c767f02009-01-09 02:49:31 +00003307 if( zTail && objc>=5 ){
danielk19776622cce2004-05-20 11:00:52 +00003308 if( bytes>=0 ){
3309 bytes = bytes - (zTail-zSql);
3310 }
danielk19773a2c8c82008-04-03 14:36:25 +00003311 if( strlen(zTail)<bytes ){
3312 bytes = strlen(zTail);
3313 }
danielk19776622cce2004-05-20 11:00:52 +00003314 Tcl_ObjSetVar2(interp, objv[4], 0, Tcl_NewStringObj(zTail, bytes), 0);
3315 }
danielk19774ad17132004-05-21 01:47:26 +00003316 if( rc!=SQLITE_OK ){
3317 assert( pStmt==0 );
3318 sprintf(zBuf, "(%d) ", rc);
3319 Tcl_AppendResult(interp, zBuf, sqlite3_errmsg(db), 0);
3320 return TCL_ERROR;
3321 }
danielk19776622cce2004-05-20 11:00:52 +00003322
danielk19774ad17132004-05-21 01:47:26 +00003323 if( pStmt ){
drh64b1bea2006-01-15 02:30:57 +00003324 if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR;
danielk19774ad17132004-05-21 01:47:26 +00003325 Tcl_AppendResult(interp, zBuf, 0);
3326 }
danielk19776622cce2004-05-20 11:00:52 +00003327 return TCL_OK;
3328}
3329
3330/*
drh1c767f02009-01-09 02:49:31 +00003331** Usage: sqlite3_prepare_v2 DB sql bytes ?tailvar?
drhb900aaf2006-11-09 00:24:53 +00003332**
3333** Compile up to <bytes> bytes of the supplied SQL string <sql> using
3334** database handle <DB>. The parameter <tailval> is the name of a global
3335** variable that is set to the unused portion of <sql> (if any). A
3336** STMT handle is returned.
3337*/
3338static int test_prepare_v2(
3339 void * clientData,
3340 Tcl_Interp *interp,
3341 int objc,
3342 Tcl_Obj *CONST objv[]
3343){
3344 sqlite3 *db;
3345 const char *zSql;
3346 int bytes;
3347 const char *zTail = 0;
3348 sqlite3_stmt *pStmt = 0;
3349 char zBuf[50];
3350 int rc;
3351
drh1c767f02009-01-09 02:49:31 +00003352 if( objc!=5 && objc!=4 ){
drhb900aaf2006-11-09 00:24:53 +00003353 Tcl_AppendResult(interp, "wrong # args: should be \"",
3354 Tcl_GetString(objv[0]), " DB sql bytes tailvar", 0);
3355 return TCL_ERROR;
3356 }
3357 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
3358 zSql = Tcl_GetString(objv[2]);
3359 if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR;
3360
drh1c767f02009-01-09 02:49:31 +00003361 rc = sqlite3_prepare_v2(db, zSql, bytes, &pStmt, objc>=5 ? &zTail : 0);
danielk19777e29e952007-04-19 11:09:01 +00003362 assert(rc==SQLITE_OK || pStmt==0);
dan937d0de2009-10-15 18:35:38 +00003363 Tcl_ResetResult(interp);
drhb900aaf2006-11-09 00:24:53 +00003364 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
drh1c767f02009-01-09 02:49:31 +00003365 if( zTail && objc>=5 ){
drhb900aaf2006-11-09 00:24:53 +00003366 if( bytes>=0 ){
3367 bytes = bytes - (zTail-zSql);
3368 }
3369 Tcl_ObjSetVar2(interp, objv[4], 0, Tcl_NewStringObj(zTail, bytes), 0);
3370 }
3371 if( rc!=SQLITE_OK ){
3372 assert( pStmt==0 );
3373 sprintf(zBuf, "(%d) ", rc);
3374 Tcl_AppendResult(interp, zBuf, sqlite3_errmsg(db), 0);
3375 return TCL_ERROR;
3376 }
3377
3378 if( pStmt ){
3379 if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR;
3380 Tcl_AppendResult(interp, zBuf, 0);
3381 }
3382 return TCL_OK;
3383}
3384
3385/*
drh4837f532008-05-23 14:49:49 +00003386** Usage: sqlite3_prepare_tkt3134 DB
3387**
3388** Generate a prepared statement for a zero-byte string as a test
3389** for ticket #3134. The string should be preceeded by a zero byte.
3390*/
3391static int test_prepare_tkt3134(
3392 void * clientData,
3393 Tcl_Interp *interp,
3394 int objc,
3395 Tcl_Obj *CONST objv[]
3396){
3397 sqlite3 *db;
3398 static const char zSql[] = "\000SELECT 1";
3399 sqlite3_stmt *pStmt = 0;
3400 char zBuf[50];
3401 int rc;
3402
3403 if( objc!=2 ){
3404 Tcl_AppendResult(interp, "wrong # args: should be \"",
3405 Tcl_GetString(objv[0]), " DB sql bytes tailvar", 0);
3406 return TCL_ERROR;
3407 }
3408 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
3409 rc = sqlite3_prepare_v2(db, &zSql[1], 0, &pStmt, 0);
3410 assert(rc==SQLITE_OK || pStmt==0);
3411 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
3412 if( rc!=SQLITE_OK ){
3413 assert( pStmt==0 );
3414 sprintf(zBuf, "(%d) ", rc);
3415 Tcl_AppendResult(interp, zBuf, sqlite3_errmsg(db), 0);
3416 return TCL_ERROR;
3417 }
3418
3419 if( pStmt ){
3420 if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR;
3421 Tcl_AppendResult(interp, zBuf, 0);
3422 }
3423 return TCL_OK;
3424}
3425
3426/*
drhb900aaf2006-11-09 00:24:53 +00003427** Usage: sqlite3_prepare16 DB sql bytes tailvar
danielk19776622cce2004-05-20 11:00:52 +00003428**
3429** Compile up to <bytes> bytes of the supplied SQL string <sql> using
3430** database handle <DB>. The parameter <tailval> is the name of a global
3431** variable that is set to the unused portion of <sql> (if any). A
3432** STMT handle is returned.
3433*/
3434static int test_prepare16(
3435 void * clientData,
3436 Tcl_Interp *interp,
3437 int objc,
3438 Tcl_Obj *CONST objv[]
3439){
drh5436dc22004-11-14 04:04:17 +00003440#ifndef SQLITE_OMIT_UTF16
danielk19776622cce2004-05-20 11:00:52 +00003441 sqlite3 *db;
3442 const void *zSql;
3443 const void *zTail = 0;
3444 Tcl_Obj *pTail = 0;
3445 sqlite3_stmt *pStmt = 0;
drhc60d0442004-09-30 13:43:13 +00003446 char zBuf[50];
3447 int rc;
danielk19776622cce2004-05-20 11:00:52 +00003448 int bytes; /* The integer specified as arg 3 */
3449 int objlen; /* The byte-array length of arg 2 */
3450
drh1c767f02009-01-09 02:49:31 +00003451 if( objc!=5 && objc!=4 ){
danielk19776622cce2004-05-20 11:00:52 +00003452 Tcl_AppendResult(interp, "wrong # args: should be \"",
drh1c767f02009-01-09 02:49:31 +00003453 Tcl_GetString(objv[0]), " DB sql bytes ?tailvar?", 0);
danielk19776622cce2004-05-20 11:00:52 +00003454 return TCL_ERROR;
3455 }
3456 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
3457 zSql = Tcl_GetByteArrayFromObj(objv[2], &objlen);
3458 if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR;
3459
drh1c767f02009-01-09 02:49:31 +00003460 rc = sqlite3_prepare16(db, zSql, bytes, &pStmt, objc>=5 ? &zTail : 0);
drhc60d0442004-09-30 13:43:13 +00003461 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
3462 if( rc ){
danielk19776622cce2004-05-20 11:00:52 +00003463 return TCL_ERROR;
3464 }
3465
drh1c767f02009-01-09 02:49:31 +00003466 if( objc>=5 ){
3467 if( zTail ){
3468 objlen = objlen - ((u8 *)zTail-(u8 *)zSql);
3469 }else{
3470 objlen = 0;
3471 }
3472 pTail = Tcl_NewByteArrayObj((u8 *)zTail, objlen);
3473 Tcl_IncrRefCount(pTail);
3474 Tcl_ObjSetVar2(interp, objv[4], 0, pTail, 0);
3475 Tcl_DecrRefCount(pTail);
danielk19776622cce2004-05-20 11:00:52 +00003476 }
danielk19776622cce2004-05-20 11:00:52 +00003477
danielk19774ad17132004-05-21 01:47:26 +00003478 if( pStmt ){
drh64b1bea2006-01-15 02:30:57 +00003479 if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR;
danielk19774ad17132004-05-21 01:47:26 +00003480 }
danielk19776622cce2004-05-20 11:00:52 +00003481 Tcl_AppendResult(interp, zBuf, 0);
drh5436dc22004-11-14 04:04:17 +00003482#endif /* SQLITE_OMIT_UTF16 */
danielk19776622cce2004-05-20 11:00:52 +00003483 return TCL_OK;
3484}
3485
danielk19774ad17132004-05-21 01:47:26 +00003486/*
drh1c767f02009-01-09 02:49:31 +00003487** Usage: sqlite3_prepare16_v2 DB sql bytes ?tailvar?
drhb900aaf2006-11-09 00:24:53 +00003488**
3489** Compile up to <bytes> bytes of the supplied SQL string <sql> using
3490** database handle <DB>. The parameter <tailval> is the name of a global
3491** variable that is set to the unused portion of <sql> (if any). A
3492** STMT handle is returned.
3493*/
3494static int test_prepare16_v2(
3495 void * clientData,
3496 Tcl_Interp *interp,
3497 int objc,
3498 Tcl_Obj *CONST objv[]
3499){
3500#ifndef SQLITE_OMIT_UTF16
3501 sqlite3 *db;
3502 const void *zSql;
3503 const void *zTail = 0;
3504 Tcl_Obj *pTail = 0;
3505 sqlite3_stmt *pStmt = 0;
3506 char zBuf[50];
3507 int rc;
3508 int bytes; /* The integer specified as arg 3 */
3509 int objlen; /* The byte-array length of arg 2 */
3510
drh1c767f02009-01-09 02:49:31 +00003511 if( objc!=5 && objc!=4 ){
drhb900aaf2006-11-09 00:24:53 +00003512 Tcl_AppendResult(interp, "wrong # args: should be \"",
drh1c767f02009-01-09 02:49:31 +00003513 Tcl_GetString(objv[0]), " DB sql bytes ?tailvar?", 0);
drhb900aaf2006-11-09 00:24:53 +00003514 return TCL_ERROR;
3515 }
3516 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
3517 zSql = Tcl_GetByteArrayFromObj(objv[2], &objlen);
3518 if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR;
3519
drh1c767f02009-01-09 02:49:31 +00003520 rc = sqlite3_prepare16_v2(db, zSql, bytes, &pStmt, objc>=5 ? &zTail : 0);
drhb900aaf2006-11-09 00:24:53 +00003521 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
3522 if( rc ){
3523 return TCL_ERROR;
3524 }
3525
drh1c767f02009-01-09 02:49:31 +00003526 if( objc>=5 ){
3527 if( zTail ){
3528 objlen = objlen - ((u8 *)zTail-(u8 *)zSql);
3529 }else{
3530 objlen = 0;
3531 }
3532 pTail = Tcl_NewByteArrayObj((u8 *)zTail, objlen);
3533 Tcl_IncrRefCount(pTail);
3534 Tcl_ObjSetVar2(interp, objv[4], 0, pTail, 0);
3535 Tcl_DecrRefCount(pTail);
drhb900aaf2006-11-09 00:24:53 +00003536 }
drhb900aaf2006-11-09 00:24:53 +00003537
3538 if( pStmt ){
3539 if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR;
3540 }
3541 Tcl_AppendResult(interp, zBuf, 0);
3542#endif /* SQLITE_OMIT_UTF16 */
3543 return TCL_OK;
3544}
3545
3546/*
danielk19774ad17132004-05-21 01:47:26 +00003547** Usage: sqlite3_open filename ?options-list?
3548*/
3549static int test_open(
3550 void * clientData,
3551 Tcl_Interp *interp,
3552 int objc,
3553 Tcl_Obj *CONST objv[]
3554){
3555 const char *zFilename;
3556 sqlite3 *db;
3557 int rc;
3558 char zBuf[100];
3559
drhafc91042008-02-21 02:09:45 +00003560 if( objc!=3 && objc!=2 && objc!=1 ){
danielk19774ad17132004-05-21 01:47:26 +00003561 Tcl_AppendResult(interp, "wrong # args: should be \"",
3562 Tcl_GetString(objv[0]), " filename options-list", 0);
3563 return TCL_ERROR;
3564 }
3565
drhafc91042008-02-21 02:09:45 +00003566 zFilename = objc>1 ? Tcl_GetString(objv[1]) : 0;
danielk19774f057f92004-06-08 00:02:33 +00003567 rc = sqlite3_open(zFilename, &db);
danielk19774ad17132004-05-21 01:47:26 +00003568
drh64b1bea2006-01-15 02:30:57 +00003569 if( sqlite3TestMakePointerStr(interp, zBuf, db) ) return TCL_ERROR;
danielk19774ad17132004-05-21 01:47:26 +00003570 Tcl_AppendResult(interp, zBuf, 0);
3571 return TCL_OK;
3572}
3573
3574/*
3575** Usage: sqlite3_open16 filename options
3576*/
3577static int test_open16(
3578 void * clientData,
3579 Tcl_Interp *interp,
3580 int objc,
3581 Tcl_Obj *CONST objv[]
3582){
drh5436dc22004-11-14 04:04:17 +00003583#ifndef SQLITE_OMIT_UTF16
danielk19774ad17132004-05-21 01:47:26 +00003584 const void *zFilename;
3585 sqlite3 *db;
3586 int rc;
3587 char zBuf[100];
3588
3589 if( objc!=3 ){
3590 Tcl_AppendResult(interp, "wrong # args: should be \"",
3591 Tcl_GetString(objv[0]), " filename options-list", 0);
3592 return TCL_ERROR;
3593 }
3594
3595 zFilename = Tcl_GetByteArrayFromObj(objv[1], 0);
danielk19774f057f92004-06-08 00:02:33 +00003596 rc = sqlite3_open16(zFilename, &db);
danielk19774ad17132004-05-21 01:47:26 +00003597
drh64b1bea2006-01-15 02:30:57 +00003598 if( sqlite3TestMakePointerStr(interp, zBuf, db) ) return TCL_ERROR;
danielk19774ad17132004-05-21 01:47:26 +00003599 Tcl_AppendResult(interp, zBuf, 0);
drh5436dc22004-11-14 04:04:17 +00003600#endif /* SQLITE_OMIT_UTF16 */
danielk19774ad17132004-05-21 01:47:26 +00003601 return TCL_OK;
3602}
drhd3d39e92004-05-20 22:16:29 +00003603
3604/*
danielk1977bc6ada42004-06-30 08:20:16 +00003605** Usage: sqlite3_complete16 <UTF-16 string>
3606**
3607** Return 1 if the supplied argument is a complete SQL statement, or zero
3608** otherwise.
3609*/
3610static int test_complete16(
3611 void * clientData,
3612 Tcl_Interp *interp,
3613 int objc,
3614 Tcl_Obj *CONST objv[]
3615){
drhccae6022005-02-26 17:31:26 +00003616#if !defined(SQLITE_OMIT_COMPLETE) && !defined(SQLITE_OMIT_UTF16)
danielk1977bc6ada42004-06-30 08:20:16 +00003617 char *zBuf;
3618
3619 if( objc!=2 ){
3620 Tcl_WrongNumArgs(interp, 1, objv, "<utf-16 sql>");
3621 return TCL_ERROR;
3622 }
3623
drh03d847e2005-12-09 20:21:58 +00003624 zBuf = (char*)Tcl_GetByteArrayFromObj(objv[1], 0);
danielk1977bc6ada42004-06-30 08:20:16 +00003625 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_complete16(zBuf)));
drhccae6022005-02-26 17:31:26 +00003626#endif /* SQLITE_OMIT_COMPLETE && SQLITE_OMIT_UTF16 */
danielk1977bc6ada42004-06-30 08:20:16 +00003627 return TCL_OK;
3628}
3629
3630/*
danielk1977106bb232004-05-21 10:08:53 +00003631** Usage: sqlite3_step STMT
3632**
3633** Advance the statement to the next row.
3634*/
danielk197717240fd2004-05-26 00:07:25 +00003635static int test_step(
danielk1977106bb232004-05-21 10:08:53 +00003636 void * clientData,
3637 Tcl_Interp *interp,
3638 int objc,
3639 Tcl_Obj *CONST objv[]
3640){
3641 sqlite3_stmt *pStmt;
3642 int rc;
3643
danielk1977e1cd9872004-05-22 10:33:04 +00003644 if( objc!=2 ){
danielk1977106bb232004-05-21 10:08:53 +00003645 Tcl_AppendResult(interp, "wrong # args: should be \"",
3646 Tcl_GetString(objv[0]), " STMT", 0);
3647 return TCL_ERROR;
3648 }
3649
3650 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
danielk197717240fd2004-05-26 00:07:25 +00003651 rc = sqlite3_step(pStmt);
danielk1977106bb232004-05-21 10:08:53 +00003652
danielk1977fbcd5852004-06-15 02:44:18 +00003653 /* if( rc!=SQLITE_DONE && rc!=SQLITE_ROW ) return TCL_ERROR; */
drh4f0c5872007-03-26 22:05:01 +00003654 Tcl_SetResult(interp, (char *)t1ErrorName(rc), 0);
danielk1977e1cd9872004-05-22 10:33:04 +00003655 return TCL_OK;
3656}
3657
danielk1977404ca072009-03-16 13:19:36 +00003658static int test_sql(
3659 void * clientData,
3660 Tcl_Interp *interp,
3661 int objc,
3662 Tcl_Obj *CONST objv[]
3663){
3664 sqlite3_stmt *pStmt;
3665
3666 if( objc!=2 ){
3667 Tcl_WrongNumArgs(interp, 1, objv, "STMT");
3668 return TCL_ERROR;
3669 }
3670
3671 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3672 Tcl_SetResult(interp, (char *)sqlite3_sql(pStmt), TCL_VOLATILE);
3673 return TCL_OK;
3674}
3675
danielk1977e1cd9872004-05-22 10:33:04 +00003676/*
danielk197717240fd2004-05-26 00:07:25 +00003677** Usage: sqlite3_column_count STMT
3678**
3679** Return the number of columns returned by the sql statement STMT.
3680*/
3681static int test_column_count(
3682 void * clientData,
3683 Tcl_Interp *interp,
3684 int objc,
3685 Tcl_Obj *CONST objv[]
3686){
3687 sqlite3_stmt *pStmt;
3688
3689 if( objc!=2 ){
3690 Tcl_AppendResult(interp, "wrong # args: should be \"",
3691 Tcl_GetString(objv[0]), " STMT column", 0);
3692 return TCL_ERROR;
3693 }
3694
3695 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3696
3697 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_column_count(pStmt)));
3698 return TCL_OK;
3699}
3700
3701/*
danielk19773cf86062004-05-26 10:11:05 +00003702** Usage: sqlite3_column_type STMT column
3703**
3704** Return the type of the data in column 'column' of the current row.
3705*/
3706static int test_column_type(
3707 void * clientData,
3708 Tcl_Interp *interp,
3709 int objc,
3710 Tcl_Obj *CONST objv[]
3711){
3712 sqlite3_stmt *pStmt;
3713 int col;
3714 int tp;
3715
3716 if( objc!=3 ){
3717 Tcl_AppendResult(interp, "wrong # args: should be \"",
3718 Tcl_GetString(objv[0]), " STMT column", 0);
3719 return TCL_ERROR;
3720 }
3721
3722 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3723 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
3724
3725 tp = sqlite3_column_type(pStmt, col);
3726 switch( tp ){
drh9c054832004-05-31 18:51:57 +00003727 case SQLITE_INTEGER:
danielk19773cf86062004-05-26 10:11:05 +00003728 Tcl_SetResult(interp, "INTEGER", TCL_STATIC);
3729 break;
drh9c054832004-05-31 18:51:57 +00003730 case SQLITE_NULL:
danielk19773cf86062004-05-26 10:11:05 +00003731 Tcl_SetResult(interp, "NULL", TCL_STATIC);
3732 break;
drh9c054832004-05-31 18:51:57 +00003733 case SQLITE_FLOAT:
danielk19773cf86062004-05-26 10:11:05 +00003734 Tcl_SetResult(interp, "FLOAT", TCL_STATIC);
3735 break;
drh9c054832004-05-31 18:51:57 +00003736 case SQLITE_TEXT:
danielk19773cf86062004-05-26 10:11:05 +00003737 Tcl_SetResult(interp, "TEXT", TCL_STATIC);
3738 break;
drh9c054832004-05-31 18:51:57 +00003739 case SQLITE_BLOB:
danielk19773cf86062004-05-26 10:11:05 +00003740 Tcl_SetResult(interp, "BLOB", TCL_STATIC);
3741 break;
3742 default:
3743 assert(0);
3744 }
3745
3746 return TCL_OK;
3747}
3748
3749/*
danielk197704f2e682004-05-27 01:04:07 +00003750** Usage: sqlite3_column_int64 STMT column
danielk19773cf86062004-05-26 10:11:05 +00003751**
3752** Return the data in column 'column' of the current row cast as an
danielk197704f2e682004-05-27 01:04:07 +00003753** wide (64-bit) integer.
danielk19773cf86062004-05-26 10:11:05 +00003754*/
danielk197704f2e682004-05-27 01:04:07 +00003755static int test_column_int64(
danielk19773cf86062004-05-26 10:11:05 +00003756 void * clientData,
3757 Tcl_Interp *interp,
3758 int objc,
3759 Tcl_Obj *CONST objv[]
3760){
3761 sqlite3_stmt *pStmt;
3762 int col;
danielk197704f2e682004-05-27 01:04:07 +00003763 i64 iVal;
danielk19773cf86062004-05-26 10:11:05 +00003764
3765 if( objc!=3 ){
3766 Tcl_AppendResult(interp, "wrong # args: should be \"",
3767 Tcl_GetString(objv[0]), " STMT column", 0);
3768 return TCL_ERROR;
3769 }
3770
3771 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3772 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
3773
danielk197704f2e682004-05-27 01:04:07 +00003774 iVal = sqlite3_column_int64(pStmt, col);
3775 Tcl_SetObjResult(interp, Tcl_NewWideIntObj(iVal));
3776 return TCL_OK;
3777}
3778
3779/*
danielk1977ea61b2c2004-05-27 01:49:51 +00003780** Usage: sqlite3_column_blob STMT column
3781*/
3782static int test_column_blob(
3783 void * clientData,
3784 Tcl_Interp *interp,
3785 int objc,
3786 Tcl_Obj *CONST objv[]
3787){
3788 sqlite3_stmt *pStmt;
3789 int col;
3790
3791 int len;
danielk1977c572ef72004-05-27 09:28:41 +00003792 const void *pBlob;
danielk1977ea61b2c2004-05-27 01:49:51 +00003793
3794 if( objc!=3 ){
3795 Tcl_AppendResult(interp, "wrong # args: should be \"",
3796 Tcl_GetString(objv[0]), " STMT column", 0);
3797 return TCL_ERROR;
3798 }
3799
3800 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3801 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
3802
danielk1977ea61b2c2004-05-27 01:49:51 +00003803 len = sqlite3_column_bytes(pStmt, col);
drh9310ef22007-04-27 17:16:20 +00003804 pBlob = sqlite3_column_blob(pStmt, col);
danielk1977ea61b2c2004-05-27 01:49:51 +00003805 Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(pBlob, len));
3806 return TCL_OK;
3807}
3808
3809/*
danielk197704f2e682004-05-27 01:04:07 +00003810** Usage: sqlite3_column_double STMT column
3811**
3812** Return the data in column 'column' of the current row cast as a double.
3813*/
3814static int test_column_double(
3815 void * clientData,
3816 Tcl_Interp *interp,
3817 int objc,
3818 Tcl_Obj *CONST objv[]
3819){
3820 sqlite3_stmt *pStmt;
3821 int col;
3822 double rVal;
3823
3824 if( objc!=3 ){
3825 Tcl_AppendResult(interp, "wrong # args: should be \"",
3826 Tcl_GetString(objv[0]), " STMT column", 0);
3827 return TCL_ERROR;
3828 }
3829
3830 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3831 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
3832
3833 rVal = sqlite3_column_double(pStmt, col);
danielk1977c572ef72004-05-27 09:28:41 +00003834 Tcl_SetObjResult(interp, Tcl_NewDoubleObj(rVal));
danielk19773cf86062004-05-26 10:11:05 +00003835 return TCL_OK;
3836}
3837
3838/*
danielk197717240fd2004-05-26 00:07:25 +00003839** Usage: sqlite3_data_count STMT
3840**
3841** Return the number of columns returned by the sql statement STMT.
3842*/
3843static int test_data_count(
3844 void * clientData,
3845 Tcl_Interp *interp,
3846 int objc,
3847 Tcl_Obj *CONST objv[]
3848){
3849 sqlite3_stmt *pStmt;
3850
3851 if( objc!=2 ){
3852 Tcl_AppendResult(interp, "wrong # args: should be \"",
3853 Tcl_GetString(objv[0]), " STMT column", 0);
3854 return TCL_ERROR;
3855 }
3856
3857 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3858
3859 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_data_count(pStmt)));
3860 return TCL_OK;
3861}
3862
3863/*
danielk197704f2e682004-05-27 01:04:07 +00003864** Usage: sqlite3_column_text STMT column
3865**
3866** Usage: sqlite3_column_decltype STMT column
3867**
3868** Usage: sqlite3_column_name STMT column
3869*/
3870static int test_stmt_utf8(
drh241db312004-06-22 12:46:53 +00003871 void * clientData, /* Pointer to SQLite API function to be invoke */
danielk197704f2e682004-05-27 01:04:07 +00003872 Tcl_Interp *interp,
3873 int objc,
3874 Tcl_Obj *CONST objv[]
3875){
3876 sqlite3_stmt *pStmt;
3877 int col;
danielk197744a376f2008-08-12 15:04:58 +00003878 const char *(*xFunc)(sqlite3_stmt*, int);
danielk1977f93bbbe2004-05-27 10:30:52 +00003879 const char *zRet;
danielk197704f2e682004-05-27 01:04:07 +00003880
danielk197744a376f2008-08-12 15:04:58 +00003881 xFunc = (const char *(*)(sqlite3_stmt*, int))clientData;
danielk197704f2e682004-05-27 01:04:07 +00003882 if( objc!=3 ){
3883 Tcl_AppendResult(interp, "wrong # args: should be \"",
3884 Tcl_GetString(objv[0]), " STMT column", 0);
3885 return TCL_ERROR;
3886 }
3887
3888 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3889 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
danielk1977f93bbbe2004-05-27 10:30:52 +00003890 zRet = xFunc(pStmt, col);
3891 if( zRet ){
3892 Tcl_SetResult(interp, (char *)zRet, 0);
3893 }
danielk197704f2e682004-05-27 01:04:07 +00003894 return TCL_OK;
3895}
3896
danielk19776b456a22005-03-21 04:04:02 +00003897static int test_global_recover(
3898 void * clientData,
3899 Tcl_Interp *interp,
3900 int objc,
3901 Tcl_Obj *CONST objv[]
3902){
3903#ifndef SQLITE_OMIT_GLOBALRECOVER
shaneeec556d2008-10-12 00:27:53 +00003904#ifndef SQLITE_OMIT_DEPRECATED
danielk19776b456a22005-03-21 04:04:02 +00003905 int rc;
3906 if( objc!=1 ){
3907 Tcl_WrongNumArgs(interp, 1, objv, "");
3908 return TCL_ERROR;
3909 }
3910 rc = sqlite3_global_recover();
drh4f0c5872007-03-26 22:05:01 +00003911 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
danielk19776b456a22005-03-21 04:04:02 +00003912#endif
shaneeec556d2008-10-12 00:27:53 +00003913#endif
danielk19776b456a22005-03-21 04:04:02 +00003914 return TCL_OK;
3915}
3916
danielk197704f2e682004-05-27 01:04:07 +00003917/*
3918** Usage: sqlite3_column_text STMT column
3919**
3920** Usage: sqlite3_column_decltype STMT column
3921**
3922** Usage: sqlite3_column_name STMT column
3923*/
3924static int test_stmt_utf16(
drh241db312004-06-22 12:46:53 +00003925 void * clientData, /* Pointer to SQLite API function to be invoked */
danielk197704f2e682004-05-27 01:04:07 +00003926 Tcl_Interp *interp,
3927 int objc,
3928 Tcl_Obj *CONST objv[]
3929){
drh5436dc22004-11-14 04:04:17 +00003930#ifndef SQLITE_OMIT_UTF16
danielk197704f2e682004-05-27 01:04:07 +00003931 sqlite3_stmt *pStmt;
3932 int col;
3933 Tcl_Obj *pRet;
3934 const void *zName16;
danielk197744a376f2008-08-12 15:04:58 +00003935 const void *(*xFunc)(sqlite3_stmt*, int);
danielk197704f2e682004-05-27 01:04:07 +00003936
danielk197744a376f2008-08-12 15:04:58 +00003937 xFunc = (const void *(*)(sqlite3_stmt*, int))clientData;
danielk197704f2e682004-05-27 01:04:07 +00003938 if( objc!=3 ){
3939 Tcl_AppendResult(interp, "wrong # args: should be \"",
3940 Tcl_GetString(objv[0]), " STMT column", 0);
3941 return TCL_ERROR;
3942 }
3943
3944 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3945 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
3946
3947 zName16 = xFunc(pStmt, col);
danielk1977f93bbbe2004-05-27 10:30:52 +00003948 if( zName16 ){
drhaed382f2009-04-01 18:40:32 +00003949 int n;
3950 const char *z = zName16;
3951 for(n=0; z[n] || z[n+1]; n+=2){}
3952 pRet = Tcl_NewByteArrayObj(zName16, n+2);
danielk1977f93bbbe2004-05-27 10:30:52 +00003953 Tcl_SetObjResult(interp, pRet);
3954 }
drh5436dc22004-11-14 04:04:17 +00003955#endif /* SQLITE_OMIT_UTF16 */
danielk197704f2e682004-05-27 01:04:07 +00003956
3957 return TCL_OK;
3958}
3959
3960/*
3961** Usage: sqlite3_column_int STMT column
3962**
3963** Usage: sqlite3_column_bytes STMT column
3964**
3965** Usage: sqlite3_column_bytes16 STMT column
3966**
3967*/
3968static int test_stmt_int(
drh241db312004-06-22 12:46:53 +00003969 void * clientData, /* Pointer to SQLite API function to be invoked */
danielk197704f2e682004-05-27 01:04:07 +00003970 Tcl_Interp *interp,
3971 int objc,
3972 Tcl_Obj *CONST objv[]
3973){
3974 sqlite3_stmt *pStmt;
3975 int col;
danielk197744a376f2008-08-12 15:04:58 +00003976 int (*xFunc)(sqlite3_stmt*, int);
danielk197704f2e682004-05-27 01:04:07 +00003977
danielk197755a25a12008-09-11 10:29:15 +00003978 xFunc = (int (*)(sqlite3_stmt*, int))clientData;
danielk197704f2e682004-05-27 01:04:07 +00003979 if( objc!=3 ){
3980 Tcl_AppendResult(interp, "wrong # args: should be \"",
3981 Tcl_GetString(objv[0]), " STMT column", 0);
3982 return TCL_ERROR;
3983 }
3984
3985 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3986 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
3987
3988 Tcl_SetObjResult(interp, Tcl_NewIntObj(xFunc(pStmt, col)));
3989 return TCL_OK;
3990}
3991
danielk19776622cce2004-05-20 11:00:52 +00003992/*
drhcacb2082005-01-11 15:28:33 +00003993** Usage: sqlite_set_magic DB MAGIC-NUMBER
3994**
3995** Set the db->magic value. This is used to test error recovery logic.
3996*/
3997static int sqlite_set_magic(
3998 void * clientData,
3999 Tcl_Interp *interp,
4000 int argc,
4001 char **argv
4002){
4003 sqlite3 *db;
4004 if( argc!=3 ){
4005 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
4006 " DB MAGIC", 0);
4007 return TCL_ERROR;
4008 }
4009 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
4010 if( strcmp(argv[2], "SQLITE_MAGIC_OPEN")==0 ){
4011 db->magic = SQLITE_MAGIC_OPEN;
4012 }else if( strcmp(argv[2], "SQLITE_MAGIC_CLOSED")==0 ){
4013 db->magic = SQLITE_MAGIC_CLOSED;
4014 }else if( strcmp(argv[2], "SQLITE_MAGIC_BUSY")==0 ){
4015 db->magic = SQLITE_MAGIC_BUSY;
4016 }else if( strcmp(argv[2], "SQLITE_MAGIC_ERROR")==0 ){
4017 db->magic = SQLITE_MAGIC_ERROR;
drh902b9ee2008-12-05 17:17:07 +00004018 }else if( Tcl_GetInt(interp, argv[2], (int*)&db->magic) ){
drhcacb2082005-01-11 15:28:33 +00004019 return TCL_ERROR;
4020 }
4021 return TCL_OK;
4022}
4023
4024/*
drhc5cdca62005-01-11 16:54:14 +00004025** Usage: sqlite3_interrupt DB
4026**
4027** Trigger an interrupt on DB
4028*/
4029static int test_interrupt(
4030 void * clientData,
4031 Tcl_Interp *interp,
4032 int argc,
4033 char **argv
4034){
4035 sqlite3 *db;
4036 if( argc!=2 ){
4037 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " DB", 0);
4038 return TCL_ERROR;
4039 }
4040 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
4041 sqlite3_interrupt(db);
4042 return TCL_OK;
4043}
4044
drh79158e12005-09-06 21:40:45 +00004045static u8 *sqlite3_stack_baseline = 0;
4046
drhc5cdca62005-01-11 16:54:14 +00004047/*
drh79158e12005-09-06 21:40:45 +00004048** Fill the stack with a known bitpattern.
danielk1977600dd0b2005-01-20 01:14:23 +00004049*/
drh79158e12005-09-06 21:40:45 +00004050static void prepStack(void){
4051 int i;
4052 u32 bigBuf[65536];
4053 for(i=0; i<sizeof(bigBuf); i++) bigBuf[i] = 0xdeadbeef;
4054 sqlite3_stack_baseline = (u8*)&bigBuf[65536];
4055}
4056
4057/*
4058** Get the current stack depth. Used for debugging only.
4059*/
4060u64 sqlite3StackDepth(void){
4061 u8 x;
4062 return (u64)(sqlite3_stack_baseline - &x);
4063}
4064
4065/*
4066** Usage: sqlite3_stack_used DB SQL
4067**
4068** Try to measure the amount of stack space used by a call to sqlite3_exec
4069*/
4070static int test_stack_used(
danielk1977600dd0b2005-01-20 01:14:23 +00004071 void * clientData,
4072 Tcl_Interp *interp,
4073 int argc,
4074 char **argv
4075){
4076 sqlite3 *db;
drh79158e12005-09-06 21:40:45 +00004077 int i;
4078 if( argc!=3 ){
4079 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
4080 " DB SQL", 0);
danielk1977600dd0b2005-01-20 01:14:23 +00004081 return TCL_ERROR;
4082 }
drh79158e12005-09-06 21:40:45 +00004083 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
4084 prepStack();
drh37527852006-03-16 16:19:56 +00004085 (void)sqlite3_exec(db, argv[2], 0, 0, 0);
drh79158e12005-09-06 21:40:45 +00004086 for(i=65535; i>=0 && ((u32*)sqlite3_stack_baseline)[-i]==0xdeadbeef; i--){}
4087 Tcl_SetObjResult(interp, Tcl_NewIntObj(i*4));
danielk1977600dd0b2005-01-20 01:14:23 +00004088 return TCL_OK;
4089}
danielk1977600dd0b2005-01-20 01:14:23 +00004090
4091/*
danielk19779636c4e2005-01-25 04:27:54 +00004092** Usage: sqlite_delete_function DB function-name
4093**
4094** Delete the user function 'function-name' from database handle DB. It
4095** is assumed that the user function was created as UTF8, any number of
4096** arguments (the way the TCL interface does it).
4097*/
4098static int delete_function(
4099 void * clientData,
4100 Tcl_Interp *interp,
4101 int argc,
4102 char **argv
4103){
4104 int rc;
4105 sqlite3 *db;
4106 if( argc!=3 ){
4107 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
4108 " DB function-name", 0);
4109 return TCL_ERROR;
4110 }
4111 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
4112 rc = sqlite3_create_function(db, argv[2], -1, SQLITE_UTF8, 0, 0, 0, 0);
drh4f0c5872007-03-26 22:05:01 +00004113 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
danielk19779636c4e2005-01-25 04:27:54 +00004114 return TCL_OK;
4115}
4116
4117/*
4118** Usage: sqlite_delete_collation DB collation-name
4119**
4120** Delete the collation sequence 'collation-name' from database handle
4121** DB. It is assumed that the collation sequence was created as UTF8 (the
4122** way the TCL interface does it).
4123*/
4124static int delete_collation(
4125 void * clientData,
4126 Tcl_Interp *interp,
4127 int argc,
4128 char **argv
4129){
4130 int rc;
4131 sqlite3 *db;
4132 if( argc!=3 ){
4133 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
4134 " DB function-name", 0);
4135 return TCL_ERROR;
4136 }
4137 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
4138 rc = sqlite3_create_collation(db, argv[2], SQLITE_UTF8, 0, 0);
drh4f0c5872007-03-26 22:05:01 +00004139 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
danielk19779636c4e2005-01-25 04:27:54 +00004140 return TCL_OK;
4141}
4142
4143/*
drh3e1d8e62005-05-26 16:23:34 +00004144** Usage: sqlite3_get_autocommit DB
4145**
4146** Return true if the database DB is currently in auto-commit mode.
4147** Return false if not.
4148*/
4149static int get_autocommit(
4150 void * clientData,
4151 Tcl_Interp *interp,
4152 int argc,
4153 char **argv
4154){
4155 char zBuf[30];
4156 sqlite3 *db;
4157 if( argc!=2 ){
4158 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
4159 " DB", 0);
4160 return TCL_ERROR;
4161 }
4162 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
4163 sprintf(zBuf, "%d", sqlite3_get_autocommit(db));
4164 Tcl_AppendResult(interp, zBuf, 0);
4165 return TCL_OK;
4166}
4167
4168/*
drh30867652006-07-06 10:59:57 +00004169** Usage: sqlite3_busy_timeout DB MS
4170**
4171** Set the busy timeout. This is more easily done using the timeout
4172** method of the TCL interface. But we need a way to test the case
4173** where it returns SQLITE_MISUSE.
4174*/
4175static int test_busy_timeout(
4176 void * clientData,
4177 Tcl_Interp *interp,
4178 int argc,
4179 char **argv
4180){
4181 int rc, ms;
4182 sqlite3 *db;
4183 if( argc!=3 ){
4184 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
4185 " DB", 0);
4186 return TCL_ERROR;
4187 }
4188 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
4189 if( Tcl_GetInt(interp, argv[2], &ms) ) return TCL_ERROR;
4190 rc = sqlite3_busy_timeout(db, ms);
4191 Tcl_AppendResult(interp, sqlite3TestErrorName(rc), 0);
4192 return TCL_OK;
4193}
4194
4195/*
drh92febd92004-08-20 18:34:20 +00004196** Usage: tcl_variable_type VARIABLENAME
4197**
4198** Return the name of the internal representation for the
4199** value of the given variable.
4200*/
4201static int tcl_variable_type(
4202 void * clientData,
4203 Tcl_Interp *interp,
4204 int objc,
4205 Tcl_Obj *CONST objv[]
4206){
4207 Tcl_Obj *pVar;
4208 if( objc!=2 ){
4209 Tcl_WrongNumArgs(interp, 1, objv, "VARIABLE");
4210 return TCL_ERROR;
4211 }
4212 pVar = Tcl_GetVar2Ex(interp, Tcl_GetString(objv[1]), 0, TCL_LEAVE_ERR_MSG);
4213 if( pVar==0 ) return TCL_ERROR;
4214 if( pVar->typePtr ){
4215 Tcl_SetObjResult(interp, Tcl_NewStringObj(pVar->typePtr->name, -1));
4216 }
4217 return TCL_OK;
4218}
4219
4220/*
drh6aafc292006-01-05 15:50:06 +00004221** Usage: sqlite3_release_memory ?N?
4222**
4223** Attempt to release memory currently held but not actually required.
4224** The integer N is the number of bytes we are trying to release. The
4225** return value is the amount of memory actually released.
4226*/
4227static int test_release_memory(
4228 void * clientData,
4229 Tcl_Interp *interp,
4230 int objc,
4231 Tcl_Obj *CONST objv[]
4232){
drh6f7adc82006-01-11 21:41:20 +00004233#if defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) && !defined(SQLITE_OMIT_DISKIO)
drh6aafc292006-01-05 15:50:06 +00004234 int N;
4235 int amt;
4236 if( objc!=1 && objc!=2 ){
4237 Tcl_WrongNumArgs(interp, 1, objv, "?N?");
4238 return TCL_ERROR;
4239 }
4240 if( objc==2 ){
4241 if( Tcl_GetIntFromObj(interp, objv[1], &N) ) return TCL_ERROR;
4242 }else{
4243 N = -1;
4244 }
4245 amt = sqlite3_release_memory(N);
4246 Tcl_SetObjResult(interp, Tcl_NewIntObj(amt));
4247#endif
4248 return TCL_OK;
4249}
4250
4251/*
4252** Usage: sqlite3_soft_heap_limit ?N?
4253**
4254** Query or set the soft heap limit for the current thread. The
4255** limit is only changed if the N is present. The previous limit
4256** is returned.
4257*/
4258static int test_soft_heap_limit(
4259 void * clientData,
4260 Tcl_Interp *interp,
4261 int objc,
4262 Tcl_Obj *CONST objv[]
4263){
drh86f8c192007-08-22 00:39:19 +00004264 static int softHeapLimit = 0;
drh6aafc292006-01-05 15:50:06 +00004265 int amt;
4266 if( objc!=1 && objc!=2 ){
4267 Tcl_WrongNumArgs(interp, 1, objv, "?N?");
4268 return TCL_ERROR;
4269 }
drh86f8c192007-08-22 00:39:19 +00004270 amt = softHeapLimit;
drh6aafc292006-01-05 15:50:06 +00004271 if( objc==2 ){
4272 int N;
4273 if( Tcl_GetIntFromObj(interp, objv[1], &N) ) return TCL_ERROR;
4274 sqlite3_soft_heap_limit(N);
drh86f8c192007-08-22 00:39:19 +00004275 softHeapLimit = N;
drh6aafc292006-01-05 15:50:06 +00004276 }
4277 Tcl_SetObjResult(interp, Tcl_NewIntObj(amt));
drh6aafc292006-01-05 15:50:06 +00004278 return TCL_OK;
4279}
4280
4281/*
drhb4bc7052006-01-11 23:40:33 +00004282** Usage: sqlite3_thread_cleanup
4283**
4284** Call the sqlite3_thread_cleanup API.
4285*/
4286static int test_thread_cleanup(
4287 void * clientData,
4288 Tcl_Interp *interp,
4289 int objc,
4290 Tcl_Obj *CONST objv[]
4291){
shaneeec556d2008-10-12 00:27:53 +00004292#ifndef SQLITE_OMIT_DEPRECATED
drhb4bc7052006-01-11 23:40:33 +00004293 sqlite3_thread_cleanup();
shaneeec556d2008-10-12 00:27:53 +00004294#endif
drhb4bc7052006-01-11 23:40:33 +00004295 return TCL_OK;
4296}
4297
drhb4bc7052006-01-11 23:40:33 +00004298/*
drhc6ba55f2007-04-05 17:36:18 +00004299** Usage: sqlite3_pager_refcounts DB
4300**
drhf5345442007-04-09 12:45:02 +00004301** Return a list of numbers which are the PagerRefcount for all
4302** pagers on each database connection.
drhc6ba55f2007-04-05 17:36:18 +00004303*/
4304static int test_pager_refcounts(
4305 void * clientData,
4306 Tcl_Interp *interp,
4307 int objc,
4308 Tcl_Obj *CONST objv[]
4309){
4310 sqlite3 *db;
4311 int i;
4312 int v, *a;
4313 Tcl_Obj *pResult;
4314
4315 if( objc!=2 ){
4316 Tcl_AppendResult(interp, "wrong # args: should be \"",
drhf5345442007-04-09 12:45:02 +00004317 Tcl_GetStringFromObj(objv[0], 0), " DB", 0);
drhc6ba55f2007-04-05 17:36:18 +00004318 return TCL_ERROR;
4319 }
4320 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
4321 pResult = Tcl_NewObj();
4322 for(i=0; i<db->nDb; i++){
4323 if( db->aDb[i].pBt==0 ){
4324 v = -1;
4325 }else{
drh27641702007-08-22 02:56:42 +00004326 sqlite3_mutex_enter(db->mutex);
drhc6ba55f2007-04-05 17:36:18 +00004327 a = sqlite3PagerStats(sqlite3BtreePager(db->aDb[i].pBt));
4328 v = a[0];
drh27641702007-08-22 02:56:42 +00004329 sqlite3_mutex_leave(db->mutex);
drhc6ba55f2007-04-05 17:36:18 +00004330 }
4331 Tcl_ListObjAppendElement(0, pResult, Tcl_NewIntObj(v));
4332 }
4333 Tcl_SetObjResult(interp, pResult);
4334 return TCL_OK;
4335}
4336
4337
4338/*
drh80788d82006-09-02 14:50:23 +00004339** tclcmd: working_64bit_int
4340**
4341** Some TCL builds (ex: cygwin) do not support 64-bit integers. This
4342** leads to a number of test failures. The present command checks the
4343** TCL build to see whether or not it supports 64-bit integers. It
4344** returns TRUE if it does and FALSE if not.
4345**
4346** This command is used to warn users that their TCL build is defective
4347** and that the errors they are seeing in the test scripts might be
4348** a result of their defective TCL rather than problems in SQLite.
4349*/
4350static int working_64bit_int(
4351 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
4352 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
4353 int objc, /* Number of arguments */
4354 Tcl_Obj *CONST objv[] /* Command arguments */
4355){
4356 Tcl_Obj *pTestObj;
4357 int working = 0;
4358
4359 pTestObj = Tcl_NewWideIntObj(1000000*(i64)1234567890);
4360 working = strcmp(Tcl_GetString(pTestObj), "1234567890000000")==0;
4361 Tcl_DecrRefCount(pTestObj);
4362 Tcl_SetObjResult(interp, Tcl_NewBooleanObj(working));
4363 return TCL_OK;
4364}
4365
4366
4367/*
drh9bc54492007-10-23 14:49:59 +00004368** tclcmd: vfs_unlink_test
4369**
4370** This TCL command unregisters the primary VFS and then registers
4371** it back again. This is used to test the ability to register a
4372** VFS when none are previously registered, and the ability to
4373** unregister the only available VFS. Ticket #2738
4374*/
4375static int vfs_unlink_test(
4376 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
4377 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
4378 int objc, /* Number of arguments */
4379 Tcl_Obj *CONST objv[] /* Command arguments */
4380){
4381 int i;
drh91fd4d42008-01-19 20:11:25 +00004382 sqlite3_vfs *pMain;
drh9bc54492007-10-23 14:49:59 +00004383 sqlite3_vfs *apVfs[20];
drh91fd4d42008-01-19 20:11:25 +00004384 sqlite3_vfs one, two;
drh9bc54492007-10-23 14:49:59 +00004385
drh91fd4d42008-01-19 20:11:25 +00004386 sqlite3_vfs_unregister(0); /* Unregister of NULL is harmless */
4387 one.zName = "__one";
4388 two.zName = "__two";
4389
4390 /* Calling sqlite3_vfs_register with 2nd argument of 0 does not
4391 ** change the default VFS
4392 */
4393 pMain = sqlite3_vfs_find(0);
4394 sqlite3_vfs_register(&one, 0);
4395 assert( pMain==0 || pMain==sqlite3_vfs_find(0) );
4396 sqlite3_vfs_register(&two, 0);
4397 assert( pMain==0 || pMain==sqlite3_vfs_find(0) );
4398
4399 /* We can find a VFS by its name */
4400 assert( sqlite3_vfs_find("__one")==&one );
4401 assert( sqlite3_vfs_find("__two")==&two );
4402
4403 /* Calling sqlite_vfs_register with non-zero second parameter changes the
4404 ** default VFS, even if the 1st parameter is an existig VFS that is
4405 ** previously registered as the non-default.
4406 */
4407 sqlite3_vfs_register(&one, 1);
4408 assert( sqlite3_vfs_find("__one")==&one );
4409 assert( sqlite3_vfs_find("__two")==&two );
4410 assert( sqlite3_vfs_find(0)==&one );
4411 sqlite3_vfs_register(&two, 1);
4412 assert( sqlite3_vfs_find("__one")==&one );
4413 assert( sqlite3_vfs_find("__two")==&two );
4414 assert( sqlite3_vfs_find(0)==&two );
4415 if( pMain ){
4416 sqlite3_vfs_register(pMain, 1);
4417 assert( sqlite3_vfs_find("__one")==&one );
4418 assert( sqlite3_vfs_find("__two")==&two );
4419 assert( sqlite3_vfs_find(0)==pMain );
4420 }
4421
4422 /* Unlink the default VFS. Repeat until there are no more VFSes
4423 ** registered.
4424 */
drh9bc54492007-10-23 14:49:59 +00004425 for(i=0; i<sizeof(apVfs)/sizeof(apVfs[0]); i++){
4426 apVfs[i] = sqlite3_vfs_find(0);
4427 if( apVfs[i] ){
4428 assert( apVfs[i]==sqlite3_vfs_find(apVfs[i]->zName) );
4429 sqlite3_vfs_unregister(apVfs[i]);
4430 assert( 0==sqlite3_vfs_find(apVfs[i]->zName) );
4431 }
4432 }
4433 assert( 0==sqlite3_vfs_find(0) );
mlcreech1f045332008-04-08 03:07:54 +00004434
4435 /* Register the main VFS as non-default (will be made default, since
4436 ** it'll be the only one in existence).
4437 */
4438 sqlite3_vfs_register(pMain, 0);
4439 assert( sqlite3_vfs_find(0)==pMain );
4440
4441 /* Un-register the main VFS again to restore an empty VFS list */
4442 sqlite3_vfs_unregister(pMain);
4443 assert( 0==sqlite3_vfs_find(0) );
drh91fd4d42008-01-19 20:11:25 +00004444
4445 /* Relink all VFSes in reverse order. */
drh9bc54492007-10-23 14:49:59 +00004446 for(i=sizeof(apVfs)/sizeof(apVfs[0])-1; i>=0; i--){
4447 if( apVfs[i] ){
4448 sqlite3_vfs_register(apVfs[i], 1);
4449 assert( apVfs[i]==sqlite3_vfs_find(0) );
4450 assert( apVfs[i]==sqlite3_vfs_find(apVfs[i]->zName) );
4451 }
4452 }
drh91fd4d42008-01-19 20:11:25 +00004453
4454 /* Unregister out sample VFSes. */
4455 sqlite3_vfs_unregister(&one);
4456 sqlite3_vfs_unregister(&two);
4457
4458 /* Unregistering a VFS that is not currently registered is harmless */
4459 sqlite3_vfs_unregister(&one);
4460 sqlite3_vfs_unregister(&two);
4461 assert( sqlite3_vfs_find("__one")==0 );
4462 assert( sqlite3_vfs_find("__two")==0 );
4463
4464 /* We should be left with the original default VFS back as the
4465 ** original */
4466 assert( sqlite3_vfs_find(0)==pMain );
4467
drh9bc54492007-10-23 14:49:59 +00004468 return TCL_OK;
4469}
4470
drh93aed5a2008-01-16 17:46:38 +00004471/*
drhc8d75672008-07-08 02:12:37 +00004472** tclcmd: vfs_initfail_test
4473**
4474** This TCL command attempts to vfs_find and vfs_register when the
4475** sqlite3_initialize() interface is failing. All calls should fail.
4476*/
4477static int vfs_initfail_test(
4478 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
4479 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
4480 int objc, /* Number of arguments */
4481 Tcl_Obj *CONST objv[] /* Command arguments */
4482){
4483 sqlite3_vfs one;
4484 one.zName = "__one";
4485
4486 if( sqlite3_vfs_find(0) ) return TCL_ERROR;
4487 sqlite3_vfs_register(&one, 0);
4488 if( sqlite3_vfs_find(0) ) return TCL_ERROR;
4489 sqlite3_vfs_register(&one, 1);
4490 if( sqlite3_vfs_find(0) ) return TCL_ERROR;
4491 return TCL_OK;
4492}
4493
4494/*
drha2820972008-07-07 13:31:58 +00004495** Saved VFSes
4496*/
4497static sqlite3_vfs *apVfs[20];
4498static int nVfs = 0;
4499
4500/*
4501** tclcmd: vfs_unregister_all
4502**
4503** Unregister all VFSes.
4504*/
4505static int vfs_unregister_all(
4506 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
4507 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
4508 int objc, /* Number of arguments */
4509 Tcl_Obj *CONST objv[] /* Command arguments */
4510){
4511 int i;
4512 for(i=0; i<ArraySize(apVfs); i++){
4513 apVfs[i] = sqlite3_vfs_find(0);
4514 if( apVfs[i]==0 ) break;
4515 sqlite3_vfs_unregister(apVfs[i]);
4516 }
4517 nVfs = i;
4518 return TCL_OK;
4519}
4520/*
4521** tclcmd: vfs_reregister_all
4522**
4523** Restore all VFSes that were removed using vfs_unregister_all
4524*/
4525static int vfs_reregister_all(
4526 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
4527 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
4528 int objc, /* Number of arguments */
4529 Tcl_Obj *CONST objv[] /* Command arguments */
4530){
4531 int i;
4532 for(i=0; i<nVfs; i++){
4533 sqlite3_vfs_register(apVfs[i], i==0);
4534 }
4535 return TCL_OK;
4536}
4537
4538
4539/*
drh55176252008-01-22 14:50:16 +00004540** tclcmd: file_control_test DB
4541**
4542** This TCL command runs the sqlite3_file_control interface and
4543** verifies correct operation of the same.
4544*/
4545static int file_control_test(
4546 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
4547 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
4548 int objc, /* Number of arguments */
4549 Tcl_Obj *CONST objv[] /* Command arguments */
4550){
4551 int iArg = 0;
4552 sqlite3 *db;
4553 int rc;
4554
4555 if( objc!=2 ){
4556 Tcl_AppendResult(interp, "wrong # args: should be \"",
4557 Tcl_GetStringFromObj(objv[0], 0), " DB", 0);
4558 return TCL_ERROR;
4559 }
4560 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
4561 rc = sqlite3_file_control(db, 0, 0, &iArg);
4562 assert( rc==SQLITE_ERROR );
4563 rc = sqlite3_file_control(db, "notadatabase", SQLITE_FCNTL_LOCKSTATE, &iArg);
4564 assert( rc==SQLITE_ERROR );
4565 rc = sqlite3_file_control(db, "main", -1, &iArg);
4566 assert( rc==SQLITE_ERROR );
4567 rc = sqlite3_file_control(db, "temp", -1, &iArg);
4568 assert( rc==SQLITE_ERROR );
aswiftaebf4132008-11-21 00:10:35 +00004569
4570 return TCL_OK;
4571}
4572
4573
4574/*
4575** tclcmd: file_control_lasterrno_test DB
4576**
4577** This TCL command runs the sqlite3_file_control interface and
4578** verifies correct operation of the SQLITE_LAST_ERRNO verb.
4579*/
4580static int file_control_lasterrno_test(
4581 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
4582 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
4583 int objc, /* Number of arguments */
4584 Tcl_Obj *CONST objv[] /* Command arguments */
4585){
4586 int iArg = 0;
4587 sqlite3 *db;
4588 int rc;
4589
4590 if( objc!=2 ){
4591 Tcl_AppendResult(interp, "wrong # args: should be \"",
4592 Tcl_GetStringFromObj(objv[0], 0), " DB", 0);
4593 return TCL_ERROR;
4594 }
shane9db299f2009-01-30 05:59:10 +00004595 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
4596 return TCL_ERROR;
4597 }
aswiftaebf4132008-11-21 00:10:35 +00004598 rc = sqlite3_file_control(db, NULL, SQLITE_LAST_ERRNO, &iArg);
shane9db299f2009-01-30 05:59:10 +00004599 if( rc ){
4600 Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
4601 return TCL_ERROR;
4602 }
aswiftaebf4132008-11-21 00:10:35 +00004603 if( iArg!=0 ) {
4604 Tcl_AppendResult(interp, "Unexpected non-zero errno: ",
4605 Tcl_GetStringFromObj(Tcl_NewIntObj(iArg), 0), " ", 0);
4606 return TCL_ERROR;
4607 }
drh55176252008-01-22 14:50:16 +00004608 return TCL_OK;
4609}
4610
4611/*
drhad245812010-06-01 00:28:42 +00004612** tclcmd: file_control_lockproxy_test DB PWD
aswiftaebf4132008-11-21 00:10:35 +00004613**
4614** This TCL command runs the sqlite3_file_control interface and
4615** verifies correct operation of the SQLITE_GET_LOCKPROXYFILE and
4616** SQLITE_SET_LOCKPROXYFILE verbs.
4617*/
4618static int file_control_lockproxy_test(
4619 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
4620 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
4621 int objc, /* Number of arguments */
4622 Tcl_Obj *CONST objv[] /* Command arguments */
4623){
aswiftaebf4132008-11-21 00:10:35 +00004624 sqlite3 *db;
drhad245812010-06-01 00:28:42 +00004625 const char *zPwd;
4626 int nPwd;
aswiftaebf4132008-11-21 00:10:35 +00004627
drhad245812010-06-01 00:28:42 +00004628 if( objc!=3 ){
aswiftaebf4132008-11-21 00:10:35 +00004629 Tcl_AppendResult(interp, "wrong # args: should be \"",
drhad245812010-06-01 00:28:42 +00004630 Tcl_GetStringFromObj(objv[0], 0), " DB PWD", 0);
aswiftaebf4132008-11-21 00:10:35 +00004631 return TCL_ERROR;
4632 }
shane9db299f2009-01-30 05:59:10 +00004633 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
4634 return TCL_ERROR;
4635 }
drhad245812010-06-01 00:28:42 +00004636 zPwd = Tcl_GetStringFromObj(objv[2], &nPwd);
aswiftaebf4132008-11-21 00:10:35 +00004637
drh9b35ea62008-11-29 02:20:26 +00004638#if !defined(SQLITE_ENABLE_LOCKING_STYLE)
drhd2cb50b2009-01-09 21:41:17 +00004639# if defined(__APPLE__)
drh9b35ea62008-11-29 02:20:26 +00004640# define SQLITE_ENABLE_LOCKING_STYLE 1
4641# else
4642# define SQLITE_ENABLE_LOCKING_STYLE 0
4643# endif
4644#endif
drhd2cb50b2009-01-09 21:41:17 +00004645#if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
drh7708e972008-11-29 00:56:52 +00004646 {
aswiftaebf4132008-11-21 00:10:35 +00004647 char *testPath;
drh103fe742008-12-11 02:56:07 +00004648 int rc;
drhad245812010-06-01 00:28:42 +00004649 char proxyPath[400];
4650
4651 if( sizeof(proxyPath)<nPwd+20 ){
4652 Tcl_AppendResult(interp, "PWD too big", (void*)0);
4653 return TCL_ERROR;
4654 }
4655 sprintf(proxyPath, "%s/test.proxy", zPwd);
drh7708e972008-11-29 00:56:52 +00004656 rc = sqlite3_file_control(db, NULL, SQLITE_SET_LOCKPROXYFILE, proxyPath);
4657 if( rc ){
shane9db299f2009-01-30 05:59:10 +00004658 Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
4659 return TCL_ERROR;
drh7708e972008-11-29 00:56:52 +00004660 }
aswiftaebf4132008-11-21 00:10:35 +00004661 rc = sqlite3_file_control(db, NULL, SQLITE_GET_LOCKPROXYFILE, &testPath);
shane9db299f2009-01-30 05:59:10 +00004662 if( strncmp(proxyPath,testPath,11) ){
drh7708e972008-11-29 00:56:52 +00004663 Tcl_AppendResult(interp, "Lock proxy file did not match the "
4664 "previously assigned value", 0);
aswiftaebf4132008-11-21 00:10:35 +00004665 return TCL_ERROR;
4666 }
drh7708e972008-11-29 00:56:52 +00004667 if( rc ){
4668 Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
4669 return TCL_ERROR;
4670 }
4671 rc = sqlite3_file_control(db, NULL, SQLITE_SET_LOCKPROXYFILE, proxyPath);
4672 if( rc ){
4673 Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
4674 return TCL_ERROR;
4675 }
aswiftaebf4132008-11-21 00:10:35 +00004676 }
4677#endif
4678 return TCL_OK;
4679}
4680
4681
4682/*
danielk1977e339d652008-06-28 11:23:00 +00004683** tclcmd: sqlite3_vfs_list
4684**
4685** Return a tcl list containing the names of all registered vfs's.
4686*/
4687static int vfs_list(
4688 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
4689 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
4690 int objc, /* Number of arguments */
4691 Tcl_Obj *CONST objv[] /* Command arguments */
4692){
4693 sqlite3_vfs *pVfs;
4694 Tcl_Obj *pRet = Tcl_NewObj();
4695 if( objc!=1 ){
4696 Tcl_WrongNumArgs(interp, 1, objv, "");
4697 return TCL_ERROR;
4698 }
4699 for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){
4700 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewStringObj(pVfs->zName, -1));
4701 }
4702 Tcl_SetObjResult(interp, pRet);
4703 return TCL_OK;
4704}
4705
4706/*
drhb1a6c3c2008-03-20 16:30:17 +00004707** tclcmd: sqlite3_limit DB ID VALUE
4708**
4709** This TCL command runs the sqlite3_limit interface and
4710** verifies correct operation of the same.
4711*/
4712static int test_limit(
4713 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
4714 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
4715 int objc, /* Number of arguments */
4716 Tcl_Obj *CONST objv[] /* Command arguments */
4717){
4718 sqlite3 *db;
4719 int rc;
4720 static const struct {
4721 char *zName;
4722 int id;
4723 } aId[] = {
4724 { "SQLITE_LIMIT_LENGTH", SQLITE_LIMIT_LENGTH },
4725 { "SQLITE_LIMIT_SQL_LENGTH", SQLITE_LIMIT_SQL_LENGTH },
4726 { "SQLITE_LIMIT_COLUMN", SQLITE_LIMIT_COLUMN },
4727 { "SQLITE_LIMIT_EXPR_DEPTH", SQLITE_LIMIT_EXPR_DEPTH },
4728 { "SQLITE_LIMIT_COMPOUND_SELECT", SQLITE_LIMIT_COMPOUND_SELECT },
4729 { "SQLITE_LIMIT_VDBE_OP", SQLITE_LIMIT_VDBE_OP },
4730 { "SQLITE_LIMIT_FUNCTION_ARG", SQLITE_LIMIT_FUNCTION_ARG },
4731 { "SQLITE_LIMIT_ATTACHED", SQLITE_LIMIT_ATTACHED },
4732 { "SQLITE_LIMIT_LIKE_PATTERN_LENGTH", SQLITE_LIMIT_LIKE_PATTERN_LENGTH },
4733 { "SQLITE_LIMIT_VARIABLE_NUMBER", SQLITE_LIMIT_VARIABLE_NUMBER },
drh417168a2009-09-07 18:14:02 +00004734 { "SQLITE_LIMIT_TRIGGER_DEPTH", SQLITE_LIMIT_TRIGGER_DEPTH },
drh521cc842008-04-15 02:36:33 +00004735
4736 /* Out of range test cases */
4737 { "SQLITE_LIMIT_TOOSMALL", -1, },
drh417168a2009-09-07 18:14:02 +00004738 { "SQLITE_LIMIT_TOOBIG", SQLITE_LIMIT_TRIGGER_DEPTH+1 },
drhb1a6c3c2008-03-20 16:30:17 +00004739 };
4740 int i, id;
4741 int val;
4742 const char *zId;
4743
4744 if( objc!=4 ){
4745 Tcl_AppendResult(interp, "wrong # args: should be \"",
4746 Tcl_GetStringFromObj(objv[0], 0), " DB ID VALUE", 0);
4747 return TCL_ERROR;
4748 }
4749 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
4750 zId = Tcl_GetString(objv[2]);
4751 for(i=0; i<sizeof(aId)/sizeof(aId[0]); i++){
4752 if( strcmp(zId, aId[i].zName)==0 ){
4753 id = aId[i].id;
4754 break;
4755 }
4756 }
4757 if( i>=sizeof(aId)/sizeof(aId[0]) ){
4758 Tcl_AppendResult(interp, "unknown limit type: ", zId, (char*)0);
4759 return TCL_ERROR;
4760 }
4761 if( Tcl_GetIntFromObj(interp, objv[3], &val) ) return TCL_ERROR;
4762 rc = sqlite3_limit(db, id, val);
4763 Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
4764 return TCL_OK;
4765}
4766
4767/*
drh93aed5a2008-01-16 17:46:38 +00004768** tclcmd: save_prng_state
drha2820972008-07-07 13:31:58 +00004769**
4770** Save the state of the pseudo-random number generator.
4771** At the same time, verify that sqlite3_test_control works even when
4772** called with an out-of-range opcode.
drh93aed5a2008-01-16 17:46:38 +00004773*/
4774static int save_prng_state(
4775 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
4776 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
4777 int objc, /* Number of arguments */
4778 Tcl_Obj *CONST objv[] /* Command arguments */
4779){
drha2820972008-07-07 13:31:58 +00004780 int rc = sqlite3_test_control(9999);
4781 assert( rc==0 );
4782 rc = sqlite3_test_control(-1);
4783 assert( rc==0 );
drh2fa18682008-03-19 14:15:34 +00004784 sqlite3_test_control(SQLITE_TESTCTRL_PRNG_SAVE);
drh93aed5a2008-01-16 17:46:38 +00004785 return TCL_OK;
4786}
4787/*
4788** tclcmd: restore_prng_state
4789*/
4790static int restore_prng_state(
4791 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
4792 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
4793 int objc, /* Number of arguments */
4794 Tcl_Obj *CONST objv[] /* Command arguments */
4795){
drh2fa18682008-03-19 14:15:34 +00004796 sqlite3_test_control(SQLITE_TESTCTRL_PRNG_RESTORE);
drh93aed5a2008-01-16 17:46:38 +00004797 return TCL_OK;
4798}
4799/*
4800** tclcmd: reset_prng_state
4801*/
4802static int reset_prng_state(
4803 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
4804 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
4805 int objc, /* Number of arguments */
4806 Tcl_Obj *CONST objv[] /* Command arguments */
4807){
drh2fa18682008-03-19 14:15:34 +00004808 sqlite3_test_control(SQLITE_TESTCTRL_PRNG_RESET);
drh93aed5a2008-01-16 17:46:38 +00004809 return TCL_OK;
4810}
4811
danielk1977062d4cb2008-08-29 09:10:02 +00004812/*
4813** tclcmd: pcache_stats
4814*/
4815static int test_pcache_stats(
4816 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
4817 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
4818 int objc, /* Number of arguments */
4819 Tcl_Obj *CONST objv[] /* Command arguments */
4820){
4821 int nMin;
4822 int nMax;
4823 int nCurrent;
4824 int nRecyclable;
4825 Tcl_Obj *pRet;
4826
4827 sqlite3PcacheStats(&nCurrent, &nMax, &nMin, &nRecyclable);
4828
4829 pRet = Tcl_NewObj();
4830 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewStringObj("current", -1));
4831 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(nCurrent));
4832 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewStringObj("max", -1));
4833 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(nMax));
4834 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewStringObj("min", -1));
4835 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(nMin));
4836 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewStringObj("recyclable", -1));
4837 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(nRecyclable));
4838
4839 Tcl_SetObjResult(interp, pRet);
4840
4841 return TCL_OK;
4842}
4843
drh69910da2009-03-27 12:32:54 +00004844#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
danielk1977404ca072009-03-16 13:19:36 +00004845static void test_unlock_notify_cb(void **aArg, int nArg){
4846 int ii;
4847 for(ii=0; ii<nArg; ii++){
4848 Tcl_EvalEx((Tcl_Interp *)aArg[ii], "unlock_notify", -1, TCL_EVAL_GLOBAL);
4849 }
4850}
drh69910da2009-03-27 12:32:54 +00004851#endif /* SQLITE_ENABLE_UNLOCK_NOTIFY */
danielk1977404ca072009-03-16 13:19:36 +00004852
4853/*
4854** tclcmd: sqlite3_unlock_notify db
4855*/
4856#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
4857static int test_unlock_notify(
4858 ClientData clientData, /* Unused */
4859 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
4860 int objc, /* Number of arguments */
4861 Tcl_Obj *CONST objv[] /* Command arguments */
4862){
4863 sqlite3 *db;
4864 int rc;
4865
4866 if( objc!=2 ){
4867 Tcl_WrongNumArgs(interp, 1, objv, "DB");
4868 return TCL_ERROR;
4869 }
4870
4871 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
4872 return TCL_ERROR;
4873 }
4874 rc = sqlite3_unlock_notify(db, test_unlock_notify_cb, (void *)interp);
4875 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
4876 return TCL_OK;
4877}
4878#endif
4879
dan87c1fe12010-05-03 12:14:15 +00004880/*
4881** tclcmd: sqlite3_wal_checkpoint db ?NAME?
4882*/
4883static int test_wal_checkpoint(
4884 ClientData clientData, /* Unused */
4885 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
4886 int objc, /* Number of arguments */
4887 Tcl_Obj *CONST objv[] /* Command arguments */
4888){
4889 char *zDb = 0;
4890 sqlite3 *db;
4891 int rc;
4892
4893 if( objc!=3 && objc!=2 ){
4894 Tcl_WrongNumArgs(interp, 1, objv, "DB ?NAME?");
4895 return TCL_ERROR;
4896 }
4897
4898 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
4899 return TCL_ERROR;
4900 }
4901 if( objc==3 ){
4902 zDb = Tcl_GetString(objv[2]);
4903 }
4904 rc = sqlite3_wal_checkpoint(db, zDb);
4905 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
4906 return TCL_OK;
4907}
4908
drh9bc54492007-10-23 14:49:59 +00004909
4910/*
drha2c8a952009-10-13 18:38:34 +00004911** tcl_objproc COMMANDNAME ARGS...
4912**
4913** Run a TCL command using its objProc interface. Throw an error if
4914** the command has no objProc interface.
4915*/
4916static int runAsObjProc(
4917 void * clientData,
4918 Tcl_Interp *interp,
4919 int objc,
4920 Tcl_Obj *CONST objv[]
4921){
4922 Tcl_CmdInfo cmdInfo;
4923 if( objc<2 ){
4924 Tcl_WrongNumArgs(interp, 1, objv, "COMMAND ...");
4925 return TCL_ERROR;
4926 }
4927 if( !Tcl_GetCommandInfo(interp, Tcl_GetString(objv[1]), &cmdInfo) ){
4928 Tcl_AppendResult(interp, "command not found: ",
4929 Tcl_GetString(objv[1]), (char*)0);
4930 return TCL_ERROR;
4931 }
4932 if( cmdInfo.objProc==0 ){
4933 Tcl_AppendResult(interp, "command has no objProc: ",
4934 Tcl_GetString(objv[1]), (char*)0);
4935 return TCL_ERROR;
4936 }
4937 return cmdInfo.objProc(cmdInfo.objClientData, interp, objc-1, objv+1);
4938}
4939
4940
4941/*
drhd1bf3512001-04-07 15:24:33 +00004942** Register commands with the TCL interpreter.
4943*/
4944int Sqlitetest1_Init(Tcl_Interp *interp){
danielk19776f8a5032004-05-10 10:34:51 +00004945 extern int sqlite3_search_count;
dan0ff297e2009-09-25 17:03:14 +00004946 extern int sqlite3_found_count;
danielk19776f8a5032004-05-10 10:34:51 +00004947 extern int sqlite3_interrupt_count;
4948 extern int sqlite3_open_file_count;
drh6bf89572004-11-03 16:27:01 +00004949 extern int sqlite3_sort_count;
danielk19776f8a5032004-05-10 10:34:51 +00004950 extern int sqlite3_current_time;
drh84a2bf62010-03-05 13:41:06 +00004951#if SQLITE_OS_UNIX && defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
aswiftaebf4132008-11-21 00:10:35 +00004952 extern int sqlite3_hostid_num;
pweilbacheraabbed22008-11-21 23:35:02 +00004953#endif
drhae7e1512007-05-02 16:51:59 +00004954 extern int sqlite3_max_blobsize;
drh16a9b832007-05-05 18:39:25 +00004955 extern int sqlite3BtreeSharedCacheReport(void*,
4956 Tcl_Interp*,int,Tcl_Obj*CONST*);
drhc2eef3b2002-08-31 18:53:06 +00004957 static struct {
4958 char *zName;
4959 Tcl_CmdProc *xProc;
4960 } aCmd[] = {
drh27641702007-08-22 02:56:42 +00004961 { "db_enter", (Tcl_CmdProc*)db_enter },
4962 { "db_leave", (Tcl_CmdProc*)db_leave },
drhd3d39e92004-05-20 22:16:29 +00004963 { "sqlite3_mprintf_int", (Tcl_CmdProc*)sqlite3_mprintf_int },
drhe9707672004-06-25 01:10:48 +00004964 { "sqlite3_mprintf_int64", (Tcl_CmdProc*)sqlite3_mprintf_int64 },
drhc5cad1e2009-02-01 00:21:09 +00004965 { "sqlite3_mprintf_long", (Tcl_CmdProc*)sqlite3_mprintf_long },
drhd3d39e92004-05-20 22:16:29 +00004966 { "sqlite3_mprintf_str", (Tcl_CmdProc*)sqlite3_mprintf_str },
drhb3738b62007-03-31 15:02:49 +00004967 { "sqlite3_snprintf_str", (Tcl_CmdProc*)sqlite3_snprintf_str },
drhe29b1a02004-07-17 21:56:09 +00004968 { "sqlite3_mprintf_stronly", (Tcl_CmdProc*)sqlite3_mprintf_stronly},
drhd3d39e92004-05-20 22:16:29 +00004969 { "sqlite3_mprintf_double", (Tcl_CmdProc*)sqlite3_mprintf_double },
4970 { "sqlite3_mprintf_scaled", (Tcl_CmdProc*)sqlite3_mprintf_scaled },
drh63782852005-08-30 19:30:59 +00004971 { "sqlite3_mprintf_hexdouble", (Tcl_CmdProc*)sqlite3_mprintf_hexdouble},
drhd3d39e92004-05-20 22:16:29 +00004972 { "sqlite3_mprintf_z_test", (Tcl_CmdProc*)test_mprintf_z },
drh05a82982006-03-19 13:00:25 +00004973 { "sqlite3_mprintf_n_test", (Tcl_CmdProc*)test_mprintf_n },
drh68853902007-05-07 11:24:30 +00004974 { "sqlite3_snprintf_int", (Tcl_CmdProc*)test_snprintf_int },
drhd3d39e92004-05-20 22:16:29 +00004975 { "sqlite3_last_insert_rowid", (Tcl_CmdProc*)test_last_rowid },
4976 { "sqlite3_exec_printf", (Tcl_CmdProc*)test_exec_printf },
drh5bd98ae2009-01-07 18:24:03 +00004977 { "sqlite3_exec_hex", (Tcl_CmdProc*)test_exec_hex },
drhb62c3352006-11-23 09:39:16 +00004978 { "sqlite3_exec", (Tcl_CmdProc*)test_exec },
4979 { "sqlite3_exec_nr", (Tcl_CmdProc*)test_exec_nr },
shane8225f5a2008-07-31 02:05:04 +00004980#ifndef SQLITE_OMIT_GET_TABLE
drhd3d39e92004-05-20 22:16:29 +00004981 { "sqlite3_get_table_printf", (Tcl_CmdProc*)test_get_table_printf },
shane8225f5a2008-07-31 02:05:04 +00004982#endif
drhd3d39e92004-05-20 22:16:29 +00004983 { "sqlite3_close", (Tcl_CmdProc*)sqlite_test_close },
4984 { "sqlite3_create_function", (Tcl_CmdProc*)test_create_function },
4985 { "sqlite3_create_aggregate", (Tcl_CmdProc*)test_create_aggregate },
4986 { "sqlite_register_test_function", (Tcl_CmdProc*)test_register_func },
4987 { "sqlite_abort", (Tcl_CmdProc*)sqlite_abort },
drh25d65432004-07-22 15:02:25 +00004988 { "sqlite_bind", (Tcl_CmdProc*)test_bind },
4989 { "breakpoint", (Tcl_CmdProc*)test_breakpoint },
4990 { "sqlite3_key", (Tcl_CmdProc*)test_key },
4991 { "sqlite3_rekey", (Tcl_CmdProc*)test_rekey },
drhcacb2082005-01-11 15:28:33 +00004992 { "sqlite_set_magic", (Tcl_CmdProc*)sqlite_set_magic },
drhc5cdca62005-01-11 16:54:14 +00004993 { "sqlite3_interrupt", (Tcl_CmdProc*)test_interrupt },
drh3e1d8e62005-05-26 16:23:34 +00004994 { "sqlite_delete_function", (Tcl_CmdProc*)delete_function },
4995 { "sqlite_delete_collation", (Tcl_CmdProc*)delete_collation },
4996 { "sqlite3_get_autocommit", (Tcl_CmdProc*)get_autocommit },
drh79158e12005-09-06 21:40:45 +00004997 { "sqlite3_stack_used", (Tcl_CmdProc*)test_stack_used },
drh30867652006-07-06 10:59:57 +00004998 { "sqlite3_busy_timeout", (Tcl_CmdProc*)test_busy_timeout },
drh3c23a882007-01-09 14:01:13 +00004999 { "printf", (Tcl_CmdProc*)test_printf },
mlcreech3a00f902008-03-04 17:45:01 +00005000 { "sqlite3IoTrace", (Tcl_CmdProc*)test_io_trace },
drhc2eef3b2002-08-31 18:53:06 +00005001 };
danielk197751e3d8e2004-05-20 01:12:34 +00005002 static struct {
5003 char *zName;
5004 Tcl_ObjCmdProc *xProc;
danielk197704f2e682004-05-27 01:04:07 +00005005 void *clientData;
danielk197751e3d8e2004-05-20 01:12:34 +00005006 } aObjCmd[] = {
drhdddca282006-01-03 00:33:50 +00005007 { "sqlite3_connection_pointer", get_sqlite_pointer, 0 },
drh241db312004-06-22 12:46:53 +00005008 { "sqlite3_bind_int", test_bind_int, 0 },
drhb026e052007-05-02 01:34:31 +00005009 { "sqlite3_bind_zeroblob", test_bind_zeroblob, 0 },
drh241db312004-06-22 12:46:53 +00005010 { "sqlite3_bind_int64", test_bind_int64, 0 },
5011 { "sqlite3_bind_double", test_bind_double, 0 },
danielk197704f2e682004-05-27 01:04:07 +00005012 { "sqlite3_bind_null", test_bind_null ,0 },
5013 { "sqlite3_bind_text", test_bind_text ,0 },
5014 { "sqlite3_bind_text16", test_bind_text16 ,0 },
5015 { "sqlite3_bind_blob", test_bind_blob ,0 },
drh75f6a032004-07-15 14:15:00 +00005016 { "sqlite3_bind_parameter_count", test_bind_parameter_count, 0},
drh895d7472004-08-20 16:02:39 +00005017 { "sqlite3_bind_parameter_name", test_bind_parameter_name, 0},
drhfa6bc002004-09-07 16:19:52 +00005018 { "sqlite3_bind_parameter_index", test_bind_parameter_index, 0},
danielk1977600dd0b2005-01-20 01:14:23 +00005019 { "sqlite3_clear_bindings", test_clear_bindings, 0},
drhf9cb7f52006-06-27 20:06:44 +00005020 { "sqlite3_sleep", test_sleep, 0},
danielk197704f2e682004-05-27 01:04:07 +00005021 { "sqlite3_errcode", test_errcode ,0 },
drh99dfe5e2008-10-30 15:03:15 +00005022 { "sqlite3_extended_errcode", test_ex_errcode ,0 },
danielk197704f2e682004-05-27 01:04:07 +00005023 { "sqlite3_errmsg", test_errmsg ,0 },
5024 { "sqlite3_errmsg16", test_errmsg16 ,0 },
5025 { "sqlite3_open", test_open ,0 },
5026 { "sqlite3_open16", test_open16 ,0 },
danielk1977bc6ada42004-06-30 08:20:16 +00005027 { "sqlite3_complete16", test_complete16 ,0 },
danielk197704f2e682004-05-27 01:04:07 +00005028
5029 { "sqlite3_prepare", test_prepare ,0 },
5030 { "sqlite3_prepare16", test_prepare16 ,0 },
drhb900aaf2006-11-09 00:24:53 +00005031 { "sqlite3_prepare_v2", test_prepare_v2 ,0 },
drh4837f532008-05-23 14:49:49 +00005032 { "sqlite3_prepare_tkt3134", test_prepare_tkt3134, 0},
drhb900aaf2006-11-09 00:24:53 +00005033 { "sqlite3_prepare16_v2", test_prepare16_v2 ,0 },
danielk197704f2e682004-05-27 01:04:07 +00005034 { "sqlite3_finalize", test_finalize ,0 },
drhd1d38482008-10-07 23:46:38 +00005035 { "sqlite3_stmt_status", test_stmt_status ,0 },
danielk197704f2e682004-05-27 01:04:07 +00005036 { "sqlite3_reset", test_reset ,0 },
drhd89bd002005-01-22 03:03:54 +00005037 { "sqlite3_expired", test_expired ,0 },
drhf8db1bc2005-04-22 02:38:37 +00005038 { "sqlite3_transfer_bindings", test_transfer_bind ,0 },
danielk1977fbcd5852004-06-15 02:44:18 +00005039 { "sqlite3_changes", test_changes ,0 },
5040 { "sqlite3_step", test_step ,0 },
danielk1977404ca072009-03-16 13:19:36 +00005041 { "sqlite3_sql", test_sql ,0 },
drhbb5a9c32008-06-19 02:52:25 +00005042 { "sqlite3_next_stmt", test_next_stmt ,0 },
danielk197704f2e682004-05-27 01:04:07 +00005043
drhb4bc7052006-01-11 23:40:33 +00005044 { "sqlite3_release_memory", test_release_memory, 0},
5045 { "sqlite3_soft_heap_limit", test_soft_heap_limit, 0},
drhb4bc7052006-01-11 23:40:33 +00005046 { "sqlite3_thread_cleanup", test_thread_cleanup, 0},
drhc6ba55f2007-04-05 17:36:18 +00005047 { "sqlite3_pager_refcounts", test_pager_refcounts, 0},
drh6aafc292006-01-05 15:50:06 +00005048
drhc2e87a32006-06-27 15:16:14 +00005049 { "sqlite3_load_extension", test_load_extension, 0},
5050 { "sqlite3_enable_load_extension", test_enable_load, 0},
drh4ac285a2006-09-15 07:28:50 +00005051 { "sqlite3_extended_result_codes", test_extended_result_codes, 0},
drhb1a6c3c2008-03-20 16:30:17 +00005052 { "sqlite3_limit", test_limit, 0},
drhc2e87a32006-06-27 15:16:14 +00005053
drh93aed5a2008-01-16 17:46:38 +00005054 { "save_prng_state", save_prng_state, 0 },
5055 { "restore_prng_state", restore_prng_state, 0 },
5056 { "reset_prng_state", reset_prng_state, 0 },
drha2c8a952009-10-13 18:38:34 +00005057 { "tcl_objproc", runAsObjProc, 0 },
drh93aed5a2008-01-16 17:46:38 +00005058
danielk197704f2e682004-05-27 01:04:07 +00005059 /* sqlite3_column_*() API */
5060 { "sqlite3_column_count", test_column_count ,0 },
5061 { "sqlite3_data_count", test_data_count ,0 },
5062 { "sqlite3_column_type", test_column_type ,0 },
danielk1977ea61b2c2004-05-27 01:49:51 +00005063 { "sqlite3_column_blob", test_column_blob ,0 },
danielk197704f2e682004-05-27 01:04:07 +00005064 { "sqlite3_column_double", test_column_double ,0 },
5065 { "sqlite3_column_int64", test_column_int64 ,0 },
danielk197744a376f2008-08-12 15:04:58 +00005066 { "sqlite3_column_text", test_stmt_utf8, (void*)sqlite3_column_text },
5067 { "sqlite3_column_name", test_stmt_utf8, (void*)sqlite3_column_name },
5068 { "sqlite3_column_int", test_stmt_int, (void*)sqlite3_column_int },
5069 { "sqlite3_column_bytes", test_stmt_int, (void*)sqlite3_column_bytes},
drh3f913572008-03-22 01:07:17 +00005070#ifndef SQLITE_OMIT_DECLTYPE
danielk197744a376f2008-08-12 15:04:58 +00005071 { "sqlite3_column_decltype",test_stmt_utf8,(void*)sqlite3_column_decltype},
drh3f913572008-03-22 01:07:17 +00005072#endif
danielk19774b1ae992006-02-10 03:06:10 +00005073#ifdef SQLITE_ENABLE_COLUMN_METADATA
danielk197744a376f2008-08-12 15:04:58 +00005074{ "sqlite3_column_database_name",test_stmt_utf8,(void*)sqlite3_column_database_name},
5075{ "sqlite3_column_table_name",test_stmt_utf8,(void*)sqlite3_column_table_name},
5076{ "sqlite3_column_origin_name",test_stmt_utf8,(void*)sqlite3_column_origin_name},
danielk19774b1ae992006-02-10 03:06:10 +00005077#endif
danielk1977955de522006-02-10 02:27:42 +00005078
drh6c626082004-11-14 21:56:29 +00005079#ifndef SQLITE_OMIT_UTF16
danielk197744a376f2008-08-12 15:04:58 +00005080 { "sqlite3_column_bytes16", test_stmt_int, (void*)sqlite3_column_bytes16 },
5081 { "sqlite3_column_text16", test_stmt_utf16, (void*)sqlite3_column_text16},
5082 { "sqlite3_column_name16", test_stmt_utf16, (void*)sqlite3_column_name16},
drh7d9bd4e2006-02-16 18:16:36 +00005083 { "add_alignment_test_collations", add_alignment_test_collations, 0 },
drh3f913572008-03-22 01:07:17 +00005084#ifndef SQLITE_OMIT_DECLTYPE
danielk197744a376f2008-08-12 15:04:58 +00005085 { "sqlite3_column_decltype16",test_stmt_utf16,(void*)sqlite3_column_decltype16},
drh3f913572008-03-22 01:07:17 +00005086#endif
danielk19774b1ae992006-02-10 03:06:10 +00005087#ifdef SQLITE_ENABLE_COLUMN_METADATA
danielk1977955de522006-02-10 02:27:42 +00005088{"sqlite3_column_database_name16",
5089 test_stmt_utf16, sqlite3_column_database_name16},
danielk197744a376f2008-08-12 15:04:58 +00005090{"sqlite3_column_table_name16", test_stmt_utf16, (void*)sqlite3_column_table_name16},
5091{"sqlite3_column_origin_name16", test_stmt_utf16, (void*)sqlite3_column_origin_name16},
drh6c626082004-11-14 21:56:29 +00005092#endif
danielk19774b1ae992006-02-10 03:06:10 +00005093#endif
danielk1977a393c032007-05-07 14:58:53 +00005094 { "sqlite3_create_collation_v2", test_create_collation_v2, 0 },
danielk1977a9808b32007-05-07 09:32:45 +00005095 { "sqlite3_global_recover", test_global_recover, 0 },
5096 { "working_64bit_int", working_64bit_int, 0 },
drh9bc54492007-10-23 14:49:59 +00005097 { "vfs_unlink_test", vfs_unlink_test, 0 },
drhc8d75672008-07-08 02:12:37 +00005098 { "vfs_initfail_test", vfs_initfail_test, 0 },
drha2820972008-07-07 13:31:58 +00005099 { "vfs_unregister_all", vfs_unregister_all, 0 },
5100 { "vfs_reregister_all", vfs_reregister_all, 0 },
drh55176252008-01-22 14:50:16 +00005101 { "file_control_test", file_control_test, 0 },
aswiftaebf4132008-11-21 00:10:35 +00005102 { "file_control_lasterrno_test", file_control_lasterrno_test, 0 },
5103 { "file_control_lockproxy_test", file_control_lockproxy_test, 0 },
danielk1977e339d652008-06-28 11:23:00 +00005104 { "sqlite3_vfs_list", vfs_list, 0 },
danielk197704f2e682004-05-27 01:04:07 +00005105
danielk19779a1d0ab2004-06-01 14:09:28 +00005106 /* Functions from os.h */
drh5436dc22004-11-14 04:04:17 +00005107#ifndef SQLITE_OMIT_UTF16
danielk1977312d6b32004-06-29 13:18:23 +00005108 { "add_test_collate", test_collate, 0 },
5109 { "add_test_collate_needed", test_collate_needed, 0 },
5110 { "add_test_function", test_function, 0 },
drh5436dc22004-11-14 04:04:17 +00005111#endif
danielk1977312d6b32004-06-29 13:18:23 +00005112 { "sqlite3_test_errstr", test_errstr, 0 },
drh92febd92004-08-20 18:34:20 +00005113 { "tcl_variable_type", tcl_variable_type, 0 },
danielk1977aef0bf62005-12-30 16:28:01 +00005114#ifndef SQLITE_OMIT_SHARED_CACHE
drh6f7adc82006-01-11 21:41:20 +00005115 { "sqlite3_enable_shared_cache", test_enable_shared, 0 },
drh16a9b832007-05-05 18:39:25 +00005116 { "sqlite3_shared_cache_report", sqlite3BtreeSharedCacheReport, 0},
danielk1977aef0bf62005-12-30 16:28:01 +00005117#endif
danielk1977161fb792006-01-24 10:58:21 +00005118 { "sqlite3_libversion_number", test_libversion_number, 0 },
danielk1977deb802c2006-02-09 13:43:28 +00005119#ifdef SQLITE_ENABLE_COLUMN_METADATA
5120 { "sqlite3_table_column_metadata", test_table_column_metadata, 0 },
5121#endif
danielk1977dcbb5d32007-05-04 18:36:44 +00005122#ifndef SQLITE_OMIT_INCRBLOB
5123 { "sqlite3_blob_read", test_blob_read, 0 },
5124 { "sqlite3_blob_write", test_blob_write, 0 },
5125#endif
danielk1977062d4cb2008-08-29 09:10:02 +00005126 { "pcache_stats", test_pcache_stats, 0 },
danielk1977404ca072009-03-16 13:19:36 +00005127#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
5128 { "sqlite3_unlock_notify", test_unlock_notify, 0 },
5129#endif
dan87c1fe12010-05-03 12:14:15 +00005130 { "sqlite3_wal_checkpoint", test_wal_checkpoint, 0 },
danielk197751e3d8e2004-05-20 01:12:34 +00005131 };
drh1398ad32005-01-19 23:24:50 +00005132 static int bitmask_size = sizeof(Bitmask)*8;
drhc2eef3b2002-08-31 18:53:06 +00005133 int i;
drhb851b2c2005-03-10 14:11:12 +00005134 extern int sqlite3_sync_count, sqlite3_fullsync_count;
drhaf6df112005-06-07 02:12:30 +00005135 extern int sqlite3_opentemp_count;
drh55ef4d92005-08-14 01:20:37 +00005136 extern int sqlite3_like_count;
drhdd735212007-02-24 13:53:05 +00005137 extern int sqlite3_xferopt_count;
drh538f5702007-04-13 02:14:30 +00005138 extern int sqlite3_pager_readdb_count;
5139 extern int sqlite3_pager_writedb_count;
5140 extern int sqlite3_pager_writej_count;
danielk197729bafea2008-06-26 10:41:19 +00005141#if SQLITE_OS_WIN
drhc0929982005-09-05 19:08:29 +00005142 extern int sqlite3_os_type;
5143#endif
drh8b3d9902005-08-19 00:14:42 +00005144#ifdef SQLITE_DEBUG
mlcreech3a00f902008-03-04 17:45:01 +00005145 extern int sqlite3WhereTrace;
5146 extern int sqlite3OSTrace;
5147 extern int sqlite3VdbeAddopTrace;
drhc74c3332010-05-31 12:15:19 +00005148 extern int sqlite3WalTrace;
drh549c8b62005-09-19 13:15:23 +00005149#endif
5150#ifdef SQLITE_TEST
5151 extern char sqlite3_query_plan[];
drh9042f392005-07-15 23:24:23 +00005152 static char *query_plan = sqlite3_query_plan;
danielk197733e89032008-12-17 15:18:17 +00005153#ifdef SQLITE_ENABLE_FTS3
5154 extern int sqlite3_fts3_enable_parentheses;
5155#endif
drh48083ce2005-09-19 12:37:27 +00005156#endif
drhc2eef3b2002-08-31 18:53:06 +00005157
5158 for(i=0; i<sizeof(aCmd)/sizeof(aCmd[0]); i++){
5159 Tcl_CreateCommand(interp, aCmd[i].zName, aCmd[i].xProc, 0, 0);
5160 }
danielk197751e3d8e2004-05-20 01:12:34 +00005161 for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){
danielk1977c572ef72004-05-27 09:28:41 +00005162 Tcl_CreateObjCommand(interp, aObjCmd[i].zName,
5163 aObjCmd[i].xProc, aObjCmd[i].clientData, 0);
danielk197751e3d8e2004-05-20 01:12:34 +00005164 }
danielk19776490beb2004-05-11 06:17:21 +00005165 Tcl_LinkVar(interp, "sqlite_search_count",
danielk19776f8a5032004-05-10 10:34:51 +00005166 (char*)&sqlite3_search_count, TCL_LINK_INT);
dan0ff297e2009-09-25 17:03:14 +00005167 Tcl_LinkVar(interp, "sqlite_found_count",
5168 (char*)&sqlite3_found_count, TCL_LINK_INT);
drh6bf89572004-11-03 16:27:01 +00005169 Tcl_LinkVar(interp, "sqlite_sort_count",
5170 (char*)&sqlite3_sort_count, TCL_LINK_INT);
drhae7e1512007-05-02 16:51:59 +00005171 Tcl_LinkVar(interp, "sqlite3_max_blobsize",
5172 (char*)&sqlite3_max_blobsize, TCL_LINK_INT);
drh55ef4d92005-08-14 01:20:37 +00005173 Tcl_LinkVar(interp, "sqlite_like_count",
5174 (char*)&sqlite3_like_count, TCL_LINK_INT);
danielk19776490beb2004-05-11 06:17:21 +00005175 Tcl_LinkVar(interp, "sqlite_interrupt_count",
danielk19776f8a5032004-05-10 10:34:51 +00005176 (char*)&sqlite3_interrupt_count, TCL_LINK_INT);
danielk19776490beb2004-05-11 06:17:21 +00005177 Tcl_LinkVar(interp, "sqlite_open_file_count",
danielk19776f8a5032004-05-10 10:34:51 +00005178 (char*)&sqlite3_open_file_count, TCL_LINK_INT);
danielk19776490beb2004-05-11 06:17:21 +00005179 Tcl_LinkVar(interp, "sqlite_current_time",
danielk19776f8a5032004-05-10 10:34:51 +00005180 (char*)&sqlite3_current_time, TCL_LINK_INT);
drh84a2bf62010-03-05 13:41:06 +00005181#if SQLITE_OS_UNIX && defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
aswiftaebf4132008-11-21 00:10:35 +00005182 Tcl_LinkVar(interp, "sqlite_hostid_num",
5183 (char*)&sqlite3_hostid_num, TCL_LINK_INT);
pweilbacheraabbed22008-11-21 23:35:02 +00005184#endif
drhdd735212007-02-24 13:53:05 +00005185 Tcl_LinkVar(interp, "sqlite3_xferopt_count",
5186 (char*)&sqlite3_xferopt_count, TCL_LINK_INT);
drh538f5702007-04-13 02:14:30 +00005187 Tcl_LinkVar(interp, "sqlite3_pager_readdb_count",
5188 (char*)&sqlite3_pager_readdb_count, TCL_LINK_INT);
5189 Tcl_LinkVar(interp, "sqlite3_pager_writedb_count",
5190 (char*)&sqlite3_pager_writedb_count, TCL_LINK_INT);
5191 Tcl_LinkVar(interp, "sqlite3_pager_writej_count",
5192 (char*)&sqlite3_pager_writej_count, TCL_LINK_INT);
danielk19774b2688a2006-06-20 11:01:07 +00005193#ifndef SQLITE_OMIT_UTF16
drh7d9bd4e2006-02-16 18:16:36 +00005194 Tcl_LinkVar(interp, "unaligned_string_counter",
5195 (char*)&unaligned_string_counter, TCL_LINK_INT);
danielk19774b2688a2006-06-20 11:01:07 +00005196#endif
drh268803a2005-12-14 20:11:30 +00005197#ifndef SQLITE_OMIT_UTF16
5198 Tcl_LinkVar(interp, "sqlite_last_needed_collation",
5199 (char*)&pzNeededCollation, TCL_LINK_STRING|TCL_LINK_READ_ONLY);
5200#endif
danielk197729bafea2008-06-26 10:41:19 +00005201#if SQLITE_OS_WIN
drhc0929982005-09-05 19:08:29 +00005202 Tcl_LinkVar(interp, "sqlite_os_type",
5203 (char*)&sqlite3_os_type, TCL_LINK_INT);
5204#endif
drh549c8b62005-09-19 13:15:23 +00005205#ifdef SQLITE_TEST
5206 Tcl_LinkVar(interp, "sqlite_query_plan",
5207 (char*)&query_plan, TCL_LINK_STRING|TCL_LINK_READ_ONLY);
5208#endif
drh8b3d9902005-08-19 00:14:42 +00005209#ifdef SQLITE_DEBUG
5210 Tcl_LinkVar(interp, "sqlite_addop_trace",
mlcreech3a00f902008-03-04 17:45:01 +00005211 (char*)&sqlite3VdbeAddopTrace, TCL_LINK_INT);
drh48083ce2005-09-19 12:37:27 +00005212 Tcl_LinkVar(interp, "sqlite_where_trace",
mlcreech3a00f902008-03-04 17:45:01 +00005213 (char*)&sqlite3WhereTrace, TCL_LINK_INT);
drh73be5012007-08-08 12:11:21 +00005214 Tcl_LinkVar(interp, "sqlite_os_trace",
mlcreech3a00f902008-03-04 17:45:01 +00005215 (char*)&sqlite3OSTrace, TCL_LINK_INT);
dan38e1a272010-06-28 11:23:09 +00005216#ifndef SQLITE_OMIT_WAL
drhc74c3332010-05-31 12:15:19 +00005217 Tcl_LinkVar(interp, "sqlite_wal_trace",
5218 (char*)&sqlite3WalTrace, TCL_LINK_INT);
drh8b3d9902005-08-19 00:14:42 +00005219#endif
dan38e1a272010-06-28 11:23:09 +00005220#endif
danielk1977cbe21be2005-06-07 07:58:48 +00005221#ifndef SQLITE_OMIT_DISKIO
drhaf6df112005-06-07 02:12:30 +00005222 Tcl_LinkVar(interp, "sqlite_opentemp_count",
5223 (char*)&sqlite3_opentemp_count, TCL_LINK_INT);
danielk1977cbe21be2005-06-07 07:58:48 +00005224#endif
drh7c972de2003-09-06 22:18:07 +00005225 Tcl_LinkVar(interp, "sqlite_static_bind_value",
5226 (char*)&sqlite_static_bind_value, TCL_LINK_STRING);
drhf0313812006-09-04 15:53:53 +00005227 Tcl_LinkVar(interp, "sqlite_static_bind_nbyte",
5228 (char*)&sqlite_static_bind_nbyte, TCL_LINK_INT);
drhab3f9fe2004-08-14 17:10:10 +00005229 Tcl_LinkVar(interp, "sqlite_temp_directory",
drheffd02b2004-08-29 23:42:13 +00005230 (char*)&sqlite3_temp_directory, TCL_LINK_STRING);
drh1398ad32005-01-19 23:24:50 +00005231 Tcl_LinkVar(interp, "bitmask_size",
5232 (char*)&bitmask_size, TCL_LINK_INT|TCL_LINK_READ_ONLY);
drhb851b2c2005-03-10 14:11:12 +00005233 Tcl_LinkVar(interp, "sqlite_sync_count",
5234 (char*)&sqlite3_sync_count, TCL_LINK_INT);
5235 Tcl_LinkVar(interp, "sqlite_fullsync_count",
5236 (char*)&sqlite3_fullsync_count, TCL_LINK_INT);
drhd1fa7bc2009-01-10 13:24:50 +00005237#if defined(SQLITE_ENABLE_FTS3) && defined(SQLITE_TEST)
danielk197733e89032008-12-17 15:18:17 +00005238 Tcl_LinkVar(interp, "sqlite_fts3_enable_parentheses",
5239 (char*)&sqlite3_fts3_enable_parentheses, TCL_LINK_INT);
5240#endif
drhd1bf3512001-04-07 15:24:33 +00005241 return TCL_OK;
5242}