| | @@ -1150,11 +1150,11 @@ |
| 1150 | 1150 | ** [sqlite3_libversion_number()], [sqlite3_sourceid()], |
| 1151 | 1151 | ** [sqlite_version()] and [sqlite_source_id()]. |
| 1152 | 1152 | */ |
| 1153 | 1153 | #define SQLITE_VERSION "3.24.0" |
| 1154 | 1154 | #define SQLITE_VERSION_NUMBER 3024000 |
| 1155 | | -#define SQLITE_SOURCE_ID "2018-05-14 00:41:12 d0f35739af3b226c8eef39676407293650cde551acef06fe8628fdd5b59bd66a" |
| 1155 | +#define SQLITE_SOURCE_ID "2018-05-30 01:14:20 86ee267ee86f5264774a9f215b1158aeaa2d605e77c205731b5ee3945d7de4c2" |
| 1156 | 1156 | |
| 1157 | 1157 | /* |
| 1158 | 1158 | ** CAPI3REF: Run-Time Library Version Numbers |
| 1159 | 1159 | ** KEYWORDS: sqlite3_version sqlite3_sourceid |
| 1160 | 1160 | ** |
| | @@ -1529,17 +1529,19 @@ |
| 1529 | 1529 | #define SQLITE_IOERR_AUTH (SQLITE_IOERR | (28<<8)) |
| 1530 | 1530 | #define SQLITE_IOERR_BEGIN_ATOMIC (SQLITE_IOERR | (29<<8)) |
| 1531 | 1531 | #define SQLITE_IOERR_COMMIT_ATOMIC (SQLITE_IOERR | (30<<8)) |
| 1532 | 1532 | #define SQLITE_IOERR_ROLLBACK_ATOMIC (SQLITE_IOERR | (31<<8)) |
| 1533 | 1533 | #define SQLITE_LOCKED_SHAREDCACHE (SQLITE_LOCKED | (1<<8)) |
| 1534 | +#define SQLITE_LOCKED_VTAB (SQLITE_LOCKED | (2<<8)) |
| 1534 | 1535 | #define SQLITE_BUSY_RECOVERY (SQLITE_BUSY | (1<<8)) |
| 1535 | 1536 | #define SQLITE_BUSY_SNAPSHOT (SQLITE_BUSY | (2<<8)) |
| 1536 | 1537 | #define SQLITE_CANTOPEN_NOTEMPDIR (SQLITE_CANTOPEN | (1<<8)) |
| 1537 | 1538 | #define SQLITE_CANTOPEN_ISDIR (SQLITE_CANTOPEN | (2<<8)) |
| 1538 | 1539 | #define SQLITE_CANTOPEN_FULLPATH (SQLITE_CANTOPEN | (3<<8)) |
| 1539 | 1540 | #define SQLITE_CANTOPEN_CONVPATH (SQLITE_CANTOPEN | (4<<8)) |
| 1540 | 1541 | #define SQLITE_CORRUPT_VTAB (SQLITE_CORRUPT | (1<<8)) |
| 1542 | +#define SQLITE_CORRUPT_SEQUENCE (SQLITE_CORRUPT | (2<<8)) |
| 1541 | 1543 | #define SQLITE_READONLY_RECOVERY (SQLITE_READONLY | (1<<8)) |
| 1542 | 1544 | #define SQLITE_READONLY_CANTLOCK (SQLITE_READONLY | (2<<8)) |
| 1543 | 1545 | #define SQLITE_READONLY_ROLLBACK (SQLITE_READONLY | (3<<8)) |
| 1544 | 1546 | #define SQLITE_READONLY_DBMOVED (SQLITE_READONLY | (4<<8)) |
| 1545 | 1547 | #define SQLITE_READONLY_CANTINIT (SQLITE_READONLY | (5<<8)) |
| | @@ -7317,10 +7319,14 @@ |
| 7317 | 7319 | sqlite3_uint64 colUsed; /* Input: Mask of columns used by statement */ |
| 7318 | 7320 | }; |
| 7319 | 7321 | |
| 7320 | 7322 | /* |
| 7321 | 7323 | ** CAPI3REF: Virtual Table Scan Flags |
| 7324 | +** |
| 7325 | +** Virtual table implementations are allowed to set the |
| 7326 | +** [sqlite3_index_info].idxFlags field to some combination of |
| 7327 | +** these bits. |
| 7322 | 7328 | */ |
| 7323 | 7329 | #define SQLITE_INDEX_SCAN_UNIQUE 1 /* Scan visits at most 1 row */ |
| 7324 | 7330 | |
| 7325 | 7331 | /* |
| 7326 | 7332 | ** CAPI3REF: Virtual Table Constraint Operator Codes |
| | @@ -8180,15 +8186,23 @@ |
| 8180 | 8186 | /* |
| 8181 | 8187 | ** CAPI3REF: Create A New Dynamic String Object |
| 8182 | 8188 | ** CONSTRUCTOR: sqlite3_str |
| 8183 | 8189 | ** |
| 8184 | 8190 | ** ^The [sqlite3_str_new(D)] interface allocates and initializes |
| 8185 | | -** a new [sqlite3_str] |
| 8186 | | -** object. ^The [sqlite3_str_new()] interface returns NULL on an out-of-memory |
| 8187 | | -** condition. To avoid memory leaks, the object returned by |
| 8191 | +** a new [sqlite3_str] object. To avoid memory leaks, the object returned by |
| 8188 | 8192 | ** [sqlite3_str_new()] must be freed by a subsequent call to |
| 8189 | 8193 | ** [sqlite3_str_finish(X)]. |
| 8194 | +** |
| 8195 | +** ^The [sqlite3_str_new(D)] interface always returns a pointer to a |
| 8196 | +** valid [sqlite3_str] object, though in the event of an out-of-memory |
| 8197 | +** error the returned object might be a special singleton that will |
| 8198 | +** silently reject new text, always return SQLITE_NOMEM from |
| 8199 | +** [sqlite3_str_errcode()], always return 0 for |
| 8200 | +** [sqlite3_str_length()], and always return NULL from |
| 8201 | +** [sqlite3_str_finish(X)]. It is always safe to use the value |
| 8202 | +** returned by [sqlite3_str_new(D)] as the sqlite3_str parameter |
| 8203 | +** to any of the other [sqlite3_str] methods. |
| 8190 | 8204 | ** |
| 8191 | 8205 | ** The D parameter to [sqlite3_str_new(D)] may be NULL. If the |
| 8192 | 8206 | ** D parameter in [sqlite3_str_new(D)] is not NULL, then the maximum |
| 8193 | 8207 | ** length of the string contained in the [sqlite3_str] object will be |
| 8194 | 8208 | ** the value set for [sqlite3_limit](D,[SQLITE_LIMIT_LENGTH]) instead |
| | @@ -9550,15 +9564,15 @@ |
| 9550 | 9564 | ** |
| 9551 | 9565 | ** If the sqlite3_vtab_nochange(X) routine is called within the [xColumn] |
| 9552 | 9566 | ** method of a [virtual table], then it returns true if and only if the |
| 9553 | 9567 | ** column is being fetched as part of an UPDATE operation during which the |
| 9554 | 9568 | ** column value will not change. Applications might use this to substitute |
| 9555 | | -** a lighter-weight value to return that the corresponding [xUpdate] method |
| 9556 | | -** understands as a "no-change" value. |
| 9569 | +** a return value that is less expensive to compute and that the corresponding |
| 9570 | +** [xUpdate] method understands as a "no-change" value. |
| 9557 | 9571 | ** |
| 9558 | 9572 | ** If the [xColumn] method calls sqlite3_vtab_nochange() and finds that |
| 9559 | | -** the column is not changed by the UPDATE statement, they the xColumn |
| 9573 | +** the column is not changed by the UPDATE statement, then the xColumn |
| 9560 | 9574 | ** method can optionally return without setting a result, without calling |
| 9561 | 9575 | ** any of the [sqlite3_result_int|sqlite3_result_xxxxx() interfaces]. |
| 9562 | 9576 | ** In that case, [sqlite3_value_nochange(X)] will return true for the |
| 9563 | 9577 | ** same column in the [xUpdate] method. |
| 9564 | 9578 | */ |
| | @@ -14632,10 +14646,11 @@ |
| 14632 | 14646 | #define OP_Function 166 /* synopsis: r[P3]=func(r[P2@P5]) */ |
| 14633 | 14647 | #define OP_Trace 167 |
| 14634 | 14648 | #define OP_CursorHint 168 |
| 14635 | 14649 | #define OP_Noop 169 |
| 14636 | 14650 | #define OP_Explain 170 |
| 14651 | +#define OP_Abortable 171 |
| 14637 | 14652 | |
| 14638 | 14653 | /* Properties such as "out2" or "jump" that are specified in |
| 14639 | 14654 | ** comments following the "case" for each opcode in the vdbe.c |
| 14640 | 14655 | ** are encoded into bitvectors as follows: |
| 14641 | 14656 | */ |
| | @@ -14665,11 +14680,11 @@ |
| 14665 | 14680 | /* 128 */ 0x04, 0x04, 0x00, 0x00, 0x10, 0x10, 0x10, 0x00,\ |
| 14666 | 14681 | /* 136 */ 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\ |
| 14667 | 14682 | /* 144 */ 0x00, 0x06, 0x10, 0x00, 0x04, 0x1a, 0x00, 0x00,\ |
| 14668 | 14683 | /* 152 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\ |
| 14669 | 14684 | /* 160 */ 0x00, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,\ |
| 14670 | | -/* 168 */ 0x00, 0x00, 0x00,} |
| 14685 | +/* 168 */ 0x00, 0x00, 0x00, 0x00,} |
| 14671 | 14686 | |
| 14672 | 14687 | /* The sqlite3P2Values() routine is able to run faster if it knows |
| 14673 | 14688 | ** the value of the largest JUMP opcode. The smaller the maximum |
| 14674 | 14689 | ** JUMP opcode the better, so the mkopcodeh.tcl script that |
| 14675 | 14690 | ** generated this include file strives to group all JUMP opcodes |
| | @@ -14707,10 +14722,15 @@ |
| 14707 | 14722 | SQLITE_PRIVATE void sqlite3VdbeVerifyNoResultRow(Vdbe *p); |
| 14708 | 14723 | #else |
| 14709 | 14724 | # define sqlite3VdbeVerifyNoMallocRequired(A,B) |
| 14710 | 14725 | # define sqlite3VdbeVerifyNoResultRow(A) |
| 14711 | 14726 | #endif |
| 14727 | +#if defined(SQLITE_DEBUG) |
| 14728 | +SQLITE_PRIVATE void sqlite3VdbeVerifyAbortable(Vdbe *p, int); |
| 14729 | +#else |
| 14730 | +# define sqlite3VdbeVerifyAbortable(A,B) |
| 14731 | +#endif |
| 14712 | 14732 | SQLITE_PRIVATE VdbeOp *sqlite3VdbeAddOpList(Vdbe*, int nOp, VdbeOpList const *aOp,int iLineno); |
| 14713 | 14733 | #ifndef SQLITE_OMIT_EXPLAIN |
| 14714 | 14734 | SQLITE_PRIVATE void sqlite3VdbeExplain(Parse*,u8,const char*,...); |
| 14715 | 14735 | SQLITE_PRIVATE void sqlite3VdbeExplainPop(Parse*); |
| 14716 | 14736 | SQLITE_PRIVATE int sqlite3VdbeExplainParent(Parse*); |
| | @@ -18683,10 +18703,11 @@ |
| 18683 | 18703 | SQLITE_PRIVATE int sqlite3CreateFunc(sqlite3 *, const char *, int, int, void *, |
| 18684 | 18704 | void (*)(sqlite3_context*,int,sqlite3_value **), |
| 18685 | 18705 | void (*)(sqlite3_context*,int,sqlite3_value **), void (*)(sqlite3_context*), |
| 18686 | 18706 | FuncDestructor *pDestructor |
| 18687 | 18707 | ); |
| 18708 | +SQLITE_PRIVATE void sqlite3NoopDestructor(void*); |
| 18688 | 18709 | SQLITE_PRIVATE void sqlite3OomFault(sqlite3*); |
| 18689 | 18710 | SQLITE_PRIVATE void sqlite3OomClear(sqlite3*); |
| 18690 | 18711 | SQLITE_PRIVATE int sqlite3ApiExit(sqlite3 *db, int); |
| 18691 | 18712 | SQLITE_PRIVATE int sqlite3OpenTempDatabase(Parse *); |
| 18692 | 18713 | |
| | @@ -18785,11 +18806,10 @@ |
| 18785 | 18806 | SQLITE_PRIVATE int sqlite3VtabCallCreate(sqlite3*, int, const char *, char **); |
| 18786 | 18807 | SQLITE_PRIVATE int sqlite3VtabCallConnect(Parse*, Table*); |
| 18787 | 18808 | SQLITE_PRIVATE int sqlite3VtabCallDestroy(sqlite3*, int, const char *); |
| 18788 | 18809 | SQLITE_PRIVATE int sqlite3VtabBegin(sqlite3 *, VTable *); |
| 18789 | 18810 | SQLITE_PRIVATE FuncDef *sqlite3VtabOverloadFunction(sqlite3 *,FuncDef*, int nArg, Expr*); |
| 18790 | | -SQLITE_PRIVATE void sqlite3InvalidFunction(sqlite3_context*,int,sqlite3_value**); |
| 18791 | 18811 | SQLITE_PRIVATE sqlite3_int64 sqlite3StmtCurrentTime(sqlite3_context*); |
| 18792 | 18812 | SQLITE_PRIVATE int sqlite3VdbeParameterIndex(Vdbe*, const char*, int); |
| 18793 | 18813 | SQLITE_PRIVATE int sqlite3TransferBindings(sqlite3_stmt *, sqlite3_stmt *); |
| 18794 | 18814 | SQLITE_PRIVATE void sqlite3ParserReset(Parse*); |
| 18795 | 18815 | SQLITE_PRIVATE int sqlite3Reprepare(Vdbe*); |
| | @@ -19715,10 +19735,11 @@ |
| 19715 | 19735 | i64 startTime; /* Time when query started - used for profiling */ |
| 19716 | 19736 | #endif |
| 19717 | 19737 | int nOp; /* Number of instructions in the program */ |
| 19718 | 19738 | #ifdef SQLITE_DEBUG |
| 19719 | 19739 | int rcApp; /* errcode set by sqlite3_result_error_code() */ |
| 19740 | + u32 nWrite; /* Number of write operations that have occurred */ |
| 19720 | 19741 | #endif |
| 19721 | 19742 | u16 nResColumn; /* Number of columns in one row of the result set */ |
| 19722 | 19743 | u8 errorAction; /* Recovery action to do in case of an error */ |
| 19723 | 19744 | u8 minWriteFileFormat; /* Minimum file format for writable database files */ |
| 19724 | 19745 | u8 prepFlags; /* SQLITE_PREPARE_* flags */ |
| | @@ -19849,10 +19870,18 @@ |
| 19849 | 19870 | SQLITE_PRIVATE int sqlite3VdbeSorterRowkey(const VdbeCursor *, Mem *); |
| 19850 | 19871 | SQLITE_PRIVATE int sqlite3VdbeSorterNext(sqlite3 *, const VdbeCursor *); |
| 19851 | 19872 | SQLITE_PRIVATE int sqlite3VdbeSorterRewind(const VdbeCursor *, int *); |
| 19852 | 19873 | SQLITE_PRIVATE int sqlite3VdbeSorterWrite(const VdbeCursor *, Mem *); |
| 19853 | 19874 | SQLITE_PRIVATE int sqlite3VdbeSorterCompare(const VdbeCursor *, Mem *, int, int *); |
| 19875 | + |
| 19876 | +#ifdef SQLITE_DEBUG |
| 19877 | +SQLITE_PRIVATE void sqlite3VdbeIncrWriteCounter(Vdbe*, VdbeCursor*); |
| 19878 | +SQLITE_PRIVATE void sqlite3VdbeAssertAbortable(Vdbe*); |
| 19879 | +#else |
| 19880 | +# define sqlite3VdbeIncrWriteCounter(V,C) |
| 19881 | +# define sqlite3VdbeAssertAbortable(V) |
| 19882 | +#endif |
| 19854 | 19883 | |
| 19855 | 19884 | #if !defined(SQLITE_OMIT_SHARED_CACHE) |
| 19856 | 19885 | SQLITE_PRIVATE void sqlite3VdbeEnter(Vdbe*); |
| 19857 | 19886 | #else |
| 19858 | 19887 | # define sqlite3VdbeEnter(X) |
| | @@ -27537,16 +27566,26 @@ |
| 27537 | 27566 | return strAccumFinishRealloc(p); |
| 27538 | 27567 | } |
| 27539 | 27568 | } |
| 27540 | 27569 | return p->zText; |
| 27541 | 27570 | } |
| 27571 | + |
| 27572 | +/* |
| 27573 | +** This singleton is an sqlite3_str object that is returned if |
| 27574 | +** sqlite3_malloc() fails to provide space for a real one. This |
| 27575 | +** sqlite3_str object accepts no new text and always returns |
| 27576 | +** an SQLITE_NOMEM error. |
| 27577 | +*/ |
| 27578 | +static sqlite3_str sqlite3OomStr = { |
| 27579 | + 0, 0, 0, 0, 0, SQLITE_NOMEM, 0 |
| 27580 | +}; |
| 27542 | 27581 | |
| 27543 | 27582 | /* Finalize a string created using sqlite3_str_new(). |
| 27544 | 27583 | */ |
| 27545 | 27584 | SQLITE_API char *sqlite3_str_finish(sqlite3_str *p){ |
| 27546 | 27585 | char *z; |
| 27547 | | - if( p ){ |
| 27586 | + if( p!=0 && p!=&sqlite3OomStr ){ |
| 27548 | 27587 | z = sqlite3StrAccumFinish(p); |
| 27549 | 27588 | sqlite3_free(p); |
| 27550 | 27589 | }else{ |
| 27551 | 27590 | z = 0; |
| 27552 | 27591 | } |
| | @@ -27611,10 +27650,12 @@ |
| 27611 | 27650 | SQLITE_API sqlite3_str *sqlite3_str_new(sqlite3 *db){ |
| 27612 | 27651 | sqlite3_str *p = sqlite3_malloc64(sizeof(*p)); |
| 27613 | 27652 | if( p ){ |
| 27614 | 27653 | sqlite3StrAccumInit(p, 0, 0, 0, |
| 27615 | 27654 | db ? db->aLimit[SQLITE_LIMIT_LENGTH] : SQLITE_MAX_LENGTH); |
| 27655 | + }else{ |
| 27656 | + p = &sqlite3OomStr; |
| 27616 | 27657 | } |
| 27617 | 27658 | return p; |
| 27618 | 27659 | } |
| 27619 | 27660 | |
| 27620 | 27661 | /* |
| | @@ -31409,10 +31450,11 @@ |
| 31409 | 31450 | /* 166 */ "Function" OpHelp("r[P3]=func(r[P2@P5])"), |
| 31410 | 31451 | /* 167 */ "Trace" OpHelp(""), |
| 31411 | 31452 | /* 168 */ "CursorHint" OpHelp(""), |
| 31412 | 31453 | /* 169 */ "Noop" OpHelp(""), |
| 31413 | 31454 | /* 170 */ "Explain" OpHelp(""), |
| 31455 | + /* 171 */ "Abortable" OpHelp(""), |
| 31414 | 31456 | }; |
| 31415 | 31457 | return azName[i]; |
| 31416 | 31458 | } |
| 31417 | 31459 | #endif |
| 31418 | 31460 | |
| | @@ -73732,11 +73774,11 @@ |
| 73732 | 73774 | pMem->flags = MEM_Int; |
| 73733 | 73775 | } |
| 73734 | 73776 | } |
| 73735 | 73777 | |
| 73736 | 73778 | /* A no-op destructor */ |
| 73737 | | -static void sqlite3NoopDestructor(void *p){ UNUSED_PARAMETER(p); } |
| 73779 | +SQLITE_PRIVATE void sqlite3NoopDestructor(void *p){ UNUSED_PARAMETER(p); } |
| 73738 | 73780 | |
| 73739 | 73781 | /* |
| 73740 | 73782 | ** Set the value stored in *pMem should already be a NULL. |
| 73741 | 73783 | ** Also store a pointer to go with it. |
| 73742 | 73784 | */ |
| | @@ -75378,10 +75420,36 @@ |
| 75378 | 75420 | return ( v->db->mallocFailed || hasAbort==mayAbort || hasFkCounter |
| 75379 | 75421 | || (hasCreateTable && hasInitCoroutine) ); |
| 75380 | 75422 | } |
| 75381 | 75423 | #endif /* SQLITE_DEBUG - the sqlite3AssertMayAbort() function */ |
| 75382 | 75424 | |
| 75425 | +#ifdef SQLITE_DEBUG |
| 75426 | +/* |
| 75427 | +** Increment the nWrite counter in the VDBE if the cursor is not an |
| 75428 | +** ephemeral cursor, or if the cursor argument is NULL. |
| 75429 | +*/ |
| 75430 | +SQLITE_PRIVATE void sqlite3VdbeIncrWriteCounter(Vdbe *p, VdbeCursor *pC){ |
| 75431 | + if( pC==0 |
| 75432 | + || (pC->eCurType!=CURTYPE_SORTER |
| 75433 | + && pC->eCurType!=CURTYPE_PSEUDO |
| 75434 | + && !pC->isEphemeral) |
| 75435 | + ){ |
| 75436 | + p->nWrite++; |
| 75437 | + } |
| 75438 | +} |
| 75439 | +#endif |
| 75440 | + |
| 75441 | +#ifdef SQLITE_DEBUG |
| 75442 | +/* |
| 75443 | +** Assert if an Abort at this point in time might result in a corrupt |
| 75444 | +** database. |
| 75445 | +*/ |
| 75446 | +SQLITE_PRIVATE void sqlite3VdbeAssertAbortable(Vdbe *p){ |
| 75447 | + assert( p->nWrite==0 || p->usesStmtJournal ); |
| 75448 | +} |
| 75449 | +#endif |
| 75450 | + |
| 75383 | 75451 | /* |
| 75384 | 75452 | ** This routine is called after all opcodes have been inserted. It loops |
| 75385 | 75453 | ** through all the opcodes and fixes up some details. |
| 75386 | 75454 | ** |
| 75387 | 75455 | ** (1) For each jump instruction with a negative P2 value (a label) |
| | @@ -75537,10 +75605,21 @@ |
| 75537 | 75605 | assert( p->aOp[i].opcode!=OP_ResultRow ); |
| 75538 | 75606 | } |
| 75539 | 75607 | } |
| 75540 | 75608 | #endif |
| 75541 | 75609 | |
| 75610 | +/* |
| 75611 | +** Generate code (a single OP_Abortable opcode) that will |
| 75612 | +** verify that the VDBE program can safely call Abort in the current |
| 75613 | +** context. |
| 75614 | +*/ |
| 75615 | +#if defined(SQLITE_DEBUG) |
| 75616 | +SQLITE_PRIVATE void sqlite3VdbeVerifyAbortable(Vdbe *p, int onError){ |
| 75617 | + if( onError==OE_Abort ) sqlite3VdbeAddOp0(p, OP_Abortable); |
| 75618 | +} |
| 75619 | +#endif |
| 75620 | + |
| 75542 | 75621 | /* |
| 75543 | 75622 | ** This function returns a pointer to the array of opcodes associated with |
| 75544 | 75623 | ** the Vdbe passed as the first argument. It is the callers responsibility |
| 75545 | 75624 | ** to arrange for the returned array to be eventually freed using the |
| 75546 | 75625 | ** vdbeFreeOpArray() function. |
| | @@ -77770,10 +77849,13 @@ |
| 77770 | 77849 | } |
| 77771 | 77850 | #endif |
| 77772 | 77851 | sqlite3DbFree(db, p->zErrMsg); |
| 77773 | 77852 | p->zErrMsg = 0; |
| 77774 | 77853 | p->pResultSet = 0; |
| 77854 | +#ifdef SQLITE_DEBUG |
| 77855 | + p->nWrite = 0; |
| 77856 | +#endif |
| 77775 | 77857 | |
| 77776 | 77858 | /* Save profiling information from this VDBE run. |
| 77777 | 77859 | */ |
| 77778 | 77860 | #ifdef VDBE_PROFILE |
| 77779 | 77861 | { |
| | @@ -78692,17 +78774,14 @@ |
| 78692 | 78774 | return 0; |
| 78693 | 78775 | }else{ |
| 78694 | 78776 | i64 y; |
| 78695 | 78777 | double s; |
| 78696 | 78778 | if( r<-9223372036854775808.0 ) return +1; |
| 78697 | | - if( r>9223372036854775807.0 ) return -1; |
| 78779 | + if( r>=9223372036854775808.0 ) return -1; |
| 78698 | 78780 | y = (i64)r; |
| 78699 | 78781 | if( i<y ) return -1; |
| 78700 | | - if( i>y ){ |
| 78701 | | - if( y==SMALLEST_INT64 && r>0.0 ) return -1; |
| 78702 | | - return +1; |
| 78703 | | - } |
| 78782 | + if( i>y ) return +1; |
| 78704 | 78783 | s = (double)i; |
| 78705 | 78784 | if( s<r ) return -1; |
| 78706 | 78785 | if( s>r ) return +1; |
| 78707 | 78786 | return 0; |
| 78708 | 78787 | } |
| | @@ -80376,32 +80455,10 @@ |
| 80376 | 80455 | if( rc ) *piTime = 0; |
| 80377 | 80456 | } |
| 80378 | 80457 | return *piTime; |
| 80379 | 80458 | } |
| 80380 | 80459 | |
| 80381 | | -/* |
| 80382 | | -** The following is the implementation of an SQL function that always |
| 80383 | | -** fails with an error message stating that the function is used in the |
| 80384 | | -** wrong context. The sqlite3_overload_function() API might construct |
| 80385 | | -** SQL function that use this routine so that the functions will exist |
| 80386 | | -** for name resolution but are actually overloaded by the xFindFunction |
| 80387 | | -** method of virtual tables. |
| 80388 | | -*/ |
| 80389 | | -SQLITE_PRIVATE void sqlite3InvalidFunction( |
| 80390 | | - sqlite3_context *context, /* The function calling context */ |
| 80391 | | - int NotUsed, /* Number of arguments to the function */ |
| 80392 | | - sqlite3_value **NotUsed2 /* Value of each argument */ |
| 80393 | | -){ |
| 80394 | | - const char *zName = context->pFunc->zName; |
| 80395 | | - char *zErr; |
| 80396 | | - UNUSED_PARAMETER2(NotUsed, NotUsed2); |
| 80397 | | - zErr = sqlite3_mprintf( |
| 80398 | | - "unable to use function %s in the requested context", zName); |
| 80399 | | - sqlite3_result_error(context, zErr, -1); |
| 80400 | | - sqlite3_free(zErr); |
| 80401 | | -} |
| 80402 | | - |
| 80403 | 80460 | /* |
| 80404 | 80461 | ** Create a new aggregate context for p and return a pointer to |
| 80405 | 80462 | ** its pMem->z element. |
| 80406 | 80463 | */ |
| 80407 | 80464 | static SQLITE_NOINLINE void *createAggContext(sqlite3_context *p, int nByte){ |
| | @@ -82773,10 +82830,13 @@ |
| 82773 | 82830 | ** value in register P3 is not NULL, then this routine is a no-op. |
| 82774 | 82831 | ** The P5 parameter should be 1. |
| 82775 | 82832 | */ |
| 82776 | 82833 | case OP_HaltIfNull: { /* in3 */ |
| 82777 | 82834 | pIn3 = &aMem[pOp->p3]; |
| 82835 | +#ifdef SQLITE_DEBUG |
| 82836 | + if( pOp->p2==OE_Abort ){ sqlite3VdbeAssertAbortable(p); } |
| 82837 | +#endif |
| 82778 | 82838 | if( (pIn3->flags & MEM_Null)==0 ) break; |
| 82779 | 82839 | /* Fall through into OP_Halt */ |
| 82780 | 82840 | } |
| 82781 | 82841 | |
| 82782 | 82842 | /* Opcode: Halt P1 P2 * P4 P5 |
| | @@ -82812,10 +82872,13 @@ |
| 82812 | 82872 | case OP_Halt: { |
| 82813 | 82873 | VdbeFrame *pFrame; |
| 82814 | 82874 | int pcx; |
| 82815 | 82875 | |
| 82816 | 82876 | pcx = (int)(pOp - aOp); |
| 82877 | +#ifdef SQLITE_DEBUG |
| 82878 | + if( pOp->p2==OE_Abort ){ sqlite3VdbeAssertAbortable(p); } |
| 82879 | +#endif |
| 82817 | 82880 | if( pOp->p1==SQLITE_OK && p->pFrame ){ |
| 82818 | 82881 | /* Halt the sub-program. Return control to the parent frame. */ |
| 82819 | 82882 | pFrame = p->pFrame; |
| 82820 | 82883 | p->pFrame = pFrame->pParent; |
| 82821 | 82884 | p->nFrame--; |
| | @@ -85182,10 +85245,12 @@ |
| 85182 | 85245 | ** |
| 85183 | 85246 | ** A transaction must be started before executing this opcode. |
| 85184 | 85247 | */ |
| 85185 | 85248 | case OP_SetCookie: { |
| 85186 | 85249 | Db *pDb; |
| 85250 | + |
| 85251 | + sqlite3VdbeIncrWriteCounter(p, 0); |
| 85187 | 85252 | assert( pOp->p2<SQLITE_N_BTREE_META ); |
| 85188 | 85253 | assert( pOp->p1>=0 && pOp->p1<db->nDb ); |
| 85189 | 85254 | assert( DbMaskTest(p->btreeMask, pOp->p1) ); |
| 85190 | 85255 | assert( p->readOnly==0 ); |
| 85191 | 85256 | pDb = &db->aDb[pOp->p1]; |
| | @@ -86145,15 +86210,12 @@ |
| 86145 | 86210 | v = 0; |
| 86146 | 86211 | res = 0; |
| 86147 | 86212 | pOut = out2Prerelease(p, pOp); |
| 86148 | 86213 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 86149 | 86214 | pC = p->apCsr[pOp->p1]; |
| 86150 | | - if( !pC->isTable ){ |
| 86151 | | - rc = SQLITE_CORRUPT_BKPT; |
| 86152 | | - goto abort_due_to_error; |
| 86153 | | - } |
| 86154 | 86215 | assert( pC!=0 ); |
| 86216 | + assert( pC->isTable ); |
| 86155 | 86217 | assert( pC->eCurType==CURTYPE_BTREE ); |
| 86156 | 86218 | assert( pC->uc.pCursor!=0 ); |
| 86157 | 86219 | { |
| 86158 | 86220 | /* The next rowid or record number (different terms for the same |
| 86159 | 86221 | ** thing) is obtained in a two-step algorithm. |
| | @@ -86318,10 +86380,11 @@ |
| 86318 | 86380 | assert( pC->eCurType==CURTYPE_BTREE ); |
| 86319 | 86381 | assert( pC->uc.pCursor!=0 ); |
| 86320 | 86382 | assert( (pOp->p5 & OPFLAG_ISNOOP) || pC->isTable ); |
| 86321 | 86383 | assert( pOp->p4type==P4_TABLE || pOp->p4type>=P4_STATIC ); |
| 86322 | 86384 | REGISTER_TRACE(pOp->p2, pData); |
| 86385 | + sqlite3VdbeIncrWriteCounter(p, pC); |
| 86323 | 86386 | |
| 86324 | 86387 | if( pOp->opcode==OP_Insert ){ |
| 86325 | 86388 | pKey = &aMem[pOp->p3]; |
| 86326 | 86389 | assert( pKey->flags & MEM_Int ); |
| 86327 | 86390 | assert( memIsValid(pKey) ); |
| | @@ -86432,10 +86495,11 @@ |
| 86432 | 86495 | pC = p->apCsr[pOp->p1]; |
| 86433 | 86496 | assert( pC!=0 ); |
| 86434 | 86497 | assert( pC->eCurType==CURTYPE_BTREE ); |
| 86435 | 86498 | assert( pC->uc.pCursor!=0 ); |
| 86436 | 86499 | assert( pC->deferredMoveto==0 ); |
| 86500 | + sqlite3VdbeIncrWriteCounter(p, pC); |
| 86437 | 86501 | |
| 86438 | 86502 | #ifdef SQLITE_DEBUG |
| 86439 | 86503 | if( pOp->p4type==P4_TABLE && HasRowid(pOp->p4.pTab) && pOp->p5==0 ){ |
| 86440 | 86504 | /* If p5 is zero, the seek operation that positioned the cursor prior to |
| 86441 | 86505 | ** OP_Delete will have also set the pC->movetoTarget field to the rowid of |
| | @@ -87050,10 +87114,11 @@ |
| 87050 | 87114 | VdbeCursor *pC; |
| 87051 | 87115 | BtreePayload x; |
| 87052 | 87116 | |
| 87053 | 87117 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 87054 | 87118 | pC = p->apCsr[pOp->p1]; |
| 87119 | + sqlite3VdbeIncrWriteCounter(p, pC); |
| 87055 | 87120 | assert( pC!=0 ); |
| 87056 | 87121 | assert( isSorter(pC)==(pOp->opcode==OP_SorterInsert) ); |
| 87057 | 87122 | pIn2 = &aMem[pOp->p2]; |
| 87058 | 87123 | assert( pIn2->flags & MEM_Blob ); |
| 87059 | 87124 | if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++; |
| | @@ -87096,10 +87161,11 @@ |
| 87096 | 87161 | assert( pOp->p2>0 && pOp->p2+pOp->p3<=(p->nMem+1 - p->nCursor)+1 ); |
| 87097 | 87162 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 87098 | 87163 | pC = p->apCsr[pOp->p1]; |
| 87099 | 87164 | assert( pC!=0 ); |
| 87100 | 87165 | assert( pC->eCurType==CURTYPE_BTREE ); |
| 87166 | + sqlite3VdbeIncrWriteCounter(p, pC); |
| 87101 | 87167 | pCrsr = pC->uc.pCursor; |
| 87102 | 87168 | assert( pCrsr!=0 ); |
| 87103 | 87169 | assert( pOp->p5==0 ); |
| 87104 | 87170 | r.pKeyInfo = pC->pKeyInfo; |
| 87105 | 87171 | r.nField = (u16)pOp->p3; |
| | @@ -87318,10 +87384,11 @@ |
| 87318 | 87384 | */ |
| 87319 | 87385 | case OP_Destroy: { /* out2 */ |
| 87320 | 87386 | int iMoved; |
| 87321 | 87387 | int iDb; |
| 87322 | 87388 | |
| 87389 | + sqlite3VdbeIncrWriteCounter(p, 0); |
| 87323 | 87390 | assert( p->readOnly==0 ); |
| 87324 | 87391 | assert( pOp->p1>1 ); |
| 87325 | 87392 | pOut = out2Prerelease(p, pOp); |
| 87326 | 87393 | pOut->flags = MEM_Null; |
| 87327 | 87394 | if( db->nVdbeRead > db->nVDestroy+1 ){ |
| | @@ -87367,10 +87434,11 @@ |
| 87367 | 87434 | ** See also: Destroy |
| 87368 | 87435 | */ |
| 87369 | 87436 | case OP_Clear: { |
| 87370 | 87437 | int nChange; |
| 87371 | 87438 | |
| 87439 | + sqlite3VdbeIncrWriteCounter(p, 0); |
| 87372 | 87440 | nChange = 0; |
| 87373 | 87441 | assert( p->readOnly==0 ); |
| 87374 | 87442 | assert( DbMaskTest(p->btreeMask, pOp->p2) ); |
| 87375 | 87443 | rc = sqlite3BtreeClearTable( |
| 87376 | 87444 | db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &nChange : 0) |
| | @@ -87423,10 +87491,11 @@ |
| 87423 | 87491 | */ |
| 87424 | 87492 | case OP_CreateBtree: { /* out2 */ |
| 87425 | 87493 | int pgno; |
| 87426 | 87494 | Db *pDb; |
| 87427 | 87495 | |
| 87496 | + sqlite3VdbeIncrWriteCounter(p, 0); |
| 87428 | 87497 | pOut = out2Prerelease(p, pOp); |
| 87429 | 87498 | pgno = 0; |
| 87430 | 87499 | assert( pOp->p3==BTREE_INTKEY || pOp->p3==BTREE_BLOBKEY ); |
| 87431 | 87500 | assert( pOp->p1>=0 && pOp->p1<db->nDb ); |
| 87432 | 87501 | assert( DbMaskTest(p->btreeMask, pOp->p1) ); |
| | @@ -87442,10 +87511,11 @@ |
| 87442 | 87511 | /* Opcode: SqlExec * * * P4 * |
| 87443 | 87512 | ** |
| 87444 | 87513 | ** Run the SQL statement or statements specified in the P4 string. |
| 87445 | 87514 | */ |
| 87446 | 87515 | case OP_SqlExec: { |
| 87516 | + sqlite3VdbeIncrWriteCounter(p, 0); |
| 87447 | 87517 | db->nSqlExec++; |
| 87448 | 87518 | rc = sqlite3_exec(db, pOp->p4.z, 0, 0, 0); |
| 87449 | 87519 | db->nSqlExec--; |
| 87450 | 87520 | if( rc ) goto abort_due_to_error; |
| 87451 | 87521 | break; |
| | @@ -87531,10 +87601,11 @@ |
| 87531 | 87601 | ** is dropped from disk (using the Destroy opcode) in order to keep |
| 87532 | 87602 | ** the internal representation of the |
| 87533 | 87603 | ** schema consistent with what is on disk. |
| 87534 | 87604 | */ |
| 87535 | 87605 | case OP_DropTable: { |
| 87606 | + sqlite3VdbeIncrWriteCounter(p, 0); |
| 87536 | 87607 | sqlite3UnlinkAndDeleteTable(db, pOp->p1, pOp->p4.z); |
| 87537 | 87608 | break; |
| 87538 | 87609 | } |
| 87539 | 87610 | |
| 87540 | 87611 | /* Opcode: DropIndex P1 * * P4 * |
| | @@ -87544,10 +87615,11 @@ |
| 87544 | 87615 | ** is dropped from disk (using the Destroy opcode) |
| 87545 | 87616 | ** in order to keep the internal representation of the |
| 87546 | 87617 | ** schema consistent with what is on disk. |
| 87547 | 87618 | */ |
| 87548 | 87619 | case OP_DropIndex: { |
| 87620 | + sqlite3VdbeIncrWriteCounter(p, 0); |
| 87549 | 87621 | sqlite3UnlinkAndDeleteIndex(db, pOp->p1, pOp->p4.z); |
| 87550 | 87622 | break; |
| 87551 | 87623 | } |
| 87552 | 87624 | |
| 87553 | 87625 | /* Opcode: DropTrigger P1 * * P4 * |
| | @@ -87557,10 +87629,11 @@ |
| 87557 | 87629 | ** is dropped from disk (using the Destroy opcode) in order to keep |
| 87558 | 87630 | ** the internal representation of the |
| 87559 | 87631 | ** schema consistent with what is on disk. |
| 87560 | 87632 | */ |
| 87561 | 87633 | case OP_DropTrigger: { |
| 87634 | + sqlite3VdbeIncrWriteCounter(p, 0); |
| 87562 | 87635 | sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p4.z); |
| 87563 | 87636 | break; |
| 87564 | 87637 | } |
| 87565 | 87638 | |
| 87566 | 87639 | |
| | @@ -88603,11 +88676,11 @@ |
| 88603 | 88676 | ** the current row of the virtual-table of cursor P1. |
| 88604 | 88677 | ** |
| 88605 | 88678 | ** If the VColumn opcode is being used to fetch the value of |
| 88606 | 88679 | ** an unchanging column during an UPDATE operation, then the P5 |
| 88607 | 88680 | ** value is 1. Otherwise, P5 is 0. The P5 value is returned |
| 88608 | | -** by sqlite3_vtab_nochange() routine can can be used |
| 88681 | +** by sqlite3_vtab_nochange() routine and can be used |
| 88609 | 88682 | ** by virtual table implementations to return special "no-change" |
| 88610 | 88683 | ** marks which can be more efficient, depending on the virtual table. |
| 88611 | 88684 | */ |
| 88612 | 88685 | case OP_VColumn: { |
| 88613 | 88686 | sqlite3_vtab *pVtab; |
| | @@ -88766,10 +88839,11 @@ |
| 88766 | 88839 | |
| 88767 | 88840 | assert( pOp->p2==1 || pOp->p5==OE_Fail || pOp->p5==OE_Rollback |
| 88768 | 88841 | || pOp->p5==OE_Abort || pOp->p5==OE_Ignore || pOp->p5==OE_Replace |
| 88769 | 88842 | ); |
| 88770 | 88843 | assert( p->readOnly==0 ); |
| 88844 | + sqlite3VdbeIncrWriteCounter(p, 0); |
| 88771 | 88845 | pVtab = pOp->p4.pVtab->pVtab; |
| 88772 | 88846 | if( pVtab==0 || NEVER(pVtab->pModule==0) ){ |
| 88773 | 88847 | rc = SQLITE_LOCKED; |
| 88774 | 88848 | goto abort_due_to_error; |
| 88775 | 88849 | } |
| | @@ -89082,10 +89156,26 @@ |
| 89082 | 89156 | pOp->p4.pExpr, aMem); |
| 89083 | 89157 | } |
| 89084 | 89158 | break; |
| 89085 | 89159 | } |
| 89086 | 89160 | #endif /* SQLITE_ENABLE_CURSOR_HINTS */ |
| 89161 | + |
| 89162 | +#ifdef SQLITE_DEBUG |
| 89163 | +/* Opcode: Abortable * * * * * |
| 89164 | +** |
| 89165 | +** Verify that an Abort can happen. Assert if an Abort at this point |
| 89166 | +** might cause database corruption. This opcode only appears in debugging |
| 89167 | +** builds. |
| 89168 | +** |
| 89169 | +** An Abort is safe if either there have been no writes, or if there is |
| 89170 | +** an active statement journal. |
| 89171 | +*/ |
| 89172 | +case OP_Abortable: { |
| 89173 | + sqlite3VdbeAssertAbortable(p); |
| 89174 | + break; |
| 89175 | +} |
| 89176 | +#endif |
| 89087 | 89177 | |
| 89088 | 89178 | /* Opcode: Noop * * * * * |
| 89089 | 89179 | ** |
| 89090 | 89180 | ** Do nothing. This instruction is often useful as a jump |
| 89091 | 89181 | ** destination. |
| | @@ -89094,12 +89184,13 @@ |
| 89094 | 89184 | ** The magic Explain opcode are only inserted when explain==2 (which |
| 89095 | 89185 | ** is to say when the EXPLAIN QUERY PLAN syntax is used.) |
| 89096 | 89186 | ** This opcode records information from the optimizer. It is the |
| 89097 | 89187 | ** the same as a no-op. This opcodesnever appears in a real VM program. |
| 89098 | 89188 | */ |
| 89099 | | -default: { /* This is really OP_Noop and OP_Explain */ |
| 89189 | +default: { /* This is really OP_Noop, OP_Explain */ |
| 89100 | 89190 | assert( pOp->opcode==OP_Noop || pOp->opcode==OP_Explain ); |
| 89191 | + |
| 89101 | 89192 | break; |
| 89102 | 89193 | } |
| 89103 | 89194 | |
| 89104 | 89195 | /***************************************************************************** |
| 89105 | 89196 | ** The cases of the switch statement above this line should all be indented |
| | @@ -98641,11 +98732,11 @@ |
| 98641 | 98732 | } |
| 98642 | 98733 | case TK_SPAN: |
| 98643 | 98734 | case TK_COLLATE: |
| 98644 | 98735 | case TK_UPLUS: { |
| 98645 | 98736 | pExpr = pExpr->pLeft; |
| 98646 | | - goto expr_code_doover; |
| 98737 | + goto expr_code_doover; /* 2018-04-28: Prevent deep recursion. OSSFuzz. */ |
| 98647 | 98738 | } |
| 98648 | 98739 | |
| 98649 | 98740 | case TK_TRIGGER: { |
| 98650 | 98741 | /* If the opcode is TK_TRIGGER, then the expression is a reference |
| 98651 | 98742 | ** to a column in the new.* or old.* pseudo-tables available to |
| | @@ -106664,10 +106755,11 @@ |
| 106664 | 106755 | /* Open the table. Loop through all rows of the table, inserting index |
| 106665 | 106756 | ** records into the sorter. */ |
| 106666 | 106757 | sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead); |
| 106667 | 106758 | addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iTab, 0); VdbeCoverage(v); |
| 106668 | 106759 | regRecord = sqlite3GetTempReg(pParse); |
| 106760 | + sqlite3MultiWrite(pParse); |
| 106669 | 106761 | |
| 106670 | 106762 | sqlite3GenerateIndexKey(pParse,pIndex,iTab,regRecord,0,&iPartIdxLabel,0,0); |
| 106671 | 106763 | sqlite3VdbeAddOp2(v, OP_SorterInsert, iSorter, regRecord); |
| 106672 | 106764 | sqlite3ResolvePartIdxLabel(pParse, iPartIdxLabel); |
| 106673 | 106765 | sqlite3VdbeAddOp2(v, OP_Next, iTab, addr1+1); VdbeCoverage(v); |
| | @@ -106677,16 +106769,17 @@ |
| 106677 | 106769 | (char *)pKey, P4_KEYINFO); |
| 106678 | 106770 | sqlite3VdbeChangeP5(v, OPFLAG_BULKCSR|((memRootPage>=0)?OPFLAG_P2ISREG:0)); |
| 106679 | 106771 | |
| 106680 | 106772 | addr1 = sqlite3VdbeAddOp2(v, OP_SorterSort, iSorter, 0); VdbeCoverage(v); |
| 106681 | 106773 | if( IsUniqueIndex(pIndex) ){ |
| 106682 | | - int j2 = sqlite3VdbeCurrentAddr(v) + 3; |
| 106683 | | - sqlite3VdbeGoto(v, j2); |
| 106774 | + int j2 = sqlite3VdbeGoto(v, 1); |
| 106684 | 106775 | addr2 = sqlite3VdbeCurrentAddr(v); |
| 106776 | + sqlite3VdbeVerifyAbortable(v, OE_Abort); |
| 106685 | 106777 | sqlite3VdbeAddOp4Int(v, OP_SorterCompare, iSorter, j2, regRecord, |
| 106686 | 106778 | pIndex->nKeyCol); VdbeCoverage(v); |
| 106687 | 106779 | sqlite3UniqueConstraint(pParse, OE_Abort, pIndex); |
| 106780 | + sqlite3VdbeJumpHere(v, j2); |
| 106688 | 106781 | }else{ |
| 106689 | 106782 | addr2 = sqlite3VdbeCurrentAddr(v); |
| 106690 | 106783 | } |
| 106691 | 106784 | sqlite3VdbeAddOp3(v, OP_SorterData, iSorter, regRecord, iIdx); |
| 106692 | 106785 | sqlite3VdbeAddOp1(v, OP_SeekEnd, iIdx); |
| | @@ -108723,14 +108816,16 @@ |
| 108723 | 108816 | ** new entry to the hash table and return it. |
| 108724 | 108817 | */ |
| 108725 | 108818 | if( createFlag && bestScore<FUNC_PERFECT_MATCH && |
| 108726 | 108819 | (pBest = sqlite3DbMallocZero(db, sizeof(*pBest)+nName+1))!=0 ){ |
| 108727 | 108820 | FuncDef *pOther; |
| 108821 | + u8 *z; |
| 108728 | 108822 | pBest->zName = (const char*)&pBest[1]; |
| 108729 | 108823 | pBest->nArg = (u16)nArg; |
| 108730 | 108824 | pBest->funcFlags = enc; |
| 108731 | 108825 | memcpy((char*)&pBest[1], zName, nName+1); |
| 108826 | + for(z=(u8*)pBest->zName; *z; z++) *z = sqlite3UpperToLower[*z]; |
| 108732 | 108827 | pOther = (FuncDef*)sqlite3HashInsert(&db->aFunc, pBest->zName, pBest); |
| 108733 | 108828 | if( pOther==pBest ){ |
| 108734 | 108829 | sqlite3DbFree(db, pBest); |
| 108735 | 108830 | sqlite3OomFault(db); |
| 108736 | 108831 | return 0; |
| | @@ -109359,17 +109454,20 @@ |
| 109359 | 109454 | /* Delete the row */ |
| 109360 | 109455 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 109361 | 109456 | if( IsVirtual(pTab) ){ |
| 109362 | 109457 | const char *pVTab = (const char *)sqlite3GetVTable(db, pTab); |
| 109363 | 109458 | sqlite3VtabMakeWritable(pParse, pTab); |
| 109364 | | - sqlite3VdbeAddOp4(v, OP_VUpdate, 0, 1, iKey, pVTab, P4_VTAB); |
| 109365 | | - sqlite3VdbeChangeP5(v, OE_Abort); |
| 109366 | 109459 | assert( eOnePass==ONEPASS_OFF || eOnePass==ONEPASS_SINGLE ); |
| 109367 | 109460 | sqlite3MayAbort(pParse); |
| 109368 | | - if( eOnePass==ONEPASS_SINGLE && sqlite3IsToplevel(pParse) ){ |
| 109369 | | - pParse->isMultiWrite = 0; |
| 109461 | + if( eOnePass==ONEPASS_SINGLE ){ |
| 109462 | + sqlite3VdbeAddOp1(v, OP_Close, iTabCur); |
| 109463 | + if( sqlite3IsToplevel(pParse) ){ |
| 109464 | + pParse->isMultiWrite = 0; |
| 109465 | + } |
| 109370 | 109466 | } |
| 109467 | + sqlite3VdbeAddOp4(v, OP_VUpdate, 0, 1, iKey, pVTab, P4_VTAB); |
| 109468 | + sqlite3VdbeChangeP5(v, OE_Abort); |
| 109371 | 109469 | }else |
| 109372 | 109470 | #endif |
| 109373 | 109471 | { |
| 109374 | 109472 | int count = (pParse->nested==0); /* True to count changes */ |
| 109375 | 109473 | sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur, |
| | @@ -112000,10 +112098,16 @@ |
| 112000 | 112098 | int i; /* Iterator variable */ |
| 112001 | 112099 | Vdbe *v = sqlite3GetVdbe(pParse); /* Vdbe to add code to */ |
| 112002 | 112100 | int iCur = pParse->nTab - 1; /* Cursor number to use */ |
| 112003 | 112101 | int iOk = sqlite3VdbeMakeLabel(v); /* jump here if parent key found */ |
| 112004 | 112102 | |
| 112103 | + sqlite3VdbeVerifyAbortable(v, |
| 112104 | + (!pFKey->isDeferred |
| 112105 | + && !(pParse->db->flags & SQLITE_DeferFKs) |
| 112106 | + && !pParse->pToplevel |
| 112107 | + && !pParse->isMultiWrite) ? OE_Abort : OE_Ignore); |
| 112108 | + |
| 112005 | 112109 | /* If nIncr is less than zero, then check at runtime if there are any |
| 112006 | 112110 | ** outstanding constraints to resolve. If there are not, there is no need |
| 112007 | 112111 | ** to check if deleting this row resolves any outstanding violations. |
| 112008 | 112112 | ** |
| 112009 | 112113 | ** Check if any of the key columns in the child table row are NULL. If |
| | @@ -112407,10 +112511,11 @@ |
| 112407 | 112511 | ** If the SQLITE_DeferFKs flag is set, then this is not required, as |
| 112408 | 112512 | ** the statement transaction will not be rolled back even if FK |
| 112409 | 112513 | ** constraints are violated. |
| 112410 | 112514 | */ |
| 112411 | 112515 | if( (db->flags & SQLITE_DeferFKs)==0 ){ |
| 112516 | + sqlite3VdbeVerifyAbortable(v, OE_Abort); |
| 112412 | 112517 | sqlite3VdbeAddOp2(v, OP_FkIfZero, 0, sqlite3VdbeCurrentAddr(v)+2); |
| 112413 | 112518 | VdbeCoverage(v); |
| 112414 | 112519 | sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_FOREIGNKEY, |
| 112415 | 112520 | OE_Abort, 0, P4_STATIC, P5_ConstraintFK); |
| 112416 | 112521 | } |
| | @@ -113319,15 +113424,30 @@ |
| 113319 | 113424 | Parse *pParse, /* Parsing context */ |
| 113320 | 113425 | int iDb, /* Index of the database holding pTab */ |
| 113321 | 113426 | Table *pTab /* The table we are writing to */ |
| 113322 | 113427 | ){ |
| 113323 | 113428 | int memId = 0; /* Register holding maximum rowid */ |
| 113429 | + assert( pParse->db->aDb[iDb].pSchema!=0 ); |
| 113324 | 113430 | if( (pTab->tabFlags & TF_Autoincrement)!=0 |
| 113325 | 113431 | && (pParse->db->mDbFlags & DBFLAG_Vacuum)==0 |
| 113326 | 113432 | ){ |
| 113327 | 113433 | Parse *pToplevel = sqlite3ParseToplevel(pParse); |
| 113328 | 113434 | AutoincInfo *pInfo; |
| 113435 | + Table *pSeqTab = pParse->db->aDb[iDb].pSchema->pSeqTab; |
| 113436 | + |
| 113437 | + /* Verify that the sqlite_sequence table exists and is an ordinary |
| 113438 | + ** rowid table with exactly two columns. |
| 113439 | + ** Ticket d8dc2b3a58cd5dc2918a1d4acb 2018-05-23 */ |
| 113440 | + if( pSeqTab==0 |
| 113441 | + || !HasRowid(pSeqTab) |
| 113442 | + || IsVirtual(pSeqTab) |
| 113443 | + || pSeqTab->nCol!=2 |
| 113444 | + ){ |
| 113445 | + pParse->nErr++; |
| 113446 | + pParse->rc = SQLITE_CORRUPT_SEQUENCE; |
| 113447 | + return 0; |
| 113448 | + } |
| 113329 | 113449 | |
| 113330 | 113450 | pInfo = pToplevel->pAinc; |
| 113331 | 113451 | while( pInfo && pInfo->pTab!=pTab ){ pInfo = pInfo->pNext; } |
| 113332 | 113452 | if( pInfo==0 ){ |
| 113333 | 113453 | pInfo = sqlite3DbMallocRawNN(pParse->db, sizeof(*pInfo)); |
| | @@ -114498,10 +114618,11 @@ |
| 114498 | 114618 | for(i=0; i<pCheck->nExpr; i++){ |
| 114499 | 114619 | int allOk; |
| 114500 | 114620 | Expr *pExpr = pCheck->a[i].pExpr; |
| 114501 | 114621 | if( aiChng && checkConstraintUnchanged(pExpr, aiChng, pkChng) ) continue; |
| 114502 | 114622 | allOk = sqlite3VdbeMakeLabel(v); |
| 114623 | + sqlite3VdbeVerifyAbortable(v, onError); |
| 114503 | 114624 | sqlite3ExprIfTrue(pParse, pExpr, allOk, SQLITE_JUMPIFNULL); |
| 114504 | 114625 | if( onError==OE_Ignore ){ |
| 114505 | 114626 | sqlite3VdbeGoto(v, ignoreDest); |
| 114506 | 114627 | }else{ |
| 114507 | 114628 | char *zName = pCheck->a[i].zName; |
| | @@ -114607,10 +114728,11 @@ |
| 114607 | 114728 | } |
| 114608 | 114729 | |
| 114609 | 114730 | /* Check to see if the new rowid already exists in the table. Skip |
| 114610 | 114731 | ** the following conflict logic if it does not. */ |
| 114611 | 114732 | VdbeNoopComment((v, "uniqueness check for ROWID")); |
| 114733 | + sqlite3VdbeVerifyAbortable(v, onError); |
| 114612 | 114734 | sqlite3VdbeAddOp3(v, OP_NotExists, iDataCur, addrRowidOk, regNewData); |
| 114613 | 114735 | VdbeCoverage(v); |
| 114614 | 114736 | |
| 114615 | 114737 | switch( onError ){ |
| 114616 | 114738 | default: { |
| | @@ -114819,10 +114941,11 @@ |
| 114819 | 114941 | continue; |
| 114820 | 114942 | } |
| 114821 | 114943 | |
| 114822 | 114944 | /* Check to see if the new index entry will be unique */ |
| 114823 | 114945 | sqlite3ExprCachePush(pParse); |
| 114946 | + sqlite3VdbeVerifyAbortable(v, onError); |
| 114824 | 114947 | sqlite3VdbeAddOp4Int(v, OP_NoConflict, iThisCur, addrUniqueOk, |
| 114825 | 114948 | regIdx, pIdx->nKeyCol); VdbeCoverage(v); |
| 114826 | 114949 | |
| 114827 | 114950 | /* Generate code to handle collisions */ |
| 114828 | 114951 | regR = (pIdx==pPk) ? regIdx : sqlite3GetTempRange(pParse, nPkField); |
| | @@ -114905,13 +115028,15 @@ |
| 114905 | 115028 | break; |
| 114906 | 115029 | } |
| 114907 | 115030 | default: { |
| 114908 | 115031 | Trigger *pTrigger = 0; |
| 114909 | 115032 | assert( onError==OE_Replace ); |
| 114910 | | - sqlite3MultiWrite(pParse); |
| 114911 | 115033 | if( db->flags&SQLITE_RecTriggers ){ |
| 114912 | 115034 | pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0); |
| 115035 | + } |
| 115036 | + if( pTrigger || sqlite3FkRequired(pParse, pTab, 0, 0) ){ |
| 115037 | + sqlite3MultiWrite(pParse); |
| 114913 | 115038 | } |
| 114914 | 115039 | sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur, |
| 114915 | 115040 | regR, nPkField, 0, OE_Replace, |
| 114916 | 115041 | (pIdx==pPk ? ONEPASS_SINGLE : ONEPASS_OFF), iThisCur); |
| 114917 | 115042 | seenReplace = 1; |
| | @@ -115428,10 +115553,11 @@ |
| 115428 | 115553 | u8 insFlags; |
| 115429 | 115554 | sqlite3OpenTable(pParse, iSrc, iDbSrc, pSrc, OP_OpenRead); |
| 115430 | 115555 | emptySrcTest = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0); VdbeCoverage(v); |
| 115431 | 115556 | if( pDest->iPKey>=0 ){ |
| 115432 | 115557 | addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid); |
| 115558 | + sqlite3VdbeVerifyAbortable(v, onError); |
| 115433 | 115559 | addr2 = sqlite3VdbeAddOp3(v, OP_NotExists, iDest, 0, regRowid); |
| 115434 | 115560 | VdbeCoverage(v); |
| 115435 | 115561 | sqlite3RowidConstraint(pParse, onError, pDest); |
| 115436 | 115562 | sqlite3VdbeJumpHere(v, addr2); |
| 115437 | 115563 | autoIncStep(pParse, regAutoinc, regRowid); |
| | @@ -129709,11 +129835,11 @@ |
| 129709 | 129835 | int regArg; /* First register in VUpdate arg array */ |
| 129710 | 129836 | int regRec; /* Register in which to assemble record */ |
| 129711 | 129837 | int regRowid; /* Register for ephem table rowid */ |
| 129712 | 129838 | int iCsr = pSrc->a[0].iCursor; /* Cursor used for virtual table scan */ |
| 129713 | 129839 | int aDummy[2]; /* Unused arg for sqlite3WhereOkOnePass() */ |
| 129714 | | - int bOnePass; /* True to use onepass strategy */ |
| 129840 | + int eOnePass; /* True to use onepass strategy */ |
| 129715 | 129841 | int addr; /* Address of OP_OpenEphemeral */ |
| 129716 | 129842 | |
| 129717 | 129843 | /* Allocate nArg registers in which to gather the arguments for VUpdate. Then |
| 129718 | 129844 | ** create and open the ephemeral table in which the records created from |
| 129719 | 129845 | ** these arguments will be temporarily stored. */ |
| | @@ -129754,16 +129880,20 @@ |
| 129754 | 129880 | iPk = pPk->aiColumn[0]; |
| 129755 | 129881 | sqlite3VdbeAddOp3(v, OP_VColumn, iCsr, iPk, regArg); |
| 129756 | 129882 | sqlite3VdbeAddOp2(v, OP_SCopy, regArg+2+iPk, regArg+1); |
| 129757 | 129883 | } |
| 129758 | 129884 | |
| 129759 | | - bOnePass = sqlite3WhereOkOnePass(pWInfo, aDummy); |
| 129885 | + eOnePass = sqlite3WhereOkOnePass(pWInfo, aDummy); |
| 129760 | 129886 | |
| 129761 | | - if( bOnePass ){ |
| 129887 | + /* There is no ONEPASS_MULTI on virtual tables */ |
| 129888 | + assert( eOnePass==ONEPASS_OFF || eOnePass==ONEPASS_SINGLE ); |
| 129889 | + |
| 129890 | + if( eOnePass ){ |
| 129762 | 129891 | /* If using the onepass strategy, no-op out the OP_OpenEphemeral coded |
| 129763 | 129892 | ** above. */ |
| 129764 | 129893 | sqlite3VdbeChangeToNoop(v, addr); |
| 129894 | + sqlite3VdbeAddOp1(v, OP_Close, iCsr); |
| 129765 | 129895 | }else{ |
| 129766 | 129896 | /* Create a record from the argument register contents and insert it into |
| 129767 | 129897 | ** the ephemeral table. */ |
| 129768 | 129898 | sqlite3MultiWrite(pParse); |
| 129769 | 129899 | sqlite3VdbeAddOp3(v, OP_MakeRecord, regArg, nArg, regRec); |
| | @@ -129775,11 +129905,11 @@ |
| 129775 | 129905 | sqlite3VdbeAddOp2(v, OP_NewRowid, ephemTab, regRowid); |
| 129776 | 129906 | sqlite3VdbeAddOp3(v, OP_Insert, ephemTab, regRec, regRowid); |
| 129777 | 129907 | } |
| 129778 | 129908 | |
| 129779 | 129909 | |
| 129780 | | - if( bOnePass==0 ){ |
| 129910 | + if( eOnePass==ONEPASS_OFF ){ |
| 129781 | 129911 | /* End the virtual table scan */ |
| 129782 | 129912 | sqlite3WhereEnd(pWInfo); |
| 129783 | 129913 | |
| 129784 | 129914 | /* Begin scannning through the ephemeral table. */ |
| 129785 | 129915 | addr = sqlite3VdbeAddOp1(v, OP_Rewind, ephemTab); VdbeCoverage(v); |
| | @@ -129795,11 +129925,11 @@ |
| 129795 | 129925 | sqlite3VdbeChangeP5(v, onError==OE_Default ? OE_Abort : onError); |
| 129796 | 129926 | sqlite3MayAbort(pParse); |
| 129797 | 129927 | |
| 129798 | 129928 | /* End of the ephemeral table scan. Or, if using the onepass strategy, |
| 129799 | 129929 | ** jump to here if the scan visited zero rows. */ |
| 129800 | | - if( bOnePass==0 ){ |
| 129930 | + if( eOnePass==ONEPASS_OFF ){ |
| 129801 | 129931 | sqlite3VdbeAddOp2(v, OP_Next, ephemTab, addr+1); VdbeCoverage(v); |
| 129802 | 129932 | sqlite3VdbeJumpHere(v, addr); |
| 129803 | 129933 | sqlite3VdbeAddOp2(v, OP_Close, ephemTab, 0); |
| 129804 | 129934 | }else{ |
| 129805 | 129935 | sqlite3WhereEnd(pWInfo); |
| | @@ -130038,10 +130168,11 @@ |
| 130038 | 130168 | k = sqlite3ColumnOfIndex(pIdx, pPk->aiColumn[i]); |
| 130039 | 130169 | sqlite3VdbeAddOp3(v, OP_Column, iCur, k, iPk+i); |
| 130040 | 130170 | VdbeComment((v, "%s.%s", pIdx->zName, |
| 130041 | 130171 | pTab->aCol[pPk->aiColumn[i]].zName)); |
| 130042 | 130172 | } |
| 130173 | + sqlite3VdbeVerifyAbortable(v, OE_Abort); |
| 130043 | 130174 | i = sqlite3VdbeAddOp4Int(v, OP_Found, iDataCur, 0, iPk, nPk); |
| 130044 | 130175 | VdbeCoverage(v); |
| 130045 | 130176 | sqlite3VdbeAddOp4(v, OP_Halt, SQLITE_CORRUPT, OE_Abort, 0, |
| 130046 | 130177 | "corrupt database", P4_STATIC); |
| 130047 | 130178 | sqlite3VdbeJumpHere(v, i); |
| | @@ -131490,13 +131621,10 @@ |
| 131490 | 131621 | sqlite3_module *pMod; |
| 131491 | 131622 | void (*xSFunc)(sqlite3_context*,int,sqlite3_value**) = 0; |
| 131492 | 131623 | void *pArg = 0; |
| 131493 | 131624 | FuncDef *pNew; |
| 131494 | 131625 | int rc = 0; |
| 131495 | | - char *zLowerName; |
| 131496 | | - unsigned char *z; |
| 131497 | | - |
| 131498 | 131626 | |
| 131499 | 131627 | /* Check to see the left operand is a column in a virtual table */ |
| 131500 | 131628 | if( NEVER(pExpr==0) ) return pDef; |
| 131501 | 131629 | if( pExpr->op!=TK_COLUMN ) return pDef; |
| 131502 | 131630 | pTab = pExpr->pTab; |
| | @@ -131507,20 +131635,26 @@ |
| 131507 | 131635 | assert( pVtab->pModule!=0 ); |
| 131508 | 131636 | pMod = (sqlite3_module *)pVtab->pModule; |
| 131509 | 131637 | if( pMod->xFindFunction==0 ) return pDef; |
| 131510 | 131638 | |
| 131511 | 131639 | /* Call the xFindFunction method on the virtual table implementation |
| 131512 | | - ** to see if the implementation wants to overload this function |
| 131640 | + ** to see if the implementation wants to overload this function. |
| 131641 | + ** |
| 131642 | + ** Though undocumented, we have historically always invoked xFindFunction |
| 131643 | + ** with an all lower-case function name. Continue in this tradition to |
| 131644 | + ** avoid any chance of an incompatibility. |
| 131513 | 131645 | */ |
| 131514 | | - zLowerName = sqlite3DbStrDup(db, pDef->zName); |
| 131515 | | - if( zLowerName ){ |
| 131516 | | - for(z=(unsigned char*)zLowerName; *z; z++){ |
| 131517 | | - *z = sqlite3UpperToLower[*z]; |
| 131518 | | - } |
| 131519 | | - rc = pMod->xFindFunction(pVtab, nArg, zLowerName, &xSFunc, &pArg); |
| 131520 | | - sqlite3DbFree(db, zLowerName); |
| 131521 | | - } |
| 131646 | +#ifdef SQLITE_DEBUG |
| 131647 | + { |
| 131648 | + int i; |
| 131649 | + for(i=0; pDef->zName[i]; i++){ |
| 131650 | + unsigned char x = (unsigned char)pDef->zName[i]; |
| 131651 | + assert( x==sqlite3UpperToLower[x] ); |
| 131652 | + } |
| 131653 | + } |
| 131654 | +#endif |
| 131655 | + rc = pMod->xFindFunction(pVtab, nArg, pDef->zName, &xSFunc, &pArg); |
| 131522 | 131656 | if( rc==0 ){ |
| 131523 | 131657 | return pDef; |
| 131524 | 131658 | } |
| 131525 | 131659 | |
| 131526 | 131660 | /* Create a new ephemeral function definition for the overloaded |
| | @@ -138881,18 +139015,20 @@ |
| 138881 | 139015 | pNew->nLTerm = 1; |
| 138882 | 139016 | pNew->aLTerm[0] = pTerm; |
| 138883 | 139017 | /* TUNING: One-time cost for computing the automatic index is |
| 138884 | 139018 | ** estimated to be X*N*log2(N) where N is the number of rows in |
| 138885 | 139019 | ** the table being indexed and where X is 7 (LogEst=28) for normal |
| 138886 | | - ** tables or 1.375 (LogEst=4) for views and subqueries. The value |
| 139020 | + ** tables or 0.5 (LogEst=-10) for views and subqueries. The value |
| 138887 | 139021 | ** of X is smaller for views and subqueries so that the query planner |
| 138888 | 139022 | ** will be more aggressive about generating automatic indexes for |
| 138889 | 139023 | ** those objects, since there is no opportunity to add schema |
| 138890 | 139024 | ** indexes on subqueries and views. */ |
| 138891 | | - pNew->rSetup = rLogSize + rSize + 4; |
| 139025 | + pNew->rSetup = rLogSize + rSize; |
| 138892 | 139026 | if( pTab->pSelect==0 && (pTab->tabFlags & TF_Ephemeral)==0 ){ |
| 138893 | | - pNew->rSetup += 24; |
| 139027 | + pNew->rSetup += 28; |
| 139028 | + }else{ |
| 139029 | + pNew->rSetup -= 10; |
| 138894 | 139030 | } |
| 138895 | 139031 | ApplyCostMultiplier(pNew->rSetup, pTab->costMult); |
| 138896 | 139032 | if( pNew->rSetup<0 ) pNew->rSetup = 0; |
| 138897 | 139033 | /* TUNING: Each index lookup yields 20 rows in the table. This |
| 138898 | 139034 | ** is more than the usual guess of 10 rows, since we have no way |
| | @@ -140024,16 +140160,19 @@ |
| 140024 | 140160 | Bitmask maskNew; /* Mask of src visited by (..) */ |
| 140025 | 140161 | Bitmask revMask = 0; /* Mask of rev-order loops for (..) */ |
| 140026 | 140162 | |
| 140027 | 140163 | if( (pWLoop->prereq & ~pFrom->maskLoop)!=0 ) continue; |
| 140028 | 140164 | if( (pWLoop->maskSelf & pFrom->maskLoop)!=0 ) continue; |
| 140029 | | - if( (pWLoop->wsFlags & WHERE_AUTO_INDEX)!=0 && pFrom->nRow<10 ){ |
| 140165 | + if( (pWLoop->wsFlags & WHERE_AUTO_INDEX)!=0 && pFrom->nRow<3 ){ |
| 140030 | 140166 | /* Do not use an automatic index if the this loop is expected |
| 140031 | | - ** to run less than 2 times. */ |
| 140167 | + ** to run less than 1.25 times. It is tempting to also exclude |
| 140168 | + ** automatic index usage on an outer loop, but sometimes an automatic |
| 140169 | + ** index is useful in the outer loop of a correlated subquery. */ |
| 140032 | 140170 | assert( 10==sqlite3LogEst(2) ); |
| 140033 | 140171 | continue; |
| 140034 | 140172 | } |
| 140173 | + |
| 140035 | 140174 | /* At this point, pWLoop is a candidate to be the next loop. |
| 140036 | 140175 | ** Compute its cost */ |
| 140037 | 140176 | rUnsorted = sqlite3LogEstAdd(pWLoop->rSetup,pWLoop->rRun + pFrom->nRow); |
| 140038 | 140177 | rUnsorted = sqlite3LogEstAdd(rUnsorted, pFrom->rUnsorted); |
| 140039 | 140178 | nOut = pFrom->nRow + pWLoop->nOut; |
| | @@ -148446,23 +148585,25 @@ |
| 148446 | 148585 | return SQLITE_MISUSE_BKPT; |
| 148447 | 148586 | } |
| 148448 | 148587 | #endif |
| 148449 | 148588 | sqlite3_mutex_enter(db->mutex); |
| 148450 | 148589 | if( xDestroy ){ |
| 148451 | | - pArg = (FuncDestructor *)sqlite3DbMallocZero(db, sizeof(FuncDestructor)); |
| 148590 | + pArg = (FuncDestructor *)sqlite3Malloc(sizeof(FuncDestructor)); |
| 148452 | 148591 | if( !pArg ){ |
| 148592 | + sqlite3OomFault(db); |
| 148453 | 148593 | xDestroy(p); |
| 148454 | 148594 | goto out; |
| 148455 | 148595 | } |
| 148596 | + pArg->nRef = 0; |
| 148456 | 148597 | pArg->xDestroy = xDestroy; |
| 148457 | 148598 | pArg->pUserData = p; |
| 148458 | 148599 | } |
| 148459 | 148600 | rc = sqlite3CreateFunc(db, zFunc, nArg, enc, p, xSFunc, xStep, xFinal, pArg); |
| 148460 | 148601 | if( pArg && pArg->nRef==0 ){ |
| 148461 | 148602 | assert( rc!=SQLITE_OK ); |
| 148462 | 148603 | xDestroy(p); |
| 148463 | | - sqlite3DbFree(db, pArg); |
| 148604 | + sqlite3_free(pArg); |
| 148464 | 148605 | } |
| 148465 | 148606 | |
| 148466 | 148607 | out: |
| 148467 | 148608 | rc = sqlite3ApiExit(db, rc); |
| 148468 | 148609 | sqlite3_mutex_leave(db->mutex); |
| | @@ -148495,10 +148636,32 @@ |
| 148495 | 148636 | sqlite3_mutex_leave(db->mutex); |
| 148496 | 148637 | return rc; |
| 148497 | 148638 | } |
| 148498 | 148639 | #endif |
| 148499 | 148640 | |
| 148641 | + |
| 148642 | +/* |
| 148643 | +** The following is the implementation of an SQL function that always |
| 148644 | +** fails with an error message stating that the function is used in the |
| 148645 | +** wrong context. The sqlite3_overload_function() API might construct |
| 148646 | +** SQL function that use this routine so that the functions will exist |
| 148647 | +** for name resolution but are actually overloaded by the xFindFunction |
| 148648 | +** method of virtual tables. |
| 148649 | +*/ |
| 148650 | +static void sqlite3InvalidFunction( |
| 148651 | + sqlite3_context *context, /* The function calling context */ |
| 148652 | + int NotUsed, /* Number of arguments to the function */ |
| 148653 | + sqlite3_value **NotUsed2 /* Value of each argument */ |
| 148654 | +){ |
| 148655 | + const char *zName = (const char*)sqlite3_user_data(context); |
| 148656 | + char *zErr; |
| 148657 | + UNUSED_PARAMETER2(NotUsed, NotUsed2); |
| 148658 | + zErr = sqlite3_mprintf( |
| 148659 | + "unable to use function %s in the requested context", zName); |
| 148660 | + sqlite3_result_error(context, zErr, -1); |
| 148661 | + sqlite3_free(zErr); |
| 148662 | +} |
| 148500 | 148663 | |
| 148501 | 148664 | /* |
| 148502 | 148665 | ** Declare that a function has been overloaded by a virtual table. |
| 148503 | 148666 | ** |
| 148504 | 148667 | ** If the function already exists as a regular global function, then |
| | @@ -148513,25 +148676,26 @@ |
| 148513 | 148676 | SQLITE_API int sqlite3_overload_function( |
| 148514 | 148677 | sqlite3 *db, |
| 148515 | 148678 | const char *zName, |
| 148516 | 148679 | int nArg |
| 148517 | 148680 | ){ |
| 148518 | | - int rc = SQLITE_OK; |
| 148681 | + int rc; |
| 148682 | + char *zCopy; |
| 148519 | 148683 | |
| 148520 | 148684 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 148521 | 148685 | if( !sqlite3SafetyCheckOk(db) || zName==0 || nArg<-2 ){ |
| 148522 | 148686 | return SQLITE_MISUSE_BKPT; |
| 148523 | 148687 | } |
| 148524 | 148688 | #endif |
| 148525 | 148689 | sqlite3_mutex_enter(db->mutex); |
| 148526 | | - if( sqlite3FindFunction(db, zName, nArg, SQLITE_UTF8, 0)==0 ){ |
| 148527 | | - rc = sqlite3CreateFunc(db, zName, nArg, SQLITE_UTF8, |
| 148528 | | - 0, sqlite3InvalidFunction, 0, 0, 0); |
| 148529 | | - } |
| 148530 | | - rc = sqlite3ApiExit(db, rc); |
| 148690 | + rc = sqlite3FindFunction(db, zName, nArg, SQLITE_UTF8, 0)!=0; |
| 148531 | 148691 | sqlite3_mutex_leave(db->mutex); |
| 148532 | | - return rc; |
| 148692 | + if( rc ) return SQLITE_OK; |
| 148693 | + zCopy = sqlite3_mprintf(zName); |
| 148694 | + if( zCopy==0 ) return SQLITE_NOMEM; |
| 148695 | + return sqlite3_create_function_v2(db, zName, nArg, SQLITE_UTF8, |
| 148696 | + zCopy, sqlite3InvalidFunction, 0, 0, sqlite3_free); |
| 148533 | 148697 | } |
| 148534 | 148698 | |
| 148535 | 148699 | #ifndef SQLITE_OMIT_TRACE |
| 148536 | 148700 | /* |
| 148537 | 148701 | ** Register a trace function. The pArg from the previously registered trace |
| | @@ -170371,18 +170535,19 @@ |
| 170371 | 170535 | ** in the table name is replaced with the user-supplied name of the r-tree |
| 170372 | 170536 | ** table. |
| 170373 | 170537 | ** |
| 170374 | 170538 | ** CREATE TABLE %_node(nodeno INTEGER PRIMARY KEY, data BLOB) |
| 170375 | 170539 | ** CREATE TABLE %_parent(nodeno INTEGER PRIMARY KEY, parentnode INTEGER) |
| 170376 | | -** CREATE TABLE %_rowid(rowid INTEGER PRIMARY KEY, nodeno INTEGER) |
| 170540 | +** CREATE TABLE %_rowid(rowid INTEGER PRIMARY KEY, nodeno INTEGER, ...) |
| 170377 | 170541 | ** |
| 170378 | 170542 | ** The data for each node of the r-tree structure is stored in the %_node |
| 170379 | 170543 | ** table. For each node that is not the root node of the r-tree, there is |
| 170380 | 170544 | ** an entry in the %_parent table associating the node with its parent. |
| 170381 | 170545 | ** And for each row of data in the table, there is an entry in the %_rowid |
| 170382 | 170546 | ** table that maps from the entries rowid to the id of the node that it |
| 170383 | | -** is stored on. |
| 170547 | +** is stored on. If the r-tree contains auxiliary columns, those are stored |
| 170548 | +** on the end of the %_rowid table. |
| 170384 | 170549 | ** |
| 170385 | 170550 | ** The root node of an r-tree always exists, even if the r-tree table is |
| 170386 | 170551 | ** empty. The nodeno of the root node is always 1. All other nodes in the |
| 170387 | 170552 | ** table must be the same size as the root node. The content of each node |
| 170388 | 170553 | ** is formatted as follows: |
| | @@ -170440,10 +170605,13 @@ |
| 170440 | 170605 | typedef union RtreeCoord RtreeCoord; |
| 170441 | 170606 | typedef struct RtreeSearchPoint RtreeSearchPoint; |
| 170442 | 170607 | |
| 170443 | 170608 | /* The rtree may have between 1 and RTREE_MAX_DIMENSIONS dimensions. */ |
| 170444 | 170609 | #define RTREE_MAX_DIMENSIONS 5 |
| 170610 | + |
| 170611 | +/* Maximum number of auxiliary columns */ |
| 170612 | +#define RTREE_MAX_AUX_COLUMN 100 |
| 170445 | 170613 | |
| 170446 | 170614 | /* Size of hash table Rtree.aHash. This hash table is not expected to |
| 170447 | 170615 | ** ever contain very many entries, so a fixed number of buckets is |
| 170448 | 170616 | ** used. |
| 170449 | 170617 | */ |
| | @@ -170469,16 +170637,19 @@ |
| 170469 | 170637 | u8 nDim; /* Number of dimensions */ |
| 170470 | 170638 | u8 nDim2; /* Twice the number of dimensions */ |
| 170471 | 170639 | u8 eCoordType; /* RTREE_COORD_REAL32 or RTREE_COORD_INT32 */ |
| 170472 | 170640 | u8 nBytesPerCell; /* Bytes consumed per cell */ |
| 170473 | 170641 | u8 inWrTrans; /* True if inside write transaction */ |
| 170642 | + u8 nAux; /* # of auxiliary columns in %_rowid */ |
| 170474 | 170643 | int iDepth; /* Current depth of the r-tree structure */ |
| 170475 | 170644 | char *zDb; /* Name of database containing r-tree table */ |
| 170476 | 170645 | char *zName; /* Name of r-tree table */ |
| 170477 | 170646 | u32 nBusy; /* Current number of users of this structure */ |
| 170478 | 170647 | i64 nRowEst; /* Estimated number of rows in this table */ |
| 170479 | 170648 | u32 nCursor; /* Number of open cursors */ |
| 170649 | + u32 nNodeRef; /* Number RtreeNodes with positive nRef */ |
| 170650 | + char *zReadAuxSql; /* SQL for statement to read aux data */ |
| 170480 | 170651 | |
| 170481 | 170652 | /* List of nodes removed during a CondenseTree operation. List is |
| 170482 | 170653 | ** linked together via the pointer normally used for hash chains - |
| 170483 | 170654 | ** RtreeNode.pNext. RtreeNode.iNode stores the depth of the sub-tree |
| 170484 | 170655 | ** headed by the node (leaf nodes have RtreeNode.iNode==0). |
| | @@ -170500,10 +170671,13 @@ |
| 170500 | 170671 | |
| 170501 | 170672 | /* Statements to read/write/delete a record from xxx_parent */ |
| 170502 | 170673 | sqlite3_stmt *pReadParent; |
| 170503 | 170674 | sqlite3_stmt *pWriteParent; |
| 170504 | 170675 | sqlite3_stmt *pDeleteParent; |
| 170676 | + |
| 170677 | + /* Statement for writing to the "aux:" fields, if there are any */ |
| 170678 | + sqlite3_stmt *pWriteAux; |
| 170505 | 170679 | |
| 170506 | 170680 | RtreeNode *aHash[HASHSIZE]; /* Hash table of in-memory nodes. */ |
| 170507 | 170681 | }; |
| 170508 | 170682 | |
| 170509 | 170683 | /* Possible values for Rtree.eCoordType: */ |
| | @@ -170577,17 +170751,19 @@ |
| 170577 | 170751 | */ |
| 170578 | 170752 | struct RtreeCursor { |
| 170579 | 170753 | sqlite3_vtab_cursor base; /* Base class. Must be first */ |
| 170580 | 170754 | u8 atEOF; /* True if at end of search */ |
| 170581 | 170755 | u8 bPoint; /* True if sPoint is valid */ |
| 170756 | + u8 bAuxValid; /* True if pReadAux is valid */ |
| 170582 | 170757 | int iStrategy; /* Copy of idxNum search parameter */ |
| 170583 | 170758 | int nConstraint; /* Number of entries in aConstraint */ |
| 170584 | 170759 | RtreeConstraint *aConstraint; /* Search constraints. */ |
| 170585 | 170760 | int nPointAlloc; /* Number of slots allocated for aPoint[] */ |
| 170586 | 170761 | int nPoint; /* Number of slots used in aPoint[] */ |
| 170587 | 170762 | int mxLevel; /* iLevel value for root of the tree */ |
| 170588 | 170763 | RtreeSearchPoint *aPoint; /* Priority queue for search points */ |
| 170764 | + sqlite3_stmt *pReadAux; /* Statement to read aux-data */ |
| 170589 | 170765 | RtreeSearchPoint sPoint; /* Cached next search point */ |
| 170590 | 170766 | RtreeNode *aNode[RTREE_CACHE_SZ]; /* Rtree node cache */ |
| 170591 | 170767 | u32 anQueue[RTREE_MAX_DEPTH+1]; /* Number of queued entries by iLevel */ |
| 170592 | 170768 | }; |
| 170593 | 170769 | |
| | @@ -170870,10 +171046,11 @@ |
| 170870 | 171046 | /* |
| 170871 | 171047 | ** Increment the reference count of node p. |
| 170872 | 171048 | */ |
| 170873 | 171049 | static void nodeReference(RtreeNode *p){ |
| 170874 | 171050 | if( p ){ |
| 171051 | + assert( p->nRef>0 ); |
| 170875 | 171052 | p->nRef++; |
| 170876 | 171053 | } |
| 170877 | 171054 | } |
| 170878 | 171055 | |
| 170879 | 171056 | /* |
| | @@ -170937,10 +171114,11 @@ |
| 170937 | 171114 | pNode = (RtreeNode *)sqlite3_malloc(sizeof(RtreeNode) + pRtree->iNodeSize); |
| 170938 | 171115 | if( pNode ){ |
| 170939 | 171116 | memset(pNode, 0, sizeof(RtreeNode) + pRtree->iNodeSize); |
| 170940 | 171117 | pNode->zData = (u8 *)&pNode[1]; |
| 170941 | 171118 | pNode->nRef = 1; |
| 171119 | + pRtree->nNodeRef++; |
| 170942 | 171120 | pNode->pParent = pParent; |
| 170943 | 171121 | pNode->isDirty = 1; |
| 170944 | 171122 | nodeReference(pParent); |
| 170945 | 171123 | } |
| 170946 | 171124 | return pNode; |
| | @@ -170970,14 +171148,14 @@ |
| 170970 | 171148 | RtreeNode *pNode = 0; |
| 170971 | 171149 | |
| 170972 | 171150 | /* Check if the requested node is already in the hash table. If so, |
| 170973 | 171151 | ** increase its reference count and return it. |
| 170974 | 171152 | */ |
| 170975 | | - if( (pNode = nodeHashLookup(pRtree, iNode)) ){ |
| 171153 | + if( (pNode = nodeHashLookup(pRtree, iNode))!=0 ){ |
| 170976 | 171154 | assert( !pParent || !pNode->pParent || pNode->pParent==pParent ); |
| 170977 | 171155 | if( pParent && !pNode->pParent ){ |
| 170978 | | - nodeReference(pParent); |
| 171156 | + pParent->nRef++; |
| 170979 | 171157 | pNode->pParent = pParent; |
| 170980 | 171158 | } |
| 170981 | 171159 | pNode->nRef++; |
| 170982 | 171160 | *ppNode = pNode; |
| 170983 | 171161 | return SQLITE_OK; |
| | @@ -171012,10 +171190,11 @@ |
| 171012 | 171190 | rc = SQLITE_NOMEM; |
| 171013 | 171191 | }else{ |
| 171014 | 171192 | pNode->pParent = pParent; |
| 171015 | 171193 | pNode->zData = (u8 *)&pNode[1]; |
| 171016 | 171194 | pNode->nRef = 1; |
| 171195 | + pRtree->nNodeRef++; |
| 171017 | 171196 | pNode->iNode = iNode; |
| 171018 | 171197 | pNode->isDirty = 0; |
| 171019 | 171198 | pNode->pNext = 0; |
| 171020 | 171199 | rc = sqlite3_blob_read(pRtree->pNodeBlob, pNode->zData, |
| 171021 | 171200 | pRtree->iNodeSize, 0); |
| | @@ -171052,11 +171231,14 @@ |
| 171052 | 171231 | }else{ |
| 171053 | 171232 | rc = SQLITE_CORRUPT_VTAB; |
| 171054 | 171233 | } |
| 171055 | 171234 | *ppNode = pNode; |
| 171056 | 171235 | }else{ |
| 171057 | | - sqlite3_free(pNode); |
| 171236 | + if( pNode ){ |
| 171237 | + pRtree->nNodeRef--; |
| 171238 | + sqlite3_free(pNode); |
| 171239 | + } |
| 171058 | 171240 | *ppNode = 0; |
| 171059 | 171241 | } |
| 171060 | 171242 | |
| 171061 | 171243 | return rc; |
| 171062 | 171244 | } |
| | @@ -171149,12 +171331,14 @@ |
| 171149 | 171331 | */ |
| 171150 | 171332 | static int nodeRelease(Rtree *pRtree, RtreeNode *pNode){ |
| 171151 | 171333 | int rc = SQLITE_OK; |
| 171152 | 171334 | if( pNode ){ |
| 171153 | 171335 | assert( pNode->nRef>0 ); |
| 171336 | + assert( pRtree->nNodeRef>0 ); |
| 171154 | 171337 | pNode->nRef--; |
| 171155 | 171338 | if( pNode->nRef==0 ){ |
| 171339 | + pRtree->nNodeRef--; |
| 171156 | 171340 | if( pNode->iNode==1 ){ |
| 171157 | 171341 | pRtree->iDepth = -1; |
| 171158 | 171342 | } |
| 171159 | 171343 | if( pNode->pParent ){ |
| 171160 | 171344 | rc = nodeRelease(pRtree, pNode->pParent); |
| | @@ -171267,20 +171451,23 @@ |
| 171267 | 171451 | */ |
| 171268 | 171452 | static void rtreeRelease(Rtree *pRtree){ |
| 171269 | 171453 | pRtree->nBusy--; |
| 171270 | 171454 | if( pRtree->nBusy==0 ){ |
| 171271 | 171455 | pRtree->inWrTrans = 0; |
| 171272 | | - pRtree->nCursor = 0; |
| 171456 | + assert( pRtree->nCursor==0 ); |
| 171273 | 171457 | nodeBlobReset(pRtree); |
| 171458 | + assert( pRtree->nNodeRef==0 ); |
| 171274 | 171459 | sqlite3_finalize(pRtree->pWriteNode); |
| 171275 | 171460 | sqlite3_finalize(pRtree->pDeleteNode); |
| 171276 | 171461 | sqlite3_finalize(pRtree->pReadRowid); |
| 171277 | 171462 | sqlite3_finalize(pRtree->pWriteRowid); |
| 171278 | 171463 | sqlite3_finalize(pRtree->pDeleteRowid); |
| 171279 | 171464 | sqlite3_finalize(pRtree->pReadParent); |
| 171280 | 171465 | sqlite3_finalize(pRtree->pWriteParent); |
| 171281 | 171466 | sqlite3_finalize(pRtree->pDeleteParent); |
| 171467 | + sqlite3_finalize(pRtree->pWriteAux); |
| 171468 | + sqlite3_free(pRtree->zReadAuxSql); |
| 171282 | 171469 | sqlite3_free(pRtree); |
| 171283 | 171470 | } |
| 171284 | 171471 | } |
| 171285 | 171472 | |
| 171286 | 171473 | /* |
| | @@ -171365,10 +171552,11 @@ |
| 171365 | 171552 | Rtree *pRtree = (Rtree *)(cur->pVtab); |
| 171366 | 171553 | int ii; |
| 171367 | 171554 | RtreeCursor *pCsr = (RtreeCursor *)cur; |
| 171368 | 171555 | assert( pRtree->nCursor>0 ); |
| 171369 | 171556 | freeCursorConstraints(pCsr); |
| 171557 | + sqlite3_finalize(pCsr->pReadAux); |
| 171370 | 171558 | sqlite3_free(pCsr->aPoint); |
| 171371 | 171559 | for(ii=0; ii<RTREE_CACHE_SZ; ii++) nodeRelease(pRtree, pCsr->aNode[ii]); |
| 171372 | 171560 | sqlite3_free(pCsr); |
| 171373 | 171561 | pRtree->nCursor--; |
| 171374 | 171562 | nodeBlobReset(pRtree); |
| | @@ -171736,11 +171924,11 @@ |
| 171736 | 171924 | if( pNew==0 ) return 0; |
| 171737 | 171925 | ii = (int)(pNew - pCur->aPoint) + 1; |
| 171738 | 171926 | if( ii<RTREE_CACHE_SZ ){ |
| 171739 | 171927 | assert( pCur->aNode[ii]==0 ); |
| 171740 | 171928 | pCur->aNode[ii] = pCur->aNode[0]; |
| 171741 | | - }else{ |
| 171929 | + }else{ |
| 171742 | 171930 | nodeRelease(RTREE_OF_CURSOR(pCur), pCur->aNode[0]); |
| 171743 | 171931 | } |
| 171744 | 171932 | pCur->aNode[0] = 0; |
| 171745 | 171933 | *pNew = pCur->sPoint; |
| 171746 | 171934 | } |
| | @@ -171907,10 +172095,14 @@ |
| 171907 | 172095 | RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor; |
| 171908 | 172096 | int rc = SQLITE_OK; |
| 171909 | 172097 | |
| 171910 | 172098 | /* Move to the next entry that matches the configured constraints. */ |
| 171911 | 172099 | RTREE_QUEUE_TRACE(pCsr, "POP-Nx:"); |
| 172100 | + if( pCsr->bAuxValid ){ |
| 172101 | + pCsr->bAuxValid = 0; |
| 172102 | + sqlite3_reset(pCsr->pReadAux); |
| 172103 | + } |
| 171912 | 172104 | rtreeSearchPointPop(pCsr); |
| 171913 | 172105 | rc = rtreeStepToLeaf(pCsr); |
| 171914 | 172106 | return rc; |
| 171915 | 172107 | } |
| 171916 | 172108 | |
| | @@ -171941,11 +172133,11 @@ |
| 171941 | 172133 | |
| 171942 | 172134 | if( rc ) return rc; |
| 171943 | 172135 | if( p==0 ) return SQLITE_OK; |
| 171944 | 172136 | if( i==0 ){ |
| 171945 | 172137 | sqlite3_result_int64(ctx, nodeGetRowid(pRtree, pNode, p->iCell)); |
| 171946 | | - }else{ |
| 172138 | + }else if( i<=pRtree->nDim2 ){ |
| 171947 | 172139 | nodeGetCoord(pRtree, pNode, p->iCell, i-1, &c); |
| 171948 | 172140 | #ifndef SQLITE_RTREE_INT_ONLY |
| 171949 | 172141 | if( pRtree->eCoordType==RTREE_COORD_REAL32 ){ |
| 171950 | 172142 | sqlite3_result_double(ctx, c.f); |
| 171951 | 172143 | }else |
| | @@ -171952,11 +172144,31 @@ |
| 171952 | 172144 | #endif |
| 171953 | 172145 | { |
| 171954 | 172146 | assert( pRtree->eCoordType==RTREE_COORD_INT32 ); |
| 171955 | 172147 | sqlite3_result_int(ctx, c.i); |
| 171956 | 172148 | } |
| 171957 | | - } |
| 172149 | + }else{ |
| 172150 | + if( !pCsr->bAuxValid ){ |
| 172151 | + if( pCsr->pReadAux==0 ){ |
| 172152 | + rc = sqlite3_prepare_v3(pRtree->db, pRtree->zReadAuxSql, -1, 0, |
| 172153 | + &pCsr->pReadAux, 0); |
| 172154 | + if( rc ) return rc; |
| 172155 | + } |
| 172156 | + sqlite3_bind_int64(pCsr->pReadAux, 1, |
| 172157 | + nodeGetRowid(pRtree, pNode, p->iCell)); |
| 172158 | + rc = sqlite3_step(pCsr->pReadAux); |
| 172159 | + if( rc==SQLITE_ROW ){ |
| 172160 | + pCsr->bAuxValid = 1; |
| 172161 | + }else{ |
| 172162 | + sqlite3_reset(pCsr->pReadAux); |
| 172163 | + if( rc==SQLITE_DONE ) rc = SQLITE_OK; |
| 172164 | + return rc; |
| 172165 | + } |
| 172166 | + } |
| 172167 | + sqlite3_result_value(ctx, |
| 172168 | + sqlite3_column_value(pCsr->pReadAux, i - pRtree->nDim2 + 1)); |
| 172169 | + } |
| 171958 | 172170 | return SQLITE_OK; |
| 171959 | 172171 | } |
| 171960 | 172172 | |
| 171961 | 172173 | /* |
| 171962 | 172174 | ** Use nodeAcquire() to obtain the leaf node containing the record with |
| | @@ -172030,18 +172242,21 @@ |
| 172030 | 172242 | RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor; |
| 172031 | 172243 | RtreeNode *pRoot = 0; |
| 172032 | 172244 | int ii; |
| 172033 | 172245 | int rc = SQLITE_OK; |
| 172034 | 172246 | int iCell = 0; |
| 172247 | + sqlite3_stmt *pStmt; |
| 172035 | 172248 | |
| 172036 | 172249 | rtreeReference(pRtree); |
| 172037 | 172250 | |
| 172038 | 172251 | /* Reset the cursor to the same state as rtreeOpen() leaves it in. */ |
| 172039 | 172252 | freeCursorConstraints(pCsr); |
| 172040 | 172253 | sqlite3_free(pCsr->aPoint); |
| 172254 | + pStmt = pCsr->pReadAux; |
| 172041 | 172255 | memset(pCsr, 0, sizeof(RtreeCursor)); |
| 172042 | 172256 | pCsr->base.pVtab = (sqlite3_vtab*)pRtree; |
| 172257 | + pCsr->pReadAux = pStmt; |
| 172043 | 172258 | |
| 172044 | 172259 | pCsr->iStrategy = idxNum; |
| 172045 | 172260 | if( idxNum==1 ){ |
| 172046 | 172261 | /* Special case - lookup by rowid. */ |
| 172047 | 172262 | RtreeNode *pLeaf; /* Leaf on which the required cell resides */ |
| | @@ -172200,14 +172415,18 @@ |
| 172200 | 172415 | ** sqlite uses an internal cost of 0.0). It is expected to return |
| 172201 | 172416 | ** a single row. |
| 172202 | 172417 | */ |
| 172203 | 172418 | pIdxInfo->estimatedCost = 30.0; |
| 172204 | 172419 | pIdxInfo->estimatedRows = 1; |
| 172420 | + pIdxInfo->idxFlags = SQLITE_INDEX_SCAN_UNIQUE; |
| 172205 | 172421 | return SQLITE_OK; |
| 172206 | 172422 | } |
| 172207 | 172423 | |
| 172208 | | - if( p->usable && (p->iColumn>0 || p->op==SQLITE_INDEX_CONSTRAINT_MATCH) ){ |
| 172424 | + if( p->usable |
| 172425 | + && ((p->iColumn>0 && p->iColumn<=pRtree->nDim2) |
| 172426 | + || p->op==SQLITE_INDEX_CONSTRAINT_MATCH) |
| 172427 | + ){ |
| 172209 | 172428 | u8 op; |
| 172210 | 172429 | switch( p->op ){ |
| 172211 | 172430 | case SQLITE_INDEX_CONSTRAINT_EQ: op = RTREE_EQ; break; |
| 172212 | 172431 | case SQLITE_INDEX_CONSTRAINT_GT: op = RTREE_GT; break; |
| 172213 | 172432 | case SQLITE_INDEX_CONSTRAINT_LE: op = RTREE_LE; break; |
| | @@ -172776,11 +172995,11 @@ |
| 172776 | 172995 | pNode->isDirty = 1; |
| 172777 | 172996 | writeInt16(pNode->zData, pRtree->iDepth); |
| 172778 | 172997 | }else{ |
| 172779 | 172998 | pLeft = pNode; |
| 172780 | 172999 | pRight = nodeNew(pRtree, pLeft->pParent); |
| 172781 | | - nodeReference(pLeft); |
| 173000 | + pLeft->nRef++; |
| 172782 | 173001 | } |
| 172783 | 173002 | |
| 172784 | 173003 | if( !pLeft || !pRight ){ |
| 172785 | 173004 | rc = SQLITE_NOMEM; |
| 172786 | 173005 | goto splitnode_out; |
| | @@ -173266,10 +173485,11 @@ |
| 173266 | 173485 | for(pLeaf=pRtree->pDeleted; pLeaf; pLeaf=pRtree->pDeleted){ |
| 173267 | 173486 | if( rc==SQLITE_OK ){ |
| 173268 | 173487 | rc = reinsertNodeContent(pRtree, pLeaf); |
| 173269 | 173488 | } |
| 173270 | 173489 | pRtree->pDeleted = pLeaf->pNext; |
| 173490 | + pRtree->nNodeRef--; |
| 173271 | 173491 | sqlite3_free(pLeaf); |
| 173272 | 173492 | } |
| 173273 | 173493 | |
| 173274 | 173494 | /* Release the reference to the root node. */ |
| 173275 | 173495 | if( rc==SQLITE_OK ){ |
| | @@ -173362,18 +173582,24 @@ |
| 173362 | 173582 | ** The xUpdate method for rtree module virtual tables. |
| 173363 | 173583 | */ |
| 173364 | 173584 | static int rtreeUpdate( |
| 173365 | 173585 | sqlite3_vtab *pVtab, |
| 173366 | 173586 | int nData, |
| 173367 | | - sqlite3_value **azData, |
| 173587 | + sqlite3_value **aData, |
| 173368 | 173588 | sqlite_int64 *pRowid |
| 173369 | 173589 | ){ |
| 173370 | 173590 | Rtree *pRtree = (Rtree *)pVtab; |
| 173371 | 173591 | int rc = SQLITE_OK; |
| 173372 | 173592 | RtreeCell cell; /* New cell to insert if nData>1 */ |
| 173373 | 173593 | int bHaveRowid = 0; /* Set to 1 after new rowid is determined */ |
| 173374 | 173594 | |
| 173595 | + if( pRtree->nNodeRef ){ |
| 173596 | + /* Unable to write to the btree while another cursor is reading from it, |
| 173597 | + ** since the write might do a rebalance which would disrupt the read |
| 173598 | + ** cursor. */ |
| 173599 | + return SQLITE_LOCKED_VTAB; |
| 173600 | + } |
| 173375 | 173601 | rtreeReference(pRtree); |
| 173376 | 173602 | assert(nData>=1); |
| 173377 | 173603 | |
| 173378 | 173604 | cell.iRowid = 0; /* Used only to suppress a compiler warning */ |
| 173379 | 173605 | |
| | @@ -173388,50 +173614,51 @@ |
| 173388 | 173614 | ** case, SQLITE_CONSTRAINT must be returned regardless of the |
| 173389 | 173615 | ** conflict-handling mode specified by the user. |
| 173390 | 173616 | */ |
| 173391 | 173617 | if( nData>1 ){ |
| 173392 | 173618 | int ii; |
| 173619 | + int nn = nData - 4; |
| 173393 | 173620 | |
| 173394 | | - /* Populate the cell.aCoord[] array. The first coordinate is azData[3]. |
| 173621 | + if( nn > pRtree->nDim2 ) nn = pRtree->nDim2; |
| 173622 | + /* Populate the cell.aCoord[] array. The first coordinate is aData[3]. |
| 173395 | 173623 | ** |
| 173396 | 173624 | ** NB: nData can only be less than nDim*2+3 if the rtree is mis-declared |
| 173397 | 173625 | ** with "column" that are interpreted as table constraints. |
| 173398 | 173626 | ** Example: CREATE VIRTUAL TABLE bad USING rtree(x,y,CHECK(y>5)); |
| 173399 | 173627 | ** This problem was discovered after years of use, so we silently ignore |
| 173400 | 173628 | ** these kinds of misdeclared tables to avoid breaking any legacy. |
| 173401 | 173629 | */ |
| 173402 | | - assert( nData<=(pRtree->nDim2 + 3) ); |
| 173403 | 173630 | |
| 173404 | 173631 | #ifndef SQLITE_RTREE_INT_ONLY |
| 173405 | 173632 | if( pRtree->eCoordType==RTREE_COORD_REAL32 ){ |
| 173406 | | - for(ii=0; ii<nData-4; ii+=2){ |
| 173407 | | - cell.aCoord[ii].f = rtreeValueDown(azData[ii+3]); |
| 173408 | | - cell.aCoord[ii+1].f = rtreeValueUp(azData[ii+4]); |
| 173633 | + for(ii=0; ii<nn; ii+=2){ |
| 173634 | + cell.aCoord[ii].f = rtreeValueDown(aData[ii+3]); |
| 173635 | + cell.aCoord[ii+1].f = rtreeValueUp(aData[ii+4]); |
| 173409 | 173636 | if( cell.aCoord[ii].f>cell.aCoord[ii+1].f ){ |
| 173410 | 173637 | rc = rtreeConstraintError(pRtree, ii+1); |
| 173411 | 173638 | goto constraint; |
| 173412 | 173639 | } |
| 173413 | 173640 | } |
| 173414 | 173641 | }else |
| 173415 | 173642 | #endif |
| 173416 | 173643 | { |
| 173417 | | - for(ii=0; ii<nData-4; ii+=2){ |
| 173418 | | - cell.aCoord[ii].i = sqlite3_value_int(azData[ii+3]); |
| 173419 | | - cell.aCoord[ii+1].i = sqlite3_value_int(azData[ii+4]); |
| 173644 | + for(ii=0; ii<nn; ii+=2){ |
| 173645 | + cell.aCoord[ii].i = sqlite3_value_int(aData[ii+3]); |
| 173646 | + cell.aCoord[ii+1].i = sqlite3_value_int(aData[ii+4]); |
| 173420 | 173647 | if( cell.aCoord[ii].i>cell.aCoord[ii+1].i ){ |
| 173421 | 173648 | rc = rtreeConstraintError(pRtree, ii+1); |
| 173422 | 173649 | goto constraint; |
| 173423 | 173650 | } |
| 173424 | 173651 | } |
| 173425 | 173652 | } |
| 173426 | 173653 | |
| 173427 | 173654 | /* If a rowid value was supplied, check if it is already present in |
| 173428 | 173655 | ** the table. If so, the constraint has failed. */ |
| 173429 | | - if( sqlite3_value_type(azData[2])!=SQLITE_NULL ){ |
| 173430 | | - cell.iRowid = sqlite3_value_int64(azData[2]); |
| 173431 | | - if( sqlite3_value_type(azData[0])==SQLITE_NULL |
| 173432 | | - || sqlite3_value_int64(azData[0])!=cell.iRowid |
| 173656 | + if( sqlite3_value_type(aData[2])!=SQLITE_NULL ){ |
| 173657 | + cell.iRowid = sqlite3_value_int64(aData[2]); |
| 173658 | + if( sqlite3_value_type(aData[0])==SQLITE_NULL |
| 173659 | + || sqlite3_value_int64(aData[0])!=cell.iRowid |
| 173433 | 173660 | ){ |
| 173434 | 173661 | int steprc; |
| 173435 | 173662 | sqlite3_bind_int64(pRtree->pReadRowid, 1, cell.iRowid); |
| 173436 | 173663 | steprc = sqlite3_step(pRtree->pReadRowid); |
| 173437 | 173664 | rc = sqlite3_reset(pRtree->pReadRowid); |
| | @@ -173446,20 +173673,20 @@ |
| 173446 | 173673 | } |
| 173447 | 173674 | bHaveRowid = 1; |
| 173448 | 173675 | } |
| 173449 | 173676 | } |
| 173450 | 173677 | |
| 173451 | | - /* If azData[0] is not an SQL NULL value, it is the rowid of a |
| 173678 | + /* If aData[0] is not an SQL NULL value, it is the rowid of a |
| 173452 | 173679 | ** record to delete from the r-tree table. The following block does |
| 173453 | 173680 | ** just that. |
| 173454 | 173681 | */ |
| 173455 | | - if( sqlite3_value_type(azData[0])!=SQLITE_NULL ){ |
| 173456 | | - rc = rtreeDeleteRowid(pRtree, sqlite3_value_int64(azData[0])); |
| 173682 | + if( sqlite3_value_type(aData[0])!=SQLITE_NULL ){ |
| 173683 | + rc = rtreeDeleteRowid(pRtree, sqlite3_value_int64(aData[0])); |
| 173457 | 173684 | } |
| 173458 | 173685 | |
| 173459 | | - /* If the azData[] array contains more than one element, elements |
| 173460 | | - ** (azData[2]..azData[argc-1]) contain a new record to insert into |
| 173686 | + /* If the aData[] array contains more than one element, elements |
| 173687 | + ** (aData[2]..aData[argc-1]) contain a new record to insert into |
| 173461 | 173688 | ** the r-tree structure. |
| 173462 | 173689 | */ |
| 173463 | 173690 | if( rc==SQLITE_OK && nData>1 ){ |
| 173464 | 173691 | /* Insert the new record into the r-tree */ |
| 173465 | 173692 | RtreeNode *pLeaf = 0; |
| | @@ -173480,10 +173707,20 @@ |
| 173480 | 173707 | rc2 = nodeRelease(pRtree, pLeaf); |
| 173481 | 173708 | if( rc==SQLITE_OK ){ |
| 173482 | 173709 | rc = rc2; |
| 173483 | 173710 | } |
| 173484 | 173711 | } |
| 173712 | + if( pRtree->nAux ){ |
| 173713 | + sqlite3_stmt *pUp = pRtree->pWriteAux; |
| 173714 | + int jj; |
| 173715 | + sqlite3_bind_int64(pUp, 1, *pRowid); |
| 173716 | + for(jj=0; jj<pRtree->nAux; jj++){ |
| 173717 | + sqlite3_bind_value(pUp, jj+2, aData[pRtree->nDim2+3+jj]); |
| 173718 | + } |
| 173719 | + sqlite3_step(pUp); |
| 173720 | + rc = sqlite3_reset(pUp); |
| 173721 | + } |
| 173485 | 173722 | } |
| 173486 | 173723 | |
| 173487 | 173724 | constraint: |
| 173488 | 173725 | rtreeRelease(pRtree); |
| 173489 | 173726 | return rc; |
| | @@ -173636,37 +173873,48 @@ |
| 173636 | 173873 | int rc = SQLITE_OK; |
| 173637 | 173874 | |
| 173638 | 173875 | #define N_STATEMENT 8 |
| 173639 | 173876 | static const char *azSql[N_STATEMENT] = { |
| 173640 | 173877 | /* Write the xxx_node table */ |
| 173641 | | - "INSERT OR REPLACE INTO '%q'.'%q_node' VALUES(:1, :2)", |
| 173642 | | - "DELETE FROM '%q'.'%q_node' WHERE nodeno = :1", |
| 173878 | + "INSERT OR REPLACE INTO '%q'.'%q_node' VALUES(?1, ?2)", |
| 173879 | + "DELETE FROM '%q'.'%q_node' WHERE nodeno = ?1", |
| 173643 | 173880 | |
| 173644 | 173881 | /* Read and write the xxx_rowid table */ |
| 173645 | | - "SELECT nodeno FROM '%q'.'%q_rowid' WHERE rowid = :1", |
| 173646 | | - "INSERT OR REPLACE INTO '%q'.'%q_rowid' VALUES(:1, :2)", |
| 173647 | | - "DELETE FROM '%q'.'%q_rowid' WHERE rowid = :1", |
| 173882 | + "SELECT nodeno FROM '%q'.'%q_rowid' WHERE rowid = ?1", |
| 173883 | + "INSERT OR REPLACE INTO '%q'.'%q_rowid' VALUES(?1, ?2)", |
| 173884 | + "DELETE FROM '%q'.'%q_rowid' WHERE rowid = ?1", |
| 173648 | 173885 | |
| 173649 | 173886 | /* Read and write the xxx_parent table */ |
| 173650 | | - "SELECT parentnode FROM '%q'.'%q_parent' WHERE nodeno = :1", |
| 173651 | | - "INSERT OR REPLACE INTO '%q'.'%q_parent' VALUES(:1, :2)", |
| 173652 | | - "DELETE FROM '%q'.'%q_parent' WHERE nodeno = :1" |
| 173887 | + "SELECT parentnode FROM '%q'.'%q_parent' WHERE nodeno = ?1", |
| 173888 | + "INSERT OR REPLACE INTO '%q'.'%q_parent' VALUES(?1, ?2)", |
| 173889 | + "DELETE FROM '%q'.'%q_parent' WHERE nodeno = ?1" |
| 173653 | 173890 | }; |
| 173654 | 173891 | sqlite3_stmt **appStmt[N_STATEMENT]; |
| 173655 | 173892 | int i; |
| 173656 | 173893 | |
| 173657 | 173894 | pRtree->db = db; |
| 173658 | 173895 | |
| 173659 | 173896 | if( isCreate ){ |
| 173660 | | - char *zCreate = sqlite3_mprintf( |
| 173661 | | -"CREATE TABLE \"%w\".\"%w_node\"(nodeno INTEGER PRIMARY KEY, data BLOB);" |
| 173662 | | -"CREATE TABLE \"%w\".\"%w_rowid\"(rowid INTEGER PRIMARY KEY, nodeno INTEGER);" |
| 173663 | | -"CREATE TABLE \"%w\".\"%w_parent\"(nodeno INTEGER PRIMARY KEY," |
| 173664 | | - " parentnode INTEGER);" |
| 173665 | | -"INSERT INTO '%q'.'%q_node' VALUES(1, zeroblob(%d))", |
| 173666 | | - zDb, zPrefix, zDb, zPrefix, zDb, zPrefix, zDb, zPrefix, pRtree->iNodeSize |
| 173667 | | - ); |
| 173897 | + char *zCreate; |
| 173898 | + sqlite3_str *p = sqlite3_str_new(db); |
| 173899 | + int ii; |
| 173900 | + sqlite3_str_appendf(p, |
| 173901 | + "CREATE TABLE \"%w\".\"%w_rowid\"(rowid INTEGER PRIMARY KEY,nodeno", |
| 173902 | + zDb, zPrefix); |
| 173903 | + for(ii=0; ii<pRtree->nAux; ii++){ |
| 173904 | + sqlite3_str_appendf(p,",a%d",ii); |
| 173905 | + } |
| 173906 | + sqlite3_str_appendf(p, |
| 173907 | + ");CREATE TABLE \"%w\".\"%w_node\"(nodeno INTEGER PRIMARY KEY,data);", |
| 173908 | + zDb, zPrefix); |
| 173909 | + sqlite3_str_appendf(p, |
| 173910 | + "CREATE TABLE \"%w\".\"%w_parent\"(nodeno INTEGER PRIMARY KEY,parentnode);", |
| 173911 | + zDb, zPrefix); |
| 173912 | + sqlite3_str_appendf(p, |
| 173913 | + "INSERT INTO \"%w\".\"%w_node\"VALUES(1,zeroblob(%d))", |
| 173914 | + zDb, zPrefix, pRtree->iNodeSize); |
| 173915 | + zCreate = sqlite3_str_finish(p); |
| 173668 | 173916 | if( !zCreate ){ |
| 173669 | 173917 | return SQLITE_NOMEM; |
| 173670 | 173918 | } |
| 173671 | 173919 | rc = sqlite3_exec(db, zCreate, 0, 0, 0); |
| 173672 | 173920 | sqlite3_free(zCreate); |
| | @@ -173684,18 +173932,54 @@ |
| 173684 | 173932 | appStmt[6] = &pRtree->pWriteParent; |
| 173685 | 173933 | appStmt[7] = &pRtree->pDeleteParent; |
| 173686 | 173934 | |
| 173687 | 173935 | rc = rtreeQueryStat1(db, pRtree); |
| 173688 | 173936 | for(i=0; i<N_STATEMENT && rc==SQLITE_OK; i++){ |
| 173689 | | - char *zSql = sqlite3_mprintf(azSql[i], zDb, zPrefix); |
| 173937 | + char *zSql; |
| 173938 | + const char *zFormat; |
| 173939 | + if( i!=3 || pRtree->nAux==0 ){ |
| 173940 | + zFormat = azSql[i]; |
| 173941 | + }else { |
| 173942 | + /* An UPSERT is very slightly slower than REPLACE, but it is needed |
| 173943 | + ** if there are auxiliary columns */ |
| 173944 | + zFormat = "INSERT INTO\"%w\".\"%w_rowid\"(rowid,nodeno)VALUES(?1,?2)" |
| 173945 | + "ON CONFLICT(rowid)DO UPDATE SET nodeno=excluded.nodeno"; |
| 173946 | + } |
| 173947 | + zSql = sqlite3_mprintf(zFormat, zDb, zPrefix); |
| 173690 | 173948 | if( zSql ){ |
| 173691 | 173949 | rc = sqlite3_prepare_v3(db, zSql, -1, SQLITE_PREPARE_PERSISTENT, |
| 173692 | 173950 | appStmt[i], 0); |
| 173693 | 173951 | }else{ |
| 173694 | 173952 | rc = SQLITE_NOMEM; |
| 173695 | 173953 | } |
| 173696 | 173954 | sqlite3_free(zSql); |
| 173955 | + } |
| 173956 | + if( pRtree->nAux ){ |
| 173957 | + pRtree->zReadAuxSql = sqlite3_mprintf( |
| 173958 | + "SELECT * FROM \"%w\".\"%w_rowid\" WHERE rowid=?1", |
| 173959 | + zDb, zPrefix); |
| 173960 | + if( pRtree->zReadAuxSql==0 ){ |
| 173961 | + rc = SQLITE_NOMEM; |
| 173962 | + }else{ |
| 173963 | + sqlite3_str *p = sqlite3_str_new(db); |
| 173964 | + int ii; |
| 173965 | + char *zSql; |
| 173966 | + sqlite3_str_appendf(p, "UPDATE \"%w\".\"%w_rowid\"SET ", zDb, zPrefix); |
| 173967 | + for(ii=0; ii<pRtree->nAux; ii++){ |
| 173968 | + if( ii ) sqlite3_str_append(p, ",", 1); |
| 173969 | + sqlite3_str_appendf(p,"a%d=?%d",ii,ii+2); |
| 173970 | + } |
| 173971 | + sqlite3_str_appendf(p, " WHERE rowid=?1"); |
| 173972 | + zSql = sqlite3_str_finish(p); |
| 173973 | + if( zSql==0 ){ |
| 173974 | + rc = SQLITE_NOMEM; |
| 173975 | + }else{ |
| 173976 | + rc = sqlite3_prepare_v3(db, zSql, -1, SQLITE_PREPARE_PERSISTENT, |
| 173977 | + &pRtree->pWriteAux, 0); |
| 173978 | + sqlite3_free(zSql); |
| 173979 | + } |
| 173980 | + } |
| 173697 | 173981 | } |
| 173698 | 173982 | |
| 173699 | 173983 | return rc; |
| 173700 | 173984 | } |
| 173701 | 173985 | |
| | @@ -173795,21 +174079,26 @@ |
| 173795 | 174079 | int rc = SQLITE_OK; |
| 173796 | 174080 | Rtree *pRtree; |
| 173797 | 174081 | int nDb; /* Length of string argv[1] */ |
| 173798 | 174082 | int nName; /* Length of string argv[2] */ |
| 173799 | 174083 | int eCoordType = (pAux ? RTREE_COORD_INT32 : RTREE_COORD_REAL32); |
| 174084 | + sqlite3_str *pSql; |
| 174085 | + char *zSql; |
| 174086 | + int ii = 4; |
| 174087 | + int iErr; |
| 173800 | 174088 | |
| 173801 | 174089 | const char *aErrMsg[] = { |
| 173802 | 174090 | 0, /* 0 */ |
| 173803 | 174091 | "Wrong number of columns for an rtree table", /* 1 */ |
| 173804 | 174092 | "Too few columns for an rtree table", /* 2 */ |
| 173805 | | - "Too many columns for an rtree table" /* 3 */ |
| 174093 | + "Too many columns for an rtree table", /* 3 */ |
| 174094 | + "Auxiliary rtree columns must be last" /* 4 */ |
| 173806 | 174095 | }; |
| 173807 | 174096 | |
| 173808 | | - int iErr = (argc<6) ? 2 : argc>(RTREE_MAX_DIMENSIONS*2+4) ? 3 : argc%2; |
| 173809 | | - if( aErrMsg[iErr] ){ |
| 173810 | | - *pzErr = sqlite3_mprintf("%s", aErrMsg[iErr]); |
| 174097 | + assert( RTREE_MAX_AUX_COLUMN<256 ); /* Aux columns counted by a u8 */ |
| 174098 | + if( argc>RTREE_MAX_AUX_COLUMN+3 ){ |
| 174099 | + *pzErr = sqlite3_mprintf("%s", aErrMsg[3]); |
| 173811 | 174100 | return SQLITE_ERROR; |
| 173812 | 174101 | } |
| 173813 | 174102 | |
| 173814 | 174103 | sqlite3_vtab_config(db, SQLITE_VTAB_CONSTRAINT_SUPPORT, 1); |
| 173815 | 174104 | |
| | @@ -173823,57 +174112,77 @@ |
| 173823 | 174112 | memset(pRtree, 0, sizeof(Rtree)+nDb+nName+2); |
| 173824 | 174113 | pRtree->nBusy = 1; |
| 173825 | 174114 | pRtree->base.pModule = &rtreeModule; |
| 173826 | 174115 | pRtree->zDb = (char *)&pRtree[1]; |
| 173827 | 174116 | pRtree->zName = &pRtree->zDb[nDb+1]; |
| 173828 | | - pRtree->nDim = (u8)((argc-4)/2); |
| 173829 | | - pRtree->nDim2 = pRtree->nDim*2; |
| 173830 | | - pRtree->nBytesPerCell = 8 + pRtree->nDim2*4; |
| 173831 | 174117 | pRtree->eCoordType = (u8)eCoordType; |
| 173832 | 174118 | memcpy(pRtree->zDb, argv[1], nDb); |
| 173833 | 174119 | memcpy(pRtree->zName, argv[2], nName); |
| 173834 | 174120 | |
| 173835 | | - /* Figure out the node size to use. */ |
| 173836 | | - rc = getNodeSize(db, pRtree, isCreate, pzErr); |
| 173837 | 174121 | |
| 173838 | 174122 | /* Create/Connect to the underlying relational database schema. If |
| 173839 | 174123 | ** that is successful, call sqlite3_declare_vtab() to configure |
| 173840 | 174124 | ** the r-tree table schema. |
| 173841 | 174125 | */ |
| 173842 | | - if( rc==SQLITE_OK ){ |
| 173843 | | - if( (rc = rtreeSqlInit(pRtree, db, argv[1], argv[2], isCreate)) ){ |
| 173844 | | - *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db)); |
| 173845 | | - }else{ |
| 173846 | | - char *zSql = sqlite3_mprintf("CREATE TABLE x(%s", argv[3]); |
| 173847 | | - char *zTmp; |
| 173848 | | - int ii; |
| 173849 | | - for(ii=4; zSql && ii<argc; ii++){ |
| 173850 | | - zTmp = zSql; |
| 173851 | | - zSql = sqlite3_mprintf("%s, %s", zTmp, argv[ii]); |
| 173852 | | - sqlite3_free(zTmp); |
| 173853 | | - } |
| 173854 | | - if( zSql ){ |
| 173855 | | - zTmp = zSql; |
| 173856 | | - zSql = sqlite3_mprintf("%s);", zTmp); |
| 173857 | | - sqlite3_free(zTmp); |
| 173858 | | - } |
| 173859 | | - if( !zSql ){ |
| 173860 | | - rc = SQLITE_NOMEM; |
| 173861 | | - }else if( SQLITE_OK!=(rc = sqlite3_declare_vtab(db, zSql)) ){ |
| 173862 | | - *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db)); |
| 173863 | | - } |
| 173864 | | - sqlite3_free(zSql); |
| 173865 | | - } |
| 173866 | | - } |
| 173867 | | - |
| 173868 | | - if( rc==SQLITE_OK ){ |
| 173869 | | - *ppVtab = (sqlite3_vtab *)pRtree; |
| 173870 | | - }else{ |
| 173871 | | - assert( *ppVtab==0 ); |
| 173872 | | - assert( pRtree->nBusy==1 ); |
| 173873 | | - rtreeRelease(pRtree); |
| 173874 | | - } |
| 174126 | + pSql = sqlite3_str_new(db); |
| 174127 | + sqlite3_str_appendf(pSql, "CREATE TABLE x(%s", argv[3]); |
| 174128 | + for(ii=4; ii<argc; ii++){ |
| 174129 | + if( argv[ii][0]=='+' ){ |
| 174130 | + pRtree->nAux++; |
| 174131 | + sqlite3_str_appendf(pSql, ",%s", argv[ii]+1); |
| 174132 | + }else if( pRtree->nAux>0 ){ |
| 174133 | + break; |
| 174134 | + }else{ |
| 174135 | + pRtree->nDim2++; |
| 174136 | + sqlite3_str_appendf(pSql, ",%s", argv[ii]); |
| 174137 | + } |
| 174138 | + } |
| 174139 | + sqlite3_str_appendf(pSql, ");"); |
| 174140 | + zSql = sqlite3_str_finish(pSql); |
| 174141 | + if( !zSql ){ |
| 174142 | + rc = SQLITE_NOMEM; |
| 174143 | + }else if( ii<argc ){ |
| 174144 | + *pzErr = sqlite3_mprintf("%s", aErrMsg[4]); |
| 174145 | + rc = SQLITE_ERROR; |
| 174146 | + }else if( SQLITE_OK!=(rc = sqlite3_declare_vtab(db, zSql)) ){ |
| 174147 | + *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db)); |
| 174148 | + } |
| 174149 | + sqlite3_free(zSql); |
| 174150 | + if( rc ) goto rtreeInit_fail; |
| 174151 | + pRtree->nDim = pRtree->nDim2/2; |
| 174152 | + if( pRtree->nDim<1 ){ |
| 174153 | + iErr = 2; |
| 174154 | + }else if( pRtree->nDim2>RTREE_MAX_DIMENSIONS*2 ){ |
| 174155 | + iErr = 3; |
| 174156 | + }else if( pRtree->nDim2 % 2 ){ |
| 174157 | + iErr = 1; |
| 174158 | + }else{ |
| 174159 | + iErr = 0; |
| 174160 | + } |
| 174161 | + if( iErr ){ |
| 174162 | + *pzErr = sqlite3_mprintf("%s", aErrMsg[iErr]); |
| 174163 | + goto rtreeInit_fail; |
| 174164 | + } |
| 174165 | + pRtree->nBytesPerCell = 8 + pRtree->nDim2*4; |
| 174166 | + |
| 174167 | + /* Figure out the node size to use. */ |
| 174168 | + rc = getNodeSize(db, pRtree, isCreate, pzErr); |
| 174169 | + if( rc ) goto rtreeInit_fail; |
| 174170 | + rc = rtreeSqlInit(pRtree, db, argv[1], argv[2], isCreate); |
| 174171 | + if( rc ){ |
| 174172 | + *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db)); |
| 174173 | + goto rtreeInit_fail; |
| 174174 | + } |
| 174175 | + |
| 174176 | + *ppVtab = (sqlite3_vtab *)pRtree; |
| 174177 | + return SQLITE_OK; |
| 174178 | + |
| 174179 | +rtreeInit_fail: |
| 174180 | + if( rc==SQLITE_OK ) rc = SQLITE_ERROR; |
| 174181 | + assert( *ppVtab==0 ); |
| 174182 | + assert( pRtree->nBusy==1 ); |
| 174183 | + rtreeRelease(pRtree); |
| 173875 | 174184 | return rc; |
| 173876 | 174185 | } |
| 173877 | 174186 | |
| 173878 | 174187 | |
| 173879 | 174188 | /* |
| | @@ -174098,11 +174407,11 @@ |
| 174098 | 174407 | ** This function is used to check that the %_parent (if bLeaf==0) or %_rowid |
| 174099 | 174408 | ** (if bLeaf==1) table contains a specified entry. The schemas of the |
| 174100 | 174409 | ** two tables are: |
| 174101 | 174410 | ** |
| 174102 | 174411 | ** CREATE TABLE %_parent(nodeno INTEGER PRIMARY KEY, parentnode INTEGER) |
| 174103 | | -** CREATE TABLE %_rowid(rowid INTEGER PRIMARY KEY, nodeno INTEGER) |
| 174412 | +** CREATE TABLE %_rowid(rowid INTEGER PRIMARY KEY, nodeno INTEGER, ...) |
| 174104 | 174413 | ** |
| 174105 | 174414 | ** In both cases, this function checks that there exists an entry with |
| 174106 | 174415 | ** IPK value iKey and the second column set to iVal. |
| 174107 | 174416 | ** |
| 174108 | 174417 | */ |
| | @@ -174113,12 +174422,12 @@ |
| 174113 | 174422 | i64 iVal /* Expected value for mapping */ |
| 174114 | 174423 | ){ |
| 174115 | 174424 | int rc; |
| 174116 | 174425 | sqlite3_stmt *pStmt; |
| 174117 | 174426 | const char *azSql[2] = { |
| 174118 | | - "SELECT parentnode FROM %Q.'%q_parent' WHERE nodeno=?", |
| 174119 | | - "SELECT nodeno FROM %Q.'%q_rowid' WHERE rowid=?" |
| 174427 | + "SELECT parentnode FROM %Q.'%q_parent' WHERE nodeno=?1", |
| 174428 | + "SELECT nodeno FROM %Q.'%q_rowid' WHERE rowid=?1" |
| 174120 | 174429 | }; |
| 174121 | 174430 | |
| 174122 | 174431 | assert( bLeaf==0 || bLeaf==1 ); |
| 174123 | 174432 | if( pCheck->aCheckMapping[bLeaf]==0 ){ |
| 174124 | 174433 | pCheck->aCheckMapping[bLeaf] = rtreeCheckPrepare(pCheck, |
| | @@ -174298,10 +174607,11 @@ |
| 174298 | 174607 | char **pzReport /* OUT: sqlite3_malloc'd report text */ |
| 174299 | 174608 | ){ |
| 174300 | 174609 | RtreeCheck check; /* Common context for various routines */ |
| 174301 | 174610 | sqlite3_stmt *pStmt = 0; /* Used to find column count of rtree table */ |
| 174302 | 174611 | int bEnd = 0; /* True if transaction should be closed */ |
| 174612 | + int nAux = 0; /* Number of extra columns. */ |
| 174303 | 174613 | |
| 174304 | 174614 | /* Initialize the context object */ |
| 174305 | 174615 | memset(&check, 0, sizeof(check)); |
| 174306 | 174616 | check.db = db; |
| 174307 | 174617 | check.zDb = zDb; |
| | @@ -174312,16 +174622,26 @@ |
| 174312 | 174622 | ** on a consistent snapshot. */ |
| 174313 | 174623 | if( sqlite3_get_autocommit(db) ){ |
| 174314 | 174624 | check.rc = sqlite3_exec(db, "BEGIN", 0, 0, 0); |
| 174315 | 174625 | bEnd = 1; |
| 174316 | 174626 | } |
| 174627 | + |
| 174628 | + /* Find the number of auxiliary columns */ |
| 174629 | + if( check.rc==SQLITE_OK ){ |
| 174630 | + pStmt = rtreeCheckPrepare(&check, "SELECT * FROM %Q.'%q_rowid'", zDb, zTab); |
| 174631 | + if( pStmt ){ |
| 174632 | + nAux = sqlite3_column_count(pStmt) - 2; |
| 174633 | + sqlite3_finalize(pStmt); |
| 174634 | + } |
| 174635 | + check.rc = SQLITE_OK; |
| 174636 | + } |
| 174317 | 174637 | |
| 174318 | 174638 | /* Find number of dimensions in the rtree table. */ |
| 174319 | 174639 | pStmt = rtreeCheckPrepare(&check, "SELECT * FROM %Q.%Q", zDb, zTab); |
| 174320 | 174640 | if( pStmt ){ |
| 174321 | 174641 | int rc; |
| 174322 | | - check.nDim = (sqlite3_column_count(pStmt) - 1) / 2; |
| 174642 | + check.nDim = (sqlite3_column_count(pStmt) - 1 - nAux) / 2; |
| 174323 | 174643 | if( check.nDim<1 ){ |
| 174324 | 174644 | rtreeCheckAppendMsg(&check, "Schema corrupt or not an rtree"); |
| 174325 | 174645 | }else if( SQLITE_ROW==sqlite3_step(pStmt) ){ |
| 174326 | 174646 | check.bInt = (sqlite3_column_type(pStmt, 1)==SQLITE_INTEGER); |
| 174327 | 174647 | } |
| | @@ -189525,11 +189845,11 @@ |
| 189525 | 189845 | }else{ |
| 189526 | 189846 | jsonAppendChar(&x, '$'); |
| 189527 | 189847 | } |
| 189528 | 189848 | if( p->eType==JSON_ARRAY ){ |
| 189529 | 189849 | jsonPrintf(30, &x, "[%d]", p->iRowid); |
| 189530 | | - }else{ |
| 189850 | + }else if( p->eType==JSON_OBJECT ){ |
| 189531 | 189851 | jsonPrintf(pThis->n, &x, ".%.*s", pThis->n-2, pThis->u.zJContent+1); |
| 189532 | 189852 | } |
| 189533 | 189853 | } |
| 189534 | 189854 | jsonResult(&x); |
| 189535 | 189855 | break; |
| | @@ -207256,11 +207576,11 @@ |
| 207256 | 207576 | int nArg, /* Number of args */ |
| 207257 | 207577 | sqlite3_value **apUnused /* Function arguments */ |
| 207258 | 207578 | ){ |
| 207259 | 207579 | assert( nArg==0 ); |
| 207260 | 207580 | UNUSED_PARAM2(nArg, apUnused); |
| 207261 | | - sqlite3_result_text(pCtx, "fts5: 2018-05-14 00:41:12 d0f35739af3b226c8eef39676407293650cde551acef06fe8628fdd5b59bd66a", -1, SQLITE_TRANSIENT); |
| 207581 | + sqlite3_result_text(pCtx, "fts5: 2018-05-30 01:14:20 86ee267ee86f5264774a9f215b1158aeaa2d605e77c205731b5ee3945d7de4c2", -1, SQLITE_TRANSIENT); |
| 207262 | 207582 | } |
| 207263 | 207583 | |
| 207264 | 207584 | static int fts5Init(sqlite3 *db){ |
| 207265 | 207585 | static const sqlite3_module fts5Mod = { |
| 207266 | 207586 | /* iVersion */ 2, |
| | @@ -211526,12 +211846,12 @@ |
| 211526 | 211846 | } |
| 211527 | 211847 | #endif /* SQLITE_CORE */ |
| 211528 | 211848 | #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_STMTVTAB) */ |
| 211529 | 211849 | |
| 211530 | 211850 | /************** End of stmt.c ************************************************/ |
| 211531 | | -#if __LINE__!=211531 |
| 211851 | +#if __LINE__!=211851 |
| 211532 | 211852 | #undef SQLITE_SOURCE_ID |
| 211533 | | -#define SQLITE_SOURCE_ID "2018-05-14 00:41:12 d0f35739af3b226c8eef39676407293650cde551acef06fe8628fdd5b59balt2" |
| 211853 | +#define SQLITE_SOURCE_ID "2018-05-30 01:14:20 86ee267ee86f5264774a9f215b1158aeaa2d605e77c205731b5ee3945d7dalt2" |
| 211534 | 211854 | #endif |
| 211535 | 211855 | /* Return the source-id for this library */ |
| 211536 | 211856 | SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; } |
| 211537 | 211857 | /************************** End of sqlite3.c ******************************/ |
| 211538 | 211858 | |