sqlite
[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.
Brought by drakestapleton
| 1 | /* |
| 2 | ** 2001 September 15 |
| 3 | ** |
| 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
| 6 | ** |
| 7 | ** 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. |
| 10 | ** |
| 11 | ************************************************************************* |
| 12 | ** Code for testing all sorts of SQLite interfaces. This code |
| 13 | ** is not included in the SQLite library. It is used for automated |
| 14 | ** testing of the SQLite library. |
| 15 | */ |
| 16 | #include "sqliteInt.h" |
| 17 | #if SQLITE_OS_WIN |
| 18 | # include "os_win.h" |
| 19 | # include <windows.h> |
| 20 | #else |
| 21 | # include <unistd.h> |
| 22 | # if defined(__APPLE__) |
| 23 | # include <sys/param.h> |
| 24 | # include <sys/sysctl.h> |
| 25 | # endif |
| 26 | #endif |
| 27 | |
| 28 | #include "vdbeInt.h" |
| 29 | #include "tclsqlite.h" |
| 30 | #include <stdlib.h> |
| 31 | #include <string.h> |
| 32 | |
| 33 | |
| 34 | /* |
| 35 | ** Global hash table mapping from string to pointer value. |
| 36 | ** |
| 37 | ** Sometimes the SQLite Tcl test scripts manipulate pointer values directly |
| 38 | ** (e.g. (sqlite3_stmt*) pointers). This used to be accomplished by converting |
| 39 | ** the pointer to a string containing hex digits (e.g. using printf("%p")) |
| 40 | ** before handing it to a Tcl script, then converting it back to the original |
| 41 | ** pointer value when the script passed it to a C routine. But this doesn't |
| 42 | ** work for safe compilers like filcc, which does not allow pointers decoded |
| 43 | ** from strings or integers to be dereferenced. |
| 44 | ** |
| 45 | ** To work around this restriction, each time a pointer is to be passed to a |
| 46 | ** Tcl script, an entry is added to this hash table, and the string key |
| 47 | ** passed to Tcl. Later on, when the string key is passed back to a C |
| 48 | ** routine, that routine retrieves the original pointer from this hash table. |
| 49 | ** |
| 50 | ** There is currently no way to remove elements from this hash table. |
| 51 | */ |
| 52 | static struct Test1Global { |
| 53 | int isInit; |
| 54 | Tcl_HashTable h; |
| 55 | } g; |
| 56 | |
| 57 | static void testEnterHashMutex(){ |
| 58 | sqlite3_mutex_enter( sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_APP3) ); |
| 59 | } |
| 60 | static void testLeaveHashMutex(){ |
| 61 | sqlite3_mutex_leave( sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_APP3) ); |
| 62 | } |
| 63 | |
| 64 | |
| 65 | /* |
| 66 | ** Convert a pointer to a string, and return a pointer to a buffer containing |
| 67 | ** a copy of that string. The buffer belongs to the global hash table and |
| 68 | ** should not be disposed of or modified in any way by the caller. |
| 69 | */ |
| 70 | static const char *testPointerToString(void *pPtr){ |
| 71 | char aKey[64]; |
| 72 | Tcl_HashEntry *pEntry = 0; |
| 73 | int dummy = 0; |
| 74 | const char *zRet = 0; |
| 75 | |
| 76 | if( pPtr==0 ){ |
| 77 | zRet = "0"; |
| 78 | }else{ |
| 79 | testEnterHashMutex(); |
| 80 | if( g.isInit==0 ){ |
| 81 | Tcl_InitHashTable(&g.h, TCL_STRING_KEYS); |
| 82 | g.isInit = 1; |
| 83 | } |
| 84 | sprintf(aKey, "ptr:%p", pPtr); |
| 85 | pEntry = Tcl_CreateHashEntry(&g.h, aKey, &dummy); |
| 86 | Tcl_SetHashValue(pEntry, pPtr); |
| 87 | zRet = Tcl_GetHashKey(&g.h, pEntry); |
| 88 | testLeaveHashMutex(); |
| 89 | } |
| 90 | |
| 91 | return zRet; |
| 92 | } |
| 93 | |
| 94 | /* |
| 95 | ** Convert a string returned by an earlier call to testPointerToString() |
| 96 | ** back to the original pointer. |
| 97 | */ |
| 98 | static void *testStringToPointer(const char *zStr){ |
| 99 | void *pRet = 0; |
| 100 | |
| 101 | if( zStr[0]!='0' ){ |
| 102 | testEnterHashMutex(); |
| 103 | if( g.isInit ){ |
| 104 | Tcl_HashEntry *pEntry = Tcl_FindHashEntry(&g.h, zStr); |
| 105 | if( pEntry ){ |
| 106 | pRet = Tcl_GetHashValue(pEntry); |
| 107 | } |
| 108 | } |
| 109 | testLeaveHashMutex(); |
| 110 | } |
| 111 | |
| 112 | return pRet; |
| 113 | } |
| 114 | |
| 115 | /* |
| 116 | ** This is a copy of the first part of the SqliteDb structure in |
| 117 | ** tclsqlite.c. We need it here so that the get_sqlite_pointer routine |
| 118 | ** can extract the sqlite3* pointer from an existing Tcl SQLite |
| 119 | ** connection. |
| 120 | */ |
| 121 | struct SqliteDb { |
| 122 | sqlite3 *db; |
| 123 | }; |
| 124 | |
| 125 | /* |
| 126 | ** Convert text generated by the "%p" conversion format back into |
| 127 | ** a pointer. |
| 128 | */ |
| 129 | static int testHexToInt(int h){ |
| 130 | if( h>='0' && h<='9' ){ |
| 131 | return h - '0'; |
| 132 | }else if( h>='a' && h<='f' ){ |
| 133 | return h - 'a' + 10; |
| 134 | }else{ |
| 135 | assert( h>='A' && h<='F' ); |
| 136 | return h - 'A' + 10; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | void *sqlite3TestTextToPtr(const char *z){ |
| 141 | return testStringToPointer(z); |
| 142 | } |
| 143 | const char* sqlite3TestPtrToText(void *p){ |
| 144 | return testPointerToString(p); |
| 145 | } |
| 146 | |
| 147 | |
| 148 | /* |
| 149 | ** A TCL command that returns the address of the sqlite* pointer |
| 150 | ** for an sqlite connection instance. Bad things happen if the |
| 151 | ** input is not an sqlite connection. |
| 152 | */ |
| 153 | static int SQLITE_TCLAPI get_sqlite_pointer( |
| 154 | void * clientData, |
| 155 | Tcl_Interp *interp, |
| 156 | int objc, |
| 157 | Tcl_Obj *CONST objv[] |
| 158 | ){ |
| 159 | struct SqliteDb *p; |
| 160 | Tcl_CmdInfo cmdInfo; |
| 161 | if( objc!=2 ){ |
| 162 | Tcl_WrongNumArgs(interp, 1, objv, "SQLITE-CONNECTION"); |
| 163 | return TCL_ERROR; |
| 164 | } |
| 165 | if( !Tcl_GetCommandInfo(interp, Tcl_GetString(objv[1]), &cmdInfo) ){ |
| 166 | Tcl_AppendResult(interp, "command not found: ", |
| 167 | Tcl_GetString(objv[1]), (char*)0); |
| 168 | return TCL_ERROR; |
| 169 | } |
| 170 | p = (struct SqliteDb*)cmdInfo.objClientData; |
| 171 | Tcl_AppendResult(interp, sqlite3TestPtrToText(p->db), NULL); |
| 172 | return TCL_OK; |
| 173 | } |
| 174 | |
| 175 | /* |
| 176 | ** Decode a pointer to an sqlite3 object. |
| 177 | */ |
| 178 | int getDbPointer(Tcl_Interp *interp, const char *zA, sqlite3 **ppDb){ |
| 179 | struct SqliteDb *p; |
| 180 | Tcl_CmdInfo cmdInfo; |
| 181 | if( Tcl_GetCommandInfo(interp, zA, &cmdInfo) ){ |
| 182 | p = (struct SqliteDb*)cmdInfo.objClientData; |
| 183 | *ppDb = p->db; |
| 184 | }else{ |
| 185 | *ppDb = (sqlite3*)testStringToPointer(zA); |
| 186 | } |
| 187 | return TCL_OK; |
| 188 | } |
| 189 | |
| 190 | #if SQLITE_OS_WIN |
| 191 | /* |
| 192 | ** Decode a Win32 HANDLE object. |
| 193 | */ |
| 194 | int getWin32Handle(Tcl_Interp *interp, const char *zA, LPHANDLE phFile){ |
| 195 | *phFile = (HANDLE)testStringToPointer(zA); |
| 196 | return TCL_OK; |
| 197 | } |
| 198 | #endif |
| 199 | |
| 200 | extern const char *sqlite3ErrName(int); |
| 201 | #define t1ErrorName sqlite3ErrName |
| 202 | |
| 203 | /* |
| 204 | ** Convert an sqlite3_stmt* into an sqlite3*. This depends on the |
| 205 | ** fact that the sqlite3* is the first field in the Vdbe structure. |
| 206 | */ |
| 207 | #define StmtToDb(X) sqlite3_db_handle(X) |
| 208 | |
| 209 | /* |
| 210 | ** Check a return value to make sure it agrees with the results |
| 211 | ** from sqlite3_errcode. |
| 212 | */ |
| 213 | int sqlite3TestErrCode(Tcl_Interp *interp, sqlite3 *db, int rc){ |
| 214 | if( sqlite3_threadsafe()==0 && rc!=SQLITE_MISUSE && rc!=SQLITE_OK |
| 215 | && sqlite3_errcode(db)!=rc ){ |
| 216 | char zBuf[200]; |
| 217 | int r2 = sqlite3_errcode(db); |
| 218 | sqlite3_snprintf(sizeof(zBuf), zBuf, |
| 219 | "error code %s (%d) does not match sqlite3_errcode %s (%d)", |
| 220 | t1ErrorName(rc), rc, t1ErrorName(r2), r2); |
| 221 | Tcl_ResetResult(interp); |
| 222 | Tcl_AppendResult(interp, zBuf, NULL); |
| 223 | return 1; |
| 224 | } |
| 225 | return 0; |
| 226 | } |
| 227 | |
| 228 | /* |
| 229 | ** Decode a pointer to an sqlite3_stmt object. |
| 230 | */ |
| 231 | static int getStmtPointer( |
| 232 | Tcl_Interp *interp, |
| 233 | const char *zArg, |
| 234 | sqlite3_stmt **ppStmt |
| 235 | ){ |
| 236 | *ppStmt = (sqlite3_stmt*)testStringToPointer(zArg); |
| 237 | return TCL_OK; |
| 238 | } |
| 239 | |
| 240 | /* |
| 241 | ** Generate a text representation of a pointer that can be understood |
| 242 | ** by the getDbPointer and getVmPointer routines above. |
| 243 | */ |
| 244 | int sqlite3TestMakePointerStr(Tcl_Interp *interp, char *zPtr, void *p){ |
| 245 | sqlite3_snprintf(100, zPtr, "%s", testPointerToString(p)); |
| 246 | return TCL_OK; |
| 247 | } |
| 248 | |
| 249 | /* |
| 250 | ** The callback routine for sqlite3_exec_printf(). |
| 251 | */ |
| 252 | static int exec_printf_cb(void *pArg, int argc, char **argv, char **name){ |
| 253 | Tcl_DString *str = (Tcl_DString*)pArg; |
| 254 | int i; |
| 255 | |
| 256 | if( Tcl_DStringLength(str)==0 ){ |
| 257 | for(i=0; i<argc; i++){ |
| 258 | Tcl_DStringAppendElement(str, name[i] ? name[i] : "NULL"); |
| 259 | } |
| 260 | } |
| 261 | for(i=0; i<argc; i++){ |
| 262 | Tcl_DStringAppendElement(str, argv[i] ? argv[i] : "NULL"); |
| 263 | } |
| 264 | return 0; |
| 265 | } |
| 266 | |
| 267 | /* |
| 268 | ** The I/O tracing callback. |
| 269 | */ |
| 270 | #if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE) |
| 271 | static FILE *iotrace_file = 0; |
| 272 | static void io_trace_callback(const char *zFormat, ...){ |
| 273 | va_list ap; |
| 274 | va_start(ap, zFormat); |
| 275 | vfprintf(iotrace_file, zFormat, ap); |
| 276 | va_end(ap); |
| 277 | fflush(iotrace_file); |
| 278 | } |
| 279 | #endif |
| 280 | |
| 281 | /* |
| 282 | ** Usage: io_trace FILENAME |
| 283 | ** |
| 284 | ** Turn I/O tracing on or off. If FILENAME is not an empty string, |
| 285 | ** I/O tracing begins going into FILENAME. If FILENAME is an empty |
| 286 | ** string, I/O tracing is turned off. |
| 287 | */ |
| 288 | static int SQLITE_TCLAPI test_io_trace( |
| 289 | void *NotUsed, |
| 290 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 291 | int argc, /* Number of arguments */ |
| 292 | char **argv /* Text of each argument */ |
| 293 | ){ |
| 294 | #if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE) |
| 295 | if( argc!=2 ){ |
| 296 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 297 | " FILENAME\"", 0); |
| 298 | return TCL_ERROR; |
| 299 | } |
| 300 | if( iotrace_file ){ |
| 301 | if( iotrace_file!=stdout && iotrace_file!=stderr ){ |
| 302 | fclose(iotrace_file); |
| 303 | } |
| 304 | iotrace_file = 0; |
| 305 | sqlite3IoTrace = 0; |
| 306 | } |
| 307 | if( argv[1][0] ){ |
| 308 | if( strcmp(argv[1],"stdout")==0 ){ |
| 309 | iotrace_file = stdout; |
| 310 | }else if( strcmp(argv[1],"stderr")==0 ){ |
| 311 | iotrace_file = stderr; |
| 312 | }else{ |
| 313 | iotrace_file = fopen(argv[1], "w"); |
| 314 | } |
| 315 | sqlite3IoTrace = io_trace_callback; |
| 316 | } |
| 317 | #endif |
| 318 | return TCL_OK; |
| 319 | } |
| 320 | |
| 321 | /* |
| 322 | ** Usage: filc_build |
| 323 | ** |
| 324 | ** Returns true if the program was compiled using fil-c. |
| 325 | */ |
| 326 | static int SQLITE_TCLAPI filc_build( |
| 327 | void *NotUsed, |
| 328 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 329 | int argc, /* Number of arguments */ |
| 330 | char **argv /* Text of each argument */ |
| 331 | ){ |
| 332 | int res = 0; |
| 333 | #ifdef __PIZLONATOR_WAS_HERE__ |
| 334 | res = 1; |
| 335 | #endif |
| 336 | Tcl_SetObjResult(interp, Tcl_NewIntObj(res)); |
| 337 | return TCL_OK; |
| 338 | } |
| 339 | |
| 340 | /* |
| 341 | ** Usage: clang_sanitize_address |
| 342 | ** |
| 343 | ** Returns true if the program was compiled using clang with the |
| 344 | ** -fsanitize=address switch on the command line. False otherwise. |
| 345 | ** |
| 346 | ** 2026-08-13: Environments are getting increasingly picky about |
| 347 | ** misuse. So do not run misuse tests unless ENABLE_MISUSE environment |
| 348 | ** variable is set. |
| 349 | */ |
| 350 | static int SQLITE_TCLAPI clang_sanitize_address( |
| 351 | void *NotUsed, |
| 352 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 353 | int argc, /* Number of arguments */ |
| 354 | char **argv /* Text of each argument */ |
| 355 | ){ |
| 356 | int res = 0; |
| 357 | #if defined(__has_feature) |
| 358 | # if __has_feature(address_sanitizer) |
| 359 | res = 1; |
| 360 | # endif |
| 361 | #endif |
| 362 | #ifdef __SANITIZE_ADDRESS__ |
| 363 | res = 1; |
| 364 | #endif |
| 365 | #ifdef __PIZLONATOR_WAS_HERE__ |
| 366 | /* Fil-C */ |
| 367 | res = 1; |
| 368 | #endif |
| 369 | if( getenv("ENABLE_MISUSE")==0 ){ |
| 370 | res = 1; |
| 371 | } |
| 372 | Tcl_SetObjResult(interp, Tcl_NewIntObj(res)); |
| 373 | return TCL_OK; |
| 374 | } |
| 375 | |
| 376 | /* |
| 377 | ** Usage: sqlite3_exec_printf DB FORMAT STRING |
| 378 | ** |
| 379 | ** Invoke the sqlite3_exec_printf() interface using the open database |
| 380 | ** DB. The SQL is the string FORMAT. The format string should contain |
| 381 | ** one %s or %q. STRING is the value inserted into %s or %q. |
| 382 | */ |
| 383 | static int SQLITE_TCLAPI test_exec_printf( |
| 384 | void *NotUsed, |
| 385 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 386 | int argc, /* Number of arguments */ |
| 387 | char **argv /* Text of each argument */ |
| 388 | ){ |
| 389 | sqlite3 *db; |
| 390 | Tcl_DString str; |
| 391 | int rc; |
| 392 | char *zErr = 0; |
| 393 | char *zSql; |
| 394 | char zBuf[30]; |
| 395 | if( argc!=4 ){ |
| 396 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 397 | " DB FORMAT STRING", 0); |
| 398 | return TCL_ERROR; |
| 399 | } |
| 400 | if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; |
| 401 | Tcl_DStringInit(&str); |
| 402 | zSql = sqlite3_mprintf(argv[2], argv[3]); |
| 403 | rc = sqlite3_exec(db, zSql, exec_printf_cb, &str, &zErr); |
| 404 | sqlite3_free(zSql); |
| 405 | sqlite3_snprintf(sizeof(zBuf), zBuf, "%d", rc); |
| 406 | Tcl_AppendElement(interp, zBuf); |
| 407 | Tcl_AppendElement(interp, rc==SQLITE_OK ? Tcl_DStringValue(&str) : zErr); |
| 408 | Tcl_DStringFree(&str); |
| 409 | if( zErr ) sqlite3_free(zErr); |
| 410 | if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; |
| 411 | return TCL_OK; |
| 412 | } |
| 413 | |
| 414 | /* |
| 415 | ** Usage: sqlite3_exec_hex DB HEX |
| 416 | ** |
| 417 | ** Invoke the sqlite3_exec() on a string that is obtained by translating |
| 418 | ** HEX into ASCII. Most characters are translated as is. %HH becomes |
| 419 | ** a hex character. |
| 420 | */ |
| 421 | static int SQLITE_TCLAPI test_exec_hex( |
| 422 | void *NotUsed, |
| 423 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 424 | int argc, /* Number of arguments */ |
| 425 | char **argv /* Text of each argument */ |
| 426 | ){ |
| 427 | sqlite3 *db; |
| 428 | Tcl_DString str; |
| 429 | int rc, i, j; |
| 430 | char *zErr = 0; |
| 431 | char *zHex; |
| 432 | char zSql[501]; |
| 433 | char zBuf[30]; |
| 434 | if( argc!=3 ){ |
| 435 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 436 | " DB HEX", 0); |
| 437 | return TCL_ERROR; |
| 438 | } |
| 439 | if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; |
| 440 | zHex = argv[2]; |
| 441 | for(i=j=0; i<(sizeof(zSql)-1) && zHex[j]; i++, j++){ |
| 442 | if( zHex[j]=='%' && zHex[j+1] && zHex[j+2] ){ |
| 443 | zSql[i] = (testHexToInt(zHex[j+1])<<4) + testHexToInt(zHex[j+2]); |
| 444 | j += 2; |
| 445 | }else{ |
| 446 | zSql[i] = zHex[j]; |
| 447 | } |
| 448 | } |
| 449 | zSql[i] = 0; |
| 450 | Tcl_DStringInit(&str); |
| 451 | rc = sqlite3_exec(db, zSql, exec_printf_cb, &str, &zErr); |
| 452 | sqlite3_snprintf(sizeof(zBuf), zBuf, "%d", rc); |
| 453 | Tcl_AppendElement(interp, zBuf); |
| 454 | Tcl_AppendElement(interp, rc==SQLITE_OK ? Tcl_DStringValue(&str) : zErr); |
| 455 | Tcl_DStringFree(&str); |
| 456 | if( zErr ) sqlite3_free(zErr); |
| 457 | if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; |
| 458 | return TCL_OK; |
| 459 | } |
| 460 | |
| 461 | /* |
| 462 | ** Usage: db_enter DB |
| 463 | ** db_leave DB |
| 464 | ** |
| 465 | ** Enter or leave the mutex on a database connection. |
| 466 | */ |
| 467 | static int SQLITE_TCLAPI db_enter( |
| 468 | void *NotUsed, |
| 469 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 470 | int argc, /* Number of arguments */ |
| 471 | char **argv /* Text of each argument */ |
| 472 | ){ |
| 473 | sqlite3 *db; |
| 474 | if( argc!=2 ){ |
| 475 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 476 | " DB", 0); |
| 477 | return TCL_ERROR; |
| 478 | } |
| 479 | if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; |
| 480 | sqlite3_mutex_enter(db->mutex); |
| 481 | return TCL_OK; |
| 482 | } |
| 483 | static int SQLITE_TCLAPI db_leave( |
| 484 | void *NotUsed, |
| 485 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 486 | int argc, /* Number of arguments */ |
| 487 | char **argv /* Text of each argument */ |
| 488 | ){ |
| 489 | sqlite3 *db; |
| 490 | if( argc!=2 ){ |
| 491 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 492 | " DB", 0); |
| 493 | return TCL_ERROR; |
| 494 | } |
| 495 | if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; |
| 496 | sqlite3_mutex_leave(db->mutex); |
| 497 | return TCL_OK; |
| 498 | } |
| 499 | |
| 500 | /* |
| 501 | ** Usage: sqlite3_exec DB SQL |
| 502 | ** |
| 503 | ** Invoke the sqlite3_exec interface using the open database DB |
| 504 | */ |
| 505 | static int SQLITE_TCLAPI test_exec( |
| 506 | void *NotUsed, |
| 507 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 508 | int argc, /* Number of arguments */ |
| 509 | char **argv /* Text of each argument */ |
| 510 | ){ |
| 511 | sqlite3 *db; |
| 512 | Tcl_DString str; |
| 513 | int rc; |
| 514 | char *zErr = 0; |
| 515 | char *zSql; |
| 516 | int i, j; |
| 517 | char zBuf[30]; |
| 518 | if( argc!=3 ){ |
| 519 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 520 | " DB SQL", 0); |
| 521 | return TCL_ERROR; |
| 522 | } |
| 523 | if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; |
| 524 | Tcl_DStringInit(&str); |
| 525 | zSql = sqlite3_mprintf("%s", argv[2]); |
| 526 | for(i=j=0; zSql[i];){ |
| 527 | if( zSql[i]=='%' ){ |
| 528 | zSql[j++] = (testHexToInt(zSql[i+1])<<4) + testHexToInt(zSql[i+2]); |
| 529 | i += 3; |
| 530 | }else{ |
| 531 | zSql[j++] = zSql[i++]; |
| 532 | } |
| 533 | } |
| 534 | zSql[j] = 0; |
| 535 | rc = sqlite3_exec(db, zSql, exec_printf_cb, &str, &zErr); |
| 536 | sqlite3_free(zSql); |
| 537 | sqlite3_snprintf(sizeof(zBuf), zBuf, "%d", rc); |
| 538 | Tcl_AppendElement(interp, zBuf); |
| 539 | Tcl_AppendElement(interp, rc==SQLITE_OK ? Tcl_DStringValue(&str) : zErr); |
| 540 | Tcl_DStringFree(&str); |
| 541 | if( zErr ) sqlite3_free(zErr); |
| 542 | if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; |
| 543 | return TCL_OK; |
| 544 | } |
| 545 | |
| 546 | /* |
| 547 | ** Usage: sqlite3_exec_nr DB SQL |
| 548 | ** |
| 549 | ** Invoke the sqlite3_exec interface using the open database DB. Discard |
| 550 | ** all results |
| 551 | */ |
| 552 | static int SQLITE_TCLAPI test_exec_nr( |
| 553 | void *NotUsed, |
| 554 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 555 | int argc, /* Number of arguments */ |
| 556 | char **argv /* Text of each argument */ |
| 557 | ){ |
| 558 | sqlite3 *db; |
| 559 | int rc; |
| 560 | char *zErr = 0; |
| 561 | if( argc!=3 ){ |
| 562 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 563 | " DB SQL", 0); |
| 564 | return TCL_ERROR; |
| 565 | } |
| 566 | if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; |
| 567 | rc = sqlite3_exec(db, argv[2], 0, 0, &zErr); |
| 568 | if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; |
| 569 | return TCL_OK; |
| 570 | } |
| 571 | |
| 572 | /* |
| 573 | ** Usage: sqlite3_mprintf_z_test SEPARATOR ARG0 ARG1 ... |
| 574 | ** |
| 575 | ** Test the %z format of sqlite_mprintf(). Use multiple mprintf() calls to |
| 576 | ** concatenate arg0 through argn using separator as the separator. |
| 577 | ** Return the result. |
| 578 | */ |
| 579 | static int SQLITE_TCLAPI test_mprintf_z( |
| 580 | void *NotUsed, |
| 581 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 582 | int argc, /* Number of arguments */ |
| 583 | char **argv /* Text of each argument */ |
| 584 | ){ |
| 585 | char *zResult = 0; |
| 586 | int i; |
| 587 | |
| 588 | for(i=2; i<argc && (i==2 || zResult); i++){ |
| 589 | zResult = sqlite3_mprintf("%z%s%s", zResult, argv[1], argv[i]); |
| 590 | } |
| 591 | Tcl_AppendResult(interp, zResult, NULL); |
| 592 | sqlite3_free(zResult); |
| 593 | return TCL_OK; |
| 594 | } |
| 595 | |
| 596 | /* |
| 597 | ** Usage: sqlite3_mprintf_n_test STRING |
| 598 | ** |
| 599 | ** Test the %n format of sqlite_mprintf(). Return the length of the |
| 600 | ** input string. |
| 601 | */ |
| 602 | static int SQLITE_TCLAPI test_mprintf_n( |
| 603 | void *NotUsed, |
| 604 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 605 | int argc, /* Number of arguments */ |
| 606 | char **argv /* Text of each argument */ |
| 607 | ){ |
| 608 | char *zStr; |
| 609 | int n = 0; |
| 610 | zStr = sqlite3_mprintf("%s%n", argv[1], &n); |
| 611 | sqlite3_free(zStr); |
| 612 | Tcl_SetObjResult(interp, Tcl_NewIntObj(n)); |
| 613 | return TCL_OK; |
| 614 | } |
| 615 | |
| 616 | /* |
| 617 | ** Usage: sqlite3_snprintf_int SIZE FORMAT INT |
| 618 | ** |
| 619 | ** Test the of sqlite3_snprintf() routine. SIZE is the size of the |
| 620 | ** output buffer in bytes. The maximum size is 100. FORMAT is the |
| 621 | ** format string. INT is a single integer argument. The FORMAT |
| 622 | ** string must require no more than this one integer argument. If |
| 623 | ** You pass in a format string that requires more than one argument, |
| 624 | ** bad things will happen. |
| 625 | */ |
| 626 | static int SQLITE_TCLAPI test_snprintf_int( |
| 627 | void *NotUsed, |
| 628 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 629 | int argc, /* Number of arguments */ |
| 630 | char **argv /* Text of each argument */ |
| 631 | ){ |
| 632 | char zStr[100]; |
| 633 | int n = atoi(argv[1]); |
| 634 | const char *zFormat = argv[2]; |
| 635 | int a1 = atoi(argv[3]); |
| 636 | if( n>sizeof(zStr) ) n = sizeof(zStr); |
| 637 | sqlite3_snprintf(sizeof(zStr), zStr, "abcdefghijklmnopqrstuvwxyz"); |
| 638 | sqlite3_snprintf(n, zStr, zFormat, a1); |
| 639 | Tcl_AppendResult(interp, zStr, NULL); |
| 640 | return TCL_OK; |
| 641 | } |
| 642 | |
| 643 | #ifndef SQLITE_OMIT_GET_TABLE |
| 644 | |
| 645 | /* |
| 646 | ** Usage: sqlite3_get_table_printf DB FORMAT STRING ?--no-counts? |
| 647 | ** |
| 648 | ** Invoke the sqlite3_get_table_printf() interface using the open database |
| 649 | ** DB. The SQL is the string FORMAT. The format string should contain |
| 650 | ** one %s or %q. STRING is the value inserted into %s or %q. |
| 651 | */ |
| 652 | static int SQLITE_TCLAPI test_get_table_printf( |
| 653 | void *NotUsed, |
| 654 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 655 | int argc, /* Number of arguments */ |
| 656 | char **argv /* Text of each argument */ |
| 657 | ){ |
| 658 | sqlite3 *db; |
| 659 | Tcl_DString str; |
| 660 | int rc; |
| 661 | char *zErr = 0; |
| 662 | int nRow = 0, nCol = 0; |
| 663 | char **aResult; |
| 664 | int i; |
| 665 | char zBuf[30]; |
| 666 | char *zSql; |
| 667 | int resCount = -1; |
| 668 | if( argc==5 ){ |
| 669 | if( Tcl_GetInt(interp, argv[4], &resCount) ) return TCL_ERROR; |
| 670 | } |
| 671 | if( argc!=4 && argc!=5 ){ |
| 672 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 673 | " DB FORMAT STRING ?COUNT?", 0); |
| 674 | return TCL_ERROR; |
| 675 | } |
| 676 | if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; |
| 677 | Tcl_DStringInit(&str); |
| 678 | zSql = sqlite3_mprintf(argv[2],argv[3]); |
| 679 | if( argc==5 ){ |
| 680 | rc = sqlite3_get_table(db, zSql, &aResult, 0, 0, &zErr); |
| 681 | }else{ |
| 682 | rc = sqlite3_get_table(db, zSql, &aResult, &nRow, &nCol, &zErr); |
| 683 | resCount = (nRow+1)*nCol; |
| 684 | } |
| 685 | sqlite3_free(zSql); |
| 686 | sqlite3_snprintf(sizeof(zBuf), zBuf, "%d", rc); |
| 687 | Tcl_ResetResult(interp); |
| 688 | Tcl_AppendElement(interp, zBuf); |
| 689 | if( rc==SQLITE_OK ){ |
| 690 | if( argc==4 ){ |
| 691 | sqlite3_snprintf(sizeof(zBuf), zBuf, "%d", nRow); |
| 692 | Tcl_AppendElement(interp, zBuf); |
| 693 | sqlite3_snprintf(sizeof(zBuf), zBuf, "%d", nCol); |
| 694 | Tcl_AppendElement(interp, zBuf); |
| 695 | } |
| 696 | for(i=0; i<resCount; i++){ |
| 697 | Tcl_AppendElement(interp, aResult[i] ? aResult[i] : "NULL"); |
| 698 | } |
| 699 | }else{ |
| 700 | Tcl_AppendElement(interp, zErr); |
| 701 | } |
| 702 | sqlite3_free_table(aResult); |
| 703 | if( zErr ) sqlite3_free(zErr); |
| 704 | if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; |
| 705 | return TCL_OK; |
| 706 | } |
| 707 | |
| 708 | #endif /* SQLITE_OMIT_GET_TABLE */ |
| 709 | |
| 710 | |
| 711 | /* |
| 712 | ** Usage: sqlite3_last_insert_rowid DB |
| 713 | ** |
| 714 | ** Returns the integer ROWID of the most recent insert. |
| 715 | */ |
| 716 | static int SQLITE_TCLAPI test_last_rowid( |
| 717 | void *NotUsed, |
| 718 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 719 | int argc, /* Number of arguments */ |
| 720 | char **argv /* Text of each argument */ |
| 721 | ){ |
| 722 | sqlite3 *db; |
| 723 | char zBuf[30]; |
| 724 | |
| 725 | if( argc!=2 ){ |
| 726 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " DB\"", NULL); |
| 727 | return TCL_ERROR; |
| 728 | } |
| 729 | if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; |
| 730 | sqlite3_snprintf(sizeof(zBuf), zBuf, "%lld", sqlite3_last_insert_rowid(db)); |
| 731 | Tcl_AppendResult(interp, zBuf, NULL); |
| 732 | return SQLITE_OK; |
| 733 | } |
| 734 | |
| 735 | /* |
| 736 | ** Usage: sqlite3_key DB KEY |
| 737 | ** |
| 738 | ** Set the codec key. |
| 739 | */ |
| 740 | static int SQLITE_TCLAPI test_key( |
| 741 | void *NotUsed, |
| 742 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 743 | int argc, /* Number of arguments */ |
| 744 | char **argv /* Text of each argument */ |
| 745 | ){ |
| 746 | return TCL_OK; |
| 747 | } |
| 748 | |
| 749 | /* |
| 750 | ** Usage: sqlite3_rekey DB KEY |
| 751 | ** |
| 752 | ** Change the codec key. |
| 753 | */ |
| 754 | static int SQLITE_TCLAPI test_rekey( |
| 755 | void *NotUsed, |
| 756 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 757 | int argc, /* Number of arguments */ |
| 758 | char **argv /* Text of each argument */ |
| 759 | ){ |
| 760 | return TCL_OK; |
| 761 | } |
| 762 | |
| 763 | /* |
| 764 | ** Usage: sqlite3_close DB |
| 765 | ** |
| 766 | ** Closes the database opened by sqlite3_open. |
| 767 | */ |
| 768 | static int SQLITE_TCLAPI sqlite_test_close( |
| 769 | void *NotUsed, |
| 770 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 771 | int argc, /* Number of arguments */ |
| 772 | char **argv /* Text of each argument */ |
| 773 | ){ |
| 774 | sqlite3 *db; |
| 775 | int rc; |
| 776 | if( argc!=2 ){ |
| 777 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 778 | " FILENAME\"", 0); |
| 779 | return TCL_ERROR; |
| 780 | } |
| 781 | if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; |
| 782 | rc = sqlite3_close(db); |
| 783 | Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC); |
| 784 | return TCL_OK; |
| 785 | } |
| 786 | |
| 787 | /* |
| 788 | ** Usage: sqlite3_close_v2 DB |
| 789 | ** |
| 790 | ** Closes the database opened by sqlite3_open. |
| 791 | */ |
| 792 | static int SQLITE_TCLAPI sqlite_test_close_v2( |
| 793 | void *NotUsed, |
| 794 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 795 | int argc, /* Number of arguments */ |
| 796 | char **argv /* Text of each argument */ |
| 797 | ){ |
| 798 | sqlite3 *db; |
| 799 | int rc; |
| 800 | if( argc!=2 ){ |
| 801 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 802 | " FILENAME\"", 0); |
| 803 | return TCL_ERROR; |
| 804 | } |
| 805 | if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; |
| 806 | rc = sqlite3_close_v2(db); |
| 807 | Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC); |
| 808 | return TCL_OK; |
| 809 | } |
| 810 | |
| 811 | /* |
| 812 | ** Implementation of the x_coalesce() function. |
| 813 | ** Return the first argument non-NULL argument. |
| 814 | */ |
| 815 | static void t1_ifnullFunc( |
| 816 | sqlite3_context *context, |
| 817 | int argc, |
| 818 | sqlite3_value **argv |
| 819 | ){ |
| 820 | int i; |
| 821 | for(i=0; i<argc; i++){ |
| 822 | if( SQLITE_NULL!=sqlite3_value_type(argv[i]) ){ |
| 823 | int n = sqlite3_value_bytes(argv[i]); |
| 824 | sqlite3_result_text(context, (char*)sqlite3_value_text(argv[i]), |
| 825 | n, SQLITE_TRANSIENT); |
| 826 | break; |
| 827 | } |
| 828 | } |
| 829 | } |
| 830 | |
| 831 | /* |
| 832 | ** These are test functions. hex8() interprets its argument as |
| 833 | ** UTF8 and returns a hex encoding. hex16le() interprets its argument |
| 834 | ** as UTF16le and returns a hex encoding. |
| 835 | */ |
| 836 | static void hex8Func(sqlite3_context *p, int argc, sqlite3_value **argv){ |
| 837 | const unsigned char *z; |
| 838 | int i; |
| 839 | char zBuf[200]; |
| 840 | z = sqlite3_value_text(argv[0]); |
| 841 | for(i=0; i<sizeof(zBuf)/2 - 2 && z[i]; i++){ |
| 842 | sqlite3_snprintf(sizeof(zBuf)-i*2, &zBuf[i*2], "%02x", z[i]); |
| 843 | } |
| 844 | zBuf[i*2] = 0; |
| 845 | sqlite3_result_text(p, (char*)zBuf, -1, SQLITE_TRANSIENT); |
| 846 | } |
| 847 | #ifndef SQLITE_OMIT_UTF16 |
| 848 | static void hex16Func(sqlite3_context *p, int argc, sqlite3_value **argv){ |
| 849 | const unsigned short int *z; |
| 850 | int i; |
| 851 | char zBuf[400]; |
| 852 | z = sqlite3_value_text16(argv[0]); |
| 853 | for(i=0; i<sizeof(zBuf)/4 - 4 && z[i]; i++){ |
| 854 | sqlite3_snprintf(sizeof(zBuf)-i*4, &zBuf[i*4],"%04x", z[i]&0xff); |
| 855 | } |
| 856 | zBuf[i*4] = 0; |
| 857 | sqlite3_result_text(p, (char*)zBuf, -1, SQLITE_TRANSIENT); |
| 858 | } |
| 859 | #endif |
| 860 | |
| 861 | /* |
| 862 | ** A structure into which to accumulate text. |
| 863 | */ |
| 864 | struct dstr { |
| 865 | int nAlloc; /* Space allocated */ |
| 866 | int nUsed; /* Space used */ |
| 867 | char *z; /* The space */ |
| 868 | }; |
| 869 | |
| 870 | /* |
| 871 | ** Append text to a dstr |
| 872 | */ |
| 873 | static void dstrAppend(struct dstr *p, const char *z, int divider){ |
| 874 | int n = (int)strlen(z); |
| 875 | if( p->nUsed + n + 2 > p->nAlloc ){ |
| 876 | char *zNew; |
| 877 | p->nAlloc = p->nAlloc*2 + n + 200; |
| 878 | zNew = sqlite3_realloc(p->z, p->nAlloc); |
| 879 | if( zNew==0 ){ |
| 880 | sqlite3_free(p->z); |
| 881 | memset(p, 0, sizeof(*p)); |
| 882 | return; |
| 883 | } |
| 884 | p->z = zNew; |
| 885 | } |
| 886 | if( divider && p->nUsed>0 ){ |
| 887 | p->z[p->nUsed++] = divider; |
| 888 | } |
| 889 | memcpy(&p->z[p->nUsed], z, n+1); |
| 890 | p->nUsed += n; |
| 891 | } |
| 892 | |
| 893 | /* |
| 894 | ** Invoked for each callback from sqlite3ExecFunc |
| 895 | */ |
| 896 | static int execFuncCallback(void *pData, int argc, char **argv, char **NotUsed){ |
| 897 | struct dstr *p = (struct dstr*)pData; |
| 898 | int i; |
| 899 | for(i=0; i<argc; i++){ |
| 900 | if( argv[i]==0 ){ |
| 901 | dstrAppend(p, "NULL", ' '); |
| 902 | }else{ |
| 903 | dstrAppend(p, argv[i], ' '); |
| 904 | } |
| 905 | } |
| 906 | return 0; |
| 907 | } |
| 908 | |
| 909 | /* |
| 910 | ** Implementation of the x_sqlite_exec() function. This function takes |
| 911 | ** a single argument and attempts to execute that argument as SQL code. |
| 912 | ** This is illegal and should set the SQLITE_MISUSE flag on the database. |
| 913 | ** |
| 914 | ** 2004-Jan-07: We have changed this to make it legal to call sqlite3_exec() |
| 915 | ** from within a function call. |
| 916 | ** |
| 917 | ** This routine simulates the effect of having two threads attempt to |
| 918 | ** use the same database at the same time. |
| 919 | */ |
| 920 | static void sqlite3ExecFunc( |
| 921 | sqlite3_context *context, |
| 922 | int argc, |
| 923 | sqlite3_value **argv |
| 924 | ){ |
| 925 | struct dstr x; |
| 926 | memset(&x, 0, sizeof(x)); |
| 927 | (void)sqlite3_exec((sqlite3*)sqlite3_user_data(context), |
| 928 | (char*)sqlite3_value_text(argv[0]), |
| 929 | execFuncCallback, &x, 0); |
| 930 | sqlite3_result_text(context, x.z, x.nUsed, SQLITE_TRANSIENT); |
| 931 | sqlite3_free(x.z); |
| 932 | } |
| 933 | |
| 934 | /* |
| 935 | ** Implementation of tkt2213func(), a scalar function that takes exactly |
| 936 | ** one argument. It has two interesting features: |
| 937 | ** |
| 938 | ** * It calls sqlite3_value_text() 3 times on the argument sqlite3_value*. |
| 939 | ** If the three pointers returned are not the same an SQL error is raised. |
| 940 | ** |
| 941 | ** * Otherwise it returns a copy of the text representation of its |
| 942 | ** argument in such a way as the VDBE representation is a Mem* cell |
| 943 | ** with the MEM_Term flag clear. |
| 944 | ** |
| 945 | ** Ticket #2213 can therefore be tested by evaluating the following |
| 946 | ** SQL expression: |
| 947 | ** |
| 948 | ** tkt2213func(tkt2213func('a string')); |
| 949 | */ |
| 950 | static void tkt2213Function( |
| 951 | sqlite3_context *context, |
| 952 | int argc, |
| 953 | sqlite3_value **argv |
| 954 | ){ |
| 955 | int nText; |
| 956 | unsigned char const *zText1; |
| 957 | unsigned char const *zText2; |
| 958 | unsigned char const *zText3; |
| 959 | |
| 960 | nText = sqlite3_value_bytes(argv[0]); |
| 961 | zText1 = sqlite3_value_text(argv[0]); |
| 962 | zText2 = sqlite3_value_text(argv[0]); |
| 963 | zText3 = sqlite3_value_text(argv[0]); |
| 964 | |
| 965 | if( zText1!=zText2 || zText2!=zText3 ){ |
| 966 | sqlite3_result_error(context, "tkt2213 is not fixed", -1); |
| 967 | }else{ |
| 968 | char *zCopy = (char *)sqlite3_malloc(nText); |
| 969 | memcpy(zCopy, zText1, nText); |
| 970 | sqlite3_result_text(context, zCopy, nText, sqlite3_free); |
| 971 | } |
| 972 | } |
| 973 | |
| 974 | /* |
| 975 | ** The following SQL function takes 4 arguments. The 2nd and |
| 976 | ** 4th argument must be one of these strings: 'text', 'text16', |
| 977 | ** or 'blob' corresponding to API functions |
| 978 | ** |
| 979 | ** sqlite3_value_text() |
| 980 | ** sqlite3_value_text16() |
| 981 | ** sqlite3_value_blob() |
| 982 | ** |
| 983 | ** The third argument is a string, either 'bytes' or 'bytes16' or 'noop', |
| 984 | ** corresponding to APIs: |
| 985 | ** |
| 986 | ** sqlite3_value_bytes() |
| 987 | ** sqlite3_value_bytes16() |
| 988 | ** noop |
| 989 | ** |
| 990 | ** The APIs designated by the 2nd through 4th arguments are applied |
| 991 | ** to the first argument in order. If the pointers returned by the |
| 992 | ** second and fourth are different, this routine returns 1. Otherwise, |
| 993 | ** this routine returns 0. |
| 994 | ** |
| 995 | ** This function is used to test to see when returned pointers from |
| 996 | ** the _text(), _text16() and _blob() APIs become invalidated. |
| 997 | */ |
| 998 | static void ptrChngFunction( |
| 999 | sqlite3_context *context, |
| 1000 | int argc, |
| 1001 | sqlite3_value **argv |
| 1002 | ){ |
| 1003 | const void *p1, *p2; |
| 1004 | const char *zCmd; |
| 1005 | if( argc!=4 ) return; |
| 1006 | zCmd = (const char*)sqlite3_value_text(argv[1]); |
| 1007 | if( zCmd==0 ) return; |
| 1008 | if( strcmp(zCmd,"text")==0 ){ |
| 1009 | p1 = (const void*)sqlite3_value_text(argv[0]); |
| 1010 | #ifndef SQLITE_OMIT_UTF16 |
| 1011 | }else if( strcmp(zCmd, "text16")==0 ){ |
| 1012 | p1 = (const void*)sqlite3_value_text16(argv[0]); |
| 1013 | #endif |
| 1014 | }else if( strcmp(zCmd, "blob")==0 ){ |
| 1015 | p1 = (const void*)sqlite3_value_blob(argv[0]); |
| 1016 | }else{ |
| 1017 | return; |
| 1018 | } |
| 1019 | zCmd = (const char*)sqlite3_value_text(argv[2]); |
| 1020 | if( zCmd==0 ) return; |
| 1021 | if( strcmp(zCmd,"bytes")==0 ){ |
| 1022 | sqlite3_value_bytes(argv[0]); |
| 1023 | #ifndef SQLITE_OMIT_UTF16 |
| 1024 | }else if( strcmp(zCmd, "bytes16")==0 ){ |
| 1025 | sqlite3_value_bytes16(argv[0]); |
| 1026 | #endif |
| 1027 | }else if( strcmp(zCmd, "noop")==0 ){ |
| 1028 | /* do nothing */ |
| 1029 | }else{ |
| 1030 | return; |
| 1031 | } |
| 1032 | zCmd = (const char*)sqlite3_value_text(argv[3]); |
| 1033 | if( zCmd==0 ) return; |
| 1034 | if( strcmp(zCmd,"text")==0 ){ |
| 1035 | p2 = (const void*)sqlite3_value_text(argv[0]); |
| 1036 | #ifndef SQLITE_OMIT_UTF16 |
| 1037 | }else if( strcmp(zCmd, "text16")==0 ){ |
| 1038 | p2 = (const void*)sqlite3_value_text16(argv[0]); |
| 1039 | #endif |
| 1040 | }else if( strcmp(zCmd, "blob")==0 ){ |
| 1041 | p2 = (const void*)sqlite3_value_blob(argv[0]); |
| 1042 | }else{ |
| 1043 | return; |
| 1044 | } |
| 1045 | sqlite3_result_int(context, p1!=p2); |
| 1046 | } |
| 1047 | |
| 1048 | /* |
| 1049 | ** This SQL function returns a different answer each time it is called, even if |
| 1050 | ** the arguments are the same. |
| 1051 | */ |
| 1052 | static void nondeterministicFunction( |
| 1053 | sqlite3_context *context, |
| 1054 | int argc, |
| 1055 | sqlite3_value **argv |
| 1056 | ){ |
| 1057 | static int cnt = 0; |
| 1058 | sqlite3_result_int(context, cnt++); |
| 1059 | } |
| 1060 | |
| 1061 | /* |
| 1062 | ** This SQL function returns the integer value of its argument as a MEM_IntReal |
| 1063 | ** value. |
| 1064 | */ |
| 1065 | static void intrealFunction( |
| 1066 | sqlite3_context *context, |
| 1067 | int argc, |
| 1068 | sqlite3_value **argv |
| 1069 | ){ |
| 1070 | sqlite3_int64 v = sqlite3_value_int64(argv[0]); |
| 1071 | sqlite3_result_int64(context, v); |
| 1072 | sqlite3_test_control(SQLITE_TESTCTRL_RESULT_INTREAL, context); |
| 1073 | } |
| 1074 | |
| 1075 | /* |
| 1076 | ** These SQL functions attempt to return a value (their first argument) |
| 1077 | ** that has been modified to have multiple datatypes. For example both |
| 1078 | ** TEXT and INTEGER. |
| 1079 | */ |
| 1080 | static void addTextTypeFunction( |
| 1081 | sqlite3_context *context, |
| 1082 | int argc, |
| 1083 | sqlite3_value **argv |
| 1084 | ){ |
| 1085 | (void)sqlite3_value_text(argv[0]); |
| 1086 | (void)argc; |
| 1087 | sqlite3_result_value(context, argv[0]); |
| 1088 | } |
| 1089 | static void addIntTypeFunction( |
| 1090 | sqlite3_context *context, |
| 1091 | int argc, |
| 1092 | sqlite3_value **argv |
| 1093 | ){ |
| 1094 | (void)sqlite3_value_int64(argv[0]); |
| 1095 | (void)argc; |
| 1096 | sqlite3_result_value(context, argv[0]); |
| 1097 | } |
| 1098 | static void addRealTypeFunction( |
| 1099 | sqlite3_context *context, |
| 1100 | int argc, |
| 1101 | sqlite3_value **argv |
| 1102 | ){ |
| 1103 | (void)sqlite3_value_double(argv[0]); |
| 1104 | (void)argc; |
| 1105 | sqlite3_result_value(context, argv[0]); |
| 1106 | } |
| 1107 | |
| 1108 | /* |
| 1109 | ** SQL function: strtod(X) |
| 1110 | ** |
| 1111 | ** Use the C-library strtod() function to convert string X into a double. |
| 1112 | ** Used for comparing the accuracy of SQLite's internal text-to-float conversion |
| 1113 | ** routines against the C-library. |
| 1114 | */ |
| 1115 | static void shellStrtod( |
| 1116 | sqlite3_context *pCtx, |
| 1117 | int nVal, |
| 1118 | sqlite3_value **apVal |
| 1119 | ){ |
| 1120 | char *z = (char*)sqlite3_value_text(apVal[0]); |
| 1121 | UNUSED_PARAMETER(nVal); |
| 1122 | if( z==0 ) return; |
| 1123 | sqlite3_result_double(pCtx, strtod(z,0)); |
| 1124 | } |
| 1125 | |
| 1126 | /* |
| 1127 | ** SQL function: dtostr(X) |
| 1128 | ** |
| 1129 | ** Use the C-library printf() function to convert real value X into a string. |
| 1130 | ** Used for comparing the accuracy of SQLite's internal float-to-text conversion |
| 1131 | ** routines against the C-library. |
| 1132 | */ |
| 1133 | static void shellDtostr( |
| 1134 | sqlite3_context *pCtx, |
| 1135 | int nVal, |
| 1136 | sqlite3_value **apVal |
| 1137 | ){ |
| 1138 | double r = sqlite3_value_double(apVal[0]); |
| 1139 | int n = nVal>=2 ? sqlite3_value_int(apVal[1]) : 26; |
| 1140 | char z[400]; |
| 1141 | if( n<1 ) n = 1; |
| 1142 | if( n>350 ) n = 350; |
| 1143 | sprintf(z, "%#+.*e", n, r); |
| 1144 | sqlite3_result_text(pCtx, z, -1, SQLITE_TRANSIENT); |
| 1145 | } |
| 1146 | |
| 1147 | /* |
| 1148 | ** We need a method for setting the pointer values created by the |
| 1149 | ** intarray_addr, int64array_addr, doublearray_addr, and textarray_addr |
| 1150 | ** routines below. The inttoptr(X) SQL function accomplishes |
| 1151 | ** this. Tcl scripts will bind an array address as an integer X and |
| 1152 | ** the inttoptr() SQL function will use sqlite3_result_pointer() to |
| 1153 | ** convert that integer into a pointer usable by carray(). |
| 1154 | */ |
| 1155 | static void inttoptrFunc( |
| 1156 | sqlite3_context *context, |
| 1157 | int argc, |
| 1158 | sqlite3_value **argv |
| 1159 | ){ |
| 1160 | unsigned char *p; |
| 1161 | p = (unsigned char*)testStringToPointer( |
| 1162 | (const char*)sqlite3_value_text(argv[0])); |
| 1163 | if( argc==2 ){ |
| 1164 | p += sqlite3_value_int(argv[1]); |
| 1165 | } |
| 1166 | sqlite3_result_pointer(context, p, "carray", 0); |
| 1167 | } |
| 1168 | |
| 1169 | /* |
| 1170 | ** Usage: sqlite3_create_function DB |
| 1171 | ** |
| 1172 | ** Call the sqlite3_create_function API on the given database in order |
| 1173 | ** to create a function named "x_coalesce". This function does the same thing |
| 1174 | ** as the "coalesce" function. This function also registers an SQL function |
| 1175 | ** named "x_sqlite_exec" that invokes sqlite3_exec(). Invoking sqlite3_exec() |
| 1176 | ** in this way is illegal recursion and should raise an SQLITE_MISUSE error. |
| 1177 | ** The effect is similar to trying to use the same database connection from |
| 1178 | ** two threads at the same time. |
| 1179 | ** |
| 1180 | ** The original motivation for this routine was to be able to call the |
| 1181 | ** sqlite3_create_function function while a query is in progress in order |
| 1182 | ** to test the SQLITE_MISUSE detection logic. It is now also used to register |
| 1183 | ** a bunch of SQL functions that are useful for testing. |
| 1184 | */ |
| 1185 | static int SQLITE_TCLAPI test_create_function( |
| 1186 | void *NotUsed, |
| 1187 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 1188 | int argc, /* Number of arguments */ |
| 1189 | char **argv /* Text of each argument */ |
| 1190 | ){ |
| 1191 | int rc; |
| 1192 | sqlite3 *db; |
| 1193 | |
| 1194 | if( argc!=2 ){ |
| 1195 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 1196 | " DB\"", 0); |
| 1197 | return TCL_ERROR; |
| 1198 | } |
| 1199 | if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; |
| 1200 | rc = sqlite3_create_function(db, "x_coalesce", -1, SQLITE_UTF8, 0, |
| 1201 | t1_ifnullFunc, 0, 0); |
| 1202 | if( rc==SQLITE_OK ){ |
| 1203 | rc = sqlite3_create_function(db, "hex8", 1, SQLITE_UTF8 | SQLITE_DETERMINISTIC, |
| 1204 | 0, hex8Func, 0, 0); |
| 1205 | } |
| 1206 | #ifndef SQLITE_OMIT_UTF16 |
| 1207 | if( rc==SQLITE_OK ){ |
| 1208 | rc = sqlite3_create_function(db, "hex16", 1, SQLITE_UTF16 | SQLITE_DETERMINISTIC, |
| 1209 | 0, hex16Func, 0, 0); |
| 1210 | } |
| 1211 | #endif |
| 1212 | if( rc==SQLITE_OK ){ |
| 1213 | rc = sqlite3_create_function(db, "tkt2213func", 1, SQLITE_ANY, 0, |
| 1214 | tkt2213Function, 0, 0); |
| 1215 | } |
| 1216 | if( rc==SQLITE_OK ){ |
| 1217 | rc = sqlite3_create_function(db, "pointer_change", 4, SQLITE_ANY, 0, |
| 1218 | ptrChngFunction, 0, 0); |
| 1219 | } |
| 1220 | |
| 1221 | /* Functions counter1() and counter2() have the same implementation - they |
| 1222 | ** both return an ascending integer with each call. But counter1() is marked |
| 1223 | ** as non-deterministic and counter2() is marked as deterministic. |
| 1224 | */ |
| 1225 | if( rc==SQLITE_OK ){ |
| 1226 | rc = sqlite3_create_function(db, "counter1", -1, SQLITE_UTF8, |
| 1227 | 0, nondeterministicFunction, 0, 0); |
| 1228 | } |
| 1229 | if( rc==SQLITE_OK ){ |
| 1230 | rc = sqlite3_create_function(db, "counter2", -1, SQLITE_UTF8|SQLITE_DETERMINISTIC, |
| 1231 | 0, nondeterministicFunction, 0, 0); |
| 1232 | } |
| 1233 | |
| 1234 | /* The intreal() function converts its argument to an integer and returns |
| 1235 | ** it as a MEM_IntReal. |
| 1236 | */ |
| 1237 | if( rc==SQLITE_OK ){ |
| 1238 | rc = sqlite3_create_function(db, "intreal", 1, SQLITE_UTF8, |
| 1239 | 0, intrealFunction, 0, 0); |
| 1240 | } |
| 1241 | |
| 1242 | /* The add_text_type(), add_int_type(), and add_real_type() functions |
| 1243 | ** attempt to return a value that has multiple datatypes. |
| 1244 | */ |
| 1245 | if( rc==SQLITE_OK ){ |
| 1246 | rc = sqlite3_create_function(db, "add_text_type", 1, SQLITE_UTF8, |
| 1247 | 0, addTextTypeFunction, 0, 0); |
| 1248 | } |
| 1249 | if( rc==SQLITE_OK ){ |
| 1250 | rc = sqlite3_create_function(db, "add_int_type", 1, SQLITE_UTF8, |
| 1251 | 0, addIntTypeFunction, 0, 0); |
| 1252 | } |
| 1253 | if( rc==SQLITE_OK ){ |
| 1254 | rc = sqlite3_create_function(db, "add_real_type", 1, SQLITE_UTF8, |
| 1255 | 0, addRealTypeFunction, 0, 0); |
| 1256 | } |
| 1257 | |
| 1258 | /* Functions strtod() and dtostr() work as in the shell. These routines |
| 1259 | ** use the standard C library to convert between floating point and |
| 1260 | ** text. This is used to compare SQLite's internal conversion routines |
| 1261 | ** against the standard library conversion routines. |
| 1262 | ** |
| 1263 | ** Both routines copy/pasted from the shell.c.in implementation |
| 1264 | ** on 2023-07-03. |
| 1265 | */ |
| 1266 | if( rc==SQLITE_OK ){ |
| 1267 | rc = sqlite3_create_function(db, "strtod", 1, SQLITE_UTF8, 0, |
| 1268 | shellStrtod, 0, 0); |
| 1269 | } |
| 1270 | if( rc==SQLITE_OK ){ |
| 1271 | rc = sqlite3_create_function(db, "dtostr", 1, SQLITE_UTF8, 0, |
| 1272 | shellDtostr, 0, 0); |
| 1273 | } |
| 1274 | if( rc==SQLITE_OK ){ |
| 1275 | rc = sqlite3_create_function(db, "dtostr", 2, SQLITE_UTF8, 0, |
| 1276 | shellDtostr, 0, 0); |
| 1277 | } |
| 1278 | if( rc==SQLITE_OK ){ |
| 1279 | rc = sqlite3_create_function(db, "inttoptr", 1, SQLITE_UTF8, 0, |
| 1280 | inttoptrFunc, 0, 0); |
| 1281 | } |
| 1282 | if( rc==SQLITE_OK ){ |
| 1283 | rc = sqlite3_create_function(db, "inttoptr", 2, SQLITE_UTF8, 0, |
| 1284 | inttoptrFunc, 0, 0); |
| 1285 | } |
| 1286 | |
| 1287 | #ifndef SQLITE_OMIT_UTF16 |
| 1288 | /* Use the sqlite3_create_function16() API here. Mainly for fun, but also |
| 1289 | ** because it is not tested anywhere else. */ |
| 1290 | if( rc==SQLITE_OK ){ |
| 1291 | const void *zUtf16; |
| 1292 | sqlite3_value *pVal; |
| 1293 | sqlite3_mutex_enter(db->mutex); |
| 1294 | pVal = sqlite3ValueNew(db); |
| 1295 | sqlite3ValueSetStr(pVal, -1, "x_sqlite_exec", SQLITE_UTF8, SQLITE_STATIC); |
| 1296 | zUtf16 = sqlite3ValueText(pVal, SQLITE_UTF16NATIVE); |
| 1297 | if( db->mallocFailed ){ |
| 1298 | rc = SQLITE_NOMEM; |
| 1299 | }else{ |
| 1300 | rc = sqlite3_create_function16(db, zUtf16, |
| 1301 | 1, SQLITE_UTF16, db, sqlite3ExecFunc, 0, 0); |
| 1302 | } |
| 1303 | sqlite3ValueFree(pVal); |
| 1304 | sqlite3_mutex_leave(db->mutex); |
| 1305 | } |
| 1306 | #endif |
| 1307 | |
| 1308 | if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; |
| 1309 | Tcl_SetResult(interp, (char *)t1ErrorName(rc), 0); |
| 1310 | return TCL_OK; |
| 1311 | } |
| 1312 | |
| 1313 | /* |
| 1314 | ** Usage: sqlite3_drop_modules DB ?NAME ...? |
| 1315 | ** |
| 1316 | ** Invoke the sqlite3_drop_modules(D,L) interface on database |
| 1317 | ** connection DB, in order to drop all modules except those named in |
| 1318 | ** the argument. |
| 1319 | */ |
| 1320 | static int SQLITE_TCLAPI test_drop_modules( |
| 1321 | void *NotUsed, |
| 1322 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 1323 | int argc, /* Number of arguments */ |
| 1324 | char **argv /* Text of each argument */ |
| 1325 | ){ |
| 1326 | sqlite3 *db; |
| 1327 | |
| 1328 | if( argc<2 ){ |
| 1329 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 1330 | " DB\"", 0); |
| 1331 | return TCL_ERROR; |
| 1332 | } |
| 1333 | if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; |
| 1334 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 1335 | sqlite3_drop_modules(db, argc>2 ? (const char**)(argv+2) : 0); |
| 1336 | #endif |
| 1337 | return TCL_OK; |
| 1338 | } |
| 1339 | |
| 1340 | /* |
| 1341 | ** Routines to implement the x_count() aggregate function. |
| 1342 | ** |
| 1343 | ** x_count() counts the number of non-null arguments. But there are |
| 1344 | ** some twists for testing purposes. |
| 1345 | ** |
| 1346 | ** If the argument to x_count() is 40 then a UTF-8 error is reported |
| 1347 | ** on the step function. If x_count(41) is seen, then a UTF-16 error |
| 1348 | ** is reported on the step function. If the total count is 42, then |
| 1349 | ** a UTF-8 error is reported on the finalize function. |
| 1350 | */ |
| 1351 | typedef struct t1CountCtx t1CountCtx; |
| 1352 | struct t1CountCtx { |
| 1353 | int n; |
| 1354 | }; |
| 1355 | static void t1CountStep( |
| 1356 | sqlite3_context *context, |
| 1357 | int argc, |
| 1358 | sqlite3_value **argv |
| 1359 | ){ |
| 1360 | t1CountCtx *p; |
| 1361 | p = sqlite3_aggregate_context(context, sizeof(*p)); |
| 1362 | if( (argc==0 || SQLITE_NULL!=sqlite3_value_type(argv[0]) ) && p ){ |
| 1363 | p->n++; |
| 1364 | } |
| 1365 | if( argc>0 ){ |
| 1366 | int v = sqlite3_value_int(argv[0]); |
| 1367 | if( v==40 ){ |
| 1368 | sqlite3_result_error(context, "value of 40 handed to x_count", -1); |
| 1369 | #ifndef SQLITE_OMIT_UTF16 |
| 1370 | }else if( v==41 ){ |
| 1371 | const char zUtf16ErrMsg[] = { 0, 0x61, 0, 0x62, 0, 0x63, 0, 0, 0}; |
| 1372 | sqlite3_result_error16(context, &zUtf16ErrMsg[1-SQLITE_BIGENDIAN], -1); |
| 1373 | #endif |
| 1374 | } |
| 1375 | } |
| 1376 | } |
| 1377 | static void t1CountFinalize(sqlite3_context *context){ |
| 1378 | t1CountCtx *p; |
| 1379 | p = sqlite3_aggregate_context(context, sizeof(*p)); |
| 1380 | if( p ){ |
| 1381 | if( p->n==42 ){ |
| 1382 | sqlite3_result_error(context, "x_count totals to 42", -1); |
| 1383 | }else{ |
| 1384 | sqlite3_result_int(context, p ? p->n : 0); |
| 1385 | } |
| 1386 | } |
| 1387 | } |
| 1388 | |
| 1389 | #ifndef SQLITE_OMIT_DEPRECATED |
| 1390 | static void legacyCountStep( |
| 1391 | sqlite3_context *context, |
| 1392 | int argc, |
| 1393 | sqlite3_value **argv |
| 1394 | ){ |
| 1395 | /* no-op */ |
| 1396 | } |
| 1397 | |
| 1398 | static void legacyCountFinalize(sqlite3_context *context){ |
| 1399 | sqlite3_result_int(context, sqlite3_aggregate_count(context)); |
| 1400 | } |
| 1401 | #endif |
| 1402 | |
| 1403 | /* |
| 1404 | ** Usage: sqlite3_create_aggregate DB |
| 1405 | ** |
| 1406 | ** Call the sqlite3_create_function API on the given database in order |
| 1407 | ** to create a function named "x_count". This function is similar |
| 1408 | ** to the built-in count() function, with a few special quirks |
| 1409 | ** for testing the sqlite3_result_error() APIs. |
| 1410 | ** |
| 1411 | ** The original motivation for this routine was to be able to call the |
| 1412 | ** sqlite3_create_aggregate function while a query is in progress in order |
| 1413 | ** to test the SQLITE_MISUSE detection logic. See misuse.test. |
| 1414 | ** |
| 1415 | ** This routine was later extended to test the use of sqlite3_result_error() |
| 1416 | ** within aggregate functions. |
| 1417 | ** |
| 1418 | ** Later: It is now also extended to register the aggregate function |
| 1419 | ** "legacy_count()" with the supplied database handle. This is used |
| 1420 | ** to test the deprecated sqlite3_aggregate_count() API. |
| 1421 | */ |
| 1422 | static int SQLITE_TCLAPI test_create_aggregate( |
| 1423 | void *NotUsed, |
| 1424 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 1425 | int argc, /* Number of arguments */ |
| 1426 | char **argv /* Text of each argument */ |
| 1427 | ){ |
| 1428 | sqlite3 *db; |
| 1429 | int rc; |
| 1430 | if( argc!=2 ){ |
| 1431 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 1432 | " FILENAME\"", 0); |
| 1433 | return TCL_ERROR; |
| 1434 | } |
| 1435 | if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; |
| 1436 | rc = sqlite3_create_function(db, "x_count", 0, SQLITE_UTF8, 0, 0, |
| 1437 | t1CountStep,t1CountFinalize); |
| 1438 | if( rc==SQLITE_OK ){ |
| 1439 | rc = sqlite3_create_function(db, "x_count", 1, SQLITE_UTF8, 0, 0, |
| 1440 | t1CountStep,t1CountFinalize); |
| 1441 | } |
| 1442 | #ifndef SQLITE_OMIT_DEPRECATED |
| 1443 | if( rc==SQLITE_OK ){ |
| 1444 | rc = sqlite3_create_function(db, "legacy_count", 0, SQLITE_ANY, 0, 0, |
| 1445 | legacyCountStep, legacyCountFinalize |
| 1446 | ); |
| 1447 | } |
| 1448 | #endif |
| 1449 | if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; |
| 1450 | Tcl_SetResult(interp, (char *)t1ErrorName(rc), 0); |
| 1451 | return TCL_OK; |
| 1452 | } |
| 1453 | |
| 1454 | |
| 1455 | /* |
| 1456 | ** Usage: printf TEXT |
| 1457 | ** |
| 1458 | ** Send output to printf. Use this rather than puts to merge the output |
| 1459 | ** in the correct sequence with debugging printfs inserted into C code. |
| 1460 | ** Puts uses a separate buffer and debugging statements will be out of |
| 1461 | ** sequence if it is used. |
| 1462 | */ |
| 1463 | static int SQLITE_TCLAPI test_printf( |
| 1464 | void *NotUsed, |
| 1465 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 1466 | int argc, /* Number of arguments */ |
| 1467 | char **argv /* Text of each argument */ |
| 1468 | ){ |
| 1469 | if( argc!=2 ){ |
| 1470 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 1471 | " TEXT\"", 0); |
| 1472 | return TCL_ERROR; |
| 1473 | } |
| 1474 | printf("%s\n", argv[1]); |
| 1475 | return TCL_OK; |
| 1476 | } |
| 1477 | |
| 1478 | |
| 1479 | |
| 1480 | /* |
| 1481 | ** Usage: sqlite3_mprintf_int FORMAT INTEGER INTEGER INTEGER |
| 1482 | ** |
| 1483 | ** Call mprintf with three integer arguments |
| 1484 | */ |
| 1485 | static int SQLITE_TCLAPI sqlite3_mprintf_int( |
| 1486 | void *NotUsed, |
| 1487 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 1488 | int argc, /* Number of arguments */ |
| 1489 | char **argv /* Text of each argument */ |
| 1490 | ){ |
| 1491 | int a[3], i; |
| 1492 | char *z; |
| 1493 | if( argc!=5 ){ |
| 1494 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 1495 | " FORMAT INT INT INT\"", 0); |
| 1496 | return TCL_ERROR; |
| 1497 | } |
| 1498 | for(i=2; i<5; i++){ |
| 1499 | if( Tcl_GetInt(interp, argv[i], &a[i-2]) ) return TCL_ERROR; |
| 1500 | } |
| 1501 | z = sqlite3_mprintf(argv[1], a[0], a[1], a[2]); |
| 1502 | Tcl_AppendResult(interp, z, NULL); |
| 1503 | sqlite3_free(z); |
| 1504 | return TCL_OK; |
| 1505 | } |
| 1506 | |
| 1507 | /* |
| 1508 | ** Usage: sqlite3_mprintf_int64 FORMAT INTEGER INTEGER INTEGER |
| 1509 | ** |
| 1510 | ** Call mprintf with three 64-bit integer arguments |
| 1511 | */ |
| 1512 | static int SQLITE_TCLAPI sqlite3_mprintf_int64( |
| 1513 | void *NotUsed, |
| 1514 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 1515 | int argc, /* Number of arguments */ |
| 1516 | char **argv /* Text of each argument */ |
| 1517 | ){ |
| 1518 | int i; |
| 1519 | sqlite_int64 a[3]; |
| 1520 | char *z; |
| 1521 | if( argc!=5 ){ |
| 1522 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 1523 | " FORMAT INT INT INT\"", 0); |
| 1524 | return TCL_ERROR; |
| 1525 | } |
| 1526 | for(i=2; i<5; i++){ |
| 1527 | if( sqlite3Atoi64(argv[i], &a[i-2], sqlite3Strlen30(argv[i]), SQLITE_UTF8) ){ |
| 1528 | Tcl_AppendResult(interp, "argument is not a valid 64-bit integer", NULL); |
| 1529 | return TCL_ERROR; |
| 1530 | } |
| 1531 | } |
| 1532 | z = sqlite3_mprintf(argv[1], a[0], a[1], a[2]); |
| 1533 | Tcl_AppendResult(interp, z, NULL); |
| 1534 | sqlite3_free(z); |
| 1535 | return TCL_OK; |
| 1536 | } |
| 1537 | |
| 1538 | /* |
| 1539 | ** Usage: sqlite3_mprintf_long FORMAT INTEGER INTEGER INTEGER |
| 1540 | ** |
| 1541 | ** Call mprintf with three long integer arguments. This might be the |
| 1542 | ** same as sqlite3_mprintf_int or sqlite3_mprintf_int64, depending on |
| 1543 | ** platform. |
| 1544 | */ |
| 1545 | static int SQLITE_TCLAPI sqlite3_mprintf_long( |
| 1546 | void *NotUsed, |
| 1547 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 1548 | int argc, /* Number of arguments */ |
| 1549 | char **argv /* Text of each argument */ |
| 1550 | ){ |
| 1551 | int i; |
| 1552 | long int a[3]; |
| 1553 | int b[3]; |
| 1554 | char *z; |
| 1555 | if( argc!=5 ){ |
| 1556 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 1557 | " FORMAT INT INT INT\"", 0); |
| 1558 | return TCL_ERROR; |
| 1559 | } |
| 1560 | for(i=2; i<5; i++){ |
| 1561 | if( Tcl_GetInt(interp, argv[i], &b[i-2]) ) return TCL_ERROR; |
| 1562 | a[i-2] = (long int)b[i-2]; |
| 1563 | a[i-2] &= (((u64)1)<<(sizeof(int)*8))-1; |
| 1564 | } |
| 1565 | z = sqlite3_mprintf(argv[1], a[0], a[1], a[2]); |
| 1566 | Tcl_AppendResult(interp, z, NULL); |
| 1567 | sqlite3_free(z); |
| 1568 | return TCL_OK; |
| 1569 | } |
| 1570 | |
| 1571 | /* |
| 1572 | ** Usage: sqlite3_mprintf_str FORMAT INTEGER INTEGER STRING |
| 1573 | ** |
| 1574 | ** Call mprintf with two integer arguments and one string argument |
| 1575 | */ |
| 1576 | static int SQLITE_TCLAPI sqlite3_mprintf_str( |
| 1577 | void *NotUsed, |
| 1578 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 1579 | int argc, /* Number of arguments */ |
| 1580 | char **argv /* Text of each argument */ |
| 1581 | ){ |
| 1582 | int a[3], i; |
| 1583 | char *z; |
| 1584 | if( argc<4 || argc>5 ){ |
| 1585 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 1586 | " FORMAT INT INT ?STRING?\"", 0); |
| 1587 | return TCL_ERROR; |
| 1588 | } |
| 1589 | for(i=2; i<4; i++){ |
| 1590 | if( Tcl_GetInt(interp, argv[i], &a[i-2]) ) return TCL_ERROR; |
| 1591 | } |
| 1592 | z = sqlite3_mprintf(argv[1], a[0], a[1], argc>4 ? argv[4] : NULL); |
| 1593 | Tcl_AppendResult(interp, z, NULL); |
| 1594 | sqlite3_free(z); |
| 1595 | return TCL_OK; |
| 1596 | } |
| 1597 | |
| 1598 | /* |
| 1599 | ** Usage: sqlite3_snprintf_str INTEGER FORMAT INTEGER INTEGER STRING |
| 1600 | ** |
| 1601 | ** Call mprintf with two integer arguments and one string argument |
| 1602 | */ |
| 1603 | static int SQLITE_TCLAPI sqlite3_snprintf_str( |
| 1604 | void *NotUsed, |
| 1605 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 1606 | int argc, /* Number of arguments */ |
| 1607 | char **argv /* Text of each argument */ |
| 1608 | ){ |
| 1609 | int a[3], i; |
| 1610 | int n; |
| 1611 | char *z; |
| 1612 | if( argc<5 || argc>6 ){ |
| 1613 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 1614 | " INT FORMAT INT INT ?STRING?\"", 0); |
| 1615 | return TCL_ERROR; |
| 1616 | } |
| 1617 | if( Tcl_GetInt(interp, argv[1], &n) ) return TCL_ERROR; |
| 1618 | if( n<0 ){ |
| 1619 | Tcl_AppendResult(interp, "N must be non-negative", NULL); |
| 1620 | return TCL_ERROR; |
| 1621 | } |
| 1622 | for(i=3; i<5; i++){ |
| 1623 | if( Tcl_GetInt(interp, argv[i], &a[i-3]) ) return TCL_ERROR; |
| 1624 | } |
| 1625 | z = sqlite3_malloc( n+1 ); |
| 1626 | sqlite3_snprintf(n, z, argv[2], a[0], a[1], argc>4 ? argv[5] : NULL); |
| 1627 | Tcl_AppendResult(interp, z, NULL); |
| 1628 | sqlite3_free(z); |
| 1629 | return TCL_OK; |
| 1630 | } |
| 1631 | |
| 1632 | /* |
| 1633 | ** Usage: sqlite3_mprintf_double FORMAT INTEGER INTEGER DOUBLE |
| 1634 | ** |
| 1635 | ** Call mprintf with two integer arguments and one double argument |
| 1636 | */ |
| 1637 | static int SQLITE_TCLAPI sqlite3_mprintf_double( |
| 1638 | void *NotUsed, |
| 1639 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 1640 | int argc, /* Number of arguments */ |
| 1641 | char **argv /* Text of each argument */ |
| 1642 | ){ |
| 1643 | int a[3], i; |
| 1644 | double r; |
| 1645 | char *z; |
| 1646 | if( argc!=5 ){ |
| 1647 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 1648 | " FORMAT INT INT DOUBLE\"", 0); |
| 1649 | return TCL_ERROR; |
| 1650 | } |
| 1651 | for(i=2; i<4; i++){ |
| 1652 | if( Tcl_GetInt(interp, argv[i], &a[i-2]) ) return TCL_ERROR; |
| 1653 | } |
| 1654 | if( Tcl_GetDouble(interp, argv[4], &r) ) return TCL_ERROR; |
| 1655 | z = sqlite3_mprintf(argv[1], a[0], a[1], r); |
| 1656 | Tcl_AppendResult(interp, z, NULL); |
| 1657 | sqlite3_free(z); |
| 1658 | return TCL_OK; |
| 1659 | } |
| 1660 | |
| 1661 | /* |
| 1662 | ** Usage: sqlite3_mprintf_scaled FORMAT DOUBLE DOUBLE |
| 1663 | ** |
| 1664 | ** Call mprintf with a single double argument which is the product of the |
| 1665 | ** two arguments given above. This is used to generate overflow and underflow |
| 1666 | ** doubles to test that they are converted properly. |
| 1667 | */ |
| 1668 | static int SQLITE_TCLAPI sqlite3_mprintf_scaled( |
| 1669 | void *NotUsed, |
| 1670 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 1671 | int argc, /* Number of arguments */ |
| 1672 | char **argv /* Text of each argument */ |
| 1673 | ){ |
| 1674 | int i; |
| 1675 | double r[2]; |
| 1676 | char *z; |
| 1677 | if( argc!=4 ){ |
| 1678 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 1679 | " FORMAT DOUBLE DOUBLE\"", 0); |
| 1680 | return TCL_ERROR; |
| 1681 | } |
| 1682 | for(i=2; i<4; i++){ |
| 1683 | if( Tcl_GetDouble(interp, argv[i], &r[i-2]) ) return TCL_ERROR; |
| 1684 | } |
| 1685 | z = sqlite3_mprintf(argv[1], r[0]*r[1]); |
| 1686 | Tcl_AppendResult(interp, z, NULL); |
| 1687 | sqlite3_free(z); |
| 1688 | return TCL_OK; |
| 1689 | } |
| 1690 | |
| 1691 | /* |
| 1692 | ** Usage: sqlite3_mprintf_stronly FORMAT STRING |
| 1693 | ** |
| 1694 | ** Call mprintf with a single double argument which is the product of the |
| 1695 | ** two arguments given above. This is used to generate overflow and underflow |
| 1696 | ** doubles to test that they are converted properly. |
| 1697 | */ |
| 1698 | static int SQLITE_TCLAPI sqlite3_mprintf_stronly( |
| 1699 | void *NotUsed, |
| 1700 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 1701 | int argc, /* Number of arguments */ |
| 1702 | char **argv /* Text of each argument */ |
| 1703 | ){ |
| 1704 | char *z; |
| 1705 | if( argc!=3 ){ |
| 1706 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 1707 | " FORMAT STRING\"", 0); |
| 1708 | return TCL_ERROR; |
| 1709 | } |
| 1710 | z = sqlite3_mprintf(argv[1], argv[2]); |
| 1711 | Tcl_AppendResult(interp, z, NULL); |
| 1712 | sqlite3_free(z); |
| 1713 | return TCL_OK; |
| 1714 | } |
| 1715 | |
| 1716 | /* |
| 1717 | ** Usage: sqlite3_mprintf_hexdouble FORMAT HEX |
| 1718 | ** |
| 1719 | ** Call mprintf with a single double argument which is derived from the |
| 1720 | ** hexadecimal encoding of an IEEE double. |
| 1721 | */ |
| 1722 | static int SQLITE_TCLAPI sqlite3_mprintf_hexdouble( |
| 1723 | void *NotUsed, |
| 1724 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 1725 | int argc, /* Number of arguments */ |
| 1726 | char **argv /* Text of each argument */ |
| 1727 | ){ |
| 1728 | char *z; |
| 1729 | double r; |
| 1730 | unsigned int x1, x2; |
| 1731 | sqlite_uint64 d; |
| 1732 | if( argc!=3 ){ |
| 1733 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 1734 | " FORMAT STRING\"", 0); |
| 1735 | return TCL_ERROR; |
| 1736 | } |
| 1737 | if( sscanf(argv[2], "%08x%08x", &x2, &x1)!=2 ){ |
| 1738 | Tcl_AppendResult(interp, "2nd argument should be 16-characters of hex", NULL); |
| 1739 | return TCL_ERROR; |
| 1740 | } |
| 1741 | d = x2; |
| 1742 | d = (d<<32) + x1; |
| 1743 | memcpy(&r, &d, sizeof(r)); |
| 1744 | z = sqlite3_mprintf(argv[1], r); |
| 1745 | Tcl_AppendResult(interp, z, NULL); |
| 1746 | sqlite3_free(z); |
| 1747 | return TCL_OK; |
| 1748 | } |
| 1749 | |
| 1750 | /* |
| 1751 | ** Usage: sqlite3_enable_shared_cache ?BOOLEAN? |
| 1752 | ** |
| 1753 | */ |
| 1754 | #if !defined(SQLITE_OMIT_SHARED_CACHE) |
| 1755 | static int SQLITE_TCLAPI test_enable_shared( |
| 1756 | ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ |
| 1757 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 1758 | int objc, /* Number of arguments */ |
| 1759 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 1760 | ){ |
| 1761 | int rc; |
| 1762 | int enable; |
| 1763 | int ret = 0; |
| 1764 | |
| 1765 | if( objc!=2 && objc!=1 ){ |
| 1766 | Tcl_WrongNumArgs(interp, 1, objv, "?BOOLEAN?"); |
| 1767 | return TCL_ERROR; |
| 1768 | } |
| 1769 | ret = sqlite3GlobalConfig.sharedCacheEnabled; |
| 1770 | |
| 1771 | if( objc==2 ){ |
| 1772 | if( Tcl_GetBooleanFromObj(interp, objv[1], &enable) ){ |
| 1773 | return TCL_ERROR; |
| 1774 | } |
| 1775 | rc = sqlite3_enable_shared_cache(enable); |
| 1776 | if( rc!=SQLITE_OK ){ |
| 1777 | Tcl_SetResult(interp, (char *)sqlite3ErrStr(rc), TCL_STATIC); |
| 1778 | return TCL_ERROR; |
| 1779 | } |
| 1780 | } |
| 1781 | Tcl_SetObjResult(interp, Tcl_NewBooleanObj(ret)); |
| 1782 | return TCL_OK; |
| 1783 | } |
| 1784 | #endif |
| 1785 | |
| 1786 | |
| 1787 | |
| 1788 | /* |
| 1789 | ** Usage: sqlite3_extended_result_codes DB BOOLEAN |
| 1790 | ** |
| 1791 | */ |
| 1792 | static int SQLITE_TCLAPI test_extended_result_codes( |
| 1793 | ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ |
| 1794 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 1795 | int objc, /* Number of arguments */ |
| 1796 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 1797 | ){ |
| 1798 | int enable; |
| 1799 | sqlite3 *db; |
| 1800 | |
| 1801 | if( objc!=3 ){ |
| 1802 | Tcl_WrongNumArgs(interp, 1, objv, "DB BOOLEAN"); |
| 1803 | return TCL_ERROR; |
| 1804 | } |
| 1805 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 1806 | if( Tcl_GetBooleanFromObj(interp, objv[2], &enable) ) return TCL_ERROR; |
| 1807 | sqlite3_extended_result_codes(db, enable); |
| 1808 | return TCL_OK; |
| 1809 | } |
| 1810 | |
| 1811 | /* |
| 1812 | ** Usage: sqlite3_libversion_number |
| 1813 | ** |
| 1814 | */ |
| 1815 | static int SQLITE_TCLAPI test_libversion_number( |
| 1816 | ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ |
| 1817 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 1818 | int objc, /* Number of arguments */ |
| 1819 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 1820 | ){ |
| 1821 | Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_libversion_number())); |
| 1822 | return TCL_OK; |
| 1823 | } |
| 1824 | |
| 1825 | /* |
| 1826 | ** Usage: sqlite3_table_column_metadata DB dbname tblname colname |
| 1827 | ** |
| 1828 | */ |
| 1829 | static int SQLITE_TCLAPI test_table_column_metadata( |
| 1830 | ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ |
| 1831 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 1832 | int objc, /* Number of arguments */ |
| 1833 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 1834 | ){ |
| 1835 | sqlite3 *db; |
| 1836 | const char *zDb; |
| 1837 | const char *zTbl; |
| 1838 | const char *zCol; |
| 1839 | int rc; |
| 1840 | Tcl_Obj *pRet; |
| 1841 | |
| 1842 | const char *zDatatype; |
| 1843 | const char *zCollseq; |
| 1844 | int notnull; |
| 1845 | int primarykey; |
| 1846 | int autoincrement; |
| 1847 | |
| 1848 | if( objc!=5 && objc!=4 ){ |
| 1849 | Tcl_WrongNumArgs(interp, 1, objv, "DB dbname tblname colname"); |
| 1850 | return TCL_ERROR; |
| 1851 | } |
| 1852 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 1853 | zDb = Tcl_GetString(objv[2]); |
| 1854 | zTbl = Tcl_GetString(objv[3]); |
| 1855 | zCol = objc==5 ? Tcl_GetString(objv[4]) : 0; |
| 1856 | |
| 1857 | if( strlen(zDb)==0 ) zDb = 0; |
| 1858 | |
| 1859 | rc = sqlite3_table_column_metadata(db, zDb, zTbl, zCol, |
| 1860 | &zDatatype, &zCollseq, ¬null, &primarykey, &autoincrement); |
| 1861 | |
| 1862 | if( rc!=SQLITE_OK ){ |
| 1863 | Tcl_AppendResult(interp, sqlite3_errmsg(db), NULL); |
| 1864 | return TCL_ERROR; |
| 1865 | } |
| 1866 | |
| 1867 | pRet = Tcl_NewObj(); |
| 1868 | Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zDatatype, -1)); |
| 1869 | Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zCollseq, -1)); |
| 1870 | Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(notnull)); |
| 1871 | Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(primarykey)); |
| 1872 | Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(autoincrement)); |
| 1873 | Tcl_SetObjResult(interp, pRet); |
| 1874 | |
| 1875 | return TCL_OK; |
| 1876 | } |
| 1877 | |
| 1878 | #ifndef SQLITE_OMIT_INCRBLOB |
| 1879 | |
| 1880 | static int SQLITE_TCLAPI blobHandleFromObj( |
| 1881 | Tcl_Interp *interp, |
| 1882 | Tcl_Obj *pObj, |
| 1883 | sqlite3_blob **ppBlob |
| 1884 | ){ |
| 1885 | char *z; |
| 1886 | Tcl_Size n; |
| 1887 | |
| 1888 | z = Tcl_GetStringFromObj(pObj, &n); |
| 1889 | if( n==0 ){ |
| 1890 | *ppBlob = 0; |
| 1891 | }else{ |
| 1892 | int notUsed; |
| 1893 | Tcl_Channel channel; |
| 1894 | ClientData instanceData; |
| 1895 | |
| 1896 | channel = Tcl_GetChannel(interp, z, ¬Used); |
| 1897 | if( !channel ) return TCL_ERROR; |
| 1898 | |
| 1899 | Tcl_Flush(channel); |
| 1900 | Tcl_Seek(channel, 0, SEEK_SET); |
| 1901 | |
| 1902 | instanceData = Tcl_GetChannelInstanceData(channel); |
| 1903 | *ppBlob = *((sqlite3_blob **)instanceData); |
| 1904 | } |
| 1905 | |
| 1906 | return TCL_OK; |
| 1907 | } |
| 1908 | |
| 1909 | static int SQLITE_TCLAPI test_blob_reopen( |
| 1910 | ClientData clientData, /* Not used */ |
| 1911 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 1912 | int objc, /* Number of arguments */ |
| 1913 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 1914 | ){ |
| 1915 | Tcl_WideInt iRowid; |
| 1916 | sqlite3_blob *pBlob; |
| 1917 | int rc; |
| 1918 | |
| 1919 | if( objc!=3 ){ |
| 1920 | Tcl_WrongNumArgs(interp, 1, objv, "CHANNEL ROWID"); |
| 1921 | return TCL_ERROR; |
| 1922 | } |
| 1923 | |
| 1924 | if( blobHandleFromObj(interp, objv[1], &pBlob) ) return TCL_ERROR; |
| 1925 | if( Tcl_GetWideIntFromObj(interp, objv[2], &iRowid) ) return TCL_ERROR; |
| 1926 | |
| 1927 | rc = sqlite3_blob_reopen(pBlob, iRowid); |
| 1928 | if( rc!=SQLITE_OK ){ |
| 1929 | Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_VOLATILE); |
| 1930 | } |
| 1931 | |
| 1932 | return (rc==SQLITE_OK ? TCL_OK : TCL_ERROR); |
| 1933 | } |
| 1934 | |
| 1935 | #endif |
| 1936 | |
| 1937 | /* |
| 1938 | ** Usage: sqlite3_create_collation_v2 DB-HANDLE NAME CMP-PROC DEL-PROC |
| 1939 | ** |
| 1940 | ** This Tcl proc is used for testing the experimental |
| 1941 | ** sqlite3_create_collation_v2() interface. |
| 1942 | */ |
| 1943 | struct TestCollationX { |
| 1944 | Tcl_Interp *interp; |
| 1945 | Tcl_Obj *pCmp; |
| 1946 | Tcl_Obj *pDel; |
| 1947 | }; |
| 1948 | typedef struct TestCollationX TestCollationX; |
| 1949 | static void testCreateCollationDel(void *pCtx){ |
| 1950 | TestCollationX *p = (TestCollationX *)pCtx; |
| 1951 | |
| 1952 | int rc = Tcl_EvalObjEx(p->interp, p->pDel, TCL_EVAL_DIRECT|TCL_EVAL_GLOBAL); |
| 1953 | if( rc!=TCL_OK ){ |
| 1954 | Tcl_BackgroundError(p->interp); |
| 1955 | } |
| 1956 | |
| 1957 | Tcl_DecrRefCount(p->pCmp); |
| 1958 | Tcl_DecrRefCount(p->pDel); |
| 1959 | sqlite3_free((void *)p); |
| 1960 | } |
| 1961 | static int testCreateCollationCmp( |
| 1962 | void *pCtx, |
| 1963 | int nLeft, |
| 1964 | const void *zLeft, |
| 1965 | int nRight, |
| 1966 | const void *zRight |
| 1967 | ){ |
| 1968 | TestCollationX *p = (TestCollationX *)pCtx; |
| 1969 | Tcl_Obj *pScript = Tcl_DuplicateObj(p->pCmp); |
| 1970 | int iRes = 0; |
| 1971 | |
| 1972 | Tcl_IncrRefCount(pScript); |
| 1973 | Tcl_ListObjAppendElement(0, pScript, Tcl_NewStringObj((char *)zLeft, nLeft)); |
| 1974 | Tcl_ListObjAppendElement(0, pScript, Tcl_NewStringObj((char *)zRight,nRight)); |
| 1975 | |
| 1976 | if( TCL_OK!=Tcl_EvalObjEx(p->interp, pScript, TCL_EVAL_DIRECT|TCL_EVAL_GLOBAL) |
| 1977 | || TCL_OK!=Tcl_GetIntFromObj(p->interp, Tcl_GetObjResult(p->interp), &iRes) |
| 1978 | ){ |
| 1979 | Tcl_BackgroundError(p->interp); |
| 1980 | } |
| 1981 | Tcl_DecrRefCount(pScript); |
| 1982 | |
| 1983 | return iRes; |
| 1984 | } |
| 1985 | static int SQLITE_TCLAPI test_create_collation_v2( |
| 1986 | ClientData clientData, /* Not used */ |
| 1987 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 1988 | int objc, /* Number of arguments */ |
| 1989 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 1990 | ){ |
| 1991 | TestCollationX *p; |
| 1992 | sqlite3 *db; |
| 1993 | int rc; |
| 1994 | |
| 1995 | if( objc!=5 ){ |
| 1996 | Tcl_WrongNumArgs(interp, 1, objv, "DB-HANDLE NAME CMP-PROC DEL-PROC"); |
| 1997 | return TCL_ERROR; |
| 1998 | } |
| 1999 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 2000 | |
| 2001 | p = (TestCollationX *)sqlite3_malloc(sizeof(TestCollationX)); |
| 2002 | p->pCmp = objv[3]; |
| 2003 | p->pDel = objv[4]; |
| 2004 | p->interp = interp; |
| 2005 | Tcl_IncrRefCount(p->pCmp); |
| 2006 | Tcl_IncrRefCount(p->pDel); |
| 2007 | |
| 2008 | rc = sqlite3_create_collation_v2(db, Tcl_GetString(objv[2]), 16, |
| 2009 | (void *)p, testCreateCollationCmp, testCreateCollationDel |
| 2010 | ); |
| 2011 | if( rc!=SQLITE_MISUSE ){ |
| 2012 | Tcl_AppendResult(interp, "sqlite3_create_collate_v2() failed to detect " |
| 2013 | "an invalid encoding", (char*)0); |
| 2014 | return TCL_ERROR; |
| 2015 | } |
| 2016 | rc = sqlite3_create_collation_v2(db, Tcl_GetString(objv[2]), SQLITE_UTF8, |
| 2017 | (void *)p, testCreateCollationCmp, testCreateCollationDel |
| 2018 | ); |
| 2019 | return TCL_OK; |
| 2020 | } |
| 2021 | |
| 2022 | /* |
| 2023 | ** USAGE: sqlite3_create_function_v2 DB NAME NARG ENC ?SWITCHES? |
| 2024 | ** |
| 2025 | ** Available switches are: |
| 2026 | ** |
| 2027 | ** -func SCRIPT |
| 2028 | ** -step SCRIPT |
| 2029 | ** -final SCRIPT |
| 2030 | ** -destroy SCRIPT |
| 2031 | */ |
| 2032 | typedef struct CreateFunctionV2 CreateFunctionV2; |
| 2033 | struct CreateFunctionV2 { |
| 2034 | Tcl_Interp *interp; |
| 2035 | Tcl_Obj *pFunc; /* Script for function invocation */ |
| 2036 | Tcl_Obj *pStep; /* Script for agg. step invocation */ |
| 2037 | Tcl_Obj *pFinal; /* Script for agg. finalization invocation */ |
| 2038 | Tcl_Obj *pDestroy; /* Destructor script */ |
| 2039 | }; |
| 2040 | static void cf2Func(sqlite3_context *ctx, int nArg, sqlite3_value **aArg){ |
| 2041 | } |
| 2042 | static void cf2Step(sqlite3_context *ctx, int nArg, sqlite3_value **aArg){ |
| 2043 | } |
| 2044 | static void cf2Final(sqlite3_context *ctx){ |
| 2045 | } |
| 2046 | static void cf2Destroy(void *pUser){ |
| 2047 | CreateFunctionV2 *p = (CreateFunctionV2 *)pUser; |
| 2048 | |
| 2049 | if( p->interp && p->pDestroy ){ |
| 2050 | int rc = Tcl_EvalObjEx(p->interp, p->pDestroy, 0); |
| 2051 | if( rc!=TCL_OK ) Tcl_BackgroundError(p->interp); |
| 2052 | } |
| 2053 | |
| 2054 | if( p->pFunc ) Tcl_DecrRefCount(p->pFunc); |
| 2055 | if( p->pStep ) Tcl_DecrRefCount(p->pStep); |
| 2056 | if( p->pFinal ) Tcl_DecrRefCount(p->pFinal); |
| 2057 | if( p->pDestroy ) Tcl_DecrRefCount(p->pDestroy); |
| 2058 | sqlite3_free(p); |
| 2059 | } |
| 2060 | static int SQLITE_TCLAPI test_create_function_v2( |
| 2061 | ClientData clientData, /* Not used */ |
| 2062 | Tcl_Interp *interp, /* The invoking TCL interpreter */ |
| 2063 | int objc, /* Number of arguments */ |
| 2064 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 2065 | ){ |
| 2066 | sqlite3 *db; |
| 2067 | const char *zFunc; |
| 2068 | int nArg; |
| 2069 | int enc; |
| 2070 | CreateFunctionV2 *p; |
| 2071 | int i; |
| 2072 | int rc; |
| 2073 | |
| 2074 | struct EncTable { |
| 2075 | const char *zEnc; |
| 2076 | int enc; |
| 2077 | } aEnc[] = { |
| 2078 | {"utf8", SQLITE_UTF8 }, |
| 2079 | {"utf16", SQLITE_UTF16 }, |
| 2080 | {"utf16le", SQLITE_UTF16LE }, |
| 2081 | {"utf16be", SQLITE_UTF16BE }, |
| 2082 | {"any", SQLITE_ANY }, |
| 2083 | {"0", 0 }, |
| 2084 | {0, 0 } |
| 2085 | }; |
| 2086 | |
| 2087 | if( objc<5 || (objc%2)==0 ){ |
| 2088 | Tcl_WrongNumArgs(interp, 1, objv, "DB NAME NARG ENC SWITCHES..."); |
| 2089 | return TCL_ERROR; |
| 2090 | } |
| 2091 | |
| 2092 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 2093 | zFunc = Tcl_GetString(objv[2]); |
| 2094 | if( Tcl_GetIntFromObj(interp, objv[3], &nArg) ) return TCL_ERROR; |
| 2095 | if( Tcl_GetIndexFromObjStruct(interp, objv[4], aEnc, sizeof(aEnc[0]), |
| 2096 | "encoding", 0, &enc) |
| 2097 | ){ |
| 2098 | return TCL_ERROR; |
| 2099 | } |
| 2100 | enc = aEnc[enc].enc; |
| 2101 | |
| 2102 | p = sqlite3_malloc(sizeof(CreateFunctionV2)); |
| 2103 | assert( p ); |
| 2104 | memset(p, 0, sizeof(CreateFunctionV2)); |
| 2105 | p->interp = interp; |
| 2106 | |
| 2107 | for(i=5; i<objc; i+=2){ |
| 2108 | int iSwitch; |
| 2109 | const char *azSwitch[] = {"-func", "-step", "-final", "-destroy", 0}; |
| 2110 | if( Tcl_GetIndexFromObj(interp, objv[i], azSwitch, "switch", 0, &iSwitch) ){ |
| 2111 | sqlite3_free(p); |
| 2112 | return TCL_ERROR; |
| 2113 | } |
| 2114 | |
| 2115 | switch( iSwitch ){ |
| 2116 | case 0: p->pFunc = objv[i+1]; break; |
| 2117 | case 1: p->pStep = objv[i+1]; break; |
| 2118 | case 2: p->pFinal = objv[i+1]; break; |
| 2119 | case 3: p->pDestroy = objv[i+1]; break; |
| 2120 | } |
| 2121 | } |
| 2122 | if( p->pFunc ) p->pFunc = Tcl_DuplicateObj(p->pFunc); |
| 2123 | if( p->pStep ) p->pStep = Tcl_DuplicateObj(p->pStep); |
| 2124 | if( p->pFinal ) p->pFinal = Tcl_DuplicateObj(p->pFinal); |
| 2125 | if( p->pDestroy ) p->pDestroy = Tcl_DuplicateObj(p->pDestroy); |
| 2126 | |
| 2127 | if( p->pFunc ) Tcl_IncrRefCount(p->pFunc); |
| 2128 | if( p->pStep ) Tcl_IncrRefCount(p->pStep); |
| 2129 | if( p->pFinal ) Tcl_IncrRefCount(p->pFinal); |
| 2130 | if( p->pDestroy ) Tcl_IncrRefCount(p->pDestroy); |
| 2131 | |
| 2132 | rc = sqlite3_create_function_v2(db, zFunc, nArg, enc, (void *)p, |
| 2133 | (p->pFunc ? cf2Func : 0), |
| 2134 | (p->pStep ? cf2Step : 0), |
| 2135 | (p->pFinal ? cf2Final : 0), |
| 2136 | cf2Destroy |
| 2137 | ); |
| 2138 | if( rc!=SQLITE_OK ){ |
| 2139 | Tcl_ResetResult(interp); |
| 2140 | Tcl_AppendResult(interp, sqlite3ErrName(rc), NULL); |
| 2141 | return TCL_ERROR; |
| 2142 | } |
| 2143 | return TCL_OK; |
| 2144 | } |
| 2145 | |
| 2146 | /* |
| 2147 | ** Usage: sqlite3_load_extension DB-HANDLE FILE ?PROC? |
| 2148 | */ |
| 2149 | static int SQLITE_TCLAPI test_load_extension( |
| 2150 | ClientData clientData, /* Not used */ |
| 2151 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 2152 | int objc, /* Number of arguments */ |
| 2153 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 2154 | ){ |
| 2155 | Tcl_CmdInfo cmdInfo; |
| 2156 | sqlite3 *db; |
| 2157 | int rc; |
| 2158 | char *zDb; |
| 2159 | char *zFile; |
| 2160 | char *zProc = 0; |
| 2161 | char *zErr = 0; |
| 2162 | |
| 2163 | if( objc!=4 && objc!=3 ){ |
| 2164 | Tcl_WrongNumArgs(interp, 1, objv, "DB-HANDLE FILE ?PROC?"); |
| 2165 | return TCL_ERROR; |
| 2166 | } |
| 2167 | zDb = Tcl_GetString(objv[1]); |
| 2168 | zFile = Tcl_GetString(objv[2]); |
| 2169 | if( objc==4 ){ |
| 2170 | zProc = Tcl_GetString(objv[3]); |
| 2171 | } |
| 2172 | |
| 2173 | /* Extract the C database handle from the Tcl command name */ |
| 2174 | if( !Tcl_GetCommandInfo(interp, zDb, &cmdInfo) ){ |
| 2175 | Tcl_AppendResult(interp, "command not found: ", zDb, (char*)0); |
| 2176 | return TCL_ERROR; |
| 2177 | } |
| 2178 | db = ((struct SqliteDb*)cmdInfo.objClientData)->db; |
| 2179 | assert(db); |
| 2180 | |
| 2181 | /* Call the underlying C function. If an error occurs, set rc to |
| 2182 | ** TCL_ERROR and load any error string into the interpreter. If no |
| 2183 | ** error occurs, set rc to TCL_OK. |
| 2184 | */ |
| 2185 | #ifdef SQLITE_OMIT_LOAD_EXTENSION |
| 2186 | rc = SQLITE_ERROR; |
| 2187 | zErr = sqlite3_mprintf("this build omits sqlite3_load_extension()"); |
| 2188 | (void)zProc; |
| 2189 | (void)zFile; |
| 2190 | #else |
| 2191 | rc = sqlite3_load_extension(db, zFile, zProc, &zErr); |
| 2192 | #endif |
| 2193 | if( rc!=SQLITE_OK ){ |
| 2194 | Tcl_SetResult(interp, zErr ? zErr : "", TCL_VOLATILE); |
| 2195 | rc = TCL_ERROR; |
| 2196 | }else{ |
| 2197 | rc = TCL_OK; |
| 2198 | } |
| 2199 | sqlite3_free(zErr); |
| 2200 | |
| 2201 | return rc; |
| 2202 | } |
| 2203 | |
| 2204 | /* |
| 2205 | ** Usage: sqlite3_enable_load_extension DB-HANDLE ONOFF |
| 2206 | */ |
| 2207 | static int SQLITE_TCLAPI test_enable_load( |
| 2208 | ClientData clientData, /* Not used */ |
| 2209 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 2210 | int objc, /* Number of arguments */ |
| 2211 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 2212 | ){ |
| 2213 | Tcl_CmdInfo cmdInfo; |
| 2214 | sqlite3 *db; |
| 2215 | char *zDb; |
| 2216 | int onoff; |
| 2217 | |
| 2218 | if( objc!=3 ){ |
| 2219 | Tcl_WrongNumArgs(interp, 1, objv, "DB-HANDLE ONOFF"); |
| 2220 | return TCL_ERROR; |
| 2221 | } |
| 2222 | zDb = Tcl_GetString(objv[1]); |
| 2223 | |
| 2224 | /* Extract the C database handle from the Tcl command name */ |
| 2225 | if( !Tcl_GetCommandInfo(interp, zDb, &cmdInfo) ){ |
| 2226 | Tcl_AppendResult(interp, "command not found: ", zDb, (char*)0); |
| 2227 | return TCL_ERROR; |
| 2228 | } |
| 2229 | db = ((struct SqliteDb*)cmdInfo.objClientData)->db; |
| 2230 | assert(db); |
| 2231 | |
| 2232 | /* Get the onoff parameter */ |
| 2233 | if( Tcl_GetBooleanFromObj(interp, objv[2], &onoff) ){ |
| 2234 | return TCL_ERROR; |
| 2235 | } |
| 2236 | |
| 2237 | #ifdef SQLITE_OMIT_LOAD_EXTENSION |
| 2238 | Tcl_AppendResult(interp, "this build omits sqlite3_load_extension()"); |
| 2239 | return TCL_ERROR; |
| 2240 | #else |
| 2241 | sqlite3_enable_load_extension(db, onoff); |
| 2242 | return TCL_OK; |
| 2243 | #endif |
| 2244 | } |
| 2245 | |
| 2246 | /* |
| 2247 | ** Usage: sqlite_abort |
| 2248 | ** |
| 2249 | ** Shutdown the process immediately. This is not a clean shutdown. |
| 2250 | ** This command is used to test the recoverability of a database in |
| 2251 | ** the event of a program crash. |
| 2252 | */ |
| 2253 | static int SQLITE_TCLAPI sqlite_abort( |
| 2254 | void *NotUsed, |
| 2255 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 2256 | int argc, /* Number of arguments */ |
| 2257 | char **argv /* Text of each argument */ |
| 2258 | ){ |
| 2259 | #if defined(_MSC_VER) |
| 2260 | /* We do this, otherwise the test will halt with a popup message |
| 2261 | * that we have to click away before the test will continue. |
| 2262 | */ |
| 2263 | _set_abort_behavior( 0, _CALL_REPORTFAULT ); |
| 2264 | #endif |
| 2265 | exit(255); |
| 2266 | assert( interp==0 ); /* This will always fail */ |
| 2267 | return TCL_OK; |
| 2268 | } |
| 2269 | |
| 2270 | /* |
| 2271 | ** The following routine is a user-defined SQL function whose purpose |
| 2272 | ** is to test the sqlite_set_result() API. |
| 2273 | */ |
| 2274 | static void testFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ |
| 2275 | while( argc>=2 ){ |
| 2276 | const char *zArg0 = (char*)sqlite3_value_text(argv[0]); |
| 2277 | if( zArg0 ){ |
| 2278 | if( 0==sqlite3StrICmp(zArg0, "int") ){ |
| 2279 | sqlite3_result_int(context, sqlite3_value_int(argv[1])); |
| 2280 | }else if( sqlite3StrICmp(zArg0,"int64")==0 ){ |
| 2281 | sqlite3_result_int64(context, sqlite3_value_int64(argv[1])); |
| 2282 | }else if( sqlite3StrICmp(zArg0,"string")==0 ){ |
| 2283 | sqlite3_result_text(context, (char*)sqlite3_value_text(argv[1]), -1, |
| 2284 | SQLITE_TRANSIENT); |
| 2285 | }else if( sqlite3StrICmp(zArg0,"double")==0 ){ |
| 2286 | sqlite3_result_double(context, sqlite3_value_double(argv[1])); |
| 2287 | }else if( sqlite3StrICmp(zArg0,"null")==0 ){ |
| 2288 | sqlite3_result_null(context); |
| 2289 | }else if( sqlite3StrICmp(zArg0,"value")==0 ){ |
| 2290 | sqlite3_result_value(context, argv[sqlite3_value_int(argv[1])]); |
| 2291 | }else{ |
| 2292 | goto error_out; |
| 2293 | } |
| 2294 | }else{ |
| 2295 | goto error_out; |
| 2296 | } |
| 2297 | argc -= 2; |
| 2298 | argv += 2; |
| 2299 | } |
| 2300 | return; |
| 2301 | |
| 2302 | error_out: |
| 2303 | sqlite3_result_error(context,"first argument should be one of: " |
| 2304 | "int int64 string double null value", -1); |
| 2305 | } |
| 2306 | |
| 2307 | /* |
| 2308 | ** Usage: sqlite_register_test_function DB NAME |
| 2309 | ** |
| 2310 | ** Register the test SQL function on the database DB under the name NAME. |
| 2311 | */ |
| 2312 | static int SQLITE_TCLAPI test_register_func( |
| 2313 | void *NotUsed, |
| 2314 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 2315 | int argc, /* Number of arguments */ |
| 2316 | char **argv /* Text of each argument */ |
| 2317 | ){ |
| 2318 | sqlite3 *db; |
| 2319 | int rc; |
| 2320 | if( argc!=3 ){ |
| 2321 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 2322 | " DB FUNCTION-NAME", 0); |
| 2323 | return TCL_ERROR; |
| 2324 | } |
| 2325 | if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; |
| 2326 | rc = sqlite3_create_function(db, argv[2], -1, SQLITE_UTF8, 0, |
| 2327 | testFunc, 0, 0); |
| 2328 | if( rc!=0 ){ |
| 2329 | Tcl_AppendResult(interp, sqlite3ErrStr(rc), NULL); |
| 2330 | return TCL_ERROR; |
| 2331 | } |
| 2332 | if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; |
| 2333 | return TCL_OK; |
| 2334 | } |
| 2335 | |
| 2336 | /* |
| 2337 | ** Usage: sqlite3_finalize STMT |
| 2338 | ** |
| 2339 | ** Finalize a statement handle. |
| 2340 | */ |
| 2341 | static int SQLITE_TCLAPI test_finalize( |
| 2342 | void * clientData, |
| 2343 | Tcl_Interp *interp, |
| 2344 | int objc, |
| 2345 | Tcl_Obj *CONST objv[] |
| 2346 | ){ |
| 2347 | sqlite3_stmt *pStmt; |
| 2348 | int rc; |
| 2349 | sqlite3 *db = 0; |
| 2350 | |
| 2351 | if( objc!=2 ){ |
| 2352 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 2353 | Tcl_GetStringFromObj(objv[0], 0), " <STMT>", 0); |
| 2354 | return TCL_ERROR; |
| 2355 | } |
| 2356 | |
| 2357 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 2358 | |
| 2359 | if( pStmt ){ |
| 2360 | db = StmtToDb(pStmt); |
| 2361 | } |
| 2362 | rc = sqlite3_finalize(pStmt); |
| 2363 | Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC); |
| 2364 | if( db && sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; |
| 2365 | return TCL_OK; |
| 2366 | } |
| 2367 | |
| 2368 | /* |
| 2369 | ** Usage: sqlite3_stmt_status STMT CODE RESETFLAG |
| 2370 | ** |
| 2371 | ** Get the value of a status counter from a statement. |
| 2372 | */ |
| 2373 | static int SQLITE_TCLAPI test_stmt_status( |
| 2374 | void * clientData, |
| 2375 | Tcl_Interp *interp, |
| 2376 | int objc, |
| 2377 | Tcl_Obj *CONST objv[] |
| 2378 | ){ |
| 2379 | int iValue; |
| 2380 | int i, op = 0, resetFlag; |
| 2381 | const char *zOpName; |
| 2382 | sqlite3_stmt *pStmt; |
| 2383 | |
| 2384 | static const struct { |
| 2385 | const char *zName; |
| 2386 | int op; |
| 2387 | } aOp[] = { |
| 2388 | { "SQLITE_STMTSTATUS_FULLSCAN_STEP", SQLITE_STMTSTATUS_FULLSCAN_STEP }, |
| 2389 | { "SQLITE_STMTSTATUS_SORT", SQLITE_STMTSTATUS_SORT }, |
| 2390 | { "SQLITE_STMTSTATUS_AUTOINDEX", SQLITE_STMTSTATUS_AUTOINDEX }, |
| 2391 | { "SQLITE_STMTSTATUS_VM_STEP", SQLITE_STMTSTATUS_VM_STEP }, |
| 2392 | { "SQLITE_STMTSTATUS_REPREPARE", SQLITE_STMTSTATUS_REPREPARE }, |
| 2393 | { "SQLITE_STMTSTATUS_RUN", SQLITE_STMTSTATUS_RUN }, |
| 2394 | { "SQLITE_STMTSTATUS_MEMUSED", SQLITE_STMTSTATUS_MEMUSED }, |
| 2395 | }; |
| 2396 | if( objc!=4 ){ |
| 2397 | Tcl_WrongNumArgs(interp, 1, objv, "STMT PARAMETER RESETFLAG"); |
| 2398 | return TCL_ERROR; |
| 2399 | } |
| 2400 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 2401 | zOpName = Tcl_GetString(objv[2]); |
| 2402 | for(i=0; i<ArraySize(aOp); i++){ |
| 2403 | if( strcmp(aOp[i].zName, zOpName)==0 ){ |
| 2404 | op = aOp[i].op; |
| 2405 | break; |
| 2406 | } |
| 2407 | } |
| 2408 | if( i>=ArraySize(aOp) ){ |
| 2409 | if( Tcl_GetIntFromObj(interp, objv[2], &op) ) return TCL_ERROR; |
| 2410 | } |
| 2411 | if( Tcl_GetBooleanFromObj(interp, objv[3], &resetFlag) ) return TCL_ERROR; |
| 2412 | iValue = sqlite3_stmt_status(pStmt, op, resetFlag); |
| 2413 | Tcl_SetObjResult(interp, Tcl_NewIntObj(iValue)); |
| 2414 | return TCL_OK; |
| 2415 | } |
| 2416 | |
| 2417 | #ifdef SQLITE_ENABLE_STMT_SCANSTATUS |
| 2418 | /* |
| 2419 | ** Usage: sqlite3_stmt_scanstatus ?-flags FLAGS? STMT IDX |
| 2420 | */ |
| 2421 | static int SQLITE_TCLAPI test_stmt_scanstatus( |
| 2422 | void * clientData, |
| 2423 | Tcl_Interp *interp, |
| 2424 | int objc, |
| 2425 | Tcl_Obj *CONST objv[] |
| 2426 | ){ |
| 2427 | sqlite3_stmt *pStmt; /* First argument */ |
| 2428 | int idx; /* Second argument */ |
| 2429 | |
| 2430 | const char *zName; |
| 2431 | const char *zExplain; |
| 2432 | sqlite3_int64 nLoop; |
| 2433 | sqlite3_int64 nVisit; |
| 2434 | sqlite3_int64 nCycle; |
| 2435 | double rEst; |
| 2436 | int res; |
| 2437 | int flags = 0; |
| 2438 | int iSelectId = 0; |
| 2439 | int iParentId = 0; |
| 2440 | int bDebug = 0; |
| 2441 | |
| 2442 | if( objc==5 ){ |
| 2443 | struct Flag { |
| 2444 | const char *zFlag; |
| 2445 | int flag; |
| 2446 | } aTbl[] = { |
| 2447 | {"complex", SQLITE_SCANSTAT_COMPLEX}, |
| 2448 | {"debug", -1}, |
| 2449 | {0, 0} |
| 2450 | }; |
| 2451 | |
| 2452 | Tcl_Obj **aFlag = 0; |
| 2453 | Tcl_Size nFlag = 0; |
| 2454 | int ii; |
| 2455 | |
| 2456 | if( Tcl_ListObjGetElements(interp, objv[2], &nFlag, &aFlag) ){ |
| 2457 | return TCL_ERROR; |
| 2458 | } |
| 2459 | for(ii=0; ii<(int)nFlag; ii++){ |
| 2460 | int iVal = 0; |
| 2461 | res = Tcl_GetIndexFromObjStruct( |
| 2462 | interp, aFlag[ii], aTbl, sizeof(aTbl[0]), "flag", 0, &iVal |
| 2463 | ); |
| 2464 | if( res ) return TCL_ERROR; |
| 2465 | if( aTbl[iVal].flag==-1 ){ |
| 2466 | bDebug = 1; |
| 2467 | }else{ |
| 2468 | flags |= aTbl[iVal].flag; |
| 2469 | } |
| 2470 | } |
| 2471 | } |
| 2472 | |
| 2473 | if( objc!=3 && objc!=5 ){ |
| 2474 | Tcl_WrongNumArgs(interp, 1, objv, "-flags FLAGS STMT IDX"); |
| 2475 | return TCL_ERROR; |
| 2476 | } |
| 2477 | if( getStmtPointer(interp, Tcl_GetString(objv[objc-2]), &pStmt) |
| 2478 | || Tcl_GetIntFromObj(interp, objv[objc-1], &idx) |
| 2479 | ){ |
| 2480 | return TCL_ERROR; |
| 2481 | } |
| 2482 | |
| 2483 | if( bDebug && 0==(flags & SQLITE_SCANSTAT_COMPLEX) ){ |
| 2484 | Tcl_SetObjResult(interp, |
| 2485 | Tcl_NewStringObj("may not specify debug without complex", -1) |
| 2486 | ); |
| 2487 | return TCL_ERROR; |
| 2488 | } |
| 2489 | |
| 2490 | if( idx<0 ){ |
| 2491 | Tcl_Obj *pRet = Tcl_NewObj(); |
| 2492 | res = sqlite3_stmt_scanstatus_v2( |
| 2493 | pStmt, -1, SQLITE_SCANSTAT_NCYCLE, flags, (void*)&nCycle |
| 2494 | ); |
| 2495 | sqlite3_stmt_scanstatus_v2( |
| 2496 | pStmt, idx, SQLITE_SCANSTAT_NCYCLE, flags, (void*)&nCycle); |
| 2497 | Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj("nCycle", -1)); |
| 2498 | Tcl_ListObjAppendElement(0, pRet, Tcl_NewWideIntObj(nCycle)); |
| 2499 | Tcl_SetObjResult(interp, pRet); |
| 2500 | }else{ |
| 2501 | res = sqlite3_stmt_scanstatus_v2( |
| 2502 | pStmt, idx, SQLITE_SCANSTAT_NLOOP, flags, (void*)&nLoop |
| 2503 | ); |
| 2504 | if( res==0 ){ |
| 2505 | Tcl_Obj *pRet = Tcl_NewObj(); |
| 2506 | Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj("nLoop", -1)); |
| 2507 | Tcl_ListObjAppendElement(0, pRet, Tcl_NewWideIntObj(nLoop)); |
| 2508 | sqlite3_stmt_scanstatus_v2( |
| 2509 | pStmt, idx, SQLITE_SCANSTAT_NVISIT, flags, (void*)&nVisit); |
| 2510 | Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj("nVisit", -1)); |
| 2511 | Tcl_ListObjAppendElement(0, pRet, Tcl_NewWideIntObj(nVisit)); |
| 2512 | sqlite3_stmt_scanstatus_v2( |
| 2513 | pStmt, idx, SQLITE_SCANSTAT_EST, flags, (void*)&rEst); |
| 2514 | Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj("nEst", -1)); |
| 2515 | Tcl_ListObjAppendElement(0, pRet, Tcl_NewDoubleObj(rEst)); |
| 2516 | sqlite3_stmt_scanstatus_v2( |
| 2517 | pStmt, idx, SQLITE_SCANSTAT_NAME, flags, (void*)&zName); |
| 2518 | Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj("zName", -1)); |
| 2519 | Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zName, -1)); |
| 2520 | sqlite3_stmt_scanstatus_v2( |
| 2521 | pStmt, idx, SQLITE_SCANSTAT_EXPLAIN, flags, (void*)&zExplain); |
| 2522 | Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj("zExplain", -1)); |
| 2523 | Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zExplain, -1)); |
| 2524 | sqlite3_stmt_scanstatus_v2( |
| 2525 | pStmt, idx, SQLITE_SCANSTAT_SELECTID, flags, (void*)&iSelectId); |
| 2526 | Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj("iSelectId", -1)); |
| 2527 | Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(iSelectId)); |
| 2528 | sqlite3_stmt_scanstatus_v2( |
| 2529 | pStmt, idx, SQLITE_SCANSTAT_PARENTID, flags, (void*)&iParentId); |
| 2530 | Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj("iParentId", -1)); |
| 2531 | Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(iParentId)); |
| 2532 | sqlite3_stmt_scanstatus_v2( |
| 2533 | pStmt, idx, SQLITE_SCANSTAT_NCYCLE, flags, (void*)&nCycle); |
| 2534 | Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj("nCycle", -1)); |
| 2535 | Tcl_ListObjAppendElement(0, pRet, Tcl_NewWideIntObj(nCycle)); |
| 2536 | |
| 2537 | if( bDebug ){ |
| 2538 | int ii; |
| 2539 | ScanStatus *pScan = &((Vdbe*)pStmt)->aScan[idx]; |
| 2540 | Tcl_Obj *pRange = Tcl_NewObj(); |
| 2541 | Tcl_Obj *pCsr = Tcl_NewObj(); |
| 2542 | |
| 2543 | Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj("debug_loop", -1)); |
| 2544 | Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(pScan->addrLoop)); |
| 2545 | Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj("debug_visit", -1)); |
| 2546 | Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(pScan->addrVisit)); |
| 2547 | Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj("debug_explain",-1)); |
| 2548 | Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(pScan->addrExplain)); |
| 2549 | for(ii=0; ii<ArraySize(pScan->aAddrRange)/2; ii++){ |
| 2550 | int iStart = pScan->aAddrRange[ii*2]; |
| 2551 | int iEnd = pScan->aAddrRange[ii*2+1]; |
| 2552 | if( iStart>0 ){ |
| 2553 | Tcl_ListObjAppendElement(0, pRange, Tcl_NewIntObj(iStart)); |
| 2554 | Tcl_ListObjAppendElement(0, pRange, Tcl_NewIntObj(iEnd)); |
| 2555 | }else if( iStart<0 ){ |
| 2556 | Tcl_ListObjAppendElement(0, pCsr, Tcl_NewIntObj(iEnd)); |
| 2557 | } |
| 2558 | } |
| 2559 | |
| 2560 | Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj("debug_range", -1)); |
| 2561 | Tcl_ListObjAppendElement(0, pRet, pRange); |
| 2562 | Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj("debug_csr", -1)); |
| 2563 | Tcl_ListObjAppendElement(0, pRet, pCsr); |
| 2564 | } |
| 2565 | |
| 2566 | Tcl_SetObjResult(interp, pRet); |
| 2567 | }else{ |
| 2568 | Tcl_ResetResult(interp); |
| 2569 | } |
| 2570 | } |
| 2571 | return TCL_OK; |
| 2572 | } |
| 2573 | |
| 2574 | /* |
| 2575 | ** Usage: sqlite3_stmt_scanstatus_reset STMT |
| 2576 | */ |
| 2577 | static int SQLITE_TCLAPI test_stmt_scanstatus_reset( |
| 2578 | void * clientData, |
| 2579 | Tcl_Interp *interp, |
| 2580 | int objc, |
| 2581 | Tcl_Obj *CONST objv[] |
| 2582 | ){ |
| 2583 | sqlite3_stmt *pStmt; /* First argument */ |
| 2584 | if( objc!=2 ){ |
| 2585 | Tcl_WrongNumArgs(interp, 1, objv, "STMT"); |
| 2586 | return TCL_ERROR; |
| 2587 | } |
| 2588 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 2589 | sqlite3_stmt_scanstatus_reset(pStmt); |
| 2590 | return TCL_OK; |
| 2591 | } |
| 2592 | #endif |
| 2593 | |
| 2594 | #ifdef SQLITE_ENABLE_SQLLOG |
| 2595 | /* |
| 2596 | ** Usage: sqlite3_config_sqllog |
| 2597 | ** |
| 2598 | ** Zero the SQLITE_CONFIG_SQLLOG configuration |
| 2599 | */ |
| 2600 | static int SQLITE_TCLAPI test_config_sqllog( |
| 2601 | void * clientData, |
| 2602 | Tcl_Interp *interp, |
| 2603 | int objc, |
| 2604 | Tcl_Obj *CONST objv[] |
| 2605 | ){ |
| 2606 | if( objc!=1 ){ |
| 2607 | Tcl_WrongNumArgs(interp, 1, objv, ""); |
| 2608 | return TCL_ERROR; |
| 2609 | } |
| 2610 | sqlite3_config(SQLITE_CONFIG_SQLLOG, 0, 0); |
| 2611 | return TCL_OK; |
| 2612 | } |
| 2613 | #endif |
| 2614 | |
| 2615 | /* |
| 2616 | ** Usage: sqlite3_config_sorterref |
| 2617 | ** |
| 2618 | ** Set the SQLITE_CONFIG_SORTERREF_SIZE configuration option |
| 2619 | */ |
| 2620 | static int SQLITE_TCLAPI test_config_sorterref( |
| 2621 | void * clientData, |
| 2622 | Tcl_Interp *interp, |
| 2623 | int objc, |
| 2624 | Tcl_Obj *CONST objv[] |
| 2625 | ){ |
| 2626 | int iVal; |
| 2627 | if( objc!=2 ){ |
| 2628 | Tcl_WrongNumArgs(interp, 1, objv, "NBYTE"); |
| 2629 | return TCL_ERROR; |
| 2630 | } |
| 2631 | if( Tcl_GetIntFromObj(interp, objv[1], &iVal) ) return TCL_ERROR; |
| 2632 | sqlite3_config(SQLITE_CONFIG_SORTERREF_SIZE, iVal); |
| 2633 | return TCL_OK; |
| 2634 | } |
| 2635 | |
| 2636 | /* |
| 2637 | ** Usage: vfs_current_time_int64 |
| 2638 | ** |
| 2639 | ** Return the value returned by the default VFS's xCurrentTimeInt64 method. |
| 2640 | */ |
| 2641 | static int SQLITE_TCLAPI vfsCurrentTimeInt64( |
| 2642 | void * clientData, |
| 2643 | Tcl_Interp *interp, |
| 2644 | int objc, |
| 2645 | Tcl_Obj *CONST objv[] |
| 2646 | ){ |
| 2647 | i64 t; |
| 2648 | sqlite3_vfs *pVfs = sqlite3_vfs_find(0); |
| 2649 | if( objc!=1 ){ |
| 2650 | Tcl_WrongNumArgs(interp, 1, objv, ""); |
| 2651 | return TCL_ERROR; |
| 2652 | } |
| 2653 | pVfs->xCurrentTimeInt64(pVfs, &t); |
| 2654 | Tcl_SetObjResult(interp, Tcl_NewWideIntObj(t)); |
| 2655 | return TCL_OK; |
| 2656 | } |
| 2657 | |
| 2658 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 2659 | /* |
| 2660 | ** Usage: create_null_module DB NAME |
| 2661 | */ |
| 2662 | static int SQLITE_TCLAPI test_create_null_module( |
| 2663 | void * clientData, |
| 2664 | Tcl_Interp *interp, |
| 2665 | int objc, |
| 2666 | Tcl_Obj *CONST objv[] |
| 2667 | ){ |
| 2668 | sqlite3 *db; |
| 2669 | char *zName; |
| 2670 | |
| 2671 | if( objc!=3 ){ |
| 2672 | Tcl_WrongNumArgs(interp, 1, objv, "DB DBNAME"); |
| 2673 | return TCL_ERROR; |
| 2674 | } |
| 2675 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 2676 | zName = Tcl_GetString(objv[2]); |
| 2677 | |
| 2678 | sqlite3_create_module(db, zName, 0, 0); |
| 2679 | return TCL_OK; |
| 2680 | } |
| 2681 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 2682 | |
| 2683 | #ifdef SQLITE_ENABLE_SNAPSHOT |
| 2684 | /* |
| 2685 | ** Usage: sqlite3_snapshot_get DB DBNAME |
| 2686 | */ |
| 2687 | static int SQLITE_TCLAPI test_snapshot_get( |
| 2688 | void * clientData, |
| 2689 | Tcl_Interp *interp, |
| 2690 | int objc, |
| 2691 | Tcl_Obj *CONST objv[] |
| 2692 | ){ |
| 2693 | int rc; |
| 2694 | sqlite3 *db; |
| 2695 | char *zName; |
| 2696 | sqlite3_snapshot *pSnapshot = 0; |
| 2697 | |
| 2698 | if( objc!=3 ){ |
| 2699 | Tcl_WrongNumArgs(interp, 1, objv, "DB DBNAME"); |
| 2700 | return TCL_ERROR; |
| 2701 | } |
| 2702 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 2703 | zName = Tcl_GetString(objv[2]); |
| 2704 | |
| 2705 | rc = sqlite3_snapshot_get(db, zName, &pSnapshot); |
| 2706 | if( rc!=SQLITE_OK ){ |
| 2707 | Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3ErrName(rc), -1)); |
| 2708 | return TCL_ERROR; |
| 2709 | }else{ |
| 2710 | char zBuf[100]; |
| 2711 | if( sqlite3TestMakePointerStr(interp, zBuf, pSnapshot) ) return TCL_ERROR; |
| 2712 | Tcl_SetObjResult(interp, Tcl_NewStringObj(zBuf, -1)); |
| 2713 | } |
| 2714 | return TCL_OK; |
| 2715 | } |
| 2716 | #endif /* SQLITE_ENABLE_SNAPSHOT */ |
| 2717 | |
| 2718 | #ifdef SQLITE_ENABLE_SNAPSHOT |
| 2719 | /* |
| 2720 | ** Usage: sqlite3_snapshot_recover DB DBNAME |
| 2721 | */ |
| 2722 | static int SQLITE_TCLAPI test_snapshot_recover( |
| 2723 | void * clientData, |
| 2724 | Tcl_Interp *interp, |
| 2725 | int objc, |
| 2726 | Tcl_Obj *CONST objv[] |
| 2727 | ){ |
| 2728 | int rc; |
| 2729 | sqlite3 *db; |
| 2730 | char *zName; |
| 2731 | |
| 2732 | if( objc!=3 ){ |
| 2733 | Tcl_WrongNumArgs(interp, 1, objv, "DB DBNAME"); |
| 2734 | return TCL_ERROR; |
| 2735 | } |
| 2736 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 2737 | zName = Tcl_GetString(objv[2]); |
| 2738 | |
| 2739 | rc = sqlite3_snapshot_recover(db, zName); |
| 2740 | if( rc!=SQLITE_OK ){ |
| 2741 | Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3ErrName(rc), -1)); |
| 2742 | return TCL_ERROR; |
| 2743 | }else{ |
| 2744 | Tcl_ResetResult(interp); |
| 2745 | } |
| 2746 | return TCL_OK; |
| 2747 | } |
| 2748 | #endif /* SQLITE_ENABLE_SNAPSHOT */ |
| 2749 | |
| 2750 | #ifdef SQLITE_ENABLE_SNAPSHOT |
| 2751 | /* |
| 2752 | ** Usage: sqlite3_snapshot_open DB DBNAME SNAPSHOT |
| 2753 | */ |
| 2754 | static int SQLITE_TCLAPI test_snapshot_open( |
| 2755 | void * clientData, |
| 2756 | Tcl_Interp *interp, |
| 2757 | int objc, |
| 2758 | Tcl_Obj *CONST objv[] |
| 2759 | ){ |
| 2760 | int rc; |
| 2761 | sqlite3 *db; |
| 2762 | char *zName; |
| 2763 | sqlite3_snapshot *pSnapshot; |
| 2764 | |
| 2765 | if( objc!=4 ){ |
| 2766 | Tcl_WrongNumArgs(interp, 1, objv, "DB DBNAME SNAPSHOT"); |
| 2767 | return TCL_ERROR; |
| 2768 | } |
| 2769 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 2770 | zName = Tcl_GetString(objv[2]); |
| 2771 | pSnapshot = (sqlite3_snapshot*)testStringToPointer(Tcl_GetString(objv[3])); |
| 2772 | |
| 2773 | rc = sqlite3_snapshot_open(db, zName, pSnapshot); |
| 2774 | if( rc!=SQLITE_OK ){ |
| 2775 | Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3ErrName(rc), -1)); |
| 2776 | return TCL_ERROR; |
| 2777 | }else{ |
| 2778 | Tcl_ResetResult(interp); |
| 2779 | } |
| 2780 | return TCL_OK; |
| 2781 | } |
| 2782 | #endif /* SQLITE_ENABLE_SNAPSHOT */ |
| 2783 | |
| 2784 | #ifdef SQLITE_ENABLE_SNAPSHOT |
| 2785 | /* |
| 2786 | ** Usage: sqlite3_snapshot_free SNAPSHOT |
| 2787 | */ |
| 2788 | static int SQLITE_TCLAPI test_snapshot_free( |
| 2789 | void * clientData, |
| 2790 | Tcl_Interp *interp, |
| 2791 | int objc, |
| 2792 | Tcl_Obj *CONST objv[] |
| 2793 | ){ |
| 2794 | sqlite3_snapshot *pSnapshot; |
| 2795 | if( objc!=2 ){ |
| 2796 | Tcl_WrongNumArgs(interp, 1, objv, "SNAPSHOT"); |
| 2797 | return TCL_ERROR; |
| 2798 | } |
| 2799 | pSnapshot = (sqlite3_snapshot*)testStringToPointer(Tcl_GetString(objv[1])); |
| 2800 | sqlite3_snapshot_free(pSnapshot); |
| 2801 | return TCL_OK; |
| 2802 | } |
| 2803 | #endif /* SQLITE_ENABLE_SNAPSHOT */ |
| 2804 | |
| 2805 | #ifdef SQLITE_ENABLE_SNAPSHOT |
| 2806 | /* |
| 2807 | ** Usage: sqlite3_snapshot_cmp SNAPSHOT1 SNAPSHOT2 |
| 2808 | */ |
| 2809 | static int SQLITE_TCLAPI test_snapshot_cmp( |
| 2810 | void * clientData, |
| 2811 | Tcl_Interp *interp, |
| 2812 | int objc, |
| 2813 | Tcl_Obj *CONST objv[] |
| 2814 | ){ |
| 2815 | int res; |
| 2816 | sqlite3_snapshot *p1; |
| 2817 | sqlite3_snapshot *p2; |
| 2818 | if( objc!=3 ){ |
| 2819 | Tcl_WrongNumArgs(interp, 1, objv, "SNAPSHOT1 SNAPSHOT2"); |
| 2820 | return TCL_ERROR; |
| 2821 | } |
| 2822 | p1 = (sqlite3_snapshot*)testStringToPointer(Tcl_GetString(objv[1])); |
| 2823 | p2 = (sqlite3_snapshot*)testStringToPointer(Tcl_GetString(objv[2])); |
| 2824 | res = sqlite3_snapshot_cmp(p1, p2); |
| 2825 | Tcl_SetObjResult(interp, Tcl_NewIntObj(res)); |
| 2826 | return TCL_OK; |
| 2827 | } |
| 2828 | #endif /* SQLITE_ENABLE_SNAPSHOT */ |
| 2829 | |
| 2830 | #ifdef SQLITE_ENABLE_SNAPSHOT |
| 2831 | /* |
| 2832 | ** Usage: sqlite3_snapshot_get_blob DB DBNAME |
| 2833 | */ |
| 2834 | static int SQLITE_TCLAPI test_snapshot_get_blob( |
| 2835 | void * clientData, |
| 2836 | Tcl_Interp *interp, |
| 2837 | int objc, |
| 2838 | Tcl_Obj *CONST objv[] |
| 2839 | ){ |
| 2840 | int rc; |
| 2841 | sqlite3 *db; |
| 2842 | char *zName; |
| 2843 | sqlite3_snapshot *pSnapshot = 0; |
| 2844 | |
| 2845 | if( objc!=3 ){ |
| 2846 | Tcl_WrongNumArgs(interp, 1, objv, "DB DBNAME"); |
| 2847 | return TCL_ERROR; |
| 2848 | } |
| 2849 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 2850 | zName = Tcl_GetString(objv[2]); |
| 2851 | |
| 2852 | rc = sqlite3_snapshot_get(db, zName, &pSnapshot); |
| 2853 | if( rc!=SQLITE_OK ){ |
| 2854 | Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3ErrName(rc), -1)); |
| 2855 | return TCL_ERROR; |
| 2856 | }else{ |
| 2857 | Tcl_SetObjResult(interp, |
| 2858 | Tcl_NewByteArrayObj((unsigned char*)pSnapshot, sizeof(sqlite3_snapshot)) |
| 2859 | ); |
| 2860 | sqlite3_snapshot_free(pSnapshot); |
| 2861 | } |
| 2862 | return TCL_OK; |
| 2863 | } |
| 2864 | #endif /* SQLITE_ENABLE_SNAPSHOT */ |
| 2865 | |
| 2866 | #ifdef SQLITE_ENABLE_SNAPSHOT |
| 2867 | /* |
| 2868 | ** Usage: sqlite3_snapshot_open_blob DB DBNAME SNAPSHOT |
| 2869 | */ |
| 2870 | static int SQLITE_TCLAPI test_snapshot_open_blob( |
| 2871 | void * clientData, |
| 2872 | Tcl_Interp *interp, |
| 2873 | int objc, |
| 2874 | Tcl_Obj *CONST objv[] |
| 2875 | ){ |
| 2876 | int rc; |
| 2877 | sqlite3 *db; |
| 2878 | char *zName; |
| 2879 | unsigned char *pBlob; |
| 2880 | Tcl_Size nBlob; |
| 2881 | |
| 2882 | if( objc!=4 ){ |
| 2883 | Tcl_WrongNumArgs(interp, 1, objv, "DB DBNAME SNAPSHOT"); |
| 2884 | return TCL_ERROR; |
| 2885 | } |
| 2886 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 2887 | zName = Tcl_GetString(objv[2]); |
| 2888 | pBlob = Tcl_GetByteArrayFromObj(objv[3], &nBlob); |
| 2889 | if( nBlob!=sizeof(sqlite3_snapshot) ){ |
| 2890 | Tcl_AppendResult(interp, "bad SNAPSHOT", NULL); |
| 2891 | return TCL_ERROR; |
| 2892 | } |
| 2893 | rc = sqlite3_snapshot_open(db, zName, (sqlite3_snapshot*)pBlob); |
| 2894 | if( rc!=SQLITE_OK ){ |
| 2895 | Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3ErrName(rc), -1)); |
| 2896 | return TCL_ERROR; |
| 2897 | } |
| 2898 | return TCL_OK; |
| 2899 | } |
| 2900 | #endif /* SQLITE_ENABLE_SNAPSHOT */ |
| 2901 | |
| 2902 | #ifdef SQLITE_ENABLE_SNAPSHOT |
| 2903 | /* |
| 2904 | ** Usage: sqlite3_snapshot_cmp_blob SNAPSHOT1 SNAPSHOT2 |
| 2905 | */ |
| 2906 | static int SQLITE_TCLAPI test_snapshot_cmp_blob( |
| 2907 | void * clientData, |
| 2908 | Tcl_Interp *interp, |
| 2909 | int objc, |
| 2910 | Tcl_Obj *CONST objv[] |
| 2911 | ){ |
| 2912 | int res; |
| 2913 | unsigned char *p1; |
| 2914 | unsigned char *p2; |
| 2915 | Tcl_Size n1; |
| 2916 | Tcl_Size n2; |
| 2917 | |
| 2918 | if( objc!=3 ){ |
| 2919 | Tcl_WrongNumArgs(interp, 1, objv, "SNAPSHOT1 SNAPSHOT2"); |
| 2920 | return TCL_ERROR; |
| 2921 | } |
| 2922 | |
| 2923 | p1 = Tcl_GetByteArrayFromObj(objv[1], &n1); |
| 2924 | p2 = Tcl_GetByteArrayFromObj(objv[2], &n2); |
| 2925 | |
| 2926 | if( n1!=sizeof(sqlite3_snapshot) || n1!=n2 ){ |
| 2927 | Tcl_AppendResult(interp, "bad SNAPSHOT", NULL); |
| 2928 | return TCL_ERROR; |
| 2929 | } |
| 2930 | |
| 2931 | res = sqlite3_snapshot_cmp((sqlite3_snapshot*)p1, (sqlite3_snapshot*)p2); |
| 2932 | Tcl_SetObjResult(interp, Tcl_NewIntObj(res)); |
| 2933 | return TCL_OK; |
| 2934 | } |
| 2935 | #endif /* SQLITE_ENABLE_SNAPSHOT */ |
| 2936 | |
| 2937 | /* |
| 2938 | ** Usage: sqlite3_delete_database FILENAME |
| 2939 | */ |
| 2940 | int sqlite3_delete_database(const char*); /* in test_delete.c */ |
| 2941 | static int SQLITE_TCLAPI test_delete_database( |
| 2942 | void * clientData, |
| 2943 | Tcl_Interp *interp, |
| 2944 | int objc, |
| 2945 | Tcl_Obj *CONST objv[] |
| 2946 | ){ |
| 2947 | int rc; |
| 2948 | const char *zFile; |
| 2949 | if( objc!=2 ){ |
| 2950 | Tcl_WrongNumArgs(interp, 1, objv, "FILE"); |
| 2951 | return TCL_ERROR; |
| 2952 | } |
| 2953 | zFile = (const char*)Tcl_GetString(objv[1]); |
| 2954 | rc = sqlite3_delete_database(zFile); |
| 2955 | |
| 2956 | Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3ErrName(rc), -1)); |
| 2957 | return TCL_OK; |
| 2958 | } |
| 2959 | |
| 2960 | /* |
| 2961 | ** Usage: atomic_batch_write PATH |
| 2962 | */ |
| 2963 | static int SQLITE_TCLAPI test_atomic_batch_write( |
| 2964 | void * clientData, |
| 2965 | Tcl_Interp *interp, |
| 2966 | int objc, |
| 2967 | Tcl_Obj *CONST objv[] |
| 2968 | ){ |
| 2969 | char *zFile = 0; /* Path to file to test */ |
| 2970 | sqlite3 *db = 0; /* Database handle */ |
| 2971 | sqlite3_file *pFd = 0; /* SQLite fd open on zFile */ |
| 2972 | int bRes = 0; /* Integer result of this command */ |
| 2973 | int dc = 0; /* Device-characteristics mask */ |
| 2974 | int rc; /* sqlite3_open() return code */ |
| 2975 | |
| 2976 | if( objc!=2 ){ |
| 2977 | Tcl_WrongNumArgs(interp, 1, objv, "PATH"); |
| 2978 | return TCL_ERROR; |
| 2979 | } |
| 2980 | zFile = Tcl_GetString(objv[1]); |
| 2981 | |
| 2982 | rc = sqlite3_open(zFile, &db); |
| 2983 | if( rc!=SQLITE_OK ){ |
| 2984 | Tcl_AppendResult(interp, sqlite3_errmsg(db), NULL); |
| 2985 | sqlite3_close(db); |
| 2986 | return TCL_ERROR; |
| 2987 | } |
| 2988 | |
| 2989 | rc = sqlite3_file_control(db, "main", SQLITE_FCNTL_FILE_POINTER, (void*)&pFd); |
| 2990 | dc = pFd->pMethods->xDeviceCharacteristics(pFd); |
| 2991 | if( dc & SQLITE_IOCAP_BATCH_ATOMIC ){ |
| 2992 | bRes = 1; |
| 2993 | } |
| 2994 | |
| 2995 | Tcl_SetObjResult(interp, Tcl_NewIntObj(bRes)); |
| 2996 | sqlite3_close(db); |
| 2997 | return TCL_OK; |
| 2998 | } |
| 2999 | |
| 3000 | /* |
| 3001 | ** Usage: sqlite3_next_stmt DB STMT |
| 3002 | ** |
| 3003 | ** Return the next statement in sequence after STMT. |
| 3004 | */ |
| 3005 | static int SQLITE_TCLAPI test_next_stmt( |
| 3006 | void * clientData, |
| 3007 | Tcl_Interp *interp, |
| 3008 | int objc, |
| 3009 | Tcl_Obj *CONST objv[] |
| 3010 | ){ |
| 3011 | sqlite3_stmt *pStmt; |
| 3012 | sqlite3 *db = 0; |
| 3013 | char zBuf[50]; |
| 3014 | |
| 3015 | if( objc!=3 ){ |
| 3016 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 3017 | Tcl_GetStringFromObj(objv[0], 0), " DB STMT", 0); |
| 3018 | return TCL_ERROR; |
| 3019 | } |
| 3020 | |
| 3021 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 3022 | if( getStmtPointer(interp, Tcl_GetString(objv[2]), &pStmt) ) return TCL_ERROR; |
| 3023 | pStmt = sqlite3_next_stmt(db, pStmt); |
| 3024 | if( pStmt ){ |
| 3025 | if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR; |
| 3026 | Tcl_AppendResult(interp, zBuf, NULL); |
| 3027 | } |
| 3028 | return TCL_OK; |
| 3029 | } |
| 3030 | |
| 3031 | /* |
| 3032 | ** Usage: sqlite3_stmt_readonly STMT |
| 3033 | ** |
| 3034 | ** Return true if STMT is a NULL pointer or a pointer to a statement |
| 3035 | ** that is guaranteed to leave the database unmodified. |
| 3036 | */ |
| 3037 | static int SQLITE_TCLAPI test_stmt_readonly( |
| 3038 | void * clientData, |
| 3039 | Tcl_Interp *interp, |
| 3040 | int objc, |
| 3041 | Tcl_Obj *CONST objv[] |
| 3042 | ){ |
| 3043 | sqlite3_stmt *pStmt; |
| 3044 | int rc; |
| 3045 | |
| 3046 | if( objc!=2 ){ |
| 3047 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 3048 | Tcl_GetStringFromObj(objv[0], 0), " STMT", 0); |
| 3049 | return TCL_ERROR; |
| 3050 | } |
| 3051 | |
| 3052 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 3053 | rc = sqlite3_stmt_readonly(pStmt); |
| 3054 | Tcl_SetObjResult(interp, Tcl_NewBooleanObj(rc)); |
| 3055 | return TCL_OK; |
| 3056 | } |
| 3057 | |
| 3058 | /* |
| 3059 | ** Usage: sqlite3_stmt_isexplain STMT |
| 3060 | ** |
| 3061 | ** Return 1, 2, or 0 respectively if STMT is an EXPLAIN statement, an |
| 3062 | ** EXPLAIN QUERY PLAN statement or an ordinary statement or NULL pointer. |
| 3063 | */ |
| 3064 | static int SQLITE_TCLAPI test_stmt_isexplain( |
| 3065 | void * clientData, |
| 3066 | Tcl_Interp *interp, |
| 3067 | int objc, |
| 3068 | Tcl_Obj *CONST objv[] |
| 3069 | ){ |
| 3070 | sqlite3_stmt *pStmt; |
| 3071 | int rc; |
| 3072 | |
| 3073 | if( objc!=2 ){ |
| 3074 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 3075 | Tcl_GetStringFromObj(objv[0], 0), " STMT", 0); |
| 3076 | return TCL_ERROR; |
| 3077 | } |
| 3078 | |
| 3079 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 3080 | rc = sqlite3_stmt_isexplain(pStmt); |
| 3081 | Tcl_SetObjResult(interp, Tcl_NewIntObj(rc)); |
| 3082 | return TCL_OK; |
| 3083 | } |
| 3084 | |
| 3085 | /* |
| 3086 | ** Usage: sqlite3_stmt_explain STMT INT |
| 3087 | ** |
| 3088 | ** Set the explain to normal (0), EXPLAIN (1) or EXPLAIN QUERY PLAN (2). |
| 3089 | */ |
| 3090 | static int SQLITE_TCLAPI test_stmt_explain( |
| 3091 | void * clientData, |
| 3092 | Tcl_Interp *interp, |
| 3093 | int objc, |
| 3094 | Tcl_Obj *CONST objv[] |
| 3095 | ){ |
| 3096 | sqlite3_stmt *pStmt; |
| 3097 | int eMode = 0; |
| 3098 | int rc; |
| 3099 | |
| 3100 | if( objc!=3 ){ |
| 3101 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 3102 | Tcl_GetStringFromObj(objv[0], 0), " STMT INT", 0); |
| 3103 | return TCL_ERROR; |
| 3104 | } |
| 3105 | |
| 3106 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 3107 | if( Tcl_GetIntFromObj(interp, objv[2], &eMode) ) return TCL_ERROR; |
| 3108 | rc = sqlite3_stmt_explain(pStmt, eMode); |
| 3109 | Tcl_SetObjResult(interp, Tcl_NewIntObj(rc)); |
| 3110 | return TCL_OK; |
| 3111 | } |
| 3112 | |
| 3113 | /* |
| 3114 | ** Usage: sqlite3_stmt_busy STMT |
| 3115 | ** |
| 3116 | ** Return true if STMT is a non-NULL pointer to a statement |
| 3117 | ** that has been stepped but not to completion. |
| 3118 | */ |
| 3119 | static int SQLITE_TCLAPI test_stmt_busy( |
| 3120 | void * clientData, |
| 3121 | Tcl_Interp *interp, |
| 3122 | int objc, |
| 3123 | Tcl_Obj *CONST objv[] |
| 3124 | ){ |
| 3125 | sqlite3_stmt *pStmt; |
| 3126 | int rc; |
| 3127 | |
| 3128 | if( objc!=2 ){ |
| 3129 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 3130 | Tcl_GetStringFromObj(objv[0], 0), " STMT", 0); |
| 3131 | return TCL_ERROR; |
| 3132 | } |
| 3133 | |
| 3134 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 3135 | rc = sqlite3_stmt_busy(pStmt); |
| 3136 | Tcl_SetObjResult(interp, Tcl_NewBooleanObj(rc)); |
| 3137 | return TCL_OK; |
| 3138 | } |
| 3139 | |
| 3140 | /* |
| 3141 | ** Usage: uses_stmt_journal STMT |
| 3142 | ** |
| 3143 | ** Return true if STMT uses a statement journal. |
| 3144 | */ |
| 3145 | static int SQLITE_TCLAPI uses_stmt_journal( |
| 3146 | void * clientData, |
| 3147 | Tcl_Interp *interp, |
| 3148 | int objc, |
| 3149 | Tcl_Obj *CONST objv[] |
| 3150 | ){ |
| 3151 | sqlite3_stmt *pStmt; |
| 3152 | |
| 3153 | if( objc!=2 ){ |
| 3154 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 3155 | Tcl_GetStringFromObj(objv[0], 0), " STMT", 0); |
| 3156 | return TCL_ERROR; |
| 3157 | } |
| 3158 | |
| 3159 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 3160 | sqlite3_stmt_readonly(pStmt); |
| 3161 | Tcl_SetObjResult(interp, Tcl_NewBooleanObj(((Vdbe *)pStmt)->usesStmtJournal)); |
| 3162 | return TCL_OK; |
| 3163 | } |
| 3164 | |
| 3165 | |
| 3166 | /* |
| 3167 | ** Usage: sqlite3_reset STMT |
| 3168 | ** |
| 3169 | ** Reset a statement handle. |
| 3170 | */ |
| 3171 | static int SQLITE_TCLAPI test_reset( |
| 3172 | void * clientData, |
| 3173 | Tcl_Interp *interp, |
| 3174 | int objc, |
| 3175 | Tcl_Obj *CONST objv[] |
| 3176 | ){ |
| 3177 | sqlite3_stmt *pStmt; |
| 3178 | int rc; |
| 3179 | |
| 3180 | if( objc!=2 ){ |
| 3181 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 3182 | Tcl_GetStringFromObj(objv[0], 0), " <STMT>", 0); |
| 3183 | return TCL_ERROR; |
| 3184 | } |
| 3185 | |
| 3186 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 3187 | |
| 3188 | rc = sqlite3_reset(pStmt); |
| 3189 | if( pStmt && sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ){ |
| 3190 | return TCL_ERROR; |
| 3191 | } |
| 3192 | Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC); |
| 3193 | /* |
| 3194 | if( rc ){ |
| 3195 | return TCL_ERROR; |
| 3196 | } |
| 3197 | */ |
| 3198 | return TCL_OK; |
| 3199 | } |
| 3200 | |
| 3201 | /* |
| 3202 | ** Usage: sqlite3_expired STMT |
| 3203 | ** |
| 3204 | ** Return TRUE if a recompilation of the statement is recommended. |
| 3205 | */ |
| 3206 | static int SQLITE_TCLAPI test_expired( |
| 3207 | void * clientData, |
| 3208 | Tcl_Interp *interp, |
| 3209 | int objc, |
| 3210 | Tcl_Obj *CONST objv[] |
| 3211 | ){ |
| 3212 | #ifndef SQLITE_OMIT_DEPRECATED |
| 3213 | sqlite3_stmt *pStmt; |
| 3214 | if( objc!=2 ){ |
| 3215 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 3216 | Tcl_GetStringFromObj(objv[0], 0), " <STMT>", 0); |
| 3217 | return TCL_ERROR; |
| 3218 | } |
| 3219 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 3220 | Tcl_SetObjResult(interp, Tcl_NewBooleanObj(sqlite3_expired(pStmt))); |
| 3221 | #endif |
| 3222 | return TCL_OK; |
| 3223 | } |
| 3224 | |
| 3225 | /* |
| 3226 | ** Usage: sqlite3_transfer_bindings FROMSTMT TOSTMT |
| 3227 | ** |
| 3228 | ** Transfer all bindings from FROMSTMT over to TOSTMT |
| 3229 | */ |
| 3230 | static int SQLITE_TCLAPI test_transfer_bind( |
| 3231 | void * clientData, |
| 3232 | Tcl_Interp *interp, |
| 3233 | int objc, |
| 3234 | Tcl_Obj *CONST objv[] |
| 3235 | ){ |
| 3236 | #ifndef SQLITE_OMIT_DEPRECATED |
| 3237 | sqlite3_stmt *pStmt1, *pStmt2; |
| 3238 | if( objc!=3 ){ |
| 3239 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 3240 | Tcl_GetStringFromObj(objv[0], 0), " FROM-STMT TO-STMT", 0); |
| 3241 | return TCL_ERROR; |
| 3242 | } |
| 3243 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt1)) return TCL_ERROR; |
| 3244 | if( getStmtPointer(interp, Tcl_GetString(objv[2]), &pStmt2)) return TCL_ERROR; |
| 3245 | Tcl_SetObjResult(interp, |
| 3246 | Tcl_NewIntObj(sqlite3_transfer_bindings(pStmt1,pStmt2))); |
| 3247 | #endif |
| 3248 | return TCL_OK; |
| 3249 | } |
| 3250 | |
| 3251 | /* |
| 3252 | ** Usage: sqlite3_changes DB |
| 3253 | ** |
| 3254 | ** Return the number of changes made to the database by the last SQL |
| 3255 | ** execution. |
| 3256 | */ |
| 3257 | static int SQLITE_TCLAPI test_changes( |
| 3258 | void * clientData, |
| 3259 | Tcl_Interp *interp, |
| 3260 | int objc, |
| 3261 | Tcl_Obj *CONST objv[] |
| 3262 | ){ |
| 3263 | sqlite3 *db; |
| 3264 | if( objc!=2 ){ |
| 3265 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 3266 | Tcl_GetString(objv[0]), " DB", 0); |
| 3267 | return TCL_ERROR; |
| 3268 | } |
| 3269 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 3270 | Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_changes(db))); |
| 3271 | return TCL_OK; |
| 3272 | } |
| 3273 | |
| 3274 | /* |
| 3275 | ** This is the "static_bind_value" that variables are bound to when |
| 3276 | ** the FLAG option of sqlite3_bind is "static" |
| 3277 | */ |
| 3278 | static char *sqlite_static_bind_value = 0; |
| 3279 | static int sqlite_static_bind_nbyte = 0; |
| 3280 | |
| 3281 | /* |
| 3282 | ** Usage: sqlite3_bind VM IDX VALUE FLAGS |
| 3283 | ** |
| 3284 | ** Sets the value of the IDX-th occurrence of "?" in the original SQL |
| 3285 | ** string. VALUE is the new value. If FLAGS=="null" then VALUE is |
| 3286 | ** ignored and the value is set to NULL. If FLAGS=="static" then |
| 3287 | ** the value is set to the value of a static variable named |
| 3288 | ** "sqlite_static_bind_value". If FLAGS=="normal" then a copy |
| 3289 | ** of the VALUE is made. If FLAGS=="blob10" then a VALUE is ignored |
| 3290 | ** an a 10-byte blob "abc\000xyz\000pq" is inserted. |
| 3291 | */ |
| 3292 | static int SQLITE_TCLAPI test_bind( |
| 3293 | void *NotUsed, |
| 3294 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 3295 | int argc, /* Number of arguments */ |
| 3296 | char **argv /* Text of each argument */ |
| 3297 | ){ |
| 3298 | sqlite3_stmt *pStmt; |
| 3299 | int rc; |
| 3300 | int idx; |
| 3301 | if( argc!=5 ){ |
| 3302 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 3303 | " VM IDX VALUE (null|static|normal)\"", 0); |
| 3304 | return TCL_ERROR; |
| 3305 | } |
| 3306 | if( getStmtPointer(interp, argv[1], &pStmt) ) return TCL_ERROR; |
| 3307 | if( Tcl_GetInt(interp, argv[2], &idx) ) return TCL_ERROR; |
| 3308 | if( strcmp(argv[4],"null")==0 ){ |
| 3309 | rc = sqlite3_bind_null(pStmt, idx); |
| 3310 | }else if( strcmp(argv[4],"static")==0 ){ |
| 3311 | rc = sqlite3_bind_text(pStmt, idx, sqlite_static_bind_value, -1, 0); |
| 3312 | }else if( strcmp(argv[4],"static-nbytes")==0 ){ |
| 3313 | rc = sqlite3_bind_text(pStmt, idx, sqlite_static_bind_value, |
| 3314 | sqlite_static_bind_nbyte, 0); |
| 3315 | }else if( strcmp(argv[4],"normal")==0 ){ |
| 3316 | rc = sqlite3_bind_text(pStmt, idx, argv[3], -1, SQLITE_TRANSIENT); |
| 3317 | }else if( strcmp(argv[4],"blob10")==0 ){ |
| 3318 | rc = sqlite3_bind_text(pStmt, idx, "abc\000xyz\000pq", 10, SQLITE_STATIC); |
| 3319 | }else{ |
| 3320 | Tcl_AppendResult(interp, "4th argument should be " |
| 3321 | "\"null\" or \"static\" or \"normal\"", 0); |
| 3322 | return TCL_ERROR; |
| 3323 | } |
| 3324 | if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR; |
| 3325 | if( rc ){ |
| 3326 | char zBuf[50]; |
| 3327 | sqlite3_snprintf(sizeof(zBuf), zBuf, "(%d) ", rc); |
| 3328 | Tcl_AppendResult(interp, zBuf, sqlite3ErrStr(rc), NULL); |
| 3329 | return TCL_ERROR; |
| 3330 | } |
| 3331 | return TCL_OK; |
| 3332 | } |
| 3333 | |
| 3334 | #ifndef SQLITE_OMIT_UTF16 |
| 3335 | /* |
| 3336 | ** Usage: add_test_collate <db ptr> <utf8> <utf16le> <utf16be> |
| 3337 | ** |
| 3338 | ** This function is used to test that SQLite selects the correct collation |
| 3339 | ** sequence callback when multiple versions (for different text encodings) |
| 3340 | ** are available. |
| 3341 | ** |
| 3342 | ** Calling this routine registers the collation sequence "test_collate" |
| 3343 | ** with database handle <db>. The second argument must be a list of three |
| 3344 | ** boolean values. If the first is true, then a version of test_collate is |
| 3345 | ** registered for UTF-8, if the second is true, a version is registered for |
| 3346 | ** UTF-16le, if the third is true, a UTF-16be version is available. |
| 3347 | ** Previous versions of test_collate are deleted. |
| 3348 | ** |
| 3349 | ** The collation sequence test_collate is implemented by calling the |
| 3350 | ** following TCL script: |
| 3351 | ** |
| 3352 | ** "test_collate <enc> <lhs> <rhs>" |
| 3353 | ** |
| 3354 | ** The <lhs> and <rhs> are the two values being compared, encoded in UTF-8. |
| 3355 | ** The <enc> parameter is the encoding of the collation function that |
| 3356 | ** SQLite selected to call. The TCL test script implements the |
| 3357 | ** "test_collate" proc. |
| 3358 | ** |
| 3359 | ** Note that this will only work with one interpreter at a time, as the |
| 3360 | ** interp pointer to use when evaluating the TCL script is stored in |
| 3361 | ** pTestCollateInterp. |
| 3362 | */ |
| 3363 | static Tcl_Interp* pTestCollateInterp; |
| 3364 | static int test_collate_func( |
| 3365 | void *pCtx, |
| 3366 | int nA, const void *zA, |
| 3367 | int nB, const void *zB |
| 3368 | ){ |
| 3369 | Tcl_Interp *i = pTestCollateInterp; |
| 3370 | int encin = SQLITE_PTR_TO_INT(pCtx); |
| 3371 | int res; |
| 3372 | int n; |
| 3373 | |
| 3374 | sqlite3_value *pVal; |
| 3375 | Tcl_Obj *pX; |
| 3376 | |
| 3377 | pX = Tcl_NewStringObj("test_collate", -1); |
| 3378 | Tcl_IncrRefCount(pX); |
| 3379 | |
| 3380 | switch( encin ){ |
| 3381 | case SQLITE_UTF8: |
| 3382 | Tcl_ListObjAppendElement(i,pX,Tcl_NewStringObj("UTF-8",-1)); |
| 3383 | break; |
| 3384 | case SQLITE_UTF16LE: |
| 3385 | Tcl_ListObjAppendElement(i,pX,Tcl_NewStringObj("UTF-16LE",-1)); |
| 3386 | break; |
| 3387 | case SQLITE_UTF16BE: |
| 3388 | Tcl_ListObjAppendElement(i,pX,Tcl_NewStringObj("UTF-16BE",-1)); |
| 3389 | break; |
| 3390 | default: |
| 3391 | assert(0); |
| 3392 | } |
| 3393 | |
| 3394 | sqlite3BeginBenignMalloc(); |
| 3395 | pVal = sqlite3ValueNew(0); |
| 3396 | if( pVal ){ |
| 3397 | sqlite3ValueSetStr(pVal, nA, zA, encin, SQLITE_STATIC); |
| 3398 | n = sqlite3_value_bytes(pVal); |
| 3399 | Tcl_ListObjAppendElement(i,pX, |
| 3400 | Tcl_NewStringObj((char*)sqlite3_value_text(pVal),n)); |
| 3401 | sqlite3ValueSetStr(pVal, nB, zB, encin, SQLITE_STATIC); |
| 3402 | n = sqlite3_value_bytes(pVal); |
| 3403 | Tcl_ListObjAppendElement(i,pX, |
| 3404 | Tcl_NewStringObj((char*)sqlite3_value_text(pVal),n)); |
| 3405 | sqlite3ValueFree(pVal); |
| 3406 | } |
| 3407 | sqlite3EndBenignMalloc(); |
| 3408 | |
| 3409 | Tcl_EvalObjEx(i, pX, 0); |
| 3410 | Tcl_DecrRefCount(pX); |
| 3411 | Tcl_GetIntFromObj(i, Tcl_GetObjResult(i), &res); |
| 3412 | return res; |
| 3413 | } |
| 3414 | static int SQLITE_TCLAPI test_collate( |
| 3415 | void * clientData, |
| 3416 | Tcl_Interp *interp, |
| 3417 | int objc, |
| 3418 | Tcl_Obj *CONST objv[] |
| 3419 | ){ |
| 3420 | sqlite3 *db; |
| 3421 | int val; |
| 3422 | sqlite3_value *pVal; |
| 3423 | int rc; |
| 3424 | |
| 3425 | if( objc!=5 ) goto bad_args; |
| 3426 | pTestCollateInterp = interp; |
| 3427 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 3428 | |
| 3429 | if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[2], &val) ) return TCL_ERROR; |
| 3430 | rc = sqlite3_create_collation(db, "test_collate", SQLITE_UTF8, |
| 3431 | (void *)SQLITE_UTF8, val?test_collate_func:0); |
| 3432 | if( rc==SQLITE_OK ){ |
| 3433 | const void *zUtf16; |
| 3434 | if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[3], &val) ) return TCL_ERROR; |
| 3435 | rc = sqlite3_create_collation(db, "test_collate", SQLITE_UTF16LE, |
| 3436 | (void *)SQLITE_UTF16LE, val?test_collate_func:0); |
| 3437 | if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[4], &val) ) return TCL_ERROR; |
| 3438 | |
| 3439 | #if 0 |
| 3440 | if( sqlite3_iMallocFail>0 ){ |
| 3441 | sqlite3_iMallocFail++; |
| 3442 | } |
| 3443 | #endif |
| 3444 | sqlite3_mutex_enter(db->mutex); |
| 3445 | pVal = sqlite3ValueNew(db); |
| 3446 | sqlite3ValueSetStr(pVal, -1, "test_collate", SQLITE_UTF8, SQLITE_STATIC); |
| 3447 | zUtf16 = sqlite3ValueText(pVal, SQLITE_UTF16NATIVE); |
| 3448 | if( db->mallocFailed ){ |
| 3449 | rc = SQLITE_NOMEM; |
| 3450 | }else{ |
| 3451 | rc = sqlite3_create_collation16(db, zUtf16, SQLITE_UTF16BE, |
| 3452 | (void *)SQLITE_UTF16BE, val?test_collate_func:0); |
| 3453 | } |
| 3454 | sqlite3ValueFree(pVal); |
| 3455 | sqlite3_mutex_leave(db->mutex); |
| 3456 | } |
| 3457 | if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; |
| 3458 | |
| 3459 | if( rc!=SQLITE_OK ){ |
| 3460 | Tcl_AppendResult(interp, sqlite3ErrName(rc), NULL); |
| 3461 | return TCL_ERROR; |
| 3462 | } |
| 3463 | return TCL_OK; |
| 3464 | |
| 3465 | bad_args: |
| 3466 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 3467 | Tcl_GetStringFromObj(objv[0], 0), " <DB> <utf8> <utf16le> <utf16be>", NULL); |
| 3468 | return TCL_ERROR; |
| 3469 | } |
| 3470 | |
| 3471 | /* |
| 3472 | ** Usage: add_test_utf16bin_collate <db ptr> |
| 3473 | ** |
| 3474 | ** Add a utf-16 collation sequence named "utf16bin" to the database |
| 3475 | ** handle. This collation sequence compares arguments in the same way as the |
| 3476 | ** built-in collation "binary". |
| 3477 | */ |
| 3478 | static int test_utf16bin_collate_func( |
| 3479 | void *pCtx, |
| 3480 | int nA, const void *zA, |
| 3481 | int nB, const void *zB |
| 3482 | ){ |
| 3483 | int nCmp = (nA>nB ? nB : nA); |
| 3484 | int res = memcmp(zA, zB, nCmp); |
| 3485 | if( res==0 ) res = nA - nB; |
| 3486 | return res; |
| 3487 | } |
| 3488 | static int SQLITE_TCLAPI test_utf16bin_collate( |
| 3489 | void * clientData, |
| 3490 | Tcl_Interp *interp, |
| 3491 | int objc, |
| 3492 | Tcl_Obj *CONST objv[] |
| 3493 | ){ |
| 3494 | sqlite3 *db; |
| 3495 | int rc; |
| 3496 | |
| 3497 | if( objc!=2 ) goto bad_args; |
| 3498 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 3499 | |
| 3500 | rc = sqlite3_create_collation(db, "utf16bin", SQLITE_UTF16, 0, |
| 3501 | test_utf16bin_collate_func |
| 3502 | ); |
| 3503 | if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; |
| 3504 | return TCL_OK; |
| 3505 | |
| 3506 | bad_args: |
| 3507 | Tcl_WrongNumArgs(interp, 1, objv, "DB"); |
| 3508 | return TCL_ERROR; |
| 3509 | } |
| 3510 | |
| 3511 | /* |
| 3512 | ** When the collation needed callback is invoked, record the name of |
| 3513 | ** the requested collating function here. The recorded name is linked |
| 3514 | ** to a TCL variable and used to make sure that the requested collation |
| 3515 | ** name is correct. |
| 3516 | */ |
| 3517 | static char zNeededCollation[200]; |
| 3518 | static char *pzNeededCollation = zNeededCollation; |
| 3519 | |
| 3520 | |
| 3521 | /* |
| 3522 | ** Called when a collating sequence is needed. Registered using |
| 3523 | ** sqlite3_collation_needed16(). |
| 3524 | */ |
| 3525 | static void test_collate_needed_cb( |
| 3526 | void *pCtx, |
| 3527 | sqlite3 *db, |
| 3528 | int eTextRep, |
| 3529 | const void *pName |
| 3530 | ){ |
| 3531 | int enc = ENC(db); |
| 3532 | int i; |
| 3533 | char *z; |
| 3534 | for(z = (char*)pName, i=0; *z || z[1]; z++){ |
| 3535 | if( *z ) zNeededCollation[i++] = *z; |
| 3536 | } |
| 3537 | zNeededCollation[i] = 0; |
| 3538 | sqlite3_create_collation( |
| 3539 | db, "test_collate", ENC(db), SQLITE_INT_TO_PTR(enc), test_collate_func); |
| 3540 | } |
| 3541 | |
| 3542 | /* |
| 3543 | ** Usage: add_test_collate_needed DB |
| 3544 | */ |
| 3545 | static int SQLITE_TCLAPI test_collate_needed( |
| 3546 | void * clientData, |
| 3547 | Tcl_Interp *interp, |
| 3548 | int objc, |
| 3549 | Tcl_Obj *CONST objv[] |
| 3550 | ){ |
| 3551 | sqlite3 *db; |
| 3552 | int rc; |
| 3553 | |
| 3554 | if( objc!=2 ) goto bad_args; |
| 3555 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 3556 | rc = sqlite3_collation_needed16(db, 0, test_collate_needed_cb); |
| 3557 | zNeededCollation[0] = 0; |
| 3558 | if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; |
| 3559 | return TCL_OK; |
| 3560 | |
| 3561 | bad_args: |
| 3562 | Tcl_WrongNumArgs(interp, 1, objv, "DB"); |
| 3563 | return TCL_ERROR; |
| 3564 | } |
| 3565 | |
| 3566 | /* |
| 3567 | ** tclcmd: add_alignment_test_collations DB |
| 3568 | ** |
| 3569 | ** Add two new collating sequences to the database DB |
| 3570 | ** |
| 3571 | ** utf16_aligned |
| 3572 | ** utf16_unaligned |
| 3573 | ** |
| 3574 | ** Both collating sequences use the same sort order as BINARY. |
| 3575 | ** The only difference is that the utf16_aligned collating |
| 3576 | ** sequence is declared with the SQLITE_UTF16_ALIGNED flag. |
| 3577 | ** Both collating functions increment the unaligned utf16 counter |
| 3578 | ** whenever they see a string that begins on an odd byte boundary. |
| 3579 | */ |
| 3580 | static int unaligned_string_counter = 0; |
| 3581 | static int alignmentCollFunc( |
| 3582 | void *NotUsed, |
| 3583 | int nKey1, const void *pKey1, |
| 3584 | int nKey2, const void *pKey2 |
| 3585 | ){ |
| 3586 | int rc, n; |
| 3587 | n = nKey1<nKey2 ? nKey1 : nKey2; |
| 3588 | if( nKey1>0 && 1==(1&(SQLITE_PTR_TO_INT(pKey1))) ) unaligned_string_counter++; |
| 3589 | if( nKey2>0 && 1==(1&(SQLITE_PTR_TO_INT(pKey2))) ) unaligned_string_counter++; |
| 3590 | rc = memcmp(pKey1, pKey2, n); |
| 3591 | if( rc==0 ){ |
| 3592 | rc = nKey1 - nKey2; |
| 3593 | } |
| 3594 | return rc; |
| 3595 | } |
| 3596 | static int SQLITE_TCLAPI add_alignment_test_collations( |
| 3597 | void * clientData, |
| 3598 | Tcl_Interp *interp, |
| 3599 | int objc, |
| 3600 | Tcl_Obj *CONST objv[] |
| 3601 | ){ |
| 3602 | sqlite3 *db; |
| 3603 | if( objc>=2 ){ |
| 3604 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 3605 | sqlite3_create_collation(db, "utf16_unaligned", SQLITE_UTF16, |
| 3606 | 0, alignmentCollFunc); |
| 3607 | sqlite3_create_collation(db, "utf16_aligned", SQLITE_UTF16_ALIGNED, |
| 3608 | 0, alignmentCollFunc); |
| 3609 | } |
| 3610 | return SQLITE_OK; |
| 3611 | } |
| 3612 | #endif /* !defined(SQLITE_OMIT_UTF16) */ |
| 3613 | |
| 3614 | /* |
| 3615 | ** Usage: add_test_function <db ptr> <utf8> <utf16le> <utf16be> |
| 3616 | ** |
| 3617 | ** This function is used to test that SQLite selects the correct user |
| 3618 | ** function callback when multiple versions (for different text encodings) |
| 3619 | ** are available. |
| 3620 | ** |
| 3621 | ** Calling this routine registers up to three versions of the user function |
| 3622 | ** "test_function" with database handle <db>. If the second argument is |
| 3623 | ** true, then a version of test_function is registered for UTF-8, if the |
| 3624 | ** third is true, a version is registered for UTF-16le, if the fourth is |
| 3625 | ** true, a UTF-16be version is available. Previous versions of |
| 3626 | ** test_function are deleted. |
| 3627 | ** |
| 3628 | ** The user function is implemented by calling the following TCL script: |
| 3629 | ** |
| 3630 | ** "test_function <enc> <arg>" |
| 3631 | ** |
| 3632 | ** Where <enc> is one of UTF-8, UTF-16LE or UTF16BE, and <arg> is the |
| 3633 | ** single argument passed to the SQL function. The value returned by |
| 3634 | ** the TCL script is used as the return value of the SQL function. It |
| 3635 | ** is passed to SQLite using UTF-16BE for a UTF-8 test_function(), UTF-8 |
| 3636 | ** for a UTF-16LE test_function(), and UTF-16LE for an implementation that |
| 3637 | ** prefers UTF-16BE. |
| 3638 | */ |
| 3639 | #ifndef SQLITE_OMIT_UTF16 |
| 3640 | static void test_function_utf8( |
| 3641 | sqlite3_context *pCtx, |
| 3642 | int nArg, |
| 3643 | sqlite3_value **argv |
| 3644 | ){ |
| 3645 | Tcl_Interp *interp; |
| 3646 | Tcl_Obj *pX; |
| 3647 | sqlite3_value *pVal; |
| 3648 | interp = (Tcl_Interp *)sqlite3_user_data(pCtx); |
| 3649 | pX = Tcl_NewStringObj("test_function", -1); |
| 3650 | Tcl_IncrRefCount(pX); |
| 3651 | Tcl_ListObjAppendElement(interp, pX, Tcl_NewStringObj("UTF-8", -1)); |
| 3652 | Tcl_ListObjAppendElement(interp, pX, |
| 3653 | Tcl_NewStringObj((char*)sqlite3_value_text(argv[0]), -1)); |
| 3654 | Tcl_EvalObjEx(interp, pX, 0); |
| 3655 | Tcl_DecrRefCount(pX); |
| 3656 | sqlite3_result_text(pCtx, Tcl_GetStringResult(interp), -1, SQLITE_TRANSIENT); |
| 3657 | pVal = sqlite3ValueNew(0); |
| 3658 | sqlite3ValueSetStr(pVal, -1, Tcl_GetStringResult(interp), |
| 3659 | SQLITE_UTF8, SQLITE_STATIC); |
| 3660 | sqlite3_result_text16be(pCtx, sqlite3_value_text16be(pVal), |
| 3661 | -1, SQLITE_TRANSIENT); |
| 3662 | sqlite3ValueFree(pVal); |
| 3663 | } |
| 3664 | static void test_function_utf16le( |
| 3665 | sqlite3_context *pCtx, |
| 3666 | int nArg, |
| 3667 | sqlite3_value **argv |
| 3668 | ){ |
| 3669 | Tcl_Interp *interp; |
| 3670 | Tcl_Obj *pX; |
| 3671 | sqlite3_value *pVal; |
| 3672 | interp = (Tcl_Interp *)sqlite3_user_data(pCtx); |
| 3673 | pX = Tcl_NewStringObj("test_function", -1); |
| 3674 | Tcl_IncrRefCount(pX); |
| 3675 | Tcl_ListObjAppendElement(interp, pX, Tcl_NewStringObj("UTF-16LE", -1)); |
| 3676 | Tcl_ListObjAppendElement(interp, pX, |
| 3677 | Tcl_NewStringObj((char*)sqlite3_value_text(argv[0]), -1)); |
| 3678 | Tcl_EvalObjEx(interp, pX, 0); |
| 3679 | Tcl_DecrRefCount(pX); |
| 3680 | pVal = sqlite3ValueNew(0); |
| 3681 | sqlite3ValueSetStr(pVal, -1, Tcl_GetStringResult(interp), |
| 3682 | SQLITE_UTF8, SQLITE_STATIC); |
| 3683 | sqlite3_result_text(pCtx,(char*)sqlite3_value_text(pVal),-1,SQLITE_TRANSIENT); |
| 3684 | sqlite3ValueFree(pVal); |
| 3685 | } |
| 3686 | static void test_function_utf16be( |
| 3687 | sqlite3_context *pCtx, |
| 3688 | int nArg, |
| 3689 | sqlite3_value **argv |
| 3690 | ){ |
| 3691 | Tcl_Interp *interp; |
| 3692 | Tcl_Obj *pX; |
| 3693 | sqlite3_value *pVal; |
| 3694 | interp = (Tcl_Interp *)sqlite3_user_data(pCtx); |
| 3695 | pX = Tcl_NewStringObj("test_function", -1); |
| 3696 | Tcl_IncrRefCount(pX); |
| 3697 | Tcl_ListObjAppendElement(interp, pX, Tcl_NewStringObj("UTF-16BE", -1)); |
| 3698 | Tcl_ListObjAppendElement(interp, pX, |
| 3699 | Tcl_NewStringObj((char*)sqlite3_value_text(argv[0]), -1)); |
| 3700 | Tcl_EvalObjEx(interp, pX, 0); |
| 3701 | Tcl_DecrRefCount(pX); |
| 3702 | pVal = sqlite3ValueNew(0); |
| 3703 | sqlite3ValueSetStr(pVal, -1, Tcl_GetStringResult(interp), |
| 3704 | SQLITE_UTF8, SQLITE_STATIC); |
| 3705 | sqlite3_result_text16(pCtx, sqlite3_value_text16le(pVal), |
| 3706 | -1, SQLITE_TRANSIENT); |
| 3707 | sqlite3_result_text16be(pCtx, sqlite3_value_text16le(pVal), |
| 3708 | -1, SQLITE_TRANSIENT); |
| 3709 | sqlite3_result_text16le(pCtx, sqlite3_value_text16le(pVal), |
| 3710 | -1, SQLITE_TRANSIENT); |
| 3711 | sqlite3ValueFree(pVal); |
| 3712 | } |
| 3713 | #endif /* SQLITE_OMIT_UTF16 */ |
| 3714 | static int SQLITE_TCLAPI test_function( |
| 3715 | void * clientData, |
| 3716 | Tcl_Interp *interp, |
| 3717 | int objc, |
| 3718 | Tcl_Obj *CONST objv[] |
| 3719 | ){ |
| 3720 | #ifndef SQLITE_OMIT_UTF16 |
| 3721 | sqlite3 *db; |
| 3722 | int val; |
| 3723 | |
| 3724 | if( objc!=5 ) goto bad_args; |
| 3725 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 3726 | |
| 3727 | if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[2], &val) ) return TCL_ERROR; |
| 3728 | if( val ){ |
| 3729 | sqlite3_create_function(db, "test_function", 1, SQLITE_UTF8, |
| 3730 | interp, test_function_utf8, 0, 0); |
| 3731 | } |
| 3732 | if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[3], &val) ) return TCL_ERROR; |
| 3733 | if( val ){ |
| 3734 | sqlite3_create_function(db, "test_function", 1, SQLITE_UTF16LE, |
| 3735 | interp, test_function_utf16le, 0, 0); |
| 3736 | } |
| 3737 | if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[4], &val) ) return TCL_ERROR; |
| 3738 | if( val ){ |
| 3739 | sqlite3_create_function(db, "test_function", 1, SQLITE_UTF16BE, |
| 3740 | interp, test_function_utf16be, 0, 0); |
| 3741 | } |
| 3742 | |
| 3743 | return TCL_OK; |
| 3744 | bad_args: |
| 3745 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 3746 | Tcl_GetStringFromObj(objv[0], 0), " <DB> <utf8> <utf16le> <utf16be>", NULL); |
| 3747 | #endif /* SQLITE_OMIT_UTF16 */ |
| 3748 | return TCL_ERROR; |
| 3749 | } |
| 3750 | |
| 3751 | /* |
| 3752 | ** Usage: sqlite3_test_errstr <err code> |
| 3753 | ** |
| 3754 | ** Test that the English language string equivalents for sqlite error codes |
| 3755 | ** are sane. The parameter is an integer representing an sqlite error code. |
| 3756 | ** The result is a list of two elements, the string representation of the |
| 3757 | ** error code and the English language explanation. |
| 3758 | */ |
| 3759 | static int SQLITE_TCLAPI test_errstr( |
| 3760 | void * clientData, |
| 3761 | Tcl_Interp *interp, |
| 3762 | int objc, |
| 3763 | Tcl_Obj *CONST objv[] |
| 3764 | ){ |
| 3765 | char *zCode; |
| 3766 | int i; |
| 3767 | if( objc!=1 ){ |
| 3768 | Tcl_WrongNumArgs(interp, 1, objv, "<error code>"); |
| 3769 | } |
| 3770 | |
| 3771 | zCode = Tcl_GetString(objv[1]); |
| 3772 | for(i=0; i<200; i++){ |
| 3773 | if( 0==strcmp(t1ErrorName(i), zCode) ) break; |
| 3774 | } |
| 3775 | Tcl_SetResult(interp, (char *)sqlite3ErrStr(i), 0); |
| 3776 | return TCL_OK; |
| 3777 | } |
| 3778 | |
| 3779 | /* |
| 3780 | ** Usage: breakpoint |
| 3781 | ** |
| 3782 | ** This routine exists for one purpose - to provide a place to put a |
| 3783 | ** breakpoint with GDB that can be triggered using TCL code. The use |
| 3784 | ** for this is when a particular test fails on (say) the 1485th iteration. |
| 3785 | ** In the TCL test script, we can add code like this: |
| 3786 | ** |
| 3787 | ** if {$i==1485} breakpoint |
| 3788 | ** |
| 3789 | ** Then run testfixture in the debugger and wait for the breakpoint to |
| 3790 | ** fire. Then additional breakpoints can be set to trace down the bug. |
| 3791 | */ |
| 3792 | static int SQLITE_TCLAPI test_breakpoint( |
| 3793 | void *NotUsed, |
| 3794 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 3795 | int argc, /* Number of arguments */ |
| 3796 | char **argv /* Text of each argument */ |
| 3797 | ){ |
| 3798 | return TCL_OK; /* Do nothing */ |
| 3799 | } |
| 3800 | |
| 3801 | /* |
| 3802 | ** Usage: sqlite3_bind_zeroblob STMT IDX N |
| 3803 | ** |
| 3804 | ** Test the sqlite3_bind_zeroblob interface. STMT is a prepared statement. |
| 3805 | ** IDX is the index of a wildcard in the prepared statement. This command |
| 3806 | ** binds a N-byte zero-filled BLOB to the wildcard. |
| 3807 | */ |
| 3808 | static int SQLITE_TCLAPI test_bind_zeroblob( |
| 3809 | void * clientData, |
| 3810 | Tcl_Interp *interp, |
| 3811 | int objc, |
| 3812 | Tcl_Obj *CONST objv[] |
| 3813 | ){ |
| 3814 | sqlite3_stmt *pStmt; |
| 3815 | int idx; |
| 3816 | int n; |
| 3817 | int rc; |
| 3818 | |
| 3819 | if( objc!=4 ){ |
| 3820 | Tcl_WrongNumArgs(interp, 1, objv, "STMT IDX N"); |
| 3821 | return TCL_ERROR; |
| 3822 | } |
| 3823 | |
| 3824 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 3825 | if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR; |
| 3826 | if( Tcl_GetIntFromObj(interp, objv[3], &n) ) return TCL_ERROR; |
| 3827 | |
| 3828 | rc = sqlite3_bind_zeroblob(pStmt, idx, n); |
| 3829 | if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR; |
| 3830 | if( rc!=SQLITE_OK ){ |
| 3831 | return TCL_ERROR; |
| 3832 | } |
| 3833 | |
| 3834 | return TCL_OK; |
| 3835 | } |
| 3836 | |
| 3837 | /* |
| 3838 | ** Usage: sqlite3_bind_zeroblob64 STMT IDX N |
| 3839 | ** |
| 3840 | ** Test the sqlite3_bind_zeroblob64 interface. STMT is a prepared statement. |
| 3841 | ** IDX is the index of a wildcard in the prepared statement. This command |
| 3842 | ** binds a N-byte zero-filled BLOB to the wildcard. |
| 3843 | */ |
| 3844 | static int SQLITE_TCLAPI test_bind_zeroblob64( |
| 3845 | void * clientData, |
| 3846 | Tcl_Interp *interp, |
| 3847 | int objc, |
| 3848 | Tcl_Obj *CONST objv[] |
| 3849 | ){ |
| 3850 | sqlite3_stmt *pStmt; |
| 3851 | int idx; |
| 3852 | Tcl_WideInt n; |
| 3853 | int rc; |
| 3854 | |
| 3855 | if( objc!=4 ){ |
| 3856 | Tcl_WrongNumArgs(interp, 1, objv, "STMT IDX N"); |
| 3857 | return TCL_ERROR; |
| 3858 | } |
| 3859 | |
| 3860 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 3861 | if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR; |
| 3862 | if( Tcl_GetWideIntFromObj(interp, objv[3], &n) ) return TCL_ERROR; |
| 3863 | |
| 3864 | rc = sqlite3_bind_zeroblob64(pStmt, idx, n); |
| 3865 | if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR; |
| 3866 | if( rc!=SQLITE_OK ){ |
| 3867 | Tcl_AppendResult(interp, sqlite3ErrName(rc), NULL); |
| 3868 | return TCL_ERROR; |
| 3869 | } |
| 3870 | |
| 3871 | return TCL_OK; |
| 3872 | } |
| 3873 | |
| 3874 | /* |
| 3875 | ** Usage: sqlite3_bind_int STMT N VALUE |
| 3876 | ** |
| 3877 | ** Test the sqlite3_bind_int interface. STMT is a prepared statement. |
| 3878 | ** N is the index of a wildcard in the prepared statement. This command |
| 3879 | ** binds a 32-bit integer VALUE to that wildcard. |
| 3880 | */ |
| 3881 | static int SQLITE_TCLAPI test_bind_int( |
| 3882 | void * clientData, |
| 3883 | Tcl_Interp *interp, |
| 3884 | int objc, |
| 3885 | Tcl_Obj *CONST objv[] |
| 3886 | ){ |
| 3887 | sqlite3_stmt *pStmt; |
| 3888 | int idx; |
| 3889 | int value; |
| 3890 | int rc; |
| 3891 | |
| 3892 | if( objc!=4 ){ |
| 3893 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 3894 | Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE", NULL); |
| 3895 | return TCL_ERROR; |
| 3896 | } |
| 3897 | |
| 3898 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 3899 | if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR; |
| 3900 | if( Tcl_GetIntFromObj(interp, objv[3], &value) ) return TCL_ERROR; |
| 3901 | |
| 3902 | rc = sqlite3_bind_int(pStmt, idx, value); |
| 3903 | if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR; |
| 3904 | if( rc!=SQLITE_OK ){ |
| 3905 | return TCL_ERROR; |
| 3906 | } |
| 3907 | |
| 3908 | return TCL_OK; |
| 3909 | } |
| 3910 | |
| 3911 | |
| 3912 | /* |
| 3913 | ** Usage: intarray_addr INT ... |
| 3914 | ** |
| 3915 | ** Return the address of a C-language array of 32-bit integers. |
| 3916 | ** |
| 3917 | ** Space to hold the array is obtained from malloc(). Call this procedure once |
| 3918 | ** with no arguments in order to release memory. Each call to this procedure |
| 3919 | ** overwrites the previous array. |
| 3920 | */ |
| 3921 | static int SQLITE_TCLAPI test_intarray_addr( |
| 3922 | void * clientData, |
| 3923 | Tcl_Interp *interp, |
| 3924 | int objc, |
| 3925 | Tcl_Obj *CONST objv[] |
| 3926 | ){ |
| 3927 | int i; |
| 3928 | static int *p = 0; |
| 3929 | |
| 3930 | sqlite3_free(p); |
| 3931 | p = 0; |
| 3932 | if( objc>1 ){ |
| 3933 | p = sqlite3_malloc( sizeof(p[0])*(objc-1) ); |
| 3934 | if( p==0 ) return TCL_ERROR; |
| 3935 | for(i=0; i<objc-1; i++){ |
| 3936 | if( Tcl_GetIntFromObj(interp, objv[1+i], &p[i]) ){ |
| 3937 | sqlite3_free(p); |
| 3938 | p = 0; |
| 3939 | return TCL_ERROR; |
| 3940 | } |
| 3941 | } |
| 3942 | } |
| 3943 | Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3TestPtrToText(p), -1)); |
| 3944 | return TCL_OK; |
| 3945 | } |
| 3946 | /* |
| 3947 | ** Usage: int64array_addr INT ... |
| 3948 | ** |
| 3949 | ** Return the address of a C-language array of 32-bit integers. |
| 3950 | ** |
| 3951 | ** Space to hold the array is obtained from malloc(). Call this procedure once |
| 3952 | ** with no arguments in order to release memory. Each call to this procedure |
| 3953 | ** overwrites the previous array. |
| 3954 | */ |
| 3955 | static int SQLITE_TCLAPI test_int64array_addr( |
| 3956 | void * clientData, |
| 3957 | Tcl_Interp *interp, |
| 3958 | int objc, |
| 3959 | Tcl_Obj *CONST objv[] |
| 3960 | ){ |
| 3961 | int i; |
| 3962 | static sqlite3_int64 *p = 0; |
| 3963 | |
| 3964 | sqlite3_free(p); |
| 3965 | p = 0; |
| 3966 | if( objc>1 ){ |
| 3967 | p = sqlite3_malloc( sizeof(p[0])*(objc-1) ); |
| 3968 | if( p==0 ) return TCL_ERROR; |
| 3969 | for(i=0; i<objc-1; i++){ |
| 3970 | Tcl_WideInt v; |
| 3971 | if( Tcl_GetWideIntFromObj(interp, objv[1+i], &v) ){ |
| 3972 | sqlite3_free(p); |
| 3973 | p = 0; |
| 3974 | return TCL_ERROR; |
| 3975 | } |
| 3976 | p[i] = v; |
| 3977 | } |
| 3978 | } |
| 3979 | Tcl_SetObjResult(interp, Tcl_NewStringObj(testPointerToString(p), -1)); |
| 3980 | return TCL_OK; |
| 3981 | } |
| 3982 | /* |
| 3983 | ** Usage: doublearray_addr INT ... |
| 3984 | ** |
| 3985 | ** Return the address of a C-language array of doubles. |
| 3986 | ** |
| 3987 | ** Space to hold the array is obtained from malloc(). Call this procedure once |
| 3988 | ** with no arguments in order to release memory. Each call to this procedure |
| 3989 | ** overwrites the previous array. |
| 3990 | */ |
| 3991 | static int SQLITE_TCLAPI test_doublearray_addr( |
| 3992 | void * clientData, |
| 3993 | Tcl_Interp *interp, |
| 3994 | int objc, |
| 3995 | Tcl_Obj *CONST objv[] |
| 3996 | ){ |
| 3997 | int i; |
| 3998 | static double *p = 0; |
| 3999 | |
| 4000 | sqlite3_free(p); |
| 4001 | p = 0; |
| 4002 | if( objc>1 ){ |
| 4003 | p = sqlite3_malloc( sizeof(p[0])*(objc-1) ); |
| 4004 | if( p==0 ) return TCL_ERROR; |
| 4005 | for(i=0; i<objc-1; i++){ |
| 4006 | if( Tcl_GetDoubleFromObj(interp, objv[1+i], &p[i]) ){ |
| 4007 | sqlite3_free(p); |
| 4008 | p = 0; |
| 4009 | return TCL_ERROR; |
| 4010 | } |
| 4011 | } |
| 4012 | } |
| 4013 | Tcl_SetObjResult(interp, Tcl_NewStringObj(testPointerToString(p), -1)); |
| 4014 | return TCL_OK; |
| 4015 | } |
| 4016 | /* |
| 4017 | ** Usage: textarray_addr TEXT ... |
| 4018 | ** |
| 4019 | ** Return the address of a C-language array of strings. |
| 4020 | ** |
| 4021 | ** Space to hold the array is obtained from malloc(). Call this procedure once |
| 4022 | ** with no arguments in order to release memory. Each call to this procedure |
| 4023 | ** overwrites the previous array. |
| 4024 | */ |
| 4025 | static int SQLITE_TCLAPI test_textarray_addr( |
| 4026 | void * clientData, |
| 4027 | Tcl_Interp *interp, |
| 4028 | int objc, |
| 4029 | Tcl_Obj *CONST objv[] |
| 4030 | ){ |
| 4031 | int i; |
| 4032 | static int n = 0; |
| 4033 | static char **p = 0; |
| 4034 | |
| 4035 | for(i=0; i<n; i++) sqlite3_free(p[i]); |
| 4036 | sqlite3_free(p); |
| 4037 | p = 0; |
| 4038 | if( objc>1 ){ |
| 4039 | p = sqlite3_malloc( sizeof(p[0])*(objc-1) ); |
| 4040 | if( p==0 ) return TCL_ERROR; |
| 4041 | for(i=0; i<objc-1; i++){ |
| 4042 | p[i] = sqlite3_mprintf("%s", Tcl_GetString(objv[1+i])); |
| 4043 | } |
| 4044 | } |
| 4045 | n = objc-1; |
| 4046 | Tcl_SetObjResult(interp, Tcl_NewStringObj(testPointerToString(p), -1)); |
| 4047 | return TCL_OK; |
| 4048 | } |
| 4049 | |
| 4050 | /* |
| 4051 | ** Usage: sqlite3_bind_int64 STMT N VALUE |
| 4052 | ** |
| 4053 | ** Test the sqlite3_bind_int64 interface. STMT is a prepared statement. |
| 4054 | ** N is the index of a wildcard in the prepared statement. This command |
| 4055 | ** binds a 64-bit integer VALUE to that wildcard. |
| 4056 | */ |
| 4057 | static int SQLITE_TCLAPI test_bind_int64( |
| 4058 | void * clientData, |
| 4059 | Tcl_Interp *interp, |
| 4060 | int objc, |
| 4061 | Tcl_Obj *CONST objv[] |
| 4062 | ){ |
| 4063 | sqlite3_stmt *pStmt; |
| 4064 | int idx; |
| 4065 | Tcl_WideInt value; |
| 4066 | int rc; |
| 4067 | |
| 4068 | if( objc!=4 ){ |
| 4069 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 4070 | Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE", NULL); |
| 4071 | return TCL_ERROR; |
| 4072 | } |
| 4073 | |
| 4074 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 4075 | if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR; |
| 4076 | if( Tcl_GetWideIntFromObj(interp, objv[3], &value) ) return TCL_ERROR; |
| 4077 | |
| 4078 | rc = sqlite3_bind_int64(pStmt, idx, value); |
| 4079 | if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR; |
| 4080 | if( rc!=SQLITE_OK ){ |
| 4081 | return TCL_ERROR; |
| 4082 | } |
| 4083 | |
| 4084 | return TCL_OK; |
| 4085 | } |
| 4086 | |
| 4087 | |
| 4088 | /* |
| 4089 | ** Usage: sqlite3_bind_double STMT N VALUE |
| 4090 | ** |
| 4091 | ** Test the sqlite3_bind_double interface. STMT is a prepared statement. |
| 4092 | ** N is the index of a wildcard in the prepared statement. This command |
| 4093 | ** binds a 64-bit integer VALUE to that wildcard. |
| 4094 | */ |
| 4095 | static int SQLITE_TCLAPI test_bind_double( |
| 4096 | void * clientData, |
| 4097 | Tcl_Interp *interp, |
| 4098 | int objc, |
| 4099 | Tcl_Obj *CONST objv[] |
| 4100 | ){ |
| 4101 | sqlite3_stmt *pStmt; |
| 4102 | int idx; |
| 4103 | double value = 0; |
| 4104 | int rc; |
| 4105 | const char *zVal; |
| 4106 | int i; |
| 4107 | static const struct { |
| 4108 | const char *zName; /* Name of the special floating point value */ |
| 4109 | unsigned int iUpper; /* Upper 32 bits */ |
| 4110 | unsigned int iLower; /* Lower 32 bits */ |
| 4111 | } aSpecialFp[] = { |
| 4112 | { "NaN", 0x7fffffff, 0xffffffff }, |
| 4113 | { "SNaN", 0x7ff7ffff, 0xffffffff }, |
| 4114 | { "-NaN", 0xffffffff, 0xffffffff }, |
| 4115 | { "-SNaN", 0xfff7ffff, 0xffffffff }, |
| 4116 | { "+Inf", 0x7ff00000, 0x00000000 }, |
| 4117 | { "-Inf", 0xfff00000, 0x00000000 }, |
| 4118 | { "Epsilon", 0x00000000, 0x00000001 }, |
| 4119 | { "-Epsilon", 0x80000000, 0x00000001 }, |
| 4120 | { "NaN0", 0x7ff80000, 0x00000000 }, |
| 4121 | { "-NaN0", 0xfff80000, 0x00000000 }, |
| 4122 | }; |
| 4123 | |
| 4124 | if( objc!=4 ){ |
| 4125 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 4126 | Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE", NULL); |
| 4127 | return TCL_ERROR; |
| 4128 | } |
| 4129 | |
| 4130 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 4131 | if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR; |
| 4132 | |
| 4133 | /* Intercept the string "NaN" and generate a NaN value for it. |
| 4134 | ** All other strings are passed through to Tcl_GetDoubleFromObj(). |
| 4135 | ** Tcl_GetDoubleFromObj() should understand "NaN" but some versions |
| 4136 | ** contain a bug. |
| 4137 | */ |
| 4138 | zVal = Tcl_GetString(objv[3]); |
| 4139 | for(i=0; i<sizeof(aSpecialFp)/sizeof(aSpecialFp[0]); i++){ |
| 4140 | if( strcmp(aSpecialFp[i].zName, zVal)==0 ){ |
| 4141 | sqlite3_uint64 x; |
| 4142 | x = aSpecialFp[i].iUpper; |
| 4143 | x <<= 32; |
| 4144 | x |= aSpecialFp[i].iLower; |
| 4145 | assert( sizeof(value)==8 ); |
| 4146 | assert( sizeof(x)==8 ); |
| 4147 | memcpy(&value, &x, 8); |
| 4148 | break; |
| 4149 | } |
| 4150 | } |
| 4151 | if( i>=sizeof(aSpecialFp)/sizeof(aSpecialFp[0]) && |
| 4152 | Tcl_GetDoubleFromObj(interp, objv[3], &value) ){ |
| 4153 | return TCL_ERROR; |
| 4154 | } |
| 4155 | rc = sqlite3_bind_double(pStmt, idx, value); |
| 4156 | if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR; |
| 4157 | if( rc!=SQLITE_OK ){ |
| 4158 | return TCL_ERROR; |
| 4159 | } |
| 4160 | |
| 4161 | return TCL_OK; |
| 4162 | } |
| 4163 | |
| 4164 | /* |
| 4165 | ** Usage: sqlite3_bind_null STMT N |
| 4166 | ** |
| 4167 | ** Test the sqlite3_bind_null interface. STMT is a prepared statement. |
| 4168 | ** N is the index of a wildcard in the prepared statement. This command |
| 4169 | ** binds a NULL to the wildcard. |
| 4170 | */ |
| 4171 | static int SQLITE_TCLAPI test_bind_null( |
| 4172 | void * clientData, |
| 4173 | Tcl_Interp *interp, |
| 4174 | int objc, |
| 4175 | Tcl_Obj *CONST objv[] |
| 4176 | ){ |
| 4177 | sqlite3_stmt *pStmt; |
| 4178 | int idx; |
| 4179 | int rc; |
| 4180 | |
| 4181 | if( objc!=3 ){ |
| 4182 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 4183 | Tcl_GetStringFromObj(objv[0], 0), " STMT N", NULL); |
| 4184 | return TCL_ERROR; |
| 4185 | } |
| 4186 | |
| 4187 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 4188 | if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR; |
| 4189 | |
| 4190 | rc = sqlite3_bind_null(pStmt, idx); |
| 4191 | if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR; |
| 4192 | if( rc!=SQLITE_OK ){ |
| 4193 | return TCL_ERROR; |
| 4194 | } |
| 4195 | |
| 4196 | return TCL_OK; |
| 4197 | } |
| 4198 | |
| 4199 | /* |
| 4200 | ** Usage: sqlite3_bind_text STMT N STRING BYTES |
| 4201 | ** |
| 4202 | ** Test the sqlite3_bind_text interface. STMT is a prepared statement. |
| 4203 | ** N is the index of a wildcard in the prepared statement. This command |
| 4204 | ** binds a UTF-8 string STRING to the wildcard. The string is BYTES bytes |
| 4205 | ** long. |
| 4206 | */ |
| 4207 | static int SQLITE_TCLAPI test_bind_text( |
| 4208 | void * clientData, |
| 4209 | Tcl_Interp *interp, |
| 4210 | int objc, |
| 4211 | Tcl_Obj *CONST objv[] |
| 4212 | ){ |
| 4213 | sqlite3_stmt *pStmt; |
| 4214 | int idx; |
| 4215 | Tcl_Size trueLength = 0; |
| 4216 | int bytes; |
| 4217 | char *value; |
| 4218 | int rc; |
| 4219 | char *toFree = 0; |
| 4220 | |
| 4221 | if( objc!=5 ){ |
| 4222 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 4223 | Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE BYTES", NULL); |
| 4224 | return TCL_ERROR; |
| 4225 | } |
| 4226 | |
| 4227 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 4228 | if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR; |
| 4229 | value = (char*)Tcl_GetByteArrayFromObj(objv[3], &trueLength); |
| 4230 | if( Tcl_GetIntFromObj(interp, objv[4], &bytes) ) return TCL_ERROR; |
| 4231 | if( bytes<0 ){ |
| 4232 | toFree = malloc( trueLength + 1 ); |
| 4233 | if( toFree==0 ){ |
| 4234 | Tcl_AppendResult(interp, "out of memory", (void*)0); |
| 4235 | return TCL_ERROR; |
| 4236 | } |
| 4237 | memcpy(toFree, value, trueLength); |
| 4238 | toFree[trueLength] = 0; |
| 4239 | value = toFree; |
| 4240 | } |
| 4241 | rc = sqlite3_bind_text(pStmt, idx, value, bytes, SQLITE_TRANSIENT); |
| 4242 | free(toFree); |
| 4243 | if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR; |
| 4244 | if( rc!=SQLITE_OK ){ |
| 4245 | Tcl_AppendResult(interp, sqlite3ErrName(rc), (void*)0); |
| 4246 | return TCL_ERROR; |
| 4247 | } |
| 4248 | |
| 4249 | return TCL_OK; |
| 4250 | } |
| 4251 | |
| 4252 | /* |
| 4253 | ** Usage: sqlite3_bind_text16 ?-static? STMT N STRING BYTES |
| 4254 | ** |
| 4255 | ** Test the sqlite3_bind_text16 interface. STMT is a prepared statement. |
| 4256 | ** N is the index of a wildcard in the prepared statement. This command |
| 4257 | ** binds a UTF-16 string STRING to the wildcard. The string is BYTES bytes |
| 4258 | ** long. |
| 4259 | */ |
| 4260 | static int SQLITE_TCLAPI test_bind_text16( |
| 4261 | void * clientData, |
| 4262 | Tcl_Interp *interp, |
| 4263 | int objc, |
| 4264 | Tcl_Obj *CONST objv[] |
| 4265 | ){ |
| 4266 | #ifndef SQLITE_OMIT_UTF16 |
| 4267 | sqlite3_stmt *pStmt; |
| 4268 | int idx; |
| 4269 | int bytes; |
| 4270 | char *value; |
| 4271 | char *toFree = 0; |
| 4272 | int rc; |
| 4273 | Tcl_Size trueLength = 0; |
| 4274 | |
| 4275 | void (*xDel)(void*) = (objc==6?SQLITE_STATIC:SQLITE_TRANSIENT); |
| 4276 | Tcl_Obj *oStmt = objv[objc-4]; |
| 4277 | Tcl_Obj *oN = objv[objc-3]; |
| 4278 | Tcl_Obj *oString = objv[objc-2]; |
| 4279 | Tcl_Obj *oBytes = objv[objc-1]; |
| 4280 | |
| 4281 | if( objc!=5 && objc!=6){ |
| 4282 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 4283 | Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE BYTES", NULL); |
| 4284 | return TCL_ERROR; |
| 4285 | } |
| 4286 | |
| 4287 | if( getStmtPointer(interp, Tcl_GetString(oStmt), &pStmt) ) return TCL_ERROR; |
| 4288 | if( Tcl_GetIntFromObj(interp, oN, &idx) ) return TCL_ERROR; |
| 4289 | value = (char*)Tcl_GetByteArrayFromObj(oString, &trueLength); |
| 4290 | if( Tcl_GetIntFromObj(interp, oBytes, &bytes) ) return TCL_ERROR; |
| 4291 | if( bytes<0 && xDel==SQLITE_TRANSIENT ){ |
| 4292 | toFree = malloc( trueLength + 3 ); |
| 4293 | if( toFree==0 ){ |
| 4294 | Tcl_AppendResult(interp, "out of memory", (void*)0); |
| 4295 | return TCL_ERROR; |
| 4296 | } |
| 4297 | memcpy(toFree, value, trueLength); |
| 4298 | memset(toFree+trueLength, 0, 3); |
| 4299 | value = toFree; |
| 4300 | } |
| 4301 | rc = sqlite3_bind_text16(pStmt, idx, (void *)value, bytes, xDel); |
| 4302 | free(toFree); |
| 4303 | if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR; |
| 4304 | if( rc!=SQLITE_OK ){ |
| 4305 | Tcl_AppendResult(interp, sqlite3ErrName(rc), NULL); |
| 4306 | return TCL_ERROR; |
| 4307 | } |
| 4308 | |
| 4309 | #endif /* SQLITE_OMIT_UTF16 */ |
| 4310 | return TCL_OK; |
| 4311 | } |
| 4312 | |
| 4313 | /* |
| 4314 | ** Usage: sqlite3_bind_blob ?-static? STMT N DATA BYTES |
| 4315 | ** |
| 4316 | ** Test the sqlite3_bind_blob interface. STMT is a prepared statement. |
| 4317 | ** N is the index of a wildcard in the prepared statement. This command |
| 4318 | ** binds a BLOB to the wildcard. The BLOB is BYTES bytes in size. |
| 4319 | */ |
| 4320 | static int SQLITE_TCLAPI test_bind_blob( |
| 4321 | void * clientData, |
| 4322 | Tcl_Interp *interp, |
| 4323 | int objc, |
| 4324 | Tcl_Obj *CONST objv[] |
| 4325 | ){ |
| 4326 | sqlite3_stmt *pStmt; |
| 4327 | Tcl_Size len; |
| 4328 | int idx; |
| 4329 | int bytes; |
| 4330 | char *value; |
| 4331 | int rc; |
| 4332 | sqlite3_destructor_type xDestructor = SQLITE_TRANSIENT; |
| 4333 | |
| 4334 | if( objc!=5 && objc!=6 ){ |
| 4335 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 4336 | Tcl_GetStringFromObj(objv[0], 0), " STMT N DATA BYTES", NULL); |
| 4337 | return TCL_ERROR; |
| 4338 | } |
| 4339 | |
| 4340 | if( objc==6 ){ |
| 4341 | xDestructor = SQLITE_STATIC; |
| 4342 | objv++; |
| 4343 | } |
| 4344 | |
| 4345 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 4346 | if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR; |
| 4347 | |
| 4348 | value = (char*)Tcl_GetByteArrayFromObj(objv[3], &len); |
| 4349 | if( Tcl_GetIntFromObj(interp, objv[4], &bytes) ) return TCL_ERROR; |
| 4350 | |
| 4351 | if( bytes>len ){ |
| 4352 | char zBuf[200]; |
| 4353 | sqlite3_snprintf(sizeof(zBuf), zBuf, |
| 4354 | "cannot use %d blob bytes, have %d", bytes, (int)len); |
| 4355 | Tcl_AppendResult(interp, zBuf, (char*)0); |
| 4356 | return TCL_ERROR; |
| 4357 | } |
| 4358 | |
| 4359 | rc = sqlite3_bind_blob(pStmt, idx, value, bytes, xDestructor); |
| 4360 | if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR; |
| 4361 | if( rc!=SQLITE_OK ){ |
| 4362 | return TCL_ERROR; |
| 4363 | } |
| 4364 | |
| 4365 | return TCL_OK; |
| 4366 | } |
| 4367 | |
| 4368 | /* |
| 4369 | ** Usage: sqlite3_bind_value_from_preupdate STMT N NEW|OLD IDX |
| 4370 | ** |
| 4371 | ** Test the sqlite3_bind_value interface using sqlite3_value objects |
| 4372 | ** obtained from either sqlite3_preupdate_new() (if arg[3]=="new") or |
| 4373 | ** sqlite3_preupdate_old() if (arg[3]=="old"). IDX is the index to |
| 4374 | ** pass to the sqlite3_preupdate_xxx() function. |
| 4375 | */ |
| 4376 | static int SQLITE_TCLAPI test_bind_value_from_preupdate( |
| 4377 | void * clientData, |
| 4378 | Tcl_Interp *interp, |
| 4379 | int objc, |
| 4380 | Tcl_Obj *CONST objv[] |
| 4381 | ){ |
| 4382 | sqlite3_stmt *pStmt; |
| 4383 | int idx; |
| 4384 | int bidx; |
| 4385 | #ifdef SQLITE_ENABLE_PREUPDATE_HOOK |
| 4386 | const char *z3 = 0; |
| 4387 | sqlite3 *db = 0; |
| 4388 | sqlite3_value *pVal = 0; |
| 4389 | #endif |
| 4390 | |
| 4391 | if( objc!=5 ){ |
| 4392 | Tcl_WrongNumArgs(interp, 1, objv, "STMT N NEW|OLD IDX"); |
| 4393 | return TCL_ERROR; |
| 4394 | } |
| 4395 | |
| 4396 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 4397 | if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR; |
| 4398 | if( Tcl_GetIntFromObj(interp, objv[4], &bidx) ) return TCL_ERROR; |
| 4399 | |
| 4400 | #ifdef SQLITE_ENABLE_PREUPDATE_HOOK |
| 4401 | z3 = Tcl_GetString(objv[3]); |
| 4402 | db = sqlite3_db_handle(pStmt); |
| 4403 | if( z3[0]=='n' ){ |
| 4404 | sqlite3_preupdate_new(db, bidx, &pVal); |
| 4405 | }else if( z3[0]=='o' ){ |
| 4406 | sqlite3_preupdate_old(db, bidx, &pVal); |
| 4407 | }else{ |
| 4408 | Tcl_AppendResult(interp, "expected new or old, got: ", z3, (char*)0); |
| 4409 | return TCL_ERROR; |
| 4410 | } |
| 4411 | sqlite3_bind_value(pStmt, idx, pVal); |
| 4412 | #endif |
| 4413 | |
| 4414 | return TCL_OK; |
| 4415 | } |
| 4416 | |
| 4417 | /* |
| 4418 | ** Usage: sqlite3_bind_value_from_select STMT N SELECT |
| 4419 | ** |
| 4420 | ** Test the sqlite3_bind_value interface. STMT is a prepared statement. |
| 4421 | ** N is the index of a wildcard in the prepared statement. |
| 4422 | */ |
| 4423 | static int SQLITE_TCLAPI test_bind_value_from_select( |
| 4424 | void * clientData, |
| 4425 | Tcl_Interp *interp, |
| 4426 | int objc, |
| 4427 | Tcl_Obj *CONST objv[] |
| 4428 | ){ |
| 4429 | sqlite3_stmt *pStmt; |
| 4430 | sqlite3_stmt *pStmt2; |
| 4431 | int idx; |
| 4432 | const char *zSql = 0; |
| 4433 | sqlite3 *db = 0; |
| 4434 | int rc = SQLITE_OK; |
| 4435 | |
| 4436 | if( objc!=4 ){ |
| 4437 | Tcl_WrongNumArgs(interp, 1, objv, "STMT N SELECT"); |
| 4438 | return TCL_ERROR; |
| 4439 | } |
| 4440 | |
| 4441 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 4442 | if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR; |
| 4443 | zSql = Tcl_GetString(objv[3]); |
| 4444 | db = sqlite3_db_handle(pStmt); |
| 4445 | |
| 4446 | rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt2, 0); |
| 4447 | if( rc!=SQLITE_OK ){ |
| 4448 | Tcl_AppendResult(interp, "error in SQL: ", sqlite3_errmsg(db), (char*)0); |
| 4449 | return TCL_ERROR; |
| 4450 | } |
| 4451 | if( sqlite3_step(pStmt2)==SQLITE_ROW ){ |
| 4452 | sqlite3_value *pVal = sqlite3_column_value(pStmt2, 0); |
| 4453 | sqlite3_bind_value(pStmt, idx, pVal); |
| 4454 | } |
| 4455 | rc = sqlite3_finalize(pStmt2); |
| 4456 | if( rc!=SQLITE_OK ){ |
| 4457 | Tcl_AppendResult(interp, |
| 4458 | "error runnning SQL: ", sqlite3_errmsg(db), (char*)0 |
| 4459 | ); |
| 4460 | return TCL_ERROR; |
| 4461 | } |
| 4462 | |
| 4463 | return TCL_OK; |
| 4464 | } |
| 4465 | |
| 4466 | #ifdef _WIN32 |
| 4467 | struct iovec { |
| 4468 | void *iov_base; |
| 4469 | size_t iov_len; |
| 4470 | }; |
| 4471 | #else |
| 4472 | # include <sys/uio.h> |
| 4473 | #endif |
| 4474 | |
| 4475 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 4476 | |
| 4477 | /* |
| 4478 | ** These two are used by the -malloc option to sqlite3_carray_bind() |
| 4479 | */ |
| 4480 | static void *testCarrayAlloc(int n){ |
| 4481 | u8 *pRet = (u8*)sqlite3_malloc(n+16); |
| 4482 | if( pRet ){ |
| 4483 | pRet = &pRet[16]; |
| 4484 | } |
| 4485 | return (void*)pRet; |
| 4486 | } |
| 4487 | static void testCarrayFree(void *p){ |
| 4488 | if( p ){ |
| 4489 | u8 *p2 = (u8*)p; |
| 4490 | sqlite3_free(&p2[-16]); |
| 4491 | } |
| 4492 | } |
| 4493 | |
| 4494 | static void delIntptr(void *p){ |
| 4495 | ckfree(p); |
| 4496 | } |
| 4497 | |
| 4498 | /* |
| 4499 | ** bind_carray_intptr STMT IPARAM INT-0 INT-1 INT-2... |
| 4500 | */ |
| 4501 | static int SQLITE_TCLAPI bind_carray_intptr( |
| 4502 | void * clientData, |
| 4503 | Tcl_Interp *interp, |
| 4504 | int objc, |
| 4505 | Tcl_Obj *CONST objv[] |
| 4506 | ){ |
| 4507 | sqlite3_stmt *pStmt = 0; |
| 4508 | int iVar = 0; |
| 4509 | int *aInt = 0; |
| 4510 | int nInt = 0; |
| 4511 | int ii = 0; |
| 4512 | int rc = SQLITE_OK; |
| 4513 | |
| 4514 | if( objc<3 ){ |
| 4515 | Tcl_WrongNumArgs(interp, 1, objv, "STMT"); |
| 4516 | return TCL_ERROR; |
| 4517 | } |
| 4518 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 4519 | if( Tcl_GetIntFromObj(interp, objv[2], &iVar) ) return TCL_ERROR; |
| 4520 | nInt = objc - 3; |
| 4521 | |
| 4522 | aInt = ckalloc((nInt+1) * sizeof(int)); |
| 4523 | for(ii=0; ii<nInt; ii++){ |
| 4524 | if( Tcl_GetIntFromObj(interp, objv[3+ii], &aInt[ii]) ){ |
| 4525 | ckfree(aInt); |
| 4526 | return TCL_ERROR; |
| 4527 | } |
| 4528 | } |
| 4529 | |
| 4530 | rc = sqlite3_bind_pointer(pStmt, iVar, (void*)aInt, "carray", delIntptr); |
| 4531 | Tcl_SetResult(interp, (char *)t1ErrorName(rc), 0); |
| 4532 | |
| 4533 | return TCL_OK; |
| 4534 | } |
| 4535 | |
| 4536 | /* |
| 4537 | ** sqlite3_carray_bind [options...] STMT NAME VALUE ... |
| 4538 | ** |
| 4539 | ** Options: |
| 4540 | ** -malloc |
| 4541 | ** -transient |
| 4542 | ** -static |
| 4543 | ** -v2 |
| 4544 | ** -int32 |
| 4545 | ** -int64 |
| 4546 | ** -double |
| 4547 | ** -text |
| 4548 | ** -blob |
| 4549 | ** |
| 4550 | ** Each call clears static data. Called with no options does nothing |
| 4551 | ** but clear static data. |
| 4552 | */ |
| 4553 | static int SQLITE_TCLAPI test_carray_bind( |
| 4554 | void * clientData, |
| 4555 | Tcl_Interp *interp, |
| 4556 | int objc, |
| 4557 | Tcl_Obj *CONST objv[] |
| 4558 | ){ |
| 4559 | sqlite3_stmt *pStmt; |
| 4560 | int eType = 0; /* CARRAY_INT32 */ |
| 4561 | int mFlagsOverride = 0; |
| 4562 | int nData = 0; |
| 4563 | void *aData = 0; |
| 4564 | int isTransient = 0; |
| 4565 | int isStatic = 0; |
| 4566 | int isV2 = 0; |
| 4567 | int isMalloc = 0; /* True to use custom xDel function */ |
| 4568 | int idx; |
| 4569 | int i, j; |
| 4570 | int rc = SQLITE_OK; |
| 4571 | void (*xDel)(void*) = sqlite3_free; |
| 4572 | static void *aStaticData = 0; |
| 4573 | static int nStaticData = 0; |
| 4574 | static int eStaticType = 0; |
| 4575 | |
| 4576 | if( aStaticData ){ |
| 4577 | /* Always clear preexisting static data on every call */ |
| 4578 | if( eStaticType==3 ){ |
| 4579 | for(i=0; i<nStaticData; i++){ |
| 4580 | sqlite3_free(((char**)aStaticData)[i]); |
| 4581 | } |
| 4582 | } |
| 4583 | if( eStaticType==4 ){ |
| 4584 | for(i=0; i<nStaticData; i++){ |
| 4585 | sqlite3_free(((struct iovec*)aStaticData)[i].iov_base); |
| 4586 | } |
| 4587 | } |
| 4588 | sqlite3_free(aStaticData); |
| 4589 | aStaticData = 0; |
| 4590 | nStaticData = 0; |
| 4591 | eStaticType = 0; |
| 4592 | } |
| 4593 | if( objc==1 ) return TCL_OK; |
| 4594 | |
| 4595 | for(i=1; i<objc && Tcl_GetString(objv[i])[0]=='-'; i++){ |
| 4596 | const char *z = Tcl_GetString(objv[i]); |
| 4597 | if( strcmp(z, "-transient")==0 ){ |
| 4598 | isTransient = 1; |
| 4599 | isStatic = isMalloc = 0; |
| 4600 | xDel = SQLITE_TRANSIENT; |
| 4601 | }else |
| 4602 | if( strcmp(z, "-static")==0 ){ |
| 4603 | isStatic = 1; |
| 4604 | isMalloc = isTransient = 0; |
| 4605 | xDel = SQLITE_STATIC; |
| 4606 | }else |
| 4607 | if( strcmp(z, "-malloc")==0 ){ |
| 4608 | isMalloc = 1; |
| 4609 | isStatic = isTransient = 0; |
| 4610 | xDel = testCarrayFree; |
| 4611 | }else |
| 4612 | if( strcmp(z, "-v2")==0 ){ |
| 4613 | isV2 = 1; |
| 4614 | }else |
| 4615 | if( strcmp(z, "-int32")==0 ){ |
| 4616 | eType = 0; /* CARRAY_INT32 */ |
| 4617 | }else |
| 4618 | if( strcmp(z, "-int64")==0 ){ |
| 4619 | eType = 1; /* CARRAY_INT64 */ |
| 4620 | }else |
| 4621 | if( strcmp(z, "-double")==0 ){ |
| 4622 | eType = 2; /* CARRAY_DOUBLE */ |
| 4623 | }else |
| 4624 | if( strcmp(z, "-text")==0 ){ |
| 4625 | eType = 3; /* CARRAY_TEXT */ |
| 4626 | }else |
| 4627 | if( strcmp(z, "-blob")==0 ){ |
| 4628 | eType = 4; /* CARRAY_BLOB */ |
| 4629 | }else |
| 4630 | if( i<(objc-1) && strcmp(z, "-flags")==0 ){ |
| 4631 | i++; |
| 4632 | if( Tcl_GetIntFromObj(interp, objv[i], &mFlagsOverride) ){ |
| 4633 | return TCL_ERROR; |
| 4634 | } |
| 4635 | }else |
| 4636 | if( strcmp(z, "--")==0 ){ |
| 4637 | break; |
| 4638 | }else |
| 4639 | { |
| 4640 | Tcl_AppendResult(interp, "unknown option: ", z, (char*)0); |
| 4641 | return TCL_ERROR; |
| 4642 | } |
| 4643 | } |
| 4644 | if( eType==3 && !isStatic && !isTransient ){ |
| 4645 | Tcl_AppendResult(interp, "text data must be either -static or -transient", |
| 4646 | (char*)0); |
| 4647 | return TCL_ERROR; |
| 4648 | } |
| 4649 | if( eType==4 && !isStatic && !isTransient ){ |
| 4650 | Tcl_AppendResult(interp, "blob data must be either -static or -transient", |
| 4651 | (char*)0); |
| 4652 | return TCL_ERROR; |
| 4653 | } |
| 4654 | if( isStatic && isTransient ){ |
| 4655 | Tcl_AppendResult(interp, "cannot be both -static and -transient", |
| 4656 | (char*)0); |
| 4657 | return TCL_ERROR; |
| 4658 | } |
| 4659 | if( objc-i < 2 ){ |
| 4660 | Tcl_WrongNumArgs(interp, 1, objv, "[OPTIONS] STMT IDX VALUE ..."); |
| 4661 | return TCL_ERROR; |
| 4662 | } |
| 4663 | if( getStmtPointer(interp, Tcl_GetString(objv[i]), &pStmt) ) return TCL_ERROR; |
| 4664 | i++; |
| 4665 | if( Tcl_GetIntFromObj(interp, objv[i], &idx) ) return TCL_ERROR; |
| 4666 | i++; |
| 4667 | nData = objc - i; |
| 4668 | switch( eType + 5*(nData<=0) ){ |
| 4669 | case 0: { /* INT32 */ |
| 4670 | int *a = sqlite3_malloc( sizeof(int)*nData ); |
| 4671 | if( a==0 ){ rc = SQLITE_NOMEM; goto carray_bind_done; } |
| 4672 | for(j=0; j<nData; j++){ |
| 4673 | int v; |
| 4674 | if( Tcl_GetIntFromObj(interp, objv[i+j], &v) ){ |
| 4675 | sqlite3_free(a); |
| 4676 | return TCL_ERROR; |
| 4677 | } |
| 4678 | a[j] = v; |
| 4679 | } |
| 4680 | aData = a; |
| 4681 | break; |
| 4682 | } |
| 4683 | case 1: { /* INT64 */ |
| 4684 | sqlite3_int64 *a = sqlite3_malloc( sizeof(sqlite3_int64)*nData ); |
| 4685 | if( a==0 ){ rc = SQLITE_NOMEM; goto carray_bind_done; } |
| 4686 | for(j=0; j<nData; j++){ |
| 4687 | Tcl_WideInt v; |
| 4688 | if( Tcl_GetWideIntFromObj(interp, objv[i+j], &v) ){ |
| 4689 | sqlite3_free(a); |
| 4690 | return TCL_ERROR; |
| 4691 | } |
| 4692 | a[j] = v; |
| 4693 | } |
| 4694 | aData = a; |
| 4695 | break; |
| 4696 | } |
| 4697 | case 2: { /* DOUBLE */ |
| 4698 | double *a = sqlite3_malloc( sizeof(double)*nData ); |
| 4699 | if( a==0 ){ rc = SQLITE_NOMEM; goto carray_bind_done; } |
| 4700 | for(j=0; j<nData; j++){ |
| 4701 | double v; |
| 4702 | if( Tcl_GetDoubleFromObj(interp, objv[i+j], &v) ){ |
| 4703 | sqlite3_free(a); |
| 4704 | return TCL_ERROR; |
| 4705 | } |
| 4706 | a[j] = v; |
| 4707 | } |
| 4708 | aData = a; |
| 4709 | break; |
| 4710 | } |
| 4711 | case 3: { /* TEXT */ |
| 4712 | char **a = sqlite3_malloc( sizeof(char*)*nData ); |
| 4713 | if( a==0 ){ |
| 4714 | rc = SQLITE_NOMEM; |
| 4715 | }else{ |
| 4716 | memset(a, 0, sizeof(char*)*nData); |
| 4717 | } |
| 4718 | for(j=0; rc==SQLITE_OK && j<nData; j++){ |
| 4719 | const char *v = Tcl_GetString(objv[i+j]); |
| 4720 | if( v && strcmp(v, "NULL") ){ |
| 4721 | a[j] = sqlite3_mprintf("%s", v); |
| 4722 | if( a[j]==0 ) rc = SQLITE_NOMEM; |
| 4723 | } |
| 4724 | } |
| 4725 | aData = a; |
| 4726 | break; |
| 4727 | } |
| 4728 | case 4: { /* BLOB */ |
| 4729 | struct iovec *a = sqlite3_malloc( sizeof(struct iovec)*nData ); |
| 4730 | if( a==0 ){ |
| 4731 | rc = SQLITE_NOMEM; |
| 4732 | }else{ |
| 4733 | memset(a, 0, sizeof(struct iovec)*nData); |
| 4734 | } |
| 4735 | for(j=0; rc==SQLITE_OK && j<nData; j++){ |
| 4736 | Tcl_Size n = 0; |
| 4737 | unsigned char *v = Tcl_GetByteArrayFromObj(objv[i+i], &n); |
| 4738 | a[j].iov_len = (size_t)n; |
| 4739 | a[j].iov_base = sqlite3_malloc64( n ); |
| 4740 | if( a[j].iov_base==0 ){ |
| 4741 | a[j].iov_len = 0; |
| 4742 | rc = SQLITE_NOMEM; |
| 4743 | }else{ |
| 4744 | memcpy(a[j].iov_base, v, n); |
| 4745 | } |
| 4746 | } |
| 4747 | aData = a; |
| 4748 | break; |
| 4749 | } |
| 4750 | case 5: { /* nData==0 */ |
| 4751 | aData = ""; |
| 4752 | /* Preserve SQLITE_TRANSIENT so that the copy path is exercised. Since |
| 4753 | ** aData is not a malloc()ed pointer, clear isTransient below. */ |
| 4754 | if( xDel!=SQLITE_TRANSIENT ) xDel = SQLITE_STATIC; |
| 4755 | isTransient = 0; |
| 4756 | isStatic = 0; |
| 4757 | break; |
| 4758 | } |
| 4759 | } |
| 4760 | |
| 4761 | if( rc==SQLITE_OK ){ |
| 4762 | if( isStatic ){ |
| 4763 | aStaticData = aData; |
| 4764 | nStaticData = nData; |
| 4765 | eStaticType = eType; |
| 4766 | } |
| 4767 | else if( isMalloc ){ |
| 4768 | int nByte = ((eType==0) ? sizeof(int) : sizeof(i64)) * nData; |
| 4769 | void *aByte = testCarrayAlloc(nByte); |
| 4770 | if( aByte==0 ){ |
| 4771 | sqlite3_free(aData); |
| 4772 | rc = SQLITE_NOMEM; |
| 4773 | }else{ |
| 4774 | memcpy(aByte, aData, nByte); |
| 4775 | sqlite3_free(aData); |
| 4776 | aData = aByte; |
| 4777 | xDel = testCarrayFree; |
| 4778 | } |
| 4779 | assert( eType==0 || eType==1 || eType==2 ); |
| 4780 | } |
| 4781 | } |
| 4782 | |
| 4783 | if( rc==SQLITE_OK ){ |
| 4784 | if( mFlagsOverride==0 ) mFlagsOverride = eType; |
| 4785 | if( isV2 ){ |
| 4786 | void *pDel; |
| 4787 | if( xDel==testCarrayFree ){ |
| 4788 | u8 *p2 = (u8*)aData; |
| 4789 | pDel = (void*)&p2[-16]; |
| 4790 | xDel = sqlite3_free; |
| 4791 | }else{ |
| 4792 | pDel = aData; |
| 4793 | } |
| 4794 | rc = sqlite3_carray_bind_v2(pStmt, idx, aData, nData, mFlagsOverride, |
| 4795 | xDel, pDel); |
| 4796 | }else{ |
| 4797 | rc = sqlite3_carray_bind(pStmt, idx, aData, nData, mFlagsOverride, xDel); |
| 4798 | } |
| 4799 | } |
| 4800 | if( isTransient ){ |
| 4801 | if( eType==3 && aData ){ |
| 4802 | for(i=0; i<nData; i++) sqlite3_free(((char**)aData)[i]); |
| 4803 | } |
| 4804 | if( eType==4 && aData ){ |
| 4805 | for(i=0; i<nData; i++) sqlite3_free(((struct iovec*)aData)[i].iov_base); |
| 4806 | } |
| 4807 | sqlite3_free(aData); |
| 4808 | } |
| 4809 | carray_bind_done: |
| 4810 | if( rc ){ |
| 4811 | Tcl_AppendResult(interp, sqlite3_errstr(rc), (char*)0); |
| 4812 | return TCL_ERROR; |
| 4813 | } |
| 4814 | return TCL_OK; |
| 4815 | } |
| 4816 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 4817 | |
| 4818 | /* |
| 4819 | ** Usage: sqlite3_bind_parameter_count STMT |
| 4820 | ** |
| 4821 | ** Return the number of wildcards in the given statement. |
| 4822 | */ |
| 4823 | static int SQLITE_TCLAPI test_bind_parameter_count( |
| 4824 | void * clientData, |
| 4825 | Tcl_Interp *interp, |
| 4826 | int objc, |
| 4827 | Tcl_Obj *CONST objv[] |
| 4828 | ){ |
| 4829 | sqlite3_stmt *pStmt; |
| 4830 | |
| 4831 | if( objc!=2 ){ |
| 4832 | Tcl_WrongNumArgs(interp, 1, objv, "STMT"); |
| 4833 | return TCL_ERROR; |
| 4834 | } |
| 4835 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 4836 | Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_bind_parameter_count(pStmt))); |
| 4837 | return TCL_OK; |
| 4838 | } |
| 4839 | |
| 4840 | /* |
| 4841 | ** Usage: sqlite3_bind_parameter_name STMT N |
| 4842 | ** |
| 4843 | ** Return the name of the Nth wildcard. The first wildcard is 1. |
| 4844 | ** An empty string is returned if N is out of range or if the wildcard |
| 4845 | ** is nameless. |
| 4846 | */ |
| 4847 | static int SQLITE_TCLAPI test_bind_parameter_name( |
| 4848 | void * clientData, |
| 4849 | Tcl_Interp *interp, |
| 4850 | int objc, |
| 4851 | Tcl_Obj *CONST objv[] |
| 4852 | ){ |
| 4853 | sqlite3_stmt *pStmt; |
| 4854 | int i; |
| 4855 | |
| 4856 | if( objc!=3 ){ |
| 4857 | Tcl_WrongNumArgs(interp, 1, objv, "STMT N"); |
| 4858 | return TCL_ERROR; |
| 4859 | } |
| 4860 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 4861 | if( Tcl_GetIntFromObj(interp, objv[2], &i) ) return TCL_ERROR; |
| 4862 | Tcl_SetObjResult(interp, |
| 4863 | Tcl_NewStringObj(sqlite3_bind_parameter_name(pStmt,i),-1) |
| 4864 | ); |
| 4865 | return TCL_OK; |
| 4866 | } |
| 4867 | |
| 4868 | /* |
| 4869 | ** Usage: sqlite3_bind_parameter_index STMT NAME |
| 4870 | ** |
| 4871 | ** Return the index of the wildcard called NAME. Return 0 if there is |
| 4872 | ** no such wildcard. |
| 4873 | */ |
| 4874 | static int SQLITE_TCLAPI test_bind_parameter_index( |
| 4875 | void * clientData, |
| 4876 | Tcl_Interp *interp, |
| 4877 | int objc, |
| 4878 | Tcl_Obj *CONST objv[] |
| 4879 | ){ |
| 4880 | sqlite3_stmt *pStmt; |
| 4881 | |
| 4882 | if( objc!=3 ){ |
| 4883 | Tcl_WrongNumArgs(interp, 1, objv, "STMT NAME"); |
| 4884 | return TCL_ERROR; |
| 4885 | } |
| 4886 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 4887 | Tcl_SetObjResult(interp, |
| 4888 | Tcl_NewIntObj( |
| 4889 | sqlite3_bind_parameter_index(pStmt,Tcl_GetString(objv[2])) |
| 4890 | ) |
| 4891 | ); |
| 4892 | return TCL_OK; |
| 4893 | } |
| 4894 | |
| 4895 | /* |
| 4896 | ** Usage: sqlite3_clear_bindings STMT |
| 4897 | ** |
| 4898 | */ |
| 4899 | static int SQLITE_TCLAPI test_clear_bindings( |
| 4900 | void * clientData, |
| 4901 | Tcl_Interp *interp, |
| 4902 | int objc, |
| 4903 | Tcl_Obj *CONST objv[] |
| 4904 | ){ |
| 4905 | sqlite3_stmt *pStmt; |
| 4906 | |
| 4907 | if( objc!=2 ){ |
| 4908 | Tcl_WrongNumArgs(interp, 1, objv, "STMT"); |
| 4909 | return TCL_ERROR; |
| 4910 | } |
| 4911 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 4912 | Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_clear_bindings(pStmt))); |
| 4913 | return TCL_OK; |
| 4914 | } |
| 4915 | |
| 4916 | /* |
| 4917 | ** Usage: sqlite3_sleep MILLISECONDS |
| 4918 | */ |
| 4919 | static int SQLITE_TCLAPI test_sleep( |
| 4920 | void * clientData, |
| 4921 | Tcl_Interp *interp, |
| 4922 | int objc, |
| 4923 | Tcl_Obj *CONST objv[] |
| 4924 | ){ |
| 4925 | int ms; |
| 4926 | |
| 4927 | if( objc!=2 ){ |
| 4928 | Tcl_WrongNumArgs(interp, 1, objv, "MILLISECONDS"); |
| 4929 | return TCL_ERROR; |
| 4930 | } |
| 4931 | if( Tcl_GetIntFromObj(interp, objv[1], &ms) ){ |
| 4932 | return TCL_ERROR; |
| 4933 | } |
| 4934 | Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_sleep(ms))); |
| 4935 | return TCL_OK; |
| 4936 | } |
| 4937 | |
| 4938 | /* |
| 4939 | ** Usage: sqlite3_extended_errcode DB |
| 4940 | ** |
| 4941 | ** Return the string representation of the most recent sqlite3_* API |
| 4942 | ** error code. e.g. "SQLITE_ERROR". |
| 4943 | */ |
| 4944 | static int SQLITE_TCLAPI test_ex_errcode( |
| 4945 | void * clientData, |
| 4946 | Tcl_Interp *interp, |
| 4947 | int objc, |
| 4948 | Tcl_Obj *CONST objv[] |
| 4949 | ){ |
| 4950 | sqlite3 *db; |
| 4951 | int rc; |
| 4952 | |
| 4953 | if( objc!=2 ){ |
| 4954 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 4955 | Tcl_GetString(objv[0]), " DB", NULL); |
| 4956 | return TCL_ERROR; |
| 4957 | } |
| 4958 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 4959 | rc = sqlite3_extended_errcode(db); |
| 4960 | Tcl_AppendResult(interp, (char *)t1ErrorName(rc), NULL); |
| 4961 | return TCL_OK; |
| 4962 | } |
| 4963 | |
| 4964 | |
| 4965 | /* |
| 4966 | ** Usage: sqlite3_errcode DB |
| 4967 | ** |
| 4968 | ** Return the string representation of the most recent sqlite3_* API |
| 4969 | ** error code. e.g. "SQLITE_ERROR". |
| 4970 | */ |
| 4971 | static int SQLITE_TCLAPI test_errcode( |
| 4972 | void * clientData, |
| 4973 | Tcl_Interp *interp, |
| 4974 | int objc, |
| 4975 | Tcl_Obj *CONST objv[] |
| 4976 | ){ |
| 4977 | sqlite3 *db; |
| 4978 | int rc; |
| 4979 | |
| 4980 | if( objc!=2 ){ |
| 4981 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 4982 | Tcl_GetString(objv[0]), " DB", NULL); |
| 4983 | return TCL_ERROR; |
| 4984 | } |
| 4985 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 4986 | rc = sqlite3_errcode(db); |
| 4987 | Tcl_AppendResult(interp, (char *)t1ErrorName(rc), NULL); |
| 4988 | return TCL_OK; |
| 4989 | } |
| 4990 | |
| 4991 | /* |
| 4992 | ** Usage: sqlite3_errmsg DB |
| 4993 | ** |
| 4994 | ** Returns the UTF-8 representation of the error message string for the |
| 4995 | ** most recent sqlite3_* API call. |
| 4996 | */ |
| 4997 | static int SQLITE_TCLAPI test_errmsg( |
| 4998 | void * clientData, |
| 4999 | Tcl_Interp *interp, |
| 5000 | int objc, |
| 5001 | Tcl_Obj *CONST objv[] |
| 5002 | ){ |
| 5003 | sqlite3 *db; |
| 5004 | const char *zErr; |
| 5005 | |
| 5006 | if( objc!=2 ){ |
| 5007 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 5008 | Tcl_GetString(objv[0]), " DB", NULL); |
| 5009 | return TCL_ERROR; |
| 5010 | } |
| 5011 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 5012 | |
| 5013 | zErr = sqlite3_errmsg(db); |
| 5014 | Tcl_SetObjResult(interp, Tcl_NewStringObj(zErr, -1)); |
| 5015 | return TCL_OK; |
| 5016 | } |
| 5017 | |
| 5018 | |
| 5019 | /* |
| 5020 | ** Usage: sqlite3_error_offset DB |
| 5021 | ** |
| 5022 | ** Return the byte offset into the input UTF8 SQL for the most recent |
| 5023 | ** error, or -1 of the error does not refer to a specific token. |
| 5024 | */ |
| 5025 | static int SQLITE_TCLAPI test_error_offset( |
| 5026 | void * clientData, |
| 5027 | Tcl_Interp *interp, |
| 5028 | int objc, |
| 5029 | Tcl_Obj *CONST objv[] |
| 5030 | ){ |
| 5031 | sqlite3 *db; |
| 5032 | int iByteOffset; |
| 5033 | |
| 5034 | if( objc!=2 ){ |
| 5035 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 5036 | Tcl_GetString(objv[0]), " DB", NULL); |
| 5037 | return TCL_ERROR; |
| 5038 | } |
| 5039 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 5040 | |
| 5041 | iByteOffset = sqlite3_error_offset(db); |
| 5042 | Tcl_SetObjResult(interp, Tcl_NewIntObj(iByteOffset)); |
| 5043 | return TCL_OK; |
| 5044 | } |
| 5045 | |
| 5046 | /* |
| 5047 | ** Usage: test_errmsg16 DB |
| 5048 | ** |
| 5049 | ** Returns the UTF-16 representation of the error message string for the |
| 5050 | ** most recent sqlite3_* API call. This is a byte array object at the TCL |
| 5051 | ** level, and it includes the 0x00 0x00 terminator bytes at the end of the |
| 5052 | ** UTF-16 string. |
| 5053 | */ |
| 5054 | static int SQLITE_TCLAPI test_errmsg16( |
| 5055 | void * clientData, |
| 5056 | Tcl_Interp *interp, |
| 5057 | int objc, |
| 5058 | Tcl_Obj *CONST objv[] |
| 5059 | ){ |
| 5060 | #ifndef SQLITE_OMIT_UTF16 |
| 5061 | sqlite3 *db; |
| 5062 | const void *zErr; |
| 5063 | const char *z; |
| 5064 | int bytes = 0; |
| 5065 | |
| 5066 | if( objc!=2 ){ |
| 5067 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 5068 | Tcl_GetString(objv[0]), " DB", NULL); |
| 5069 | return TCL_ERROR; |
| 5070 | } |
| 5071 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 5072 | |
| 5073 | zErr = sqlite3_errmsg16(db); |
| 5074 | if( zErr ){ |
| 5075 | z = zErr; |
| 5076 | for(bytes=0; z[bytes] || z[bytes+1]; bytes+=2){} |
| 5077 | } |
| 5078 | Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(zErr, bytes)); |
| 5079 | #endif /* SQLITE_OMIT_UTF16 */ |
| 5080 | return TCL_OK; |
| 5081 | } |
| 5082 | |
| 5083 | /* |
| 5084 | ** Usage: sqlite3_set_errmsg DB ERRCODE ERRMSG |
| 5085 | */ |
| 5086 | static int SQLITE_TCLAPI test_set_errmsg( |
| 5087 | void * clientData, |
| 5088 | Tcl_Interp *interp, |
| 5089 | int objc, |
| 5090 | Tcl_Obj *CONST objv[] |
| 5091 | ){ |
| 5092 | const char *zDb = 0; |
| 5093 | const char *zErr = 0; |
| 5094 | int iErr = 0; |
| 5095 | sqlite3 *db = 0; |
| 5096 | int rc; |
| 5097 | |
| 5098 | if( objc!=4 ){ |
| 5099 | Tcl_WrongNumArgs(interp, 1, objv, "DB ERRCODE ERRMSG"); |
| 5100 | return TCL_ERROR; |
| 5101 | } |
| 5102 | zDb = Tcl_GetString(objv[1]); |
| 5103 | if( zDb[0] ){ |
| 5104 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 5105 | } |
| 5106 | if( Tcl_GetIntFromObj(interp, objv[2], &iErr) ) return TCL_ERROR; |
| 5107 | zErr = Tcl_GetString(objv[3]); |
| 5108 | |
| 5109 | rc = sqlite3_set_errmsg(db, iErr, (zErr[0] ? zErr : 0)); |
| 5110 | Tcl_SetResult(interp, (char *)t1ErrorName(rc), 0); |
| 5111 | return TCL_OK; |
| 5112 | } |
| 5113 | |
| 5114 | /* |
| 5115 | ** Usage: sqlite3_prepare DB sql bytes ?tailvar? |
| 5116 | ** |
| 5117 | ** Compile up to <bytes> bytes of the supplied SQL string <sql> using |
| 5118 | ** database handle <DB>. The parameter <tailval> is the name of a global |
| 5119 | ** variable that is set to the unused portion of <sql> (if any). A |
| 5120 | ** STMT handle is returned. |
| 5121 | */ |
| 5122 | static int SQLITE_TCLAPI test_prepare( |
| 5123 | void * clientData, |
| 5124 | Tcl_Interp *interp, |
| 5125 | int objc, |
| 5126 | Tcl_Obj *CONST objv[] |
| 5127 | ){ |
| 5128 | sqlite3 *db; |
| 5129 | const char *zSql; |
| 5130 | int bytes; |
| 5131 | const char *zTail = 0; |
| 5132 | sqlite3_stmt *pStmt = 0; |
| 5133 | char zBuf[50]; |
| 5134 | int rc; |
| 5135 | |
| 5136 | if( objc!=5 && objc!=4 ){ |
| 5137 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 5138 | Tcl_GetString(objv[0]), " DB sql bytes ?tailvar?", NULL); |
| 5139 | return TCL_ERROR; |
| 5140 | } |
| 5141 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 5142 | zSql = Tcl_GetString(objv[2]); |
| 5143 | if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR; |
| 5144 | |
| 5145 | rc = sqlite3_prepare(db, zSql, bytes, &pStmt, objc>=5 ? &zTail : 0); |
| 5146 | Tcl_ResetResult(interp); |
| 5147 | if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; |
| 5148 | if( zTail && objc>=5 ){ |
| 5149 | if( bytes>=0 ){ |
| 5150 | bytes = bytes - (int)(zTail-zSql); |
| 5151 | } |
| 5152 | if( (int)strlen(zTail)<bytes ){ |
| 5153 | bytes = (int)strlen(zTail); |
| 5154 | } |
| 5155 | Tcl_ObjSetVar2(interp, objv[4], 0, Tcl_NewStringObj(zTail, bytes), 0); |
| 5156 | } |
| 5157 | if( rc!=SQLITE_OK ){ |
| 5158 | assert( pStmt==0 ); |
| 5159 | sqlite3_snprintf(sizeof(zBuf), zBuf, "(%d) ", rc); |
| 5160 | Tcl_AppendResult(interp, zBuf, sqlite3_errmsg(db), NULL); |
| 5161 | return TCL_ERROR; |
| 5162 | } |
| 5163 | |
| 5164 | if( pStmt ){ |
| 5165 | if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR; |
| 5166 | Tcl_AppendResult(interp, zBuf, NULL); |
| 5167 | } |
| 5168 | return TCL_OK; |
| 5169 | } |
| 5170 | |
| 5171 | /* |
| 5172 | ** Usage: sqlite3_prepare_v2 DB sql bytes ?tailvar? |
| 5173 | ** |
| 5174 | ** Compile up to <bytes> bytes of the supplied SQL string <sql> using |
| 5175 | ** database handle <DB>. The parameter <tailval> is the name of a global |
| 5176 | ** variable that is set to the unused portion of <sql> (if any). A |
| 5177 | ** STMT handle is returned. |
| 5178 | */ |
| 5179 | static int SQLITE_TCLAPI test_prepare_v2( |
| 5180 | void * clientData, |
| 5181 | Tcl_Interp *interp, |
| 5182 | int objc, |
| 5183 | Tcl_Obj *CONST objv[] |
| 5184 | ){ |
| 5185 | sqlite3 *db; |
| 5186 | const char *zSql; |
| 5187 | char *zCopy = 0; /* malloc() copy of zSql */ |
| 5188 | int bytes; |
| 5189 | const char *zTail = 0; |
| 5190 | const char **pzTail; |
| 5191 | sqlite3_stmt *pStmt = 0; |
| 5192 | char zBuf[50]; |
| 5193 | int rc; |
| 5194 | |
| 5195 | if( objc!=5 && objc!=4 ){ |
| 5196 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 5197 | Tcl_GetString(objv[0]), " DB sql bytes tailvar", NULL); |
| 5198 | return TCL_ERROR; |
| 5199 | } |
| 5200 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 5201 | zSql = Tcl_GetString(objv[2]); |
| 5202 | if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR; |
| 5203 | |
| 5204 | /* Instead of using zSql directly, make a copy into a buffer obtained |
| 5205 | ** directly from malloc(). The idea is to make it easier for valgrind |
| 5206 | ** to spot buffer overreads. */ |
| 5207 | if( bytes>=0 ){ |
| 5208 | zCopy = malloc(bytes); |
| 5209 | memcpy(zCopy, zSql, bytes); |
| 5210 | }else{ |
| 5211 | int n = (int)strlen(zSql) + 1; |
| 5212 | zCopy = malloc(n); |
| 5213 | memcpy(zCopy, zSql, n); |
| 5214 | } |
| 5215 | pzTail = objc>=5 ? &zTail : 0; |
| 5216 | rc = sqlite3_prepare_v2(db, zCopy, bytes, &pStmt, pzTail); |
| 5217 | if( objc>=5 ){ |
| 5218 | zTail = &zSql[(zTail - zCopy)]; |
| 5219 | } |
| 5220 | free(zCopy); |
| 5221 | |
| 5222 | assert(rc==SQLITE_OK || pStmt==0); |
| 5223 | Tcl_ResetResult(interp); |
| 5224 | if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; |
| 5225 | if( rc==SQLITE_OK && objc>=5 && zTail ){ |
| 5226 | if( bytes>=0 ){ |
| 5227 | bytes = bytes - (int)(zTail-zSql); |
| 5228 | } |
| 5229 | Tcl_ObjSetVar2(interp, objv[4], 0, Tcl_NewStringObj(zTail, bytes), 0); |
| 5230 | } |
| 5231 | if( rc!=SQLITE_OK ){ |
| 5232 | assert( pStmt==0 ); |
| 5233 | sqlite3_snprintf(sizeof(zBuf), zBuf, "(%d) ", rc); |
| 5234 | Tcl_AppendResult(interp, zBuf, sqlite3_errmsg(db), NULL); |
| 5235 | return TCL_ERROR; |
| 5236 | } |
| 5237 | |
| 5238 | if( pStmt ){ |
| 5239 | if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR; |
| 5240 | Tcl_AppendResult(interp, zBuf, NULL); |
| 5241 | } |
| 5242 | return TCL_OK; |
| 5243 | } |
| 5244 | |
| 5245 | /* |
| 5246 | ** Usage: sqlite3_prepare_v3 DB sql bytes flags ?tailvar? |
| 5247 | ** |
| 5248 | ** Compile up to <bytes> bytes of the supplied SQL string <sql> using |
| 5249 | ** database handle <DB> and flags <flags>. The parameter <tailval> is |
| 5250 | ** the name of a global variable that is set to the unused portion of |
| 5251 | ** <sql> (if any). A STMT handle is returned. |
| 5252 | */ |
| 5253 | static int SQLITE_TCLAPI test_prepare_v3( |
| 5254 | void * clientData, |
| 5255 | Tcl_Interp *interp, |
| 5256 | int objc, |
| 5257 | Tcl_Obj *CONST objv[] |
| 5258 | ){ |
| 5259 | sqlite3 *db; |
| 5260 | const char *zSql; |
| 5261 | char *zCopy = 0; /* malloc() copy of zSql */ |
| 5262 | int bytes, flags; |
| 5263 | const char *zTail = 0; |
| 5264 | const char **pzTail; |
| 5265 | sqlite3_stmt *pStmt = 0; |
| 5266 | char zBuf[50]; |
| 5267 | int rc; |
| 5268 | |
| 5269 | if( objc!=6 && objc!=5 ){ |
| 5270 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 5271 | Tcl_GetString(objv[0]), " DB sql bytes flags tailvar", NULL); |
| 5272 | return TCL_ERROR; |
| 5273 | } |
| 5274 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 5275 | zSql = Tcl_GetString(objv[2]); |
| 5276 | if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR; |
| 5277 | if( Tcl_GetIntFromObj(interp, objv[4], &flags) ) return TCL_ERROR; |
| 5278 | |
| 5279 | /* Instead of using zSql directly, make a copy into a buffer obtained |
| 5280 | ** directly from malloc(). The idea is to make it easier for valgrind |
| 5281 | ** to spot buffer overreads. */ |
| 5282 | if( bytes>=0 ){ |
| 5283 | zCopy = malloc(bytes); |
| 5284 | memcpy(zCopy, zSql, bytes); |
| 5285 | }else{ |
| 5286 | int n = (int)strlen(zSql) + 1; |
| 5287 | zCopy = malloc(n); |
| 5288 | memcpy(zCopy, zSql, n); |
| 5289 | } |
| 5290 | pzTail = objc>=6 ? &zTail : 0; |
| 5291 | rc = sqlite3_prepare_v3(db, zCopy, bytes, (unsigned int)flags,&pStmt,pzTail); |
| 5292 | zTail = &zSql[(zTail - zCopy)]; |
| 5293 | free(zCopy); |
| 5294 | |
| 5295 | assert(rc==SQLITE_OK || pStmt==0); |
| 5296 | Tcl_ResetResult(interp); |
| 5297 | if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; |
| 5298 | if( rc==SQLITE_OK && zTail && objc>=6 ){ |
| 5299 | if( bytes>=0 ){ |
| 5300 | bytes = bytes - (int)(zTail-zSql); |
| 5301 | } |
| 5302 | Tcl_ObjSetVar2(interp, objv[5], 0, Tcl_NewStringObj(zTail, bytes), 0); |
| 5303 | } |
| 5304 | if( rc!=SQLITE_OK ){ |
| 5305 | assert( pStmt==0 ); |
| 5306 | sqlite3_snprintf(sizeof(zBuf), zBuf, "(%d) ", rc); |
| 5307 | Tcl_AppendResult(interp, zBuf, sqlite3_errmsg(db), NULL); |
| 5308 | return TCL_ERROR; |
| 5309 | } |
| 5310 | |
| 5311 | if( pStmt ){ |
| 5312 | if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR; |
| 5313 | Tcl_AppendResult(interp, zBuf, NULL); |
| 5314 | } |
| 5315 | return TCL_OK; |
| 5316 | } |
| 5317 | |
| 5318 | /* |
| 5319 | ** Usage: sqlite3_prepare_tkt3134 DB |
| 5320 | ** |
| 5321 | ** Generate a prepared statement for a zero-byte string as a test |
| 5322 | ** for ticket #3134. The string should be preceded by a zero byte. |
| 5323 | */ |
| 5324 | static int SQLITE_TCLAPI test_prepare_tkt3134( |
| 5325 | void * clientData, |
| 5326 | Tcl_Interp *interp, |
| 5327 | int objc, |
| 5328 | Tcl_Obj *CONST objv[] |
| 5329 | ){ |
| 5330 | sqlite3 *db; |
| 5331 | static const char zSql[] = "\000SELECT 1"; |
| 5332 | sqlite3_stmt *pStmt = 0; |
| 5333 | char zBuf[50]; |
| 5334 | int rc; |
| 5335 | |
| 5336 | if( objc!=2 ){ |
| 5337 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 5338 | Tcl_GetString(objv[0]), " DB sql bytes tailvar", NULL); |
| 5339 | return TCL_ERROR; |
| 5340 | } |
| 5341 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 5342 | rc = sqlite3_prepare_v2(db, &zSql[1], 0, &pStmt, 0); |
| 5343 | assert(rc==SQLITE_OK || pStmt==0); |
| 5344 | if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; |
| 5345 | if( rc!=SQLITE_OK ){ |
| 5346 | assert( pStmt==0 ); |
| 5347 | sqlite3_snprintf(sizeof(zBuf), zBuf, "(%d) ", rc); |
| 5348 | Tcl_AppendResult(interp, zBuf, sqlite3_errmsg(db), NULL); |
| 5349 | return TCL_ERROR; |
| 5350 | } |
| 5351 | |
| 5352 | if( pStmt ){ |
| 5353 | if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR; |
| 5354 | Tcl_AppendResult(interp, zBuf, NULL); |
| 5355 | } |
| 5356 | return TCL_OK; |
| 5357 | } |
| 5358 | |
| 5359 | /* |
| 5360 | ** Usage: sqlite3_prepare16 DB sql bytes tailvar |
| 5361 | ** |
| 5362 | ** Compile up to <bytes> bytes of the supplied SQL string <sql> using |
| 5363 | ** database handle <DB>. The parameter <tailval> is the name of a global |
| 5364 | ** variable that is set to the unused portion of <sql> (if any). A |
| 5365 | ** STMT handle is returned. |
| 5366 | */ |
| 5367 | static int SQLITE_TCLAPI test_prepare16( |
| 5368 | void * clientData, |
| 5369 | Tcl_Interp *interp, |
| 5370 | int objc, |
| 5371 | Tcl_Obj *CONST objv[] |
| 5372 | ){ |
| 5373 | #ifndef SQLITE_OMIT_UTF16 |
| 5374 | sqlite3 *db; |
| 5375 | const void *zSql; |
| 5376 | const void *zTail = 0; |
| 5377 | Tcl_Obj *pTail = 0; |
| 5378 | sqlite3_stmt *pStmt = 0; |
| 5379 | char zBuf[50]; |
| 5380 | int rc; |
| 5381 | int bytes; /* The integer specified as arg 3 */ |
| 5382 | Tcl_Size objlen; /* The byte-array length of arg 2 */ |
| 5383 | |
| 5384 | if( objc!=5 && objc!=4 ){ |
| 5385 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 5386 | Tcl_GetString(objv[0]), " DB sql bytes ?tailvar?", NULL); |
| 5387 | return TCL_ERROR; |
| 5388 | } |
| 5389 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 5390 | zSql = Tcl_GetByteArrayFromObj(objv[2], &objlen); |
| 5391 | if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR; |
| 5392 | |
| 5393 | rc = sqlite3_prepare16(db, zSql, bytes, &pStmt, objc>=5 ? &zTail : 0); |
| 5394 | if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; |
| 5395 | if( rc ){ |
| 5396 | return TCL_ERROR; |
| 5397 | } |
| 5398 | |
| 5399 | if( objc>=5 ){ |
| 5400 | if( zTail ){ |
| 5401 | objlen = objlen - (int)((u8 *)zTail-(u8 *)zSql); |
| 5402 | }else{ |
| 5403 | objlen = 0; |
| 5404 | } |
| 5405 | pTail = Tcl_NewByteArrayObj((u8 *)zTail, objlen); |
| 5406 | Tcl_IncrRefCount(pTail); |
| 5407 | Tcl_ObjSetVar2(interp, objv[4], 0, pTail, 0); |
| 5408 | Tcl_DecrRefCount(pTail); |
| 5409 | } |
| 5410 | |
| 5411 | if( pStmt ){ |
| 5412 | if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR; |
| 5413 | } |
| 5414 | Tcl_AppendResult(interp, zBuf, NULL); |
| 5415 | #endif /* SQLITE_OMIT_UTF16 */ |
| 5416 | return TCL_OK; |
| 5417 | } |
| 5418 | |
| 5419 | /* |
| 5420 | ** Usage: sqlite3_prepare16_v2 DB sql bytes ?tailvar? |
| 5421 | ** |
| 5422 | ** Compile up to <bytes> bytes of the supplied SQL string <sql> using |
| 5423 | ** database handle <DB>. The parameter <tailval> is the name of a global |
| 5424 | ** variable that is set to the unused portion of <sql> (if any). A |
| 5425 | ** STMT handle is returned. |
| 5426 | */ |
| 5427 | static int SQLITE_TCLAPI test_prepare16_v2( |
| 5428 | void * clientData, |
| 5429 | Tcl_Interp *interp, |
| 5430 | int objc, |
| 5431 | Tcl_Obj *CONST objv[] |
| 5432 | ){ |
| 5433 | #ifndef SQLITE_OMIT_UTF16 |
| 5434 | sqlite3 *db; |
| 5435 | const void *zSql; |
| 5436 | const void *zTail = 0; |
| 5437 | Tcl_Obj *pTail = 0; |
| 5438 | sqlite3_stmt *pStmt = 0; |
| 5439 | char zBuf[50]; |
| 5440 | int rc; |
| 5441 | int bytes; /* The integer specified as arg 3 */ |
| 5442 | Tcl_Size objlen; /* The byte-array length of arg 2 */ |
| 5443 | |
| 5444 | if( objc!=5 && objc!=4 ){ |
| 5445 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 5446 | Tcl_GetString(objv[0]), " DB sql bytes ?tailvar?", NULL); |
| 5447 | return TCL_ERROR; |
| 5448 | } |
| 5449 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 5450 | zSql = Tcl_GetByteArrayFromObj(objv[2], &objlen); |
| 5451 | if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR; |
| 5452 | |
| 5453 | rc = sqlite3_prepare16_v2(db, zSql, bytes, &pStmt, objc>=5 ? &zTail : 0); |
| 5454 | if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; |
| 5455 | if( rc ){ |
| 5456 | return TCL_ERROR; |
| 5457 | } |
| 5458 | |
| 5459 | if( objc>=5 ){ |
| 5460 | if( zTail ){ |
| 5461 | objlen = objlen - (int)((u8 *)zTail-(u8 *)zSql); |
| 5462 | }else{ |
| 5463 | objlen = 0; |
| 5464 | } |
| 5465 | pTail = Tcl_NewByteArrayObj((u8 *)zTail, objlen); |
| 5466 | Tcl_IncrRefCount(pTail); |
| 5467 | Tcl_ObjSetVar2(interp, objv[4], 0, pTail, 0); |
| 5468 | Tcl_DecrRefCount(pTail); |
| 5469 | } |
| 5470 | |
| 5471 | if( pStmt ){ |
| 5472 | if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR; |
| 5473 | } |
| 5474 | Tcl_AppendResult(interp, zBuf, NULL); |
| 5475 | #endif /* SQLITE_OMIT_UTF16 */ |
| 5476 | return TCL_OK; |
| 5477 | } |
| 5478 | |
| 5479 | /* |
| 5480 | ** Usage: sqlite3_open filename ?options-list? |
| 5481 | */ |
| 5482 | static int SQLITE_TCLAPI test_open( |
| 5483 | void * clientData, |
| 5484 | Tcl_Interp *interp, |
| 5485 | int objc, |
| 5486 | Tcl_Obj *CONST objv[] |
| 5487 | ){ |
| 5488 | const char *zFilename; |
| 5489 | sqlite3 *db; |
| 5490 | char zBuf[100]; |
| 5491 | |
| 5492 | if( objc!=3 && objc!=2 && objc!=1 ){ |
| 5493 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 5494 | Tcl_GetString(objv[0]), " filename options-list", NULL); |
| 5495 | return TCL_ERROR; |
| 5496 | } |
| 5497 | |
| 5498 | zFilename = objc>1 ? Tcl_GetString(objv[1]) : 0; |
| 5499 | sqlite3_open(zFilename, &db); |
| 5500 | |
| 5501 | if( sqlite3TestMakePointerStr(interp, zBuf, db) ) return TCL_ERROR; |
| 5502 | Tcl_AppendResult(interp, zBuf, NULL); |
| 5503 | return TCL_OK; |
| 5504 | } |
| 5505 | |
| 5506 | /* |
| 5507 | ** Usage: sqlite3_open_v2 FILENAME FLAGS VFS |
| 5508 | */ |
| 5509 | static int SQLITE_TCLAPI test_open_v2( |
| 5510 | void * clientData, |
| 5511 | Tcl_Interp *interp, |
| 5512 | int objc, |
| 5513 | Tcl_Obj *CONST objv[] |
| 5514 | ){ |
| 5515 | const char *zFilename; |
| 5516 | const char *zVfs; |
| 5517 | int flags = 0; |
| 5518 | sqlite3 *db; |
| 5519 | int rc; |
| 5520 | char zBuf[100]; |
| 5521 | |
| 5522 | Tcl_Size nFlag; |
| 5523 | Tcl_Obj **apFlag; |
| 5524 | Tcl_Size i; |
| 5525 | |
| 5526 | if( objc!=4 ){ |
| 5527 | Tcl_WrongNumArgs(interp, 1, objv, "FILENAME FLAGS VFS"); |
| 5528 | return TCL_ERROR; |
| 5529 | } |
| 5530 | zFilename = Tcl_GetString(objv[1]); |
| 5531 | zVfs = Tcl_GetString(objv[3]); |
| 5532 | if( zVfs[0]==0x00 ) zVfs = 0; |
| 5533 | |
| 5534 | rc = Tcl_ListObjGetElements(interp, objv[2], &nFlag, &apFlag); |
| 5535 | if( rc!=TCL_OK ) return rc; |
| 5536 | for(i=0; i<nFlag; i++){ |
| 5537 | int iFlag; |
| 5538 | struct OpenFlag { |
| 5539 | const char *zFlag; |
| 5540 | int flag; |
| 5541 | } aFlag[] = { |
| 5542 | { "SQLITE_OPEN_READONLY", SQLITE_OPEN_READONLY }, |
| 5543 | { "SQLITE_OPEN_READWRITE", SQLITE_OPEN_READWRITE }, |
| 5544 | { "SQLITE_OPEN_CREATE", SQLITE_OPEN_CREATE }, |
| 5545 | { "SQLITE_OPEN_DELETEONCLOSE", SQLITE_OPEN_DELETEONCLOSE }, |
| 5546 | { "SQLITE_OPEN_EXCLUSIVE", SQLITE_OPEN_EXCLUSIVE }, |
| 5547 | { "SQLITE_OPEN_AUTOPROXY", SQLITE_OPEN_AUTOPROXY }, |
| 5548 | { "SQLITE_OPEN_MAIN_DB", SQLITE_OPEN_MAIN_DB }, |
| 5549 | { "SQLITE_OPEN_TEMP_DB", SQLITE_OPEN_TEMP_DB }, |
| 5550 | { "SQLITE_OPEN_TRANSIENT_DB", SQLITE_OPEN_TRANSIENT_DB }, |
| 5551 | { "SQLITE_OPEN_MAIN_JOURNAL", SQLITE_OPEN_MAIN_JOURNAL }, |
| 5552 | { "SQLITE_OPEN_TEMP_JOURNAL", SQLITE_OPEN_TEMP_JOURNAL }, |
| 5553 | { "SQLITE_OPEN_SUBJOURNAL", SQLITE_OPEN_SUBJOURNAL }, |
| 5554 | { "SQLITE_OPEN_SUPER_JOURNAL", SQLITE_OPEN_SUPER_JOURNAL }, |
| 5555 | { "SQLITE_OPEN_NOMUTEX", SQLITE_OPEN_NOMUTEX }, |
| 5556 | { "SQLITE_OPEN_FULLMUTEX", SQLITE_OPEN_FULLMUTEX }, |
| 5557 | { "SQLITE_OPEN_SHAREDCACHE", SQLITE_OPEN_SHAREDCACHE }, |
| 5558 | { "SQLITE_OPEN_PRIVATECACHE", SQLITE_OPEN_PRIVATECACHE }, |
| 5559 | { "SQLITE_OPEN_WAL", SQLITE_OPEN_WAL }, |
| 5560 | { "SQLITE_OPEN_URI", SQLITE_OPEN_URI }, |
| 5561 | { "SQLITE_OPEN_EXRESCODE", SQLITE_OPEN_EXRESCODE }, |
| 5562 | { 0, 0 } |
| 5563 | }; |
| 5564 | rc = Tcl_GetIndexFromObjStruct(interp, apFlag[i], aFlag, sizeof(aFlag[0]), |
| 5565 | "flag", 0, &iFlag |
| 5566 | ); |
| 5567 | if( rc!=TCL_OK ) return rc; |
| 5568 | flags |= aFlag[iFlag].flag; |
| 5569 | } |
| 5570 | |
| 5571 | rc = sqlite3_open_v2(zFilename, &db, flags, zVfs); |
| 5572 | if( sqlite3TestMakePointerStr(interp, zBuf, db) ) return TCL_ERROR; |
| 5573 | Tcl_AppendResult(interp, zBuf, NULL); |
| 5574 | return TCL_OK; |
| 5575 | } |
| 5576 | |
| 5577 | /* |
| 5578 | ** Usage: sqlite3_open16 filename options |
| 5579 | */ |
| 5580 | static int SQLITE_TCLAPI test_open16( |
| 5581 | void * clientData, |
| 5582 | Tcl_Interp *interp, |
| 5583 | int objc, |
| 5584 | Tcl_Obj *CONST objv[] |
| 5585 | ){ |
| 5586 | #ifndef SQLITE_OMIT_UTF16 |
| 5587 | const void *zFilename; |
| 5588 | sqlite3 *db; |
| 5589 | char zBuf[100]; |
| 5590 | |
| 5591 | if( objc!=3 ){ |
| 5592 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 5593 | Tcl_GetString(objv[0]), " filename options-list", NULL); |
| 5594 | return TCL_ERROR; |
| 5595 | } |
| 5596 | |
| 5597 | zFilename = Tcl_GetByteArrayFromObj(objv[1], 0); |
| 5598 | sqlite3_open16(zFilename, &db); |
| 5599 | |
| 5600 | if( sqlite3TestMakePointerStr(interp, zBuf, db) ) return TCL_ERROR; |
| 5601 | Tcl_AppendResult(interp, zBuf, NULL); |
| 5602 | #endif /* SQLITE_OMIT_UTF16 */ |
| 5603 | return TCL_OK; |
| 5604 | } |
| 5605 | |
| 5606 | /* |
| 5607 | ** Usage: sqlite3_complete16 <UTF-16 string> |
| 5608 | ** |
| 5609 | ** Return 1 if the supplied argument is a complete SQL statement, or zero |
| 5610 | ** otherwise. |
| 5611 | */ |
| 5612 | static int SQLITE_TCLAPI test_complete16( |
| 5613 | void * clientData, |
| 5614 | Tcl_Interp *interp, |
| 5615 | int objc, |
| 5616 | Tcl_Obj *CONST objv[] |
| 5617 | ){ |
| 5618 | #if !defined(SQLITE_OMIT_COMPLETE) && !defined(SQLITE_OMIT_UTF16) |
| 5619 | char *zBuf; |
| 5620 | |
| 5621 | if( objc!=2 ){ |
| 5622 | Tcl_WrongNumArgs(interp, 1, objv, "<utf-16 sql>"); |
| 5623 | return TCL_ERROR; |
| 5624 | } |
| 5625 | |
| 5626 | zBuf = (char*)Tcl_GetByteArrayFromObj(objv[1], 0); |
| 5627 | Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_complete16(zBuf))); |
| 5628 | #endif /* SQLITE_OMIT_COMPLETE && SQLITE_OMIT_UTF16 */ |
| 5629 | return TCL_OK; |
| 5630 | } |
| 5631 | |
| 5632 | /* |
| 5633 | ** Usage: sqlite3_normalize SQL |
| 5634 | ** |
| 5635 | ** Return the normalized value for an SQL statement. |
| 5636 | */ |
| 5637 | static int SQLITE_TCLAPI test_normalize( |
| 5638 | void * clientData, |
| 5639 | Tcl_Interp *interp, |
| 5640 | int objc, |
| 5641 | Tcl_Obj *CONST objv[] |
| 5642 | ){ |
| 5643 | char *zSql; |
| 5644 | char *zNorm; |
| 5645 | extern char *sqlite3_normalize(const char*); |
| 5646 | |
| 5647 | if( objc!=2 ){ |
| 5648 | Tcl_WrongNumArgs(interp, 1, objv, "SQL"); |
| 5649 | return TCL_ERROR; |
| 5650 | } |
| 5651 | |
| 5652 | zSql = (char*)Tcl_GetString(objv[1]); |
| 5653 | zNorm = sqlite3_normalize(zSql); |
| 5654 | if( zNorm ){ |
| 5655 | Tcl_SetObjResult(interp, Tcl_NewStringObj(zNorm, -1)); |
| 5656 | sqlite3_free(zNorm); |
| 5657 | } |
| 5658 | return TCL_OK; |
| 5659 | } |
| 5660 | |
| 5661 | /* |
| 5662 | ** Usage: sqlite3_step STMT |
| 5663 | ** |
| 5664 | ** Advance the statement to the next row. |
| 5665 | */ |
| 5666 | static int SQLITE_TCLAPI test_step( |
| 5667 | void * clientData, |
| 5668 | Tcl_Interp *interp, |
| 5669 | int objc, |
| 5670 | Tcl_Obj *CONST objv[] |
| 5671 | ){ |
| 5672 | sqlite3_stmt *pStmt; |
| 5673 | int rc; |
| 5674 | |
| 5675 | if( objc!=2 ){ |
| 5676 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 5677 | Tcl_GetString(objv[0]), " STMT", NULL); |
| 5678 | return TCL_ERROR; |
| 5679 | } |
| 5680 | |
| 5681 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 5682 | rc = sqlite3_step(pStmt); |
| 5683 | |
| 5684 | /* if( rc!=SQLITE_DONE && rc!=SQLITE_ROW ) return TCL_ERROR; */ |
| 5685 | Tcl_SetResult(interp, (char *)t1ErrorName(rc), 0); |
| 5686 | return TCL_OK; |
| 5687 | } |
| 5688 | |
| 5689 | static int SQLITE_TCLAPI test_sql( |
| 5690 | void * clientData, |
| 5691 | Tcl_Interp *interp, |
| 5692 | int objc, |
| 5693 | Tcl_Obj *CONST objv[] |
| 5694 | ){ |
| 5695 | sqlite3_stmt *pStmt; |
| 5696 | |
| 5697 | if( objc!=2 ){ |
| 5698 | Tcl_WrongNumArgs(interp, 1, objv, "STMT"); |
| 5699 | return TCL_ERROR; |
| 5700 | } |
| 5701 | |
| 5702 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 5703 | Tcl_SetResult(interp, (char *)sqlite3_sql(pStmt), TCL_VOLATILE); |
| 5704 | return TCL_OK; |
| 5705 | } |
| 5706 | static int SQLITE_TCLAPI test_ex_sql( |
| 5707 | void * clientData, |
| 5708 | Tcl_Interp *interp, |
| 5709 | int objc, |
| 5710 | Tcl_Obj *CONST objv[] |
| 5711 | ){ |
| 5712 | sqlite3_stmt *pStmt; |
| 5713 | char *z; |
| 5714 | |
| 5715 | if( objc!=2 ){ |
| 5716 | Tcl_WrongNumArgs(interp, 1, objv, "STMT"); |
| 5717 | return TCL_ERROR; |
| 5718 | } |
| 5719 | |
| 5720 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 5721 | z = sqlite3_expanded_sql(pStmt); |
| 5722 | Tcl_SetResult(interp, z, TCL_VOLATILE); |
| 5723 | sqlite3_free(z); |
| 5724 | return TCL_OK; |
| 5725 | } |
| 5726 | #ifdef SQLITE_ENABLE_NORMALIZE |
| 5727 | static int SQLITE_TCLAPI test_norm_sql( |
| 5728 | void * clientData, |
| 5729 | Tcl_Interp *interp, |
| 5730 | int objc, |
| 5731 | Tcl_Obj *CONST objv[] |
| 5732 | ){ |
| 5733 | sqlite3_stmt *pStmt; |
| 5734 | |
| 5735 | if( objc!=2 ){ |
| 5736 | Tcl_WrongNumArgs(interp, 1, objv, "STMT"); |
| 5737 | return TCL_ERROR; |
| 5738 | } |
| 5739 | |
| 5740 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 5741 | Tcl_SetResult(interp, (char *)sqlite3_normalized_sql(pStmt), TCL_VOLATILE); |
| 5742 | return TCL_OK; |
| 5743 | } |
| 5744 | #endif /* SQLITE_ENABLE_NORMALIZE */ |
| 5745 | |
| 5746 | /* |
| 5747 | ** Usage: sqlite3_column_count STMT |
| 5748 | ** |
| 5749 | ** Return the number of columns returned by the sql statement STMT. |
| 5750 | */ |
| 5751 | static int SQLITE_TCLAPI test_column_count( |
| 5752 | void * clientData, |
| 5753 | Tcl_Interp *interp, |
| 5754 | int objc, |
| 5755 | Tcl_Obj *CONST objv[] |
| 5756 | ){ |
| 5757 | sqlite3_stmt *pStmt; |
| 5758 | |
| 5759 | if( objc!=2 ){ |
| 5760 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 5761 | Tcl_GetString(objv[0]), " STMT column", NULL); |
| 5762 | return TCL_ERROR; |
| 5763 | } |
| 5764 | |
| 5765 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 5766 | |
| 5767 | Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_column_count(pStmt))); |
| 5768 | return TCL_OK; |
| 5769 | } |
| 5770 | |
| 5771 | /* |
| 5772 | ** Usage: sqlite3_column_type STMT column |
| 5773 | ** |
| 5774 | ** Return the type of the data in column 'column' of the current row. |
| 5775 | */ |
| 5776 | static int SQLITE_TCLAPI test_column_type( |
| 5777 | void * clientData, |
| 5778 | Tcl_Interp *interp, |
| 5779 | int objc, |
| 5780 | Tcl_Obj *CONST objv[] |
| 5781 | ){ |
| 5782 | sqlite3_stmt *pStmt; |
| 5783 | int col; |
| 5784 | int tp; |
| 5785 | |
| 5786 | if( objc!=3 ){ |
| 5787 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 5788 | Tcl_GetString(objv[0]), " STMT column", NULL); |
| 5789 | return TCL_ERROR; |
| 5790 | } |
| 5791 | |
| 5792 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 5793 | if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR; |
| 5794 | |
| 5795 | tp = sqlite3_column_type(pStmt, col); |
| 5796 | switch( tp ){ |
| 5797 | case SQLITE_INTEGER: |
| 5798 | Tcl_SetResult(interp, "INTEGER", TCL_STATIC); |
| 5799 | break; |
| 5800 | case SQLITE_NULL: |
| 5801 | Tcl_SetResult(interp, "NULL", TCL_STATIC); |
| 5802 | break; |
| 5803 | case SQLITE_FLOAT: |
| 5804 | Tcl_SetResult(interp, "FLOAT", TCL_STATIC); |
| 5805 | break; |
| 5806 | case SQLITE_TEXT: |
| 5807 | Tcl_SetResult(interp, "TEXT", TCL_STATIC); |
| 5808 | break; |
| 5809 | case SQLITE_BLOB: |
| 5810 | Tcl_SetResult(interp, "BLOB", TCL_STATIC); |
| 5811 | break; |
| 5812 | default: |
| 5813 | assert(0); |
| 5814 | } |
| 5815 | |
| 5816 | return TCL_OK; |
| 5817 | } |
| 5818 | |
| 5819 | /* |
| 5820 | ** Usage: sqlite3_column_int64 STMT column |
| 5821 | ** |
| 5822 | ** Return the data in column 'column' of the current row cast as an |
| 5823 | ** wide (64-bit) integer. |
| 5824 | */ |
| 5825 | static int SQLITE_TCLAPI test_column_int64( |
| 5826 | void * clientData, |
| 5827 | Tcl_Interp *interp, |
| 5828 | int objc, |
| 5829 | Tcl_Obj *CONST objv[] |
| 5830 | ){ |
| 5831 | sqlite3_stmt *pStmt; |
| 5832 | int col; |
| 5833 | i64 iVal; |
| 5834 | |
| 5835 | if( objc!=3 ){ |
| 5836 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 5837 | Tcl_GetString(objv[0]), " STMT column", NULL); |
| 5838 | return TCL_ERROR; |
| 5839 | } |
| 5840 | |
| 5841 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 5842 | if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR; |
| 5843 | |
| 5844 | iVal = sqlite3_column_int64(pStmt, col); |
| 5845 | Tcl_SetObjResult(interp, Tcl_NewWideIntObj(iVal)); |
| 5846 | return TCL_OK; |
| 5847 | } |
| 5848 | |
| 5849 | /* |
| 5850 | ** Usage: sqlite3_column_blob STMT column |
| 5851 | */ |
| 5852 | static int SQLITE_TCLAPI test_column_blob( |
| 5853 | void * clientData, |
| 5854 | Tcl_Interp *interp, |
| 5855 | int objc, |
| 5856 | Tcl_Obj *CONST objv[] |
| 5857 | ){ |
| 5858 | sqlite3_stmt *pStmt; |
| 5859 | int col; |
| 5860 | |
| 5861 | int len; |
| 5862 | const void *pBlob; |
| 5863 | |
| 5864 | if( objc!=3 ){ |
| 5865 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 5866 | Tcl_GetString(objv[0]), " STMT column", NULL); |
| 5867 | return TCL_ERROR; |
| 5868 | } |
| 5869 | |
| 5870 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 5871 | if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR; |
| 5872 | |
| 5873 | len = sqlite3_column_bytes(pStmt, col); |
| 5874 | pBlob = sqlite3_column_blob(pStmt, col); |
| 5875 | Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(pBlob, len)); |
| 5876 | return TCL_OK; |
| 5877 | } |
| 5878 | |
| 5879 | /* |
| 5880 | ** Usage: sqlite3_column_double STMT column |
| 5881 | ** |
| 5882 | ** Return the data in column 'column' of the current row cast as a double. |
| 5883 | */ |
| 5884 | static int SQLITE_TCLAPI test_column_double( |
| 5885 | void * clientData, |
| 5886 | Tcl_Interp *interp, |
| 5887 | int objc, |
| 5888 | Tcl_Obj *CONST objv[] |
| 5889 | ){ |
| 5890 | sqlite3_stmt *pStmt; |
| 5891 | int col; |
| 5892 | double rVal; |
| 5893 | |
| 5894 | if( objc!=3 ){ |
| 5895 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 5896 | Tcl_GetString(objv[0]), " STMT column", NULL); |
| 5897 | return TCL_ERROR; |
| 5898 | } |
| 5899 | |
| 5900 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 5901 | if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR; |
| 5902 | |
| 5903 | rVal = sqlite3_column_double(pStmt, col); |
| 5904 | Tcl_SetObjResult(interp, Tcl_NewDoubleObj(rVal)); |
| 5905 | return TCL_OK; |
| 5906 | } |
| 5907 | |
| 5908 | /* |
| 5909 | ** Usage: sqlite3_data_count STMT |
| 5910 | ** |
| 5911 | ** Return the number of columns returned by the sql statement STMT. |
| 5912 | */ |
| 5913 | static int SQLITE_TCLAPI test_data_count( |
| 5914 | void * clientData, |
| 5915 | Tcl_Interp *interp, |
| 5916 | int objc, |
| 5917 | Tcl_Obj *CONST objv[] |
| 5918 | ){ |
| 5919 | sqlite3_stmt *pStmt; |
| 5920 | |
| 5921 | if( objc!=2 ){ |
| 5922 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 5923 | Tcl_GetString(objv[0]), " STMT column", NULL); |
| 5924 | return TCL_ERROR; |
| 5925 | } |
| 5926 | |
| 5927 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 5928 | |
| 5929 | Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_data_count(pStmt))); |
| 5930 | return TCL_OK; |
| 5931 | } |
| 5932 | |
| 5933 | /* |
| 5934 | ** Usage: sqlite3_column_text STMT column |
| 5935 | ** |
| 5936 | ** Usage: sqlite3_column_decltype STMT column |
| 5937 | ** |
| 5938 | ** Usage: sqlite3_column_name STMT column |
| 5939 | */ |
| 5940 | static int SQLITE_TCLAPI test_stmt_utf8( |
| 5941 | void * clientData, /* Pointer to SQLite API function to be invoke */ |
| 5942 | Tcl_Interp *interp, |
| 5943 | int objc, |
| 5944 | Tcl_Obj *CONST objv[] |
| 5945 | ){ |
| 5946 | sqlite3_stmt *pStmt; |
| 5947 | int col; |
| 5948 | const char *(*xFunc)(sqlite3_stmt*, int); |
| 5949 | const unsigned char *(*xFuncU)(sqlite3_stmt*, int); |
| 5950 | const char *zRet; |
| 5951 | |
| 5952 | xFunc = (const char *(*)(sqlite3_stmt*, int))clientData; |
| 5953 | xFuncU = (const unsigned char*(*)(sqlite3_stmt*,int))xFunc; |
| 5954 | if( objc!=3 ){ |
| 5955 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 5956 | Tcl_GetString(objv[0]), " STMT column", NULL); |
| 5957 | return TCL_ERROR; |
| 5958 | } |
| 5959 | |
| 5960 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 5961 | if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR; |
| 5962 | if( xFunc==sqlite3_column_name || xFunc==sqlite3_column_decltype ){ |
| 5963 | zRet = xFunc(pStmt, col); |
| 5964 | }else{ |
| 5965 | zRet = (const char*)xFuncU(pStmt, col); |
| 5966 | } |
| 5967 | if( zRet ){ |
| 5968 | Tcl_SetResult(interp, (char *)zRet, 0); |
| 5969 | } |
| 5970 | return TCL_OK; |
| 5971 | } |
| 5972 | |
| 5973 | static int SQLITE_TCLAPI test_global_recover( |
| 5974 | void * clientData, |
| 5975 | Tcl_Interp *interp, |
| 5976 | int objc, |
| 5977 | Tcl_Obj *CONST objv[] |
| 5978 | ){ |
| 5979 | #ifndef SQLITE_OMIT_DEPRECATED |
| 5980 | int rc; |
| 5981 | if( objc!=1 ){ |
| 5982 | Tcl_WrongNumArgs(interp, 1, objv, ""); |
| 5983 | return TCL_ERROR; |
| 5984 | } |
| 5985 | rc = sqlite3_global_recover(); |
| 5986 | Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC); |
| 5987 | #endif |
| 5988 | return TCL_OK; |
| 5989 | } |
| 5990 | |
| 5991 | /* |
| 5992 | ** Usage: sqlite3_column_text STMT column |
| 5993 | ** |
| 5994 | ** Usage: sqlite3_column_decltype STMT column |
| 5995 | ** |
| 5996 | ** Usage: sqlite3_column_name STMT column |
| 5997 | */ |
| 5998 | static int SQLITE_TCLAPI test_stmt_utf16( |
| 5999 | void * clientData, /* Pointer to SQLite API function to be invoked */ |
| 6000 | Tcl_Interp *interp, |
| 6001 | int objc, |
| 6002 | Tcl_Obj *CONST objv[] |
| 6003 | ){ |
| 6004 | #ifndef SQLITE_OMIT_UTF16 |
| 6005 | sqlite3_stmt *pStmt; |
| 6006 | int col; |
| 6007 | Tcl_Obj *pRet; |
| 6008 | const void *zName16; |
| 6009 | const void *(*xFunc)(sqlite3_stmt*, int); |
| 6010 | |
| 6011 | xFunc = (const void *(*)(sqlite3_stmt*, int))clientData; |
| 6012 | if( objc!=3 ){ |
| 6013 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 6014 | Tcl_GetString(objv[0]), " STMT column", NULL); |
| 6015 | return TCL_ERROR; |
| 6016 | } |
| 6017 | |
| 6018 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 6019 | if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR; |
| 6020 | |
| 6021 | zName16 = xFunc(pStmt, col); |
| 6022 | if( zName16 ){ |
| 6023 | int n; |
| 6024 | const char *z = zName16; |
| 6025 | for(n=0; z[n] || z[n+1]; n+=2){} |
| 6026 | pRet = Tcl_NewByteArrayObj(zName16, n+2); |
| 6027 | Tcl_SetObjResult(interp, pRet); |
| 6028 | } |
| 6029 | #endif /* SQLITE_OMIT_UTF16 */ |
| 6030 | |
| 6031 | return TCL_OK; |
| 6032 | } |
| 6033 | |
| 6034 | /* |
| 6035 | ** Usage: sqlite3_column_int STMT column |
| 6036 | ** |
| 6037 | ** Usage: sqlite3_column_bytes STMT column |
| 6038 | ** |
| 6039 | ** Usage: sqlite3_column_bytes16 STMT column |
| 6040 | ** |
| 6041 | */ |
| 6042 | static int SQLITE_TCLAPI test_stmt_int( |
| 6043 | void * clientData, /* Pointer to SQLite API function to be invoked */ |
| 6044 | Tcl_Interp *interp, |
| 6045 | int objc, |
| 6046 | Tcl_Obj *CONST objv[] |
| 6047 | ){ |
| 6048 | sqlite3_stmt *pStmt; |
| 6049 | int col; |
| 6050 | int (*xFunc)(sqlite3_stmt*, int); |
| 6051 | |
| 6052 | xFunc = (int (*)(sqlite3_stmt*, int))clientData; |
| 6053 | if( objc!=3 ){ |
| 6054 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 6055 | Tcl_GetString(objv[0]), " STMT column", NULL); |
| 6056 | return TCL_ERROR; |
| 6057 | } |
| 6058 | |
| 6059 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 6060 | if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR; |
| 6061 | |
| 6062 | Tcl_SetObjResult(interp, Tcl_NewIntObj(xFunc(pStmt, col))); |
| 6063 | return TCL_OK; |
| 6064 | } |
| 6065 | |
| 6066 | /* |
| 6067 | ** Usage: sqlite3_interrupt DB |
| 6068 | ** |
| 6069 | ** Trigger an interrupt on DB |
| 6070 | */ |
| 6071 | static int SQLITE_TCLAPI test_interrupt( |
| 6072 | void * clientData, |
| 6073 | Tcl_Interp *interp, |
| 6074 | int argc, |
| 6075 | char **argv |
| 6076 | ){ |
| 6077 | sqlite3 *db; |
| 6078 | if( argc!=2 ){ |
| 6079 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " DB", NULL); |
| 6080 | return TCL_ERROR; |
| 6081 | } |
| 6082 | if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; |
| 6083 | sqlite3_interrupt(db); |
| 6084 | return TCL_OK; |
| 6085 | } |
| 6086 | |
| 6087 | /* |
| 6088 | ** Usage: sqlite3_is_interrupted DB |
| 6089 | ** |
| 6090 | ** return true if an interrupt is current in effect on DB |
| 6091 | */ |
| 6092 | static int SQLITE_TCLAPI test_is_interrupted( |
| 6093 | void * clientData, |
| 6094 | Tcl_Interp *interp, |
| 6095 | int argc, |
| 6096 | char **argv |
| 6097 | ){ |
| 6098 | sqlite3 *db; |
| 6099 | int rc; |
| 6100 | if( argc!=2 ){ |
| 6101 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " DB", NULL); |
| 6102 | return TCL_ERROR; |
| 6103 | } |
| 6104 | if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; |
| 6105 | rc = sqlite3_is_interrupted(db); |
| 6106 | Tcl_AppendResult(interp, rc ? "1" : "0", (void*)0); |
| 6107 | return TCL_OK; |
| 6108 | } |
| 6109 | |
| 6110 | /* |
| 6111 | ** Usage: sqlite_delete_function DB function-name |
| 6112 | ** |
| 6113 | ** Delete the user function 'function-name' from database handle DB. It |
| 6114 | ** is assumed that the user function was created as UTF8, any number of |
| 6115 | ** arguments (the way the TCL interface does it). |
| 6116 | */ |
| 6117 | static int SQLITE_TCLAPI delete_function( |
| 6118 | void * clientData, |
| 6119 | Tcl_Interp *interp, |
| 6120 | int argc, |
| 6121 | char **argv |
| 6122 | ){ |
| 6123 | int rc; |
| 6124 | sqlite3 *db; |
| 6125 | if( argc!=3 ){ |
| 6126 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 6127 | " DB function-name", 0); |
| 6128 | return TCL_ERROR; |
| 6129 | } |
| 6130 | if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; |
| 6131 | rc = sqlite3_create_function(db, argv[2], -1, SQLITE_UTF8, 0, 0, 0, 0); |
| 6132 | Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC); |
| 6133 | return TCL_OK; |
| 6134 | } |
| 6135 | |
| 6136 | /* |
| 6137 | ** Usage: sqlite_delete_collation DB collation-name |
| 6138 | ** |
| 6139 | ** Delete the collation sequence 'collation-name' from database handle |
| 6140 | ** DB. It is assumed that the collation sequence was created as UTF8 (the |
| 6141 | ** way the TCL interface does it). |
| 6142 | */ |
| 6143 | static int SQLITE_TCLAPI delete_collation( |
| 6144 | void * clientData, |
| 6145 | Tcl_Interp *interp, |
| 6146 | int argc, |
| 6147 | char **argv |
| 6148 | ){ |
| 6149 | int rc; |
| 6150 | sqlite3 *db; |
| 6151 | if( argc!=3 ){ |
| 6152 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 6153 | " DB function-name", 0); |
| 6154 | return TCL_ERROR; |
| 6155 | } |
| 6156 | if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; |
| 6157 | rc = sqlite3_create_collation(db, argv[2], SQLITE_UTF8, 0, 0); |
| 6158 | Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC); |
| 6159 | return TCL_OK; |
| 6160 | } |
| 6161 | |
| 6162 | /* |
| 6163 | ** Usage: sqlite3_get_autocommit DB |
| 6164 | ** |
| 6165 | ** Return true if the database DB is currently in auto-commit mode. |
| 6166 | ** Return false if not. |
| 6167 | */ |
| 6168 | static int SQLITE_TCLAPI get_autocommit( |
| 6169 | void * clientData, |
| 6170 | Tcl_Interp *interp, |
| 6171 | int argc, |
| 6172 | char **argv |
| 6173 | ){ |
| 6174 | char zBuf[30]; |
| 6175 | sqlite3 *db; |
| 6176 | if( argc!=2 ){ |
| 6177 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 6178 | " DB", 0); |
| 6179 | return TCL_ERROR; |
| 6180 | } |
| 6181 | if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; |
| 6182 | sqlite3_snprintf(sizeof(zBuf), zBuf, "%d", sqlite3_get_autocommit(db)); |
| 6183 | Tcl_AppendResult(interp, zBuf, NULL); |
| 6184 | return TCL_OK; |
| 6185 | } |
| 6186 | |
| 6187 | /* |
| 6188 | ** Usage: sqlite3_busy_timeout DB MS |
| 6189 | ** |
| 6190 | ** Set the busy timeout. This is more easily done using the timeout |
| 6191 | ** method of the TCL interface. But we need a way to test the case |
| 6192 | ** where it returns SQLITE_MISUSE. |
| 6193 | */ |
| 6194 | static int SQLITE_TCLAPI test_busy_timeout( |
| 6195 | void * clientData, |
| 6196 | Tcl_Interp *interp, |
| 6197 | int argc, |
| 6198 | char **argv |
| 6199 | ){ |
| 6200 | int rc, ms; |
| 6201 | sqlite3 *db; |
| 6202 | if( argc!=3 ){ |
| 6203 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 6204 | " DB", NULL); |
| 6205 | return TCL_ERROR; |
| 6206 | } |
| 6207 | if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; |
| 6208 | if( Tcl_GetInt(interp, argv[2], &ms) ) return TCL_ERROR; |
| 6209 | rc = sqlite3_busy_timeout(db, ms); |
| 6210 | Tcl_AppendResult(interp, sqlite3ErrName(rc), NULL); |
| 6211 | return TCL_OK; |
| 6212 | } |
| 6213 | |
| 6214 | /* |
| 6215 | ** Usage: sqlite3_setlk_timeout ?-blockonconnect? DB MS |
| 6216 | ** |
| 6217 | ** Set the setlk timeout. |
| 6218 | */ |
| 6219 | static int SQLITE_TCLAPI test_setlk_timeout( |
| 6220 | void * clientData, |
| 6221 | Tcl_Interp *interp, |
| 6222 | int argc, |
| 6223 | char **argv |
| 6224 | ){ |
| 6225 | int rc, ms; |
| 6226 | sqlite3 *db; |
| 6227 | int bBlockOnConnect = 0; |
| 6228 | |
| 6229 | if( argc==4 ){ |
| 6230 | const char *zArg = argv[1]; |
| 6231 | const size_t nArg = strlen(zArg); |
| 6232 | if( nArg>=2 && nArg<=15 && memcmp(zArg, "-blockonconnect", nArg)==0 ){ |
| 6233 | bBlockOnConnect = 1; |
| 6234 | } |
| 6235 | } |
| 6236 | if( argc!=(3+bBlockOnConnect) ){ |
| 6237 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 6238 | " ?-blockonconnect? DB MS", NULL); |
| 6239 | return TCL_ERROR; |
| 6240 | } |
| 6241 | if( getDbPointer(interp, argv[argc-2], &db) ) return TCL_ERROR; |
| 6242 | if( Tcl_GetInt(interp, argv[argc-1], &ms) ) return TCL_ERROR; |
| 6243 | rc = sqlite3_setlk_timeout( |
| 6244 | db, ms, (bBlockOnConnect ? SQLITE_SETLK_BLOCK_ON_CONNECT : 0) |
| 6245 | ); |
| 6246 | Tcl_AppendResult(interp, sqlite3ErrName(rc), NULL); |
| 6247 | return TCL_OK; |
| 6248 | } |
| 6249 | |
| 6250 | /* |
| 6251 | ** Usage: tcl_variable_type VARIABLENAME |
| 6252 | ** |
| 6253 | ** Return the name of the internal representation for the |
| 6254 | ** value of the given variable. |
| 6255 | */ |
| 6256 | static int SQLITE_TCLAPI tcl_variable_type( |
| 6257 | void * clientData, |
| 6258 | Tcl_Interp *interp, |
| 6259 | int objc, |
| 6260 | Tcl_Obj *CONST objv[] |
| 6261 | ){ |
| 6262 | Tcl_Obj *pVar; |
| 6263 | if( objc!=2 ){ |
| 6264 | Tcl_WrongNumArgs(interp, 1, objv, "VARIABLE"); |
| 6265 | return TCL_ERROR; |
| 6266 | } |
| 6267 | pVar = Tcl_GetVar2Ex(interp, Tcl_GetString(objv[1]), 0, TCL_LEAVE_ERR_MSG); |
| 6268 | if( pVar==0 ) return TCL_ERROR; |
| 6269 | if( pVar->typePtr ){ |
| 6270 | Tcl_SetObjResult(interp, Tcl_NewStringObj(pVar->typePtr->name, -1)); |
| 6271 | } |
| 6272 | return TCL_OK; |
| 6273 | } |
| 6274 | |
| 6275 | #include <ctype.h> |
| 6276 | |
| 6277 | /* |
| 6278 | ** Usage: fpnum_compare STRING1 STRING2 |
| 6279 | ** |
| 6280 | ** Compare two strings. Return true if the strings are the same and |
| 6281 | ** false if they differ. |
| 6282 | ** |
| 6283 | ** For this comparison, the strings are analyzed as a sequenced of |
| 6284 | ** whitespace separated tokens. The whitespace is ignored. Only the |
| 6285 | ** tokens are compared. Comparison rules: |
| 6286 | ** |
| 6287 | ** A. Tokens that are not floating-point numbers must match exactly. |
| 6288 | ** |
| 6289 | ** B. Floating point number must have exactly the same digits before |
| 6290 | ** the decimal point. |
| 6291 | ** |
| 6292 | ** C. Digits must match after the decimal point up to 15 digits, |
| 6293 | ** taking rounding into consideration. |
| 6294 | ** |
| 6295 | ** D. An exponent on a floating point of the form "e+NN" will |
| 6296 | ** match "e+N" if NN==N. Likewise for the negative exponent. |
| 6297 | ** |
| 6298 | ** This routine is used for comparing results that might involve floating |
| 6299 | ** point values. Tcl9.0 and Tcl8.6 differ in the number of significant |
| 6300 | ** digits that they show, so there is no way to write a portable test result |
| 6301 | ** without this routine. |
| 6302 | ** |
| 6303 | ** This routine is only called after [string compare] fails, which is seldom, |
| 6304 | ** so performance is not a pressing concern. Better to get the correct answer |
| 6305 | ** slowly. |
| 6306 | */ |
| 6307 | static int SQLITE_TCLAPI fpnum_compare( |
| 6308 | void * clientData, |
| 6309 | Tcl_Interp *interp, |
| 6310 | int objc, |
| 6311 | Tcl_Obj *CONST objv[] |
| 6312 | ){ |
| 6313 | const unsigned char *zA; |
| 6314 | const unsigned char *zB; |
| 6315 | int i, j; |
| 6316 | int nDigit; |
| 6317 | |
| 6318 | if( objc!=3 ){ |
| 6319 | Tcl_WrongNumArgs(interp, 1, objv, "STRING1 STRING2"); |
| 6320 | return TCL_ERROR; |
| 6321 | } |
| 6322 | zA = (const unsigned char*)Tcl_GetString(objv[1]); |
| 6323 | zB = (const unsigned char*)Tcl_GetString(objv[2]); |
| 6324 | i = j = 0; |
| 6325 | while( 1 ){ |
| 6326 | /* Skip whitespace before and after tokens */ |
| 6327 | while( isspace(zA[i]) ){ i++; } |
| 6328 | while( isspace(zB[j]) ){ j++; } |
| 6329 | |
| 6330 | if( zA[i]!=zB[j] ) break; /* First character must match */ |
| 6331 | if( zA[i]=='-' && isdigit(zA[i+1]) ){ i++; j++; } /* Skip initial '-' */ |
| 6332 | if( !isdigit(zA[i]) ){ |
| 6333 | /* Not a number. Must match exactly */ |
| 6334 | while( !isspace(zA[i]) && zA[i] && zA[i]==zB[j] ){ i++; j++; } |
| 6335 | if( zA[i]!=zB[j] ) break; |
| 6336 | if( isspace(zA[i]) ) continue; |
| 6337 | break; |
| 6338 | } |
| 6339 | |
| 6340 | /* At this point we know we are dealing with a number zA[i] and zB[j] |
| 6341 | ** are both digits (leading "-" have been skipped). See if they are |
| 6342 | ** the same number. Start by matching digits before the decimal |
| 6343 | ** point, which must all be the same. */ |
| 6344 | nDigit = 0; |
| 6345 | while( zA[i]==zB[j] && isdigit(zA[i]) ){ i++; j++; nDigit++; } |
| 6346 | if( zA[i]!=zB[j] ) break; |
| 6347 | if( zA[i]==0 ) break; |
| 6348 | if( zA[i]=='.' && zB[j]=='.' ){ |
| 6349 | /* Count more matching digits after the decimal point */ |
| 6350 | i++; |
| 6351 | j++; |
| 6352 | while( zA[i]==zB[j] && isdigit(zA[i]) ){ i++; j++; nDigit++; } |
| 6353 | if( zA[i]==0 ){ |
| 6354 | while( zB[j]=='0' || (isdigit(zB[j]) && nDigit>=15) ){ j++; nDigit++; } |
| 6355 | break; |
| 6356 | } |
| 6357 | if( zB[j]==0 ){ |
| 6358 | while( zA[i]=='0' || (isdigit(zA[i]) && nDigit>=15) ){ i++; nDigit++; } |
| 6359 | break; |
| 6360 | } |
| 6361 | if( isspace(zA[i]) && isspace(zB[j]) ) continue; |
| 6362 | |
| 6363 | if( isdigit(zA[i]) && isdigit(zB[j]) ){ |
| 6364 | /* A and B are both digits, but different digits */ |
| 6365 | if( zA[i]==zB[j]+1 && !isdigit(zA[i+1]) && isdigit(zB[j+1]) ){ |
| 6366 | /* Is A a rounded up version of B? */ |
| 6367 | j++; |
| 6368 | while( zB[j]=='9' ){ j++; nDigit++; } |
| 6369 | if( nDigit<14 && (!isdigit(zB[j]) || zB[j]<5) ) break; |
| 6370 | while( isdigit(zB[j]) ){ j++; } |
| 6371 | i++; |
| 6372 | }else if( zB[j]==zA[i]+1 && !isdigit(zB[j+1]) && isdigit(zA[i+1]) ){ |
| 6373 | /* Is B a rounded up version of A? */ |
| 6374 | i++; |
| 6375 | while( zA[i]=='9' ){ i++; nDigit++; } |
| 6376 | if( nDigit<14 && (!isdigit(zA[i]) || zA[i]<5) ) break; |
| 6377 | while( isdigit(zA[i]) ){ i++; } |
| 6378 | j++; |
| 6379 | }else{ |
| 6380 | break; |
| 6381 | } |
| 6382 | }else if( !isdigit(zA[i]) && isdigit(zB[j]) ){ |
| 6383 | while( zB[j]=='0' ){ j++; nDigit++; } |
| 6384 | if( nDigit<15 ) break; |
| 6385 | while( isdigit(zB[j]) ){ j++; } |
| 6386 | }else if( !isdigit(zB[j]) && isdigit(zA[i]) ){ |
| 6387 | while( zA[i]=='0' ){ i++; nDigit++; } |
| 6388 | if( nDigit<15 ) break; |
| 6389 | while( isdigit(zA[i]) ){ i++; } |
| 6390 | }else{ |
| 6391 | break; |
| 6392 | } |
| 6393 | } |
| 6394 | if( zA[i]=='e' && zB[j]=='e' ){ |
| 6395 | i++; |
| 6396 | j++; |
| 6397 | if( (zA[i]=='+' || zA[i]=='-') && zB[j]==zA[i] ){ i++; j++; } |
| 6398 | if( zA[i]!=zB[j] ){ |
| 6399 | if( zA[i]=='0' && zA[i+1]==zB[j] ){ i++; } |
| 6400 | if( zB[j]=='0' && zB[j+1]==zA[i] ){ j++; } |
| 6401 | } |
| 6402 | while( zA[i]==zB[j] && isdigit(zA[i]) ){ i++; j++; } |
| 6403 | if( zA[i]!=zB[j] ) break; |
| 6404 | if( zA[i]==0 ) break; |
| 6405 | continue; |
| 6406 | } |
| 6407 | } |
| 6408 | while( isspace(zA[i]) ){ i++; } |
| 6409 | while( isspace(zB[j]) ){ j++; } |
| 6410 | Tcl_SetObjResult(interp, Tcl_NewIntObj(zA[i]==0 && zB[j]==0)); |
| 6411 | return TCL_OK; |
| 6412 | } |
| 6413 | |
| 6414 | /* |
| 6415 | ** Usage: sqlite3_release_memory ?N? |
| 6416 | ** |
| 6417 | ** Attempt to release memory currently held but not actually required. |
| 6418 | ** The integer N is the number of bytes we are trying to release. The |
| 6419 | ** return value is the amount of memory actually released. |
| 6420 | */ |
| 6421 | static int SQLITE_TCLAPI test_release_memory( |
| 6422 | void * clientData, |
| 6423 | Tcl_Interp *interp, |
| 6424 | int objc, |
| 6425 | Tcl_Obj *CONST objv[] |
| 6426 | ){ |
| 6427 | #if defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) && !defined(SQLITE_OMIT_DISKIO) |
| 6428 | int N; |
| 6429 | int amt; |
| 6430 | if( objc!=1 && objc!=2 ){ |
| 6431 | Tcl_WrongNumArgs(interp, 1, objv, "?N?"); |
| 6432 | return TCL_ERROR; |
| 6433 | } |
| 6434 | if( objc==2 ){ |
| 6435 | if( Tcl_GetIntFromObj(interp, objv[1], &N) ) return TCL_ERROR; |
| 6436 | }else{ |
| 6437 | N = -1; |
| 6438 | } |
| 6439 | amt = sqlite3_release_memory(N); |
| 6440 | Tcl_SetObjResult(interp, Tcl_NewIntObj(amt)); |
| 6441 | #endif |
| 6442 | return TCL_OK; |
| 6443 | } |
| 6444 | |
| 6445 | |
| 6446 | /* |
| 6447 | ** Usage: sqlite3_db_release_memory DB |
| 6448 | ** |
| 6449 | ** Attempt to release memory currently held by database DB. Return the |
| 6450 | ** result code (which in the current implementation is always zero). |
| 6451 | */ |
| 6452 | static int SQLITE_TCLAPI test_db_release_memory( |
| 6453 | void * clientData, |
| 6454 | Tcl_Interp *interp, |
| 6455 | int objc, |
| 6456 | Tcl_Obj *CONST objv[] |
| 6457 | ){ |
| 6458 | sqlite3 *db; |
| 6459 | int rc; |
| 6460 | if( objc!=2 ){ |
| 6461 | Tcl_WrongNumArgs(interp, 1, objv, "DB"); |
| 6462 | return TCL_ERROR; |
| 6463 | } |
| 6464 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 6465 | rc = sqlite3_db_release_memory(db); |
| 6466 | Tcl_SetObjResult(interp, Tcl_NewIntObj(rc)); |
| 6467 | return TCL_OK; |
| 6468 | } |
| 6469 | |
| 6470 | /* |
| 6471 | ** Usage: sqlite3_db_cacheflush DB |
| 6472 | ** |
| 6473 | ** Attempt to flush any dirty pages to disk. |
| 6474 | */ |
| 6475 | static int SQLITE_TCLAPI test_db_cacheflush( |
| 6476 | void * clientData, |
| 6477 | Tcl_Interp *interp, |
| 6478 | int objc, |
| 6479 | Tcl_Obj *CONST objv[] |
| 6480 | ){ |
| 6481 | sqlite3 *db; |
| 6482 | int rc; |
| 6483 | if( objc!=2 ){ |
| 6484 | Tcl_WrongNumArgs(interp, 1, objv, "DB"); |
| 6485 | return TCL_ERROR; |
| 6486 | } |
| 6487 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 6488 | rc = sqlite3_db_cacheflush(db); |
| 6489 | if( rc ){ |
| 6490 | Tcl_SetResult(interp, (char *)sqlite3ErrStr(rc), TCL_STATIC); |
| 6491 | return TCL_ERROR; |
| 6492 | } |
| 6493 | |
| 6494 | Tcl_ResetResult(interp); |
| 6495 | return TCL_OK; |
| 6496 | } |
| 6497 | |
| 6498 | /* |
| 6499 | ** Usage: sqlite3_system_errno DB |
| 6500 | ** |
| 6501 | ** Return the low-level system errno value. |
| 6502 | */ |
| 6503 | static int SQLITE_TCLAPI test_system_errno( |
| 6504 | void * clientData, |
| 6505 | Tcl_Interp *interp, |
| 6506 | int objc, |
| 6507 | Tcl_Obj *CONST objv[] |
| 6508 | ){ |
| 6509 | sqlite3 *db; |
| 6510 | int iErrno; |
| 6511 | if( objc!=2 ){ |
| 6512 | Tcl_WrongNumArgs(interp, 1, objv, "DB"); |
| 6513 | return TCL_ERROR; |
| 6514 | } |
| 6515 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 6516 | iErrno = sqlite3_system_errno(db); |
| 6517 | Tcl_SetObjResult(interp, Tcl_NewIntObj(iErrno)); |
| 6518 | return TCL_OK; |
| 6519 | } |
| 6520 | |
| 6521 | /* |
| 6522 | ** Usage: sqlite3_db_filename DB DBNAME |
| 6523 | ** |
| 6524 | ** Return the name of a file associated with a database. |
| 6525 | */ |
| 6526 | static int SQLITE_TCLAPI test_db_filename( |
| 6527 | void * clientData, |
| 6528 | Tcl_Interp *interp, |
| 6529 | int objc, |
| 6530 | Tcl_Obj *CONST objv[] |
| 6531 | ){ |
| 6532 | sqlite3 *db; |
| 6533 | const char *zDbName; |
| 6534 | if( objc!=3 ){ |
| 6535 | Tcl_WrongNumArgs(interp, 1, objv, "DB DBNAME"); |
| 6536 | return TCL_ERROR; |
| 6537 | } |
| 6538 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 6539 | zDbName = Tcl_GetString(objv[2]); |
| 6540 | Tcl_AppendResult(interp, sqlite3_db_filename(db, zDbName), (void*)0); |
| 6541 | return TCL_OK; |
| 6542 | } |
| 6543 | |
| 6544 | /* |
| 6545 | ** Usage: sqlite3_db_readonly DB DBNAME |
| 6546 | ** |
| 6547 | ** Return 1 or 0 if DBNAME is readonly or not. Return -1 if DBNAME does |
| 6548 | ** not exist. |
| 6549 | */ |
| 6550 | static int SQLITE_TCLAPI test_db_readonly( |
| 6551 | void * clientData, |
| 6552 | Tcl_Interp *interp, |
| 6553 | int objc, |
| 6554 | Tcl_Obj *CONST objv[] |
| 6555 | ){ |
| 6556 | sqlite3 *db; |
| 6557 | const char *zDbName; |
| 6558 | if( objc!=3 ){ |
| 6559 | Tcl_WrongNumArgs(interp, 1, objv, "DB DBNAME"); |
| 6560 | return TCL_ERROR; |
| 6561 | } |
| 6562 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 6563 | zDbName = Tcl_GetString(objv[2]); |
| 6564 | Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_db_readonly(db, zDbName))); |
| 6565 | return TCL_OK; |
| 6566 | } |
| 6567 | |
| 6568 | /* |
| 6569 | ** Usage: sqlite3_soft_heap_limit ?N? |
| 6570 | ** |
| 6571 | ** Query or set the soft heap limit for the current thread. The |
| 6572 | ** limit is only changed if the N is present. The previous limit |
| 6573 | ** is returned. |
| 6574 | */ |
| 6575 | static int SQLITE_TCLAPI test_soft_heap_limit( |
| 6576 | void * clientData, |
| 6577 | Tcl_Interp *interp, |
| 6578 | int objc, |
| 6579 | Tcl_Obj *CONST objv[] |
| 6580 | ){ |
| 6581 | sqlite3_int64 amt; |
| 6582 | Tcl_WideInt N = -1; |
| 6583 | if( objc!=1 && objc!=2 ){ |
| 6584 | Tcl_WrongNumArgs(interp, 1, objv, "?N?"); |
| 6585 | return TCL_ERROR; |
| 6586 | } |
| 6587 | if( objc==2 ){ |
| 6588 | if( Tcl_GetWideIntFromObj(interp, objv[1], &N) ) return TCL_ERROR; |
| 6589 | } |
| 6590 | amt = sqlite3_soft_heap_limit64(N); |
| 6591 | Tcl_SetObjResult(interp, Tcl_NewWideIntObj(amt)); |
| 6592 | return TCL_OK; |
| 6593 | } |
| 6594 | |
| 6595 | /* |
| 6596 | ** Usage: sqlite3_hard_heap_limit ?N? |
| 6597 | ** |
| 6598 | ** Query or set the hard heap limit for the current thread. The |
| 6599 | ** limit is only changed if the N is present. The previous limit |
| 6600 | ** is returned. |
| 6601 | */ |
| 6602 | static int SQLITE_TCLAPI test_hard_heap_limit( |
| 6603 | void * clientData, |
| 6604 | Tcl_Interp *interp, |
| 6605 | int objc, |
| 6606 | Tcl_Obj *CONST objv[] |
| 6607 | ){ |
| 6608 | sqlite3_int64 amt; |
| 6609 | Tcl_WideInt N = -1; |
| 6610 | if( objc!=1 && objc!=2 ){ |
| 6611 | Tcl_WrongNumArgs(interp, 1, objv, "?N?"); |
| 6612 | return TCL_ERROR; |
| 6613 | } |
| 6614 | if( objc==2 ){ |
| 6615 | if( Tcl_GetWideIntFromObj(interp, objv[1], &N) ) return TCL_ERROR; |
| 6616 | } |
| 6617 | amt = sqlite3_hard_heap_limit64(N); |
| 6618 | Tcl_SetObjResult(interp, Tcl_NewWideIntObj(amt)); |
| 6619 | return TCL_OK; |
| 6620 | } |
| 6621 | |
| 6622 | /* |
| 6623 | ** Usage: sqlite3_thread_cleanup |
| 6624 | ** |
| 6625 | ** Call the sqlite3_thread_cleanup API. |
| 6626 | */ |
| 6627 | static int SQLITE_TCLAPI test_thread_cleanup( |
| 6628 | void * clientData, |
| 6629 | Tcl_Interp *interp, |
| 6630 | int objc, |
| 6631 | Tcl_Obj *CONST objv[] |
| 6632 | ){ |
| 6633 | #ifndef SQLITE_OMIT_DEPRECATED |
| 6634 | sqlite3_thread_cleanup(); |
| 6635 | #endif |
| 6636 | return TCL_OK; |
| 6637 | } |
| 6638 | |
| 6639 | /* |
| 6640 | ** Usage: sqlite3_pager_refcounts DB |
| 6641 | ** |
| 6642 | ** Return a list of numbers which are the PagerRefcount for all |
| 6643 | ** pagers on each database connection. |
| 6644 | */ |
| 6645 | static int SQLITE_TCLAPI test_pager_refcounts( |
| 6646 | void * clientData, |
| 6647 | Tcl_Interp *interp, |
| 6648 | int objc, |
| 6649 | Tcl_Obj *CONST objv[] |
| 6650 | ){ |
| 6651 | sqlite3 *db; |
| 6652 | int i; |
| 6653 | int v, *a; |
| 6654 | Tcl_Obj *pResult; |
| 6655 | |
| 6656 | if( objc!=2 ){ |
| 6657 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 6658 | Tcl_GetStringFromObj(objv[0], 0), " DB", NULL); |
| 6659 | return TCL_ERROR; |
| 6660 | } |
| 6661 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 6662 | pResult = Tcl_NewObj(); |
| 6663 | for(i=0; i<db->nDb; i++){ |
| 6664 | if( db->aDb[i].pBt==0 ){ |
| 6665 | v = -1; |
| 6666 | }else{ |
| 6667 | sqlite3_mutex_enter(db->mutex); |
| 6668 | a = sqlite3PagerStats(sqlite3BtreePager(db->aDb[i].pBt)); |
| 6669 | v = a[0]; |
| 6670 | sqlite3_mutex_leave(db->mutex); |
| 6671 | } |
| 6672 | Tcl_ListObjAppendElement(0, pResult, Tcl_NewIntObj(v)); |
| 6673 | } |
| 6674 | Tcl_SetObjResult(interp, pResult); |
| 6675 | return TCL_OK; |
| 6676 | } |
| 6677 | |
| 6678 | |
| 6679 | /* |
| 6680 | ** tclcmd: working_64bit_int |
| 6681 | ** |
| 6682 | ** Some TCL builds (ex: cygwin) do not support 64-bit integers. This |
| 6683 | ** leads to a number of test failures. The present command checks the |
| 6684 | ** TCL build to see whether or not it supports 64-bit integers. It |
| 6685 | ** returns TRUE if it does and FALSE if not. |
| 6686 | ** |
| 6687 | ** This command is used to warn users that their TCL build is defective |
| 6688 | ** and that the errors they are seeing in the test scripts might be |
| 6689 | ** a result of their defective TCL rather than problems in SQLite. |
| 6690 | */ |
| 6691 | static int SQLITE_TCLAPI working_64bit_int( |
| 6692 | ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ |
| 6693 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 6694 | int objc, /* Number of arguments */ |
| 6695 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 6696 | ){ |
| 6697 | Tcl_Obj *pTestObj; |
| 6698 | int working = 0; |
| 6699 | |
| 6700 | pTestObj = Tcl_NewWideIntObj(1000000*(i64)1234567890); |
| 6701 | working = strcmp(Tcl_GetString(pTestObj), "1234567890000000")==0; |
| 6702 | Tcl_DecrRefCount(pTestObj); |
| 6703 | Tcl_SetObjResult(interp, Tcl_NewBooleanObj(working)); |
| 6704 | return TCL_OK; |
| 6705 | } |
| 6706 | |
| 6707 | |
| 6708 | /* |
| 6709 | ** tclcmd: vfs_open_tempfile ?VFS? |
| 6710 | ** |
| 6711 | ** Invoke the xOpen() method of VFS (or of the default VFS if no VFS is |
| 6712 | ** named) with a NULL filename, as the core does when it needs a temporary |
| 6713 | ** file. The file is closed again immediately. Return a two-element list |
| 6714 | ** consisting of the integer return code from xOpen() and the value that |
| 6715 | ** xOpen() wrote into *pOutFlags. *pOutFlags is initialized to zero before |
| 6716 | ** the call, so a result of zero means that xOpen() did not set it. |
| 6717 | */ |
| 6718 | static int SQLITE_TCLAPI vfs_open_tempfile( |
| 6719 | ClientData clientData, /* Not used */ |
| 6720 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 6721 | int objc, /* Number of arguments */ |
| 6722 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 6723 | ){ |
| 6724 | sqlite3_vfs *pVfs; |
| 6725 | sqlite3_file *pFd; |
| 6726 | int flags = SQLITE_OPEN_TEMP_DB | SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE |
| 6727 | | SQLITE_OPEN_EXCLUSIVE | SQLITE_OPEN_DELETEONCLOSE; |
| 6728 | int outFlags = 0; |
| 6729 | int rc; |
| 6730 | Tcl_Obj *pRet; |
| 6731 | |
| 6732 | if( objc!=1 && objc!=2 ){ |
| 6733 | Tcl_WrongNumArgs(interp, 1, objv, "?VFS?"); |
| 6734 | return TCL_ERROR; |
| 6735 | } |
| 6736 | pVfs = sqlite3_vfs_find(objc==2 ? Tcl_GetString(objv[1]) : 0); |
| 6737 | if( pVfs==0 ){ |
| 6738 | Tcl_AppendResult(interp, "no such vfs", (char*)0); |
| 6739 | return TCL_ERROR; |
| 6740 | } |
| 6741 | pFd = (sqlite3_file*)ckalloc(pVfs->szOsFile); |
| 6742 | memset(pFd, 0, pVfs->szOsFile); |
| 6743 | rc = pVfs->xOpen(pVfs, 0, pFd, flags, &outFlags); |
| 6744 | if( pFd->pMethods ) pFd->pMethods->xClose(pFd); |
| 6745 | ckfree((char*)pFd); |
| 6746 | |
| 6747 | pRet = Tcl_NewObj(); |
| 6748 | Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(rc)); |
| 6749 | Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(outFlags)); |
| 6750 | Tcl_SetObjResult(interp, pRet); |
| 6751 | return TCL_OK; |
| 6752 | } |
| 6753 | |
| 6754 | /* |
| 6755 | ** tclcmd: vfs_unlink_test |
| 6756 | ** |
| 6757 | ** This TCL command unregisters the primary VFS and then registers |
| 6758 | ** it back again. This is used to test the ability to register a |
| 6759 | ** VFS when none are previously registered, and the ability to |
| 6760 | ** unregister the only available VFS. Ticket #2738 |
| 6761 | */ |
| 6762 | static int SQLITE_TCLAPI vfs_unlink_test( |
| 6763 | ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ |
| 6764 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 6765 | int objc, /* Number of arguments */ |
| 6766 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 6767 | ){ |
| 6768 | int i; |
| 6769 | sqlite3_vfs *pMain; |
| 6770 | sqlite3_vfs *apVfs[20]; |
| 6771 | sqlite3_vfs one, two; |
| 6772 | |
| 6773 | sqlite3_vfs_unregister(0); /* Unregister of NULL is harmless */ |
| 6774 | one.zName = "__one"; |
| 6775 | two.zName = "__two"; |
| 6776 | |
| 6777 | /* Calling sqlite3_vfs_register with 2nd argument of 0 does not |
| 6778 | ** change the default VFS |
| 6779 | */ |
| 6780 | pMain = sqlite3_vfs_find(0); |
| 6781 | sqlite3_vfs_register(&one, 0); |
| 6782 | assert( pMain==0 || pMain==sqlite3_vfs_find(0) ); |
| 6783 | sqlite3_vfs_register(&two, 0); |
| 6784 | assert( pMain==0 || pMain==sqlite3_vfs_find(0) ); |
| 6785 | |
| 6786 | /* We can find a VFS by its name */ |
| 6787 | assert( sqlite3_vfs_find("__one")==&one ); |
| 6788 | assert( sqlite3_vfs_find("__two")==&two ); |
| 6789 | |
| 6790 | /* Calling sqlite_vfs_register with non-zero second parameter changes the |
| 6791 | ** default VFS, even if the 1st parameter is an existing VFS that is |
| 6792 | ** previously registered as the non-default. |
| 6793 | */ |
| 6794 | sqlite3_vfs_register(&one, 1); |
| 6795 | assert( sqlite3_vfs_find("__one")==&one ); |
| 6796 | assert( sqlite3_vfs_find("__two")==&two ); |
| 6797 | assert( sqlite3_vfs_find(0)==&one ); |
| 6798 | sqlite3_vfs_register(&two, 1); |
| 6799 | assert( sqlite3_vfs_find("__one")==&one ); |
| 6800 | assert( sqlite3_vfs_find("__two")==&two ); |
| 6801 | assert( sqlite3_vfs_find(0)==&two ); |
| 6802 | if( pMain ){ |
| 6803 | sqlite3_vfs_register(pMain, 1); |
| 6804 | assert( sqlite3_vfs_find("__one")==&one ); |
| 6805 | assert( sqlite3_vfs_find("__two")==&two ); |
| 6806 | assert( sqlite3_vfs_find(0)==pMain ); |
| 6807 | } |
| 6808 | |
| 6809 | /* Unlink the default VFS. Repeat until there are no more VFSes |
| 6810 | ** registered. |
| 6811 | */ |
| 6812 | for(i=0; i<sizeof(apVfs)/sizeof(apVfs[0]); i++){ |
| 6813 | apVfs[i] = sqlite3_vfs_find(0); |
| 6814 | if( apVfs[i] ){ |
| 6815 | assert( apVfs[i]==sqlite3_vfs_find(apVfs[i]->zName) ); |
| 6816 | sqlite3_vfs_unregister(apVfs[i]); |
| 6817 | assert( 0==sqlite3_vfs_find(apVfs[i]->zName) ); |
| 6818 | } |
| 6819 | } |
| 6820 | assert( 0==sqlite3_vfs_find(0) ); |
| 6821 | |
| 6822 | /* Register the main VFS as non-default (will be made default, since |
| 6823 | ** it'll be the only one in existence). |
| 6824 | */ |
| 6825 | sqlite3_vfs_register(pMain, 0); |
| 6826 | assert( sqlite3_vfs_find(0)==pMain ); |
| 6827 | |
| 6828 | /* Un-register the main VFS again to restore an empty VFS list */ |
| 6829 | sqlite3_vfs_unregister(pMain); |
| 6830 | assert( 0==sqlite3_vfs_find(0) ); |
| 6831 | |
| 6832 | /* Relink all VFSes in reverse order. */ |
| 6833 | for(i=sizeof(apVfs)/sizeof(apVfs[0])-1; i>=0; i--){ |
| 6834 | if( apVfs[i] ){ |
| 6835 | sqlite3_vfs_register(apVfs[i], 1); |
| 6836 | assert( apVfs[i]==sqlite3_vfs_find(0) ); |
| 6837 | assert( apVfs[i]==sqlite3_vfs_find(apVfs[i]->zName) ); |
| 6838 | } |
| 6839 | } |
| 6840 | |
| 6841 | /* Unregister out sample VFSes. */ |
| 6842 | sqlite3_vfs_unregister(&one); |
| 6843 | sqlite3_vfs_unregister(&two); |
| 6844 | |
| 6845 | /* Unregistering a VFS that is not currently registered is harmless */ |
| 6846 | sqlite3_vfs_unregister(&one); |
| 6847 | sqlite3_vfs_unregister(&two); |
| 6848 | assert( sqlite3_vfs_find("__one")==0 ); |
| 6849 | assert( sqlite3_vfs_find("__two")==0 ); |
| 6850 | |
| 6851 | /* We should be left with the original default VFS back as the |
| 6852 | ** original */ |
| 6853 | assert( sqlite3_vfs_find(0)==pMain ); |
| 6854 | |
| 6855 | return TCL_OK; |
| 6856 | } |
| 6857 | |
| 6858 | /* |
| 6859 | ** tclcmd: vfs_initfail_test |
| 6860 | ** |
| 6861 | ** This TCL command attempts to vfs_find and vfs_register when the |
| 6862 | ** sqlite3_initialize() interface is failing. All calls should fail. |
| 6863 | */ |
| 6864 | static int SQLITE_TCLAPI vfs_initfail_test( |
| 6865 | ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ |
| 6866 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 6867 | int objc, /* Number of arguments */ |
| 6868 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 6869 | ){ |
| 6870 | sqlite3_vfs one; |
| 6871 | one.zName = "__one"; |
| 6872 | |
| 6873 | if( sqlite3_vfs_find(0) ) return TCL_ERROR; |
| 6874 | sqlite3_vfs_register(&one, 0); |
| 6875 | if( sqlite3_vfs_find(0) ) return TCL_ERROR; |
| 6876 | sqlite3_vfs_register(&one, 1); |
| 6877 | if( sqlite3_vfs_find(0) ) return TCL_ERROR; |
| 6878 | return TCL_OK; |
| 6879 | } |
| 6880 | |
| 6881 | /* |
| 6882 | ** Saved VFSes |
| 6883 | */ |
| 6884 | static sqlite3_vfs *apVfs[20]; |
| 6885 | static int nVfs = 0; |
| 6886 | |
| 6887 | /* |
| 6888 | ** tclcmd: vfs_unregister_all |
| 6889 | ** |
| 6890 | ** Unregister all VFSes. |
| 6891 | */ |
| 6892 | static int SQLITE_TCLAPI vfs_unregister_all( |
| 6893 | ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ |
| 6894 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 6895 | int objc, /* Number of arguments */ |
| 6896 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 6897 | ){ |
| 6898 | int i; |
| 6899 | for(i=0; i<ArraySize(apVfs); i++){ |
| 6900 | apVfs[i] = sqlite3_vfs_find(0); |
| 6901 | if( apVfs[i]==0 ) break; |
| 6902 | sqlite3_vfs_unregister(apVfs[i]); |
| 6903 | } |
| 6904 | nVfs = i; |
| 6905 | return TCL_OK; |
| 6906 | } |
| 6907 | /* |
| 6908 | ** tclcmd: vfs_reregister_all |
| 6909 | ** |
| 6910 | ** Restore all VFSes that were removed using vfs_unregister_all. Taking |
| 6911 | ** care to put the linked list back together in the same order as it was |
| 6912 | ** in before vfs_unregister_all was invoked. |
| 6913 | */ |
| 6914 | static int SQLITE_TCLAPI vfs_reregister_all( |
| 6915 | ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ |
| 6916 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 6917 | int objc, /* Number of arguments */ |
| 6918 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 6919 | ){ |
| 6920 | int i; |
| 6921 | for(i=nVfs-1; i>=0; i--){ |
| 6922 | sqlite3_vfs_register(apVfs[i], 1); |
| 6923 | } |
| 6924 | return TCL_OK; |
| 6925 | } |
| 6926 | |
| 6927 | |
| 6928 | /* |
| 6929 | ** tclcmd: file_control_test DB |
| 6930 | ** |
| 6931 | ** This TCL command runs the sqlite3_file_control interface and |
| 6932 | ** verifies correct operation of the same. |
| 6933 | */ |
| 6934 | static int SQLITE_TCLAPI file_control_test( |
| 6935 | ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ |
| 6936 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 6937 | int objc, /* Number of arguments */ |
| 6938 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 6939 | ){ |
| 6940 | int iArg = 0; |
| 6941 | sqlite3 *db; |
| 6942 | int rc; |
| 6943 | |
| 6944 | if( objc!=2 ){ |
| 6945 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 6946 | Tcl_GetStringFromObj(objv[0], 0), " DB", NULL); |
| 6947 | return TCL_ERROR; |
| 6948 | } |
| 6949 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 6950 | rc = sqlite3_file_control(db, 0, 0, &iArg); |
| 6951 | assert( rc==SQLITE_NOTFOUND ); |
| 6952 | rc = sqlite3_file_control(db, "notadatabase", SQLITE_FCNTL_LOCKSTATE, &iArg); |
| 6953 | assert( rc==SQLITE_ERROR ); |
| 6954 | rc = sqlite3_file_control(db, "main", -1, &iArg); |
| 6955 | assert( rc==SQLITE_NOTFOUND ); |
| 6956 | rc = sqlite3_file_control(db, "temp", -1, &iArg); |
| 6957 | assert( rc==SQLITE_NOTFOUND || rc==SQLITE_ERROR ); |
| 6958 | |
| 6959 | return TCL_OK; |
| 6960 | } |
| 6961 | |
| 6962 | |
| 6963 | /* |
| 6964 | ** tclcmd: file_control_lasterrno_test DB |
| 6965 | ** |
| 6966 | ** This TCL command runs the sqlite3_file_control interface and |
| 6967 | ** verifies correct operation of the SQLITE_LAST_ERRNO verb. |
| 6968 | */ |
| 6969 | static int SQLITE_TCLAPI file_control_lasterrno_test( |
| 6970 | ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ |
| 6971 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 6972 | int objc, /* Number of arguments */ |
| 6973 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 6974 | ){ |
| 6975 | int iArg = 0; |
| 6976 | sqlite3 *db; |
| 6977 | int rc; |
| 6978 | |
| 6979 | if( objc!=2 ){ |
| 6980 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 6981 | Tcl_GetStringFromObj(objv[0], 0), " DB", NULL); |
| 6982 | return TCL_ERROR; |
| 6983 | } |
| 6984 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){ |
| 6985 | return TCL_ERROR; |
| 6986 | } |
| 6987 | rc = sqlite3_file_control(db, NULL, SQLITE_LAST_ERRNO, &iArg); |
| 6988 | if( rc ){ |
| 6989 | Tcl_SetObjResult(interp, Tcl_NewIntObj(rc)); |
| 6990 | return TCL_ERROR; |
| 6991 | } |
| 6992 | if( iArg!=0 ) { |
| 6993 | Tcl_AppendResult(interp, "Unexpected non-zero errno: ", |
| 6994 | Tcl_GetStringFromObj(Tcl_NewIntObj(iArg), 0), " ", NULL); |
| 6995 | return TCL_ERROR; |
| 6996 | } |
| 6997 | return TCL_OK; |
| 6998 | } |
| 6999 | |
| 7000 | /* |
| 7001 | ** tclcmd: file_control_data_version DB DBNAME |
| 7002 | ** |
| 7003 | ** This TCL command runs the sqlite3_file_control with the |
| 7004 | ** SQLITE_FCNTL_DATA_VERSION opcode, returning the result. |
| 7005 | */ |
| 7006 | static int SQLITE_TCLAPI file_control_data_version( |
| 7007 | ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ |
| 7008 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 7009 | int objc, /* Number of arguments */ |
| 7010 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 7011 | ){ |
| 7012 | unsigned int iVers; /* data version */ |
| 7013 | char *zDb; /* Db name ("main", "temp" etc.) */ |
| 7014 | sqlite3 *db; /* Database handle */ |
| 7015 | int rc; /* file_control() return code */ |
| 7016 | char zBuf[100]; |
| 7017 | |
| 7018 | if( objc!=3 && objc!=2 ){ |
| 7019 | Tcl_WrongNumArgs(interp, 1, objv, "DB [DBNAME]"); |
| 7020 | return TCL_ERROR; |
| 7021 | } |
| 7022 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){ |
| 7023 | return TCL_ERROR; |
| 7024 | } |
| 7025 | zDb = objc==3 ? Tcl_GetString(objv[2]) : NULL; |
| 7026 | |
| 7027 | rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_DATA_VERSION, (void *)&iVers); |
| 7028 | if( rc ){ |
| 7029 | Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_STATIC); |
| 7030 | return TCL_ERROR; |
| 7031 | }else{ |
| 7032 | sqlite3_snprintf(sizeof(zBuf),zBuf,"%u",iVers); |
| 7033 | Tcl_SetResult(interp, (char *)zBuf, TCL_VOLATILE); |
| 7034 | return TCL_OK; |
| 7035 | } |
| 7036 | } |
| 7037 | |
| 7038 | /* |
| 7039 | ** tclcmd: file_control_chunksize_test DB DBNAME SIZE |
| 7040 | ** |
| 7041 | ** This TCL command runs the sqlite3_file_control interface and |
| 7042 | ** verifies correct operation of the SQLITE_GET_LOCKPROXYFILE and |
| 7043 | ** SQLITE_SET_LOCKPROXYFILE verbs. |
| 7044 | */ |
| 7045 | static int SQLITE_TCLAPI file_control_chunksize_test( |
| 7046 | ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ |
| 7047 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 7048 | int objc, /* Number of arguments */ |
| 7049 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 7050 | ){ |
| 7051 | int nSize; /* New chunk size */ |
| 7052 | char *zDb; /* Db name ("main", "temp" etc.) */ |
| 7053 | sqlite3 *db; /* Database handle */ |
| 7054 | int rc; /* file_control() return code */ |
| 7055 | |
| 7056 | if( objc!=4 ){ |
| 7057 | Tcl_WrongNumArgs(interp, 1, objv, "DB DBNAME SIZE"); |
| 7058 | return TCL_ERROR; |
| 7059 | } |
| 7060 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) |
| 7061 | || Tcl_GetIntFromObj(interp, objv[3], &nSize) |
| 7062 | ){ |
| 7063 | return TCL_ERROR; |
| 7064 | } |
| 7065 | zDb = Tcl_GetString(objv[2]); |
| 7066 | if( zDb[0]=='\0' ) zDb = NULL; |
| 7067 | |
| 7068 | rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_CHUNK_SIZE, (void *)&nSize); |
| 7069 | if( rc ){ |
| 7070 | Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_STATIC); |
| 7071 | return TCL_ERROR; |
| 7072 | } |
| 7073 | return TCL_OK; |
| 7074 | } |
| 7075 | |
| 7076 | /* |
| 7077 | ** tclcmd: file_control_sizehint_test DB DBNAME SIZE |
| 7078 | ** |
| 7079 | ** This TCL command runs the sqlite3_file_control interface |
| 7080 | ** with SQLITE_FCNTL_SIZE_HINT |
| 7081 | */ |
| 7082 | static int SQLITE_TCLAPI file_control_sizehint_test( |
| 7083 | ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ |
| 7084 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 7085 | int objc, /* Number of arguments */ |
| 7086 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 7087 | ){ |
| 7088 | Tcl_WideInt nSize; /* Hinted size */ |
| 7089 | char *zDb; /* Db name ("main", "temp" etc.) */ |
| 7090 | sqlite3 *db; /* Database handle */ |
| 7091 | int rc; /* file_control() return code */ |
| 7092 | |
| 7093 | if( objc!=4 ){ |
| 7094 | Tcl_WrongNumArgs(interp, 1, objv, "DB DBNAME SIZE"); |
| 7095 | return TCL_ERROR; |
| 7096 | } |
| 7097 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) |
| 7098 | || Tcl_GetWideIntFromObj(interp, objv[3], &nSize) |
| 7099 | ){ |
| 7100 | return TCL_ERROR; |
| 7101 | } |
| 7102 | zDb = Tcl_GetString(objv[2]); |
| 7103 | if( zDb[0]=='\0' ) zDb = NULL; |
| 7104 | |
| 7105 | rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_SIZE_HINT, (void *)&nSize); |
| 7106 | if( rc ){ |
| 7107 | Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_STATIC); |
| 7108 | return TCL_ERROR; |
| 7109 | } |
| 7110 | return TCL_OK; |
| 7111 | } |
| 7112 | |
| 7113 | /* |
| 7114 | ** tclcmd: file_control_lockproxy_test DB PWD |
| 7115 | ** |
| 7116 | ** This TCL command runs the sqlite3_file_control interface and |
| 7117 | ** verifies correct operation of the SQLITE_GET_LOCKPROXYFILE and |
| 7118 | ** SQLITE_SET_LOCKPROXYFILE verbs. |
| 7119 | */ |
| 7120 | static int SQLITE_TCLAPI file_control_lockproxy_test( |
| 7121 | ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ |
| 7122 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 7123 | int objc, /* Number of arguments */ |
| 7124 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 7125 | ){ |
| 7126 | sqlite3 *db; |
| 7127 | |
| 7128 | if( objc!=3 ){ |
| 7129 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 7130 | Tcl_GetStringFromObj(objv[0], 0), " DB PWD", NULL); |
| 7131 | return TCL_ERROR; |
| 7132 | } |
| 7133 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){ |
| 7134 | return TCL_ERROR; |
| 7135 | } |
| 7136 | |
| 7137 | #if !defined(SQLITE_ENABLE_LOCKING_STYLE) |
| 7138 | # if defined(__APPLE__) |
| 7139 | # define SQLITE_ENABLE_LOCKING_STYLE 1 |
| 7140 | # else |
| 7141 | # define SQLITE_ENABLE_LOCKING_STYLE 0 |
| 7142 | # endif |
| 7143 | #endif |
| 7144 | #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
| 7145 | { |
| 7146 | char *testPath; |
| 7147 | int rc; |
| 7148 | Tcl_Size nPwd; |
| 7149 | const char *zPwd; |
| 7150 | char proxyPath[400]; |
| 7151 | |
| 7152 | zPwd = Tcl_GetStringFromObj(objv[2], &nPwd); |
| 7153 | if( sizeof(proxyPath)<nPwd+20 ){ |
| 7154 | Tcl_AppendResult(interp, "PWD too big", (void*)0); |
| 7155 | return TCL_ERROR; |
| 7156 | } |
| 7157 | sqlite3_snprintf(sizeof(proxyPath), proxyPath, "%s/test.proxy", zPwd); |
| 7158 | rc = sqlite3_file_control(db, NULL, SQLITE_SET_LOCKPROXYFILE, proxyPath); |
| 7159 | if( rc ){ |
| 7160 | Tcl_SetObjResult(interp, Tcl_NewIntObj(rc)); |
| 7161 | return TCL_ERROR; |
| 7162 | } |
| 7163 | rc = sqlite3_file_control(db, NULL, SQLITE_GET_LOCKPROXYFILE, &testPath); |
| 7164 | if( strncmp(proxyPath,testPath,11) ){ |
| 7165 | Tcl_AppendResult(interp, "Lock proxy file did not match the " |
| 7166 | "previously assigned value", NULL); |
| 7167 | return TCL_ERROR; |
| 7168 | } |
| 7169 | if( rc ){ |
| 7170 | Tcl_SetObjResult(interp, Tcl_NewIntObj(rc)); |
| 7171 | return TCL_ERROR; |
| 7172 | } |
| 7173 | rc = sqlite3_file_control(db, NULL, SQLITE_SET_LOCKPROXYFILE, proxyPath); |
| 7174 | if( rc ){ |
| 7175 | Tcl_SetObjResult(interp, Tcl_NewIntObj(rc)); |
| 7176 | return TCL_ERROR; |
| 7177 | } |
| 7178 | } |
| 7179 | #endif |
| 7180 | return TCL_OK; |
| 7181 | } |
| 7182 | |
| 7183 | #if SQLITE_OS_WIN |
| 7184 | /* |
| 7185 | ** tclcmd: file_control_win32_av_retry DB NRETRY DELAY |
| 7186 | ** |
| 7187 | ** This TCL command runs the sqlite3_file_control interface with |
| 7188 | ** the SQLITE_FCNTL_WIN32_AV_RETRY opcode. |
| 7189 | */ |
| 7190 | static int SQLITE_TCLAPI file_control_win32_av_retry( |
| 7191 | ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ |
| 7192 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 7193 | int objc, /* Number of arguments */ |
| 7194 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 7195 | ){ |
| 7196 | sqlite3 *db; |
| 7197 | int rc; |
| 7198 | int a[2]; |
| 7199 | char z[100]; |
| 7200 | |
| 7201 | if( objc!=4 ){ |
| 7202 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 7203 | Tcl_GetStringFromObj(objv[0], 0), " DB NRETRY DELAY", NULL); |
| 7204 | return TCL_ERROR; |
| 7205 | } |
| 7206 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){ |
| 7207 | return TCL_ERROR; |
| 7208 | } |
| 7209 | if( Tcl_GetIntFromObj(interp, objv[2], &a[0]) ) return TCL_ERROR; |
| 7210 | if( Tcl_GetIntFromObj(interp, objv[3], &a[1]) ) return TCL_ERROR; |
| 7211 | rc = sqlite3_file_control(db, NULL, SQLITE_FCNTL_WIN32_AV_RETRY, (void*)a); |
| 7212 | sqlite3_snprintf(sizeof(z), z, "%d %d %d", rc, a[0], a[1]); |
| 7213 | Tcl_AppendResult(interp, z, (char*)0); |
| 7214 | return TCL_OK; |
| 7215 | } |
| 7216 | |
| 7217 | /* |
| 7218 | ** tclcmd: file_control_win32_get_handle DB |
| 7219 | ** |
| 7220 | ** This TCL command runs the sqlite3_file_control interface with |
| 7221 | ** the SQLITE_FCNTL_WIN32_GET_HANDLE opcode. |
| 7222 | */ |
| 7223 | static int file_control_win32_get_handle( |
| 7224 | ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ |
| 7225 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 7226 | int objc, /* Number of arguments */ |
| 7227 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 7228 | ){ |
| 7229 | sqlite3 *db; |
| 7230 | int rc; |
| 7231 | HANDLE hFile = NULL; |
| 7232 | |
| 7233 | if( objc!=2 ){ |
| 7234 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 7235 | Tcl_GetStringFromObj(objv[0], 0), " DB", NULL); |
| 7236 | return TCL_ERROR; |
| 7237 | } |
| 7238 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){ |
| 7239 | return TCL_ERROR; |
| 7240 | } |
| 7241 | rc = sqlite3_file_control(db, NULL, SQLITE_FCNTL_WIN32_GET_HANDLE, |
| 7242 | (void*)&hFile); |
| 7243 | Tcl_AppendResult(interp, sqlite3TestPtrToText((void*)hFile), (char*)0); |
| 7244 | return TCL_OK; |
| 7245 | } |
| 7246 | |
| 7247 | /* |
| 7248 | ** tclcmd: file_control_win32_set_handle DB HANDLE |
| 7249 | ** |
| 7250 | ** This TCL command runs the sqlite3_file_control interface with |
| 7251 | ** the SQLITE_FCNTL_WIN32_SET_HANDLE opcode. |
| 7252 | */ |
| 7253 | static int SQLITE_TCLAPI file_control_win32_set_handle( |
| 7254 | ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ |
| 7255 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 7256 | int objc, /* Number of arguments */ |
| 7257 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 7258 | ){ |
| 7259 | sqlite3 *db; |
| 7260 | int rc; |
| 7261 | HANDLE hFile = NULL; |
| 7262 | char z[100]; |
| 7263 | |
| 7264 | if( objc!=3 ){ |
| 7265 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 7266 | Tcl_GetStringFromObj(objv[0], 0), " DB HANDLE", NULL); |
| 7267 | return TCL_ERROR; |
| 7268 | } |
| 7269 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){ |
| 7270 | return TCL_ERROR; |
| 7271 | } |
| 7272 | if( getWin32Handle(interp, Tcl_GetString(objv[2]), &hFile) ){ |
| 7273 | return TCL_ERROR; |
| 7274 | } |
| 7275 | rc = sqlite3_file_control(db, NULL, SQLITE_FCNTL_WIN32_SET_HANDLE, |
| 7276 | (void*)&hFile); |
| 7277 | sqlite3_snprintf(sizeof(z), z, "%d %s", rc,testPointerToString((void*)hFile)); |
| 7278 | Tcl_AppendResult(interp, z, (char*)0); |
| 7279 | return TCL_OK; |
| 7280 | } |
| 7281 | #endif |
| 7282 | |
| 7283 | /* |
| 7284 | ** tclcmd: file_control_persist_wal DB PERSIST-FLAG |
| 7285 | ** |
| 7286 | ** This TCL command runs the sqlite3_file_control interface with |
| 7287 | ** the SQLITE_FCNTL_PERSIST_WAL opcode. |
| 7288 | */ |
| 7289 | static int SQLITE_TCLAPI file_control_persist_wal( |
| 7290 | ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ |
| 7291 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 7292 | int objc, /* Number of arguments */ |
| 7293 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 7294 | ){ |
| 7295 | sqlite3 *db; |
| 7296 | int rc; |
| 7297 | int bPersist; |
| 7298 | char z[100]; |
| 7299 | |
| 7300 | if( objc!=3 ){ |
| 7301 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 7302 | Tcl_GetStringFromObj(objv[0], 0), " DB FLAG", NULL); |
| 7303 | return TCL_ERROR; |
| 7304 | } |
| 7305 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){ |
| 7306 | return TCL_ERROR; |
| 7307 | } |
| 7308 | if( Tcl_GetIntFromObj(interp, objv[2], &bPersist) ) return TCL_ERROR; |
| 7309 | rc = sqlite3_file_control(db, NULL, SQLITE_FCNTL_PERSIST_WAL, (void*)&bPersist); |
| 7310 | sqlite3_snprintf(sizeof(z), z, "%d %d", rc, bPersist); |
| 7311 | Tcl_AppendResult(interp, z, (char*)0); |
| 7312 | return TCL_OK; |
| 7313 | } |
| 7314 | |
| 7315 | /* |
| 7316 | ** tclcmd: file_control_powersafe_overwrite DB PSOW-FLAG |
| 7317 | ** |
| 7318 | ** This TCL command runs the sqlite3_file_control interface with |
| 7319 | ** the SQLITE_FCNTL_POWERSAFE_OVERWRITE opcode. |
| 7320 | */ |
| 7321 | static int SQLITE_TCLAPI file_control_powersafe_overwrite( |
| 7322 | ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ |
| 7323 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 7324 | int objc, /* Number of arguments */ |
| 7325 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 7326 | ){ |
| 7327 | sqlite3 *db; |
| 7328 | int rc; |
| 7329 | int b; |
| 7330 | char z[100]; |
| 7331 | |
| 7332 | if( objc!=3 ){ |
| 7333 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 7334 | Tcl_GetStringFromObj(objv[0], 0), " DB FLAG", NULL); |
| 7335 | return TCL_ERROR; |
| 7336 | } |
| 7337 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){ |
| 7338 | return TCL_ERROR; |
| 7339 | } |
| 7340 | if( Tcl_GetIntFromObj(interp, objv[2], &b) ) return TCL_ERROR; |
| 7341 | rc = sqlite3_file_control(db,NULL,SQLITE_FCNTL_POWERSAFE_OVERWRITE,(void*)&b); |
| 7342 | sqlite3_snprintf(sizeof(z), z, "%d %d", rc, b); |
| 7343 | Tcl_AppendResult(interp, z, (char*)0); |
| 7344 | return TCL_OK; |
| 7345 | } |
| 7346 | |
| 7347 | |
| 7348 | /* |
| 7349 | ** tclcmd: file_control_vfsname DB ?AUXDB? |
| 7350 | ** |
| 7351 | ** Return a string that describes the stack of VFSes. |
| 7352 | */ |
| 7353 | static int SQLITE_TCLAPI file_control_vfsname( |
| 7354 | ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ |
| 7355 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 7356 | int objc, /* Number of arguments */ |
| 7357 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 7358 | ){ |
| 7359 | sqlite3 *db; |
| 7360 | const char *zDbName = "main"; |
| 7361 | char *zVfsName = 0; |
| 7362 | |
| 7363 | if( objc!=2 && objc!=3 ){ |
| 7364 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 7365 | Tcl_GetStringFromObj(objv[0], 0), " DB ?AUXDB?", NULL); |
| 7366 | return TCL_ERROR; |
| 7367 | } |
| 7368 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){ |
| 7369 | return TCL_ERROR; |
| 7370 | } |
| 7371 | if( objc==3 ){ |
| 7372 | zDbName = Tcl_GetString(objv[2]); |
| 7373 | } |
| 7374 | sqlite3_file_control(db, zDbName, SQLITE_FCNTL_VFSNAME,(void*)&zVfsName); |
| 7375 | Tcl_AppendResult(interp, zVfsName, (char*)0); |
| 7376 | sqlite3_free(zVfsName); |
| 7377 | return TCL_OK; |
| 7378 | } |
| 7379 | |
| 7380 | /* |
| 7381 | ** tclcmd: file_control_reservebytes DB N |
| 7382 | */ |
| 7383 | static int SQLITE_TCLAPI file_control_reservebytes( |
| 7384 | ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ |
| 7385 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 7386 | int objc, /* Number of arguments */ |
| 7387 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 7388 | ){ |
| 7389 | sqlite3 *db; |
| 7390 | const char *zDbName = "main"; |
| 7391 | int n = 0; |
| 7392 | int rc; |
| 7393 | |
| 7394 | if( objc!=3 ){ |
| 7395 | Tcl_WrongNumArgs(interp, 1, objv, "DB N"); |
| 7396 | return TCL_ERROR; |
| 7397 | } |
| 7398 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) |
| 7399 | || Tcl_GetIntFromObj(interp, objv[2], &n) |
| 7400 | ){ |
| 7401 | return TCL_ERROR; |
| 7402 | } |
| 7403 | |
| 7404 | rc = sqlite3_file_control(db, zDbName, SQLITE_FCNTL_RESERVE_BYTES, (void*)&n); |
| 7405 | Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3ErrName(rc), -1)); |
| 7406 | return TCL_OK; |
| 7407 | } |
| 7408 | |
| 7409 | |
| 7410 | /* |
| 7411 | ** tclcmd: file_control_tempfilename DB ?AUXDB? |
| 7412 | ** |
| 7413 | ** Return a string that is a temporary filename |
| 7414 | */ |
| 7415 | static int SQLITE_TCLAPI file_control_tempfilename( |
| 7416 | ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ |
| 7417 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 7418 | int objc, /* Number of arguments */ |
| 7419 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 7420 | ){ |
| 7421 | sqlite3 *db; |
| 7422 | const char *zDbName = "main"; |
| 7423 | char *zTName = 0; |
| 7424 | |
| 7425 | if( objc!=2 && objc!=3 ){ |
| 7426 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 7427 | Tcl_GetStringFromObj(objv[0], 0), " DB ?AUXDB?", NULL); |
| 7428 | return TCL_ERROR; |
| 7429 | } |
| 7430 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){ |
| 7431 | return TCL_ERROR; |
| 7432 | } |
| 7433 | if( objc==3 ){ |
| 7434 | zDbName = Tcl_GetString(objv[2]); |
| 7435 | } |
| 7436 | sqlite3_file_control(db, zDbName, SQLITE_FCNTL_TEMPFILENAME, (void*)&zTName); |
| 7437 | Tcl_AppendResult(interp, zTName, (char*)0); |
| 7438 | sqlite3_free(zTName); |
| 7439 | return TCL_OK; |
| 7440 | } |
| 7441 | |
| 7442 | /* |
| 7443 | ** tclcmd: file_control_external_reader DB ?AUXDB? |
| 7444 | ** |
| 7445 | ** Return a string that is a temporary filename |
| 7446 | */ |
| 7447 | static int SQLITE_TCLAPI file_control_external_reader( |
| 7448 | ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ |
| 7449 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 7450 | int objc, /* Number of arguments */ |
| 7451 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 7452 | ){ |
| 7453 | sqlite3 *db; |
| 7454 | const char *zName = "main"; |
| 7455 | int iRes = 0; |
| 7456 | int rc = SQLITE_OK; |
| 7457 | |
| 7458 | if( objc!=2 && objc!=3 ){ |
| 7459 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 7460 | Tcl_GetStringFromObj(objv[0], 0), " DB ?AUXDB?", NULL); |
| 7461 | return TCL_ERROR; |
| 7462 | } |
| 7463 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){ |
| 7464 | return TCL_ERROR; |
| 7465 | } |
| 7466 | if( objc==3 ){ |
| 7467 | zName = Tcl_GetString(objv[2]); |
| 7468 | } |
| 7469 | rc = sqlite3_file_control(db, zName, SQLITE_FCNTL_EXTERNAL_READER, &iRes); |
| 7470 | if( rc!=SQLITE_OK ){ |
| 7471 | Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC); |
| 7472 | return TCL_ERROR; |
| 7473 | } |
| 7474 | Tcl_SetObjResult(interp, Tcl_NewIntObj(iRes)); |
| 7475 | return TCL_OK; |
| 7476 | } |
| 7477 | |
| 7478 | |
| 7479 | /* |
| 7480 | ** tclcmd: sqlite3_vfs_list |
| 7481 | ** |
| 7482 | ** Return a tcl list containing the names of all registered vfs's. |
| 7483 | */ |
| 7484 | static int SQLITE_TCLAPI vfs_list( |
| 7485 | ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ |
| 7486 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 7487 | int objc, /* Number of arguments */ |
| 7488 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 7489 | ){ |
| 7490 | sqlite3_vfs *pVfs; |
| 7491 | Tcl_Obj *pRet = Tcl_NewObj(); |
| 7492 | if( objc!=1 ){ |
| 7493 | Tcl_WrongNumArgs(interp, 1, objv, ""); |
| 7494 | return TCL_ERROR; |
| 7495 | } |
| 7496 | for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){ |
| 7497 | Tcl_ListObjAppendElement(interp, pRet, Tcl_NewStringObj(pVfs->zName, -1)); |
| 7498 | } |
| 7499 | Tcl_SetObjResult(interp, pRet); |
| 7500 | return TCL_OK; |
| 7501 | } |
| 7502 | |
| 7503 | /* |
| 7504 | ** tclcmd: sqlite3_limit DB ID VALUE |
| 7505 | ** |
| 7506 | ** This TCL command runs the sqlite3_limit interface and |
| 7507 | ** verifies correct operation of the same. |
| 7508 | */ |
| 7509 | static int SQLITE_TCLAPI test_limit( |
| 7510 | ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ |
| 7511 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 7512 | int objc, /* Number of arguments */ |
| 7513 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 7514 | ){ |
| 7515 | sqlite3 *db; |
| 7516 | int rc; |
| 7517 | static const struct { |
| 7518 | char *zName; |
| 7519 | int id; |
| 7520 | } aId[] = { |
| 7521 | { "SQLITE_LIMIT_LENGTH", SQLITE_LIMIT_LENGTH }, |
| 7522 | { "SQLITE_LIMIT_SQL_LENGTH", SQLITE_LIMIT_SQL_LENGTH }, |
| 7523 | { "SQLITE_LIMIT_COLUMN", SQLITE_LIMIT_COLUMN }, |
| 7524 | { "SQLITE_LIMIT_EXPR_DEPTH", SQLITE_LIMIT_EXPR_DEPTH }, |
| 7525 | { "SQLITE_LIMIT_PARSER_DEPTH", SQLITE_LIMIT_PARSER_DEPTH }, |
| 7526 | { "SQLITE_LIMIT_COMPOUND_SELECT", SQLITE_LIMIT_COMPOUND_SELECT }, |
| 7527 | { "SQLITE_LIMIT_VDBE_OP", SQLITE_LIMIT_VDBE_OP }, |
| 7528 | { "SQLITE_LIMIT_FUNCTION_ARG", SQLITE_LIMIT_FUNCTION_ARG }, |
| 7529 | { "SQLITE_LIMIT_ATTACHED", SQLITE_LIMIT_ATTACHED }, |
| 7530 | { "SQLITE_LIMIT_LIKE_PATTERN_LENGTH", SQLITE_LIMIT_LIKE_PATTERN_LENGTH }, |
| 7531 | { "SQLITE_LIMIT_VARIABLE_NUMBER", SQLITE_LIMIT_VARIABLE_NUMBER }, |
| 7532 | { "SQLITE_LIMIT_TRIGGER_DEPTH", SQLITE_LIMIT_TRIGGER_DEPTH }, |
| 7533 | { "SQLITE_LIMIT_WORKER_THREADS", SQLITE_LIMIT_WORKER_THREADS }, |
| 7534 | { "SQLITE_LIMIT_SCHEMA", SQLITE_LIMIT_SCHEMA }, |
| 7535 | { "SQLITE_LIMIT_TRIGGER_STEPS", SQLITE_LIMIT_TRIGGER_STEPS }, |
| 7536 | { "SQLITE_LIMIT_CTES", SQLITE_LIMIT_CTES }, |
| 7537 | |
| 7538 | /* Out of range test cases */ |
| 7539 | { "SQLITE_LIMIT_TOOSMALL", -1, }, |
| 7540 | { "SQLITE_LIMIT_TOOBIG", SQLITE_LIMIT_CTES+1 }, |
| 7541 | }; |
| 7542 | int i, id = 0; |
| 7543 | int val; |
| 7544 | const char *zId; |
| 7545 | |
| 7546 | if( objc!=4 ){ |
| 7547 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 7548 | Tcl_GetStringFromObj(objv[0], 0), " DB ID VALUE", NULL); |
| 7549 | return TCL_ERROR; |
| 7550 | } |
| 7551 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 7552 | zId = Tcl_GetString(objv[2]); |
| 7553 | for(i=0; i<sizeof(aId)/sizeof(aId[0]); i++){ |
| 7554 | if( strcmp(zId, aId[i].zName)==0 ){ |
| 7555 | id = aId[i].id; |
| 7556 | break; |
| 7557 | } |
| 7558 | } |
| 7559 | if( i>=sizeof(aId)/sizeof(aId[0]) ){ |
| 7560 | Tcl_AppendResult(interp, "unknown limit type: ", zId, (char*)0); |
| 7561 | return TCL_ERROR; |
| 7562 | } |
| 7563 | if( Tcl_GetIntFromObj(interp, objv[3], &val) ) return TCL_ERROR; |
| 7564 | rc = sqlite3_limit(db, id, val); |
| 7565 | Tcl_SetObjResult(interp, Tcl_NewIntObj(rc)); |
| 7566 | return TCL_OK; |
| 7567 | } |
| 7568 | |
| 7569 | /* |
| 7570 | ** tclcmd: save_prng_state |
| 7571 | ** |
| 7572 | ** Save the state of the pseudo-random number generator. |
| 7573 | ** At the same time, verify that sqlite3_test_control works even when |
| 7574 | ** called with an out-of-range opcode. |
| 7575 | */ |
| 7576 | static int SQLITE_TCLAPI save_prng_state( |
| 7577 | ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ |
| 7578 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 7579 | int objc, /* Number of arguments */ |
| 7580 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 7581 | ){ |
| 7582 | int rc = sqlite3_test_control(9999); |
| 7583 | assert( rc==0 ); |
| 7584 | rc = sqlite3_test_control(-1); |
| 7585 | assert( rc==0 ); |
| 7586 | sqlite3_test_control(SQLITE_TESTCTRL_PRNG_SAVE); |
| 7587 | return TCL_OK; |
| 7588 | } |
| 7589 | /* |
| 7590 | ** tclcmd: restore_prng_state |
| 7591 | */ |
| 7592 | static int SQLITE_TCLAPI restore_prng_state( |
| 7593 | ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ |
| 7594 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 7595 | int objc, /* Number of arguments */ |
| 7596 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 7597 | ){ |
| 7598 | sqlite3_test_control(SQLITE_TESTCTRL_PRNG_RESTORE); |
| 7599 | return TCL_OK; |
| 7600 | } |
| 7601 | /* |
| 7602 | ** tclcmd: reset_prng_state |
| 7603 | */ |
| 7604 | static int SQLITE_TCLAPI reset_prng_state( |
| 7605 | ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ |
| 7606 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 7607 | int objc, /* Number of arguments */ |
| 7608 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 7609 | ){ |
| 7610 | sqlite3_randomness(0,0); |
| 7611 | return TCL_OK; |
| 7612 | } |
| 7613 | /* |
| 7614 | ** tclcmd: prng_seed INT ?DB? |
| 7615 | ** |
| 7616 | ** Set up the SQLITE_TESTCTRL_PRNG_SEED pragma with parameter INT and DB. |
| 7617 | ** INT is an integer. DB is a database connection, or a NULL pointer if |
| 7618 | ** omitted. |
| 7619 | ** |
| 7620 | ** When INT!=0 and DB!=0, set the PRNG seed to the value of the schema |
| 7621 | ** cookie for DB, or to INT if the schema cookie happens to be zero. |
| 7622 | ** |
| 7623 | ** When INT!=0 and DB==0, set the PRNG seed to just INT. |
| 7624 | ** |
| 7625 | ** If INT==0 and DB==0 then use the default procedure of calling the |
| 7626 | ** xRandomness method on the default VFS to get the PRNG seed. |
| 7627 | */ |
| 7628 | static int SQLITE_TCLAPI prng_seed( |
| 7629 | ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ |
| 7630 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 7631 | int objc, /* Number of arguments */ |
| 7632 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 7633 | ){ |
| 7634 | int i = 0; |
| 7635 | sqlite3 *db = 0; |
| 7636 | if( objc!=2 && objc!=3 ){ |
| 7637 | Tcl_WrongNumArgs(interp, 1, objv, "SEED ?DB?"); |
| 7638 | return TCL_ERROR; |
| 7639 | } |
| 7640 | if( Tcl_GetIntFromObj(interp,objv[1],&i) ) return TCL_ERROR; |
| 7641 | if( objc==3 && getDbPointer(interp, Tcl_GetString(objv[2]), &db) ){ |
| 7642 | return TCL_ERROR; |
| 7643 | } |
| 7644 | sqlite3_test_control(SQLITE_TESTCTRL_PRNG_SEED, i, db); |
| 7645 | return TCL_OK; |
| 7646 | } |
| 7647 | |
| 7648 | /* |
| 7649 | ** tclcmd: extra_schema_checks BOOLEAN |
| 7650 | ** |
| 7651 | ** Enable or disable schema checks when parsing the sqlite_schema file. |
| 7652 | ** This is always enabled in production, but it is sometimes useful to |
| 7653 | ** disable the checks in order to make some internal error states reachable |
| 7654 | ** for testing. |
| 7655 | */ |
| 7656 | static int SQLITE_TCLAPI extra_schema_checks( |
| 7657 | ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ |
| 7658 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 7659 | int objc, /* Number of arguments */ |
| 7660 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 7661 | ){ |
| 7662 | int i = 0; |
| 7663 | if( objc!=2 ){ |
| 7664 | Tcl_WrongNumArgs(interp, 1, objv, "BOOLEAN"); |
| 7665 | return TCL_ERROR; |
| 7666 | } |
| 7667 | if( Tcl_GetBooleanFromObj(interp,objv[1],&i) ) return TCL_ERROR; |
| 7668 | sqlite3_test_control(SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS, i); |
| 7669 | return TCL_OK; |
| 7670 | } |
| 7671 | |
| 7672 | /* |
| 7673 | ** tclcmd: database_may_be_corrupt |
| 7674 | ** |
| 7675 | ** Indicate that database files might be corrupt. In other words, set the normal |
| 7676 | ** state of operation. |
| 7677 | */ |
| 7678 | static int SQLITE_TCLAPI database_may_be_corrupt( |
| 7679 | ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ |
| 7680 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 7681 | int objc, /* Number of arguments */ |
| 7682 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 7683 | ){ |
| 7684 | sqlite3_test_control(SQLITE_TESTCTRL_NEVER_CORRUPT, 0); |
| 7685 | return TCL_OK; |
| 7686 | } |
| 7687 | /* |
| 7688 | ** tclcmd: database_never_corrupt |
| 7689 | ** |
| 7690 | ** Indicate that database files are always well-formed. This enables |
| 7691 | ** extra assert() statements that test conditions that are always true |
| 7692 | ** for well-formed databases. |
| 7693 | */ |
| 7694 | static int SQLITE_TCLAPI database_never_corrupt( |
| 7695 | ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ |
| 7696 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 7697 | int objc, /* Number of arguments */ |
| 7698 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 7699 | ){ |
| 7700 | sqlite3_test_control(SQLITE_TESTCTRL_NEVER_CORRUPT, 1); |
| 7701 | return TCL_OK; |
| 7702 | } |
| 7703 | |
| 7704 | /* |
| 7705 | ** tclcmd: pcache_stats |
| 7706 | */ |
| 7707 | static int SQLITE_TCLAPI test_pcache_stats( |
| 7708 | ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ |
| 7709 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 7710 | int objc, /* Number of arguments */ |
| 7711 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 7712 | ){ |
| 7713 | int nMin; |
| 7714 | int nMax; |
| 7715 | int nCurrent; |
| 7716 | int nRecyclable; |
| 7717 | Tcl_Obj *pRet; |
| 7718 | |
| 7719 | sqlite3PcacheStats(&nCurrent, &nMax, &nMin, &nRecyclable); |
| 7720 | |
| 7721 | pRet = Tcl_NewObj(); |
| 7722 | Tcl_ListObjAppendElement(interp, pRet, Tcl_NewStringObj("current", -1)); |
| 7723 | Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(nCurrent)); |
| 7724 | Tcl_ListObjAppendElement(interp, pRet, Tcl_NewStringObj("max", -1)); |
| 7725 | Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(nMax)); |
| 7726 | Tcl_ListObjAppendElement(interp, pRet, Tcl_NewStringObj("min", -1)); |
| 7727 | Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(nMin)); |
| 7728 | Tcl_ListObjAppendElement(interp, pRet, Tcl_NewStringObj("recyclable", -1)); |
| 7729 | Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(nRecyclable)); |
| 7730 | |
| 7731 | Tcl_SetObjResult(interp, pRet); |
| 7732 | |
| 7733 | return TCL_OK; |
| 7734 | } |
| 7735 | |
| 7736 | #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY |
| 7737 | static void test_unlock_notify_cb(void **aArg, int nArg){ |
| 7738 | int ii; |
| 7739 | for(ii=0; ii<nArg; ii++){ |
| 7740 | Tcl_EvalEx((Tcl_Interp *)aArg[ii], "unlock_notify", -1, TCL_EVAL_GLOBAL); |
| 7741 | } |
| 7742 | } |
| 7743 | #endif /* SQLITE_ENABLE_UNLOCK_NOTIFY */ |
| 7744 | |
| 7745 | /* |
| 7746 | ** tclcmd: sqlite3_unlock_notify db |
| 7747 | */ |
| 7748 | #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY |
| 7749 | static int SQLITE_TCLAPI test_unlock_notify( |
| 7750 | ClientData clientData, /* Unused */ |
| 7751 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 7752 | int objc, /* Number of arguments */ |
| 7753 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 7754 | ){ |
| 7755 | sqlite3 *db; |
| 7756 | int rc; |
| 7757 | |
| 7758 | if( objc!=2 ){ |
| 7759 | Tcl_WrongNumArgs(interp, 1, objv, "DB"); |
| 7760 | return TCL_ERROR; |
| 7761 | } |
| 7762 | |
| 7763 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){ |
| 7764 | return TCL_ERROR; |
| 7765 | } |
| 7766 | rc = sqlite3_unlock_notify(db, test_unlock_notify_cb, (void *)interp); |
| 7767 | Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC); |
| 7768 | return TCL_OK; |
| 7769 | } |
| 7770 | #endif |
| 7771 | |
| 7772 | /* |
| 7773 | ** tclcmd: sqlite3_wal_checkpoint db ?NAME? |
| 7774 | */ |
| 7775 | static int SQLITE_TCLAPI test_wal_checkpoint( |
| 7776 | ClientData clientData, /* Unused */ |
| 7777 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 7778 | int objc, /* Number of arguments */ |
| 7779 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 7780 | ){ |
| 7781 | char *zDb = 0; |
| 7782 | sqlite3 *db; |
| 7783 | int rc; |
| 7784 | |
| 7785 | if( objc!=3 && objc!=2 ){ |
| 7786 | Tcl_WrongNumArgs(interp, 1, objv, "DB ?NAME?"); |
| 7787 | return TCL_ERROR; |
| 7788 | } |
| 7789 | |
| 7790 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){ |
| 7791 | return TCL_ERROR; |
| 7792 | } |
| 7793 | if( objc==3 ){ |
| 7794 | zDb = Tcl_GetString(objv[2]); |
| 7795 | } |
| 7796 | rc = sqlite3_wal_checkpoint(db, zDb); |
| 7797 | Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC); |
| 7798 | return TCL_OK; |
| 7799 | } |
| 7800 | |
| 7801 | /* |
| 7802 | ** tclcmd: sqlite3_wal_checkpoint_v2 db MODE ?NAME? |
| 7803 | ** |
| 7804 | ** This command calls the wal_checkpoint_v2() function with the specified |
| 7805 | ** mode argument (passive, full or restart). If present, the database name |
| 7806 | ** NAME is passed as the second argument to wal_checkpoint_v2(). If it the |
| 7807 | ** NAME argument is not present, a NULL pointer is passed instead. |
| 7808 | ** |
| 7809 | ** If wal_checkpoint_v2() returns any value other than SQLITE_BUSY or |
| 7810 | ** SQLITE_OK, then this command returns TCL_ERROR. The Tcl result is set |
| 7811 | ** to the error message obtained from sqlite3_errmsg(). |
| 7812 | ** |
| 7813 | ** Otherwise, this command returns a list of three integers. The first integer |
| 7814 | ** is 1 if SQLITE_BUSY was returned, or 0 otherwise. The following two integers |
| 7815 | ** are the values returned via the output parameters by wal_checkpoint_v2() - |
| 7816 | ** the number of frames in the log and the number of frames in the log |
| 7817 | ** that have been checkpointed. |
| 7818 | */ |
| 7819 | static int SQLITE_TCLAPI test_wal_checkpoint_v2( |
| 7820 | ClientData clientData, /* Unused */ |
| 7821 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 7822 | int objc, /* Number of arguments */ |
| 7823 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 7824 | ){ |
| 7825 | char *zDb = 0; |
| 7826 | sqlite3 *db; |
| 7827 | int rc; |
| 7828 | |
| 7829 | int eMode; |
| 7830 | int nLog = -555; |
| 7831 | int nCkpt = -555; |
| 7832 | Tcl_Obj *pRet; |
| 7833 | |
| 7834 | const char * aMode[] = {"noop", "passive", "full", "restart", "truncate", 0}; |
| 7835 | assert( SQLITE_CHECKPOINT_NOOP==-1 ); |
| 7836 | assert( SQLITE_CHECKPOINT_PASSIVE==0 ); |
| 7837 | assert( SQLITE_CHECKPOINT_FULL==1 ); |
| 7838 | assert( SQLITE_CHECKPOINT_RESTART==2 ); |
| 7839 | assert( SQLITE_CHECKPOINT_TRUNCATE==3 ); |
| 7840 | |
| 7841 | if( objc!=3 && objc!=4 ){ |
| 7842 | Tcl_WrongNumArgs(interp, 1, objv, "DB MODE ?NAME?"); |
| 7843 | return TCL_ERROR; |
| 7844 | } |
| 7845 | |
| 7846 | if( objc==4 ){ |
| 7847 | zDb = Tcl_GetString(objv[3]); |
| 7848 | } |
| 7849 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){ |
| 7850 | return TCL_ERROR; |
| 7851 | } |
| 7852 | if( TCL_OK!=Tcl_GetIntFromObj(0, objv[2], &eMode) ){ |
| 7853 | if( TCL_OK!=Tcl_GetIndexFromObj(interp, objv[2], aMode, "mode", 0,&eMode) ){ |
| 7854 | return TCL_ERROR; |
| 7855 | } |
| 7856 | eMode = eMode - 1; |
| 7857 | } |
| 7858 | |
| 7859 | rc = sqlite3_wal_checkpoint_v2(db, zDb, eMode, &nLog, &nCkpt); |
| 7860 | if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){ |
| 7861 | const char *zErrCode = sqlite3ErrName(rc); |
| 7862 | Tcl_ResetResult(interp); |
| 7863 | Tcl_AppendResult(interp, zErrCode, " - ", (char *)sqlite3_errmsg(db), NULL); |
| 7864 | return TCL_ERROR; |
| 7865 | } |
| 7866 | |
| 7867 | pRet = Tcl_NewObj(); |
| 7868 | Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(rc==SQLITE_BUSY?1:0)); |
| 7869 | Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(nLog)); |
| 7870 | Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(nCkpt)); |
| 7871 | Tcl_SetObjResult(interp, pRet); |
| 7872 | |
| 7873 | return TCL_OK; |
| 7874 | } |
| 7875 | |
| 7876 | /* |
| 7877 | ** tclcmd: sqlite3_wal_autocheckpoint db VALUE |
| 7878 | */ |
| 7879 | static int SQLITE_TCLAPI test_wal_autocheckpoint( |
| 7880 | ClientData clientData, /* Unused */ |
| 7881 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 7882 | int objc, /* Number of arguments */ |
| 7883 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 7884 | ){ |
| 7885 | sqlite3 *db; |
| 7886 | int rc; |
| 7887 | int iVal; |
| 7888 | |
| 7889 | |
| 7890 | if( objc!=3 ){ |
| 7891 | Tcl_WrongNumArgs(interp, 1, objv, "DB VALUE"); |
| 7892 | return TCL_ERROR; |
| 7893 | } |
| 7894 | |
| 7895 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) |
| 7896 | || Tcl_GetIntFromObj(0, objv[2], &iVal) |
| 7897 | ){ |
| 7898 | return TCL_ERROR; |
| 7899 | } |
| 7900 | |
| 7901 | rc = sqlite3_wal_autocheckpoint(db, iVal); |
| 7902 | Tcl_ResetResult(interp); |
| 7903 | if( rc!=SQLITE_OK ){ |
| 7904 | const char *zErrCode = sqlite3ErrName(rc); |
| 7905 | Tcl_SetObjResult(interp, Tcl_NewStringObj(zErrCode, -1)); |
| 7906 | return TCL_ERROR; |
| 7907 | } |
| 7908 | |
| 7909 | return TCL_OK; |
| 7910 | } |
| 7911 | |
| 7912 | |
| 7913 | /* |
| 7914 | ** tclcmd: test_sqlite3_log ?SCRIPT? |
| 7915 | ** |
| 7916 | ** Caution: If you register a log callback, you must deregister it (by |
| 7917 | ** invoking test_sqlite3_log with no arguments) prior to closing the |
| 7918 | ** Tcl interpreter or else a memory error will occur. |
| 7919 | */ |
| 7920 | static struct LogCallback { |
| 7921 | Tcl_Interp *pInterp; |
| 7922 | Tcl_Obj *pObj; |
| 7923 | } logcallback = {0, 0}; |
| 7924 | static void xLogcallback(void *unused, int err, const char *zMsg){ |
| 7925 | Tcl_Obj *pNew = Tcl_DuplicateObj(logcallback.pObj); |
| 7926 | Tcl_IncrRefCount(pNew); |
| 7927 | Tcl_ListObjAppendElement( |
| 7928 | 0, pNew, Tcl_NewStringObj(sqlite3ErrName(err), -1) |
| 7929 | ); |
| 7930 | Tcl_ListObjAppendElement(0, pNew, Tcl_NewStringObj(zMsg, -1)); |
| 7931 | Tcl_EvalObjEx(logcallback.pInterp, pNew, TCL_EVAL_GLOBAL|TCL_EVAL_DIRECT); |
| 7932 | Tcl_DecrRefCount(pNew); |
| 7933 | } |
| 7934 | static int SQLITE_TCLAPI test_sqlite3_log( |
| 7935 | ClientData clientData, |
| 7936 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 7937 | int objc, /* Number of arguments */ |
| 7938 | Tcl_Obj *CONST objv[] /* Command arguments */ |
| 7939 | ){ |
| 7940 | if( objc>2 ){ |
| 7941 | Tcl_WrongNumArgs(interp, 1, objv, "SCRIPT"); |
| 7942 | return TCL_ERROR; |
| 7943 | } |
| 7944 | if( logcallback.pObj ){ |
| 7945 | Tcl_DecrRefCount(logcallback.pObj); |
| 7946 | logcallback.pObj = 0; |
| 7947 | logcallback.pInterp = 0; |
| 7948 | sqlite3_config(SQLITE_CONFIG_LOG, (void*)0, (void*)0); |
| 7949 | } |
| 7950 | if( objc>1 && Tcl_GetString(objv[1])[0]!=0 ){ |
| 7951 | logcallback.pObj = objv[1]; |
| 7952 | Tcl_IncrRefCount(logcallback.pObj); |
| 7953 | logcallback.pInterp = interp; |
| 7954 | sqlite3_config(SQLITE_CONFIG_LOG, xLogcallback, (void*)0); |
| 7955 | } |
| 7956 | return TCL_OK; |
| 7957 | } |
| 7958 | |
| 7959 | /* |
| 7960 | ** tcl_objproc COMMANDNAME ARGS... |
| 7961 | ** |
| 7962 | ** Run a TCL command using its objProc interface. Throw an error if |
| 7963 | ** the command has no objProc interface. |
| 7964 | */ |
| 7965 | static int SQLITE_TCLAPI runAsObjProc( |
| 7966 | void * clientData, |
| 7967 | Tcl_Interp *interp, |
| 7968 | int objc, |
| 7969 | Tcl_Obj *CONST objv[] |
| 7970 | ){ |
| 7971 | Tcl_CmdInfo cmdInfo; |
| 7972 | if( objc<2 ){ |
| 7973 | Tcl_WrongNumArgs(interp, 1, objv, "COMMAND ..."); |
| 7974 | return TCL_ERROR; |
| 7975 | } |
| 7976 | if( !Tcl_GetCommandInfo(interp, Tcl_GetString(objv[1]), &cmdInfo) ){ |
| 7977 | Tcl_AppendResult(interp, "command not found: ", |
| 7978 | Tcl_GetString(objv[1]), (char*)0); |
| 7979 | return TCL_ERROR; |
| 7980 | } |
| 7981 | if( cmdInfo.objProc==0 ){ |
| 7982 | Tcl_AppendResult(interp, "command has no objProc: ", |
| 7983 | Tcl_GetString(objv[1]), (char*)0); |
| 7984 | return TCL_ERROR; |
| 7985 | } |
| 7986 | return cmdInfo.objProc(cmdInfo.objClientData, interp, objc-1, objv+1); |
| 7987 | } |
| 7988 | |
| 7989 | #ifndef SQLITE_OMIT_EXPLAIN |
| 7990 | /* |
| 7991 | ** WARNING: The following function, printExplainQueryPlan() is an exact |
| 7992 | ** copy of example code from eqp.in (eqp.html). If this code is modified, |
| 7993 | ** then the documentation copy needs to be modified as well. |
| 7994 | */ |
| 7995 | /* |
| 7996 | ** Argument pStmt is a prepared SQL statement. This function compiles |
| 7997 | ** an EXPLAIN QUERY PLAN command to report on the prepared statement, |
| 7998 | ** and prints the report to stdout using printf(). |
| 7999 | */ |
| 8000 | int printExplainQueryPlan(sqlite3_stmt *pStmt){ |
| 8001 | const char *zSql; /* Input SQL */ |
| 8002 | char *zExplain; /* SQL with EXPLAIN QUERY PLAN prepended */ |
| 8003 | sqlite3_stmt *pExplain; /* Compiled EXPLAIN QUERY PLAN command */ |
| 8004 | int rc; /* Return code from sqlite3_prepare_v2() */ |
| 8005 | |
| 8006 | zSql = sqlite3_sql(pStmt); |
| 8007 | if( zSql==0 ) return SQLITE_ERROR; |
| 8008 | |
| 8009 | zExplain = sqlite3_mprintf("EXPLAIN QUERY PLAN %s", zSql); |
| 8010 | if( zExplain==0 ) return SQLITE_NOMEM; |
| 8011 | |
| 8012 | rc = sqlite3_prepare_v2(sqlite3_db_handle(pStmt), zExplain, -1, &pExplain, 0); |
| 8013 | sqlite3_free(zExplain); |
| 8014 | if( rc!=SQLITE_OK ) return rc; |
| 8015 | |
| 8016 | while( SQLITE_ROW==sqlite3_step(pExplain) ){ |
| 8017 | int iSelectid = sqlite3_column_int(pExplain, 0); |
| 8018 | int iOrder = sqlite3_column_int(pExplain, 1); |
| 8019 | int iFrom = sqlite3_column_int(pExplain, 2); |
| 8020 | const char *zDetail = (const char *)sqlite3_column_text(pExplain, 3); |
| 8021 | |
| 8022 | printf("%d %d %d %s\n", iSelectid, iOrder, iFrom, zDetail); |
| 8023 | } |
| 8024 | |
| 8025 | return sqlite3_finalize(pExplain); |
| 8026 | } |
| 8027 | |
| 8028 | static int SQLITE_TCLAPI test_print_eqp( |
| 8029 | void * clientData, |
| 8030 | Tcl_Interp *interp, |
| 8031 | int objc, |
| 8032 | Tcl_Obj *CONST objv[] |
| 8033 | ){ |
| 8034 | int rc; |
| 8035 | sqlite3_stmt *pStmt; |
| 8036 | |
| 8037 | if( objc!=2 ){ |
| 8038 | Tcl_WrongNumArgs(interp, 1, objv, "STMT"); |
| 8039 | return TCL_ERROR; |
| 8040 | } |
| 8041 | if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; |
| 8042 | rc = printExplainQueryPlan(pStmt); |
| 8043 | /* This is needed on Windows so that a test case using this |
| 8044 | ** function can open a read pipe and get the output of |
| 8045 | ** printExplainQueryPlan() immediately. |
| 8046 | */ |
| 8047 | fflush(stdout); |
| 8048 | Tcl_SetResult(interp, (char *)t1ErrorName(rc), 0); |
| 8049 | return TCL_OK; |
| 8050 | } |
| 8051 | #endif /* SQLITE_OMIT_EXPLAIN */ |
| 8052 | |
| 8053 | #include <time.h> |
| 8054 | /* |
| 8055 | ** This is an alternative localtime_r() implementation used for testing |
| 8056 | ** the 'localtime' and 'utc' modifiers of date-time functions. Because |
| 8057 | ** the OS-supplied localtime_r() is locale-dependent, this alternative is |
| 8058 | ** provided as a stable test platform. |
| 8059 | ** |
| 8060 | ** Operation: |
| 8061 | ** |
| 8062 | ** (1) Localtime is 30 minutes earlier than (west of) UTC on |
| 8063 | ** even days (counting from 1970-01-01) |
| 8064 | ** |
| 8065 | ** (2) Localtime is 30 minutes later than (east of) UTC on odd days. |
| 8066 | ** |
| 8067 | ** (3) The function fails for the specific date/time value |
| 8068 | ** of 2000-05-29 14:16:00 in order to test the ability of |
| 8069 | ** SQLite to deal with localtime_r() failures. |
| 8070 | */ |
| 8071 | static int testLocaltime(const void *aliasT, void *aliasTM){ |
| 8072 | const time_t t = *(const time_t*)aliasT; |
| 8073 | struct tm *pTm = (struct tm *)aliasTM; |
| 8074 | time_t altT; |
| 8075 | sqlite3_int64 iJD; |
| 8076 | int Z, A, B, C, D, E, X1, S; |
| 8077 | |
| 8078 | if( (t/86400) & 1 ){ |
| 8079 | altT = t + 1800; /* 30 minutes later on odd days */ |
| 8080 | }else{ |
| 8081 | altT = t - 1800; /* 30 minutes earlier on even days */ |
| 8082 | } |
| 8083 | iJD = (sqlite3_int64)(altT + 210866760000); |
| 8084 | Z = (int)((iJD + 43200)/86400); |
| 8085 | A = (int)((Z - 1867216.25)/36524.25); |
| 8086 | A = Z + 1 + A - (A/4); |
| 8087 | B = A + 1524; |
| 8088 | C = (int)((B - 122.1)/365.25); |
| 8089 | D = (36525*(C&32767))/100; |
| 8090 | E = (int)((B-D)/30.6001); |
| 8091 | X1 = (int)(30.6001*E); |
| 8092 | pTm->tm_mday = B - D - X1; |
| 8093 | pTm->tm_mon = E<14 ? E-2 : E-14; |
| 8094 | pTm->tm_year = (pTm->tm_mon>1 ? C - 4716 : C - 4715) - 1900; |
| 8095 | S = (int)((iJD + 43200)%86400); |
| 8096 | pTm->tm_hour = S/3600; |
| 8097 | pTm->tm_min = (S/60)%60; |
| 8098 | pTm->tm_sec = S % 60; |
| 8099 | return t==959609760; /* Special case: 2000-05-29 14:16:00 fails */ |
| 8100 | } |
| 8101 | |
| 8102 | /* |
| 8103 | ** TCLCMD: strftime FORMAT UNIXTIMESTAMP |
| 8104 | ** |
| 8105 | ** Access to the C-library strftime() routine, so that its results |
| 8106 | ** can be compared against SQLite's internal strftime() SQL function |
| 8107 | ** implementation. |
| 8108 | */ |
| 8109 | static int SQLITE_TCLAPI strftime_cmd( |
| 8110 | void * clientData, |
| 8111 | Tcl_Interp *interp, |
| 8112 | int objc, |
| 8113 | Tcl_Obj *CONST objv[] |
| 8114 | ){ |
| 8115 | Tcl_WideInt ts; |
| 8116 | time_t t; |
| 8117 | struct tm *pTm; |
| 8118 | const char *zFmt; |
| 8119 | size_t n; |
| 8120 | char zBuf[1000]; |
| 8121 | if( objc!=3 ){ |
| 8122 | Tcl_WrongNumArgs(interp, 1, objv, "FORMAT UNIXTIMESTAMP"); |
| 8123 | return TCL_ERROR; |
| 8124 | } |
| 8125 | if( Tcl_GetWideIntFromObj(interp, objv[2], &ts) ) return TCL_ERROR; |
| 8126 | zFmt = Tcl_GetString(objv[1]); |
| 8127 | t = (time_t)ts; |
| 8128 | pTm = gmtime(&t); |
| 8129 | n = strftime(zBuf, sizeof(zBuf)-1, zFmt, pTm); |
| 8130 | if( n>=0 && n<sizeof(zBuf) ){ |
| 8131 | zBuf[n] = 0; |
| 8132 | Tcl_SetResult(interp, zBuf, TCL_VOLATILE); |
| 8133 | } |
| 8134 | return TCL_OK; |
| 8135 | } |
| 8136 | |
| 8137 | /* |
| 8138 | ** .treetrace N |
| 8139 | */ |
| 8140 | static int SQLITE_TCLAPI test_treetrace( |
| 8141 | void * clientData, |
| 8142 | Tcl_Interp *interp, |
| 8143 | int objc, |
| 8144 | Tcl_Obj *CONST objv[] |
| 8145 | ){ |
| 8146 | unsigned int v = 0; |
| 8147 | if( objc>=2 ){ |
| 8148 | if( Tcl_GetIntFromObj(interp, objv[1], (int*)&v)==TCL_OK ){ |
| 8149 | sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &v); |
| 8150 | } |
| 8151 | } |
| 8152 | sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 0, &v); |
| 8153 | Tcl_SetObjResult(interp, Tcl_NewIntObj((int)v)); |
| 8154 | return TCL_OK; |
| 8155 | } |
| 8156 | |
| 8157 | /* |
| 8158 | ** sqlite3_test_control VERB ARGS... |
| 8159 | */ |
| 8160 | static int SQLITE_TCLAPI test_test_control( |
| 8161 | void * clientData, |
| 8162 | Tcl_Interp *interp, |
| 8163 | int objc, |
| 8164 | Tcl_Obj *CONST objv[] |
| 8165 | ){ |
| 8166 | struct Verb { |
| 8167 | const char *zName; |
| 8168 | int i; |
| 8169 | } aVerb[] = { |
| 8170 | { "SQLITE_TESTCTRL_LOCALTIME_FAULT", SQLITE_TESTCTRL_LOCALTIME_FAULT }, |
| 8171 | { "SQLITE_TESTCTRL_SORTER_MMAP", SQLITE_TESTCTRL_SORTER_MMAP }, |
| 8172 | { "SQLITE_TESTCTRL_IMPOSTER", SQLITE_TESTCTRL_IMPOSTER }, |
| 8173 | { "SQLITE_TESTCTRL_INTERNAL_FUNCTIONS", SQLITE_TESTCTRL_INTERNAL_FUNCTIONS}, |
| 8174 | { "SQLITE_TESTCTRL_FK_NO_ACTION", SQLITE_TESTCTRL_FK_NO_ACTION}, |
| 8175 | { 0, 0 } |
| 8176 | }; |
| 8177 | int iVerb; |
| 8178 | int iFlag; |
| 8179 | int rc; |
| 8180 | |
| 8181 | if( objc<2 ){ |
| 8182 | Tcl_WrongNumArgs(interp, 1, objv, "VERB ARGS..."); |
| 8183 | return TCL_ERROR; |
| 8184 | } |
| 8185 | |
| 8186 | rc = Tcl_GetIndexFromObjStruct( |
| 8187 | interp, objv[1], aVerb, sizeof(aVerb[0]), "VERB", 0, &iVerb |
| 8188 | ); |
| 8189 | if( rc!=TCL_OK ) return rc; |
| 8190 | |
| 8191 | iFlag = aVerb[iVerb].i; |
| 8192 | switch( iFlag ){ |
| 8193 | case SQLITE_TESTCTRL_INTERNAL_FUNCTIONS: { |
| 8194 | sqlite3 *db = 0; |
| 8195 | if( objc!=3 ){ |
| 8196 | Tcl_WrongNumArgs(interp, 2, objv, "DB"); |
| 8197 | return TCL_ERROR; |
| 8198 | } |
| 8199 | if( getDbPointer(interp, Tcl_GetString(objv[2]), &db) ) return TCL_ERROR; |
| 8200 | sqlite3_test_control(SQLITE_TESTCTRL_INTERNAL_FUNCTIONS, db); |
| 8201 | break; |
| 8202 | } |
| 8203 | case SQLITE_TESTCTRL_LOCALTIME_FAULT: { |
| 8204 | int val; |
| 8205 | if( objc!=3 ){ |
| 8206 | Tcl_WrongNumArgs(interp, 2, objv, "0|1|2"); |
| 8207 | return TCL_ERROR; |
| 8208 | } |
| 8209 | if( Tcl_GetIntFromObj(interp, objv[2], &val) ) return TCL_ERROR; |
| 8210 | sqlite3_test_control(iFlag, val, testLocaltime); |
| 8211 | break; |
| 8212 | } |
| 8213 | |
| 8214 | case SQLITE_TESTCTRL_FK_NO_ACTION: { |
| 8215 | int val = 0; |
| 8216 | sqlite3 *db = 0; |
| 8217 | if( objc!=4 ){ |
| 8218 | Tcl_WrongNumArgs(interp, 2, objv, "DB BOOLEAN"); |
| 8219 | return TCL_ERROR; |
| 8220 | } |
| 8221 | if( getDbPointer(interp, Tcl_GetString(objv[2]), &db) ) return TCL_ERROR; |
| 8222 | if( Tcl_GetBooleanFromObj(interp, objv[3], &val) ) return TCL_ERROR; |
| 8223 | |
| 8224 | sqlite3_test_control(SQLITE_TESTCTRL_FK_NO_ACTION, db, val); |
| 8225 | break; |
| 8226 | } |
| 8227 | |
| 8228 | case SQLITE_TESTCTRL_SORTER_MMAP: { |
| 8229 | int val; |
| 8230 | sqlite3 *db; |
| 8231 | if( objc!=4 ){ |
| 8232 | Tcl_WrongNumArgs(interp, 2, objv, "DB LIMIT"); |
| 8233 | return TCL_ERROR; |
| 8234 | } |
| 8235 | if( getDbPointer(interp, Tcl_GetString(objv[2]), &db) ) return TCL_ERROR; |
| 8236 | if( Tcl_GetIntFromObj(interp, objv[3], &val) ) return TCL_ERROR; |
| 8237 | sqlite3_test_control(SQLITE_TESTCTRL_SORTER_MMAP, db, val); |
| 8238 | break; |
| 8239 | } |
| 8240 | |
| 8241 | case SQLITE_TESTCTRL_IMPOSTER: { |
| 8242 | int onOff, tnum; |
| 8243 | const char *zDbName; |
| 8244 | sqlite3 *db; |
| 8245 | if( objc!=6 ){ |
| 8246 | Tcl_WrongNumArgs(interp, 2, objv, "DB dbName onOff tnum"); |
| 8247 | return TCL_ERROR; |
| 8248 | } |
| 8249 | if( getDbPointer(interp, Tcl_GetString(objv[2]), &db) ) return TCL_ERROR; |
| 8250 | zDbName = Tcl_GetString(objv[3]); |
| 8251 | if( Tcl_GetIntFromObj(interp, objv[4], &onOff) ) return TCL_ERROR; |
| 8252 | if( Tcl_GetIntFromObj(interp, objv[5], &tnum) ) return TCL_ERROR; |
| 8253 | sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, db, zDbName, onOff, tnum); |
| 8254 | break; |
| 8255 | } |
| 8256 | } |
| 8257 | |
| 8258 | Tcl_ResetResult(interp); |
| 8259 | return TCL_OK; |
| 8260 | } |
| 8261 | |
| 8262 | #if SQLITE_OS_UNIX |
| 8263 | #include <sys/time.h> |
| 8264 | #include <sys/resource.h> |
| 8265 | |
| 8266 | static int SQLITE_TCLAPI test_getrusage( |
| 8267 | void * clientData, |
| 8268 | Tcl_Interp *interp, |
| 8269 | int objc, |
| 8270 | Tcl_Obj *CONST objv[] |
| 8271 | ){ |
| 8272 | char buf[1024]; |
| 8273 | struct rusage r; |
| 8274 | memset(&r, 0, sizeof(r)); |
| 8275 | getrusage(RUSAGE_SELF, &r); |
| 8276 | |
| 8277 | sqlite3_snprintf(sizeof(buf), buf, |
| 8278 | "ru_utime=%d.%06d ru_stime=%d.%06d ru_minflt=%d ru_majflt=%d", |
| 8279 | (int)r.ru_utime.tv_sec, (int)r.ru_utime.tv_usec, |
| 8280 | (int)r.ru_stime.tv_sec, (int)r.ru_stime.tv_usec, |
| 8281 | (int)r.ru_minflt, (int)r.ru_majflt |
| 8282 | ); |
| 8283 | Tcl_SetObjResult(interp, Tcl_NewStringObj(buf, -1)); |
| 8284 | return TCL_OK; |
| 8285 | } |
| 8286 | #endif |
| 8287 | |
| 8288 | #if SQLITE_OS_WIN |
| 8289 | /* |
| 8290 | ** Information passed from the main thread into the windows file locker |
| 8291 | ** background thread. |
| 8292 | */ |
| 8293 | struct win32FileLocker { |
| 8294 | char *evName; /* Name of event to signal thread startup */ |
| 8295 | sqlite3_file *pFd; /* Handle of the file to be locked */ |
| 8296 | int delay1; /* Delay before locking */ |
| 8297 | int delay2; /* Delay before unlocking */ |
| 8298 | int ok; /* Finished ok */ |
| 8299 | int err; /* True if an error occurs */ |
| 8300 | }; |
| 8301 | #endif |
| 8302 | |
| 8303 | |
| 8304 | #ifdef _WIN32 |
| 8305 | #include <process.h> |
| 8306 | /* |
| 8307 | ** The background thread that does file locking. |
| 8308 | */ |
| 8309 | static void SQLITE_CDECL win32_file_locker(void *pAppData){ |
| 8310 | struct win32FileLocker *p = (struct win32FileLocker*)pAppData; |
| 8311 | sqlite3_file *pFd = p->pFd; |
| 8312 | HANDLE h = INVALID_HANDLE_VALUE; |
| 8313 | if( p->evName ){ |
| 8314 | HANDLE ev = OpenEvent(EVENT_MODIFY_STATE, FALSE, p->evName); |
| 8315 | if ( ev ){ |
| 8316 | SetEvent(ev); |
| 8317 | CloseHandle(ev); |
| 8318 | } |
| 8319 | } |
| 8320 | if( p->delay1 ) Sleep(p->delay1); |
| 8321 | pFd->pMethods->xFileControl(pFd, SQLITE_FCNTL_WIN32_GET_HANDLE, (void*)&h); |
| 8322 | if( LockFile(h, 0, 0, 100000000, 0) ){ |
| 8323 | Sleep(p->delay2); |
| 8324 | UnlockFile(h, 0, 0, 100000000, 0); |
| 8325 | p->ok = 1; |
| 8326 | }else{ |
| 8327 | p->err = 1; |
| 8328 | } |
| 8329 | pFd->pMethods->xClose(pFd); |
| 8330 | sqlite3_free(pFd); |
| 8331 | p->pFd = 0; |
| 8332 | p->delay1 = 0; |
| 8333 | p->delay2 = 0; |
| 8334 | } |
| 8335 | #endif |
| 8336 | |
| 8337 | #ifdef _WIN32 |
| 8338 | /* |
| 8339 | ** lock_win32_file FILENAME DELAY1 DELAY2 |
| 8340 | ** |
| 8341 | ** Get an exclusive manditory lock on file for DELAY2 milliseconds. |
| 8342 | ** Wait DELAY1 milliseconds before acquiring the lock. |
| 8343 | */ |
| 8344 | static int SQLITE_TCLAPI win32_file_lock( |
| 8345 | void * clientData, |
| 8346 | Tcl_Interp *interp, |
| 8347 | int objc, |
| 8348 | Tcl_Obj *CONST objv[] |
| 8349 | ){ |
| 8350 | static struct win32FileLocker x = { "win32_file_lock", 0, 0, 0, 0, 0 }; |
| 8351 | const char *zFilename = 0; |
| 8352 | Tcl_Size nFilename = 0; |
| 8353 | char *zTerm = 0; |
| 8354 | char zBuf[200]; |
| 8355 | int retry = 0; |
| 8356 | HANDLE ev; |
| 8357 | DWORD wResult; |
| 8358 | sqlite3_vfs *pVfs = 0; |
| 8359 | int flags = SQLITE_OPEN_MAIN_DB | SQLITE_OPEN_READWRITE; |
| 8360 | int rc = SQLITE_OK; |
| 8361 | |
| 8362 | if( objc!=4 && objc!=1 ){ |
| 8363 | Tcl_WrongNumArgs(interp, 1, objv, "FILENAME DELAY1 DELAY2"); |
| 8364 | return TCL_ERROR; |
| 8365 | } |
| 8366 | if( objc==1 ){ |
| 8367 | HANDLE h = INVALID_HANDLE_VALUE; |
| 8368 | if( x.pFd ){ |
| 8369 | x.pFd->pMethods->xFileControl( |
| 8370 | x.pFd, SQLITE_FCNTL_WIN32_GET_HANDLE, (void*)&h |
| 8371 | ); |
| 8372 | } |
| 8373 | sqlite3_snprintf(sizeof(zBuf), zBuf, "%d %d %d %d %d", |
| 8374 | x.ok, x.err, x.delay1, x.delay2, h); |
| 8375 | Tcl_AppendResult(interp, zBuf, (char*)0); |
| 8376 | return TCL_OK; |
| 8377 | } |
| 8378 | while( x.pFd && retry<30 ){ |
| 8379 | retry++; |
| 8380 | Sleep(100); |
| 8381 | } |
| 8382 | if( x.pFd ){ |
| 8383 | Tcl_AppendResult(interp, "busy", (char*)0); |
| 8384 | return TCL_ERROR; |
| 8385 | } |
| 8386 | if( Tcl_GetIntFromObj(interp, objv[2], &x.delay1) ) return TCL_ERROR; |
| 8387 | if( Tcl_GetIntFromObj(interp, objv[3], &x.delay2) ) return TCL_ERROR; |
| 8388 | pVfs = sqlite3_vfs_find(0); |
| 8389 | x.pFd = (sqlite3_file*)sqlite3_malloc(pVfs->szOsFile); |
| 8390 | |
| 8391 | /* xOpen() must be passed a dual-nul-terminated string preceded in memory |
| 8392 | ** by 4 0x00 bytes. */ |
| 8393 | zFilename = Tcl_GetStringFromObj(objv[1], &nFilename); |
| 8394 | zTerm = (char*)sqlite3_malloc(nFilename+6); |
| 8395 | memset(zTerm, 0, nFilename+6); |
| 8396 | memcpy(&zTerm[4], zFilename, nFilename); |
| 8397 | rc = pVfs->xOpen(pVfs, &zTerm[4], x.pFd, flags, &flags); |
| 8398 | sqlite3_free(zTerm); |
| 8399 | |
| 8400 | if( rc!=SQLITE_OK ){ |
| 8401 | Tcl_AppendResult(interp, "cannot open file: ", zFilename, (char*)0); |
| 8402 | return TCL_ERROR; |
| 8403 | } |
| 8404 | ev = CreateEvent(NULL, TRUE, FALSE, x.evName); |
| 8405 | if ( !ev ){ |
| 8406 | Tcl_AppendResult(interp, "cannot create event: ", x.evName, (char*)0); |
| 8407 | return TCL_ERROR; |
| 8408 | } |
| 8409 | _beginthread(win32_file_locker, 0, (void*)&x); |
| 8410 | Sleep(0); |
| 8411 | if ( (wResult = WaitForSingleObject(ev, 10000))!=WAIT_OBJECT_0 ){ |
| 8412 | sqlite3_snprintf(sizeof(zBuf), zBuf, "0x%x", wResult); |
| 8413 | Tcl_AppendResult(interp, "wait failed: ", zBuf, (char*)0); |
| 8414 | CloseHandle(ev); |
| 8415 | return TCL_ERROR; |
| 8416 | } |
| 8417 | CloseHandle(ev); |
| 8418 | return TCL_OK; |
| 8419 | } |
| 8420 | #endif |
| 8421 | |
| 8422 | #ifdef _WIN32 |
| 8423 | /* |
| 8424 | ** An instance of this structure is allocated for each command created |
| 8425 | ** by [map_win32_file]. |
| 8426 | */ |
| 8427 | typedef struct TestMapFile TestMapFile; |
| 8428 | struct TestMapFile { |
| 8429 | HANDLE hFile; /* Handle open on mapped file */ |
| 8430 | HANDLE hMap; /* Handle returned by CreateFileMappingW() */ |
| 8431 | void *pMap; /* Pointer returned by MapViewOfFile() */ |
| 8432 | }; |
| 8433 | |
| 8434 | /* |
| 8435 | ** Release all resources held by the TestMapFile object passed as the |
| 8436 | ** only argument. This is used as the delete-proc for the Tcl command. |
| 8437 | */ |
| 8438 | static void SQLITE_TCLAPI testMapFileDel(void *clientData){ |
| 8439 | TestMapFile *p = (TestMapFile*)clientData; |
| 8440 | if( p->pMap ) UnmapViewOfFile(p->pMap); |
| 8441 | if( p->hMap ) CloseHandle(p->hMap); |
| 8442 | if( p->hFile!=INVALID_HANDLE_VALUE ) CloseHandle(p->hFile); |
| 8443 | ckfree((char*)p); |
| 8444 | } |
| 8445 | |
| 8446 | /* |
| 8447 | ** Implementation of the command created by [map_win32_file]: |
| 8448 | ** |
| 8449 | ** CMD destroy |
| 8450 | */ |
| 8451 | static int SQLITE_TCLAPI testMapFileCmd( |
| 8452 | void *clientData, |
| 8453 | Tcl_Interp *interp, |
| 8454 | int objc, |
| 8455 | Tcl_Obj *CONST objv[] |
| 8456 | ){ |
| 8457 | static const char *azMethod[] = { "destroy", 0 }; |
| 8458 | enum { MAPFILE_DESTROY }; |
| 8459 | int iMethod = 0; |
| 8460 | |
| 8461 | if( objc<2 ){ |
| 8462 | Tcl_WrongNumArgs(interp, 1, objv, "SUB-COMMAND ..."); |
| 8463 | return TCL_ERROR; |
| 8464 | } |
| 8465 | if( Tcl_GetIndexFromObj(interp, objv[1], azMethod, "method", 0, &iMethod) ){ |
| 8466 | return TCL_ERROR; |
| 8467 | } |
| 8468 | |
| 8469 | switch( iMethod ){ |
| 8470 | case MAPFILE_DESTROY: { |
| 8471 | if( objc!=2 ){ |
| 8472 | Tcl_WrongNumArgs(interp, 2, objv, ""); |
| 8473 | return TCL_ERROR; |
| 8474 | } |
| 8475 | Tcl_DeleteCommand(interp, Tcl_GetString(objv[0])); |
| 8476 | break; |
| 8477 | } |
| 8478 | } |
| 8479 | |
| 8480 | return TCL_OK; |
| 8481 | } |
| 8482 | |
| 8483 | /* |
| 8484 | ** map_win32_file CMD FILENAME |
| 8485 | ** |
| 8486 | ** Open file FILENAME and memory-map the entire file using the same |
| 8487 | ** CreateFileMappingW() and MapViewOfFile() APIs used by winShmMap() in |
| 8488 | ** os_win.c. Create a new Tcl command named CMD that may be used to |
| 8489 | ** unmap the file ([CMD destroy]). |
| 8490 | */ |
| 8491 | static int SQLITE_TCLAPI map_win32_file( |
| 8492 | void * clientData, |
| 8493 | Tcl_Interp *interp, |
| 8494 | int objc, |
| 8495 | Tcl_Obj *CONST objv[] |
| 8496 | ){ |
| 8497 | const char *zCmd = 0; |
| 8498 | const char *zFile = 0; |
| 8499 | LPWSTR zWide = 0; |
| 8500 | int nWide = 0; |
| 8501 | TestMapFile *p = 0; |
| 8502 | LARGE_INTEGER sz; |
| 8503 | char zErr[100]; |
| 8504 | |
| 8505 | if( objc!=3 ){ |
| 8506 | Tcl_WrongNumArgs(interp, 1, objv, "CMD FILENAME"); |
| 8507 | return TCL_ERROR; |
| 8508 | } |
| 8509 | zCmd = Tcl_GetString(objv[1]); |
| 8510 | zFile = Tcl_GetString(objv[2]); |
| 8511 | |
| 8512 | /* Convert the utf-8 filename to utf-16 */ |
| 8513 | nWide = MultiByteToWideChar(CP_UTF8, 0, zFile, -1, NULL, 0); |
| 8514 | if( nWide<=0 ){ |
| 8515 | Tcl_AppendResult(interp, "cannot convert filename: ", zFile, (char*)0); |
| 8516 | return TCL_ERROR; |
| 8517 | } |
| 8518 | zWide = (LPWSTR)ckalloc(nWide * sizeof(WCHAR)); |
| 8519 | MultiByteToWideChar(CP_UTF8, 0, zFile, -1, zWide, nWide); |
| 8520 | |
| 8521 | p = (TestMapFile*)ckalloc(sizeof(TestMapFile)); |
| 8522 | memset(p, 0, sizeof(TestMapFile)); |
| 8523 | p->hFile = CreateFileW(zWide, |
| 8524 | GENERIC_READ | GENERIC_WRITE, |
| 8525 | FILE_SHARE_READ | FILE_SHARE_WRITE, |
| 8526 | NULL, |
| 8527 | OPEN_EXISTING, |
| 8528 | FILE_ATTRIBUTE_NORMAL, |
| 8529 | NULL |
| 8530 | ); |
| 8531 | if( p->hFile==INVALID_HANDLE_VALUE ){ |
| 8532 | DWORD lastErr = GetLastError(); |
| 8533 | ckfree((char*)zWide); |
| 8534 | sqlite3_snprintf(sizeof(zErr), zErr, " (%lu)", lastErr); |
| 8535 | Tcl_AppendResult(interp, "cannot open file: ", zFile, zErr, (char*)0); |
| 8536 | testMapFileDel((void*)p); |
| 8537 | return TCL_ERROR; |
| 8538 | } |
| 8539 | ckfree((char*)zWide); |
| 8540 | |
| 8541 | if( !GetFileSizeEx(p->hFile, &sz) ){ |
| 8542 | sqlite3_snprintf(sizeof(zErr), zErr, " (%lu)", GetLastError()); |
| 8543 | Tcl_AppendResult(interp, "GetFileSizeEx failed", zErr, (char*)0); |
| 8544 | testMapFileDel((void*)p); |
| 8545 | return TCL_ERROR; |
| 8546 | } |
| 8547 | if( sz.QuadPart==0 ){ |
| 8548 | Tcl_AppendResult(interp, "cannot map zero-length file: ", zFile, (char*)0); |
| 8549 | testMapFileDel((void*)p); |
| 8550 | return TCL_ERROR; |
| 8551 | } |
| 8552 | |
| 8553 | /* Map the entire file. Same protection and flags as winShmMap(). */ |
| 8554 | p->hMap = CreateFileMappingW(p->hFile, NULL, PAGE_READWRITE, |
| 8555 | (DWORD)(sz.QuadPart >> 32), (DWORD)(sz.QuadPart & 0xFFFFFFFF), NULL |
| 8556 | ); |
| 8557 | if( p->hMap==NULL ){ |
| 8558 | sqlite3_snprintf(sizeof(zErr), zErr, " (%lu)", GetLastError()); |
| 8559 | Tcl_AppendResult(interp, "CreateFileMappingW failed", zErr, (char*)0); |
| 8560 | testMapFileDel((void*)p); |
| 8561 | return TCL_ERROR; |
| 8562 | } |
| 8563 | p->pMap = MapViewOfFile(p->hMap, FILE_MAP_WRITE | FILE_MAP_READ, 0, 0, |
| 8564 | (SIZE_T)sz.QuadPart |
| 8565 | ); |
| 8566 | if( p->pMap==NULL ){ |
| 8567 | sqlite3_snprintf(sizeof(zErr), zErr, " (%lu)", GetLastError()); |
| 8568 | Tcl_AppendResult(interp, "MapViewOfFile failed", zErr, (char*)0); |
| 8569 | testMapFileDel((void*)p); |
| 8570 | return TCL_ERROR; |
| 8571 | } |
| 8572 | |
| 8573 | Tcl_CreateObjCommand(interp, zCmd, testMapFileCmd, (void*)p, testMapFileDel); |
| 8574 | Tcl_SetObjResult(interp, objv[1]); |
| 8575 | return TCL_OK; |
| 8576 | } |
| 8577 | #endif |
| 8578 | |
| 8579 | |
| 8580 | /* |
| 8581 | ** optimization_control DB OPT BOOLEAN |
| 8582 | ** |
| 8583 | ** Enable or disable query optimizations using the sqlite3_test_control() |
| 8584 | ** interface. Disable if BOOLEAN is false and enable if BOOLEAN is true. |
| 8585 | ** OPT is the name of the optimization to be disabled. OPT can also be a |
| 8586 | ** list or optimizations names, in which case all optimizations named are |
| 8587 | ** enabled or disabled. |
| 8588 | ** |
| 8589 | ** Each invocation of this control overrides all prior invocations. The |
| 8590 | ** changes are not cumulative. |
| 8591 | */ |
| 8592 | static int SQLITE_TCLAPI optimization_control( |
| 8593 | void * clientData, |
| 8594 | Tcl_Interp *interp, |
| 8595 | int objc, |
| 8596 | Tcl_Obj *CONST objv[] |
| 8597 | ){ |
| 8598 | size_t i, j, k; |
| 8599 | sqlite3 *db; |
| 8600 | const char *zOpt; |
| 8601 | int onoff; |
| 8602 | u64 m = 0, mask = 0; |
| 8603 | char zBuf[50]; |
| 8604 | extern int sqlite3_optimization_mask(const char*,sqlite3_uint64*); |
| 8605 | extern const char *sqlite3_optimization_name(sqlite3_uint64); |
| 8606 | |
| 8607 | if( objc!=4 ){ |
| 8608 | Tcl_WrongNumArgs(interp, 1, objv, "DB OPT BOOLEAN"); |
| 8609 | return TCL_ERROR; |
| 8610 | } |
| 8611 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 8612 | if( Tcl_GetBooleanFromObj(interp, objv[3], &onoff) ) return TCL_ERROR; |
| 8613 | zOpt = Tcl_GetString(objv[2]); |
| 8614 | while( zOpt && zOpt[0] ){ |
| 8615 | while( zOpt[0]==' ' || zOpt[0]==',' ) zOpt++; |
| 8616 | for(i=0; zOpt[i] && zOpt[i]!=',' && zOpt[i]!=' '; i++){} |
| 8617 | if( i==0 ) break; |
| 8618 | if( i<sizeof(zBuf)-1 ){ |
| 8619 | for(j=k=0; j<i; j++){ |
| 8620 | if( zOpt[j]!='-' ) zBuf[k++] = zOpt[j]; |
| 8621 | } |
| 8622 | zBuf[k] = 0; |
| 8623 | if( sqlite3_optimization_mask(zBuf, &m)==SQLITE_NOTFOUND ){ |
| 8624 | const char *zName; |
| 8625 | Tcl_AppendResult(interp, "unknown optimization \"", zBuf, |
| 8626 | "\" - should be one of:", (char*)0); |
| 8627 | for(i=0; i<64; i++){ |
| 8628 | m = 1ull<<i; |
| 8629 | zName = sqlite3_optimization_name(m); |
| 8630 | if( zName ){ |
| 8631 | Tcl_AppendResult(interp, " ", zName, (char*)0); |
| 8632 | } |
| 8633 | } |
| 8634 | return TCL_ERROR; |
| 8635 | } |
| 8636 | mask |= m; |
| 8637 | } |
| 8638 | zOpt += i; |
| 8639 | } |
| 8640 | if( onoff ) mask = ~mask; |
| 8641 | sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS,db, mask); |
| 8642 | Tcl_SetObjResult(interp, Tcl_NewWideIntObj(mask)); |
| 8643 | return TCL_OK; |
| 8644 | } |
| 8645 | |
| 8646 | /* |
| 8647 | ** load_static_extension DB NAME ... |
| 8648 | ** |
| 8649 | ** Load one or more statically linked extensions. |
| 8650 | */ |
| 8651 | static int SQLITE_TCLAPI tclLoadStaticExtensionCmd( |
| 8652 | void * clientData, |
| 8653 | Tcl_Interp *interp, |
| 8654 | int objc, |
| 8655 | Tcl_Obj *CONST objv[] |
| 8656 | ){ |
| 8657 | extern int sqlite3_amatch_init(sqlite3*,char**,const sqlite3_api_routines*); |
| 8658 | extern int sqlite3_appendvfs_init(sqlite3*,char**,const sqlite3_api_routines*); |
| 8659 | extern int sqlite3_basexx_init(sqlite3*,char**,const sqlite3_api_routines*); |
| 8660 | extern int sqlite3_closure_init(sqlite3*,char**,const sqlite3_api_routines*); |
| 8661 | extern int sqlite3_csv_init(sqlite3*,char**,const sqlite3_api_routines*); |
| 8662 | extern int sqlite3_eval_init(sqlite3*,char**,const sqlite3_api_routines*); |
| 8663 | extern int sqlite3_explain_init(sqlite3*,char**,const sqlite3_api_routines*); |
| 8664 | extern int sqlite3_fileio_init(sqlite3*,char**,const sqlite3_api_routines*); |
| 8665 | extern int sqlite3_decimal_init(sqlite3*,char**,const sqlite3_api_routines*); |
| 8666 | extern int sqlite3_diskused_init(sqlite3*,char**,const sqlite3_api_routines*); |
| 8667 | extern int sqlite3_fuzzer_init(sqlite3*,char**,const sqlite3_api_routines*); |
| 8668 | extern int sqlite3_ieee_init(sqlite3*,char**,const sqlite3_api_routines*); |
| 8669 | extern int sqlite3_nextchar_init(sqlite3*,char**,const sqlite3_api_routines*); |
| 8670 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 8671 | extern int sqlite3_prefixes_init(sqlite3*,char**,const sqlite3_api_routines*); |
| 8672 | #endif |
| 8673 | extern int sqlite3_qpvtab_init(sqlite3*,char**,const sqlite3_api_routines*); |
| 8674 | extern int sqlite3_randomjson_init(sqlite3*,char**,const sqlite3_api_routines*); |
| 8675 | extern int sqlite3_regexp_init(sqlite3*,char**,const sqlite3_api_routines*); |
| 8676 | extern int sqlite3_remember_init(sqlite3*,char**,const sqlite3_api_routines*); |
| 8677 | extern int sqlite3_series_init(sqlite3*,char**,const sqlite3_api_routines*); |
| 8678 | extern int sqlite3_spellfix_init(sqlite3*,char**,const sqlite3_api_routines*); |
| 8679 | extern int sqlite3_stmtrand_init(sqlite3*,char**,const sqlite3_api_routines*); |
| 8680 | extern int sqlite3_strdup_init(sqlite3*,char**,const sqlite3_api_routines*); |
| 8681 | extern int sqlite3_totype_init(sqlite3*,char**,const sqlite3_api_routines*); |
| 8682 | extern int sqlite3_tryeval_init(sqlite3*,char**,const sqlite3_api_routines*); |
| 8683 | extern int sqlite3_wholenumber_init(sqlite3*,char**,const sqlite3_api_routines*); |
| 8684 | extern int sqlite3_unionvtab_init(sqlite3*,char**,const sqlite3_api_routines*); |
| 8685 | #ifdef SQLITE_HAVE_ZLIB |
| 8686 | extern int sqlite3_zipfile_init(sqlite3*,char**,const sqlite3_api_routines*); |
| 8687 | #endif |
| 8688 | static const struct { |
| 8689 | const char *zExtName; |
| 8690 | int (*pInit)(sqlite3*,char**,const sqlite3_api_routines*); |
| 8691 | } aExtension[] = { |
| 8692 | { "amatch", sqlite3_amatch_init }, |
| 8693 | { "appendvfs", sqlite3_appendvfs_init }, |
| 8694 | { "basexx", sqlite3_basexx_init }, |
| 8695 | { "closure", sqlite3_closure_init }, |
| 8696 | { "csv", sqlite3_csv_init }, |
| 8697 | { "decimal", sqlite3_decimal_init }, |
| 8698 | { "diskused", sqlite3_diskused_init }, |
| 8699 | { "eval", sqlite3_eval_init }, |
| 8700 | { "explain", sqlite3_explain_init }, |
| 8701 | { "fileio", sqlite3_fileio_init }, |
| 8702 | { "fuzzer", sqlite3_fuzzer_init }, |
| 8703 | { "ieee754", sqlite3_ieee_init }, |
| 8704 | { "nextchar", sqlite3_nextchar_init }, |
| 8705 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 8706 | { "prefixes", sqlite3_prefixes_init }, |
| 8707 | #endif |
| 8708 | { "qpvtab", sqlite3_qpvtab_init }, |
| 8709 | { "randomjson", sqlite3_randomjson_init }, |
| 8710 | { "regexp", sqlite3_regexp_init }, |
| 8711 | { "remember", sqlite3_remember_init }, |
| 8712 | { "series", sqlite3_series_init }, |
| 8713 | { "spellfix", sqlite3_spellfix_init }, |
| 8714 | { "stmtrand", sqlite3_stmtrand_init }, |
| 8715 | { "strdup", sqlite3_strdup_init }, |
| 8716 | { "totype", sqlite3_totype_init }, |
| 8717 | { "tryeval", sqlite3_tryeval_init }, |
| 8718 | { "unionvtab", sqlite3_unionvtab_init }, |
| 8719 | { "wholenumber", sqlite3_wholenumber_init }, |
| 8720 | #ifdef SQLITE_HAVE_ZLIB |
| 8721 | { "zipfile", sqlite3_zipfile_init }, |
| 8722 | #endif |
| 8723 | }; |
| 8724 | sqlite3 *db; |
| 8725 | const char *zName; |
| 8726 | int i, j, rc; |
| 8727 | char *zErrMsg = 0; |
| 8728 | if( objc<3 ){ |
| 8729 | Tcl_WrongNumArgs(interp, 1, objv, "DB NAME ..."); |
| 8730 | return TCL_ERROR; |
| 8731 | } |
| 8732 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 8733 | for(j=2; j<objc; j++){ |
| 8734 | zName = Tcl_GetString(objv[j]); |
| 8735 | for(i=0; i<ArraySize(aExtension); i++){ |
| 8736 | if( strcmp(zName, aExtension[i].zExtName)==0 ) break; |
| 8737 | } |
| 8738 | if( i>=ArraySize(aExtension) ){ |
| 8739 | Tcl_AppendResult(interp, "no such extension: ", zName, (char*)0); |
| 8740 | return TCL_ERROR; |
| 8741 | } |
| 8742 | if( aExtension[i].pInit ){ |
| 8743 | rc = aExtension[i].pInit(db, &zErrMsg, 0); |
| 8744 | }else{ |
| 8745 | rc = SQLITE_OK; |
| 8746 | } |
| 8747 | if( (rc!=SQLITE_OK && rc!=SQLITE_OK_LOAD_PERMANENTLY) || zErrMsg ){ |
| 8748 | Tcl_AppendResult(interp, "initialization of ", zName, " failed: ", zErrMsg, |
| 8749 | (char*)0); |
| 8750 | sqlite3_free(zErrMsg); |
| 8751 | return TCL_ERROR; |
| 8752 | } |
| 8753 | } |
| 8754 | return TCL_OK; |
| 8755 | } |
| 8756 | |
| 8757 | /* |
| 8758 | ** sorter_test_fakeheap BOOL |
| 8759 | ** |
| 8760 | */ |
| 8761 | static int SQLITE_TCLAPI sorter_test_fakeheap( |
| 8762 | void * clientData, |
| 8763 | Tcl_Interp *interp, |
| 8764 | int objc, |
| 8765 | Tcl_Obj *CONST objv[] |
| 8766 | ){ |
| 8767 | int bArg; |
| 8768 | if( objc!=2 ){ |
| 8769 | Tcl_WrongNumArgs(interp, 1, objv, "BOOL"); |
| 8770 | return TCL_ERROR; |
| 8771 | } |
| 8772 | |
| 8773 | if( Tcl_GetBooleanFromObj(interp, objv[1], &bArg) ){ |
| 8774 | return TCL_ERROR; |
| 8775 | } |
| 8776 | |
| 8777 | if( bArg ){ |
| 8778 | if( sqlite3GlobalConfig.pHeap==0 ){ |
| 8779 | sqlite3GlobalConfig.pHeap = SQLITE_INT_TO_PTR(-1); |
| 8780 | } |
| 8781 | }else{ |
| 8782 | if( sqlite3GlobalConfig.pHeap==SQLITE_INT_TO_PTR(-1) ){ |
| 8783 | sqlite3GlobalConfig.pHeap = 0; |
| 8784 | } |
| 8785 | } |
| 8786 | |
| 8787 | Tcl_ResetResult(interp); |
| 8788 | return TCL_OK; |
| 8789 | } |
| 8790 | |
| 8791 | /* |
| 8792 | ** sorter_test_sort4_helper DB SQL1 NSTEP SQL2 |
| 8793 | ** |
| 8794 | ** Compile SQL statement $SQL1 and step it $NSTEP times. For each row, |
| 8795 | ** check that the leftmost and rightmost columns returned are both integers, |
| 8796 | ** and that both contain the same value. |
| 8797 | ** |
| 8798 | ** Then execute statement $SQL2. Check that the statement returns the same |
| 8799 | ** set of integers in the same order as in the previous step (using $SQL1). |
| 8800 | */ |
| 8801 | static int SQLITE_TCLAPI sorter_test_sort4_helper( |
| 8802 | void * clientData, |
| 8803 | Tcl_Interp *interp, |
| 8804 | int objc, |
| 8805 | Tcl_Obj *CONST objv[] |
| 8806 | ){ |
| 8807 | const char *zSql1; |
| 8808 | const char *zSql2; |
| 8809 | int nStep; |
| 8810 | int iStep; |
| 8811 | unsigned int iCksum1 = 0; |
| 8812 | unsigned int iCksum2 = 0; |
| 8813 | int rc; |
| 8814 | int iB; |
| 8815 | sqlite3 *db; |
| 8816 | sqlite3_stmt *pStmt; |
| 8817 | |
| 8818 | if( objc!=5 ){ |
| 8819 | Tcl_WrongNumArgs(interp, 1, objv, "DB SQL1 NSTEP SQL2"); |
| 8820 | return TCL_ERROR; |
| 8821 | } |
| 8822 | |
| 8823 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 8824 | zSql1 = Tcl_GetString(objv[2]); |
| 8825 | if( Tcl_GetIntFromObj(interp, objv[3], &nStep) ) return TCL_ERROR; |
| 8826 | zSql2 = Tcl_GetString(objv[4]); |
| 8827 | |
| 8828 | rc = sqlite3_prepare_v2(db, zSql1, -1, &pStmt, 0); |
| 8829 | if( rc!=SQLITE_OK ) goto sql_error; |
| 8830 | |
| 8831 | iB = sqlite3_column_count(pStmt)-1; |
| 8832 | for(iStep=0; iStep<nStep && SQLITE_ROW==sqlite3_step(pStmt); iStep++){ |
| 8833 | int a = sqlite3_column_int(pStmt, 0); |
| 8834 | if( a!=sqlite3_column_int(pStmt, iB) ){ |
| 8835 | Tcl_AppendResult(interp, "data error: (a!=b)", (void*)0); |
| 8836 | return TCL_ERROR; |
| 8837 | } |
| 8838 | |
| 8839 | iCksum1 += (iCksum1 << 3) + (unsigned int)a; |
| 8840 | } |
| 8841 | rc = sqlite3_finalize(pStmt); |
| 8842 | if( rc!=SQLITE_OK ) goto sql_error; |
| 8843 | |
| 8844 | rc = sqlite3_prepare_v2(db, zSql2, -1, &pStmt, 0); |
| 8845 | if( rc!=SQLITE_OK ) goto sql_error; |
| 8846 | for(iStep=0; SQLITE_ROW==sqlite3_step(pStmt); iStep++){ |
| 8847 | int a = sqlite3_column_int(pStmt, 0); |
| 8848 | iCksum2 += (iCksum2 << 3) + (unsigned int)a; |
| 8849 | } |
| 8850 | rc = sqlite3_finalize(pStmt); |
| 8851 | if( rc!=SQLITE_OK ) goto sql_error; |
| 8852 | |
| 8853 | if( iCksum1!=iCksum2 ){ |
| 8854 | Tcl_AppendResult(interp, "checksum mismatch", (void*)0); |
| 8855 | return TCL_ERROR; |
| 8856 | } |
| 8857 | |
| 8858 | return TCL_OK; |
| 8859 | sql_error: |
| 8860 | Tcl_AppendResult(interp, "sql error: ", sqlite3_errmsg(db), (void*)0); |
| 8861 | return TCL_ERROR; |
| 8862 | } |
| 8863 | |
| 8864 | |
| 8865 | |
| 8866 | /* |
| 8867 | ** tclcmd: register_dbstat_vtab DB |
| 8868 | ** |
| 8869 | ** Cause the dbstat virtual table to be available on the connection DB |
| 8870 | */ |
| 8871 | static int SQLITE_TCLAPI test_register_dbstat_vtab( |
| 8872 | void *clientData, |
| 8873 | Tcl_Interp *interp, |
| 8874 | int objc, |
| 8875 | Tcl_Obj *CONST objv[] |
| 8876 | ){ |
| 8877 | #ifdef SQLITE_OMIT_VIRTUALTABLE |
| 8878 | Tcl_AppendResult(interp, "dbstat not available because of " |
| 8879 | "SQLITE_OMIT_VIRTUALTABLE", (void*)0); |
| 8880 | return TCL_ERROR; |
| 8881 | #else |
| 8882 | struct SqliteDb { sqlite3 *db; }; |
| 8883 | char *zDb; |
| 8884 | Tcl_CmdInfo cmdInfo; |
| 8885 | |
| 8886 | if( objc!=2 ){ |
| 8887 | Tcl_WrongNumArgs(interp, 1, objv, "DB"); |
| 8888 | return TCL_ERROR; |
| 8889 | } |
| 8890 | |
| 8891 | zDb = Tcl_GetString(objv[1]); |
| 8892 | if( Tcl_GetCommandInfo(interp, zDb, &cmdInfo) ){ |
| 8893 | sqlite3* db = ((struct SqliteDb*)cmdInfo.objClientData)->db; |
| 8894 | sqlite3DbstatRegister(db); |
| 8895 | } |
| 8896 | return TCL_OK; |
| 8897 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 8898 | } |
| 8899 | |
| 8900 | /* |
| 8901 | ** tclcmd: sqlite3_db_config DB SETTING VALUE |
| 8902 | ** |
| 8903 | ** Invoke sqlite3_db_config() for one of the setting values. |
| 8904 | */ |
| 8905 | static int SQLITE_TCLAPI test_sqlite3_db_config( |
| 8906 | void *clientData, |
| 8907 | Tcl_Interp *interp, |
| 8908 | int objc, |
| 8909 | Tcl_Obj *CONST objv[] |
| 8910 | ){ |
| 8911 | static const struct { |
| 8912 | const char *zName; |
| 8913 | int eVal; |
| 8914 | } aSetting[] = { |
| 8915 | { "FKEY", SQLITE_DBCONFIG_ENABLE_FKEY }, |
| 8916 | { "TRIGGER", SQLITE_DBCONFIG_ENABLE_TRIGGER }, |
| 8917 | { "FTS3_TOKENIZER", SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER }, |
| 8918 | { "LOAD_EXTENSION", SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION }, |
| 8919 | { "NO_CKPT_ON_CLOSE", SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE }, |
| 8920 | { "QPSG", SQLITE_DBCONFIG_ENABLE_QPSG }, |
| 8921 | { "TRIGGER_EQP", SQLITE_DBCONFIG_TRIGGER_EQP }, |
| 8922 | { "RESET_DB", SQLITE_DBCONFIG_RESET_DATABASE }, |
| 8923 | { "DEFENSIVE", SQLITE_DBCONFIG_DEFENSIVE }, |
| 8924 | { "WRITABLE_SCHEMA", SQLITE_DBCONFIG_WRITABLE_SCHEMA }, |
| 8925 | { "LEGACY_ALTER_TABLE", SQLITE_DBCONFIG_LEGACY_ALTER_TABLE }, |
| 8926 | { "DQS_DML", SQLITE_DBCONFIG_DQS_DML }, |
| 8927 | { "DQS_DDL", SQLITE_DBCONFIG_DQS_DDL }, |
| 8928 | { "LEGACY_FILE_FORMAT", SQLITE_DBCONFIG_LEGACY_FILE_FORMAT }, |
| 8929 | { "TRUSTED_SCHEMA", SQLITE_DBCONFIG_TRUSTED_SCHEMA }, |
| 8930 | { "STMT_SCANSTATUS", SQLITE_DBCONFIG_STMT_SCANSTATUS }, |
| 8931 | { "REVERSE_SCANORDER", SQLITE_DBCONFIG_REVERSE_SCANORDER }, |
| 8932 | { "ATTACH_CREATE", SQLITE_DBCONFIG_ENABLE_ATTACH_CREATE }, |
| 8933 | { "ATTACH_WRITE", SQLITE_DBCONFIG_ENABLE_ATTACH_WRITE }, |
| 8934 | { "COMMENTS", SQLITE_DBCONFIG_ENABLE_COMMENTS }, |
| 8935 | { "FP_DIGITS", SQLITE_DBCONFIG_FP_DIGITS }, |
| 8936 | }; |
| 8937 | int i; |
| 8938 | int v = 0; |
| 8939 | const char *zSetting; |
| 8940 | sqlite3 *db; |
| 8941 | |
| 8942 | if( objc!=4 && objc!=3 ){ |
| 8943 | Tcl_WrongNumArgs(interp, 1, objv, "DB SETTING [VALUE]"); |
| 8944 | return TCL_ERROR; |
| 8945 | } |
| 8946 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 8947 | zSetting = Tcl_GetString(objv[2]); |
| 8948 | if( sqlite3_strglob("SQLITE_*", zSetting)==0 ) zSetting += 7; |
| 8949 | if( sqlite3_strglob("DBCONFIG_*", zSetting)==0 ) zSetting += 9; |
| 8950 | if( sqlite3_strglob("ENABLE_*", zSetting)==0 ) zSetting += 7; |
| 8951 | for(i=0; i<ArraySize(aSetting); i++){ |
| 8952 | if( strcmp(zSetting, aSetting[i].zName)==0 ) break; |
| 8953 | } |
| 8954 | if( i>=ArraySize(aSetting) ){ |
| 8955 | Tcl_SetObjResult(interp, |
| 8956 | Tcl_NewStringObj("unknown sqlite3_db_config setting", -1)); |
| 8957 | return TCL_ERROR; |
| 8958 | } |
| 8959 | if( objc==4 ){ |
| 8960 | if( Tcl_GetIntFromObj(interp, objv[3], &v) ) return TCL_ERROR; |
| 8961 | }else{ |
| 8962 | v = -1; |
| 8963 | } |
| 8964 | sqlite3_db_config(db, aSetting[i].eVal, v, &v); |
| 8965 | Tcl_SetObjResult(interp, Tcl_NewIntObj(v)); |
| 8966 | return TCL_OK; |
| 8967 | } |
| 8968 | /* |
| 8969 | ** tclcmd: sqlite3_txn_state DB ?SCHEMA? |
| 8970 | ** |
| 8971 | ** Invoke sqlite3_txn_state(DB,SCHEMA) and return the |
| 8972 | ** numeric value that results. Use NULL for SCHEMA if the 3 argument |
| 8973 | ** is omitted. |
| 8974 | */ |
| 8975 | static int SQLITE_TCLAPI test_sqlite3_txn_state( |
| 8976 | void *clientData, |
| 8977 | Tcl_Interp *interp, |
| 8978 | int objc, |
| 8979 | Tcl_Obj *CONST objv[] |
| 8980 | ){ |
| 8981 | sqlite3 *db; |
| 8982 | const char *zSchema; |
| 8983 | int iTxn; |
| 8984 | |
| 8985 | if( objc!=2 && objc!=3 ){ |
| 8986 | Tcl_WrongNumArgs(interp, 1, objv, "DB ?SCHEMA?"); |
| 8987 | return TCL_ERROR; |
| 8988 | } |
| 8989 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 8990 | zSchema = objc==3 ? Tcl_GetString(objv[2]) : 0; |
| 8991 | iTxn = sqlite3_txn_state(db, zSchema); |
| 8992 | Tcl_SetObjResult(interp, Tcl_NewIntObj(iTxn)); |
| 8993 | return TCL_OK; |
| 8994 | } |
| 8995 | |
| 8996 | /* |
| 8997 | ** Change the name of the main database schema from "main" to "icecube". |
| 8998 | */ |
| 8999 | static int SQLITE_TCLAPI test_dbconfig_maindbname_icecube( |
| 9000 | void * clientData, |
| 9001 | Tcl_Interp *interp, |
| 9002 | int objc, |
| 9003 | Tcl_Obj *CONST objv[] |
| 9004 | ){ |
| 9005 | int rc; |
| 9006 | sqlite3 *db; |
| 9007 | extern int getDbPointer(Tcl_Interp*, const char*, sqlite3**); |
| 9008 | if( objc!=2 ){ |
| 9009 | Tcl_WrongNumArgs(interp, 1, objv, "DB"); |
| 9010 | return TCL_ERROR; |
| 9011 | }else{ |
| 9012 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 9013 | rc = sqlite3_db_config(db, SQLITE_DBCONFIG_MAINDBNAME, "icecube"); |
| 9014 | Tcl_SetObjResult(interp, Tcl_NewIntObj(rc)); |
| 9015 | return TCL_OK; |
| 9016 | } |
| 9017 | } |
| 9018 | |
| 9019 | /* |
| 9020 | ** Usage: sqlite3_mmap_warm DB DBNAME |
| 9021 | */ |
| 9022 | static int SQLITE_TCLAPI test_mmap_warm( |
| 9023 | void * clientData, |
| 9024 | Tcl_Interp *interp, |
| 9025 | int objc, |
| 9026 | Tcl_Obj *CONST objv[] |
| 9027 | ){ |
| 9028 | extern int getDbPointer(Tcl_Interp*, const char*, sqlite3**); |
| 9029 | extern int sqlite3_mmap_warm(sqlite3 *db, const char *); |
| 9030 | |
| 9031 | if( objc!=2 && objc!=3 ){ |
| 9032 | Tcl_WrongNumArgs(interp, 1, objv, "DB ?DBNAME?"); |
| 9033 | return TCL_ERROR; |
| 9034 | }else{ |
| 9035 | int rc; |
| 9036 | sqlite3 *db; |
| 9037 | const char *zDb = 0; |
| 9038 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 9039 | if( objc==3 ){ |
| 9040 | zDb = Tcl_GetString(objv[2]); |
| 9041 | } |
| 9042 | rc = sqlite3_mmap_warm(db, zDb); |
| 9043 | Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3ErrName(rc), -1)); |
| 9044 | return TCL_OK; |
| 9045 | } |
| 9046 | } |
| 9047 | |
| 9048 | /* |
| 9049 | ** Usage: test_write_db DB OFFSET DATA |
| 9050 | ** |
| 9051 | ** Obtain the sqlite3_file* object for the database file for the "main" db |
| 9052 | ** of handle DB. Then invoke its xWrite method to write data DATA to offset |
| 9053 | ** OFFSET. |
| 9054 | */ |
| 9055 | static int SQLITE_TCLAPI test_write_db( |
| 9056 | void * clientData, |
| 9057 | Tcl_Interp *interp, |
| 9058 | int objc, |
| 9059 | Tcl_Obj *CONST objv[] |
| 9060 | ){ |
| 9061 | sqlite3 *db = 0; |
| 9062 | Tcl_WideInt iOff = 0; |
| 9063 | const unsigned char *aData = 0; |
| 9064 | Tcl_Size nData = 0; |
| 9065 | sqlite3_file *pFile = 0; |
| 9066 | int rc; |
| 9067 | |
| 9068 | if( objc!=4 ){ |
| 9069 | Tcl_WrongNumArgs(interp, 1, objv, "DB OFFSET DATA"); |
| 9070 | return TCL_ERROR; |
| 9071 | } |
| 9072 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 9073 | if( Tcl_GetWideIntFromObj(interp, objv[2], &iOff) ) return TCL_ERROR; |
| 9074 | aData = Tcl_GetByteArrayFromObj(objv[3], &nData); |
| 9075 | |
| 9076 | sqlite3_file_control(db, "main", SQLITE_FCNTL_FILE_POINTER, (void*)&pFile); |
| 9077 | rc = pFile->pMethods->xWrite(pFile, aData, (int)(nData&0x7fffffff), iOff); |
| 9078 | |
| 9079 | Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_VOLATILE); |
| 9080 | return TCL_OK; |
| 9081 | } |
| 9082 | |
| 9083 | /* |
| 9084 | ** Usage: sqlite3_register_cksumvfs |
| 9085 | ** |
| 9086 | */ |
| 9087 | static int SQLITE_TCLAPI test_register_cksumvfs( |
| 9088 | void * clientData, |
| 9089 | Tcl_Interp *interp, |
| 9090 | int objc, |
| 9091 | Tcl_Obj *CONST objv[] |
| 9092 | ){ |
| 9093 | if( objc!=1 ){ |
| 9094 | Tcl_WrongNumArgs(interp, 1, objv, ""); |
| 9095 | return TCL_ERROR; |
| 9096 | }else{ |
| 9097 | extern int sqlite3_register_cksumvfs(const char*); |
| 9098 | int rc = sqlite3_register_cksumvfs(0); |
| 9099 | Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_VOLATILE); |
| 9100 | } |
| 9101 | return TCL_OK; |
| 9102 | } |
| 9103 | |
| 9104 | /* |
| 9105 | ** Usage: sqlite3_unregister_cksumvfs |
| 9106 | ** |
| 9107 | */ |
| 9108 | static int SQLITE_TCLAPI test_unregister_cksumvfs( |
| 9109 | void * clientData, |
| 9110 | Tcl_Interp *interp, |
| 9111 | int objc, |
| 9112 | Tcl_Obj *CONST objv[] |
| 9113 | ){ |
| 9114 | if( objc!=1 ){ |
| 9115 | Tcl_WrongNumArgs(interp, 1, objv, ""); |
| 9116 | return TCL_ERROR; |
| 9117 | }else{ |
| 9118 | extern int sqlite3_unregister_cksumvfs(void); |
| 9119 | int rc = sqlite3_unregister_cksumvfs(); |
| 9120 | Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_VOLATILE); |
| 9121 | } |
| 9122 | return TCL_OK; |
| 9123 | } |
| 9124 | |
| 9125 | /* |
| 9126 | ** Usage: decode_hexdb TEXT |
| 9127 | ** |
| 9128 | ** Example: db deserialize [decode_hexdb $output_of_dbtotxt] |
| 9129 | ** |
| 9130 | ** This routine returns a byte-array for an SQLite database file that |
| 9131 | ** is constructed from a text input which is the output of the "dbtotxt" |
| 9132 | ** utility. |
| 9133 | */ |
| 9134 | static int SQLITE_TCLAPI test_decode_hexdb( |
| 9135 | void * clientData, |
| 9136 | Tcl_Interp *interp, |
| 9137 | int objc, |
| 9138 | Tcl_Obj *CONST objv[] |
| 9139 | ){ |
| 9140 | const char *zIn = 0; |
| 9141 | unsigned char *a = 0; |
| 9142 | int n = 0; |
| 9143 | int i, iNext; |
| 9144 | int iOffset = 0; |
| 9145 | int j, k; |
| 9146 | int rc; |
| 9147 | unsigned int x[16]; |
| 9148 | if( objc!=2 ){ |
| 9149 | Tcl_WrongNumArgs(interp, 1, objv, "HEXDB"); |
| 9150 | return TCL_ERROR; |
| 9151 | } |
| 9152 | zIn = Tcl_GetString(objv[1]); |
| 9153 | for(i=0; zIn[i]; i=iNext){ |
| 9154 | for(iNext=i; zIn[iNext] && zIn[iNext]!='\n'; iNext++){} |
| 9155 | if( zIn[iNext]=='\n' ) iNext++; |
| 9156 | while( zIn[i]==' ' || zIn[i]=='\t' ){ i++; } |
| 9157 | if( a==0 ){ |
| 9158 | int pgsz; |
| 9159 | rc = sscanf(zIn+i, "| size %d pagesize %d", &n, &pgsz); |
| 9160 | if( rc!=2 ) continue; |
| 9161 | if( pgsz<512 || pgsz>65536 || (pgsz&(pgsz-1))!=0 ){ |
| 9162 | Tcl_AppendResult(interp, "bad 'pagesize' field", (void*)0); |
| 9163 | return TCL_ERROR; |
| 9164 | } |
| 9165 | n = (n+pgsz-1)&~(pgsz-1); /* Round n up to the next multiple of pgsz */ |
| 9166 | if( n<512 ){ |
| 9167 | Tcl_AppendResult(interp, "bad 'size' field", (void*)0); |
| 9168 | return TCL_ERROR; |
| 9169 | } |
| 9170 | a = malloc( n ); |
| 9171 | if( a==0 ){ |
| 9172 | Tcl_AppendResult(interp, "out of memory", (void*)0); |
| 9173 | return TCL_ERROR; |
| 9174 | } |
| 9175 | memset(a, 0, n); |
| 9176 | continue; |
| 9177 | } |
| 9178 | rc = sscanf(zIn+i, "| page %d offset %d", &j, &k); |
| 9179 | if( rc==2 ){ |
| 9180 | iOffset = k; |
| 9181 | continue; |
| 9182 | } |
| 9183 | rc = sscanf(zIn+i,"| %d: %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x", |
| 9184 | &j, &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7], |
| 9185 | &x[8], &x[9], &x[10], &x[11], &x[12], &x[13], &x[14], &x[15]); |
| 9186 | if( rc==17 ){ |
| 9187 | k = iOffset+j; |
| 9188 | if( k+16<=n ){ |
| 9189 | int ii; |
| 9190 | for(ii=0; ii<16; ii++) a[k+ii] = x[ii]&0xff; |
| 9191 | } |
| 9192 | continue; |
| 9193 | } |
| 9194 | } |
| 9195 | Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(a, n)); |
| 9196 | free(a); |
| 9197 | return TCL_OK; |
| 9198 | } |
| 9199 | |
| 9200 | /* |
| 9201 | ** Client data for the autovacuum_pages callback. |
| 9202 | */ |
| 9203 | struct AutovacPageData { |
| 9204 | Tcl_Interp *interp; |
| 9205 | char *zScript; |
| 9206 | }; |
| 9207 | typedef struct AutovacPageData AutovacPageData; |
| 9208 | |
| 9209 | /* |
| 9210 | ** Callback functions for sqlite3_autovacuum_pages |
| 9211 | */ |
| 9212 | static unsigned int test_autovacuum_pages_callback( |
| 9213 | void *pClientData, |
| 9214 | const char *zSchema, |
| 9215 | unsigned int nFilePages, |
| 9216 | unsigned int nFreePages, |
| 9217 | unsigned int nBytePerPage |
| 9218 | ){ |
| 9219 | AutovacPageData *pData = (AutovacPageData*)pClientData; |
| 9220 | Tcl_DString str; |
| 9221 | unsigned int x; |
| 9222 | char zBuf[100]; |
| 9223 | Tcl_DStringInit(&str); |
| 9224 | Tcl_DStringAppend(&str, pData->zScript, -1); |
| 9225 | Tcl_DStringAppendElement(&str, zSchema); |
| 9226 | sqlite3_snprintf(sizeof(zBuf), zBuf, "%u", nFilePages); |
| 9227 | Tcl_DStringAppendElement(&str, zBuf); |
| 9228 | sqlite3_snprintf(sizeof(zBuf), zBuf, "%u", nFreePages); |
| 9229 | Tcl_DStringAppendElement(&str, zBuf); |
| 9230 | sqlite3_snprintf(sizeof(zBuf), zBuf, "%u", nBytePerPage); |
| 9231 | Tcl_DStringAppendElement(&str, zBuf); |
| 9232 | Tcl_ResetResult(pData->interp); |
| 9233 | Tcl_Eval(pData->interp, Tcl_DStringValue(&str)); |
| 9234 | Tcl_DStringFree(&str); |
| 9235 | x = nFreePages; |
| 9236 | (void)Tcl_GetIntFromObj(0, Tcl_GetObjResult(pData->interp), (int*)&x); |
| 9237 | return x; |
| 9238 | } |
| 9239 | |
| 9240 | /* |
| 9241 | ** Usage: sqlite3_autovacuum_pages DB SCRIPT |
| 9242 | ** |
| 9243 | ** Add an autovacuum-pages callback to database connection DB. The callback |
| 9244 | ** will invoke SCRIPT, after appending parameters. |
| 9245 | ** |
| 9246 | ** If SCRIPT is an empty string or is omitted, then the callback is |
| 9247 | ** cancelled. |
| 9248 | */ |
| 9249 | static int SQLITE_TCLAPI test_autovacuum_pages( |
| 9250 | void * clientData, |
| 9251 | Tcl_Interp *interp, |
| 9252 | int objc, |
| 9253 | Tcl_Obj *CONST objv[] |
| 9254 | ){ |
| 9255 | AutovacPageData *pData; |
| 9256 | sqlite3 *db; |
| 9257 | int rc; |
| 9258 | const char *zScript; |
| 9259 | if( objc!=2 && objc!=3 ){ |
| 9260 | Tcl_WrongNumArgs(interp, 1, objv, "DB ?SCRIPT?"); |
| 9261 | return TCL_ERROR; |
| 9262 | } |
| 9263 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 9264 | zScript = objc==3 ? Tcl_GetString(objv[2]) : 0; |
| 9265 | if( zScript ){ |
| 9266 | size_t nScript = strlen(zScript); |
| 9267 | pData = sqlite3_malloc64( sizeof(*pData) + nScript + 1 ); |
| 9268 | if( pData==0 ){ |
| 9269 | Tcl_AppendResult(interp, "out of memory", (void*)0); |
| 9270 | return TCL_ERROR; |
| 9271 | } |
| 9272 | pData->interp = interp; |
| 9273 | pData->zScript = (char*)&pData[1]; |
| 9274 | memcpy(pData->zScript, zScript, nScript+1); |
| 9275 | rc = sqlite3_autovacuum_pages(db,test_autovacuum_pages_callback, |
| 9276 | pData, sqlite3_free); |
| 9277 | }else{ |
| 9278 | rc = sqlite3_autovacuum_pages(db, 0, 0, 0); |
| 9279 | } |
| 9280 | if( rc ){ |
| 9281 | char zBuf[1000]; |
| 9282 | sqlite3_snprintf(sizeof(zBuf), zBuf, |
| 9283 | "sqlite3_autovacuum_pages() returns %d", rc); |
| 9284 | Tcl_AppendResult(interp, zBuf, (void*)0); |
| 9285 | return TCL_ERROR; |
| 9286 | } |
| 9287 | return TCL_OK; |
| 9288 | } |
| 9289 | |
| 9290 | /* |
| 9291 | ** Usage: number_of_cores |
| 9292 | ** |
| 9293 | ** Return a guess at the number of available cores available on the |
| 9294 | ** processor on which this process is running. |
| 9295 | */ |
| 9296 | static int SQLITE_TCLAPI guess_number_of_cores( |
| 9297 | void * clientData, |
| 9298 | Tcl_Interp *interp, |
| 9299 | int objc, |
| 9300 | Tcl_Obj *CONST objv[] |
| 9301 | ){ |
| 9302 | unsigned int nCore = 1; |
| 9303 | #ifdef _WIN32 |
| 9304 | SYSTEM_INFO sysinfo; |
| 9305 | GetSystemInfo(&sysinfo); |
| 9306 | nCore = (unsigned int)sysinfo.dwNumberOfProcessors; |
| 9307 | #elif defined(__APPLE__) |
| 9308 | int nm[2]; |
| 9309 | size_t len = 4; |
| 9310 | nm[0] = CTL_HW; nm[1] = HW_AVAILCPU; |
| 9311 | sysctl(nm, 2, &nCore, &len, NULL, 0); |
| 9312 | if( nCore<1 ){ |
| 9313 | nm[1] = HW_NCPU; |
| 9314 | sysctl(nm, 2, &nCore, &len, NULL, 0); |
| 9315 | } |
| 9316 | #else |
| 9317 | nCore = sysconf(_SC_NPROCESSORS_ONLN); |
| 9318 | #endif |
| 9319 | if( nCore<=0 ) nCore = 1; |
| 9320 | Tcl_SetObjResult(interp, Tcl_NewIntObj((int)nCore)); |
| 9321 | return SQLITE_OK; |
| 9322 | } |
| 9323 | |
| 9324 | |
| 9325 | /* |
| 9326 | ** Register commands with the TCL interpreter. |
| 9327 | */ |
| 9328 | int Sqlitetest1_Init(Tcl_Interp *interp){ |
| 9329 | extern int sqlite3_search_count; |
| 9330 | extern int sqlite3_found_count; |
| 9331 | extern int sqlite3_interrupt_count; |
| 9332 | extern int sqlite3_open_file_count; |
| 9333 | extern int sqlite3_sort_count; |
| 9334 | extern int sqlite3_current_time; |
| 9335 | #if SQLITE_OS_UNIX && defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
| 9336 | extern int sqlite3_hostid_num; |
| 9337 | #endif |
| 9338 | extern int sqlite3_max_blobsize; |
| 9339 | extern int SQLITE_TCLAPI sqlite3BtreeSharedCacheReport(void*, |
| 9340 | Tcl_Interp*,int,Tcl_Obj*CONST*); |
| 9341 | static struct { |
| 9342 | char *zName; |
| 9343 | Tcl_CmdProc *xProc; |
| 9344 | } aCmd[] = { |
| 9345 | { "db_enter", (Tcl_CmdProc*)db_enter }, |
| 9346 | { "db_leave", (Tcl_CmdProc*)db_leave }, |
| 9347 | { "sqlite3_mprintf_int", (Tcl_CmdProc*)sqlite3_mprintf_int }, |
| 9348 | { "sqlite3_mprintf_int64", (Tcl_CmdProc*)sqlite3_mprintf_int64 }, |
| 9349 | { "sqlite3_mprintf_long", (Tcl_CmdProc*)sqlite3_mprintf_long }, |
| 9350 | { "sqlite3_mprintf_str", (Tcl_CmdProc*)sqlite3_mprintf_str }, |
| 9351 | { "sqlite3_snprintf_str", (Tcl_CmdProc*)sqlite3_snprintf_str }, |
| 9352 | { "sqlite3_mprintf_stronly", (Tcl_CmdProc*)sqlite3_mprintf_stronly}, |
| 9353 | { "sqlite3_mprintf_double", (Tcl_CmdProc*)sqlite3_mprintf_double }, |
| 9354 | { "sqlite3_mprintf_scaled", (Tcl_CmdProc*)sqlite3_mprintf_scaled }, |
| 9355 | { "sqlite3_mprintf_hexdouble", (Tcl_CmdProc*)sqlite3_mprintf_hexdouble}, |
| 9356 | { "sqlite3_mprintf_z_test", (Tcl_CmdProc*)test_mprintf_z }, |
| 9357 | { "sqlite3_mprintf_n_test", (Tcl_CmdProc*)test_mprintf_n }, |
| 9358 | { "sqlite3_snprintf_int", (Tcl_CmdProc*)test_snprintf_int }, |
| 9359 | { "sqlite3_last_insert_rowid", (Tcl_CmdProc*)test_last_rowid }, |
| 9360 | { "sqlite3_exec_printf", (Tcl_CmdProc*)test_exec_printf }, |
| 9361 | { "sqlite3_exec_hex", (Tcl_CmdProc*)test_exec_hex }, |
| 9362 | { "sqlite3_exec", (Tcl_CmdProc*)test_exec }, |
| 9363 | { "sqlite3_exec_nr", (Tcl_CmdProc*)test_exec_nr }, |
| 9364 | #ifndef SQLITE_OMIT_GET_TABLE |
| 9365 | { "sqlite3_get_table_printf", (Tcl_CmdProc*)test_get_table_printf }, |
| 9366 | #endif |
| 9367 | { "sqlite3_close", (Tcl_CmdProc*)sqlite_test_close }, |
| 9368 | { "sqlite3_close_v2", (Tcl_CmdProc*)sqlite_test_close_v2 }, |
| 9369 | { "sqlite3_create_function", (Tcl_CmdProc*)test_create_function }, |
| 9370 | { "sqlite3_create_aggregate", (Tcl_CmdProc*)test_create_aggregate }, |
| 9371 | { "sqlite3_drop_modules", (Tcl_CmdProc*)test_drop_modules }, |
| 9372 | { "sqlite_register_test_function", (Tcl_CmdProc*)test_register_func }, |
| 9373 | { "sqlite_abort", (Tcl_CmdProc*)sqlite_abort }, |
| 9374 | { "sqlite_bind", (Tcl_CmdProc*)test_bind }, |
| 9375 | { "breakpoint", (Tcl_CmdProc*)test_breakpoint }, |
| 9376 | { "sqlite3_key", (Tcl_CmdProc*)test_key }, |
| 9377 | { "sqlite3_rekey", (Tcl_CmdProc*)test_rekey }, |
| 9378 | { "sqlite3_interrupt", (Tcl_CmdProc*)test_interrupt }, |
| 9379 | { "sqlite3_is_interrupted", (Tcl_CmdProc*)test_is_interrupted }, |
| 9380 | { "sqlite_delete_function", (Tcl_CmdProc*)delete_function }, |
| 9381 | { "sqlite_delete_collation", (Tcl_CmdProc*)delete_collation }, |
| 9382 | { "sqlite3_get_autocommit", (Tcl_CmdProc*)get_autocommit }, |
| 9383 | { "sqlite3_busy_timeout", (Tcl_CmdProc*)test_busy_timeout }, |
| 9384 | { "sqlite3_setlk_timeout", (Tcl_CmdProc*)test_setlk_timeout }, |
| 9385 | { "printf", (Tcl_CmdProc*)test_printf }, |
| 9386 | { "sqlite3IoTrace", (Tcl_CmdProc*)test_io_trace }, |
| 9387 | { "clang_sanitize_address", (Tcl_CmdProc*)clang_sanitize_address }, |
| 9388 | { "filc_build", (Tcl_CmdProc*)filc_build }, |
| 9389 | }; |
| 9390 | static struct { |
| 9391 | char *zName; |
| 9392 | Tcl_ObjCmdProc *xProc; |
| 9393 | void *clientData; |
| 9394 | } aObjCmd[] = { |
| 9395 | { "sqlite3_db_config", test_sqlite3_db_config, 0 }, |
| 9396 | { "sqlite3_txn_state", test_sqlite3_txn_state, 0 }, |
| 9397 | { "register_dbstat_vtab", test_register_dbstat_vtab }, |
| 9398 | { "sqlite3_connection_pointer", get_sqlite_pointer, 0 }, |
| 9399 | { "intarray_addr", test_intarray_addr, 0 }, |
| 9400 | { "int64array_addr", test_int64array_addr, 0 }, |
| 9401 | { "doublearray_addr", test_doublearray_addr, 0 }, |
| 9402 | { "textarray_addr", test_textarray_addr, 0 }, |
| 9403 | { "sqlite3_bind_int", test_bind_int, 0 }, |
| 9404 | { "sqlite3_bind_zeroblob", test_bind_zeroblob, 0 }, |
| 9405 | { "sqlite3_bind_zeroblob64", test_bind_zeroblob64, 0 }, |
| 9406 | { "sqlite3_bind_int64", test_bind_int64, 0 }, |
| 9407 | { "sqlite3_bind_double", test_bind_double, 0 }, |
| 9408 | { "sqlite3_bind_null", test_bind_null ,0 }, |
| 9409 | { "sqlite3_bind_text", test_bind_text ,0 }, |
| 9410 | { "sqlite3_bind_text16", test_bind_text16 ,0 }, |
| 9411 | { "sqlite3_bind_blob", test_bind_blob ,0 }, |
| 9412 | { "sqlite3_bind_value_from_select",test_bind_value_from_select ,0 }, |
| 9413 | { "sqlite3_bind_value_from_preupdate",test_bind_value_from_preupdate ,0 }, |
| 9414 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 9415 | { "sqlite3_carray_bind", test_carray_bind ,0 }, |
| 9416 | { "bind_carray_intptr", bind_carray_intptr ,0 }, |
| 9417 | #endif |
| 9418 | { "sqlite3_bind_parameter_count", test_bind_parameter_count, 0}, |
| 9419 | { "sqlite3_bind_parameter_name", test_bind_parameter_name, 0}, |
| 9420 | { "sqlite3_bind_parameter_index", test_bind_parameter_index, 0}, |
| 9421 | { "sqlite3_clear_bindings", test_clear_bindings, 0}, |
| 9422 | { "sqlite3_sleep", test_sleep, 0}, |
| 9423 | { "sqlite3_errcode", test_errcode ,0 }, |
| 9424 | { "sqlite3_extended_errcode", test_ex_errcode ,0 }, |
| 9425 | { "sqlite3_errmsg", test_errmsg ,0 }, |
| 9426 | { "sqlite3_error_offset", test_error_offset ,0 }, |
| 9427 | { "sqlite3_errmsg16", test_errmsg16 ,0 }, |
| 9428 | { "sqlite3_set_errmsg", test_set_errmsg ,0 }, |
| 9429 | { "sqlite3_open", test_open ,0 }, |
| 9430 | { "sqlite3_open16", test_open16 ,0 }, |
| 9431 | { "sqlite3_open_v2", test_open_v2 ,0 }, |
| 9432 | { "sqlite3_complete16", test_complete16 ,0 }, |
| 9433 | { "sqlite3_normalize", test_normalize ,0 }, |
| 9434 | |
| 9435 | { "sqlite3_prepare", test_prepare ,0 }, |
| 9436 | { "sqlite3_prepare16", test_prepare16 ,0 }, |
| 9437 | { "sqlite3_prepare_v2", test_prepare_v2 ,0 }, |
| 9438 | { "sqlite3_prepare_v3", test_prepare_v3 ,0 }, |
| 9439 | { "sqlite3_prepare_tkt3134", test_prepare_tkt3134, 0}, |
| 9440 | { "sqlite3_prepare16_v2", test_prepare16_v2 ,0 }, |
| 9441 | { "sqlite3_finalize", test_finalize ,0 }, |
| 9442 | { "sqlite3_stmt_status", test_stmt_status ,0 }, |
| 9443 | { "sqlite3_reset", test_reset ,0 }, |
| 9444 | { "sqlite3_expired", test_expired ,0 }, |
| 9445 | { "sqlite3_transfer_bindings", test_transfer_bind ,0 }, |
| 9446 | { "sqlite3_changes", test_changes ,0 }, |
| 9447 | { "sqlite3_step", test_step ,0 }, |
| 9448 | { "sqlite3_sql", test_sql ,0 }, |
| 9449 | { "sqlite3_expanded_sql", test_ex_sql ,0 }, |
| 9450 | #ifdef SQLITE_ENABLE_NORMALIZE |
| 9451 | { "sqlite3_normalized_sql", test_norm_sql ,0 }, |
| 9452 | #endif |
| 9453 | { "sqlite3_next_stmt", test_next_stmt ,0 }, |
| 9454 | { "sqlite3_stmt_readonly", test_stmt_readonly ,0 }, |
| 9455 | { "sqlite3_stmt_isexplain", test_stmt_isexplain,0 }, |
| 9456 | { "sqlite3_stmt_explain", test_stmt_explain ,0 }, |
| 9457 | { "sqlite3_stmt_busy", test_stmt_busy ,0 }, |
| 9458 | { "uses_stmt_journal", uses_stmt_journal ,0 }, |
| 9459 | |
| 9460 | { "sqlite3_release_memory", test_release_memory, 0}, |
| 9461 | { "sqlite3_db_release_memory", test_db_release_memory, 0}, |
| 9462 | { "sqlite3_db_cacheflush", test_db_cacheflush, 0}, |
| 9463 | { "sqlite3_system_errno", test_system_errno, 0}, |
| 9464 | { "sqlite3_db_filename", test_db_filename, 0}, |
| 9465 | { "sqlite3_db_readonly", test_db_readonly, 0}, |
| 9466 | { "sqlite3_soft_heap_limit", test_soft_heap_limit, 0}, |
| 9467 | { "sqlite3_soft_heap_limit64", test_soft_heap_limit, 0}, |
| 9468 | { "sqlite3_hard_heap_limit64", test_hard_heap_limit, 0}, |
| 9469 | { "sqlite3_thread_cleanup", test_thread_cleanup, 0}, |
| 9470 | { "sqlite3_pager_refcounts", test_pager_refcounts, 0}, |
| 9471 | |
| 9472 | { "sqlite3_load_extension", test_load_extension, 0}, |
| 9473 | { "sqlite3_enable_load_extension", test_enable_load, 0}, |
| 9474 | { "sqlite3_extended_result_codes", test_extended_result_codes, 0}, |
| 9475 | { "sqlite3_limit", test_limit, 0}, |
| 9476 | { "dbconfig_maindbname_icecube", test_dbconfig_maindbname_icecube }, |
| 9477 | |
| 9478 | { "save_prng_state", save_prng_state, 0 }, |
| 9479 | { "restore_prng_state", restore_prng_state, 0 }, |
| 9480 | { "reset_prng_state", reset_prng_state, 0 }, |
| 9481 | { "prng_seed", prng_seed, 0 }, |
| 9482 | { "extra_schema_checks", extra_schema_checks, 0}, |
| 9483 | { "database_never_corrupt", database_never_corrupt, 0}, |
| 9484 | { "database_may_be_corrupt", database_may_be_corrupt, 0}, |
| 9485 | { "optimization_control", optimization_control,0}, |
| 9486 | #ifdef _WIN32 |
| 9487 | { "lock_win32_file", win32_file_lock, 0 }, |
| 9488 | { "map_win32_file", map_win32_file, 0 }, |
| 9489 | #endif |
| 9490 | { "tcl_objproc", runAsObjProc, 0 }, |
| 9491 | |
| 9492 | /* sqlite3_column_*() API */ |
| 9493 | { "sqlite3_column_count", test_column_count ,0 }, |
| 9494 | { "sqlite3_data_count", test_data_count ,0 }, |
| 9495 | { "sqlite3_column_type", test_column_type ,0 }, |
| 9496 | { "sqlite3_column_blob", test_column_blob ,0 }, |
| 9497 | { "sqlite3_column_double", test_column_double ,0 }, |
| 9498 | { "sqlite3_column_int64", test_column_int64 ,0 }, |
| 9499 | { "sqlite3_column_text", test_stmt_utf8, (void*)sqlite3_column_text }, |
| 9500 | { "sqlite3_column_name", test_stmt_utf8, (void*)sqlite3_column_name }, |
| 9501 | { "sqlite3_column_int", test_stmt_int, (void*)sqlite3_column_int }, |
| 9502 | { "sqlite3_column_bytes", test_stmt_int, (void*)sqlite3_column_bytes}, |
| 9503 | #ifndef SQLITE_OMIT_DECLTYPE |
| 9504 | { "sqlite3_column_decltype",test_stmt_utf8,(void*)sqlite3_column_decltype}, |
| 9505 | #endif |
| 9506 | #ifdef SQLITE_ENABLE_COLUMN_METADATA |
| 9507 | { "sqlite3_column_database_name",test_stmt_utf8,(void*)sqlite3_column_database_name}, |
| 9508 | { "sqlite3_column_table_name",test_stmt_utf8,(void*)sqlite3_column_table_name}, |
| 9509 | { "sqlite3_column_origin_name",test_stmt_utf8,(void*)sqlite3_column_origin_name}, |
| 9510 | #endif |
| 9511 | |
| 9512 | #ifndef SQLITE_OMIT_UTF16 |
| 9513 | { "sqlite3_column_bytes16", test_stmt_int, (void*)sqlite3_column_bytes16 }, |
| 9514 | { "sqlite3_column_text16", test_stmt_utf16, (void*)sqlite3_column_text16}, |
| 9515 | { "sqlite3_column_name16", test_stmt_utf16, (void*)sqlite3_column_name16}, |
| 9516 | { "add_alignment_test_collations", add_alignment_test_collations, 0 }, |
| 9517 | #ifndef SQLITE_OMIT_DECLTYPE |
| 9518 | { "sqlite3_column_decltype16",test_stmt_utf16,(void*)sqlite3_column_decltype16}, |
| 9519 | #endif |
| 9520 | #ifdef SQLITE_ENABLE_COLUMN_METADATA |
| 9521 | {"sqlite3_column_database_name16", |
| 9522 | test_stmt_utf16, (void*)sqlite3_column_database_name16}, |
| 9523 | {"sqlite3_column_table_name16", test_stmt_utf16, (void*)sqlite3_column_table_name16}, |
| 9524 | {"sqlite3_column_origin_name16", test_stmt_utf16, (void*)sqlite3_column_origin_name16}, |
| 9525 | #endif |
| 9526 | #endif |
| 9527 | { "sqlite3_create_collation_v2", test_create_collation_v2, 0 }, |
| 9528 | { "sqlite3_global_recover", test_global_recover, 0 }, |
| 9529 | { "working_64bit_int", working_64bit_int, 0 }, |
| 9530 | { "vfs_open_tempfile", vfs_open_tempfile, 0 }, |
| 9531 | { "vfs_unlink_test", vfs_unlink_test, 0 }, |
| 9532 | { "vfs_initfail_test", vfs_initfail_test, 0 }, |
| 9533 | { "vfs_unregister_all", vfs_unregister_all, 0 }, |
| 9534 | { "vfs_reregister_all", vfs_reregister_all, 0 }, |
| 9535 | { "file_control_test", file_control_test, 0 }, |
| 9536 | { "file_control_lasterrno_test", file_control_lasterrno_test, 0 }, |
| 9537 | { "file_control_lockproxy_test", file_control_lockproxy_test, 0 }, |
| 9538 | { "file_control_chunksize_test", file_control_chunksize_test, 0 }, |
| 9539 | { "file_control_sizehint_test", file_control_sizehint_test, 0 }, |
| 9540 | { "file_control_data_version", file_control_data_version, 0 }, |
| 9541 | #if SQLITE_OS_WIN |
| 9542 | { "file_control_win32_av_retry", file_control_win32_av_retry, 0 }, |
| 9543 | { "file_control_win32_get_handle", file_control_win32_get_handle, 0 }, |
| 9544 | { "file_control_win32_set_handle", file_control_win32_set_handle, 0 }, |
| 9545 | #endif |
| 9546 | { "file_control_persist_wal", file_control_persist_wal, 0 }, |
| 9547 | { "file_control_powersafe_overwrite",file_control_powersafe_overwrite,0}, |
| 9548 | { "file_control_vfsname", file_control_vfsname, 0 }, |
| 9549 | { "file_control_reservebytes", file_control_reservebytes, 0 }, |
| 9550 | { "file_control_tempfilename", file_control_tempfilename, 0 }, |
| 9551 | { "file_control_external_reader", file_control_external_reader, 0 }, |
| 9552 | { "sqlite3_vfs_list", vfs_list, 0 }, |
| 9553 | { "sqlite3_create_function_v2", test_create_function_v2, 0 }, |
| 9554 | |
| 9555 | /* Functions from os.h */ |
| 9556 | #ifndef SQLITE_OMIT_UTF16 |
| 9557 | { "add_test_collate", test_collate, 0 }, |
| 9558 | { "add_test_collate_needed", test_collate_needed, 0 }, |
| 9559 | { "add_test_function", test_function, 0 }, |
| 9560 | { "add_test_utf16bin_collate", test_utf16bin_collate, 0 }, |
| 9561 | #endif |
| 9562 | { "sqlite3_test_errstr", test_errstr, 0 }, |
| 9563 | { "tcl_variable_type", tcl_variable_type, 0 }, |
| 9564 | { "fpnum_compare", fpnum_compare, 0 }, |
| 9565 | #ifndef SQLITE_OMIT_SHARED_CACHE |
| 9566 | { "sqlite3_enable_shared_cache", test_enable_shared, 0 }, |
| 9567 | { "sqlite3_shared_cache_report", sqlite3BtreeSharedCacheReport, 0}, |
| 9568 | #endif |
| 9569 | { "sqlite3_libversion_number", test_libversion_number, 0 }, |
| 9570 | { "sqlite3_table_column_metadata", test_table_column_metadata, 0 }, |
| 9571 | #ifndef SQLITE_OMIT_INCRBLOB |
| 9572 | { "sqlite3_blob_reopen", test_blob_reopen, 0 }, |
| 9573 | #endif |
| 9574 | { "pcache_stats", test_pcache_stats, 0 }, |
| 9575 | #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY |
| 9576 | { "sqlite3_unlock_notify", test_unlock_notify, 0 }, |
| 9577 | #endif |
| 9578 | { "sqlite3_wal_checkpoint", test_wal_checkpoint, 0 }, |
| 9579 | { "sqlite3_wal_checkpoint_v2",test_wal_checkpoint_v2, 0 }, |
| 9580 | { "sqlite3_wal_autocheckpoint",test_wal_autocheckpoint, 0 }, |
| 9581 | { "test_sqlite3_log", test_sqlite3_log, 0 }, |
| 9582 | #ifndef SQLITE_OMIT_EXPLAIN |
| 9583 | { "print_explain_query_plan", test_print_eqp, 0 }, |
| 9584 | #endif |
| 9585 | { "strftime", strftime_cmd }, |
| 9586 | { "sqlite3_test_control", test_test_control }, |
| 9587 | { ".treetrace", test_treetrace }, |
| 9588 | #if SQLITE_OS_UNIX |
| 9589 | { "getrusage", test_getrusage }, |
| 9590 | #endif |
| 9591 | { "load_static_extension", tclLoadStaticExtensionCmd }, |
| 9592 | { "sorter_test_fakeheap", sorter_test_fakeheap }, |
| 9593 | { "sorter_test_sort4_helper", sorter_test_sort4_helper }, |
| 9594 | #ifdef SQLITE_ENABLE_STMT_SCANSTATUS |
| 9595 | { "sqlite3_stmt_scanstatus", test_stmt_scanstatus, 0 }, |
| 9596 | { "sqlite3_stmt_scanstatus_reset", test_stmt_scanstatus_reset, 0 }, |
| 9597 | #endif |
| 9598 | #ifdef SQLITE_ENABLE_SQLLOG |
| 9599 | { "sqlite3_config_sqllog", test_config_sqllog, 0 }, |
| 9600 | #endif |
| 9601 | { "vfs_current_time_int64", vfsCurrentTimeInt64, 0 }, |
| 9602 | #ifdef SQLITE_ENABLE_SNAPSHOT |
| 9603 | { "sqlite3_snapshot_get", test_snapshot_get, 0 }, |
| 9604 | { "sqlite3_snapshot_open", test_snapshot_open, 0 }, |
| 9605 | { "sqlite3_snapshot_free", test_snapshot_free, 0 }, |
| 9606 | { "sqlite3_snapshot_cmp", test_snapshot_cmp, 0 }, |
| 9607 | { "sqlite3_snapshot_recover", test_snapshot_recover, 0 }, |
| 9608 | { "sqlite3_snapshot_get_blob", test_snapshot_get_blob, 0 }, |
| 9609 | { "sqlite3_snapshot_open_blob", test_snapshot_open_blob, 0 }, |
| 9610 | { "sqlite3_snapshot_cmp_blob", test_snapshot_cmp_blob, 0 }, |
| 9611 | #endif |
| 9612 | { "sqlite3_delete_database", test_delete_database, 0 }, |
| 9613 | { "atomic_batch_write", test_atomic_batch_write, 0 }, |
| 9614 | { "sqlite3_mmap_warm", test_mmap_warm, 0 }, |
| 9615 | { "sqlite3_config_sorterref", test_config_sorterref, 0 }, |
| 9616 | { "sqlite3_autovacuum_pages", test_autovacuum_pages, 0 }, |
| 9617 | { "decode_hexdb", test_decode_hexdb, 0 }, |
| 9618 | { "test_write_db", test_write_db, 0 }, |
| 9619 | { "sqlite3_register_cksumvfs", test_register_cksumvfs, 0 }, |
| 9620 | { "sqlite3_unregister_cksumvfs", test_unregister_cksumvfs, 0 }, |
| 9621 | { "number_of_cores", guess_number_of_cores, 0 }, |
| 9622 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 9623 | { "create_null_module", test_create_null_module, 0 }, |
| 9624 | #endif |
| 9625 | }; |
| 9626 | static int bitmask_size = sizeof(Bitmask)*8; |
| 9627 | int i; |
| 9628 | extern int sqlite3_sync_count, sqlite3_fullsync_count; |
| 9629 | extern int sqlite3_opentemp_count; |
| 9630 | extern int sqlite3_like_count; |
| 9631 | extern int sqlite3_xferopt_count; |
| 9632 | extern int sqlite3_pager_readdb_count; |
| 9633 | extern int sqlite3_pager_writedb_count; |
| 9634 | extern int sqlite3_pager_writej_count; |
| 9635 | #if SQLITE_OS_WIN |
| 9636 | extern int sqlite3_win_test_unc_locking; |
| 9637 | extern LONG volatile sqlite3_os_type; |
| 9638 | #endif |
| 9639 | #ifdef SQLITE_DEBUG |
| 9640 | extern u32 sqlite3WhereTrace; |
| 9641 | extern int sqlite3OSTrace; |
| 9642 | extern int sqlite3WalTrace; |
| 9643 | #endif |
| 9644 | #ifdef SQLITE_TEST |
| 9645 | #ifdef SQLITE_ENABLE_FTS3 |
| 9646 | extern int sqlite3_fts3_enable_parentheses; |
| 9647 | #endif |
| 9648 | #endif |
| 9649 | |
| 9650 | for(i=0; i<sizeof(aCmd)/sizeof(aCmd[0]); i++){ |
| 9651 | Tcl_CreateCommand(interp, aCmd[i].zName, aCmd[i].xProc, 0, 0); |
| 9652 | } |
| 9653 | for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){ |
| 9654 | Tcl_CreateObjCommand(interp, aObjCmd[i].zName, |
| 9655 | aObjCmd[i].xProc, aObjCmd[i].clientData, 0); |
| 9656 | } |
| 9657 | Tcl_LinkVar(interp, "sqlite_search_count", |
| 9658 | (char*)&sqlite3_search_count, TCL_LINK_INT); |
| 9659 | Tcl_LinkVar(interp, "sqlite_found_count", |
| 9660 | (char*)&sqlite3_found_count, TCL_LINK_INT); |
| 9661 | Tcl_LinkVar(interp, "sqlite_sort_count", |
| 9662 | (char*)&sqlite3_sort_count, TCL_LINK_INT); |
| 9663 | Tcl_LinkVar(interp, "sqlite3_max_blobsize", |
| 9664 | (char*)&sqlite3_max_blobsize, TCL_LINK_INT); |
| 9665 | Tcl_LinkVar(interp, "sqlite_like_count", |
| 9666 | (char*)&sqlite3_like_count, TCL_LINK_INT); |
| 9667 | Tcl_LinkVar(interp, "sqlite_interrupt_count", |
| 9668 | (char*)&sqlite3_interrupt_count, TCL_LINK_INT); |
| 9669 | Tcl_LinkVar(interp, "sqlite_open_file_count", |
| 9670 | (char*)&sqlite3_open_file_count, TCL_LINK_INT); |
| 9671 | Tcl_LinkVar(interp, "sqlite_current_time", |
| 9672 | (char*)&sqlite3_current_time, TCL_LINK_INT); |
| 9673 | #if SQLITE_OS_UNIX && defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
| 9674 | Tcl_LinkVar(interp, "sqlite_hostid_num", |
| 9675 | (char*)&sqlite3_hostid_num, TCL_LINK_INT); |
| 9676 | #endif |
| 9677 | Tcl_LinkVar(interp, "sqlite3_xferopt_count", |
| 9678 | (char*)&sqlite3_xferopt_count, TCL_LINK_INT); |
| 9679 | Tcl_LinkVar(interp, "sqlite3_pager_readdb_count", |
| 9680 | (char*)&sqlite3_pager_readdb_count, TCL_LINK_INT); |
| 9681 | Tcl_LinkVar(interp, "sqlite3_pager_writedb_count", |
| 9682 | (char*)&sqlite3_pager_writedb_count, TCL_LINK_INT); |
| 9683 | Tcl_LinkVar(interp, "sqlite3_pager_writej_count", |
| 9684 | (char*)&sqlite3_pager_writej_count, TCL_LINK_INT); |
| 9685 | #ifndef SQLITE_OMIT_UTF16 |
| 9686 | Tcl_LinkVar(interp, "unaligned_string_counter", |
| 9687 | (char*)&unaligned_string_counter, TCL_LINK_INT); |
| 9688 | #endif |
| 9689 | #ifndef SQLITE_OMIT_UTF16 |
| 9690 | Tcl_LinkVar(interp, "sqlite_last_needed_collation", |
| 9691 | (char*)&pzNeededCollation, TCL_LINK_STRING|TCL_LINK_READ_ONLY); |
| 9692 | #endif |
| 9693 | #if SQLITE_OS_WIN |
| 9694 | Tcl_LinkVar(interp, "sqlite3_win_test_unc_locking", |
| 9695 | (char*)&sqlite3_win_test_unc_locking, TCL_LINK_INT); |
| 9696 | #endif |
| 9697 | #ifdef SQLITE_TEST |
| 9698 | { |
| 9699 | static const char *query_plan = "*** OBSOLETE VARIABLE ***"; |
| 9700 | Tcl_LinkVar(interp, "sqlite_query_plan", |
| 9701 | (char*)&query_plan, TCL_LINK_STRING|TCL_LINK_READ_ONLY); |
| 9702 | } |
| 9703 | #endif |
| 9704 | #ifdef SQLITE_DEBUG |
| 9705 | Tcl_LinkVar(interp, "sqlite_where_trace", |
| 9706 | (char*)&sqlite3WhereTrace, TCL_LINK_INT); |
| 9707 | Tcl_LinkVar(interp, "sqlite_os_trace", |
| 9708 | (char*)&sqlite3OSTrace, TCL_LINK_INT); |
| 9709 | #ifndef SQLITE_OMIT_WAL |
| 9710 | Tcl_LinkVar(interp, "sqlite_wal_trace", |
| 9711 | (char*)&sqlite3WalTrace, TCL_LINK_INT); |
| 9712 | #endif |
| 9713 | #endif |
| 9714 | #ifndef SQLITE_OMIT_DISKIO |
| 9715 | Tcl_LinkVar(interp, "sqlite_opentemp_count", |
| 9716 | (char*)&sqlite3_opentemp_count, TCL_LINK_INT); |
| 9717 | #endif |
| 9718 | Tcl_LinkVar(interp, "sqlite_static_bind_value", |
| 9719 | (char*)&sqlite_static_bind_value, TCL_LINK_STRING); |
| 9720 | Tcl_LinkVar(interp, "sqlite_static_bind_nbyte", |
| 9721 | (char*)&sqlite_static_bind_nbyte, TCL_LINK_INT); |
| 9722 | Tcl_LinkVar(interp, "sqlite_temp_directory", |
| 9723 | (char*)&sqlite3_temp_directory, TCL_LINK_STRING); |
| 9724 | Tcl_LinkVar(interp, "sqlite_data_directory", |
| 9725 | (char*)&sqlite3_data_directory, TCL_LINK_STRING); |
| 9726 | Tcl_LinkVar(interp, "bitmask_size", |
| 9727 | (char*)&bitmask_size, TCL_LINK_INT|TCL_LINK_READ_ONLY); |
| 9728 | Tcl_LinkVar(interp, "sqlite_sync_count", |
| 9729 | (char*)&sqlite3_sync_count, TCL_LINK_INT); |
| 9730 | Tcl_LinkVar(interp, "sqlite_fullsync_count", |
| 9731 | (char*)&sqlite3_fullsync_count, TCL_LINK_INT); |
| 9732 | #if defined(SQLITE_ENABLE_TREETRACE) |
| 9733 | Tcl_LinkVar(interp, "sqlite3_unsupported_treetrace", |
| 9734 | (char*)&sqlite3TreeTrace, TCL_LINK_INT); |
| 9735 | #endif |
| 9736 | #if defined(SQLITE_ENABLE_FTS3) && defined(SQLITE_TEST) |
| 9737 | Tcl_LinkVar(interp, "sqlite_fts3_enable_parentheses", |
| 9738 | (char*)&sqlite3_fts3_enable_parentheses, TCL_LINK_INT); |
| 9739 | #endif |
| 9740 | return TCL_OK; |
| 9741 | } |