Fossil SCM

Update to the latest SQLite. On the "Ticket" page, if "nobody" is not allowed to create new tickets but "anonymous" is, then offer a hyperlink to log in as anonymous. Change the default settings to be more secure.

drh 2009-07-06 23:53 trunk
Commit 623a9129c8fdaa6820a51cc4e16a93ea358c4456
+4 -2
--- src/db.c
+++ src/db.c
@@ -891,15 +891,17 @@
891891
"VALUES(%Q,lower(hex(randomblob(3))),'s','')", zUser
892892
);
893893
if( !setupUserOnly ){
894894
db_multi_exec(
895895
"INSERT INTO user(login,pw,cap,info)"
896
- " VALUES('anonymous','anonymous','ghknw','Anon');"
896
+ " VALUES('anonymous','anonymous','ghmncz','Anon');"
897897
"INSERT INTO user(login,pw,cap,info)"
898898
" VALUES('nobody','','jor','Nobody');"
899899
"INSERT INTO user(login,pw,cap,info)"
900
- " VALUES('developer','','deipt','Dev');"
900
+ " VALUES('developer','','dei','Dev');"
901
+ "INSERT INTO user(login,pw,cap,info)"
902
+ " VALUES('reader','','kptw','Reader');"
901903
);
902904
}
903905
}
904906
905907
/*
906908
--- src/db.c
+++ src/db.c
@@ -891,15 +891,17 @@
891 "VALUES(%Q,lower(hex(randomblob(3))),'s','')", zUser
892 );
893 if( !setupUserOnly ){
894 db_multi_exec(
895 "INSERT INTO user(login,pw,cap,info)"
896 " VALUES('anonymous','anonymous','ghknw','Anon');"
897 "INSERT INTO user(login,pw,cap,info)"
898 " VALUES('nobody','','jor','Nobody');"
899 "INSERT INTO user(login,pw,cap,info)"
900 " VALUES('developer','','deipt','Dev');"
 
 
901 );
902 }
903 }
904
905 /*
906
--- src/db.c
+++ src/db.c
@@ -891,15 +891,17 @@
891 "VALUES(%Q,lower(hex(randomblob(3))),'s','')", zUser
892 );
893 if( !setupUserOnly ){
894 db_multi_exec(
895 "INSERT INTO user(login,pw,cap,info)"
896 " VALUES('anonymous','anonymous','ghmncz','Anon');"
897 "INSERT INTO user(login,pw,cap,info)"
898 " VALUES('nobody','','jor','Nobody');"
899 "INSERT INTO user(login,pw,cap,info)"
900 " VALUES('developer','','dei','Dev');"
901 "INSERT INTO user(login,pw,cap,info)"
902 " VALUES('reader','','kptw','Reader');"
903 );
904 }
905 }
906
907 /*
908
--- src/report.c
+++ src/report.c
@@ -45,10 +45,16 @@
4545
if( g.okNewTkt ){
4646
@ <p>Enter a new ticket:</p>
4747
@ <ol><li value="1"><a href="tktnew">New ticket</a></li></ol>
4848
@
4949
cnt++;
50
+ }else if( db_exists(
51
+ "SELECT 1 FROM user"
52
+ " WHERE login='anonymous' AND cap GLOB '*n*'")
53
+ ){
54
+ @ <p><a href="login?anon=1&g=tktnew">Login as "anonymous"</a>
55
+ @ to enter a new ticket.</p>
5056
}
5157
if( !g.okRdTkt ){
5258
@ <p>You are not authorized to view existing tickets.</p>
5359
}else{
5460
db_prepare(&q, "SELECT rn, title, owner FROM reportfmt ORDER BY title");
5561
--- src/report.c
+++ src/report.c
@@ -45,10 +45,16 @@
45 if( g.okNewTkt ){
46 @ <p>Enter a new ticket:</p>
47 @ <ol><li value="1"><a href="tktnew">New ticket</a></li></ol>
48 @
49 cnt++;
 
 
 
 
 
 
50 }
51 if( !g.okRdTkt ){
52 @ <p>You are not authorized to view existing tickets.</p>
53 }else{
54 db_prepare(&q, "SELECT rn, title, owner FROM reportfmt ORDER BY title");
55
--- src/report.c
+++ src/report.c
@@ -45,10 +45,16 @@
45 if( g.okNewTkt ){
46 @ <p>Enter a new ticket:</p>
47 @ <ol><li value="1"><a href="tktnew">New ticket</a></li></ol>
48 @
49 cnt++;
50 }else if( db_exists(
51 "SELECT 1 FROM user"
52 " WHERE login='anonymous' AND cap GLOB '*n*'")
53 ){
54 @ <p><a href="login?anon=1&g=tktnew">Login as "anonymous"</a>
55 @ to enter a new ticket.</p>
56 }
57 if( !g.okRdTkt ){
58 @ <p>You are not authorized to view existing tickets.</p>
59 }else{
60 db_prepare(&q, "SELECT rn, title, owner FROM reportfmt ORDER BY title");
61
+1 -1
--- src/setup.c
+++ src/setup.c
@@ -175,11 +175,11 @@
175175
@ <tr><td valign="top"><b>s</b></td>
176176
@ <td><i>Setup/Super-user:</i> Setup and configure this website</td></tr>
177177
@ <tr><td valign="top"><b>t</b></td>
178178
@ <td><i>Tkt-Report:</i> Create new bug summary reports</td></tr>
179179
@ <tr><td valign="top"><b>u</b></td>
180
- @ <td><i>Developer:</i> Inherit privileges of
180
+ @ <td><i>Reader:</i> Inherit privileges of
181181
@ user <tt>reader</tt></td></tr>
182182
@ <tr><td valign="top"><b>v</b></td>
183183
@ <td><i>Developer:</i> Inherit privileges of
184184
@ user <tt>developer</tt></td></tr>
185185
@ <tr><td valign="top"><b>w</b></td>
186186
--- src/setup.c
+++ src/setup.c
@@ -175,11 +175,11 @@
175 @ <tr><td valign="top"><b>s</b></td>
176 @ <td><i>Setup/Super-user:</i> Setup and configure this website</td></tr>
177 @ <tr><td valign="top"><b>t</b></td>
178 @ <td><i>Tkt-Report:</i> Create new bug summary reports</td></tr>
179 @ <tr><td valign="top"><b>u</b></td>
180 @ <td><i>Developer:</i> Inherit privileges of
181 @ user <tt>reader</tt></td></tr>
182 @ <tr><td valign="top"><b>v</b></td>
183 @ <td><i>Developer:</i> Inherit privileges of
184 @ user <tt>developer</tt></td></tr>
185 @ <tr><td valign="top"><b>w</b></td>
186
--- src/setup.c
+++ src/setup.c
@@ -175,11 +175,11 @@
175 @ <tr><td valign="top"><b>s</b></td>
176 @ <td><i>Setup/Super-user:</i> Setup and configure this website</td></tr>
177 @ <tr><td valign="top"><b>t</b></td>
178 @ <td><i>Tkt-Report:</i> Create new bug summary reports</td></tr>
179 @ <tr><td valign="top"><b>u</b></td>
180 @ <td><i>Reader:</i> Inherit privileges of
181 @ user <tt>reader</tt></td></tr>
182 @ <tr><td valign="top"><b>v</b></td>
183 @ <td><i>Developer:</i> Inherit privileges of
184 @ user <tt>developer</tt></td></tr>
185 @ <tr><td valign="top"><b>w</b></td>
186
+423 -386
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -1,8 +1,8 @@
11
/******************************************************************************
22
** This file is an amalgamation of many separate C source files from SQLite
3
-** version 3.6.15. By combining all the individual C code files into this
3
+** version 3.6.16. By combining all the individual C code files into this
44
** single large file, the entire code can be compiled as a one translation
55
** unit. This allows many compilers to do optimizations that would not be
66
** possible if the files were compiled separately. Performance improvements
77
** of 5% are more are commonly seen when SQLite is compiled as a single
88
** translation unit.
@@ -15,11 +15,11 @@
1515
** needed if you want a wrapper to interface SQLite with your choice of
1616
** programming language. The code for the "sqlite3" command-line shell
1717
** is also in a separate file. This file contains only code for the core
1818
** SQLite library.
1919
**
20
-** This amalgamation was generated on 2009-06-23 14:42:37 UTC.
20
+** This amalgamation was generated on 2009-06-27 13:43:17 UTC.
2121
*/
2222
#define SQLITE_CORE 1
2323
#define SQLITE_AMALGAMATION 1
2424
#ifndef SQLITE_PRIVATE
2525
# define SQLITE_PRIVATE static
@@ -39,11 +39,11 @@
3939
** May you share freely, never taking more than you give.
4040
**
4141
*************************************************************************
4242
** Internal interface definitions for SQLite.
4343
**
44
-** @(#) $Id: sqliteInt.h,v 1.886 2009/06/19 14:06:03 drh Exp $
44
+** @(#) $Id: sqliteInt.h,v 1.890 2009/06/26 15:14:55 drh Exp $
4545
*/
4646
#ifndef _SQLITEINT_H_
4747
#define _SQLITEINT_H_
4848
4949
/*
@@ -489,13 +489,12 @@
489489
*/
490490
#if defined(SQLITE_COVERAGE_TEST)
491491
# define ALWAYS(X) (1)
492492
# define NEVER(X) (0)
493493
#elif !defined(NDEBUG)
494
-SQLITE_PRIVATE int sqlite3Assert(void);
495
-# define ALWAYS(X) ((X)?1:sqlite3Assert())
496
-# define NEVER(X) ((X)?sqlite3Assert():0)
494
+# define ALWAYS(X) ((X)?1:(assert(0),0))
495
+# define NEVER(X) ((X)?(assert(0),1):0)
497496
#else
498497
# define ALWAYS(X) (X)
499498
# define NEVER(X) (X)
500499
#endif
501500
@@ -614,12 +613,12 @@
614613
**
615614
** See also: [sqlite3_libversion()] and [sqlite3_libversion_number()].
616615
**
617616
** Requirements: [H10011] [H10014]
618617
*/
619
-#define SQLITE_VERSION "3.6.15"
620
-#define SQLITE_VERSION_NUMBER 3006015
618
+#define SQLITE_VERSION "3.6.16"
619
+#define SQLITE_VERSION_NUMBER 3006016
621620
622621
/*
623622
** CAPI3REF: Run-Time Library Version Numbers {H10020} <S60100>
624623
** KEYWORDS: sqlite3_version
625624
**
@@ -6693,10 +6692,11 @@
66936692
/*
66946693
** Forward references to structures
66956694
*/
66966695
typedef struct AggInfo AggInfo;
66976696
typedef struct AuthContext AuthContext;
6697
+typedef struct AutoincInfo AutoincInfo;
66986698
typedef struct Bitvec Bitvec;
66996699
typedef struct RowSet RowSet;
67006700
typedef struct CollSeq CollSeq;
67016701
typedef struct Column Column;
67026702
typedef struct Db Db;
@@ -8218,11 +8218,10 @@
82188218
signed char nextAutovac; /* Autovac setting after VACUUM if >=0 */
82198219
int nextPagesize; /* Pagesize after VACUUM if >0 */
82208220
int nTable; /* Number of tables in the database */
82218221
CollSeq *pDfltColl; /* The default collating sequence (BINARY) */
82228222
i64 lastRowid; /* ROWID of most recent insert (see above) */
8223
- i64 priorNewRowid; /* Last randomly generated ROWID */
82248223
u32 magic; /* Magic number for detect library misuse */
82258224
int nChange; /* Value returned by sqlite3_changes() */
82268225
int nTotalChange; /* Value returned by sqlite3_total_changes() */
82278226
sqlite3_mutex *mutex; /* Connection mutex */
82288227
int aLimit[SQLITE_N_LIMIT]; /* Limits */
@@ -9354,10 +9353,26 @@
93549353
int iParm; /* A parameter used by the eDest disposal method */
93559354
int iMem; /* Base register where results are written */
93569355
int nMem; /* Number of registers allocated */
93579356
};
93589357
9358
+/*
9359
+** During code generation of statements that do inserts into AUTOINCREMENT
9360
+** tables, the following information is attached to the Table.u.autoInc.p
9361
+** pointer of each autoincrement table to record some side information that
9362
+** the code generator needs. We have to keep per-table autoincrement
9363
+** information in case inserts are down within triggers. Triggers do not
9364
+** normally coordinate their activities, but we do need to coordinate the
9365
+** loading and saving of autoincrement information.
9366
+*/
9367
+struct AutoincInfo {
9368
+ AutoincInfo *pNext; /* Next info block in a list of them all */
9369
+ Table *pTab; /* Table this info block refers to */
9370
+ int iDb; /* Index in sqlite3.aDb[] of database holding pTab */
9371
+ int regCtr; /* Memory register holding the rowid counter */
9372
+};
9373
+
93599374
/*
93609375
** Size of the column cache
93619376
*/
93629377
#ifndef SQLITE_N_COLCACHE
93639378
# define SQLITE_N_COLCACHE 10
@@ -9420,10 +9435,11 @@
94209435
int nTableLock; /* Number of locks in aTableLock */
94219436
TableLock *aTableLock; /* Required table locks for shared-cache mode */
94229437
#endif
94239438
int regRowid; /* Register holding rowid of CREATE TABLE entry */
94249439
int regRoot; /* Register holding root page number for new objects */
9440
+ AutoincInfo *pAinc; /* Information about AUTOINCREMENT counters */
94259441
94269442
/* Above is constant between recursions. Below is reset before and after
94279443
** each recursion */
94289444
94299445
int nVar; /* Number of '?' variables seen in the SQL so far */
@@ -9910,10 +9926,17 @@
99109926
# define sqlite3ViewGetColumnNames(A,B) 0
99119927
#endif
99129928
99139929
SQLITE_PRIVATE void sqlite3DropTable(Parse*, SrcList*, int, int);
99149930
SQLITE_PRIVATE void sqlite3DeleteTable(Table*);
9931
+#ifndef SQLITE_OMIT_AUTOINCREMENT
9932
+SQLITE_PRIVATE void sqlite3AutoincrementBegin(Parse *pParse);
9933
+SQLITE_PRIVATE void sqlite3AutoincrementEnd(Parse *pParse);
9934
+#else
9935
+# define sqlite3AutoincrementBegin(X)
9936
+# define sqlite3AutoincrementEnd(X)
9937
+#endif
99159938
SQLITE_PRIVATE void sqlite3Insert(Parse*, SrcList*, ExprList*, Select*, IdList*, int);
99169939
SQLITE_PRIVATE void *sqlite3ArrayAllocate(sqlite3*,void*,int,int,int*,int*,int*);
99179940
SQLITE_PRIVATE IdList *sqlite3IdListAppend(sqlite3*, IdList*, Token*);
99189941
SQLITE_PRIVATE int sqlite3IdListIndex(IdList*,const char*);
99199942
SQLITE_PRIVATE SrcList *sqlite3SrcListEnlarge(sqlite3*, SrcList*, int, int);
@@ -15212,11 +15235,11 @@
1521215235
**
1521315236
*************************************************************************
1521415237
**
1521515238
** Memory allocation functions used throughout sqlite.
1521615239
**
15217
-** $Id: malloc.c,v 1.62 2009/05/03 20:23:54 drh Exp $
15240
+** $Id: malloc.c,v 1.64 2009/06/27 00:48:33 drh Exp $
1521815241
*/
1521915242
1522015243
/*
1522115244
** This routine runs when the memory allocator sees that the
1522215245
** total memory allocation is about to exceed the soft heap
@@ -15464,19 +15487,16 @@
1546415487
** Allocate memory. This routine is like sqlite3_malloc() except that it
1546515488
** assumes the memory subsystem has already been initialized.
1546615489
*/
1546715490
SQLITE_PRIVATE void *sqlite3Malloc(int n){
1546815491
void *p;
15469
- if( n<=0 || NEVER(n>=0x7fffff00) ){
15470
- /* The NEVER(n>=0x7fffff00) term is added out of paranoia. We want to make
15471
- ** absolutely sure that there is nothing within SQLite that can cause a
15472
- ** memory allocation of a number of bytes which is near the maximum signed
15473
- ** integer value and thus cause an integer overflow inside of the xMalloc()
15474
- ** implementation. The n>=0x7fffff00 gives us 255 bytes of headroom. The
15475
- ** test should never be true because SQLITE_MAX_LENGTH should be much
15476
- ** less than 0x7fffff00 and it should catch large memory allocations
15477
- ** before they reach this point. */
15492
+ if( n<=0 || n>=0x7fffff00 ){
15493
+ /* A memory allocation of a number of bytes which is near the maximum
15494
+ ** signed integer value might cause an integer overflow inside of the
15495
+ ** xMalloc(). Hence we limit the maximum size to 0x7fffff00, giving
15496
+ ** 255 bytes of overhead. SQLite itself will never use anything near
15497
+ ** this amount. The only way to reach the limit is with sqlite3_malloc() */
1547815498
p = 0;
1547915499
}else if( sqlite3GlobalConfig.bMemstat ){
1548015500
sqlite3_mutex_enter(mem0.mutex);
1548115501
mallocWithAlarm(n, &p);
1548215502
sqlite3_mutex_leave(mem0.mutex);
@@ -15674,14 +15694,17 @@
1567415694
int nOld, nNew;
1567515695
void *pNew;
1567615696
if( pOld==0 ){
1567715697
return sqlite3Malloc(nBytes);
1567815698
}
15679
- if( nBytes<=0 || NEVER(nBytes>=0x7fffff00) ){
15680
- /* The NEVER(...) term is explained in comments on sqlite3Malloc() */
15699
+ if( nBytes<=0 ){
1568115700
sqlite3_free(pOld);
1568215701
return 0;
15702
+ }
15703
+ if( nBytes>=0x7fffff00 ){
15704
+ /* The 0x7ffff00 limit term is explained in comments on sqlite3Malloc() */
15705
+ return 0;
1568315706
}
1568415707
nOld = sqlite3MallocSize(pOld);
1568515708
if( sqlite3GlobalConfig.bMemstat ){
1568615709
sqlite3_mutex_enter(mem0.mutex);
1568715710
sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, nBytes);
@@ -17977,11 +18000,11 @@
1797718000
** Utility functions used throughout sqlite.
1797818001
**
1797918002
** This file contains functions for allocating memory, comparing
1798018003
** strings, and stuff like that.
1798118004
**
17982
-** $Id: util.c,v 1.260 2009/06/17 16:20:04 drh Exp $
18005
+** $Id: util.c,v 1.261 2009/06/24 10:26:33 drh Exp $
1798318006
*/
1798418007
#ifdef SQLITE_HAVE_ISNAN
1798518008
# include <math.h>
1798618009
#endif
1798718010
@@ -17991,31 +18014,10 @@
1799118014
#ifdef SQLITE_COVERAGE_TEST
1799218015
SQLITE_PRIVATE void sqlite3Coverage(int x){
1799318016
static int dummy = 0;
1799418017
dummy += x;
1799518018
}
17996
-#endif
17997
-
17998
-/*
17999
-** Routine needed to support the ALWAYS() and NEVER() macros.
18000
-**
18001
-** The argument to ALWAYS() should always be true and the argument
18002
-** to NEVER() should always be false. If either is not the case
18003
-** then this routine is called in order to throw an error.
18004
-**
18005
-** This routine only exists if assert() is operational. It always
18006
-** throws an assert on its first invocation. The variable has a long
18007
-** name to help the assert() message be more readable. The variable
18008
-** is used to prevent a too-clever optimizer from optimizing out the
18009
-** entire call.
18010
-*/
18011
-#ifndef NDEBUG
18012
-SQLITE_PRIVATE int sqlite3Assert(void){
18013
- static volatile int ALWAYS_was_false_or_NEVER_was_true = 0;
18014
- assert( ALWAYS_was_false_or_NEVER_was_true ); /* Always fails */
18015
- return ALWAYS_was_false_or_NEVER_was_true++; /* Not Reached */
18016
-}
1801718019
#endif
1801818020
1801918021
/*
1802018022
** Return true if the floating point value is Not a Number (NaN).
1802118023
**
@@ -30495,11 +30497,11 @@
3049530497
** is separate from the database file. The pager also implements file
3049630498
** locking to prevent two processes from writing the same database
3049730499
** file simultaneously, or one process from reading the database while
3049830500
** another is writing.
3049930501
**
30500
-** @(#) $Id: pager.c,v 1.601 2009/06/22 05:43:24 danielk1977 Exp $
30502
+** @(#) $Id: pager.c,v 1.603 2009/06/26 12:15:23 drh Exp $
3050130503
*/
3050230504
#ifndef SQLITE_OMIT_DISKIO
3050330505
3050430506
/*
3050530507
** Macros for troubleshooting. Normally turned off
@@ -31233,11 +31235,10 @@
3123331235
pPager->aSavepoint[ii].iHdrOffset = pPager->journalOff;
3123431236
}
3123531237
}
3123631238
3123731239
pPager->journalHdr = pPager->journalOff = journalHdrOffset(pPager);
31238
- memcpy(zHeader, aJournalMagic, sizeof(aJournalMagic));
3123931240
3124031241
/*
3124131242
** Write the nRec Field - the number of page records that follow this
3124231243
** journal header. Normally, zero is written to this value at this time.
3124331244
** After the records are added to the journal (and the journal synced,
@@ -31259,12 +31260,14 @@
3125931260
*/
3126031261
assert( isOpen(pPager->fd) || pPager->noSync );
3126131262
if( (pPager->noSync) || (pPager->journalMode==PAGER_JOURNALMODE_MEMORY)
3126231263
|| (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND)
3126331264
){
31265
+ memcpy(zHeader, aJournalMagic, sizeof(aJournalMagic));
3126431266
put32bits(&zHeader[sizeof(aJournalMagic)], 0xffffffff);
3126531267
}else{
31268
+ zHeader[0] = '\0';
3126631269
put32bits(&zHeader[sizeof(aJournalMagic)], 0);
3126731270
}
3126831271
3126931272
/* The random check-hash initialiser */
3127031273
sqlite3_randomness(sizeof(pPager->cksumInit), &pPager->cksumInit);
@@ -31328,10 +31331,11 @@
3132831331
** returned and *pNRec and *PDbSize are undefined. If JOURNAL_HDR_SZ bytes
3132931332
** cannot be read from the journal file an error code is returned.
3133031333
*/
3133131334
static int readJournalHdr(
3133231335
Pager *pPager, /* Pager object */
31336
+ int isHot,
3133331337
i64 journalSize, /* Size of the open journal file in bytes */
3133431338
u32 *pNRec, /* OUT: Value read from the nRec field */
3133531339
u32 *pDbSize /* OUT: Value of original database size field */
3133631340
){
3133731341
int rc; /* Return code */
@@ -31353,16 +31357,18 @@
3135331357
/* Read in the first 8 bytes of the journal header. If they do not match
3135431358
** the magic string found at the start of each journal header, return
3135531359
** SQLITE_DONE. If an IO error occurs, return an error code. Otherwise,
3135631360
** proceed.
3135731361
*/
31358
- rc = sqlite3OsRead(pPager->jfd, aMagic, sizeof(aMagic), iHdrOff);
31359
- if( rc ){
31360
- return rc;
31361
- }
31362
- if( memcmp(aMagic, aJournalMagic, sizeof(aMagic))!=0 ){
31363
- return SQLITE_DONE;
31362
+ if( isHot || iHdrOff!=pPager->journalHdr ){
31363
+ rc = sqlite3OsRead(pPager->jfd, aMagic, sizeof(aMagic), iHdrOff);
31364
+ if( rc ){
31365
+ return rc;
31366
+ }
31367
+ if( memcmp(aMagic, aJournalMagic, sizeof(aMagic))!=0 ){
31368
+ return SQLITE_DONE;
31369
+ }
3136431370
}
3136531371
3136631372
/* Read the first three 32-bit fields of the journal header: The nRec
3136731373
** field, the checksum-initializer and the database size at the start
3136831374
** of the transaction. Return an error code if anything goes wrong.
@@ -32453,11 +32459,11 @@
3245332459
/* Read the next journal header from the journal file. If there are
3245432460
** not enough bytes left in the journal file for a complete header, or
3245532461
** it is corrupted, then a process must of failed while writing it.
3245632462
** This indicates nothing more needs to be rolled back.
3245732463
*/
32458
- rc = readJournalHdr(pPager, szJ, &nRec, &mxPg);
32464
+ rc = readJournalHdr(pPager, isHot, szJ, &nRec, &mxPg);
3245932465
if( rc!=SQLITE_OK ){
3246032466
if( rc==SQLITE_DONE ){
3246132467
rc = SQLITE_OK;
3246232468
}
3246332469
goto end_playback;
@@ -32673,11 +32679,11 @@
3267332679
*/
3267432680
while( rc==SQLITE_OK && pPager->journalOff<szJ ){
3267532681
u32 ii; /* Loop counter */
3267632682
u32 nJRec = 0; /* Number of Journal Records */
3267732683
u32 dummy;
32678
- rc = readJournalHdr(pPager, szJ, &nJRec, &dummy);
32684
+ rc = readJournalHdr(pPager, 0, szJ, &nJRec, &dummy);
3267932685
assert( rc!=SQLITE_DONE );
3268032686
3268132687
/*
3268232688
** The "pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff"
3268332689
** test is related to ticket #2565. See the discussion in the
@@ -33236,16 +33242,10 @@
3323633242
int rc; /* Return code */
3323733243
const int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
3323833244
assert( isOpen(pPager->jfd) );
3323933245
3324033246
if( 0==(iDc&SQLITE_IOCAP_SAFE_APPEND) ){
33241
- /* Variable iNRecOffset is set to the offset in the journal file
33242
- ** of the nRec field of the most recently written journal header.
33243
- ** This field will be updated following the xSync() operation
33244
- ** on the journal file. */
33245
- i64 iNRecOffset = pPager->journalHdr + sizeof(aJournalMagic);
33246
-
3324733247
/* This block deals with an obscure problem. If the last connection
3324833248
** that wrote to this database was operating in persistent-journal
3324933249
** mode, then the journal file may at this point actually be larger
3325033250
** than Pager.journalOff bytes. If the next thing in the journal
3325133251
** file happens to be a journal-header (written as part of the
@@ -33264,12 +33264,18 @@
3326433264
** Variable iNextHdrOffset is set to the offset at which this
3326533265
** problematic header will occur, if it exists. aMagic is used
3326633266
** as a temporary buffer to inspect the first couple of bytes of
3326733267
** the potential journal header.
3326833268
*/
33269
- i64 iNextHdrOffset = journalHdrOffset(pPager);
33269
+ i64 iNextHdrOffset;
3327033270
u8 aMagic[8];
33271
+ u8 zHeader[sizeof(aJournalMagic)+4];
33272
+
33273
+ memcpy(zHeader, aJournalMagic, sizeof(aJournalMagic));
33274
+ put32bits(&zHeader[sizeof(aJournalMagic)], pPager->nRec);
33275
+
33276
+ iNextHdrOffset = journalHdrOffset(pPager);
3327133277
rc = sqlite3OsRead(pPager->jfd, aMagic, 8, iNextHdrOffset);
3327233278
if( rc==SQLITE_OK && 0==memcmp(aMagic, aJournalMagic, 8) ){
3327333279
static const u8 zerobyte = 0;
3327433280
rc = sqlite3OsWrite(pPager->jfd, &zerobyte, 1, iNextHdrOffset);
3327533281
}
@@ -33292,12 +33298,14 @@
3329233298
PAGERTRACE(("SYNC journal of %d\n", PAGERID(pPager)));
3329333299
IOTRACE(("JSYNC %p\n", pPager))
3329433300
rc = sqlite3OsSync(pPager->jfd, pPager->sync_flags);
3329533301
if( rc!=SQLITE_OK ) return rc;
3329633302
}
33297
- IOTRACE(("JHDR %p %lld %d\n", pPager, iNRecOffset, 4));
33298
- rc = write32bits(pPager->jfd, iNRecOffset, pPager->nRec);
33303
+ IOTRACE(("JHDR %p %lld\n", pPager, pPager->journalHdr));
33304
+ rc = sqlite3OsWrite(
33305
+ pPager->jfd, zHeader, sizeof(zHeader), pPager->journalHdr
33306
+ );
3329933307
if( rc!=SQLITE_OK ) return rc;
3330033308
}
3330133309
if( 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
3330233310
PAGERTRACE(("SYNC journal of %d\n", PAGERID(pPager)));
3330333311
IOTRACE(("JSYNC %p\n", pPager))
@@ -35864,11 +35872,11 @@
3586435872
** May you do good and not evil.
3586535873
** May you find forgiveness for yourself and forgive others.
3586635874
** May you share freely, never taking more than you give.
3586735875
**
3586835876
*************************************************************************
35869
-** $Id: btreeInt.h,v 1.48 2009/06/22 12:05:10 drh Exp $
35877
+** $Id: btreeInt.h,v 1.49 2009/06/24 05:40:34 danielk1977 Exp $
3587035878
**
3587135879
** This file implements a external (disk-based) database using BTrees.
3587235880
** For a detailed discussion of BTrees, refer to
3587335881
**
3587435882
** Donald E. Knuth, THE ART OF COMPUTER PROGRAMMING, Volume 3:
@@ -36330,13 +36338,10 @@
3633036338
int skip; /* (skip<0) -> Prev() is a no-op. (skip>0) -> Next() is */
3633136339
#ifndef SQLITE_OMIT_INCRBLOB
3633236340
u8 isIncrblobHandle; /* True if this cursor is an incr. io handle */
3633336341
Pgno *aOverflow; /* Cache of overflow page locations */
3633436342
#endif
36335
-#ifndef NDEBUG
36336
- u8 pagesShuffled; /* True if Btree pages are rearranged by balance()*/
36337
-#endif
3633836343
i16 iPage; /* Index of current page in apPage */
3633936344
MemPage *apPage[BTCURSOR_MAX_DEPTH]; /* Pages from root to current page */
3634036345
u16 aiIdx[BTCURSOR_MAX_DEPTH]; /* Current index in apPage[i] */
3634136346
};
3634236347
@@ -36858,11 +36863,11 @@
3685836863
** May you do good and not evil.
3685936864
** May you find forgiveness for yourself and forgive others.
3686036865
** May you share freely, never taking more than you give.
3686136866
**
3686236867
*************************************************************************
36863
-** $Id: btree.c,v 1.639 2009/06/23 11:22:29 danielk1977 Exp $
36868
+** $Id: btree.c,v 1.645 2009/06/26 16:32:13 shane Exp $
3686436869
**
3686536870
** This file implements a external (disk-based) database using BTrees.
3686636871
** See the header comment on "btreeInt.h" for additional information.
3686736872
** Including a description of file format and an overview of operation.
3686836873
*/
@@ -37785,11 +37790,10 @@
3778537790
*/
3778637791
assert( nByte <= (
3778737792
get2byte(&data[hdr+5])-(hdr+8+(pPage->leaf?0:4)+2*get2byte(&data[hdr+3]))
3778837793
));
3778937794
37790
- pPage->nFree -= (u16)nByte;
3779137795
nFrag = data[hdr+7];
3779237796
if( nFrag>=60 ){
3779337797
defragmentPage(pPage);
3779437798
}else{
3779537799
/* Search the freelist looking for a free slot big enough to satisfy
@@ -37864,11 +37868,11 @@
3786437868
}
3786537869
assert( pbegin>addr || pbegin==0 );
3786637870
put2byte(&data[addr], start);
3786737871
put2byte(&data[start], pbegin);
3786837872
put2byte(&data[start+2], size);
37869
- pPage->nFree += (u16)size;
37873
+ pPage->nFree = pPage->nFree + (u16)size;
3787037874
3787137875
/* Coalesce adjacent free blocks */
3787237876
addr = pPage->hdrOffset + 1;
3787337877
while( (pbegin = get2byte(&data[addr]))>0 ){
3787437878
int pnext, psize, x;
@@ -38963,14 +38967,14 @@
3896338967
** If there is a transaction in progress, this routine is a no-op.
3896438968
*/
3896538969
static void unlockBtreeIfUnused(BtShared *pBt){
3896638970
assert( sqlite3_mutex_held(pBt->mutex) );
3896738971
if( pBt->inTransaction==TRANS_NONE && pBt->pCursor==0 && pBt->pPage1!=0 ){
38968
- if( sqlite3PagerRefcount(pBt->pPager)>=1 ){
38969
- assert( pBt->pPage1->aData );
38970
- releasePage(pBt->pPage1);
38971
- }
38972
+ assert( pBt->pPage1->aData );
38973
+ assert( sqlite3PagerRefcount(pBt->pPager)==1 );
38974
+ assert( pBt->pPage1->aData );
38975
+ releasePage(pBt->pPage1);
3897238976
pBt->pPage1 = 0;
3897338977
}
3897438978
}
3897538979
3897638980
/*
@@ -41961,11 +41965,11 @@
4196141965
assert( end <= get2byte(&data[hdr+5]) );
4196241966
if (idx+sz > pPage->pBt->usableSize) {
4196341967
return SQLITE_CORRUPT_BKPT;
4196441968
}
4196541969
pPage->nCell++;
41966
- pPage->nFree -= 2;
41970
+ pPage->nFree = pPage->nFree - (u16)(2 + sz);
4196741971
memcpy(&data[idx+nSkip], pCell+nSkip, sz-nSkip);
4196841972
if( iChild ){
4196941973
put4byte(&data[idx], iChild);
4197041974
}
4197141975
for(j=end-2, ptr=&data[j]; j>ins; j-=2, ptr-=2){
@@ -41977,11 +41981,11 @@
4197741981
#ifndef SQLITE_OMIT_AUTOVACUUM
4197841982
if( pPage->pBt->autoVacuum ){
4197941983
/* The cell may contain a pointer to an overflow page. If so, write
4198041984
** the entry for the overflow page into the pointer map.
4198141985
*/
41982
- rc = ptrmapPutOvflPtr(pPage, pCell);
41986
+ return ptrmapPutOvflPtr(pPage, pCell);
4198341987
}
4198441988
#endif
4198541989
}
4198641990
4198741991
return SQLITE_OK;
@@ -42186,10 +42190,59 @@
4218642190
}
4218742191
return 1;
4218842192
}
4218942193
#endif
4219042194
42195
+/*
42196
+** This function is used to copy the contents of the b-tree node stored
42197
+** on page pFrom to page pTo. If page pFrom was not a leaf page, then
42198
+** the pointer-map entries for each child page are updated so that the
42199
+** parent page stored in the pointer map is page pTo. If pFrom contained
42200
+** any cells with overflow page pointers, then the corresponding pointer
42201
+** map entries are also updated so that the parent page is page pTo.
42202
+**
42203
+** If pFrom is currently carrying any overflow cells (entries in the
42204
+** MemPage.aOvfl[] array), they are not copied to pTo.
42205
+**
42206
+** Before returning, page pTo is reinitialized using sqlite3BtreeInitPage().
42207
+**
42208
+** The performance of this function is not critical. It is only used by
42209
+** the balance_shallower() and balance_deeper() procedures, neither of
42210
+** which are called often under normal circumstances.
42211
+*/
42212
+static int copyNodeContent(MemPage *pFrom, MemPage *pTo){
42213
+ BtShared * const pBt = pFrom->pBt;
42214
+ u8 * const aFrom = pFrom->aData;
42215
+ u8 * const aTo = pTo->aData;
42216
+ int const iFromHdr = pFrom->hdrOffset;
42217
+ int const iToHdr = ((pTo->pgno==1) ? 100 : 0);
42218
+ int rc = SQLITE_OK;
42219
+ int iData;
42220
+
42221
+ assert( pFrom->isInit );
42222
+ assert( pFrom->nFree>=iToHdr );
42223
+ assert( get2byte(&aFrom[iFromHdr+5])<=pBt->usableSize );
42224
+
42225
+ /* Copy the b-tree node content from page pFrom to page pTo. */
42226
+ iData = get2byte(&aFrom[iFromHdr+5]);
42227
+ memcpy(&aTo[iData], &aFrom[iData], pBt->usableSize-iData);
42228
+ memcpy(&aTo[iToHdr], &aFrom[iFromHdr], pFrom->cellOffset + 2*pFrom->nCell);
42229
+
42230
+ /* Reinitialize page pTo so that the contents of the MemPage structure
42231
+ ** match the new data. The initialization of pTo "cannot" fail, as the
42232
+ ** data copied from pFrom is known to be valid. */
42233
+ pTo->isInit = 0;
42234
+ TESTONLY(rc = ) sqlite3BtreeInitPage(pTo);
42235
+ assert( rc==SQLITE_OK );
42236
+
42237
+ /* If this is an auto-vacuum database, update the pointer-map entries
42238
+ ** for any b-tree or overflow pages that pTo now contains the pointers to. */
42239
+ if( ISAUTOVACUUM ){
42240
+ rc = setChildPtrmaps(pTo);
42241
+ }
42242
+ return rc;
42243
+}
4219142244
4219242245
/*
4219342246
** This routine redistributes cells on the iParentIdx'th child of pParent
4219442247
** (hereafter "the page") and up to 2 siblings so that all pages have about the
4219542248
** same amount of free space. Usually a single sibling on either side of the
@@ -42230,21 +42283,22 @@
4223042283
** SQLITE_NOMEM.
4223142284
*/
4223242285
static int balance_nonroot(
4223342286
MemPage *pParent, /* Parent page of siblings being balanced */
4223442287
int iParentIdx, /* Index of "the page" in pParent */
42235
- u8 *aOvflSpace /* page-size bytes of space for parent ovfl */
42288
+ u8 *aOvflSpace, /* page-size bytes of space for parent ovfl */
42289
+ int isRoot /* True if pParent is a root-page */
4223642290
){
4223742291
BtShared *pBt; /* The whole database */
4223842292
int nCell = 0; /* Number of cells in apCell[] */
4223942293
int nMaxCells = 0; /* Allocated size of apCell, szCell, aFrom. */
4224042294
int nNew = 0; /* Number of pages in apNew[] */
4224142295
int nOld; /* Number of pages in apOld[] */
4224242296
int i, j, k; /* Loop counters */
4224342297
int nxDiv; /* Next divider slot in pParent->aCell[] */
4224442298
int rc = SQLITE_OK; /* The return code */
42245
- int leafCorrection; /* 4 if pPage is a leaf. 0 if not */
42299
+ u16 leafCorrection; /* 4 if pPage is a leaf. 0 if not */
4224642300
int leafData; /* True if pPage is a leaf of a LEAFDATA tree */
4224742301
int usableSpace; /* Bytes in pPage beyond the header */
4224842302
int pageFlags; /* Value of pPage->aData[0] */
4224942303
int subtotal; /* Subtotal of bytes in cells on one page */
4225042304
int iSpace1 = 0; /* First unused byte of aSpace1[] */
@@ -42409,11 +42463,11 @@
4240942463
apCell[nCell] = findOverflowCell(pOld, j);
4241042464
szCell[nCell] = cellSizePtr(pOld, apCell[nCell]);
4241142465
nCell++;
4241242466
}
4241342467
if( i<nOld-1 && !leafData){
42414
- u16 sz = szNew[i];
42468
+ u16 sz = (u16)szNew[i];
4241542469
u8 *pTemp;
4241642470
assert( nCell<nMaxCells );
4241742471
szCell[nCell] = sz;
4241842472
pTemp = &aSpace1[iSpace1];
4241942473
iSpace1 += sz;
@@ -42420,11 +42474,11 @@
4242042474
assert( sz<=pBt->pageSize/4 );
4242142475
assert( iSpace1<=pBt->pageSize );
4242242476
memcpy(pTemp, apDiv[i], sz);
4242342477
apCell[nCell] = pTemp+leafCorrection;
4242442478
assert( leafCorrection==0 || leafCorrection==4 );
42425
- szCell[nCell] -= (u16)leafCorrection;
42479
+ szCell[nCell] = szCell[nCell] - leafCorrection;
4242642480
if( !pOld->leaf ){
4242742481
assert( leafCorrection==0 );
4242842482
assert( pOld->hdrOffset==0 );
4242942483
/* The right pointer of the child page pOld becomes the left
4243042484
** pointer of the divider cell */
@@ -42618,11 +42672,12 @@
4261842672
j = cntNew[i];
4261942673
4262042674
/* If the sibling page assembled above was not the right-most sibling,
4262142675
** insert a divider cell into the parent page.
4262242676
*/
42623
- if( i<nNew-1 && j<nCell ){
42677
+ assert( i<nNew-1 || j==nCell );
42678
+ if( j<nCell ){
4262442679
u8 *pCell;
4262542680
u8 *pTemp;
4262642681
int sz;
4262742682
4262842683
assert( j<nMaxCells );
@@ -42678,42 +42733,64 @@
4267842733
if( (pageFlags & PTF_LEAF)==0 ){
4267942734
u8 *zChild = &apCopy[nOld-1]->aData[8];
4268042735
memcpy(&apNew[nNew-1]->aData[8], zChild, 4);
4268142736
}
4268242737
42683
- /* Fix the pointer-map entries for all the cells that were shifted around.
42684
- ** There are several different types of pointer-map entries that need to
42685
- ** be dealt with by this routine. Some of these have been set already, but
42686
- ** many have not. The following is a summary:
42687
- **
42688
- ** 1) The entries associated with new sibling pages that were not
42689
- ** siblings when this function was called. These have already
42690
- ** been set. We don't need to worry about old siblings that were
42691
- ** moved to the free-list - the freePage() code has taken care
42692
- ** of those.
42693
- **
42694
- ** 2) The pointer-map entries associated with the first overflow
42695
- ** page in any overflow chains used by new divider cells. These
42696
- ** have also already been taken care of by the insertCell() code.
42697
- **
42698
- ** 3) If the sibling pages are not leaves, then the child pages of
42699
- ** cells stored on the sibling pages may need to be updated.
42700
- **
42701
- ** 4) If the sibling pages are not internal intkey nodes, then any
42702
- ** overflow pages used by these cells may need to be updated
42703
- ** (internal intkey nodes never contain pointers to overflow pages).
42704
- **
42705
- ** 5) If the sibling pages are not leaves, then the pointer-map
42706
- ** entries for the right-child pages of each sibling may need
42707
- ** to be updated.
42708
- **
42709
- ** Cases 1 and 2 are dealt with above by other code. The following
42710
- ** block deals with cases 3 and 4. Since setting a pointer map entry
42711
- ** is a relatively expensive operation, this code only sets pointer
42712
- ** map entries for child or overflow pages that have actually moved
42713
- ** between pages. */
42714
- if( ISAUTOVACUUM ){
42738
+ if( isRoot && pParent->nCell==0 && pParent->hdrOffset<=apNew[0]->nFree ){
42739
+ /* The root page of the b-tree now contains no cells. The only sibling
42740
+ ** page is the right-child of the parent. Copy the contents of the
42741
+ ** child page into the parent, decreasing the overall height of the
42742
+ ** b-tree structure by one. This is described as the "balance-shallower"
42743
+ ** sub-algorithm in some documentation.
42744
+ **
42745
+ ** If this is an auto-vacuum database, the call to copyNodeContent()
42746
+ ** sets all pointer-map entries corresponding to database image pages
42747
+ ** for which the pointer is stored within the content being copied.
42748
+ **
42749
+ ** The second assert below verifies that the child page is defragmented
42750
+ ** (it must be, as it was just reconstructed using assemblePage()). This
42751
+ ** is important if the parent page happens to be page 1 of the database
42752
+ ** image. */
42753
+ assert( nNew==1 );
42754
+ assert( apNew[0]->nFree ==
42755
+ (get2byte(&apNew[0]->aData[5])-apNew[0]->cellOffset-apNew[0]->nCell*2)
42756
+ );
42757
+ if( SQLITE_OK==(rc = copyNodeContent(apNew[0], pParent)) ){
42758
+ rc = freePage(apNew[0]);
42759
+ }
42760
+ }else if( ISAUTOVACUUM ){
42761
+ /* Fix the pointer-map entries for all the cells that were shifted around.
42762
+ ** There are several different types of pointer-map entries that need to
42763
+ ** be dealt with by this routine. Some of these have been set already, but
42764
+ ** many have not. The following is a summary:
42765
+ **
42766
+ ** 1) The entries associated with new sibling pages that were not
42767
+ ** siblings when this function was called. These have already
42768
+ ** been set. We don't need to worry about old siblings that were
42769
+ ** moved to the free-list - the freePage() code has taken care
42770
+ ** of those.
42771
+ **
42772
+ ** 2) The pointer-map entries associated with the first overflow
42773
+ ** page in any overflow chains used by new divider cells. These
42774
+ ** have also already been taken care of by the insertCell() code.
42775
+ **
42776
+ ** 3) If the sibling pages are not leaves, then the child pages of
42777
+ ** cells stored on the sibling pages may need to be updated.
42778
+ **
42779
+ ** 4) If the sibling pages are not internal intkey nodes, then any
42780
+ ** overflow pages used by these cells may need to be updated
42781
+ ** (internal intkey nodes never contain pointers to overflow pages).
42782
+ **
42783
+ ** 5) If the sibling pages are not leaves, then the pointer-map
42784
+ ** entries for the right-child pages of each sibling may need
42785
+ ** to be updated.
42786
+ **
42787
+ ** Cases 1 and 2 are dealt with above by other code. The next
42788
+ ** block deals with cases 3 and 4 and the one after that, case 5. Since
42789
+ ** setting a pointer map entry is a relatively expensive operation, this
42790
+ ** code only sets pointer map entries for child or overflow pages that have
42791
+ ** actually moved between pages. */
4271542792
MemPage *pNew = apNew[0];
4271642793
MemPage *pOld = apCopy[0];
4271742794
int nOverflow = pOld->nOverflow;
4271842795
int iNextOld = pOld->nCell + nOverflow;
4271942796
int iOverflow = (nOverflow ? pOld->aOvfl[0].idx : -1);
@@ -42787,11 +42864,11 @@
4278742864
}
4278842865
4278942866
assert( pParent->isInit );
4279042867
TRACE(("BALANCE: finished: old=%d new=%d cells=%d\n",
4279142868
nOld, nNew, nCell));
42792
-
42869
+
4279342870
/*
4279442871
** Cleanup before returning.
4279542872
*/
4279642873
balance_cleanup:
4279742874
sqlite3ScratchFree(apCell);
@@ -42800,113 +42877,10 @@
4280042877
}
4280142878
for(i=0; i<nNew; i++){
4280242879
releasePage(apNew[i]);
4280342880
}
4280442881
42805
- return rc;
42806
-}
42807
-
42808
-/*
42809
-** This function is used to copy the contents of the b-tree node stored
42810
-** on page pFrom to page pTo. If page pFrom was not a leaf page, then
42811
-** the pointer-map entries for each child page are updated so that the
42812
-** parent page stored in the pointer map is page pTo. If pFrom contained
42813
-** any cells with overflow page pointers, then the corresponding pointer
42814
-** map entries are also updated so that the parent page is page pTo.
42815
-**
42816
-** If pFrom is currently carrying any overflow cells (entries in the
42817
-** MemPage.aOvfl[] array), they are not copied to pTo.
42818
-**
42819
-** Before returning, page pTo is reinitialized using sqlite3BtreeInitPage().
42820
-**
42821
-** The performance of this function is not critical. It is only used by
42822
-** the balance_shallower() and balance_deeper() procedures, neither of
42823
-** which are called often under normal circumstances.
42824
-*/
42825
-static int copyNodeContent(MemPage *pFrom, MemPage *pTo){
42826
- BtShared * const pBt = pFrom->pBt;
42827
- u8 * const aFrom = pFrom->aData;
42828
- u8 * const aTo = pTo->aData;
42829
- int const iFromHdr = pFrom->hdrOffset;
42830
- int const iToHdr = ((pTo->pgno==1) ? 100 : 0);
42831
- int rc = SQLITE_OK;
42832
- int iData;
42833
-
42834
- assert( pFrom->isInit );
42835
- assert( pFrom->nFree>=iToHdr );
42836
- assert( get2byte(&aFrom[iFromHdr+5])<=pBt->usableSize );
42837
-
42838
- /* Copy the b-tree node content from page pFrom to page pTo. */
42839
- iData = get2byte(&aFrom[iFromHdr+5]);
42840
- memcpy(&aTo[iData], &aFrom[iData], pBt->usableSize-iData);
42841
- memcpy(&aTo[iToHdr], &aFrom[iFromHdr], pFrom->cellOffset + 2*pFrom->nCell);
42842
-
42843
- /* Reinitialize page pTo so that the contents of the MemPage structure
42844
- ** match the new data. The initialization of pTo "cannot" fail, as the
42845
- ** data copied from pFrom is known to be valid. */
42846
- pTo->isInit = 0;
42847
- TESTONLY(rc = ) sqlite3BtreeInitPage(pTo);
42848
- assert( rc==SQLITE_OK );
42849
-
42850
- /* If this is an auto-vacuum database, update the pointer-map entries
42851
- ** for any b-tree or overflow pages that pTo now contains the pointers to. */
42852
- if( ISAUTOVACUUM ){
42853
- rc = setChildPtrmaps(pTo);
42854
- }
42855
- return rc;
42856
-}
42857
-
42858
-/*
42859
-** This routine is called on the root page of a btree when the root
42860
-** page contains no cells. This is an opportunity to make the tree
42861
-** shallower by one level.
42862
-*/
42863
-static int balance_shallower(MemPage *pRoot){
42864
- /* The root page is empty but has one child. Transfer the
42865
- ** information from that one child into the root page if it
42866
- ** will fit. This reduces the depth of the tree by one.
42867
- **
42868
- ** If the root page is page 1, it has less space available than
42869
- ** its child (due to the 100 byte header that occurs at the beginning
42870
- ** of the database fle), so it might not be able to hold all of the
42871
- ** information currently contained in the child. If this is the
42872
- ** case, then do not do the transfer. Leave page 1 empty except
42873
- ** for the right-pointer to the child page. The child page becomes
42874
- ** the virtual root of the tree.
42875
- */
42876
- int rc = SQLITE_OK; /* Return code */
42877
- int const hdr = pRoot->hdrOffset; /* Offset of root page header */
42878
- MemPage *pChild; /* Only child of pRoot */
42879
- Pgno const pgnoChild = get4byte(&pRoot->aData[pRoot->hdrOffset+8]);
42880
-
42881
- assert( pRoot->nCell==0 );
42882
- assert( sqlite3_mutex_held(pRoot->pBt->mutex) );
42883
- assert( !pRoot->leaf );
42884
- assert( pgnoChild>0 );
42885
- assert( pgnoChild<=pagerPagecount(pRoot->pBt) );
42886
- assert( hdr==0 || pRoot->pgno==1 );
42887
-
42888
- rc = sqlite3BtreeGetPage(pRoot->pBt, pgnoChild, &pChild, 0);
42889
- if( rc==SQLITE_OK ){
42890
- if( pChild->nFree>=hdr ){
42891
- if( hdr ){
42892
- rc = defragmentPage(pChild);
42893
- }
42894
- if( rc==SQLITE_OK ){
42895
- rc = copyNodeContent(pChild, pRoot);
42896
- }
42897
- if( rc==SQLITE_OK ){
42898
- rc = freePage(pChild);
42899
- }
42900
- }else{
42901
- /* The child has more information that will fit on the root.
42902
- ** The tree is already balanced. Do nothing. */
42903
- TRACE(("BALANCE: child %d will not fit on page 1\n", pChild->pgno));
42904
- }
42905
- releasePage(pChild);
42906
- }
42907
-
4290842882
return rc;
4290942883
}
4291042884
4291142885
4291242886
/*
@@ -42974,18 +42948,12 @@
4297442948
** some way. This function figures out if this modification means the
4297542949
** tree needs to be balanced, and if so calls the appropriate balancing
4297642950
** routine. Balancing routines are:
4297742951
**
4297842952
** balance_quick()
42979
-** balance_shallower()
4298042953
** balance_deeper()
4298142954
** balance_nonroot()
42982
-**
42983
-** If built with SQLITE_DEBUG, pCur->pagesShuffled is set to true if
42984
-** balance_shallower(), balance_deeper() or balance_nonroot() is called.
42985
-** If none of these functions are invoked, pCur->pagesShuffled is left
42986
-** unmodified.
4298742955
*/
4298842956
static int balance(BtCursor *pCur){
4298942957
int rc = SQLITE_OK;
4299042958
const int nMin = pCur->pBt->usableSize * 2 / 3;
4299142959
u8 aBalanceQuickSpace[13];
@@ -43011,25 +42979,11 @@
4301142979
pCur->iPage = 1;
4301242980
pCur->aiIdx[0] = 0;
4301342981
pCur->aiIdx[1] = 0;
4301442982
assert( pCur->apPage[1]->nOverflow );
4301542983
}
43016
- VVA_ONLY( pCur->pagesShuffled = 1 );
4301742984
}else{
43018
- /* The root page of the b-tree is now empty. If the root-page is not
43019
- ** also a leaf page, it will have a single child page. Call
43020
- ** balance_shallower to attempt to copy the contents of the single
43021
- ** child-page into the root page (this may not be possible if the
43022
- ** root page is page 1).
43023
- **
43024
- ** Whether or not this is possible , the tree is now balanced.
43025
- ** Therefore is no next iteration of the do-loop.
43026
- */
43027
- if( pPage->nCell==0 && !pPage->leaf ){
43028
- rc = balance_shallower(pPage);
43029
- VVA_ONLY( pCur->pagesShuffled = 1 );
43030
- }
4303142985
break;
4303242986
}
4303342987
}else if( pPage->nOverflow==0 && pPage->nFree<=nMin ){
4303442988
break;
4303542989
}else{
@@ -43079,11 +43033,11 @@
4307943033
** the previous call, as the overflow cell data will have been
4308043034
** copied either into the body of a database page or into the new
4308143035
** pSpace buffer passed to the latter call to balance_nonroot().
4308243036
*/
4308343037
u8 *pSpace = sqlite3PageMalloc(pCur->pBt->pageSize);
43084
- rc = balance_nonroot(pParent, iIdx, pSpace);
43038
+ rc = balance_nonroot(pParent, iIdx, pSpace, iPage==1);
4308543039
if( pFree ){
4308643040
/* If pFree is not NULL, it points to the pSpace buffer used
4308743041
** by a previous call to balance_nonroot(). Its contents are
4308843042
** now stored either on real database pages or within the
4308943043
** new pSpace buffer, so it may be safely freed here. */
@@ -43092,11 +43046,10 @@
4309243046
4309343047
/* The pSpace buffer will be freed after the next call to
4309443048
** balance_nonroot(), or just before this function returns, whichever
4309543049
** comes first. */
4309643050
pFree = pSpace;
43097
- VVA_ONLY( pCur->pagesShuffled = 1 );
4309843051
}
4309943052
}
4310043053
4310143054
pPage->nOverflow = 0;
4310243055
@@ -45290,11 +45243,11 @@
4529045243
** This file contains code use to manipulate "Mem" structure. A "Mem"
4529145244
** stores a single value in the VDBE. Mem is an opaque structure visible
4529245245
** only within the VDBE. Interface routines refer to a Mem using the
4529345246
** name sqlite_value
4529445247
**
45295
-** $Id: vdbemem.c,v 1.149 2009/06/22 19:05:41 drh Exp $
45248
+** $Id: vdbemem.c,v 1.150 2009/06/25 01:47:12 drh Exp $
4529645249
*/
4529745250
4529845251
/*
4529945252
** Call sqlite3VdbeMemExpandBlob() on the supplied value (type Mem*)
4530045253
** P if required.
@@ -45369,11 +45322,11 @@
4536945322
sqlite3DbFree(pMem->db, pMem->zMalloc);
4537045323
pMem->zMalloc = sqlite3DbMallocRaw(pMem->db, n);
4537145324
}
4537245325
}
4537345326
45374
- if( preserve && pMem->z && pMem->zMalloc && pMem->z!=pMem->zMalloc ){
45327
+ if( pMem->z && preserve && pMem->zMalloc && pMem->z!=pMem->zMalloc ){
4537545328
memcpy(pMem->zMalloc, pMem->z, pMem->n);
4537645329
}
4537745330
if( pMem->flags&MEM_Dyn && pMem->xDel ){
4537845331
pMem->xDel((void *)(pMem->z));
4537945332
}
@@ -45518,11 +45471,11 @@
4551845471
** Return SQLITE_ERROR if the finalizer reports an error. SQLITE_OK
4551945472
** otherwise.
4552045473
*/
4552145474
SQLITE_PRIVATE int sqlite3VdbeMemFinalize(Mem *pMem, FuncDef *pFunc){
4552245475
int rc = SQLITE_OK;
45523
- if( pFunc && pFunc->xFinalize ){
45476
+ if( ALWAYS(pFunc && pFunc->xFinalize) ){
4552445477
sqlite3_context ctx;
4552545478
assert( (pMem->flags & MEM_Null)!=0 || pFunc==pMem->u.pDef );
4552645479
assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
4552745480
memset(&ctx, 0, sizeof(ctx));
4552845481
ctx.s.flags = MEM_Null;
@@ -45531,11 +45484,11 @@
4553145484
ctx.pFunc = pFunc;
4553245485
pFunc->xFinalize(&ctx);
4553345486
assert( 0==(pMem->flags&MEM_Dyn) && !pMem->xDel );
4553445487
sqlite3DbFree(pMem->db, pMem->zMalloc);
4553545488
memcpy(pMem, &ctx.s, sizeof(ctx.s));
45536
- rc = (ctx.isError?SQLITE_ERROR:SQLITE_OK);
45489
+ rc = ctx.isError;
4553745490
}
4553845491
return rc;
4553945492
}
4554045493
4554145494
/*
@@ -45806,16 +45759,13 @@
4580645759
** empty boolean index.
4580745760
*/
4580845761
SQLITE_PRIVATE void sqlite3VdbeMemSetRowSet(Mem *pMem){
4580945762
sqlite3 *db = pMem->db;
4581045763
assert( db!=0 );
45811
- if( pMem->flags & MEM_RowSet ){
45812
- sqlite3RowSetClear(pMem->u.pRowSet);
45813
- }else{
45814
- sqlite3VdbeMemRelease(pMem);
45815
- pMem->zMalloc = sqlite3DbMallocRaw(db, 64);
45816
- }
45764
+ assert( (pMem->flags & MEM_RowSet)==0 );
45765
+ sqlite3VdbeMemRelease(pMem);
45766
+ pMem->zMalloc = sqlite3DbMallocRaw(db, 64);
4581745767
if( db->mallocFailed ){
4581845768
pMem->flags = MEM_Null;
4581945769
}else{
4582045770
assert( pMem->zMalloc );
4582145771
pMem->u.pRowSet = sqlite3RowSetInit(db, pMem->zMalloc,
@@ -46153,11 +46103,11 @@
4615346103
}else{
4615446104
zData = (char *)sqlite3BtreeDataFetch(pCur, &available);
4615546105
}
4615646106
assert( zData!=0 );
4615746107
46158
- if( offset+amt<=available && ((pMem->flags&MEM_Dyn)==0 || pMem->xDel) ){
46108
+ if( offset+amt<=available && (pMem->flags&MEM_Dyn)==0 ){
4615946109
sqlite3VdbeMemRelease(pMem);
4616046110
pMem->z = &zData[offset];
4616146111
pMem->flags = MEM_Blob|MEM_Ephem;
4616246112
}else if( SQLITE_OK==(rc = sqlite3VdbeMemGrow(pMem, amt+2, 0)) ){
4616346113
pMem->flags = MEM_Blob|MEM_Dyn|MEM_Term;
@@ -46370,11 +46320,11 @@
4637046320
** This file contains code used for creating, destroying, and populating
4637146321
** a VDBE (or an "sqlite3_stmt" as it is known to the outside world.) Prior
4637246322
** to version 2.8.7, all this code was combined into the vdbe.c source file.
4637346323
** But that file was getting too big so this subroutines were split out.
4637446324
**
46375
-** $Id: vdbeaux.c,v 1.464 2009/06/23 14:15:04 drh Exp $
46325
+** $Id: vdbeaux.c,v 1.467 2009/06/26 16:32:13 shane Exp $
4637646326
*/
4637746327
4637846328
4637946329
4638046330
/*
@@ -46499,11 +46449,11 @@
4649946449
i = p->nOp;
4650046450
assert( p->magic==VDBE_MAGIC_INIT );
4650146451
assert( op>0 && op<0xff );
4650246452
if( p->nOpAlloc<=i ){
4650346453
if( growOpArray(p) ){
46504
- return 0;
46454
+ return 1;
4650546455
}
4650646456
}
4650746457
p->nOp++;
4650846458
pOp = &p->aOp[i];
4650946459
pOp->opcode = (u8)op;
@@ -46704,11 +46654,11 @@
4670446654
assert( p->magic==VDBE_MAGIC_INIT );
4670546655
if( p->nOp + nOp > p->nOpAlloc && growOpArray(p) ){
4670646656
return 0;
4670746657
}
4670846658
addr = p->nOp;
46709
- if( nOp>0 ){
46659
+ if( ALWAYS(nOp>0) ){
4671046660
int i;
4671146661
VdbeOpList const *pIn = aOp;
4671246662
for(i=0; i<nOp; i++, pIn++){
4671346663
int p2 = pIn->p2;
4671446664
VdbeOp *pOut = &p->aOp[i+addr];
@@ -46740,44 +46690,47 @@
4674046690
** This routine is useful when a large program is loaded from a
4674146691
** static array using sqlite3VdbeAddOpList but we want to make a
4674246692
** few minor changes to the program.
4674346693
*/
4674446694
SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe *p, int addr, int val){
46745
- assert( p==0 || p->magic==VDBE_MAGIC_INIT );
46746
- if( p && addr>=0 && p->nOp>addr && p->aOp ){
46695
+ assert( p!=0 );
46696
+ assert( addr>=0 );
46697
+ if( p->nOp>addr ){
4674746698
p->aOp[addr].p1 = val;
4674846699
}
4674946700
}
4675046701
4675146702
/*
4675246703
** Change the value of the P2 operand for a specific instruction.
4675346704
** This routine is useful for setting a jump destination.
4675446705
*/
4675546706
SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe *p, int addr, int val){
46756
- assert( p==0 || p->magic==VDBE_MAGIC_INIT );
46757
- if( p && addr>=0 && p->nOp>addr && p->aOp ){
46707
+ assert( p!=0 );
46708
+ assert( addr>=0 );
46709
+ if( p->nOp>addr ){
4675846710
p->aOp[addr].p2 = val;
4675946711
}
4676046712
}
4676146713
4676246714
/*
4676346715
** Change the value of the P3 operand for a specific instruction.
4676446716
*/
4676546717
SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe *p, int addr, int val){
46766
- assert( p==0 || p->magic==VDBE_MAGIC_INIT );
46767
- if( p && addr>=0 && p->nOp>addr && p->aOp ){
46718
+ assert( p!=0 );
46719
+ assert( addr>=0 );
46720
+ if( p->nOp>addr ){
4676846721
p->aOp[addr].p3 = val;
4676946722
}
4677046723
}
4677146724
4677246725
/*
4677346726
** Change the value of the P5 operand for the most recently
4677446727
** added operation.
4677546728
*/
4677646729
SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe *p, u8 val){
46777
- assert( p==0 || p->magic==VDBE_MAGIC_INIT );
46778
- if( p && p->aOp ){
46730
+ assert( p!=0 );
46731
+ if( p->aOp ){
4677946732
assert( p->nOp>0 );
4678046733
p->aOp[p->nOp-1].p5 = val;
4678146734
}
4678246735
}
4678346736
@@ -46793,11 +46746,11 @@
4679346746
/*
4679446747
** If the input FuncDef structure is ephemeral, then free it. If
4679546748
** the FuncDef is not ephermal, then do nothing.
4679646749
*/
4679746750
static void freeEphemeralFunction(sqlite3 *db, FuncDef *pDef){
46798
- if( pDef && (pDef->flags & SQLITE_FUNC_EPHEM)!=0 ){
46751
+ if( ALWAYS(pDef) && (pDef->flags & SQLITE_FUNC_EPHEM)!=0 ){
4679946752
sqlite3DbFree(db, pDef);
4680046753
}
4680146754
}
4680246755
4680346756
/*
@@ -46838,11 +46791,11 @@
4683846791
4683946792
/*
4684046793
** Change N opcodes starting at addr to No-ops.
4684146794
*/
4684246795
SQLITE_PRIVATE void sqlite3VdbeChangeToNoop(Vdbe *p, int addr, int N){
46843
- if( p && p->aOp ){
46796
+ if( p->aOp ){
4684446797
VdbeOp *pOp = &p->aOp[addr];
4684546798
sqlite3 *db = p->db;
4684646799
while( N-- ){
4684746800
freeP4(db, pOp->p4type, pOp->p4.p);
4684846801
memset(pOp, 0, sizeof(pOp[0]));
@@ -46887,14 +46840,14 @@
4688746840
if (n != P4_KEYINFO) {
4688846841
freeP4(db, n, (void*)*(char**)&zP4);
4688946842
}
4689046843
return;
4689146844
}
46845
+ assert( p->nOp>0 );
4689246846
assert( addr<p->nOp );
4689346847
if( addr<0 ){
4689446848
addr = p->nOp - 1;
46895
- if( addr<0 ) return;
4689646849
}
4689746850
pOp = &p->aOp[addr];
4689846851
freeP4(db, pOp->p4type, pOp->p4.p);
4689946852
pOp->p4.p = 0;
4690046853
if( n==P4_INT32 ){
@@ -47498,13 +47451,13 @@
4749847451
}
4749947452
zCsr = p->pFree;
4750047453
zEnd = &zCsr[nByte];
4750147454
}while( nByte && !db->mallocFailed );
4750247455
47503
- p->nCursor = nCursor;
47456
+ p->nCursor = (u16)nCursor;
4750447457
if( p->aVar ){
47505
- p->nVar = nVar;
47458
+ p->nVar = (u16)nVar;
4750647459
for(n=0; n<nVar; n++){
4750747460
p->aVar[n].flags = MEM_Null;
4750847461
p->aVar[n].db = db;
4750947462
}
4751047463
}
@@ -47632,11 +47585,11 @@
4763247585
sqlite3 *db = p->db;
4763347586
4763447587
releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
4763547588
sqlite3DbFree(db, p->aColName);
4763647589
n = nResColumn*COLNAME_N;
47637
- p->nResColumn = nResColumn;
47590
+ p->nResColumn = (u16)nResColumn;
4763847591
p->aColName = pColName = (Mem*)sqlite3DbMallocZero(db, sizeof(Mem)*n );
4763947592
if( p->aColName==0 ) return;
4764047593
while( n-- > 0 ){
4764147594
pColName->flags = MEM_Null;
4764247595
pColName->db = p->db;
@@ -47685,10 +47638,17 @@
4768547638
static int vdbeCommit(sqlite3 *db, Vdbe *p){
4768647639
int i;
4768747640
int nTrans = 0; /* Number of databases with an active write-transaction */
4768847641
int rc = SQLITE_OK;
4768947642
int needXcommit = 0;
47643
+
47644
+#ifdef SQLITE_OMIT_VIRTUALTABLE
47645
+ /* With this option, sqlite3VtabSync() is defined to be simply
47646
+ ** SQLITE_OK so p is not used.
47647
+ */
47648
+ UNUSED_PARAMETER(p);
47649
+#endif
4769047650
4769147651
/* Before doing anything else, call the xSync() callback for any
4769247652
** virtual module tables written in this transaction. This has to
4769347653
** be done before determining whether a master journal file is
4769447654
** required, as an xSync() callback may add an attached database
@@ -48792,26 +48752,25 @@
4879248752
p->nField = u;
4879348753
return (void*)p;
4879448754
}
4879548755
4879648756
/*
48797
-** This routine destroys a UnpackedRecord object
48757
+** This routine destroys a UnpackedRecord object.
4879848758
*/
4879948759
SQLITE_PRIVATE void sqlite3VdbeDeleteUnpackedRecord(UnpackedRecord *p){
48800
- if( p ){
48801
- if( p->flags & UNPACKED_NEED_DESTROY ){
48802
- int i;
48803
- Mem *pMem;
48804
- for(i=0, pMem=p->aMem; i<p->nField; i++, pMem++){
48805
- if( pMem->zMalloc ){
48806
- sqlite3VdbeMemRelease(pMem);
48807
- }
48808
- }
48809
- }
48810
- if( p->flags & UNPACKED_NEED_FREE ){
48811
- sqlite3DbFree(p->pKeyInfo->db, p);
48812
- }
48760
+ int i;
48761
+ Mem *pMem;
48762
+
48763
+ assert( p!=0 );
48764
+ assert( p->flags & UNPACKED_NEED_DESTROY );
48765
+ for(i=0, pMem=p->aMem; i<p->nField; i++, pMem++){
48766
+ if( pMem->zMalloc ){
48767
+ sqlite3VdbeMemRelease(pMem);
48768
+ }
48769
+ }
48770
+ if( p->flags & UNPACKED_NEED_FREE ){
48771
+ sqlite3DbFree(p->pKeyInfo->db, p);
4881348772
}
4881448773
}
4881548774
4881648775
/*
4881748776
** This function compares the two table rows or index records
@@ -48940,15 +48899,15 @@
4894048899
u32 typeRowid; /* Serial type of the rowid */
4894148900
u32 lenRowid; /* Size of the rowid */
4894248901
Mem m, v;
4894348902
4894448903
/* Get the size of the index entry. Only indices entries of less
48945
- ** than 2GiB are support - anything large must be database corruption */
48904
+ ** than 2GiB are support - anything large must be database corruption.
48905
+ ** Any corruption is detected in sqlite3BtreeParseCellPtr(), though, so
48906
+ ** this code can safely assume that nCellKey is 32-bits */
4894648907
sqlite3BtreeKeySize(pCur, &nCellKey);
48947
- if( unlikely(nCellKey<=0 || nCellKey>0x7fffffff) ){
48948
- return SQLITE_CORRUPT_BKPT;
48949
- }
48908
+ assert( (nCellKey & SQLITE_MAX_U32)==(u64)nCellKey );
4895048909
4895148910
/* Read in the complete content of the index entry */
4895248911
m.flags = 0;
4895348912
m.db = db;
4895448913
m.zMalloc = 0;
@@ -48957,13 +48916,13 @@
4895748916
return rc;
4895848917
}
4895948918
4896048919
/* The index entry must begin with a header size */
4896148920
(void)getVarint32((u8*)m.z, szHdr);
48962
- testcase( szHdr==2 );
48921
+ testcase( szHdr==3 );
4896348922
testcase( szHdr==m.n );
48964
- if( unlikely(szHdr<2 || (int)szHdr>m.n) ){
48923
+ if( unlikely(szHdr<3 || (int)szHdr>m.n) ){
4896548924
goto idx_rowid_corruption;
4896648925
}
4896748926
4896848927
/* The last field of the index should be an integer - the ROWID.
4896948928
** Verify that the last entry really is an integer. */
@@ -49098,11 +49057,11 @@
4909849057
*************************************************************************
4909949058
**
4910049059
** This file contains code use to implement APIs that are part of the
4910149060
** VDBE.
4910249061
**
49103
-** $Id: vdbeapi.c,v 1.166 2009/06/19 14:06:03 drh Exp $
49062
+** $Id: vdbeapi.c,v 1.167 2009/06/25 01:47:12 drh Exp $
4910449063
*/
4910549064
4910649065
#ifndef SQLITE_OMIT_DEPRECATED
4910749066
/*
4910849067
** Return TRUE (non-zero) of the statement supplied as an argument needs
@@ -49238,20 +49197,35 @@
4923849197
}
4923949198
4924049199
/**************************** sqlite3_result_ *******************************
4924149200
** The following routines are used by user-defined functions to specify
4924249201
** the function result.
49202
+**
49203
+** The setStrOrError() funtion calls sqlite3VdbeMemSetStr() to store the
49204
+** result as a string or blob but if the string or blob is too large, it
49205
+** then sets the error code to SQLITE_TOOBIG
4924349206
*/
49207
+static void setResultStrOrError(
49208
+ sqlite3_context *pCtx, /* Function context */
49209
+ const char *z, /* String pointer */
49210
+ int n, /* Bytes in string, or negative */
49211
+ u8 enc, /* Encoding of z. 0 for BLOBs */
49212
+ void (*xDel)(void*) /* Destructor function */
49213
+){
49214
+ if( sqlite3VdbeMemSetStr(&pCtx->s, z, n, enc, xDel)==SQLITE_TOOBIG ){
49215
+ sqlite3_result_error_toobig(pCtx);
49216
+ }
49217
+}
4924449218
SQLITE_API void sqlite3_result_blob(
4924549219
sqlite3_context *pCtx,
4924649220
const void *z,
4924749221
int n,
4924849222
void (*xDel)(void *)
4924949223
){
4925049224
assert( n>=0 );
4925149225
assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
49252
- sqlite3VdbeMemSetStr(&pCtx->s, z, n, 0, xDel);
49226
+ setResultStrOrError(pCtx, z, n, 0, xDel);
4925349227
}
4925449228
SQLITE_API void sqlite3_result_double(sqlite3_context *pCtx, double rVal){
4925549229
assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
4925649230
sqlite3VdbeMemSetDouble(&pCtx->s, rVal);
4925749231
}
@@ -49284,39 +49258,39 @@
4928449258
const char *z,
4928549259
int n,
4928649260
void (*xDel)(void *)
4928749261
){
4928849262
assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
49289
- sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF8, xDel);
49263
+ setResultStrOrError(pCtx, z, n, SQLITE_UTF8, xDel);
4929049264
}
4929149265
#ifndef SQLITE_OMIT_UTF16
4929249266
SQLITE_API void sqlite3_result_text16(
4929349267
sqlite3_context *pCtx,
4929449268
const void *z,
4929549269
int n,
4929649270
void (*xDel)(void *)
4929749271
){
4929849272
assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
49299
- sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16NATIVE, xDel);
49273
+ setResultStrOrError(pCtx, z, n, SQLITE_UTF16NATIVE, xDel);
4930049274
}
4930149275
SQLITE_API void sqlite3_result_text16be(
4930249276
sqlite3_context *pCtx,
4930349277
const void *z,
4930449278
int n,
4930549279
void (*xDel)(void *)
4930649280
){
4930749281
assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
49308
- sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16BE, xDel);
49282
+ setResultStrOrError(pCtx, z, n, SQLITE_UTF16BE, xDel);
4930949283
}
4931049284
SQLITE_API void sqlite3_result_text16le(
4931149285
sqlite3_context *pCtx,
4931249286
const void *z,
4931349287
int n,
4931449288
void (*xDel)(void *)
4931549289
){
4931649290
assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
49317
- sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16LE, xDel);
49291
+ setResultStrOrError(pCtx, z, n, SQLITE_UTF16LE, xDel);
4931849292
}
4931949293
#endif /* SQLITE_OMIT_UTF16 */
4932049294
SQLITE_API void sqlite3_result_value(sqlite3_context *pCtx, sqlite3_value *pValue){
4932149295
assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
4932249296
sqlite3VdbeMemCopy(&pCtx->s, pValue);
@@ -50319,11 +50293,11 @@
5031950293
** documentation, headers files, or other derived files. The formatting
5032050294
** of the code in this file is, therefore, important. See other comments
5032150295
** in this file for details. If in doubt, do not deviate from existing
5032250296
** commenting and indentation practices when changing or adding code.
5032350297
**
50324
-** $Id: vdbe.c,v 1.862 2009/06/23 14:15:04 drh Exp $
50298
+** $Id: vdbe.c,v 1.866 2009/06/26 16:32:13 shane Exp $
5032550299
*/
5032650300
5032750301
/*
5032850302
** The following global variable is incremented every time a cursor
5032950303
** moves, either by the OP_SeekXX, OP_Next, or OP_Prev opcodes. The test
@@ -51615,11 +51589,11 @@
5161551589
** is the same as executing Halt.
5161651590
*/
5161751591
case OP_Halt: {
5161851592
p->rc = pOp->p1;
5161951593
p->pc = pc;
51620
- p->errorAction = pOp->p2;
51594
+ p->errorAction = (u8)pOp->p2;
5162151595
if( pOp->p4.z ){
5162251596
sqlite3SetString(&p->zErrMsg, db, "%s", pOp->p4.z);
5162351597
}
5162451598
rc = sqlite3VdbeHalt(p);
5162551599
assert( rc==SQLITE_BUSY || rc==SQLITE_OK );
@@ -52816,10 +52790,11 @@
5281652790
memset(&u.am.sMem, 0, sizeof(u.am.sMem));
5281752791
assert( u.am.p1<p->nCursor );
5281852792
assert( pOp->p3>0 && pOp->p3<=p->nMem );
5281952793
u.am.pDest = &p->aMem[pOp->p3];
5282052794
MemSetTypeFlag(u.am.pDest, MEM_Null);
52795
+ u.am.zRec = 0;
5282152796
5282252797
/* This block sets the variable u.am.payloadSize to be the total number of
5282352798
** bytes in the record.
5282452799
**
5282552800
** u.am.zRec is set to be the complete text of the record if it is available.
@@ -52834,16 +52809,15 @@
5283452809
u.am.pC = p->apCsr[u.am.p1];
5283552810
assert( u.am.pC!=0 );
5283652811
#ifndef SQLITE_OMIT_VIRTUALTABLE
5283752812
assert( u.am.pC->pVtabCursor==0 );
5283852813
#endif
52839
- if( u.am.pC->pCursor!=0 ){
52814
+ u.am.pCrsr = u.am.pC->pCursor;
52815
+ if( u.am.pCrsr!=0 ){
5284052816
/* The record is stored in a B-Tree */
5284152817
rc = sqlite3VdbeCursorMoveto(u.am.pC);
5284252818
if( rc ) goto abort_due_to_error;
52843
- u.am.zRec = 0;
52844
- u.am.pCrsr = u.am.pC->pCursor;
5284552819
if( u.am.pC->nullRow ){
5284652820
u.am.payloadSize = 0;
5284752821
}else if( u.am.pC->cacheStatus==p->cacheCtr ){
5284852822
u.am.payloadSize = u.am.pC->payloadSize;
5284952823
u.am.zRec = (char*)u.am.pC->aRow;
@@ -52855,19 +52829,16 @@
5285552829
assert( (u.am.payloadSize64 & SQLITE_MAX_U32)==(u64)u.am.payloadSize64 );
5285652830
u.am.payloadSize = (u32)u.am.payloadSize64;
5285752831
}else{
5285852832
sqlite3BtreeDataSize(u.am.pCrsr, &u.am.payloadSize);
5285952833
}
52860
- u.am.nField = u.am.pC->nField;
5286152834
}else if( u.am.pC->pseudoTable ){
5286252835
/* The record is the sole entry of a pseudo-table */
5286352836
u.am.payloadSize = u.am.pC->nData;
5286452837
u.am.zRec = u.am.pC->pData;
5286552838
u.am.pC->cacheStatus = CACHE_STALE;
5286652839
assert( u.am.payloadSize==0 || u.am.zRec!=0 );
52867
- u.am.nField = u.am.pC->nField;
52868
- u.am.pCrsr = 0;
5286952840
}else{
5287052841
/* Consider the row to be NULL */
5287152842
u.am.payloadSize = 0;
5287252843
}
5287352844
@@ -52879,10 +52850,11 @@
5287952850
assert( db->aLimit[SQLITE_LIMIT_LENGTH]>=0 );
5288052851
if( u.am.payloadSize > (u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
5288152852
goto too_big;
5288252853
}
5288352854
52855
+ u.am.nField = u.am.pC->nField;
5288452856
assert( u.am.p2<u.am.nField );
5288552857
5288652858
/* Read and parse the table header. Store the results of the parse
5288752859
** into the record header cache fields of the cursor.
5288852860
*/
@@ -54478,11 +54450,11 @@
5447854450
sqlite3BtreeSetCachedRowid(u.bf.pC->pCursor, u.bf.v<MAX_ROWID ? u.bf.v+1 : 0);
5447954451
}
5448054452
if( u.bf.pC->useRandomRowid ){
5448154453
assert( pOp->p3==0 ); /* We cannot be in random rowid mode if this is
5448254454
** an AUTOINCREMENT table. */
54483
- u.bf.v = db->priorNewRowid;
54455
+ u.bf.v = db->lastRowid;
5448454456
u.bf.cnt = 0;
5448554457
do{
5448654458
if( u.bf.cnt==0 && (u.bf.v&0xffffff)==u.bf.v ){
5448754459
u.bf.v++;
5448854460
}else{
@@ -54490,11 +54462,10 @@
5449054462
if( u.bf.cnt<5 ) u.bf.v &= 0xffffff;
5449154463
}
5449254464
rc = sqlite3BtreeMovetoUnpacked(u.bf.pC->pCursor, 0, (u64)u.bf.v, 0, &u.bf.res);
5449354465
u.bf.cnt++;
5449454466
}while( u.bf.cnt<100 && rc==SQLITE_OK && u.bf.res==0 );
54495
- db->priorNewRowid = u.bf.v;
5449654467
if( rc==SQLITE_OK && u.bf.res==0 ){
5449754468
rc = SQLITE_FULL;
5449854469
goto abort_due_to_error;
5449954470
}
5450054471
}
@@ -55359,11 +55330,11 @@
5535955330
** can result in a "no such table: sqlite_master" or "malformed
5536055331
** database schema" error being returned to the user.
5536155332
*/
5536255333
assert( sqlite3BtreeHoldsMutex(db->aDb[u.bv.iDb].pBt) );
5536355334
sqlite3BtreeEnterAll(db);
55364
- if( pOp->p2 || ALWAYS(DbHasProperty(db, u.bv.iDb, DB_SchemaLoaded)) ){
55335
+ if( pOp->p2 || DbHasProperty(db, u.bv.iDb, DB_SchemaLoaded) ){
5536555336
u.bv.zMaster = SCHEMA_TABLE(u.bv.iDb);
5536655337
u.bv.initData.db = db;
5536755338
u.bv.initData.iDb = pOp->p1;
5536855339
u.bv.initData.pzErrMsg = &p->zErrMsg;
5536955340
u.bv.zSql = sqlite3MPrintf(db,
@@ -55801,11 +55772,11 @@
5580155772
#endif /* local variables moved into u.cd */
5580255773
assert( pOp->p1>0 && pOp->p1<=p->nMem );
5580355774
u.cd.pMem = &p->aMem[pOp->p1];
5580455775
assert( (u.cd.pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
5580555776
rc = sqlite3VdbeMemFinalize(u.cd.pMem, pOp->p4.pFunc);
55806
- if( rc==SQLITE_ERROR ){
55777
+ if( rc ){
5580755778
sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(u.cd.pMem));
5580855779
}
5580955780
sqlite3VdbeChangeEncoding(u.cd.pMem, encoding);
5581055781
UPDATE_MAX_BLOBSIZE(u.cd.pMem);
5581155782
if( sqlite3VdbeMemTooBig(u.cd.pMem) ){
@@ -56122,10 +56093,13 @@
5612256093
if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
5612356094
rc = u.cj.pModule->xColumn(pCur->pVtabCursor, &u.cj.sContext, pOp->p2);
5612456095
sqlite3DbFree(db, p->zErrMsg);
5612556096
p->zErrMsg = u.cj.pVtab->zErrMsg;
5612656097
u.cj.pVtab->zErrMsg = 0;
56098
+ if( u.cj.sContext.isError ){
56099
+ rc = u.cj.sContext.isError;
56100
+ }
5612756101
5612856102
/* Copy the result of the function to the P3 register. We
5612956103
** do this regardless of whether or not an error occurred to ensure any
5613056104
** dynamic allocation in u.cj.sContext.s (a Mem struct) is released.
5613156105
*/
@@ -64024,11 +63998,11 @@
6402463998
** creating ID lists
6402563999
** BEGIN TRANSACTION
6402664000
** COMMIT
6402764001
** ROLLBACK
6402864002
**
64029
-** $Id: build.c,v 1.552 2009/06/18 17:22:39 drh Exp $
64003
+** $Id: build.c,v 1.554 2009/06/25 11:50:21 drh Exp $
6403064004
*/
6403164005
6403264006
/*
6403364007
** This routine is called when a new SQL statement is beginning to
6403464008
** be parsed. Initialize the pParse structure as needed.
@@ -64175,10 +64149,16 @@
6417564149
/* Once all the cookies have been verified and transactions opened,
6417664150
** obtain the required table-locks. This is a no-op unless the
6417764151
** shared-cache feature is enabled.
6417864152
*/
6417964153
codeTableLocks(pParse);
64154
+
64155
+ /* Initialize any AUTOINCREMENT data structures required.
64156
+ */
64157
+ sqlite3AutoincrementBegin(pParse);
64158
+
64159
+ /* Finally, jump back to the beginning of the executable code. */
6418064160
sqlite3VdbeAddOp2(v, OP_Goto, 0, pParse->cookieGoto);
6418164161
}
6418264162
}
6418364163
6418464164
@@ -64373,14 +64353,11 @@
6437364353
int len;
6437464354
Hash *pHash = &db->aDb[iDb].pSchema->idxHash;
6437564355
6437664356
len = sqlite3Strlen30(zIdxName);
6437764357
pIndex = sqlite3HashInsert(pHash, zIdxName, len, 0);
64378
- /* Justification of ALWAYS(): This routine is only called from the
64379
- ** OP_DropIndex opcode. And there is no way that opcode will ever run
64380
- ** unless the corresponding index is in the symbol table. */
64381
- if( ALWAYS(pIndex) ){
64358
+ if( pIndex ){
6438264359
if( pIndex->pTable->pIndex==pIndex ){
6438364360
pIndex->pTable->pIndex = pIndex->pNext;
6438464361
}else{
6438564362
Index *p;
6438664363
/* Justification of ALWAYS(); The index must be on the list of
@@ -68090,11 +68067,11 @@
6809068067
**
6809168068
*************************************************************************
6809268069
** This file contains C code routines that are called by the parser
6809368070
** in order to generate code for DELETE FROM statements.
6809468071
**
68095
-** $Id: delete.c,v 1.203 2009/05/28 01:00:55 drh Exp $
68072
+** $Id: delete.c,v 1.204 2009/06/23 20:28:54 drh Exp $
6809668073
*/
6809768074
6809868075
/*
6809968076
** Look up every table that is named in pSrc. If any table is not found,
6810068077
** add an error message to pParse->zErrMsg and return NULL. If all tables
@@ -68551,10 +68528,18 @@
6855168528
sqlite3VdbeAddOp2(v, OP_Close, iCur + i, pIdx->tnum);
6855268529
}
6855368530
sqlite3VdbeAddOp1(v, OP_Close, iCur);
6855468531
}
6855568532
}
68533
+
68534
+ /* Update the sqlite_sequence table by storing the content of the
68535
+ ** maximum rowid counter values recorded while inserting into
68536
+ ** autoincrement tables.
68537
+ */
68538
+ if( pParse->nested==0 && pParse->trigStack==0 ){
68539
+ sqlite3AutoincrementEnd(pParse);
68540
+ }
6855668541
6855768542
/*
6855868543
** Return the number of rows that were deleted. If this routine is
6855968544
** generating code because of a call to sqlite3NestedParse(), do not
6856068545
** invoke the callback function.
@@ -70184,11 +70169,11 @@
7018470169
**
7018570170
*************************************************************************
7018670171
** This file contains C code routines that are called by the parser
7018770172
** to handle INSERT statements in SQLite.
7018870173
**
70189
-** $Id: insert.c,v 1.268 2009/05/29 19:00:13 drh Exp $
70174
+** $Id: insert.c,v 1.269 2009/06/23 20:28:54 drh Exp $
7019070175
*/
7019170176
7019270177
/*
7019370178
** Generate code that will open a table for reading.
7019470179
*/
@@ -70333,58 +70318,87 @@
7033370318
return 0;
7033470319
}
7033570320
7033670321
#ifndef SQLITE_OMIT_AUTOINCREMENT
7033770322
/*
70338
-** Write out code to initialize the autoincrement logic. This code
70339
-** looks up the current autoincrement value in the sqlite_sequence
70340
-** table and stores that value in a register. Code generated by
70341
-** autoIncStep() will keep that register holding the largest
70342
-** rowid value. Code generated by autoIncEnd() will write the new
70343
-** largest value of the counter back into the sqlite_sequence table.
70344
-**
70345
-** This routine returns the index of the mem[] cell that contains
70346
-** the maximum rowid counter.
70347
-**
70348
-** Three consecutive registers are allocated by this routine. The
70349
-** first two hold the name of the target table and the maximum rowid
70350
-** inserted into the target table, respectively.
70351
-** The third holds the rowid in sqlite_sequence where we will
70352
-** write back the revised maximum rowid. This routine returns the
70353
-** index of the second of these three registers.
70323
+** Locate or create an AutoincInfo structure associated with table pTab
70324
+** which is in database iDb. Return the register number for the register
70325
+** that holds the maximum rowid.
70326
+**
70327
+** There is at most one AutoincInfo structure per table even if the
70328
+** same table is autoincremented multiple times due to inserts within
70329
+** triggers. A new AutoincInfo structure is created if this is the
70330
+** first use of table pTab. On 2nd and subsequent uses, the original
70331
+** AutoincInfo structure is used.
70332
+**
70333
+** Three memory locations are allocated:
70334
+**
70335
+** (1) Register to hold the name of the pTab table.
70336
+** (2) Register to hold the maximum ROWID of pTab.
70337
+** (3) Register to hold the rowid in sqlite_sequence of pTab
70338
+**
70339
+** The 2nd register is the one that is returned. That is all the
70340
+** insert routine needs to know about.
7035470341
*/
7035570342
static int autoIncBegin(
7035670343
Parse *pParse, /* Parsing context */
7035770344
int iDb, /* Index of the database holding pTab */
7035870345
Table *pTab /* The table we are writing to */
7035970346
){
7036070347
int memId = 0; /* Register holding maximum rowid */
7036170348
if( pTab->tabFlags & TF_Autoincrement ){
70362
- Vdbe *v = pParse->pVdbe;
70363
- Db *pDb = &pParse->db->aDb[iDb];
70364
- int iCur = pParse->nTab++;
70365
- int addr; /* Address of the top of the loop */
70366
- assert( v );
70367
- pParse->nMem++; /* Holds name of table */
70368
- memId = ++pParse->nMem;
70369
- pParse->nMem++;
70370
- sqlite3OpenTable(pParse, iCur, iDb, pDb->pSchema->pSeqTab, OP_OpenRead);
70371
- addr = sqlite3VdbeCurrentAddr(v);
70372
- sqlite3VdbeAddOp4(v, OP_String8, 0, memId-1, 0, pTab->zName, 0);
70373
- sqlite3VdbeAddOp2(v, OP_Rewind, iCur, addr+9);
70374
- sqlite3VdbeAddOp3(v, OP_Column, iCur, 0, memId);
70375
- sqlite3VdbeAddOp3(v, OP_Ne, memId-1, addr+7, memId);
70376
- sqlite3VdbeChangeP5(v, SQLITE_JUMPIFNULL);
70377
- sqlite3VdbeAddOp2(v, OP_Rowid, iCur, memId+1);
70378
- sqlite3VdbeAddOp3(v, OP_Column, iCur, 1, memId);
70379
- sqlite3VdbeAddOp2(v, OP_Goto, 0, addr+9);
70380
- sqlite3VdbeAddOp2(v, OP_Next, iCur, addr+2);
70381
- sqlite3VdbeAddOp2(v, OP_Integer, 0, memId);
70382
- sqlite3VdbeAddOp2(v, OP_Close, iCur, 0);
70349
+ AutoincInfo *pInfo;
70350
+
70351
+ pInfo = pParse->pAinc;
70352
+ while( pInfo && pInfo->pTab!=pTab ){ pInfo = pInfo->pNext; }
70353
+ if( pInfo==0 ){
70354
+ pInfo = sqlite3DbMallocRaw(pParse->db, sizeof(*pInfo));
70355
+ if( pInfo==0 ) return 0;
70356
+ pInfo->pNext = pParse->pAinc;
70357
+ pParse->pAinc = pInfo;
70358
+ pInfo->pTab = pTab;
70359
+ pInfo->iDb = iDb;
70360
+ pParse->nMem++; /* Register to hold name of table */
70361
+ pInfo->regCtr = ++pParse->nMem; /* Max rowid register */
70362
+ pParse->nMem++; /* Rowid in sqlite_sequence */
70363
+ }
70364
+ memId = pInfo->regCtr;
7038370365
}
7038470366
return memId;
7038570367
}
70368
+
70369
+/*
70370
+** This routine generates code that will initialize all of the
70371
+** register used by the autoincrement tracker.
70372
+*/
70373
+SQLITE_PRIVATE void sqlite3AutoincrementBegin(Parse *pParse){
70374
+ AutoincInfo *p; /* Information about an AUTOINCREMENT */
70375
+ sqlite3 *db = pParse->db; /* The database connection */
70376
+ Db *pDb; /* Database only autoinc table */
70377
+ int memId; /* Register holding max rowid */
70378
+ int addr; /* A VDBE address */
70379
+ Vdbe *v = pParse->pVdbe; /* VDBE under construction */
70380
+
70381
+ assert( v ); /* We failed long ago if this is not so */
70382
+ for(p = pParse->pAinc; p; p = p->pNext){
70383
+ pDb = &db->aDb[p->iDb];
70384
+ memId = p->regCtr;
70385
+ sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenRead);
70386
+ addr = sqlite3VdbeCurrentAddr(v);
70387
+ sqlite3VdbeAddOp4(v, OP_String8, 0, memId-1, 0, p->pTab->zName, 0);
70388
+ sqlite3VdbeAddOp2(v, OP_Rewind, 0, addr+9);
70389
+ sqlite3VdbeAddOp3(v, OP_Column, 0, 0, memId);
70390
+ sqlite3VdbeAddOp3(v, OP_Ne, memId-1, addr+7, memId);
70391
+ sqlite3VdbeChangeP5(v, SQLITE_JUMPIFNULL);
70392
+ sqlite3VdbeAddOp2(v, OP_Rowid, 0, memId+1);
70393
+ sqlite3VdbeAddOp3(v, OP_Column, 0, 1, memId);
70394
+ sqlite3VdbeAddOp2(v, OP_Goto, 0, addr+9);
70395
+ sqlite3VdbeAddOp2(v, OP_Next, 0, addr+2);
70396
+ sqlite3VdbeAddOp2(v, OP_Integer, 0, memId);
70397
+ sqlite3VdbeAddOp0(v, OP_Close);
70398
+ }
70399
+}
7038670400
7038770401
/*
7038870402
** Update the maximum rowid for an autoincrement calculation.
7038970403
**
7039070404
** This routine should be called when the top of the stack holds a
@@ -70397,46 +70411,56 @@
7039770411
sqlite3VdbeAddOp2(pParse->pVdbe, OP_MemMax, memId, regRowid);
7039870412
}
7039970413
}
7040070414
7040170415
/*
70402
-** After doing one or more inserts, the maximum rowid is stored
70403
-** in reg[memId]. Generate code to write this value back into the
70404
-** the sqlite_sequence table.
70416
+** This routine generates the code needed to write autoincrement
70417
+** maximum rowid values back into the sqlite_sequence register.
70418
+** Every statement that might do an INSERT into an autoincrement
70419
+** table (either directly or through triggers) needs to call this
70420
+** routine just before the "exit" code.
7040570421
*/
70406
-static void autoIncEnd(
70407
- Parse *pParse, /* The parsing context */
70408
- int iDb, /* Index of the database holding pTab */
70409
- Table *pTab, /* Table we are inserting into */
70410
- int memId /* Memory cell holding the maximum rowid */
70411
-){
70412
- if( pTab->tabFlags & TF_Autoincrement ){
70413
- int iCur = pParse->nTab++;
70414
- Vdbe *v = pParse->pVdbe;
70415
- Db *pDb = &pParse->db->aDb[iDb];
70416
- int j1;
70417
- int iRec = ++pParse->nMem; /* Memory cell used for record */
70418
-
70419
- assert( v );
70420
- sqlite3OpenTable(pParse, iCur, iDb, pDb->pSchema->pSeqTab, OP_OpenWrite);
70422
+SQLITE_PRIVATE void sqlite3AutoincrementEnd(Parse *pParse){
70423
+ AutoincInfo *p;
70424
+ Vdbe *v = pParse->pVdbe;
70425
+ sqlite3 *db = pParse->db;
70426
+
70427
+ assert( v );
70428
+ for(p = pParse->pAinc; p; p = p->pNext){
70429
+ Db *pDb = &db->aDb[p->iDb];
70430
+ int j1, j2, j3, j4, j5;
70431
+ int iRec;
70432
+ int memId = p->regCtr;
70433
+
70434
+ iRec = sqlite3GetTempReg(pParse);
70435
+ sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenWrite);
7042170436
j1 = sqlite3VdbeAddOp1(v, OP_NotNull, memId+1);
70422
- sqlite3VdbeAddOp2(v, OP_NewRowid, iCur, memId+1);
70437
+ j2 = sqlite3VdbeAddOp0(v, OP_Rewind);
70438
+ j3 = sqlite3VdbeAddOp3(v, OP_Column, 0, 0, iRec);
70439
+ j4 = sqlite3VdbeAddOp3(v, OP_Eq, memId-1, 0, iRec);
70440
+ sqlite3VdbeAddOp2(v, OP_Next, 0, j3);
70441
+ sqlite3VdbeJumpHere(v, j2);
70442
+ sqlite3VdbeAddOp2(v, OP_NewRowid, 0, memId+1);
70443
+ j5 = sqlite3VdbeAddOp0(v, OP_Goto);
70444
+ sqlite3VdbeJumpHere(v, j4);
70445
+ sqlite3VdbeAddOp2(v, OP_Rowid, 0, memId+1);
7042370446
sqlite3VdbeJumpHere(v, j1);
70447
+ sqlite3VdbeJumpHere(v, j5);
7042470448
sqlite3VdbeAddOp3(v, OP_MakeRecord, memId-1, 2, iRec);
70425
- sqlite3VdbeAddOp3(v, OP_Insert, iCur, iRec, memId+1);
70449
+ sqlite3VdbeAddOp3(v, OP_Insert, 0, iRec, memId+1);
7042670450
sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
70427
- sqlite3VdbeAddOp1(v, OP_Close, iCur);
70451
+ sqlite3VdbeAddOp0(v, OP_Close);
70452
+ sqlite3ReleaseTempReg(pParse, iRec);
7042870453
}
7042970454
}
7043070455
#else
7043170456
/*
7043270457
** If SQLITE_OMIT_AUTOINCREMENT is defined, then the three routines
7043370458
** above are all no-ops
7043470459
*/
7043570460
# define autoIncBegin(A,B,C) (0)
7043670461
# define autoIncStep(A,B,C)
70437
-# define autoIncEnd(A,B,C,D)
7043870462
#endif /* SQLITE_OMIT_AUTOINCREMENT */
7043970463
7044070464
7044170465
/* Forward declaration */
7044270466
static int xferOptimization(
@@ -70678,11 +70702,11 @@
7067870702
** This is the 2nd template.
7067970703
*/
7068070704
if( pColumn==0 && xferOptimization(pParse, pTab, pSelect, onError, iDb) ){
7068170705
assert( !pTrigger );
7068270706
assert( pList==0 );
70683
- goto insert_cleanup;
70707
+ goto insert_end;
7068470708
}
7068570709
#endif /* SQLITE_OMIT_XFER_OPT */
7068670710
7068770711
/* If this is an AUTOINCREMENT table, look up the sequence number in the
7068870712
** sqlite_sequence table and store it in memory cell regAutoinc.
@@ -71160,15 +71184,18 @@
7116071184
for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
7116171185
sqlite3VdbeAddOp1(v, OP_Close, idx+baseCur);
7116271186
}
7116371187
}
7116471188
71189
+insert_end:
7116571190
/* Update the sqlite_sequence table by storing the content of the
71166
- ** counter value in memory regAutoinc back into the sqlite_sequence
71167
- ** table.
71191
+ ** maximum rowid counter values recorded while inserting into
71192
+ ** autoincrement tables.
7116871193
*/
71169
- autoIncEnd(pParse, iDb, pTab, regAutoinc);
71194
+ if( pParse->nested==0 && pParse->trigStack==0 ){
71195
+ sqlite3AutoincrementEnd(pParse);
71196
+ }
7117071197
7117171198
/*
7117271199
** Return the number of rows inserted. If this routine is
7117371200
** generating code because of a call to sqlite3NestedParse(), do not
7117471201
** invoke the callback function.
@@ -71887,11 +71914,10 @@
7188771914
sqlite3VdbeAddOp2(v, OP_RowData, iSrc, regData);
7188871915
sqlite3VdbeAddOp3(v, OP_Insert, iDest, regData, regRowid);
7188971916
sqlite3VdbeChangeP5(v, OPFLAG_NCHANGE|OPFLAG_LASTROWID|OPFLAG_APPEND);
7189071917
sqlite3VdbeChangeP4(v, -1, pDest->zName, 0);
7189171918
sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1);
71892
- autoIncEnd(pParse, iDbDest, pDest, regAutoinc);
7189371919
for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
7189471920
for(pSrcIdx=pSrc->pIndex; ALWAYS(pSrcIdx); pSrcIdx=pSrcIdx->pNext){
7189571921
if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break;
7189671922
}
7189771923
assert( pSrcIdx );
@@ -74504,11 +74530,11 @@
7450474530
*************************************************************************
7450574531
** This file contains the implementation of the sqlite3_prepare()
7450674532
** interface, and routines that contribute to loading the database schema
7450774533
** from disk.
7450874534
**
74509
-** $Id: prepare.c,v 1.124 2009/06/22 12:05:10 drh Exp $
74535
+** $Id: prepare.c,v 1.125 2009/06/25 11:50:21 drh Exp $
7451074536
*/
7451174537
7451274538
/*
7451374539
** Fill the InitData structure with an error message that indicates
7451474540
** that the database is corrupt.
@@ -74577,11 +74603,11 @@
7457774603
assert( rc!=SQLITE_OK || zErr==0 );
7457874604
if( SQLITE_OK!=rc ){
7457974605
pData->rc = rc;
7458074606
if( rc==SQLITE_NOMEM ){
7458174607
db->mallocFailed = 1;
74582
- }else if( rc!=SQLITE_INTERRUPT ){
74608
+ }else if( rc!=SQLITE_INTERRUPT && rc!=SQLITE_LOCKED ){
7458374609
corruptSchema(pData, argv[0], zErr);
7458474610
}
7458574611
sqlite3DbFree(db, zErr);
7458674612
}
7458774613
}else if( argv[0]==0 ){
@@ -80648,11 +80674,11 @@
8064880674
**
8064980675
*************************************************************************
8065080676
** This file contains C code routines that are called by the parser
8065180677
** to handle UPDATE statements.
8065280678
**
80653
-** $Id: update.c,v 1.202 2009/05/28 01:00:55 drh Exp $
80679
+** $Id: update.c,v 1.204 2009/06/27 11:17:35 drh Exp $
8065480680
*/
8065580681
8065680682
#ifndef SQLITE_OMIT_VIRTUALTABLE
8065780683
/* Forward declaration */
8065880684
static void updateVirtualTable(
@@ -81198,10 +81224,18 @@
8119881224
sqlite3VdbeAddOp2(v, OP_Close, iCur, 0);
8119981225
if( pTrigger ){
8120081226
sqlite3VdbeAddOp2(v, OP_Close, newIdx, 0);
8120181227
sqlite3VdbeAddOp2(v, OP_Close, oldIdx, 0);
8120281228
}
81229
+
81230
+ /* Update the sqlite_sequence table by storing the content of the
81231
+ ** maximum rowid counter values recorded while inserting into
81232
+ ** autoincrement tables.
81233
+ */
81234
+ if( pParse->nested==0 && pParse->trigStack==0 ){
81235
+ sqlite3AutoincrementEnd(pParse);
81236
+ }
8120381237
8120481238
/*
8120581239
** Return the number of rows that were changed. If this routine is
8120681240
** generating code because of a call to sqlite3NestedParse(), do not
8120781241
** invoke the callback function.
@@ -81296,21 +81330,20 @@
8129681330
sqlite3Select(pParse, pSelect, &dest);
8129781331
8129881332
/* Generate code to scan the ephemeral table and call VUpdate. */
8129981333
iReg = ++pParse->nMem;
8130081334
pParse->nMem += pTab->nCol+1;
81301
- sqlite3VdbeAddOp2(v, OP_Rewind, ephemTab, 0);
81302
- addr = sqlite3VdbeCurrentAddr(v);
81335
+ addr = sqlite3VdbeAddOp2(v, OP_Rewind, ephemTab, 0);
8130381336
sqlite3VdbeAddOp3(v, OP_Column, ephemTab, 0, iReg);
8130481337
sqlite3VdbeAddOp3(v, OP_Column, ephemTab, (pRowid?1:0), iReg+1);
8130581338
for(i=0; i<pTab->nCol; i++){
8130681339
sqlite3VdbeAddOp3(v, OP_Column, ephemTab, i+1+(pRowid!=0), iReg+2+i);
8130781340
}
8130881341
sqlite3VtabMakeWritable(pParse, pTab);
8130981342
sqlite3VdbeAddOp4(v, OP_VUpdate, 0, pTab->nCol+2, iReg, pVtab, P4_VTAB);
81310
- sqlite3VdbeAddOp2(v, OP_Next, ephemTab, addr);
81311
- sqlite3VdbeJumpHere(v, addr-1);
81343
+ sqlite3VdbeAddOp2(v, OP_Next, ephemTab, addr+1);
81344
+ sqlite3VdbeJumpHere(v, addr);
8131281345
sqlite3VdbeAddOp2(v, OP_Close, ephemTab, 0);
8131381346
8131481347
/* Cleanup */
8131581348
sqlite3SelectDelete(db, pSelect);
8131681349
}
@@ -89455,11 +89488,11 @@
8945589488
**
8945689489
** This file contains C code that splits an SQL input string up into
8945789490
** individual tokens and sends those tokens one-by-one over to the
8945889491
** parser for analysis.
8945989492
**
89460
-** $Id: tokenize.c,v 1.161 2009/06/17 01:17:13 drh Exp $
89493
+** $Id: tokenize.c,v 1.162 2009/06/23 20:28:54 drh Exp $
8946189494
*/
8946289495
8946389496
/*
8946489497
** The charMap() macro maps alphabetic characters into their
8946589498
** lower-case ASCII equivalent. On ASCII machines, this is just
@@ -90226,10 +90259,15 @@
9022690259
}
9022790260
9022890261
sqlite3DeleteTrigger(db, pParse->pNewTrigger);
9022990262
sqlite3DbFree(db, pParse->apVarExpr);
9023090263
sqlite3DbFree(db, pParse->aAlias);
90264
+ while( pParse->pAinc ){
90265
+ AutoincInfo *p = pParse->pAinc;
90266
+ pParse->pAinc = p->pNext;
90267
+ sqlite3DbFree(db, p);
90268
+ }
9023190269
while( pParse->pZombieTab ){
9023290270
Table *p = pParse->pZombieTab;
9023390271
pParse->pZombieTab = p->pNextZombie;
9023490272
sqlite3DeleteTable(p);
9023590273
}
@@ -90534,11 +90572,11 @@
9053490572
** Main file for the SQLite library. The routines in this file
9053590573
** implement the programmer interface to the library. Routines in
9053690574
** other files are for internal use by SQLite and should not be
9053790575
** accessed by users of the library.
9053890576
**
90539
-** $Id: main.c,v 1.558 2009/06/19 14:06:03 drh Exp $
90577
+** $Id: main.c,v 1.560 2009/06/26 15:14:55 drh Exp $
9054090578
*/
9054190579
9054290580
#ifdef SQLITE_ENABLE_FTS3
9054390581
/************** Include fts3.h in the middle of main.c ***********************/
9054490582
/************** Begin file fts3.h ********************************************/
@@ -91356,11 +91394,11 @@
9135691394
/* SQLITE_FULL */ "database or disk is full",
9135791395
/* SQLITE_CANTOPEN */ "unable to open database file",
9135891396
/* SQLITE_PROTOCOL */ 0,
9135991397
/* SQLITE_EMPTY */ "table contains no data",
9136091398
/* SQLITE_SCHEMA */ "database schema has changed",
91361
- /* SQLITE_TOOBIG */ "String or BLOB exceeded size limit",
91399
+ /* SQLITE_TOOBIG */ "string or blob too big",
9136291400
/* SQLITE_CONSTRAINT */ "constraint failed",
9136391401
/* SQLITE_MISMATCH */ "datatype mismatch",
9136491402
/* SQLITE_MISUSE */ "library routine called out of sequence",
9136591403
/* SQLITE_NOLFS */ "large file support is disabled",
9136691404
/* SQLITE_AUTH */ "authorization denied",
@@ -92169,11 +92207,10 @@
9216992207
goto opendb_out;
9217092208
}
9217192209
}
9217292210
sqlite3_mutex_enter(db->mutex);
9217392211
db->errMask = 0xff;
92174
- db->priorNewRowid = 0;
9217592212
db->nDb = 2;
9217692213
db->magic = SQLITE_MAGIC_BUSY;
9217792214
db->aDb = db->aDbStatic;
9217892215
9217992216
assert( sizeof(db->aLimit)==sizeof(aHardLimit) );
9218092217
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -1,8 +1,8 @@
1 /******************************************************************************
2 ** This file is an amalgamation of many separate C source files from SQLite
3 ** version 3.6.15. By combining all the individual C code files into this
4 ** single large file, the entire code can be compiled as a one translation
5 ** unit. This allows many compilers to do optimizations that would not be
6 ** possible if the files were compiled separately. Performance improvements
7 ** of 5% are more are commonly seen when SQLite is compiled as a single
8 ** translation unit.
@@ -15,11 +15,11 @@
15 ** needed if you want a wrapper to interface SQLite with your choice of
16 ** programming language. The code for the "sqlite3" command-line shell
17 ** is also in a separate file. This file contains only code for the core
18 ** SQLite library.
19 **
20 ** This amalgamation was generated on 2009-06-23 14:42:37 UTC.
21 */
22 #define SQLITE_CORE 1
23 #define SQLITE_AMALGAMATION 1
24 #ifndef SQLITE_PRIVATE
25 # define SQLITE_PRIVATE static
@@ -39,11 +39,11 @@
39 ** May you share freely, never taking more than you give.
40 **
41 *************************************************************************
42 ** Internal interface definitions for SQLite.
43 **
44 ** @(#) $Id: sqliteInt.h,v 1.886 2009/06/19 14:06:03 drh Exp $
45 */
46 #ifndef _SQLITEINT_H_
47 #define _SQLITEINT_H_
48
49 /*
@@ -489,13 +489,12 @@
489 */
490 #if defined(SQLITE_COVERAGE_TEST)
491 # define ALWAYS(X) (1)
492 # define NEVER(X) (0)
493 #elif !defined(NDEBUG)
494 SQLITE_PRIVATE int sqlite3Assert(void);
495 # define ALWAYS(X) ((X)?1:sqlite3Assert())
496 # define NEVER(X) ((X)?sqlite3Assert():0)
497 #else
498 # define ALWAYS(X) (X)
499 # define NEVER(X) (X)
500 #endif
501
@@ -614,12 +613,12 @@
614 **
615 ** See also: [sqlite3_libversion()] and [sqlite3_libversion_number()].
616 **
617 ** Requirements: [H10011] [H10014]
618 */
619 #define SQLITE_VERSION "3.6.15"
620 #define SQLITE_VERSION_NUMBER 3006015
621
622 /*
623 ** CAPI3REF: Run-Time Library Version Numbers {H10020} <S60100>
624 ** KEYWORDS: sqlite3_version
625 **
@@ -6693,10 +6692,11 @@
6693 /*
6694 ** Forward references to structures
6695 */
6696 typedef struct AggInfo AggInfo;
6697 typedef struct AuthContext AuthContext;
 
6698 typedef struct Bitvec Bitvec;
6699 typedef struct RowSet RowSet;
6700 typedef struct CollSeq CollSeq;
6701 typedef struct Column Column;
6702 typedef struct Db Db;
@@ -8218,11 +8218,10 @@
8218 signed char nextAutovac; /* Autovac setting after VACUUM if >=0 */
8219 int nextPagesize; /* Pagesize after VACUUM if >0 */
8220 int nTable; /* Number of tables in the database */
8221 CollSeq *pDfltColl; /* The default collating sequence (BINARY) */
8222 i64 lastRowid; /* ROWID of most recent insert (see above) */
8223 i64 priorNewRowid; /* Last randomly generated ROWID */
8224 u32 magic; /* Magic number for detect library misuse */
8225 int nChange; /* Value returned by sqlite3_changes() */
8226 int nTotalChange; /* Value returned by sqlite3_total_changes() */
8227 sqlite3_mutex *mutex; /* Connection mutex */
8228 int aLimit[SQLITE_N_LIMIT]; /* Limits */
@@ -9354,10 +9353,26 @@
9354 int iParm; /* A parameter used by the eDest disposal method */
9355 int iMem; /* Base register where results are written */
9356 int nMem; /* Number of registers allocated */
9357 };
9358
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9359 /*
9360 ** Size of the column cache
9361 */
9362 #ifndef SQLITE_N_COLCACHE
9363 # define SQLITE_N_COLCACHE 10
@@ -9420,10 +9435,11 @@
9420 int nTableLock; /* Number of locks in aTableLock */
9421 TableLock *aTableLock; /* Required table locks for shared-cache mode */
9422 #endif
9423 int regRowid; /* Register holding rowid of CREATE TABLE entry */
9424 int regRoot; /* Register holding root page number for new objects */
 
9425
9426 /* Above is constant between recursions. Below is reset before and after
9427 ** each recursion */
9428
9429 int nVar; /* Number of '?' variables seen in the SQL so far */
@@ -9910,10 +9926,17 @@
9910 # define sqlite3ViewGetColumnNames(A,B) 0
9911 #endif
9912
9913 SQLITE_PRIVATE void sqlite3DropTable(Parse*, SrcList*, int, int);
9914 SQLITE_PRIVATE void sqlite3DeleteTable(Table*);
 
 
 
 
 
 
 
9915 SQLITE_PRIVATE void sqlite3Insert(Parse*, SrcList*, ExprList*, Select*, IdList*, int);
9916 SQLITE_PRIVATE void *sqlite3ArrayAllocate(sqlite3*,void*,int,int,int*,int*,int*);
9917 SQLITE_PRIVATE IdList *sqlite3IdListAppend(sqlite3*, IdList*, Token*);
9918 SQLITE_PRIVATE int sqlite3IdListIndex(IdList*,const char*);
9919 SQLITE_PRIVATE SrcList *sqlite3SrcListEnlarge(sqlite3*, SrcList*, int, int);
@@ -15212,11 +15235,11 @@
15212 **
15213 *************************************************************************
15214 **
15215 ** Memory allocation functions used throughout sqlite.
15216 **
15217 ** $Id: malloc.c,v 1.62 2009/05/03 20:23:54 drh Exp $
15218 */
15219
15220 /*
15221 ** This routine runs when the memory allocator sees that the
15222 ** total memory allocation is about to exceed the soft heap
@@ -15464,19 +15487,16 @@
15464 ** Allocate memory. This routine is like sqlite3_malloc() except that it
15465 ** assumes the memory subsystem has already been initialized.
15466 */
15467 SQLITE_PRIVATE void *sqlite3Malloc(int n){
15468 void *p;
15469 if( n<=0 || NEVER(n>=0x7fffff00) ){
15470 /* The NEVER(n>=0x7fffff00) term is added out of paranoia. We want to make
15471 ** absolutely sure that there is nothing within SQLite that can cause a
15472 ** memory allocation of a number of bytes which is near the maximum signed
15473 ** integer value and thus cause an integer overflow inside of the xMalloc()
15474 ** implementation. The n>=0x7fffff00 gives us 255 bytes of headroom. The
15475 ** test should never be true because SQLITE_MAX_LENGTH should be much
15476 ** less than 0x7fffff00 and it should catch large memory allocations
15477 ** before they reach this point. */
15478 p = 0;
15479 }else if( sqlite3GlobalConfig.bMemstat ){
15480 sqlite3_mutex_enter(mem0.mutex);
15481 mallocWithAlarm(n, &p);
15482 sqlite3_mutex_leave(mem0.mutex);
@@ -15674,14 +15694,17 @@
15674 int nOld, nNew;
15675 void *pNew;
15676 if( pOld==0 ){
15677 return sqlite3Malloc(nBytes);
15678 }
15679 if( nBytes<=0 || NEVER(nBytes>=0x7fffff00) ){
15680 /* The NEVER(...) term is explained in comments on sqlite3Malloc() */
15681 sqlite3_free(pOld);
15682 return 0;
 
 
 
 
15683 }
15684 nOld = sqlite3MallocSize(pOld);
15685 if( sqlite3GlobalConfig.bMemstat ){
15686 sqlite3_mutex_enter(mem0.mutex);
15687 sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, nBytes);
@@ -17977,11 +18000,11 @@
17977 ** Utility functions used throughout sqlite.
17978 **
17979 ** This file contains functions for allocating memory, comparing
17980 ** strings, and stuff like that.
17981 **
17982 ** $Id: util.c,v 1.260 2009/06/17 16:20:04 drh Exp $
17983 */
17984 #ifdef SQLITE_HAVE_ISNAN
17985 # include <math.h>
17986 #endif
17987
@@ -17991,31 +18014,10 @@
17991 #ifdef SQLITE_COVERAGE_TEST
17992 SQLITE_PRIVATE void sqlite3Coverage(int x){
17993 static int dummy = 0;
17994 dummy += x;
17995 }
17996 #endif
17997
17998 /*
17999 ** Routine needed to support the ALWAYS() and NEVER() macros.
18000 **
18001 ** The argument to ALWAYS() should always be true and the argument
18002 ** to NEVER() should always be false. If either is not the case
18003 ** then this routine is called in order to throw an error.
18004 **
18005 ** This routine only exists if assert() is operational. It always
18006 ** throws an assert on its first invocation. The variable has a long
18007 ** name to help the assert() message be more readable. The variable
18008 ** is used to prevent a too-clever optimizer from optimizing out the
18009 ** entire call.
18010 */
18011 #ifndef NDEBUG
18012 SQLITE_PRIVATE int sqlite3Assert(void){
18013 static volatile int ALWAYS_was_false_or_NEVER_was_true = 0;
18014 assert( ALWAYS_was_false_or_NEVER_was_true ); /* Always fails */
18015 return ALWAYS_was_false_or_NEVER_was_true++; /* Not Reached */
18016 }
18017 #endif
18018
18019 /*
18020 ** Return true if the floating point value is Not a Number (NaN).
18021 **
@@ -30495,11 +30497,11 @@
30495 ** is separate from the database file. The pager also implements file
30496 ** locking to prevent two processes from writing the same database
30497 ** file simultaneously, or one process from reading the database while
30498 ** another is writing.
30499 **
30500 ** @(#) $Id: pager.c,v 1.601 2009/06/22 05:43:24 danielk1977 Exp $
30501 */
30502 #ifndef SQLITE_OMIT_DISKIO
30503
30504 /*
30505 ** Macros for troubleshooting. Normally turned off
@@ -31233,11 +31235,10 @@
31233 pPager->aSavepoint[ii].iHdrOffset = pPager->journalOff;
31234 }
31235 }
31236
31237 pPager->journalHdr = pPager->journalOff = journalHdrOffset(pPager);
31238 memcpy(zHeader, aJournalMagic, sizeof(aJournalMagic));
31239
31240 /*
31241 ** Write the nRec Field - the number of page records that follow this
31242 ** journal header. Normally, zero is written to this value at this time.
31243 ** After the records are added to the journal (and the journal synced,
@@ -31259,12 +31260,14 @@
31259 */
31260 assert( isOpen(pPager->fd) || pPager->noSync );
31261 if( (pPager->noSync) || (pPager->journalMode==PAGER_JOURNALMODE_MEMORY)
31262 || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND)
31263 ){
 
31264 put32bits(&zHeader[sizeof(aJournalMagic)], 0xffffffff);
31265 }else{
 
31266 put32bits(&zHeader[sizeof(aJournalMagic)], 0);
31267 }
31268
31269 /* The random check-hash initialiser */
31270 sqlite3_randomness(sizeof(pPager->cksumInit), &pPager->cksumInit);
@@ -31328,10 +31331,11 @@
31328 ** returned and *pNRec and *PDbSize are undefined. If JOURNAL_HDR_SZ bytes
31329 ** cannot be read from the journal file an error code is returned.
31330 */
31331 static int readJournalHdr(
31332 Pager *pPager, /* Pager object */
 
31333 i64 journalSize, /* Size of the open journal file in bytes */
31334 u32 *pNRec, /* OUT: Value read from the nRec field */
31335 u32 *pDbSize /* OUT: Value of original database size field */
31336 ){
31337 int rc; /* Return code */
@@ -31353,16 +31357,18 @@
31353 /* Read in the first 8 bytes of the journal header. If they do not match
31354 ** the magic string found at the start of each journal header, return
31355 ** SQLITE_DONE. If an IO error occurs, return an error code. Otherwise,
31356 ** proceed.
31357 */
31358 rc = sqlite3OsRead(pPager->jfd, aMagic, sizeof(aMagic), iHdrOff);
31359 if( rc ){
31360 return rc;
31361 }
31362 if( memcmp(aMagic, aJournalMagic, sizeof(aMagic))!=0 ){
31363 return SQLITE_DONE;
 
 
31364 }
31365
31366 /* Read the first three 32-bit fields of the journal header: The nRec
31367 ** field, the checksum-initializer and the database size at the start
31368 ** of the transaction. Return an error code if anything goes wrong.
@@ -32453,11 +32459,11 @@
32453 /* Read the next journal header from the journal file. If there are
32454 ** not enough bytes left in the journal file for a complete header, or
32455 ** it is corrupted, then a process must of failed while writing it.
32456 ** This indicates nothing more needs to be rolled back.
32457 */
32458 rc = readJournalHdr(pPager, szJ, &nRec, &mxPg);
32459 if( rc!=SQLITE_OK ){
32460 if( rc==SQLITE_DONE ){
32461 rc = SQLITE_OK;
32462 }
32463 goto end_playback;
@@ -32673,11 +32679,11 @@
32673 */
32674 while( rc==SQLITE_OK && pPager->journalOff<szJ ){
32675 u32 ii; /* Loop counter */
32676 u32 nJRec = 0; /* Number of Journal Records */
32677 u32 dummy;
32678 rc = readJournalHdr(pPager, szJ, &nJRec, &dummy);
32679 assert( rc!=SQLITE_DONE );
32680
32681 /*
32682 ** The "pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff"
32683 ** test is related to ticket #2565. See the discussion in the
@@ -33236,16 +33242,10 @@
33236 int rc; /* Return code */
33237 const int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
33238 assert( isOpen(pPager->jfd) );
33239
33240 if( 0==(iDc&SQLITE_IOCAP_SAFE_APPEND) ){
33241 /* Variable iNRecOffset is set to the offset in the journal file
33242 ** of the nRec field of the most recently written journal header.
33243 ** This field will be updated following the xSync() operation
33244 ** on the journal file. */
33245 i64 iNRecOffset = pPager->journalHdr + sizeof(aJournalMagic);
33246
33247 /* This block deals with an obscure problem. If the last connection
33248 ** that wrote to this database was operating in persistent-journal
33249 ** mode, then the journal file may at this point actually be larger
33250 ** than Pager.journalOff bytes. If the next thing in the journal
33251 ** file happens to be a journal-header (written as part of the
@@ -33264,12 +33264,18 @@
33264 ** Variable iNextHdrOffset is set to the offset at which this
33265 ** problematic header will occur, if it exists. aMagic is used
33266 ** as a temporary buffer to inspect the first couple of bytes of
33267 ** the potential journal header.
33268 */
33269 i64 iNextHdrOffset = journalHdrOffset(pPager);
33270 u8 aMagic[8];
 
 
 
 
 
 
33271 rc = sqlite3OsRead(pPager->jfd, aMagic, 8, iNextHdrOffset);
33272 if( rc==SQLITE_OK && 0==memcmp(aMagic, aJournalMagic, 8) ){
33273 static const u8 zerobyte = 0;
33274 rc = sqlite3OsWrite(pPager->jfd, &zerobyte, 1, iNextHdrOffset);
33275 }
@@ -33292,12 +33298,14 @@
33292 PAGERTRACE(("SYNC journal of %d\n", PAGERID(pPager)));
33293 IOTRACE(("JSYNC %p\n", pPager))
33294 rc = sqlite3OsSync(pPager->jfd, pPager->sync_flags);
33295 if( rc!=SQLITE_OK ) return rc;
33296 }
33297 IOTRACE(("JHDR %p %lld %d\n", pPager, iNRecOffset, 4));
33298 rc = write32bits(pPager->jfd, iNRecOffset, pPager->nRec);
 
 
33299 if( rc!=SQLITE_OK ) return rc;
33300 }
33301 if( 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
33302 PAGERTRACE(("SYNC journal of %d\n", PAGERID(pPager)));
33303 IOTRACE(("JSYNC %p\n", pPager))
@@ -35864,11 +35872,11 @@
35864 ** May you do good and not evil.
35865 ** May you find forgiveness for yourself and forgive others.
35866 ** May you share freely, never taking more than you give.
35867 **
35868 *************************************************************************
35869 ** $Id: btreeInt.h,v 1.48 2009/06/22 12:05:10 drh Exp $
35870 **
35871 ** This file implements a external (disk-based) database using BTrees.
35872 ** For a detailed discussion of BTrees, refer to
35873 **
35874 ** Donald E. Knuth, THE ART OF COMPUTER PROGRAMMING, Volume 3:
@@ -36330,13 +36338,10 @@
36330 int skip; /* (skip<0) -> Prev() is a no-op. (skip>0) -> Next() is */
36331 #ifndef SQLITE_OMIT_INCRBLOB
36332 u8 isIncrblobHandle; /* True if this cursor is an incr. io handle */
36333 Pgno *aOverflow; /* Cache of overflow page locations */
36334 #endif
36335 #ifndef NDEBUG
36336 u8 pagesShuffled; /* True if Btree pages are rearranged by balance()*/
36337 #endif
36338 i16 iPage; /* Index of current page in apPage */
36339 MemPage *apPage[BTCURSOR_MAX_DEPTH]; /* Pages from root to current page */
36340 u16 aiIdx[BTCURSOR_MAX_DEPTH]; /* Current index in apPage[i] */
36341 };
36342
@@ -36858,11 +36863,11 @@
36858 ** May you do good and not evil.
36859 ** May you find forgiveness for yourself and forgive others.
36860 ** May you share freely, never taking more than you give.
36861 **
36862 *************************************************************************
36863 ** $Id: btree.c,v 1.639 2009/06/23 11:22:29 danielk1977 Exp $
36864 **
36865 ** This file implements a external (disk-based) database using BTrees.
36866 ** See the header comment on "btreeInt.h" for additional information.
36867 ** Including a description of file format and an overview of operation.
36868 */
@@ -37785,11 +37790,10 @@
37785 */
37786 assert( nByte <= (
37787 get2byte(&data[hdr+5])-(hdr+8+(pPage->leaf?0:4)+2*get2byte(&data[hdr+3]))
37788 ));
37789
37790 pPage->nFree -= (u16)nByte;
37791 nFrag = data[hdr+7];
37792 if( nFrag>=60 ){
37793 defragmentPage(pPage);
37794 }else{
37795 /* Search the freelist looking for a free slot big enough to satisfy
@@ -37864,11 +37868,11 @@
37864 }
37865 assert( pbegin>addr || pbegin==0 );
37866 put2byte(&data[addr], start);
37867 put2byte(&data[start], pbegin);
37868 put2byte(&data[start+2], size);
37869 pPage->nFree += (u16)size;
37870
37871 /* Coalesce adjacent free blocks */
37872 addr = pPage->hdrOffset + 1;
37873 while( (pbegin = get2byte(&data[addr]))>0 ){
37874 int pnext, psize, x;
@@ -38963,14 +38967,14 @@
38963 ** If there is a transaction in progress, this routine is a no-op.
38964 */
38965 static void unlockBtreeIfUnused(BtShared *pBt){
38966 assert( sqlite3_mutex_held(pBt->mutex) );
38967 if( pBt->inTransaction==TRANS_NONE && pBt->pCursor==0 && pBt->pPage1!=0 ){
38968 if( sqlite3PagerRefcount(pBt->pPager)>=1 ){
38969 assert( pBt->pPage1->aData );
38970 releasePage(pBt->pPage1);
38971 }
38972 pBt->pPage1 = 0;
38973 }
38974 }
38975
38976 /*
@@ -41961,11 +41965,11 @@
41961 assert( end <= get2byte(&data[hdr+5]) );
41962 if (idx+sz > pPage->pBt->usableSize) {
41963 return SQLITE_CORRUPT_BKPT;
41964 }
41965 pPage->nCell++;
41966 pPage->nFree -= 2;
41967 memcpy(&data[idx+nSkip], pCell+nSkip, sz-nSkip);
41968 if( iChild ){
41969 put4byte(&data[idx], iChild);
41970 }
41971 for(j=end-2, ptr=&data[j]; j>ins; j-=2, ptr-=2){
@@ -41977,11 +41981,11 @@
41977 #ifndef SQLITE_OMIT_AUTOVACUUM
41978 if( pPage->pBt->autoVacuum ){
41979 /* The cell may contain a pointer to an overflow page. If so, write
41980 ** the entry for the overflow page into the pointer map.
41981 */
41982 rc = ptrmapPutOvflPtr(pPage, pCell);
41983 }
41984 #endif
41985 }
41986
41987 return SQLITE_OK;
@@ -42186,10 +42190,59 @@
42186 }
42187 return 1;
42188 }
42189 #endif
42190
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42191
42192 /*
42193 ** This routine redistributes cells on the iParentIdx'th child of pParent
42194 ** (hereafter "the page") and up to 2 siblings so that all pages have about the
42195 ** same amount of free space. Usually a single sibling on either side of the
@@ -42230,21 +42283,22 @@
42230 ** SQLITE_NOMEM.
42231 */
42232 static int balance_nonroot(
42233 MemPage *pParent, /* Parent page of siblings being balanced */
42234 int iParentIdx, /* Index of "the page" in pParent */
42235 u8 *aOvflSpace /* page-size bytes of space for parent ovfl */
 
42236 ){
42237 BtShared *pBt; /* The whole database */
42238 int nCell = 0; /* Number of cells in apCell[] */
42239 int nMaxCells = 0; /* Allocated size of apCell, szCell, aFrom. */
42240 int nNew = 0; /* Number of pages in apNew[] */
42241 int nOld; /* Number of pages in apOld[] */
42242 int i, j, k; /* Loop counters */
42243 int nxDiv; /* Next divider slot in pParent->aCell[] */
42244 int rc = SQLITE_OK; /* The return code */
42245 int leafCorrection; /* 4 if pPage is a leaf. 0 if not */
42246 int leafData; /* True if pPage is a leaf of a LEAFDATA tree */
42247 int usableSpace; /* Bytes in pPage beyond the header */
42248 int pageFlags; /* Value of pPage->aData[0] */
42249 int subtotal; /* Subtotal of bytes in cells on one page */
42250 int iSpace1 = 0; /* First unused byte of aSpace1[] */
@@ -42409,11 +42463,11 @@
42409 apCell[nCell] = findOverflowCell(pOld, j);
42410 szCell[nCell] = cellSizePtr(pOld, apCell[nCell]);
42411 nCell++;
42412 }
42413 if( i<nOld-1 && !leafData){
42414 u16 sz = szNew[i];
42415 u8 *pTemp;
42416 assert( nCell<nMaxCells );
42417 szCell[nCell] = sz;
42418 pTemp = &aSpace1[iSpace1];
42419 iSpace1 += sz;
@@ -42420,11 +42474,11 @@
42420 assert( sz<=pBt->pageSize/4 );
42421 assert( iSpace1<=pBt->pageSize );
42422 memcpy(pTemp, apDiv[i], sz);
42423 apCell[nCell] = pTemp+leafCorrection;
42424 assert( leafCorrection==0 || leafCorrection==4 );
42425 szCell[nCell] -= (u16)leafCorrection;
42426 if( !pOld->leaf ){
42427 assert( leafCorrection==0 );
42428 assert( pOld->hdrOffset==0 );
42429 /* The right pointer of the child page pOld becomes the left
42430 ** pointer of the divider cell */
@@ -42618,11 +42672,12 @@
42618 j = cntNew[i];
42619
42620 /* If the sibling page assembled above was not the right-most sibling,
42621 ** insert a divider cell into the parent page.
42622 */
42623 if( i<nNew-1 && j<nCell ){
 
42624 u8 *pCell;
42625 u8 *pTemp;
42626 int sz;
42627
42628 assert( j<nMaxCells );
@@ -42678,42 +42733,64 @@
42678 if( (pageFlags & PTF_LEAF)==0 ){
42679 u8 *zChild = &apCopy[nOld-1]->aData[8];
42680 memcpy(&apNew[nNew-1]->aData[8], zChild, 4);
42681 }
42682
42683 /* Fix the pointer-map entries for all the cells that were shifted around.
42684 ** There are several different types of pointer-map entries that need to
42685 ** be dealt with by this routine. Some of these have been set already, but
42686 ** many have not. The following is a summary:
42687 **
42688 ** 1) The entries associated with new sibling pages that were not
42689 ** siblings when this function was called. These have already
42690 ** been set. We don't need to worry about old siblings that were
42691 ** moved to the free-list - the freePage() code has taken care
42692 ** of those.
42693 **
42694 ** 2) The pointer-map entries associated with the first overflow
42695 ** page in any overflow chains used by new divider cells. These
42696 ** have also already been taken care of by the insertCell() code.
42697 **
42698 ** 3) If the sibling pages are not leaves, then the child pages of
42699 ** cells stored on the sibling pages may need to be updated.
42700 **
42701 ** 4) If the sibling pages are not internal intkey nodes, then any
42702 ** overflow pages used by these cells may need to be updated
42703 ** (internal intkey nodes never contain pointers to overflow pages).
42704 **
42705 ** 5) If the sibling pages are not leaves, then the pointer-map
42706 ** entries for the right-child pages of each sibling may need
42707 ** to be updated.
42708 **
42709 ** Cases 1 and 2 are dealt with above by other code. The following
42710 ** block deals with cases 3 and 4. Since setting a pointer map entry
42711 ** is a relatively expensive operation, this code only sets pointer
42712 ** map entries for child or overflow pages that have actually moved
42713 ** between pages. */
42714 if( ISAUTOVACUUM ){
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42715 MemPage *pNew = apNew[0];
42716 MemPage *pOld = apCopy[0];
42717 int nOverflow = pOld->nOverflow;
42718 int iNextOld = pOld->nCell + nOverflow;
42719 int iOverflow = (nOverflow ? pOld->aOvfl[0].idx : -1);
@@ -42787,11 +42864,11 @@
42787 }
42788
42789 assert( pParent->isInit );
42790 TRACE(("BALANCE: finished: old=%d new=%d cells=%d\n",
42791 nOld, nNew, nCell));
42792
42793 /*
42794 ** Cleanup before returning.
42795 */
42796 balance_cleanup:
42797 sqlite3ScratchFree(apCell);
@@ -42800,113 +42877,10 @@
42800 }
42801 for(i=0; i<nNew; i++){
42802 releasePage(apNew[i]);
42803 }
42804
42805 return rc;
42806 }
42807
42808 /*
42809 ** This function is used to copy the contents of the b-tree node stored
42810 ** on page pFrom to page pTo. If page pFrom was not a leaf page, then
42811 ** the pointer-map entries for each child page are updated so that the
42812 ** parent page stored in the pointer map is page pTo. If pFrom contained
42813 ** any cells with overflow page pointers, then the corresponding pointer
42814 ** map entries are also updated so that the parent page is page pTo.
42815 **
42816 ** If pFrom is currently carrying any overflow cells (entries in the
42817 ** MemPage.aOvfl[] array), they are not copied to pTo.
42818 **
42819 ** Before returning, page pTo is reinitialized using sqlite3BtreeInitPage().
42820 **
42821 ** The performance of this function is not critical. It is only used by
42822 ** the balance_shallower() and balance_deeper() procedures, neither of
42823 ** which are called often under normal circumstances.
42824 */
42825 static int copyNodeContent(MemPage *pFrom, MemPage *pTo){
42826 BtShared * const pBt = pFrom->pBt;
42827 u8 * const aFrom = pFrom->aData;
42828 u8 * const aTo = pTo->aData;
42829 int const iFromHdr = pFrom->hdrOffset;
42830 int const iToHdr = ((pTo->pgno==1) ? 100 : 0);
42831 int rc = SQLITE_OK;
42832 int iData;
42833
42834 assert( pFrom->isInit );
42835 assert( pFrom->nFree>=iToHdr );
42836 assert( get2byte(&aFrom[iFromHdr+5])<=pBt->usableSize );
42837
42838 /* Copy the b-tree node content from page pFrom to page pTo. */
42839 iData = get2byte(&aFrom[iFromHdr+5]);
42840 memcpy(&aTo[iData], &aFrom[iData], pBt->usableSize-iData);
42841 memcpy(&aTo[iToHdr], &aFrom[iFromHdr], pFrom->cellOffset + 2*pFrom->nCell);
42842
42843 /* Reinitialize page pTo so that the contents of the MemPage structure
42844 ** match the new data. The initialization of pTo "cannot" fail, as the
42845 ** data copied from pFrom is known to be valid. */
42846 pTo->isInit = 0;
42847 TESTONLY(rc = ) sqlite3BtreeInitPage(pTo);
42848 assert( rc==SQLITE_OK );
42849
42850 /* If this is an auto-vacuum database, update the pointer-map entries
42851 ** for any b-tree or overflow pages that pTo now contains the pointers to. */
42852 if( ISAUTOVACUUM ){
42853 rc = setChildPtrmaps(pTo);
42854 }
42855 return rc;
42856 }
42857
42858 /*
42859 ** This routine is called on the root page of a btree when the root
42860 ** page contains no cells. This is an opportunity to make the tree
42861 ** shallower by one level.
42862 */
42863 static int balance_shallower(MemPage *pRoot){
42864 /* The root page is empty but has one child. Transfer the
42865 ** information from that one child into the root page if it
42866 ** will fit. This reduces the depth of the tree by one.
42867 **
42868 ** If the root page is page 1, it has less space available than
42869 ** its child (due to the 100 byte header that occurs at the beginning
42870 ** of the database fle), so it might not be able to hold all of the
42871 ** information currently contained in the child. If this is the
42872 ** case, then do not do the transfer. Leave page 1 empty except
42873 ** for the right-pointer to the child page. The child page becomes
42874 ** the virtual root of the tree.
42875 */
42876 int rc = SQLITE_OK; /* Return code */
42877 int const hdr = pRoot->hdrOffset; /* Offset of root page header */
42878 MemPage *pChild; /* Only child of pRoot */
42879 Pgno const pgnoChild = get4byte(&pRoot->aData[pRoot->hdrOffset+8]);
42880
42881 assert( pRoot->nCell==0 );
42882 assert( sqlite3_mutex_held(pRoot->pBt->mutex) );
42883 assert( !pRoot->leaf );
42884 assert( pgnoChild>0 );
42885 assert( pgnoChild<=pagerPagecount(pRoot->pBt) );
42886 assert( hdr==0 || pRoot->pgno==1 );
42887
42888 rc = sqlite3BtreeGetPage(pRoot->pBt, pgnoChild, &pChild, 0);
42889 if( rc==SQLITE_OK ){
42890 if( pChild->nFree>=hdr ){
42891 if( hdr ){
42892 rc = defragmentPage(pChild);
42893 }
42894 if( rc==SQLITE_OK ){
42895 rc = copyNodeContent(pChild, pRoot);
42896 }
42897 if( rc==SQLITE_OK ){
42898 rc = freePage(pChild);
42899 }
42900 }else{
42901 /* The child has more information that will fit on the root.
42902 ** The tree is already balanced. Do nothing. */
42903 TRACE(("BALANCE: child %d will not fit on page 1\n", pChild->pgno));
42904 }
42905 releasePage(pChild);
42906 }
42907
42908 return rc;
42909 }
42910
42911
42912 /*
@@ -42974,18 +42948,12 @@
42974 ** some way. This function figures out if this modification means the
42975 ** tree needs to be balanced, and if so calls the appropriate balancing
42976 ** routine. Balancing routines are:
42977 **
42978 ** balance_quick()
42979 ** balance_shallower()
42980 ** balance_deeper()
42981 ** balance_nonroot()
42982 **
42983 ** If built with SQLITE_DEBUG, pCur->pagesShuffled is set to true if
42984 ** balance_shallower(), balance_deeper() or balance_nonroot() is called.
42985 ** If none of these functions are invoked, pCur->pagesShuffled is left
42986 ** unmodified.
42987 */
42988 static int balance(BtCursor *pCur){
42989 int rc = SQLITE_OK;
42990 const int nMin = pCur->pBt->usableSize * 2 / 3;
42991 u8 aBalanceQuickSpace[13];
@@ -43011,25 +42979,11 @@
43011 pCur->iPage = 1;
43012 pCur->aiIdx[0] = 0;
43013 pCur->aiIdx[1] = 0;
43014 assert( pCur->apPage[1]->nOverflow );
43015 }
43016 VVA_ONLY( pCur->pagesShuffled = 1 );
43017 }else{
43018 /* The root page of the b-tree is now empty. If the root-page is not
43019 ** also a leaf page, it will have a single child page. Call
43020 ** balance_shallower to attempt to copy the contents of the single
43021 ** child-page into the root page (this may not be possible if the
43022 ** root page is page 1).
43023 **
43024 ** Whether or not this is possible , the tree is now balanced.
43025 ** Therefore is no next iteration of the do-loop.
43026 */
43027 if( pPage->nCell==0 && !pPage->leaf ){
43028 rc = balance_shallower(pPage);
43029 VVA_ONLY( pCur->pagesShuffled = 1 );
43030 }
43031 break;
43032 }
43033 }else if( pPage->nOverflow==0 && pPage->nFree<=nMin ){
43034 break;
43035 }else{
@@ -43079,11 +43033,11 @@
43079 ** the previous call, as the overflow cell data will have been
43080 ** copied either into the body of a database page or into the new
43081 ** pSpace buffer passed to the latter call to balance_nonroot().
43082 */
43083 u8 *pSpace = sqlite3PageMalloc(pCur->pBt->pageSize);
43084 rc = balance_nonroot(pParent, iIdx, pSpace);
43085 if( pFree ){
43086 /* If pFree is not NULL, it points to the pSpace buffer used
43087 ** by a previous call to balance_nonroot(). Its contents are
43088 ** now stored either on real database pages or within the
43089 ** new pSpace buffer, so it may be safely freed here. */
@@ -43092,11 +43046,10 @@
43092
43093 /* The pSpace buffer will be freed after the next call to
43094 ** balance_nonroot(), or just before this function returns, whichever
43095 ** comes first. */
43096 pFree = pSpace;
43097 VVA_ONLY( pCur->pagesShuffled = 1 );
43098 }
43099 }
43100
43101 pPage->nOverflow = 0;
43102
@@ -45290,11 +45243,11 @@
45290 ** This file contains code use to manipulate "Mem" structure. A "Mem"
45291 ** stores a single value in the VDBE. Mem is an opaque structure visible
45292 ** only within the VDBE. Interface routines refer to a Mem using the
45293 ** name sqlite_value
45294 **
45295 ** $Id: vdbemem.c,v 1.149 2009/06/22 19:05:41 drh Exp $
45296 */
45297
45298 /*
45299 ** Call sqlite3VdbeMemExpandBlob() on the supplied value (type Mem*)
45300 ** P if required.
@@ -45369,11 +45322,11 @@
45369 sqlite3DbFree(pMem->db, pMem->zMalloc);
45370 pMem->zMalloc = sqlite3DbMallocRaw(pMem->db, n);
45371 }
45372 }
45373
45374 if( preserve && pMem->z && pMem->zMalloc && pMem->z!=pMem->zMalloc ){
45375 memcpy(pMem->zMalloc, pMem->z, pMem->n);
45376 }
45377 if( pMem->flags&MEM_Dyn && pMem->xDel ){
45378 pMem->xDel((void *)(pMem->z));
45379 }
@@ -45518,11 +45471,11 @@
45518 ** Return SQLITE_ERROR if the finalizer reports an error. SQLITE_OK
45519 ** otherwise.
45520 */
45521 SQLITE_PRIVATE int sqlite3VdbeMemFinalize(Mem *pMem, FuncDef *pFunc){
45522 int rc = SQLITE_OK;
45523 if( pFunc && pFunc->xFinalize ){
45524 sqlite3_context ctx;
45525 assert( (pMem->flags & MEM_Null)!=0 || pFunc==pMem->u.pDef );
45526 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
45527 memset(&ctx, 0, sizeof(ctx));
45528 ctx.s.flags = MEM_Null;
@@ -45531,11 +45484,11 @@
45531 ctx.pFunc = pFunc;
45532 pFunc->xFinalize(&ctx);
45533 assert( 0==(pMem->flags&MEM_Dyn) && !pMem->xDel );
45534 sqlite3DbFree(pMem->db, pMem->zMalloc);
45535 memcpy(pMem, &ctx.s, sizeof(ctx.s));
45536 rc = (ctx.isError?SQLITE_ERROR:SQLITE_OK);
45537 }
45538 return rc;
45539 }
45540
45541 /*
@@ -45806,16 +45759,13 @@
45806 ** empty boolean index.
45807 */
45808 SQLITE_PRIVATE void sqlite3VdbeMemSetRowSet(Mem *pMem){
45809 sqlite3 *db = pMem->db;
45810 assert( db!=0 );
45811 if( pMem->flags & MEM_RowSet ){
45812 sqlite3RowSetClear(pMem->u.pRowSet);
45813 }else{
45814 sqlite3VdbeMemRelease(pMem);
45815 pMem->zMalloc = sqlite3DbMallocRaw(db, 64);
45816 }
45817 if( db->mallocFailed ){
45818 pMem->flags = MEM_Null;
45819 }else{
45820 assert( pMem->zMalloc );
45821 pMem->u.pRowSet = sqlite3RowSetInit(db, pMem->zMalloc,
@@ -46153,11 +46103,11 @@
46153 }else{
46154 zData = (char *)sqlite3BtreeDataFetch(pCur, &available);
46155 }
46156 assert( zData!=0 );
46157
46158 if( offset+amt<=available && ((pMem->flags&MEM_Dyn)==0 || pMem->xDel) ){
46159 sqlite3VdbeMemRelease(pMem);
46160 pMem->z = &zData[offset];
46161 pMem->flags = MEM_Blob|MEM_Ephem;
46162 }else if( SQLITE_OK==(rc = sqlite3VdbeMemGrow(pMem, amt+2, 0)) ){
46163 pMem->flags = MEM_Blob|MEM_Dyn|MEM_Term;
@@ -46370,11 +46320,11 @@
46370 ** This file contains code used for creating, destroying, and populating
46371 ** a VDBE (or an "sqlite3_stmt" as it is known to the outside world.) Prior
46372 ** to version 2.8.7, all this code was combined into the vdbe.c source file.
46373 ** But that file was getting too big so this subroutines were split out.
46374 **
46375 ** $Id: vdbeaux.c,v 1.464 2009/06/23 14:15:04 drh Exp $
46376 */
46377
46378
46379
46380 /*
@@ -46499,11 +46449,11 @@
46499 i = p->nOp;
46500 assert( p->magic==VDBE_MAGIC_INIT );
46501 assert( op>0 && op<0xff );
46502 if( p->nOpAlloc<=i ){
46503 if( growOpArray(p) ){
46504 return 0;
46505 }
46506 }
46507 p->nOp++;
46508 pOp = &p->aOp[i];
46509 pOp->opcode = (u8)op;
@@ -46704,11 +46654,11 @@
46704 assert( p->magic==VDBE_MAGIC_INIT );
46705 if( p->nOp + nOp > p->nOpAlloc && growOpArray(p) ){
46706 return 0;
46707 }
46708 addr = p->nOp;
46709 if( nOp>0 ){
46710 int i;
46711 VdbeOpList const *pIn = aOp;
46712 for(i=0; i<nOp; i++, pIn++){
46713 int p2 = pIn->p2;
46714 VdbeOp *pOut = &p->aOp[i+addr];
@@ -46740,44 +46690,47 @@
46740 ** This routine is useful when a large program is loaded from a
46741 ** static array using sqlite3VdbeAddOpList but we want to make a
46742 ** few minor changes to the program.
46743 */
46744 SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe *p, int addr, int val){
46745 assert( p==0 || p->magic==VDBE_MAGIC_INIT );
46746 if( p && addr>=0 && p->nOp>addr && p->aOp ){
 
46747 p->aOp[addr].p1 = val;
46748 }
46749 }
46750
46751 /*
46752 ** Change the value of the P2 operand for a specific instruction.
46753 ** This routine is useful for setting a jump destination.
46754 */
46755 SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe *p, int addr, int val){
46756 assert( p==0 || p->magic==VDBE_MAGIC_INIT );
46757 if( p && addr>=0 && p->nOp>addr && p->aOp ){
 
46758 p->aOp[addr].p2 = val;
46759 }
46760 }
46761
46762 /*
46763 ** Change the value of the P3 operand for a specific instruction.
46764 */
46765 SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe *p, int addr, int val){
46766 assert( p==0 || p->magic==VDBE_MAGIC_INIT );
46767 if( p && addr>=0 && p->nOp>addr && p->aOp ){
 
46768 p->aOp[addr].p3 = val;
46769 }
46770 }
46771
46772 /*
46773 ** Change the value of the P5 operand for the most recently
46774 ** added operation.
46775 */
46776 SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe *p, u8 val){
46777 assert( p==0 || p->magic==VDBE_MAGIC_INIT );
46778 if( p && p->aOp ){
46779 assert( p->nOp>0 );
46780 p->aOp[p->nOp-1].p5 = val;
46781 }
46782 }
46783
@@ -46793,11 +46746,11 @@
46793 /*
46794 ** If the input FuncDef structure is ephemeral, then free it. If
46795 ** the FuncDef is not ephermal, then do nothing.
46796 */
46797 static void freeEphemeralFunction(sqlite3 *db, FuncDef *pDef){
46798 if( pDef && (pDef->flags & SQLITE_FUNC_EPHEM)!=0 ){
46799 sqlite3DbFree(db, pDef);
46800 }
46801 }
46802
46803 /*
@@ -46838,11 +46791,11 @@
46838
46839 /*
46840 ** Change N opcodes starting at addr to No-ops.
46841 */
46842 SQLITE_PRIVATE void sqlite3VdbeChangeToNoop(Vdbe *p, int addr, int N){
46843 if( p && p->aOp ){
46844 VdbeOp *pOp = &p->aOp[addr];
46845 sqlite3 *db = p->db;
46846 while( N-- ){
46847 freeP4(db, pOp->p4type, pOp->p4.p);
46848 memset(pOp, 0, sizeof(pOp[0]));
@@ -46887,14 +46840,14 @@
46887 if (n != P4_KEYINFO) {
46888 freeP4(db, n, (void*)*(char**)&zP4);
46889 }
46890 return;
46891 }
 
46892 assert( addr<p->nOp );
46893 if( addr<0 ){
46894 addr = p->nOp - 1;
46895 if( addr<0 ) return;
46896 }
46897 pOp = &p->aOp[addr];
46898 freeP4(db, pOp->p4type, pOp->p4.p);
46899 pOp->p4.p = 0;
46900 if( n==P4_INT32 ){
@@ -47498,13 +47451,13 @@
47498 }
47499 zCsr = p->pFree;
47500 zEnd = &zCsr[nByte];
47501 }while( nByte && !db->mallocFailed );
47502
47503 p->nCursor = nCursor;
47504 if( p->aVar ){
47505 p->nVar = nVar;
47506 for(n=0; n<nVar; n++){
47507 p->aVar[n].flags = MEM_Null;
47508 p->aVar[n].db = db;
47509 }
47510 }
@@ -47632,11 +47585,11 @@
47632 sqlite3 *db = p->db;
47633
47634 releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
47635 sqlite3DbFree(db, p->aColName);
47636 n = nResColumn*COLNAME_N;
47637 p->nResColumn = nResColumn;
47638 p->aColName = pColName = (Mem*)sqlite3DbMallocZero(db, sizeof(Mem)*n );
47639 if( p->aColName==0 ) return;
47640 while( n-- > 0 ){
47641 pColName->flags = MEM_Null;
47642 pColName->db = p->db;
@@ -47685,10 +47638,17 @@
47685 static int vdbeCommit(sqlite3 *db, Vdbe *p){
47686 int i;
47687 int nTrans = 0; /* Number of databases with an active write-transaction */
47688 int rc = SQLITE_OK;
47689 int needXcommit = 0;
 
 
 
 
 
 
 
47690
47691 /* Before doing anything else, call the xSync() callback for any
47692 ** virtual module tables written in this transaction. This has to
47693 ** be done before determining whether a master journal file is
47694 ** required, as an xSync() callback may add an attached database
@@ -48792,26 +48752,25 @@
48792 p->nField = u;
48793 return (void*)p;
48794 }
48795
48796 /*
48797 ** This routine destroys a UnpackedRecord object
48798 */
48799 SQLITE_PRIVATE void sqlite3VdbeDeleteUnpackedRecord(UnpackedRecord *p){
48800 if( p ){
48801 if( p->flags & UNPACKED_NEED_DESTROY ){
48802 int i;
48803 Mem *pMem;
48804 for(i=0, pMem=p->aMem; i<p->nField; i++, pMem++){
48805 if( pMem->zMalloc ){
48806 sqlite3VdbeMemRelease(pMem);
48807 }
48808 }
48809 }
48810 if( p->flags & UNPACKED_NEED_FREE ){
48811 sqlite3DbFree(p->pKeyInfo->db, p);
48812 }
48813 }
48814 }
48815
48816 /*
48817 ** This function compares the two table rows or index records
@@ -48940,15 +48899,15 @@
48940 u32 typeRowid; /* Serial type of the rowid */
48941 u32 lenRowid; /* Size of the rowid */
48942 Mem m, v;
48943
48944 /* Get the size of the index entry. Only indices entries of less
48945 ** than 2GiB are support - anything large must be database corruption */
 
 
48946 sqlite3BtreeKeySize(pCur, &nCellKey);
48947 if( unlikely(nCellKey<=0 || nCellKey>0x7fffffff) ){
48948 return SQLITE_CORRUPT_BKPT;
48949 }
48950
48951 /* Read in the complete content of the index entry */
48952 m.flags = 0;
48953 m.db = db;
48954 m.zMalloc = 0;
@@ -48957,13 +48916,13 @@
48957 return rc;
48958 }
48959
48960 /* The index entry must begin with a header size */
48961 (void)getVarint32((u8*)m.z, szHdr);
48962 testcase( szHdr==2 );
48963 testcase( szHdr==m.n );
48964 if( unlikely(szHdr<2 || (int)szHdr>m.n) ){
48965 goto idx_rowid_corruption;
48966 }
48967
48968 /* The last field of the index should be an integer - the ROWID.
48969 ** Verify that the last entry really is an integer. */
@@ -49098,11 +49057,11 @@
49098 *************************************************************************
49099 **
49100 ** This file contains code use to implement APIs that are part of the
49101 ** VDBE.
49102 **
49103 ** $Id: vdbeapi.c,v 1.166 2009/06/19 14:06:03 drh Exp $
49104 */
49105
49106 #ifndef SQLITE_OMIT_DEPRECATED
49107 /*
49108 ** Return TRUE (non-zero) of the statement supplied as an argument needs
@@ -49238,20 +49197,35 @@
49238 }
49239
49240 /**************************** sqlite3_result_ *******************************
49241 ** The following routines are used by user-defined functions to specify
49242 ** the function result.
 
 
 
 
49243 */
 
 
 
 
 
 
 
 
 
 
 
49244 SQLITE_API void sqlite3_result_blob(
49245 sqlite3_context *pCtx,
49246 const void *z,
49247 int n,
49248 void (*xDel)(void *)
49249 ){
49250 assert( n>=0 );
49251 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
49252 sqlite3VdbeMemSetStr(&pCtx->s, z, n, 0, xDel);
49253 }
49254 SQLITE_API void sqlite3_result_double(sqlite3_context *pCtx, double rVal){
49255 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
49256 sqlite3VdbeMemSetDouble(&pCtx->s, rVal);
49257 }
@@ -49284,39 +49258,39 @@
49284 const char *z,
49285 int n,
49286 void (*xDel)(void *)
49287 ){
49288 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
49289 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF8, xDel);
49290 }
49291 #ifndef SQLITE_OMIT_UTF16
49292 SQLITE_API void sqlite3_result_text16(
49293 sqlite3_context *pCtx,
49294 const void *z,
49295 int n,
49296 void (*xDel)(void *)
49297 ){
49298 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
49299 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16NATIVE, xDel);
49300 }
49301 SQLITE_API void sqlite3_result_text16be(
49302 sqlite3_context *pCtx,
49303 const void *z,
49304 int n,
49305 void (*xDel)(void *)
49306 ){
49307 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
49308 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16BE, xDel);
49309 }
49310 SQLITE_API void sqlite3_result_text16le(
49311 sqlite3_context *pCtx,
49312 const void *z,
49313 int n,
49314 void (*xDel)(void *)
49315 ){
49316 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
49317 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16LE, xDel);
49318 }
49319 #endif /* SQLITE_OMIT_UTF16 */
49320 SQLITE_API void sqlite3_result_value(sqlite3_context *pCtx, sqlite3_value *pValue){
49321 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
49322 sqlite3VdbeMemCopy(&pCtx->s, pValue);
@@ -50319,11 +50293,11 @@
50319 ** documentation, headers files, or other derived files. The formatting
50320 ** of the code in this file is, therefore, important. See other comments
50321 ** in this file for details. If in doubt, do not deviate from existing
50322 ** commenting and indentation practices when changing or adding code.
50323 **
50324 ** $Id: vdbe.c,v 1.862 2009/06/23 14:15:04 drh Exp $
50325 */
50326
50327 /*
50328 ** The following global variable is incremented every time a cursor
50329 ** moves, either by the OP_SeekXX, OP_Next, or OP_Prev opcodes. The test
@@ -51615,11 +51589,11 @@
51615 ** is the same as executing Halt.
51616 */
51617 case OP_Halt: {
51618 p->rc = pOp->p1;
51619 p->pc = pc;
51620 p->errorAction = pOp->p2;
51621 if( pOp->p4.z ){
51622 sqlite3SetString(&p->zErrMsg, db, "%s", pOp->p4.z);
51623 }
51624 rc = sqlite3VdbeHalt(p);
51625 assert( rc==SQLITE_BUSY || rc==SQLITE_OK );
@@ -52816,10 +52790,11 @@
52816 memset(&u.am.sMem, 0, sizeof(u.am.sMem));
52817 assert( u.am.p1<p->nCursor );
52818 assert( pOp->p3>0 && pOp->p3<=p->nMem );
52819 u.am.pDest = &p->aMem[pOp->p3];
52820 MemSetTypeFlag(u.am.pDest, MEM_Null);
 
52821
52822 /* This block sets the variable u.am.payloadSize to be the total number of
52823 ** bytes in the record.
52824 **
52825 ** u.am.zRec is set to be the complete text of the record if it is available.
@@ -52834,16 +52809,15 @@
52834 u.am.pC = p->apCsr[u.am.p1];
52835 assert( u.am.pC!=0 );
52836 #ifndef SQLITE_OMIT_VIRTUALTABLE
52837 assert( u.am.pC->pVtabCursor==0 );
52838 #endif
52839 if( u.am.pC->pCursor!=0 ){
 
52840 /* The record is stored in a B-Tree */
52841 rc = sqlite3VdbeCursorMoveto(u.am.pC);
52842 if( rc ) goto abort_due_to_error;
52843 u.am.zRec = 0;
52844 u.am.pCrsr = u.am.pC->pCursor;
52845 if( u.am.pC->nullRow ){
52846 u.am.payloadSize = 0;
52847 }else if( u.am.pC->cacheStatus==p->cacheCtr ){
52848 u.am.payloadSize = u.am.pC->payloadSize;
52849 u.am.zRec = (char*)u.am.pC->aRow;
@@ -52855,19 +52829,16 @@
52855 assert( (u.am.payloadSize64 & SQLITE_MAX_U32)==(u64)u.am.payloadSize64 );
52856 u.am.payloadSize = (u32)u.am.payloadSize64;
52857 }else{
52858 sqlite3BtreeDataSize(u.am.pCrsr, &u.am.payloadSize);
52859 }
52860 u.am.nField = u.am.pC->nField;
52861 }else if( u.am.pC->pseudoTable ){
52862 /* The record is the sole entry of a pseudo-table */
52863 u.am.payloadSize = u.am.pC->nData;
52864 u.am.zRec = u.am.pC->pData;
52865 u.am.pC->cacheStatus = CACHE_STALE;
52866 assert( u.am.payloadSize==0 || u.am.zRec!=0 );
52867 u.am.nField = u.am.pC->nField;
52868 u.am.pCrsr = 0;
52869 }else{
52870 /* Consider the row to be NULL */
52871 u.am.payloadSize = 0;
52872 }
52873
@@ -52879,10 +52850,11 @@
52879 assert( db->aLimit[SQLITE_LIMIT_LENGTH]>=0 );
52880 if( u.am.payloadSize > (u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
52881 goto too_big;
52882 }
52883
 
52884 assert( u.am.p2<u.am.nField );
52885
52886 /* Read and parse the table header. Store the results of the parse
52887 ** into the record header cache fields of the cursor.
52888 */
@@ -54478,11 +54450,11 @@
54478 sqlite3BtreeSetCachedRowid(u.bf.pC->pCursor, u.bf.v<MAX_ROWID ? u.bf.v+1 : 0);
54479 }
54480 if( u.bf.pC->useRandomRowid ){
54481 assert( pOp->p3==0 ); /* We cannot be in random rowid mode if this is
54482 ** an AUTOINCREMENT table. */
54483 u.bf.v = db->priorNewRowid;
54484 u.bf.cnt = 0;
54485 do{
54486 if( u.bf.cnt==0 && (u.bf.v&0xffffff)==u.bf.v ){
54487 u.bf.v++;
54488 }else{
@@ -54490,11 +54462,10 @@
54490 if( u.bf.cnt<5 ) u.bf.v &= 0xffffff;
54491 }
54492 rc = sqlite3BtreeMovetoUnpacked(u.bf.pC->pCursor, 0, (u64)u.bf.v, 0, &u.bf.res);
54493 u.bf.cnt++;
54494 }while( u.bf.cnt<100 && rc==SQLITE_OK && u.bf.res==0 );
54495 db->priorNewRowid = u.bf.v;
54496 if( rc==SQLITE_OK && u.bf.res==0 ){
54497 rc = SQLITE_FULL;
54498 goto abort_due_to_error;
54499 }
54500 }
@@ -55359,11 +55330,11 @@
55359 ** can result in a "no such table: sqlite_master" or "malformed
55360 ** database schema" error being returned to the user.
55361 */
55362 assert( sqlite3BtreeHoldsMutex(db->aDb[u.bv.iDb].pBt) );
55363 sqlite3BtreeEnterAll(db);
55364 if( pOp->p2 || ALWAYS(DbHasProperty(db, u.bv.iDb, DB_SchemaLoaded)) ){
55365 u.bv.zMaster = SCHEMA_TABLE(u.bv.iDb);
55366 u.bv.initData.db = db;
55367 u.bv.initData.iDb = pOp->p1;
55368 u.bv.initData.pzErrMsg = &p->zErrMsg;
55369 u.bv.zSql = sqlite3MPrintf(db,
@@ -55801,11 +55772,11 @@
55801 #endif /* local variables moved into u.cd */
55802 assert( pOp->p1>0 && pOp->p1<=p->nMem );
55803 u.cd.pMem = &p->aMem[pOp->p1];
55804 assert( (u.cd.pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
55805 rc = sqlite3VdbeMemFinalize(u.cd.pMem, pOp->p4.pFunc);
55806 if( rc==SQLITE_ERROR ){
55807 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(u.cd.pMem));
55808 }
55809 sqlite3VdbeChangeEncoding(u.cd.pMem, encoding);
55810 UPDATE_MAX_BLOBSIZE(u.cd.pMem);
55811 if( sqlite3VdbeMemTooBig(u.cd.pMem) ){
@@ -56122,10 +56093,13 @@
56122 if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
56123 rc = u.cj.pModule->xColumn(pCur->pVtabCursor, &u.cj.sContext, pOp->p2);
56124 sqlite3DbFree(db, p->zErrMsg);
56125 p->zErrMsg = u.cj.pVtab->zErrMsg;
56126 u.cj.pVtab->zErrMsg = 0;
 
 
 
56127
56128 /* Copy the result of the function to the P3 register. We
56129 ** do this regardless of whether or not an error occurred to ensure any
56130 ** dynamic allocation in u.cj.sContext.s (a Mem struct) is released.
56131 */
@@ -64024,11 +63998,11 @@
64024 ** creating ID lists
64025 ** BEGIN TRANSACTION
64026 ** COMMIT
64027 ** ROLLBACK
64028 **
64029 ** $Id: build.c,v 1.552 2009/06/18 17:22:39 drh Exp $
64030 */
64031
64032 /*
64033 ** This routine is called when a new SQL statement is beginning to
64034 ** be parsed. Initialize the pParse structure as needed.
@@ -64175,10 +64149,16 @@
64175 /* Once all the cookies have been verified and transactions opened,
64176 ** obtain the required table-locks. This is a no-op unless the
64177 ** shared-cache feature is enabled.
64178 */
64179 codeTableLocks(pParse);
 
 
 
 
 
 
64180 sqlite3VdbeAddOp2(v, OP_Goto, 0, pParse->cookieGoto);
64181 }
64182 }
64183
64184
@@ -64373,14 +64353,11 @@
64373 int len;
64374 Hash *pHash = &db->aDb[iDb].pSchema->idxHash;
64375
64376 len = sqlite3Strlen30(zIdxName);
64377 pIndex = sqlite3HashInsert(pHash, zIdxName, len, 0);
64378 /* Justification of ALWAYS(): This routine is only called from the
64379 ** OP_DropIndex opcode. And there is no way that opcode will ever run
64380 ** unless the corresponding index is in the symbol table. */
64381 if( ALWAYS(pIndex) ){
64382 if( pIndex->pTable->pIndex==pIndex ){
64383 pIndex->pTable->pIndex = pIndex->pNext;
64384 }else{
64385 Index *p;
64386 /* Justification of ALWAYS(); The index must be on the list of
@@ -68090,11 +68067,11 @@
68090 **
68091 *************************************************************************
68092 ** This file contains C code routines that are called by the parser
68093 ** in order to generate code for DELETE FROM statements.
68094 **
68095 ** $Id: delete.c,v 1.203 2009/05/28 01:00:55 drh Exp $
68096 */
68097
68098 /*
68099 ** Look up every table that is named in pSrc. If any table is not found,
68100 ** add an error message to pParse->zErrMsg and return NULL. If all tables
@@ -68551,10 +68528,18 @@
68551 sqlite3VdbeAddOp2(v, OP_Close, iCur + i, pIdx->tnum);
68552 }
68553 sqlite3VdbeAddOp1(v, OP_Close, iCur);
68554 }
68555 }
 
 
 
 
 
 
 
 
68556
68557 /*
68558 ** Return the number of rows that were deleted. If this routine is
68559 ** generating code because of a call to sqlite3NestedParse(), do not
68560 ** invoke the callback function.
@@ -70184,11 +70169,11 @@
70184 **
70185 *************************************************************************
70186 ** This file contains C code routines that are called by the parser
70187 ** to handle INSERT statements in SQLite.
70188 **
70189 ** $Id: insert.c,v 1.268 2009/05/29 19:00:13 drh Exp $
70190 */
70191
70192 /*
70193 ** Generate code that will open a table for reading.
70194 */
@@ -70333,58 +70318,87 @@
70333 return 0;
70334 }
70335
70336 #ifndef SQLITE_OMIT_AUTOINCREMENT
70337 /*
70338 ** Write out code to initialize the autoincrement logic. This code
70339 ** looks up the current autoincrement value in the sqlite_sequence
70340 ** table and stores that value in a register. Code generated by
70341 ** autoIncStep() will keep that register holding the largest
70342 ** rowid value. Code generated by autoIncEnd() will write the new
70343 ** largest value of the counter back into the sqlite_sequence table.
70344 **
70345 ** This routine returns the index of the mem[] cell that contains
70346 ** the maximum rowid counter.
70347 **
70348 ** Three consecutive registers are allocated by this routine. The
70349 ** first two hold the name of the target table and the maximum rowid
70350 ** inserted into the target table, respectively.
70351 ** The third holds the rowid in sqlite_sequence where we will
70352 ** write back the revised maximum rowid. This routine returns the
70353 ** index of the second of these three registers.
 
 
70354 */
70355 static int autoIncBegin(
70356 Parse *pParse, /* Parsing context */
70357 int iDb, /* Index of the database holding pTab */
70358 Table *pTab /* The table we are writing to */
70359 ){
70360 int memId = 0; /* Register holding maximum rowid */
70361 if( pTab->tabFlags & TF_Autoincrement ){
70362 Vdbe *v = pParse->pVdbe;
70363 Db *pDb = &pParse->db->aDb[iDb];
70364 int iCur = pParse->nTab++;
70365 int addr; /* Address of the top of the loop */
70366 assert( v );
70367 pParse->nMem++; /* Holds name of table */
70368 memId = ++pParse->nMem;
70369 pParse->nMem++;
70370 sqlite3OpenTable(pParse, iCur, iDb, pDb->pSchema->pSeqTab, OP_OpenRead);
70371 addr = sqlite3VdbeCurrentAddr(v);
70372 sqlite3VdbeAddOp4(v, OP_String8, 0, memId-1, 0, pTab->zName, 0);
70373 sqlite3VdbeAddOp2(v, OP_Rewind, iCur, addr+9);
70374 sqlite3VdbeAddOp3(v, OP_Column, iCur, 0, memId);
70375 sqlite3VdbeAddOp3(v, OP_Ne, memId-1, addr+7, memId);
70376 sqlite3VdbeChangeP5(v, SQLITE_JUMPIFNULL);
70377 sqlite3VdbeAddOp2(v, OP_Rowid, iCur, memId+1);
70378 sqlite3VdbeAddOp3(v, OP_Column, iCur, 1, memId);
70379 sqlite3VdbeAddOp2(v, OP_Goto, 0, addr+9);
70380 sqlite3VdbeAddOp2(v, OP_Next, iCur, addr+2);
70381 sqlite3VdbeAddOp2(v, OP_Integer, 0, memId);
70382 sqlite3VdbeAddOp2(v, OP_Close, iCur, 0);
70383 }
70384 return memId;
70385 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70386
70387 /*
70388 ** Update the maximum rowid for an autoincrement calculation.
70389 **
70390 ** This routine should be called when the top of the stack holds a
@@ -70397,46 +70411,56 @@
70397 sqlite3VdbeAddOp2(pParse->pVdbe, OP_MemMax, memId, regRowid);
70398 }
70399 }
70400
70401 /*
70402 ** After doing one or more inserts, the maximum rowid is stored
70403 ** in reg[memId]. Generate code to write this value back into the
70404 ** the sqlite_sequence table.
 
 
70405 */
70406 static void autoIncEnd(
70407 Parse *pParse, /* The parsing context */
70408 int iDb, /* Index of the database holding pTab */
70409 Table *pTab, /* Table we are inserting into */
70410 int memId /* Memory cell holding the maximum rowid */
70411 ){
70412 if( pTab->tabFlags & TF_Autoincrement ){
70413 int iCur = pParse->nTab++;
70414 Vdbe *v = pParse->pVdbe;
70415 Db *pDb = &pParse->db->aDb[iDb];
70416 int j1;
70417 int iRec = ++pParse->nMem; /* Memory cell used for record */
70418
70419 assert( v );
70420 sqlite3OpenTable(pParse, iCur, iDb, pDb->pSchema->pSeqTab, OP_OpenWrite);
70421 j1 = sqlite3VdbeAddOp1(v, OP_NotNull, memId+1);
70422 sqlite3VdbeAddOp2(v, OP_NewRowid, iCur, memId+1);
 
 
 
 
 
 
 
 
70423 sqlite3VdbeJumpHere(v, j1);
 
70424 sqlite3VdbeAddOp3(v, OP_MakeRecord, memId-1, 2, iRec);
70425 sqlite3VdbeAddOp3(v, OP_Insert, iCur, iRec, memId+1);
70426 sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
70427 sqlite3VdbeAddOp1(v, OP_Close, iCur);
 
70428 }
70429 }
70430 #else
70431 /*
70432 ** If SQLITE_OMIT_AUTOINCREMENT is defined, then the three routines
70433 ** above are all no-ops
70434 */
70435 # define autoIncBegin(A,B,C) (0)
70436 # define autoIncStep(A,B,C)
70437 # define autoIncEnd(A,B,C,D)
70438 #endif /* SQLITE_OMIT_AUTOINCREMENT */
70439
70440
70441 /* Forward declaration */
70442 static int xferOptimization(
@@ -70678,11 +70702,11 @@
70678 ** This is the 2nd template.
70679 */
70680 if( pColumn==0 && xferOptimization(pParse, pTab, pSelect, onError, iDb) ){
70681 assert( !pTrigger );
70682 assert( pList==0 );
70683 goto insert_cleanup;
70684 }
70685 #endif /* SQLITE_OMIT_XFER_OPT */
70686
70687 /* If this is an AUTOINCREMENT table, look up the sequence number in the
70688 ** sqlite_sequence table and store it in memory cell regAutoinc.
@@ -71160,15 +71184,18 @@
71160 for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
71161 sqlite3VdbeAddOp1(v, OP_Close, idx+baseCur);
71162 }
71163 }
71164
 
71165 /* Update the sqlite_sequence table by storing the content of the
71166 ** counter value in memory regAutoinc back into the sqlite_sequence
71167 ** table.
71168 */
71169 autoIncEnd(pParse, iDb, pTab, regAutoinc);
 
 
71170
71171 /*
71172 ** Return the number of rows inserted. If this routine is
71173 ** generating code because of a call to sqlite3NestedParse(), do not
71174 ** invoke the callback function.
@@ -71887,11 +71914,10 @@
71887 sqlite3VdbeAddOp2(v, OP_RowData, iSrc, regData);
71888 sqlite3VdbeAddOp3(v, OP_Insert, iDest, regData, regRowid);
71889 sqlite3VdbeChangeP5(v, OPFLAG_NCHANGE|OPFLAG_LASTROWID|OPFLAG_APPEND);
71890 sqlite3VdbeChangeP4(v, -1, pDest->zName, 0);
71891 sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1);
71892 autoIncEnd(pParse, iDbDest, pDest, regAutoinc);
71893 for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
71894 for(pSrcIdx=pSrc->pIndex; ALWAYS(pSrcIdx); pSrcIdx=pSrcIdx->pNext){
71895 if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break;
71896 }
71897 assert( pSrcIdx );
@@ -74504,11 +74530,11 @@
74504 *************************************************************************
74505 ** This file contains the implementation of the sqlite3_prepare()
74506 ** interface, and routines that contribute to loading the database schema
74507 ** from disk.
74508 **
74509 ** $Id: prepare.c,v 1.124 2009/06/22 12:05:10 drh Exp $
74510 */
74511
74512 /*
74513 ** Fill the InitData structure with an error message that indicates
74514 ** that the database is corrupt.
@@ -74577,11 +74603,11 @@
74577 assert( rc!=SQLITE_OK || zErr==0 );
74578 if( SQLITE_OK!=rc ){
74579 pData->rc = rc;
74580 if( rc==SQLITE_NOMEM ){
74581 db->mallocFailed = 1;
74582 }else if( rc!=SQLITE_INTERRUPT ){
74583 corruptSchema(pData, argv[0], zErr);
74584 }
74585 sqlite3DbFree(db, zErr);
74586 }
74587 }else if( argv[0]==0 ){
@@ -80648,11 +80674,11 @@
80648 **
80649 *************************************************************************
80650 ** This file contains C code routines that are called by the parser
80651 ** to handle UPDATE statements.
80652 **
80653 ** $Id: update.c,v 1.202 2009/05/28 01:00:55 drh Exp $
80654 */
80655
80656 #ifndef SQLITE_OMIT_VIRTUALTABLE
80657 /* Forward declaration */
80658 static void updateVirtualTable(
@@ -81198,10 +81224,18 @@
81198 sqlite3VdbeAddOp2(v, OP_Close, iCur, 0);
81199 if( pTrigger ){
81200 sqlite3VdbeAddOp2(v, OP_Close, newIdx, 0);
81201 sqlite3VdbeAddOp2(v, OP_Close, oldIdx, 0);
81202 }
 
 
 
 
 
 
 
 
81203
81204 /*
81205 ** Return the number of rows that were changed. If this routine is
81206 ** generating code because of a call to sqlite3NestedParse(), do not
81207 ** invoke the callback function.
@@ -81296,21 +81330,20 @@
81296 sqlite3Select(pParse, pSelect, &dest);
81297
81298 /* Generate code to scan the ephemeral table and call VUpdate. */
81299 iReg = ++pParse->nMem;
81300 pParse->nMem += pTab->nCol+1;
81301 sqlite3VdbeAddOp2(v, OP_Rewind, ephemTab, 0);
81302 addr = sqlite3VdbeCurrentAddr(v);
81303 sqlite3VdbeAddOp3(v, OP_Column, ephemTab, 0, iReg);
81304 sqlite3VdbeAddOp3(v, OP_Column, ephemTab, (pRowid?1:0), iReg+1);
81305 for(i=0; i<pTab->nCol; i++){
81306 sqlite3VdbeAddOp3(v, OP_Column, ephemTab, i+1+(pRowid!=0), iReg+2+i);
81307 }
81308 sqlite3VtabMakeWritable(pParse, pTab);
81309 sqlite3VdbeAddOp4(v, OP_VUpdate, 0, pTab->nCol+2, iReg, pVtab, P4_VTAB);
81310 sqlite3VdbeAddOp2(v, OP_Next, ephemTab, addr);
81311 sqlite3VdbeJumpHere(v, addr-1);
81312 sqlite3VdbeAddOp2(v, OP_Close, ephemTab, 0);
81313
81314 /* Cleanup */
81315 sqlite3SelectDelete(db, pSelect);
81316 }
@@ -89455,11 +89488,11 @@
89455 **
89456 ** This file contains C code that splits an SQL input string up into
89457 ** individual tokens and sends those tokens one-by-one over to the
89458 ** parser for analysis.
89459 **
89460 ** $Id: tokenize.c,v 1.161 2009/06/17 01:17:13 drh Exp $
89461 */
89462
89463 /*
89464 ** The charMap() macro maps alphabetic characters into their
89465 ** lower-case ASCII equivalent. On ASCII machines, this is just
@@ -90226,10 +90259,15 @@
90226 }
90227
90228 sqlite3DeleteTrigger(db, pParse->pNewTrigger);
90229 sqlite3DbFree(db, pParse->apVarExpr);
90230 sqlite3DbFree(db, pParse->aAlias);
 
 
 
 
 
90231 while( pParse->pZombieTab ){
90232 Table *p = pParse->pZombieTab;
90233 pParse->pZombieTab = p->pNextZombie;
90234 sqlite3DeleteTable(p);
90235 }
@@ -90534,11 +90572,11 @@
90534 ** Main file for the SQLite library. The routines in this file
90535 ** implement the programmer interface to the library. Routines in
90536 ** other files are for internal use by SQLite and should not be
90537 ** accessed by users of the library.
90538 **
90539 ** $Id: main.c,v 1.558 2009/06/19 14:06:03 drh Exp $
90540 */
90541
90542 #ifdef SQLITE_ENABLE_FTS3
90543 /************** Include fts3.h in the middle of main.c ***********************/
90544 /************** Begin file fts3.h ********************************************/
@@ -91356,11 +91394,11 @@
91356 /* SQLITE_FULL */ "database or disk is full",
91357 /* SQLITE_CANTOPEN */ "unable to open database file",
91358 /* SQLITE_PROTOCOL */ 0,
91359 /* SQLITE_EMPTY */ "table contains no data",
91360 /* SQLITE_SCHEMA */ "database schema has changed",
91361 /* SQLITE_TOOBIG */ "String or BLOB exceeded size limit",
91362 /* SQLITE_CONSTRAINT */ "constraint failed",
91363 /* SQLITE_MISMATCH */ "datatype mismatch",
91364 /* SQLITE_MISUSE */ "library routine called out of sequence",
91365 /* SQLITE_NOLFS */ "large file support is disabled",
91366 /* SQLITE_AUTH */ "authorization denied",
@@ -92169,11 +92207,10 @@
92169 goto opendb_out;
92170 }
92171 }
92172 sqlite3_mutex_enter(db->mutex);
92173 db->errMask = 0xff;
92174 db->priorNewRowid = 0;
92175 db->nDb = 2;
92176 db->magic = SQLITE_MAGIC_BUSY;
92177 db->aDb = db->aDbStatic;
92178
92179 assert( sizeof(db->aLimit)==sizeof(aHardLimit) );
92180
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -1,8 +1,8 @@
1 /******************************************************************************
2 ** This file is an amalgamation of many separate C source files from SQLite
3 ** version 3.6.16. By combining all the individual C code files into this
4 ** single large file, the entire code can be compiled as a one translation
5 ** unit. This allows many compilers to do optimizations that would not be
6 ** possible if the files were compiled separately. Performance improvements
7 ** of 5% are more are commonly seen when SQLite is compiled as a single
8 ** translation unit.
@@ -15,11 +15,11 @@
15 ** needed if you want a wrapper to interface SQLite with your choice of
16 ** programming language. The code for the "sqlite3" command-line shell
17 ** is also in a separate file. This file contains only code for the core
18 ** SQLite library.
19 **
20 ** This amalgamation was generated on 2009-06-27 13:43:17 UTC.
21 */
22 #define SQLITE_CORE 1
23 #define SQLITE_AMALGAMATION 1
24 #ifndef SQLITE_PRIVATE
25 # define SQLITE_PRIVATE static
@@ -39,11 +39,11 @@
39 ** May you share freely, never taking more than you give.
40 **
41 *************************************************************************
42 ** Internal interface definitions for SQLite.
43 **
44 ** @(#) $Id: sqliteInt.h,v 1.890 2009/06/26 15:14:55 drh Exp $
45 */
46 #ifndef _SQLITEINT_H_
47 #define _SQLITEINT_H_
48
49 /*
@@ -489,13 +489,12 @@
489 */
490 #if defined(SQLITE_COVERAGE_TEST)
491 # define ALWAYS(X) (1)
492 # define NEVER(X) (0)
493 #elif !defined(NDEBUG)
494 # define ALWAYS(X) ((X)?1:(assert(0),0))
495 # define NEVER(X) ((X)?(assert(0),1):0)
 
496 #else
497 # define ALWAYS(X) (X)
498 # define NEVER(X) (X)
499 #endif
500
@@ -614,12 +613,12 @@
613 **
614 ** See also: [sqlite3_libversion()] and [sqlite3_libversion_number()].
615 **
616 ** Requirements: [H10011] [H10014]
617 */
618 #define SQLITE_VERSION "3.6.16"
619 #define SQLITE_VERSION_NUMBER 3006016
620
621 /*
622 ** CAPI3REF: Run-Time Library Version Numbers {H10020} <S60100>
623 ** KEYWORDS: sqlite3_version
624 **
@@ -6693,10 +6692,11 @@
6692 /*
6693 ** Forward references to structures
6694 */
6695 typedef struct AggInfo AggInfo;
6696 typedef struct AuthContext AuthContext;
6697 typedef struct AutoincInfo AutoincInfo;
6698 typedef struct Bitvec Bitvec;
6699 typedef struct RowSet RowSet;
6700 typedef struct CollSeq CollSeq;
6701 typedef struct Column Column;
6702 typedef struct Db Db;
@@ -8218,11 +8218,10 @@
8218 signed char nextAutovac; /* Autovac setting after VACUUM if >=0 */
8219 int nextPagesize; /* Pagesize after VACUUM if >0 */
8220 int nTable; /* Number of tables in the database */
8221 CollSeq *pDfltColl; /* The default collating sequence (BINARY) */
8222 i64 lastRowid; /* ROWID of most recent insert (see above) */
 
8223 u32 magic; /* Magic number for detect library misuse */
8224 int nChange; /* Value returned by sqlite3_changes() */
8225 int nTotalChange; /* Value returned by sqlite3_total_changes() */
8226 sqlite3_mutex *mutex; /* Connection mutex */
8227 int aLimit[SQLITE_N_LIMIT]; /* Limits */
@@ -9354,10 +9353,26 @@
9353 int iParm; /* A parameter used by the eDest disposal method */
9354 int iMem; /* Base register where results are written */
9355 int nMem; /* Number of registers allocated */
9356 };
9357
9358 /*
9359 ** During code generation of statements that do inserts into AUTOINCREMENT
9360 ** tables, the following information is attached to the Table.u.autoInc.p
9361 ** pointer of each autoincrement table to record some side information that
9362 ** the code generator needs. We have to keep per-table autoincrement
9363 ** information in case inserts are down within triggers. Triggers do not
9364 ** normally coordinate their activities, but we do need to coordinate the
9365 ** loading and saving of autoincrement information.
9366 */
9367 struct AutoincInfo {
9368 AutoincInfo *pNext; /* Next info block in a list of them all */
9369 Table *pTab; /* Table this info block refers to */
9370 int iDb; /* Index in sqlite3.aDb[] of database holding pTab */
9371 int regCtr; /* Memory register holding the rowid counter */
9372 };
9373
9374 /*
9375 ** Size of the column cache
9376 */
9377 #ifndef SQLITE_N_COLCACHE
9378 # define SQLITE_N_COLCACHE 10
@@ -9420,10 +9435,11 @@
9435 int nTableLock; /* Number of locks in aTableLock */
9436 TableLock *aTableLock; /* Required table locks for shared-cache mode */
9437 #endif
9438 int regRowid; /* Register holding rowid of CREATE TABLE entry */
9439 int regRoot; /* Register holding root page number for new objects */
9440 AutoincInfo *pAinc; /* Information about AUTOINCREMENT counters */
9441
9442 /* Above is constant between recursions. Below is reset before and after
9443 ** each recursion */
9444
9445 int nVar; /* Number of '?' variables seen in the SQL so far */
@@ -9910,10 +9926,17 @@
9926 # define sqlite3ViewGetColumnNames(A,B) 0
9927 #endif
9928
9929 SQLITE_PRIVATE void sqlite3DropTable(Parse*, SrcList*, int, int);
9930 SQLITE_PRIVATE void sqlite3DeleteTable(Table*);
9931 #ifndef SQLITE_OMIT_AUTOINCREMENT
9932 SQLITE_PRIVATE void sqlite3AutoincrementBegin(Parse *pParse);
9933 SQLITE_PRIVATE void sqlite3AutoincrementEnd(Parse *pParse);
9934 #else
9935 # define sqlite3AutoincrementBegin(X)
9936 # define sqlite3AutoincrementEnd(X)
9937 #endif
9938 SQLITE_PRIVATE void sqlite3Insert(Parse*, SrcList*, ExprList*, Select*, IdList*, int);
9939 SQLITE_PRIVATE void *sqlite3ArrayAllocate(sqlite3*,void*,int,int,int*,int*,int*);
9940 SQLITE_PRIVATE IdList *sqlite3IdListAppend(sqlite3*, IdList*, Token*);
9941 SQLITE_PRIVATE int sqlite3IdListIndex(IdList*,const char*);
9942 SQLITE_PRIVATE SrcList *sqlite3SrcListEnlarge(sqlite3*, SrcList*, int, int);
@@ -15212,11 +15235,11 @@
15235 **
15236 *************************************************************************
15237 **
15238 ** Memory allocation functions used throughout sqlite.
15239 **
15240 ** $Id: malloc.c,v 1.64 2009/06/27 00:48:33 drh Exp $
15241 */
15242
15243 /*
15244 ** This routine runs when the memory allocator sees that the
15245 ** total memory allocation is about to exceed the soft heap
@@ -15464,19 +15487,16 @@
15487 ** Allocate memory. This routine is like sqlite3_malloc() except that it
15488 ** assumes the memory subsystem has already been initialized.
15489 */
15490 SQLITE_PRIVATE void *sqlite3Malloc(int n){
15491 void *p;
15492 if( n<=0 || n>=0x7fffff00 ){
15493 /* A memory allocation of a number of bytes which is near the maximum
15494 ** signed integer value might cause an integer overflow inside of the
15495 ** xMalloc(). Hence we limit the maximum size to 0x7fffff00, giving
15496 ** 255 bytes of overhead. SQLite itself will never use anything near
15497 ** this amount. The only way to reach the limit is with sqlite3_malloc() */
 
 
 
15498 p = 0;
15499 }else if( sqlite3GlobalConfig.bMemstat ){
15500 sqlite3_mutex_enter(mem0.mutex);
15501 mallocWithAlarm(n, &p);
15502 sqlite3_mutex_leave(mem0.mutex);
@@ -15674,14 +15694,17 @@
15694 int nOld, nNew;
15695 void *pNew;
15696 if( pOld==0 ){
15697 return sqlite3Malloc(nBytes);
15698 }
15699 if( nBytes<=0 ){
 
15700 sqlite3_free(pOld);
15701 return 0;
15702 }
15703 if( nBytes>=0x7fffff00 ){
15704 /* The 0x7ffff00 limit term is explained in comments on sqlite3Malloc() */
15705 return 0;
15706 }
15707 nOld = sqlite3MallocSize(pOld);
15708 if( sqlite3GlobalConfig.bMemstat ){
15709 sqlite3_mutex_enter(mem0.mutex);
15710 sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, nBytes);
@@ -17977,11 +18000,11 @@
18000 ** Utility functions used throughout sqlite.
18001 **
18002 ** This file contains functions for allocating memory, comparing
18003 ** strings, and stuff like that.
18004 **
18005 ** $Id: util.c,v 1.261 2009/06/24 10:26:33 drh Exp $
18006 */
18007 #ifdef SQLITE_HAVE_ISNAN
18008 # include <math.h>
18009 #endif
18010
@@ -17991,31 +18014,10 @@
18014 #ifdef SQLITE_COVERAGE_TEST
18015 SQLITE_PRIVATE void sqlite3Coverage(int x){
18016 static int dummy = 0;
18017 dummy += x;
18018 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18019 #endif
18020
18021 /*
18022 ** Return true if the floating point value is Not a Number (NaN).
18023 **
@@ -30495,11 +30497,11 @@
30497 ** is separate from the database file. The pager also implements file
30498 ** locking to prevent two processes from writing the same database
30499 ** file simultaneously, or one process from reading the database while
30500 ** another is writing.
30501 **
30502 ** @(#) $Id: pager.c,v 1.603 2009/06/26 12:15:23 drh Exp $
30503 */
30504 #ifndef SQLITE_OMIT_DISKIO
30505
30506 /*
30507 ** Macros for troubleshooting. Normally turned off
@@ -31233,11 +31235,10 @@
31235 pPager->aSavepoint[ii].iHdrOffset = pPager->journalOff;
31236 }
31237 }
31238
31239 pPager->journalHdr = pPager->journalOff = journalHdrOffset(pPager);
 
31240
31241 /*
31242 ** Write the nRec Field - the number of page records that follow this
31243 ** journal header. Normally, zero is written to this value at this time.
31244 ** After the records are added to the journal (and the journal synced,
@@ -31259,12 +31260,14 @@
31260 */
31261 assert( isOpen(pPager->fd) || pPager->noSync );
31262 if( (pPager->noSync) || (pPager->journalMode==PAGER_JOURNALMODE_MEMORY)
31263 || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND)
31264 ){
31265 memcpy(zHeader, aJournalMagic, sizeof(aJournalMagic));
31266 put32bits(&zHeader[sizeof(aJournalMagic)], 0xffffffff);
31267 }else{
31268 zHeader[0] = '\0';
31269 put32bits(&zHeader[sizeof(aJournalMagic)], 0);
31270 }
31271
31272 /* The random check-hash initialiser */
31273 sqlite3_randomness(sizeof(pPager->cksumInit), &pPager->cksumInit);
@@ -31328,10 +31331,11 @@
31331 ** returned and *pNRec and *PDbSize are undefined. If JOURNAL_HDR_SZ bytes
31332 ** cannot be read from the journal file an error code is returned.
31333 */
31334 static int readJournalHdr(
31335 Pager *pPager, /* Pager object */
31336 int isHot,
31337 i64 journalSize, /* Size of the open journal file in bytes */
31338 u32 *pNRec, /* OUT: Value read from the nRec field */
31339 u32 *pDbSize /* OUT: Value of original database size field */
31340 ){
31341 int rc; /* Return code */
@@ -31353,16 +31357,18 @@
31357 /* Read in the first 8 bytes of the journal header. If they do not match
31358 ** the magic string found at the start of each journal header, return
31359 ** SQLITE_DONE. If an IO error occurs, return an error code. Otherwise,
31360 ** proceed.
31361 */
31362 if( isHot || iHdrOff!=pPager->journalHdr ){
31363 rc = sqlite3OsRead(pPager->jfd, aMagic, sizeof(aMagic), iHdrOff);
31364 if( rc ){
31365 return rc;
31366 }
31367 if( memcmp(aMagic, aJournalMagic, sizeof(aMagic))!=0 ){
31368 return SQLITE_DONE;
31369 }
31370 }
31371
31372 /* Read the first three 32-bit fields of the journal header: The nRec
31373 ** field, the checksum-initializer and the database size at the start
31374 ** of the transaction. Return an error code if anything goes wrong.
@@ -32453,11 +32459,11 @@
32459 /* Read the next journal header from the journal file. If there are
32460 ** not enough bytes left in the journal file for a complete header, or
32461 ** it is corrupted, then a process must of failed while writing it.
32462 ** This indicates nothing more needs to be rolled back.
32463 */
32464 rc = readJournalHdr(pPager, isHot, szJ, &nRec, &mxPg);
32465 if( rc!=SQLITE_OK ){
32466 if( rc==SQLITE_DONE ){
32467 rc = SQLITE_OK;
32468 }
32469 goto end_playback;
@@ -32673,11 +32679,11 @@
32679 */
32680 while( rc==SQLITE_OK && pPager->journalOff<szJ ){
32681 u32 ii; /* Loop counter */
32682 u32 nJRec = 0; /* Number of Journal Records */
32683 u32 dummy;
32684 rc = readJournalHdr(pPager, 0, szJ, &nJRec, &dummy);
32685 assert( rc!=SQLITE_DONE );
32686
32687 /*
32688 ** The "pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff"
32689 ** test is related to ticket #2565. See the discussion in the
@@ -33236,16 +33242,10 @@
33242 int rc; /* Return code */
33243 const int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
33244 assert( isOpen(pPager->jfd) );
33245
33246 if( 0==(iDc&SQLITE_IOCAP_SAFE_APPEND) ){
 
 
 
 
 
 
33247 /* This block deals with an obscure problem. If the last connection
33248 ** that wrote to this database was operating in persistent-journal
33249 ** mode, then the journal file may at this point actually be larger
33250 ** than Pager.journalOff bytes. If the next thing in the journal
33251 ** file happens to be a journal-header (written as part of the
@@ -33264,12 +33264,18 @@
33264 ** Variable iNextHdrOffset is set to the offset at which this
33265 ** problematic header will occur, if it exists. aMagic is used
33266 ** as a temporary buffer to inspect the first couple of bytes of
33267 ** the potential journal header.
33268 */
33269 i64 iNextHdrOffset;
33270 u8 aMagic[8];
33271 u8 zHeader[sizeof(aJournalMagic)+4];
33272
33273 memcpy(zHeader, aJournalMagic, sizeof(aJournalMagic));
33274 put32bits(&zHeader[sizeof(aJournalMagic)], pPager->nRec);
33275
33276 iNextHdrOffset = journalHdrOffset(pPager);
33277 rc = sqlite3OsRead(pPager->jfd, aMagic, 8, iNextHdrOffset);
33278 if( rc==SQLITE_OK && 0==memcmp(aMagic, aJournalMagic, 8) ){
33279 static const u8 zerobyte = 0;
33280 rc = sqlite3OsWrite(pPager->jfd, &zerobyte, 1, iNextHdrOffset);
33281 }
@@ -33292,12 +33298,14 @@
33298 PAGERTRACE(("SYNC journal of %d\n", PAGERID(pPager)));
33299 IOTRACE(("JSYNC %p\n", pPager))
33300 rc = sqlite3OsSync(pPager->jfd, pPager->sync_flags);
33301 if( rc!=SQLITE_OK ) return rc;
33302 }
33303 IOTRACE(("JHDR %p %lld\n", pPager, pPager->journalHdr));
33304 rc = sqlite3OsWrite(
33305 pPager->jfd, zHeader, sizeof(zHeader), pPager->journalHdr
33306 );
33307 if( rc!=SQLITE_OK ) return rc;
33308 }
33309 if( 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
33310 PAGERTRACE(("SYNC journal of %d\n", PAGERID(pPager)));
33311 IOTRACE(("JSYNC %p\n", pPager))
@@ -35864,11 +35872,11 @@
35872 ** May you do good and not evil.
35873 ** May you find forgiveness for yourself and forgive others.
35874 ** May you share freely, never taking more than you give.
35875 **
35876 *************************************************************************
35877 ** $Id: btreeInt.h,v 1.49 2009/06/24 05:40:34 danielk1977 Exp $
35878 **
35879 ** This file implements a external (disk-based) database using BTrees.
35880 ** For a detailed discussion of BTrees, refer to
35881 **
35882 ** Donald E. Knuth, THE ART OF COMPUTER PROGRAMMING, Volume 3:
@@ -36330,13 +36338,10 @@
36338 int skip; /* (skip<0) -> Prev() is a no-op. (skip>0) -> Next() is */
36339 #ifndef SQLITE_OMIT_INCRBLOB
36340 u8 isIncrblobHandle; /* True if this cursor is an incr. io handle */
36341 Pgno *aOverflow; /* Cache of overflow page locations */
36342 #endif
 
 
 
36343 i16 iPage; /* Index of current page in apPage */
36344 MemPage *apPage[BTCURSOR_MAX_DEPTH]; /* Pages from root to current page */
36345 u16 aiIdx[BTCURSOR_MAX_DEPTH]; /* Current index in apPage[i] */
36346 };
36347
@@ -36858,11 +36863,11 @@
36863 ** May you do good and not evil.
36864 ** May you find forgiveness for yourself and forgive others.
36865 ** May you share freely, never taking more than you give.
36866 **
36867 *************************************************************************
36868 ** $Id: btree.c,v 1.645 2009/06/26 16:32:13 shane Exp $
36869 **
36870 ** This file implements a external (disk-based) database using BTrees.
36871 ** See the header comment on "btreeInt.h" for additional information.
36872 ** Including a description of file format and an overview of operation.
36873 */
@@ -37785,11 +37790,10 @@
37790 */
37791 assert( nByte <= (
37792 get2byte(&data[hdr+5])-(hdr+8+(pPage->leaf?0:4)+2*get2byte(&data[hdr+3]))
37793 ));
37794
 
37795 nFrag = data[hdr+7];
37796 if( nFrag>=60 ){
37797 defragmentPage(pPage);
37798 }else{
37799 /* Search the freelist looking for a free slot big enough to satisfy
@@ -37864,11 +37868,11 @@
37868 }
37869 assert( pbegin>addr || pbegin==0 );
37870 put2byte(&data[addr], start);
37871 put2byte(&data[start], pbegin);
37872 put2byte(&data[start+2], size);
37873 pPage->nFree = pPage->nFree + (u16)size;
37874
37875 /* Coalesce adjacent free blocks */
37876 addr = pPage->hdrOffset + 1;
37877 while( (pbegin = get2byte(&data[addr]))>0 ){
37878 int pnext, psize, x;
@@ -38963,14 +38967,14 @@
38967 ** If there is a transaction in progress, this routine is a no-op.
38968 */
38969 static void unlockBtreeIfUnused(BtShared *pBt){
38970 assert( sqlite3_mutex_held(pBt->mutex) );
38971 if( pBt->inTransaction==TRANS_NONE && pBt->pCursor==0 && pBt->pPage1!=0 ){
38972 assert( pBt->pPage1->aData );
38973 assert( sqlite3PagerRefcount(pBt->pPager)==1 );
38974 assert( pBt->pPage1->aData );
38975 releasePage(pBt->pPage1);
38976 pBt->pPage1 = 0;
38977 }
38978 }
38979
38980 /*
@@ -41961,11 +41965,11 @@
41965 assert( end <= get2byte(&data[hdr+5]) );
41966 if (idx+sz > pPage->pBt->usableSize) {
41967 return SQLITE_CORRUPT_BKPT;
41968 }
41969 pPage->nCell++;
41970 pPage->nFree = pPage->nFree - (u16)(2 + sz);
41971 memcpy(&data[idx+nSkip], pCell+nSkip, sz-nSkip);
41972 if( iChild ){
41973 put4byte(&data[idx], iChild);
41974 }
41975 for(j=end-2, ptr=&data[j]; j>ins; j-=2, ptr-=2){
@@ -41977,11 +41981,11 @@
41981 #ifndef SQLITE_OMIT_AUTOVACUUM
41982 if( pPage->pBt->autoVacuum ){
41983 /* The cell may contain a pointer to an overflow page. If so, write
41984 ** the entry for the overflow page into the pointer map.
41985 */
41986 return ptrmapPutOvflPtr(pPage, pCell);
41987 }
41988 #endif
41989 }
41990
41991 return SQLITE_OK;
@@ -42186,10 +42190,59 @@
42190 }
42191 return 1;
42192 }
42193 #endif
42194
42195 /*
42196 ** This function is used to copy the contents of the b-tree node stored
42197 ** on page pFrom to page pTo. If page pFrom was not a leaf page, then
42198 ** the pointer-map entries for each child page are updated so that the
42199 ** parent page stored in the pointer map is page pTo. If pFrom contained
42200 ** any cells with overflow page pointers, then the corresponding pointer
42201 ** map entries are also updated so that the parent page is page pTo.
42202 **
42203 ** If pFrom is currently carrying any overflow cells (entries in the
42204 ** MemPage.aOvfl[] array), they are not copied to pTo.
42205 **
42206 ** Before returning, page pTo is reinitialized using sqlite3BtreeInitPage().
42207 **
42208 ** The performance of this function is not critical. It is only used by
42209 ** the balance_shallower() and balance_deeper() procedures, neither of
42210 ** which are called often under normal circumstances.
42211 */
42212 static int copyNodeContent(MemPage *pFrom, MemPage *pTo){
42213 BtShared * const pBt = pFrom->pBt;
42214 u8 * const aFrom = pFrom->aData;
42215 u8 * const aTo = pTo->aData;
42216 int const iFromHdr = pFrom->hdrOffset;
42217 int const iToHdr = ((pTo->pgno==1) ? 100 : 0);
42218 int rc = SQLITE_OK;
42219 int iData;
42220
42221 assert( pFrom->isInit );
42222 assert( pFrom->nFree>=iToHdr );
42223 assert( get2byte(&aFrom[iFromHdr+5])<=pBt->usableSize );
42224
42225 /* Copy the b-tree node content from page pFrom to page pTo. */
42226 iData = get2byte(&aFrom[iFromHdr+5]);
42227 memcpy(&aTo[iData], &aFrom[iData], pBt->usableSize-iData);
42228 memcpy(&aTo[iToHdr], &aFrom[iFromHdr], pFrom->cellOffset + 2*pFrom->nCell);
42229
42230 /* Reinitialize page pTo so that the contents of the MemPage structure
42231 ** match the new data. The initialization of pTo "cannot" fail, as the
42232 ** data copied from pFrom is known to be valid. */
42233 pTo->isInit = 0;
42234 TESTONLY(rc = ) sqlite3BtreeInitPage(pTo);
42235 assert( rc==SQLITE_OK );
42236
42237 /* If this is an auto-vacuum database, update the pointer-map entries
42238 ** for any b-tree or overflow pages that pTo now contains the pointers to. */
42239 if( ISAUTOVACUUM ){
42240 rc = setChildPtrmaps(pTo);
42241 }
42242 return rc;
42243 }
42244
42245 /*
42246 ** This routine redistributes cells on the iParentIdx'th child of pParent
42247 ** (hereafter "the page") and up to 2 siblings so that all pages have about the
42248 ** same amount of free space. Usually a single sibling on either side of the
@@ -42230,21 +42283,22 @@
42283 ** SQLITE_NOMEM.
42284 */
42285 static int balance_nonroot(
42286 MemPage *pParent, /* Parent page of siblings being balanced */
42287 int iParentIdx, /* Index of "the page" in pParent */
42288 u8 *aOvflSpace, /* page-size bytes of space for parent ovfl */
42289 int isRoot /* True if pParent is a root-page */
42290 ){
42291 BtShared *pBt; /* The whole database */
42292 int nCell = 0; /* Number of cells in apCell[] */
42293 int nMaxCells = 0; /* Allocated size of apCell, szCell, aFrom. */
42294 int nNew = 0; /* Number of pages in apNew[] */
42295 int nOld; /* Number of pages in apOld[] */
42296 int i, j, k; /* Loop counters */
42297 int nxDiv; /* Next divider slot in pParent->aCell[] */
42298 int rc = SQLITE_OK; /* The return code */
42299 u16 leafCorrection; /* 4 if pPage is a leaf. 0 if not */
42300 int leafData; /* True if pPage is a leaf of a LEAFDATA tree */
42301 int usableSpace; /* Bytes in pPage beyond the header */
42302 int pageFlags; /* Value of pPage->aData[0] */
42303 int subtotal; /* Subtotal of bytes in cells on one page */
42304 int iSpace1 = 0; /* First unused byte of aSpace1[] */
@@ -42409,11 +42463,11 @@
42463 apCell[nCell] = findOverflowCell(pOld, j);
42464 szCell[nCell] = cellSizePtr(pOld, apCell[nCell]);
42465 nCell++;
42466 }
42467 if( i<nOld-1 && !leafData){
42468 u16 sz = (u16)szNew[i];
42469 u8 *pTemp;
42470 assert( nCell<nMaxCells );
42471 szCell[nCell] = sz;
42472 pTemp = &aSpace1[iSpace1];
42473 iSpace1 += sz;
@@ -42420,11 +42474,11 @@
42474 assert( sz<=pBt->pageSize/4 );
42475 assert( iSpace1<=pBt->pageSize );
42476 memcpy(pTemp, apDiv[i], sz);
42477 apCell[nCell] = pTemp+leafCorrection;
42478 assert( leafCorrection==0 || leafCorrection==4 );
42479 szCell[nCell] = szCell[nCell] - leafCorrection;
42480 if( !pOld->leaf ){
42481 assert( leafCorrection==0 );
42482 assert( pOld->hdrOffset==0 );
42483 /* The right pointer of the child page pOld becomes the left
42484 ** pointer of the divider cell */
@@ -42618,11 +42672,12 @@
42672 j = cntNew[i];
42673
42674 /* If the sibling page assembled above was not the right-most sibling,
42675 ** insert a divider cell into the parent page.
42676 */
42677 assert( i<nNew-1 || j==nCell );
42678 if( j<nCell ){
42679 u8 *pCell;
42680 u8 *pTemp;
42681 int sz;
42682
42683 assert( j<nMaxCells );
@@ -42678,42 +42733,64 @@
42733 if( (pageFlags & PTF_LEAF)==0 ){
42734 u8 *zChild = &apCopy[nOld-1]->aData[8];
42735 memcpy(&apNew[nNew-1]->aData[8], zChild, 4);
42736 }
42737
42738 if( isRoot && pParent->nCell==0 && pParent->hdrOffset<=apNew[0]->nFree ){
42739 /* The root page of the b-tree now contains no cells. The only sibling
42740 ** page is the right-child of the parent. Copy the contents of the
42741 ** child page into the parent, decreasing the overall height of the
42742 ** b-tree structure by one. This is described as the "balance-shallower"
42743 ** sub-algorithm in some documentation.
42744 **
42745 ** If this is an auto-vacuum database, the call to copyNodeContent()
42746 ** sets all pointer-map entries corresponding to database image pages
42747 ** for which the pointer is stored within the content being copied.
42748 **
42749 ** The second assert below verifies that the child page is defragmented
42750 ** (it must be, as it was just reconstructed using assemblePage()). This
42751 ** is important if the parent page happens to be page 1 of the database
42752 ** image. */
42753 assert( nNew==1 );
42754 assert( apNew[0]->nFree ==
42755 (get2byte(&apNew[0]->aData[5])-apNew[0]->cellOffset-apNew[0]->nCell*2)
42756 );
42757 if( SQLITE_OK==(rc = copyNodeContent(apNew[0], pParent)) ){
42758 rc = freePage(apNew[0]);
42759 }
42760 }else if( ISAUTOVACUUM ){
42761 /* Fix the pointer-map entries for all the cells that were shifted around.
42762 ** There are several different types of pointer-map entries that need to
42763 ** be dealt with by this routine. Some of these have been set already, but
42764 ** many have not. The following is a summary:
42765 **
42766 ** 1) The entries associated with new sibling pages that were not
42767 ** siblings when this function was called. These have already
42768 ** been set. We don't need to worry about old siblings that were
42769 ** moved to the free-list - the freePage() code has taken care
42770 ** of those.
42771 **
42772 ** 2) The pointer-map entries associated with the first overflow
42773 ** page in any overflow chains used by new divider cells. These
42774 ** have also already been taken care of by the insertCell() code.
42775 **
42776 ** 3) If the sibling pages are not leaves, then the child pages of
42777 ** cells stored on the sibling pages may need to be updated.
42778 **
42779 ** 4) If the sibling pages are not internal intkey nodes, then any
42780 ** overflow pages used by these cells may need to be updated
42781 ** (internal intkey nodes never contain pointers to overflow pages).
42782 **
42783 ** 5) If the sibling pages are not leaves, then the pointer-map
42784 ** entries for the right-child pages of each sibling may need
42785 ** to be updated.
42786 **
42787 ** Cases 1 and 2 are dealt with above by other code. The next
42788 ** block deals with cases 3 and 4 and the one after that, case 5. Since
42789 ** setting a pointer map entry is a relatively expensive operation, this
42790 ** code only sets pointer map entries for child or overflow pages that have
42791 ** actually moved between pages. */
42792 MemPage *pNew = apNew[0];
42793 MemPage *pOld = apCopy[0];
42794 int nOverflow = pOld->nOverflow;
42795 int iNextOld = pOld->nCell + nOverflow;
42796 int iOverflow = (nOverflow ? pOld->aOvfl[0].idx : -1);
@@ -42787,11 +42864,11 @@
42864 }
42865
42866 assert( pParent->isInit );
42867 TRACE(("BALANCE: finished: old=%d new=%d cells=%d\n",
42868 nOld, nNew, nCell));
42869
42870 /*
42871 ** Cleanup before returning.
42872 */
42873 balance_cleanup:
42874 sqlite3ScratchFree(apCell);
@@ -42800,113 +42877,10 @@
42877 }
42878 for(i=0; i<nNew; i++){
42879 releasePage(apNew[i]);
42880 }
42881
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42882 return rc;
42883 }
42884
42885
42886 /*
@@ -42974,18 +42948,12 @@
42948 ** some way. This function figures out if this modification means the
42949 ** tree needs to be balanced, and if so calls the appropriate balancing
42950 ** routine. Balancing routines are:
42951 **
42952 ** balance_quick()
 
42953 ** balance_deeper()
42954 ** balance_nonroot()
 
 
 
 
 
42955 */
42956 static int balance(BtCursor *pCur){
42957 int rc = SQLITE_OK;
42958 const int nMin = pCur->pBt->usableSize * 2 / 3;
42959 u8 aBalanceQuickSpace[13];
@@ -43011,25 +42979,11 @@
42979 pCur->iPage = 1;
42980 pCur->aiIdx[0] = 0;
42981 pCur->aiIdx[1] = 0;
42982 assert( pCur->apPage[1]->nOverflow );
42983 }
 
42984 }else{
 
 
 
 
 
 
 
 
 
 
 
 
 
42985 break;
42986 }
42987 }else if( pPage->nOverflow==0 && pPage->nFree<=nMin ){
42988 break;
42989 }else{
@@ -43079,11 +43033,11 @@
43033 ** the previous call, as the overflow cell data will have been
43034 ** copied either into the body of a database page or into the new
43035 ** pSpace buffer passed to the latter call to balance_nonroot().
43036 */
43037 u8 *pSpace = sqlite3PageMalloc(pCur->pBt->pageSize);
43038 rc = balance_nonroot(pParent, iIdx, pSpace, iPage==1);
43039 if( pFree ){
43040 /* If pFree is not NULL, it points to the pSpace buffer used
43041 ** by a previous call to balance_nonroot(). Its contents are
43042 ** now stored either on real database pages or within the
43043 ** new pSpace buffer, so it may be safely freed here. */
@@ -43092,11 +43046,10 @@
43046
43047 /* The pSpace buffer will be freed after the next call to
43048 ** balance_nonroot(), or just before this function returns, whichever
43049 ** comes first. */
43050 pFree = pSpace;
 
43051 }
43052 }
43053
43054 pPage->nOverflow = 0;
43055
@@ -45290,11 +45243,11 @@
45243 ** This file contains code use to manipulate "Mem" structure. A "Mem"
45244 ** stores a single value in the VDBE. Mem is an opaque structure visible
45245 ** only within the VDBE. Interface routines refer to a Mem using the
45246 ** name sqlite_value
45247 **
45248 ** $Id: vdbemem.c,v 1.150 2009/06/25 01:47:12 drh Exp $
45249 */
45250
45251 /*
45252 ** Call sqlite3VdbeMemExpandBlob() on the supplied value (type Mem*)
45253 ** P if required.
@@ -45369,11 +45322,11 @@
45322 sqlite3DbFree(pMem->db, pMem->zMalloc);
45323 pMem->zMalloc = sqlite3DbMallocRaw(pMem->db, n);
45324 }
45325 }
45326
45327 if( pMem->z && preserve && pMem->zMalloc && pMem->z!=pMem->zMalloc ){
45328 memcpy(pMem->zMalloc, pMem->z, pMem->n);
45329 }
45330 if( pMem->flags&MEM_Dyn && pMem->xDel ){
45331 pMem->xDel((void *)(pMem->z));
45332 }
@@ -45518,11 +45471,11 @@
45471 ** Return SQLITE_ERROR if the finalizer reports an error. SQLITE_OK
45472 ** otherwise.
45473 */
45474 SQLITE_PRIVATE int sqlite3VdbeMemFinalize(Mem *pMem, FuncDef *pFunc){
45475 int rc = SQLITE_OK;
45476 if( ALWAYS(pFunc && pFunc->xFinalize) ){
45477 sqlite3_context ctx;
45478 assert( (pMem->flags & MEM_Null)!=0 || pFunc==pMem->u.pDef );
45479 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
45480 memset(&ctx, 0, sizeof(ctx));
45481 ctx.s.flags = MEM_Null;
@@ -45531,11 +45484,11 @@
45484 ctx.pFunc = pFunc;
45485 pFunc->xFinalize(&ctx);
45486 assert( 0==(pMem->flags&MEM_Dyn) && !pMem->xDel );
45487 sqlite3DbFree(pMem->db, pMem->zMalloc);
45488 memcpy(pMem, &ctx.s, sizeof(ctx.s));
45489 rc = ctx.isError;
45490 }
45491 return rc;
45492 }
45493
45494 /*
@@ -45806,16 +45759,13 @@
45759 ** empty boolean index.
45760 */
45761 SQLITE_PRIVATE void sqlite3VdbeMemSetRowSet(Mem *pMem){
45762 sqlite3 *db = pMem->db;
45763 assert( db!=0 );
45764 assert( (pMem->flags & MEM_RowSet)==0 );
45765 sqlite3VdbeMemRelease(pMem);
45766 pMem->zMalloc = sqlite3DbMallocRaw(db, 64);
 
 
 
45767 if( db->mallocFailed ){
45768 pMem->flags = MEM_Null;
45769 }else{
45770 assert( pMem->zMalloc );
45771 pMem->u.pRowSet = sqlite3RowSetInit(db, pMem->zMalloc,
@@ -46153,11 +46103,11 @@
46103 }else{
46104 zData = (char *)sqlite3BtreeDataFetch(pCur, &available);
46105 }
46106 assert( zData!=0 );
46107
46108 if( offset+amt<=available && (pMem->flags&MEM_Dyn)==0 ){
46109 sqlite3VdbeMemRelease(pMem);
46110 pMem->z = &zData[offset];
46111 pMem->flags = MEM_Blob|MEM_Ephem;
46112 }else if( SQLITE_OK==(rc = sqlite3VdbeMemGrow(pMem, amt+2, 0)) ){
46113 pMem->flags = MEM_Blob|MEM_Dyn|MEM_Term;
@@ -46370,11 +46320,11 @@
46320 ** This file contains code used for creating, destroying, and populating
46321 ** a VDBE (or an "sqlite3_stmt" as it is known to the outside world.) Prior
46322 ** to version 2.8.7, all this code was combined into the vdbe.c source file.
46323 ** But that file was getting too big so this subroutines were split out.
46324 **
46325 ** $Id: vdbeaux.c,v 1.467 2009/06/26 16:32:13 shane Exp $
46326 */
46327
46328
46329
46330 /*
@@ -46499,11 +46449,11 @@
46449 i = p->nOp;
46450 assert( p->magic==VDBE_MAGIC_INIT );
46451 assert( op>0 && op<0xff );
46452 if( p->nOpAlloc<=i ){
46453 if( growOpArray(p) ){
46454 return 1;
46455 }
46456 }
46457 p->nOp++;
46458 pOp = &p->aOp[i];
46459 pOp->opcode = (u8)op;
@@ -46704,11 +46654,11 @@
46654 assert( p->magic==VDBE_MAGIC_INIT );
46655 if( p->nOp + nOp > p->nOpAlloc && growOpArray(p) ){
46656 return 0;
46657 }
46658 addr = p->nOp;
46659 if( ALWAYS(nOp>0) ){
46660 int i;
46661 VdbeOpList const *pIn = aOp;
46662 for(i=0; i<nOp; i++, pIn++){
46663 int p2 = pIn->p2;
46664 VdbeOp *pOut = &p->aOp[i+addr];
@@ -46740,44 +46690,47 @@
46690 ** This routine is useful when a large program is loaded from a
46691 ** static array using sqlite3VdbeAddOpList but we want to make a
46692 ** few minor changes to the program.
46693 */
46694 SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe *p, int addr, int val){
46695 assert( p!=0 );
46696 assert( addr>=0 );
46697 if( p->nOp>addr ){
46698 p->aOp[addr].p1 = val;
46699 }
46700 }
46701
46702 /*
46703 ** Change the value of the P2 operand for a specific instruction.
46704 ** This routine is useful for setting a jump destination.
46705 */
46706 SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe *p, int addr, int val){
46707 assert( p!=0 );
46708 assert( addr>=0 );
46709 if( p->nOp>addr ){
46710 p->aOp[addr].p2 = val;
46711 }
46712 }
46713
46714 /*
46715 ** Change the value of the P3 operand for a specific instruction.
46716 */
46717 SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe *p, int addr, int val){
46718 assert( p!=0 );
46719 assert( addr>=0 );
46720 if( p->nOp>addr ){
46721 p->aOp[addr].p3 = val;
46722 }
46723 }
46724
46725 /*
46726 ** Change the value of the P5 operand for the most recently
46727 ** added operation.
46728 */
46729 SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe *p, u8 val){
46730 assert( p!=0 );
46731 if( p->aOp ){
46732 assert( p->nOp>0 );
46733 p->aOp[p->nOp-1].p5 = val;
46734 }
46735 }
46736
@@ -46793,11 +46746,11 @@
46746 /*
46747 ** If the input FuncDef structure is ephemeral, then free it. If
46748 ** the FuncDef is not ephermal, then do nothing.
46749 */
46750 static void freeEphemeralFunction(sqlite3 *db, FuncDef *pDef){
46751 if( ALWAYS(pDef) && (pDef->flags & SQLITE_FUNC_EPHEM)!=0 ){
46752 sqlite3DbFree(db, pDef);
46753 }
46754 }
46755
46756 /*
@@ -46838,11 +46791,11 @@
46791
46792 /*
46793 ** Change N opcodes starting at addr to No-ops.
46794 */
46795 SQLITE_PRIVATE void sqlite3VdbeChangeToNoop(Vdbe *p, int addr, int N){
46796 if( p->aOp ){
46797 VdbeOp *pOp = &p->aOp[addr];
46798 sqlite3 *db = p->db;
46799 while( N-- ){
46800 freeP4(db, pOp->p4type, pOp->p4.p);
46801 memset(pOp, 0, sizeof(pOp[0]));
@@ -46887,14 +46840,14 @@
46840 if (n != P4_KEYINFO) {
46841 freeP4(db, n, (void*)*(char**)&zP4);
46842 }
46843 return;
46844 }
46845 assert( p->nOp>0 );
46846 assert( addr<p->nOp );
46847 if( addr<0 ){
46848 addr = p->nOp - 1;
 
46849 }
46850 pOp = &p->aOp[addr];
46851 freeP4(db, pOp->p4type, pOp->p4.p);
46852 pOp->p4.p = 0;
46853 if( n==P4_INT32 ){
@@ -47498,13 +47451,13 @@
47451 }
47452 zCsr = p->pFree;
47453 zEnd = &zCsr[nByte];
47454 }while( nByte && !db->mallocFailed );
47455
47456 p->nCursor = (u16)nCursor;
47457 if( p->aVar ){
47458 p->nVar = (u16)nVar;
47459 for(n=0; n<nVar; n++){
47460 p->aVar[n].flags = MEM_Null;
47461 p->aVar[n].db = db;
47462 }
47463 }
@@ -47632,11 +47585,11 @@
47585 sqlite3 *db = p->db;
47586
47587 releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
47588 sqlite3DbFree(db, p->aColName);
47589 n = nResColumn*COLNAME_N;
47590 p->nResColumn = (u16)nResColumn;
47591 p->aColName = pColName = (Mem*)sqlite3DbMallocZero(db, sizeof(Mem)*n );
47592 if( p->aColName==0 ) return;
47593 while( n-- > 0 ){
47594 pColName->flags = MEM_Null;
47595 pColName->db = p->db;
@@ -47685,10 +47638,17 @@
47638 static int vdbeCommit(sqlite3 *db, Vdbe *p){
47639 int i;
47640 int nTrans = 0; /* Number of databases with an active write-transaction */
47641 int rc = SQLITE_OK;
47642 int needXcommit = 0;
47643
47644 #ifdef SQLITE_OMIT_VIRTUALTABLE
47645 /* With this option, sqlite3VtabSync() is defined to be simply
47646 ** SQLITE_OK so p is not used.
47647 */
47648 UNUSED_PARAMETER(p);
47649 #endif
47650
47651 /* Before doing anything else, call the xSync() callback for any
47652 ** virtual module tables written in this transaction. This has to
47653 ** be done before determining whether a master journal file is
47654 ** required, as an xSync() callback may add an attached database
@@ -48792,26 +48752,25 @@
48752 p->nField = u;
48753 return (void*)p;
48754 }
48755
48756 /*
48757 ** This routine destroys a UnpackedRecord object.
48758 */
48759 SQLITE_PRIVATE void sqlite3VdbeDeleteUnpackedRecord(UnpackedRecord *p){
48760 int i;
48761 Mem *pMem;
48762
48763 assert( p!=0 );
48764 assert( p->flags & UNPACKED_NEED_DESTROY );
48765 for(i=0, pMem=p->aMem; i<p->nField; i++, pMem++){
48766 if( pMem->zMalloc ){
48767 sqlite3VdbeMemRelease(pMem);
48768 }
48769 }
48770 if( p->flags & UNPACKED_NEED_FREE ){
48771 sqlite3DbFree(p->pKeyInfo->db, p);
 
48772 }
48773 }
48774
48775 /*
48776 ** This function compares the two table rows or index records
@@ -48940,15 +48899,15 @@
48899 u32 typeRowid; /* Serial type of the rowid */
48900 u32 lenRowid; /* Size of the rowid */
48901 Mem m, v;
48902
48903 /* Get the size of the index entry. Only indices entries of less
48904 ** than 2GiB are support - anything large must be database corruption.
48905 ** Any corruption is detected in sqlite3BtreeParseCellPtr(), though, so
48906 ** this code can safely assume that nCellKey is 32-bits */
48907 sqlite3BtreeKeySize(pCur, &nCellKey);
48908 assert( (nCellKey & SQLITE_MAX_U32)==(u64)nCellKey );
 
 
48909
48910 /* Read in the complete content of the index entry */
48911 m.flags = 0;
48912 m.db = db;
48913 m.zMalloc = 0;
@@ -48957,13 +48916,13 @@
48916 return rc;
48917 }
48918
48919 /* The index entry must begin with a header size */
48920 (void)getVarint32((u8*)m.z, szHdr);
48921 testcase( szHdr==3 );
48922 testcase( szHdr==m.n );
48923 if( unlikely(szHdr<3 || (int)szHdr>m.n) ){
48924 goto idx_rowid_corruption;
48925 }
48926
48927 /* The last field of the index should be an integer - the ROWID.
48928 ** Verify that the last entry really is an integer. */
@@ -49098,11 +49057,11 @@
49057 *************************************************************************
49058 **
49059 ** This file contains code use to implement APIs that are part of the
49060 ** VDBE.
49061 **
49062 ** $Id: vdbeapi.c,v 1.167 2009/06/25 01:47:12 drh Exp $
49063 */
49064
49065 #ifndef SQLITE_OMIT_DEPRECATED
49066 /*
49067 ** Return TRUE (non-zero) of the statement supplied as an argument needs
@@ -49238,20 +49197,35 @@
49197 }
49198
49199 /**************************** sqlite3_result_ *******************************
49200 ** The following routines are used by user-defined functions to specify
49201 ** the function result.
49202 **
49203 ** The setStrOrError() funtion calls sqlite3VdbeMemSetStr() to store the
49204 ** result as a string or blob but if the string or blob is too large, it
49205 ** then sets the error code to SQLITE_TOOBIG
49206 */
49207 static void setResultStrOrError(
49208 sqlite3_context *pCtx, /* Function context */
49209 const char *z, /* String pointer */
49210 int n, /* Bytes in string, or negative */
49211 u8 enc, /* Encoding of z. 0 for BLOBs */
49212 void (*xDel)(void*) /* Destructor function */
49213 ){
49214 if( sqlite3VdbeMemSetStr(&pCtx->s, z, n, enc, xDel)==SQLITE_TOOBIG ){
49215 sqlite3_result_error_toobig(pCtx);
49216 }
49217 }
49218 SQLITE_API void sqlite3_result_blob(
49219 sqlite3_context *pCtx,
49220 const void *z,
49221 int n,
49222 void (*xDel)(void *)
49223 ){
49224 assert( n>=0 );
49225 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
49226 setResultStrOrError(pCtx, z, n, 0, xDel);
49227 }
49228 SQLITE_API void sqlite3_result_double(sqlite3_context *pCtx, double rVal){
49229 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
49230 sqlite3VdbeMemSetDouble(&pCtx->s, rVal);
49231 }
@@ -49284,39 +49258,39 @@
49258 const char *z,
49259 int n,
49260 void (*xDel)(void *)
49261 ){
49262 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
49263 setResultStrOrError(pCtx, z, n, SQLITE_UTF8, xDel);
49264 }
49265 #ifndef SQLITE_OMIT_UTF16
49266 SQLITE_API void sqlite3_result_text16(
49267 sqlite3_context *pCtx,
49268 const void *z,
49269 int n,
49270 void (*xDel)(void *)
49271 ){
49272 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
49273 setResultStrOrError(pCtx, z, n, SQLITE_UTF16NATIVE, xDel);
49274 }
49275 SQLITE_API void sqlite3_result_text16be(
49276 sqlite3_context *pCtx,
49277 const void *z,
49278 int n,
49279 void (*xDel)(void *)
49280 ){
49281 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
49282 setResultStrOrError(pCtx, z, n, SQLITE_UTF16BE, xDel);
49283 }
49284 SQLITE_API void sqlite3_result_text16le(
49285 sqlite3_context *pCtx,
49286 const void *z,
49287 int n,
49288 void (*xDel)(void *)
49289 ){
49290 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
49291 setResultStrOrError(pCtx, z, n, SQLITE_UTF16LE, xDel);
49292 }
49293 #endif /* SQLITE_OMIT_UTF16 */
49294 SQLITE_API void sqlite3_result_value(sqlite3_context *pCtx, sqlite3_value *pValue){
49295 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
49296 sqlite3VdbeMemCopy(&pCtx->s, pValue);
@@ -50319,11 +50293,11 @@
50293 ** documentation, headers files, or other derived files. The formatting
50294 ** of the code in this file is, therefore, important. See other comments
50295 ** in this file for details. If in doubt, do not deviate from existing
50296 ** commenting and indentation practices when changing or adding code.
50297 **
50298 ** $Id: vdbe.c,v 1.866 2009/06/26 16:32:13 shane Exp $
50299 */
50300
50301 /*
50302 ** The following global variable is incremented every time a cursor
50303 ** moves, either by the OP_SeekXX, OP_Next, or OP_Prev opcodes. The test
@@ -51615,11 +51589,11 @@
51589 ** is the same as executing Halt.
51590 */
51591 case OP_Halt: {
51592 p->rc = pOp->p1;
51593 p->pc = pc;
51594 p->errorAction = (u8)pOp->p2;
51595 if( pOp->p4.z ){
51596 sqlite3SetString(&p->zErrMsg, db, "%s", pOp->p4.z);
51597 }
51598 rc = sqlite3VdbeHalt(p);
51599 assert( rc==SQLITE_BUSY || rc==SQLITE_OK );
@@ -52816,10 +52790,11 @@
52790 memset(&u.am.sMem, 0, sizeof(u.am.sMem));
52791 assert( u.am.p1<p->nCursor );
52792 assert( pOp->p3>0 && pOp->p3<=p->nMem );
52793 u.am.pDest = &p->aMem[pOp->p3];
52794 MemSetTypeFlag(u.am.pDest, MEM_Null);
52795 u.am.zRec = 0;
52796
52797 /* This block sets the variable u.am.payloadSize to be the total number of
52798 ** bytes in the record.
52799 **
52800 ** u.am.zRec is set to be the complete text of the record if it is available.
@@ -52834,16 +52809,15 @@
52809 u.am.pC = p->apCsr[u.am.p1];
52810 assert( u.am.pC!=0 );
52811 #ifndef SQLITE_OMIT_VIRTUALTABLE
52812 assert( u.am.pC->pVtabCursor==0 );
52813 #endif
52814 u.am.pCrsr = u.am.pC->pCursor;
52815 if( u.am.pCrsr!=0 ){
52816 /* The record is stored in a B-Tree */
52817 rc = sqlite3VdbeCursorMoveto(u.am.pC);
52818 if( rc ) goto abort_due_to_error;
 
 
52819 if( u.am.pC->nullRow ){
52820 u.am.payloadSize = 0;
52821 }else if( u.am.pC->cacheStatus==p->cacheCtr ){
52822 u.am.payloadSize = u.am.pC->payloadSize;
52823 u.am.zRec = (char*)u.am.pC->aRow;
@@ -52855,19 +52829,16 @@
52829 assert( (u.am.payloadSize64 & SQLITE_MAX_U32)==(u64)u.am.payloadSize64 );
52830 u.am.payloadSize = (u32)u.am.payloadSize64;
52831 }else{
52832 sqlite3BtreeDataSize(u.am.pCrsr, &u.am.payloadSize);
52833 }
 
52834 }else if( u.am.pC->pseudoTable ){
52835 /* The record is the sole entry of a pseudo-table */
52836 u.am.payloadSize = u.am.pC->nData;
52837 u.am.zRec = u.am.pC->pData;
52838 u.am.pC->cacheStatus = CACHE_STALE;
52839 assert( u.am.payloadSize==0 || u.am.zRec!=0 );
 
 
52840 }else{
52841 /* Consider the row to be NULL */
52842 u.am.payloadSize = 0;
52843 }
52844
@@ -52879,10 +52850,11 @@
52850 assert( db->aLimit[SQLITE_LIMIT_LENGTH]>=0 );
52851 if( u.am.payloadSize > (u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
52852 goto too_big;
52853 }
52854
52855 u.am.nField = u.am.pC->nField;
52856 assert( u.am.p2<u.am.nField );
52857
52858 /* Read and parse the table header. Store the results of the parse
52859 ** into the record header cache fields of the cursor.
52860 */
@@ -54478,11 +54450,11 @@
54450 sqlite3BtreeSetCachedRowid(u.bf.pC->pCursor, u.bf.v<MAX_ROWID ? u.bf.v+1 : 0);
54451 }
54452 if( u.bf.pC->useRandomRowid ){
54453 assert( pOp->p3==0 ); /* We cannot be in random rowid mode if this is
54454 ** an AUTOINCREMENT table. */
54455 u.bf.v = db->lastRowid;
54456 u.bf.cnt = 0;
54457 do{
54458 if( u.bf.cnt==0 && (u.bf.v&0xffffff)==u.bf.v ){
54459 u.bf.v++;
54460 }else{
@@ -54490,11 +54462,10 @@
54462 if( u.bf.cnt<5 ) u.bf.v &= 0xffffff;
54463 }
54464 rc = sqlite3BtreeMovetoUnpacked(u.bf.pC->pCursor, 0, (u64)u.bf.v, 0, &u.bf.res);
54465 u.bf.cnt++;
54466 }while( u.bf.cnt<100 && rc==SQLITE_OK && u.bf.res==0 );
 
54467 if( rc==SQLITE_OK && u.bf.res==0 ){
54468 rc = SQLITE_FULL;
54469 goto abort_due_to_error;
54470 }
54471 }
@@ -55359,11 +55330,11 @@
55330 ** can result in a "no such table: sqlite_master" or "malformed
55331 ** database schema" error being returned to the user.
55332 */
55333 assert( sqlite3BtreeHoldsMutex(db->aDb[u.bv.iDb].pBt) );
55334 sqlite3BtreeEnterAll(db);
55335 if( pOp->p2 || DbHasProperty(db, u.bv.iDb, DB_SchemaLoaded) ){
55336 u.bv.zMaster = SCHEMA_TABLE(u.bv.iDb);
55337 u.bv.initData.db = db;
55338 u.bv.initData.iDb = pOp->p1;
55339 u.bv.initData.pzErrMsg = &p->zErrMsg;
55340 u.bv.zSql = sqlite3MPrintf(db,
@@ -55801,11 +55772,11 @@
55772 #endif /* local variables moved into u.cd */
55773 assert( pOp->p1>0 && pOp->p1<=p->nMem );
55774 u.cd.pMem = &p->aMem[pOp->p1];
55775 assert( (u.cd.pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
55776 rc = sqlite3VdbeMemFinalize(u.cd.pMem, pOp->p4.pFunc);
55777 if( rc ){
55778 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(u.cd.pMem));
55779 }
55780 sqlite3VdbeChangeEncoding(u.cd.pMem, encoding);
55781 UPDATE_MAX_BLOBSIZE(u.cd.pMem);
55782 if( sqlite3VdbeMemTooBig(u.cd.pMem) ){
@@ -56122,10 +56093,13 @@
56093 if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
56094 rc = u.cj.pModule->xColumn(pCur->pVtabCursor, &u.cj.sContext, pOp->p2);
56095 sqlite3DbFree(db, p->zErrMsg);
56096 p->zErrMsg = u.cj.pVtab->zErrMsg;
56097 u.cj.pVtab->zErrMsg = 0;
56098 if( u.cj.sContext.isError ){
56099 rc = u.cj.sContext.isError;
56100 }
56101
56102 /* Copy the result of the function to the P3 register. We
56103 ** do this regardless of whether or not an error occurred to ensure any
56104 ** dynamic allocation in u.cj.sContext.s (a Mem struct) is released.
56105 */
@@ -64024,11 +63998,11 @@
63998 ** creating ID lists
63999 ** BEGIN TRANSACTION
64000 ** COMMIT
64001 ** ROLLBACK
64002 **
64003 ** $Id: build.c,v 1.554 2009/06/25 11:50:21 drh Exp $
64004 */
64005
64006 /*
64007 ** This routine is called when a new SQL statement is beginning to
64008 ** be parsed. Initialize the pParse structure as needed.
@@ -64175,10 +64149,16 @@
64149 /* Once all the cookies have been verified and transactions opened,
64150 ** obtain the required table-locks. This is a no-op unless the
64151 ** shared-cache feature is enabled.
64152 */
64153 codeTableLocks(pParse);
64154
64155 /* Initialize any AUTOINCREMENT data structures required.
64156 */
64157 sqlite3AutoincrementBegin(pParse);
64158
64159 /* Finally, jump back to the beginning of the executable code. */
64160 sqlite3VdbeAddOp2(v, OP_Goto, 0, pParse->cookieGoto);
64161 }
64162 }
64163
64164
@@ -64373,14 +64353,11 @@
64353 int len;
64354 Hash *pHash = &db->aDb[iDb].pSchema->idxHash;
64355
64356 len = sqlite3Strlen30(zIdxName);
64357 pIndex = sqlite3HashInsert(pHash, zIdxName, len, 0);
64358 if( pIndex ){
 
 
 
64359 if( pIndex->pTable->pIndex==pIndex ){
64360 pIndex->pTable->pIndex = pIndex->pNext;
64361 }else{
64362 Index *p;
64363 /* Justification of ALWAYS(); The index must be on the list of
@@ -68090,11 +68067,11 @@
68067 **
68068 *************************************************************************
68069 ** This file contains C code routines that are called by the parser
68070 ** in order to generate code for DELETE FROM statements.
68071 **
68072 ** $Id: delete.c,v 1.204 2009/06/23 20:28:54 drh Exp $
68073 */
68074
68075 /*
68076 ** Look up every table that is named in pSrc. If any table is not found,
68077 ** add an error message to pParse->zErrMsg and return NULL. If all tables
@@ -68551,10 +68528,18 @@
68528 sqlite3VdbeAddOp2(v, OP_Close, iCur + i, pIdx->tnum);
68529 }
68530 sqlite3VdbeAddOp1(v, OP_Close, iCur);
68531 }
68532 }
68533
68534 /* Update the sqlite_sequence table by storing the content of the
68535 ** maximum rowid counter values recorded while inserting into
68536 ** autoincrement tables.
68537 */
68538 if( pParse->nested==0 && pParse->trigStack==0 ){
68539 sqlite3AutoincrementEnd(pParse);
68540 }
68541
68542 /*
68543 ** Return the number of rows that were deleted. If this routine is
68544 ** generating code because of a call to sqlite3NestedParse(), do not
68545 ** invoke the callback function.
@@ -70184,11 +70169,11 @@
70169 **
70170 *************************************************************************
70171 ** This file contains C code routines that are called by the parser
70172 ** to handle INSERT statements in SQLite.
70173 **
70174 ** $Id: insert.c,v 1.269 2009/06/23 20:28:54 drh Exp $
70175 */
70176
70177 /*
70178 ** Generate code that will open a table for reading.
70179 */
@@ -70333,58 +70318,87 @@
70318 return 0;
70319 }
70320
70321 #ifndef SQLITE_OMIT_AUTOINCREMENT
70322 /*
70323 ** Locate or create an AutoincInfo structure associated with table pTab
70324 ** which is in database iDb. Return the register number for the register
70325 ** that holds the maximum rowid.
70326 **
70327 ** There is at most one AutoincInfo structure per table even if the
70328 ** same table is autoincremented multiple times due to inserts within
70329 ** triggers. A new AutoincInfo structure is created if this is the
70330 ** first use of table pTab. On 2nd and subsequent uses, the original
70331 ** AutoincInfo structure is used.
70332 **
70333 ** Three memory locations are allocated:
70334 **
70335 ** (1) Register to hold the name of the pTab table.
70336 ** (2) Register to hold the maximum ROWID of pTab.
70337 ** (3) Register to hold the rowid in sqlite_sequence of pTab
70338 **
70339 ** The 2nd register is the one that is returned. That is all the
70340 ** insert routine needs to know about.
70341 */
70342 static int autoIncBegin(
70343 Parse *pParse, /* Parsing context */
70344 int iDb, /* Index of the database holding pTab */
70345 Table *pTab /* The table we are writing to */
70346 ){
70347 int memId = 0; /* Register holding maximum rowid */
70348 if( pTab->tabFlags & TF_Autoincrement ){
70349 AutoincInfo *pInfo;
70350
70351 pInfo = pParse->pAinc;
70352 while( pInfo && pInfo->pTab!=pTab ){ pInfo = pInfo->pNext; }
70353 if( pInfo==0 ){
70354 pInfo = sqlite3DbMallocRaw(pParse->db, sizeof(*pInfo));
70355 if( pInfo==0 ) return 0;
70356 pInfo->pNext = pParse->pAinc;
70357 pParse->pAinc = pInfo;
70358 pInfo->pTab = pTab;
70359 pInfo->iDb = iDb;
70360 pParse->nMem++; /* Register to hold name of table */
70361 pInfo->regCtr = ++pParse->nMem; /* Max rowid register */
70362 pParse->nMem++; /* Rowid in sqlite_sequence */
70363 }
70364 memId = pInfo->regCtr;
 
 
 
 
 
70365 }
70366 return memId;
70367 }
70368
70369 /*
70370 ** This routine generates code that will initialize all of the
70371 ** register used by the autoincrement tracker.
70372 */
70373 SQLITE_PRIVATE void sqlite3AutoincrementBegin(Parse *pParse){
70374 AutoincInfo *p; /* Information about an AUTOINCREMENT */
70375 sqlite3 *db = pParse->db; /* The database connection */
70376 Db *pDb; /* Database only autoinc table */
70377 int memId; /* Register holding max rowid */
70378 int addr; /* A VDBE address */
70379 Vdbe *v = pParse->pVdbe; /* VDBE under construction */
70380
70381 assert( v ); /* We failed long ago if this is not so */
70382 for(p = pParse->pAinc; p; p = p->pNext){
70383 pDb = &db->aDb[p->iDb];
70384 memId = p->regCtr;
70385 sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenRead);
70386 addr = sqlite3VdbeCurrentAddr(v);
70387 sqlite3VdbeAddOp4(v, OP_String8, 0, memId-1, 0, p->pTab->zName, 0);
70388 sqlite3VdbeAddOp2(v, OP_Rewind, 0, addr+9);
70389 sqlite3VdbeAddOp3(v, OP_Column, 0, 0, memId);
70390 sqlite3VdbeAddOp3(v, OP_Ne, memId-1, addr+7, memId);
70391 sqlite3VdbeChangeP5(v, SQLITE_JUMPIFNULL);
70392 sqlite3VdbeAddOp2(v, OP_Rowid, 0, memId+1);
70393 sqlite3VdbeAddOp3(v, OP_Column, 0, 1, memId);
70394 sqlite3VdbeAddOp2(v, OP_Goto, 0, addr+9);
70395 sqlite3VdbeAddOp2(v, OP_Next, 0, addr+2);
70396 sqlite3VdbeAddOp2(v, OP_Integer, 0, memId);
70397 sqlite3VdbeAddOp0(v, OP_Close);
70398 }
70399 }
70400
70401 /*
70402 ** Update the maximum rowid for an autoincrement calculation.
70403 **
70404 ** This routine should be called when the top of the stack holds a
@@ -70397,46 +70411,56 @@
70411 sqlite3VdbeAddOp2(pParse->pVdbe, OP_MemMax, memId, regRowid);
70412 }
70413 }
70414
70415 /*
70416 ** This routine generates the code needed to write autoincrement
70417 ** maximum rowid values back into the sqlite_sequence register.
70418 ** Every statement that might do an INSERT into an autoincrement
70419 ** table (either directly or through triggers) needs to call this
70420 ** routine just before the "exit" code.
70421 */
70422 SQLITE_PRIVATE void sqlite3AutoincrementEnd(Parse *pParse){
70423 AutoincInfo *p;
70424 Vdbe *v = pParse->pVdbe;
70425 sqlite3 *db = pParse->db;
70426
70427 assert( v );
70428 for(p = pParse->pAinc; p; p = p->pNext){
70429 Db *pDb = &db->aDb[p->iDb];
70430 int j1, j2, j3, j4, j5;
70431 int iRec;
70432 int memId = p->regCtr;
70433
70434 iRec = sqlite3GetTempReg(pParse);
70435 sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenWrite);
 
70436 j1 = sqlite3VdbeAddOp1(v, OP_NotNull, memId+1);
70437 j2 = sqlite3VdbeAddOp0(v, OP_Rewind);
70438 j3 = sqlite3VdbeAddOp3(v, OP_Column, 0, 0, iRec);
70439 j4 = sqlite3VdbeAddOp3(v, OP_Eq, memId-1, 0, iRec);
70440 sqlite3VdbeAddOp2(v, OP_Next, 0, j3);
70441 sqlite3VdbeJumpHere(v, j2);
70442 sqlite3VdbeAddOp2(v, OP_NewRowid, 0, memId+1);
70443 j5 = sqlite3VdbeAddOp0(v, OP_Goto);
70444 sqlite3VdbeJumpHere(v, j4);
70445 sqlite3VdbeAddOp2(v, OP_Rowid, 0, memId+1);
70446 sqlite3VdbeJumpHere(v, j1);
70447 sqlite3VdbeJumpHere(v, j5);
70448 sqlite3VdbeAddOp3(v, OP_MakeRecord, memId-1, 2, iRec);
70449 sqlite3VdbeAddOp3(v, OP_Insert, 0, iRec, memId+1);
70450 sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
70451 sqlite3VdbeAddOp0(v, OP_Close);
70452 sqlite3ReleaseTempReg(pParse, iRec);
70453 }
70454 }
70455 #else
70456 /*
70457 ** If SQLITE_OMIT_AUTOINCREMENT is defined, then the three routines
70458 ** above are all no-ops
70459 */
70460 # define autoIncBegin(A,B,C) (0)
70461 # define autoIncStep(A,B,C)
 
70462 #endif /* SQLITE_OMIT_AUTOINCREMENT */
70463
70464
70465 /* Forward declaration */
70466 static int xferOptimization(
@@ -70678,11 +70702,11 @@
70702 ** This is the 2nd template.
70703 */
70704 if( pColumn==0 && xferOptimization(pParse, pTab, pSelect, onError, iDb) ){
70705 assert( !pTrigger );
70706 assert( pList==0 );
70707 goto insert_end;
70708 }
70709 #endif /* SQLITE_OMIT_XFER_OPT */
70710
70711 /* If this is an AUTOINCREMENT table, look up the sequence number in the
70712 ** sqlite_sequence table and store it in memory cell regAutoinc.
@@ -71160,15 +71184,18 @@
71184 for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
71185 sqlite3VdbeAddOp1(v, OP_Close, idx+baseCur);
71186 }
71187 }
71188
71189 insert_end:
71190 /* Update the sqlite_sequence table by storing the content of the
71191 ** maximum rowid counter values recorded while inserting into
71192 ** autoincrement tables.
71193 */
71194 if( pParse->nested==0 && pParse->trigStack==0 ){
71195 sqlite3AutoincrementEnd(pParse);
71196 }
71197
71198 /*
71199 ** Return the number of rows inserted. If this routine is
71200 ** generating code because of a call to sqlite3NestedParse(), do not
71201 ** invoke the callback function.
@@ -71887,11 +71914,10 @@
71914 sqlite3VdbeAddOp2(v, OP_RowData, iSrc, regData);
71915 sqlite3VdbeAddOp3(v, OP_Insert, iDest, regData, regRowid);
71916 sqlite3VdbeChangeP5(v, OPFLAG_NCHANGE|OPFLAG_LASTROWID|OPFLAG_APPEND);
71917 sqlite3VdbeChangeP4(v, -1, pDest->zName, 0);
71918 sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1);
 
71919 for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
71920 for(pSrcIdx=pSrc->pIndex; ALWAYS(pSrcIdx); pSrcIdx=pSrcIdx->pNext){
71921 if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break;
71922 }
71923 assert( pSrcIdx );
@@ -74504,11 +74530,11 @@
74530 *************************************************************************
74531 ** This file contains the implementation of the sqlite3_prepare()
74532 ** interface, and routines that contribute to loading the database schema
74533 ** from disk.
74534 **
74535 ** $Id: prepare.c,v 1.125 2009/06/25 11:50:21 drh Exp $
74536 */
74537
74538 /*
74539 ** Fill the InitData structure with an error message that indicates
74540 ** that the database is corrupt.
@@ -74577,11 +74603,11 @@
74603 assert( rc!=SQLITE_OK || zErr==0 );
74604 if( SQLITE_OK!=rc ){
74605 pData->rc = rc;
74606 if( rc==SQLITE_NOMEM ){
74607 db->mallocFailed = 1;
74608 }else if( rc!=SQLITE_INTERRUPT && rc!=SQLITE_LOCKED ){
74609 corruptSchema(pData, argv[0], zErr);
74610 }
74611 sqlite3DbFree(db, zErr);
74612 }
74613 }else if( argv[0]==0 ){
@@ -80648,11 +80674,11 @@
80674 **
80675 *************************************************************************
80676 ** This file contains C code routines that are called by the parser
80677 ** to handle UPDATE statements.
80678 **
80679 ** $Id: update.c,v 1.204 2009/06/27 11:17:35 drh Exp $
80680 */
80681
80682 #ifndef SQLITE_OMIT_VIRTUALTABLE
80683 /* Forward declaration */
80684 static void updateVirtualTable(
@@ -81198,10 +81224,18 @@
81224 sqlite3VdbeAddOp2(v, OP_Close, iCur, 0);
81225 if( pTrigger ){
81226 sqlite3VdbeAddOp2(v, OP_Close, newIdx, 0);
81227 sqlite3VdbeAddOp2(v, OP_Close, oldIdx, 0);
81228 }
81229
81230 /* Update the sqlite_sequence table by storing the content of the
81231 ** maximum rowid counter values recorded while inserting into
81232 ** autoincrement tables.
81233 */
81234 if( pParse->nested==0 && pParse->trigStack==0 ){
81235 sqlite3AutoincrementEnd(pParse);
81236 }
81237
81238 /*
81239 ** Return the number of rows that were changed. If this routine is
81240 ** generating code because of a call to sqlite3NestedParse(), do not
81241 ** invoke the callback function.
@@ -81296,21 +81330,20 @@
81330 sqlite3Select(pParse, pSelect, &dest);
81331
81332 /* Generate code to scan the ephemeral table and call VUpdate. */
81333 iReg = ++pParse->nMem;
81334 pParse->nMem += pTab->nCol+1;
81335 addr = sqlite3VdbeAddOp2(v, OP_Rewind, ephemTab, 0);
 
81336 sqlite3VdbeAddOp3(v, OP_Column, ephemTab, 0, iReg);
81337 sqlite3VdbeAddOp3(v, OP_Column, ephemTab, (pRowid?1:0), iReg+1);
81338 for(i=0; i<pTab->nCol; i++){
81339 sqlite3VdbeAddOp3(v, OP_Column, ephemTab, i+1+(pRowid!=0), iReg+2+i);
81340 }
81341 sqlite3VtabMakeWritable(pParse, pTab);
81342 sqlite3VdbeAddOp4(v, OP_VUpdate, 0, pTab->nCol+2, iReg, pVtab, P4_VTAB);
81343 sqlite3VdbeAddOp2(v, OP_Next, ephemTab, addr+1);
81344 sqlite3VdbeJumpHere(v, addr);
81345 sqlite3VdbeAddOp2(v, OP_Close, ephemTab, 0);
81346
81347 /* Cleanup */
81348 sqlite3SelectDelete(db, pSelect);
81349 }
@@ -89455,11 +89488,11 @@
89488 **
89489 ** This file contains C code that splits an SQL input string up into
89490 ** individual tokens and sends those tokens one-by-one over to the
89491 ** parser for analysis.
89492 **
89493 ** $Id: tokenize.c,v 1.162 2009/06/23 20:28:54 drh Exp $
89494 */
89495
89496 /*
89497 ** The charMap() macro maps alphabetic characters into their
89498 ** lower-case ASCII equivalent. On ASCII machines, this is just
@@ -90226,10 +90259,15 @@
90259 }
90260
90261 sqlite3DeleteTrigger(db, pParse->pNewTrigger);
90262 sqlite3DbFree(db, pParse->apVarExpr);
90263 sqlite3DbFree(db, pParse->aAlias);
90264 while( pParse->pAinc ){
90265 AutoincInfo *p = pParse->pAinc;
90266 pParse->pAinc = p->pNext;
90267 sqlite3DbFree(db, p);
90268 }
90269 while( pParse->pZombieTab ){
90270 Table *p = pParse->pZombieTab;
90271 pParse->pZombieTab = p->pNextZombie;
90272 sqlite3DeleteTable(p);
90273 }
@@ -90534,11 +90572,11 @@
90572 ** Main file for the SQLite library. The routines in this file
90573 ** implement the programmer interface to the library. Routines in
90574 ** other files are for internal use by SQLite and should not be
90575 ** accessed by users of the library.
90576 **
90577 ** $Id: main.c,v 1.560 2009/06/26 15:14:55 drh Exp $
90578 */
90579
90580 #ifdef SQLITE_ENABLE_FTS3
90581 /************** Include fts3.h in the middle of main.c ***********************/
90582 /************** Begin file fts3.h ********************************************/
@@ -91356,11 +91394,11 @@
91394 /* SQLITE_FULL */ "database or disk is full",
91395 /* SQLITE_CANTOPEN */ "unable to open database file",
91396 /* SQLITE_PROTOCOL */ 0,
91397 /* SQLITE_EMPTY */ "table contains no data",
91398 /* SQLITE_SCHEMA */ "database schema has changed",
91399 /* SQLITE_TOOBIG */ "string or blob too big",
91400 /* SQLITE_CONSTRAINT */ "constraint failed",
91401 /* SQLITE_MISMATCH */ "datatype mismatch",
91402 /* SQLITE_MISUSE */ "library routine called out of sequence",
91403 /* SQLITE_NOLFS */ "large file support is disabled",
91404 /* SQLITE_AUTH */ "authorization denied",
@@ -92169,11 +92207,10 @@
92207 goto opendb_out;
92208 }
92209 }
92210 sqlite3_mutex_enter(db->mutex);
92211 db->errMask = 0xff;
 
92212 db->nDb = 2;
92213 db->magic = SQLITE_MAGIC_BUSY;
92214 db->aDb = db->aDbStatic;
92215
92216 assert( sizeof(db->aLimit)==sizeof(aHardLimit) );
92217
+2 -2
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -97,12 +97,12 @@
9797
**
9898
** See also: [sqlite3_libversion()] and [sqlite3_libversion_number()].
9999
**
100100
** Requirements: [H10011] [H10014]
101101
*/
102
-#define SQLITE_VERSION "3.6.15"
103
-#define SQLITE_VERSION_NUMBER 3006015
102
+#define SQLITE_VERSION "3.6.16"
103
+#define SQLITE_VERSION_NUMBER 3006016
104104
105105
/*
106106
** CAPI3REF: Run-Time Library Version Numbers {H10020} <S60100>
107107
** KEYWORDS: sqlite3_version
108108
**
109109
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -97,12 +97,12 @@
97 **
98 ** See also: [sqlite3_libversion()] and [sqlite3_libversion_number()].
99 **
100 ** Requirements: [H10011] [H10014]
101 */
102 #define SQLITE_VERSION "3.6.15"
103 #define SQLITE_VERSION_NUMBER 3006015
104
105 /*
106 ** CAPI3REF: Run-Time Library Version Numbers {H10020} <S60100>
107 ** KEYWORDS: sqlite3_version
108 **
109
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -97,12 +97,12 @@
97 **
98 ** See also: [sqlite3_libversion()] and [sqlite3_libversion_number()].
99 **
100 ** Requirements: [H10011] [H10014]
101 */
102 #define SQLITE_VERSION "3.6.16"
103 #define SQLITE_VERSION_NUMBER 3006016
104
105 /*
106 ** CAPI3REF: Run-Time Library Version Numbers {H10020} <S60100>
107 ** KEYWORDS: sqlite3_version
108 **
109
+4 -2
--- src/user.c
+++ src/user.c
@@ -316,12 +316,14 @@
316316
317317
if( attempt_user(db_get("default-user", 0)) ) return;
318318
319319
if( attempt_user(getenv("USER")) ) return;
320320
321
- db_prepare(&s, "SELECT uid, login FROM user"
322
- " WHERE login NOT IN ('anonymous','nobody')");
321
+ db_prepare(&s,
322
+ "SELECT uid, login FROM user"
323
+ " WHERE login NOT IN ('anonymous','nobody','reader','developer')"
324
+ );
323325
if( db_step(&s)==SQLITE_ROW ){
324326
g.userUid = db_column_int(&s, 0);
325327
g.zLogin = mprintf("%s", db_column_text(&s, 1));
326328
}
327329
db_finalize(&s);
328330
--- src/user.c
+++ src/user.c
@@ -316,12 +316,14 @@
316
317 if( attempt_user(db_get("default-user", 0)) ) return;
318
319 if( attempt_user(getenv("USER")) ) return;
320
321 db_prepare(&s, "SELECT uid, login FROM user"
322 " WHERE login NOT IN ('anonymous','nobody')");
 
 
323 if( db_step(&s)==SQLITE_ROW ){
324 g.userUid = db_column_int(&s, 0);
325 g.zLogin = mprintf("%s", db_column_text(&s, 1));
326 }
327 db_finalize(&s);
328
--- src/user.c
+++ src/user.c
@@ -316,12 +316,14 @@
316
317 if( attempt_user(db_get("default-user", 0)) ) return;
318
319 if( attempt_user(getenv("USER")) ) return;
320
321 db_prepare(&s,
322 "SELECT uid, login FROM user"
323 " WHERE login NOT IN ('anonymous','nobody','reader','developer')"
324 );
325 if( db_step(&s)==SQLITE_ROW ){
326 g.userUid = db_column_int(&s, 0);
327 g.zLogin = mprintf("%s", db_column_text(&s, 1));
328 }
329 db_finalize(&s);
330

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button