blob: b08333eef171dd440a384fe52b67908d58410ced [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*************************************************************************
12** Code for testing the printf() interface to SQLite. This code
13** is not included in the SQLite library. It is used for automated
14** testing of the SQLite library.
15**
drh55ef4d92005-08-14 01:20:37 +000016** $Id: test1.c,v 1.154 2005/08/14 01:20:39 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
drh2e66f0b2005-04-28 17:18:48 +000024const char *sqlite3TestErrorName(int rc){
danielk19776622cce2004-05-20 11:00:52 +000025 const char *zName = 0;
26 switch( rc ){
27 case SQLITE_OK: zName = "SQLITE_OK"; break;
28 case SQLITE_ERROR: zName = "SQLITE_ERROR"; break;
danielk19776622cce2004-05-20 11:00:52 +000029 case SQLITE_PERM: zName = "SQLITE_PERM"; break;
30 case SQLITE_ABORT: zName = "SQLITE_ABORT"; break;
31 case SQLITE_BUSY: zName = "SQLITE_BUSY"; break;
32 case SQLITE_LOCKED: zName = "SQLITE_LOCKED"; break;
33 case SQLITE_NOMEM: zName = "SQLITE_NOMEM"; break;
34 case SQLITE_READONLY: zName = "SQLITE_READONLY"; break;
35 case SQLITE_INTERRUPT: zName = "SQLITE_INTERRUPT"; break;
36 case SQLITE_IOERR: zName = "SQLITE_IOERR"; break;
37 case SQLITE_CORRUPT: zName = "SQLITE_CORRUPT"; break;
danielk19776622cce2004-05-20 11:00:52 +000038 case SQLITE_FULL: zName = "SQLITE_FULL"; break;
39 case SQLITE_CANTOPEN: zName = "SQLITE_CANTOPEN"; break;
40 case SQLITE_PROTOCOL: zName = "SQLITE_PROTOCOL"; break;
41 case SQLITE_EMPTY: zName = "SQLITE_EMPTY"; break;
42 case SQLITE_SCHEMA: zName = "SQLITE_SCHEMA"; break;
danielk19776622cce2004-05-20 11:00:52 +000043 case SQLITE_CONSTRAINT: zName = "SQLITE_CONSTRAINT"; break;
44 case SQLITE_MISMATCH: zName = "SQLITE_MISMATCH"; break;
45 case SQLITE_MISUSE: zName = "SQLITE_MISUSE"; break;
46 case SQLITE_NOLFS: zName = "SQLITE_NOLFS"; break;
47 case SQLITE_AUTH: zName = "SQLITE_AUTH"; break;
48 case SQLITE_FORMAT: zName = "SQLITE_FORMAT"; break;
49 case SQLITE_RANGE: zName = "SQLITE_RANGE"; break;
50 case SQLITE_ROW: zName = "SQLITE_ROW"; break;
51 case SQLITE_DONE: zName = "SQLITE_DONE"; break;
drhc60d0442004-09-30 13:43:13 +000052 case SQLITE_NOTADB: zName = "SQLITE_NOTADB"; break;
danielk19776622cce2004-05-20 11:00:52 +000053 default: zName = "SQLITE_Unknown"; break;
54 }
55 return zName;
56}
drh2e66f0b2005-04-28 17:18:48 +000057#define errorName sqlite3TestErrorName
danielk19776622cce2004-05-20 11:00:52 +000058
drhd1bf3512001-04-07 15:24:33 +000059/*
drhc60d0442004-09-30 13:43:13 +000060** Convert an sqlite3_stmt* into an sqlite3*. This depends on the
61** fact that the sqlite3* is the first field in the Vdbe structure.
62*/
drh51942bc2005-06-12 22:01:42 +000063#define StmtToDb(X) sqlite3_db_handle(X)
drhc60d0442004-09-30 13:43:13 +000064
65/*
66** Check a return value to make sure it agrees with the results
67** from sqlite3_errcode.
68*/
69int sqlite3TestErrCode(Tcl_Interp *interp, sqlite3 *db, int rc){
70 if( rc!=SQLITE_MISUSE && rc!=SQLITE_OK && sqlite3_errcode(db)!=rc ){
71 char zBuf[200];
72 int r2 = sqlite3_errcode(db);
73 sprintf(zBuf, "error code %s (%d) does not match sqlite3_errcode %s (%d)",
74 errorName(rc), rc, errorName(r2), r2);
75 Tcl_ResetResult(interp);
76 Tcl_AppendResult(interp, zBuf, 0);
77 return 1;
78 }
79 return 0;
80}
81
82/*
drh9bb575f2004-09-06 17:24:11 +000083** Decode a pointer to an sqlite3 object.
drhb86ccfb2003-01-28 23:13:10 +000084*/
drh9bb575f2004-09-06 17:24:11 +000085static int getDbPointer(Tcl_Interp *interp, const char *zA, sqlite3 **ppDb){
drhfe63d1c2004-09-08 20:13:04 +000086 *ppDb = (sqlite3*)sqlite3TextToPtr(zA);
drhb86ccfb2003-01-28 23:13:10 +000087 return TCL_OK;
88}
89
90/*
danielk197751e3d8e2004-05-20 01:12:34 +000091** Decode a pointer to an sqlite3_stmt object.
92*/
93static int getStmtPointer(
94 Tcl_Interp *interp,
95 const char *zArg,
96 sqlite3_stmt **ppStmt
97){
drhfe63d1c2004-09-08 20:13:04 +000098 *ppStmt = (sqlite3_stmt*)sqlite3TextToPtr(zArg);
danielk197751e3d8e2004-05-20 01:12:34 +000099 return TCL_OK;
100}
101
102/*
danielk19779a1d0ab2004-06-01 14:09:28 +0000103** Decode a pointer to an sqlite3_stmt object.
104*/
105static int getFilePointer(
106 Tcl_Interp *interp,
107 const char *zArg,
108 OsFile **ppFile
109){
drhfe63d1c2004-09-08 20:13:04 +0000110 *ppFile = (OsFile*)sqlite3TextToPtr(zArg);
danielk19779a1d0ab2004-06-01 14:09:28 +0000111 return TCL_OK;
112}
113
114/*
drh7d8085a2003-04-26 13:19:38 +0000115** Generate a text representation of a pointer that can be understood
116** by the getDbPointer and getVmPointer routines above.
117**
118** The problem is, on some machines (Solaris) if you do a printf with
119** "%p" you cannot turn around and do a scanf with the same "%p" and
120** get your pointer back. You have to prepend a "0x" before it will
121** work. Or at least that is what is reported to me (drh). But this
122** behavior varies from machine to machine. The solution used her is
123** to test the string right after it is generated to see if it can be
124** understood by scanf, and if not, try prepending an "0x" to see if
125** that helps. If nothing works, a fatal error is generated.
126*/
127static int makePointerStr(Tcl_Interp *interp, char *zPtr, void *p){
drhfe63d1c2004-09-08 20:13:04 +0000128 sqlite3_snprintf(100, zPtr, "%p", p);
drh7d8085a2003-04-26 13:19:38 +0000129 return TCL_OK;
130}
131
132/*
danielk19776f8a5032004-05-10 10:34:51 +0000133** The callback routine for sqlite3_exec_printf().
drhd1bf3512001-04-07 15:24:33 +0000134*/
135static int exec_printf_cb(void *pArg, int argc, char **argv, char **name){
136 Tcl_DString *str = (Tcl_DString*)pArg;
137 int i;
138
139 if( Tcl_DStringLength(str)==0 ){
140 for(i=0; i<argc; i++){
141 Tcl_DStringAppendElement(str, name[i] ? name[i] : "NULL");
142 }
143 }
144 for(i=0; i<argc; i++){
145 Tcl_DStringAppendElement(str, argv[i] ? argv[i] : "NULL");
146 }
147 return 0;
148}
149
150/*
danielk19776f8a5032004-05-10 10:34:51 +0000151** Usage: sqlite3_exec_printf DB FORMAT STRING
drhd1bf3512001-04-07 15:24:33 +0000152**
danielk19776f8a5032004-05-10 10:34:51 +0000153** Invoke the sqlite3_exec_printf() interface using the open database
drhd1bf3512001-04-07 15:24:33 +0000154** DB. The SQL is the string FORMAT. The format string should contain
155** one %s or %q. STRING is the value inserted into %s or %q.
156*/
157static int test_exec_printf(
158 void *NotUsed,
159 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
160 int argc, /* Number of arguments */
161 char **argv /* Text of each argument */
162){
drh9bb575f2004-09-06 17:24:11 +0000163 sqlite3 *db;
drhd1bf3512001-04-07 15:24:33 +0000164 Tcl_DString str;
165 int rc;
166 char *zErr = 0;
drh1211de32004-07-26 12:24:22 +0000167 char *zSql;
drhd1bf3512001-04-07 15:24:33 +0000168 char zBuf[30];
169 if( argc!=4 ){
170 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
171 " DB FORMAT STRING", 0);
172 return TCL_ERROR;
173 }
drhb86ccfb2003-01-28 23:13:10 +0000174 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
drhd1bf3512001-04-07 15:24:33 +0000175 Tcl_DStringInit(&str);
drh1211de32004-07-26 12:24:22 +0000176 zSql = sqlite3_mprintf(argv[2], argv[3]);
177 rc = sqlite3_exec(db, zSql, exec_printf_cb, &str, &zErr);
178 sqlite3_free(zSql);
drhd1bf3512001-04-07 15:24:33 +0000179 sprintf(zBuf, "%d", rc);
180 Tcl_AppendElement(interp, zBuf);
181 Tcl_AppendElement(interp, rc==SQLITE_OK ? Tcl_DStringValue(&str) : zErr);
182 Tcl_DStringFree(&str);
183 if( zErr ) free(zErr);
drhc60d0442004-09-30 13:43:13 +0000184 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
drhd1bf3512001-04-07 15:24:33 +0000185 return TCL_OK;
186}
187
188/*
danielk19776f8a5032004-05-10 10:34:51 +0000189** Usage: sqlite3_mprintf_z_test SEPARATOR ARG0 ARG1 ...
drhd93d8a82003-06-16 03:08:18 +0000190**
191** Test the %z format of mprintf(). Use multiple mprintf() calls to
192** concatenate arg0 through argn using separator as the separator.
193** Return the result.
194*/
195static int test_mprintf_z(
196 void *NotUsed,
197 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
198 int argc, /* Number of arguments */
199 char **argv /* Text of each argument */
200){
201 char *zResult = 0;
202 int i;
203
204 for(i=2; i<argc; i++){
danielk19774adee202004-05-08 08:23:19 +0000205 zResult = sqlite3MPrintf("%z%s%s", zResult, argv[1], argv[i]);
drhd93d8a82003-06-16 03:08:18 +0000206 }
207 Tcl_AppendResult(interp, zResult, 0);
drh5f968432004-02-21 19:02:30 +0000208 sqliteFree(zResult);
drhd93d8a82003-06-16 03:08:18 +0000209 return TCL_OK;
210}
211
212/*
danielk19776f8a5032004-05-10 10:34:51 +0000213** Usage: sqlite3_get_table_printf DB FORMAT STRING
drhd1bf3512001-04-07 15:24:33 +0000214**
danielk19776f8a5032004-05-10 10:34:51 +0000215** Invoke the sqlite3_get_table_printf() interface using the open database
drhd1bf3512001-04-07 15:24:33 +0000216** DB. The SQL is the string FORMAT. The format string should contain
217** one %s or %q. STRING is the value inserted into %s or %q.
218*/
219static int test_get_table_printf(
220 void *NotUsed,
221 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
222 int argc, /* Number of arguments */
223 char **argv /* Text of each argument */
224){
drh9bb575f2004-09-06 17:24:11 +0000225 sqlite3 *db;
drhd1bf3512001-04-07 15:24:33 +0000226 Tcl_DString str;
227 int rc;
228 char *zErr = 0;
229 int nRow, nCol;
230 char **aResult;
231 int i;
232 char zBuf[30];
drh1211de32004-07-26 12:24:22 +0000233 char *zSql;
drhd1bf3512001-04-07 15:24:33 +0000234 if( argc!=4 ){
235 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
236 " DB FORMAT STRING", 0);
237 return TCL_ERROR;
238 }
drhb86ccfb2003-01-28 23:13:10 +0000239 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
drhd1bf3512001-04-07 15:24:33 +0000240 Tcl_DStringInit(&str);
drh1211de32004-07-26 12:24:22 +0000241 zSql = sqlite3_mprintf(argv[2],argv[3]);
242 rc = sqlite3_get_table(db, zSql, &aResult, &nRow, &nCol, &zErr);
243 sqlite3_free(zSql);
drhd1bf3512001-04-07 15:24:33 +0000244 sprintf(zBuf, "%d", rc);
245 Tcl_AppendElement(interp, zBuf);
246 if( rc==SQLITE_OK ){
247 sprintf(zBuf, "%d", nRow);
248 Tcl_AppendElement(interp, zBuf);
249 sprintf(zBuf, "%d", nCol);
250 Tcl_AppendElement(interp, zBuf);
251 for(i=0; i<(nRow+1)*nCol; i++){
252 Tcl_AppendElement(interp, aResult[i] ? aResult[i] : "NULL");
253 }
254 }else{
255 Tcl_AppendElement(interp, zErr);
256 }
danielk19776f8a5032004-05-10 10:34:51 +0000257 sqlite3_free_table(aResult);
drhd1bf3512001-04-07 15:24:33 +0000258 if( zErr ) free(zErr);
drhc60d0442004-09-30 13:43:13 +0000259 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
drhd1bf3512001-04-07 15:24:33 +0000260 return TCL_OK;
261}
262
drhaf9ff332002-01-16 21:00:27 +0000263
264/*
danielk19776f8a5032004-05-10 10:34:51 +0000265** Usage: sqlite3_last_insert_rowid DB
drhaf9ff332002-01-16 21:00:27 +0000266**
267** Returns the integer ROWID of the most recent insert.
268*/
269static int test_last_rowid(
270 void *NotUsed,
271 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
272 int argc, /* Number of arguments */
273 char **argv /* Text of each argument */
274){
drh9bb575f2004-09-06 17:24:11 +0000275 sqlite3 *db;
drhaf9ff332002-01-16 21:00:27 +0000276 char zBuf[30];
277
278 if( argc!=2 ){
279 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " DB\"", 0);
280 return TCL_ERROR;
281 }
drhb86ccfb2003-01-28 23:13:10 +0000282 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
danielk1977c572ef72004-05-27 09:28:41 +0000283 sprintf(zBuf, "%lld", sqlite3_last_insert_rowid(db));
drhaf9ff332002-01-16 21:00:27 +0000284 Tcl_AppendResult(interp, zBuf, 0);
285 return SQLITE_OK;
286}
287
drhd1bf3512001-04-07 15:24:33 +0000288/*
drh25d65432004-07-22 15:02:25 +0000289** Usage: sqlite3_key DB KEY
290**
291** Set the codec key.
292*/
293static int test_key(
294 void *NotUsed,
295 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
296 int argc, /* Number of arguments */
297 char **argv /* Text of each argument */
298){
drh9bb575f2004-09-06 17:24:11 +0000299 sqlite3 *db;
drh25d65432004-07-22 15:02:25 +0000300 const char *zKey;
301 int nKey;
302 if( argc!=3 ){
303 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
304 " FILENAME\"", 0);
305 return TCL_ERROR;
306 }
307 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
308 zKey = argv[2];
309 nKey = strlen(zKey);
310#ifdef SQLITE_HAS_CODEC
311 sqlite3_key(db, zKey, nKey);
312#endif
313 return TCL_OK;
314}
315
316/*
317** Usage: sqlite3_rekey DB KEY
318**
319** Change the codec key.
320*/
321static int test_rekey(
322 void *NotUsed,
323 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
324 int argc, /* Number of arguments */
325 char **argv /* Text of each argument */
326){
drh9bb575f2004-09-06 17:24:11 +0000327 sqlite3 *db;
drh25d65432004-07-22 15:02:25 +0000328 const char *zKey;
329 int nKey;
330 if( argc!=3 ){
331 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
332 " FILENAME\"", 0);
333 return TCL_ERROR;
334 }
335 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
336 zKey = argv[2];
337 nKey = strlen(zKey);
338#ifdef SQLITE_HAS_CODEC
339 sqlite3_rekey(db, zKey, nKey);
340#endif
341 return TCL_OK;
342}
343
344/*
danielk19776f8a5032004-05-10 10:34:51 +0000345** Usage: sqlite3_close DB
drhd1bf3512001-04-07 15:24:33 +0000346**
danielk19776f8a5032004-05-10 10:34:51 +0000347** Closes the database opened by sqlite3_open.
drhd1bf3512001-04-07 15:24:33 +0000348*/
349static int sqlite_test_close(
350 void *NotUsed,
351 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
352 int argc, /* Number of arguments */
353 char **argv /* Text of each argument */
354){
drh9bb575f2004-09-06 17:24:11 +0000355 sqlite3 *db;
danielk197796d81f92004-06-19 03:33:57 +0000356 int rc;
drhd1bf3512001-04-07 15:24:33 +0000357 if( argc!=2 ){
358 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
359 " FILENAME\"", 0);
360 return TCL_ERROR;
361 }
drhb86ccfb2003-01-28 23:13:10 +0000362 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
danielk197796d81f92004-06-19 03:33:57 +0000363 rc = sqlite3_close(db);
danielk1977f9d64d22004-06-19 08:18:07 +0000364 Tcl_SetResult(interp, (char *)errorName(rc), TCL_STATIC);
drhd1bf3512001-04-07 15:24:33 +0000365 return TCL_OK;
366}
367
368/*
drhc22bd472002-05-10 13:14:07 +0000369** Implementation of the x_coalesce() function.
370** Return the first argument non-NULL argument.
371*/
danielk19770ae8b832004-05-25 12:05:56 +0000372static void ifnullFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
drhc22bd472002-05-10 13:14:07 +0000373 int i;
374 for(i=0; i<argc; i++){
drh9c054832004-05-31 18:51:57 +0000375 if( SQLITE_NULL!=sqlite3_value_type(argv[i]) ){
danielk1977d8123362004-06-12 09:25:12 +0000376 sqlite3_result_text(context, sqlite3_value_text(argv[i]), -1,
377 SQLITE_TRANSIENT);
drhc22bd472002-05-10 13:14:07 +0000378 break;
379 }
380 }
381}
382
383/*
drhd1d9fc32004-01-07 19:24:48 +0000384** A structure into which to accumulate text.
385*/
386struct dstr {
387 int nAlloc; /* Space allocated */
388 int nUsed; /* Space used */
389 char *z; /* The space */
390};
391
392/*
393** Append text to a dstr
394*/
395static void dstrAppend(struct dstr *p, const char *z, int divider){
396 int n = strlen(z);
397 if( p->nUsed + n + 2 > p->nAlloc ){
398 char *zNew;
399 p->nAlloc = p->nAlloc*2 + n + 200;
400 zNew = sqliteRealloc(p->z, p->nAlloc);
401 if( zNew==0 ){
402 sqliteFree(p->z);
403 memset(p, 0, sizeof(*p));
404 return;
405 }
406 p->z = zNew;
407 }
408 if( divider && p->nUsed>0 ){
409 p->z[p->nUsed++] = divider;
410 }
411 memcpy(&p->z[p->nUsed], z, n+1);
412 p->nUsed += n;
413}
414
415/*
danielk19774adee202004-05-08 08:23:19 +0000416** Invoked for each callback from sqlite3ExecFunc
drhd1d9fc32004-01-07 19:24:48 +0000417*/
418static int execFuncCallback(void *pData, int argc, char **argv, char **NotUsed){
419 struct dstr *p = (struct dstr*)pData;
420 int i;
421 for(i=0; i<argc; i++){
422 if( argv[i]==0 ){
423 dstrAppend(p, "NULL", ' ');
424 }else{
425 dstrAppend(p, argv[i], ' ');
426 }
427 }
428 return 0;
429}
430
431/*
danielk1977e35ee192004-06-26 09:50:11 +0000432** Implementation of the x_sqlite_exec() function. This function takes
drhc22bd472002-05-10 13:14:07 +0000433** a single argument and attempts to execute that argument as SQL code.
drh6cbe1f12002-07-01 00:31:36 +0000434** This is illegal and should set the SQLITE_MISUSE flag on the database.
drhd1d9fc32004-01-07 19:24:48 +0000435**
danielk19776f8a5032004-05-10 10:34:51 +0000436** 2004-Jan-07: We have changed this to make it legal to call sqlite3_exec()
drhd1d9fc32004-01-07 19:24:48 +0000437** from within a function call.
drhc22bd472002-05-10 13:14:07 +0000438**
439** This routine simulates the effect of having two threads attempt to
440** use the same database at the same time.
441*/
danielk197751ad0ec2004-05-24 12:39:02 +0000442static void sqlite3ExecFunc(
danielk19770ae8b832004-05-25 12:05:56 +0000443 sqlite3_context *context,
danielk197751ad0ec2004-05-24 12:39:02 +0000444 int argc,
445 sqlite3_value **argv
446){
drhd1d9fc32004-01-07 19:24:48 +0000447 struct dstr x;
448 memset(&x, 0, sizeof(x));
drh9bb575f2004-09-06 17:24:11 +0000449 sqlite3_exec((sqlite3*)sqlite3_user_data(context),
drh4f26d6c2004-05-26 23:25:30 +0000450 sqlite3_value_text(argv[0]),
drhd1d9fc32004-01-07 19:24:48 +0000451 execFuncCallback, &x, 0);
danielk1977d8123362004-06-12 09:25:12 +0000452 sqlite3_result_text(context, x.z, x.nUsed, SQLITE_TRANSIENT);
drhd1d9fc32004-01-07 19:24:48 +0000453 sqliteFree(x.z);
drhc22bd472002-05-10 13:14:07 +0000454}
455
456/*
457** Usage: sqlite_test_create_function DB
458**
danielk19776f8a5032004-05-10 10:34:51 +0000459** Call the sqlite3_create_function API on the given database in order
drhc22bd472002-05-10 13:14:07 +0000460** to create a function named "x_coalesce". This function does the same thing
461** as the "coalesce" function. This function also registers an SQL function
danielk1977e35ee192004-06-26 09:50:11 +0000462** named "x_sqlite_exec" that invokes sqlite3_exec(). Invoking sqlite3_exec()
drhc22bd472002-05-10 13:14:07 +0000463** in this way is illegal recursion and should raise an SQLITE_MISUSE error.
464** The effect is similar to trying to use the same database connection from
465** two threads at the same time.
466**
467** The original motivation for this routine was to be able to call the
danielk19776f8a5032004-05-10 10:34:51 +0000468** sqlite3_create_function function while a query is in progress in order
drhc22bd472002-05-10 13:14:07 +0000469** to test the SQLITE_MISUSE detection logic.
470*/
drhc2eef3b2002-08-31 18:53:06 +0000471static int test_create_function(
drhc22bd472002-05-10 13:14:07 +0000472 void *NotUsed,
473 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
474 int argc, /* Number of arguments */
475 char **argv /* Text of each argument */
476){
drhc60d0442004-09-30 13:43:13 +0000477 int rc;
drh9bb575f2004-09-06 17:24:11 +0000478 sqlite3 *db;
drh9bb575f2004-09-06 17:24:11 +0000479 extern void Md5_Register(sqlite3*);
danielk1977312d6b32004-06-29 13:18:23 +0000480
drhc22bd472002-05-10 13:14:07 +0000481 if( argc!=2 ){
482 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
danielk19774397de52005-01-12 12:44:03 +0000483 " DB\"", 0);
drhc22bd472002-05-10 13:14:07 +0000484 return TCL_ERROR;
485 }
drhb86ccfb2003-01-28 23:13:10 +0000486 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
drhc60d0442004-09-30 13:43:13 +0000487 rc = sqlite3_create_function(db, "x_coalesce", -1, SQLITE_ANY, 0,
488 ifnullFunc, 0, 0);
danielk1977312d6b32004-06-29 13:18:23 +0000489
drh5436dc22004-11-14 04:04:17 +0000490#ifndef SQLITE_OMIT_UTF16
danielk1977312d6b32004-06-29 13:18:23 +0000491 /* Use the sqlite3_create_function16() API here. Mainly for fun, but also
492 ** because it is not tested anywhere else. */
drhc60d0442004-09-30 13:43:13 +0000493 if( rc==SQLITE_OK ){
danielk1977576ec6b2005-01-21 11:55:25 +0000494 sqlite3_value *pVal;
drhc60d0442004-09-30 13:43:13 +0000495 pVal = sqlite3ValueNew();
496 sqlite3ValueSetStr(pVal, -1, "x_sqlite_exec", SQLITE_UTF8, SQLITE_STATIC);
497 rc = sqlite3_create_function16(db,
498 sqlite3ValueText(pVal, SQLITE_UTF16NATIVE),
499 1, SQLITE_UTF16, db, sqlite3ExecFunc, 0, 0);
500 sqlite3ValueFree(pVal);
501 }
drh5436dc22004-11-14 04:04:17 +0000502#endif
503
drhc60d0442004-09-30 13:43:13 +0000504 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
danielk19774397de52005-01-12 12:44:03 +0000505 Tcl_SetResult(interp, (char *)errorName(rc), 0);
drhc22bd472002-05-10 13:14:07 +0000506 return TCL_OK;
507}
508
509/*
510** Routines to implement the x_count() aggregate function.
511*/
512typedef struct CountCtx CountCtx;
513struct CountCtx {
514 int n;
515};
danielk19770ae8b832004-05-25 12:05:56 +0000516static void countStep(sqlite3_context *context, int argc, sqlite3_value **argv){
drhc22bd472002-05-10 13:14:07 +0000517 CountCtx *p;
drh4f26d6c2004-05-26 23:25:30 +0000518 p = sqlite3_aggregate_context(context, sizeof(*p));
drh9c054832004-05-31 18:51:57 +0000519 if( (argc==0 || SQLITE_NULL!=sqlite3_value_type(argv[0]) ) && p ){
drhc22bd472002-05-10 13:14:07 +0000520 p->n++;
521 }
522}
danielk19770ae8b832004-05-25 12:05:56 +0000523static void countFinalize(sqlite3_context *context){
drhc22bd472002-05-10 13:14:07 +0000524 CountCtx *p;
drh4f26d6c2004-05-26 23:25:30 +0000525 p = sqlite3_aggregate_context(context, sizeof(*p));
danielk1977c572ef72004-05-27 09:28:41 +0000526 sqlite3_result_int(context, p ? p->n : 0);
drhc22bd472002-05-10 13:14:07 +0000527}
528
529/*
530** Usage: sqlite_test_create_aggregate DB
531**
danielk19776f8a5032004-05-10 10:34:51 +0000532** Call the sqlite3_create_function API on the given database in order
drhc22bd472002-05-10 13:14:07 +0000533** to create a function named "x_count". This function does the same thing
534** as the "md5sum" function.
535**
536** The original motivation for this routine was to be able to call the
danielk19776f8a5032004-05-10 10:34:51 +0000537** sqlite3_create_aggregate function while a query is in progress in order
drhc22bd472002-05-10 13:14:07 +0000538** to test the SQLITE_MISUSE detection logic.
539*/
drhc2eef3b2002-08-31 18:53:06 +0000540static int test_create_aggregate(
drhc22bd472002-05-10 13:14:07 +0000541 void *NotUsed,
542 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
543 int argc, /* Number of arguments */
544 char **argv /* Text of each argument */
545){
drh9bb575f2004-09-06 17:24:11 +0000546 sqlite3 *db;
drhc60d0442004-09-30 13:43:13 +0000547 int rc;
drhc22bd472002-05-10 13:14:07 +0000548 if( argc!=2 ){
549 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
550 " FILENAME\"", 0);
551 return TCL_ERROR;
552 }
drhb86ccfb2003-01-28 23:13:10 +0000553 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
drhc60d0442004-09-30 13:43:13 +0000554 rc = sqlite3_create_function(db, "x_count", 0, SQLITE_UTF8, 0, 0,
danielk1977d8123362004-06-12 09:25:12 +0000555 countStep,countFinalize);
drhc60d0442004-09-30 13:43:13 +0000556 if( rc==SQLITE_OK ){
557 sqlite3_create_function(db, "x_count", 1, SQLITE_UTF8, 0, 0,
558 countStep,countFinalize);
559 }
560 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
drhc22bd472002-05-10 13:14:07 +0000561 return TCL_OK;
562}
563
564
565
566/*
danielk19776f8a5032004-05-10 10:34:51 +0000567** Usage: sqlite3_mprintf_int FORMAT INTEGER INTEGER INTEGER
drhd1bf3512001-04-07 15:24:33 +0000568**
569** Call mprintf with three integer arguments
570*/
danielk19776f8a5032004-05-10 10:34:51 +0000571static int sqlite3_mprintf_int(
drhd1bf3512001-04-07 15:24:33 +0000572 void *NotUsed,
573 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
574 int argc, /* Number of arguments */
575 char **argv /* Text of each argument */
576){
577 int a[3], i;
578 char *z;
579 if( argc!=5 ){
580 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
581 " FORMAT INT INT INT\"", 0);
582 return TCL_ERROR;
583 }
584 for(i=2; i<5; i++){
585 if( Tcl_GetInt(interp, argv[i], &a[i-2]) ) return TCL_ERROR;
586 }
danielk19776f8a5032004-05-10 10:34:51 +0000587 z = sqlite3_mprintf(argv[1], a[0], a[1], a[2]);
drhd1bf3512001-04-07 15:24:33 +0000588 Tcl_AppendResult(interp, z, 0);
drh3f4fedb2004-05-31 19:34:33 +0000589 sqlite3_free(z);
drhd1bf3512001-04-07 15:24:33 +0000590 return TCL_OK;
591}
592
593/*
drh9d213ef2004-06-30 04:02:11 +0000594** If zNum represents an integer that will fit in 64-bits, then set
595** *pValue to that integer and return true. Otherwise return false.
596*/
597static int sqlite3GetInt64(const char *zNum, i64 *pValue){
598 if( sqlite3FitsIn64Bits(zNum) ){
599 sqlite3atoi64(zNum, pValue);
600 return 1;
601 }
602 return 0;
603}
604
605/*
drhe9707672004-06-25 01:10:48 +0000606** Usage: sqlite3_mprintf_int64 FORMAT INTEGER INTEGER INTEGER
607**
608** Call mprintf with three 64-bit integer arguments
609*/
610static int sqlite3_mprintf_int64(
611 void *NotUsed,
612 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
613 int argc, /* Number of arguments */
614 char **argv /* Text of each argument */
615){
616 int i;
617 sqlite_int64 a[3];
618 char *z;
619 if( argc!=5 ){
620 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
621 " FORMAT INT INT INT\"", 0);
622 return TCL_ERROR;
623 }
624 for(i=2; i<5; i++){
625 if( !sqlite3GetInt64(argv[i], &a[i-2]) ){
626 Tcl_AppendResult(interp, "argument is not a valid 64-bit integer", 0);
627 return TCL_ERROR;
628 }
629 }
630 z = sqlite3_mprintf(argv[1], a[0], a[1], a[2]);
631 Tcl_AppendResult(interp, z, 0);
632 sqlite3_free(z);
633 return TCL_OK;
634}
635
636/*
danielk19776f8a5032004-05-10 10:34:51 +0000637** Usage: sqlite3_mprintf_str FORMAT INTEGER INTEGER STRING
drhd1bf3512001-04-07 15:24:33 +0000638**
639** Call mprintf with two integer arguments and one string argument
640*/
danielk19776f8a5032004-05-10 10:34:51 +0000641static int sqlite3_mprintf_str(
drhd1bf3512001-04-07 15:24:33 +0000642 void *NotUsed,
643 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
644 int argc, /* Number of arguments */
645 char **argv /* Text of each argument */
646){
647 int a[3], i;
648 char *z;
chwf220b242002-06-16 04:54:28 +0000649 if( argc<4 || argc>5 ){
drhd1bf3512001-04-07 15:24:33 +0000650 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
chwf220b242002-06-16 04:54:28 +0000651 " FORMAT INT INT ?STRING?\"", 0);
drhd1bf3512001-04-07 15:24:33 +0000652 return TCL_ERROR;
653 }
654 for(i=2; i<4; i++){
655 if( Tcl_GetInt(interp, argv[i], &a[i-2]) ) return TCL_ERROR;
656 }
danielk19776f8a5032004-05-10 10:34:51 +0000657 z = sqlite3_mprintf(argv[1], a[0], a[1], argc>4 ? argv[4] : NULL);
drhd1bf3512001-04-07 15:24:33 +0000658 Tcl_AppendResult(interp, z, 0);
drh3f4fedb2004-05-31 19:34:33 +0000659 sqlite3_free(z);
drhd1bf3512001-04-07 15:24:33 +0000660 return TCL_OK;
661}
662
663/*
danielk19776f8a5032004-05-10 10:34:51 +0000664** Usage: sqlite3_mprintf_str FORMAT INTEGER INTEGER DOUBLE
drhd1bf3512001-04-07 15:24:33 +0000665**
666** Call mprintf with two integer arguments and one double argument
667*/
danielk19776f8a5032004-05-10 10:34:51 +0000668static int sqlite3_mprintf_double(
drhd1bf3512001-04-07 15:24:33 +0000669 void *NotUsed,
670 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
671 int argc, /* Number of arguments */
672 char **argv /* Text of each argument */
673){
674 int a[3], i;
675 double r;
676 char *z;
677 if( argc!=5 ){
678 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
679 " FORMAT INT INT STRING\"", 0);
680 return TCL_ERROR;
681 }
682 for(i=2; i<4; i++){
683 if( Tcl_GetInt(interp, argv[i], &a[i-2]) ) return TCL_ERROR;
684 }
685 if( Tcl_GetDouble(interp, argv[4], &r) ) return TCL_ERROR;
danielk19776f8a5032004-05-10 10:34:51 +0000686 z = sqlite3_mprintf(argv[1], a[0], a[1], r);
drhd1bf3512001-04-07 15:24:33 +0000687 Tcl_AppendResult(interp, z, 0);
drh3f4fedb2004-05-31 19:34:33 +0000688 sqlite3_free(z);
drhd1bf3512001-04-07 15:24:33 +0000689 return TCL_OK;
690}
691
692/*
danielk19776f8a5032004-05-10 10:34:51 +0000693** Usage: sqlite3_mprintf_str FORMAT DOUBLE DOUBLE
drhb621c232004-02-21 19:41:04 +0000694**
695** Call mprintf with a single double argument which is the product of the
696** two arguments given above. This is used to generate overflow and underflow
697** doubles to test that they are converted properly.
698*/
danielk19776f8a5032004-05-10 10:34:51 +0000699static int sqlite3_mprintf_scaled(
drhb621c232004-02-21 19:41:04 +0000700 void *NotUsed,
701 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
702 int argc, /* Number of arguments */
703 char **argv /* Text of each argument */
704){
705 int i;
706 double r[2];
707 char *z;
708 if( argc!=4 ){
709 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
710 " FORMAT DOUBLE DOUBLE\"", 0);
711 return TCL_ERROR;
712 }
713 for(i=2; i<4; i++){
714 if( Tcl_GetDouble(interp, argv[i], &r[i-2]) ) return TCL_ERROR;
715 }
danielk19776f8a5032004-05-10 10:34:51 +0000716 z = sqlite3_mprintf(argv[1], r[0]*r[1]);
drhb621c232004-02-21 19:41:04 +0000717 Tcl_AppendResult(interp, z, 0);
drh3f4fedb2004-05-31 19:34:33 +0000718 sqlite3_free(z);
drhb621c232004-02-21 19:41:04 +0000719 return TCL_OK;
720}
721
722/*
drhe29b1a02004-07-17 21:56:09 +0000723** Usage: sqlite3_mprintf_stronly FORMAT STRING
724**
725** Call mprintf with a single double argument which is the product of the
726** two arguments given above. This is used to generate overflow and underflow
727** doubles to test that they are converted properly.
728*/
729static int sqlite3_mprintf_stronly(
730 void *NotUsed,
731 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
732 int argc, /* Number of arguments */
733 char **argv /* Text of each argument */
734){
735 char *z;
736 if( argc!=3 ){
737 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
738 " FORMAT STRING\"", 0);
739 return TCL_ERROR;
740 }
741 z = sqlite3_mprintf(argv[1], argv[2]);
742 Tcl_AppendResult(interp, z, 0);
743 sqlite3_free(z);
744 return TCL_OK;
745}
746
747/*
drh46934232004-11-20 19:18:00 +0000748** Usage: sqlite_malloc_fail N ?REPEAT-INTERVAL?
drhdaffd0e2001-04-11 14:28:42 +0000749**
drh46934232004-11-20 19:18:00 +0000750** Rig sqliteMalloc() to fail on the N-th call and every REPEAT-INTERVAL call
751** after that. If REPEAT-INTERVAL is 0 or is omitted, then only a single
752** malloc will fail. If REPEAT-INTERVAL is 1 then all mallocs after the
753** first failure will continue to fail on every call. If REPEAT-INTERVAL is
754** 2 then every other malloc will fail. And so forth.
755**
756** Turn off this mechanism and reset the sqlite3_malloc_failed variable is N==0.
drhdaffd0e2001-04-11 14:28:42 +0000757*/
danielk19772c336542005-01-13 02:14:23 +0000758#ifdef SQLITE_MEMDEBUG
drhdaffd0e2001-04-11 14:28:42 +0000759static int sqlite_malloc_fail(
760 void *NotUsed,
761 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
762 int argc, /* Number of arguments */
763 char **argv /* Text of each argument */
764){
765 int n;
drh46934232004-11-20 19:18:00 +0000766 int rep;
767 if( argc!=2 && argc!=3 ){
drhdaffd0e2001-04-11 14:28:42 +0000768 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " N\"", 0);
769 return TCL_ERROR;
770 }
771 if( Tcl_GetInt(interp, argv[1], &n) ) return TCL_ERROR;
drh46934232004-11-20 19:18:00 +0000772 if( argc==3 ){
773 if( Tcl_GetInt(interp, argv[2], &rep) ) return TCL_ERROR;
774 }else{
775 rep = 0;
776 }
danielk19776f8a5032004-05-10 10:34:51 +0000777 sqlite3_iMallocFail = n;
drh46934232004-11-20 19:18:00 +0000778 sqlite3_iMallocReset = rep;
danielk19776f8a5032004-05-10 10:34:51 +0000779 sqlite3_malloc_failed = 0;
drhdaffd0e2001-04-11 14:28:42 +0000780 return TCL_OK;
781}
782#endif
783
784/*
785** Usage: sqlite_malloc_stat
786**
787** Return the number of prior calls to sqliteMalloc() and sqliteFree().
788*/
danielk19772c336542005-01-13 02:14:23 +0000789#ifdef SQLITE_MEMDEBUG
drhdaffd0e2001-04-11 14:28:42 +0000790static int sqlite_malloc_stat(
791 void *NotUsed,
792 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
793 int argc, /* Number of arguments */
794 char **argv /* Text of each argument */
795){
796 char zBuf[200];
danielk19776f8a5032004-05-10 10:34:51 +0000797 sprintf(zBuf, "%d %d %d", sqlite3_nMalloc, sqlite3_nFree, sqlite3_iMallocFail);
drhdaffd0e2001-04-11 14:28:42 +0000798 Tcl_AppendResult(interp, zBuf, 0);
799 return TCL_OK;
800}
801#endif
802
803/*
drh28b4e482002-03-11 02:06:13 +0000804** Usage: sqlite_abort
805**
806** Shutdown the process immediately. This is not a clean shutdown.
807** This command is used to test the recoverability of a database in
808** the event of a program crash.
809*/
810static int sqlite_abort(
811 void *NotUsed,
812 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
813 int argc, /* Number of arguments */
814 char **argv /* Text of each argument */
815){
816 assert( interp==0 ); /* This will always fail */
817 return TCL_OK;
818}
819
820/*
drh6cbe1f12002-07-01 00:31:36 +0000821** The following routine is a user-defined SQL function whose purpose
822** is to test the sqlite_set_result() API.
823*/
danielk19770ae8b832004-05-25 12:05:56 +0000824static void testFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
drh6cbe1f12002-07-01 00:31:36 +0000825 while( argc>=2 ){
drh4f26d6c2004-05-26 23:25:30 +0000826 const char *zArg0 = sqlite3_value_text(argv[0]);
danielk19776d88bad2004-05-27 14:23:36 +0000827 if( zArg0 ){
828 if( 0==sqlite3StrICmp(zArg0, "int") ){
829 sqlite3_result_int(context, sqlite3_value_int(argv[1]));
830 }else if( sqlite3StrICmp(zArg0,"int64")==0 ){
831 sqlite3_result_int64(context, sqlite3_value_int64(argv[1]));
832 }else if( sqlite3StrICmp(zArg0,"string")==0 ){
danielk1977d8123362004-06-12 09:25:12 +0000833 sqlite3_result_text(context, sqlite3_value_text(argv[1]), -1,
834 SQLITE_TRANSIENT);
danielk19776d88bad2004-05-27 14:23:36 +0000835 }else if( sqlite3StrICmp(zArg0,"double")==0 ){
836 sqlite3_result_double(context, sqlite3_value_double(argv[1]));
837 }else if( sqlite3StrICmp(zArg0,"null")==0 ){
838 sqlite3_result_null(context);
839 }else if( sqlite3StrICmp(zArg0,"value")==0 ){
840 sqlite3_result_value(context, argv[sqlite3_value_int(argv[1])]);
841 }else{
842 goto error_out;
843 }
drh6cbe1f12002-07-01 00:31:36 +0000844 }else{
danielk19776d88bad2004-05-27 14:23:36 +0000845 goto error_out;
drh6cbe1f12002-07-01 00:31:36 +0000846 }
847 argc -= 2;
848 argv += 2;
849 }
danielk19776d88bad2004-05-27 14:23:36 +0000850 return;
851
852error_out:
853 sqlite3_result_error(context,"first argument should be one of: "
854 "int int64 string double null value", -1);
drh6cbe1f12002-07-01 00:31:36 +0000855}
856
857/*
858** Usage: sqlite_register_test_function DB NAME
859**
860** Register the test SQL function on the database DB under the name NAME.
861*/
drhc2eef3b2002-08-31 18:53:06 +0000862static int test_register_func(
drh6cbe1f12002-07-01 00:31:36 +0000863 void *NotUsed,
864 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
865 int argc, /* Number of arguments */
866 char **argv /* Text of each argument */
867){
drh9bb575f2004-09-06 17:24:11 +0000868 sqlite3 *db;
drh6cbe1f12002-07-01 00:31:36 +0000869 int rc;
870 if( argc!=3 ){
871 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
872 " DB FUNCTION-NAME", 0);
873 return TCL_ERROR;
874 }
drhb86ccfb2003-01-28 23:13:10 +0000875 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
danielk1977f9d64d22004-06-19 08:18:07 +0000876 rc = sqlite3_create_function(db, argv[2], -1, SQLITE_UTF8, 0,
danielk1977d8123362004-06-12 09:25:12 +0000877 testFunc, 0, 0);
drh6cbe1f12002-07-01 00:31:36 +0000878 if( rc!=0 ){
danielk1977f20b21c2004-05-31 23:56:42 +0000879 Tcl_AppendResult(interp, sqlite3ErrStr(rc), 0);
drh6cbe1f12002-07-01 00:31:36 +0000880 return TCL_ERROR;
881 }
drhc60d0442004-09-30 13:43:13 +0000882 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
drh6cbe1f12002-07-01 00:31:36 +0000883 return TCL_OK;
884}
885
886/*
danielk1977106bb232004-05-21 10:08:53 +0000887** Usage: sqlite3_finalize STMT
drhb86ccfb2003-01-28 23:13:10 +0000888**
danielk1977106bb232004-05-21 10:08:53 +0000889** Finalize a statement handle.
drhb86ccfb2003-01-28 23:13:10 +0000890*/
891static int test_finalize(
danielk1977106bb232004-05-21 10:08:53 +0000892 void * clientData,
893 Tcl_Interp *interp,
894 int objc,
895 Tcl_Obj *CONST objv[]
drhb86ccfb2003-01-28 23:13:10 +0000896){
danielk1977106bb232004-05-21 10:08:53 +0000897 sqlite3_stmt *pStmt;
drhb86ccfb2003-01-28 23:13:10 +0000898 int rc;
drhc60d0442004-09-30 13:43:13 +0000899 sqlite3 *db;
danielk1977106bb232004-05-21 10:08:53 +0000900
901 if( objc!=2 ){
902 Tcl_AppendResult(interp, "wrong # args: should be \"",
903 Tcl_GetStringFromObj(objv[0], 0), " <STMT>", 0);
drhb86ccfb2003-01-28 23:13:10 +0000904 return TCL_ERROR;
905 }
danielk1977106bb232004-05-21 10:08:53 +0000906
907 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
908
danielk19774397de52005-01-12 12:44:03 +0000909 if( pStmt ){
910 db = StmtToDb(pStmt);
911 }
danielk1977fc57d7b2004-05-26 02:04:57 +0000912 rc = sqlite3_finalize(pStmt);
danielk1977b77f5da2004-05-26 13:27:00 +0000913 Tcl_SetResult(interp, (char *)errorName(rc), TCL_STATIC);
danielk19774397de52005-01-12 12:44:03 +0000914 if( db && sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
danielk1977106bb232004-05-21 10:08:53 +0000915 return TCL_OK;
916}
917
918/*
919** Usage: sqlite3_reset STMT
920**
921** Finalize a statement handle.
922*/
923static int test_reset(
924 void * clientData,
925 Tcl_Interp *interp,
926 int objc,
927 Tcl_Obj *CONST objv[]
928){
929 sqlite3_stmt *pStmt;
930 int rc;
931
932 if( objc!=2 ){
933 Tcl_AppendResult(interp, "wrong # args: should be \"",
934 Tcl_GetStringFromObj(objv[0], 0), " <STMT>", 0);
935 return TCL_ERROR;
936 }
937
938 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
939
danielk1977fc57d7b2004-05-26 02:04:57 +0000940 rc = sqlite3_reset(pStmt);
danielk19774397de52005-01-12 12:44:03 +0000941 if( pStmt &&
942 sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
943 Tcl_SetResult(interp, (char *)errorName(rc), TCL_STATIC);
danielk1977106bb232004-05-21 10:08:53 +0000944 if( rc ){
drhb86ccfb2003-01-28 23:13:10 +0000945 return TCL_ERROR;
946 }
947 return TCL_OK;
948}
949
drh5a387052003-01-11 14:19:51 +0000950/*
drhd89bd002005-01-22 03:03:54 +0000951** Usage: sqlite3_expired STMT
952**
953** Return TRUE if a recompilation of the statement is recommended.
954*/
955static int test_expired(
956 void * clientData,
957 Tcl_Interp *interp,
958 int objc,
959 Tcl_Obj *CONST objv[]
960){
961 sqlite3_stmt *pStmt;
962 if( objc!=2 ){
963 Tcl_AppendResult(interp, "wrong # args: should be \"",
964 Tcl_GetStringFromObj(objv[0], 0), " <STMT>", 0);
965 return TCL_ERROR;
966 }
967 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
968 Tcl_SetObjResult(interp, Tcl_NewBooleanObj(sqlite3_expired(pStmt)));
969 return TCL_OK;
970}
971
972/*
drhf8db1bc2005-04-22 02:38:37 +0000973** Usage: sqlite3_transfer_bindings FROMSTMT TOSTMT
974**
975** Transfer all bindings from FROMSTMT over to TOSTMT
976*/
977static int test_transfer_bind(
978 void * clientData,
979 Tcl_Interp *interp,
980 int objc,
981 Tcl_Obj *CONST objv[]
982){
983 sqlite3_stmt *pStmt1, *pStmt2;
984 if( objc!=3 ){
985 Tcl_AppendResult(interp, "wrong # args: should be \"",
986 Tcl_GetStringFromObj(objv[0], 0), " FROM-STMT TO-STMT", 0);
987 return TCL_ERROR;
988 }
989 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt1)) return TCL_ERROR;
990 if( getStmtPointer(interp, Tcl_GetString(objv[2]), &pStmt2)) return TCL_ERROR;
991 Tcl_SetObjResult(interp,
992 Tcl_NewIntObj(sqlite3_transfer_bindings(pStmt1,pStmt2)));
993 return TCL_OK;
994}
995
996/*
danielk1977fbcd5852004-06-15 02:44:18 +0000997** Usage: sqlite3_changes DB
drh50457892003-09-06 01:10:47 +0000998**
danielk1977fbcd5852004-06-15 02:44:18 +0000999** Return the number of changes made to the database by the last SQL
1000** execution.
drh50457892003-09-06 01:10:47 +00001001*/
danielk1977fbcd5852004-06-15 02:44:18 +00001002static int test_changes(
1003 void * clientData,
1004 Tcl_Interp *interp,
1005 int objc,
1006 Tcl_Obj *CONST objv[]
1007){
1008 sqlite3 *db;
1009 if( objc!=2 ){
1010 Tcl_AppendResult(interp, "wrong # args: should be \"",
1011 Tcl_GetString(objv[0]), " DB", 0);
1012 return TCL_ERROR;
1013 }
1014 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
1015 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_changes(db)));
1016 return TCL_OK;
1017}
drh50457892003-09-06 01:10:47 +00001018
1019/*
drh7c972de2003-09-06 22:18:07 +00001020** This is the "static_bind_value" that variables are bound to when
danielk19776f8a5032004-05-10 10:34:51 +00001021** the FLAG option of sqlite3_bind is "static"
drh50457892003-09-06 01:10:47 +00001022*/
drh7c972de2003-09-06 22:18:07 +00001023static char *sqlite_static_bind_value = 0;
1024
1025/*
danielk19776f8a5032004-05-10 10:34:51 +00001026** Usage: sqlite3_bind VM IDX VALUE FLAGS
drh7c972de2003-09-06 22:18:07 +00001027**
1028** Sets the value of the IDX-th occurance of "?" in the original SQL
1029** string. VALUE is the new value. If FLAGS=="null" then VALUE is
1030** ignored and the value is set to NULL. If FLAGS=="static" then
1031** the value is set to the value of a static variable named
1032** "sqlite_static_bind_value". If FLAGS=="normal" then a copy
1033** of the VALUE is made.
1034*/
1035static int test_bind(
drh50457892003-09-06 01:10:47 +00001036 void *NotUsed,
1037 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1038 int argc, /* Number of arguments */
1039 char **argv /* Text of each argument */
1040){
danielk1977fc57d7b2004-05-26 02:04:57 +00001041 sqlite3_stmt *pStmt;
drh50457892003-09-06 01:10:47 +00001042 int rc;
drh7c972de2003-09-06 22:18:07 +00001043 int idx;
1044 if( argc!=5 ){
drh50457892003-09-06 01:10:47 +00001045 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
drh7c972de2003-09-06 22:18:07 +00001046 " VM IDX VALUE (null|static|normal)\"", 0);
drh50457892003-09-06 01:10:47 +00001047 return TCL_ERROR;
1048 }
danielk1977fc57d7b2004-05-26 02:04:57 +00001049 if( getStmtPointer(interp, argv[1], &pStmt) ) return TCL_ERROR;
drh7c972de2003-09-06 22:18:07 +00001050 if( Tcl_GetInt(interp, argv[2], &idx) ) return TCL_ERROR;
1051 if( strcmp(argv[4],"null")==0 ){
danielk1977fc57d7b2004-05-26 02:04:57 +00001052 rc = sqlite3_bind_null(pStmt, idx);
drh7c972de2003-09-06 22:18:07 +00001053 }else if( strcmp(argv[4],"static")==0 ){
danielk1977fc57d7b2004-05-26 02:04:57 +00001054 rc = sqlite3_bind_text(pStmt, idx, sqlite_static_bind_value, -1, 0);
drh7c972de2003-09-06 22:18:07 +00001055 }else if( strcmp(argv[4],"normal")==0 ){
danielk1977d8123362004-06-12 09:25:12 +00001056 rc = sqlite3_bind_text(pStmt, idx, argv[3], -1, SQLITE_TRANSIENT);
drh7c972de2003-09-06 22:18:07 +00001057 }else{
1058 Tcl_AppendResult(interp, "4th argument should be "
1059 "\"null\" or \"static\" or \"normal\"", 0);
1060 return TCL_ERROR;
1061 }
drhc60d0442004-09-30 13:43:13 +00001062 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
drh50457892003-09-06 01:10:47 +00001063 if( rc ){
1064 char zBuf[50];
1065 sprintf(zBuf, "(%d) ", rc);
danielk1977f20b21c2004-05-31 23:56:42 +00001066 Tcl_AppendResult(interp, zBuf, sqlite3ErrStr(rc), 0);
drh50457892003-09-06 01:10:47 +00001067 return TCL_ERROR;
1068 }
1069 return TCL_OK;
1070}
1071
drh5436dc22004-11-14 04:04:17 +00001072#ifndef SQLITE_OMIT_UTF16
danielk19774e6af132004-06-10 14:01:08 +00001073/*
1074** Usage: add_test_collate <db ptr> <utf8> <utf16le> <utf16be>
1075**
1076** This function is used to test that SQLite selects the correct collation
1077** sequence callback when multiple versions (for different text encodings)
1078** are available.
1079**
1080** Calling this routine registers the collation sequence "test_collate"
1081** with database handle <db>. The second argument must be a list of three
1082** boolean values. If the first is true, then a version of test_collate is
1083** registered for UTF-8, if the second is true, a version is registered for
1084** UTF-16le, if the third is true, a UTF-16be version is available.
1085** Previous versions of test_collate are deleted.
1086**
1087** The collation sequence test_collate is implemented by calling the
1088** following TCL script:
1089**
1090** "test_collate <enc> <lhs> <rhs>"
1091**
1092** The <lhs> and <rhs> are the two values being compared, encoded in UTF-8.
1093** The <enc> parameter is the encoding of the collation function that
1094** SQLite selected to call. The TCL test script implements the
1095** "test_collate" proc.
1096**
1097** Note that this will only work with one intepreter at a time, as the
1098** interp pointer to use when evaluating the TCL script is stored in
1099** pTestCollateInterp.
1100*/
1101static Tcl_Interp* pTestCollateInterp;
1102static int test_collate_func(
1103 void *pCtx,
1104 int nA, const void *zA,
1105 int nB, const void *zB
1106){
1107 Tcl_Interp *i = pTestCollateInterp;
1108 int encin = (int)pCtx;
1109 int res;
1110
1111 sqlite3_value *pVal;
1112 Tcl_Obj *pX;
1113
1114 pX = Tcl_NewStringObj("test_collate", -1);
1115 Tcl_IncrRefCount(pX);
1116
1117 switch( encin ){
1118 case SQLITE_UTF8:
1119 Tcl_ListObjAppendElement(i,pX,Tcl_NewStringObj("UTF-8",-1));
1120 break;
1121 case SQLITE_UTF16LE:
1122 Tcl_ListObjAppendElement(i,pX,Tcl_NewStringObj("UTF-16LE",-1));
1123 break;
1124 case SQLITE_UTF16BE:
1125 Tcl_ListObjAppendElement(i,pX,Tcl_NewStringObj("UTF-16BE",-1));
1126 break;
1127 default:
1128 assert(0);
1129 }
1130
1131 pVal = sqlite3ValueNew();
danielk1977bfd6cce2004-06-18 04:24:54 +00001132 sqlite3ValueSetStr(pVal, nA, zA, encin, SQLITE_STATIC);
danielk19774e6af132004-06-10 14:01:08 +00001133 Tcl_ListObjAppendElement(i,pX,Tcl_NewStringObj(sqlite3_value_text(pVal),-1));
danielk1977bfd6cce2004-06-18 04:24:54 +00001134 sqlite3ValueSetStr(pVal, nB, zB, encin, SQLITE_STATIC);
danielk19774e6af132004-06-10 14:01:08 +00001135 Tcl_ListObjAppendElement(i,pX,Tcl_NewStringObj(sqlite3_value_text(pVal),-1));
1136 sqlite3ValueFree(pVal);
1137
1138 Tcl_EvalObjEx(i, pX, 0);
1139 Tcl_DecrRefCount(pX);
1140 Tcl_GetIntFromObj(i, Tcl_GetObjResult(i), &res);
1141 return res;
1142}
1143static int test_collate(
1144 void * clientData,
1145 Tcl_Interp *interp,
1146 int objc,
1147 Tcl_Obj *CONST objv[]
1148){
1149 sqlite3 *db;
1150 int val;
danielk1977312d6b32004-06-29 13:18:23 +00001151 sqlite3_value *pVal;
drhc60d0442004-09-30 13:43:13 +00001152 int rc;
danielk19774e6af132004-06-10 14:01:08 +00001153
1154 if( objc!=5 ) goto bad_args;
1155 pTestCollateInterp = interp;
1156 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
1157
1158 if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[2], &val) ) return TCL_ERROR;
drhc60d0442004-09-30 13:43:13 +00001159 rc = sqlite3_create_collation(db, "test_collate", SQLITE_UTF8,
1160 (void *)SQLITE_UTF8, val?test_collate_func:0);
1161 if( rc==SQLITE_OK ){
1162 if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[3], &val) ) return TCL_ERROR;
1163 rc = sqlite3_create_collation(db, "test_collate", SQLITE_UTF16LE,
1164 (void *)SQLITE_UTF16LE, val?test_collate_func:0);
1165 if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[4], &val) ) return TCL_ERROR;
danielk1977312d6b32004-06-29 13:18:23 +00001166
drhc60d0442004-09-30 13:43:13 +00001167 pVal = sqlite3ValueNew();
1168 sqlite3ValueSetStr(pVal, -1, "test_collate", SQLITE_UTF8, SQLITE_STATIC);
1169 sqlite3_create_collation16(db, sqlite3ValueText(pVal, SQLITE_UTF16NATIVE),
1170 SQLITE_UTF16BE, (void *)SQLITE_UTF16BE, val?test_collate_func:0);
1171 sqlite3ValueFree(pVal);
1172 }
1173 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
danielk19774e6af132004-06-10 14:01:08 +00001174 return TCL_OK;
1175
1176bad_args:
1177 Tcl_AppendResult(interp, "wrong # args: should be \"",
1178 Tcl_GetStringFromObj(objv[0], 0), " <DB> <utf8> <utf16le> <utf16be>", 0);
1179 return TCL_ERROR;
1180}
1181
danielk1977312d6b32004-06-29 13:18:23 +00001182static void test_collate_needed_cb(
1183 void *pCtx,
1184 sqlite3 *db,
1185 int eTextRep,
1186 const void *notUsed
1187){
1188 int enc = db->enc;
1189 sqlite3_create_collation(
1190 db, "test_collate", db->enc, (void *)enc, test_collate_func);
1191}
1192
1193/*
1194** Usage: add_test_collate_needed DB
1195*/
1196static int test_collate_needed(
1197 void * clientData,
1198 Tcl_Interp *interp,
1199 int objc,
1200 Tcl_Obj *CONST objv[]
1201){
1202 sqlite3 *db;
drhc60d0442004-09-30 13:43:13 +00001203 int rc;
danielk1977312d6b32004-06-29 13:18:23 +00001204
1205 if( objc!=2 ) goto bad_args;
1206 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
drhc60d0442004-09-30 13:43:13 +00001207 rc = sqlite3_collation_needed16(db, 0, test_collate_needed_cb);
1208 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
danielk1977312d6b32004-06-29 13:18:23 +00001209 return TCL_OK;
1210
1211bad_args:
1212 Tcl_WrongNumArgs(interp, 1, objv, "DB");
1213 return TCL_ERROR;
1214}
drh5436dc22004-11-14 04:04:17 +00001215#endif /* SQLITE_OMIT_UTF16 */
danielk1977312d6b32004-06-29 13:18:23 +00001216
danielk1977c8e9a2d2004-06-25 12:08:46 +00001217/*
1218** Usage: add_test_function <db ptr> <utf8> <utf16le> <utf16be>
1219**
1220** This function is used to test that SQLite selects the correct user
1221** function callback when multiple versions (for different text encodings)
1222** are available.
1223**
1224** Calling this routine registers up to three versions of the user function
1225** "test_function" with database handle <db>. If the second argument is
1226** true, then a version of test_function is registered for UTF-8, if the
1227** third is true, a version is registered for UTF-16le, if the fourth is
1228** true, a UTF-16be version is available. Previous versions of
1229** test_function are deleted.
1230**
1231** The user function is implemented by calling the following TCL script:
1232**
1233** "test_function <enc> <arg>"
1234**
1235** Where <enc> is one of UTF-8, UTF-16LE or UTF16BE, and <arg> is the
1236** single argument passed to the SQL function. The value returned by
1237** the TCL script is used as the return value of the SQL function. It
1238** is passed to SQLite using UTF-16BE for a UTF-8 test_function(), UTF-8
1239** for a UTF-16LE test_function(), and UTF-16LE for an implementation that
1240** prefers UTF-16BE.
1241*/
drh5436dc22004-11-14 04:04:17 +00001242#ifndef SQLITE_OMIT_UTF16
danielk1977c8e9a2d2004-06-25 12:08:46 +00001243static void test_function_utf8(
1244 sqlite3_context *pCtx,
1245 int nArg,
1246 sqlite3_value **argv
1247){
1248 Tcl_Interp *interp;
1249 Tcl_Obj *pX;
1250 sqlite3_value *pVal;
1251 interp = (Tcl_Interp *)sqlite3_user_data(pCtx);
1252 pX = Tcl_NewStringObj("test_function", -1);
1253 Tcl_IncrRefCount(pX);
1254 Tcl_ListObjAppendElement(interp, pX, Tcl_NewStringObj("UTF-8", -1));
1255 Tcl_ListObjAppendElement(interp, pX,
1256 Tcl_NewStringObj(sqlite3_value_text(argv[0]), -1));
1257 Tcl_EvalObjEx(interp, pX, 0);
1258 Tcl_DecrRefCount(pX);
1259 sqlite3_result_text(pCtx, Tcl_GetStringResult(interp), -1, SQLITE_TRANSIENT);
1260 pVal = sqlite3ValueNew();
1261 sqlite3ValueSetStr(pVal, -1, Tcl_GetStringResult(interp),
1262 SQLITE_UTF8, SQLITE_STATIC);
1263 sqlite3_result_text16be(pCtx, sqlite3_value_text16be(pVal),
1264 -1, SQLITE_TRANSIENT);
1265 sqlite3ValueFree(pVal);
1266}
1267static void test_function_utf16le(
1268 sqlite3_context *pCtx,
1269 int nArg,
1270 sqlite3_value **argv
1271){
1272 Tcl_Interp *interp;
1273 Tcl_Obj *pX;
1274 sqlite3_value *pVal;
1275 interp = (Tcl_Interp *)sqlite3_user_data(pCtx);
1276 pX = Tcl_NewStringObj("test_function", -1);
1277 Tcl_IncrRefCount(pX);
1278 Tcl_ListObjAppendElement(interp, pX, Tcl_NewStringObj("UTF-16LE", -1));
1279 Tcl_ListObjAppendElement(interp, pX,
1280 Tcl_NewStringObj(sqlite3_value_text(argv[0]), -1));
1281 Tcl_EvalObjEx(interp, pX, 0);
1282 Tcl_DecrRefCount(pX);
1283 pVal = sqlite3ValueNew();
1284 sqlite3ValueSetStr(pVal, -1, Tcl_GetStringResult(interp),
1285 SQLITE_UTF8, SQLITE_STATIC);
1286 sqlite3_result_text(pCtx,sqlite3_value_text(pVal),-1,SQLITE_TRANSIENT);
1287 sqlite3ValueFree(pVal);
1288}
1289static void test_function_utf16be(
1290 sqlite3_context *pCtx,
1291 int nArg,
1292 sqlite3_value **argv
1293){
1294 Tcl_Interp *interp;
1295 Tcl_Obj *pX;
1296 sqlite3_value *pVal;
1297 interp = (Tcl_Interp *)sqlite3_user_data(pCtx);
1298 pX = Tcl_NewStringObj("test_function", -1);
1299 Tcl_IncrRefCount(pX);
1300 Tcl_ListObjAppendElement(interp, pX, Tcl_NewStringObj("UTF-16BE", -1));
1301 Tcl_ListObjAppendElement(interp, pX,
1302 Tcl_NewStringObj(sqlite3_value_text(argv[0]), -1));
1303 Tcl_EvalObjEx(interp, pX, 0);
1304 Tcl_DecrRefCount(pX);
1305 pVal = sqlite3ValueNew();
1306 sqlite3ValueSetStr(pVal, -1, Tcl_GetStringResult(interp),
1307 SQLITE_UTF8, SQLITE_STATIC);
1308 sqlite3_result_text16le(pCtx, sqlite3_value_text16le(pVal),
1309 -1, SQLITE_TRANSIENT);
1310 sqlite3ValueFree(pVal);
1311}
drh5436dc22004-11-14 04:04:17 +00001312#endif /* SQLITE_OMIT_UTF16 */
danielk1977c8e9a2d2004-06-25 12:08:46 +00001313static int test_function(
1314 void * clientData,
1315 Tcl_Interp *interp,
1316 int objc,
1317 Tcl_Obj *CONST objv[]
1318){
drh5436dc22004-11-14 04:04:17 +00001319#ifndef SQLITE_OMIT_UTF16
danielk1977c8e9a2d2004-06-25 12:08:46 +00001320 sqlite3 *db;
1321 int val;
1322
1323 if( objc!=5 ) goto bad_args;
1324 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
1325
1326 if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[2], &val) ) return TCL_ERROR;
1327 if( val ){
1328 sqlite3_create_function(db, "test_function", 1, SQLITE_UTF8,
1329 interp, test_function_utf8, 0, 0);
1330 }
1331 if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[3], &val) ) return TCL_ERROR;
1332 if( val ){
1333 sqlite3_create_function(db, "test_function", 1, SQLITE_UTF16LE,
1334 interp, test_function_utf16le, 0, 0);
1335 }
1336 if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[4], &val) ) return TCL_ERROR;
1337 if( val ){
1338 sqlite3_create_function(db, "test_function", 1, SQLITE_UTF16BE,
1339 interp, test_function_utf16be, 0, 0);
1340 }
1341
1342 return TCL_OK;
1343bad_args:
1344 Tcl_AppendResult(interp, "wrong # args: should be \"",
1345 Tcl_GetStringFromObj(objv[0], 0), " <DB> <utf8> <utf16le> <utf16be>", 0);
drh5436dc22004-11-14 04:04:17 +00001346#endif /* SQLITE_OMIT_UTF16 */
danielk1977c8e9a2d2004-06-25 12:08:46 +00001347 return TCL_ERROR;
1348}
1349
danielk1977312d6b32004-06-29 13:18:23 +00001350/*
1351** Usage: test_errstr <err code>
1352**
1353** Test that the english language string equivalents for sqlite error codes
1354** are sane. The parameter is an integer representing an sqlite error code.
1355** The result is a list of two elements, the string representation of the
1356** error code and the english language explanation.
1357*/
1358static int test_errstr(
1359 void * clientData,
1360 Tcl_Interp *interp,
1361 int objc,
1362 Tcl_Obj *CONST objv[]
1363){
1364 char *zCode;
1365 int i;
1366 if( objc!=1 ){
1367 Tcl_WrongNumArgs(interp, 1, objv, "<error code>");
1368 }
1369
1370 zCode = Tcl_GetString(objv[1]);
1371 for(i=0; i<200; i++){
1372 if( 0==strcmp(errorName(i), zCode) ) break;
1373 }
1374 Tcl_SetResult(interp, (char *)sqlite3ErrStr(i), 0);
1375 return TCL_OK;
1376}
1377
danielk1977c8e9a2d2004-06-25 12:08:46 +00001378static int sqlite3_crashparams(
danielk19778a6c5502004-06-22 12:18:32 +00001379 void * clientData,
1380 Tcl_Interp *interp,
1381 int objc,
1382 Tcl_Obj *CONST objv[]
1383){
1384#ifdef OS_TEST
danielk1977c8e9a2d2004-06-25 12:08:46 +00001385 int delay;
1386 if( objc!=3 ) goto bad_args;
1387 if( Tcl_GetIntFromObj(interp, objv[1], &delay) ) return TCL_ERROR;
1388 sqlite3SetCrashParams(delay, Tcl_GetString(objv[2]));
danielk19778a6c5502004-06-22 12:18:32 +00001389#endif
1390 return TCL_OK;
1391
drh241db312004-06-22 12:46:53 +00001392#ifdef OS_TEST
danielk19778a6c5502004-06-22 12:18:32 +00001393bad_args:
1394 Tcl_AppendResult(interp, "wrong # args: should be \"",
danielk1977c8e9a2d2004-06-25 12:08:46 +00001395 Tcl_GetStringFromObj(objv[0], 0), "<delay> <filename>", 0);
danielk19778a6c5502004-06-22 12:18:32 +00001396 return TCL_ERROR;
drh241db312004-06-22 12:46:53 +00001397#endif
danielk19778a6c5502004-06-22 12:18:32 +00001398}
1399
1400
drh50457892003-09-06 01:10:47 +00001401/*
drh99ee3602003-02-16 19:13:36 +00001402** Usage: breakpoint
1403**
1404** This routine exists for one purpose - to provide a place to put a
1405** breakpoint with GDB that can be triggered using TCL code. The use
1406** for this is when a particular test fails on (say) the 1485th iteration.
1407** In the TCL test script, we can add code like this:
1408**
1409** if {$i==1485} breakpoint
1410**
1411** Then run testfixture in the debugger and wait for the breakpoint to
1412** fire. Then additional breakpoints can be set to trace down the bug.
1413*/
1414static int test_breakpoint(
1415 void *NotUsed,
1416 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1417 int argc, /* Number of arguments */
1418 char **argv /* Text of each argument */
1419){
1420 return TCL_OK; /* Do nothing */
1421}
1422
drh241db312004-06-22 12:46:53 +00001423/*
1424** Usage: sqlite3_bind_int STMT N VALUE
1425**
1426** Test the sqlite3_bind_int interface. STMT is a prepared statement.
1427** N is the index of a wildcard in the prepared statement. This command
1428** binds a 32-bit integer VALUE to that wildcard.
1429*/
1430static int test_bind_int(
danielk197751e3d8e2004-05-20 01:12:34 +00001431 void * clientData,
1432 Tcl_Interp *interp,
1433 int objc,
1434 Tcl_Obj *CONST objv[]
1435){
1436 sqlite3_stmt *pStmt;
1437 int idx;
1438 int value;
1439 int rc;
1440
1441 if( objc!=4 ){
1442 Tcl_AppendResult(interp, "wrong # args: should be \"",
drh241db312004-06-22 12:46:53 +00001443 Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE", 0);
danielk197751e3d8e2004-05-20 01:12:34 +00001444 return TCL_ERROR;
1445 }
1446
1447 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
1448 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
1449 if( Tcl_GetIntFromObj(interp, objv[3], &value) ) return TCL_ERROR;
1450
danielk1977c572ef72004-05-27 09:28:41 +00001451 rc = sqlite3_bind_int(pStmt, idx, value);
drhc60d0442004-09-30 13:43:13 +00001452 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
danielk197751e3d8e2004-05-20 01:12:34 +00001453 if( rc!=SQLITE_OK ){
1454 return TCL_ERROR;
1455 }
1456
1457 return TCL_OK;
1458}
1459
drh241db312004-06-22 12:46:53 +00001460
1461/*
1462** Usage: sqlite3_bind_int64 STMT N VALUE
1463**
1464** Test the sqlite3_bind_int64 interface. STMT is a prepared statement.
1465** N is the index of a wildcard in the prepared statement. This command
1466** binds a 64-bit integer VALUE to that wildcard.
1467*/
danielk197751e3d8e2004-05-20 01:12:34 +00001468static int test_bind_int64(
1469 void * clientData,
1470 Tcl_Interp *interp,
1471 int objc,
1472 Tcl_Obj *CONST objv[]
1473){
1474 sqlite3_stmt *pStmt;
1475 int idx;
1476 i64 value;
1477 int rc;
1478
1479 if( objc!=4 ){
1480 Tcl_AppendResult(interp, "wrong # args: should be \"",
drh241db312004-06-22 12:46:53 +00001481 Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE", 0);
danielk197751e3d8e2004-05-20 01:12:34 +00001482 return TCL_ERROR;
1483 }
1484
1485 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
1486 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
1487 if( Tcl_GetWideIntFromObj(interp, objv[3], &value) ) return TCL_ERROR;
1488
1489 rc = sqlite3_bind_int64(pStmt, idx, value);
drhc60d0442004-09-30 13:43:13 +00001490 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
danielk197751e3d8e2004-05-20 01:12:34 +00001491 if( rc!=SQLITE_OK ){
1492 return TCL_ERROR;
1493 }
1494
1495 return TCL_OK;
1496}
1497
drh241db312004-06-22 12:46:53 +00001498
1499/*
1500** Usage: sqlite3_bind_double STMT N VALUE
1501**
1502** Test the sqlite3_bind_double interface. STMT is a prepared statement.
1503** N is the index of a wildcard in the prepared statement. This command
1504** binds a 64-bit integer VALUE to that wildcard.
1505*/
danielk197751e3d8e2004-05-20 01:12:34 +00001506static int test_bind_double(
1507 void * clientData,
1508 Tcl_Interp *interp,
1509 int objc,
1510 Tcl_Obj *CONST objv[]
1511){
1512 sqlite3_stmt *pStmt;
1513 int idx;
1514 double value;
1515 int rc;
1516
1517 if( objc!=4 ){
1518 Tcl_AppendResult(interp, "wrong # args: should be \"",
drh241db312004-06-22 12:46:53 +00001519 Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE", 0);
danielk197751e3d8e2004-05-20 01:12:34 +00001520 return TCL_ERROR;
1521 }
1522
1523 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
1524 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
1525 if( Tcl_GetDoubleFromObj(interp, objv[3], &value) ) return TCL_ERROR;
1526
1527 rc = sqlite3_bind_double(pStmt, idx, value);
drhc60d0442004-09-30 13:43:13 +00001528 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
danielk197751e3d8e2004-05-20 01:12:34 +00001529 if( rc!=SQLITE_OK ){
1530 return TCL_ERROR;
1531 }
1532
1533 return TCL_OK;
1534}
1535
drh241db312004-06-22 12:46:53 +00001536/*
1537** Usage: sqlite3_bind_null STMT N
1538**
1539** Test the sqlite3_bind_null interface. STMT is a prepared statement.
1540** N is the index of a wildcard in the prepared statement. This command
1541** binds a NULL to the wildcard.
1542*/
danielk197751e3d8e2004-05-20 01:12:34 +00001543static int test_bind_null(
1544 void * clientData,
1545 Tcl_Interp *interp,
1546 int objc,
1547 Tcl_Obj *CONST objv[]
1548){
1549 sqlite3_stmt *pStmt;
1550 int idx;
1551 int rc;
1552
1553 if( objc!=3 ){
1554 Tcl_AppendResult(interp, "wrong # args: should be \"",
drh241db312004-06-22 12:46:53 +00001555 Tcl_GetStringFromObj(objv[0], 0), " STMT N", 0);
danielk197751e3d8e2004-05-20 01:12:34 +00001556 return TCL_ERROR;
1557 }
1558
1559 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
1560 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
1561
1562 rc = sqlite3_bind_null(pStmt, idx);
drhc60d0442004-09-30 13:43:13 +00001563 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
danielk197751e3d8e2004-05-20 01:12:34 +00001564 if( rc!=SQLITE_OK ){
1565 return TCL_ERROR;
1566 }
1567
1568 return TCL_OK;
1569}
1570
drh241db312004-06-22 12:46:53 +00001571/*
1572** Usage: sqlite3_bind_text STMT N STRING BYTES
1573**
1574** Test the sqlite3_bind_text interface. STMT is a prepared statement.
1575** N is the index of a wildcard in the prepared statement. This command
1576** binds a UTF-8 string STRING to the wildcard. The string is BYTES bytes
1577** long.
1578*/
danielk197751e3d8e2004-05-20 01:12:34 +00001579static int test_bind_text(
1580 void * clientData,
1581 Tcl_Interp *interp,
1582 int objc,
1583 Tcl_Obj *CONST objv[]
1584){
1585 sqlite3_stmt *pStmt;
1586 int idx;
1587 int bytes;
1588 char *value;
1589 int rc;
1590
1591 if( objc!=5 ){
1592 Tcl_AppendResult(interp, "wrong # args: should be \"",
drh241db312004-06-22 12:46:53 +00001593 Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE BYTES", 0);
danielk197751e3d8e2004-05-20 01:12:34 +00001594 return TCL_ERROR;
1595 }
1596
1597 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
1598 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
1599 value = Tcl_GetString(objv[3]);
1600 if( Tcl_GetIntFromObj(interp, objv[4], &bytes) ) return TCL_ERROR;
1601
danielk1977d8123362004-06-12 09:25:12 +00001602 rc = sqlite3_bind_text(pStmt, idx, value, bytes, SQLITE_TRANSIENT);
drhc60d0442004-09-30 13:43:13 +00001603 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
danielk197751e3d8e2004-05-20 01:12:34 +00001604 if( rc!=SQLITE_OK ){
drh473d1792005-06-06 17:54:55 +00001605 Tcl_AppendResult(interp, sqlite3TestErrorName(rc), 0);
danielk197751e3d8e2004-05-20 01:12:34 +00001606 return TCL_ERROR;
1607 }
1608
1609 return TCL_OK;
1610}
1611
drh241db312004-06-22 12:46:53 +00001612/*
1613** Usage: sqlite3_bind_text16 STMT N STRING BYTES
1614**
1615** Test the sqlite3_bind_text16 interface. STMT is a prepared statement.
1616** N is the index of a wildcard in the prepared statement. This command
1617** binds a UTF-16 string STRING to the wildcard. The string is BYTES bytes
1618** long.
1619*/
danielk197751e3d8e2004-05-20 01:12:34 +00001620static int test_bind_text16(
1621 void * clientData,
1622 Tcl_Interp *interp,
1623 int objc,
1624 Tcl_Obj *CONST objv[]
1625){
drh5436dc22004-11-14 04:04:17 +00001626#ifndef SQLITE_OMIT_UTF16
danielk197751e3d8e2004-05-20 01:12:34 +00001627 sqlite3_stmt *pStmt;
1628 int idx;
1629 int bytes;
1630 char *value;
1631 int rc;
1632
1633 if( objc!=5 ){
1634 Tcl_AppendResult(interp, "wrong # args: should be \"",
drh241db312004-06-22 12:46:53 +00001635 Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE BYTES", 0);
danielk197751e3d8e2004-05-20 01:12:34 +00001636 return TCL_ERROR;
1637 }
1638
1639 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
1640 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
1641 value = Tcl_GetByteArrayFromObj(objv[3], 0);
1642 if( Tcl_GetIntFromObj(interp, objv[4], &bytes) ) return TCL_ERROR;
1643
danielk1977d8123362004-06-12 09:25:12 +00001644 rc = sqlite3_bind_text16(pStmt, idx, (void *)value, bytes, SQLITE_TRANSIENT);
drhc60d0442004-09-30 13:43:13 +00001645 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
danielk197751e3d8e2004-05-20 01:12:34 +00001646 if( rc!=SQLITE_OK ){
1647 return TCL_ERROR;
1648 }
1649
drh5436dc22004-11-14 04:04:17 +00001650#endif /* SQLITE_OMIT_UTF16 */
danielk197751e3d8e2004-05-20 01:12:34 +00001651 return TCL_OK;
1652}
1653
drh241db312004-06-22 12:46:53 +00001654/*
1655** Usage: sqlite3_bind_blob STMT N DATA BYTES
1656**
1657** Test the sqlite3_bind_blob interface. STMT is a prepared statement.
1658** N is the index of a wildcard in the prepared statement. This command
1659** binds a BLOB to the wildcard. The BLOB is BYTES bytes in size.
1660*/
danielk197751e3d8e2004-05-20 01:12:34 +00001661static int test_bind_blob(
1662 void * clientData,
1663 Tcl_Interp *interp,
1664 int objc,
1665 Tcl_Obj *CONST objv[]
1666){
1667 sqlite3_stmt *pStmt;
1668 int idx;
1669 int bytes;
1670 char *value;
1671 int rc;
1672
1673 if( objc!=5 ){
1674 Tcl_AppendResult(interp, "wrong # args: should be \"",
drh241db312004-06-22 12:46:53 +00001675 Tcl_GetStringFromObj(objv[0], 0), " STMT N DATA BYTES", 0);
danielk197751e3d8e2004-05-20 01:12:34 +00001676 return TCL_ERROR;
1677 }
1678
1679 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
1680 if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
1681 value = Tcl_GetString(objv[3]);
danielk197749e46432004-05-27 13:55:27 +00001682 if( Tcl_GetIntFromObj(interp, objv[4], &bytes) ) return TCL_ERROR;
danielk197751e3d8e2004-05-20 01:12:34 +00001683
danielk1977d8123362004-06-12 09:25:12 +00001684 rc = sqlite3_bind_blob(pStmt, idx, value, bytes, SQLITE_TRANSIENT);
drhc60d0442004-09-30 13:43:13 +00001685 if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
danielk197751e3d8e2004-05-20 01:12:34 +00001686 if( rc!=SQLITE_OK ){
1687 return TCL_ERROR;
1688 }
1689
1690 return TCL_OK;
1691}
1692
drh99ee3602003-02-16 19:13:36 +00001693/*
drh75f6a032004-07-15 14:15:00 +00001694** Usage: sqlite3_bind_parameter_count STMT
1695**
1696** Return the number of wildcards in the given statement.
1697*/
1698static int test_bind_parameter_count(
1699 void * clientData,
1700 Tcl_Interp *interp,
1701 int objc,
1702 Tcl_Obj *CONST objv[]
1703){
1704 sqlite3_stmt *pStmt;
1705
1706 if( objc!=2 ){
1707 Tcl_WrongNumArgs(interp, 1, objv, "STMT");
1708 return TCL_ERROR;
1709 }
1710 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
1711 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_bind_parameter_count(pStmt)));
1712 return TCL_OK;
1713}
1714
1715/*
drh895d7472004-08-20 16:02:39 +00001716** Usage: sqlite3_bind_parameter_name STMT N
1717**
1718** Return the name of the Nth wildcard. The first wildcard is 1.
1719** An empty string is returned if N is out of range or if the wildcard
1720** is nameless.
1721*/
1722static int test_bind_parameter_name(
1723 void * clientData,
1724 Tcl_Interp *interp,
1725 int objc,
1726 Tcl_Obj *CONST objv[]
1727){
1728 sqlite3_stmt *pStmt;
1729 int i;
1730
1731 if( objc!=3 ){
1732 Tcl_WrongNumArgs(interp, 1, objv, "STMT N");
1733 return TCL_ERROR;
1734 }
1735 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
1736 if( Tcl_GetIntFromObj(interp, objv[2], &i) ) return TCL_ERROR;
1737 Tcl_SetObjResult(interp,
1738 Tcl_NewStringObj(sqlite3_bind_parameter_name(pStmt,i),-1)
1739 );
1740 return TCL_OK;
1741}
1742
1743/*
drhfa6bc002004-09-07 16:19:52 +00001744** Usage: sqlite3_bind_parameter_index STMT NAME
1745**
1746** Return the index of the wildcard called NAME. Return 0 if there is
1747** no such wildcard.
1748*/
1749static int test_bind_parameter_index(
1750 void * clientData,
1751 Tcl_Interp *interp,
1752 int objc,
1753 Tcl_Obj *CONST objv[]
1754){
1755 sqlite3_stmt *pStmt;
1756
1757 if( objc!=3 ){
1758 Tcl_WrongNumArgs(interp, 1, objv, "STMT NAME");
1759 return TCL_ERROR;
1760 }
1761 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
1762 Tcl_SetObjResult(interp,
1763 Tcl_NewIntObj(
1764 sqlite3_bind_parameter_index(pStmt,Tcl_GetString(objv[2]))
1765 )
1766 );
1767 return TCL_OK;
1768}
1769
1770/*
danielk1977600dd0b2005-01-20 01:14:23 +00001771** Usage: sqlite3_clear_bindings STMT
1772**
1773*/
1774#if 0
1775static int test_clear_bindings(
1776 void * clientData,
1777 Tcl_Interp *interp,
1778 int objc,
1779 Tcl_Obj *CONST objv[]
1780){
1781 sqlite3_stmt *pStmt;
1782
1783 if( objc!=2 ){
1784 Tcl_WrongNumArgs(interp, 1, objv, "STMT");
1785 return TCL_ERROR;
1786 }
1787 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
1788 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_clear_bindings(pStmt)));
1789 return TCL_OK;
1790}
1791#endif
1792
1793/*
danielk19776622cce2004-05-20 11:00:52 +00001794** Usage: sqlite3_errcode DB
1795**
1796** Return the string representation of the most recent sqlite3_* API
1797** error code. e.g. "SQLITE_ERROR".
1798*/
1799static int test_errcode(
1800 void * clientData,
1801 Tcl_Interp *interp,
1802 int objc,
1803 Tcl_Obj *CONST objv[]
1804){
1805 sqlite3 *db;
1806
1807 if( objc!=2 ){
1808 Tcl_AppendResult(interp, "wrong # args: should be \"",
1809 Tcl_GetString(objv[0]), " DB", 0);
1810 return TCL_ERROR;
1811 }
1812 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
1813 Tcl_SetResult(interp, (char *)errorName(sqlite3_errcode(db)), 0);
1814 return TCL_OK;
1815}
1816
1817/*
1818** Usage: test_errmsg DB
1819**
1820** Returns the UTF-8 representation of the error message string for the
1821** most recent sqlite3_* API call.
1822*/
1823static int test_errmsg(
1824 void * clientData,
1825 Tcl_Interp *interp,
1826 int objc,
1827 Tcl_Obj *CONST objv[]
1828){
drh9bb575f2004-09-06 17:24:11 +00001829 sqlite3 *db;
danielk19776622cce2004-05-20 11:00:52 +00001830 const char *zErr;
1831
1832 if( objc!=2 ){
1833 Tcl_AppendResult(interp, "wrong # args: should be \"",
1834 Tcl_GetString(objv[0]), " DB", 0);
1835 return TCL_ERROR;
1836 }
1837 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
1838
1839 zErr = sqlite3_errmsg(db);
1840 Tcl_SetObjResult(interp, Tcl_NewStringObj(zErr, -1));
1841 return TCL_OK;
1842}
1843
1844/*
1845** Usage: test_errmsg16 DB
1846**
1847** Returns the UTF-16 representation of the error message string for the
1848** most recent sqlite3_* API call. This is a byte array object at the TCL
1849** level, and it includes the 0x00 0x00 terminator bytes at the end of the
1850** UTF-16 string.
1851*/
1852static int test_errmsg16(
1853 void * clientData,
1854 Tcl_Interp *interp,
1855 int objc,
1856 Tcl_Obj *CONST objv[]
1857){
drh5436dc22004-11-14 04:04:17 +00001858#ifndef SQLITE_OMIT_UTF16
drh9bb575f2004-09-06 17:24:11 +00001859 sqlite3 *db;
danielk19776622cce2004-05-20 11:00:52 +00001860 const void *zErr;
1861 int bytes;
1862
1863 if( objc!=2 ){
1864 Tcl_AppendResult(interp, "wrong # args: should be \"",
1865 Tcl_GetString(objv[0]), " DB", 0);
1866 return TCL_ERROR;
1867 }
1868 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
1869
1870 zErr = sqlite3_errmsg16(db);
1871 bytes = sqlite3utf16ByteLen(zErr, -1);
1872 Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(zErr, bytes));
drh5436dc22004-11-14 04:04:17 +00001873#endif /* SQLITE_OMIT_UTF16 */
danielk19776622cce2004-05-20 11:00:52 +00001874 return TCL_OK;
1875}
1876
1877/*
1878** Usage: sqlite3_prepare DB sql bytes tailvar
1879**
1880** Compile up to <bytes> bytes of the supplied SQL string <sql> using
1881** database handle <DB>. The parameter <tailval> is the name of a global
1882** variable that is set to the unused portion of <sql> (if any). A
1883** STMT handle is returned.
1884*/
1885static int test_prepare(
1886 void * clientData,
1887 Tcl_Interp *interp,
1888 int objc,
1889 Tcl_Obj *CONST objv[]
1890){
1891 sqlite3 *db;
1892 const char *zSql;
1893 int bytes;
1894 const char *zTail = 0;
1895 sqlite3_stmt *pStmt = 0;
1896 char zBuf[50];
danielk19774ad17132004-05-21 01:47:26 +00001897 int rc;
danielk19776622cce2004-05-20 11:00:52 +00001898
1899 if( objc!=5 ){
1900 Tcl_AppendResult(interp, "wrong # args: should be \"",
1901 Tcl_GetString(objv[0]), " DB sql bytes tailvar", 0);
1902 return TCL_ERROR;
1903 }
1904 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
1905 zSql = Tcl_GetString(objv[2]);
1906 if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR;
1907
danielk19774ad17132004-05-21 01:47:26 +00001908 rc = sqlite3_prepare(db, zSql, bytes, &pStmt, &zTail);
drhc60d0442004-09-30 13:43:13 +00001909 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
danielk19776622cce2004-05-20 11:00:52 +00001910 if( zTail ){
1911 if( bytes>=0 ){
1912 bytes = bytes - (zTail-zSql);
1913 }
1914 Tcl_ObjSetVar2(interp, objv[4], 0, Tcl_NewStringObj(zTail, bytes), 0);
1915 }
danielk19774ad17132004-05-21 01:47:26 +00001916 if( rc!=SQLITE_OK ){
1917 assert( pStmt==0 );
1918 sprintf(zBuf, "(%d) ", rc);
1919 Tcl_AppendResult(interp, zBuf, sqlite3_errmsg(db), 0);
1920 return TCL_ERROR;
1921 }
danielk19776622cce2004-05-20 11:00:52 +00001922
danielk19774ad17132004-05-21 01:47:26 +00001923 if( pStmt ){
1924 if( makePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR;
1925 Tcl_AppendResult(interp, zBuf, 0);
1926 }
danielk19776622cce2004-05-20 11:00:52 +00001927 return TCL_OK;
1928}
1929
1930/*
1931** Usage: sqlite3_prepare DB sql bytes tailvar
1932**
1933** Compile up to <bytes> bytes of the supplied SQL string <sql> using
1934** database handle <DB>. The parameter <tailval> is the name of a global
1935** variable that is set to the unused portion of <sql> (if any). A
1936** STMT handle is returned.
1937*/
1938static int test_prepare16(
1939 void * clientData,
1940 Tcl_Interp *interp,
1941 int objc,
1942 Tcl_Obj *CONST objv[]
1943){
drh5436dc22004-11-14 04:04:17 +00001944#ifndef SQLITE_OMIT_UTF16
danielk19776622cce2004-05-20 11:00:52 +00001945 sqlite3 *db;
1946 const void *zSql;
1947 const void *zTail = 0;
1948 Tcl_Obj *pTail = 0;
1949 sqlite3_stmt *pStmt = 0;
drhc60d0442004-09-30 13:43:13 +00001950 char zBuf[50];
1951 int rc;
danielk19776622cce2004-05-20 11:00:52 +00001952 int bytes; /* The integer specified as arg 3 */
1953 int objlen; /* The byte-array length of arg 2 */
1954
1955 if( objc!=5 ){
1956 Tcl_AppendResult(interp, "wrong # args: should be \"",
1957 Tcl_GetString(objv[0]), " DB sql bytes tailvar", 0);
1958 return TCL_ERROR;
1959 }
1960 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
1961 zSql = Tcl_GetByteArrayFromObj(objv[2], &objlen);
1962 if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR;
1963
drhc60d0442004-09-30 13:43:13 +00001964 rc = sqlite3_prepare16(db, zSql, bytes, &pStmt, &zTail);
1965 if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
1966 if( rc ){
danielk19776622cce2004-05-20 11:00:52 +00001967 return TCL_ERROR;
1968 }
1969
1970 if( zTail ){
1971 objlen = objlen - ((u8 *)zTail-(u8 *)zSql);
1972 }else{
1973 objlen = 0;
1974 }
1975 pTail = Tcl_NewByteArrayObj((u8 *)zTail, objlen);
1976 Tcl_IncrRefCount(pTail);
1977 Tcl_ObjSetVar2(interp, objv[4], 0, pTail, 0);
danielk19774ad17132004-05-21 01:47:26 +00001978 Tcl_DecrRefCount(pTail);
danielk19776622cce2004-05-20 11:00:52 +00001979
danielk19774ad17132004-05-21 01:47:26 +00001980 if( pStmt ){
1981 if( makePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR;
1982 }
danielk19776622cce2004-05-20 11:00:52 +00001983 Tcl_AppendResult(interp, zBuf, 0);
drh5436dc22004-11-14 04:04:17 +00001984#endif /* SQLITE_OMIT_UTF16 */
danielk19776622cce2004-05-20 11:00:52 +00001985 return TCL_OK;
1986}
1987
danielk19774ad17132004-05-21 01:47:26 +00001988/*
1989** Usage: sqlite3_open filename ?options-list?
1990*/
1991static int test_open(
1992 void * clientData,
1993 Tcl_Interp *interp,
1994 int objc,
1995 Tcl_Obj *CONST objv[]
1996){
1997 const char *zFilename;
1998 sqlite3 *db;
1999 int rc;
2000 char zBuf[100];
2001
2002 if( objc!=3 && objc!=2 ){
2003 Tcl_AppendResult(interp, "wrong # args: should be \"",
2004 Tcl_GetString(objv[0]), " filename options-list", 0);
2005 return TCL_ERROR;
2006 }
2007
2008 zFilename = Tcl_GetString(objv[1]);
danielk19774f057f92004-06-08 00:02:33 +00002009 rc = sqlite3_open(zFilename, &db);
danielk19774ad17132004-05-21 01:47:26 +00002010
2011 if( makePointerStr(interp, zBuf, db) ) return TCL_ERROR;
2012 Tcl_AppendResult(interp, zBuf, 0);
2013 return TCL_OK;
2014}
2015
2016/*
2017** Usage: sqlite3_open16 filename options
2018*/
2019static int test_open16(
2020 void * clientData,
2021 Tcl_Interp *interp,
2022 int objc,
2023 Tcl_Obj *CONST objv[]
2024){
drh5436dc22004-11-14 04:04:17 +00002025#ifndef SQLITE_OMIT_UTF16
danielk19774ad17132004-05-21 01:47:26 +00002026 const void *zFilename;
2027 sqlite3 *db;
2028 int rc;
2029 char zBuf[100];
2030
2031 if( objc!=3 ){
2032 Tcl_AppendResult(interp, "wrong # args: should be \"",
2033 Tcl_GetString(objv[0]), " filename options-list", 0);
2034 return TCL_ERROR;
2035 }
2036
2037 zFilename = Tcl_GetByteArrayFromObj(objv[1], 0);
danielk19774f057f92004-06-08 00:02:33 +00002038 rc = sqlite3_open16(zFilename, &db);
danielk19774ad17132004-05-21 01:47:26 +00002039
2040 if( makePointerStr(interp, zBuf, db) ) return TCL_ERROR;
2041 Tcl_AppendResult(interp, zBuf, 0);
drh5436dc22004-11-14 04:04:17 +00002042#endif /* SQLITE_OMIT_UTF16 */
danielk19774ad17132004-05-21 01:47:26 +00002043 return TCL_OK;
2044}
drhd3d39e92004-05-20 22:16:29 +00002045
2046/*
danielk1977bc6ada42004-06-30 08:20:16 +00002047** Usage: sqlite3_complete16 <UTF-16 string>
2048**
2049** Return 1 if the supplied argument is a complete SQL statement, or zero
2050** otherwise.
2051*/
2052static int test_complete16(
2053 void * clientData,
2054 Tcl_Interp *interp,
2055 int objc,
2056 Tcl_Obj *CONST objv[]
2057){
drhccae6022005-02-26 17:31:26 +00002058#if !defined(SQLITE_OMIT_COMPLETE) && !defined(SQLITE_OMIT_UTF16)
danielk1977bc6ada42004-06-30 08:20:16 +00002059 char *zBuf;
2060
2061 if( objc!=2 ){
2062 Tcl_WrongNumArgs(interp, 1, objv, "<utf-16 sql>");
2063 return TCL_ERROR;
2064 }
2065
2066 zBuf = Tcl_GetByteArrayFromObj(objv[1], 0);
2067 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_complete16(zBuf)));
drhccae6022005-02-26 17:31:26 +00002068#endif /* SQLITE_OMIT_COMPLETE && SQLITE_OMIT_UTF16 */
danielk1977bc6ada42004-06-30 08:20:16 +00002069 return TCL_OK;
2070}
2071
2072/*
danielk1977106bb232004-05-21 10:08:53 +00002073** Usage: sqlite3_step STMT
2074**
2075** Advance the statement to the next row.
2076*/
danielk197717240fd2004-05-26 00:07:25 +00002077static int test_step(
danielk1977106bb232004-05-21 10:08:53 +00002078 void * clientData,
2079 Tcl_Interp *interp,
2080 int objc,
2081 Tcl_Obj *CONST objv[]
2082){
2083 sqlite3_stmt *pStmt;
2084 int rc;
2085
danielk1977e1cd9872004-05-22 10:33:04 +00002086 if( objc!=2 ){
danielk1977106bb232004-05-21 10:08:53 +00002087 Tcl_AppendResult(interp, "wrong # args: should be \"",
2088 Tcl_GetString(objv[0]), " STMT", 0);
2089 return TCL_ERROR;
2090 }
2091
2092 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
danielk197717240fd2004-05-26 00:07:25 +00002093 rc = sqlite3_step(pStmt);
danielk1977106bb232004-05-21 10:08:53 +00002094
danielk1977fbcd5852004-06-15 02:44:18 +00002095 /* if( rc!=SQLITE_DONE && rc!=SQLITE_ROW ) return TCL_ERROR; */
danielk197765904932004-05-26 06:18:37 +00002096 Tcl_SetResult(interp, (char *)errorName(rc), 0);
danielk1977e1cd9872004-05-22 10:33:04 +00002097 return TCL_OK;
2098}
2099
2100/*
danielk197717240fd2004-05-26 00:07:25 +00002101** Usage: sqlite3_column_count STMT
2102**
2103** Return the number of columns returned by the sql statement STMT.
2104*/
2105static int test_column_count(
2106 void * clientData,
2107 Tcl_Interp *interp,
2108 int objc,
2109 Tcl_Obj *CONST objv[]
2110){
2111 sqlite3_stmt *pStmt;
2112
2113 if( objc!=2 ){
2114 Tcl_AppendResult(interp, "wrong # args: should be \"",
2115 Tcl_GetString(objv[0]), " STMT column", 0);
2116 return TCL_ERROR;
2117 }
2118
2119 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2120
2121 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_column_count(pStmt)));
2122 return TCL_OK;
2123}
2124
2125/*
danielk19773cf86062004-05-26 10:11:05 +00002126** Usage: sqlite3_column_type STMT column
2127**
2128** Return the type of the data in column 'column' of the current row.
2129*/
2130static int test_column_type(
2131 void * clientData,
2132 Tcl_Interp *interp,
2133 int objc,
2134 Tcl_Obj *CONST objv[]
2135){
2136 sqlite3_stmt *pStmt;
2137 int col;
2138 int tp;
2139
2140 if( objc!=3 ){
2141 Tcl_AppendResult(interp, "wrong # args: should be \"",
2142 Tcl_GetString(objv[0]), " STMT column", 0);
2143 return TCL_ERROR;
2144 }
2145
2146 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2147 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
2148
2149 tp = sqlite3_column_type(pStmt, col);
2150 switch( tp ){
drh9c054832004-05-31 18:51:57 +00002151 case SQLITE_INTEGER:
danielk19773cf86062004-05-26 10:11:05 +00002152 Tcl_SetResult(interp, "INTEGER", TCL_STATIC);
2153 break;
drh9c054832004-05-31 18:51:57 +00002154 case SQLITE_NULL:
danielk19773cf86062004-05-26 10:11:05 +00002155 Tcl_SetResult(interp, "NULL", TCL_STATIC);
2156 break;
drh9c054832004-05-31 18:51:57 +00002157 case SQLITE_FLOAT:
danielk19773cf86062004-05-26 10:11:05 +00002158 Tcl_SetResult(interp, "FLOAT", TCL_STATIC);
2159 break;
drh9c054832004-05-31 18:51:57 +00002160 case SQLITE_TEXT:
danielk19773cf86062004-05-26 10:11:05 +00002161 Tcl_SetResult(interp, "TEXT", TCL_STATIC);
2162 break;
drh9c054832004-05-31 18:51:57 +00002163 case SQLITE_BLOB:
danielk19773cf86062004-05-26 10:11:05 +00002164 Tcl_SetResult(interp, "BLOB", TCL_STATIC);
2165 break;
2166 default:
2167 assert(0);
2168 }
2169
2170 return TCL_OK;
2171}
2172
2173/*
danielk197704f2e682004-05-27 01:04:07 +00002174** Usage: sqlite3_column_int64 STMT column
danielk19773cf86062004-05-26 10:11:05 +00002175**
2176** Return the data in column 'column' of the current row cast as an
danielk197704f2e682004-05-27 01:04:07 +00002177** wide (64-bit) integer.
danielk19773cf86062004-05-26 10:11:05 +00002178*/
danielk197704f2e682004-05-27 01:04:07 +00002179static int test_column_int64(
danielk19773cf86062004-05-26 10:11:05 +00002180 void * clientData,
2181 Tcl_Interp *interp,
2182 int objc,
2183 Tcl_Obj *CONST objv[]
2184){
2185 sqlite3_stmt *pStmt;
2186 int col;
danielk197704f2e682004-05-27 01:04:07 +00002187 i64 iVal;
danielk19773cf86062004-05-26 10:11:05 +00002188
2189 if( objc!=3 ){
2190 Tcl_AppendResult(interp, "wrong # args: should be \"",
2191 Tcl_GetString(objv[0]), " STMT column", 0);
2192 return TCL_ERROR;
2193 }
2194
2195 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2196 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
2197
danielk197704f2e682004-05-27 01:04:07 +00002198 iVal = sqlite3_column_int64(pStmt, col);
2199 Tcl_SetObjResult(interp, Tcl_NewWideIntObj(iVal));
2200 return TCL_OK;
2201}
2202
2203/*
danielk1977ea61b2c2004-05-27 01:49:51 +00002204** Usage: sqlite3_column_blob STMT column
2205*/
2206static int test_column_blob(
2207 void * clientData,
2208 Tcl_Interp *interp,
2209 int objc,
2210 Tcl_Obj *CONST objv[]
2211){
2212 sqlite3_stmt *pStmt;
2213 int col;
2214
2215 int len;
danielk1977c572ef72004-05-27 09:28:41 +00002216 const void *pBlob;
danielk1977ea61b2c2004-05-27 01:49:51 +00002217
2218 if( objc!=3 ){
2219 Tcl_AppendResult(interp, "wrong # args: should be \"",
2220 Tcl_GetString(objv[0]), " STMT column", 0);
2221 return TCL_ERROR;
2222 }
2223
2224 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2225 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
2226
2227 pBlob = sqlite3_column_blob(pStmt, col);
2228 len = sqlite3_column_bytes(pStmt, col);
2229 Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(pBlob, len));
2230 return TCL_OK;
2231}
2232
2233/*
danielk197704f2e682004-05-27 01:04:07 +00002234** Usage: sqlite3_column_double STMT column
2235**
2236** Return the data in column 'column' of the current row cast as a double.
2237*/
2238static int test_column_double(
2239 void * clientData,
2240 Tcl_Interp *interp,
2241 int objc,
2242 Tcl_Obj *CONST objv[]
2243){
2244 sqlite3_stmt *pStmt;
2245 int col;
2246 double rVal;
2247
2248 if( objc!=3 ){
2249 Tcl_AppendResult(interp, "wrong # args: should be \"",
2250 Tcl_GetString(objv[0]), " STMT column", 0);
2251 return TCL_ERROR;
2252 }
2253
2254 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2255 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
2256
2257 rVal = sqlite3_column_double(pStmt, col);
danielk1977c572ef72004-05-27 09:28:41 +00002258 Tcl_SetObjResult(interp, Tcl_NewDoubleObj(rVal));
danielk19773cf86062004-05-26 10:11:05 +00002259 return TCL_OK;
2260}
2261
2262/*
danielk197717240fd2004-05-26 00:07:25 +00002263** Usage: sqlite3_data_count STMT
2264**
2265** Return the number of columns returned by the sql statement STMT.
2266*/
2267static int test_data_count(
2268 void * clientData,
2269 Tcl_Interp *interp,
2270 int objc,
2271 Tcl_Obj *CONST objv[]
2272){
2273 sqlite3_stmt *pStmt;
2274
2275 if( objc!=2 ){
2276 Tcl_AppendResult(interp, "wrong # args: should be \"",
2277 Tcl_GetString(objv[0]), " STMT column", 0);
2278 return TCL_ERROR;
2279 }
2280
2281 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2282
2283 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_data_count(pStmt)));
2284 return TCL_OK;
2285}
2286
2287/*
danielk197704f2e682004-05-27 01:04:07 +00002288** Usage: sqlite3_column_text STMT column
2289**
2290** Usage: sqlite3_column_decltype STMT column
2291**
2292** Usage: sqlite3_column_name STMT column
2293*/
2294static int test_stmt_utf8(
drh241db312004-06-22 12:46:53 +00002295 void * clientData, /* Pointer to SQLite API function to be invoke */
danielk197704f2e682004-05-27 01:04:07 +00002296 Tcl_Interp *interp,
2297 int objc,
2298 Tcl_Obj *CONST objv[]
2299){
2300 sqlite3_stmt *pStmt;
2301 int col;
danielk1977c572ef72004-05-27 09:28:41 +00002302 const char *(*xFunc)(sqlite3_stmt*, int) = clientData;
danielk1977f93bbbe2004-05-27 10:30:52 +00002303 const char *zRet;
danielk197704f2e682004-05-27 01:04:07 +00002304
2305 if( objc!=3 ){
2306 Tcl_AppendResult(interp, "wrong # args: should be \"",
2307 Tcl_GetString(objv[0]), " STMT column", 0);
2308 return TCL_ERROR;
2309 }
2310
2311 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2312 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
danielk1977f93bbbe2004-05-27 10:30:52 +00002313 zRet = xFunc(pStmt, col);
2314 if( zRet ){
2315 Tcl_SetResult(interp, (char *)zRet, 0);
2316 }
danielk197704f2e682004-05-27 01:04:07 +00002317 return TCL_OK;
2318}
2319
danielk19776b456a22005-03-21 04:04:02 +00002320static int test_global_recover(
2321 void * clientData,
2322 Tcl_Interp *interp,
2323 int objc,
2324 Tcl_Obj *CONST objv[]
2325){
2326#ifndef SQLITE_OMIT_GLOBALRECOVER
2327 int rc;
2328 if( objc!=1 ){
2329 Tcl_WrongNumArgs(interp, 1, objv, "");
2330 return TCL_ERROR;
2331 }
2332 rc = sqlite3_global_recover();
2333 Tcl_SetResult(interp, (char *)errorName(rc), TCL_STATIC);
2334#endif
2335 return TCL_OK;
2336}
2337
danielk197704f2e682004-05-27 01:04:07 +00002338/*
2339** Usage: sqlite3_column_text STMT column
2340**
2341** Usage: sqlite3_column_decltype STMT column
2342**
2343** Usage: sqlite3_column_name STMT column
2344*/
2345static int test_stmt_utf16(
drh241db312004-06-22 12:46:53 +00002346 void * clientData, /* Pointer to SQLite API function to be invoked */
danielk197704f2e682004-05-27 01:04:07 +00002347 Tcl_Interp *interp,
2348 int objc,
2349 Tcl_Obj *CONST objv[]
2350){
drh5436dc22004-11-14 04:04:17 +00002351#ifndef SQLITE_OMIT_UTF16
danielk197704f2e682004-05-27 01:04:07 +00002352 sqlite3_stmt *pStmt;
2353 int col;
2354 Tcl_Obj *pRet;
2355 const void *zName16;
danielk1977c572ef72004-05-27 09:28:41 +00002356 const void *(*xFunc)(sqlite3_stmt*, int) = clientData;
danielk197704f2e682004-05-27 01:04:07 +00002357
2358 if( objc!=3 ){
2359 Tcl_AppendResult(interp, "wrong # args: should be \"",
2360 Tcl_GetString(objv[0]), " STMT column", 0);
2361 return TCL_ERROR;
2362 }
2363
2364 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2365 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
2366
2367 zName16 = xFunc(pStmt, col);
danielk1977f93bbbe2004-05-27 10:30:52 +00002368 if( zName16 ){
2369 pRet = Tcl_NewByteArrayObj(zName16, sqlite3utf16ByteLen(zName16, -1)+2);
2370 Tcl_SetObjResult(interp, pRet);
2371 }
drh5436dc22004-11-14 04:04:17 +00002372#endif /* SQLITE_OMIT_UTF16 */
danielk197704f2e682004-05-27 01:04:07 +00002373
2374 return TCL_OK;
2375}
2376
2377/*
2378** Usage: sqlite3_column_int STMT column
2379**
2380** Usage: sqlite3_column_bytes STMT column
2381**
2382** Usage: sqlite3_column_bytes16 STMT column
2383**
2384*/
2385static int test_stmt_int(
drh241db312004-06-22 12:46:53 +00002386 void * clientData, /* Pointer to SQLite API function to be invoked */
danielk197704f2e682004-05-27 01:04:07 +00002387 Tcl_Interp *interp,
2388 int objc,
2389 Tcl_Obj *CONST objv[]
2390){
2391 sqlite3_stmt *pStmt;
2392 int col;
danielk1977c572ef72004-05-27 09:28:41 +00002393 int (*xFunc)(sqlite3_stmt*, int) = clientData;
danielk197704f2e682004-05-27 01:04:07 +00002394
2395 if( objc!=3 ){
2396 Tcl_AppendResult(interp, "wrong # args: should be \"",
2397 Tcl_GetString(objv[0]), " STMT column", 0);
2398 return TCL_ERROR;
2399 }
2400
2401 if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
2402 if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
2403
2404 Tcl_SetObjResult(interp, Tcl_NewIntObj(xFunc(pStmt, col)));
2405 return TCL_OK;
2406}
2407
danielk197744ee5bf2005-05-27 09:41:12 +00002408#ifndef SQLITE_OMIT_DISKIO
danielk19779a1d0ab2004-06-01 14:09:28 +00002409/*
2410** Usage: sqlite3OsOpenReadWrite <filename>
2411*/
2412static int test_sqlite3OsOpenReadWrite(
2413 void * clientData,
2414 Tcl_Interp *interp,
2415 int objc,
2416 Tcl_Obj *CONST objv[]
2417){
2418 OsFile * pFile;
2419 int rc;
2420 int dummy;
2421 char zBuf[100];
danielk197704f2e682004-05-27 01:04:07 +00002422
danielk19779a1d0ab2004-06-01 14:09:28 +00002423 if( objc!=2 ){
2424 Tcl_AppendResult(interp, "wrong # args: should be \"",
2425 Tcl_GetString(objv[0]), " filename", 0);
2426 return TCL_ERROR;
2427 }
2428
2429 pFile = sqliteMalloc(sizeof(OsFile));
2430 rc = sqlite3OsOpenReadWrite(Tcl_GetString(objv[1]), pFile, &dummy);
2431 if( rc!=SQLITE_OK ){
2432 sqliteFree(pFile);
2433 Tcl_SetResult(interp, (char *)errorName(rc), TCL_STATIC);
2434 return TCL_ERROR;
2435 }
2436 makePointerStr(interp, zBuf, pFile);
2437 Tcl_SetResult(interp, zBuf, 0);
2438 return TCL_ERROR;
2439}
2440
2441/*
2442** Usage: sqlite3OsClose <file handle>
2443*/
2444static int test_sqlite3OsClose(
2445 void * clientData,
2446 Tcl_Interp *interp,
2447 int objc,
2448 Tcl_Obj *CONST objv[]
2449){
2450 OsFile * pFile;
2451 int rc;
2452
2453 if( objc!=2 ){
2454 Tcl_AppendResult(interp, "wrong # args: should be \"",
2455 Tcl_GetString(objv[0]), " filehandle", 0);
2456 return TCL_ERROR;
2457 }
2458
2459 if( getFilePointer(interp, Tcl_GetString(objv[1]), &pFile) ){
2460 return TCL_ERROR;
2461 }
2462 rc = sqlite3OsClose(pFile);
2463 if( rc!=SQLITE_OK ){
2464 Tcl_SetResult(interp, (char *)errorName(rc), TCL_STATIC);
2465 return TCL_ERROR;
2466 }
2467 sqliteFree(pFile);
2468 return TCL_OK;
2469}
2470
2471/*
2472** Usage: sqlite3OsLock <file handle> <locktype>
2473*/
2474static int test_sqlite3OsLock(
2475 void * clientData,
2476 Tcl_Interp *interp,
2477 int objc,
2478 Tcl_Obj *CONST objv[]
2479){
2480 OsFile * pFile;
2481 int rc;
2482
2483 if( objc!=3 ){
2484 Tcl_AppendResult(interp, "wrong # args: should be \"",
2485 Tcl_GetString(objv[0]),
2486 " filehandle (SHARED|RESERVED|PENDING|EXCLUSIVE)", 0);
2487 return TCL_ERROR;
2488 }
2489
2490 if( getFilePointer(interp, Tcl_GetString(objv[1]), &pFile) ){
2491 return TCL_ERROR;
2492 }
2493
2494 if( 0==strcmp("SHARED", Tcl_GetString(objv[2])) ){
2495 rc = sqlite3OsLock(pFile, SHARED_LOCK);
2496 }
2497 else if( 0==strcmp("RESERVED", Tcl_GetString(objv[2])) ){
2498 rc = sqlite3OsLock(pFile, RESERVED_LOCK);
2499 }
2500 else if( 0==strcmp("PENDING", Tcl_GetString(objv[2])) ){
2501 rc = sqlite3OsLock(pFile, PENDING_LOCK);
2502 }
2503 else if( 0==strcmp("EXCLUSIVE", Tcl_GetString(objv[2])) ){
2504 rc = sqlite3OsLock(pFile, EXCLUSIVE_LOCK);
2505 }else{
2506 Tcl_AppendResult(interp, "wrong # args: should be \"",
2507 Tcl_GetString(objv[0]),
2508 " filehandle (SHARED|RESERVED|PENDING|EXCLUSIVE)", 0);
2509 return TCL_ERROR;
2510 }
2511
2512 if( rc!=SQLITE_OK ){
2513 Tcl_SetResult(interp, (char *)errorName(rc), TCL_STATIC);
2514 return TCL_ERROR;
2515 }
2516 return TCL_OK;
2517}
2518
2519/*
2520** Usage: sqlite3OsUnlock <file handle>
2521*/
2522static int test_sqlite3OsUnlock(
2523 void * clientData,
2524 Tcl_Interp *interp,
2525 int objc,
2526 Tcl_Obj *CONST objv[]
2527){
2528 OsFile * pFile;
2529 int rc;
2530
2531 if( objc!=2 ){
2532 Tcl_AppendResult(interp, "wrong # args: should be \"",
2533 Tcl_GetString(objv[0]), " filehandle", 0);
2534 return TCL_ERROR;
2535 }
2536
2537 if( getFilePointer(interp, Tcl_GetString(objv[1]), &pFile) ){
2538 return TCL_ERROR;
2539 }
drha6abd042004-06-09 17:37:22 +00002540 rc = sqlite3OsUnlock(pFile, NO_LOCK);
danielk19779a1d0ab2004-06-01 14:09:28 +00002541 if( rc!=SQLITE_OK ){
2542 Tcl_SetResult(interp, (char *)errorName(rc), TCL_STATIC);
2543 return TCL_ERROR;
2544 }
2545 return TCL_OK;
2546}
drhd3d39e92004-05-20 22:16:29 +00002547
drhab3f9fe2004-08-14 17:10:10 +00002548/*
2549** Usage: sqlite3OsTempFileName
2550*/
2551static int test_sqlite3OsTempFileName(
2552 void * clientData,
2553 Tcl_Interp *interp,
2554 int objc,
2555 Tcl_Obj *CONST objv[]
2556){
2557 char zFile[SQLITE_TEMPNAME_SIZE];
2558 int rc;
2559
2560 rc = sqlite3OsTempFileName(zFile);
2561 if( rc!=SQLITE_OK ){
2562 Tcl_SetResult(interp, (char *)errorName(rc), TCL_STATIC);
2563 return TCL_ERROR;
2564 }
2565 Tcl_AppendResult(interp, zFile, 0);
2566 return TCL_OK;
2567}
danielk197744ee5bf2005-05-27 09:41:12 +00002568#endif
danielk1977d8123362004-06-12 09:25:12 +00002569
danielk19776622cce2004-05-20 11:00:52 +00002570/*
drhcacb2082005-01-11 15:28:33 +00002571** Usage: sqlite_set_magic DB MAGIC-NUMBER
2572**
2573** Set the db->magic value. This is used to test error recovery logic.
2574*/
2575static int sqlite_set_magic(
2576 void * clientData,
2577 Tcl_Interp *interp,
2578 int argc,
2579 char **argv
2580){
2581 sqlite3 *db;
2582 if( argc!=3 ){
2583 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
2584 " DB MAGIC", 0);
2585 return TCL_ERROR;
2586 }
2587 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
2588 if( strcmp(argv[2], "SQLITE_MAGIC_OPEN")==0 ){
2589 db->magic = SQLITE_MAGIC_OPEN;
2590 }else if( strcmp(argv[2], "SQLITE_MAGIC_CLOSED")==0 ){
2591 db->magic = SQLITE_MAGIC_CLOSED;
2592 }else if( strcmp(argv[2], "SQLITE_MAGIC_BUSY")==0 ){
2593 db->magic = SQLITE_MAGIC_BUSY;
2594 }else if( strcmp(argv[2], "SQLITE_MAGIC_ERROR")==0 ){
2595 db->magic = SQLITE_MAGIC_ERROR;
2596 }else if( Tcl_GetInt(interp, argv[2], &db->magic) ){
2597 return TCL_ERROR;
2598 }
2599 return TCL_OK;
2600}
2601
2602/*
drhc5cdca62005-01-11 16:54:14 +00002603** Usage: sqlite3_interrupt DB
2604**
2605** Trigger an interrupt on DB
2606*/
2607static int test_interrupt(
2608 void * clientData,
2609 Tcl_Interp *interp,
2610 int argc,
2611 char **argv
2612){
2613 sqlite3 *db;
2614 if( argc!=2 ){
2615 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " DB", 0);
2616 return TCL_ERROR;
2617 }
2618 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
2619 sqlite3_interrupt(db);
2620 return TCL_OK;
2621}
2622
2623/*
danielk1977600dd0b2005-01-20 01:14:23 +00002624** Usage: sqlite3_sleep ms
2625**
2626** Sleep for the specified number of ms.
2627*/
2628#if 0
2629static int test_sleep(
2630 void * clientData,
2631 Tcl_Interp *interp,
2632 int argc,
2633 char **argv
2634){
2635 sqlite3 *db;
2636 if( argc!=2 ){
2637 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " ms", 0);
2638 return TCL_ERROR;
2639 }
2640 Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_sleep(atoi(argv[1]))));
2641 return TCL_OK;
2642}
2643#endif
2644
2645/*
danielk19779636c4e2005-01-25 04:27:54 +00002646** Usage: sqlite_delete_function DB function-name
2647**
2648** Delete the user function 'function-name' from database handle DB. It
2649** is assumed that the user function was created as UTF8, any number of
2650** arguments (the way the TCL interface does it).
2651*/
2652static int delete_function(
2653 void * clientData,
2654 Tcl_Interp *interp,
2655 int argc,
2656 char **argv
2657){
2658 int rc;
2659 sqlite3 *db;
2660 if( argc!=3 ){
2661 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
2662 " DB function-name", 0);
2663 return TCL_ERROR;
2664 }
2665 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
2666 rc = sqlite3_create_function(db, argv[2], -1, SQLITE_UTF8, 0, 0, 0, 0);
2667 Tcl_SetResult(interp, (char *)errorName(rc), TCL_STATIC);
2668 return TCL_OK;
2669}
2670
2671/*
2672** Usage: sqlite_delete_collation DB collation-name
2673**
2674** Delete the collation sequence 'collation-name' from database handle
2675** DB. It is assumed that the collation sequence was created as UTF8 (the
2676** way the TCL interface does it).
2677*/
2678static int delete_collation(
2679 void * clientData,
2680 Tcl_Interp *interp,
2681 int argc,
2682 char **argv
2683){
2684 int rc;
2685 sqlite3 *db;
2686 if( argc!=3 ){
2687 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
2688 " DB function-name", 0);
2689 return TCL_ERROR;
2690 }
2691 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
2692 rc = sqlite3_create_collation(db, argv[2], SQLITE_UTF8, 0, 0);
2693 Tcl_SetResult(interp, (char *)errorName(rc), TCL_STATIC);
2694 return TCL_OK;
2695}
2696
2697/*
drh3e1d8e62005-05-26 16:23:34 +00002698** Usage: sqlite3_get_autocommit DB
2699**
2700** Return true if the database DB is currently in auto-commit mode.
2701** Return false if not.
2702*/
2703static int get_autocommit(
2704 void * clientData,
2705 Tcl_Interp *interp,
2706 int argc,
2707 char **argv
2708){
2709 char zBuf[30];
2710 sqlite3 *db;
2711 if( argc!=2 ){
2712 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
2713 " DB", 0);
2714 return TCL_ERROR;
2715 }
2716 if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
2717 sprintf(zBuf, "%d", sqlite3_get_autocommit(db));
2718 Tcl_AppendResult(interp, zBuf, 0);
2719 return TCL_OK;
2720}
2721
2722/*
drh92febd92004-08-20 18:34:20 +00002723** Usage: tcl_variable_type VARIABLENAME
2724**
2725** Return the name of the internal representation for the
2726** value of the given variable.
2727*/
2728static int tcl_variable_type(
2729 void * clientData,
2730 Tcl_Interp *interp,
2731 int objc,
2732 Tcl_Obj *CONST objv[]
2733){
2734 Tcl_Obj *pVar;
2735 if( objc!=2 ){
2736 Tcl_WrongNumArgs(interp, 1, objv, "VARIABLE");
2737 return TCL_ERROR;
2738 }
2739 pVar = Tcl_GetVar2Ex(interp, Tcl_GetString(objv[1]), 0, TCL_LEAVE_ERR_MSG);
2740 if( pVar==0 ) return TCL_ERROR;
2741 if( pVar->typePtr ){
2742 Tcl_SetObjResult(interp, Tcl_NewStringObj(pVar->typePtr->name, -1));
2743 }
2744 return TCL_OK;
2745}
2746
2747/*
drh27d258a2004-10-30 20:23:09 +00002748** This routine sets entries in the global ::sqlite_options() array variable
2749** according to the compile-time configuration of the database. Test
2750** procedures use this to determine when tests should be omitted.
2751*/
2752static void set_options(Tcl_Interp *interp){
drh75f86a42005-02-17 00:03:06 +00002753#ifdef SQLITE_32BIT_ROWID
2754 Tcl_SetVar2(interp, "sqlite_options", "rowid32", "1", TCL_GLOBAL_ONLY);
2755#else
2756 Tcl_SetVar2(interp, "sqlite_options", "rowid32", "0", TCL_GLOBAL_ONLY);
2757#endif
2758
drh9f18e8a2005-07-08 12:13:04 +00002759#ifdef SQLITE_CASE_SENSITIVE_LIKE
2760 Tcl_SetVar2(interp, "sqlite_options","casesensitivelike","1",TCL_GLOBAL_ONLY);
2761#else
2762 Tcl_SetVar2(interp, "sqlite_options","casesensitivelike","0",TCL_GLOBAL_ONLY);
2763#endif
2764
danielk19771c8c23c2004-11-12 15:53:37 +00002765#ifdef SQLITE_OMIT_ALTERTABLE
2766 Tcl_SetVar2(interp, "sqlite_options", "altertable", "0", TCL_GLOBAL_ONLY);
2767#else
2768 Tcl_SetVar2(interp, "sqlite_options", "altertable", "1", TCL_GLOBAL_ONLY);
2769#endif
drh13d70422004-11-13 15:59:14 +00002770
drh9f18e8a2005-07-08 12:13:04 +00002771#ifdef SQLITE_OMIT_ANALYZE
2772 Tcl_SetVar2(interp, "sqlite_options", "analyze", "0", TCL_GLOBAL_ONLY);
2773#else
2774 Tcl_SetVar2(interp, "sqlite_options", "analyze", "1", TCL_GLOBAL_ONLY);
2775#endif
2776
drh13d70422004-11-13 15:59:14 +00002777#ifdef SQLITE_OMIT_AUTHORIZATION
2778 Tcl_SetVar2(interp, "sqlite_options", "auth", "0", TCL_GLOBAL_ONLY);
2779#else
2780 Tcl_SetVar2(interp, "sqlite_options", "auth", "1", TCL_GLOBAL_ONLY);
2781#endif
2782
drhf3388142004-11-13 03:48:06 +00002783#ifdef SQLITE_OMIT_AUTOINCREMENT
2784 Tcl_SetVar2(interp, "sqlite_options", "autoinc", "0", TCL_GLOBAL_ONLY);
2785#else
2786 Tcl_SetVar2(interp, "sqlite_options", "autoinc", "1", TCL_GLOBAL_ONLY);
2787#endif
2788
danielk197745901d62004-11-10 15:27:38 +00002789#ifdef SQLITE_OMIT_AUTOVACUUM
2790 Tcl_SetVar2(interp, "sqlite_options", "autovacuum", "0", TCL_GLOBAL_ONLY);
2791#else
2792 Tcl_SetVar2(interp, "sqlite_options", "autovacuum", "1", TCL_GLOBAL_ONLY);
drhf3388142004-11-13 03:48:06 +00002793#endif /* SQLITE_OMIT_AUTOVACUUM */
danielk19774d36b812004-11-19 07:07:30 +00002794#if !defined(SQLITE_DEFAULT_AUTOVACUUM) || SQLITE_DEFAULT_AUTOVACUUM==0
danielk197745901d62004-11-10 15:27:38 +00002795 Tcl_SetVar2(interp,"sqlite_options","default_autovacuum","0",TCL_GLOBAL_ONLY);
2796#else
2797 Tcl_SetVar2(interp,"sqlite_options","default_autovacuum","1",TCL_GLOBAL_ONLY);
2798#endif
drh13d70422004-11-13 15:59:14 +00002799
drh55ef4d92005-08-14 01:20:37 +00002800#ifdef SQLITE_OMIT_BETWEEN_OPTIMIZATION
2801 Tcl_SetVar2(interp, "sqlite_options", "between_opt", "0", TCL_GLOBAL_ONLY);
2802#else
2803 Tcl_SetVar2(interp, "sqlite_options", "between_opt", "1", TCL_GLOBAL_ONLY);
2804#endif
2805
drh13d70422004-11-13 15:59:14 +00002806#ifdef SQLITE_OMIT_BLOB_LITERAL
2807 Tcl_SetVar2(interp, "sqlite_options", "bloblit", "0", TCL_GLOBAL_ONLY);
2808#else
2809 Tcl_SetVar2(interp, "sqlite_options", "bloblit", "1", TCL_GLOBAL_ONLY);
2810#endif
2811
drh487e2622005-06-25 18:42:14 +00002812#ifdef SQLITE_OMIT_CAST
2813 Tcl_SetVar2(interp, "sqlite_options", "cast", "0", TCL_GLOBAL_ONLY);
2814#else
2815 Tcl_SetVar2(interp, "sqlite_options", "cast", "1", TCL_GLOBAL_ONLY);
2816#endif
2817
drhccae6022005-02-26 17:31:26 +00002818#ifdef SQLITE_OMIT_COMPLETE
2819 Tcl_SetVar2(interp, "sqlite_options", "complete", "0", TCL_GLOBAL_ONLY);
2820#else
2821 Tcl_SetVar2(interp, "sqlite_options", "complete", "1", TCL_GLOBAL_ONLY);
2822#endif
2823
drh13d70422004-11-13 15:59:14 +00002824#ifdef SQLITE_OMIT_COMPOUND_SELECT
2825 Tcl_SetVar2(interp, "sqlite_options", "compound", "0", TCL_GLOBAL_ONLY);
2826#else
2827 Tcl_SetVar2(interp, "sqlite_options", "compound", "1", TCL_GLOBAL_ONLY);
2828#endif
2829
2830#ifdef SQLITE_OMIT_CONFLICT_CLAUSE
2831 Tcl_SetVar2(interp, "sqlite_options", "conflict", "0", TCL_GLOBAL_ONLY);
2832#else
2833 Tcl_SetVar2(interp, "sqlite_options", "conflict", "1", TCL_GLOBAL_ONLY);
2834#endif
2835
2836#ifdef SQLITE_OMIT_DATETIME_FUNCS
2837 Tcl_SetVar2(interp, "sqlite_options", "datetime", "0", TCL_GLOBAL_ONLY);
2838#else
2839 Tcl_SetVar2(interp, "sqlite_options", "datetime", "1", TCL_GLOBAL_ONLY);
2840#endif
2841
drh2e66f0b2005-04-28 17:18:48 +00002842#ifdef SQLITE_OMIT_DISKIO
2843 Tcl_SetVar2(interp, "sqlite_options", "diskio", "0", TCL_GLOBAL_ONLY);
2844#else
2845 Tcl_SetVar2(interp, "sqlite_options", "diskio", "1", TCL_GLOBAL_ONLY);
2846#endif
2847
drh13d70422004-11-13 15:59:14 +00002848#ifdef SQLITE_OMIT_EXPLAIN
2849 Tcl_SetVar2(interp, "sqlite_options", "explain", "0", TCL_GLOBAL_ONLY);
2850#else
2851 Tcl_SetVar2(interp, "sqlite_options", "explain", "1", TCL_GLOBAL_ONLY);
2852#endif
2853
2854#ifdef SQLITE_OMIT_FLOATING_POINT
2855 Tcl_SetVar2(interp, "sqlite_options", "floatingpoint", "0", TCL_GLOBAL_ONLY);
2856#else
2857 Tcl_SetVar2(interp, "sqlite_options", "floatingpoint", "1", TCL_GLOBAL_ONLY);
2858#endif
2859
2860#ifdef SQLITE_OMIT_FOREIGN_KEY
2861 Tcl_SetVar2(interp, "sqlite_options", "foreignkey", "0", TCL_GLOBAL_ONLY);
2862#else
2863 Tcl_SetVar2(interp, "sqlite_options", "foreignkey", "1", TCL_GLOBAL_ONLY);
2864#endif
2865
danielk19776b456a22005-03-21 04:04:02 +00002866#ifdef SQLITE_OMIT_GLOBALRECOVER
2867 Tcl_SetVar2(interp, "sqlite_options", "globalrecover", "0", TCL_GLOBAL_ONLY);
2868#else
2869 Tcl_SetVar2(interp, "sqlite_options", "globalrecover", "1", TCL_GLOBAL_ONLY);
2870#endif
2871
drh13d70422004-11-13 15:59:14 +00002872#ifdef SQLITE_OMIT_INTEGRITY_CHECK
2873 Tcl_SetVar2(interp, "sqlite_options", "integrityck", "0", TCL_GLOBAL_ONLY);
2874#else
2875 Tcl_SetVar2(interp, "sqlite_options", "integrityck", "1", TCL_GLOBAL_ONLY);
2876#endif
2877
drh55ef4d92005-08-14 01:20:37 +00002878#ifdef SQLITE_OMIT_LIKE_OPTIMIZATION
2879 Tcl_SetVar2(interp, "sqlite_options", "like_opt", "0", TCL_GLOBAL_ONLY);
2880#else
2881 Tcl_SetVar2(interp, "sqlite_options", "like_opt", "1", TCL_GLOBAL_ONLY);
2882#endif
2883
drh13d70422004-11-13 15:59:14 +00002884#ifdef SQLITE_OMIT_MEMORYDB
2885 Tcl_SetVar2(interp, "sqlite_options", "memorydb", "0", TCL_GLOBAL_ONLY);
2886#else
2887 Tcl_SetVar2(interp, "sqlite_options", "memorydb", "1", TCL_GLOBAL_ONLY);
2888#endif
2889
drh55ef4d92005-08-14 01:20:37 +00002890#ifdef SQLITE_OMIT_OR_OPTIMIZATION
2891 Tcl_SetVar2(interp, "sqlite_options", "or_opt", "0", TCL_GLOBAL_ONLY);
2892#else
2893 Tcl_SetVar2(interp, "sqlite_options", "or_opt", "1", TCL_GLOBAL_ONLY);
2894#endif
2895
drh13d70422004-11-13 15:59:14 +00002896#ifdef SQLITE_OMIT_PAGER_PRAGMAS
2897 Tcl_SetVar2(interp, "sqlite_options", "pager_pragmas", "0", TCL_GLOBAL_ONLY);
2898#else
2899 Tcl_SetVar2(interp, "sqlite_options", "pager_pragmas", "1", TCL_GLOBAL_ONLY);
2900#endif
2901
drh2e66f0b2005-04-28 17:18:48 +00002902#ifdef SQLITE_OMIT_PARSER
2903 Tcl_SetVar2(interp, "sqlite_options", "parser", "0", TCL_GLOBAL_ONLY);
2904#else
2905 Tcl_SetVar2(interp, "sqlite_options", "parser", "1", TCL_GLOBAL_ONLY);
2906#endif
2907
drhbf216272005-02-26 18:10:44 +00002908#if defined(SQLITE_OMIT_PRAGMA) || defined(SQLITE_OMIT_FLAG_PRAGMAS)
drh13d70422004-11-13 15:59:14 +00002909 Tcl_SetVar2(interp, "sqlite_options", "pragma", "0", TCL_GLOBAL_ONLY);
2910 Tcl_SetVar2(interp, "sqlite_options", "integrityck", "0", TCL_GLOBAL_ONLY);
2911#else
2912 Tcl_SetVar2(interp, "sqlite_options", "pragma", "1", TCL_GLOBAL_ONLY);
2913#endif
2914
2915#ifdef SQLITE_OMIT_PROGRESS_CALLBACK
2916 Tcl_SetVar2(interp, "sqlite_options", "progress", "0", TCL_GLOBAL_ONLY);
2917#else
2918 Tcl_SetVar2(interp, "sqlite_options", "progress", "1", TCL_GLOBAL_ONLY);
2919#endif
2920
2921#ifdef SQLITE_OMIT_REINDEX
2922 Tcl_SetVar2(interp, "sqlite_options", "reindex", "0", TCL_GLOBAL_ONLY);
2923#else
2924 Tcl_SetVar2(interp, "sqlite_options", "reindex", "1", TCL_GLOBAL_ONLY);
2925#endif
2926
2927#ifdef SQLITE_OMIT_SCHEMA_PRAGMAS
2928 Tcl_SetVar2(interp, "sqlite_options", "schema_pragmas", "0", TCL_GLOBAL_ONLY);
2929#else
2930 Tcl_SetVar2(interp, "sqlite_options", "schema_pragmas", "1", TCL_GLOBAL_ONLY);
2931#endif
2932
2933#ifdef SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS
2934 Tcl_SetVar2(interp, "sqlite_options", "schema_version", "0", TCL_GLOBAL_ONLY);
2935#else
2936 Tcl_SetVar2(interp, "sqlite_options", "schema_version", "1", TCL_GLOBAL_ONLY);
2937#endif
2938
danielk19773e8c37e2005-01-21 03:12:14 +00002939#ifdef SQLITE_OMIT_SUBQUERY
2940 Tcl_SetVar2(interp, "sqlite_options", "subquery", "0", TCL_GLOBAL_ONLY);
2941#else
2942 Tcl_SetVar2(interp, "sqlite_options", "subquery", "1", TCL_GLOBAL_ONLY);
2943#endif
2944
drh13d70422004-11-13 15:59:14 +00002945#ifdef SQLITE_OMIT_TCL_VARIABLE
2946 Tcl_SetVar2(interp, "sqlite_options", "tclvar", "0", TCL_GLOBAL_ONLY);
2947#else
2948 Tcl_SetVar2(interp, "sqlite_options", "tclvar", "1", TCL_GLOBAL_ONLY);
2949#endif
2950
2951#if defined(THREADSAFE) && THREADSAFE
2952 Tcl_SetVar2(interp, "sqlite_options", "threadsafe", "1", TCL_GLOBAL_ONLY);
2953#else
2954 Tcl_SetVar2(interp, "sqlite_options", "threadsafe", "0", TCL_GLOBAL_ONLY);
2955#endif
2956
2957#ifdef SQLITE_OMIT_TRIGGER
2958 Tcl_SetVar2(interp, "sqlite_options", "trigger", "0", TCL_GLOBAL_ONLY);
2959#else
2960 Tcl_SetVar2(interp, "sqlite_options", "trigger", "1", TCL_GLOBAL_ONLY);
2961#endif
2962
danielk197753c0f742005-03-29 03:10:59 +00002963#ifdef SQLITE_OMIT_TEMPDB
2964 Tcl_SetVar2(interp, "sqlite_options", "tempdb", "0", TCL_GLOBAL_ONLY);
2965#else
2966 Tcl_SetVar2(interp, "sqlite_options", "tempdb", "1", TCL_GLOBAL_ONLY);
2967#endif
2968
drh13d70422004-11-13 15:59:14 +00002969#ifdef SQLITE_OMIT_UTF16
2970 Tcl_SetVar2(interp, "sqlite_options", "utf16", "0", TCL_GLOBAL_ONLY);
2971#else
2972 Tcl_SetVar2(interp, "sqlite_options", "utf16", "1", TCL_GLOBAL_ONLY);
2973#endif
2974
2975#ifdef SQLITE_OMIT_VACUUM
2976 Tcl_SetVar2(interp, "sqlite_options", "vacuum", "0", TCL_GLOBAL_ONLY);
2977#else
2978 Tcl_SetVar2(interp, "sqlite_options", "vacuum", "1", TCL_GLOBAL_ONLY);
2979#endif
2980
2981#ifdef SQLITE_OMIT_VIEW
2982 Tcl_SetVar2(interp, "sqlite_options", "view", "0", TCL_GLOBAL_ONLY);
2983#else
2984 Tcl_SetVar2(interp, "sqlite_options", "view", "1", TCL_GLOBAL_ONLY);
2985#endif
drh27d258a2004-10-30 20:23:09 +00002986}
2987
2988/*
drhd1bf3512001-04-07 15:24:33 +00002989** Register commands with the TCL interpreter.
2990*/
2991int Sqlitetest1_Init(Tcl_Interp *interp){
danielk19776f8a5032004-05-10 10:34:51 +00002992 extern int sqlite3_search_count;
2993 extern int sqlite3_interrupt_count;
2994 extern int sqlite3_open_file_count;
drh6bf89572004-11-03 16:27:01 +00002995 extern int sqlite3_sort_count;
danielk19776f8a5032004-05-10 10:34:51 +00002996 extern int sqlite3_current_time;
drhc2eef3b2002-08-31 18:53:06 +00002997 static struct {
2998 char *zName;
2999 Tcl_CmdProc *xProc;
3000 } aCmd[] = {
drhd3d39e92004-05-20 22:16:29 +00003001 { "sqlite3_mprintf_int", (Tcl_CmdProc*)sqlite3_mprintf_int },
drhe9707672004-06-25 01:10:48 +00003002 { "sqlite3_mprintf_int64", (Tcl_CmdProc*)sqlite3_mprintf_int64 },
drhd3d39e92004-05-20 22:16:29 +00003003 { "sqlite3_mprintf_str", (Tcl_CmdProc*)sqlite3_mprintf_str },
drhe29b1a02004-07-17 21:56:09 +00003004 { "sqlite3_mprintf_stronly", (Tcl_CmdProc*)sqlite3_mprintf_stronly},
drhd3d39e92004-05-20 22:16:29 +00003005 { "sqlite3_mprintf_double", (Tcl_CmdProc*)sqlite3_mprintf_double },
3006 { "sqlite3_mprintf_scaled", (Tcl_CmdProc*)sqlite3_mprintf_scaled },
3007 { "sqlite3_mprintf_z_test", (Tcl_CmdProc*)test_mprintf_z },
drhd3d39e92004-05-20 22:16:29 +00003008 { "sqlite3_last_insert_rowid", (Tcl_CmdProc*)test_last_rowid },
3009 { "sqlite3_exec_printf", (Tcl_CmdProc*)test_exec_printf },
3010 { "sqlite3_get_table_printf", (Tcl_CmdProc*)test_get_table_printf },
3011 { "sqlite3_close", (Tcl_CmdProc*)sqlite_test_close },
3012 { "sqlite3_create_function", (Tcl_CmdProc*)test_create_function },
3013 { "sqlite3_create_aggregate", (Tcl_CmdProc*)test_create_aggregate },
3014 { "sqlite_register_test_function", (Tcl_CmdProc*)test_register_func },
3015 { "sqlite_abort", (Tcl_CmdProc*)sqlite_abort },
danielk19772c336542005-01-13 02:14:23 +00003016#ifdef SQLITE_MEMDEBUG
drhd3d39e92004-05-20 22:16:29 +00003017 { "sqlite_malloc_fail", (Tcl_CmdProc*)sqlite_malloc_fail },
3018 { "sqlite_malloc_stat", (Tcl_CmdProc*)sqlite_malloc_stat },
drhc2eef3b2002-08-31 18:53:06 +00003019#endif
drh25d65432004-07-22 15:02:25 +00003020 { "sqlite_bind", (Tcl_CmdProc*)test_bind },
3021 { "breakpoint", (Tcl_CmdProc*)test_breakpoint },
3022 { "sqlite3_key", (Tcl_CmdProc*)test_key },
3023 { "sqlite3_rekey", (Tcl_CmdProc*)test_rekey },
drhcacb2082005-01-11 15:28:33 +00003024 { "sqlite_set_magic", (Tcl_CmdProc*)sqlite_set_magic },
drhc5cdca62005-01-11 16:54:14 +00003025 { "sqlite3_interrupt", (Tcl_CmdProc*)test_interrupt },
danielk1977600dd0b2005-01-20 01:14:23 +00003026#if 0
3027 { "sqlite3_sleep", (Tcl_CmdProc*)test_sleep },
3028#endif
drh3e1d8e62005-05-26 16:23:34 +00003029 { "sqlite_delete_function", (Tcl_CmdProc*)delete_function },
3030 { "sqlite_delete_collation", (Tcl_CmdProc*)delete_collation },
3031 { "sqlite3_get_autocommit", (Tcl_CmdProc*)get_autocommit },
drhc2eef3b2002-08-31 18:53:06 +00003032 };
danielk197751e3d8e2004-05-20 01:12:34 +00003033 static struct {
3034 char *zName;
3035 Tcl_ObjCmdProc *xProc;
danielk197704f2e682004-05-27 01:04:07 +00003036 void *clientData;
danielk197751e3d8e2004-05-20 01:12:34 +00003037 } aObjCmd[] = {
drh241db312004-06-22 12:46:53 +00003038 { "sqlite3_bind_int", test_bind_int, 0 },
3039 { "sqlite3_bind_int64", test_bind_int64, 0 },
3040 { "sqlite3_bind_double", test_bind_double, 0 },
danielk197704f2e682004-05-27 01:04:07 +00003041 { "sqlite3_bind_null", test_bind_null ,0 },
3042 { "sqlite3_bind_text", test_bind_text ,0 },
3043 { "sqlite3_bind_text16", test_bind_text16 ,0 },
3044 { "sqlite3_bind_blob", test_bind_blob ,0 },
drh75f6a032004-07-15 14:15:00 +00003045 { "sqlite3_bind_parameter_count", test_bind_parameter_count, 0},
drh895d7472004-08-20 16:02:39 +00003046 { "sqlite3_bind_parameter_name", test_bind_parameter_name, 0},
drhfa6bc002004-09-07 16:19:52 +00003047 { "sqlite3_bind_parameter_index", test_bind_parameter_index, 0},
danielk1977600dd0b2005-01-20 01:14:23 +00003048#if 0
3049 { "sqlite3_clear_bindings", test_clear_bindings, 0},
3050#endif
danielk197704f2e682004-05-27 01:04:07 +00003051 { "sqlite3_errcode", test_errcode ,0 },
3052 { "sqlite3_errmsg", test_errmsg ,0 },
3053 { "sqlite3_errmsg16", test_errmsg16 ,0 },
3054 { "sqlite3_open", test_open ,0 },
3055 { "sqlite3_open16", test_open16 ,0 },
danielk1977bc6ada42004-06-30 08:20:16 +00003056 { "sqlite3_complete16", test_complete16 ,0 },
danielk197704f2e682004-05-27 01:04:07 +00003057
3058 { "sqlite3_prepare", test_prepare ,0 },
3059 { "sqlite3_prepare16", test_prepare16 ,0 },
3060 { "sqlite3_finalize", test_finalize ,0 },
3061 { "sqlite3_reset", test_reset ,0 },
drhd89bd002005-01-22 03:03:54 +00003062 { "sqlite3_expired", test_expired ,0 },
drhf8db1bc2005-04-22 02:38:37 +00003063 { "sqlite3_transfer_bindings", test_transfer_bind ,0 },
danielk1977fbcd5852004-06-15 02:44:18 +00003064 { "sqlite3_changes", test_changes ,0 },
3065 { "sqlite3_step", test_step ,0 },
danielk197704f2e682004-05-27 01:04:07 +00003066
3067 /* sqlite3_column_*() API */
3068 { "sqlite3_column_count", test_column_count ,0 },
3069 { "sqlite3_data_count", test_data_count ,0 },
3070 { "sqlite3_column_type", test_column_type ,0 },
danielk1977ea61b2c2004-05-27 01:49:51 +00003071 { "sqlite3_column_blob", test_column_blob ,0 },
danielk197704f2e682004-05-27 01:04:07 +00003072 { "sqlite3_column_double", test_column_double ,0 },
3073 { "sqlite3_column_int64", test_column_int64 ,0 },
drh241db312004-06-22 12:46:53 +00003074 { "sqlite3_column_text", test_stmt_utf8, sqlite3_column_text },
3075 { "sqlite3_column_decltype", test_stmt_utf8, sqlite3_column_decltype },
3076 { "sqlite3_column_name", test_stmt_utf8, sqlite3_column_name },
drh6c626082004-11-14 21:56:29 +00003077 { "sqlite3_column_int", test_stmt_int, sqlite3_column_int },
3078 { "sqlite3_column_bytes", test_stmt_int, sqlite3_column_bytes },
3079#ifndef SQLITE_OMIT_UTF16
3080 { "sqlite3_column_bytes16", test_stmt_int, sqlite3_column_bytes16 },
drh241db312004-06-22 12:46:53 +00003081 { "sqlite3_column_text16", test_stmt_utf16, sqlite3_column_text16 },
3082 { "sqlite3_column_decltype16", test_stmt_utf16, sqlite3_column_decltype16},
3083 { "sqlite3_column_name16", test_stmt_utf16, sqlite3_column_name16 },
drh6c626082004-11-14 21:56:29 +00003084#endif
danielk19776b456a22005-03-21 04:04:02 +00003085 { "sqlite3_global_recover", test_global_recover, 0 },
danielk197704f2e682004-05-27 01:04:07 +00003086
danielk19779a1d0ab2004-06-01 14:09:28 +00003087 /* Functions from os.h */
danielk197744ee5bf2005-05-27 09:41:12 +00003088#ifndef SQLITE_OMIT_DISKIO
danielk19779a1d0ab2004-06-01 14:09:28 +00003089 { "sqlite3OsOpenReadWrite",test_sqlite3OsOpenReadWrite, 0 },
3090 { "sqlite3OsClose", test_sqlite3OsClose, 0 },
3091 { "sqlite3OsLock", test_sqlite3OsLock, 0 },
drhab3f9fe2004-08-14 17:10:10 +00003092 { "sqlite3OsTempFileName", test_sqlite3OsTempFileName, 0 },
danielk1977312d6b32004-06-29 13:18:23 +00003093
3094 /* Custom test interfaces */
3095 { "sqlite3OsUnlock", test_sqlite3OsUnlock, 0 },
danielk197744ee5bf2005-05-27 09:41:12 +00003096#endif
drh5436dc22004-11-14 04:04:17 +00003097#ifndef SQLITE_OMIT_UTF16
danielk1977312d6b32004-06-29 13:18:23 +00003098 { "add_test_collate", test_collate, 0 },
3099 { "add_test_collate_needed", test_collate_needed, 0 },
3100 { "add_test_function", test_function, 0 },
drh5436dc22004-11-14 04:04:17 +00003101#endif
danielk1977312d6b32004-06-29 13:18:23 +00003102 { "sqlite3_crashparams", sqlite3_crashparams, 0 },
3103 { "sqlite3_test_errstr", test_errstr, 0 },
drh92febd92004-08-20 18:34:20 +00003104 { "tcl_variable_type", tcl_variable_type, 0 },
danielk197751e3d8e2004-05-20 01:12:34 +00003105 };
drh1398ad32005-01-19 23:24:50 +00003106 static int bitmask_size = sizeof(Bitmask)*8;
drhc2eef3b2002-08-31 18:53:06 +00003107 int i;
drh2ac3ee92004-06-07 16:27:46 +00003108 extern int sqlite3_os_trace;
drh51147ba2005-07-23 22:59:55 +00003109 extern int sqlite3_where_trace;
drhb851b2c2005-03-10 14:11:12 +00003110 extern int sqlite3_sync_count, sqlite3_fullsync_count;
drhaf6df112005-06-07 02:12:30 +00003111 extern int sqlite3_opentemp_count;
drh01397202005-07-20 14:31:53 +00003112 extern int sqlite3_memUsed;
3113 extern int sqlite3_memMax;
drh84bfda42005-07-15 13:05:21 +00003114 extern char sqlite3_query_plan[];
drh55ef4d92005-08-14 01:20:37 +00003115 extern int sqlite3_like_count;
drh9042f392005-07-15 23:24:23 +00003116 static char *query_plan = sqlite3_query_plan;
drhc2eef3b2002-08-31 18:53:06 +00003117
3118 for(i=0; i<sizeof(aCmd)/sizeof(aCmd[0]); i++){
3119 Tcl_CreateCommand(interp, aCmd[i].zName, aCmd[i].xProc, 0, 0);
3120 }
danielk197751e3d8e2004-05-20 01:12:34 +00003121 for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){
danielk1977c572ef72004-05-27 09:28:41 +00003122 Tcl_CreateObjCommand(interp, aObjCmd[i].zName,
3123 aObjCmd[i].xProc, aObjCmd[i].clientData, 0);
danielk197751e3d8e2004-05-20 01:12:34 +00003124 }
danielk19776490beb2004-05-11 06:17:21 +00003125 Tcl_LinkVar(interp, "sqlite_search_count",
danielk19776f8a5032004-05-10 10:34:51 +00003126 (char*)&sqlite3_search_count, TCL_LINK_INT);
drh6bf89572004-11-03 16:27:01 +00003127 Tcl_LinkVar(interp, "sqlite_sort_count",
3128 (char*)&sqlite3_sort_count, TCL_LINK_INT);
drh55ef4d92005-08-14 01:20:37 +00003129 Tcl_LinkVar(interp, "sqlite_like_count",
3130 (char*)&sqlite3_like_count, TCL_LINK_INT);
danielk19776490beb2004-05-11 06:17:21 +00003131 Tcl_LinkVar(interp, "sqlite_interrupt_count",
danielk19776f8a5032004-05-10 10:34:51 +00003132 (char*)&sqlite3_interrupt_count, TCL_LINK_INT);
danielk19776490beb2004-05-11 06:17:21 +00003133 Tcl_LinkVar(interp, "sqlite_open_file_count",
danielk19776f8a5032004-05-10 10:34:51 +00003134 (char*)&sqlite3_open_file_count, TCL_LINK_INT);
danielk19776490beb2004-05-11 06:17:21 +00003135 Tcl_LinkVar(interp, "sqlite_current_time",
danielk19776f8a5032004-05-10 10:34:51 +00003136 (char*)&sqlite3_current_time, TCL_LINK_INT);
drh2ac3ee92004-06-07 16:27:46 +00003137 Tcl_LinkVar(interp, "sqlite_os_trace",
3138 (char*)&sqlite3_os_trace, TCL_LINK_INT);
drh51147ba2005-07-23 22:59:55 +00003139 Tcl_LinkVar(interp, "sqlite_where_trace",
3140 (char*)&sqlite3_where_trace, TCL_LINK_INT);
drh70180302005-08-02 21:42:16 +00003141#ifdef SQLITE_MEMDEBUG
drh01397202005-07-20 14:31:53 +00003142 Tcl_LinkVar(interp, "sqlite_memused",
3143 (char*)&sqlite3_memUsed, TCL_LINK_INT | TCL_LINK_READ_ONLY);
3144 Tcl_LinkVar(interp, "sqlite_memmax",
3145 (char*)&sqlite3_memMax, TCL_LINK_INT | TCL_LINK_READ_ONLY);
drh70180302005-08-02 21:42:16 +00003146#endif
drh84bfda42005-07-15 13:05:21 +00003147 Tcl_LinkVar(interp, "sqlite_query_plan",
drh9042f392005-07-15 23:24:23 +00003148 (char*)&query_plan, TCL_LINK_STRING|TCL_LINK_READ_ONLY);
danielk1977cbe21be2005-06-07 07:58:48 +00003149#ifndef SQLITE_OMIT_DISKIO
drhaf6df112005-06-07 02:12:30 +00003150 Tcl_LinkVar(interp, "sqlite_opentemp_count",
3151 (char*)&sqlite3_opentemp_count, TCL_LINK_INT);
danielk1977cbe21be2005-06-07 07:58:48 +00003152#endif
drh7c972de2003-09-06 22:18:07 +00003153 Tcl_LinkVar(interp, "sqlite_static_bind_value",
3154 (char*)&sqlite_static_bind_value, TCL_LINK_STRING);
drhab3f9fe2004-08-14 17:10:10 +00003155 Tcl_LinkVar(interp, "sqlite_temp_directory",
drheffd02b2004-08-29 23:42:13 +00003156 (char*)&sqlite3_temp_directory, TCL_LINK_STRING);
drh1398ad32005-01-19 23:24:50 +00003157 Tcl_LinkVar(interp, "bitmask_size",
3158 (char*)&bitmask_size, TCL_LINK_INT|TCL_LINK_READ_ONLY);
drh748f7632005-03-11 04:41:39 +00003159#if OS_UNIX
drhb851b2c2005-03-10 14:11:12 +00003160 Tcl_LinkVar(interp, "sqlite_sync_count",
3161 (char*)&sqlite3_sync_count, TCL_LINK_INT);
3162 Tcl_LinkVar(interp, "sqlite_fullsync_count",
3163 (char*)&sqlite3_fullsync_count, TCL_LINK_INT);
drh748f7632005-03-11 04:41:39 +00003164#endif /* OS_UNIX */
drh27d258a2004-10-30 20:23:09 +00003165 set_options(interp);
drhd1bf3512001-04-07 15:24:33 +00003166 return TCL_OK;
3167}