blob: 69f5112d74dbc823d423ad9a0ed3a90c4697d4fa [file] [log] [blame]
drhd1bf3512001-04-07 15:24:33 +00001/*
drhb19a2bc2001-09-16 00:13:26 +00002** 2001 September 15
drhd1bf3512001-04-07 15:24:33 +00003**
drhb19a2bc2001-09-16 00:13:26 +00004** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
drhd1bf3512001-04-07 15:24:33 +00006**
drhb19a2bc2001-09-16 00:13:26 +00007** May you do good and not evil.
8** May you find forgiveness for yourself and forgive others.
9** May you share freely, never taking more than you give.
drhd1bf3512001-04-07 15:24:33 +000010**
11*************************************************************************
drh05a82982006-03-19 13:00:25 +000012** Code for testing all sorts of SQLite interfaces. This code
drhd1bf3512001-04-07 15:24:33 +000013** is not included in the SQLite library. It is used for automated
14** testing of the SQLite library.
15**
drh68853902007-05-07 11:24:30 +000016** $Id: test1.c,v 1.249 2007/05/07 11:24:30 drh Exp $
drhd1bf3512001-04-07 15:24:33 +000017*/
18#include "sqliteInt.h"
19#include "tcl.h"
drh94e92032003-02-16 22:21:32 +000020#include "os.h"
drhd1bf3512001-04-07 15:24:33 +000021#include <stdlib.h>
22#include <string.h>
23
drhdddca282006-01-03 00:33:50 +000024/*
25** This is a copy of the first part of the SqliteDb structure in
26** tclsqlite.c. We need it here so that the get_sqlite_pointer routine
27** can extract the sqlite3* pointer from an existing Tcl SQLite
28** connection.
29*/
30struct SqliteDb {
31 sqlite3 *db;
32};
33
34/*
drha3152892007-05-05 11:48:52 +000035** Convert text generated by the "%p" conversion format back into
36** a pointer.
37*/
38static int testHexToInt(int h){
39 if( h>='0' && h<='9' ){
40 return h - '0';
41 }else if( h>='a' && h<='f' ){
42 return h - 'a' + 10;
43 }else{
44 assert( h>='A' && h<='F' );
45 return h - 'A' + 10;
46 }
47}
48void *sqlite3TextToPtr(const char *z){
49 void *p;
50 u64 v;
51 u32 v2;
52 if( z[0]=='0' && z[1]=='x' ){
53 z += 2;
54 }
55 v = 0;
56 while( *z ){
57 v = (v<<4) + testHexToInt(*z);
58 z++;
59 }
60 if( sizeof(p)==sizeof(v) ){
61 memcpy(&p, &v, sizeof(p));
62 }else{
63 assert( sizeof(p)==sizeof(v2) );
64 v2 = (u32)v;
65 memcpy(&p, &v2, sizeof(p));
66 }
67 return p;
68}
69
70
71/*
drhdddca282006-01-03 00:33:50 +000072** A TCL command that returns the address of the sqlite* pointer
73** for an sqlite connection instance. Bad things happen if the
74** input is not an sqlite connection.
75*/
76static int get_sqlite_pointer(
77 void * clientData,
78 Tcl_Interp *interp,
79 int objc,
80 Tcl_Obj *CONST objv[]
81){
82 struct SqliteDb *p;
83 Tcl_CmdInfo cmdInfo;
84 char zBuf[100];
85 if( objc!=2 ){
86 Tcl_WrongNumArgs(interp, 1, objv, "SQLITE-CONNECTION");
87 return TCL_ERROR;
88 }
89 if( !Tcl_GetCommandInfo(interp, Tcl_GetString(objv[1]), &cmdInfo) ){
90 Tcl_AppendResult(interp, "command not found: ",
91 Tcl_GetString(objv[1]), (char*)0);
92 return TCL_ERROR;
93 }
94 p = (struct SqliteDb*)cmdInfo.objClientData;
95 sprintf(zBuf, "%p", p->db);
96 if( strncmp(zBuf,"0x",2) ){
97 sprintf(zBuf, "0x%p", p->db);
98 }
99 Tcl_AppendResult(interp, zBuf, 0);
100 return TCL_OK;
101}
102
drhb62c3352006-11-23 09:39:16 +0000103/*
104** Decode a pointer to an sqlite3 object.
105*/
106static int getDbPointer(Tcl_Interp *interp, const char *zA, sqlite3 **ppDb){
107 struct SqliteDb *p;
108 Tcl_CmdInfo cmdInfo;
109 if( Tcl_GetCommandInfo(interp, zA, &cmdInfo) ){
110 p = (struct SqliteDb*)cmdInfo.objClientData;
111 *ppDb = p->db;
112 }else{
113 *ppDb = (sqlite3*)sqlite3TextToPtr(zA);
114 }
115 return TCL_OK;
116}
117
118
drh2e66f0b2005-04-28 17:18:48 +0000119const char *sqlite3TestErrorName(int rc){
danielk19776622cce2004-05-20 11:00:52 +0000120 const char *zName = 0;
drh4ac285a2006-09-15 07:28:50 +0000121 switch( rc & 0xff ){
danielk19776622cce2004-05-20 11:00:52 +0000122 case SQLITE_OK: zName = "SQLITE_OK"; break;
123 case SQLITE_ERROR: zName = "SQLITE_ERROR"; break;
danielk19776622cce2004-05-20 11:00:52 +0000124 case SQLITE_PERM: zName = "SQLITE_PERM"; break;
125 case SQLITE_ABORT: zName = "SQLITE_ABORT"; break;
126 case SQLITE_BUSY: zName = "SQLITE_BUSY"; break;
127 case SQLITE_LOCKED: zName = "SQLITE_LOCKED"; break;
128 case SQLITE_NOMEM: zName = "SQLITE_NOMEM"; break;
129 case SQLITE_READONLY: zName = "SQLITE_READONLY"; break;
130 case SQLITE_INTERRUPT: zName = "SQLITE_INTERRUPT"; break;
131 case SQLITE_IOERR: zName = "SQLITE_IOERR"; break;
132 case SQLITE_CORRUPT: zName = "SQLITE_CORRUPT"; break;
danielk19776622cce2004-05-20 11:00:52 +0000133 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;
danielk19776622cce2004-05-20 11:00:52 +0000138 case SQLITE_CONSTRAINT: zName = "SQLITE_CONSTRAINT"; break;
139 case SQLITE_MISMATCH: zName = "SQLITE_MISMATCH"; break;
140 case SQLITE_MISUSE: zName = "SQLITE_MISUSE"; break;
141 case SQLITE_NOLFS: zName = "SQLITE_NOLFS"; break;
142 case SQLITE_AUTH: zName = "SQLITE_AUTH"; break;
143 case SQLITE_FORMAT: zName = "SQLITE_FORMAT"; break;
144 case SQLITE_RANGE: zName = "SQLITE_RANGE"; break;
145 case SQLITE_ROW: zName = "SQLITE_ROW"; break;
146 case SQLITE_DONE: zName = "SQLITE_DONE"; break;
drhc60d0442004-09-30 13:43:13 +0000147 case SQLITE_NOTADB: zName = "SQLITE_NOTADB"; break;
danielk19776622cce2004-05-20 11:00:52 +0000148 default: zName = "SQLITE_Unknown"; break;
149 }
150 return zName;
151}
drh4f0c5872007-03-26 22:05:01 +0000152#define t1ErrorName sqlite3TestErrorName
danielk19776622cce2004-05-20 11:00:52 +0000153
drhd1bf3512001-04-07 15:24:33 +0000154/*
drhc60d0442004-09-30 13:43:13 +0000155** Convert an sqlite3_stmt* into an sqlite3*. This depends on the
156** fact that the sqlite3* is the first field in the Vdbe structure.
157*/
drh51942bc2005-06-12 22:01:42 +0000158#define StmtToDb(X) sqlite3_db_handle(X)
drhc60d0442004-09-30 13:43:13 +0000159
160/*
161** Check a return value to make sure it agrees with the results
162** from sqlite3_errcode.
163*/
164int sqlite3TestErrCode(Tcl_Interp *interp, sqlite3 *db, int rc){
165 if( rc!=SQLITE_MISUSE && rc!=SQLITE_OK && sqlite3_errcode(db)!=rc ){
166 char zBuf[200];
167 int r2 = sqlite3_errcode(db);
168 sprintf(zBuf, "error code %s (%d) does not match sqlite3_errcode %s (%d)",
drh4f0c5872007-03-26 22:05:01 +0000169 t1ErrorName(rc), rc, t1ErrorName(r2), r2);
drhc60d0442004-09-30 13:43:13 +0000170 Tcl_ResetResult(interp);
171 Tcl_AppendResult(interp, zBuf, 0);
172 return 1;
173 }
174 return 0;
175}
176
177/*
danielk197751e3d8e2004-05-20 01:12:34 +0000178** Decode a pointer to an sqlite3_stmt object.
179*/
180static int getStmtPointer(
181 Tcl_Interp *interp,
182 const char *zArg,
183 sqlite3_stmt **ppStmt
184){
drhfe63d1c2004-09-08 20:13:04 +0000185 *ppStmt = (sqlite3_stmt*)sqlite3TextToPtr(zArg);
danielk197751e3d8e2004-05-20 01:12:34 +0000186 return TCL_OK;
187}
188
189/*
danielk19779a1d0ab2004-06-01 14:09:28 +0000190** Decode a pointer to an sqlite3_stmt object.
191*/
192static int getFilePointer(
193 Tcl_Interp *interp,
194 const char *zArg,
195 OsFile **ppFile
196){
drhfe63d1c2004-09-08 20:13:04 +0000197 *ppFile = (OsFile*)sqlite3TextToPtr(zArg);
danielk19779a1d0ab2004-06-01 14:09:28 +0000198 return TCL_OK;
199}
200
201/*
drh7d8085a2003-04-26 13:19:38 +0000202** Generate a text representation of a pointer that can be understood
203** by the getDbPointer and getVmPointer routines above.
204**
205** The problem is, on some machines (Solaris) if you do a printf with
206** "%p" you cannot turn around and do a scanf with the same "%p" and
207** get your pointer back. You have to prepend a "0x" before it will
208** work. Or at least that is what is reported to me (drh). But this
209** behavior varies from machine to machine. The solution used her is
210** to test the string right after it is generated to see if it can be
211** understood by scanf, and if not, try prepending an "0x" to see if
212** that helps. If nothing works, a fatal error is generated.
213*/
drh64b1bea2006-01-15 02:30:57 +0000214int sqlite3TestMakePointerStr(Tcl_Interp *interp, char *zPtr, void *p){
drhfe63d1c2004-09-08 20:13:04 +0000215 sqlite3_snprintf(100, zPtr, "%p", p);
drh7d8085a2003-04-26 13:19:38 +0000216 return TCL_OK;
217}
218
219/*
danielk19776f8a5032004-05-10 10:34:51 +0000220** The callback routine for sqlite3_exec_printf().
drhd1bf3512001-04-07 15:24:33 +0000221*/
222static int exec_printf_cb(void *pArg, int argc, char **argv, char **name){
223 Tcl_DString *str = (Tcl_DString*)pArg;
224 int i;
225
226 if( Tcl_DStringLength(str)==0 ){
227 for(i=0; i<argc; i++){
228 Tcl_DStringAppendElement(str, name[i] ? name[i] : "NULL");
229 }
230 }
231 for(i=0; i<argc; i++){
232 Tcl_DStringAppendElement(str, argv[i] ? argv[i] : "NULL");
233 }
234 return 0;
235}
236
237/*
drh538f5702007-04-13 02:14:30 +0000238** The I/O tracing callback.
239*/
240static FILE *iotrace_file = 0;
241static void io_trace_callback(const char *zFormat, ...){
242 va_list ap;
243 va_start(ap, zFormat);
244 vfprintf(iotrace_file, zFormat, ap);
245 va_end(ap);
246 fflush(iotrace_file);
247}
248
249/*
250** Usage: io_trace FILENAME
251**
252** Turn I/O tracing on or off. If FILENAME is not an empty string,
253** I/O tracing begins going into FILENAME. If FILENAME is an empty
254** string, I/O tracing is turned off.
255*/
256static int test_io_trace(
257 void *NotUsed,
258 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
259 int argc, /* Number of arguments */
260 char **argv /* Text of each argument */
261){
262 if( argc!=2 ){
263 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
264 " FILENAME\"", 0);
265 return TCL_ERROR;
266 }
267 if( iotrace_file ){
268 if( iotrace_file!=stdout && iotrace_file!=stderr ){
269 fclose(iotrace_file);
270 }
271 iotrace_file = 0;
272 sqlite3_io_trace = 0;
273 }
274 if( argv[1][0] ){
275 if( strcmp(argv[1],"stdout")==0 ){
276 iotrace_file = stdout;
277 }else if( strcmp(argv[1],"stderr")==0 ){
278 iotrace_file = stderr;
279 }else{
280 iotrace_file = fopen(argv[1], "w");
281 }
282 sqlite3_io_trace = io_trace_callback;
283 }
284 return SQLITE_OK;
285}
286
287
288/*
danielk19776f8a5032004-05-10 10:34:51 +0000289** Usage: sqlite3_exec_printf DB FORMAT STRING
drhd1bf3512001-04-07 15:24:33 +0000290**
danielk19776f8a5032004-05-10 10:34:51 +0000291** Invoke the sqlite3_exec_printf() interface using the open database
drhd1bf3512001-04-07 15:24:33 +0000292** DB. The SQL is the string FORMAT. The format string should contain
293** one %s or %q. STRING is the value inserted into %s or %q.
294*/
295static int test_exec_printf(
296 void *NotUsed,
297 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
298 int argc, /* Number of arguments */
299 char **argv /* Text of each argument */
300){
drh9bb575f2004-09-06 17:24:11 +0000301 sqlite3 *db;
drhd1bf3512001-04-07 15:24:33 +0000302 Tcl_DString str;
303 int rc;
304 char *zErr = 0;
drh1211de32004-07-26 12:24:22 +0000305 char *zSql;
drhd1bf3512001-04-07 15:24:33 +0000306 char zBuf[30];
307 if( argc!=4 ){
308 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
309 " DB FORMAT STRING", 0);
310 return TCL_ERROR;
311 }
drhb86ccfb2003-01-28 23:13:10 +0000312 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
drhd1bf3512001-04-07 15:24:33 +0000313 Tcl_DStringInit(&str);
drh1211de32004-07-26 12:24:22 +0000314 zSql = sqlite3_mprintf(argv[2], argv[3]);
315 rc = sqlite3_exec(db, zSql, exec_printf_cb, &str, &zErr);
316 sqlite3_free(zSql);
drhd1bf3512001-04-07 15:24:33 +0000317 sprintf(zBuf, "%d", rc);
318 Tcl_AppendElement(interp, zBuf);
319 Tcl_AppendElement(interp, rc==SQLITE_OK ? Tcl_DStringValue(&str) : zErr);
320 Tcl_DStringFree(&str);
danielk1977926aab22006-06-27 07:34:40 +0000321 if( zErr ) sqlite3_free(zErr);
drhc60d0442004-09-30 13:43:13 +0000322 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
drhd1bf3512001-04-07 15:24:33 +0000323 return TCL_OK;
324}
325
326/*
drhb62c3352006-11-23 09:39:16 +0000327** Usage: sqlite3_exec DB SQL
328**
329** Invoke the sqlite3_exec interface using the open database DB
330*/
331static int test_exec(
332 void *NotUsed,
333 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
334 int argc, /* Number of arguments */
335 char **argv /* Text of each argument */
336){
337 sqlite3 *db;
338 Tcl_DString str;
339 int rc;
340 char *zErr = 0;
341 char zBuf[30];
342 if( argc!=3 ){
343 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
344 " DB SQL", 0);
345 return TCL_ERROR;
346 }
347 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
348 Tcl_DStringInit(&str);
349 rc = sqlite3_exec(db, argv[2], exec_printf_cb, &str, &zErr);
350 sprintf(zBuf, "%d", rc);
351 Tcl_AppendElement(interp, zBuf);
352 Tcl_AppendElement(interp, rc==SQLITE_OK ? Tcl_DStringValue(&str) : zErr);
353 Tcl_DStringFree(&str);
354 if( zErr ) sqlite3_free(zErr);
355 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
356 return TCL_OK;
357}
358
359/*
360** Usage: sqlite3_exec_nr DB SQL
361**
362** Invoke the sqlite3_exec interface using the open database DB. Discard
363** all results
364*/
365static int test_exec_nr(
366 void *NotUsed,
367 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
368 int argc, /* Number of arguments */
369 char **argv /* Text of each argument */
370){
371 sqlite3 *db;
372 int rc;
373 char *zErr = 0;
374 if( argc!=3 ){
375 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
376 " DB SQL", 0);
377 return TCL_ERROR;
378 }
379 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
380 rc = sqlite3_exec(db, argv[2], 0, 0, &zErr);
381 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
382 return TCL_OK;
383}
384
385/*
danielk19776f8a5032004-05-10 10:34:51 +0000386** Usage: sqlite3_mprintf_z_test SEPARATOR ARG0 ARG1 ...
drhd93d8a82003-06-16 03:08:18 +0000387**
drh05a82982006-03-19 13:00:25 +0000388** Test the %z format of sqliteMPrintf(). Use multiple mprintf() calls to
drhd93d8a82003-06-16 03:08:18 +0000389** concatenate arg0 through argn using separator as the separator.
390** Return the result.
391*/
392static int test_mprintf_z(
393 void *NotUsed,
394 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
395 int argc, /* Number of arguments */
396 char **argv /* Text of each argument */
397){
398 char *zResult = 0;
399 int i;
400
401 for(i=2; i<argc; i++){
danielk19774adee202004-05-08 08:23:19 +0000402 zResult = sqlite3MPrintf("%z%s%s", zResult, argv[1], argv[i]);
drhd93d8a82003-06-16 03:08:18 +0000403 }
404 Tcl_AppendResult(interp, zResult, 0);
drh5f968432004-02-21 19:02:30 +0000405 sqliteFree(zResult);
drhd93d8a82003-06-16 03:08:18 +0000406 return TCL_OK;
407}
408
409/*
drh05a82982006-03-19 13:00:25 +0000410** Usage: sqlite3_mprintf_n_test STRING
411**
412** Test the %n format of sqliteMPrintf(). Return the length of the
413** input string.
414*/
415static int test_mprintf_n(
416 void *NotUsed,
417 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
418 int argc, /* Number of arguments */
419 char **argv /* Text of each argument */
420){
421 char *zStr;
422 int n = 0;
423 zStr = sqlite3MPrintf("%s%n", argv[1], &n);
424 sqliteFree(zStr);
425 Tcl_SetObjResult(interp, Tcl_NewIntObj(n));
426 return TCL_OK;
427}
428
429/*
drh68853902007-05-07 11:24:30 +0000430** Usage: sqlite3_snprintf_int SIZE FORMAT INT
431**
432** Test the of sqlite3_snprintf() routine. SIZE is the size of the
433** output buffer in bytes. The maximum size is 100. FORMAT is the
434** format string. INT is a single integer argument. The FORMAT
435** string must require no more than this one integer argument. If
436** You pass in a format string that requires more than one argument,
437** bad things will happen.
438*/
439static int test_snprintf_int(
440 void *NotUsed,
441 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
442 int argc, /* Number of arguments */
443 char **argv /* Text of each argument */
444){
445 char zStr[100];
446 int n = atoi(argv[1]);
447 if( n>sizeof(zStr) ) n = sizeof(zStr);
448 const char *zFormat = argv[2];
449 int a1 = atoi(argv[3]);
450 strcpy(zStr, "abcdefghijklmnopqrstuvwxyz");
451 sqlite3_snprintf(n, zStr, zFormat, a1);
452 Tcl_AppendResult(interp, zStr, 0);
453 return TCL_OK;
454}
455
456/*
danielk19776f8a5032004-05-10 10:34:51 +0000457** Usage: sqlite3_get_table_printf DB FORMAT STRING
drhd1bf3512001-04-07 15:24:33 +0000458**
danielk19776f8a5032004-05-10 10:34:51 +0000459** Invoke the sqlite3_get_table_printf() interface using the open database
drhd1bf3512001-04-07 15:24:33 +0000460** DB. The SQL is the string FORMAT. The format string should contain
461** one %s or %q. STRING is the value inserted into %s or %q.
462*/
463static int test_get_table_printf(
464 void *NotUsed,
465 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
466 int argc, /* Number of arguments */
467 char **argv /* Text of each argument */
468){
drh9bb575f2004-09-06 17:24:11 +0000469 sqlite3 *db;
drhd1bf3512001-04-07 15:24:33 +0000470 Tcl_DString str;
471 int rc;
472 char *zErr = 0;
473 int nRow, nCol;
474 char **aResult;
475 int i;
476 char zBuf[30];
drh1211de32004-07-26 12:24:22 +0000477 char *zSql;
drhd1bf3512001-04-07 15:24:33 +0000478 if( argc!=4 ){
479 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
480 " DB FORMAT STRING", 0);
481 return TCL_ERROR;
482 }
drhb86ccfb2003-01-28 23:13:10 +0000483 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
drhd1bf3512001-04-07 15:24:33 +0000484 Tcl_DStringInit(&str);
drh1211de32004-07-26 12:24:22 +0000485 zSql = sqlite3_mprintf(argv[2],argv[3]);
486 rc = sqlite3_get_table(db, zSql, &aResult, &nRow, &nCol, &zErr);
487 sqlite3_free(zSql);
drhd1bf3512001-04-07 15:24:33 +0000488 sprintf(zBuf, "%d", rc);
489 Tcl_AppendElement(interp, zBuf);
490 if( rc==SQLITE_OK ){
491 sprintf(zBuf, "%d", nRow);
492 Tcl_AppendElement(interp, zBuf);
493 sprintf(zBuf, "%d", nCol);
494 Tcl_AppendElement(interp, zBuf);
495 for(i=0; i<(nRow+1)*nCol; i++){
496 Tcl_AppendElement(interp, aResult[i] ? aResult[i] : "NULL");
497 }
498 }else{
499 Tcl_AppendElement(interp, zErr);
500 }
danielk19776f8a5032004-05-10 10:34:51 +0000501 sqlite3_free_table(aResult);
danielk1977926aab22006-06-27 07:34:40 +0000502 if( zErr ) sqlite3_free(zErr);
drhc60d0442004-09-30 13:43:13 +0000503 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
drhd1bf3512001-04-07 15:24:33 +0000504 return TCL_OK;
505}
506
drhaf9ff332002-01-16 21:00:27 +0000507
508/*
danielk19776f8a5032004-05-10 10:34:51 +0000509** Usage: sqlite3_last_insert_rowid DB
drhaf9ff332002-01-16 21:00:27 +0000510**
511** Returns the integer ROWID of the most recent insert.
512*/
513static int test_last_rowid(
514 void *NotUsed,
515 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
516 int argc, /* Number of arguments */
517 char **argv /* Text of each argument */
518){
drh9bb575f2004-09-06 17:24:11 +0000519 sqlite3 *db;
drhaf9ff332002-01-16 21:00:27 +0000520 char zBuf[30];
521
522 if( argc!=2 ){
523 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " DB\"", 0);
524 return TCL_ERROR;
525 }
drhb86ccfb2003-01-28 23:13:10 +0000526 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
danielk1977c572ef72004-05-27 09:28:41 +0000527 sprintf(zBuf, "%lld", sqlite3_last_insert_rowid(db));
drhaf9ff332002-01-16 21:00:27 +0000528 Tcl_AppendResult(interp, zBuf, 0);
529 return SQLITE_OK;
530}
531
drhd1bf3512001-04-07 15:24:33 +0000532/*
drh25d65432004-07-22 15:02:25 +0000533** Usage: sqlite3_key DB KEY
534**
535** Set the codec key.
536*/
537static int test_key(
538 void *NotUsed,
539 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
540 int argc, /* Number of arguments */
541 char **argv /* Text of each argument */
542){
drh9bb575f2004-09-06 17:24:11 +0000543 sqlite3 *db;
drh25d65432004-07-22 15:02:25 +0000544 const char *zKey;
545 int nKey;
546 if( argc!=3 ){
547 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
548 " FILENAME\"", 0);
549 return TCL_ERROR;
550 }
551 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
552 zKey = argv[2];
553 nKey = strlen(zKey);
554#ifdef SQLITE_HAS_CODEC
555 sqlite3_key(db, zKey, nKey);
556#endif
557 return TCL_OK;
558}
559
560/*
561** Usage: sqlite3_rekey DB KEY
562**
563** Change the codec key.
564*/
565static int test_rekey(
566 void *NotUsed,
567 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
568 int argc, /* Number of arguments */
569 char **argv /* Text of each argument */
570){
drh9bb575f2004-09-06 17:24:11 +0000571 sqlite3 *db;
drh25d65432004-07-22 15:02:25 +0000572 const char *zKey;
573 int nKey;
574 if( argc!=3 ){
575 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
576 " FILENAME\"", 0);
577 return TCL_ERROR;
578 }
579 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
580 zKey = argv[2];
581 nKey = strlen(zKey);
582#ifdef SQLITE_HAS_CODEC
583 sqlite3_rekey(db, zKey, nKey);
584#endif
585 return TCL_OK;
586}
587
588/*
danielk19776f8a5032004-05-10 10:34:51 +0000589** Usage: sqlite3_close DB
drhd1bf3512001-04-07 15:24:33 +0000590**
danielk19776f8a5032004-05-10 10:34:51 +0000591** Closes the database opened by sqlite3_open.
drhd1bf3512001-04-07 15:24:33 +0000592*/
593static int sqlite_test_close(
594 void *NotUsed,
595 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
596 int argc, /* Number of arguments */
597 char **argv /* Text of each argument */
598){
drh9bb575f2004-09-06 17:24:11 +0000599 sqlite3 *db;
danielk197796d81f92004-06-19 03:33:57 +0000600 int rc;
drhd1bf3512001-04-07 15:24:33 +0000601 if( argc!=2 ){
602 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
603 " FILENAME\"", 0);
604 return TCL_ERROR;
605 }
drhb86ccfb2003-01-28 23:13:10 +0000606 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
danielk197796d81f92004-06-19 03:33:57 +0000607 rc = sqlite3_close(db);
drh4f0c5872007-03-26 22:05:01 +0000608 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
drhd1bf3512001-04-07 15:24:33 +0000609 return TCL_OK;
610}
611
612/*
drhc22bd472002-05-10 13:14:07 +0000613** Implementation of the x_coalesce() function.
614** Return the first argument non-NULL argument.
615*/
drh4f0c5872007-03-26 22:05:01 +0000616static void t1_ifnullFunc(
617 sqlite3_context *context,
618 int argc,
619 sqlite3_value **argv
620){
drhc22bd472002-05-10 13:14:07 +0000621 int i;
622 for(i=0; i<argc; i++){
drh9c054832004-05-31 18:51:57 +0000623 if( SQLITE_NULL!=sqlite3_value_type(argv[i]) ){
drh9310ef22007-04-27 17:16:20 +0000624 int n = sqlite3_value_bytes(argv[i]);
drh03d847e2005-12-09 20:21:58 +0000625 sqlite3_result_text(context, (char*)sqlite3_value_text(argv[i]),
drh9310ef22007-04-27 17:16:20 +0000626 n, SQLITE_TRANSIENT);
drhc22bd472002-05-10 13:14:07 +0000627 break;
628 }
629 }
630}
631
632/*
drhf0313812006-09-04 15:53:53 +0000633** These are test functions. hex8() interprets its argument as
634** UTF8 and returns a hex encoding. hex16le() interprets its argument
635** as UTF16le and returns a hex encoding.
636*/
637static void hex8Func(sqlite3_context *p, int argc, sqlite3_value **argv){
638 const unsigned char *z;
639 int i;
640 char zBuf[200];
641 z = sqlite3_value_text(argv[0]);
642 for(i=0; i<sizeof(zBuf)/2 - 2 && z[i]; i++){
643 sprintf(&zBuf[i*2], "%02x", z[i]&0xff);
644 }
645 zBuf[i*2] = 0;
646 sqlite3_result_text(p, (char*)zBuf, -1, SQLITE_TRANSIENT);
647}
drhaf304692007-04-23 23:56:31 +0000648#ifndef SQLITE_OMIT_UTF16
drhf0313812006-09-04 15:53:53 +0000649static void hex16Func(sqlite3_context *p, int argc, sqlite3_value **argv){
650 const unsigned short int *z;
651 int i;
652 char zBuf[400];
653 z = sqlite3_value_text16(argv[0]);
654 for(i=0; i<sizeof(zBuf)/4 - 4 && z[i]; i++){
655 sprintf(&zBuf[i*4], "%04x", z[i]&0xff);
656 }
657 zBuf[i*4] = 0;
658 sqlite3_result_text(p, (char*)zBuf, -1, SQLITE_TRANSIENT);
659}
drhaf304692007-04-23 23:56:31 +0000660#endif
drhf0313812006-09-04 15:53:53 +0000661
662/*
drhd1d9fc32004-01-07 19:24:48 +0000663** A structure into which to accumulate text.
664*/
665struct dstr {
666 int nAlloc; /* Space allocated */
667 int nUsed; /* Space used */
668 char *z; /* The space */
669};
670
671/*
672** Append text to a dstr
673*/
674static void dstrAppend(struct dstr *p, const char *z, int divider){
675 int n = strlen(z);
676 if( p->nUsed + n + 2 > p->nAlloc ){
677 char *zNew;
678 p->nAlloc = p->nAlloc*2 + n + 200;
679 zNew = sqliteRealloc(p->z, p->nAlloc);
680 if( zNew==0 ){
681 sqliteFree(p->z);
682 memset(p, 0, sizeof(*p));
683 return;
684 }
685 p->z = zNew;
686 }
687 if( divider && p->nUsed>0 ){
688 p->z[p->nUsed++] = divider;
689 }
690 memcpy(&p->z[p->nUsed], z, n+1);
691 p->nUsed += n;
692}
693
694/*
danielk19774adee202004-05-08 08:23:19 +0000695** Invoked for each callback from sqlite3ExecFunc
drhd1d9fc32004-01-07 19:24:48 +0000696*/
697static int execFuncCallback(void *pData, int argc, char **argv, char **NotUsed){
698 struct dstr *p = (struct dstr*)pData;
699 int i;
700 for(i=0; i<argc; i++){
701 if( argv[i]==0 ){
702 dstrAppend(p, "NULL", ' ');
703 }else{
704 dstrAppend(p, argv[i], ' ');
705 }
706 }
707 return 0;
708}
709
710/*
danielk1977e35ee192004-06-26 09:50:11 +0000711** Implementation of the x_sqlite_exec() function. This function takes
drhc22bd472002-05-10 13:14:07 +0000712** a single argument and attempts to execute that argument as SQL code.
drh6cbe1f12002-07-01 00:31:36 +0000713** This is illegal and should set the SQLITE_MISUSE flag on the database.
drhd1d9fc32004-01-07 19:24:48 +0000714**
danielk19776f8a5032004-05-10 10:34:51 +0000715** 2004-Jan-07: We have changed this to make it legal to call sqlite3_exec()
drhd1d9fc32004-01-07 19:24:48 +0000716** from within a function call.
drhc22bd472002-05-10 13:14:07 +0000717**
718** This routine simulates the effect of having two threads attempt to
719** use the same database at the same time.
720*/
danielk197751ad0ec2004-05-24 12:39:02 +0000721static void sqlite3ExecFunc(
danielk19770ae8b832004-05-25 12:05:56 +0000722 sqlite3_context *context,
danielk197751ad0ec2004-05-24 12:39:02 +0000723 int argc,
724 sqlite3_value **argv
725){
drhd1d9fc32004-01-07 19:24:48 +0000726 struct dstr x;
727 memset(&x, 0, sizeof(x));
drh37527852006-03-16 16:19:56 +0000728 (void)sqlite3_exec((sqlite3*)sqlite3_user_data(context),
drh03d847e2005-12-09 20:21:58 +0000729 (char*)sqlite3_value_text(argv[0]),
drhd1d9fc32004-01-07 19:24:48 +0000730 execFuncCallback, &x, 0);
danielk1977d8123362004-06-12 09:25:12 +0000731 sqlite3_result_text(context, x.z, x.nUsed, SQLITE_TRANSIENT);
drhd1d9fc32004-01-07 19:24:48 +0000732 sqliteFree(x.z);
drhc22bd472002-05-10 13:14:07 +0000733}
734
735/*
danielk1977d7263922007-02-05 14:21:47 +0000736** Implementation of tkt2213func(), a scalar function that takes exactly
737** one argument. It has two interesting features:
738**
739** * It calls sqlite3_value_text() 3 times on the argument sqlite3_value*.
740** If the three pointers returned are not the same an SQL error is raised.
741**
742** * Otherwise it returns a copy of the text representation of it's
743** argument in such a way as the VDBE representation is a Mem* cell
744** with the MEM_Term flag clear.
745**
746** Ticket #2213 can therefore be tested by evaluating the following
747** SQL expression:
748**
749** tkt2213func(tkt2213func('a string'));
750*/
751static void tkt2213Function(
752 sqlite3_context *context,
753 int argc,
754 sqlite3_value **argv
755){
756 int nText;
757 unsigned char const *zText1;
758 unsigned char const *zText2;
759 unsigned char const *zText3;
760
761 nText = sqlite3_value_bytes(argv[0]);
762 zText1 = sqlite3_value_text(argv[0]);
763 zText2 = sqlite3_value_text(argv[0]);
764 zText3 = sqlite3_value_text(argv[0]);
765
766 if( zText1!=zText2 || zText2!=zText3 ){
767 sqlite3_result_error(context, "tkt2213 is not fixed", -1);
768 }else{
769 char *zCopy = (char *)sqlite3_malloc(nText);
770 memcpy(zCopy, zText1, nText);
771 sqlite3_result_text(context, zCopy, nText, sqlite3_free);
772 }
773}
774
775/*
drh9310ef22007-04-27 17:16:20 +0000776** The following SQL function takes 4 arguments. The 2nd and
777** 4th argument must be one of these strings: 'text', 'text16',
778** or 'blob' corresponding to API functions
779**
780** sqlite3_value_text()
781** sqlite3_value_text16()
782** sqlite3_value_blob()
783**
784** The third argument is a string, either 'bytes' or 'bytes16' or 'noop',
785** corresponding to APIs:
786**
787** sqlite3_value_bytes()
788** sqlite3_value_bytes16()
789** noop
790**
791** The APIs designated by the 2nd through 4th arguments are applied
792** to the first argument in order. If the pointers returned by the
793** second and fourth are different, this routine returns 1. Otherwise,
794** this routine returns 0.
795**
796** This function is used to test to see when returned pointers from
797** the _text(), _text16() and _blob() APIs become invalidated.
798*/
799static void ptrChngFunction(
800 sqlite3_context *context,
801 int argc,
802 sqlite3_value **argv
803){
804 const void *p1, *p2;
805 const char *zCmd;
806 if( argc!=4 ) return;
807 zCmd = (const char*)sqlite3_value_text(argv[1]);
808 if( zCmd==0 ) return;
809 if( strcmp(zCmd,"text")==0 ){
810 p1 = (const void*)sqlite3_value_text(argv[0]);
811#ifndef SQLITE_OMIT_UTF16
812 }else if( strcmp(zCmd, "text16")==0 ){
813 p1 = (const void*)sqlite3_value_text16(argv[0]);
814#endif
815 }else if( strcmp(zCmd, "blob")==0 ){
816 p1 = (const void*)sqlite3_value_blob(argv[0]);
817 }else{
818 return;
819 }
820 zCmd = (const char*)sqlite3_value_text(argv[2]);
821 if( zCmd==0 ) return;
822 if( strcmp(zCmd,"bytes")==0 ){
823 sqlite3_value_bytes(argv[0]);
824#ifndef SQLITE_OMIT_UTF16
825 }else if( strcmp(zCmd, "bytes16")==0 ){
826 sqlite3_value_bytes16(argv[0]);
827#endif
828 }else if( strcmp(zCmd, "noop")==0 ){
829 /* do nothing */
830 }else{
831 return;
832 }
833 zCmd = (const char*)sqlite3_value_text(argv[3]);
834 if( zCmd==0 ) return;
835 if( strcmp(zCmd,"text")==0 ){
836 p2 = (const void*)sqlite3_value_text(argv[0]);
837#ifndef SQLITE_OMIT_UTF16
838 }else if( strcmp(zCmd, "text16")==0 ){
839 p2 = (const void*)sqlite3_value_text16(argv[0]);
840#endif
841 }else if( strcmp(zCmd, "blob")==0 ){
842 p2 = (const void*)sqlite3_value_blob(argv[0]);
843 }else{
844 return;
845 }
846 sqlite3_result_int(context, p1!=p2);
847}
848
849
850/*
drhc22bd472002-05-10 13:14:07 +0000851** Usage: sqlite_test_create_function DB
852**
danielk19776f8a5032004-05-10 10:34:51 +0000853** Call the sqlite3_create_function API on the given database in order
drhc22bd472002-05-10 13:14:07 +0000854** to create a function named "x_coalesce". This function does the same thing
855** as the "coalesce" function. This function also registers an SQL function
danielk1977e35ee192004-06-26 09:50:11 +0000856** named "x_sqlite_exec" that invokes sqlite3_exec(). Invoking sqlite3_exec()
drhc22bd472002-05-10 13:14:07 +0000857** in this way is illegal recursion and should raise an SQLITE_MISUSE error.
858** The effect is similar to trying to use the same database connection from
859** two threads at the same time.
860**
861** The original motivation for this routine was to be able to call the
danielk19776f8a5032004-05-10 10:34:51 +0000862** sqlite3_create_function function while a query is in progress in order
drhc22bd472002-05-10 13:14:07 +0000863** to test the SQLITE_MISUSE detection logic.
864*/
drhc2eef3b2002-08-31 18:53:06 +0000865static int test_create_function(
drhc22bd472002-05-10 13:14:07 +0000866 void *NotUsed,
867 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
868 int argc, /* Number of arguments */
869 char **argv /* Text of each argument */
870){
drhc60d0442004-09-30 13:43:13 +0000871 int rc;
drh9bb575f2004-09-06 17:24:11 +0000872 sqlite3 *db;
drh9bb575f2004-09-06 17:24:11 +0000873 extern void Md5_Register(sqlite3*);
danielk1977312d6b32004-06-29 13:18:23 +0000874
drhc22bd472002-05-10 13:14:07 +0000875 if( argc!=2 ){
876 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
danielk19774397de52005-01-12 12:44:03 +0000877 " DB\"", 0);
drhc22bd472002-05-10 13:14:07 +0000878 return TCL_ERROR;
879 }
drhb86ccfb2003-01-28 23:13:10 +0000880 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
drhc60d0442004-09-30 13:43:13 +0000881 rc = sqlite3_create_function(db, "x_coalesce", -1, SQLITE_ANY, 0,
drh4f0c5872007-03-26 22:05:01 +0000882 t1_ifnullFunc, 0, 0);
drh235a8182006-09-13 19:21:28 +0000883 if( rc==SQLITE_OK ){
884 rc = sqlite3_create_function(db, "hex8", 1, SQLITE_ANY, 0,
885 hex8Func, 0, 0);
886 }
drhaf304692007-04-23 23:56:31 +0000887#ifndef SQLITE_OMIT_UTF16
drh235a8182006-09-13 19:21:28 +0000888 if( rc==SQLITE_OK ){
889 rc = sqlite3_create_function(db, "hex16", 1, SQLITE_ANY, 0,
890 hex16Func, 0, 0);
891 }
drhaf304692007-04-23 23:56:31 +0000892#endif
danielk1977d7263922007-02-05 14:21:47 +0000893 if( rc==SQLITE_OK ){
894 rc = sqlite3_create_function(db, "tkt2213func", 1, SQLITE_ANY, 0,
895 tkt2213Function, 0, 0);
896 }
drh9310ef22007-04-27 17:16:20 +0000897 if( rc==SQLITE_OK ){
898 rc = sqlite3_create_function(db, "pointer_change", 4, SQLITE_ANY, 0,
899 ptrChngFunction, 0, 0);
900 }
danielk1977312d6b32004-06-29 13:18:23 +0000901
drh5436dc22004-11-14 04:04:17 +0000902#ifndef SQLITE_OMIT_UTF16
danielk1977312d6b32004-06-29 13:18:23 +0000903 /* Use the sqlite3_create_function16() API here. Mainly for fun, but also
904 ** because it is not tested anywhere else. */
drhc60d0442004-09-30 13:43:13 +0000905 if( rc==SQLITE_OK ){
danielk1977576ec6b2005-01-21 11:55:25 +0000906 sqlite3_value *pVal;
danielk1977771151b2006-01-17 13:21:40 +0000907#ifdef SQLITE_MEMDEBUG
908 if( sqlite3_iMallocFail>0 ){
909 sqlite3_iMallocFail++;
910 }
911#endif
drhc60d0442004-09-30 13:43:13 +0000912 pVal = sqlite3ValueNew();
913 sqlite3ValueSetStr(pVal, -1, "x_sqlite_exec", SQLITE_UTF8, SQLITE_STATIC);
914 rc = sqlite3_create_function16(db,
915 sqlite3ValueText(pVal, SQLITE_UTF16NATIVE),
916 1, SQLITE_UTF16, db, sqlite3ExecFunc, 0, 0);
917 sqlite3ValueFree(pVal);
918 }
drh5436dc22004-11-14 04:04:17 +0000919#endif
920
drhc60d0442004-09-30 13:43:13 +0000921 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
drh4f0c5872007-03-26 22:05:01 +0000922 Tcl_SetResult(interp, (char *)t1ErrorName(rc), 0);
drhc22bd472002-05-10 13:14:07 +0000923 return TCL_OK;
924}
925
926/*
927** Routines to implement the x_count() aggregate function.
drh90669c12006-01-20 15:45:36 +0000928**
929** x_count() counts the number of non-null arguments. But there are
930** some twists for testing purposes.
931**
932** If the argument to x_count() is 40 then a UTF-8 error is reported
933** on the step function. If x_count(41) is seen, then a UTF-16 error
934** is reported on the step function. If the total count is 42, then
935** a UTF-8 error is reported on the finalize function.
drhc22bd472002-05-10 13:14:07 +0000936*/
drh4f0c5872007-03-26 22:05:01 +0000937typedef struct t1CountCtx t1CountCtx;
938struct t1CountCtx {
drhc22bd472002-05-10 13:14:07 +0000939 int n;
940};
drh4f0c5872007-03-26 22:05:01 +0000941static void t1CountStep(
942 sqlite3_context *context,
943 int argc,
944 sqlite3_value **argv
945){
946 t1CountCtx *p;
drh4f26d6c2004-05-26 23:25:30 +0000947 p = sqlite3_aggregate_context(context, sizeof(*p));
drh9c054832004-05-31 18:51:57 +0000948 if( (argc==0 || SQLITE_NULL!=sqlite3_value_type(argv[0]) ) && p ){
drhc22bd472002-05-10 13:14:07 +0000949 p->n++;
950 }
drh90669c12006-01-20 15:45:36 +0000951 if( argc>0 ){
952 int v = sqlite3_value_int(argv[0]);
953 if( v==40 ){
954 sqlite3_result_error(context, "value of 40 handed to x_count", -1);
danielk1977a1686c92006-01-23 07:52:37 +0000955#ifndef SQLITE_OMIT_UTF16
drh90669c12006-01-20 15:45:36 +0000956 }else if( v==41 ){
957 const char zUtf16ErrMsg[] = { 0, 0x61, 0, 0x62, 0, 0x63, 0, 0, 0};
958 sqlite3_result_error16(context, &zUtf16ErrMsg[1-SQLITE_BIGENDIAN], -1);
danielk1977a1686c92006-01-23 07:52:37 +0000959#endif
drh90669c12006-01-20 15:45:36 +0000960 }
961 }
drhc22bd472002-05-10 13:14:07 +0000962}
drh4f0c5872007-03-26 22:05:01 +0000963static void t1CountFinalize(sqlite3_context *context){
964 t1CountCtx *p;
drh4f26d6c2004-05-26 23:25:30 +0000965 p = sqlite3_aggregate_context(context, sizeof(*p));
drh90669c12006-01-20 15:45:36 +0000966 if( p ){
967 if( p->n==42 ){
968 sqlite3_result_error(context, "x_count totals to 42", -1);
969 }else{
970 sqlite3_result_int(context, p ? p->n : 0);
971 }
972 }
drhc22bd472002-05-10 13:14:07 +0000973}
974
975/*
976** Usage: sqlite_test_create_aggregate DB
977**
danielk19776f8a5032004-05-10 10:34:51 +0000978** Call the sqlite3_create_function API on the given database in order
drhc22bd472002-05-10 13:14:07 +0000979** to create a function named "x_count". This function does the same thing
980** as the "md5sum" function.
981**
982** The original motivation for this routine was to be able to call the
danielk19776f8a5032004-05-10 10:34:51 +0000983** sqlite3_create_aggregate function while a query is in progress in order
drh90669c12006-01-20 15:45:36 +0000984** to test the SQLITE_MISUSE detection logic. See misuse.test.
985**
986** This routine was later extended to test the use of sqlite3_result_error()
987** within aggregate functions.
drhc22bd472002-05-10 13:14:07 +0000988*/
drhc2eef3b2002-08-31 18:53:06 +0000989static int test_create_aggregate(
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){
drh9bb575f2004-09-06 17:24:11 +0000995 sqlite3 *db;
drhc60d0442004-09-30 13:43:13 +0000996 int rc;
drhc22bd472002-05-10 13:14:07 +0000997 if( argc!=2 ){
998 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
999 " FILENAME\"", 0);
1000 return TCL_ERROR;
1001 }
drhb86ccfb2003-01-28 23:13:10 +00001002 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
drhc60d0442004-09-30 13:43:13 +00001003 rc = sqlite3_create_function(db, "x_count", 0, SQLITE_UTF8, 0, 0,
drh4f0c5872007-03-26 22:05:01 +00001004 t1CountStep,t1CountFinalize);
drhc60d0442004-09-30 13:43:13 +00001005 if( rc==SQLITE_OK ){
1006 sqlite3_create_function(db, "x_count", 1, SQLITE_UTF8, 0, 0,
drh4f0c5872007-03-26 22:05:01 +00001007 t1CountStep,t1CountFinalize);
drhc60d0442004-09-30 13:43:13 +00001008 }
1009 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
drhc22bd472002-05-10 13:14:07 +00001010 return TCL_OK;
1011}
1012
1013
drh3c23a882007-01-09 14:01:13 +00001014/*
1015** Usage: printf TEXT
1016**
1017** Send output to printf. Use this rather than puts to merge the output
1018** in the correct sequence with debugging printfs inserted into C code.
1019** Puts uses a separate buffer and debugging statements will be out of
1020** sequence if it is used.
1021*/
1022static int test_printf(
1023 void *NotUsed,
1024 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1025 int argc, /* Number of arguments */
1026 char **argv /* Text of each argument */
1027){
1028 if( argc!=2 ){
1029 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1030 " TEXT\"", 0);
1031 return TCL_ERROR;
1032 }
1033 printf("%s\n", argv[1]);
1034 return TCL_OK;
1035}
1036
1037
drhc22bd472002-05-10 13:14:07 +00001038
1039/*
danielk19776f8a5032004-05-10 10:34:51 +00001040** Usage: sqlite3_mprintf_int FORMAT INTEGER INTEGER INTEGER
drhd1bf3512001-04-07 15:24:33 +00001041**
1042** Call mprintf with three integer arguments
1043*/
danielk19776f8a5032004-05-10 10:34:51 +00001044static int sqlite3_mprintf_int(
drhd1bf3512001-04-07 15:24:33 +00001045 void *NotUsed,
1046 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1047 int argc, /* Number of arguments */
1048 char **argv /* Text of each argument */
1049){
1050 int a[3], i;
1051 char *z;
1052 if( argc!=5 ){
1053 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1054 " FORMAT INT INT INT\"", 0);
1055 return TCL_ERROR;
1056 }
1057 for(i=2; i<5; i++){
1058 if( Tcl_GetInt(interp, argv[i], &a[i-2]) ) return TCL_ERROR;
1059 }
danielk19776f8a5032004-05-10 10:34:51 +00001060 z = sqlite3_mprintf(argv[1], a[0], a[1], a[2]);
drhd1bf3512001-04-07 15:24:33 +00001061 Tcl_AppendResult(interp, z, 0);
drh3f4fedb2004-05-31 19:34:33 +00001062 sqlite3_free(z);
drhd1bf3512001-04-07 15:24:33 +00001063 return TCL_OK;
1064}
1065
1066/*
drh9d213ef2004-06-30 04:02:11 +00001067** If zNum represents an integer that will fit in 64-bits, then set
1068** *pValue to that integer and return true. Otherwise return false.
1069*/
1070static int sqlite3GetInt64(const char *zNum, i64 *pValue){
1071 if( sqlite3FitsIn64Bits(zNum) ){
1072 sqlite3atoi64(zNum, pValue);
1073 return 1;
1074 }
1075 return 0;
1076}
1077
1078/*
drhe9707672004-06-25 01:10:48 +00001079** Usage: sqlite3_mprintf_int64 FORMAT INTEGER INTEGER INTEGER
1080**
1081** Call mprintf with three 64-bit integer arguments
1082*/
1083static int sqlite3_mprintf_int64(
1084 void *NotUsed,
1085 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1086 int argc, /* Number of arguments */
1087 char **argv /* Text of each argument */
1088){
1089 int i;
1090 sqlite_int64 a[3];
1091 char *z;
1092 if( argc!=5 ){
1093 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1094 " FORMAT INT INT INT\"", 0);
1095 return TCL_ERROR;
1096 }
1097 for(i=2; i<5; i++){
1098 if( !sqlite3GetInt64(argv[i], &a[i-2]) ){
1099 Tcl_AppendResult(interp, "argument is not a valid 64-bit integer", 0);
1100 return TCL_ERROR;
1101 }
1102 }
1103 z = sqlite3_mprintf(argv[1], a[0], a[1], a[2]);
1104 Tcl_AppendResult(interp, z, 0);
1105 sqlite3_free(z);
1106 return TCL_OK;
1107}
1108
1109/*
danielk19776f8a5032004-05-10 10:34:51 +00001110** Usage: sqlite3_mprintf_str FORMAT INTEGER INTEGER STRING
drhd1bf3512001-04-07 15:24:33 +00001111**
1112** Call mprintf with two integer arguments and one string argument
1113*/
danielk19776f8a5032004-05-10 10:34:51 +00001114static int sqlite3_mprintf_str(
drhd1bf3512001-04-07 15:24:33 +00001115 void *NotUsed,
1116 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1117 int argc, /* Number of arguments */
1118 char **argv /* Text of each argument */
1119){
1120 int a[3], i;
1121 char *z;
chwf220b242002-06-16 04:54:28 +00001122 if( argc<4 || argc>5 ){
drhd1bf3512001-04-07 15:24:33 +00001123 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
chwf220b242002-06-16 04:54:28 +00001124 " FORMAT INT INT ?STRING?\"", 0);
drhd1bf3512001-04-07 15:24:33 +00001125 return TCL_ERROR;
1126 }
1127 for(i=2; i<4; i++){
1128 if( Tcl_GetInt(interp, argv[i], &a[i-2]) ) return TCL_ERROR;
1129 }
danielk19776f8a5032004-05-10 10:34:51 +00001130 z = sqlite3_mprintf(argv[1], a[0], a[1], argc>4 ? argv[4] : NULL);
drhd1bf3512001-04-07 15:24:33 +00001131 Tcl_AppendResult(interp, z, 0);
drh3f4fedb2004-05-31 19:34:33 +00001132 sqlite3_free(z);
drhd1bf3512001-04-07 15:24:33 +00001133 return TCL_OK;
1134}
1135
1136/*
drhb3738b62007-03-31 15:02:49 +00001137** Usage: sqlite3_snprintf_str INTEGER FORMAT INTEGER INTEGER STRING
1138**
1139** Call mprintf with two integer arguments and one string argument
1140*/
1141static int sqlite3_snprintf_str(
1142 void *NotUsed,
1143 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1144 int argc, /* Number of arguments */
1145 char **argv /* Text of each argument */
1146){
1147 int a[3], i;
1148 int n;
1149 char *z;
1150 if( argc<5 || argc>6 ){
1151 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1152 " INT FORMAT INT INT ?STRING?\"", 0);
1153 return TCL_ERROR;
1154 }
1155 if( Tcl_GetInt(interp, argv[1], &n) ) return TCL_ERROR;
1156 if( n<0 ){
1157 Tcl_AppendResult(interp, "N must be non-negative", 0);
1158 return TCL_ERROR;
1159 }
1160 for(i=3; i<5; i++){
1161 if( Tcl_GetInt(interp, argv[i], &a[i-3]) ) return TCL_ERROR;
1162 }
1163 z = sqlite3_malloc( n+1 );
1164 sqlite3_snprintf(n, z, argv[2], a[0], a[1], argc>4 ? argv[5] : NULL);
1165 Tcl_AppendResult(interp, z, 0);
1166 sqlite3_free(z);
1167 return TCL_OK;
1168}
1169
1170/*
drh63782852005-08-30 19:30:59 +00001171** Usage: sqlite3_mprintf_double FORMAT INTEGER INTEGER DOUBLE
drhd1bf3512001-04-07 15:24:33 +00001172**
1173** Call mprintf with two integer arguments and one double argument
1174*/
danielk19776f8a5032004-05-10 10:34:51 +00001175static int sqlite3_mprintf_double(
drhd1bf3512001-04-07 15:24:33 +00001176 void *NotUsed,
1177 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1178 int argc, /* Number of arguments */
1179 char **argv /* Text of each argument */
1180){
1181 int a[3], i;
1182 double r;
1183 char *z;
1184 if( argc!=5 ){
1185 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
drh63782852005-08-30 19:30:59 +00001186 " FORMAT INT INT DOUBLE\"", 0);
drhd1bf3512001-04-07 15:24:33 +00001187 return TCL_ERROR;
1188 }
1189 for(i=2; i<4; i++){
1190 if( Tcl_GetInt(interp, argv[i], &a[i-2]) ) return TCL_ERROR;
1191 }
1192 if( Tcl_GetDouble(interp, argv[4], &r) ) return TCL_ERROR;
danielk19776f8a5032004-05-10 10:34:51 +00001193 z = sqlite3_mprintf(argv[1], a[0], a[1], r);
drhd1bf3512001-04-07 15:24:33 +00001194 Tcl_AppendResult(interp, z, 0);
drh3f4fedb2004-05-31 19:34:33 +00001195 sqlite3_free(z);
drhd1bf3512001-04-07 15:24:33 +00001196 return TCL_OK;
1197}
1198
1199/*
drh63782852005-08-30 19:30:59 +00001200** Usage: sqlite3_mprintf_scaled FORMAT DOUBLE DOUBLE
drhb621c232004-02-21 19:41:04 +00001201**
1202** Call mprintf with a single double argument which is the product of the
1203** two arguments given above. This is used to generate overflow and underflow
1204** doubles to test that they are converted properly.
1205*/
danielk19776f8a5032004-05-10 10:34:51 +00001206static int sqlite3_mprintf_scaled(
drhb621c232004-02-21 19:41:04 +00001207 void *NotUsed,
1208 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1209 int argc, /* Number of arguments */
1210 char **argv /* Text of each argument */
1211){
1212 int i;
1213 double r[2];
1214 char *z;
1215 if( argc!=4 ){
1216 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1217 " FORMAT DOUBLE DOUBLE\"", 0);
1218 return TCL_ERROR;
1219 }
1220 for(i=2; i<4; i++){
1221 if( Tcl_GetDouble(interp, argv[i], &r[i-2]) ) return TCL_ERROR;
1222 }
danielk19776f8a5032004-05-10 10:34:51 +00001223 z = sqlite3_mprintf(argv[1], r[0]*r[1]);
drhb621c232004-02-21 19:41:04 +00001224 Tcl_AppendResult(interp, z, 0);
drh3f4fedb2004-05-31 19:34:33 +00001225 sqlite3_free(z);
drhb621c232004-02-21 19:41:04 +00001226 return TCL_OK;
1227}
1228
1229/*
drhe29b1a02004-07-17 21:56:09 +00001230** Usage: sqlite3_mprintf_stronly FORMAT STRING
1231**
1232** Call mprintf with a single double argument which is the product of the
1233** two arguments given above. This is used to generate overflow and underflow
1234** doubles to test that they are converted properly.
1235*/
1236static int sqlite3_mprintf_stronly(
1237 void *NotUsed,
1238 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1239 int argc, /* Number of arguments */
1240 char **argv /* Text of each argument */
1241){
1242 char *z;
1243 if( argc!=3 ){
1244 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1245 " FORMAT STRING\"", 0);
1246 return TCL_ERROR;
1247 }
1248 z = sqlite3_mprintf(argv[1], argv[2]);
1249 Tcl_AppendResult(interp, z, 0);
1250 sqlite3_free(z);
1251 return TCL_OK;
1252}
1253
1254/*
drh63782852005-08-30 19:30:59 +00001255** Usage: sqlite3_mprintf_hexdouble FORMAT HEX
1256**
1257** Call mprintf with a single double argument which is derived from the
1258** hexadecimal encoding of an IEEE double.
1259*/
1260static int sqlite3_mprintf_hexdouble(
1261 void *NotUsed,
1262 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1263 int argc, /* Number of arguments */
1264 char **argv /* Text of each argument */
1265){
1266 char *z;
1267 double r;
1268 unsigned x1, x2;
1269 long long unsigned d;
1270 if( argc!=3 ){
1271 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1272 " FORMAT STRING\"", 0);
1273 return TCL_ERROR;
1274 }
1275 if( sscanf(argv[2], "%08x%08x", &x2, &x1)!=2 ){
1276 Tcl_AppendResult(interp, "2nd argument should be 16-characters of hex", 0);
1277 return TCL_ERROR;
1278 }
1279 d = x2;
1280 d = (d<<32) + x1;
1281 memcpy(&r, &d, sizeof(r));
1282 z = sqlite3_mprintf(argv[1], r);
1283 Tcl_AppendResult(interp, z, 0);
1284 sqlite3_free(z);
1285 return TCL_OK;
1286}
1287
1288/*
drh46934232004-11-20 19:18:00 +00001289** Usage: sqlite_malloc_fail N ?REPEAT-INTERVAL?
drhdaffd0e2001-04-11 14:28:42 +00001290**
drh46934232004-11-20 19:18:00 +00001291** Rig sqliteMalloc() to fail on the N-th call and every REPEAT-INTERVAL call
1292** after that. If REPEAT-INTERVAL is 0 or is omitted, then only a single
1293** malloc will fail. If REPEAT-INTERVAL is 1 then all mallocs after the
1294** first failure will continue to fail on every call. If REPEAT-INTERVAL is
1295** 2 then every other malloc will fail. And so forth.
1296**
danielk19779e128002006-01-18 16:51:35 +00001297** Turn off this mechanism and reset the sqlite3ThreadData()->mallocFailed
1298** variable if N==0.
drhdaffd0e2001-04-11 14:28:42 +00001299*/
danielk19772c336542005-01-13 02:14:23 +00001300#ifdef SQLITE_MEMDEBUG
drhdaffd0e2001-04-11 14:28:42 +00001301static int sqlite_malloc_fail(
1302 void *NotUsed,
1303 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1304 int argc, /* Number of arguments */
1305 char **argv /* Text of each argument */
1306){
1307 int n;
drh46934232004-11-20 19:18:00 +00001308 int rep;
1309 if( argc!=2 && argc!=3 ){
drhdaffd0e2001-04-11 14:28:42 +00001310 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " N\"", 0);
1311 return TCL_ERROR;
1312 }
1313 if( Tcl_GetInt(interp, argv[1], &n) ) return TCL_ERROR;
drh46934232004-11-20 19:18:00 +00001314 if( argc==3 ){
1315 if( Tcl_GetInt(interp, argv[2], &rep) ) return TCL_ERROR;
1316 }else{
1317 rep = 0;
1318 }
danielk19776f8a5032004-05-10 10:34:51 +00001319 sqlite3_iMallocFail = n;
drh46934232004-11-20 19:18:00 +00001320 sqlite3_iMallocReset = rep;
drhdaffd0e2001-04-11 14:28:42 +00001321 return TCL_OK;
1322}
1323#endif
1324
1325/*
1326** Usage: sqlite_malloc_stat
1327**
1328** Return the number of prior calls to sqliteMalloc() and sqliteFree().
1329*/
danielk19772c336542005-01-13 02:14:23 +00001330#ifdef SQLITE_MEMDEBUG
drhdaffd0e2001-04-11 14:28:42 +00001331static int sqlite_malloc_stat(
1332 void *NotUsed,
1333 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1334 int argc, /* Number of arguments */
1335 char **argv /* Text of each argument */
1336){
1337 char zBuf[200];
danielk19775591df52005-12-20 09:19:37 +00001338 sprintf(zBuf, "%d %d %d", sqlite3_nMalloc,sqlite3_nFree,sqlite3_iMallocFail);
drhdaffd0e2001-04-11 14:28:42 +00001339 Tcl_AppendResult(interp, zBuf, 0);
1340 return TCL_OK;
1341}
danielk19775591df52005-12-20 09:19:37 +00001342
1343/*
1344** This function implements a Tcl command that may be invoked using any of
1345** the four forms enumerated below.
1346**
1347** sqlite_malloc_outstanding
1348** Return a summary of all unfreed blocks of memory allocated by the
1349** current thread. See comments above function sqlite3OutstandingMallocs()
1350** in util.c for a description of the returned value.
1351**
1352** sqlite_malloc_outstanding -bytes
1353** Return the total amount of unfreed memory (in bytes) allocated by
1354** this thread.
1355**
1356** sqlite_malloc_outstanding -maxbytes
1357** Return the maximum amount of dynamic memory in use at one time
1358** by this thread.
1359**
1360** sqlite_malloc_outstanding -clearmaxbytes
1361** Set the value returned by [sqlite_malloc_outstanding -maxbytes]
1362** to the current value of [sqlite_malloc_outstanding -bytes].
1363*/
danielk19772e588c72005-12-09 14:25:08 +00001364static int sqlite_malloc_outstanding(
danielk19775591df52005-12-20 09:19:37 +00001365 ClientData clientData,
danielk19772e588c72005-12-09 14:25:08 +00001366 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
danielk19775591df52005-12-20 09:19:37 +00001367 int objc, /* Number of arguments */
1368 Tcl_Obj *CONST objv[] /* Command arguments */
danielk19772e588c72005-12-09 14:25:08 +00001369){
1370 extern int sqlite3OutstandingMallocs(Tcl_Interp *interp);
danielk19775591df52005-12-20 09:19:37 +00001371
drhdab97c12006-01-19 11:28:06 +00001372#if defined(SQLITE_DEBUG) && defined(SQLITE_MEMDEBUG) && SQLITE_MEMDEBUG>1
danielk19775591df52005-12-20 09:19:37 +00001373 if( objc==2 ){
1374 const char *zArg = Tcl_GetString(objv[1]);
danielk1977191c3e72006-01-19 07:18:14 +00001375#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
1376 ThreadData const *pTd = sqlite3ThreadDataReadOnly();
danielk19775591df52005-12-20 09:19:37 +00001377 if( 0==strcmp(zArg, "-bytes") ){
danielk197752622822006-01-09 09:59:49 +00001378 Tcl_SetObjResult(interp, Tcl_NewIntObj(pTd->nAlloc));
danielk19775591df52005-12-20 09:19:37 +00001379 }else if( 0==strcmp(zArg, "-clearmaxbytes") ){
danielk19779e128002006-01-18 16:51:35 +00001380 sqlite3_nMaxAlloc = pTd->nAlloc;
danielk1977191c3e72006-01-19 07:18:14 +00001381 }else
1382#endif
1383 if( 0==strcmp(zArg, "-maxbytes") ){
1384 Tcl_SetObjResult(interp, Tcl_NewWideIntObj(sqlite3_nMaxAlloc));
danielk19775591df52005-12-20 09:19:37 +00001385 }else{
1386 Tcl_AppendResult(interp, "bad option \"", zArg,
1387 "\": must be -bytes, -maxbytes or -clearmaxbytes", 0
1388 );
1389 return TCL_ERROR;
1390 }
1391
1392 return TCL_OK;
1393 }
danielk197752622822006-01-09 09:59:49 +00001394
1395 if( objc!=1 ){
1396 Tcl_WrongNumArgs(interp, 1, objv, "?-bytes?");
1397 return TCL_ERROR;
1398 }
danielk19775591df52005-12-20 09:19:37 +00001399
danielk19772e588c72005-12-09 14:25:08 +00001400 return sqlite3OutstandingMallocs(interp);
danielk1977191c3e72006-01-19 07:18:14 +00001401#else
1402 return TCL_OK;
1403#endif
danielk19772e588c72005-12-09 14:25:08 +00001404}
drhdaffd0e2001-04-11 14:28:42 +00001405#endif
1406
1407/*
danielk197752622822006-01-09 09:59:49 +00001408** Usage: sqlite3_enable_shared_cache BOOLEAN
danielk1977aef0bf62005-12-30 16:28:01 +00001409**
1410*/
drh6f7adc82006-01-11 21:41:20 +00001411#if !defined(SQLITE_OMIT_SHARED_CACHE)
1412static int test_enable_shared(
danielk197752622822006-01-09 09:59:49 +00001413 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
danielk1977aef0bf62005-12-30 16:28:01 +00001414 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1415 int objc, /* Number of arguments */
1416 Tcl_Obj *CONST objv[] /* Command arguments */
1417){
1418 int rc;
1419 int enable;
danielk197752622822006-01-09 09:59:49 +00001420 int ret = 0;
danielk1977aef0bf62005-12-30 16:28:01 +00001421
1422 if( objc!=2 ){
1423 Tcl_WrongNumArgs(interp, 1, objv, "BOOLEAN");
1424 return TCL_ERROR;
1425 }
1426 if( Tcl_GetBooleanFromObj(interp, objv[1], &enable) ){
1427 return TCL_ERROR;
1428 }
drh6f7adc82006-01-11 21:41:20 +00001429 ret = sqlite3ThreadDataReadOnly()->useSharedData;
1430 rc = sqlite3_enable_shared_cache(enable);
danielk1977aef0bf62005-12-30 16:28:01 +00001431 if( rc!=SQLITE_OK ){
1432 Tcl_SetResult(interp, (char *)sqlite3ErrStr(rc), TCL_STATIC);
1433 return TCL_ERROR;
1434 }
danielk197752622822006-01-09 09:59:49 +00001435 Tcl_SetObjResult(interp, Tcl_NewBooleanObj(ret));
danielk1977aef0bf62005-12-30 16:28:01 +00001436 return TCL_OK;
1437}
1438#endif
1439
drh16a9b832007-05-05 18:39:25 +00001440
1441
danielk1977aef0bf62005-12-30 16:28:01 +00001442/*
drh4ac285a2006-09-15 07:28:50 +00001443** Usage: sqlite3_extended_result_codes DB BOOLEAN
1444**
1445*/
1446static int test_extended_result_codes(
1447 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
1448 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1449 int objc, /* Number of arguments */
1450 Tcl_Obj *CONST objv[] /* Command arguments */
1451){
1452 int enable;
1453 sqlite3 *db;
1454
1455 if( objc!=3 ){
1456 Tcl_WrongNumArgs(interp, 1, objv, "DB BOOLEAN");
1457 return TCL_ERROR;
1458 }
1459 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
1460 if( Tcl_GetBooleanFromObj(interp, objv[2], &enable) ) return TCL_ERROR;
1461 sqlite3_extended_result_codes(db, enable);
1462 return TCL_OK;
1463}
1464
1465/*
danielk1977161fb792006-01-24 10:58:21 +00001466** Usage: sqlite3_libversion_number
1467**
1468*/
1469static int test_libversion_number(
1470 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
1471 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1472 int objc, /* Number of arguments */
1473 Tcl_Obj *CONST objv[] /* Command arguments */
1474){
1475 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_libversion_number()));
1476 return TCL_OK;
1477}
1478
1479/*
danielk1977deb802c2006-02-09 13:43:28 +00001480** Usage: sqlite3_table_column_metadata DB dbname tblname colname
1481**
1482*/
1483#ifdef SQLITE_ENABLE_COLUMN_METADATA
1484static int test_table_column_metadata(
1485 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
1486 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1487 int objc, /* Number of arguments */
1488 Tcl_Obj *CONST objv[] /* Command arguments */
1489){
1490 sqlite3 *db;
1491 const char *zDb;
1492 const char *zTbl;
1493 const char *zCol;
1494 int rc;
1495 Tcl_Obj *pRet;
1496
1497 const char *zDatatype;
1498 const char *zCollseq;
1499 int notnull;
1500 int primarykey;
1501 int autoincrement;
1502
1503 if( objc!=5 ){
1504 Tcl_WrongNumArgs(interp, 1, objv, "DB dbname tblname colname");
1505 return TCL_ERROR;
1506 }
1507 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
1508 zDb = Tcl_GetString(objv[2]);
1509 zTbl = Tcl_GetString(objv[3]);
1510 zCol = Tcl_GetString(objv[4]);
1511
1512 if( strlen(zDb)==0 ) zDb = 0;
1513
1514 rc = sqlite3_table_column_metadata(db, zDb, zTbl, zCol,
1515 &zDatatype, &zCollseq, &notnull, &primarykey, &autoincrement);
1516
1517 if( rc!=SQLITE_OK ){
1518 Tcl_AppendResult(interp, sqlite3_errmsg(db), 0);
1519 return TCL_ERROR;
1520 }
1521
1522 pRet = Tcl_NewObj();
1523 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zDatatype, -1));
1524 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zCollseq, -1));
1525 Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(notnull));
1526 Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(primarykey));
1527 Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(autoincrement));
1528 Tcl_SetObjResult(interp, pRet);
1529
1530 return TCL_OK;
1531}
1532#endif
1533
danielk1977dcbb5d32007-05-04 18:36:44 +00001534#ifndef SQLITE_OMIT_INCRBLOB
1535
1536/*
1537** sqlite3_blob_read CHANNEL OFFSET N
danielk1977a9808b32007-05-07 09:32:45 +00001538**
1539** This command is used to test the sqlite3_blob_read() in ways that
1540** the Tcl channel interface does not. The first argument should
1541** be the name of a valid channel created by the [incrblob] method
1542** of a database handle. This function calls sqlite3_blob_read()
1543** to read N bytes from offset OFFSET from the underlying SQLite
1544** blob handle.
1545**
1546** On success, a byte-array object containing the read data is
1547** returned. On failure, the interpreter result is set to the
1548** text representation of the returned error code (i.e. "SQLITE_NOMEM")
1549** and a Tcl exception is thrown.
danielk1977dcbb5d32007-05-04 18:36:44 +00001550*/
1551static int test_blob_read(
1552 ClientData clientData, /* Not used */
1553 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1554 int objc, /* Number of arguments */
1555 Tcl_Obj *CONST objv[] /* Command arguments */
1556){
1557 Tcl_Channel channel;
1558 ClientData instanceData;
1559 sqlite3_blob *pBlob;
1560 int notUsed;
1561 int nByte;
1562 int iOffset;
1563 unsigned char *zBuf;
1564 int rc;
1565
1566 if( objc!=4 ){
1567 Tcl_WrongNumArgs(interp, 1, objv, "CHANNEL OFFSET N");
1568 return TCL_ERROR;
1569 }
1570
1571 channel = Tcl_GetChannel(interp, Tcl_GetString(objv[1]), &notUsed);
1572 if( !channel
1573 || TCL_OK!=Tcl_GetIntFromObj(interp, objv[2], &iOffset)
1574 || TCL_OK!=Tcl_GetIntFromObj(interp, objv[3], &nByte)
1575 || nByte<0 || iOffset<0
1576 ){
1577 return TCL_ERROR;
1578 }
1579
1580 instanceData = Tcl_GetChannelInstanceData(channel);
1581 pBlob = *((sqlite3_blob **)instanceData);
1582
1583 zBuf = (unsigned char *)Tcl_Alloc(nByte);
1584 rc = sqlite3_blob_read(pBlob, zBuf, nByte, iOffset);
1585 if( rc==SQLITE_OK ){
1586 Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(zBuf, nByte));
1587 }else{
1588 Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_VOLATILE);
1589 }
1590 Tcl_Free((char *)zBuf);
1591
1592 return (rc==SQLITE_OK ? TCL_OK : TCL_ERROR);
1593}
1594
1595/*
1596** sqlite3_blob_write CHANNEL OFFSET DATA
danielk1977a9808b32007-05-07 09:32:45 +00001597**
1598** This command is used to test the sqlite3_blob_write() in ways that
1599** the Tcl channel interface does not. The first argument should
1600** be the name of a valid channel created by the [incrblob] method
1601** of a database handle. This function calls sqlite3_blob_write()
1602** to write the DATA byte-array to the underlying SQLite blob handle.
1603** at offset OFFSET.
1604**
1605** On success, an empty string is returned. On failure, the interpreter
1606** result is set to the text representation of the returned error code
1607** (i.e. "SQLITE_NOMEM") and a Tcl exception is thrown.
danielk1977dcbb5d32007-05-04 18:36:44 +00001608*/
1609static int test_blob_write(
1610 ClientData clientData, /* Not used */
1611 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1612 int objc, /* Number of arguments */
1613 Tcl_Obj *CONST objv[] /* Command arguments */
1614){
1615 Tcl_Channel channel;
1616 ClientData instanceData;
1617 sqlite3_blob *pBlob;
1618 int notUsed;
1619 int iOffset;
1620 int rc;
1621
1622 unsigned char *zBuf;
1623 int nBuf;
1624
1625 if( objc!=4 ){
1626 Tcl_WrongNumArgs(interp, 1, objv, "CHANNEL OFFSET DATA");
1627 return TCL_ERROR;
1628 }
1629
1630 channel = Tcl_GetChannel(interp, Tcl_GetString(objv[1]), &notUsed);
1631 if( !channel
1632 || TCL_OK!=Tcl_GetIntFromObj(interp, objv[2], &iOffset)
1633 || iOffset<0
1634 ){
1635 return TCL_ERROR;
1636 }
1637
1638 instanceData = Tcl_GetChannelInstanceData(channel);
1639 pBlob = *((sqlite3_blob **)instanceData);
1640
1641 zBuf = Tcl_GetByteArrayFromObj(objv[3], &nBuf);
1642 rc = sqlite3_blob_write(pBlob, zBuf, nBuf, iOffset);
1643 if( rc!=SQLITE_OK ){
1644 Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_VOLATILE);
1645 }
1646
1647 return (rc==SQLITE_OK ? TCL_OK : TCL_ERROR);
1648}
1649#endif
drhc2e87a32006-06-27 15:16:14 +00001650
danielk1977deb802c2006-02-09 13:43:28 +00001651/*
danielk1977a9808b32007-05-07 09:32:45 +00001652** Usage: sqlite3_create_collation_x DB-HANDLE NAME CMP-PROC DEL-PROC
1653**
1654** This Tcl proc is used for testing the experimental
1655** sqlite3_create_collation_x() interface.
1656*/
1657struct TestCollationX {
1658 Tcl_Interp *interp;
1659 Tcl_Obj *pCmp;
1660 Tcl_Obj *pDel;
1661};
1662typedef struct TestCollationX TestCollationX;
1663static void testCreateCollationDel(void *pCtx){
1664 TestCollationX *p = (TestCollationX *)pCtx;
1665
1666 int rc = Tcl_EvalObjEx(p->interp, p->pDel, TCL_EVAL_DIRECT|TCL_EVAL_GLOBAL);
1667 if( rc!=TCL_OK ){
1668 Tcl_BackgroundError(p->interp);
1669 }
1670
1671 Tcl_DecrRefCount(p->pCmp);
1672 Tcl_DecrRefCount(p->pDel);
1673 sqlite3_free((void *)p);
1674}
1675static int testCreateCollationCmp(
1676 void *pCtx,
1677 int nLeft,
1678 const void *zLeft,
1679 int nRight,
1680 const void *zRight
1681){
1682 TestCollationX *p = (TestCollationX *)pCtx;
1683 Tcl_Obj *pScript = Tcl_DuplicateObj(p->pCmp);
1684 int iRes = 0;
1685
1686 Tcl_IncrRefCount(pScript);
1687 Tcl_ListObjAppendElement(0, pScript, Tcl_NewStringObj((char *)zLeft, nLeft));
1688 Tcl_ListObjAppendElement(0, pScript, Tcl_NewStringObj((char *)zRight,nRight));
1689
1690 if( TCL_OK!=Tcl_EvalObjEx(p->interp, pScript, TCL_EVAL_DIRECT|TCL_EVAL_GLOBAL)
1691 || TCL_OK!=Tcl_GetIntFromObj(p->interp, Tcl_GetObjResult(p->interp), &iRes)
1692 ){
1693 Tcl_BackgroundError(p->interp);
1694 }
1695 Tcl_DecrRefCount(pScript);
1696
1697 return iRes;
1698}
1699static int test_create_collation_x(
1700 ClientData clientData, /* Not used */
1701 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1702 int objc, /* Number of arguments */
1703 Tcl_Obj *CONST objv[] /* Command arguments */
1704){
1705 TestCollationX *p;
1706 sqlite3 *db;
1707
1708 if( objc!=5 ){
1709 Tcl_WrongNumArgs(interp, 1, objv, "DB-HANDLE NAME CMP-PROC DEL-PROC");
1710 return TCL_ERROR;
1711 }
1712 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
1713
1714 p = (TestCollationX *)sqlite3_malloc(sizeof(TestCollationX));
1715 p->pCmp = objv[3];
1716 p->pDel = objv[4];
1717 p->interp = interp;
1718 Tcl_IncrRefCount(p->pCmp);
1719 Tcl_IncrRefCount(p->pDel);
1720
1721 sqlite3_create_collation_x(db, Tcl_GetString(objv[2]), SQLITE_UTF8,
1722 (void *)p, testCreateCollationCmp, testCreateCollationDel
1723 );
1724 return TCL_OK;
1725}
1726
1727/*
danielk197769e777f2006-06-14 10:38:02 +00001728** Usage: sqlite3_load_extension DB-HANDLE FILE ?PROC?
1729*/
1730static int test_load_extension(
1731 ClientData clientData, /* Not used */
1732 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1733 int objc, /* Number of arguments */
1734 Tcl_Obj *CONST objv[] /* Command arguments */
1735){
1736 Tcl_CmdInfo cmdInfo;
1737 sqlite3 *db;
1738 int rc;
1739 char *zDb;
1740 char *zFile;
1741 char *zProc = 0;
1742 char *zErr = 0;
1743
1744 if( objc!=4 && objc!=3 ){
1745 Tcl_WrongNumArgs(interp, 1, objv, "DB-HANDLE FILE ?PROC?");
1746 return TCL_ERROR;
1747 }
1748 zDb = Tcl_GetString(objv[1]);
1749 zFile = Tcl_GetString(objv[2]);
1750 if( objc==4 ){
1751 zProc = Tcl_GetString(objv[3]);
1752 }
1753
1754 /* Extract the C database handle from the Tcl command name */
1755 if( !Tcl_GetCommandInfo(interp, zDb, &cmdInfo) ){
1756 Tcl_AppendResult(interp, "command not found: ", zDb, (char*)0);
1757 return TCL_ERROR;
1758 }
1759 db = ((struct SqliteDb*)cmdInfo.objClientData)->db;
1760 assert(db);
1761
1762 /* Call the underlying C function. If an error occurs, set rc to
1763 ** TCL_ERROR and load any error string into the interpreter. If no
1764 ** error occurs, set rc to TCL_OK.
1765 */
drhc2e87a32006-06-27 15:16:14 +00001766#ifdef SQLITE_OMIT_LOAD_EXTENSION
1767 rc = SQLITE_ERROR;
1768 zErr = sqlite3_mprintf("this build omits sqlite3_load_extension()");
1769#else
danielk197769e777f2006-06-14 10:38:02 +00001770 rc = sqlite3_load_extension(db, zFile, zProc, &zErr);
drhc2e87a32006-06-27 15:16:14 +00001771#endif
danielk197769e777f2006-06-14 10:38:02 +00001772 if( rc!=SQLITE_OK ){
1773 Tcl_SetResult(interp, zErr ? zErr : "", TCL_VOLATILE);
1774 rc = TCL_ERROR;
1775 }else{
1776 rc = TCL_OK;
1777 }
1778 sqlite3_free(zErr);
1779
1780 return rc;
1781}
1782
1783/*
drhc2e87a32006-06-27 15:16:14 +00001784** Usage: sqlite3_enable_load_extension DB-HANDLE ONOFF
1785*/
1786static int test_enable_load(
1787 ClientData clientData, /* Not used */
1788 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1789 int objc, /* Number of arguments */
1790 Tcl_Obj *CONST objv[] /* Command arguments */
1791){
1792 Tcl_CmdInfo cmdInfo;
1793 sqlite3 *db;
1794 char *zDb;
1795 int onoff;
1796
1797 if( objc!=3 ){
1798 Tcl_WrongNumArgs(interp, 1, objv, "DB-HANDLE ONOFF");
1799 return TCL_ERROR;
1800 }
1801 zDb = Tcl_GetString(objv[1]);
1802
1803 /* Extract the C database handle from the Tcl command name */
1804 if( !Tcl_GetCommandInfo(interp, zDb, &cmdInfo) ){
1805 Tcl_AppendResult(interp, "command not found: ", zDb, (char*)0);
1806 return TCL_ERROR;
1807 }
1808 db = ((struct SqliteDb*)cmdInfo.objClientData)->db;
1809 assert(db);
1810
1811 /* Get the onoff parameter */
1812 if( Tcl_GetBooleanFromObj(interp, objv[2], &onoff) ){
1813 return TCL_ERROR;
1814 }
1815
1816#ifdef SQLITE_OMIT_LOAD_EXTENSION
1817 Tcl_AppendResult(interp, "this build omits sqlite3_load_extension()");
1818 return TCL_ERROR;
1819#else
1820 sqlite3_enable_load_extension(db, onoff);
1821 return TCL_OK;
1822#endif
1823}
1824
1825/*
drh28b4e482002-03-11 02:06:13 +00001826** Usage: sqlite_abort
1827**
1828** Shutdown the process immediately. This is not a clean shutdown.
1829** This command is used to test the recoverability of a database in
1830** the event of a program crash.
1831*/
1832static int sqlite_abort(
1833 void *NotUsed,
1834 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1835 int argc, /* Number of arguments */
1836 char **argv /* Text of each argument */
1837){
1838 assert( interp==0 ); /* This will always fail */
1839 return TCL_OK;
1840}
1841
1842/*
drh6cbe1f12002-07-01 00:31:36 +00001843** The following routine is a user-defined SQL function whose purpose
1844** is to test the sqlite_set_result() API.
1845*/
danielk19770ae8b832004-05-25 12:05:56 +00001846static void testFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
drh6cbe1f12002-07-01 00:31:36 +00001847 while( argc>=2 ){
drh03d847e2005-12-09 20:21:58 +00001848 const char *zArg0 = (char*)sqlite3_value_text(argv[0]);
danielk19776d88bad2004-05-27 14:23:36 +00001849 if( zArg0 ){
1850 if( 0==sqlite3StrICmp(zArg0, "int") ){
1851 sqlite3_result_int(context, sqlite3_value_int(argv[1]));
1852 }else if( sqlite3StrICmp(zArg0,"int64")==0 ){
1853 sqlite3_result_int64(context, sqlite3_value_int64(argv[1]));
1854 }else if( sqlite3StrICmp(zArg0,"string")==0 ){
drh03d847e2005-12-09 20:21:58 +00001855 sqlite3_result_text(context, (char*)sqlite3_value_text(argv[1]), -1,
danielk1977d8123362004-06-12 09:25:12 +00001856 SQLITE_TRANSIENT);
danielk19776d88bad2004-05-27 14:23:36 +00001857 }else if( sqlite3StrICmp(zArg0,"double")==0 ){
1858 sqlite3_result_double(context, sqlite3_value_double(argv[1]));
1859 }else if( sqlite3StrICmp(zArg0,"null")==0 ){
1860 sqlite3_result_null(context);
1861 }else if( sqlite3StrICmp(zArg0,"value")==0 ){
1862 sqlite3_result_value(context, argv[sqlite3_value_int(argv[1])]);
1863 }else{
1864 goto error_out;
1865 }
drh6cbe1f12002-07-01 00:31:36 +00001866 }else{
danielk19776d88bad2004-05-27 14:23:36 +00001867 goto error_out;
drh6cbe1f12002-07-01 00:31:36 +00001868 }
1869 argc -= 2;
1870 argv += 2;
1871 }
danielk19776d88bad2004-05-27 14:23:36 +00001872 return;
1873
1874error_out:
1875 sqlite3_result_error(context,"first argument should be one of: "
1876 "int int64 string double null value", -1);
drh6cbe1f12002-07-01 00:31:36 +00001877}
1878
1879/*
1880** Usage: sqlite_register_test_function DB NAME
1881**
1882** Register the test SQL function on the database DB under the name NAME.
1883*/
drhc2eef3b2002-08-31 18:53:06 +00001884static int test_register_func(
drh6cbe1f12002-07-01 00:31:36 +00001885 void *NotUsed,
1886 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1887 int argc, /* Number of arguments */
1888 char **argv /* Text of each argument */
1889){
drh9bb575f2004-09-06 17:24:11 +00001890 sqlite3 *db;
drh6cbe1f12002-07-01 00:31:36 +00001891 int rc;
1892 if( argc!=3 ){
1893 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1894 " DB FUNCTION-NAME", 0);
1895 return TCL_ERROR;
1896 }
drhb86ccfb2003-01-28 23:13:10 +00001897 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
danielk1977f9d64d22004-06-19 08:18:07 +00001898 rc = sqlite3_create_function(db, argv[2], -1, SQLITE_UTF8, 0,
danielk1977d8123362004-06-12 09:25:12 +00001899 testFunc, 0, 0);
drh6cbe1f12002-07-01 00:31:36 +00001900 if( rc!=0 ){
danielk1977f20b21c2004-05-31 23:56:42 +00001901 Tcl_AppendResult(interp, sqlite3ErrStr(rc), 0);
drh6cbe1f12002-07-01 00:31:36 +00001902 return TCL_ERROR;
1903 }
drhc60d0442004-09-30 13:43:13 +00001904 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
drh6cbe1f12002-07-01 00:31:36 +00001905 return TCL_OK;
1906}
1907
1908/*
danielk1977106bb232004-05-21 10:08:53 +00001909** Usage: sqlite3_finalize STMT
drhb86ccfb2003-01-28 23:13:10 +00001910**
danielk1977106bb232004-05-21 10:08:53 +00001911** Finalize a statement handle.
drhb86ccfb2003-01-28 23:13:10 +00001912*/
1913static int test_finalize(
danielk1977106bb232004-05-21 10:08:53 +00001914 void * clientData,
1915 Tcl_Interp *interp,
1916 int objc,
1917 Tcl_Obj *CONST objv[]
drhb86ccfb2003-01-28 23:13:10 +00001918){
danielk1977106bb232004-05-21 10:08:53 +00001919 sqlite3_stmt *pStmt;
drhb86ccfb2003-01-28 23:13:10 +00001920 int rc;
drhdddb2f22007-01-03 23:37:28 +00001921 sqlite3 *db = 0;
danielk1977106bb232004-05-21 10:08:53 +00001922
1923 if( objc!=2 ){
1924 Tcl_AppendResult(interp, "wrong # args: should be \"",
1925 Tcl_GetStringFromObj(objv[0], 0), " <STMT>", 0);
drhb86ccfb2003-01-28 23:13:10 +00001926 return TCL_ERROR;
1927 }
danielk1977106bb232004-05-21 10:08:53 +00001928
1929 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
1930
danielk19774397de52005-01-12 12:44:03 +00001931 if( pStmt ){
1932 db = StmtToDb(pStmt);
1933 }
danielk1977fc57d7b2004-05-26 02:04:57 +00001934 rc = sqlite3_finalize(pStmt);
drh4f0c5872007-03-26 22:05:01 +00001935 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
danielk19774397de52005-01-12 12:44:03 +00001936 if( db && sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
danielk1977106bb232004-05-21 10:08:53 +00001937 return TCL_OK;
1938}
1939
1940/*
1941** Usage: sqlite3_reset STMT
1942**
danielk1977261919c2005-12-06 12:52:59 +00001943** Reset a statement handle.
danielk1977106bb232004-05-21 10:08:53 +00001944*/
1945static int test_reset(
1946 void * clientData,
1947 Tcl_Interp *interp,
1948 int objc,
1949 Tcl_Obj *CONST objv[]
1950){
1951 sqlite3_stmt *pStmt;
1952 int rc;
1953
1954 if( objc!=2 ){
1955 Tcl_AppendResult(interp, "wrong # args: should be \"",
1956 Tcl_GetStringFromObj(objv[0], 0), " <STMT>", 0);
1957 return TCL_ERROR;
1958 }
1959
1960 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
1961
danielk1977fc57d7b2004-05-26 02:04:57 +00001962 rc = sqlite3_reset(pStmt);
danielk1977261919c2005-12-06 12:52:59 +00001963 if( pStmt && sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ){
1964 return TCL_ERROR;
1965 }
drh4f0c5872007-03-26 22:05:01 +00001966 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
danielk1977261919c2005-12-06 12:52:59 +00001967/*
danielk1977106bb232004-05-21 10:08:53 +00001968 if( rc ){
drhb86ccfb2003-01-28 23:13:10 +00001969 return TCL_ERROR;
1970 }
danielk1977261919c2005-12-06 12:52:59 +00001971*/
drhb86ccfb2003-01-28 23:13:10 +00001972 return TCL_OK;
1973}
1974
drh5a387052003-01-11 14:19:51 +00001975/*
drhd89bd002005-01-22 03:03:54 +00001976** Usage: sqlite3_expired STMT
1977**
1978** Return TRUE if a recompilation of the statement is recommended.
1979*/
1980static int test_expired(
1981 void * clientData,
1982 Tcl_Interp *interp,
1983 int objc,
1984 Tcl_Obj *CONST objv[]
1985){
1986 sqlite3_stmt *pStmt;
1987 if( objc!=2 ){
1988 Tcl_AppendResult(interp, "wrong # args: should be \"",
1989 Tcl_GetStringFromObj(objv[0], 0), " <STMT>", 0);
1990 return TCL_ERROR;
1991 }
1992 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
1993 Tcl_SetObjResult(interp, Tcl_NewBooleanObj(sqlite3_expired(pStmt)));
1994 return TCL_OK;
1995}
1996
1997/*
drhf8db1bc2005-04-22 02:38:37 +00001998** Usage: sqlite3_transfer_bindings FROMSTMT TOSTMT
1999**
2000** Transfer all bindings from FROMSTMT over to TOSTMT
2001*/
2002static int test_transfer_bind(
2003 void * clientData,
2004 Tcl_Interp *interp,
2005 int objc,
2006 Tcl_Obj *CONST objv[]
2007){
2008 sqlite3_stmt *pStmt1, *pStmt2;
2009 if( objc!=3 ){
2010 Tcl_AppendResult(interp, "wrong # args: should be \"",
2011 Tcl_GetStringFromObj(objv[0], 0), " FROM-STMT TO-STMT", 0);
2012 return TCL_ERROR;
2013 }
2014 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt1)) return TCL_ERROR;
2015 if( getStmtPointer(interp, Tcl_GetString(objv[2]), &pStmt2)) return TCL_ERROR;
2016 Tcl_SetObjResult(interp,
2017 Tcl_NewIntObj(sqlite3_transfer_bindings(pStmt1,pStmt2)));
2018 return TCL_OK;
2019}
2020
2021/*
danielk1977fbcd5852004-06-15 02:44:18 +00002022** Usage: sqlite3_changes DB
drh50457892003-09-06 01:10:47 +00002023**
danielk1977fbcd5852004-06-15 02:44:18 +00002024** Return the number of changes made to the database by the last SQL
2025** execution.
drh50457892003-09-06 01:10:47 +00002026*/
danielk1977fbcd5852004-06-15 02:44:18 +00002027static int test_changes(
2028 void * clientData,
2029 Tcl_Interp *interp,
2030 int objc,
2031 Tcl_Obj *CONST objv[]
2032){
2033 sqlite3 *db;
2034 if( objc!=2 ){
2035 Tcl_AppendResult(interp, "wrong # args: should be \"",
2036 Tcl_GetString(objv[0]), " DB", 0);
2037 return TCL_ERROR;
2038 }
2039 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
2040 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_changes(db)));
2041 return TCL_OK;
2042}
drh50457892003-09-06 01:10:47 +00002043
2044/*
drh7c972de2003-09-06 22:18:07 +00002045** This is the "static_bind_value" that variables are bound to when
danielk19776f8a5032004-05-10 10:34:51 +00002046** the FLAG option of sqlite3_bind is "static"
drh50457892003-09-06 01:10:47 +00002047*/
drh7c972de2003-09-06 22:18:07 +00002048static char *sqlite_static_bind_value = 0;
drhf0313812006-09-04 15:53:53 +00002049static int sqlite_static_bind_nbyte = 0;
drh7c972de2003-09-06 22:18:07 +00002050
2051/*
danielk19776f8a5032004-05-10 10:34:51 +00002052** Usage: sqlite3_bind VM IDX VALUE FLAGS
drh7c972de2003-09-06 22:18:07 +00002053**
2054** Sets the value of the IDX-th occurance of "?" in the original SQL
2055** string. VALUE is the new value. If FLAGS=="null" then VALUE is
2056** ignored and the value is set to NULL. If FLAGS=="static" then
2057** the value is set to the value of a static variable named
2058** "sqlite_static_bind_value". If FLAGS=="normal" then a copy
drhbf8aa2a2005-12-02 02:44:05 +00002059** of the VALUE is made. If FLAGS=="blob10" then a VALUE is ignored
2060** an a 10-byte blob "abc\000xyz\000pq" is inserted.
drh7c972de2003-09-06 22:18:07 +00002061*/
2062static int test_bind(
drh50457892003-09-06 01:10:47 +00002063 void *NotUsed,
2064 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
2065 int argc, /* Number of arguments */
2066 char **argv /* Text of each argument */
2067){
danielk1977fc57d7b2004-05-26 02:04:57 +00002068 sqlite3_stmt *pStmt;
drh50457892003-09-06 01:10:47 +00002069 int rc;
drh7c972de2003-09-06 22:18:07 +00002070 int idx;
2071 if( argc!=5 ){
drh50457892003-09-06 01:10:47 +00002072 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
drh7c972de2003-09-06 22:18:07 +00002073 " VM IDX VALUE (null|static|normal)\"", 0);
drh50457892003-09-06 01:10:47 +00002074 return TCL_ERROR;
2075 }
danielk1977fc57d7b2004-05-26 02:04:57 +00002076 if( getStmtPointer(interp, argv[1], &pStmt) ) return TCL_ERROR;
drh7c972de2003-09-06 22:18:07 +00002077 if( Tcl_GetInt(interp, argv[2], &idx) ) return TCL_ERROR;
2078 if( strcmp(argv[4],"null")==0 ){
danielk1977fc57d7b2004-05-26 02:04:57 +00002079 rc = sqlite3_bind_null(pStmt, idx);
drh7c972de2003-09-06 22:18:07 +00002080 }else if( strcmp(argv[4],"static")==0 ){
danielk1977fc57d7b2004-05-26 02:04:57 +00002081 rc = sqlite3_bind_text(pStmt, idx, sqlite_static_bind_value, -1, 0);
drhf0313812006-09-04 15:53:53 +00002082 }else if( strcmp(argv[4],"static-nbytes")==0 ){
2083 rc = sqlite3_bind_text(pStmt, idx, sqlite_static_bind_value,
2084 sqlite_static_bind_nbyte, 0);
drh7c972de2003-09-06 22:18:07 +00002085 }else if( strcmp(argv[4],"normal")==0 ){
danielk1977d8123362004-06-12 09:25:12 +00002086 rc = sqlite3_bind_text(pStmt, idx, argv[3], -1, SQLITE_TRANSIENT);
drhbf8aa2a2005-12-02 02:44:05 +00002087 }else if( strcmp(argv[4],"blob10")==0 ){
2088 rc = sqlite3_bind_text(pStmt, idx, "abc\000xyz\000pq", 10, SQLITE_STATIC);
drh7c972de2003-09-06 22:18:07 +00002089 }else{
2090 Tcl_AppendResult(interp, "4th argument should be "
2091 "\"null\" or \"static\" or \"normal\"", 0);
2092 return TCL_ERROR;
2093 }
drhc60d0442004-09-30 13:43:13 +00002094 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
drh50457892003-09-06 01:10:47 +00002095 if( rc ){
2096 char zBuf[50];
2097 sprintf(zBuf, "(%d) ", rc);
danielk1977f20b21c2004-05-31 23:56:42 +00002098 Tcl_AppendResult(interp, zBuf, sqlite3ErrStr(rc), 0);
drh50457892003-09-06 01:10:47 +00002099 return TCL_ERROR;
2100 }
2101 return TCL_OK;
2102}
2103
drh5436dc22004-11-14 04:04:17 +00002104#ifndef SQLITE_OMIT_UTF16
danielk19774e6af132004-06-10 14:01:08 +00002105/*
2106** Usage: add_test_collate <db ptr> <utf8> <utf16le> <utf16be>
2107**
2108** This function is used to test that SQLite selects the correct collation
2109** sequence callback when multiple versions (for different text encodings)
2110** are available.
2111**
2112** Calling this routine registers the collation sequence "test_collate"
2113** with database handle <db>. The second argument must be a list of three
2114** boolean values. If the first is true, then a version of test_collate is
2115** registered for UTF-8, if the second is true, a version is registered for
2116** UTF-16le, if the third is true, a UTF-16be version is available.
2117** Previous versions of test_collate are deleted.
2118**
2119** The collation sequence test_collate is implemented by calling the
2120** following TCL script:
2121**
2122** "test_collate <enc> <lhs> <rhs>"
2123**
2124** The <lhs> and <rhs> are the two values being compared, encoded in UTF-8.
2125** The <enc> parameter is the encoding of the collation function that
2126** SQLite selected to call. The TCL test script implements the
2127** "test_collate" proc.
2128**
2129** Note that this will only work with one intepreter at a time, as the
2130** interp pointer to use when evaluating the TCL script is stored in
2131** pTestCollateInterp.
2132*/
2133static Tcl_Interp* pTestCollateInterp;
2134static int test_collate_func(
2135 void *pCtx,
2136 int nA, const void *zA,
2137 int nB, const void *zB
2138){
2139 Tcl_Interp *i = pTestCollateInterp;
2140 int encin = (int)pCtx;
2141 int res;
drh4db38a72005-09-01 12:16:28 +00002142 int n;
danielk19774e6af132004-06-10 14:01:08 +00002143
2144 sqlite3_value *pVal;
2145 Tcl_Obj *pX;
2146
2147 pX = Tcl_NewStringObj("test_collate", -1);
2148 Tcl_IncrRefCount(pX);
2149
2150 switch( encin ){
2151 case SQLITE_UTF8:
2152 Tcl_ListObjAppendElement(i,pX,Tcl_NewStringObj("UTF-8",-1));
2153 break;
2154 case SQLITE_UTF16LE:
2155 Tcl_ListObjAppendElement(i,pX,Tcl_NewStringObj("UTF-16LE",-1));
2156 break;
2157 case SQLITE_UTF16BE:
2158 Tcl_ListObjAppendElement(i,pX,Tcl_NewStringObj("UTF-16BE",-1));
2159 break;
2160 default:
2161 assert(0);
2162 }
2163
2164 pVal = sqlite3ValueNew();
danielk1977bfd6cce2004-06-18 04:24:54 +00002165 sqlite3ValueSetStr(pVal, nA, zA, encin, SQLITE_STATIC);
drh4db38a72005-09-01 12:16:28 +00002166 n = sqlite3_value_bytes(pVal);
drh03d847e2005-12-09 20:21:58 +00002167 Tcl_ListObjAppendElement(i,pX,
2168 Tcl_NewStringObj((char*)sqlite3_value_text(pVal),n));
danielk1977bfd6cce2004-06-18 04:24:54 +00002169 sqlite3ValueSetStr(pVal, nB, zB, encin, SQLITE_STATIC);
drh4db38a72005-09-01 12:16:28 +00002170 n = sqlite3_value_bytes(pVal);
drh03d847e2005-12-09 20:21:58 +00002171 Tcl_ListObjAppendElement(i,pX,
2172 Tcl_NewStringObj((char*)sqlite3_value_text(pVal),n));
danielk19774e6af132004-06-10 14:01:08 +00002173 sqlite3ValueFree(pVal);
2174
2175 Tcl_EvalObjEx(i, pX, 0);
2176 Tcl_DecrRefCount(pX);
2177 Tcl_GetIntFromObj(i, Tcl_GetObjResult(i), &res);
2178 return res;
2179}
2180static int test_collate(
2181 void * clientData,
2182 Tcl_Interp *interp,
2183 int objc,
2184 Tcl_Obj *CONST objv[]
2185){
2186 sqlite3 *db;
2187 int val;
danielk1977312d6b32004-06-29 13:18:23 +00002188 sqlite3_value *pVal;
drhc60d0442004-09-30 13:43:13 +00002189 int rc;
danielk19774e6af132004-06-10 14:01:08 +00002190
2191 if( objc!=5 ) goto bad_args;
2192 pTestCollateInterp = interp;
2193 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
2194
2195 if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[2], &val) ) return TCL_ERROR;
drhc60d0442004-09-30 13:43:13 +00002196 rc = sqlite3_create_collation(db, "test_collate", SQLITE_UTF8,
2197 (void *)SQLITE_UTF8, val?test_collate_func:0);
2198 if( rc==SQLITE_OK ){
2199 if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[3], &val) ) return TCL_ERROR;
2200 rc = sqlite3_create_collation(db, "test_collate", SQLITE_UTF16LE,
2201 (void *)SQLITE_UTF16LE, val?test_collate_func:0);
2202 if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[4], &val) ) return TCL_ERROR;
danielk1977312d6b32004-06-29 13:18:23 +00002203
danielk19779a30cf62006-01-18 04:26:07 +00002204#ifdef SQLITE_MEMDEBUG
2205 if( sqlite3_iMallocFail>0 ){
2206 sqlite3_iMallocFail++;
2207 }
2208#endif
drhc60d0442004-09-30 13:43:13 +00002209 pVal = sqlite3ValueNew();
2210 sqlite3ValueSetStr(pVal, -1, "test_collate", SQLITE_UTF8, SQLITE_STATIC);
danielk19779a30cf62006-01-18 04:26:07 +00002211 rc = sqlite3_create_collation16(db,
2212 sqlite3ValueText(pVal, SQLITE_UTF16NATIVE), SQLITE_UTF16BE,
2213 (void *)SQLITE_UTF16BE, val?test_collate_func:0);
drhc60d0442004-09-30 13:43:13 +00002214 sqlite3ValueFree(pVal);
2215 }
2216 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
danielk19779a30cf62006-01-18 04:26:07 +00002217
2218 if( rc!=SQLITE_OK ){
2219 Tcl_AppendResult(interp, sqlite3TestErrorName(rc), 0);
2220 return TCL_ERROR;
2221 }
danielk19774e6af132004-06-10 14:01:08 +00002222 return TCL_OK;
2223
2224bad_args:
2225 Tcl_AppendResult(interp, "wrong # args: should be \"",
2226 Tcl_GetStringFromObj(objv[0], 0), " <DB> <utf8> <utf16le> <utf16be>", 0);
2227 return TCL_ERROR;
2228}
2229
drh268803a2005-12-14 20:11:30 +00002230/*
2231** When the collation needed callback is invoked, record the name of
2232** the requested collating function here. The recorded name is linked
2233** to a TCL variable and used to make sure that the requested collation
2234** name is correct.
2235*/
2236static char zNeededCollation[200];
2237static char *pzNeededCollation = zNeededCollation;
2238
2239
2240/*
2241** Called when a collating sequence is needed. Registered using
2242** sqlite3_collation_needed16().
2243*/
danielk1977312d6b32004-06-29 13:18:23 +00002244static void test_collate_needed_cb(
2245 void *pCtx,
2246 sqlite3 *db,
2247 int eTextRep,
drh268803a2005-12-14 20:11:30 +00002248 const void *pName
danielk1977312d6b32004-06-29 13:18:23 +00002249){
danielk197714db2662006-01-09 16:12:04 +00002250 int enc = ENC(db);
drh268803a2005-12-14 20:11:30 +00002251 int i;
2252 char *z;
2253 for(z = (char*)pName, i=0; *z || z[1]; z++){
2254 if( *z ) zNeededCollation[i++] = *z;
2255 }
2256 zNeededCollation[i] = 0;
danielk1977312d6b32004-06-29 13:18:23 +00002257 sqlite3_create_collation(
danielk197714db2662006-01-09 16:12:04 +00002258 db, "test_collate", ENC(db), (void *)enc, test_collate_func);
danielk1977312d6b32004-06-29 13:18:23 +00002259}
2260
2261/*
2262** Usage: add_test_collate_needed DB
2263*/
2264static int test_collate_needed(
2265 void * clientData,
2266 Tcl_Interp *interp,
2267 int objc,
2268 Tcl_Obj *CONST objv[]
2269){
2270 sqlite3 *db;
drhc60d0442004-09-30 13:43:13 +00002271 int rc;
danielk1977312d6b32004-06-29 13:18:23 +00002272
2273 if( objc!=2 ) goto bad_args;
2274 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
drhc60d0442004-09-30 13:43:13 +00002275 rc = sqlite3_collation_needed16(db, 0, test_collate_needed_cb);
drh268803a2005-12-14 20:11:30 +00002276 zNeededCollation[0] = 0;
drhc60d0442004-09-30 13:43:13 +00002277 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
danielk1977312d6b32004-06-29 13:18:23 +00002278 return TCL_OK;
2279
2280bad_args:
2281 Tcl_WrongNumArgs(interp, 1, objv, "DB");
2282 return TCL_ERROR;
2283}
drh7d9bd4e2006-02-16 18:16:36 +00002284
2285/*
2286** tclcmd: add_alignment_test_collations DB
2287**
2288** Add two new collating sequences to the database DB
2289**
2290** utf16_aligned
2291** utf16_unaligned
2292**
2293** Both collating sequences use the same sort order as BINARY.
2294** The only difference is that the utf16_aligned collating
2295** sequence is declared with the SQLITE_UTF16_ALIGNED flag.
2296** Both collating functions increment the unaligned utf16 counter
2297** whenever they see a string that begins on an odd byte boundary.
2298*/
2299static int unaligned_string_counter = 0;
2300static int alignmentCollFunc(
2301 void *NotUsed,
2302 int nKey1, const void *pKey1,
2303 int nKey2, const void *pKey2
2304){
2305 int rc, n;
2306 n = nKey1<nKey2 ? nKey1 : nKey2;
2307 if( nKey1>0 && 1==(1&(int)pKey1) ) unaligned_string_counter++;
2308 if( nKey2>0 && 1==(1&(int)pKey2) ) unaligned_string_counter++;
2309 rc = memcmp(pKey1, pKey2, n);
2310 if( rc==0 ){
2311 rc = nKey1 - nKey2;
2312 }
2313 return rc;
2314}
2315static int add_alignment_test_collations(
2316 void * clientData,
2317 Tcl_Interp *interp,
2318 int objc,
2319 Tcl_Obj *CONST objv[]
2320){
2321 sqlite3 *db;
2322 if( objc>=2 ){
2323 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
2324 sqlite3_create_collation(db, "utf16_unaligned",
2325 SQLITE_UTF16,
2326 0, alignmentCollFunc);
2327 sqlite3_create_collation(db, "utf16_aligned",
2328 SQLITE_UTF16 | SQLITE_UTF16_ALIGNED,
2329 0, alignmentCollFunc);
2330 }
2331 return SQLITE_OK;
2332}
2333#endif /* !defined(SQLITE_OMIT_UTF16) */
danielk1977312d6b32004-06-29 13:18:23 +00002334
danielk1977c8e9a2d2004-06-25 12:08:46 +00002335/*
2336** Usage: add_test_function <db ptr> <utf8> <utf16le> <utf16be>
2337**
2338** This function is used to test that SQLite selects the correct user
2339** function callback when multiple versions (for different text encodings)
2340** are available.
2341**
2342** Calling this routine registers up to three versions of the user function
2343** "test_function" with database handle <db>. If the second argument is
2344** true, then a version of test_function is registered for UTF-8, if the
2345** third is true, a version is registered for UTF-16le, if the fourth is
2346** true, a UTF-16be version is available. Previous versions of
2347** test_function are deleted.
2348**
2349** The user function is implemented by calling the following TCL script:
2350**
2351** "test_function <enc> <arg>"
2352**
2353** Where <enc> is one of UTF-8, UTF-16LE or UTF16BE, and <arg> is the
2354** single argument passed to the SQL function. The value returned by
2355** the TCL script is used as the return value of the SQL function. It
2356** is passed to SQLite using UTF-16BE for a UTF-8 test_function(), UTF-8
2357** for a UTF-16LE test_function(), and UTF-16LE for an implementation that
2358** prefers UTF-16BE.
2359*/
drh5436dc22004-11-14 04:04:17 +00002360#ifndef SQLITE_OMIT_UTF16
danielk1977c8e9a2d2004-06-25 12:08:46 +00002361static void test_function_utf8(
2362 sqlite3_context *pCtx,
2363 int nArg,
2364 sqlite3_value **argv
2365){
2366 Tcl_Interp *interp;
2367 Tcl_Obj *pX;
2368 sqlite3_value *pVal;
2369 interp = (Tcl_Interp *)sqlite3_user_data(pCtx);
2370 pX = Tcl_NewStringObj("test_function", -1);
2371 Tcl_IncrRefCount(pX);
2372 Tcl_ListObjAppendElement(interp, pX, Tcl_NewStringObj("UTF-8", -1));
2373 Tcl_ListObjAppendElement(interp, pX,
drh03d847e2005-12-09 20:21:58 +00002374 Tcl_NewStringObj((char*)sqlite3_value_text(argv[0]), -1));
danielk1977c8e9a2d2004-06-25 12:08:46 +00002375 Tcl_EvalObjEx(interp, pX, 0);
2376 Tcl_DecrRefCount(pX);
2377 sqlite3_result_text(pCtx, Tcl_GetStringResult(interp), -1, SQLITE_TRANSIENT);
2378 pVal = sqlite3ValueNew();
2379 sqlite3ValueSetStr(pVal, -1, Tcl_GetStringResult(interp),
2380 SQLITE_UTF8, SQLITE_STATIC);
2381 sqlite3_result_text16be(pCtx, sqlite3_value_text16be(pVal),
2382 -1, SQLITE_TRANSIENT);
2383 sqlite3ValueFree(pVal);
2384}
2385static void test_function_utf16le(
2386 sqlite3_context *pCtx,
2387 int nArg,
2388 sqlite3_value **argv
2389){
2390 Tcl_Interp *interp;
2391 Tcl_Obj *pX;
2392 sqlite3_value *pVal;
2393 interp = (Tcl_Interp *)sqlite3_user_data(pCtx);
2394 pX = Tcl_NewStringObj("test_function", -1);
2395 Tcl_IncrRefCount(pX);
2396 Tcl_ListObjAppendElement(interp, pX, Tcl_NewStringObj("UTF-16LE", -1));
2397 Tcl_ListObjAppendElement(interp, pX,
drh03d847e2005-12-09 20:21:58 +00002398 Tcl_NewStringObj((char*)sqlite3_value_text(argv[0]), -1));
danielk1977c8e9a2d2004-06-25 12:08:46 +00002399 Tcl_EvalObjEx(interp, pX, 0);
2400 Tcl_DecrRefCount(pX);
2401 pVal = sqlite3ValueNew();
2402 sqlite3ValueSetStr(pVal, -1, Tcl_GetStringResult(interp),
2403 SQLITE_UTF8, SQLITE_STATIC);
drh03d847e2005-12-09 20:21:58 +00002404 sqlite3_result_text(pCtx,(char*)sqlite3_value_text(pVal),-1,SQLITE_TRANSIENT);
danielk1977c8e9a2d2004-06-25 12:08:46 +00002405 sqlite3ValueFree(pVal);
2406}
2407static void test_function_utf16be(
2408 sqlite3_context *pCtx,
2409 int nArg,
2410 sqlite3_value **argv
2411){
2412 Tcl_Interp *interp;
2413 Tcl_Obj *pX;
2414 sqlite3_value *pVal;
2415 interp = (Tcl_Interp *)sqlite3_user_data(pCtx);
2416 pX = Tcl_NewStringObj("test_function", -1);
2417 Tcl_IncrRefCount(pX);
2418 Tcl_ListObjAppendElement(interp, pX, Tcl_NewStringObj("UTF-16BE", -1));
2419 Tcl_ListObjAppendElement(interp, pX,
drh03d847e2005-12-09 20:21:58 +00002420 Tcl_NewStringObj((char*)sqlite3_value_text(argv[0]), -1));
danielk1977c8e9a2d2004-06-25 12:08:46 +00002421 Tcl_EvalObjEx(interp, pX, 0);
2422 Tcl_DecrRefCount(pX);
2423 pVal = sqlite3ValueNew();
2424 sqlite3ValueSetStr(pVal, -1, Tcl_GetStringResult(interp),
2425 SQLITE_UTF8, SQLITE_STATIC);
2426 sqlite3_result_text16le(pCtx, sqlite3_value_text16le(pVal),
2427 -1, SQLITE_TRANSIENT);
2428 sqlite3ValueFree(pVal);
2429}
drh5436dc22004-11-14 04:04:17 +00002430#endif /* SQLITE_OMIT_UTF16 */
danielk1977c8e9a2d2004-06-25 12:08:46 +00002431static int test_function(
2432 void * clientData,
2433 Tcl_Interp *interp,
2434 int objc,
2435 Tcl_Obj *CONST objv[]
2436){
drh5436dc22004-11-14 04:04:17 +00002437#ifndef SQLITE_OMIT_UTF16
danielk1977c8e9a2d2004-06-25 12:08:46 +00002438 sqlite3 *db;
2439 int val;
2440
2441 if( objc!=5 ) goto bad_args;
2442 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
2443
2444 if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[2], &val) ) return TCL_ERROR;
2445 if( val ){
2446 sqlite3_create_function(db, "test_function", 1, SQLITE_UTF8,
2447 interp, test_function_utf8, 0, 0);
2448 }
2449 if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[3], &val) ) return TCL_ERROR;
2450 if( val ){
2451 sqlite3_create_function(db, "test_function", 1, SQLITE_UTF16LE,
2452 interp, test_function_utf16le, 0, 0);
2453 }
2454 if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[4], &val) ) return TCL_ERROR;
2455 if( val ){
2456 sqlite3_create_function(db, "test_function", 1, SQLITE_UTF16BE,
2457 interp, test_function_utf16be, 0, 0);
2458 }
2459
2460 return TCL_OK;
2461bad_args:
2462 Tcl_AppendResult(interp, "wrong # args: should be \"",
2463 Tcl_GetStringFromObj(objv[0], 0), " <DB> <utf8> <utf16le> <utf16be>", 0);
drh5436dc22004-11-14 04:04:17 +00002464#endif /* SQLITE_OMIT_UTF16 */
danielk1977c8e9a2d2004-06-25 12:08:46 +00002465 return TCL_ERROR;
2466}
2467
danielk1977312d6b32004-06-29 13:18:23 +00002468/*
2469** Usage: test_errstr <err code>
2470**
2471** Test that the english language string equivalents for sqlite error codes
2472** are sane. The parameter is an integer representing an sqlite error code.
2473** The result is a list of two elements, the string representation of the
2474** error code and the english language explanation.
2475*/
2476static int test_errstr(
2477 void * clientData,
2478 Tcl_Interp *interp,
2479 int objc,
2480 Tcl_Obj *CONST objv[]
2481){
2482 char *zCode;
2483 int i;
2484 if( objc!=1 ){
2485 Tcl_WrongNumArgs(interp, 1, objv, "<error code>");
2486 }
2487
2488 zCode = Tcl_GetString(objv[1]);
2489 for(i=0; i<200; i++){
drh4f0c5872007-03-26 22:05:01 +00002490 if( 0==strcmp(t1ErrorName(i), zCode) ) break;
danielk1977312d6b32004-06-29 13:18:23 +00002491 }
2492 Tcl_SetResult(interp, (char *)sqlite3ErrStr(i), 0);
2493 return TCL_OK;
2494}
2495
drh50457892003-09-06 01:10:47 +00002496/*
drh99ee3602003-02-16 19:13:36 +00002497** Usage: breakpoint
2498**
2499** This routine exists for one purpose - to provide a place to put a
2500** breakpoint with GDB that can be triggered using TCL code. The use
2501** for this is when a particular test fails on (say) the 1485th iteration.
2502** In the TCL test script, we can add code like this:
2503**
2504** if {$i==1485} breakpoint
2505**
2506** Then run testfixture in the debugger and wait for the breakpoint to
2507** fire. Then additional breakpoints can be set to trace down the bug.
2508*/
2509static int test_breakpoint(
2510 void *NotUsed,
2511 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
2512 int argc, /* Number of arguments */
2513 char **argv /* Text of each argument */
2514){
2515 return TCL_OK; /* Do nothing */
2516}
2517
drh241db312004-06-22 12:46:53 +00002518/*
drhb026e052007-05-02 01:34:31 +00002519** Usage: sqlite3_bind_zeroblob STMT IDX N
2520**
2521** Test the sqlite3_bind_zeroblob interface. STMT is a prepared statement.
2522** IDX is the index of a wildcard in the prepared statement. This command
2523** binds a N-byte zero-filled BLOB to the wildcard.
2524*/
2525static int test_bind_zeroblob(
2526 void * clientData,
2527 Tcl_Interp *interp,
2528 int objc,
2529 Tcl_Obj *CONST objv[]
2530){
2531 sqlite3_stmt *pStmt;
2532 int idx;
2533 int n;
2534 int rc;
2535
2536 if( objc!=4 ){
2537 Tcl_AppendResult(interp, "wrong # args: should be \"",
2538 Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE", 0);
2539 return TCL_ERROR;
2540 }
2541
2542 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2543 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
2544 if( Tcl_GetIntFromObj(interp, objv[3], &n) ) return TCL_ERROR;
2545
2546 rc = sqlite3_bind_zeroblob(pStmt, idx, n);
2547 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
2548 if( rc!=SQLITE_OK ){
2549 return TCL_ERROR;
2550 }
2551
2552 return TCL_OK;
2553}
2554
2555/*
drh241db312004-06-22 12:46:53 +00002556** Usage: sqlite3_bind_int STMT N VALUE
2557**
2558** Test the sqlite3_bind_int interface. STMT is a prepared statement.
2559** N is the index of a wildcard in the prepared statement. This command
2560** binds a 32-bit integer VALUE to that wildcard.
2561*/
2562static int test_bind_int(
danielk197751e3d8e2004-05-20 01:12:34 +00002563 void * clientData,
2564 Tcl_Interp *interp,
2565 int objc,
2566 Tcl_Obj *CONST objv[]
2567){
2568 sqlite3_stmt *pStmt;
2569 int idx;
2570 int value;
2571 int rc;
2572
2573 if( objc!=4 ){
2574 Tcl_AppendResult(interp, "wrong # args: should be \"",
drh241db312004-06-22 12:46:53 +00002575 Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE", 0);
danielk197751e3d8e2004-05-20 01:12:34 +00002576 return TCL_ERROR;
2577 }
2578
2579 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2580 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
2581 if( Tcl_GetIntFromObj(interp, objv[3], &value) ) return TCL_ERROR;
2582
danielk1977c572ef72004-05-27 09:28:41 +00002583 rc = sqlite3_bind_int(pStmt, idx, value);
drhc60d0442004-09-30 13:43:13 +00002584 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
danielk197751e3d8e2004-05-20 01:12:34 +00002585 if( rc!=SQLITE_OK ){
2586 return TCL_ERROR;
2587 }
2588
2589 return TCL_OK;
2590}
2591
drh241db312004-06-22 12:46:53 +00002592
2593/*
2594** Usage: sqlite3_bind_int64 STMT N VALUE
2595**
2596** Test the sqlite3_bind_int64 interface. STMT is a prepared statement.
2597** N is the index of a wildcard in the prepared statement. This command
2598** binds a 64-bit integer VALUE to that wildcard.
2599*/
danielk197751e3d8e2004-05-20 01:12:34 +00002600static int test_bind_int64(
2601 void * clientData,
2602 Tcl_Interp *interp,
2603 int objc,
2604 Tcl_Obj *CONST objv[]
2605){
2606 sqlite3_stmt *pStmt;
2607 int idx;
2608 i64 value;
2609 int rc;
2610
2611 if( objc!=4 ){
2612 Tcl_AppendResult(interp, "wrong # args: should be \"",
drh241db312004-06-22 12:46:53 +00002613 Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE", 0);
danielk197751e3d8e2004-05-20 01:12:34 +00002614 return TCL_ERROR;
2615 }
2616
2617 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2618 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
2619 if( Tcl_GetWideIntFromObj(interp, objv[3], &value) ) return TCL_ERROR;
2620
2621 rc = sqlite3_bind_int64(pStmt, idx, value);
drhc60d0442004-09-30 13:43:13 +00002622 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
danielk197751e3d8e2004-05-20 01:12:34 +00002623 if( rc!=SQLITE_OK ){
2624 return TCL_ERROR;
2625 }
2626
2627 return TCL_OK;
2628}
2629
drh241db312004-06-22 12:46:53 +00002630
2631/*
2632** Usage: sqlite3_bind_double STMT N VALUE
2633**
2634** Test the sqlite3_bind_double interface. STMT is a prepared statement.
2635** N is the index of a wildcard in the prepared statement. This command
2636** binds a 64-bit integer VALUE to that wildcard.
2637*/
danielk197751e3d8e2004-05-20 01:12:34 +00002638static int test_bind_double(
2639 void * clientData,
2640 Tcl_Interp *interp,
2641 int objc,
2642 Tcl_Obj *CONST objv[]
2643){
2644 sqlite3_stmt *pStmt;
2645 int idx;
2646 double value;
2647 int rc;
2648
2649 if( objc!=4 ){
2650 Tcl_AppendResult(interp, "wrong # args: should be \"",
drh241db312004-06-22 12:46:53 +00002651 Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE", 0);
danielk197751e3d8e2004-05-20 01:12:34 +00002652 return TCL_ERROR;
2653 }
2654
2655 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2656 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
2657 if( Tcl_GetDoubleFromObj(interp, objv[3], &value) ) return TCL_ERROR;
2658
2659 rc = sqlite3_bind_double(pStmt, idx, value);
drhc60d0442004-09-30 13:43:13 +00002660 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
danielk197751e3d8e2004-05-20 01:12:34 +00002661 if( rc!=SQLITE_OK ){
2662 return TCL_ERROR;
2663 }
2664
2665 return TCL_OK;
2666}
2667
drh241db312004-06-22 12:46:53 +00002668/*
2669** Usage: sqlite3_bind_null STMT N
2670**
2671** Test the sqlite3_bind_null interface. STMT is a prepared statement.
2672** N is the index of a wildcard in the prepared statement. This command
2673** binds a NULL to the wildcard.
2674*/
danielk197751e3d8e2004-05-20 01:12:34 +00002675static int test_bind_null(
2676 void * clientData,
2677 Tcl_Interp *interp,
2678 int objc,
2679 Tcl_Obj *CONST objv[]
2680){
2681 sqlite3_stmt *pStmt;
2682 int idx;
2683 int rc;
2684
2685 if( objc!=3 ){
2686 Tcl_AppendResult(interp, "wrong # args: should be \"",
drh241db312004-06-22 12:46:53 +00002687 Tcl_GetStringFromObj(objv[0], 0), " STMT N", 0);
danielk197751e3d8e2004-05-20 01:12:34 +00002688 return TCL_ERROR;
2689 }
2690
2691 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2692 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
2693
2694 rc = sqlite3_bind_null(pStmt, idx);
drhc60d0442004-09-30 13:43:13 +00002695 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
danielk197751e3d8e2004-05-20 01:12:34 +00002696 if( rc!=SQLITE_OK ){
2697 return TCL_ERROR;
2698 }
2699
2700 return TCL_OK;
2701}
2702
drh241db312004-06-22 12:46:53 +00002703/*
2704** Usage: sqlite3_bind_text STMT N STRING BYTES
2705**
2706** Test the sqlite3_bind_text interface. STMT is a prepared statement.
2707** N is the index of a wildcard in the prepared statement. This command
2708** binds a UTF-8 string STRING to the wildcard. The string is BYTES bytes
2709** long.
2710*/
danielk197751e3d8e2004-05-20 01:12:34 +00002711static int test_bind_text(
2712 void * clientData,
2713 Tcl_Interp *interp,
2714 int objc,
2715 Tcl_Obj *CONST objv[]
2716){
2717 sqlite3_stmt *pStmt;
2718 int idx;
2719 int bytes;
2720 char *value;
2721 int rc;
2722
2723 if( objc!=5 ){
2724 Tcl_AppendResult(interp, "wrong # args: should be \"",
drh241db312004-06-22 12:46:53 +00002725 Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE BYTES", 0);
danielk197751e3d8e2004-05-20 01:12:34 +00002726 return TCL_ERROR;
2727 }
2728
2729 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2730 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
2731 value = Tcl_GetString(objv[3]);
2732 if( Tcl_GetIntFromObj(interp, objv[4], &bytes) ) return TCL_ERROR;
2733
danielk1977d8123362004-06-12 09:25:12 +00002734 rc = sqlite3_bind_text(pStmt, idx, value, bytes, SQLITE_TRANSIENT);
drhc60d0442004-09-30 13:43:13 +00002735 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
danielk197751e3d8e2004-05-20 01:12:34 +00002736 if( rc!=SQLITE_OK ){
drh473d1792005-06-06 17:54:55 +00002737 Tcl_AppendResult(interp, sqlite3TestErrorName(rc), 0);
danielk197751e3d8e2004-05-20 01:12:34 +00002738 return TCL_ERROR;
2739 }
2740
2741 return TCL_OK;
2742}
2743
drh241db312004-06-22 12:46:53 +00002744/*
danielk1977161fb792006-01-24 10:58:21 +00002745** Usage: sqlite3_bind_text16 ?-static? STMT N STRING BYTES
drh241db312004-06-22 12:46:53 +00002746**
2747** Test the sqlite3_bind_text16 interface. STMT is a prepared statement.
2748** N is the index of a wildcard in the prepared statement. This command
2749** binds a UTF-16 string STRING to the wildcard. The string is BYTES bytes
2750** long.
2751*/
danielk197751e3d8e2004-05-20 01:12:34 +00002752static int test_bind_text16(
2753 void * clientData,
2754 Tcl_Interp *interp,
2755 int objc,
2756 Tcl_Obj *CONST objv[]
2757){
drh5436dc22004-11-14 04:04:17 +00002758#ifndef SQLITE_OMIT_UTF16
danielk197751e3d8e2004-05-20 01:12:34 +00002759 sqlite3_stmt *pStmt;
2760 int idx;
2761 int bytes;
2762 char *value;
2763 int rc;
2764
danielk1977161fb792006-01-24 10:58:21 +00002765 void (*xDel)() = (objc==6?SQLITE_STATIC:SQLITE_TRANSIENT);
2766 Tcl_Obj *oStmt = objv[objc-4];
2767 Tcl_Obj *oN = objv[objc-3];
2768 Tcl_Obj *oString = objv[objc-2];
2769 Tcl_Obj *oBytes = objv[objc-1];
2770
2771 if( objc!=5 && objc!=6){
danielk197751e3d8e2004-05-20 01:12:34 +00002772 Tcl_AppendResult(interp, "wrong # args: should be \"",
drh241db312004-06-22 12:46:53 +00002773 Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE BYTES", 0);
danielk197751e3d8e2004-05-20 01:12:34 +00002774 return TCL_ERROR;
2775 }
2776
danielk1977161fb792006-01-24 10:58:21 +00002777 if( getStmtPointer(interp, Tcl_GetString(oStmt), &pStmt) ) return TCL_ERROR;
2778 if( Tcl_GetIntFromObj(interp, oN, &idx) ) return TCL_ERROR;
2779 value = (char*)Tcl_GetByteArrayFromObj(oString, 0);
2780 if( Tcl_GetIntFromObj(interp, oBytes, &bytes) ) return TCL_ERROR;
danielk197751e3d8e2004-05-20 01:12:34 +00002781
danielk1977161fb792006-01-24 10:58:21 +00002782 rc = sqlite3_bind_text16(pStmt, idx, (void *)value, bytes, xDel);
drhc60d0442004-09-30 13:43:13 +00002783 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
danielk197751e3d8e2004-05-20 01:12:34 +00002784 if( rc!=SQLITE_OK ){
2785 return TCL_ERROR;
2786 }
2787
drh5436dc22004-11-14 04:04:17 +00002788#endif /* SQLITE_OMIT_UTF16 */
danielk197751e3d8e2004-05-20 01:12:34 +00002789 return TCL_OK;
2790}
2791
drh241db312004-06-22 12:46:53 +00002792/*
2793** Usage: sqlite3_bind_blob STMT N DATA BYTES
2794**
2795** Test the sqlite3_bind_blob interface. STMT is a prepared statement.
2796** N is the index of a wildcard in the prepared statement. This command
2797** binds a BLOB to the wildcard. The BLOB is BYTES bytes in size.
2798*/
danielk197751e3d8e2004-05-20 01:12:34 +00002799static int test_bind_blob(
2800 void * clientData,
2801 Tcl_Interp *interp,
2802 int objc,
2803 Tcl_Obj *CONST objv[]
2804){
2805 sqlite3_stmt *pStmt;
2806 int idx;
2807 int bytes;
2808 char *value;
2809 int rc;
2810
2811 if( objc!=5 ){
2812 Tcl_AppendResult(interp, "wrong # args: should be \"",
drh241db312004-06-22 12:46:53 +00002813 Tcl_GetStringFromObj(objv[0], 0), " STMT N DATA BYTES", 0);
danielk197751e3d8e2004-05-20 01:12:34 +00002814 return TCL_ERROR;
2815 }
2816
2817 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2818 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
2819 value = Tcl_GetString(objv[3]);
danielk197749e46432004-05-27 13:55:27 +00002820 if( Tcl_GetIntFromObj(interp, objv[4], &bytes) ) return TCL_ERROR;
danielk197751e3d8e2004-05-20 01:12:34 +00002821
danielk1977d8123362004-06-12 09:25:12 +00002822 rc = sqlite3_bind_blob(pStmt, idx, value, bytes, SQLITE_TRANSIENT);
drhc60d0442004-09-30 13:43:13 +00002823 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
danielk197751e3d8e2004-05-20 01:12:34 +00002824 if( rc!=SQLITE_OK ){
2825 return TCL_ERROR;
2826 }
2827
2828 return TCL_OK;
2829}
2830
drh99ee3602003-02-16 19:13:36 +00002831/*
drh75f6a032004-07-15 14:15:00 +00002832** Usage: sqlite3_bind_parameter_count STMT
2833**
2834** Return the number of wildcards in the given statement.
2835*/
2836static int test_bind_parameter_count(
2837 void * clientData,
2838 Tcl_Interp *interp,
2839 int objc,
2840 Tcl_Obj *CONST objv[]
2841){
2842 sqlite3_stmt *pStmt;
2843
2844 if( objc!=2 ){
2845 Tcl_WrongNumArgs(interp, 1, objv, "STMT");
2846 return TCL_ERROR;
2847 }
2848 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2849 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_bind_parameter_count(pStmt)));
2850 return TCL_OK;
2851}
2852
2853/*
drh895d7472004-08-20 16:02:39 +00002854** Usage: sqlite3_bind_parameter_name STMT N
2855**
2856** Return the name of the Nth wildcard. The first wildcard is 1.
2857** An empty string is returned if N is out of range or if the wildcard
2858** is nameless.
2859*/
2860static int test_bind_parameter_name(
2861 void * clientData,
2862 Tcl_Interp *interp,
2863 int objc,
2864 Tcl_Obj *CONST objv[]
2865){
2866 sqlite3_stmt *pStmt;
2867 int i;
2868
2869 if( objc!=3 ){
2870 Tcl_WrongNumArgs(interp, 1, objv, "STMT N");
2871 return TCL_ERROR;
2872 }
2873 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2874 if( Tcl_GetIntFromObj(interp, objv[2], &i) ) return TCL_ERROR;
2875 Tcl_SetObjResult(interp,
2876 Tcl_NewStringObj(sqlite3_bind_parameter_name(pStmt,i),-1)
2877 );
2878 return TCL_OK;
2879}
2880
2881/*
drhfa6bc002004-09-07 16:19:52 +00002882** Usage: sqlite3_bind_parameter_index STMT NAME
2883**
2884** Return the index of the wildcard called NAME. Return 0 if there is
2885** no such wildcard.
2886*/
2887static int test_bind_parameter_index(
2888 void * clientData,
2889 Tcl_Interp *interp,
2890 int objc,
2891 Tcl_Obj *CONST objv[]
2892){
2893 sqlite3_stmt *pStmt;
2894
2895 if( objc!=3 ){
2896 Tcl_WrongNumArgs(interp, 1, objv, "STMT NAME");
2897 return TCL_ERROR;
2898 }
2899 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2900 Tcl_SetObjResult(interp,
2901 Tcl_NewIntObj(
2902 sqlite3_bind_parameter_index(pStmt,Tcl_GetString(objv[2]))
2903 )
2904 );
2905 return TCL_OK;
2906}
2907
2908/*
danielk1977600dd0b2005-01-20 01:14:23 +00002909** Usage: sqlite3_clear_bindings STMT
2910**
2911*/
danielk1977600dd0b2005-01-20 01:14:23 +00002912static int test_clear_bindings(
2913 void * clientData,
2914 Tcl_Interp *interp,
2915 int objc,
2916 Tcl_Obj *CONST objv[]
2917){
2918 sqlite3_stmt *pStmt;
2919
2920 if( objc!=2 ){
2921 Tcl_WrongNumArgs(interp, 1, objv, "STMT");
2922 return TCL_ERROR;
2923 }
2924 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2925 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_clear_bindings(pStmt)));
2926 return TCL_OK;
2927}
drhf9cb7f52006-06-27 20:06:44 +00002928
2929/*
2930** Usage: sqlite3_sleep MILLISECONDS
2931*/
2932static int test_sleep(
2933 void * clientData,
2934 Tcl_Interp *interp,
2935 int objc,
2936 Tcl_Obj *CONST objv[]
2937){
2938 int ms;
2939
2940 if( objc!=2 ){
2941 Tcl_WrongNumArgs(interp, 1, objv, "MILLISECONDS");
2942 return TCL_ERROR;
2943 }
2944 if( Tcl_GetIntFromObj(interp, objv[1], &ms) ){
2945 return TCL_ERROR;
2946 }
2947 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_sleep(ms)));
2948 return TCL_OK;
2949}
danielk1977600dd0b2005-01-20 01:14:23 +00002950
2951/*
danielk19776622cce2004-05-20 11:00:52 +00002952** Usage: sqlite3_errcode DB
2953**
2954** Return the string representation of the most recent sqlite3_* API
2955** error code. e.g. "SQLITE_ERROR".
2956*/
2957static int test_errcode(
2958 void * clientData,
2959 Tcl_Interp *interp,
2960 int objc,
2961 Tcl_Obj *CONST objv[]
2962){
2963 sqlite3 *db;
drh4ac285a2006-09-15 07:28:50 +00002964 int rc;
2965 char zBuf[30];
danielk19776622cce2004-05-20 11:00:52 +00002966
2967 if( objc!=2 ){
2968 Tcl_AppendResult(interp, "wrong # args: should be \"",
2969 Tcl_GetString(objv[0]), " DB", 0);
2970 return TCL_ERROR;
2971 }
2972 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
drh4ac285a2006-09-15 07:28:50 +00002973 rc = sqlite3_errcode(db);
2974 if( (rc&0xff)==rc ){
2975 zBuf[0] = 0;
2976 }else{
2977 sprintf(zBuf,"+%d", rc>>8);
2978 }
drh4f0c5872007-03-26 22:05:01 +00002979 Tcl_AppendResult(interp, (char *)t1ErrorName(rc), zBuf, 0);
danielk19776622cce2004-05-20 11:00:52 +00002980 return TCL_OK;
2981}
2982
2983/*
2984** Usage: test_errmsg DB
2985**
2986** Returns the UTF-8 representation of the error message string for the
2987** most recent sqlite3_* API call.
2988*/
2989static int test_errmsg(
2990 void * clientData,
2991 Tcl_Interp *interp,
2992 int objc,
2993 Tcl_Obj *CONST objv[]
2994){
drh9bb575f2004-09-06 17:24:11 +00002995 sqlite3 *db;
danielk19776622cce2004-05-20 11:00:52 +00002996 const char *zErr;
2997
2998 if( objc!=2 ){
2999 Tcl_AppendResult(interp, "wrong # args: should be \"",
3000 Tcl_GetString(objv[0]), " DB", 0);
3001 return TCL_ERROR;
3002 }
3003 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
3004
3005 zErr = sqlite3_errmsg(db);
3006 Tcl_SetObjResult(interp, Tcl_NewStringObj(zErr, -1));
3007 return TCL_OK;
3008}
3009
3010/*
3011** Usage: test_errmsg16 DB
3012**
3013** Returns the UTF-16 representation of the error message string for the
3014** most recent sqlite3_* API call. This is a byte array object at the TCL
3015** level, and it includes the 0x00 0x00 terminator bytes at the end of the
3016** UTF-16 string.
3017*/
3018static int test_errmsg16(
3019 void * clientData,
3020 Tcl_Interp *interp,
3021 int objc,
3022 Tcl_Obj *CONST objv[]
3023){
drh5436dc22004-11-14 04:04:17 +00003024#ifndef SQLITE_OMIT_UTF16
drh9bb575f2004-09-06 17:24:11 +00003025 sqlite3 *db;
danielk19776622cce2004-05-20 11:00:52 +00003026 const void *zErr;
danielk1977950f0542006-01-18 05:51:57 +00003027 int bytes = 0;
danielk19776622cce2004-05-20 11:00:52 +00003028
3029 if( objc!=2 ){
3030 Tcl_AppendResult(interp, "wrong # args: should be \"",
3031 Tcl_GetString(objv[0]), " DB", 0);
3032 return TCL_ERROR;
3033 }
3034 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
3035
3036 zErr = sqlite3_errmsg16(db);
danielk1977950f0542006-01-18 05:51:57 +00003037 if( zErr ){
3038 bytes = sqlite3utf16ByteLen(zErr, -1);
3039 }
danielk19776622cce2004-05-20 11:00:52 +00003040 Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(zErr, bytes));
drh5436dc22004-11-14 04:04:17 +00003041#endif /* SQLITE_OMIT_UTF16 */
danielk19776622cce2004-05-20 11:00:52 +00003042 return TCL_OK;
3043}
3044
3045/*
3046** Usage: sqlite3_prepare DB sql bytes tailvar
3047**
3048** Compile up to <bytes> bytes of the supplied SQL string <sql> using
3049** database handle <DB>. The parameter <tailval> is the name of a global
3050** variable that is set to the unused portion of <sql> (if any). A
3051** STMT handle is returned.
3052*/
3053static int test_prepare(
3054 void * clientData,
3055 Tcl_Interp *interp,
3056 int objc,
3057 Tcl_Obj *CONST objv[]
3058){
3059 sqlite3 *db;
3060 const char *zSql;
3061 int bytes;
3062 const char *zTail = 0;
3063 sqlite3_stmt *pStmt = 0;
3064 char zBuf[50];
danielk19774ad17132004-05-21 01:47:26 +00003065 int rc;
danielk19776622cce2004-05-20 11:00:52 +00003066
3067 if( objc!=5 ){
3068 Tcl_AppendResult(interp, "wrong # args: should be \"",
3069 Tcl_GetString(objv[0]), " DB sql bytes tailvar", 0);
3070 return TCL_ERROR;
3071 }
3072 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
3073 zSql = Tcl_GetString(objv[2]);
3074 if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR;
3075
danielk19774ad17132004-05-21 01:47:26 +00003076 rc = sqlite3_prepare(db, zSql, bytes, &pStmt, &zTail);
drhc60d0442004-09-30 13:43:13 +00003077 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
danielk19776622cce2004-05-20 11:00:52 +00003078 if( zTail ){
3079 if( bytes>=0 ){
3080 bytes = bytes - (zTail-zSql);
3081 }
3082 Tcl_ObjSetVar2(interp, objv[4], 0, Tcl_NewStringObj(zTail, bytes), 0);
3083 }
danielk19774ad17132004-05-21 01:47:26 +00003084 if( rc!=SQLITE_OK ){
3085 assert( pStmt==0 );
3086 sprintf(zBuf, "(%d) ", rc);
3087 Tcl_AppendResult(interp, zBuf, sqlite3_errmsg(db), 0);
3088 return TCL_ERROR;
3089 }
danielk19776622cce2004-05-20 11:00:52 +00003090
danielk19774ad17132004-05-21 01:47:26 +00003091 if( pStmt ){
drh64b1bea2006-01-15 02:30:57 +00003092 if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR;
danielk19774ad17132004-05-21 01:47:26 +00003093 Tcl_AppendResult(interp, zBuf, 0);
3094 }
danielk19776622cce2004-05-20 11:00:52 +00003095 return TCL_OK;
3096}
3097
3098/*
drhb900aaf2006-11-09 00:24:53 +00003099** Usage: sqlite3_prepare_v2 DB sql bytes tailvar
3100**
3101** Compile up to <bytes> bytes of the supplied SQL string <sql> using
3102** database handle <DB>. The parameter <tailval> is the name of a global
3103** variable that is set to the unused portion of <sql> (if any). A
3104** STMT handle is returned.
3105*/
3106static int test_prepare_v2(
3107 void * clientData,
3108 Tcl_Interp *interp,
3109 int objc,
3110 Tcl_Obj *CONST objv[]
3111){
3112 sqlite3 *db;
3113 const char *zSql;
3114 int bytes;
3115 const char *zTail = 0;
3116 sqlite3_stmt *pStmt = 0;
3117 char zBuf[50];
3118 int rc;
3119
3120 if( objc!=5 ){
3121 Tcl_AppendResult(interp, "wrong # args: should be \"",
3122 Tcl_GetString(objv[0]), " DB sql bytes tailvar", 0);
3123 return TCL_ERROR;
3124 }
3125 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
3126 zSql = Tcl_GetString(objv[2]);
3127 if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR;
3128
3129 rc = sqlite3_prepare_v2(db, zSql, bytes, &pStmt, &zTail);
danielk19777e29e952007-04-19 11:09:01 +00003130 assert(rc==SQLITE_OK || pStmt==0);
drhb900aaf2006-11-09 00:24:53 +00003131 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
3132 if( zTail ){
3133 if( bytes>=0 ){
3134 bytes = bytes - (zTail-zSql);
3135 }
3136 Tcl_ObjSetVar2(interp, objv[4], 0, Tcl_NewStringObj(zTail, bytes), 0);
3137 }
3138 if( rc!=SQLITE_OK ){
3139 assert( pStmt==0 );
3140 sprintf(zBuf, "(%d) ", rc);
3141 Tcl_AppendResult(interp, zBuf, sqlite3_errmsg(db), 0);
3142 return TCL_ERROR;
3143 }
3144
3145 if( pStmt ){
3146 if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR;
3147 Tcl_AppendResult(interp, zBuf, 0);
3148 }
3149 return TCL_OK;
3150}
3151
3152/*
3153** Usage: sqlite3_prepare16 DB sql bytes tailvar
danielk19776622cce2004-05-20 11:00:52 +00003154**
3155** Compile up to <bytes> bytes of the supplied SQL string <sql> using
3156** database handle <DB>. The parameter <tailval> is the name of a global
3157** variable that is set to the unused portion of <sql> (if any). A
3158** STMT handle is returned.
3159*/
3160static int test_prepare16(
3161 void * clientData,
3162 Tcl_Interp *interp,
3163 int objc,
3164 Tcl_Obj *CONST objv[]
3165){
drh5436dc22004-11-14 04:04:17 +00003166#ifndef SQLITE_OMIT_UTF16
danielk19776622cce2004-05-20 11:00:52 +00003167 sqlite3 *db;
3168 const void *zSql;
3169 const void *zTail = 0;
3170 Tcl_Obj *pTail = 0;
3171 sqlite3_stmt *pStmt = 0;
drhc60d0442004-09-30 13:43:13 +00003172 char zBuf[50];
3173 int rc;
danielk19776622cce2004-05-20 11:00:52 +00003174 int bytes; /* The integer specified as arg 3 */
3175 int objlen; /* The byte-array length of arg 2 */
3176
3177 if( objc!=5 ){
3178 Tcl_AppendResult(interp, "wrong # args: should be \"",
3179 Tcl_GetString(objv[0]), " DB sql bytes tailvar", 0);
3180 return TCL_ERROR;
3181 }
3182 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
3183 zSql = Tcl_GetByteArrayFromObj(objv[2], &objlen);
3184 if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR;
3185
drhc60d0442004-09-30 13:43:13 +00003186 rc = sqlite3_prepare16(db, zSql, bytes, &pStmt, &zTail);
3187 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
3188 if( rc ){
danielk19776622cce2004-05-20 11:00:52 +00003189 return TCL_ERROR;
3190 }
3191
3192 if( zTail ){
3193 objlen = objlen - ((u8 *)zTail-(u8 *)zSql);
3194 }else{
3195 objlen = 0;
3196 }
3197 pTail = Tcl_NewByteArrayObj((u8 *)zTail, objlen);
3198 Tcl_IncrRefCount(pTail);
3199 Tcl_ObjSetVar2(interp, objv[4], 0, pTail, 0);
danielk19774ad17132004-05-21 01:47:26 +00003200 Tcl_DecrRefCount(pTail);
danielk19776622cce2004-05-20 11:00:52 +00003201
danielk19774ad17132004-05-21 01:47:26 +00003202 if( pStmt ){
drh64b1bea2006-01-15 02:30:57 +00003203 if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR;
danielk19774ad17132004-05-21 01:47:26 +00003204 }
danielk19776622cce2004-05-20 11:00:52 +00003205 Tcl_AppendResult(interp, zBuf, 0);
drh5436dc22004-11-14 04:04:17 +00003206#endif /* SQLITE_OMIT_UTF16 */
danielk19776622cce2004-05-20 11:00:52 +00003207 return TCL_OK;
3208}
3209
danielk19774ad17132004-05-21 01:47:26 +00003210/*
drhb900aaf2006-11-09 00:24:53 +00003211** Usage: sqlite3_prepare16_v2 DB sql bytes tailvar
3212**
3213** Compile up to <bytes> bytes of the supplied SQL string <sql> using
3214** database handle <DB>. The parameter <tailval> is the name of a global
3215** variable that is set to the unused portion of <sql> (if any). A
3216** STMT handle is returned.
3217*/
3218static int test_prepare16_v2(
3219 void * clientData,
3220 Tcl_Interp *interp,
3221 int objc,
3222 Tcl_Obj *CONST objv[]
3223){
3224#ifndef SQLITE_OMIT_UTF16
3225 sqlite3 *db;
3226 const void *zSql;
3227 const void *zTail = 0;
3228 Tcl_Obj *pTail = 0;
3229 sqlite3_stmt *pStmt = 0;
3230 char zBuf[50];
3231 int rc;
3232 int bytes; /* The integer specified as arg 3 */
3233 int objlen; /* The byte-array length of arg 2 */
3234
3235 if( objc!=5 ){
3236 Tcl_AppendResult(interp, "wrong # args: should be \"",
3237 Tcl_GetString(objv[0]), " DB sql bytes tailvar", 0);
3238 return TCL_ERROR;
3239 }
3240 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
3241 zSql = Tcl_GetByteArrayFromObj(objv[2], &objlen);
3242 if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR;
3243
3244 rc = sqlite3_prepare16_v2(db, zSql, bytes, &pStmt, &zTail);
3245 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
3246 if( rc ){
3247 return TCL_ERROR;
3248 }
3249
3250 if( zTail ){
3251 objlen = objlen - ((u8 *)zTail-(u8 *)zSql);
3252 }else{
3253 objlen = 0;
3254 }
3255 pTail = Tcl_NewByteArrayObj((u8 *)zTail, objlen);
3256 Tcl_IncrRefCount(pTail);
3257 Tcl_ObjSetVar2(interp, objv[4], 0, pTail, 0);
3258 Tcl_DecrRefCount(pTail);
3259
3260 if( pStmt ){
3261 if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR;
3262 }
3263 Tcl_AppendResult(interp, zBuf, 0);
3264#endif /* SQLITE_OMIT_UTF16 */
3265 return TCL_OK;
3266}
3267
3268/*
danielk19774ad17132004-05-21 01:47:26 +00003269** Usage: sqlite3_open filename ?options-list?
3270*/
3271static int test_open(
3272 void * clientData,
3273 Tcl_Interp *interp,
3274 int objc,
3275 Tcl_Obj *CONST objv[]
3276){
3277 const char *zFilename;
3278 sqlite3 *db;
3279 int rc;
3280 char zBuf[100];
3281
3282 if( objc!=3 && objc!=2 ){
3283 Tcl_AppendResult(interp, "wrong # args: should be \"",
3284 Tcl_GetString(objv[0]), " filename options-list", 0);
3285 return TCL_ERROR;
3286 }
3287
3288 zFilename = Tcl_GetString(objv[1]);
danielk19774f057f92004-06-08 00:02:33 +00003289 rc = sqlite3_open(zFilename, &db);
danielk19774ad17132004-05-21 01:47:26 +00003290
drh64b1bea2006-01-15 02:30:57 +00003291 if( sqlite3TestMakePointerStr(interp, zBuf, db) ) return TCL_ERROR;
danielk19774ad17132004-05-21 01:47:26 +00003292 Tcl_AppendResult(interp, zBuf, 0);
3293 return TCL_OK;
3294}
3295
3296/*
3297** Usage: sqlite3_open16 filename options
3298*/
3299static int test_open16(
3300 void * clientData,
3301 Tcl_Interp *interp,
3302 int objc,
3303 Tcl_Obj *CONST objv[]
3304){
drh5436dc22004-11-14 04:04:17 +00003305#ifndef SQLITE_OMIT_UTF16
danielk19774ad17132004-05-21 01:47:26 +00003306 const void *zFilename;
3307 sqlite3 *db;
3308 int rc;
3309 char zBuf[100];
3310
3311 if( objc!=3 ){
3312 Tcl_AppendResult(interp, "wrong # args: should be \"",
3313 Tcl_GetString(objv[0]), " filename options-list", 0);
3314 return TCL_ERROR;
3315 }
3316
3317 zFilename = Tcl_GetByteArrayFromObj(objv[1], 0);
danielk19774f057f92004-06-08 00:02:33 +00003318 rc = sqlite3_open16(zFilename, &db);
danielk19774ad17132004-05-21 01:47:26 +00003319
drh64b1bea2006-01-15 02:30:57 +00003320 if( sqlite3TestMakePointerStr(interp, zBuf, db) ) return TCL_ERROR;
danielk19774ad17132004-05-21 01:47:26 +00003321 Tcl_AppendResult(interp, zBuf, 0);
drh5436dc22004-11-14 04:04:17 +00003322#endif /* SQLITE_OMIT_UTF16 */
danielk19774ad17132004-05-21 01:47:26 +00003323 return TCL_OK;
3324}
drhd3d39e92004-05-20 22:16:29 +00003325
3326/*
danielk1977bc6ada42004-06-30 08:20:16 +00003327** Usage: sqlite3_complete16 <UTF-16 string>
3328**
3329** Return 1 if the supplied argument is a complete SQL statement, or zero
3330** otherwise.
3331*/
3332static int test_complete16(
3333 void * clientData,
3334 Tcl_Interp *interp,
3335 int objc,
3336 Tcl_Obj *CONST objv[]
3337){
drhccae6022005-02-26 17:31:26 +00003338#if !defined(SQLITE_OMIT_COMPLETE) && !defined(SQLITE_OMIT_UTF16)
danielk1977bc6ada42004-06-30 08:20:16 +00003339 char *zBuf;
3340
3341 if( objc!=2 ){
3342 Tcl_WrongNumArgs(interp, 1, objv, "<utf-16 sql>");
3343 return TCL_ERROR;
3344 }
3345
drh03d847e2005-12-09 20:21:58 +00003346 zBuf = (char*)Tcl_GetByteArrayFromObj(objv[1], 0);
danielk1977bc6ada42004-06-30 08:20:16 +00003347 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_complete16(zBuf)));
drhccae6022005-02-26 17:31:26 +00003348#endif /* SQLITE_OMIT_COMPLETE && SQLITE_OMIT_UTF16 */
danielk1977bc6ada42004-06-30 08:20:16 +00003349 return TCL_OK;
3350}
3351
3352/*
danielk1977106bb232004-05-21 10:08:53 +00003353** Usage: sqlite3_step STMT
3354**
3355** Advance the statement to the next row.
3356*/
danielk197717240fd2004-05-26 00:07:25 +00003357static int test_step(
danielk1977106bb232004-05-21 10:08:53 +00003358 void * clientData,
3359 Tcl_Interp *interp,
3360 int objc,
3361 Tcl_Obj *CONST objv[]
3362){
3363 sqlite3_stmt *pStmt;
3364 int rc;
3365
danielk1977e1cd9872004-05-22 10:33:04 +00003366 if( objc!=2 ){
danielk1977106bb232004-05-21 10:08:53 +00003367 Tcl_AppendResult(interp, "wrong # args: should be \"",
3368 Tcl_GetString(objv[0]), " STMT", 0);
3369 return TCL_ERROR;
3370 }
3371
3372 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
danielk197717240fd2004-05-26 00:07:25 +00003373 rc = sqlite3_step(pStmt);
danielk1977106bb232004-05-21 10:08:53 +00003374
danielk1977fbcd5852004-06-15 02:44:18 +00003375 /* if( rc!=SQLITE_DONE && rc!=SQLITE_ROW ) return TCL_ERROR; */
drh4f0c5872007-03-26 22:05:01 +00003376 Tcl_SetResult(interp, (char *)t1ErrorName(rc), 0);
danielk1977e1cd9872004-05-22 10:33:04 +00003377 return TCL_OK;
3378}
3379
3380/*
danielk197717240fd2004-05-26 00:07:25 +00003381** Usage: sqlite3_column_count STMT
3382**
3383** Return the number of columns returned by the sql statement STMT.
3384*/
3385static int test_column_count(
3386 void * clientData,
3387 Tcl_Interp *interp,
3388 int objc,
3389 Tcl_Obj *CONST objv[]
3390){
3391 sqlite3_stmt *pStmt;
3392
3393 if( objc!=2 ){
3394 Tcl_AppendResult(interp, "wrong # args: should be \"",
3395 Tcl_GetString(objv[0]), " STMT column", 0);
3396 return TCL_ERROR;
3397 }
3398
3399 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3400
3401 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_column_count(pStmt)));
3402 return TCL_OK;
3403}
3404
3405/*
danielk19773cf86062004-05-26 10:11:05 +00003406** Usage: sqlite3_column_type STMT column
3407**
3408** Return the type of the data in column 'column' of the current row.
3409*/
3410static int test_column_type(
3411 void * clientData,
3412 Tcl_Interp *interp,
3413 int objc,
3414 Tcl_Obj *CONST objv[]
3415){
3416 sqlite3_stmt *pStmt;
3417 int col;
3418 int tp;
3419
3420 if( objc!=3 ){
3421 Tcl_AppendResult(interp, "wrong # args: should be \"",
3422 Tcl_GetString(objv[0]), " STMT column", 0);
3423 return TCL_ERROR;
3424 }
3425
3426 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3427 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
3428
3429 tp = sqlite3_column_type(pStmt, col);
3430 switch( tp ){
drh9c054832004-05-31 18:51:57 +00003431 case SQLITE_INTEGER:
danielk19773cf86062004-05-26 10:11:05 +00003432 Tcl_SetResult(interp, "INTEGER", TCL_STATIC);
3433 break;
drh9c054832004-05-31 18:51:57 +00003434 case SQLITE_NULL:
danielk19773cf86062004-05-26 10:11:05 +00003435 Tcl_SetResult(interp, "NULL", TCL_STATIC);
3436 break;
drh9c054832004-05-31 18:51:57 +00003437 case SQLITE_FLOAT:
danielk19773cf86062004-05-26 10:11:05 +00003438 Tcl_SetResult(interp, "FLOAT", TCL_STATIC);
3439 break;
drh9c054832004-05-31 18:51:57 +00003440 case SQLITE_TEXT:
danielk19773cf86062004-05-26 10:11:05 +00003441 Tcl_SetResult(interp, "TEXT", TCL_STATIC);
3442 break;
drh9c054832004-05-31 18:51:57 +00003443 case SQLITE_BLOB:
danielk19773cf86062004-05-26 10:11:05 +00003444 Tcl_SetResult(interp, "BLOB", TCL_STATIC);
3445 break;
3446 default:
3447 assert(0);
3448 }
3449
3450 return TCL_OK;
3451}
3452
3453/*
danielk197704f2e682004-05-27 01:04:07 +00003454** Usage: sqlite3_column_int64 STMT column
danielk19773cf86062004-05-26 10:11:05 +00003455**
3456** Return the data in column 'column' of the current row cast as an
danielk197704f2e682004-05-27 01:04:07 +00003457** wide (64-bit) integer.
danielk19773cf86062004-05-26 10:11:05 +00003458*/
danielk197704f2e682004-05-27 01:04:07 +00003459static int test_column_int64(
danielk19773cf86062004-05-26 10:11:05 +00003460 void * clientData,
3461 Tcl_Interp *interp,
3462 int objc,
3463 Tcl_Obj *CONST objv[]
3464){
3465 sqlite3_stmt *pStmt;
3466 int col;
danielk197704f2e682004-05-27 01:04:07 +00003467 i64 iVal;
danielk19773cf86062004-05-26 10:11:05 +00003468
3469 if( objc!=3 ){
3470 Tcl_AppendResult(interp, "wrong # args: should be \"",
3471 Tcl_GetString(objv[0]), " STMT column", 0);
3472 return TCL_ERROR;
3473 }
3474
3475 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3476 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
3477
danielk197704f2e682004-05-27 01:04:07 +00003478 iVal = sqlite3_column_int64(pStmt, col);
3479 Tcl_SetObjResult(interp, Tcl_NewWideIntObj(iVal));
3480 return TCL_OK;
3481}
3482
3483/*
danielk1977ea61b2c2004-05-27 01:49:51 +00003484** Usage: sqlite3_column_blob STMT column
3485*/
3486static int test_column_blob(
3487 void * clientData,
3488 Tcl_Interp *interp,
3489 int objc,
3490 Tcl_Obj *CONST objv[]
3491){
3492 sqlite3_stmt *pStmt;
3493 int col;
3494
3495 int len;
danielk1977c572ef72004-05-27 09:28:41 +00003496 const void *pBlob;
danielk1977ea61b2c2004-05-27 01:49:51 +00003497
3498 if( objc!=3 ){
3499 Tcl_AppendResult(interp, "wrong # args: should be \"",
3500 Tcl_GetString(objv[0]), " STMT column", 0);
3501 return TCL_ERROR;
3502 }
3503
3504 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3505 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
3506
danielk1977ea61b2c2004-05-27 01:49:51 +00003507 len = sqlite3_column_bytes(pStmt, col);
drh9310ef22007-04-27 17:16:20 +00003508 pBlob = sqlite3_column_blob(pStmt, col);
danielk1977ea61b2c2004-05-27 01:49:51 +00003509 Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(pBlob, len));
3510 return TCL_OK;
3511}
3512
3513/*
danielk197704f2e682004-05-27 01:04:07 +00003514** Usage: sqlite3_column_double STMT column
3515**
3516** Return the data in column 'column' of the current row cast as a double.
3517*/
3518static int test_column_double(
3519 void * clientData,
3520 Tcl_Interp *interp,
3521 int objc,
3522 Tcl_Obj *CONST objv[]
3523){
3524 sqlite3_stmt *pStmt;
3525 int col;
3526 double rVal;
3527
3528 if( objc!=3 ){
3529 Tcl_AppendResult(interp, "wrong # args: should be \"",
3530 Tcl_GetString(objv[0]), " STMT column", 0);
3531 return TCL_ERROR;
3532 }
3533
3534 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3535 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
3536
3537 rVal = sqlite3_column_double(pStmt, col);
danielk1977c572ef72004-05-27 09:28:41 +00003538 Tcl_SetObjResult(interp, Tcl_NewDoubleObj(rVal));
danielk19773cf86062004-05-26 10:11:05 +00003539 return TCL_OK;
3540}
3541
3542/*
danielk197717240fd2004-05-26 00:07:25 +00003543** Usage: sqlite3_data_count STMT
3544**
3545** Return the number of columns returned by the sql statement STMT.
3546*/
3547static int test_data_count(
3548 void * clientData,
3549 Tcl_Interp *interp,
3550 int objc,
3551 Tcl_Obj *CONST objv[]
3552){
3553 sqlite3_stmt *pStmt;
3554
3555 if( objc!=2 ){
3556 Tcl_AppendResult(interp, "wrong # args: should be \"",
3557 Tcl_GetString(objv[0]), " STMT column", 0);
3558 return TCL_ERROR;
3559 }
3560
3561 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3562
3563 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_data_count(pStmt)));
3564 return TCL_OK;
3565}
3566
3567/*
danielk197704f2e682004-05-27 01:04:07 +00003568** Usage: sqlite3_column_text STMT column
3569**
3570** Usage: sqlite3_column_decltype STMT column
3571**
3572** Usage: sqlite3_column_name STMT column
3573*/
3574static int test_stmt_utf8(
drh241db312004-06-22 12:46:53 +00003575 void * clientData, /* Pointer to SQLite API function to be invoke */
danielk197704f2e682004-05-27 01:04:07 +00003576 Tcl_Interp *interp,
3577 int objc,
3578 Tcl_Obj *CONST objv[]
3579){
3580 sqlite3_stmt *pStmt;
3581 int col;
danielk1977c572ef72004-05-27 09:28:41 +00003582 const char *(*xFunc)(sqlite3_stmt*, int) = clientData;
danielk1977f93bbbe2004-05-27 10:30:52 +00003583 const char *zRet;
danielk197704f2e682004-05-27 01:04:07 +00003584
3585 if( objc!=3 ){
3586 Tcl_AppendResult(interp, "wrong # args: should be \"",
3587 Tcl_GetString(objv[0]), " STMT column", 0);
3588 return TCL_ERROR;
3589 }
3590
3591 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3592 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
danielk1977f93bbbe2004-05-27 10:30:52 +00003593 zRet = xFunc(pStmt, col);
3594 if( zRet ){
3595 Tcl_SetResult(interp, (char *)zRet, 0);
3596 }
danielk197704f2e682004-05-27 01:04:07 +00003597 return TCL_OK;
3598}
3599
danielk19776b456a22005-03-21 04:04:02 +00003600static int test_global_recover(
3601 void * clientData,
3602 Tcl_Interp *interp,
3603 int objc,
3604 Tcl_Obj *CONST objv[]
3605){
3606#ifndef SQLITE_OMIT_GLOBALRECOVER
3607 int rc;
3608 if( objc!=1 ){
3609 Tcl_WrongNumArgs(interp, 1, objv, "");
3610 return TCL_ERROR;
3611 }
3612 rc = sqlite3_global_recover();
drh4f0c5872007-03-26 22:05:01 +00003613 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
danielk19776b456a22005-03-21 04:04:02 +00003614#endif
3615 return TCL_OK;
3616}
3617
danielk197704f2e682004-05-27 01:04:07 +00003618/*
3619** Usage: sqlite3_column_text STMT column
3620**
3621** Usage: sqlite3_column_decltype STMT column
3622**
3623** Usage: sqlite3_column_name STMT column
3624*/
3625static int test_stmt_utf16(
drh241db312004-06-22 12:46:53 +00003626 void * clientData, /* Pointer to SQLite API function to be invoked */
danielk197704f2e682004-05-27 01:04:07 +00003627 Tcl_Interp *interp,
3628 int objc,
3629 Tcl_Obj *CONST objv[]
3630){
drh5436dc22004-11-14 04:04:17 +00003631#ifndef SQLITE_OMIT_UTF16
danielk197704f2e682004-05-27 01:04:07 +00003632 sqlite3_stmt *pStmt;
3633 int col;
3634 Tcl_Obj *pRet;
3635 const void *zName16;
danielk1977c572ef72004-05-27 09:28:41 +00003636 const void *(*xFunc)(sqlite3_stmt*, int) = clientData;
danielk197704f2e682004-05-27 01:04:07 +00003637
3638 if( objc!=3 ){
3639 Tcl_AppendResult(interp, "wrong # args: should be \"",
3640 Tcl_GetString(objv[0]), " STMT column", 0);
3641 return TCL_ERROR;
3642 }
3643
3644 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3645 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
3646
3647 zName16 = xFunc(pStmt, col);
danielk1977f93bbbe2004-05-27 10:30:52 +00003648 if( zName16 ){
3649 pRet = Tcl_NewByteArrayObj(zName16, sqlite3utf16ByteLen(zName16, -1)+2);
3650 Tcl_SetObjResult(interp, pRet);
3651 }
drh5436dc22004-11-14 04:04:17 +00003652#endif /* SQLITE_OMIT_UTF16 */
danielk197704f2e682004-05-27 01:04:07 +00003653
3654 return TCL_OK;
3655}
3656
3657/*
3658** Usage: sqlite3_column_int STMT column
3659**
3660** Usage: sqlite3_column_bytes STMT column
3661**
3662** Usage: sqlite3_column_bytes16 STMT column
3663**
3664*/
3665static int test_stmt_int(
drh241db312004-06-22 12:46:53 +00003666 void * clientData, /* Pointer to SQLite API function to be invoked */
danielk197704f2e682004-05-27 01:04:07 +00003667 Tcl_Interp *interp,
3668 int objc,
3669 Tcl_Obj *CONST objv[]
3670){
3671 sqlite3_stmt *pStmt;
3672 int col;
danielk1977c572ef72004-05-27 09:28:41 +00003673 int (*xFunc)(sqlite3_stmt*, int) = clientData;
danielk197704f2e682004-05-27 01:04:07 +00003674
3675 if( objc!=3 ){
3676 Tcl_AppendResult(interp, "wrong # args: should be \"",
3677 Tcl_GetString(objv[0]), " STMT column", 0);
3678 return TCL_ERROR;
3679 }
3680
3681 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
3682 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
3683
3684 Tcl_SetObjResult(interp, Tcl_NewIntObj(xFunc(pStmt, col)));
3685 return TCL_OK;
3686}
3687
danielk197744ee5bf2005-05-27 09:41:12 +00003688#ifndef SQLITE_OMIT_DISKIO
danielk19779a1d0ab2004-06-01 14:09:28 +00003689/*
3690** Usage: sqlite3OsOpenReadWrite <filename>
3691*/
3692static int test_sqlite3OsOpenReadWrite(
3693 void * clientData,
3694 Tcl_Interp *interp,
3695 int objc,
3696 Tcl_Obj *CONST objv[]
3697){
drh9cbe6352005-11-29 03:13:21 +00003698 OsFile *pFile;
danielk19779a1d0ab2004-06-01 14:09:28 +00003699 int rc;
3700 int dummy;
3701 char zBuf[100];
danielk197704f2e682004-05-27 01:04:07 +00003702
danielk19779a1d0ab2004-06-01 14:09:28 +00003703 if( objc!=2 ){
3704 Tcl_AppendResult(interp, "wrong # args: should be \"",
3705 Tcl_GetString(objv[0]), " filename", 0);
3706 return TCL_ERROR;
3707 }
3708
drh66560ad2006-01-06 14:32:19 +00003709 rc = sqlite3OsOpenReadWrite(Tcl_GetString(objv[1]), &pFile, &dummy);
danielk19779a1d0ab2004-06-01 14:09:28 +00003710 if( rc!=SQLITE_OK ){
drh4f0c5872007-03-26 22:05:01 +00003711 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
danielk19779a1d0ab2004-06-01 14:09:28 +00003712 return TCL_ERROR;
3713 }
drh64b1bea2006-01-15 02:30:57 +00003714 sqlite3TestMakePointerStr(interp, zBuf, pFile);
danielk19779a1d0ab2004-06-01 14:09:28 +00003715 Tcl_SetResult(interp, zBuf, 0);
3716 return TCL_ERROR;
3717}
3718
3719/*
3720** Usage: sqlite3OsClose <file handle>
3721*/
3722static int test_sqlite3OsClose(
3723 void * clientData,
3724 Tcl_Interp *interp,
3725 int objc,
3726 Tcl_Obj *CONST objv[]
3727){
drh9cbe6352005-11-29 03:13:21 +00003728 OsFile *pFile;
danielk19779a1d0ab2004-06-01 14:09:28 +00003729 int rc;
3730
3731 if( objc!=2 ){
3732 Tcl_AppendResult(interp, "wrong # args: should be \"",
3733 Tcl_GetString(objv[0]), " filehandle", 0);
3734 return TCL_ERROR;
3735 }
3736
3737 if( getFilePointer(interp, Tcl_GetString(objv[1]), &pFile) ){
3738 return TCL_ERROR;
3739 }
drh054889e2005-11-30 03:20:31 +00003740 rc = sqlite3OsClose(&pFile);
danielk19779a1d0ab2004-06-01 14:09:28 +00003741 if( rc!=SQLITE_OK ){
drh4f0c5872007-03-26 22:05:01 +00003742 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
danielk19779a1d0ab2004-06-01 14:09:28 +00003743 return TCL_ERROR;
3744 }
danielk19779a1d0ab2004-06-01 14:09:28 +00003745 return TCL_OK;
3746}
3747
3748/*
3749** Usage: sqlite3OsLock <file handle> <locktype>
3750*/
3751static int test_sqlite3OsLock(
3752 void * clientData,
3753 Tcl_Interp *interp,
3754 int objc,
3755 Tcl_Obj *CONST objv[]
3756){
3757 OsFile * pFile;
3758 int rc;
3759
3760 if( objc!=3 ){
3761 Tcl_AppendResult(interp, "wrong # args: should be \"",
3762 Tcl_GetString(objv[0]),
3763 " filehandle (SHARED|RESERVED|PENDING|EXCLUSIVE)", 0);
3764 return TCL_ERROR;
3765 }
3766
3767 if( getFilePointer(interp, Tcl_GetString(objv[1]), &pFile) ){
3768 return TCL_ERROR;
3769 }
3770
3771 if( 0==strcmp("SHARED", Tcl_GetString(objv[2])) ){
drh054889e2005-11-30 03:20:31 +00003772 rc = sqlite3OsLock(pFile, SHARED_LOCK);
danielk19779a1d0ab2004-06-01 14:09:28 +00003773 }
3774 else if( 0==strcmp("RESERVED", Tcl_GetString(objv[2])) ){
drh054889e2005-11-30 03:20:31 +00003775 rc = sqlite3OsLock(pFile, RESERVED_LOCK);
danielk19779a1d0ab2004-06-01 14:09:28 +00003776 }
3777 else if( 0==strcmp("PENDING", Tcl_GetString(objv[2])) ){
drh054889e2005-11-30 03:20:31 +00003778 rc = sqlite3OsLock(pFile, PENDING_LOCK);
danielk19779a1d0ab2004-06-01 14:09:28 +00003779 }
3780 else if( 0==strcmp("EXCLUSIVE", Tcl_GetString(objv[2])) ){
drh054889e2005-11-30 03:20:31 +00003781 rc = sqlite3OsLock(pFile, EXCLUSIVE_LOCK);
danielk19779a1d0ab2004-06-01 14:09:28 +00003782 }else{
3783 Tcl_AppendResult(interp, "wrong # args: should be \"",
3784 Tcl_GetString(objv[0]),
3785 " filehandle (SHARED|RESERVED|PENDING|EXCLUSIVE)", 0);
3786 return TCL_ERROR;
3787 }
3788
3789 if( rc!=SQLITE_OK ){
drh4f0c5872007-03-26 22:05:01 +00003790 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
danielk19779a1d0ab2004-06-01 14:09:28 +00003791 return TCL_ERROR;
3792 }
3793 return TCL_OK;
3794}
3795
3796/*
3797** Usage: sqlite3OsUnlock <file handle>
3798*/
3799static int test_sqlite3OsUnlock(
3800 void * clientData,
3801 Tcl_Interp *interp,
3802 int objc,
3803 Tcl_Obj *CONST objv[]
3804){
3805 OsFile * pFile;
3806 int rc;
3807
3808 if( objc!=2 ){
3809 Tcl_AppendResult(interp, "wrong # args: should be \"",
3810 Tcl_GetString(objv[0]), " filehandle", 0);
3811 return TCL_ERROR;
3812 }
3813
3814 if( getFilePointer(interp, Tcl_GetString(objv[1]), &pFile) ){
3815 return TCL_ERROR;
3816 }
drh054889e2005-11-30 03:20:31 +00003817 rc = sqlite3OsUnlock(pFile, NO_LOCK);
danielk19779a1d0ab2004-06-01 14:09:28 +00003818 if( rc!=SQLITE_OK ){
drh4f0c5872007-03-26 22:05:01 +00003819 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
danielk19779a1d0ab2004-06-01 14:09:28 +00003820 return TCL_ERROR;
3821 }
3822 return TCL_OK;
3823}
drhd3d39e92004-05-20 22:16:29 +00003824
drhab3f9fe2004-08-14 17:10:10 +00003825/*
3826** Usage: sqlite3OsTempFileName
3827*/
3828static int test_sqlite3OsTempFileName(
3829 void * clientData,
3830 Tcl_Interp *interp,
3831 int objc,
3832 Tcl_Obj *CONST objv[]
3833){
3834 char zFile[SQLITE_TEMPNAME_SIZE];
3835 int rc;
3836
drh66560ad2006-01-06 14:32:19 +00003837 rc = sqlite3OsTempFileName(zFile);
drhab3f9fe2004-08-14 17:10:10 +00003838 if( rc!=SQLITE_OK ){
drh4f0c5872007-03-26 22:05:01 +00003839 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
drhab3f9fe2004-08-14 17:10:10 +00003840 return TCL_ERROR;
3841 }
3842 Tcl_AppendResult(interp, zFile, 0);
3843 return TCL_OK;
3844}
danielk197744ee5bf2005-05-27 09:41:12 +00003845#endif
danielk1977d8123362004-06-12 09:25:12 +00003846
danielk19776622cce2004-05-20 11:00:52 +00003847/*
drhcacb2082005-01-11 15:28:33 +00003848** Usage: sqlite_set_magic DB MAGIC-NUMBER
3849**
3850** Set the db->magic value. This is used to test error recovery logic.
3851*/
3852static int sqlite_set_magic(
3853 void * clientData,
3854 Tcl_Interp *interp,
3855 int argc,
3856 char **argv
3857){
3858 sqlite3 *db;
3859 if( argc!=3 ){
3860 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
3861 " DB MAGIC", 0);
3862 return TCL_ERROR;
3863 }
3864 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
3865 if( strcmp(argv[2], "SQLITE_MAGIC_OPEN")==0 ){
3866 db->magic = SQLITE_MAGIC_OPEN;
3867 }else if( strcmp(argv[2], "SQLITE_MAGIC_CLOSED")==0 ){
3868 db->magic = SQLITE_MAGIC_CLOSED;
3869 }else if( strcmp(argv[2], "SQLITE_MAGIC_BUSY")==0 ){
3870 db->magic = SQLITE_MAGIC_BUSY;
3871 }else if( strcmp(argv[2], "SQLITE_MAGIC_ERROR")==0 ){
3872 db->magic = SQLITE_MAGIC_ERROR;
3873 }else if( Tcl_GetInt(interp, argv[2], &db->magic) ){
3874 return TCL_ERROR;
3875 }
3876 return TCL_OK;
3877}
3878
3879/*
drhc5cdca62005-01-11 16:54:14 +00003880** Usage: sqlite3_interrupt DB
3881**
3882** Trigger an interrupt on DB
3883*/
3884static int test_interrupt(
3885 void * clientData,
3886 Tcl_Interp *interp,
3887 int argc,
3888 char **argv
3889){
3890 sqlite3 *db;
3891 if( argc!=2 ){
3892 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " DB", 0);
3893 return TCL_ERROR;
3894 }
3895 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
3896 sqlite3_interrupt(db);
3897 return TCL_OK;
3898}
3899
drh79158e12005-09-06 21:40:45 +00003900static u8 *sqlite3_stack_baseline = 0;
3901
drhc5cdca62005-01-11 16:54:14 +00003902/*
drh79158e12005-09-06 21:40:45 +00003903** Fill the stack with a known bitpattern.
danielk1977600dd0b2005-01-20 01:14:23 +00003904*/
drh79158e12005-09-06 21:40:45 +00003905static void prepStack(void){
3906 int i;
3907 u32 bigBuf[65536];
3908 for(i=0; i<sizeof(bigBuf); i++) bigBuf[i] = 0xdeadbeef;
3909 sqlite3_stack_baseline = (u8*)&bigBuf[65536];
3910}
3911
3912/*
3913** Get the current stack depth. Used for debugging only.
3914*/
3915u64 sqlite3StackDepth(void){
3916 u8 x;
3917 return (u64)(sqlite3_stack_baseline - &x);
3918}
3919
3920/*
3921** Usage: sqlite3_stack_used DB SQL
3922**
3923** Try to measure the amount of stack space used by a call to sqlite3_exec
3924*/
3925static int test_stack_used(
danielk1977600dd0b2005-01-20 01:14:23 +00003926 void * clientData,
3927 Tcl_Interp *interp,
3928 int argc,
3929 char **argv
3930){
3931 sqlite3 *db;
drh79158e12005-09-06 21:40:45 +00003932 int i;
3933 if( argc!=3 ){
3934 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
3935 " DB SQL", 0);
danielk1977600dd0b2005-01-20 01:14:23 +00003936 return TCL_ERROR;
3937 }
drh79158e12005-09-06 21:40:45 +00003938 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
3939 prepStack();
drh37527852006-03-16 16:19:56 +00003940 (void)sqlite3_exec(db, argv[2], 0, 0, 0);
drh79158e12005-09-06 21:40:45 +00003941 for(i=65535; i>=0 && ((u32*)sqlite3_stack_baseline)[-i]==0xdeadbeef; i--){}
3942 Tcl_SetObjResult(interp, Tcl_NewIntObj(i*4));
danielk1977600dd0b2005-01-20 01:14:23 +00003943 return TCL_OK;
3944}
danielk1977600dd0b2005-01-20 01:14:23 +00003945
3946/*
danielk19779636c4e2005-01-25 04:27:54 +00003947** Usage: sqlite_delete_function DB function-name
3948**
3949** Delete the user function 'function-name' from database handle DB. It
3950** is assumed that the user function was created as UTF8, any number of
3951** arguments (the way the TCL interface does it).
3952*/
3953static int delete_function(
3954 void * clientData,
3955 Tcl_Interp *interp,
3956 int argc,
3957 char **argv
3958){
3959 int rc;
3960 sqlite3 *db;
3961 if( argc!=3 ){
3962 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
3963 " DB function-name", 0);
3964 return TCL_ERROR;
3965 }
3966 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
3967 rc = sqlite3_create_function(db, argv[2], -1, SQLITE_UTF8, 0, 0, 0, 0);
drh4f0c5872007-03-26 22:05:01 +00003968 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
danielk19779636c4e2005-01-25 04:27:54 +00003969 return TCL_OK;
3970}
3971
3972/*
3973** Usage: sqlite_delete_collation DB collation-name
3974**
3975** Delete the collation sequence 'collation-name' from database handle
3976** DB. It is assumed that the collation sequence was created as UTF8 (the
3977** way the TCL interface does it).
3978*/
3979static int delete_collation(
3980 void * clientData,
3981 Tcl_Interp *interp,
3982 int argc,
3983 char **argv
3984){
3985 int rc;
3986 sqlite3 *db;
3987 if( argc!=3 ){
3988 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
3989 " DB function-name", 0);
3990 return TCL_ERROR;
3991 }
3992 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
3993 rc = sqlite3_create_collation(db, argv[2], SQLITE_UTF8, 0, 0);
drh4f0c5872007-03-26 22:05:01 +00003994 Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
danielk19779636c4e2005-01-25 04:27:54 +00003995 return TCL_OK;
3996}
3997
3998/*
drh3e1d8e62005-05-26 16:23:34 +00003999** Usage: sqlite3_get_autocommit DB
4000**
4001** Return true if the database DB is currently in auto-commit mode.
4002** Return false if not.
4003*/
4004static int get_autocommit(
4005 void * clientData,
4006 Tcl_Interp *interp,
4007 int argc,
4008 char **argv
4009){
4010 char zBuf[30];
4011 sqlite3 *db;
4012 if( argc!=2 ){
4013 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
4014 " DB", 0);
4015 return TCL_ERROR;
4016 }
4017 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
4018 sprintf(zBuf, "%d", sqlite3_get_autocommit(db));
4019 Tcl_AppendResult(interp, zBuf, 0);
4020 return TCL_OK;
4021}
4022
4023/*
drh30867652006-07-06 10:59:57 +00004024** Usage: sqlite3_busy_timeout DB MS
4025**
4026** Set the busy timeout. This is more easily done using the timeout
4027** method of the TCL interface. But we need a way to test the case
4028** where it returns SQLITE_MISUSE.
4029*/
4030static int test_busy_timeout(
4031 void * clientData,
4032 Tcl_Interp *interp,
4033 int argc,
4034 char **argv
4035){
4036 int rc, ms;
4037 sqlite3 *db;
4038 if( argc!=3 ){
4039 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
4040 " DB", 0);
4041 return TCL_ERROR;
4042 }
4043 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
4044 if( Tcl_GetInt(interp, argv[2], &ms) ) return TCL_ERROR;
4045 rc = sqlite3_busy_timeout(db, ms);
4046 Tcl_AppendResult(interp, sqlite3TestErrorName(rc), 0);
4047 return TCL_OK;
4048}
4049
4050/*
drh92febd92004-08-20 18:34:20 +00004051** Usage: tcl_variable_type VARIABLENAME
4052**
4053** Return the name of the internal representation for the
4054** value of the given variable.
4055*/
4056static int tcl_variable_type(
4057 void * clientData,
4058 Tcl_Interp *interp,
4059 int objc,
4060 Tcl_Obj *CONST objv[]
4061){
4062 Tcl_Obj *pVar;
4063 if( objc!=2 ){
4064 Tcl_WrongNumArgs(interp, 1, objv, "VARIABLE");
4065 return TCL_ERROR;
4066 }
4067 pVar = Tcl_GetVar2Ex(interp, Tcl_GetString(objv[1]), 0, TCL_LEAVE_ERR_MSG);
4068 if( pVar==0 ) return TCL_ERROR;
4069 if( pVar->typePtr ){
4070 Tcl_SetObjResult(interp, Tcl_NewStringObj(pVar->typePtr->name, -1));
4071 }
4072 return TCL_OK;
4073}
4074
4075/*
drh6aafc292006-01-05 15:50:06 +00004076** Usage: sqlite3_release_memory ?N?
4077**
4078** Attempt to release memory currently held but not actually required.
4079** The integer N is the number of bytes we are trying to release. The
4080** return value is the amount of memory actually released.
4081*/
4082static int test_release_memory(
4083 void * clientData,
4084 Tcl_Interp *interp,
4085 int objc,
4086 Tcl_Obj *CONST objv[]
4087){
drh6f7adc82006-01-11 21:41:20 +00004088#if defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) && !defined(SQLITE_OMIT_DISKIO)
drh6aafc292006-01-05 15:50:06 +00004089 int N;
4090 int amt;
4091 if( objc!=1 && objc!=2 ){
4092 Tcl_WrongNumArgs(interp, 1, objv, "?N?");
4093 return TCL_ERROR;
4094 }
4095 if( objc==2 ){
4096 if( Tcl_GetIntFromObj(interp, objv[1], &N) ) return TCL_ERROR;
4097 }else{
4098 N = -1;
4099 }
4100 amt = sqlite3_release_memory(N);
4101 Tcl_SetObjResult(interp, Tcl_NewIntObj(amt));
4102#endif
4103 return TCL_OK;
4104}
4105
4106/*
4107** Usage: sqlite3_soft_heap_limit ?N?
4108**
4109** Query or set the soft heap limit for the current thread. The
4110** limit is only changed if the N is present. The previous limit
4111** is returned.
4112*/
4113static int test_soft_heap_limit(
4114 void * clientData,
4115 Tcl_Interp *interp,
4116 int objc,
4117 Tcl_Obj *CONST objv[]
4118){
drh6f7adc82006-01-11 21:41:20 +00004119#if defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) && !defined(SQLITE_OMIT_DISKIO)
drh6aafc292006-01-05 15:50:06 +00004120 int amt;
4121 if( objc!=1 && objc!=2 ){
4122 Tcl_WrongNumArgs(interp, 1, objv, "?N?");
4123 return TCL_ERROR;
4124 }
drh6f7adc82006-01-11 21:41:20 +00004125 amt = sqlite3ThreadDataReadOnly()->nSoftHeapLimit;
drh6aafc292006-01-05 15:50:06 +00004126 if( objc==2 ){
4127 int N;
4128 if( Tcl_GetIntFromObj(interp, objv[1], &N) ) return TCL_ERROR;
4129 sqlite3_soft_heap_limit(N);
4130 }
4131 Tcl_SetObjResult(interp, Tcl_NewIntObj(amt));
4132#endif
4133 return TCL_OK;
4134}
4135
4136/*
drhb4bc7052006-01-11 23:40:33 +00004137** Usage: sqlite3_clear_tsd_memdebug
4138**
4139** Clear all of the MEMDEBUG information out of thread-specific data.
4140** This will allow it to be deallocated.
4141*/
4142static int test_clear_tsd_memdebug(
4143 void * clientData,
4144 Tcl_Interp *interp,
4145 int objc,
4146 Tcl_Obj *CONST objv[]
4147){
drhb4bc7052006-01-11 23:40:33 +00004148 return TCL_OK;
4149}
4150
4151/*
4152** Usage: sqlite3_tsd_release
4153**
4154** Call sqlite3ReleaseThreadData.
4155*/
4156static int test_tsd_release(
4157 void * clientData,
4158 Tcl_Interp *interp,
4159 int objc,
4160 Tcl_Obj *CONST objv[]
4161){
4162#if defined(SQLITE_MEMDEBUG)
4163 sqlite3ReleaseThreadData();
4164#endif
4165 return TCL_OK;
4166}
4167
4168/*
4169** Usage: sqlite3_thread_cleanup
4170**
4171** Call the sqlite3_thread_cleanup API.
4172*/
4173static int test_thread_cleanup(
4174 void * clientData,
4175 Tcl_Interp *interp,
4176 int objc,
4177 Tcl_Obj *CONST objv[]
4178){
4179 sqlite3_thread_cleanup();
4180 return TCL_OK;
4181}
4182
4183
4184/*
drhc6ba55f2007-04-05 17:36:18 +00004185** Usage: sqlite3_pager_refcounts DB
4186**
drhf5345442007-04-09 12:45:02 +00004187** Return a list of numbers which are the PagerRefcount for all
4188** pagers on each database connection.
drhc6ba55f2007-04-05 17:36:18 +00004189*/
4190static int test_pager_refcounts(
4191 void * clientData,
4192 Tcl_Interp *interp,
4193 int objc,
4194 Tcl_Obj *CONST objv[]
4195){
4196 sqlite3 *db;
4197 int i;
4198 int v, *a;
4199 Tcl_Obj *pResult;
4200
4201 if( objc!=2 ){
4202 Tcl_AppendResult(interp, "wrong # args: should be \"",
drhf5345442007-04-09 12:45:02 +00004203 Tcl_GetStringFromObj(objv[0], 0), " DB", 0);
drhc6ba55f2007-04-05 17:36:18 +00004204 return TCL_ERROR;
4205 }
4206 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
4207 pResult = Tcl_NewObj();
4208 for(i=0; i<db->nDb; i++){
4209 if( db->aDb[i].pBt==0 ){
4210 v = -1;
4211 }else{
4212 a = sqlite3PagerStats(sqlite3BtreePager(db->aDb[i].pBt));
4213 v = a[0];
4214 }
4215 Tcl_ListObjAppendElement(0, pResult, Tcl_NewIntObj(v));
4216 }
4217 Tcl_SetObjResult(interp, pResult);
4218 return TCL_OK;
4219}
4220
4221
4222/*
drh27d258a2004-10-30 20:23:09 +00004223** This routine sets entries in the global ::sqlite_options() array variable
4224** according to the compile-time configuration of the database. Test
4225** procedures use this to determine when tests should be omitted.
4226*/
4227static void set_options(Tcl_Interp *interp){
drh75f86a42005-02-17 00:03:06 +00004228#ifdef SQLITE_32BIT_ROWID
4229 Tcl_SetVar2(interp, "sqlite_options", "rowid32", "1", TCL_GLOBAL_ONLY);
4230#else
4231 Tcl_SetVar2(interp, "sqlite_options", "rowid32", "0", TCL_GLOBAL_ONLY);
4232#endif
4233
drh9f18e8a2005-07-08 12:13:04 +00004234#ifdef SQLITE_CASE_SENSITIVE_LIKE
4235 Tcl_SetVar2(interp, "sqlite_options","casesensitivelike","1",TCL_GLOBAL_ONLY);
4236#else
4237 Tcl_SetVar2(interp, "sqlite_options","casesensitivelike","0",TCL_GLOBAL_ONLY);
4238#endif
4239
danielk1977d7c03f72005-11-25 10:38:22 +00004240#ifdef SQLITE_DISABLE_DIRSYNC
4241 Tcl_SetVar2(interp, "sqlite_options", "dirsync", "0", TCL_GLOBAL_ONLY);
4242#else
4243 Tcl_SetVar2(interp, "sqlite_options", "dirsync", "1", TCL_GLOBAL_ONLY);
4244#endif
4245
danielk197726c5d792005-11-25 09:01:23 +00004246#ifdef SQLITE_DISABLE_LFS
4247 Tcl_SetVar2(interp, "sqlite_options", "lfs", "0", TCL_GLOBAL_ONLY);
4248#else
4249 Tcl_SetVar2(interp, "sqlite_options", "lfs", "1", TCL_GLOBAL_ONLY);
4250#endif
4251
danielk19771c8c23c2004-11-12 15:53:37 +00004252#ifdef SQLITE_OMIT_ALTERTABLE
4253 Tcl_SetVar2(interp, "sqlite_options", "altertable", "0", TCL_GLOBAL_ONLY);
4254#else
4255 Tcl_SetVar2(interp, "sqlite_options", "altertable", "1", TCL_GLOBAL_ONLY);
4256#endif
drh13d70422004-11-13 15:59:14 +00004257
drh9f18e8a2005-07-08 12:13:04 +00004258#ifdef SQLITE_OMIT_ANALYZE
4259 Tcl_SetVar2(interp, "sqlite_options", "analyze", "0", TCL_GLOBAL_ONLY);
4260#else
4261 Tcl_SetVar2(interp, "sqlite_options", "analyze", "1", TCL_GLOBAL_ONLY);
4262#endif
4263
drhfdbcdee2007-03-27 14:44:50 +00004264#ifdef SQLITE_OMIT_ATTACH
4265 Tcl_SetVar2(interp, "sqlite_options", "attach", "0", TCL_GLOBAL_ONLY);
4266#else
4267 Tcl_SetVar2(interp, "sqlite_options", "attach", "1", TCL_GLOBAL_ONLY);
4268#endif
4269
drh13d70422004-11-13 15:59:14 +00004270#ifdef SQLITE_OMIT_AUTHORIZATION
4271 Tcl_SetVar2(interp, "sqlite_options", "auth", "0", TCL_GLOBAL_ONLY);
4272#else
4273 Tcl_SetVar2(interp, "sqlite_options", "auth", "1", TCL_GLOBAL_ONLY);
4274#endif
4275
drhf3388142004-11-13 03:48:06 +00004276#ifdef SQLITE_OMIT_AUTOINCREMENT
4277 Tcl_SetVar2(interp, "sqlite_options", "autoinc", "0", TCL_GLOBAL_ONLY);
4278#else
4279 Tcl_SetVar2(interp, "sqlite_options", "autoinc", "1", TCL_GLOBAL_ONLY);
4280#endif
4281
danielk197745901d62004-11-10 15:27:38 +00004282#ifdef SQLITE_OMIT_AUTOVACUUM
4283 Tcl_SetVar2(interp, "sqlite_options", "autovacuum", "0", TCL_GLOBAL_ONLY);
4284#else
4285 Tcl_SetVar2(interp, "sqlite_options", "autovacuum", "1", TCL_GLOBAL_ONLY);
drhf3388142004-11-13 03:48:06 +00004286#endif /* SQLITE_OMIT_AUTOVACUUM */
danielk19774d36b812004-11-19 07:07:30 +00004287#if !defined(SQLITE_DEFAULT_AUTOVACUUM) || SQLITE_DEFAULT_AUTOVACUUM==0
danielk197745901d62004-11-10 15:27:38 +00004288 Tcl_SetVar2(interp,"sqlite_options","default_autovacuum","0",TCL_GLOBAL_ONLY);
4289#else
4290 Tcl_SetVar2(interp,"sqlite_options","default_autovacuum","1",TCL_GLOBAL_ONLY);
4291#endif
drh13d70422004-11-13 15:59:14 +00004292
drh55ef4d92005-08-14 01:20:37 +00004293#ifdef SQLITE_OMIT_BETWEEN_OPTIMIZATION
4294 Tcl_SetVar2(interp, "sqlite_options", "between_opt", "0", TCL_GLOBAL_ONLY);
4295#else
4296 Tcl_SetVar2(interp, "sqlite_options", "between_opt", "1", TCL_GLOBAL_ONLY);
4297#endif
4298
drh13d70422004-11-13 15:59:14 +00004299#ifdef SQLITE_OMIT_BLOB_LITERAL
4300 Tcl_SetVar2(interp, "sqlite_options", "bloblit", "0", TCL_GLOBAL_ONLY);
4301#else
4302 Tcl_SetVar2(interp, "sqlite_options", "bloblit", "1", TCL_GLOBAL_ONLY);
4303#endif
4304
drh487e2622005-06-25 18:42:14 +00004305#ifdef SQLITE_OMIT_CAST
4306 Tcl_SetVar2(interp, "sqlite_options", "cast", "0", TCL_GLOBAL_ONLY);
4307#else
4308 Tcl_SetVar2(interp, "sqlite_options", "cast", "1", TCL_GLOBAL_ONLY);
4309#endif
4310
drhffe07b22005-11-03 00:41:17 +00004311#ifdef SQLITE_OMIT_CHECK
4312 Tcl_SetVar2(interp, "sqlite_options", "check", "0", TCL_GLOBAL_ONLY);
4313#else
4314 Tcl_SetVar2(interp, "sqlite_options", "check", "1", TCL_GLOBAL_ONLY);
4315#endif
4316
danielk1977deb802c2006-02-09 13:43:28 +00004317#ifdef SQLITE_ENABLE_COLUMN_METADATA
4318 Tcl_SetVar2(interp, "sqlite_options", "columnmetadata", "1", TCL_GLOBAL_ONLY);
4319#else
4320 Tcl_SetVar2(interp, "sqlite_options", "columnmetadata", "0", TCL_GLOBAL_ONLY);
4321#endif
4322
drhccae6022005-02-26 17:31:26 +00004323#ifdef SQLITE_OMIT_COMPLETE
4324 Tcl_SetVar2(interp, "sqlite_options", "complete", "0", TCL_GLOBAL_ONLY);
4325#else
4326 Tcl_SetVar2(interp, "sqlite_options", "complete", "1", TCL_GLOBAL_ONLY);
4327#endif
4328
drh13d70422004-11-13 15:59:14 +00004329#ifdef SQLITE_OMIT_COMPOUND_SELECT
4330 Tcl_SetVar2(interp, "sqlite_options", "compound", "0", TCL_GLOBAL_ONLY);
4331#else
4332 Tcl_SetVar2(interp, "sqlite_options", "compound", "1", TCL_GLOBAL_ONLY);
4333#endif
4334
4335#ifdef SQLITE_OMIT_CONFLICT_CLAUSE
4336 Tcl_SetVar2(interp, "sqlite_options", "conflict", "0", TCL_GLOBAL_ONLY);
4337#else
4338 Tcl_SetVar2(interp, "sqlite_options", "conflict", "1", TCL_GLOBAL_ONLY);
4339#endif
4340
drh66560ad2006-01-06 14:32:19 +00004341#if OS_UNIX
4342 Tcl_SetVar2(interp, "sqlite_options", "crashtest", "1", TCL_GLOBAL_ONLY);
4343#else
4344 Tcl_SetVar2(interp, "sqlite_options", "crashtest", "0", TCL_GLOBAL_ONLY);
4345#endif
4346
drh13d70422004-11-13 15:59:14 +00004347#ifdef SQLITE_OMIT_DATETIME_FUNCS
4348 Tcl_SetVar2(interp, "sqlite_options", "datetime", "0", TCL_GLOBAL_ONLY);
4349#else
4350 Tcl_SetVar2(interp, "sqlite_options", "datetime", "1", TCL_GLOBAL_ONLY);
4351#endif
4352
drh2e66f0b2005-04-28 17:18:48 +00004353#ifdef SQLITE_OMIT_DISKIO
4354 Tcl_SetVar2(interp, "sqlite_options", "diskio", "0", TCL_GLOBAL_ONLY);
4355#else
4356 Tcl_SetVar2(interp, "sqlite_options", "diskio", "1", TCL_GLOBAL_ONLY);
4357#endif
4358
drh13d70422004-11-13 15:59:14 +00004359#ifdef SQLITE_OMIT_EXPLAIN
4360 Tcl_SetVar2(interp, "sqlite_options", "explain", "0", TCL_GLOBAL_ONLY);
4361#else
4362 Tcl_SetVar2(interp, "sqlite_options", "explain", "1", TCL_GLOBAL_ONLY);
4363#endif
4364
4365#ifdef SQLITE_OMIT_FLOATING_POINT
4366 Tcl_SetVar2(interp, "sqlite_options", "floatingpoint", "0", TCL_GLOBAL_ONLY);
4367#else
4368 Tcl_SetVar2(interp, "sqlite_options", "floatingpoint", "1", TCL_GLOBAL_ONLY);
4369#endif
4370
4371#ifdef SQLITE_OMIT_FOREIGN_KEY
4372 Tcl_SetVar2(interp, "sqlite_options", "foreignkey", "0", TCL_GLOBAL_ONLY);
4373#else
4374 Tcl_SetVar2(interp, "sqlite_options", "foreignkey", "1", TCL_GLOBAL_ONLY);
4375#endif
4376
drha2a9d182006-09-10 03:34:06 +00004377#ifdef SQLITE_ENABLE_FTS1
4378 Tcl_SetVar2(interp, "sqlite_options", "fts1", "1", TCL_GLOBAL_ONLY);
4379#else
4380 Tcl_SetVar2(interp, "sqlite_options", "fts1", "0", TCL_GLOBAL_ONLY);
4381#endif
4382
shessa26cf572006-10-19 20:27:58 +00004383#ifdef SQLITE_ENABLE_FTS2
4384 Tcl_SetVar2(interp, "sqlite_options", "fts2", "1", TCL_GLOBAL_ONLY);
4385#else
4386 Tcl_SetVar2(interp, "sqlite_options", "fts2", "0", TCL_GLOBAL_ONLY);
4387#endif
4388
danielk19776b456a22005-03-21 04:04:02 +00004389#ifdef SQLITE_OMIT_GLOBALRECOVER
4390 Tcl_SetVar2(interp, "sqlite_options", "globalrecover", "0", TCL_GLOBAL_ONLY);
4391#else
4392 Tcl_SetVar2(interp, "sqlite_options", "globalrecover", "1", TCL_GLOBAL_ONLY);
4393#endif
4394
danielk197783852ac2007-05-06 16:04:11 +00004395#ifdef SQLITE_ENABLE_ICU
4396 Tcl_SetVar2(interp, "sqlite_options", "icu", "1", TCL_GLOBAL_ONLY);
4397#else
4398 Tcl_SetVar2(interp, "sqlite_options", "icu", "0", TCL_GLOBAL_ONLY);
4399#endif
4400
danielk197732a0d8b2007-05-04 19:03:02 +00004401#ifdef SQLITE_OMIT_INCRBLOB
4402 Tcl_SetVar2(interp, "sqlite_options", "incrblob", "0", TCL_GLOBAL_ONLY);
4403#else
4404 Tcl_SetVar2(interp, "sqlite_options", "incrblob", "1", TCL_GLOBAL_ONLY);
4405#endif /* SQLITE_OMIT_AUTOVACUUM */
4406
drh13d70422004-11-13 15:59:14 +00004407#ifdef SQLITE_OMIT_INTEGRITY_CHECK
4408 Tcl_SetVar2(interp, "sqlite_options", "integrityck", "0", TCL_GLOBAL_ONLY);
4409#else
4410 Tcl_SetVar2(interp, "sqlite_options", "integrityck", "1", TCL_GLOBAL_ONLY);
4411#endif
4412
drhff91c452006-06-27 12:51:12 +00004413#if defined(SQLITE_DEFAULT_FILE_FORMAT) && SQLITE_DEFAULT_FILE_FORMAT==1
4414 Tcl_SetVar2(interp, "sqlite_options", "legacyformat", "1", TCL_GLOBAL_ONLY);
4415#else
4416 Tcl_SetVar2(interp, "sqlite_options", "legacyformat", "0", TCL_GLOBAL_ONLY);
4417#endif
4418
drh55ef4d92005-08-14 01:20:37 +00004419#ifdef SQLITE_OMIT_LIKE_OPTIMIZATION
4420 Tcl_SetVar2(interp, "sqlite_options", "like_opt", "0", TCL_GLOBAL_ONLY);
4421#else
4422 Tcl_SetVar2(interp, "sqlite_options", "like_opt", "1", TCL_GLOBAL_ONLY);
4423#endif
4424
drh1e9daa62007-04-06 21:42:22 +00004425#ifdef SQLITE_OMIT_LOAD_EXTENSION
4426 Tcl_SetVar2(interp, "sqlite_options", "load_ext", "0", TCL_GLOBAL_ONLY);
4427#else
4428 Tcl_SetVar2(interp, "sqlite_options", "load_ext", "1", TCL_GLOBAL_ONLY);
4429#endif
4430
drh13d70422004-11-13 15:59:14 +00004431#ifdef SQLITE_OMIT_MEMORYDB
4432 Tcl_SetVar2(interp, "sqlite_options", "memorydb", "0", TCL_GLOBAL_ONLY);
4433#else
4434 Tcl_SetVar2(interp, "sqlite_options", "memorydb", "1", TCL_GLOBAL_ONLY);
4435#endif
4436
drh6f7adc82006-01-11 21:41:20 +00004437#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
drh6aafc292006-01-05 15:50:06 +00004438 Tcl_SetVar2(interp, "sqlite_options", "memorymanage", "1", TCL_GLOBAL_ONLY);
drh6f7adc82006-01-11 21:41:20 +00004439#else
4440 Tcl_SetVar2(interp, "sqlite_options", "memorymanage", "0", TCL_GLOBAL_ONLY);
drh6aafc292006-01-05 15:50:06 +00004441#endif
4442
drh55ef4d92005-08-14 01:20:37 +00004443#ifdef SQLITE_OMIT_OR_OPTIMIZATION
4444 Tcl_SetVar2(interp, "sqlite_options", "or_opt", "0", TCL_GLOBAL_ONLY);
4445#else
4446 Tcl_SetVar2(interp, "sqlite_options", "or_opt", "1", TCL_GLOBAL_ONLY);
4447#endif
4448
drh13d70422004-11-13 15:59:14 +00004449#ifdef SQLITE_OMIT_PAGER_PRAGMAS
4450 Tcl_SetVar2(interp, "sqlite_options", "pager_pragmas", "0", TCL_GLOBAL_ONLY);
4451#else
4452 Tcl_SetVar2(interp, "sqlite_options", "pager_pragmas", "1", TCL_GLOBAL_ONLY);
4453#endif
4454
drh2e66f0b2005-04-28 17:18:48 +00004455#ifdef SQLITE_OMIT_PARSER
4456 Tcl_SetVar2(interp, "sqlite_options", "parser", "0", TCL_GLOBAL_ONLY);
4457#else
4458 Tcl_SetVar2(interp, "sqlite_options", "parser", "1", TCL_GLOBAL_ONLY);
4459#endif
4460
drhbf216272005-02-26 18:10:44 +00004461#if defined(SQLITE_OMIT_PRAGMA) || defined(SQLITE_OMIT_FLAG_PRAGMAS)
drh13d70422004-11-13 15:59:14 +00004462 Tcl_SetVar2(interp, "sqlite_options", "pragma", "0", TCL_GLOBAL_ONLY);
4463 Tcl_SetVar2(interp, "sqlite_options", "integrityck", "0", TCL_GLOBAL_ONLY);
4464#else
4465 Tcl_SetVar2(interp, "sqlite_options", "pragma", "1", TCL_GLOBAL_ONLY);
4466#endif
4467
4468#ifdef SQLITE_OMIT_PROGRESS_CALLBACK
4469 Tcl_SetVar2(interp, "sqlite_options", "progress", "0", TCL_GLOBAL_ONLY);
4470#else
4471 Tcl_SetVar2(interp, "sqlite_options", "progress", "1", TCL_GLOBAL_ONLY);
4472#endif
4473
drh3f459022006-01-07 16:06:07 +00004474#ifdef SQLITE_ENABLE_REDEF_IO
4475 Tcl_SetVar2(interp, "sqlite_options", "redefio", "1", TCL_GLOBAL_ONLY);
4476#else
4477 Tcl_SetVar2(interp, "sqlite_options", "redefio", "0", TCL_GLOBAL_ONLY);
4478#endif
4479
drh13d70422004-11-13 15:59:14 +00004480#ifdef SQLITE_OMIT_REINDEX
4481 Tcl_SetVar2(interp, "sqlite_options", "reindex", "0", TCL_GLOBAL_ONLY);
4482#else
4483 Tcl_SetVar2(interp, "sqlite_options", "reindex", "1", TCL_GLOBAL_ONLY);
4484#endif
4485
4486#ifdef SQLITE_OMIT_SCHEMA_PRAGMAS
4487 Tcl_SetVar2(interp, "sqlite_options", "schema_pragmas", "0", TCL_GLOBAL_ONLY);
4488#else
4489 Tcl_SetVar2(interp, "sqlite_options", "schema_pragmas", "1", TCL_GLOBAL_ONLY);
4490#endif
4491
4492#ifdef SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS
4493 Tcl_SetVar2(interp, "sqlite_options", "schema_version", "0", TCL_GLOBAL_ONLY);
4494#else
4495 Tcl_SetVar2(interp, "sqlite_options", "schema_version", "1", TCL_GLOBAL_ONLY);
4496#endif
4497
danielk1977aef0bf62005-12-30 16:28:01 +00004498#ifdef SQLITE_OMIT_SHARED_CACHE
4499 Tcl_SetVar2(interp, "sqlite_options", "shared_cache", "0", TCL_GLOBAL_ONLY);
4500#else
4501 Tcl_SetVar2(interp, "sqlite_options", "shared_cache", "1", TCL_GLOBAL_ONLY);
4502#endif
4503
danielk19773e8c37e2005-01-21 03:12:14 +00004504#ifdef SQLITE_OMIT_SUBQUERY
4505 Tcl_SetVar2(interp, "sqlite_options", "subquery", "0", TCL_GLOBAL_ONLY);
4506#else
4507 Tcl_SetVar2(interp, "sqlite_options", "subquery", "1", TCL_GLOBAL_ONLY);
4508#endif
4509
drh13d70422004-11-13 15:59:14 +00004510#ifdef SQLITE_OMIT_TCL_VARIABLE
4511 Tcl_SetVar2(interp, "sqlite_options", "tclvar", "0", TCL_GLOBAL_ONLY);
4512#else
4513 Tcl_SetVar2(interp, "sqlite_options", "tclvar", "1", TCL_GLOBAL_ONLY);
4514#endif
4515
4516#if defined(THREADSAFE) && THREADSAFE
4517 Tcl_SetVar2(interp, "sqlite_options", "threadsafe", "1", TCL_GLOBAL_ONLY);
4518#else
4519 Tcl_SetVar2(interp, "sqlite_options", "threadsafe", "0", TCL_GLOBAL_ONLY);
4520#endif
4521
drh19e2d372005-08-29 23:00:03 +00004522#ifdef SQLITE_OMIT_TRACE
4523 Tcl_SetVar2(interp, "sqlite_options", "trace", "0", TCL_GLOBAL_ONLY);
4524#else
4525 Tcl_SetVar2(interp, "sqlite_options", "trace", "1", TCL_GLOBAL_ONLY);
4526#endif
4527
drh13d70422004-11-13 15:59:14 +00004528#ifdef SQLITE_OMIT_TRIGGER
4529 Tcl_SetVar2(interp, "sqlite_options", "trigger", "0", TCL_GLOBAL_ONLY);
4530#else
4531 Tcl_SetVar2(interp, "sqlite_options", "trigger", "1", TCL_GLOBAL_ONLY);
4532#endif
4533
danielk197753c0f742005-03-29 03:10:59 +00004534#ifdef SQLITE_OMIT_TEMPDB
4535 Tcl_SetVar2(interp, "sqlite_options", "tempdb", "0", TCL_GLOBAL_ONLY);
4536#else
4537 Tcl_SetVar2(interp, "sqlite_options", "tempdb", "1", TCL_GLOBAL_ONLY);
4538#endif
4539
drh13d70422004-11-13 15:59:14 +00004540#ifdef SQLITE_OMIT_UTF16
4541 Tcl_SetVar2(interp, "sqlite_options", "utf16", "0", TCL_GLOBAL_ONLY);
4542#else
4543 Tcl_SetVar2(interp, "sqlite_options", "utf16", "1", TCL_GLOBAL_ONLY);
4544#endif
4545
drhfdbcdee2007-03-27 14:44:50 +00004546#if defined(SQLITE_OMIT_VACUUM) || defined(SQLITE_OMIT_ATTACH)
drh13d70422004-11-13 15:59:14 +00004547 Tcl_SetVar2(interp, "sqlite_options", "vacuum", "0", TCL_GLOBAL_ONLY);
4548#else
4549 Tcl_SetVar2(interp, "sqlite_options", "vacuum", "1", TCL_GLOBAL_ONLY);
4550#endif
4551
4552#ifdef SQLITE_OMIT_VIEW
4553 Tcl_SetVar2(interp, "sqlite_options", "view", "0", TCL_GLOBAL_ONLY);
4554#else
4555 Tcl_SetVar2(interp, "sqlite_options", "view", "1", TCL_GLOBAL_ONLY);
4556#endif
drhb9bb7c12006-06-11 23:41:55 +00004557
4558#ifdef SQLITE_OMIT_VIRTUALTABLE
4559 Tcl_SetVar2(interp, "sqlite_options", "vtab", "0", TCL_GLOBAL_ONLY);
4560#else
4561 Tcl_SetVar2(interp, "sqlite_options", "vtab", "1", TCL_GLOBAL_ONLY);
4562#endif
danielk1977cd1aa902007-04-02 12:29:01 +00004563
4564#ifdef SQLITE_DEFAULT_FILE_FORMAT
4565 Tcl_ObjSetVar2(interp,
4566 Tcl_NewStringObj("sqlite_default_file_format", -1), 0,
4567 Tcl_NewIntObj(SQLITE_DEFAULT_FILE_FORMAT), TCL_GLOBAL_ONLY
4568 );
4569#endif
drh1e9daa62007-04-06 21:42:22 +00004570#ifdef SQLITE_MAX_PAGE_SIZE
4571 Tcl_ObjSetVar2(interp,
4572 Tcl_NewStringObj("SQLITE_MAX_PAGE_SIZE", -1), 0,
4573 Tcl_NewIntObj(SQLITE_MAX_PAGE_SIZE), TCL_GLOBAL_ONLY
4574 );
4575#endif
4576#ifdef TEMP_STORE
4577 Tcl_ObjSetVar2(interp,
4578 Tcl_NewStringObj("TEMP_STORE", -1), 0,
4579 Tcl_NewIntObj(TEMP_STORE), TCL_GLOBAL_ONLY
4580 );
4581#endif
drh27d258a2004-10-30 20:23:09 +00004582}
4583
4584/*
drh80788d82006-09-02 14:50:23 +00004585** tclcmd: working_64bit_int
4586**
4587** Some TCL builds (ex: cygwin) do not support 64-bit integers. This
4588** leads to a number of test failures. The present command checks the
4589** TCL build to see whether or not it supports 64-bit integers. It
4590** returns TRUE if it does and FALSE if not.
4591**
4592** This command is used to warn users that their TCL build is defective
4593** and that the errors they are seeing in the test scripts might be
4594** a result of their defective TCL rather than problems in SQLite.
4595*/
4596static int working_64bit_int(
4597 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
4598 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
4599 int objc, /* Number of arguments */
4600 Tcl_Obj *CONST objv[] /* Command arguments */
4601){
4602 Tcl_Obj *pTestObj;
4603 int working = 0;
4604
4605 pTestObj = Tcl_NewWideIntObj(1000000*(i64)1234567890);
4606 working = strcmp(Tcl_GetString(pTestObj), "1234567890000000")==0;
4607 Tcl_DecrRefCount(pTestObj);
4608 Tcl_SetObjResult(interp, Tcl_NewBooleanObj(working));
4609 return TCL_OK;
4610}
4611
4612
4613/*
drhd1bf3512001-04-07 15:24:33 +00004614** Register commands with the TCL interpreter.
4615*/
4616int Sqlitetest1_Init(Tcl_Interp *interp){
danielk19776f8a5032004-05-10 10:34:51 +00004617 extern int sqlite3_search_count;
4618 extern int sqlite3_interrupt_count;
4619 extern int sqlite3_open_file_count;
drh6bf89572004-11-03 16:27:01 +00004620 extern int sqlite3_sort_count;
danielk19776f8a5032004-05-10 10:34:51 +00004621 extern int sqlite3_current_time;
drhae7e1512007-05-02 16:51:59 +00004622 extern int sqlite3_max_blobsize;
drh16a9b832007-05-05 18:39:25 +00004623 extern int sqlite3BtreeSharedCacheReport(void*,
4624 Tcl_Interp*,int,Tcl_Obj*CONST*);
drhc2eef3b2002-08-31 18:53:06 +00004625 static struct {
4626 char *zName;
4627 Tcl_CmdProc *xProc;
4628 } aCmd[] = {
drhd3d39e92004-05-20 22:16:29 +00004629 { "sqlite3_mprintf_int", (Tcl_CmdProc*)sqlite3_mprintf_int },
drhe9707672004-06-25 01:10:48 +00004630 { "sqlite3_mprintf_int64", (Tcl_CmdProc*)sqlite3_mprintf_int64 },
drhd3d39e92004-05-20 22:16:29 +00004631 { "sqlite3_mprintf_str", (Tcl_CmdProc*)sqlite3_mprintf_str },
drhb3738b62007-03-31 15:02:49 +00004632 { "sqlite3_snprintf_str", (Tcl_CmdProc*)sqlite3_snprintf_str },
drhe29b1a02004-07-17 21:56:09 +00004633 { "sqlite3_mprintf_stronly", (Tcl_CmdProc*)sqlite3_mprintf_stronly},
drhd3d39e92004-05-20 22:16:29 +00004634 { "sqlite3_mprintf_double", (Tcl_CmdProc*)sqlite3_mprintf_double },
4635 { "sqlite3_mprintf_scaled", (Tcl_CmdProc*)sqlite3_mprintf_scaled },
drh63782852005-08-30 19:30:59 +00004636 { "sqlite3_mprintf_hexdouble", (Tcl_CmdProc*)sqlite3_mprintf_hexdouble},
drhd3d39e92004-05-20 22:16:29 +00004637 { "sqlite3_mprintf_z_test", (Tcl_CmdProc*)test_mprintf_z },
drh05a82982006-03-19 13:00:25 +00004638 { "sqlite3_mprintf_n_test", (Tcl_CmdProc*)test_mprintf_n },
drh68853902007-05-07 11:24:30 +00004639 { "sqlite3_snprintf_int", (Tcl_CmdProc*)test_snprintf_int },
drhd3d39e92004-05-20 22:16:29 +00004640 { "sqlite3_last_insert_rowid", (Tcl_CmdProc*)test_last_rowid },
4641 { "sqlite3_exec_printf", (Tcl_CmdProc*)test_exec_printf },
drhb62c3352006-11-23 09:39:16 +00004642 { "sqlite3_exec", (Tcl_CmdProc*)test_exec },
4643 { "sqlite3_exec_nr", (Tcl_CmdProc*)test_exec_nr },
drhd3d39e92004-05-20 22:16:29 +00004644 { "sqlite3_get_table_printf", (Tcl_CmdProc*)test_get_table_printf },
4645 { "sqlite3_close", (Tcl_CmdProc*)sqlite_test_close },
4646 { "sqlite3_create_function", (Tcl_CmdProc*)test_create_function },
4647 { "sqlite3_create_aggregate", (Tcl_CmdProc*)test_create_aggregate },
4648 { "sqlite_register_test_function", (Tcl_CmdProc*)test_register_func },
4649 { "sqlite_abort", (Tcl_CmdProc*)sqlite_abort },
danielk19772c336542005-01-13 02:14:23 +00004650#ifdef SQLITE_MEMDEBUG
drhd3d39e92004-05-20 22:16:29 +00004651 { "sqlite_malloc_fail", (Tcl_CmdProc*)sqlite_malloc_fail },
4652 { "sqlite_malloc_stat", (Tcl_CmdProc*)sqlite_malloc_stat },
drhc2eef3b2002-08-31 18:53:06 +00004653#endif
drh25d65432004-07-22 15:02:25 +00004654 { "sqlite_bind", (Tcl_CmdProc*)test_bind },
4655 { "breakpoint", (Tcl_CmdProc*)test_breakpoint },
4656 { "sqlite3_key", (Tcl_CmdProc*)test_key },
4657 { "sqlite3_rekey", (Tcl_CmdProc*)test_rekey },
drhcacb2082005-01-11 15:28:33 +00004658 { "sqlite_set_magic", (Tcl_CmdProc*)sqlite_set_magic },
drhc5cdca62005-01-11 16:54:14 +00004659 { "sqlite3_interrupt", (Tcl_CmdProc*)test_interrupt },
drh3e1d8e62005-05-26 16:23:34 +00004660 { "sqlite_delete_function", (Tcl_CmdProc*)delete_function },
4661 { "sqlite_delete_collation", (Tcl_CmdProc*)delete_collation },
4662 { "sqlite3_get_autocommit", (Tcl_CmdProc*)get_autocommit },
drh79158e12005-09-06 21:40:45 +00004663 { "sqlite3_stack_used", (Tcl_CmdProc*)test_stack_used },
drh30867652006-07-06 10:59:57 +00004664 { "sqlite3_busy_timeout", (Tcl_CmdProc*)test_busy_timeout },
drh3c23a882007-01-09 14:01:13 +00004665 { "printf", (Tcl_CmdProc*)test_printf },
drh538f5702007-04-13 02:14:30 +00004666 { "sqlite3_io_trace", (Tcl_CmdProc*)test_io_trace },
drhc2eef3b2002-08-31 18:53:06 +00004667 };
danielk197751e3d8e2004-05-20 01:12:34 +00004668 static struct {
4669 char *zName;
4670 Tcl_ObjCmdProc *xProc;
danielk197704f2e682004-05-27 01:04:07 +00004671 void *clientData;
danielk197751e3d8e2004-05-20 01:12:34 +00004672 } aObjCmd[] = {
drhdddca282006-01-03 00:33:50 +00004673 { "sqlite3_connection_pointer", get_sqlite_pointer, 0 },
drh241db312004-06-22 12:46:53 +00004674 { "sqlite3_bind_int", test_bind_int, 0 },
drhb026e052007-05-02 01:34:31 +00004675 { "sqlite3_bind_zeroblob", test_bind_zeroblob, 0 },
drh241db312004-06-22 12:46:53 +00004676 { "sqlite3_bind_int64", test_bind_int64, 0 },
4677 { "sqlite3_bind_double", test_bind_double, 0 },
danielk197704f2e682004-05-27 01:04:07 +00004678 { "sqlite3_bind_null", test_bind_null ,0 },
4679 { "sqlite3_bind_text", test_bind_text ,0 },
4680 { "sqlite3_bind_text16", test_bind_text16 ,0 },
4681 { "sqlite3_bind_blob", test_bind_blob ,0 },
drh75f6a032004-07-15 14:15:00 +00004682 { "sqlite3_bind_parameter_count", test_bind_parameter_count, 0},
drh895d7472004-08-20 16:02:39 +00004683 { "sqlite3_bind_parameter_name", test_bind_parameter_name, 0},
drhfa6bc002004-09-07 16:19:52 +00004684 { "sqlite3_bind_parameter_index", test_bind_parameter_index, 0},
danielk1977600dd0b2005-01-20 01:14:23 +00004685 { "sqlite3_clear_bindings", test_clear_bindings, 0},
drhf9cb7f52006-06-27 20:06:44 +00004686 { "sqlite3_sleep", test_sleep, 0},
danielk197704f2e682004-05-27 01:04:07 +00004687 { "sqlite3_errcode", test_errcode ,0 },
4688 { "sqlite3_errmsg", test_errmsg ,0 },
4689 { "sqlite3_errmsg16", test_errmsg16 ,0 },
4690 { "sqlite3_open", test_open ,0 },
4691 { "sqlite3_open16", test_open16 ,0 },
danielk1977bc6ada42004-06-30 08:20:16 +00004692 { "sqlite3_complete16", test_complete16 ,0 },
danielk197704f2e682004-05-27 01:04:07 +00004693
4694 { "sqlite3_prepare", test_prepare ,0 },
4695 { "sqlite3_prepare16", test_prepare16 ,0 },
drhb900aaf2006-11-09 00:24:53 +00004696 { "sqlite3_prepare_v2", test_prepare_v2 ,0 },
4697 { "sqlite3_prepare16_v2", test_prepare16_v2 ,0 },
danielk197704f2e682004-05-27 01:04:07 +00004698 { "sqlite3_finalize", test_finalize ,0 },
4699 { "sqlite3_reset", test_reset ,0 },
drhd89bd002005-01-22 03:03:54 +00004700 { "sqlite3_expired", test_expired ,0 },
drhf8db1bc2005-04-22 02:38:37 +00004701 { "sqlite3_transfer_bindings", test_transfer_bind ,0 },
danielk1977fbcd5852004-06-15 02:44:18 +00004702 { "sqlite3_changes", test_changes ,0 },
4703 { "sqlite3_step", test_step ,0 },
danielk197704f2e682004-05-27 01:04:07 +00004704
drhb4bc7052006-01-11 23:40:33 +00004705 { "sqlite3_release_memory", test_release_memory, 0},
4706 { "sqlite3_soft_heap_limit", test_soft_heap_limit, 0},
4707 { "sqlite3_clear_tsd_memdebug", test_clear_tsd_memdebug, 0},
4708 { "sqlite3_tsd_release", test_tsd_release, 0},
4709 { "sqlite3_thread_cleanup", test_thread_cleanup, 0},
drhc6ba55f2007-04-05 17:36:18 +00004710 { "sqlite3_pager_refcounts", test_pager_refcounts, 0},
drh6aafc292006-01-05 15:50:06 +00004711
drhc2e87a32006-06-27 15:16:14 +00004712 { "sqlite3_load_extension", test_load_extension, 0},
4713 { "sqlite3_enable_load_extension", test_enable_load, 0},
drh4ac285a2006-09-15 07:28:50 +00004714 { "sqlite3_extended_result_codes", test_extended_result_codes, 0},
drhc2e87a32006-06-27 15:16:14 +00004715
danielk197704f2e682004-05-27 01:04:07 +00004716 /* sqlite3_column_*() API */
4717 { "sqlite3_column_count", test_column_count ,0 },
4718 { "sqlite3_data_count", test_data_count ,0 },
4719 { "sqlite3_column_type", test_column_type ,0 },
danielk1977ea61b2c2004-05-27 01:49:51 +00004720 { "sqlite3_column_blob", test_column_blob ,0 },
danielk197704f2e682004-05-27 01:04:07 +00004721 { "sqlite3_column_double", test_column_double ,0 },
4722 { "sqlite3_column_int64", test_column_int64 ,0 },
drh241db312004-06-22 12:46:53 +00004723 { "sqlite3_column_text", test_stmt_utf8, sqlite3_column_text },
4724 { "sqlite3_column_decltype", test_stmt_utf8, sqlite3_column_decltype },
4725 { "sqlite3_column_name", test_stmt_utf8, sqlite3_column_name },
drh6c626082004-11-14 21:56:29 +00004726 { "sqlite3_column_int", test_stmt_int, sqlite3_column_int },
4727 { "sqlite3_column_bytes", test_stmt_int, sqlite3_column_bytes },
danielk19774b1ae992006-02-10 03:06:10 +00004728#ifdef SQLITE_ENABLE_COLUMN_METADATA
danielk1977955de522006-02-10 02:27:42 +00004729{ "sqlite3_column_database_name", test_stmt_utf8, sqlite3_column_database_name},
4730{ "sqlite3_column_table_name", test_stmt_utf8, sqlite3_column_table_name},
4731{ "sqlite3_column_origin_name", test_stmt_utf8, sqlite3_column_origin_name},
danielk19774b1ae992006-02-10 03:06:10 +00004732#endif
danielk1977955de522006-02-10 02:27:42 +00004733
drh6c626082004-11-14 21:56:29 +00004734#ifndef SQLITE_OMIT_UTF16
4735 { "sqlite3_column_bytes16", test_stmt_int, sqlite3_column_bytes16 },
drh241db312004-06-22 12:46:53 +00004736 { "sqlite3_column_text16", test_stmt_utf16, sqlite3_column_text16 },
4737 { "sqlite3_column_decltype16", test_stmt_utf16, sqlite3_column_decltype16},
4738 { "sqlite3_column_name16", test_stmt_utf16, sqlite3_column_name16 },
drh7d9bd4e2006-02-16 18:16:36 +00004739 { "add_alignment_test_collations", add_alignment_test_collations, 0 },
danielk19774b1ae992006-02-10 03:06:10 +00004740#ifdef SQLITE_ENABLE_COLUMN_METADATA
danielk1977955de522006-02-10 02:27:42 +00004741{"sqlite3_column_database_name16",
4742 test_stmt_utf16, sqlite3_column_database_name16},
4743{"sqlite3_column_table_name16", test_stmt_utf16, sqlite3_column_table_name16},
4744{"sqlite3_column_origin_name16", test_stmt_utf16, sqlite3_column_origin_name16},
drh6c626082004-11-14 21:56:29 +00004745#endif
danielk19774b1ae992006-02-10 03:06:10 +00004746#endif
danielk1977a9808b32007-05-07 09:32:45 +00004747 { "sqlite3_create_collation_x", test_create_collation_x, 0 },
4748 { "sqlite3_global_recover", test_global_recover, 0 },
4749 { "working_64bit_int", working_64bit_int, 0 },
danielk197704f2e682004-05-27 01:04:07 +00004750
danielk19779a1d0ab2004-06-01 14:09:28 +00004751 /* Functions from os.h */
danielk197744ee5bf2005-05-27 09:41:12 +00004752#ifndef SQLITE_OMIT_DISKIO
danielk19779a1d0ab2004-06-01 14:09:28 +00004753 { "sqlite3OsOpenReadWrite",test_sqlite3OsOpenReadWrite, 0 },
4754 { "sqlite3OsClose", test_sqlite3OsClose, 0 },
4755 { "sqlite3OsLock", test_sqlite3OsLock, 0 },
drhab3f9fe2004-08-14 17:10:10 +00004756 { "sqlite3OsTempFileName", test_sqlite3OsTempFileName, 0 },
danielk1977312d6b32004-06-29 13:18:23 +00004757
4758 /* Custom test interfaces */
4759 { "sqlite3OsUnlock", test_sqlite3OsUnlock, 0 },
danielk197744ee5bf2005-05-27 09:41:12 +00004760#endif
drh5436dc22004-11-14 04:04:17 +00004761#ifndef SQLITE_OMIT_UTF16
danielk1977312d6b32004-06-29 13:18:23 +00004762 { "add_test_collate", test_collate, 0 },
4763 { "add_test_collate_needed", test_collate_needed, 0 },
4764 { "add_test_function", test_function, 0 },
drh5436dc22004-11-14 04:04:17 +00004765#endif
danielk19775591df52005-12-20 09:19:37 +00004766#ifdef SQLITE_MEMDEBUG
4767 { "sqlite_malloc_outstanding", sqlite_malloc_outstanding, 0},
4768#endif
danielk1977312d6b32004-06-29 13:18:23 +00004769 { "sqlite3_test_errstr", test_errstr, 0 },
drh92febd92004-08-20 18:34:20 +00004770 { "tcl_variable_type", tcl_variable_type, 0 },
danielk1977aef0bf62005-12-30 16:28:01 +00004771#ifndef SQLITE_OMIT_SHARED_CACHE
drh6f7adc82006-01-11 21:41:20 +00004772 { "sqlite3_enable_shared_cache", test_enable_shared, 0 },
drh16a9b832007-05-05 18:39:25 +00004773 { "sqlite3_shared_cache_report", sqlite3BtreeSharedCacheReport, 0},
danielk1977aef0bf62005-12-30 16:28:01 +00004774#endif
danielk1977161fb792006-01-24 10:58:21 +00004775 { "sqlite3_libversion_number", test_libversion_number, 0 },
danielk1977deb802c2006-02-09 13:43:28 +00004776#ifdef SQLITE_ENABLE_COLUMN_METADATA
4777 { "sqlite3_table_column_metadata", test_table_column_metadata, 0 },
4778#endif
danielk1977dcbb5d32007-05-04 18:36:44 +00004779#ifndef SQLITE_OMIT_INCRBLOB
4780 { "sqlite3_blob_read", test_blob_read, 0 },
4781 { "sqlite3_blob_write", test_blob_write, 0 },
4782#endif
danielk197751e3d8e2004-05-20 01:12:34 +00004783 };
drh1398ad32005-01-19 23:24:50 +00004784 static int bitmask_size = sizeof(Bitmask)*8;
drhc2eef3b2002-08-31 18:53:06 +00004785 int i;
drh2ac3ee92004-06-07 16:27:46 +00004786 extern int sqlite3_os_trace;
drh51147ba2005-07-23 22:59:55 +00004787 extern int sqlite3_where_trace;
drhb851b2c2005-03-10 14:11:12 +00004788 extern int sqlite3_sync_count, sqlite3_fullsync_count;
drhaf6df112005-06-07 02:12:30 +00004789 extern int sqlite3_opentemp_count;
drh01397202005-07-20 14:31:53 +00004790 extern int sqlite3_memUsed;
drh4f0c5872007-03-26 22:05:01 +00004791 extern char *sqlite3_malloc_id;
drh01397202005-07-20 14:31:53 +00004792 extern int sqlite3_memMax;
drh55ef4d92005-08-14 01:20:37 +00004793 extern int sqlite3_like_count;
drhb4bc7052006-01-11 23:40:33 +00004794 extern int sqlite3_tsd_count;
drhdd735212007-02-24 13:53:05 +00004795 extern int sqlite3_xferopt_count;
drh538f5702007-04-13 02:14:30 +00004796 extern int sqlite3_pager_readdb_count;
4797 extern int sqlite3_pager_writedb_count;
4798 extern int sqlite3_pager_writej_count;
4799 extern int sqlite3_pager_pgfree_count;
drh029b44b2006-01-15 00:13:15 +00004800#if OS_UNIX && defined(SQLITE_TEST) && defined(THREADSAFE) && THREADSAFE
4801 extern int threadsOverrideEachOthersLocks;
4802#endif
drhc0929982005-09-05 19:08:29 +00004803#if OS_WIN
4804 extern int sqlite3_os_type;
4805#endif
drh8b3d9902005-08-19 00:14:42 +00004806#ifdef SQLITE_DEBUG
4807 extern int sqlite3_vdbe_addop_trace;
drh549c8b62005-09-19 13:15:23 +00004808#endif
4809#ifdef SQLITE_TEST
4810 extern char sqlite3_query_plan[];
drh9042f392005-07-15 23:24:23 +00004811 static char *query_plan = sqlite3_query_plan;
drh48083ce2005-09-19 12:37:27 +00004812#endif
drhc2eef3b2002-08-31 18:53:06 +00004813
4814 for(i=0; i<sizeof(aCmd)/sizeof(aCmd[0]); i++){
4815 Tcl_CreateCommand(interp, aCmd[i].zName, aCmd[i].xProc, 0, 0);
4816 }
danielk197751e3d8e2004-05-20 01:12:34 +00004817 for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){
danielk1977c572ef72004-05-27 09:28:41 +00004818 Tcl_CreateObjCommand(interp, aObjCmd[i].zName,
4819 aObjCmd[i].xProc, aObjCmd[i].clientData, 0);
danielk197751e3d8e2004-05-20 01:12:34 +00004820 }
danielk19776490beb2004-05-11 06:17:21 +00004821 Tcl_LinkVar(interp, "sqlite_search_count",
danielk19776f8a5032004-05-10 10:34:51 +00004822 (char*)&sqlite3_search_count, TCL_LINK_INT);
drh6bf89572004-11-03 16:27:01 +00004823 Tcl_LinkVar(interp, "sqlite_sort_count",
4824 (char*)&sqlite3_sort_count, TCL_LINK_INT);
drhae7e1512007-05-02 16:51:59 +00004825 Tcl_LinkVar(interp, "sqlite3_max_blobsize",
4826 (char*)&sqlite3_max_blobsize, TCL_LINK_INT);
drh55ef4d92005-08-14 01:20:37 +00004827 Tcl_LinkVar(interp, "sqlite_like_count",
4828 (char*)&sqlite3_like_count, TCL_LINK_INT);
danielk19776490beb2004-05-11 06:17:21 +00004829 Tcl_LinkVar(interp, "sqlite_interrupt_count",
danielk19776f8a5032004-05-10 10:34:51 +00004830 (char*)&sqlite3_interrupt_count, TCL_LINK_INT);
danielk19776490beb2004-05-11 06:17:21 +00004831 Tcl_LinkVar(interp, "sqlite_open_file_count",
danielk19776f8a5032004-05-10 10:34:51 +00004832 (char*)&sqlite3_open_file_count, TCL_LINK_INT);
danielk19776490beb2004-05-11 06:17:21 +00004833 Tcl_LinkVar(interp, "sqlite_current_time",
danielk19776f8a5032004-05-10 10:34:51 +00004834 (char*)&sqlite3_current_time, TCL_LINK_INT);
drh2ac3ee92004-06-07 16:27:46 +00004835 Tcl_LinkVar(interp, "sqlite_os_trace",
4836 (char*)&sqlite3_os_trace, TCL_LINK_INT);
drhb4bc7052006-01-11 23:40:33 +00004837 Tcl_LinkVar(interp, "sqlite3_tsd_count",
4838 (char*)&sqlite3_tsd_count, TCL_LINK_INT);
drhdd735212007-02-24 13:53:05 +00004839 Tcl_LinkVar(interp, "sqlite3_xferopt_count",
4840 (char*)&sqlite3_xferopt_count, TCL_LINK_INT);
drh538f5702007-04-13 02:14:30 +00004841 Tcl_LinkVar(interp, "sqlite3_pager_readdb_count",
4842 (char*)&sqlite3_pager_readdb_count, TCL_LINK_INT);
4843 Tcl_LinkVar(interp, "sqlite3_pager_writedb_count",
4844 (char*)&sqlite3_pager_writedb_count, TCL_LINK_INT);
4845 Tcl_LinkVar(interp, "sqlite3_pager_writej_count",
4846 (char*)&sqlite3_pager_writej_count, TCL_LINK_INT);
4847 Tcl_LinkVar(interp, "sqlite3_pager_pgfree_count",
4848 (char*)&sqlite3_pager_pgfree_count, TCL_LINK_INT);
danielk19774b2688a2006-06-20 11:01:07 +00004849#ifndef SQLITE_OMIT_UTF16
drh7d9bd4e2006-02-16 18:16:36 +00004850 Tcl_LinkVar(interp, "unaligned_string_counter",
4851 (char*)&unaligned_string_counter, TCL_LINK_INT);
danielk19774b2688a2006-06-20 11:01:07 +00004852#endif
drh029b44b2006-01-15 00:13:15 +00004853#if OS_UNIX && defined(SQLITE_TEST) && defined(THREADSAFE) && THREADSAFE
4854 Tcl_LinkVar(interp, "threadsOverrideEachOthersLocks",
4855 (char*)&threadsOverrideEachOthersLocks, TCL_LINK_INT);
4856#endif
drh268803a2005-12-14 20:11:30 +00004857#ifndef SQLITE_OMIT_UTF16
4858 Tcl_LinkVar(interp, "sqlite_last_needed_collation",
4859 (char*)&pzNeededCollation, TCL_LINK_STRING|TCL_LINK_READ_ONLY);
4860#endif
danielk197797cb2e92005-12-09 14:39:04 +00004861#ifdef SQLITE_MEMDEBUG
danielk19772e588c72005-12-09 14:25:08 +00004862 Tcl_LinkVar(interp, "sqlite_malloc_id",
4863 (char*)&sqlite3_malloc_id, TCL_LINK_STRING);
danielk197797cb2e92005-12-09 14:39:04 +00004864#endif
drhc0929982005-09-05 19:08:29 +00004865#if OS_WIN
4866 Tcl_LinkVar(interp, "sqlite_os_type",
4867 (char*)&sqlite3_os_type, TCL_LINK_INT);
4868#endif
drh549c8b62005-09-19 13:15:23 +00004869#ifdef SQLITE_TEST
4870 Tcl_LinkVar(interp, "sqlite_query_plan",
4871 (char*)&query_plan, TCL_LINK_STRING|TCL_LINK_READ_ONLY);
4872#endif
drh8b3d9902005-08-19 00:14:42 +00004873#ifdef SQLITE_DEBUG
4874 Tcl_LinkVar(interp, "sqlite_addop_trace",
4875 (char*)&sqlite3_vdbe_addop_trace, TCL_LINK_INT);
drh48083ce2005-09-19 12:37:27 +00004876 Tcl_LinkVar(interp, "sqlite_where_trace",
4877 (char*)&sqlite3_where_trace, TCL_LINK_INT);
drh8b3d9902005-08-19 00:14:42 +00004878#endif
drh70180302005-08-02 21:42:16 +00004879#ifdef SQLITE_MEMDEBUG
drh01397202005-07-20 14:31:53 +00004880 Tcl_LinkVar(interp, "sqlite_memused",
4881 (char*)&sqlite3_memUsed, TCL_LINK_INT | TCL_LINK_READ_ONLY);
4882 Tcl_LinkVar(interp, "sqlite_memmax",
4883 (char*)&sqlite3_memMax, TCL_LINK_INT | TCL_LINK_READ_ONLY);
drh70180302005-08-02 21:42:16 +00004884#endif
danielk1977cbe21be2005-06-07 07:58:48 +00004885#ifndef SQLITE_OMIT_DISKIO
drhaf6df112005-06-07 02:12:30 +00004886 Tcl_LinkVar(interp, "sqlite_opentemp_count",
4887 (char*)&sqlite3_opentemp_count, TCL_LINK_INT);
danielk1977cbe21be2005-06-07 07:58:48 +00004888#endif
drh7c972de2003-09-06 22:18:07 +00004889 Tcl_LinkVar(interp, "sqlite_static_bind_value",
4890 (char*)&sqlite_static_bind_value, TCL_LINK_STRING);
drhf0313812006-09-04 15:53:53 +00004891 Tcl_LinkVar(interp, "sqlite_static_bind_nbyte",
4892 (char*)&sqlite_static_bind_nbyte, TCL_LINK_INT);
drhab3f9fe2004-08-14 17:10:10 +00004893 Tcl_LinkVar(interp, "sqlite_temp_directory",
drheffd02b2004-08-29 23:42:13 +00004894 (char*)&sqlite3_temp_directory, TCL_LINK_STRING);
drh1398ad32005-01-19 23:24:50 +00004895 Tcl_LinkVar(interp, "bitmask_size",
4896 (char*)&bitmask_size, TCL_LINK_INT|TCL_LINK_READ_ONLY);
drh748f7632005-03-11 04:41:39 +00004897#if OS_UNIX
drhb851b2c2005-03-10 14:11:12 +00004898 Tcl_LinkVar(interp, "sqlite_sync_count",
4899 (char*)&sqlite3_sync_count, TCL_LINK_INT);
4900 Tcl_LinkVar(interp, "sqlite_fullsync_count",
4901 (char*)&sqlite3_fullsync_count, TCL_LINK_INT);
drh748f7632005-03-11 04:41:39 +00004902#endif /* OS_UNIX */
drh27d258a2004-10-30 20:23:09 +00004903 set_options(interp);
danielk1977b82e7ed2006-01-11 14:09:31 +00004904
drhd1bf3512001-04-07 15:24:33 +00004905 return TCL_OK;
4906}