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 the btree.c module in SQLite. 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 | #include "btreeInt.h" |
| 18 | #include "tclsqlite.h" |
| 19 | #include <stdlib.h> |
| 20 | #include <string.h> |
| 21 | |
| 22 | extern const char *sqlite3ErrName(int); |
| 23 | |
| 24 | /* |
| 25 | ** A bogus sqlite3 connection structure for use in the btree |
| 26 | ** tests. |
| 27 | */ |
| 28 | static sqlite3 sDb; |
| 29 | static int nRefSqlite3 = 0; |
| 30 | |
| 31 | /* |
| 32 | ** Usage: btree_open FILENAME NCACHE |
| 33 | ** |
| 34 | ** Open a new database |
| 35 | */ |
| 36 | static int SQLITE_TCLAPI btree_open( |
| 37 | void *NotUsed, |
| 38 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 39 | int argc, /* Number of arguments */ |
| 40 | const char **argv /* Text of each argument */ |
| 41 | ){ |
| 42 | Btree *pBt; |
| 43 | int rc, nCache; |
| 44 | int n; |
| 45 | char *zFilename; |
| 46 | if( argc!=3 ){ |
| 47 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 48 | " FILENAME NCACHE FLAGS\"", NULL); |
| 49 | return TCL_ERROR; |
| 50 | } |
| 51 | |
| 52 | if( Tcl_GetInt(interp, argv[2], &nCache) ) return TCL_ERROR; |
| 53 | nRefSqlite3++; |
| 54 | if( nRefSqlite3==1 ){ |
| 55 | sDb.pVfs = sqlite3_vfs_find(0); |
| 56 | sDb.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_RECURSIVE); |
| 57 | sqlite3_mutex_enter(sDb.mutex); |
| 58 | } |
| 59 | n = (int)strlen(argv[1]); |
| 60 | zFilename = sqlite3_malloc( n+2 ); |
| 61 | if( zFilename==0 ) return TCL_ERROR; |
| 62 | memcpy(zFilename, argv[1], n+1); |
| 63 | zFilename[n+1] = 0; |
| 64 | rc = sqlite3BtreeOpen(sDb.pVfs, zFilename, &sDb, &pBt, 0, |
| 65 | SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_MAIN_DB); |
| 66 | sqlite3_free(zFilename); |
| 67 | if( rc!=SQLITE_OK ){ |
| 68 | Tcl_AppendResult(interp, sqlite3ErrName(rc), NULL); |
| 69 | return TCL_ERROR; |
| 70 | } |
| 71 | sqlite3BtreeSetCacheSize(pBt, nCache); |
| 72 | Tcl_AppendResult(interp, sqlite3TestPtrToText(pBt), NULL); |
| 73 | return TCL_OK; |
| 74 | } |
| 75 | |
| 76 | /* |
| 77 | ** Usage: btree_close ID |
| 78 | ** |
| 79 | ** Close the given database. |
| 80 | */ |
| 81 | static int SQLITE_TCLAPI btree_close( |
| 82 | void *NotUsed, |
| 83 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 84 | int argc, /* Number of arguments */ |
| 85 | const char **argv /* Text of each argument */ |
| 86 | ){ |
| 87 | Btree *pBt; |
| 88 | int rc; |
| 89 | if( argc!=2 ){ |
| 90 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 91 | " ID\"", NULL); |
| 92 | return TCL_ERROR; |
| 93 | } |
| 94 | pBt = sqlite3TestTextToPtr(argv[1]); |
| 95 | rc = sqlite3BtreeClose(pBt); |
| 96 | if( rc!=SQLITE_OK ){ |
| 97 | Tcl_AppendResult(interp, sqlite3ErrName(rc), NULL); |
| 98 | return TCL_ERROR; |
| 99 | } |
| 100 | nRefSqlite3--; |
| 101 | if( nRefSqlite3==0 ){ |
| 102 | sqlite3_mutex_leave(sDb.mutex); |
| 103 | sqlite3_mutex_free(sDb.mutex); |
| 104 | sDb.mutex = 0; |
| 105 | sDb.pVfs = 0; |
| 106 | } |
| 107 | return TCL_OK; |
| 108 | } |
| 109 | |
| 110 | |
| 111 | /* |
| 112 | ** Usage: btree_begin_transaction ID |
| 113 | ** |
| 114 | ** Start a new transaction |
| 115 | */ |
| 116 | static int SQLITE_TCLAPI btree_begin_transaction( |
| 117 | void *NotUsed, |
| 118 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 119 | int argc, /* Number of arguments */ |
| 120 | const char **argv /* Text of each argument */ |
| 121 | ){ |
| 122 | Btree *pBt; |
| 123 | int rc; |
| 124 | if( argc!=2 ){ |
| 125 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 126 | " ID\"", NULL); |
| 127 | return TCL_ERROR; |
| 128 | } |
| 129 | pBt = sqlite3TestTextToPtr(argv[1]); |
| 130 | sqlite3BtreeEnter(pBt); |
| 131 | rc = sqlite3BtreeBeginTrans(pBt, 1, 0); |
| 132 | sqlite3BtreeLeave(pBt); |
| 133 | if( rc!=SQLITE_OK ){ |
| 134 | Tcl_AppendResult(interp, sqlite3ErrName(rc), NULL); |
| 135 | return TCL_ERROR; |
| 136 | } |
| 137 | return TCL_OK; |
| 138 | } |
| 139 | |
| 140 | /* |
| 141 | ** Usage: btree_pager_stats ID |
| 142 | ** |
| 143 | ** Returns pager statistics |
| 144 | */ |
| 145 | static int SQLITE_TCLAPI btree_pager_stats( |
| 146 | void *NotUsed, |
| 147 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 148 | int argc, /* Number of arguments */ |
| 149 | const char **argv /* Text of each argument */ |
| 150 | ){ |
| 151 | Btree *pBt; |
| 152 | int i; |
| 153 | int *a; |
| 154 | |
| 155 | if( argc!=2 ){ |
| 156 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 157 | " ID\"", NULL); |
| 158 | return TCL_ERROR; |
| 159 | } |
| 160 | pBt = sqlite3TestTextToPtr(argv[1]); |
| 161 | |
| 162 | /* Normally in this file, with a b-tree handle opened using the |
| 163 | ** [btree_open] command it is safe to call sqlite3BtreeEnter() directly. |
| 164 | ** But this function is sometimes called with a btree handle obtained |
| 165 | ** from an open SQLite connection (using [btree_from_db]). In this case |
| 166 | ** we need to obtain the mutex for the controlling SQLite handle before |
| 167 | ** it is safe to call sqlite3BtreeEnter(). |
| 168 | */ |
| 169 | sqlite3_mutex_enter(pBt->db->mutex); |
| 170 | |
| 171 | sqlite3BtreeEnter(pBt); |
| 172 | a = sqlite3PagerStats(sqlite3BtreePager(pBt)); |
| 173 | for(i=0; i<11; i++){ |
| 174 | static char *zName[] = { |
| 175 | "ref", "page", "max", "size", "state", "err", |
| 176 | "hit", "miss", "ovfl", "read", "write" |
| 177 | }; |
| 178 | char zBuf[100]; |
| 179 | Tcl_AppendElement(interp, zName[i]); |
| 180 | sqlite3_snprintf(sizeof(zBuf), zBuf,"%d",a[i]); |
| 181 | Tcl_AppendElement(interp, zBuf); |
| 182 | } |
| 183 | sqlite3BtreeLeave(pBt); |
| 184 | |
| 185 | /* Release the mutex on the SQLite handle that controls this b-tree */ |
| 186 | sqlite3_mutex_leave(pBt->db->mutex); |
| 187 | return TCL_OK; |
| 188 | } |
| 189 | |
| 190 | /* |
| 191 | ** Usage: btree_cursor ID TABLENUM WRITEABLE |
| 192 | ** |
| 193 | ** Create a new cursor. Return the ID for the cursor. |
| 194 | */ |
| 195 | static int SQLITE_TCLAPI btree_cursor( |
| 196 | void *NotUsed, |
| 197 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 198 | int argc, /* Number of arguments */ |
| 199 | const char **argv /* Text of each argument */ |
| 200 | ){ |
| 201 | Btree *pBt; |
| 202 | int iTable; |
| 203 | BtCursor *pCur; |
| 204 | int rc = SQLITE_OK; |
| 205 | int wrFlag; |
| 206 | |
| 207 | if( argc!=4 ){ |
| 208 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 209 | " ID TABLENUM WRITEABLE\"", NULL); |
| 210 | return TCL_ERROR; |
| 211 | } |
| 212 | pBt = sqlite3TestTextToPtr(argv[1]); |
| 213 | if( Tcl_GetInt(interp, argv[2], &iTable) ) return TCL_ERROR; |
| 214 | if( Tcl_GetBoolean(interp, argv[3], &wrFlag) ) return TCL_ERROR; |
| 215 | if( wrFlag ) wrFlag = BTREE_WRCSR; |
| 216 | pCur = (BtCursor *)ckalloc(sqlite3BtreeCursorSize()); |
| 217 | memset(pCur, 0, sqlite3BtreeCursorSize()); |
| 218 | sqlite3_mutex_enter(pBt->db->mutex); |
| 219 | sqlite3BtreeEnter(pBt); |
| 220 | #ifndef SQLITE_OMIT_SHARED_CACHE |
| 221 | rc = sqlite3BtreeLockTable(pBt, iTable, !!wrFlag); |
| 222 | #endif |
| 223 | if( rc==SQLITE_OK ){ |
| 224 | rc = sqlite3BtreeCursor(pBt, iTable, wrFlag, 0, pCur); |
| 225 | } |
| 226 | sqlite3BtreeLeave(pBt); |
| 227 | sqlite3_mutex_leave(pBt->db->mutex); |
| 228 | if( rc ){ |
| 229 | ckfree((char *)pCur); |
| 230 | Tcl_AppendResult(interp, sqlite3ErrName(rc), NULL); |
| 231 | return TCL_ERROR; |
| 232 | } |
| 233 | Tcl_AppendResult(interp, sqlite3TestPtrToText(pCur), NULL); |
| 234 | return SQLITE_OK; |
| 235 | } |
| 236 | |
| 237 | /* |
| 238 | ** Usage: btree_close_cursor ID |
| 239 | ** |
| 240 | ** Close a cursor opened using btree_cursor. |
| 241 | */ |
| 242 | static int SQLITE_TCLAPI btree_close_cursor( |
| 243 | void *NotUsed, |
| 244 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 245 | int argc, /* Number of arguments */ |
| 246 | const char **argv /* Text of each argument */ |
| 247 | ){ |
| 248 | BtCursor *pCur; |
| 249 | int rc; |
| 250 | |
| 251 | if( argc!=2 ){ |
| 252 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 253 | " ID\"", NULL); |
| 254 | return TCL_ERROR; |
| 255 | } |
| 256 | pCur = sqlite3TestTextToPtr(argv[1]); |
| 257 | #if SQLITE_THREADSAFE>0 |
| 258 | { |
| 259 | Btree *pBt = pCur->pBtree; |
| 260 | sqlite3_mutex_enter(pBt->db->mutex); |
| 261 | sqlite3BtreeEnter(pBt); |
| 262 | rc = sqlite3BtreeCloseCursor(pCur); |
| 263 | sqlite3BtreeLeave(pBt); |
| 264 | sqlite3_mutex_leave(pBt->db->mutex); |
| 265 | } |
| 266 | #else |
| 267 | rc = sqlite3BtreeCloseCursor(pCur); |
| 268 | #endif |
| 269 | ckfree((char *)pCur); |
| 270 | if( rc ){ |
| 271 | Tcl_AppendResult(interp, sqlite3ErrName(rc), NULL); |
| 272 | return TCL_ERROR; |
| 273 | } |
| 274 | return SQLITE_OK; |
| 275 | } |
| 276 | |
| 277 | /* |
| 278 | ** Usage: btree_next ID |
| 279 | ** |
| 280 | ** Move the cursor to the next entry in the table. Return 0 on success |
| 281 | ** or 1 if the cursor was already on the last entry in the table or if |
| 282 | ** the table is empty. |
| 283 | */ |
| 284 | static int SQLITE_TCLAPI btree_next( |
| 285 | void *NotUsed, |
| 286 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 287 | int argc, /* Number of arguments */ |
| 288 | const char **argv /* Text of each argument */ |
| 289 | ){ |
| 290 | BtCursor *pCur; |
| 291 | int rc; |
| 292 | int res = 0; |
| 293 | char zBuf[100]; |
| 294 | |
| 295 | if( argc!=2 ){ |
| 296 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 297 | " ID\"", NULL); |
| 298 | return TCL_ERROR; |
| 299 | } |
| 300 | pCur = sqlite3TestTextToPtr(argv[1]); |
| 301 | sqlite3BtreeEnter(pCur->pBtree); |
| 302 | rc = sqlite3BtreeNext(pCur, 0); |
| 303 | if( rc==SQLITE_DONE ){ |
| 304 | res = 1; |
| 305 | rc = SQLITE_OK; |
| 306 | } |
| 307 | sqlite3BtreeLeave(pCur->pBtree); |
| 308 | if( rc ){ |
| 309 | Tcl_AppendResult(interp, sqlite3ErrName(rc), NULL); |
| 310 | return TCL_ERROR; |
| 311 | } |
| 312 | sqlite3_snprintf(sizeof(zBuf),zBuf,"%d",res); |
| 313 | Tcl_AppendResult(interp, zBuf, NULL); |
| 314 | return SQLITE_OK; |
| 315 | } |
| 316 | |
| 317 | /* |
| 318 | ** Usage: btree_first ID |
| 319 | ** |
| 320 | ** Move the cursor to the first entry in the table. Return 0 if the |
| 321 | ** cursor was left point to something and 1 if the table is empty. |
| 322 | */ |
| 323 | static int SQLITE_TCLAPI btree_first( |
| 324 | void *NotUsed, |
| 325 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 326 | int argc, /* Number of arguments */ |
| 327 | const char **argv /* Text of each argument */ |
| 328 | ){ |
| 329 | BtCursor *pCur; |
| 330 | int rc; |
| 331 | int res = 0; |
| 332 | char zBuf[100]; |
| 333 | |
| 334 | if( argc!=2 ){ |
| 335 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 336 | " ID\"", NULL); |
| 337 | return TCL_ERROR; |
| 338 | } |
| 339 | pCur = sqlite3TestTextToPtr(argv[1]); |
| 340 | sqlite3BtreeEnter(pCur->pBtree); |
| 341 | rc = sqlite3BtreeFirst(pCur, &res); |
| 342 | sqlite3BtreeLeave(pCur->pBtree); |
| 343 | if( rc ){ |
| 344 | Tcl_AppendResult(interp, sqlite3ErrName(rc), NULL); |
| 345 | return TCL_ERROR; |
| 346 | } |
| 347 | sqlite3_snprintf(sizeof(zBuf),zBuf,"%d",res); |
| 348 | Tcl_AppendResult(interp, zBuf, NULL); |
| 349 | return SQLITE_OK; |
| 350 | } |
| 351 | |
| 352 | /* |
| 353 | ** Usage: btree_eof ID |
| 354 | ** |
| 355 | ** Return TRUE if the given cursor is not pointing at a valid entry. |
| 356 | ** Return FALSE if the cursor does point to a valid entry. |
| 357 | */ |
| 358 | static int SQLITE_TCLAPI btree_eof( |
| 359 | void *NotUsed, |
| 360 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 361 | int argc, /* Number of arguments */ |
| 362 | const char **argv /* Text of each argument */ |
| 363 | ){ |
| 364 | BtCursor *pCur; |
| 365 | int rc; |
| 366 | char zBuf[50]; |
| 367 | |
| 368 | if( argc!=2 ){ |
| 369 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 370 | " ID\"", NULL); |
| 371 | return TCL_ERROR; |
| 372 | } |
| 373 | pCur = sqlite3TestTextToPtr(argv[1]); |
| 374 | sqlite3BtreeEnter(pCur->pBtree); |
| 375 | rc = sqlite3BtreeEof(pCur); |
| 376 | sqlite3BtreeLeave(pCur->pBtree); |
| 377 | sqlite3_snprintf(sizeof(zBuf),zBuf, "%d", rc); |
| 378 | Tcl_AppendResult(interp, zBuf, NULL); |
| 379 | return SQLITE_OK; |
| 380 | } |
| 381 | |
| 382 | /* |
| 383 | ** Usage: btree_payload_size ID |
| 384 | ** |
| 385 | ** Return the number of bytes of payload |
| 386 | */ |
| 387 | static int SQLITE_TCLAPI btree_payload_size( |
| 388 | void *NotUsed, |
| 389 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 390 | int argc, /* Number of arguments */ |
| 391 | const char **argv /* Text of each argument */ |
| 392 | ){ |
| 393 | BtCursor *pCur; |
| 394 | u32 n; |
| 395 | char zBuf[50]; |
| 396 | |
| 397 | if( argc!=2 ){ |
| 398 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 399 | " ID\"", NULL); |
| 400 | return TCL_ERROR; |
| 401 | } |
| 402 | pCur = sqlite3TestTextToPtr(argv[1]); |
| 403 | sqlite3BtreeEnter(pCur->pBtree); |
| 404 | n = sqlite3BtreePayloadSize(pCur); |
| 405 | sqlite3BtreeLeave(pCur->pBtree); |
| 406 | sqlite3_snprintf(sizeof(zBuf),zBuf, "%u", n); |
| 407 | Tcl_AppendResult(interp, zBuf, NULL); |
| 408 | return SQLITE_OK; |
| 409 | } |
| 410 | |
| 411 | /* |
| 412 | ** usage: varint_test START MULTIPLIER COUNT INCREMENT |
| 413 | ** |
| 414 | ** This command tests the sqlite3PutVarint() and sqlite3GetVarint() |
| 415 | ** routines, both for accuracy and for speed. |
| 416 | ** |
| 417 | ** An integer is written using sqlite3PutVarint() and read back with |
| 418 | ** sqlite3GetVarint() and verified to be unchanged. This repeats COUNT |
| 419 | ** times. The first integer is START*MULTIPLIER. Each iteration |
| 420 | ** increases the integer by INCREMENT. |
| 421 | ** |
| 422 | ** This command returns nothing if it works. It returns an error message |
| 423 | ** if something goes wrong. |
| 424 | */ |
| 425 | static int SQLITE_TCLAPI btree_varint_test( |
| 426 | void *NotUsed, |
| 427 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 428 | int argc, /* Number of arguments */ |
| 429 | const char **argv /* Text of each argument */ |
| 430 | ){ |
| 431 | u32 start, mult, count, incr; |
| 432 | u64 in, out; |
| 433 | int n1, n2, i, j; |
| 434 | unsigned char zBuf[100]; |
| 435 | if( argc!=5 ){ |
| 436 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 437 | " START MULTIPLIER COUNT INCREMENT\"", NULL); |
| 438 | return TCL_ERROR; |
| 439 | } |
| 440 | if( Tcl_GetInt(interp, argv[1], (int*)&start) ) return TCL_ERROR; |
| 441 | if( Tcl_GetInt(interp, argv[2], (int*)&mult) ) return TCL_ERROR; |
| 442 | if( Tcl_GetInt(interp, argv[3], (int*)&count) ) return TCL_ERROR; |
| 443 | if( Tcl_GetInt(interp, argv[4], (int*)&incr) ) return TCL_ERROR; |
| 444 | in = start; |
| 445 | in *= mult; |
| 446 | for(i=0; i<(int)count; i++){ |
| 447 | char zErr[200]; |
| 448 | n1 = sqlite3PutVarint(zBuf, in); |
| 449 | if( n1>9 || n1<1 ){ |
| 450 | sqlite3_snprintf(sizeof(zErr), zErr, |
| 451 | "putVarint returned %d - should be between 1 and 9", n1); |
| 452 | Tcl_AppendResult(interp, zErr, NULL); |
| 453 | return TCL_ERROR; |
| 454 | } |
| 455 | n2 = sqlite3GetVarint(zBuf, &out); |
| 456 | if( n1!=n2 ){ |
| 457 | sqlite3_snprintf(sizeof(zErr), zErr, |
| 458 | "putVarint returned %d and getVarint returned %d", n1, n2); |
| 459 | Tcl_AppendResult(interp, zErr, NULL); |
| 460 | return TCL_ERROR; |
| 461 | } |
| 462 | if( in!=out ){ |
| 463 | sqlite3_snprintf(sizeof(zErr), zErr, |
| 464 | "Wrote 0x%016llx and got back 0x%016llx", in, out); |
| 465 | Tcl_AppendResult(interp, zErr, NULL); |
| 466 | return TCL_ERROR; |
| 467 | } |
| 468 | if( (in & 0xffffffff)==in ){ |
| 469 | u32 out32; |
| 470 | n2 = getVarint32(zBuf, out32); |
| 471 | out = out32; |
| 472 | if( n1!=n2 ){ |
| 473 | sqlite3_snprintf(sizeof(zErr), zErr, |
| 474 | "putVarint returned %d and GetVarint32 returned %d", |
| 475 | n1, n2); |
| 476 | Tcl_AppendResult(interp, zErr, NULL); |
| 477 | return TCL_ERROR; |
| 478 | } |
| 479 | if( in!=out ){ |
| 480 | sqlite3_snprintf(sizeof(zErr), zErr, |
| 481 | "Wrote 0x%016llx and got back 0x%016llx from GetVarint32", |
| 482 | in, out); |
| 483 | Tcl_AppendResult(interp, zErr, NULL); |
| 484 | return TCL_ERROR; |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | /* In order to get realistic timings, run getVarint 19 more times. |
| 489 | ** This is because getVarint is called about 20 times more often |
| 490 | ** than putVarint. |
| 491 | */ |
| 492 | for(j=0; j<19; j++){ |
| 493 | sqlite3GetVarint(zBuf, &out); |
| 494 | } |
| 495 | in += incr; |
| 496 | } |
| 497 | return TCL_OK; |
| 498 | } |
| 499 | |
| 500 | /* |
| 501 | ** usage: btree_from_db DB-HANDLE |
| 502 | ** |
| 503 | ** This command returns the btree handle for the main database associated |
| 504 | ** with the database-handle passed as the argument. Example usage: |
| 505 | ** |
| 506 | ** sqlite3 db test.db |
| 507 | ** set bt [btree_from_db db] |
| 508 | */ |
| 509 | static int SQLITE_TCLAPI btree_from_db( |
| 510 | void *NotUsed, |
| 511 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 512 | int argc, /* Number of arguments */ |
| 513 | const char **argv /* Text of each argument */ |
| 514 | ){ |
| 515 | Tcl_CmdInfo info; |
| 516 | sqlite3 *db; |
| 517 | Btree *pBt; |
| 518 | int iDb = 0; |
| 519 | |
| 520 | if( argc!=2 && argc!=3 ){ |
| 521 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 522 | " DB-HANDLE ?N?\"", NULL); |
| 523 | return TCL_ERROR; |
| 524 | } |
| 525 | |
| 526 | if( 1!=Tcl_GetCommandInfo(interp, argv[1], &info) ){ |
| 527 | Tcl_AppendResult(interp, "No such db-handle: \"", argv[1], "\"", NULL); |
| 528 | return TCL_ERROR; |
| 529 | } |
| 530 | if( argc==3 ){ |
| 531 | iDb = atoi(argv[2]); |
| 532 | } |
| 533 | |
| 534 | db = *((sqlite3 **)info.objClientData); |
| 535 | assert( db ); |
| 536 | |
| 537 | pBt = db->aDb[iDb].pBt; |
| 538 | Tcl_SetResult(interp, (char*)sqlite3TestPtrToText(pBt), TCL_VOLATILE); |
| 539 | return TCL_OK; |
| 540 | } |
| 541 | |
| 542 | /* |
| 543 | ** Usage: btree_ismemdb ID |
| 544 | ** |
| 545 | ** Return true if the B-Tree is currently stored entirely in memory. |
| 546 | */ |
| 547 | static int SQLITE_TCLAPI btree_ismemdb( |
| 548 | void *NotUsed, |
| 549 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 550 | int argc, /* Number of arguments */ |
| 551 | const char **argv /* Text of each argument */ |
| 552 | ){ |
| 553 | Btree *pBt; |
| 554 | int res; |
| 555 | sqlite3_file *pFile; |
| 556 | |
| 557 | if( argc!=2 ){ |
| 558 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 559 | " ID\"", NULL); |
| 560 | return TCL_ERROR; |
| 561 | } |
| 562 | pBt = sqlite3TestTextToPtr(argv[1]); |
| 563 | sqlite3_mutex_enter(pBt->db->mutex); |
| 564 | sqlite3BtreeEnter(pBt); |
| 565 | pFile = sqlite3PagerFile(sqlite3BtreePager(pBt)); |
| 566 | res = (pFile->pMethods==0); |
| 567 | sqlite3BtreeLeave(pBt); |
| 568 | sqlite3_mutex_leave(pBt->db->mutex); |
| 569 | Tcl_SetObjResult(interp, Tcl_NewBooleanObj(res)); |
| 570 | return SQLITE_OK; |
| 571 | } |
| 572 | |
| 573 | /* |
| 574 | ** usage: btree_set_cache_size ID NCACHE |
| 575 | ** |
| 576 | ** Set the size of the cache used by btree $ID. |
| 577 | */ |
| 578 | static int SQLITE_TCLAPI btree_set_cache_size( |
| 579 | void *NotUsed, |
| 580 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 581 | int argc, /* Number of arguments */ |
| 582 | const char **argv /* Text of each argument */ |
| 583 | ){ |
| 584 | int nCache; |
| 585 | Btree *pBt; |
| 586 | |
| 587 | if( argc!=3 ){ |
| 588 | Tcl_AppendResult( |
| 589 | interp, "wrong # args: should be \"", argv[0], " BT NCACHE\"", NULL); |
| 590 | return TCL_ERROR; |
| 591 | } |
| 592 | pBt = sqlite3TestTextToPtr(argv[1]); |
| 593 | if( Tcl_GetInt(interp, argv[2], &nCache) ) return TCL_ERROR; |
| 594 | |
| 595 | sqlite3_mutex_enter(pBt->db->mutex); |
| 596 | sqlite3BtreeEnter(pBt); |
| 597 | sqlite3BtreeSetCacheSize(pBt, nCache); |
| 598 | sqlite3BtreeLeave(pBt); |
| 599 | sqlite3_mutex_leave(pBt->db->mutex); |
| 600 | return TCL_OK; |
| 601 | } |
| 602 | |
| 603 | /* |
| 604 | ** usage: btree_insert CSR ?KEY? VALUE |
| 605 | ** |
| 606 | ** Set the size of the cache used by btree $ID. |
| 607 | */ |
| 608 | static int SQLITE_TCLAPI btree_insert( |
| 609 | ClientData clientData, |
| 610 | Tcl_Interp *interp, |
| 611 | int objc, |
| 612 | Tcl_Obj *const objv[] |
| 613 | ){ |
| 614 | BtCursor *pCur; |
| 615 | int rc; |
| 616 | BtreePayload x; |
| 617 | Tcl_Size n; |
| 618 | |
| 619 | if( objc!=4 && objc!=3 ){ |
| 620 | Tcl_WrongNumArgs(interp, 1, objv, "?-intkey? CSR KEY VALUE"); |
| 621 | return TCL_ERROR; |
| 622 | } |
| 623 | |
| 624 | memset(&x, 0, sizeof(x)); |
| 625 | if( objc==4 ){ |
| 626 | if( Tcl_GetIntFromObj(interp, objv[2], &rc) ) return TCL_ERROR; |
| 627 | x.nKey = rc; |
| 628 | x.pData = (void*)Tcl_GetByteArrayFromObj(objv[3], &n); |
| 629 | x.nData = (int)n; |
| 630 | }else{ |
| 631 | x.pKey = (void*)Tcl_GetByteArrayFromObj(objv[2], &n); |
| 632 | x.nKey = (int)n; |
| 633 | } |
| 634 | pCur = (BtCursor*)sqlite3TestTextToPtr(Tcl_GetString(objv[1])); |
| 635 | |
| 636 | sqlite3_mutex_enter(pCur->pBtree->db->mutex); |
| 637 | sqlite3BtreeEnter(pCur->pBtree); |
| 638 | rc = sqlite3BtreeInsert(pCur, &x, 0, 0); |
| 639 | sqlite3BtreeLeave(pCur->pBtree); |
| 640 | sqlite3_mutex_leave(pCur->pBtree->db->mutex); |
| 641 | |
| 642 | Tcl_ResetResult(interp); |
| 643 | if( rc ){ |
| 644 | Tcl_AppendResult(interp, sqlite3ErrName(rc), NULL); |
| 645 | return TCL_ERROR; |
| 646 | } |
| 647 | return TCL_OK; |
| 648 | } |
| 649 | |
| 650 | |
| 651 | /* |
| 652 | ** Register commands with the TCL interpreter. |
| 653 | */ |
| 654 | int Sqlitetest3_Init(Tcl_Interp *interp){ |
| 655 | static struct { |
| 656 | char *zName; |
| 657 | Tcl_CmdProc *xProc; |
| 658 | } aCmd[] = { |
| 659 | { "btree_open", (Tcl_CmdProc*)btree_open }, |
| 660 | { "btree_close", (Tcl_CmdProc*)btree_close }, |
| 661 | { "btree_begin_transaction", (Tcl_CmdProc*)btree_begin_transaction }, |
| 662 | { "btree_pager_stats", (Tcl_CmdProc*)btree_pager_stats }, |
| 663 | { "btree_cursor", (Tcl_CmdProc*)btree_cursor }, |
| 664 | { "btree_close_cursor", (Tcl_CmdProc*)btree_close_cursor }, |
| 665 | { "btree_next", (Tcl_CmdProc*)btree_next }, |
| 666 | { "btree_eof", (Tcl_CmdProc*)btree_eof }, |
| 667 | { "btree_payload_size", (Tcl_CmdProc*)btree_payload_size }, |
| 668 | { "btree_first", (Tcl_CmdProc*)btree_first }, |
| 669 | { "btree_varint_test", (Tcl_CmdProc*)btree_varint_test }, |
| 670 | { "btree_from_db", (Tcl_CmdProc*)btree_from_db }, |
| 671 | { "btree_ismemdb", (Tcl_CmdProc*)btree_ismemdb }, |
| 672 | { "btree_set_cache_size", (Tcl_CmdProc*)btree_set_cache_size } |
| 673 | }; |
| 674 | int i; |
| 675 | |
| 676 | for(i=0; i<sizeof(aCmd)/sizeof(aCmd[0]); i++){ |
| 677 | Tcl_CreateCommand(interp, aCmd[i].zName, aCmd[i].xProc, 0, 0); |
| 678 | } |
| 679 | |
| 680 | Tcl_CreateObjCommand(interp, "btree_insert", btree_insert, 0, 0); |
| 681 | |
| 682 | return TCL_OK; |
| 683 | } |