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 ** Main file for the SQLite library. The routines in this file 13 ** implement the programmer interface to the library. Routines in 14 ** other files are for internal use by SQLite and should not be 15 ** accessed by users of the library. 16 ** 17 ** $Id: main.c,v 1.360 2006/12/19 18:57:11 drh Exp $ 18 */ 19 #include "sqliteInt.h" 20 #include "os.h" 21 #include <ctype.h> 22 23 /* 24 ** The following constant value is used by the SQLITE_BIGENDIAN and 25 ** SQLITE_LITTLEENDIAN macros. 26 */ 27 const int sqlite3one = 1; 28 29 /* 30 ** The version of the library 31 */ 32 const char sqlite3_version[] = SQLITE_VERSION; 33 const char *sqlite3_libversion(void){ return sqlite3_version; } 34 int sqlite3_libversion_number(void){ return SQLITE_VERSION_NUMBER; } 35 36 /* 37 ** This is the default collating function named "BINARY" which is always 38 ** available. 39 */ 40 static int binCollFunc( 41 void *NotUsed, 42 int nKey1, const void *pKey1, 43 int nKey2, const void *pKey2 44 ){ 45 int rc, n; 46 n = nKey1<nKey2 ? nKey1 : nKey2; 47 rc = memcmp(pKey1, pKey2, n); 48 if( rc==0 ){ 49 rc = nKey1 - nKey2; 50 } 51 return rc; 52 } 53 54 /* 55 ** Another built-in collating sequence: NOCASE. 56 ** 57 ** This collating sequence is intended to be used for "case independant 58 ** comparison". SQLite's knowledge of upper and lower case equivalents 59 ** extends only to the 26 characters used in the English language. 60 ** 61 ** At the moment there is only a UTF-8 implementation. 62 */ 63 static int nocaseCollatingFunc( 64 void *NotUsed, 65 int nKey1, const void *pKey1, 66 int nKey2, const void *pKey2 67 ){ 68 int r = sqlite3StrNICmp( 69 (const char *)pKey1, (const char *)pKey2, (nKey1<nKey2)?nKey1:nKey2); 70 if( 0==r ){ 71 r = nKey1-nKey2; 72 } 73 return r; 74 } 75 76 /* 77 ** Return the ROWID of the most recent insert 78 */ 79 sqlite_int64 sqlite3_last_insert_rowid(sqlite3 *db){ 80 return db->lastRowid; 81 } 82 83 /* 84 ** Return the number of changes in the most recent call to sqlite3_exec(). 85 */ 86 int sqlite3_changes(sqlite3 *db){ 87 return db->nChange; 88 } 89 90 /* 91 ** Return the number of changes since the database handle was opened. 92 */ 93 int sqlite3_total_changes(sqlite3 *db){ 94 return db->nTotalChange; 95 } 96 97 /* 98 ** Close an existing SQLite database 99 */ 100 int sqlite3_close(sqlite3 *db){ 101 HashElem *i; 102 int j; 103 104 if( !db ){ 105 return SQLITE_OK; 106 } 107 if( sqlite3SafetyCheck(db) ){ 108 return SQLITE_MISUSE; 109 } 110 111 #ifdef SQLITE_SSE 112 { 113 extern void sqlite3SseCleanup(sqlite3*); 114 sqlite3SseCleanup(db); 115 } 116 #endif 117 118 /* If there are any outstanding VMs, return SQLITE_BUSY. */ 119 sqlite3ResetInternalSchema(db, 0); 120 if( db->pVdbe ){ 121 sqlite3Error(db, SQLITE_BUSY, 122 "Unable to close due to unfinalised statements"); 123 return SQLITE_BUSY; 124 } 125 assert( !sqlite3SafetyCheck(db) ); 126 127 /* FIX ME: db->magic may be set to SQLITE_MAGIC_CLOSED if the database 128 ** cannot be opened for some reason. So this routine needs to run in 129 ** that case. But maybe there should be an extra magic value for the 130 ** "failed to open" state. 131 */ 132 if( db->magic!=SQLITE_MAGIC_CLOSED && sqlite3SafetyOn(db) ){ 133 /* printf("DID NOT CLOSE\n"); fflush(stdout); */ 134 return SQLITE_ERROR; 135 } 136 137 sqlite3VtabRollback(db); 138 139 for(j=0; j<db->nDb; j++){ 140 struct Db *pDb = &db->aDb[j]; 141 if( pDb->pBt ){ 142 sqlite3BtreeClose(pDb->pBt); 143 pDb->pBt = 0; 144 if( j!=1 ){ 145 pDb->pSchema = 0; 146 } 147 } 148 } 149 sqlite3ResetInternalSchema(db, 0); 150 assert( db->nDb<=2 ); 151 assert( db->aDb==db->aDbStatic ); 152 for(i=sqliteHashFirst(&db->aFunc); i; i=sqliteHashNext(i)){ 153 FuncDef *pFunc, *pNext; 154 for(pFunc = (FuncDef*)sqliteHashData(i); pFunc; pFunc=pNext){ 155 pNext = pFunc->pNext; 156 sqliteFree(pFunc); 157 } 158 } 159 160 for(i=sqliteHashFirst(&db->aCollSeq); i; i=sqliteHashNext(i)){ 161 CollSeq *pColl = (CollSeq *)sqliteHashData(i); 162 sqliteFree(pColl); 163 } 164 sqlite3HashClear(&db->aCollSeq); 165 #ifndef SQLITE_OMIT_VIRTUALTABLE 166 for(i=sqliteHashFirst(&db->aModule); i; i=sqliteHashNext(i)){ 167 Module *pMod = (Module *)sqliteHashData(i); 168 sqliteFree(pMod); 169 } 170 sqlite3HashClear(&db->aModule); 171 #endif 172 173 sqlite3HashClear(&db->aFunc); 174 sqlite3Error(db, SQLITE_OK, 0); /* Deallocates any cached error strings. */ 175 if( db->pErr ){ 176 sqlite3ValueFree(db->pErr); 177 } 178 sqlite3CloseExtensions(db); 179 180 db->magic = SQLITE_MAGIC_ERROR; 181 182 /* The temp-database schema is allocated differently from the other schema 183 ** objects (using sqliteMalloc() directly, instead of sqlite3BtreeSchema()). 184 ** So it needs to be freed here. Todo: Why not roll the temp schema into 185 ** the same sqliteMalloc() as the one that allocates the database 186 ** structure? 187 */ 188 sqliteFree(db->aDb[1].pSchema); 189 sqliteFree(db); 190 sqlite3ReleaseThreadData(); 191 return SQLITE_OK; 192 } 193 194 /* 195 ** Rollback all database files. 196 */ 197 void sqlite3RollbackAll(sqlite3 *db){ 198 int i; 199 int inTrans = 0; 200 for(i=0; i<db->nDb; i++){ 201 if( db->aDb[i].pBt ){ 202 if( sqlite3BtreeIsInTrans(db->aDb[i].pBt) ){ 203 inTrans = 1; 204 } 205 sqlite3BtreeRollback(db->aDb[i].pBt); 206 db->aDb[i].inTrans = 0; 207 } 208 } 209 sqlite3VtabRollback(db); 210 if( db->flags&SQLITE_InternChanges ){ 211 sqlite3ResetInternalSchema(db, 0); 212 } 213 214 /* If one has been configured, invoke the rollback-hook callback */ 215 if( db->xRollbackCallback && (inTrans || !db->autoCommit) ){ 216 db->xRollbackCallback(db->pRollbackArg); 217 } 218 } 219 220 /* 221 ** Return a static string that describes the kind of error specified in the 222 ** argument. 223 */ 224 const char *sqlite3ErrStr(int rc){ 225 const char *z; 226 switch( rc & 0xff ){ 227 case SQLITE_ROW: 228 case SQLITE_DONE: 229 case SQLITE_OK: z = "not an error"; break; 230 case SQLITE_ERROR: z = "SQL logic error or missing database"; break; 231 case SQLITE_PERM: z = "access permission denied"; break; 232 case SQLITE_ABORT: z = "callback requested query abort"; break; 233 case SQLITE_BUSY: z = "database is locked"; break; 234 case SQLITE_LOCKED: z = "database table is locked"; break; 235 case SQLITE_NOMEM: z = "out of memory"; break; 236 case SQLITE_READONLY: z = "attempt to write a readonly database"; break; 237 case SQLITE_INTERRUPT: z = "interrupted"; break; 238 case SQLITE_IOERR: z = "disk I/O error"; break; 239 case SQLITE_CORRUPT: z = "database disk image is malformed"; break; 240 case SQLITE_FULL: z = "database or disk is full"; break; 241 case SQLITE_CANTOPEN: z = "unable to open database file"; break; 242 case SQLITE_PROTOCOL: z = "database locking protocol failure"; break; 243 case SQLITE_EMPTY: z = "table contains no data"; break; 244 case SQLITE_SCHEMA: z = "database schema has changed"; break; 245 case SQLITE_CONSTRAINT: z = "constraint failed"; break; 246 case SQLITE_MISMATCH: z = "datatype mismatch"; break; 247 case SQLITE_MISUSE: z = "library routine called out of sequence";break; 248 case SQLITE_NOLFS: z = "kernel lacks large file support"; break; 249 case SQLITE_AUTH: z = "authorization denied"; break; 250 case SQLITE_FORMAT: z = "auxiliary database format error"; break; 251 case SQLITE_RANGE: z = "bind or column index out of range"; break; 252 case SQLITE_NOTADB: z = "file is encrypted or is not a database";break; 253 default: z = "unknown error"; break; 254 } 255 return z; 256 } 257 258 /* 259 ** This routine implements a busy callback that sleeps and tries 260 ** again until a timeout value is reached. The timeout value is 261 ** an integer number of milliseconds passed in as the first 262 ** argument. 263 */ 264 static int sqliteDefaultBusyCallback( 265 void *ptr, /* Database connection */ 266 int count /* Number of times table has been busy */ 267 ){ 268 #if OS_WIN || (defined(HAVE_USLEEP) && HAVE_USLEEP) 269 static const u8 delays[] = 270 { 1, 2, 5, 10, 15, 20, 25, 25, 25, 50, 50, 100 }; 271 static const u8 totals[] = 272 { 0, 1, 3, 8, 18, 33, 53, 78, 103, 128, 178, 228 }; 273 # define NDELAY (sizeof(delays)/sizeof(delays[0])) 274 int timeout = ((sqlite3 *)ptr)->busyTimeout; 275 int delay, prior; 276 277 assert( count>=0 ); 278 if( count < NDELAY ){ 279 delay = delays[count]; 280 prior = totals[count]; 281 }else{ 282 delay = delays[NDELAY-1]; 283 prior = totals[NDELAY-1] + delay*(count-(NDELAY-1)); 284 } 285 if( prior + delay > timeout ){ 286 delay = timeout - prior; 287 if( delay<=0 ) return 0; 288 } 289 sqlite3OsSleep(delay); 290 return 1; 291 #else 292 int timeout = ((sqlite3 *)ptr)->busyTimeout; 293 if( (count+1)*1000 > timeout ){ 294 return 0; 295 } 296 sqlite3OsSleep(1000); 297 return 1; 298 #endif 299 } 300 301 /* 302 ** Invoke the given busy handler. 303 ** 304 ** This routine is called when an operation failed with a lock. 305 ** If this routine returns non-zero, the lock is retried. If it 306 ** returns 0, the operation aborts with an SQLITE_BUSY error. 307 */ 308 int sqlite3InvokeBusyHandler(BusyHandler *p){ 309 int rc; 310 if( p==0 || p->xFunc==0 || p->nBusy<0 ) return 0; 311 rc = p->xFunc(p->pArg, p->nBusy); 312 if( rc==0 ){ 313 p->nBusy = -1; 314 }else{ 315 p->nBusy++; 316 } 317 return rc; 318 } 319 320 /* 321 ** This routine sets the busy callback for an Sqlite database to the 322 ** given callback function with the given argument. 323 */ 324 int sqlite3_busy_handler( 325 sqlite3 *db, 326 int (*xBusy)(void*,int), 327 void *pArg 328 ){ 329 if( sqlite3SafetyCheck(db) ){ 330 return SQLITE_MISUSE; 331 } 332 db->busyHandler.xFunc = xBusy; 333 db->busyHandler.pArg = pArg; 334 db->busyHandler.nBusy = 0; 335 return SQLITE_OK; 336 } 337 338 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK 339 /* 340 ** This routine sets the progress callback for an Sqlite database to the 341 ** given callback function with the given argument. The progress callback will 342 ** be invoked every nOps opcodes. 343 */ 344 void sqlite3_progress_handler( 345 sqlite3 *db, 346 int nOps, 347 int (*xProgress)(void*), 348 void *pArg 349 ){ 350 if( !sqlite3SafetyCheck(db) ){ 351 if( nOps>0 ){ 352 db->xProgress = xProgress; 353 db->nProgressOps = nOps; 354 db->pProgressArg = pArg; 355 }else{ 356 db->xProgress = 0; 357 db->nProgressOps = 0; 358 db->pProgressArg = 0; 359 } 360 } 361 } 362 #endif 363 364 365 /* 366 ** This routine installs a default busy handler that waits for the 367 ** specified number of milliseconds before returning 0. 368 */ 369 int sqlite3_busy_timeout(sqlite3 *db, int ms){ 370 if( sqlite3SafetyCheck(db) ){ 371 return SQLITE_MISUSE; 372 } 373 if( ms>0 ){ 374 db->busyTimeout = ms; 375 sqlite3_busy_handler(db, sqliteDefaultBusyCallback, (void*)db); 376 }else{ 377 sqlite3_busy_handler(db, 0, 0); 378 } 379 return SQLITE_OK; 380 } 381 382 /* 383 ** Cause any pending operation to stop at its earliest opportunity. 384 */ 385 void sqlite3_interrupt(sqlite3 *db){ 386 if( db && (db->magic==SQLITE_MAGIC_OPEN || db->magic==SQLITE_MAGIC_BUSY) ){ 387 db->u1.isInterrupted = 1; 388 } 389 } 390 391 /* 392 ** Memory allocation routines that use SQLites internal memory 393 ** memory allocator. Depending on how SQLite is compiled, the 394 ** internal memory allocator might be just an alias for the 395 ** system default malloc/realloc/free. Or the built-in allocator 396 ** might do extra stuff like put sentinals around buffers to 397 ** check for overruns or look for memory leaks. 398 ** 399 ** Use sqlite3_free() to free memory returned by sqlite3_mprintf(). 400 */ 401 void sqlite3_free(void *p){ if( p ) sqlite3OsFree(p); } 402 void *sqlite3_malloc(int nByte){ return nByte>0 ? sqlite3OsMalloc(nByte) : 0; } 403 void *sqlite3_realloc(void *pOld, int nByte){ 404 if( pOld ){ 405 if( nByte>0 ){ 406 return sqlite3OsRealloc(pOld, nByte); 407 }else{ 408 sqlite3OsFree(pOld); 409 return 0; 410 } 411 }else{ 412 return sqlite3_malloc(nByte); 413 } 414 } 415 416 /* 417 ** This function is exactly the same as sqlite3_create_function(), except 418 ** that it is designed to be called by internal code. The difference is 419 ** that if a malloc() fails in sqlite3_create_function(), an error code 420 ** is returned and the mallocFailed flag cleared. 421 */ 422 int sqlite3CreateFunc( 423 sqlite3 *db, 424 const char *zFunctionName, 425 int nArg, 426 int enc, 427 void *pUserData, 428 void (*xFunc)(sqlite3_context*,int,sqlite3_value **), 429 void (*xStep)(sqlite3_context*,int,sqlite3_value **), 430 void (*xFinal)(sqlite3_context*) 431 ){ 432 FuncDef *p; 433 int nName; 434 435 if( sqlite3SafetyCheck(db) ){ 436 return SQLITE_MISUSE; 437 } 438 if( zFunctionName==0 || 439 (xFunc && (xFinal || xStep)) || 440 (!xFunc && (xFinal && !xStep)) || 441 (!xFunc && (!xFinal && xStep)) || 442 (nArg<-1 || nArg>127) || 443 (255<(nName = strlen(zFunctionName))) ){ 444 sqlite3Error(db, SQLITE_ERROR, "bad parameters"); 445 return SQLITE_ERROR; 446 } 447 448 #ifndef SQLITE_OMIT_UTF16 449 /* If SQLITE_UTF16 is specified as the encoding type, transform this 450 ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the 451 ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally. 452 ** 453 ** If SQLITE_ANY is specified, add three versions of the function 454 ** to the hash table. 455 */ 456 if( enc==SQLITE_UTF16 ){ 457 enc = SQLITE_UTF16NATIVE; 458 }else if( enc==SQLITE_ANY ){ 459 int rc; 460 rc = sqlite3CreateFunc(db, zFunctionName, nArg, SQLITE_UTF8, 461 pUserData, xFunc, xStep, xFinal); 462 if( rc!=SQLITE_OK ) return rc; 463 rc = sqlite3CreateFunc(db, zFunctionName, nArg, SQLITE_UTF16LE, 464 pUserData, xFunc, xStep, xFinal); 465 if( rc!=SQLITE_OK ) return rc; 466 enc = SQLITE_UTF16BE; 467 } 468 #else 469 enc = SQLITE_UTF8; 470 #endif 471 472 /* Check if an existing function is being overridden or deleted. If so, 473 ** and there are active VMs, then return SQLITE_BUSY. If a function 474 ** is being overridden/deleted but there are no active VMs, allow the 475 ** operation to continue but invalidate all precompiled statements. 476 */ 477 p = sqlite3FindFunction(db, zFunctionName, nName, nArg, enc, 0); 478 if( p && p->iPrefEnc==enc && p->nArg==nArg ){ 479 if( db->activeVdbeCnt ){ 480 sqlite3Error(db, SQLITE_BUSY, 481 "Unable to delete/modify user-function due to active statements"); 482 assert( !sqlite3MallocFailed() ); 483 return SQLITE_BUSY; 484 }else{ 485 sqlite3ExpirePreparedStatements(db); 486 } 487 } 488 489 p = sqlite3FindFunction(db, zFunctionName, nName, nArg, enc, 1); 490 if( p ){ 491 p->flags = 0; 492 p->xFunc = xFunc; 493 p->xStep = xStep; 494 p->xFinalize = xFinal; 495 p->pUserData = pUserData; 496 p->nArg = nArg; 497 } 498 return SQLITE_OK; 499 } 500 501 /* 502 ** Create new user functions. 503 */ 504 int sqlite3_create_function( 505 sqlite3 *db, 506 const char *zFunctionName, 507 int nArg, 508 int enc, 509 void *p, 510 void (*xFunc)(sqlite3_context*,int,sqlite3_value **), 511 void (*xStep)(sqlite3_context*,int,sqlite3_value **), 512 void (*xFinal)(sqlite3_context*) 513 ){ 514 int rc; 515 assert( !sqlite3MallocFailed() ); 516 rc = sqlite3CreateFunc(db, zFunctionName, nArg, enc, p, xFunc, xStep, xFinal); 517 518 return sqlite3ApiExit(db, rc); 519 } 520 521 #ifndef SQLITE_OMIT_UTF16 522 int sqlite3_create_function16( 523 sqlite3 *db, 524 const void *zFunctionName, 525 int nArg, 526 int eTextRep, 527 void *p, 528 void (*xFunc)(sqlite3_context*,int,sqlite3_value**), 529 void (*xStep)(sqlite3_context*,int,sqlite3_value**), 530 void (*xFinal)(sqlite3_context*) 531 ){ 532 int rc; 533 char *zFunc8; 534 assert( !sqlite3MallocFailed() ); 535 536 zFunc8 = sqlite3utf16to8(zFunctionName, -1); 537 rc = sqlite3CreateFunc(db, zFunc8, nArg, eTextRep, p, xFunc, xStep, xFinal); 538 sqliteFree(zFunc8); 539 540 return sqlite3ApiExit(db, rc); 541 } 542 #endif 543 544 545 /* 546 ** Declare that a function has been overloaded by a virtual table. 547 ** 548 ** If the function already exists as a regular global function, then 549 ** this routine is a no-op. If the function does not exist, then create 550 ** a new one that always throws a run-time error. 551 ** 552 ** When virtual tables intend to provide an overloaded function, they 553 ** should call this routine to make sure the global function exists. 554 ** A global function must exist in order for name resolution to work 555 ** properly. 556 */ 557 int sqlite3_overload_function( 558 sqlite3 *db, 559 const char *zName, 560 int nArg 561 ){ 562 int nName = strlen(zName); 563 if( sqlite3FindFunction(db, zName, nName, nArg, SQLITE_UTF8, 0)==0 ){ 564 sqlite3CreateFunc(db, zName, nArg, SQLITE_UTF8, 565 0, sqlite3InvalidFunction, 0, 0); 566 } 567 return sqlite3ApiExit(db, SQLITE_OK); 568 } 569 570 #ifndef SQLITE_OMIT_TRACE 571 /* 572 ** Register a trace function. The pArg from the previously registered trace 573 ** is returned. 574 ** 575 ** A NULL trace function means that no tracing is executes. A non-NULL 576 ** trace is a pointer to a function that is invoked at the start of each 577 ** SQL statement. 578 */ 579 void *sqlite3_trace(sqlite3 *db, void (*xTrace)(void*,const char*), void *pArg){ 580 void *pOld = db->pTraceArg; 581 db->xTrace = xTrace; 582 db->pTraceArg = pArg; 583 return pOld; 584 } 585 /* 586 ** Register a profile function. The pArg from the previously registered 587 ** profile function is returned. 588 ** 589 ** A NULL profile function means that no profiling is executes. A non-NULL 590 ** profile is a pointer to a function that is invoked at the conclusion of 591 ** each SQL statement that is run. 592 */ 593 void *sqlite3_profile( 594 sqlite3 *db, 595 void (*xProfile)(void*,const char*,sqlite_uint64), 596 void *pArg 597 ){ 598 void *pOld = db->pProfileArg; 599 db->xProfile = xProfile; 600 db->pProfileArg = pArg; 601 return pOld; 602 } 603 #endif /* SQLITE_OMIT_TRACE */ 604 605 /*** EXPERIMENTAL *** 606 ** 607 ** Register a function to be invoked when a transaction comments. 608 ** If the invoked function returns non-zero, then the commit becomes a 609 ** rollback. 610 */ 611 void *sqlite3_commit_hook( 612 sqlite3 *db, /* Attach the hook to this database */ 613 int (*xCallback)(void*), /* Function to invoke on each commit */ 614 void *pArg /* Argument to the function */ 615 ){ 616 void *pOld = db->pCommitArg; 617 db->xCommitCallback = xCallback; 618 db->pCommitArg = pArg; 619 return pOld; 620 } 621 622 /* 623 ** Register a callback to be invoked each time a row is updated, 624 ** inserted or deleted using this database connection. 625 */ 626 void *sqlite3_update_hook( 627 sqlite3 *db, /* Attach the hook to this database */ 628 void (*xCallback)(void*,int,char const *,char const *,sqlite_int64), 629 void *pArg /* Argument to the function */ 630 ){ 631 void *pRet = db->pUpdateArg; 632 db->xUpdateCallback = xCallback; 633 db->pUpdateArg = pArg; 634 return pRet; 635 } 636 637 /* 638 ** Register a callback to be invoked each time a transaction is rolled 639 ** back by this database connection. 640 */ 641 void *sqlite3_rollback_hook( 642 sqlite3 *db, /* Attach the hook to this database */ 643 void (*xCallback)(void*), /* Callback function */ 644 void *pArg /* Argument to the function */ 645 ){ 646 void *pRet = db->pRollbackArg; 647 db->xRollbackCallback = xCallback; 648 db->pRollbackArg = pArg; 649 return pRet; 650 } 651 652 /* 653 ** This routine is called to create a connection to a database BTree 654 ** driver. If zFilename is the name of a file, then that file is 655 ** opened and used. If zFilename is the magic name ":memory:" then 656 ** the database is stored in memory (and is thus forgotten as soon as 657 ** the connection is closed.) If zFilename is NULL then the database 658 ** is a "virtual" database for transient use only and is deleted as 659 ** soon as the connection is closed. 660 ** 661 ** A virtual database can be either a disk file (that is automatically 662 ** deleted when the file is closed) or it an be held entirely in memory, 663 ** depending on the values of the TEMP_STORE compile-time macro and the 664 ** db->temp_store variable, according to the following chart: 665 ** 666 ** TEMP_STORE db->temp_store Location of temporary database 667 ** ---------- -------------- ------------------------------ 668 ** 0 any file 669 ** 1 1 file 670 ** 1 2 memory 671 ** 1 0 file 672 ** 2 1 file 673 ** 2 2 memory 674 ** 2 0 memory 675 ** 3 any memory 676 */ 677 int sqlite3BtreeFactory( 678 const sqlite3 *db, /* Main database when opening aux otherwise 0 */ 679 const char *zFilename, /* Name of the file containing the BTree database */ 680 int omitJournal, /* if TRUE then do not journal this file */ 681 int nCache, /* How many pages in the page cache */ 682 Btree **ppBtree /* Pointer to new Btree object written here */ 683 ){ 684 int btree_flags = 0; 685 int rc; 686 687 assert( ppBtree != 0); 688 if( omitJournal ){ 689 btree_flags |= BTREE_OMIT_JOURNAL; 690 } 691 if( db->flags & SQLITE_NoReadlock ){ 692 btree_flags |= BTREE_NO_READLOCK; 693 } 694 if( zFilename==0 ){ 695 #if TEMP_STORE==0 696 /* Do nothing */ 697 #endif 698 #ifndef SQLITE_OMIT_MEMORYDB 699 #if TEMP_STORE==1 700 if( db->temp_store==2 ) zFilename = ":memory:"; 701 #endif 702 #if TEMP_STORE==2 703 if( db->temp_store!=1 ) zFilename = ":memory:"; 704 #endif 705 #if TEMP_STORE==3 706 zFilename = ":memory:"; 707 #endif 708 #endif /* SQLITE_OMIT_MEMORYDB */ 709 } 710 711 rc = sqlite3BtreeOpen(zFilename, (sqlite3 *)db, ppBtree, btree_flags); 712 if( rc==SQLITE_OK ){ 713 sqlite3BtreeSetBusyHandler(*ppBtree, (void*)&db->busyHandler); 714 sqlite3BtreeSetCacheSize(*ppBtree, nCache); 715 } 716 return rc; 717 } 718 719 /* 720 ** Return UTF-8 encoded English language explanation of the most recent 721 ** error. 722 */ 723 const char *sqlite3_errmsg(sqlite3 *db){ 724 const char *z; 725 if( !db || sqlite3MallocFailed() ){ 726 return sqlite3ErrStr(SQLITE_NOMEM); 727 } 728 if( sqlite3SafetyCheck(db) || db->errCode==SQLITE_MISUSE ){ 729 return sqlite3ErrStr(SQLITE_MISUSE); 730 } 731 z = (char*)sqlite3_value_text(db->pErr); 732 if( z==0 ){ 733 z = sqlite3ErrStr(db->errCode); 734 } 735 return z; 736 } 737 738 #ifndef SQLITE_OMIT_UTF16 739 /* 740 ** Return UTF-16 encoded English language explanation of the most recent 741 ** error. 742 */ 743 const void *sqlite3_errmsg16(sqlite3 *db){ 744 /* Because all the characters in the string are in the unicode 745 ** range 0x00-0xFF, if we pad the big-endian string with a 746 ** zero byte, we can obtain the little-endian string with 747 ** &big_endian[1]. 748 */ 749 static const char outOfMemBe[] = { 750 0, 'o', 0, 'u', 0, 't', 0, ' ', 751 0, 'o', 0, 'f', 0, ' ', 752 0, 'm', 0, 'e', 0, 'm', 0, 'o', 0, 'r', 0, 'y', 0, 0, 0 753 }; 754 static const char misuseBe [] = { 755 0, 'l', 0, 'i', 0, 'b', 0, 'r', 0, 'a', 0, 'r', 0, 'y', 0, ' ', 756 0, 'r', 0, 'o', 0, 'u', 0, 't', 0, 'i', 0, 'n', 0, 'e', 0, ' ', 757 0, 'c', 0, 'a', 0, 'l', 0, 'l', 0, 'e', 0, 'd', 0, ' ', 758 0, 'o', 0, 'u', 0, 't', 0, ' ', 759 0, 'o', 0, 'f', 0, ' ', 760 0, 's', 0, 'e', 0, 'q', 0, 'u', 0, 'e', 0, 'n', 0, 'c', 0, 'e', 0, 0, 0 761 }; 762 763 const void *z; 764 if( sqlite3MallocFailed() ){ 765 return (void *)(&outOfMemBe[SQLITE_UTF16NATIVE==SQLITE_UTF16LE?1:0]); 766 } 767 if( sqlite3SafetyCheck(db) || db->errCode==SQLITE_MISUSE ){ 768 return (void *)(&misuseBe[SQLITE_UTF16NATIVE==SQLITE_UTF16LE?1:0]); 769 } 770 z = sqlite3_value_text16(db->pErr); 771 if( z==0 ){ 772 sqlite3ValueSetStr(db->pErr, -1, sqlite3ErrStr(db->errCode), 773 SQLITE_UTF8, SQLITE_STATIC); 774 z = sqlite3_value_text16(db->pErr); 775 } 776 sqlite3ApiExit(0, 0); 777 return z; 778 } 779 #endif /* SQLITE_OMIT_UTF16 */ 780 781 /* 782 ** Return the most recent error code generated by an SQLite routine. If NULL is 783 ** passed to this function, we assume a malloc() failed during sqlite3_open(). 784 */ 785 int sqlite3_errcode(sqlite3 *db){ 786 if( !db || sqlite3MallocFailed() ){ 787 return SQLITE_NOMEM; 788 } 789 if( sqlite3SafetyCheck(db) ){ 790 return SQLITE_MISUSE; 791 } 792 return db->errCode & db->errMask; 793 } 794 795 /* 796 ** Create a new collating function for database "db". The name is zName 797 ** and the encoding is enc. 798 */ 799 static int createCollation( 800 sqlite3* db, 801 const char *zName, 802 int enc, 803 void* pCtx, 804 int(*xCompare)(void*,int,const void*,int,const void*) 805 ){ 806 CollSeq *pColl; 807 int enc2; 808 809 if( sqlite3SafetyCheck(db) ){ 810 return SQLITE_MISUSE; 811 } 812 813 /* If SQLITE_UTF16 is specified as the encoding type, transform this 814 ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the 815 ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally. 816 */ 817 enc2 = enc & ~SQLITE_UTF16_ALIGNED; 818 if( enc2==SQLITE_UTF16 ){ 819 enc2 = SQLITE_UTF16NATIVE; 820 } 821 822 if( (enc2&~3)!=0 ){ 823 sqlite3Error(db, SQLITE_ERROR, "unknown encoding"); 824 return SQLITE_ERROR; 825 } 826 827 /* Check if this call is removing or replacing an existing collation 828 ** sequence. If so, and there are active VMs, return busy. If there 829 ** are no active VMs, invalidate any pre-compiled statements. 830 */ 831 pColl = sqlite3FindCollSeq(db, (u8)enc2, zName, strlen(zName), 0); 832 if( pColl && pColl->xCmp ){ 833 if( db->activeVdbeCnt ){ 834 sqlite3Error(db, SQLITE_BUSY, 835 "Unable to delete/modify collation sequence due to active statements"); 836 return SQLITE_BUSY; 837 } 838 sqlite3ExpirePreparedStatements(db); 839 } 840 841 pColl = sqlite3FindCollSeq(db, (u8)enc2, zName, strlen(zName), 1); 842 if( pColl ){ 843 pColl->xCmp = xCompare; 844 pColl->pUser = pCtx; 845 pColl->enc = enc2 | (enc & SQLITE_UTF16_ALIGNED); 846 } 847 sqlite3Error(db, SQLITE_OK, 0); 848 return SQLITE_OK; 849 } 850 851 852 /* 853 ** This routine does the work of opening a database on behalf of 854 ** sqlite3_open() and sqlite3_open16(). The database filename "zFilename" 855 ** is UTF-8 encoded. 856 */ 857 static int openDatabase( 858 const char *zFilename, /* Database filename UTF-8 encoded */ 859 sqlite3 **ppDb /* OUT: Returned database handle */ 860 ){ 861 sqlite3 *db; 862 int rc; 863 CollSeq *pColl; 864 865 assert( !sqlite3MallocFailed() ); 866 867 /* Allocate the sqlite data structure */ 868 db = sqliteMalloc( sizeof(sqlite3) ); 869 if( db==0 ) goto opendb_out; 870 db->errMask = 0xff; 871 db->priorNewRowid = 0; 872 db->magic = SQLITE_MAGIC_BUSY; 873 db->nDb = 2; 874 db->aDb = db->aDbStatic; 875 db->autoCommit = 1; 876 db->flags |= SQLITE_ShortColNames 877 #if SQLITE_DEFAULT_FILE_FORMAT<4 878 | SQLITE_LegacyFileFmt 879 #endif 880 ; 881 sqlite3HashInit(&db->aFunc, SQLITE_HASH_STRING, 0); 882 sqlite3HashInit(&db->aCollSeq, SQLITE_HASH_STRING, 0); 883 #ifndef SQLITE_OMIT_VIRTUALTABLE 884 sqlite3HashInit(&db->aModule, SQLITE_HASH_STRING, 0); 885 #endif 886 887 /* Add the default collation sequence BINARY. BINARY works for both UTF-8 888 ** and UTF-16, so add a version for each to avoid any unnecessary 889 ** conversions. The only error that can occur here is a malloc() failure. 890 */ 891 if( createCollation(db, "BINARY", SQLITE_UTF8, 0, binCollFunc) || 892 createCollation(db, "BINARY", SQLITE_UTF16BE, 0, binCollFunc) || 893 createCollation(db, "BINARY", SQLITE_UTF16LE, 0, binCollFunc) || 894 (db->pDfltColl = sqlite3FindCollSeq(db, SQLITE_UTF8, "BINARY", 6, 0))==0 895 ){ 896 assert( sqlite3MallocFailed() ); 897 db->magic = SQLITE_MAGIC_CLOSED; 898 goto opendb_out; 899 } 900 901 /* Also add a UTF-8 case-insensitive collation sequence. */ 902 createCollation(db, "NOCASE", SQLITE_UTF8, 0, nocaseCollatingFunc); 903 904 /* Set flags on the built-in collating sequences */ 905 db->pDfltColl->type = SQLITE_COLL_BINARY; 906 pColl = sqlite3FindCollSeq(db, SQLITE_UTF8, "NOCASE", 6, 0); 907 if( pColl ){ 908 pColl->type = SQLITE_COLL_NOCASE; 909 } 910 911 /* Open the backend database driver */ 912 rc = sqlite3BtreeFactory(db, zFilename, 0, MAX_PAGES, &db->aDb[0].pBt); 913 if( rc!=SQLITE_OK ){ 914 sqlite3Error(db, rc, 0); 915 db->magic = SQLITE_MAGIC_CLOSED; 916 goto opendb_out; 917 } 918 db->aDb[0].pSchema = sqlite3SchemaGet(db->aDb[0].pBt); 919 db->aDb[1].pSchema = sqlite3SchemaGet(0); 920 921 922 /* The default safety_level for the main database is 'full'; for the temp 923 ** database it is 'NONE'. This matches the pager layer defaults. 924 */ 925 db->aDb[0].zName = "main"; 926 db->aDb[0].safety_level = 3; 927 #ifndef SQLITE_OMIT_TEMPDB 928 db->aDb[1].zName = "temp"; 929 db->aDb[1].safety_level = 1; 930 #endif 931 932 /* Register all built-in functions, but do not attempt to read the 933 ** database schema yet. This is delayed until the first time the database 934 ** is accessed. 935 */ 936 if( !sqlite3MallocFailed() ){ 937 sqlite3Error(db, SQLITE_OK, 0); 938 sqlite3RegisterBuiltinFunctions(db); 939 } 940 db->magic = SQLITE_MAGIC_OPEN; 941 942 /* Load automatic extensions - extensions that have been registered 943 ** using the sqlite3_automatic_extension() API. 944 */ 945 (void)sqlite3AutoLoadExtensions(db); 946 947 #ifdef SQLITE_ENABLE_FTS1 948 { 949 extern int sqlite3Fts1Init(sqlite3*); 950 sqlite3Fts1Init(db); 951 } 952 #endif 953 954 #ifdef SQLITE_ENABLE_FTS2 955 { 956 extern int sqlite3Fts2Init(sqlite3*); 957 sqlite3Fts2Init(db); 958 } 959 #endif 960 961 opendb_out: 962 if( SQLITE_NOMEM==(rc = sqlite3_errcode(db)) ){ 963 sqlite3_close(db); 964 db = 0; 965 } 966 *ppDb = db; 967 return sqlite3ApiExit(0, rc); 968 } 969 970 /* 971 ** Open a new database handle. 972 */ 973 int sqlite3_open( 974 const char *zFilename, 975 sqlite3 **ppDb 976 ){ 977 return openDatabase(zFilename, ppDb); 978 } 979 980 #ifndef SQLITE_OMIT_UTF16 981 /* 982 ** Open a new database handle. 983 */ 984 int sqlite3_open16( 985 const void *zFilename, 986 sqlite3 **ppDb 987 ){ 988 char const *zFilename8; /* zFilename encoded in UTF-8 instead of UTF-16 */ 989 int rc = SQLITE_OK; 990 sqlite3_value *pVal; 991 992 assert( zFilename ); 993 assert( ppDb ); 994 *ppDb = 0; 995 pVal = sqlite3ValueNew(); 996 sqlite3ValueSetStr(pVal, -1, zFilename, SQLITE_UTF16NATIVE, SQLITE_STATIC); 997 zFilename8 = sqlite3ValueText(pVal, SQLITE_UTF8); 998 if( zFilename8 ){ 999 rc = openDatabase(zFilename8, ppDb); 1000 if( rc==SQLITE_OK && *ppDb ){ 1001 rc = sqlite3_exec(*ppDb, "PRAGMA encoding = 'UTF-16'", 0, 0, 0); 1002 if( rc!=SQLITE_OK ){ 1003 sqlite3_close(*ppDb); 1004 *ppDb = 0; 1005 } 1006 } 1007 } 1008 sqlite3ValueFree(pVal); 1009 1010 return sqlite3ApiExit(0, rc); 1011 } 1012 #endif /* SQLITE_OMIT_UTF16 */ 1013 1014 /* 1015 ** The following routine destroys a virtual machine that is created by 1016 ** the sqlite3_compile() routine. The integer returned is an SQLITE_ 1017 ** success/failure code that describes the result of executing the virtual 1018 ** machine. 1019 ** 1020 ** This routine sets the error code and string returned by 1021 ** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16(). 1022 */ 1023 int sqlite3_finalize(sqlite3_stmt *pStmt){ 1024 int rc; 1025 if( pStmt==0 ){ 1026 rc = SQLITE_OK; 1027 }else{ 1028 rc = sqlite3VdbeFinalize((Vdbe*)pStmt); 1029 } 1030 return rc; 1031 } 1032 1033 /* 1034 ** Terminate the current execution of an SQL statement and reset it 1035 ** back to its starting state so that it can be reused. A success code from 1036 ** the prior execution is returned. 1037 ** 1038 ** This routine sets the error code and string returned by 1039 ** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16(). 1040 */ 1041 int sqlite3_reset(sqlite3_stmt *pStmt){ 1042 int rc; 1043 if( pStmt==0 ){ 1044 rc = SQLITE_OK; 1045 }else{ 1046 rc = sqlite3VdbeReset((Vdbe*)pStmt); 1047 sqlite3VdbeMakeReady((Vdbe*)pStmt, -1, 0, 0, 0); 1048 assert( (rc & (sqlite3_db_handle(pStmt)->errMask))==rc ); 1049 } 1050 return rc; 1051 } 1052 1053 /* 1054 ** Register a new collation sequence with the database handle db. 1055 */ 1056 int sqlite3_create_collation( 1057 sqlite3* db, 1058 const char *zName, 1059 int enc, 1060 void* pCtx, 1061 int(*xCompare)(void*,int,const void*,int,const void*) 1062 ){ 1063 int rc; 1064 assert( !sqlite3MallocFailed() ); 1065 rc = createCollation(db, zName, enc, pCtx, xCompare); 1066 return sqlite3ApiExit(db, rc); 1067 } 1068 1069 #ifndef SQLITE_OMIT_UTF16 1070 /* 1071 ** Register a new collation sequence with the database handle db. 1072 */ 1073 int sqlite3_create_collation16( 1074 sqlite3* db, 1075 const char *zName, 1076 int enc, 1077 void* pCtx, 1078 int(*xCompare)(void*,int,const void*,int,const void*) 1079 ){ 1080 int rc = SQLITE_OK; 1081 char *zName8; 1082 assert( !sqlite3MallocFailed() ); 1083 zName8 = sqlite3utf16to8(zName, -1); 1084 if( zName8 ){ 1085 rc = createCollation(db, zName8, enc, pCtx, xCompare); 1086 sqliteFree(zName8); 1087 } 1088 return sqlite3ApiExit(db, rc); 1089 } 1090 #endif /* SQLITE_OMIT_UTF16 */ 1091 1092 /* 1093 ** Register a collation sequence factory callback with the database handle 1094 ** db. Replace any previously installed collation sequence factory. 1095 */ 1096 int sqlite3_collation_needed( 1097 sqlite3 *db, 1098 void *pCollNeededArg, 1099 void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*) 1100 ){ 1101 if( sqlite3SafetyCheck(db) ){ 1102 return SQLITE_MISUSE; 1103 } 1104 db->xCollNeeded = xCollNeeded; 1105 db->xCollNeeded16 = 0; 1106 db->pCollNeededArg = pCollNeededArg; 1107 return SQLITE_OK; 1108 } 1109 1110 #ifndef SQLITE_OMIT_UTF16 1111 /* 1112 ** Register a collation sequence factory callback with the database handle 1113 ** db. Replace any previously installed collation sequence factory. 1114 */ 1115 int sqlite3_collation_needed16( 1116 sqlite3 *db, 1117 void *pCollNeededArg, 1118 void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*) 1119 ){ 1120 if( sqlite3SafetyCheck(db) ){ 1121 return SQLITE_MISUSE; 1122 } 1123 db->xCollNeeded = 0; 1124 db->xCollNeeded16 = xCollNeeded16; 1125 db->pCollNeededArg = pCollNeededArg; 1126 return SQLITE_OK; 1127 } 1128 #endif /* SQLITE_OMIT_UTF16 */ 1129 1130 #ifndef SQLITE_OMIT_GLOBALRECOVER 1131 /* 1132 ** This function is now an anachronism. It used to be used to recover from a 1133 ** malloc() failure, but SQLite now does this automatically. 1134 */ 1135 int sqlite3_global_recover(){ 1136 return SQLITE_OK; 1137 } 1138 #endif 1139 1140 /* 1141 ** Test to see whether or not the database connection is in autocommit 1142 ** mode. Return TRUE if it is and FALSE if not. Autocommit mode is on 1143 ** by default. Autocommit is disabled by a BEGIN statement and reenabled 1144 ** by the next COMMIT or ROLLBACK. 1145 ** 1146 ******* THIS IS AN EXPERIMENTAL API AND IS SUBJECT TO CHANGE ****** 1147 */ 1148 int sqlite3_get_autocommit(sqlite3 *db){ 1149 return db->autoCommit; 1150 } 1151 1152 #ifdef SQLITE_DEBUG 1153 /* 1154 ** The following routine is subtituted for constant SQLITE_CORRUPT in 1155 ** debugging builds. This provides a way to set a breakpoint for when 1156 ** corruption is first detected. 1157 */ 1158 int sqlite3Corrupt(void){ 1159 return SQLITE_CORRUPT; 1160 } 1161 #endif 1162 1163 1164 #ifndef SQLITE_OMIT_SHARED_CACHE 1165 /* 1166 ** Enable or disable the shared pager and schema features for the 1167 ** current thread. 1168 ** 1169 ** This routine should only be called when there are no open 1170 ** database connections. 1171 */ 1172 int sqlite3_enable_shared_cache(int enable){ 1173 ThreadData *pTd = sqlite3ThreadData(); 1174 if( pTd ){ 1175 /* It is only legal to call sqlite3_enable_shared_cache() when there 1176 ** are no currently open b-trees that were opened by the calling thread. 1177 ** This condition is only easy to detect if the shared-cache were 1178 ** previously enabled (and is being disabled). 1179 */ 1180 if( pTd->pBtree && !enable ){ 1181 assert( pTd->useSharedData ); 1182 return SQLITE_MISUSE; 1183 } 1184 1185 pTd->useSharedData = enable; 1186 sqlite3ReleaseThreadData(); 1187 } 1188 return sqlite3ApiExit(0, SQLITE_OK); 1189 } 1190 #endif 1191 1192 /* 1193 ** This is a convenience routine that makes sure that all thread-specific 1194 ** data for this thread has been deallocated. 1195 */ 1196 void sqlite3_thread_cleanup(void){ 1197 ThreadData *pTd = sqlite3OsThreadSpecificData(0); 1198 if( pTd ){ 1199 memset(pTd, 0, sizeof(*pTd)); 1200 sqlite3OsThreadSpecificData(-1); 1201 } 1202 } 1203 1204 /* 1205 ** Return meta information about a specific column of a database table. 1206 ** See comment in sqlite3.h (sqlite.h.in) for details. 1207 */ 1208 #ifdef SQLITE_ENABLE_COLUMN_METADATA 1209 int sqlite3_table_column_metadata( 1210 sqlite3 *db, /* Connection handle */ 1211 const char *zDbName, /* Database name or NULL */ 1212 const char *zTableName, /* Table name */ 1213 const char *zColumnName, /* Column name */ 1214 char const **pzDataType, /* OUTPUT: Declared data type */ 1215 char const **pzCollSeq, /* OUTPUT: Collation sequence name */ 1216 int *pNotNull, /* OUTPUT: True if NOT NULL constraint exists */ 1217 int *pPrimaryKey, /* OUTPUT: True if column part of PK */ 1218 int *pAutoinc /* OUTPUT: True if colums is auto-increment */ 1219 ){ 1220 int rc; 1221 char *zErrMsg = 0; 1222 Table *pTab = 0; 1223 Column *pCol = 0; 1224 int iCol; 1225 1226 char const *zDataType = 0; 1227 char const *zCollSeq = 0; 1228 int notnull = 0; 1229 int primarykey = 0; 1230 int autoinc = 0; 1231 1232 /* Ensure the database schema has been loaded */ 1233 if( sqlite3SafetyOn(db) ){ 1234 return SQLITE_MISUSE; 1235 } 1236 rc = sqlite3Init(db, &zErrMsg); 1237 if( SQLITE_OK!=rc ){ 1238 goto error_out; 1239 } 1240 1241 /* Locate the table in question */ 1242 pTab = sqlite3FindTable(db, zTableName, zDbName); 1243 if( !pTab || pTab->pSelect ){ 1244 pTab = 0; 1245 goto error_out; 1246 } 1247 1248 /* Find the column for which info is requested */ 1249 if( sqlite3IsRowid(zColumnName) ){ 1250 iCol = pTab->iPKey; 1251 if( iCol>=0 ){ 1252 pCol = &pTab->aCol[iCol]; 1253 } 1254 }else{ 1255 for(iCol=0; iCol<pTab->nCol; iCol++){ 1256 pCol = &pTab->aCol[iCol]; 1257 if( 0==sqlite3StrICmp(pCol->zName, zColumnName) ){ 1258 break; 1259 } 1260 } 1261 if( iCol==pTab->nCol ){ 1262 pTab = 0; 1263 goto error_out; 1264 } 1265 } 1266 1267 /* The following block stores the meta information that will be returned 1268 ** to the caller in local variables zDataType, zCollSeq, notnull, primarykey 1269 ** and autoinc. At this point there are two possibilities: 1270 ** 1271 ** 1. The specified column name was rowid", "oid" or "_rowid_" 1272 ** and there is no explicitly declared IPK column. 1273 ** 1274 ** 2. The table is not a view and the column name identified an 1275 ** explicitly declared column. Copy meta information from *pCol. 1276 */ 1277 if( pCol ){ 1278 zDataType = pCol->zType; 1279 zCollSeq = pCol->zColl; 1280 notnull = (pCol->notNull?1:0); 1281 primarykey = (pCol->isPrimKey?1:0); 1282 autoinc = ((pTab->iPKey==iCol && pTab->autoInc)?1:0); 1283 }else{ 1284 zDataType = "INTEGER"; 1285 primarykey = 1; 1286 } 1287 if( !zCollSeq ){ 1288 zCollSeq = "BINARY"; 1289 } 1290 1291 error_out: 1292 if( sqlite3SafetyOff(db) ){ 1293 rc = SQLITE_MISUSE; 1294 } 1295 1296 /* Whether the function call succeeded or failed, set the output parameters 1297 ** to whatever their local counterparts contain. If an error did occur, 1298 ** this has the effect of zeroing all output parameters. 1299 */ 1300 if( pzDataType ) *pzDataType = zDataType; 1301 if( pzCollSeq ) *pzCollSeq = zCollSeq; 1302 if( pNotNull ) *pNotNull = notnull; 1303 if( pPrimaryKey ) *pPrimaryKey = primarykey; 1304 if( pAutoinc ) *pAutoinc = autoinc; 1305 1306 if( SQLITE_OK==rc && !pTab ){ 1307 sqlite3SetString(&zErrMsg, "no such table column: ", zTableName, ".", 1308 zColumnName, 0); 1309 rc = SQLITE_ERROR; 1310 } 1311 sqlite3Error(db, rc, (zErrMsg?"%s":0), zErrMsg); 1312 sqliteFree(zErrMsg); 1313 return sqlite3ApiExit(db, rc); 1314 } 1315 #endif 1316 1317 /* 1318 ** Set all the parameters in the compiled SQL statement to NULL. 1319 */ 1320 int sqlite3_clear_bindings(sqlite3_stmt *pStmt){ 1321 int i; 1322 int rc = SQLITE_OK; 1323 for(i=1; rc==SQLITE_OK && i<=sqlite3_bind_parameter_count(pStmt); i++){ 1324 rc = sqlite3_bind_null(pStmt, i); 1325 } 1326 return rc; 1327 } 1328 1329 /* 1330 ** Sleep for a little while. Return the amount of time slept. 1331 */ 1332 int sqlite3_sleep(int ms){ 1333 return sqlite3OsSleep(ms); 1334 } 1335 1336 /* 1337 ** Enable or disable the extended result codes. 1338 */ 1339 int sqlite3_extended_result_codes(sqlite3 *db, int onoff){ 1340 db->errMask = onoff ? 0xffffffff : 0xff; 1341 return SQLITE_OK; 1342 } 1343