blob: f8257e0c152d46f05f1c73afa324878e545547e5 [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){
shaneh2ceced12010-07-07 16:51:36 +00001904#if defined(_MSC_VER)
1905 /* We do this, otherwise the test will halt with a popup message
1906 * that we have to click away before the test will continue.
1907 */
1908 _set_abort_behavior( 0, _CALL_REPORTFAULT );
1909#endif
drh28b4e482002-03-11 02:06:13 +00001910 assert( interp==0 ); /* This will always fail */
dan89ccf442010-07-01 15:09:47 +00001911 abort();
drh28b4e482002-03-11 02:06:13 +00001912 return TCL_OK;
1913}
1914
1915/*
drh6cbe1f12002-07-01 00:31:36 +00001916** The following routine is a user-defined SQL function whose purpose
1917** is to test the sqlite_set_result() API.
1918*/
danielk19770ae8b832004-05-25 12:05:56 +00001919static void testFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
drh6cbe1f12002-07-01 00:31:36 +00001920 while( argc>=2 ){
drh03d847e2005-12-09 20:21:58 +00001921 const char *zArg0 = (char*)sqlite3_value_text(argv[0]);
danielk19776d88bad2004-05-27 14:23:36 +00001922 if( zArg0 ){
1923 if( 0==sqlite3StrICmp(zArg0, "int") ){
1924 sqlite3_result_int(context, sqlite3_value_int(argv[1]));
1925 }else if( sqlite3StrICmp(zArg0,"int64")==0 ){
1926 sqlite3_result_int64(context, sqlite3_value_int64(argv[1]));
1927 }else if( sqlite3StrICmp(zArg0,"string")==0 ){
drh03d847e2005-12-09 20:21:58 +00001928 sqlite3_result_text(context, (char*)sqlite3_value_text(argv[1]), -1,
danielk1977d8123362004-06-12 09:25:12 +00001929 SQLITE_TRANSIENT);
danielk19776d88bad2004-05-27 14:23:36 +00001930 }else if( sqlite3StrICmp(zArg0,"double")==0 ){
1931 sqlite3_result_double(context, sqlite3_value_double(argv[1]));
1932 }else if( sqlite3StrICmp(zArg0,"null")==0 ){
1933 sqlite3_result_null(context);
1934 }else if( sqlite3StrICmp(zArg0,"value")==0 ){
1935 sqlite3_result_value(context, argv[sqlite3_value_int(argv[1])]);
1936 }else{
1937 goto error_out;
1938 }
drh6cbe1f12002-07-01 00:31:36 +00001939 }else{
danielk19776d88bad2004-05-27 14:23:36 +00001940 goto error_out;
drh6cbe1f12002-07-01 00:31:36 +00001941 }
1942 argc -= 2;
1943 argv += 2;
1944 }
danielk19776d88bad2004-05-27 14:23:36 +00001945 return;
1946
1947error_out:
1948 sqlite3_result_error(context,"first argument should be one of: "
1949 "int int64 string double null value", -1);
drh6cbe1f12002-07-01 00:31:36 +00001950}
1951
1952/*
1953** Usage: sqlite_register_test_function DB NAME
1954**
1955** Register the test SQL function on the database DB under the name NAME.
1956*/
drhc2eef3b2002-08-31 18:53:06 +00001957static int test_register_func(
drh6cbe1f12002-07-01 00:31:36 +00001958 void *NotUsed,
1959 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1960 int argc, /* Number of arguments */
1961 char **argv /* Text of each argument */
1962){
drh9bb575f2004-09-06 17:24:11 +00001963 sqlite3 *db;
drh6cbe1f12002-07-01 00:31:36 +00001964 int rc;
1965 if( argc!=3 ){
1966 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1967 " DB FUNCTION-NAME", 0);
1968 return TCL_ERROR;
1969 }
drhb86ccfb2003-01-28 23:13:10 +00001970 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
danielk1977f9d64d22004-06-19 08:18:07 +00001971 rc = sqlite3_create_function(db, argv[2], -1, SQLITE_UTF8, 0,
danielk1977d8123362004-06-12 09:25:12 +00001972 testFunc, 0, 0);
drh6cbe1f12002-07-01 00:31:36 +00001973 if( rc!=0 ){
danielk1977f20b21c2004-05-31 23:56:42 +00001974 Tcl_AppendResult(interp, sqlite3ErrStr(rc), 0);
drh6cbe1f12002-07-01 00:31:36 +00001975 return TCL_ERROR;
1976 }
drhc60d0442004-09-30 13:43:13 +00001977 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
drh6cbe1f12002-07-01 00:31:36 +00001978 return TCL_OK;
1979}
1980
1981/*
danielk1977106bb232004-05-21 10:08:53 +00001982** Usage: sqlite3_finalize STMT
drhb86ccfb2003-01-28 23:13:10 +00001983**
danielk1977106bb232004-05-21 10:08:53 +00001984** Finalize a statement handle.
drhb86ccfb2003-01-28 23:13:10 +00001985*/
1986static int test_finalize(
danielk1977106bb232004-05-21 10:08:53 +00001987 void * clientData,
1988 Tcl_Interp *interp,
1989 int objc,
1990 Tcl_Obj *CONST objv[]
drhb86ccfb2003-01-28 23:13:10 +00001991){
danielk1977106bb232004-05-21 10:08:53 +00001992 sqlite3_stmt *pStmt;
drhb86ccfb2003-01-28 23:13:10 +00001993 int rc;
drhdddb2f22007-01-03 23:37:28 +00001994 sqlite3 *db = 0;
danielk1977106bb232004-05-21 10:08:53 +00001995
1996 if( objc!=2 ){
1997 Tcl_AppendResult(interp, "wrong # args: should be \"",
1998 Tcl_GetStringFromObj(objv[0], 0), " <STMT>", 0);
drhb86ccfb2003-01-28 23:13:10 +00001999 return TCL_ERROR;
2000 }
danielk1977106bb232004-05-21 10:08:53 +00002001
2002 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2003
danielk19774397de52005-01-12 12:44:03 +00002004 if( pStmt ){
2005 db = StmtToDb(pStmt);
2006 }
danielk1977fc57d7b2004-05-26 02:04:57 +00002007 rc = sqlite3_finalize(pStmt);
drh4f0c5872007-03-26 22:05:01 +00002008 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
danielk19774397de52005-01-12 12:44:03 +00002009 if( db && sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
danielk1977106bb232004-05-21 10:08:53 +00002010 return TCL_OK;
2011}
2012
2013/*
drhd1d38482008-10-07 23:46:38 +00002014** Usage: sqlite3_stmt_status STMT CODE RESETFLAG
2015**
2016** Get the value of a status counter from a statement.
2017*/
2018static int test_stmt_status(
2019 void * clientData,
2020 Tcl_Interp *interp,
2021 int objc,
2022 Tcl_Obj *CONST objv[]
2023){
2024 int iValue;
2025 int i, op, resetFlag;
2026 const char *zOpName;
2027 sqlite3_stmt *pStmt;
2028
2029 static const struct {
2030 const char *zName;
2031 int op;
2032 } aOp[] = {
2033 { "SQLITE_STMTSTATUS_FULLSCAN_STEP", SQLITE_STMTSTATUS_FULLSCAN_STEP },
2034 { "SQLITE_STMTSTATUS_SORT", SQLITE_STMTSTATUS_SORT },
drha21a64d2010-04-06 22:33:55 +00002035 { "SQLITE_STMTSTATUS_AUTOINDEX", SQLITE_STMTSTATUS_AUTOINDEX },
drhd1d38482008-10-07 23:46:38 +00002036 };
2037 if( objc!=4 ){
2038 Tcl_WrongNumArgs(interp, 1, objv, "STMT PARAMETER RESETFLAG");
2039 return TCL_ERROR;
2040 }
2041 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2042 zOpName = Tcl_GetString(objv[2]);
2043 for(i=0; i<ArraySize(aOp); i++){
2044 if( strcmp(aOp[i].zName, zOpName)==0 ){
2045 op = aOp[i].op;
2046 break;
2047 }
2048 }
2049 if( i>=ArraySize(aOp) ){
2050 if( Tcl_GetIntFromObj(interp, objv[2], &op) ) return TCL_ERROR;
2051 }
2052 if( Tcl_GetBooleanFromObj(interp, objv[3], &resetFlag) ) return TCL_ERROR;
2053 iValue = sqlite3_stmt_status(pStmt, op, resetFlag);
2054 Tcl_SetObjResult(interp, Tcl_NewIntObj(iValue));
2055 return TCL_OK;
2056}
2057
2058/*
drhbb5a9c32008-06-19 02:52:25 +00002059** Usage: sqlite3_next_stmt DB STMT
2060**
2061** Return the next statment in sequence after STMT.
2062*/
2063static int test_next_stmt(
2064 void * clientData,
2065 Tcl_Interp *interp,
2066 int objc,
2067 Tcl_Obj *CONST objv[]
2068){
2069 sqlite3_stmt *pStmt;
2070 sqlite3 *db = 0;
2071 char zBuf[50];
2072
2073 if( objc!=3 ){
2074 Tcl_AppendResult(interp, "wrong # args: should be \"",
2075 Tcl_GetStringFromObj(objv[0], 0), " DB STMT", 0);
2076 return TCL_ERROR;
2077 }
2078
2079 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
2080 if( getStmtPointer(interp, Tcl_GetString(objv[2]), &pStmt) ) return TCL_ERROR;
2081 pStmt = sqlite3_next_stmt(db, pStmt);
2082 if( pStmt ){
2083 if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR;
2084 Tcl_AppendResult(interp, zBuf, 0);
2085 }
2086 return TCL_OK;
2087}
2088
2089
2090/*
danielk1977106bb232004-05-21 10:08:53 +00002091** Usage: sqlite3_reset STMT
2092**
danielk1977261919c2005-12-06 12:52:59 +00002093** Reset a statement handle.
danielk1977106bb232004-05-21 10:08:53 +00002094*/
2095static int test_reset(
2096 void * clientData,
2097 Tcl_Interp *interp,
2098 int objc,
2099 Tcl_Obj *CONST objv[]
2100){
2101 sqlite3_stmt *pStmt;
2102 int rc;
2103
2104 if( objc!=2 ){
2105 Tcl_AppendResult(interp, "wrong # args: should be \"",
2106 Tcl_GetStringFromObj(objv[0], 0), " <STMT>", 0);
2107 return TCL_ERROR;
2108 }
2109
2110 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2111
danielk1977fc57d7b2004-05-26 02:04:57 +00002112 rc = sqlite3_reset(pStmt);
danielk1977261919c2005-12-06 12:52:59 +00002113 if( pStmt && sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ){
2114 return TCL_ERROR;
2115 }
drh4f0c5872007-03-26 22:05:01 +00002116 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
danielk1977261919c2005-12-06 12:52:59 +00002117/*
danielk1977106bb232004-05-21 10:08:53 +00002118 if( rc ){
drhb86ccfb2003-01-28 23:13:10 +00002119 return TCL_ERROR;
2120 }
danielk1977261919c2005-12-06 12:52:59 +00002121*/
drhb86ccfb2003-01-28 23:13:10 +00002122 return TCL_OK;
2123}
2124
drh5a387052003-01-11 14:19:51 +00002125/*
drhd89bd002005-01-22 03:03:54 +00002126** Usage: sqlite3_expired STMT
2127**
2128** Return TRUE if a recompilation of the statement is recommended.
2129*/
2130static int test_expired(
2131 void * clientData,
2132 Tcl_Interp *interp,
2133 int objc,
2134 Tcl_Obj *CONST objv[]
2135){
shaneeec556d2008-10-12 00:27:53 +00002136#ifndef SQLITE_OMIT_DEPRECATED
drhd89bd002005-01-22 03:03:54 +00002137 sqlite3_stmt *pStmt;
2138 if( objc!=2 ){
2139 Tcl_AppendResult(interp, "wrong # args: should be \"",
2140 Tcl_GetStringFromObj(objv[0], 0), " <STMT>", 0);
2141 return TCL_ERROR;
2142 }
2143 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2144 Tcl_SetObjResult(interp, Tcl_NewBooleanObj(sqlite3_expired(pStmt)));
shaneeec556d2008-10-12 00:27:53 +00002145#endif
drhd89bd002005-01-22 03:03:54 +00002146 return TCL_OK;
2147}
2148
2149/*
drhf8db1bc2005-04-22 02:38:37 +00002150** Usage: sqlite3_transfer_bindings FROMSTMT TOSTMT
2151**
2152** Transfer all bindings from FROMSTMT over to TOSTMT
2153*/
2154static int test_transfer_bind(
2155 void * clientData,
2156 Tcl_Interp *interp,
2157 int objc,
2158 Tcl_Obj *CONST objv[]
2159){
shaneeec556d2008-10-12 00:27:53 +00002160#ifndef SQLITE_OMIT_DEPRECATED
drhf8db1bc2005-04-22 02:38:37 +00002161 sqlite3_stmt *pStmt1, *pStmt2;
2162 if( objc!=3 ){
2163 Tcl_AppendResult(interp, "wrong # args: should be \"",
2164 Tcl_GetStringFromObj(objv[0], 0), " FROM-STMT TO-STMT", 0);
2165 return TCL_ERROR;
2166 }
2167 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt1)) return TCL_ERROR;
2168 if( getStmtPointer(interp, Tcl_GetString(objv[2]), &pStmt2)) return TCL_ERROR;
2169 Tcl_SetObjResult(interp,
2170 Tcl_NewIntObj(sqlite3_transfer_bindings(pStmt1,pStmt2)));
shaneeec556d2008-10-12 00:27:53 +00002171#endif
drhf8db1bc2005-04-22 02:38:37 +00002172 return TCL_OK;
2173}
2174
2175/*
danielk1977fbcd5852004-06-15 02:44:18 +00002176** Usage: sqlite3_changes DB
drh50457892003-09-06 01:10:47 +00002177**
danielk1977fbcd5852004-06-15 02:44:18 +00002178** Return the number of changes made to the database by the last SQL
2179** execution.
drh50457892003-09-06 01:10:47 +00002180*/
danielk1977fbcd5852004-06-15 02:44:18 +00002181static int test_changes(
2182 void * clientData,
2183 Tcl_Interp *interp,
2184 int objc,
2185 Tcl_Obj *CONST objv[]
2186){
2187 sqlite3 *db;
2188 if( objc!=2 ){
2189 Tcl_AppendResult(interp, "wrong # args: should be \"",
2190 Tcl_GetString(objv[0]), " DB", 0);
2191 return TCL_ERROR;
2192 }
2193 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
2194 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_changes(db)));
2195 return TCL_OK;
2196}
drh50457892003-09-06 01:10:47 +00002197
2198/*
drh7c972de2003-09-06 22:18:07 +00002199** This is the "static_bind_value" that variables are bound to when
danielk19776f8a5032004-05-10 10:34:51 +00002200** the FLAG option of sqlite3_bind is "static"
drh50457892003-09-06 01:10:47 +00002201*/
drh7c972de2003-09-06 22:18:07 +00002202static char *sqlite_static_bind_value = 0;
drhf0313812006-09-04 15:53:53 +00002203static int sqlite_static_bind_nbyte = 0;
drh7c972de2003-09-06 22:18:07 +00002204
2205/*
danielk19776f8a5032004-05-10 10:34:51 +00002206** Usage: sqlite3_bind VM IDX VALUE FLAGS
drh7c972de2003-09-06 22:18:07 +00002207**
2208** Sets the value of the IDX-th occurance of "?" in the original SQL
2209** string. VALUE is the new value. If FLAGS=="null" then VALUE is
2210** ignored and the value is set to NULL. If FLAGS=="static" then
2211** the value is set to the value of a static variable named
2212** "sqlite_static_bind_value". If FLAGS=="normal" then a copy
drhbf8aa2a2005-12-02 02:44:05 +00002213** of the VALUE is made. If FLAGS=="blob10" then a VALUE is ignored
2214** an a 10-byte blob "abc\000xyz\000pq" is inserted.
drh7c972de2003-09-06 22:18:07 +00002215*/
2216static int test_bind(
drh50457892003-09-06 01:10:47 +00002217 void *NotUsed,
2218 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
2219 int argc, /* Number of arguments */
2220 char **argv /* Text of each argument */
2221){
danielk1977fc57d7b2004-05-26 02:04:57 +00002222 sqlite3_stmt *pStmt;
drh50457892003-09-06 01:10:47 +00002223 int rc;
drh7c972de2003-09-06 22:18:07 +00002224 int idx;
2225 if( argc!=5 ){
drh50457892003-09-06 01:10:47 +00002226 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
drh7c972de2003-09-06 22:18:07 +00002227 " VM IDX VALUE (null|static|normal)\"", 0);
drh50457892003-09-06 01:10:47 +00002228 return TCL_ERROR;
2229 }
danielk1977fc57d7b2004-05-26 02:04:57 +00002230 if( getStmtPointer(interp, argv[1], &pStmt) ) return TCL_ERROR;
drh7c972de2003-09-06 22:18:07 +00002231 if( Tcl_GetInt(interp, argv[2], &idx) ) return TCL_ERROR;
2232 if( strcmp(argv[4],"null")==0 ){
danielk1977fc57d7b2004-05-26 02:04:57 +00002233 rc = sqlite3_bind_null(pStmt, idx);
drh7c972de2003-09-06 22:18:07 +00002234 }else if( strcmp(argv[4],"static")==0 ){
danielk1977fc57d7b2004-05-26 02:04:57 +00002235 rc = sqlite3_bind_text(pStmt, idx, sqlite_static_bind_value, -1, 0);
drhf0313812006-09-04 15:53:53 +00002236 }else if( strcmp(argv[4],"static-nbytes")==0 ){
2237 rc = sqlite3_bind_text(pStmt, idx, sqlite_static_bind_value,
2238 sqlite_static_bind_nbyte, 0);
drh7c972de2003-09-06 22:18:07 +00002239 }else if( strcmp(argv[4],"normal")==0 ){
danielk1977d8123362004-06-12 09:25:12 +00002240 rc = sqlite3_bind_text(pStmt, idx, argv[3], -1, SQLITE_TRANSIENT);
drhbf8aa2a2005-12-02 02:44:05 +00002241 }else if( strcmp(argv[4],"blob10")==0 ){
2242 rc = sqlite3_bind_text(pStmt, idx, "abc\000xyz\000pq", 10, SQLITE_STATIC);
drh7c972de2003-09-06 22:18:07 +00002243 }else{
2244 Tcl_AppendResult(interp, "4th argument should be "
2245 "\"null\" or \"static\" or \"normal\"", 0);
2246 return TCL_ERROR;
2247 }
drhc60d0442004-09-30 13:43:13 +00002248 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
drh50457892003-09-06 01:10:47 +00002249 if( rc ){
2250 char zBuf[50];
2251 sprintf(zBuf, "(%d) ", rc);
danielk1977f20b21c2004-05-31 23:56:42 +00002252 Tcl_AppendResult(interp, zBuf, sqlite3ErrStr(rc), 0);
drh50457892003-09-06 01:10:47 +00002253 return TCL_ERROR;
2254 }
2255 return TCL_OK;
2256}
2257
drh5436dc22004-11-14 04:04:17 +00002258#ifndef SQLITE_OMIT_UTF16
danielk19774e6af132004-06-10 14:01:08 +00002259/*
2260** Usage: add_test_collate <db ptr> <utf8> <utf16le> <utf16be>
2261**
2262** This function is used to test that SQLite selects the correct collation
2263** sequence callback when multiple versions (for different text encodings)
2264** are available.
2265**
2266** Calling this routine registers the collation sequence "test_collate"
2267** with database handle <db>. The second argument must be a list of three
2268** boolean values. If the first is true, then a version of test_collate is
2269** registered for UTF-8, if the second is true, a version is registered for
2270** UTF-16le, if the third is true, a UTF-16be version is available.
2271** Previous versions of test_collate are deleted.
2272**
2273** The collation sequence test_collate is implemented by calling the
2274** following TCL script:
2275**
2276** "test_collate <enc> <lhs> <rhs>"
2277**
2278** The <lhs> and <rhs> are the two values being compared, encoded in UTF-8.
2279** The <enc> parameter is the encoding of the collation function that
2280** SQLite selected to call. The TCL test script implements the
2281** "test_collate" proc.
2282**
2283** Note that this will only work with one intepreter at a time, as the
2284** interp pointer to use when evaluating the TCL script is stored in
2285** pTestCollateInterp.
2286*/
2287static Tcl_Interp* pTestCollateInterp;
2288static int test_collate_func(
2289 void *pCtx,
2290 int nA, const void *zA,
2291 int nB, const void *zB
2292){
2293 Tcl_Interp *i = pTestCollateInterp;
2294 int encin = (int)pCtx;
2295 int res;
drh4db38a72005-09-01 12:16:28 +00002296 int n;
danielk19774e6af132004-06-10 14:01:08 +00002297
2298 sqlite3_value *pVal;
2299 Tcl_Obj *pX;
2300
2301 pX = Tcl_NewStringObj("test_collate", -1);
2302 Tcl_IncrRefCount(pX);
2303
2304 switch( encin ){
2305 case SQLITE_UTF8:
2306 Tcl_ListObjAppendElement(i,pX,Tcl_NewStringObj("UTF-8",-1));
2307 break;
2308 case SQLITE_UTF16LE:
2309 Tcl_ListObjAppendElement(i,pX,Tcl_NewStringObj("UTF-16LE",-1));
2310 break;
2311 case SQLITE_UTF16BE:
2312 Tcl_ListObjAppendElement(i,pX,Tcl_NewStringObj("UTF-16BE",-1));
2313 break;
2314 default:
2315 assert(0);
2316 }
2317
dan02fa4692009-08-17 17:06:58 +00002318 sqlite3BeginBenignMalloc();
danielk19771e536952007-08-16 10:09:01 +00002319 pVal = sqlite3ValueNew(0);
dan02fa4692009-08-17 17:06:58 +00002320 if( pVal ){
2321 sqlite3ValueSetStr(pVal, nA, zA, encin, SQLITE_STATIC);
2322 n = sqlite3_value_bytes(pVal);
2323 Tcl_ListObjAppendElement(i,pX,
2324 Tcl_NewStringObj((char*)sqlite3_value_text(pVal),n));
2325 sqlite3ValueSetStr(pVal, nB, zB, encin, SQLITE_STATIC);
2326 n = sqlite3_value_bytes(pVal);
2327 Tcl_ListObjAppendElement(i,pX,
2328 Tcl_NewStringObj((char*)sqlite3_value_text(pVal),n));
2329 sqlite3ValueFree(pVal);
2330 }
2331 sqlite3EndBenignMalloc();
danielk19774e6af132004-06-10 14:01:08 +00002332
2333 Tcl_EvalObjEx(i, pX, 0);
2334 Tcl_DecrRefCount(pX);
2335 Tcl_GetIntFromObj(i, Tcl_GetObjResult(i), &res);
2336 return res;
2337}
2338static int test_collate(
2339 void * clientData,
2340 Tcl_Interp *interp,
2341 int objc,
2342 Tcl_Obj *CONST objv[]
2343){
2344 sqlite3 *db;
2345 int val;
danielk1977312d6b32004-06-29 13:18:23 +00002346 sqlite3_value *pVal;
drhc60d0442004-09-30 13:43:13 +00002347 int rc;
danielk19774e6af132004-06-10 14:01:08 +00002348
2349 if( objc!=5 ) goto bad_args;
2350 pTestCollateInterp = interp;
2351 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
2352
2353 if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[2], &val) ) return TCL_ERROR;
drhc60d0442004-09-30 13:43:13 +00002354 rc = sqlite3_create_collation(db, "test_collate", SQLITE_UTF8,
2355 (void *)SQLITE_UTF8, val?test_collate_func:0);
2356 if( rc==SQLITE_OK ){
drheee4c8c2008-02-18 22:24:57 +00002357 const void *zUtf16;
drhc60d0442004-09-30 13:43:13 +00002358 if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[3], &val) ) return TCL_ERROR;
2359 rc = sqlite3_create_collation(db, "test_collate", SQLITE_UTF16LE,
2360 (void *)SQLITE_UTF16LE, val?test_collate_func:0);
2361 if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[4], &val) ) return TCL_ERROR;
danielk1977312d6b32004-06-29 13:18:23 +00002362
drh86f8c192007-08-22 00:39:19 +00002363#if 0
danielk19779a30cf62006-01-18 04:26:07 +00002364 if( sqlite3_iMallocFail>0 ){
2365 sqlite3_iMallocFail++;
2366 }
2367#endif
drhf3a65f72007-08-22 20:18:21 +00002368 sqlite3_mutex_enter(db->mutex);
2369 pVal = sqlite3ValueNew(db);
drhb21c8cd2007-08-21 19:33:56 +00002370 sqlite3ValueSetStr(pVal, -1, "test_collate", SQLITE_UTF8, SQLITE_STATIC);
danielk1977a7a8e142008-02-13 18:25:27 +00002371 zUtf16 = sqlite3ValueText(pVal, SQLITE_UTF16NATIVE);
drhf3a65f72007-08-22 20:18:21 +00002372 if( db->mallocFailed ){
2373 rc = SQLITE_NOMEM;
2374 }else{
danielk1977a7a8e142008-02-13 18:25:27 +00002375 rc = sqlite3_create_collation16(db, zUtf16, SQLITE_UTF16BE,
danielk19779a30cf62006-01-18 04:26:07 +00002376 (void *)SQLITE_UTF16BE, val?test_collate_func:0);
drhf3a65f72007-08-22 20:18:21 +00002377 }
drhc60d0442004-09-30 13:43:13 +00002378 sqlite3ValueFree(pVal);
drhf3a65f72007-08-22 20:18:21 +00002379 sqlite3_mutex_leave(db->mutex);
drhc60d0442004-09-30 13:43:13 +00002380 }
2381 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
danielk19779a30cf62006-01-18 04:26:07 +00002382
2383 if( rc!=SQLITE_OK ){
2384 Tcl_AppendResult(interp, sqlite3TestErrorName(rc), 0);
2385 return TCL_ERROR;
2386 }
danielk19774e6af132004-06-10 14:01:08 +00002387 return TCL_OK;
2388
2389bad_args:
2390 Tcl_AppendResult(interp, "wrong # args: should be \"",
2391 Tcl_GetStringFromObj(objv[0], 0), " <DB> <utf8> <utf16le> <utf16be>", 0);
2392 return TCL_ERROR;
2393}
2394
drh268803a2005-12-14 20:11:30 +00002395/*
2396** When the collation needed callback is invoked, record the name of
2397** the requested collating function here. The recorded name is linked
2398** to a TCL variable and used to make sure that the requested collation
2399** name is correct.
2400*/
2401static char zNeededCollation[200];
2402static char *pzNeededCollation = zNeededCollation;
2403
2404
2405/*
2406** Called when a collating sequence is needed. Registered using
2407** sqlite3_collation_needed16().
2408*/
danielk1977312d6b32004-06-29 13:18:23 +00002409static void test_collate_needed_cb(
2410 void *pCtx,
2411 sqlite3 *db,
2412 int eTextRep,
drh268803a2005-12-14 20:11:30 +00002413 const void *pName
danielk1977312d6b32004-06-29 13:18:23 +00002414){
danielk197714db2662006-01-09 16:12:04 +00002415 int enc = ENC(db);
drh268803a2005-12-14 20:11:30 +00002416 int i;
2417 char *z;
2418 for(z = (char*)pName, i=0; *z || z[1]; z++){
2419 if( *z ) zNeededCollation[i++] = *z;
2420 }
2421 zNeededCollation[i] = 0;
danielk1977312d6b32004-06-29 13:18:23 +00002422 sqlite3_create_collation(
danielk197714db2662006-01-09 16:12:04 +00002423 db, "test_collate", ENC(db), (void *)enc, test_collate_func);
danielk1977312d6b32004-06-29 13:18:23 +00002424}
2425
2426/*
2427** Usage: add_test_collate_needed DB
2428*/
2429static int test_collate_needed(
2430 void * clientData,
2431 Tcl_Interp *interp,
2432 int objc,
2433 Tcl_Obj *CONST objv[]
2434){
2435 sqlite3 *db;
drhc60d0442004-09-30 13:43:13 +00002436 int rc;
danielk1977312d6b32004-06-29 13:18:23 +00002437
2438 if( objc!=2 ) goto bad_args;
2439 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
drhc60d0442004-09-30 13:43:13 +00002440 rc = sqlite3_collation_needed16(db, 0, test_collate_needed_cb);
drh268803a2005-12-14 20:11:30 +00002441 zNeededCollation[0] = 0;
drhc60d0442004-09-30 13:43:13 +00002442 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
danielk1977312d6b32004-06-29 13:18:23 +00002443 return TCL_OK;
2444
2445bad_args:
2446 Tcl_WrongNumArgs(interp, 1, objv, "DB");
2447 return TCL_ERROR;
2448}
drh7d9bd4e2006-02-16 18:16:36 +00002449
2450/*
2451** tclcmd: add_alignment_test_collations DB
2452**
2453** Add two new collating sequences to the database DB
2454**
2455** utf16_aligned
2456** utf16_unaligned
2457**
2458** Both collating sequences use the same sort order as BINARY.
2459** The only difference is that the utf16_aligned collating
2460** sequence is declared with the SQLITE_UTF16_ALIGNED flag.
2461** Both collating functions increment the unaligned utf16 counter
2462** whenever they see a string that begins on an odd byte boundary.
2463*/
2464static int unaligned_string_counter = 0;
2465static int alignmentCollFunc(
2466 void *NotUsed,
2467 int nKey1, const void *pKey1,
2468 int nKey2, const void *pKey2
2469){
2470 int rc, n;
2471 n = nKey1<nKey2 ? nKey1 : nKey2;
2472 if( nKey1>0 && 1==(1&(int)pKey1) ) unaligned_string_counter++;
2473 if( nKey2>0 && 1==(1&(int)pKey2) ) unaligned_string_counter++;
2474 rc = memcmp(pKey1, pKey2, n);
2475 if( rc==0 ){
2476 rc = nKey1 - nKey2;
2477 }
2478 return rc;
2479}
2480static int add_alignment_test_collations(
2481 void * clientData,
2482 Tcl_Interp *interp,
2483 int objc,
2484 Tcl_Obj *CONST objv[]
2485){
2486 sqlite3 *db;
2487 if( objc>=2 ){
2488 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
danielk1977ebb32932009-04-28 15:35:38 +00002489 sqlite3_create_collation(db, "utf16_unaligned", SQLITE_UTF16,
drh7d9bd4e2006-02-16 18:16:36 +00002490 0, alignmentCollFunc);
danielk1977ebb32932009-04-28 15:35:38 +00002491 sqlite3_create_collation(db, "utf16_aligned", SQLITE_UTF16_ALIGNED,
drh7d9bd4e2006-02-16 18:16:36 +00002492 0, alignmentCollFunc);
2493 }
2494 return SQLITE_OK;
2495}
2496#endif /* !defined(SQLITE_OMIT_UTF16) */
danielk1977312d6b32004-06-29 13:18:23 +00002497
danielk1977c8e9a2d2004-06-25 12:08:46 +00002498/*
2499** Usage: add_test_function <db ptr> <utf8> <utf16le> <utf16be>
2500**
2501** This function is used to test that SQLite selects the correct user
2502** function callback when multiple versions (for different text encodings)
2503** are available.
2504**
2505** Calling this routine registers up to three versions of the user function
2506** "test_function" with database handle <db>. If the second argument is
2507** true, then a version of test_function is registered for UTF-8, if the
2508** third is true, a version is registered for UTF-16le, if the fourth is
2509** true, a UTF-16be version is available. Previous versions of
2510** test_function are deleted.
2511**
2512** The user function is implemented by calling the following TCL script:
2513**
2514** "test_function <enc> <arg>"
2515**
2516** Where <enc> is one of UTF-8, UTF-16LE or UTF16BE, and <arg> is the
2517** single argument passed to the SQL function. The value returned by
2518** the TCL script is used as the return value of the SQL function. It
2519** is passed to SQLite using UTF-16BE for a UTF-8 test_function(), UTF-8
2520** for a UTF-16LE test_function(), and UTF-16LE for an implementation that
2521** prefers UTF-16BE.
2522*/
drh5436dc22004-11-14 04:04:17 +00002523#ifndef SQLITE_OMIT_UTF16
danielk1977c8e9a2d2004-06-25 12:08:46 +00002524static void test_function_utf8(
2525 sqlite3_context *pCtx,
2526 int nArg,
2527 sqlite3_value **argv
2528){
2529 Tcl_Interp *interp;
2530 Tcl_Obj *pX;
2531 sqlite3_value *pVal;
2532 interp = (Tcl_Interp *)sqlite3_user_data(pCtx);
2533 pX = Tcl_NewStringObj("test_function", -1);
2534 Tcl_IncrRefCount(pX);
2535 Tcl_ListObjAppendElement(interp, pX, Tcl_NewStringObj("UTF-8", -1));
2536 Tcl_ListObjAppendElement(interp, pX,
drh03d847e2005-12-09 20:21:58 +00002537 Tcl_NewStringObj((char*)sqlite3_value_text(argv[0]), -1));
danielk1977c8e9a2d2004-06-25 12:08:46 +00002538 Tcl_EvalObjEx(interp, pX, 0);
2539 Tcl_DecrRefCount(pX);
2540 sqlite3_result_text(pCtx, Tcl_GetStringResult(interp), -1, SQLITE_TRANSIENT);
danielk19771e536952007-08-16 10:09:01 +00002541 pVal = sqlite3ValueNew(0);
drhb21c8cd2007-08-21 19:33:56 +00002542 sqlite3ValueSetStr(pVal, -1, Tcl_GetStringResult(interp),
danielk1977c8e9a2d2004-06-25 12:08:46 +00002543 SQLITE_UTF8, SQLITE_STATIC);
2544 sqlite3_result_text16be(pCtx, sqlite3_value_text16be(pVal),
2545 -1, SQLITE_TRANSIENT);
2546 sqlite3ValueFree(pVal);
2547}
2548static void test_function_utf16le(
2549 sqlite3_context *pCtx,
2550 int nArg,
2551 sqlite3_value **argv
2552){
2553 Tcl_Interp *interp;
2554 Tcl_Obj *pX;
2555 sqlite3_value *pVal;
2556 interp = (Tcl_Interp *)sqlite3_user_data(pCtx);
2557 pX = Tcl_NewStringObj("test_function", -1);
2558 Tcl_IncrRefCount(pX);
2559 Tcl_ListObjAppendElement(interp, pX, Tcl_NewStringObj("UTF-16LE", -1));
2560 Tcl_ListObjAppendElement(interp, pX,
drh03d847e2005-12-09 20:21:58 +00002561 Tcl_NewStringObj((char*)sqlite3_value_text(argv[0]), -1));
danielk1977c8e9a2d2004-06-25 12:08:46 +00002562 Tcl_EvalObjEx(interp, pX, 0);
2563 Tcl_DecrRefCount(pX);
danielk19771e536952007-08-16 10:09:01 +00002564 pVal = sqlite3ValueNew(0);
drhb21c8cd2007-08-21 19:33:56 +00002565 sqlite3ValueSetStr(pVal, -1, Tcl_GetStringResult(interp),
danielk1977c8e9a2d2004-06-25 12:08:46 +00002566 SQLITE_UTF8, SQLITE_STATIC);
drh03d847e2005-12-09 20:21:58 +00002567 sqlite3_result_text(pCtx,(char*)sqlite3_value_text(pVal),-1,SQLITE_TRANSIENT);
danielk1977c8e9a2d2004-06-25 12:08:46 +00002568 sqlite3ValueFree(pVal);
2569}
2570static void test_function_utf16be(
2571 sqlite3_context *pCtx,
2572 int nArg,
2573 sqlite3_value **argv
2574){
2575 Tcl_Interp *interp;
2576 Tcl_Obj *pX;
2577 sqlite3_value *pVal;
2578 interp = (Tcl_Interp *)sqlite3_user_data(pCtx);
2579 pX = Tcl_NewStringObj("test_function", -1);
2580 Tcl_IncrRefCount(pX);
2581 Tcl_ListObjAppendElement(interp, pX, Tcl_NewStringObj("UTF-16BE", -1));
2582 Tcl_ListObjAppendElement(interp, pX,
drh03d847e2005-12-09 20:21:58 +00002583 Tcl_NewStringObj((char*)sqlite3_value_text(argv[0]), -1));
danielk1977c8e9a2d2004-06-25 12:08:46 +00002584 Tcl_EvalObjEx(interp, pX, 0);
2585 Tcl_DecrRefCount(pX);
danielk19771e536952007-08-16 10:09:01 +00002586 pVal = sqlite3ValueNew(0);
drhb21c8cd2007-08-21 19:33:56 +00002587 sqlite3ValueSetStr(pVal, -1, Tcl_GetStringResult(interp),
danielk1977c8e9a2d2004-06-25 12:08:46 +00002588 SQLITE_UTF8, SQLITE_STATIC);
drhde4fcfd2008-01-19 23:50:26 +00002589 sqlite3_result_text16(pCtx, sqlite3_value_text16le(pVal),
2590 -1, SQLITE_TRANSIENT);
2591 sqlite3_result_text16be(pCtx, sqlite3_value_text16le(pVal),
2592 -1, SQLITE_TRANSIENT);
danielk1977c8e9a2d2004-06-25 12:08:46 +00002593 sqlite3_result_text16le(pCtx, sqlite3_value_text16le(pVal),
2594 -1, SQLITE_TRANSIENT);
2595 sqlite3ValueFree(pVal);
2596}
drh5436dc22004-11-14 04:04:17 +00002597#endif /* SQLITE_OMIT_UTF16 */
danielk1977c8e9a2d2004-06-25 12:08:46 +00002598static int test_function(
2599 void * clientData,
2600 Tcl_Interp *interp,
2601 int objc,
2602 Tcl_Obj *CONST objv[]
2603){
drh5436dc22004-11-14 04:04:17 +00002604#ifndef SQLITE_OMIT_UTF16
danielk1977c8e9a2d2004-06-25 12:08:46 +00002605 sqlite3 *db;
2606 int val;
2607
2608 if( objc!=5 ) goto bad_args;
2609 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
2610
2611 if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[2], &val) ) return TCL_ERROR;
2612 if( val ){
2613 sqlite3_create_function(db, "test_function", 1, SQLITE_UTF8,
2614 interp, test_function_utf8, 0, 0);
2615 }
2616 if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[3], &val) ) return TCL_ERROR;
2617 if( val ){
2618 sqlite3_create_function(db, "test_function", 1, SQLITE_UTF16LE,
2619 interp, test_function_utf16le, 0, 0);
2620 }
2621 if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[4], &val) ) return TCL_ERROR;
2622 if( val ){
2623 sqlite3_create_function(db, "test_function", 1, SQLITE_UTF16BE,
2624 interp, test_function_utf16be, 0, 0);
2625 }
2626
2627 return TCL_OK;
2628bad_args:
2629 Tcl_AppendResult(interp, "wrong # args: should be \"",
2630 Tcl_GetStringFromObj(objv[0], 0), " <DB> <utf8> <utf16le> <utf16be>", 0);
drh5436dc22004-11-14 04:04:17 +00002631#endif /* SQLITE_OMIT_UTF16 */
danielk1977c8e9a2d2004-06-25 12:08:46 +00002632 return TCL_ERROR;
2633}
2634
danielk1977312d6b32004-06-29 13:18:23 +00002635/*
danba3cbf32010-06-30 04:29:03 +00002636** Usage: sqlite3_test_errstr <err code>
danielk1977312d6b32004-06-29 13:18:23 +00002637**
2638** Test that the english language string equivalents for sqlite error codes
2639** are sane. The parameter is an integer representing an sqlite error code.
2640** The result is a list of two elements, the string representation of the
2641** error code and the english language explanation.
2642*/
2643static int test_errstr(
2644 void * clientData,
2645 Tcl_Interp *interp,
2646 int objc,
2647 Tcl_Obj *CONST objv[]
2648){
2649 char *zCode;
2650 int i;
2651 if( objc!=1 ){
2652 Tcl_WrongNumArgs(interp, 1, objv, "<error code>");
2653 }
2654
2655 zCode = Tcl_GetString(objv[1]);
2656 for(i=0; i<200; i++){
drh4f0c5872007-03-26 22:05:01 +00002657 if( 0==strcmp(t1ErrorName(i), zCode) ) break;
danielk1977312d6b32004-06-29 13:18:23 +00002658 }
2659 Tcl_SetResult(interp, (char *)sqlite3ErrStr(i), 0);
2660 return TCL_OK;
2661}
2662
drh50457892003-09-06 01:10:47 +00002663/*
drh99ee3602003-02-16 19:13:36 +00002664** Usage: breakpoint
2665**
2666** This routine exists for one purpose - to provide a place to put a
2667** breakpoint with GDB that can be triggered using TCL code. The use
2668** for this is when a particular test fails on (say) the 1485th iteration.
2669** In the TCL test script, we can add code like this:
2670**
2671** if {$i==1485} breakpoint
2672**
2673** Then run testfixture in the debugger and wait for the breakpoint to
2674** fire. Then additional breakpoints can be set to trace down the bug.
2675*/
2676static int test_breakpoint(
2677 void *NotUsed,
2678 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
2679 int argc, /* Number of arguments */
2680 char **argv /* Text of each argument */
2681){
2682 return TCL_OK; /* Do nothing */
2683}
2684
drh241db312004-06-22 12:46:53 +00002685/*
drhb026e052007-05-02 01:34:31 +00002686** Usage: sqlite3_bind_zeroblob STMT IDX N
2687**
2688** Test the sqlite3_bind_zeroblob interface. STMT is a prepared statement.
2689** IDX is the index of a wildcard in the prepared statement. This command
2690** binds a N-byte zero-filled BLOB to the wildcard.
2691*/
2692static int test_bind_zeroblob(
2693 void * clientData,
2694 Tcl_Interp *interp,
2695 int objc,
2696 Tcl_Obj *CONST objv[]
2697){
2698 sqlite3_stmt *pStmt;
2699 int idx;
2700 int n;
2701 int rc;
2702
2703 if( objc!=4 ){
danielk197728c66302007-09-01 11:04:26 +00002704 Tcl_WrongNumArgs(interp, 1, objv, "STMT IDX N");
drhb026e052007-05-02 01:34:31 +00002705 return TCL_ERROR;
2706 }
2707
2708 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2709 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
2710 if( Tcl_GetIntFromObj(interp, objv[3], &n) ) return TCL_ERROR;
2711
2712 rc = sqlite3_bind_zeroblob(pStmt, idx, n);
2713 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
2714 if( rc!=SQLITE_OK ){
2715 return TCL_ERROR;
2716 }
2717
2718 return TCL_OK;
2719}
2720
2721/*
drh241db312004-06-22 12:46:53 +00002722** Usage: sqlite3_bind_int STMT N VALUE
2723**
2724** Test the sqlite3_bind_int interface. STMT is a prepared statement.
2725** N is the index of a wildcard in the prepared statement. This command
2726** binds a 32-bit integer VALUE to that wildcard.
2727*/
2728static int test_bind_int(
danielk197751e3d8e2004-05-20 01:12:34 +00002729 void * clientData,
2730 Tcl_Interp *interp,
2731 int objc,
2732 Tcl_Obj *CONST objv[]
2733){
2734 sqlite3_stmt *pStmt;
2735 int idx;
2736 int value;
2737 int rc;
2738
2739 if( objc!=4 ){
2740 Tcl_AppendResult(interp, "wrong # args: should be \"",
drh241db312004-06-22 12:46:53 +00002741 Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE", 0);
danielk197751e3d8e2004-05-20 01:12:34 +00002742 return TCL_ERROR;
2743 }
2744
2745 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2746 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
2747 if( Tcl_GetIntFromObj(interp, objv[3], &value) ) return TCL_ERROR;
2748
danielk1977c572ef72004-05-27 09:28:41 +00002749 rc = sqlite3_bind_int(pStmt, idx, value);
drhc60d0442004-09-30 13:43:13 +00002750 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
danielk197751e3d8e2004-05-20 01:12:34 +00002751 if( rc!=SQLITE_OK ){
2752 return TCL_ERROR;
2753 }
2754
2755 return TCL_OK;
2756}
2757
drh241db312004-06-22 12:46:53 +00002758
2759/*
2760** Usage: sqlite3_bind_int64 STMT N VALUE
2761**
2762** Test the sqlite3_bind_int64 interface. STMT is a prepared statement.
2763** N is the index of a wildcard in the prepared statement. This command
2764** binds a 64-bit integer VALUE to that wildcard.
2765*/
danielk197751e3d8e2004-05-20 01:12:34 +00002766static int test_bind_int64(
2767 void * clientData,
2768 Tcl_Interp *interp,
2769 int objc,
2770 Tcl_Obj *CONST objv[]
2771){
2772 sqlite3_stmt *pStmt;
2773 int idx;
2774 i64 value;
2775 int rc;
2776
2777 if( objc!=4 ){
2778 Tcl_AppendResult(interp, "wrong # args: should be \"",
drh241db312004-06-22 12:46:53 +00002779 Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE", 0);
danielk197751e3d8e2004-05-20 01:12:34 +00002780 return TCL_ERROR;
2781 }
2782
2783 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2784 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
2785 if( Tcl_GetWideIntFromObj(interp, objv[3], &value) ) return TCL_ERROR;
2786
2787 rc = sqlite3_bind_int64(pStmt, idx, value);
drhc60d0442004-09-30 13:43:13 +00002788 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
danielk197751e3d8e2004-05-20 01:12:34 +00002789 if( rc!=SQLITE_OK ){
2790 return TCL_ERROR;
2791 }
2792
2793 return TCL_OK;
2794}
2795
drh241db312004-06-22 12:46:53 +00002796
2797/*
2798** Usage: sqlite3_bind_double STMT N VALUE
2799**
2800** Test the sqlite3_bind_double interface. STMT is a prepared statement.
2801** N is the index of a wildcard in the prepared statement. This command
2802** binds a 64-bit integer VALUE to that wildcard.
2803*/
danielk197751e3d8e2004-05-20 01:12:34 +00002804static int test_bind_double(
2805 void * clientData,
2806 Tcl_Interp *interp,
2807 int objc,
2808 Tcl_Obj *CONST objv[]
2809){
2810 sqlite3_stmt *pStmt;
2811 int idx;
2812 double value;
2813 int rc;
drha06f17f2008-05-11 11:07:06 +00002814 const char *zVal;
2815 int i;
2816 static const struct {
2817 const char *zName; /* Name of the special floating point value */
2818 unsigned int iUpper; /* Upper 32 bits */
2819 unsigned int iLower; /* Lower 32 bits */
2820 } aSpecialFp[] = {
2821 { "NaN", 0x7fffffff, 0xffffffff },
2822 { "SNaN", 0x7ff7ffff, 0xffffffff },
2823 { "-NaN", 0xffffffff, 0xffffffff },
2824 { "-SNaN", 0xfff7ffff, 0xffffffff },
2825 { "+Inf", 0x7ff00000, 0x00000000 },
2826 { "-Inf", 0xfff00000, 0x00000000 },
2827 { "Epsilon", 0x00000000, 0x00000001 },
2828 { "-Epsilon", 0x80000000, 0x00000001 },
2829 { "NaN0", 0x7ff80000, 0x00000000 },
2830 { "-NaN0", 0xfff80000, 0x00000000 },
2831 };
danielk197751e3d8e2004-05-20 01:12:34 +00002832
2833 if( objc!=4 ){
2834 Tcl_AppendResult(interp, "wrong # args: should be \"",
drh241db312004-06-22 12:46:53 +00002835 Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE", 0);
danielk197751e3d8e2004-05-20 01:12:34 +00002836 return TCL_ERROR;
2837 }
2838
2839 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2840 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
danielk197751e3d8e2004-05-20 01:12:34 +00002841
drh394f07e2008-04-28 15:23:02 +00002842 /* Intercept the string "NaN" and generate a NaN value for it.
2843 ** All other strings are passed through to Tcl_GetDoubleFromObj().
2844 ** Tcl_GetDoubleFromObj() should understand "NaN" but some versions
2845 ** contain a bug.
2846 */
drha06f17f2008-05-11 11:07:06 +00002847 zVal = Tcl_GetString(objv[3]);
2848 for(i=0; i<sizeof(aSpecialFp)/sizeof(aSpecialFp[0]); i++){
2849 if( strcmp(aSpecialFp[i].zName, zVal)==0 ){
2850 sqlite3_uint64 x;
2851 x = aSpecialFp[i].iUpper;
2852 x <<= 32;
2853 x |= aSpecialFp[i].iLower;
drh0a667332008-05-11 17:22:01 +00002854 assert( sizeof(value)==8 );
2855 assert( sizeof(x)==8 );
2856 memcpy(&value, &x, 8);
drha06f17f2008-05-11 11:07:06 +00002857 break;
2858 }
2859 }
2860 if( i>=sizeof(aSpecialFp)/sizeof(aSpecialFp[0]) &&
2861 Tcl_GetDoubleFromObj(interp, objv[3], &value) ){
drh394f07e2008-04-28 15:23:02 +00002862 return TCL_ERROR;
2863 }
danielk197751e3d8e2004-05-20 01:12:34 +00002864 rc = sqlite3_bind_double(pStmt, idx, value);
drhc60d0442004-09-30 13:43:13 +00002865 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
danielk197751e3d8e2004-05-20 01:12:34 +00002866 if( rc!=SQLITE_OK ){
2867 return TCL_ERROR;
2868 }
2869
2870 return TCL_OK;
2871}
2872
drh241db312004-06-22 12:46:53 +00002873/*
2874** Usage: sqlite3_bind_null STMT N
2875**
2876** Test the sqlite3_bind_null interface. STMT is a prepared statement.
2877** N is the index of a wildcard in the prepared statement. This command
2878** binds a NULL to the wildcard.
2879*/
danielk197751e3d8e2004-05-20 01:12:34 +00002880static int test_bind_null(
2881 void * clientData,
2882 Tcl_Interp *interp,
2883 int objc,
2884 Tcl_Obj *CONST objv[]
2885){
2886 sqlite3_stmt *pStmt;
2887 int idx;
2888 int rc;
2889
2890 if( objc!=3 ){
2891 Tcl_AppendResult(interp, "wrong # args: should be \"",
drh241db312004-06-22 12:46:53 +00002892 Tcl_GetStringFromObj(objv[0], 0), " STMT N", 0);
danielk197751e3d8e2004-05-20 01:12:34 +00002893 return TCL_ERROR;
2894 }
2895
2896 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2897 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
2898
2899 rc = sqlite3_bind_null(pStmt, idx);
drhc60d0442004-09-30 13:43:13 +00002900 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
danielk197751e3d8e2004-05-20 01:12:34 +00002901 if( rc!=SQLITE_OK ){
2902 return TCL_ERROR;
2903 }
2904
2905 return TCL_OK;
2906}
2907
drh241db312004-06-22 12:46:53 +00002908/*
2909** Usage: sqlite3_bind_text STMT N STRING BYTES
2910**
2911** Test the sqlite3_bind_text interface. STMT is a prepared statement.
2912** N is the index of a wildcard in the prepared statement. This command
2913** binds a UTF-8 string STRING to the wildcard. The string is BYTES bytes
2914** long.
2915*/
danielk197751e3d8e2004-05-20 01:12:34 +00002916static int test_bind_text(
2917 void * clientData,
2918 Tcl_Interp *interp,
2919 int objc,
2920 Tcl_Obj *CONST objv[]
2921){
2922 sqlite3_stmt *pStmt;
2923 int idx;
2924 int bytes;
2925 char *value;
2926 int rc;
2927
2928 if( objc!=5 ){
2929 Tcl_AppendResult(interp, "wrong # args: should be \"",
drh241db312004-06-22 12:46:53 +00002930 Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE BYTES", 0);
danielk197751e3d8e2004-05-20 01:12:34 +00002931 return TCL_ERROR;
2932 }
2933
2934 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2935 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
drh10dfbbb2008-04-16 12:58:53 +00002936 value = (char*)Tcl_GetByteArrayFromObj(objv[3], &bytes);
danielk197751e3d8e2004-05-20 01:12:34 +00002937 if( Tcl_GetIntFromObj(interp, objv[4], &bytes) ) return TCL_ERROR;
2938
danielk1977d8123362004-06-12 09:25:12 +00002939 rc = sqlite3_bind_text(pStmt, idx, value, bytes, SQLITE_TRANSIENT);
drhc60d0442004-09-30 13:43:13 +00002940 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
danielk197751e3d8e2004-05-20 01:12:34 +00002941 if( rc!=SQLITE_OK ){
drh473d1792005-06-06 17:54:55 +00002942 Tcl_AppendResult(interp, sqlite3TestErrorName(rc), 0);
danielk197751e3d8e2004-05-20 01:12:34 +00002943 return TCL_ERROR;
2944 }
2945
2946 return TCL_OK;
2947}
2948
drh241db312004-06-22 12:46:53 +00002949/*
danielk1977161fb792006-01-24 10:58:21 +00002950** Usage: sqlite3_bind_text16 ?-static? STMT N STRING BYTES
drh241db312004-06-22 12:46:53 +00002951**
2952** Test the sqlite3_bind_text16 interface. STMT is a prepared statement.
2953** N is the index of a wildcard in the prepared statement. This command
2954** binds a UTF-16 string STRING to the wildcard. The string is BYTES bytes
2955** long.
2956*/
danielk197751e3d8e2004-05-20 01:12:34 +00002957static int test_bind_text16(
2958 void * clientData,
2959 Tcl_Interp *interp,
2960 int objc,
2961 Tcl_Obj *CONST objv[]
2962){
drh5436dc22004-11-14 04:04:17 +00002963#ifndef SQLITE_OMIT_UTF16
danielk197751e3d8e2004-05-20 01:12:34 +00002964 sqlite3_stmt *pStmt;
2965 int idx;
2966 int bytes;
2967 char *value;
2968 int rc;
2969
danielk1977161fb792006-01-24 10:58:21 +00002970 void (*xDel)() = (objc==6?SQLITE_STATIC:SQLITE_TRANSIENT);
2971 Tcl_Obj *oStmt = objv[objc-4];
2972 Tcl_Obj *oN = objv[objc-3];
2973 Tcl_Obj *oString = objv[objc-2];
2974 Tcl_Obj *oBytes = objv[objc-1];
2975
2976 if( objc!=5 && objc!=6){
danielk197751e3d8e2004-05-20 01:12:34 +00002977 Tcl_AppendResult(interp, "wrong # args: should be \"",
drh241db312004-06-22 12:46:53 +00002978 Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE BYTES", 0);
danielk197751e3d8e2004-05-20 01:12:34 +00002979 return TCL_ERROR;
2980 }
2981
danielk1977161fb792006-01-24 10:58:21 +00002982 if( getStmtPointer(interp, Tcl_GetString(oStmt), &pStmt) ) return TCL_ERROR;
2983 if( Tcl_GetIntFromObj(interp, oN, &idx) ) return TCL_ERROR;
2984 value = (char*)Tcl_GetByteArrayFromObj(oString, 0);
2985 if( Tcl_GetIntFromObj(interp, oBytes, &bytes) ) return TCL_ERROR;
danielk197751e3d8e2004-05-20 01:12:34 +00002986
danielk1977161fb792006-01-24 10:58:21 +00002987 rc = sqlite3_bind_text16(pStmt, idx, (void *)value, bytes, xDel);
drhc60d0442004-09-30 13:43:13 +00002988 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
danielk197751e3d8e2004-05-20 01:12:34 +00002989 if( rc!=SQLITE_OK ){
drhddff9ae2008-07-08 15:26:49 +00002990 Tcl_AppendResult(interp, sqlite3TestErrorName(rc), 0);
danielk197751e3d8e2004-05-20 01:12:34 +00002991 return TCL_ERROR;
2992 }
2993
drh5436dc22004-11-14 04:04:17 +00002994#endif /* SQLITE_OMIT_UTF16 */
danielk197751e3d8e2004-05-20 01:12:34 +00002995 return TCL_OK;
2996}
2997
drh241db312004-06-22 12:46:53 +00002998/*
danielk19775b159dc2007-05-17 16:34:43 +00002999** Usage: sqlite3_bind_blob ?-static? STMT N DATA BYTES
drh241db312004-06-22 12:46:53 +00003000**
3001** Test the sqlite3_bind_blob interface. STMT is a prepared statement.
3002** N is the index of a wildcard in the prepared statement. This command
3003** binds a BLOB to the wildcard. The BLOB is BYTES bytes in size.
3004*/
danielk197751e3d8e2004-05-20 01:12:34 +00003005static int test_bind_blob(
3006 void * clientData,
3007 Tcl_Interp *interp,
3008 int objc,
3009 Tcl_Obj *CONST objv[]
3010){
3011 sqlite3_stmt *pStmt;
3012 int idx;
3013 int bytes;
3014 char *value;
3015 int rc;
danielk19775b159dc2007-05-17 16:34:43 +00003016 sqlite3_destructor_type xDestructor = SQLITE_TRANSIENT;
danielk197751e3d8e2004-05-20 01:12:34 +00003017
danielk19775b159dc2007-05-17 16:34:43 +00003018 if( objc!=5 && objc!=6 ){
danielk197751e3d8e2004-05-20 01:12:34 +00003019 Tcl_AppendResult(interp, "wrong # args: should be \"",
drh241db312004-06-22 12:46:53 +00003020 Tcl_GetStringFromObj(objv[0], 0), " STMT N DATA BYTES", 0);
danielk197751e3d8e2004-05-20 01:12:34 +00003021 return TCL_ERROR;
3022 }
3023
danielk19775b159dc2007-05-17 16:34:43 +00003024 if( objc==6 ){
3025 xDestructor = SQLITE_STATIC;
3026 objv++;
3027 }
3028
danielk197751e3d8e2004-05-20 01:12:34 +00003029 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3030 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
3031 value = Tcl_GetString(objv[3]);
danielk197749e46432004-05-27 13:55:27 +00003032 if( Tcl_GetIntFromObj(interp, objv[4], &bytes) ) return TCL_ERROR;
danielk197751e3d8e2004-05-20 01:12:34 +00003033
danielk19775b159dc2007-05-17 16:34:43 +00003034 rc = sqlite3_bind_blob(pStmt, idx, value, bytes, xDestructor);
drhc60d0442004-09-30 13:43:13 +00003035 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
danielk197751e3d8e2004-05-20 01:12:34 +00003036 if( rc!=SQLITE_OK ){
3037 return TCL_ERROR;
3038 }
3039
3040 return TCL_OK;
3041}
3042
drh99ee3602003-02-16 19:13:36 +00003043/*
drh75f6a032004-07-15 14:15:00 +00003044** Usage: sqlite3_bind_parameter_count STMT
3045**
3046** Return the number of wildcards in the given statement.
3047*/
3048static int test_bind_parameter_count(
3049 void * clientData,
3050 Tcl_Interp *interp,
3051 int objc,
3052 Tcl_Obj *CONST objv[]
3053){
3054 sqlite3_stmt *pStmt;
3055
3056 if( objc!=2 ){
3057 Tcl_WrongNumArgs(interp, 1, objv, "STMT");
3058 return TCL_ERROR;
3059 }
3060 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3061 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_bind_parameter_count(pStmt)));
3062 return TCL_OK;
3063}
3064
3065/*
drh895d7472004-08-20 16:02:39 +00003066** Usage: sqlite3_bind_parameter_name STMT N
3067**
3068** Return the name of the Nth wildcard. The first wildcard is 1.
3069** An empty string is returned if N is out of range or if the wildcard
3070** is nameless.
3071*/
3072static int test_bind_parameter_name(
3073 void * clientData,
3074 Tcl_Interp *interp,
3075 int objc,
3076 Tcl_Obj *CONST objv[]
3077){
3078 sqlite3_stmt *pStmt;
3079 int i;
3080
3081 if( objc!=3 ){
3082 Tcl_WrongNumArgs(interp, 1, objv, "STMT N");
3083 return TCL_ERROR;
3084 }
3085 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3086 if( Tcl_GetIntFromObj(interp, objv[2], &i) ) return TCL_ERROR;
3087 Tcl_SetObjResult(interp,
3088 Tcl_NewStringObj(sqlite3_bind_parameter_name(pStmt,i),-1)
3089 );
3090 return TCL_OK;
3091}
3092
3093/*
drhfa6bc002004-09-07 16:19:52 +00003094** Usage: sqlite3_bind_parameter_index STMT NAME
3095**
3096** Return the index of the wildcard called NAME. Return 0 if there is
3097** no such wildcard.
3098*/
3099static int test_bind_parameter_index(
3100 void * clientData,
3101 Tcl_Interp *interp,
3102 int objc,
3103 Tcl_Obj *CONST objv[]
3104){
3105 sqlite3_stmt *pStmt;
3106
3107 if( objc!=3 ){
3108 Tcl_WrongNumArgs(interp, 1, objv, "STMT NAME");
3109 return TCL_ERROR;
3110 }
3111 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3112 Tcl_SetObjResult(interp,
3113 Tcl_NewIntObj(
3114 sqlite3_bind_parameter_index(pStmt,Tcl_GetString(objv[2]))
3115 )
3116 );
3117 return TCL_OK;
3118}
3119
3120/*
danielk1977600dd0b2005-01-20 01:14:23 +00003121** Usage: sqlite3_clear_bindings STMT
3122**
3123*/
danielk1977600dd0b2005-01-20 01:14:23 +00003124static int test_clear_bindings(
3125 void * clientData,
3126 Tcl_Interp *interp,
3127 int objc,
3128 Tcl_Obj *CONST objv[]
3129){
3130 sqlite3_stmt *pStmt;
3131
3132 if( objc!=2 ){
3133 Tcl_WrongNumArgs(interp, 1, objv, "STMT");
3134 return TCL_ERROR;
3135 }
3136 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3137 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_clear_bindings(pStmt)));
3138 return TCL_OK;
3139}
drhf9cb7f52006-06-27 20:06:44 +00003140
3141/*
3142** Usage: sqlite3_sleep MILLISECONDS
3143*/
3144static int test_sleep(
3145 void * clientData,
3146 Tcl_Interp *interp,
3147 int objc,
3148 Tcl_Obj *CONST objv[]
3149){
3150 int ms;
3151
3152 if( objc!=2 ){
3153 Tcl_WrongNumArgs(interp, 1, objv, "MILLISECONDS");
3154 return TCL_ERROR;
3155 }
3156 if( Tcl_GetIntFromObj(interp, objv[1], &ms) ){
3157 return TCL_ERROR;
3158 }
3159 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_sleep(ms)));
3160 return TCL_OK;
3161}
danielk1977600dd0b2005-01-20 01:14:23 +00003162
3163/*
drh99dfe5e2008-10-30 15:03:15 +00003164** Usage: sqlite3_extended_errcode DB
3165**
3166** Return the string representation of the most recent sqlite3_* API
3167** error code. e.g. "SQLITE_ERROR".
3168*/
3169static int test_ex_errcode(
3170 void * clientData,
3171 Tcl_Interp *interp,
3172 int objc,
3173 Tcl_Obj *CONST objv[]
3174){
3175 sqlite3 *db;
3176 int rc;
3177
3178 if( objc!=2 ){
3179 Tcl_AppendResult(interp, "wrong # args: should be \"",
3180 Tcl_GetString(objv[0]), " DB", 0);
3181 return TCL_ERROR;
3182 }
3183 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
3184 rc = sqlite3_extended_errcode(db);
3185 Tcl_AppendResult(interp, (char *)t1ErrorName(rc), 0);
3186 return TCL_OK;
3187}
3188
3189
3190/*
danielk19776622cce2004-05-20 11:00:52 +00003191** Usage: sqlite3_errcode DB
3192**
3193** Return the string representation of the most recent sqlite3_* API
3194** error code. e.g. "SQLITE_ERROR".
3195*/
3196static int test_errcode(
3197 void * clientData,
3198 Tcl_Interp *interp,
3199 int objc,
3200 Tcl_Obj *CONST objv[]
3201){
3202 sqlite3 *db;
drh4ac285a2006-09-15 07:28:50 +00003203 int rc;
danielk19776622cce2004-05-20 11:00:52 +00003204
3205 if( objc!=2 ){
3206 Tcl_AppendResult(interp, "wrong # args: should be \"",
3207 Tcl_GetString(objv[0]), " DB", 0);
3208 return TCL_ERROR;
3209 }
3210 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
drh4ac285a2006-09-15 07:28:50 +00003211 rc = sqlite3_errcode(db);
drh99dfe5e2008-10-30 15:03:15 +00003212 Tcl_AppendResult(interp, (char *)t1ErrorName(rc), 0);
danielk19776622cce2004-05-20 11:00:52 +00003213 return TCL_OK;
3214}
3215
3216/*
danielk197704103022009-02-03 16:51:24 +00003217** Usage: sqlite3_errmsg DB
danielk19776622cce2004-05-20 11:00:52 +00003218**
3219** Returns the UTF-8 representation of the error message string for the
3220** most recent sqlite3_* API call.
3221*/
3222static int test_errmsg(
3223 void * clientData,
3224 Tcl_Interp *interp,
3225 int objc,
3226 Tcl_Obj *CONST objv[]
3227){
drh9bb575f2004-09-06 17:24:11 +00003228 sqlite3 *db;
danielk19776622cce2004-05-20 11:00:52 +00003229 const char *zErr;
3230
3231 if( objc!=2 ){
3232 Tcl_AppendResult(interp, "wrong # args: should be \"",
3233 Tcl_GetString(objv[0]), " DB", 0);
3234 return TCL_ERROR;
3235 }
3236 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
3237
3238 zErr = sqlite3_errmsg(db);
3239 Tcl_SetObjResult(interp, Tcl_NewStringObj(zErr, -1));
3240 return TCL_OK;
3241}
3242
3243/*
3244** Usage: test_errmsg16 DB
3245**
3246** Returns the UTF-16 representation of the error message string for the
3247** most recent sqlite3_* API call. This is a byte array object at the TCL
3248** level, and it includes the 0x00 0x00 terminator bytes at the end of the
3249** UTF-16 string.
3250*/
3251static int test_errmsg16(
3252 void * clientData,
3253 Tcl_Interp *interp,
3254 int objc,
3255 Tcl_Obj *CONST objv[]
3256){
drh5436dc22004-11-14 04:04:17 +00003257#ifndef SQLITE_OMIT_UTF16
drh9bb575f2004-09-06 17:24:11 +00003258 sqlite3 *db;
danielk19776622cce2004-05-20 11:00:52 +00003259 const void *zErr;
drhaed382f2009-04-01 18:40:32 +00003260 const char *z;
danielk1977950f0542006-01-18 05:51:57 +00003261 int bytes = 0;
danielk19776622cce2004-05-20 11:00:52 +00003262
3263 if( objc!=2 ){
3264 Tcl_AppendResult(interp, "wrong # args: should be \"",
3265 Tcl_GetString(objv[0]), " DB", 0);
3266 return TCL_ERROR;
3267 }
3268 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
3269
3270 zErr = sqlite3_errmsg16(db);
danielk1977950f0542006-01-18 05:51:57 +00003271 if( zErr ){
drhaed382f2009-04-01 18:40:32 +00003272 z = zErr;
3273 for(bytes=0; z[bytes] || z[bytes+1]; bytes+=2){}
danielk1977950f0542006-01-18 05:51:57 +00003274 }
danielk19776622cce2004-05-20 11:00:52 +00003275 Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(zErr, bytes));
drh5436dc22004-11-14 04:04:17 +00003276#endif /* SQLITE_OMIT_UTF16 */
danielk19776622cce2004-05-20 11:00:52 +00003277 return TCL_OK;
3278}
3279
3280/*
drh1c767f02009-01-09 02:49:31 +00003281** Usage: sqlite3_prepare DB sql bytes ?tailvar?
danielk19776622cce2004-05-20 11:00:52 +00003282**
3283** Compile up to <bytes> bytes of the supplied SQL string <sql> using
3284** database handle <DB>. The parameter <tailval> is the name of a global
3285** variable that is set to the unused portion of <sql> (if any). A
3286** STMT handle is returned.
3287*/
3288static int test_prepare(
3289 void * clientData,
3290 Tcl_Interp *interp,
3291 int objc,
3292 Tcl_Obj *CONST objv[]
3293){
3294 sqlite3 *db;
3295 const char *zSql;
3296 int bytes;
3297 const char *zTail = 0;
3298 sqlite3_stmt *pStmt = 0;
3299 char zBuf[50];
danielk19774ad17132004-05-21 01:47:26 +00003300 int rc;
danielk19776622cce2004-05-20 11:00:52 +00003301
drh1c767f02009-01-09 02:49:31 +00003302 if( objc!=5 && objc!=4 ){
danielk19776622cce2004-05-20 11:00:52 +00003303 Tcl_AppendResult(interp, "wrong # args: should be \"",
drh1c767f02009-01-09 02:49:31 +00003304 Tcl_GetString(objv[0]), " DB sql bytes ?tailvar?", 0);
danielk19776622cce2004-05-20 11:00:52 +00003305 return TCL_ERROR;
3306 }
3307 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
3308 zSql = Tcl_GetString(objv[2]);
3309 if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR;
3310
drh1c767f02009-01-09 02:49:31 +00003311 rc = sqlite3_prepare(db, zSql, bytes, &pStmt, objc>=5 ? &zTail : 0);
dan937d0de2009-10-15 18:35:38 +00003312 Tcl_ResetResult(interp);
drhc60d0442004-09-30 13:43:13 +00003313 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
drh1c767f02009-01-09 02:49:31 +00003314 if( zTail && objc>=5 ){
danielk19776622cce2004-05-20 11:00:52 +00003315 if( bytes>=0 ){
3316 bytes = bytes - (zTail-zSql);
3317 }
danielk19773a2c8c82008-04-03 14:36:25 +00003318 if( strlen(zTail)<bytes ){
3319 bytes = strlen(zTail);
3320 }
danielk19776622cce2004-05-20 11:00:52 +00003321 Tcl_ObjSetVar2(interp, objv[4], 0, Tcl_NewStringObj(zTail, bytes), 0);
3322 }
danielk19774ad17132004-05-21 01:47:26 +00003323 if( rc!=SQLITE_OK ){
3324 assert( pStmt==0 );
3325 sprintf(zBuf, "(%d) ", rc);
3326 Tcl_AppendResult(interp, zBuf, sqlite3_errmsg(db), 0);
3327 return TCL_ERROR;
3328 }
danielk19776622cce2004-05-20 11:00:52 +00003329
danielk19774ad17132004-05-21 01:47:26 +00003330 if( pStmt ){
drh64b1bea2006-01-15 02:30:57 +00003331 if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR;
danielk19774ad17132004-05-21 01:47:26 +00003332 Tcl_AppendResult(interp, zBuf, 0);
3333 }
danielk19776622cce2004-05-20 11:00:52 +00003334 return TCL_OK;
3335}
3336
3337/*
drh1c767f02009-01-09 02:49:31 +00003338** Usage: sqlite3_prepare_v2 DB sql bytes ?tailvar?
drhb900aaf2006-11-09 00:24:53 +00003339**
3340** Compile up to <bytes> bytes of the supplied SQL string <sql> using
3341** database handle <DB>. The parameter <tailval> is the name of a global
3342** variable that is set to the unused portion of <sql> (if any). A
3343** STMT handle is returned.
3344*/
3345static int test_prepare_v2(
3346 void * clientData,
3347 Tcl_Interp *interp,
3348 int objc,
3349 Tcl_Obj *CONST objv[]
3350){
3351 sqlite3 *db;
3352 const char *zSql;
3353 int bytes;
3354 const char *zTail = 0;
3355 sqlite3_stmt *pStmt = 0;
3356 char zBuf[50];
3357 int rc;
3358
drh1c767f02009-01-09 02:49:31 +00003359 if( objc!=5 && objc!=4 ){
drhb900aaf2006-11-09 00:24:53 +00003360 Tcl_AppendResult(interp, "wrong # args: should be \"",
3361 Tcl_GetString(objv[0]), " DB sql bytes tailvar", 0);
3362 return TCL_ERROR;
3363 }
3364 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
3365 zSql = Tcl_GetString(objv[2]);
3366 if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR;
3367
drh1c767f02009-01-09 02:49:31 +00003368 rc = sqlite3_prepare_v2(db, zSql, bytes, &pStmt, objc>=5 ? &zTail : 0);
danielk19777e29e952007-04-19 11:09:01 +00003369 assert(rc==SQLITE_OK || pStmt==0);
dan937d0de2009-10-15 18:35:38 +00003370 Tcl_ResetResult(interp);
drhb900aaf2006-11-09 00:24:53 +00003371 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
drh1c767f02009-01-09 02:49:31 +00003372 if( zTail && objc>=5 ){
drhb900aaf2006-11-09 00:24:53 +00003373 if( bytes>=0 ){
3374 bytes = bytes - (zTail-zSql);
3375 }
3376 Tcl_ObjSetVar2(interp, objv[4], 0, Tcl_NewStringObj(zTail, bytes), 0);
3377 }
3378 if( rc!=SQLITE_OK ){
3379 assert( pStmt==0 );
3380 sprintf(zBuf, "(%d) ", rc);
3381 Tcl_AppendResult(interp, zBuf, sqlite3_errmsg(db), 0);
3382 return TCL_ERROR;
3383 }
3384
3385 if( pStmt ){
3386 if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR;
3387 Tcl_AppendResult(interp, zBuf, 0);
3388 }
3389 return TCL_OK;
3390}
3391
3392/*
drh4837f532008-05-23 14:49:49 +00003393** Usage: sqlite3_prepare_tkt3134 DB
3394**
3395** Generate a prepared statement for a zero-byte string as a test
3396** for ticket #3134. The string should be preceeded by a zero byte.
3397*/
3398static int test_prepare_tkt3134(
3399 void * clientData,
3400 Tcl_Interp *interp,
3401 int objc,
3402 Tcl_Obj *CONST objv[]
3403){
3404 sqlite3 *db;
3405 static const char zSql[] = "\000SELECT 1";
3406 sqlite3_stmt *pStmt = 0;
3407 char zBuf[50];
3408 int rc;
3409
3410 if( objc!=2 ){
3411 Tcl_AppendResult(interp, "wrong # args: should be \"",
3412 Tcl_GetString(objv[0]), " DB sql bytes tailvar", 0);
3413 return TCL_ERROR;
3414 }
3415 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
3416 rc = sqlite3_prepare_v2(db, &zSql[1], 0, &pStmt, 0);
3417 assert(rc==SQLITE_OK || pStmt==0);
3418 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
3419 if( rc!=SQLITE_OK ){
3420 assert( pStmt==0 );
3421 sprintf(zBuf, "(%d) ", rc);
3422 Tcl_AppendResult(interp, zBuf, sqlite3_errmsg(db), 0);
3423 return TCL_ERROR;
3424 }
3425
3426 if( pStmt ){
3427 if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR;
3428 Tcl_AppendResult(interp, zBuf, 0);
3429 }
3430 return TCL_OK;
3431}
3432
3433/*
drhb900aaf2006-11-09 00:24:53 +00003434** Usage: sqlite3_prepare16 DB sql bytes tailvar
danielk19776622cce2004-05-20 11:00:52 +00003435**
3436** Compile up to <bytes> bytes of the supplied SQL string <sql> using
3437** database handle <DB>. The parameter <tailval> is the name of a global
3438** variable that is set to the unused portion of <sql> (if any). A
3439** STMT handle is returned.
3440*/
3441static int test_prepare16(
3442 void * clientData,
3443 Tcl_Interp *interp,
3444 int objc,
3445 Tcl_Obj *CONST objv[]
3446){
drh5436dc22004-11-14 04:04:17 +00003447#ifndef SQLITE_OMIT_UTF16
danielk19776622cce2004-05-20 11:00:52 +00003448 sqlite3 *db;
3449 const void *zSql;
3450 const void *zTail = 0;
3451 Tcl_Obj *pTail = 0;
3452 sqlite3_stmt *pStmt = 0;
drhc60d0442004-09-30 13:43:13 +00003453 char zBuf[50];
3454 int rc;
danielk19776622cce2004-05-20 11:00:52 +00003455 int bytes; /* The integer specified as arg 3 */
3456 int objlen; /* The byte-array length of arg 2 */
3457
drh1c767f02009-01-09 02:49:31 +00003458 if( objc!=5 && objc!=4 ){
danielk19776622cce2004-05-20 11:00:52 +00003459 Tcl_AppendResult(interp, "wrong # args: should be \"",
drh1c767f02009-01-09 02:49:31 +00003460 Tcl_GetString(objv[0]), " DB sql bytes ?tailvar?", 0);
danielk19776622cce2004-05-20 11:00:52 +00003461 return TCL_ERROR;
3462 }
3463 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
3464 zSql = Tcl_GetByteArrayFromObj(objv[2], &objlen);
3465 if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR;
3466
drh1c767f02009-01-09 02:49:31 +00003467 rc = sqlite3_prepare16(db, zSql, bytes, &pStmt, objc>=5 ? &zTail : 0);
drhc60d0442004-09-30 13:43:13 +00003468 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
3469 if( rc ){
danielk19776622cce2004-05-20 11:00:52 +00003470 return TCL_ERROR;
3471 }
3472
drh1c767f02009-01-09 02:49:31 +00003473 if( objc>=5 ){
3474 if( zTail ){
3475 objlen = objlen - ((u8 *)zTail-(u8 *)zSql);
3476 }else{
3477 objlen = 0;
3478 }
3479 pTail = Tcl_NewByteArrayObj((u8 *)zTail, objlen);
3480 Tcl_IncrRefCount(pTail);
3481 Tcl_ObjSetVar2(interp, objv[4], 0, pTail, 0);
3482 Tcl_DecrRefCount(pTail);
danielk19776622cce2004-05-20 11:00:52 +00003483 }
danielk19776622cce2004-05-20 11:00:52 +00003484
danielk19774ad17132004-05-21 01:47:26 +00003485 if( pStmt ){
drh64b1bea2006-01-15 02:30:57 +00003486 if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR;
danielk19774ad17132004-05-21 01:47:26 +00003487 }
danielk19776622cce2004-05-20 11:00:52 +00003488 Tcl_AppendResult(interp, zBuf, 0);
drh5436dc22004-11-14 04:04:17 +00003489#endif /* SQLITE_OMIT_UTF16 */
danielk19776622cce2004-05-20 11:00:52 +00003490 return TCL_OK;
3491}
3492
danielk19774ad17132004-05-21 01:47:26 +00003493/*
drh1c767f02009-01-09 02:49:31 +00003494** Usage: sqlite3_prepare16_v2 DB sql bytes ?tailvar?
drhb900aaf2006-11-09 00:24:53 +00003495**
3496** Compile up to <bytes> bytes of the supplied SQL string <sql> using
3497** database handle <DB>. The parameter <tailval> is the name of a global
3498** variable that is set to the unused portion of <sql> (if any). A
3499** STMT handle is returned.
3500*/
3501static int test_prepare16_v2(
3502 void * clientData,
3503 Tcl_Interp *interp,
3504 int objc,
3505 Tcl_Obj *CONST objv[]
3506){
3507#ifndef SQLITE_OMIT_UTF16
3508 sqlite3 *db;
3509 const void *zSql;
3510 const void *zTail = 0;
3511 Tcl_Obj *pTail = 0;
3512 sqlite3_stmt *pStmt = 0;
3513 char zBuf[50];
3514 int rc;
3515 int bytes; /* The integer specified as arg 3 */
3516 int objlen; /* The byte-array length of arg 2 */
3517
drh1c767f02009-01-09 02:49:31 +00003518 if( objc!=5 && objc!=4 ){
drhb900aaf2006-11-09 00:24:53 +00003519 Tcl_AppendResult(interp, "wrong # args: should be \"",
drh1c767f02009-01-09 02:49:31 +00003520 Tcl_GetString(objv[0]), " DB sql bytes ?tailvar?", 0);
drhb900aaf2006-11-09 00:24:53 +00003521 return TCL_ERROR;
3522 }
3523 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
3524 zSql = Tcl_GetByteArrayFromObj(objv[2], &objlen);
3525 if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR;
3526
drh1c767f02009-01-09 02:49:31 +00003527 rc = sqlite3_prepare16_v2(db, zSql, bytes, &pStmt, objc>=5 ? &zTail : 0);
drhb900aaf2006-11-09 00:24:53 +00003528 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
3529 if( rc ){
3530 return TCL_ERROR;
3531 }
3532
drh1c767f02009-01-09 02:49:31 +00003533 if( objc>=5 ){
3534 if( zTail ){
3535 objlen = objlen - ((u8 *)zTail-(u8 *)zSql);
3536 }else{
3537 objlen = 0;
3538 }
3539 pTail = Tcl_NewByteArrayObj((u8 *)zTail, objlen);
3540 Tcl_IncrRefCount(pTail);
3541 Tcl_ObjSetVar2(interp, objv[4], 0, pTail, 0);
3542 Tcl_DecrRefCount(pTail);
drhb900aaf2006-11-09 00:24:53 +00003543 }
drhb900aaf2006-11-09 00:24:53 +00003544
3545 if( pStmt ){
3546 if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR;
3547 }
3548 Tcl_AppendResult(interp, zBuf, 0);
3549#endif /* SQLITE_OMIT_UTF16 */
3550 return TCL_OK;
3551}
3552
3553/*
danielk19774ad17132004-05-21 01:47:26 +00003554** Usage: sqlite3_open filename ?options-list?
3555*/
3556static int test_open(
3557 void * clientData,
3558 Tcl_Interp *interp,
3559 int objc,
3560 Tcl_Obj *CONST objv[]
3561){
3562 const char *zFilename;
3563 sqlite3 *db;
3564 int rc;
3565 char zBuf[100];
3566
drhafc91042008-02-21 02:09:45 +00003567 if( objc!=3 && objc!=2 && objc!=1 ){
danielk19774ad17132004-05-21 01:47:26 +00003568 Tcl_AppendResult(interp, "wrong # args: should be \"",
3569 Tcl_GetString(objv[0]), " filename options-list", 0);
3570 return TCL_ERROR;
3571 }
3572
drhafc91042008-02-21 02:09:45 +00003573 zFilename = objc>1 ? Tcl_GetString(objv[1]) : 0;
danielk19774f057f92004-06-08 00:02:33 +00003574 rc = sqlite3_open(zFilename, &db);
danielk19774ad17132004-05-21 01:47:26 +00003575
drh64b1bea2006-01-15 02:30:57 +00003576 if( sqlite3TestMakePointerStr(interp, zBuf, db) ) return TCL_ERROR;
danielk19774ad17132004-05-21 01:47:26 +00003577 Tcl_AppendResult(interp, zBuf, 0);
3578 return TCL_OK;
3579}
3580
3581/*
3582** Usage: sqlite3_open16 filename options
3583*/
3584static int test_open16(
3585 void * clientData,
3586 Tcl_Interp *interp,
3587 int objc,
3588 Tcl_Obj *CONST objv[]
3589){
drh5436dc22004-11-14 04:04:17 +00003590#ifndef SQLITE_OMIT_UTF16
danielk19774ad17132004-05-21 01:47:26 +00003591 const void *zFilename;
3592 sqlite3 *db;
3593 int rc;
3594 char zBuf[100];
3595
3596 if( objc!=3 ){
3597 Tcl_AppendResult(interp, "wrong # args: should be \"",
3598 Tcl_GetString(objv[0]), " filename options-list", 0);
3599 return TCL_ERROR;
3600 }
3601
3602 zFilename = Tcl_GetByteArrayFromObj(objv[1], 0);
danielk19774f057f92004-06-08 00:02:33 +00003603 rc = sqlite3_open16(zFilename, &db);
danielk19774ad17132004-05-21 01:47:26 +00003604
drh64b1bea2006-01-15 02:30:57 +00003605 if( sqlite3TestMakePointerStr(interp, zBuf, db) ) return TCL_ERROR;
danielk19774ad17132004-05-21 01:47:26 +00003606 Tcl_AppendResult(interp, zBuf, 0);
drh5436dc22004-11-14 04:04:17 +00003607#endif /* SQLITE_OMIT_UTF16 */
danielk19774ad17132004-05-21 01:47:26 +00003608 return TCL_OK;
3609}
drhd3d39e92004-05-20 22:16:29 +00003610
3611/*
danielk1977bc6ada42004-06-30 08:20:16 +00003612** Usage: sqlite3_complete16 <UTF-16 string>
3613**
3614** Return 1 if the supplied argument is a complete SQL statement, or zero
3615** otherwise.
3616*/
3617static int test_complete16(
3618 void * clientData,
3619 Tcl_Interp *interp,
3620 int objc,
3621 Tcl_Obj *CONST objv[]
3622){
drhccae6022005-02-26 17:31:26 +00003623#if !defined(SQLITE_OMIT_COMPLETE) && !defined(SQLITE_OMIT_UTF16)
danielk1977bc6ada42004-06-30 08:20:16 +00003624 char *zBuf;
3625
3626 if( objc!=2 ){
3627 Tcl_WrongNumArgs(interp, 1, objv, "<utf-16 sql>");
3628 return TCL_ERROR;
3629 }
3630
drh03d847e2005-12-09 20:21:58 +00003631 zBuf = (char*)Tcl_GetByteArrayFromObj(objv[1], 0);
danielk1977bc6ada42004-06-30 08:20:16 +00003632 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_complete16(zBuf)));
drhccae6022005-02-26 17:31:26 +00003633#endif /* SQLITE_OMIT_COMPLETE && SQLITE_OMIT_UTF16 */
danielk1977bc6ada42004-06-30 08:20:16 +00003634 return TCL_OK;
3635}
3636
3637/*
danielk1977106bb232004-05-21 10:08:53 +00003638** Usage: sqlite3_step STMT
3639**
3640** Advance the statement to the next row.
3641*/
danielk197717240fd2004-05-26 00:07:25 +00003642static int test_step(
danielk1977106bb232004-05-21 10:08:53 +00003643 void * clientData,
3644 Tcl_Interp *interp,
3645 int objc,
3646 Tcl_Obj *CONST objv[]
3647){
3648 sqlite3_stmt *pStmt;
3649 int rc;
3650
danielk1977e1cd9872004-05-22 10:33:04 +00003651 if( objc!=2 ){
danielk1977106bb232004-05-21 10:08:53 +00003652 Tcl_AppendResult(interp, "wrong # args: should be \"",
3653 Tcl_GetString(objv[0]), " STMT", 0);
3654 return TCL_ERROR;
3655 }
3656
3657 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
danielk197717240fd2004-05-26 00:07:25 +00003658 rc = sqlite3_step(pStmt);
danielk1977106bb232004-05-21 10:08:53 +00003659
danielk1977fbcd5852004-06-15 02:44:18 +00003660 /* if( rc!=SQLITE_DONE && rc!=SQLITE_ROW ) return TCL_ERROR; */
drh4f0c5872007-03-26 22:05:01 +00003661 Tcl_SetResult(interp, (char *)t1ErrorName(rc), 0);
danielk1977e1cd9872004-05-22 10:33:04 +00003662 return TCL_OK;
3663}
3664
danielk1977404ca072009-03-16 13:19:36 +00003665static int test_sql(
3666 void * clientData,
3667 Tcl_Interp *interp,
3668 int objc,
3669 Tcl_Obj *CONST objv[]
3670){
3671 sqlite3_stmt *pStmt;
3672
3673 if( objc!=2 ){
3674 Tcl_WrongNumArgs(interp, 1, objv, "STMT");
3675 return TCL_ERROR;
3676 }
3677
3678 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3679 Tcl_SetResult(interp, (char *)sqlite3_sql(pStmt), TCL_VOLATILE);
3680 return TCL_OK;
3681}
3682
danielk1977e1cd9872004-05-22 10:33:04 +00003683/*
danielk197717240fd2004-05-26 00:07:25 +00003684** Usage: sqlite3_column_count STMT
3685**
3686** Return the number of columns returned by the sql statement STMT.
3687*/
3688static int test_column_count(
3689 void * clientData,
3690 Tcl_Interp *interp,
3691 int objc,
3692 Tcl_Obj *CONST objv[]
3693){
3694 sqlite3_stmt *pStmt;
3695
3696 if( objc!=2 ){
3697 Tcl_AppendResult(interp, "wrong # args: should be \"",
3698 Tcl_GetString(objv[0]), " STMT column", 0);
3699 return TCL_ERROR;
3700 }
3701
3702 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3703
3704 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_column_count(pStmt)));
3705 return TCL_OK;
3706}
3707
3708/*
danielk19773cf86062004-05-26 10:11:05 +00003709** Usage: sqlite3_column_type STMT column
3710**
3711** Return the type of the data in column 'column' of the current row.
3712*/
3713static int test_column_type(
3714 void * clientData,
3715 Tcl_Interp *interp,
3716 int objc,
3717 Tcl_Obj *CONST objv[]
3718){
3719 sqlite3_stmt *pStmt;
3720 int col;
3721 int tp;
3722
3723 if( objc!=3 ){
3724 Tcl_AppendResult(interp, "wrong # args: should be \"",
3725 Tcl_GetString(objv[0]), " STMT column", 0);
3726 return TCL_ERROR;
3727 }
3728
3729 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3730 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
3731
3732 tp = sqlite3_column_type(pStmt, col);
3733 switch( tp ){
drh9c054832004-05-31 18:51:57 +00003734 case SQLITE_INTEGER:
danielk19773cf86062004-05-26 10:11:05 +00003735 Tcl_SetResult(interp, "INTEGER", TCL_STATIC);
3736 break;
drh9c054832004-05-31 18:51:57 +00003737 case SQLITE_NULL:
danielk19773cf86062004-05-26 10:11:05 +00003738 Tcl_SetResult(interp, "NULL", TCL_STATIC);
3739 break;
drh9c054832004-05-31 18:51:57 +00003740 case SQLITE_FLOAT:
danielk19773cf86062004-05-26 10:11:05 +00003741 Tcl_SetResult(interp, "FLOAT", TCL_STATIC);
3742 break;
drh9c054832004-05-31 18:51:57 +00003743 case SQLITE_TEXT:
danielk19773cf86062004-05-26 10:11:05 +00003744 Tcl_SetResult(interp, "TEXT", TCL_STATIC);
3745 break;
drh9c054832004-05-31 18:51:57 +00003746 case SQLITE_BLOB:
danielk19773cf86062004-05-26 10:11:05 +00003747 Tcl_SetResult(interp, "BLOB", TCL_STATIC);
3748 break;
3749 default:
3750 assert(0);
3751 }
3752
3753 return TCL_OK;
3754}
3755
3756/*
danielk197704f2e682004-05-27 01:04:07 +00003757** Usage: sqlite3_column_int64 STMT column
danielk19773cf86062004-05-26 10:11:05 +00003758**
3759** Return the data in column 'column' of the current row cast as an
danielk197704f2e682004-05-27 01:04:07 +00003760** wide (64-bit) integer.
danielk19773cf86062004-05-26 10:11:05 +00003761*/
danielk197704f2e682004-05-27 01:04:07 +00003762static int test_column_int64(
danielk19773cf86062004-05-26 10:11:05 +00003763 void * clientData,
3764 Tcl_Interp *interp,
3765 int objc,
3766 Tcl_Obj *CONST objv[]
3767){
3768 sqlite3_stmt *pStmt;
3769 int col;
danielk197704f2e682004-05-27 01:04:07 +00003770 i64 iVal;
danielk19773cf86062004-05-26 10:11:05 +00003771
3772 if( objc!=3 ){
3773 Tcl_AppendResult(interp, "wrong # args: should be \"",
3774 Tcl_GetString(objv[0]), " STMT column", 0);
3775 return TCL_ERROR;
3776 }
3777
3778 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3779 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
3780
danielk197704f2e682004-05-27 01:04:07 +00003781 iVal = sqlite3_column_int64(pStmt, col);
3782 Tcl_SetObjResult(interp, Tcl_NewWideIntObj(iVal));
3783 return TCL_OK;
3784}
3785
3786/*
danielk1977ea61b2c2004-05-27 01:49:51 +00003787** Usage: sqlite3_column_blob STMT column
3788*/
3789static int test_column_blob(
3790 void * clientData,
3791 Tcl_Interp *interp,
3792 int objc,
3793 Tcl_Obj *CONST objv[]
3794){
3795 sqlite3_stmt *pStmt;
3796 int col;
3797
3798 int len;
danielk1977c572ef72004-05-27 09:28:41 +00003799 const void *pBlob;
danielk1977ea61b2c2004-05-27 01:49:51 +00003800
3801 if( objc!=3 ){
3802 Tcl_AppendResult(interp, "wrong # args: should be \"",
3803 Tcl_GetString(objv[0]), " STMT column", 0);
3804 return TCL_ERROR;
3805 }
3806
3807 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3808 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
3809
danielk1977ea61b2c2004-05-27 01:49:51 +00003810 len = sqlite3_column_bytes(pStmt, col);
drh9310ef22007-04-27 17:16:20 +00003811 pBlob = sqlite3_column_blob(pStmt, col);
danielk1977ea61b2c2004-05-27 01:49:51 +00003812 Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(pBlob, len));
3813 return TCL_OK;
3814}
3815
3816/*
danielk197704f2e682004-05-27 01:04:07 +00003817** Usage: sqlite3_column_double STMT column
3818**
3819** Return the data in column 'column' of the current row cast as a double.
3820*/
3821static int test_column_double(
3822 void * clientData,
3823 Tcl_Interp *interp,
3824 int objc,
3825 Tcl_Obj *CONST objv[]
3826){
3827 sqlite3_stmt *pStmt;
3828 int col;
3829 double rVal;
3830
3831 if( objc!=3 ){
3832 Tcl_AppendResult(interp, "wrong # args: should be \"",
3833 Tcl_GetString(objv[0]), " STMT column", 0);
3834 return TCL_ERROR;
3835 }
3836
3837 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3838 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
3839
3840 rVal = sqlite3_column_double(pStmt, col);
danielk1977c572ef72004-05-27 09:28:41 +00003841 Tcl_SetObjResult(interp, Tcl_NewDoubleObj(rVal));
danielk19773cf86062004-05-26 10:11:05 +00003842 return TCL_OK;
3843}
3844
3845/*
danielk197717240fd2004-05-26 00:07:25 +00003846** Usage: sqlite3_data_count STMT
3847**
3848** Return the number of columns returned by the sql statement STMT.
3849*/
3850static int test_data_count(
3851 void * clientData,
3852 Tcl_Interp *interp,
3853 int objc,
3854 Tcl_Obj *CONST objv[]
3855){
3856 sqlite3_stmt *pStmt;
3857
3858 if( objc!=2 ){
3859 Tcl_AppendResult(interp, "wrong # args: should be \"",
3860 Tcl_GetString(objv[0]), " STMT column", 0);
3861 return TCL_ERROR;
3862 }
3863
3864 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3865
3866 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_data_count(pStmt)));
3867 return TCL_OK;
3868}
3869
3870/*
danielk197704f2e682004-05-27 01:04:07 +00003871** Usage: sqlite3_column_text STMT column
3872**
3873** Usage: sqlite3_column_decltype STMT column
3874**
3875** Usage: sqlite3_column_name STMT column
3876*/
3877static int test_stmt_utf8(
drh241db312004-06-22 12:46:53 +00003878 void * clientData, /* Pointer to SQLite API function to be invoke */
danielk197704f2e682004-05-27 01:04:07 +00003879 Tcl_Interp *interp,
3880 int objc,
3881 Tcl_Obj *CONST objv[]
3882){
3883 sqlite3_stmt *pStmt;
3884 int col;
danielk197744a376f2008-08-12 15:04:58 +00003885 const char *(*xFunc)(sqlite3_stmt*, int);
danielk1977f93bbbe2004-05-27 10:30:52 +00003886 const char *zRet;
danielk197704f2e682004-05-27 01:04:07 +00003887
danielk197744a376f2008-08-12 15:04:58 +00003888 xFunc = (const char *(*)(sqlite3_stmt*, int))clientData;
danielk197704f2e682004-05-27 01:04:07 +00003889 if( objc!=3 ){
3890 Tcl_AppendResult(interp, "wrong # args: should be \"",
3891 Tcl_GetString(objv[0]), " STMT column", 0);
3892 return TCL_ERROR;
3893 }
3894
3895 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3896 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
danielk1977f93bbbe2004-05-27 10:30:52 +00003897 zRet = xFunc(pStmt, col);
3898 if( zRet ){
3899 Tcl_SetResult(interp, (char *)zRet, 0);
3900 }
danielk197704f2e682004-05-27 01:04:07 +00003901 return TCL_OK;
3902}
3903
danielk19776b456a22005-03-21 04:04:02 +00003904static int test_global_recover(
3905 void * clientData,
3906 Tcl_Interp *interp,
3907 int objc,
3908 Tcl_Obj *CONST objv[]
3909){
shaneeec556d2008-10-12 00:27:53 +00003910#ifndef SQLITE_OMIT_DEPRECATED
danielk19776b456a22005-03-21 04:04:02 +00003911 int rc;
3912 if( objc!=1 ){
3913 Tcl_WrongNumArgs(interp, 1, objv, "");
3914 return TCL_ERROR;
3915 }
3916 rc = sqlite3_global_recover();
drh4f0c5872007-03-26 22:05:01 +00003917 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
danielk19776b456a22005-03-21 04:04:02 +00003918#endif
3919 return TCL_OK;
3920}
3921
danielk197704f2e682004-05-27 01:04:07 +00003922/*
3923** Usage: sqlite3_column_text STMT column
3924**
3925** Usage: sqlite3_column_decltype STMT column
3926**
3927** Usage: sqlite3_column_name STMT column
3928*/
3929static int test_stmt_utf16(
drh241db312004-06-22 12:46:53 +00003930 void * clientData, /* Pointer to SQLite API function to be invoked */
danielk197704f2e682004-05-27 01:04:07 +00003931 Tcl_Interp *interp,
3932 int objc,
3933 Tcl_Obj *CONST objv[]
3934){
drh5436dc22004-11-14 04:04:17 +00003935#ifndef SQLITE_OMIT_UTF16
danielk197704f2e682004-05-27 01:04:07 +00003936 sqlite3_stmt *pStmt;
3937 int col;
3938 Tcl_Obj *pRet;
3939 const void *zName16;
danielk197744a376f2008-08-12 15:04:58 +00003940 const void *(*xFunc)(sqlite3_stmt*, int);
danielk197704f2e682004-05-27 01:04:07 +00003941
danielk197744a376f2008-08-12 15:04:58 +00003942 xFunc = (const void *(*)(sqlite3_stmt*, int))clientData;
danielk197704f2e682004-05-27 01:04:07 +00003943 if( objc!=3 ){
3944 Tcl_AppendResult(interp, "wrong # args: should be \"",
3945 Tcl_GetString(objv[0]), " STMT column", 0);
3946 return TCL_ERROR;
3947 }
3948
3949 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3950 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
3951
3952 zName16 = xFunc(pStmt, col);
danielk1977f93bbbe2004-05-27 10:30:52 +00003953 if( zName16 ){
drhaed382f2009-04-01 18:40:32 +00003954 int n;
3955 const char *z = zName16;
3956 for(n=0; z[n] || z[n+1]; n+=2){}
3957 pRet = Tcl_NewByteArrayObj(zName16, n+2);
danielk1977f93bbbe2004-05-27 10:30:52 +00003958 Tcl_SetObjResult(interp, pRet);
3959 }
drh5436dc22004-11-14 04:04:17 +00003960#endif /* SQLITE_OMIT_UTF16 */
danielk197704f2e682004-05-27 01:04:07 +00003961
3962 return TCL_OK;
3963}
3964
3965/*
3966** Usage: sqlite3_column_int STMT column
3967**
3968** Usage: sqlite3_column_bytes STMT column
3969**
3970** Usage: sqlite3_column_bytes16 STMT column
3971**
3972*/
3973static int test_stmt_int(
drh241db312004-06-22 12:46:53 +00003974 void * clientData, /* Pointer to SQLite API function to be invoked */
danielk197704f2e682004-05-27 01:04:07 +00003975 Tcl_Interp *interp,
3976 int objc,
3977 Tcl_Obj *CONST objv[]
3978){
3979 sqlite3_stmt *pStmt;
3980 int col;
danielk197744a376f2008-08-12 15:04:58 +00003981 int (*xFunc)(sqlite3_stmt*, int);
danielk197704f2e682004-05-27 01:04:07 +00003982
danielk197755a25a12008-09-11 10:29:15 +00003983 xFunc = (int (*)(sqlite3_stmt*, int))clientData;
danielk197704f2e682004-05-27 01:04:07 +00003984 if( objc!=3 ){
3985 Tcl_AppendResult(interp, "wrong # args: should be \"",
3986 Tcl_GetString(objv[0]), " STMT column", 0);
3987 return TCL_ERROR;
3988 }
3989
3990 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3991 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
3992
3993 Tcl_SetObjResult(interp, Tcl_NewIntObj(xFunc(pStmt, col)));
3994 return TCL_OK;
3995}
3996
danielk19776622cce2004-05-20 11:00:52 +00003997/*
drhcacb2082005-01-11 15:28:33 +00003998** Usage: sqlite_set_magic DB MAGIC-NUMBER
3999**
4000** Set the db->magic value. This is used to test error recovery logic.
4001*/
4002static int sqlite_set_magic(
4003 void * clientData,
4004 Tcl_Interp *interp,
4005 int argc,
4006 char **argv
4007){
4008 sqlite3 *db;
4009 if( argc!=3 ){
4010 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
4011 " DB MAGIC", 0);
4012 return TCL_ERROR;
4013 }
4014 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
4015 if( strcmp(argv[2], "SQLITE_MAGIC_OPEN")==0 ){
4016 db->magic = SQLITE_MAGIC_OPEN;
4017 }else if( strcmp(argv[2], "SQLITE_MAGIC_CLOSED")==0 ){
4018 db->magic = SQLITE_MAGIC_CLOSED;
4019 }else if( strcmp(argv[2], "SQLITE_MAGIC_BUSY")==0 ){
4020 db->magic = SQLITE_MAGIC_BUSY;
4021 }else if( strcmp(argv[2], "SQLITE_MAGIC_ERROR")==0 ){
4022 db->magic = SQLITE_MAGIC_ERROR;
drh902b9ee2008-12-05 17:17:07 +00004023 }else if( Tcl_GetInt(interp, argv[2], (int*)&db->magic) ){
drhcacb2082005-01-11 15:28:33 +00004024 return TCL_ERROR;
4025 }
4026 return TCL_OK;
4027}
4028
4029/*
drhc5cdca62005-01-11 16:54:14 +00004030** Usage: sqlite3_interrupt DB
4031**
4032** Trigger an interrupt on DB
4033*/
4034static int test_interrupt(
4035 void * clientData,
4036 Tcl_Interp *interp,
4037 int argc,
4038 char **argv
4039){
4040 sqlite3 *db;
4041 if( argc!=2 ){
4042 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " DB", 0);
4043 return TCL_ERROR;
4044 }
4045 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
4046 sqlite3_interrupt(db);
4047 return TCL_OK;
4048}
4049
drh79158e12005-09-06 21:40:45 +00004050static u8 *sqlite3_stack_baseline = 0;
4051
drhc5cdca62005-01-11 16:54:14 +00004052/*
drh79158e12005-09-06 21:40:45 +00004053** Fill the stack with a known bitpattern.
danielk1977600dd0b2005-01-20 01:14:23 +00004054*/
drh79158e12005-09-06 21:40:45 +00004055static void prepStack(void){
4056 int i;
4057 u32 bigBuf[65536];
4058 for(i=0; i<sizeof(bigBuf); i++) bigBuf[i] = 0xdeadbeef;
4059 sqlite3_stack_baseline = (u8*)&bigBuf[65536];
4060}
4061
4062/*
4063** Get the current stack depth. Used for debugging only.
4064*/
4065u64 sqlite3StackDepth(void){
4066 u8 x;
4067 return (u64)(sqlite3_stack_baseline - &x);
4068}
4069
4070/*
4071** Usage: sqlite3_stack_used DB SQL
4072**
4073** Try to measure the amount of stack space used by a call to sqlite3_exec
4074*/
4075static int test_stack_used(
danielk1977600dd0b2005-01-20 01:14:23 +00004076 void * clientData,
4077 Tcl_Interp *interp,
4078 int argc,
4079 char **argv
4080){
4081 sqlite3 *db;
drh79158e12005-09-06 21:40:45 +00004082 int i;
4083 if( argc!=3 ){
4084 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
4085 " DB SQL", 0);
danielk1977600dd0b2005-01-20 01:14:23 +00004086 return TCL_ERROR;
4087 }
drh79158e12005-09-06 21:40:45 +00004088 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
4089 prepStack();
drh37527852006-03-16 16:19:56 +00004090 (void)sqlite3_exec(db, argv[2], 0, 0, 0);
drh79158e12005-09-06 21:40:45 +00004091 for(i=65535; i>=0 && ((u32*)sqlite3_stack_baseline)[-i]==0xdeadbeef; i--){}
4092 Tcl_SetObjResult(interp, Tcl_NewIntObj(i*4));
danielk1977600dd0b2005-01-20 01:14:23 +00004093 return TCL_OK;
4094}
danielk1977600dd0b2005-01-20 01:14:23 +00004095
4096/*
danielk19779636c4e2005-01-25 04:27:54 +00004097** Usage: sqlite_delete_function DB function-name
4098**
4099** Delete the user function 'function-name' from database handle DB. It
4100** is assumed that the user function was created as UTF8, any number of
4101** arguments (the way the TCL interface does it).
4102*/
4103static int delete_function(
4104 void * clientData,
4105 Tcl_Interp *interp,
4106 int argc,
4107 char **argv
4108){
4109 int rc;
4110 sqlite3 *db;
4111 if( argc!=3 ){
4112 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
4113 " DB function-name", 0);
4114 return TCL_ERROR;
4115 }
4116 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
4117 rc = sqlite3_create_function(db, argv[2], -1, SQLITE_UTF8, 0, 0, 0, 0);
drh4f0c5872007-03-26 22:05:01 +00004118 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
danielk19779636c4e2005-01-25 04:27:54 +00004119 return TCL_OK;
4120}
4121
4122/*
4123** Usage: sqlite_delete_collation DB collation-name
4124**
4125** Delete the collation sequence 'collation-name' from database handle
4126** DB. It is assumed that the collation sequence was created as UTF8 (the
4127** way the TCL interface does it).
4128*/
4129static int delete_collation(
4130 void * clientData,
4131 Tcl_Interp *interp,
4132 int argc,
4133 char **argv
4134){
4135 int rc;
4136 sqlite3 *db;
4137 if( argc!=3 ){
4138 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
4139 " DB function-name", 0);
4140 return TCL_ERROR;
4141 }
4142 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
4143 rc = sqlite3_create_collation(db, argv[2], SQLITE_UTF8, 0, 0);
drh4f0c5872007-03-26 22:05:01 +00004144 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
danielk19779636c4e2005-01-25 04:27:54 +00004145 return TCL_OK;
4146}
4147
4148/*
drh3e1d8e62005-05-26 16:23:34 +00004149** Usage: sqlite3_get_autocommit DB
4150**
4151** Return true if the database DB is currently in auto-commit mode.
4152** Return false if not.
4153*/
4154static int get_autocommit(
4155 void * clientData,
4156 Tcl_Interp *interp,
4157 int argc,
4158 char **argv
4159){
4160 char zBuf[30];
4161 sqlite3 *db;
4162 if( argc!=2 ){
4163 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
4164 " DB", 0);
4165 return TCL_ERROR;
4166 }
4167 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
4168 sprintf(zBuf, "%d", sqlite3_get_autocommit(db));
4169 Tcl_AppendResult(interp, zBuf, 0);
4170 return TCL_OK;
4171}
4172
4173/*
drh30867652006-07-06 10:59:57 +00004174** Usage: sqlite3_busy_timeout DB MS
4175**
4176** Set the busy timeout. This is more easily done using the timeout
4177** method of the TCL interface. But we need a way to test the case
4178** where it returns SQLITE_MISUSE.
4179*/
4180static int test_busy_timeout(
4181 void * clientData,
4182 Tcl_Interp *interp,
4183 int argc,
4184 char **argv
4185){
4186 int rc, ms;
4187 sqlite3 *db;
4188 if( argc!=3 ){
4189 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
4190 " DB", 0);
4191 return TCL_ERROR;
4192 }
4193 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
4194 if( Tcl_GetInt(interp, argv[2], &ms) ) return TCL_ERROR;
4195 rc = sqlite3_busy_timeout(db, ms);
4196 Tcl_AppendResult(interp, sqlite3TestErrorName(rc), 0);
4197 return TCL_OK;
4198}
4199
4200/*
drh92febd92004-08-20 18:34:20 +00004201** Usage: tcl_variable_type VARIABLENAME
4202**
4203** Return the name of the internal representation for the
4204** value of the given variable.
4205*/
4206static int tcl_variable_type(
4207 void * clientData,
4208 Tcl_Interp *interp,
4209 int objc,
4210 Tcl_Obj *CONST objv[]
4211){
4212 Tcl_Obj *pVar;
4213 if( objc!=2 ){
4214 Tcl_WrongNumArgs(interp, 1, objv, "VARIABLE");
4215 return TCL_ERROR;
4216 }
4217 pVar = Tcl_GetVar2Ex(interp, Tcl_GetString(objv[1]), 0, TCL_LEAVE_ERR_MSG);
4218 if( pVar==0 ) return TCL_ERROR;
4219 if( pVar->typePtr ){
4220 Tcl_SetObjResult(interp, Tcl_NewStringObj(pVar->typePtr->name, -1));
4221 }
4222 return TCL_OK;
4223}
4224
4225/*
drh6aafc292006-01-05 15:50:06 +00004226** Usage: sqlite3_release_memory ?N?
4227**
4228** Attempt to release memory currently held but not actually required.
4229** The integer N is the number of bytes we are trying to release. The
4230** return value is the amount of memory actually released.
4231*/
4232static int test_release_memory(
4233 void * clientData,
4234 Tcl_Interp *interp,
4235 int objc,
4236 Tcl_Obj *CONST objv[]
4237){
drh6f7adc82006-01-11 21:41:20 +00004238#if defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) && !defined(SQLITE_OMIT_DISKIO)
drh6aafc292006-01-05 15:50:06 +00004239 int N;
4240 int amt;
4241 if( objc!=1 && objc!=2 ){
4242 Tcl_WrongNumArgs(interp, 1, objv, "?N?");
4243 return TCL_ERROR;
4244 }
4245 if( objc==2 ){
4246 if( Tcl_GetIntFromObj(interp, objv[1], &N) ) return TCL_ERROR;
4247 }else{
4248 N = -1;
4249 }
4250 amt = sqlite3_release_memory(N);
4251 Tcl_SetObjResult(interp, Tcl_NewIntObj(amt));
4252#endif
4253 return TCL_OK;
4254}
4255
4256/*
4257** Usage: sqlite3_soft_heap_limit ?N?
4258**
4259** Query or set the soft heap limit for the current thread. The
4260** limit is only changed if the N is present. The previous limit
4261** is returned.
4262*/
4263static int test_soft_heap_limit(
4264 void * clientData,
4265 Tcl_Interp *interp,
4266 int objc,
4267 Tcl_Obj *CONST objv[]
4268){
drh86f8c192007-08-22 00:39:19 +00004269 static int softHeapLimit = 0;
drh6aafc292006-01-05 15:50:06 +00004270 int amt;
4271 if( objc!=1 && objc!=2 ){
4272 Tcl_WrongNumArgs(interp, 1, objv, "?N?");
4273 return TCL_ERROR;
4274 }
drh86f8c192007-08-22 00:39:19 +00004275 amt = softHeapLimit;
drh6aafc292006-01-05 15:50:06 +00004276 if( objc==2 ){
4277 int N;
4278 if( Tcl_GetIntFromObj(interp, objv[1], &N) ) return TCL_ERROR;
4279 sqlite3_soft_heap_limit(N);
drh86f8c192007-08-22 00:39:19 +00004280 softHeapLimit = N;
drh6aafc292006-01-05 15:50:06 +00004281 }
4282 Tcl_SetObjResult(interp, Tcl_NewIntObj(amt));
drh6aafc292006-01-05 15:50:06 +00004283 return TCL_OK;
4284}
4285
4286/*
drhb4bc7052006-01-11 23:40:33 +00004287** Usage: sqlite3_thread_cleanup
4288**
4289** Call the sqlite3_thread_cleanup API.
4290*/
4291static int test_thread_cleanup(
4292 void * clientData,
4293 Tcl_Interp *interp,
4294 int objc,
4295 Tcl_Obj *CONST objv[]
4296){
shaneeec556d2008-10-12 00:27:53 +00004297#ifndef SQLITE_OMIT_DEPRECATED
drhb4bc7052006-01-11 23:40:33 +00004298 sqlite3_thread_cleanup();
shaneeec556d2008-10-12 00:27:53 +00004299#endif
drhb4bc7052006-01-11 23:40:33 +00004300 return TCL_OK;
4301}
4302
drhb4bc7052006-01-11 23:40:33 +00004303/*
drhc6ba55f2007-04-05 17:36:18 +00004304** Usage: sqlite3_pager_refcounts DB
4305**
drhf5345442007-04-09 12:45:02 +00004306** Return a list of numbers which are the PagerRefcount for all
4307** pagers on each database connection.
drhc6ba55f2007-04-05 17:36:18 +00004308*/
4309static int test_pager_refcounts(
4310 void * clientData,
4311 Tcl_Interp *interp,
4312 int objc,
4313 Tcl_Obj *CONST objv[]
4314){
4315 sqlite3 *db;
4316 int i;
4317 int v, *a;
4318 Tcl_Obj *pResult;
4319
4320 if( objc!=2 ){
4321 Tcl_AppendResult(interp, "wrong # args: should be \"",
drhf5345442007-04-09 12:45:02 +00004322 Tcl_GetStringFromObj(objv[0], 0), " DB", 0);
drhc6ba55f2007-04-05 17:36:18 +00004323 return TCL_ERROR;
4324 }
4325 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
4326 pResult = Tcl_NewObj();
4327 for(i=0; i<db->nDb; i++){
4328 if( db->aDb[i].pBt==0 ){
4329 v = -1;
4330 }else{
drh27641702007-08-22 02:56:42 +00004331 sqlite3_mutex_enter(db->mutex);
drhc6ba55f2007-04-05 17:36:18 +00004332 a = sqlite3PagerStats(sqlite3BtreePager(db->aDb[i].pBt));
4333 v = a[0];
drh27641702007-08-22 02:56:42 +00004334 sqlite3_mutex_leave(db->mutex);
drhc6ba55f2007-04-05 17:36:18 +00004335 }
4336 Tcl_ListObjAppendElement(0, pResult, Tcl_NewIntObj(v));
4337 }
4338 Tcl_SetObjResult(interp, pResult);
4339 return TCL_OK;
4340}
4341
4342
4343/*
drh80788d82006-09-02 14:50:23 +00004344** tclcmd: working_64bit_int
4345**
4346** Some TCL builds (ex: cygwin) do not support 64-bit integers. This
4347** leads to a number of test failures. The present command checks the
4348** TCL build to see whether or not it supports 64-bit integers. It
4349** returns TRUE if it does and FALSE if not.
4350**
4351** This command is used to warn users that their TCL build is defective
4352** and that the errors they are seeing in the test scripts might be
4353** a result of their defective TCL rather than problems in SQLite.
4354*/
4355static int working_64bit_int(
4356 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
4357 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
4358 int objc, /* Number of arguments */
4359 Tcl_Obj *CONST objv[] /* Command arguments */
4360){
4361 Tcl_Obj *pTestObj;
4362 int working = 0;
4363
4364 pTestObj = Tcl_NewWideIntObj(1000000*(i64)1234567890);
4365 working = strcmp(Tcl_GetString(pTestObj), "1234567890000000")==0;
4366 Tcl_DecrRefCount(pTestObj);
4367 Tcl_SetObjResult(interp, Tcl_NewBooleanObj(working));
4368 return TCL_OK;
4369}
4370
4371
4372/*
drh9bc54492007-10-23 14:49:59 +00004373** tclcmd: vfs_unlink_test
4374**
4375** This TCL command unregisters the primary VFS and then registers
4376** it back again. This is used to test the ability to register a
4377** VFS when none are previously registered, and the ability to
4378** unregister the only available VFS. Ticket #2738
4379*/
4380static int vfs_unlink_test(
4381 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
4382 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
4383 int objc, /* Number of arguments */
4384 Tcl_Obj *CONST objv[] /* Command arguments */
4385){
4386 int i;
drh91fd4d42008-01-19 20:11:25 +00004387 sqlite3_vfs *pMain;
drh9bc54492007-10-23 14:49:59 +00004388 sqlite3_vfs *apVfs[20];
drh91fd4d42008-01-19 20:11:25 +00004389 sqlite3_vfs one, two;
drh9bc54492007-10-23 14:49:59 +00004390
drh91fd4d42008-01-19 20:11:25 +00004391 sqlite3_vfs_unregister(0); /* Unregister of NULL is harmless */
4392 one.zName = "__one";
4393 two.zName = "__two";
4394
4395 /* Calling sqlite3_vfs_register with 2nd argument of 0 does not
4396 ** change the default VFS
4397 */
4398 pMain = sqlite3_vfs_find(0);
4399 sqlite3_vfs_register(&one, 0);
4400 assert( pMain==0 || pMain==sqlite3_vfs_find(0) );
4401 sqlite3_vfs_register(&two, 0);
4402 assert( pMain==0 || pMain==sqlite3_vfs_find(0) );
4403
4404 /* We can find a VFS by its name */
4405 assert( sqlite3_vfs_find("__one")==&one );
4406 assert( sqlite3_vfs_find("__two")==&two );
4407
4408 /* Calling sqlite_vfs_register with non-zero second parameter changes the
4409 ** default VFS, even if the 1st parameter is an existig VFS that is
4410 ** previously registered as the non-default.
4411 */
4412 sqlite3_vfs_register(&one, 1);
4413 assert( sqlite3_vfs_find("__one")==&one );
4414 assert( sqlite3_vfs_find("__two")==&two );
4415 assert( sqlite3_vfs_find(0)==&one );
4416 sqlite3_vfs_register(&two, 1);
4417 assert( sqlite3_vfs_find("__one")==&one );
4418 assert( sqlite3_vfs_find("__two")==&two );
4419 assert( sqlite3_vfs_find(0)==&two );
4420 if( pMain ){
4421 sqlite3_vfs_register(pMain, 1);
4422 assert( sqlite3_vfs_find("__one")==&one );
4423 assert( sqlite3_vfs_find("__two")==&two );
4424 assert( sqlite3_vfs_find(0)==pMain );
4425 }
4426
4427 /* Unlink the default VFS. Repeat until there are no more VFSes
4428 ** registered.
4429 */
drh9bc54492007-10-23 14:49:59 +00004430 for(i=0; i<sizeof(apVfs)/sizeof(apVfs[0]); i++){
4431 apVfs[i] = sqlite3_vfs_find(0);
4432 if( apVfs[i] ){
4433 assert( apVfs[i]==sqlite3_vfs_find(apVfs[i]->zName) );
4434 sqlite3_vfs_unregister(apVfs[i]);
4435 assert( 0==sqlite3_vfs_find(apVfs[i]->zName) );
4436 }
4437 }
4438 assert( 0==sqlite3_vfs_find(0) );
mlcreech1f045332008-04-08 03:07:54 +00004439
4440 /* Register the main VFS as non-default (will be made default, since
4441 ** it'll be the only one in existence).
4442 */
4443 sqlite3_vfs_register(pMain, 0);
4444 assert( sqlite3_vfs_find(0)==pMain );
4445
4446 /* Un-register the main VFS again to restore an empty VFS list */
4447 sqlite3_vfs_unregister(pMain);
4448 assert( 0==sqlite3_vfs_find(0) );
drh91fd4d42008-01-19 20:11:25 +00004449
4450 /* Relink all VFSes in reverse order. */
drh9bc54492007-10-23 14:49:59 +00004451 for(i=sizeof(apVfs)/sizeof(apVfs[0])-1; i>=0; i--){
4452 if( apVfs[i] ){
4453 sqlite3_vfs_register(apVfs[i], 1);
4454 assert( apVfs[i]==sqlite3_vfs_find(0) );
4455 assert( apVfs[i]==sqlite3_vfs_find(apVfs[i]->zName) );
4456 }
4457 }
drh91fd4d42008-01-19 20:11:25 +00004458
4459 /* Unregister out sample VFSes. */
4460 sqlite3_vfs_unregister(&one);
4461 sqlite3_vfs_unregister(&two);
4462
4463 /* Unregistering a VFS that is not currently registered is harmless */
4464 sqlite3_vfs_unregister(&one);
4465 sqlite3_vfs_unregister(&two);
4466 assert( sqlite3_vfs_find("__one")==0 );
4467 assert( sqlite3_vfs_find("__two")==0 );
4468
4469 /* We should be left with the original default VFS back as the
4470 ** original */
4471 assert( sqlite3_vfs_find(0)==pMain );
4472
drh9bc54492007-10-23 14:49:59 +00004473 return TCL_OK;
4474}
4475
drh93aed5a2008-01-16 17:46:38 +00004476/*
drhc8d75672008-07-08 02:12:37 +00004477** tclcmd: vfs_initfail_test
4478**
4479** This TCL command attempts to vfs_find and vfs_register when the
4480** sqlite3_initialize() interface is failing. All calls should fail.
4481*/
4482static int vfs_initfail_test(
4483 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
4484 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
4485 int objc, /* Number of arguments */
4486 Tcl_Obj *CONST objv[] /* Command arguments */
4487){
4488 sqlite3_vfs one;
4489 one.zName = "__one";
4490
4491 if( sqlite3_vfs_find(0) ) return TCL_ERROR;
4492 sqlite3_vfs_register(&one, 0);
4493 if( sqlite3_vfs_find(0) ) return TCL_ERROR;
4494 sqlite3_vfs_register(&one, 1);
4495 if( sqlite3_vfs_find(0) ) return TCL_ERROR;
4496 return TCL_OK;
4497}
4498
4499/*
drha2820972008-07-07 13:31:58 +00004500** Saved VFSes
4501*/
4502static sqlite3_vfs *apVfs[20];
4503static int nVfs = 0;
4504
4505/*
4506** tclcmd: vfs_unregister_all
4507**
4508** Unregister all VFSes.
4509*/
4510static int vfs_unregister_all(
4511 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
4512 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
4513 int objc, /* Number of arguments */
4514 Tcl_Obj *CONST objv[] /* Command arguments */
4515){
4516 int i;
4517 for(i=0; i<ArraySize(apVfs); i++){
4518 apVfs[i] = sqlite3_vfs_find(0);
4519 if( apVfs[i]==0 ) break;
4520 sqlite3_vfs_unregister(apVfs[i]);
4521 }
4522 nVfs = i;
4523 return TCL_OK;
4524}
4525/*
4526** tclcmd: vfs_reregister_all
4527**
4528** Restore all VFSes that were removed using vfs_unregister_all
4529*/
4530static int vfs_reregister_all(
4531 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
4532 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
4533 int objc, /* Number of arguments */
4534 Tcl_Obj *CONST objv[] /* Command arguments */
4535){
4536 int i;
4537 for(i=0; i<nVfs; i++){
4538 sqlite3_vfs_register(apVfs[i], i==0);
4539 }
4540 return TCL_OK;
4541}
4542
4543
4544/*
drh55176252008-01-22 14:50:16 +00004545** tclcmd: file_control_test DB
4546**
4547** This TCL command runs the sqlite3_file_control interface and
4548** verifies correct operation of the same.
4549*/
4550static int file_control_test(
4551 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
4552 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
4553 int objc, /* Number of arguments */
4554 Tcl_Obj *CONST objv[] /* Command arguments */
4555){
4556 int iArg = 0;
4557 sqlite3 *db;
4558 int rc;
4559
4560 if( objc!=2 ){
4561 Tcl_AppendResult(interp, "wrong # args: should be \"",
4562 Tcl_GetStringFromObj(objv[0], 0), " DB", 0);
4563 return TCL_ERROR;
4564 }
4565 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
4566 rc = sqlite3_file_control(db, 0, 0, &iArg);
4567 assert( rc==SQLITE_ERROR );
4568 rc = sqlite3_file_control(db, "notadatabase", SQLITE_FCNTL_LOCKSTATE, &iArg);
4569 assert( rc==SQLITE_ERROR );
4570 rc = sqlite3_file_control(db, "main", -1, &iArg);
4571 assert( rc==SQLITE_ERROR );
4572 rc = sqlite3_file_control(db, "temp", -1, &iArg);
4573 assert( rc==SQLITE_ERROR );
aswiftaebf4132008-11-21 00:10:35 +00004574
4575 return TCL_OK;
4576}
4577
4578
4579/*
4580** tclcmd: file_control_lasterrno_test DB
4581**
4582** This TCL command runs the sqlite3_file_control interface and
4583** verifies correct operation of the SQLITE_LAST_ERRNO verb.
4584*/
4585static int file_control_lasterrno_test(
4586 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
4587 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
4588 int objc, /* Number of arguments */
4589 Tcl_Obj *CONST objv[] /* Command arguments */
4590){
4591 int iArg = 0;
4592 sqlite3 *db;
4593 int rc;
4594
4595 if( objc!=2 ){
4596 Tcl_AppendResult(interp, "wrong # args: should be \"",
4597 Tcl_GetStringFromObj(objv[0], 0), " DB", 0);
4598 return TCL_ERROR;
4599 }
shane9db299f2009-01-30 05:59:10 +00004600 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
4601 return TCL_ERROR;
4602 }
aswiftaebf4132008-11-21 00:10:35 +00004603 rc = sqlite3_file_control(db, NULL, SQLITE_LAST_ERRNO, &iArg);
shane9db299f2009-01-30 05:59:10 +00004604 if( rc ){
4605 Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
4606 return TCL_ERROR;
4607 }
aswiftaebf4132008-11-21 00:10:35 +00004608 if( iArg!=0 ) {
4609 Tcl_AppendResult(interp, "Unexpected non-zero errno: ",
4610 Tcl_GetStringFromObj(Tcl_NewIntObj(iArg), 0), " ", 0);
4611 return TCL_ERROR;
4612 }
drh55176252008-01-22 14:50:16 +00004613 return TCL_OK;
4614}
4615
4616/*
drhad245812010-06-01 00:28:42 +00004617** tclcmd: file_control_lockproxy_test DB PWD
aswiftaebf4132008-11-21 00:10:35 +00004618**
4619** This TCL command runs the sqlite3_file_control interface and
4620** verifies correct operation of the SQLITE_GET_LOCKPROXYFILE and
4621** SQLITE_SET_LOCKPROXYFILE verbs.
4622*/
4623static int file_control_lockproxy_test(
4624 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
4625 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
4626 int objc, /* Number of arguments */
4627 Tcl_Obj *CONST objv[] /* Command arguments */
4628){
aswiftaebf4132008-11-21 00:10:35 +00004629 sqlite3 *db;
drhad245812010-06-01 00:28:42 +00004630 const char *zPwd;
4631 int nPwd;
aswiftaebf4132008-11-21 00:10:35 +00004632
drhad245812010-06-01 00:28:42 +00004633 if( objc!=3 ){
aswiftaebf4132008-11-21 00:10:35 +00004634 Tcl_AppendResult(interp, "wrong # args: should be \"",
drhad245812010-06-01 00:28:42 +00004635 Tcl_GetStringFromObj(objv[0], 0), " DB PWD", 0);
aswiftaebf4132008-11-21 00:10:35 +00004636 return TCL_ERROR;
4637 }
shane9db299f2009-01-30 05:59:10 +00004638 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
4639 return TCL_ERROR;
4640 }
drhad245812010-06-01 00:28:42 +00004641 zPwd = Tcl_GetStringFromObj(objv[2], &nPwd);
aswiftaebf4132008-11-21 00:10:35 +00004642
drh9b35ea62008-11-29 02:20:26 +00004643#if !defined(SQLITE_ENABLE_LOCKING_STYLE)
drhd2cb50b2009-01-09 21:41:17 +00004644# if defined(__APPLE__)
drh9b35ea62008-11-29 02:20:26 +00004645# define SQLITE_ENABLE_LOCKING_STYLE 1
4646# else
4647# define SQLITE_ENABLE_LOCKING_STYLE 0
4648# endif
4649#endif
drhd2cb50b2009-01-09 21:41:17 +00004650#if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
drh7708e972008-11-29 00:56:52 +00004651 {
aswiftaebf4132008-11-21 00:10:35 +00004652 char *testPath;
drh103fe742008-12-11 02:56:07 +00004653 int rc;
drhad245812010-06-01 00:28:42 +00004654 char proxyPath[400];
4655
4656 if( sizeof(proxyPath)<nPwd+20 ){
4657 Tcl_AppendResult(interp, "PWD too big", (void*)0);
4658 return TCL_ERROR;
4659 }
4660 sprintf(proxyPath, "%s/test.proxy", zPwd);
drh7708e972008-11-29 00:56:52 +00004661 rc = sqlite3_file_control(db, NULL, SQLITE_SET_LOCKPROXYFILE, proxyPath);
4662 if( rc ){
shane9db299f2009-01-30 05:59:10 +00004663 Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
4664 return TCL_ERROR;
drh7708e972008-11-29 00:56:52 +00004665 }
aswiftaebf4132008-11-21 00:10:35 +00004666 rc = sqlite3_file_control(db, NULL, SQLITE_GET_LOCKPROXYFILE, &testPath);
shane9db299f2009-01-30 05:59:10 +00004667 if( strncmp(proxyPath,testPath,11) ){
drh7708e972008-11-29 00:56:52 +00004668 Tcl_AppendResult(interp, "Lock proxy file did not match the "
4669 "previously assigned value", 0);
aswiftaebf4132008-11-21 00:10:35 +00004670 return TCL_ERROR;
4671 }
drh7708e972008-11-29 00:56:52 +00004672 if( rc ){
4673 Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
4674 return TCL_ERROR;
4675 }
4676 rc = sqlite3_file_control(db, NULL, SQLITE_SET_LOCKPROXYFILE, proxyPath);
4677 if( rc ){
4678 Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
4679 return TCL_ERROR;
4680 }
aswiftaebf4132008-11-21 00:10:35 +00004681 }
4682#endif
4683 return TCL_OK;
4684}
4685
4686
4687/*
danielk1977e339d652008-06-28 11:23:00 +00004688** tclcmd: sqlite3_vfs_list
4689**
4690** Return a tcl list containing the names of all registered vfs's.
4691*/
4692static int vfs_list(
4693 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
4694 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
4695 int objc, /* Number of arguments */
4696 Tcl_Obj *CONST objv[] /* Command arguments */
4697){
4698 sqlite3_vfs *pVfs;
4699 Tcl_Obj *pRet = Tcl_NewObj();
4700 if( objc!=1 ){
4701 Tcl_WrongNumArgs(interp, 1, objv, "");
4702 return TCL_ERROR;
4703 }
4704 for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){
4705 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewStringObj(pVfs->zName, -1));
4706 }
4707 Tcl_SetObjResult(interp, pRet);
4708 return TCL_OK;
4709}
4710
4711/*
drhb1a6c3c2008-03-20 16:30:17 +00004712** tclcmd: sqlite3_limit DB ID VALUE
4713**
4714** This TCL command runs the sqlite3_limit interface and
4715** verifies correct operation of the same.
4716*/
4717static int test_limit(
4718 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
4719 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
4720 int objc, /* Number of arguments */
4721 Tcl_Obj *CONST objv[] /* Command arguments */
4722){
4723 sqlite3 *db;
4724 int rc;
4725 static const struct {
4726 char *zName;
4727 int id;
4728 } aId[] = {
4729 { "SQLITE_LIMIT_LENGTH", SQLITE_LIMIT_LENGTH },
4730 { "SQLITE_LIMIT_SQL_LENGTH", SQLITE_LIMIT_SQL_LENGTH },
4731 { "SQLITE_LIMIT_COLUMN", SQLITE_LIMIT_COLUMN },
4732 { "SQLITE_LIMIT_EXPR_DEPTH", SQLITE_LIMIT_EXPR_DEPTH },
4733 { "SQLITE_LIMIT_COMPOUND_SELECT", SQLITE_LIMIT_COMPOUND_SELECT },
4734 { "SQLITE_LIMIT_VDBE_OP", SQLITE_LIMIT_VDBE_OP },
4735 { "SQLITE_LIMIT_FUNCTION_ARG", SQLITE_LIMIT_FUNCTION_ARG },
4736 { "SQLITE_LIMIT_ATTACHED", SQLITE_LIMIT_ATTACHED },
4737 { "SQLITE_LIMIT_LIKE_PATTERN_LENGTH", SQLITE_LIMIT_LIKE_PATTERN_LENGTH },
4738 { "SQLITE_LIMIT_VARIABLE_NUMBER", SQLITE_LIMIT_VARIABLE_NUMBER },
drh417168a2009-09-07 18:14:02 +00004739 { "SQLITE_LIMIT_TRIGGER_DEPTH", SQLITE_LIMIT_TRIGGER_DEPTH },
drh521cc842008-04-15 02:36:33 +00004740
4741 /* Out of range test cases */
4742 { "SQLITE_LIMIT_TOOSMALL", -1, },
drh417168a2009-09-07 18:14:02 +00004743 { "SQLITE_LIMIT_TOOBIG", SQLITE_LIMIT_TRIGGER_DEPTH+1 },
drhb1a6c3c2008-03-20 16:30:17 +00004744 };
4745 int i, id;
4746 int val;
4747 const char *zId;
4748
4749 if( objc!=4 ){
4750 Tcl_AppendResult(interp, "wrong # args: should be \"",
4751 Tcl_GetStringFromObj(objv[0], 0), " DB ID VALUE", 0);
4752 return TCL_ERROR;
4753 }
4754 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
4755 zId = Tcl_GetString(objv[2]);
4756 for(i=0; i<sizeof(aId)/sizeof(aId[0]); i++){
4757 if( strcmp(zId, aId[i].zName)==0 ){
4758 id = aId[i].id;
4759 break;
4760 }
4761 }
4762 if( i>=sizeof(aId)/sizeof(aId[0]) ){
4763 Tcl_AppendResult(interp, "unknown limit type: ", zId, (char*)0);
4764 return TCL_ERROR;
4765 }
4766 if( Tcl_GetIntFromObj(interp, objv[3], &val) ) return TCL_ERROR;
4767 rc = sqlite3_limit(db, id, val);
4768 Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
4769 return TCL_OK;
4770}
4771
4772/*
drh93aed5a2008-01-16 17:46:38 +00004773** tclcmd: save_prng_state
drha2820972008-07-07 13:31:58 +00004774**
4775** Save the state of the pseudo-random number generator.
4776** At the same time, verify that sqlite3_test_control works even when
4777** called with an out-of-range opcode.
drh93aed5a2008-01-16 17:46:38 +00004778*/
4779static int save_prng_state(
4780 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
4781 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
4782 int objc, /* Number of arguments */
4783 Tcl_Obj *CONST objv[] /* Command arguments */
4784){
drha2820972008-07-07 13:31:58 +00004785 int rc = sqlite3_test_control(9999);
4786 assert( rc==0 );
4787 rc = sqlite3_test_control(-1);
4788 assert( rc==0 );
drh2fa18682008-03-19 14:15:34 +00004789 sqlite3_test_control(SQLITE_TESTCTRL_PRNG_SAVE);
drh93aed5a2008-01-16 17:46:38 +00004790 return TCL_OK;
4791}
4792/*
4793** tclcmd: restore_prng_state
4794*/
4795static int restore_prng_state(
4796 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
4797 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
4798 int objc, /* Number of arguments */
4799 Tcl_Obj *CONST objv[] /* Command arguments */
4800){
drh2fa18682008-03-19 14:15:34 +00004801 sqlite3_test_control(SQLITE_TESTCTRL_PRNG_RESTORE);
drh93aed5a2008-01-16 17:46:38 +00004802 return TCL_OK;
4803}
4804/*
4805** tclcmd: reset_prng_state
4806*/
4807static int reset_prng_state(
4808 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
4809 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
4810 int objc, /* Number of arguments */
4811 Tcl_Obj *CONST objv[] /* Command arguments */
4812){
drh2fa18682008-03-19 14:15:34 +00004813 sqlite3_test_control(SQLITE_TESTCTRL_PRNG_RESET);
drh93aed5a2008-01-16 17:46:38 +00004814 return TCL_OK;
4815}
4816
danielk1977062d4cb2008-08-29 09:10:02 +00004817/*
4818** tclcmd: pcache_stats
4819*/
4820static int test_pcache_stats(
4821 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
4822 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
4823 int objc, /* Number of arguments */
4824 Tcl_Obj *CONST objv[] /* Command arguments */
4825){
4826 int nMin;
4827 int nMax;
4828 int nCurrent;
4829 int nRecyclable;
4830 Tcl_Obj *pRet;
4831
4832 sqlite3PcacheStats(&nCurrent, &nMax, &nMin, &nRecyclable);
4833
4834 pRet = Tcl_NewObj();
4835 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewStringObj("current", -1));
4836 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(nCurrent));
4837 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewStringObj("max", -1));
4838 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(nMax));
4839 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewStringObj("min", -1));
4840 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(nMin));
4841 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewStringObj("recyclable", -1));
4842 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(nRecyclable));
4843
4844 Tcl_SetObjResult(interp, pRet);
4845
4846 return TCL_OK;
4847}
4848
drh69910da2009-03-27 12:32:54 +00004849#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
danielk1977404ca072009-03-16 13:19:36 +00004850static void test_unlock_notify_cb(void **aArg, int nArg){
4851 int ii;
4852 for(ii=0; ii<nArg; ii++){
4853 Tcl_EvalEx((Tcl_Interp *)aArg[ii], "unlock_notify", -1, TCL_EVAL_GLOBAL);
4854 }
4855}
drh69910da2009-03-27 12:32:54 +00004856#endif /* SQLITE_ENABLE_UNLOCK_NOTIFY */
danielk1977404ca072009-03-16 13:19:36 +00004857
4858/*
4859** tclcmd: sqlite3_unlock_notify db
4860*/
4861#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
4862static int test_unlock_notify(
4863 ClientData clientData, /* Unused */
4864 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
4865 int objc, /* Number of arguments */
4866 Tcl_Obj *CONST objv[] /* Command arguments */
4867){
4868 sqlite3 *db;
4869 int rc;
4870
4871 if( objc!=2 ){
4872 Tcl_WrongNumArgs(interp, 1, objv, "DB");
4873 return TCL_ERROR;
4874 }
4875
4876 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
4877 return TCL_ERROR;
4878 }
4879 rc = sqlite3_unlock_notify(db, test_unlock_notify_cb, (void *)interp);
4880 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
4881 return TCL_OK;
4882}
4883#endif
4884
dan87c1fe12010-05-03 12:14:15 +00004885/*
4886** tclcmd: sqlite3_wal_checkpoint db ?NAME?
4887*/
4888static int test_wal_checkpoint(
4889 ClientData clientData, /* Unused */
4890 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
4891 int objc, /* Number of arguments */
4892 Tcl_Obj *CONST objv[] /* Command arguments */
4893){
4894 char *zDb = 0;
4895 sqlite3 *db;
4896 int rc;
4897
4898 if( objc!=3 && objc!=2 ){
4899 Tcl_WrongNumArgs(interp, 1, objv, "DB ?NAME?");
4900 return TCL_ERROR;
4901 }
4902
4903 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
4904 return TCL_ERROR;
4905 }
4906 if( objc==3 ){
4907 zDb = Tcl_GetString(objv[2]);
4908 }
4909 rc = sqlite3_wal_checkpoint(db, zDb);
4910 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
4911 return TCL_OK;
4912}
4913
drh9bc54492007-10-23 14:49:59 +00004914
4915/*
drha2c8a952009-10-13 18:38:34 +00004916** tcl_objproc COMMANDNAME ARGS...
4917**
4918** Run a TCL command using its objProc interface. Throw an error if
4919** the command has no objProc interface.
4920*/
4921static int runAsObjProc(
4922 void * clientData,
4923 Tcl_Interp *interp,
4924 int objc,
4925 Tcl_Obj *CONST objv[]
4926){
4927 Tcl_CmdInfo cmdInfo;
4928 if( objc<2 ){
4929 Tcl_WrongNumArgs(interp, 1, objv, "COMMAND ...");
4930 return TCL_ERROR;
4931 }
4932 if( !Tcl_GetCommandInfo(interp, Tcl_GetString(objv[1]), &cmdInfo) ){
4933 Tcl_AppendResult(interp, "command not found: ",
4934 Tcl_GetString(objv[1]), (char*)0);
4935 return TCL_ERROR;
4936 }
4937 if( cmdInfo.objProc==0 ){
4938 Tcl_AppendResult(interp, "command has no objProc: ",
4939 Tcl_GetString(objv[1]), (char*)0);
4940 return TCL_ERROR;
4941 }
4942 return cmdInfo.objProc(cmdInfo.objClientData, interp, objc-1, objv+1);
4943}
4944
4945
4946/*
drhd1bf3512001-04-07 15:24:33 +00004947** Register commands with the TCL interpreter.
4948*/
4949int Sqlitetest1_Init(Tcl_Interp *interp){
danielk19776f8a5032004-05-10 10:34:51 +00004950 extern int sqlite3_search_count;
dan0ff297e2009-09-25 17:03:14 +00004951 extern int sqlite3_found_count;
danielk19776f8a5032004-05-10 10:34:51 +00004952 extern int sqlite3_interrupt_count;
4953 extern int sqlite3_open_file_count;
drh6bf89572004-11-03 16:27:01 +00004954 extern int sqlite3_sort_count;
danielk19776f8a5032004-05-10 10:34:51 +00004955 extern int sqlite3_current_time;
drh84a2bf62010-03-05 13:41:06 +00004956#if SQLITE_OS_UNIX && defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
aswiftaebf4132008-11-21 00:10:35 +00004957 extern int sqlite3_hostid_num;
pweilbacheraabbed22008-11-21 23:35:02 +00004958#endif
drhae7e1512007-05-02 16:51:59 +00004959 extern int sqlite3_max_blobsize;
drh16a9b832007-05-05 18:39:25 +00004960 extern int sqlite3BtreeSharedCacheReport(void*,
4961 Tcl_Interp*,int,Tcl_Obj*CONST*);
drhc2eef3b2002-08-31 18:53:06 +00004962 static struct {
4963 char *zName;
4964 Tcl_CmdProc *xProc;
4965 } aCmd[] = {
drh27641702007-08-22 02:56:42 +00004966 { "db_enter", (Tcl_CmdProc*)db_enter },
4967 { "db_leave", (Tcl_CmdProc*)db_leave },
drhd3d39e92004-05-20 22:16:29 +00004968 { "sqlite3_mprintf_int", (Tcl_CmdProc*)sqlite3_mprintf_int },
drhe9707672004-06-25 01:10:48 +00004969 { "sqlite3_mprintf_int64", (Tcl_CmdProc*)sqlite3_mprintf_int64 },
drhc5cad1e2009-02-01 00:21:09 +00004970 { "sqlite3_mprintf_long", (Tcl_CmdProc*)sqlite3_mprintf_long },
drhd3d39e92004-05-20 22:16:29 +00004971 { "sqlite3_mprintf_str", (Tcl_CmdProc*)sqlite3_mprintf_str },
drhb3738b62007-03-31 15:02:49 +00004972 { "sqlite3_snprintf_str", (Tcl_CmdProc*)sqlite3_snprintf_str },
drhe29b1a02004-07-17 21:56:09 +00004973 { "sqlite3_mprintf_stronly", (Tcl_CmdProc*)sqlite3_mprintf_stronly},
drhd3d39e92004-05-20 22:16:29 +00004974 { "sqlite3_mprintf_double", (Tcl_CmdProc*)sqlite3_mprintf_double },
4975 { "sqlite3_mprintf_scaled", (Tcl_CmdProc*)sqlite3_mprintf_scaled },
drh63782852005-08-30 19:30:59 +00004976 { "sqlite3_mprintf_hexdouble", (Tcl_CmdProc*)sqlite3_mprintf_hexdouble},
drhd3d39e92004-05-20 22:16:29 +00004977 { "sqlite3_mprintf_z_test", (Tcl_CmdProc*)test_mprintf_z },
drh05a82982006-03-19 13:00:25 +00004978 { "sqlite3_mprintf_n_test", (Tcl_CmdProc*)test_mprintf_n },
drh68853902007-05-07 11:24:30 +00004979 { "sqlite3_snprintf_int", (Tcl_CmdProc*)test_snprintf_int },
drhd3d39e92004-05-20 22:16:29 +00004980 { "sqlite3_last_insert_rowid", (Tcl_CmdProc*)test_last_rowid },
4981 { "sqlite3_exec_printf", (Tcl_CmdProc*)test_exec_printf },
drh5bd98ae2009-01-07 18:24:03 +00004982 { "sqlite3_exec_hex", (Tcl_CmdProc*)test_exec_hex },
drhb62c3352006-11-23 09:39:16 +00004983 { "sqlite3_exec", (Tcl_CmdProc*)test_exec },
4984 { "sqlite3_exec_nr", (Tcl_CmdProc*)test_exec_nr },
shane8225f5a2008-07-31 02:05:04 +00004985#ifndef SQLITE_OMIT_GET_TABLE
drhd3d39e92004-05-20 22:16:29 +00004986 { "sqlite3_get_table_printf", (Tcl_CmdProc*)test_get_table_printf },
shane8225f5a2008-07-31 02:05:04 +00004987#endif
drhd3d39e92004-05-20 22:16:29 +00004988 { "sqlite3_close", (Tcl_CmdProc*)sqlite_test_close },
4989 { "sqlite3_create_function", (Tcl_CmdProc*)test_create_function },
4990 { "sqlite3_create_aggregate", (Tcl_CmdProc*)test_create_aggregate },
4991 { "sqlite_register_test_function", (Tcl_CmdProc*)test_register_func },
4992 { "sqlite_abort", (Tcl_CmdProc*)sqlite_abort },
drh25d65432004-07-22 15:02:25 +00004993 { "sqlite_bind", (Tcl_CmdProc*)test_bind },
4994 { "breakpoint", (Tcl_CmdProc*)test_breakpoint },
4995 { "sqlite3_key", (Tcl_CmdProc*)test_key },
4996 { "sqlite3_rekey", (Tcl_CmdProc*)test_rekey },
drhcacb2082005-01-11 15:28:33 +00004997 { "sqlite_set_magic", (Tcl_CmdProc*)sqlite_set_magic },
drhc5cdca62005-01-11 16:54:14 +00004998 { "sqlite3_interrupt", (Tcl_CmdProc*)test_interrupt },
drh3e1d8e62005-05-26 16:23:34 +00004999 { "sqlite_delete_function", (Tcl_CmdProc*)delete_function },
5000 { "sqlite_delete_collation", (Tcl_CmdProc*)delete_collation },
5001 { "sqlite3_get_autocommit", (Tcl_CmdProc*)get_autocommit },
drh79158e12005-09-06 21:40:45 +00005002 { "sqlite3_stack_used", (Tcl_CmdProc*)test_stack_used },
drh30867652006-07-06 10:59:57 +00005003 { "sqlite3_busy_timeout", (Tcl_CmdProc*)test_busy_timeout },
drh3c23a882007-01-09 14:01:13 +00005004 { "printf", (Tcl_CmdProc*)test_printf },
mlcreech3a00f902008-03-04 17:45:01 +00005005 { "sqlite3IoTrace", (Tcl_CmdProc*)test_io_trace },
drhc2eef3b2002-08-31 18:53:06 +00005006 };
danielk197751e3d8e2004-05-20 01:12:34 +00005007 static struct {
5008 char *zName;
5009 Tcl_ObjCmdProc *xProc;
danielk197704f2e682004-05-27 01:04:07 +00005010 void *clientData;
danielk197751e3d8e2004-05-20 01:12:34 +00005011 } aObjCmd[] = {
drhdddca282006-01-03 00:33:50 +00005012 { "sqlite3_connection_pointer", get_sqlite_pointer, 0 },
drh241db312004-06-22 12:46:53 +00005013 { "sqlite3_bind_int", test_bind_int, 0 },
drhb026e052007-05-02 01:34:31 +00005014 { "sqlite3_bind_zeroblob", test_bind_zeroblob, 0 },
drh241db312004-06-22 12:46:53 +00005015 { "sqlite3_bind_int64", test_bind_int64, 0 },
5016 { "sqlite3_bind_double", test_bind_double, 0 },
danielk197704f2e682004-05-27 01:04:07 +00005017 { "sqlite3_bind_null", test_bind_null ,0 },
5018 { "sqlite3_bind_text", test_bind_text ,0 },
5019 { "sqlite3_bind_text16", test_bind_text16 ,0 },
5020 { "sqlite3_bind_blob", test_bind_blob ,0 },
drh75f6a032004-07-15 14:15:00 +00005021 { "sqlite3_bind_parameter_count", test_bind_parameter_count, 0},
drh895d7472004-08-20 16:02:39 +00005022 { "sqlite3_bind_parameter_name", test_bind_parameter_name, 0},
drhfa6bc002004-09-07 16:19:52 +00005023 { "sqlite3_bind_parameter_index", test_bind_parameter_index, 0},
danielk1977600dd0b2005-01-20 01:14:23 +00005024 { "sqlite3_clear_bindings", test_clear_bindings, 0},
drhf9cb7f52006-06-27 20:06:44 +00005025 { "sqlite3_sleep", test_sleep, 0},
danielk197704f2e682004-05-27 01:04:07 +00005026 { "sqlite3_errcode", test_errcode ,0 },
drh99dfe5e2008-10-30 15:03:15 +00005027 { "sqlite3_extended_errcode", test_ex_errcode ,0 },
danielk197704f2e682004-05-27 01:04:07 +00005028 { "sqlite3_errmsg", test_errmsg ,0 },
5029 { "sqlite3_errmsg16", test_errmsg16 ,0 },
5030 { "sqlite3_open", test_open ,0 },
5031 { "sqlite3_open16", test_open16 ,0 },
danielk1977bc6ada42004-06-30 08:20:16 +00005032 { "sqlite3_complete16", test_complete16 ,0 },
danielk197704f2e682004-05-27 01:04:07 +00005033
5034 { "sqlite3_prepare", test_prepare ,0 },
5035 { "sqlite3_prepare16", test_prepare16 ,0 },
drhb900aaf2006-11-09 00:24:53 +00005036 { "sqlite3_prepare_v2", test_prepare_v2 ,0 },
drh4837f532008-05-23 14:49:49 +00005037 { "sqlite3_prepare_tkt3134", test_prepare_tkt3134, 0},
drhb900aaf2006-11-09 00:24:53 +00005038 { "sqlite3_prepare16_v2", test_prepare16_v2 ,0 },
danielk197704f2e682004-05-27 01:04:07 +00005039 { "sqlite3_finalize", test_finalize ,0 },
drhd1d38482008-10-07 23:46:38 +00005040 { "sqlite3_stmt_status", test_stmt_status ,0 },
danielk197704f2e682004-05-27 01:04:07 +00005041 { "sqlite3_reset", test_reset ,0 },
drhd89bd002005-01-22 03:03:54 +00005042 { "sqlite3_expired", test_expired ,0 },
drhf8db1bc2005-04-22 02:38:37 +00005043 { "sqlite3_transfer_bindings", test_transfer_bind ,0 },
danielk1977fbcd5852004-06-15 02:44:18 +00005044 { "sqlite3_changes", test_changes ,0 },
5045 { "sqlite3_step", test_step ,0 },
danielk1977404ca072009-03-16 13:19:36 +00005046 { "sqlite3_sql", test_sql ,0 },
drhbb5a9c32008-06-19 02:52:25 +00005047 { "sqlite3_next_stmt", test_next_stmt ,0 },
danielk197704f2e682004-05-27 01:04:07 +00005048
drhb4bc7052006-01-11 23:40:33 +00005049 { "sqlite3_release_memory", test_release_memory, 0},
5050 { "sqlite3_soft_heap_limit", test_soft_heap_limit, 0},
drhb4bc7052006-01-11 23:40:33 +00005051 { "sqlite3_thread_cleanup", test_thread_cleanup, 0},
drhc6ba55f2007-04-05 17:36:18 +00005052 { "sqlite3_pager_refcounts", test_pager_refcounts, 0},
drh6aafc292006-01-05 15:50:06 +00005053
drhc2e87a32006-06-27 15:16:14 +00005054 { "sqlite3_load_extension", test_load_extension, 0},
5055 { "sqlite3_enable_load_extension", test_enable_load, 0},
drh4ac285a2006-09-15 07:28:50 +00005056 { "sqlite3_extended_result_codes", test_extended_result_codes, 0},
drhb1a6c3c2008-03-20 16:30:17 +00005057 { "sqlite3_limit", test_limit, 0},
drhc2e87a32006-06-27 15:16:14 +00005058
drh93aed5a2008-01-16 17:46:38 +00005059 { "save_prng_state", save_prng_state, 0 },
5060 { "restore_prng_state", restore_prng_state, 0 },
5061 { "reset_prng_state", reset_prng_state, 0 },
drha2c8a952009-10-13 18:38:34 +00005062 { "tcl_objproc", runAsObjProc, 0 },
drh93aed5a2008-01-16 17:46:38 +00005063
danielk197704f2e682004-05-27 01:04:07 +00005064 /* sqlite3_column_*() API */
5065 { "sqlite3_column_count", test_column_count ,0 },
5066 { "sqlite3_data_count", test_data_count ,0 },
5067 { "sqlite3_column_type", test_column_type ,0 },
danielk1977ea61b2c2004-05-27 01:49:51 +00005068 { "sqlite3_column_blob", test_column_blob ,0 },
danielk197704f2e682004-05-27 01:04:07 +00005069 { "sqlite3_column_double", test_column_double ,0 },
5070 { "sqlite3_column_int64", test_column_int64 ,0 },
danielk197744a376f2008-08-12 15:04:58 +00005071 { "sqlite3_column_text", test_stmt_utf8, (void*)sqlite3_column_text },
5072 { "sqlite3_column_name", test_stmt_utf8, (void*)sqlite3_column_name },
5073 { "sqlite3_column_int", test_stmt_int, (void*)sqlite3_column_int },
5074 { "sqlite3_column_bytes", test_stmt_int, (void*)sqlite3_column_bytes},
drh3f913572008-03-22 01:07:17 +00005075#ifndef SQLITE_OMIT_DECLTYPE
danielk197744a376f2008-08-12 15:04:58 +00005076 { "sqlite3_column_decltype",test_stmt_utf8,(void*)sqlite3_column_decltype},
drh3f913572008-03-22 01:07:17 +00005077#endif
danielk19774b1ae992006-02-10 03:06:10 +00005078#ifdef SQLITE_ENABLE_COLUMN_METADATA
danielk197744a376f2008-08-12 15:04:58 +00005079{ "sqlite3_column_database_name",test_stmt_utf8,(void*)sqlite3_column_database_name},
5080{ "sqlite3_column_table_name",test_stmt_utf8,(void*)sqlite3_column_table_name},
5081{ "sqlite3_column_origin_name",test_stmt_utf8,(void*)sqlite3_column_origin_name},
danielk19774b1ae992006-02-10 03:06:10 +00005082#endif
danielk1977955de522006-02-10 02:27:42 +00005083
drh6c626082004-11-14 21:56:29 +00005084#ifndef SQLITE_OMIT_UTF16
danielk197744a376f2008-08-12 15:04:58 +00005085 { "sqlite3_column_bytes16", test_stmt_int, (void*)sqlite3_column_bytes16 },
5086 { "sqlite3_column_text16", test_stmt_utf16, (void*)sqlite3_column_text16},
5087 { "sqlite3_column_name16", test_stmt_utf16, (void*)sqlite3_column_name16},
drh7d9bd4e2006-02-16 18:16:36 +00005088 { "add_alignment_test_collations", add_alignment_test_collations, 0 },
drh3f913572008-03-22 01:07:17 +00005089#ifndef SQLITE_OMIT_DECLTYPE
danielk197744a376f2008-08-12 15:04:58 +00005090 { "sqlite3_column_decltype16",test_stmt_utf16,(void*)sqlite3_column_decltype16},
drh3f913572008-03-22 01:07:17 +00005091#endif
danielk19774b1ae992006-02-10 03:06:10 +00005092#ifdef SQLITE_ENABLE_COLUMN_METADATA
danielk1977955de522006-02-10 02:27:42 +00005093{"sqlite3_column_database_name16",
5094 test_stmt_utf16, sqlite3_column_database_name16},
danielk197744a376f2008-08-12 15:04:58 +00005095{"sqlite3_column_table_name16", test_stmt_utf16, (void*)sqlite3_column_table_name16},
5096{"sqlite3_column_origin_name16", test_stmt_utf16, (void*)sqlite3_column_origin_name16},
drh6c626082004-11-14 21:56:29 +00005097#endif
danielk19774b1ae992006-02-10 03:06:10 +00005098#endif
danielk1977a393c032007-05-07 14:58:53 +00005099 { "sqlite3_create_collation_v2", test_create_collation_v2, 0 },
danielk1977a9808b32007-05-07 09:32:45 +00005100 { "sqlite3_global_recover", test_global_recover, 0 },
5101 { "working_64bit_int", working_64bit_int, 0 },
drh9bc54492007-10-23 14:49:59 +00005102 { "vfs_unlink_test", vfs_unlink_test, 0 },
drhc8d75672008-07-08 02:12:37 +00005103 { "vfs_initfail_test", vfs_initfail_test, 0 },
drha2820972008-07-07 13:31:58 +00005104 { "vfs_unregister_all", vfs_unregister_all, 0 },
5105 { "vfs_reregister_all", vfs_reregister_all, 0 },
drh55176252008-01-22 14:50:16 +00005106 { "file_control_test", file_control_test, 0 },
aswiftaebf4132008-11-21 00:10:35 +00005107 { "file_control_lasterrno_test", file_control_lasterrno_test, 0 },
5108 { "file_control_lockproxy_test", file_control_lockproxy_test, 0 },
danielk1977e339d652008-06-28 11:23:00 +00005109 { "sqlite3_vfs_list", vfs_list, 0 },
danielk197704f2e682004-05-27 01:04:07 +00005110
danielk19779a1d0ab2004-06-01 14:09:28 +00005111 /* Functions from os.h */
drh5436dc22004-11-14 04:04:17 +00005112#ifndef SQLITE_OMIT_UTF16
danielk1977312d6b32004-06-29 13:18:23 +00005113 { "add_test_collate", test_collate, 0 },
5114 { "add_test_collate_needed", test_collate_needed, 0 },
5115 { "add_test_function", test_function, 0 },
drh5436dc22004-11-14 04:04:17 +00005116#endif
danielk1977312d6b32004-06-29 13:18:23 +00005117 { "sqlite3_test_errstr", test_errstr, 0 },
drh92febd92004-08-20 18:34:20 +00005118 { "tcl_variable_type", tcl_variable_type, 0 },
danielk1977aef0bf62005-12-30 16:28:01 +00005119#ifndef SQLITE_OMIT_SHARED_CACHE
drh6f7adc82006-01-11 21:41:20 +00005120 { "sqlite3_enable_shared_cache", test_enable_shared, 0 },
drh16a9b832007-05-05 18:39:25 +00005121 { "sqlite3_shared_cache_report", sqlite3BtreeSharedCacheReport, 0},
danielk1977aef0bf62005-12-30 16:28:01 +00005122#endif
danielk1977161fb792006-01-24 10:58:21 +00005123 { "sqlite3_libversion_number", test_libversion_number, 0 },
danielk1977deb802c2006-02-09 13:43:28 +00005124#ifdef SQLITE_ENABLE_COLUMN_METADATA
5125 { "sqlite3_table_column_metadata", test_table_column_metadata, 0 },
5126#endif
danielk1977dcbb5d32007-05-04 18:36:44 +00005127#ifndef SQLITE_OMIT_INCRBLOB
5128 { "sqlite3_blob_read", test_blob_read, 0 },
5129 { "sqlite3_blob_write", test_blob_write, 0 },
5130#endif
danielk1977062d4cb2008-08-29 09:10:02 +00005131 { "pcache_stats", test_pcache_stats, 0 },
danielk1977404ca072009-03-16 13:19:36 +00005132#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
5133 { "sqlite3_unlock_notify", test_unlock_notify, 0 },
5134#endif
dan87c1fe12010-05-03 12:14:15 +00005135 { "sqlite3_wal_checkpoint", test_wal_checkpoint, 0 },
danielk197751e3d8e2004-05-20 01:12:34 +00005136 };
drh1398ad32005-01-19 23:24:50 +00005137 static int bitmask_size = sizeof(Bitmask)*8;
drhc2eef3b2002-08-31 18:53:06 +00005138 int i;
drhb851b2c2005-03-10 14:11:12 +00005139 extern int sqlite3_sync_count, sqlite3_fullsync_count;
drhaf6df112005-06-07 02:12:30 +00005140 extern int sqlite3_opentemp_count;
drh55ef4d92005-08-14 01:20:37 +00005141 extern int sqlite3_like_count;
drhdd735212007-02-24 13:53:05 +00005142 extern int sqlite3_xferopt_count;
drh538f5702007-04-13 02:14:30 +00005143 extern int sqlite3_pager_readdb_count;
5144 extern int sqlite3_pager_writedb_count;
5145 extern int sqlite3_pager_writej_count;
danielk197729bafea2008-06-26 10:41:19 +00005146#if SQLITE_OS_WIN
drhc0929982005-09-05 19:08:29 +00005147 extern int sqlite3_os_type;
5148#endif
drh8b3d9902005-08-19 00:14:42 +00005149#ifdef SQLITE_DEBUG
mlcreech3a00f902008-03-04 17:45:01 +00005150 extern int sqlite3WhereTrace;
5151 extern int sqlite3OSTrace;
5152 extern int sqlite3VdbeAddopTrace;
drhc74c3332010-05-31 12:15:19 +00005153 extern int sqlite3WalTrace;
drh549c8b62005-09-19 13:15:23 +00005154#endif
5155#ifdef SQLITE_TEST
5156 extern char sqlite3_query_plan[];
drh9042f392005-07-15 23:24:23 +00005157 static char *query_plan = sqlite3_query_plan;
danielk197733e89032008-12-17 15:18:17 +00005158#ifdef SQLITE_ENABLE_FTS3
5159 extern int sqlite3_fts3_enable_parentheses;
5160#endif
drh48083ce2005-09-19 12:37:27 +00005161#endif
drhc2eef3b2002-08-31 18:53:06 +00005162
5163 for(i=0; i<sizeof(aCmd)/sizeof(aCmd[0]); i++){
5164 Tcl_CreateCommand(interp, aCmd[i].zName, aCmd[i].xProc, 0, 0);
5165 }
danielk197751e3d8e2004-05-20 01:12:34 +00005166 for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){
danielk1977c572ef72004-05-27 09:28:41 +00005167 Tcl_CreateObjCommand(interp, aObjCmd[i].zName,
5168 aObjCmd[i].xProc, aObjCmd[i].clientData, 0);
danielk197751e3d8e2004-05-20 01:12:34 +00005169 }
danielk19776490beb2004-05-11 06:17:21 +00005170 Tcl_LinkVar(interp, "sqlite_search_count",
danielk19776f8a5032004-05-10 10:34:51 +00005171 (char*)&sqlite3_search_count, TCL_LINK_INT);
dan0ff297e2009-09-25 17:03:14 +00005172 Tcl_LinkVar(interp, "sqlite_found_count",
5173 (char*)&sqlite3_found_count, TCL_LINK_INT);
drh6bf89572004-11-03 16:27:01 +00005174 Tcl_LinkVar(interp, "sqlite_sort_count",
5175 (char*)&sqlite3_sort_count, TCL_LINK_INT);
drhae7e1512007-05-02 16:51:59 +00005176 Tcl_LinkVar(interp, "sqlite3_max_blobsize",
5177 (char*)&sqlite3_max_blobsize, TCL_LINK_INT);
drh55ef4d92005-08-14 01:20:37 +00005178 Tcl_LinkVar(interp, "sqlite_like_count",
5179 (char*)&sqlite3_like_count, TCL_LINK_INT);
danielk19776490beb2004-05-11 06:17:21 +00005180 Tcl_LinkVar(interp, "sqlite_interrupt_count",
danielk19776f8a5032004-05-10 10:34:51 +00005181 (char*)&sqlite3_interrupt_count, TCL_LINK_INT);
danielk19776490beb2004-05-11 06:17:21 +00005182 Tcl_LinkVar(interp, "sqlite_open_file_count",
danielk19776f8a5032004-05-10 10:34:51 +00005183 (char*)&sqlite3_open_file_count, TCL_LINK_INT);
danielk19776490beb2004-05-11 06:17:21 +00005184 Tcl_LinkVar(interp, "sqlite_current_time",
danielk19776f8a5032004-05-10 10:34:51 +00005185 (char*)&sqlite3_current_time, TCL_LINK_INT);
drh84a2bf62010-03-05 13:41:06 +00005186#if SQLITE_OS_UNIX && defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
aswiftaebf4132008-11-21 00:10:35 +00005187 Tcl_LinkVar(interp, "sqlite_hostid_num",
5188 (char*)&sqlite3_hostid_num, TCL_LINK_INT);
pweilbacheraabbed22008-11-21 23:35:02 +00005189#endif
drhdd735212007-02-24 13:53:05 +00005190 Tcl_LinkVar(interp, "sqlite3_xferopt_count",
5191 (char*)&sqlite3_xferopt_count, TCL_LINK_INT);
drh538f5702007-04-13 02:14:30 +00005192 Tcl_LinkVar(interp, "sqlite3_pager_readdb_count",
5193 (char*)&sqlite3_pager_readdb_count, TCL_LINK_INT);
5194 Tcl_LinkVar(interp, "sqlite3_pager_writedb_count",
5195 (char*)&sqlite3_pager_writedb_count, TCL_LINK_INT);
5196 Tcl_LinkVar(interp, "sqlite3_pager_writej_count",
5197 (char*)&sqlite3_pager_writej_count, TCL_LINK_INT);
danielk19774b2688a2006-06-20 11:01:07 +00005198#ifndef SQLITE_OMIT_UTF16
drh7d9bd4e2006-02-16 18:16:36 +00005199 Tcl_LinkVar(interp, "unaligned_string_counter",
5200 (char*)&unaligned_string_counter, TCL_LINK_INT);
danielk19774b2688a2006-06-20 11:01:07 +00005201#endif
drh268803a2005-12-14 20:11:30 +00005202#ifndef SQLITE_OMIT_UTF16
5203 Tcl_LinkVar(interp, "sqlite_last_needed_collation",
5204 (char*)&pzNeededCollation, TCL_LINK_STRING|TCL_LINK_READ_ONLY);
5205#endif
danielk197729bafea2008-06-26 10:41:19 +00005206#if SQLITE_OS_WIN
drhc0929982005-09-05 19:08:29 +00005207 Tcl_LinkVar(interp, "sqlite_os_type",
5208 (char*)&sqlite3_os_type, TCL_LINK_INT);
5209#endif
drh549c8b62005-09-19 13:15:23 +00005210#ifdef SQLITE_TEST
5211 Tcl_LinkVar(interp, "sqlite_query_plan",
5212 (char*)&query_plan, TCL_LINK_STRING|TCL_LINK_READ_ONLY);
5213#endif
drh8b3d9902005-08-19 00:14:42 +00005214#ifdef SQLITE_DEBUG
5215 Tcl_LinkVar(interp, "sqlite_addop_trace",
mlcreech3a00f902008-03-04 17:45:01 +00005216 (char*)&sqlite3VdbeAddopTrace, TCL_LINK_INT);
drh48083ce2005-09-19 12:37:27 +00005217 Tcl_LinkVar(interp, "sqlite_where_trace",
mlcreech3a00f902008-03-04 17:45:01 +00005218 (char*)&sqlite3WhereTrace, TCL_LINK_INT);
drh73be5012007-08-08 12:11:21 +00005219 Tcl_LinkVar(interp, "sqlite_os_trace",
mlcreech3a00f902008-03-04 17:45:01 +00005220 (char*)&sqlite3OSTrace, TCL_LINK_INT);
dan38e1a272010-06-28 11:23:09 +00005221#ifndef SQLITE_OMIT_WAL
drhc74c3332010-05-31 12:15:19 +00005222 Tcl_LinkVar(interp, "sqlite_wal_trace",
5223 (char*)&sqlite3WalTrace, TCL_LINK_INT);
drh8b3d9902005-08-19 00:14:42 +00005224#endif
dan38e1a272010-06-28 11:23:09 +00005225#endif
danielk1977cbe21be2005-06-07 07:58:48 +00005226#ifndef SQLITE_OMIT_DISKIO
drhaf6df112005-06-07 02:12:30 +00005227 Tcl_LinkVar(interp, "sqlite_opentemp_count",
5228 (char*)&sqlite3_opentemp_count, TCL_LINK_INT);
danielk1977cbe21be2005-06-07 07:58:48 +00005229#endif
drh7c972de2003-09-06 22:18:07 +00005230 Tcl_LinkVar(interp, "sqlite_static_bind_value",
5231 (char*)&sqlite_static_bind_value, TCL_LINK_STRING);
drhf0313812006-09-04 15:53:53 +00005232 Tcl_LinkVar(interp, "sqlite_static_bind_nbyte",
5233 (char*)&sqlite_static_bind_nbyte, TCL_LINK_INT);
drhab3f9fe2004-08-14 17:10:10 +00005234 Tcl_LinkVar(interp, "sqlite_temp_directory",
drheffd02b2004-08-29 23:42:13 +00005235 (char*)&sqlite3_temp_directory, TCL_LINK_STRING);
drh1398ad32005-01-19 23:24:50 +00005236 Tcl_LinkVar(interp, "bitmask_size",
5237 (char*)&bitmask_size, TCL_LINK_INT|TCL_LINK_READ_ONLY);
drhb851b2c2005-03-10 14:11:12 +00005238 Tcl_LinkVar(interp, "sqlite_sync_count",
5239 (char*)&sqlite3_sync_count, TCL_LINK_INT);
5240 Tcl_LinkVar(interp, "sqlite_fullsync_count",
5241 (char*)&sqlite3_fullsync_count, TCL_LINK_INT);
drhd1fa7bc2009-01-10 13:24:50 +00005242#if defined(SQLITE_ENABLE_FTS3) && defined(SQLITE_TEST)
danielk197733e89032008-12-17 15:18:17 +00005243 Tcl_LinkVar(interp, "sqlite_fts3_enable_parentheses",
5244 (char*)&sqlite3_fts3_enable_parentheses, TCL_LINK_INT);
5245#endif
drhd1bf3512001-04-07 15:24:33 +00005246 return TCL_OK;
5247}