| | @@ -1147,11 +1147,11 @@ |
| 1147 | 1147 | ** [sqlite3_libversion_number()], [sqlite3_sourceid()], |
| 1148 | 1148 | ** [sqlite_version()] and [sqlite_source_id()]. |
| 1149 | 1149 | */ |
| 1150 | 1150 | #define SQLITE_VERSION "3.21.0" |
| 1151 | 1151 | #define SQLITE_VERSION_NUMBER 3021000 |
| 1152 | | -#define SQLITE_SOURCE_ID "2017-10-02 02:52:54 c9104b59c7ed360291f7f6fc8caae938e9840c77620d598e4096f78183bf807a" |
| 1152 | +#define SQLITE_SOURCE_ID "2017-10-21 17:17:17 fb3ee1b7cac09e4950e4f48b44c277e4f391cb6c8f069644732d2389ca653da4" |
| 1153 | 1153 | |
| 1154 | 1154 | /* |
| 1155 | 1155 | ** CAPI3REF: Run-Time Library Version Numbers |
| 1156 | 1156 | ** KEYWORDS: sqlite3_version sqlite3_sourceid |
| 1157 | 1157 | ** |
| | @@ -10253,12 +10253,12 @@ |
| 10253 | 10253 | ** a single table are grouped together, tables appear in the order in which |
| 10254 | 10254 | ** they were attached to the session object). |
| 10255 | 10255 | */ |
| 10256 | 10256 | SQLITE_API int sqlite3session_patchset( |
| 10257 | 10257 | sqlite3_session *pSession, /* Session object */ |
| 10258 | | - int *pnPatchset, /* OUT: Size of buffer at *ppChangeset */ |
| 10259 | | - void **ppPatchset /* OUT: Buffer containing changeset */ |
| 10258 | + int *pnPatchset, /* OUT: Size of buffer at *ppPatchset */ |
| 10259 | + void **ppPatchset /* OUT: Buffer containing patchset */ |
| 10260 | 10260 | ); |
| 10261 | 10261 | |
| 10262 | 10262 | /* |
| 10263 | 10263 | ** CAPI3REF: Test if a changeset has recorded any changes. |
| 10264 | 10264 | ** |
| | @@ -11021,16 +11021,16 @@ |
| 11021 | 11021 | ** The six streaming API xxx_strm() functions serve similar purposes to the |
| 11022 | 11022 | ** corresponding non-streaming API functions: |
| 11023 | 11023 | ** |
| 11024 | 11024 | ** <table border=1 style="margin-left:8ex;margin-right:8ex"> |
| 11025 | 11025 | ** <tr><th>Streaming function<th>Non-streaming equivalent</th> |
| 11026 | | -** <tr><td>sqlite3changeset_apply_str<td>[sqlite3changeset_apply] |
| 11027 | | -** <tr><td>sqlite3changeset_concat_str<td>[sqlite3changeset_concat] |
| 11028 | | -** <tr><td>sqlite3changeset_invert_str<td>[sqlite3changeset_invert] |
| 11029 | | -** <tr><td>sqlite3changeset_start_str<td>[sqlite3changeset_start] |
| 11030 | | -** <tr><td>sqlite3session_changeset_str<td>[sqlite3session_changeset] |
| 11031 | | -** <tr><td>sqlite3session_patchset_str<td>[sqlite3session_patchset] |
| 11026 | +** <tr><td>sqlite3changeset_apply_strm<td>[sqlite3changeset_apply] |
| 11027 | +** <tr><td>sqlite3changeset_concat_strm<td>[sqlite3changeset_concat] |
| 11028 | +** <tr><td>sqlite3changeset_invert_strm<td>[sqlite3changeset_invert] |
| 11029 | +** <tr><td>sqlite3changeset_start_strm<td>[sqlite3changeset_start] |
| 11030 | +** <tr><td>sqlite3session_changeset_strm<td>[sqlite3session_changeset] |
| 11031 | +** <tr><td>sqlite3session_patchset_strm<td>[sqlite3session_patchset] |
| 11032 | 11032 | ** </table> |
| 11033 | 11033 | ** |
| 11034 | 11034 | ** Non-streaming functions that accept changesets (or patchsets) as input |
| 11035 | 11035 | ** require that the entire changeset be stored in a single buffer in memory. |
| 11036 | 11036 | ** Similarly, those that return a changeset or patchset do so by returning |
| | @@ -12246,10 +12246,25 @@ |
| 12246 | 12246 | #else |
| 12247 | 12247 | # define ALWAYS(X) (X) |
| 12248 | 12248 | # define NEVER(X) (X) |
| 12249 | 12249 | #endif |
| 12250 | 12250 | |
| 12251 | +/* |
| 12252 | +** Some conditionals are optimizations only. In other words, if the |
| 12253 | +** conditionals are replaced with a constant 1 (true) or 0 (false) then |
| 12254 | +** the correct answer is still obtained, though perhaps not as quickly. |
| 12255 | +** |
| 12256 | +** The following macros mark these optimizations conditionals. |
| 12257 | +*/ |
| 12258 | +#if defined(SQLITE_MUTATION_TEST) |
| 12259 | +# define OK_IF_ALWAYS_TRUE(X) (1) |
| 12260 | +# define OK_IF_ALWAYS_FALSE(X) (0) |
| 12261 | +#else |
| 12262 | +# define OK_IF_ALWAYS_TRUE(X) (X) |
| 12263 | +# define OK_IF_ALWAYS_FALSE(X) (X) |
| 12264 | +#endif |
| 12265 | + |
| 12251 | 12266 | /* |
| 12252 | 12267 | ** Some malloc failures are only possible if SQLITE_TEST_REALLOC_STRESS is |
| 12253 | 12268 | ** defined. We need to defend against those failures when testing with |
| 12254 | 12269 | ** SQLITE_TEST_REALLOC_STRESS, but we don't want the unreachable branches |
| 12255 | 12270 | ** during a normal build. The following macro can be used to disable tests |
| | @@ -12999,11 +13014,11 @@ |
| 12999 | 13014 | |
| 13000 | 13015 | /* |
| 13001 | 13016 | ** SELECTTRACE_ENABLED will be either 1 or 0 depending on whether or not |
| 13002 | 13017 | ** the Select query generator tracing logic is turned on. |
| 13003 | 13018 | */ |
| 13004 | | -#if defined(SQLITE_DEBUG) || defined(SQLITE_ENABLE_SELECTTRACE) |
| 13019 | +#if defined(SQLITE_ENABLE_SELECTTRACE) |
| 13005 | 13020 | # define SELECTTRACE_ENABLED 1 |
| 13006 | 13021 | #else |
| 13007 | 13022 | # define SELECTTRACE_ENABLED 0 |
| 13008 | 13023 | #endif |
| 13009 | 13024 | |
| | @@ -14766,14 +14781,16 @@ |
| 14766 | 14781 | SQLITE_PRIVATE int sqlite3OsFileControl(sqlite3_file*,int,void*); |
| 14767 | 14782 | SQLITE_PRIVATE void sqlite3OsFileControlHint(sqlite3_file*,int,void*); |
| 14768 | 14783 | #define SQLITE_FCNTL_DB_UNCHANGED 0xca093fa0 |
| 14769 | 14784 | SQLITE_PRIVATE int sqlite3OsSectorSize(sqlite3_file *id); |
| 14770 | 14785 | SQLITE_PRIVATE int sqlite3OsDeviceCharacteristics(sqlite3_file *id); |
| 14786 | +#ifndef SQLITE_OMIT_WAL |
| 14771 | 14787 | SQLITE_PRIVATE int sqlite3OsShmMap(sqlite3_file *,int,int,int,void volatile **); |
| 14772 | 14788 | SQLITE_PRIVATE int sqlite3OsShmLock(sqlite3_file *id, int, int, int); |
| 14773 | 14789 | SQLITE_PRIVATE void sqlite3OsShmBarrier(sqlite3_file *id); |
| 14774 | 14790 | SQLITE_PRIVATE int sqlite3OsShmUnmap(sqlite3_file *id, int); |
| 14791 | +#endif /* SQLITE_OMIT_WAL */ |
| 14775 | 14792 | SQLITE_PRIVATE int sqlite3OsFetch(sqlite3_file *id, i64, int, void **); |
| 14776 | 14793 | SQLITE_PRIVATE int sqlite3OsUnfetch(sqlite3_file *, i64, void *); |
| 14777 | 14794 | |
| 14778 | 14795 | |
| 14779 | 14796 | /* |
| | @@ -18158,10 +18175,13 @@ |
| 18158 | 18175 | #if SQLITE_MAX_WORKER_THREADS>0 |
| 18159 | 18176 | SQLITE_PRIVATE int sqlite3ThreadCreate(SQLiteThread**,void*(*)(void*),void*); |
| 18160 | 18177 | SQLITE_PRIVATE int sqlite3ThreadJoin(SQLiteThread*, void**); |
| 18161 | 18178 | #endif |
| 18162 | 18179 | |
| 18180 | +#if defined(SQLITE_ENABLE_DBPAGE_VTAB) || defined(SQLITE_TEST) |
| 18181 | +SQLITE_PRIVATE int sqlite3DbpageRegister(sqlite3*); |
| 18182 | +#endif |
| 18163 | 18183 | #if defined(SQLITE_ENABLE_DBSTAT_VTAB) || defined(SQLITE_TEST) |
| 18164 | 18184 | SQLITE_PRIVATE int sqlite3DbstatRegister(sqlite3*); |
| 18165 | 18185 | #endif |
| 18166 | 18186 | |
| 18167 | 18187 | SQLITE_PRIVATE int sqlite3ExprVectorSize(Expr *pExpr); |
| | @@ -20823,10 +20843,11 @@ |
| 20823 | 20843 | return (xSectorSize ? xSectorSize(id) : SQLITE_DEFAULT_SECTOR_SIZE); |
| 20824 | 20844 | } |
| 20825 | 20845 | SQLITE_PRIVATE int sqlite3OsDeviceCharacteristics(sqlite3_file *id){ |
| 20826 | 20846 | return id->pMethods->xDeviceCharacteristics(id); |
| 20827 | 20847 | } |
| 20848 | +#ifndef SQLITE_OMIT_WAL |
| 20828 | 20849 | SQLITE_PRIVATE int sqlite3OsShmLock(sqlite3_file *id, int offset, int n, int flags){ |
| 20829 | 20850 | return id->pMethods->xShmLock(id, offset, n, flags); |
| 20830 | 20851 | } |
| 20831 | 20852 | SQLITE_PRIVATE void sqlite3OsShmBarrier(sqlite3_file *id){ |
| 20832 | 20853 | id->pMethods->xShmBarrier(id); |
| | @@ -20842,10 +20863,11 @@ |
| 20842 | 20863 | void volatile **pp /* OUT: Pointer to mapping */ |
| 20843 | 20864 | ){ |
| 20844 | 20865 | DO_OS_MALLOC_TEST(id); |
| 20845 | 20866 | return id->pMethods->xShmMap(id, iPage, pgsz, bExtend, pp); |
| 20846 | 20867 | } |
| 20868 | +#endif /* SQLITE_OMIT_WAL */ |
| 20847 | 20869 | |
| 20848 | 20870 | #if SQLITE_MAX_MMAP_SIZE>0 |
| 20849 | 20871 | /* The real implementation of xFetch and xUnfetch */ |
| 20850 | 20872 | SQLITE_PRIVATE int sqlite3OsFetch(sqlite3_file *id, i64 iOff, int iAmt, void **pp){ |
| 20851 | 20873 | DO_OS_MALLOC_TEST(id); |
| | @@ -48663,10 +48685,12 @@ |
| 48663 | 48685 | || defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE) |
| 48664 | 48686 | int dc; /* Device characteristics */ |
| 48665 | 48687 | |
| 48666 | 48688 | assert( isOpen(pPager->fd) ); |
| 48667 | 48689 | dc = sqlite3OsDeviceCharacteristics(pPager->fd); |
| 48690 | +#else |
| 48691 | + UNUSED_PARAMETER(pPager); |
| 48668 | 48692 | #endif |
| 48669 | 48693 | |
| 48670 | 48694 | #ifdef SQLITE_ENABLE_BATCH_ATOMIC_WRITE |
| 48671 | 48695 | if( dc&SQLITE_IOCAP_BATCH_ATOMIC ){ |
| 48672 | 48696 | return -1; |
| | @@ -50467,10 +50491,12 @@ |
| 50467 | 50491 | ** Otherwise, SQLITE_OK is returned. |
| 50468 | 50492 | */ |
| 50469 | 50493 | static int readDbPage(PgHdr *pPg){ |
| 50470 | 50494 | Pager *pPager = pPg->pPager; /* Pager object associated with page pPg */ |
| 50471 | 50495 | int rc = SQLITE_OK; /* Return code */ |
| 50496 | + |
| 50497 | +#ifndef SQLITE_OMIT_WAL |
| 50472 | 50498 | u32 iFrame = 0; /* Frame of WAL containing pgno */ |
| 50473 | 50499 | |
| 50474 | 50500 | assert( pPager->eState>=PAGER_READER && !MEMDB ); |
| 50475 | 50501 | assert( isOpen(pPager->fd) ); |
| 50476 | 50502 | |
| | @@ -50478,11 +50504,13 @@ |
| 50478 | 50504 | rc = sqlite3WalFindFrame(pPager->pWal, pPg->pgno, &iFrame); |
| 50479 | 50505 | if( rc ) return rc; |
| 50480 | 50506 | } |
| 50481 | 50507 | if( iFrame ){ |
| 50482 | 50508 | rc = sqlite3WalReadFrame(pPager->pWal, iFrame,pPager->pageSize,pPg->pData); |
| 50483 | | - }else{ |
| 50509 | + }else |
| 50510 | +#endif |
| 50511 | + { |
| 50484 | 50512 | i64 iOffset = (pPg->pgno-1)*(i64)pPager->pageSize; |
| 50485 | 50513 | rc = sqlite3OsRead(pPager->fd, pPg->pData, pPager->pageSize, iOffset); |
| 50486 | 50514 | if( rc==SQLITE_IOERR_SHORT_READ ){ |
| 50487 | 50515 | rc = SQLITE_OK; |
| 50488 | 50516 | } |
| | @@ -61683,11 +61711,11 @@ |
| 61683 | 61711 | static Pgno btreePagecount(BtShared *pBt){ |
| 61684 | 61712 | return pBt->nPage; |
| 61685 | 61713 | } |
| 61686 | 61714 | SQLITE_PRIVATE u32 sqlite3BtreeLastPage(Btree *p){ |
| 61687 | 61715 | assert( sqlite3BtreeHoldsMutex(p) ); |
| 61688 | | - assert( ((p->pBt->nPage)&0x8000000)==0 ); |
| 61716 | + assert( ((p->pBt->nPage)&0x80000000)==0 ); |
| 61689 | 61717 | return btreePagecount(p->pBt); |
| 61690 | 61718 | } |
| 61691 | 61719 | |
| 61692 | 61720 | /* |
| 61693 | 61721 | ** Get a page from the pager and initialize it. |
| | @@ -62565,11 +62593,12 @@ |
| 62565 | 62593 | ** If the user has not set the safety-level for this database connection |
| 62566 | 62594 | ** using "PRAGMA synchronous", and if the safety-level is not already |
| 62567 | 62595 | ** set to the value passed to this function as the second parameter, |
| 62568 | 62596 | ** set it so. |
| 62569 | 62597 | */ |
| 62570 | | -#if SQLITE_DEFAULT_SYNCHRONOUS!=SQLITE_DEFAULT_WAL_SYNCHRONOUS |
| 62598 | +#if SQLITE_DEFAULT_SYNCHRONOUS!=SQLITE_DEFAULT_WAL_SYNCHRONOUS \ |
| 62599 | + && !defined(SQLITE_OMIT_WAL) |
| 62571 | 62600 | static void setDefaultSyncFlag(BtShared *pBt, u8 safety_level){ |
| 62572 | 62601 | sqlite3 *db; |
| 62573 | 62602 | Db *pDb; |
| 62574 | 62603 | if( (db=pBt->db)!=0 && (pDb=db->aDb)!=0 ){ |
| 62575 | 62604 | while( pDb->pBt==0 || pDb->pBt->pBt!=pBt ){ pDb++; } |
| | @@ -64466,22 +64495,27 @@ |
| 64466 | 64495 | */ |
| 64467 | 64496 | static const void *fetchPayload( |
| 64468 | 64497 | BtCursor *pCur, /* Cursor pointing to entry to read from */ |
| 64469 | 64498 | u32 *pAmt /* Write the number of available bytes here */ |
| 64470 | 64499 | ){ |
| 64471 | | - u32 amt; |
| 64500 | + int amt; |
| 64472 | 64501 | assert( pCur!=0 && pCur->iPage>=0 && pCur->pPage); |
| 64473 | 64502 | assert( pCur->eState==CURSOR_VALID ); |
| 64474 | 64503 | assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) ); |
| 64475 | 64504 | assert( cursorOwnsBtShared(pCur) ); |
| 64476 | 64505 | assert( pCur->ix<pCur->pPage->nCell ); |
| 64477 | 64506 | assert( pCur->info.nSize>0 ); |
| 64478 | 64507 | assert( pCur->info.pPayload>pCur->pPage->aData || CORRUPT_DB ); |
| 64479 | 64508 | assert( pCur->info.pPayload<pCur->pPage->aDataEnd ||CORRUPT_DB); |
| 64480 | | - amt = (int)(pCur->pPage->aDataEnd - pCur->info.pPayload); |
| 64481 | | - if( pCur->info.nLocal<amt ) amt = pCur->info.nLocal; |
| 64482 | | - *pAmt = amt; |
| 64509 | + amt = pCur->info.nLocal; |
| 64510 | + if( amt>(int)(pCur->pPage->aDataEnd - pCur->info.pPayload) ){ |
| 64511 | + /* There is too little space on the page for the expected amount |
| 64512 | + ** of local content. Database must be corrupt. */ |
| 64513 | + assert( CORRUPT_DB ); |
| 64514 | + amt = MAX(0, (int)(pCur->pPage->aDataEnd - pCur->info.pPayload)); |
| 64515 | + } |
| 64516 | + *pAmt = (u32)amt; |
| 64483 | 64517 | return (void*)pCur->info.pPayload; |
| 64484 | 64518 | } |
| 64485 | 64519 | |
| 64486 | 64520 | |
| 64487 | 64521 | /* |
| | @@ -66944,12 +66978,10 @@ |
| 66944 | 66978 | szScratch = |
| 66945 | 66979 | nMaxCells*sizeof(u8*) /* b.apCell */ |
| 66946 | 66980 | + nMaxCells*sizeof(u16) /* b.szCell */ |
| 66947 | 66981 | + pBt->pageSize; /* aSpace1 */ |
| 66948 | 66982 | |
| 66949 | | - /* EVIDENCE-OF: R-28375-38319 SQLite will never request a scratch buffer |
| 66950 | | - ** that is more than 6 times the database page size. */ |
| 66951 | 66983 | assert( szScratch<=6*(int)pBt->pageSize ); |
| 66952 | 66984 | b.apCell = sqlite3StackAllocRaw(0, szScratch ); |
| 66953 | 66985 | if( b.apCell==0 ){ |
| 66954 | 66986 | rc = SQLITE_NOMEM_BKPT; |
| 66955 | 66987 | goto balance_cleanup; |
| | @@ -75017,10 +75049,14 @@ |
| 75017 | 75049 | ** To look at it another way, this routine resets the state of the |
| 75018 | 75050 | ** virtual machine from VDBE_MAGIC_RUN or VDBE_MAGIC_HALT back to |
| 75019 | 75051 | ** VDBE_MAGIC_INIT. |
| 75020 | 75052 | */ |
| 75021 | 75053 | SQLITE_PRIVATE int sqlite3VdbeReset(Vdbe *p){ |
| 75054 | +#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE) |
| 75055 | + int i; |
| 75056 | +#endif |
| 75057 | + |
| 75022 | 75058 | sqlite3 *db; |
| 75023 | 75059 | db = p->db; |
| 75024 | 75060 | |
| 75025 | 75061 | /* If the VM did not run to completion or if it encountered an |
| 75026 | 75062 | ** error, then it might not have been halted properly. So halt |
| | @@ -75048,11 +75084,10 @@ |
| 75048 | 75084 | /* Reset register contents and reclaim error message memory. |
| 75049 | 75085 | */ |
| 75050 | 75086 | #ifdef SQLITE_DEBUG |
| 75051 | 75087 | /* Execute assert() statements to ensure that the Vdbe.apCsr[] and |
| 75052 | 75088 | ** Vdbe.aMem[] arrays have already been cleaned up. */ |
| 75053 | | - int i; |
| 75054 | 75089 | if( p->apCsr ) for(i=0; i<p->nCursor; i++) assert( p->apCsr[i]==0 ); |
| 75055 | 75090 | if( p->aMem ){ |
| 75056 | 75091 | for(i=0; i<p->nMem; i++) assert( p->aMem[i].flags==MEM_Undefined ); |
| 75057 | 75092 | } |
| 75058 | 75093 | #endif |
| | @@ -75064,11 +75099,10 @@ |
| 75064 | 75099 | */ |
| 75065 | 75100 | #ifdef VDBE_PROFILE |
| 75066 | 75101 | { |
| 75067 | 75102 | FILE *out = fopen("vdbe_profile.out", "a"); |
| 75068 | 75103 | if( out ){ |
| 75069 | | - int i; |
| 75070 | 75104 | fprintf(out, "---- "); |
| 75071 | 75105 | for(i=0; i<p->nOp; i++){ |
| 75072 | 75106 | fprintf(out, "%02x", p->aOp[i].opcode); |
| 75073 | 75107 | } |
| 75074 | 75108 | fprintf(out, "\n"); |
| | @@ -91194,16 +91228,14 @@ |
| 91194 | 91228 | int moreToDo = 1; |
| 91195 | 91229 | |
| 91196 | 91230 | pOrderBy = pSelect->pOrderBy; |
| 91197 | 91231 | if( pOrderBy==0 ) return 0; |
| 91198 | 91232 | db = pParse->db; |
| 91199 | | -#if SQLITE_MAX_COLUMN |
| 91200 | 91233 | if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){ |
| 91201 | 91234 | sqlite3ErrorMsg(pParse, "too many terms in ORDER BY clause"); |
| 91202 | 91235 | return 1; |
| 91203 | 91236 | } |
| 91204 | | -#endif |
| 91205 | 91237 | for(i=0; i<pOrderBy->nExpr; i++){ |
| 91206 | 91238 | pOrderBy->a[i].done = 0; |
| 91207 | 91239 | } |
| 91208 | 91240 | pSelect->pNext = 0; |
| 91209 | 91241 | while( pSelect->pPrior ){ |
| | @@ -91291,16 +91323,14 @@ |
| 91291 | 91323 | sqlite3 *db = pParse->db; |
| 91292 | 91324 | ExprList *pEList; |
| 91293 | 91325 | struct ExprList_item *pItem; |
| 91294 | 91326 | |
| 91295 | 91327 | if( pOrderBy==0 || pParse->db->mallocFailed ) return 0; |
| 91296 | | -#if SQLITE_MAX_COLUMN |
| 91297 | 91328 | if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){ |
| 91298 | 91329 | sqlite3ErrorMsg(pParse, "too many terms in %s BY clause", zType); |
| 91299 | 91330 | return 1; |
| 91300 | 91331 | } |
| 91301 | | -#endif |
| 91302 | 91332 | pEList = pSelect->pEList; |
| 91303 | 91333 | assert( pEList!=0 ); /* sqlite3SelectNew() guarantees this */ |
| 91304 | 91334 | for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){ |
| 91305 | 91335 | if( pItem->u.x.iOrderByCol ){ |
| 91306 | 91336 | if( pItem->u.x.iOrderByCol>pEList->nExpr ){ |
| | @@ -101733,16 +101763,14 @@ |
| 101733 | 101763 | char *z; |
| 101734 | 101764 | char *zType; |
| 101735 | 101765 | Column *pCol; |
| 101736 | 101766 | sqlite3 *db = pParse->db; |
| 101737 | 101767 | if( (p = pParse->pNewTable)==0 ) return; |
| 101738 | | -#if SQLITE_MAX_COLUMN |
| 101739 | 101768 | if( p->nCol+1>db->aLimit[SQLITE_LIMIT_COLUMN] ){ |
| 101740 | 101769 | sqlite3ErrorMsg(pParse, "too many columns on %s", p->zName); |
| 101741 | 101770 | return; |
| 101742 | 101771 | } |
| 101743 | | -#endif |
| 101744 | 101772 | z = sqlite3DbMallocRaw(db, pName->n + pType->n + 2); |
| 101745 | 101773 | if( z==0 ) return; |
| 101746 | 101774 | memcpy(z, pName->z, pName->n); |
| 101747 | 101775 | z[pName->n] = 0; |
| 101748 | 101776 | sqlite3Dequote(z); |
| | @@ -103036,18 +103064,10 @@ |
| 103036 | 103064 | ** Code to update the sqlite_master tables and internal schema definitions |
| 103037 | 103065 | ** in case a root-page belonging to another table is moved by the btree layer |
| 103038 | 103066 | ** is also added (this can happen with an auto-vacuum database). |
| 103039 | 103067 | */ |
| 103040 | 103068 | static void destroyTable(Parse *pParse, Table *pTab){ |
| 103041 | | -#ifdef SQLITE_OMIT_AUTOVACUUM |
| 103042 | | - Index *pIdx; |
| 103043 | | - int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema); |
| 103044 | | - destroyRootPage(pParse, pTab->tnum, iDb); |
| 103045 | | - for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
| 103046 | | - destroyRootPage(pParse, pIdx->tnum, iDb); |
| 103047 | | - } |
| 103048 | | -#else |
| 103049 | 103069 | /* If the database may be auto-vacuum capable (if SQLITE_OMIT_AUTOVACUUM |
| 103050 | 103070 | ** is not defined), then it is important to call OP_Destroy on the |
| 103051 | 103071 | ** table and index root-pages in order, starting with the numerically |
| 103052 | 103072 | ** largest root-page number. This guarantees that none of the root-pages |
| 103053 | 103073 | ** to be destroyed is relocated by an earlier OP_Destroy. i.e. if the |
| | @@ -103086,11 +103106,10 @@ |
| 103086 | 103106 | assert( iDb>=0 && iDb<pParse->db->nDb ); |
| 103087 | 103107 | destroyRootPage(pParse, iLargest, iDb); |
| 103088 | 103108 | iDestroyed = iLargest; |
| 103089 | 103109 | } |
| 103090 | 103110 | } |
| 103091 | | -#endif |
| 103092 | 103111 | } |
| 103093 | 103112 | |
| 103094 | 103113 | /* |
| 103095 | 103114 | ** Remove entries from the sqlite_statN tables (for N in (1,2,3)) |
| 103096 | 103115 | ** after a DROP INDEX or DROP TABLE command. |
| | @@ -110324,11 +110343,10 @@ |
| 110324 | 110343 | IdList *pColumn, /* Column names corresponding to IDLIST. */ |
| 110325 | 110344 | int onError /* How to handle constraint errors */ |
| 110326 | 110345 | ){ |
| 110327 | 110346 | sqlite3 *db; /* The main database structure */ |
| 110328 | 110347 | Table *pTab; /* The table to insert into. aka TABLE */ |
| 110329 | | - char *zTab; /* Name of the table into which we are inserting */ |
| 110330 | 110348 | int i, j; /* Loop counters */ |
| 110331 | 110349 | Vdbe *v; /* Generate code into this virtual machine */ |
| 110332 | 110350 | Index *pIdx; /* For looping over indices of the table */ |
| 110333 | 110351 | int nColumn; /* Number of columns in the data */ |
| 110334 | 110352 | int nHidden = 0; /* Number of hidden columns if TABLE is virtual */ |
| | @@ -110380,12 +110398,10 @@ |
| 110380 | 110398 | } |
| 110381 | 110399 | |
| 110382 | 110400 | /* Locate the table into which we will be inserting new information. |
| 110383 | 110401 | */ |
| 110384 | 110402 | assert( pTabList->nSrc==1 ); |
| 110385 | | - zTab = pTabList->a[0].zName; |
| 110386 | | - if( NEVER(zTab==0) ) goto insert_cleanup; |
| 110387 | 110403 | pTab = sqlite3SrcListLookup(pParse, pTabList); |
| 110388 | 110404 | if( pTab==0 ){ |
| 110389 | 110405 | goto insert_cleanup; |
| 110390 | 110406 | } |
| 110391 | 110407 | iDb = sqlite3SchemaToIndex(db, pTab->pSchema); |
| | @@ -112403,11 +112419,11 @@ |
| 112403 | 112419 | void (*result_value)(sqlite3_context*,sqlite3_value*); |
| 112404 | 112420 | void * (*rollback_hook)(sqlite3*,void(*)(void*),void*); |
| 112405 | 112421 | int (*set_authorizer)(sqlite3*,int(*)(void*,int,const char*,const char*, |
| 112406 | 112422 | const char*,const char*),void*); |
| 112407 | 112423 | void (*set_auxdata)(sqlite3_context*,int,void*,void (*)(void*)); |
| 112408 | | - char * (*snprintf)(int,char*,const char*,...); |
| 112424 | + char * (*xsnprintf)(int,char*,const char*,...); |
| 112409 | 112425 | int (*step)(sqlite3_stmt*); |
| 112410 | 112426 | int (*table_column_metadata)(sqlite3*,const char*,const char*,const char*, |
| 112411 | 112427 | char const**,char const**,int*,int*,int*); |
| 112412 | 112428 | void (*thread_cleanup)(void); |
| 112413 | 112429 | int (*total_changes)(sqlite3*); |
| | @@ -112687,11 +112703,11 @@ |
| 112687 | 112703 | #define sqlite3_result_text16le sqlite3_api->result_text16le |
| 112688 | 112704 | #define sqlite3_result_value sqlite3_api->result_value |
| 112689 | 112705 | #define sqlite3_rollback_hook sqlite3_api->rollback_hook |
| 112690 | 112706 | #define sqlite3_set_authorizer sqlite3_api->set_authorizer |
| 112691 | 112707 | #define sqlite3_set_auxdata sqlite3_api->set_auxdata |
| 112692 | | -#define sqlite3_snprintf sqlite3_api->snprintf |
| 112708 | +#define sqlite3_snprintf sqlite3_api->xsnprintf |
| 112693 | 112709 | #define sqlite3_step sqlite3_api->step |
| 112694 | 112710 | #define sqlite3_table_column_metadata sqlite3_api->table_column_metadata |
| 112695 | 112711 | #define sqlite3_thread_cleanup sqlite3_api->thread_cleanup |
| 112696 | 112712 | #define sqlite3_total_changes sqlite3_api->total_changes |
| 112697 | 112713 | #define sqlite3_trace sqlite3_api->trace |
| | @@ -117748,11 +117764,11 @@ |
| 117748 | 117764 | sqlite3ExprListDelete(db, p->pGroupBy); |
| 117749 | 117765 | sqlite3ExprDelete(db, p->pHaving); |
| 117750 | 117766 | sqlite3ExprListDelete(db, p->pOrderBy); |
| 117751 | 117767 | sqlite3ExprDelete(db, p->pLimit); |
| 117752 | 117768 | sqlite3ExprDelete(db, p->pOffset); |
| 117753 | | - if( p->pWith ) sqlite3WithDelete(db, p->pWith); |
| 117769 | + if( OK_IF_ALWAYS_TRUE(p->pWith) ) sqlite3WithDelete(db, p->pWith); |
| 117754 | 117770 | if( bFree ) sqlite3DbFreeNN(db, p); |
| 117755 | 117771 | p = pPrior; |
| 117756 | 117772 | bFree = 1; |
| 117757 | 117773 | } |
| 117758 | 117774 | } |
| | @@ -117844,11 +117860,11 @@ |
| 117844 | 117860 | |
| 117845 | 117861 | /* |
| 117846 | 117862 | ** Delete the given Select structure and all of its substructures. |
| 117847 | 117863 | */ |
| 117848 | 117864 | SQLITE_PRIVATE void sqlite3SelectDelete(sqlite3 *db, Select *p){ |
| 117849 | | - if( p ) clearSelect(db, p, 1); |
| 117865 | + if( OK_IF_ALWAYS_TRUE(p) ) clearSelect(db, p, 1); |
| 117850 | 117866 | } |
| 117851 | 117867 | |
| 117852 | 117868 | /* |
| 117853 | 117869 | ** Return a pointer to the right-most SELECT statement in a compound. |
| 117854 | 117870 | */ |
| | @@ -118085,15 +118101,14 @@ |
| 118085 | 118101 | |
| 118086 | 118102 | pSrc = p->pSrc; |
| 118087 | 118103 | pLeft = &pSrc->a[0]; |
| 118088 | 118104 | pRight = &pLeft[1]; |
| 118089 | 118105 | for(i=0; i<pSrc->nSrc-1; i++, pRight++, pLeft++){ |
| 118090 | | - Table *pLeftTab = pLeft->pTab; |
| 118091 | 118106 | Table *pRightTab = pRight->pTab; |
| 118092 | 118107 | int isOuter; |
| 118093 | 118108 | |
| 118094 | | - if( NEVER(pLeftTab==0 || pRightTab==0) ) continue; |
| 118109 | + if( NEVER(pLeft->pTab==0 || pRightTab==0) ) continue; |
| 118095 | 118110 | isOuter = (pRight->fg.jointype & JT_OUTER)!=0; |
| 118096 | 118111 | |
| 118097 | 118112 | /* When the NATURAL keyword is present, add WHERE clause terms for |
| 118098 | 118113 | ** every column that the two tables have in common. |
| 118099 | 118114 | */ |
| | @@ -119035,27 +119050,27 @@ |
| 119035 | 119050 | ** |
| 119036 | 119051 | ** This routine has either 3 or 6 parameters depending on whether or not |
| 119037 | 119052 | ** the SQLITE_ENABLE_COLUMN_METADATA compile-time option is used. |
| 119038 | 119053 | */ |
| 119039 | 119054 | #ifdef SQLITE_ENABLE_COLUMN_METADATA |
| 119040 | | -# define columnType(A,B,C,D,E,F) columnTypeImpl(A,B,C,D,E,F) |
| 119055 | +# define columnType(A,B,C,D,E) columnTypeImpl(A,B,C,D,E) |
| 119041 | 119056 | #else /* if !defined(SQLITE_ENABLE_COLUMN_METADATA) */ |
| 119042 | | -# define columnType(A,B,C,D,E,F) columnTypeImpl(A,B,F) |
| 119057 | +# define columnType(A,B,C,D,E) columnTypeImpl(A,B) |
| 119043 | 119058 | #endif |
| 119044 | 119059 | static const char *columnTypeImpl( |
| 119045 | 119060 | NameContext *pNC, |
| 119061 | +#ifndef SQLITE_ENABLE_COLUMN_METADATA |
| 119062 | + Expr *pExpr |
| 119063 | +#else |
| 119046 | 119064 | Expr *pExpr, |
| 119047 | | -#ifdef SQLITE_ENABLE_COLUMN_METADATA |
| 119048 | 119065 | const char **pzOrigDb, |
| 119049 | 119066 | const char **pzOrigTab, |
| 119050 | | - const char **pzOrigCol, |
| 119067 | + const char **pzOrigCol |
| 119051 | 119068 | #endif |
| 119052 | | - u8 *pEstWidth |
| 119053 | 119069 | ){ |
| 119054 | 119070 | char const *zType = 0; |
| 119055 | 119071 | int j; |
| 119056 | | - u8 estWidth = 1; |
| 119057 | 119072 | #ifdef SQLITE_ENABLE_COLUMN_METADATA |
| 119058 | 119073 | char const *zOrigDb = 0; |
| 119059 | 119074 | char const *zOrigTab = 0; |
| 119060 | 119075 | char const *zOrigCol = 0; |
| 119061 | 119076 | #endif |
| | @@ -119120,37 +119135,36 @@ |
| 119120 | 119135 | NameContext sNC; |
| 119121 | 119136 | Expr *p = pS->pEList->a[iCol].pExpr; |
| 119122 | 119137 | sNC.pSrcList = pS->pSrc; |
| 119123 | 119138 | sNC.pNext = pNC; |
| 119124 | 119139 | sNC.pParse = pNC->pParse; |
| 119125 | | - zType = columnType(&sNC, p,&zOrigDb,&zOrigTab,&zOrigCol, &estWidth); |
| 119140 | + zType = columnType(&sNC, p,&zOrigDb,&zOrigTab,&zOrigCol); |
| 119126 | 119141 | } |
| 119127 | | - }else if( pTab->pSchema ){ |
| 119128 | | - /* A real table */ |
| 119142 | + }else{ |
| 119143 | + /* A real table or a CTE table */ |
| 119129 | 119144 | assert( !pS ); |
| 119145 | +#ifdef SQLITE_ENABLE_COLUMN_METADATA |
| 119130 | 119146 | if( iCol<0 ) iCol = pTab->iPKey; |
| 119131 | | - assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) ); |
| 119132 | | -#ifdef SQLITE_ENABLE_COLUMN_METADATA |
| 119147 | + assert( iCol==XN_ROWID || (iCol>=0 && iCol<pTab->nCol) ); |
| 119133 | 119148 | if( iCol<0 ){ |
| 119134 | 119149 | zType = "INTEGER"; |
| 119135 | 119150 | zOrigCol = "rowid"; |
| 119136 | 119151 | }else{ |
| 119137 | 119152 | zOrigCol = pTab->aCol[iCol].zName; |
| 119138 | 119153 | zType = sqlite3ColumnType(&pTab->aCol[iCol],0); |
| 119139 | | - estWidth = pTab->aCol[iCol].szEst; |
| 119140 | 119154 | } |
| 119141 | 119155 | zOrigTab = pTab->zName; |
| 119142 | | - if( pNC->pParse ){ |
| 119156 | + if( pNC->pParse && pTab->pSchema ){ |
| 119143 | 119157 | int iDb = sqlite3SchemaToIndex(pNC->pParse->db, pTab->pSchema); |
| 119144 | 119158 | zOrigDb = pNC->pParse->db->aDb[iDb].zDbSName; |
| 119145 | 119159 | } |
| 119146 | 119160 | #else |
| 119161 | + assert( iCol==XN_ROWID || (iCol>=0 && iCol<pTab->nCol) ); |
| 119147 | 119162 | if( iCol<0 ){ |
| 119148 | 119163 | zType = "INTEGER"; |
| 119149 | 119164 | }else{ |
| 119150 | 119165 | zType = sqlite3ColumnType(&pTab->aCol[iCol],0); |
| 119151 | | - estWidth = pTab->aCol[iCol].szEst; |
| 119152 | 119166 | } |
| 119153 | 119167 | #endif |
| 119154 | 119168 | } |
| 119155 | 119169 | break; |
| 119156 | 119170 | } |
| | @@ -119165,11 +119179,11 @@ |
| 119165 | 119179 | Expr *p = pS->pEList->a[0].pExpr; |
| 119166 | 119180 | assert( ExprHasProperty(pExpr, EP_xIsSelect) ); |
| 119167 | 119181 | sNC.pSrcList = pS->pSrc; |
| 119168 | 119182 | sNC.pNext = pNC; |
| 119169 | 119183 | sNC.pParse = pNC->pParse; |
| 119170 | | - zType = columnType(&sNC, p, &zOrigDb, &zOrigTab, &zOrigCol, &estWidth); |
| 119184 | + zType = columnType(&sNC, p, &zOrigDb, &zOrigTab, &zOrigCol); |
| 119171 | 119185 | break; |
| 119172 | 119186 | } |
| 119173 | 119187 | #endif |
| 119174 | 119188 | } |
| 119175 | 119189 | |
| | @@ -119179,11 +119193,10 @@ |
| 119179 | 119193 | *pzOrigDb = zOrigDb; |
| 119180 | 119194 | *pzOrigTab = zOrigTab; |
| 119181 | 119195 | *pzOrigCol = zOrigCol; |
| 119182 | 119196 | } |
| 119183 | 119197 | #endif |
| 119184 | | - if( pEstWidth ) *pEstWidth = estWidth; |
| 119185 | 119198 | return zType; |
| 119186 | 119199 | } |
| 119187 | 119200 | |
| 119188 | 119201 | /* |
| 119189 | 119202 | ** Generate code that will tell the VDBE the declaration types of columns |
| | @@ -119206,21 +119219,21 @@ |
| 119206 | 119219 | const char *zType; |
| 119207 | 119220 | #ifdef SQLITE_ENABLE_COLUMN_METADATA |
| 119208 | 119221 | const char *zOrigDb = 0; |
| 119209 | 119222 | const char *zOrigTab = 0; |
| 119210 | 119223 | const char *zOrigCol = 0; |
| 119211 | | - zType = columnType(&sNC, p, &zOrigDb, &zOrigTab, &zOrigCol, 0); |
| 119224 | + zType = columnType(&sNC, p, &zOrigDb, &zOrigTab, &zOrigCol); |
| 119212 | 119225 | |
| 119213 | 119226 | /* The vdbe must make its own copy of the column-type and other |
| 119214 | 119227 | ** column specific strings, in case the schema is reset before this |
| 119215 | 119228 | ** virtual machine is deleted. |
| 119216 | 119229 | */ |
| 119217 | 119230 | sqlite3VdbeSetColName(v, i, COLNAME_DATABASE, zOrigDb, SQLITE_TRANSIENT); |
| 119218 | 119231 | sqlite3VdbeSetColName(v, i, COLNAME_TABLE, zOrigTab, SQLITE_TRANSIENT); |
| 119219 | 119232 | sqlite3VdbeSetColName(v, i, COLNAME_COLUMN, zOrigCol, SQLITE_TRANSIENT); |
| 119220 | 119233 | #else |
| 119221 | | - zType = columnType(&sNC, p, 0, 0, 0, 0); |
| 119234 | + zType = columnType(&sNC, p, 0, 0, 0); |
| 119222 | 119235 | #endif |
| 119223 | 119236 | sqlite3VdbeSetColName(v, i, COLNAME_DECLTYPE, zType, SQLITE_TRANSIENT); |
| 119224 | 119237 | } |
| 119225 | 119238 | #endif /* !defined(SQLITE_OMIT_DECLTYPE) */ |
| 119226 | 119239 | } |
| | @@ -119365,10 +119378,11 @@ |
| 119365 | 119378 | sqlite3HashInit(&ht); |
| 119366 | 119379 | if( pEList ){ |
| 119367 | 119380 | nCol = pEList->nExpr; |
| 119368 | 119381 | aCol = sqlite3DbMallocZero(db, sizeof(aCol[0])*nCol); |
| 119369 | 119382 | testcase( aCol==0 ); |
| 119383 | + if( nCol>32767 ) nCol = 32767; |
| 119370 | 119384 | }else{ |
| 119371 | 119385 | nCol = 0; |
| 119372 | 119386 | aCol = 0; |
| 119373 | 119387 | } |
| 119374 | 119388 | assert( nCol==(i16)nCol ); |
| | @@ -119461,11 +119475,10 @@ |
| 119461 | 119475 | Column *pCol; |
| 119462 | 119476 | CollSeq *pColl; |
| 119463 | 119477 | int i; |
| 119464 | 119478 | Expr *p; |
| 119465 | 119479 | struct ExprList_item *a; |
| 119466 | | - u64 szAll = 0; |
| 119467 | 119480 | |
| 119468 | 119481 | assert( pSelect!=0 ); |
| 119469 | 119482 | assert( (pSelect->selFlags & SF_Resolved)!=0 ); |
| 119470 | 119483 | assert( pTab->nCol==pSelect->pEList->nExpr || db->mallocFailed ); |
| 119471 | 119484 | if( db->mallocFailed ) return; |
| | @@ -119474,14 +119487,15 @@ |
| 119474 | 119487 | a = pSelect->pEList->a; |
| 119475 | 119488 | for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){ |
| 119476 | 119489 | const char *zType; |
| 119477 | 119490 | int n, m; |
| 119478 | 119491 | p = a[i].pExpr; |
| 119479 | | - zType = columnType(&sNC, p, 0, 0, 0, &pCol->szEst); |
| 119480 | | - szAll += pCol->szEst; |
| 119492 | + zType = columnType(&sNC, p, 0, 0, 0); |
| 119493 | + /* pCol->szEst = ... // Column size est for SELECT tables never used */ |
| 119481 | 119494 | pCol->affinity = sqlite3ExprAffinity(p); |
| 119482 | | - if( zType && (m = sqlite3Strlen30(zType))>0 ){ |
| 119495 | + if( zType ){ |
| 119496 | + m = sqlite3Strlen30(zType); |
| 119483 | 119497 | n = sqlite3Strlen30(pCol->zName); |
| 119484 | 119498 | pCol->zName = sqlite3DbReallocOrFree(db, pCol->zName, n+m+2); |
| 119485 | 119499 | if( pCol->zName ){ |
| 119486 | 119500 | memcpy(&pCol->zName[n+1], zType, m+1); |
| 119487 | 119501 | pCol->colFlags |= COLFLAG_HASTYPE; |
| | @@ -119491,11 +119505,11 @@ |
| 119491 | 119505 | pColl = sqlite3ExprCollSeq(pParse, p); |
| 119492 | 119506 | if( pColl && pCol->zColl==0 ){ |
| 119493 | 119507 | pCol->zColl = sqlite3DbStrDup(db, pColl->zName); |
| 119494 | 119508 | } |
| 119495 | 119509 | } |
| 119496 | | - pTab->szTabRow = sqlite3LogEst(szAll*4); |
| 119510 | + pTab->szTabRow = 1; /* Any non-zero value works */ |
| 119497 | 119511 | } |
| 119498 | 119512 | |
| 119499 | 119513 | /* |
| 119500 | 119514 | ** Given a SELECT statement, generate a Table structure that describes |
| 119501 | 119515 | ** the result set of that SELECT. |
| | @@ -121031,11 +121045,11 @@ |
| 121031 | 121045 | ** until we introduced the group_concat() function. |
| 121032 | 121046 | ** |
| 121033 | 121047 | ** (17) If the subquery is a compound select, then |
| 121034 | 121048 | ** (17a) all compound operators must be a UNION ALL, and |
| 121035 | 121049 | ** (17b) no terms within the subquery compound may be aggregate |
| 121036 | | -** or DISTINT, and |
| 121050 | +** or DISTINCT, and |
| 121037 | 121051 | ** (17c) every term within the subquery compound must have a FROM clause |
| 121038 | 121052 | ** (17d) the outer query may not be |
| 121039 | 121053 | ** (17d1) aggregate, or |
| 121040 | 121054 | ** (17d2) DISTINCT, or |
| 121041 | 121055 | ** (17d3) a join. |
| | @@ -121058,23 +121072,25 @@ |
| 121058 | 121072 | ** columns of the sub-query. |
| 121059 | 121073 | ** |
| 121060 | 121074 | ** (19) If the subquery uses LIMIT then the outer query may not |
| 121061 | 121075 | ** have a WHERE clause. |
| 121062 | 121076 | ** |
| 121063 | | -** (20) If the sub-query is a compound select, then it must not use |
| 121064 | | -** an ORDER BY clause. Ticket #3773. We could relax this constraint |
| 121065 | | -** somewhat by saying that the terms of the ORDER BY clause must |
| 121066 | | -** appear as unmodified result columns in the outer query. But we |
| 121067 | | -** have other optimizations in mind to deal with that case. |
| 121077 | +** (**) Subsumed into (17d3). Was: If the sub-query is a compound select, |
| 121078 | +** then it must not use an ORDER BY clause - Ticket #3773. Because |
| 121079 | +** of (17d3), then only way to have a compound subquery is if it is |
| 121080 | +** the only term in the FROM clause of the outer query. But if the |
| 121081 | +** only term in the FROM clause has an ORDER BY, then it will be |
| 121082 | +** implemented as a co-routine and the flattener will never be called. |
| 121068 | 121083 | ** |
| 121069 | 121084 | ** (21) If the subquery uses LIMIT then the outer query may not be |
| 121070 | 121085 | ** DISTINCT. (See ticket [752e1646fc]). |
| 121071 | 121086 | ** |
| 121072 | 121087 | ** (22) The subquery may not be a recursive CTE. |
| 121073 | 121088 | ** |
| 121074 | | -** (23) If the outer query is a recursive CTE, then the sub-query may not be |
| 121075 | | -** a compound query. This restriction is because transforming the |
| 121089 | +** (**) Subsumed into restriction (17d3). Was: If the outer query is |
| 121090 | +** a recursive CTE, then the sub-query may not be a compound query. |
| 121091 | +** This restriction is because transforming the |
| 121076 | 121092 | ** parent to a compound query confuses the code that handles |
| 121077 | 121093 | ** recursive queries in multiSelect(). |
| 121078 | 121094 | ** |
| 121079 | 121095 | ** (**) We no longer attempt to flatten aggregate subqueries. Was: |
| 121080 | 121096 | ** The subquery may not be an aggregate that uses the built-in min() or |
| | @@ -121151,13 +121167,10 @@ |
| 121151 | 121167 | return 0; /* Restriction (21) */ |
| 121152 | 121168 | } |
| 121153 | 121169 | if( pSub->selFlags & (SF_Recursive) ){ |
| 121154 | 121170 | return 0; /* Restrictions (22) */ |
| 121155 | 121171 | } |
| 121156 | | - if( (p->selFlags & SF_Recursive) && pSub->pPrior ){ |
| 121157 | | - return 0; /* Restriction (23) */ |
| 121158 | | - } |
| 121159 | 121172 | |
| 121160 | 121173 | /* |
| 121161 | 121174 | ** If the subquery is the right operand of a LEFT JOIN, then the |
| 121162 | 121175 | ** subquery may not be a join itself (3a). Example of why this is not |
| 121163 | 121176 | ** allowed: |
| | @@ -121198,13 +121211,10 @@ |
| 121198 | 121211 | ** use only the UNION ALL operator. And none of the simple select queries |
| 121199 | 121212 | ** that make up the compound SELECT are allowed to be aggregate or distinct |
| 121200 | 121213 | ** queries. |
| 121201 | 121214 | */ |
| 121202 | 121215 | if( pSub->pPrior ){ |
| 121203 | | - if( pSub->pOrderBy ){ |
| 121204 | | - return 0; /* Restriction (20) */ |
| 121205 | | - } |
| 121206 | 121216 | if( isAgg || (p->selFlags & SF_Distinct)!=0 || pSrc->nSrc!=1 ){ |
| 121207 | 121217 | return 0; /* (17d1), (17d2), or (17d3) */ |
| 121208 | 121218 | } |
| 121209 | 121219 | for(pSub1=pSub; pSub1; pSub1=pSub1->pPrior){ |
| 121210 | 121220 | testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct ); |
| | @@ -121227,10 +121237,27 @@ |
| 121227 | 121237 | if( p->pOrderBy->a[ii].u.x.iOrderByCol==0 ) return 0; |
| 121228 | 121238 | } |
| 121229 | 121239 | } |
| 121230 | 121240 | } |
| 121231 | 121241 | |
| 121242 | + /* Ex-restriction (23): |
| 121243 | + ** The only way that the recursive part of a CTE can contain a compound |
| 121244 | + ** subquery is for the subquery to be one term of a join. But if the |
| 121245 | + ** subquery is a join, then the flattening has already been stopped by |
| 121246 | + ** restriction (17d3) |
| 121247 | + */ |
| 121248 | + assert( (p->selFlags & SF_Recursive)==0 || pSub->pPrior==0 ); |
| 121249 | + |
| 121250 | + /* Ex-restriction (20): |
| 121251 | + ** A compound subquery must be the only term in the FROM clause of the |
| 121252 | + ** outer query by restriction (17d3). But if that term also has an |
| 121253 | + ** ORDER BY clause, then the subquery will be implemented by co-routine |
| 121254 | + ** and so the flattener will never be invoked. Hence, it is not possible |
| 121255 | + ** for the subquery to be a compound and have an ORDER BY clause. |
| 121256 | + */ |
| 121257 | + assert( pSub->pPrior==0 || pSub->pOrderBy==0 ); |
| 121258 | + |
| 121232 | 121259 | /***** If we reach this point, flattening is permitted. *****/ |
| 121233 | 121260 | SELECTTRACE(1,pParse,p,("flatten %s.%p from term %d\n", |
| 121234 | 121261 | pSub->zSelName, pSub, iFrom)); |
| 121235 | 121262 | |
| 121236 | 121263 | /* Authorize the subquery */ |
| | @@ -121958,11 +121985,11 @@ |
| 121958 | 121985 | ** sqlite3SelectExpand() when walking a SELECT tree to resolve table |
| 121959 | 121986 | ** names and other FROM clause elements. |
| 121960 | 121987 | */ |
| 121961 | 121988 | static void selectPopWith(Walker *pWalker, Select *p){ |
| 121962 | 121989 | Parse *pParse = pWalker->pParse; |
| 121963 | | - if( pParse->pWith && p->pPrior==0 ){ |
| 121990 | + if( OK_IF_ALWAYS_TRUE(pParse->pWith) && p->pPrior==0 ){ |
| 121964 | 121991 | With *pWith = findRightmost(p)->pWith; |
| 121965 | 121992 | if( pWith!=0 ){ |
| 121966 | 121993 | assert( pParse->pWith==pWith ); |
| 121967 | 121994 | pParse->pWith = pWith->pOuter; |
| 121968 | 121995 | } |
| | @@ -122013,11 +122040,11 @@ |
| 122013 | 122040 | if( NEVER(p->pSrc==0) || (selFlags & SF_Expanded)!=0 ){ |
| 122014 | 122041 | return WRC_Prune; |
| 122015 | 122042 | } |
| 122016 | 122043 | pTabList = p->pSrc; |
| 122017 | 122044 | pEList = p->pEList; |
| 122018 | | - if( p->pWith ){ |
| 122045 | + if( OK_IF_ALWAYS_TRUE(p->pWith) ){ |
| 122019 | 122046 | sqlite3WithPush(pParse, p->pWith, 0); |
| 122020 | 122047 | } |
| 122021 | 122048 | |
| 122022 | 122049 | /* Make sure cursor numbers have been assigned to all entries in |
| 122023 | 122050 | ** the FROM clause of the SELECT statement. |
| | @@ -122259,16 +122286,14 @@ |
| 122259 | 122286 | } |
| 122260 | 122287 | } |
| 122261 | 122288 | sqlite3ExprListDelete(db, pEList); |
| 122262 | 122289 | p->pEList = pNew; |
| 122263 | 122290 | } |
| 122264 | | -#if SQLITE_MAX_COLUMN |
| 122265 | 122291 | if( p->pEList && p->pEList->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){ |
| 122266 | 122292 | sqlite3ErrorMsg(pParse, "too many columns in result set"); |
| 122267 | 122293 | return WRC_Abort; |
| 122268 | 122294 | } |
| 122269 | | -#endif |
| 122270 | 122295 | return WRC_Continue; |
| 122271 | 122296 | } |
| 122272 | 122297 | |
| 122273 | 122298 | /* |
| 122274 | 122299 | ** No-op routine for the parse-tree walker. |
| | @@ -122318,11 +122343,11 @@ |
| 122318 | 122343 | */ |
| 122319 | 122344 | static void sqlite3SelectExpand(Parse *pParse, Select *pSelect){ |
| 122320 | 122345 | Walker w; |
| 122321 | 122346 | w.xExprCallback = sqlite3ExprWalkNoop; |
| 122322 | 122347 | w.pParse = pParse; |
| 122323 | | - if( pParse->hasCompound ){ |
| 122348 | + if( OK_IF_ALWAYS_TRUE(pParse->hasCompound) ){ |
| 122324 | 122349 | w.xSelectCallback = convertCompoundSelectToSubquery; |
| 122325 | 122350 | w.xSelectCallback2 = 0; |
| 122326 | 122351 | sqlite3WalkSelect(&w, pSelect); |
| 122327 | 122352 | } |
| 122328 | 122353 | w.xSelectCallback = selectExpander; |
| | @@ -122406,19 +122431,17 @@ |
| 122406 | 122431 | SQLITE_PRIVATE void sqlite3SelectPrep( |
| 122407 | 122432 | Parse *pParse, /* The parser context */ |
| 122408 | 122433 | Select *p, /* The SELECT statement being coded. */ |
| 122409 | 122434 | NameContext *pOuterNC /* Name context for container */ |
| 122410 | 122435 | ){ |
| 122411 | | - sqlite3 *db; |
| 122412 | | - if( NEVER(p==0) ) return; |
| 122413 | | - db = pParse->db; |
| 122414 | | - if( db->mallocFailed ) return; |
| 122436 | + assert( p!=0 || pParse->db->mallocFailed ); |
| 122437 | + if( pParse->db->mallocFailed ) return; |
| 122415 | 122438 | if( p->selFlags & SF_HasTypeInfo ) return; |
| 122416 | 122439 | sqlite3SelectExpand(pParse, p); |
| 122417 | | - if( pParse->nErr || db->mallocFailed ) return; |
| 122440 | + if( pParse->nErr || pParse->db->mallocFailed ) return; |
| 122418 | 122441 | sqlite3ResolveSelectNames(pParse, p, pOuterNC); |
| 122419 | | - if( pParse->nErr || db->mallocFailed ) return; |
| 122442 | + if( pParse->nErr || pParse->db->mallocFailed ) return; |
| 122420 | 122443 | sqlite3SelectAddTypeInfo(pParse, p); |
| 122421 | 122444 | } |
| 122422 | 122445 | |
| 122423 | 122446 | /* |
| 122424 | 122447 | ** Reset the aggregate accumulator. |
| | @@ -126333,12 +126356,14 @@ |
| 126333 | 126356 | void *pAux, /* Context pointer for xCreate/xConnect */ |
| 126334 | 126357 | void (*xDestroy)(void *) /* Module destructor function */ |
| 126335 | 126358 | ){ |
| 126336 | 126359 | Module *pMod; |
| 126337 | 126360 | int nName = sqlite3Strlen30(zName); |
| 126338 | | - pMod = (Module *)sqlite3DbMallocRawNN(db, sizeof(Module) + nName + 1); |
| 126339 | | - if( pMod ){ |
| 126361 | + pMod = (Module *)sqlite3Malloc(sizeof(Module) + nName + 1); |
| 126362 | + if( pMod==0 ){ |
| 126363 | + sqlite3OomFault(db); |
| 126364 | + }else{ |
| 126340 | 126365 | Module *pDel; |
| 126341 | 126366 | char *zCopy = (char *)(&pMod[1]); |
| 126342 | 126367 | memcpy(zCopy, zName, nName+1); |
| 126343 | 126368 | pMod->zName = zCopy; |
| 126344 | 126369 | pMod->pModule = pModule; |
| | @@ -126809,17 +126834,18 @@ |
| 126809 | 126834 | ); |
| 126810 | 126835 | return SQLITE_LOCKED; |
| 126811 | 126836 | } |
| 126812 | 126837 | } |
| 126813 | 126838 | |
| 126814 | | - zModuleName = sqlite3MPrintf(db, "%s", pTab->zName); |
| 126839 | + zModuleName = sqlite3DbStrDup(db, pTab->zName); |
| 126815 | 126840 | if( !zModuleName ){ |
| 126816 | 126841 | return SQLITE_NOMEM_BKPT; |
| 126817 | 126842 | } |
| 126818 | 126843 | |
| 126819 | | - pVTable = sqlite3DbMallocZero(db, sizeof(VTable)); |
| 126844 | + pVTable = sqlite3MallocZero(sizeof(VTable)); |
| 126820 | 126845 | if( !pVTable ){ |
| 126846 | + sqlite3OomFault(db); |
| 126821 | 126847 | sqlite3DbFree(db, zModuleName); |
| 126822 | 126848 | return SQLITE_NOMEM_BKPT; |
| 126823 | 126849 | } |
| 126824 | 126850 | pVTable->db = db; |
| 126825 | 126851 | pVTable->pMod = pMod; |
| | @@ -133669,22 +133695,23 @@ |
| 133669 | 133695 | |
| 133670 | 133696 | /* |
| 133671 | 133697 | ** Return TRUE if all of the following are true: |
| 133672 | 133698 | ** |
| 133673 | 133699 | ** (1) X has the same or lower cost that Y |
| 133674 | | -** (2) X is a proper subset of Y |
| 133675 | | -** (3) X skips at least as many columns as Y |
| 133700 | +** (2) X uses fewer WHERE clause terms than Y |
| 133701 | +** (3) Every WHERE clause term used by X is also used by Y |
| 133702 | +** (4) X skips at least as many columns as Y |
| 133703 | +** (5) If X is a covering index, than Y is too |
| 133676 | 133704 | ** |
| 133677 | | -** By "proper subset" we mean that X uses fewer WHERE clause terms |
| 133678 | | -** than Y and that every WHERE clause term used by X is also used |
| 133679 | | -** by Y. |
| 133680 | | -** |
| 133705 | +** Conditions (2) and (3) mean that X is a "proper subset" of Y. |
| 133681 | 133706 | ** If X is a proper subset of Y then Y is a better choice and ought |
| 133682 | 133707 | ** to have a lower cost. This routine returns TRUE when that cost |
| 133683 | | -** relationship is inverted and needs to be adjusted. The third rule |
| 133708 | +** relationship is inverted and needs to be adjusted. Constraint (4) |
| 133684 | 133709 | ** was added because if X uses skip-scan less than Y it still might |
| 133685 | | -** deserve a lower cost even if it is a proper subset of Y. |
| 133710 | +** deserve a lower cost even if it is a proper subset of Y. Constraint (5) |
| 133711 | +** was added because a covering index probably deserves to have a lower cost |
| 133712 | +** than a non-covering index even if it is a proper subset. |
| 133686 | 133713 | */ |
| 133687 | 133714 | static int whereLoopCheaperProperSubset( |
| 133688 | 133715 | const WhereLoop *pX, /* First WhereLoop to compare */ |
| 133689 | 133716 | const WhereLoop *pY /* Compare against this WhereLoop */ |
| 133690 | 133717 | ){ |
| | @@ -133701,10 +133728,14 @@ |
| 133701 | 133728 | if( pX->aLTerm[i]==0 ) continue; |
| 133702 | 133729 | for(j=pY->nLTerm-1; j>=0; j--){ |
| 133703 | 133730 | if( pY->aLTerm[j]==pX->aLTerm[i] ) break; |
| 133704 | 133731 | } |
| 133705 | 133732 | if( j<0 ) return 0; /* X not a subset of Y since term X[i] not used by Y */ |
| 133733 | + } |
| 133734 | + if( (pX->wsFlags&WHERE_IDX_ONLY)!=0 |
| 133735 | + && (pY->wsFlags&WHERE_IDX_ONLY)==0 ){ |
| 133736 | + return 0; /* Constraint (5) */ |
| 133706 | 133737 | } |
| 133707 | 133738 | return 1; /* All conditions meet */ |
| 133708 | 133739 | } |
| 133709 | 133740 | |
| 133710 | 133741 | /* |
| | @@ -141996,13 +142027,15 @@ |
| 141996 | 142027 | /* IMPLEMENTATION-OF: R-53536-42575 The sqlite3_libversion() function returns |
| 141997 | 142028 | ** a pointer to the to the sqlite3_version[] string constant. |
| 141998 | 142029 | */ |
| 141999 | 142030 | SQLITE_API const char *sqlite3_libversion(void){ return sqlite3_version; } |
| 142000 | 142031 | |
| 142001 | | -/* IMPLEMENTATION-OF: R-63124-39300 The sqlite3_sourceid() function returns a |
| 142032 | +/* IMPLEMENTATION-OF: R-25063-23286 The sqlite3_sourceid() function returns a |
| 142002 | 142033 | ** pointer to a string constant whose value is the same as the |
| 142003 | | -** SQLITE_SOURCE_ID C preprocessor macro. |
| 142034 | +** SQLITE_SOURCE_ID C preprocessor macro. Except if SQLite is built using |
| 142035 | +** an edited copy of the amalgamation, then the last four characters of |
| 142036 | +** the hash might be different from SQLITE_SOURCE_ID. |
| 142004 | 142037 | */ |
| 142005 | 142038 | /* SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; } */ |
| 142006 | 142039 | |
| 142007 | 142040 | /* IMPLEMENTATION-OF: R-35210-63508 The sqlite3_libversion_number() function |
| 142008 | 142041 | ** returns an integer equal to SQLITE_VERSION_NUMBER. |
| | @@ -145000,10 +145033,16 @@ |
| 145000 | 145033 | #ifdef SQLITE_ENABLE_RTREE |
| 145001 | 145034 | if( !db->mallocFailed && rc==SQLITE_OK){ |
| 145002 | 145035 | rc = sqlite3RtreeInit(db); |
| 145003 | 145036 | } |
| 145004 | 145037 | #endif |
| 145038 | + |
| 145039 | +#ifdef SQLITE_ENABLE_DBPAGE_VTAB |
| 145040 | + if( !db->mallocFailed && rc==SQLITE_OK){ |
| 145041 | + rc = sqlite3DbpageRegister(db); |
| 145042 | + } |
| 145043 | +#endif |
| 145005 | 145044 | |
| 145006 | 145045 | #ifdef SQLITE_ENABLE_DBSTAT_VTAB |
| 145007 | 145046 | if( !db->mallocFailed && rc==SQLITE_OK){ |
| 145008 | 145047 | rc = sqlite3DbstatRegister(db); |
| 145009 | 145048 | } |
| | @@ -145660,11 +145699,11 @@ |
| 145660 | 145699 | ** sqlite3_test_control(SQLITE_TESTCTRL_ALWAYS, int X) |
| 145661 | 145700 | ** |
| 145662 | 145701 | ** This action provides a run-time test to see how the ALWAYS and |
| 145663 | 145702 | ** NEVER macros were defined at compile-time. |
| 145664 | 145703 | ** |
| 145665 | | - ** The return value is ALWAYS(X). |
| 145704 | + ** The return value is ALWAYS(X) if X is true, or 0 if X is false. |
| 145666 | 145705 | ** |
| 145667 | 145706 | ** The recommended test is X==2. If the return value is 2, that means |
| 145668 | 145707 | ** ALWAYS() and NEVER() are both no-op pass-through macros, which is the |
| 145669 | 145708 | ** default setting. If the return value is 1, then ALWAYS() is either |
| 145670 | 145709 | ** hard-coded to true or else it asserts if its argument is false. |
| | @@ -145683,11 +145722,11 @@ |
| 145683 | 145722 | ** // ALWAYS(x) is a constant 1. NEVER(x) is a constant 0. |
| 145684 | 145723 | ** } |
| 145685 | 145724 | */ |
| 145686 | 145725 | case SQLITE_TESTCTRL_ALWAYS: { |
| 145687 | 145726 | int x = va_arg(ap,int); |
| 145688 | | - rc = ALWAYS(x); |
| 145727 | + rc = x ? ALWAYS(x) : 0; |
| 145689 | 145728 | break; |
| 145690 | 145729 | } |
| 145691 | 145730 | |
| 145692 | 145731 | /* |
| 145693 | 145732 | ** sqlite3_test_control(SQLITE_TESTCTRL_BYTEORDER); |
| | @@ -170889,10 +170928,17 @@ |
| 170889 | 170928 | #endif |
| 170890 | 170929 | |
| 170891 | 170930 | /* Maximum number of prepared UPDATE statements held by this module */ |
| 170892 | 170931 | #define SQLITE_RBU_UPDATE_CACHESIZE 16 |
| 170893 | 170932 | |
| 170933 | +/* Delta checksums disabled by default. Compile with -DRBU_ENABLE_DELTA_CKSUM |
| 170934 | +** to enable checksum verification. |
| 170935 | +*/ |
| 170936 | +#ifndef RBU_ENABLE_DELTA_CKSUM |
| 170937 | +# define RBU_ENABLE_DELTA_CKSUM 0 |
| 170938 | +#endif |
| 170939 | + |
| 170894 | 170940 | /* |
| 170895 | 170941 | ** Swap two objects of type TYPE. |
| 170896 | 170942 | */ |
| 170897 | 170943 | #if !defined(SQLITE_AMALGAMATION) |
| 170898 | 170944 | # define SWAP(TYPE,A,B) {TYPE t=A; A=B; B=t;} |
| | @@ -171263,10 +171309,11 @@ |
| 171263 | 171309 | *pLen -= z - zStart; |
| 171264 | 171310 | *pz = (char*)z; |
| 171265 | 171311 | return v; |
| 171266 | 171312 | } |
| 171267 | 171313 | |
| 171314 | +#if RBU_ENABLE_DELTA_CKSUM |
| 171268 | 171315 | /* |
| 171269 | 171316 | ** Compute a 32-bit checksum on the N-byte buffer. Return the result. |
| 171270 | 171317 | */ |
| 171271 | 171318 | static unsigned int rbuDeltaChecksum(const char *zIn, size_t N){ |
| 171272 | 171319 | const unsigned char *z = (const unsigned char *)zIn; |
| | @@ -171297,10 +171344,11 @@ |
| 171297 | 171344 | case 1: sum3 += (z[0] << 24); |
| 171298 | 171345 | default: ; |
| 171299 | 171346 | } |
| 171300 | 171347 | return sum3; |
| 171301 | 171348 | } |
| 171349 | +#endif |
| 171302 | 171350 | |
| 171303 | 171351 | /* |
| 171304 | 171352 | ** Apply a delta. |
| 171305 | 171353 | ** |
| 171306 | 171354 | ** The output buffer should be big enough to hold the whole output |
| | @@ -171327,11 +171375,11 @@ |
| 171327 | 171375 | int lenDelta, /* Length of the delta */ |
| 171328 | 171376 | char *zOut /* Write the output into this preallocated buffer */ |
| 171329 | 171377 | ){ |
| 171330 | 171378 | unsigned int limit; |
| 171331 | 171379 | unsigned int total = 0; |
| 171332 | | -#ifndef FOSSIL_OMIT_DELTA_CKSUM_TEST |
| 171380 | +#if RBU_ENABLE_DELTA_CKSUM |
| 171333 | 171381 | char *zOrigOut = zOut; |
| 171334 | 171382 | #endif |
| 171335 | 171383 | |
| 171336 | 171384 | limit = rbuDeltaGetInt(&zDelta, &lenDelta); |
| 171337 | 171385 | if( *zDelta!='\n' ){ |
| | @@ -171382,11 +171430,11 @@ |
| 171382 | 171430 | break; |
| 171383 | 171431 | } |
| 171384 | 171432 | case ';': { |
| 171385 | 171433 | zDelta++; lenDelta--; |
| 171386 | 171434 | zOut[0] = 0; |
| 171387 | | -#ifndef FOSSIL_OMIT_DELTA_CKSUM_TEST |
| 171435 | +#if RBU_ENABLE_DELTA_CKSUM |
| 171388 | 171436 | if( cnt!=rbuDeltaChecksum(zOrigOut, total) ){ |
| 171389 | 171437 | /* ERROR: bad checksum */ |
| 171390 | 171438 | return -1; |
| 171391 | 171439 | } |
| 171392 | 171440 | #endif |
| | @@ -176366,10 +176414,342 @@ |
| 176366 | 176414 | #elif defined(SQLITE_ENABLE_DBSTAT_VTAB) |
| 176367 | 176415 | SQLITE_PRIVATE int sqlite3DbstatRegister(sqlite3 *db){ return SQLITE_OK; } |
| 176368 | 176416 | #endif /* SQLITE_ENABLE_DBSTAT_VTAB */ |
| 176369 | 176417 | |
| 176370 | 176418 | /************** End of dbstat.c **********************************************/ |
| 176419 | +/************** Begin file dbpage.c ******************************************/ |
| 176420 | +/* |
| 176421 | +** 2017-10-11 |
| 176422 | +** |
| 176423 | +** The author disclaims copyright to this source code. In place of |
| 176424 | +** a legal notice, here is a blessing: |
| 176425 | +** |
| 176426 | +** May you do good and not evil. |
| 176427 | +** May you find forgiveness for yourself and forgive others. |
| 176428 | +** May you share freely, never taking more than you give. |
| 176429 | +** |
| 176430 | +****************************************************************************** |
| 176431 | +** |
| 176432 | +** This file contains an implementation of the "sqlite_dbpage" virtual table. |
| 176433 | +** |
| 176434 | +** The sqlite_dbpage virtual table is used to read or write whole raw |
| 176435 | +** pages of the database file. The pager interface is used so that |
| 176436 | +** uncommitted changes and changes recorded in the WAL file are correctly |
| 176437 | +** retrieved. |
| 176438 | +** |
| 176439 | +** Usage example: |
| 176440 | +** |
| 176441 | +** SELECT data FROM sqlite_dbpage('aux1') WHERE pgno=123; |
| 176442 | +** |
| 176443 | +** This is an eponymous virtual table so it does not need to be created before |
| 176444 | +** use. The optional argument to the sqlite_dbpage() table name is the |
| 176445 | +** schema for the database file that is to be read. The default schema is |
| 176446 | +** "main". |
| 176447 | +** |
| 176448 | +** The data field of sqlite_dbpage table can be updated. The new |
| 176449 | +** value must be a BLOB which is the correct page size, otherwise the |
| 176450 | +** update fails. Rows may not be deleted or inserted. |
| 176451 | +*/ |
| 176452 | + |
| 176453 | +/* #include "sqliteInt.h" ** Requires access to internal data structures ** */ |
| 176454 | +#if (defined(SQLITE_ENABLE_DBPAGE_VTAB) || defined(SQLITE_TEST)) \ |
| 176455 | + && !defined(SQLITE_OMIT_VIRTUALTABLE) |
| 176456 | + |
| 176457 | +typedef struct DbpageTable DbpageTable; |
| 176458 | +typedef struct DbpageCursor DbpageCursor; |
| 176459 | + |
| 176460 | +struct DbpageCursor { |
| 176461 | + sqlite3_vtab_cursor base; /* Base class. Must be first */ |
| 176462 | + int pgno; /* Current page number */ |
| 176463 | + int mxPgno; /* Last page to visit on this scan */ |
| 176464 | +}; |
| 176465 | + |
| 176466 | +struct DbpageTable { |
| 176467 | + sqlite3_vtab base; /* Base class. Must be first */ |
| 176468 | + sqlite3 *db; /* The database */ |
| 176469 | + Pager *pPager; /* Pager being read/written */ |
| 176470 | + int iDb; /* Index of database to analyze */ |
| 176471 | + int szPage; /* Size of each page in bytes */ |
| 176472 | + int nPage; /* Number of pages in the file */ |
| 176473 | +}; |
| 176474 | + |
| 176475 | +/* |
| 176476 | +** Connect to or create a dbpagevfs virtual table. |
| 176477 | +*/ |
| 176478 | +static int dbpageConnect( |
| 176479 | + sqlite3 *db, |
| 176480 | + void *pAux, |
| 176481 | + int argc, const char *const*argv, |
| 176482 | + sqlite3_vtab **ppVtab, |
| 176483 | + char **pzErr |
| 176484 | +){ |
| 176485 | + DbpageTable *pTab = 0; |
| 176486 | + int rc = SQLITE_OK; |
| 176487 | + int iDb; |
| 176488 | + |
| 176489 | + if( argc>=4 ){ |
| 176490 | + Token nm; |
| 176491 | + sqlite3TokenInit(&nm, (char*)argv[3]); |
| 176492 | + iDb = sqlite3FindDb(db, &nm); |
| 176493 | + if( iDb<0 ){ |
| 176494 | + *pzErr = sqlite3_mprintf("no such schema: %s", argv[3]); |
| 176495 | + return SQLITE_ERROR; |
| 176496 | + } |
| 176497 | + }else{ |
| 176498 | + iDb = 0; |
| 176499 | + } |
| 176500 | + rc = sqlite3_declare_vtab(db, |
| 176501 | + "CREATE TABLE x(pgno INTEGER PRIMARY KEY, data BLOB, schema HIDDEN)"); |
| 176502 | + if( rc==SQLITE_OK ){ |
| 176503 | + pTab = (DbpageTable *)sqlite3_malloc64(sizeof(DbpageTable)); |
| 176504 | + if( pTab==0 ) rc = SQLITE_NOMEM_BKPT; |
| 176505 | + } |
| 176506 | + |
| 176507 | + assert( rc==SQLITE_OK || pTab==0 ); |
| 176508 | + if( rc==SQLITE_OK ){ |
| 176509 | + Btree *pBt = db->aDb[iDb].pBt; |
| 176510 | + memset(pTab, 0, sizeof(DbpageTable)); |
| 176511 | + pTab->db = db; |
| 176512 | + pTab->iDb = iDb; |
| 176513 | + pTab->pPager = pBt ? sqlite3BtreePager(pBt) : 0; |
| 176514 | + } |
| 176515 | + |
| 176516 | + *ppVtab = (sqlite3_vtab*)pTab; |
| 176517 | + return rc; |
| 176518 | +} |
| 176519 | + |
| 176520 | +/* |
| 176521 | +** Disconnect from or destroy a dbpagevfs virtual table. |
| 176522 | +*/ |
| 176523 | +static int dbpageDisconnect(sqlite3_vtab *pVtab){ |
| 176524 | + sqlite3_free(pVtab); |
| 176525 | + return SQLITE_OK; |
| 176526 | +} |
| 176527 | + |
| 176528 | +/* |
| 176529 | +** idxNum: |
| 176530 | +** |
| 176531 | +** 0 full table scan |
| 176532 | +** 1 pgno=?1 |
| 176533 | +*/ |
| 176534 | +static int dbpageBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){ |
| 176535 | + int i; |
| 176536 | + pIdxInfo->estimatedCost = 1.0e6; /* Initial cost estimate */ |
| 176537 | + for(i=0; i<pIdxInfo->nConstraint; i++){ |
| 176538 | + struct sqlite3_index_constraint *p = &pIdxInfo->aConstraint[i]; |
| 176539 | + if( p->usable && p->iColumn<=0 && p->op==SQLITE_INDEX_CONSTRAINT_EQ ){ |
| 176540 | + pIdxInfo->estimatedRows = 1; |
| 176541 | + pIdxInfo->idxFlags = SQLITE_INDEX_SCAN_UNIQUE; |
| 176542 | + pIdxInfo->estimatedCost = 1.0; |
| 176543 | + pIdxInfo->idxNum = 1; |
| 176544 | + pIdxInfo->aConstraintUsage[i].argvIndex = 1; |
| 176545 | + pIdxInfo->aConstraintUsage[i].omit = 1; |
| 176546 | + break; |
| 176547 | + } |
| 176548 | + } |
| 176549 | + if( pIdxInfo->nOrderBy>=1 |
| 176550 | + && pIdxInfo->aOrderBy[0].iColumn<=0 |
| 176551 | + && pIdxInfo->aOrderBy[0].desc==0 |
| 176552 | + ){ |
| 176553 | + pIdxInfo->orderByConsumed = 1; |
| 176554 | + } |
| 176555 | + return SQLITE_OK; |
| 176556 | +} |
| 176557 | + |
| 176558 | +/* |
| 176559 | +** Open a new dbpagevfs cursor. |
| 176560 | +*/ |
| 176561 | +static int dbpageOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){ |
| 176562 | + DbpageCursor *pCsr; |
| 176563 | + |
| 176564 | + pCsr = (DbpageCursor *)sqlite3_malloc64(sizeof(DbpageCursor)); |
| 176565 | + if( pCsr==0 ){ |
| 176566 | + return SQLITE_NOMEM_BKPT; |
| 176567 | + }else{ |
| 176568 | + memset(pCsr, 0, sizeof(DbpageCursor)); |
| 176569 | + pCsr->base.pVtab = pVTab; |
| 176570 | + pCsr->pgno = -1; |
| 176571 | + } |
| 176572 | + |
| 176573 | + *ppCursor = (sqlite3_vtab_cursor *)pCsr; |
| 176574 | + return SQLITE_OK; |
| 176575 | +} |
| 176576 | + |
| 176577 | +/* |
| 176578 | +** Close a dbpagevfs cursor. |
| 176579 | +*/ |
| 176580 | +static int dbpageClose(sqlite3_vtab_cursor *pCursor){ |
| 176581 | + DbpageCursor *pCsr = (DbpageCursor *)pCursor; |
| 176582 | + sqlite3_free(pCsr); |
| 176583 | + return SQLITE_OK; |
| 176584 | +} |
| 176585 | + |
| 176586 | +/* |
| 176587 | +** Move a dbpagevfs cursor to the next entry in the file. |
| 176588 | +*/ |
| 176589 | +static int dbpageNext(sqlite3_vtab_cursor *pCursor){ |
| 176590 | + int rc = SQLITE_OK; |
| 176591 | + DbpageCursor *pCsr = (DbpageCursor *)pCursor; |
| 176592 | + pCsr->pgno++; |
| 176593 | + return rc; |
| 176594 | +} |
| 176595 | + |
| 176596 | +static int dbpageEof(sqlite3_vtab_cursor *pCursor){ |
| 176597 | + DbpageCursor *pCsr = (DbpageCursor *)pCursor; |
| 176598 | + return pCsr->pgno > pCsr->mxPgno; |
| 176599 | +} |
| 176600 | + |
| 176601 | +static int dbpageFilter( |
| 176602 | + sqlite3_vtab_cursor *pCursor, |
| 176603 | + int idxNum, const char *idxStr, |
| 176604 | + int argc, sqlite3_value **argv |
| 176605 | +){ |
| 176606 | + DbpageCursor *pCsr = (DbpageCursor *)pCursor; |
| 176607 | + DbpageTable *pTab = (DbpageTable *)pCursor->pVtab; |
| 176608 | + int rc = SQLITE_OK; |
| 176609 | + Btree *pBt = pTab->db->aDb[pTab->iDb].pBt; |
| 176610 | + |
| 176611 | + pTab->szPage = sqlite3BtreeGetPageSize(pBt); |
| 176612 | + pTab->nPage = sqlite3BtreeLastPage(pBt); |
| 176613 | + if( idxNum==1 ){ |
| 176614 | + pCsr->pgno = sqlite3_value_int(argv[0]); |
| 176615 | + if( pCsr->pgno<1 || pCsr->pgno>pTab->nPage ){ |
| 176616 | + pCsr->pgno = 1; |
| 176617 | + pCsr->mxPgno = 0; |
| 176618 | + }else{ |
| 176619 | + pCsr->mxPgno = pCsr->pgno; |
| 176620 | + } |
| 176621 | + }else{ |
| 176622 | + pCsr->pgno = 1; |
| 176623 | + pCsr->mxPgno = pTab->nPage; |
| 176624 | + } |
| 176625 | + return rc; |
| 176626 | +} |
| 176627 | + |
| 176628 | +static int dbpageColumn( |
| 176629 | + sqlite3_vtab_cursor *pCursor, |
| 176630 | + sqlite3_context *ctx, |
| 176631 | + int i |
| 176632 | +){ |
| 176633 | + DbpageCursor *pCsr = (DbpageCursor *)pCursor; |
| 176634 | + DbpageTable *pTab = (DbpageTable *)pCursor->pVtab; |
| 176635 | + int rc = SQLITE_OK; |
| 176636 | + switch( i ){ |
| 176637 | + case 0: { /* pgno */ |
| 176638 | + sqlite3_result_int(ctx, pCsr->pgno); |
| 176639 | + break; |
| 176640 | + } |
| 176641 | + case 1: { /* data */ |
| 176642 | + DbPage *pDbPage = 0; |
| 176643 | + rc = sqlite3PagerGet(pTab->pPager, pCsr->pgno, (DbPage**)&pDbPage, 0); |
| 176644 | + if( rc==SQLITE_OK ){ |
| 176645 | + sqlite3_result_blob(ctx, sqlite3PagerGetData(pDbPage), pTab->szPage, |
| 176646 | + SQLITE_TRANSIENT); |
| 176647 | + } |
| 176648 | + sqlite3PagerUnref(pDbPage); |
| 176649 | + break; |
| 176650 | + } |
| 176651 | + default: { /* schema */ |
| 176652 | + sqlite3 *db = sqlite3_context_db_handle(ctx); |
| 176653 | + sqlite3_result_text(ctx, db->aDb[pTab->iDb].zDbSName, -1, SQLITE_STATIC); |
| 176654 | + break; |
| 176655 | + } |
| 176656 | + } |
| 176657 | + return SQLITE_OK; |
| 176658 | +} |
| 176659 | + |
| 176660 | +static int dbpageRowid(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){ |
| 176661 | + DbpageCursor *pCsr = (DbpageCursor *)pCursor; |
| 176662 | + *pRowid = pCsr->pgno; |
| 176663 | + return SQLITE_OK; |
| 176664 | +} |
| 176665 | + |
| 176666 | +static int dbpageUpdate( |
| 176667 | + sqlite3_vtab *pVtab, |
| 176668 | + int argc, |
| 176669 | + sqlite3_value **argv, |
| 176670 | + sqlite_int64 *pRowid |
| 176671 | +){ |
| 176672 | + DbpageTable *pTab = (DbpageTable *)pVtab; |
| 176673 | + int pgno; |
| 176674 | + DbPage *pDbPage = 0; |
| 176675 | + int rc = SQLITE_OK; |
| 176676 | + char *zErr = 0; |
| 176677 | + |
| 176678 | + if( argc==1 ){ |
| 176679 | + zErr = "cannot delete"; |
| 176680 | + goto update_fail; |
| 176681 | + } |
| 176682 | + pgno = sqlite3_value_int(argv[0]); |
| 176683 | + if( pgno<1 || pgno>pTab->nPage ){ |
| 176684 | + zErr = "bad page number"; |
| 176685 | + goto update_fail; |
| 176686 | + } |
| 176687 | + if( sqlite3_value_int(argv[1])!=pgno ){ |
| 176688 | + zErr = "cannot insert"; |
| 176689 | + goto update_fail; |
| 176690 | + } |
| 176691 | + if( sqlite3_value_type(argv[3])!=SQLITE_BLOB |
| 176692 | + || sqlite3_value_bytes(argv[3])!=pTab->szPage |
| 176693 | + ){ |
| 176694 | + zErr = "bad page value"; |
| 176695 | + goto update_fail; |
| 176696 | + } |
| 176697 | + rc = sqlite3PagerGet(pTab->pPager, pgno, (DbPage**)&pDbPage, 0); |
| 176698 | + if( rc==SQLITE_OK ){ |
| 176699 | + rc = sqlite3PagerWrite(pDbPage); |
| 176700 | + if( rc==SQLITE_OK ){ |
| 176701 | + memcpy(sqlite3PagerGetData(pDbPage), |
| 176702 | + sqlite3_value_blob(argv[3]), |
| 176703 | + pTab->szPage); |
| 176704 | + } |
| 176705 | + } |
| 176706 | + sqlite3PagerUnref(pDbPage); |
| 176707 | + return rc; |
| 176708 | + |
| 176709 | +update_fail: |
| 176710 | + sqlite3_free(pVtab->zErrMsg); |
| 176711 | + pVtab->zErrMsg = sqlite3_mprintf("%s", zErr); |
| 176712 | + return SQLITE_ERROR; |
| 176713 | +} |
| 176714 | + |
| 176715 | +/* |
| 176716 | +** Invoke this routine to register the "dbpage" virtual table module |
| 176717 | +*/ |
| 176718 | +SQLITE_PRIVATE int sqlite3DbpageRegister(sqlite3 *db){ |
| 176719 | + static sqlite3_module dbpage_module = { |
| 176720 | + 0, /* iVersion */ |
| 176721 | + dbpageConnect, /* xCreate */ |
| 176722 | + dbpageConnect, /* xConnect */ |
| 176723 | + dbpageBestIndex, /* xBestIndex */ |
| 176724 | + dbpageDisconnect, /* xDisconnect */ |
| 176725 | + dbpageDisconnect, /* xDestroy */ |
| 176726 | + dbpageOpen, /* xOpen - open a cursor */ |
| 176727 | + dbpageClose, /* xClose - close a cursor */ |
| 176728 | + dbpageFilter, /* xFilter - configure scan constraints */ |
| 176729 | + dbpageNext, /* xNext - advance a cursor */ |
| 176730 | + dbpageEof, /* xEof - check for end of scan */ |
| 176731 | + dbpageColumn, /* xColumn - read data */ |
| 176732 | + dbpageRowid, /* xRowid - read data */ |
| 176733 | + dbpageUpdate, /* xUpdate */ |
| 176734 | + 0, /* xBegin */ |
| 176735 | + 0, /* xSync */ |
| 176736 | + 0, /* xCommit */ |
| 176737 | + 0, /* xRollback */ |
| 176738 | + 0, /* xFindMethod */ |
| 176739 | + 0, /* xRename */ |
| 176740 | + 0, /* xSavepoint */ |
| 176741 | + 0, /* xRelease */ |
| 176742 | + 0, /* xRollbackTo */ |
| 176743 | + }; |
| 176744 | + return sqlite3_create_module(db, "sqlite_dbpage", &dbpage_module, 0); |
| 176745 | +} |
| 176746 | +#elif defined(SQLITE_ENABLE_DBPAGE_VTAB) |
| 176747 | +SQLITE_PRIVATE int sqlite3DbpageRegister(sqlite3 *db){ return SQLITE_OK; } |
| 176748 | +#endif /* SQLITE_ENABLE_DBSTAT_VTAB */ |
| 176749 | + |
| 176750 | +/************** End of dbpage.c **********************************************/ |
| 176371 | 176751 | /************** Begin file sqlite3session.c **********************************/ |
| 176372 | 176752 | |
| 176373 | 176753 | #if defined(SQLITE_ENABLE_SESSION) && defined(SQLITE_ENABLE_PREUPDATE_HOOK) |
| 176374 | 176754 | /* #include "sqlite3session.h" */ |
| 176375 | 176755 | /* #include <assert.h> */ |
| | @@ -200697,11 +201077,11 @@ |
| 200697 | 201077 | int nArg, /* Number of args */ |
| 200698 | 201078 | sqlite3_value **apUnused /* Function arguments */ |
| 200699 | 201079 | ){ |
| 200700 | 201080 | assert( nArg==0 ); |
| 200701 | 201081 | UNUSED_PARAM2(nArg, apUnused); |
| 200702 | | - sqlite3_result_text(pCtx, "fts5: 2017-09-29 16:07:56 0840f9f824c16212ce3fd6c859e501176eb0a58924ea1728a54d5bdfd0c25c86", -1, SQLITE_TRANSIENT); |
| 201082 | + sqlite3_result_text(pCtx, "fts5: 2017-10-19 15:17:38 04925dee41a21ffca9a9f9df27d8165431668c42c2b33d08b077fdb28011170b", -1, SQLITE_TRANSIENT); |
| 200703 | 201083 | } |
| 200704 | 201084 | |
| 200705 | 201085 | static int fts5Init(sqlite3 *db){ |
| 200706 | 201086 | static const sqlite3_module fts5Mod = { |
| 200707 | 201087 | /* iVersion */ 2, |
| | @@ -204579,11 +204959,10 @@ |
| 204579 | 204959 | iVal = pCsr->aDoc[0]; |
| 204580 | 204960 | }else{ |
| 204581 | 204961 | iVal = pCsr->aCnt[0]; |
| 204582 | 204962 | } |
| 204583 | 204963 | }else{ |
| 204584 | | - int eDetail = pCsr->pConfig->eDetail; |
| 204585 | 204964 | assert( eType==FTS5_VOCAB_INSTANCE ); |
| 204586 | 204965 | switch( iCol ){ |
| 204587 | 204966 | case 1: |
| 204588 | 204967 | sqlite3_result_int64(pCtx, pCsr->pIter->iRowid); |
| 204589 | 204968 | break; |
| | @@ -204590,11 +204969,11 @@ |
| 204590 | 204969 | case 2: { |
| 204591 | 204970 | int ii = -1; |
| 204592 | 204971 | if( eDetail==FTS5_DETAIL_FULL ){ |
| 204593 | 204972 | ii = FTS5_POS2COLUMN(pCsr->iInstPos); |
| 204594 | 204973 | }else if( eDetail==FTS5_DETAIL_COLUMNS ){ |
| 204595 | | - ii = pCsr->iInstPos; |
| 204974 | + ii = (int)pCsr->iInstPos; |
| 204596 | 204975 | } |
| 204597 | 204976 | if( ii>=0 && ii<pCsr->pConfig->nCol ){ |
| 204598 | 204977 | const char *z = pCsr->pConfig->azCol[ii]; |
| 204599 | 204978 | sqlite3_result_text(pCtx, z, -1, SQLITE_STATIC); |
| 204600 | 204979 | } |
| | @@ -204966,12 +205345,12 @@ |
| 204966 | 205345 | } |
| 204967 | 205346 | #endif /* SQLITE_CORE */ |
| 204968 | 205347 | #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_STMTVTAB) */ |
| 204969 | 205348 | |
| 204970 | 205349 | /************** End of stmt.c ************************************************/ |
| 204971 | | -#if __LINE__!=204971 |
| 205350 | +#if __LINE__!=205350 |
| 204972 | 205351 | #undef SQLITE_SOURCE_ID |
| 204973 | | -#define SQLITE_SOURCE_ID "2017-10-02 02:52:54 c9104b59c7ed360291f7f6fc8caae938e9840c77620d598e4096f78183bfalt2" |
| 205352 | +#define SQLITE_SOURCE_ID "2017-10-21 17:17:17 fb3ee1b7cac09e4950e4f48b44c277e4f391cb6c8f069644732d2389ca65alt2" |
| 204974 | 205353 | #endif |
| 204975 | 205354 | /* Return the source-id for this library */ |
| 204976 | 205355 | SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; } |
| 204977 | 205356 | /************************** End of sqlite3.c ******************************/ |
| 204978 | 205357 | |