Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(936)

Side by Side Diff: third_party/sqlite/amalgamation/sqlite3.03.c

Issue 2755803002: NCI: trybot test for sqlite 3.17 import. (Closed)
Patch Set: also clang on Linux i386 Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /************** Begin file btree.c *******************************************/
2 /*
3 ** 2004 April 6
4 **
5 ** The author disclaims copyright to this source code. In place of
6 ** a legal notice, here is a blessing:
7 **
8 ** May you do good and not evil.
9 ** May you find forgiveness for yourself and forgive others.
10 ** May you share freely, never taking more than you give.
11 **
12 *************************************************************************
13 ** This file implements an external (disk-based) database using BTrees.
14 ** See the header comment on "btreeInt.h" for additional information.
15 ** Including a description of file format and an overview of operation.
16 */
17 /* #include "btreeInt.h" */
18
19 /*
20 ** The header string that appears at the beginning of every
21 ** SQLite database.
22 */
23 static const char zMagicHeader[] = SQLITE_FILE_HEADER;
24
25 /*
26 ** Set this global variable to 1 to enable tracing using the TRACE
27 ** macro.
28 */
29 #if 0
30 int sqlite3BtreeTrace=1; /* True to enable tracing */
31 # define TRACE(X) if(sqlite3BtreeTrace){printf X;fflush(stdout);}
32 #else
33 # define TRACE(X)
34 #endif
35
36 /*
37 ** Extract a 2-byte big-endian integer from an array of unsigned bytes.
38 ** But if the value is zero, make it 65536.
39 **
40 ** This routine is used to extract the "offset to cell content area" value
41 ** from the header of a btree page. If the page size is 65536 and the page
42 ** is empty, the offset should be 65536, but the 2-byte value stores zero.
43 ** This routine makes the necessary adjustment to 65536.
44 */
45 #define get2byteNotZero(X) (((((int)get2byte(X))-1)&0xffff)+1)
46
47 /*
48 ** Values passed as the 5th argument to allocateBtreePage()
49 */
50 #define BTALLOC_ANY 0 /* Allocate any page */
51 #define BTALLOC_EXACT 1 /* Allocate exact page if possible */
52 #define BTALLOC_LE 2 /* Allocate any page <= the parameter */
53
54 /*
55 ** Macro IfNotOmitAV(x) returns (x) if SQLITE_OMIT_AUTOVACUUM is not
56 ** defined, or 0 if it is. For example:
57 **
58 ** bIncrVacuum = IfNotOmitAV(pBtShared->incrVacuum);
59 */
60 #ifndef SQLITE_OMIT_AUTOVACUUM
61 #define IfNotOmitAV(expr) (expr)
62 #else
63 #define IfNotOmitAV(expr) 0
64 #endif
65
66 #ifndef SQLITE_OMIT_SHARED_CACHE
67 /*
68 ** A list of BtShared objects that are eligible for participation
69 ** in shared cache. This variable has file scope during normal builds,
70 ** but the test harness needs to access it so we make it global for
71 ** test builds.
72 **
73 ** Access to this variable is protected by SQLITE_MUTEX_STATIC_MASTER.
74 */
75 #ifdef SQLITE_TEST
76 SQLITE_PRIVATE BtShared *SQLITE_WSD sqlite3SharedCacheList = 0;
77 #else
78 static BtShared *SQLITE_WSD sqlite3SharedCacheList = 0;
79 #endif
80 #endif /* SQLITE_OMIT_SHARED_CACHE */
81
82 #ifndef SQLITE_OMIT_SHARED_CACHE
83 /*
84 ** Enable or disable the shared pager and schema features.
85 **
86 ** This routine has no effect on existing database connections.
87 ** The shared cache setting effects only future calls to
88 ** sqlite3_open(), sqlite3_open16(), or sqlite3_open_v2().
89 */
90 SQLITE_API int sqlite3_enable_shared_cache(int enable){
91 sqlite3GlobalConfig.sharedCacheEnabled = enable;
92 return SQLITE_OK;
93 }
94 #endif
95
96
97
98 #ifdef SQLITE_OMIT_SHARED_CACHE
99 /*
100 ** The functions querySharedCacheTableLock(), setSharedCacheTableLock(),
101 ** and clearAllSharedCacheTableLocks()
102 ** manipulate entries in the BtShared.pLock linked list used to store
103 ** shared-cache table level locks. If the library is compiled with the
104 ** shared-cache feature disabled, then there is only ever one user
105 ** of each BtShared structure and so this locking is not necessary.
106 ** So define the lock related functions as no-ops.
107 */
108 #define querySharedCacheTableLock(a,b,c) SQLITE_OK
109 #define setSharedCacheTableLock(a,b,c) SQLITE_OK
110 #define clearAllSharedCacheTableLocks(a)
111 #define downgradeAllSharedCacheTableLocks(a)
112 #define hasSharedCacheTableLock(a,b,c,d) 1
113 #define hasReadConflicts(a, b) 0
114 #endif
115
116 #ifndef SQLITE_OMIT_SHARED_CACHE
117
118 #ifdef SQLITE_DEBUG
119 /*
120 **** This function is only used as part of an assert() statement. ***
121 **
122 ** Check to see if pBtree holds the required locks to read or write to the
123 ** table with root page iRoot. Return 1 if it does and 0 if not.
124 **
125 ** For example, when writing to a table with root-page iRoot via
126 ** Btree connection pBtree:
127 **
128 ** assert( hasSharedCacheTableLock(pBtree, iRoot, 0, WRITE_LOCK) );
129 **
130 ** When writing to an index that resides in a sharable database, the
131 ** caller should have first obtained a lock specifying the root page of
132 ** the corresponding table. This makes things a bit more complicated,
133 ** as this module treats each table as a separate structure. To determine
134 ** the table corresponding to the index being written, this
135 ** function has to search through the database schema.
136 **
137 ** Instead of a lock on the table/index rooted at page iRoot, the caller may
138 ** hold a write-lock on the schema table (root page 1). This is also
139 ** acceptable.
140 */
141 static int hasSharedCacheTableLock(
142 Btree *pBtree, /* Handle that must hold lock */
143 Pgno iRoot, /* Root page of b-tree */
144 int isIndex, /* True if iRoot is the root of an index b-tree */
145 int eLockType /* Required lock type (READ_LOCK or WRITE_LOCK) */
146 ){
147 Schema *pSchema = (Schema *)pBtree->pBt->pSchema;
148 Pgno iTab = 0;
149 BtLock *pLock;
150
151 /* If this database is not shareable, or if the client is reading
152 ** and has the read-uncommitted flag set, then no lock is required.
153 ** Return true immediately.
154 */
155 if( (pBtree->sharable==0)
156 || (eLockType==READ_LOCK && (pBtree->db->flags & SQLITE_ReadUncommitted))
157 ){
158 return 1;
159 }
160
161 /* If the client is reading or writing an index and the schema is
162 ** not loaded, then it is too difficult to actually check to see if
163 ** the correct locks are held. So do not bother - just return true.
164 ** This case does not come up very often anyhow.
165 */
166 if( isIndex && (!pSchema || (pSchema->schemaFlags&DB_SchemaLoaded)==0) ){
167 return 1;
168 }
169
170 /* Figure out the root-page that the lock should be held on. For table
171 ** b-trees, this is just the root page of the b-tree being read or
172 ** written. For index b-trees, it is the root page of the associated
173 ** table. */
174 if( isIndex ){
175 HashElem *p;
176 for(p=sqliteHashFirst(&pSchema->idxHash); p; p=sqliteHashNext(p)){
177 Index *pIdx = (Index *)sqliteHashData(p);
178 if( pIdx->tnum==(int)iRoot ){
179 if( iTab ){
180 /* Two or more indexes share the same root page. There must
181 ** be imposter tables. So just return true. The assert is not
182 ** useful in that case. */
183 return 1;
184 }
185 iTab = pIdx->pTable->tnum;
186 }
187 }
188 }else{
189 iTab = iRoot;
190 }
191
192 /* Search for the required lock. Either a write-lock on root-page iTab, a
193 ** write-lock on the schema table, or (if the client is reading) a
194 ** read-lock on iTab will suffice. Return 1 if any of these are found. */
195 for(pLock=pBtree->pBt->pLock; pLock; pLock=pLock->pNext){
196 if( pLock->pBtree==pBtree
197 && (pLock->iTable==iTab || (pLock->eLock==WRITE_LOCK && pLock->iTable==1))
198 && pLock->eLock>=eLockType
199 ){
200 return 1;
201 }
202 }
203
204 /* Failed to find the required lock. */
205 return 0;
206 }
207 #endif /* SQLITE_DEBUG */
208
209 #ifdef SQLITE_DEBUG
210 /*
211 **** This function may be used as part of assert() statements only. ****
212 **
213 ** Return true if it would be illegal for pBtree to write into the
214 ** table or index rooted at iRoot because other shared connections are
215 ** simultaneously reading that same table or index.
216 **
217 ** It is illegal for pBtree to write if some other Btree object that
218 ** shares the same BtShared object is currently reading or writing
219 ** the iRoot table. Except, if the other Btree object has the
220 ** read-uncommitted flag set, then it is OK for the other object to
221 ** have a read cursor.
222 **
223 ** For example, before writing to any part of the table or index
224 ** rooted at page iRoot, one should call:
225 **
226 ** assert( !hasReadConflicts(pBtree, iRoot) );
227 */
228 static int hasReadConflicts(Btree *pBtree, Pgno iRoot){
229 BtCursor *p;
230 for(p=pBtree->pBt->pCursor; p; p=p->pNext){
231 if( p->pgnoRoot==iRoot
232 && p->pBtree!=pBtree
233 && 0==(p->pBtree->db->flags & SQLITE_ReadUncommitted)
234 ){
235 return 1;
236 }
237 }
238 return 0;
239 }
240 #endif /* #ifdef SQLITE_DEBUG */
241
242 /*
243 ** Query to see if Btree handle p may obtain a lock of type eLock
244 ** (READ_LOCK or WRITE_LOCK) on the table with root-page iTab. Return
245 ** SQLITE_OK if the lock may be obtained (by calling
246 ** setSharedCacheTableLock()), or SQLITE_LOCKED if not.
247 */
248 static int querySharedCacheTableLock(Btree *p, Pgno iTab, u8 eLock){
249 BtShared *pBt = p->pBt;
250 BtLock *pIter;
251
252 assert( sqlite3BtreeHoldsMutex(p) );
253 assert( eLock==READ_LOCK || eLock==WRITE_LOCK );
254 assert( p->db!=0 );
255 assert( !(p->db->flags&SQLITE_ReadUncommitted)||eLock==WRITE_LOCK||iTab==1 );
256
257 /* If requesting a write-lock, then the Btree must have an open write
258 ** transaction on this file. And, obviously, for this to be so there
259 ** must be an open write transaction on the file itself.
260 */
261 assert( eLock==READ_LOCK || (p==pBt->pWriter && p->inTrans==TRANS_WRITE) );
262 assert( eLock==READ_LOCK || pBt->inTransaction==TRANS_WRITE );
263
264 /* This routine is a no-op if the shared-cache is not enabled */
265 if( !p->sharable ){
266 return SQLITE_OK;
267 }
268
269 /* If some other connection is holding an exclusive lock, the
270 ** requested lock may not be obtained.
271 */
272 if( pBt->pWriter!=p && (pBt->btsFlags & BTS_EXCLUSIVE)!=0 ){
273 sqlite3ConnectionBlocked(p->db, pBt->pWriter->db);
274 return SQLITE_LOCKED_SHAREDCACHE;
275 }
276
277 for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
278 /* The condition (pIter->eLock!=eLock) in the following if(...)
279 ** statement is a simplification of:
280 **
281 ** (eLock==WRITE_LOCK || pIter->eLock==WRITE_LOCK)
282 **
283 ** since we know that if eLock==WRITE_LOCK, then no other connection
284 ** may hold a WRITE_LOCK on any table in this file (since there can
285 ** only be a single writer).
286 */
287 assert( pIter->eLock==READ_LOCK || pIter->eLock==WRITE_LOCK );
288 assert( eLock==READ_LOCK || pIter->pBtree==p || pIter->eLock==READ_LOCK);
289 if( pIter->pBtree!=p && pIter->iTable==iTab && pIter->eLock!=eLock ){
290 sqlite3ConnectionBlocked(p->db, pIter->pBtree->db);
291 if( eLock==WRITE_LOCK ){
292 assert( p==pBt->pWriter );
293 pBt->btsFlags |= BTS_PENDING;
294 }
295 return SQLITE_LOCKED_SHAREDCACHE;
296 }
297 }
298 return SQLITE_OK;
299 }
300 #endif /* !SQLITE_OMIT_SHARED_CACHE */
301
302 #ifndef SQLITE_OMIT_SHARED_CACHE
303 /*
304 ** Add a lock on the table with root-page iTable to the shared-btree used
305 ** by Btree handle p. Parameter eLock must be either READ_LOCK or
306 ** WRITE_LOCK.
307 **
308 ** This function assumes the following:
309 **
310 ** (a) The specified Btree object p is connected to a sharable
311 ** database (one with the BtShared.sharable flag set), and
312 **
313 ** (b) No other Btree objects hold a lock that conflicts
314 ** with the requested lock (i.e. querySharedCacheTableLock() has
315 ** already been called and returned SQLITE_OK).
316 **
317 ** SQLITE_OK is returned if the lock is added successfully. SQLITE_NOMEM
318 ** is returned if a malloc attempt fails.
319 */
320 static int setSharedCacheTableLock(Btree *p, Pgno iTable, u8 eLock){
321 BtShared *pBt = p->pBt;
322 BtLock *pLock = 0;
323 BtLock *pIter;
324
325 assert( sqlite3BtreeHoldsMutex(p) );
326 assert( eLock==READ_LOCK || eLock==WRITE_LOCK );
327 assert( p->db!=0 );
328
329 /* A connection with the read-uncommitted flag set will never try to
330 ** obtain a read-lock using this function. The only read-lock obtained
331 ** by a connection in read-uncommitted mode is on the sqlite_master
332 ** table, and that lock is obtained in BtreeBeginTrans(). */
333 assert( 0==(p->db->flags&SQLITE_ReadUncommitted) || eLock==WRITE_LOCK );
334
335 /* This function should only be called on a sharable b-tree after it
336 ** has been determined that no other b-tree holds a conflicting lock. */
337 assert( p->sharable );
338 assert( SQLITE_OK==querySharedCacheTableLock(p, iTable, eLock) );
339
340 /* First search the list for an existing lock on this table. */
341 for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
342 if( pIter->iTable==iTable && pIter->pBtree==p ){
343 pLock = pIter;
344 break;
345 }
346 }
347
348 /* If the above search did not find a BtLock struct associating Btree p
349 ** with table iTable, allocate one and link it into the list.
350 */
351 if( !pLock ){
352 pLock = (BtLock *)sqlite3MallocZero(sizeof(BtLock));
353 if( !pLock ){
354 return SQLITE_NOMEM_BKPT;
355 }
356 pLock->iTable = iTable;
357 pLock->pBtree = p;
358 pLock->pNext = pBt->pLock;
359 pBt->pLock = pLock;
360 }
361
362 /* Set the BtLock.eLock variable to the maximum of the current lock
363 ** and the requested lock. This means if a write-lock was already held
364 ** and a read-lock requested, we don't incorrectly downgrade the lock.
365 */
366 assert( WRITE_LOCK>READ_LOCK );
367 if( eLock>pLock->eLock ){
368 pLock->eLock = eLock;
369 }
370
371 return SQLITE_OK;
372 }
373 #endif /* !SQLITE_OMIT_SHARED_CACHE */
374
375 #ifndef SQLITE_OMIT_SHARED_CACHE
376 /*
377 ** Release all the table locks (locks obtained via calls to
378 ** the setSharedCacheTableLock() procedure) held by Btree object p.
379 **
380 ** This function assumes that Btree p has an open read or write
381 ** transaction. If it does not, then the BTS_PENDING flag
382 ** may be incorrectly cleared.
383 */
384 static void clearAllSharedCacheTableLocks(Btree *p){
385 BtShared *pBt = p->pBt;
386 BtLock **ppIter = &pBt->pLock;
387
388 assert( sqlite3BtreeHoldsMutex(p) );
389 assert( p->sharable || 0==*ppIter );
390 assert( p->inTrans>0 );
391
392 while( *ppIter ){
393 BtLock *pLock = *ppIter;
394 assert( (pBt->btsFlags & BTS_EXCLUSIVE)==0 || pBt->pWriter==pLock->pBtree );
395 assert( pLock->pBtree->inTrans>=pLock->eLock );
396 if( pLock->pBtree==p ){
397 *ppIter = pLock->pNext;
398 assert( pLock->iTable!=1 || pLock==&p->lock );
399 if( pLock->iTable!=1 ){
400 sqlite3_free(pLock);
401 }
402 }else{
403 ppIter = &pLock->pNext;
404 }
405 }
406
407 assert( (pBt->btsFlags & BTS_PENDING)==0 || pBt->pWriter );
408 if( pBt->pWriter==p ){
409 pBt->pWriter = 0;
410 pBt->btsFlags &= ~(BTS_EXCLUSIVE|BTS_PENDING);
411 }else if( pBt->nTransaction==2 ){
412 /* This function is called when Btree p is concluding its
413 ** transaction. If there currently exists a writer, and p is not
414 ** that writer, then the number of locks held by connections other
415 ** than the writer must be about to drop to zero. In this case
416 ** set the BTS_PENDING flag to 0.
417 **
418 ** If there is not currently a writer, then BTS_PENDING must
419 ** be zero already. So this next line is harmless in that case.
420 */
421 pBt->btsFlags &= ~BTS_PENDING;
422 }
423 }
424
425 /*
426 ** This function changes all write-locks held by Btree p into read-locks.
427 */
428 static void downgradeAllSharedCacheTableLocks(Btree *p){
429 BtShared *pBt = p->pBt;
430 if( pBt->pWriter==p ){
431 BtLock *pLock;
432 pBt->pWriter = 0;
433 pBt->btsFlags &= ~(BTS_EXCLUSIVE|BTS_PENDING);
434 for(pLock=pBt->pLock; pLock; pLock=pLock->pNext){
435 assert( pLock->eLock==READ_LOCK || pLock->pBtree==p );
436 pLock->eLock = READ_LOCK;
437 }
438 }
439 }
440
441 #endif /* SQLITE_OMIT_SHARED_CACHE */
442
443 static void releasePage(MemPage *pPage); /* Forward reference */
444
445 /*
446 ***** This routine is used inside of assert() only ****
447 **
448 ** Verify that the cursor holds the mutex on its BtShared
449 */
450 #ifdef SQLITE_DEBUG
451 static int cursorHoldsMutex(BtCursor *p){
452 return sqlite3_mutex_held(p->pBt->mutex);
453 }
454
455 /* Verify that the cursor and the BtShared agree about what is the current
456 ** database connetion. This is important in shared-cache mode. If the database
457 ** connection pointers get out-of-sync, it is possible for routines like
458 ** btreeInitPage() to reference an stale connection pointer that references a
459 ** a connection that has already closed. This routine is used inside assert()
460 ** statements only and for the purpose of double-checking that the btree code
461 ** does keep the database connection pointers up-to-date.
462 */
463 static int cursorOwnsBtShared(BtCursor *p){
464 assert( cursorHoldsMutex(p) );
465 return (p->pBtree->db==p->pBt->db);
466 }
467 #endif
468
469 /*
470 ** Invalidate the overflow cache of the cursor passed as the first argument.
471 ** on the shared btree structure pBt.
472 */
473 #define invalidateOverflowCache(pCur) (pCur->curFlags &= ~BTCF_ValidOvfl)
474
475 /*
476 ** Invalidate the overflow page-list cache for all cursors opened
477 ** on the shared btree structure pBt.
478 */
479 static void invalidateAllOverflowCache(BtShared *pBt){
480 BtCursor *p;
481 assert( sqlite3_mutex_held(pBt->mutex) );
482 for(p=pBt->pCursor; p; p=p->pNext){
483 invalidateOverflowCache(p);
484 }
485 }
486
487 #ifndef SQLITE_OMIT_INCRBLOB
488 /*
489 ** This function is called before modifying the contents of a table
490 ** to invalidate any incrblob cursors that are open on the
491 ** row or one of the rows being modified.
492 **
493 ** If argument isClearTable is true, then the entire contents of the
494 ** table is about to be deleted. In this case invalidate all incrblob
495 ** cursors open on any row within the table with root-page pgnoRoot.
496 **
497 ** Otherwise, if argument isClearTable is false, then the row with
498 ** rowid iRow is being replaced or deleted. In this case invalidate
499 ** only those incrblob cursors open on that specific row.
500 */
501 static void invalidateIncrblobCursors(
502 Btree *pBtree, /* The database file to check */
503 i64 iRow, /* The rowid that might be changing */
504 int isClearTable /* True if all rows are being deleted */
505 ){
506 BtCursor *p;
507 if( pBtree->hasIncrblobCur==0 ) return;
508 assert( sqlite3BtreeHoldsMutex(pBtree) );
509 pBtree->hasIncrblobCur = 0;
510 for(p=pBtree->pBt->pCursor; p; p=p->pNext){
511 if( (p->curFlags & BTCF_Incrblob)!=0 ){
512 pBtree->hasIncrblobCur = 1;
513 if( isClearTable || p->info.nKey==iRow ){
514 p->eState = CURSOR_INVALID;
515 }
516 }
517 }
518 }
519
520 #else
521 /* Stub function when INCRBLOB is omitted */
522 #define invalidateIncrblobCursors(x,y,z)
523 #endif /* SQLITE_OMIT_INCRBLOB */
524
525 /*
526 ** Set bit pgno of the BtShared.pHasContent bitvec. This is called
527 ** when a page that previously contained data becomes a free-list leaf
528 ** page.
529 **
530 ** The BtShared.pHasContent bitvec exists to work around an obscure
531 ** bug caused by the interaction of two useful IO optimizations surrounding
532 ** free-list leaf pages:
533 **
534 ** 1) When all data is deleted from a page and the page becomes
535 ** a free-list leaf page, the page is not written to the database
536 ** (as free-list leaf pages contain no meaningful data). Sometimes
537 ** such a page is not even journalled (as it will not be modified,
538 ** why bother journalling it?).
539 **
540 ** 2) When a free-list leaf page is reused, its content is not read
541 ** from the database or written to the journal file (why should it
542 ** be, if it is not at all meaningful?).
543 **
544 ** By themselves, these optimizations work fine and provide a handy
545 ** performance boost to bulk delete or insert operations. However, if
546 ** a page is moved to the free-list and then reused within the same
547 ** transaction, a problem comes up. If the page is not journalled when
548 ** it is moved to the free-list and it is also not journalled when it
549 ** is extracted from the free-list and reused, then the original data
550 ** may be lost. In the event of a rollback, it may not be possible
551 ** to restore the database to its original configuration.
552 **
553 ** The solution is the BtShared.pHasContent bitvec. Whenever a page is
554 ** moved to become a free-list leaf page, the corresponding bit is
555 ** set in the bitvec. Whenever a leaf page is extracted from the free-list,
556 ** optimization 2 above is omitted if the corresponding bit is already
557 ** set in BtShared.pHasContent. The contents of the bitvec are cleared
558 ** at the end of every transaction.
559 */
560 static int btreeSetHasContent(BtShared *pBt, Pgno pgno){
561 int rc = SQLITE_OK;
562 if( !pBt->pHasContent ){
563 assert( pgno<=pBt->nPage );
564 pBt->pHasContent = sqlite3BitvecCreate(pBt->nPage);
565 if( !pBt->pHasContent ){
566 rc = SQLITE_NOMEM_BKPT;
567 }
568 }
569 if( rc==SQLITE_OK && pgno<=sqlite3BitvecSize(pBt->pHasContent) ){
570 rc = sqlite3BitvecSet(pBt->pHasContent, pgno);
571 }
572 return rc;
573 }
574
575 /*
576 ** Query the BtShared.pHasContent vector.
577 **
578 ** This function is called when a free-list leaf page is removed from the
579 ** free-list for reuse. It returns false if it is safe to retrieve the
580 ** page from the pager layer with the 'no-content' flag set. True otherwise.
581 */
582 static int btreeGetHasContent(BtShared *pBt, Pgno pgno){
583 Bitvec *p = pBt->pHasContent;
584 return (p && (pgno>sqlite3BitvecSize(p) || sqlite3BitvecTest(p, pgno)));
585 }
586
587 /*
588 ** Clear (destroy) the BtShared.pHasContent bitvec. This should be
589 ** invoked at the conclusion of each write-transaction.
590 */
591 static void btreeClearHasContent(BtShared *pBt){
592 sqlite3BitvecDestroy(pBt->pHasContent);
593 pBt->pHasContent = 0;
594 }
595
596 /*
597 ** Release all of the apPage[] pages for a cursor.
598 */
599 static void btreeReleaseAllCursorPages(BtCursor *pCur){
600 int i;
601 for(i=0; i<=pCur->iPage; i++){
602 releasePage(pCur->apPage[i]);
603 pCur->apPage[i] = 0;
604 }
605 pCur->iPage = -1;
606 }
607
608 /*
609 ** The cursor passed as the only argument must point to a valid entry
610 ** when this function is called (i.e. have eState==CURSOR_VALID). This
611 ** function saves the current cursor key in variables pCur->nKey and
612 ** pCur->pKey. SQLITE_OK is returned if successful or an SQLite error
613 ** code otherwise.
614 **
615 ** If the cursor is open on an intkey table, then the integer key
616 ** (the rowid) is stored in pCur->nKey and pCur->pKey is left set to
617 ** NULL. If the cursor is open on a non-intkey table, then pCur->pKey is
618 ** set to point to a malloced buffer pCur->nKey bytes in size containing
619 ** the key.
620 */
621 static int saveCursorKey(BtCursor *pCur){
622 int rc = SQLITE_OK;
623 assert( CURSOR_VALID==pCur->eState );
624 assert( 0==pCur->pKey );
625 assert( cursorHoldsMutex(pCur) );
626
627 if( pCur->curIntKey ){
628 /* Only the rowid is required for a table btree */
629 pCur->nKey = sqlite3BtreeIntegerKey(pCur);
630 }else{
631 /* For an index btree, save the complete key content */
632 void *pKey;
633 pCur->nKey = sqlite3BtreePayloadSize(pCur);
634 pKey = sqlite3Malloc( pCur->nKey );
635 if( pKey ){
636 rc = sqlite3BtreePayload(pCur, 0, (int)pCur->nKey, pKey);
637 if( rc==SQLITE_OK ){
638 pCur->pKey = pKey;
639 }else{
640 sqlite3_free(pKey);
641 }
642 }else{
643 rc = SQLITE_NOMEM_BKPT;
644 }
645 }
646 assert( !pCur->curIntKey || !pCur->pKey );
647 return rc;
648 }
649
650 /*
651 ** Save the current cursor position in the variables BtCursor.nKey
652 ** and BtCursor.pKey. The cursor's state is set to CURSOR_REQUIRESEEK.
653 **
654 ** The caller must ensure that the cursor is valid (has eState==CURSOR_VALID)
655 ** prior to calling this routine.
656 */
657 static int saveCursorPosition(BtCursor *pCur){
658 int rc;
659
660 assert( CURSOR_VALID==pCur->eState || CURSOR_SKIPNEXT==pCur->eState );
661 assert( 0==pCur->pKey );
662 assert( cursorHoldsMutex(pCur) );
663
664 if( pCur->eState==CURSOR_SKIPNEXT ){
665 pCur->eState = CURSOR_VALID;
666 }else{
667 pCur->skipNext = 0;
668 }
669
670 rc = saveCursorKey(pCur);
671 if( rc==SQLITE_OK ){
672 btreeReleaseAllCursorPages(pCur);
673 pCur->eState = CURSOR_REQUIRESEEK;
674 }
675
676 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl|BTCF_AtLast);
677 return rc;
678 }
679
680 /* Forward reference */
681 static int SQLITE_NOINLINE saveCursorsOnList(BtCursor*,Pgno,BtCursor*);
682
683 /*
684 ** Save the positions of all cursors (except pExcept) that are open on
685 ** the table with root-page iRoot. "Saving the cursor position" means that
686 ** the location in the btree is remembered in such a way that it can be
687 ** moved back to the same spot after the btree has been modified. This
688 ** routine is called just before cursor pExcept is used to modify the
689 ** table, for example in BtreeDelete() or BtreeInsert().
690 **
691 ** If there are two or more cursors on the same btree, then all such
692 ** cursors should have their BTCF_Multiple flag set. The btreeCursor()
693 ** routine enforces that rule. This routine only needs to be called in
694 ** the uncommon case when pExpect has the BTCF_Multiple flag set.
695 **
696 ** If pExpect!=NULL and if no other cursors are found on the same root-page,
697 ** then the BTCF_Multiple flag on pExpect is cleared, to avoid another
698 ** pointless call to this routine.
699 **
700 ** Implementation note: This routine merely checks to see if any cursors
701 ** need to be saved. It calls out to saveCursorsOnList() in the (unusual)
702 ** event that cursors are in need to being saved.
703 */
704 static int saveAllCursors(BtShared *pBt, Pgno iRoot, BtCursor *pExcept){
705 BtCursor *p;
706 assert( sqlite3_mutex_held(pBt->mutex) );
707 assert( pExcept==0 || pExcept->pBt==pBt );
708 for(p=pBt->pCursor; p; p=p->pNext){
709 if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) ) break;
710 }
711 if( p ) return saveCursorsOnList(p, iRoot, pExcept);
712 if( pExcept ) pExcept->curFlags &= ~BTCF_Multiple;
713 return SQLITE_OK;
714 }
715
716 /* This helper routine to saveAllCursors does the actual work of saving
717 ** the cursors if and when a cursor is found that actually requires saving.
718 ** The common case is that no cursors need to be saved, so this routine is
719 ** broken out from its caller to avoid unnecessary stack pointer movement.
720 */
721 static int SQLITE_NOINLINE saveCursorsOnList(
722 BtCursor *p, /* The first cursor that needs saving */
723 Pgno iRoot, /* Only save cursor with this iRoot. Save all if zero */
724 BtCursor *pExcept /* Do not save this cursor */
725 ){
726 do{
727 if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) ){
728 if( p->eState==CURSOR_VALID || p->eState==CURSOR_SKIPNEXT ){
729 int rc = saveCursorPosition(p);
730 if( SQLITE_OK!=rc ){
731 return rc;
732 }
733 }else{
734 testcase( p->iPage>0 );
735 btreeReleaseAllCursorPages(p);
736 }
737 }
738 p = p->pNext;
739 }while( p );
740 return SQLITE_OK;
741 }
742
743 /*
744 ** Clear the current cursor position.
745 */
746 SQLITE_PRIVATE void sqlite3BtreeClearCursor(BtCursor *pCur){
747 assert( cursorHoldsMutex(pCur) );
748 sqlite3_free(pCur->pKey);
749 pCur->pKey = 0;
750 pCur->eState = CURSOR_INVALID;
751 }
752
753 /*
754 ** In this version of BtreeMoveto, pKey is a packed index record
755 ** such as is generated by the OP_MakeRecord opcode. Unpack the
756 ** record and then call BtreeMovetoUnpacked() to do the work.
757 */
758 static int btreeMoveto(
759 BtCursor *pCur, /* Cursor open on the btree to be searched */
760 const void *pKey, /* Packed key if the btree is an index */
761 i64 nKey, /* Integer key for tables. Size of pKey for indices */
762 int bias, /* Bias search to the high end */
763 int *pRes /* Write search results here */
764 ){
765 int rc; /* Status code */
766 UnpackedRecord *pIdxKey; /* Unpacked index key */
767
768 if( pKey ){
769 assert( nKey==(i64)(int)nKey );
770 pIdxKey = sqlite3VdbeAllocUnpackedRecord(pCur->pKeyInfo);
771 if( pIdxKey==0 ) return SQLITE_NOMEM_BKPT;
772 sqlite3VdbeRecordUnpack(pCur->pKeyInfo, (int)nKey, pKey, pIdxKey);
773 if( pIdxKey->nField==0 ){
774 rc = SQLITE_CORRUPT_BKPT;
775 goto moveto_done;
776 }
777 }else{
778 pIdxKey = 0;
779 }
780 rc = sqlite3BtreeMovetoUnpacked(pCur, pIdxKey, nKey, bias, pRes);
781 moveto_done:
782 if( pIdxKey ){
783 sqlite3DbFree(pCur->pKeyInfo->db, pIdxKey);
784 }
785 return rc;
786 }
787
788 /*
789 ** Restore the cursor to the position it was in (or as close to as possible)
790 ** when saveCursorPosition() was called. Note that this call deletes the
791 ** saved position info stored by saveCursorPosition(), so there can be
792 ** at most one effective restoreCursorPosition() call after each
793 ** saveCursorPosition().
794 */
795 static int btreeRestoreCursorPosition(BtCursor *pCur){
796 int rc;
797 int skipNext;
798 assert( cursorOwnsBtShared(pCur) );
799 assert( pCur->eState>=CURSOR_REQUIRESEEK );
800 if( pCur->eState==CURSOR_FAULT ){
801 return pCur->skipNext;
802 }
803 pCur->eState = CURSOR_INVALID;
804 rc = btreeMoveto(pCur, pCur->pKey, pCur->nKey, 0, &skipNext);
805 if( rc==SQLITE_OK ){
806 sqlite3_free(pCur->pKey);
807 pCur->pKey = 0;
808 assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_INVALID );
809 pCur->skipNext |= skipNext;
810 if( pCur->skipNext && pCur->eState==CURSOR_VALID ){
811 pCur->eState = CURSOR_SKIPNEXT;
812 }
813 }
814 return rc;
815 }
816
817 #define restoreCursorPosition(p) \
818 (p->eState>=CURSOR_REQUIRESEEK ? \
819 btreeRestoreCursorPosition(p) : \
820 SQLITE_OK)
821
822 /*
823 ** Determine whether or not a cursor has moved from the position where
824 ** it was last placed, or has been invalidated for any other reason.
825 ** Cursors can move when the row they are pointing at is deleted out
826 ** from under them, for example. Cursor might also move if a btree
827 ** is rebalanced.
828 **
829 ** Calling this routine with a NULL cursor pointer returns false.
830 **
831 ** Use the separate sqlite3BtreeCursorRestore() routine to restore a cursor
832 ** back to where it ought to be if this routine returns true.
833 */
834 SQLITE_PRIVATE int sqlite3BtreeCursorHasMoved(BtCursor *pCur){
835 return pCur->eState!=CURSOR_VALID;
836 }
837
838 /*
839 ** This routine restores a cursor back to its original position after it
840 ** has been moved by some outside activity (such as a btree rebalance or
841 ** a row having been deleted out from under the cursor).
842 **
843 ** On success, the *pDifferentRow parameter is false if the cursor is left
844 ** pointing at exactly the same row. *pDifferntRow is the row the cursor
845 ** was pointing to has been deleted, forcing the cursor to point to some
846 ** nearby row.
847 **
848 ** This routine should only be called for a cursor that just returned
849 ** TRUE from sqlite3BtreeCursorHasMoved().
850 */
851 SQLITE_PRIVATE int sqlite3BtreeCursorRestore(BtCursor *pCur, int *pDifferentRow) {
852 int rc;
853
854 assert( pCur!=0 );
855 assert( pCur->eState!=CURSOR_VALID );
856 rc = restoreCursorPosition(pCur);
857 if( rc ){
858 *pDifferentRow = 1;
859 return rc;
860 }
861 if( pCur->eState!=CURSOR_VALID ){
862 *pDifferentRow = 1;
863 }else{
864 assert( pCur->skipNext==0 );
865 *pDifferentRow = 0;
866 }
867 return SQLITE_OK;
868 }
869
870 #ifdef SQLITE_ENABLE_CURSOR_HINTS
871 /*
872 ** Provide hints to the cursor. The particular hint given (and the type
873 ** and number of the varargs parameters) is determined by the eHintType
874 ** parameter. See the definitions of the BTREE_HINT_* macros for details.
875 */
876 SQLITE_PRIVATE void sqlite3BtreeCursorHint(BtCursor *pCur, int eHintType, ...){
877 /* Used only by system that substitute their own storage engine */
878 }
879 #endif
880
881 /*
882 ** Provide flag hints to the cursor.
883 */
884 SQLITE_PRIVATE void sqlite3BtreeCursorHintFlags(BtCursor *pCur, unsigned x){
885 assert( x==BTREE_SEEK_EQ || x==BTREE_BULKLOAD || x==0 );
886 pCur->hints = x;
887 }
888
889
890 #ifndef SQLITE_OMIT_AUTOVACUUM
891 /*
892 ** Given a page number of a regular database page, return the page
893 ** number for the pointer-map page that contains the entry for the
894 ** input page number.
895 **
896 ** Return 0 (not a valid page) for pgno==1 since there is
897 ** no pointer map associated with page 1. The integrity_check logic
898 ** requires that ptrmapPageno(*,1)!=1.
899 */
900 static Pgno ptrmapPageno(BtShared *pBt, Pgno pgno){
901 int nPagesPerMapPage;
902 Pgno iPtrMap, ret;
903 assert( sqlite3_mutex_held(pBt->mutex) );
904 if( pgno<2 ) return 0;
905 nPagesPerMapPage = (pBt->usableSize/5)+1;
906 iPtrMap = (pgno-2)/nPagesPerMapPage;
907 ret = (iPtrMap*nPagesPerMapPage) + 2;
908 if( ret==PENDING_BYTE_PAGE(pBt) ){
909 ret++;
910 }
911 return ret;
912 }
913
914 /*
915 ** Write an entry into the pointer map.
916 **
917 ** This routine updates the pointer map entry for page number 'key'
918 ** so that it maps to type 'eType' and parent page number 'pgno'.
919 **
920 ** If *pRC is initially non-zero (non-SQLITE_OK) then this routine is
921 ** a no-op. If an error occurs, the appropriate error code is written
922 ** into *pRC.
923 */
924 static void ptrmapPut(BtShared *pBt, Pgno key, u8 eType, Pgno parent, int *pRC){
925 DbPage *pDbPage; /* The pointer map page */
926 u8 *pPtrmap; /* The pointer map data */
927 Pgno iPtrmap; /* The pointer map page number */
928 int offset; /* Offset in pointer map page */
929 int rc; /* Return code from subfunctions */
930
931 if( *pRC ) return;
932
933 assert( sqlite3_mutex_held(pBt->mutex) );
934 /* The master-journal page number must never be used as a pointer map page */
935 assert( 0==PTRMAP_ISPAGE(pBt, PENDING_BYTE_PAGE(pBt)) );
936
937 assert( pBt->autoVacuum );
938 if( key==0 ){
939 *pRC = SQLITE_CORRUPT_BKPT;
940 return;
941 }
942 iPtrmap = PTRMAP_PAGENO(pBt, key);
943 rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage, 0);
944 if( rc!=SQLITE_OK ){
945 *pRC = rc;
946 return;
947 }
948 offset = PTRMAP_PTROFFSET(iPtrmap, key);
949 if( offset<0 ){
950 *pRC = SQLITE_CORRUPT_BKPT;
951 goto ptrmap_exit;
952 }
953 assert( offset <= (int)pBt->usableSize-5 );
954 pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage);
955
956 if( eType!=pPtrmap[offset] || get4byte(&pPtrmap[offset+1])!=parent ){
957 TRACE(("PTRMAP_UPDATE: %d->(%d,%d)\n", key, eType, parent));
958 *pRC= rc = sqlite3PagerWrite(pDbPage);
959 if( rc==SQLITE_OK ){
960 pPtrmap[offset] = eType;
961 put4byte(&pPtrmap[offset+1], parent);
962 }
963 }
964
965 ptrmap_exit:
966 sqlite3PagerUnref(pDbPage);
967 }
968
969 /*
970 ** Read an entry from the pointer map.
971 **
972 ** This routine retrieves the pointer map entry for page 'key', writing
973 ** the type and parent page number to *pEType and *pPgno respectively.
974 ** An error code is returned if something goes wrong, otherwise SQLITE_OK.
975 */
976 static int ptrmapGet(BtShared *pBt, Pgno key, u8 *pEType, Pgno *pPgno){
977 DbPage *pDbPage; /* The pointer map page */
978 int iPtrmap; /* Pointer map page index */
979 u8 *pPtrmap; /* Pointer map page data */
980 int offset; /* Offset of entry in pointer map */
981 int rc;
982
983 assert( sqlite3_mutex_held(pBt->mutex) );
984
985 iPtrmap = PTRMAP_PAGENO(pBt, key);
986 rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage, 0);
987 if( rc!=0 ){
988 return rc;
989 }
990 pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage);
991
992 offset = PTRMAP_PTROFFSET(iPtrmap, key);
993 if( offset<0 ){
994 sqlite3PagerUnref(pDbPage);
995 return SQLITE_CORRUPT_BKPT;
996 }
997 assert( offset <= (int)pBt->usableSize-5 );
998 assert( pEType!=0 );
999 *pEType = pPtrmap[offset];
1000 if( pPgno ) *pPgno = get4byte(&pPtrmap[offset+1]);
1001
1002 sqlite3PagerUnref(pDbPage);
1003 if( *pEType<1 || *pEType>5 ) return SQLITE_CORRUPT_BKPT;
1004 return SQLITE_OK;
1005 }
1006
1007 #else /* if defined SQLITE_OMIT_AUTOVACUUM */
1008 #define ptrmapPut(w,x,y,z,rc)
1009 #define ptrmapGet(w,x,y,z) SQLITE_OK
1010 #define ptrmapPutOvflPtr(x, y, rc)
1011 #endif
1012
1013 /*
1014 ** Given a btree page and a cell index (0 means the first cell on
1015 ** the page, 1 means the second cell, and so forth) return a pointer
1016 ** to the cell content.
1017 **
1018 ** findCellPastPtr() does the same except it skips past the initial
1019 ** 4-byte child pointer found on interior pages, if there is one.
1020 **
1021 ** This routine works only for pages that do not contain overflow cells.
1022 */
1023 #define findCell(P,I) \
1024 ((P)->aData + ((P)->maskPage & get2byteAligned(&(P)->aCellIdx[2*(I)])))
1025 #define findCellPastPtr(P,I) \
1026 ((P)->aDataOfst + ((P)->maskPage & get2byteAligned(&(P)->aCellIdx[2*(I)])))
1027
1028
1029 /*
1030 ** This is common tail processing for btreeParseCellPtr() and
1031 ** btreeParseCellPtrIndex() for the case when the cell does not fit entirely
1032 ** on a single B-tree page. Make necessary adjustments to the CellInfo
1033 ** structure.
1034 */
1035 static SQLITE_NOINLINE void btreeParseCellAdjustSizeForOverflow(
1036 MemPage *pPage, /* Page containing the cell */
1037 u8 *pCell, /* Pointer to the cell text. */
1038 CellInfo *pInfo /* Fill in this structure */
1039 ){
1040 /* If the payload will not fit completely on the local page, we have
1041 ** to decide how much to store locally and how much to spill onto
1042 ** overflow pages. The strategy is to minimize the amount of unused
1043 ** space on overflow pages while keeping the amount of local storage
1044 ** in between minLocal and maxLocal.
1045 **
1046 ** Warning: changing the way overflow payload is distributed in any
1047 ** way will result in an incompatible file format.
1048 */
1049 int minLocal; /* Minimum amount of payload held locally */
1050 int maxLocal; /* Maximum amount of payload held locally */
1051 int surplus; /* Overflow payload available for local storage */
1052
1053 minLocal = pPage->minLocal;
1054 maxLocal = pPage->maxLocal;
1055 surplus = minLocal + (pInfo->nPayload - minLocal)%(pPage->pBt->usableSize-4);
1056 testcase( surplus==maxLocal );
1057 testcase( surplus==maxLocal+1 );
1058 if( surplus <= maxLocal ){
1059 pInfo->nLocal = (u16)surplus;
1060 }else{
1061 pInfo->nLocal = (u16)minLocal;
1062 }
1063 pInfo->nSize = (u16)(&pInfo->pPayload[pInfo->nLocal] - pCell) + 4;
1064 }
1065
1066 /*
1067 ** The following routines are implementations of the MemPage.xParseCell()
1068 ** method.
1069 **
1070 ** Parse a cell content block and fill in the CellInfo structure.
1071 **
1072 ** btreeParseCellPtr() => table btree leaf nodes
1073 ** btreeParseCellNoPayload() => table btree internal nodes
1074 ** btreeParseCellPtrIndex() => index btree nodes
1075 **
1076 ** There is also a wrapper function btreeParseCell() that works for
1077 ** all MemPage types and that references the cell by index rather than
1078 ** by pointer.
1079 */
1080 static void btreeParseCellPtrNoPayload(
1081 MemPage *pPage, /* Page containing the cell */
1082 u8 *pCell, /* Pointer to the cell text. */
1083 CellInfo *pInfo /* Fill in this structure */
1084 ){
1085 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
1086 assert( pPage->leaf==0 );
1087 assert( pPage->childPtrSize==4 );
1088 #ifndef SQLITE_DEBUG
1089 UNUSED_PARAMETER(pPage);
1090 #endif
1091 pInfo->nSize = 4 + getVarint(&pCell[4], (u64*)&pInfo->nKey);
1092 pInfo->nPayload = 0;
1093 pInfo->nLocal = 0;
1094 pInfo->pPayload = 0;
1095 return;
1096 }
1097 static void btreeParseCellPtr(
1098 MemPage *pPage, /* Page containing the cell */
1099 u8 *pCell, /* Pointer to the cell text. */
1100 CellInfo *pInfo /* Fill in this structure */
1101 ){
1102 u8 *pIter; /* For scanning through pCell */
1103 u32 nPayload; /* Number of bytes of cell payload */
1104 u64 iKey; /* Extracted Key value */
1105
1106 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
1107 assert( pPage->leaf==0 || pPage->leaf==1 );
1108 assert( pPage->intKeyLeaf );
1109 assert( pPage->childPtrSize==0 );
1110 pIter = pCell;
1111
1112 /* The next block of code is equivalent to:
1113 **
1114 ** pIter += getVarint32(pIter, nPayload);
1115 **
1116 ** The code is inlined to avoid a function call.
1117 */
1118 nPayload = *pIter;
1119 if( nPayload>=0x80 ){
1120 u8 *pEnd = &pIter[8];
1121 nPayload &= 0x7f;
1122 do{
1123 nPayload = (nPayload<<7) | (*++pIter & 0x7f);
1124 }while( (*pIter)>=0x80 && pIter<pEnd );
1125 }
1126 pIter++;
1127
1128 /* The next block of code is equivalent to:
1129 **
1130 ** pIter += getVarint(pIter, (u64*)&pInfo->nKey);
1131 **
1132 ** The code is inlined to avoid a function call.
1133 */
1134 iKey = *pIter;
1135 if( iKey>=0x80 ){
1136 u8 *pEnd = &pIter[7];
1137 iKey &= 0x7f;
1138 while(1){
1139 iKey = (iKey<<7) | (*++pIter & 0x7f);
1140 if( (*pIter)<0x80 ) break;
1141 if( pIter>=pEnd ){
1142 iKey = (iKey<<8) | *++pIter;
1143 break;
1144 }
1145 }
1146 }
1147 pIter++;
1148
1149 pInfo->nKey = *(i64*)&iKey;
1150 pInfo->nPayload = nPayload;
1151 pInfo->pPayload = pIter;
1152 testcase( nPayload==pPage->maxLocal );
1153 testcase( nPayload==pPage->maxLocal+1 );
1154 if( nPayload<=pPage->maxLocal ){
1155 /* This is the (easy) common case where the entire payload fits
1156 ** on the local page. No overflow is required.
1157 */
1158 pInfo->nSize = nPayload + (u16)(pIter - pCell);
1159 if( pInfo->nSize<4 ) pInfo->nSize = 4;
1160 pInfo->nLocal = (u16)nPayload;
1161 }else{
1162 btreeParseCellAdjustSizeForOverflow(pPage, pCell, pInfo);
1163 }
1164 }
1165 static void btreeParseCellPtrIndex(
1166 MemPage *pPage, /* Page containing the cell */
1167 u8 *pCell, /* Pointer to the cell text. */
1168 CellInfo *pInfo /* Fill in this structure */
1169 ){
1170 u8 *pIter; /* For scanning through pCell */
1171 u32 nPayload; /* Number of bytes of cell payload */
1172
1173 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
1174 assert( pPage->leaf==0 || pPage->leaf==1 );
1175 assert( pPage->intKeyLeaf==0 );
1176 pIter = pCell + pPage->childPtrSize;
1177 nPayload = *pIter;
1178 if( nPayload>=0x80 ){
1179 u8 *pEnd = &pIter[8];
1180 nPayload &= 0x7f;
1181 do{
1182 nPayload = (nPayload<<7) | (*++pIter & 0x7f);
1183 }while( *(pIter)>=0x80 && pIter<pEnd );
1184 }
1185 pIter++;
1186 pInfo->nKey = nPayload;
1187 pInfo->nPayload = nPayload;
1188 pInfo->pPayload = pIter;
1189 testcase( nPayload==pPage->maxLocal );
1190 testcase( nPayload==pPage->maxLocal+1 );
1191 if( nPayload<=pPage->maxLocal ){
1192 /* This is the (easy) common case where the entire payload fits
1193 ** on the local page. No overflow is required.
1194 */
1195 pInfo->nSize = nPayload + (u16)(pIter - pCell);
1196 if( pInfo->nSize<4 ) pInfo->nSize = 4;
1197 pInfo->nLocal = (u16)nPayload;
1198 }else{
1199 btreeParseCellAdjustSizeForOverflow(pPage, pCell, pInfo);
1200 }
1201 }
1202 static void btreeParseCell(
1203 MemPage *pPage, /* Page containing the cell */
1204 int iCell, /* The cell index. First cell is 0 */
1205 CellInfo *pInfo /* Fill in this structure */
1206 ){
1207 pPage->xParseCell(pPage, findCell(pPage, iCell), pInfo);
1208 }
1209
1210 /*
1211 ** The following routines are implementations of the MemPage.xCellSize
1212 ** method.
1213 **
1214 ** Compute the total number of bytes that a Cell needs in the cell
1215 ** data area of the btree-page. The return number includes the cell
1216 ** data header and the local payload, but not any overflow page or
1217 ** the space used by the cell pointer.
1218 **
1219 ** cellSizePtrNoPayload() => table internal nodes
1220 ** cellSizePtr() => all index nodes & table leaf nodes
1221 */
1222 static u16 cellSizePtr(MemPage *pPage, u8 *pCell){
1223 u8 *pIter = pCell + pPage->childPtrSize; /* For looping over bytes of pCell */
1224 u8 *pEnd; /* End mark for a varint */
1225 u32 nSize; /* Size value to return */
1226
1227 #ifdef SQLITE_DEBUG
1228 /* The value returned by this function should always be the same as
1229 ** the (CellInfo.nSize) value found by doing a full parse of the
1230 ** cell. If SQLITE_DEBUG is defined, an assert() at the bottom of
1231 ** this function verifies that this invariant is not violated. */
1232 CellInfo debuginfo;
1233 pPage->xParseCell(pPage, pCell, &debuginfo);
1234 #endif
1235
1236 nSize = *pIter;
1237 if( nSize>=0x80 ){
1238 pEnd = &pIter[8];
1239 nSize &= 0x7f;
1240 do{
1241 nSize = (nSize<<7) | (*++pIter & 0x7f);
1242 }while( *(pIter)>=0x80 && pIter<pEnd );
1243 }
1244 pIter++;
1245 if( pPage->intKey ){
1246 /* pIter now points at the 64-bit integer key value, a variable length
1247 ** integer. The following block moves pIter to point at the first byte
1248 ** past the end of the key value. */
1249 pEnd = &pIter[9];
1250 while( (*pIter++)&0x80 && pIter<pEnd );
1251 }
1252 testcase( nSize==pPage->maxLocal );
1253 testcase( nSize==pPage->maxLocal+1 );
1254 if( nSize<=pPage->maxLocal ){
1255 nSize += (u32)(pIter - pCell);
1256 if( nSize<4 ) nSize = 4;
1257 }else{
1258 int minLocal = pPage->minLocal;
1259 nSize = minLocal + (nSize - minLocal) % (pPage->pBt->usableSize - 4);
1260 testcase( nSize==pPage->maxLocal );
1261 testcase( nSize==pPage->maxLocal+1 );
1262 if( nSize>pPage->maxLocal ){
1263 nSize = minLocal;
1264 }
1265 nSize += 4 + (u16)(pIter - pCell);
1266 }
1267 assert( nSize==debuginfo.nSize || CORRUPT_DB );
1268 return (u16)nSize;
1269 }
1270 static u16 cellSizePtrNoPayload(MemPage *pPage, u8 *pCell){
1271 u8 *pIter = pCell + 4; /* For looping over bytes of pCell */
1272 u8 *pEnd; /* End mark for a varint */
1273
1274 #ifdef SQLITE_DEBUG
1275 /* The value returned by this function should always be the same as
1276 ** the (CellInfo.nSize) value found by doing a full parse of the
1277 ** cell. If SQLITE_DEBUG is defined, an assert() at the bottom of
1278 ** this function verifies that this invariant is not violated. */
1279 CellInfo debuginfo;
1280 pPage->xParseCell(pPage, pCell, &debuginfo);
1281 #else
1282 UNUSED_PARAMETER(pPage);
1283 #endif
1284
1285 assert( pPage->childPtrSize==4 );
1286 pEnd = pIter + 9;
1287 while( (*pIter++)&0x80 && pIter<pEnd );
1288 assert( debuginfo.nSize==(u16)(pIter - pCell) || CORRUPT_DB );
1289 return (u16)(pIter - pCell);
1290 }
1291
1292
1293 #ifdef SQLITE_DEBUG
1294 /* This variation on cellSizePtr() is used inside of assert() statements
1295 ** only. */
1296 static u16 cellSize(MemPage *pPage, int iCell){
1297 return pPage->xCellSize(pPage, findCell(pPage, iCell));
1298 }
1299 #endif
1300
1301 #ifndef SQLITE_OMIT_AUTOVACUUM
1302 /*
1303 ** If the cell pCell, part of page pPage contains a pointer
1304 ** to an overflow page, insert an entry into the pointer-map
1305 ** for the overflow page.
1306 */
1307 static void ptrmapPutOvflPtr(MemPage *pPage, u8 *pCell, int *pRC){
1308 CellInfo info;
1309 if( *pRC ) return;
1310 assert( pCell!=0 );
1311 pPage->xParseCell(pPage, pCell, &info);
1312 if( info.nLocal<info.nPayload ){
1313 Pgno ovfl = get4byte(&pCell[info.nSize-4]);
1314 ptrmapPut(pPage->pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno, pRC);
1315 }
1316 }
1317 #endif
1318
1319
1320 /*
1321 ** Defragment the page given. All Cells are moved to the
1322 ** end of the page and all free space is collected into one
1323 ** big FreeBlk that occurs in between the header and cell
1324 ** pointer array and the cell content area.
1325 **
1326 ** EVIDENCE-OF: R-44582-60138 SQLite may from time to time reorganize a
1327 ** b-tree page so that there are no freeblocks or fragment bytes, all
1328 ** unused bytes are contained in the unallocated space region, and all
1329 ** cells are packed tightly at the end of the page.
1330 */
1331 static int defragmentPage(MemPage *pPage){
1332 int i; /* Loop counter */
1333 int pc; /* Address of the i-th cell */
1334 int hdr; /* Offset to the page header */
1335 int size; /* Size of a cell */
1336 int usableSize; /* Number of usable bytes on a page */
1337 int cellOffset; /* Offset to the cell pointer array */
1338 int cbrk; /* Offset to the cell content area */
1339 int nCell; /* Number of cells on the page */
1340 unsigned char *data; /* The page data */
1341 unsigned char *temp; /* Temp area for cell content */
1342 unsigned char *src; /* Source of content */
1343 int iCellFirst; /* First allowable cell index */
1344 int iCellLast; /* Last possible cell index */
1345
1346
1347 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
1348 assert( pPage->pBt!=0 );
1349 assert( pPage->pBt->usableSize <= SQLITE_MAX_PAGE_SIZE );
1350 assert( pPage->nOverflow==0 );
1351 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
1352 temp = 0;
1353 src = data = pPage->aData;
1354 hdr = pPage->hdrOffset;
1355 cellOffset = pPage->cellOffset;
1356 nCell = pPage->nCell;
1357 assert( nCell==get2byte(&data[hdr+3]) );
1358 usableSize = pPage->pBt->usableSize;
1359 cbrk = usableSize;
1360 iCellFirst = cellOffset + 2*nCell;
1361 iCellLast = usableSize - 4;
1362 for(i=0; i<nCell; i++){
1363 u8 *pAddr; /* The i-th cell pointer */
1364 pAddr = &data[cellOffset + i*2];
1365 pc = get2byte(pAddr);
1366 testcase( pc==iCellFirst );
1367 testcase( pc==iCellLast );
1368 /* These conditions have already been verified in btreeInitPage()
1369 ** if PRAGMA cell_size_check=ON.
1370 */
1371 if( pc<iCellFirst || pc>iCellLast ){
1372 return SQLITE_CORRUPT_BKPT;
1373 }
1374 assert( pc>=iCellFirst && pc<=iCellLast );
1375 size = pPage->xCellSize(pPage, &src[pc]);
1376 cbrk -= size;
1377 if( cbrk<iCellFirst || pc+size>usableSize ){
1378 return SQLITE_CORRUPT_BKPT;
1379 }
1380 assert( cbrk+size<=usableSize && cbrk>=iCellFirst );
1381 testcase( cbrk+size==usableSize );
1382 testcase( pc+size==usableSize );
1383 put2byte(pAddr, cbrk);
1384 if( temp==0 ){
1385 int x;
1386 if( cbrk==pc ) continue;
1387 temp = sqlite3PagerTempSpace(pPage->pBt->pPager);
1388 x = get2byte(&data[hdr+5]);
1389 memcpy(&temp[x], &data[x], (cbrk+size) - x);
1390 src = temp;
1391 }
1392 memcpy(&data[cbrk], &src[pc], size);
1393 }
1394 assert( cbrk>=iCellFirst );
1395 put2byte(&data[hdr+5], cbrk);
1396 data[hdr+1] = 0;
1397 data[hdr+2] = 0;
1398 data[hdr+7] = 0;
1399 memset(&data[iCellFirst], 0, cbrk-iCellFirst);
1400 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
1401 if( cbrk-iCellFirst!=pPage->nFree ){
1402 return SQLITE_CORRUPT_BKPT;
1403 }
1404 return SQLITE_OK;
1405 }
1406
1407 /*
1408 ** Search the free-list on page pPg for space to store a cell nByte bytes in
1409 ** size. If one can be found, return a pointer to the space and remove it
1410 ** from the free-list.
1411 **
1412 ** If no suitable space can be found on the free-list, return NULL.
1413 **
1414 ** This function may detect corruption within pPg. If corruption is
1415 ** detected then *pRc is set to SQLITE_CORRUPT and NULL is returned.
1416 **
1417 ** Slots on the free list that are between 1 and 3 bytes larger than nByte
1418 ** will be ignored if adding the extra space to the fragmentation count
1419 ** causes the fragmentation count to exceed 60.
1420 */
1421 static u8 *pageFindSlot(MemPage *pPg, int nByte, int *pRc){
1422 const int hdr = pPg->hdrOffset;
1423 u8 * const aData = pPg->aData;
1424 int iAddr = hdr + 1;
1425 int pc = get2byte(&aData[iAddr]);
1426 int x;
1427 int usableSize = pPg->pBt->usableSize;
1428
1429 assert( pc>0 );
1430 do{
1431 int size; /* Size of the free slot */
1432 /* EVIDENCE-OF: R-06866-39125 Freeblocks are always connected in order of
1433 ** increasing offset. */
1434 if( pc>usableSize-4 || pc<iAddr+4 ){
1435 *pRc = SQLITE_CORRUPT_BKPT;
1436 return 0;
1437 }
1438 /* EVIDENCE-OF: R-22710-53328 The third and fourth bytes of each
1439 ** freeblock form a big-endian integer which is the size of the freeblock
1440 ** in bytes, including the 4-byte header. */
1441 size = get2byte(&aData[pc+2]);
1442 if( (x = size - nByte)>=0 ){
1443 testcase( x==4 );
1444 testcase( x==3 );
1445 if( pc < pPg->cellOffset+2*pPg->nCell || size+pc > usableSize ){
1446 *pRc = SQLITE_CORRUPT_BKPT;
1447 return 0;
1448 }else if( x<4 ){
1449 /* EVIDENCE-OF: R-11498-58022 In a well-formed b-tree page, the total
1450 ** number of bytes in fragments may not exceed 60. */
1451 if( aData[hdr+7]>57 ) return 0;
1452
1453 /* Remove the slot from the free-list. Update the number of
1454 ** fragmented bytes within the page. */
1455 memcpy(&aData[iAddr], &aData[pc], 2);
1456 aData[hdr+7] += (u8)x;
1457 }else{
1458 /* The slot remains on the free-list. Reduce its size to account
1459 ** for the portion used by the new allocation. */
1460 put2byte(&aData[pc+2], x);
1461 }
1462 return &aData[pc + x];
1463 }
1464 iAddr = pc;
1465 pc = get2byte(&aData[pc]);
1466 }while( pc );
1467
1468 return 0;
1469 }
1470
1471 /*
1472 ** Allocate nByte bytes of space from within the B-Tree page passed
1473 ** as the first argument. Write into *pIdx the index into pPage->aData[]
1474 ** of the first byte of allocated space. Return either SQLITE_OK or
1475 ** an error code (usually SQLITE_CORRUPT).
1476 **
1477 ** The caller guarantees that there is sufficient space to make the
1478 ** allocation. This routine might need to defragment in order to bring
1479 ** all the space together, however. This routine will avoid using
1480 ** the first two bytes past the cell pointer area since presumably this
1481 ** allocation is being made in order to insert a new cell, so we will
1482 ** also end up needing a new cell pointer.
1483 */
1484 static int allocateSpace(MemPage *pPage, int nByte, int *pIdx){
1485 const int hdr = pPage->hdrOffset; /* Local cache of pPage->hdrOffset */
1486 u8 * const data = pPage->aData; /* Local cache of pPage->aData */
1487 int top; /* First byte of cell content area */
1488 int rc = SQLITE_OK; /* Integer return code */
1489 int gap; /* First byte of gap between cell pointers and cell content */
1490
1491 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
1492 assert( pPage->pBt );
1493 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
1494 assert( nByte>=0 ); /* Minimum cell size is 4 */
1495 assert( pPage->nFree>=nByte );
1496 assert( pPage->nOverflow==0 );
1497 assert( nByte < (int)(pPage->pBt->usableSize-8) );
1498
1499 assert( pPage->cellOffset == hdr + 12 - 4*pPage->leaf );
1500 gap = pPage->cellOffset + 2*pPage->nCell;
1501 assert( gap<=65536 );
1502 /* EVIDENCE-OF: R-29356-02391 If the database uses a 65536-byte page size
1503 ** and the reserved space is zero (the usual value for reserved space)
1504 ** then the cell content offset of an empty page wants to be 65536.
1505 ** However, that integer is too large to be stored in a 2-byte unsigned
1506 ** integer, so a value of 0 is used in its place. */
1507 top = get2byte(&data[hdr+5]);
1508 assert( top<=(int)pPage->pBt->usableSize ); /* Prevent by getAndInitPage() */
1509 if( gap>top ){
1510 if( top==0 && pPage->pBt->usableSize==65536 ){
1511 top = 65536;
1512 }else{
1513 return SQLITE_CORRUPT_BKPT;
1514 }
1515 }
1516
1517 /* If there is enough space between gap and top for one more cell pointer
1518 ** array entry offset, and if the freelist is not empty, then search the
1519 ** freelist looking for a free slot big enough to satisfy the request.
1520 */
1521 testcase( gap+2==top );
1522 testcase( gap+1==top );
1523 testcase( gap==top );
1524 if( (data[hdr+2] || data[hdr+1]) && gap+2<=top ){
1525 u8 *pSpace = pageFindSlot(pPage, nByte, &rc);
1526 if( pSpace ){
1527 assert( pSpace>=data && (pSpace - data)<65536 );
1528 *pIdx = (int)(pSpace - data);
1529 return SQLITE_OK;
1530 }else if( rc ){
1531 return rc;
1532 }
1533 }
1534
1535 /* The request could not be fulfilled using a freelist slot. Check
1536 ** to see if defragmentation is necessary.
1537 */
1538 testcase( gap+2+nByte==top );
1539 if( gap+2+nByte>top ){
1540 assert( pPage->nCell>0 || CORRUPT_DB );
1541 rc = defragmentPage(pPage);
1542 if( rc ) return rc;
1543 top = get2byteNotZero(&data[hdr+5]);
1544 assert( gap+nByte<=top );
1545 }
1546
1547
1548 /* Allocate memory from the gap in between the cell pointer array
1549 ** and the cell content area. The btreeInitPage() call has already
1550 ** validated the freelist. Given that the freelist is valid, there
1551 ** is no way that the allocation can extend off the end of the page.
1552 ** The assert() below verifies the previous sentence.
1553 */
1554 top -= nByte;
1555 put2byte(&data[hdr+5], top);
1556 assert( top+nByte <= (int)pPage->pBt->usableSize );
1557 *pIdx = top;
1558 return SQLITE_OK;
1559 }
1560
1561 /*
1562 ** Return a section of the pPage->aData to the freelist.
1563 ** The first byte of the new free block is pPage->aData[iStart]
1564 ** and the size of the block is iSize bytes.
1565 **
1566 ** Adjacent freeblocks are coalesced.
1567 **
1568 ** Note that even though the freeblock list was checked by btreeInitPage(),
1569 ** that routine will not detect overlap between cells or freeblocks. Nor
1570 ** does it detect cells or freeblocks that encrouch into the reserved bytes
1571 ** at the end of the page. So do additional corruption checks inside this
1572 ** routine and return SQLITE_CORRUPT if any problems are found.
1573 */
1574 static int freeSpace(MemPage *pPage, u16 iStart, u16 iSize){
1575 u16 iPtr; /* Address of ptr to next freeblock */
1576 u16 iFreeBlk; /* Address of the next freeblock */
1577 u8 hdr; /* Page header size. 0 or 100 */
1578 u8 nFrag = 0; /* Reduction in fragmentation */
1579 u16 iOrigSize = iSize; /* Original value of iSize */
1580 u32 iLast = pPage->pBt->usableSize-4; /* Largest possible freeblock offset */
1581 u32 iEnd = iStart + iSize; /* First byte past the iStart buffer */
1582 unsigned char *data = pPage->aData; /* Page content */
1583
1584 assert( pPage->pBt!=0 );
1585 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
1586 assert( CORRUPT_DB || iStart>=pPage->hdrOffset+6+pPage->childPtrSize );
1587 assert( CORRUPT_DB || iEnd <= pPage->pBt->usableSize );
1588 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
1589 assert( iSize>=4 ); /* Minimum cell size is 4 */
1590 assert( iStart<=iLast );
1591
1592 /* Overwrite deleted information with zeros when the secure_delete
1593 ** option is enabled */
1594 if( pPage->pBt->btsFlags & BTS_SECURE_DELETE ){
1595 memset(&data[iStart], 0, iSize);
1596 }
1597
1598 /* The list of freeblocks must be in ascending order. Find the
1599 ** spot on the list where iStart should be inserted.
1600 */
1601 hdr = pPage->hdrOffset;
1602 iPtr = hdr + 1;
1603 if( data[iPtr+1]==0 && data[iPtr]==0 ){
1604 iFreeBlk = 0; /* Shortcut for the case when the freelist is empty */
1605 }else{
1606 while( (iFreeBlk = get2byte(&data[iPtr]))<iStart ){
1607 if( iFreeBlk<iPtr+4 ){
1608 if( iFreeBlk==0 ) break;
1609 return SQLITE_CORRUPT_BKPT;
1610 }
1611 iPtr = iFreeBlk;
1612 }
1613 if( iFreeBlk>iLast ) return SQLITE_CORRUPT_BKPT;
1614 assert( iFreeBlk>iPtr || iFreeBlk==0 );
1615
1616 /* At this point:
1617 ** iFreeBlk: First freeblock after iStart, or zero if none
1618 ** iPtr: The address of a pointer to iFreeBlk
1619 **
1620 ** Check to see if iFreeBlk should be coalesced onto the end of iStart.
1621 */
1622 if( iFreeBlk && iEnd+3>=iFreeBlk ){
1623 nFrag = iFreeBlk - iEnd;
1624 if( iEnd>iFreeBlk ) return SQLITE_CORRUPT_BKPT;
1625 iEnd = iFreeBlk + get2byte(&data[iFreeBlk+2]);
1626 if( iEnd > pPage->pBt->usableSize ) return SQLITE_CORRUPT_BKPT;
1627 iSize = iEnd - iStart;
1628 iFreeBlk = get2byte(&data[iFreeBlk]);
1629 }
1630
1631 /* If iPtr is another freeblock (that is, if iPtr is not the freelist
1632 ** pointer in the page header) then check to see if iStart should be
1633 ** coalesced onto the end of iPtr.
1634 */
1635 if( iPtr>hdr+1 ){
1636 int iPtrEnd = iPtr + get2byte(&data[iPtr+2]);
1637 if( iPtrEnd+3>=iStart ){
1638 if( iPtrEnd>iStart ) return SQLITE_CORRUPT_BKPT;
1639 nFrag += iStart - iPtrEnd;
1640 iSize = iEnd - iPtr;
1641 iStart = iPtr;
1642 }
1643 }
1644 if( nFrag>data[hdr+7] ) return SQLITE_CORRUPT_BKPT;
1645 data[hdr+7] -= nFrag;
1646 }
1647 if( iStart==get2byte(&data[hdr+5]) ){
1648 /* The new freeblock is at the beginning of the cell content area,
1649 ** so just extend the cell content area rather than create another
1650 ** freelist entry */
1651 if( iPtr!=hdr+1 ) return SQLITE_CORRUPT_BKPT;
1652 put2byte(&data[hdr+1], iFreeBlk);
1653 put2byte(&data[hdr+5], iEnd);
1654 }else{
1655 /* Insert the new freeblock into the freelist */
1656 put2byte(&data[iPtr], iStart);
1657 put2byte(&data[iStart], iFreeBlk);
1658 put2byte(&data[iStart+2], iSize);
1659 }
1660 pPage->nFree += iOrigSize;
1661 return SQLITE_OK;
1662 }
1663
1664 /*
1665 ** Decode the flags byte (the first byte of the header) for a page
1666 ** and initialize fields of the MemPage structure accordingly.
1667 **
1668 ** Only the following combinations are supported. Anything different
1669 ** indicates a corrupt database files:
1670 **
1671 ** PTF_ZERODATA
1672 ** PTF_ZERODATA | PTF_LEAF
1673 ** PTF_LEAFDATA | PTF_INTKEY
1674 ** PTF_LEAFDATA | PTF_INTKEY | PTF_LEAF
1675 */
1676 static int decodeFlags(MemPage *pPage, int flagByte){
1677 BtShared *pBt; /* A copy of pPage->pBt */
1678
1679 assert( pPage->hdrOffset==(pPage->pgno==1 ? 100 : 0) );
1680 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
1681 pPage->leaf = (u8)(flagByte>>3); assert( PTF_LEAF == 1<<3 );
1682 flagByte &= ~PTF_LEAF;
1683 pPage->childPtrSize = 4-4*pPage->leaf;
1684 pPage->xCellSize = cellSizePtr;
1685 pBt = pPage->pBt;
1686 if( flagByte==(PTF_LEAFDATA | PTF_INTKEY) ){
1687 /* EVIDENCE-OF: R-07291-35328 A value of 5 (0x05) means the page is an
1688 ** interior table b-tree page. */
1689 assert( (PTF_LEAFDATA|PTF_INTKEY)==5 );
1690 /* EVIDENCE-OF: R-26900-09176 A value of 13 (0x0d) means the page is a
1691 ** leaf table b-tree page. */
1692 assert( (PTF_LEAFDATA|PTF_INTKEY|PTF_LEAF)==13 );
1693 pPage->intKey = 1;
1694 if( pPage->leaf ){
1695 pPage->intKeyLeaf = 1;
1696 pPage->xParseCell = btreeParseCellPtr;
1697 }else{
1698 pPage->intKeyLeaf = 0;
1699 pPage->xCellSize = cellSizePtrNoPayload;
1700 pPage->xParseCell = btreeParseCellPtrNoPayload;
1701 }
1702 pPage->maxLocal = pBt->maxLeaf;
1703 pPage->minLocal = pBt->minLeaf;
1704 }else if( flagByte==PTF_ZERODATA ){
1705 /* EVIDENCE-OF: R-43316-37308 A value of 2 (0x02) means the page is an
1706 ** interior index b-tree page. */
1707 assert( (PTF_ZERODATA)==2 );
1708 /* EVIDENCE-OF: R-59615-42828 A value of 10 (0x0a) means the page is a
1709 ** leaf index b-tree page. */
1710 assert( (PTF_ZERODATA|PTF_LEAF)==10 );
1711 pPage->intKey = 0;
1712 pPage->intKeyLeaf = 0;
1713 pPage->xParseCell = btreeParseCellPtrIndex;
1714 pPage->maxLocal = pBt->maxLocal;
1715 pPage->minLocal = pBt->minLocal;
1716 }else{
1717 /* EVIDENCE-OF: R-47608-56469 Any other value for the b-tree page type is
1718 ** an error. */
1719 return SQLITE_CORRUPT_BKPT;
1720 }
1721 pPage->max1bytePayload = pBt->max1bytePayload;
1722 return SQLITE_OK;
1723 }
1724
1725 /*
1726 ** Initialize the auxiliary information for a disk block.
1727 **
1728 ** Return SQLITE_OK on success. If we see that the page does
1729 ** not contain a well-formed database page, then return
1730 ** SQLITE_CORRUPT. Note that a return of SQLITE_OK does not
1731 ** guarantee that the page is well-formed. It only shows that
1732 ** we failed to detect any corruption.
1733 */
1734 static int btreeInitPage(MemPage *pPage){
1735
1736 assert( pPage->pBt!=0 );
1737 assert( pPage->pBt->db!=0 );
1738 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
1739 assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) );
1740 assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) );
1741 assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) );
1742
1743 if( !pPage->isInit ){
1744 int pc; /* Address of a freeblock within pPage->aData[] */
1745 u8 hdr; /* Offset to beginning of page header */
1746 u8 *data; /* Equal to pPage->aData */
1747 BtShared *pBt; /* The main btree structure */
1748 int usableSize; /* Amount of usable space on each page */
1749 u16 cellOffset; /* Offset from start of page to first cell pointer */
1750 int nFree; /* Number of unused bytes on the page */
1751 int top; /* First byte of the cell content area */
1752 int iCellFirst; /* First allowable cell or freeblock offset */
1753 int iCellLast; /* Last possible cell or freeblock offset */
1754
1755 pBt = pPage->pBt;
1756
1757 hdr = pPage->hdrOffset;
1758 data = pPage->aData;
1759 /* EVIDENCE-OF: R-28594-02890 The one-byte flag at offset 0 indicating
1760 ** the b-tree page type. */
1761 if( decodeFlags(pPage, data[hdr]) ) return SQLITE_CORRUPT_BKPT;
1762 assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
1763 pPage->maskPage = (u16)(pBt->pageSize - 1);
1764 pPage->nOverflow = 0;
1765 usableSize = pBt->usableSize;
1766 pPage->cellOffset = cellOffset = hdr + 8 + pPage->childPtrSize;
1767 pPage->aDataEnd = &data[usableSize];
1768 pPage->aCellIdx = &data[cellOffset];
1769 pPage->aDataOfst = &data[pPage->childPtrSize];
1770 /* EVIDENCE-OF: R-58015-48175 The two-byte integer at offset 5 designates
1771 ** the start of the cell content area. A zero value for this integer is
1772 ** interpreted as 65536. */
1773 top = get2byteNotZero(&data[hdr+5]);
1774 /* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the
1775 ** number of cells on the page. */
1776 pPage->nCell = get2byte(&data[hdr+3]);
1777 if( pPage->nCell>MX_CELL(pBt) ){
1778 /* To many cells for a single page. The page must be corrupt */
1779 return SQLITE_CORRUPT_BKPT;
1780 }
1781 testcase( pPage->nCell==MX_CELL(pBt) );
1782 /* EVIDENCE-OF: R-24089-57979 If a page contains no cells (which is only
1783 ** possible for a root page of a table that contains no rows) then the
1784 ** offset to the cell content area will equal the page size minus the
1785 ** bytes of reserved space. */
1786 assert( pPage->nCell>0 || top==usableSize || CORRUPT_DB );
1787
1788 /* A malformed database page might cause us to read past the end
1789 ** of page when parsing a cell.
1790 **
1791 ** The following block of code checks early to see if a cell extends
1792 ** past the end of a page boundary and causes SQLITE_CORRUPT to be
1793 ** returned if it does.
1794 */
1795 iCellFirst = cellOffset + 2*pPage->nCell;
1796 iCellLast = usableSize - 4;
1797 if( pBt->db->flags & SQLITE_CellSizeCk ){
1798 int i; /* Index into the cell pointer array */
1799 int sz; /* Size of a cell */
1800
1801 if( !pPage->leaf ) iCellLast--;
1802 for(i=0; i<pPage->nCell; i++){
1803 pc = get2byteAligned(&data[cellOffset+i*2]);
1804 testcase( pc==iCellFirst );
1805 testcase( pc==iCellLast );
1806 if( pc<iCellFirst || pc>iCellLast ){
1807 return SQLITE_CORRUPT_BKPT;
1808 }
1809 sz = pPage->xCellSize(pPage, &data[pc]);
1810 testcase( pc+sz==usableSize );
1811 if( pc+sz>usableSize ){
1812 return SQLITE_CORRUPT_BKPT;
1813 }
1814 }
1815 if( !pPage->leaf ) iCellLast++;
1816 }
1817
1818 /* Compute the total free space on the page
1819 ** EVIDENCE-OF: R-23588-34450 The two-byte integer at offset 1 gives the
1820 ** start of the first freeblock on the page, or is zero if there are no
1821 ** freeblocks. */
1822 pc = get2byte(&data[hdr+1]);
1823 nFree = data[hdr+7] + top; /* Init nFree to non-freeblock free space */
1824 if( pc>0 ){
1825 u32 next, size;
1826 if( pc<iCellFirst ){
1827 /* EVIDENCE-OF: R-55530-52930 In a well-formed b-tree page, there will
1828 ** always be at least one cell before the first freeblock.
1829 */
1830 return SQLITE_CORRUPT_BKPT;
1831 }
1832 while( 1 ){
1833 if( pc>iCellLast ){
1834 return SQLITE_CORRUPT_BKPT; /* Freeblock off the end of the page */
1835 }
1836 next = get2byte(&data[pc]);
1837 size = get2byte(&data[pc+2]);
1838 nFree = nFree + size;
1839 if( next<=pc+size+3 ) break;
1840 pc = next;
1841 }
1842 if( next>0 ){
1843 return SQLITE_CORRUPT_BKPT; /* Freeblock not in ascending order */
1844 }
1845 if( pc+size>(unsigned int)usableSize ){
1846 return SQLITE_CORRUPT_BKPT; /* Last freeblock extends past page end */
1847 }
1848 }
1849
1850 /* At this point, nFree contains the sum of the offset to the start
1851 ** of the cell-content area plus the number of free bytes within
1852 ** the cell-content area. If this is greater than the usable-size
1853 ** of the page, then the page must be corrupted. This check also
1854 ** serves to verify that the offset to the start of the cell-content
1855 ** area, according to the page header, lies within the page.
1856 */
1857 if( nFree>usableSize ){
1858 return SQLITE_CORRUPT_BKPT;
1859 }
1860 pPage->nFree = (u16)(nFree - iCellFirst);
1861 pPage->isInit = 1;
1862 }
1863 return SQLITE_OK;
1864 }
1865
1866 /*
1867 ** Set up a raw page so that it looks like a database page holding
1868 ** no entries.
1869 */
1870 static void zeroPage(MemPage *pPage, int flags){
1871 unsigned char *data = pPage->aData;
1872 BtShared *pBt = pPage->pBt;
1873 u8 hdr = pPage->hdrOffset;
1874 u16 first;
1875
1876 assert( sqlite3PagerPagenumber(pPage->pDbPage)==pPage->pgno );
1877 assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
1878 assert( sqlite3PagerGetData(pPage->pDbPage) == data );
1879 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
1880 assert( sqlite3_mutex_held(pBt->mutex) );
1881 if( pBt->btsFlags & BTS_SECURE_DELETE ){
1882 memset(&data[hdr], 0, pBt->usableSize - hdr);
1883 }
1884 data[hdr] = (char)flags;
1885 first = hdr + ((flags&PTF_LEAF)==0 ? 12 : 8);
1886 memset(&data[hdr+1], 0, 4);
1887 data[hdr+7] = 0;
1888 put2byte(&data[hdr+5], pBt->usableSize);
1889 pPage->nFree = (u16)(pBt->usableSize - first);
1890 decodeFlags(pPage, flags);
1891 pPage->cellOffset = first;
1892 pPage->aDataEnd = &data[pBt->usableSize];
1893 pPage->aCellIdx = &data[first];
1894 pPage->aDataOfst = &data[pPage->childPtrSize];
1895 pPage->nOverflow = 0;
1896 assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
1897 pPage->maskPage = (u16)(pBt->pageSize - 1);
1898 pPage->nCell = 0;
1899 pPage->isInit = 1;
1900 }
1901
1902
1903 /*
1904 ** Convert a DbPage obtained from the pager into a MemPage used by
1905 ** the btree layer.
1906 */
1907 static MemPage *btreePageFromDbPage(DbPage *pDbPage, Pgno pgno, BtShared *pBt){
1908 MemPage *pPage = (MemPage*)sqlite3PagerGetExtra(pDbPage);
1909 if( pgno!=pPage->pgno ){
1910 pPage->aData = sqlite3PagerGetData(pDbPage);
1911 pPage->pDbPage = pDbPage;
1912 pPage->pBt = pBt;
1913 pPage->pgno = pgno;
1914 pPage->hdrOffset = pgno==1 ? 100 : 0;
1915 }
1916 assert( pPage->aData==sqlite3PagerGetData(pDbPage) );
1917 return pPage;
1918 }
1919
1920 /*
1921 ** Get a page from the pager. Initialize the MemPage.pBt and
1922 ** MemPage.aData elements if needed. See also: btreeGetUnusedPage().
1923 **
1924 ** If the PAGER_GET_NOCONTENT flag is set, it means that we do not care
1925 ** about the content of the page at this time. So do not go to the disk
1926 ** to fetch the content. Just fill in the content with zeros for now.
1927 ** If in the future we call sqlite3PagerWrite() on this page, that
1928 ** means we have started to be concerned about content and the disk
1929 ** read should occur at that point.
1930 */
1931 static int btreeGetPage(
1932 BtShared *pBt, /* The btree */
1933 Pgno pgno, /* Number of the page to fetch */
1934 MemPage **ppPage, /* Return the page in this parameter */
1935 int flags /* PAGER_GET_NOCONTENT or PAGER_GET_READONLY */
1936 ){
1937 int rc;
1938 DbPage *pDbPage;
1939
1940 assert( flags==0 || flags==PAGER_GET_NOCONTENT || flags==PAGER_GET_READONLY );
1941 assert( sqlite3_mutex_held(pBt->mutex) );
1942 rc = sqlite3PagerGet(pBt->pPager, pgno, (DbPage**)&pDbPage, flags);
1943 if( rc ) return rc;
1944 *ppPage = btreePageFromDbPage(pDbPage, pgno, pBt);
1945 return SQLITE_OK;
1946 }
1947
1948 /*
1949 ** Retrieve a page from the pager cache. If the requested page is not
1950 ** already in the pager cache return NULL. Initialize the MemPage.pBt and
1951 ** MemPage.aData elements if needed.
1952 */
1953 static MemPage *btreePageLookup(BtShared *pBt, Pgno pgno){
1954 DbPage *pDbPage;
1955 assert( sqlite3_mutex_held(pBt->mutex) );
1956 pDbPage = sqlite3PagerLookup(pBt->pPager, pgno);
1957 if( pDbPage ){
1958 return btreePageFromDbPage(pDbPage, pgno, pBt);
1959 }
1960 return 0;
1961 }
1962
1963 /*
1964 ** Return the size of the database file in pages. If there is any kind of
1965 ** error, return ((unsigned int)-1).
1966 */
1967 static Pgno btreePagecount(BtShared *pBt){
1968 return pBt->nPage;
1969 }
1970 SQLITE_PRIVATE u32 sqlite3BtreeLastPage(Btree *p){
1971 assert( sqlite3BtreeHoldsMutex(p) );
1972 assert( ((p->pBt->nPage)&0x8000000)==0 );
1973 return btreePagecount(p->pBt);
1974 }
1975
1976 /*
1977 ** Get a page from the pager and initialize it.
1978 **
1979 ** If pCur!=0 then the page is being fetched as part of a moveToChild()
1980 ** call. Do additional sanity checking on the page in this case.
1981 ** And if the fetch fails, this routine must decrement pCur->iPage.
1982 **
1983 ** The page is fetched as read-write unless pCur is not NULL and is
1984 ** a read-only cursor.
1985 **
1986 ** If an error occurs, then *ppPage is undefined. It
1987 ** may remain unchanged, or it may be set to an invalid value.
1988 */
1989 static int getAndInitPage(
1990 BtShared *pBt, /* The database file */
1991 Pgno pgno, /* Number of the page to get */
1992 MemPage **ppPage, /* Write the page pointer here */
1993 BtCursor *pCur, /* Cursor to receive the page, or NULL */
1994 int bReadOnly /* True for a read-only page */
1995 ){
1996 int rc;
1997 DbPage *pDbPage;
1998 assert( sqlite3_mutex_held(pBt->mutex) );
1999 assert( pCur==0 || ppPage==&pCur->apPage[pCur->iPage] );
2000 assert( pCur==0 || bReadOnly==pCur->curPagerFlags );
2001 assert( pCur==0 || pCur->iPage>0 );
2002
2003 if( pgno>btreePagecount(pBt) ){
2004 rc = SQLITE_CORRUPT_BKPT;
2005 goto getAndInitPage_error;
2006 }
2007 rc = sqlite3PagerGet(pBt->pPager, pgno, (DbPage**)&pDbPage, bReadOnly);
2008 if( rc ){
2009 goto getAndInitPage_error;
2010 }
2011 *ppPage = (MemPage*)sqlite3PagerGetExtra(pDbPage);
2012 if( (*ppPage)->isInit==0 ){
2013 btreePageFromDbPage(pDbPage, pgno, pBt);
2014 rc = btreeInitPage(*ppPage);
2015 if( rc!=SQLITE_OK ){
2016 releasePage(*ppPage);
2017 goto getAndInitPage_error;
2018 }
2019 }
2020 assert( (*ppPage)->pgno==pgno );
2021 assert( (*ppPage)->aData==sqlite3PagerGetData(pDbPage) );
2022
2023 /* If obtaining a child page for a cursor, we must verify that the page is
2024 ** compatible with the root page. */
2025 if( pCur && ((*ppPage)->nCell<1 || (*ppPage)->intKey!=pCur->curIntKey) ){
2026 rc = SQLITE_CORRUPT_BKPT;
2027 releasePage(*ppPage);
2028 goto getAndInitPage_error;
2029 }
2030 return SQLITE_OK;
2031
2032 getAndInitPage_error:
2033 if( pCur ) pCur->iPage--;
2034 testcase( pgno==0 );
2035 assert( pgno!=0 || rc==SQLITE_CORRUPT );
2036 return rc;
2037 }
2038
2039 /*
2040 ** Release a MemPage. This should be called once for each prior
2041 ** call to btreeGetPage.
2042 */
2043 static void releasePageNotNull(MemPage *pPage){
2044 assert( pPage->aData );
2045 assert( pPage->pBt );
2046 assert( pPage->pDbPage!=0 );
2047 assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
2048 assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData );
2049 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
2050 sqlite3PagerUnrefNotNull(pPage->pDbPage);
2051 }
2052 static void releasePage(MemPage *pPage){
2053 if( pPage ) releasePageNotNull(pPage);
2054 }
2055
2056 /*
2057 ** Get an unused page.
2058 **
2059 ** This works just like btreeGetPage() with the addition:
2060 **
2061 ** * If the page is already in use for some other purpose, immediately
2062 ** release it and return an SQLITE_CURRUPT error.
2063 ** * Make sure the isInit flag is clear
2064 */
2065 static int btreeGetUnusedPage(
2066 BtShared *pBt, /* The btree */
2067 Pgno pgno, /* Number of the page to fetch */
2068 MemPage **ppPage, /* Return the page in this parameter */
2069 int flags /* PAGER_GET_NOCONTENT or PAGER_GET_READONLY */
2070 ){
2071 int rc = btreeGetPage(pBt, pgno, ppPage, flags);
2072 if( rc==SQLITE_OK ){
2073 if( sqlite3PagerPageRefcount((*ppPage)->pDbPage)>1 ){
2074 releasePage(*ppPage);
2075 *ppPage = 0;
2076 return SQLITE_CORRUPT_BKPT;
2077 }
2078 (*ppPage)->isInit = 0;
2079 }else{
2080 *ppPage = 0;
2081 }
2082 return rc;
2083 }
2084
2085
2086 /*
2087 ** During a rollback, when the pager reloads information into the cache
2088 ** so that the cache is restored to its original state at the start of
2089 ** the transaction, for each page restored this routine is called.
2090 **
2091 ** This routine needs to reset the extra data section at the end of the
2092 ** page to agree with the restored data.
2093 */
2094 static void pageReinit(DbPage *pData){
2095 MemPage *pPage;
2096 pPage = (MemPage *)sqlite3PagerGetExtra(pData);
2097 assert( sqlite3PagerPageRefcount(pData)>0 );
2098 if( pPage->isInit ){
2099 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
2100 pPage->isInit = 0;
2101 if( sqlite3PagerPageRefcount(pData)>1 ){
2102 /* pPage might not be a btree page; it might be an overflow page
2103 ** or ptrmap page or a free page. In those cases, the following
2104 ** call to btreeInitPage() will likely return SQLITE_CORRUPT.
2105 ** But no harm is done by this. And it is very important that
2106 ** btreeInitPage() be called on every btree page so we make
2107 ** the call for every page that comes in for re-initing. */
2108 btreeInitPage(pPage);
2109 }
2110 }
2111 }
2112
2113 /*
2114 ** Invoke the busy handler for a btree.
2115 */
2116 static int btreeInvokeBusyHandler(void *pArg){
2117 BtShared *pBt = (BtShared*)pArg;
2118 assert( pBt->db );
2119 assert( sqlite3_mutex_held(pBt->db->mutex) );
2120 return sqlite3InvokeBusyHandler(&pBt->db->busyHandler);
2121 }
2122
2123 /*
2124 ** Open a database file.
2125 **
2126 ** zFilename is the name of the database file. If zFilename is NULL
2127 ** then an ephemeral database is created. The ephemeral database might
2128 ** be exclusively in memory, or it might use a disk-based memory cache.
2129 ** Either way, the ephemeral database will be automatically deleted
2130 ** when sqlite3BtreeClose() is called.
2131 **
2132 ** If zFilename is ":memory:" then an in-memory database is created
2133 ** that is automatically destroyed when it is closed.
2134 **
2135 ** The "flags" parameter is a bitmask that might contain bits like
2136 ** BTREE_OMIT_JOURNAL and/or BTREE_MEMORY.
2137 **
2138 ** If the database is already opened in the same database connection
2139 ** and we are in shared cache mode, then the open will fail with an
2140 ** SQLITE_CONSTRAINT error. We cannot allow two or more BtShared
2141 ** objects in the same database connection since doing so will lead
2142 ** to problems with locking.
2143 */
2144 SQLITE_PRIVATE int sqlite3BtreeOpen(
2145 sqlite3_vfs *pVfs, /* VFS to use for this b-tree */
2146 const char *zFilename, /* Name of the file containing the BTree database */
2147 sqlite3 *db, /* Associated database handle */
2148 Btree **ppBtree, /* Pointer to new Btree object written here */
2149 int flags, /* Options */
2150 int vfsFlags /* Flags passed through to sqlite3_vfs.xOpen() */
2151 ){
2152 BtShared *pBt = 0; /* Shared part of btree structure */
2153 Btree *p; /* Handle to return */
2154 sqlite3_mutex *mutexOpen = 0; /* Prevents a race condition. Ticket #3537 */
2155 int rc = SQLITE_OK; /* Result code from this function */
2156 u8 nReserve; /* Byte of unused space on each page */
2157 unsigned char zDbHeader[100]; /* Database header content */
2158
2159 /* True if opening an ephemeral, temporary database */
2160 const int isTempDb = zFilename==0 || zFilename[0]==0;
2161
2162 /* Set the variable isMemdb to true for an in-memory database, or
2163 ** false for a file-based database.
2164 */
2165 #ifdef SQLITE_OMIT_MEMORYDB
2166 const int isMemdb = 0;
2167 #else
2168 const int isMemdb = (zFilename && strcmp(zFilename, ":memory:")==0)
2169 || (isTempDb && sqlite3TempInMemory(db))
2170 || (vfsFlags & SQLITE_OPEN_MEMORY)!=0;
2171 #endif
2172
2173 assert( db!=0 );
2174 assert( pVfs!=0 );
2175 assert( sqlite3_mutex_held(db->mutex) );
2176 assert( (flags&0xff)==flags ); /* flags fit in 8 bits */
2177
2178 /* Only a BTREE_SINGLE database can be BTREE_UNORDERED */
2179 assert( (flags & BTREE_UNORDERED)==0 || (flags & BTREE_SINGLE)!=0 );
2180
2181 /* A BTREE_SINGLE database is always a temporary and/or ephemeral */
2182 assert( (flags & BTREE_SINGLE)==0 || isTempDb );
2183
2184 if( isMemdb ){
2185 flags |= BTREE_MEMORY;
2186 }
2187 if( (vfsFlags & SQLITE_OPEN_MAIN_DB)!=0 && (isMemdb || isTempDb) ){
2188 vfsFlags = (vfsFlags & ~SQLITE_OPEN_MAIN_DB) | SQLITE_OPEN_TEMP_DB;
2189 }
2190 p = sqlite3MallocZero(sizeof(Btree));
2191 if( !p ){
2192 return SQLITE_NOMEM_BKPT;
2193 }
2194 p->inTrans = TRANS_NONE;
2195 p->db = db;
2196 #ifndef SQLITE_OMIT_SHARED_CACHE
2197 p->lock.pBtree = p;
2198 p->lock.iTable = 1;
2199 #endif
2200
2201 #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
2202 /*
2203 ** If this Btree is a candidate for shared cache, try to find an
2204 ** existing BtShared object that we can share with
2205 */
2206 if( isTempDb==0 && (isMemdb==0 || (vfsFlags&SQLITE_OPEN_URI)!=0) ){
2207 if( vfsFlags & SQLITE_OPEN_SHAREDCACHE ){
2208 int nFilename = sqlite3Strlen30(zFilename)+1;
2209 int nFullPathname = pVfs->mxPathname+1;
2210 char *zFullPathname = sqlite3Malloc(MAX(nFullPathname,nFilename));
2211 MUTEX_LOGIC( sqlite3_mutex *mutexShared; )
2212
2213 p->sharable = 1;
2214 if( !zFullPathname ){
2215 sqlite3_free(p);
2216 return SQLITE_NOMEM_BKPT;
2217 }
2218 if( isMemdb ){
2219 memcpy(zFullPathname, zFilename, nFilename);
2220 }else{
2221 rc = sqlite3OsFullPathname(pVfs, zFilename,
2222 nFullPathname, zFullPathname);
2223 if( rc ){
2224 sqlite3_free(zFullPathname);
2225 sqlite3_free(p);
2226 return rc;
2227 }
2228 }
2229 #if SQLITE_THREADSAFE
2230 mutexOpen = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_OPEN);
2231 sqlite3_mutex_enter(mutexOpen);
2232 mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
2233 sqlite3_mutex_enter(mutexShared);
2234 #endif
2235 for(pBt=GLOBAL(BtShared*,sqlite3SharedCacheList); pBt; pBt=pBt->pNext){
2236 assert( pBt->nRef>0 );
2237 if( 0==strcmp(zFullPathname, sqlite3PagerFilename(pBt->pPager, 0))
2238 && sqlite3PagerVfs(pBt->pPager)==pVfs ){
2239 int iDb;
2240 for(iDb=db->nDb-1; iDb>=0; iDb--){
2241 Btree *pExisting = db->aDb[iDb].pBt;
2242 if( pExisting && pExisting->pBt==pBt ){
2243 sqlite3_mutex_leave(mutexShared);
2244 sqlite3_mutex_leave(mutexOpen);
2245 sqlite3_free(zFullPathname);
2246 sqlite3_free(p);
2247 return SQLITE_CONSTRAINT;
2248 }
2249 }
2250 p->pBt = pBt;
2251 pBt->nRef++;
2252 break;
2253 }
2254 }
2255 sqlite3_mutex_leave(mutexShared);
2256 sqlite3_free(zFullPathname);
2257 }
2258 #ifdef SQLITE_DEBUG
2259 else{
2260 /* In debug mode, we mark all persistent databases as sharable
2261 ** even when they are not. This exercises the locking code and
2262 ** gives more opportunity for asserts(sqlite3_mutex_held())
2263 ** statements to find locking problems.
2264 */
2265 p->sharable = 1;
2266 }
2267 #endif
2268 }
2269 #endif
2270 if( pBt==0 ){
2271 /*
2272 ** The following asserts make sure that structures used by the btree are
2273 ** the right size. This is to guard against size changes that result
2274 ** when compiling on a different architecture.
2275 */
2276 assert( sizeof(i64)==8 );
2277 assert( sizeof(u64)==8 );
2278 assert( sizeof(u32)==4 );
2279 assert( sizeof(u16)==2 );
2280 assert( sizeof(Pgno)==4 );
2281
2282 pBt = sqlite3MallocZero( sizeof(*pBt) );
2283 if( pBt==0 ){
2284 rc = SQLITE_NOMEM_BKPT;
2285 goto btree_open_out;
2286 }
2287 rc = sqlite3PagerOpen(pVfs, &pBt->pPager, zFilename,
2288 sizeof(MemPage), flags, vfsFlags, pageReinit);
2289 if( rc==SQLITE_OK ){
2290 sqlite3PagerSetMmapLimit(pBt->pPager, db->szMmap);
2291 rc = sqlite3PagerReadFileheader(pBt->pPager,sizeof(zDbHeader),zDbHeader);
2292 }
2293 if( rc!=SQLITE_OK ){
2294 goto btree_open_out;
2295 }
2296 pBt->openFlags = (u8)flags;
2297 pBt->db = db;
2298 sqlite3PagerSetBusyhandler(pBt->pPager, btreeInvokeBusyHandler, pBt);
2299 p->pBt = pBt;
2300
2301 pBt->pCursor = 0;
2302 pBt->pPage1 = 0;
2303 if( sqlite3PagerIsreadonly(pBt->pPager) ) pBt->btsFlags |= BTS_READ_ONLY;
2304 #ifdef SQLITE_SECURE_DELETE
2305 pBt->btsFlags |= BTS_SECURE_DELETE;
2306 #endif
2307 /* EVIDENCE-OF: R-51873-39618 The page size for a database file is
2308 ** determined by the 2-byte integer located at an offset of 16 bytes from
2309 ** the beginning of the database file. */
2310 pBt->pageSize = (zDbHeader[16]<<8) | (zDbHeader[17]<<16);
2311 if( pBt->pageSize<512 || pBt->pageSize>SQLITE_MAX_PAGE_SIZE
2312 || ((pBt->pageSize-1)&pBt->pageSize)!=0 ){
2313 pBt->pageSize = 0;
2314 #ifndef SQLITE_OMIT_AUTOVACUUM
2315 /* If the magic name ":memory:" will create an in-memory database, then
2316 ** leave the autoVacuum mode at 0 (do not auto-vacuum), even if
2317 ** SQLITE_DEFAULT_AUTOVACUUM is true. On the other hand, if
2318 ** SQLITE_OMIT_MEMORYDB has been defined, then ":memory:" is just a
2319 ** regular file-name. In this case the auto-vacuum applies as per normal.
2320 */
2321 if( zFilename && !isMemdb ){
2322 pBt->autoVacuum = (SQLITE_DEFAULT_AUTOVACUUM ? 1 : 0);
2323 pBt->incrVacuum = (SQLITE_DEFAULT_AUTOVACUUM==2 ? 1 : 0);
2324 }
2325 #endif
2326 nReserve = 0;
2327 }else{
2328 /* EVIDENCE-OF: R-37497-42412 The size of the reserved region is
2329 ** determined by the one-byte unsigned integer found at an offset of 20
2330 ** into the database file header. */
2331 nReserve = zDbHeader[20];
2332 pBt->btsFlags |= BTS_PAGESIZE_FIXED;
2333 #ifndef SQLITE_OMIT_AUTOVACUUM
2334 pBt->autoVacuum = (get4byte(&zDbHeader[36 + 4*4])?1:0);
2335 pBt->incrVacuum = (get4byte(&zDbHeader[36 + 7*4])?1:0);
2336 #endif
2337 }
2338 rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve);
2339 if( rc ) goto btree_open_out;
2340 pBt->usableSize = pBt->pageSize - nReserve;
2341 assert( (pBt->pageSize & 7)==0 ); /* 8-byte alignment of pageSize */
2342
2343 #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
2344 /* Add the new BtShared object to the linked list sharable BtShareds.
2345 */
2346 pBt->nRef = 1;
2347 if( p->sharable ){
2348 MUTEX_LOGIC( sqlite3_mutex *mutexShared; )
2349 MUTEX_LOGIC( mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);)
2350 if( SQLITE_THREADSAFE && sqlite3GlobalConfig.bCoreMutex ){
2351 pBt->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_FAST);
2352 if( pBt->mutex==0 ){
2353 rc = SQLITE_NOMEM_BKPT;
2354 goto btree_open_out;
2355 }
2356 }
2357 sqlite3_mutex_enter(mutexShared);
2358 pBt->pNext = GLOBAL(BtShared*,sqlite3SharedCacheList);
2359 GLOBAL(BtShared*,sqlite3SharedCacheList) = pBt;
2360 sqlite3_mutex_leave(mutexShared);
2361 }
2362 #endif
2363 }
2364
2365 #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
2366 /* If the new Btree uses a sharable pBtShared, then link the new
2367 ** Btree into the list of all sharable Btrees for the same connection.
2368 ** The list is kept in ascending order by pBt address.
2369 */
2370 if( p->sharable ){
2371 int i;
2372 Btree *pSib;
2373 for(i=0; i<db->nDb; i++){
2374 if( (pSib = db->aDb[i].pBt)!=0 && pSib->sharable ){
2375 while( pSib->pPrev ){ pSib = pSib->pPrev; }
2376 if( (uptr)p->pBt<(uptr)pSib->pBt ){
2377 p->pNext = pSib;
2378 p->pPrev = 0;
2379 pSib->pPrev = p;
2380 }else{
2381 while( pSib->pNext && (uptr)pSib->pNext->pBt<(uptr)p->pBt ){
2382 pSib = pSib->pNext;
2383 }
2384 p->pNext = pSib->pNext;
2385 p->pPrev = pSib;
2386 if( p->pNext ){
2387 p->pNext->pPrev = p;
2388 }
2389 pSib->pNext = p;
2390 }
2391 break;
2392 }
2393 }
2394 }
2395 #endif
2396 *ppBtree = p;
2397
2398 btree_open_out:
2399 if( rc!=SQLITE_OK ){
2400 if( pBt && pBt->pPager ){
2401 sqlite3PagerClose(pBt->pPager, 0);
2402 }
2403 sqlite3_free(pBt);
2404 sqlite3_free(p);
2405 *ppBtree = 0;
2406 }else{
2407 sqlite3_file *pFile;
2408
2409 /* If the B-Tree was successfully opened, set the pager-cache size to the
2410 ** default value. Except, when opening on an existing shared pager-cache,
2411 ** do not change the pager-cache size.
2412 */
2413 if( sqlite3BtreeSchema(p, 0, 0)==0 ){
2414 sqlite3PagerSetCachesize(p->pBt->pPager, SQLITE_DEFAULT_CACHE_SIZE);
2415 }
2416
2417 pFile = sqlite3PagerFile(pBt->pPager);
2418 if( pFile->pMethods ){
2419 sqlite3OsFileControlHint(pFile, SQLITE_FCNTL_PDB, (void*)&pBt->db);
2420 }
2421 }
2422 if( mutexOpen ){
2423 assert( sqlite3_mutex_held(mutexOpen) );
2424 sqlite3_mutex_leave(mutexOpen);
2425 }
2426 assert( rc!=SQLITE_OK || sqlite3BtreeConnectionCount(*ppBtree)>0 );
2427 return rc;
2428 }
2429
2430 /*
2431 ** Decrement the BtShared.nRef counter. When it reaches zero,
2432 ** remove the BtShared structure from the sharing list. Return
2433 ** true if the BtShared.nRef counter reaches zero and return
2434 ** false if it is still positive.
2435 */
2436 static int removeFromSharingList(BtShared *pBt){
2437 #ifndef SQLITE_OMIT_SHARED_CACHE
2438 MUTEX_LOGIC( sqlite3_mutex *pMaster; )
2439 BtShared *pList;
2440 int removed = 0;
2441
2442 assert( sqlite3_mutex_notheld(pBt->mutex) );
2443 MUTEX_LOGIC( pMaster = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); )
2444 sqlite3_mutex_enter(pMaster);
2445 pBt->nRef--;
2446 if( pBt->nRef<=0 ){
2447 if( GLOBAL(BtShared*,sqlite3SharedCacheList)==pBt ){
2448 GLOBAL(BtShared*,sqlite3SharedCacheList) = pBt->pNext;
2449 }else{
2450 pList = GLOBAL(BtShared*,sqlite3SharedCacheList);
2451 while( ALWAYS(pList) && pList->pNext!=pBt ){
2452 pList=pList->pNext;
2453 }
2454 if( ALWAYS(pList) ){
2455 pList->pNext = pBt->pNext;
2456 }
2457 }
2458 if( SQLITE_THREADSAFE ){
2459 sqlite3_mutex_free(pBt->mutex);
2460 }
2461 removed = 1;
2462 }
2463 sqlite3_mutex_leave(pMaster);
2464 return removed;
2465 #else
2466 return 1;
2467 #endif
2468 }
2469
2470 /*
2471 ** Make sure pBt->pTmpSpace points to an allocation of
2472 ** MX_CELL_SIZE(pBt) bytes with a 4-byte prefix for a left-child
2473 ** pointer.
2474 */
2475 static void allocateTempSpace(BtShared *pBt){
2476 if( !pBt->pTmpSpace ){
2477 pBt->pTmpSpace = sqlite3PageMalloc( pBt->pageSize );
2478
2479 /* One of the uses of pBt->pTmpSpace is to format cells before
2480 ** inserting them into a leaf page (function fillInCell()). If
2481 ** a cell is less than 4 bytes in size, it is rounded up to 4 bytes
2482 ** by the various routines that manipulate binary cells. Which
2483 ** can mean that fillInCell() only initializes the first 2 or 3
2484 ** bytes of pTmpSpace, but that the first 4 bytes are copied from
2485 ** it into a database page. This is not actually a problem, but it
2486 ** does cause a valgrind error when the 1 or 2 bytes of unitialized
2487 ** data is passed to system call write(). So to avoid this error,
2488 ** zero the first 4 bytes of temp space here.
2489 **
2490 ** Also: Provide four bytes of initialized space before the
2491 ** beginning of pTmpSpace as an area available to prepend the
2492 ** left-child pointer to the beginning of a cell.
2493 */
2494 if( pBt->pTmpSpace ){
2495 memset(pBt->pTmpSpace, 0, 8);
2496 pBt->pTmpSpace += 4;
2497 }
2498 }
2499 }
2500
2501 /*
2502 ** Free the pBt->pTmpSpace allocation
2503 */
2504 static void freeTempSpace(BtShared *pBt){
2505 if( pBt->pTmpSpace ){
2506 pBt->pTmpSpace -= 4;
2507 sqlite3PageFree(pBt->pTmpSpace);
2508 pBt->pTmpSpace = 0;
2509 }
2510 }
2511
2512 /*
2513 ** Close an open database and invalidate all cursors.
2514 */
2515 SQLITE_PRIVATE int sqlite3BtreeClose(Btree *p){
2516 BtShared *pBt = p->pBt;
2517 BtCursor *pCur;
2518
2519 /* Close all cursors opened via this handle. */
2520 assert( sqlite3_mutex_held(p->db->mutex) );
2521 sqlite3BtreeEnter(p);
2522 pCur = pBt->pCursor;
2523 while( pCur ){
2524 BtCursor *pTmp = pCur;
2525 pCur = pCur->pNext;
2526 if( pTmp->pBtree==p ){
2527 sqlite3BtreeCloseCursor(pTmp);
2528 }
2529 }
2530
2531 /* Rollback any active transaction and free the handle structure.
2532 ** The call to sqlite3BtreeRollback() drops any table-locks held by
2533 ** this handle.
2534 */
2535 sqlite3BtreeRollback(p, SQLITE_OK, 0);
2536 sqlite3BtreeLeave(p);
2537
2538 /* If there are still other outstanding references to the shared-btree
2539 ** structure, return now. The remainder of this procedure cleans
2540 ** up the shared-btree.
2541 */
2542 assert( p->wantToLock==0 && p->locked==0 );
2543 if( !p->sharable || removeFromSharingList(pBt) ){
2544 /* The pBt is no longer on the sharing list, so we can access
2545 ** it without having to hold the mutex.
2546 **
2547 ** Clean out and delete the BtShared object.
2548 */
2549 assert( !pBt->pCursor );
2550 sqlite3PagerClose(pBt->pPager, p->db);
2551 if( pBt->xFreeSchema && pBt->pSchema ){
2552 pBt->xFreeSchema(pBt->pSchema);
2553 }
2554 sqlite3DbFree(0, pBt->pSchema);
2555 freeTempSpace(pBt);
2556 sqlite3_free(pBt);
2557 }
2558
2559 #ifndef SQLITE_OMIT_SHARED_CACHE
2560 assert( p->wantToLock==0 );
2561 assert( p->locked==0 );
2562 if( p->pPrev ) p->pPrev->pNext = p->pNext;
2563 if( p->pNext ) p->pNext->pPrev = p->pPrev;
2564 #endif
2565
2566 sqlite3_free(p);
2567 return SQLITE_OK;
2568 }
2569
2570 /*
2571 ** Change the "soft" limit on the number of pages in the cache.
2572 ** Unused and unmodified pages will be recycled when the number of
2573 ** pages in the cache exceeds this soft limit. But the size of the
2574 ** cache is allowed to grow larger than this limit if it contains
2575 ** dirty pages or pages still in active use.
2576 */
2577 SQLITE_PRIVATE int sqlite3BtreeSetCacheSize(Btree *p, int mxPage){
2578 BtShared *pBt = p->pBt;
2579 assert( sqlite3_mutex_held(p->db->mutex) );
2580 sqlite3BtreeEnter(p);
2581 sqlite3PagerSetCachesize(pBt->pPager, mxPage);
2582 sqlite3BtreeLeave(p);
2583 return SQLITE_OK;
2584 }
2585
2586 /*
2587 ** Change the "spill" limit on the number of pages in the cache.
2588 ** If the number of pages exceeds this limit during a write transaction,
2589 ** the pager might attempt to "spill" pages to the journal early in
2590 ** order to free up memory.
2591 **
2592 ** The value returned is the current spill size. If zero is passed
2593 ** as an argument, no changes are made to the spill size setting, so
2594 ** using mxPage of 0 is a way to query the current spill size.
2595 */
2596 SQLITE_PRIVATE int sqlite3BtreeSetSpillSize(Btree *p, int mxPage){
2597 BtShared *pBt = p->pBt;
2598 int res;
2599 assert( sqlite3_mutex_held(p->db->mutex) );
2600 sqlite3BtreeEnter(p);
2601 res = sqlite3PagerSetSpillsize(pBt->pPager, mxPage);
2602 sqlite3BtreeLeave(p);
2603 return res;
2604 }
2605
2606 #if SQLITE_MAX_MMAP_SIZE>0
2607 /*
2608 ** Change the limit on the amount of the database file that may be
2609 ** memory mapped.
2610 */
2611 SQLITE_PRIVATE int sqlite3BtreeSetMmapLimit(Btree *p, sqlite3_int64 szMmap){
2612 BtShared *pBt = p->pBt;
2613 assert( sqlite3_mutex_held(p->db->mutex) );
2614 sqlite3BtreeEnter(p);
2615 sqlite3PagerSetMmapLimit(pBt->pPager, szMmap);
2616 sqlite3BtreeLeave(p);
2617 return SQLITE_OK;
2618 }
2619 #endif /* SQLITE_MAX_MMAP_SIZE>0 */
2620
2621 /*
2622 ** Change the way data is synced to disk in order to increase or decrease
2623 ** how well the database resists damage due to OS crashes and power
2624 ** failures. Level 1 is the same as asynchronous (no syncs() occur and
2625 ** there is a high probability of damage) Level 2 is the default. There
2626 ** is a very low but non-zero probability of damage. Level 3 reduces the
2627 ** probability of damage to near zero but with a write performance reduction.
2628 */
2629 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
2630 SQLITE_PRIVATE int sqlite3BtreeSetPagerFlags(
2631 Btree *p, /* The btree to set the safety level on */
2632 unsigned pgFlags /* Various PAGER_* flags */
2633 ){
2634 BtShared *pBt = p->pBt;
2635 assert( sqlite3_mutex_held(p->db->mutex) );
2636 sqlite3BtreeEnter(p);
2637 sqlite3PagerSetFlags(pBt->pPager, pgFlags);
2638 sqlite3BtreeLeave(p);
2639 return SQLITE_OK;
2640 }
2641 #endif
2642
2643 /*
2644 ** Change the default pages size and the number of reserved bytes per page.
2645 ** Or, if the page size has already been fixed, return SQLITE_READONLY
2646 ** without changing anything.
2647 **
2648 ** The page size must be a power of 2 between 512 and 65536. If the page
2649 ** size supplied does not meet this constraint then the page size is not
2650 ** changed.
2651 **
2652 ** Page sizes are constrained to be a power of two so that the region
2653 ** of the database file used for locking (beginning at PENDING_BYTE,
2654 ** the first byte past the 1GB boundary, 0x40000000) needs to occur
2655 ** at the beginning of a page.
2656 **
2657 ** If parameter nReserve is less than zero, then the number of reserved
2658 ** bytes per page is left unchanged.
2659 **
2660 ** If the iFix!=0 then the BTS_PAGESIZE_FIXED flag is set so that the page size
2661 ** and autovacuum mode can no longer be changed.
2662 */
2663 SQLITE_PRIVATE int sqlite3BtreeSetPageSize(Btree *p, int pageSize, int nReserve, int iFix){
2664 int rc = SQLITE_OK;
2665 BtShared *pBt = p->pBt;
2666 assert( nReserve>=-1 && nReserve<=255 );
2667 sqlite3BtreeEnter(p);
2668 #if SQLITE_HAS_CODEC
2669 if( nReserve>pBt->optimalReserve ) pBt->optimalReserve = (u8)nReserve;
2670 #endif
2671 if( pBt->btsFlags & BTS_PAGESIZE_FIXED ){
2672 sqlite3BtreeLeave(p);
2673 return SQLITE_READONLY;
2674 }
2675 if( nReserve<0 ){
2676 nReserve = pBt->pageSize - pBt->usableSize;
2677 }
2678 assert( nReserve>=0 && nReserve<=255 );
2679 if( pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE &&
2680 ((pageSize-1)&pageSize)==0 ){
2681 assert( (pageSize & 7)==0 );
2682 assert( !pBt->pCursor );
2683 pBt->pageSize = (u32)pageSize;
2684 freeTempSpace(pBt);
2685 }
2686 rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve);
2687 pBt->usableSize = pBt->pageSize - (u16)nReserve;
2688 if( iFix ) pBt->btsFlags |= BTS_PAGESIZE_FIXED;
2689 sqlite3BtreeLeave(p);
2690 return rc;
2691 }
2692
2693 /*
2694 ** Return the currently defined page size
2695 */
2696 SQLITE_PRIVATE int sqlite3BtreeGetPageSize(Btree *p){
2697 return p->pBt->pageSize;
2698 }
2699
2700 /*
2701 ** This function is similar to sqlite3BtreeGetReserve(), except that it
2702 ** may only be called if it is guaranteed that the b-tree mutex is already
2703 ** held.
2704 **
2705 ** This is useful in one special case in the backup API code where it is
2706 ** known that the shared b-tree mutex is held, but the mutex on the
2707 ** database handle that owns *p is not. In this case if sqlite3BtreeEnter()
2708 ** were to be called, it might collide with some other operation on the
2709 ** database handle that owns *p, causing undefined behavior.
2710 */
2711 SQLITE_PRIVATE int sqlite3BtreeGetReserveNoMutex(Btree *p){
2712 int n;
2713 assert( sqlite3_mutex_held(p->pBt->mutex) );
2714 n = p->pBt->pageSize - p->pBt->usableSize;
2715 return n;
2716 }
2717
2718 /*
2719 ** Return the number of bytes of space at the end of every page that
2720 ** are intentually left unused. This is the "reserved" space that is
2721 ** sometimes used by extensions.
2722 **
2723 ** If SQLITE_HAS_MUTEX is defined then the number returned is the
2724 ** greater of the current reserved space and the maximum requested
2725 ** reserve space.
2726 */
2727 SQLITE_PRIVATE int sqlite3BtreeGetOptimalReserve(Btree *p){
2728 int n;
2729 sqlite3BtreeEnter(p);
2730 n = sqlite3BtreeGetReserveNoMutex(p);
2731 #ifdef SQLITE_HAS_CODEC
2732 if( n<p->pBt->optimalReserve ) n = p->pBt->optimalReserve;
2733 #endif
2734 sqlite3BtreeLeave(p);
2735 return n;
2736 }
2737
2738
2739 /*
2740 ** Set the maximum page count for a database if mxPage is positive.
2741 ** No changes are made if mxPage is 0 or negative.
2742 ** Regardless of the value of mxPage, return the maximum page count.
2743 */
2744 SQLITE_PRIVATE int sqlite3BtreeMaxPageCount(Btree *p, int mxPage){
2745 int n;
2746 sqlite3BtreeEnter(p);
2747 n = sqlite3PagerMaxPageCount(p->pBt->pPager, mxPage);
2748 sqlite3BtreeLeave(p);
2749 return n;
2750 }
2751
2752 /*
2753 ** Set the BTS_SECURE_DELETE flag if newFlag is 0 or 1. If newFlag is -1,
2754 ** then make no changes. Always return the value of the BTS_SECURE_DELETE
2755 ** setting after the change.
2756 */
2757 SQLITE_PRIVATE int sqlite3BtreeSecureDelete(Btree *p, int newFlag){
2758 int b;
2759 if( p==0 ) return 0;
2760 sqlite3BtreeEnter(p);
2761 if( newFlag>=0 ){
2762 p->pBt->btsFlags &= ~BTS_SECURE_DELETE;
2763 if( newFlag ) p->pBt->btsFlags |= BTS_SECURE_DELETE;
2764 }
2765 b = (p->pBt->btsFlags & BTS_SECURE_DELETE)!=0;
2766 sqlite3BtreeLeave(p);
2767 return b;
2768 }
2769
2770 /*
2771 ** Change the 'auto-vacuum' property of the database. If the 'autoVacuum'
2772 ** parameter is non-zero, then auto-vacuum mode is enabled. If zero, it
2773 ** is disabled. The default value for the auto-vacuum property is
2774 ** determined by the SQLITE_DEFAULT_AUTOVACUUM macro.
2775 */
2776 SQLITE_PRIVATE int sqlite3BtreeSetAutoVacuum(Btree *p, int autoVacuum){
2777 #ifdef SQLITE_OMIT_AUTOVACUUM
2778 return SQLITE_READONLY;
2779 #else
2780 BtShared *pBt = p->pBt;
2781 int rc = SQLITE_OK;
2782 u8 av = (u8)autoVacuum;
2783
2784 sqlite3BtreeEnter(p);
2785 if( (pBt->btsFlags & BTS_PAGESIZE_FIXED)!=0 && (av ?1:0)!=pBt->autoVacuum ){
2786 rc = SQLITE_READONLY;
2787 }else{
2788 pBt->autoVacuum = av ?1:0;
2789 pBt->incrVacuum = av==2 ?1:0;
2790 }
2791 sqlite3BtreeLeave(p);
2792 return rc;
2793 #endif
2794 }
2795
2796 /*
2797 ** Return the value of the 'auto-vacuum' property. If auto-vacuum is
2798 ** enabled 1 is returned. Otherwise 0.
2799 */
2800 SQLITE_PRIVATE int sqlite3BtreeGetAutoVacuum(Btree *p){
2801 #ifdef SQLITE_OMIT_AUTOVACUUM
2802 return BTREE_AUTOVACUUM_NONE;
2803 #else
2804 int rc;
2805 sqlite3BtreeEnter(p);
2806 rc = (
2807 (!p->pBt->autoVacuum)?BTREE_AUTOVACUUM_NONE:
2808 (!p->pBt->incrVacuum)?BTREE_AUTOVACUUM_FULL:
2809 BTREE_AUTOVACUUM_INCR
2810 );
2811 sqlite3BtreeLeave(p);
2812 return rc;
2813 #endif
2814 }
2815
2816
2817 /*
2818 ** Get a reference to pPage1 of the database file. This will
2819 ** also acquire a readlock on that file.
2820 **
2821 ** SQLITE_OK is returned on success. If the file is not a
2822 ** well-formed database file, then SQLITE_CORRUPT is returned.
2823 ** SQLITE_BUSY is returned if the database is locked. SQLITE_NOMEM
2824 ** is returned if we run out of memory.
2825 */
2826 static int lockBtree(BtShared *pBt){
2827 int rc; /* Result code from subfunctions */
2828 MemPage *pPage1; /* Page 1 of the database file */
2829 int nPage; /* Number of pages in the database */
2830 int nPageFile = 0; /* Number of pages in the database file */
2831 int nPageHeader; /* Number of pages in the database according to hdr */
2832
2833 assert( sqlite3_mutex_held(pBt->mutex) );
2834 assert( pBt->pPage1==0 );
2835 rc = sqlite3PagerSharedLock(pBt->pPager);
2836 if( rc!=SQLITE_OK ) return rc;
2837 rc = btreeGetPage(pBt, 1, &pPage1, 0);
2838 if( rc!=SQLITE_OK ) return rc;
2839
2840 /* Do some checking to help insure the file we opened really is
2841 ** a valid database file.
2842 */
2843 nPage = nPageHeader = get4byte(28+(u8*)pPage1->aData);
2844 sqlite3PagerPagecount(pBt->pPager, &nPageFile);
2845 if( nPage==0 || memcmp(24+(u8*)pPage1->aData, 92+(u8*)pPage1->aData,4)!=0 ){
2846 nPage = nPageFile;
2847 }
2848 if( nPage>0 ){
2849 u32 pageSize;
2850 u32 usableSize;
2851 u8 *page1 = pPage1->aData;
2852 rc = SQLITE_NOTADB;
2853 /* EVIDENCE-OF: R-43737-39999 Every valid SQLite database file begins
2854 ** with the following 16 bytes (in hex): 53 51 4c 69 74 65 20 66 6f 72 6d
2855 ** 61 74 20 33 00. */
2856 if( memcmp(page1, zMagicHeader, 16)!=0 ){
2857 goto page1_init_failed;
2858 }
2859
2860 #ifdef SQLITE_OMIT_WAL
2861 if( page1[18]>1 ){
2862 pBt->btsFlags |= BTS_READ_ONLY;
2863 }
2864 if( page1[19]>1 ){
2865 goto page1_init_failed;
2866 }
2867 #else
2868 if( page1[18]>2 ){
2869 pBt->btsFlags |= BTS_READ_ONLY;
2870 }
2871 if( page1[19]>2 ){
2872 goto page1_init_failed;
2873 }
2874
2875 /* If the write version is set to 2, this database should be accessed
2876 ** in WAL mode. If the log is not already open, open it now. Then
2877 ** return SQLITE_OK and return without populating BtShared.pPage1.
2878 ** The caller detects this and calls this function again. This is
2879 ** required as the version of page 1 currently in the page1 buffer
2880 ** may not be the latest version - there may be a newer one in the log
2881 ** file.
2882 */
2883 if( page1[19]==2 && (pBt->btsFlags & BTS_NO_WAL)==0 ){
2884 int isOpen = 0;
2885 rc = sqlite3PagerOpenWal(pBt->pPager, &isOpen);
2886 if( rc!=SQLITE_OK ){
2887 goto page1_init_failed;
2888 }else{
2889 #if SQLITE_DEFAULT_SYNCHRONOUS!=SQLITE_DEFAULT_WAL_SYNCHRONOUS
2890 sqlite3 *db;
2891 Db *pDb;
2892 if( (db=pBt->db)!=0 && (pDb=db->aDb)!=0 ){
2893 while( pDb->pBt==0 || pDb->pBt->pBt!=pBt ){ pDb++; }
2894 if( pDb->bSyncSet==0
2895 && pDb->safety_level==SQLITE_DEFAULT_SYNCHRONOUS+1
2896 ){
2897 pDb->safety_level = SQLITE_DEFAULT_WAL_SYNCHRONOUS+1;
2898 sqlite3PagerSetFlags(pBt->pPager,
2899 pDb->safety_level | (db->flags & PAGER_FLAGS_MASK));
2900 }
2901 }
2902 #endif
2903 if( isOpen==0 ){
2904 releasePage(pPage1);
2905 return SQLITE_OK;
2906 }
2907 }
2908 rc = SQLITE_NOTADB;
2909 }
2910 #endif
2911
2912 /* EVIDENCE-OF: R-15465-20813 The maximum and minimum embedded payload
2913 ** fractions and the leaf payload fraction values must be 64, 32, and 32.
2914 **
2915 ** The original design allowed these amounts to vary, but as of
2916 ** version 3.6.0, we require them to be fixed.
2917 */
2918 if( memcmp(&page1[21], "\100\040\040",3)!=0 ){
2919 goto page1_init_failed;
2920 }
2921 /* EVIDENCE-OF: R-51873-39618 The page size for a database file is
2922 ** determined by the 2-byte integer located at an offset of 16 bytes from
2923 ** the beginning of the database file. */
2924 pageSize = (page1[16]<<8) | (page1[17]<<16);
2925 /* EVIDENCE-OF: R-25008-21688 The size of a page is a power of two
2926 ** between 512 and 65536 inclusive. */
2927 if( ((pageSize-1)&pageSize)!=0
2928 || pageSize>SQLITE_MAX_PAGE_SIZE
2929 || pageSize<=256
2930 ){
2931 goto page1_init_failed;
2932 }
2933 assert( (pageSize & 7)==0 );
2934 /* EVIDENCE-OF: R-59310-51205 The "reserved space" size in the 1-byte
2935 ** integer at offset 20 is the number of bytes of space at the end of
2936 ** each page to reserve for extensions.
2937 **
2938 ** EVIDENCE-OF: R-37497-42412 The size of the reserved region is
2939 ** determined by the one-byte unsigned integer found at an offset of 20
2940 ** into the database file header. */
2941 usableSize = pageSize - page1[20];
2942 if( (u32)pageSize!=pBt->pageSize ){
2943 /* After reading the first page of the database assuming a page size
2944 ** of BtShared.pageSize, we have discovered that the page-size is
2945 ** actually pageSize. Unlock the database, leave pBt->pPage1 at
2946 ** zero and return SQLITE_OK. The caller will call this function
2947 ** again with the correct page-size.
2948 */
2949 releasePage(pPage1);
2950 pBt->usableSize = usableSize;
2951 pBt->pageSize = pageSize;
2952 freeTempSpace(pBt);
2953 rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize,
2954 pageSize-usableSize);
2955 return rc;
2956 }
2957 if( (pBt->db->flags & SQLITE_RecoveryMode)==0 && nPage>nPageFile ){
2958 rc = SQLITE_CORRUPT_BKPT;
2959 goto page1_init_failed;
2960 }
2961 /* EVIDENCE-OF: R-28312-64704 However, the usable size is not allowed to
2962 ** be less than 480. In other words, if the page size is 512, then the
2963 ** reserved space size cannot exceed 32. */
2964 if( usableSize<480 ){
2965 goto page1_init_failed;
2966 }
2967 pBt->pageSize = pageSize;
2968 pBt->usableSize = usableSize;
2969 #ifndef SQLITE_OMIT_AUTOVACUUM
2970 pBt->autoVacuum = (get4byte(&page1[36 + 4*4])?1:0);
2971 pBt->incrVacuum = (get4byte(&page1[36 + 7*4])?1:0);
2972 #endif
2973 }
2974
2975 /* maxLocal is the maximum amount of payload to store locally for
2976 ** a cell. Make sure it is small enough so that at least minFanout
2977 ** cells can will fit on one page. We assume a 10-byte page header.
2978 ** Besides the payload, the cell must store:
2979 ** 2-byte pointer to the cell
2980 ** 4-byte child pointer
2981 ** 9-byte nKey value
2982 ** 4-byte nData value
2983 ** 4-byte overflow page pointer
2984 ** So a cell consists of a 2-byte pointer, a header which is as much as
2985 ** 17 bytes long, 0 to N bytes of payload, and an optional 4 byte overflow
2986 ** page pointer.
2987 */
2988 pBt->maxLocal = (u16)((pBt->usableSize-12)*64/255 - 23);
2989 pBt->minLocal = (u16)((pBt->usableSize-12)*32/255 - 23);
2990 pBt->maxLeaf = (u16)(pBt->usableSize - 35);
2991 pBt->minLeaf = (u16)((pBt->usableSize-12)*32/255 - 23);
2992 if( pBt->maxLocal>127 ){
2993 pBt->max1bytePayload = 127;
2994 }else{
2995 pBt->max1bytePayload = (u8)pBt->maxLocal;
2996 }
2997 assert( pBt->maxLeaf + 23 <= MX_CELL_SIZE(pBt) );
2998 pBt->pPage1 = pPage1;
2999 pBt->nPage = nPage;
3000 return SQLITE_OK;
3001
3002 page1_init_failed:
3003 releasePage(pPage1);
3004 pBt->pPage1 = 0;
3005 return rc;
3006 }
3007
3008 #ifndef NDEBUG
3009 /*
3010 ** Return the number of cursors open on pBt. This is for use
3011 ** in assert() expressions, so it is only compiled if NDEBUG is not
3012 ** defined.
3013 **
3014 ** Only write cursors are counted if wrOnly is true. If wrOnly is
3015 ** false then all cursors are counted.
3016 **
3017 ** For the purposes of this routine, a cursor is any cursor that
3018 ** is capable of reading or writing to the database. Cursors that
3019 ** have been tripped into the CURSOR_FAULT state are not counted.
3020 */
3021 static int countValidCursors(BtShared *pBt, int wrOnly){
3022 BtCursor *pCur;
3023 int r = 0;
3024 for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){
3025 if( (wrOnly==0 || (pCur->curFlags & BTCF_WriteFlag)!=0)
3026 && pCur->eState!=CURSOR_FAULT ) r++;
3027 }
3028 return r;
3029 }
3030 #endif
3031
3032 /*
3033 ** If there are no outstanding cursors and we are not in the middle
3034 ** of a transaction but there is a read lock on the database, then
3035 ** this routine unrefs the first page of the database file which
3036 ** has the effect of releasing the read lock.
3037 **
3038 ** If there is a transaction in progress, this routine is a no-op.
3039 */
3040 static void unlockBtreeIfUnused(BtShared *pBt){
3041 assert( sqlite3_mutex_held(pBt->mutex) );
3042 assert( countValidCursors(pBt,0)==0 || pBt->inTransaction>TRANS_NONE );
3043 if( pBt->inTransaction==TRANS_NONE && pBt->pPage1!=0 ){
3044 MemPage *pPage1 = pBt->pPage1;
3045 assert( pPage1->aData );
3046 assert( sqlite3PagerRefcount(pBt->pPager)==1 );
3047 pBt->pPage1 = 0;
3048 releasePageNotNull(pPage1);
3049 }
3050 }
3051
3052 /*
3053 ** If pBt points to an empty file then convert that empty file
3054 ** into a new empty database by initializing the first page of
3055 ** the database.
3056 */
3057 static int newDatabase(BtShared *pBt){
3058 MemPage *pP1;
3059 unsigned char *data;
3060 int rc;
3061
3062 assert( sqlite3_mutex_held(pBt->mutex) );
3063 if( pBt->nPage>0 ){
3064 return SQLITE_OK;
3065 }
3066 pP1 = pBt->pPage1;
3067 assert( pP1!=0 );
3068 data = pP1->aData;
3069 rc = sqlite3PagerWrite(pP1->pDbPage);
3070 if( rc ) return rc;
3071 memcpy(data, zMagicHeader, sizeof(zMagicHeader));
3072 assert( sizeof(zMagicHeader)==16 );
3073 data[16] = (u8)((pBt->pageSize>>8)&0xff);
3074 data[17] = (u8)((pBt->pageSize>>16)&0xff);
3075 data[18] = 1;
3076 data[19] = 1;
3077 assert( pBt->usableSize<=pBt->pageSize && pBt->usableSize+255>=pBt->pageSize);
3078 data[20] = (u8)(pBt->pageSize - pBt->usableSize);
3079 data[21] = 64;
3080 data[22] = 32;
3081 data[23] = 32;
3082 memset(&data[24], 0, 100-24);
3083 zeroPage(pP1, PTF_INTKEY|PTF_LEAF|PTF_LEAFDATA );
3084 pBt->btsFlags |= BTS_PAGESIZE_FIXED;
3085 #ifndef SQLITE_OMIT_AUTOVACUUM
3086 assert( pBt->autoVacuum==1 || pBt->autoVacuum==0 );
3087 assert( pBt->incrVacuum==1 || pBt->incrVacuum==0 );
3088 put4byte(&data[36 + 4*4], pBt->autoVacuum);
3089 put4byte(&data[36 + 7*4], pBt->incrVacuum);
3090 #endif
3091 pBt->nPage = 1;
3092 data[31] = 1;
3093 return SQLITE_OK;
3094 }
3095
3096 /*
3097 ** Initialize the first page of the database file (creating a database
3098 ** consisting of a single page and no schema objects). Return SQLITE_OK
3099 ** if successful, or an SQLite error code otherwise.
3100 */
3101 SQLITE_PRIVATE int sqlite3BtreeNewDb(Btree *p){
3102 int rc;
3103 sqlite3BtreeEnter(p);
3104 p->pBt->nPage = 0;
3105 rc = newDatabase(p->pBt);
3106 sqlite3BtreeLeave(p);
3107 return rc;
3108 }
3109
3110 /*
3111 ** Attempt to start a new transaction. A write-transaction
3112 ** is started if the second argument is nonzero, otherwise a read-
3113 ** transaction. If the second argument is 2 or more and exclusive
3114 ** transaction is started, meaning that no other process is allowed
3115 ** to access the database. A preexisting transaction may not be
3116 ** upgraded to exclusive by calling this routine a second time - the
3117 ** exclusivity flag only works for a new transaction.
3118 **
3119 ** A write-transaction must be started before attempting any
3120 ** changes to the database. None of the following routines
3121 ** will work unless a transaction is started first:
3122 **
3123 ** sqlite3BtreeCreateTable()
3124 ** sqlite3BtreeCreateIndex()
3125 ** sqlite3BtreeClearTable()
3126 ** sqlite3BtreeDropTable()
3127 ** sqlite3BtreeInsert()
3128 ** sqlite3BtreeDelete()
3129 ** sqlite3BtreeUpdateMeta()
3130 **
3131 ** If an initial attempt to acquire the lock fails because of lock contention
3132 ** and the database was previously unlocked, then invoke the busy handler
3133 ** if there is one. But if there was previously a read-lock, do not
3134 ** invoke the busy handler - just return SQLITE_BUSY. SQLITE_BUSY is
3135 ** returned when there is already a read-lock in order to avoid a deadlock.
3136 **
3137 ** Suppose there are two processes A and B. A has a read lock and B has
3138 ** a reserved lock. B tries to promote to exclusive but is blocked because
3139 ** of A's read lock. A tries to promote to reserved but is blocked by B.
3140 ** One or the other of the two processes must give way or there can be
3141 ** no progress. By returning SQLITE_BUSY and not invoking the busy callback
3142 ** when A already has a read lock, we encourage A to give up and let B
3143 ** proceed.
3144 */
3145 SQLITE_PRIVATE int sqlite3BtreeBeginTrans(Btree *p, int wrflag){
3146 BtShared *pBt = p->pBt;
3147 int rc = SQLITE_OK;
3148
3149 sqlite3BtreeEnter(p);
3150 btreeIntegrity(p);
3151
3152 /* If the btree is already in a write-transaction, or it
3153 ** is already in a read-transaction and a read-transaction
3154 ** is requested, this is a no-op.
3155 */
3156 if( p->inTrans==TRANS_WRITE || (p->inTrans==TRANS_READ && !wrflag) ){
3157 goto trans_begun;
3158 }
3159 assert( pBt->inTransaction==TRANS_WRITE || IfNotOmitAV(pBt->bDoTruncate)==0 );
3160
3161 /* Write transactions are not possible on a read-only database */
3162 if( (pBt->btsFlags & BTS_READ_ONLY)!=0 && wrflag ){
3163 rc = SQLITE_READONLY;
3164 goto trans_begun;
3165 }
3166
3167 #ifndef SQLITE_OMIT_SHARED_CACHE
3168 {
3169 sqlite3 *pBlock = 0;
3170 /* If another database handle has already opened a write transaction
3171 ** on this shared-btree structure and a second write transaction is
3172 ** requested, return SQLITE_LOCKED.
3173 */
3174 if( (wrflag && pBt->inTransaction==TRANS_WRITE)
3175 || (pBt->btsFlags & BTS_PENDING)!=0
3176 ){
3177 pBlock = pBt->pWriter->db;
3178 }else if( wrflag>1 ){
3179 BtLock *pIter;
3180 for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
3181 if( pIter->pBtree!=p ){
3182 pBlock = pIter->pBtree->db;
3183 break;
3184 }
3185 }
3186 }
3187 if( pBlock ){
3188 sqlite3ConnectionBlocked(p->db, pBlock);
3189 rc = SQLITE_LOCKED_SHAREDCACHE;
3190 goto trans_begun;
3191 }
3192 }
3193 #endif
3194
3195 /* Any read-only or read-write transaction implies a read-lock on
3196 ** page 1. So if some other shared-cache client already has a write-lock
3197 ** on page 1, the transaction cannot be opened. */
3198 rc = querySharedCacheTableLock(p, MASTER_ROOT, READ_LOCK);
3199 if( SQLITE_OK!=rc ) goto trans_begun;
3200
3201 pBt->btsFlags &= ~BTS_INITIALLY_EMPTY;
3202 if( pBt->nPage==0 ) pBt->btsFlags |= BTS_INITIALLY_EMPTY;
3203 do {
3204 /* Call lockBtree() until either pBt->pPage1 is populated or
3205 ** lockBtree() returns something other than SQLITE_OK. lockBtree()
3206 ** may return SQLITE_OK but leave pBt->pPage1 set to 0 if after
3207 ** reading page 1 it discovers that the page-size of the database
3208 ** file is not pBt->pageSize. In this case lockBtree() will update
3209 ** pBt->pageSize to the page-size of the file on disk.
3210 */
3211 while( pBt->pPage1==0 && SQLITE_OK==(rc = lockBtree(pBt)) );
3212
3213 if( rc==SQLITE_OK && wrflag ){
3214 if( (pBt->btsFlags & BTS_READ_ONLY)!=0 ){
3215 rc = SQLITE_READONLY;
3216 }else{
3217 rc = sqlite3PagerBegin(pBt->pPager,wrflag>1,sqlite3TempInMemory(p->db));
3218 if( rc==SQLITE_OK ){
3219 rc = newDatabase(pBt);
3220 }
3221 }
3222 }
3223
3224 if( rc!=SQLITE_OK ){
3225 unlockBtreeIfUnused(pBt);
3226 }
3227 }while( (rc&0xFF)==SQLITE_BUSY && pBt->inTransaction==TRANS_NONE &&
3228 btreeInvokeBusyHandler(pBt) );
3229
3230 if( rc==SQLITE_OK ){
3231 if( p->inTrans==TRANS_NONE ){
3232 pBt->nTransaction++;
3233 #ifndef SQLITE_OMIT_SHARED_CACHE
3234 if( p->sharable ){
3235 assert( p->lock.pBtree==p && p->lock.iTable==1 );
3236 p->lock.eLock = READ_LOCK;
3237 p->lock.pNext = pBt->pLock;
3238 pBt->pLock = &p->lock;
3239 }
3240 #endif
3241 }
3242 p->inTrans = (wrflag?TRANS_WRITE:TRANS_READ);
3243 if( p->inTrans>pBt->inTransaction ){
3244 pBt->inTransaction = p->inTrans;
3245 }
3246 if( wrflag ){
3247 MemPage *pPage1 = pBt->pPage1;
3248 #ifndef SQLITE_OMIT_SHARED_CACHE
3249 assert( !pBt->pWriter );
3250 pBt->pWriter = p;
3251 pBt->btsFlags &= ~BTS_EXCLUSIVE;
3252 if( wrflag>1 ) pBt->btsFlags |= BTS_EXCLUSIVE;
3253 #endif
3254
3255 /* If the db-size header field is incorrect (as it may be if an old
3256 ** client has been writing the database file), update it now. Doing
3257 ** this sooner rather than later means the database size can safely
3258 ** re-read the database size from page 1 if a savepoint or transaction
3259 ** rollback occurs within the transaction.
3260 */
3261 if( pBt->nPage!=get4byte(&pPage1->aData[28]) ){
3262 rc = sqlite3PagerWrite(pPage1->pDbPage);
3263 if( rc==SQLITE_OK ){
3264 put4byte(&pPage1->aData[28], pBt->nPage);
3265 }
3266 }
3267 }
3268 }
3269
3270
3271 trans_begun:
3272 if( rc==SQLITE_OK && wrflag ){
3273 /* This call makes sure that the pager has the correct number of
3274 ** open savepoints. If the second parameter is greater than 0 and
3275 ** the sub-journal is not already open, then it will be opened here.
3276 */
3277 rc = sqlite3PagerOpenSavepoint(pBt->pPager, p->db->nSavepoint);
3278 }
3279
3280 btreeIntegrity(p);
3281 sqlite3BtreeLeave(p);
3282 return rc;
3283 }
3284
3285 #ifndef SQLITE_OMIT_AUTOVACUUM
3286
3287 /*
3288 ** Set the pointer-map entries for all children of page pPage. Also, if
3289 ** pPage contains cells that point to overflow pages, set the pointer
3290 ** map entries for the overflow pages as well.
3291 */
3292 static int setChildPtrmaps(MemPage *pPage){
3293 int i; /* Counter variable */
3294 int nCell; /* Number of cells in page pPage */
3295 int rc; /* Return code */
3296 BtShared *pBt = pPage->pBt;
3297 Pgno pgno = pPage->pgno;
3298
3299 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
3300 rc = btreeInitPage(pPage);
3301 if( rc!=SQLITE_OK ) return rc;
3302 nCell = pPage->nCell;
3303
3304 for(i=0; i<nCell; i++){
3305 u8 *pCell = findCell(pPage, i);
3306
3307 ptrmapPutOvflPtr(pPage, pCell, &rc);
3308
3309 if( !pPage->leaf ){
3310 Pgno childPgno = get4byte(pCell);
3311 ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno, &rc);
3312 }
3313 }
3314
3315 if( !pPage->leaf ){
3316 Pgno childPgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
3317 ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno, &rc);
3318 }
3319
3320 return rc;
3321 }
3322
3323 /*
3324 ** Somewhere on pPage is a pointer to page iFrom. Modify this pointer so
3325 ** that it points to iTo. Parameter eType describes the type of pointer to
3326 ** be modified, as follows:
3327 **
3328 ** PTRMAP_BTREE: pPage is a btree-page. The pointer points at a child
3329 ** page of pPage.
3330 **
3331 ** PTRMAP_OVERFLOW1: pPage is a btree-page. The pointer points at an overflow
3332 ** page pointed to by one of the cells on pPage.
3333 **
3334 ** PTRMAP_OVERFLOW2: pPage is an overflow-page. The pointer points at the next
3335 ** overflow page in the list.
3336 */
3337 static int modifyPagePointer(MemPage *pPage, Pgno iFrom, Pgno iTo, u8 eType){
3338 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
3339 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
3340 if( eType==PTRMAP_OVERFLOW2 ){
3341 /* The pointer is always the first 4 bytes of the page in this case. */
3342 if( get4byte(pPage->aData)!=iFrom ){
3343 return SQLITE_CORRUPT_BKPT;
3344 }
3345 put4byte(pPage->aData, iTo);
3346 }else{
3347 int i;
3348 int nCell;
3349 int rc;
3350
3351 rc = btreeInitPage(pPage);
3352 if( rc ) return rc;
3353 nCell = pPage->nCell;
3354
3355 for(i=0; i<nCell; i++){
3356 u8 *pCell = findCell(pPage, i);
3357 if( eType==PTRMAP_OVERFLOW1 ){
3358 CellInfo info;
3359 pPage->xParseCell(pPage, pCell, &info);
3360 if( info.nLocal<info.nPayload ){
3361 if( pCell+info.nSize > pPage->aData+pPage->pBt->usableSize ){
3362 return SQLITE_CORRUPT_BKPT;
3363 }
3364 if( iFrom==get4byte(pCell+info.nSize-4) ){
3365 put4byte(pCell+info.nSize-4, iTo);
3366 break;
3367 }
3368 }
3369 }else{
3370 if( get4byte(pCell)==iFrom ){
3371 put4byte(pCell, iTo);
3372 break;
3373 }
3374 }
3375 }
3376
3377 if( i==nCell ){
3378 if( eType!=PTRMAP_BTREE ||
3379 get4byte(&pPage->aData[pPage->hdrOffset+8])!=iFrom ){
3380 return SQLITE_CORRUPT_BKPT;
3381 }
3382 put4byte(&pPage->aData[pPage->hdrOffset+8], iTo);
3383 }
3384 }
3385 return SQLITE_OK;
3386 }
3387
3388
3389 /*
3390 ** Move the open database page pDbPage to location iFreePage in the
3391 ** database. The pDbPage reference remains valid.
3392 **
3393 ** The isCommit flag indicates that there is no need to remember that
3394 ** the journal needs to be sync()ed before database page pDbPage->pgno
3395 ** can be written to. The caller has already promised not to write to that
3396 ** page.
3397 */
3398 static int relocatePage(
3399 BtShared *pBt, /* Btree */
3400 MemPage *pDbPage, /* Open page to move */
3401 u8 eType, /* Pointer map 'type' entry for pDbPage */
3402 Pgno iPtrPage, /* Pointer map 'page-no' entry for pDbPage */
3403 Pgno iFreePage, /* The location to move pDbPage to */
3404 int isCommit /* isCommit flag passed to sqlite3PagerMovepage */
3405 ){
3406 MemPage *pPtrPage; /* The page that contains a pointer to pDbPage */
3407 Pgno iDbPage = pDbPage->pgno;
3408 Pager *pPager = pBt->pPager;
3409 int rc;
3410
3411 assert( eType==PTRMAP_OVERFLOW2 || eType==PTRMAP_OVERFLOW1 ||
3412 eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE );
3413 assert( sqlite3_mutex_held(pBt->mutex) );
3414 assert( pDbPage->pBt==pBt );
3415
3416 /* Move page iDbPage from its current location to page number iFreePage */
3417 TRACE(("AUTOVACUUM: Moving %d to free page %d (ptr page %d type %d)\n",
3418 iDbPage, iFreePage, iPtrPage, eType));
3419 rc = sqlite3PagerMovepage(pPager, pDbPage->pDbPage, iFreePage, isCommit);
3420 if( rc!=SQLITE_OK ){
3421 return rc;
3422 }
3423 pDbPage->pgno = iFreePage;
3424
3425 /* If pDbPage was a btree-page, then it may have child pages and/or cells
3426 ** that point to overflow pages. The pointer map entries for all these
3427 ** pages need to be changed.
3428 **
3429 ** If pDbPage is an overflow page, then the first 4 bytes may store a
3430 ** pointer to a subsequent overflow page. If this is the case, then
3431 ** the pointer map needs to be updated for the subsequent overflow page.
3432 */
3433 if( eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE ){
3434 rc = setChildPtrmaps(pDbPage);
3435 if( rc!=SQLITE_OK ){
3436 return rc;
3437 }
3438 }else{
3439 Pgno nextOvfl = get4byte(pDbPage->aData);
3440 if( nextOvfl!=0 ){
3441 ptrmapPut(pBt, nextOvfl, PTRMAP_OVERFLOW2, iFreePage, &rc);
3442 if( rc!=SQLITE_OK ){
3443 return rc;
3444 }
3445 }
3446 }
3447
3448 /* Fix the database pointer on page iPtrPage that pointed at iDbPage so
3449 ** that it points at iFreePage. Also fix the pointer map entry for
3450 ** iPtrPage.
3451 */
3452 if( eType!=PTRMAP_ROOTPAGE ){
3453 rc = btreeGetPage(pBt, iPtrPage, &pPtrPage, 0);
3454 if( rc!=SQLITE_OK ){
3455 return rc;
3456 }
3457 rc = sqlite3PagerWrite(pPtrPage->pDbPage);
3458 if( rc!=SQLITE_OK ){
3459 releasePage(pPtrPage);
3460 return rc;
3461 }
3462 rc = modifyPagePointer(pPtrPage, iDbPage, iFreePage, eType);
3463 releasePage(pPtrPage);
3464 if( rc==SQLITE_OK ){
3465 ptrmapPut(pBt, iFreePage, eType, iPtrPage, &rc);
3466 }
3467 }
3468 return rc;
3469 }
3470
3471 /* Forward declaration required by incrVacuumStep(). */
3472 static int allocateBtreePage(BtShared *, MemPage **, Pgno *, Pgno, u8);
3473
3474 /*
3475 ** Perform a single step of an incremental-vacuum. If successful, return
3476 ** SQLITE_OK. If there is no work to do (and therefore no point in
3477 ** calling this function again), return SQLITE_DONE. Or, if an error
3478 ** occurs, return some other error code.
3479 **
3480 ** More specifically, this function attempts to re-organize the database so
3481 ** that the last page of the file currently in use is no longer in use.
3482 **
3483 ** Parameter nFin is the number of pages that this database would contain
3484 ** were this function called until it returns SQLITE_DONE.
3485 **
3486 ** If the bCommit parameter is non-zero, this function assumes that the
3487 ** caller will keep calling incrVacuumStep() until it returns SQLITE_DONE
3488 ** or an error. bCommit is passed true for an auto-vacuum-on-commit
3489 ** operation, or false for an incremental vacuum.
3490 */
3491 static int incrVacuumStep(BtShared *pBt, Pgno nFin, Pgno iLastPg, int bCommit){
3492 Pgno nFreeList; /* Number of pages still on the free-list */
3493 int rc;
3494
3495 assert( sqlite3_mutex_held(pBt->mutex) );
3496 assert( iLastPg>nFin );
3497
3498 if( !PTRMAP_ISPAGE(pBt, iLastPg) && iLastPg!=PENDING_BYTE_PAGE(pBt) ){
3499 u8 eType;
3500 Pgno iPtrPage;
3501
3502 nFreeList = get4byte(&pBt->pPage1->aData[36]);
3503 if( nFreeList==0 ){
3504 return SQLITE_DONE;
3505 }
3506
3507 rc = ptrmapGet(pBt, iLastPg, &eType, &iPtrPage);
3508 if( rc!=SQLITE_OK ){
3509 return rc;
3510 }
3511 if( eType==PTRMAP_ROOTPAGE ){
3512 return SQLITE_CORRUPT_BKPT;
3513 }
3514
3515 if( eType==PTRMAP_FREEPAGE ){
3516 if( bCommit==0 ){
3517 /* Remove the page from the files free-list. This is not required
3518 ** if bCommit is non-zero. In that case, the free-list will be
3519 ** truncated to zero after this function returns, so it doesn't
3520 ** matter if it still contains some garbage entries.
3521 */
3522 Pgno iFreePg;
3523 MemPage *pFreePg;
3524 rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, iLastPg, BTALLOC_EXACT);
3525 if( rc!=SQLITE_OK ){
3526 return rc;
3527 }
3528 assert( iFreePg==iLastPg );
3529 releasePage(pFreePg);
3530 }
3531 } else {
3532 Pgno iFreePg; /* Index of free page to move pLastPg to */
3533 MemPage *pLastPg;
3534 u8 eMode = BTALLOC_ANY; /* Mode parameter for allocateBtreePage() */
3535 Pgno iNear = 0; /* nearby parameter for allocateBtreePage() */
3536
3537 rc = btreeGetPage(pBt, iLastPg, &pLastPg, 0);
3538 if( rc!=SQLITE_OK ){
3539 return rc;
3540 }
3541
3542 /* If bCommit is zero, this loop runs exactly once and page pLastPg
3543 ** is swapped with the first free page pulled off the free list.
3544 **
3545 ** On the other hand, if bCommit is greater than zero, then keep
3546 ** looping until a free-page located within the first nFin pages
3547 ** of the file is found.
3548 */
3549 if( bCommit==0 ){
3550 eMode = BTALLOC_LE;
3551 iNear = nFin;
3552 }
3553 do {
3554 MemPage *pFreePg;
3555 rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, iNear, eMode);
3556 if( rc!=SQLITE_OK ){
3557 releasePage(pLastPg);
3558 return rc;
3559 }
3560 releasePage(pFreePg);
3561 }while( bCommit && iFreePg>nFin );
3562 assert( iFreePg<iLastPg );
3563
3564 rc = relocatePage(pBt, pLastPg, eType, iPtrPage, iFreePg, bCommit);
3565 releasePage(pLastPg);
3566 if( rc!=SQLITE_OK ){
3567 return rc;
3568 }
3569 }
3570 }
3571
3572 if( bCommit==0 ){
3573 do {
3574 iLastPg--;
3575 }while( iLastPg==PENDING_BYTE_PAGE(pBt) || PTRMAP_ISPAGE(pBt, iLastPg) );
3576 pBt->bDoTruncate = 1;
3577 pBt->nPage = iLastPg;
3578 }
3579 return SQLITE_OK;
3580 }
3581
3582 /*
3583 ** The database opened by the first argument is an auto-vacuum database
3584 ** nOrig pages in size containing nFree free pages. Return the expected
3585 ** size of the database in pages following an auto-vacuum operation.
3586 */
3587 static Pgno finalDbSize(BtShared *pBt, Pgno nOrig, Pgno nFree){
3588 int nEntry; /* Number of entries on one ptrmap page */
3589 Pgno nPtrmap; /* Number of PtrMap pages to be freed */
3590 Pgno nFin; /* Return value */
3591
3592 nEntry = pBt->usableSize/5;
3593 nPtrmap = (nFree-nOrig+PTRMAP_PAGENO(pBt, nOrig)+nEntry)/nEntry;
3594 nFin = nOrig - nFree - nPtrmap;
3595 if( nOrig>PENDING_BYTE_PAGE(pBt) && nFin<PENDING_BYTE_PAGE(pBt) ){
3596 nFin--;
3597 }
3598 while( PTRMAP_ISPAGE(pBt, nFin) || nFin==PENDING_BYTE_PAGE(pBt) ){
3599 nFin--;
3600 }
3601
3602 return nFin;
3603 }
3604
3605 /*
3606 ** A write-transaction must be opened before calling this function.
3607 ** It performs a single unit of work towards an incremental vacuum.
3608 **
3609 ** If the incremental vacuum is finished after this function has run,
3610 ** SQLITE_DONE is returned. If it is not finished, but no error occurred,
3611 ** SQLITE_OK is returned. Otherwise an SQLite error code.
3612 */
3613 SQLITE_PRIVATE int sqlite3BtreeIncrVacuum(Btree *p){
3614 int rc;
3615 BtShared *pBt = p->pBt;
3616
3617 sqlite3BtreeEnter(p);
3618 assert( pBt->inTransaction==TRANS_WRITE && p->inTrans==TRANS_WRITE );
3619 if( !pBt->autoVacuum ){
3620 rc = SQLITE_DONE;
3621 }else{
3622 Pgno nOrig = btreePagecount(pBt);
3623 Pgno nFree = get4byte(&pBt->pPage1->aData[36]);
3624 Pgno nFin = finalDbSize(pBt, nOrig, nFree);
3625
3626 if( nOrig<nFin ){
3627 rc = SQLITE_CORRUPT_BKPT;
3628 }else if( nFree>0 ){
3629 rc = saveAllCursors(pBt, 0, 0);
3630 if( rc==SQLITE_OK ){
3631 invalidateAllOverflowCache(pBt);
3632 rc = incrVacuumStep(pBt, nFin, nOrig, 0);
3633 }
3634 if( rc==SQLITE_OK ){
3635 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
3636 put4byte(&pBt->pPage1->aData[28], pBt->nPage);
3637 }
3638 }else{
3639 rc = SQLITE_DONE;
3640 }
3641 }
3642 sqlite3BtreeLeave(p);
3643 return rc;
3644 }
3645
3646 /*
3647 ** This routine is called prior to sqlite3PagerCommit when a transaction
3648 ** is committed for an auto-vacuum database.
3649 **
3650 ** If SQLITE_OK is returned, then *pnTrunc is set to the number of pages
3651 ** the database file should be truncated to during the commit process.
3652 ** i.e. the database has been reorganized so that only the first *pnTrunc
3653 ** pages are in use.
3654 */
3655 static int autoVacuumCommit(BtShared *pBt){
3656 int rc = SQLITE_OK;
3657 Pager *pPager = pBt->pPager;
3658 VVA_ONLY( int nRef = sqlite3PagerRefcount(pPager); )
3659
3660 assert( sqlite3_mutex_held(pBt->mutex) );
3661 invalidateAllOverflowCache(pBt);
3662 assert(pBt->autoVacuum);
3663 if( !pBt->incrVacuum ){
3664 Pgno nFin; /* Number of pages in database after autovacuuming */
3665 Pgno nFree; /* Number of pages on the freelist initially */
3666 Pgno iFree; /* The next page to be freed */
3667 Pgno nOrig; /* Database size before freeing */
3668
3669 nOrig = btreePagecount(pBt);
3670 if( PTRMAP_ISPAGE(pBt, nOrig) || nOrig==PENDING_BYTE_PAGE(pBt) ){
3671 /* It is not possible to create a database for which the final page
3672 ** is either a pointer-map page or the pending-byte page. If one
3673 ** is encountered, this indicates corruption.
3674 */
3675 return SQLITE_CORRUPT_BKPT;
3676 }
3677
3678 nFree = get4byte(&pBt->pPage1->aData[36]);
3679 nFin = finalDbSize(pBt, nOrig, nFree);
3680 if( nFin>nOrig ) return SQLITE_CORRUPT_BKPT;
3681 if( nFin<nOrig ){
3682 rc = saveAllCursors(pBt, 0, 0);
3683 }
3684 for(iFree=nOrig; iFree>nFin && rc==SQLITE_OK; iFree--){
3685 rc = incrVacuumStep(pBt, nFin, iFree, 1);
3686 }
3687 if( (rc==SQLITE_DONE || rc==SQLITE_OK) && nFree>0 ){
3688 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
3689 put4byte(&pBt->pPage1->aData[32], 0);
3690 put4byte(&pBt->pPage1->aData[36], 0);
3691 put4byte(&pBt->pPage1->aData[28], nFin);
3692 pBt->bDoTruncate = 1;
3693 pBt->nPage = nFin;
3694 }
3695 if( rc!=SQLITE_OK ){
3696 sqlite3PagerRollback(pPager);
3697 }
3698 }
3699
3700 assert( nRef>=sqlite3PagerRefcount(pPager) );
3701 return rc;
3702 }
3703
3704 #else /* ifndef SQLITE_OMIT_AUTOVACUUM */
3705 # define setChildPtrmaps(x) SQLITE_OK
3706 #endif
3707
3708 /*
3709 ** This routine does the first phase of a two-phase commit. This routine
3710 ** causes a rollback journal to be created (if it does not already exist)
3711 ** and populated with enough information so that if a power loss occurs
3712 ** the database can be restored to its original state by playing back
3713 ** the journal. Then the contents of the journal are flushed out to
3714 ** the disk. After the journal is safely on oxide, the changes to the
3715 ** database are written into the database file and flushed to oxide.
3716 ** At the end of this call, the rollback journal still exists on the
3717 ** disk and we are still holding all locks, so the transaction has not
3718 ** committed. See sqlite3BtreeCommitPhaseTwo() for the second phase of the
3719 ** commit process.
3720 **
3721 ** This call is a no-op if no write-transaction is currently active on pBt.
3722 **
3723 ** Otherwise, sync the database file for the btree pBt. zMaster points to
3724 ** the name of a master journal file that should be written into the
3725 ** individual journal file, or is NULL, indicating no master journal file
3726 ** (single database transaction).
3727 **
3728 ** When this is called, the master journal should already have been
3729 ** created, populated with this journal pointer and synced to disk.
3730 **
3731 ** Once this is routine has returned, the only thing required to commit
3732 ** the write-transaction for this database file is to delete the journal.
3733 */
3734 SQLITE_PRIVATE int sqlite3BtreeCommitPhaseOne(Btree *p, const char *zMaster){
3735 int rc = SQLITE_OK;
3736 if( p->inTrans==TRANS_WRITE ){
3737 BtShared *pBt = p->pBt;
3738 sqlite3BtreeEnter(p);
3739 #ifndef SQLITE_OMIT_AUTOVACUUM
3740 if( pBt->autoVacuum ){
3741 rc = autoVacuumCommit(pBt);
3742 if( rc!=SQLITE_OK ){
3743 sqlite3BtreeLeave(p);
3744 return rc;
3745 }
3746 }
3747 if( pBt->bDoTruncate ){
3748 sqlite3PagerTruncateImage(pBt->pPager, pBt->nPage);
3749 }
3750 #endif
3751 rc = sqlite3PagerCommitPhaseOne(pBt->pPager, zMaster, 0);
3752 sqlite3BtreeLeave(p);
3753 }
3754 return rc;
3755 }
3756
3757 /*
3758 ** This function is called from both BtreeCommitPhaseTwo() and BtreeRollback()
3759 ** at the conclusion of a transaction.
3760 */
3761 static void btreeEndTransaction(Btree *p){
3762 BtShared *pBt = p->pBt;
3763 sqlite3 *db = p->db;
3764 assert( sqlite3BtreeHoldsMutex(p) );
3765
3766 #ifndef SQLITE_OMIT_AUTOVACUUM
3767 pBt->bDoTruncate = 0;
3768 #endif
3769 if( p->inTrans>TRANS_NONE && db->nVdbeRead>1 ){
3770 /* If there are other active statements that belong to this database
3771 ** handle, downgrade to a read-only transaction. The other statements
3772 ** may still be reading from the database. */
3773 downgradeAllSharedCacheTableLocks(p);
3774 p->inTrans = TRANS_READ;
3775 }else{
3776 /* If the handle had any kind of transaction open, decrement the
3777 ** transaction count of the shared btree. If the transaction count
3778 ** reaches 0, set the shared state to TRANS_NONE. The unlockBtreeIfUnused()
3779 ** call below will unlock the pager. */
3780 if( p->inTrans!=TRANS_NONE ){
3781 clearAllSharedCacheTableLocks(p);
3782 pBt->nTransaction--;
3783 if( 0==pBt->nTransaction ){
3784 pBt->inTransaction = TRANS_NONE;
3785 }
3786 }
3787
3788 /* Set the current transaction state to TRANS_NONE and unlock the
3789 ** pager if this call closed the only read or write transaction. */
3790 p->inTrans = TRANS_NONE;
3791 unlockBtreeIfUnused(pBt);
3792 }
3793
3794 btreeIntegrity(p);
3795 }
3796
3797 /*
3798 ** Commit the transaction currently in progress.
3799 **
3800 ** This routine implements the second phase of a 2-phase commit. The
3801 ** sqlite3BtreeCommitPhaseOne() routine does the first phase and should
3802 ** be invoked prior to calling this routine. The sqlite3BtreeCommitPhaseOne()
3803 ** routine did all the work of writing information out to disk and flushing the
3804 ** contents so that they are written onto the disk platter. All this
3805 ** routine has to do is delete or truncate or zero the header in the
3806 ** the rollback journal (which causes the transaction to commit) and
3807 ** drop locks.
3808 **
3809 ** Normally, if an error occurs while the pager layer is attempting to
3810 ** finalize the underlying journal file, this function returns an error and
3811 ** the upper layer will attempt a rollback. However, if the second argument
3812 ** is non-zero then this b-tree transaction is part of a multi-file
3813 ** transaction. In this case, the transaction has already been committed
3814 ** (by deleting a master journal file) and the caller will ignore this
3815 ** functions return code. So, even if an error occurs in the pager layer,
3816 ** reset the b-tree objects internal state to indicate that the write
3817 ** transaction has been closed. This is quite safe, as the pager will have
3818 ** transitioned to the error state.
3819 **
3820 ** This will release the write lock on the database file. If there
3821 ** are no active cursors, it also releases the read lock.
3822 */
3823 SQLITE_PRIVATE int sqlite3BtreeCommitPhaseTwo(Btree *p, int bCleanup){
3824
3825 if( p->inTrans==TRANS_NONE ) return SQLITE_OK;
3826 sqlite3BtreeEnter(p);
3827 btreeIntegrity(p);
3828
3829 /* If the handle has a write-transaction open, commit the shared-btrees
3830 ** transaction and set the shared state to TRANS_READ.
3831 */
3832 if( p->inTrans==TRANS_WRITE ){
3833 int rc;
3834 BtShared *pBt = p->pBt;
3835 assert( pBt->inTransaction==TRANS_WRITE );
3836 assert( pBt->nTransaction>0 );
3837 rc = sqlite3PagerCommitPhaseTwo(pBt->pPager);
3838 if( rc!=SQLITE_OK && bCleanup==0 ){
3839 sqlite3BtreeLeave(p);
3840 return rc;
3841 }
3842 p->iDataVersion--; /* Compensate for pPager->iDataVersion++; */
3843 pBt->inTransaction = TRANS_READ;
3844 btreeClearHasContent(pBt);
3845 }
3846
3847 btreeEndTransaction(p);
3848 sqlite3BtreeLeave(p);
3849 return SQLITE_OK;
3850 }
3851
3852 /*
3853 ** Do both phases of a commit.
3854 */
3855 SQLITE_PRIVATE int sqlite3BtreeCommit(Btree *p){
3856 int rc;
3857 sqlite3BtreeEnter(p);
3858 rc = sqlite3BtreeCommitPhaseOne(p, 0);
3859 if( rc==SQLITE_OK ){
3860 rc = sqlite3BtreeCommitPhaseTwo(p, 0);
3861 }
3862 sqlite3BtreeLeave(p);
3863 return rc;
3864 }
3865
3866 /*
3867 ** This routine sets the state to CURSOR_FAULT and the error
3868 ** code to errCode for every cursor on any BtShared that pBtree
3869 ** references. Or if the writeOnly flag is set to 1, then only
3870 ** trip write cursors and leave read cursors unchanged.
3871 **
3872 ** Every cursor is a candidate to be tripped, including cursors
3873 ** that belong to other database connections that happen to be
3874 ** sharing the cache with pBtree.
3875 **
3876 ** This routine gets called when a rollback occurs. If the writeOnly
3877 ** flag is true, then only write-cursors need be tripped - read-only
3878 ** cursors save their current positions so that they may continue
3879 ** following the rollback. Or, if writeOnly is false, all cursors are
3880 ** tripped. In general, writeOnly is false if the transaction being
3881 ** rolled back modified the database schema. In this case b-tree root
3882 ** pages may be moved or deleted from the database altogether, making
3883 ** it unsafe for read cursors to continue.
3884 **
3885 ** If the writeOnly flag is true and an error is encountered while
3886 ** saving the current position of a read-only cursor, all cursors,
3887 ** including all read-cursors are tripped.
3888 **
3889 ** SQLITE_OK is returned if successful, or if an error occurs while
3890 ** saving a cursor position, an SQLite error code.
3891 */
3892 SQLITE_PRIVATE int sqlite3BtreeTripAllCursors(Btree *pBtree, int errCode, int wr iteOnly){
3893 BtCursor *p;
3894 int rc = SQLITE_OK;
3895
3896 assert( (writeOnly==0 || writeOnly==1) && BTCF_WriteFlag==1 );
3897 if( pBtree ){
3898 sqlite3BtreeEnter(pBtree);
3899 for(p=pBtree->pBt->pCursor; p; p=p->pNext){
3900 int i;
3901 if( writeOnly && (p->curFlags & BTCF_WriteFlag)==0 ){
3902 if( p->eState==CURSOR_VALID || p->eState==CURSOR_SKIPNEXT ){
3903 rc = saveCursorPosition(p);
3904 if( rc!=SQLITE_OK ){
3905 (void)sqlite3BtreeTripAllCursors(pBtree, rc, 0);
3906 break;
3907 }
3908 }
3909 }else{
3910 sqlite3BtreeClearCursor(p);
3911 p->eState = CURSOR_FAULT;
3912 p->skipNext = errCode;
3913 }
3914 for(i=0; i<=p->iPage; i++){
3915 releasePage(p->apPage[i]);
3916 p->apPage[i] = 0;
3917 }
3918 }
3919 sqlite3BtreeLeave(pBtree);
3920 }
3921 return rc;
3922 }
3923
3924 /*
3925 ** Rollback the transaction in progress.
3926 **
3927 ** If tripCode is not SQLITE_OK then cursors will be invalidated (tripped).
3928 ** Only write cursors are tripped if writeOnly is true but all cursors are
3929 ** tripped if writeOnly is false. Any attempt to use
3930 ** a tripped cursor will result in an error.
3931 **
3932 ** This will release the write lock on the database file. If there
3933 ** are no active cursors, it also releases the read lock.
3934 */
3935 SQLITE_PRIVATE int sqlite3BtreeRollback(Btree *p, int tripCode, int writeOnly){
3936 int rc;
3937 BtShared *pBt = p->pBt;
3938 MemPage *pPage1;
3939
3940 assert( writeOnly==1 || writeOnly==0 );
3941 assert( tripCode==SQLITE_ABORT_ROLLBACK || tripCode==SQLITE_OK );
3942 sqlite3BtreeEnter(p);
3943 if( tripCode==SQLITE_OK ){
3944 rc = tripCode = saveAllCursors(pBt, 0, 0);
3945 if( rc ) writeOnly = 0;
3946 }else{
3947 rc = SQLITE_OK;
3948 }
3949 if( tripCode ){
3950 int rc2 = sqlite3BtreeTripAllCursors(p, tripCode, writeOnly);
3951 assert( rc==SQLITE_OK || (writeOnly==0 && rc2==SQLITE_OK) );
3952 if( rc2!=SQLITE_OK ) rc = rc2;
3953 }
3954 btreeIntegrity(p);
3955
3956 if( p->inTrans==TRANS_WRITE ){
3957 int rc2;
3958
3959 assert( TRANS_WRITE==pBt->inTransaction );
3960 rc2 = sqlite3PagerRollback(pBt->pPager);
3961 if( rc2!=SQLITE_OK ){
3962 rc = rc2;
3963 }
3964
3965 /* The rollback may have destroyed the pPage1->aData value. So
3966 ** call btreeGetPage() on page 1 again to make
3967 ** sure pPage1->aData is set correctly. */
3968 if( btreeGetPage(pBt, 1, &pPage1, 0)==SQLITE_OK ){
3969 int nPage = get4byte(28+(u8*)pPage1->aData);
3970 testcase( nPage==0 );
3971 if( nPage==0 ) sqlite3PagerPagecount(pBt->pPager, &nPage);
3972 testcase( pBt->nPage!=nPage );
3973 pBt->nPage = nPage;
3974 releasePage(pPage1);
3975 }
3976 assert( countValidCursors(pBt, 1)==0 );
3977 pBt->inTransaction = TRANS_READ;
3978 btreeClearHasContent(pBt);
3979 }
3980
3981 btreeEndTransaction(p);
3982 sqlite3BtreeLeave(p);
3983 return rc;
3984 }
3985
3986 /*
3987 ** Start a statement subtransaction. The subtransaction can be rolled
3988 ** back independently of the main transaction. You must start a transaction
3989 ** before starting a subtransaction. The subtransaction is ended automatically
3990 ** if the main transaction commits or rolls back.
3991 **
3992 ** Statement subtransactions are used around individual SQL statements
3993 ** that are contained within a BEGIN...COMMIT block. If a constraint
3994 ** error occurs within the statement, the effect of that one statement
3995 ** can be rolled back without having to rollback the entire transaction.
3996 **
3997 ** A statement sub-transaction is implemented as an anonymous savepoint. The
3998 ** value passed as the second parameter is the total number of savepoints,
3999 ** including the new anonymous savepoint, open on the B-Tree. i.e. if there
4000 ** are no active savepoints and no other statement-transactions open,
4001 ** iStatement is 1. This anonymous savepoint can be released or rolled back
4002 ** using the sqlite3BtreeSavepoint() function.
4003 */
4004 SQLITE_PRIVATE int sqlite3BtreeBeginStmt(Btree *p, int iStatement){
4005 int rc;
4006 BtShared *pBt = p->pBt;
4007 sqlite3BtreeEnter(p);
4008 assert( p->inTrans==TRANS_WRITE );
4009 assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
4010 assert( iStatement>0 );
4011 assert( iStatement>p->db->nSavepoint );
4012 assert( pBt->inTransaction==TRANS_WRITE );
4013 /* At the pager level, a statement transaction is a savepoint with
4014 ** an index greater than all savepoints created explicitly using
4015 ** SQL statements. It is illegal to open, release or rollback any
4016 ** such savepoints while the statement transaction savepoint is active.
4017 */
4018 rc = sqlite3PagerOpenSavepoint(pBt->pPager, iStatement);
4019 sqlite3BtreeLeave(p);
4020 return rc;
4021 }
4022
4023 /*
4024 ** The second argument to this function, op, is always SAVEPOINT_ROLLBACK
4025 ** or SAVEPOINT_RELEASE. This function either releases or rolls back the
4026 ** savepoint identified by parameter iSavepoint, depending on the value
4027 ** of op.
4028 **
4029 ** Normally, iSavepoint is greater than or equal to zero. However, if op is
4030 ** SAVEPOINT_ROLLBACK, then iSavepoint may also be -1. In this case the
4031 ** contents of the entire transaction are rolled back. This is different
4032 ** from a normal transaction rollback, as no locks are released and the
4033 ** transaction remains open.
4034 */
4035 SQLITE_PRIVATE int sqlite3BtreeSavepoint(Btree *p, int op, int iSavepoint){
4036 int rc = SQLITE_OK;
4037 if( p && p->inTrans==TRANS_WRITE ){
4038 BtShared *pBt = p->pBt;
4039 assert( op==SAVEPOINT_RELEASE || op==SAVEPOINT_ROLLBACK );
4040 assert( iSavepoint>=0 || (iSavepoint==-1 && op==SAVEPOINT_ROLLBACK) );
4041 sqlite3BtreeEnter(p);
4042 if( op==SAVEPOINT_ROLLBACK ){
4043 rc = saveAllCursors(pBt, 0, 0);
4044 }
4045 if( rc==SQLITE_OK ){
4046 rc = sqlite3PagerSavepoint(pBt->pPager, op, iSavepoint);
4047 }
4048 if( rc==SQLITE_OK ){
4049 if( iSavepoint<0 && (pBt->btsFlags & BTS_INITIALLY_EMPTY)!=0 ){
4050 pBt->nPage = 0;
4051 }
4052 rc = newDatabase(pBt);
4053 pBt->nPage = get4byte(28 + pBt->pPage1->aData);
4054
4055 /* The database size was written into the offset 28 of the header
4056 ** when the transaction started, so we know that the value at offset
4057 ** 28 is nonzero. */
4058 assert( pBt->nPage>0 );
4059 }
4060 sqlite3BtreeLeave(p);
4061 }
4062 return rc;
4063 }
4064
4065 /*
4066 ** Create a new cursor for the BTree whose root is on the page
4067 ** iTable. If a read-only cursor is requested, it is assumed that
4068 ** the caller already has at least a read-only transaction open
4069 ** on the database already. If a write-cursor is requested, then
4070 ** the caller is assumed to have an open write transaction.
4071 **
4072 ** If the BTREE_WRCSR bit of wrFlag is clear, then the cursor can only
4073 ** be used for reading. If the BTREE_WRCSR bit is set, then the cursor
4074 ** can be used for reading or for writing if other conditions for writing
4075 ** are also met. These are the conditions that must be met in order
4076 ** for writing to be allowed:
4077 **
4078 ** 1: The cursor must have been opened with wrFlag containing BTREE_WRCSR
4079 **
4080 ** 2: Other database connections that share the same pager cache
4081 ** but which are not in the READ_UNCOMMITTED state may not have
4082 ** cursors open with wrFlag==0 on the same table. Otherwise
4083 ** the changes made by this write cursor would be visible to
4084 ** the read cursors in the other database connection.
4085 **
4086 ** 3: The database must be writable (not on read-only media)
4087 **
4088 ** 4: There must be an active transaction.
4089 **
4090 ** The BTREE_FORDELETE bit of wrFlag may optionally be set if BTREE_WRCSR
4091 ** is set. If FORDELETE is set, that is a hint to the implementation that
4092 ** this cursor will only be used to seek to and delete entries of an index
4093 ** as part of a larger DELETE statement. The FORDELETE hint is not used by
4094 ** this implementation. But in a hypothetical alternative storage engine
4095 ** in which index entries are automatically deleted when corresponding table
4096 ** rows are deleted, the FORDELETE flag is a hint that all SEEK and DELETE
4097 ** operations on this cursor can be no-ops and all READ operations can
4098 ** return a null row (2-bytes: 0x01 0x00).
4099 **
4100 ** No checking is done to make sure that page iTable really is the
4101 ** root page of a b-tree. If it is not, then the cursor acquired
4102 ** will not work correctly.
4103 **
4104 ** It is assumed that the sqlite3BtreeCursorZero() has been called
4105 ** on pCur to initialize the memory space prior to invoking this routine.
4106 */
4107 static int btreeCursor(
4108 Btree *p, /* The btree */
4109 int iTable, /* Root page of table to open */
4110 int wrFlag, /* 1 to write. 0 read-only */
4111 struct KeyInfo *pKeyInfo, /* First arg to comparison function */
4112 BtCursor *pCur /* Space for new cursor */
4113 ){
4114 BtShared *pBt = p->pBt; /* Shared b-tree handle */
4115 BtCursor *pX; /* Looping over other all cursors */
4116
4117 assert( sqlite3BtreeHoldsMutex(p) );
4118 assert( wrFlag==0
4119 || wrFlag==BTREE_WRCSR
4120 || wrFlag==(BTREE_WRCSR|BTREE_FORDELETE)
4121 );
4122
4123 /* The following assert statements verify that if this is a sharable
4124 ** b-tree database, the connection is holding the required table locks,
4125 ** and that no other connection has any open cursor that conflicts with
4126 ** this lock. */
4127 assert( hasSharedCacheTableLock(p, iTable, pKeyInfo!=0, (wrFlag?2:1)) );
4128 assert( wrFlag==0 || !hasReadConflicts(p, iTable) );
4129
4130 /* Assert that the caller has opened the required transaction. */
4131 assert( p->inTrans>TRANS_NONE );
4132 assert( wrFlag==0 || p->inTrans==TRANS_WRITE );
4133 assert( pBt->pPage1 && pBt->pPage1->aData );
4134 assert( wrFlag==0 || (pBt->btsFlags & BTS_READ_ONLY)==0 );
4135
4136 if( wrFlag ){
4137 allocateTempSpace(pBt);
4138 if( pBt->pTmpSpace==0 ) return SQLITE_NOMEM_BKPT;
4139 }
4140 if( iTable==1 && btreePagecount(pBt)==0 ){
4141 assert( wrFlag==0 );
4142 iTable = 0;
4143 }
4144
4145 /* Now that no other errors can occur, finish filling in the BtCursor
4146 ** variables and link the cursor into the BtShared list. */
4147 pCur->pgnoRoot = (Pgno)iTable;
4148 pCur->iPage = -1;
4149 pCur->pKeyInfo = pKeyInfo;
4150 pCur->pBtree = p;
4151 pCur->pBt = pBt;
4152 pCur->curFlags = wrFlag ? BTCF_WriteFlag : 0;
4153 pCur->curPagerFlags = wrFlag ? 0 : PAGER_GET_READONLY;
4154 /* If there are two or more cursors on the same btree, then all such
4155 ** cursors *must* have the BTCF_Multiple flag set. */
4156 for(pX=pBt->pCursor; pX; pX=pX->pNext){
4157 if( pX->pgnoRoot==(Pgno)iTable ){
4158 pX->curFlags |= BTCF_Multiple;
4159 pCur->curFlags |= BTCF_Multiple;
4160 }
4161 }
4162 pCur->pNext = pBt->pCursor;
4163 pBt->pCursor = pCur;
4164 pCur->eState = CURSOR_INVALID;
4165 return SQLITE_OK;
4166 }
4167 SQLITE_PRIVATE int sqlite3BtreeCursor(
4168 Btree *p, /* The btree */
4169 int iTable, /* Root page of table to open */
4170 int wrFlag, /* 1 to write. 0 read-only */
4171 struct KeyInfo *pKeyInfo, /* First arg to xCompare() */
4172 BtCursor *pCur /* Write new cursor here */
4173 ){
4174 int rc;
4175 if( iTable<1 ){
4176 rc = SQLITE_CORRUPT_BKPT;
4177 }else{
4178 sqlite3BtreeEnter(p);
4179 rc = btreeCursor(p, iTable, wrFlag, pKeyInfo, pCur);
4180 sqlite3BtreeLeave(p);
4181 }
4182 return rc;
4183 }
4184
4185 /*
4186 ** Return the size of a BtCursor object in bytes.
4187 **
4188 ** This interfaces is needed so that users of cursors can preallocate
4189 ** sufficient storage to hold a cursor. The BtCursor object is opaque
4190 ** to users so they cannot do the sizeof() themselves - they must call
4191 ** this routine.
4192 */
4193 SQLITE_PRIVATE int sqlite3BtreeCursorSize(void){
4194 return ROUND8(sizeof(BtCursor));
4195 }
4196
4197 /*
4198 ** Initialize memory that will be converted into a BtCursor object.
4199 **
4200 ** The simple approach here would be to memset() the entire object
4201 ** to zero. But it turns out that the apPage[] and aiIdx[] arrays
4202 ** do not need to be zeroed and they are large, so we can save a lot
4203 ** of run-time by skipping the initialization of those elements.
4204 */
4205 SQLITE_PRIVATE void sqlite3BtreeCursorZero(BtCursor *p){
4206 memset(p, 0, offsetof(BtCursor, iPage));
4207 }
4208
4209 /*
4210 ** Close a cursor. The read lock on the database file is released
4211 ** when the last cursor is closed.
4212 */
4213 SQLITE_PRIVATE int sqlite3BtreeCloseCursor(BtCursor *pCur){
4214 Btree *pBtree = pCur->pBtree;
4215 if( pBtree ){
4216 int i;
4217 BtShared *pBt = pCur->pBt;
4218 sqlite3BtreeEnter(pBtree);
4219 sqlite3BtreeClearCursor(pCur);
4220 assert( pBt->pCursor!=0 );
4221 if( pBt->pCursor==pCur ){
4222 pBt->pCursor = pCur->pNext;
4223 }else{
4224 BtCursor *pPrev = pBt->pCursor;
4225 do{
4226 if( pPrev->pNext==pCur ){
4227 pPrev->pNext = pCur->pNext;
4228 break;
4229 }
4230 pPrev = pPrev->pNext;
4231 }while( ALWAYS(pPrev) );
4232 }
4233 for(i=0; i<=pCur->iPage; i++){
4234 releasePage(pCur->apPage[i]);
4235 }
4236 unlockBtreeIfUnused(pBt);
4237 sqlite3_free(pCur->aOverflow);
4238 /* sqlite3_free(pCur); */
4239 sqlite3BtreeLeave(pBtree);
4240 }
4241 return SQLITE_OK;
4242 }
4243
4244 /*
4245 ** Make sure the BtCursor* given in the argument has a valid
4246 ** BtCursor.info structure. If it is not already valid, call
4247 ** btreeParseCell() to fill it in.
4248 **
4249 ** BtCursor.info is a cache of the information in the current cell.
4250 ** Using this cache reduces the number of calls to btreeParseCell().
4251 */
4252 #ifndef NDEBUG
4253 static void assertCellInfo(BtCursor *pCur){
4254 CellInfo info;
4255 int iPage = pCur->iPage;
4256 memset(&info, 0, sizeof(info));
4257 btreeParseCell(pCur->apPage[iPage], pCur->aiIdx[iPage], &info);
4258 assert( CORRUPT_DB || memcmp(&info, &pCur->info, sizeof(info))==0 );
4259 }
4260 #else
4261 #define assertCellInfo(x)
4262 #endif
4263 static SQLITE_NOINLINE void getCellInfo(BtCursor *pCur){
4264 if( pCur->info.nSize==0 ){
4265 int iPage = pCur->iPage;
4266 pCur->curFlags |= BTCF_ValidNKey;
4267 btreeParseCell(pCur->apPage[iPage],pCur->aiIdx[iPage],&pCur->info);
4268 }else{
4269 assertCellInfo(pCur);
4270 }
4271 }
4272
4273 #ifndef NDEBUG /* The next routine used only within assert() statements */
4274 /*
4275 ** Return true if the given BtCursor is valid. A valid cursor is one
4276 ** that is currently pointing to a row in a (non-empty) table.
4277 ** This is a verification routine is used only within assert() statements.
4278 */
4279 SQLITE_PRIVATE int sqlite3BtreeCursorIsValid(BtCursor *pCur){
4280 return pCur && pCur->eState==CURSOR_VALID;
4281 }
4282 #endif /* NDEBUG */
4283 SQLITE_PRIVATE int sqlite3BtreeCursorIsValidNN(BtCursor *pCur){
4284 assert( pCur!=0 );
4285 return pCur->eState==CURSOR_VALID;
4286 }
4287
4288 /*
4289 ** Return the value of the integer key or "rowid" for a table btree.
4290 ** This routine is only valid for a cursor that is pointing into a
4291 ** ordinary table btree. If the cursor points to an index btree or
4292 ** is invalid, the result of this routine is undefined.
4293 */
4294 SQLITE_PRIVATE i64 sqlite3BtreeIntegerKey(BtCursor *pCur){
4295 assert( cursorHoldsMutex(pCur) );
4296 assert( pCur->eState==CURSOR_VALID );
4297 assert( pCur->curIntKey );
4298 getCellInfo(pCur);
4299 return pCur->info.nKey;
4300 }
4301
4302 /*
4303 ** Return the number of bytes of payload for the entry that pCur is
4304 ** currently pointing to. For table btrees, this will be the amount
4305 ** of data. For index btrees, this will be the size of the key.
4306 **
4307 ** The caller must guarantee that the cursor is pointing to a non-NULL
4308 ** valid entry. In other words, the calling procedure must guarantee
4309 ** that the cursor has Cursor.eState==CURSOR_VALID.
4310 */
4311 SQLITE_PRIVATE u32 sqlite3BtreePayloadSize(BtCursor *pCur){
4312 assert( cursorHoldsMutex(pCur) );
4313 assert( pCur->eState==CURSOR_VALID );
4314 getCellInfo(pCur);
4315 return pCur->info.nPayload;
4316 }
4317
4318 /*
4319 ** Given the page number of an overflow page in the database (parameter
4320 ** ovfl), this function finds the page number of the next page in the
4321 ** linked list of overflow pages. If possible, it uses the auto-vacuum
4322 ** pointer-map data instead of reading the content of page ovfl to do so.
4323 **
4324 ** If an error occurs an SQLite error code is returned. Otherwise:
4325 **
4326 ** The page number of the next overflow page in the linked list is
4327 ** written to *pPgnoNext. If page ovfl is the last page in its linked
4328 ** list, *pPgnoNext is set to zero.
4329 **
4330 ** If ppPage is not NULL, and a reference to the MemPage object corresponding
4331 ** to page number pOvfl was obtained, then *ppPage is set to point to that
4332 ** reference. It is the responsibility of the caller to call releasePage()
4333 ** on *ppPage to free the reference. In no reference was obtained (because
4334 ** the pointer-map was used to obtain the value for *pPgnoNext), then
4335 ** *ppPage is set to zero.
4336 */
4337 static int getOverflowPage(
4338 BtShared *pBt, /* The database file */
4339 Pgno ovfl, /* Current overflow page number */
4340 MemPage **ppPage, /* OUT: MemPage handle (may be NULL) */
4341 Pgno *pPgnoNext /* OUT: Next overflow page number */
4342 ){
4343 Pgno next = 0;
4344 MemPage *pPage = 0;
4345 int rc = SQLITE_OK;
4346
4347 assert( sqlite3_mutex_held(pBt->mutex) );
4348 assert(pPgnoNext);
4349
4350 #ifndef SQLITE_OMIT_AUTOVACUUM
4351 /* Try to find the next page in the overflow list using the
4352 ** autovacuum pointer-map pages. Guess that the next page in
4353 ** the overflow list is page number (ovfl+1). If that guess turns
4354 ** out to be wrong, fall back to loading the data of page
4355 ** number ovfl to determine the next page number.
4356 */
4357 if( pBt->autoVacuum ){
4358 Pgno pgno;
4359 Pgno iGuess = ovfl+1;
4360 u8 eType;
4361
4362 while( PTRMAP_ISPAGE(pBt, iGuess) || iGuess==PENDING_BYTE_PAGE(pBt) ){
4363 iGuess++;
4364 }
4365
4366 if( iGuess<=btreePagecount(pBt) ){
4367 rc = ptrmapGet(pBt, iGuess, &eType, &pgno);
4368 if( rc==SQLITE_OK && eType==PTRMAP_OVERFLOW2 && pgno==ovfl ){
4369 next = iGuess;
4370 rc = SQLITE_DONE;
4371 }
4372 }
4373 }
4374 #endif
4375
4376 assert( next==0 || rc==SQLITE_DONE );
4377 if( rc==SQLITE_OK ){
4378 rc = btreeGetPage(pBt, ovfl, &pPage, (ppPage==0) ? PAGER_GET_READONLY : 0);
4379 assert( rc==SQLITE_OK || pPage==0 );
4380 if( rc==SQLITE_OK ){
4381 next = get4byte(pPage->aData);
4382 }
4383 }
4384
4385 *pPgnoNext = next;
4386 if( ppPage ){
4387 *ppPage = pPage;
4388 }else{
4389 releasePage(pPage);
4390 }
4391 return (rc==SQLITE_DONE ? SQLITE_OK : rc);
4392 }
4393
4394 /*
4395 ** Copy data from a buffer to a page, or from a page to a buffer.
4396 **
4397 ** pPayload is a pointer to data stored on database page pDbPage.
4398 ** If argument eOp is false, then nByte bytes of data are copied
4399 ** from pPayload to the buffer pointed at by pBuf. If eOp is true,
4400 ** then sqlite3PagerWrite() is called on pDbPage and nByte bytes
4401 ** of data are copied from the buffer pBuf to pPayload.
4402 **
4403 ** SQLITE_OK is returned on success, otherwise an error code.
4404 */
4405 static int copyPayload(
4406 void *pPayload, /* Pointer to page data */
4407 void *pBuf, /* Pointer to buffer */
4408 int nByte, /* Number of bytes to copy */
4409 int eOp, /* 0 -> copy from page, 1 -> copy to page */
4410 DbPage *pDbPage /* Page containing pPayload */
4411 ){
4412 if( eOp ){
4413 /* Copy data from buffer to page (a write operation) */
4414 int rc = sqlite3PagerWrite(pDbPage);
4415 if( rc!=SQLITE_OK ){
4416 return rc;
4417 }
4418 memcpy(pPayload, pBuf, nByte);
4419 }else{
4420 /* Copy data from page to buffer (a read operation) */
4421 memcpy(pBuf, pPayload, nByte);
4422 }
4423 return SQLITE_OK;
4424 }
4425
4426 /*
4427 ** This function is used to read or overwrite payload information
4428 ** for the entry that the pCur cursor is pointing to. The eOp
4429 ** argument is interpreted as follows:
4430 **
4431 ** 0: The operation is a read. Populate the overflow cache.
4432 ** 1: The operation is a write. Populate the overflow cache.
4433 **
4434 ** A total of "amt" bytes are read or written beginning at "offset".
4435 ** Data is read to or from the buffer pBuf.
4436 **
4437 ** The content being read or written might appear on the main page
4438 ** or be scattered out on multiple overflow pages.
4439 **
4440 ** If the current cursor entry uses one or more overflow pages
4441 ** this function may allocate space for and lazily populate
4442 ** the overflow page-list cache array (BtCursor.aOverflow).
4443 ** Subsequent calls use this cache to make seeking to the supplied offset
4444 ** more efficient.
4445 **
4446 ** Once an overflow page-list cache has been allocated, it must be
4447 ** invalidated if some other cursor writes to the same table, or if
4448 ** the cursor is moved to a different row. Additionally, in auto-vacuum
4449 ** mode, the following events may invalidate an overflow page-list cache.
4450 **
4451 ** * An incremental vacuum,
4452 ** * A commit in auto_vacuum="full" mode,
4453 ** * Creating a table (may require moving an overflow page).
4454 */
4455 static int accessPayload(
4456 BtCursor *pCur, /* Cursor pointing to entry to read from */
4457 u32 offset, /* Begin reading this far into payload */
4458 u32 amt, /* Read this many bytes */
4459 unsigned char *pBuf, /* Write the bytes into this buffer */
4460 int eOp /* zero to read. non-zero to write. */
4461 ){
4462 unsigned char *aPayload;
4463 int rc = SQLITE_OK;
4464 int iIdx = 0;
4465 MemPage *pPage = pCur->apPage[pCur->iPage]; /* Btree page of current entry */
4466 BtShared *pBt = pCur->pBt; /* Btree this cursor belongs to */
4467 #ifdef SQLITE_DIRECT_OVERFLOW_READ
4468 unsigned char * const pBufStart = pBuf; /* Start of original out buffer */
4469 #endif
4470
4471 assert( pPage );
4472 assert( eOp==0 || eOp==1 );
4473 assert( pCur->eState==CURSOR_VALID );
4474 assert( pCur->aiIdx[pCur->iPage]<pPage->nCell );
4475 assert( cursorHoldsMutex(pCur) );
4476
4477 getCellInfo(pCur);
4478 aPayload = pCur->info.pPayload;
4479 assert( offset+amt <= pCur->info.nPayload );
4480
4481 assert( aPayload > pPage->aData );
4482 if( (uptr)(aPayload - pPage->aData) > (pBt->usableSize - pCur->info.nLocal) ){
4483 /* Trying to read or write past the end of the data is an error. The
4484 ** conditional above is really:
4485 ** &aPayload[pCur->info.nLocal] > &pPage->aData[pBt->usableSize]
4486 ** but is recast into its current form to avoid integer overflow problems
4487 */
4488 return SQLITE_CORRUPT_BKPT;
4489 }
4490
4491 /* Check if data must be read/written to/from the btree page itself. */
4492 if( offset<pCur->info.nLocal ){
4493 int a = amt;
4494 if( a+offset>pCur->info.nLocal ){
4495 a = pCur->info.nLocal - offset;
4496 }
4497 rc = copyPayload(&aPayload[offset], pBuf, a, eOp, pPage->pDbPage);
4498 offset = 0;
4499 pBuf += a;
4500 amt -= a;
4501 }else{
4502 offset -= pCur->info.nLocal;
4503 }
4504
4505
4506 if( rc==SQLITE_OK && amt>0 ){
4507 const u32 ovflSize = pBt->usableSize - 4; /* Bytes content per ovfl page */
4508 Pgno nextPage;
4509
4510 nextPage = get4byte(&aPayload[pCur->info.nLocal]);
4511
4512 /* If the BtCursor.aOverflow[] has not been allocated, allocate it now.
4513 **
4514 ** The aOverflow[] array is sized at one entry for each overflow page
4515 ** in the overflow chain. The page number of the first overflow page is
4516 ** stored in aOverflow[0], etc. A value of 0 in the aOverflow[] array
4517 ** means "not yet known" (the cache is lazily populated).
4518 */
4519 if( (pCur->curFlags & BTCF_ValidOvfl)==0 ){
4520 int nOvfl = (pCur->info.nPayload-pCur->info.nLocal+ovflSize-1)/ovflSize;
4521 if( nOvfl>pCur->nOvflAlloc ){
4522 Pgno *aNew = (Pgno*)sqlite3Realloc(
4523 pCur->aOverflow, nOvfl*2*sizeof(Pgno)
4524 );
4525 if( aNew==0 ){
4526 return SQLITE_NOMEM_BKPT;
4527 }else{
4528 pCur->nOvflAlloc = nOvfl*2;
4529 pCur->aOverflow = aNew;
4530 }
4531 }
4532 memset(pCur->aOverflow, 0, nOvfl*sizeof(Pgno));
4533 pCur->curFlags |= BTCF_ValidOvfl;
4534 }else{
4535 /* If the overflow page-list cache has been allocated and the
4536 ** entry for the first required overflow page is valid, skip
4537 ** directly to it.
4538 */
4539 if( pCur->aOverflow[offset/ovflSize] ){
4540 iIdx = (offset/ovflSize);
4541 nextPage = pCur->aOverflow[iIdx];
4542 offset = (offset%ovflSize);
4543 }
4544 }
4545
4546 assert( rc==SQLITE_OK && amt>0 );
4547 while( nextPage ){
4548 /* If required, populate the overflow page-list cache. */
4549 assert( pCur->aOverflow[iIdx]==0
4550 || pCur->aOverflow[iIdx]==nextPage
4551 || CORRUPT_DB );
4552 pCur->aOverflow[iIdx] = nextPage;
4553
4554 if( offset>=ovflSize ){
4555 /* The only reason to read this page is to obtain the page
4556 ** number for the next page in the overflow chain. The page
4557 ** data is not required. So first try to lookup the overflow
4558 ** page-list cache, if any, then fall back to the getOverflowPage()
4559 ** function.
4560 */
4561 assert( pCur->curFlags & BTCF_ValidOvfl );
4562 assert( pCur->pBtree->db==pBt->db );
4563 if( pCur->aOverflow[iIdx+1] ){
4564 nextPage = pCur->aOverflow[iIdx+1];
4565 }else{
4566 rc = getOverflowPage(pBt, nextPage, 0, &nextPage);
4567 }
4568 offset -= ovflSize;
4569 }else{
4570 /* Need to read this page properly. It contains some of the
4571 ** range of data that is being read (eOp==0) or written (eOp!=0).
4572 */
4573 #ifdef SQLITE_DIRECT_OVERFLOW_READ
4574 sqlite3_file *fd; /* File from which to do direct overflow read */
4575 #endif
4576 int a = amt;
4577 if( a + offset > ovflSize ){
4578 a = ovflSize - offset;
4579 }
4580
4581 #ifdef SQLITE_DIRECT_OVERFLOW_READ
4582 /* If all the following are true:
4583 **
4584 ** 1) this is a read operation, and
4585 ** 2) data is required from the start of this overflow page, and
4586 ** 3) there is no open write-transaction, and
4587 ** 4) the database is file-backed, and
4588 ** 5) the page is not in the WAL file
4589 ** 6) at least 4 bytes have already been read into the output buffer
4590 **
4591 ** then data can be read directly from the database file into the
4592 ** output buffer, bypassing the page-cache altogether. This speeds
4593 ** up loading large records that span many overflow pages.
4594 */
4595 if( eOp==0 /* (1) */
4596 && offset==0 /* (2) */
4597 && pBt->inTransaction==TRANS_READ /* (3) */
4598 && (fd = sqlite3PagerFile(pBt->pPager))->pMethods /* (4) */
4599 && 0==sqlite3PagerUseWal(pBt->pPager, nextPage) /* (5) */
4600 && &pBuf[-4]>=pBufStart /* (6) */
4601 ){
4602 u8 aSave[4];
4603 u8 *aWrite = &pBuf[-4];
4604 assert( aWrite>=pBufStart ); /* due to (6) */
4605 memcpy(aSave, aWrite, 4);
4606 rc = sqlite3OsRead(fd, aWrite, a+4, (i64)pBt->pageSize*(nextPage-1));
4607 nextPage = get4byte(aWrite);
4608 memcpy(aWrite, aSave, 4);
4609 }else
4610 #endif
4611
4612 {
4613 DbPage *pDbPage;
4614 rc = sqlite3PagerGet(pBt->pPager, nextPage, &pDbPage,
4615 (eOp==0 ? PAGER_GET_READONLY : 0)
4616 );
4617 if( rc==SQLITE_OK ){
4618 aPayload = sqlite3PagerGetData(pDbPage);
4619 nextPage = get4byte(aPayload);
4620 rc = copyPayload(&aPayload[offset+4], pBuf, a, eOp, pDbPage);
4621 sqlite3PagerUnref(pDbPage);
4622 offset = 0;
4623 }
4624 }
4625 amt -= a;
4626 if( amt==0 ) return rc;
4627 pBuf += a;
4628 }
4629 if( rc ) break;
4630 iIdx++;
4631 }
4632 }
4633
4634 if( rc==SQLITE_OK && amt>0 ){
4635 return SQLITE_CORRUPT_BKPT; /* Overflow chain ends prematurely */
4636 }
4637 return rc;
4638 }
4639
4640 /*
4641 ** Read part of the payload for the row at which that cursor pCur is currently
4642 ** pointing. "amt" bytes will be transferred into pBuf[]. The transfer
4643 ** begins at "offset".
4644 **
4645 ** pCur can be pointing to either a table or an index b-tree.
4646 ** If pointing to a table btree, then the content section is read. If
4647 ** pCur is pointing to an index b-tree then the key section is read.
4648 **
4649 ** For sqlite3BtreePayload(), the caller must ensure that pCur is pointing
4650 ** to a valid row in the table. For sqlite3BtreePayloadChecked(), the
4651 ** cursor might be invalid or might need to be restored before being read.
4652 **
4653 ** Return SQLITE_OK on success or an error code if anything goes
4654 ** wrong. An error is returned if "offset+amt" is larger than
4655 ** the available payload.
4656 */
4657 SQLITE_PRIVATE int sqlite3BtreePayload(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
4658 assert( cursorHoldsMutex(pCur) );
4659 assert( pCur->eState==CURSOR_VALID );
4660 assert( pCur->iPage>=0 && pCur->apPage[pCur->iPage] );
4661 assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell );
4662 return accessPayload(pCur, offset, amt, (unsigned char*)pBuf, 0);
4663 }
4664
4665 /*
4666 ** This variant of sqlite3BtreePayload() works even if the cursor has not
4667 ** in the CURSOR_VALID state. It is only used by the sqlite3_blob_read()
4668 ** interface.
4669 */
4670 #ifndef SQLITE_OMIT_INCRBLOB
4671 static SQLITE_NOINLINE int accessPayloadChecked(
4672 BtCursor *pCur,
4673 u32 offset,
4674 u32 amt,
4675 void *pBuf
4676 ){
4677 int rc;
4678 if ( pCur->eState==CURSOR_INVALID ){
4679 return SQLITE_ABORT;
4680 }
4681 assert( cursorOwnsBtShared(pCur) );
4682 rc = btreeRestoreCursorPosition(pCur);
4683 return rc ? rc : accessPayload(pCur, offset, amt, pBuf, 0);
4684 }
4685 SQLITE_PRIVATE int sqlite3BtreePayloadChecked(BtCursor *pCur, u32 offset, u32 am t, void *pBuf){
4686 if( pCur->eState==CURSOR_VALID ){
4687 assert( cursorOwnsBtShared(pCur) );
4688 return accessPayload(pCur, offset, amt, pBuf, 0);
4689 }else{
4690 return accessPayloadChecked(pCur, offset, amt, pBuf);
4691 }
4692 }
4693 #endif /* SQLITE_OMIT_INCRBLOB */
4694
4695 /*
4696 ** Return a pointer to payload information from the entry that the
4697 ** pCur cursor is pointing to. The pointer is to the beginning of
4698 ** the key if index btrees (pPage->intKey==0) and is the data for
4699 ** table btrees (pPage->intKey==1). The number of bytes of available
4700 ** key/data is written into *pAmt. If *pAmt==0, then the value
4701 ** returned will not be a valid pointer.
4702 **
4703 ** This routine is an optimization. It is common for the entire key
4704 ** and data to fit on the local page and for there to be no overflow
4705 ** pages. When that is so, this routine can be used to access the
4706 ** key and data without making a copy. If the key and/or data spills
4707 ** onto overflow pages, then accessPayload() must be used to reassemble
4708 ** the key/data and copy it into a preallocated buffer.
4709 **
4710 ** The pointer returned by this routine looks directly into the cached
4711 ** page of the database. The data might change or move the next time
4712 ** any btree routine is called.
4713 */
4714 static const void *fetchPayload(
4715 BtCursor *pCur, /* Cursor pointing to entry to read from */
4716 u32 *pAmt /* Write the number of available bytes here */
4717 ){
4718 u32 amt;
4719 assert( pCur!=0 && pCur->iPage>=0 && pCur->apPage[pCur->iPage]);
4720 assert( pCur->eState==CURSOR_VALID );
4721 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
4722 assert( cursorOwnsBtShared(pCur) );
4723 assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell );
4724 assert( pCur->info.nSize>0 );
4725 assert( pCur->info.pPayload>pCur->apPage[pCur->iPage]->aData || CORRUPT_DB );
4726 assert( pCur->info.pPayload<pCur->apPage[pCur->iPage]->aDataEnd ||CORRUPT_DB);
4727 amt = (int)(pCur->apPage[pCur->iPage]->aDataEnd - pCur->info.pPayload);
4728 if( pCur->info.nLocal<amt ) amt = pCur->info.nLocal;
4729 *pAmt = amt;
4730 return (void*)pCur->info.pPayload;
4731 }
4732
4733
4734 /*
4735 ** For the entry that cursor pCur is point to, return as
4736 ** many bytes of the key or data as are available on the local
4737 ** b-tree page. Write the number of available bytes into *pAmt.
4738 **
4739 ** The pointer returned is ephemeral. The key/data may move
4740 ** or be destroyed on the next call to any Btree routine,
4741 ** including calls from other threads against the same cache.
4742 ** Hence, a mutex on the BtShared should be held prior to calling
4743 ** this routine.
4744 **
4745 ** These routines is used to get quick access to key and data
4746 ** in the common case where no overflow pages are used.
4747 */
4748 SQLITE_PRIVATE const void *sqlite3BtreePayloadFetch(BtCursor *pCur, u32 *pAmt){
4749 return fetchPayload(pCur, pAmt);
4750 }
4751
4752
4753 /*
4754 ** Move the cursor down to a new child page. The newPgno argument is the
4755 ** page number of the child page to move to.
4756 **
4757 ** This function returns SQLITE_CORRUPT if the page-header flags field of
4758 ** the new child page does not match the flags field of the parent (i.e.
4759 ** if an intkey page appears to be the parent of a non-intkey page, or
4760 ** vice-versa).
4761 */
4762 static int moveToChild(BtCursor *pCur, u32 newPgno){
4763 BtShared *pBt = pCur->pBt;
4764
4765 assert( cursorOwnsBtShared(pCur) );
4766 assert( pCur->eState==CURSOR_VALID );
4767 assert( pCur->iPage<BTCURSOR_MAX_DEPTH );
4768 assert( pCur->iPage>=0 );
4769 if( pCur->iPage>=(BTCURSOR_MAX_DEPTH-1) ){
4770 return SQLITE_CORRUPT_BKPT;
4771 }
4772 pCur->info.nSize = 0;
4773 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
4774 pCur->iPage++;
4775 pCur->aiIdx[pCur->iPage] = 0;
4776 return getAndInitPage(pBt, newPgno, &pCur->apPage[pCur->iPage],
4777 pCur, pCur->curPagerFlags);
4778 }
4779
4780 #if SQLITE_DEBUG
4781 /*
4782 ** Page pParent is an internal (non-leaf) tree page. This function
4783 ** asserts that page number iChild is the left-child if the iIdx'th
4784 ** cell in page pParent. Or, if iIdx is equal to the total number of
4785 ** cells in pParent, that page number iChild is the right-child of
4786 ** the page.
4787 */
4788 static void assertParentIndex(MemPage *pParent, int iIdx, Pgno iChild){
4789 if( CORRUPT_DB ) return; /* The conditions tested below might not be true
4790 ** in a corrupt database */
4791 assert( iIdx<=pParent->nCell );
4792 if( iIdx==pParent->nCell ){
4793 assert( get4byte(&pParent->aData[pParent->hdrOffset+8])==iChild );
4794 }else{
4795 assert( get4byte(findCell(pParent, iIdx))==iChild );
4796 }
4797 }
4798 #else
4799 # define assertParentIndex(x,y,z)
4800 #endif
4801
4802 /*
4803 ** Move the cursor up to the parent page.
4804 **
4805 ** pCur->idx is set to the cell index that contains the pointer
4806 ** to the page we are coming from. If we are coming from the
4807 ** right-most child page then pCur->idx is set to one more than
4808 ** the largest cell index.
4809 */
4810 static void moveToParent(BtCursor *pCur){
4811 assert( cursorOwnsBtShared(pCur) );
4812 assert( pCur->eState==CURSOR_VALID );
4813 assert( pCur->iPage>0 );
4814 assert( pCur->apPage[pCur->iPage] );
4815 assertParentIndex(
4816 pCur->apPage[pCur->iPage-1],
4817 pCur->aiIdx[pCur->iPage-1],
4818 pCur->apPage[pCur->iPage]->pgno
4819 );
4820 testcase( pCur->aiIdx[pCur->iPage-1] > pCur->apPage[pCur->iPage-1]->nCell );
4821 pCur->info.nSize = 0;
4822 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
4823 releasePageNotNull(pCur->apPage[pCur->iPage--]);
4824 }
4825
4826 /*
4827 ** Move the cursor to point to the root page of its b-tree structure.
4828 **
4829 ** If the table has a virtual root page, then the cursor is moved to point
4830 ** to the virtual root page instead of the actual root page. A table has a
4831 ** virtual root page when the actual root page contains no cells and a
4832 ** single child page. This can only happen with the table rooted at page 1.
4833 **
4834 ** If the b-tree structure is empty, the cursor state is set to
4835 ** CURSOR_INVALID. Otherwise, the cursor is set to point to the first
4836 ** cell located on the root (or virtual root) page and the cursor state
4837 ** is set to CURSOR_VALID.
4838 **
4839 ** If this function returns successfully, it may be assumed that the
4840 ** page-header flags indicate that the [virtual] root-page is the expected
4841 ** kind of b-tree page (i.e. if when opening the cursor the caller did not
4842 ** specify a KeyInfo structure the flags byte is set to 0x05 or 0x0D,
4843 ** indicating a table b-tree, or if the caller did specify a KeyInfo
4844 ** structure the flags byte is set to 0x02 or 0x0A, indicating an index
4845 ** b-tree).
4846 */
4847 static int moveToRoot(BtCursor *pCur){
4848 MemPage *pRoot;
4849 int rc = SQLITE_OK;
4850
4851 assert( cursorOwnsBtShared(pCur) );
4852 assert( CURSOR_INVALID < CURSOR_REQUIRESEEK );
4853 assert( CURSOR_VALID < CURSOR_REQUIRESEEK );
4854 assert( CURSOR_FAULT > CURSOR_REQUIRESEEK );
4855 if( pCur->eState>=CURSOR_REQUIRESEEK ){
4856 if( pCur->eState==CURSOR_FAULT ){
4857 assert( pCur->skipNext!=SQLITE_OK );
4858 return pCur->skipNext;
4859 }
4860 sqlite3BtreeClearCursor(pCur);
4861 }
4862
4863 if( pCur->iPage>=0 ){
4864 if( pCur->iPage ){
4865 do{
4866 assert( pCur->apPage[pCur->iPage]!=0 );
4867 releasePageNotNull(pCur->apPage[pCur->iPage--]);
4868 }while( pCur->iPage);
4869 goto skip_init;
4870 }
4871 }else if( pCur->pgnoRoot==0 ){
4872 pCur->eState = CURSOR_INVALID;
4873 return SQLITE_OK;
4874 }else{
4875 assert( pCur->iPage==(-1) );
4876 rc = getAndInitPage(pCur->pBtree->pBt, pCur->pgnoRoot, &pCur->apPage[0],
4877 0, pCur->curPagerFlags);
4878 if( rc!=SQLITE_OK ){
4879 pCur->eState = CURSOR_INVALID;
4880 return rc;
4881 }
4882 pCur->iPage = 0;
4883 pCur->curIntKey = pCur->apPage[0]->intKey;
4884 }
4885 pRoot = pCur->apPage[0];
4886 assert( pRoot->pgno==pCur->pgnoRoot );
4887
4888 /* If pCur->pKeyInfo is not NULL, then the caller that opened this cursor
4889 ** expected to open it on an index b-tree. Otherwise, if pKeyInfo is
4890 ** NULL, the caller expects a table b-tree. If this is not the case,
4891 ** return an SQLITE_CORRUPT error.
4892 **
4893 ** Earlier versions of SQLite assumed that this test could not fail
4894 ** if the root page was already loaded when this function was called (i.e.
4895 ** if pCur->iPage>=0). But this is not so if the database is corrupted
4896 ** in such a way that page pRoot is linked into a second b-tree table
4897 ** (or the freelist). */
4898 assert( pRoot->intKey==1 || pRoot->intKey==0 );
4899 if( pRoot->isInit==0 || (pCur->pKeyInfo==0)!=pRoot->intKey ){
4900 return SQLITE_CORRUPT_BKPT;
4901 }
4902
4903 skip_init:
4904 pCur->aiIdx[0] = 0;
4905 pCur->info.nSize = 0;
4906 pCur->curFlags &= ~(BTCF_AtLast|BTCF_ValidNKey|BTCF_ValidOvfl);
4907
4908 pRoot = pCur->apPage[0];
4909 if( pRoot->nCell>0 ){
4910 pCur->eState = CURSOR_VALID;
4911 }else if( !pRoot->leaf ){
4912 Pgno subpage;
4913 if( pRoot->pgno!=1 ) return SQLITE_CORRUPT_BKPT;
4914 subpage = get4byte(&pRoot->aData[pRoot->hdrOffset+8]);
4915 pCur->eState = CURSOR_VALID;
4916 rc = moveToChild(pCur, subpage);
4917 }else{
4918 pCur->eState = CURSOR_INVALID;
4919 }
4920 return rc;
4921 }
4922
4923 /*
4924 ** Move the cursor down to the left-most leaf entry beneath the
4925 ** entry to which it is currently pointing.
4926 **
4927 ** The left-most leaf is the one with the smallest key - the first
4928 ** in ascending order.
4929 */
4930 static int moveToLeftmost(BtCursor *pCur){
4931 Pgno pgno;
4932 int rc = SQLITE_OK;
4933 MemPage *pPage;
4934
4935 assert( cursorOwnsBtShared(pCur) );
4936 assert( pCur->eState==CURSOR_VALID );
4937 while( rc==SQLITE_OK && !(pPage = pCur->apPage[pCur->iPage])->leaf ){
4938 assert( pCur->aiIdx[pCur->iPage]<pPage->nCell );
4939 pgno = get4byte(findCell(pPage, pCur->aiIdx[pCur->iPage]));
4940 rc = moveToChild(pCur, pgno);
4941 }
4942 return rc;
4943 }
4944
4945 /*
4946 ** Move the cursor down to the right-most leaf entry beneath the
4947 ** page to which it is currently pointing. Notice the difference
4948 ** between moveToLeftmost() and moveToRightmost(). moveToLeftmost()
4949 ** finds the left-most entry beneath the *entry* whereas moveToRightmost()
4950 ** finds the right-most entry beneath the *page*.
4951 **
4952 ** The right-most entry is the one with the largest key - the last
4953 ** key in ascending order.
4954 */
4955 static int moveToRightmost(BtCursor *pCur){
4956 Pgno pgno;
4957 int rc = SQLITE_OK;
4958 MemPage *pPage = 0;
4959
4960 assert( cursorOwnsBtShared(pCur) );
4961 assert( pCur->eState==CURSOR_VALID );
4962 while( !(pPage = pCur->apPage[pCur->iPage])->leaf ){
4963 pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
4964 pCur->aiIdx[pCur->iPage] = pPage->nCell;
4965 rc = moveToChild(pCur, pgno);
4966 if( rc ) return rc;
4967 }
4968 pCur->aiIdx[pCur->iPage] = pPage->nCell-1;
4969 assert( pCur->info.nSize==0 );
4970 assert( (pCur->curFlags & BTCF_ValidNKey)==0 );
4971 return SQLITE_OK;
4972 }
4973
4974 /* Move the cursor to the first entry in the table. Return SQLITE_OK
4975 ** on success. Set *pRes to 0 if the cursor actually points to something
4976 ** or set *pRes to 1 if the table is empty.
4977 */
4978 SQLITE_PRIVATE int sqlite3BtreeFirst(BtCursor *pCur, int *pRes){
4979 int rc;
4980
4981 assert( cursorOwnsBtShared(pCur) );
4982 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
4983 rc = moveToRoot(pCur);
4984 if( rc==SQLITE_OK ){
4985 if( pCur->eState==CURSOR_INVALID ){
4986 assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->nCell==0 );
4987 *pRes = 1;
4988 }else{
4989 assert( pCur->apPage[pCur->iPage]->nCell>0 );
4990 *pRes = 0;
4991 rc = moveToLeftmost(pCur);
4992 }
4993 }
4994 return rc;
4995 }
4996
4997 /* Move the cursor to the last entry in the table. Return SQLITE_OK
4998 ** on success. Set *pRes to 0 if the cursor actually points to something
4999 ** or set *pRes to 1 if the table is empty.
5000 */
5001 SQLITE_PRIVATE int sqlite3BtreeLast(BtCursor *pCur, int *pRes){
5002 int rc;
5003
5004 assert( cursorOwnsBtShared(pCur) );
5005 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
5006
5007 /* If the cursor already points to the last entry, this is a no-op. */
5008 if( CURSOR_VALID==pCur->eState && (pCur->curFlags & BTCF_AtLast)!=0 ){
5009 #ifdef SQLITE_DEBUG
5010 /* This block serves to assert() that the cursor really does point
5011 ** to the last entry in the b-tree. */
5012 int ii;
5013 for(ii=0; ii<pCur->iPage; ii++){
5014 assert( pCur->aiIdx[ii]==pCur->apPage[ii]->nCell );
5015 }
5016 assert( pCur->aiIdx[pCur->iPage]==pCur->apPage[pCur->iPage]->nCell-1 );
5017 assert( pCur->apPage[pCur->iPage]->leaf );
5018 #endif
5019 return SQLITE_OK;
5020 }
5021
5022 rc = moveToRoot(pCur);
5023 if( rc==SQLITE_OK ){
5024 if( CURSOR_INVALID==pCur->eState ){
5025 assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->nCell==0 );
5026 *pRes = 1;
5027 }else{
5028 assert( pCur->eState==CURSOR_VALID );
5029 *pRes = 0;
5030 rc = moveToRightmost(pCur);
5031 if( rc==SQLITE_OK ){
5032 pCur->curFlags |= BTCF_AtLast;
5033 }else{
5034 pCur->curFlags &= ~BTCF_AtLast;
5035 }
5036
5037 }
5038 }
5039 return rc;
5040 }
5041
5042 /* Move the cursor so that it points to an entry near the key
5043 ** specified by pIdxKey or intKey. Return a success code.
5044 **
5045 ** For INTKEY tables, the intKey parameter is used. pIdxKey
5046 ** must be NULL. For index tables, pIdxKey is used and intKey
5047 ** is ignored.
5048 **
5049 ** If an exact match is not found, then the cursor is always
5050 ** left pointing at a leaf page which would hold the entry if it
5051 ** were present. The cursor might point to an entry that comes
5052 ** before or after the key.
5053 **
5054 ** An integer is written into *pRes which is the result of
5055 ** comparing the key with the entry to which the cursor is
5056 ** pointing. The meaning of the integer written into
5057 ** *pRes is as follows:
5058 **
5059 ** *pRes<0 The cursor is left pointing at an entry that
5060 ** is smaller than intKey/pIdxKey or if the table is empty
5061 ** and the cursor is therefore left point to nothing.
5062 **
5063 ** *pRes==0 The cursor is left pointing at an entry that
5064 ** exactly matches intKey/pIdxKey.
5065 **
5066 ** *pRes>0 The cursor is left pointing at an entry that
5067 ** is larger than intKey/pIdxKey.
5068 **
5069 ** For index tables, the pIdxKey->eqSeen field is set to 1 if there
5070 ** exists an entry in the table that exactly matches pIdxKey.
5071 */
5072 SQLITE_PRIVATE int sqlite3BtreeMovetoUnpacked(
5073 BtCursor *pCur, /* The cursor to be moved */
5074 UnpackedRecord *pIdxKey, /* Unpacked index key */
5075 i64 intKey, /* The table key */
5076 int biasRight, /* If true, bias the search to the high end */
5077 int *pRes /* Write search results here */
5078 ){
5079 int rc;
5080 RecordCompare xRecordCompare;
5081
5082 assert( cursorOwnsBtShared(pCur) );
5083 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
5084 assert( pRes );
5085 assert( (pIdxKey==0)==(pCur->pKeyInfo==0) );
5086 assert( pCur->eState!=CURSOR_VALID || (pIdxKey==0)==(pCur->curIntKey!=0) );
5087
5088 /* If the cursor is already positioned at the point we are trying
5089 ** to move to, then just return without doing any work */
5090 if( pIdxKey==0
5091 && pCur->eState==CURSOR_VALID && (pCur->curFlags & BTCF_ValidNKey)!=0
5092 ){
5093 if( pCur->info.nKey==intKey ){
5094 *pRes = 0;
5095 return SQLITE_OK;
5096 }
5097 if( pCur->info.nKey<intKey ){
5098 if( (pCur->curFlags & BTCF_AtLast)!=0 ){
5099 *pRes = -1;
5100 return SQLITE_OK;
5101 }
5102 /* If the requested key is one more than the previous key, then
5103 ** try to get there using sqlite3BtreeNext() rather than a full
5104 ** binary search. This is an optimization only. The correct answer
5105 ** is still obtained without this ase, only a little more slowely */
5106 if( pCur->info.nKey+1==intKey && !pCur->skipNext ){
5107 *pRes = 0;
5108 rc = sqlite3BtreeNext(pCur, pRes);
5109 if( rc ) return rc;
5110 if( *pRes==0 ){
5111 getCellInfo(pCur);
5112 if( pCur->info.nKey==intKey ){
5113 return SQLITE_OK;
5114 }
5115 }
5116 }
5117 }
5118 }
5119
5120 if( pIdxKey ){
5121 xRecordCompare = sqlite3VdbeFindCompare(pIdxKey);
5122 pIdxKey->errCode = 0;
5123 assert( pIdxKey->default_rc==1
5124 || pIdxKey->default_rc==0
5125 || pIdxKey->default_rc==-1
5126 );
5127 }else{
5128 xRecordCompare = 0; /* All keys are integers */
5129 }
5130
5131 rc = moveToRoot(pCur);
5132 if( rc ){
5133 return rc;
5134 }
5135 assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage] );
5136 assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->isInit );
5137 assert( pCur->eState==CURSOR_INVALID || pCur->apPage[pCur->iPage]->nCell>0 );
5138 if( pCur->eState==CURSOR_INVALID ){
5139 *pRes = -1;
5140 assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->nCell==0 );
5141 return SQLITE_OK;
5142 }
5143 assert( pCur->apPage[0]->intKey==pCur->curIntKey );
5144 assert( pCur->curIntKey || pIdxKey );
5145 for(;;){
5146 int lwr, upr, idx, c;
5147 Pgno chldPg;
5148 MemPage *pPage = pCur->apPage[pCur->iPage];
5149 u8 *pCell; /* Pointer to current cell in pPage */
5150
5151 /* pPage->nCell must be greater than zero. If this is the root-page
5152 ** the cursor would have been INVALID above and this for(;;) loop
5153 ** not run. If this is not the root-page, then the moveToChild() routine
5154 ** would have already detected db corruption. Similarly, pPage must
5155 ** be the right kind (index or table) of b-tree page. Otherwise
5156 ** a moveToChild() or moveToRoot() call would have detected corruption. */
5157 assert( pPage->nCell>0 );
5158 assert( pPage->intKey==(pIdxKey==0) );
5159 lwr = 0;
5160 upr = pPage->nCell-1;
5161 assert( biasRight==0 || biasRight==1 );
5162 idx = upr>>(1-biasRight); /* idx = biasRight ? upr : (lwr+upr)/2; */
5163 pCur->aiIdx[pCur->iPage] = (u16)idx;
5164 if( xRecordCompare==0 ){
5165 for(;;){
5166 i64 nCellKey;
5167 pCell = findCellPastPtr(pPage, idx);
5168 if( pPage->intKeyLeaf ){
5169 while( 0x80 <= *(pCell++) ){
5170 if( pCell>=pPage->aDataEnd ) return SQLITE_CORRUPT_BKPT;
5171 }
5172 }
5173 getVarint(pCell, (u64*)&nCellKey);
5174 if( nCellKey<intKey ){
5175 lwr = idx+1;
5176 if( lwr>upr ){ c = -1; break; }
5177 }else if( nCellKey>intKey ){
5178 upr = idx-1;
5179 if( lwr>upr ){ c = +1; break; }
5180 }else{
5181 assert( nCellKey==intKey );
5182 pCur->aiIdx[pCur->iPage] = (u16)idx;
5183 if( !pPage->leaf ){
5184 lwr = idx;
5185 goto moveto_next_layer;
5186 }else{
5187 pCur->curFlags |= BTCF_ValidNKey;
5188 pCur->info.nKey = nCellKey;
5189 pCur->info.nSize = 0;
5190 *pRes = 0;
5191 return SQLITE_OK;
5192 }
5193 }
5194 assert( lwr+upr>=0 );
5195 idx = (lwr+upr)>>1; /* idx = (lwr+upr)/2; */
5196 }
5197 }else{
5198 for(;;){
5199 int nCell; /* Size of the pCell cell in bytes */
5200 pCell = findCellPastPtr(pPage, idx);
5201
5202 /* The maximum supported page-size is 65536 bytes. This means that
5203 ** the maximum number of record bytes stored on an index B-Tree
5204 ** page is less than 16384 bytes and may be stored as a 2-byte
5205 ** varint. This information is used to attempt to avoid parsing
5206 ** the entire cell by checking for the cases where the record is
5207 ** stored entirely within the b-tree page by inspecting the first
5208 ** 2 bytes of the cell.
5209 */
5210 nCell = pCell[0];
5211 if( nCell<=pPage->max1bytePayload ){
5212 /* This branch runs if the record-size field of the cell is a
5213 ** single byte varint and the record fits entirely on the main
5214 ** b-tree page. */
5215 testcase( pCell+nCell+1==pPage->aDataEnd );
5216 c = xRecordCompare(nCell, (void*)&pCell[1], pIdxKey);
5217 }else if( !(pCell[1] & 0x80)
5218 && (nCell = ((nCell&0x7f)<<7) + pCell[1])<=pPage->maxLocal
5219 ){
5220 /* The record-size field is a 2 byte varint and the record
5221 ** fits entirely on the main b-tree page. */
5222 testcase( pCell+nCell+2==pPage->aDataEnd );
5223 c = xRecordCompare(nCell, (void*)&pCell[2], pIdxKey);
5224 }else{
5225 /* The record flows over onto one or more overflow pages. In
5226 ** this case the whole cell needs to be parsed, a buffer allocated
5227 ** and accessPayload() used to retrieve the record into the
5228 ** buffer before VdbeRecordCompare() can be called.
5229 **
5230 ** If the record is corrupt, the xRecordCompare routine may read
5231 ** up to two varints past the end of the buffer. An extra 18
5232 ** bytes of padding is allocated at the end of the buffer in
5233 ** case this happens. */
5234 void *pCellKey;
5235 u8 * const pCellBody = pCell - pPage->childPtrSize;
5236 pPage->xParseCell(pPage, pCellBody, &pCur->info);
5237 nCell = (int)pCur->info.nKey;
5238 testcase( nCell<0 ); /* True if key size is 2^32 or more */
5239 testcase( nCell==0 ); /* Invalid key size: 0x80 0x80 0x00 */
5240 testcase( nCell==1 ); /* Invalid key size: 0x80 0x80 0x01 */
5241 testcase( nCell==2 ); /* Minimum legal index key size */
5242 if( nCell<2 ){
5243 rc = SQLITE_CORRUPT_BKPT;
5244 goto moveto_finish;
5245 }
5246 pCellKey = sqlite3Malloc( nCell+18 );
5247 if( pCellKey==0 ){
5248 rc = SQLITE_NOMEM_BKPT;
5249 goto moveto_finish;
5250 }
5251 pCur->aiIdx[pCur->iPage] = (u16)idx;
5252 rc = accessPayload(pCur, 0, nCell, (unsigned char*)pCellKey, 0);
5253 pCur->curFlags &= ~BTCF_ValidOvfl;
5254 if( rc ){
5255 sqlite3_free(pCellKey);
5256 goto moveto_finish;
5257 }
5258 c = xRecordCompare(nCell, pCellKey, pIdxKey);
5259 sqlite3_free(pCellKey);
5260 }
5261 assert(
5262 (pIdxKey->errCode!=SQLITE_CORRUPT || c==0)
5263 && (pIdxKey->errCode!=SQLITE_NOMEM || pCur->pBtree->db->mallocFailed)
5264 );
5265 if( c<0 ){
5266 lwr = idx+1;
5267 }else if( c>0 ){
5268 upr = idx-1;
5269 }else{
5270 assert( c==0 );
5271 *pRes = 0;
5272 rc = SQLITE_OK;
5273 pCur->aiIdx[pCur->iPage] = (u16)idx;
5274 if( pIdxKey->errCode ) rc = SQLITE_CORRUPT;
5275 goto moveto_finish;
5276 }
5277 if( lwr>upr ) break;
5278 assert( lwr+upr>=0 );
5279 idx = (lwr+upr)>>1; /* idx = (lwr+upr)/2 */
5280 }
5281 }
5282 assert( lwr==upr+1 || (pPage->intKey && !pPage->leaf) );
5283 assert( pPage->isInit );
5284 if( pPage->leaf ){
5285 assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell );
5286 pCur->aiIdx[pCur->iPage] = (u16)idx;
5287 *pRes = c;
5288 rc = SQLITE_OK;
5289 goto moveto_finish;
5290 }
5291 moveto_next_layer:
5292 if( lwr>=pPage->nCell ){
5293 chldPg = get4byte(&pPage->aData[pPage->hdrOffset+8]);
5294 }else{
5295 chldPg = get4byte(findCell(pPage, lwr));
5296 }
5297 pCur->aiIdx[pCur->iPage] = (u16)lwr;
5298 rc = moveToChild(pCur, chldPg);
5299 if( rc ) break;
5300 }
5301 moveto_finish:
5302 pCur->info.nSize = 0;
5303 assert( (pCur->curFlags & BTCF_ValidOvfl)==0 );
5304 return rc;
5305 }
5306
5307
5308 /*
5309 ** Return TRUE if the cursor is not pointing at an entry of the table.
5310 **
5311 ** TRUE will be returned after a call to sqlite3BtreeNext() moves
5312 ** past the last entry in the table or sqlite3BtreePrev() moves past
5313 ** the first entry. TRUE is also returned if the table is empty.
5314 */
5315 SQLITE_PRIVATE int sqlite3BtreeEof(BtCursor *pCur){
5316 /* TODO: What if the cursor is in CURSOR_REQUIRESEEK but all table entries
5317 ** have been deleted? This API will need to change to return an error code
5318 ** as well as the boolean result value.
5319 */
5320 return (CURSOR_VALID!=pCur->eState);
5321 }
5322
5323 /*
5324 ** Advance the cursor to the next entry in the database. If
5325 ** successful then set *pRes=0. If the cursor
5326 ** was already pointing to the last entry in the database before
5327 ** this routine was called, then set *pRes=1.
5328 **
5329 ** The main entry point is sqlite3BtreeNext(). That routine is optimized
5330 ** for the common case of merely incrementing the cell counter BtCursor.aiIdx
5331 ** to the next cell on the current page. The (slower) btreeNext() helper
5332 ** routine is called when it is necessary to move to a different page or
5333 ** to restore the cursor.
5334 **
5335 ** The calling function will set *pRes to 0 or 1. The initial *pRes value
5336 ** will be 1 if the cursor being stepped corresponds to an SQL index and
5337 ** if this routine could have been skipped if that SQL index had been
5338 ** a unique index. Otherwise the caller will have set *pRes to zero.
5339 ** Zero is the common case. The btree implementation is free to use the
5340 ** initial *pRes value as a hint to improve performance, but the current
5341 ** SQLite btree implementation does not. (Note that the comdb2 btree
5342 ** implementation does use this hint, however.)
5343 */
5344 static SQLITE_NOINLINE int btreeNext(BtCursor *pCur, int *pRes){
5345 int rc;
5346 int idx;
5347 MemPage *pPage;
5348
5349 assert( cursorOwnsBtShared(pCur) );
5350 assert( pCur->skipNext==0 || pCur->eState!=CURSOR_VALID );
5351 assert( *pRes==0 );
5352 if( pCur->eState!=CURSOR_VALID ){
5353 assert( (pCur->curFlags & BTCF_ValidOvfl)==0 );
5354 rc = restoreCursorPosition(pCur);
5355 if( rc!=SQLITE_OK ){
5356 return rc;
5357 }
5358 if( CURSOR_INVALID==pCur->eState ){
5359 *pRes = 1;
5360 return SQLITE_OK;
5361 }
5362 if( pCur->skipNext ){
5363 assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_SKIPNEXT );
5364 pCur->eState = CURSOR_VALID;
5365 if( pCur->skipNext>0 ){
5366 pCur->skipNext = 0;
5367 return SQLITE_OK;
5368 }
5369 pCur->skipNext = 0;
5370 }
5371 }
5372
5373 pPage = pCur->apPage[pCur->iPage];
5374 idx = ++pCur->aiIdx[pCur->iPage];
5375 assert( pPage->isInit );
5376
5377 /* If the database file is corrupt, it is possible for the value of idx
5378 ** to be invalid here. This can only occur if a second cursor modifies
5379 ** the page while cursor pCur is holding a reference to it. Which can
5380 ** only happen if the database is corrupt in such a way as to link the
5381 ** page into more than one b-tree structure. */
5382 testcase( idx>pPage->nCell );
5383
5384 if( idx>=pPage->nCell ){
5385 if( !pPage->leaf ){
5386 rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8]));
5387 if( rc ) return rc;
5388 return moveToLeftmost(pCur);
5389 }
5390 do{
5391 if( pCur->iPage==0 ){
5392 *pRes = 1;
5393 pCur->eState = CURSOR_INVALID;
5394 return SQLITE_OK;
5395 }
5396 moveToParent(pCur);
5397 pPage = pCur->apPage[pCur->iPage];
5398 }while( pCur->aiIdx[pCur->iPage]>=pPage->nCell );
5399 if( pPage->intKey ){
5400 return sqlite3BtreeNext(pCur, pRes);
5401 }else{
5402 return SQLITE_OK;
5403 }
5404 }
5405 if( pPage->leaf ){
5406 return SQLITE_OK;
5407 }else{
5408 return moveToLeftmost(pCur);
5409 }
5410 }
5411 SQLITE_PRIVATE int sqlite3BtreeNext(BtCursor *pCur, int *pRes){
5412 MemPage *pPage;
5413 assert( cursorOwnsBtShared(pCur) );
5414 assert( pRes!=0 );
5415 assert( *pRes==0 || *pRes==1 );
5416 assert( pCur->skipNext==0 || pCur->eState!=CURSOR_VALID );
5417 pCur->info.nSize = 0;
5418 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
5419 *pRes = 0;
5420 if( pCur->eState!=CURSOR_VALID ) return btreeNext(pCur, pRes);
5421 pPage = pCur->apPage[pCur->iPage];
5422 if( (++pCur->aiIdx[pCur->iPage])>=pPage->nCell ){
5423 pCur->aiIdx[pCur->iPage]--;
5424 return btreeNext(pCur, pRes);
5425 }
5426 if( pPage->leaf ){
5427 return SQLITE_OK;
5428 }else{
5429 return moveToLeftmost(pCur);
5430 }
5431 }
5432
5433 /*
5434 ** Step the cursor to the back to the previous entry in the database. If
5435 ** successful then set *pRes=0. If the cursor
5436 ** was already pointing to the first entry in the database before
5437 ** this routine was called, then set *pRes=1.
5438 **
5439 ** The main entry point is sqlite3BtreePrevious(). That routine is optimized
5440 ** for the common case of merely decrementing the cell counter BtCursor.aiIdx
5441 ** to the previous cell on the current page. The (slower) btreePrevious()
5442 ** helper routine is called when it is necessary to move to a different page
5443 ** or to restore the cursor.
5444 **
5445 ** The calling function will set *pRes to 0 or 1. The initial *pRes value
5446 ** will be 1 if the cursor being stepped corresponds to an SQL index and
5447 ** if this routine could have been skipped if that SQL index had been
5448 ** a unique index. Otherwise the caller will have set *pRes to zero.
5449 ** Zero is the common case. The btree implementation is free to use the
5450 ** initial *pRes value as a hint to improve performance, but the current
5451 ** SQLite btree implementation does not. (Note that the comdb2 btree
5452 ** implementation does use this hint, however.)
5453 */
5454 static SQLITE_NOINLINE int btreePrevious(BtCursor *pCur, int *pRes){
5455 int rc;
5456 MemPage *pPage;
5457
5458 assert( cursorOwnsBtShared(pCur) );
5459 assert( pRes!=0 );
5460 assert( *pRes==0 );
5461 assert( pCur->skipNext==0 || pCur->eState!=CURSOR_VALID );
5462 assert( (pCur->curFlags & (BTCF_AtLast|BTCF_ValidOvfl|BTCF_ValidNKey))==0 );
5463 assert( pCur->info.nSize==0 );
5464 if( pCur->eState!=CURSOR_VALID ){
5465 rc = restoreCursorPosition(pCur);
5466 if( rc!=SQLITE_OK ){
5467 return rc;
5468 }
5469 if( CURSOR_INVALID==pCur->eState ){
5470 *pRes = 1;
5471 return SQLITE_OK;
5472 }
5473 if( pCur->skipNext ){
5474 assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_SKIPNEXT );
5475 pCur->eState = CURSOR_VALID;
5476 if( pCur->skipNext<0 ){
5477 pCur->skipNext = 0;
5478 return SQLITE_OK;
5479 }
5480 pCur->skipNext = 0;
5481 }
5482 }
5483
5484 pPage = pCur->apPage[pCur->iPage];
5485 assert( pPage->isInit );
5486 if( !pPage->leaf ){
5487 int idx = pCur->aiIdx[pCur->iPage];
5488 rc = moveToChild(pCur, get4byte(findCell(pPage, idx)));
5489 if( rc ) return rc;
5490 rc = moveToRightmost(pCur);
5491 }else{
5492 while( pCur->aiIdx[pCur->iPage]==0 ){
5493 if( pCur->iPage==0 ){
5494 pCur->eState = CURSOR_INVALID;
5495 *pRes = 1;
5496 return SQLITE_OK;
5497 }
5498 moveToParent(pCur);
5499 }
5500 assert( pCur->info.nSize==0 );
5501 assert( (pCur->curFlags & (BTCF_ValidOvfl))==0 );
5502
5503 pCur->aiIdx[pCur->iPage]--;
5504 pPage = pCur->apPage[pCur->iPage];
5505 if( pPage->intKey && !pPage->leaf ){
5506 rc = sqlite3BtreePrevious(pCur, pRes);
5507 }else{
5508 rc = SQLITE_OK;
5509 }
5510 }
5511 return rc;
5512 }
5513 SQLITE_PRIVATE int sqlite3BtreePrevious(BtCursor *pCur, int *pRes){
5514 assert( cursorOwnsBtShared(pCur) );
5515 assert( pRes!=0 );
5516 assert( *pRes==0 || *pRes==1 );
5517 assert( pCur->skipNext==0 || pCur->eState!=CURSOR_VALID );
5518 *pRes = 0;
5519 pCur->curFlags &= ~(BTCF_AtLast|BTCF_ValidOvfl|BTCF_ValidNKey);
5520 pCur->info.nSize = 0;
5521 if( pCur->eState!=CURSOR_VALID
5522 || pCur->aiIdx[pCur->iPage]==0
5523 || pCur->apPage[pCur->iPage]->leaf==0
5524 ){
5525 return btreePrevious(pCur, pRes);
5526 }
5527 pCur->aiIdx[pCur->iPage]--;
5528 return SQLITE_OK;
5529 }
5530
5531 /*
5532 ** Allocate a new page from the database file.
5533 **
5534 ** The new page is marked as dirty. (In other words, sqlite3PagerWrite()
5535 ** has already been called on the new page.) The new page has also
5536 ** been referenced and the calling routine is responsible for calling
5537 ** sqlite3PagerUnref() on the new page when it is done.
5538 **
5539 ** SQLITE_OK is returned on success. Any other return value indicates
5540 ** an error. *ppPage is set to NULL in the event of an error.
5541 **
5542 ** If the "nearby" parameter is not 0, then an effort is made to
5543 ** locate a page close to the page number "nearby". This can be used in an
5544 ** attempt to keep related pages close to each other in the database file,
5545 ** which in turn can make database access faster.
5546 **
5547 ** If the eMode parameter is BTALLOC_EXACT and the nearby page exists
5548 ** anywhere on the free-list, then it is guaranteed to be returned. If
5549 ** eMode is BTALLOC_LT then the page returned will be less than or equal
5550 ** to nearby if any such page exists. If eMode is BTALLOC_ANY then there
5551 ** are no restrictions on which page is returned.
5552 */
5553 static int allocateBtreePage(
5554 BtShared *pBt, /* The btree */
5555 MemPage **ppPage, /* Store pointer to the allocated page here */
5556 Pgno *pPgno, /* Store the page number here */
5557 Pgno nearby, /* Search for a page near this one */
5558 u8 eMode /* BTALLOC_EXACT, BTALLOC_LT, or BTALLOC_ANY */
5559 ){
5560 MemPage *pPage1;
5561 int rc;
5562 u32 n; /* Number of pages on the freelist */
5563 u32 k; /* Number of leaves on the trunk of the freelist */
5564 MemPage *pTrunk = 0;
5565 MemPage *pPrevTrunk = 0;
5566 Pgno mxPage; /* Total size of the database file */
5567
5568 assert( sqlite3_mutex_held(pBt->mutex) );
5569 assert( eMode==BTALLOC_ANY || (nearby>0 && IfNotOmitAV(pBt->autoVacuum)) );
5570 pPage1 = pBt->pPage1;
5571 mxPage = btreePagecount(pBt);
5572 /* EVIDENCE-OF: R-05119-02637 The 4-byte big-endian integer at offset 36
5573 ** stores stores the total number of pages on the freelist. */
5574 n = get4byte(&pPage1->aData[36]);
5575 testcase( n==mxPage-1 );
5576 if( n>=mxPage ){
5577 return SQLITE_CORRUPT_BKPT;
5578 }
5579 if( n>0 ){
5580 /* There are pages on the freelist. Reuse one of those pages. */
5581 Pgno iTrunk;
5582 u8 searchList = 0; /* If the free-list must be searched for 'nearby' */
5583 u32 nSearch = 0; /* Count of the number of search attempts */
5584
5585 /* If eMode==BTALLOC_EXACT and a query of the pointer-map
5586 ** shows that the page 'nearby' is somewhere on the free-list, then
5587 ** the entire-list will be searched for that page.
5588 */
5589 #ifndef SQLITE_OMIT_AUTOVACUUM
5590 if( eMode==BTALLOC_EXACT ){
5591 if( nearby<=mxPage ){
5592 u8 eType;
5593 assert( nearby>0 );
5594 assert( pBt->autoVacuum );
5595 rc = ptrmapGet(pBt, nearby, &eType, 0);
5596 if( rc ) return rc;
5597 if( eType==PTRMAP_FREEPAGE ){
5598 searchList = 1;
5599 }
5600 }
5601 }else if( eMode==BTALLOC_LE ){
5602 searchList = 1;
5603 }
5604 #endif
5605
5606 /* Decrement the free-list count by 1. Set iTrunk to the index of the
5607 ** first free-list trunk page. iPrevTrunk is initially 1.
5608 */
5609 rc = sqlite3PagerWrite(pPage1->pDbPage);
5610 if( rc ) return rc;
5611 put4byte(&pPage1->aData[36], n-1);
5612
5613 /* The code within this loop is run only once if the 'searchList' variable
5614 ** is not true. Otherwise, it runs once for each trunk-page on the
5615 ** free-list until the page 'nearby' is located (eMode==BTALLOC_EXACT)
5616 ** or until a page less than 'nearby' is located (eMode==BTALLOC_LT)
5617 */
5618 do {
5619 pPrevTrunk = pTrunk;
5620 if( pPrevTrunk ){
5621 /* EVIDENCE-OF: R-01506-11053 The first integer on a freelist trunk page
5622 ** is the page number of the next freelist trunk page in the list or
5623 ** zero if this is the last freelist trunk page. */
5624 iTrunk = get4byte(&pPrevTrunk->aData[0]);
5625 }else{
5626 /* EVIDENCE-OF: R-59841-13798 The 4-byte big-endian integer at offset 32
5627 ** stores the page number of the first page of the freelist, or zero if
5628 ** the freelist is empty. */
5629 iTrunk = get4byte(&pPage1->aData[32]);
5630 }
5631 testcase( iTrunk==mxPage );
5632 if( iTrunk>mxPage || nSearch++ > n ){
5633 rc = SQLITE_CORRUPT_BKPT;
5634 }else{
5635 rc = btreeGetUnusedPage(pBt, iTrunk, &pTrunk, 0);
5636 }
5637 if( rc ){
5638 pTrunk = 0;
5639 goto end_allocate_page;
5640 }
5641 assert( pTrunk!=0 );
5642 assert( pTrunk->aData!=0 );
5643 /* EVIDENCE-OF: R-13523-04394 The second integer on a freelist trunk page
5644 ** is the number of leaf page pointers to follow. */
5645 k = get4byte(&pTrunk->aData[4]);
5646 if( k==0 && !searchList ){
5647 /* The trunk has no leaves and the list is not being searched.
5648 ** So extract the trunk page itself and use it as the newly
5649 ** allocated page */
5650 assert( pPrevTrunk==0 );
5651 rc = sqlite3PagerWrite(pTrunk->pDbPage);
5652 if( rc ){
5653 goto end_allocate_page;
5654 }
5655 *pPgno = iTrunk;
5656 memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4);
5657 *ppPage = pTrunk;
5658 pTrunk = 0;
5659 TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1));
5660 }else if( k>(u32)(pBt->usableSize/4 - 2) ){
5661 /* Value of k is out of range. Database corruption */
5662 rc = SQLITE_CORRUPT_BKPT;
5663 goto end_allocate_page;
5664 #ifndef SQLITE_OMIT_AUTOVACUUM
5665 }else if( searchList
5666 && (nearby==iTrunk || (iTrunk<nearby && eMode==BTALLOC_LE))
5667 ){
5668 /* The list is being searched and this trunk page is the page
5669 ** to allocate, regardless of whether it has leaves.
5670 */
5671 *pPgno = iTrunk;
5672 *ppPage = pTrunk;
5673 searchList = 0;
5674 rc = sqlite3PagerWrite(pTrunk->pDbPage);
5675 if( rc ){
5676 goto end_allocate_page;
5677 }
5678 if( k==0 ){
5679 if( !pPrevTrunk ){
5680 memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4);
5681 }else{
5682 rc = sqlite3PagerWrite(pPrevTrunk->pDbPage);
5683 if( rc!=SQLITE_OK ){
5684 goto end_allocate_page;
5685 }
5686 memcpy(&pPrevTrunk->aData[0], &pTrunk->aData[0], 4);
5687 }
5688 }else{
5689 /* The trunk page is required by the caller but it contains
5690 ** pointers to free-list leaves. The first leaf becomes a trunk
5691 ** page in this case.
5692 */
5693 MemPage *pNewTrunk;
5694 Pgno iNewTrunk = get4byte(&pTrunk->aData[8]);
5695 if( iNewTrunk>mxPage ){
5696 rc = SQLITE_CORRUPT_BKPT;
5697 goto end_allocate_page;
5698 }
5699 testcase( iNewTrunk==mxPage );
5700 rc = btreeGetUnusedPage(pBt, iNewTrunk, &pNewTrunk, 0);
5701 if( rc!=SQLITE_OK ){
5702 goto end_allocate_page;
5703 }
5704 rc = sqlite3PagerWrite(pNewTrunk->pDbPage);
5705 if( rc!=SQLITE_OK ){
5706 releasePage(pNewTrunk);
5707 goto end_allocate_page;
5708 }
5709 memcpy(&pNewTrunk->aData[0], &pTrunk->aData[0], 4);
5710 put4byte(&pNewTrunk->aData[4], k-1);
5711 memcpy(&pNewTrunk->aData[8], &pTrunk->aData[12], (k-1)*4);
5712 releasePage(pNewTrunk);
5713 if( !pPrevTrunk ){
5714 assert( sqlite3PagerIswriteable(pPage1->pDbPage) );
5715 put4byte(&pPage1->aData[32], iNewTrunk);
5716 }else{
5717 rc = sqlite3PagerWrite(pPrevTrunk->pDbPage);
5718 if( rc ){
5719 goto end_allocate_page;
5720 }
5721 put4byte(&pPrevTrunk->aData[0], iNewTrunk);
5722 }
5723 }
5724 pTrunk = 0;
5725 TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1));
5726 #endif
5727 }else if( k>0 ){
5728 /* Extract a leaf from the trunk */
5729 u32 closest;
5730 Pgno iPage;
5731 unsigned char *aData = pTrunk->aData;
5732 if( nearby>0 ){
5733 u32 i;
5734 closest = 0;
5735 if( eMode==BTALLOC_LE ){
5736 for(i=0; i<k; i++){
5737 iPage = get4byte(&aData[8+i*4]);
5738 if( iPage<=nearby ){
5739 closest = i;
5740 break;
5741 }
5742 }
5743 }else{
5744 int dist;
5745 dist = sqlite3AbsInt32(get4byte(&aData[8]) - nearby);
5746 for(i=1; i<k; i++){
5747 int d2 = sqlite3AbsInt32(get4byte(&aData[8+i*4]) - nearby);
5748 if( d2<dist ){
5749 closest = i;
5750 dist = d2;
5751 }
5752 }
5753 }
5754 }else{
5755 closest = 0;
5756 }
5757
5758 iPage = get4byte(&aData[8+closest*4]);
5759 testcase( iPage==mxPage );
5760 if( iPage>mxPage ){
5761 rc = SQLITE_CORRUPT_BKPT;
5762 goto end_allocate_page;
5763 }
5764 testcase( iPage==mxPage );
5765 if( !searchList
5766 || (iPage==nearby || (iPage<nearby && eMode==BTALLOC_LE))
5767 ){
5768 int noContent;
5769 *pPgno = iPage;
5770 TRACE(("ALLOCATE: %d was leaf %d of %d on trunk %d"
5771 ": %d more free pages\n",
5772 *pPgno, closest+1, k, pTrunk->pgno, n-1));
5773 rc = sqlite3PagerWrite(pTrunk->pDbPage);
5774 if( rc ) goto end_allocate_page;
5775 if( closest<k-1 ){
5776 memcpy(&aData[8+closest*4], &aData[4+k*4], 4);
5777 }
5778 put4byte(&aData[4], k-1);
5779 noContent = !btreeGetHasContent(pBt, *pPgno)? PAGER_GET_NOCONTENT : 0;
5780 rc = btreeGetUnusedPage(pBt, *pPgno, ppPage, noContent);
5781 if( rc==SQLITE_OK ){
5782 rc = sqlite3PagerWrite((*ppPage)->pDbPage);
5783 if( rc!=SQLITE_OK ){
5784 releasePage(*ppPage);
5785 *ppPage = 0;
5786 }
5787 }
5788 searchList = 0;
5789 }
5790 }
5791 releasePage(pPrevTrunk);
5792 pPrevTrunk = 0;
5793 }while( searchList );
5794 }else{
5795 /* There are no pages on the freelist, so append a new page to the
5796 ** database image.
5797 **
5798 ** Normally, new pages allocated by this block can be requested from the
5799 ** pager layer with the 'no-content' flag set. This prevents the pager
5800 ** from trying to read the pages content from disk. However, if the
5801 ** current transaction has already run one or more incremental-vacuum
5802 ** steps, then the page we are about to allocate may contain content
5803 ** that is required in the event of a rollback. In this case, do
5804 ** not set the no-content flag. This causes the pager to load and journal
5805 ** the current page content before overwriting it.
5806 **
5807 ** Note that the pager will not actually attempt to load or journal
5808 ** content for any page that really does lie past the end of the database
5809 ** file on disk. So the effects of disabling the no-content optimization
5810 ** here are confined to those pages that lie between the end of the
5811 ** database image and the end of the database file.
5812 */
5813 int bNoContent = (0==IfNotOmitAV(pBt->bDoTruncate))? PAGER_GET_NOCONTENT:0;
5814
5815 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
5816 if( rc ) return rc;
5817 pBt->nPage++;
5818 if( pBt->nPage==PENDING_BYTE_PAGE(pBt) ) pBt->nPage++;
5819
5820 #ifndef SQLITE_OMIT_AUTOVACUUM
5821 if( pBt->autoVacuum && PTRMAP_ISPAGE(pBt, pBt->nPage) ){
5822 /* If *pPgno refers to a pointer-map page, allocate two new pages
5823 ** at the end of the file instead of one. The first allocated page
5824 ** becomes a new pointer-map page, the second is used by the caller.
5825 */
5826 MemPage *pPg = 0;
5827 TRACE(("ALLOCATE: %d from end of file (pointer-map page)\n", pBt->nPage));
5828 assert( pBt->nPage!=PENDING_BYTE_PAGE(pBt) );
5829 rc = btreeGetUnusedPage(pBt, pBt->nPage, &pPg, bNoContent);
5830 if( rc==SQLITE_OK ){
5831 rc = sqlite3PagerWrite(pPg->pDbPage);
5832 releasePage(pPg);
5833 }
5834 if( rc ) return rc;
5835 pBt->nPage++;
5836 if( pBt->nPage==PENDING_BYTE_PAGE(pBt) ){ pBt->nPage++; }
5837 }
5838 #endif
5839 put4byte(28 + (u8*)pBt->pPage1->aData, pBt->nPage);
5840 *pPgno = pBt->nPage;
5841
5842 assert( *pPgno!=PENDING_BYTE_PAGE(pBt) );
5843 rc = btreeGetUnusedPage(pBt, *pPgno, ppPage, bNoContent);
5844 if( rc ) return rc;
5845 rc = sqlite3PagerWrite((*ppPage)->pDbPage);
5846 if( rc!=SQLITE_OK ){
5847 releasePage(*ppPage);
5848 *ppPage = 0;
5849 }
5850 TRACE(("ALLOCATE: %d from end of file\n", *pPgno));
5851 }
5852
5853 assert( *pPgno!=PENDING_BYTE_PAGE(pBt) );
5854
5855 end_allocate_page:
5856 releasePage(pTrunk);
5857 releasePage(pPrevTrunk);
5858 assert( rc!=SQLITE_OK || sqlite3PagerPageRefcount((*ppPage)->pDbPage)<=1 );
5859 assert( rc!=SQLITE_OK || (*ppPage)->isInit==0 );
5860 return rc;
5861 }
5862
5863 /*
5864 ** This function is used to add page iPage to the database file free-list.
5865 ** It is assumed that the page is not already a part of the free-list.
5866 **
5867 ** The value passed as the second argument to this function is optional.
5868 ** If the caller happens to have a pointer to the MemPage object
5869 ** corresponding to page iPage handy, it may pass it as the second value.
5870 ** Otherwise, it may pass NULL.
5871 **
5872 ** If a pointer to a MemPage object is passed as the second argument,
5873 ** its reference count is not altered by this function.
5874 */
5875 static int freePage2(BtShared *pBt, MemPage *pMemPage, Pgno iPage){
5876 MemPage *pTrunk = 0; /* Free-list trunk page */
5877 Pgno iTrunk = 0; /* Page number of free-list trunk page */
5878 MemPage *pPage1 = pBt->pPage1; /* Local reference to page 1 */
5879 MemPage *pPage; /* Page being freed. May be NULL. */
5880 int rc; /* Return Code */
5881 int nFree; /* Initial number of pages on free-list */
5882
5883 assert( sqlite3_mutex_held(pBt->mutex) );
5884 assert( CORRUPT_DB || iPage>1 );
5885 assert( !pMemPage || pMemPage->pgno==iPage );
5886
5887 if( iPage<2 ) return SQLITE_CORRUPT_BKPT;
5888 if( pMemPage ){
5889 pPage = pMemPage;
5890 sqlite3PagerRef(pPage->pDbPage);
5891 }else{
5892 pPage = btreePageLookup(pBt, iPage);
5893 }
5894
5895 /* Increment the free page count on pPage1 */
5896 rc = sqlite3PagerWrite(pPage1->pDbPage);
5897 if( rc ) goto freepage_out;
5898 nFree = get4byte(&pPage1->aData[36]);
5899 put4byte(&pPage1->aData[36], nFree+1);
5900
5901 if( pBt->btsFlags & BTS_SECURE_DELETE ){
5902 /* If the secure_delete option is enabled, then
5903 ** always fully overwrite deleted information with zeros.
5904 */
5905 if( (!pPage && ((rc = btreeGetPage(pBt, iPage, &pPage, 0))!=0) )
5906 || ((rc = sqlite3PagerWrite(pPage->pDbPage))!=0)
5907 ){
5908 goto freepage_out;
5909 }
5910 memset(pPage->aData, 0, pPage->pBt->pageSize);
5911 }
5912
5913 /* If the database supports auto-vacuum, write an entry in the pointer-map
5914 ** to indicate that the page is free.
5915 */
5916 if( ISAUTOVACUUM ){
5917 ptrmapPut(pBt, iPage, PTRMAP_FREEPAGE, 0, &rc);
5918 if( rc ) goto freepage_out;
5919 }
5920
5921 /* Now manipulate the actual database free-list structure. There are two
5922 ** possibilities. If the free-list is currently empty, or if the first
5923 ** trunk page in the free-list is full, then this page will become a
5924 ** new free-list trunk page. Otherwise, it will become a leaf of the
5925 ** first trunk page in the current free-list. This block tests if it
5926 ** is possible to add the page as a new free-list leaf.
5927 */
5928 if( nFree!=0 ){
5929 u32 nLeaf; /* Initial number of leaf cells on trunk page */
5930
5931 iTrunk = get4byte(&pPage1->aData[32]);
5932 rc = btreeGetPage(pBt, iTrunk, &pTrunk, 0);
5933 if( rc!=SQLITE_OK ){
5934 goto freepage_out;
5935 }
5936
5937 nLeaf = get4byte(&pTrunk->aData[4]);
5938 assert( pBt->usableSize>32 );
5939 if( nLeaf > (u32)pBt->usableSize/4 - 2 ){
5940 rc = SQLITE_CORRUPT_BKPT;
5941 goto freepage_out;
5942 }
5943 if( nLeaf < (u32)pBt->usableSize/4 - 8 ){
5944 /* In this case there is room on the trunk page to insert the page
5945 ** being freed as a new leaf.
5946 **
5947 ** Note that the trunk page is not really full until it contains
5948 ** usableSize/4 - 2 entries, not usableSize/4 - 8 entries as we have
5949 ** coded. But due to a coding error in versions of SQLite prior to
5950 ** 3.6.0, databases with freelist trunk pages holding more than
5951 ** usableSize/4 - 8 entries will be reported as corrupt. In order
5952 ** to maintain backwards compatibility with older versions of SQLite,
5953 ** we will continue to restrict the number of entries to usableSize/4 - 8
5954 ** for now. At some point in the future (once everyone has upgraded
5955 ** to 3.6.0 or later) we should consider fixing the conditional above
5956 ** to read "usableSize/4-2" instead of "usableSize/4-8".
5957 **
5958 ** EVIDENCE-OF: R-19920-11576 However, newer versions of SQLite still
5959 ** avoid using the last six entries in the freelist trunk page array in
5960 ** order that database files created by newer versions of SQLite can be
5961 ** read by older versions of SQLite.
5962 */
5963 rc = sqlite3PagerWrite(pTrunk->pDbPage);
5964 if( rc==SQLITE_OK ){
5965 put4byte(&pTrunk->aData[4], nLeaf+1);
5966 put4byte(&pTrunk->aData[8+nLeaf*4], iPage);
5967 if( pPage && (pBt->btsFlags & BTS_SECURE_DELETE)==0 ){
5968 sqlite3PagerDontWrite(pPage->pDbPage);
5969 }
5970 rc = btreeSetHasContent(pBt, iPage);
5971 }
5972 TRACE(("FREE-PAGE: %d leaf on trunk page %d\n",pPage->pgno,pTrunk->pgno));
5973 goto freepage_out;
5974 }
5975 }
5976
5977 /* If control flows to this point, then it was not possible to add the
5978 ** the page being freed as a leaf page of the first trunk in the free-list.
5979 ** Possibly because the free-list is empty, or possibly because the
5980 ** first trunk in the free-list is full. Either way, the page being freed
5981 ** will become the new first trunk page in the free-list.
5982 */
5983 if( pPage==0 && SQLITE_OK!=(rc = btreeGetPage(pBt, iPage, &pPage, 0)) ){
5984 goto freepage_out;
5985 }
5986 rc = sqlite3PagerWrite(pPage->pDbPage);
5987 if( rc!=SQLITE_OK ){
5988 goto freepage_out;
5989 }
5990 put4byte(pPage->aData, iTrunk);
5991 put4byte(&pPage->aData[4], 0);
5992 put4byte(&pPage1->aData[32], iPage);
5993 TRACE(("FREE-PAGE: %d new trunk page replacing %d\n", pPage->pgno, iTrunk));
5994
5995 freepage_out:
5996 if( pPage ){
5997 pPage->isInit = 0;
5998 }
5999 releasePage(pPage);
6000 releasePage(pTrunk);
6001 return rc;
6002 }
6003 static void freePage(MemPage *pPage, int *pRC){
6004 if( (*pRC)==SQLITE_OK ){
6005 *pRC = freePage2(pPage->pBt, pPage, pPage->pgno);
6006 }
6007 }
6008
6009 /*
6010 ** Free any overflow pages associated with the given Cell. Write the
6011 ** local Cell size (the number of bytes on the original page, omitting
6012 ** overflow) into *pnSize.
6013 */
6014 static int clearCell(
6015 MemPage *pPage, /* The page that contains the Cell */
6016 unsigned char *pCell, /* First byte of the Cell */
6017 CellInfo *pInfo /* Size information about the cell */
6018 ){
6019 BtShared *pBt = pPage->pBt;
6020 Pgno ovflPgno;
6021 int rc;
6022 int nOvfl;
6023 u32 ovflPageSize;
6024
6025 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
6026 pPage->xParseCell(pPage, pCell, pInfo);
6027 if( pInfo->nLocal==pInfo->nPayload ){
6028 return SQLITE_OK; /* No overflow pages. Return without doing anything */
6029 }
6030 if( pCell+pInfo->nSize-1 > pPage->aData+pPage->maskPage ){
6031 return SQLITE_CORRUPT_BKPT; /* Cell extends past end of page */
6032 }
6033 ovflPgno = get4byte(pCell + pInfo->nSize - 4);
6034 assert( pBt->usableSize > 4 );
6035 ovflPageSize = pBt->usableSize - 4;
6036 nOvfl = (pInfo->nPayload - pInfo->nLocal + ovflPageSize - 1)/ovflPageSize;
6037 assert( nOvfl>0 ||
6038 (CORRUPT_DB && (pInfo->nPayload + ovflPageSize)<ovflPageSize)
6039 );
6040 while( nOvfl-- ){
6041 Pgno iNext = 0;
6042 MemPage *pOvfl = 0;
6043 if( ovflPgno<2 || ovflPgno>btreePagecount(pBt) ){
6044 /* 0 is not a legal page number and page 1 cannot be an
6045 ** overflow page. Therefore if ovflPgno<2 or past the end of the
6046 ** file the database must be corrupt. */
6047 return SQLITE_CORRUPT_BKPT;
6048 }
6049 if( nOvfl ){
6050 rc = getOverflowPage(pBt, ovflPgno, &pOvfl, &iNext);
6051 if( rc ) return rc;
6052 }
6053
6054 if( ( pOvfl || ((pOvfl = btreePageLookup(pBt, ovflPgno))!=0) )
6055 && sqlite3PagerPageRefcount(pOvfl->pDbPage)!=1
6056 ){
6057 /* There is no reason any cursor should have an outstanding reference
6058 ** to an overflow page belonging to a cell that is being deleted/updated.
6059 ** So if there exists more than one reference to this page, then it
6060 ** must not really be an overflow page and the database must be corrupt.
6061 ** It is helpful to detect this before calling freePage2(), as
6062 ** freePage2() may zero the page contents if secure-delete mode is
6063 ** enabled. If this 'overflow' page happens to be a page that the
6064 ** caller is iterating through or using in some other way, this
6065 ** can be problematic.
6066 */
6067 rc = SQLITE_CORRUPT_BKPT;
6068 }else{
6069 rc = freePage2(pBt, pOvfl, ovflPgno);
6070 }
6071
6072 if( pOvfl ){
6073 sqlite3PagerUnref(pOvfl->pDbPage);
6074 }
6075 if( rc ) return rc;
6076 ovflPgno = iNext;
6077 }
6078 return SQLITE_OK;
6079 }
6080
6081 /*
6082 ** Create the byte sequence used to represent a cell on page pPage
6083 ** and write that byte sequence into pCell[]. Overflow pages are
6084 ** allocated and filled in as necessary. The calling procedure
6085 ** is responsible for making sure sufficient space has been allocated
6086 ** for pCell[].
6087 **
6088 ** Note that pCell does not necessary need to point to the pPage->aData
6089 ** area. pCell might point to some temporary storage. The cell will
6090 ** be constructed in this temporary area then copied into pPage->aData
6091 ** later.
6092 */
6093 static int fillInCell(
6094 MemPage *pPage, /* The page that contains the cell */
6095 unsigned char *pCell, /* Complete text of the cell */
6096 const BtreePayload *pX, /* Payload with which to construct the cell */
6097 int *pnSize /* Write cell size here */
6098 ){
6099 int nPayload;
6100 const u8 *pSrc;
6101 int nSrc, n, rc;
6102 int spaceLeft;
6103 MemPage *pOvfl = 0;
6104 MemPage *pToRelease = 0;
6105 unsigned char *pPrior;
6106 unsigned char *pPayload;
6107 BtShared *pBt = pPage->pBt;
6108 Pgno pgnoOvfl = 0;
6109 int nHeader;
6110
6111 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
6112
6113 /* pPage is not necessarily writeable since pCell might be auxiliary
6114 ** buffer space that is separate from the pPage buffer area */
6115 assert( pCell<pPage->aData || pCell>=&pPage->aData[pBt->pageSize]
6116 || sqlite3PagerIswriteable(pPage->pDbPage) );
6117
6118 /* Fill in the header. */
6119 nHeader = pPage->childPtrSize;
6120 if( pPage->intKey ){
6121 nPayload = pX->nData + pX->nZero;
6122 pSrc = pX->pData;
6123 nSrc = pX->nData;
6124 assert( pPage->intKeyLeaf ); /* fillInCell() only called for leaves */
6125 nHeader += putVarint32(&pCell[nHeader], nPayload);
6126 nHeader += putVarint(&pCell[nHeader], *(u64*)&pX->nKey);
6127 }else{
6128 assert( pX->nKey<=0x7fffffff && pX->pKey!=0 );
6129 nSrc = nPayload = (int)pX->nKey;
6130 pSrc = pX->pKey;
6131 nHeader += putVarint32(&pCell[nHeader], nPayload);
6132 }
6133
6134 /* Fill in the payload */
6135 if( nPayload<=pPage->maxLocal ){
6136 n = nHeader + nPayload;
6137 testcase( n==3 );
6138 testcase( n==4 );
6139 if( n<4 ) n = 4;
6140 *pnSize = n;
6141 spaceLeft = nPayload;
6142 pPrior = pCell;
6143 }else{
6144 int mn = pPage->minLocal;
6145 n = mn + (nPayload - mn) % (pPage->pBt->usableSize - 4);
6146 testcase( n==pPage->maxLocal );
6147 testcase( n==pPage->maxLocal+1 );
6148 if( n > pPage->maxLocal ) n = mn;
6149 spaceLeft = n;
6150 *pnSize = n + nHeader + 4;
6151 pPrior = &pCell[nHeader+n];
6152 }
6153 pPayload = &pCell[nHeader];
6154
6155 /* At this point variables should be set as follows:
6156 **
6157 ** nPayload Total payload size in bytes
6158 ** pPayload Begin writing payload here
6159 ** spaceLeft Space available at pPayload. If nPayload>spaceLeft,
6160 ** that means content must spill into overflow pages.
6161 ** *pnSize Size of the local cell (not counting overflow pages)
6162 ** pPrior Where to write the pgno of the first overflow page
6163 **
6164 ** Use a call to btreeParseCellPtr() to verify that the values above
6165 ** were computed correctly.
6166 */
6167 #if SQLITE_DEBUG
6168 {
6169 CellInfo info;
6170 pPage->xParseCell(pPage, pCell, &info);
6171 assert( nHeader==(int)(info.pPayload - pCell) );
6172 assert( info.nKey==pX->nKey );
6173 assert( *pnSize == info.nSize );
6174 assert( spaceLeft == info.nLocal );
6175 }
6176 #endif
6177
6178 /* Write the payload into the local Cell and any extra into overflow pages */
6179 while( nPayload>0 ){
6180 if( spaceLeft==0 ){
6181 #ifndef SQLITE_OMIT_AUTOVACUUM
6182 Pgno pgnoPtrmap = pgnoOvfl; /* Overflow page pointer-map entry page */
6183 if( pBt->autoVacuum ){
6184 do{
6185 pgnoOvfl++;
6186 } while(
6187 PTRMAP_ISPAGE(pBt, pgnoOvfl) || pgnoOvfl==PENDING_BYTE_PAGE(pBt)
6188 );
6189 }
6190 #endif
6191 rc = allocateBtreePage(pBt, &pOvfl, &pgnoOvfl, pgnoOvfl, 0);
6192 #ifndef SQLITE_OMIT_AUTOVACUUM
6193 /* If the database supports auto-vacuum, and the second or subsequent
6194 ** overflow page is being allocated, add an entry to the pointer-map
6195 ** for that page now.
6196 **
6197 ** If this is the first overflow page, then write a partial entry
6198 ** to the pointer-map. If we write nothing to this pointer-map slot,
6199 ** then the optimistic overflow chain processing in clearCell()
6200 ** may misinterpret the uninitialized values and delete the
6201 ** wrong pages from the database.
6202 */
6203 if( pBt->autoVacuum && rc==SQLITE_OK ){
6204 u8 eType = (pgnoPtrmap?PTRMAP_OVERFLOW2:PTRMAP_OVERFLOW1);
6205 ptrmapPut(pBt, pgnoOvfl, eType, pgnoPtrmap, &rc);
6206 if( rc ){
6207 releasePage(pOvfl);
6208 }
6209 }
6210 #endif
6211 if( rc ){
6212 releasePage(pToRelease);
6213 return rc;
6214 }
6215
6216 /* If pToRelease is not zero than pPrior points into the data area
6217 ** of pToRelease. Make sure pToRelease is still writeable. */
6218 assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) );
6219
6220 /* If pPrior is part of the data area of pPage, then make sure pPage
6221 ** is still writeable */
6222 assert( pPrior<pPage->aData || pPrior>=&pPage->aData[pBt->pageSize]
6223 || sqlite3PagerIswriteable(pPage->pDbPage) );
6224
6225 put4byte(pPrior, pgnoOvfl);
6226 releasePage(pToRelease);
6227 pToRelease = pOvfl;
6228 pPrior = pOvfl->aData;
6229 put4byte(pPrior, 0);
6230 pPayload = &pOvfl->aData[4];
6231 spaceLeft = pBt->usableSize - 4;
6232 }
6233 n = nPayload;
6234 if( n>spaceLeft ) n = spaceLeft;
6235
6236 /* If pToRelease is not zero than pPayload points into the data area
6237 ** of pToRelease. Make sure pToRelease is still writeable. */
6238 assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) );
6239
6240 /* If pPayload is part of the data area of pPage, then make sure pPage
6241 ** is still writeable */
6242 assert( pPayload<pPage->aData || pPayload>=&pPage->aData[pBt->pageSize]
6243 || sqlite3PagerIswriteable(pPage->pDbPage) );
6244
6245 if( nSrc>0 ){
6246 if( n>nSrc ) n = nSrc;
6247 assert( pSrc );
6248 memcpy(pPayload, pSrc, n);
6249 }else{
6250 memset(pPayload, 0, n);
6251 }
6252 nPayload -= n;
6253 pPayload += n;
6254 pSrc += n;
6255 nSrc -= n;
6256 spaceLeft -= n;
6257 }
6258 releasePage(pToRelease);
6259 return SQLITE_OK;
6260 }
6261
6262 /*
6263 ** Remove the i-th cell from pPage. This routine effects pPage only.
6264 ** The cell content is not freed or deallocated. It is assumed that
6265 ** the cell content has been copied someplace else. This routine just
6266 ** removes the reference to the cell from pPage.
6267 **
6268 ** "sz" must be the number of bytes in the cell.
6269 */
6270 static void dropCell(MemPage *pPage, int idx, int sz, int *pRC){
6271 u32 pc; /* Offset to cell content of cell being deleted */
6272 u8 *data; /* pPage->aData */
6273 u8 *ptr; /* Used to move bytes around within data[] */
6274 int rc; /* The return code */
6275 int hdr; /* Beginning of the header. 0 most pages. 100 page 1 */
6276
6277 if( *pRC ) return;
6278 assert( idx>=0 && idx<pPage->nCell );
6279 assert( CORRUPT_DB || sz==cellSize(pPage, idx) );
6280 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
6281 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
6282 data = pPage->aData;
6283 ptr = &pPage->aCellIdx[2*idx];
6284 pc = get2byte(ptr);
6285 hdr = pPage->hdrOffset;
6286 testcase( pc==get2byte(&data[hdr+5]) );
6287 testcase( pc+sz==pPage->pBt->usableSize );
6288 if( pc < (u32)get2byte(&data[hdr+5]) || pc+sz > pPage->pBt->usableSize ){
6289 *pRC = SQLITE_CORRUPT_BKPT;
6290 return;
6291 }
6292 rc = freeSpace(pPage, pc, sz);
6293 if( rc ){
6294 *pRC = rc;
6295 return;
6296 }
6297 pPage->nCell--;
6298 if( pPage->nCell==0 ){
6299 memset(&data[hdr+1], 0, 4);
6300 data[hdr+7] = 0;
6301 put2byte(&data[hdr+5], pPage->pBt->usableSize);
6302 pPage->nFree = pPage->pBt->usableSize - pPage->hdrOffset
6303 - pPage->childPtrSize - 8;
6304 }else{
6305 memmove(ptr, ptr+2, 2*(pPage->nCell - idx));
6306 put2byte(&data[hdr+3], pPage->nCell);
6307 pPage->nFree += 2;
6308 }
6309 }
6310
6311 /*
6312 ** Insert a new cell on pPage at cell index "i". pCell points to the
6313 ** content of the cell.
6314 **
6315 ** If the cell content will fit on the page, then put it there. If it
6316 ** will not fit, then make a copy of the cell content into pTemp if
6317 ** pTemp is not null. Regardless of pTemp, allocate a new entry
6318 ** in pPage->apOvfl[] and make it point to the cell content (either
6319 ** in pTemp or the original pCell) and also record its index.
6320 ** Allocating a new entry in pPage->aCell[] implies that
6321 ** pPage->nOverflow is incremented.
6322 **
6323 ** *pRC must be SQLITE_OK when this routine is called.
6324 */
6325 static void insertCell(
6326 MemPage *pPage, /* Page into which we are copying */
6327 int i, /* New cell becomes the i-th cell of the page */
6328 u8 *pCell, /* Content of the new cell */
6329 int sz, /* Bytes of content in pCell */
6330 u8 *pTemp, /* Temp storage space for pCell, if needed */
6331 Pgno iChild, /* If non-zero, replace first 4 bytes with this value */
6332 int *pRC /* Read and write return code from here */
6333 ){
6334 int idx = 0; /* Where to write new cell content in data[] */
6335 int j; /* Loop counter */
6336 u8 *data; /* The content of the whole page */
6337 u8 *pIns; /* The point in pPage->aCellIdx[] where no cell inserted */
6338
6339 assert( *pRC==SQLITE_OK );
6340 assert( i>=0 && i<=pPage->nCell+pPage->nOverflow );
6341 assert( MX_CELL(pPage->pBt)<=10921 );
6342 assert( pPage->nCell<=MX_CELL(pPage->pBt) || CORRUPT_DB );
6343 assert( pPage->nOverflow<=ArraySize(pPage->apOvfl) );
6344 assert( ArraySize(pPage->apOvfl)==ArraySize(pPage->aiOvfl) );
6345 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
6346 /* The cell should normally be sized correctly. However, when moving a
6347 ** malformed cell from a leaf page to an interior page, if the cell size
6348 ** wanted to be less than 4 but got rounded up to 4 on the leaf, then size
6349 ** might be less than 8 (leaf-size + pointer) on the interior node. Hence
6350 ** the term after the || in the following assert(). */
6351 assert( sz==pPage->xCellSize(pPage, pCell) || (sz==8 && iChild>0) );
6352 if( pPage->nOverflow || sz+2>pPage->nFree ){
6353 if( pTemp ){
6354 memcpy(pTemp, pCell, sz);
6355 pCell = pTemp;
6356 }
6357 if( iChild ){
6358 put4byte(pCell, iChild);
6359 }
6360 j = pPage->nOverflow++;
6361 /* Comparison against ArraySize-1 since we hold back one extra slot
6362 ** as a contingency. In other words, never need more than 3 overflow
6363 ** slots but 4 are allocated, just to be safe. */
6364 assert( j < ArraySize(pPage->apOvfl)-1 );
6365 pPage->apOvfl[j] = pCell;
6366 pPage->aiOvfl[j] = (u16)i;
6367
6368 /* When multiple overflows occur, they are always sequential and in
6369 ** sorted order. This invariants arise because multiple overflows can
6370 ** only occur when inserting divider cells into the parent page during
6371 ** balancing, and the dividers are adjacent and sorted.
6372 */
6373 assert( j==0 || pPage->aiOvfl[j-1]<(u16)i ); /* Overflows in sorted order */
6374 assert( j==0 || i==pPage->aiOvfl[j-1]+1 ); /* Overflows are sequential */
6375 }else{
6376 int rc = sqlite3PagerWrite(pPage->pDbPage);
6377 if( rc!=SQLITE_OK ){
6378 *pRC = rc;
6379 return;
6380 }
6381 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
6382 data = pPage->aData;
6383 assert( &data[pPage->cellOffset]==pPage->aCellIdx );
6384 rc = allocateSpace(pPage, sz, &idx);
6385 if( rc ){ *pRC = rc; return; }
6386 /* The allocateSpace() routine guarantees the following properties
6387 ** if it returns successfully */
6388 assert( idx >= 0 );
6389 assert( idx >= pPage->cellOffset+2*pPage->nCell+2 || CORRUPT_DB );
6390 assert( idx+sz <= (int)pPage->pBt->usableSize );
6391 pPage->nFree -= (u16)(2 + sz);
6392 memcpy(&data[idx], pCell, sz);
6393 if( iChild ){
6394 put4byte(&data[idx], iChild);
6395 }
6396 pIns = pPage->aCellIdx + i*2;
6397 memmove(pIns+2, pIns, 2*(pPage->nCell - i));
6398 put2byte(pIns, idx);
6399 pPage->nCell++;
6400 /* increment the cell count */
6401 if( (++data[pPage->hdrOffset+4])==0 ) data[pPage->hdrOffset+3]++;
6402 assert( get2byte(&data[pPage->hdrOffset+3])==pPage->nCell );
6403 #ifndef SQLITE_OMIT_AUTOVACUUM
6404 if( pPage->pBt->autoVacuum ){
6405 /* The cell may contain a pointer to an overflow page. If so, write
6406 ** the entry for the overflow page into the pointer map.
6407 */
6408 ptrmapPutOvflPtr(pPage, pCell, pRC);
6409 }
6410 #endif
6411 }
6412 }
6413
6414 /*
6415 ** A CellArray object contains a cache of pointers and sizes for a
6416 ** consecutive sequence of cells that might be held on multiple pages.
6417 */
6418 typedef struct CellArray CellArray;
6419 struct CellArray {
6420 int nCell; /* Number of cells in apCell[] */
6421 MemPage *pRef; /* Reference page */
6422 u8 **apCell; /* All cells begin balanced */
6423 u16 *szCell; /* Local size of all cells in apCell[] */
6424 };
6425
6426 /*
6427 ** Make sure the cell sizes at idx, idx+1, ..., idx+N-1 have been
6428 ** computed.
6429 */
6430 static void populateCellCache(CellArray *p, int idx, int N){
6431 assert( idx>=0 && idx+N<=p->nCell );
6432 while( N>0 ){
6433 assert( p->apCell[idx]!=0 );
6434 if( p->szCell[idx]==0 ){
6435 p->szCell[idx] = p->pRef->xCellSize(p->pRef, p->apCell[idx]);
6436 }else{
6437 assert( CORRUPT_DB ||
6438 p->szCell[idx]==p->pRef->xCellSize(p->pRef, p->apCell[idx]) );
6439 }
6440 idx++;
6441 N--;
6442 }
6443 }
6444
6445 /*
6446 ** Return the size of the Nth element of the cell array
6447 */
6448 static SQLITE_NOINLINE u16 computeCellSize(CellArray *p, int N){
6449 assert( N>=0 && N<p->nCell );
6450 assert( p->szCell[N]==0 );
6451 p->szCell[N] = p->pRef->xCellSize(p->pRef, p->apCell[N]);
6452 return p->szCell[N];
6453 }
6454 static u16 cachedCellSize(CellArray *p, int N){
6455 assert( N>=0 && N<p->nCell );
6456 if( p->szCell[N] ) return p->szCell[N];
6457 return computeCellSize(p, N);
6458 }
6459
6460 /*
6461 ** Array apCell[] contains pointers to nCell b-tree page cells. The
6462 ** szCell[] array contains the size in bytes of each cell. This function
6463 ** replaces the current contents of page pPg with the contents of the cell
6464 ** array.
6465 **
6466 ** Some of the cells in apCell[] may currently be stored in pPg. This
6467 ** function works around problems caused by this by making a copy of any
6468 ** such cells before overwriting the page data.
6469 **
6470 ** The MemPage.nFree field is invalidated by this function. It is the
6471 ** responsibility of the caller to set it correctly.
6472 */
6473 static int rebuildPage(
6474 MemPage *pPg, /* Edit this page */
6475 int nCell, /* Final number of cells on page */
6476 u8 **apCell, /* Array of cells */
6477 u16 *szCell /* Array of cell sizes */
6478 ){
6479 const int hdr = pPg->hdrOffset; /* Offset of header on pPg */
6480 u8 * const aData = pPg->aData; /* Pointer to data for pPg */
6481 const int usableSize = pPg->pBt->usableSize;
6482 u8 * const pEnd = &aData[usableSize];
6483 int i;
6484 u8 *pCellptr = pPg->aCellIdx;
6485 u8 *pTmp = sqlite3PagerTempSpace(pPg->pBt->pPager);
6486 u8 *pData;
6487
6488 i = get2byte(&aData[hdr+5]);
6489 memcpy(&pTmp[i], &aData[i], usableSize - i);
6490
6491 pData = pEnd;
6492 for(i=0; i<nCell; i++){
6493 u8 *pCell = apCell[i];
6494 if( SQLITE_WITHIN(pCell,aData,pEnd) ){
6495 pCell = &pTmp[pCell - aData];
6496 }
6497 pData -= szCell[i];
6498 put2byte(pCellptr, (pData - aData));
6499 pCellptr += 2;
6500 if( pData < pCellptr ) return SQLITE_CORRUPT_BKPT;
6501 memcpy(pData, pCell, szCell[i]);
6502 assert( szCell[i]==pPg->xCellSize(pPg, pCell) || CORRUPT_DB );
6503 testcase( szCell[i]!=pPg->xCellSize(pPg,pCell) );
6504 }
6505
6506 /* The pPg->nFree field is now set incorrectly. The caller will fix it. */
6507 pPg->nCell = nCell;
6508 pPg->nOverflow = 0;
6509
6510 put2byte(&aData[hdr+1], 0);
6511 put2byte(&aData[hdr+3], pPg->nCell);
6512 put2byte(&aData[hdr+5], pData - aData);
6513 aData[hdr+7] = 0x00;
6514 return SQLITE_OK;
6515 }
6516
6517 /*
6518 ** Array apCell[] contains nCell pointers to b-tree cells. Array szCell
6519 ** contains the size in bytes of each such cell. This function attempts to
6520 ** add the cells stored in the array to page pPg. If it cannot (because
6521 ** the page needs to be defragmented before the cells will fit), non-zero
6522 ** is returned. Otherwise, if the cells are added successfully, zero is
6523 ** returned.
6524 **
6525 ** Argument pCellptr points to the first entry in the cell-pointer array
6526 ** (part of page pPg) to populate. After cell apCell[0] is written to the
6527 ** page body, a 16-bit offset is written to pCellptr. And so on, for each
6528 ** cell in the array. It is the responsibility of the caller to ensure
6529 ** that it is safe to overwrite this part of the cell-pointer array.
6530 **
6531 ** When this function is called, *ppData points to the start of the
6532 ** content area on page pPg. If the size of the content area is extended,
6533 ** *ppData is updated to point to the new start of the content area
6534 ** before returning.
6535 **
6536 ** Finally, argument pBegin points to the byte immediately following the
6537 ** end of the space required by this page for the cell-pointer area (for
6538 ** all cells - not just those inserted by the current call). If the content
6539 ** area must be extended to before this point in order to accomodate all
6540 ** cells in apCell[], then the cells do not fit and non-zero is returned.
6541 */
6542 static int pageInsertArray(
6543 MemPage *pPg, /* Page to add cells to */
6544 u8 *pBegin, /* End of cell-pointer array */
6545 u8 **ppData, /* IN/OUT: Page content -area pointer */
6546 u8 *pCellptr, /* Pointer to cell-pointer area */
6547 int iFirst, /* Index of first cell to add */
6548 int nCell, /* Number of cells to add to pPg */
6549 CellArray *pCArray /* Array of cells */
6550 ){
6551 int i;
6552 u8 *aData = pPg->aData;
6553 u8 *pData = *ppData;
6554 int iEnd = iFirst + nCell;
6555 assert( CORRUPT_DB || pPg->hdrOffset==0 ); /* Never called on page 1 */
6556 for(i=iFirst; i<iEnd; i++){
6557 int sz, rc;
6558 u8 *pSlot;
6559 sz = cachedCellSize(pCArray, i);
6560 if( (aData[1]==0 && aData[2]==0) || (pSlot = pageFindSlot(pPg,sz,&rc))==0 ){
6561 if( (pData - pBegin)<sz ) return 1;
6562 pData -= sz;
6563 pSlot = pData;
6564 }
6565 /* pSlot and pCArray->apCell[i] will never overlap on a well-formed
6566 ** database. But they might for a corrupt database. Hence use memmove()
6567 ** since memcpy() sends SIGABORT with overlapping buffers on OpenBSD */
6568 assert( (pSlot+sz)<=pCArray->apCell[i]
6569 || pSlot>=(pCArray->apCell[i]+sz)
6570 || CORRUPT_DB );
6571 memmove(pSlot, pCArray->apCell[i], sz);
6572 put2byte(pCellptr, (pSlot - aData));
6573 pCellptr += 2;
6574 }
6575 *ppData = pData;
6576 return 0;
6577 }
6578
6579 /*
6580 ** Array apCell[] contains nCell pointers to b-tree cells. Array szCell
6581 ** contains the size in bytes of each such cell. This function adds the
6582 ** space associated with each cell in the array that is currently stored
6583 ** within the body of pPg to the pPg free-list. The cell-pointers and other
6584 ** fields of the page are not updated.
6585 **
6586 ** This function returns the total number of cells added to the free-list.
6587 */
6588 static int pageFreeArray(
6589 MemPage *pPg, /* Page to edit */
6590 int iFirst, /* First cell to delete */
6591 int nCell, /* Cells to delete */
6592 CellArray *pCArray /* Array of cells */
6593 ){
6594 u8 * const aData = pPg->aData;
6595 u8 * const pEnd = &aData[pPg->pBt->usableSize];
6596 u8 * const pStart = &aData[pPg->hdrOffset + 8 + pPg->childPtrSize];
6597 int nRet = 0;
6598 int i;
6599 int iEnd = iFirst + nCell;
6600 u8 *pFree = 0;
6601 int szFree = 0;
6602
6603 for(i=iFirst; i<iEnd; i++){
6604 u8 *pCell = pCArray->apCell[i];
6605 if( SQLITE_WITHIN(pCell, pStart, pEnd) ){
6606 int sz;
6607 /* No need to use cachedCellSize() here. The sizes of all cells that
6608 ** are to be freed have already been computing while deciding which
6609 ** cells need freeing */
6610 sz = pCArray->szCell[i]; assert( sz>0 );
6611 if( pFree!=(pCell + sz) ){
6612 if( pFree ){
6613 assert( pFree>aData && (pFree - aData)<65536 );
6614 freeSpace(pPg, (u16)(pFree - aData), szFree);
6615 }
6616 pFree = pCell;
6617 szFree = sz;
6618 if( pFree+sz>pEnd ) return 0;
6619 }else{
6620 pFree = pCell;
6621 szFree += sz;
6622 }
6623 nRet++;
6624 }
6625 }
6626 if( pFree ){
6627 assert( pFree>aData && (pFree - aData)<65536 );
6628 freeSpace(pPg, (u16)(pFree - aData), szFree);
6629 }
6630 return nRet;
6631 }
6632
6633 /*
6634 ** apCell[] and szCell[] contains pointers to and sizes of all cells in the
6635 ** pages being balanced. The current page, pPg, has pPg->nCell cells starting
6636 ** with apCell[iOld]. After balancing, this page should hold nNew cells
6637 ** starting at apCell[iNew].
6638 **
6639 ** This routine makes the necessary adjustments to pPg so that it contains
6640 ** the correct cells after being balanced.
6641 **
6642 ** The pPg->nFree field is invalid when this function returns. It is the
6643 ** responsibility of the caller to set it correctly.
6644 */
6645 static int editPage(
6646 MemPage *pPg, /* Edit this page */
6647 int iOld, /* Index of first cell currently on page */
6648 int iNew, /* Index of new first cell on page */
6649 int nNew, /* Final number of cells on page */
6650 CellArray *pCArray /* Array of cells and sizes */
6651 ){
6652 u8 * const aData = pPg->aData;
6653 const int hdr = pPg->hdrOffset;
6654 u8 *pBegin = &pPg->aCellIdx[nNew * 2];
6655 int nCell = pPg->nCell; /* Cells stored on pPg */
6656 u8 *pData;
6657 u8 *pCellptr;
6658 int i;
6659 int iOldEnd = iOld + pPg->nCell + pPg->nOverflow;
6660 int iNewEnd = iNew + nNew;
6661
6662 #ifdef SQLITE_DEBUG
6663 u8 *pTmp = sqlite3PagerTempSpace(pPg->pBt->pPager);
6664 memcpy(pTmp, aData, pPg->pBt->usableSize);
6665 #endif
6666
6667 /* Remove cells from the start and end of the page */
6668 if( iOld<iNew ){
6669 int nShift = pageFreeArray(pPg, iOld, iNew-iOld, pCArray);
6670 memmove(pPg->aCellIdx, &pPg->aCellIdx[nShift*2], nCell*2);
6671 nCell -= nShift;
6672 }
6673 if( iNewEnd < iOldEnd ){
6674 nCell -= pageFreeArray(pPg, iNewEnd, iOldEnd - iNewEnd, pCArray);
6675 }
6676
6677 pData = &aData[get2byteNotZero(&aData[hdr+5])];
6678 if( pData<pBegin ) goto editpage_fail;
6679
6680 /* Add cells to the start of the page */
6681 if( iNew<iOld ){
6682 int nAdd = MIN(nNew,iOld-iNew);
6683 assert( (iOld-iNew)<nNew || nCell==0 || CORRUPT_DB );
6684 pCellptr = pPg->aCellIdx;
6685 memmove(&pCellptr[nAdd*2], pCellptr, nCell*2);
6686 if( pageInsertArray(
6687 pPg, pBegin, &pData, pCellptr,
6688 iNew, nAdd, pCArray
6689 ) ) goto editpage_fail;
6690 nCell += nAdd;
6691 }
6692
6693 /* Add any overflow cells */
6694 for(i=0; i<pPg->nOverflow; i++){
6695 int iCell = (iOld + pPg->aiOvfl[i]) - iNew;
6696 if( iCell>=0 && iCell<nNew ){
6697 pCellptr = &pPg->aCellIdx[iCell * 2];
6698 memmove(&pCellptr[2], pCellptr, (nCell - iCell) * 2);
6699 nCell++;
6700 if( pageInsertArray(
6701 pPg, pBegin, &pData, pCellptr,
6702 iCell+iNew, 1, pCArray
6703 ) ) goto editpage_fail;
6704 }
6705 }
6706
6707 /* Append cells to the end of the page */
6708 pCellptr = &pPg->aCellIdx[nCell*2];
6709 if( pageInsertArray(
6710 pPg, pBegin, &pData, pCellptr,
6711 iNew+nCell, nNew-nCell, pCArray
6712 ) ) goto editpage_fail;
6713
6714 pPg->nCell = nNew;
6715 pPg->nOverflow = 0;
6716
6717 put2byte(&aData[hdr+3], pPg->nCell);
6718 put2byte(&aData[hdr+5], pData - aData);
6719
6720 #ifdef SQLITE_DEBUG
6721 for(i=0; i<nNew && !CORRUPT_DB; i++){
6722 u8 *pCell = pCArray->apCell[i+iNew];
6723 int iOff = get2byteAligned(&pPg->aCellIdx[i*2]);
6724 if( SQLITE_WITHIN(pCell, aData, &aData[pPg->pBt->usableSize]) ){
6725 pCell = &pTmp[pCell - aData];
6726 }
6727 assert( 0==memcmp(pCell, &aData[iOff],
6728 pCArray->pRef->xCellSize(pCArray->pRef, pCArray->apCell[i+iNew])) );
6729 }
6730 #endif
6731
6732 return SQLITE_OK;
6733 editpage_fail:
6734 /* Unable to edit this page. Rebuild it from scratch instead. */
6735 populateCellCache(pCArray, iNew, nNew);
6736 return rebuildPage(pPg, nNew, &pCArray->apCell[iNew], &pCArray->szCell[iNew]);
6737 }
6738
6739 /*
6740 ** The following parameters determine how many adjacent pages get involved
6741 ** in a balancing operation. NN is the number of neighbors on either side
6742 ** of the page that participate in the balancing operation. NB is the
6743 ** total number of pages that participate, including the target page and
6744 ** NN neighbors on either side.
6745 **
6746 ** The minimum value of NN is 1 (of course). Increasing NN above 1
6747 ** (to 2 or 3) gives a modest improvement in SELECT and DELETE performance
6748 ** in exchange for a larger degradation in INSERT and UPDATE performance.
6749 ** The value of NN appears to give the best results overall.
6750 */
6751 #define NN 1 /* Number of neighbors on either side of pPage */
6752 #define NB (NN*2+1) /* Total pages involved in the balance */
6753
6754
6755 #ifndef SQLITE_OMIT_QUICKBALANCE
6756 /*
6757 ** This version of balance() handles the common special case where
6758 ** a new entry is being inserted on the extreme right-end of the
6759 ** tree, in other words, when the new entry will become the largest
6760 ** entry in the tree.
6761 **
6762 ** Instead of trying to balance the 3 right-most leaf pages, just add
6763 ** a new page to the right-hand side and put the one new entry in
6764 ** that page. This leaves the right side of the tree somewhat
6765 ** unbalanced. But odds are that we will be inserting new entries
6766 ** at the end soon afterwards so the nearly empty page will quickly
6767 ** fill up. On average.
6768 **
6769 ** pPage is the leaf page which is the right-most page in the tree.
6770 ** pParent is its parent. pPage must have a single overflow entry
6771 ** which is also the right-most entry on the page.
6772 **
6773 ** The pSpace buffer is used to store a temporary copy of the divider
6774 ** cell that will be inserted into pParent. Such a cell consists of a 4
6775 ** byte page number followed by a variable length integer. In other
6776 ** words, at most 13 bytes. Hence the pSpace buffer must be at
6777 ** least 13 bytes in size.
6778 */
6779 static int balance_quick(MemPage *pParent, MemPage *pPage, u8 *pSpace){
6780 BtShared *const pBt = pPage->pBt; /* B-Tree Database */
6781 MemPage *pNew; /* Newly allocated page */
6782 int rc; /* Return Code */
6783 Pgno pgnoNew; /* Page number of pNew */
6784
6785 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
6786 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
6787 assert( pPage->nOverflow==1 );
6788
6789 /* This error condition is now caught prior to reaching this function */
6790 if( NEVER(pPage->nCell==0) ) return SQLITE_CORRUPT_BKPT;
6791
6792 /* Allocate a new page. This page will become the right-sibling of
6793 ** pPage. Make the parent page writable, so that the new divider cell
6794 ** may be inserted. If both these operations are successful, proceed.
6795 */
6796 rc = allocateBtreePage(pBt, &pNew, &pgnoNew, 0, 0);
6797
6798 if( rc==SQLITE_OK ){
6799
6800 u8 *pOut = &pSpace[4];
6801 u8 *pCell = pPage->apOvfl[0];
6802 u16 szCell = pPage->xCellSize(pPage, pCell);
6803 u8 *pStop;
6804
6805 assert( sqlite3PagerIswriteable(pNew->pDbPage) );
6806 assert( pPage->aData[0]==(PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF) );
6807 zeroPage(pNew, PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF);
6808 rc = rebuildPage(pNew, 1, &pCell, &szCell);
6809 if( NEVER(rc) ) return rc;
6810 pNew->nFree = pBt->usableSize - pNew->cellOffset - 2 - szCell;
6811
6812 /* If this is an auto-vacuum database, update the pointer map
6813 ** with entries for the new page, and any pointer from the
6814 ** cell on the page to an overflow page. If either of these
6815 ** operations fails, the return code is set, but the contents
6816 ** of the parent page are still manipulated by thh code below.
6817 ** That is Ok, at this point the parent page is guaranteed to
6818 ** be marked as dirty. Returning an error code will cause a
6819 ** rollback, undoing any changes made to the parent page.
6820 */
6821 if( ISAUTOVACUUM ){
6822 ptrmapPut(pBt, pgnoNew, PTRMAP_BTREE, pParent->pgno, &rc);
6823 if( szCell>pNew->minLocal ){
6824 ptrmapPutOvflPtr(pNew, pCell, &rc);
6825 }
6826 }
6827
6828 /* Create a divider cell to insert into pParent. The divider cell
6829 ** consists of a 4-byte page number (the page number of pPage) and
6830 ** a variable length key value (which must be the same value as the
6831 ** largest key on pPage).
6832 **
6833 ** To find the largest key value on pPage, first find the right-most
6834 ** cell on pPage. The first two fields of this cell are the
6835 ** record-length (a variable length integer at most 32-bits in size)
6836 ** and the key value (a variable length integer, may have any value).
6837 ** The first of the while(...) loops below skips over the record-length
6838 ** field. The second while(...) loop copies the key value from the
6839 ** cell on pPage into the pSpace buffer.
6840 */
6841 pCell = findCell(pPage, pPage->nCell-1);
6842 pStop = &pCell[9];
6843 while( (*(pCell++)&0x80) && pCell<pStop );
6844 pStop = &pCell[9];
6845 while( ((*(pOut++) = *(pCell++))&0x80) && pCell<pStop );
6846
6847 /* Insert the new divider cell into pParent. */
6848 if( rc==SQLITE_OK ){
6849 insertCell(pParent, pParent->nCell, pSpace, (int)(pOut-pSpace),
6850 0, pPage->pgno, &rc);
6851 }
6852
6853 /* Set the right-child pointer of pParent to point to the new page. */
6854 put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew);
6855
6856 /* Release the reference to the new page. */
6857 releasePage(pNew);
6858 }
6859
6860 return rc;
6861 }
6862 #endif /* SQLITE_OMIT_QUICKBALANCE */
6863
6864 #if 0
6865 /*
6866 ** This function does not contribute anything to the operation of SQLite.
6867 ** it is sometimes activated temporarily while debugging code responsible
6868 ** for setting pointer-map entries.
6869 */
6870 static int ptrmapCheckPages(MemPage **apPage, int nPage){
6871 int i, j;
6872 for(i=0; i<nPage; i++){
6873 Pgno n;
6874 u8 e;
6875 MemPage *pPage = apPage[i];
6876 BtShared *pBt = pPage->pBt;
6877 assert( pPage->isInit );
6878
6879 for(j=0; j<pPage->nCell; j++){
6880 CellInfo info;
6881 u8 *z;
6882
6883 z = findCell(pPage, j);
6884 pPage->xParseCell(pPage, z, &info);
6885 if( info.nLocal<info.nPayload ){
6886 Pgno ovfl = get4byte(&z[info.nSize-4]);
6887 ptrmapGet(pBt, ovfl, &e, &n);
6888 assert( n==pPage->pgno && e==PTRMAP_OVERFLOW1 );
6889 }
6890 if( !pPage->leaf ){
6891 Pgno child = get4byte(z);
6892 ptrmapGet(pBt, child, &e, &n);
6893 assert( n==pPage->pgno && e==PTRMAP_BTREE );
6894 }
6895 }
6896 if( !pPage->leaf ){
6897 Pgno child = get4byte(&pPage->aData[pPage->hdrOffset+8]);
6898 ptrmapGet(pBt, child, &e, &n);
6899 assert( n==pPage->pgno && e==PTRMAP_BTREE );
6900 }
6901 }
6902 return 1;
6903 }
6904 #endif
6905
6906 /*
6907 ** This function is used to copy the contents of the b-tree node stored
6908 ** on page pFrom to page pTo. If page pFrom was not a leaf page, then
6909 ** the pointer-map entries for each child page are updated so that the
6910 ** parent page stored in the pointer map is page pTo. If pFrom contained
6911 ** any cells with overflow page pointers, then the corresponding pointer
6912 ** map entries are also updated so that the parent page is page pTo.
6913 **
6914 ** If pFrom is currently carrying any overflow cells (entries in the
6915 ** MemPage.apOvfl[] array), they are not copied to pTo.
6916 **
6917 ** Before returning, page pTo is reinitialized using btreeInitPage().
6918 **
6919 ** The performance of this function is not critical. It is only used by
6920 ** the balance_shallower() and balance_deeper() procedures, neither of
6921 ** which are called often under normal circumstances.
6922 */
6923 static void copyNodeContent(MemPage *pFrom, MemPage *pTo, int *pRC){
6924 if( (*pRC)==SQLITE_OK ){
6925 BtShared * const pBt = pFrom->pBt;
6926 u8 * const aFrom = pFrom->aData;
6927 u8 * const aTo = pTo->aData;
6928 int const iFromHdr = pFrom->hdrOffset;
6929 int const iToHdr = ((pTo->pgno==1) ? 100 : 0);
6930 int rc;
6931 int iData;
6932
6933
6934 assert( pFrom->isInit );
6935 assert( pFrom->nFree>=iToHdr );
6936 assert( get2byte(&aFrom[iFromHdr+5]) <= (int)pBt->usableSize );
6937
6938 /* Copy the b-tree node content from page pFrom to page pTo. */
6939 iData = get2byte(&aFrom[iFromHdr+5]);
6940 memcpy(&aTo[iData], &aFrom[iData], pBt->usableSize-iData);
6941 memcpy(&aTo[iToHdr], &aFrom[iFromHdr], pFrom->cellOffset + 2*pFrom->nCell);
6942
6943 /* Reinitialize page pTo so that the contents of the MemPage structure
6944 ** match the new data. The initialization of pTo can actually fail under
6945 ** fairly obscure circumstances, even though it is a copy of initialized
6946 ** page pFrom.
6947 */
6948 pTo->isInit = 0;
6949 rc = btreeInitPage(pTo);
6950 if( rc!=SQLITE_OK ){
6951 *pRC = rc;
6952 return;
6953 }
6954
6955 /* If this is an auto-vacuum database, update the pointer-map entries
6956 ** for any b-tree or overflow pages that pTo now contains the pointers to.
6957 */
6958 if( ISAUTOVACUUM ){
6959 *pRC = setChildPtrmaps(pTo);
6960 }
6961 }
6962 }
6963
6964 /*
6965 ** This routine redistributes cells on the iParentIdx'th child of pParent
6966 ** (hereafter "the page") and up to 2 siblings so that all pages have about the
6967 ** same amount of free space. Usually a single sibling on either side of the
6968 ** page are used in the balancing, though both siblings might come from one
6969 ** side if the page is the first or last child of its parent. If the page
6970 ** has fewer than 2 siblings (something which can only happen if the page
6971 ** is a root page or a child of a root page) then all available siblings
6972 ** participate in the balancing.
6973 **
6974 ** The number of siblings of the page might be increased or decreased by
6975 ** one or two in an effort to keep pages nearly full but not over full.
6976 **
6977 ** Note that when this routine is called, some of the cells on the page
6978 ** might not actually be stored in MemPage.aData[]. This can happen
6979 ** if the page is overfull. This routine ensures that all cells allocated
6980 ** to the page and its siblings fit into MemPage.aData[] before returning.
6981 **
6982 ** In the course of balancing the page and its siblings, cells may be
6983 ** inserted into or removed from the parent page (pParent). Doing so
6984 ** may cause the parent page to become overfull or underfull. If this
6985 ** happens, it is the responsibility of the caller to invoke the correct
6986 ** balancing routine to fix this problem (see the balance() routine).
6987 **
6988 ** If this routine fails for any reason, it might leave the database
6989 ** in a corrupted state. So if this routine fails, the database should
6990 ** be rolled back.
6991 **
6992 ** The third argument to this function, aOvflSpace, is a pointer to a
6993 ** buffer big enough to hold one page. If while inserting cells into the parent
6994 ** page (pParent) the parent page becomes overfull, this buffer is
6995 ** used to store the parent's overflow cells. Because this function inserts
6996 ** a maximum of four divider cells into the parent page, and the maximum
6997 ** size of a cell stored within an internal node is always less than 1/4
6998 ** of the page-size, the aOvflSpace[] buffer is guaranteed to be large
6999 ** enough for all overflow cells.
7000 **
7001 ** If aOvflSpace is set to a null pointer, this function returns
7002 ** SQLITE_NOMEM.
7003 */
7004 static int balance_nonroot(
7005 MemPage *pParent, /* Parent page of siblings being balanced */
7006 int iParentIdx, /* Index of "the page" in pParent */
7007 u8 *aOvflSpace, /* page-size bytes of space for parent ovfl */
7008 int isRoot, /* True if pParent is a root-page */
7009 int bBulk /* True if this call is part of a bulk load */
7010 ){
7011 BtShared *pBt; /* The whole database */
7012 int nMaxCells = 0; /* Allocated size of apCell, szCell, aFrom. */
7013 int nNew = 0; /* Number of pages in apNew[] */
7014 int nOld; /* Number of pages in apOld[] */
7015 int i, j, k; /* Loop counters */
7016 int nxDiv; /* Next divider slot in pParent->aCell[] */
7017 int rc = SQLITE_OK; /* The return code */
7018 u16 leafCorrection; /* 4 if pPage is a leaf. 0 if not */
7019 int leafData; /* True if pPage is a leaf of a LEAFDATA tree */
7020 int usableSpace; /* Bytes in pPage beyond the header */
7021 int pageFlags; /* Value of pPage->aData[0] */
7022 int iSpace1 = 0; /* First unused byte of aSpace1[] */
7023 int iOvflSpace = 0; /* First unused byte of aOvflSpace[] */
7024 int szScratch; /* Size of scratch memory requested */
7025 MemPage *apOld[NB]; /* pPage and up to two siblings */
7026 MemPage *apNew[NB+2]; /* pPage and up to NB siblings after balancing */
7027 u8 *pRight; /* Location in parent of right-sibling pointer */
7028 u8 *apDiv[NB-1]; /* Divider cells in pParent */
7029 int cntNew[NB+2]; /* Index in b.paCell[] of cell after i-th page */
7030 int cntOld[NB+2]; /* Old index in b.apCell[] */
7031 int szNew[NB+2]; /* Combined size of cells placed on i-th page */
7032 u8 *aSpace1; /* Space for copies of dividers cells */
7033 Pgno pgno; /* Temp var to store a page number in */
7034 u8 abDone[NB+2]; /* True after i'th new page is populated */
7035 Pgno aPgno[NB+2]; /* Page numbers of new pages before shuffling */
7036 Pgno aPgOrder[NB+2]; /* Copy of aPgno[] used for sorting pages */
7037 u16 aPgFlags[NB+2]; /* flags field of new pages before shuffling */
7038 CellArray b; /* Parsed information on cells being balanced */
7039
7040 memset(abDone, 0, sizeof(abDone));
7041 b.nCell = 0;
7042 b.apCell = 0;
7043 pBt = pParent->pBt;
7044 assert( sqlite3_mutex_held(pBt->mutex) );
7045 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
7046
7047 #if 0
7048 TRACE(("BALANCE: begin page %d child of %d\n", pPage->pgno, pParent->pgno));
7049 #endif
7050
7051 /* At this point pParent may have at most one overflow cell. And if
7052 ** this overflow cell is present, it must be the cell with
7053 ** index iParentIdx. This scenario comes about when this function
7054 ** is called (indirectly) from sqlite3BtreeDelete().
7055 */
7056 assert( pParent->nOverflow==0 || pParent->nOverflow==1 );
7057 assert( pParent->nOverflow==0 || pParent->aiOvfl[0]==iParentIdx );
7058
7059 if( !aOvflSpace ){
7060 return SQLITE_NOMEM_BKPT;
7061 }
7062
7063 /* Find the sibling pages to balance. Also locate the cells in pParent
7064 ** that divide the siblings. An attempt is made to find NN siblings on
7065 ** either side of pPage. More siblings are taken from one side, however,
7066 ** if there are fewer than NN siblings on the other side. If pParent
7067 ** has NB or fewer children then all children of pParent are taken.
7068 **
7069 ** This loop also drops the divider cells from the parent page. This
7070 ** way, the remainder of the function does not have to deal with any
7071 ** overflow cells in the parent page, since if any existed they will
7072 ** have already been removed.
7073 */
7074 i = pParent->nOverflow + pParent->nCell;
7075 if( i<2 ){
7076 nxDiv = 0;
7077 }else{
7078 assert( bBulk==0 || bBulk==1 );
7079 if( iParentIdx==0 ){
7080 nxDiv = 0;
7081 }else if( iParentIdx==i ){
7082 nxDiv = i-2+bBulk;
7083 }else{
7084 nxDiv = iParentIdx-1;
7085 }
7086 i = 2-bBulk;
7087 }
7088 nOld = i+1;
7089 if( (i+nxDiv-pParent->nOverflow)==pParent->nCell ){
7090 pRight = &pParent->aData[pParent->hdrOffset+8];
7091 }else{
7092 pRight = findCell(pParent, i+nxDiv-pParent->nOverflow);
7093 }
7094 pgno = get4byte(pRight);
7095 while( 1 ){
7096 rc = getAndInitPage(pBt, pgno, &apOld[i], 0, 0);
7097 if( rc ){
7098 memset(apOld, 0, (i+1)*sizeof(MemPage*));
7099 goto balance_cleanup;
7100 }
7101 nMaxCells += 1+apOld[i]->nCell+apOld[i]->nOverflow;
7102 if( (i--)==0 ) break;
7103
7104 if( pParent->nOverflow && i+nxDiv==pParent->aiOvfl[0] ){
7105 apDiv[i] = pParent->apOvfl[0];
7106 pgno = get4byte(apDiv[i]);
7107 szNew[i] = pParent->xCellSize(pParent, apDiv[i]);
7108 pParent->nOverflow = 0;
7109 }else{
7110 apDiv[i] = findCell(pParent, i+nxDiv-pParent->nOverflow);
7111 pgno = get4byte(apDiv[i]);
7112 szNew[i] = pParent->xCellSize(pParent, apDiv[i]);
7113
7114 /* Drop the cell from the parent page. apDiv[i] still points to
7115 ** the cell within the parent, even though it has been dropped.
7116 ** This is safe because dropping a cell only overwrites the first
7117 ** four bytes of it, and this function does not need the first
7118 ** four bytes of the divider cell. So the pointer is safe to use
7119 ** later on.
7120 **
7121 ** But not if we are in secure-delete mode. In secure-delete mode,
7122 ** the dropCell() routine will overwrite the entire cell with zeroes.
7123 ** In this case, temporarily copy the cell into the aOvflSpace[]
7124 ** buffer. It will be copied out again as soon as the aSpace[] buffer
7125 ** is allocated. */
7126 if( pBt->btsFlags & BTS_SECURE_DELETE ){
7127 int iOff;
7128
7129 iOff = SQLITE_PTR_TO_INT(apDiv[i]) - SQLITE_PTR_TO_INT(pParent->aData);
7130 if( (iOff+szNew[i])>(int)pBt->usableSize ){
7131 rc = SQLITE_CORRUPT_BKPT;
7132 memset(apOld, 0, (i+1)*sizeof(MemPage*));
7133 goto balance_cleanup;
7134 }else{
7135 memcpy(&aOvflSpace[iOff], apDiv[i], szNew[i]);
7136 apDiv[i] = &aOvflSpace[apDiv[i]-pParent->aData];
7137 }
7138 }
7139 dropCell(pParent, i+nxDiv-pParent->nOverflow, szNew[i], &rc);
7140 }
7141 }
7142
7143 /* Make nMaxCells a multiple of 4 in order to preserve 8-byte
7144 ** alignment */
7145 nMaxCells = (nMaxCells + 3)&~3;
7146
7147 /*
7148 ** Allocate space for memory structures
7149 */
7150 szScratch =
7151 nMaxCells*sizeof(u8*) /* b.apCell */
7152 + nMaxCells*sizeof(u16) /* b.szCell */
7153 + pBt->pageSize; /* aSpace1 */
7154
7155 /* EVIDENCE-OF: R-28375-38319 SQLite will never request a scratch buffer
7156 ** that is more than 6 times the database page size. */
7157 assert( szScratch<=6*(int)pBt->pageSize );
7158 b.apCell = sqlite3ScratchMalloc( szScratch );
7159 if( b.apCell==0 ){
7160 rc = SQLITE_NOMEM_BKPT;
7161 goto balance_cleanup;
7162 }
7163 b.szCell = (u16*)&b.apCell[nMaxCells];
7164 aSpace1 = (u8*)&b.szCell[nMaxCells];
7165 assert( EIGHT_BYTE_ALIGNMENT(aSpace1) );
7166
7167 /*
7168 ** Load pointers to all cells on sibling pages and the divider cells
7169 ** into the local b.apCell[] array. Make copies of the divider cells
7170 ** into space obtained from aSpace1[]. The divider cells have already
7171 ** been removed from pParent.
7172 **
7173 ** If the siblings are on leaf pages, then the child pointers of the
7174 ** divider cells are stripped from the cells before they are copied
7175 ** into aSpace1[]. In this way, all cells in b.apCell[] are without
7176 ** child pointers. If siblings are not leaves, then all cell in
7177 ** b.apCell[] include child pointers. Either way, all cells in b.apCell[]
7178 ** are alike.
7179 **
7180 ** leafCorrection: 4 if pPage is a leaf. 0 if pPage is not a leaf.
7181 ** leafData: 1 if pPage holds key+data and pParent holds only keys.
7182 */
7183 b.pRef = apOld[0];
7184 leafCorrection = b.pRef->leaf*4;
7185 leafData = b.pRef->intKeyLeaf;
7186 for(i=0; i<nOld; i++){
7187 MemPage *pOld = apOld[i];
7188 int limit = pOld->nCell;
7189 u8 *aData = pOld->aData;
7190 u16 maskPage = pOld->maskPage;
7191 u8 *piCell = aData + pOld->cellOffset;
7192 u8 *piEnd;
7193
7194 /* Verify that all sibling pages are of the same "type" (table-leaf,
7195 ** table-interior, index-leaf, or index-interior).
7196 */
7197 if( pOld->aData[0]!=apOld[0]->aData[0] ){
7198 rc = SQLITE_CORRUPT_BKPT;
7199 goto balance_cleanup;
7200 }
7201
7202 /* Load b.apCell[] with pointers to all cells in pOld. If pOld
7203 ** constains overflow cells, include them in the b.apCell[] array
7204 ** in the correct spot.
7205 **
7206 ** Note that when there are multiple overflow cells, it is always the
7207 ** case that they are sequential and adjacent. This invariant arises
7208 ** because multiple overflows can only occurs when inserting divider
7209 ** cells into a parent on a prior balance, and divider cells are always
7210 ** adjacent and are inserted in order. There is an assert() tagged
7211 ** with "NOTE 1" in the overflow cell insertion loop to prove this
7212 ** invariant.
7213 **
7214 ** This must be done in advance. Once the balance starts, the cell
7215 ** offset section of the btree page will be overwritten and we will no
7216 ** long be able to find the cells if a pointer to each cell is not saved
7217 ** first.
7218 */
7219 memset(&b.szCell[b.nCell], 0, sizeof(b.szCell[0])*(limit+pOld->nOverflow));
7220 if( pOld->nOverflow>0 ){
7221 limit = pOld->aiOvfl[0];
7222 for(j=0; j<limit; j++){
7223 b.apCell[b.nCell] = aData + (maskPage & get2byteAligned(piCell));
7224 piCell += 2;
7225 b.nCell++;
7226 }
7227 for(k=0; k<pOld->nOverflow; k++){
7228 assert( k==0 || pOld->aiOvfl[k-1]+1==pOld->aiOvfl[k] );/* NOTE 1 */
7229 b.apCell[b.nCell] = pOld->apOvfl[k];
7230 b.nCell++;
7231 }
7232 }
7233 piEnd = aData + pOld->cellOffset + 2*pOld->nCell;
7234 while( piCell<piEnd ){
7235 assert( b.nCell<nMaxCells );
7236 b.apCell[b.nCell] = aData + (maskPage & get2byteAligned(piCell));
7237 piCell += 2;
7238 b.nCell++;
7239 }
7240
7241 cntOld[i] = b.nCell;
7242 if( i<nOld-1 && !leafData){
7243 u16 sz = (u16)szNew[i];
7244 u8 *pTemp;
7245 assert( b.nCell<nMaxCells );
7246 b.szCell[b.nCell] = sz;
7247 pTemp = &aSpace1[iSpace1];
7248 iSpace1 += sz;
7249 assert( sz<=pBt->maxLocal+23 );
7250 assert( iSpace1 <= (int)pBt->pageSize );
7251 memcpy(pTemp, apDiv[i], sz);
7252 b.apCell[b.nCell] = pTemp+leafCorrection;
7253 assert( leafCorrection==0 || leafCorrection==4 );
7254 b.szCell[b.nCell] = b.szCell[b.nCell] - leafCorrection;
7255 if( !pOld->leaf ){
7256 assert( leafCorrection==0 );
7257 assert( pOld->hdrOffset==0 );
7258 /* The right pointer of the child page pOld becomes the left
7259 ** pointer of the divider cell */
7260 memcpy(b.apCell[b.nCell], &pOld->aData[8], 4);
7261 }else{
7262 assert( leafCorrection==4 );
7263 while( b.szCell[b.nCell]<4 ){
7264 /* Do not allow any cells smaller than 4 bytes. If a smaller cell
7265 ** does exist, pad it with 0x00 bytes. */
7266 assert( b.szCell[b.nCell]==3 || CORRUPT_DB );
7267 assert( b.apCell[b.nCell]==&aSpace1[iSpace1-3] || CORRUPT_DB );
7268 aSpace1[iSpace1++] = 0x00;
7269 b.szCell[b.nCell]++;
7270 }
7271 }
7272 b.nCell++;
7273 }
7274 }
7275
7276 /*
7277 ** Figure out the number of pages needed to hold all b.nCell cells.
7278 ** Store this number in "k". Also compute szNew[] which is the total
7279 ** size of all cells on the i-th page and cntNew[] which is the index
7280 ** in b.apCell[] of the cell that divides page i from page i+1.
7281 ** cntNew[k] should equal b.nCell.
7282 **
7283 ** Values computed by this block:
7284 **
7285 ** k: The total number of sibling pages
7286 ** szNew[i]: Spaced used on the i-th sibling page.
7287 ** cntNew[i]: Index in b.apCell[] and b.szCell[] for the first cell to
7288 ** the right of the i-th sibling page.
7289 ** usableSpace: Number of bytes of space available on each sibling.
7290 **
7291 */
7292 usableSpace = pBt->usableSize - 12 + leafCorrection;
7293 for(i=0; i<nOld; i++){
7294 MemPage *p = apOld[i];
7295 szNew[i] = usableSpace - p->nFree;
7296 for(j=0; j<p->nOverflow; j++){
7297 szNew[i] += 2 + p->xCellSize(p, p->apOvfl[j]);
7298 }
7299 cntNew[i] = cntOld[i];
7300 }
7301 k = nOld;
7302 for(i=0; i<k; i++){
7303 int sz;
7304 while( szNew[i]>usableSpace ){
7305 if( i+1>=k ){
7306 k = i+2;
7307 if( k>NB+2 ){ rc = SQLITE_CORRUPT_BKPT; goto balance_cleanup; }
7308 szNew[k-1] = 0;
7309 cntNew[k-1] = b.nCell;
7310 }
7311 sz = 2 + cachedCellSize(&b, cntNew[i]-1);
7312 szNew[i] -= sz;
7313 if( !leafData ){
7314 if( cntNew[i]<b.nCell ){
7315 sz = 2 + cachedCellSize(&b, cntNew[i]);
7316 }else{
7317 sz = 0;
7318 }
7319 }
7320 szNew[i+1] += sz;
7321 cntNew[i]--;
7322 }
7323 while( cntNew[i]<b.nCell ){
7324 sz = 2 + cachedCellSize(&b, cntNew[i]);
7325 if( szNew[i]+sz>usableSpace ) break;
7326 szNew[i] += sz;
7327 cntNew[i]++;
7328 if( !leafData ){
7329 if( cntNew[i]<b.nCell ){
7330 sz = 2 + cachedCellSize(&b, cntNew[i]);
7331 }else{
7332 sz = 0;
7333 }
7334 }
7335 szNew[i+1] -= sz;
7336 }
7337 if( cntNew[i]>=b.nCell ){
7338 k = i+1;
7339 }else if( cntNew[i] <= (i>0 ? cntNew[i-1] : 0) ){
7340 rc = SQLITE_CORRUPT_BKPT;
7341 goto balance_cleanup;
7342 }
7343 }
7344
7345 /*
7346 ** The packing computed by the previous block is biased toward the siblings
7347 ** on the left side (siblings with smaller keys). The left siblings are
7348 ** always nearly full, while the right-most sibling might be nearly empty.
7349 ** The next block of code attempts to adjust the packing of siblings to
7350 ** get a better balance.
7351 **
7352 ** This adjustment is more than an optimization. The packing above might
7353 ** be so out of balance as to be illegal. For example, the right-most
7354 ** sibling might be completely empty. This adjustment is not optional.
7355 */
7356 for(i=k-1; i>0; i--){
7357 int szRight = szNew[i]; /* Size of sibling on the right */
7358 int szLeft = szNew[i-1]; /* Size of sibling on the left */
7359 int r; /* Index of right-most cell in left sibling */
7360 int d; /* Index of first cell to the left of right sibling */
7361
7362 r = cntNew[i-1] - 1;
7363 d = r + 1 - leafData;
7364 (void)cachedCellSize(&b, d);
7365 do{
7366 assert( d<nMaxCells );
7367 assert( r<nMaxCells );
7368 (void)cachedCellSize(&b, r);
7369 if( szRight!=0
7370 && (bBulk || szRight+b.szCell[d]+2 > szLeft-(b.szCell[r]+(i==k-1?0:2)))){
7371 break;
7372 }
7373 szRight += b.szCell[d] + 2;
7374 szLeft -= b.szCell[r] + 2;
7375 cntNew[i-1] = r;
7376 r--;
7377 d--;
7378 }while( r>=0 );
7379 szNew[i] = szRight;
7380 szNew[i-1] = szLeft;
7381 if( cntNew[i-1] <= (i>1 ? cntNew[i-2] : 0) ){
7382 rc = SQLITE_CORRUPT_BKPT;
7383 goto balance_cleanup;
7384 }
7385 }
7386
7387 /* Sanity check: For a non-corrupt database file one of the follwing
7388 ** must be true:
7389 ** (1) We found one or more cells (cntNew[0])>0), or
7390 ** (2) pPage is a virtual root page. A virtual root page is when
7391 ** the real root page is page 1 and we are the only child of
7392 ** that page.
7393 */
7394 assert( cntNew[0]>0 || (pParent->pgno==1 && pParent->nCell==0) || CORRUPT_DB);
7395 TRACE(("BALANCE: old: %d(nc=%d) %d(nc=%d) %d(nc=%d)\n",
7396 apOld[0]->pgno, apOld[0]->nCell,
7397 nOld>=2 ? apOld[1]->pgno : 0, nOld>=2 ? apOld[1]->nCell : 0,
7398 nOld>=3 ? apOld[2]->pgno : 0, nOld>=3 ? apOld[2]->nCell : 0
7399 ));
7400
7401 /*
7402 ** Allocate k new pages. Reuse old pages where possible.
7403 */
7404 pageFlags = apOld[0]->aData[0];
7405 for(i=0; i<k; i++){
7406 MemPage *pNew;
7407 if( i<nOld ){
7408 pNew = apNew[i] = apOld[i];
7409 apOld[i] = 0;
7410 rc = sqlite3PagerWrite(pNew->pDbPage);
7411 nNew++;
7412 if( rc ) goto balance_cleanup;
7413 }else{
7414 assert( i>0 );
7415 rc = allocateBtreePage(pBt, &pNew, &pgno, (bBulk ? 1 : pgno), 0);
7416 if( rc ) goto balance_cleanup;
7417 zeroPage(pNew, pageFlags);
7418 apNew[i] = pNew;
7419 nNew++;
7420 cntOld[i] = b.nCell;
7421
7422 /* Set the pointer-map entry for the new sibling page. */
7423 if( ISAUTOVACUUM ){
7424 ptrmapPut(pBt, pNew->pgno, PTRMAP_BTREE, pParent->pgno, &rc);
7425 if( rc!=SQLITE_OK ){
7426 goto balance_cleanup;
7427 }
7428 }
7429 }
7430 }
7431
7432 /*
7433 ** Reassign page numbers so that the new pages are in ascending order.
7434 ** This helps to keep entries in the disk file in order so that a scan
7435 ** of the table is closer to a linear scan through the file. That in turn
7436 ** helps the operating system to deliver pages from the disk more rapidly.
7437 **
7438 ** An O(n^2) insertion sort algorithm is used, but since n is never more
7439 ** than (NB+2) (a small constant), that should not be a problem.
7440 **
7441 ** When NB==3, this one optimization makes the database about 25% faster
7442 ** for large insertions and deletions.
7443 */
7444 for(i=0; i<nNew; i++){
7445 aPgOrder[i] = aPgno[i] = apNew[i]->pgno;
7446 aPgFlags[i] = apNew[i]->pDbPage->flags;
7447 for(j=0; j<i; j++){
7448 if( aPgno[j]==aPgno[i] ){
7449 /* This branch is taken if the set of sibling pages somehow contains
7450 ** duplicate entries. This can happen if the database is corrupt.
7451 ** It would be simpler to detect this as part of the loop below, but
7452 ** we do the detection here in order to avoid populating the pager
7453 ** cache with two separate objects associated with the same
7454 ** page number. */
7455 assert( CORRUPT_DB );
7456 rc = SQLITE_CORRUPT_BKPT;
7457 goto balance_cleanup;
7458 }
7459 }
7460 }
7461 for(i=0; i<nNew; i++){
7462 int iBest = 0; /* aPgno[] index of page number to use */
7463 for(j=1; j<nNew; j++){
7464 if( aPgOrder[j]<aPgOrder[iBest] ) iBest = j;
7465 }
7466 pgno = aPgOrder[iBest];
7467 aPgOrder[iBest] = 0xffffffff;
7468 if( iBest!=i ){
7469 if( iBest>i ){
7470 sqlite3PagerRekey(apNew[iBest]->pDbPage, pBt->nPage+iBest+1, 0);
7471 }
7472 sqlite3PagerRekey(apNew[i]->pDbPage, pgno, aPgFlags[iBest]);
7473 apNew[i]->pgno = pgno;
7474 }
7475 }
7476
7477 TRACE(("BALANCE: new: %d(%d nc=%d) %d(%d nc=%d) %d(%d nc=%d) "
7478 "%d(%d nc=%d) %d(%d nc=%d)\n",
7479 apNew[0]->pgno, szNew[0], cntNew[0],
7480 nNew>=2 ? apNew[1]->pgno : 0, nNew>=2 ? szNew[1] : 0,
7481 nNew>=2 ? cntNew[1] - cntNew[0] - !leafData : 0,
7482 nNew>=3 ? apNew[2]->pgno : 0, nNew>=3 ? szNew[2] : 0,
7483 nNew>=3 ? cntNew[2] - cntNew[1] - !leafData : 0,
7484 nNew>=4 ? apNew[3]->pgno : 0, nNew>=4 ? szNew[3] : 0,
7485 nNew>=4 ? cntNew[3] - cntNew[2] - !leafData : 0,
7486 nNew>=5 ? apNew[4]->pgno : 0, nNew>=5 ? szNew[4] : 0,
7487 nNew>=5 ? cntNew[4] - cntNew[3] - !leafData : 0
7488 ));
7489
7490 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
7491 put4byte(pRight, apNew[nNew-1]->pgno);
7492
7493 /* If the sibling pages are not leaves, ensure that the right-child pointer
7494 ** of the right-most new sibling page is set to the value that was
7495 ** originally in the same field of the right-most old sibling page. */
7496 if( (pageFlags & PTF_LEAF)==0 && nOld!=nNew ){
7497 MemPage *pOld = (nNew>nOld ? apNew : apOld)[nOld-1];
7498 memcpy(&apNew[nNew-1]->aData[8], &pOld->aData[8], 4);
7499 }
7500
7501 /* Make any required updates to pointer map entries associated with
7502 ** cells stored on sibling pages following the balance operation. Pointer
7503 ** map entries associated with divider cells are set by the insertCell()
7504 ** routine. The associated pointer map entries are:
7505 **
7506 ** a) if the cell contains a reference to an overflow chain, the
7507 ** entry associated with the first page in the overflow chain, and
7508 **
7509 ** b) if the sibling pages are not leaves, the child page associated
7510 ** with the cell.
7511 **
7512 ** If the sibling pages are not leaves, then the pointer map entry
7513 ** associated with the right-child of each sibling may also need to be
7514 ** updated. This happens below, after the sibling pages have been
7515 ** populated, not here.
7516 */
7517 if( ISAUTOVACUUM ){
7518 MemPage *pNew = apNew[0];
7519 u8 *aOld = pNew->aData;
7520 int cntOldNext = pNew->nCell + pNew->nOverflow;
7521 int usableSize = pBt->usableSize;
7522 int iNew = 0;
7523 int iOld = 0;
7524
7525 for(i=0; i<b.nCell; i++){
7526 u8 *pCell = b.apCell[i];
7527 if( i==cntOldNext ){
7528 MemPage *pOld = (++iOld)<nNew ? apNew[iOld] : apOld[iOld];
7529 cntOldNext += pOld->nCell + pOld->nOverflow + !leafData;
7530 aOld = pOld->aData;
7531 }
7532 if( i==cntNew[iNew] ){
7533 pNew = apNew[++iNew];
7534 if( !leafData ) continue;
7535 }
7536
7537 /* Cell pCell is destined for new sibling page pNew. Originally, it
7538 ** was either part of sibling page iOld (possibly an overflow cell),
7539 ** or else the divider cell to the left of sibling page iOld. So,
7540 ** if sibling page iOld had the same page number as pNew, and if
7541 ** pCell really was a part of sibling page iOld (not a divider or
7542 ** overflow cell), we can skip updating the pointer map entries. */
7543 if( iOld>=nNew
7544 || pNew->pgno!=aPgno[iOld]
7545 || !SQLITE_WITHIN(pCell,aOld,&aOld[usableSize])
7546 ){
7547 if( !leafCorrection ){
7548 ptrmapPut(pBt, get4byte(pCell), PTRMAP_BTREE, pNew->pgno, &rc);
7549 }
7550 if( cachedCellSize(&b,i)>pNew->minLocal ){
7551 ptrmapPutOvflPtr(pNew, pCell, &rc);
7552 }
7553 if( rc ) goto balance_cleanup;
7554 }
7555 }
7556 }
7557
7558 /* Insert new divider cells into pParent. */
7559 for(i=0; i<nNew-1; i++){
7560 u8 *pCell;
7561 u8 *pTemp;
7562 int sz;
7563 MemPage *pNew = apNew[i];
7564 j = cntNew[i];
7565
7566 assert( j<nMaxCells );
7567 assert( b.apCell[j]!=0 );
7568 pCell = b.apCell[j];
7569 sz = b.szCell[j] + leafCorrection;
7570 pTemp = &aOvflSpace[iOvflSpace];
7571 if( !pNew->leaf ){
7572 memcpy(&pNew->aData[8], pCell, 4);
7573 }else if( leafData ){
7574 /* If the tree is a leaf-data tree, and the siblings are leaves,
7575 ** then there is no divider cell in b.apCell[]. Instead, the divider
7576 ** cell consists of the integer key for the right-most cell of
7577 ** the sibling-page assembled above only.
7578 */
7579 CellInfo info;
7580 j--;
7581 pNew->xParseCell(pNew, b.apCell[j], &info);
7582 pCell = pTemp;
7583 sz = 4 + putVarint(&pCell[4], info.nKey);
7584 pTemp = 0;
7585 }else{
7586 pCell -= 4;
7587 /* Obscure case for non-leaf-data trees: If the cell at pCell was
7588 ** previously stored on a leaf node, and its reported size was 4
7589 ** bytes, then it may actually be smaller than this
7590 ** (see btreeParseCellPtr(), 4 bytes is the minimum size of
7591 ** any cell). But it is important to pass the correct size to
7592 ** insertCell(), so reparse the cell now.
7593 **
7594 ** This can only happen for b-trees used to evaluate "IN (SELECT ...)"
7595 ** and WITHOUT ROWID tables with exactly one column which is the
7596 ** primary key.
7597 */
7598 if( b.szCell[j]==4 ){
7599 assert(leafCorrection==4);
7600 sz = pParent->xCellSize(pParent, pCell);
7601 }
7602 }
7603 iOvflSpace += sz;
7604 assert( sz<=pBt->maxLocal+23 );
7605 assert( iOvflSpace <= (int)pBt->pageSize );
7606 insertCell(pParent, nxDiv+i, pCell, sz, pTemp, pNew->pgno, &rc);
7607 if( rc!=SQLITE_OK ) goto balance_cleanup;
7608 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
7609 }
7610
7611 /* Now update the actual sibling pages. The order in which they are updated
7612 ** is important, as this code needs to avoid disrupting any page from which
7613 ** cells may still to be read. In practice, this means:
7614 **
7615 ** (1) If cells are moving left (from apNew[iPg] to apNew[iPg-1])
7616 ** then it is not safe to update page apNew[iPg] until after
7617 ** the left-hand sibling apNew[iPg-1] has been updated.
7618 **
7619 ** (2) If cells are moving right (from apNew[iPg] to apNew[iPg+1])
7620 ** then it is not safe to update page apNew[iPg] until after
7621 ** the right-hand sibling apNew[iPg+1] has been updated.
7622 **
7623 ** If neither of the above apply, the page is safe to update.
7624 **
7625 ** The iPg value in the following loop starts at nNew-1 goes down
7626 ** to 0, then back up to nNew-1 again, thus making two passes over
7627 ** the pages. On the initial downward pass, only condition (1) above
7628 ** needs to be tested because (2) will always be true from the previous
7629 ** step. On the upward pass, both conditions are always true, so the
7630 ** upwards pass simply processes pages that were missed on the downward
7631 ** pass.
7632 */
7633 for(i=1-nNew; i<nNew; i++){
7634 int iPg = i<0 ? -i : i;
7635 assert( iPg>=0 && iPg<nNew );
7636 if( abDone[iPg] ) continue; /* Skip pages already processed */
7637 if( i>=0 /* On the upwards pass, or... */
7638 || cntOld[iPg-1]>=cntNew[iPg-1] /* Condition (1) is true */
7639 ){
7640 int iNew;
7641 int iOld;
7642 int nNewCell;
7643
7644 /* Verify condition (1): If cells are moving left, update iPg
7645 ** only after iPg-1 has already been updated. */
7646 assert( iPg==0 || cntOld[iPg-1]>=cntNew[iPg-1] || abDone[iPg-1] );
7647
7648 /* Verify condition (2): If cells are moving right, update iPg
7649 ** only after iPg+1 has already been updated. */
7650 assert( cntNew[iPg]>=cntOld[iPg] || abDone[iPg+1] );
7651
7652 if( iPg==0 ){
7653 iNew = iOld = 0;
7654 nNewCell = cntNew[0];
7655 }else{
7656 iOld = iPg<nOld ? (cntOld[iPg-1] + !leafData) : b.nCell;
7657 iNew = cntNew[iPg-1] + !leafData;
7658 nNewCell = cntNew[iPg] - iNew;
7659 }
7660
7661 rc = editPage(apNew[iPg], iOld, iNew, nNewCell, &b);
7662 if( rc ) goto balance_cleanup;
7663 abDone[iPg]++;
7664 apNew[iPg]->nFree = usableSpace-szNew[iPg];
7665 assert( apNew[iPg]->nOverflow==0 );
7666 assert( apNew[iPg]->nCell==nNewCell );
7667 }
7668 }
7669
7670 /* All pages have been processed exactly once */
7671 assert( memcmp(abDone, "\01\01\01\01\01", nNew)==0 );
7672
7673 assert( nOld>0 );
7674 assert( nNew>0 );
7675
7676 if( isRoot && pParent->nCell==0 && pParent->hdrOffset<=apNew[0]->nFree ){
7677 /* The root page of the b-tree now contains no cells. The only sibling
7678 ** page is the right-child of the parent. Copy the contents of the
7679 ** child page into the parent, decreasing the overall height of the
7680 ** b-tree structure by one. This is described as the "balance-shallower"
7681 ** sub-algorithm in some documentation.
7682 **
7683 ** If this is an auto-vacuum database, the call to copyNodeContent()
7684 ** sets all pointer-map entries corresponding to database image pages
7685 ** for which the pointer is stored within the content being copied.
7686 **
7687 ** It is critical that the child page be defragmented before being
7688 ** copied into the parent, because if the parent is page 1 then it will
7689 ** by smaller than the child due to the database header, and so all the
7690 ** free space needs to be up front.
7691 */
7692 assert( nNew==1 || CORRUPT_DB );
7693 rc = defragmentPage(apNew[0]);
7694 testcase( rc!=SQLITE_OK );
7695 assert( apNew[0]->nFree ==
7696 (get2byte(&apNew[0]->aData[5])-apNew[0]->cellOffset-apNew[0]->nCell*2)
7697 || rc!=SQLITE_OK
7698 );
7699 copyNodeContent(apNew[0], pParent, &rc);
7700 freePage(apNew[0], &rc);
7701 }else if( ISAUTOVACUUM && !leafCorrection ){
7702 /* Fix the pointer map entries associated with the right-child of each
7703 ** sibling page. All other pointer map entries have already been taken
7704 ** care of. */
7705 for(i=0; i<nNew; i++){
7706 u32 key = get4byte(&apNew[i]->aData[8]);
7707 ptrmapPut(pBt, key, PTRMAP_BTREE, apNew[i]->pgno, &rc);
7708 }
7709 }
7710
7711 assert( pParent->isInit );
7712 TRACE(("BALANCE: finished: old=%d new=%d cells=%d\n",
7713 nOld, nNew, b.nCell));
7714
7715 /* Free any old pages that were not reused as new pages.
7716 */
7717 for(i=nNew; i<nOld; i++){
7718 freePage(apOld[i], &rc);
7719 }
7720
7721 #if 0
7722 if( ISAUTOVACUUM && rc==SQLITE_OK && apNew[0]->isInit ){
7723 /* The ptrmapCheckPages() contains assert() statements that verify that
7724 ** all pointer map pages are set correctly. This is helpful while
7725 ** debugging. This is usually disabled because a corrupt database may
7726 ** cause an assert() statement to fail. */
7727 ptrmapCheckPages(apNew, nNew);
7728 ptrmapCheckPages(&pParent, 1);
7729 }
7730 #endif
7731
7732 /*
7733 ** Cleanup before returning.
7734 */
7735 balance_cleanup:
7736 sqlite3ScratchFree(b.apCell);
7737 for(i=0; i<nOld; i++){
7738 releasePage(apOld[i]);
7739 }
7740 for(i=0; i<nNew; i++){
7741 releasePage(apNew[i]);
7742 }
7743
7744 return rc;
7745 }
7746
7747
7748 /*
7749 ** This function is called when the root page of a b-tree structure is
7750 ** overfull (has one or more overflow pages).
7751 **
7752 ** A new child page is allocated and the contents of the current root
7753 ** page, including overflow cells, are copied into the child. The root
7754 ** page is then overwritten to make it an empty page with the right-child
7755 ** pointer pointing to the new page.
7756 **
7757 ** Before returning, all pointer-map entries corresponding to pages
7758 ** that the new child-page now contains pointers to are updated. The
7759 ** entry corresponding to the new right-child pointer of the root
7760 ** page is also updated.
7761 **
7762 ** If successful, *ppChild is set to contain a reference to the child
7763 ** page and SQLITE_OK is returned. In this case the caller is required
7764 ** to call releasePage() on *ppChild exactly once. If an error occurs,
7765 ** an error code is returned and *ppChild is set to 0.
7766 */
7767 static int balance_deeper(MemPage *pRoot, MemPage **ppChild){
7768 int rc; /* Return value from subprocedures */
7769 MemPage *pChild = 0; /* Pointer to a new child page */
7770 Pgno pgnoChild = 0; /* Page number of the new child page */
7771 BtShared *pBt = pRoot->pBt; /* The BTree */
7772
7773 assert( pRoot->nOverflow>0 );
7774 assert( sqlite3_mutex_held(pBt->mutex) );
7775
7776 /* Make pRoot, the root page of the b-tree, writable. Allocate a new
7777 ** page that will become the new right-child of pPage. Copy the contents
7778 ** of the node stored on pRoot into the new child page.
7779 */
7780 rc = sqlite3PagerWrite(pRoot->pDbPage);
7781 if( rc==SQLITE_OK ){
7782 rc = allocateBtreePage(pBt,&pChild,&pgnoChild,pRoot->pgno,0);
7783 copyNodeContent(pRoot, pChild, &rc);
7784 if( ISAUTOVACUUM ){
7785 ptrmapPut(pBt, pgnoChild, PTRMAP_BTREE, pRoot->pgno, &rc);
7786 }
7787 }
7788 if( rc ){
7789 *ppChild = 0;
7790 releasePage(pChild);
7791 return rc;
7792 }
7793 assert( sqlite3PagerIswriteable(pChild->pDbPage) );
7794 assert( sqlite3PagerIswriteable(pRoot->pDbPage) );
7795 assert( pChild->nCell==pRoot->nCell );
7796
7797 TRACE(("BALANCE: copy root %d into %d\n", pRoot->pgno, pChild->pgno));
7798
7799 /* Copy the overflow cells from pRoot to pChild */
7800 memcpy(pChild->aiOvfl, pRoot->aiOvfl,
7801 pRoot->nOverflow*sizeof(pRoot->aiOvfl[0]));
7802 memcpy(pChild->apOvfl, pRoot->apOvfl,
7803 pRoot->nOverflow*sizeof(pRoot->apOvfl[0]));
7804 pChild->nOverflow = pRoot->nOverflow;
7805
7806 /* Zero the contents of pRoot. Then install pChild as the right-child. */
7807 zeroPage(pRoot, pChild->aData[0] & ~PTF_LEAF);
7808 put4byte(&pRoot->aData[pRoot->hdrOffset+8], pgnoChild);
7809
7810 *ppChild = pChild;
7811 return SQLITE_OK;
7812 }
7813
7814 /*
7815 ** The page that pCur currently points to has just been modified in
7816 ** some way. This function figures out if this modification means the
7817 ** tree needs to be balanced, and if so calls the appropriate balancing
7818 ** routine. Balancing routines are:
7819 **
7820 ** balance_quick()
7821 ** balance_deeper()
7822 ** balance_nonroot()
7823 */
7824 static int balance(BtCursor *pCur){
7825 int rc = SQLITE_OK;
7826 const int nMin = pCur->pBt->usableSize * 2 / 3;
7827 u8 aBalanceQuickSpace[13];
7828 u8 *pFree = 0;
7829
7830 VVA_ONLY( int balance_quick_called = 0 );
7831 VVA_ONLY( int balance_deeper_called = 0 );
7832
7833 do {
7834 int iPage = pCur->iPage;
7835 MemPage *pPage = pCur->apPage[iPage];
7836
7837 if( iPage==0 ){
7838 if( pPage->nOverflow ){
7839 /* The root page of the b-tree is overfull. In this case call the
7840 ** balance_deeper() function to create a new child for the root-page
7841 ** and copy the current contents of the root-page to it. The
7842 ** next iteration of the do-loop will balance the child page.
7843 */
7844 assert( balance_deeper_called==0 );
7845 VVA_ONLY( balance_deeper_called++ );
7846 rc = balance_deeper(pPage, &pCur->apPage[1]);
7847 if( rc==SQLITE_OK ){
7848 pCur->iPage = 1;
7849 pCur->aiIdx[0] = 0;
7850 pCur->aiIdx[1] = 0;
7851 assert( pCur->apPage[1]->nOverflow );
7852 }
7853 }else{
7854 break;
7855 }
7856 }else if( pPage->nOverflow==0 && pPage->nFree<=nMin ){
7857 break;
7858 }else{
7859 MemPage * const pParent = pCur->apPage[iPage-1];
7860 int const iIdx = pCur->aiIdx[iPage-1];
7861
7862 rc = sqlite3PagerWrite(pParent->pDbPage);
7863 if( rc==SQLITE_OK ){
7864 #ifndef SQLITE_OMIT_QUICKBALANCE
7865 if( pPage->intKeyLeaf
7866 && pPage->nOverflow==1
7867 && pPage->aiOvfl[0]==pPage->nCell
7868 && pParent->pgno!=1
7869 && pParent->nCell==iIdx
7870 ){
7871 /* Call balance_quick() to create a new sibling of pPage on which
7872 ** to store the overflow cell. balance_quick() inserts a new cell
7873 ** into pParent, which may cause pParent overflow. If this
7874 ** happens, the next iteration of the do-loop will balance pParent
7875 ** use either balance_nonroot() or balance_deeper(). Until this
7876 ** happens, the overflow cell is stored in the aBalanceQuickSpace[]
7877 ** buffer.
7878 **
7879 ** The purpose of the following assert() is to check that only a
7880 ** single call to balance_quick() is made for each call to this
7881 ** function. If this were not verified, a subtle bug involving reuse
7882 ** of the aBalanceQuickSpace[] might sneak in.
7883 */
7884 assert( balance_quick_called==0 );
7885 VVA_ONLY( balance_quick_called++ );
7886 rc = balance_quick(pParent, pPage, aBalanceQuickSpace);
7887 }else
7888 #endif
7889 {
7890 /* In this case, call balance_nonroot() to redistribute cells
7891 ** between pPage and up to 2 of its sibling pages. This involves
7892 ** modifying the contents of pParent, which may cause pParent to
7893 ** become overfull or underfull. The next iteration of the do-loop
7894 ** will balance the parent page to correct this.
7895 **
7896 ** If the parent page becomes overfull, the overflow cell or cells
7897 ** are stored in the pSpace buffer allocated immediately below.
7898 ** A subsequent iteration of the do-loop will deal with this by
7899 ** calling balance_nonroot() (balance_deeper() may be called first,
7900 ** but it doesn't deal with overflow cells - just moves them to a
7901 ** different page). Once this subsequent call to balance_nonroot()
7902 ** has completed, it is safe to release the pSpace buffer used by
7903 ** the previous call, as the overflow cell data will have been
7904 ** copied either into the body of a database page or into the new
7905 ** pSpace buffer passed to the latter call to balance_nonroot().
7906 */
7907 u8 *pSpace = sqlite3PageMalloc(pCur->pBt->pageSize);
7908 rc = balance_nonroot(pParent, iIdx, pSpace, iPage==1,
7909 pCur->hints&BTREE_BULKLOAD);
7910 if( pFree ){
7911 /* If pFree is not NULL, it points to the pSpace buffer used
7912 ** by a previous call to balance_nonroot(). Its contents are
7913 ** now stored either on real database pages or within the
7914 ** new pSpace buffer, so it may be safely freed here. */
7915 sqlite3PageFree(pFree);
7916 }
7917
7918 /* The pSpace buffer will be freed after the next call to
7919 ** balance_nonroot(), or just before this function returns, whichever
7920 ** comes first. */
7921 pFree = pSpace;
7922 }
7923 }
7924
7925 pPage->nOverflow = 0;
7926
7927 /* The next iteration of the do-loop balances the parent page. */
7928 releasePage(pPage);
7929 pCur->iPage--;
7930 assert( pCur->iPage>=0 );
7931 }
7932 }while( rc==SQLITE_OK );
7933
7934 if( pFree ){
7935 sqlite3PageFree(pFree);
7936 }
7937 return rc;
7938 }
7939
7940
7941 /*
7942 ** Insert a new record into the BTree. The content of the new record
7943 ** is described by the pX object. The pCur cursor is used only to
7944 ** define what table the record should be inserted into, and is left
7945 ** pointing at a random location.
7946 **
7947 ** For a table btree (used for rowid tables), only the pX.nKey value of
7948 ** the key is used. The pX.pKey value must be NULL. The pX.nKey is the
7949 ** rowid or INTEGER PRIMARY KEY of the row. The pX.nData,pData,nZero fields
7950 ** hold the content of the row.
7951 **
7952 ** For an index btree (used for indexes and WITHOUT ROWID tables), the
7953 ** key is an arbitrary byte sequence stored in pX.pKey,nKey. The
7954 ** pX.pData,nData,nZero fields must be zero.
7955 **
7956 ** If the seekResult parameter is non-zero, then a successful call to
7957 ** MovetoUnpacked() to seek cursor pCur to (pKey,nKey) has already
7958 ** been performed. In other words, if seekResult!=0 then the cursor
7959 ** is currently pointing to a cell that will be adjacent to the cell
7960 ** to be inserted. If seekResult<0 then pCur points to a cell that is
7961 ** smaller then (pKey,nKey). If seekResult>0 then pCur points to a cell
7962 ** that is larger than (pKey,nKey).
7963 **
7964 ** If seekResult==0, that means pCur is pointing at some unknown location.
7965 ** In that case, this routine must seek the cursor to the correct insertion
7966 ** point for (pKey,nKey) before doing the insertion. For index btrees,
7967 ** if pX->nMem is non-zero, then pX->aMem contains pointers to the unpacked
7968 ** key values and pX->aMem can be used instead of pX->pKey to avoid having
7969 ** to decode the key.
7970 */
7971 SQLITE_PRIVATE int sqlite3BtreeInsert(
7972 BtCursor *pCur, /* Insert data into the table of this cursor */
7973 const BtreePayload *pX, /* Content of the row to be inserted */
7974 int flags, /* True if this is likely an append */
7975 int seekResult /* Result of prior MovetoUnpacked() call */
7976 ){
7977 int rc;
7978 int loc = seekResult; /* -1: before desired location +1: after */
7979 int szNew = 0;
7980 int idx;
7981 MemPage *pPage;
7982 Btree *p = pCur->pBtree;
7983 BtShared *pBt = p->pBt;
7984 unsigned char *oldCell;
7985 unsigned char *newCell = 0;
7986
7987 assert( (flags & (BTREE_SAVEPOSITION|BTREE_APPEND))==flags );
7988
7989 if( pCur->eState==CURSOR_FAULT ){
7990 assert( pCur->skipNext!=SQLITE_OK );
7991 return pCur->skipNext;
7992 }
7993
7994 assert( cursorOwnsBtShared(pCur) );
7995 assert( (pCur->curFlags & BTCF_WriteFlag)!=0
7996 && pBt->inTransaction==TRANS_WRITE
7997 && (pBt->btsFlags & BTS_READ_ONLY)==0 );
7998 assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
7999
8000 /* Assert that the caller has been consistent. If this cursor was opened
8001 ** expecting an index b-tree, then the caller should be inserting blob
8002 ** keys with no associated data. If the cursor was opened expecting an
8003 ** intkey table, the caller should be inserting integer keys with a
8004 ** blob of associated data. */
8005 assert( (pX->pKey==0)==(pCur->pKeyInfo==0) );
8006
8007 /* Save the positions of any other cursors open on this table.
8008 **
8009 ** In some cases, the call to btreeMoveto() below is a no-op. For
8010 ** example, when inserting data into a table with auto-generated integer
8011 ** keys, the VDBE layer invokes sqlite3BtreeLast() to figure out the
8012 ** integer key to use. It then calls this function to actually insert the
8013 ** data into the intkey B-Tree. In this case btreeMoveto() recognizes
8014 ** that the cursor is already where it needs to be and returns without
8015 ** doing any work. To avoid thwarting these optimizations, it is important
8016 ** not to clear the cursor here.
8017 */
8018 if( pCur->curFlags & BTCF_Multiple ){
8019 rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur);
8020 if( rc ) return rc;
8021 }
8022
8023 if( pCur->pKeyInfo==0 ){
8024 assert( pX->pKey==0 );
8025 /* If this is an insert into a table b-tree, invalidate any incrblob
8026 ** cursors open on the row being replaced */
8027 invalidateIncrblobCursors(p, pX->nKey, 0);
8028
8029 /* If BTREE_SAVEPOSITION is set, the cursor must already be pointing
8030 ** to a row with the same key as the new entry being inserted. */
8031 assert( (flags & BTREE_SAVEPOSITION)==0 ||
8032 ((pCur->curFlags&BTCF_ValidNKey)!=0 && pX->nKey==pCur->info.nKey) );
8033
8034 /* If the cursor is currently on the last row and we are appending a
8035 ** new row onto the end, set the "loc" to avoid an unnecessary
8036 ** btreeMoveto() call */
8037 if( (pCur->curFlags&BTCF_ValidNKey)!=0 && pX->nKey==pCur->info.nKey ){
8038 loc = 0;
8039 }else if( (pCur->curFlags&BTCF_ValidNKey)!=0 && pX->nKey>0
8040 && pCur->info.nKey==pX->nKey-1 ){
8041 loc = -1;
8042 }else if( loc==0 ){
8043 rc = sqlite3BtreeMovetoUnpacked(pCur, 0, pX->nKey, flags!=0, &loc);
8044 if( rc ) return rc;
8045 }
8046 }else if( loc==0 && (flags & BTREE_SAVEPOSITION)==0 ){
8047 if( pX->nMem ){
8048 UnpackedRecord r;
8049 r.pKeyInfo = pCur->pKeyInfo;
8050 r.aMem = pX->aMem;
8051 r.nField = pX->nMem;
8052 r.default_rc = 0;
8053 r.errCode = 0;
8054 r.r1 = 0;
8055 r.r2 = 0;
8056 r.eqSeen = 0;
8057 rc = sqlite3BtreeMovetoUnpacked(pCur, &r, 0, flags!=0, &loc);
8058 }else{
8059 rc = btreeMoveto(pCur, pX->pKey, pX->nKey, flags!=0, &loc);
8060 }
8061 if( rc ) return rc;
8062 }
8063 assert( pCur->eState==CURSOR_VALID || (pCur->eState==CURSOR_INVALID && loc) );
8064
8065 pPage = pCur->apPage[pCur->iPage];
8066 assert( pPage->intKey || pX->nKey>=0 );
8067 assert( pPage->leaf || !pPage->intKey );
8068
8069 TRACE(("INSERT: table=%d nkey=%lld ndata=%d page=%d %s\n",
8070 pCur->pgnoRoot, pX->nKey, pX->nData, pPage->pgno,
8071 loc==0 ? "overwrite" : "new entry"));
8072 assert( pPage->isInit );
8073 newCell = pBt->pTmpSpace;
8074 assert( newCell!=0 );
8075 rc = fillInCell(pPage, newCell, pX, &szNew);
8076 if( rc ) goto end_insert;
8077 assert( szNew==pPage->xCellSize(pPage, newCell) );
8078 assert( szNew <= MX_CELL_SIZE(pBt) );
8079 idx = pCur->aiIdx[pCur->iPage];
8080 if( loc==0 ){
8081 CellInfo info;
8082 assert( idx<pPage->nCell );
8083 rc = sqlite3PagerWrite(pPage->pDbPage);
8084 if( rc ){
8085 goto end_insert;
8086 }
8087 oldCell = findCell(pPage, idx);
8088 if( !pPage->leaf ){
8089 memcpy(newCell, oldCell, 4);
8090 }
8091 rc = clearCell(pPage, oldCell, &info);
8092 if( info.nSize==szNew && info.nLocal==info.nPayload ){
8093 /* Overwrite the old cell with the new if they are the same size.
8094 ** We could also try to do this if the old cell is smaller, then add
8095 ** the leftover space to the free list. But experiments show that
8096 ** doing that is no faster then skipping this optimization and just
8097 ** calling dropCell() and insertCell(). */
8098 assert( rc==SQLITE_OK ); /* clearCell never fails when nLocal==nPayload */
8099 if( oldCell+szNew > pPage->aDataEnd ) return SQLITE_CORRUPT_BKPT;
8100 memcpy(oldCell, newCell, szNew);
8101 return SQLITE_OK;
8102 }
8103 dropCell(pPage, idx, info.nSize, &rc);
8104 if( rc ) goto end_insert;
8105 }else if( loc<0 && pPage->nCell>0 ){
8106 assert( pPage->leaf );
8107 idx = ++pCur->aiIdx[pCur->iPage];
8108 }else{
8109 assert( pPage->leaf );
8110 }
8111 insertCell(pPage, idx, newCell, szNew, 0, 0, &rc);
8112 assert( pPage->nOverflow==0 || rc==SQLITE_OK );
8113 assert( rc!=SQLITE_OK || pPage->nCell>0 || pPage->nOverflow>0 );
8114
8115 /* If no error has occurred and pPage has an overflow cell, call balance()
8116 ** to redistribute the cells within the tree. Since balance() may move
8117 ** the cursor, zero the BtCursor.info.nSize and BTCF_ValidNKey
8118 ** variables.
8119 **
8120 ** Previous versions of SQLite called moveToRoot() to move the cursor
8121 ** back to the root page as balance() used to invalidate the contents
8122 ** of BtCursor.apPage[] and BtCursor.aiIdx[]. Instead of doing that,
8123 ** set the cursor state to "invalid". This makes common insert operations
8124 ** slightly faster.
8125 **
8126 ** There is a subtle but important optimization here too. When inserting
8127 ** multiple records into an intkey b-tree using a single cursor (as can
8128 ** happen while processing an "INSERT INTO ... SELECT" statement), it
8129 ** is advantageous to leave the cursor pointing to the last entry in
8130 ** the b-tree if possible. If the cursor is left pointing to the last
8131 ** entry in the table, and the next row inserted has an integer key
8132 ** larger than the largest existing key, it is possible to insert the
8133 ** row without seeking the cursor. This can be a big performance boost.
8134 */
8135 pCur->info.nSize = 0;
8136 if( pPage->nOverflow ){
8137 assert( rc==SQLITE_OK );
8138 pCur->curFlags &= ~(BTCF_ValidNKey);
8139 rc = balance(pCur);
8140
8141 /* Must make sure nOverflow is reset to zero even if the balance()
8142 ** fails. Internal data structure corruption will result otherwise.
8143 ** Also, set the cursor state to invalid. This stops saveCursorPosition()
8144 ** from trying to save the current position of the cursor. */
8145 pCur->apPage[pCur->iPage]->nOverflow = 0;
8146 pCur->eState = CURSOR_INVALID;
8147 if( (flags & BTREE_SAVEPOSITION) && rc==SQLITE_OK ){
8148 rc = moveToRoot(pCur);
8149 if( pCur->pKeyInfo ){
8150 assert( pCur->pKey==0 );
8151 pCur->pKey = sqlite3Malloc( pX->nKey );
8152 if( pCur->pKey==0 ){
8153 rc = SQLITE_NOMEM;
8154 }else{
8155 memcpy(pCur->pKey, pX->pKey, pX->nKey);
8156 }
8157 }
8158 pCur->eState = CURSOR_REQUIRESEEK;
8159 pCur->nKey = pX->nKey;
8160 }
8161 }
8162 assert( pCur->apPage[pCur->iPage]->nOverflow==0 );
8163
8164 end_insert:
8165 return rc;
8166 }
8167
8168 /*
8169 ** Delete the entry that the cursor is pointing to.
8170 **
8171 ** If the BTREE_SAVEPOSITION bit of the flags parameter is zero, then
8172 ** the cursor is left pointing at an arbitrary location after the delete.
8173 ** But if that bit is set, then the cursor is left in a state such that
8174 ** the next call to BtreeNext() or BtreePrev() moves it to the same row
8175 ** as it would have been on if the call to BtreeDelete() had been omitted.
8176 **
8177 ** The BTREE_AUXDELETE bit of flags indicates that is one of several deletes
8178 ** associated with a single table entry and its indexes. Only one of those
8179 ** deletes is considered the "primary" delete. The primary delete occurs
8180 ** on a cursor that is not a BTREE_FORDELETE cursor. All but one delete
8181 ** operation on non-FORDELETE cursors is tagged with the AUXDELETE flag.
8182 ** The BTREE_AUXDELETE bit is a hint that is not used by this implementation,
8183 ** but which might be used by alternative storage engines.
8184 */
8185 SQLITE_PRIVATE int sqlite3BtreeDelete(BtCursor *pCur, u8 flags){
8186 Btree *p = pCur->pBtree;
8187 BtShared *pBt = p->pBt;
8188 int rc; /* Return code */
8189 MemPage *pPage; /* Page to delete cell from */
8190 unsigned char *pCell; /* Pointer to cell to delete */
8191 int iCellIdx; /* Index of cell to delete */
8192 int iCellDepth; /* Depth of node containing pCell */
8193 CellInfo info; /* Size of the cell being deleted */
8194 int bSkipnext = 0; /* Leaf cursor in SKIPNEXT state */
8195 u8 bPreserve = flags & BTREE_SAVEPOSITION; /* Keep cursor valid */
8196
8197 assert( cursorOwnsBtShared(pCur) );
8198 assert( pBt->inTransaction==TRANS_WRITE );
8199 assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
8200 assert( pCur->curFlags & BTCF_WriteFlag );
8201 assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
8202 assert( !hasReadConflicts(p, pCur->pgnoRoot) );
8203 assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell );
8204 assert( pCur->eState==CURSOR_VALID );
8205 assert( (flags & ~(BTREE_SAVEPOSITION | BTREE_AUXDELETE))==0 );
8206
8207 iCellDepth = pCur->iPage;
8208 iCellIdx = pCur->aiIdx[iCellDepth];
8209 pPage = pCur->apPage[iCellDepth];
8210 pCell = findCell(pPage, iCellIdx);
8211
8212 /* If the bPreserve flag is set to true, then the cursor position must
8213 ** be preserved following this delete operation. If the current delete
8214 ** will cause a b-tree rebalance, then this is done by saving the cursor
8215 ** key and leaving the cursor in CURSOR_REQUIRESEEK state before
8216 ** returning.
8217 **
8218 ** Or, if the current delete will not cause a rebalance, then the cursor
8219 ** will be left in CURSOR_SKIPNEXT state pointing to the entry immediately
8220 ** before or after the deleted entry. In this case set bSkipnext to true. */
8221 if( bPreserve ){
8222 if( !pPage->leaf
8223 || (pPage->nFree+cellSizePtr(pPage,pCell)+2)>(int)(pBt->usableSize*2/3)
8224 ){
8225 /* A b-tree rebalance will be required after deleting this entry.
8226 ** Save the cursor key. */
8227 rc = saveCursorKey(pCur);
8228 if( rc ) return rc;
8229 }else{
8230 bSkipnext = 1;
8231 }
8232 }
8233
8234 /* If the page containing the entry to delete is not a leaf page, move
8235 ** the cursor to the largest entry in the tree that is smaller than
8236 ** the entry being deleted. This cell will replace the cell being deleted
8237 ** from the internal node. The 'previous' entry is used for this instead
8238 ** of the 'next' entry, as the previous entry is always a part of the
8239 ** sub-tree headed by the child page of the cell being deleted. This makes
8240 ** balancing the tree following the delete operation easier. */
8241 if( !pPage->leaf ){
8242 int notUsed = 0;
8243 rc = sqlite3BtreePrevious(pCur, &notUsed);
8244 if( rc ) return rc;
8245 }
8246
8247 /* Save the positions of any other cursors open on this table before
8248 ** making any modifications. */
8249 if( pCur->curFlags & BTCF_Multiple ){
8250 rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur);
8251 if( rc ) return rc;
8252 }
8253
8254 /* If this is a delete operation to remove a row from a table b-tree,
8255 ** invalidate any incrblob cursors open on the row being deleted. */
8256 if( pCur->pKeyInfo==0 ){
8257 invalidateIncrblobCursors(p, pCur->info.nKey, 0);
8258 }
8259
8260 /* Make the page containing the entry to be deleted writable. Then free any
8261 ** overflow pages associated with the entry and finally remove the cell
8262 ** itself from within the page. */
8263 rc = sqlite3PagerWrite(pPage->pDbPage);
8264 if( rc ) return rc;
8265 rc = clearCell(pPage, pCell, &info);
8266 dropCell(pPage, iCellIdx, info.nSize, &rc);
8267 if( rc ) return rc;
8268
8269 /* If the cell deleted was not located on a leaf page, then the cursor
8270 ** is currently pointing to the largest entry in the sub-tree headed
8271 ** by the child-page of the cell that was just deleted from an internal
8272 ** node. The cell from the leaf node needs to be moved to the internal
8273 ** node to replace the deleted cell. */
8274 if( !pPage->leaf ){
8275 MemPage *pLeaf = pCur->apPage[pCur->iPage];
8276 int nCell;
8277 Pgno n = pCur->apPage[iCellDepth+1]->pgno;
8278 unsigned char *pTmp;
8279
8280 pCell = findCell(pLeaf, pLeaf->nCell-1);
8281 if( pCell<&pLeaf->aData[4] ) return SQLITE_CORRUPT_BKPT;
8282 nCell = pLeaf->xCellSize(pLeaf, pCell);
8283 assert( MX_CELL_SIZE(pBt) >= nCell );
8284 pTmp = pBt->pTmpSpace;
8285 assert( pTmp!=0 );
8286 rc = sqlite3PagerWrite(pLeaf->pDbPage);
8287 if( rc==SQLITE_OK ){
8288 insertCell(pPage, iCellIdx, pCell-4, nCell+4, pTmp, n, &rc);
8289 }
8290 dropCell(pLeaf, pLeaf->nCell-1, nCell, &rc);
8291 if( rc ) return rc;
8292 }
8293
8294 /* Balance the tree. If the entry deleted was located on a leaf page,
8295 ** then the cursor still points to that page. In this case the first
8296 ** call to balance() repairs the tree, and the if(...) condition is
8297 ** never true.
8298 **
8299 ** Otherwise, if the entry deleted was on an internal node page, then
8300 ** pCur is pointing to the leaf page from which a cell was removed to
8301 ** replace the cell deleted from the internal node. This is slightly
8302 ** tricky as the leaf node may be underfull, and the internal node may
8303 ** be either under or overfull. In this case run the balancing algorithm
8304 ** on the leaf node first. If the balance proceeds far enough up the
8305 ** tree that we can be sure that any problem in the internal node has
8306 ** been corrected, so be it. Otherwise, after balancing the leaf node,
8307 ** walk the cursor up the tree to the internal node and balance it as
8308 ** well. */
8309 rc = balance(pCur);
8310 if( rc==SQLITE_OK && pCur->iPage>iCellDepth ){
8311 while( pCur->iPage>iCellDepth ){
8312 releasePage(pCur->apPage[pCur->iPage--]);
8313 }
8314 rc = balance(pCur);
8315 }
8316
8317 if( rc==SQLITE_OK ){
8318 if( bSkipnext ){
8319 assert( bPreserve && (pCur->iPage==iCellDepth || CORRUPT_DB) );
8320 assert( pPage==pCur->apPage[pCur->iPage] || CORRUPT_DB );
8321 assert( (pPage->nCell>0 || CORRUPT_DB) && iCellIdx<=pPage->nCell );
8322 pCur->eState = CURSOR_SKIPNEXT;
8323 if( iCellIdx>=pPage->nCell ){
8324 pCur->skipNext = -1;
8325 pCur->aiIdx[iCellDepth] = pPage->nCell-1;
8326 }else{
8327 pCur->skipNext = 1;
8328 }
8329 }else{
8330 rc = moveToRoot(pCur);
8331 if( bPreserve ){
8332 pCur->eState = CURSOR_REQUIRESEEK;
8333 }
8334 }
8335 }
8336 return rc;
8337 }
8338
8339 /*
8340 ** Create a new BTree table. Write into *piTable the page
8341 ** number for the root page of the new table.
8342 **
8343 ** The type of type is determined by the flags parameter. Only the
8344 ** following values of flags are currently in use. Other values for
8345 ** flags might not work:
8346 **
8347 ** BTREE_INTKEY|BTREE_LEAFDATA Used for SQL tables with rowid keys
8348 ** BTREE_ZERODATA Used for SQL indices
8349 */
8350 static int btreeCreateTable(Btree *p, int *piTable, int createTabFlags){
8351 BtShared *pBt = p->pBt;
8352 MemPage *pRoot;
8353 Pgno pgnoRoot;
8354 int rc;
8355 int ptfFlags; /* Page-type flage for the root page of new table */
8356
8357 assert( sqlite3BtreeHoldsMutex(p) );
8358 assert( pBt->inTransaction==TRANS_WRITE );
8359 assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
8360
8361 #ifdef SQLITE_OMIT_AUTOVACUUM
8362 rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0);
8363 if( rc ){
8364 return rc;
8365 }
8366 #else
8367 if( pBt->autoVacuum ){
8368 Pgno pgnoMove; /* Move a page here to make room for the root-page */
8369 MemPage *pPageMove; /* The page to move to. */
8370
8371 /* Creating a new table may probably require moving an existing database
8372 ** to make room for the new tables root page. In case this page turns
8373 ** out to be an overflow page, delete all overflow page-map caches
8374 ** held by open cursors.
8375 */
8376 invalidateAllOverflowCache(pBt);
8377
8378 /* Read the value of meta[3] from the database to determine where the
8379 ** root page of the new table should go. meta[3] is the largest root-page
8380 ** created so far, so the new root-page is (meta[3]+1).
8381 */
8382 sqlite3BtreeGetMeta(p, BTREE_LARGEST_ROOT_PAGE, &pgnoRoot);
8383 pgnoRoot++;
8384
8385 /* The new root-page may not be allocated on a pointer-map page, or the
8386 ** PENDING_BYTE page.
8387 */
8388 while( pgnoRoot==PTRMAP_PAGENO(pBt, pgnoRoot) ||
8389 pgnoRoot==PENDING_BYTE_PAGE(pBt) ){
8390 pgnoRoot++;
8391 }
8392 assert( pgnoRoot>=3 || CORRUPT_DB );
8393 testcase( pgnoRoot<3 );
8394
8395 /* Allocate a page. The page that currently resides at pgnoRoot will
8396 ** be moved to the allocated page (unless the allocated page happens
8397 ** to reside at pgnoRoot).
8398 */
8399 rc = allocateBtreePage(pBt, &pPageMove, &pgnoMove, pgnoRoot, BTALLOC_EXACT);
8400 if( rc!=SQLITE_OK ){
8401 return rc;
8402 }
8403
8404 if( pgnoMove!=pgnoRoot ){
8405 /* pgnoRoot is the page that will be used for the root-page of
8406 ** the new table (assuming an error did not occur). But we were
8407 ** allocated pgnoMove. If required (i.e. if it was not allocated
8408 ** by extending the file), the current page at position pgnoMove
8409 ** is already journaled.
8410 */
8411 u8 eType = 0;
8412 Pgno iPtrPage = 0;
8413
8414 /* Save the positions of any open cursors. This is required in
8415 ** case they are holding a reference to an xFetch reference
8416 ** corresponding to page pgnoRoot. */
8417 rc = saveAllCursors(pBt, 0, 0);
8418 releasePage(pPageMove);
8419 if( rc!=SQLITE_OK ){
8420 return rc;
8421 }
8422
8423 /* Move the page currently at pgnoRoot to pgnoMove. */
8424 rc = btreeGetPage(pBt, pgnoRoot, &pRoot, 0);
8425 if( rc!=SQLITE_OK ){
8426 return rc;
8427 }
8428 rc = ptrmapGet(pBt, pgnoRoot, &eType, &iPtrPage);
8429 if( eType==PTRMAP_ROOTPAGE || eType==PTRMAP_FREEPAGE ){
8430 rc = SQLITE_CORRUPT_BKPT;
8431 }
8432 if( rc!=SQLITE_OK ){
8433 releasePage(pRoot);
8434 return rc;
8435 }
8436 assert( eType!=PTRMAP_ROOTPAGE );
8437 assert( eType!=PTRMAP_FREEPAGE );
8438 rc = relocatePage(pBt, pRoot, eType, iPtrPage, pgnoMove, 0);
8439 releasePage(pRoot);
8440
8441 /* Obtain the page at pgnoRoot */
8442 if( rc!=SQLITE_OK ){
8443 return rc;
8444 }
8445 rc = btreeGetPage(pBt, pgnoRoot, &pRoot, 0);
8446 if( rc!=SQLITE_OK ){
8447 return rc;
8448 }
8449 rc = sqlite3PagerWrite(pRoot->pDbPage);
8450 if( rc!=SQLITE_OK ){
8451 releasePage(pRoot);
8452 return rc;
8453 }
8454 }else{
8455 pRoot = pPageMove;
8456 }
8457
8458 /* Update the pointer-map and meta-data with the new root-page number. */
8459 ptrmapPut(pBt, pgnoRoot, PTRMAP_ROOTPAGE, 0, &rc);
8460 if( rc ){
8461 releasePage(pRoot);
8462 return rc;
8463 }
8464
8465 /* When the new root page was allocated, page 1 was made writable in
8466 ** order either to increase the database filesize, or to decrement the
8467 ** freelist count. Hence, the sqlite3BtreeUpdateMeta() call cannot fail.
8468 */
8469 assert( sqlite3PagerIswriteable(pBt->pPage1->pDbPage) );
8470 rc = sqlite3BtreeUpdateMeta(p, 4, pgnoRoot);
8471 if( NEVER(rc) ){
8472 releasePage(pRoot);
8473 return rc;
8474 }
8475
8476 }else{
8477 rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0);
8478 if( rc ) return rc;
8479 }
8480 #endif
8481 assert( sqlite3PagerIswriteable(pRoot->pDbPage) );
8482 if( createTabFlags & BTREE_INTKEY ){
8483 ptfFlags = PTF_INTKEY | PTF_LEAFDATA | PTF_LEAF;
8484 }else{
8485 ptfFlags = PTF_ZERODATA | PTF_LEAF;
8486 }
8487 zeroPage(pRoot, ptfFlags);
8488 sqlite3PagerUnref(pRoot->pDbPage);
8489 assert( (pBt->openFlags & BTREE_SINGLE)==0 || pgnoRoot==2 );
8490 *piTable = (int)pgnoRoot;
8491 return SQLITE_OK;
8492 }
8493 SQLITE_PRIVATE int sqlite3BtreeCreateTable(Btree *p, int *piTable, int flags){
8494 int rc;
8495 sqlite3BtreeEnter(p);
8496 rc = btreeCreateTable(p, piTable, flags);
8497 sqlite3BtreeLeave(p);
8498 return rc;
8499 }
8500
8501 /*
8502 ** Erase the given database page and all its children. Return
8503 ** the page to the freelist.
8504 */
8505 static int clearDatabasePage(
8506 BtShared *pBt, /* The BTree that contains the table */
8507 Pgno pgno, /* Page number to clear */
8508 int freePageFlag, /* Deallocate page if true */
8509 int *pnChange /* Add number of Cells freed to this counter */
8510 ){
8511 MemPage *pPage;
8512 int rc;
8513 unsigned char *pCell;
8514 int i;
8515 int hdr;
8516 CellInfo info;
8517
8518 assert( sqlite3_mutex_held(pBt->mutex) );
8519 if( pgno>btreePagecount(pBt) ){
8520 return SQLITE_CORRUPT_BKPT;
8521 }
8522 rc = getAndInitPage(pBt, pgno, &pPage, 0, 0);
8523 if( rc ) return rc;
8524 if( pPage->bBusy ){
8525 rc = SQLITE_CORRUPT_BKPT;
8526 goto cleardatabasepage_out;
8527 }
8528 pPage->bBusy = 1;
8529 hdr = pPage->hdrOffset;
8530 for(i=0; i<pPage->nCell; i++){
8531 pCell = findCell(pPage, i);
8532 if( !pPage->leaf ){
8533 rc = clearDatabasePage(pBt, get4byte(pCell), 1, pnChange);
8534 if( rc ) goto cleardatabasepage_out;
8535 }
8536 rc = clearCell(pPage, pCell, &info);
8537 if( rc ) goto cleardatabasepage_out;
8538 }
8539 if( !pPage->leaf ){
8540 rc = clearDatabasePage(pBt, get4byte(&pPage->aData[hdr+8]), 1, pnChange);
8541 if( rc ) goto cleardatabasepage_out;
8542 }else if( pnChange ){
8543 assert( pPage->intKey || CORRUPT_DB );
8544 testcase( !pPage->intKey );
8545 *pnChange += pPage->nCell;
8546 }
8547 if( freePageFlag ){
8548 freePage(pPage, &rc);
8549 }else if( (rc = sqlite3PagerWrite(pPage->pDbPage))==0 ){
8550 zeroPage(pPage, pPage->aData[hdr] | PTF_LEAF);
8551 }
8552
8553 cleardatabasepage_out:
8554 pPage->bBusy = 0;
8555 releasePage(pPage);
8556 return rc;
8557 }
8558
8559 /*
8560 ** Delete all information from a single table in the database. iTable is
8561 ** the page number of the root of the table. After this routine returns,
8562 ** the root page is empty, but still exists.
8563 **
8564 ** This routine will fail with SQLITE_LOCKED if there are any open
8565 ** read cursors on the table. Open write cursors are moved to the
8566 ** root of the table.
8567 **
8568 ** If pnChange is not NULL, then table iTable must be an intkey table. The
8569 ** integer value pointed to by pnChange is incremented by the number of
8570 ** entries in the table.
8571 */
8572 SQLITE_PRIVATE int sqlite3BtreeClearTable(Btree *p, int iTable, int *pnChange){
8573 int rc;
8574 BtShared *pBt = p->pBt;
8575 sqlite3BtreeEnter(p);
8576 assert( p->inTrans==TRANS_WRITE );
8577
8578 rc = saveAllCursors(pBt, (Pgno)iTable, 0);
8579
8580 if( SQLITE_OK==rc ){
8581 /* Invalidate all incrblob cursors open on table iTable (assuming iTable
8582 ** is the root of a table b-tree - if it is not, the following call is
8583 ** a no-op). */
8584 invalidateIncrblobCursors(p, 0, 1);
8585 rc = clearDatabasePage(pBt, (Pgno)iTable, 0, pnChange);
8586 }
8587 sqlite3BtreeLeave(p);
8588 return rc;
8589 }
8590
8591 /*
8592 ** Delete all information from the single table that pCur is open on.
8593 **
8594 ** This routine only work for pCur on an ephemeral table.
8595 */
8596 SQLITE_PRIVATE int sqlite3BtreeClearTableOfCursor(BtCursor *pCur){
8597 return sqlite3BtreeClearTable(pCur->pBtree, pCur->pgnoRoot, 0);
8598 }
8599
8600 /*
8601 ** Erase all information in a table and add the root of the table to
8602 ** the freelist. Except, the root of the principle table (the one on
8603 ** page 1) is never added to the freelist.
8604 **
8605 ** This routine will fail with SQLITE_LOCKED if there are any open
8606 ** cursors on the table.
8607 **
8608 ** If AUTOVACUUM is enabled and the page at iTable is not the last
8609 ** root page in the database file, then the last root page
8610 ** in the database file is moved into the slot formerly occupied by
8611 ** iTable and that last slot formerly occupied by the last root page
8612 ** is added to the freelist instead of iTable. In this say, all
8613 ** root pages are kept at the beginning of the database file, which
8614 ** is necessary for AUTOVACUUM to work right. *piMoved is set to the
8615 ** page number that used to be the last root page in the file before
8616 ** the move. If no page gets moved, *piMoved is set to 0.
8617 ** The last root page is recorded in meta[3] and the value of
8618 ** meta[3] is updated by this procedure.
8619 */
8620 static int btreeDropTable(Btree *p, Pgno iTable, int *piMoved){
8621 int rc;
8622 MemPage *pPage = 0;
8623 BtShared *pBt = p->pBt;
8624
8625 assert( sqlite3BtreeHoldsMutex(p) );
8626 assert( p->inTrans==TRANS_WRITE );
8627 assert( iTable>=2 );
8628
8629 rc = btreeGetPage(pBt, (Pgno)iTable, &pPage, 0);
8630 if( rc ) return rc;
8631 rc = sqlite3BtreeClearTable(p, iTable, 0);
8632 if( rc ){
8633 releasePage(pPage);
8634 return rc;
8635 }
8636
8637 *piMoved = 0;
8638
8639 #ifdef SQLITE_OMIT_AUTOVACUUM
8640 freePage(pPage, &rc);
8641 releasePage(pPage);
8642 #else
8643 if( pBt->autoVacuum ){
8644 Pgno maxRootPgno;
8645 sqlite3BtreeGetMeta(p, BTREE_LARGEST_ROOT_PAGE, &maxRootPgno);
8646
8647 if( iTable==maxRootPgno ){
8648 /* If the table being dropped is the table with the largest root-page
8649 ** number in the database, put the root page on the free list.
8650 */
8651 freePage(pPage, &rc);
8652 releasePage(pPage);
8653 if( rc!=SQLITE_OK ){
8654 return rc;
8655 }
8656 }else{
8657 /* The table being dropped does not have the largest root-page
8658 ** number in the database. So move the page that does into the
8659 ** gap left by the deleted root-page.
8660 */
8661 MemPage *pMove;
8662 releasePage(pPage);
8663 rc = btreeGetPage(pBt, maxRootPgno, &pMove, 0);
8664 if( rc!=SQLITE_OK ){
8665 return rc;
8666 }
8667 rc = relocatePage(pBt, pMove, PTRMAP_ROOTPAGE, 0, iTable, 0);
8668 releasePage(pMove);
8669 if( rc!=SQLITE_OK ){
8670 return rc;
8671 }
8672 pMove = 0;
8673 rc = btreeGetPage(pBt, maxRootPgno, &pMove, 0);
8674 freePage(pMove, &rc);
8675 releasePage(pMove);
8676 if( rc!=SQLITE_OK ){
8677 return rc;
8678 }
8679 *piMoved = maxRootPgno;
8680 }
8681
8682 /* Set the new 'max-root-page' value in the database header. This
8683 ** is the old value less one, less one more if that happens to
8684 ** be a root-page number, less one again if that is the
8685 ** PENDING_BYTE_PAGE.
8686 */
8687 maxRootPgno--;
8688 while( maxRootPgno==PENDING_BYTE_PAGE(pBt)
8689 || PTRMAP_ISPAGE(pBt, maxRootPgno) ){
8690 maxRootPgno--;
8691 }
8692 assert( maxRootPgno!=PENDING_BYTE_PAGE(pBt) );
8693
8694 rc = sqlite3BtreeUpdateMeta(p, 4, maxRootPgno);
8695 }else{
8696 freePage(pPage, &rc);
8697 releasePage(pPage);
8698 }
8699 #endif
8700 return rc;
8701 }
8702 SQLITE_PRIVATE int sqlite3BtreeDropTable(Btree *p, int iTable, int *piMoved){
8703 int rc;
8704 sqlite3BtreeEnter(p);
8705 rc = btreeDropTable(p, iTable, piMoved);
8706 sqlite3BtreeLeave(p);
8707 return rc;
8708 }
8709
8710
8711 /*
8712 ** This function may only be called if the b-tree connection already
8713 ** has a read or write transaction open on the database.
8714 **
8715 ** Read the meta-information out of a database file. Meta[0]
8716 ** is the number of free pages currently in the database. Meta[1]
8717 ** through meta[15] are available for use by higher layers. Meta[0]
8718 ** is read-only, the others are read/write.
8719 **
8720 ** The schema layer numbers meta values differently. At the schema
8721 ** layer (and the SetCookie and ReadCookie opcodes) the number of
8722 ** free pages is not visible. So Cookie[0] is the same as Meta[1].
8723 **
8724 ** This routine treats Meta[BTREE_DATA_VERSION] as a special case. Instead
8725 ** of reading the value out of the header, it instead loads the "DataVersion"
8726 ** from the pager. The BTREE_DATA_VERSION value is not actually stored in the
8727 ** database file. It is a number computed by the pager. But its access
8728 ** pattern is the same as header meta values, and so it is convenient to
8729 ** read it from this routine.
8730 */
8731 SQLITE_PRIVATE void sqlite3BtreeGetMeta(Btree *p, int idx, u32 *pMeta){
8732 BtShared *pBt = p->pBt;
8733
8734 sqlite3BtreeEnter(p);
8735 assert( p->inTrans>TRANS_NONE );
8736 assert( SQLITE_OK==querySharedCacheTableLock(p, MASTER_ROOT, READ_LOCK) );
8737 assert( pBt->pPage1 );
8738 assert( idx>=0 && idx<=15 );
8739
8740 if( idx==BTREE_DATA_VERSION ){
8741 *pMeta = sqlite3PagerDataVersion(pBt->pPager) + p->iDataVersion;
8742 }else{
8743 *pMeta = get4byte(&pBt->pPage1->aData[36 + idx*4]);
8744 }
8745
8746 /* If auto-vacuum is disabled in this build and this is an auto-vacuum
8747 ** database, mark the database as read-only. */
8748 #ifdef SQLITE_OMIT_AUTOVACUUM
8749 if( idx==BTREE_LARGEST_ROOT_PAGE && *pMeta>0 ){
8750 pBt->btsFlags |= BTS_READ_ONLY;
8751 }
8752 #endif
8753
8754 sqlite3BtreeLeave(p);
8755 }
8756
8757 /*
8758 ** Write meta-information back into the database. Meta[0] is
8759 ** read-only and may not be written.
8760 */
8761 SQLITE_PRIVATE int sqlite3BtreeUpdateMeta(Btree *p, int idx, u32 iMeta){
8762 BtShared *pBt = p->pBt;
8763 unsigned char *pP1;
8764 int rc;
8765 assert( idx>=1 && idx<=15 );
8766 sqlite3BtreeEnter(p);
8767 assert( p->inTrans==TRANS_WRITE );
8768 assert( pBt->pPage1!=0 );
8769 pP1 = pBt->pPage1->aData;
8770 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
8771 if( rc==SQLITE_OK ){
8772 put4byte(&pP1[36 + idx*4], iMeta);
8773 #ifndef SQLITE_OMIT_AUTOVACUUM
8774 if( idx==BTREE_INCR_VACUUM ){
8775 assert( pBt->autoVacuum || iMeta==0 );
8776 assert( iMeta==0 || iMeta==1 );
8777 pBt->incrVacuum = (u8)iMeta;
8778 }
8779 #endif
8780 }
8781 sqlite3BtreeLeave(p);
8782 return rc;
8783 }
8784
8785 #ifndef SQLITE_OMIT_BTREECOUNT
8786 /*
8787 ** The first argument, pCur, is a cursor opened on some b-tree. Count the
8788 ** number of entries in the b-tree and write the result to *pnEntry.
8789 **
8790 ** SQLITE_OK is returned if the operation is successfully executed.
8791 ** Otherwise, if an error is encountered (i.e. an IO error or database
8792 ** corruption) an SQLite error code is returned.
8793 */
8794 SQLITE_PRIVATE int sqlite3BtreeCount(BtCursor *pCur, i64 *pnEntry){
8795 i64 nEntry = 0; /* Value to return in *pnEntry */
8796 int rc; /* Return code */
8797
8798 if( pCur->pgnoRoot==0 ){
8799 *pnEntry = 0;
8800 return SQLITE_OK;
8801 }
8802 rc = moveToRoot(pCur);
8803
8804 /* Unless an error occurs, the following loop runs one iteration for each
8805 ** page in the B-Tree structure (not including overflow pages).
8806 */
8807 while( rc==SQLITE_OK ){
8808 int iIdx; /* Index of child node in parent */
8809 MemPage *pPage; /* Current page of the b-tree */
8810
8811 /* If this is a leaf page or the tree is not an int-key tree, then
8812 ** this page contains countable entries. Increment the entry counter
8813 ** accordingly.
8814 */
8815 pPage = pCur->apPage[pCur->iPage];
8816 if( pPage->leaf || !pPage->intKey ){
8817 nEntry += pPage->nCell;
8818 }
8819
8820 /* pPage is a leaf node. This loop navigates the cursor so that it
8821 ** points to the first interior cell that it points to the parent of
8822 ** the next page in the tree that has not yet been visited. The
8823 ** pCur->aiIdx[pCur->iPage] value is set to the index of the parent cell
8824 ** of the page, or to the number of cells in the page if the next page
8825 ** to visit is the right-child of its parent.
8826 **
8827 ** If all pages in the tree have been visited, return SQLITE_OK to the
8828 ** caller.
8829 */
8830 if( pPage->leaf ){
8831 do {
8832 if( pCur->iPage==0 ){
8833 /* All pages of the b-tree have been visited. Return successfully. */
8834 *pnEntry = nEntry;
8835 return moveToRoot(pCur);
8836 }
8837 moveToParent(pCur);
8838 }while ( pCur->aiIdx[pCur->iPage]>=pCur->apPage[pCur->iPage]->nCell );
8839
8840 pCur->aiIdx[pCur->iPage]++;
8841 pPage = pCur->apPage[pCur->iPage];
8842 }
8843
8844 /* Descend to the child node of the cell that the cursor currently
8845 ** points at. This is the right-child if (iIdx==pPage->nCell).
8846 */
8847 iIdx = pCur->aiIdx[pCur->iPage];
8848 if( iIdx==pPage->nCell ){
8849 rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8]));
8850 }else{
8851 rc = moveToChild(pCur, get4byte(findCell(pPage, iIdx)));
8852 }
8853 }
8854
8855 /* An error has occurred. Return an error code. */
8856 return rc;
8857 }
8858 #endif
8859
8860 /*
8861 ** Return the pager associated with a BTree. This routine is used for
8862 ** testing and debugging only.
8863 */
8864 SQLITE_PRIVATE Pager *sqlite3BtreePager(Btree *p){
8865 return p->pBt->pPager;
8866 }
8867
8868 #ifndef SQLITE_OMIT_INTEGRITY_CHECK
8869 /*
8870 ** Append a message to the error message string.
8871 */
8872 static void checkAppendMsg(
8873 IntegrityCk *pCheck,
8874 const char *zFormat,
8875 ...
8876 ){
8877 va_list ap;
8878 if( !pCheck->mxErr ) return;
8879 pCheck->mxErr--;
8880 pCheck->nErr++;
8881 va_start(ap, zFormat);
8882 if( pCheck->errMsg.nChar ){
8883 sqlite3StrAccumAppend(&pCheck->errMsg, "\n", 1);
8884 }
8885 if( pCheck->zPfx ){
8886 sqlite3XPrintf(&pCheck->errMsg, pCheck->zPfx, pCheck->v1, pCheck->v2);
8887 }
8888 sqlite3VXPrintf(&pCheck->errMsg, zFormat, ap);
8889 va_end(ap);
8890 if( pCheck->errMsg.accError==STRACCUM_NOMEM ){
8891 pCheck->mallocFailed = 1;
8892 }
8893 }
8894 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
8895
8896 #ifndef SQLITE_OMIT_INTEGRITY_CHECK
8897
8898 /*
8899 ** Return non-zero if the bit in the IntegrityCk.aPgRef[] array that
8900 ** corresponds to page iPg is already set.
8901 */
8902 static int getPageReferenced(IntegrityCk *pCheck, Pgno iPg){
8903 assert( iPg<=pCheck->nPage && sizeof(pCheck->aPgRef[0])==1 );
8904 return (pCheck->aPgRef[iPg/8] & (1 << (iPg & 0x07)));
8905 }
8906
8907 /*
8908 ** Set the bit in the IntegrityCk.aPgRef[] array that corresponds to page iPg.
8909 */
8910 static void setPageReferenced(IntegrityCk *pCheck, Pgno iPg){
8911 assert( iPg<=pCheck->nPage && sizeof(pCheck->aPgRef[0])==1 );
8912 pCheck->aPgRef[iPg/8] |= (1 << (iPg & 0x07));
8913 }
8914
8915
8916 /*
8917 ** Add 1 to the reference count for page iPage. If this is the second
8918 ** reference to the page, add an error message to pCheck->zErrMsg.
8919 ** Return 1 if there are 2 or more references to the page and 0 if
8920 ** if this is the first reference to the page.
8921 **
8922 ** Also check that the page number is in bounds.
8923 */
8924 static int checkRef(IntegrityCk *pCheck, Pgno iPage){
8925 if( iPage==0 ) return 1;
8926 if( iPage>pCheck->nPage ){
8927 checkAppendMsg(pCheck, "invalid page number %d", iPage);
8928 return 1;
8929 }
8930 if( getPageReferenced(pCheck, iPage) ){
8931 checkAppendMsg(pCheck, "2nd reference to page %d", iPage);
8932 return 1;
8933 }
8934 setPageReferenced(pCheck, iPage);
8935 return 0;
8936 }
8937
8938 #ifndef SQLITE_OMIT_AUTOVACUUM
8939 /*
8940 ** Check that the entry in the pointer-map for page iChild maps to
8941 ** page iParent, pointer type ptrType. If not, append an error message
8942 ** to pCheck.
8943 */
8944 static void checkPtrmap(
8945 IntegrityCk *pCheck, /* Integrity check context */
8946 Pgno iChild, /* Child page number */
8947 u8 eType, /* Expected pointer map type */
8948 Pgno iParent /* Expected pointer map parent page number */
8949 ){
8950 int rc;
8951 u8 ePtrmapType;
8952 Pgno iPtrmapParent;
8953
8954 rc = ptrmapGet(pCheck->pBt, iChild, &ePtrmapType, &iPtrmapParent);
8955 if( rc!=SQLITE_OK ){
8956 if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ) pCheck->mallocFailed = 1;
8957 checkAppendMsg(pCheck, "Failed to read ptrmap key=%d", iChild);
8958 return;
8959 }
8960
8961 if( ePtrmapType!=eType || iPtrmapParent!=iParent ){
8962 checkAppendMsg(pCheck,
8963 "Bad ptr map entry key=%d expected=(%d,%d) got=(%d,%d)",
8964 iChild, eType, iParent, ePtrmapType, iPtrmapParent);
8965 }
8966 }
8967 #endif
8968
8969 /*
8970 ** Check the integrity of the freelist or of an overflow page list.
8971 ** Verify that the number of pages on the list is N.
8972 */
8973 static void checkList(
8974 IntegrityCk *pCheck, /* Integrity checking context */
8975 int isFreeList, /* True for a freelist. False for overflow page list */
8976 int iPage, /* Page number for first page in the list */
8977 int N /* Expected number of pages in the list */
8978 ){
8979 int i;
8980 int expected = N;
8981 int iFirst = iPage;
8982 while( N-- > 0 && pCheck->mxErr ){
8983 DbPage *pOvflPage;
8984 unsigned char *pOvflData;
8985 if( iPage<1 ){
8986 checkAppendMsg(pCheck,
8987 "%d of %d pages missing from overflow list starting at %d",
8988 N+1, expected, iFirst);
8989 break;
8990 }
8991 if( checkRef(pCheck, iPage) ) break;
8992 if( sqlite3PagerGet(pCheck->pPager, (Pgno)iPage, &pOvflPage, 0) ){
8993 checkAppendMsg(pCheck, "failed to get page %d", iPage);
8994 break;
8995 }
8996 pOvflData = (unsigned char *)sqlite3PagerGetData(pOvflPage);
8997 if( isFreeList ){
8998 int n = get4byte(&pOvflData[4]);
8999 #ifndef SQLITE_OMIT_AUTOVACUUM
9000 if( pCheck->pBt->autoVacuum ){
9001 checkPtrmap(pCheck, iPage, PTRMAP_FREEPAGE, 0);
9002 }
9003 #endif
9004 if( n>(int)pCheck->pBt->usableSize/4-2 ){
9005 checkAppendMsg(pCheck,
9006 "freelist leaf count too big on page %d", iPage);
9007 N--;
9008 }else{
9009 for(i=0; i<n; i++){
9010 Pgno iFreePage = get4byte(&pOvflData[8+i*4]);
9011 #ifndef SQLITE_OMIT_AUTOVACUUM
9012 if( pCheck->pBt->autoVacuum ){
9013 checkPtrmap(pCheck, iFreePage, PTRMAP_FREEPAGE, 0);
9014 }
9015 #endif
9016 checkRef(pCheck, iFreePage);
9017 }
9018 N -= n;
9019 }
9020 }
9021 #ifndef SQLITE_OMIT_AUTOVACUUM
9022 else{
9023 /* If this database supports auto-vacuum and iPage is not the last
9024 ** page in this overflow list, check that the pointer-map entry for
9025 ** the following page matches iPage.
9026 */
9027 if( pCheck->pBt->autoVacuum && N>0 ){
9028 i = get4byte(pOvflData);
9029 checkPtrmap(pCheck, i, PTRMAP_OVERFLOW2, iPage);
9030 }
9031 }
9032 #endif
9033 iPage = get4byte(pOvflData);
9034 sqlite3PagerUnref(pOvflPage);
9035
9036 if( isFreeList && N<(iPage!=0) ){
9037 checkAppendMsg(pCheck, "free-page count in header is too small");
9038 }
9039 }
9040 }
9041 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
9042
9043 /*
9044 ** An implementation of a min-heap.
9045 **
9046 ** aHeap[0] is the number of elements on the heap. aHeap[1] is the
9047 ** root element. The daughter nodes of aHeap[N] are aHeap[N*2]
9048 ** and aHeap[N*2+1].
9049 **
9050 ** The heap property is this: Every node is less than or equal to both
9051 ** of its daughter nodes. A consequence of the heap property is that the
9052 ** root node aHeap[1] is always the minimum value currently in the heap.
9053 **
9054 ** The btreeHeapInsert() routine inserts an unsigned 32-bit number onto
9055 ** the heap, preserving the heap property. The btreeHeapPull() routine
9056 ** removes the root element from the heap (the minimum value in the heap)
9057 ** and then moves other nodes around as necessary to preserve the heap
9058 ** property.
9059 **
9060 ** This heap is used for cell overlap and coverage testing. Each u32
9061 ** entry represents the span of a cell or freeblock on a btree page.
9062 ** The upper 16 bits are the index of the first byte of a range and the
9063 ** lower 16 bits are the index of the last byte of that range.
9064 */
9065 static void btreeHeapInsert(u32 *aHeap, u32 x){
9066 u32 j, i = ++aHeap[0];
9067 aHeap[i] = x;
9068 while( (j = i/2)>0 && aHeap[j]>aHeap[i] ){
9069 x = aHeap[j];
9070 aHeap[j] = aHeap[i];
9071 aHeap[i] = x;
9072 i = j;
9073 }
9074 }
9075 static int btreeHeapPull(u32 *aHeap, u32 *pOut){
9076 u32 j, i, x;
9077 if( (x = aHeap[0])==0 ) return 0;
9078 *pOut = aHeap[1];
9079 aHeap[1] = aHeap[x];
9080 aHeap[x] = 0xffffffff;
9081 aHeap[0]--;
9082 i = 1;
9083 while( (j = i*2)<=aHeap[0] ){
9084 if( aHeap[j]>aHeap[j+1] ) j++;
9085 if( aHeap[i]<aHeap[j] ) break;
9086 x = aHeap[i];
9087 aHeap[i] = aHeap[j];
9088 aHeap[j] = x;
9089 i = j;
9090 }
9091 return 1;
9092 }
9093
9094 #ifndef SQLITE_OMIT_INTEGRITY_CHECK
9095 /*
9096 ** Do various sanity checks on a single page of a tree. Return
9097 ** the tree depth. Root pages return 0. Parents of root pages
9098 ** return 1, and so forth.
9099 **
9100 ** These checks are done:
9101 **
9102 ** 1. Make sure that cells and freeblocks do not overlap
9103 ** but combine to completely cover the page.
9104 ** 2. Make sure integer cell keys are in order.
9105 ** 3. Check the integrity of overflow pages.
9106 ** 4. Recursively call checkTreePage on all children.
9107 ** 5. Verify that the depth of all children is the same.
9108 */
9109 static int checkTreePage(
9110 IntegrityCk *pCheck, /* Context for the sanity check */
9111 int iPage, /* Page number of the page to check */
9112 i64 *piMinKey, /* Write minimum integer primary key here */
9113 i64 maxKey /* Error if integer primary key greater than this */
9114 ){
9115 MemPage *pPage = 0; /* The page being analyzed */
9116 int i; /* Loop counter */
9117 int rc; /* Result code from subroutine call */
9118 int depth = -1, d2; /* Depth of a subtree */
9119 int pgno; /* Page number */
9120 int nFrag; /* Number of fragmented bytes on the page */
9121 int hdr; /* Offset to the page header */
9122 int cellStart; /* Offset to the start of the cell pointer array */
9123 int nCell; /* Number of cells */
9124 int doCoverageCheck = 1; /* True if cell coverage checking should be done */
9125 int keyCanBeEqual = 1; /* True if IPK can be equal to maxKey
9126 ** False if IPK must be strictly less than maxKey */
9127 u8 *data; /* Page content */
9128 u8 *pCell; /* Cell content */
9129 u8 *pCellIdx; /* Next element of the cell pointer array */
9130 BtShared *pBt; /* The BtShared object that owns pPage */
9131 u32 pc; /* Address of a cell */
9132 u32 usableSize; /* Usable size of the page */
9133 u32 contentOffset; /* Offset to the start of the cell content area */
9134 u32 *heap = 0; /* Min-heap used for checking cell coverage */
9135 u32 x, prev = 0; /* Next and previous entry on the min-heap */
9136 const char *saved_zPfx = pCheck->zPfx;
9137 int saved_v1 = pCheck->v1;
9138 int saved_v2 = pCheck->v2;
9139 u8 savedIsInit = 0;
9140
9141 /* Check that the page exists
9142 */
9143 pBt = pCheck->pBt;
9144 usableSize = pBt->usableSize;
9145 if( iPage==0 ) return 0;
9146 if( checkRef(pCheck, iPage) ) return 0;
9147 pCheck->zPfx = "Page %d: ";
9148 pCheck->v1 = iPage;
9149 if( (rc = btreeGetPage(pBt, (Pgno)iPage, &pPage, 0))!=0 ){
9150 checkAppendMsg(pCheck,
9151 "unable to get the page. error code=%d", rc);
9152 goto end_of_check;
9153 }
9154
9155 /* Clear MemPage.isInit to make sure the corruption detection code in
9156 ** btreeInitPage() is executed. */
9157 savedIsInit = pPage->isInit;
9158 pPage->isInit = 0;
9159 if( (rc = btreeInitPage(pPage))!=0 ){
9160 assert( rc==SQLITE_CORRUPT ); /* The only possible error from InitPage */
9161 checkAppendMsg(pCheck,
9162 "btreeInitPage() returns error code %d", rc);
9163 goto end_of_check;
9164 }
9165 data = pPage->aData;
9166 hdr = pPage->hdrOffset;
9167
9168 /* Set up for cell analysis */
9169 pCheck->zPfx = "On tree page %d cell %d: ";
9170 contentOffset = get2byteNotZero(&data[hdr+5]);
9171 assert( contentOffset<=usableSize ); /* Enforced by btreeInitPage() */
9172
9173 /* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the
9174 ** number of cells on the page. */
9175 nCell = get2byte(&data[hdr+3]);
9176 assert( pPage->nCell==nCell );
9177
9178 /* EVIDENCE-OF: R-23882-45353 The cell pointer array of a b-tree page
9179 ** immediately follows the b-tree page header. */
9180 cellStart = hdr + 12 - 4*pPage->leaf;
9181 assert( pPage->aCellIdx==&data[cellStart] );
9182 pCellIdx = &data[cellStart + 2*(nCell-1)];
9183
9184 if( !pPage->leaf ){
9185 /* Analyze the right-child page of internal pages */
9186 pgno = get4byte(&data[hdr+8]);
9187 #ifndef SQLITE_OMIT_AUTOVACUUM
9188 if( pBt->autoVacuum ){
9189 pCheck->zPfx = "On page %d at right child: ";
9190 checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage);
9191 }
9192 #endif
9193 depth = checkTreePage(pCheck, pgno, &maxKey, maxKey);
9194 keyCanBeEqual = 0;
9195 }else{
9196 /* For leaf pages, the coverage check will occur in the same loop
9197 ** as the other cell checks, so initialize the heap. */
9198 heap = pCheck->heap;
9199 heap[0] = 0;
9200 }
9201
9202 /* EVIDENCE-OF: R-02776-14802 The cell pointer array consists of K 2-byte
9203 ** integer offsets to the cell contents. */
9204 for(i=nCell-1; i>=0 && pCheck->mxErr; i--){
9205 CellInfo info;
9206
9207 /* Check cell size */
9208 pCheck->v2 = i;
9209 assert( pCellIdx==&data[cellStart + i*2] );
9210 pc = get2byteAligned(pCellIdx);
9211 pCellIdx -= 2;
9212 if( pc<contentOffset || pc>usableSize-4 ){
9213 checkAppendMsg(pCheck, "Offset %d out of range %d..%d",
9214 pc, contentOffset, usableSize-4);
9215 doCoverageCheck = 0;
9216 continue;
9217 }
9218 pCell = &data[pc];
9219 pPage->xParseCell(pPage, pCell, &info);
9220 if( pc+info.nSize>usableSize ){
9221 checkAppendMsg(pCheck, "Extends off end of page");
9222 doCoverageCheck = 0;
9223 continue;
9224 }
9225
9226 /* Check for integer primary key out of range */
9227 if( pPage->intKey ){
9228 if( keyCanBeEqual ? (info.nKey > maxKey) : (info.nKey >= maxKey) ){
9229 checkAppendMsg(pCheck, "Rowid %lld out of order", info.nKey);
9230 }
9231 maxKey = info.nKey;
9232 }
9233
9234 /* Check the content overflow list */
9235 if( info.nPayload>info.nLocal ){
9236 int nPage; /* Number of pages on the overflow chain */
9237 Pgno pgnoOvfl; /* First page of the overflow chain */
9238 assert( pc + info.nSize - 4 <= usableSize );
9239 nPage = (info.nPayload - info.nLocal + usableSize - 5)/(usableSize - 4);
9240 pgnoOvfl = get4byte(&pCell[info.nSize - 4]);
9241 #ifndef SQLITE_OMIT_AUTOVACUUM
9242 if( pBt->autoVacuum ){
9243 checkPtrmap(pCheck, pgnoOvfl, PTRMAP_OVERFLOW1, iPage);
9244 }
9245 #endif
9246 checkList(pCheck, 0, pgnoOvfl, nPage);
9247 }
9248
9249 if( !pPage->leaf ){
9250 /* Check sanity of left child page for internal pages */
9251 pgno = get4byte(pCell);
9252 #ifndef SQLITE_OMIT_AUTOVACUUM
9253 if( pBt->autoVacuum ){
9254 checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage);
9255 }
9256 #endif
9257 d2 = checkTreePage(pCheck, pgno, &maxKey, maxKey);
9258 keyCanBeEqual = 0;
9259 if( d2!=depth ){
9260 checkAppendMsg(pCheck, "Child page depth differs");
9261 depth = d2;
9262 }
9263 }else{
9264 /* Populate the coverage-checking heap for leaf pages */
9265 btreeHeapInsert(heap, (pc<<16)|(pc+info.nSize-1));
9266 }
9267 }
9268 *piMinKey = maxKey;
9269
9270 /* Check for complete coverage of the page
9271 */
9272 pCheck->zPfx = 0;
9273 if( doCoverageCheck && pCheck->mxErr>0 ){
9274 /* For leaf pages, the min-heap has already been initialized and the
9275 ** cells have already been inserted. But for internal pages, that has
9276 ** not yet been done, so do it now */
9277 if( !pPage->leaf ){
9278 heap = pCheck->heap;
9279 heap[0] = 0;
9280 for(i=nCell-1; i>=0; i--){
9281 u32 size;
9282 pc = get2byteAligned(&data[cellStart+i*2]);
9283 size = pPage->xCellSize(pPage, &data[pc]);
9284 btreeHeapInsert(heap, (pc<<16)|(pc+size-1));
9285 }
9286 }
9287 /* Add the freeblocks to the min-heap
9288 **
9289 ** EVIDENCE-OF: R-20690-50594 The second field of the b-tree page header
9290 ** is the offset of the first freeblock, or zero if there are no
9291 ** freeblocks on the page.
9292 */
9293 i = get2byte(&data[hdr+1]);
9294 while( i>0 ){
9295 int size, j;
9296 assert( (u32)i<=usableSize-4 ); /* Enforced by btreeInitPage() */
9297 size = get2byte(&data[i+2]);
9298 assert( (u32)(i+size)<=usableSize ); /* Enforced by btreeInitPage() */
9299 btreeHeapInsert(heap, (((u32)i)<<16)|(i+size-1));
9300 /* EVIDENCE-OF: R-58208-19414 The first 2 bytes of a freeblock are a
9301 ** big-endian integer which is the offset in the b-tree page of the next
9302 ** freeblock in the chain, or zero if the freeblock is the last on the
9303 ** chain. */
9304 j = get2byte(&data[i]);
9305 /* EVIDENCE-OF: R-06866-39125 Freeblocks are always connected in order of
9306 ** increasing offset. */
9307 assert( j==0 || j>i+size ); /* Enforced by btreeInitPage() */
9308 assert( (u32)j<=usableSize-4 ); /* Enforced by btreeInitPage() */
9309 i = j;
9310 }
9311 /* Analyze the min-heap looking for overlap between cells and/or
9312 ** freeblocks, and counting the number of untracked bytes in nFrag.
9313 **
9314 ** Each min-heap entry is of the form: (start_address<<16)|end_address.
9315 ** There is an implied first entry the covers the page header, the cell
9316 ** pointer index, and the gap between the cell pointer index and the start
9317 ** of cell content.
9318 **
9319 ** The loop below pulls entries from the min-heap in order and compares
9320 ** the start_address against the previous end_address. If there is an
9321 ** overlap, that means bytes are used multiple times. If there is a gap,
9322 ** that gap is added to the fragmentation count.
9323 */
9324 nFrag = 0;
9325 prev = contentOffset - 1; /* Implied first min-heap entry */
9326 while( btreeHeapPull(heap,&x) ){
9327 if( (prev&0xffff)>=(x>>16) ){
9328 checkAppendMsg(pCheck,
9329 "Multiple uses for byte %u of page %d", x>>16, iPage);
9330 break;
9331 }else{
9332 nFrag += (x>>16) - (prev&0xffff) - 1;
9333 prev = x;
9334 }
9335 }
9336 nFrag += usableSize - (prev&0xffff) - 1;
9337 /* EVIDENCE-OF: R-43263-13491 The total number of bytes in all fragments
9338 ** is stored in the fifth field of the b-tree page header.
9339 ** EVIDENCE-OF: R-07161-27322 The one-byte integer at offset 7 gives the
9340 ** number of fragmented free bytes within the cell content area.
9341 */
9342 if( heap[0]==0 && nFrag!=data[hdr+7] ){
9343 checkAppendMsg(pCheck,
9344 "Fragmentation of %d bytes reported as %d on page %d",
9345 nFrag, data[hdr+7], iPage);
9346 }
9347 }
9348
9349 end_of_check:
9350 if( !doCoverageCheck ) pPage->isInit = savedIsInit;
9351 releasePage(pPage);
9352 pCheck->zPfx = saved_zPfx;
9353 pCheck->v1 = saved_v1;
9354 pCheck->v2 = saved_v2;
9355 return depth+1;
9356 }
9357 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
9358
9359 #ifndef SQLITE_OMIT_INTEGRITY_CHECK
9360 /*
9361 ** This routine does a complete check of the given BTree file. aRoot[] is
9362 ** an array of pages numbers were each page number is the root page of
9363 ** a table. nRoot is the number of entries in aRoot.
9364 **
9365 ** A read-only or read-write transaction must be opened before calling
9366 ** this function.
9367 **
9368 ** Write the number of error seen in *pnErr. Except for some memory
9369 ** allocation errors, an error message held in memory obtained from
9370 ** malloc is returned if *pnErr is non-zero. If *pnErr==0 then NULL is
9371 ** returned. If a memory allocation error occurs, NULL is returned.
9372 */
9373 SQLITE_PRIVATE char *sqlite3BtreeIntegrityCheck(
9374 Btree *p, /* The btree to be checked */
9375 int *aRoot, /* An array of root pages numbers for individual trees */
9376 int nRoot, /* Number of entries in aRoot[] */
9377 int mxErr, /* Stop reporting errors after this many */
9378 int *pnErr /* Write number of errors seen to this variable */
9379 ){
9380 Pgno i;
9381 IntegrityCk sCheck;
9382 BtShared *pBt = p->pBt;
9383 int savedDbFlags = pBt->db->flags;
9384 char zErr[100];
9385 VVA_ONLY( int nRef );
9386
9387 sqlite3BtreeEnter(p);
9388 assert( p->inTrans>TRANS_NONE && pBt->inTransaction>TRANS_NONE );
9389 VVA_ONLY( nRef = sqlite3PagerRefcount(pBt->pPager) );
9390 assert( nRef>=0 );
9391 sCheck.pBt = pBt;
9392 sCheck.pPager = pBt->pPager;
9393 sCheck.nPage = btreePagecount(sCheck.pBt);
9394 sCheck.mxErr = mxErr;
9395 sCheck.nErr = 0;
9396 sCheck.mallocFailed = 0;
9397 sCheck.zPfx = 0;
9398 sCheck.v1 = 0;
9399 sCheck.v2 = 0;
9400 sCheck.aPgRef = 0;
9401 sCheck.heap = 0;
9402 sqlite3StrAccumInit(&sCheck.errMsg, 0, zErr, sizeof(zErr), SQLITE_MAX_LENGTH);
9403 sCheck.errMsg.printfFlags = SQLITE_PRINTF_INTERNAL;
9404 if( sCheck.nPage==0 ){
9405 goto integrity_ck_cleanup;
9406 }
9407
9408 sCheck.aPgRef = sqlite3MallocZero((sCheck.nPage / 8)+ 1);
9409 if( !sCheck.aPgRef ){
9410 sCheck.mallocFailed = 1;
9411 goto integrity_ck_cleanup;
9412 }
9413 sCheck.heap = (u32*)sqlite3PageMalloc( pBt->pageSize );
9414 if( sCheck.heap==0 ){
9415 sCheck.mallocFailed = 1;
9416 goto integrity_ck_cleanup;
9417 }
9418
9419 i = PENDING_BYTE_PAGE(pBt);
9420 if( i<=sCheck.nPage ) setPageReferenced(&sCheck, i);
9421
9422 /* Check the integrity of the freelist
9423 */
9424 sCheck.zPfx = "Main freelist: ";
9425 checkList(&sCheck, 1, get4byte(&pBt->pPage1->aData[32]),
9426 get4byte(&pBt->pPage1->aData[36]));
9427 sCheck.zPfx = 0;
9428
9429 /* Check all the tables.
9430 */
9431 testcase( pBt->db->flags & SQLITE_CellSizeCk );
9432 pBt->db->flags &= ~SQLITE_CellSizeCk;
9433 for(i=0; (int)i<nRoot && sCheck.mxErr; i++){
9434 i64 notUsed;
9435 if( aRoot[i]==0 ) continue;
9436 #ifndef SQLITE_OMIT_AUTOVACUUM
9437 if( pBt->autoVacuum && aRoot[i]>1 ){
9438 checkPtrmap(&sCheck, aRoot[i], PTRMAP_ROOTPAGE, 0);
9439 }
9440 #endif
9441 checkTreePage(&sCheck, aRoot[i], &notUsed, LARGEST_INT64);
9442 }
9443 pBt->db->flags = savedDbFlags;
9444
9445 /* Make sure every page in the file is referenced
9446 */
9447 for(i=1; i<=sCheck.nPage && sCheck.mxErr; i++){
9448 #ifdef SQLITE_OMIT_AUTOVACUUM
9449 if( getPageReferenced(&sCheck, i)==0 ){
9450 checkAppendMsg(&sCheck, "Page %d is never used", i);
9451 }
9452 #else
9453 /* If the database supports auto-vacuum, make sure no tables contain
9454 ** references to pointer-map pages.
9455 */
9456 if( getPageReferenced(&sCheck, i)==0 &&
9457 (PTRMAP_PAGENO(pBt, i)!=i || !pBt->autoVacuum) ){
9458 checkAppendMsg(&sCheck, "Page %d is never used", i);
9459 }
9460 if( getPageReferenced(&sCheck, i)!=0 &&
9461 (PTRMAP_PAGENO(pBt, i)==i && pBt->autoVacuum) ){
9462 checkAppendMsg(&sCheck, "Pointer map page %d is referenced", i);
9463 }
9464 #endif
9465 }
9466
9467 /* Clean up and report errors.
9468 */
9469 integrity_ck_cleanup:
9470 sqlite3PageFree(sCheck.heap);
9471 sqlite3_free(sCheck.aPgRef);
9472 if( sCheck.mallocFailed ){
9473 sqlite3StrAccumReset(&sCheck.errMsg);
9474 sCheck.nErr++;
9475 }
9476 *pnErr = sCheck.nErr;
9477 if( sCheck.nErr==0 ) sqlite3StrAccumReset(&sCheck.errMsg);
9478 /* Make sure this analysis did not leave any unref() pages. */
9479 assert( nRef==sqlite3PagerRefcount(pBt->pPager) );
9480 sqlite3BtreeLeave(p);
9481 return sqlite3StrAccumFinish(&sCheck.errMsg);
9482 }
9483 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
9484
9485 /*
9486 ** Return the full pathname of the underlying database file. Return
9487 ** an empty string if the database is in-memory or a TEMP database.
9488 **
9489 ** The pager filename is invariant as long as the pager is
9490 ** open so it is safe to access without the BtShared mutex.
9491 */
9492 SQLITE_PRIVATE const char *sqlite3BtreeGetFilename(Btree *p){
9493 assert( p->pBt->pPager!=0 );
9494 return sqlite3PagerFilename(p->pBt->pPager, 1);
9495 }
9496
9497 /*
9498 ** Return the pathname of the journal file for this database. The return
9499 ** value of this routine is the same regardless of whether the journal file
9500 ** has been created or not.
9501 **
9502 ** The pager journal filename is invariant as long as the pager is
9503 ** open so it is safe to access without the BtShared mutex.
9504 */
9505 SQLITE_PRIVATE const char *sqlite3BtreeGetJournalname(Btree *p){
9506 assert( p->pBt->pPager!=0 );
9507 return sqlite3PagerJournalname(p->pBt->pPager);
9508 }
9509
9510 /*
9511 ** Return non-zero if a transaction is active.
9512 */
9513 SQLITE_PRIVATE int sqlite3BtreeIsInTrans(Btree *p){
9514 assert( p==0 || sqlite3_mutex_held(p->db->mutex) );
9515 return (p && (p->inTrans==TRANS_WRITE));
9516 }
9517
9518 #ifndef SQLITE_OMIT_WAL
9519 /*
9520 ** Run a checkpoint on the Btree passed as the first argument.
9521 **
9522 ** Return SQLITE_LOCKED if this or any other connection has an open
9523 ** transaction on the shared-cache the argument Btree is connected to.
9524 **
9525 ** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL or RESTART.
9526 */
9527 SQLITE_PRIVATE int sqlite3BtreeCheckpoint(Btree *p, int eMode, int *pnLog, int * pnCkpt){
9528 int rc = SQLITE_OK;
9529 if( p ){
9530 BtShared *pBt = p->pBt;
9531 sqlite3BtreeEnter(p);
9532 if( pBt->inTransaction!=TRANS_NONE ){
9533 rc = SQLITE_LOCKED;
9534 }else{
9535 rc = sqlite3PagerCheckpoint(pBt->pPager, p->db, eMode, pnLog, pnCkpt);
9536 }
9537 sqlite3BtreeLeave(p);
9538 }
9539 return rc;
9540 }
9541 #endif
9542
9543 /*
9544 ** Return non-zero if a read (or write) transaction is active.
9545 */
9546 SQLITE_PRIVATE int sqlite3BtreeIsInReadTrans(Btree *p){
9547 assert( p );
9548 assert( sqlite3_mutex_held(p->db->mutex) );
9549 return p->inTrans!=TRANS_NONE;
9550 }
9551
9552 SQLITE_PRIVATE int sqlite3BtreeIsInBackup(Btree *p){
9553 assert( p );
9554 assert( sqlite3_mutex_held(p->db->mutex) );
9555 return p->nBackup!=0;
9556 }
9557
9558 /*
9559 ** This function returns a pointer to a blob of memory associated with
9560 ** a single shared-btree. The memory is used by client code for its own
9561 ** purposes (for example, to store a high-level schema associated with
9562 ** the shared-btree). The btree layer manages reference counting issues.
9563 **
9564 ** The first time this is called on a shared-btree, nBytes bytes of memory
9565 ** are allocated, zeroed, and returned to the caller. For each subsequent
9566 ** call the nBytes parameter is ignored and a pointer to the same blob
9567 ** of memory returned.
9568 **
9569 ** If the nBytes parameter is 0 and the blob of memory has not yet been
9570 ** allocated, a null pointer is returned. If the blob has already been
9571 ** allocated, it is returned as normal.
9572 **
9573 ** Just before the shared-btree is closed, the function passed as the
9574 ** xFree argument when the memory allocation was made is invoked on the
9575 ** blob of allocated memory. The xFree function should not call sqlite3_free()
9576 ** on the memory, the btree layer does that.
9577 */
9578 SQLITE_PRIVATE void *sqlite3BtreeSchema(Btree *p, int nBytes, void(*xFree)(void *)){
9579 BtShared *pBt = p->pBt;
9580 sqlite3BtreeEnter(p);
9581 if( !pBt->pSchema && nBytes ){
9582 pBt->pSchema = sqlite3DbMallocZero(0, nBytes);
9583 pBt->xFreeSchema = xFree;
9584 }
9585 sqlite3BtreeLeave(p);
9586 return pBt->pSchema;
9587 }
9588
9589 /*
9590 ** Return SQLITE_LOCKED_SHAREDCACHE if another user of the same shared
9591 ** btree as the argument handle holds an exclusive lock on the
9592 ** sqlite_master table. Otherwise SQLITE_OK.
9593 */
9594 SQLITE_PRIVATE int sqlite3BtreeSchemaLocked(Btree *p){
9595 int rc;
9596 assert( sqlite3_mutex_held(p->db->mutex) );
9597 sqlite3BtreeEnter(p);
9598 rc = querySharedCacheTableLock(p, MASTER_ROOT, READ_LOCK);
9599 assert( rc==SQLITE_OK || rc==SQLITE_LOCKED_SHAREDCACHE );
9600 sqlite3BtreeLeave(p);
9601 return rc;
9602 }
9603
9604
9605 #ifndef SQLITE_OMIT_SHARED_CACHE
9606 /*
9607 ** Obtain a lock on the table whose root page is iTab. The
9608 ** lock is a write lock if isWritelock is true or a read lock
9609 ** if it is false.
9610 */
9611 SQLITE_PRIVATE int sqlite3BtreeLockTable(Btree *p, int iTab, u8 isWriteLock){
9612 int rc = SQLITE_OK;
9613 assert( p->inTrans!=TRANS_NONE );
9614 if( p->sharable ){
9615 u8 lockType = READ_LOCK + isWriteLock;
9616 assert( READ_LOCK+1==WRITE_LOCK );
9617 assert( isWriteLock==0 || isWriteLock==1 );
9618
9619 sqlite3BtreeEnter(p);
9620 rc = querySharedCacheTableLock(p, iTab, lockType);
9621 if( rc==SQLITE_OK ){
9622 rc = setSharedCacheTableLock(p, iTab, lockType);
9623 }
9624 sqlite3BtreeLeave(p);
9625 }
9626 return rc;
9627 }
9628 #endif
9629
9630 #ifndef SQLITE_OMIT_INCRBLOB
9631 /*
9632 ** Argument pCsr must be a cursor opened for writing on an
9633 ** INTKEY table currently pointing at a valid table entry.
9634 ** This function modifies the data stored as part of that entry.
9635 **
9636 ** Only the data content may only be modified, it is not possible to
9637 ** change the length of the data stored. If this function is called with
9638 ** parameters that attempt to write past the end of the existing data,
9639 ** no modifications are made and SQLITE_CORRUPT is returned.
9640 */
9641 SQLITE_PRIVATE int sqlite3BtreePutData(BtCursor *pCsr, u32 offset, u32 amt, void *z){
9642 int rc;
9643 assert( cursorOwnsBtShared(pCsr) );
9644 assert( sqlite3_mutex_held(pCsr->pBtree->db->mutex) );
9645 assert( pCsr->curFlags & BTCF_Incrblob );
9646
9647 rc = restoreCursorPosition(pCsr);
9648 if( rc!=SQLITE_OK ){
9649 return rc;
9650 }
9651 assert( pCsr->eState!=CURSOR_REQUIRESEEK );
9652 if( pCsr->eState!=CURSOR_VALID ){
9653 return SQLITE_ABORT;
9654 }
9655
9656 /* Save the positions of all other cursors open on this table. This is
9657 ** required in case any of them are holding references to an xFetch
9658 ** version of the b-tree page modified by the accessPayload call below.
9659 **
9660 ** Note that pCsr must be open on a INTKEY table and saveCursorPosition()
9661 ** and hence saveAllCursors() cannot fail on a BTREE_INTKEY table, hence
9662 ** saveAllCursors can only return SQLITE_OK.
9663 */
9664 VVA_ONLY(rc =) saveAllCursors(pCsr->pBt, pCsr->pgnoRoot, pCsr);
9665 assert( rc==SQLITE_OK );
9666
9667 /* Check some assumptions:
9668 ** (a) the cursor is open for writing,
9669 ** (b) there is a read/write transaction open,
9670 ** (c) the connection holds a write-lock on the table (if required),
9671 ** (d) there are no conflicting read-locks, and
9672 ** (e) the cursor points at a valid row of an intKey table.
9673 */
9674 if( (pCsr->curFlags & BTCF_WriteFlag)==0 ){
9675 return SQLITE_READONLY;
9676 }
9677 assert( (pCsr->pBt->btsFlags & BTS_READ_ONLY)==0
9678 && pCsr->pBt->inTransaction==TRANS_WRITE );
9679 assert( hasSharedCacheTableLock(pCsr->pBtree, pCsr->pgnoRoot, 0, 2) );
9680 assert( !hasReadConflicts(pCsr->pBtree, pCsr->pgnoRoot) );
9681 assert( pCsr->apPage[pCsr->iPage]->intKey );
9682
9683 return accessPayload(pCsr, offset, amt, (unsigned char *)z, 1);
9684 }
9685
9686 /*
9687 ** Mark this cursor as an incremental blob cursor.
9688 */
9689 SQLITE_PRIVATE void sqlite3BtreeIncrblobCursor(BtCursor *pCur){
9690 pCur->curFlags |= BTCF_Incrblob;
9691 pCur->pBtree->hasIncrblobCur = 1;
9692 }
9693 #endif
9694
9695 /*
9696 ** Set both the "read version" (single byte at byte offset 18) and
9697 ** "write version" (single byte at byte offset 19) fields in the database
9698 ** header to iVersion.
9699 */
9700 SQLITE_PRIVATE int sqlite3BtreeSetVersion(Btree *pBtree, int iVersion){
9701 BtShared *pBt = pBtree->pBt;
9702 int rc; /* Return code */
9703
9704 assert( iVersion==1 || iVersion==2 );
9705
9706 /* If setting the version fields to 1, do not automatically open the
9707 ** WAL connection, even if the version fields are currently set to 2.
9708 */
9709 pBt->btsFlags &= ~BTS_NO_WAL;
9710 if( iVersion==1 ) pBt->btsFlags |= BTS_NO_WAL;
9711
9712 rc = sqlite3BtreeBeginTrans(pBtree, 0);
9713 if( rc==SQLITE_OK ){
9714 u8 *aData = pBt->pPage1->aData;
9715 if( aData[18]!=(u8)iVersion || aData[19]!=(u8)iVersion ){
9716 rc = sqlite3BtreeBeginTrans(pBtree, 2);
9717 if( rc==SQLITE_OK ){
9718 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
9719 if( rc==SQLITE_OK ){
9720 aData[18] = (u8)iVersion;
9721 aData[19] = (u8)iVersion;
9722 }
9723 }
9724 }
9725 }
9726
9727 pBt->btsFlags &= ~BTS_NO_WAL;
9728 return rc;
9729 }
9730
9731 /*
9732 ** Return true if the cursor has a hint specified. This routine is
9733 ** only used from within assert() statements
9734 */
9735 SQLITE_PRIVATE int sqlite3BtreeCursorHasHint(BtCursor *pCsr, unsigned int mask){
9736 return (pCsr->hints & mask)!=0;
9737 }
9738
9739 /*
9740 ** Return true if the given Btree is read-only.
9741 */
9742 SQLITE_PRIVATE int sqlite3BtreeIsReadonly(Btree *p){
9743 return (p->pBt->btsFlags & BTS_READ_ONLY)!=0;
9744 }
9745
9746 /*
9747 ** Return the size of the header added to each page by this module.
9748 */
9749 SQLITE_PRIVATE int sqlite3HeaderSizeBtree(void){ return ROUND8(sizeof(MemPage)); }
9750
9751 #if !defined(SQLITE_OMIT_SHARED_CACHE)
9752 /*
9753 ** Return true if the Btree passed as the only argument is sharable.
9754 */
9755 SQLITE_PRIVATE int sqlite3BtreeSharable(Btree *p){
9756 return p->sharable;
9757 }
9758
9759 /*
9760 ** Return the number of connections to the BtShared object accessed by
9761 ** the Btree handle passed as the only argument. For private caches
9762 ** this is always 1. For shared caches it may be 1 or greater.
9763 */
9764 SQLITE_PRIVATE int sqlite3BtreeConnectionCount(Btree *p){
9765 testcase( p->sharable );
9766 return p->pBt->nRef;
9767 }
9768 #endif
9769
9770 /************** End of btree.c ***********************************************/
9771 /************** Begin file backup.c ******************************************/
9772 /*
9773 ** 2009 January 28
9774 **
9775 ** The author disclaims copyright to this source code. In place of
9776 ** a legal notice, here is a blessing:
9777 **
9778 ** May you do good and not evil.
9779 ** May you find forgiveness for yourself and forgive others.
9780 ** May you share freely, never taking more than you give.
9781 **
9782 *************************************************************************
9783 ** This file contains the implementation of the sqlite3_backup_XXX()
9784 ** API functions and the related features.
9785 */
9786 /* #include "sqliteInt.h" */
9787 /* #include "btreeInt.h" */
9788
9789 /*
9790 ** Structure allocated for each backup operation.
9791 */
9792 struct sqlite3_backup {
9793 sqlite3* pDestDb; /* Destination database handle */
9794 Btree *pDest; /* Destination b-tree file */
9795 u32 iDestSchema; /* Original schema cookie in destination */
9796 int bDestLocked; /* True once a write-transaction is open on pDest */
9797
9798 Pgno iNext; /* Page number of the next source page to copy */
9799 sqlite3* pSrcDb; /* Source database handle */
9800 Btree *pSrc; /* Source b-tree file */
9801
9802 int rc; /* Backup process error code */
9803
9804 /* These two variables are set by every call to backup_step(). They are
9805 ** read by calls to backup_remaining() and backup_pagecount().
9806 */
9807 Pgno nRemaining; /* Number of pages left to copy */
9808 Pgno nPagecount; /* Total number of pages to copy */
9809
9810 int isAttached; /* True once backup has been registered with pager */
9811 sqlite3_backup *pNext; /* Next backup associated with source pager */
9812 };
9813
9814 /*
9815 ** THREAD SAFETY NOTES:
9816 **
9817 ** Once it has been created using backup_init(), a single sqlite3_backup
9818 ** structure may be accessed via two groups of thread-safe entry points:
9819 **
9820 ** * Via the sqlite3_backup_XXX() API function backup_step() and
9821 ** backup_finish(). Both these functions obtain the source database
9822 ** handle mutex and the mutex associated with the source BtShared
9823 ** structure, in that order.
9824 **
9825 ** * Via the BackupUpdate() and BackupRestart() functions, which are
9826 ** invoked by the pager layer to report various state changes in
9827 ** the page cache associated with the source database. The mutex
9828 ** associated with the source database BtShared structure will always
9829 ** be held when either of these functions are invoked.
9830 **
9831 ** The other sqlite3_backup_XXX() API functions, backup_remaining() and
9832 ** backup_pagecount() are not thread-safe functions. If they are called
9833 ** while some other thread is calling backup_step() or backup_finish(),
9834 ** the values returned may be invalid. There is no way for a call to
9835 ** BackupUpdate() or BackupRestart() to interfere with backup_remaining()
9836 ** or backup_pagecount().
9837 **
9838 ** Depending on the SQLite configuration, the database handles and/or
9839 ** the Btree objects may have their own mutexes that require locking.
9840 ** Non-sharable Btrees (in-memory databases for example), do not have
9841 ** associated mutexes.
9842 */
9843
9844 /*
9845 ** Return a pointer corresponding to database zDb (i.e. "main", "temp")
9846 ** in connection handle pDb. If such a database cannot be found, return
9847 ** a NULL pointer and write an error message to pErrorDb.
9848 **
9849 ** If the "temp" database is requested, it may need to be opened by this
9850 ** function. If an error occurs while doing so, return 0 and write an
9851 ** error message to pErrorDb.
9852 */
9853 static Btree *findBtree(sqlite3 *pErrorDb, sqlite3 *pDb, const char *zDb){
9854 int i = sqlite3FindDbName(pDb, zDb);
9855
9856 if( i==1 ){
9857 Parse sParse;
9858 int rc = 0;
9859 memset(&sParse, 0, sizeof(sParse));
9860 sParse.db = pDb;
9861 if( sqlite3OpenTempDatabase(&sParse) ){
9862 sqlite3ErrorWithMsg(pErrorDb, sParse.rc, "%s", sParse.zErrMsg);
9863 rc = SQLITE_ERROR;
9864 }
9865 sqlite3DbFree(pErrorDb, sParse.zErrMsg);
9866 sqlite3ParserReset(&sParse);
9867 if( rc ){
9868 return 0;
9869 }
9870 }
9871
9872 if( i<0 ){
9873 sqlite3ErrorWithMsg(pErrorDb, SQLITE_ERROR, "unknown database %s", zDb);
9874 return 0;
9875 }
9876
9877 return pDb->aDb[i].pBt;
9878 }
9879
9880 /*
9881 ** Attempt to set the page size of the destination to match the page size
9882 ** of the source.
9883 */
9884 static int setDestPgsz(sqlite3_backup *p){
9885 int rc;
9886 rc = sqlite3BtreeSetPageSize(p->pDest,sqlite3BtreeGetPageSize(p->pSrc),-1,0);
9887 return rc;
9888 }
9889
9890 /*
9891 ** Check that there is no open read-transaction on the b-tree passed as the
9892 ** second argument. If there is not, return SQLITE_OK. Otherwise, if there
9893 ** is an open read-transaction, return SQLITE_ERROR and leave an error
9894 ** message in database handle db.
9895 */
9896 static int checkReadTransaction(sqlite3 *db, Btree *p){
9897 if( sqlite3BtreeIsInReadTrans(p) ){
9898 sqlite3ErrorWithMsg(db, SQLITE_ERROR, "destination database is in use");
9899 return SQLITE_ERROR;
9900 }
9901 return SQLITE_OK;
9902 }
9903
9904 /*
9905 ** Create an sqlite3_backup process to copy the contents of zSrcDb from
9906 ** connection handle pSrcDb to zDestDb in pDestDb. If successful, return
9907 ** a pointer to the new sqlite3_backup object.
9908 **
9909 ** If an error occurs, NULL is returned and an error code and error message
9910 ** stored in database handle pDestDb.
9911 */
9912 SQLITE_API sqlite3_backup *sqlite3_backup_init(
9913 sqlite3* pDestDb, /* Database to write to */
9914 const char *zDestDb, /* Name of database within pDestDb */
9915 sqlite3* pSrcDb, /* Database connection to read from */
9916 const char *zSrcDb /* Name of database within pSrcDb */
9917 ){
9918 sqlite3_backup *p; /* Value to return */
9919
9920 #ifdef SQLITE_ENABLE_API_ARMOR
9921 if( !sqlite3SafetyCheckOk(pSrcDb)||!sqlite3SafetyCheckOk(pDestDb) ){
9922 (void)SQLITE_MISUSE_BKPT;
9923 return 0;
9924 }
9925 #endif
9926
9927 /* Lock the source database handle. The destination database
9928 ** handle is not locked in this routine, but it is locked in
9929 ** sqlite3_backup_step(). The user is required to ensure that no
9930 ** other thread accesses the destination handle for the duration
9931 ** of the backup operation. Any attempt to use the destination
9932 ** database connection while a backup is in progress may cause
9933 ** a malfunction or a deadlock.
9934 */
9935 sqlite3_mutex_enter(pSrcDb->mutex);
9936 sqlite3_mutex_enter(pDestDb->mutex);
9937
9938 if( pSrcDb==pDestDb ){
9939 sqlite3ErrorWithMsg(
9940 pDestDb, SQLITE_ERROR, "source and destination must be distinct"
9941 );
9942 p = 0;
9943 }else {
9944 /* Allocate space for a new sqlite3_backup object...
9945 ** EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a
9946 ** call to sqlite3_backup_init() and is destroyed by a call to
9947 ** sqlite3_backup_finish(). */
9948 p = (sqlite3_backup *)sqlite3MallocZero(sizeof(sqlite3_backup));
9949 if( !p ){
9950 sqlite3Error(pDestDb, SQLITE_NOMEM_BKPT);
9951 }
9952 }
9953
9954 /* If the allocation succeeded, populate the new object. */
9955 if( p ){
9956 p->pSrc = findBtree(pDestDb, pSrcDb, zSrcDb);
9957 p->pDest = findBtree(pDestDb, pDestDb, zDestDb);
9958 p->pDestDb = pDestDb;
9959 p->pSrcDb = pSrcDb;
9960 p->iNext = 1;
9961 p->isAttached = 0;
9962
9963 if( 0==p->pSrc || 0==p->pDest
9964 || checkReadTransaction(pDestDb, p->pDest)!=SQLITE_OK
9965 ){
9966 /* One (or both) of the named databases did not exist or an OOM
9967 ** error was hit. Or there is a transaction open on the destination
9968 ** database. The error has already been written into the pDestDb
9969 ** handle. All that is left to do here is free the sqlite3_backup
9970 ** structure. */
9971 sqlite3_free(p);
9972 p = 0;
9973 }
9974 }
9975 if( p ){
9976 p->pSrc->nBackup++;
9977 }
9978
9979 sqlite3_mutex_leave(pDestDb->mutex);
9980 sqlite3_mutex_leave(pSrcDb->mutex);
9981 return p;
9982 }
9983
9984 /*
9985 ** Argument rc is an SQLite error code. Return true if this error is
9986 ** considered fatal if encountered during a backup operation. All errors
9987 ** are considered fatal except for SQLITE_BUSY and SQLITE_LOCKED.
9988 */
9989 static int isFatalError(int rc){
9990 return (rc!=SQLITE_OK && rc!=SQLITE_BUSY && ALWAYS(rc!=SQLITE_LOCKED));
9991 }
9992
9993 /*
9994 ** Parameter zSrcData points to a buffer containing the data for
9995 ** page iSrcPg from the source database. Copy this data into the
9996 ** destination database.
9997 */
9998 static int backupOnePage(
9999 sqlite3_backup *p, /* Backup handle */
10000 Pgno iSrcPg, /* Source database page to backup */
10001 const u8 *zSrcData, /* Source database page data */
10002 int bUpdate /* True for an update, false otherwise */
10003 ){
10004 Pager * const pDestPager = sqlite3BtreePager(p->pDest);
10005 const int nSrcPgsz = sqlite3BtreeGetPageSize(p->pSrc);
10006 int nDestPgsz = sqlite3BtreeGetPageSize(p->pDest);
10007 const int nCopy = MIN(nSrcPgsz, nDestPgsz);
10008 const i64 iEnd = (i64)iSrcPg*(i64)nSrcPgsz;
10009 #ifdef SQLITE_HAS_CODEC
10010 /* Use BtreeGetReserveNoMutex() for the source b-tree, as although it is
10011 ** guaranteed that the shared-mutex is held by this thread, handle
10012 ** p->pSrc may not actually be the owner. */
10013 int nSrcReserve = sqlite3BtreeGetReserveNoMutex(p->pSrc);
10014 int nDestReserve = sqlite3BtreeGetOptimalReserve(p->pDest);
10015 #endif
10016 int rc = SQLITE_OK;
10017 i64 iOff;
10018
10019 assert( sqlite3BtreeGetReserveNoMutex(p->pSrc)>=0 );
10020 assert( p->bDestLocked );
10021 assert( !isFatalError(p->rc) );
10022 assert( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) );
10023 assert( zSrcData );
10024
10025 /* Catch the case where the destination is an in-memory database and the
10026 ** page sizes of the source and destination differ.
10027 */
10028 if( nSrcPgsz!=nDestPgsz && sqlite3PagerIsMemdb(pDestPager) ){
10029 rc = SQLITE_READONLY;
10030 }
10031
10032 #ifdef SQLITE_HAS_CODEC
10033 /* Backup is not possible if the page size of the destination is changing
10034 ** and a codec is in use.
10035 */
10036 if( nSrcPgsz!=nDestPgsz && sqlite3PagerGetCodec(pDestPager)!=0 ){
10037 rc = SQLITE_READONLY;
10038 }
10039
10040 /* Backup is not possible if the number of bytes of reserve space differ
10041 ** between source and destination. If there is a difference, try to
10042 ** fix the destination to agree with the source. If that is not possible,
10043 ** then the backup cannot proceed.
10044 */
10045 if( nSrcReserve!=nDestReserve ){
10046 u32 newPgsz = nSrcPgsz;
10047 rc = sqlite3PagerSetPagesize(pDestPager, &newPgsz, nSrcReserve);
10048 if( rc==SQLITE_OK && newPgsz!=nSrcPgsz ) rc = SQLITE_READONLY;
10049 }
10050 #endif
10051
10052 /* This loop runs once for each destination page spanned by the source
10053 ** page. For each iteration, variable iOff is set to the byte offset
10054 ** of the destination page.
10055 */
10056 for(iOff=iEnd-(i64)nSrcPgsz; rc==SQLITE_OK && iOff<iEnd; iOff+=nDestPgsz){
10057 DbPage *pDestPg = 0;
10058 Pgno iDest = (Pgno)(iOff/nDestPgsz)+1;
10059 if( iDest==PENDING_BYTE_PAGE(p->pDest->pBt) ) continue;
10060 if( SQLITE_OK==(rc = sqlite3PagerGet(pDestPager, iDest, &pDestPg, 0))
10061 && SQLITE_OK==(rc = sqlite3PagerWrite(pDestPg))
10062 ){
10063 const u8 *zIn = &zSrcData[iOff%nSrcPgsz];
10064 u8 *zDestData = sqlite3PagerGetData(pDestPg);
10065 u8 *zOut = &zDestData[iOff%nDestPgsz];
10066
10067 /* Copy the data from the source page into the destination page.
10068 ** Then clear the Btree layer MemPage.isInit flag. Both this module
10069 ** and the pager code use this trick (clearing the first byte
10070 ** of the page 'extra' space to invalidate the Btree layers
10071 ** cached parse of the page). MemPage.isInit is marked
10072 ** "MUST BE FIRST" for this purpose.
10073 */
10074 memcpy(zOut, zIn, nCopy);
10075 ((u8 *)sqlite3PagerGetExtra(pDestPg))[0] = 0;
10076 if( iOff==0 && bUpdate==0 ){
10077 sqlite3Put4byte(&zOut[28], sqlite3BtreeLastPage(p->pSrc));
10078 }
10079 }
10080 sqlite3PagerUnref(pDestPg);
10081 }
10082
10083 return rc;
10084 }
10085
10086 /*
10087 ** If pFile is currently larger than iSize bytes, then truncate it to
10088 ** exactly iSize bytes. If pFile is not larger than iSize bytes, then
10089 ** this function is a no-op.
10090 **
10091 ** Return SQLITE_OK if everything is successful, or an SQLite error
10092 ** code if an error occurs.
10093 */
10094 static int backupTruncateFile(sqlite3_file *pFile, i64 iSize){
10095 i64 iCurrent;
10096 int rc = sqlite3OsFileSize(pFile, &iCurrent);
10097 if( rc==SQLITE_OK && iCurrent>iSize ){
10098 rc = sqlite3OsTruncate(pFile, iSize);
10099 }
10100 return rc;
10101 }
10102
10103 /*
10104 ** Register this backup object with the associated source pager for
10105 ** callbacks when pages are changed or the cache invalidated.
10106 */
10107 static void attachBackupObject(sqlite3_backup *p){
10108 sqlite3_backup **pp;
10109 assert( sqlite3BtreeHoldsMutex(p->pSrc) );
10110 pp = sqlite3PagerBackupPtr(sqlite3BtreePager(p->pSrc));
10111 p->pNext = *pp;
10112 *pp = p;
10113 p->isAttached = 1;
10114 }
10115
10116 /*
10117 ** Copy nPage pages from the source b-tree to the destination.
10118 */
10119 SQLITE_API int sqlite3_backup_step(sqlite3_backup *p, int nPage){
10120 int rc;
10121 int destMode; /* Destination journal mode */
10122 int pgszSrc = 0; /* Source page size */
10123 int pgszDest = 0; /* Destination page size */
10124
10125 #ifdef SQLITE_ENABLE_API_ARMOR
10126 if( p==0 ) return SQLITE_MISUSE_BKPT;
10127 #endif
10128 sqlite3_mutex_enter(p->pSrcDb->mutex);
10129 sqlite3BtreeEnter(p->pSrc);
10130 if( p->pDestDb ){
10131 sqlite3_mutex_enter(p->pDestDb->mutex);
10132 }
10133
10134 rc = p->rc;
10135 if( !isFatalError(rc) ){
10136 Pager * const pSrcPager = sqlite3BtreePager(p->pSrc); /* Source pager */
10137 Pager * const pDestPager = sqlite3BtreePager(p->pDest); /* Dest pager */
10138 int ii; /* Iterator variable */
10139 int nSrcPage = -1; /* Size of source db in pages */
10140 int bCloseTrans = 0; /* True if src db requires unlocking */
10141
10142 /* If the source pager is currently in a write-transaction, return
10143 ** SQLITE_BUSY immediately.
10144 */
10145 if( p->pDestDb && p->pSrc->pBt->inTransaction==TRANS_WRITE ){
10146 rc = SQLITE_BUSY;
10147 }else{
10148 rc = SQLITE_OK;
10149 }
10150
10151 /* If there is no open read-transaction on the source database, open
10152 ** one now. If a transaction is opened here, then it will be closed
10153 ** before this function exits.
10154 */
10155 if( rc==SQLITE_OK && 0==sqlite3BtreeIsInReadTrans(p->pSrc) ){
10156 rc = sqlite3BtreeBeginTrans(p->pSrc, 0);
10157 bCloseTrans = 1;
10158 }
10159
10160 /* If the destination database has not yet been locked (i.e. if this
10161 ** is the first call to backup_step() for the current backup operation),
10162 ** try to set its page size to the same as the source database. This
10163 ** is especially important on ZipVFS systems, as in that case it is
10164 ** not possible to create a database file that uses one page size by
10165 ** writing to it with another. */
10166 if( p->bDestLocked==0 && rc==SQLITE_OK && setDestPgsz(p)==SQLITE_NOMEM ){
10167 rc = SQLITE_NOMEM;
10168 }
10169
10170 /* Lock the destination database, if it is not locked already. */
10171 if( SQLITE_OK==rc && p->bDestLocked==0
10172 && SQLITE_OK==(rc = sqlite3BtreeBeginTrans(p->pDest, 2))
10173 ){
10174 p->bDestLocked = 1;
10175 sqlite3BtreeGetMeta(p->pDest, BTREE_SCHEMA_VERSION, &p->iDestSchema);
10176 }
10177
10178 /* Do not allow backup if the destination database is in WAL mode
10179 ** and the page sizes are different between source and destination */
10180 pgszSrc = sqlite3BtreeGetPageSize(p->pSrc);
10181 pgszDest = sqlite3BtreeGetPageSize(p->pDest);
10182 destMode = sqlite3PagerGetJournalMode(sqlite3BtreePager(p->pDest));
10183 if( SQLITE_OK==rc && destMode==PAGER_JOURNALMODE_WAL && pgszSrc!=pgszDest ){
10184 rc = SQLITE_READONLY;
10185 }
10186
10187 /* Now that there is a read-lock on the source database, query the
10188 ** source pager for the number of pages in the database.
10189 */
10190 nSrcPage = (int)sqlite3BtreeLastPage(p->pSrc);
10191 assert( nSrcPage>=0 );
10192 for(ii=0; (nPage<0 || ii<nPage) && p->iNext<=(Pgno)nSrcPage && !rc; ii++){
10193 const Pgno iSrcPg = p->iNext; /* Source page number */
10194 if( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) ){
10195 DbPage *pSrcPg; /* Source page object */
10196 rc = sqlite3PagerGet(pSrcPager, iSrcPg, &pSrcPg,PAGER_GET_READONLY);
10197 if( rc==SQLITE_OK ){
10198 rc = backupOnePage(p, iSrcPg, sqlite3PagerGetData(pSrcPg), 0);
10199 sqlite3PagerUnref(pSrcPg);
10200 }
10201 }
10202 p->iNext++;
10203 }
10204 if( rc==SQLITE_OK ){
10205 p->nPagecount = nSrcPage;
10206 p->nRemaining = nSrcPage+1-p->iNext;
10207 if( p->iNext>(Pgno)nSrcPage ){
10208 rc = SQLITE_DONE;
10209 }else if( !p->isAttached ){
10210 attachBackupObject(p);
10211 }
10212 }
10213
10214 /* Update the schema version field in the destination database. This
10215 ** is to make sure that the schema-version really does change in
10216 ** the case where the source and destination databases have the
10217 ** same schema version.
10218 */
10219 if( rc==SQLITE_DONE ){
10220 if( nSrcPage==0 ){
10221 rc = sqlite3BtreeNewDb(p->pDest);
10222 nSrcPage = 1;
10223 }
10224 if( rc==SQLITE_OK || rc==SQLITE_DONE ){
10225 rc = sqlite3BtreeUpdateMeta(p->pDest,1,p->iDestSchema+1);
10226 }
10227 if( rc==SQLITE_OK ){
10228 if( p->pDestDb ){
10229 sqlite3ResetAllSchemasOfConnection(p->pDestDb);
10230 }
10231 if( destMode==PAGER_JOURNALMODE_WAL ){
10232 rc = sqlite3BtreeSetVersion(p->pDest, 2);
10233 }
10234 }
10235 if( rc==SQLITE_OK ){
10236 int nDestTruncate;
10237 /* Set nDestTruncate to the final number of pages in the destination
10238 ** database. The complication here is that the destination page
10239 ** size may be different to the source page size.
10240 **
10241 ** If the source page size is smaller than the destination page size,
10242 ** round up. In this case the call to sqlite3OsTruncate() below will
10243 ** fix the size of the file. However it is important to call
10244 ** sqlite3PagerTruncateImage() here so that any pages in the
10245 ** destination file that lie beyond the nDestTruncate page mark are
10246 ** journalled by PagerCommitPhaseOne() before they are destroyed
10247 ** by the file truncation.
10248 */
10249 assert( pgszSrc==sqlite3BtreeGetPageSize(p->pSrc) );
10250 assert( pgszDest==sqlite3BtreeGetPageSize(p->pDest) );
10251 if( pgszSrc<pgszDest ){
10252 int ratio = pgszDest/pgszSrc;
10253 nDestTruncate = (nSrcPage+ratio-1)/ratio;
10254 if( nDestTruncate==(int)PENDING_BYTE_PAGE(p->pDest->pBt) ){
10255 nDestTruncate--;
10256 }
10257 }else{
10258 nDestTruncate = nSrcPage * (pgszSrc/pgszDest);
10259 }
10260 assert( nDestTruncate>0 );
10261
10262 if( pgszSrc<pgszDest ){
10263 /* If the source page-size is smaller than the destination page-size,
10264 ** two extra things may need to happen:
10265 **
10266 ** * The destination may need to be truncated, and
10267 **
10268 ** * Data stored on the pages immediately following the
10269 ** pending-byte page in the source database may need to be
10270 ** copied into the destination database.
10271 */
10272 const i64 iSize = (i64)pgszSrc * (i64)nSrcPage;
10273 sqlite3_file * const pFile = sqlite3PagerFile(pDestPager);
10274 Pgno iPg;
10275 int nDstPage;
10276 i64 iOff;
10277 i64 iEnd;
10278
10279 assert( pFile );
10280 assert( nDestTruncate==0
10281 || (i64)nDestTruncate*(i64)pgszDest >= iSize || (
10282 nDestTruncate==(int)(PENDING_BYTE_PAGE(p->pDest->pBt)-1)
10283 && iSize>=PENDING_BYTE && iSize<=PENDING_BYTE+pgszDest
10284 ));
10285
10286 /* This block ensures that all data required to recreate the original
10287 ** database has been stored in the journal for pDestPager and the
10288 ** journal synced to disk. So at this point we may safely modify
10289 ** the database file in any way, knowing that if a power failure
10290 ** occurs, the original database will be reconstructed from the
10291 ** journal file. */
10292 sqlite3PagerPagecount(pDestPager, &nDstPage);
10293 for(iPg=nDestTruncate; rc==SQLITE_OK && iPg<=(Pgno)nDstPage; iPg++){
10294 if( iPg!=PENDING_BYTE_PAGE(p->pDest->pBt) ){
10295 DbPage *pPg;
10296 rc = sqlite3PagerGet(pDestPager, iPg, &pPg, 0);
10297 if( rc==SQLITE_OK ){
10298 rc = sqlite3PagerWrite(pPg);
10299 sqlite3PagerUnref(pPg);
10300 }
10301 }
10302 }
10303 if( rc==SQLITE_OK ){
10304 rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 1);
10305 }
10306
10307 /* Write the extra pages and truncate the database file as required */
10308 iEnd = MIN(PENDING_BYTE + pgszDest, iSize);
10309 for(
10310 iOff=PENDING_BYTE+pgszSrc;
10311 rc==SQLITE_OK && iOff<iEnd;
10312 iOff+=pgszSrc
10313 ){
10314 PgHdr *pSrcPg = 0;
10315 const Pgno iSrcPg = (Pgno)((iOff/pgszSrc)+1);
10316 rc = sqlite3PagerGet(pSrcPager, iSrcPg, &pSrcPg, 0);
10317 if( rc==SQLITE_OK ){
10318 u8 *zData = sqlite3PagerGetData(pSrcPg);
10319 rc = sqlite3OsWrite(pFile, zData, pgszSrc, iOff);
10320 }
10321 sqlite3PagerUnref(pSrcPg);
10322 }
10323 if( rc==SQLITE_OK ){
10324 rc = backupTruncateFile(pFile, iSize);
10325 }
10326
10327 /* Sync the database file to disk. */
10328 if( rc==SQLITE_OK ){
10329 rc = sqlite3PagerSync(pDestPager, 0);
10330 }
10331 }else{
10332 sqlite3PagerTruncateImage(pDestPager, nDestTruncate);
10333 rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 0);
10334 }
10335
10336 /* Finish committing the transaction to the destination database. */
10337 if( SQLITE_OK==rc
10338 && SQLITE_OK==(rc = sqlite3BtreeCommitPhaseTwo(p->pDest, 0))
10339 ){
10340 rc = SQLITE_DONE;
10341 }
10342 }
10343 }
10344
10345 /* If bCloseTrans is true, then this function opened a read transaction
10346 ** on the source database. Close the read transaction here. There is
10347 ** no need to check the return values of the btree methods here, as
10348 ** "committing" a read-only transaction cannot fail.
10349 */
10350 if( bCloseTrans ){
10351 TESTONLY( int rc2 );
10352 TESTONLY( rc2 = ) sqlite3BtreeCommitPhaseOne(p->pSrc, 0);
10353 TESTONLY( rc2 |= ) sqlite3BtreeCommitPhaseTwo(p->pSrc, 0);
10354 assert( rc2==SQLITE_OK );
10355 }
10356
10357 if( rc==SQLITE_IOERR_NOMEM ){
10358 rc = SQLITE_NOMEM_BKPT;
10359 }
10360 p->rc = rc;
10361 }
10362 if( p->pDestDb ){
10363 sqlite3_mutex_leave(p->pDestDb->mutex);
10364 }
10365 sqlite3BtreeLeave(p->pSrc);
10366 sqlite3_mutex_leave(p->pSrcDb->mutex);
10367 return rc;
10368 }
10369
10370 /*
10371 ** Release all resources associated with an sqlite3_backup* handle.
10372 */
10373 SQLITE_API int sqlite3_backup_finish(sqlite3_backup *p){
10374 sqlite3_backup **pp; /* Ptr to head of pagers backup list */
10375 sqlite3 *pSrcDb; /* Source database connection */
10376 int rc; /* Value to return */
10377
10378 /* Enter the mutexes */
10379 if( p==0 ) return SQLITE_OK;
10380 pSrcDb = p->pSrcDb;
10381 sqlite3_mutex_enter(pSrcDb->mutex);
10382 sqlite3BtreeEnter(p->pSrc);
10383 if( p->pDestDb ){
10384 sqlite3_mutex_enter(p->pDestDb->mutex);
10385 }
10386
10387 /* Detach this backup from the source pager. */
10388 if( p->pDestDb ){
10389 p->pSrc->nBackup--;
10390 }
10391 if( p->isAttached ){
10392 pp = sqlite3PagerBackupPtr(sqlite3BtreePager(p->pSrc));
10393 while( *pp!=p ){
10394 pp = &(*pp)->pNext;
10395 }
10396 *pp = p->pNext;
10397 }
10398
10399 /* If a transaction is still open on the Btree, roll it back. */
10400 sqlite3BtreeRollback(p->pDest, SQLITE_OK, 0);
10401
10402 /* Set the error code of the destination database handle. */
10403 rc = (p->rc==SQLITE_DONE) ? SQLITE_OK : p->rc;
10404 if( p->pDestDb ){
10405 sqlite3Error(p->pDestDb, rc);
10406
10407 /* Exit the mutexes and free the backup context structure. */
10408 sqlite3LeaveMutexAndCloseZombie(p->pDestDb);
10409 }
10410 sqlite3BtreeLeave(p->pSrc);
10411 if( p->pDestDb ){
10412 /* EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a
10413 ** call to sqlite3_backup_init() and is destroyed by a call to
10414 ** sqlite3_backup_finish(). */
10415 sqlite3_free(p);
10416 }
10417 sqlite3LeaveMutexAndCloseZombie(pSrcDb);
10418 return rc;
10419 }
10420
10421 /*
10422 ** Return the number of pages still to be backed up as of the most recent
10423 ** call to sqlite3_backup_step().
10424 */
10425 SQLITE_API int sqlite3_backup_remaining(sqlite3_backup *p){
10426 #ifdef SQLITE_ENABLE_API_ARMOR
10427 if( p==0 ){
10428 (void)SQLITE_MISUSE_BKPT;
10429 return 0;
10430 }
10431 #endif
10432 return p->nRemaining;
10433 }
10434
10435 /*
10436 ** Return the total number of pages in the source database as of the most
10437 ** recent call to sqlite3_backup_step().
10438 */
10439 SQLITE_API int sqlite3_backup_pagecount(sqlite3_backup *p){
10440 #ifdef SQLITE_ENABLE_API_ARMOR
10441 if( p==0 ){
10442 (void)SQLITE_MISUSE_BKPT;
10443 return 0;
10444 }
10445 #endif
10446 return p->nPagecount;
10447 }
10448
10449 /*
10450 ** This function is called after the contents of page iPage of the
10451 ** source database have been modified. If page iPage has already been
10452 ** copied into the destination database, then the data written to the
10453 ** destination is now invalidated. The destination copy of iPage needs
10454 ** to be updated with the new data before the backup operation is
10455 ** complete.
10456 **
10457 ** It is assumed that the mutex associated with the BtShared object
10458 ** corresponding to the source database is held when this function is
10459 ** called.
10460 */
10461 static SQLITE_NOINLINE void backupUpdate(
10462 sqlite3_backup *p,
10463 Pgno iPage,
10464 const u8 *aData
10465 ){
10466 assert( p!=0 );
10467 do{
10468 assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) );
10469 if( !isFatalError(p->rc) && iPage<p->iNext ){
10470 /* The backup process p has already copied page iPage. But now it
10471 ** has been modified by a transaction on the source pager. Copy
10472 ** the new data into the backup.
10473 */
10474 int rc;
10475 assert( p->pDestDb );
10476 sqlite3_mutex_enter(p->pDestDb->mutex);
10477 rc = backupOnePage(p, iPage, aData, 1);
10478 sqlite3_mutex_leave(p->pDestDb->mutex);
10479 assert( rc!=SQLITE_BUSY && rc!=SQLITE_LOCKED );
10480 if( rc!=SQLITE_OK ){
10481 p->rc = rc;
10482 }
10483 }
10484 }while( (p = p->pNext)!=0 );
10485 }
10486 SQLITE_PRIVATE void sqlite3BackupUpdate(sqlite3_backup *pBackup, Pgno iPage, con st u8 *aData){
10487 if( pBackup ) backupUpdate(pBackup, iPage, aData);
10488 }
10489
10490 /*
10491 ** Restart the backup process. This is called when the pager layer
10492 ** detects that the database has been modified by an external database
10493 ** connection. In this case there is no way of knowing which of the
10494 ** pages that have been copied into the destination database are still
10495 ** valid and which are not, so the entire process needs to be restarted.
10496 **
10497 ** It is assumed that the mutex associated with the BtShared object
10498 ** corresponding to the source database is held when this function is
10499 ** called.
10500 */
10501 SQLITE_PRIVATE void sqlite3BackupRestart(sqlite3_backup *pBackup){
10502 sqlite3_backup *p; /* Iterator variable */
10503 for(p=pBackup; p; p=p->pNext){
10504 assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) );
10505 p->iNext = 1;
10506 }
10507 }
10508
10509 #ifndef SQLITE_OMIT_VACUUM
10510 /*
10511 ** Copy the complete content of pBtFrom into pBtTo. A transaction
10512 ** must be active for both files.
10513 **
10514 ** The size of file pTo may be reduced by this operation. If anything
10515 ** goes wrong, the transaction on pTo is rolled back. If successful, the
10516 ** transaction is committed before returning.
10517 */
10518 SQLITE_PRIVATE int sqlite3BtreeCopyFile(Btree *pTo, Btree *pFrom){
10519 int rc;
10520 sqlite3_file *pFd; /* File descriptor for database pTo */
10521 sqlite3_backup b;
10522 sqlite3BtreeEnter(pTo);
10523 sqlite3BtreeEnter(pFrom);
10524
10525 assert( sqlite3BtreeIsInTrans(pTo) );
10526 pFd = sqlite3PagerFile(sqlite3BtreePager(pTo));
10527 if( pFd->pMethods ){
10528 i64 nByte = sqlite3BtreeGetPageSize(pFrom)*(i64)sqlite3BtreeLastPage(pFrom);
10529 rc = sqlite3OsFileControl(pFd, SQLITE_FCNTL_OVERWRITE, &nByte);
10530 if( rc==SQLITE_NOTFOUND ) rc = SQLITE_OK;
10531 if( rc ) goto copy_finished;
10532 }
10533
10534 /* Set up an sqlite3_backup object. sqlite3_backup.pDestDb must be set
10535 ** to 0. This is used by the implementations of sqlite3_backup_step()
10536 ** and sqlite3_backup_finish() to detect that they are being called
10537 ** from this function, not directly by the user.
10538 */
10539 memset(&b, 0, sizeof(b));
10540 b.pSrcDb = pFrom->db;
10541 b.pSrc = pFrom;
10542 b.pDest = pTo;
10543 b.iNext = 1;
10544
10545 #ifdef SQLITE_HAS_CODEC
10546 sqlite3PagerAlignReserve(sqlite3BtreePager(pTo), sqlite3BtreePager(pFrom));
10547 #endif
10548
10549 /* 0x7FFFFFFF is the hard limit for the number of pages in a database
10550 ** file. By passing this as the number of pages to copy to
10551 ** sqlite3_backup_step(), we can guarantee that the copy finishes
10552 ** within a single call (unless an error occurs). The assert() statement
10553 ** checks this assumption - (p->rc) should be set to either SQLITE_DONE
10554 ** or an error code. */
10555 sqlite3_backup_step(&b, 0x7FFFFFFF);
10556 assert( b.rc!=SQLITE_OK );
10557
10558 rc = sqlite3_backup_finish(&b);
10559 if( rc==SQLITE_OK ){
10560 pTo->pBt->btsFlags &= ~BTS_PAGESIZE_FIXED;
10561 }else{
10562 sqlite3PagerClearCache(sqlite3BtreePager(b.pDest));
10563 }
10564
10565 assert( sqlite3BtreeIsInTrans(pTo)==0 );
10566 copy_finished:
10567 sqlite3BtreeLeave(pFrom);
10568 sqlite3BtreeLeave(pTo);
10569 return rc;
10570 }
10571 #endif /* SQLITE_OMIT_VACUUM */
10572
10573 /************** End of backup.c **********************************************/
10574 /************** Begin file vdbemem.c *****************************************/
10575 /*
10576 ** 2004 May 26
10577 **
10578 ** The author disclaims copyright to this source code. In place of
10579 ** a legal notice, here is a blessing:
10580 **
10581 ** May you do good and not evil.
10582 ** May you find forgiveness for yourself and forgive others.
10583 ** May you share freely, never taking more than you give.
10584 **
10585 *************************************************************************
10586 **
10587 ** This file contains code use to manipulate "Mem" structure. A "Mem"
10588 ** stores a single value in the VDBE. Mem is an opaque structure visible
10589 ** only within the VDBE. Interface routines refer to a Mem using the
10590 ** name sqlite_value
10591 */
10592 /* #include "sqliteInt.h" */
10593 /* #include "vdbeInt.h" */
10594
10595 #ifdef SQLITE_DEBUG
10596 /*
10597 ** Check invariants on a Mem object.
10598 **
10599 ** This routine is intended for use inside of assert() statements, like
10600 ** this: assert( sqlite3VdbeCheckMemInvariants(pMem) );
10601 */
10602 SQLITE_PRIVATE int sqlite3VdbeCheckMemInvariants(Mem *p){
10603 /* If MEM_Dyn is set then Mem.xDel!=0.
10604 ** Mem.xDel is might not be initialized if MEM_Dyn is clear.
10605 */
10606 assert( (p->flags & MEM_Dyn)==0 || p->xDel!=0 );
10607
10608 /* MEM_Dyn may only be set if Mem.szMalloc==0. In this way we
10609 ** ensure that if Mem.szMalloc>0 then it is safe to do
10610 ** Mem.z = Mem.zMalloc without having to check Mem.flags&MEM_Dyn.
10611 ** That saves a few cycles in inner loops. */
10612 assert( (p->flags & MEM_Dyn)==0 || p->szMalloc==0 );
10613
10614 /* Cannot be both MEM_Int and MEM_Real at the same time */
10615 assert( (p->flags & (MEM_Int|MEM_Real))!=(MEM_Int|MEM_Real) );
10616
10617 /* The szMalloc field holds the correct memory allocation size */
10618 assert( p->szMalloc==0
10619 || p->szMalloc==sqlite3DbMallocSize(p->db,p->zMalloc) );
10620
10621 /* If p holds a string or blob, the Mem.z must point to exactly
10622 ** one of the following:
10623 **
10624 ** (1) Memory in Mem.zMalloc and managed by the Mem object
10625 ** (2) Memory to be freed using Mem.xDel
10626 ** (3) An ephemeral string or blob
10627 ** (4) A static string or blob
10628 */
10629 if( (p->flags & (MEM_Str|MEM_Blob)) && p->n>0 ){
10630 assert(
10631 ((p->szMalloc>0 && p->z==p->zMalloc)? 1 : 0) +
10632 ((p->flags&MEM_Dyn)!=0 ? 1 : 0) +
10633 ((p->flags&MEM_Ephem)!=0 ? 1 : 0) +
10634 ((p->flags&MEM_Static)!=0 ? 1 : 0) == 1
10635 );
10636 }
10637 return 1;
10638 }
10639 #endif
10640
10641
10642 /*
10643 ** If pMem is an object with a valid string representation, this routine
10644 ** ensures the internal encoding for the string representation is
10645 ** 'desiredEnc', one of SQLITE_UTF8, SQLITE_UTF16LE or SQLITE_UTF16BE.
10646 **
10647 ** If pMem is not a string object, or the encoding of the string
10648 ** representation is already stored using the requested encoding, then this
10649 ** routine is a no-op.
10650 **
10651 ** SQLITE_OK is returned if the conversion is successful (or not required).
10652 ** SQLITE_NOMEM may be returned if a malloc() fails during conversion
10653 ** between formats.
10654 */
10655 SQLITE_PRIVATE int sqlite3VdbeChangeEncoding(Mem *pMem, int desiredEnc){
10656 #ifndef SQLITE_OMIT_UTF16
10657 int rc;
10658 #endif
10659 assert( (pMem->flags&MEM_RowSet)==0 );
10660 assert( desiredEnc==SQLITE_UTF8 || desiredEnc==SQLITE_UTF16LE
10661 || desiredEnc==SQLITE_UTF16BE );
10662 if( !(pMem->flags&MEM_Str) || pMem->enc==desiredEnc ){
10663 return SQLITE_OK;
10664 }
10665 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
10666 #ifdef SQLITE_OMIT_UTF16
10667 return SQLITE_ERROR;
10668 #else
10669
10670 /* MemTranslate() may return SQLITE_OK or SQLITE_NOMEM. If NOMEM is returned,
10671 ** then the encoding of the value may not have changed.
10672 */
10673 rc = sqlite3VdbeMemTranslate(pMem, (u8)desiredEnc);
10674 assert(rc==SQLITE_OK || rc==SQLITE_NOMEM);
10675 assert(rc==SQLITE_OK || pMem->enc!=desiredEnc);
10676 assert(rc==SQLITE_NOMEM || pMem->enc==desiredEnc);
10677 return rc;
10678 #endif
10679 }
10680
10681 /*
10682 ** Make sure pMem->z points to a writable allocation of at least
10683 ** min(n,32) bytes.
10684 **
10685 ** If the bPreserve argument is true, then copy of the content of
10686 ** pMem->z into the new allocation. pMem must be either a string or
10687 ** blob if bPreserve is true. If bPreserve is false, any prior content
10688 ** in pMem->z is discarded.
10689 */
10690 SQLITE_PRIVATE SQLITE_NOINLINE int sqlite3VdbeMemGrow(Mem *pMem, int n, int bPre serve){
10691 assert( sqlite3VdbeCheckMemInvariants(pMem) );
10692 assert( (pMem->flags&MEM_RowSet)==0 );
10693 testcase( pMem->db==0 );
10694
10695 /* If the bPreserve flag is set to true, then the memory cell must already
10696 ** contain a valid string or blob value. */
10697 assert( bPreserve==0 || pMem->flags&(MEM_Blob|MEM_Str) );
10698 testcase( bPreserve && pMem->z==0 );
10699
10700 assert( pMem->szMalloc==0
10701 || pMem->szMalloc==sqlite3DbMallocSize(pMem->db, pMem->zMalloc) );
10702 if( pMem->szMalloc<n ){
10703 if( n<32 ) n = 32;
10704 if( bPreserve && pMem->szMalloc>0 && pMem->z==pMem->zMalloc ){
10705 pMem->z = pMem->zMalloc = sqlite3DbReallocOrFree(pMem->db, pMem->z, n);
10706 bPreserve = 0;
10707 }else{
10708 if( pMem->szMalloc>0 ) sqlite3DbFree(pMem->db, pMem->zMalloc);
10709 pMem->zMalloc = sqlite3DbMallocRaw(pMem->db, n);
10710 }
10711 if( pMem->zMalloc==0 ){
10712 sqlite3VdbeMemSetNull(pMem);
10713 pMem->z = 0;
10714 pMem->szMalloc = 0;
10715 return SQLITE_NOMEM_BKPT;
10716 }else{
10717 pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->zMalloc);
10718 }
10719 }
10720
10721 if( bPreserve && pMem->z && pMem->z!=pMem->zMalloc ){
10722 memcpy(pMem->zMalloc, pMem->z, pMem->n);
10723 }
10724 if( (pMem->flags&MEM_Dyn)!=0 ){
10725 assert( pMem->xDel!=0 && pMem->xDel!=SQLITE_DYNAMIC );
10726 pMem->xDel((void *)(pMem->z));
10727 }
10728
10729 pMem->z = pMem->zMalloc;
10730 pMem->flags &= ~(MEM_Dyn|MEM_Ephem|MEM_Static);
10731 return SQLITE_OK;
10732 }
10733
10734 /*
10735 ** Change the pMem->zMalloc allocation to be at least szNew bytes.
10736 ** If pMem->zMalloc already meets or exceeds the requested size, this
10737 ** routine is a no-op.
10738 **
10739 ** Any prior string or blob content in the pMem object may be discarded.
10740 ** The pMem->xDel destructor is called, if it exists. Though MEM_Str
10741 ** and MEM_Blob values may be discarded, MEM_Int, MEM_Real, and MEM_Null
10742 ** values are preserved.
10743 **
10744 ** Return SQLITE_OK on success or an error code (probably SQLITE_NOMEM)
10745 ** if unable to complete the resizing.
10746 */
10747 SQLITE_PRIVATE int sqlite3VdbeMemClearAndResize(Mem *pMem, int szNew){
10748 assert( szNew>0 );
10749 assert( (pMem->flags & MEM_Dyn)==0 || pMem->szMalloc==0 );
10750 if( pMem->szMalloc<szNew ){
10751 return sqlite3VdbeMemGrow(pMem, szNew, 0);
10752 }
10753 assert( (pMem->flags & MEM_Dyn)==0 );
10754 pMem->z = pMem->zMalloc;
10755 pMem->flags &= (MEM_Null|MEM_Int|MEM_Real);
10756 return SQLITE_OK;
10757 }
10758
10759 /*
10760 ** Change pMem so that its MEM_Str or MEM_Blob value is stored in
10761 ** MEM.zMalloc, where it can be safely written.
10762 **
10763 ** Return SQLITE_OK on success or SQLITE_NOMEM if malloc fails.
10764 */
10765 SQLITE_PRIVATE int sqlite3VdbeMemMakeWriteable(Mem *pMem){
10766 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
10767 assert( (pMem->flags&MEM_RowSet)==0 );
10768 if( (pMem->flags & (MEM_Str|MEM_Blob))!=0 ){
10769 if( ExpandBlob(pMem) ) return SQLITE_NOMEM;
10770 if( pMem->szMalloc==0 || pMem->z!=pMem->zMalloc ){
10771 if( sqlite3VdbeMemGrow(pMem, pMem->n + 2, 1) ){
10772 return SQLITE_NOMEM_BKPT;
10773 }
10774 pMem->z[pMem->n] = 0;
10775 pMem->z[pMem->n+1] = 0;
10776 pMem->flags |= MEM_Term;
10777 }
10778 }
10779 pMem->flags &= ~MEM_Ephem;
10780 #ifdef SQLITE_DEBUG
10781 pMem->pScopyFrom = 0;
10782 #endif
10783
10784 return SQLITE_OK;
10785 }
10786
10787 /*
10788 ** If the given Mem* has a zero-filled tail, turn it into an ordinary
10789 ** blob stored in dynamically allocated space.
10790 */
10791 #ifndef SQLITE_OMIT_INCRBLOB
10792 SQLITE_PRIVATE int sqlite3VdbeMemExpandBlob(Mem *pMem){
10793 int nByte;
10794 assert( pMem->flags & MEM_Zero );
10795 assert( pMem->flags&MEM_Blob );
10796 assert( (pMem->flags&MEM_RowSet)==0 );
10797 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
10798
10799 /* Set nByte to the number of bytes required to store the expanded blob. */
10800 nByte = pMem->n + pMem->u.nZero;
10801 if( nByte<=0 ){
10802 nByte = 1;
10803 }
10804 if( sqlite3VdbeMemGrow(pMem, nByte, 1) ){
10805 return SQLITE_NOMEM_BKPT;
10806 }
10807
10808 memset(&pMem->z[pMem->n], 0, pMem->u.nZero);
10809 pMem->n += pMem->u.nZero;
10810 pMem->flags &= ~(MEM_Zero|MEM_Term);
10811 return SQLITE_OK;
10812 }
10813 #endif
10814
10815 /*
10816 ** It is already known that pMem contains an unterminated string.
10817 ** Add the zero terminator.
10818 */
10819 static SQLITE_NOINLINE int vdbeMemAddTerminator(Mem *pMem){
10820 if( sqlite3VdbeMemGrow(pMem, pMem->n+2, 1) ){
10821 return SQLITE_NOMEM_BKPT;
10822 }
10823 pMem->z[pMem->n] = 0;
10824 pMem->z[pMem->n+1] = 0;
10825 pMem->flags |= MEM_Term;
10826 return SQLITE_OK;
10827 }
10828
10829 /*
10830 ** Make sure the given Mem is \u0000 terminated.
10831 */
10832 SQLITE_PRIVATE int sqlite3VdbeMemNulTerminate(Mem *pMem){
10833 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
10834 testcase( (pMem->flags & (MEM_Term|MEM_Str))==(MEM_Term|MEM_Str) );
10835 testcase( (pMem->flags & (MEM_Term|MEM_Str))==0 );
10836 if( (pMem->flags & (MEM_Term|MEM_Str))!=MEM_Str ){
10837 return SQLITE_OK; /* Nothing to do */
10838 }else{
10839 return vdbeMemAddTerminator(pMem);
10840 }
10841 }
10842
10843 /*
10844 ** Add MEM_Str to the set of representations for the given Mem. Numbers
10845 ** are converted using sqlite3_snprintf(). Converting a BLOB to a string
10846 ** is a no-op.
10847 **
10848 ** Existing representations MEM_Int and MEM_Real are invalidated if
10849 ** bForce is true but are retained if bForce is false.
10850 **
10851 ** A MEM_Null value will never be passed to this function. This function is
10852 ** used for converting values to text for returning to the user (i.e. via
10853 ** sqlite3_value_text()), or for ensuring that values to be used as btree
10854 ** keys are strings. In the former case a NULL pointer is returned the
10855 ** user and the latter is an internal programming error.
10856 */
10857 SQLITE_PRIVATE int sqlite3VdbeMemStringify(Mem *pMem, u8 enc, u8 bForce){
10858 int fg = pMem->flags;
10859 const int nByte = 32;
10860
10861 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
10862 assert( !(fg&MEM_Zero) );
10863 assert( !(fg&(MEM_Str|MEM_Blob)) );
10864 assert( fg&(MEM_Int|MEM_Real) );
10865 assert( (pMem->flags&MEM_RowSet)==0 );
10866 assert( EIGHT_BYTE_ALIGNMENT(pMem) );
10867
10868
10869 if( sqlite3VdbeMemClearAndResize(pMem, nByte) ){
10870 pMem->enc = 0;
10871 return SQLITE_NOMEM_BKPT;
10872 }
10873
10874 /* For a Real or Integer, use sqlite3_snprintf() to produce the UTF-8
10875 ** string representation of the value. Then, if the required encoding
10876 ** is UTF-16le or UTF-16be do a translation.
10877 **
10878 ** FIX ME: It would be better if sqlite3_snprintf() could do UTF-16.
10879 */
10880 if( fg & MEM_Int ){
10881 sqlite3_snprintf(nByte, pMem->z, "%lld", pMem->u.i);
10882 }else{
10883 assert( fg & MEM_Real );
10884 sqlite3_snprintf(nByte, pMem->z, "%!.15g", pMem->u.r);
10885 }
10886 pMem->n = sqlite3Strlen30(pMem->z);
10887 pMem->enc = SQLITE_UTF8;
10888 pMem->flags |= MEM_Str|MEM_Term;
10889 if( bForce ) pMem->flags &= ~(MEM_Int|MEM_Real);
10890 sqlite3VdbeChangeEncoding(pMem, enc);
10891 return SQLITE_OK;
10892 }
10893
10894 /*
10895 ** Memory cell pMem contains the context of an aggregate function.
10896 ** This routine calls the finalize method for that function. The
10897 ** result of the aggregate is stored back into pMem.
10898 **
10899 ** Return SQLITE_ERROR if the finalizer reports an error. SQLITE_OK
10900 ** otherwise.
10901 */
10902 SQLITE_PRIVATE int sqlite3VdbeMemFinalize(Mem *pMem, FuncDef *pFunc){
10903 int rc = SQLITE_OK;
10904 if( ALWAYS(pFunc && pFunc->xFinalize) ){
10905 sqlite3_context ctx;
10906 Mem t;
10907 assert( (pMem->flags & MEM_Null)!=0 || pFunc==pMem->u.pDef );
10908 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
10909 memset(&ctx, 0, sizeof(ctx));
10910 memset(&t, 0, sizeof(t));
10911 t.flags = MEM_Null;
10912 t.db = pMem->db;
10913 ctx.pOut = &t;
10914 ctx.pMem = pMem;
10915 ctx.pFunc = pFunc;
10916 pFunc->xFinalize(&ctx); /* IMP: R-24505-23230 */
10917 assert( (pMem->flags & MEM_Dyn)==0 );
10918 if( pMem->szMalloc>0 ) sqlite3DbFree(pMem->db, pMem->zMalloc);
10919 memcpy(pMem, &t, sizeof(t));
10920 rc = ctx.isError;
10921 }
10922 return rc;
10923 }
10924
10925 /*
10926 ** If the memory cell contains a value that must be freed by
10927 ** invoking the external callback in Mem.xDel, then this routine
10928 ** will free that value. It also sets Mem.flags to MEM_Null.
10929 **
10930 ** This is a helper routine for sqlite3VdbeMemSetNull() and
10931 ** for sqlite3VdbeMemRelease(). Use those other routines as the
10932 ** entry point for releasing Mem resources.
10933 */
10934 static SQLITE_NOINLINE void vdbeMemClearExternAndSetNull(Mem *p){
10935 assert( p->db==0 || sqlite3_mutex_held(p->db->mutex) );
10936 assert( VdbeMemDynamic(p) );
10937 if( p->flags&MEM_Agg ){
10938 sqlite3VdbeMemFinalize(p, p->u.pDef);
10939 assert( (p->flags & MEM_Agg)==0 );
10940 testcase( p->flags & MEM_Dyn );
10941 }
10942 if( p->flags&MEM_Dyn ){
10943 assert( (p->flags&MEM_RowSet)==0 );
10944 assert( p->xDel!=SQLITE_DYNAMIC && p->xDel!=0 );
10945 p->xDel((void *)p->z);
10946 }else if( p->flags&MEM_RowSet ){
10947 sqlite3RowSetClear(p->u.pRowSet);
10948 }else if( p->flags&MEM_Frame ){
10949 VdbeFrame *pFrame = p->u.pFrame;
10950 pFrame->pParent = pFrame->v->pDelFrame;
10951 pFrame->v->pDelFrame = pFrame;
10952 }
10953 p->flags = MEM_Null;
10954 }
10955
10956 /*
10957 ** Release memory held by the Mem p, both external memory cleared
10958 ** by p->xDel and memory in p->zMalloc.
10959 **
10960 ** This is a helper routine invoked by sqlite3VdbeMemRelease() in
10961 ** the unusual case where there really is memory in p that needs
10962 ** to be freed.
10963 */
10964 static SQLITE_NOINLINE void vdbeMemClear(Mem *p){
10965 if( VdbeMemDynamic(p) ){
10966 vdbeMemClearExternAndSetNull(p);
10967 }
10968 if( p->szMalloc ){
10969 sqlite3DbFree(p->db, p->zMalloc);
10970 p->szMalloc = 0;
10971 }
10972 p->z = 0;
10973 }
10974
10975 /*
10976 ** Release any memory resources held by the Mem. Both the memory that is
10977 ** free by Mem.xDel and the Mem.zMalloc allocation are freed.
10978 **
10979 ** Use this routine prior to clean up prior to abandoning a Mem, or to
10980 ** reset a Mem back to its minimum memory utilization.
10981 **
10982 ** Use sqlite3VdbeMemSetNull() to release just the Mem.xDel space
10983 ** prior to inserting new content into the Mem.
10984 */
10985 SQLITE_PRIVATE void sqlite3VdbeMemRelease(Mem *p){
10986 assert( sqlite3VdbeCheckMemInvariants(p) );
10987 if( VdbeMemDynamic(p) || p->szMalloc ){
10988 vdbeMemClear(p);
10989 }
10990 }
10991
10992 /*
10993 ** Convert a 64-bit IEEE double into a 64-bit signed integer.
10994 ** If the double is out of range of a 64-bit signed integer then
10995 ** return the closest available 64-bit signed integer.
10996 */
10997 static i64 doubleToInt64(double r){
10998 #ifdef SQLITE_OMIT_FLOATING_POINT
10999 /* When floating-point is omitted, double and int64 are the same thing */
11000 return r;
11001 #else
11002 /*
11003 ** Many compilers we encounter do not define constants for the
11004 ** minimum and maximum 64-bit integers, or they define them
11005 ** inconsistently. And many do not understand the "LL" notation.
11006 ** So we define our own static constants here using nothing
11007 ** larger than a 32-bit integer constant.
11008 */
11009 static const i64 maxInt = LARGEST_INT64;
11010 static const i64 minInt = SMALLEST_INT64;
11011
11012 if( r<=(double)minInt ){
11013 return minInt;
11014 }else if( r>=(double)maxInt ){
11015 return maxInt;
11016 }else{
11017 return (i64)r;
11018 }
11019 #endif
11020 }
11021
11022 /*
11023 ** Return some kind of integer value which is the best we can do
11024 ** at representing the value that *pMem describes as an integer.
11025 ** If pMem is an integer, then the value is exact. If pMem is
11026 ** a floating-point then the value returned is the integer part.
11027 ** If pMem is a string or blob, then we make an attempt to convert
11028 ** it into an integer and return that. If pMem represents an
11029 ** an SQL-NULL value, return 0.
11030 **
11031 ** If pMem represents a string value, its encoding might be changed.
11032 */
11033 SQLITE_PRIVATE i64 sqlite3VdbeIntValue(Mem *pMem){
11034 int flags;
11035 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
11036 assert( EIGHT_BYTE_ALIGNMENT(pMem) );
11037 flags = pMem->flags;
11038 if( flags & MEM_Int ){
11039 return pMem->u.i;
11040 }else if( flags & MEM_Real ){
11041 return doubleToInt64(pMem->u.r);
11042 }else if( flags & (MEM_Str|MEM_Blob) ){
11043 i64 value = 0;
11044 assert( pMem->z || pMem->n==0 );
11045 sqlite3Atoi64(pMem->z, &value, pMem->n, pMem->enc);
11046 return value;
11047 }else{
11048 return 0;
11049 }
11050 }
11051
11052 /*
11053 ** Return the best representation of pMem that we can get into a
11054 ** double. If pMem is already a double or an integer, return its
11055 ** value. If it is a string or blob, try to convert it to a double.
11056 ** If it is a NULL, return 0.0.
11057 */
11058 SQLITE_PRIVATE double sqlite3VdbeRealValue(Mem *pMem){
11059 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
11060 assert( EIGHT_BYTE_ALIGNMENT(pMem) );
11061 if( pMem->flags & MEM_Real ){
11062 return pMem->u.r;
11063 }else if( pMem->flags & MEM_Int ){
11064 return (double)pMem->u.i;
11065 }else if( pMem->flags & (MEM_Str|MEM_Blob) ){
11066 /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
11067 double val = (double)0;
11068 sqlite3AtoF(pMem->z, &val, pMem->n, pMem->enc);
11069 return val;
11070 }else{
11071 /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
11072 return (double)0;
11073 }
11074 }
11075
11076 /*
11077 ** The MEM structure is already a MEM_Real. Try to also make it a
11078 ** MEM_Int if we can.
11079 */
11080 SQLITE_PRIVATE void sqlite3VdbeIntegerAffinity(Mem *pMem){
11081 i64 ix;
11082 assert( pMem->flags & MEM_Real );
11083 assert( (pMem->flags & MEM_RowSet)==0 );
11084 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
11085 assert( EIGHT_BYTE_ALIGNMENT(pMem) );
11086
11087 ix = doubleToInt64(pMem->u.r);
11088
11089 /* Only mark the value as an integer if
11090 **
11091 ** (1) the round-trip conversion real->int->real is a no-op, and
11092 ** (2) The integer is neither the largest nor the smallest
11093 ** possible integer (ticket #3922)
11094 **
11095 ** The second and third terms in the following conditional enforces
11096 ** the second condition under the assumption that addition overflow causes
11097 ** values to wrap around.
11098 */
11099 if( pMem->u.r==ix && ix>SMALLEST_INT64 && ix<LARGEST_INT64 ){
11100 pMem->u.i = ix;
11101 MemSetTypeFlag(pMem, MEM_Int);
11102 }
11103 }
11104
11105 /*
11106 ** Convert pMem to type integer. Invalidate any prior representations.
11107 */
11108 SQLITE_PRIVATE int sqlite3VdbeMemIntegerify(Mem *pMem){
11109 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
11110 assert( (pMem->flags & MEM_RowSet)==0 );
11111 assert( EIGHT_BYTE_ALIGNMENT(pMem) );
11112
11113 pMem->u.i = sqlite3VdbeIntValue(pMem);
11114 MemSetTypeFlag(pMem, MEM_Int);
11115 return SQLITE_OK;
11116 }
11117
11118 /*
11119 ** Convert pMem so that it is of type MEM_Real.
11120 ** Invalidate any prior representations.
11121 */
11122 SQLITE_PRIVATE int sqlite3VdbeMemRealify(Mem *pMem){
11123 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
11124 assert( EIGHT_BYTE_ALIGNMENT(pMem) );
11125
11126 pMem->u.r = sqlite3VdbeRealValue(pMem);
11127 MemSetTypeFlag(pMem, MEM_Real);
11128 return SQLITE_OK;
11129 }
11130
11131 /*
11132 ** Convert pMem so that it has types MEM_Real or MEM_Int or both.
11133 ** Invalidate any prior representations.
11134 **
11135 ** Every effort is made to force the conversion, even if the input
11136 ** is a string that does not look completely like a number. Convert
11137 ** as much of the string as we can and ignore the rest.
11138 */
11139 SQLITE_PRIVATE int sqlite3VdbeMemNumerify(Mem *pMem){
11140 if( (pMem->flags & (MEM_Int|MEM_Real|MEM_Null))==0 ){
11141 assert( (pMem->flags & (MEM_Blob|MEM_Str))!=0 );
11142 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
11143 if( 0==sqlite3Atoi64(pMem->z, &pMem->u.i, pMem->n, pMem->enc) ){
11144 MemSetTypeFlag(pMem, MEM_Int);
11145 }else{
11146 pMem->u.r = sqlite3VdbeRealValue(pMem);
11147 MemSetTypeFlag(pMem, MEM_Real);
11148 sqlite3VdbeIntegerAffinity(pMem);
11149 }
11150 }
11151 assert( (pMem->flags & (MEM_Int|MEM_Real|MEM_Null))!=0 );
11152 pMem->flags &= ~(MEM_Str|MEM_Blob|MEM_Zero);
11153 return SQLITE_OK;
11154 }
11155
11156 /*
11157 ** Cast the datatype of the value in pMem according to the affinity
11158 ** "aff". Casting is different from applying affinity in that a cast
11159 ** is forced. In other words, the value is converted into the desired
11160 ** affinity even if that results in loss of data. This routine is
11161 ** used (for example) to implement the SQL "cast()" operator.
11162 */
11163 SQLITE_PRIVATE void sqlite3VdbeMemCast(Mem *pMem, u8 aff, u8 encoding){
11164 if( pMem->flags & MEM_Null ) return;
11165 switch( aff ){
11166 case SQLITE_AFF_BLOB: { /* Really a cast to BLOB */
11167 if( (pMem->flags & MEM_Blob)==0 ){
11168 sqlite3ValueApplyAffinity(pMem, SQLITE_AFF_TEXT, encoding);
11169 assert( pMem->flags & MEM_Str || pMem->db->mallocFailed );
11170 if( pMem->flags & MEM_Str ) MemSetTypeFlag(pMem, MEM_Blob);
11171 }else{
11172 pMem->flags &= ~(MEM_TypeMask&~MEM_Blob);
11173 }
11174 break;
11175 }
11176 case SQLITE_AFF_NUMERIC: {
11177 sqlite3VdbeMemNumerify(pMem);
11178 break;
11179 }
11180 case SQLITE_AFF_INTEGER: {
11181 sqlite3VdbeMemIntegerify(pMem);
11182 break;
11183 }
11184 case SQLITE_AFF_REAL: {
11185 sqlite3VdbeMemRealify(pMem);
11186 break;
11187 }
11188 default: {
11189 assert( aff==SQLITE_AFF_TEXT );
11190 assert( MEM_Str==(MEM_Blob>>3) );
11191 pMem->flags |= (pMem->flags&MEM_Blob)>>3;
11192 sqlite3ValueApplyAffinity(pMem, SQLITE_AFF_TEXT, encoding);
11193 assert( pMem->flags & MEM_Str || pMem->db->mallocFailed );
11194 pMem->flags &= ~(MEM_Int|MEM_Real|MEM_Blob|MEM_Zero);
11195 break;
11196 }
11197 }
11198 }
11199
11200 /*
11201 ** Initialize bulk memory to be a consistent Mem object.
11202 **
11203 ** The minimum amount of initialization feasible is performed.
11204 */
11205 SQLITE_PRIVATE void sqlite3VdbeMemInit(Mem *pMem, sqlite3 *db, u16 flags){
11206 assert( (flags & ~MEM_TypeMask)==0 );
11207 pMem->flags = flags;
11208 pMem->db = db;
11209 pMem->szMalloc = 0;
11210 }
11211
11212
11213 /*
11214 ** Delete any previous value and set the value stored in *pMem to NULL.
11215 **
11216 ** This routine calls the Mem.xDel destructor to dispose of values that
11217 ** require the destructor. But it preserves the Mem.zMalloc memory allocation.
11218 ** To free all resources, use sqlite3VdbeMemRelease(), which both calls this
11219 ** routine to invoke the destructor and deallocates Mem.zMalloc.
11220 **
11221 ** Use this routine to reset the Mem prior to insert a new value.
11222 **
11223 ** Use sqlite3VdbeMemRelease() to complete erase the Mem prior to abandoning it.
11224 */
11225 SQLITE_PRIVATE void sqlite3VdbeMemSetNull(Mem *pMem){
11226 if( VdbeMemDynamic(pMem) ){
11227 vdbeMemClearExternAndSetNull(pMem);
11228 }else{
11229 pMem->flags = MEM_Null;
11230 }
11231 }
11232 SQLITE_PRIVATE void sqlite3ValueSetNull(sqlite3_value *p){
11233 sqlite3VdbeMemSetNull((Mem*)p);
11234 }
11235
11236 /*
11237 ** Delete any previous value and set the value to be a BLOB of length
11238 ** n containing all zeros.
11239 */
11240 SQLITE_PRIVATE void sqlite3VdbeMemSetZeroBlob(Mem *pMem, int n){
11241 sqlite3VdbeMemRelease(pMem);
11242 pMem->flags = MEM_Blob|MEM_Zero;
11243 pMem->n = 0;
11244 if( n<0 ) n = 0;
11245 pMem->u.nZero = n;
11246 pMem->enc = SQLITE_UTF8;
11247 pMem->z = 0;
11248 }
11249
11250 /*
11251 ** The pMem is known to contain content that needs to be destroyed prior
11252 ** to a value change. So invoke the destructor, then set the value to
11253 ** a 64-bit integer.
11254 */
11255 static SQLITE_NOINLINE void vdbeReleaseAndSetInt64(Mem *pMem, i64 val){
11256 sqlite3VdbeMemSetNull(pMem);
11257 pMem->u.i = val;
11258 pMem->flags = MEM_Int;
11259 }
11260
11261 /*
11262 ** Delete any previous value and set the value stored in *pMem to val,
11263 ** manifest type INTEGER.
11264 */
11265 SQLITE_PRIVATE void sqlite3VdbeMemSetInt64(Mem *pMem, i64 val){
11266 if( VdbeMemDynamic(pMem) ){
11267 vdbeReleaseAndSetInt64(pMem, val);
11268 }else{
11269 pMem->u.i = val;
11270 pMem->flags = MEM_Int;
11271 }
11272 }
11273
11274 #ifndef SQLITE_OMIT_FLOATING_POINT
11275 /*
11276 ** Delete any previous value and set the value stored in *pMem to val,
11277 ** manifest type REAL.
11278 */
11279 SQLITE_PRIVATE void sqlite3VdbeMemSetDouble(Mem *pMem, double val){
11280 sqlite3VdbeMemSetNull(pMem);
11281 if( !sqlite3IsNaN(val) ){
11282 pMem->u.r = val;
11283 pMem->flags = MEM_Real;
11284 }
11285 }
11286 #endif
11287
11288 /*
11289 ** Delete any previous value and set the value of pMem to be an
11290 ** empty boolean index.
11291 */
11292 SQLITE_PRIVATE void sqlite3VdbeMemSetRowSet(Mem *pMem){
11293 sqlite3 *db = pMem->db;
11294 assert( db!=0 );
11295 assert( (pMem->flags & MEM_RowSet)==0 );
11296 sqlite3VdbeMemRelease(pMem);
11297 pMem->zMalloc = sqlite3DbMallocRawNN(db, 64);
11298 if( db->mallocFailed ){
11299 pMem->flags = MEM_Null;
11300 pMem->szMalloc = 0;
11301 }else{
11302 assert( pMem->zMalloc );
11303 pMem->szMalloc = sqlite3DbMallocSize(db, pMem->zMalloc);
11304 pMem->u.pRowSet = sqlite3RowSetInit(db, pMem->zMalloc, pMem->szMalloc);
11305 assert( pMem->u.pRowSet!=0 );
11306 pMem->flags = MEM_RowSet;
11307 }
11308 }
11309
11310 /*
11311 ** Return true if the Mem object contains a TEXT or BLOB that is
11312 ** too large - whose size exceeds SQLITE_MAX_LENGTH.
11313 */
11314 SQLITE_PRIVATE int sqlite3VdbeMemTooBig(Mem *p){
11315 assert( p->db!=0 );
11316 if( p->flags & (MEM_Str|MEM_Blob) ){
11317 int n = p->n;
11318 if( p->flags & MEM_Zero ){
11319 n += p->u.nZero;
11320 }
11321 return n>p->db->aLimit[SQLITE_LIMIT_LENGTH];
11322 }
11323 return 0;
11324 }
11325
11326 #ifdef SQLITE_DEBUG
11327 /*
11328 ** This routine prepares a memory cell for modification by breaking
11329 ** its link to a shallow copy and by marking any current shallow
11330 ** copies of this cell as invalid.
11331 **
11332 ** This is used for testing and debugging only - to make sure shallow
11333 ** copies are not misused.
11334 */
11335 SQLITE_PRIVATE void sqlite3VdbeMemAboutToChange(Vdbe *pVdbe, Mem *pMem){
11336 int i;
11337 Mem *pX;
11338 for(i=0, pX=pVdbe->aMem; i<pVdbe->nMem; i++, pX++){
11339 if( pX->pScopyFrom==pMem ){
11340 pX->flags |= MEM_Undefined;
11341 pX->pScopyFrom = 0;
11342 }
11343 }
11344 pMem->pScopyFrom = 0;
11345 }
11346 #endif /* SQLITE_DEBUG */
11347
11348
11349 /*
11350 ** Make an shallow copy of pFrom into pTo. Prior contents of
11351 ** pTo are freed. The pFrom->z field is not duplicated. If
11352 ** pFrom->z is used, then pTo->z points to the same thing as pFrom->z
11353 ** and flags gets srcType (either MEM_Ephem or MEM_Static).
11354 */
11355 static SQLITE_NOINLINE void vdbeClrCopy(Mem *pTo, const Mem *pFrom, int eType){
11356 vdbeMemClearExternAndSetNull(pTo);
11357 assert( !VdbeMemDynamic(pTo) );
11358 sqlite3VdbeMemShallowCopy(pTo, pFrom, eType);
11359 }
11360 SQLITE_PRIVATE void sqlite3VdbeMemShallowCopy(Mem *pTo, const Mem *pFrom, int sr cType){
11361 assert( (pFrom->flags & MEM_RowSet)==0 );
11362 assert( pTo->db==pFrom->db );
11363 if( VdbeMemDynamic(pTo) ){ vdbeClrCopy(pTo,pFrom,srcType); return; }
11364 memcpy(pTo, pFrom, MEMCELLSIZE);
11365 if( (pFrom->flags&MEM_Static)==0 ){
11366 pTo->flags &= ~(MEM_Dyn|MEM_Static|MEM_Ephem);
11367 assert( srcType==MEM_Ephem || srcType==MEM_Static );
11368 pTo->flags |= srcType;
11369 }
11370 }
11371
11372 /*
11373 ** Make a full copy of pFrom into pTo. Prior contents of pTo are
11374 ** freed before the copy is made.
11375 */
11376 SQLITE_PRIVATE int sqlite3VdbeMemCopy(Mem *pTo, const Mem *pFrom){
11377 int rc = SQLITE_OK;
11378
11379 assert( (pFrom->flags & MEM_RowSet)==0 );
11380 if( VdbeMemDynamic(pTo) ) vdbeMemClearExternAndSetNull(pTo);
11381 memcpy(pTo, pFrom, MEMCELLSIZE);
11382 pTo->flags &= ~MEM_Dyn;
11383 if( pTo->flags&(MEM_Str|MEM_Blob) ){
11384 if( 0==(pFrom->flags&MEM_Static) ){
11385 pTo->flags |= MEM_Ephem;
11386 rc = sqlite3VdbeMemMakeWriteable(pTo);
11387 }
11388 }
11389
11390 return rc;
11391 }
11392
11393 /*
11394 ** Transfer the contents of pFrom to pTo. Any existing value in pTo is
11395 ** freed. If pFrom contains ephemeral data, a copy is made.
11396 **
11397 ** pFrom contains an SQL NULL when this routine returns.
11398 */
11399 SQLITE_PRIVATE void sqlite3VdbeMemMove(Mem *pTo, Mem *pFrom){
11400 assert( pFrom->db==0 || sqlite3_mutex_held(pFrom->db->mutex) );
11401 assert( pTo->db==0 || sqlite3_mutex_held(pTo->db->mutex) );
11402 assert( pFrom->db==0 || pTo->db==0 || pFrom->db==pTo->db );
11403
11404 sqlite3VdbeMemRelease(pTo);
11405 memcpy(pTo, pFrom, sizeof(Mem));
11406 pFrom->flags = MEM_Null;
11407 pFrom->szMalloc = 0;
11408 }
11409
11410 /*
11411 ** Change the value of a Mem to be a string or a BLOB.
11412 **
11413 ** The memory management strategy depends on the value of the xDel
11414 ** parameter. If the value passed is SQLITE_TRANSIENT, then the
11415 ** string is copied into a (possibly existing) buffer managed by the
11416 ** Mem structure. Otherwise, any existing buffer is freed and the
11417 ** pointer copied.
11418 **
11419 ** If the string is too large (if it exceeds the SQLITE_LIMIT_LENGTH
11420 ** size limit) then no memory allocation occurs. If the string can be
11421 ** stored without allocating memory, then it is. If a memory allocation
11422 ** is required to store the string, then value of pMem is unchanged. In
11423 ** either case, SQLITE_TOOBIG is returned.
11424 */
11425 SQLITE_PRIVATE int sqlite3VdbeMemSetStr(
11426 Mem *pMem, /* Memory cell to set to string value */
11427 const char *z, /* String pointer */
11428 int n, /* Bytes in string, or negative */
11429 u8 enc, /* Encoding of z. 0 for BLOBs */
11430 void (*xDel)(void*) /* Destructor function */
11431 ){
11432 int nByte = n; /* New value for pMem->n */
11433 int iLimit; /* Maximum allowed string or blob size */
11434 u16 flags = 0; /* New value for pMem->flags */
11435
11436 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
11437 assert( (pMem->flags & MEM_RowSet)==0 );
11438
11439 /* If z is a NULL pointer, set pMem to contain an SQL NULL. */
11440 if( !z ){
11441 sqlite3VdbeMemSetNull(pMem);
11442 return SQLITE_OK;
11443 }
11444
11445 if( pMem->db ){
11446 iLimit = pMem->db->aLimit[SQLITE_LIMIT_LENGTH];
11447 }else{
11448 iLimit = SQLITE_MAX_LENGTH;
11449 }
11450 flags = (enc==0?MEM_Blob:MEM_Str);
11451 if( nByte<0 ){
11452 assert( enc!=0 );
11453 if( enc==SQLITE_UTF8 ){
11454 nByte = sqlite3Strlen30(z);
11455 if( nByte>iLimit ) nByte = iLimit+1;
11456 }else{
11457 for(nByte=0; nByte<=iLimit && (z[nByte] | z[nByte+1]); nByte+=2){}
11458 }
11459 flags |= MEM_Term;
11460 }
11461
11462 /* The following block sets the new values of Mem.z and Mem.xDel. It
11463 ** also sets a flag in local variable "flags" to indicate the memory
11464 ** management (one of MEM_Dyn or MEM_Static).
11465 */
11466 if( xDel==SQLITE_TRANSIENT ){
11467 int nAlloc = nByte;
11468 if( flags&MEM_Term ){
11469 nAlloc += (enc==SQLITE_UTF8?1:2);
11470 }
11471 if( nByte>iLimit ){
11472 return SQLITE_TOOBIG;
11473 }
11474 testcase( nAlloc==0 );
11475 testcase( nAlloc==31 );
11476 testcase( nAlloc==32 );
11477 if( sqlite3VdbeMemClearAndResize(pMem, MAX(nAlloc,32)) ){
11478 return SQLITE_NOMEM_BKPT;
11479 }
11480 memcpy(pMem->z, z, nAlloc);
11481 }else if( xDel==SQLITE_DYNAMIC ){
11482 sqlite3VdbeMemRelease(pMem);
11483 pMem->zMalloc = pMem->z = (char *)z;
11484 pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->zMalloc);
11485 }else{
11486 sqlite3VdbeMemRelease(pMem);
11487 pMem->z = (char *)z;
11488 pMem->xDel = xDel;
11489 flags |= ((xDel==SQLITE_STATIC)?MEM_Static:MEM_Dyn);
11490 }
11491
11492 pMem->n = nByte;
11493 pMem->flags = flags;
11494 pMem->enc = (enc==0 ? SQLITE_UTF8 : enc);
11495
11496 #ifndef SQLITE_OMIT_UTF16
11497 if( pMem->enc!=SQLITE_UTF8 && sqlite3VdbeMemHandleBom(pMem) ){
11498 return SQLITE_NOMEM_BKPT;
11499 }
11500 #endif
11501
11502 if( nByte>iLimit ){
11503 return SQLITE_TOOBIG;
11504 }
11505
11506 return SQLITE_OK;
11507 }
11508
11509 /*
11510 ** Move data out of a btree key or data field and into a Mem structure.
11511 ** The data is payload from the entry that pCur is currently pointing
11512 ** to. offset and amt determine what portion of the data or key to retrieve.
11513 ** The result is written into the pMem element.
11514 **
11515 ** The pMem object must have been initialized. This routine will use
11516 ** pMem->zMalloc to hold the content from the btree, if possible. New
11517 ** pMem->zMalloc space will be allocated if necessary. The calling routine
11518 ** is responsible for making sure that the pMem object is eventually
11519 ** destroyed.
11520 **
11521 ** If this routine fails for any reason (malloc returns NULL or unable
11522 ** to read from the disk) then the pMem is left in an inconsistent state.
11523 */
11524 static SQLITE_NOINLINE int vdbeMemFromBtreeResize(
11525 BtCursor *pCur, /* Cursor pointing at record to retrieve. */
11526 u32 offset, /* Offset from the start of data to return bytes from. */
11527 u32 amt, /* Number of bytes to return. */
11528 Mem *pMem /* OUT: Return data in this Mem structure. */
11529 ){
11530 int rc;
11531 pMem->flags = MEM_Null;
11532 if( SQLITE_OK==(rc = sqlite3VdbeMemClearAndResize(pMem, amt+2)) ){
11533 rc = sqlite3BtreePayload(pCur, offset, amt, pMem->z);
11534 if( rc==SQLITE_OK ){
11535 pMem->z[amt] = 0;
11536 pMem->z[amt+1] = 0;
11537 pMem->flags = MEM_Blob|MEM_Term;
11538 pMem->n = (int)amt;
11539 }else{
11540 sqlite3VdbeMemRelease(pMem);
11541 }
11542 }
11543 return rc;
11544 }
11545 SQLITE_PRIVATE int sqlite3VdbeMemFromBtree(
11546 BtCursor *pCur, /* Cursor pointing at record to retrieve. */
11547 u32 offset, /* Offset from the start of data to return bytes from. */
11548 u32 amt, /* Number of bytes to return. */
11549 Mem *pMem /* OUT: Return data in this Mem structure. */
11550 ){
11551 char *zData; /* Data from the btree layer */
11552 u32 available = 0; /* Number of bytes available on the local btree page */
11553 int rc = SQLITE_OK; /* Return code */
11554
11555 assert( sqlite3BtreeCursorIsValid(pCur) );
11556 assert( !VdbeMemDynamic(pMem) );
11557
11558 /* Note: the calls to BtreeKeyFetch() and DataFetch() below assert()
11559 ** that both the BtShared and database handle mutexes are held. */
11560 assert( (pMem->flags & MEM_RowSet)==0 );
11561 zData = (char *)sqlite3BtreePayloadFetch(pCur, &available);
11562 assert( zData!=0 );
11563
11564 if( offset+amt<=available ){
11565 pMem->z = &zData[offset];
11566 pMem->flags = MEM_Blob|MEM_Ephem;
11567 pMem->n = (int)amt;
11568 }else{
11569 rc = vdbeMemFromBtreeResize(pCur, offset, amt, pMem);
11570 }
11571
11572 return rc;
11573 }
11574
11575 /*
11576 ** The pVal argument is known to be a value other than NULL.
11577 ** Convert it into a string with encoding enc and return a pointer
11578 ** to a zero-terminated version of that string.
11579 */
11580 static SQLITE_NOINLINE const void *valueToText(sqlite3_value* pVal, u8 enc){
11581 assert( pVal!=0 );
11582 assert( pVal->db==0 || sqlite3_mutex_held(pVal->db->mutex) );
11583 assert( (enc&3)==(enc&~SQLITE_UTF16_ALIGNED) );
11584 assert( (pVal->flags & MEM_RowSet)==0 );
11585 assert( (pVal->flags & (MEM_Null))==0 );
11586 if( pVal->flags & (MEM_Blob|MEM_Str) ){
11587 if( ExpandBlob(pVal) ) return 0;
11588 pVal->flags |= MEM_Str;
11589 if( pVal->enc != (enc & ~SQLITE_UTF16_ALIGNED) ){
11590 sqlite3VdbeChangeEncoding(pVal, enc & ~SQLITE_UTF16_ALIGNED);
11591 }
11592 if( (enc & SQLITE_UTF16_ALIGNED)!=0 && 1==(1&SQLITE_PTR_TO_INT(pVal->z)) ){
11593 assert( (pVal->flags & (MEM_Ephem|MEM_Static))!=0 );
11594 if( sqlite3VdbeMemMakeWriteable(pVal)!=SQLITE_OK ){
11595 return 0;
11596 }
11597 }
11598 sqlite3VdbeMemNulTerminate(pVal); /* IMP: R-31275-44060 */
11599 }else{
11600 sqlite3VdbeMemStringify(pVal, enc, 0);
11601 assert( 0==(1&SQLITE_PTR_TO_INT(pVal->z)) );
11602 }
11603 assert(pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) || pVal->db==0
11604 || pVal->db->mallocFailed );
11605 if( pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) ){
11606 return pVal->z;
11607 }else{
11608 return 0;
11609 }
11610 }
11611
11612 /* This function is only available internally, it is not part of the
11613 ** external API. It works in a similar way to sqlite3_value_text(),
11614 ** except the data returned is in the encoding specified by the second
11615 ** parameter, which must be one of SQLITE_UTF16BE, SQLITE_UTF16LE or
11616 ** SQLITE_UTF8.
11617 **
11618 ** (2006-02-16:) The enc value can be or-ed with SQLITE_UTF16_ALIGNED.
11619 ** If that is the case, then the result must be aligned on an even byte
11620 ** boundary.
11621 */
11622 SQLITE_PRIVATE const void *sqlite3ValueText(sqlite3_value* pVal, u8 enc){
11623 if( !pVal ) return 0;
11624 assert( pVal->db==0 || sqlite3_mutex_held(pVal->db->mutex) );
11625 assert( (enc&3)==(enc&~SQLITE_UTF16_ALIGNED) );
11626 assert( (pVal->flags & MEM_RowSet)==0 );
11627 if( (pVal->flags&(MEM_Str|MEM_Term))==(MEM_Str|MEM_Term) && pVal->enc==enc ){
11628 return pVal->z;
11629 }
11630 if( pVal->flags&MEM_Null ){
11631 return 0;
11632 }
11633 return valueToText(pVal, enc);
11634 }
11635
11636 /*
11637 ** Create a new sqlite3_value object.
11638 */
11639 SQLITE_PRIVATE sqlite3_value *sqlite3ValueNew(sqlite3 *db){
11640 Mem *p = sqlite3DbMallocZero(db, sizeof(*p));
11641 if( p ){
11642 p->flags = MEM_Null;
11643 p->db = db;
11644 }
11645 return p;
11646 }
11647
11648 /*
11649 ** Context object passed by sqlite3Stat4ProbeSetValue() through to
11650 ** valueNew(). See comments above valueNew() for details.
11651 */
11652 struct ValueNewStat4Ctx {
11653 Parse *pParse;
11654 Index *pIdx;
11655 UnpackedRecord **ppRec;
11656 int iVal;
11657 };
11658
11659 /*
11660 ** Allocate and return a pointer to a new sqlite3_value object. If
11661 ** the second argument to this function is NULL, the object is allocated
11662 ** by calling sqlite3ValueNew().
11663 **
11664 ** Otherwise, if the second argument is non-zero, then this function is
11665 ** being called indirectly by sqlite3Stat4ProbeSetValue(). If it has not
11666 ** already been allocated, allocate the UnpackedRecord structure that
11667 ** that function will return to its caller here. Then return a pointer to
11668 ** an sqlite3_value within the UnpackedRecord.a[] array.
11669 */
11670 static sqlite3_value *valueNew(sqlite3 *db, struct ValueNewStat4Ctx *p){
11671 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
11672 if( p ){
11673 UnpackedRecord *pRec = p->ppRec[0];
11674
11675 if( pRec==0 ){
11676 Index *pIdx = p->pIdx; /* Index being probed */
11677 int nByte; /* Bytes of space to allocate */
11678 int i; /* Counter variable */
11679 int nCol = pIdx->nColumn; /* Number of index columns including rowid */
11680
11681 nByte = sizeof(Mem) * nCol + ROUND8(sizeof(UnpackedRecord));
11682 pRec = (UnpackedRecord*)sqlite3DbMallocZero(db, nByte);
11683 if( pRec ){
11684 pRec->pKeyInfo = sqlite3KeyInfoOfIndex(p->pParse, pIdx);
11685 if( pRec->pKeyInfo ){
11686 assert( pRec->pKeyInfo->nField+pRec->pKeyInfo->nXField==nCol );
11687 assert( pRec->pKeyInfo->enc==ENC(db) );
11688 pRec->aMem = (Mem *)((u8*)pRec + ROUND8(sizeof(UnpackedRecord)));
11689 for(i=0; i<nCol; i++){
11690 pRec->aMem[i].flags = MEM_Null;
11691 pRec->aMem[i].db = db;
11692 }
11693 }else{
11694 sqlite3DbFree(db, pRec);
11695 pRec = 0;
11696 }
11697 }
11698 if( pRec==0 ) return 0;
11699 p->ppRec[0] = pRec;
11700 }
11701
11702 pRec->nField = p->iVal+1;
11703 return &pRec->aMem[p->iVal];
11704 }
11705 #else
11706 UNUSED_PARAMETER(p);
11707 #endif /* defined(SQLITE_ENABLE_STAT3_OR_STAT4) */
11708 return sqlite3ValueNew(db);
11709 }
11710
11711 /*
11712 ** The expression object indicated by the second argument is guaranteed
11713 ** to be a scalar SQL function. If
11714 **
11715 ** * all function arguments are SQL literals,
11716 ** * one of the SQLITE_FUNC_CONSTANT or _SLOCHNG function flags is set, and
11717 ** * the SQLITE_FUNC_NEEDCOLL function flag is not set,
11718 **
11719 ** then this routine attempts to invoke the SQL function. Assuming no
11720 ** error occurs, output parameter (*ppVal) is set to point to a value
11721 ** object containing the result before returning SQLITE_OK.
11722 **
11723 ** Affinity aff is applied to the result of the function before returning.
11724 ** If the result is a text value, the sqlite3_value object uses encoding
11725 ** enc.
11726 **
11727 ** If the conditions above are not met, this function returns SQLITE_OK
11728 ** and sets (*ppVal) to NULL. Or, if an error occurs, (*ppVal) is set to
11729 ** NULL and an SQLite error code returned.
11730 */
11731 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
11732 static int valueFromFunction(
11733 sqlite3 *db, /* The database connection */
11734 Expr *p, /* The expression to evaluate */
11735 u8 enc, /* Encoding to use */
11736 u8 aff, /* Affinity to use */
11737 sqlite3_value **ppVal, /* Write the new value here */
11738 struct ValueNewStat4Ctx *pCtx /* Second argument for valueNew() */
11739 ){
11740 sqlite3_context ctx; /* Context object for function invocation */
11741 sqlite3_value **apVal = 0; /* Function arguments */
11742 int nVal = 0; /* Size of apVal[] array */
11743 FuncDef *pFunc = 0; /* Function definition */
11744 sqlite3_value *pVal = 0; /* New value */
11745 int rc = SQLITE_OK; /* Return code */
11746 ExprList *pList = 0; /* Function arguments */
11747 int i; /* Iterator variable */
11748
11749 assert( pCtx!=0 );
11750 assert( (p->flags & EP_TokenOnly)==0 );
11751 pList = p->x.pList;
11752 if( pList ) nVal = pList->nExpr;
11753 pFunc = sqlite3FindFunction(db, p->u.zToken, nVal, enc, 0);
11754 assert( pFunc );
11755 if( (pFunc->funcFlags & (SQLITE_FUNC_CONSTANT|SQLITE_FUNC_SLOCHNG))==0
11756 || (pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL)
11757 ){
11758 return SQLITE_OK;
11759 }
11760
11761 if( pList ){
11762 apVal = (sqlite3_value**)sqlite3DbMallocZero(db, sizeof(apVal[0]) * nVal);
11763 if( apVal==0 ){
11764 rc = SQLITE_NOMEM_BKPT;
11765 goto value_from_function_out;
11766 }
11767 for(i=0; i<nVal; i++){
11768 rc = sqlite3ValueFromExpr(db, pList->a[i].pExpr, enc, aff, &apVal[i]);
11769 if( apVal[i]==0 || rc!=SQLITE_OK ) goto value_from_function_out;
11770 }
11771 }
11772
11773 pVal = valueNew(db, pCtx);
11774 if( pVal==0 ){
11775 rc = SQLITE_NOMEM_BKPT;
11776 goto value_from_function_out;
11777 }
11778
11779 assert( pCtx->pParse->rc==SQLITE_OK );
11780 memset(&ctx, 0, sizeof(ctx));
11781 ctx.pOut = pVal;
11782 ctx.pFunc = pFunc;
11783 pFunc->xSFunc(&ctx, nVal, apVal);
11784 if( ctx.isError ){
11785 rc = ctx.isError;
11786 sqlite3ErrorMsg(pCtx->pParse, "%s", sqlite3_value_text(pVal));
11787 }else{
11788 sqlite3ValueApplyAffinity(pVal, aff, SQLITE_UTF8);
11789 assert( rc==SQLITE_OK );
11790 rc = sqlite3VdbeChangeEncoding(pVal, enc);
11791 if( rc==SQLITE_OK && sqlite3VdbeMemTooBig(pVal) ){
11792 rc = SQLITE_TOOBIG;
11793 pCtx->pParse->nErr++;
11794 }
11795 }
11796 pCtx->pParse->rc = rc;
11797
11798 value_from_function_out:
11799 if( rc!=SQLITE_OK ){
11800 pVal = 0;
11801 }
11802 if( apVal ){
11803 for(i=0; i<nVal; i++){
11804 sqlite3ValueFree(apVal[i]);
11805 }
11806 sqlite3DbFree(db, apVal);
11807 }
11808
11809 *ppVal = pVal;
11810 return rc;
11811 }
11812 #else
11813 # define valueFromFunction(a,b,c,d,e,f) SQLITE_OK
11814 #endif /* defined(SQLITE_ENABLE_STAT3_OR_STAT4) */
11815
11816 /*
11817 ** Extract a value from the supplied expression in the manner described
11818 ** above sqlite3ValueFromExpr(). Allocate the sqlite3_value object
11819 ** using valueNew().
11820 **
11821 ** If pCtx is NULL and an error occurs after the sqlite3_value object
11822 ** has been allocated, it is freed before returning. Or, if pCtx is not
11823 ** NULL, it is assumed that the caller will free any allocated object
11824 ** in all cases.
11825 */
11826 static int valueFromExpr(
11827 sqlite3 *db, /* The database connection */
11828 Expr *pExpr, /* The expression to evaluate */
11829 u8 enc, /* Encoding to use */
11830 u8 affinity, /* Affinity to use */
11831 sqlite3_value **ppVal, /* Write the new value here */
11832 struct ValueNewStat4Ctx *pCtx /* Second argument for valueNew() */
11833 ){
11834 int op;
11835 char *zVal = 0;
11836 sqlite3_value *pVal = 0;
11837 int negInt = 1;
11838 const char *zNeg = "";
11839 int rc = SQLITE_OK;
11840
11841 assert( pExpr!=0 );
11842 while( (op = pExpr->op)==TK_UPLUS || op==TK_SPAN ) pExpr = pExpr->pLeft;
11843 if( NEVER(op==TK_REGISTER) ) op = pExpr->op2;
11844
11845 /* Compressed expressions only appear when parsing the DEFAULT clause
11846 ** on a table column definition, and hence only when pCtx==0. This
11847 ** check ensures that an EP_TokenOnly expression is never passed down
11848 ** into valueFromFunction(). */
11849 assert( (pExpr->flags & EP_TokenOnly)==0 || pCtx==0 );
11850
11851 if( op==TK_CAST ){
11852 u8 aff = sqlite3AffinityType(pExpr->u.zToken,0);
11853 rc = valueFromExpr(db, pExpr->pLeft, enc, aff, ppVal, pCtx);
11854 testcase( rc!=SQLITE_OK );
11855 if( *ppVal ){
11856 sqlite3VdbeMemCast(*ppVal, aff, SQLITE_UTF8);
11857 sqlite3ValueApplyAffinity(*ppVal, affinity, SQLITE_UTF8);
11858 }
11859 return rc;
11860 }
11861
11862 /* Handle negative integers in a single step. This is needed in the
11863 ** case when the value is -9223372036854775808.
11864 */
11865 if( op==TK_UMINUS
11866 && (pExpr->pLeft->op==TK_INTEGER || pExpr->pLeft->op==TK_FLOAT) ){
11867 pExpr = pExpr->pLeft;
11868 op = pExpr->op;
11869 negInt = -1;
11870 zNeg = "-";
11871 }
11872
11873 if( op==TK_STRING || op==TK_FLOAT || op==TK_INTEGER ){
11874 pVal = valueNew(db, pCtx);
11875 if( pVal==0 ) goto no_mem;
11876 if( ExprHasProperty(pExpr, EP_IntValue) ){
11877 sqlite3VdbeMemSetInt64(pVal, (i64)pExpr->u.iValue*negInt);
11878 }else{
11879 zVal = sqlite3MPrintf(db, "%s%s", zNeg, pExpr->u.zToken);
11880 if( zVal==0 ) goto no_mem;
11881 sqlite3ValueSetStr(pVal, -1, zVal, SQLITE_UTF8, SQLITE_DYNAMIC);
11882 }
11883 if( (op==TK_INTEGER || op==TK_FLOAT ) && affinity==SQLITE_AFF_BLOB ){
11884 sqlite3ValueApplyAffinity(pVal, SQLITE_AFF_NUMERIC, SQLITE_UTF8);
11885 }else{
11886 sqlite3ValueApplyAffinity(pVal, affinity, SQLITE_UTF8);
11887 }
11888 if( pVal->flags & (MEM_Int|MEM_Real) ) pVal->flags &= ~MEM_Str;
11889 if( enc!=SQLITE_UTF8 ){
11890 rc = sqlite3VdbeChangeEncoding(pVal, enc);
11891 }
11892 }else if( op==TK_UMINUS ) {
11893 /* This branch happens for multiple negative signs. Ex: -(-5) */
11894 if( SQLITE_OK==sqlite3ValueFromExpr(db,pExpr->pLeft,enc,affinity,&pVal)
11895 && pVal!=0
11896 ){
11897 sqlite3VdbeMemNumerify(pVal);
11898 if( pVal->flags & MEM_Real ){
11899 pVal->u.r = -pVal->u.r;
11900 }else if( pVal->u.i==SMALLEST_INT64 ){
11901 pVal->u.r = -(double)SMALLEST_INT64;
11902 MemSetTypeFlag(pVal, MEM_Real);
11903 }else{
11904 pVal->u.i = -pVal->u.i;
11905 }
11906 sqlite3ValueApplyAffinity(pVal, affinity, enc);
11907 }
11908 }else if( op==TK_NULL ){
11909 pVal = valueNew(db, pCtx);
11910 if( pVal==0 ) goto no_mem;
11911 sqlite3VdbeMemNumerify(pVal);
11912 }
11913 #ifndef SQLITE_OMIT_BLOB_LITERAL
11914 else if( op==TK_BLOB ){
11915 int nVal;
11916 assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' );
11917 assert( pExpr->u.zToken[1]=='\'' );
11918 pVal = valueNew(db, pCtx);
11919 if( !pVal ) goto no_mem;
11920 zVal = &pExpr->u.zToken[2];
11921 nVal = sqlite3Strlen30(zVal)-1;
11922 assert( zVal[nVal]=='\'' );
11923 sqlite3VdbeMemSetStr(pVal, sqlite3HexToBlob(db, zVal, nVal), nVal/2,
11924 0, SQLITE_DYNAMIC);
11925 }
11926 #endif
11927
11928 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
11929 else if( op==TK_FUNCTION && pCtx!=0 ){
11930 rc = valueFromFunction(db, pExpr, enc, affinity, &pVal, pCtx);
11931 }
11932 #endif
11933
11934 *ppVal = pVal;
11935 return rc;
11936
11937 no_mem:
11938 sqlite3OomFault(db);
11939 sqlite3DbFree(db, zVal);
11940 assert( *ppVal==0 );
11941 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
11942 if( pCtx==0 ) sqlite3ValueFree(pVal);
11943 #else
11944 assert( pCtx==0 ); sqlite3ValueFree(pVal);
11945 #endif
11946 return SQLITE_NOMEM_BKPT;
11947 }
11948
11949 /*
11950 ** Create a new sqlite3_value object, containing the value of pExpr.
11951 **
11952 ** This only works for very simple expressions that consist of one constant
11953 ** token (i.e. "5", "5.1", "'a string'"). If the expression can
11954 ** be converted directly into a value, then the value is allocated and
11955 ** a pointer written to *ppVal. The caller is responsible for deallocating
11956 ** the value by passing it to sqlite3ValueFree() later on. If the expression
11957 ** cannot be converted to a value, then *ppVal is set to NULL.
11958 */
11959 SQLITE_PRIVATE int sqlite3ValueFromExpr(
11960 sqlite3 *db, /* The database connection */
11961 Expr *pExpr, /* The expression to evaluate */
11962 u8 enc, /* Encoding to use */
11963 u8 affinity, /* Affinity to use */
11964 sqlite3_value **ppVal /* Write the new value here */
11965 ){
11966 return pExpr ? valueFromExpr(db, pExpr, enc, affinity, ppVal, 0) : 0;
11967 }
11968
11969 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
11970 /*
11971 ** The implementation of the sqlite_record() function. This function accepts
11972 ** a single argument of any type. The return value is a formatted database
11973 ** record (a blob) containing the argument value.
11974 **
11975 ** This is used to convert the value stored in the 'sample' column of the
11976 ** sqlite_stat3 table to the record format SQLite uses internally.
11977 */
11978 static void recordFunc(
11979 sqlite3_context *context,
11980 int argc,
11981 sqlite3_value **argv
11982 ){
11983 const int file_format = 1;
11984 u32 iSerial; /* Serial type */
11985 int nSerial; /* Bytes of space for iSerial as varint */
11986 u32 nVal; /* Bytes of space required for argv[0] */
11987 int nRet;
11988 sqlite3 *db;
11989 u8 *aRet;
11990
11991 UNUSED_PARAMETER( argc );
11992 iSerial = sqlite3VdbeSerialType(argv[0], file_format, &nVal);
11993 nSerial = sqlite3VarintLen(iSerial);
11994 db = sqlite3_context_db_handle(context);
11995
11996 nRet = 1 + nSerial + nVal;
11997 aRet = sqlite3DbMallocRawNN(db, nRet);
11998 if( aRet==0 ){
11999 sqlite3_result_error_nomem(context);
12000 }else{
12001 aRet[0] = nSerial+1;
12002 putVarint32(&aRet[1], iSerial);
12003 sqlite3VdbeSerialPut(&aRet[1+nSerial], argv[0], iSerial);
12004 sqlite3_result_blob(context, aRet, nRet, SQLITE_TRANSIENT);
12005 sqlite3DbFree(db, aRet);
12006 }
12007 }
12008
12009 /*
12010 ** Register built-in functions used to help read ANALYZE data.
12011 */
12012 SQLITE_PRIVATE void sqlite3AnalyzeFunctions(void){
12013 static FuncDef aAnalyzeTableFuncs[] = {
12014 FUNCTION(sqlite_record, 1, 0, 0, recordFunc),
12015 };
12016 sqlite3InsertBuiltinFuncs(aAnalyzeTableFuncs, ArraySize(aAnalyzeTableFuncs));
12017 }
12018
12019 /*
12020 ** Attempt to extract a value from pExpr and use it to construct *ppVal.
12021 **
12022 ** If pAlloc is not NULL, then an UnpackedRecord object is created for
12023 ** pAlloc if one does not exist and the new value is added to the
12024 ** UnpackedRecord object.
12025 **
12026 ** A value is extracted in the following cases:
12027 **
12028 ** * (pExpr==0). In this case the value is assumed to be an SQL NULL,
12029 **
12030 ** * The expression is a bound variable, and this is a reprepare, or
12031 **
12032 ** * The expression is a literal value.
12033 **
12034 ** On success, *ppVal is made to point to the extracted value. The caller
12035 ** is responsible for ensuring that the value is eventually freed.
12036 */
12037 static int stat4ValueFromExpr(
12038 Parse *pParse, /* Parse context */
12039 Expr *pExpr, /* The expression to extract a value from */
12040 u8 affinity, /* Affinity to use */
12041 struct ValueNewStat4Ctx *pAlloc,/* How to allocate space. Or NULL */
12042 sqlite3_value **ppVal /* OUT: New value object (or NULL) */
12043 ){
12044 int rc = SQLITE_OK;
12045 sqlite3_value *pVal = 0;
12046 sqlite3 *db = pParse->db;
12047
12048 /* Skip over any TK_COLLATE nodes */
12049 pExpr = sqlite3ExprSkipCollate(pExpr);
12050
12051 if( !pExpr ){
12052 pVal = valueNew(db, pAlloc);
12053 if( pVal ){
12054 sqlite3VdbeMemSetNull((Mem*)pVal);
12055 }
12056 }else if( pExpr->op==TK_VARIABLE
12057 || NEVER(pExpr->op==TK_REGISTER && pExpr->op2==TK_VARIABLE)
12058 ){
12059 Vdbe *v;
12060 int iBindVar = pExpr->iColumn;
12061 sqlite3VdbeSetVarmask(pParse->pVdbe, iBindVar);
12062 if( (v = pParse->pReprepare)!=0 ){
12063 pVal = valueNew(db, pAlloc);
12064 if( pVal ){
12065 rc = sqlite3VdbeMemCopy((Mem*)pVal, &v->aVar[iBindVar-1]);
12066 if( rc==SQLITE_OK ){
12067 sqlite3ValueApplyAffinity(pVal, affinity, ENC(db));
12068 }
12069 pVal->db = pParse->db;
12070 }
12071 }
12072 }else{
12073 rc = valueFromExpr(db, pExpr, ENC(db), affinity, &pVal, pAlloc);
12074 }
12075
12076 assert( pVal==0 || pVal->db==db );
12077 *ppVal = pVal;
12078 return rc;
12079 }
12080
12081 /*
12082 ** This function is used to allocate and populate UnpackedRecord
12083 ** structures intended to be compared against sample index keys stored
12084 ** in the sqlite_stat4 table.
12085 **
12086 ** A single call to this function populates zero or more fields of the
12087 ** record starting with field iVal (fields are numbered from left to
12088 ** right starting with 0). A single field is populated if:
12089 **
12090 ** * (pExpr==0). In this case the value is assumed to be an SQL NULL,
12091 **
12092 ** * The expression is a bound variable, and this is a reprepare, or
12093 **
12094 ** * The sqlite3ValueFromExpr() function is able to extract a value
12095 ** from the expression (i.e. the expression is a literal value).
12096 **
12097 ** Or, if pExpr is a TK_VECTOR, one field is populated for each of the
12098 ** vector components that match either of the two latter criteria listed
12099 ** above.
12100 **
12101 ** Before any value is appended to the record, the affinity of the
12102 ** corresponding column within index pIdx is applied to it. Before
12103 ** this function returns, output parameter *pnExtract is set to the
12104 ** number of values appended to the record.
12105 **
12106 ** When this function is called, *ppRec must either point to an object
12107 ** allocated by an earlier call to this function, or must be NULL. If it
12108 ** is NULL and a value can be successfully extracted, a new UnpackedRecord
12109 ** is allocated (and *ppRec set to point to it) before returning.
12110 **
12111 ** Unless an error is encountered, SQLITE_OK is returned. It is not an
12112 ** error if a value cannot be extracted from pExpr. If an error does
12113 ** occur, an SQLite error code is returned.
12114 */
12115 SQLITE_PRIVATE int sqlite3Stat4ProbeSetValue(
12116 Parse *pParse, /* Parse context */
12117 Index *pIdx, /* Index being probed */
12118 UnpackedRecord **ppRec, /* IN/OUT: Probe record */
12119 Expr *pExpr, /* The expression to extract a value from */
12120 int nElem, /* Maximum number of values to append */
12121 int iVal, /* Array element to populate */
12122 int *pnExtract /* OUT: Values appended to the record */
12123 ){
12124 int rc = SQLITE_OK;
12125 int nExtract = 0;
12126
12127 if( pExpr==0 || pExpr->op!=TK_SELECT ){
12128 int i;
12129 struct ValueNewStat4Ctx alloc;
12130
12131 alloc.pParse = pParse;
12132 alloc.pIdx = pIdx;
12133 alloc.ppRec = ppRec;
12134
12135 for(i=0; i<nElem; i++){
12136 sqlite3_value *pVal = 0;
12137 Expr *pElem = (pExpr ? sqlite3VectorFieldSubexpr(pExpr, i) : 0);
12138 u8 aff = sqlite3IndexColumnAffinity(pParse->db, pIdx, iVal+i);
12139 alloc.iVal = iVal+i;
12140 rc = stat4ValueFromExpr(pParse, pElem, aff, &alloc, &pVal);
12141 if( !pVal ) break;
12142 nExtract++;
12143 }
12144 }
12145
12146 *pnExtract = nExtract;
12147 return rc;
12148 }
12149
12150 /*
12151 ** Attempt to extract a value from expression pExpr using the methods
12152 ** as described for sqlite3Stat4ProbeSetValue() above.
12153 **
12154 ** If successful, set *ppVal to point to a new value object and return
12155 ** SQLITE_OK. If no value can be extracted, but no other error occurs
12156 ** (e.g. OOM), return SQLITE_OK and set *ppVal to NULL. Or, if an error
12157 ** does occur, return an SQLite error code. The final value of *ppVal
12158 ** is undefined in this case.
12159 */
12160 SQLITE_PRIVATE int sqlite3Stat4ValueFromExpr(
12161 Parse *pParse, /* Parse context */
12162 Expr *pExpr, /* The expression to extract a value from */
12163 u8 affinity, /* Affinity to use */
12164 sqlite3_value **ppVal /* OUT: New value object (or NULL) */
12165 ){
12166 return stat4ValueFromExpr(pParse, pExpr, affinity, 0, ppVal);
12167 }
12168
12169 /*
12170 ** Extract the iCol-th column from the nRec-byte record in pRec. Write
12171 ** the column value into *ppVal. If *ppVal is initially NULL then a new
12172 ** sqlite3_value object is allocated.
12173 **
12174 ** If *ppVal is initially NULL then the caller is responsible for
12175 ** ensuring that the value written into *ppVal is eventually freed.
12176 */
12177 SQLITE_PRIVATE int sqlite3Stat4Column(
12178 sqlite3 *db, /* Database handle */
12179 const void *pRec, /* Pointer to buffer containing record */
12180 int nRec, /* Size of buffer pRec in bytes */
12181 int iCol, /* Column to extract */
12182 sqlite3_value **ppVal /* OUT: Extracted value */
12183 ){
12184 u32 t; /* a column type code */
12185 int nHdr; /* Size of the header in the record */
12186 int iHdr; /* Next unread header byte */
12187 int iField; /* Next unread data byte */
12188 int szField; /* Size of the current data field */
12189 int i; /* Column index */
12190 u8 *a = (u8*)pRec; /* Typecast byte array */
12191 Mem *pMem = *ppVal; /* Write result into this Mem object */
12192
12193 assert( iCol>0 );
12194 iHdr = getVarint32(a, nHdr);
12195 if( nHdr>nRec || iHdr>=nHdr ) return SQLITE_CORRUPT_BKPT;
12196 iField = nHdr;
12197 for(i=0; i<=iCol; i++){
12198 iHdr += getVarint32(&a[iHdr], t);
12199 testcase( iHdr==nHdr );
12200 testcase( iHdr==nHdr+1 );
12201 if( iHdr>nHdr ) return SQLITE_CORRUPT_BKPT;
12202 szField = sqlite3VdbeSerialTypeLen(t);
12203 iField += szField;
12204 }
12205 testcase( iField==nRec );
12206 testcase( iField==nRec+1 );
12207 if( iField>nRec ) return SQLITE_CORRUPT_BKPT;
12208 if( pMem==0 ){
12209 pMem = *ppVal = sqlite3ValueNew(db);
12210 if( pMem==0 ) return SQLITE_NOMEM_BKPT;
12211 }
12212 sqlite3VdbeSerialGet(&a[iField-szField], t, pMem);
12213 pMem->enc = ENC(db);
12214 return SQLITE_OK;
12215 }
12216
12217 /*
12218 ** Unless it is NULL, the argument must be an UnpackedRecord object returned
12219 ** by an earlier call to sqlite3Stat4ProbeSetValue(). This call deletes
12220 ** the object.
12221 */
12222 SQLITE_PRIVATE void sqlite3Stat4ProbeFree(UnpackedRecord *pRec){
12223 if( pRec ){
12224 int i;
12225 int nCol = pRec->pKeyInfo->nField+pRec->pKeyInfo->nXField;
12226 Mem *aMem = pRec->aMem;
12227 sqlite3 *db = aMem[0].db;
12228 for(i=0; i<nCol; i++){
12229 sqlite3VdbeMemRelease(&aMem[i]);
12230 }
12231 sqlite3KeyInfoUnref(pRec->pKeyInfo);
12232 sqlite3DbFree(db, pRec);
12233 }
12234 }
12235 #endif /* ifdef SQLITE_ENABLE_STAT4 */
12236
12237 /*
12238 ** Change the string value of an sqlite3_value object
12239 */
12240 SQLITE_PRIVATE void sqlite3ValueSetStr(
12241 sqlite3_value *v, /* Value to be set */
12242 int n, /* Length of string z */
12243 const void *z, /* Text of the new string */
12244 u8 enc, /* Encoding to use */
12245 void (*xDel)(void*) /* Destructor for the string */
12246 ){
12247 if( v ) sqlite3VdbeMemSetStr((Mem *)v, z, n, enc, xDel);
12248 }
12249
12250 /*
12251 ** Free an sqlite3_value object
12252 */
12253 SQLITE_PRIVATE void sqlite3ValueFree(sqlite3_value *v){
12254 if( !v ) return;
12255 sqlite3VdbeMemRelease((Mem *)v);
12256 sqlite3DbFree(((Mem*)v)->db, v);
12257 }
12258
12259 /*
12260 ** The sqlite3ValueBytes() routine returns the number of bytes in the
12261 ** sqlite3_value object assuming that it uses the encoding "enc".
12262 ** The valueBytes() routine is a helper function.
12263 */
12264 static SQLITE_NOINLINE int valueBytes(sqlite3_value *pVal, u8 enc){
12265 return valueToText(pVal, enc)!=0 ? pVal->n : 0;
12266 }
12267 SQLITE_PRIVATE int sqlite3ValueBytes(sqlite3_value *pVal, u8 enc){
12268 Mem *p = (Mem*)pVal;
12269 assert( (p->flags & MEM_Null)==0 || (p->flags & (MEM_Str|MEM_Blob))==0 );
12270 if( (p->flags & MEM_Str)!=0 && pVal->enc==enc ){
12271 return p->n;
12272 }
12273 if( (p->flags & MEM_Blob)!=0 ){
12274 if( p->flags & MEM_Zero ){
12275 return p->n + p->u.nZero;
12276 }else{
12277 return p->n;
12278 }
12279 }
12280 if( p->flags & MEM_Null ) return 0;
12281 return valueBytes(pVal, enc);
12282 }
12283
12284 /************** End of vdbemem.c *********************************************/
12285 /************** Begin file vdbeaux.c *****************************************/
12286 /*
12287 ** 2003 September 6
12288 **
12289 ** The author disclaims copyright to this source code. In place of
12290 ** a legal notice, here is a blessing:
12291 **
12292 ** May you do good and not evil.
12293 ** May you find forgiveness for yourself and forgive others.
12294 ** May you share freely, never taking more than you give.
12295 **
12296 *************************************************************************
12297 ** This file contains code used for creating, destroying, and populating
12298 ** a VDBE (or an "sqlite3_stmt" as it is known to the outside world.)
12299 */
12300 /* #include "sqliteInt.h" */
12301 /* #include "vdbeInt.h" */
12302
12303 /*
12304 ** Create a new virtual database engine.
12305 */
12306 SQLITE_PRIVATE Vdbe *sqlite3VdbeCreate(Parse *pParse){
12307 sqlite3 *db = pParse->db;
12308 Vdbe *p;
12309 p = sqlite3DbMallocRawNN(db, sizeof(Vdbe) );
12310 if( p==0 ) return 0;
12311 memset(&p->aOp, 0, sizeof(Vdbe)-offsetof(Vdbe,aOp));
12312 p->db = db;
12313 if( db->pVdbe ){
12314 db->pVdbe->pPrev = p;
12315 }
12316 p->pNext = db->pVdbe;
12317 p->pPrev = 0;
12318 db->pVdbe = p;
12319 p->magic = VDBE_MAGIC_INIT;
12320 p->pParse = pParse;
12321 assert( pParse->aLabel==0 );
12322 assert( pParse->nLabel==0 );
12323 assert( pParse->nOpAlloc==0 );
12324 assert( pParse->szOpAlloc==0 );
12325 return p;
12326 }
12327
12328 /*
12329 ** Change the error string stored in Vdbe.zErrMsg
12330 */
12331 SQLITE_PRIVATE void sqlite3VdbeError(Vdbe *p, const char *zFormat, ...){
12332 va_list ap;
12333 sqlite3DbFree(p->db, p->zErrMsg);
12334 va_start(ap, zFormat);
12335 p->zErrMsg = sqlite3VMPrintf(p->db, zFormat, ap);
12336 va_end(ap);
12337 }
12338
12339 /*
12340 ** Remember the SQL string for a prepared statement.
12341 */
12342 SQLITE_PRIVATE void sqlite3VdbeSetSql(Vdbe *p, const char *z, int n, int isPrepa reV2){
12343 assert( isPrepareV2==1 || isPrepareV2==0 );
12344 if( p==0 ) return;
12345 #if defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_ENABLE_SQLLOG)
12346 if( !isPrepareV2 ) return;
12347 #endif
12348 assert( p->zSql==0 );
12349 p->zSql = sqlite3DbStrNDup(p->db, z, n);
12350 p->isPrepareV2 = (u8)isPrepareV2;
12351 }
12352
12353 /*
12354 ** Swap all content between two VDBE structures.
12355 */
12356 SQLITE_PRIVATE void sqlite3VdbeSwap(Vdbe *pA, Vdbe *pB){
12357 Vdbe tmp, *pTmp;
12358 char *zTmp;
12359 assert( pA->db==pB->db );
12360 tmp = *pA;
12361 *pA = *pB;
12362 *pB = tmp;
12363 pTmp = pA->pNext;
12364 pA->pNext = pB->pNext;
12365 pB->pNext = pTmp;
12366 pTmp = pA->pPrev;
12367 pA->pPrev = pB->pPrev;
12368 pB->pPrev = pTmp;
12369 zTmp = pA->zSql;
12370 pA->zSql = pB->zSql;
12371 pB->zSql = zTmp;
12372 pB->isPrepareV2 = pA->isPrepareV2;
12373 }
12374
12375 /*
12376 ** Resize the Vdbe.aOp array so that it is at least nOp elements larger
12377 ** than its current size. nOp is guaranteed to be less than or equal
12378 ** to 1024/sizeof(Op).
12379 **
12380 ** If an out-of-memory error occurs while resizing the array, return
12381 ** SQLITE_NOMEM. In this case Vdbe.aOp and Parse.nOpAlloc remain
12382 ** unchanged (this is so that any opcodes already allocated can be
12383 ** correctly deallocated along with the rest of the Vdbe).
12384 */
12385 static int growOpArray(Vdbe *v, int nOp){
12386 VdbeOp *pNew;
12387 Parse *p = v->pParse;
12388
12389 /* The SQLITE_TEST_REALLOC_STRESS compile-time option is designed to force
12390 ** more frequent reallocs and hence provide more opportunities for
12391 ** simulated OOM faults. SQLITE_TEST_REALLOC_STRESS is generally used
12392 ** during testing only. With SQLITE_TEST_REALLOC_STRESS grow the op array
12393 ** by the minimum* amount required until the size reaches 512. Normal
12394 ** operation (without SQLITE_TEST_REALLOC_STRESS) is to double the current
12395 ** size of the op array or add 1KB of space, whichever is smaller. */
12396 #ifdef SQLITE_TEST_REALLOC_STRESS
12397 int nNew = (p->nOpAlloc>=512 ? p->nOpAlloc*2 : p->nOpAlloc+nOp);
12398 #else
12399 int nNew = (p->nOpAlloc ? p->nOpAlloc*2 : (int)(1024/sizeof(Op)));
12400 UNUSED_PARAMETER(nOp);
12401 #endif
12402
12403 assert( nOp<=(1024/sizeof(Op)) );
12404 assert( nNew>=(p->nOpAlloc+nOp) );
12405 pNew = sqlite3DbRealloc(p->db, v->aOp, nNew*sizeof(Op));
12406 if( pNew ){
12407 p->szOpAlloc = sqlite3DbMallocSize(p->db, pNew);
12408 p->nOpAlloc = p->szOpAlloc/sizeof(Op);
12409 v->aOp = pNew;
12410 }
12411 return (pNew ? SQLITE_OK : SQLITE_NOMEM_BKPT);
12412 }
12413
12414 #ifdef SQLITE_DEBUG
12415 /* This routine is just a convenient place to set a breakpoint that will
12416 ** fire after each opcode is inserted and displayed using
12417 ** "PRAGMA vdbe_addoptrace=on".
12418 */
12419 static void test_addop_breakpoint(void){
12420 static int n = 0;
12421 n++;
12422 }
12423 #endif
12424
12425 /*
12426 ** Add a new instruction to the list of instructions current in the
12427 ** VDBE. Return the address of the new instruction.
12428 **
12429 ** Parameters:
12430 **
12431 ** p Pointer to the VDBE
12432 **
12433 ** op The opcode for this instruction
12434 **
12435 ** p1, p2, p3 Operands
12436 **
12437 ** Use the sqlite3VdbeResolveLabel() function to fix an address and
12438 ** the sqlite3VdbeChangeP4() function to change the value of the P4
12439 ** operand.
12440 */
12441 static SQLITE_NOINLINE int growOp3(Vdbe *p, int op, int p1, int p2, int p3){
12442 assert( p->pParse->nOpAlloc<=p->nOp );
12443 if( growOpArray(p, 1) ) return 1;
12444 assert( p->pParse->nOpAlloc>p->nOp );
12445 return sqlite3VdbeAddOp3(p, op, p1, p2, p3);
12446 }
12447 SQLITE_PRIVATE int sqlite3VdbeAddOp3(Vdbe *p, int op, int p1, int p2, int p3){
12448 int i;
12449 VdbeOp *pOp;
12450
12451 i = p->nOp;
12452 assert( p->magic==VDBE_MAGIC_INIT );
12453 assert( op>=0 && op<0xff );
12454 if( p->pParse->nOpAlloc<=i ){
12455 return growOp3(p, op, p1, p2, p3);
12456 }
12457 p->nOp++;
12458 pOp = &p->aOp[i];
12459 pOp->opcode = (u8)op;
12460 pOp->p5 = 0;
12461 pOp->p1 = p1;
12462 pOp->p2 = p2;
12463 pOp->p3 = p3;
12464 pOp->p4.p = 0;
12465 pOp->p4type = P4_NOTUSED;
12466 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
12467 pOp->zComment = 0;
12468 #endif
12469 #ifdef SQLITE_DEBUG
12470 if( p->db->flags & SQLITE_VdbeAddopTrace ){
12471 int jj, kk;
12472 Parse *pParse = p->pParse;
12473 for(jj=kk=0; jj<pParse->nColCache; jj++){
12474 struct yColCache *x = pParse->aColCache + jj;
12475 printf(" r[%d]={%d:%d}", x->iReg, x->iTable, x->iColumn);
12476 kk++;
12477 }
12478 if( kk ) printf("\n");
12479 sqlite3VdbePrintOp(0, i, &p->aOp[i]);
12480 test_addop_breakpoint();
12481 }
12482 #endif
12483 #ifdef VDBE_PROFILE
12484 pOp->cycles = 0;
12485 pOp->cnt = 0;
12486 #endif
12487 #ifdef SQLITE_VDBE_COVERAGE
12488 pOp->iSrcLine = 0;
12489 #endif
12490 return i;
12491 }
12492 SQLITE_PRIVATE int sqlite3VdbeAddOp0(Vdbe *p, int op){
12493 return sqlite3VdbeAddOp3(p, op, 0, 0, 0);
12494 }
12495 SQLITE_PRIVATE int sqlite3VdbeAddOp1(Vdbe *p, int op, int p1){
12496 return sqlite3VdbeAddOp3(p, op, p1, 0, 0);
12497 }
12498 SQLITE_PRIVATE int sqlite3VdbeAddOp2(Vdbe *p, int op, int p1, int p2){
12499 return sqlite3VdbeAddOp3(p, op, p1, p2, 0);
12500 }
12501
12502 /* Generate code for an unconditional jump to instruction iDest
12503 */
12504 SQLITE_PRIVATE int sqlite3VdbeGoto(Vdbe *p, int iDest){
12505 return sqlite3VdbeAddOp3(p, OP_Goto, 0, iDest, 0);
12506 }
12507
12508 /* Generate code to cause the string zStr to be loaded into
12509 ** register iDest
12510 */
12511 SQLITE_PRIVATE int sqlite3VdbeLoadString(Vdbe *p, int iDest, const char *zStr){
12512 return sqlite3VdbeAddOp4(p, OP_String8, 0, iDest, 0, zStr, 0);
12513 }
12514
12515 /*
12516 ** Generate code that initializes multiple registers to string or integer
12517 ** constants. The registers begin with iDest and increase consecutively.
12518 ** One register is initialized for each characgter in zTypes[]. For each
12519 ** "s" character in zTypes[], the register is a string if the argument is
12520 ** not NULL, or OP_Null if the value is a null pointer. For each "i" character
12521 ** in zTypes[], the register is initialized to an integer.
12522 */
12523 SQLITE_PRIVATE void sqlite3VdbeMultiLoad(Vdbe *p, int iDest, const char *zTypes, ...){
12524 va_list ap;
12525 int i;
12526 char c;
12527 va_start(ap, zTypes);
12528 for(i=0; (c = zTypes[i])!=0; i++){
12529 if( c=='s' ){
12530 const char *z = va_arg(ap, const char*);
12531 sqlite3VdbeAddOp4(p, z==0 ? OP_Null : OP_String8, 0, iDest++, 0, z, 0);
12532 }else{
12533 assert( c=='i' );
12534 sqlite3VdbeAddOp2(p, OP_Integer, va_arg(ap, int), iDest++);
12535 }
12536 }
12537 va_end(ap);
12538 }
12539
12540 /*
12541 ** Add an opcode that includes the p4 value as a pointer.
12542 */
12543 SQLITE_PRIVATE int sqlite3VdbeAddOp4(
12544 Vdbe *p, /* Add the opcode to this VM */
12545 int op, /* The new opcode */
12546 int p1, /* The P1 operand */
12547 int p2, /* The P2 operand */
12548 int p3, /* The P3 operand */
12549 const char *zP4, /* The P4 operand */
12550 int p4type /* P4 operand type */
12551 ){
12552 int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3);
12553 sqlite3VdbeChangeP4(p, addr, zP4, p4type);
12554 return addr;
12555 }
12556
12557 /*
12558 ** Add an opcode that includes the p4 value with a P4_INT64 or
12559 ** P4_REAL type.
12560 */
12561 SQLITE_PRIVATE int sqlite3VdbeAddOp4Dup8(
12562 Vdbe *p, /* Add the opcode to this VM */
12563 int op, /* The new opcode */
12564 int p1, /* The P1 operand */
12565 int p2, /* The P2 operand */
12566 int p3, /* The P3 operand */
12567 const u8 *zP4, /* The P4 operand */
12568 int p4type /* P4 operand type */
12569 ){
12570 char *p4copy = sqlite3DbMallocRawNN(sqlite3VdbeDb(p), 8);
12571 if( p4copy ) memcpy(p4copy, zP4, 8);
12572 return sqlite3VdbeAddOp4(p, op, p1, p2, p3, p4copy, p4type);
12573 }
12574
12575 /*
12576 ** Add an OP_ParseSchema opcode. This routine is broken out from
12577 ** sqlite3VdbeAddOp4() since it needs to also needs to mark all btrees
12578 ** as having been used.
12579 **
12580 ** The zWhere string must have been obtained from sqlite3_malloc().
12581 ** This routine will take ownership of the allocated memory.
12582 */
12583 SQLITE_PRIVATE void sqlite3VdbeAddParseSchemaOp(Vdbe *p, int iDb, char *zWhere){
12584 int j;
12585 sqlite3VdbeAddOp4(p, OP_ParseSchema, iDb, 0, 0, zWhere, P4_DYNAMIC);
12586 for(j=0; j<p->db->nDb; j++) sqlite3VdbeUsesBtree(p, j);
12587 }
12588
12589 /*
12590 ** Add an opcode that includes the p4 value as an integer.
12591 */
12592 SQLITE_PRIVATE int sqlite3VdbeAddOp4Int(
12593 Vdbe *p, /* Add the opcode to this VM */
12594 int op, /* The new opcode */
12595 int p1, /* The P1 operand */
12596 int p2, /* The P2 operand */
12597 int p3, /* The P3 operand */
12598 int p4 /* The P4 operand as an integer */
12599 ){
12600 int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3);
12601 if( p->db->mallocFailed==0 ){
12602 VdbeOp *pOp = &p->aOp[addr];
12603 pOp->p4type = P4_INT32;
12604 pOp->p4.i = p4;
12605 }
12606 return addr;
12607 }
12608
12609 /* Insert the end of a co-routine
12610 */
12611 SQLITE_PRIVATE void sqlite3VdbeEndCoroutine(Vdbe *v, int regYield){
12612 sqlite3VdbeAddOp1(v, OP_EndCoroutine, regYield);
12613
12614 /* Clear the temporary register cache, thereby ensuring that each
12615 ** co-routine has its own independent set of registers, because co-routines
12616 ** might expect their registers to be preserved across an OP_Yield, and
12617 ** that could cause problems if two or more co-routines are using the same
12618 ** temporary register.
12619 */
12620 v->pParse->nTempReg = 0;
12621 v->pParse->nRangeReg = 0;
12622 }
12623
12624 /*
12625 ** Create a new symbolic label for an instruction that has yet to be
12626 ** coded. The symbolic label is really just a negative number. The
12627 ** label can be used as the P2 value of an operation. Later, when
12628 ** the label is resolved to a specific address, the VDBE will scan
12629 ** through its operation list and change all values of P2 which match
12630 ** the label into the resolved address.
12631 **
12632 ** The VDBE knows that a P2 value is a label because labels are
12633 ** always negative and P2 values are suppose to be non-negative.
12634 ** Hence, a negative P2 value is a label that has yet to be resolved.
12635 **
12636 ** Zero is returned if a malloc() fails.
12637 */
12638 SQLITE_PRIVATE int sqlite3VdbeMakeLabel(Vdbe *v){
12639 Parse *p = v->pParse;
12640 int i = p->nLabel++;
12641 assert( v->magic==VDBE_MAGIC_INIT );
12642 if( (i & (i-1))==0 ){
12643 p->aLabel = sqlite3DbReallocOrFree(p->db, p->aLabel,
12644 (i*2+1)*sizeof(p->aLabel[0]));
12645 }
12646 if( p->aLabel ){
12647 p->aLabel[i] = -1;
12648 }
12649 return ADDR(i);
12650 }
12651
12652 /*
12653 ** Resolve label "x" to be the address of the next instruction to
12654 ** be inserted. The parameter "x" must have been obtained from
12655 ** a prior call to sqlite3VdbeMakeLabel().
12656 */
12657 SQLITE_PRIVATE void sqlite3VdbeResolveLabel(Vdbe *v, int x){
12658 Parse *p = v->pParse;
12659 int j = ADDR(x);
12660 assert( v->magic==VDBE_MAGIC_INIT );
12661 assert( j<p->nLabel );
12662 assert( j>=0 );
12663 if( p->aLabel ){
12664 p->aLabel[j] = v->nOp;
12665 }
12666 }
12667
12668 /*
12669 ** Mark the VDBE as one that can only be run one time.
12670 */
12671 SQLITE_PRIVATE void sqlite3VdbeRunOnlyOnce(Vdbe *p){
12672 p->runOnlyOnce = 1;
12673 }
12674
12675 /*
12676 ** Mark the VDBE as one that can only be run multiple times.
12677 */
12678 SQLITE_PRIVATE void sqlite3VdbeReusable(Vdbe *p){
12679 p->runOnlyOnce = 0;
12680 }
12681
12682 #ifdef SQLITE_DEBUG /* sqlite3AssertMayAbort() logic */
12683
12684 /*
12685 ** The following type and function are used to iterate through all opcodes
12686 ** in a Vdbe main program and each of the sub-programs (triggers) it may
12687 ** invoke directly or indirectly. It should be used as follows:
12688 **
12689 ** Op *pOp;
12690 ** VdbeOpIter sIter;
12691 **
12692 ** memset(&sIter, 0, sizeof(sIter));
12693 ** sIter.v = v; // v is of type Vdbe*
12694 ** while( (pOp = opIterNext(&sIter)) ){
12695 ** // Do something with pOp
12696 ** }
12697 ** sqlite3DbFree(v->db, sIter.apSub);
12698 **
12699 */
12700 typedef struct VdbeOpIter VdbeOpIter;
12701 struct VdbeOpIter {
12702 Vdbe *v; /* Vdbe to iterate through the opcodes of */
12703 SubProgram **apSub; /* Array of subprograms */
12704 int nSub; /* Number of entries in apSub */
12705 int iAddr; /* Address of next instruction to return */
12706 int iSub; /* 0 = main program, 1 = first sub-program etc. */
12707 };
12708 static Op *opIterNext(VdbeOpIter *p){
12709 Vdbe *v = p->v;
12710 Op *pRet = 0;
12711 Op *aOp;
12712 int nOp;
12713
12714 if( p->iSub<=p->nSub ){
12715
12716 if( p->iSub==0 ){
12717 aOp = v->aOp;
12718 nOp = v->nOp;
12719 }else{
12720 aOp = p->apSub[p->iSub-1]->aOp;
12721 nOp = p->apSub[p->iSub-1]->nOp;
12722 }
12723 assert( p->iAddr<nOp );
12724
12725 pRet = &aOp[p->iAddr];
12726 p->iAddr++;
12727 if( p->iAddr==nOp ){
12728 p->iSub++;
12729 p->iAddr = 0;
12730 }
12731
12732 if( pRet->p4type==P4_SUBPROGRAM ){
12733 int nByte = (p->nSub+1)*sizeof(SubProgram*);
12734 int j;
12735 for(j=0; j<p->nSub; j++){
12736 if( p->apSub[j]==pRet->p4.pProgram ) break;
12737 }
12738 if( j==p->nSub ){
12739 p->apSub = sqlite3DbReallocOrFree(v->db, p->apSub, nByte);
12740 if( !p->apSub ){
12741 pRet = 0;
12742 }else{
12743 p->apSub[p->nSub++] = pRet->p4.pProgram;
12744 }
12745 }
12746 }
12747 }
12748
12749 return pRet;
12750 }
12751
12752 /*
12753 ** Check if the program stored in the VM associated with pParse may
12754 ** throw an ABORT exception (causing the statement, but not entire transaction
12755 ** to be rolled back). This condition is true if the main program or any
12756 ** sub-programs contains any of the following:
12757 **
12758 ** * OP_Halt with P1=SQLITE_CONSTRAINT and P2=OE_Abort.
12759 ** * OP_HaltIfNull with P1=SQLITE_CONSTRAINT and P2=OE_Abort.
12760 ** * OP_Destroy
12761 ** * OP_VUpdate
12762 ** * OP_VRename
12763 ** * OP_FkCounter with P2==0 (immediate foreign key constraint)
12764 ** * OP_CreateTable and OP_InitCoroutine (for CREATE TABLE AS SELECT ...)
12765 **
12766 ** Then check that the value of Parse.mayAbort is true if an
12767 ** ABORT may be thrown, or false otherwise. Return true if it does
12768 ** match, or false otherwise. This function is intended to be used as
12769 ** part of an assert statement in the compiler. Similar to:
12770 **
12771 ** assert( sqlite3VdbeAssertMayAbort(pParse->pVdbe, pParse->mayAbort) );
12772 */
12773 SQLITE_PRIVATE int sqlite3VdbeAssertMayAbort(Vdbe *v, int mayAbort){
12774 int hasAbort = 0;
12775 int hasFkCounter = 0;
12776 int hasCreateTable = 0;
12777 int hasInitCoroutine = 0;
12778 Op *pOp;
12779 VdbeOpIter sIter;
12780 memset(&sIter, 0, sizeof(sIter));
12781 sIter.v = v;
12782
12783 while( (pOp = opIterNext(&sIter))!=0 ){
12784 int opcode = pOp->opcode;
12785 if( opcode==OP_Destroy || opcode==OP_VUpdate || opcode==OP_VRename
12786 || ((opcode==OP_Halt || opcode==OP_HaltIfNull)
12787 && ((pOp->p1&0xff)==SQLITE_CONSTRAINT && pOp->p2==OE_Abort))
12788 ){
12789 hasAbort = 1;
12790 break;
12791 }
12792 if( opcode==OP_CreateTable ) hasCreateTable = 1;
12793 if( opcode==OP_InitCoroutine ) hasInitCoroutine = 1;
12794 #ifndef SQLITE_OMIT_FOREIGN_KEY
12795 if( opcode==OP_FkCounter && pOp->p1==0 && pOp->p2==1 ){
12796 hasFkCounter = 1;
12797 }
12798 #endif
12799 }
12800 sqlite3DbFree(v->db, sIter.apSub);
12801
12802 /* Return true if hasAbort==mayAbort. Or if a malloc failure occurred.
12803 ** If malloc failed, then the while() loop above may not have iterated
12804 ** through all opcodes and hasAbort may be set incorrectly. Return
12805 ** true for this case to prevent the assert() in the callers frame
12806 ** from failing. */
12807 return ( v->db->mallocFailed || hasAbort==mayAbort || hasFkCounter
12808 || (hasCreateTable && hasInitCoroutine) );
12809 }
12810 #endif /* SQLITE_DEBUG - the sqlite3AssertMayAbort() function */
12811
12812 /*
12813 ** This routine is called after all opcodes have been inserted. It loops
12814 ** through all the opcodes and fixes up some details.
12815 **
12816 ** (1) For each jump instruction with a negative P2 value (a label)
12817 ** resolve the P2 value to an actual address.
12818 **
12819 ** (2) Compute the maximum number of arguments used by any SQL function
12820 ** and store that value in *pMaxFuncArgs.
12821 **
12822 ** (3) Update the Vdbe.readOnly and Vdbe.bIsReader flags to accurately
12823 ** indicate what the prepared statement actually does.
12824 **
12825 ** (4) Initialize the p4.xAdvance pointer on opcodes that use it.
12826 **
12827 ** (5) Reclaim the memory allocated for storing labels.
12828 **
12829 ** This routine will only function correctly if the mkopcodeh.tcl generator
12830 ** script numbers the opcodes correctly. Changes to this routine must be
12831 ** coordinated with changes to mkopcodeh.tcl.
12832 */
12833 static void resolveP2Values(Vdbe *p, int *pMaxFuncArgs){
12834 int nMaxArgs = *pMaxFuncArgs;
12835 Op *pOp;
12836 Parse *pParse = p->pParse;
12837 int *aLabel = pParse->aLabel;
12838 p->readOnly = 1;
12839 p->bIsReader = 0;
12840 pOp = &p->aOp[p->nOp-1];
12841 while(1){
12842
12843 /* Only JUMP opcodes and the short list of special opcodes in the switch
12844 ** below need to be considered. The mkopcodeh.tcl generator script groups
12845 ** all these opcodes together near the front of the opcode list. Skip
12846 ** any opcode that does not need processing by virtual of the fact that
12847 ** it is larger than SQLITE_MX_JUMP_OPCODE, as a performance optimization.
12848 */
12849 if( pOp->opcode<=SQLITE_MX_JUMP_OPCODE ){
12850 /* NOTE: Be sure to update mkopcodeh.tcl when adding or removing
12851 ** cases from this switch! */
12852 switch( pOp->opcode ){
12853 case OP_Transaction: {
12854 if( pOp->p2!=0 ) p->readOnly = 0;
12855 /* fall thru */
12856 }
12857 case OP_AutoCommit:
12858 case OP_Savepoint: {
12859 p->bIsReader = 1;
12860 break;
12861 }
12862 #ifndef SQLITE_OMIT_WAL
12863 case OP_Checkpoint:
12864 #endif
12865 case OP_Vacuum:
12866 case OP_JournalMode: {
12867 p->readOnly = 0;
12868 p->bIsReader = 1;
12869 break;
12870 }
12871 #ifndef SQLITE_OMIT_VIRTUALTABLE
12872 case OP_VUpdate: {
12873 if( pOp->p2>nMaxArgs ) nMaxArgs = pOp->p2;
12874 break;
12875 }
12876 case OP_VFilter: {
12877 int n;
12878 assert( (pOp - p->aOp) >= 3 );
12879 assert( pOp[-1].opcode==OP_Integer );
12880 n = pOp[-1].p1;
12881 if( n>nMaxArgs ) nMaxArgs = n;
12882 break;
12883 }
12884 #endif
12885 case OP_Next:
12886 case OP_NextIfOpen:
12887 case OP_SorterNext: {
12888 pOp->p4.xAdvance = sqlite3BtreeNext;
12889 pOp->p4type = P4_ADVANCE;
12890 break;
12891 }
12892 case OP_Prev:
12893 case OP_PrevIfOpen: {
12894 pOp->p4.xAdvance = sqlite3BtreePrevious;
12895 pOp->p4type = P4_ADVANCE;
12896 break;
12897 }
12898 }
12899 if( (sqlite3OpcodeProperty[pOp->opcode] & OPFLG_JUMP)!=0 && pOp->p2<0 ){
12900 assert( ADDR(pOp->p2)<pParse->nLabel );
12901 pOp->p2 = aLabel[ADDR(pOp->p2)];
12902 }
12903 }
12904 if( pOp==p->aOp ) break;
12905 pOp--;
12906 }
12907 sqlite3DbFree(p->db, pParse->aLabel);
12908 pParse->aLabel = 0;
12909 pParse->nLabel = 0;
12910 *pMaxFuncArgs = nMaxArgs;
12911 assert( p->bIsReader!=0 || DbMaskAllZero(p->btreeMask) );
12912 }
12913
12914 /*
12915 ** Return the address of the next instruction to be inserted.
12916 */
12917 SQLITE_PRIVATE int sqlite3VdbeCurrentAddr(Vdbe *p){
12918 assert( p->magic==VDBE_MAGIC_INIT );
12919 return p->nOp;
12920 }
12921
12922 /*
12923 ** Verify that at least N opcode slots are available in p without
12924 ** having to malloc for more space (except when compiled using
12925 ** SQLITE_TEST_REALLOC_STRESS). This interface is used during testing
12926 ** to verify that certain calls to sqlite3VdbeAddOpList() can never
12927 ** fail due to a OOM fault and hence that the return value from
12928 ** sqlite3VdbeAddOpList() will always be non-NULL.
12929 */
12930 #if defined(SQLITE_DEBUG) && !defined(SQLITE_TEST_REALLOC_STRESS)
12931 SQLITE_PRIVATE void sqlite3VdbeVerifyNoMallocRequired(Vdbe *p, int N){
12932 assert( p->nOp + N <= p->pParse->nOpAlloc );
12933 }
12934 #endif
12935
12936 /*
12937 ** Verify that the VM passed as the only argument does not contain
12938 ** an OP_ResultRow opcode. Fail an assert() if it does. This is used
12939 ** by code in pragma.c to ensure that the implementation of certain
12940 ** pragmas comports with the flags specified in the mkpragmatab.tcl
12941 ** script.
12942 */
12943 #if defined(SQLITE_DEBUG) && !defined(SQLITE_TEST_REALLOC_STRESS)
12944 SQLITE_PRIVATE void sqlite3VdbeVerifyNoResultRow(Vdbe *p){
12945 int i;
12946 for(i=0; i<p->nOp; i++){
12947 assert( p->aOp[i].opcode!=OP_ResultRow );
12948 }
12949 }
12950 #endif
12951
12952 /*
12953 ** This function returns a pointer to the array of opcodes associated with
12954 ** the Vdbe passed as the first argument. It is the callers responsibility
12955 ** to arrange for the returned array to be eventually freed using the
12956 ** vdbeFreeOpArray() function.
12957 **
12958 ** Before returning, *pnOp is set to the number of entries in the returned
12959 ** array. Also, *pnMaxArg is set to the larger of its current value and
12960 ** the number of entries in the Vdbe.apArg[] array required to execute the
12961 ** returned program.
12962 */
12963 SQLITE_PRIVATE VdbeOp *sqlite3VdbeTakeOpArray(Vdbe *p, int *pnOp, int *pnMaxArg) {
12964 VdbeOp *aOp = p->aOp;
12965 assert( aOp && !p->db->mallocFailed );
12966
12967 /* Check that sqlite3VdbeUsesBtree() was not called on this VM */
12968 assert( DbMaskAllZero(p->btreeMask) );
12969
12970 resolveP2Values(p, pnMaxArg);
12971 *pnOp = p->nOp;
12972 p->aOp = 0;
12973 return aOp;
12974 }
12975
12976 /*
12977 ** Add a whole list of operations to the operation stack. Return a
12978 ** pointer to the first operation inserted.
12979 **
12980 ** Non-zero P2 arguments to jump instructions are automatically adjusted
12981 ** so that the jump target is relative to the first operation inserted.
12982 */
12983 SQLITE_PRIVATE VdbeOp *sqlite3VdbeAddOpList(
12984 Vdbe *p, /* Add opcodes to the prepared statement */
12985 int nOp, /* Number of opcodes to add */
12986 VdbeOpList const *aOp, /* The opcodes to be added */
12987 int iLineno /* Source-file line number of first opcode */
12988 ){
12989 int i;
12990 VdbeOp *pOut, *pFirst;
12991 assert( nOp>0 );
12992 assert( p->magic==VDBE_MAGIC_INIT );
12993 if( p->nOp + nOp > p->pParse->nOpAlloc && growOpArray(p, nOp) ){
12994 return 0;
12995 }
12996 pFirst = pOut = &p->aOp[p->nOp];
12997 for(i=0; i<nOp; i++, aOp++, pOut++){
12998 pOut->opcode = aOp->opcode;
12999 pOut->p1 = aOp->p1;
13000 pOut->p2 = aOp->p2;
13001 assert( aOp->p2>=0 );
13002 if( (sqlite3OpcodeProperty[aOp->opcode] & OPFLG_JUMP)!=0 && aOp->p2>0 ){
13003 pOut->p2 += p->nOp;
13004 }
13005 pOut->p3 = aOp->p3;
13006 pOut->p4type = P4_NOTUSED;
13007 pOut->p4.p = 0;
13008 pOut->p5 = 0;
13009 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
13010 pOut->zComment = 0;
13011 #endif
13012 #ifdef SQLITE_VDBE_COVERAGE
13013 pOut->iSrcLine = iLineno+i;
13014 #else
13015 (void)iLineno;
13016 #endif
13017 #ifdef SQLITE_DEBUG
13018 if( p->db->flags & SQLITE_VdbeAddopTrace ){
13019 sqlite3VdbePrintOp(0, i+p->nOp, &p->aOp[i+p->nOp]);
13020 }
13021 #endif
13022 }
13023 p->nOp += nOp;
13024 return pFirst;
13025 }
13026
13027 #if defined(SQLITE_ENABLE_STMT_SCANSTATUS)
13028 /*
13029 ** Add an entry to the array of counters managed by sqlite3_stmt_scanstatus().
13030 */
13031 SQLITE_PRIVATE void sqlite3VdbeScanStatus(
13032 Vdbe *p, /* VM to add scanstatus() to */
13033 int addrExplain, /* Address of OP_Explain (or 0) */
13034 int addrLoop, /* Address of loop counter */
13035 int addrVisit, /* Address of rows visited counter */
13036 LogEst nEst, /* Estimated number of output rows */
13037 const char *zName /* Name of table or index being scanned */
13038 ){
13039 int nByte = (p->nScan+1) * sizeof(ScanStatus);
13040 ScanStatus *aNew;
13041 aNew = (ScanStatus*)sqlite3DbRealloc(p->db, p->aScan, nByte);
13042 if( aNew ){
13043 ScanStatus *pNew = &aNew[p->nScan++];
13044 pNew->addrExplain = addrExplain;
13045 pNew->addrLoop = addrLoop;
13046 pNew->addrVisit = addrVisit;
13047 pNew->nEst = nEst;
13048 pNew->zName = sqlite3DbStrDup(p->db, zName);
13049 p->aScan = aNew;
13050 }
13051 }
13052 #endif
13053
13054
13055 /*
13056 ** Change the value of the opcode, or P1, P2, P3, or P5 operands
13057 ** for a specific instruction.
13058 */
13059 SQLITE_PRIVATE void sqlite3VdbeChangeOpcode(Vdbe *p, u32 addr, u8 iNewOpcode){
13060 sqlite3VdbeGetOp(p,addr)->opcode = iNewOpcode;
13061 }
13062 SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe *p, u32 addr, int val){
13063 sqlite3VdbeGetOp(p,addr)->p1 = val;
13064 }
13065 SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe *p, u32 addr, int val){
13066 sqlite3VdbeGetOp(p,addr)->p2 = val;
13067 }
13068 SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe *p, u32 addr, int val){
13069 sqlite3VdbeGetOp(p,addr)->p3 = val;
13070 }
13071 SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe *p, u16 p5){
13072 assert( p->nOp>0 || p->db->mallocFailed );
13073 if( p->nOp>0 ) p->aOp[p->nOp-1].p5 = p5;
13074 }
13075
13076 /*
13077 ** Change the P2 operand of instruction addr so that it points to
13078 ** the address of the next instruction to be coded.
13079 */
13080 SQLITE_PRIVATE void sqlite3VdbeJumpHere(Vdbe *p, int addr){
13081 sqlite3VdbeChangeP2(p, addr, p->nOp);
13082 }
13083
13084
13085 /*
13086 ** If the input FuncDef structure is ephemeral, then free it. If
13087 ** the FuncDef is not ephermal, then do nothing.
13088 */
13089 static void freeEphemeralFunction(sqlite3 *db, FuncDef *pDef){
13090 if( (pDef->funcFlags & SQLITE_FUNC_EPHEM)!=0 ){
13091 sqlite3DbFree(db, pDef);
13092 }
13093 }
13094
13095 static void vdbeFreeOpArray(sqlite3 *, Op *, int);
13096
13097 /*
13098 ** Delete a P4 value if necessary.
13099 */
13100 static SQLITE_NOINLINE void freeP4Mem(sqlite3 *db, Mem *p){
13101 if( p->szMalloc ) sqlite3DbFree(db, p->zMalloc);
13102 sqlite3DbFree(db, p);
13103 }
13104 static SQLITE_NOINLINE void freeP4FuncCtx(sqlite3 *db, sqlite3_context *p){
13105 freeEphemeralFunction(db, p->pFunc);
13106 sqlite3DbFree(db, p);
13107 }
13108 static void freeP4(sqlite3 *db, int p4type, void *p4){
13109 assert( db );
13110 switch( p4type ){
13111 case P4_FUNCCTX: {
13112 freeP4FuncCtx(db, (sqlite3_context*)p4);
13113 break;
13114 }
13115 case P4_REAL:
13116 case P4_INT64:
13117 case P4_DYNAMIC:
13118 case P4_INTARRAY: {
13119 sqlite3DbFree(db, p4);
13120 break;
13121 }
13122 case P4_KEYINFO: {
13123 if( db->pnBytesFreed==0 ) sqlite3KeyInfoUnref((KeyInfo*)p4);
13124 break;
13125 }
13126 #ifdef SQLITE_ENABLE_CURSOR_HINTS
13127 case P4_EXPR: {
13128 sqlite3ExprDelete(db, (Expr*)p4);
13129 break;
13130 }
13131 #endif
13132 case P4_FUNCDEF: {
13133 freeEphemeralFunction(db, (FuncDef*)p4);
13134 break;
13135 }
13136 case P4_MEM: {
13137 if( db->pnBytesFreed==0 ){
13138 sqlite3ValueFree((sqlite3_value*)p4);
13139 }else{
13140 freeP4Mem(db, (Mem*)p4);
13141 }
13142 break;
13143 }
13144 case P4_VTAB : {
13145 if( db->pnBytesFreed==0 ) sqlite3VtabUnlock((VTable *)p4);
13146 break;
13147 }
13148 }
13149 }
13150
13151 /*
13152 ** Free the space allocated for aOp and any p4 values allocated for the
13153 ** opcodes contained within. If aOp is not NULL it is assumed to contain
13154 ** nOp entries.
13155 */
13156 static void vdbeFreeOpArray(sqlite3 *db, Op *aOp, int nOp){
13157 if( aOp ){
13158 Op *pOp;
13159 for(pOp=aOp; pOp<&aOp[nOp]; pOp++){
13160 if( pOp->p4type ) freeP4(db, pOp->p4type, pOp->p4.p);
13161 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
13162 sqlite3DbFree(db, pOp->zComment);
13163 #endif
13164 }
13165 }
13166 sqlite3DbFree(db, aOp);
13167 }
13168
13169 /*
13170 ** Link the SubProgram object passed as the second argument into the linked
13171 ** list at Vdbe.pSubProgram. This list is used to delete all sub-program
13172 ** objects when the VM is no longer required.
13173 */
13174 SQLITE_PRIVATE void sqlite3VdbeLinkSubProgram(Vdbe *pVdbe, SubProgram *p){
13175 p->pNext = pVdbe->pProgram;
13176 pVdbe->pProgram = p;
13177 }
13178
13179 /*
13180 ** Change the opcode at addr into OP_Noop
13181 */
13182 SQLITE_PRIVATE int sqlite3VdbeChangeToNoop(Vdbe *p, int addr){
13183 VdbeOp *pOp;
13184 if( p->db->mallocFailed ) return 0;
13185 assert( addr>=0 && addr<p->nOp );
13186 pOp = &p->aOp[addr];
13187 freeP4(p->db, pOp->p4type, pOp->p4.p);
13188 pOp->p4type = P4_NOTUSED;
13189 pOp->p4.z = 0;
13190 pOp->opcode = OP_Noop;
13191 return 1;
13192 }
13193
13194 /*
13195 ** If the last opcode is "op" and it is not a jump destination,
13196 ** then remove it. Return true if and only if an opcode was removed.
13197 */
13198 SQLITE_PRIVATE int sqlite3VdbeDeletePriorOpcode(Vdbe *p, u8 op){
13199 if( p->nOp>0 && p->aOp[p->nOp-1].opcode==op ){
13200 return sqlite3VdbeChangeToNoop(p, p->nOp-1);
13201 }else{
13202 return 0;
13203 }
13204 }
13205
13206 /*
13207 ** Change the value of the P4 operand for a specific instruction.
13208 ** This routine is useful when a large program is loaded from a
13209 ** static array using sqlite3VdbeAddOpList but we want to make a
13210 ** few minor changes to the program.
13211 **
13212 ** If n>=0 then the P4 operand is dynamic, meaning that a copy of
13213 ** the string is made into memory obtained from sqlite3_malloc().
13214 ** A value of n==0 means copy bytes of zP4 up to and including the
13215 ** first null byte. If n>0 then copy n+1 bytes of zP4.
13216 **
13217 ** Other values of n (P4_STATIC, P4_COLLSEQ etc.) indicate that zP4 points
13218 ** to a string or structure that is guaranteed to exist for the lifetime of
13219 ** the Vdbe. In these cases we can just copy the pointer.
13220 **
13221 ** If addr<0 then change P4 on the most recently inserted instruction.
13222 */
13223 static void SQLITE_NOINLINE vdbeChangeP4Full(
13224 Vdbe *p,
13225 Op *pOp,
13226 const char *zP4,
13227 int n
13228 ){
13229 if( pOp->p4type ){
13230 freeP4(p->db, pOp->p4type, pOp->p4.p);
13231 pOp->p4type = 0;
13232 pOp->p4.p = 0;
13233 }
13234 if( n<0 ){
13235 sqlite3VdbeChangeP4(p, (int)(pOp - p->aOp), zP4, n);
13236 }else{
13237 if( n==0 ) n = sqlite3Strlen30(zP4);
13238 pOp->p4.z = sqlite3DbStrNDup(p->db, zP4, n);
13239 pOp->p4type = P4_DYNAMIC;
13240 }
13241 }
13242 SQLITE_PRIVATE void sqlite3VdbeChangeP4(Vdbe *p, int addr, const char *zP4, int n){
13243 Op *pOp;
13244 sqlite3 *db;
13245 assert( p!=0 );
13246 db = p->db;
13247 assert( p->magic==VDBE_MAGIC_INIT );
13248 assert( p->aOp!=0 || db->mallocFailed );
13249 if( db->mallocFailed ){
13250 if( n!=P4_VTAB ) freeP4(db, n, (void*)*(char**)&zP4);
13251 return;
13252 }
13253 assert( p->nOp>0 );
13254 assert( addr<p->nOp );
13255 if( addr<0 ){
13256 addr = p->nOp - 1;
13257 }
13258 pOp = &p->aOp[addr];
13259 if( n>=0 || pOp->p4type ){
13260 vdbeChangeP4Full(p, pOp, zP4, n);
13261 return;
13262 }
13263 if( n==P4_INT32 ){
13264 /* Note: this cast is safe, because the origin data point was an int
13265 ** that was cast to a (const char *). */
13266 pOp->p4.i = SQLITE_PTR_TO_INT(zP4);
13267 pOp->p4type = P4_INT32;
13268 }else if( zP4!=0 ){
13269 assert( n<0 );
13270 pOp->p4.p = (void*)zP4;
13271 pOp->p4type = (signed char)n;
13272 if( n==P4_VTAB ) sqlite3VtabLock((VTable*)zP4);
13273 }
13274 }
13275
13276 /*
13277 ** Change the P4 operand of the most recently coded instruction
13278 ** to the value defined by the arguments. This is a high-speed
13279 ** version of sqlite3VdbeChangeP4().
13280 **
13281 ** The P4 operand must not have been previously defined. And the new
13282 ** P4 must not be P4_INT32. Use sqlite3VdbeChangeP4() in either of
13283 ** those cases.
13284 */
13285 SQLITE_PRIVATE void sqlite3VdbeAppendP4(Vdbe *p, void *pP4, int n){
13286 VdbeOp *pOp;
13287 assert( n!=P4_INT32 && n!=P4_VTAB );
13288 assert( n<=0 );
13289 if( p->db->mallocFailed ){
13290 freeP4(p->db, n, pP4);
13291 }else{
13292 assert( pP4!=0 );
13293 assert( p->nOp>0 );
13294 pOp = &p->aOp[p->nOp-1];
13295 assert( pOp->p4type==P4_NOTUSED );
13296 pOp->p4type = n;
13297 pOp->p4.p = pP4;
13298 }
13299 }
13300
13301 /*
13302 ** Set the P4 on the most recently added opcode to the KeyInfo for the
13303 ** index given.
13304 */
13305 SQLITE_PRIVATE void sqlite3VdbeSetP4KeyInfo(Parse *pParse, Index *pIdx){
13306 Vdbe *v = pParse->pVdbe;
13307 KeyInfo *pKeyInfo;
13308 assert( v!=0 );
13309 assert( pIdx!=0 );
13310 pKeyInfo = sqlite3KeyInfoOfIndex(pParse, pIdx);
13311 if( pKeyInfo ) sqlite3VdbeAppendP4(v, pKeyInfo, P4_KEYINFO);
13312 }
13313
13314 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
13315 /*
13316 ** Change the comment on the most recently coded instruction. Or
13317 ** insert a No-op and add the comment to that new instruction. This
13318 ** makes the code easier to read during debugging. None of this happens
13319 ** in a production build.
13320 */
13321 static void vdbeVComment(Vdbe *p, const char *zFormat, va_list ap){
13322 assert( p->nOp>0 || p->aOp==0 );
13323 assert( p->aOp==0 || p->aOp[p->nOp-1].zComment==0 || p->db->mallocFailed );
13324 if( p->nOp ){
13325 assert( p->aOp );
13326 sqlite3DbFree(p->db, p->aOp[p->nOp-1].zComment);
13327 p->aOp[p->nOp-1].zComment = sqlite3VMPrintf(p->db, zFormat, ap);
13328 }
13329 }
13330 SQLITE_PRIVATE void sqlite3VdbeComment(Vdbe *p, const char *zFormat, ...){
13331 va_list ap;
13332 if( p ){
13333 va_start(ap, zFormat);
13334 vdbeVComment(p, zFormat, ap);
13335 va_end(ap);
13336 }
13337 }
13338 SQLITE_PRIVATE void sqlite3VdbeNoopComment(Vdbe *p, const char *zFormat, ...){
13339 va_list ap;
13340 if( p ){
13341 sqlite3VdbeAddOp0(p, OP_Noop);
13342 va_start(ap, zFormat);
13343 vdbeVComment(p, zFormat, ap);
13344 va_end(ap);
13345 }
13346 }
13347 #endif /* NDEBUG */
13348
13349 #ifdef SQLITE_VDBE_COVERAGE
13350 /*
13351 ** Set the value if the iSrcLine field for the previously coded instruction.
13352 */
13353 SQLITE_PRIVATE void sqlite3VdbeSetLineNumber(Vdbe *v, int iLine){
13354 sqlite3VdbeGetOp(v,-1)->iSrcLine = iLine;
13355 }
13356 #endif /* SQLITE_VDBE_COVERAGE */
13357
13358 /*
13359 ** Return the opcode for a given address. If the address is -1, then
13360 ** return the most recently inserted opcode.
13361 **
13362 ** If a memory allocation error has occurred prior to the calling of this
13363 ** routine, then a pointer to a dummy VdbeOp will be returned. That opcode
13364 ** is readable but not writable, though it is cast to a writable value.
13365 ** The return of a dummy opcode allows the call to continue functioning
13366 ** after an OOM fault without having to check to see if the return from
13367 ** this routine is a valid pointer. But because the dummy.opcode is 0,
13368 ** dummy will never be written to. This is verified by code inspection and
13369 ** by running with Valgrind.
13370 */
13371 SQLITE_PRIVATE VdbeOp *sqlite3VdbeGetOp(Vdbe *p, int addr){
13372 /* C89 specifies that the constant "dummy" will be initialized to all
13373 ** zeros, which is correct. MSVC generates a warning, nevertheless. */
13374 static VdbeOp dummy; /* Ignore the MSVC warning about no initializer */
13375 assert( p->magic==VDBE_MAGIC_INIT );
13376 if( addr<0 ){
13377 addr = p->nOp - 1;
13378 }
13379 assert( (addr>=0 && addr<p->nOp) || p->db->mallocFailed );
13380 if( p->db->mallocFailed ){
13381 return (VdbeOp*)&dummy;
13382 }else{
13383 return &p->aOp[addr];
13384 }
13385 }
13386
13387 #if defined(SQLITE_ENABLE_EXPLAIN_COMMENTS)
13388 /*
13389 ** Return an integer value for one of the parameters to the opcode pOp
13390 ** determined by character c.
13391 */
13392 static int translateP(char c, const Op *pOp){
13393 if( c=='1' ) return pOp->p1;
13394 if( c=='2' ) return pOp->p2;
13395 if( c=='3' ) return pOp->p3;
13396 if( c=='4' ) return pOp->p4.i;
13397 return pOp->p5;
13398 }
13399
13400 /*
13401 ** Compute a string for the "comment" field of a VDBE opcode listing.
13402 **
13403 ** The Synopsis: field in comments in the vdbe.c source file gets converted
13404 ** to an extra string that is appended to the sqlite3OpcodeName(). In the
13405 ** absence of other comments, this synopsis becomes the comment on the opcode.
13406 ** Some translation occurs:
13407 **
13408 ** "PX" -> "r[X]"
13409 ** "PX@PY" -> "r[X..X+Y-1]" or "r[x]" if y is 0 or 1
13410 ** "PX@PY+1" -> "r[X..X+Y]" or "r[x]" if y is 0
13411 ** "PY..PY" -> "r[X..Y]" or "r[x]" if y<=x
13412 */
13413 static int displayComment(
13414 const Op *pOp, /* The opcode to be commented */
13415 const char *zP4, /* Previously obtained value for P4 */
13416 char *zTemp, /* Write result here */
13417 int nTemp /* Space available in zTemp[] */
13418 ){
13419 const char *zOpName;
13420 const char *zSynopsis;
13421 int nOpName;
13422 int ii, jj;
13423 char zAlt[50];
13424 zOpName = sqlite3OpcodeName(pOp->opcode);
13425 nOpName = sqlite3Strlen30(zOpName);
13426 if( zOpName[nOpName+1] ){
13427 int seenCom = 0;
13428 char c;
13429 zSynopsis = zOpName += nOpName + 1;
13430 if( strncmp(zSynopsis,"IF ",3)==0 ){
13431 if( pOp->p5 & SQLITE_STOREP2 ){
13432 sqlite3_snprintf(sizeof(zAlt), zAlt, "r[P2] = (%s)", zSynopsis+3);
13433 }else{
13434 sqlite3_snprintf(sizeof(zAlt), zAlt, "if %s goto P2", zSynopsis+3);
13435 }
13436 zSynopsis = zAlt;
13437 }
13438 for(ii=jj=0; jj<nTemp-1 && (c = zSynopsis[ii])!=0; ii++){
13439 if( c=='P' ){
13440 c = zSynopsis[++ii];
13441 if( c=='4' ){
13442 sqlite3_snprintf(nTemp-jj, zTemp+jj, "%s", zP4);
13443 }else if( c=='X' ){
13444 sqlite3_snprintf(nTemp-jj, zTemp+jj, "%s", pOp->zComment);
13445 seenCom = 1;
13446 }else{
13447 int v1 = translateP(c, pOp);
13448 int v2;
13449 sqlite3_snprintf(nTemp-jj, zTemp+jj, "%d", v1);
13450 if( strncmp(zSynopsis+ii+1, "@P", 2)==0 ){
13451 ii += 3;
13452 jj += sqlite3Strlen30(zTemp+jj);
13453 v2 = translateP(zSynopsis[ii], pOp);
13454 if( strncmp(zSynopsis+ii+1,"+1",2)==0 ){
13455 ii += 2;
13456 v2++;
13457 }
13458 if( v2>1 ){
13459 sqlite3_snprintf(nTemp-jj, zTemp+jj, "..%d", v1+v2-1);
13460 }
13461 }else if( strncmp(zSynopsis+ii+1, "..P3", 4)==0 && pOp->p3==0 ){
13462 ii += 4;
13463 }
13464 }
13465 jj += sqlite3Strlen30(zTemp+jj);
13466 }else{
13467 zTemp[jj++] = c;
13468 }
13469 }
13470 if( !seenCom && jj<nTemp-5 && pOp->zComment ){
13471 sqlite3_snprintf(nTemp-jj, zTemp+jj, "; %s", pOp->zComment);
13472 jj += sqlite3Strlen30(zTemp+jj);
13473 }
13474 if( jj<nTemp ) zTemp[jj] = 0;
13475 }else if( pOp->zComment ){
13476 sqlite3_snprintf(nTemp, zTemp, "%s", pOp->zComment);
13477 jj = sqlite3Strlen30(zTemp);
13478 }else{
13479 zTemp[0] = 0;
13480 jj = 0;
13481 }
13482 return jj;
13483 }
13484 #endif /* SQLITE_DEBUG */
13485
13486 #if VDBE_DISPLAY_P4 && defined(SQLITE_ENABLE_CURSOR_HINTS)
13487 /*
13488 ** Translate the P4.pExpr value for an OP_CursorHint opcode into text
13489 ** that can be displayed in the P4 column of EXPLAIN output.
13490 */
13491 static void displayP4Expr(StrAccum *p, Expr *pExpr){
13492 const char *zOp = 0;
13493 switch( pExpr->op ){
13494 case TK_STRING:
13495 sqlite3XPrintf(p, "%Q", pExpr->u.zToken);
13496 break;
13497 case TK_INTEGER:
13498 sqlite3XPrintf(p, "%d", pExpr->u.iValue);
13499 break;
13500 case TK_NULL:
13501 sqlite3XPrintf(p, "NULL");
13502 break;
13503 case TK_REGISTER: {
13504 sqlite3XPrintf(p, "r[%d]", pExpr->iTable);
13505 break;
13506 }
13507 case TK_COLUMN: {
13508 if( pExpr->iColumn<0 ){
13509 sqlite3XPrintf(p, "rowid");
13510 }else{
13511 sqlite3XPrintf(p, "c%d", (int)pExpr->iColumn);
13512 }
13513 break;
13514 }
13515 case TK_LT: zOp = "LT"; break;
13516 case TK_LE: zOp = "LE"; break;
13517 case TK_GT: zOp = "GT"; break;
13518 case TK_GE: zOp = "GE"; break;
13519 case TK_NE: zOp = "NE"; break;
13520 case TK_EQ: zOp = "EQ"; break;
13521 case TK_IS: zOp = "IS"; break;
13522 case TK_ISNOT: zOp = "ISNOT"; break;
13523 case TK_AND: zOp = "AND"; break;
13524 case TK_OR: zOp = "OR"; break;
13525 case TK_PLUS: zOp = "ADD"; break;
13526 case TK_STAR: zOp = "MUL"; break;
13527 case TK_MINUS: zOp = "SUB"; break;
13528 case TK_REM: zOp = "REM"; break;
13529 case TK_BITAND: zOp = "BITAND"; break;
13530 case TK_BITOR: zOp = "BITOR"; break;
13531 case TK_SLASH: zOp = "DIV"; break;
13532 case TK_LSHIFT: zOp = "LSHIFT"; break;
13533 case TK_RSHIFT: zOp = "RSHIFT"; break;
13534 case TK_CONCAT: zOp = "CONCAT"; break;
13535 case TK_UMINUS: zOp = "MINUS"; break;
13536 case TK_UPLUS: zOp = "PLUS"; break;
13537 case TK_BITNOT: zOp = "BITNOT"; break;
13538 case TK_NOT: zOp = "NOT"; break;
13539 case TK_ISNULL: zOp = "ISNULL"; break;
13540 case TK_NOTNULL: zOp = "NOTNULL"; break;
13541
13542 default:
13543 sqlite3XPrintf(p, "%s", "expr");
13544 break;
13545 }
13546
13547 if( zOp ){
13548 sqlite3XPrintf(p, "%s(", zOp);
13549 displayP4Expr(p, pExpr->pLeft);
13550 if( pExpr->pRight ){
13551 sqlite3StrAccumAppend(p, ",", 1);
13552 displayP4Expr(p, pExpr->pRight);
13553 }
13554 sqlite3StrAccumAppend(p, ")", 1);
13555 }
13556 }
13557 #endif /* VDBE_DISPLAY_P4 && defined(SQLITE_ENABLE_CURSOR_HINTS) */
13558
13559
13560 #if VDBE_DISPLAY_P4
13561 /*
13562 ** Compute a string that describes the P4 parameter for an opcode.
13563 ** Use zTemp for any required temporary buffer space.
13564 */
13565 static char *displayP4(Op *pOp, char *zTemp, int nTemp){
13566 char *zP4 = zTemp;
13567 StrAccum x;
13568 assert( nTemp>=20 );
13569 sqlite3StrAccumInit(&x, 0, zTemp, nTemp, 0);
13570 switch( pOp->p4type ){
13571 case P4_KEYINFO: {
13572 int j;
13573 KeyInfo *pKeyInfo = pOp->p4.pKeyInfo;
13574 assert( pKeyInfo->aSortOrder!=0 );
13575 sqlite3XPrintf(&x, "k(%d", pKeyInfo->nField);
13576 for(j=0; j<pKeyInfo->nField; j++){
13577 CollSeq *pColl = pKeyInfo->aColl[j];
13578 const char *zColl = pColl ? pColl->zName : "";
13579 if( strcmp(zColl, "BINARY")==0 ) zColl = "B";
13580 sqlite3XPrintf(&x, ",%s%s", pKeyInfo->aSortOrder[j] ? "-" : "", zColl);
13581 }
13582 sqlite3StrAccumAppend(&x, ")", 1);
13583 break;
13584 }
13585 #ifdef SQLITE_ENABLE_CURSOR_HINTS
13586 case P4_EXPR: {
13587 displayP4Expr(&x, pOp->p4.pExpr);
13588 break;
13589 }
13590 #endif
13591 case P4_COLLSEQ: {
13592 CollSeq *pColl = pOp->p4.pColl;
13593 sqlite3XPrintf(&x, "(%.20s)", pColl->zName);
13594 break;
13595 }
13596 case P4_FUNCDEF: {
13597 FuncDef *pDef = pOp->p4.pFunc;
13598 sqlite3XPrintf(&x, "%s(%d)", pDef->zName, pDef->nArg);
13599 break;
13600 }
13601 #if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
13602 case P4_FUNCCTX: {
13603 FuncDef *pDef = pOp->p4.pCtx->pFunc;
13604 sqlite3XPrintf(&x, "%s(%d)", pDef->zName, pDef->nArg);
13605 break;
13606 }
13607 #endif
13608 case P4_INT64: {
13609 sqlite3XPrintf(&x, "%lld", *pOp->p4.pI64);
13610 break;
13611 }
13612 case P4_INT32: {
13613 sqlite3XPrintf(&x, "%d", pOp->p4.i);
13614 break;
13615 }
13616 case P4_REAL: {
13617 sqlite3XPrintf(&x, "%.16g", *pOp->p4.pReal);
13618 break;
13619 }
13620 case P4_MEM: {
13621 Mem *pMem = pOp->p4.pMem;
13622 if( pMem->flags & MEM_Str ){
13623 zP4 = pMem->z;
13624 }else if( pMem->flags & MEM_Int ){
13625 sqlite3XPrintf(&x, "%lld", pMem->u.i);
13626 }else if( pMem->flags & MEM_Real ){
13627 sqlite3XPrintf(&x, "%.16g", pMem->u.r);
13628 }else if( pMem->flags & MEM_Null ){
13629 zP4 = "NULL";
13630 }else{
13631 assert( pMem->flags & MEM_Blob );
13632 zP4 = "(blob)";
13633 }
13634 break;
13635 }
13636 #ifndef SQLITE_OMIT_VIRTUALTABLE
13637 case P4_VTAB: {
13638 sqlite3_vtab *pVtab = pOp->p4.pVtab->pVtab;
13639 sqlite3XPrintf(&x, "vtab:%p", pVtab);
13640 break;
13641 }
13642 #endif
13643 case P4_INTARRAY: {
13644 int i;
13645 int *ai = pOp->p4.ai;
13646 int n = ai[0]; /* The first element of an INTARRAY is always the
13647 ** count of the number of elements to follow */
13648 for(i=1; i<n; i++){
13649 sqlite3XPrintf(&x, ",%d", ai[i]);
13650 }
13651 zTemp[0] = '[';
13652 sqlite3StrAccumAppend(&x, "]", 1);
13653 break;
13654 }
13655 case P4_SUBPROGRAM: {
13656 sqlite3XPrintf(&x, "program");
13657 break;
13658 }
13659 case P4_ADVANCE: {
13660 zTemp[0] = 0;
13661 break;
13662 }
13663 case P4_TABLE: {
13664 sqlite3XPrintf(&x, "%s", pOp->p4.pTab->zName);
13665 break;
13666 }
13667 default: {
13668 zP4 = pOp->p4.z;
13669 if( zP4==0 ){
13670 zP4 = zTemp;
13671 zTemp[0] = 0;
13672 }
13673 }
13674 }
13675 sqlite3StrAccumFinish(&x);
13676 assert( zP4!=0 );
13677 return zP4;
13678 }
13679 #endif /* VDBE_DISPLAY_P4 */
13680
13681 /*
13682 ** Declare to the Vdbe that the BTree object at db->aDb[i] is used.
13683 **
13684 ** The prepared statements need to know in advance the complete set of
13685 ** attached databases that will be use. A mask of these databases
13686 ** is maintained in p->btreeMask. The p->lockMask value is the subset of
13687 ** p->btreeMask of databases that will require a lock.
13688 */
13689 SQLITE_PRIVATE void sqlite3VdbeUsesBtree(Vdbe *p, int i){
13690 assert( i>=0 && i<p->db->nDb && i<(int)sizeof(yDbMask)*8 );
13691 assert( i<(int)sizeof(p->btreeMask)*8 );
13692 DbMaskSet(p->btreeMask, i);
13693 if( i!=1 && sqlite3BtreeSharable(p->db->aDb[i].pBt) ){
13694 DbMaskSet(p->lockMask, i);
13695 }
13696 }
13697
13698 #if !defined(SQLITE_OMIT_SHARED_CACHE)
13699 /*
13700 ** If SQLite is compiled to support shared-cache mode and to be threadsafe,
13701 ** this routine obtains the mutex associated with each BtShared structure
13702 ** that may be accessed by the VM passed as an argument. In doing so it also
13703 ** sets the BtShared.db member of each of the BtShared structures, ensuring
13704 ** that the correct busy-handler callback is invoked if required.
13705 **
13706 ** If SQLite is not threadsafe but does support shared-cache mode, then
13707 ** sqlite3BtreeEnter() is invoked to set the BtShared.db variables
13708 ** of all of BtShared structures accessible via the database handle
13709 ** associated with the VM.
13710 **
13711 ** If SQLite is not threadsafe and does not support shared-cache mode, this
13712 ** function is a no-op.
13713 **
13714 ** The p->btreeMask field is a bitmask of all btrees that the prepared
13715 ** statement p will ever use. Let N be the number of bits in p->btreeMask
13716 ** corresponding to btrees that use shared cache. Then the runtime of
13717 ** this routine is N*N. But as N is rarely more than 1, this should not
13718 ** be a problem.
13719 */
13720 SQLITE_PRIVATE void sqlite3VdbeEnter(Vdbe *p){
13721 int i;
13722 sqlite3 *db;
13723 Db *aDb;
13724 int nDb;
13725 if( DbMaskAllZero(p->lockMask) ) return; /* The common case */
13726 db = p->db;
13727 aDb = db->aDb;
13728 nDb = db->nDb;
13729 for(i=0; i<nDb; i++){
13730 if( i!=1 && DbMaskTest(p->lockMask,i) && ALWAYS(aDb[i].pBt!=0) ){
13731 sqlite3BtreeEnter(aDb[i].pBt);
13732 }
13733 }
13734 }
13735 #endif
13736
13737 #if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0
13738 /*
13739 ** Unlock all of the btrees previously locked by a call to sqlite3VdbeEnter().
13740 */
13741 static SQLITE_NOINLINE void vdbeLeave(Vdbe *p){
13742 int i;
13743 sqlite3 *db;
13744 Db *aDb;
13745 int nDb;
13746 db = p->db;
13747 aDb = db->aDb;
13748 nDb = db->nDb;
13749 for(i=0; i<nDb; i++){
13750 if( i!=1 && DbMaskTest(p->lockMask,i) && ALWAYS(aDb[i].pBt!=0) ){
13751 sqlite3BtreeLeave(aDb[i].pBt);
13752 }
13753 }
13754 }
13755 SQLITE_PRIVATE void sqlite3VdbeLeave(Vdbe *p){
13756 if( DbMaskAllZero(p->lockMask) ) return; /* The common case */
13757 vdbeLeave(p);
13758 }
13759 #endif
13760
13761 #if defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
13762 /*
13763 ** Print a single opcode. This routine is used for debugging only.
13764 */
13765 SQLITE_PRIVATE void sqlite3VdbePrintOp(FILE *pOut, int pc, Op *pOp){
13766 char *zP4;
13767 char zPtr[50];
13768 char zCom[100];
13769 static const char *zFormat1 = "%4d %-13s %4d %4d %4d %-13s %.2X %s\n";
13770 if( pOut==0 ) pOut = stdout;
13771 zP4 = displayP4(pOp, zPtr, sizeof(zPtr));
13772 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
13773 displayComment(pOp, zP4, zCom, sizeof(zCom));
13774 #else
13775 zCom[0] = 0;
13776 #endif
13777 /* NB: The sqlite3OpcodeName() function is implemented by code created
13778 ** by the mkopcodeh.awk and mkopcodec.awk scripts which extract the
13779 ** information from the vdbe.c source text */
13780 fprintf(pOut, zFormat1, pc,
13781 sqlite3OpcodeName(pOp->opcode), pOp->p1, pOp->p2, pOp->p3, zP4, pOp->p5,
13782 zCom
13783 );
13784 fflush(pOut);
13785 }
13786 #endif
13787
13788 /*
13789 ** Initialize an array of N Mem element.
13790 */
13791 static void initMemArray(Mem *p, int N, sqlite3 *db, u16 flags){
13792 while( (N--)>0 ){
13793 p->db = db;
13794 p->flags = flags;
13795 p->szMalloc = 0;
13796 #ifdef SQLITE_DEBUG
13797 p->pScopyFrom = 0;
13798 #endif
13799 p++;
13800 }
13801 }
13802
13803 /*
13804 ** Release an array of N Mem elements
13805 */
13806 static void releaseMemArray(Mem *p, int N){
13807 if( p && N ){
13808 Mem *pEnd = &p[N];
13809 sqlite3 *db = p->db;
13810 if( db->pnBytesFreed ){
13811 do{
13812 if( p->szMalloc ) sqlite3DbFree(db, p->zMalloc);
13813 }while( (++p)<pEnd );
13814 return;
13815 }
13816 do{
13817 assert( (&p[1])==pEnd || p[0].db==p[1].db );
13818 assert( sqlite3VdbeCheckMemInvariants(p) );
13819
13820 /* This block is really an inlined version of sqlite3VdbeMemRelease()
13821 ** that takes advantage of the fact that the memory cell value is
13822 ** being set to NULL after releasing any dynamic resources.
13823 **
13824 ** The justification for duplicating code is that according to
13825 ** callgrind, this causes a certain test case to hit the CPU 4.7
13826 ** percent less (x86 linux, gcc version 4.1.2, -O6) than if
13827 ** sqlite3MemRelease() were called from here. With -O2, this jumps
13828 ** to 6.6 percent. The test case is inserting 1000 rows into a table
13829 ** with no indexes using a single prepared INSERT statement, bind()
13830 ** and reset(). Inserts are grouped into a transaction.
13831 */
13832 testcase( p->flags & MEM_Agg );
13833 testcase( p->flags & MEM_Dyn );
13834 testcase( p->flags & MEM_Frame );
13835 testcase( p->flags & MEM_RowSet );
13836 if( p->flags&(MEM_Agg|MEM_Dyn|MEM_Frame|MEM_RowSet) ){
13837 sqlite3VdbeMemRelease(p);
13838 }else if( p->szMalloc ){
13839 sqlite3DbFree(db, p->zMalloc);
13840 p->szMalloc = 0;
13841 }
13842
13843 p->flags = MEM_Undefined;
13844 }while( (++p)<pEnd );
13845 }
13846 }
13847
13848 /*
13849 ** Delete a VdbeFrame object and its contents. VdbeFrame objects are
13850 ** allocated by the OP_Program opcode in sqlite3VdbeExec().
13851 */
13852 SQLITE_PRIVATE void sqlite3VdbeFrameDelete(VdbeFrame *p){
13853 int i;
13854 Mem *aMem = VdbeFrameMem(p);
13855 VdbeCursor **apCsr = (VdbeCursor **)&aMem[p->nChildMem];
13856 for(i=0; i<p->nChildCsr; i++){
13857 sqlite3VdbeFreeCursor(p->v, apCsr[i]);
13858 }
13859 releaseMemArray(aMem, p->nChildMem);
13860 sqlite3VdbeDeleteAuxData(p->v->db, &p->pAuxData, -1, 0);
13861 sqlite3DbFree(p->v->db, p);
13862 }
13863
13864 #ifndef SQLITE_OMIT_EXPLAIN
13865 /*
13866 ** Give a listing of the program in the virtual machine.
13867 **
13868 ** The interface is the same as sqlite3VdbeExec(). But instead of
13869 ** running the code, it invokes the callback once for each instruction.
13870 ** This feature is used to implement "EXPLAIN".
13871 **
13872 ** When p->explain==1, each instruction is listed. When
13873 ** p->explain==2, only OP_Explain instructions are listed and these
13874 ** are shown in a different format. p->explain==2 is used to implement
13875 ** EXPLAIN QUERY PLAN.
13876 **
13877 ** When p->explain==1, first the main program is listed, then each of
13878 ** the trigger subprograms are listed one by one.
13879 */
13880 SQLITE_PRIVATE int sqlite3VdbeList(
13881 Vdbe *p /* The VDBE */
13882 ){
13883 int nRow; /* Stop when row count reaches this */
13884 int nSub = 0; /* Number of sub-vdbes seen so far */
13885 SubProgram **apSub = 0; /* Array of sub-vdbes */
13886 Mem *pSub = 0; /* Memory cell hold array of subprogs */
13887 sqlite3 *db = p->db; /* The database connection */
13888 int i; /* Loop counter */
13889 int rc = SQLITE_OK; /* Return code */
13890 Mem *pMem = &p->aMem[1]; /* First Mem of result set */
13891
13892 assert( p->explain );
13893 assert( p->magic==VDBE_MAGIC_RUN );
13894 assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY || p->rc==SQLITE_NOMEM );
13895
13896 /* Even though this opcode does not use dynamic strings for
13897 ** the result, result columns may become dynamic if the user calls
13898 ** sqlite3_column_text16(), causing a translation to UTF-16 encoding.
13899 */
13900 releaseMemArray(pMem, 8);
13901 p->pResultSet = 0;
13902
13903 if( p->rc==SQLITE_NOMEM_BKPT ){
13904 /* This happens if a malloc() inside a call to sqlite3_column_text() or
13905 ** sqlite3_column_text16() failed. */
13906 sqlite3OomFault(db);
13907 return SQLITE_ERROR;
13908 }
13909
13910 /* When the number of output rows reaches nRow, that means the
13911 ** listing has finished and sqlite3_step() should return SQLITE_DONE.
13912 ** nRow is the sum of the number of rows in the main program, plus
13913 ** the sum of the number of rows in all trigger subprograms encountered
13914 ** so far. The nRow value will increase as new trigger subprograms are
13915 ** encountered, but p->pc will eventually catch up to nRow.
13916 */
13917 nRow = p->nOp;
13918 if( p->explain==1 ){
13919 /* The first 8 memory cells are used for the result set. So we will
13920 ** commandeer the 9th cell to use as storage for an array of pointers
13921 ** to trigger subprograms. The VDBE is guaranteed to have at least 9
13922 ** cells. */
13923 assert( p->nMem>9 );
13924 pSub = &p->aMem[9];
13925 if( pSub->flags&MEM_Blob ){
13926 /* On the first call to sqlite3_step(), pSub will hold a NULL. It is
13927 ** initialized to a BLOB by the P4_SUBPROGRAM processing logic below */
13928 nSub = pSub->n/sizeof(Vdbe*);
13929 apSub = (SubProgram **)pSub->z;
13930 }
13931 for(i=0; i<nSub; i++){
13932 nRow += apSub[i]->nOp;
13933 }
13934 }
13935
13936 do{
13937 i = p->pc++;
13938 }while( i<nRow && p->explain==2 && p->aOp[i].opcode!=OP_Explain );
13939 if( i>=nRow ){
13940 p->rc = SQLITE_OK;
13941 rc = SQLITE_DONE;
13942 }else if( db->u1.isInterrupted ){
13943 p->rc = SQLITE_INTERRUPT;
13944 rc = SQLITE_ERROR;
13945 sqlite3VdbeError(p, sqlite3ErrStr(p->rc));
13946 }else{
13947 char *zP4;
13948 Op *pOp;
13949 if( i<p->nOp ){
13950 /* The output line number is small enough that we are still in the
13951 ** main program. */
13952 pOp = &p->aOp[i];
13953 }else{
13954 /* We are currently listing subprograms. Figure out which one and
13955 ** pick up the appropriate opcode. */
13956 int j;
13957 i -= p->nOp;
13958 for(j=0; i>=apSub[j]->nOp; j++){
13959 i -= apSub[j]->nOp;
13960 }
13961 pOp = &apSub[j]->aOp[i];
13962 }
13963 if( p->explain==1 ){
13964 pMem->flags = MEM_Int;
13965 pMem->u.i = i; /* Program counter */
13966 pMem++;
13967
13968 pMem->flags = MEM_Static|MEM_Str|MEM_Term;
13969 pMem->z = (char*)sqlite3OpcodeName(pOp->opcode); /* Opcode */
13970 assert( pMem->z!=0 );
13971 pMem->n = sqlite3Strlen30(pMem->z);
13972 pMem->enc = SQLITE_UTF8;
13973 pMem++;
13974
13975 /* When an OP_Program opcode is encounter (the only opcode that has
13976 ** a P4_SUBPROGRAM argument), expand the size of the array of subprograms
13977 ** kept in p->aMem[9].z to hold the new program - assuming this subprogram
13978 ** has not already been seen.
13979 */
13980 if( pOp->p4type==P4_SUBPROGRAM ){
13981 int nByte = (nSub+1)*sizeof(SubProgram*);
13982 int j;
13983 for(j=0; j<nSub; j++){
13984 if( apSub[j]==pOp->p4.pProgram ) break;
13985 }
13986 if( j==nSub && SQLITE_OK==sqlite3VdbeMemGrow(pSub, nByte, nSub!=0) ){
13987 apSub = (SubProgram **)pSub->z;
13988 apSub[nSub++] = pOp->p4.pProgram;
13989 pSub->flags |= MEM_Blob;
13990 pSub->n = nSub*sizeof(SubProgram*);
13991 }
13992 }
13993 }
13994
13995 pMem->flags = MEM_Int;
13996 pMem->u.i = pOp->p1; /* P1 */
13997 pMem++;
13998
13999 pMem->flags = MEM_Int;
14000 pMem->u.i = pOp->p2; /* P2 */
14001 pMem++;
14002
14003 pMem->flags = MEM_Int;
14004 pMem->u.i = pOp->p3; /* P3 */
14005 pMem++;
14006
14007 if( sqlite3VdbeMemClearAndResize(pMem, 100) ){ /* P4 */
14008 assert( p->db->mallocFailed );
14009 return SQLITE_ERROR;
14010 }
14011 pMem->flags = MEM_Str|MEM_Term;
14012 zP4 = displayP4(pOp, pMem->z, pMem->szMalloc);
14013 if( zP4!=pMem->z ){
14014 pMem->n = 0;
14015 sqlite3VdbeMemSetStr(pMem, zP4, -1, SQLITE_UTF8, 0);
14016 }else{
14017 assert( pMem->z!=0 );
14018 pMem->n = sqlite3Strlen30(pMem->z);
14019 pMem->enc = SQLITE_UTF8;
14020 }
14021 pMem++;
14022
14023 if( p->explain==1 ){
14024 if( sqlite3VdbeMemClearAndResize(pMem, 4) ){
14025 assert( p->db->mallocFailed );
14026 return SQLITE_ERROR;
14027 }
14028 pMem->flags = MEM_Str|MEM_Term;
14029 pMem->n = 2;
14030 sqlite3_snprintf(3, pMem->z, "%.2x", pOp->p5); /* P5 */
14031 pMem->enc = SQLITE_UTF8;
14032 pMem++;
14033
14034 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
14035 if( sqlite3VdbeMemClearAndResize(pMem, 500) ){
14036 assert( p->db->mallocFailed );
14037 return SQLITE_ERROR;
14038 }
14039 pMem->flags = MEM_Str|MEM_Term;
14040 pMem->n = displayComment(pOp, zP4, pMem->z, 500);
14041 pMem->enc = SQLITE_UTF8;
14042 #else
14043 pMem->flags = MEM_Null; /* Comment */
14044 #endif
14045 }
14046
14047 p->nResColumn = 8 - 4*(p->explain-1);
14048 p->pResultSet = &p->aMem[1];
14049 p->rc = SQLITE_OK;
14050 rc = SQLITE_ROW;
14051 }
14052 return rc;
14053 }
14054 #endif /* SQLITE_OMIT_EXPLAIN */
14055
14056 #ifdef SQLITE_DEBUG
14057 /*
14058 ** Print the SQL that was used to generate a VDBE program.
14059 */
14060 SQLITE_PRIVATE void sqlite3VdbePrintSql(Vdbe *p){
14061 const char *z = 0;
14062 if( p->zSql ){
14063 z = p->zSql;
14064 }else if( p->nOp>=1 ){
14065 const VdbeOp *pOp = &p->aOp[0];
14066 if( pOp->opcode==OP_Init && pOp->p4.z!=0 ){
14067 z = pOp->p4.z;
14068 while( sqlite3Isspace(*z) ) z++;
14069 }
14070 }
14071 if( z ) printf("SQL: [%s]\n", z);
14072 }
14073 #endif
14074
14075 #if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE)
14076 /*
14077 ** Print an IOTRACE message showing SQL content.
14078 */
14079 SQLITE_PRIVATE void sqlite3VdbeIOTraceSql(Vdbe *p){
14080 int nOp = p->nOp;
14081 VdbeOp *pOp;
14082 if( sqlite3IoTrace==0 ) return;
14083 if( nOp<1 ) return;
14084 pOp = &p->aOp[0];
14085 if( pOp->opcode==OP_Init && pOp->p4.z!=0 ){
14086 int i, j;
14087 char z[1000];
14088 sqlite3_snprintf(sizeof(z), z, "%s", pOp->p4.z);
14089 for(i=0; sqlite3Isspace(z[i]); i++){}
14090 for(j=0; z[i]; i++){
14091 if( sqlite3Isspace(z[i]) ){
14092 if( z[i-1]!=' ' ){
14093 z[j++] = ' ';
14094 }
14095 }else{
14096 z[j++] = z[i];
14097 }
14098 }
14099 z[j] = 0;
14100 sqlite3IoTrace("SQL %s\n", z);
14101 }
14102 }
14103 #endif /* !SQLITE_OMIT_TRACE && SQLITE_ENABLE_IOTRACE */
14104
14105 /* An instance of this object describes bulk memory available for use
14106 ** by subcomponents of a prepared statement. Space is allocated out
14107 ** of a ReusableSpace object by the allocSpace() routine below.
14108 */
14109 struct ReusableSpace {
14110 u8 *pSpace; /* Available memory */
14111 int nFree; /* Bytes of available memory */
14112 int nNeeded; /* Total bytes that could not be allocated */
14113 };
14114
14115 /* Try to allocate nByte bytes of 8-byte aligned bulk memory for pBuf
14116 ** from the ReusableSpace object. Return a pointer to the allocated
14117 ** memory on success. If insufficient memory is available in the
14118 ** ReusableSpace object, increase the ReusableSpace.nNeeded
14119 ** value by the amount needed and return NULL.
14120 **
14121 ** If pBuf is not initially NULL, that means that the memory has already
14122 ** been allocated by a prior call to this routine, so just return a copy
14123 ** of pBuf and leave ReusableSpace unchanged.
14124 **
14125 ** This allocator is employed to repurpose unused slots at the end of the
14126 ** opcode array of prepared state for other memory needs of the prepared
14127 ** statement.
14128 */
14129 static void *allocSpace(
14130 struct ReusableSpace *p, /* Bulk memory available for allocation */
14131 void *pBuf, /* Pointer to a prior allocation */
14132 int nByte /* Bytes of memory needed */
14133 ){
14134 assert( EIGHT_BYTE_ALIGNMENT(p->pSpace) );
14135 if( pBuf==0 ){
14136 nByte = ROUND8(nByte);
14137 if( nByte <= p->nFree ){
14138 p->nFree -= nByte;
14139 pBuf = &p->pSpace[p->nFree];
14140 }else{
14141 p->nNeeded += nByte;
14142 }
14143 }
14144 assert( EIGHT_BYTE_ALIGNMENT(pBuf) );
14145 return pBuf;
14146 }
14147
14148 /*
14149 ** Rewind the VDBE back to the beginning in preparation for
14150 ** running it.
14151 */
14152 SQLITE_PRIVATE void sqlite3VdbeRewind(Vdbe *p){
14153 #if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
14154 int i;
14155 #endif
14156 assert( p!=0 );
14157 assert( p->magic==VDBE_MAGIC_INIT || p->magic==VDBE_MAGIC_RESET );
14158
14159 /* There should be at least one opcode.
14160 */
14161 assert( p->nOp>0 );
14162
14163 /* Set the magic to VDBE_MAGIC_RUN sooner rather than later. */
14164 p->magic = VDBE_MAGIC_RUN;
14165
14166 #ifdef SQLITE_DEBUG
14167 for(i=0; i<p->nMem; i++){
14168 assert( p->aMem[i].db==p->db );
14169 }
14170 #endif
14171 p->pc = -1;
14172 p->rc = SQLITE_OK;
14173 p->errorAction = OE_Abort;
14174 p->nChange = 0;
14175 p->cacheCtr = 1;
14176 p->minWriteFileFormat = 255;
14177 p->iStatement = 0;
14178 p->nFkConstraint = 0;
14179 #ifdef VDBE_PROFILE
14180 for(i=0; i<p->nOp; i++){
14181 p->aOp[i].cnt = 0;
14182 p->aOp[i].cycles = 0;
14183 }
14184 #endif
14185 }
14186
14187 /*
14188 ** Prepare a virtual machine for execution for the first time after
14189 ** creating the virtual machine. This involves things such
14190 ** as allocating registers and initializing the program counter.
14191 ** After the VDBE has be prepped, it can be executed by one or more
14192 ** calls to sqlite3VdbeExec().
14193 **
14194 ** This function may be called exactly once on each virtual machine.
14195 ** After this routine is called the VM has been "packaged" and is ready
14196 ** to run. After this routine is called, further calls to
14197 ** sqlite3VdbeAddOp() functions are prohibited. This routine disconnects
14198 ** the Vdbe from the Parse object that helped generate it so that the
14199 ** the Vdbe becomes an independent entity and the Parse object can be
14200 ** destroyed.
14201 **
14202 ** Use the sqlite3VdbeRewind() procedure to restore a virtual machine back
14203 ** to its initial state after it has been run.
14204 */
14205 SQLITE_PRIVATE void sqlite3VdbeMakeReady(
14206 Vdbe *p, /* The VDBE */
14207 Parse *pParse /* Parsing context */
14208 ){
14209 sqlite3 *db; /* The database connection */
14210 int nVar; /* Number of parameters */
14211 int nMem; /* Number of VM memory registers */
14212 int nCursor; /* Number of cursors required */
14213 int nArg; /* Number of arguments in subprograms */
14214 int n; /* Loop counter */
14215 struct ReusableSpace x; /* Reusable bulk memory */
14216
14217 assert( p!=0 );
14218 assert( p->nOp>0 );
14219 assert( pParse!=0 );
14220 assert( p->magic==VDBE_MAGIC_INIT );
14221 assert( pParse==p->pParse );
14222 db = p->db;
14223 assert( db->mallocFailed==0 );
14224 nVar = pParse->nVar;
14225 nMem = pParse->nMem;
14226 nCursor = pParse->nTab;
14227 nArg = pParse->nMaxArg;
14228
14229 /* Each cursor uses a memory cell. The first cursor (cursor 0) can
14230 ** use aMem[0] which is not otherwise used by the VDBE program. Allocate
14231 ** space at the end of aMem[] for cursors 1 and greater.
14232 ** See also: allocateCursor().
14233 */
14234 nMem += nCursor;
14235 if( nCursor==0 && nMem>0 ) nMem++; /* Space for aMem[0] even if not used */
14236
14237 /* Figure out how much reusable memory is available at the end of the
14238 ** opcode array. This extra memory will be reallocated for other elements
14239 ** of the prepared statement.
14240 */
14241 n = ROUND8(sizeof(Op)*p->nOp); /* Bytes of opcode memory used */
14242 x.pSpace = &((u8*)p->aOp)[n]; /* Unused opcode memory */
14243 assert( EIGHT_BYTE_ALIGNMENT(x.pSpace) );
14244 x.nFree = ROUNDDOWN8(pParse->szOpAlloc - n); /* Bytes of unused memory */
14245 assert( x.nFree>=0 );
14246 assert( EIGHT_BYTE_ALIGNMENT(&x.pSpace[x.nFree]) );
14247
14248 resolveP2Values(p, &nArg);
14249 p->usesStmtJournal = (u8)(pParse->isMultiWrite && pParse->mayAbort);
14250 if( pParse->explain && nMem<10 ){
14251 nMem = 10;
14252 }
14253 p->expired = 0;
14254
14255 /* Memory for registers, parameters, cursor, etc, is allocated in one or two
14256 ** passes. On the first pass, we try to reuse unused memory at the
14257 ** end of the opcode array. If we are unable to satisfy all memory
14258 ** requirements by reusing the opcode array tail, then the second
14259 ** pass will fill in the remainder using a fresh memory allocation.
14260 **
14261 ** This two-pass approach that reuses as much memory as possible from
14262 ** the leftover memory at the end of the opcode array. This can significantly
14263 ** reduce the amount of memory held by a prepared statement.
14264 */
14265 do {
14266 x.nNeeded = 0;
14267 p->aMem = allocSpace(&x, p->aMem, nMem*sizeof(Mem));
14268 p->aVar = allocSpace(&x, p->aVar, nVar*sizeof(Mem));
14269 p->apArg = allocSpace(&x, p->apArg, nArg*sizeof(Mem*));
14270 p->apCsr = allocSpace(&x, p->apCsr, nCursor*sizeof(VdbeCursor*));
14271 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
14272 p->anExec = allocSpace(&x, p->anExec, p->nOp*sizeof(i64));
14273 #endif
14274 if( x.nNeeded==0 ) break;
14275 x.pSpace = p->pFree = sqlite3DbMallocRawNN(db, x.nNeeded);
14276 x.nFree = x.nNeeded;
14277 }while( !db->mallocFailed );
14278
14279 p->pVList = pParse->pVList;
14280 pParse->pVList = 0;
14281 p->explain = pParse->explain;
14282 if( db->mallocFailed ){
14283 p->nVar = 0;
14284 p->nCursor = 0;
14285 p->nMem = 0;
14286 }else{
14287 p->nCursor = nCursor;
14288 p->nVar = (ynVar)nVar;
14289 initMemArray(p->aVar, nVar, db, MEM_Null);
14290 p->nMem = nMem;
14291 initMemArray(p->aMem, nMem, db, MEM_Undefined);
14292 memset(p->apCsr, 0, nCursor*sizeof(VdbeCursor*));
14293 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
14294 memset(p->anExec, 0, p->nOp*sizeof(i64));
14295 #endif
14296 }
14297 sqlite3VdbeRewind(p);
14298 }
14299
14300 /*
14301 ** Close a VDBE cursor and release all the resources that cursor
14302 ** happens to hold.
14303 */
14304 SQLITE_PRIVATE void sqlite3VdbeFreeCursor(Vdbe *p, VdbeCursor *pCx){
14305 if( pCx==0 ){
14306 return;
14307 }
14308 assert( pCx->pBtx==0 || pCx->eCurType==CURTYPE_BTREE );
14309 switch( pCx->eCurType ){
14310 case CURTYPE_SORTER: {
14311 sqlite3VdbeSorterClose(p->db, pCx);
14312 break;
14313 }
14314 case CURTYPE_BTREE: {
14315 if( pCx->pBtx ){
14316 sqlite3BtreeClose(pCx->pBtx);
14317 /* The pCx->pCursor will be close automatically, if it exists, by
14318 ** the call above. */
14319 }else{
14320 assert( pCx->uc.pCursor!=0 );
14321 sqlite3BtreeCloseCursor(pCx->uc.pCursor);
14322 }
14323 break;
14324 }
14325 #ifndef SQLITE_OMIT_VIRTUALTABLE
14326 case CURTYPE_VTAB: {
14327 sqlite3_vtab_cursor *pVCur = pCx->uc.pVCur;
14328 const sqlite3_module *pModule = pVCur->pVtab->pModule;
14329 assert( pVCur->pVtab->nRef>0 );
14330 pVCur->pVtab->nRef--;
14331 pModule->xClose(pVCur);
14332 break;
14333 }
14334 #endif
14335 }
14336 }
14337
14338 /*
14339 ** Close all cursors in the current frame.
14340 */
14341 static void closeCursorsInFrame(Vdbe *p){
14342 if( p->apCsr ){
14343 int i;
14344 for(i=0; i<p->nCursor; i++){
14345 VdbeCursor *pC = p->apCsr[i];
14346 if( pC ){
14347 sqlite3VdbeFreeCursor(p, pC);
14348 p->apCsr[i] = 0;
14349 }
14350 }
14351 }
14352 }
14353
14354 /*
14355 ** Copy the values stored in the VdbeFrame structure to its Vdbe. This
14356 ** is used, for example, when a trigger sub-program is halted to restore
14357 ** control to the main program.
14358 */
14359 SQLITE_PRIVATE int sqlite3VdbeFrameRestore(VdbeFrame *pFrame){
14360 Vdbe *v = pFrame->v;
14361 closeCursorsInFrame(v);
14362 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
14363 v->anExec = pFrame->anExec;
14364 #endif
14365 v->aOp = pFrame->aOp;
14366 v->nOp = pFrame->nOp;
14367 v->aMem = pFrame->aMem;
14368 v->nMem = pFrame->nMem;
14369 v->apCsr = pFrame->apCsr;
14370 v->nCursor = pFrame->nCursor;
14371 v->db->lastRowid = pFrame->lastRowid;
14372 v->nChange = pFrame->nChange;
14373 v->db->nChange = pFrame->nDbChange;
14374 sqlite3VdbeDeleteAuxData(v->db, &v->pAuxData, -1, 0);
14375 v->pAuxData = pFrame->pAuxData;
14376 pFrame->pAuxData = 0;
14377 return pFrame->pc;
14378 }
14379
14380 /*
14381 ** Close all cursors.
14382 **
14383 ** Also release any dynamic memory held by the VM in the Vdbe.aMem memory
14384 ** cell array. This is necessary as the memory cell array may contain
14385 ** pointers to VdbeFrame objects, which may in turn contain pointers to
14386 ** open cursors.
14387 */
14388 static void closeAllCursors(Vdbe *p){
14389 if( p->pFrame ){
14390 VdbeFrame *pFrame;
14391 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
14392 sqlite3VdbeFrameRestore(pFrame);
14393 p->pFrame = 0;
14394 p->nFrame = 0;
14395 }
14396 assert( p->nFrame==0 );
14397 closeCursorsInFrame(p);
14398 if( p->aMem ){
14399 releaseMemArray(p->aMem, p->nMem);
14400 }
14401 while( p->pDelFrame ){
14402 VdbeFrame *pDel = p->pDelFrame;
14403 p->pDelFrame = pDel->pParent;
14404 sqlite3VdbeFrameDelete(pDel);
14405 }
14406
14407 /* Delete any auxdata allocations made by the VM */
14408 if( p->pAuxData ) sqlite3VdbeDeleteAuxData(p->db, &p->pAuxData, -1, 0);
14409 assert( p->pAuxData==0 );
14410 }
14411
14412 /*
14413 ** Clean up the VM after a single run.
14414 */
14415 static void Cleanup(Vdbe *p){
14416 sqlite3 *db = p->db;
14417
14418 #ifdef SQLITE_DEBUG
14419 /* Execute assert() statements to ensure that the Vdbe.apCsr[] and
14420 ** Vdbe.aMem[] arrays have already been cleaned up. */
14421 int i;
14422 if( p->apCsr ) for(i=0; i<p->nCursor; i++) assert( p->apCsr[i]==0 );
14423 if( p->aMem ){
14424 for(i=0; i<p->nMem; i++) assert( p->aMem[i].flags==MEM_Undefined );
14425 }
14426 #endif
14427
14428 sqlite3DbFree(db, p->zErrMsg);
14429 p->zErrMsg = 0;
14430 p->pResultSet = 0;
14431 }
14432
14433 /*
14434 ** Set the number of result columns that will be returned by this SQL
14435 ** statement. This is now set at compile time, rather than during
14436 ** execution of the vdbe program so that sqlite3_column_count() can
14437 ** be called on an SQL statement before sqlite3_step().
14438 */
14439 SQLITE_PRIVATE void sqlite3VdbeSetNumCols(Vdbe *p, int nResColumn){
14440 Mem *pColName;
14441 int n;
14442 sqlite3 *db = p->db;
14443
14444 releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
14445 sqlite3DbFree(db, p->aColName);
14446 n = nResColumn*COLNAME_N;
14447 p->nResColumn = (u16)nResColumn;
14448 p->aColName = pColName = (Mem*)sqlite3DbMallocRawNN(db, sizeof(Mem)*n );
14449 if( p->aColName==0 ) return;
14450 initMemArray(p->aColName, n, p->db, MEM_Null);
14451 }
14452
14453 /*
14454 ** Set the name of the idx'th column to be returned by the SQL statement.
14455 ** zName must be a pointer to a nul terminated string.
14456 **
14457 ** This call must be made after a call to sqlite3VdbeSetNumCols().
14458 **
14459 ** The final parameter, xDel, must be one of SQLITE_DYNAMIC, SQLITE_STATIC
14460 ** or SQLITE_TRANSIENT. If it is SQLITE_DYNAMIC, then the buffer pointed
14461 ** to by zName will be freed by sqlite3DbFree() when the vdbe is destroyed.
14462 */
14463 SQLITE_PRIVATE int sqlite3VdbeSetColName(
14464 Vdbe *p, /* Vdbe being configured */
14465 int idx, /* Index of column zName applies to */
14466 int var, /* One of the COLNAME_* constants */
14467 const char *zName, /* Pointer to buffer containing name */
14468 void (*xDel)(void*) /* Memory management strategy for zName */
14469 ){
14470 int rc;
14471 Mem *pColName;
14472 assert( idx<p->nResColumn );
14473 assert( var<COLNAME_N );
14474 if( p->db->mallocFailed ){
14475 assert( !zName || xDel!=SQLITE_DYNAMIC );
14476 return SQLITE_NOMEM_BKPT;
14477 }
14478 assert( p->aColName!=0 );
14479 pColName = &(p->aColName[idx+var*p->nResColumn]);
14480 rc = sqlite3VdbeMemSetStr(pColName, zName, -1, SQLITE_UTF8, xDel);
14481 assert( rc!=0 || !zName || (pColName->flags&MEM_Term)!=0 );
14482 return rc;
14483 }
14484
14485 /*
14486 ** A read or write transaction may or may not be active on database handle
14487 ** db. If a transaction is active, commit it. If there is a
14488 ** write-transaction spanning more than one database file, this routine
14489 ** takes care of the master journal trickery.
14490 */
14491 static int vdbeCommit(sqlite3 *db, Vdbe *p){
14492 int i;
14493 int nTrans = 0; /* Number of databases with an active write-transaction
14494 ** that are candidates for a two-phase commit using a
14495 ** master-journal */
14496 int rc = SQLITE_OK;
14497 int needXcommit = 0;
14498
14499 #ifdef SQLITE_OMIT_VIRTUALTABLE
14500 /* With this option, sqlite3VtabSync() is defined to be simply
14501 ** SQLITE_OK so p is not used.
14502 */
14503 UNUSED_PARAMETER(p);
14504 #endif
14505
14506 /* Before doing anything else, call the xSync() callback for any
14507 ** virtual module tables written in this transaction. This has to
14508 ** be done before determining whether a master journal file is
14509 ** required, as an xSync() callback may add an attached database
14510 ** to the transaction.
14511 */
14512 rc = sqlite3VtabSync(db, p);
14513
14514 /* This loop determines (a) if the commit hook should be invoked and
14515 ** (b) how many database files have open write transactions, not
14516 ** including the temp database. (b) is important because if more than
14517 ** one database file has an open write transaction, a master journal
14518 ** file is required for an atomic commit.
14519 */
14520 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
14521 Btree *pBt = db->aDb[i].pBt;
14522 if( sqlite3BtreeIsInTrans(pBt) ){
14523 /* Whether or not a database might need a master journal depends upon
14524 ** its journal mode (among other things). This matrix determines which
14525 ** journal modes use a master journal and which do not */
14526 static const u8 aMJNeeded[] = {
14527 /* DELETE */ 1,
14528 /* PERSIST */ 1,
14529 /* OFF */ 0,
14530 /* TRUNCATE */ 1,
14531 /* MEMORY */ 0,
14532 /* WAL */ 0
14533 };
14534 Pager *pPager; /* Pager associated with pBt */
14535 needXcommit = 1;
14536 sqlite3BtreeEnter(pBt);
14537 pPager = sqlite3BtreePager(pBt);
14538 if( db->aDb[i].safety_level!=PAGER_SYNCHRONOUS_OFF
14539 && aMJNeeded[sqlite3PagerGetJournalMode(pPager)]
14540 ){
14541 assert( i!=1 );
14542 nTrans++;
14543 }
14544 rc = sqlite3PagerExclusiveLock(pPager);
14545 sqlite3BtreeLeave(pBt);
14546 }
14547 }
14548 if( rc!=SQLITE_OK ){
14549 return rc;
14550 }
14551
14552 /* If there are any write-transactions at all, invoke the commit hook */
14553 if( needXcommit && db->xCommitCallback ){
14554 rc = db->xCommitCallback(db->pCommitArg);
14555 if( rc ){
14556 return SQLITE_CONSTRAINT_COMMITHOOK;
14557 }
14558 }
14559
14560 /* The simple case - no more than one database file (not counting the
14561 ** TEMP database) has a transaction active. There is no need for the
14562 ** master-journal.
14563 **
14564 ** If the return value of sqlite3BtreeGetFilename() is a zero length
14565 ** string, it means the main database is :memory: or a temp file. In
14566 ** that case we do not support atomic multi-file commits, so use the
14567 ** simple case then too.
14568 */
14569 if( 0==sqlite3Strlen30(sqlite3BtreeGetFilename(db->aDb[0].pBt))
14570 || nTrans<=1
14571 ){
14572 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
14573 Btree *pBt = db->aDb[i].pBt;
14574 if( pBt ){
14575 rc = sqlite3BtreeCommitPhaseOne(pBt, 0);
14576 }
14577 }
14578
14579 /* Do the commit only if all databases successfully complete phase 1.
14580 ** If one of the BtreeCommitPhaseOne() calls fails, this indicates an
14581 ** IO error while deleting or truncating a journal file. It is unlikely,
14582 ** but could happen. In this case abandon processing and return the error.
14583 */
14584 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
14585 Btree *pBt = db->aDb[i].pBt;
14586 if( pBt ){
14587 rc = sqlite3BtreeCommitPhaseTwo(pBt, 0);
14588 }
14589 }
14590 if( rc==SQLITE_OK ){
14591 sqlite3VtabCommit(db);
14592 }
14593 }
14594
14595 /* The complex case - There is a multi-file write-transaction active.
14596 ** This requires a master journal file to ensure the transaction is
14597 ** committed atomically.
14598 */
14599 #ifndef SQLITE_OMIT_DISKIO
14600 else{
14601 sqlite3_vfs *pVfs = db->pVfs;
14602 char *zMaster = 0; /* File-name for the master journal */
14603 char const *zMainFile = sqlite3BtreeGetFilename(db->aDb[0].pBt);
14604 sqlite3_file *pMaster = 0;
14605 i64 offset = 0;
14606 int res;
14607 int retryCount = 0;
14608 int nMainFile;
14609
14610 /* Select a master journal file name */
14611 nMainFile = sqlite3Strlen30(zMainFile);
14612 zMaster = sqlite3MPrintf(db, "%s-mjXXXXXX9XXz", zMainFile);
14613 if( zMaster==0 ) return SQLITE_NOMEM_BKPT;
14614 do {
14615 u32 iRandom;
14616 if( retryCount ){
14617 if( retryCount>100 ){
14618 sqlite3_log(SQLITE_FULL, "MJ delete: %s", zMaster);
14619 sqlite3OsDelete(pVfs, zMaster, 0);
14620 break;
14621 }else if( retryCount==1 ){
14622 sqlite3_log(SQLITE_FULL, "MJ collide: %s", zMaster);
14623 }
14624 }
14625 retryCount++;
14626 sqlite3_randomness(sizeof(iRandom), &iRandom);
14627 sqlite3_snprintf(13, &zMaster[nMainFile], "-mj%06X9%02X",
14628 (iRandom>>8)&0xffffff, iRandom&0xff);
14629 /* The antipenultimate character of the master journal name must
14630 ** be "9" to avoid name collisions when using 8+3 filenames. */
14631 assert( zMaster[sqlite3Strlen30(zMaster)-3]=='9' );
14632 sqlite3FileSuffix3(zMainFile, zMaster);
14633 rc = sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS, &res);
14634 }while( rc==SQLITE_OK && res );
14635 if( rc==SQLITE_OK ){
14636 /* Open the master journal. */
14637 rc = sqlite3OsOpenMalloc(pVfs, zMaster, &pMaster,
14638 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|
14639 SQLITE_OPEN_EXCLUSIVE|SQLITE_OPEN_MASTER_JOURNAL, 0
14640 );
14641 }
14642 if( rc!=SQLITE_OK ){
14643 sqlite3DbFree(db, zMaster);
14644 return rc;
14645 }
14646
14647 /* Write the name of each database file in the transaction into the new
14648 ** master journal file. If an error occurs at this point close
14649 ** and delete the master journal file. All the individual journal files
14650 ** still have 'null' as the master journal pointer, so they will roll
14651 ** back independently if a failure occurs.
14652 */
14653 for(i=0; i<db->nDb; i++){
14654 Btree *pBt = db->aDb[i].pBt;
14655 if( sqlite3BtreeIsInTrans(pBt) ){
14656 char const *zFile = sqlite3BtreeGetJournalname(pBt);
14657 if( zFile==0 ){
14658 continue; /* Ignore TEMP and :memory: databases */
14659 }
14660 assert( zFile[0]!=0 );
14661 rc = sqlite3OsWrite(pMaster, zFile, sqlite3Strlen30(zFile)+1, offset);
14662 offset += sqlite3Strlen30(zFile)+1;
14663 if( rc!=SQLITE_OK ){
14664 sqlite3OsCloseFree(pMaster);
14665 sqlite3OsDelete(pVfs, zMaster, 0);
14666 sqlite3DbFree(db, zMaster);
14667 return rc;
14668 }
14669 }
14670 }
14671
14672 /* Sync the master journal file. If the IOCAP_SEQUENTIAL device
14673 ** flag is set this is not required.
14674 */
14675 if( 0==(sqlite3OsDeviceCharacteristics(pMaster)&SQLITE_IOCAP_SEQUENTIAL)
14676 && SQLITE_OK!=(rc = sqlite3OsSync(pMaster, SQLITE_SYNC_NORMAL))
14677 ){
14678 sqlite3OsCloseFree(pMaster);
14679 sqlite3OsDelete(pVfs, zMaster, 0);
14680 sqlite3DbFree(db, zMaster);
14681 return rc;
14682 }
14683
14684 /* Sync all the db files involved in the transaction. The same call
14685 ** sets the master journal pointer in each individual journal. If
14686 ** an error occurs here, do not delete the master journal file.
14687 **
14688 ** If the error occurs during the first call to
14689 ** sqlite3BtreeCommitPhaseOne(), then there is a chance that the
14690 ** master journal file will be orphaned. But we cannot delete it,
14691 ** in case the master journal file name was written into the journal
14692 ** file before the failure occurred.
14693 */
14694 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
14695 Btree *pBt = db->aDb[i].pBt;
14696 if( pBt ){
14697 rc = sqlite3BtreeCommitPhaseOne(pBt, zMaster);
14698 }
14699 }
14700 sqlite3OsCloseFree(pMaster);
14701 assert( rc!=SQLITE_BUSY );
14702 if( rc!=SQLITE_OK ){
14703 sqlite3DbFree(db, zMaster);
14704 return rc;
14705 }
14706
14707 /* Delete the master journal file. This commits the transaction. After
14708 ** doing this the directory is synced again before any individual
14709 ** transaction files are deleted.
14710 */
14711 rc = sqlite3OsDelete(pVfs, zMaster, 1);
14712 sqlite3DbFree(db, zMaster);
14713 zMaster = 0;
14714 if( rc ){
14715 return rc;
14716 }
14717
14718 /* All files and directories have already been synced, so the following
14719 ** calls to sqlite3BtreeCommitPhaseTwo() are only closing files and
14720 ** deleting or truncating journals. If something goes wrong while
14721 ** this is happening we don't really care. The integrity of the
14722 ** transaction is already guaranteed, but some stray 'cold' journals
14723 ** may be lying around. Returning an error code won't help matters.
14724 */
14725 disable_simulated_io_errors();
14726 sqlite3BeginBenignMalloc();
14727 for(i=0; i<db->nDb; i++){
14728 Btree *pBt = db->aDb[i].pBt;
14729 if( pBt ){
14730 sqlite3BtreeCommitPhaseTwo(pBt, 1);
14731 }
14732 }
14733 sqlite3EndBenignMalloc();
14734 enable_simulated_io_errors();
14735
14736 sqlite3VtabCommit(db);
14737 }
14738 #endif
14739
14740 return rc;
14741 }
14742
14743 /*
14744 ** This routine checks that the sqlite3.nVdbeActive count variable
14745 ** matches the number of vdbe's in the list sqlite3.pVdbe that are
14746 ** currently active. An assertion fails if the two counts do not match.
14747 ** This is an internal self-check only - it is not an essential processing
14748 ** step.
14749 **
14750 ** This is a no-op if NDEBUG is defined.
14751 */
14752 #ifndef NDEBUG
14753 static void checkActiveVdbeCnt(sqlite3 *db){
14754 Vdbe *p;
14755 int cnt = 0;
14756 int nWrite = 0;
14757 int nRead = 0;
14758 p = db->pVdbe;
14759 while( p ){
14760 if( sqlite3_stmt_busy((sqlite3_stmt*)p) ){
14761 cnt++;
14762 if( p->readOnly==0 ) nWrite++;
14763 if( p->bIsReader ) nRead++;
14764 }
14765 p = p->pNext;
14766 }
14767 assert( cnt==db->nVdbeActive );
14768 assert( nWrite==db->nVdbeWrite );
14769 assert( nRead==db->nVdbeRead );
14770 }
14771 #else
14772 #define checkActiveVdbeCnt(x)
14773 #endif
14774
14775 /*
14776 ** If the Vdbe passed as the first argument opened a statement-transaction,
14777 ** close it now. Argument eOp must be either SAVEPOINT_ROLLBACK or
14778 ** SAVEPOINT_RELEASE. If it is SAVEPOINT_ROLLBACK, then the statement
14779 ** transaction is rolled back. If eOp is SAVEPOINT_RELEASE, then the
14780 ** statement transaction is committed.
14781 **
14782 ** If an IO error occurs, an SQLITE_IOERR_XXX error code is returned.
14783 ** Otherwise SQLITE_OK.
14784 */
14785 static SQLITE_NOINLINE int vdbeCloseStatement(Vdbe *p, int eOp){
14786 sqlite3 *const db = p->db;
14787 int rc = SQLITE_OK;
14788 int i;
14789 const int iSavepoint = p->iStatement-1;
14790
14791 assert( eOp==SAVEPOINT_ROLLBACK || eOp==SAVEPOINT_RELEASE);
14792 assert( db->nStatement>0 );
14793 assert( p->iStatement==(db->nStatement+db->nSavepoint) );
14794
14795 for(i=0; i<db->nDb; i++){
14796 int rc2 = SQLITE_OK;
14797 Btree *pBt = db->aDb[i].pBt;
14798 if( pBt ){
14799 if( eOp==SAVEPOINT_ROLLBACK ){
14800 rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_ROLLBACK, iSavepoint);
14801 }
14802 if( rc2==SQLITE_OK ){
14803 rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_RELEASE, iSavepoint);
14804 }
14805 if( rc==SQLITE_OK ){
14806 rc = rc2;
14807 }
14808 }
14809 }
14810 db->nStatement--;
14811 p->iStatement = 0;
14812
14813 if( rc==SQLITE_OK ){
14814 if( eOp==SAVEPOINT_ROLLBACK ){
14815 rc = sqlite3VtabSavepoint(db, SAVEPOINT_ROLLBACK, iSavepoint);
14816 }
14817 if( rc==SQLITE_OK ){
14818 rc = sqlite3VtabSavepoint(db, SAVEPOINT_RELEASE, iSavepoint);
14819 }
14820 }
14821
14822 /* If the statement transaction is being rolled back, also restore the
14823 ** database handles deferred constraint counter to the value it had when
14824 ** the statement transaction was opened. */
14825 if( eOp==SAVEPOINT_ROLLBACK ){
14826 db->nDeferredCons = p->nStmtDefCons;
14827 db->nDeferredImmCons = p->nStmtDefImmCons;
14828 }
14829 return rc;
14830 }
14831 SQLITE_PRIVATE int sqlite3VdbeCloseStatement(Vdbe *p, int eOp){
14832 if( p->db->nStatement && p->iStatement ){
14833 return vdbeCloseStatement(p, eOp);
14834 }
14835 return SQLITE_OK;
14836 }
14837
14838
14839 /*
14840 ** This function is called when a transaction opened by the database
14841 ** handle associated with the VM passed as an argument is about to be
14842 ** committed. If there are outstanding deferred foreign key constraint
14843 ** violations, return SQLITE_ERROR. Otherwise, SQLITE_OK.
14844 **
14845 ** If there are outstanding FK violations and this function returns
14846 ** SQLITE_ERROR, set the result of the VM to SQLITE_CONSTRAINT_FOREIGNKEY
14847 ** and write an error message to it. Then return SQLITE_ERROR.
14848 */
14849 #ifndef SQLITE_OMIT_FOREIGN_KEY
14850 SQLITE_PRIVATE int sqlite3VdbeCheckFk(Vdbe *p, int deferred){
14851 sqlite3 *db = p->db;
14852 if( (deferred && (db->nDeferredCons+db->nDeferredImmCons)>0)
14853 || (!deferred && p->nFkConstraint>0)
14854 ){
14855 p->rc = SQLITE_CONSTRAINT_FOREIGNKEY;
14856 p->errorAction = OE_Abort;
14857 sqlite3VdbeError(p, "FOREIGN KEY constraint failed");
14858 return SQLITE_ERROR;
14859 }
14860 return SQLITE_OK;
14861 }
14862 #endif
14863
14864 /*
14865 ** This routine is called the when a VDBE tries to halt. If the VDBE
14866 ** has made changes and is in autocommit mode, then commit those
14867 ** changes. If a rollback is needed, then do the rollback.
14868 **
14869 ** This routine is the only way to move the state of a VM from
14870 ** SQLITE_MAGIC_RUN to SQLITE_MAGIC_HALT. It is harmless to
14871 ** call this on a VM that is in the SQLITE_MAGIC_HALT state.
14872 **
14873 ** Return an error code. If the commit could not complete because of
14874 ** lock contention, return SQLITE_BUSY. If SQLITE_BUSY is returned, it
14875 ** means the close did not happen and needs to be repeated.
14876 */
14877 SQLITE_PRIVATE int sqlite3VdbeHalt(Vdbe *p){
14878 int rc; /* Used to store transient return codes */
14879 sqlite3 *db = p->db;
14880
14881 /* This function contains the logic that determines if a statement or
14882 ** transaction will be committed or rolled back as a result of the
14883 ** execution of this virtual machine.
14884 **
14885 ** If any of the following errors occur:
14886 **
14887 ** SQLITE_NOMEM
14888 ** SQLITE_IOERR
14889 ** SQLITE_FULL
14890 ** SQLITE_INTERRUPT
14891 **
14892 ** Then the internal cache might have been left in an inconsistent
14893 ** state. We need to rollback the statement transaction, if there is
14894 ** one, or the complete transaction if there is no statement transaction.
14895 */
14896
14897 if( db->mallocFailed ){
14898 p->rc = SQLITE_NOMEM_BKPT;
14899 }
14900 closeAllCursors(p);
14901 if( p->magic!=VDBE_MAGIC_RUN ){
14902 return SQLITE_OK;
14903 }
14904 checkActiveVdbeCnt(db);
14905
14906 /* No commit or rollback needed if the program never started or if the
14907 ** SQL statement does not read or write a database file. */
14908 if( p->pc>=0 && p->bIsReader ){
14909 int mrc; /* Primary error code from p->rc */
14910 int eStatementOp = 0;
14911 int isSpecialError; /* Set to true if a 'special' error */
14912
14913 /* Lock all btrees used by the statement */
14914 sqlite3VdbeEnter(p);
14915
14916 /* Check for one of the special errors */
14917 mrc = p->rc & 0xff;
14918 isSpecialError = mrc==SQLITE_NOMEM || mrc==SQLITE_IOERR
14919 || mrc==SQLITE_INTERRUPT || mrc==SQLITE_FULL;
14920 if( isSpecialError ){
14921 /* If the query was read-only and the error code is SQLITE_INTERRUPT,
14922 ** no rollback is necessary. Otherwise, at least a savepoint
14923 ** transaction must be rolled back to restore the database to a
14924 ** consistent state.
14925 **
14926 ** Even if the statement is read-only, it is important to perform
14927 ** a statement or transaction rollback operation. If the error
14928 ** occurred while writing to the journal, sub-journal or database
14929 ** file as part of an effort to free up cache space (see function
14930 ** pagerStress() in pager.c), the rollback is required to restore
14931 ** the pager to a consistent state.
14932 */
14933 if( !p->readOnly || mrc!=SQLITE_INTERRUPT ){
14934 if( (mrc==SQLITE_NOMEM || mrc==SQLITE_FULL) && p->usesStmtJournal ){
14935 eStatementOp = SAVEPOINT_ROLLBACK;
14936 }else{
14937 /* We are forced to roll back the active transaction. Before doing
14938 ** so, abort any other statements this handle currently has active.
14939 */
14940 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
14941 sqlite3CloseSavepoints(db);
14942 db->autoCommit = 1;
14943 p->nChange = 0;
14944 }
14945 }
14946 }
14947
14948 /* Check for immediate foreign key violations. */
14949 if( p->rc==SQLITE_OK ){
14950 sqlite3VdbeCheckFk(p, 0);
14951 }
14952
14953 /* If the auto-commit flag is set and this is the only active writer
14954 ** VM, then we do either a commit or rollback of the current transaction.
14955 **
14956 ** Note: This block also runs if one of the special errors handled
14957 ** above has occurred.
14958 */
14959 if( !sqlite3VtabInSync(db)
14960 && db->autoCommit
14961 && db->nVdbeWrite==(p->readOnly==0)
14962 ){
14963 if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){
14964 rc = sqlite3VdbeCheckFk(p, 1);
14965 if( rc!=SQLITE_OK ){
14966 if( NEVER(p->readOnly) ){
14967 sqlite3VdbeLeave(p);
14968 return SQLITE_ERROR;
14969 }
14970 rc = SQLITE_CONSTRAINT_FOREIGNKEY;
14971 }else{
14972 /* The auto-commit flag is true, the vdbe program was successful
14973 ** or hit an 'OR FAIL' constraint and there are no deferred foreign
14974 ** key constraints to hold up the transaction. This means a commit
14975 ** is required. */
14976 rc = vdbeCommit(db, p);
14977 }
14978 if( rc==SQLITE_BUSY && p->readOnly ){
14979 sqlite3VdbeLeave(p);
14980 return SQLITE_BUSY;
14981 }else if( rc!=SQLITE_OK ){
14982 p->rc = rc;
14983 sqlite3RollbackAll(db, SQLITE_OK);
14984 p->nChange = 0;
14985 }else{
14986 db->nDeferredCons = 0;
14987 db->nDeferredImmCons = 0;
14988 db->flags &= ~SQLITE_DeferFKs;
14989 sqlite3CommitInternalChanges(db);
14990 }
14991 }else{
14992 sqlite3RollbackAll(db, SQLITE_OK);
14993 p->nChange = 0;
14994 }
14995 db->nStatement = 0;
14996 }else if( eStatementOp==0 ){
14997 if( p->rc==SQLITE_OK || p->errorAction==OE_Fail ){
14998 eStatementOp = SAVEPOINT_RELEASE;
14999 }else if( p->errorAction==OE_Abort ){
15000 eStatementOp = SAVEPOINT_ROLLBACK;
15001 }else{
15002 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
15003 sqlite3CloseSavepoints(db);
15004 db->autoCommit = 1;
15005 p->nChange = 0;
15006 }
15007 }
15008
15009 /* If eStatementOp is non-zero, then a statement transaction needs to
15010 ** be committed or rolled back. Call sqlite3VdbeCloseStatement() to
15011 ** do so. If this operation returns an error, and the current statement
15012 ** error code is SQLITE_OK or SQLITE_CONSTRAINT, then promote the
15013 ** current statement error code.
15014 */
15015 if( eStatementOp ){
15016 rc = sqlite3VdbeCloseStatement(p, eStatementOp);
15017 if( rc ){
15018 if( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT ){
15019 p->rc = rc;
15020 sqlite3DbFree(db, p->zErrMsg);
15021 p->zErrMsg = 0;
15022 }
15023 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
15024 sqlite3CloseSavepoints(db);
15025 db->autoCommit = 1;
15026 p->nChange = 0;
15027 }
15028 }
15029
15030 /* If this was an INSERT, UPDATE or DELETE and no statement transaction
15031 ** has been rolled back, update the database connection change-counter.
15032 */
15033 if( p->changeCntOn ){
15034 if( eStatementOp!=SAVEPOINT_ROLLBACK ){
15035 sqlite3VdbeSetChanges(db, p->nChange);
15036 }else{
15037 sqlite3VdbeSetChanges(db, 0);
15038 }
15039 p->nChange = 0;
15040 }
15041
15042 /* Release the locks */
15043 sqlite3VdbeLeave(p);
15044 }
15045
15046 /* We have successfully halted and closed the VM. Record this fact. */
15047 if( p->pc>=0 ){
15048 db->nVdbeActive--;
15049 if( !p->readOnly ) db->nVdbeWrite--;
15050 if( p->bIsReader ) db->nVdbeRead--;
15051 assert( db->nVdbeActive>=db->nVdbeRead );
15052 assert( db->nVdbeRead>=db->nVdbeWrite );
15053 assert( db->nVdbeWrite>=0 );
15054 }
15055 p->magic = VDBE_MAGIC_HALT;
15056 checkActiveVdbeCnt(db);
15057 if( db->mallocFailed ){
15058 p->rc = SQLITE_NOMEM_BKPT;
15059 }
15060
15061 /* If the auto-commit flag is set to true, then any locks that were held
15062 ** by connection db have now been released. Call sqlite3ConnectionUnlocked()
15063 ** to invoke any required unlock-notify callbacks.
15064 */
15065 if( db->autoCommit ){
15066 sqlite3ConnectionUnlocked(db);
15067 }
15068
15069 assert( db->nVdbeActive>0 || db->autoCommit==0 || db->nStatement==0 );
15070 return (p->rc==SQLITE_BUSY ? SQLITE_BUSY : SQLITE_OK);
15071 }
15072
15073
15074 /*
15075 ** Each VDBE holds the result of the most recent sqlite3_step() call
15076 ** in p->rc. This routine sets that result back to SQLITE_OK.
15077 */
15078 SQLITE_PRIVATE void sqlite3VdbeResetStepResult(Vdbe *p){
15079 p->rc = SQLITE_OK;
15080 }
15081
15082 /*
15083 ** Copy the error code and error message belonging to the VDBE passed
15084 ** as the first argument to its database handle (so that they will be
15085 ** returned by calls to sqlite3_errcode() and sqlite3_errmsg()).
15086 **
15087 ** This function does not clear the VDBE error code or message, just
15088 ** copies them to the database handle.
15089 */
15090 SQLITE_PRIVATE int sqlite3VdbeTransferError(Vdbe *p){
15091 sqlite3 *db = p->db;
15092 int rc = p->rc;
15093 if( p->zErrMsg ){
15094 db->bBenignMalloc++;
15095 sqlite3BeginBenignMalloc();
15096 if( db->pErr==0 ) db->pErr = sqlite3ValueNew(db);
15097 sqlite3ValueSetStr(db->pErr, -1, p->zErrMsg, SQLITE_UTF8, SQLITE_TRANSIENT);
15098 sqlite3EndBenignMalloc();
15099 db->bBenignMalloc--;
15100 db->errCode = rc;
15101 }else{
15102 sqlite3Error(db, rc);
15103 }
15104 return rc;
15105 }
15106
15107 #ifdef SQLITE_ENABLE_SQLLOG
15108 /*
15109 ** If an SQLITE_CONFIG_SQLLOG hook is registered and the VM has been run,
15110 ** invoke it.
15111 */
15112 static void vdbeInvokeSqllog(Vdbe *v){
15113 if( sqlite3GlobalConfig.xSqllog && v->rc==SQLITE_OK && v->zSql && v->pc>=0 ){
15114 char *zExpanded = sqlite3VdbeExpandSql(v, v->zSql);
15115 assert( v->db->init.busy==0 );
15116 if( zExpanded ){
15117 sqlite3GlobalConfig.xSqllog(
15118 sqlite3GlobalConfig.pSqllogArg, v->db, zExpanded, 1
15119 );
15120 sqlite3DbFree(v->db, zExpanded);
15121 }
15122 }
15123 }
15124 #else
15125 # define vdbeInvokeSqllog(x)
15126 #endif
15127
15128 /*
15129 ** Clean up a VDBE after execution but do not delete the VDBE just yet.
15130 ** Write any error messages into *pzErrMsg. Return the result code.
15131 **
15132 ** After this routine is run, the VDBE should be ready to be executed
15133 ** again.
15134 **
15135 ** To look at it another way, this routine resets the state of the
15136 ** virtual machine from VDBE_MAGIC_RUN or VDBE_MAGIC_HALT back to
15137 ** VDBE_MAGIC_INIT.
15138 */
15139 SQLITE_PRIVATE int sqlite3VdbeReset(Vdbe *p){
15140 sqlite3 *db;
15141 db = p->db;
15142
15143 /* If the VM did not run to completion or if it encountered an
15144 ** error, then it might not have been halted properly. So halt
15145 ** it now.
15146 */
15147 sqlite3VdbeHalt(p);
15148
15149 /* If the VDBE has be run even partially, then transfer the error code
15150 ** and error message from the VDBE into the main database structure. But
15151 ** if the VDBE has just been set to run but has not actually executed any
15152 ** instructions yet, leave the main database error information unchanged.
15153 */
15154 if( p->pc>=0 ){
15155 vdbeInvokeSqllog(p);
15156 sqlite3VdbeTransferError(p);
15157 sqlite3DbFree(db, p->zErrMsg);
15158 p->zErrMsg = 0;
15159 if( p->runOnlyOnce ) p->expired = 1;
15160 }else if( p->rc && p->expired ){
15161 /* The expired flag was set on the VDBE before the first call
15162 ** to sqlite3_step(). For consistency (since sqlite3_step() was
15163 ** called), set the database error in this case as well.
15164 */
15165 sqlite3ErrorWithMsg(db, p->rc, p->zErrMsg ? "%s" : 0, p->zErrMsg);
15166 sqlite3DbFree(db, p->zErrMsg);
15167 p->zErrMsg = 0;
15168 }
15169
15170 /* Reclaim all memory used by the VDBE
15171 */
15172 Cleanup(p);
15173
15174 /* Save profiling information from this VDBE run.
15175 */
15176 #ifdef VDBE_PROFILE
15177 {
15178 FILE *out = fopen("vdbe_profile.out", "a");
15179 if( out ){
15180 int i;
15181 fprintf(out, "---- ");
15182 for(i=0; i<p->nOp; i++){
15183 fprintf(out, "%02x", p->aOp[i].opcode);
15184 }
15185 fprintf(out, "\n");
15186 if( p->zSql ){
15187 char c, pc = 0;
15188 fprintf(out, "-- ");
15189 for(i=0; (c = p->zSql[i])!=0; i++){
15190 if( pc=='\n' ) fprintf(out, "-- ");
15191 putc(c, out);
15192 pc = c;
15193 }
15194 if( pc!='\n' ) fprintf(out, "\n");
15195 }
15196 for(i=0; i<p->nOp; i++){
15197 char zHdr[100];
15198 sqlite3_snprintf(sizeof(zHdr), zHdr, "%6u %12llu %8llu ",
15199 p->aOp[i].cnt,
15200 p->aOp[i].cycles,
15201 p->aOp[i].cnt>0 ? p->aOp[i].cycles/p->aOp[i].cnt : 0
15202 );
15203 fprintf(out, "%s", zHdr);
15204 sqlite3VdbePrintOp(out, i, &p->aOp[i]);
15205 }
15206 fclose(out);
15207 }
15208 }
15209 #endif
15210 p->iCurrentTime = 0;
15211 p->magic = VDBE_MAGIC_RESET;
15212 return p->rc & db->errMask;
15213 }
15214
15215 /*
15216 ** Clean up and delete a VDBE after execution. Return an integer which is
15217 ** the result code. Write any error message text into *pzErrMsg.
15218 */
15219 SQLITE_PRIVATE int sqlite3VdbeFinalize(Vdbe *p){
15220 int rc = SQLITE_OK;
15221 if( p->magic==VDBE_MAGIC_RUN || p->magic==VDBE_MAGIC_HALT ){
15222 rc = sqlite3VdbeReset(p);
15223 assert( (rc & p->db->errMask)==rc );
15224 }
15225 sqlite3VdbeDelete(p);
15226 return rc;
15227 }
15228
15229 /*
15230 ** If parameter iOp is less than zero, then invoke the destructor for
15231 ** all auxiliary data pointers currently cached by the VM passed as
15232 ** the first argument.
15233 **
15234 ** Or, if iOp is greater than or equal to zero, then the destructor is
15235 ** only invoked for those auxiliary data pointers created by the user
15236 ** function invoked by the OP_Function opcode at instruction iOp of
15237 ** VM pVdbe, and only then if:
15238 **
15239 ** * the associated function parameter is the 32nd or later (counting
15240 ** from left to right), or
15241 **
15242 ** * the corresponding bit in argument mask is clear (where the first
15243 ** function parameter corresponds to bit 0 etc.).
15244 */
15245 SQLITE_PRIVATE void sqlite3VdbeDeleteAuxData(sqlite3 *db, AuxData **pp, int iOp, int mask){
15246 while( *pp ){
15247 AuxData *pAux = *pp;
15248 if( (iOp<0)
15249 || (pAux->iOp==iOp && (pAux->iArg>31 || !(mask & MASKBIT32(pAux->iArg))))
15250 ){
15251 testcase( pAux->iArg==31 );
15252 if( pAux->xDelete ){
15253 pAux->xDelete(pAux->pAux);
15254 }
15255 *pp = pAux->pNext;
15256 sqlite3DbFree(db, pAux);
15257 }else{
15258 pp= &pAux->pNext;
15259 }
15260 }
15261 }
15262
15263 /*
15264 ** Free all memory associated with the Vdbe passed as the second argument,
15265 ** except for object itself, which is preserved.
15266 **
15267 ** The difference between this function and sqlite3VdbeDelete() is that
15268 ** VdbeDelete() also unlinks the Vdbe from the list of VMs associated with
15269 ** the database connection and frees the object itself.
15270 */
15271 SQLITE_PRIVATE void sqlite3VdbeClearObject(sqlite3 *db, Vdbe *p){
15272 SubProgram *pSub, *pNext;
15273 assert( p->db==0 || p->db==db );
15274 releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
15275 for(pSub=p->pProgram; pSub; pSub=pNext){
15276 pNext = pSub->pNext;
15277 vdbeFreeOpArray(db, pSub->aOp, pSub->nOp);
15278 sqlite3DbFree(db, pSub);
15279 }
15280 if( p->magic!=VDBE_MAGIC_INIT ){
15281 releaseMemArray(p->aVar, p->nVar);
15282 sqlite3DbFree(db, p->pVList);
15283 sqlite3DbFree(db, p->pFree);
15284 }
15285 vdbeFreeOpArray(db, p->aOp, p->nOp);
15286 sqlite3DbFree(db, p->aColName);
15287 sqlite3DbFree(db, p->zSql);
15288 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
15289 {
15290 int i;
15291 for(i=0; i<p->nScan; i++){
15292 sqlite3DbFree(db, p->aScan[i].zName);
15293 }
15294 sqlite3DbFree(db, p->aScan);
15295 }
15296 #endif
15297 }
15298
15299 /*
15300 ** Delete an entire VDBE.
15301 */
15302 SQLITE_PRIVATE void sqlite3VdbeDelete(Vdbe *p){
15303 sqlite3 *db;
15304
15305 if( NEVER(p==0) ) return;
15306 db = p->db;
15307 assert( sqlite3_mutex_held(db->mutex) );
15308 sqlite3VdbeClearObject(db, p);
15309 if( p->pPrev ){
15310 p->pPrev->pNext = p->pNext;
15311 }else{
15312 assert( db->pVdbe==p );
15313 db->pVdbe = p->pNext;
15314 }
15315 if( p->pNext ){
15316 p->pNext->pPrev = p->pPrev;
15317 }
15318 p->magic = VDBE_MAGIC_DEAD;
15319 p->db = 0;
15320 sqlite3DbFree(db, p);
15321 }
15322
15323 /*
15324 ** The cursor "p" has a pending seek operation that has not yet been
15325 ** carried out. Seek the cursor now. If an error occurs, return
15326 ** the appropriate error code.
15327 */
15328 static int SQLITE_NOINLINE handleDeferredMoveto(VdbeCursor *p){
15329 int res, rc;
15330 #ifdef SQLITE_TEST
15331 extern int sqlite3_search_count;
15332 #endif
15333 assert( p->deferredMoveto );
15334 assert( p->isTable );
15335 assert( p->eCurType==CURTYPE_BTREE );
15336 rc = sqlite3BtreeMovetoUnpacked(p->uc.pCursor, 0, p->movetoTarget, 0, &res);
15337 if( rc ) return rc;
15338 if( res!=0 ) return SQLITE_CORRUPT_BKPT;
15339 #ifdef SQLITE_TEST
15340 sqlite3_search_count++;
15341 #endif
15342 p->deferredMoveto = 0;
15343 p->cacheStatus = CACHE_STALE;
15344 return SQLITE_OK;
15345 }
15346
15347 /*
15348 ** Something has moved cursor "p" out of place. Maybe the row it was
15349 ** pointed to was deleted out from under it. Or maybe the btree was
15350 ** rebalanced. Whatever the cause, try to restore "p" to the place it
15351 ** is supposed to be pointing. If the row was deleted out from under the
15352 ** cursor, set the cursor to point to a NULL row.
15353 */
15354 static int SQLITE_NOINLINE handleMovedCursor(VdbeCursor *p){
15355 int isDifferentRow, rc;
15356 assert( p->eCurType==CURTYPE_BTREE );
15357 assert( p->uc.pCursor!=0 );
15358 assert( sqlite3BtreeCursorHasMoved(p->uc.pCursor) );
15359 rc = sqlite3BtreeCursorRestore(p->uc.pCursor, &isDifferentRow);
15360 p->cacheStatus = CACHE_STALE;
15361 if( isDifferentRow ) p->nullRow = 1;
15362 return rc;
15363 }
15364
15365 /*
15366 ** Check to ensure that the cursor is valid. Restore the cursor
15367 ** if need be. Return any I/O error from the restore operation.
15368 */
15369 SQLITE_PRIVATE int sqlite3VdbeCursorRestore(VdbeCursor *p){
15370 assert( p->eCurType==CURTYPE_BTREE );
15371 if( sqlite3BtreeCursorHasMoved(p->uc.pCursor) ){
15372 return handleMovedCursor(p);
15373 }
15374 return SQLITE_OK;
15375 }
15376
15377 /*
15378 ** Make sure the cursor p is ready to read or write the row to which it
15379 ** was last positioned. Return an error code if an OOM fault or I/O error
15380 ** prevents us from positioning the cursor to its correct position.
15381 **
15382 ** If a MoveTo operation is pending on the given cursor, then do that
15383 ** MoveTo now. If no move is pending, check to see if the row has been
15384 ** deleted out from under the cursor and if it has, mark the row as
15385 ** a NULL row.
15386 **
15387 ** If the cursor is already pointing to the correct row and that row has
15388 ** not been deleted out from under the cursor, then this routine is a no-op.
15389 */
15390 SQLITE_PRIVATE int sqlite3VdbeCursorMoveto(VdbeCursor **pp, int *piCol){
15391 VdbeCursor *p = *pp;
15392 if( p->eCurType==CURTYPE_BTREE ){
15393 if( p->deferredMoveto ){
15394 int iMap;
15395 if( p->aAltMap && (iMap = p->aAltMap[1+*piCol])>0 ){
15396 *pp = p->pAltCursor;
15397 *piCol = iMap - 1;
15398 return SQLITE_OK;
15399 }
15400 return handleDeferredMoveto(p);
15401 }
15402 if( sqlite3BtreeCursorHasMoved(p->uc.pCursor) ){
15403 return handleMovedCursor(p);
15404 }
15405 }
15406 return SQLITE_OK;
15407 }
15408
15409 /*
15410 ** The following functions:
15411 **
15412 ** sqlite3VdbeSerialType()
15413 ** sqlite3VdbeSerialTypeLen()
15414 ** sqlite3VdbeSerialLen()
15415 ** sqlite3VdbeSerialPut()
15416 ** sqlite3VdbeSerialGet()
15417 **
15418 ** encapsulate the code that serializes values for storage in SQLite
15419 ** data and index records. Each serialized value consists of a
15420 ** 'serial-type' and a blob of data. The serial type is an 8-byte unsigned
15421 ** integer, stored as a varint.
15422 **
15423 ** In an SQLite index record, the serial type is stored directly before
15424 ** the blob of data that it corresponds to. In a table record, all serial
15425 ** types are stored at the start of the record, and the blobs of data at
15426 ** the end. Hence these functions allow the caller to handle the
15427 ** serial-type and data blob separately.
15428 **
15429 ** The following table describes the various storage classes for data:
15430 **
15431 ** serial type bytes of data type
15432 ** -------------- --------------- ---------------
15433 ** 0 0 NULL
15434 ** 1 1 signed integer
15435 ** 2 2 signed integer
15436 ** 3 3 signed integer
15437 ** 4 4 signed integer
15438 ** 5 6 signed integer
15439 ** 6 8 signed integer
15440 ** 7 8 IEEE float
15441 ** 8 0 Integer constant 0
15442 ** 9 0 Integer constant 1
15443 ** 10,11 reserved for expansion
15444 ** N>=12 and even (N-12)/2 BLOB
15445 ** N>=13 and odd (N-13)/2 text
15446 **
15447 ** The 8 and 9 types were added in 3.3.0, file format 4. Prior versions
15448 ** of SQLite will not understand those serial types.
15449 */
15450
15451 /*
15452 ** Return the serial-type for the value stored in pMem.
15453 */
15454 SQLITE_PRIVATE u32 sqlite3VdbeSerialType(Mem *pMem, int file_format, u32 *pLen){
15455 int flags = pMem->flags;
15456 u32 n;
15457
15458 assert( pLen!=0 );
15459 if( flags&MEM_Null ){
15460 *pLen = 0;
15461 return 0;
15462 }
15463 if( flags&MEM_Int ){
15464 /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */
15465 # define MAX_6BYTE ((((i64)0x00008000)<<32)-1)
15466 i64 i = pMem->u.i;
15467 u64 u;
15468 if( i<0 ){
15469 u = ~i;
15470 }else{
15471 u = i;
15472 }
15473 if( u<=127 ){
15474 if( (i&1)==i && file_format>=4 ){
15475 *pLen = 0;
15476 return 8+(u32)u;
15477 }else{
15478 *pLen = 1;
15479 return 1;
15480 }
15481 }
15482 if( u<=32767 ){ *pLen = 2; return 2; }
15483 if( u<=8388607 ){ *pLen = 3; return 3; }
15484 if( u<=2147483647 ){ *pLen = 4; return 4; }
15485 if( u<=MAX_6BYTE ){ *pLen = 6; return 5; }
15486 *pLen = 8;
15487 return 6;
15488 }
15489 if( flags&MEM_Real ){
15490 *pLen = 8;
15491 return 7;
15492 }
15493 assert( pMem->db->mallocFailed || flags&(MEM_Str|MEM_Blob) );
15494 assert( pMem->n>=0 );
15495 n = (u32)pMem->n;
15496 if( flags & MEM_Zero ){
15497 n += pMem->u.nZero;
15498 }
15499 *pLen = n;
15500 return ((n*2) + 12 + ((flags&MEM_Str)!=0));
15501 }
15502
15503 /*
15504 ** The sizes for serial types less than 128
15505 */
15506 static const u8 sqlite3SmallTypeSizes[] = {
15507 /* 0 1 2 3 4 5 6 7 8 9 */
15508 /* 0 */ 0, 1, 2, 3, 4, 6, 8, 8, 0, 0,
15509 /* 10 */ 0, 0, 0, 0, 1, 1, 2, 2, 3, 3,
15510 /* 20 */ 4, 4, 5, 5, 6, 6, 7, 7, 8, 8,
15511 /* 30 */ 9, 9, 10, 10, 11, 11, 12, 12, 13, 13,
15512 /* 40 */ 14, 14, 15, 15, 16, 16, 17, 17, 18, 18,
15513 /* 50 */ 19, 19, 20, 20, 21, 21, 22, 22, 23, 23,
15514 /* 60 */ 24, 24, 25, 25, 26, 26, 27, 27, 28, 28,
15515 /* 70 */ 29, 29, 30, 30, 31, 31, 32, 32, 33, 33,
15516 /* 80 */ 34, 34, 35, 35, 36, 36, 37, 37, 38, 38,
15517 /* 90 */ 39, 39, 40, 40, 41, 41, 42, 42, 43, 43,
15518 /* 100 */ 44, 44, 45, 45, 46, 46, 47, 47, 48, 48,
15519 /* 110 */ 49, 49, 50, 50, 51, 51, 52, 52, 53, 53,
15520 /* 120 */ 54, 54, 55, 55, 56, 56, 57, 57
15521 };
15522
15523 /*
15524 ** Return the length of the data corresponding to the supplied serial-type.
15525 */
15526 SQLITE_PRIVATE u32 sqlite3VdbeSerialTypeLen(u32 serial_type){
15527 if( serial_type>=128 ){
15528 return (serial_type-12)/2;
15529 }else{
15530 assert( serial_type<12
15531 || sqlite3SmallTypeSizes[serial_type]==(serial_type - 12)/2 );
15532 return sqlite3SmallTypeSizes[serial_type];
15533 }
15534 }
15535 SQLITE_PRIVATE u8 sqlite3VdbeOneByteSerialTypeLen(u8 serial_type){
15536 assert( serial_type<128 );
15537 return sqlite3SmallTypeSizes[serial_type];
15538 }
15539
15540 /*
15541 ** If we are on an architecture with mixed-endian floating
15542 ** points (ex: ARM7) then swap the lower 4 bytes with the
15543 ** upper 4 bytes. Return the result.
15544 **
15545 ** For most architectures, this is a no-op.
15546 **
15547 ** (later): It is reported to me that the mixed-endian problem
15548 ** on ARM7 is an issue with GCC, not with the ARM7 chip. It seems
15549 ** that early versions of GCC stored the two words of a 64-bit
15550 ** float in the wrong order. And that error has been propagated
15551 ** ever since. The blame is not necessarily with GCC, though.
15552 ** GCC might have just copying the problem from a prior compiler.
15553 ** I am also told that newer versions of GCC that follow a different
15554 ** ABI get the byte order right.
15555 **
15556 ** Developers using SQLite on an ARM7 should compile and run their
15557 ** application using -DSQLITE_DEBUG=1 at least once. With DEBUG
15558 ** enabled, some asserts below will ensure that the byte order of
15559 ** floating point values is correct.
15560 **
15561 ** (2007-08-30) Frank van Vugt has studied this problem closely
15562 ** and has send his findings to the SQLite developers. Frank
15563 ** writes that some Linux kernels offer floating point hardware
15564 ** emulation that uses only 32-bit mantissas instead of a full
15565 ** 48-bits as required by the IEEE standard. (This is the
15566 ** CONFIG_FPE_FASTFPE option.) On such systems, floating point
15567 ** byte swapping becomes very complicated. To avoid problems,
15568 ** the necessary byte swapping is carried out using a 64-bit integer
15569 ** rather than a 64-bit float. Frank assures us that the code here
15570 ** works for him. We, the developers, have no way to independently
15571 ** verify this, but Frank seems to know what he is talking about
15572 ** so we trust him.
15573 */
15574 #ifdef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
15575 static u64 floatSwap(u64 in){
15576 union {
15577 u64 r;
15578 u32 i[2];
15579 } u;
15580 u32 t;
15581
15582 u.r = in;
15583 t = u.i[0];
15584 u.i[0] = u.i[1];
15585 u.i[1] = t;
15586 return u.r;
15587 }
15588 # define swapMixedEndianFloat(X) X = floatSwap(X)
15589 #else
15590 # define swapMixedEndianFloat(X)
15591 #endif
15592
15593 /*
15594 ** Write the serialized data blob for the value stored in pMem into
15595 ** buf. It is assumed that the caller has allocated sufficient space.
15596 ** Return the number of bytes written.
15597 **
15598 ** nBuf is the amount of space left in buf[]. The caller is responsible
15599 ** for allocating enough space to buf[] to hold the entire field, exclusive
15600 ** of the pMem->u.nZero bytes for a MEM_Zero value.
15601 **
15602 ** Return the number of bytes actually written into buf[]. The number
15603 ** of bytes in the zero-filled tail is included in the return value only
15604 ** if those bytes were zeroed in buf[].
15605 */
15606 SQLITE_PRIVATE u32 sqlite3VdbeSerialPut(u8 *buf, Mem *pMem, u32 serial_type){
15607 u32 len;
15608
15609 /* Integer and Real */
15610 if( serial_type<=7 && serial_type>0 ){
15611 u64 v;
15612 u32 i;
15613 if( serial_type==7 ){
15614 assert( sizeof(v)==sizeof(pMem->u.r) );
15615 memcpy(&v, &pMem->u.r, sizeof(v));
15616 swapMixedEndianFloat(v);
15617 }else{
15618 v = pMem->u.i;
15619 }
15620 len = i = sqlite3SmallTypeSizes[serial_type];
15621 assert( i>0 );
15622 do{
15623 buf[--i] = (u8)(v&0xFF);
15624 v >>= 8;
15625 }while( i );
15626 return len;
15627 }
15628
15629 /* String or blob */
15630 if( serial_type>=12 ){
15631 assert( pMem->n + ((pMem->flags & MEM_Zero)?pMem->u.nZero:0)
15632 == (int)sqlite3VdbeSerialTypeLen(serial_type) );
15633 len = pMem->n;
15634 if( len>0 ) memcpy(buf, pMem->z, len);
15635 return len;
15636 }
15637
15638 /* NULL or constants 0 or 1 */
15639 return 0;
15640 }
15641
15642 /* Input "x" is a sequence of unsigned characters that represent a
15643 ** big-endian integer. Return the equivalent native integer
15644 */
15645 #define ONE_BYTE_INT(x) ((i8)(x)[0])
15646 #define TWO_BYTE_INT(x) (256*(i8)((x)[0])|(x)[1])
15647 #define THREE_BYTE_INT(x) (65536*(i8)((x)[0])|((x)[1]<<8)|(x)[2])
15648 #define FOUR_BYTE_UINT(x) (((u32)(x)[0]<<24)|((x)[1]<<16)|((x)[2]<<8)|(x)[3])
15649 #define FOUR_BYTE_INT(x) (16777216*(i8)((x)[0])|((x)[1]<<16)|((x)[2]<<8)|(x)[3])
15650
15651 /*
15652 ** Deserialize the data blob pointed to by buf as serial type serial_type
15653 ** and store the result in pMem. Return the number of bytes read.
15654 **
15655 ** This function is implemented as two separate routines for performance.
15656 ** The few cases that require local variables are broken out into a separate
15657 ** routine so that in most cases the overhead of moving the stack pointer
15658 ** is avoided.
15659 */
15660 static u32 SQLITE_NOINLINE serialGet(
15661 const unsigned char *buf, /* Buffer to deserialize from */
15662 u32 serial_type, /* Serial type to deserialize */
15663 Mem *pMem /* Memory cell to write value into */
15664 ){
15665 u64 x = FOUR_BYTE_UINT(buf);
15666 u32 y = FOUR_BYTE_UINT(buf+4);
15667 x = (x<<32) + y;
15668 if( serial_type==6 ){
15669 /* EVIDENCE-OF: R-29851-52272 Value is a big-endian 64-bit
15670 ** twos-complement integer. */
15671 pMem->u.i = *(i64*)&x;
15672 pMem->flags = MEM_Int;
15673 testcase( pMem->u.i<0 );
15674 }else{
15675 /* EVIDENCE-OF: R-57343-49114 Value is a big-endian IEEE 754-2008 64-bit
15676 ** floating point number. */
15677 #if !defined(NDEBUG) && !defined(SQLITE_OMIT_FLOATING_POINT)
15678 /* Verify that integers and floating point values use the same
15679 ** byte order. Or, that if SQLITE_MIXED_ENDIAN_64BIT_FLOAT is
15680 ** defined that 64-bit floating point values really are mixed
15681 ** endian.
15682 */
15683 static const u64 t1 = ((u64)0x3ff00000)<<32;
15684 static const double r1 = 1.0;
15685 u64 t2 = t1;
15686 swapMixedEndianFloat(t2);
15687 assert( sizeof(r1)==sizeof(t2) && memcmp(&r1, &t2, sizeof(r1))==0 );
15688 #endif
15689 assert( sizeof(x)==8 && sizeof(pMem->u.r)==8 );
15690 swapMixedEndianFloat(x);
15691 memcpy(&pMem->u.r, &x, sizeof(x));
15692 pMem->flags = sqlite3IsNaN(pMem->u.r) ? MEM_Null : MEM_Real;
15693 }
15694 return 8;
15695 }
15696 SQLITE_PRIVATE u32 sqlite3VdbeSerialGet(
15697 const unsigned char *buf, /* Buffer to deserialize from */
15698 u32 serial_type, /* Serial type to deserialize */
15699 Mem *pMem /* Memory cell to write value into */
15700 ){
15701 switch( serial_type ){
15702 case 10: /* Reserved for future use */
15703 case 11: /* Reserved for future use */
15704 case 0: { /* Null */
15705 /* EVIDENCE-OF: R-24078-09375 Value is a NULL. */
15706 pMem->flags = MEM_Null;
15707 break;
15708 }
15709 case 1: {
15710 /* EVIDENCE-OF: R-44885-25196 Value is an 8-bit twos-complement
15711 ** integer. */
15712 pMem->u.i = ONE_BYTE_INT(buf);
15713 pMem->flags = MEM_Int;
15714 testcase( pMem->u.i<0 );
15715 return 1;
15716 }
15717 case 2: { /* 2-byte signed integer */
15718 /* EVIDENCE-OF: R-49794-35026 Value is a big-endian 16-bit
15719 ** twos-complement integer. */
15720 pMem->u.i = TWO_BYTE_INT(buf);
15721 pMem->flags = MEM_Int;
15722 testcase( pMem->u.i<0 );
15723 return 2;
15724 }
15725 case 3: { /* 3-byte signed integer */
15726 /* EVIDENCE-OF: R-37839-54301 Value is a big-endian 24-bit
15727 ** twos-complement integer. */
15728 pMem->u.i = THREE_BYTE_INT(buf);
15729 pMem->flags = MEM_Int;
15730 testcase( pMem->u.i<0 );
15731 return 3;
15732 }
15733 case 4: { /* 4-byte signed integer */
15734 /* EVIDENCE-OF: R-01849-26079 Value is a big-endian 32-bit
15735 ** twos-complement integer. */
15736 pMem->u.i = FOUR_BYTE_INT(buf);
15737 #ifdef __HP_cc
15738 /* Work around a sign-extension bug in the HP compiler for HP/UX */
15739 if( buf[0]&0x80 ) pMem->u.i |= 0xffffffff80000000LL;
15740 #endif
15741 pMem->flags = MEM_Int;
15742 testcase( pMem->u.i<0 );
15743 return 4;
15744 }
15745 case 5: { /* 6-byte signed integer */
15746 /* EVIDENCE-OF: R-50385-09674 Value is a big-endian 48-bit
15747 ** twos-complement integer. */
15748 pMem->u.i = FOUR_BYTE_UINT(buf+2) + (((i64)1)<<32)*TWO_BYTE_INT(buf);
15749 pMem->flags = MEM_Int;
15750 testcase( pMem->u.i<0 );
15751 return 6;
15752 }
15753 case 6: /* 8-byte signed integer */
15754 case 7: { /* IEEE floating point */
15755 /* These use local variables, so do them in a separate routine
15756 ** to avoid having to move the frame pointer in the common case */
15757 return serialGet(buf,serial_type,pMem);
15758 }
15759 case 8: /* Integer 0 */
15760 case 9: { /* Integer 1 */
15761 /* EVIDENCE-OF: R-12976-22893 Value is the integer 0. */
15762 /* EVIDENCE-OF: R-18143-12121 Value is the integer 1. */
15763 pMem->u.i = serial_type-8;
15764 pMem->flags = MEM_Int;
15765 return 0;
15766 }
15767 default: {
15768 /* EVIDENCE-OF: R-14606-31564 Value is a BLOB that is (N-12)/2 bytes in
15769 ** length.
15770 ** EVIDENCE-OF: R-28401-00140 Value is a string in the text encoding and
15771 ** (N-13)/2 bytes in length. */
15772 static const u16 aFlag[] = { MEM_Blob|MEM_Ephem, MEM_Str|MEM_Ephem };
15773 pMem->z = (char *)buf;
15774 pMem->n = (serial_type-12)/2;
15775 pMem->flags = aFlag[serial_type&1];
15776 return pMem->n;
15777 }
15778 }
15779 return 0;
15780 }
15781 /*
15782 ** This routine is used to allocate sufficient space for an UnpackedRecord
15783 ** structure large enough to be used with sqlite3VdbeRecordUnpack() if
15784 ** the first argument is a pointer to KeyInfo structure pKeyInfo.
15785 **
15786 ** The space is either allocated using sqlite3DbMallocRaw() or from within
15787 ** the unaligned buffer passed via the second and third arguments (presumably
15788 ** stack space). If the former, then *ppFree is set to a pointer that should
15789 ** be eventually freed by the caller using sqlite3DbFree(). Or, if the
15790 ** allocation comes from the pSpace/szSpace buffer, *ppFree is set to NULL
15791 ** before returning.
15792 **
15793 ** If an OOM error occurs, NULL is returned.
15794 */
15795 SQLITE_PRIVATE UnpackedRecord *sqlite3VdbeAllocUnpackedRecord(
15796 KeyInfo *pKeyInfo /* Description of the record */
15797 ){
15798 UnpackedRecord *p; /* Unpacked record to return */
15799 int nByte; /* Number of bytes required for *p */
15800 nByte = ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*(pKeyInfo->nField+1);
15801 p = (UnpackedRecord *)sqlite3DbMallocRaw(pKeyInfo->db, nByte);
15802 if( !p ) return 0;
15803 p->aMem = (Mem*)&((char*)p)[ROUND8(sizeof(UnpackedRecord))];
15804 assert( pKeyInfo->aSortOrder!=0 );
15805 p->pKeyInfo = pKeyInfo;
15806 p->nField = pKeyInfo->nField + 1;
15807 return p;
15808 }
15809
15810 /*
15811 ** Given the nKey-byte encoding of a record in pKey[], populate the
15812 ** UnpackedRecord structure indicated by the fourth argument with the
15813 ** contents of the decoded record.
15814 */
15815 SQLITE_PRIVATE void sqlite3VdbeRecordUnpack(
15816 KeyInfo *pKeyInfo, /* Information about the record format */
15817 int nKey, /* Size of the binary record */
15818 const void *pKey, /* The binary record */
15819 UnpackedRecord *p /* Populate this structure before returning. */
15820 ){
15821 const unsigned char *aKey = (const unsigned char *)pKey;
15822 int d;
15823 u32 idx; /* Offset in aKey[] to read from */
15824 u16 u; /* Unsigned loop counter */
15825 u32 szHdr;
15826 Mem *pMem = p->aMem;
15827
15828 p->default_rc = 0;
15829 assert( EIGHT_BYTE_ALIGNMENT(pMem) );
15830 idx = getVarint32(aKey, szHdr);
15831 d = szHdr;
15832 u = 0;
15833 while( idx<szHdr && d<=nKey ){
15834 u32 serial_type;
15835
15836 idx += getVarint32(&aKey[idx], serial_type);
15837 pMem->enc = pKeyInfo->enc;
15838 pMem->db = pKeyInfo->db;
15839 /* pMem->flags = 0; // sqlite3VdbeSerialGet() will set this for us */
15840 pMem->szMalloc = 0;
15841 pMem->z = 0;
15842 d += sqlite3VdbeSerialGet(&aKey[d], serial_type, pMem);
15843 pMem++;
15844 if( (++u)>=p->nField ) break;
15845 }
15846 assert( u<=pKeyInfo->nField + 1 );
15847 p->nField = u;
15848 }
15849
15850 #if SQLITE_DEBUG
15851 /*
15852 ** This function compares two index or table record keys in the same way
15853 ** as the sqlite3VdbeRecordCompare() routine. Unlike VdbeRecordCompare(),
15854 ** this function deserializes and compares values using the
15855 ** sqlite3VdbeSerialGet() and sqlite3MemCompare() functions. It is used
15856 ** in assert() statements to ensure that the optimized code in
15857 ** sqlite3VdbeRecordCompare() returns results with these two primitives.
15858 **
15859 ** Return true if the result of comparison is equivalent to desiredResult.
15860 ** Return false if there is a disagreement.
15861 */
15862 static int vdbeRecordCompareDebug(
15863 int nKey1, const void *pKey1, /* Left key */
15864 const UnpackedRecord *pPKey2, /* Right key */
15865 int desiredResult /* Correct answer */
15866 ){
15867 u32 d1; /* Offset into aKey[] of next data element */
15868 u32 idx1; /* Offset into aKey[] of next header element */
15869 u32 szHdr1; /* Number of bytes in header */
15870 int i = 0;
15871 int rc = 0;
15872 const unsigned char *aKey1 = (const unsigned char *)pKey1;
15873 KeyInfo *pKeyInfo;
15874 Mem mem1;
15875
15876 pKeyInfo = pPKey2->pKeyInfo;
15877 if( pKeyInfo->db==0 ) return 1;
15878 mem1.enc = pKeyInfo->enc;
15879 mem1.db = pKeyInfo->db;
15880 /* mem1.flags = 0; // Will be initialized by sqlite3VdbeSerialGet() */
15881 VVA_ONLY( mem1.szMalloc = 0; ) /* Only needed by assert() statements */
15882
15883 /* Compilers may complain that mem1.u.i is potentially uninitialized.
15884 ** We could initialize it, as shown here, to silence those complaints.
15885 ** But in fact, mem1.u.i will never actually be used uninitialized, and doing
15886 ** the unnecessary initialization has a measurable negative performance
15887 ** impact, since this routine is a very high runner. And so, we choose
15888 ** to ignore the compiler warnings and leave this variable uninitialized.
15889 */
15890 /* mem1.u.i = 0; // not needed, here to silence compiler warning */
15891
15892 idx1 = getVarint32(aKey1, szHdr1);
15893 if( szHdr1>98307 ) return SQLITE_CORRUPT;
15894 d1 = szHdr1;
15895 assert( pKeyInfo->nField+pKeyInfo->nXField>=pPKey2->nField || CORRUPT_DB );
15896 assert( pKeyInfo->aSortOrder!=0 );
15897 assert( pKeyInfo->nField>0 );
15898 assert( idx1<=szHdr1 || CORRUPT_DB );
15899 do{
15900 u32 serial_type1;
15901
15902 /* Read the serial types for the next element in each key. */
15903 idx1 += getVarint32( aKey1+idx1, serial_type1 );
15904
15905 /* Verify that there is enough key space remaining to avoid
15906 ** a buffer overread. The "d1+serial_type1+2" subexpression will
15907 ** always be greater than or equal to the amount of required key space.
15908 ** Use that approximation to avoid the more expensive call to
15909 ** sqlite3VdbeSerialTypeLen() in the common case.
15910 */
15911 if( d1+serial_type1+2>(u32)nKey1
15912 && d1+sqlite3VdbeSerialTypeLen(serial_type1)>(u32)nKey1
15913 ){
15914 break;
15915 }
15916
15917 /* Extract the values to be compared.
15918 */
15919 d1 += sqlite3VdbeSerialGet(&aKey1[d1], serial_type1, &mem1);
15920
15921 /* Do the comparison
15922 */
15923 rc = sqlite3MemCompare(&mem1, &pPKey2->aMem[i], pKeyInfo->aColl[i]);
15924 if( rc!=0 ){
15925 assert( mem1.szMalloc==0 ); /* See comment below */
15926 if( pKeyInfo->aSortOrder[i] ){
15927 rc = -rc; /* Invert the result for DESC sort order. */
15928 }
15929 goto debugCompareEnd;
15930 }
15931 i++;
15932 }while( idx1<szHdr1 && i<pPKey2->nField );
15933
15934 /* No memory allocation is ever used on mem1. Prove this using
15935 ** the following assert(). If the assert() fails, it indicates a
15936 ** memory leak and a need to call sqlite3VdbeMemRelease(&mem1).
15937 */
15938 assert( mem1.szMalloc==0 );
15939
15940 /* rc==0 here means that one of the keys ran out of fields and
15941 ** all the fields up to that point were equal. Return the default_rc
15942 ** value. */
15943 rc = pPKey2->default_rc;
15944
15945 debugCompareEnd:
15946 if( desiredResult==0 && rc==0 ) return 1;
15947 if( desiredResult<0 && rc<0 ) return 1;
15948 if( desiredResult>0 && rc>0 ) return 1;
15949 if( CORRUPT_DB ) return 1;
15950 if( pKeyInfo->db->mallocFailed ) return 1;
15951 return 0;
15952 }
15953 #endif
15954
15955 #if SQLITE_DEBUG
15956 /*
15957 ** Count the number of fields (a.k.a. columns) in the record given by
15958 ** pKey,nKey. The verify that this count is less than or equal to the
15959 ** limit given by pKeyInfo->nField + pKeyInfo->nXField.
15960 **
15961 ** If this constraint is not satisfied, it means that the high-speed
15962 ** vdbeRecordCompareInt() and vdbeRecordCompareString() routines will
15963 ** not work correctly. If this assert() ever fires, it probably means
15964 ** that the KeyInfo.nField or KeyInfo.nXField values were computed
15965 ** incorrectly.
15966 */
15967 static void vdbeAssertFieldCountWithinLimits(
15968 int nKey, const void *pKey, /* The record to verify */
15969 const KeyInfo *pKeyInfo /* Compare size with this KeyInfo */
15970 ){
15971 int nField = 0;
15972 u32 szHdr;
15973 u32 idx;
15974 u32 notUsed;
15975 const unsigned char *aKey = (const unsigned char*)pKey;
15976
15977 if( CORRUPT_DB ) return;
15978 idx = getVarint32(aKey, szHdr);
15979 assert( nKey>=0 );
15980 assert( szHdr<=(u32)nKey );
15981 while( idx<szHdr ){
15982 idx += getVarint32(aKey+idx, notUsed);
15983 nField++;
15984 }
15985 assert( nField <= pKeyInfo->nField+pKeyInfo->nXField );
15986 }
15987 #else
15988 # define vdbeAssertFieldCountWithinLimits(A,B,C)
15989 #endif
15990
15991 /*
15992 ** Both *pMem1 and *pMem2 contain string values. Compare the two values
15993 ** using the collation sequence pColl. As usual, return a negative , zero
15994 ** or positive value if *pMem1 is less than, equal to or greater than
15995 ** *pMem2, respectively. Similar in spirit to "rc = (*pMem1) - (*pMem2);".
15996 */
15997 static int vdbeCompareMemString(
15998 const Mem *pMem1,
15999 const Mem *pMem2,
16000 const CollSeq *pColl,
16001 u8 *prcErr /* If an OOM occurs, set to SQLITE_NOMEM */
16002 ){
16003 if( pMem1->enc==pColl->enc ){
16004 /* The strings are already in the correct encoding. Call the
16005 ** comparison function directly */
16006 return pColl->xCmp(pColl->pUser,pMem1->n,pMem1->z,pMem2->n,pMem2->z);
16007 }else{
16008 int rc;
16009 const void *v1, *v2;
16010 int n1, n2;
16011 Mem c1;
16012 Mem c2;
16013 sqlite3VdbeMemInit(&c1, pMem1->db, MEM_Null);
16014 sqlite3VdbeMemInit(&c2, pMem1->db, MEM_Null);
16015 sqlite3VdbeMemShallowCopy(&c1, pMem1, MEM_Ephem);
16016 sqlite3VdbeMemShallowCopy(&c2, pMem2, MEM_Ephem);
16017 v1 = sqlite3ValueText((sqlite3_value*)&c1, pColl->enc);
16018 n1 = v1==0 ? 0 : c1.n;
16019 v2 = sqlite3ValueText((sqlite3_value*)&c2, pColl->enc);
16020 n2 = v2==0 ? 0 : c2.n;
16021 rc = pColl->xCmp(pColl->pUser, n1, v1, n2, v2);
16022 if( (v1==0 || v2==0) && prcErr ) *prcErr = SQLITE_NOMEM_BKPT;
16023 sqlite3VdbeMemRelease(&c1);
16024 sqlite3VdbeMemRelease(&c2);
16025 return rc;
16026 }
16027 }
16028
16029 /*
16030 ** The input pBlob is guaranteed to be a Blob that is not marked
16031 ** with MEM_Zero. Return true if it could be a zero-blob.
16032 */
16033 static int isAllZero(const char *z, int n){
16034 int i;
16035 for(i=0; i<n; i++){
16036 if( z[i] ) return 0;
16037 }
16038 return 1;
16039 }
16040
16041 /*
16042 ** Compare two blobs. Return negative, zero, or positive if the first
16043 ** is less than, equal to, or greater than the second, respectively.
16044 ** If one blob is a prefix of the other, then the shorter is the lessor.
16045 */
16046 static SQLITE_NOINLINE int sqlite3BlobCompare(const Mem *pB1, const Mem *pB2){
16047 int c;
16048 int n1 = pB1->n;
16049 int n2 = pB2->n;
16050
16051 /* It is possible to have a Blob value that has some non-zero content
16052 ** followed by zero content. But that only comes up for Blobs formed
16053 ** by the OP_MakeRecord opcode, and such Blobs never get passed into
16054 ** sqlite3MemCompare(). */
16055 assert( (pB1->flags & MEM_Zero)==0 || n1==0 );
16056 assert( (pB2->flags & MEM_Zero)==0 || n2==0 );
16057
16058 if( (pB1->flags|pB2->flags) & MEM_Zero ){
16059 if( pB1->flags & pB2->flags & MEM_Zero ){
16060 return pB1->u.nZero - pB2->u.nZero;
16061 }else if( pB1->flags & MEM_Zero ){
16062 if( !isAllZero(pB2->z, pB2->n) ) return -1;
16063 return pB1->u.nZero - n2;
16064 }else{
16065 if( !isAllZero(pB1->z, pB1->n) ) return +1;
16066 return n1 - pB2->u.nZero;
16067 }
16068 }
16069 c = memcmp(pB1->z, pB2->z, n1>n2 ? n2 : n1);
16070 if( c ) return c;
16071 return n1 - n2;
16072 }
16073
16074 /*
16075 ** Do a comparison between a 64-bit signed integer and a 64-bit floating-point
16076 ** number. Return negative, zero, or positive if the first (i64) is less than,
16077 ** equal to, or greater than the second (double).
16078 */
16079 static int sqlite3IntFloatCompare(i64 i, double r){
16080 if( sizeof(LONGDOUBLE_TYPE)>8 ){
16081 LONGDOUBLE_TYPE x = (LONGDOUBLE_TYPE)i;
16082 if( x<r ) return -1;
16083 if( x>r ) return +1;
16084 return 0;
16085 }else{
16086 i64 y;
16087 double s;
16088 if( r<-9223372036854775808.0 ) return +1;
16089 if( r>9223372036854775807.0 ) return -1;
16090 y = (i64)r;
16091 if( i<y ) return -1;
16092 if( i>y ){
16093 if( y==SMALLEST_INT64 && r>0.0 ) return -1;
16094 return +1;
16095 }
16096 s = (double)i;
16097 if( s<r ) return -1;
16098 if( s>r ) return +1;
16099 return 0;
16100 }
16101 }
16102
16103 /*
16104 ** Compare the values contained by the two memory cells, returning
16105 ** negative, zero or positive if pMem1 is less than, equal to, or greater
16106 ** than pMem2. Sorting order is NULL's first, followed by numbers (integers
16107 ** and reals) sorted numerically, followed by text ordered by the collating
16108 ** sequence pColl and finally blob's ordered by memcmp().
16109 **
16110 ** Two NULL values are considered equal by this function.
16111 */
16112 SQLITE_PRIVATE int sqlite3MemCompare(const Mem *pMem1, const Mem *pMem2, const C ollSeq *pColl){
16113 int f1, f2;
16114 int combined_flags;
16115
16116 f1 = pMem1->flags;
16117 f2 = pMem2->flags;
16118 combined_flags = f1|f2;
16119 assert( (combined_flags & MEM_RowSet)==0 );
16120
16121 /* If one value is NULL, it is less than the other. If both values
16122 ** are NULL, return 0.
16123 */
16124 if( combined_flags&MEM_Null ){
16125 return (f2&MEM_Null) - (f1&MEM_Null);
16126 }
16127
16128 /* At least one of the two values is a number
16129 */
16130 if( combined_flags&(MEM_Int|MEM_Real) ){
16131 if( (f1 & f2 & MEM_Int)!=0 ){
16132 if( pMem1->u.i < pMem2->u.i ) return -1;
16133 if( pMem1->u.i > pMem2->u.i ) return +1;
16134 return 0;
16135 }
16136 if( (f1 & f2 & MEM_Real)!=0 ){
16137 if( pMem1->u.r < pMem2->u.r ) return -1;
16138 if( pMem1->u.r > pMem2->u.r ) return +1;
16139 return 0;
16140 }
16141 if( (f1&MEM_Int)!=0 ){
16142 if( (f2&MEM_Real)!=0 ){
16143 return sqlite3IntFloatCompare(pMem1->u.i, pMem2->u.r);
16144 }else{
16145 return -1;
16146 }
16147 }
16148 if( (f1&MEM_Real)!=0 ){
16149 if( (f2&MEM_Int)!=0 ){
16150 return -sqlite3IntFloatCompare(pMem2->u.i, pMem1->u.r);
16151 }else{
16152 return -1;
16153 }
16154 }
16155 return +1;
16156 }
16157
16158 /* If one value is a string and the other is a blob, the string is less.
16159 ** If both are strings, compare using the collating functions.
16160 */
16161 if( combined_flags&MEM_Str ){
16162 if( (f1 & MEM_Str)==0 ){
16163 return 1;
16164 }
16165 if( (f2 & MEM_Str)==0 ){
16166 return -1;
16167 }
16168
16169 assert( pMem1->enc==pMem2->enc || pMem1->db->mallocFailed );
16170 assert( pMem1->enc==SQLITE_UTF8 ||
16171 pMem1->enc==SQLITE_UTF16LE || pMem1->enc==SQLITE_UTF16BE );
16172
16173 /* The collation sequence must be defined at this point, even if
16174 ** the user deletes the collation sequence after the vdbe program is
16175 ** compiled (this was not always the case).
16176 */
16177 assert( !pColl || pColl->xCmp );
16178
16179 if( pColl ){
16180 return vdbeCompareMemString(pMem1, pMem2, pColl, 0);
16181 }
16182 /* If a NULL pointer was passed as the collate function, fall through
16183 ** to the blob case and use memcmp(). */
16184 }
16185
16186 /* Both values must be blobs. Compare using memcmp(). */
16187 return sqlite3BlobCompare(pMem1, pMem2);
16188 }
16189
16190
16191 /*
16192 ** The first argument passed to this function is a serial-type that
16193 ** corresponds to an integer - all values between 1 and 9 inclusive
16194 ** except 7. The second points to a buffer containing an integer value
16195 ** serialized according to serial_type. This function deserializes
16196 ** and returns the value.
16197 */
16198 static i64 vdbeRecordDecodeInt(u32 serial_type, const u8 *aKey){
16199 u32 y;
16200 assert( CORRUPT_DB || (serial_type>=1 && serial_type<=9 && serial_type!=7) );
16201 switch( serial_type ){
16202 case 0:
16203 case 1:
16204 testcase( aKey[0]&0x80 );
16205 return ONE_BYTE_INT(aKey);
16206 case 2:
16207 testcase( aKey[0]&0x80 );
16208 return TWO_BYTE_INT(aKey);
16209 case 3:
16210 testcase( aKey[0]&0x80 );
16211 return THREE_BYTE_INT(aKey);
16212 case 4: {
16213 testcase( aKey[0]&0x80 );
16214 y = FOUR_BYTE_UINT(aKey);
16215 return (i64)*(int*)&y;
16216 }
16217 case 5: {
16218 testcase( aKey[0]&0x80 );
16219 return FOUR_BYTE_UINT(aKey+2) + (((i64)1)<<32)*TWO_BYTE_INT(aKey);
16220 }
16221 case 6: {
16222 u64 x = FOUR_BYTE_UINT(aKey);
16223 testcase( aKey[0]&0x80 );
16224 x = (x<<32) | FOUR_BYTE_UINT(aKey+4);
16225 return (i64)*(i64*)&x;
16226 }
16227 }
16228
16229 return (serial_type - 8);
16230 }
16231
16232 /*
16233 ** This function compares the two table rows or index records
16234 ** specified by {nKey1, pKey1} and pPKey2. It returns a negative, zero
16235 ** or positive integer if key1 is less than, equal to or
16236 ** greater than key2. The {nKey1, pKey1} key must be a blob
16237 ** created by the OP_MakeRecord opcode of the VDBE. The pPKey2
16238 ** key must be a parsed key such as obtained from
16239 ** sqlite3VdbeParseRecord.
16240 **
16241 ** If argument bSkip is non-zero, it is assumed that the caller has already
16242 ** determined that the first fields of the keys are equal.
16243 **
16244 ** Key1 and Key2 do not have to contain the same number of fields. If all
16245 ** fields that appear in both keys are equal, then pPKey2->default_rc is
16246 ** returned.
16247 **
16248 ** If database corruption is discovered, set pPKey2->errCode to
16249 ** SQLITE_CORRUPT and return 0. If an OOM error is encountered,
16250 ** pPKey2->errCode is set to SQLITE_NOMEM and, if it is not NULL, the
16251 ** malloc-failed flag set on database handle (pPKey2->pKeyInfo->db).
16252 */
16253 SQLITE_PRIVATE int sqlite3VdbeRecordCompareWithSkip(
16254 int nKey1, const void *pKey1, /* Left key */
16255 UnpackedRecord *pPKey2, /* Right key */
16256 int bSkip /* If true, skip the first field */
16257 ){
16258 u32 d1; /* Offset into aKey[] of next data element */
16259 int i; /* Index of next field to compare */
16260 u32 szHdr1; /* Size of record header in bytes */
16261 u32 idx1; /* Offset of first type in header */
16262 int rc = 0; /* Return value */
16263 Mem *pRhs = pPKey2->aMem; /* Next field of pPKey2 to compare */
16264 KeyInfo *pKeyInfo = pPKey2->pKeyInfo;
16265 const unsigned char *aKey1 = (const unsigned char *)pKey1;
16266 Mem mem1;
16267
16268 /* If bSkip is true, then the caller has already determined that the first
16269 ** two elements in the keys are equal. Fix the various stack variables so
16270 ** that this routine begins comparing at the second field. */
16271 if( bSkip ){
16272 u32 s1;
16273 idx1 = 1 + getVarint32(&aKey1[1], s1);
16274 szHdr1 = aKey1[0];
16275 d1 = szHdr1 + sqlite3VdbeSerialTypeLen(s1);
16276 i = 1;
16277 pRhs++;
16278 }else{
16279 idx1 = getVarint32(aKey1, szHdr1);
16280 d1 = szHdr1;
16281 if( d1>(unsigned)nKey1 ){
16282 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
16283 return 0; /* Corruption */
16284 }
16285 i = 0;
16286 }
16287
16288 VVA_ONLY( mem1.szMalloc = 0; ) /* Only needed by assert() statements */
16289 assert( pPKey2->pKeyInfo->nField+pPKey2->pKeyInfo->nXField>=pPKey2->nField
16290 || CORRUPT_DB );
16291 assert( pPKey2->pKeyInfo->aSortOrder!=0 );
16292 assert( pPKey2->pKeyInfo->nField>0 );
16293 assert( idx1<=szHdr1 || CORRUPT_DB );
16294 do{
16295 u32 serial_type;
16296
16297 /* RHS is an integer */
16298 if( pRhs->flags & MEM_Int ){
16299 serial_type = aKey1[idx1];
16300 testcase( serial_type==12 );
16301 if( serial_type>=10 ){
16302 rc = +1;
16303 }else if( serial_type==0 ){
16304 rc = -1;
16305 }else if( serial_type==7 ){
16306 sqlite3VdbeSerialGet(&aKey1[d1], serial_type, &mem1);
16307 rc = -sqlite3IntFloatCompare(pRhs->u.i, mem1.u.r);
16308 }else{
16309 i64 lhs = vdbeRecordDecodeInt(serial_type, &aKey1[d1]);
16310 i64 rhs = pRhs->u.i;
16311 if( lhs<rhs ){
16312 rc = -1;
16313 }else if( lhs>rhs ){
16314 rc = +1;
16315 }
16316 }
16317 }
16318
16319 /* RHS is real */
16320 else if( pRhs->flags & MEM_Real ){
16321 serial_type = aKey1[idx1];
16322 if( serial_type>=10 ){
16323 /* Serial types 12 or greater are strings and blobs (greater than
16324 ** numbers). Types 10 and 11 are currently "reserved for future
16325 ** use", so it doesn't really matter what the results of comparing
16326 ** them to numberic values are. */
16327 rc = +1;
16328 }else if( serial_type==0 ){
16329 rc = -1;
16330 }else{
16331 sqlite3VdbeSerialGet(&aKey1[d1], serial_type, &mem1);
16332 if( serial_type==7 ){
16333 if( mem1.u.r<pRhs->u.r ){
16334 rc = -1;
16335 }else if( mem1.u.r>pRhs->u.r ){
16336 rc = +1;
16337 }
16338 }else{
16339 rc = sqlite3IntFloatCompare(mem1.u.i, pRhs->u.r);
16340 }
16341 }
16342 }
16343
16344 /* RHS is a string */
16345 else if( pRhs->flags & MEM_Str ){
16346 getVarint32(&aKey1[idx1], serial_type);
16347 testcase( serial_type==12 );
16348 if( serial_type<12 ){
16349 rc = -1;
16350 }else if( !(serial_type & 0x01) ){
16351 rc = +1;
16352 }else{
16353 mem1.n = (serial_type - 12) / 2;
16354 testcase( (d1+mem1.n)==(unsigned)nKey1 );
16355 testcase( (d1+mem1.n+1)==(unsigned)nKey1 );
16356 if( (d1+mem1.n) > (unsigned)nKey1 ){
16357 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
16358 return 0; /* Corruption */
16359 }else if( pKeyInfo->aColl[i] ){
16360 mem1.enc = pKeyInfo->enc;
16361 mem1.db = pKeyInfo->db;
16362 mem1.flags = MEM_Str;
16363 mem1.z = (char*)&aKey1[d1];
16364 rc = vdbeCompareMemString(
16365 &mem1, pRhs, pKeyInfo->aColl[i], &pPKey2->errCode
16366 );
16367 }else{
16368 int nCmp = MIN(mem1.n, pRhs->n);
16369 rc = memcmp(&aKey1[d1], pRhs->z, nCmp);
16370 if( rc==0 ) rc = mem1.n - pRhs->n;
16371 }
16372 }
16373 }
16374
16375 /* RHS is a blob */
16376 else if( pRhs->flags & MEM_Blob ){
16377 assert( (pRhs->flags & MEM_Zero)==0 || pRhs->n==0 );
16378 getVarint32(&aKey1[idx1], serial_type);
16379 testcase( serial_type==12 );
16380 if( serial_type<12 || (serial_type & 0x01) ){
16381 rc = -1;
16382 }else{
16383 int nStr = (serial_type - 12) / 2;
16384 testcase( (d1+nStr)==(unsigned)nKey1 );
16385 testcase( (d1+nStr+1)==(unsigned)nKey1 );
16386 if( (d1+nStr) > (unsigned)nKey1 ){
16387 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
16388 return 0; /* Corruption */
16389 }else if( pRhs->flags & MEM_Zero ){
16390 if( !isAllZero((const char*)&aKey1[d1],nStr) ){
16391 rc = 1;
16392 }else{
16393 rc = nStr - pRhs->u.nZero;
16394 }
16395 }else{
16396 int nCmp = MIN(nStr, pRhs->n);
16397 rc = memcmp(&aKey1[d1], pRhs->z, nCmp);
16398 if( rc==0 ) rc = nStr - pRhs->n;
16399 }
16400 }
16401 }
16402
16403 /* RHS is null */
16404 else{
16405 serial_type = aKey1[idx1];
16406 rc = (serial_type!=0);
16407 }
16408
16409 if( rc!=0 ){
16410 if( pKeyInfo->aSortOrder[i] ){
16411 rc = -rc;
16412 }
16413 assert( vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, rc) );
16414 assert( mem1.szMalloc==0 ); /* See comment below */
16415 return rc;
16416 }
16417
16418 i++;
16419 pRhs++;
16420 d1 += sqlite3VdbeSerialTypeLen(serial_type);
16421 idx1 += sqlite3VarintLen(serial_type);
16422 }while( idx1<(unsigned)szHdr1 && i<pPKey2->nField && d1<=(unsigned)nKey1 );
16423
16424 /* No memory allocation is ever used on mem1. Prove this using
16425 ** the following assert(). If the assert() fails, it indicates a
16426 ** memory leak and a need to call sqlite3VdbeMemRelease(&mem1). */
16427 assert( mem1.szMalloc==0 );
16428
16429 /* rc==0 here means that one or both of the keys ran out of fields and
16430 ** all the fields up to that point were equal. Return the default_rc
16431 ** value. */
16432 assert( CORRUPT_DB
16433 || vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, pPKey2->default_rc)
16434 || pKeyInfo->db->mallocFailed
16435 );
16436 pPKey2->eqSeen = 1;
16437 return pPKey2->default_rc;
16438 }
16439 SQLITE_PRIVATE int sqlite3VdbeRecordCompare(
16440 int nKey1, const void *pKey1, /* Left key */
16441 UnpackedRecord *pPKey2 /* Right key */
16442 ){
16443 return sqlite3VdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2, 0);
16444 }
16445
16446
16447 /*
16448 ** This function is an optimized version of sqlite3VdbeRecordCompare()
16449 ** that (a) the first field of pPKey2 is an integer, and (b) the
16450 ** size-of-header varint at the start of (pKey1/nKey1) fits in a single
16451 ** byte (i.e. is less than 128).
16452 **
16453 ** To avoid concerns about buffer overreads, this routine is only used
16454 ** on schemas where the maximum valid header size is 63 bytes or less.
16455 */
16456 static int vdbeRecordCompareInt(
16457 int nKey1, const void *pKey1, /* Left key */
16458 UnpackedRecord *pPKey2 /* Right key */
16459 ){
16460 const u8 *aKey = &((const u8*)pKey1)[*(const u8*)pKey1 & 0x3F];
16461 int serial_type = ((const u8*)pKey1)[1];
16462 int res;
16463 u32 y;
16464 u64 x;
16465 i64 v;
16466 i64 lhs;
16467
16468 vdbeAssertFieldCountWithinLimits(nKey1, pKey1, pPKey2->pKeyInfo);
16469 assert( (*(u8*)pKey1)<=0x3F || CORRUPT_DB );
16470 switch( serial_type ){
16471 case 1: { /* 1-byte signed integer */
16472 lhs = ONE_BYTE_INT(aKey);
16473 testcase( lhs<0 );
16474 break;
16475 }
16476 case 2: { /* 2-byte signed integer */
16477 lhs = TWO_BYTE_INT(aKey);
16478 testcase( lhs<0 );
16479 break;
16480 }
16481 case 3: { /* 3-byte signed integer */
16482 lhs = THREE_BYTE_INT(aKey);
16483 testcase( lhs<0 );
16484 break;
16485 }
16486 case 4: { /* 4-byte signed integer */
16487 y = FOUR_BYTE_UINT(aKey);
16488 lhs = (i64)*(int*)&y;
16489 testcase( lhs<0 );
16490 break;
16491 }
16492 case 5: { /* 6-byte signed integer */
16493 lhs = FOUR_BYTE_UINT(aKey+2) + (((i64)1)<<32)*TWO_BYTE_INT(aKey);
16494 testcase( lhs<0 );
16495 break;
16496 }
16497 case 6: { /* 8-byte signed integer */
16498 x = FOUR_BYTE_UINT(aKey);
16499 x = (x<<32) | FOUR_BYTE_UINT(aKey+4);
16500 lhs = *(i64*)&x;
16501 testcase( lhs<0 );
16502 break;
16503 }
16504 case 8:
16505 lhs = 0;
16506 break;
16507 case 9:
16508 lhs = 1;
16509 break;
16510
16511 /* This case could be removed without changing the results of running
16512 ** this code. Including it causes gcc to generate a faster switch
16513 ** statement (since the range of switch targets now starts at zero and
16514 ** is contiguous) but does not cause any duplicate code to be generated
16515 ** (as gcc is clever enough to combine the two like cases). Other
16516 ** compilers might be similar. */
16517 case 0: case 7:
16518 return sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2);
16519
16520 default:
16521 return sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2);
16522 }
16523
16524 v = pPKey2->aMem[0].u.i;
16525 if( v>lhs ){
16526 res = pPKey2->r1;
16527 }else if( v<lhs ){
16528 res = pPKey2->r2;
16529 }else if( pPKey2->nField>1 ){
16530 /* The first fields of the two keys are equal. Compare the trailing
16531 ** fields. */
16532 res = sqlite3VdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2, 1);
16533 }else{
16534 /* The first fields of the two keys are equal and there are no trailing
16535 ** fields. Return pPKey2->default_rc in this case. */
16536 res = pPKey2->default_rc;
16537 pPKey2->eqSeen = 1;
16538 }
16539
16540 assert( vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, res) );
16541 return res;
16542 }
16543
16544 /*
16545 ** This function is an optimized version of sqlite3VdbeRecordCompare()
16546 ** that (a) the first field of pPKey2 is a string, that (b) the first field
16547 ** uses the collation sequence BINARY and (c) that the size-of-header varint
16548 ** at the start of (pKey1/nKey1) fits in a single byte.
16549 */
16550 static int vdbeRecordCompareString(
16551 int nKey1, const void *pKey1, /* Left key */
16552 UnpackedRecord *pPKey2 /* Right key */
16553 ){
16554 const u8 *aKey1 = (const u8*)pKey1;
16555 int serial_type;
16556 int res;
16557
16558 assert( pPKey2->aMem[0].flags & MEM_Str );
16559 vdbeAssertFieldCountWithinLimits(nKey1, pKey1, pPKey2->pKeyInfo);
16560 getVarint32(&aKey1[1], serial_type);
16561 if( serial_type<12 ){
16562 res = pPKey2->r1; /* (pKey1/nKey1) is a number or a null */
16563 }else if( !(serial_type & 0x01) ){
16564 res = pPKey2->r2; /* (pKey1/nKey1) is a blob */
16565 }else{
16566 int nCmp;
16567 int nStr;
16568 int szHdr = aKey1[0];
16569
16570 nStr = (serial_type-12) / 2;
16571 if( (szHdr + nStr) > nKey1 ){
16572 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
16573 return 0; /* Corruption */
16574 }
16575 nCmp = MIN( pPKey2->aMem[0].n, nStr );
16576 res = memcmp(&aKey1[szHdr], pPKey2->aMem[0].z, nCmp);
16577
16578 if( res==0 ){
16579 res = nStr - pPKey2->aMem[0].n;
16580 if( res==0 ){
16581 if( pPKey2->nField>1 ){
16582 res = sqlite3VdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2, 1);
16583 }else{
16584 res = pPKey2->default_rc;
16585 pPKey2->eqSeen = 1;
16586 }
16587 }else if( res>0 ){
16588 res = pPKey2->r2;
16589 }else{
16590 res = pPKey2->r1;
16591 }
16592 }else if( res>0 ){
16593 res = pPKey2->r2;
16594 }else{
16595 res = pPKey2->r1;
16596 }
16597 }
16598
16599 assert( vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, res)
16600 || CORRUPT_DB
16601 || pPKey2->pKeyInfo->db->mallocFailed
16602 );
16603 return res;
16604 }
16605
16606 /*
16607 ** Return a pointer to an sqlite3VdbeRecordCompare() compatible function
16608 ** suitable for comparing serialized records to the unpacked record passed
16609 ** as the only argument.
16610 */
16611 SQLITE_PRIVATE RecordCompare sqlite3VdbeFindCompare(UnpackedRecord *p){
16612 /* varintRecordCompareInt() and varintRecordCompareString() both assume
16613 ** that the size-of-header varint that occurs at the start of each record
16614 ** fits in a single byte (i.e. is 127 or less). varintRecordCompareInt()
16615 ** also assumes that it is safe to overread a buffer by at least the
16616 ** maximum possible legal header size plus 8 bytes. Because there is
16617 ** guaranteed to be at least 74 (but not 136) bytes of padding following each
16618 ** buffer passed to varintRecordCompareInt() this makes it convenient to
16619 ** limit the size of the header to 64 bytes in cases where the first field
16620 ** is an integer.
16621 **
16622 ** The easiest way to enforce this limit is to consider only records with
16623 ** 13 fields or less. If the first field is an integer, the maximum legal
16624 ** header size is (12*5 + 1 + 1) bytes. */
16625 if( (p->pKeyInfo->nField + p->pKeyInfo->nXField)<=13 ){
16626 int flags = p->aMem[0].flags;
16627 if( p->pKeyInfo->aSortOrder[0] ){
16628 p->r1 = 1;
16629 p->r2 = -1;
16630 }else{
16631 p->r1 = -1;
16632 p->r2 = 1;
16633 }
16634 if( (flags & MEM_Int) ){
16635 return vdbeRecordCompareInt;
16636 }
16637 testcase( flags & MEM_Real );
16638 testcase( flags & MEM_Null );
16639 testcase( flags & MEM_Blob );
16640 if( (flags & (MEM_Real|MEM_Null|MEM_Blob))==0 && p->pKeyInfo->aColl[0]==0 ){
16641 assert( flags & MEM_Str );
16642 return vdbeRecordCompareString;
16643 }
16644 }
16645
16646 return sqlite3VdbeRecordCompare;
16647 }
16648
16649 /*
16650 ** pCur points at an index entry created using the OP_MakeRecord opcode.
16651 ** Read the rowid (the last field in the record) and store it in *rowid.
16652 ** Return SQLITE_OK if everything works, or an error code otherwise.
16653 **
16654 ** pCur might be pointing to text obtained from a corrupt database file.
16655 ** So the content cannot be trusted. Do appropriate checks on the content.
16656 */
16657 SQLITE_PRIVATE int sqlite3VdbeIdxRowid(sqlite3 *db, BtCursor *pCur, i64 *rowid){
16658 i64 nCellKey = 0;
16659 int rc;
16660 u32 szHdr; /* Size of the header */
16661 u32 typeRowid; /* Serial type of the rowid */
16662 u32 lenRowid; /* Size of the rowid */
16663 Mem m, v;
16664
16665 /* Get the size of the index entry. Only indices entries of less
16666 ** than 2GiB are support - anything large must be database corruption.
16667 ** Any corruption is detected in sqlite3BtreeParseCellPtr(), though, so
16668 ** this code can safely assume that nCellKey is 32-bits
16669 */
16670 assert( sqlite3BtreeCursorIsValid(pCur) );
16671 nCellKey = sqlite3BtreePayloadSize(pCur);
16672 assert( (nCellKey & SQLITE_MAX_U32)==(u64)nCellKey );
16673
16674 /* Read in the complete content of the index entry */
16675 sqlite3VdbeMemInit(&m, db, 0);
16676 rc = sqlite3VdbeMemFromBtree(pCur, 0, (u32)nCellKey, &m);
16677 if( rc ){
16678 return rc;
16679 }
16680
16681 /* The index entry must begin with a header size */
16682 (void)getVarint32((u8*)m.z, szHdr);
16683 testcase( szHdr==3 );
16684 testcase( szHdr==m.n );
16685 if( unlikely(szHdr<3 || (int)szHdr>m.n) ){
16686 goto idx_rowid_corruption;
16687 }
16688
16689 /* The last field of the index should be an integer - the ROWID.
16690 ** Verify that the last entry really is an integer. */
16691 (void)getVarint32((u8*)&m.z[szHdr-1], typeRowid);
16692 testcase( typeRowid==1 );
16693 testcase( typeRowid==2 );
16694 testcase( typeRowid==3 );
16695 testcase( typeRowid==4 );
16696 testcase( typeRowid==5 );
16697 testcase( typeRowid==6 );
16698 testcase( typeRowid==8 );
16699 testcase( typeRowid==9 );
16700 if( unlikely(typeRowid<1 || typeRowid>9 || typeRowid==7) ){
16701 goto idx_rowid_corruption;
16702 }
16703 lenRowid = sqlite3SmallTypeSizes[typeRowid];
16704 testcase( (u32)m.n==szHdr+lenRowid );
16705 if( unlikely((u32)m.n<szHdr+lenRowid) ){
16706 goto idx_rowid_corruption;
16707 }
16708
16709 /* Fetch the integer off the end of the index record */
16710 sqlite3VdbeSerialGet((u8*)&m.z[m.n-lenRowid], typeRowid, &v);
16711 *rowid = v.u.i;
16712 sqlite3VdbeMemRelease(&m);
16713 return SQLITE_OK;
16714
16715 /* Jump here if database corruption is detected after m has been
16716 ** allocated. Free the m object and return SQLITE_CORRUPT. */
16717 idx_rowid_corruption:
16718 testcase( m.szMalloc!=0 );
16719 sqlite3VdbeMemRelease(&m);
16720 return SQLITE_CORRUPT_BKPT;
16721 }
16722
16723 /*
16724 ** Compare the key of the index entry that cursor pC is pointing to against
16725 ** the key string in pUnpacked. Write into *pRes a number
16726 ** that is negative, zero, or positive if pC is less than, equal to,
16727 ** or greater than pUnpacked. Return SQLITE_OK on success.
16728 **
16729 ** pUnpacked is either created without a rowid or is truncated so that it
16730 ** omits the rowid at the end. The rowid at the end of the index entry
16731 ** is ignored as well. Hence, this routine only compares the prefixes
16732 ** of the keys prior to the final rowid, not the entire key.
16733 */
16734 SQLITE_PRIVATE int sqlite3VdbeIdxKeyCompare(
16735 sqlite3 *db, /* Database connection */
16736 VdbeCursor *pC, /* The cursor to compare against */
16737 UnpackedRecord *pUnpacked, /* Unpacked version of key */
16738 int *res /* Write the comparison result here */
16739 ){
16740 i64 nCellKey = 0;
16741 int rc;
16742 BtCursor *pCur;
16743 Mem m;
16744
16745 assert( pC->eCurType==CURTYPE_BTREE );
16746 pCur = pC->uc.pCursor;
16747 assert( sqlite3BtreeCursorIsValid(pCur) );
16748 nCellKey = sqlite3BtreePayloadSize(pCur);
16749 /* nCellKey will always be between 0 and 0xffffffff because of the way
16750 ** that btreeParseCellPtr() and sqlite3GetVarint32() are implemented */
16751 if( nCellKey<=0 || nCellKey>0x7fffffff ){
16752 *res = 0;
16753 return SQLITE_CORRUPT_BKPT;
16754 }
16755 sqlite3VdbeMemInit(&m, db, 0);
16756 rc = sqlite3VdbeMemFromBtree(pCur, 0, (u32)nCellKey, &m);
16757 if( rc ){
16758 return rc;
16759 }
16760 *res = sqlite3VdbeRecordCompare(m.n, m.z, pUnpacked);
16761 sqlite3VdbeMemRelease(&m);
16762 return SQLITE_OK;
16763 }
16764
16765 /*
16766 ** This routine sets the value to be returned by subsequent calls to
16767 ** sqlite3_changes() on the database handle 'db'.
16768 */
16769 SQLITE_PRIVATE void sqlite3VdbeSetChanges(sqlite3 *db, int nChange){
16770 assert( sqlite3_mutex_held(db->mutex) );
16771 db->nChange = nChange;
16772 db->nTotalChange += nChange;
16773 }
16774
16775 /*
16776 ** Set a flag in the vdbe to update the change counter when it is finalised
16777 ** or reset.
16778 */
16779 SQLITE_PRIVATE void sqlite3VdbeCountChanges(Vdbe *v){
16780 v->changeCntOn = 1;
16781 }
16782
16783 /*
16784 ** Mark every prepared statement associated with a database connection
16785 ** as expired.
16786 **
16787 ** An expired statement means that recompilation of the statement is
16788 ** recommend. Statements expire when things happen that make their
16789 ** programs obsolete. Removing user-defined functions or collating
16790 ** sequences, or changing an authorization function are the types of
16791 ** things that make prepared statements obsolete.
16792 */
16793 SQLITE_PRIVATE void sqlite3ExpirePreparedStatements(sqlite3 *db){
16794 Vdbe *p;
16795 for(p = db->pVdbe; p; p=p->pNext){
16796 p->expired = 1;
16797 }
16798 }
16799
16800 /*
16801 ** Return the database associated with the Vdbe.
16802 */
16803 SQLITE_PRIVATE sqlite3 *sqlite3VdbeDb(Vdbe *v){
16804 return v->db;
16805 }
16806
16807 /*
16808 ** Return a pointer to an sqlite3_value structure containing the value bound
16809 ** parameter iVar of VM v. Except, if the value is an SQL NULL, return
16810 ** 0 instead. Unless it is NULL, apply affinity aff (one of the SQLITE_AFF_*
16811 ** constants) to the value before returning it.
16812 **
16813 ** The returned value must be freed by the caller using sqlite3ValueFree().
16814 */
16815 SQLITE_PRIVATE sqlite3_value *sqlite3VdbeGetBoundValue(Vdbe *v, int iVar, u8 aff ){
16816 assert( iVar>0 );
16817 if( v ){
16818 Mem *pMem = &v->aVar[iVar-1];
16819 if( 0==(pMem->flags & MEM_Null) ){
16820 sqlite3_value *pRet = sqlite3ValueNew(v->db);
16821 if( pRet ){
16822 sqlite3VdbeMemCopy((Mem *)pRet, pMem);
16823 sqlite3ValueApplyAffinity(pRet, aff, SQLITE_UTF8);
16824 }
16825 return pRet;
16826 }
16827 }
16828 return 0;
16829 }
16830
16831 /*
16832 ** Configure SQL variable iVar so that binding a new value to it signals
16833 ** to sqlite3_reoptimize() that re-preparing the statement may result
16834 ** in a better query plan.
16835 */
16836 SQLITE_PRIVATE void sqlite3VdbeSetVarmask(Vdbe *v, int iVar){
16837 assert( iVar>0 );
16838 if( iVar>32 ){
16839 v->expmask = 0xffffffff;
16840 }else{
16841 v->expmask |= ((u32)1 << (iVar-1));
16842 }
16843 }
16844
16845 #ifndef SQLITE_OMIT_VIRTUALTABLE
16846 /*
16847 ** Transfer error message text from an sqlite3_vtab.zErrMsg (text stored
16848 ** in memory obtained from sqlite3_malloc) into a Vdbe.zErrMsg (text stored
16849 ** in memory obtained from sqlite3DbMalloc).
16850 */
16851 SQLITE_PRIVATE void sqlite3VtabImportErrmsg(Vdbe *p, sqlite3_vtab *pVtab){
16852 if( pVtab->zErrMsg ){
16853 sqlite3 *db = p->db;
16854 sqlite3DbFree(db, p->zErrMsg);
16855 p->zErrMsg = sqlite3DbStrDup(db, pVtab->zErrMsg);
16856 sqlite3_free(pVtab->zErrMsg);
16857 pVtab->zErrMsg = 0;
16858 }
16859 }
16860 #endif /* SQLITE_OMIT_VIRTUALTABLE */
16861
16862 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
16863
16864 /*
16865 ** If the second argument is not NULL, release any allocations associated
16866 ** with the memory cells in the p->aMem[] array. Also free the UnpackedRecord
16867 ** structure itself, using sqlite3DbFree().
16868 **
16869 ** This function is used to free UnpackedRecord structures allocated by
16870 ** the vdbeUnpackRecord() function found in vdbeapi.c.
16871 */
16872 static void vdbeFreeUnpacked(sqlite3 *db, int nField, UnpackedRecord *p){
16873 if( p ){
16874 int i;
16875 for(i=0; i<nField; i++){
16876 Mem *pMem = &p->aMem[i];
16877 if( pMem->zMalloc ) sqlite3VdbeMemRelease(pMem);
16878 }
16879 sqlite3DbFree(db, p);
16880 }
16881 }
16882 #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
16883
16884 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
16885 /*
16886 ** Invoke the pre-update hook. If this is an UPDATE or DELETE pre-update call,
16887 ** then cursor passed as the second argument should point to the row about
16888 ** to be update or deleted. If the application calls sqlite3_preupdate_old(),
16889 ** the required value will be read from the row the cursor points to.
16890 */
16891 SQLITE_PRIVATE void sqlite3VdbePreUpdateHook(
16892 Vdbe *v, /* Vdbe pre-update hook is invoked by */
16893 VdbeCursor *pCsr, /* Cursor to grab old.* values from */
16894 int op, /* SQLITE_INSERT, UPDATE or DELETE */
16895 const char *zDb, /* Database name */
16896 Table *pTab, /* Modified table */
16897 i64 iKey1, /* Initial key value */
16898 int iReg /* Register for new.* record */
16899 ){
16900 sqlite3 *db = v->db;
16901 i64 iKey2;
16902 PreUpdate preupdate;
16903 const char *zTbl = pTab->zName;
16904 static const u8 fakeSortOrder = 0;
16905
16906 assert( db->pPreUpdate==0 );
16907 memset(&preupdate, 0, sizeof(PreUpdate));
16908 if( HasRowid(pTab)==0 ){
16909 iKey1 = iKey2 = 0;
16910 preupdate.pPk = sqlite3PrimaryKeyIndex(pTab);
16911 }else{
16912 if( op==SQLITE_UPDATE ){
16913 iKey2 = v->aMem[iReg].u.i;
16914 }else{
16915 iKey2 = iKey1;
16916 }
16917 }
16918
16919 assert( pCsr->nField==pTab->nCol
16920 || (pCsr->nField==pTab->nCol+1 && op==SQLITE_DELETE && iReg==-1)
16921 );
16922
16923 preupdate.v = v;
16924 preupdate.pCsr = pCsr;
16925 preupdate.op = op;
16926 preupdate.iNewReg = iReg;
16927 preupdate.keyinfo.db = db;
16928 preupdate.keyinfo.enc = ENC(db);
16929 preupdate.keyinfo.nField = pTab->nCol;
16930 preupdate.keyinfo.aSortOrder = (u8*)&fakeSortOrder;
16931 preupdate.iKey1 = iKey1;
16932 preupdate.iKey2 = iKey2;
16933 preupdate.pTab = pTab;
16934
16935 db->pPreUpdate = &preupdate;
16936 db->xPreUpdateCallback(db->pPreUpdateArg, db, op, zDb, zTbl, iKey1, iKey2);
16937 db->pPreUpdate = 0;
16938 sqlite3DbFree(db, preupdate.aRecord);
16939 vdbeFreeUnpacked(db, preupdate.keyinfo.nField+1, preupdate.pUnpacked);
16940 vdbeFreeUnpacked(db, preupdate.keyinfo.nField+1, preupdate.pNewUnpacked);
16941 if( preupdate.aNew ){
16942 int i;
16943 for(i=0; i<pCsr->nField; i++){
16944 sqlite3VdbeMemRelease(&preupdate.aNew[i]);
16945 }
16946 sqlite3DbFree(db, preupdate.aNew);
16947 }
16948 }
16949 #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
16950
16951 /************** End of vdbeaux.c *********************************************/
16952 /************** Begin file vdbeapi.c *****************************************/
16953 /*
16954 ** 2004 May 26
16955 **
16956 ** The author disclaims copyright to this source code. In place of
16957 ** a legal notice, here is a blessing:
16958 **
16959 ** May you do good and not evil.
16960 ** May you find forgiveness for yourself and forgive others.
16961 ** May you share freely, never taking more than you give.
16962 **
16963 *************************************************************************
16964 **
16965 ** This file contains code use to implement APIs that are part of the
16966 ** VDBE.
16967 */
16968 /* #include "sqliteInt.h" */
16969 /* #include "vdbeInt.h" */
16970
16971 #ifndef SQLITE_OMIT_DEPRECATED
16972 /*
16973 ** Return TRUE (non-zero) of the statement supplied as an argument needs
16974 ** to be recompiled. A statement needs to be recompiled whenever the
16975 ** execution environment changes in a way that would alter the program
16976 ** that sqlite3_prepare() generates. For example, if new functions or
16977 ** collating sequences are registered or if an authorizer function is
16978 ** added or changed.
16979 */
16980 SQLITE_API int sqlite3_expired(sqlite3_stmt *pStmt){
16981 Vdbe *p = (Vdbe*)pStmt;
16982 return p==0 || p->expired;
16983 }
16984 #endif
16985
16986 /*
16987 ** Check on a Vdbe to make sure it has not been finalized. Log
16988 ** an error and return true if it has been finalized (or is otherwise
16989 ** invalid). Return false if it is ok.
16990 */
16991 static int vdbeSafety(Vdbe *p){
16992 if( p->db==0 ){
16993 sqlite3_log(SQLITE_MISUSE, "API called with finalized prepared statement");
16994 return 1;
16995 }else{
16996 return 0;
16997 }
16998 }
16999 static int vdbeSafetyNotNull(Vdbe *p){
17000 if( p==0 ){
17001 sqlite3_log(SQLITE_MISUSE, "API called with NULL prepared statement");
17002 return 1;
17003 }else{
17004 return vdbeSafety(p);
17005 }
17006 }
17007
17008 #ifndef SQLITE_OMIT_TRACE
17009 /*
17010 ** Invoke the profile callback. This routine is only called if we already
17011 ** know that the profile callback is defined and needs to be invoked.
17012 */
17013 static SQLITE_NOINLINE void invokeProfileCallback(sqlite3 *db, Vdbe *p){
17014 sqlite3_int64 iNow;
17015 sqlite3_int64 iElapse;
17016 assert( p->startTime>0 );
17017 assert( db->xProfile!=0 || (db->mTrace & SQLITE_TRACE_PROFILE)!=0 );
17018 assert( db->init.busy==0 );
17019 assert( p->zSql!=0 );
17020 sqlite3OsCurrentTimeInt64(db->pVfs, &iNow);
17021 iElapse = (iNow - p->startTime)*1000000;
17022 if( db->xProfile ){
17023 db->xProfile(db->pProfileArg, p->zSql, iElapse);
17024 }
17025 if( db->mTrace & SQLITE_TRACE_PROFILE ){
17026 db->xTrace(SQLITE_TRACE_PROFILE, db->pTraceArg, p, (void*)&iElapse);
17027 }
17028 p->startTime = 0;
17029 }
17030 /*
17031 ** The checkProfileCallback(DB,P) macro checks to see if a profile callback
17032 ** is needed, and it invokes the callback if it is needed.
17033 */
17034 # define checkProfileCallback(DB,P) \
17035 if( ((P)->startTime)>0 ){ invokeProfileCallback(DB,P); }
17036 #else
17037 # define checkProfileCallback(DB,P) /*no-op*/
17038 #endif
17039
17040 /*
17041 ** The following routine destroys a virtual machine that is created by
17042 ** the sqlite3_compile() routine. The integer returned is an SQLITE_
17043 ** success/failure code that describes the result of executing the virtual
17044 ** machine.
17045 **
17046 ** This routine sets the error code and string returned by
17047 ** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
17048 */
17049 SQLITE_API int sqlite3_finalize(sqlite3_stmt *pStmt){
17050 int rc;
17051 if( pStmt==0 ){
17052 /* IMPLEMENTATION-OF: R-57228-12904 Invoking sqlite3_finalize() on a NULL
17053 ** pointer is a harmless no-op. */
17054 rc = SQLITE_OK;
17055 }else{
17056 Vdbe *v = (Vdbe*)pStmt;
17057 sqlite3 *db = v->db;
17058 if( vdbeSafety(v) ) return SQLITE_MISUSE_BKPT;
17059 sqlite3_mutex_enter(db->mutex);
17060 checkProfileCallback(db, v);
17061 rc = sqlite3VdbeFinalize(v);
17062 rc = sqlite3ApiExit(db, rc);
17063 sqlite3LeaveMutexAndCloseZombie(db);
17064 }
17065 return rc;
17066 }
17067
17068 /*
17069 ** Terminate the current execution of an SQL statement and reset it
17070 ** back to its starting state so that it can be reused. A success code from
17071 ** the prior execution is returned.
17072 **
17073 ** This routine sets the error code and string returned by
17074 ** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
17075 */
17076 SQLITE_API int sqlite3_reset(sqlite3_stmt *pStmt){
17077 int rc;
17078 if( pStmt==0 ){
17079 rc = SQLITE_OK;
17080 }else{
17081 Vdbe *v = (Vdbe*)pStmt;
17082 sqlite3 *db = v->db;
17083 sqlite3_mutex_enter(db->mutex);
17084 checkProfileCallback(db, v);
17085 rc = sqlite3VdbeReset(v);
17086 sqlite3VdbeRewind(v);
17087 assert( (rc & (db->errMask))==rc );
17088 rc = sqlite3ApiExit(db, rc);
17089 sqlite3_mutex_leave(db->mutex);
17090 }
17091 return rc;
17092 }
17093
17094 /*
17095 ** Set all the parameters in the compiled SQL statement to NULL.
17096 */
17097 SQLITE_API int sqlite3_clear_bindings(sqlite3_stmt *pStmt){
17098 int i;
17099 int rc = SQLITE_OK;
17100 Vdbe *p = (Vdbe*)pStmt;
17101 #if SQLITE_THREADSAFE
17102 sqlite3_mutex *mutex = ((Vdbe*)pStmt)->db->mutex;
17103 #endif
17104 sqlite3_mutex_enter(mutex);
17105 for(i=0; i<p->nVar; i++){
17106 sqlite3VdbeMemRelease(&p->aVar[i]);
17107 p->aVar[i].flags = MEM_Null;
17108 }
17109 if( p->isPrepareV2 && p->expmask ){
17110 p->expired = 1;
17111 }
17112 sqlite3_mutex_leave(mutex);
17113 return rc;
17114 }
17115
17116
17117 /**************************** sqlite3_value_ *******************************
17118 ** The following routines extract information from a Mem or sqlite3_value
17119 ** structure.
17120 */
17121 SQLITE_API const void *sqlite3_value_blob(sqlite3_value *pVal){
17122 Mem *p = (Mem*)pVal;
17123 if( p->flags & (MEM_Blob|MEM_Str) ){
17124 if( ExpandBlob(p)!=SQLITE_OK ){
17125 assert( p->flags==MEM_Null && p->z==0 );
17126 return 0;
17127 }
17128 p->flags |= MEM_Blob;
17129 return p->n ? p->z : 0;
17130 }else{
17131 return sqlite3_value_text(pVal);
17132 }
17133 }
17134 SQLITE_API int sqlite3_value_bytes(sqlite3_value *pVal){
17135 return sqlite3ValueBytes(pVal, SQLITE_UTF8);
17136 }
17137 SQLITE_API int sqlite3_value_bytes16(sqlite3_value *pVal){
17138 return sqlite3ValueBytes(pVal, SQLITE_UTF16NATIVE);
17139 }
17140 SQLITE_API double sqlite3_value_double(sqlite3_value *pVal){
17141 return sqlite3VdbeRealValue((Mem*)pVal);
17142 }
17143 SQLITE_API int sqlite3_value_int(sqlite3_value *pVal){
17144 return (int)sqlite3VdbeIntValue((Mem*)pVal);
17145 }
17146 SQLITE_API sqlite_int64 sqlite3_value_int64(sqlite3_value *pVal){
17147 return sqlite3VdbeIntValue((Mem*)pVal);
17148 }
17149 SQLITE_API unsigned int sqlite3_value_subtype(sqlite3_value *pVal){
17150 Mem *pMem = (Mem*)pVal;
17151 return ((pMem->flags & MEM_Subtype) ? pMem->eSubtype : 0);
17152 }
17153 SQLITE_API const unsigned char *sqlite3_value_text(sqlite3_value *pVal){
17154 return (const unsigned char *)sqlite3ValueText(pVal, SQLITE_UTF8);
17155 }
17156 #ifndef SQLITE_OMIT_UTF16
17157 SQLITE_API const void *sqlite3_value_text16(sqlite3_value* pVal){
17158 return sqlite3ValueText(pVal, SQLITE_UTF16NATIVE);
17159 }
17160 SQLITE_API const void *sqlite3_value_text16be(sqlite3_value *pVal){
17161 return sqlite3ValueText(pVal, SQLITE_UTF16BE);
17162 }
17163 SQLITE_API const void *sqlite3_value_text16le(sqlite3_value *pVal){
17164 return sqlite3ValueText(pVal, SQLITE_UTF16LE);
17165 }
17166 #endif /* SQLITE_OMIT_UTF16 */
17167 /* EVIDENCE-OF: R-12793-43283 Every value in SQLite has one of five
17168 ** fundamental datatypes: 64-bit signed integer 64-bit IEEE floating
17169 ** point number string BLOB NULL
17170 */
17171 SQLITE_API int sqlite3_value_type(sqlite3_value* pVal){
17172 static const u8 aType[] = {
17173 SQLITE_BLOB, /* 0x00 */
17174 SQLITE_NULL, /* 0x01 */
17175 SQLITE_TEXT, /* 0x02 */
17176 SQLITE_NULL, /* 0x03 */
17177 SQLITE_INTEGER, /* 0x04 */
17178 SQLITE_NULL, /* 0x05 */
17179 SQLITE_INTEGER, /* 0x06 */
17180 SQLITE_NULL, /* 0x07 */
17181 SQLITE_FLOAT, /* 0x08 */
17182 SQLITE_NULL, /* 0x09 */
17183 SQLITE_FLOAT, /* 0x0a */
17184 SQLITE_NULL, /* 0x0b */
17185 SQLITE_INTEGER, /* 0x0c */
17186 SQLITE_NULL, /* 0x0d */
17187 SQLITE_INTEGER, /* 0x0e */
17188 SQLITE_NULL, /* 0x0f */
17189 SQLITE_BLOB, /* 0x10 */
17190 SQLITE_NULL, /* 0x11 */
17191 SQLITE_TEXT, /* 0x12 */
17192 SQLITE_NULL, /* 0x13 */
17193 SQLITE_INTEGER, /* 0x14 */
17194 SQLITE_NULL, /* 0x15 */
17195 SQLITE_INTEGER, /* 0x16 */
17196 SQLITE_NULL, /* 0x17 */
17197 SQLITE_FLOAT, /* 0x18 */
17198 SQLITE_NULL, /* 0x19 */
17199 SQLITE_FLOAT, /* 0x1a */
17200 SQLITE_NULL, /* 0x1b */
17201 SQLITE_INTEGER, /* 0x1c */
17202 SQLITE_NULL, /* 0x1d */
17203 SQLITE_INTEGER, /* 0x1e */
17204 SQLITE_NULL, /* 0x1f */
17205 };
17206 return aType[pVal->flags&MEM_AffMask];
17207 }
17208
17209 /* Make a copy of an sqlite3_value object
17210 */
17211 SQLITE_API sqlite3_value *sqlite3_value_dup(const sqlite3_value *pOrig){
17212 sqlite3_value *pNew;
17213 if( pOrig==0 ) return 0;
17214 pNew = sqlite3_malloc( sizeof(*pNew) );
17215 if( pNew==0 ) return 0;
17216 memset(pNew, 0, sizeof(*pNew));
17217 memcpy(pNew, pOrig, MEMCELLSIZE);
17218 pNew->flags &= ~MEM_Dyn;
17219 pNew->db = 0;
17220 if( pNew->flags&(MEM_Str|MEM_Blob) ){
17221 pNew->flags &= ~(MEM_Static|MEM_Dyn);
17222 pNew->flags |= MEM_Ephem;
17223 if( sqlite3VdbeMemMakeWriteable(pNew)!=SQLITE_OK ){
17224 sqlite3ValueFree(pNew);
17225 pNew = 0;
17226 }
17227 }
17228 return pNew;
17229 }
17230
17231 /* Destroy an sqlite3_value object previously obtained from
17232 ** sqlite3_value_dup().
17233 */
17234 SQLITE_API void sqlite3_value_free(sqlite3_value *pOld){
17235 sqlite3ValueFree(pOld);
17236 }
17237
17238
17239 /**************************** sqlite3_result_ *******************************
17240 ** The following routines are used by user-defined functions to specify
17241 ** the function result.
17242 **
17243 ** The setStrOrError() function calls sqlite3VdbeMemSetStr() to store the
17244 ** result as a string or blob but if the string or blob is too large, it
17245 ** then sets the error code to SQLITE_TOOBIG
17246 **
17247 ** The invokeValueDestructor(P,X) routine invokes destructor function X()
17248 ** on value P is not going to be used and need to be destroyed.
17249 */
17250 static void setResultStrOrError(
17251 sqlite3_context *pCtx, /* Function context */
17252 const char *z, /* String pointer */
17253 int n, /* Bytes in string, or negative */
17254 u8 enc, /* Encoding of z. 0 for BLOBs */
17255 void (*xDel)(void*) /* Destructor function */
17256 ){
17257 if( sqlite3VdbeMemSetStr(pCtx->pOut, z, n, enc, xDel)==SQLITE_TOOBIG ){
17258 sqlite3_result_error_toobig(pCtx);
17259 }
17260 }
17261 static int invokeValueDestructor(
17262 const void *p, /* Value to destroy */
17263 void (*xDel)(void*), /* The destructor */
17264 sqlite3_context *pCtx /* Set a SQLITE_TOOBIG error if no NULL */
17265 ){
17266 assert( xDel!=SQLITE_DYNAMIC );
17267 if( xDel==0 ){
17268 /* noop */
17269 }else if( xDel==SQLITE_TRANSIENT ){
17270 /* noop */
17271 }else{
17272 xDel((void*)p);
17273 }
17274 if( pCtx ) sqlite3_result_error_toobig(pCtx);
17275 return SQLITE_TOOBIG;
17276 }
17277 SQLITE_API void sqlite3_result_blob(
17278 sqlite3_context *pCtx,
17279 const void *z,
17280 int n,
17281 void (*xDel)(void *)
17282 ){
17283 assert( n>=0 );
17284 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
17285 setResultStrOrError(pCtx, z, n, 0, xDel);
17286 }
17287 SQLITE_API void sqlite3_result_blob64(
17288 sqlite3_context *pCtx,
17289 const void *z,
17290 sqlite3_uint64 n,
17291 void (*xDel)(void *)
17292 ){
17293 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
17294 assert( xDel!=SQLITE_DYNAMIC );
17295 if( n>0x7fffffff ){
17296 (void)invokeValueDestructor(z, xDel, pCtx);
17297 }else{
17298 setResultStrOrError(pCtx, z, (int)n, 0, xDel);
17299 }
17300 }
17301 SQLITE_API void sqlite3_result_double(sqlite3_context *pCtx, double rVal){
17302 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
17303 sqlite3VdbeMemSetDouble(pCtx->pOut, rVal);
17304 }
17305 SQLITE_API void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n ){
17306 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
17307 pCtx->isError = SQLITE_ERROR;
17308 pCtx->fErrorOrAux = 1;
17309 sqlite3VdbeMemSetStr(pCtx->pOut, z, n, SQLITE_UTF8, SQLITE_TRANSIENT);
17310 }
17311 #ifndef SQLITE_OMIT_UTF16
17312 SQLITE_API void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){
17313 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
17314 pCtx->isError = SQLITE_ERROR;
17315 pCtx->fErrorOrAux = 1;
17316 sqlite3VdbeMemSetStr(pCtx->pOut, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT);
17317 }
17318 #endif
17319 SQLITE_API void sqlite3_result_int(sqlite3_context *pCtx, int iVal){
17320 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
17321 sqlite3VdbeMemSetInt64(pCtx->pOut, (i64)iVal);
17322 }
17323 SQLITE_API void sqlite3_result_int64(sqlite3_context *pCtx, i64 iVal){
17324 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
17325 sqlite3VdbeMemSetInt64(pCtx->pOut, iVal);
17326 }
17327 SQLITE_API void sqlite3_result_null(sqlite3_context *pCtx){
17328 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
17329 sqlite3VdbeMemSetNull(pCtx->pOut);
17330 }
17331 SQLITE_API void sqlite3_result_subtype(sqlite3_context *pCtx, unsigned int eSubt ype){
17332 Mem *pOut = pCtx->pOut;
17333 assert( sqlite3_mutex_held(pOut->db->mutex) );
17334 pOut->eSubtype = eSubtype & 0xff;
17335 pOut->flags |= MEM_Subtype;
17336 }
17337 SQLITE_API void sqlite3_result_text(
17338 sqlite3_context *pCtx,
17339 const char *z,
17340 int n,
17341 void (*xDel)(void *)
17342 ){
17343 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
17344 setResultStrOrError(pCtx, z, n, SQLITE_UTF8, xDel);
17345 }
17346 SQLITE_API void sqlite3_result_text64(
17347 sqlite3_context *pCtx,
17348 const char *z,
17349 sqlite3_uint64 n,
17350 void (*xDel)(void *),
17351 unsigned char enc
17352 ){
17353 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
17354 assert( xDel!=SQLITE_DYNAMIC );
17355 if( enc==SQLITE_UTF16 ) enc = SQLITE_UTF16NATIVE;
17356 if( n>0x7fffffff ){
17357 (void)invokeValueDestructor(z, xDel, pCtx);
17358 }else{
17359 setResultStrOrError(pCtx, z, (int)n, enc, xDel);
17360 }
17361 }
17362 #ifndef SQLITE_OMIT_UTF16
17363 SQLITE_API void sqlite3_result_text16(
17364 sqlite3_context *pCtx,
17365 const void *z,
17366 int n,
17367 void (*xDel)(void *)
17368 ){
17369 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
17370 setResultStrOrError(pCtx, z, n, SQLITE_UTF16NATIVE, xDel);
17371 }
17372 SQLITE_API void sqlite3_result_text16be(
17373 sqlite3_context *pCtx,
17374 const void *z,
17375 int n,
17376 void (*xDel)(void *)
17377 ){
17378 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
17379 setResultStrOrError(pCtx, z, n, SQLITE_UTF16BE, xDel);
17380 }
17381 SQLITE_API void sqlite3_result_text16le(
17382 sqlite3_context *pCtx,
17383 const void *z,
17384 int n,
17385 void (*xDel)(void *)
17386 ){
17387 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
17388 setResultStrOrError(pCtx, z, n, SQLITE_UTF16LE, xDel);
17389 }
17390 #endif /* SQLITE_OMIT_UTF16 */
17391 SQLITE_API void sqlite3_result_value(sqlite3_context *pCtx, sqlite3_value *pValu e){
17392 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
17393 sqlite3VdbeMemCopy(pCtx->pOut, pValue);
17394 }
17395 SQLITE_API void sqlite3_result_zeroblob(sqlite3_context *pCtx, int n){
17396 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
17397 sqlite3VdbeMemSetZeroBlob(pCtx->pOut, n);
17398 }
17399 SQLITE_API int sqlite3_result_zeroblob64(sqlite3_context *pCtx, u64 n){
17400 Mem *pOut = pCtx->pOut;
17401 assert( sqlite3_mutex_held(pOut->db->mutex) );
17402 if( n>(u64)pOut->db->aLimit[SQLITE_LIMIT_LENGTH] ){
17403 return SQLITE_TOOBIG;
17404 }
17405 sqlite3VdbeMemSetZeroBlob(pCtx->pOut, (int)n);
17406 return SQLITE_OK;
17407 }
17408 SQLITE_API void sqlite3_result_error_code(sqlite3_context *pCtx, int errCode){
17409 pCtx->isError = errCode;
17410 pCtx->fErrorOrAux = 1;
17411 #ifdef SQLITE_DEBUG
17412 if( pCtx->pVdbe ) pCtx->pVdbe->rcApp = errCode;
17413 #endif
17414 if( pCtx->pOut->flags & MEM_Null ){
17415 sqlite3VdbeMemSetStr(pCtx->pOut, sqlite3ErrStr(errCode), -1,
17416 SQLITE_UTF8, SQLITE_STATIC);
17417 }
17418 }
17419
17420 /* Force an SQLITE_TOOBIG error. */
17421 SQLITE_API void sqlite3_result_error_toobig(sqlite3_context *pCtx){
17422 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
17423 pCtx->isError = SQLITE_TOOBIG;
17424 pCtx->fErrorOrAux = 1;
17425 sqlite3VdbeMemSetStr(pCtx->pOut, "string or blob too big", -1,
17426 SQLITE_UTF8, SQLITE_STATIC);
17427 }
17428
17429 /* An SQLITE_NOMEM error. */
17430 SQLITE_API void sqlite3_result_error_nomem(sqlite3_context *pCtx){
17431 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
17432 sqlite3VdbeMemSetNull(pCtx->pOut);
17433 pCtx->isError = SQLITE_NOMEM_BKPT;
17434 pCtx->fErrorOrAux = 1;
17435 sqlite3OomFault(pCtx->pOut->db);
17436 }
17437
17438 /*
17439 ** This function is called after a transaction has been committed. It
17440 ** invokes callbacks registered with sqlite3_wal_hook() as required.
17441 */
17442 static int doWalCallbacks(sqlite3 *db){
17443 int rc = SQLITE_OK;
17444 #ifndef SQLITE_OMIT_WAL
17445 int i;
17446 for(i=0; i<db->nDb; i++){
17447 Btree *pBt = db->aDb[i].pBt;
17448 if( pBt ){
17449 int nEntry;
17450 sqlite3BtreeEnter(pBt);
17451 nEntry = sqlite3PagerWalCallback(sqlite3BtreePager(pBt));
17452 sqlite3BtreeLeave(pBt);
17453 if( db->xWalCallback && nEntry>0 && rc==SQLITE_OK ){
17454 rc = db->xWalCallback(db->pWalArg, db, db->aDb[i].zDbSName, nEntry);
17455 }
17456 }
17457 }
17458 #endif
17459 return rc;
17460 }
17461
17462
17463 /*
17464 ** Execute the statement pStmt, either until a row of data is ready, the
17465 ** statement is completely executed or an error occurs.
17466 **
17467 ** This routine implements the bulk of the logic behind the sqlite_step()
17468 ** API. The only thing omitted is the automatic recompile if a
17469 ** schema change has occurred. That detail is handled by the
17470 ** outer sqlite3_step() wrapper procedure.
17471 */
17472 static int sqlite3Step(Vdbe *p){
17473 sqlite3 *db;
17474 int rc;
17475
17476 assert(p);
17477 if( p->magic!=VDBE_MAGIC_RUN ){
17478 /* We used to require that sqlite3_reset() be called before retrying
17479 ** sqlite3_step() after any error or after SQLITE_DONE. But beginning
17480 ** with version 3.7.0, we changed this so that sqlite3_reset() would
17481 ** be called automatically instead of throwing the SQLITE_MISUSE error.
17482 ** This "automatic-reset" change is not technically an incompatibility,
17483 ** since any application that receives an SQLITE_MISUSE is broken by
17484 ** definition.
17485 **
17486 ** Nevertheless, some published applications that were originally written
17487 ** for version 3.6.23 or earlier do in fact depend on SQLITE_MISUSE
17488 ** returns, and those were broken by the automatic-reset change. As a
17489 ** a work-around, the SQLITE_OMIT_AUTORESET compile-time restores the
17490 ** legacy behavior of returning SQLITE_MISUSE for cases where the
17491 ** previous sqlite3_step() returned something other than a SQLITE_LOCKED
17492 ** or SQLITE_BUSY error.
17493 */
17494 #ifdef SQLITE_OMIT_AUTORESET
17495 if( (rc = p->rc&0xff)==SQLITE_BUSY || rc==SQLITE_LOCKED ){
17496 sqlite3_reset((sqlite3_stmt*)p);
17497 }else{
17498 return SQLITE_MISUSE_BKPT;
17499 }
17500 #else
17501 sqlite3_reset((sqlite3_stmt*)p);
17502 #endif
17503 }
17504
17505 /* Check that malloc() has not failed. If it has, return early. */
17506 db = p->db;
17507 if( db->mallocFailed ){
17508 p->rc = SQLITE_NOMEM;
17509 return SQLITE_NOMEM_BKPT;
17510 }
17511
17512 if( p->pc<=0 && p->expired ){
17513 p->rc = SQLITE_SCHEMA;
17514 rc = SQLITE_ERROR;
17515 goto end_of_step;
17516 }
17517 if( p->pc<0 ){
17518 /* If there are no other statements currently running, then
17519 ** reset the interrupt flag. This prevents a call to sqlite3_interrupt
17520 ** from interrupting a statement that has not yet started.
17521 */
17522 if( db->nVdbeActive==0 ){
17523 db->u1.isInterrupted = 0;
17524 }
17525
17526 assert( db->nVdbeWrite>0 || db->autoCommit==0
17527 || (db->nDeferredCons==0 && db->nDeferredImmCons==0)
17528 );
17529
17530 #ifndef SQLITE_OMIT_TRACE
17531 if( (db->xProfile || (db->mTrace & SQLITE_TRACE_PROFILE)!=0)
17532 && !db->init.busy && p->zSql ){
17533 sqlite3OsCurrentTimeInt64(db->pVfs, &p->startTime);
17534 }else{
17535 assert( p->startTime==0 );
17536 }
17537 #endif
17538
17539 db->nVdbeActive++;
17540 if( p->readOnly==0 ) db->nVdbeWrite++;
17541 if( p->bIsReader ) db->nVdbeRead++;
17542 p->pc = 0;
17543 }
17544 #ifdef SQLITE_DEBUG
17545 p->rcApp = SQLITE_OK;
17546 #endif
17547 #ifndef SQLITE_OMIT_EXPLAIN
17548 if( p->explain ){
17549 rc = sqlite3VdbeList(p);
17550 }else
17551 #endif /* SQLITE_OMIT_EXPLAIN */
17552 {
17553 db->nVdbeExec++;
17554 rc = sqlite3VdbeExec(p);
17555 db->nVdbeExec--;
17556 }
17557
17558 #ifndef SQLITE_OMIT_TRACE
17559 /* If the statement completed successfully, invoke the profile callback */
17560 if( rc!=SQLITE_ROW ) checkProfileCallback(db, p);
17561 #endif
17562
17563 if( rc==SQLITE_DONE ){
17564 assert( p->rc==SQLITE_OK );
17565 p->rc = doWalCallbacks(db);
17566 if( p->rc!=SQLITE_OK ){
17567 rc = SQLITE_ERROR;
17568 }
17569 }
17570
17571 db->errCode = rc;
17572 if( SQLITE_NOMEM==sqlite3ApiExit(p->db, p->rc) ){
17573 p->rc = SQLITE_NOMEM_BKPT;
17574 }
17575 end_of_step:
17576 /* At this point local variable rc holds the value that should be
17577 ** returned if this statement was compiled using the legacy
17578 ** sqlite3_prepare() interface. According to the docs, this can only
17579 ** be one of the values in the first assert() below. Variable p->rc
17580 ** contains the value that would be returned if sqlite3_finalize()
17581 ** were called on statement p.
17582 */
17583 assert( rc==SQLITE_ROW || rc==SQLITE_DONE || rc==SQLITE_ERROR
17584 || (rc&0xff)==SQLITE_BUSY || rc==SQLITE_MISUSE
17585 );
17586 assert( (p->rc!=SQLITE_ROW && p->rc!=SQLITE_DONE) || p->rc==p->rcApp );
17587 if( p->isPrepareV2 && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){
17588 /* If this statement was prepared using sqlite3_prepare_v2(), and an
17589 ** error has occurred, then return the error code in p->rc to the
17590 ** caller. Set the error code in the database handle to the same value.
17591 */
17592 rc = sqlite3VdbeTransferError(p);
17593 }
17594 return (rc&db->errMask);
17595 }
17596
17597 /*
17598 ** This is the top-level implementation of sqlite3_step(). Call
17599 ** sqlite3Step() to do most of the work. If a schema error occurs,
17600 ** call sqlite3Reprepare() and try again.
17601 */
17602 SQLITE_API int sqlite3_step(sqlite3_stmt *pStmt){
17603 int rc = SQLITE_OK; /* Result from sqlite3Step() */
17604 int rc2 = SQLITE_OK; /* Result from sqlite3Reprepare() */
17605 Vdbe *v = (Vdbe*)pStmt; /* the prepared statement */
17606 int cnt = 0; /* Counter to prevent infinite loop of reprepares */
17607 sqlite3 *db; /* The database connection */
17608
17609 if( vdbeSafetyNotNull(v) ){
17610 return SQLITE_MISUSE_BKPT;
17611 }
17612 db = v->db;
17613 sqlite3_mutex_enter(db->mutex);
17614 v->doingRerun = 0;
17615 while( (rc = sqlite3Step(v))==SQLITE_SCHEMA
17616 && cnt++ < SQLITE_MAX_SCHEMA_RETRY ){
17617 int savedPc = v->pc;
17618 rc2 = rc = sqlite3Reprepare(v);
17619 if( rc!=SQLITE_OK) break;
17620 sqlite3_reset(pStmt);
17621 if( savedPc>=0 ) v->doingRerun = 1;
17622 assert( v->expired==0 );
17623 }
17624 if( rc2!=SQLITE_OK ){
17625 /* This case occurs after failing to recompile an sql statement.
17626 ** The error message from the SQL compiler has already been loaded
17627 ** into the database handle. This block copies the error message
17628 ** from the database handle into the statement and sets the statement
17629 ** program counter to 0 to ensure that when the statement is
17630 ** finalized or reset the parser error message is available via
17631 ** sqlite3_errmsg() and sqlite3_errcode().
17632 */
17633 const char *zErr = (const char *)sqlite3_value_text(db->pErr);
17634 sqlite3DbFree(db, v->zErrMsg);
17635 if( !db->mallocFailed ){
17636 v->zErrMsg = sqlite3DbStrDup(db, zErr);
17637 v->rc = rc2;
17638 } else {
17639 v->zErrMsg = 0;
17640 v->rc = rc = SQLITE_NOMEM_BKPT;
17641 }
17642 }
17643 rc = sqlite3ApiExit(db, rc);
17644 sqlite3_mutex_leave(db->mutex);
17645 return rc;
17646 }
17647
17648
17649 /*
17650 ** Extract the user data from a sqlite3_context structure and return a
17651 ** pointer to it.
17652 */
17653 SQLITE_API void *sqlite3_user_data(sqlite3_context *p){
17654 assert( p && p->pFunc );
17655 return p->pFunc->pUserData;
17656 }
17657
17658 /*
17659 ** Extract the user data from a sqlite3_context structure and return a
17660 ** pointer to it.
17661 **
17662 ** IMPLEMENTATION-OF: R-46798-50301 The sqlite3_context_db_handle() interface
17663 ** returns a copy of the pointer to the database connection (the 1st
17664 ** parameter) of the sqlite3_create_function() and
17665 ** sqlite3_create_function16() routines that originally registered the
17666 ** application defined function.
17667 */
17668 SQLITE_API sqlite3 *sqlite3_context_db_handle(sqlite3_context *p){
17669 assert( p && p->pOut );
17670 return p->pOut->db;
17671 }
17672
17673 /*
17674 ** Return the current time for a statement. If the current time
17675 ** is requested more than once within the same run of a single prepared
17676 ** statement, the exact same time is returned for each invocation regardless
17677 ** of the amount of time that elapses between invocations. In other words,
17678 ** the time returned is always the time of the first call.
17679 */
17680 SQLITE_PRIVATE sqlite3_int64 sqlite3StmtCurrentTime(sqlite3_context *p){
17681 int rc;
17682 #ifndef SQLITE_ENABLE_STAT3_OR_STAT4
17683 sqlite3_int64 *piTime = &p->pVdbe->iCurrentTime;
17684 assert( p->pVdbe!=0 );
17685 #else
17686 sqlite3_int64 iTime = 0;
17687 sqlite3_int64 *piTime = p->pVdbe!=0 ? &p->pVdbe->iCurrentTime : &iTime;
17688 #endif
17689 if( *piTime==0 ){
17690 rc = sqlite3OsCurrentTimeInt64(p->pOut->db->pVfs, piTime);
17691 if( rc ) *piTime = 0;
17692 }
17693 return *piTime;
17694 }
17695
17696 /*
17697 ** The following is the implementation of an SQL function that always
17698 ** fails with an error message stating that the function is used in the
17699 ** wrong context. The sqlite3_overload_function() API might construct
17700 ** SQL function that use this routine so that the functions will exist
17701 ** for name resolution but are actually overloaded by the xFindFunction
17702 ** method of virtual tables.
17703 */
17704 SQLITE_PRIVATE void sqlite3InvalidFunction(
17705 sqlite3_context *context, /* The function calling context */
17706 int NotUsed, /* Number of arguments to the function */
17707 sqlite3_value **NotUsed2 /* Value of each argument */
17708 ){
17709 const char *zName = context->pFunc->zName;
17710 char *zErr;
17711 UNUSED_PARAMETER2(NotUsed, NotUsed2);
17712 zErr = sqlite3_mprintf(
17713 "unable to use function %s in the requested context", zName);
17714 sqlite3_result_error(context, zErr, -1);
17715 sqlite3_free(zErr);
17716 }
17717
17718 /*
17719 ** Create a new aggregate context for p and return a pointer to
17720 ** its pMem->z element.
17721 */
17722 static SQLITE_NOINLINE void *createAggContext(sqlite3_context *p, int nByte){
17723 Mem *pMem = p->pMem;
17724 assert( (pMem->flags & MEM_Agg)==0 );
17725 if( nByte<=0 ){
17726 sqlite3VdbeMemSetNull(pMem);
17727 pMem->z = 0;
17728 }else{
17729 sqlite3VdbeMemClearAndResize(pMem, nByte);
17730 pMem->flags = MEM_Agg;
17731 pMem->u.pDef = p->pFunc;
17732 if( pMem->z ){
17733 memset(pMem->z, 0, nByte);
17734 }
17735 }
17736 return (void*)pMem->z;
17737 }
17738
17739 /*
17740 ** Allocate or return the aggregate context for a user function. A new
17741 ** context is allocated on the first call. Subsequent calls return the
17742 ** same context that was returned on prior calls.
17743 */
17744 SQLITE_API void *sqlite3_aggregate_context(sqlite3_context *p, int nByte){
17745 assert( p && p->pFunc && p->pFunc->xFinalize );
17746 assert( sqlite3_mutex_held(p->pOut->db->mutex) );
17747 testcase( nByte<0 );
17748 if( (p->pMem->flags & MEM_Agg)==0 ){
17749 return createAggContext(p, nByte);
17750 }else{
17751 return (void*)p->pMem->z;
17752 }
17753 }
17754
17755 /*
17756 ** Return the auxiliary data pointer, if any, for the iArg'th argument to
17757 ** the user-function defined by pCtx.
17758 */
17759 SQLITE_API void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){
17760 AuxData *pAuxData;
17761
17762 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
17763 #if SQLITE_ENABLE_STAT3_OR_STAT4
17764 if( pCtx->pVdbe==0 ) return 0;
17765 #else
17766 assert( pCtx->pVdbe!=0 );
17767 #endif
17768 for(pAuxData=pCtx->pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNext){
17769 if( pAuxData->iOp==pCtx->iOp && pAuxData->iArg==iArg ) break;
17770 }
17771
17772 return (pAuxData ? pAuxData->pAux : 0);
17773 }
17774
17775 /*
17776 ** Set the auxiliary data pointer and delete function, for the iArg'th
17777 ** argument to the user-function defined by pCtx. Any previous value is
17778 ** deleted by calling the delete function specified when it was set.
17779 */
17780 SQLITE_API void sqlite3_set_auxdata(
17781 sqlite3_context *pCtx,
17782 int iArg,
17783 void *pAux,
17784 void (*xDelete)(void*)
17785 ){
17786 AuxData *pAuxData;
17787 Vdbe *pVdbe = pCtx->pVdbe;
17788
17789 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
17790 if( iArg<0 ) goto failed;
17791 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
17792 if( pVdbe==0 ) goto failed;
17793 #else
17794 assert( pVdbe!=0 );
17795 #endif
17796
17797 for(pAuxData=pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNext){
17798 if( pAuxData->iOp==pCtx->iOp && pAuxData->iArg==iArg ) break;
17799 }
17800 if( pAuxData==0 ){
17801 pAuxData = sqlite3DbMallocZero(pVdbe->db, sizeof(AuxData));
17802 if( !pAuxData ) goto failed;
17803 pAuxData->iOp = pCtx->iOp;
17804 pAuxData->iArg = iArg;
17805 pAuxData->pNext = pVdbe->pAuxData;
17806 pVdbe->pAuxData = pAuxData;
17807 if( pCtx->fErrorOrAux==0 ){
17808 pCtx->isError = 0;
17809 pCtx->fErrorOrAux = 1;
17810 }
17811 }else if( pAuxData->xDelete ){
17812 pAuxData->xDelete(pAuxData->pAux);
17813 }
17814
17815 pAuxData->pAux = pAux;
17816 pAuxData->xDelete = xDelete;
17817 return;
17818
17819 failed:
17820 if( xDelete ){
17821 xDelete(pAux);
17822 }
17823 }
17824
17825 #ifndef SQLITE_OMIT_DEPRECATED
17826 /*
17827 ** Return the number of times the Step function of an aggregate has been
17828 ** called.
17829 **
17830 ** This function is deprecated. Do not use it for new code. It is
17831 ** provide only to avoid breaking legacy code. New aggregate function
17832 ** implementations should keep their own counts within their aggregate
17833 ** context.
17834 */
17835 SQLITE_API int sqlite3_aggregate_count(sqlite3_context *p){
17836 assert( p && p->pMem && p->pFunc && p->pFunc->xFinalize );
17837 return p->pMem->n;
17838 }
17839 #endif
17840
17841 /*
17842 ** Return the number of columns in the result set for the statement pStmt.
17843 */
17844 SQLITE_API int sqlite3_column_count(sqlite3_stmt *pStmt){
17845 Vdbe *pVm = (Vdbe *)pStmt;
17846 return pVm ? pVm->nResColumn : 0;
17847 }
17848
17849 /*
17850 ** Return the number of values available from the current row of the
17851 ** currently executing statement pStmt.
17852 */
17853 SQLITE_API int sqlite3_data_count(sqlite3_stmt *pStmt){
17854 Vdbe *pVm = (Vdbe *)pStmt;
17855 if( pVm==0 || pVm->pResultSet==0 ) return 0;
17856 return pVm->nResColumn;
17857 }
17858
17859 /*
17860 ** Return a pointer to static memory containing an SQL NULL value.
17861 */
17862 static const Mem *columnNullValue(void){
17863 /* Even though the Mem structure contains an element
17864 ** of type i64, on certain architectures (x86) with certain compiler
17865 ** switches (-Os), gcc may align this Mem object on a 4-byte boundary
17866 ** instead of an 8-byte one. This all works fine, except that when
17867 ** running with SQLITE_DEBUG defined the SQLite code sometimes assert()s
17868 ** that a Mem structure is located on an 8-byte boundary. To prevent
17869 ** these assert()s from failing, when building with SQLITE_DEBUG defined
17870 ** using gcc, we force nullMem to be 8-byte aligned using the magical
17871 ** __attribute__((aligned(8))) macro. */
17872 static const Mem nullMem
17873 #if defined(SQLITE_DEBUG) && defined(__GNUC__)
17874 __attribute__((aligned(8)))
17875 #endif
17876 = {
17877 /* .u = */ {0},
17878 /* .flags = */ (u16)MEM_Null,
17879 /* .enc = */ (u8)0,
17880 /* .eSubtype = */ (u8)0,
17881 /* .n = */ (int)0,
17882 /* .z = */ (char*)0,
17883 /* .zMalloc = */ (char*)0,
17884 /* .szMalloc = */ (int)0,
17885 /* .uTemp = */ (u32)0,
17886 /* .db = */ (sqlite3*)0,
17887 /* .xDel = */ (void(*)(void*))0,
17888 #ifdef SQLITE_DEBUG
17889 /* .pScopyFrom = */ (Mem*)0,
17890 /* .pFiller = */ (void*)0,
17891 #endif
17892 };
17893 return &nullMem;
17894 }
17895
17896 /*
17897 ** Check to see if column iCol of the given statement is valid. If
17898 ** it is, return a pointer to the Mem for the value of that column.
17899 ** If iCol is not valid, return a pointer to a Mem which has a value
17900 ** of NULL.
17901 */
17902 static Mem *columnMem(sqlite3_stmt *pStmt, int i){
17903 Vdbe *pVm;
17904 Mem *pOut;
17905
17906 pVm = (Vdbe *)pStmt;
17907 if( pVm==0 ) return (Mem*)columnNullValue();
17908 assert( pVm->db );
17909 sqlite3_mutex_enter(pVm->db->mutex);
17910 if( pVm->pResultSet!=0 && i<pVm->nResColumn && i>=0 ){
17911 pOut = &pVm->pResultSet[i];
17912 }else{
17913 sqlite3Error(pVm->db, SQLITE_RANGE);
17914 pOut = (Mem*)columnNullValue();
17915 }
17916 return pOut;
17917 }
17918
17919 /*
17920 ** This function is called after invoking an sqlite3_value_XXX function on a
17921 ** column value (i.e. a value returned by evaluating an SQL expression in the
17922 ** select list of a SELECT statement) that may cause a malloc() failure. If
17923 ** malloc() has failed, the threads mallocFailed flag is cleared and the result
17924 ** code of statement pStmt set to SQLITE_NOMEM.
17925 **
17926 ** Specifically, this is called from within:
17927 **
17928 ** sqlite3_column_int()
17929 ** sqlite3_column_int64()
17930 ** sqlite3_column_text()
17931 ** sqlite3_column_text16()
17932 ** sqlite3_column_real()
17933 ** sqlite3_column_bytes()
17934 ** sqlite3_column_bytes16()
17935 ** sqiite3_column_blob()
17936 */
17937 static void columnMallocFailure(sqlite3_stmt *pStmt)
17938 {
17939 /* If malloc() failed during an encoding conversion within an
17940 ** sqlite3_column_XXX API, then set the return code of the statement to
17941 ** SQLITE_NOMEM. The next call to _step() (if any) will return SQLITE_ERROR
17942 ** and _finalize() will return NOMEM.
17943 */
17944 Vdbe *p = (Vdbe *)pStmt;
17945 if( p ){
17946 assert( p->db!=0 );
17947 assert( sqlite3_mutex_held(p->db->mutex) );
17948 p->rc = sqlite3ApiExit(p->db, p->rc);
17949 sqlite3_mutex_leave(p->db->mutex);
17950 }
17951 }
17952
17953 /**************************** sqlite3_column_ *******************************
17954 ** The following routines are used to access elements of the current row
17955 ** in the result set.
17956 */
17957 SQLITE_API const void *sqlite3_column_blob(sqlite3_stmt *pStmt, int i){
17958 const void *val;
17959 val = sqlite3_value_blob( columnMem(pStmt,i) );
17960 /* Even though there is no encoding conversion, value_blob() might
17961 ** need to call malloc() to expand the result of a zeroblob()
17962 ** expression.
17963 */
17964 columnMallocFailure(pStmt);
17965 return val;
17966 }
17967 SQLITE_API int sqlite3_column_bytes(sqlite3_stmt *pStmt, int i){
17968 int val = sqlite3_value_bytes( columnMem(pStmt,i) );
17969 columnMallocFailure(pStmt);
17970 return val;
17971 }
17972 SQLITE_API int sqlite3_column_bytes16(sqlite3_stmt *pStmt, int i){
17973 int val = sqlite3_value_bytes16( columnMem(pStmt,i) );
17974 columnMallocFailure(pStmt);
17975 return val;
17976 }
17977 SQLITE_API double sqlite3_column_double(sqlite3_stmt *pStmt, int i){
17978 double val = sqlite3_value_double( columnMem(pStmt,i) );
17979 columnMallocFailure(pStmt);
17980 return val;
17981 }
17982 SQLITE_API int sqlite3_column_int(sqlite3_stmt *pStmt, int i){
17983 int val = sqlite3_value_int( columnMem(pStmt,i) );
17984 columnMallocFailure(pStmt);
17985 return val;
17986 }
17987 SQLITE_API sqlite_int64 sqlite3_column_int64(sqlite3_stmt *pStmt, int i){
17988 sqlite_int64 val = sqlite3_value_int64( columnMem(pStmt,i) );
17989 columnMallocFailure(pStmt);
17990 return val;
17991 }
17992 SQLITE_API const unsigned char *sqlite3_column_text(sqlite3_stmt *pStmt, int i){
17993 const unsigned char *val = sqlite3_value_text( columnMem(pStmt,i) );
17994 columnMallocFailure(pStmt);
17995 return val;
17996 }
17997 SQLITE_API sqlite3_value *sqlite3_column_value(sqlite3_stmt *pStmt, int i){
17998 Mem *pOut = columnMem(pStmt, i);
17999 if( pOut->flags&MEM_Static ){
18000 pOut->flags &= ~MEM_Static;
18001 pOut->flags |= MEM_Ephem;
18002 }
18003 columnMallocFailure(pStmt);
18004 return (sqlite3_value *)pOut;
18005 }
18006 #ifndef SQLITE_OMIT_UTF16
18007 SQLITE_API const void *sqlite3_column_text16(sqlite3_stmt *pStmt, int i){
18008 const void *val = sqlite3_value_text16( columnMem(pStmt,i) );
18009 columnMallocFailure(pStmt);
18010 return val;
18011 }
18012 #endif /* SQLITE_OMIT_UTF16 */
18013 SQLITE_API int sqlite3_column_type(sqlite3_stmt *pStmt, int i){
18014 int iType = sqlite3_value_type( columnMem(pStmt,i) );
18015 columnMallocFailure(pStmt);
18016 return iType;
18017 }
18018
18019 /*
18020 ** Convert the N-th element of pStmt->pColName[] into a string using
18021 ** xFunc() then return that string. If N is out of range, return 0.
18022 **
18023 ** There are up to 5 names for each column. useType determines which
18024 ** name is returned. Here are the names:
18025 **
18026 ** 0 The column name as it should be displayed for output
18027 ** 1 The datatype name for the column
18028 ** 2 The name of the database that the column derives from
18029 ** 3 The name of the table that the column derives from
18030 ** 4 The name of the table column that the result column derives from
18031 **
18032 ** If the result is not a simple column reference (if it is an expression
18033 ** or a constant) then useTypes 2, 3, and 4 return NULL.
18034 */
18035 static const void *columnName(
18036 sqlite3_stmt *pStmt,
18037 int N,
18038 const void *(*xFunc)(Mem*),
18039 int useType
18040 ){
18041 const void *ret;
18042 Vdbe *p;
18043 int n;
18044 sqlite3 *db;
18045 #ifdef SQLITE_ENABLE_API_ARMOR
18046 if( pStmt==0 ){
18047 (void)SQLITE_MISUSE_BKPT;
18048 return 0;
18049 }
18050 #endif
18051 ret = 0;
18052 p = (Vdbe *)pStmt;
18053 db = p->db;
18054 assert( db!=0 );
18055 n = sqlite3_column_count(pStmt);
18056 if( N<n && N>=0 ){
18057 N += useType*n;
18058 sqlite3_mutex_enter(db->mutex);
18059 assert( db->mallocFailed==0 );
18060 ret = xFunc(&p->aColName[N]);
18061 /* A malloc may have failed inside of the xFunc() call. If this
18062 ** is the case, clear the mallocFailed flag and return NULL.
18063 */
18064 if( db->mallocFailed ){
18065 sqlite3OomClear(db);
18066 ret = 0;
18067 }
18068 sqlite3_mutex_leave(db->mutex);
18069 }
18070 return ret;
18071 }
18072
18073 /*
18074 ** Return the name of the Nth column of the result set returned by SQL
18075 ** statement pStmt.
18076 */
18077 SQLITE_API const char *sqlite3_column_name(sqlite3_stmt *pStmt, int N){
18078 return columnName(
18079 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_NAME);
18080 }
18081 #ifndef SQLITE_OMIT_UTF16
18082 SQLITE_API const void *sqlite3_column_name16(sqlite3_stmt *pStmt, int N){
18083 return columnName(
18084 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_NAME);
18085 }
18086 #endif
18087
18088 /*
18089 ** Constraint: If you have ENABLE_COLUMN_METADATA then you must
18090 ** not define OMIT_DECLTYPE.
18091 */
18092 #if defined(SQLITE_OMIT_DECLTYPE) && defined(SQLITE_ENABLE_COLUMN_METADATA)
18093 # error "Must not define both SQLITE_OMIT_DECLTYPE \
18094 and SQLITE_ENABLE_COLUMN_METADATA"
18095 #endif
18096
18097 #ifndef SQLITE_OMIT_DECLTYPE
18098 /*
18099 ** Return the column declaration type (if applicable) of the 'i'th column
18100 ** of the result set of SQL statement pStmt.
18101 */
18102 SQLITE_API const char *sqlite3_column_decltype(sqlite3_stmt *pStmt, int N){
18103 return columnName(
18104 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DECLTYPE);
18105 }
18106 #ifndef SQLITE_OMIT_UTF16
18107 SQLITE_API const void *sqlite3_column_decltype16(sqlite3_stmt *pStmt, int N){
18108 return columnName(
18109 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DECLTYPE);
18110 }
18111 #endif /* SQLITE_OMIT_UTF16 */
18112 #endif /* SQLITE_OMIT_DECLTYPE */
18113
18114 #ifdef SQLITE_ENABLE_COLUMN_METADATA
18115 /*
18116 ** Return the name of the database from which a result column derives.
18117 ** NULL is returned if the result column is an expression or constant or
18118 ** anything else which is not an unambiguous reference to a database column.
18119 */
18120 SQLITE_API const char *sqlite3_column_database_name(sqlite3_stmt *pStmt, int N){
18121 return columnName(
18122 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DATABASE);
18123 }
18124 #ifndef SQLITE_OMIT_UTF16
18125 SQLITE_API const void *sqlite3_column_database_name16(sqlite3_stmt *pStmt, int N ){
18126 return columnName(
18127 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DATABASE);
18128 }
18129 #endif /* SQLITE_OMIT_UTF16 */
18130
18131 /*
18132 ** Return the name of the table from which a result column derives.
18133 ** NULL is returned if the result column is an expression or constant or
18134 ** anything else which is not an unambiguous reference to a database column.
18135 */
18136 SQLITE_API const char *sqlite3_column_table_name(sqlite3_stmt *pStmt, int N){
18137 return columnName(
18138 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_TABLE);
18139 }
18140 #ifndef SQLITE_OMIT_UTF16
18141 SQLITE_API const void *sqlite3_column_table_name16(sqlite3_stmt *pStmt, int N){
18142 return columnName(
18143 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_TABLE);
18144 }
18145 #endif /* SQLITE_OMIT_UTF16 */
18146
18147 /*
18148 ** Return the name of the table column from which a result column derives.
18149 ** NULL is returned if the result column is an expression or constant or
18150 ** anything else which is not an unambiguous reference to a database column.
18151 */
18152 SQLITE_API const char *sqlite3_column_origin_name(sqlite3_stmt *pStmt, int N){
18153 return columnName(
18154 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_COLUMN);
18155 }
18156 #ifndef SQLITE_OMIT_UTF16
18157 SQLITE_API const void *sqlite3_column_origin_name16(sqlite3_stmt *pStmt, int N){
18158 return columnName(
18159 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_COLUMN);
18160 }
18161 #endif /* SQLITE_OMIT_UTF16 */
18162 #endif /* SQLITE_ENABLE_COLUMN_METADATA */
18163
18164
18165 /******************************* sqlite3_bind_ ***************************
18166 **
18167 ** Routines used to attach values to wildcards in a compiled SQL statement.
18168 */
18169 /*
18170 ** Unbind the value bound to variable i in virtual machine p. This is the
18171 ** the same as binding a NULL value to the column. If the "i" parameter is
18172 ** out of range, then SQLITE_RANGE is returned. Othewise SQLITE_OK.
18173 **
18174 ** A successful evaluation of this routine acquires the mutex on p.
18175 ** the mutex is released if any kind of error occurs.
18176 **
18177 ** The error code stored in database p->db is overwritten with the return
18178 ** value in any case.
18179 */
18180 static int vdbeUnbind(Vdbe *p, int i){
18181 Mem *pVar;
18182 if( vdbeSafetyNotNull(p) ){
18183 return SQLITE_MISUSE_BKPT;
18184 }
18185 sqlite3_mutex_enter(p->db->mutex);
18186 if( p->magic!=VDBE_MAGIC_RUN || p->pc>=0 ){
18187 sqlite3Error(p->db, SQLITE_MISUSE);
18188 sqlite3_mutex_leave(p->db->mutex);
18189 sqlite3_log(SQLITE_MISUSE,
18190 "bind on a busy prepared statement: [%s]", p->zSql);
18191 return SQLITE_MISUSE_BKPT;
18192 }
18193 if( i<1 || i>p->nVar ){
18194 sqlite3Error(p->db, SQLITE_RANGE);
18195 sqlite3_mutex_leave(p->db->mutex);
18196 return SQLITE_RANGE;
18197 }
18198 i--;
18199 pVar = &p->aVar[i];
18200 sqlite3VdbeMemRelease(pVar);
18201 pVar->flags = MEM_Null;
18202 sqlite3Error(p->db, SQLITE_OK);
18203
18204 /* If the bit corresponding to this variable in Vdbe.expmask is set, then
18205 ** binding a new value to this variable invalidates the current query plan.
18206 **
18207 ** IMPLEMENTATION-OF: R-48440-37595 If the specific value bound to host
18208 ** parameter in the WHERE clause might influence the choice of query plan
18209 ** for a statement, then the statement will be automatically recompiled,
18210 ** as if there had been a schema change, on the first sqlite3_step() call
18211 ** following any change to the bindings of that parameter.
18212 */
18213 if( p->isPrepareV2 &&
18214 ((i<32 && p->expmask & ((u32)1 << i)) || p->expmask==0xffffffff)
18215 ){
18216 p->expired = 1;
18217 }
18218 return SQLITE_OK;
18219 }
18220
18221 /*
18222 ** Bind a text or BLOB value.
18223 */
18224 static int bindText(
18225 sqlite3_stmt *pStmt, /* The statement to bind against */
18226 int i, /* Index of the parameter to bind */
18227 const void *zData, /* Pointer to the data to be bound */
18228 int nData, /* Number of bytes of data to be bound */
18229 void (*xDel)(void*), /* Destructor for the data */
18230 u8 encoding /* Encoding for the data */
18231 ){
18232 Vdbe *p = (Vdbe *)pStmt;
18233 Mem *pVar;
18234 int rc;
18235
18236 rc = vdbeUnbind(p, i);
18237 if( rc==SQLITE_OK ){
18238 if( zData!=0 ){
18239 pVar = &p->aVar[i-1];
18240 rc = sqlite3VdbeMemSetStr(pVar, zData, nData, encoding, xDel);
18241 if( rc==SQLITE_OK && encoding!=0 ){
18242 rc = sqlite3VdbeChangeEncoding(pVar, ENC(p->db));
18243 }
18244 sqlite3Error(p->db, rc);
18245 rc = sqlite3ApiExit(p->db, rc);
18246 }
18247 sqlite3_mutex_leave(p->db->mutex);
18248 }else if( xDel!=SQLITE_STATIC && xDel!=SQLITE_TRANSIENT ){
18249 xDel((void*)zData);
18250 }
18251 return rc;
18252 }
18253
18254
18255 /*
18256 ** Bind a blob value to an SQL statement variable.
18257 */
18258 SQLITE_API int sqlite3_bind_blob(
18259 sqlite3_stmt *pStmt,
18260 int i,
18261 const void *zData,
18262 int nData,
18263 void (*xDel)(void*)
18264 ){
18265 #ifdef SQLITE_ENABLE_API_ARMOR
18266 if( nData<0 ) return SQLITE_MISUSE_BKPT;
18267 #endif
18268 return bindText(pStmt, i, zData, nData, xDel, 0);
18269 }
18270 SQLITE_API int sqlite3_bind_blob64(
18271 sqlite3_stmt *pStmt,
18272 int i,
18273 const void *zData,
18274 sqlite3_uint64 nData,
18275 void (*xDel)(void*)
18276 ){
18277 assert( xDel!=SQLITE_DYNAMIC );
18278 if( nData>0x7fffffff ){
18279 return invokeValueDestructor(zData, xDel, 0);
18280 }else{
18281 return bindText(pStmt, i, zData, (int)nData, xDel, 0);
18282 }
18283 }
18284 SQLITE_API int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){
18285 int rc;
18286 Vdbe *p = (Vdbe *)pStmt;
18287 rc = vdbeUnbind(p, i);
18288 if( rc==SQLITE_OK ){
18289 sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue);
18290 sqlite3_mutex_leave(p->db->mutex);
18291 }
18292 return rc;
18293 }
18294 SQLITE_API int sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){
18295 return sqlite3_bind_int64(p, i, (i64)iValue);
18296 }
18297 SQLITE_API int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, sqlite_int64 iValu e){
18298 int rc;
18299 Vdbe *p = (Vdbe *)pStmt;
18300 rc = vdbeUnbind(p, i);
18301 if( rc==SQLITE_OK ){
18302 sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue);
18303 sqlite3_mutex_leave(p->db->mutex);
18304 }
18305 return rc;
18306 }
18307 SQLITE_API int sqlite3_bind_null(sqlite3_stmt *pStmt, int i){
18308 int rc;
18309 Vdbe *p = (Vdbe*)pStmt;
18310 rc = vdbeUnbind(p, i);
18311 if( rc==SQLITE_OK ){
18312 sqlite3_mutex_leave(p->db->mutex);
18313 }
18314 return rc;
18315 }
18316 SQLITE_API int sqlite3_bind_text(
18317 sqlite3_stmt *pStmt,
18318 int i,
18319 const char *zData,
18320 int nData,
18321 void (*xDel)(void*)
18322 ){
18323 return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF8);
18324 }
18325 SQLITE_API int sqlite3_bind_text64(
18326 sqlite3_stmt *pStmt,
18327 int i,
18328 const char *zData,
18329 sqlite3_uint64 nData,
18330 void (*xDel)(void*),
18331 unsigned char enc
18332 ){
18333 assert( xDel!=SQLITE_DYNAMIC );
18334 if( nData>0x7fffffff ){
18335 return invokeValueDestructor(zData, xDel, 0);
18336 }else{
18337 if( enc==SQLITE_UTF16 ) enc = SQLITE_UTF16NATIVE;
18338 return bindText(pStmt, i, zData, (int)nData, xDel, enc);
18339 }
18340 }
18341 #ifndef SQLITE_OMIT_UTF16
18342 SQLITE_API int sqlite3_bind_text16(
18343 sqlite3_stmt *pStmt,
18344 int i,
18345 const void *zData,
18346 int nData,
18347 void (*xDel)(void*)
18348 ){
18349 return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF16NATIVE);
18350 }
18351 #endif /* SQLITE_OMIT_UTF16 */
18352 SQLITE_API int sqlite3_bind_value(sqlite3_stmt *pStmt, int i, const sqlite3_valu e *pValue){
18353 int rc;
18354 switch( sqlite3_value_type((sqlite3_value*)pValue) ){
18355 case SQLITE_INTEGER: {
18356 rc = sqlite3_bind_int64(pStmt, i, pValue->u.i);
18357 break;
18358 }
18359 case SQLITE_FLOAT: {
18360 rc = sqlite3_bind_double(pStmt, i, pValue->u.r);
18361 break;
18362 }
18363 case SQLITE_BLOB: {
18364 if( pValue->flags & MEM_Zero ){
18365 rc = sqlite3_bind_zeroblob(pStmt, i, pValue->u.nZero);
18366 }else{
18367 rc = sqlite3_bind_blob(pStmt, i, pValue->z, pValue->n,SQLITE_TRANSIENT);
18368 }
18369 break;
18370 }
18371 case SQLITE_TEXT: {
18372 rc = bindText(pStmt,i, pValue->z, pValue->n, SQLITE_TRANSIENT,
18373 pValue->enc);
18374 break;
18375 }
18376 default: {
18377 rc = sqlite3_bind_null(pStmt, i);
18378 break;
18379 }
18380 }
18381 return rc;
18382 }
18383 SQLITE_API int sqlite3_bind_zeroblob(sqlite3_stmt *pStmt, int i, int n){
18384 int rc;
18385 Vdbe *p = (Vdbe *)pStmt;
18386 rc = vdbeUnbind(p, i);
18387 if( rc==SQLITE_OK ){
18388 sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n);
18389 sqlite3_mutex_leave(p->db->mutex);
18390 }
18391 return rc;
18392 }
18393 SQLITE_API int sqlite3_bind_zeroblob64(sqlite3_stmt *pStmt, int i, sqlite3_uint6 4 n){
18394 int rc;
18395 Vdbe *p = (Vdbe *)pStmt;
18396 sqlite3_mutex_enter(p->db->mutex);
18397 if( n>(u64)p->db->aLimit[SQLITE_LIMIT_LENGTH] ){
18398 rc = SQLITE_TOOBIG;
18399 }else{
18400 assert( (n & 0x7FFFFFFF)==n );
18401 rc = sqlite3_bind_zeroblob(pStmt, i, n);
18402 }
18403 rc = sqlite3ApiExit(p->db, rc);
18404 sqlite3_mutex_leave(p->db->mutex);
18405 return rc;
18406 }
18407
18408 /*
18409 ** Return the number of wildcards that can be potentially bound to.
18410 ** This routine is added to support DBD::SQLite.
18411 */
18412 SQLITE_API int sqlite3_bind_parameter_count(sqlite3_stmt *pStmt){
18413 Vdbe *p = (Vdbe*)pStmt;
18414 return p ? p->nVar : 0;
18415 }
18416
18417 /*
18418 ** Return the name of a wildcard parameter. Return NULL if the index
18419 ** is out of range or if the wildcard is unnamed.
18420 **
18421 ** The result is always UTF-8.
18422 */
18423 SQLITE_API const char *sqlite3_bind_parameter_name(sqlite3_stmt *pStmt, int i){
18424 Vdbe *p = (Vdbe*)pStmt;
18425 if( p==0 ) return 0;
18426 return sqlite3VListNumToName(p->pVList, i);
18427 }
18428
18429 /*
18430 ** Given a wildcard parameter name, return the index of the variable
18431 ** with that name. If there is no variable with the given name,
18432 ** return 0.
18433 */
18434 SQLITE_PRIVATE int sqlite3VdbeParameterIndex(Vdbe *p, const char *zName, int nNa me){
18435 if( p==0 || zName==0 ) return 0;
18436 return sqlite3VListNameToNum(p->pVList, zName, nName);
18437 }
18438 SQLITE_API int sqlite3_bind_parameter_index(sqlite3_stmt *pStmt, const char *zNa me){
18439 return sqlite3VdbeParameterIndex((Vdbe*)pStmt, zName, sqlite3Strlen30(zName));
18440 }
18441
18442 /*
18443 ** Transfer all bindings from the first statement over to the second.
18444 */
18445 SQLITE_PRIVATE int sqlite3TransferBindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){
18446 Vdbe *pFrom = (Vdbe*)pFromStmt;
18447 Vdbe *pTo = (Vdbe*)pToStmt;
18448 int i;
18449 assert( pTo->db==pFrom->db );
18450 assert( pTo->nVar==pFrom->nVar );
18451 sqlite3_mutex_enter(pTo->db->mutex);
18452 for(i=0; i<pFrom->nVar; i++){
18453 sqlite3VdbeMemMove(&pTo->aVar[i], &pFrom->aVar[i]);
18454 }
18455 sqlite3_mutex_leave(pTo->db->mutex);
18456 return SQLITE_OK;
18457 }
18458
18459 #ifndef SQLITE_OMIT_DEPRECATED
18460 /*
18461 ** Deprecated external interface. Internal/core SQLite code
18462 ** should call sqlite3TransferBindings.
18463 **
18464 ** It is misuse to call this routine with statements from different
18465 ** database connections. But as this is a deprecated interface, we
18466 ** will not bother to check for that condition.
18467 **
18468 ** If the two statements contain a different number of bindings, then
18469 ** an SQLITE_ERROR is returned. Nothing else can go wrong, so otherwise
18470 ** SQLITE_OK is returned.
18471 */
18472 SQLITE_API int sqlite3_transfer_bindings(sqlite3_stmt *pFromStmt, sqlite3_stmt * pToStmt){
18473 Vdbe *pFrom = (Vdbe*)pFromStmt;
18474 Vdbe *pTo = (Vdbe*)pToStmt;
18475 if( pFrom->nVar!=pTo->nVar ){
18476 return SQLITE_ERROR;
18477 }
18478 if( pTo->isPrepareV2 && pTo->expmask ){
18479 pTo->expired = 1;
18480 }
18481 if( pFrom->isPrepareV2 && pFrom->expmask ){
18482 pFrom->expired = 1;
18483 }
18484 return sqlite3TransferBindings(pFromStmt, pToStmt);
18485 }
18486 #endif
18487
18488 /*
18489 ** Return the sqlite3* database handle to which the prepared statement given
18490 ** in the argument belongs. This is the same database handle that was
18491 ** the first argument to the sqlite3_prepare() that was used to create
18492 ** the statement in the first place.
18493 */
18494 SQLITE_API sqlite3 *sqlite3_db_handle(sqlite3_stmt *pStmt){
18495 return pStmt ? ((Vdbe*)pStmt)->db : 0;
18496 }
18497
18498 /*
18499 ** Return true if the prepared statement is guaranteed to not modify the
18500 ** database.
18501 */
18502 SQLITE_API int sqlite3_stmt_readonly(sqlite3_stmt *pStmt){
18503 return pStmt ? ((Vdbe*)pStmt)->readOnly : 1;
18504 }
18505
18506 /*
18507 ** Return true if the prepared statement is in need of being reset.
18508 */
18509 SQLITE_API int sqlite3_stmt_busy(sqlite3_stmt *pStmt){
18510 Vdbe *v = (Vdbe*)pStmt;
18511 return v!=0 && v->magic==VDBE_MAGIC_RUN && v->pc>=0;
18512 }
18513
18514 /*
18515 ** Return a pointer to the next prepared statement after pStmt associated
18516 ** with database connection pDb. If pStmt is NULL, return the first
18517 ** prepared statement for the database connection. Return NULL if there
18518 ** are no more.
18519 */
18520 SQLITE_API sqlite3_stmt *sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt){
18521 sqlite3_stmt *pNext;
18522 #ifdef SQLITE_ENABLE_API_ARMOR
18523 if( !sqlite3SafetyCheckOk(pDb) ){
18524 (void)SQLITE_MISUSE_BKPT;
18525 return 0;
18526 }
18527 #endif
18528 sqlite3_mutex_enter(pDb->mutex);
18529 if( pStmt==0 ){
18530 pNext = (sqlite3_stmt*)pDb->pVdbe;
18531 }else{
18532 pNext = (sqlite3_stmt*)((Vdbe*)pStmt)->pNext;
18533 }
18534 sqlite3_mutex_leave(pDb->mutex);
18535 return pNext;
18536 }
18537
18538 /*
18539 ** Return the value of a status counter for a prepared statement
18540 */
18541 SQLITE_API int sqlite3_stmt_status(sqlite3_stmt *pStmt, int op, int resetFlag){
18542 Vdbe *pVdbe = (Vdbe*)pStmt;
18543 u32 v;
18544 #ifdef SQLITE_ENABLE_API_ARMOR
18545 if( !pStmt ){
18546 (void)SQLITE_MISUSE_BKPT;
18547 return 0;
18548 }
18549 #endif
18550 v = pVdbe->aCounter[op];
18551 if( resetFlag ) pVdbe->aCounter[op] = 0;
18552 return (int)v;
18553 }
18554
18555 /*
18556 ** Return the SQL associated with a prepared statement
18557 */
18558 SQLITE_API const char *sqlite3_sql(sqlite3_stmt *pStmt){
18559 Vdbe *p = (Vdbe *)pStmt;
18560 return p ? p->zSql : 0;
18561 }
18562
18563 /*
18564 ** Return the SQL associated with a prepared statement with
18565 ** bound parameters expanded. Space to hold the returned string is
18566 ** obtained from sqlite3_malloc(). The caller is responsible for
18567 ** freeing the returned string by passing it to sqlite3_free().
18568 **
18569 ** The SQLITE_TRACE_SIZE_LIMIT puts an upper bound on the size of
18570 ** expanded bound parameters.
18571 */
18572 SQLITE_API char *sqlite3_expanded_sql(sqlite3_stmt *pStmt){
18573 #ifdef SQLITE_OMIT_TRACE
18574 return 0;
18575 #else
18576 char *z = 0;
18577 const char *zSql = sqlite3_sql(pStmt);
18578 if( zSql ){
18579 Vdbe *p = (Vdbe *)pStmt;
18580 sqlite3_mutex_enter(p->db->mutex);
18581 z = sqlite3VdbeExpandSql(p, zSql);
18582 sqlite3_mutex_leave(p->db->mutex);
18583 }
18584 return z;
18585 #endif
18586 }
18587
18588 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
18589 /*
18590 ** Allocate and populate an UnpackedRecord structure based on the serialized
18591 ** record in nKey/pKey. Return a pointer to the new UnpackedRecord structure
18592 ** if successful, or a NULL pointer if an OOM error is encountered.
18593 */
18594 static UnpackedRecord *vdbeUnpackRecord(
18595 KeyInfo *pKeyInfo,
18596 int nKey,
18597 const void *pKey
18598 ){
18599 UnpackedRecord *pRet; /* Return value */
18600
18601 pRet = sqlite3VdbeAllocUnpackedRecord(pKeyInfo);
18602 if( pRet ){
18603 memset(pRet->aMem, 0, sizeof(Mem)*(pKeyInfo->nField+1));
18604 sqlite3VdbeRecordUnpack(pKeyInfo, nKey, pKey, pRet);
18605 }
18606 return pRet;
18607 }
18608
18609 /*
18610 ** This function is called from within a pre-update callback to retrieve
18611 ** a field of the row currently being updated or deleted.
18612 */
18613 SQLITE_API int sqlite3_preupdate_old(sqlite3 *db, int iIdx, sqlite3_value **ppVa lue){
18614 PreUpdate *p = db->pPreUpdate;
18615 Mem *pMem;
18616 int rc = SQLITE_OK;
18617
18618 /* Test that this call is being made from within an SQLITE_DELETE or
18619 ** SQLITE_UPDATE pre-update callback, and that iIdx is within range. */
18620 if( !p || p->op==SQLITE_INSERT ){
18621 rc = SQLITE_MISUSE_BKPT;
18622 goto preupdate_old_out;
18623 }
18624 if( p->pPk ){
18625 iIdx = sqlite3ColumnOfIndex(p->pPk, iIdx);
18626 }
18627 if( iIdx>=p->pCsr->nField || iIdx<0 ){
18628 rc = SQLITE_RANGE;
18629 goto preupdate_old_out;
18630 }
18631
18632 /* If the old.* record has not yet been loaded into memory, do so now. */
18633 if( p->pUnpacked==0 ){
18634 u32 nRec;
18635 u8 *aRec;
18636
18637 nRec = sqlite3BtreePayloadSize(p->pCsr->uc.pCursor);
18638 aRec = sqlite3DbMallocRaw(db, nRec);
18639 if( !aRec ) goto preupdate_old_out;
18640 rc = sqlite3BtreePayload(p->pCsr->uc.pCursor, 0, nRec, aRec);
18641 if( rc==SQLITE_OK ){
18642 p->pUnpacked = vdbeUnpackRecord(&p->keyinfo, nRec, aRec);
18643 if( !p->pUnpacked ) rc = SQLITE_NOMEM;
18644 }
18645 if( rc!=SQLITE_OK ){
18646 sqlite3DbFree(db, aRec);
18647 goto preupdate_old_out;
18648 }
18649 p->aRecord = aRec;
18650 }
18651
18652 pMem = *ppValue = &p->pUnpacked->aMem[iIdx];
18653 if( iIdx==p->pTab->iPKey ){
18654 sqlite3VdbeMemSetInt64(pMem, p->iKey1);
18655 }else if( iIdx>=p->pUnpacked->nField ){
18656 *ppValue = (sqlite3_value *)columnNullValue();
18657 }else if( p->pTab->aCol[iIdx].affinity==SQLITE_AFF_REAL ){
18658 if( pMem->flags & MEM_Int ){
18659 sqlite3VdbeMemRealify(pMem);
18660 }
18661 }
18662
18663 preupdate_old_out:
18664 sqlite3Error(db, rc);
18665 return sqlite3ApiExit(db, rc);
18666 }
18667 #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
18668
18669 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
18670 /*
18671 ** This function is called from within a pre-update callback to retrieve
18672 ** the number of columns in the row being updated, deleted or inserted.
18673 */
18674 SQLITE_API int sqlite3_preupdate_count(sqlite3 *db){
18675 PreUpdate *p = db->pPreUpdate;
18676 return (p ? p->keyinfo.nField : 0);
18677 }
18678 #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
18679
18680 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
18681 /*
18682 ** This function is designed to be called from within a pre-update callback
18683 ** only. It returns zero if the change that caused the callback was made
18684 ** immediately by a user SQL statement. Or, if the change was made by a
18685 ** trigger program, it returns the number of trigger programs currently
18686 ** on the stack (1 for a top-level trigger, 2 for a trigger fired by a
18687 ** top-level trigger etc.).
18688 **
18689 ** For the purposes of the previous paragraph, a foreign key CASCADE, SET NULL
18690 ** or SET DEFAULT action is considered a trigger.
18691 */
18692 SQLITE_API int sqlite3_preupdate_depth(sqlite3 *db){
18693 PreUpdate *p = db->pPreUpdate;
18694 return (p ? p->v->nFrame : 0);
18695 }
18696 #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
18697
18698 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
18699 /*
18700 ** This function is called from within a pre-update callback to retrieve
18701 ** a field of the row currently being updated or inserted.
18702 */
18703 SQLITE_API int sqlite3_preupdate_new(sqlite3 *db, int iIdx, sqlite3_value **ppVa lue){
18704 PreUpdate *p = db->pPreUpdate;
18705 int rc = SQLITE_OK;
18706 Mem *pMem;
18707
18708 if( !p || p->op==SQLITE_DELETE ){
18709 rc = SQLITE_MISUSE_BKPT;
18710 goto preupdate_new_out;
18711 }
18712 if( p->pPk && p->op!=SQLITE_UPDATE ){
18713 iIdx = sqlite3ColumnOfIndex(p->pPk, iIdx);
18714 }
18715 if( iIdx>=p->pCsr->nField || iIdx<0 ){
18716 rc = SQLITE_RANGE;
18717 goto preupdate_new_out;
18718 }
18719
18720 if( p->op==SQLITE_INSERT ){
18721 /* For an INSERT, memory cell p->iNewReg contains the serialized record
18722 ** that is being inserted. Deserialize it. */
18723 UnpackedRecord *pUnpack = p->pNewUnpacked;
18724 if( !pUnpack ){
18725 Mem *pData = &p->v->aMem[p->iNewReg];
18726 rc = ExpandBlob(pData);
18727 if( rc!=SQLITE_OK ) goto preupdate_new_out;
18728 pUnpack = vdbeUnpackRecord(&p->keyinfo, pData->n, pData->z);
18729 if( !pUnpack ){
18730 rc = SQLITE_NOMEM;
18731 goto preupdate_new_out;
18732 }
18733 p->pNewUnpacked = pUnpack;
18734 }
18735 pMem = &pUnpack->aMem[iIdx];
18736 if( iIdx==p->pTab->iPKey ){
18737 sqlite3VdbeMemSetInt64(pMem, p->iKey2);
18738 }else if( iIdx>=pUnpack->nField ){
18739 pMem = (sqlite3_value *)columnNullValue();
18740 }
18741 }else{
18742 /* For an UPDATE, memory cell (p->iNewReg+1+iIdx) contains the required
18743 ** value. Make a copy of the cell contents and return a pointer to it.
18744 ** It is not safe to return a pointer to the memory cell itself as the
18745 ** caller may modify the value text encoding.
18746 */
18747 assert( p->op==SQLITE_UPDATE );
18748 if( !p->aNew ){
18749 p->aNew = (Mem *)sqlite3DbMallocZero(db, sizeof(Mem) * p->pCsr->nField);
18750 if( !p->aNew ){
18751 rc = SQLITE_NOMEM;
18752 goto preupdate_new_out;
18753 }
18754 }
18755 assert( iIdx>=0 && iIdx<p->pCsr->nField );
18756 pMem = &p->aNew[iIdx];
18757 if( pMem->flags==0 ){
18758 if( iIdx==p->pTab->iPKey ){
18759 sqlite3VdbeMemSetInt64(pMem, p->iKey2);
18760 }else{
18761 rc = sqlite3VdbeMemCopy(pMem, &p->v->aMem[p->iNewReg+1+iIdx]);
18762 if( rc!=SQLITE_OK ) goto preupdate_new_out;
18763 }
18764 }
18765 }
18766 *ppValue = pMem;
18767
18768 preupdate_new_out:
18769 sqlite3Error(db, rc);
18770 return sqlite3ApiExit(db, rc);
18771 }
18772 #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
18773
18774 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
18775 /*
18776 ** Return status data for a single loop within query pStmt.
18777 */
18778 SQLITE_API int sqlite3_stmt_scanstatus(
18779 sqlite3_stmt *pStmt, /* Prepared statement being queried */
18780 int idx, /* Index of loop to report on */
18781 int iScanStatusOp, /* Which metric to return */
18782 void *pOut /* OUT: Write the answer here */
18783 ){
18784 Vdbe *p = (Vdbe*)pStmt;
18785 ScanStatus *pScan;
18786 if( idx<0 || idx>=p->nScan ) return 1;
18787 pScan = &p->aScan[idx];
18788 switch( iScanStatusOp ){
18789 case SQLITE_SCANSTAT_NLOOP: {
18790 *(sqlite3_int64*)pOut = p->anExec[pScan->addrLoop];
18791 break;
18792 }
18793 case SQLITE_SCANSTAT_NVISIT: {
18794 *(sqlite3_int64*)pOut = p->anExec[pScan->addrVisit];
18795 break;
18796 }
18797 case SQLITE_SCANSTAT_EST: {
18798 double r = 1.0;
18799 LogEst x = pScan->nEst;
18800 while( x<100 ){
18801 x += 10;
18802 r *= 0.5;
18803 }
18804 *(double*)pOut = r*sqlite3LogEstToInt(x);
18805 break;
18806 }
18807 case SQLITE_SCANSTAT_NAME: {
18808 *(const char**)pOut = pScan->zName;
18809 break;
18810 }
18811 case SQLITE_SCANSTAT_EXPLAIN: {
18812 if( pScan->addrExplain ){
18813 *(const char**)pOut = p->aOp[ pScan->addrExplain ].p4.z;
18814 }else{
18815 *(const char**)pOut = 0;
18816 }
18817 break;
18818 }
18819 case SQLITE_SCANSTAT_SELECTID: {
18820 if( pScan->addrExplain ){
18821 *(int*)pOut = p->aOp[ pScan->addrExplain ].p1;
18822 }else{
18823 *(int*)pOut = -1;
18824 }
18825 break;
18826 }
18827 default: {
18828 return 1;
18829 }
18830 }
18831 return 0;
18832 }
18833
18834 /*
18835 ** Zero all counters associated with the sqlite3_stmt_scanstatus() data.
18836 */
18837 SQLITE_API void sqlite3_stmt_scanstatus_reset(sqlite3_stmt *pStmt){
18838 Vdbe *p = (Vdbe*)pStmt;
18839 memset(p->anExec, 0, p->nOp * sizeof(i64));
18840 }
18841 #endif /* SQLITE_ENABLE_STMT_SCANSTATUS */
18842
18843 /************** End of vdbeapi.c *********************************************/
18844 /************** Begin file vdbetrace.c ***************************************/
18845 /*
18846 ** 2009 November 25
18847 **
18848 ** The author disclaims copyright to this source code. In place of
18849 ** a legal notice, here is a blessing:
18850 **
18851 ** May you do good and not evil.
18852 ** May you find forgiveness for yourself and forgive others.
18853 ** May you share freely, never taking more than you give.
18854 **
18855 *************************************************************************
18856 **
18857 ** This file contains code used to insert the values of host parameters
18858 ** (aka "wildcards") into the SQL text output by sqlite3_trace().
18859 **
18860 ** The Vdbe parse-tree explainer is also found here.
18861 */
18862 /* #include "sqliteInt.h" */
18863 /* #include "vdbeInt.h" */
18864
18865 #ifndef SQLITE_OMIT_TRACE
18866
18867 /*
18868 ** zSql is a zero-terminated string of UTF-8 SQL text. Return the number of
18869 ** bytes in this text up to but excluding the first character in
18870 ** a host parameter. If the text contains no host parameters, return
18871 ** the total number of bytes in the text.
18872 */
18873 static int findNextHostParameter(const char *zSql, int *pnToken){
18874 int tokenType;
18875 int nTotal = 0;
18876 int n;
18877
18878 *pnToken = 0;
18879 while( zSql[0] ){
18880 n = sqlite3GetToken((u8*)zSql, &tokenType);
18881 assert( n>0 && tokenType!=TK_ILLEGAL );
18882 if( tokenType==TK_VARIABLE ){
18883 *pnToken = n;
18884 break;
18885 }
18886 nTotal += n;
18887 zSql += n;
18888 }
18889 return nTotal;
18890 }
18891
18892 /*
18893 ** This function returns a pointer to a nul-terminated string in memory
18894 ** obtained from sqlite3DbMalloc(). If sqlite3.nVdbeExec is 1, then the
18895 ** string contains a copy of zRawSql but with host parameters expanded to
18896 ** their current bindings. Or, if sqlite3.nVdbeExec is greater than 1,
18897 ** then the returned string holds a copy of zRawSql with "-- " prepended
18898 ** to each line of text.
18899 **
18900 ** If the SQLITE_TRACE_SIZE_LIMIT macro is defined to an integer, then
18901 ** then long strings and blobs are truncated to that many bytes. This
18902 ** can be used to prevent unreasonably large trace strings when dealing
18903 ** with large (multi-megabyte) strings and blobs.
18904 **
18905 ** The calling function is responsible for making sure the memory returned
18906 ** is eventually freed.
18907 **
18908 ** ALGORITHM: Scan the input string looking for host parameters in any of
18909 ** these forms: ?, ?N, $A, @A, :A. Take care to avoid text within
18910 ** string literals, quoted identifier names, and comments. For text forms,
18911 ** the host parameter index is found by scanning the prepared
18912 ** statement for the corresponding OP_Variable opcode. Once the host
18913 ** parameter index is known, locate the value in p->aVar[]. Then render
18914 ** the value as a literal in place of the host parameter name.
18915 */
18916 SQLITE_PRIVATE char *sqlite3VdbeExpandSql(
18917 Vdbe *p, /* The prepared statement being evaluated */
18918 const char *zRawSql /* Raw text of the SQL statement */
18919 ){
18920 sqlite3 *db; /* The database connection */
18921 int idx = 0; /* Index of a host parameter */
18922 int nextIndex = 1; /* Index of next ? host parameter */
18923 int n; /* Length of a token prefix */
18924 int nToken; /* Length of the parameter token */
18925 int i; /* Loop counter */
18926 Mem *pVar; /* Value of a host parameter */
18927 StrAccum out; /* Accumulate the output here */
18928 #ifndef SQLITE_OMIT_UTF16
18929 Mem utf8; /* Used to convert UTF16 parameters into UTF8 for dis play */
18930 #endif
18931 char zBase[100]; /* Initial working space */
18932
18933 db = p->db;
18934 sqlite3StrAccumInit(&out, 0, zBase, sizeof(zBase),
18935 db->aLimit[SQLITE_LIMIT_LENGTH]);
18936 if( db->nVdbeExec>1 ){
18937 while( *zRawSql ){
18938 const char *zStart = zRawSql;
18939 while( *(zRawSql++)!='\n' && *zRawSql );
18940 sqlite3StrAccumAppend(&out, "-- ", 3);
18941 assert( (zRawSql - zStart) > 0 );
18942 sqlite3StrAccumAppend(&out, zStart, (int)(zRawSql-zStart));
18943 }
18944 }else if( p->nVar==0 ){
18945 sqlite3StrAccumAppend(&out, zRawSql, sqlite3Strlen30(zRawSql));
18946 }else{
18947 while( zRawSql[0] ){
18948 n = findNextHostParameter(zRawSql, &nToken);
18949 assert( n>0 );
18950 sqlite3StrAccumAppend(&out, zRawSql, n);
18951 zRawSql += n;
18952 assert( zRawSql[0] || nToken==0 );
18953 if( nToken==0 ) break;
18954 if( zRawSql[0]=='?' ){
18955 if( nToken>1 ){
18956 assert( sqlite3Isdigit(zRawSql[1]) );
18957 sqlite3GetInt32(&zRawSql[1], &idx);
18958 }else{
18959 idx = nextIndex;
18960 }
18961 }else{
18962 assert( zRawSql[0]==':' || zRawSql[0]=='$' ||
18963 zRawSql[0]=='@' || zRawSql[0]=='#' );
18964 testcase( zRawSql[0]==':' );
18965 testcase( zRawSql[0]=='$' );
18966 testcase( zRawSql[0]=='@' );
18967 testcase( zRawSql[0]=='#' );
18968 idx = sqlite3VdbeParameterIndex(p, zRawSql, nToken);
18969 assert( idx>0 );
18970 }
18971 zRawSql += nToken;
18972 nextIndex = idx + 1;
18973 assert( idx>0 && idx<=p->nVar );
18974 pVar = &p->aVar[idx-1];
18975 if( pVar->flags & MEM_Null ){
18976 sqlite3StrAccumAppend(&out, "NULL", 4);
18977 }else if( pVar->flags & MEM_Int ){
18978 sqlite3XPrintf(&out, "%lld", pVar->u.i);
18979 }else if( pVar->flags & MEM_Real ){
18980 sqlite3XPrintf(&out, "%!.15g", pVar->u.r);
18981 }else if( pVar->flags & MEM_Str ){
18982 int nOut; /* Number of bytes of the string text to include in output */
18983 #ifndef SQLITE_OMIT_UTF16
18984 u8 enc = ENC(db);
18985 if( enc!=SQLITE_UTF8 ){
18986 memset(&utf8, 0, sizeof(utf8));
18987 utf8.db = db;
18988 sqlite3VdbeMemSetStr(&utf8, pVar->z, pVar->n, enc, SQLITE_STATIC);
18989 if( SQLITE_NOMEM==sqlite3VdbeChangeEncoding(&utf8, SQLITE_UTF8) ){
18990 out.accError = STRACCUM_NOMEM;
18991 out.nAlloc = 0;
18992 }
18993 pVar = &utf8;
18994 }
18995 #endif
18996 nOut = pVar->n;
18997 #ifdef SQLITE_TRACE_SIZE_LIMIT
18998 if( nOut>SQLITE_TRACE_SIZE_LIMIT ){
18999 nOut = SQLITE_TRACE_SIZE_LIMIT;
19000 while( nOut<pVar->n && (pVar->z[nOut]&0xc0)==0x80 ){ nOut++; }
19001 }
19002 #endif
19003 sqlite3XPrintf(&out, "'%.*q'", nOut, pVar->z);
19004 #ifdef SQLITE_TRACE_SIZE_LIMIT
19005 if( nOut<pVar->n ){
19006 sqlite3XPrintf(&out, "/*+%d bytes*/", pVar->n-nOut);
19007 }
19008 #endif
19009 #ifndef SQLITE_OMIT_UTF16
19010 if( enc!=SQLITE_UTF8 ) sqlite3VdbeMemRelease(&utf8);
19011 #endif
19012 }else if( pVar->flags & MEM_Zero ){
19013 sqlite3XPrintf(&out, "zeroblob(%d)", pVar->u.nZero);
19014 }else{
19015 int nOut; /* Number of bytes of the blob to include in output */
19016 assert( pVar->flags & MEM_Blob );
19017 sqlite3StrAccumAppend(&out, "x'", 2);
19018 nOut = pVar->n;
19019 #ifdef SQLITE_TRACE_SIZE_LIMIT
19020 if( nOut>SQLITE_TRACE_SIZE_LIMIT ) nOut = SQLITE_TRACE_SIZE_LIMIT;
19021 #endif
19022 for(i=0; i<nOut; i++){
19023 sqlite3XPrintf(&out, "%02x", pVar->z[i]&0xff);
19024 }
19025 sqlite3StrAccumAppend(&out, "'", 1);
19026 #ifdef SQLITE_TRACE_SIZE_LIMIT
19027 if( nOut<pVar->n ){
19028 sqlite3XPrintf(&out, "/*+%d bytes*/", pVar->n-nOut);
19029 }
19030 #endif
19031 }
19032 }
19033 }
19034 if( out.accError ) sqlite3StrAccumReset(&out);
19035 return sqlite3StrAccumFinish(&out);
19036 }
19037
19038 #endif /* #ifndef SQLITE_OMIT_TRACE */
19039
19040 /************** End of vdbetrace.c *******************************************/
19041
19042 /* Chain include. */
19043 #include "sqlite3.04.c"
OLDNEW
« no previous file with comments | « third_party/sqlite/amalgamation/sqlite3.02.c ('k') | third_party/sqlite/amalgamation/sqlite3.04.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698