| 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{ |
| 159 | if( p2 ){ | |
|---|---|---|
| 160 | *(int *)p2 = n; | |
| 161 | p2 += 8; | |
| 162 | } | |
| 163 | return (void *)p2; | |
| 164 | } | |
| 165 | void sqlite3GenericFree(void *p){ | |
| 166 | assert(p); | |
| 167 | free((void *)((char *)p - 8)); | |
| 168 | } | |
| 169 | int sqlite3GenericAllocationSize(void *p){ | |
| 170 | return p ? *(int *)((char *)p - 8) : 0; | |
| 171 | } | |
| 172 | #else | |
| 173 | void *sqlite3GenericMalloc(int n){ | |
| 174 | char *p = (char *)malloc(n); | |
| 175 | return (void *)p; | |
| 176 | } | |
| 177 | void *sqlite3GenericRealloc(void *p, int n){ | |
| 178 | assert(n>0); | |
| 179 | p = realloc(p, n); | |
| 180 | return p; | |
| 181 | } | |
| 182 | void sqlite3GenericFree(void *p){ | |
| 183 | assert(p); | |
| 184 | free(p); | |
| 185 | } | |
| 186 | /* Never actually used, but needed for the linker */ | |
| 187 | int sqlite3GenericAllocationSize(void *p){ return 0; } | |
| 188 | #endif |
| 153 | /* | |
|---|---|---|
| 154 | ** Execute the statement pStmt, either until a row of data is ready, the | |
| 155 | ** statement is completely executed or an error occurs. | |
| 156 | ** | |
| 157 | ** This routine implements the bulk of the logic behind the sqlite_step() | |
| 158 | ** API. The only thing omitted is the automatic recompile if a | |
| 159 | ** schema change has occurred. That detail is handled by the | |
| 160 | ** outer sqlite3_step() wrapper procedure. | |
| 161 | */ | |
| 162 | static int sqlite3Step(Vdbe *p){ | |
| 163 | sqlite3 *db; | |
| 164 | int rc; | |
| 165 | ||
| 166 | /* Assert that malloc() has not failed */ | |
| 167 | assert( !sqlite3MallocFailed() ); | |
| 168 | ||
| 169 | if( p==0 || p->magic!=VDBE_MAGIC_RUN ){ | |
| 170 | return SQLITE_MISUSE; | |
| 171 | } | |
| 172 | if( p->aborted ){ | |
| 173 | return SQLITE_ABORT; | |
| 174 | } | |
| 175 | if( p->pc<=0 && p->expired ){ | |
| 176 | if( p->rc==SQLITE_OK ){ | |
| 177 | p->rc = SQLITE_SCHEMA; | |
| 178 | } | |
| 179 | rc = SQLITE_ERROR; | |
| 180 | goto end_of_step; | |
| 181 | } | |
| 182 | db = p->db; | |
| 183 | if( sqlite3SafetyOn(db) ){ | |
| 184 | p->rc = SQLITE_MISUSE; | |
| 185 | return SQLITE_MISUSE; | |
| 186 | } | |
| 187 | if( p->pc<0 ){ | |
| 188 | /* If there are no other statements currently running, then | |
| 189 | ** reset the interrupt flag. This prevents a call to sqlite3_interrupt | |
| 190 | ** from interrupting a statement that has not yet started. | |
| 191 | */ | |
| 192 | if( db->activeVdbeCnt==0 ){ | |
| 193 | db->u1.isInterrupted = 0; | |
| 194 | } | |
| 195 | ||
| 196 | #ifndef SQLITE_OMIT_TRACE | |
| 197 | /* Invoke the trace callback if there is one | |
| 198 | */ | |
| 199 | if( db->xTrace && !db->init.busy ){ | |
| 200 | assert( p->nOp>0 ); | |
| 201 | assert( p->aOp[p->nOp-1].opcode==OP_Noop ); | |
| 202 | assert( p->aOp[p->nOp-1].p3!=0 ); | |
| 203 | assert( p->aOp[p->nOp-1].p3type==P3_DYNAMIC ); | |
| 204 | sqlite3SafetyOff(db); | |
| 205 | db->xTrace(db->pTraceArg, p->aOp[p->nOp-1].p3); | |
| 206 | if( sqlite3SafetyOn(db) ){ | |
| 207 | p->rc = SQLITE_MISUSE; | |
| 208 | return SQLITE_MISUSE; | |
| 209 | } | |
| 210 | } | |
| 211 | if( db->xProfile && !db->init.busy ){ | |
| 212 | double rNow; | |
| 213 | sqlite3OsCurrentTime(&rNow); | |
| 214 | p->startTime = (rNow - (int)rNow)*3600.0*24.0*1000000000.0; | |
| 215 | } | |
| 216 | #endif | |
| 217 | ||
| 218 | /* Print a copy of SQL as it is executed if the SQL_TRACE pragma is turned | |
| 219 | ** on in debugging mode. | |
| 220 | */ | |
| 221 | #ifdef SQLITE_DEBUG | |
| 222 | if( (db->flags & SQLITE_SqlTrace)!=0 ){ | |
| 223 | sqlite3DebugPrintf("SQL-trace: %s\n", p->aOp[p->nOp-1].p3); | |
| 224 | } | |
| 225 | #endif /* SQLITE_DEBUG */ | |
| 226 | ||
| 227 | db->activeVdbeCnt++; | |
| 228 | p->pc = 0; | |
| 229 | } | |
| 230 | #ifndef SQLITE_OMIT_EXPLAIN | |
| 231 | if( p->explain ){ | |
| 232 | rc = sqlite3VdbeList(p); | |
| 233 | }else | |
| 234 | #endif /* SQLITE_OMIT_EXPLAIN */ | |
| 235 | { | |
| 236 | rc = sqlite3VdbeExec(p); | |
| 237 | } | |
| 238 | ||
| 239 | if( sqlite3SafetyOff(db) ){ | |
| 240 | rc = SQLITE_MISUSE; | |
| 241 | } | |
| 242 | ||
| 243 | #ifndef SQLITE_OMIT_TRACE | |
| 244 | /* Invoke the profile callback if there is one | |
| 245 | */ | |
| 246 | if( rc!=SQLITE_ROW && db->xProfile && !db->init.busy ){ | |
| 247 | double rNow; | |
| 248 | u64 elapseTime; | |
| 249 | ||
| 250 | sqlite3OsCurrentTime(&rNow); | |
| 251 | elapseTime = (rNow - (int)rNow)*3600.0*24.0*1000000000.0 - p->startTime; | |
| 252 | assert( p->nOp>0 ); | |
| 253 | assert( p->aOp[p->nOp-1].opcode==OP_Noop ); | |
| 254 | assert( p->aOp[p->nOp-1].p3!=0 ); | |
| 255 | assert( p->aOp[p->nOp-1].p3type==P3_DYNAMIC ); | |
| 256 | db->xProfile(db->pProfileArg, p->aOp[p->nOp-1].p3, elapseTime); | |
| 257 | } | |
| 258 | #endif | |
| 259 | ||
| 260 | sqlite3Error(p->db, rc, 0); | |
| 261 | p->rc = sqlite3ApiExit(p->db, p->rc); | |
| 262 | end_of_step: | |
| 263 | assert( (rc&0xff)==rc ); | |
| 264 | if( p->zSql && (rc&0xff)<SQLITE_ROW ){ | |
| 265 | /* This behavior occurs if sqlite3_prepare_v2() was used to build | |
| 266 | ** the prepared statement. Return error codes directly */ | |
| 267 | return p->rc; | |
| 268 | }else{ | |
| 269 | /* This is for legacy sqlite3_prepare() builds and when the code | |
| 270 | ** is SQLITE_ROW or SQLITE_DONE */ | |
| 271 | return rc; | |
| 272 | } | |
| 273 | } | |
| 274 | ||
| 275 | /* | |
| 276 | ** This is the top-level implementation of sqlite3_step(). Call | |
| 277 | ** sqlite3Step() to do most of the work. If a schema error occurs, | |
| 278 | ** call sqlite3Reprepare() and try again. | |
| 279 | */ | |
| 280 | #ifdef SQLITE_OMIT_PARSER | |
| 281 | int sqlite3_step(sqlite3_stmt *pStmt){ | |
| 282 | return sqlite3Step((Vdbe*)pStmt); | |
| 283 | } | |
| 284 | #else | |
| 285 | int sqlite3_step(sqlite3_stmt *pStmt){ | |
| 286 | int cnt = 0; | |
| 287 | int rc; | |
| 288 | Vdbe *v = (Vdbe*)pStmt; | |
| 289 | while( (rc = sqlite3Step(v))==SQLITE_SCHEMA | |
| 290 | && cnt++ < 5 | |
| 291 | && sqlite3Reprepare(v) ){ | |
| 292 | sqlite3_reset(pStmt); | |
| 293 | v->expired = 0; | |
| 294 | } | |
| 295 | return rc; | |
| 296 | } | |
| 297 | #endif | |
| 298 | ||
| 299 | /* | |
| 300 | ** Extract the user data from a sqlite3_context structure and return a | |
| 301 | ** pointer to it. | |
| 302 | */ | |
| 303 | void *sqlite3_user_data(sqlite3_context *p){ | |
| 304 | assert( p && p->pFunc ); | |
| 305 | return p->pFunc->pUserData; | |
| 306 | } | |
| 307 | ||
| 308 | /* | |
| 309 | ** The following is the implementation of an SQL function that always | |
| 310 | ** fails with an error message stating that the function is used in the |
| 574 | sqlite3ReleaseThreadData(); | |
|---|---|---|
| 575 | } | |
| 576 | } | |
| 577 | } | |
| 578 | #else | |
| 579 | #define updateMemoryUsedCount(x) /* no-op */ | |
| 580 | #endif | |
| 581 | ||
| 582 | /* | |
| 583 | ** Allocate and return N bytes of uninitialised memory by calling | |
| 584 | ** sqlite3OsMalloc(). If the Malloc() call fails, attempt to free memory | |
| 585 | ** by calling sqlite3_release_memory(). | |
| 586 | */ | |
| 587 | void *sqlite3MallocRaw(int n, int doMemManage){ | |
| 588 | void *p = 0; | |
| 589 | if( n>0 && !sqlite3MallocFailed() && (!doMemManage || enforceSoftLimit(n)) ){ | |
| 590 | while( (p = OSMALLOC(n))==0 && sqlite3_release_memory(n) ){} | |
| 591 | if( !p ){ | |
| 592 | sqlite3FailedMalloc(); | |
| 593 | OSMALLOC_FAILED(); | |
| 594 | }else if( doMemManage ){ | |
| 595 | updateMemoryUsedCount(OSSIZEOF(p)); | |
| 596 | } | |
| 597 | } | |
| 598 | return p; | |
| 599 | } | |
| 600 | ||
| 601 | /* | |
| 602 | ** Resize the allocation at p to n bytes by calling sqlite3OsRealloc(). The | |
| 603 | ** pointer to the new allocation is returned. If the Realloc() call fails, | |
| 604 | ** attempt to free memory by calling sqlite3_release_memory(). | |
| 605 | */ | |
| 606 | void *sqlite3Realloc(void *p, int n){ | |
| 607 | if( sqlite3MallocFailed() ){ | |
| 608 | return 0; | |
| 609 | } | |
| 610 | ||
| 611 | if( !p ){ | |
| 612 | return sqlite3Malloc(n, 1); | |
| 613 | }else{ | |
| 621 | sqlite3FailedMalloc(); | |
| 622 | OSMALLOC_FAILED(); | |
| 623 | }else{ | |
| 624 | updateMemoryUsedCount(OSSIZEOF(np) - origSize); | |
| 625 | } | |
| 626 | } | |
| 627 | return np; | |
| 628 | } | |
| 629 | } | |
| 630 | ||
| 631 | /* | |
| 632 | ** Free the memory pointed to by p. p must be either a NULL pointer or a | |
| 633 | ** value returned by a previous call to sqlite3Malloc() or sqlite3Realloc(). | |
| 634 | */ | |
| 635 | void sqlite3FreeX(void *p){ | |
| 636 | if( p ){ | |
| 637 | updateMemoryUsedCount(0 - OSSIZEOF(p)); | |
| 638 | OSFREE(p); | |
| 639 | } | |
| 640 | } | |
| 641 | ||
| 642 | /* | |
| 643 | ** A version of sqliteMalloc() that is always a function, not a macro. | |
| 644 | ** Currently, this is used only to alloc to allocate the parser engine. | |
| 645 | */ | |
| 646 | void *sqlite3MallocX(int n){ | |
| 647 | return sqliteMalloc(n); | |
| 648 | } | |
| 649 | ||
| 650 | /* | |
| 651 | ** sqlite3Malloc | |
| 652 | ** sqlite3ReallocOrFree | |
| 653 | ** | |
| 654 | ** These two are implemented as wrappers around sqlite3MallocRaw(), | |
| 732 | zNew = sqlite3MallocRaw(n+1, 1); | |
| 733 | if( zNew ){ | |
| 734 | memcpy(zNew, z, n); | |
| 735 | zNew[n] = 0; | |
| 736 | } | |
| 737 | return zNew; | |
| 738 | } | |
| 739 | ||
| 740 | /* | |
| 741 | ** Create a string from the 2nd and subsequent arguments (up to the | |
| 742 | ** first NULL argument), store the string in memory obtained from | |
| 743 | ** sqliteMalloc() and make the pointer indicated by the 1st argument | |
| 744 | ** point to that string. The 1st argument must either be NULL or | |
| 745 | ** point to memory obtained from sqliteMalloc(). | |
| 746 | */ | |
| 747 | void sqlite3SetString(char **pz, ...){ | |
| 748 | va_list ap; | |
| 749 | int nByte; | |
| 750 | const char *z; | |
| 751 | char *zResult; | |
| 752 | ||
| 753 | if( pz==0 ) return; | |
| 754 | nByte = 1; | |
| 755 | va_start(ap, pz); | |
| 756 | while( (z = va_arg(ap, const char*))!=0 ){ | |
| 757 | nByte += strlen(z); | |
| 758 | } | |
| 759 | va_end(ap); | |
| 760 | sqliteFree(*pz); | |
| 761 | *pz = zResult = sqliteMallocRaw( nByte ); | |
| 762 | if( zResult==0 ){ | |
| 763 | return; | |
| 764 | } | |
| 765 | *zResult = 0; | |
| 766 | va_start(ap, pz); | |
| 767 | while( (z = va_arg(ap, const char*))!=0 ){ | |
| 768 | strcpy(zResult, z); | |
| 769 | zResult += strlen(zResult); | |
| 770 | } | |
| 771 | va_end(ap); | |
| 772 | } | |
| 773 | ||
| 774 | /* | |
| 775 | ** Set the most recent error code and error string for the sqlite | |
| 776 | ** handle "db". The error code is set to "err_code". | |
| 777 | ** | |
| 778 | ** If it is not NULL, string zFormat specifies the format of the | |
| 779 | ** error string in the style of the printf functions: The following | |
| 780 | ** format characters are allowed: | |
| 781 | ** | |
| 782 | ** %s Insert a string | |
| 783 | ** %z A string that should be freed after use | |
| 784 | ** %d Insert an integer | |
| 785 | ** %T Insert a token | |
| 786 | ** %S Insert the first element of a SrcList | |
| 787 | ** | |
| 788 | ** zFormat and any string tokens that follow it are assumed to be | |
| 789 | ** encoded in UTF-8. | |
| 790 | ** | |
| 791 | ** To clear the most recent error for sqlite handle "db", sqlite3Error | |
| 792 | ** should be called with err_code set to SQLITE_OK and zFormat set | |
| 793 | ** to NULL. | |
| 794 | */ | |
| 795 | void sqlite3Error(sqlite3 *db, int err_code, const char *zFormat, ...){ | |
| 796 | if( db && (db->pErr || (db->pErr = sqlite3ValueNew())!=0) ){ | |
| 797 | db->errCode = err_code; | |
| 798 | if( zFormat ){ | |
| 799 | char *z; | |
| 800 | va_list ap; | |
| 801 | va_start(ap, zFormat); | |
| 802 | z = sqlite3VMPrintf(zFormat, ap); | |
| 803 | va_end(ap); | |
| 804 | sqlite3ValueSetStr(db->pErr, -1, z, SQLITE_UTF8, sqlite3FreeX); | |
| 805 | }else{ | |
| 806 | sqlite3ValueSetStr(db->pErr, 0, 0, SQLITE_UTF8, SQLITE_STATIC); | |
| 807 | } | |
| 808 | } | |
| 809 | } | |
| 810 | ||
| 811 | /* | |
| 812 | ** Add an error message to pParse->zErrMsg and increment pParse->nErr. | |
| 813 | ** The following formatting characters are allowed: | |
| 814 | ** | |
| 815 | ** %s Insert a string | |
| 816 | ** %z A string that should be freed after use | |
| 817 | ** %d Insert an integer | |
| 818 | ** %T Insert a token | |
| 819 | ** %S Insert the first element of a SrcList | |
| 820 | ** | |
| 821 | ** This function should be used to report any error that occurs whilst | |
| 822 | ** compiling an SQL statement (i.e. within sqlite3_prepare()). The | |
| 823 | ** last thing the sqlite3_prepare() function does is copy the error | |
| 1133 | ** when this routine is called. | |
| 1134 | ** | |
| 1135 | ** This routine is a attempt to detect if two threads use the | |
| 1136 | ** same sqlite* pointer at the same time. There is a race | |
| 1137 | ** condition so it is possible that the error is not detected. | |
| 1138 | ** But usually the problem will be seen. The result will be an | |
| 1139 | ** error which can be used to debug the application that is | |
| 1140 | ** using SQLite incorrectly. | |
| 1141 | ** | |
| 1142 | ** Ticket #202: If db->magic is not a valid open value, take care not | |
| 1143 | ** to modify the db structure at all. It could be that db is a stale | |
| 1144 | ** pointer. In other words, it could be that there has been a prior | |
| 1145 | ** call to sqlite3_close(db) and db has been deallocated. And we do | |
| 1146 | ** not want to write into deallocated memory. | |
| 1147 | */ | |
| 1148 | int sqlite3SafetyOn(sqlite3 *db){ | |
| 1149 | if( db->magic==SQLITE_MAGIC_OPEN ){ | |
| 1150 | db->magic = SQLITE_MAGIC_BUSY; | |
| 1151 | return 0; | |
| 1152 | }else if( db->magic==SQLITE_MAGIC_BUSY ){ | |
| 1153 | db->magic = SQLITE_MAGIC_ERROR; | |
| 1154 | db->u1.isInterrupted = 1; | |
| 1155 | } | |
| 1156 | return 1; | |
| 1157 | } | |
| 1158 | ||
| 1159 | /* | |
| 1160 | ** Change the magic from SQLITE_MAGIC_BUSY to SQLITE_MAGIC_OPEN. | |
| 1161 | ** Return an error (non-zero) if the magic was not SQLITE_MAGIC_BUSY | |
| 1162 | ** when this routine is called. | |
| 1163 | */ | |
| 1164 | int sqlite3SafetyOff(sqlite3 *db){ | |
| 1165 | if( db->magic==SQLITE_MAGIC_BUSY ){ | |
| 1166 | db->magic = SQLITE_MAGIC_OPEN; | |
| 1167 | return 0; | |
| 1168 | }else if( db->magic==SQLITE_MAGIC_OPEN ){ | |
| 1169 | db->magic = SQLITE_MAGIC_ERROR; | |
| 1170 | db->u1.isInterrupted = 1; | |
| 1171 | } | |
| 1172 | return 1; | |
| 1173 | } | |
| 1174 | ||
| 1175 | /* | |
| 1176 | ** Check to make sure we have a valid db pointer. This test is not | |
| 1177 | ** foolproof but it does provide some measure of protection against | |
| 1178 | ** misuse of the interface such as passing in db pointers that are | |
| 1179 | ** NULL or which have been previously closed. If this routine returns | |
| 1180 | ** TRUE it means that the db pointer is invalid and should not be | |
| 1181 | ** dereferenced for any reason. The calling function should invoke | |
| 1182 | ** SQLITE_MISUSE immediately. | |
| 1183 | */ | |
| 1184 | int sqlite3SafetyCheck(sqlite3 *db){ | |
| 1185 | int magic; | |
| 1186 | if( db==0 ) return 1; | |
| 1187 | magic = db->magic; | |
| 1428 | /* | |
| 1429 | ** This function must be called before exiting any API function (i.e. | |
| 1430 | ** returning control to the user) that has called sqlite3Malloc or | |
| 1431 | ** sqlite3Realloc. | |
| 1432 | ** | |
| 1433 | ** The returned value is normally a copy of the second argument to this | |
| 1434 | ** function. However, if a malloc() failure has occured since the previous | |
| 1435 | ** invocation SQLITE_NOMEM is returned instead. | |
| 1436 | ** | |
| 1437 | ** If the first argument, db, is not NULL and a malloc() error has occured, | |
| 1438 | ** then the connection error-code (the value returned by sqlite3_errcode()) | |
| 1439 | ** is set to SQLITE_NOMEM. | |
| 1440 | */ | |
| 1441 | static int mallocHasFailed = 0; | |
| 1442 | int sqlite3ApiExit(sqlite3* db, int rc){ | |
| 1443 | if( sqlite3MallocFailed() ){ | |
| 1444 | mallocHasFailed = 0; | |
| 1445 | sqlite3OsLeaveMutex(); | |
| 1446 | sqlite3Error(db, SQLITE_NOMEM, 0); | |
| 1447 | rc = SQLITE_NOMEM; | |
| 1448 | } | |
| 1449 | return rc & (db ? db->errMask : 0xff); | |
| 1450 | } | |
| 1451 | ||
| 1452 | /* | |
| 1453 | ** Return true is a malloc has failed in this thread since the last call | |
| 1454 | ** to sqlite3ApiExit(), or false otherwise. | |
| 1455 | */ | |
| 1456 | int sqlite3MallocFailed(){ | |
| 1457 | return (mallocHasFailed && sqlite3OsInMutex(1)); | |
| 1458 | } | |
| 1459 | ||
| 1460 | /* | |
| 1461 | ** Set the "malloc has failed" condition to true for this thread. | |
| 1462 | */ | |
| 1463 | void sqlite3FailedMalloc(){ | |
| 1464 | sqlite3OsEnterMutex(); | |
| 1465 | assert( mallocHasFailed==0 ); | |
| 1466 | mallocHasFailed = 1; | |
| 1467 | } | |
| 1468 | ||
| 1469 | #ifdef SQLITE_MEMDEBUG | |
| 1470 | /* | |
| 1471 | ** This function sets a flag in the thread-specific-data structure that will | |
| 1472 | ** cause an assert to fail if sqliteMalloc() or sqliteRealloc() is called. |
| 2588 | }else{ | |
|---|---|---|
| 2589 | rc = SQLITE_OK; | |
| 2590 | } | |
| 2591 | pBt->inStmt = 0; | |
| 2592 | return rc; | |
| 2593 | } | |
| 2594 | ||
| 2595 | /* | |
| 2596 | ** Rollback the active statement subtransaction. If no subtransaction | |
| 2597 | ** is active this routine is a no-op. | |
| 2598 | ** | |
| 2599 | ** All cursors will be invalidated by this operation. Any attempt | |
| 2600 | ** to use a cursor that was open at the beginning of this operation | |
| 2601 | ** will result in an error. | |
| 2602 | */ | |
| 2603 | int sqlite3BtreeRollbackStmt(Btree *p){ | |
| 2604 | int rc = SQLITE_OK; | |
| 2605 | BtShared *pBt = p->pBt; | |
| 2606 | sqlite3MallocDisallow(); | |
| 2607 | if( pBt->inStmt && !pBt->readOnly ){ | |
| 2608 | rc = sqlite3pager_stmt_rollback(pBt->pPager); | |
| 2609 | assert( countWriteCursors(pBt)==0 ); | |
| 2610 | pBt->inStmt = 0; | |
| 2611 | } | |
| 2612 | sqlite3MallocAllow(); | |
| 2613 | return rc; | |
| 2614 | } | |
| 2615 | ||
| 2616 | /* | |
| 2617 | ** Default key comparison function to be used if no comparison function | |
| 2618 | ** is specified on the sqlite3BtreeCursor() call. | |
| 2619 | */ | |
| 2620 | static int dfltCompare( | |
| 2621 | void *NotUsed, /* User data is not used */ | |
| 2622 | int n1, const void *p1, /* First key to compare */ | |
| 2623 | int n2, const void *p2 /* Second key to compare */ | |
| 2624 | ){ | |
| 2625 | int c; | |
| 2626 | c = memcmp(p1, p2, n1<n2 ? n1 : n2); | |
| 2627 | if( c==0 ){ | |
| 2628 | c = n1 - n2; |
| 406 | ** | |
|---|---|---|
| 407 | ** If the callback ever returns non-zero, then the program exits | |
| 408 | ** immediately. There will be no error message but the p->rc field is | |
| 409 | ** set to SQLITE_ABORT and this routine will return SQLITE_ERROR. | |
| 410 | ** | |
| 411 | ** A memory allocation error causes p->rc to be set to SQLITE_NOMEM and this | |
| 412 | ** routine to return SQLITE_ERROR. | |
| 413 | ** | |
| 414 | ** Other fatal errors return SQLITE_ERROR. | |
| 415 | ** | |
| 416 | ** After this routine has finished, sqlite3VdbeFinalize() should be | |
| 417 | ** used to clean up the mess that was left behind. | |
| 418 | */ | |
| 419 | int sqlite3VdbeExec( | |
| 420 | Vdbe *p /* The VDBE */ | |
| 421 | ){ | |
| 422 | int pc; /* The program counter */ | |
| 423 | Op *pOp; /* Current operation */ | |
| 424 | int rc = SQLITE_OK; /* Value to return */ | |
| 425 | sqlite3 *db = p->db; /* The database */ | |
| 426 | u8 encoding = ENC(db); /* The database encoding */ | |
| 427 | Mem *pTos; /* Top entry in the operand stack */ | |
| 428 | #ifdef VDBE_PROFILE | |
| 429 | unsigned long long start; /* CPU clock count at start of opcode */ | |
| 430 | int origPc; /* Program counter at start of opcode */ | |
| 431 | #endif | |
| 432 | #ifndef SQLITE_OMIT_PROGRESS_CALLBACK | |
| 433 | int nProgressOps = 0; /* Opcodes executed since progress callback. */ | |
| 434 | #endif | |
| 435 | #ifndef NDEBUG | |
| 436 | Mem *pStackLimit; | |
| 437 | #endif | |
| 438 | ||
| 439 | if( p->magic!=VDBE_MAGIC_RUN ) return SQLITE_MISUSE; | |
| 440 | assert( db->magic==SQLITE_MAGIC_BUSY ); | |
| 441 | pTos = p->pTos; | |
| 442 | if( p->rc==SQLITE_NOMEM ){ | |
| 443 | /* This happens if a malloc() inside a call to sqlite3_column_text() or | |
| 444 | ** sqlite3_column_text16() failed. */ | |
| 445 | goto no_mem; | |
| 446 | } | |
| 447 | assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY ); | |
| 448 | p->rc = SQLITE_OK; | |
| 449 | assert( p->explain==0 ); | |
| 450 | if( p->popStack ){ | |
| 451 | popStack(&pTos, p->popStack); | |
| 452 | p->popStack = 0; | |
| 453 | } | |
| 454 | p->resOnStack = 0; | |
| 455 | db->busyHandler.nBusy = 0; | |
| 456 | CHECK_FOR_INTERRUPT; | |
| 457 | #ifdef SQLITE_DEBUG | |
| 458 | if( (p->db->flags & SQLITE_VdbeListing)!=0 | |
| 459 | || sqlite3OsFileExists("vdbe_explain") | |
| 460 | ){ | |
| 461 | int i; | |
| 462 | printf("VDBE Program Listing:\n"); | |
| 463 | sqlite3VdbePrintSql(p); | |
| 464 | for(i=0; i<p->nOp; i++){ | |
| 465 | sqlite3VdbePrintOp(stdout, i, &p->aOp[i]); | |
| 466 | } | |
| 467 | } | |
| 468 | if( sqlite3OsFileExists("vdbe_trace") ){ | |
| 469 | p->trace = stdout; | |
| 470 | } | |
| 471 | #endif | |
| 472 | for(pc=p->pc; rc==SQLITE_OK; pc++){ | |
| 473 | assert( pc>=0 && pc<p->nOp ); | |
| 474 | assert( pTos<=&p->aStack[pc] ); | |
| 475 | if( sqlite3MallocFailed() ) goto no_mem; | |
| 476 | #ifdef VDBE_PROFILE | |
| 477 | origPc = pc; | |
| 478 | start = hwtime(); | |
| 479 | #endif | |
| 480 | pOp = &p->aOp[pc]; | |
| 481 | ||
| 482 | /* Only allow tracing if SQLITE_DEBUG is defined. | |
| 483 | */ | |
| 484 | #ifdef SQLITE_DEBUG | |
| 485 | if( p->trace ){ | |
| 486 | if( pc==0 ){ | |
| 487 | printf("VDBE Execution Trace:\n"); | |
| 488 | sqlite3VdbePrintSql(p); | |
| 489 | } | |
| 490 | sqlite3VdbePrintOp(p->trace, pc, pOp); | |
| 491 | } | |
| 492 | if( p->trace==0 && pc==0 && sqlite3OsFileExists("vdbe_sqltrace") ){ | |
| 493 | sqlite3VdbePrintSql(p); | |
| 494 | } | |
| 495 | #endif | |
| 496 | ||
| 497 | ||
| 498 | /* Check to see if we need to simulate an interrupt. This only happens | |
| 499 | ** if we have a special test build. | |
| 500 | */ | |
| 501 | #ifdef SQLITE_TEST | |
| 502 | if( sqlite3_interrupt_count>0 ){ | |
| 503 | sqlite3_interrupt_count--; | |
| 504 | if( sqlite3_interrupt_count==0 ){ | |
| 505 | sqlite3_interrupt(db); | |
| 506 | } | |
| 507 | } | |
| 508 | #endif | |
| 509 | ||
| 510 | #ifndef SQLITE_OMIT_PROGRESS_CALLBACK | |
| 511 | /* Call the progress callback if it is configured and the required number | |
| 512 | ** of VDBE ops have been executed (either since this invocation of | |
| 513 | ** sqlite3VdbeExec() or since last time the progress callback was called). | |
| 514 | ** If the progress callback returns non-zero, exit the virtual machine with | |
| 515 | ** a return code SQLITE_ABORT. | |
| 516 | */ | |
| 517 | if( db->xProgress ){ | |
| 518 | if( db->nProgressOps==nProgressOps ){ | |
| 519 | if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse; | |
| 520 | if( db->xProgress(db->pProgressArg)!=0 ){ | |
| 521 | sqlite3SafetyOn(db); | |
| 522 | rc = SQLITE_ABORT; | |
| 523 | continue; /* skip to the next iteration of the for loop */ | |
| 524 | } | |
| 525 | nProgressOps = 0; | |
| 526 | if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse; | |
| 527 | } | |
| 528 | nProgressOps++; | |
| 529 | } | |
| 530 | #endif | |
| 531 | ||
| 532 | #ifndef NDEBUG | |
| 533 | /* This is to check that the return value of static function | |
| 534 | ** opcodeNoPush() (see vdbeaux.c) returns values that match the | |
| 535 | ** implementation of the virtual machine in this file. If | |
| 536 | ** opcodeNoPush() returns non-zero, then the stack is guarenteed | |
| 537 | ** not to grow when the opcode is executed. If it returns zero, then | |
| 538 | ** the stack may grow by at most 1. | |
| 539 | ** | |
| 540 | ** The global wrapper function sqlite3VdbeOpcodeUsesStack() is not | |
| 541 | ** available if NDEBUG is defined at build time. | |
| 542 | */ | |
| 543 | pStackLimit = pTos; | |
| 544 | if( !sqlite3VdbeOpcodeNoPush(pOp->opcode) ){ | |
| 545 | pStackLimit++; | |
| 546 | } | |
| 547 | #endif | |
| 548 | ||
| 549 | switch( pOp->opcode ){ | |
| 550 | ||
| 551 | /***************************************************************************** | |
| 552 | ** What follows is a massive switch statement where each case implements a | |
| 553 | ** separate instruction in the virtual machine. If we follow the usual | |
| 554 | ** indentation conventions, each case should be indented by 6 spaces. But | |
| 555 | ** that is a lot of wasted space on the left margin. So the code within | |
| 556 | ** the switch statement will break with convention and be flush-left. Another | |
| 557 | ** big comment (similar to this one) will mark the point in the code where | |
| 558 | ** we transition back to normal indentation. | |
| 559 | ** | |
| 560 | ** The formatting of each case is important. The makefile for SQLite | |
| 561 | ** generates two C files "opcodes.h" and "opcodes.c" by scanning this | |
| 562 | ** file looking for lines that begin with "case OP_". The opcodes.h files | |
| 563 | ** will be filled with #defines that give unique integer values to each | |
| 2322 | rc = sqlite3BtreeBeginStmt(pBt); | |
| 2323 | } | |
| 2324 | } | |
| 2325 | break; | |
| 2326 | } | |
| 2327 | ||
| 2328 | /* Opcode: AutoCommit P1 P2 * | |
| 2329 | ** | |
| 2330 | ** Set the database auto-commit flag to P1 (1 or 0). If P2 is true, roll | |
| 2331 | ** back any currently active btree transactions. If there are any active | |
| 2332 | ** VMs (apart from this one), then the COMMIT or ROLLBACK statement fails. | |
| 2333 | ** | |
| 2334 | ** This instruction causes the VM to halt. | |
| 2335 | */ | |
| 2336 | case OP_AutoCommit: { /* no-push */ | |
| 2337 | u8 i = pOp->p1; | |
| 2338 | u8 rollback = pOp->p2; | |
| 2339 | ||
| 2340 | assert( i==1 || i==0 ); | |
| 2341 | assert( i==1 || rollback==0 ); | |
| 2342 | ||
| 2343 | assert( db->activeVdbeCnt>0 ); /* At least this one VM is active */ | |
| 2344 | ||