| | @@ -670,11 +670,11 @@ |
| 670 | 670 | ** [sqlite3_libversion_number()], [sqlite3_sourceid()], |
| 671 | 671 | ** [sqlite_version()] and [sqlite_source_id()]. |
| 672 | 672 | */ |
| 673 | 673 | #define SQLITE_VERSION "3.8.0" |
| 674 | 674 | #define SQLITE_VERSION_NUMBER 3008000 |
| 675 | | -#define SQLITE_SOURCE_ID "2013-07-09 03:04:32 52a49cbc1621094b2fe2b021209b768d29e0426b" |
| 675 | +#define SQLITE_SOURCE_ID "2013-07-17 11:54:47 64bf8148b84e0ebb45c12b629f49bc9b316aceba" |
| 676 | 676 | |
| 677 | 677 | /* |
| 678 | 678 | ** CAPI3REF: Run-Time Library Version Numbers |
| 679 | 679 | ** KEYWORDS: sqlite3_version, sqlite3_sourceid |
| 680 | 680 | ** |
| | @@ -3120,11 +3120,11 @@ |
| 3120 | 3120 | ** [sqlite3_exec()], [sqlite3_step()] and [sqlite3_get_table()] for |
| 3121 | 3121 | ** database connection D. An example use for this |
| 3122 | 3122 | ** interface is to keep a GUI updated during a large query. |
| 3123 | 3123 | ** |
| 3124 | 3124 | ** ^The parameter P is passed through as the only parameter to the |
| 3125 | | -** callback function X. ^The parameter N is the number of |
| 3125 | +** callback function X. ^The parameter N is the approximate number of |
| 3126 | 3126 | ** [virtual machine instructions] that are evaluated between successive |
| 3127 | 3127 | ** invocations of the callback X. |
| 3128 | 3128 | ** |
| 3129 | 3129 | ** ^Only a single progress handler may be defined at one time per |
| 3130 | 3130 | ** [database connection]; setting a new progress handler cancels the |
| | @@ -5689,14 +5689,27 @@ |
| 5689 | 5689 | ** |
| 5690 | 5690 | ** ^Calling sqlite3_auto_extension(X) with an entry point X that is already |
| 5691 | 5691 | ** on the list of automatic extensions is a harmless no-op. ^No entry point |
| 5692 | 5692 | ** will be called more than once for each database connection that is opened. |
| 5693 | 5693 | ** |
| 5694 | | -** See also: [sqlite3_reset_auto_extension()]. |
| 5694 | +** See also: [sqlite3_reset_auto_extension()] |
| 5695 | +** and [sqlite3_cancel_auto_extension()] |
| 5695 | 5696 | */ |
| 5696 | 5697 | SQLITE_API int sqlite3_auto_extension(void (*xEntryPoint)(void)); |
| 5697 | 5698 | |
| 5699 | +/* |
| 5700 | +** CAPI3REF: Cancel Automatic Extension Loading |
| 5701 | +** |
| 5702 | +** ^The [sqlite3_cancel_auto_extension(X)] interface unregisters the |
| 5703 | +** initialization routine X that was registered using a prior call to |
| 5704 | +** [sqlite3_auto_extension(X)]. ^The [sqlite3_cancel_auto_extension(X)] |
| 5705 | +** routine returns 1 if initialization routine X was successfully |
| 5706 | +** unregistered and it returns 0 if X was not on the list of initialization |
| 5707 | +** routines. |
| 5708 | +*/ |
| 5709 | +SQLITE_API int sqlite3_cancel_auto_extension(void (*xEntryPoint)(void)); |
| 5710 | + |
| 5698 | 5711 | /* |
| 5699 | 5712 | ** CAPI3REF: Reset Automatic Extension Loading |
| 5700 | 5713 | ** |
| 5701 | 5714 | ** ^This interface disables all automatic extensions previously |
| 5702 | 5715 | ** registered using [sqlite3_auto_extension()]. |
| | @@ -6805,10 +6818,16 @@ |
| 6805 | 6818 | ** transaction rollback or database recovery operations are not included. |
| 6806 | 6819 | ** If an IO or other error occurs while writing a page to disk, the effect |
| 6807 | 6820 | ** on subsequent SQLITE_DBSTATUS_CACHE_WRITE requests is undefined.)^ ^The |
| 6808 | 6821 | ** highwater mark associated with SQLITE_DBSTATUS_CACHE_WRITE is always 0. |
| 6809 | 6822 | ** </dd> |
| 6823 | +** |
| 6824 | +** [[SQLITE_DBSTATUS_DEFERRED_FKS]] ^(<dt>SQLITE_DBSTATUS_DEFERRED_FKS</dt> |
| 6825 | +** <dd>This parameter returns the zero for the current value if and only if |
| 6826 | +** there all foreign key constraints (deferred or immediate) have been |
| 6827 | +** resolved. The highwater mark is always 0. |
| 6828 | +** </dd> |
| 6810 | 6829 | ** </dl> |
| 6811 | 6830 | */ |
| 6812 | 6831 | #define SQLITE_DBSTATUS_LOOKASIDE_USED 0 |
| 6813 | 6832 | #define SQLITE_DBSTATUS_CACHE_USED 1 |
| 6814 | 6833 | #define SQLITE_DBSTATUS_SCHEMA_USED 2 |
| | @@ -6817,11 +6836,12 @@ |
| 6817 | 6836 | #define SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE 5 |
| 6818 | 6837 | #define SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL 6 |
| 6819 | 6838 | #define SQLITE_DBSTATUS_CACHE_HIT 7 |
| 6820 | 6839 | #define SQLITE_DBSTATUS_CACHE_MISS 8 |
| 6821 | 6840 | #define SQLITE_DBSTATUS_CACHE_WRITE 9 |
| 6822 | | -#define SQLITE_DBSTATUS_MAX 9 /* Largest defined DBSTATUS */ |
| 6841 | +#define SQLITE_DBSTATUS_DEFERRED_FKS 10 |
| 6842 | +#define SQLITE_DBSTATUS_MAX 10 /* Largest defined DBSTATUS */ |
| 6823 | 6843 | |
| 6824 | 6844 | |
| 6825 | 6845 | /* |
| 6826 | 6846 | ** CAPI3REF: Prepared Statement Status |
| 6827 | 6847 | ** |
| | @@ -10117,11 +10137,11 @@ |
| 10117 | 10137 | void *pAuthArg; /* 1st argument to the access auth function */ |
| 10118 | 10138 | #endif |
| 10119 | 10139 | #ifndef SQLITE_OMIT_PROGRESS_CALLBACK |
| 10120 | 10140 | int (*xProgress)(void *); /* The progress callback */ |
| 10121 | 10141 | void *pProgressArg; /* Argument to the progress callback */ |
| 10122 | | - int nProgressOps; /* Number of opcodes for progress callback */ |
| 10142 | + unsigned nProgressOps; /* Number of opcodes for progress callback */ |
| 10123 | 10143 | #endif |
| 10124 | 10144 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 10125 | 10145 | int nVTrans; /* Allocated size of aVTrans */ |
| 10126 | 10146 | Hash aModule; /* populated by sqlite3_create_module() */ |
| 10127 | 10147 | VtabCtx *pVtabCtx; /* Context for active vtab connect/create */ |
| | @@ -10135,10 +10155,11 @@ |
| 10135 | 10155 | Savepoint *pSavepoint; /* List of active savepoints */ |
| 10136 | 10156 | int busyTimeout; /* Busy handler timeout, in msec */ |
| 10137 | 10157 | int nSavepoint; /* Number of non-transaction savepoints */ |
| 10138 | 10158 | int nStatement; /* Number of nested statement-transactions */ |
| 10139 | 10159 | i64 nDeferredCons; /* Net deferred constraints this transaction. */ |
| 10160 | + i64 nDeferredImmCons; /* Net deferred immediate constraints */ |
| 10140 | 10161 | int *pnBytesFreed; /* If not NULL, increment this in DbFree() */ |
| 10141 | 10162 | |
| 10142 | 10163 | #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY |
| 10143 | 10164 | /* The following variables are all protected by the STATIC_MASTER |
| 10144 | 10165 | ** mutex, not by sqlite3.mutex. They are used by code in notify.c. |
| | @@ -10190,10 +10211,13 @@ |
| 10190 | 10211 | #define SQLITE_ForeignKeys 0x00040000 /* Enforce foreign key constraints */ |
| 10191 | 10212 | #define SQLITE_AutoIndex 0x00080000 /* Enable automatic indexes */ |
| 10192 | 10213 | #define SQLITE_PreferBuiltin 0x00100000 /* Preference to built-in funcs */ |
| 10193 | 10214 | #define SQLITE_LoadExtension 0x00200000 /* Enable load_extension */ |
| 10194 | 10215 | #define SQLITE_EnableTrigger 0x00400000 /* True to enable triggers */ |
| 10216 | +#define SQLITE_DeferFKs 0x00800000 /* Defer all FK constraints */ |
| 10217 | +#define SQLITE_QueryOnly 0x01000000 /* Disable database changes */ |
| 10218 | + |
| 10195 | 10219 | |
| 10196 | 10220 | /* |
| 10197 | 10221 | ** Bits of the sqlite3.dbOptFlags field that are used by the |
| 10198 | 10222 | ** sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS,...) interface to |
| 10199 | 10223 | ** selectively disable various optimizations. |
| | @@ -10336,10 +10360,11 @@ |
| 10336 | 10360 | ** OP_Savepoint instruction. |
| 10337 | 10361 | */ |
| 10338 | 10362 | struct Savepoint { |
| 10339 | 10363 | char *zName; /* Savepoint name (nul-terminated) */ |
| 10340 | 10364 | i64 nDeferredCons; /* Number of deferred fk violations */ |
| 10365 | + i64 nDeferredImmCons; /* Number of deferred imm fk. */ |
| 10341 | 10366 | Savepoint *pNext; /* Parent savepoint (if any) */ |
| 10342 | 10367 | }; |
| 10343 | 10368 | |
| 10344 | 10369 | /* |
| 10345 | 10370 | ** The following are used as the second parameter to sqlite3Savepoint(), |
| | @@ -13530,10 +13555,11 @@ |
| 13530 | 13555 | #ifndef SQLITE_OMIT_TRACE |
| 13531 | 13556 | i64 startTime; /* Time when query started - used for profiling */ |
| 13532 | 13557 | #endif |
| 13533 | 13558 | i64 nFkConstraint; /* Number of imm. FK constraints this VM */ |
| 13534 | 13559 | i64 nStmtDefCons; /* Number of def. constraints when stmt started */ |
| 13560 | + i64 nStmtDefImmCons; /* Number of def. imm constraints when stmt started */ |
| 13535 | 13561 | char *zSql; /* Text of the SQL statement that generated this */ |
| 13536 | 13562 | void *pFree; /* Free this when deleting the vdbe */ |
| 13537 | 13563 | #ifdef SQLITE_DEBUG |
| 13538 | 13564 | FILE *trace; /* Write an execution trace here, if not NULL */ |
| 13539 | 13565 | #endif |
| | @@ -13889,10 +13915,20 @@ |
| 13889 | 13915 | } |
| 13890 | 13916 | *pHighwater = 0; |
| 13891 | 13917 | *pCurrent = nRet; |
| 13892 | 13918 | break; |
| 13893 | 13919 | } |
| 13920 | + |
| 13921 | + /* Set *pCurrent to non-zero if there are unresolved deferred foreign |
| 13922 | + ** key constraints. Set *pCurrent to zero if all foreign key constraints |
| 13923 | + ** have been satisfied. The *pHighwater is always set to zero. |
| 13924 | + */ |
| 13925 | + case SQLITE_DBSTATUS_DEFERRED_FKS: { |
| 13926 | + *pHighwater = 0; |
| 13927 | + *pCurrent = db->nDeferredImmCons>0 || db->nDeferredCons>0; |
| 13928 | + break; |
| 13929 | + } |
| 13894 | 13930 | |
| 13895 | 13931 | default: { |
| 13896 | 13932 | rc = SQLITE_ERROR; |
| 13897 | 13933 | } |
| 13898 | 13934 | } |
| | @@ -61946,10 +61982,11 @@ |
| 61946 | 61982 | /* If the statement transaction is being rolled back, also restore the |
| 61947 | 61983 | ** database handles deferred constraint counter to the value it had when |
| 61948 | 61984 | ** the statement transaction was opened. */ |
| 61949 | 61985 | if( eOp==SAVEPOINT_ROLLBACK ){ |
| 61950 | 61986 | db->nDeferredCons = p->nStmtDefCons; |
| 61987 | + db->nDeferredImmCons = p->nStmtDefImmCons; |
| 61951 | 61988 | } |
| 61952 | 61989 | } |
| 61953 | 61990 | return rc; |
| 61954 | 61991 | } |
| 61955 | 61992 | |
| | @@ -61964,11 +62001,13 @@ |
| 61964 | 62001 | ** and write an error message to it. Then return SQLITE_ERROR. |
| 61965 | 62002 | */ |
| 61966 | 62003 | #ifndef SQLITE_OMIT_FOREIGN_KEY |
| 61967 | 62004 | SQLITE_PRIVATE int sqlite3VdbeCheckFk(Vdbe *p, int deferred){ |
| 61968 | 62005 | sqlite3 *db = p->db; |
| 61969 | | - if( (deferred && db->nDeferredCons>0) || (!deferred && p->nFkConstraint>0) ){ |
| 62006 | + if( (deferred && (db->nDeferredCons+db->nDeferredImmCons)>0) |
| 62007 | + || (!deferred && p->nFkConstraint>0) |
| 62008 | + ){ |
| 61970 | 62009 | p->rc = SQLITE_CONSTRAINT_FOREIGNKEY; |
| 61971 | 62010 | p->errorAction = OE_Abort; |
| 61972 | 62011 | sqlite3SetString(&p->zErrMsg, db, "foreign key constraint failed"); |
| 61973 | 62012 | return SQLITE_ERROR; |
| 61974 | 62013 | } |
| | @@ -62097,10 +62136,12 @@ |
| 62097 | 62136 | }else if( rc!=SQLITE_OK ){ |
| 62098 | 62137 | p->rc = rc; |
| 62099 | 62138 | sqlite3RollbackAll(db, SQLITE_OK); |
| 62100 | 62139 | }else{ |
| 62101 | 62140 | db->nDeferredCons = 0; |
| 62141 | + db->nDeferredImmCons = 0; |
| 62142 | + db->flags &= ~SQLITE_DeferFKs; |
| 62102 | 62143 | sqlite3CommitInternalChanges(db); |
| 62103 | 62144 | } |
| 62104 | 62145 | }else{ |
| 62105 | 62146 | sqlite3RollbackAll(db, SQLITE_OK); |
| 62106 | 62147 | } |
| | @@ -63536,11 +63577,13 @@ |
| 63536 | 63577 | */ |
| 63537 | 63578 | if( db->nVdbeActive==0 ){ |
| 63538 | 63579 | db->u1.isInterrupted = 0; |
| 63539 | 63580 | } |
| 63540 | 63581 | |
| 63541 | | - assert( db->nVdbeWrite>0 || db->autoCommit==0 || db->nDeferredCons==0 ); |
| 63582 | + assert( db->nVdbeWrite>0 || db->autoCommit==0 |
| 63583 | + || (db->nDeferredCons==0 && db->nDeferredImmCons==0) |
| 63584 | + ); |
| 63542 | 63585 | |
| 63543 | 63586 | #ifndef SQLITE_OMIT_TRACE |
| 63544 | 63587 | if( db->xProfile && !db->init.busy ){ |
| 63545 | 63588 | sqlite3OsCurrentTimeInt64(db->pVfs, &p->startTime); |
| 63546 | 63589 | } |
| | @@ -65384,16 +65427,15 @@ |
| 65384 | 65427 | Op *pOp; /* Current operation */ |
| 65385 | 65428 | int rc = SQLITE_OK; /* Value to return */ |
| 65386 | 65429 | sqlite3 *db = p->db; /* The database */ |
| 65387 | 65430 | u8 resetSchemaOnFault = 0; /* Reset schema after an error if positive */ |
| 65388 | 65431 | u8 encoding = ENC(db); /* The database encoding */ |
| 65389 | | -#ifndef SQLITE_OMIT_PROGRESS_CALLBACK |
| 65390 | | - int checkProgress; /* True if progress callbacks are enabled */ |
| 65391 | | - int nProgressOps = 0; /* Opcodes executed since progress callback. */ |
| 65392 | | -#endif |
| 65393 | 65432 | int iCompare = 0; /* Result of last OP_Compare operation */ |
| 65394 | 65433 | unsigned nVmStep = 0; /* Number of virtual machine steps */ |
| 65434 | +#ifndef SQLITE_OMIT_PROGRESS_CALLBACK |
| 65435 | + unsigned nProgressOps = 0; /* nVmStep at last progress callback. */ |
| 65436 | +#endif |
| 65395 | 65437 | Mem *aMem = p->aMem; /* Copy of p->aMem */ |
| 65396 | 65438 | Mem *pIn1 = 0; /* 1st input operand */ |
| 65397 | 65439 | Mem *pIn2 = 0; /* 2nd input operand */ |
| 65398 | 65440 | Mem *pIn3 = 0; /* 3rd input operand */ |
| 65399 | 65441 | Mem *pOut = 0; /* Output operand */ |
| | @@ -65847,13 +65889,10 @@ |
| 65847 | 65889 | assert( p->explain==0 ); |
| 65848 | 65890 | p->pResultSet = 0; |
| 65849 | 65891 | db->busyHandler.nBusy = 0; |
| 65850 | 65892 | CHECK_FOR_INTERRUPT; |
| 65851 | 65893 | sqlite3VdbeIOTraceSql(p); |
| 65852 | | -#ifndef SQLITE_OMIT_PROGRESS_CALLBACK |
| 65853 | | - checkProgress = db->xProgress!=0; |
| 65854 | | -#endif |
| 65855 | 65894 | #ifdef SQLITE_DEBUG |
| 65856 | 65895 | sqlite3BeginBenignMalloc(); |
| 65857 | 65896 | if( p->pc==0 && (p->db->flags & SQLITE_VdbeListing)!=0 ){ |
| 65858 | 65897 | int i; |
| 65859 | 65898 | printf("VDBE Program Listing:\n"); |
| | @@ -65895,31 +65934,10 @@ |
| 65895 | 65934 | sqlite3_interrupt_count--; |
| 65896 | 65935 | if( sqlite3_interrupt_count==0 ){ |
| 65897 | 65936 | sqlite3_interrupt(db); |
| 65898 | 65937 | } |
| 65899 | 65938 | } |
| 65900 | | -#endif |
| 65901 | | - |
| 65902 | | -#ifndef SQLITE_OMIT_PROGRESS_CALLBACK |
| 65903 | | - /* Call the progress callback if it is configured and the required number |
| 65904 | | - ** of VDBE ops have been executed (either since this invocation of |
| 65905 | | - ** sqlite3VdbeExec() or since last time the progress callback was called). |
| 65906 | | - ** If the progress callback returns non-zero, exit the virtual machine with |
| 65907 | | - ** a return code SQLITE_ABORT. |
| 65908 | | - */ |
| 65909 | | - if( checkProgress ){ |
| 65910 | | - if( db->nProgressOps==nProgressOps ){ |
| 65911 | | - int prc; |
| 65912 | | - prc = db->xProgress(db->pProgressArg); |
| 65913 | | - if( prc!=0 ){ |
| 65914 | | - rc = SQLITE_INTERRUPT; |
| 65915 | | - goto vdbe_error_halt; |
| 65916 | | - } |
| 65917 | | - nProgressOps = 0; |
| 65918 | | - } |
| 65919 | | - nProgressOps++; |
| 65920 | | - } |
| 65921 | 65939 | #endif |
| 65922 | 65940 | |
| 65923 | 65941 | /* On any opcode with the "out2-prerelease" tag, free any |
| 65924 | 65942 | ** external allocations out of mem[p2] and set mem[p2] to be |
| 65925 | 65943 | ** an undefined integer. Opcodes will either fill in the integer |
| | @@ -66010,12 +66028,42 @@ |
| 66010 | 66028 | ** The next instruction executed will be |
| 66011 | 66029 | ** the one at index P2 from the beginning of |
| 66012 | 66030 | ** the program. |
| 66013 | 66031 | */ |
| 66014 | 66032 | case OP_Goto: { /* jump */ |
| 66015 | | - CHECK_FOR_INTERRUPT; |
| 66016 | 66033 | pc = pOp->p2 - 1; |
| 66034 | + |
| 66035 | + /* Opcodes that are used as the bottom of a loop (OP_Next, OP_Prev, |
| 66036 | + ** OP_VNext, OP_RowSetNext, or OP_SorterNext) all jump here upon |
| 66037 | + ** completion. Check to see if sqlite3_interrupt() has been called |
| 66038 | + ** or if the progress callback needs to be invoked. |
| 66039 | + ** |
| 66040 | + ** This code uses unstructured "goto" statements and does not look clean. |
| 66041 | + ** But that is not due to sloppy coding habits. The code is written this |
| 66042 | + ** way for performance, to avoid having to run the interrupt and progress |
| 66043 | + ** checks on every opcode. This helps sqlite3_step() to run about 1.5% |
| 66044 | + ** faster according to "valgrind --tool=cachegrind" */ |
| 66045 | +check_for_interrupt: |
| 66046 | + CHECK_FOR_INTERRUPT; |
| 66047 | +#ifndef SQLITE_OMIT_PROGRESS_CALLBACK |
| 66048 | + /* Call the progress callback if it is configured and the required number |
| 66049 | + ** of VDBE ops have been executed (either since this invocation of |
| 66050 | + ** sqlite3VdbeExec() or since last time the progress callback was called). |
| 66051 | + ** If the progress callback returns non-zero, exit the virtual machine with |
| 66052 | + ** a return code SQLITE_ABORT. |
| 66053 | + */ |
| 66054 | + if( db->xProgress!=0 && (nVmStep - nProgressOps)>=db->nProgressOps ){ |
| 66055 | + int prc; |
| 66056 | + prc = db->xProgress(db->pProgressArg); |
| 66057 | + if( prc!=0 ){ |
| 66058 | + rc = SQLITE_INTERRUPT; |
| 66059 | + goto vdbe_error_halt; |
| 66060 | + } |
| 66061 | + nProgressOps = nVmStep; |
| 66062 | + } |
| 66063 | +#endif |
| 66064 | + |
| 66017 | 66065 | break; |
| 66018 | 66066 | } |
| 66019 | 66067 | |
| 66020 | 66068 | /* Opcode: Gosub P1 P2 * * * |
| 66021 | 66069 | ** |
| | @@ -66132,11 +66180,11 @@ |
| 66132 | 66180 | assert( rc==SQLITE_BUSY || rc==SQLITE_OK || rc==SQLITE_ERROR ); |
| 66133 | 66181 | if( rc==SQLITE_BUSY ){ |
| 66134 | 66182 | p->rc = rc = SQLITE_BUSY; |
| 66135 | 66183 | }else{ |
| 66136 | 66184 | assert( rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT ); |
| 66137 | | - assert( rc==SQLITE_OK || db->nDeferredCons>0 ); |
| 66185 | + assert( rc==SQLITE_OK || db->nDeferredCons>0 || db->nDeferredImmCons>0 ); |
| 66138 | 66186 | rc = p->rc ? SQLITE_ERROR : SQLITE_DONE; |
| 66139 | 66187 | } |
| 66140 | 66188 | goto vdbe_return; |
| 66141 | 66189 | } |
| 66142 | 66190 | |
| | @@ -68028,10 +68076,11 @@ |
| 68028 | 68076 | |
| 68029 | 68077 | /* Link the new savepoint into the database handle's list. */ |
| 68030 | 68078 | u.as.pNew->pNext = db->pSavepoint; |
| 68031 | 68079 | db->pSavepoint = u.as.pNew; |
| 68032 | 68080 | u.as.pNew->nDeferredCons = db->nDeferredCons; |
| 68081 | + u.as.pNew->nDeferredImmCons = db->nDeferredImmCons; |
| 68033 | 68082 | } |
| 68034 | 68083 | } |
| 68035 | 68084 | }else{ |
| 68036 | 68085 | u.as.iSavepoint = 0; |
| 68037 | 68086 | |
| | @@ -68115,10 +68164,11 @@ |
| 68115 | 68164 | if( !isTransaction ){ |
| 68116 | 68165 | db->nSavepoint--; |
| 68117 | 68166 | } |
| 68118 | 68167 | }else{ |
| 68119 | 68168 | db->nDeferredCons = u.as.pSavepoint->nDeferredCons; |
| 68169 | + db->nDeferredImmCons = u.as.pSavepoint->nDeferredImmCons; |
| 68120 | 68170 | } |
| 68121 | 68171 | |
| 68122 | 68172 | if( !isTransaction ){ |
| 68123 | 68173 | rc = sqlite3VtabSavepoint(db, u.as.p1, u.as.iSavepoint); |
| 68124 | 68174 | if( rc!=SQLITE_OK ) goto abort_due_to_error; |
| | @@ -68244,10 +68294,14 @@ |
| 68244 | 68294 | |
| 68245 | 68295 | assert( p->bIsReader ); |
| 68246 | 68296 | assert( p->readOnly==0 || pOp->p2==0 ); |
| 68247 | 68297 | assert( pOp->p1>=0 && pOp->p1<db->nDb ); |
| 68248 | 68298 | assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 ); |
| 68299 | + if( pOp->p2 && (db->flags & SQLITE_QueryOnly)!=0 ){ |
| 68300 | + rc = SQLITE_READONLY; |
| 68301 | + goto abort_due_to_error; |
| 68302 | + } |
| 68249 | 68303 | u.au.pBt = db->aDb[pOp->p1].pBt; |
| 68250 | 68304 | |
| 68251 | 68305 | if( u.au.pBt ){ |
| 68252 | 68306 | rc = sqlite3BtreeBeginTrans(u.au.pBt, pOp->p2); |
| 68253 | 68307 | if( rc==SQLITE_BUSY ){ |
| | @@ -68276,10 +68330,11 @@ |
| 68276 | 68330 | |
| 68277 | 68331 | /* Store the current value of the database handles deferred constraint |
| 68278 | 68332 | ** counter. If the statement transaction needs to be rolled back, |
| 68279 | 68333 | ** the value of this counter needs to be restored too. */ |
| 68280 | 68334 | p->nStmtDefCons = db->nDeferredCons; |
| 68335 | + p->nStmtDefImmCons = db->nDeferredImmCons; |
| 68281 | 68336 | } |
| 68282 | 68337 | } |
| 68283 | 68338 | break; |
| 68284 | 68339 | } |
| 68285 | 68340 | |
| | @@ -69851,11 +69906,10 @@ |
| 69851 | 69906 | #if 0 /* local variables moved into u.br */ |
| 69852 | 69907 | VdbeCursor *pC; |
| 69853 | 69908 | int res; |
| 69854 | 69909 | #endif /* local variables moved into u.br */ |
| 69855 | 69910 | |
| 69856 | | - CHECK_FOR_INTERRUPT; |
| 69857 | 69911 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 69858 | 69912 | assert( pOp->p5<=ArraySize(p->aCounter) ); |
| 69859 | 69913 | u.br.pC = p->apCsr[pOp->p1]; |
| 69860 | 69914 | if( u.br.pC==0 ){ |
| 69861 | 69915 | break; /* See ticket #2273 */ |
| | @@ -69880,11 +69934,11 @@ |
| 69880 | 69934 | #ifdef SQLITE_TEST |
| 69881 | 69935 | sqlite3_search_count++; |
| 69882 | 69936 | #endif |
| 69883 | 69937 | } |
| 69884 | 69938 | u.br.pC->rowidIsValid = 0; |
| 69885 | | - break; |
| 69939 | + goto check_for_interrupt; |
| 69886 | 69940 | } |
| 69887 | 69941 | |
| 69888 | 69942 | /* Opcode: IdxInsert P1 P2 P3 * P5 |
| 69889 | 69943 | ** |
| 69890 | 69944 | ** Register P2 holds an SQL index key made using the |
| | @@ -70426,11 +70480,11 @@ |
| 70426 | 70480 | */ |
| 70427 | 70481 | case OP_RowSetRead: { /* jump, in1, out3 */ |
| 70428 | 70482 | #if 0 /* local variables moved into u.cb */ |
| 70429 | 70483 | i64 val; |
| 70430 | 70484 | #endif /* local variables moved into u.cb */ |
| 70431 | | - CHECK_FOR_INTERRUPT; |
| 70485 | + |
| 70432 | 70486 | pIn1 = &aMem[pOp->p1]; |
| 70433 | 70487 | if( (pIn1->flags & MEM_RowSet)==0 |
| 70434 | 70488 | || sqlite3RowSetNext(pIn1->u.pRowSet, &u.cb.val)==0 |
| 70435 | 70489 | ){ |
| 70436 | 70490 | /* The boolean index is empty */ |
| | @@ -70438,11 +70492,11 @@ |
| 70438 | 70492 | pc = pOp->p2 - 1; |
| 70439 | 70493 | }else{ |
| 70440 | 70494 | /* A value was pulled from the index */ |
| 70441 | 70495 | sqlite3VdbeMemSetInt64(&aMem[pOp->p3], u.cb.val); |
| 70442 | 70496 | } |
| 70443 | | - break; |
| 70497 | + goto check_for_interrupt; |
| 70444 | 70498 | } |
| 70445 | 70499 | |
| 70446 | 70500 | /* Opcode: RowSetTest P1 P2 P3 P4 |
| 70447 | 70501 | ** |
| 70448 | 70502 | ** Register P3 is assumed to hold a 64-bit integer value. If register P1 |
| | @@ -70658,11 +70712,13 @@ |
| 70658 | 70712 | ** If P1 is non-zero, the database constraint counter is incremented |
| 70659 | 70713 | ** (deferred foreign key constraints). Otherwise, if P1 is zero, the |
| 70660 | 70714 | ** statement counter is incremented (immediate foreign key constraints). |
| 70661 | 70715 | */ |
| 70662 | 70716 | case OP_FkCounter: { |
| 70663 | | - if( pOp->p1 ){ |
| 70717 | + if( db->flags & SQLITE_DeferFKs ){ |
| 70718 | + db->nDeferredImmCons += pOp->p2; |
| 70719 | + }else if( pOp->p1 ){ |
| 70664 | 70720 | db->nDeferredCons += pOp->p2; |
| 70665 | 70721 | }else{ |
| 70666 | 70722 | p->nFkConstraint += pOp->p2; |
| 70667 | 70723 | } |
| 70668 | 70724 | break; |
| | @@ -70679,13 +70735,13 @@ |
| 70679 | 70735 | ** zero, the jump is taken if the statement constraint-counter is zero |
| 70680 | 70736 | ** (immediate foreign key constraint violations). |
| 70681 | 70737 | */ |
| 70682 | 70738 | case OP_FkIfZero: { /* jump */ |
| 70683 | 70739 | if( pOp->p1 ){ |
| 70684 | | - if( db->nDeferredCons==0 ) pc = pOp->p2-1; |
| 70740 | + if( db->nDeferredCons==0 && db->nDeferredImmCons==0 ) pc = pOp->p2-1; |
| 70685 | 70741 | }else{ |
| 70686 | | - if( p->nFkConstraint==0 ) pc = pOp->p2-1; |
| 70742 | + if( p->nFkConstraint==0 && db->nDeferredImmCons==0 ) pc = pOp->p2-1; |
| 70687 | 70743 | } |
| 70688 | 70744 | break; |
| 70689 | 70745 | } |
| 70690 | 70746 | #endif /* #ifndef SQLITE_OMIT_FOREIGN_KEY */ |
| 70691 | 70747 | |
| | @@ -71367,11 +71423,11 @@ |
| 71367 | 71423 | |
| 71368 | 71424 | if( !u.cp.res ){ |
| 71369 | 71425 | /* If there is data, jump to P2 */ |
| 71370 | 71426 | pc = pOp->p2 - 1; |
| 71371 | 71427 | } |
| 71372 | | - break; |
| 71428 | + goto check_for_interrupt; |
| 71373 | 71429 | } |
| 71374 | 71430 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 71375 | 71431 | |
| 71376 | 71432 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 71377 | 71433 | /* Opcode: VRename P1 * * P4 * |
| | @@ -89262,11 +89318,14 @@ |
| 89262 | 89318 | sqlite3ReleaseTempReg(pParse, regRec); |
| 89263 | 89319 | sqlite3ReleaseTempRange(pParse, regTemp, nCol); |
| 89264 | 89320 | } |
| 89265 | 89321 | } |
| 89266 | 89322 | |
| 89267 | | - if( !pFKey->isDeferred && !pParse->pToplevel && !pParse->isMultiWrite ){ |
| 89323 | + if( !pFKey->isDeferred && !(pParse->db->flags & SQLITE_DeferFKs) |
| 89324 | + && !pParse->pToplevel |
| 89325 | + && !pParse->isMultiWrite |
| 89326 | + ){ |
| 89268 | 89327 | /* Special case: If this is an INSERT statement that will insert exactly |
| 89269 | 89328 | ** one row into the table, raise a constraint immediately instead of |
| 89270 | 89329 | ** incrementing a counter. This is necessary as the VM code is being |
| 89271 | 89330 | ** generated for will not open a statement transaction. */ |
| 89272 | 89331 | assert( nIncr==1 ); |
| | @@ -89653,11 +89712,13 @@ |
| 89653 | 89712 | for(pFKey = sqlite3FkReferences(pTab); pFKey; pFKey=pFKey->pNextTo){ |
| 89654 | 89713 | Index *pIdx = 0; /* Foreign key index for pFKey */ |
| 89655 | 89714 | SrcList *pSrc; |
| 89656 | 89715 | int *aiCol = 0; |
| 89657 | 89716 | |
| 89658 | | - if( !pFKey->isDeferred && !pParse->pToplevel && !pParse->isMultiWrite ){ |
| 89717 | + if( !pFKey->isDeferred && !(db->flags & SQLITE_DeferFKs) |
| 89718 | + && !pParse->pToplevel && !pParse->isMultiWrite |
| 89719 | + ){ |
| 89659 | 89720 | assert( regOld==0 && regNew!=0 ); |
| 89660 | 89721 | /* Inserting a single row into a parent table cannot cause an immediate |
| 89661 | 89722 | ** foreign key violation. So do nothing in this case. */ |
| 89662 | 89723 | continue; |
| 89663 | 89724 | } |
| | @@ -92615,15 +92676,18 @@ |
| 92615 | 92676 | #ifndef SQLITE_CORE |
| 92616 | 92677 | /* This case when the file really is being compiled as a loadable |
| 92617 | 92678 | ** extension */ |
| 92618 | 92679 | # define SQLITE_EXTENSION_INIT1 const sqlite3_api_routines *sqlite3_api=0; |
| 92619 | 92680 | # define SQLITE_EXTENSION_INIT2(v) sqlite3_api=v; |
| 92681 | +# define SQLITE_EXTENSION_INIT3 \ |
| 92682 | + extern const sqlite3_api_routines *sqlite3_api; |
| 92620 | 92683 | #else |
| 92621 | 92684 | /* This case when the file is being statically linked into the |
| 92622 | 92685 | ** application */ |
| 92623 | 92686 | # define SQLITE_EXTENSION_INIT1 /*no-op*/ |
| 92624 | 92687 | # define SQLITE_EXTENSION_INIT2(v) (void)v; /* unused parameter */ |
| 92688 | +# define SQLITE_EXTENSION_INIT3 /*no-op*/ |
| 92625 | 92689 | #endif |
| 92626 | 92690 | |
| 92627 | 92691 | #endif /* _SQLITE3EXT_H_ */ |
| 92628 | 92692 | |
| 92629 | 92693 | /************** End of sqlite3ext.h ******************************************/ |
| | @@ -93275,10 +93339,39 @@ |
| 93275 | 93339 | sqlite3_mutex_leave(mutex); |
| 93276 | 93340 | assert( (rc&0xff)==rc ); |
| 93277 | 93341 | return rc; |
| 93278 | 93342 | } |
| 93279 | 93343 | } |
| 93344 | + |
| 93345 | +/* |
| 93346 | +** Cancel a prior call to sqlite3_auto_extension. Remove xInit from the |
| 93347 | +** set of routines that is invoked for each new database connection, if it |
| 93348 | +** is currently on the list. If xInit is not on the list, then this |
| 93349 | +** routine is a no-op. |
| 93350 | +** |
| 93351 | +** Return 1 if xInit was found on the list and removed. Return 0 if xInit |
| 93352 | +** was not on the list. |
| 93353 | +*/ |
| 93354 | +SQLITE_API int sqlite3_cancel_auto_extension(void (*xInit)(void)){ |
| 93355 | +#if SQLITE_THREADSAFE |
| 93356 | + sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); |
| 93357 | +#endif |
| 93358 | + int i; |
| 93359 | + int n = 0; |
| 93360 | + wsdAutoextInit; |
| 93361 | + sqlite3_mutex_enter(mutex); |
| 93362 | + for(i=wsdAutoext.nExt-1; i>=0; i--){ |
| 93363 | + if( wsdAutoext.aExt[i]==xInit ){ |
| 93364 | + wsdAutoext.nExt--; |
| 93365 | + wsdAutoext.aExt[i] = wsdAutoext.aExt[wsdAutoext.nExt]; |
| 93366 | + n++; |
| 93367 | + break; |
| 93368 | + } |
| 93369 | + } |
| 93370 | + sqlite3_mutex_leave(mutex); |
| 93371 | + return n; |
| 93372 | +} |
| 93280 | 93373 | |
| 93281 | 93374 | /* |
| 93282 | 93375 | ** Reset the automatic extension loading mechanism. |
| 93283 | 93376 | */ |
| 93284 | 93377 | SQLITE_API void sqlite3_reset_auto_extension(void){ |
| | @@ -93516,10 +93609,11 @@ |
| 93516 | 93609 | { "empty_result_callbacks", SQLITE_NullCallback }, |
| 93517 | 93610 | { "legacy_file_format", SQLITE_LegacyFileFmt }, |
| 93518 | 93611 | { "fullfsync", SQLITE_FullFSync }, |
| 93519 | 93612 | { "checkpoint_fullfsync", SQLITE_CkptFullFSync }, |
| 93520 | 93613 | { "reverse_unordered_selects", SQLITE_ReverseOrder }, |
| 93614 | + { "query_only", SQLITE_QueryOnly }, |
| 93521 | 93615 | #ifndef SQLITE_OMIT_AUTOMATIC_INDEX |
| 93522 | 93616 | { "automatic_index", SQLITE_AutoIndex }, |
| 93523 | 93617 | #endif |
| 93524 | 93618 | #ifdef SQLITE_DEBUG |
| 93525 | 93619 | { "sql_trace", SQLITE_SqlTrace }, |
| | @@ -93536,16 +93630,17 @@ |
| 93536 | 93630 | { "writable_schema", SQLITE_WriteSchema|SQLITE_RecoveryMode }, |
| 93537 | 93631 | |
| 93538 | 93632 | /* TODO: Maybe it shouldn't be possible to change the ReadUncommitted |
| 93539 | 93633 | ** flag if there are any active statements. */ |
| 93540 | 93634 | { "read_uncommitted", SQLITE_ReadUncommitted }, |
| 93541 | | - { "recursive_triggers", SQLITE_RecTriggers }, |
| 93635 | + { "recursive_triggers", SQLITE_RecTriggers }, |
| 93542 | 93636 | |
| 93543 | 93637 | /* This flag may only be set if both foreign-key and trigger support |
| 93544 | 93638 | ** are present in the build. */ |
| 93545 | 93639 | #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER) |
| 93546 | | - { "foreign_keys", SQLITE_ForeignKeys }, |
| 93640 | + { "foreign_keys", SQLITE_ForeignKeys }, |
| 93641 | + { "defer_foreign_keys", SQLITE_DeferFKs }, |
| 93547 | 93642 | #endif |
| 93548 | 93643 | }; |
| 93549 | 93644 | int i; |
| 93550 | 93645 | const struct sPragmaType *p; |
| 93551 | 93646 | for(i=0, p=aPragma; i<ArraySize(aPragma); i++, p++){ |
| | @@ -93567,10 +93662,11 @@ |
| 93567 | 93662 | |
| 93568 | 93663 | if( sqlite3GetBoolean(zRight, 0) ){ |
| 93569 | 93664 | db->flags |= mask; |
| 93570 | 93665 | }else{ |
| 93571 | 93666 | db->flags &= ~mask; |
| 93667 | + if( mask==SQLITE_DeferFKs ) db->nDeferredImmCons = 0; |
| 93572 | 93668 | } |
| 93573 | 93669 | |
| 93574 | 93670 | /* Many of the flag-pragmas modify the code generated by the SQL |
| 93575 | 93671 | ** compiler (eg. count_changes). So add an opcode to expire all |
| 93576 | 93672 | ** compiled SQL statements after modifying a pragma value. |
| | @@ -95979,10 +96075,16 @@ |
| 95979 | 96075 | |
| 95980 | 96076 | assert( ppStmt ); |
| 95981 | 96077 | *ppStmt = 0; |
| 95982 | 96078 | if( !sqlite3SafetyCheckOk(db) ){ |
| 95983 | 96079 | return SQLITE_MISUSE_BKPT; |
| 96080 | + } |
| 96081 | + if( nBytes>=0 ){ |
| 96082 | + int sz; |
| 96083 | + const char *z = (const char*)zSql; |
| 96084 | + for(sz=0; sz<nBytes && (z[sz]!=0 || z[sz+1]!=0); sz += 2){} |
| 96085 | + nBytes = sz; |
| 95984 | 96086 | } |
| 95985 | 96087 | sqlite3_mutex_enter(db->mutex); |
| 95986 | 96088 | zSql8 = sqlite3Utf16to8(db, zSql, nBytes, SQLITE_UTF16NATIVE); |
| 95987 | 96089 | if( zSql8 ){ |
| 95988 | 96090 | rc = sqlite3LockAndPrepare(db, zSql8, -1, saveSqlFlag, 0, ppStmt, &zTail8); |
| | @@ -104070,12 +104172,12 @@ |
| 104070 | 104172 | int (*x)(sqlite3_vtab *); |
| 104071 | 104173 | sqlite3_vtab *pVtab = aVTrans[i]->pVtab; |
| 104072 | 104174 | if( pVtab && (x = pVtab->pModule->xSync)!=0 ){ |
| 104073 | 104175 | rc = x(pVtab); |
| 104074 | 104176 | sqlite3DbFree(db, *pzErrmsg); |
| 104075 | | - *pzErrmsg = sqlite3DbStrDup(db, pVtab->zErrMsg); |
| 104076 | | - sqlite3_free(pVtab->zErrMsg); |
| 104177 | + *pzErrmsg = pVtab->zErrMsg; |
| 104178 | + pVtab->zErrMsg = 0; |
| 104077 | 104179 | } |
| 104078 | 104180 | } |
| 104079 | 104181 | db->aVTrans = aVTrans; |
| 104080 | 104182 | return rc; |
| 104081 | 104183 | } |
| | @@ -104391,10 +104493,12 @@ |
| 104391 | 104493 | typedef struct WhereLoop WhereLoop; |
| 104392 | 104494 | typedef struct WherePath WherePath; |
| 104393 | 104495 | typedef struct WhereTerm WhereTerm; |
| 104394 | 104496 | typedef struct WhereLoopBuilder WhereLoopBuilder; |
| 104395 | 104497 | typedef struct WhereScan WhereScan; |
| 104498 | +typedef struct WhereOrCost WhereOrCost; |
| 104499 | +typedef struct WhereOrSet WhereOrSet; |
| 104396 | 104500 | |
| 104397 | 104501 | /* |
| 104398 | 104502 | ** Cost X is tracked as 10*log2(X) stored in a 16-bit integer. The |
| 104399 | 104503 | ** maximum cost for ordinary tables is 64*(2**63) which becomes 6900. |
| 104400 | 104504 | ** (Virtual tables can return a larger cost, but let's assume they do not.) |
| | @@ -104497,10 +104601,31 @@ |
| 104497 | 104601 | u16 nLSlot; /* Number of slots allocated for aLTerm[] */ |
| 104498 | 104602 | WhereTerm **aLTerm; /* WhereTerms used */ |
| 104499 | 104603 | WhereLoop *pNextLoop; /* Next WhereLoop object in the WhereClause */ |
| 104500 | 104604 | WhereTerm *aLTermSpace[4]; /* Initial aLTerm[] space */ |
| 104501 | 104605 | }; |
| 104606 | + |
| 104607 | +/* This object holds the prerequisites and the cost of running a |
| 104608 | +** subquery on one operand of an OR operator in the WHERE clause. |
| 104609 | +** See WhereOrSet for additional information |
| 104610 | +*/ |
| 104611 | +struct WhereOrCost { |
| 104612 | + Bitmask prereq; /* Prerequisites */ |
| 104613 | + WhereCost rRun; /* Cost of running this subquery */ |
| 104614 | + WhereCost nOut; /* Number of outputs for this subquery */ |
| 104615 | +}; |
| 104616 | + |
| 104617 | +/* The WhereOrSet object holds a set of possible WhereOrCosts that |
| 104618 | +** correspond to the subquery(s) of OR-clause processing. At most |
| 104619 | +** favorable N_OR_COST elements are retained. |
| 104620 | +*/ |
| 104621 | +#define N_OR_COST 3 |
| 104622 | +struct WhereOrSet { |
| 104623 | + u16 n; /* Number of valid a[] entries */ |
| 104624 | + WhereOrCost a[N_OR_COST]; /* Set of best costs */ |
| 104625 | +}; |
| 104626 | + |
| 104502 | 104627 | |
| 104503 | 104628 | /* Forward declaration of methods */ |
| 104504 | 104629 | static int whereLoopResize(sqlite3*, WhereLoop*, int); |
| 104505 | 104630 | |
| 104506 | 104631 | /* |
| | @@ -104712,11 +104837,11 @@ |
| 104712 | 104837 | struct WhereLoopBuilder { |
| 104713 | 104838 | WhereInfo *pWInfo; /* Information about this WHERE */ |
| 104714 | 104839 | WhereClause *pWC; /* WHERE clause terms */ |
| 104715 | 104840 | ExprList *pOrderBy; /* ORDER BY clause */ |
| 104716 | 104841 | WhereLoop *pNew; /* Template WhereLoop */ |
| 104717 | | - WhereLoop *pBest; /* If non-NULL, store single best loop here */ |
| 104842 | + WhereOrSet *pOrSet; /* Record best loops here, if not NULL */ |
| 104718 | 104843 | }; |
| 104719 | 104844 | |
| 104720 | 104845 | /* |
| 104721 | 104846 | ** The WHERE clause processing routine has two halves. The |
| 104722 | 104847 | ** first part does the start of the WHERE loop and the second |
| | @@ -104854,10 +104979,58 @@ |
| 104854 | 104979 | ** UPDATE or DELETE might change subsequent WHERE clause results. |
| 104855 | 104980 | */ |
| 104856 | 104981 | SQLITE_PRIVATE int sqlite3WhereOkOnePass(WhereInfo *pWInfo){ |
| 104857 | 104982 | return pWInfo->okOnePass; |
| 104858 | 104983 | } |
| 104984 | + |
| 104985 | +/* |
| 104986 | +** Move the content of pSrc into pDest |
| 104987 | +*/ |
| 104988 | +static void whereOrMove(WhereOrSet *pDest, WhereOrSet *pSrc){ |
| 104989 | + pDest->n = pSrc->n; |
| 104990 | + memcpy(pDest->a, pSrc->a, pDest->n*sizeof(pDest->a[0])); |
| 104991 | +} |
| 104992 | + |
| 104993 | +/* |
| 104994 | +** Try to insert a new prerequisite/cost entry into the WhereOrSet pSet. |
| 104995 | +** |
| 104996 | +** The new entry might overwrite an existing entry, or it might be |
| 104997 | +** appended, or it might be discarded. Do whatever is the right thing |
| 104998 | +** so that pSet keeps the N_OR_COST best entries seen so far. |
| 104999 | +*/ |
| 105000 | +static int whereOrInsert( |
| 105001 | + WhereOrSet *pSet, /* The WhereOrSet to be updated */ |
| 105002 | + Bitmask prereq, /* Prerequisites of the new entry */ |
| 105003 | + WhereCost rRun, /* Run-cost of the new entry */ |
| 105004 | + WhereCost nOut /* Number of outputs for the new entry */ |
| 105005 | +){ |
| 105006 | + u16 i; |
| 105007 | + WhereOrCost *p; |
| 105008 | + for(i=pSet->n, p=pSet->a; i>0; i--, p++){ |
| 105009 | + if( rRun<=p->rRun && (prereq & p->prereq)==prereq ){ |
| 105010 | + goto whereOrInsert_done; |
| 105011 | + } |
| 105012 | + if( p->rRun<=rRun && (p->prereq & prereq)==p->prereq ){ |
| 105013 | + return 0; |
| 105014 | + } |
| 105015 | + } |
| 105016 | + if( pSet->n<N_OR_COST ){ |
| 105017 | + p = &pSet->a[pSet->n++]; |
| 105018 | + p->nOut = nOut; |
| 105019 | + }else{ |
| 105020 | + p = pSet->a; |
| 105021 | + for(i=1; i<pSet->n; i++){ |
| 105022 | + if( p->rRun>pSet->a[i].rRun ) p = pSet->a + i; |
| 105023 | + } |
| 105024 | + if( p->rRun<=rRun ) return 0; |
| 105025 | + } |
| 105026 | +whereOrInsert_done: |
| 105027 | + p->prereq = prereq; |
| 105028 | + p->rRun = rRun; |
| 105029 | + if( p->nOut>nOut ) p->nOut = nOut; |
| 105030 | + return 1; |
| 105031 | +} |
| 104859 | 105032 | |
| 104860 | 105033 | /* |
| 104861 | 105034 | ** Initialize a preallocated WhereClause structure. |
| 104862 | 105035 | */ |
| 104863 | 105036 | static void whereClauseInit( |
| | @@ -108089,12 +108262,13 @@ |
| 108089 | 108262 | */ |
| 108090 | 108263 | if( pWC->nTerm>1 ){ |
| 108091 | 108264 | int iTerm; |
| 108092 | 108265 | for(iTerm=0; iTerm<pWC->nTerm; iTerm++){ |
| 108093 | 108266 | Expr *pExpr = pWC->a[iTerm].pExpr; |
| 108267 | + if( &pWC->a[iTerm] == pTerm ) continue; |
| 108094 | 108268 | if( ExprHasProperty(pExpr, EP_FromJoin) ) continue; |
| 108095 | | - if( pWC->a[iTerm].wtFlags & (TERM_VIRTUAL|TERM_ORINFO) ) continue; |
| 108269 | + if( pWC->a[iTerm].wtFlags & (TERM_ORINFO) ) continue; |
| 108096 | 108270 | if( (pWC->a[iTerm].eOperator & WO_ALL)==0 ) continue; |
| 108097 | 108271 | pExpr = sqlite3ExprDup(pParse->db, pExpr, 0); |
| 108098 | 108272 | pAndExpr = sqlite3ExprAnd(pParse->db, pAndExpr, pExpr); |
| 108099 | 108273 | } |
| 108100 | 108274 | if( pAndExpr ){ |
| | @@ -108418,16 +108592,16 @@ |
| 108418 | 108592 | ** is better and has fewer dependencies. Or the template will be ignored |
| 108419 | 108593 | ** and no insert will occur if an existing WhereLoop is faster and has |
| 108420 | 108594 | ** fewer dependencies than the template. Otherwise a new WhereLoop is |
| 108421 | 108595 | ** added based on the template. |
| 108422 | 108596 | ** |
| 108423 | | -** If pBuilder->pBest is not NULL then we only care about the very |
| 108424 | | -** best template and that template should be stored in pBuilder->pBest. |
| 108425 | | -** If pBuilder->pBest is NULL then a list of the best templates are stored |
| 108426 | | -** in pBuilder->pWInfo->pLoops. |
| 108597 | +** If pBuilder->pOrSet is not NULL then we only care about only the |
| 108598 | +** prerequisites and rRun and nOut costs of the N best loops. That |
| 108599 | +** information is gathered in the pBuilder->pOrSet object. This special |
| 108600 | +** processing mode is used only for OR clause processing. |
| 108427 | 108601 | ** |
| 108428 | | -** When accumulating multiple loops (when pBuilder->pBest is NULL) we |
| 108602 | +** When accumulating multiple loops (when pBuilder->pOrSet is NULL) we |
| 108429 | 108603 | ** still might overwrite similar loops with the new template if the |
| 108430 | 108604 | ** template is better. Loops may be overwritten if the following |
| 108431 | 108605 | ** conditions are met: |
| 108432 | 108606 | ** |
| 108433 | 108607 | ** (1) They have the same iTab. |
| | @@ -108440,34 +108614,26 @@ |
| 108440 | 108614 | static int whereLoopInsert(WhereLoopBuilder *pBuilder, WhereLoop *pTemplate){ |
| 108441 | 108615 | WhereLoop **ppPrev, *p, *pNext = 0; |
| 108442 | 108616 | WhereInfo *pWInfo = pBuilder->pWInfo; |
| 108443 | 108617 | sqlite3 *db = pWInfo->pParse->db; |
| 108444 | 108618 | |
| 108445 | | - /* If pBuilder->pBest is defined, then only keep track of the single |
| 108446 | | - ** best WhereLoop. pBuilder->pBest->maskSelf==0 indicates that no |
| 108447 | | - ** prior WhereLoops have been evaluated and that the current pTemplate |
| 108448 | | - ** is therefore the first and hence the best and should be retained. |
| 108619 | + /* If pBuilder->pOrSet is defined, then only keep track of the costs |
| 108620 | + ** and prereqs. |
| 108449 | 108621 | */ |
| 108450 | | - if( (p = pBuilder->pBest)!=0 ){ |
| 108451 | | - if( p->maskSelf!=0 ){ |
| 108452 | | - WhereCost rCost = whereCostAdd(p->rRun,p->rSetup); |
| 108453 | | - WhereCost rTemplate = whereCostAdd(pTemplate->rRun,pTemplate->rSetup); |
| 108454 | | - if( rCost < rTemplate ){ |
| 108455 | | - testcase( rCost==rTemplate-1 ); |
| 108456 | | - goto whereLoopInsert_noop; |
| 108457 | | - } |
| 108458 | | - if( rCost==rTemplate && (p->prereq & pTemplate->prereq)==p->prereq ){ |
| 108459 | | - goto whereLoopInsert_noop; |
| 108460 | | - } |
| 108461 | | - } |
| 108622 | + if( pBuilder->pOrSet!=0 ){ |
| 108623 | +#if WHERETRACE_ENABLED |
| 108624 | + u16 n = pBuilder->pOrSet->n; |
| 108625 | + int x = |
| 108626 | +#endif |
| 108627 | + whereOrInsert(pBuilder->pOrSet, pTemplate->prereq, pTemplate->rRun, |
| 108628 | + pTemplate->nOut); |
| 108462 | 108629 | #if WHERETRACE_ENABLED |
| 108463 | 108630 | if( sqlite3WhereTrace & 0x8 ){ |
| 108464 | | - sqlite3DebugPrintf(p->maskSelf==0 ? "ins-init: " : "ins-best: "); |
| 108631 | + sqlite3DebugPrintf(x?" or-%d: ":" or-X: ", n); |
| 108465 | 108632 | whereLoopPrint(pTemplate, pWInfo->pTabList); |
| 108466 | 108633 | } |
| 108467 | 108634 | #endif |
| 108468 | | - whereLoopXfer(db, p, pTemplate); |
| 108469 | 108635 | return SQLITE_OK; |
| 108470 | 108636 | } |
| 108471 | 108637 | |
| 108472 | 108638 | /* Search for an existing WhereLoop to overwrite, or which takes |
| 108473 | 108639 | ** priority over pTemplate. |
| | @@ -108557,11 +108723,11 @@ |
| 108557 | 108723 | |
| 108558 | 108724 | /* Jump here if the insert is a no-op */ |
| 108559 | 108725 | whereLoopInsert_noop: |
| 108560 | 108726 | #if WHERETRACE_ENABLED |
| 108561 | 108727 | if( sqlite3WhereTrace & 0x8 ){ |
| 108562 | | - sqlite3DebugPrintf(pBuilder->pBest ? "ins-skip: " : "ins-noop: "); |
| 108728 | + sqlite3DebugPrintf("ins-noop: "); |
| 108563 | 108729 | whereLoopPrint(pTemplate, pWInfo->pTabList); |
| 108564 | 108730 | } |
| 108565 | 108731 | #endif |
| 108566 | 108732 | return SQLITE_OK; |
| 108567 | 108733 | } |
| | @@ -108839,11 +109005,11 @@ |
| 108839 | 109005 | } |
| 108840 | 109006 | rSize = whereCost(pSrc->pTab->nRowEst); |
| 108841 | 109007 | rLogSize = estLog(rSize); |
| 108842 | 109008 | |
| 108843 | 109009 | /* Automatic indexes */ |
| 108844 | | - if( !pBuilder->pBest |
| 109010 | + if( !pBuilder->pOrSet |
| 108845 | 109011 | && (pWInfo->pParse->db->flags & SQLITE_AutoIndex)!=0 |
| 108846 | 109012 | && pSrc->pIndex==0 |
| 108847 | 109013 | && !pSrc->viaCoroutine |
| 108848 | 109014 | && !pSrc->notIndexed |
| 108849 | 109015 | && !pSrc->isCorrelated |
| | @@ -109125,11 +109291,11 @@ |
| 109125 | 109291 | WhereTerm *pTerm, *pWCEnd; |
| 109126 | 109292 | int rc = SQLITE_OK; |
| 109127 | 109293 | int iCur; |
| 109128 | 109294 | WhereClause tempWC; |
| 109129 | 109295 | WhereLoopBuilder sSubBuild; |
| 109130 | | - WhereLoop sBest; |
| 109296 | + WhereOrSet sSum, sCur, sPrev; |
| 109131 | 109297 | struct SrcList_item *pItem; |
| 109132 | 109298 | |
| 109133 | 109299 | pWC = pBuilder->pWC; |
| 109134 | 109300 | if( pWInfo->wctrlFlags & WHERE_AND_ONLY ) return SQLITE_OK; |
| 109135 | 109301 | pWCEnd = pWC->a + pWC->nTerm; |
| | @@ -109140,20 +109306,18 @@ |
| 109140 | 109306 | && (pTerm->u.pOrInfo->indexable & pNew->maskSelf)!=0 |
| 109141 | 109307 | ){ |
| 109142 | 109308 | WhereClause * const pOrWC = &pTerm->u.pOrInfo->wc; |
| 109143 | 109309 | WhereTerm * const pOrWCEnd = &pOrWC->a[pOrWC->nTerm]; |
| 109144 | 109310 | WhereTerm *pOrTerm; |
| 109145 | | - WhereCost rTotal = 0; |
| 109146 | | - WhereCost nRow = 0; |
| 109147 | | - Bitmask prereq = mExtra; |
| 109311 | + int once = 1; |
| 109312 | + int i, j; |
| 109148 | 109313 | |
| 109149 | | - whereLoopInit(&sBest); |
| 109150 | 109314 | pItem = pWInfo->pTabList->a + pNew->iTab; |
| 109151 | 109315 | iCur = pItem->iCursor; |
| 109152 | 109316 | sSubBuild = *pBuilder; |
| 109153 | 109317 | sSubBuild.pOrderBy = 0; |
| 109154 | | - sSubBuild.pBest = &sBest; |
| 109318 | + sSubBuild.pOrSet = &sCur; |
| 109155 | 109319 | |
| 109156 | 109320 | for(pOrTerm=pOrWC->a; pOrTerm<pOrWCEnd; pOrTerm++){ |
| 109157 | 109321 | if( (pOrTerm->eOperator & WO_AND)!=0 ){ |
| 109158 | 109322 | sSubBuild.pWC = &pOrTerm->u.pAndInfo->wc; |
| 109159 | 109323 | }else if( pOrTerm->leftCursor==iCur ){ |
| | @@ -109164,43 +109328,52 @@ |
| 109164 | 109328 | tempWC.a = pOrTerm; |
| 109165 | 109329 | sSubBuild.pWC = &tempWC; |
| 109166 | 109330 | }else{ |
| 109167 | 109331 | continue; |
| 109168 | 109332 | } |
| 109169 | | - sBest.maskSelf = 0; |
| 109170 | | - sBest.rSetup = 0; |
| 109171 | | - sBest.rRun = 0; |
| 109333 | + sCur.n = 0; |
| 109172 | 109334 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 109173 | 109335 | if( IsVirtual(pItem->pTab) ){ |
| 109174 | 109336 | rc = whereLoopAddVirtual(&sSubBuild); |
| 109337 | + for(i=0; i<sCur.n; i++) sCur.a[i].prereq |= mExtra; |
| 109175 | 109338 | }else |
| 109176 | 109339 | #endif |
| 109177 | 109340 | { |
| 109178 | 109341 | rc = whereLoopAddBtree(&sSubBuild, mExtra); |
| 109179 | 109342 | } |
| 109180 | | - /* sBest.maskSelf is always zero if an error occurs */ |
| 109181 | | - assert( rc==SQLITE_OK || sBest.maskSelf==0 ); |
| 109182 | | - if( sBest.maskSelf==0 ) break; |
| 109183 | | - assert( sBest.rSetup==0 ); |
| 109184 | | - rTotal = whereCostAdd(rTotal, sBest.rRun); |
| 109185 | | - nRow = whereCostAdd(nRow, sBest.nOut); |
| 109186 | | - prereq |= sBest.prereq; |
| 109187 | | - } |
| 109188 | | - assert( pNew->nLSlot>=1 ); |
| 109189 | | - if( sBest.maskSelf ){ |
| 109190 | | - pNew->nLTerm = 1; |
| 109191 | | - pNew->aLTerm[0] = pTerm; |
| 109192 | | - pNew->wsFlags = WHERE_MULTI_OR; |
| 109193 | | - pNew->rSetup = 0; |
| 109343 | + assert( rc==SQLITE_OK || sCur.n==0 ); |
| 109344 | + if( sCur.n==0 ){ |
| 109345 | + sSum.n = 0; |
| 109346 | + break; |
| 109347 | + }else if( once ){ |
| 109348 | + whereOrMove(&sSum, &sCur); |
| 109349 | + once = 0; |
| 109350 | + }else{ |
| 109351 | + whereOrMove(&sPrev, &sSum); |
| 109352 | + sSum.n = 0; |
| 109353 | + for(i=0; i<sPrev.n; i++){ |
| 109354 | + for(j=0; j<sCur.n; j++){ |
| 109355 | + whereOrInsert(&sSum, sPrev.a[i].prereq | sCur.a[j].prereq, |
| 109356 | + whereCostAdd(sPrev.a[i].rRun, sCur.a[j].rRun), |
| 109357 | + whereCostAdd(sPrev.a[i].nOut, sCur.a[j].nOut)); |
| 109358 | + } |
| 109359 | + } |
| 109360 | + } |
| 109361 | + } |
| 109362 | + pNew->nLTerm = 1; |
| 109363 | + pNew->aLTerm[0] = pTerm; |
| 109364 | + pNew->wsFlags = WHERE_MULTI_OR; |
| 109365 | + pNew->rSetup = 0; |
| 109366 | + pNew->iSortIdx = 0; |
| 109367 | + memset(&pNew->u, 0, sizeof(pNew->u)); |
| 109368 | + for(i=0; rc==SQLITE_OK && i<sSum.n; i++){ |
| 109194 | 109369 | /* TUNING: Multiple by 3.5 for the secondary table lookup */ |
| 109195 | | - pNew->rRun = rTotal + 18; assert( 18==whereCost(7)-whereCost(2) ); |
| 109196 | | - pNew->nOut = nRow; |
| 109197 | | - pNew->prereq = prereq; |
| 109198 | | - memset(&pNew->u, 0, sizeof(pNew->u)); |
| 109370 | + pNew->rRun = sSum.a[i].rRun + 18; |
| 109371 | + pNew->nOut = sSum.a[i].nOut; |
| 109372 | + pNew->prereq = sSum.a[i].prereq; |
| 109199 | 109373 | rc = whereLoopInsert(pBuilder, pNew); |
| 109200 | 109374 | } |
| 109201 | | - whereLoopClear(pWInfo->pParse->db, &sBest); |
| 109202 | 109375 | } |
| 109203 | 109376 | } |
| 109204 | 109377 | return rc; |
| 109205 | 109378 | } |
| 109206 | 109379 | |
| | @@ -116127,10 +116300,12 @@ |
| 116127 | 116300 | } |
| 116128 | 116301 | sqlite3BtreeLeaveAll(db); |
| 116129 | 116302 | |
| 116130 | 116303 | /* Any deferred constraint violations have now been resolved. */ |
| 116131 | 116304 | db->nDeferredCons = 0; |
| 116305 | + db->nDeferredImmCons = 0; |
| 116306 | + db->flags &= ~SQLITE_DeferFKs; |
| 116132 | 116307 | |
| 116133 | 116308 | /* If one has been configured, invoke the rollback-hook callback */ |
| 116134 | 116309 | if( db->xRollbackCallback && (inTrans || !db->autoCommit) ){ |
| 116135 | 116310 | db->xRollbackCallback(db->pRollbackArg); |
| 116136 | 116311 | } |
| | @@ -116388,11 +116563,11 @@ |
| 116388 | 116563 | void *pArg |
| 116389 | 116564 | ){ |
| 116390 | 116565 | sqlite3_mutex_enter(db->mutex); |
| 116391 | 116566 | if( nOps>0 ){ |
| 116392 | 116567 | db->xProgress = xProgress; |
| 116393 | | - db->nProgressOps = nOps; |
| 116568 | + db->nProgressOps = (unsigned)nOps; |
| 116394 | 116569 | db->pProgressArg = pArg; |
| 116395 | 116570 | }else{ |
| 116396 | 116571 | db->xProgress = 0; |
| 116397 | 116572 | db->nProgressOps = 0; |
| 116398 | 116573 | db->pProgressArg = 0; |
| | @@ -119087,11 +119262,11 @@ |
| 119087 | 119262 | |
| 119088 | 119263 | #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) |
| 119089 | 119264 | |
| 119090 | 119265 | /* If not building as part of the core, include sqlite3ext.h. */ |
| 119091 | 119266 | #ifndef SQLITE_CORE |
| 119092 | | -SQLITE_API extern const sqlite3_api_routines *sqlite3_api; |
| 119267 | +SQLITE_EXTENSION_INIT3 |
| 119093 | 119268 | #endif |
| 119094 | 119269 | |
| 119095 | 119270 | /************** Include fts3_tokenizer.h in the middle of fts3Int.h **********/ |
| 119096 | 119271 | /************** Begin file fts3_tokenizer.h **********************************/ |
| 119097 | 119272 | /* |
| | @@ -124995,11 +125170,14 @@ |
| 124995 | 125170 | |
| 124996 | 125171 | #if !SQLITE_CORE |
| 124997 | 125172 | /* |
| 124998 | 125173 | ** Initialize API pointer table, if required. |
| 124999 | 125174 | */ |
| 125000 | | -SQLITE_API int sqlite3_extension_init( |
| 125175 | +#ifdef _WIN32 |
| 125176 | +__declspec(dllexport) |
| 125177 | +#endif |
| 125178 | +SQLITE_API int sqlite3_fts3_init( |
| 125001 | 125179 | sqlite3 *db, |
| 125002 | 125180 | char **pzErrMsg, |
| 125003 | 125181 | const sqlite3_api_routines *pApi |
| 125004 | 125182 | ){ |
| 125005 | 125183 | SQLITE_EXTENSION_INIT2(pApi) |
| | @@ -140042,11 +140220,14 @@ |
| 140042 | 140220 | (void *)pGeomCtx, geomCallback, 0, 0, doSqlite3Free |
| 140043 | 140221 | ); |
| 140044 | 140222 | } |
| 140045 | 140223 | |
| 140046 | 140224 | #if !SQLITE_CORE |
| 140047 | | -SQLITE_API int sqlite3_extension_init( |
| 140225 | +#ifdef _WIN32 |
| 140226 | +__declspec(dllexport) |
| 140227 | +#endif |
| 140228 | +SQLITE_API int sqlite3_rtree_init( |
| 140048 | 140229 | sqlite3 *db, |
| 140049 | 140230 | char **pzErrMsg, |
| 140050 | 140231 | const sqlite3_api_routines *pApi |
| 140051 | 140232 | ){ |
| 140052 | 140233 | SQLITE_EXTENSION_INIT2(pApi) |
| | @@ -140544,11 +140725,14 @@ |
| 140544 | 140725 | |
| 140545 | 140726 | return rc; |
| 140546 | 140727 | } |
| 140547 | 140728 | |
| 140548 | 140729 | #if !SQLITE_CORE |
| 140549 | | -SQLITE_API int sqlite3_extension_init( |
| 140730 | +#ifdef _WIN32 |
| 140731 | +__declspec(dllexport) |
| 140732 | +#endif |
| 140733 | +SQLITE_API int sqlite3_icu_init( |
| 140550 | 140734 | sqlite3 *db, |
| 140551 | 140735 | char **pzErrMsg, |
| 140552 | 140736 | const sqlite3_api_routines *pApi |
| 140553 | 140737 | ){ |
| 140554 | 140738 | SQLITE_EXTENSION_INIT2(pApi) |
| 140555 | 140739 | |