Fossil SCM

Update the SQLite implementation to the latest from CVS.

drh 2009-06-25 13:00 trunk
Commit 0e82ba024cb8c46c32ff6c0fa051397634c6de84
2 files changed +2930 -3043 +42 -21
+2930 -3043
--- 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.14. By combining all the individual C code files into this
3
+** version 3.6.15. 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.
@@ -9,17 +9,17 @@
99
**
1010
** This file is all you need to compile SQLite. To use SQLite in other
1111
** programs, you need this file and the "sqlite3.h" header file that defines
1212
** the programming interface to the SQLite library. (If you do not have
1313
** the "sqlite3.h" header file at hand, you will find a copy in the first
14
-** 5605 lines past this header comment.) Additional code files may be
14
+** 5626 lines past this header comment.) Additional code files may be
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-07 12:34:52 UTC.
20
+** This amalgamation was generated on 2009-06-23 14:42:37 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.882 2009/06/05 14:17:23 drh Exp $
44
+** @(#) $Id: sqliteInt.h,v 1.886 2009/06/19 14:06:03 drh Exp $
4545
*/
4646
#ifndef _SQLITEINT_H_
4747
#define _SQLITEINT_H_
4848
4949
/*
@@ -545,11 +545,11 @@
545545
** The name of this file under configuration management is "sqlite.h.in".
546546
** The makefile makes some minor changes to this file (such as inserting
547547
** the version number) and changes its name to "sqlite3.h" as
548548
** part of the build process.
549549
**
550
-** @(#) $Id: sqlite.h.in,v 1.455 2009/05/24 21:59:28 drh Exp $
550
+** @(#) $Id: sqlite.h.in,v 1.458 2009/06/19 22:50:31 drh Exp $
551551
*/
552552
#ifndef _SQLITE3_H_
553553
#define _SQLITE3_H_
554554
#include <stdarg.h> /* Needed for the definition of va_list */
555555
@@ -614,12 +614,12 @@
614614
**
615615
** See also: [sqlite3_libversion()] and [sqlite3_libversion_number()].
616616
**
617617
** Requirements: [H10011] [H10014]
618618
*/
619
-#define SQLITE_VERSION "3.6.14"
620
-#define SQLITE_VERSION_NUMBER 3006014
619
+#define SQLITE_VERSION "3.6.15"
620
+#define SQLITE_VERSION_NUMBER 3006015
621621
622622
/*
623623
** CAPI3REF: Run-Time Library Version Numbers {H10020} <S60100>
624624
** KEYWORDS: sqlite3_version
625625
**
@@ -1009,10 +1009,16 @@
10091009
** [sqlite3_file] object (or, more commonly, a subclass of the
10101010
** [sqlite3_file] object) with a pointer to an instance of this object.
10111011
** This object defines the methods used to perform various operations
10121012
** against the open file represented by the [sqlite3_file] object.
10131013
**
1014
+** If the xOpen method sets the sqlite3_file.pMethods element
1015
+** to a non-NULL pointer, then the sqlite3_io_methods.xClose method
1016
+** may be invoked even if the xOpen reported that it failed. The
1017
+** only way to prevent a call to xClose following a failed xOpen
1018
+** is for the xOpen to set the sqlite3_file.pMethods element to NULL.
1019
+**
10141020
** The flags argument to xSync may be one of [SQLITE_SYNC_NORMAL] or
10151021
** [SQLITE_SYNC_FULL]. The first choice is the normal fsync().
10161022
** The second choice is a Mac OS X style fullsync. The [SQLITE_SYNC_DATAONLY]
10171023
** flag may be ORed in to indicate that only the data of the file
10181024
** and not its inode needs to be synced.
@@ -1169,15 +1175,15 @@
11691175
**
11701176
** SQLite will guarantee that the zFilename parameter to xOpen
11711177
** is either a NULL pointer or string obtained
11721178
** from xFullPathname(). SQLite further guarantees that
11731179
** the string will be valid and unchanged until xClose() is
1174
-** called. Because of the previous sentense,
1180
+** called. Because of the previous sentence,
11751181
** the [sqlite3_file] can safely store a pointer to the
11761182
** filename if it needs to remember the filename for some reason.
11771183
** If the zFilename parameter is xOpen is a NULL pointer then xOpen
1178
-** must invite its own temporary name for the file. Whenever the
1184
+** must invent its own temporary name for the file. Whenever the
11791185
** xFilename parameter is NULL it will also be the case that the
11801186
** flags parameter will include [SQLITE_OPEN_DELETEONCLOSE].
11811187
**
11821188
** The flags argument to xOpen() includes all bits set in
11831189
** the flags argument to [sqlite3_open_v2()]. Or if [sqlite3_open()]
@@ -1229,11 +1235,16 @@
12291235
** for exclusive access.
12301236
**
12311237
** At least szOsFile bytes of memory are allocated by SQLite
12321238
** to hold the [sqlite3_file] structure passed as the third
12331239
** argument to xOpen. The xOpen method does not have to
1234
-** allocate the structure; it should just fill it in.
1240
+** allocate the structure; it should just fill it in. Note that
1241
+** the xOpen method must set the sqlite3_file.pMethods to either
1242
+** a valid [sqlite3_io_methods] object or to NULL. xOpen must do
1243
+** this even if the open fails. SQLite expects that the sqlite3_file.pMethods
1244
+** element will be valid after xOpen returns regardless of the success
1245
+** or failure of the xOpen call.
12351246
**
12361247
** The flags argument to xAccess() may be [SQLITE_ACCESS_EXISTS]
12371248
** to test for the existence of a file, or [SQLITE_ACCESS_READWRITE] to
12381249
** test whether a file is readable and writable, or [SQLITE_ACCESS_READ]
12391250
** to test whether a file is at least readable. The file can be a
@@ -1551,16 +1562,18 @@
15511562
** </ul>
15521563
** </dd>
15531564
**
15541565
** <dt>SQLITE_CONFIG_SCRATCH</dt>
15551566
** <dd>This option specifies a static memory buffer that SQLite can use for
1556
-** scratch memory. There are three arguments: A pointer to the memory, the
1557
-** size of each scratch buffer (sz), and the number of buffers (N). The sz
1567
+** scratch memory. There are three arguments: A pointer an 8-byte
1568
+** aligned memory buffer from which the scrach allocations will be
1569
+** drawn, the size of each scratch allocation (sz),
1570
+** and the maximum number of scratch allocations (N). The sz
15581571
** argument must be a multiple of 16. The sz parameter should be a few bytes
1559
-** larger than the actual scratch space required due internal overhead.
1560
-** The first
1561
-** argument should point to an allocation of at least sz*N bytes of memory.
1572
+** larger than the actual scratch space required due to internal overhead.
1573
+** The first argument should pointer to an 8-byte aligned buffer
1574
+** of at least sz*N bytes of memory.
15621575
** SQLite will use no more than one scratch buffer at once per thread, so
15631576
** N should be set to the expected maximum number of threads. The sz
15641577
** parameter should be 6 times the size of the largest database page size.
15651578
** Scratch buffers are used as part of the btree balance operation. If
15661579
** The btree balancer needs additional memory beyond what is provided by
@@ -1570,33 +1583,41 @@
15701583
** <dt>SQLITE_CONFIG_PAGECACHE</dt>
15711584
** <dd>This option specifies a static memory buffer that SQLite can use for
15721585
** the database page cache with the default page cache implemenation.
15731586
** This configuration should not be used if an application-define page
15741587
** cache implementation is loaded using the SQLITE_CONFIG_PCACHE option.
1575
-** There are three arguments to this option: A pointer to the
1588
+** There are three arguments to this option: A pointer to 8-byte aligned
15761589
** memory, the size of each page buffer (sz), and the number of pages (N).
1577
-** The sz argument must be a power of two between 512 and 32768. The first
1590
+** The sz argument should be the size of the largest database page
1591
+** (a power of two between 512 and 32768) plus a little extra for each
1592
+** page header. The page header size is 20 to 40 bytes depending on
1593
+** the host architecture. It is harmless, apart from the wasted memory,
1594
+** to make sz a little too large. The first
15781595
** argument should point to an allocation of at least sz*N bytes of memory.
15791596
** SQLite will use the memory provided by the first argument to satisfy its
15801597
** memory needs for the first N pages that it adds to cache. If additional
15811598
** page cache memory is needed beyond what is provided by this option, then
15821599
** SQLite goes to [sqlite3_malloc()] for the additional storage space.
15831600
** The implementation might use one or more of the N buffers to hold
1584
-** memory accounting information. </dd>
1601
+** memory accounting information. The pointer in the first argument must
1602
+** be aligned to an 8-byte boundary or subsequent behavior of SQLite
1603
+** will be undefined.</dd>
15851604
**
15861605
** <dt>SQLITE_CONFIG_HEAP</dt>
15871606
** <dd>This option specifies a static memory buffer that SQLite will use
15881607
** for all of its dynamic memory allocation needs beyond those provided
15891608
** for by [SQLITE_CONFIG_SCRATCH] and [SQLITE_CONFIG_PAGECACHE].
1590
-** There are three arguments: A pointer to the memory, the number of
1591
-** bytes in the memory buffer, and the minimum allocation size. If
1592
-** the first pointer (the memory pointer) is NULL, then SQLite reverts
1609
+** There are three arguments: An 8-byte aligned pointer to the memory,
1610
+** the number of bytes in the memory buffer, and the minimum allocation size.
1611
+** If the first pointer (the memory pointer) is NULL, then SQLite reverts
15931612
** to using its default memory allocator (the system malloc() implementation),
15941613
** undoing any prior invocation of [SQLITE_CONFIG_MALLOC]. If the
15951614
** memory pointer is not NULL and either [SQLITE_ENABLE_MEMSYS3] or
15961615
** [SQLITE_ENABLE_MEMSYS5] are defined, then the alternative memory
1597
-** allocator is engaged to handle all of SQLites memory allocation needs.</dd>
1616
+** allocator is engaged to handle all of SQLites memory allocation needs.
1617
+** The first pointer (the memory pointer) must be aligned to an 8-byte
1618
+** boundary or subsequent behavior of SQLite will be undefined.</dd>
15981619
**
15991620
** <dt>SQLITE_CONFIG_MUTEX</dt>
16001621
** <dd>This option takes a single argument which is a pointer to an
16011622
** instance of the [sqlite3_mutex_methods] structure. The argument specifies
16021623
** alternative low-level mutex routines to be used in place
@@ -1663,13 +1684,13 @@
16631684
** <dl>
16641685
** <dt>SQLITE_DBCONFIG_LOOKASIDE</dt>
16651686
** <dd>This option takes three additional arguments that determine the
16661687
** [lookaside memory allocator] configuration for the [database connection].
16671688
** The first argument (the third parameter to [sqlite3_db_config()] is a
1668
-** pointer to a memory buffer to use for lookaside memory. The first
1669
-** argument may be NULL in which case SQLite will allocate the lookaside
1670
-** buffer itself using [sqlite3_malloc()]. The second argument is the
1689
+** pointer to an 8-byte aligned memory buffer to use for lookaside memory.
1690
+** The first argument may be NULL in which case SQLite will allocate the
1691
+** lookaside buffer itself using [sqlite3_malloc()]. The second argument is the
16711692
** size of each lookaside buffer slot and the third argument is the number of
16721693
** slots. The size of the buffer in the first argument must be greater than
16731694
** or equal to the product of the second and third arguments.</dd>
16741695
**
16751696
** </dl>
@@ -6397,15 +6418,16 @@
63976418
*/
63986419
#ifdef SQLITE_OMIT_FLOATING_POINT
63996420
# define double sqlite_int64
64006421
# define LONGDOUBLE_TYPE sqlite_int64
64016422
# ifndef SQLITE_BIG_DBL
6402
-# define SQLITE_BIG_DBL (0x7fffffffffffffff)
6423
+# define SQLITE_BIG_DBL (((sqlite3_int64)1)<<60)
64036424
# endif
64046425
# define SQLITE_OMIT_DATETIME_FUNCS 1
64056426
# define SQLITE_OMIT_TRACE 1
64066427
# undef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
6428
+# undef SQLITE_HAVE_ISNAN
64076429
#endif
64086430
#ifndef SQLITE_BIG_DBL
64096431
# define SQLITE_BIG_DBL (1e99)
64106432
#endif
64116433
@@ -7375,11 +7397,11 @@
73757397
*************************************************************************
73767398
** This header file defines the interface that the sqlite page cache
73777399
** subsystem. The page cache subsystem reads and writes a file a page
73787400
** at a time and provides a journal for rollback.
73797401
**
7380
-** @(#) $Id: pager.h,v 1.101 2009/04/30 09:10:38 danielk1977 Exp $
7402
+** @(#) $Id: pager.h,v 1.102 2009/06/18 17:22:39 drh Exp $
73817403
*/
73827404
73837405
#ifndef _PAGER_H_
73847406
#define _PAGER_H_
73857407
@@ -7455,11 +7477,11 @@
74557477
SQLITE_PRIVATE int sqlite3PagerReadFileheader(Pager*, int, unsigned char*);
74567478
74577479
/* Functions used to configure a Pager object. */
74587480
SQLITE_PRIVATE void sqlite3PagerSetBusyhandler(Pager*, int(*)(void *), void *);
74597481
SQLITE_PRIVATE void sqlite3PagerSetReiniter(Pager*, void(*)(DbPage*));
7460
-SQLITE_PRIVATE int sqlite3PagerSetPagesize(Pager*, u16*);
7482
+SQLITE_PRIVATE int sqlite3PagerSetPagesize(Pager*, u16*, int);
74617483
SQLITE_PRIVATE int sqlite3PagerMaxPageCount(Pager*, int);
74627484
SQLITE_PRIVATE void sqlite3PagerSetCachesize(Pager*, int);
74637485
SQLITE_PRIVATE void sqlite3PagerSetSafetyLevel(Pager*,int,int);
74647486
SQLITE_PRIVATE int sqlite3PagerLockingMode(Pager *, int);
74657487
SQLITE_PRIVATE int sqlite3PagerJournalMode(Pager *, int);
@@ -7503,15 +7525,10 @@
75037525
SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager*);
75047526
75057527
/* Functions used to truncate the database file. */
75067528
SQLITE_PRIVATE void sqlite3PagerTruncateImage(Pager*,Pgno);
75077529
7508
-/* Used by encryption extensions. */
7509
-#ifdef SQLITE_HAS_CODEC
7510
-SQLITE_PRIVATE void sqlite3PagerSetCodec(Pager*,void*(*)(void*,void*,Pgno,int),void*);
7511
-#endif
7512
-
75137530
/* Functions to support testing and debugging. */
75147531
#if !defined(NDEBUG) || defined(SQLITE_TEST)
75157532
SQLITE_PRIVATE Pgno sqlite3PagerPagenumber(DbPage*);
75167533
SQLITE_PRIVATE int sqlite3PagerIswriteable(DbPage*);
75177534
#endif
@@ -8259,13 +8276,10 @@
82598276
FuncDefHash aFunc; /* Hash table of connection functions */
82608277
Hash aCollSeq; /* All collating sequences */
82618278
BusyHandler busyHandler; /* Busy callback */
82628279
int busyTimeout; /* Busy handler timeout, in msec */
82638280
Db aDbStatic[2]; /* Static space for the 2 default backends */
8264
-#ifdef SQLITE_SSE
8265
- sqlite3_stmt *pFetch; /* Used by SSE to fetch stored statements */
8266
-#endif
82678281
Savepoint *pSavepoint; /* List of active savepoints */
82688282
int nSavepoint; /* Number of non-transaction savepoints */
82698283
int nStatement; /* Number of nested statement-transactions */
82708284
u8 isTransactionSavepoint; /* True if the outermost savepoint is a TS */
82718285
@@ -10301,15 +10315,10 @@
1030110315
#define sqlite3ConnectionBlocked(x,y)
1030210316
#define sqlite3ConnectionUnlocked(x)
1030310317
#define sqlite3ConnectionClosed(x)
1030410318
#endif
1030510319
10306
-
10307
-#ifdef SQLITE_SSE
10308
-#include "sseInt.h"
10309
-#endif
10310
-
1031110320
#ifdef SQLITE_DEBUG
1031210321
SQLITE_PRIVATE void sqlite3ParserTrace(FILE*, char *);
1031310322
#endif
1031410323
1031510324
/*
@@ -17073,22 +17082,14 @@
1707317082
** VDBE. This information used to all be at the top of the single
1707417083
** source code file "vdbe.c". When that file became too big (over
1707517084
** 6000 lines long) it was split up into several smaller files and
1707617085
** this header information was factored out.
1707717086
**
17078
-** $Id: vdbeInt.h,v 1.171 2009/06/05 14:17:25 drh Exp $
17087
+** $Id: vdbeInt.h,v 1.174 2009/06/23 14:15:04 drh Exp $
1707917088
*/
1708017089
#ifndef _VDBEINT_H_
1708117090
#define _VDBEINT_H_
17082
-
17083
-/*
17084
-** intToKey() and keyToInt() used to transform the rowid. But with
17085
-** the latest versions of the design they are no-ops.
17086
-*/
17087
-#define keyToInt(X) (X)
17088
-#define intToKey(X) (X)
17089
-
1709017091
1709117092
/*
1709217093
** SQL is translated into a sequence of instructions to be
1709317094
** executed by a virtual machine. Each instruction is an instance
1709417095
** of the following structure.
@@ -17320,66 +17321,57 @@
1732017321
** "DROP TABLE" statements and to prevent some nasty side effects of
1732117322
** malloc failure when SQLite is invoked recursively by a virtual table
1732217323
** method function.
1732317324
*/
1732417325
struct Vdbe {
17325
- sqlite3 *db; /* The whole database */
17326
- Vdbe *pPrev,*pNext; /* Linked list of VDBEs with the same Vdbe.db */
17327
- int nOp; /* Number of instructions in the program */
17328
- int nOpAlloc; /* Number of slots allocated for aOp[] */
17329
- Op *aOp; /* Space to hold the virtual machine's program */
17330
- int nLabel; /* Number of labels used */
17331
- int nLabelAlloc; /* Number of slots allocated in aLabel[] */
17332
- int *aLabel; /* Space to hold the labels */
17333
- Mem **apArg; /* Arguments to currently executing user function */
17334
- Mem *aColName; /* Column names to return */
17335
- int nCursor; /* Number of slots in apCsr[] */
17336
- VdbeCursor **apCsr; /* One element of this array for each open cursor */
17337
- int nVar; /* Number of entries in aVar[] */
17338
- Mem *aVar; /* Values for the OP_Variable opcode. */
17339
- char **azVar; /* Name of variables */
17340
- int okVar; /* True if azVar[] has been initialized */
17326
+ sqlite3 *db; /* The database connection that owns this statement */
17327
+ Vdbe *pPrev,*pNext; /* Linked list of VDBEs with the same Vdbe.db */
17328
+ int nOp; /* Number of instructions in the program */
17329
+ int nOpAlloc; /* Number of slots allocated for aOp[] */
17330
+ Op *aOp; /* Space to hold the virtual machine's program */
17331
+ int nLabel; /* Number of labels used */
17332
+ int nLabelAlloc; /* Number of slots allocated in aLabel[] */
17333
+ int *aLabel; /* Space to hold the labels */
17334
+ Mem **apArg; /* Arguments to currently executing user function */
17335
+ Mem *aColName; /* Column names to return */
17336
+ Mem *pResultSet; /* Pointer to an array of results */
17337
+ u16 nResColumn; /* Number of columns in one row of the result set */
17338
+ u16 nCursor; /* Number of slots in apCsr[] */
17339
+ VdbeCursor **apCsr; /* One element of this array for each open cursor */
17340
+ u8 errorAction; /* Recovery action to do in case of an error */
17341
+ u8 okVar; /* True if azVar[] has been initialized */
17342
+ u16 nVar; /* Number of entries in aVar[] */
17343
+ Mem *aVar; /* Values for the OP_Variable opcode. */
17344
+ char **azVar; /* Name of variables */
1734117345
u32 magic; /* Magic number for sanity checking */
1734217346
int nMem; /* Number of memory locations currently allocated */
1734317347
Mem *aMem; /* The memory locations */
1734417348
int cacheCtr; /* VdbeCursor row cache generation counter */
1734517349
int contextStackTop; /* Index of top element in the context stack */
1734617350
int contextStackDepth; /* The size of the "context" stack */
1734717351
Context *contextStack; /* Stack used by opcodes ContextPush & ContextPop*/
1734817352
int pc; /* The program counter */
1734917353
int rc; /* Value to return */
17350
- int errorAction; /* Recovery action to do in case of an error */
17351
- int nResColumn; /* Number of columns in one row of the result set */
17352
- char **azResColumn; /* Values for one row of result */
1735317354
char *zErrMsg; /* Error message written here */
17354
- Mem *pResultSet; /* Pointer to an array of results */
1735517355
u8 explain; /* True if EXPLAIN present on SQL command */
1735617356
u8 changeCntOn; /* True to update the change-counter */
1735717357
u8 expired; /* True if the VM needs to be recompiled */
1735817358
u8 minWriteFileFormat; /* Minimum file format for writable database files */
1735917359
u8 inVtabMethod; /* See comments above */
1736017360
u8 usesStmtJournal; /* True if uses a statement journal */
1736117361
u8 readOnly; /* True for read-only statements */
1736217362
u8 isPrepareV2; /* True if prepared with prepare_v2() */
1736317363
int nChange; /* Number of db changes made since last reset */
17364
- i64 startTime; /* Time when query started - used for profiling */
1736517364
int btreeMask; /* Bitmask of db->aDb[] entries referenced */
17365
+ i64 startTime; /* Time when query started - used for profiling */
1736617366
BtreeMutexArray aMutex; /* An array of Btree used here and needing locks */
1736717367
int aCounter[2]; /* Counters used by sqlite3_stmt_status() */
17368
- char *zSql; /* Text of the SQL statement that generated this */
17368
+ char *zSql; /* Text of the SQL statement that generated this */
1736917369
void *pFree; /* Free this when deleting the vdbe */
17370
-#ifdef SQLITE_DEBUG
17371
- FILE *trace; /* Write an execution trace here, if not NULL */
17372
-#endif
1737317370
int iStatement; /* Statement number (or 0 if has not opened stmt) */
17374
-#ifdef SQLITE_SSE
17375
- int fetchId; /* Statement number used by sqlite3_fetch_statement */
17376
- int lru; /* Counter used for LRU cache replacement */
17377
-#endif
17378
-#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
17379
- Vdbe *pLruPrev;
17380
- Vdbe *pLruNext;
17371
+#ifdef SQLITE_DEBUG
17372
+ FILE *trace; /* Write an execution trace here, if not NULL */
1738117373
#endif
1738217374
};
1738317375
1738417376
/*
1738517377
** The following are allowed values for Vdbe.magic
@@ -17404,11 +17396,11 @@
1740417396
SQLITE_PRIVATE u32 sqlite3VdbeSerialGet(const unsigned char*, u32, Mem*);
1740517397
SQLITE_PRIVATE void sqlite3VdbeDeleteAuxData(VdbeFunc*, int);
1740617398
1740717399
int sqlite2BtreeKeyCompare(BtCursor *, const void *, int, int, int *);
1740817400
SQLITE_PRIVATE int sqlite3VdbeIdxKeyCompare(VdbeCursor*,UnpackedRecord*,int*);
17409
-SQLITE_PRIVATE int sqlite3VdbeIdxRowid(BtCursor *, i64 *);
17401
+SQLITE_PRIVATE int sqlite3VdbeIdxRowid(sqlite3*, BtCursor *, i64 *);
1741017402
SQLITE_PRIVATE int sqlite3MemCompare(const Mem*, const Mem*, const CollSeq*);
1741117403
SQLITE_PRIVATE int sqlite3VdbeExec(Vdbe*);
1741217404
SQLITE_PRIVATE int sqlite3VdbeList(Vdbe*);
1741317405
SQLITE_PRIVATE int sqlite3VdbeHalt(Vdbe*);
1741417406
SQLITE_PRIVATE int sqlite3VdbeChangeEncoding(Mem *, int);
@@ -17985,11 +17977,11 @@
1798517977
** Utility functions used throughout sqlite.
1798617978
**
1798717979
** This file contains functions for allocating memory, comparing
1798817980
** strings, and stuff like that.
1798917981
**
17990
-** $Id: util.c,v 1.258 2009/06/05 14:17:23 drh Exp $
17982
+** $Id: util.c,v 1.260 2009/06/17 16:20:04 drh Exp $
1799117983
*/
1799217984
#ifdef SQLITE_HAVE_ISNAN
1799317985
# include <math.h>
1799417986
#endif
1799517987
@@ -18077,11 +18069,11 @@
1807718069
** than the actual length of the string. For very long strings (greater
1807818070
** than 1GiB) the value returned might be less than the true string length.
1807918071
*/
1808018072
SQLITE_PRIVATE int sqlite3Strlen30(const char *z){
1808118073
const char *z2 = z;
18082
- if( NEVER(z==0) ) return 0;
18074
+ if( z==0 ) return 0;
1808318075
while( *z2 ){ z2++; }
1808418076
return 0x3fffffff & (int)(z2 - z);
1808518077
}
1808618078
1808718079
/*
@@ -18361,11 +18353,11 @@
1836118353
**
1836218354
** will return -8.
1836318355
*/
1836418356
static int compare2pow63(const char *zNum){
1836518357
int c;
18366
- c = memcmp(zNum,"922337203685477580",18);
18358
+ c = memcmp(zNum,"922337203685477580",18)*10;
1836718359
if( c==0 ){
1836818360
c = zNum[18] - '8';
1836918361
}
1837018362
return c;
1837118363
}
@@ -20864,11 +20856,11 @@
2086420856
** * sqlite3_vfs method implementations.
2086520857
** * Locking primitives for the proxy uber-locking-method. (MacOSX only)
2086620858
** * Definitions of sqlite3_vfs objects for all locking methods
2086720859
** plus implementations of sqlite3_os_init() and sqlite3_os_end().
2086820860
**
20869
-** $Id: os_unix.c,v 1.251 2009/05/08 11:34:37 danielk1977 Exp $
20861
+** $Id: os_unix.c,v 1.253 2009/06/17 13:09:39 drh Exp $
2087020862
*/
2087120863
#if SQLITE_OS_UNIX /* This file is used on unix only */
2087220864
2087320865
/*
2087420866
** There are various methods for file locking used for concurrency
@@ -21952,11 +21944,11 @@
2195221944
int rc; /* System call return code */
2195321945
int fd; /* The file descriptor for pFile */
2195421946
struct unixLockKey lockKey; /* Lookup key for the unixLockInfo structure */
2195521947
struct unixFileId fileId; /* Lookup key for the unixOpenCnt struct */
2195621948
struct stat statbuf; /* Low-level file information */
21957
- struct unixLockInfo *pLock; /* Candidate unixLockInfo object */
21949
+ struct unixLockInfo *pLock = 0;/* Candidate unixLockInfo object */
2195821950
struct unixOpenCnt *pOpen; /* Candidate unixOpenCnt object */
2195921951
2196021952
/* Get low-level information about the file that we can used to
2196121953
** create a unique name for the file.
2196221954
*/
@@ -22856,11 +22848,12 @@
2285622848
}
2285722849
2285822850
/* To fully unlock the database, delete the lock file */
2285922851
assert( locktype==NO_LOCK );
2286022852
if( unlink(zLockFile) ){
22861
- int rc, tErrno = errno;
22853
+ int rc = 0;
22854
+ int tErrno = errno;
2286222855
if( ENOENT != tErrno ){
2286322856
rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
2286422857
}
2286522858
if( IS_LOCK_ERROR(rc) ){
2286622859
pFile->lastErrno = tErrno;
@@ -25106,11 +25099,15 @@
2510625099
** Find the current time (in Universal Coordinated Time). Write the
2510725100
** current time and date as a Julian Day number into *prNow and
2510825101
** return 0. Return 1 if the time and date cannot be found.
2510925102
*/
2511025103
static int unixCurrentTime(sqlite3_vfs *NotUsed, double *prNow){
25111
-#if defined(NO_GETTOD)
25104
+#if defined(SQLITE_OMIT_FLOATING_POINT)
25105
+ time_t t;
25106
+ time(&t);
25107
+ *prNow = (((sqlite3_int64)t)/8640 + 24405875)/10;
25108
+#elif defined(NO_GETTOD)
2511225109
time_t t;
2511325110
time(&t);
2511425111
*prNow = t/86400.0 + 2440587.5;
2511525112
#elif OS_VXWORKS
2511625113
struct timespec sNow;
@@ -29299,11 +29296,11 @@
2929929296
** sqlite3_pcache interface). It also contains part of the implementation
2930029297
** of the SQLITE_CONFIG_PAGECACHE and sqlite3_release_memory() features.
2930129298
** If the default page cache implementation is overriden, then neither of
2930229299
** these two features are available.
2930329300
**
29304
-** @(#) $Id: pcache1.c,v 1.16 2009/06/03 21:04:36 drh Exp $
29301
+** @(#) $Id: pcache1.c,v 1.17 2009/06/09 18:58:53 shane Exp $
2930529302
*/
2930629303
2930729304
2930829305
typedef struct PCache1 PCache1;
2930929306
typedef struct PgHdr1 PgHdr1;
@@ -29650,11 +29647,11 @@
2965029647
*/
2965129648
static void pcache1TruncateUnsafe(
2965229649
PCache1 *pCache,
2965329650
unsigned int iLimit
2965429651
){
29655
- TESTONLY( int nPage = 0; ) /* Used to assert pCache->nPage is correct */
29652
+ TESTONLY( unsigned int nPage = 0; ) /* Used to assert pCache->nPage is correct */
2965629653
unsigned int h;
2965729654
assert( sqlite3_mutex_held(pcache1.mutex) );
2965829655
for(h=0; h<pCache->nHash; h++){
2965929656
PgHdr1 **pp = &pCache->apHash[h];
2966029657
PgHdr1 *pPage;
@@ -30498,11 +30495,11 @@
3049830495
** is separate from the database file. The pager also implements file
3049930496
** locking to prevent two processes from writing the same database
3050030497
** file simultaneously, or one process from reading the database while
3050130498
** another is writing.
3050230499
**
30503
-** @(#) $Id: pager.c,v 1.591 2009/06/02 21:31:39 drh Exp $
30500
+** @(#) $Id: pager.c,v 1.601 2009/06/22 05:43:24 danielk1977 Exp $
3050430501
*/
3050530502
#ifndef SQLITE_OMIT_DISKIO
3050630503
3050730504
/*
3050830505
** Macros for troubleshooting. Normally turned off
@@ -30582,15 +30579,18 @@
3058230579
3058330580
/*
3058430581
** A macro used for invoking the codec if there is one
3058530582
*/
3058630583
#ifdef SQLITE_HAS_CODEC
30587
-# define CODEC1(P,D,N,X) if( P->xCodec!=0 ){ P->xCodec(P->pCodecArg,D,N,X); }
30588
-# define CODEC2(P,D,N,X) ((char*)(P->xCodec!=0?P->xCodec(P->pCodecArg,D,N,X):D))
30584
+# define CODEC1(P,D,N,X,E) \
30585
+ if( P->xCodec && P->xCodec(P->pCodec,D,N,X)==0 ){ E; }
30586
+# define CODEC2(P,D,N,X,E,O) \
30587
+ if( P->xCodec==0 ){ O=(char*)D; }else \
30588
+ if( (O=(char*)(P->xCodec(P->pCodec,D,N,X)))==0 ){ E; }
3058930589
#else
30590
-# define CODEC1(P,D,N,X) /* NO-OP */
30591
-# define CODEC2(P,D,N,X) ((char*)D)
30590
+# define CODEC1(P,D,N,X,E) /* NO-OP */
30591
+# define CODEC2(P,D,N,X,E,O) O=(char*)D
3059230592
#endif
3059330593
3059430594
/*
3059530595
** The maximum allowed sector size. 16MB. If the xSectorsize() method
3059630596
** returns a value larger than this, then MAX_SECTOR_SIZE is used instead.
@@ -30766,11 +30766,12 @@
3076630766
PagerSavepoint *aSavepoint; /* Array of active savepoints */
3076730767
int nSavepoint; /* Number of elements in aSavepoint[] */
3076830768
char dbFileVers[16]; /* Changes whenever database file changes */
3076930769
u32 sectorSize; /* Assumed sector size during rollback */
3077030770
30771
- int nExtra; /* Add this many bytes to each in-memory page */
30771
+ u16 nExtra; /* Add this many bytes to each in-memory page */
30772
+ i16 nReserve; /* Number of unused bytes at end of each page */
3077230773
u32 vfsFlags; /* Flags for sqlite3_vfs.xOpen() */
3077330774
int pageSize; /* Number of bytes in a page */
3077430775
Pgno mxPgno; /* Maximum allowed size of the database */
3077530776
char *zFilename; /* Name of the database file */
3077630777
char *zJournal; /* Name of the journal file */
@@ -30781,11 +30782,13 @@
3078130782
int nRead, nWrite; /* Database pages read/written */
3078230783
#endif
3078330784
void (*xReiniter)(DbPage*); /* Call this routine when reloading pages */
3078430785
#ifdef SQLITE_HAS_CODEC
3078530786
void *(*xCodec)(void*,void*,Pgno,int); /* Routine for en/decoding data */
30786
- void *pCodecArg; /* First argument to xCodec() */
30787
+ void (*xCodecSizeChng)(void*,int,int); /* Notify of page size changes */
30788
+ void (*xCodecFree)(void*); /* Destructor for the codec */
30789
+ void *pCodec; /* First argument to xCodec... methods */
3078730790
#endif
3078830791
char *pTmpSpace; /* Pager.pageSize bytes of space for tmp use */
3078930792
i64 journalSizeLimit; /* Size limit for persistent journal files */
3079030793
PCache *pPCache; /* Pointer to page cache object */
3079130794
sqlite3_backup *pBackup; /* Pointer to list of ongoing backup processes */
@@ -31403,11 +31406,11 @@
3140331406
/* Update the page-size to match the value read from the journal.
3140431407
** Use a testcase() macro to make sure that malloc failure within
3140531408
** PagerSetPagesize() is tested.
3140631409
*/
3140731410
iPageSize16 = (u16)iPageSize;
31408
- rc = sqlite3PagerSetPagesize(pPager, &iPageSize16);
31411
+ rc = sqlite3PagerSetPagesize(pPager, &iPageSize16, -1);
3140931412
testcase( rc!=SQLITE_OK );
3141031413
assert( rc!=SQLITE_OK || iPageSize16==(u16)iPageSize );
3141131414
3141231415
/* Update the assumed sector-size to match the value used by
3141331416
** the process that created this journal. If this journal was
@@ -32005,11 +32008,15 @@
3200532008
i64 ofst = (pgno-1)*(i64)pPager->pageSize;
3200632009
rc = sqlite3OsWrite(pPager->fd, aData, pPager->pageSize, ofst);
3200732010
if( pgno>pPager->dbFileSize ){
3200832011
pPager->dbFileSize = pgno;
3200932012
}
32010
- sqlite3BackupUpdate(pPager->pBackup, pgno, aData);
32013
+ if( pPager->pBackup ){
32014
+ CODEC1(pPager, aData, pgno, 3, rc=SQLITE_NOMEM);
32015
+ sqlite3BackupUpdate(pPager->pBackup, pgno, aData);
32016
+ CODEC1(pPager, aData, pgno, 0, rc=SQLITE_NOMEM);
32017
+ }
3201132018
}else if( !isMainJrnl && pPg==0 ){
3201232019
/* If this is a rollback of a savepoint and data was not written to
3201332020
** the database and the page is not in-memory, there is a potential
3201432021
** problem. When the page is next fetched by the b-tree layer, it
3201532022
** will be read from the database file, which may or may not be
@@ -32074,11 +32081,11 @@
3207432081
if( pgno==1 ){
3207532082
memcpy(&pPager->dbFileVers, &((u8*)pData)[24],sizeof(pPager->dbFileVers));
3207632083
}
3207732084
3207832085
/* Decode the page just read from disk */
32079
- CODEC1(pPager, pData, pPg->pgno, 3);
32086
+ CODEC1(pPager, pData, pPg->pgno, 3, rc=SQLITE_NOMEM);
3208032087
sqlite3PcacheRelease(pPg);
3208132088
}
3208232089
return rc;
3208332090
}
3208432091
@@ -32837,10 +32844,25 @@
3283732844
*/
3283832845
SQLITE_PRIVATE void sqlite3PagerSetReiniter(Pager *pPager, void (*xReinit)(DbPage*)){
3283932846
pPager->xReiniter = xReinit;
3284032847
}
3284132848
32849
+/*
32850
+** Report the current page size and number of reserved bytes back
32851
+** to the codec.
32852
+*/
32853
+#ifdef SQLITE_HAS_CODEC
32854
+static void pagerReportSize(Pager *pPager){
32855
+ if( pPager->xCodecSizeChng ){
32856
+ pPager->xCodecSizeChng(pPager->pCodec, pPager->pageSize,
32857
+ (int)pPager->nReserve);
32858
+ }
32859
+}
32860
+#else
32861
+# define pagerReportSize(X) /* No-op if we do not support a codec */
32862
+#endif
32863
+
3284232864
/*
3284332865
** Change the page size used by the Pager object. The new page size
3284432866
** is passed in *pPageSize.
3284532867
**
3284632868
** If the pager is in the error state when this function is called, it
@@ -32867,11 +32889,11 @@
3286732889
** If the page size is not changed, either because one of the enumerated
3286832890
** conditions above is not true, the pager was in error state when this
3286932891
** function was called, or because the memory allocation attempt failed,
3287032892
** then *pPageSize is set to the old, retained page size before returning.
3287132893
*/
32872
-SQLITE_PRIVATE int sqlite3PagerSetPagesize(Pager *pPager, u16 *pPageSize){
32894
+SQLITE_PRIVATE int sqlite3PagerSetPagesize(Pager *pPager, u16 *pPageSize, int nReserve){
3287332895
int rc = pPager->errCode;
3287432896
if( rc==SQLITE_OK ){
3287532897
u16 pageSize = *pPageSize;
3287632898
assert( pageSize==0 || (pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE) );
3287732899
if( pageSize && pageSize!=pPager->pageSize
@@ -32888,10 +32910,14 @@
3288832910
pPager->pTmpSpace = pNew;
3288932911
sqlite3PcacheSetPageSize(pPager->pPCache, pageSize);
3289032912
}
3289132913
}
3289232914
*pPageSize = (u16)pPager->pageSize;
32915
+ if( nReserve<0 ) nReserve = pPager->nReserve;
32916
+ assert( nReserve>=0 && nReserve<1000 );
32917
+ pPager->nReserve = (i16)nReserve;
32918
+ pagerReportSize(pPager);
3289332919
}
3289432920
return rc;
3289532921
}
3289632922
3289732923
/*
@@ -33135,10 +33161,14 @@
3313533161
PAGERTRACE(("CLOSE %d\n", PAGERID(pPager)));
3313633162
IOTRACE(("CLOSE %p\n", pPager))
3313733163
sqlite3OsClose(pPager->fd);
3313833164
sqlite3PageFree(pPager->pTmpSpace);
3313933165
sqlite3PcacheClose(pPager->pPCache);
33166
+
33167
+#ifdef SQLITE_HAS_CODEC
33168
+ if( pPager->xCodecFree ) pPager->xCodecFree(pPager->pCodec);
33169
+#endif
3314033170
3314133171
assert( !pPager->aSavepoint && !pPager->pInJournal );
3314233172
assert( !isOpen(pPager->jfd) && !isOpen(pPager->sjfd) );
3314333173
3314433174
sqlite3_free(pPager);
@@ -33366,12 +33396,15 @@
3336633396
**
3336733397
** Also, do not write out any page that has the PGHDR_DONT_WRITE flag
3336833398
** set (set by sqlite3PagerDontWrite()).
3336933399
*/
3337033400
if( pgno<=pPager->dbSize && 0==(pList->flags&PGHDR_DONT_WRITE) ){
33371
- i64 offset = (pgno-1)*(i64)pPager->pageSize; /* Offset to write */
33372
- char *pData = CODEC2(pPager, pList->pData, pgno, 6); /* Data to write */
33401
+ i64 offset = (pgno-1)*(i64)pPager->pageSize; /* Offset to write */
33402
+ char *pData; /* Data to write */
33403
+
33404
+ /* Encode the database */
33405
+ CODEC2(pPager, pList->pData, pgno, 6, return SQLITE_NOMEM, pData);
3337333406
3337433407
/* Write out the page data. */
3337533408
rc = sqlite3OsWrite(pPager->fd, pData, pPager->pageSize, offset);
3337633409
3337733410
/* If page 1 was just written, update Pager.dbFileVers to match
@@ -33384,11 +33417,11 @@
3338433417
if( pgno>pPager->dbFileSize ){
3338533418
pPager->dbFileSize = pgno;
3338633419
}
3338733420
3338833421
/* Update any backup objects copying the contents of this pager. */
33389
- sqlite3BackupUpdate(pPager->pBackup, pgno, (u8 *)pData);
33422
+ sqlite3BackupUpdate(pPager->pBackup, pgno, (u8*)pList->pData);
3339033423
3339133424
PAGERTRACE(("STORE %d page %d hash(%08x)\n",
3339233425
PAGERID(pPager), pgno, pager_pagehash(pList)));
3339333426
IOTRACE(("PGOUT %p %d\n", pPager, pgno));
3339433427
PAGER_INCR(sqlite3_pager_writedb_count);
@@ -33422,12 +33455,13 @@
3342233455
int rc = SQLITE_OK;
3342333456
Pager *pPager = pPg->pPager;
3342433457
if( isOpen(pPager->sjfd) ){
3342533458
void *pData = pPg->pData;
3342633459
i64 offset = pPager->nSubRec*(4+pPager->pageSize);
33427
- char *pData2 = CODEC2(pPager, pData, pPg->pgno, 7);
33428
-
33460
+ char *pData2;
33461
+
33462
+ CODEC2(pPager, pData, pPg->pgno, 7, return SQLITE_NOMEM, pData2);
3342933463
PAGERTRACE(("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno));
3343033464
3343133465
assert( pageInJournal(pPg) || pPg->pgno>pPager->dbOrigSize );
3343233466
rc = write32bits(pPager->sjfd, offset, pPg->pgno);
3343333467
if( rc==SQLITE_OK ){
@@ -33745,18 +33779,19 @@
3374533779
** database is the same as a temp-file that is never written out to
3374633780
** disk and uses an in-memory rollback journal.
3374733781
*/
3374833782
tempFile = 1;
3374933783
pPager->state = PAGER_EXCLUSIVE;
33784
+ readOnly = (vfsFlags&SQLITE_OPEN_READONLY);
3375033785
}
3375133786
3375233787
/* The following call to PagerSetPagesize() serves to set the value of
3375333788
** Pager.pageSize and to allocate the Pager.pTmpSpace buffer.
3375433789
*/
3375533790
if( rc==SQLITE_OK ){
3375633791
assert( pPager->memDb==0 );
33757
- rc = sqlite3PagerSetPagesize(pPager, &szPageDflt);
33792
+ rc = sqlite3PagerSetPagesize(pPager, &szPageDflt, -1);
3375833793
testcase( rc!=SQLITE_OK );
3375933794
}
3376033795
3376133796
/* If an error occurred in either of the blocks above, free the
3376233797
** Pager structure and close the file.
@@ -33767,10 +33802,11 @@
3376733802
sqlite3_free(pPager);
3376833803
return rc;
3376933804
}
3377033805
3377133806
/* Initialize the PCache object. */
33807
+ assert( nExtra<1000 );
3377233808
nExtra = ROUND8(nExtra);
3377333809
sqlite3PcacheOpen(szPageDflt, nExtra, !memDb,
3377433810
!memDb?pagerStress:0, (void *)pPager, pPager->pPCache);
3377533811
3377633812
PAGERTRACE(("OPEN %d %s\n", FILEHANDLEID(pPager->fd), pPager->zFilename));
@@ -33802,11 +33838,11 @@
3380233838
pPager->fullSync = pPager->noSync ?0:1;
3380333839
pPager->sync_flags = SQLITE_SYNC_NORMAL;
3380433840
/* pPager->pFirst = 0; */
3380533841
/* pPager->pFirstSynced = 0; */
3380633842
/* pPager->pLast = 0; */
33807
- pPager->nExtra = nExtra;
33843
+ pPager->nExtra = (u16)nExtra;
3380833844
pPager->journalSizeLimit = SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT;
3380933845
assert( isOpen(pPager->fd) || tempFile );
3381033846
setSectorSize(pPager);
3381133847
if( memDb ){
3381233848
pPager->journalMode = PAGER_JOURNALMODE_MEMORY;
@@ -33966,11 +34002,11 @@
3396634002
}
3396734003
if( pgno==1 ){
3396834004
u8 *dbFileVers = &((u8*)pPg->pData)[24];
3396934005
memcpy(&pPager->dbFileVers, dbFileVers, sizeof(pPager->dbFileVers));
3397034006
}
33971
- CODEC1(pPager, pPg->pData, pgno, 3);
34007
+ CODEC1(pPager, pPg->pData, pgno, 3, rc = SQLITE_NOMEM);
3397234008
3397334009
PAGER_INCR(sqlite3_pager_readdb_count);
3397434010
PAGER_INCR(pPager->nRead);
3397534011
IOTRACE(("PGIN %p %d\n", pPager, pgno));
3397634012
PAGERTRACE(("FETCH %d page %d hash(%08x)\n",
@@ -34011,19 +34047,17 @@
3401134047
*/
3401234048
static int pagerSharedLock(Pager *pPager){
3401334049
int rc = SQLITE_OK; /* Return code */
3401434050
int isErrorReset = 0; /* True if recovering from error state */
3401534051
34016
- /* If this database is opened for exclusive access, has no outstanding
34017
- ** page references and is in an error-state, this is a chance to clear
34018
- ** the error. Discard the contents of the pager-cache and treat any
34019
- ** open journal file as a hot-journal.
34052
+ /* If this database has no outstanding page references and is in an
34053
+ ** error-state, this is a chance to clear the error. Discard the
34054
+ ** contents of the pager-cache and rollback any hot journal in the
34055
+ ** file-system.
3402034056
*/
34021
- if( !MEMDB && pPager->exclusiveMode
34022
- && sqlite3PcacheRefCount(pPager->pPCache)==0 && pPager->errCode
34023
- ){
34024
- if( isOpen(pPager->jfd) ){
34057
+ if( !MEMDB && sqlite3PcacheRefCount(pPager->pPCache)==0 && pPager->errCode ){
34058
+ if( isOpen(pPager->jfd) || pPager->zJournal ){
3402534059
isErrorReset = 1;
3402634060
}
3402734061
pPager->errCode = SQLITE_OK;
3402834062
pager_reset(pPager);
3402934063
}
@@ -34102,13 +34136,16 @@
3410234136
if( rc==SQLITE_OK && fout&SQLITE_OPEN_READONLY ){
3410334137
rc = SQLITE_CANTOPEN;
3410434138
sqlite3OsClose(pPager->jfd);
3410534139
}
3410634140
}else{
34107
- /* If the journal does not exist, that means some other process
34108
- ** has already rolled it back */
34109
- rc = SQLITE_BUSY;
34141
+ /* If the journal does not exist, it usually means that some
34142
+ ** other connection managed to get in and roll it back before
34143
+ ** this connection obtained the exclusive lock above. Or, it
34144
+ ** may mean that the pager was in the error-state when this
34145
+ ** function was called and the journal file does not exist. */
34146
+ rc = pager_end_transaction(pPager, 0);
3411034147
}
3411134148
}
3411234149
}
3411334150
if( rc!=SQLITE_OK ){
3411434151
goto failed;
@@ -34123,14 +34160,16 @@
3412334160
/* Playback and delete the journal. Drop the database write
3412434161
** lock and reacquire the read lock. Purge the cache before
3412534162
** playing back the hot-journal so that we don't end up with
3412634163
** an inconsistent cache.
3412734164
*/
34128
- rc = pager_playback(pPager, 1);
34129
- if( rc!=SQLITE_OK ){
34130
- rc = pager_error(pPager, rc);
34131
- goto failed;
34165
+ if( isOpen(pPager->jfd) ){
34166
+ rc = pager_playback(pPager, 1);
34167
+ if( rc!=SQLITE_OK ){
34168
+ rc = pager_error(pPager, rc);
34169
+ goto failed;
34170
+ }
3413234171
}
3413334172
assert( (pPager->state==PAGER_SHARED)
3413434173
|| (pPager->exclusiveMode && pPager->state>PAGER_SHARED)
3413534174
);
3413634175
}
@@ -34306,10 +34345,11 @@
3430634345
}
3430734346
assert( pPager->state!=PAGER_UNLOCK );
3430834347
3430934348
rc = sqlite3PcacheFetch(pPager->pPCache, pgno, 1, &pPg);
3431034349
if( rc!=SQLITE_OK ){
34350
+ pagerUnlockIfUnused(pPager);
3431134351
return rc;
3431234352
}
3431334353
assert( pPg->pgno==pgno );
3431434354
assert( pPg->pPager==pPager || pPg->pPager==0 );
3431534355
if( pPg->pPager==0 ){
@@ -34664,11 +34704,11 @@
3466434704
3466534705
/* We should never write to the journal file the page that
3466634706
** contains the database locks. The following assert verifies
3466734707
** that we do not. */
3466834708
assert( pPg->pgno!=PAGER_MJ_PGNO(pPager) );
34669
- pData2 = CODEC2(pPager, pData, pPg->pgno, 7);
34709
+ CODEC2(pPager, pData, pPg->pgno, 7, return SQLITE_NOMEM, pData2);
3467034710
cksum = pager_cksum(pPager, (u8*)pData2);
3467134711
rc = write32bits(pPager->jfd, pPager->journalOff, pPg->pgno);
3467234712
if( rc==SQLITE_OK ){
3467334713
rc = sqlite3OsWrite(pPager->jfd, pData2, pPager->pageSize,
3467434714
pPager->journalOff + 4);
@@ -35515,19 +35555,28 @@
3551535555
return pPager->noSync;
3551635556
}
3551735557
3551835558
#ifdef SQLITE_HAS_CODEC
3551935559
/*
35520
-** Set the codec for this pager
35560
+** Set or retrieve the codec for this pager
3552135561
*/
35522
-SQLITE_PRIVATE void sqlite3PagerSetCodec(
35562
+static void sqlite3PagerSetCodec(
3552335563
Pager *pPager,
3552435564
void *(*xCodec)(void*,void*,Pgno,int),
35525
- void *pCodecArg
35565
+ void (*xCodecSizeChng)(void*,int,int),
35566
+ void (*xCodecFree)(void*),
35567
+ void *pCodec
3552635568
){
35569
+ if( pPager->xCodecFree ) pPager->xCodecFree(pPager->pCodec);
3552735570
pPager->xCodec = xCodec;
35528
- pPager->pCodecArg = pCodecArg;
35571
+ pPager->xCodecSizeChng = xCodecSizeChng;
35572
+ pPager->xCodecFree = xCodecFree;
35573
+ pPager->pCodec = pCodec;
35574
+ pagerReportSize(pPager);
35575
+}
35576
+static void *sqlite3PagerGetCodec(Pager *pPager){
35577
+ return pPager->pCodec;
3552935578
}
3553035579
#endif
3553135580
3553235581
#ifndef SQLITE_OMIT_AUTOVACUUM
3553335582
/*
@@ -35815,11 +35864,11 @@
3581535864
** May you do good and not evil.
3581635865
** May you find forgiveness for yourself and forgive others.
3581735866
** May you share freely, never taking more than you give.
3581835867
**
3581935868
*************************************************************************
35820
-** $Id: btreeInt.h,v 1.46 2009/03/20 14:18:52 danielk1977 Exp $
35869
+** $Id: btreeInt.h,v 1.48 2009/06/22 12:05:10 drh Exp $
3582135870
**
3582235871
** This file implements a external (disk-based) database using BTrees.
3582335872
** For a detailed discussion of BTrees, refer to
3582435873
**
3582535874
** Donald E. Knuth, THE ART OF COMPUTER PROGRAMMING, Volume 3:
@@ -35876,10 +35925,21 @@
3587635925
** 24 4 File change counter
3587735926
** 28 4 Reserved for future use
3587835927
** 32 4 First freelist page
3587935928
** 36 4 Number of freelist pages in the file
3588035929
** 40 60 15 4-byte meta values passed to higher layers
35930
+**
35931
+** 40 4 Schema cookie
35932
+** 44 4 File format of schema layer
35933
+** 48 4 Size of page cache
35934
+** 52 4 Largest root-page (auto/incr_vacuum)
35935
+** 56 4 1=UTF-8 2=UTF16le 3=UTF16be
35936
+** 60 4 User version
35937
+** 64 4 Incremental vacuum mode
35938
+** 68 4 unused
35939
+** 72 4 unused
35940
+** 76 4 unused
3588135941
**
3588235942
** All of the integer values are big-endian (most significant byte first).
3588335943
**
3588435944
** The file change counter is incremented when the database is changed
3588535945
** This counter allows other processes to know when the file has changed
@@ -36441,13 +36501,16 @@
3644136501
SQLITE_PRIVATE int sqlite3BtreeGetPage(BtShared*, Pgno, MemPage**, int);
3644236502
SQLITE_PRIVATE int sqlite3BtreeInitPage(MemPage *pPage);
3644336503
SQLITE_PRIVATE void sqlite3BtreeParseCellPtr(MemPage*, u8*, CellInfo*);
3644436504
SQLITE_PRIVATE void sqlite3BtreeParseCell(MemPage*, int, CellInfo*);
3644536505
SQLITE_PRIVATE int sqlite3BtreeRestoreCursorPosition(BtCursor *pCur);
36506
+SQLITE_PRIVATE void sqlite3BtreeMoveToParent(BtCursor *pCur);
36507
+
36508
+#ifdef SQLITE_TEST
3644636509
SQLITE_PRIVATE void sqlite3BtreeGetTempCursor(BtCursor *pCur, BtCursor *pTempCur);
3644736510
SQLITE_PRIVATE void sqlite3BtreeReleaseTempCursor(BtCursor *pCur);
36448
-SQLITE_PRIVATE void sqlite3BtreeMoveToParent(BtCursor *pCur);
36511
+#endif
3644936512
3645036513
/************** End of btreeInt.h ********************************************/
3645136514
/************** Continuing where we left off in btmutex.c ********************/
3645236515
#ifndef SQLITE_OMIT_SHARED_CACHE
3645336516
#if SQLITE_THREADSAFE
@@ -36795,11 +36858,11 @@
3679536858
** May you do good and not evil.
3679636859
** May you find forgiveness for yourself and forgive others.
3679736860
** May you share freely, never taking more than you give.
3679836861
**
3679936862
*************************************************************************
36800
-** $Id: btree.c,v 1.619 2009/06/05 18:44:15 drh Exp $
36863
+** $Id: btree.c,v 1.639 2009/06/23 11:22:29 danielk1977 Exp $
3680136864
**
3680236865
** This file implements a external (disk-based) database using BTrees.
3680336866
** See the header comment on "btreeInt.h" for additional information.
3680436867
** Including a description of file format and an overview of operation.
3680536868
*/
@@ -36813,11 +36876,11 @@
3681336876
/*
3681436877
** Set this global variable to 1 to enable tracing using the TRACE
3681536878
** macro.
3681636879
*/
3681736880
#if 0
36818
-int sqlite3BtreeTrace=0; /* True to enable tracing */
36881
+int sqlite3BtreeTrace=1; /* True to enable tracing */
3681936882
# define TRACE(X) if(sqlite3BtreeTrace){printf X;fflush(stdout);}
3682036883
#else
3682136884
# define TRACE(X)
3682236885
#endif
3682336886
@@ -37423,11 +37486,10 @@
3742337486
}
3742437487
3742537488
#else /* if defined SQLITE_OMIT_AUTOVACUUM */
3742637489
#define ptrmapPut(w,x,y,z) SQLITE_OK
3742737490
#define ptrmapGet(w,x,y,z) SQLITE_OK
37428
- #define ptrmapPutOvfl(y,z) SQLITE_OK
3742937491
#endif
3743037492
3743137493
/*
3743237494
** Given a btree page and a cell index (0 means the first cell on
3743337495
** the page, 1 means the second cell, and so forth) return a pointer
@@ -37588,11 +37650,11 @@
3758837650
if( nSize>pPage->maxLocal ){
3758937651
nSize = minLocal;
3759037652
}
3759137653
nSize += 4;
3759237654
}
37593
- nSize += (pIter - pCell);
37655
+ nSize += (u32)(pIter - pCell);
3759437656
3759537657
/* The minimum size of any cell is 4 bytes. */
3759637658
if( nSize<4 ){
3759737659
nSize = 4;
3759837660
}
@@ -37615,27 +37677,16 @@
3761537677
static int ptrmapPutOvflPtr(MemPage *pPage, u8 *pCell){
3761637678
CellInfo info;
3761737679
assert( pCell!=0 );
3761837680
sqlite3BtreeParseCellPtr(pPage, pCell, &info);
3761937681
assert( (info.nData+(pPage->intKey?0:info.nKey))==info.nPayload );
37620
- if( (info.nData+(pPage->intKey?0:info.nKey))>info.nLocal ){
37682
+ if( info.iOverflow ){
3762137683
Pgno ovfl = get4byte(&pCell[info.iOverflow]);
3762237684
return ptrmapPut(pPage->pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno);
3762337685
}
3762437686
return SQLITE_OK;
3762537687
}
37626
-/*
37627
-** If the cell with index iCell on page pPage contains a pointer
37628
-** to an overflow page, insert an entry into the pointer-map
37629
-** for the overflow page.
37630
-*/
37631
-static int ptrmapPutOvfl(MemPage *pPage, int iCell){
37632
- u8 *pCell;
37633
- assert( sqlite3_mutex_held(pPage->pBt->mutex) );
37634
- pCell = findOverflowCell(pPage, iCell);
37635
- return ptrmapPutOvflPtr(pPage, pCell);
37636
-}
3763737688
#endif
3763837689
3763937690
3764037691
/*
3764137692
** Defragment the page given. All Cells are moved to the
@@ -37938,11 +37989,11 @@
3793837989
**
3793937990
** The following block of code checks early to see if a cell extends
3794037991
** past the end of a page boundary and causes SQLITE_CORRUPT to be
3794137992
** returned if it does.
3794237993
*/
37943
-#if defined(SQLITE_OVERREAD_CHECK) || 1
37994
+#if defined(SQLITE_ENABLE_OVERSIZE_CELL_CHECK)
3794437995
{
3794537996
int iCellFirst; /* First allowable cell index */
3794637997
int iCellLast; /* Last possible cell index */
3794737998
int i; /* Index into the cell pointer array */
3794837999
int sz; /* Size of a cell */
@@ -37976,11 +38027,11 @@
3797638027
size = get2byte(&data[pc+2]);
3797738028
if( next>0 && next<=pc+size+3 ){
3797838029
/* Free blocks must be in accending order */
3797938030
return SQLITE_CORRUPT_BKPT;
3798038031
}
37981
- nFree += size;
38032
+ nFree = nFree + size;
3798238033
pc = next;
3798338034
}
3798438035
3798538036
/* At this point, nFree contains the sum of the offset to the start
3798638037
** of the cell-content area plus the number of free bytes within
@@ -38386,11 +38437,11 @@
3838638437
#ifndef SQLITE_OMIT_AUTOVACUUM
3838738438
pBt->autoVacuum = (get4byte(&zDbHeader[36 + 4*4])?1:0);
3838838439
pBt->incrVacuum = (get4byte(&zDbHeader[36 + 7*4])?1:0);
3838938440
#endif
3839038441
}
38391
- rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize);
38442
+ rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve);
3839238443
if( rc ) goto btree_open_out;
3839338444
pBt->usableSize = pBt->pageSize - nReserve;
3839438445
assert( (pBt->pageSize & 7)==0 ); /* 8-byte alignment of pageSize */
3839538446
3839638447
#if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
@@ -38677,12 +38728,12 @@
3867738728
((pageSize-1)&pageSize)==0 ){
3867838729
assert( (pageSize & 7)==0 );
3867938730
assert( !pBt->pPage1 && !pBt->pCursor );
3868038731
pBt->pageSize = (u16)pageSize;
3868138732
freeTempSpace(pBt);
38682
- rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize);
3868338733
}
38734
+ rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve);
3868438735
pBt->usableSize = pBt->pageSize - (u16)nReserve;
3868538736
if( iFix ) pBt->pageSizeFixed = 1;
3868638737
sqlite3BtreeLeave(p);
3868738738
return rc;
3868838739
}
@@ -38833,15 +38884,16 @@
3883338884
*/
3883438885
releasePage(pPage1);
3883538886
pBt->usableSize = (u16)usableSize;
3883638887
pBt->pageSize = (u16)pageSize;
3883738888
freeTempSpace(pBt);
38838
- rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize);
38889
+ rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize,
38890
+ pageSize-usableSize);
3883938891
if( rc ) goto page1_init_failed;
3884038892
return SQLITE_OK;
3884138893
}
38842
- if( usableSize<500 ){
38894
+ if( usableSize<480 ){
3884338895
goto page1_init_failed;
3884438896
}
3884538897
pBt->pageSize = (u16)pageSize;
3884638898
pBt->usableSize = (u16)usableSize;
3884738899
#ifndef SQLITE_OMIT_AUTOVACUUM
@@ -39494,11 +39546,13 @@
3949439546
3949539547
assert( nRef==sqlite3PagerRefcount(pPager) );
3949639548
return rc;
3949739549
}
3949839550
39499
-#endif /* ifndef SQLITE_OMIT_AUTOVACUUM */
39551
+#else /* ifndef SQLITE_OMIT_AUTOVACUUM */
39552
+# define setChildPtrmaps(x) SQLITE_OK
39553
+#endif
3950039554
3950139555
/*
3950239556
** This routine does the first phase of a two-phase commit. This routine
3950339557
** causes a rollback journal to be created (if it does not already exist)
3950439558
** and populated with enough information so that if a power loss occurs
@@ -39991,10 +40045,11 @@
3999140045
sqlite3BtreeLeave(pBtree);
3999240046
}
3999340047
return SQLITE_OK;
3999440048
}
3999540049
40050
+#ifdef SQLITE_TEST
3999640051
/*
3999740052
** Make a temporary cursor by filling in the fields of pTempCur.
3999840053
** The temporary cursor is not on the cursor list for the Btree.
3999940054
*/
4000040055
SQLITE_PRIVATE void sqlite3BtreeGetTempCursor(BtCursor *pCur, BtCursor *pTempCur){
@@ -40006,11 +40061,13 @@
4000640061
for(i=0; i<=pTempCur->iPage; i++){
4000740062
sqlite3PagerRef(pTempCur->apPage[i]->pDbPage);
4000840063
}
4000940064
assert( pTempCur->pKey==0 );
4001040065
}
40066
+#endif /* SQLITE_TEST */
4001140067
40068
+#ifdef SQLITE_TEST
4001240069
/*
4001340070
** Delete a temporary cursor such as was made by the CreateTemporaryCursor()
4001440071
** function above.
4001540072
*/
4001640073
SQLITE_PRIVATE void sqlite3BtreeReleaseTempCursor(BtCursor *pCur){
@@ -40019,12 +40076,11 @@
4001940076
for(i=0; i<=pCur->iPage; i++){
4002040077
sqlite3PagerUnref(pCur->apPage[i]->pDbPage);
4002140078
}
4002240079
sqlite3_free(pCur->pKey);
4002340080
}
40024
-
40025
-
40081
+#endif /* SQLITE_TEST */
4002640082
4002740083
/*
4002840084
** Make sure the BtCursor* given in the argument has a valid
4002940085
** BtCursor.info structure. If it is not already valid, call
4003040086
** sqlite3BtreeParseCell() to fill it in.
@@ -41182,11 +41238,11 @@
4118241238
u8 exact
4118341239
){
4118441240
MemPage *pPage1;
4118541241
int rc;
4118641242
u32 n; /* Number of pages on the freelist */
41187
- int k; /* Number of leaves on the trunk of the freelist */
41243
+ u32 k; /* Number of leaves on the trunk of the freelist */
4118841244
MemPage *pTrunk = 0;
4118941245
MemPage *pPrevTrunk = 0;
4119041246
Pgno mxPage; /* Total size of the database file */
4119141247
4119241248
assert( sqlite3_mutex_held(pBt->mutex) );
@@ -41260,11 +41316,11 @@
4126041316
*pPgno = iTrunk;
4126141317
memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4);
4126241318
*ppPage = pTrunk;
4126341319
pTrunk = 0;
4126441320
TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1));
41265
- }else if( k>pBt->usableSize/4 - 2 ){
41321
+ }else if( k>(u32)(pBt->usableSize/4 - 2) ){
4126641322
/* Value of k is out of range. Database corruption */
4126741323
rc = SQLITE_CORRUPT_BKPT;
4126841324
goto end_allocate_page;
4126941325
#ifndef SQLITE_OMIT_AUTOVACUUM
4127041326
}else if( searchList && nearby==iTrunk ){
@@ -41320,21 +41376,22 @@
4132041376
}
4132141377
}
4132241378
pTrunk = 0;
4132341379
TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1));
4132441380
#endif
41325
- }else{
41381
+ }else if( k>0 ){
4132641382
/* Extract a leaf from the trunk */
41327
- int closest;
41383
+ u32 closest;
4132841384
Pgno iPage;
4132941385
unsigned char *aData = pTrunk->aData;
4133041386
rc = sqlite3PagerWrite(pTrunk->pDbPage);
4133141387
if( rc ){
4133241388
goto end_allocate_page;
4133341389
}
4133441390
if( nearby>0 ){
41335
- int i, dist;
41391
+ u32 i;
41392
+ int dist;
4133641393
closest = 0;
4133741394
dist = get4byte(&aData[8]) - nearby;
4133841395
if( dist<0 ) dist = -dist;
4133941396
for(i=1; i<k; i++){
4134041397
int d2 = get4byte(&aData[8+i*4]) - nearby;
@@ -41356,11 +41413,11 @@
4135641413
if( !searchList || iPage==nearby ){
4135741414
int noContent;
4135841415
Pgno nPage;
4135941416
*pPgno = iPage;
4136041417
nPage = pagerPagecount(pBt);
41361
- if( *pPgno>nPage ){
41418
+ if( iPage>nPage ){
4136241419
/* Free page off the end of the file */
4136341420
rc = SQLITE_CORRUPT_BKPT;
4136441421
goto end_allocate_page;
4136541422
}
4136641423
TRACE(("ALLOCATE: %d was leaf %d of %d on trunk %d"
@@ -41434,10 +41491,12 @@
4143441491
if( sqlite3PagerPageRefcount((*ppPage)->pDbPage)>1 ){
4143541492
releasePage(*ppPage);
4143641493
return SQLITE_CORRUPT_BKPT;
4143741494
}
4143841495
(*ppPage)->isInit = 0;
41496
+ }else{
41497
+ *ppPage = 0;
4143941498
}
4144041499
return rc;
4144141500
}
4144241501
4144341502
/*
@@ -41844,11 +41903,11 @@
4184441903
MemPage *pPage, /* Page into which we are copying */
4184541904
int i, /* New cell becomes the i-th cell of the page */
4184641905
u8 *pCell, /* Content of the new cell */
4184741906
int sz, /* Bytes of content in pCell */
4184841907
u8 *pTemp, /* Temp storage space for pCell, if needed */
41849
- u8 nSkip /* Do not write the first nSkip bytes of the cell */
41908
+ Pgno iChild /* If non-zero, replace first 4 bytes with this value */
4185041909
){
4185141910
int idx; /* Where to write new cell content in data[] */
4185241911
int j; /* Loop counter */
4185341912
int top; /* First byte of content for any cell in data[] */
4185441913
int end; /* First byte past the last cell pointer in data[] */
@@ -41855,10 +41914,12 @@
4185541914
int ins; /* Index in data[] where new cell pointer is inserted */
4185641915
int hdr; /* Offset into data[] of the page header */
4185741916
int cellOffset; /* Address of first cell pointer in data[] */
4185841917
u8 *data; /* The content of the whole page */
4185941918
u8 *ptr; /* Used for moving information around in data[] */
41919
+
41920
+ int nSkip = (iChild ? 4 : 0);
4186041921
4186141922
assert( i>=0 && i<=pPage->nCell+pPage->nOverflow );
4186241923
assert( pPage->nCell<=MX_CELL(pPage->pBt) && MX_CELL(pPage->pBt)<=5460 );
4186341924
assert( pPage->nOverflow<=ArraySize(pPage->aOvfl) );
4186441925
assert( sz==cellSizePtr(pPage, pCell) );
@@ -41866,15 +41927,17 @@
4186641927
if( pPage->nOverflow || sz+2>pPage->nFree ){
4186741928
if( pTemp ){
4186841929
memcpy(pTemp+nSkip, pCell+nSkip, sz-nSkip);
4186941930
pCell = pTemp;
4187041931
}
41932
+ if( iChild ){
41933
+ put4byte(pCell, iChild);
41934
+ }
4187141935
j = pPage->nOverflow++;
4187241936
assert( j<(int)(sizeof(pPage->aOvfl)/sizeof(pPage->aOvfl[0])) );
4187341937
pPage->aOvfl[j].pCell = pCell;
4187441938
pPage->aOvfl[j].idx = (u16)i;
41875
- pPage->nFree = 0;
4187641939
}else{
4187741940
int rc = sqlite3PagerWrite(pPage->pDbPage);
4187841941
if( rc!=SQLITE_OK ){
4187941942
return rc;
4188041943
}
@@ -41900,10 +41963,13 @@
4190041963
return SQLITE_CORRUPT_BKPT;
4190141964
}
4190241965
pPage->nCell++;
4190341966
pPage->nFree -= 2;
4190441967
memcpy(&data[idx+nSkip], pCell+nSkip, sz-nSkip);
41968
+ if( iChild ){
41969
+ put4byte(&data[idx], iChild);
41970
+ }
4190541971
for(j=end-2, ptr=&data[j]; j>ins; j-=2, ptr-=2){
4190641972
ptr[0] = ptr[-2];
4190741973
ptr[1] = ptr[-1];
4190841974
}
4190941975
put2byte(&data[ins], idx);
@@ -41911,18 +41977,11 @@
4191141977
#ifndef SQLITE_OMIT_AUTOVACUUM
4191241978
if( pPage->pBt->autoVacuum ){
4191341979
/* The cell may contain a pointer to an overflow page. If so, write
4191441980
** the entry for the overflow page into the pointer map.
4191541981
*/
41916
- CellInfo info;
41917
- sqlite3BtreeParseCellPtr(pPage, pCell, &info);
41918
- assert( (info.nData+(pPage->intKey?0:info.nKey))==info.nPayload );
41919
- if( info.iOverflow ){
41920
- Pgno pgnoOvfl = get4byte(&pCell[info.iOverflow]);
41921
- rc = ptrmapPut(pPage->pBt, pgnoOvfl, PTRMAP_OVERFLOW1, pPage->pgno);
41922
- if( rc!=SQLITE_OK ) return rc;
41923
- }
41982
+ rc = ptrmapPutOvflPtr(pPage, pCell);
4192441983
}
4192541984
#endif
4192641985
}
4192741986
4192841987
return SQLITE_OK;
@@ -41981,12 +42040,10 @@
4198142040
** The value of NN appears to give the best results overall.
4198242041
*/
4198342042
#define NN 1 /* Number of neighbors on either side of pPage */
4198442043
#define NB (NN*2+1) /* Total pages involved in the balance */
4198542044
41986
-/* Forward reference */
41987
-static int balance(BtCursor*, int);
4198842045
4198942046
#ifndef SQLITE_OMIT_QUICKBALANCE
4199042047
/*
4199142048
** This version of balance() handles the common special case where
4199242049
** a new entry is being inserted on the extreme right-end of the
@@ -42001,42 +42058,62 @@
4200142058
** fill up. On average.
4200242059
**
4200342060
** pPage is the leaf page which is the right-most page in the tree.
4200442061
** pParent is its parent. pPage must have a single overflow entry
4200542062
** which is also the right-most entry on the page.
42063
+**
42064
+** The pSpace buffer is used to store a temporary copy of the divider
42065
+** cell that will be inserted into pParent. Such a cell consists of a 4
42066
+** byte page number followed by a variable length integer. In other
42067
+** words, at most 13 bytes. Hence the pSpace buffer must be at
42068
+** least 13 bytes in size.
4200642069
*/
42007
-static int balance_quick(BtCursor *pCur){
42008
- MemPage *const pPage = pCur->apPage[pCur->iPage];
42009
- BtShared *const pBt = pCur->pBt;
42010
- MemPage *pNew = 0; /* Newly allocated page */
42070
+static int balance_quick(MemPage *pParent, MemPage *pPage, u8 *pSpace){
42071
+ BtShared *const pBt = pPage->pBt; /* B-Tree Database */
42072
+ MemPage *pNew; /* Newly allocated page */
4201142073
int rc; /* Return Code */
4201242074
Pgno pgnoNew; /* Page number of pNew */
4201342075
4201442076
assert( sqlite3_mutex_held(pPage->pBt->mutex) );
42077
+ assert( sqlite3PagerIswriteable(pParent->pDbPage) );
42078
+ assert( pPage->nOverflow==1 );
42079
+
4201542080
if( pPage->nCell<=0 ) return SQLITE_CORRUPT_BKPT;
4201642081
42017
- /* Allocate a new page. This page will become the right-sibling of pPage */
42082
+ /* Allocate a new page. This page will become the right-sibling of
42083
+ ** pPage. Make the parent page writable, so that the new divider cell
42084
+ ** may be inserted. If both these operations are successful, proceed.
42085
+ */
4201842086
rc = allocateBtreePage(pBt, &pNew, &pgnoNew, 0, 0);
4201942087
4202042088
if( rc==SQLITE_OK ){
42021
- /* The parentCell buffer is used to store a temporary copy of the divider
42022
- ** cell that will be inserted into pParent. Such a cell consists of a 4
42023
- ** byte page number followed by a variable length integer. In other
42024
- ** words, at most 13 bytes. Hence the parentCell buffer must be at
42025
- ** least 13 bytes in size.
42026
- */
42027
- MemPage * const pParent = pCur->apPage[pCur->iPage-1];
42028
- u8 parentCell[13];
42029
- u8 *pOut = &parentCell[4];
42089
+
42090
+ u8 *pOut = &pSpace[4];
4203042091
u8 *pCell = pPage->aOvfl[0].pCell;
4203142092
u16 szCell = cellSizePtr(pPage, pCell);
4203242093
u8 *pStop;
4203342094
4203442095
assert( sqlite3PagerIswriteable(pNew->pDbPage) );
42035
- zeroPage(pNew, pPage->aData[0]);
42096
+ assert( pPage->aData[0]==(PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF) );
42097
+ zeroPage(pNew, PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF);
4203642098
assemblePage(pNew, 1, &pCell, &szCell);
42037
- pPage->nOverflow = 0;
42099
+
42100
+ /* If this is an auto-vacuum database, update the pointer map
42101
+ ** with entries for the new page, and any pointer from the
42102
+ ** cell on the page to an overflow page. If either of these
42103
+ ** operations fails, the return code is set, but the contents
42104
+ ** of the parent page are still manipulated by thh code below.
42105
+ ** That is Ok, at this point the parent page is guaranteed to
42106
+ ** be marked as dirty. Returning an error code will cause a
42107
+ ** rollback, undoing any changes made to the parent page.
42108
+ */
42109
+ if( ISAUTOVACUUM ){
42110
+ rc = ptrmapPut(pBt, pgnoNew, PTRMAP_BTREE, pParent->pgno);
42111
+ if( szCell>pNew->minLocal && rc==SQLITE_OK ){
42112
+ rc = ptrmapPutOvflPtr(pNew, pCell);
42113
+ }
42114
+ }
4203842115
4203942116
/* Create a divider cell to insert into pParent. The divider cell
4204042117
** consists of a 4-byte page number (the page number of pPage) and
4204142118
** a variable length key value (which must be the same value as the
4204242119
** largest key on pPage).
@@ -42045,278 +42122,263 @@
4204542122
** cell on pPage. The first two fields of this cell are the
4204642123
** record-length (a variable length integer at most 32-bits in size)
4204742124
** and the key value (a variable length integer, may have any value).
4204842125
** The first of the while(...) loops below skips over the record-length
4204942126
** field. The second while(...) loop copies the key value from the
42050
- ** cell on pPage into the parentCell buffer.
42127
+ ** cell on pPage into the pSpace buffer.
4205142128
*/
42052
- put4byte(parentCell, pPage->pgno);
4205342129
pCell = findCell(pPage, pPage->nCell-1);
4205442130
pStop = &pCell[9];
4205542131
while( (*(pCell++)&0x80) && pCell<pStop );
4205642132
pStop = &pCell[9];
4205742133
while( ((*(pOut++) = *(pCell++))&0x80) && pCell<pStop );
4205842134
42059
- /* Insert the new divider cell into pParent */
42060
- insertCell(pParent, pParent->nCell, parentCell, pOut-parentCell, 0, 0);
42135
+ /* Insert the new divider cell into pParent. */
42136
+ insertCell(pParent,pParent->nCell,pSpace,(int)(pOut-pSpace),0,pPage->pgno);
4206142137
4206242138
/* Set the right-child pointer of pParent to point to the new page. */
4206342139
put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew);
4206442140
42065
- /* If this is an auto-vacuum database, update the pointer map
42066
- ** with entries for the new page, and any pointer from the
42067
- ** cell on the page to an overflow page.
42068
- */
42069
- if( ISAUTOVACUUM ){
42070
- rc = ptrmapPut(pBt, pgnoNew, PTRMAP_BTREE, pParent->pgno);
42071
- if( rc==SQLITE_OK ){
42072
- rc = ptrmapPutOvfl(pNew, 0);
42073
- }
42074
- }
42075
-
4207642141
/* Release the reference to the new page. */
4207742142
releasePage(pNew);
4207842143
}
4207942144
42080
- /* At this point the pPage->nFree variable is not set correctly with
42081
- ** respect to the content of the page (because it was set to 0 by
42082
- ** insertCell). So call sqlite3BtreeInitPage() to make sure it is
42083
- ** correct.
42084
- **
42085
- ** This has to be done even if an error will be returned. Normally, if
42086
- ** an error occurs during tree balancing, the contents of MemPage are
42087
- ** not important, as they will be recalculated when the page is rolled
42088
- ** back. But here, in balance_quick(), it is possible that pPage has
42089
- ** not yet been marked dirty or written into the journal file. Therefore
42090
- ** it will not be rolled back and so it is important to make sure that
42091
- ** the page data and contents of MemPage are consistent.
42092
- */
42093
- pPage->isInit = 0;
42094
- sqlite3BtreeInitPage(pPage);
42095
- assert( pPage->nOverflow==0 );
42096
-
42097
- /* If everything else succeeded, balance the parent page, in
42098
- ** case the divider cell inserted caused it to become overfull.
42099
- */
42100
- if( rc==SQLITE_OK ){
42101
- releasePage(pPage);
42102
- pCur->iPage--;
42103
- rc = balance(pCur, 0);
42104
- }
4210542145
return rc;
4210642146
}
4210742147
#endif /* SQLITE_OMIT_QUICKBALANCE */
4210842148
42149
+#if 0
4210942150
/*
42110
-** This routine redistributes Cells on pPage and up to NN*2 siblings
42111
-** of pPage so that all pages have about the same amount of free space.
42112
-** Usually NN siblings on either side of pPage is used in the balancing,
42113
-** though more siblings might come from one side if pPage is the first
42114
-** or last child of its parent. If pPage has fewer than 2*NN siblings
42115
-** (something which can only happen if pPage is the root page or a
42116
-** child of root) then all available siblings participate in the balancing.
42117
-**
42118
-** The number of siblings of pPage might be increased or decreased by one or
42119
-** two in an effort to keep pages nearly full but not over full. The root page
42120
-** is special and is allowed to be nearly empty. If pPage is
42121
-** the root page, then the depth of the tree might be increased
42122
-** or decreased by one, as necessary, to keep the root page from being
42123
-** overfull or completely empty.
42124
-**
42125
-** Note that when this routine is called, some of the Cells on pPage
42126
-** might not actually be stored in pPage->aData[]. This can happen
42127
-** if the page is overfull. Part of the job of this routine is to
42128
-** make sure all Cells for pPage once again fit in pPage->aData[].
42129
-**
42130
-** In the course of balancing the siblings of pPage, the parent of pPage
42131
-** might become overfull or underfull. If that happens, then this routine
42132
-** is called recursively on the parent.
42151
+** This function does not contribute anything to the operation of SQLite.
42152
+** it is sometimes activated temporarily while debugging code responsible
42153
+** for setting pointer-map entries.
42154
+*/
42155
+static int ptrmapCheckPages(MemPage **apPage, int nPage){
42156
+ int i, j;
42157
+ for(i=0; i<nPage; i++){
42158
+ Pgno n;
42159
+ u8 e;
42160
+ MemPage *pPage = apPage[i];
42161
+ BtShared *pBt = pPage->pBt;
42162
+ assert( pPage->isInit );
42163
+
42164
+ for(j=0; j<pPage->nCell; j++){
42165
+ CellInfo info;
42166
+ u8 *z;
42167
+
42168
+ z = findCell(pPage, j);
42169
+ sqlite3BtreeParseCellPtr(pPage, z, &info);
42170
+ if( info.iOverflow ){
42171
+ Pgno ovfl = get4byte(&z[info.iOverflow]);
42172
+ ptrmapGet(pBt, ovfl, &e, &n);
42173
+ assert( n==pPage->pgno && e==PTRMAP_OVERFLOW1 );
42174
+ }
42175
+ if( !pPage->leaf ){
42176
+ Pgno child = get4byte(z);
42177
+ ptrmapGet(pBt, child, &e, &n);
42178
+ assert( n==pPage->pgno && e==PTRMAP_BTREE );
42179
+ }
42180
+ }
42181
+ if( !pPage->leaf ){
42182
+ Pgno child = get4byte(&pPage->aData[pPage->hdrOffset+8]);
42183
+ ptrmapGet(pBt, child, &e, &n);
42184
+ assert( n==pPage->pgno && e==PTRMAP_BTREE );
42185
+ }
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
42196
+** page are used in the balancing, though both siblings might come from one
42197
+** side if the page is the first or last child of its parent. If the page
42198
+** has fewer than 2 siblings (something which can only happen if the page
42199
+** is a root page or a child of a root page) then all available siblings
42200
+** participate in the balancing.
42201
+**
42202
+** The number of siblings of the page might be increased or decreased by
42203
+** one or two in an effort to keep pages nearly full but not over full.
42204
+**
42205
+** Note that when this routine is called, some of the cells on the page
42206
+** might not actually be stored in MemPage.aData[]. This can happen
42207
+** if the page is overfull. This routine ensures that all cells allocated
42208
+** to the page and its siblings fit into MemPage.aData[] before returning.
42209
+**
42210
+** In the course of balancing the page and its siblings, cells may be
42211
+** inserted into or removed from the parent page (pParent). Doing so
42212
+** may cause the parent page to become overfull or underfull. If this
42213
+** happens, it is the responsibility of the caller to invoke the correct
42214
+** balancing routine to fix this problem (see the balance() routine).
4213342215
**
4213442216
** If this routine fails for any reason, it might leave the database
42135
-** in a corrupted state. So if this routine fails, the database should
42217
+** in a corrupted state. So if this routine fails, the database should
4213642218
** be rolled back.
42219
+**
42220
+** The third argument to this function, aOvflSpace, is a pointer to a
42221
+** buffer page-size bytes in size. If, in inserting cells into the parent
42222
+** page (pParent), the parent page becomes overfull, this buffer is
42223
+** used to store the parents overflow cells. Because this function inserts
42224
+** a maximum of four divider cells into the parent page, and the maximum
42225
+** size of a cell stored within an internal node is always less than 1/4
42226
+** of the page-size, the aOvflSpace[] buffer is guaranteed to be large
42227
+** enough for all overflow cells.
42228
+**
42229
+** If aOvflSpace is set to a null pointer, this function returns
42230
+** SQLITE_NOMEM.
4213742231
*/
42138
-static int balance_nonroot(BtCursor *pCur){
42139
- MemPage *pPage; /* The over or underfull page to balance */
42140
- MemPage *pParent; /* The parent of pPage */
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
+){
4214142237
BtShared *pBt; /* The whole database */
4214242238
int nCell = 0; /* Number of cells in apCell[] */
4214342239
int nMaxCells = 0; /* Allocated size of apCell, szCell, aFrom. */
42144
- int nOld = 0; /* Number of pages in apOld[] */
4214542240
int nNew = 0; /* Number of pages in apNew[] */
42146
- int nDiv; /* Number of cells in apDiv[] */
42241
+ int nOld; /* Number of pages in apOld[] */
4214742242
int i, j, k; /* Loop counters */
42148
- int idx; /* Index of pPage in pParent->aCell[] */
4214942243
int nxDiv; /* Next divider slot in pParent->aCell[] */
42150
- int rc; /* The return code */
42244
+ int rc = SQLITE_OK; /* The return code */
4215142245
int leafCorrection; /* 4 if pPage is a leaf. 0 if not */
4215242246
int leafData; /* True if pPage is a leaf of a LEAFDATA tree */
4215342247
int usableSpace; /* Bytes in pPage beyond the header */
4215442248
int pageFlags; /* Value of pPage->aData[0] */
4215542249
int subtotal; /* Subtotal of bytes in cells on one page */
4215642250
int iSpace1 = 0; /* First unused byte of aSpace1[] */
42157
- int iSpace2 = 0; /* First unused byte of aSpace2[] */
42251
+ int iOvflSpace = 0; /* First unused byte of aOvflSpace[] */
4215842252
int szScratch; /* Size of scratch memory requested */
4215942253
MemPage *apOld[NB]; /* pPage and up to two siblings */
42160
- Pgno pgnoOld[NB]; /* Page numbers for each page in apOld[] */
4216142254
MemPage *apCopy[NB]; /* Private copies of apOld[] pages */
4216242255
MemPage *apNew[NB+2]; /* pPage and up to NB siblings after balancing */
42163
- Pgno pgnoNew[NB+2]; /* Page numbers for each page in apNew[] */
42164
- u8 *apDiv[NB]; /* Divider cells in pParent */
42256
+ u8 *pRight; /* Location in parent of right-sibling pointer */
42257
+ u8 *apDiv[NB-1]; /* Divider cells in pParent */
4216542258
int cntNew[NB+2]; /* Index in aCell[] of cell after i-th page */
4216642259
int szNew[NB+2]; /* Combined size of cells place on i-th page */
4216742260
u8 **apCell = 0; /* All cells begin balanced */
4216842261
u16 *szCell; /* Local size of all cells in apCell[] */
42169
- u8 *aCopy[NB]; /* Space for holding data of apCopy[] */
42170
- u8 *aSpace1; /* Space for copies of dividers cells before balance */
42171
- u8 *aSpace2 = 0; /* Space for overflow dividers cells after balance */
42172
- u8 *aFrom = 0;
42173
-
42174
- pPage = pCur->apPage[pCur->iPage];
42175
- assert( sqlite3_mutex_held(pPage->pBt->mutex) );
42176
- VVA_ONLY( pCur->pagesShuffled = 1 );
42177
-
42178
- /*
42179
- ** Find the parent page.
42180
- */
42181
- assert( pCur->iPage>0 );
42182
- assert( pPage->isInit );
42183
- assert( sqlite3PagerIswriteable(pPage->pDbPage) || pPage->nOverflow==1 );
42184
- pBt = pPage->pBt;
42185
- pParent = pCur->apPage[pCur->iPage-1];
42186
- assert( pParent );
42187
- if( SQLITE_OK!=(rc = sqlite3PagerWrite(pParent->pDbPage)) ){
42188
- goto balance_cleanup;
42189
- }
42190
-
42262
+ u8 *aSpace1; /* Space for copies of dividers cells */
42263
+ Pgno pgno; /* Temp var to store a page number in */
42264
+
42265
+ pBt = pParent->pBt;
42266
+ assert( sqlite3_mutex_held(pBt->mutex) );
42267
+ assert( sqlite3PagerIswriteable(pParent->pDbPage) );
42268
+
42269
+#if 0
4219142270
TRACE(("BALANCE: begin page %d child of %d\n", pPage->pgno, pParent->pgno));
42192
-
42193
-#ifndef SQLITE_OMIT_QUICKBALANCE
42194
- /*
42195
- ** A special case: If a new entry has just been inserted into a
42196
- ** table (that is, a btree with integer keys and all data at the leaves)
42197
- ** and the new entry is the right-most entry in the tree (it has the
42198
- ** largest key) then use the special balance_quick() routine for
42199
- ** balancing. balance_quick() is much faster and results in a tighter
42200
- ** packing of data in the common case.
42201
- */
42202
- if( pPage->leaf &&
42203
- pPage->intKey &&
42204
- pPage->nOverflow==1 &&
42205
- pPage->aOvfl[0].idx==pPage->nCell &&
42206
- pParent->pgno!=1 &&
42207
- get4byte(&pParent->aData[pParent->hdrOffset+8])==pPage->pgno
42208
- ){
42209
- assert( pPage->intKey );
42210
- /*
42211
- ** TODO: Check the siblings to the left of pPage. It may be that
42212
- ** they are not full and no new page is required.
42213
- */
42214
- return balance_quick(pCur);
42215
- }
4221642271
#endif
4221742272
42218
- if( SQLITE_OK!=(rc = sqlite3PagerWrite(pPage->pDbPage)) ){
42219
- goto balance_cleanup;
42220
- }
42221
-
42222
- /*
42223
- ** Find the cell in the parent page whose left child points back
42224
- ** to pPage. The "idx" variable is the index of that cell. If pPage
42225
- ** is the rightmost child of pParent then set idx to pParent->nCell
42226
- */
42227
- idx = pCur->aiIdx[pCur->iPage-1];
42228
- assertParentIndex(pParent, idx, pPage->pgno);
42229
-
42230
- /*
42231
- ** Find sibling pages to pPage and the cells in pParent that divide
42232
- ** the siblings. An attempt is made to find NN siblings on either
42233
- ** side of pPage. More siblings are taken from one side, however, if
42234
- ** pPage there are fewer than NN siblings on the other side. If pParent
42235
- ** has NB or fewer children then all children of pParent are taken.
42236
- */
42237
- nxDiv = idx - NN;
42238
- if( nxDiv + NB > pParent->nCell ){
42239
- nxDiv = pParent->nCell - NB + 1;
42240
- }
42241
- if( nxDiv<0 ){
42273
+ /* At this point pParent may have at most one overflow cell. And if
42274
+ ** this overflow cell is present, it must be the cell with
42275
+ ** index iParentIdx. This scenario comes about when this function
42276
+ ** is called (indirectly) from sqlite3BtreeDelete(). */
42277
+ assert( pParent->nOverflow==0 || pParent->nOverflow==1 );
42278
+ assert( pParent->nOverflow==0 || pParent->aOvfl[0].idx==iParentIdx );
42279
+
42280
+ if( !aOvflSpace ){
42281
+ return SQLITE_NOMEM;
42282
+ }
42283
+
42284
+ /* Find the sibling pages to balance. Also locate the cells in pParent
42285
+ ** that divide the siblings. An attempt is made to find NN siblings on
42286
+ ** either side of pPage. More siblings are taken from one side, however,
42287
+ ** if there are fewer than NN siblings on the other side. If pParent
42288
+ ** has NB or fewer children then all children of pParent are taken.
42289
+ **
42290
+ ** This loop also drops the divider cells from the parent page. This
42291
+ ** way, the remainder of the function does not have to deal with any
42292
+ ** overflow cells in the parent page, as if one existed it has already
42293
+ ** been removed. */
42294
+ i = pParent->nOverflow + pParent->nCell;
42295
+ if( i<2 ){
4224242296
nxDiv = 0;
42243
- }
42244
- nDiv = 0;
42245
- for(i=0, k=nxDiv; i<NB; i++, k++){
42246
- if( k<pParent->nCell ){
42247
- apDiv[i] = findCell(pParent, k);
42248
- nDiv++;
42249
- assert( !pParent->leaf );
42250
- pgnoOld[i] = get4byte(apDiv[i]);
42251
- }else if( k==pParent->nCell ){
42252
- pgnoOld[i] = get4byte(&pParent->aData[pParent->hdrOffset+8]);
42253
- }else{
42254
- break;
42255
- }
42256
- rc = getAndInitPage(pBt, pgnoOld[i], &apOld[i]);
42257
- if( rc ) goto balance_cleanup;
42258
- /* apOld[i]->idxParent = k; */
42259
- apCopy[i] = 0;
42260
- assert( i==nOld );
42261
- nOld++;
42297
+ nOld = i+1;
42298
+ }else{
42299
+ nOld = 3;
42300
+ if( iParentIdx==0 ){
42301
+ nxDiv = 0;
42302
+ }else if( iParentIdx==i ){
42303
+ nxDiv = i-2;
42304
+ }else{
42305
+ nxDiv = iParentIdx-1;
42306
+ }
42307
+ i = 2;
42308
+ }
42309
+ if( (i+nxDiv-pParent->nOverflow)==pParent->nCell ){
42310
+ pRight = &pParent->aData[pParent->hdrOffset+8];
42311
+ }else{
42312
+ pRight = findCell(pParent, i+nxDiv-pParent->nOverflow);
42313
+ }
42314
+ pgno = get4byte(pRight);
42315
+ while( 1 ){
42316
+ rc = getAndInitPage(pBt, pgno, &apOld[i]);
42317
+ if( rc ){
42318
+ memset(apOld, 0, i*sizeof(MemPage*));
42319
+ goto balance_cleanup;
42320
+ }
4226242321
nMaxCells += 1+apOld[i]->nCell+apOld[i]->nOverflow;
42322
+ if( (i--)==0 ) break;
42323
+
42324
+ if( pParent->nOverflow && i+nxDiv==pParent->aOvfl[0].idx ){
42325
+ apDiv[i] = pParent->aOvfl[0].pCell;
42326
+ pgno = get4byte(apDiv[i]);
42327
+ szNew[i] = cellSizePtr(pParent, apDiv[i]);
42328
+ pParent->nOverflow = 0;
42329
+ }else{
42330
+ apDiv[i] = findCell(pParent, i+nxDiv-pParent->nOverflow);
42331
+ pgno = get4byte(apDiv[i]);
42332
+ szNew[i] = cellSizePtr(pParent, apDiv[i]);
42333
+
42334
+ /* Drop the cell from the parent page. apDiv[i] still points to
42335
+ ** the cell within the parent, even though it has been dropped.
42336
+ ** This is safe because dropping a cell only overwrites the first
42337
+ ** four bytes of it, and this function does not need the first
42338
+ ** four bytes of the divider cell. So the pointer is safe to use
42339
+ ** later on.
42340
+ **
42341
+ ** Unless SQLite is compiled in secure-delete mode. In this case,
42342
+ ** the dropCell() routine will overwrite the entire cell with zeroes.
42343
+ ** In this case, temporarily copy the cell into the aOvflSpace[]
42344
+ ** buffer. It will be copied out again as soon as the aSpace[] buffer
42345
+ ** is allocated. */
42346
+#ifdef SQLITE_SECURE_DELETE
42347
+ memcpy(&aOvflSpace[apDiv[i]-pParent->aData], apDiv[i], szNew[i]);
42348
+ apDiv[i] = &aOvflSpace[apDiv[i]-pParent->aData];
42349
+#endif
42350
+ dropCell(pParent, i+nxDiv-pParent->nOverflow, szNew[i]);
42351
+ }
4226342352
}
4226442353
4226542354
/* Make nMaxCells a multiple of 4 in order to preserve 8-byte
4226642355
** alignment */
4226742356
nMaxCells = (nMaxCells + 3)&~3;
4226842357
4226942358
/*
4227042359
** Allocate space for memory structures
4227142360
*/
42361
+ k = pBt->pageSize + ROUND8(sizeof(MemPage));
4227242362
szScratch =
4227342363
nMaxCells*sizeof(u8*) /* apCell */
4227442364
+ nMaxCells*sizeof(u16) /* szCell */
42275
- + (ROUND8(sizeof(MemPage))+pBt->pageSize)*NB /* aCopy */
4227642365
+ pBt->pageSize /* aSpace1 */
42277
- + (ISAUTOVACUUM ? nMaxCells : 0); /* aFrom */
42366
+ + k*nOld; /* Page copies (apCopy) */
4227842367
apCell = sqlite3ScratchMalloc( szScratch );
4227942368
if( apCell==0 ){
4228042369
rc = SQLITE_NOMEM;
4228142370
goto balance_cleanup;
4228242371
}
4228342372
szCell = (u16*)&apCell[nMaxCells];
42284
- aCopy[0] = (u8*)&szCell[nMaxCells];
42285
- assert( EIGHT_BYTE_ALIGNMENT(aCopy[0]) );
42286
- for(i=1; i<NB; i++){
42287
- aCopy[i] = &aCopy[i-1][pBt->pageSize+ROUND8(sizeof(MemPage))];
42288
- assert( ((aCopy[i] - (u8*)0) & 7)==0 ); /* 8-byte alignment required */
42289
- }
42290
- aSpace1 = &aCopy[NB-1][pBt->pageSize+ROUND8(sizeof(MemPage))];
42373
+ aSpace1 = (u8*)&szCell[nMaxCells];
4229142374
assert( EIGHT_BYTE_ALIGNMENT(aSpace1) );
42292
- if( ISAUTOVACUUM ){
42293
- aFrom = &aSpace1[pBt->pageSize];
42294
- }
42295
- aSpace2 = sqlite3PageMalloc(pBt->pageSize);
42296
- if( aSpace2==0 ){
42297
- rc = SQLITE_NOMEM;
42298
- goto balance_cleanup;
42299
- }
42300
-
42301
- /*
42302
- ** Make copies of the content of pPage and its siblings into aOld[].
42303
- ** The rest of this function will use data from the copies rather
42304
- ** that the original pages since the original pages will be in the
42305
- ** process of being overwritten.
42306
- */
42307
- for(i=0; i<nOld; i++){
42308
- MemPage *p = apCopy[i] = (MemPage*)aCopy[i];
42309
- memcpy(p, apOld[i], sizeof(MemPage));
42310
- p->aData = (void*)&p[1];
42311
- memcpy(p->aData, apOld[i]->aData, pBt->pageSize);
42312
- }
4231342375
4231442376
/*
4231542377
** Load pointers to all cells on sibling pages and the divider cells
4231642378
** into the local apCell[] array. Make copies of the divider cells
42317
- ** into space obtained form aSpace1[] and remove the the divider Cells
42379
+ ** into space obtained from aSpace1[] and remove the the divider Cells
4231842380
** from pParent.
4231942381
**
4232042382
** If the siblings are on leaf pages, then the child pointers of the
4232142383
** divider cells are stripped from the cells before they are copied
4232242384
** into aSpace1[]. In this way, all cells in apCell[] are without
@@ -42325,72 +42387,58 @@
4232542387
** are alike.
4232642388
**
4232742389
** leafCorrection: 4 if pPage is a leaf. 0 if pPage is not a leaf.
4232842390
** leafData: 1 if pPage holds key+data and pParent holds only keys.
4232942391
*/
42330
- nCell = 0;
42331
- leafCorrection = pPage->leaf*4;
42332
- leafData = pPage->hasData;
42392
+ leafCorrection = apOld[0]->leaf*4;
42393
+ leafData = apOld[0]->hasData;
4233342394
for(i=0; i<nOld; i++){
42334
- MemPage *pOld = apCopy[i];
42335
- int limit = pOld->nCell+pOld->nOverflow;
42395
+ int limit;
42396
+
42397
+ /* Before doing anything else, take a copy of the i'th original sibling
42398
+ ** The rest of this function will use data from the copies rather
42399
+ ** that the original pages since the original pages will be in the
42400
+ ** process of being overwritten. */
42401
+ MemPage *pOld = apCopy[i] = (MemPage*)&aSpace1[pBt->pageSize + k*i];
42402
+ memcpy(pOld, apOld[i], sizeof(MemPage));
42403
+ pOld->aData = (void*)&pOld[1];
42404
+ memcpy(pOld->aData, apOld[i]->aData, pBt->pageSize);
42405
+
42406
+ limit = pOld->nCell+pOld->nOverflow;
4233642407
for(j=0; j<limit; j++){
4233742408
assert( nCell<nMaxCells );
4233842409
apCell[nCell] = findOverflowCell(pOld, j);
4233942410
szCell[nCell] = cellSizePtr(pOld, apCell[nCell]);
42340
- if( ISAUTOVACUUM ){
42341
- int a;
42342
- aFrom[nCell] = (u8)i; assert( i>=0 && i<6 );
42343
- for(a=0; a<pOld->nOverflow; a++){
42344
- if( pOld->aOvfl[a].pCell==apCell[nCell] ){
42345
- aFrom[nCell] = 0xFF;
42346
- break;
42347
- }
42348
- }
42349
- }
42350
- nCell++;
42351
- }
42352
- if( i<nOld-1 ){
42353
- u16 sz = cellSizePtr(pParent, apDiv[i]);
42354
- if( leafData ){
42355
- /* With the LEAFDATA flag, pParent cells hold only INTKEYs that
42356
- ** are duplicates of keys on the child pages. We need to remove
42357
- ** the divider cells from pParent, but the dividers cells are not
42358
- ** added to apCell[] because they are duplicates of child cells.
42359
- */
42360
- dropCell(pParent, nxDiv, sz);
42361
- }else{
42362
- u8 *pTemp;
42363
- assert( nCell<nMaxCells );
42364
- szCell[nCell] = sz;
42365
- pTemp = &aSpace1[iSpace1];
42366
- iSpace1 += sz;
42367
- assert( sz<=pBt->pageSize/4 );
42368
- assert( iSpace1<=pBt->pageSize );
42369
- memcpy(pTemp, apDiv[i], sz);
42370
- apCell[nCell] = pTemp+leafCorrection;
42371
- if( ISAUTOVACUUM ){
42372
- aFrom[nCell] = 0xFF;
42373
- }
42374
- dropCell(pParent, nxDiv, sz);
42375
- assert( leafCorrection==0 || leafCorrection==4 );
42376
- szCell[nCell] -= (u16)leafCorrection;
42377
- assert( get4byte(pTemp)==pgnoOld[i] );
42378
- if( !pOld->leaf ){
42379
- assert( leafCorrection==0 );
42380
- /* The right pointer of the child page pOld becomes the left
42381
- ** pointer of the divider cell */
42382
- memcpy(apCell[nCell], &pOld->aData[pOld->hdrOffset+8], 4);
42383
- }else{
42384
- assert( leafCorrection==4 );
42385
- if( szCell[nCell]<4 ){
42386
- /* Do not allow any cells smaller than 4 bytes. */
42387
- szCell[nCell] = 4;
42388
- }
42389
- }
42390
- nCell++;
42391
- }
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
+ 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 */
42431
+ memcpy(apCell[nCell], &pOld->aData[8], 4);
42432
+ }else{
42433
+ assert( leafCorrection==4 );
42434
+ if( szCell[nCell]<4 ){
42435
+ /* Do not allow any cells smaller than 4 bytes. */
42436
+ szCell[nCell] = 4;
42437
+ }
42438
+ }
42439
+ nCell++;
4239242440
}
4239342441
}
4239442442
4239542443
/*
4239642444
** Figure out the number of pages needed to hold all nCell cells.
@@ -42416,10 +42464,11 @@
4241642464
szNew[k] = subtotal - szCell[i];
4241742465
cntNew[k] = i;
4241842466
if( leafData ){ i--; }
4241942467
subtotal = 0;
4242042468
k++;
42469
+ if( k>NB+1 ){ rc = SQLITE_CORRUPT; goto balance_cleanup; }
4242142470
}
4242242471
}
4242342472
szNew[k] = subtotal;
4242442473
cntNew[k] = nCell;
4242542474
k++;
@@ -42459,30 +42508,46 @@
4245942508
** a virtual root page. A virtual root page is when the real root
4246042509
** page is page 1 and we are the only child of that page.
4246142510
*/
4246242511
assert( cntNew[0]>0 || (pParent->pgno==1 && pParent->nCell==0) );
4246342512
42513
+ TRACE(("BALANCE: old: %d %d %d ",
42514
+ apOld[0]->pgno,
42515
+ nOld>=2 ? apOld[1]->pgno : 0,
42516
+ nOld>=3 ? apOld[2]->pgno : 0
42517
+ ));
42518
+
4246442519
/*
4246542520
** Allocate k new pages. Reuse old pages where possible.
4246642521
*/
42467
- assert( pPage->pgno>1 );
42468
- pageFlags = pPage->aData[0];
42522
+ if( apOld[0]->pgno<=1 ){
42523
+ rc = SQLITE_CORRUPT;
42524
+ goto balance_cleanup;
42525
+ }
42526
+ pageFlags = apOld[0]->aData[0];
4246942527
for(i=0; i<k; i++){
4247042528
MemPage *pNew;
4247142529
if( i<nOld ){
4247242530
pNew = apNew[i] = apOld[i];
42473
- pgnoNew[i] = pgnoOld[i];
4247442531
apOld[i] = 0;
4247542532
rc = sqlite3PagerWrite(pNew->pDbPage);
4247642533
nNew++;
4247742534
if( rc ) goto balance_cleanup;
4247842535
}else{
4247942536
assert( i>0 );
42480
- rc = allocateBtreePage(pBt, &pNew, &pgnoNew[i], pgnoNew[i-1], 0);
42537
+ rc = allocateBtreePage(pBt, &pNew, &pgno, pgno, 0);
4248142538
if( rc ) goto balance_cleanup;
4248242539
apNew[i] = pNew;
4248342540
nNew++;
42541
+
42542
+ /* Set the pointer-map entry for the new sibling page. */
42543
+ if( ISAUTOVACUUM ){
42544
+ rc = ptrmapPut(pBt, pNew->pgno, PTRMAP_BTREE, pParent->pgno);
42545
+ if( rc!=SQLITE_OK ){
42546
+ goto balance_cleanup;
42547
+ }
42548
+ }
4248442549
}
4248542550
}
4248642551
4248742552
/* Free any old pages that were not reused as new pages.
4248842553
*/
@@ -42507,38 +42572,36 @@
4250742572
**
4250842573
** When NB==3, this one optimization makes the database
4250942574
** about 25% faster for large insertions and deletions.
4251042575
*/
4251142576
for(i=0; i<k-1; i++){
42512
- int minV = pgnoNew[i];
42577
+ int minV = apNew[i]->pgno;
4251342578
int minI = i;
4251442579
for(j=i+1; j<k; j++){
42515
- if( pgnoNew[j]<(unsigned)minV ){
42580
+ if( apNew[j]->pgno<(unsigned)minV ){
4251642581
minI = j;
42517
- minV = pgnoNew[j];
42582
+ minV = apNew[j]->pgno;
4251842583
}
4251942584
}
4252042585
if( minI>i ){
4252142586
int t;
4252242587
MemPage *pT;
42523
- t = pgnoNew[i];
42588
+ t = apNew[i]->pgno;
4252442589
pT = apNew[i];
42525
- pgnoNew[i] = pgnoNew[minI];
4252642590
apNew[i] = apNew[minI];
42527
- pgnoNew[minI] = t;
4252842591
apNew[minI] = pT;
4252942592
}
4253042593
}
42531
- TRACE(("BALANCE: old: %d %d %d new: %d(%d) %d(%d) %d(%d) %d(%d) %d(%d)\n",
42532
- pgnoOld[0],
42533
- nOld>=2 ? pgnoOld[1] : 0,
42534
- nOld>=3 ? pgnoOld[2] : 0,
42535
- pgnoNew[0], szNew[0],
42536
- nNew>=2 ? pgnoNew[1] : 0, nNew>=2 ? szNew[1] : 0,
42537
- nNew>=3 ? pgnoNew[2] : 0, nNew>=3 ? szNew[2] : 0,
42538
- nNew>=4 ? pgnoNew[3] : 0, nNew>=4 ? szNew[3] : 0,
42539
- nNew>=5 ? pgnoNew[4] : 0, nNew>=5 ? szNew[4] : 0));
42594
+ TRACE(("new: %d(%d) %d(%d) %d(%d) %d(%d) %d(%d)\n",
42595
+ apNew[0]->pgno, szNew[0],
42596
+ nNew>=2 ? apNew[1]->pgno : 0, nNew>=2 ? szNew[1] : 0,
42597
+ nNew>=3 ? apNew[2]->pgno : 0, nNew>=3 ? szNew[2] : 0,
42598
+ nNew>=4 ? apNew[3]->pgno : 0, nNew>=4 ? szNew[3] : 0,
42599
+ nNew>=5 ? apNew[4]->pgno : 0, nNew>=5 ? szNew[4] : 0));
42600
+
42601
+ assert( sqlite3PagerIswriteable(pParent->pDbPage) );
42602
+ put4byte(pRight, apNew[nNew-1]->pgno);
4254042603
4254142604
/*
4254242605
** Evenly distribute the data in apCell[] across the new pages.
4254342606
** Insert divider cells into pParent as necessary.
4254442607
*/
@@ -42545,36 +42608,15 @@
4254542608
j = 0;
4254642609
for(i=0; i<nNew; i++){
4254742610
/* Assemble the new sibling page. */
4254842611
MemPage *pNew = apNew[i];
4254942612
assert( j<nMaxCells );
42550
- assert( pNew->pgno==pgnoNew[i] );
4255142613
zeroPage(pNew, pageFlags);
4255242614
assemblePage(pNew, cntNew[i]-j, &apCell[j], &szCell[j]);
4255342615
assert( pNew->nCell>0 || (nNew==1 && cntNew[0]==0) );
4255442616
assert( pNew->nOverflow==0 );
4255542617
42556
- /* If this is an auto-vacuum database, update the pointer map entries
42557
- ** that point to the siblings that were rearranged. These can be: left
42558
- ** children of cells, the right-child of the page, or overflow pages
42559
- ** pointed to by cells.
42560
- */
42561
- if( ISAUTOVACUUM ){
42562
- for(k=j; k<cntNew[i]; k++){
42563
- assert( k<nMaxCells );
42564
- if( aFrom[k]==0xFF || apCopy[aFrom[k]]->pgno!=pNew->pgno ){
42565
- rc = ptrmapPutOvfl(pNew, k-j);
42566
- if( rc==SQLITE_OK && leafCorrection==0 ){
42567
- rc = ptrmapPut(pBt, get4byte(apCell[k]), PTRMAP_BTREE, pNew->pgno);
42568
- }
42569
- if( rc!=SQLITE_OK ){
42570
- goto balance_cleanup;
42571
- }
42572
- }
42573
- }
42574
- }
42575
-
4257642618
j = cntNew[i];
4257742619
4257842620
/* If the sibling page assembled above was not the right-most sibling,
4257942621
** insert a divider cell into the parent page.
4258042622
*/
@@ -42584,21 +42626,13 @@
4258442626
int sz;
4258542627
4258642628
assert( j<nMaxCells );
4258742629
pCell = apCell[j];
4258842630
sz = szCell[j] + leafCorrection;
42589
- pTemp = &aSpace2[iSpace2];
42631
+ pTemp = &aOvflSpace[iOvflSpace];
4259042632
if( !pNew->leaf ){
4259142633
memcpy(&pNew->aData[8], pCell, 4);
42592
- if( ISAUTOVACUUM
42593
- && (aFrom[j]==0xFF || apCopy[aFrom[j]]->pgno!=pNew->pgno)
42594
- ){
42595
- rc = ptrmapPut(pBt, get4byte(pCell), PTRMAP_BTREE, pNew->pgno);
42596
- if( rc!=SQLITE_OK ){
42597
- goto balance_cleanup;
42598
- }
42599
- }
4260042634
}else if( leafData ){
4260142635
/* If the tree is a leaf-data tree, and the siblings are leaves,
4260242636
** then there is no divider cell in apCell[]. Instead, the divider
4260342637
** cell consists of the integer key for the right-most cell of
4260442638
** the sibling-page assembled above only.
@@ -42605,14 +42639,11 @@
4260542639
*/
4260642640
CellInfo info;
4260742641
j--;
4260842642
sqlite3BtreeParseCellPtr(pNew, apCell[j], &info);
4260942643
pCell = pTemp;
42610
- rc = fillInCell(pParent, pCell, 0, info.nKey, 0, 0, 0, &sz);
42611
- if( rc!=SQLITE_OK ){
42612
- goto balance_cleanup;
42613
- }
42644
+ sz = 4 + putVarint(&pCell[4], info.nKey);
4261442645
pTemp = 0;
4261542646
}else{
4261642647
pCell -= 4;
4261742648
/* Obscure case for non-leaf-data trees: If the cell at pCell was
4261842649
** previously stored on a leaf node, and its reported size was 4
@@ -42628,300 +42659,457 @@
4262842659
if( szCell[j]==4 ){
4262942660
assert(leafCorrection==4);
4263042661
sz = cellSizePtr(pParent, pCell);
4263142662
}
4263242663
}
42633
- iSpace2 += sz;
42664
+ iOvflSpace += sz;
4263442665
assert( sz<=pBt->pageSize/4 );
42635
- assert( iSpace2<=pBt->pageSize );
42636
- rc = insertCell(pParent, nxDiv, pCell, sz, pTemp, 4);
42666
+ assert( iOvflSpace<=pBt->pageSize );
42667
+ rc = insertCell(pParent, nxDiv, pCell, sz, pTemp, pNew->pgno);
4263742668
if( rc!=SQLITE_OK ) goto balance_cleanup;
4263842669
assert( sqlite3PagerIswriteable(pParent->pDbPage) );
42639
- put4byte(findOverflowCell(pParent,nxDiv), pNew->pgno);
42640
-
42641
- /* If this is an auto-vacuum database, and not a leaf-data tree,
42642
- ** then update the pointer map with an entry for the overflow page
42643
- ** that the cell just inserted points to (if any).
42644
- */
42645
- if( ISAUTOVACUUM && !leafData ){
42646
- rc = ptrmapPutOvfl(pParent, nxDiv);
42647
- if( rc!=SQLITE_OK ){
42648
- goto balance_cleanup;
42649
- }
42650
- }
42670
+
4265142671
j++;
4265242672
nxDiv++;
4265342673
}
42654
-
42655
- /* Set the pointer-map entry for the new sibling page. */
42656
- if( ISAUTOVACUUM ){
42657
- rc = ptrmapPut(pBt, pNew->pgno, PTRMAP_BTREE, pParent->pgno);
42658
- if( rc!=SQLITE_OK ){
42659
- goto balance_cleanup;
42660
- }
42661
- }
4266242674
}
4266342675
assert( j==nCell );
4266442676
assert( nOld>0 );
4266542677
assert( nNew>0 );
4266642678
if( (pageFlags & PTF_LEAF)==0 ){
4266742679
u8 *zChild = &apCopy[nOld-1]->aData[8];
4266842680
memcpy(&apNew[nNew-1]->aData[8], zChild, 4);
42669
- if( ISAUTOVACUUM ){
42670
- rc = ptrmapPut(pBt, get4byte(zChild), PTRMAP_BTREE, apNew[nNew-1]->pgno);
42671
- if( rc!=SQLITE_OK ){
42672
- goto balance_cleanup;
42673
- }
42674
- }
42675
- }
42676
- assert( sqlite3PagerIswriteable(pParent->pDbPage) );
42677
- if( nxDiv==pParent->nCell+pParent->nOverflow ){
42678
- /* Right-most sibling is the right-most child of pParent */
42679
- put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew[nNew-1]);
42680
- }else{
42681
- /* Right-most sibling is the left child of the first entry in pParent
42682
- ** past the right-most divider entry */
42683
- put4byte(findOverflowCell(pParent, nxDiv), pgnoNew[nNew-1]);
42684
- }
42685
-
42686
- /*
42687
- ** Balance the parent page. Note that the current page (pPage) might
42688
- ** have been added to the freelist so it might no longer be initialized.
42689
- ** But the parent page will always be initialized.
42690
- */
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);
42720
+ j = 0; /* Current 'old' sibling page */
42721
+ k = 0; /* Current 'new' sibling page */
42722
+ for(i=0; i<nCell && rc==SQLITE_OK; i++){
42723
+ int isDivider = 0;
42724
+ while( i==iNextOld ){
42725
+ /* Cell i is the cell immediately following the last cell on old
42726
+ ** sibling page j. If the siblings are not leaf pages of an
42727
+ ** intkey b-tree, then cell i was a divider cell. */
42728
+ pOld = apCopy[++j];
42729
+ iNextOld = i + !leafData + pOld->nCell + pOld->nOverflow;
42730
+ if( pOld->nOverflow ){
42731
+ nOverflow = pOld->nOverflow;
42732
+ iOverflow = i + !leafData + pOld->aOvfl[0].idx;
42733
+ }
42734
+ isDivider = !leafData;
42735
+ }
42736
+
42737
+ assert(nOverflow>0 || iOverflow<i );
42738
+ assert(nOverflow<2 || pOld->aOvfl[0].idx==pOld->aOvfl[1].idx-1);
42739
+ assert(nOverflow<3 || pOld->aOvfl[1].idx==pOld->aOvfl[2].idx-1);
42740
+ if( i==iOverflow ){
42741
+ isDivider = 1;
42742
+ if( (--nOverflow)>0 ){
42743
+ iOverflow++;
42744
+ }
42745
+ }
42746
+
42747
+ if( i==cntNew[k] ){
42748
+ /* Cell i is the cell immediately following the last cell on new
42749
+ ** sibling page k. If the siblings are not leaf pages of an
42750
+ ** intkey b-tree, then cell i is a divider cell. */
42751
+ pNew = apNew[++k];
42752
+ if( !leafData ) continue;
42753
+ }
42754
+ assert( rc==SQLITE_OK );
42755
+ assert( j<nOld );
42756
+ assert( k<nNew );
42757
+
42758
+ /* If the cell was originally divider cell (and is not now) or
42759
+ ** an overflow cell, or if the cell was located on a different sibling
42760
+ ** page before the balancing, then the pointer map entries associated
42761
+ ** with any child or overflow pages need to be updated. */
42762
+ if( isDivider || pOld->pgno!=pNew->pgno ){
42763
+ if( !leafCorrection ){
42764
+ rc = ptrmapPut(pBt, get4byte(apCell[i]), PTRMAP_BTREE, pNew->pgno);
42765
+ }
42766
+ if( szCell[i]>pNew->minLocal && rc==SQLITE_OK ){
42767
+ rc = ptrmapPutOvflPtr(pNew, apCell[i]);
42768
+ }
42769
+ }
42770
+ }
42771
+
42772
+ if( !leafCorrection ){
42773
+ for(i=0; rc==SQLITE_OK && i<nNew; i++){
42774
+ rc = ptrmapPut(
42775
+ pBt, get4byte(&apNew[i]->aData[8]), PTRMAP_BTREE, apNew[i]->pgno);
42776
+ }
42777
+ }
42778
+
42779
+#if 0
42780
+ /* The ptrmapCheckPages() contains assert() statements that verify that
42781
+ ** all pointer map pages are set correctly. This is helpful while
42782
+ ** debugging. This is usually disabled because a corrupt database may
42783
+ ** cause an assert() statement to fail. */
42784
+ ptrmapCheckPages(apNew, nNew);
42785
+ ptrmapCheckPages(&pParent, 1);
42786
+#endif
42787
+ }
42788
+
4269142789
assert( pParent->isInit );
42692
- sqlite3ScratchFree(apCell);
42693
- apCell = 0;
42694
- TRACE(("BALANCE: finished with %d: old=%d new=%d cells=%d\n",
42695
- pPage->pgno, nOld, nNew, nCell));
42696
- pPage->nOverflow = 0;
42697
- releasePage(pPage);
42698
- pCur->iPage--;
42699
- rc = balance(pCur, 0);
42700
-
42790
+ TRACE(("BALANCE: finished: old=%d new=%d cells=%d\n",
42791
+ nOld, nNew, nCell));
42792
+
4270142793
/*
4270242794
** Cleanup before returning.
4270342795
*/
4270442796
balance_cleanup:
42705
- sqlite3PageFree(aSpace2);
4270642797
sqlite3ScratchFree(apCell);
4270742798
for(i=0; i<nOld; i++){
4270842799
releasePage(apOld[i]);
4270942800
}
4271042801
for(i=0; i<nNew; i++){
4271142802
releasePage(apNew[i]);
4271242803
}
42713
- pCur->apPage[pCur->iPage]->nOverflow = 0;
4271442804
4271542805
return rc;
4271642806
}
4271742807
4271842808
/*
42719
-** This routine is called for the root page of a btree when the root
42720
-** page contains no cells. This is an opportunity to make the tree
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
4272142861
** shallower by one level.
4272242862
*/
42723
-static int balance_shallower(BtCursor *pCur){
42724
- MemPage *pPage; /* Root page of B-Tree */
42725
- MemPage *pChild; /* The only child page of pPage */
42726
- Pgno pgnoChild; /* Page number for pChild */
42727
- int rc = SQLITE_OK; /* Return code from subprocedures */
42728
- BtShared *pBt; /* The main BTree structure */
42729
- int mxCellPerPage; /* Maximum number of cells per page */
42730
- u8 **apCell; /* All cells from pages being balanced */
42731
- u16 *szCell; /* Local size of all cells */
42732
-
42733
- assert( pCur->iPage==0 );
42734
- pPage = pCur->apPage[0];
42735
-
42736
- assert( pPage->nCell==0 );
42737
- assert( sqlite3_mutex_held(pPage->pBt->mutex) );
42738
- pBt = pPage->pBt;
42739
- mxCellPerPage = MX_CELL(pBt);
42740
- apCell = sqlite3Malloc( mxCellPerPage*(sizeof(u8*)+sizeof(u16)) );
42741
- if( apCell==0 ) return SQLITE_NOMEM;
42742
- szCell = (u16*)&apCell[mxCellPerPage];
42743
- if( pPage->leaf ){
42744
- /* The table is completely empty */
42745
- TRACE(("BALANCE: empty table %d\n", pPage->pgno));
42746
- }else{
42747
- /* The root page is empty but has one child. Transfer the
42748
- ** information from that one child into the root page if it
42749
- ** will fit. This reduces the depth of the tree by one.
42750
- **
42751
- ** If the root page is page 1, it has less space available than
42752
- ** its child (due to the 100 byte header that occurs at the beginning
42753
- ** of the database fle), so it might not be able to hold all of the
42754
- ** information currently contained in the child. If this is the
42755
- ** case, then do not do the transfer. Leave page 1 empty except
42756
- ** for the right-pointer to the child page. The child page becomes
42757
- ** the virtual root of the tree.
42758
- */
42759
- VVA_ONLY( pCur->pagesShuffled = 1 );
42760
- pgnoChild = get4byte(&pPage->aData[pPage->hdrOffset+8]);
42761
- assert( pgnoChild>0 );
42762
- assert( pgnoChild<=pagerPagecount(pPage->pBt) );
42763
- rc = sqlite3BtreeGetPage(pPage->pBt, pgnoChild, &pChild, 0);
42764
- if( rc ) goto end_shallow_balance;
42765
- if( pPage->pgno==1 ){
42766
- rc = sqlite3BtreeInitPage(pChild);
42767
- if( rc ) goto end_shallow_balance;
42768
- assert( pChild->nOverflow==0 );
42769
- if( pChild->nFree>=100 ){
42770
- /* The child information will fit on the root page, so do the
42771
- ** copy */
42772
- int i;
42773
- zeroPage(pPage, pChild->aData[0]);
42774
- for(i=0; i<pChild->nCell; i++){
42775
- apCell[i] = findCell(pChild,i);
42776
- szCell[i] = cellSizePtr(pChild, apCell[i]);
42777
- }
42778
- assemblePage(pPage, pChild->nCell, apCell, szCell);
42779
- /* Copy the right-pointer of the child to the parent. */
42780
- assert( sqlite3PagerIswriteable(pPage->pDbPage) );
42781
- put4byte(&pPage->aData[pPage->hdrOffset+8],
42782
- get4byte(&pChild->aData[pChild->hdrOffset+8]));
42783
- rc = freePage(pChild);
42784
- TRACE(("BALANCE: child %d transfer to page 1\n", pChild->pgno));
42785
- }else{
42786
- /* The child has more information that will fit on the root.
42787
- ** The tree is already balanced. Do nothing. */
42788
- TRACE(("BALANCE: child %d will not fit on page 1\n", pChild->pgno));
42789
- }
42790
- }else{
42791
- memcpy(pPage->aData, pChild->aData, pPage->pBt->usableSize);
42792
- pPage->isInit = 0;
42793
- rc = sqlite3BtreeInitPage(pPage);
42794
- assert( rc==SQLITE_OK );
42795
- freePage(pChild);
42796
- TRACE(("BALANCE: transfer child %d into root %d\n",
42797
- pChild->pgno, pPage->pgno));
42798
- }
42799
- assert( pPage->nOverflow==0 );
42800
-#ifndef SQLITE_OMIT_AUTOVACUUM
42801
- if( ISAUTOVACUUM && rc==SQLITE_OK ){
42802
- rc = setChildPtrmaps(pPage);
42803
- }
42804
-#endif
42805
- releasePage(pChild);
42806
- }
42807
-end_shallow_balance:
42808
- sqlite3_free(apCell);
42809
- return rc;
42810
-}
42811
-
42812
-
42813
-/*
42814
-** The root page is overfull
42815
-**
42816
-** When this happens, Create a new child page and copy the
42817
-** contents of the root into the child. Then make the root
42818
-** page an empty page with rightChild pointing to the new
42819
-** child. Finally, call balance_internal() on the new child
42820
-** to cause it to split.
42821
-*/
42822
-static int balance_deeper(BtCursor *pCur){
42823
- int rc; /* Return value from subprocedures */
42824
- MemPage *pPage; /* Pointer to the root page */
42825
- MemPage *pChild; /* Pointer to a new child page */
42826
- Pgno pgnoChild; /* Page number of the new child page */
42827
- BtShared *pBt; /* The BTree */
42828
- int usableSize; /* Total usable size of a page */
42829
- u8 *data; /* Content of the parent page */
42830
- u8 *cdata; /* Content of the child page */
42831
- int hdr; /* Offset to page header in parent */
42832
- int cbrk; /* Offset to content of first cell in parent */
42833
-
42834
- assert( pCur->iPage==0 );
42835
- assert( pCur->apPage[0]->nOverflow>0 );
42836
-
42837
- VVA_ONLY( pCur->pagesShuffled = 1 );
42838
- pPage = pCur->apPage[0];
42839
- pBt = pPage->pBt;
42840
- assert( sqlite3_mutex_held(pBt->mutex) );
42841
- assert( sqlite3PagerIswriteable(pPage->pDbPage) );
42842
- rc = allocateBtreePage(pBt, &pChild, &pgnoChild, pPage->pgno, 0);
42843
- if( rc ) return rc;
42844
- assert( sqlite3PagerIswriteable(pChild->pDbPage) );
42845
- usableSize = pBt->usableSize;
42846
- data = pPage->aData;
42847
- hdr = pPage->hdrOffset;
42848
- cbrk = get2byte(&data[hdr+5]);
42849
- cdata = pChild->aData;
42850
- memcpy(cdata, &data[hdr], pPage->cellOffset+2*pPage->nCell-hdr);
42851
- memcpy(&cdata[cbrk], &data[cbrk], usableSize-cbrk);
42852
-
42853
- assert( pChild->isInit==0 );
42854
- rc = sqlite3BtreeInitPage(pChild);
42855
- if( rc==SQLITE_OK ){
42856
- int nCopy = pPage->nOverflow*sizeof(pPage->aOvfl[0]);
42857
- memcpy(pChild->aOvfl, pPage->aOvfl, nCopy);
42858
- pChild->nOverflow = pPage->nOverflow;
42859
- if( pChild->nOverflow ){
42860
- pChild->nFree = 0;
42861
- }
42862
- assert( pChild->nCell==pPage->nCell );
42863
- assert( sqlite3PagerIswriteable(pPage->pDbPage) );
42864
- zeroPage(pPage, pChild->aData[0] & ~PTF_LEAF);
42865
- put4byte(&pPage->aData[pPage->hdrOffset+8], pgnoChild);
42866
- TRACE(("BALANCE: copy root %d into %d\n", pPage->pgno, pChild->pgno));
42867
- if( ISAUTOVACUUM ){
42868
- rc = ptrmapPut(pBt, pChild->pgno, PTRMAP_BTREE, pPage->pgno);
42869
-#ifndef SQLITE_OMIT_AUTOVACUUM
42870
- if( rc==SQLITE_OK ){
42871
- rc = setChildPtrmaps(pChild);
42872
- }
42873
- if( rc ){
42874
- pChild->nOverflow = 0;
42875
- }
42876
-#endif
42877
- }
42878
- }
42879
-
42880
- if( rc==SQLITE_OK ){
42881
- pCur->iPage++;
42882
- pCur->apPage[1] = pChild;
42883
- pCur->aiIdx[0] = 0;
42884
- rc = balance_nonroot(pCur);
42885
- }else{
42886
- releasePage(pChild);
42887
- }
42888
-
42889
- return rc;
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
+/*
42913
+** This function is called when the root page of a b-tree structure is
42914
+** overfull (has one or more overflow pages).
42915
+**
42916
+** A new child page is allocated and the contents of the current root
42917
+** page, including overflow cells, are copied into the child. The root
42918
+** page is then overwritten to make it an empty page with the right-child
42919
+** pointer pointing to the new page.
42920
+**
42921
+** Before returning, all pointer-map entries corresponding to pages
42922
+** that the new child-page now contains pointers to are updated. The
42923
+** entry corresponding to the new right-child pointer of the root
42924
+** page is also updated.
42925
+**
42926
+** If successful, *ppChild is set to contain a reference to the child
42927
+** page and SQLITE_OK is returned. In this case the caller is required
42928
+** to call releasePage() on *ppChild exactly once. If an error occurs,
42929
+** an error code is returned and *ppChild is set to 0.
42930
+*/
42931
+static int balance_deeper(MemPage *pRoot, MemPage **ppChild){
42932
+ int rc; /* Return value from subprocedures */
42933
+ MemPage *pChild = 0; /* Pointer to a new child page */
42934
+ Pgno pgnoChild; /* Page number of the new child page */
42935
+ BtShared *pBt = pRoot->pBt; /* The BTree */
42936
+
42937
+ assert( pRoot->nOverflow>0 );
42938
+ assert( sqlite3_mutex_held(pBt->mutex) );
42939
+
42940
+ /* Make pRoot, the root page of the b-tree, writable. Allocate a new
42941
+ ** page that will become the new right-child of pPage. Copy the contents
42942
+ ** of the node stored on pRoot into the new child page.
42943
+ */
42944
+ if( SQLITE_OK!=(rc = sqlite3PagerWrite(pRoot->pDbPage))
42945
+ || SQLITE_OK!=(rc = allocateBtreePage(pBt,&pChild,&pgnoChild,pRoot->pgno,0))
42946
+ || SQLITE_OK!=(rc = copyNodeContent(pRoot, pChild))
42947
+ || (ISAUTOVACUUM &&
42948
+ SQLITE_OK!=(rc = ptrmapPut(pBt, pgnoChild, PTRMAP_BTREE, pRoot->pgno)))
42949
+ ){
42950
+ *ppChild = 0;
42951
+ releasePage(pChild);
42952
+ return rc;
42953
+ }
42954
+ assert( sqlite3PagerIswriteable(pChild->pDbPage) );
42955
+ assert( sqlite3PagerIswriteable(pRoot->pDbPage) );
42956
+ assert( pChild->nCell==pRoot->nCell );
42957
+
42958
+ TRACE(("BALANCE: copy root %d into %d\n", pRoot->pgno, pChild->pgno));
42959
+
42960
+ /* Copy the overflow cells from pRoot to pChild */
42961
+ memcpy(pChild->aOvfl, pRoot->aOvfl, pRoot->nOverflow*sizeof(pRoot->aOvfl[0]));
42962
+ pChild->nOverflow = pRoot->nOverflow;
42963
+
42964
+ /* Zero the contents of pRoot. Then install pChild as the right-child. */
42965
+ zeroPage(pRoot, pChild->aData[0] & ~PTF_LEAF);
42966
+ put4byte(&pRoot->aData[pRoot->hdrOffset+8], pgnoChild);
42967
+
42968
+ *ppChild = pChild;
42969
+ return SQLITE_OK;
4289042970
}
4289142971
4289242972
/*
4289342973
** The page that pCur currently points to has just been modified in
4289442974
** some way. This function figures out if this modification means the
4289542975
** tree needs to be balanced, and if so calls the appropriate balancing
42896
-** routine.
42897
-**
42898
-** Parameter isInsert is true if a new cell was just inserted into the
42899
-** page, or false otherwise.
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.
4290042987
*/
42901
-static int balance(BtCursor *pCur, int isInsert){
42988
+static int balance(BtCursor *pCur){
4290242989
int rc = SQLITE_OK;
42903
- MemPage *pPage = pCur->apPage[pCur->iPage];
42904
-
42905
- assert( sqlite3_mutex_held(pPage->pBt->mutex) );
42906
- if( pCur->iPage==0 ){
42907
- rc = sqlite3PagerWrite(pPage->pDbPage);
42908
- if( rc==SQLITE_OK && pPage->nOverflow>0 ){
42909
- rc = balance_deeper(pCur);
42910
- assert( pCur->apPage[0]==pPage );
42911
- assert( pPage->nOverflow==0 || rc!=SQLITE_OK );
42912
- }
42913
- if( rc==SQLITE_OK && pPage->nCell==0 ){
42914
- rc = balance_shallower(pCur);
42915
- assert( pCur->apPage[0]==pPage );
42916
- assert( pPage->nOverflow==0 || rc!=SQLITE_OK );
42917
- }
42918
- }else{
42919
- if( pPage->nOverflow>0 ||
42920
- (!isInsert && pPage->nFree>pPage->pBt->usableSize*2/3) ){
42921
- rc = balance_nonroot(pCur);
42922
- }
42990
+ const int nMin = pCur->pBt->usableSize * 2 / 3;
42991
+ u8 aBalanceQuickSpace[13];
42992
+ u8 *pFree = 0;
42993
+
42994
+ TESTONLY( int balance_quick_called = 0 );
42995
+ TESTONLY( int balance_deeper_called = 0 );
42996
+
42997
+ do {
42998
+ int iPage = pCur->iPage;
42999
+ MemPage *pPage = pCur->apPage[iPage];
43000
+
43001
+ if( iPage==0 ){
43002
+ if( pPage->nOverflow ){
43003
+ /* The root page of the b-tree is overfull. In this case call the
43004
+ ** balance_deeper() function to create a new child for the root-page
43005
+ ** and copy the current contents of the root-page to it. The
43006
+ ** next iteration of the do-loop will balance the child page.
43007
+ */
43008
+ assert( (balance_deeper_called++)==0 );
43009
+ rc = balance_deeper(pPage, &pCur->apPage[1]);
43010
+ if( rc==SQLITE_OK ){
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{
43036
+ MemPage * const pParent = pCur->apPage[iPage-1];
43037
+ int const iIdx = pCur->aiIdx[iPage-1];
43038
+
43039
+ rc = sqlite3PagerWrite(pParent->pDbPage);
43040
+ if( rc==SQLITE_OK ){
43041
+#ifndef SQLITE_OMIT_QUICKBALANCE
43042
+ if( pPage->hasData
43043
+ && pPage->nOverflow==1
43044
+ && pPage->aOvfl[0].idx==pPage->nCell
43045
+ && pParent->pgno!=1
43046
+ && pParent->nCell==iIdx
43047
+ ){
43048
+ /* Call balance_quick() to create a new sibling of pPage on which
43049
+ ** to store the overflow cell. balance_quick() inserts a new cell
43050
+ ** into pParent, which may cause pParent overflow. If this
43051
+ ** happens, the next interation of the do-loop will balance pParent
43052
+ ** use either balance_nonroot() or balance_deeper(). Until this
43053
+ ** happens, the overflow cell is stored in the aBalanceQuickSpace[]
43054
+ ** buffer.
43055
+ **
43056
+ ** The purpose of the following assert() is to check that only a
43057
+ ** single call to balance_quick() is made for each call to this
43058
+ ** function. If this were not verified, a subtle bug involving reuse
43059
+ ** of the aBalanceQuickSpace[] might sneak in.
43060
+ */
43061
+ assert( (balance_quick_called++)==0 );
43062
+ rc = balance_quick(pParent, pPage, aBalanceQuickSpace);
43063
+ }else
43064
+#endif
43065
+ {
43066
+ /* In this case, call balance_nonroot() to redistribute cells
43067
+ ** between pPage and up to 2 of its sibling pages. This involves
43068
+ ** modifying the contents of pParent, which may cause pParent to
43069
+ ** become overfull or underfull. The next iteration of the do-loop
43070
+ ** will balance the parent page to correct this.
43071
+ **
43072
+ ** If the parent page becomes overfull, the overflow cell or cells
43073
+ ** are stored in the pSpace buffer allocated immediately below.
43074
+ ** A subsequent iteration of the do-loop will deal with this by
43075
+ ** calling balance_nonroot() (balance_deeper() may be called first,
43076
+ ** but it doesn't deal with overflow cells - just moves them to a
43077
+ ** different page). Once this subsequent call to balance_nonroot()
43078
+ ** has completed, it is safe to release the pSpace buffer used by
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. */
43090
+ sqlite3PageFree(pFree);
43091
+ }
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
+
43103
+ /* The next iteration of the do-loop balances the parent page. */
43104
+ releasePage(pPage);
43105
+ pCur->iPage--;
43106
+ }
43107
+ }while( rc==SQLITE_OK );
43108
+
43109
+ if( pFree ){
43110
+ sqlite3PageFree(pFree);
4292343111
}
4292443112
return rc;
4292543113
}
4292643114
4292743115
/*
@@ -43061,10 +43249,11 @@
4306143249
SQLITE_OK!=(rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur)) || (!loc &&
4306243250
SQLITE_OK!=(rc = sqlite3BtreeMoveto(pCur, pKey, nKey, appendBias, &loc))
4306343251
)){
4306443252
return rc;
4306543253
}
43254
+ assert( pCur->eState==CURSOR_VALID || (pCur->eState==CURSOR_INVALID && loc) );
4306643255
4306743256
pPage = pCur->apPage[pCur->iPage];
4306843257
assert( pPage->intKey || nKey>=0 );
4306943258
assert( pPage->leaf || !pPage->intKey );
4307043259
TRACE(("INSERT: table=%d nkey=%lld ndata=%d page=%d %s\n",
@@ -43077,11 +43266,11 @@
4307743266
rc = fillInCell(pPage, newCell, pKey, nKey, pData, nData, nZero, &szNew);
4307843267
if( rc ) goto end_insert;
4307943268
assert( szNew==cellSizePtr(pPage, newCell) );
4308043269
assert( szNew<=MX_CELL_SIZE(pBt) );
4308143270
idx = pCur->aiIdx[pCur->iPage];
43082
- if( loc==0 && CURSOR_VALID==pCur->eState ){
43271
+ if( loc==0 ){
4308343272
u16 szOld;
4308443273
assert( idx<pPage->nCell );
4308543274
rc = sqlite3PagerWrite(pPage->pDbPage);
4308643275
if( rc ){
4308743276
goto end_insert;
@@ -43098,51 +43287,49 @@
4309843287
goto end_insert;
4309943288
}
4310043289
}else if( loc<0 && pPage->nCell>0 ){
4310143290
assert( pPage->leaf );
4310243291
idx = ++pCur->aiIdx[pCur->iPage];
43103
- pCur->info.nSize = 0;
43104
- pCur->validNKey = 0;
4310543292
}else{
4310643293
assert( pPage->leaf );
4310743294
}
4310843295
rc = insertCell(pPage, idx, newCell, szNew, 0, 0);
4310943296
assert( rc!=SQLITE_OK || pPage->nCell>0 || pPage->nOverflow>0 );
4311043297
43111
- /* If no error has occured, call balance() to deal with any overflow and
43112
- ** move the cursor to point at the root of the table (since balance may
43113
- ** have rearranged the table in such a way as to invalidate BtCursor.apPage[]
43114
- ** or BtCursor.aiIdx[]).
43115
- **
43116
- ** Except, if all of the following are true, do nothing:
43117
- **
43118
- ** * Inserting the new cell did not cause overflow,
43119
- **
43120
- ** * Before inserting the new cell the cursor was pointing at the
43121
- ** largest key in an intkey B-Tree, and
43122
- **
43123
- ** * The key value associated with the new cell is now the largest
43124
- ** in the B-Tree.
43125
- **
43126
- ** In this case the cursor can be safely left pointing at the (new)
43127
- ** largest key value in the B-Tree. Doing so speeds up inserting a set
43128
- ** of entries with increasing integer key values via a single cursor
43129
- ** (comes up with "INSERT INTO ... SELECT ..." statements), as
43130
- ** the next insert operation is not required to seek the cursor.
43131
- */
43132
- if( rc==SQLITE_OK
43133
- && (pPage->nOverflow || !pCur->atLast || loc>=0 || !pCur->apPage[0]->intKey)
43134
- ){
43135
- rc = balance(pCur, 1);
43136
- if( rc==SQLITE_OK ){
43137
- moveToRoot(pCur);
43138
- }
43139
- }
43140
-
43141
- /* Must make sure nOverflow is reset to zero even if the balance()
43142
- ** fails. Internal data structure corruption will result otherwise. */
43143
- pCur->apPage[pCur->iPage]->nOverflow = 0;
43298
+ /* If no error has occured and pPage has an overflow cell, call balance()
43299
+ ** to redistribute the cells within the tree. Since balance() may move
43300
+ ** the cursor, zero the BtCursor.info.nSize and BtCursor.validNKey
43301
+ ** variables.
43302
+ **
43303
+ ** Previous versions of SQLite called moveToRoot() to move the cursor
43304
+ ** back to the root page as balance() used to invalidate the contents
43305
+ ** of BtCursor.apPage[] and BtCursor.aiIdx[]. Instead of doing that,
43306
+ ** set the cursor state to "invalid". This makes common insert operations
43307
+ ** slightly faster.
43308
+ **
43309
+ ** There is a subtle but important optimization here too. When inserting
43310
+ ** multiple records into an intkey b-tree using a single cursor (as can
43311
+ ** happen while processing an "INSERT INTO ... SELECT" statement), it
43312
+ ** is advantageous to leave the cursor pointing to the last entry in
43313
+ ** the b-tree if possible. If the cursor is left pointing to the last
43314
+ ** entry in the table, and the next row inserted has an integer key
43315
+ ** larger than the largest existing key, it is possible to insert the
43316
+ ** row without seeking the cursor. This can be a big performance boost.
43317
+ */
43318
+ pCur->info.nSize = 0;
43319
+ pCur->validNKey = 0;
43320
+ if( rc==SQLITE_OK && pPage->nOverflow ){
43321
+ rc = balance(pCur);
43322
+
43323
+ /* Must make sure nOverflow is reset to zero even if the balance()
43324
+ ** fails. Internal data structure corruption will result otherwise.
43325
+ ** Also, set the cursor state to invalid. This stops saveCursorPosition()
43326
+ ** from trying to save the current position of the cursor. */
43327
+ pCur->apPage[pCur->iPage]->nOverflow = 0;
43328
+ pCur->eState = CURSOR_INVALID;
43329
+ }
43330
+ assert( pCur->apPage[pCur->iPage]->nOverflow==0 );
4314443331
4314543332
end_insert:
4314643333
return rc;
4314743334
}
4314843335
@@ -43149,202 +43336,114 @@
4314943336
/*
4315043337
** Delete the entry that the cursor is pointing to. The cursor
4315143338
** is left pointing at a arbitrary location.
4315243339
*/
4315343340
SQLITE_PRIVATE int sqlite3BtreeDelete(BtCursor *pCur){
43154
- MemPage *pPage = pCur->apPage[pCur->iPage];
43155
- int idx;
43156
- unsigned char *pCell;
43157
- int rc;
43158
- Pgno pgnoChild = 0;
4315943341
Btree *p = pCur->pBtree;
43160
- BtShared *pBt = p->pBt;
43342
+ BtShared *pBt = p->pBt;
43343
+ int rc; /* Return code */
43344
+ MemPage *pPage; /* Page to delete cell from */
43345
+ unsigned char *pCell; /* Pointer to cell to delete */
43346
+ int iCellIdx; /* Index of cell to delete */
43347
+ int iCellDepth; /* Depth of node containing pCell */
4316143348
4316243349
assert( cursorHoldsMutex(pCur) );
43163
- assert( pPage->isInit );
4316443350
assert( pBt->inTransaction==TRANS_WRITE );
4316543351
assert( !pBt->readOnly );
43166
- if( pCur->eState==CURSOR_FAULT ){
43167
- return pCur->skip;
43168
- }
43169
- if( NEVER(pCur->aiIdx[pCur->iPage]>=pPage->nCell) ){
43170
- return SQLITE_ERROR; /* The cursor is not pointing to anything */
43171
- }
4317243352
assert( pCur->wrFlag );
43353
+ if( NEVER(pCur->aiIdx[pCur->iPage]>=pCur->apPage[pCur->iPage]->nCell)
43354
+ || NEVER(pCur->eState!=CURSOR_VALID)
43355
+ ){
43356
+ return SQLITE_ERROR; /* Something has gone awry. */
43357
+ }
43358
+
4317343359
rc = checkForReadConflicts(p, pCur->pgnoRoot, pCur, pCur->info.nKey);
4317443360
if( rc!=SQLITE_OK ){
43175
- /* The table pCur points to has a read lock */
43176
- assert( rc==SQLITE_LOCKED_SHAREDCACHE );
43177
- return rc;
43178
- }
43179
-
43180
- /* Restore the current cursor position (a no-op if the cursor is not in
43181
- ** CURSOR_REQUIRESEEK state) and save the positions of any other cursors
43182
- ** open on the same table. Then call sqlite3PagerWrite() on the page
43183
- ** that the entry will be deleted from.
43184
- */
43185
- if(
43186
- (rc = restoreCursorPosition(pCur))!=0 ||
43187
- (rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur))!=0 ||
43188
- (rc = sqlite3PagerWrite(pPage->pDbPage))!=0
43189
- ){
43190
- return rc;
43191
- }
43192
-
43193
- /* Locate the cell within its page and leave pCell pointing to the
43194
- ** data. The clearCell() call frees any overflow pages associated with the
43195
- ** cell. The cell itself is still intact.
43196
- */
43197
- idx = pCur->aiIdx[pCur->iPage];
43198
- pCell = findCell(pPage, idx);
43199
- if( !pPage->leaf ){
43200
- pgnoChild = get4byte(pCell);
43201
- }
43202
- rc = clearCell(pPage, pCell);
43203
- if( rc ){
43204
- return rc;
43205
- }
43206
-
43207
- if( !pPage->leaf ){
43208
- /*
43209
- ** The entry we are about to delete is not a leaf so if we do not
43210
- ** do something we will leave a hole on an internal page.
43211
- ** We have to fill the hole by moving in a cell from a leaf. The
43212
- ** next Cell after the one to be deleted is guaranteed to exist and
43213
- ** to be a leaf so we can use it.
43214
- */
43215
- BtCursor leafCur;
43216
- MemPage *pLeafPage = 0;
43217
-
43218
- unsigned char *pNext;
43219
- int notUsed;
43220
- unsigned char *tempCell = 0;
43221
- assert( !pPage->intKey );
43222
- sqlite3BtreeGetTempCursor(pCur, &leafCur);
43223
- rc = sqlite3BtreeNext(&leafCur, &notUsed);
43224
- if( rc==SQLITE_OK ){
43225
- assert( leafCur.aiIdx[leafCur.iPage]==0 );
43226
- pLeafPage = leafCur.apPage[leafCur.iPage];
43227
- rc = sqlite3PagerWrite(pLeafPage->pDbPage);
43228
- }
43229
- if( rc==SQLITE_OK ){
43230
- int leafCursorInvalid = 0;
43231
- u16 szNext;
43232
- TRACE(("DELETE: table=%d delete internal from %d replace from leaf %d\n",
43233
- pCur->pgnoRoot, pPage->pgno, pLeafPage->pgno));
43234
- dropCell(pPage, idx, cellSizePtr(pPage, pCell));
43235
- pNext = findCell(pLeafPage, 0);
43236
- szNext = cellSizePtr(pLeafPage, pNext);
43237
- assert( MX_CELL_SIZE(pBt)>=szNext+4 );
43238
- allocateTempSpace(pBt);
43239
- tempCell = pBt->pTmpSpace;
43240
- if( tempCell==0 ){
43241
- rc = SQLITE_NOMEM;
43242
- }
43243
- if( rc==SQLITE_OK ){
43244
- rc = insertCell(pPage, idx, pNext-4, szNext+4, tempCell, 0);
43245
- }
43246
-
43247
-
43248
- /* The "if" statement in the next code block is critical. The
43249
- ** slightest error in that statement would allow SQLite to operate
43250
- ** correctly most of the time but produce very rare failures. To
43251
- ** guard against this, the following macros help to verify that
43252
- ** the "if" statement is well tested.
43253
- */
43254
- testcase( pPage->nOverflow==0 && pPage->nFree<pBt->usableSize*2/3
43255
- && pLeafPage->nFree+2+szNext > pBt->usableSize*2/3 );
43256
- testcase( pPage->nOverflow==0 && pPage->nFree==pBt->usableSize*2/3
43257
- && pLeafPage->nFree+2+szNext > pBt->usableSize*2/3 );
43258
- testcase( pPage->nOverflow==0 && pPage->nFree==pBt->usableSize*2/3+1
43259
- && pLeafPage->nFree+2+szNext > pBt->usableSize*2/3 );
43260
- testcase( pPage->nOverflow>0 && pPage->nFree<=pBt->usableSize*2/3
43261
- && pLeafPage->nFree+2+szNext > pBt->usableSize*2/3 );
43262
- testcase( (pPage->nOverflow>0 || (pPage->nFree > pBt->usableSize*2/3))
43263
- && pLeafPage->nFree+2+szNext == pBt->usableSize*2/3 );
43264
-
43265
-
43266
- if( (pPage->nOverflow>0 || (pPage->nFree > pBt->usableSize*2/3)) &&
43267
- (pLeafPage->nFree+2+szNext > pBt->usableSize*2/3)
43268
- ){
43269
- /* This branch is taken if the internal node is now either overflowing
43270
- ** or underfull and the leaf node will be underfull after the just cell
43271
- ** copied to the internal node is deleted from it. This is a special
43272
- ** case because the call to balance() to correct the internal node
43273
- ** may change the tree structure and invalidate the contents of
43274
- ** the leafCur.apPage[] and leafCur.aiIdx[] arrays, which will be
43275
- ** used by the balance() required to correct the underfull leaf
43276
- ** node.
43277
- **
43278
- ** The formula used in the expression above are based on facets of
43279
- ** the SQLite file-format that do not change over time.
43280
- */
43281
- testcase( pPage->nFree==pBt->usableSize*2/3+1 );
43282
- testcase( pLeafPage->nFree+2+szNext==pBt->usableSize*2/3+1 );
43283
- leafCursorInvalid = 1;
43284
- }
43285
-
43286
- if( rc==SQLITE_OK ){
43287
- assert( sqlite3PagerIswriteable(pPage->pDbPage) );
43288
- put4byte(findOverflowCell(pPage, idx), pgnoChild);
43289
- VVA_ONLY( pCur->pagesShuffled = 0 );
43290
- rc = balance(pCur, 0);
43291
- }
43292
-
43293
- if( rc==SQLITE_OK && leafCursorInvalid ){
43294
- /* The leaf-node is now underfull and so the tree needs to be
43295
- ** rebalanced. However, the balance() operation on the internal
43296
- ** node above may have modified the structure of the B-Tree and
43297
- ** so the current contents of leafCur.apPage[] and leafCur.aiIdx[]
43298
- ** may not be trusted.
43299
- **
43300
- ** It is not possible to copy the ancestry from pCur, as the same
43301
- ** balance() call has invalidated the pCur->apPage[] and aiIdx[]
43302
- ** arrays.
43303
- **
43304
- ** The call to saveCursorPosition() below internally saves the
43305
- ** key that leafCur is currently pointing to. Currently, there
43306
- ** are two copies of that key in the tree - one here on the leaf
43307
- ** page and one on some internal node in the tree. The copy on
43308
- ** the leaf node is always the next key in tree-order after the
43309
- ** copy on the internal node. So, the call to sqlite3BtreeNext()
43310
- ** calls restoreCursorPosition() to point the cursor to the copy
43311
- ** stored on the internal node, then advances to the next entry,
43312
- ** which happens to be the copy of the key on the internal node.
43313
- ** Net effect: leafCur is pointing back to the duplicate cell
43314
- ** that needs to be removed, and the leafCur.apPage[] and
43315
- ** leafCur.aiIdx[] arrays are correct.
43316
- */
43317
- VVA_ONLY( Pgno leafPgno = pLeafPage->pgno );
43318
- rc = saveCursorPosition(&leafCur);
43319
- if( rc==SQLITE_OK ){
43320
- rc = sqlite3BtreeNext(&leafCur, &notUsed);
43321
- }
43322
- pLeafPage = leafCur.apPage[leafCur.iPage];
43323
- assert( rc!=SQLITE_OK || pLeafPage->pgno==leafPgno );
43324
- assert( rc!=SQLITE_OK || leafCur.aiIdx[leafCur.iPage]==0 );
43325
- }
43326
-
43327
- if( SQLITE_OK==rc
43328
- && SQLITE_OK==(rc = sqlite3PagerWrite(pLeafPage->pDbPage))
43329
- ){
43330
- dropCell(pLeafPage, 0, szNext);
43331
- VVA_ONLY( leafCur.pagesShuffled = 0 );
43332
- rc = balance(&leafCur, 0);
43333
- assert( leafCursorInvalid || !leafCur.pagesShuffled
43334
- || !pCur->pagesShuffled );
43335
- }
43336
- }
43337
- sqlite3BtreeReleaseTempCursor(&leafCur);
43338
- }else{
43339
- TRACE(("DELETE: table=%d delete from leaf %d\n",
43340
- pCur->pgnoRoot, pPage->pgno));
43341
- rc = dropCell(pPage, idx, cellSizePtr(pPage, pCell));
43342
- if( rc==SQLITE_OK ){
43343
- rc = balance(pCur, 0);
43344
- }
43345
- }
43361
+ assert( rc==SQLITE_LOCKED_SHAREDCACHE );
43362
+ return rc; /* The table pCur points to has a read lock */
43363
+ }
43364
+
43365
+ iCellDepth = pCur->iPage;
43366
+ iCellIdx = pCur->aiIdx[iCellDepth];
43367
+ pPage = pCur->apPage[iCellDepth];
43368
+ pCell = findCell(pPage, iCellIdx);
43369
+
43370
+ /* If the page containing the entry to delete is not a leaf page, move
43371
+ ** the cursor to the largest entry in the tree that is smaller than
43372
+ ** the entry being deleted. This cell will replace the cell being deleted
43373
+ ** from the internal node. The 'previous' entry is used for this instead
43374
+ ** of the 'next' entry, as the previous entry is always a part of the
43375
+ ** sub-tree headed by the child page of the cell being deleted. This makes
43376
+ ** balancing the tree following the delete operation easier. */
43377
+ if( !pPage->leaf ){
43378
+ int notUsed;
43379
+ if( SQLITE_OK!=(rc = sqlite3BtreePrevious(pCur, &notUsed)) ){
43380
+ return rc;
43381
+ }
43382
+ }
43383
+
43384
+ /* Save the positions of any other cursors open on this table before
43385
+ ** making any modifications. Make the page containing the entry to be
43386
+ ** deleted writable. Then free any overflow pages associated with the
43387
+ ** entry and finally remove the cell itself from within the page. */
43388
+ if( SQLITE_OK!=(rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur))
43389
+ || SQLITE_OK!=(rc = sqlite3PagerWrite(pPage->pDbPage))
43390
+ || SQLITE_OK!=(rc = clearCell(pPage, pCell))
43391
+ || SQLITE_OK!=(rc = dropCell(pPage, iCellIdx, cellSizePtr(pPage, pCell)))
43392
+ ){
43393
+ return rc;
43394
+ }
43395
+
43396
+ /* If the cell deleted was not located on a leaf page, then the cursor
43397
+ ** is currently pointing to the largest entry in the sub-tree headed
43398
+ ** by the child-page of the cell that was just deleted from an internal
43399
+ ** node. The cell from the leaf node needs to be moved to the internal
43400
+ ** node to replace the deleted cell. */
43401
+ if( !pPage->leaf ){
43402
+ MemPage *pLeaf = pCur->apPage[pCur->iPage];
43403
+ int nCell;
43404
+ Pgno n = pCur->apPage[iCellDepth+1]->pgno;
43405
+ unsigned char *pTmp;
43406
+
43407
+ pCell = findCell(pLeaf, pLeaf->nCell-1);
43408
+ nCell = cellSizePtr(pLeaf, pCell);
43409
+ assert( MX_CELL_SIZE(pBt)>=nCell );
43410
+
43411
+ allocateTempSpace(pBt);
43412
+ pTmp = pBt->pTmpSpace;
43413
+
43414
+ if( SQLITE_OK!=(rc = sqlite3PagerWrite(pLeaf->pDbPage))
43415
+ || SQLITE_OK!=(rc = insertCell(pPage, iCellIdx, pCell-4, nCell+4, pTmp, n))
43416
+ || SQLITE_OK!=(rc = dropCell(pLeaf, pLeaf->nCell-1, nCell))
43417
+ ){
43418
+ return rc;
43419
+ }
43420
+ }
43421
+
43422
+ /* Balance the tree. If the entry deleted was located on a leaf page,
43423
+ ** then the cursor still points to that page. In this case the first
43424
+ ** call to balance() repairs the tree, and the if(...) condition is
43425
+ ** never true.
43426
+ **
43427
+ ** Otherwise, if the entry deleted was on an internal node page, then
43428
+ ** pCur is pointing to the leaf page from which a cell was removed to
43429
+ ** replace the cell deleted from the internal node. This is slightly
43430
+ ** tricky as the leaf node may be underfull, and the internal node may
43431
+ ** be either under or overfull. In this case run the balancing algorithm
43432
+ ** on the leaf node first. If the balance proceeds far enough up the
43433
+ ** tree that we can be sure that any problem in the internal node has
43434
+ ** been corrected, so be it. Otherwise, after balancing the leaf node,
43435
+ ** walk the cursor up the tree to the internal node and balance it as
43436
+ ** well. */
43437
+ rc = balance(pCur);
43438
+ if( rc==SQLITE_OK && pCur->iPage>iCellDepth ){
43439
+ while( pCur->iPage>iCellDepth ){
43440
+ releasePage(pCur->apPage[pCur->iPage--]);
43441
+ }
43442
+ rc = balance(pCur);
43443
+ }
43444
+
4334643445
if( rc==SQLITE_OK ){
4334743446
moveToRoot(pCur);
4334843447
}
4334943448
return rc;
4335043449
}
@@ -43431,11 +43530,14 @@
4343143530
rc = sqlite3BtreeGetPage(pBt, pgnoRoot, &pRoot, 0);
4343243531
if( rc!=SQLITE_OK ){
4343343532
return rc;
4343443533
}
4343543534
rc = ptrmapGet(pBt, pgnoRoot, &eType, &iPtrPage);
43436
- if( rc!=SQLITE_OK || eType==PTRMAP_ROOTPAGE || eType==PTRMAP_FREEPAGE ){
43535
+ if( eType==PTRMAP_ROOTPAGE || eType==PTRMAP_FREEPAGE ){
43536
+ rc = SQLITE_CORRUPT_BKPT;
43537
+ }
43538
+ if( rc!=SQLITE_OK ){
4343743539
releasePage(pRoot);
4343843540
return rc;
4343943541
}
4344043542
assert( eType!=PTRMAP_ROOTPAGE );
4344143543
assert( eType!=PTRMAP_FREEPAGE );
@@ -45188,11 +45290,11 @@
4518845290
** This file contains code use to manipulate "Mem" structure. A "Mem"
4518945291
** stores a single value in the VDBE. Mem is an opaque structure visible
4519045292
** only within the VDBE. Interface routines refer to a Mem using the
4519145293
** name sqlite_value
4519245294
**
45193
-** $Id: vdbemem.c,v 1.147 2009/05/28 11:05:57 danielk1977 Exp $
45295
+** $Id: vdbemem.c,v 1.149 2009/06/22 19:05:41 drh Exp $
4519445296
*/
4519545297
4519645298
/*
4519745299
** Call sqlite3VdbeMemExpandBlob() on the supplied value (type Mem*)
4519845300
** P if required.
@@ -45582,11 +45684,22 @@
4558245684
assert( (pMem->flags & MEM_RowSet)==0 );
4558345685
assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
4558445686
assert( EIGHT_BYTE_ALIGNMENT(pMem) );
4558545687
4558645688
pMem->u.i = doubleToInt64(pMem->r);
45587
- if( pMem->r==(double)pMem->u.i ){
45689
+
45690
+ /* Only mark the value as an integer if
45691
+ **
45692
+ ** (1) the round-trip conversion real->int->real is a no-op, and
45693
+ ** (2) The integer is neither the largest nor the smallest
45694
+ ** possible integer (ticket #3922)
45695
+ **
45696
+ ** The second term in the following conditional enforces the second
45697
+ ** condition under the assumption that additional overflow causes
45698
+ ** values to wrap around.
45699
+ */
45700
+ if( pMem->r==(double)pMem->u.i && (pMem->u.i-1) < (pMem->u.i+1) ){
4558845701
pMem->flags |= MEM_Int;
4558945702
}
4559045703
}
4559145704
4559245705
/*
@@ -45797,10 +45910,16 @@
4579745910
** The memory management strategy depends on the value of the xDel
4579845911
** parameter. If the value passed is SQLITE_TRANSIENT, then the
4579945912
** string is copied into a (possibly existing) buffer managed by the
4580045913
** Mem structure. Otherwise, any existing buffer is freed and the
4580145914
** pointer copied.
45915
+**
45916
+** If the string is too large (if it exceeds the SQLITE_LIMIT_LENGTH
45917
+** size limit) then no memory allocation occurs. If the string can be
45918
+** stored without allocating memory, then it is. If a memory allocation
45919
+** is required to store the string, then value of pMem is unchanged. In
45920
+** either case, SQLITE_TOOBIG is returned.
4580245921
*/
4580345922
SQLITE_PRIVATE int sqlite3VdbeMemSetStr(
4580445923
Mem *pMem, /* Memory cell to set to string value */
4580545924
const char *z, /* String pointer */
4580645925
int n, /* Bytes in string, or negative */
@@ -45860,13 +45979,10 @@
4586045979
sqlite3VdbeMemRelease(pMem);
4586145980
pMem->z = (char *)z;
4586245981
pMem->xDel = xDel;
4586345982
flags |= ((xDel==SQLITE_STATIC)?MEM_Static:MEM_Dyn);
4586445983
}
45865
- if( nByte>iLimit ){
45866
- return SQLITE_TOOBIG;
45867
- }
4586845984
4586945985
pMem->n = nByte;
4587045986
pMem->flags = flags;
4587145987
pMem->enc = (enc==0 ? SQLITE_UTF8 : enc);
4587245988
pMem->type = (enc==0 ? SQLITE_BLOB : SQLITE_TEXT);
@@ -45874,10 +45990,14 @@
4587445990
#ifndef SQLITE_OMIT_UTF16
4587545991
if( pMem->enc!=SQLITE_UTF8 && sqlite3VdbeMemHandleBom(pMem) ){
4587645992
return SQLITE_NOMEM;
4587745993
}
4587845994
#endif
45995
+
45996
+ if( nByte>iLimit ){
45997
+ return SQLITE_TOOBIG;
45998
+ }
4587945999
4588046000
return SQLITE_OK;
4588146001
}
4588246002
4588346003
/*
@@ -46250,11 +46370,11 @@
4625046370
** This file contains code used for creating, destroying, and populating
4625146371
** a VDBE (or an "sqlite3_stmt" as it is known to the outside world.) Prior
4625246372
** to version 2.8.7, all this code was combined into the vdbe.c source file.
4625346373
** But that file was getting too big so this subroutines were split out.
4625446374
**
46255
-** $Id: vdbeaux.c,v 1.459 2009/06/05 14:17:25 drh Exp $
46375
+** $Id: vdbeaux.c,v 1.464 2009/06/23 14:15:04 drh Exp $
4625646376
*/
4625746377
4625846378
4625946379
4626046380
/*
@@ -46860,15 +46980,28 @@
4686046980
** If a memory allocation error has occurred prior to the calling of this
4686146981
** routine, then a pointer to a dummy VdbeOp will be returned. That opcode
4686246982
** is readable and writable, but it has no effect. The return of a dummy
4686346983
** opcode allows the call to continue functioning after a OOM fault without
4686446984
** having to check to see if the return from this routine is a valid pointer.
46985
+**
46986
+** About the #ifdef SQLITE_OMIT_TRACE: Normally, this routine is never called
46987
+** unless p->nOp>0. This is because in the absense of SQLITE_OMIT_TRACE,
46988
+** an OP_Trace instruction is always inserted by sqlite3VdbeGet() as soon as
46989
+** a new VDBE is created. So we are free to set addr to p->nOp-1 without
46990
+** having to double-check to make sure that the result is non-negative. But
46991
+** if SQLITE_OMIT_TRACE is defined, the OP_Trace is omitted and we do need to
46992
+** check the value of p->nOp-1 before continuing.
4686546993
*/
4686646994
SQLITE_PRIVATE VdbeOp *sqlite3VdbeGetOp(Vdbe *p, int addr){
4686746995
static VdbeOp dummy;
4686846996
assert( p->magic==VDBE_MAGIC_INIT );
46869
- if( addr<0 ) addr = p->nOp - 1;
46997
+ if( addr<0 ){
46998
+#ifdef SQLITE_OMIT_TRACE
46999
+ if( p->nOp==0 ) return &dummy;
47000
+#endif
47001
+ addr = p->nOp - 1;
47002
+ }
4687047003
assert( (addr>=0 && addr<p->nOp) || p->db->mallocFailed );
4687147004
if( p->db->mallocFailed ){
4687247005
return &dummy;
4687347006
}else{
4687447007
return &p->aOp[addr];
@@ -48235,13 +48368,21 @@
4823548368
sqlite3DbFree(db, p->pFree);
4823648369
sqlite3DbFree(db, p);
4823748370
}
4823848371
4823948372
/*
48373
+** Make sure the cursor p is ready to read or write the row to which it
48374
+** was last positioned. Return an error code if an OOM fault or I/O error
48375
+** prevents us from positioning the cursor to its correct position.
48376
+**
4824048377
** If a MoveTo operation is pending on the given cursor, then do that
48241
-** MoveTo now. Return an error code. If no MoveTo is pending, this
48242
-** routine does nothing and returns SQLITE_OK.
48378
+** MoveTo now. If no move is pending, check to see if the row has been
48379
+** deleted out from under the cursor and if it has, mark the row as
48380
+** a NULL row.
48381
+**
48382
+** If the cursor is already pointing to the correct row and that row has
48383
+** not been deleted out from under the cursor, then this routine is a no-op.
4824348384
*/
4824448385
SQLITE_PRIVATE int sqlite3VdbeCursorMoveto(VdbeCursor *p){
4824548386
if( p->deferredMoveto ){
4824648387
int res, rc;
4824748388
#ifdef SQLITE_TEST
@@ -48248,11 +48389,11 @@
4824848389
extern int sqlite3_search_count;
4824948390
#endif
4825048391
assert( p->isTable );
4825148392
rc = sqlite3BtreeMovetoUnpacked(p->pCursor, 0, p->movetoTarget, 0, &res);
4825248393
if( rc ) return rc;
48253
- p->lastRowid = keyToInt(p->movetoTarget);
48394
+ p->lastRowid = p->movetoTarget;
4825448395
p->rowidIsValid = ALWAYS(res==0) ?1:0;
4825548396
if( NEVER(res<0) ){
4825648397
rc = sqlite3BtreeNext(p->pCursor, &res);
4825748398
if( rc ) return rc;
4825848399
}
@@ -48450,11 +48591,11 @@
4845048591
swapMixedEndianFloat(v);
4845148592
}else{
4845248593
v = pMem->u.i;
4845348594
}
4845448595
len = i = sqlite3VdbeSerialTypeLen(serial_type);
48455
- assert( len<=nBuf );
48596
+ assert( len<=(u32)nBuf );
4845648597
while( i-- ){
4845748598
buf[i] = (u8)(v&0xFF);
4845848599
v >>= 8;
4845948600
}
4846048601
return len;
@@ -48461,11 +48602,11 @@
4846148602
}
4846248603
4846348604
/* String or blob */
4846448605
if( serial_type>=12 ){
4846548606
assert( pMem->n + ((pMem->flags & MEM_Zero)?pMem->u.nZero:0)
48466
- == sqlite3VdbeSerialTypeLen(serial_type) );
48607
+ == (int)sqlite3VdbeSerialTypeLen(serial_type) );
4846748608
assert( pMem->n<=nBuf );
4846848609
len = pMem->n;
4846948610
memcpy(buf, pMem->z, len);
4847048611
if( pMem->flags & MEM_Zero ){
4847148612
len += pMem->u.nZero;
@@ -48790,11 +48931,11 @@
4879048931
** Return SQLITE_OK if everything works, or an error code otherwise.
4879148932
**
4879248933
** pCur might be pointing to text obtained from a corrupt database file.
4879348934
** So the content cannot be trusted. Do appropriate checks on the content.
4879448935
*/
48795
-SQLITE_PRIVATE int sqlite3VdbeIdxRowid(BtCursor *pCur, i64 *rowid){
48936
+SQLITE_PRIVATE int sqlite3VdbeIdxRowid(sqlite3 *db, BtCursor *pCur, i64 *rowid){
4879648937
i64 nCellKey = 0;
4879748938
int rc;
4879848939
u32 szHdr; /* Size of the header */
4879948940
u32 typeRowid; /* Serial type of the rowid */
4880048941
u32 lenRowid; /* Size of the rowid */
@@ -48807,11 +48948,11 @@
4880748948
return SQLITE_CORRUPT_BKPT;
4880848949
}
4880948950
4881048951
/* Read in the complete content of the index entry */
4881148952
m.flags = 0;
48812
- m.db = 0;
48953
+ m.db = db;
4881348954
m.zMalloc = 0;
4881448955
rc = sqlite3VdbeMemFromBtree(pCur, 0, (int)nCellKey, 1, &m);
4881548956
if( rc ){
4881648957
return rc;
4881748958
}
@@ -48957,168 +49098,12 @@
4895749098
*************************************************************************
4895849099
**
4895949100
** This file contains code use to implement APIs that are part of the
4896049101
** VDBE.
4896149102
**
48962
-** $Id: vdbeapi.c,v 1.165 2009/06/06 14:13:27 danielk1977 Exp $
48963
-*/
48964
-
48965
-#if 0 && defined(SQLITE_ENABLE_MEMORY_MANAGEMENT)
48966
-/*
48967
-** The following structure contains pointers to the end points of a
48968
-** doubly-linked list of all compiled SQL statements that may be holding
48969
-** buffers eligible for release when the sqlite3_release_memory() interface is
48970
-** invoked. Access to this list is protected by the SQLITE_MUTEX_STATIC_LRU2
48971
-** mutex.
48972
-**
48973
-** Statements are added to the end of this list when sqlite3_reset() is
48974
-** called. They are removed either when sqlite3_step() or sqlite3_finalize()
48975
-** is called. When statements are added to this list, the associated
48976
-** register array (p->aMem[1..p->nMem]) may contain dynamic buffers that
48977
-** can be freed using sqlite3VdbeReleaseMemory().
48978
-**
48979
-** When statements are added or removed from this list, the mutex
48980
-** associated with the Vdbe being added or removed (Vdbe.db->mutex) is
48981
-** already held. The LRU2 mutex is then obtained, blocking if necessary,
48982
-** the linked-list pointers manipulated and the LRU2 mutex relinquished.
48983
-*/
48984
-struct StatementLruList {
48985
- Vdbe *pFirst;
48986
- Vdbe *pLast;
48987
-};
48988
-static struct StatementLruList sqlite3LruStatements;
48989
-
48990
-/*
48991
-** Check that the list looks to be internally consistent. This is used
48992
-** as part of an assert() statement as follows:
48993
-**
48994
-** assert( stmtLruCheck() );
48995
-*/
48996
-#ifndef NDEBUG
48997
-static int stmtLruCheck(){
48998
- Vdbe *p;
48999
- for(p=sqlite3LruStatements.pFirst; p; p=p->pLruNext){
49000
- assert(p->pLruNext || p==sqlite3LruStatements.pLast);
49001
- assert(!p->pLruNext || p->pLruNext->pLruPrev==p);
49002
- assert(p->pLruPrev || p==sqlite3LruStatements.pFirst);
49003
- assert(!p->pLruPrev || p->pLruPrev->pLruNext==p);
49004
- }
49005
- return 1;
49006
-}
49007
-#endif
49008
-
49009
-/*
49010
-** Add vdbe p to the end of the statement lru list. It is assumed that
49011
-** p is not already part of the list when this is called. The lru list
49012
-** is protected by the SQLITE_MUTEX_STATIC_LRU mutex.
49013
-*/
49014
-static void stmtLruAdd(Vdbe *p){
49015
- sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_LRU2));
49016
-
49017
- if( p->pLruPrev || p->pLruNext || sqlite3LruStatements.pFirst==p ){
49018
- sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_LRU2));
49019
- return;
49020
- }
49021
-
49022
- assert( stmtLruCheck() );
49023
-
49024
- if( !sqlite3LruStatements.pFirst ){
49025
- assert( !sqlite3LruStatements.pLast );
49026
- sqlite3LruStatements.pFirst = p;
49027
- sqlite3LruStatements.pLast = p;
49028
- }else{
49029
- assert( !sqlite3LruStatements.pLast->pLruNext );
49030
- p->pLruPrev = sqlite3LruStatements.pLast;
49031
- sqlite3LruStatements.pLast->pLruNext = p;
49032
- sqlite3LruStatements.pLast = p;
49033
- }
49034
-
49035
- assert( stmtLruCheck() );
49036
-
49037
- sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_LRU2));
49038
-}
49039
-
49040
-/*
49041
-** Assuming the SQLITE_MUTEX_STATIC_LRU2 mutext is already held, remove
49042
-** statement p from the least-recently-used statement list. If the
49043
-** statement is not currently part of the list, this call is a no-op.
49044
-*/
49045
-static void stmtLruRemoveNomutex(Vdbe *p){
49046
- if( p->pLruPrev || p->pLruNext || p==sqlite3LruStatements.pFirst ){
49047
- assert( stmtLruCheck() );
49048
- if( p->pLruNext ){
49049
- p->pLruNext->pLruPrev = p->pLruPrev;
49050
- }else{
49051
- sqlite3LruStatements.pLast = p->pLruPrev;
49052
- }
49053
- if( p->pLruPrev ){
49054
- p->pLruPrev->pLruNext = p->pLruNext;
49055
- }else{
49056
- sqlite3LruStatements.pFirst = p->pLruNext;
49057
- }
49058
- p->pLruNext = 0;
49059
- p->pLruPrev = 0;
49060
- assert( stmtLruCheck() );
49061
- }
49062
-}
49063
-
49064
-/*
49065
-** Assuming the SQLITE_MUTEX_STATIC_LRU2 mutext is not held, remove
49066
-** statement p from the least-recently-used statement list. If the
49067
-** statement is not currently part of the list, this call is a no-op.
49068
-*/
49069
-static void stmtLruRemove(Vdbe *p){
49070
- sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_LRU2));
49071
- stmtLruRemoveNomutex(p);
49072
- sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_LRU2));
49073
-}
49074
-
49075
-/*
49076
-** Try to release n bytes of memory by freeing buffers associated
49077
-** with the memory registers of currently unused vdbes.
49078
-*/
49079
-SQLITE_PRIVATE int sqlite3VdbeReleaseMemory(int n){
49080
- Vdbe *p;
49081
- Vdbe *pNext;
49082
- int nFree = 0;
49083
-
49084
- sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_LRU2));
49085
- for(p=sqlite3LruStatements.pFirst; p && nFree<n; p=pNext){
49086
- pNext = p->pLruNext;
49087
-
49088
- /* For each statement handle in the lru list, attempt to obtain the
49089
- ** associated database mutex. If it cannot be obtained, continue
49090
- ** to the next statement handle. It is not possible to block on
49091
- ** the database mutex - that could cause deadlock.
49092
- */
49093
- if( SQLITE_OK==sqlite3_mutex_try(p->db->mutex) ){
49094
- nFree += sqlite3VdbeReleaseBuffers(p);
49095
- stmtLruRemoveNomutex(p);
49096
- sqlite3_mutex_leave(p->db->mutex);
49097
- }
49098
- }
49099
- sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_LRU2));
49100
-
49101
- return nFree;
49102
-}
49103
-
49104
-/*
49105
-** Call sqlite3Reprepare() on the statement. Remove it from the
49106
-** lru list before doing so, as Reprepare() will free all the
49107
-** memory register buffers anyway.
49108
-*/
49109
-int vdbeReprepare(Vdbe *p){
49110
- stmtLruRemove(p);
49111
- return sqlite3Reprepare(p);
49112
-}
49113
-
49114
-#else /* !SQLITE_ENABLE_MEMORY_MANAGEMENT */
49115
- #define stmtLruRemove(x)
49116
- #define stmtLruAdd(x)
49117
- #define vdbeReprepare(x) sqlite3Reprepare(x)
49118
-#endif
49119
-
49103
+** $Id: vdbeapi.c,v 1.166 2009/06/19 14:06:03 drh Exp $
49104
+*/
4912049105
4912149106
#ifndef SQLITE_OMIT_DEPRECATED
4912249107
/*
4912349108
** Return TRUE (non-zero) of the statement supplied as an argument needs
4912449109
** to be recompiled. A statement needs to be recompiled whenever the
@@ -49151,11 +49136,10 @@
4915149136
sqlite3 *db = v->db;
4915249137
#if SQLITE_THREADSAFE
4915349138
sqlite3_mutex *mutex = v->db->mutex;
4915449139
#endif
4915549140
sqlite3_mutex_enter(mutex);
49156
- stmtLruRemove(v);
4915749141
rc = sqlite3VdbeFinalize(v);
4915849142
rc = sqlite3ApiExit(db, rc);
4915949143
sqlite3_mutex_leave(mutex);
4916049144
}
4916149145
return rc;
@@ -49175,11 +49159,10 @@
4917549159
rc = SQLITE_OK;
4917649160
}else{
4917749161
Vdbe *v = (Vdbe*)pStmt;
4917849162
sqlite3_mutex_enter(v->db->mutex);
4917949163
rc = sqlite3VdbeReset(v);
49180
- stmtLruAdd(v);
4918149164
sqlite3VdbeMakeReady(v, -1, 0, 0, 0);
4918249165
assert( (rc & (v->db->errMask))==rc );
4918349166
rc = sqlite3ApiExit(v->db, rc);
4918449167
sqlite3_mutex_leave(v->db->mutex);
4918549168
}
@@ -49419,11 +49402,10 @@
4941949402
#endif
4942049403
4942149404
db->activeVdbeCnt++;
4942249405
if( p->readOnly==0 ) db->writeVdbeCnt++;
4942349406
p->pc = 0;
49424
- stmtLruRemove(p);
4942549407
}
4942649408
#ifndef SQLITE_OMIT_EXPLAIN
4942749409
if( p->explain ){
4942849410
rc = sqlite3VdbeList(p);
4942949411
}else
@@ -49479,33 +49461,20 @@
4947949461
/*
4948049462
** This is the top-level implementation of sqlite3_step(). Call
4948149463
** sqlite3Step() to do most of the work. If a schema error occurs,
4948249464
** call sqlite3Reprepare() and try again.
4948349465
*/
49484
-#ifdef SQLITE_OMIT_PARSER
49485
-SQLITE_API int sqlite3_step(sqlite3_stmt *pStmt){
49486
- int rc = SQLITE_MISUSE;
49487
- if( pStmt ){
49488
- Vdbe *v;
49489
- v = (Vdbe*)pStmt;
49490
- sqlite3_mutex_enter(v->db->mutex);
49491
- rc = sqlite3Step(v);
49492
- sqlite3_mutex_leave(v->db->mutex);
49493
- }
49494
- return rc;
49495
-}
49496
-#else
4949749466
SQLITE_API int sqlite3_step(sqlite3_stmt *pStmt){
4949849467
int rc = SQLITE_MISUSE;
4949949468
if( pStmt ){
4950049469
int cnt = 0;
4950149470
Vdbe *v = (Vdbe*)pStmt;
4950249471
sqlite3 *db = v->db;
4950349472
sqlite3_mutex_enter(db->mutex);
4950449473
while( (rc = sqlite3Step(v))==SQLITE_SCHEMA
4950549474
&& cnt++ < 5
49506
- && (rc = vdbeReprepare(v))==SQLITE_OK ){
49475
+ && (rc = sqlite3Reprepare(v))==SQLITE_OK ){
4950749476
sqlite3_reset(pStmt);
4950849477
v->expired = 0;
4950949478
}
4951049479
if( rc==SQLITE_SCHEMA && ALWAYS(v->isPrepareV2) && ALWAYS(db->pErr) ){
4951149480
/* This case occurs after failing to recompile an sql statement.
@@ -49528,11 +49497,10 @@
4952849497
rc = sqlite3ApiExit(db, rc);
4952949498
sqlite3_mutex_leave(db->mutex);
4953049499
}
4953149500
return rc;
4953249501
}
49533
-#endif
4953449502
4953549503
/*
4953649504
** Extract the user data from a sqlite3_context structure and return a
4953749505
** pointer to it.
4953849506
*/
@@ -50351,11 +50319,11 @@
5035150319
** documentation, headers files, or other derived files. The formatting
5035250320
** of the code in this file is, therefore, important. See other comments
5035350321
** in this file for details. If in doubt, do not deviate from existing
5035450322
** commenting and indentation practices when changing or adding code.
5035550323
**
50356
-** $Id: vdbe.c,v 1.848 2009/06/05 16:46:53 drh Exp $
50324
+** $Id: vdbe.c,v 1.862 2009/06/23 14:15:04 drh Exp $
5035750325
*/
5035850326
5035950327
/*
5036050328
** The following global variable is incremented every time a cursor
5036150329
** moves, either by the OP_SeekXX, OP_Next, or OP_Prev opcodes. The test
@@ -50495,11 +50463,11 @@
5049550463
static VdbeCursor *allocateCursor(
5049650464
Vdbe *p, /* The virtual machine */
5049750465
int iCur, /* Index of the new VdbeCursor */
5049850466
int nField, /* Number of fields in the table or index */
5049950467
int iDb, /* When database the cursor belongs to, or -1 */
50500
- int isBtreeCursor /* */
50468
+ int isBtreeCursor /* True for B-Tree vs. pseudo-table or vtab */
5050150469
){
5050250470
/* Find the memory cell that will be used to store the blob of memory
5050350471
** required for this VdbeCursor structure. It is convenient to use a
5050450472
** vdbe memory cell to manage the memory allocation required for a
5050550473
** VdbeCursor structure for the following reasons:
@@ -50732,12 +50700,14 @@
5073250700
fprintf(out, " NULL");
5073350701
}else if( (p->flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){
5073450702
fprintf(out, " si:%lld", p->u.i);
5073550703
}else if( p->flags & MEM_Int ){
5073650704
fprintf(out, " i:%lld", p->u.i);
50705
+#ifndef SQLITE_OMIT_FLOATING_POINT
5073750706
}else if( p->flags & MEM_Real ){
5073850707
fprintf(out, " r:%g", p->r);
50708
+#endif
5073950709
}else if( p->flags & MEM_RowSet ){
5074050710
fprintf(out, " (rowset)");
5074150711
}else{
5074250712
char zBuf[200];
5074350713
sqlite3VdbeMemPrettyPrint(p, zBuf);
@@ -51032,13 +51002,10 @@
5103251002
int v2; /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
5103351003
} ak;
5103451004
struct OP_IfNot_stack_vars {
5103551005
int c;
5103651006
} al;
51037
- struct OP_IsNull_stack_vars {
51038
- int n;
51039
- } am;
5104051007
struct OP_Column_stack_vars {
5104151008
u32 payloadSize; /* Number of bytes in the record */
5104251009
i64 payloadSize64; /* Number of bytes in the record */
5104351010
int p1; /* P1 value of the opcode */
5104451011
int p2; /* column number to retrieve */
@@ -51057,17 +51024,17 @@
5105751024
u8 *zEndHdr; /* Pointer to first byte after the header */
5105851025
u32 offset; /* Offset into the data */
5105951026
u64 offset64; /* 64-bit offset. 64 bits needed to catch overflow */
5106051027
int szHdr; /* Size of the header size field at start of record */
5106151028
int avail; /* Number of bytes of available data */
51062
- } an;
51029
+ } am;
5106351030
struct OP_Affinity_stack_vars {
5106451031
char *zAffinity; /* The affinity to be applied */
5106551032
Mem *pData0; /* First register to which to apply affinity */
5106651033
Mem *pLast; /* Last register to which to apply affinity */
5106751034
Mem *pRec; /* Current register */
51068
- } ao;
51035
+ } an;
5106951036
struct OP_MakeRecord_stack_vars {
5107051037
u8 *zNewRecord; /* A buffer to hold the data for the new record */
5107151038
Mem *pRec; /* The new record */
5107251039
u64 nData; /* Number of bytes of data space */
5107351040
int nHdr; /* Number of bytes of header space */
@@ -51080,270 +51047,246 @@
5108051047
int nField; /* Number of fields in the record */
5108151048
char *zAffinity; /* The affinity string for the record */
5108251049
int file_format; /* File format to use for encoding */
5108351050
int i; /* Space used in zNewRecord[] */
5108451051
int len; /* Length of a field */
51085
- } ap;
51052
+ } ao;
5108651053
struct OP_Count_stack_vars {
5108751054
i64 nEntry;
5108851055
BtCursor *pCrsr;
51089
- } aq;
51056
+ } ap;
5109051057
struct OP_Statement_stack_vars {
51091
- int i;
5109251058
Btree *pBt;
51093
- } ar;
51059
+ } aq;
5109451060
struct OP_Savepoint_stack_vars {
5109551061
int p1; /* Value of P1 operand */
5109651062
char *zName; /* Name of savepoint */
5109751063
int nName;
5109851064
Savepoint *pNew;
5109951065
Savepoint *pSavepoint;
5110051066
Savepoint *pTmp;
5110151067
int iSavepoint;
5110251068
int ii;
51103
- } as;
51069
+ } ar;
5110451070
struct OP_AutoCommit_stack_vars {
5110551071
int desiredAutoCommit;
51106
- int rollback;
51072
+ int iRollback;
5110751073
int turnOnAC;
51108
- } at;
51074
+ } as;
5110951075
struct OP_Transaction_stack_vars {
51110
- int i;
5111151076
Btree *pBt;
51112
- } au;
51077
+ } at;
5111351078
struct OP_ReadCookie_stack_vars {
5111451079
int iMeta;
5111551080
int iDb;
5111651081
int iCookie;
51117
- } av;
51082
+ } au;
5111851083
struct OP_SetCookie_stack_vars {
5111951084
Db *pDb;
51120
- } aw;
51085
+ } av;
5112151086
struct OP_VerifyCookie_stack_vars {
5112251087
int iMeta;
5112351088
Btree *pBt;
51124
- } ax;
51089
+ } aw;
5112551090
struct OP_OpenWrite_stack_vars {
5112651091
int nField;
5112751092
KeyInfo *pKeyInfo;
51128
- int i;
5112951093
int p2;
5113051094
int iDb;
5113151095
int wrFlag;
5113251096
Btree *pX;
5113351097
VdbeCursor *pCur;
5113451098
Db *pDb;
5113551099
int flags;
51136
- } ay;
51100
+ } ax;
5113751101
struct OP_OpenEphemeral_stack_vars {
51138
- int i;
51102
+ VdbeCursor *pCx;
51103
+ } ay;
51104
+ struct OP_OpenPseudo_stack_vars {
5113951105
VdbeCursor *pCx;
5114051106
} az;
51141
- struct OP_OpenPseudo_stack_vars {
51142
- int i;
51143
- VdbeCursor *pCx;
51144
- } ba;
51145
- struct OP_Close_stack_vars {
51146
- int i;
51147
- } bb;
5114851107
struct OP_SeekGt_stack_vars {
51149
- int i;
5115051108
int res;
5115151109
int oc;
5115251110
VdbeCursor *pC;
5115351111
UnpackedRecord r;
5115451112
int nField;
5115551113
i64 iKey; /* The rowid we are to seek to */
51156
- } bc;
51114
+ } ba;
5115751115
struct OP_Seek_stack_vars {
51158
- int i;
5115951116
VdbeCursor *pC;
51160
- } bd;
51117
+ } bb;
5116151118
struct OP_Found_stack_vars {
51162
- int i;
5116351119
int alreadyExists;
5116451120
VdbeCursor *pC;
5116551121
int res;
5116651122
UnpackedRecord *pIdxKey;
5116751123
char aTempRec[ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*3 + 7];
51168
- } be;
51124
+ } bc;
5116951125
struct OP_IsUnique_stack_vars {
5117051126
u16 ii;
5117151127
VdbeCursor *pCx;
5117251128
BtCursor *pCrsr;
5117351129
u16 nField;
5117451130
Mem *aMem;
5117551131
UnpackedRecord r; /* B-Tree index search key */
5117651132
i64 R; /* Rowid stored in register P3 */
51177
- } bf;
51133
+ } bd;
5117851134
struct OP_NotExists_stack_vars {
51179
- int i;
5118051135
VdbeCursor *pC;
5118151136
BtCursor *pCrsr;
5118251137
int res;
5118351138
u64 iKey;
51184
- } bg;
51139
+ } be;
5118551140
struct OP_NewRowid_stack_vars {
51186
- int i;
51187
- i64 v;
51188
- VdbeCursor *pC;
51189
- int res;
51190
- int rx;
51191
- int cnt;
51192
- i64 x;
51193
- Mem *pMem;
51194
- } bh;
51141
+ i64 v; /* The new rowid */
51142
+ VdbeCursor *pC; /* Cursor of table to get the new rowid */
51143
+ int res; /* Result of an sqlite3BtreeLast() */
51144
+ int cnt; /* Counter to limit the number of searches */
51145
+ Mem *pMem; /* Register holding largest rowid for AUTOINCREMENT */
51146
+ } bf;
5119551147
struct OP_Insert_stack_vars {
5119651148
Mem *pData;
5119751149
Mem *pKey;
5119851150
i64 iKey; /* The integer ROWID or key for the record to be inserted */
51199
- int i;
5120051151
VdbeCursor *pC;
5120151152
int nZero;
5120251153
int seekResult;
5120351154
const char *zDb;
5120451155
const char *zTbl;
5120551156
int op;
51206
- } bi;
51157
+ } bg;
5120751158
struct OP_Delete_stack_vars {
51208
- int i;
5120951159
i64 iKey;
5121051160
VdbeCursor *pC;
51211
- } bj;
51161
+ } bh;
5121251162
struct OP_RowData_stack_vars {
51213
- int i;
5121451163
VdbeCursor *pC;
5121551164
BtCursor *pCrsr;
5121651165
u32 n;
5121751166
i64 n64;
51218
- } bk;
51167
+ } bi;
5121951168
struct OP_Rowid_stack_vars {
51220
- int i;
5122151169
VdbeCursor *pC;
5122251170
i64 v;
5122351171
sqlite3_vtab *pVtab;
5122451172
const sqlite3_module *pModule;
51225
- } bl;
51173
+ } bj;
5122651174
struct OP_NullRow_stack_vars {
51227
- int i;
5122851175
VdbeCursor *pC;
51229
- } bm;
51176
+ } bk;
5123051177
struct OP_Last_stack_vars {
51231
- int i;
51178
+ VdbeCursor *pC;
51179
+ BtCursor *pCrsr;
51180
+ int res;
51181
+ } bl;
51182
+ struct OP_Rewind_stack_vars {
51183
+ VdbeCursor *pC;
51184
+ BtCursor *pCrsr;
51185
+ int res;
51186
+ } bm;
51187
+ struct OP_Next_stack_vars {
5123251188
VdbeCursor *pC;
5123351189
BtCursor *pCrsr;
5123451190
int res;
5123551191
} bn;
51236
- struct OP_Rewind_stack_vars {
51237
- int i;
51238
- VdbeCursor *pC;
51239
- BtCursor *pCrsr;
51240
- int res;
51241
- } bo;
51242
- struct OP_Next_stack_vars {
51243
- VdbeCursor *pC;
51244
- BtCursor *pCrsr;
51245
- int res;
51246
- } bp;
5124751192
struct OP_IdxInsert_stack_vars {
51248
- int i;
5124951193
VdbeCursor *pC;
5125051194
BtCursor *pCrsr;
5125151195
int nKey;
5125251196
const char *zKey;
51253
- } bq;
51197
+ } bo;
5125451198
struct OP_IdxDelete_stack_vars {
51255
- int i;
5125651199
VdbeCursor *pC;
5125751200
BtCursor *pCrsr;
51258
- } br;
51201
+ int res;
51202
+ UnpackedRecord r;
51203
+ } bp;
5125951204
struct OP_IdxRowid_stack_vars {
51260
- int i;
5126151205
BtCursor *pCrsr;
5126251206
VdbeCursor *pC;
5126351207
i64 rowid;
51264
- } bs;
51208
+ } bq;
5126551209
struct OP_IdxGE_stack_vars {
51266
- int i;
5126751210
VdbeCursor *pC;
5126851211
int res;
5126951212
UnpackedRecord r;
51270
- } bt;
51213
+ } br;
5127151214
struct OP_Destroy_stack_vars {
5127251215
int iMoved;
5127351216
int iCnt;
5127451217
Vdbe *pVdbe;
5127551218
int iDb;
51276
- } bu;
51219
+ } bs;
5127751220
struct OP_Clear_stack_vars {
5127851221
int nChange;
51279
- } bv;
51222
+ } bt;
5128051223
struct OP_CreateTable_stack_vars {
5128151224
int pgno;
5128251225
int flags;
5128351226
Db *pDb;
51284
- } bw;
51227
+ } bu;
5128551228
struct OP_ParseSchema_stack_vars {
5128651229
int iDb;
5128751230
const char *zMaster;
5128851231
char *zSql;
5128951232
InitData initData;
51290
- } bx;
51233
+ } bv;
5129151234
struct OP_IntegrityCk_stack_vars {
5129251235
int nRoot; /* Number of tables to check. (Number of root pages.) */
5129351236
int *aRoot; /* Array of rootpage numbers for tables to be checked */
5129451237
int j; /* Loop counter */
5129551238
int nErr; /* Number of errors reported */
5129651239
char *z; /* Text of the error report */
5129751240
Mem *pnErr; /* Register keeping track of errors remaining */
51298
- } by;
51241
+ } bw;
5129951242
struct OP_RowSetAdd_stack_vars {
5130051243
Mem *pIdx;
5130151244
Mem *pVal;
51302
- } bz;
51245
+ } bx;
5130351246
struct OP_RowSetRead_stack_vars {
5130451247
Mem *pIdx;
5130551248
i64 val;
51306
- } ca;
51249
+ } by;
5130751250
struct OP_RowSetTest_stack_vars {
5130851251
int iSet;
5130951252
int exists;
51310
- } cb;
51253
+ } bz;
5131151254
struct OP_ContextPush_stack_vars {
5131251255
int i;
5131351256
Context *pContext;
51314
- } cc;
51257
+ } ca;
5131551258
struct OP_ContextPop_stack_vars {
5131651259
Context *pContext;
51317
- } cd;
51260
+ } cb;
5131851261
struct OP_AggStep_stack_vars {
5131951262
int n;
5132051263
int i;
5132151264
Mem *pMem;
5132251265
Mem *pRec;
5132351266
sqlite3_context ctx;
5132451267
sqlite3_value **apVal;
51325
- } ce;
51268
+ } cc;
5132651269
struct OP_AggFinal_stack_vars {
5132751270
Mem *pMem;
51328
- } cf;
51271
+ } cd;
5132951272
struct OP_IncrVacuum_stack_vars {
5133051273
Btree *pBt;
51331
- } cg;
51274
+ } ce;
5133251275
struct OP_TableLock_stack_vars {
5133351276
int p1;
5133451277
u8 isWriteLock;
51335
- } ch;
51278
+ } cf;
5133651279
struct OP_VBegin_stack_vars {
5133751280
sqlite3_vtab *pVtab;
51338
- } ci;
51281
+ } cg;
5133951282
struct OP_VOpen_stack_vars {
5134051283
VdbeCursor *pCur;
5134151284
sqlite3_vtab_cursor *pVtabCursor;
5134251285
sqlite3_vtab *pVtab;
5134351286
sqlite3_module *pModule;
51344
- } cj;
51287
+ } ch;
5134551288
struct OP_VFilter_stack_vars {
5134651289
int nArg;
5134751290
int iQuery;
5134851291
const sqlite3_module *pModule;
5134951292
Mem *pQuery;
@@ -51352,44 +51295,44 @@
5135251295
sqlite3_vtab *pVtab;
5135351296
VdbeCursor *pCur;
5135451297
int res;
5135551298
int i;
5135651299
Mem **apArg;
51357
- } ck;
51300
+ } ci;
5135851301
struct OP_VColumn_stack_vars {
5135951302
sqlite3_vtab *pVtab;
5136051303
const sqlite3_module *pModule;
5136151304
Mem *pDest;
5136251305
sqlite3_context sContext;
51363
- } cl;
51306
+ } cj;
5136451307
struct OP_VNext_stack_vars {
5136551308
sqlite3_vtab *pVtab;
5136651309
const sqlite3_module *pModule;
5136751310
int res;
5136851311
VdbeCursor *pCur;
51369
- } cm;
51312
+ } ck;
5137051313
struct OP_VRename_stack_vars {
5137151314
sqlite3_vtab *pVtab;
5137251315
Mem *pName;
51373
- } cn;
51316
+ } cl;
5137451317
struct OP_VUpdate_stack_vars {
5137551318
sqlite3_vtab *pVtab;
5137651319
sqlite3_module *pModule;
5137751320
int nArg;
5137851321
int i;
5137951322
sqlite_int64 rowid;
5138051323
Mem **apArg;
5138151324
Mem *pX;
51382
- } co;
51325
+ } cm;
5138351326
struct OP_Pagecount_stack_vars {
5138451327
int p1;
5138551328
int nPage;
5138651329
Pager *pPager;
51387
- } cp;
51330
+ } cn;
5138851331
struct OP_Trace_stack_vars {
5138951332
char *zTrace;
51390
- } cq;
51333
+ } co;
5139151334
} u;
5139251335
/* End automatically generated code
5139351336
********************************************************************/
5139451337
5139551338
assert( p->magic==VDBE_MAGIC_RUN ); /* sqlite3_step() verifies this */
@@ -51732,27 +51675,24 @@
5173251675
pOp->opcode = OP_String;
5173351676
pOp->p1 = sqlite3Strlen30(pOp->p4.z);
5173451677
5173551678
#ifndef SQLITE_OMIT_UTF16
5173651679
if( encoding!=SQLITE_UTF8 ){
51737
- sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC);
51680
+ rc = sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC);
51681
+ if( rc==SQLITE_TOOBIG ) goto too_big;
5173851682
if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pOut, encoding) ) goto no_mem;
51739
- if( SQLITE_OK!=sqlite3VdbeMemMakeWriteable(pOut) ) goto no_mem;
51683
+ assert( pOut->zMalloc==pOut->z );
51684
+ assert( pOut->flags & MEM_Dyn );
5174051685
pOut->zMalloc = 0;
5174151686
pOut->flags |= MEM_Static;
5174251687
pOut->flags &= ~MEM_Dyn;
5174351688
if( pOp->p4type==P4_DYNAMIC ){
5174451689
sqlite3DbFree(db, pOp->p4.z);
5174551690
}
5174651691
pOp->p4type = P4_DYNAMIC;
5174751692
pOp->p4.z = pOut->z;
5174851693
pOp->p1 = pOut->n;
51749
- if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
51750
- goto too_big;
51751
- }
51752
- UPDATE_MAX_BLOBSIZE(pOut);
51753
- break;
5175451694
}
5175551695
#endif
5175651696
if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
5175751697
goto too_big;
5175851698
}
@@ -51940,13 +51880,17 @@
5194051880
** opened by this VM before returning control to the user. This is to
5194151881
** ensure that statement-transactions are always nested, not overlapping.
5194251882
** If the open statement-transaction is not closed here, then the user
5194351883
** may step another VM that opens its own statement transaction. This
5194451884
** may lead to overlapping statement transactions.
51885
+ **
51886
+ ** The statement transaction is never a top-level transaction. Hence
51887
+ ** the RELEASE call below can never fail.
5194551888
*/
5194651889
assert( p->iStatement==0 || db->flags&SQLITE_CountRows );
51947
- if( SQLITE_OK!=(rc = sqlite3VdbeCloseStatement(p, SAVEPOINT_RELEASE)) ){
51890
+ rc = sqlite3VdbeCloseStatement(p, SAVEPOINT_RELEASE);
51891
+ if( NEVER(rc!=SQLITE_OK) ){
5194851892
break;
5194951893
}
5195051894
5195151895
/* Invalidate all ephemeral cursor row caches */
5195251896
p->cacheCtr = (p->cacheCtr + 2)|1;
@@ -51990,13 +51934,12 @@
5199051934
assert( pIn1!=pOut );
5199151935
if( (pIn1->flags | pIn2->flags) & MEM_Null ){
5199251936
sqlite3VdbeMemSetNull(pOut);
5199351937
break;
5199451938
}
51995
- ExpandBlob(pIn1);
51939
+ if( ExpandBlob(pIn1) || ExpandBlob(pIn2) ) goto no_mem;
5199651940
Stringify(pIn1, encoding);
51997
- ExpandBlob(pIn2);
5199851941
Stringify(pIn2, encoding);
5199951942
u.ae.nByte = pIn1->n + pIn2->n;
5200051943
if( u.ae.nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
5200151944
goto too_big;
5200251945
}
@@ -52108,12 +52051,12 @@
5210852051
if( u.af.rA==(double)0 ) goto arithmetic_result_is_null;
5210952052
u.af.rB /= u.af.rA;
5211052053
break;
5211152054
}
5211252055
default: {
52113
- u.af.iA = u.af.rA;
52114
- u.af.iB = u.af.rB;
52056
+ u.af.iA = (i64)u.af.rA;
52057
+ u.af.iB = (i64)u.af.rB;
5211552058
if( u.af.iA==0 ) goto arithmetic_result_is_null;
5211652059
if( u.af.iA==-1 ) u.af.iA = 1;
5211752060
u.af.rB = (double)(u.af.iB % u.af.iA);
5211852061
break;
5211952062
}
@@ -52781,30 +52724,18 @@
5278152724
pc = pOp->p2-1;
5278252725
}
5278352726
break;
5278452727
}
5278552728
52786
-/* Opcode: IsNull P1 P2 P3 * *
52729
+/* Opcode: IsNull P1 P2 * * *
5278752730
**
52788
-** Jump to P2 if the value in register P1 is NULL. If P3 is greater
52789
-** than zero, then check all values reg(P1), reg(P1+1),
52790
-** reg(P1+2), ..., reg(P1+P3-1).
52731
+** Jump to P2 if the value in register P1 is NULL.
5279152732
*/
5279252733
case OP_IsNull: { /* same as TK_ISNULL, jump, in1 */
52793
-#if 0 /* local variables moved into u.am */
52794
- int n;
52795
-#endif /* local variables moved into u.am */
52796
-
52797
- u.am.n = pOp->p3;
52798
- assert( pOp->p3==0 || pOp->p1>0 );
52799
- do{
52800
- if( (pIn1->flags & MEM_Null)!=0 ){
52801
- pc = pOp->p2 - 1;
52802
- break;
52803
- }
52804
- pIn1++;
52805
- }while( --u.am.n > 0 );
52734
+ if( (pIn1->flags & MEM_Null)!=0 ){
52735
+ pc = pOp->p2 - 1;
52736
+ }
5280652737
break;
5280752738
}
5280852739
5280952740
/* Opcode: NotNull P1 P2 * * *
5281052741
**
@@ -52852,11 +52783,11 @@
5285252783
** If the column contains fewer than P2 fields, then extract a NULL. Or,
5285352784
** if the P4 argument is a P4_MEM use the value of the P4 argument as
5285452785
** the result.
5285552786
*/
5285652787
case OP_Column: {
52857
-#if 0 /* local variables moved into u.an */
52788
+#if 0 /* local variables moved into u.am */
5285852789
u32 payloadSize; /* Number of bytes in the record */
5285952790
i64 payloadSize64; /* Number of bytes in the record */
5286052791
int p1; /* P1 value of the opcode */
5286152792
int p2; /* column number to retrieve */
5286252793
VdbeCursor *pC; /* The VDBE cursor */
@@ -52874,123 +52805,125 @@
5287452805
u8 *zEndHdr; /* Pointer to first byte after the header */
5287552806
u32 offset; /* Offset into the data */
5287652807
u64 offset64; /* 64-bit offset. 64 bits needed to catch overflow */
5287752808
int szHdr; /* Size of the header size field at start of record */
5287852809
int avail; /* Number of bytes of available data */
52879
-#endif /* local variables moved into u.an */
52810
+#endif /* local variables moved into u.am */
5288052811
5288152812
52882
- u.an.p1 = pOp->p1;
52883
- u.an.p2 = pOp->p2;
52884
- u.an.pC = 0;
52885
- memset(&u.an.sMem, 0, sizeof(u.an.sMem));
52886
- assert( u.an.p1<p->nCursor );
52813
+ u.am.p1 = pOp->p1;
52814
+ u.am.p2 = pOp->p2;
52815
+ u.am.pC = 0;
52816
+ memset(&u.am.sMem, 0, sizeof(u.am.sMem));
52817
+ assert( u.am.p1<p->nCursor );
5288752818
assert( pOp->p3>0 && pOp->p3<=p->nMem );
52888
- u.an.pDest = &p->aMem[pOp->p3];
52889
- MemSetTypeFlag(u.an.pDest, MEM_Null);
52819
+ u.am.pDest = &p->aMem[pOp->p3];
52820
+ MemSetTypeFlag(u.am.pDest, MEM_Null);
5289052821
52891
- /* This block sets the variable u.an.payloadSize to be the total number of
52822
+ /* This block sets the variable u.am.payloadSize to be the total number of
5289252823
** bytes in the record.
5289352824
**
52894
- ** u.an.zRec is set to be the complete text of the record if it is available.
52825
+ ** u.am.zRec is set to be the complete text of the record if it is available.
5289552826
** The complete record text is always available for pseudo-tables
5289652827
** If the record is stored in a cursor, the complete record text
52897
- ** might be available in the u.an.pC->aRow cache. Or it might not be.
52898
- ** If the data is unavailable, u.an.zRec is set to NULL.
52828
+ ** might be available in the u.am.pC->aRow cache. Or it might not be.
52829
+ ** If the data is unavailable, u.am.zRec is set to NULL.
5289952830
**
5290052831
** We also compute the number of columns in the record. For cursors,
5290152832
** the number of columns is stored in the VdbeCursor.nField element.
5290252833
*/
52903
- u.an.pC = p->apCsr[u.an.p1];
52904
- assert( u.an.pC!=0 );
52834
+ u.am.pC = p->apCsr[u.am.p1];
52835
+ assert( u.am.pC!=0 );
5290552836
#ifndef SQLITE_OMIT_VIRTUALTABLE
52906
- assert( u.an.pC->pVtabCursor==0 );
52837
+ assert( u.am.pC->pVtabCursor==0 );
5290752838
#endif
52908
- if( u.an.pC->pCursor!=0 ){
52839
+ if( u.am.pC->pCursor!=0 ){
5290952840
/* The record is stored in a B-Tree */
52910
- rc = sqlite3VdbeCursorMoveto(u.an.pC);
52841
+ rc = sqlite3VdbeCursorMoveto(u.am.pC);
5291152842
if( rc ) goto abort_due_to_error;
52912
- u.an.zRec = 0;
52913
- u.an.pCrsr = u.an.pC->pCursor;
52914
- if( u.an.pC->nullRow ){
52915
- u.an.payloadSize = 0;
52916
- }else if( u.an.pC->cacheStatus==p->cacheCtr ){
52917
- u.an.payloadSize = u.an.pC->payloadSize;
52918
- u.an.zRec = (char*)u.an.pC->aRow;
52919
- }else if( u.an.pC->isIndex ){
52920
- sqlite3BtreeKeySize(u.an.pCrsr, &u.an.payloadSize64);
52921
- if( (u.an.payloadSize64 & SQLITE_MAX_U32)!=(u64)u.an.payloadSize64 ){
52922
- rc = SQLITE_CORRUPT_BKPT;
52923
- goto abort_due_to_error;
52924
- }
52925
- u.an.payloadSize = (u32)u.an.payloadSize64;
52926
- }else{
52927
- sqlite3BtreeDataSize(u.an.pCrsr, &u.an.payloadSize);
52928
- }
52929
- u.an.nField = u.an.pC->nField;
52930
- }else{
52931
- assert( u.an.pC->pseudoTable );
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;
52850
+ }else if( u.am.pC->isIndex ){
52851
+ sqlite3BtreeKeySize(u.am.pCrsr, &u.am.payloadSize64);
52852
+ /* sqlite3BtreeParseCellPtr() uses getVarint32() to extract the
52853
+ ** payload size, so it is impossible for u.am.payloadSize64 to be
52854
+ ** larger than 32 bits. */
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 ){
5293252862
/* The record is the sole entry of a pseudo-table */
52933
- u.an.payloadSize = u.an.pC->nData;
52934
- u.an.zRec = u.an.pC->pData;
52935
- u.an.pC->cacheStatus = CACHE_STALE;
52936
- assert( u.an.payloadSize==0 || u.an.zRec!=0 );
52937
- u.an.nField = u.an.pC->nField;
52938
- u.an.pCrsr = 0;
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;
5293952872
}
5294052873
52941
- /* If u.an.payloadSize is 0, then just store a NULL */
52942
- if( u.an.payloadSize==0 ){
52943
- assert( u.an.pDest->flags&MEM_Null );
52874
+ /* If u.am.payloadSize is 0, then just store a NULL */
52875
+ if( u.am.payloadSize==0 ){
52876
+ assert( u.am.pDest->flags&MEM_Null );
5294452877
goto op_column_out;
5294552878
}
5294652879
assert( db->aLimit[SQLITE_LIMIT_LENGTH]>=0 );
52947
- if( u.an.payloadSize > (u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
52880
+ if( u.am.payloadSize > (u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
5294852881
goto too_big;
5294952882
}
5295052883
52951
- assert( u.an.p2<u.an.nField );
52884
+ assert( u.am.p2<u.am.nField );
5295252885
5295352886
/* Read and parse the table header. Store the results of the parse
5295452887
** into the record header cache fields of the cursor.
5295552888
*/
52956
- u.an.aType = u.an.pC->aType;
52957
- if( u.an.pC->cacheStatus==p->cacheCtr ){
52958
- u.an.aOffset = u.an.pC->aOffset;
52889
+ u.am.aType = u.am.pC->aType;
52890
+ if( u.am.pC->cacheStatus==p->cacheCtr ){
52891
+ u.am.aOffset = u.am.pC->aOffset;
5295952892
}else{
52960
- assert(u.an.aType);
52961
- u.an.avail = 0;
52962
- u.an.pC->aOffset = u.an.aOffset = &u.an.aType[u.an.nField];
52963
- u.an.pC->payloadSize = u.an.payloadSize;
52964
- u.an.pC->cacheStatus = p->cacheCtr;
52893
+ assert(u.am.aType);
52894
+ u.am.avail = 0;
52895
+ u.am.pC->aOffset = u.am.aOffset = &u.am.aType[u.am.nField];
52896
+ u.am.pC->payloadSize = u.am.payloadSize;
52897
+ u.am.pC->cacheStatus = p->cacheCtr;
5296552898
5296652899
/* Figure out how many bytes are in the header */
52967
- if( u.an.zRec ){
52968
- u.an.zData = u.an.zRec;
52900
+ if( u.am.zRec ){
52901
+ u.am.zData = u.am.zRec;
5296952902
}else{
52970
- if( u.an.pC->isIndex ){
52971
- u.an.zData = (char*)sqlite3BtreeKeyFetch(u.an.pCrsr, &u.an.avail);
52903
+ if( u.am.pC->isIndex ){
52904
+ u.am.zData = (char*)sqlite3BtreeKeyFetch(u.am.pCrsr, &u.am.avail);
5297252905
}else{
52973
- u.an.zData = (char*)sqlite3BtreeDataFetch(u.an.pCrsr, &u.an.avail);
52906
+ u.am.zData = (char*)sqlite3BtreeDataFetch(u.am.pCrsr, &u.am.avail);
5297452907
}
5297552908
/* If KeyFetch()/DataFetch() managed to get the entire payload,
52976
- ** save the payload in the u.an.pC->aRow cache. That will save us from
52909
+ ** save the payload in the u.am.pC->aRow cache. That will save us from
5297752910
** having to make additional calls to fetch the content portion of
5297852911
** the record.
5297952912
*/
52980
- assert( u.an.avail>=0 );
52981
- if( u.an.payloadSize <= (u32)u.an.avail ){
52982
- u.an.zRec = u.an.zData;
52983
- u.an.pC->aRow = (u8*)u.an.zData;
52913
+ assert( u.am.avail>=0 );
52914
+ if( u.am.payloadSize <= (u32)u.am.avail ){
52915
+ u.am.zRec = u.am.zData;
52916
+ u.am.pC->aRow = (u8*)u.am.zData;
5298452917
}else{
52985
- u.an.pC->aRow = 0;
52918
+ u.am.pC->aRow = 0;
5298652919
}
5298752920
}
5298852921
/* The following assert is true in all cases accept when
5298952922
** the database file has been corrupted externally.
52990
- ** assert( u.an.zRec!=0 || u.an.avail>=u.an.payloadSize || u.an.avail>=9 ); */
52991
- u.an.szHdr = getVarint32((u8*)u.an.zData, u.an.offset);
52923
+ ** assert( u.am.zRec!=0 || u.am.avail>=u.am.payloadSize || u.am.avail>=9 ); */
52924
+ u.am.szHdr = getVarint32((u8*)u.am.zData, u.am.offset);
5299252925
5299352926
/* Make sure a corrupt database has not given us an oversize header.
5299452927
** Do this now to avoid an oversize memory allocation.
5299552928
**
5299652929
** Type entries can be between 1 and 5 bytes each. But 4 and 5 byte
@@ -52997,136 +52930,136 @@
5299752930
** types use so much data space that there can only be 4096 and 32 of
5299852931
** them, respectively. So the maximum header length results from a
5299952932
** 3-byte type for each of the maximum of 32768 columns plus three
5300052933
** extra bytes for the header length itself. 32768*3 + 3 = 98307.
5300152934
*/
53002
- if( u.an.offset > 98307 ){
52935
+ if( u.am.offset > 98307 ){
5300352936
rc = SQLITE_CORRUPT_BKPT;
5300452937
goto op_column_out;
5300552938
}
5300652939
53007
- /* Compute in u.an.len the number of bytes of data we need to read in order
53008
- ** to get u.an.nField type values. u.an.offset is an upper bound on this. But
53009
- ** u.an.nField might be significantly less than the true number of columns
53010
- ** in the table, and in that case, 5*u.an.nField+3 might be smaller than u.an.offset.
53011
- ** We want to minimize u.an.len in order to limit the size of the memory
53012
- ** allocation, especially if a corrupt database file has caused u.an.offset
52940
+ /* Compute in u.am.len the number of bytes of data we need to read in order
52941
+ ** to get u.am.nField type values. u.am.offset is an upper bound on this. But
52942
+ ** u.am.nField might be significantly less than the true number of columns
52943
+ ** in the table, and in that case, 5*u.am.nField+3 might be smaller than u.am.offset.
52944
+ ** We want to minimize u.am.len in order to limit the size of the memory
52945
+ ** allocation, especially if a corrupt database file has caused u.am.offset
5301352946
** to be oversized. Offset is limited to 98307 above. But 98307 might
5301452947
** still exceed Robson memory allocation limits on some configurations.
53015
- ** On systems that cannot tolerate large memory allocations, u.an.nField*5+3
53016
- ** will likely be much smaller since u.an.nField will likely be less than
52948
+ ** On systems that cannot tolerate large memory allocations, u.am.nField*5+3
52949
+ ** will likely be much smaller since u.am.nField will likely be less than
5301752950
** 20 or so. This insures that Robson memory allocation limits are
5301852951
** not exceeded even for corrupt database files.
5301952952
*/
53020
- u.an.len = u.an.nField*5 + 3;
53021
- if( u.an.len > u.an.offset ) u.an.len = u.an.offset;
52953
+ u.am.len = u.am.nField*5 + 3;
52954
+ if( u.am.len > (int)u.am.offset ) u.am.len = (int)u.am.offset;
5302252955
5302352956
/* The KeyFetch() or DataFetch() above are fast and will get the entire
5302452957
** record header in most cases. But they will fail to get the complete
5302552958
** record header if the record header does not fit on a single page
5302652959
** in the B-Tree. When that happens, use sqlite3VdbeMemFromBtree() to
5302752960
** acquire the complete header text.
5302852961
*/
53029
- if( !u.an.zRec && u.an.avail<u.an.len ){
53030
- u.an.sMem.flags = 0;
53031
- u.an.sMem.db = 0;
53032
- rc = sqlite3VdbeMemFromBtree(u.an.pCrsr, 0, u.an.len, u.an.pC->isIndex, &u.an.sMem);
52962
+ if( !u.am.zRec && u.am.avail<u.am.len ){
52963
+ u.am.sMem.flags = 0;
52964
+ u.am.sMem.db = 0;
52965
+ rc = sqlite3VdbeMemFromBtree(u.am.pCrsr, 0, u.am.len, u.am.pC->isIndex, &u.am.sMem);
5303352966
if( rc!=SQLITE_OK ){
5303452967
goto op_column_out;
5303552968
}
53036
- u.an.zData = u.an.sMem.z;
53037
- }
53038
- u.an.zEndHdr = (u8 *)&u.an.zData[u.an.len];
53039
- u.an.zIdx = (u8 *)&u.an.zData[u.an.szHdr];
53040
-
53041
- /* Scan the header and use it to fill in the u.an.aType[] and u.an.aOffset[]
53042
- ** arrays. u.an.aType[u.an.i] will contain the type integer for the u.an.i-th
53043
- ** column and u.an.aOffset[u.an.i] will contain the u.an.offset from the beginning
53044
- ** of the record to the start of the data for the u.an.i-th column
53045
- */
53046
- u.an.offset64 = u.an.offset;
53047
- for(u.an.i=0; u.an.i<u.an.nField; u.an.i++){
53048
- if( u.an.zIdx<u.an.zEndHdr ){
53049
- u.an.aOffset[u.an.i] = (u32)u.an.offset64;
53050
- u.an.zIdx += getVarint32(u.an.zIdx, u.an.aType[u.an.i]);
53051
- u.an.offset64 += sqlite3VdbeSerialTypeLen(u.an.aType[u.an.i]);
53052
- }else{
53053
- /* If u.an.i is less that u.an.nField, then there are less fields in this
52969
+ u.am.zData = u.am.sMem.z;
52970
+ }
52971
+ u.am.zEndHdr = (u8 *)&u.am.zData[u.am.len];
52972
+ u.am.zIdx = (u8 *)&u.am.zData[u.am.szHdr];
52973
+
52974
+ /* Scan the header and use it to fill in the u.am.aType[] and u.am.aOffset[]
52975
+ ** arrays. u.am.aType[u.am.i] will contain the type integer for the u.am.i-th
52976
+ ** column and u.am.aOffset[u.am.i] will contain the u.am.offset from the beginning
52977
+ ** of the record to the start of the data for the u.am.i-th column
52978
+ */
52979
+ u.am.offset64 = u.am.offset;
52980
+ for(u.am.i=0; u.am.i<u.am.nField; u.am.i++){
52981
+ if( u.am.zIdx<u.am.zEndHdr ){
52982
+ u.am.aOffset[u.am.i] = (u32)u.am.offset64;
52983
+ u.am.zIdx += getVarint32(u.am.zIdx, u.am.aType[u.am.i]);
52984
+ u.am.offset64 += sqlite3VdbeSerialTypeLen(u.am.aType[u.am.i]);
52985
+ }else{
52986
+ /* If u.am.i is less that u.am.nField, then there are less fields in this
5305452987
** record than SetNumColumns indicated there are columns in the
53055
- ** table. Set the u.an.offset for any extra columns not present in
52988
+ ** table. Set the u.am.offset for any extra columns not present in
5305652989
** the record to 0. This tells code below to store a NULL
5305752990
** instead of deserializing a value from the record.
5305852991
*/
53059
- u.an.aOffset[u.an.i] = 0;
52992
+ u.am.aOffset[u.am.i] = 0;
5306052993
}
5306152994
}
53062
- sqlite3VdbeMemRelease(&u.an.sMem);
53063
- u.an.sMem.flags = MEM_Null;
52995
+ sqlite3VdbeMemRelease(&u.am.sMem);
52996
+ u.am.sMem.flags = MEM_Null;
5306452997
5306552998
/* If we have read more header data than was contained in the header,
5306652999
** or if the end of the last field appears to be past the end of the
5306753000
** record, or if the end of the last field appears to be before the end
5306853001
** of the record (when all fields present), then we must be dealing
5306953002
** with a corrupt database.
5307053003
*/
53071
- if( (u.an.zIdx > u.an.zEndHdr)|| (u.an.offset64 > u.an.payloadSize)
53072
- || (u.an.zIdx==u.an.zEndHdr && u.an.offset64!=(u64)u.an.payloadSize) ){
53004
+ if( (u.am.zIdx > u.am.zEndHdr)|| (u.am.offset64 > u.am.payloadSize)
53005
+ || (u.am.zIdx==u.am.zEndHdr && u.am.offset64!=(u64)u.am.payloadSize) ){
5307353006
rc = SQLITE_CORRUPT_BKPT;
5307453007
goto op_column_out;
5307553008
}
5307653009
}
5307753010
53078
- /* Get the column information. If u.an.aOffset[u.an.p2] is non-zero, then
53079
- ** deserialize the value from the record. If u.an.aOffset[u.an.p2] is zero,
53011
+ /* Get the column information. If u.am.aOffset[u.am.p2] is non-zero, then
53012
+ ** deserialize the value from the record. If u.am.aOffset[u.am.p2] is zero,
5308053013
** then there are not enough fields in the record to satisfy the
5308153014
** request. In this case, set the value NULL or to P4 if P4 is
5308253015
** a pointer to a Mem object.
5308353016
*/
53084
- if( u.an.aOffset[u.an.p2] ){
53017
+ if( u.am.aOffset[u.am.p2] ){
5308553018
assert( rc==SQLITE_OK );
53086
- if( u.an.zRec ){
53087
- sqlite3VdbeMemReleaseExternal(u.an.pDest);
53088
- sqlite3VdbeSerialGet((u8 *)&u.an.zRec[u.an.aOffset[u.an.p2]], u.an.aType[u.an.p2], u.an.pDest);
53019
+ if( u.am.zRec ){
53020
+ sqlite3VdbeMemReleaseExternal(u.am.pDest);
53021
+ sqlite3VdbeSerialGet((u8 *)&u.am.zRec[u.am.aOffset[u.am.p2]], u.am.aType[u.am.p2], u.am.pDest);
5308953022
}else{
53090
- u.an.len = sqlite3VdbeSerialTypeLen(u.an.aType[u.an.p2]);
53091
- sqlite3VdbeMemMove(&u.an.sMem, u.an.pDest);
53092
- rc = sqlite3VdbeMemFromBtree(u.an.pCrsr, u.an.aOffset[u.an.p2], u.an.len, u.an.pC->isIndex, &u.an.sMem);
53023
+ u.am.len = sqlite3VdbeSerialTypeLen(u.am.aType[u.am.p2]);
53024
+ sqlite3VdbeMemMove(&u.am.sMem, u.am.pDest);
53025
+ rc = sqlite3VdbeMemFromBtree(u.am.pCrsr, u.am.aOffset[u.am.p2], u.am.len, u.am.pC->isIndex, &u.am.sMem);
5309353026
if( rc!=SQLITE_OK ){
5309453027
goto op_column_out;
5309553028
}
53096
- u.an.zData = u.an.sMem.z;
53097
- sqlite3VdbeSerialGet((u8*)u.an.zData, u.an.aType[u.an.p2], u.an.pDest);
53029
+ u.am.zData = u.am.sMem.z;
53030
+ sqlite3VdbeSerialGet((u8*)u.am.zData, u.am.aType[u.am.p2], u.am.pDest);
5309853031
}
53099
- u.an.pDest->enc = encoding;
53032
+ u.am.pDest->enc = encoding;
5310053033
}else{
5310153034
if( pOp->p4type==P4_MEM ){
53102
- sqlite3VdbeMemShallowCopy(u.an.pDest, pOp->p4.pMem, MEM_Static);
53035
+ sqlite3VdbeMemShallowCopy(u.am.pDest, pOp->p4.pMem, MEM_Static);
5310353036
}else{
53104
- assert( u.an.pDest->flags&MEM_Null );
53037
+ assert( u.am.pDest->flags&MEM_Null );
5310553038
}
5310653039
}
5310753040
5310853041
/* If we dynamically allocated space to hold the data (in the
5310953042
** sqlite3VdbeMemFromBtree() call above) then transfer control of that
53110
- ** dynamically allocated space over to the u.an.pDest structure.
53043
+ ** dynamically allocated space over to the u.am.pDest structure.
5311153044
** This prevents a memory copy.
5311253045
*/
53113
- if( u.an.sMem.zMalloc ){
53114
- assert( u.an.sMem.z==u.an.sMem.zMalloc );
53115
- assert( !(u.an.pDest->flags & MEM_Dyn) );
53116
- assert( !(u.an.pDest->flags & (MEM_Blob|MEM_Str)) || u.an.pDest->z==u.an.sMem.z );
53117
- u.an.pDest->flags &= ~(MEM_Ephem|MEM_Static);
53118
- u.an.pDest->flags |= MEM_Term;
53119
- u.an.pDest->z = u.an.sMem.z;
53120
- u.an.pDest->zMalloc = u.an.sMem.zMalloc;
53046
+ if( u.am.sMem.zMalloc ){
53047
+ assert( u.am.sMem.z==u.am.sMem.zMalloc );
53048
+ assert( !(u.am.pDest->flags & MEM_Dyn) );
53049
+ assert( !(u.am.pDest->flags & (MEM_Blob|MEM_Str)) || u.am.pDest->z==u.am.sMem.z );
53050
+ u.am.pDest->flags &= ~(MEM_Ephem|MEM_Static);
53051
+ u.am.pDest->flags |= MEM_Term;
53052
+ u.am.pDest->z = u.am.sMem.z;
53053
+ u.am.pDest->zMalloc = u.am.sMem.zMalloc;
5312153054
}
5312253055
53123
- rc = sqlite3VdbeMemMakeWriteable(u.an.pDest);
53056
+ rc = sqlite3VdbeMemMakeWriteable(u.am.pDest);
5312453057
5312553058
op_column_out:
53126
- UPDATE_MAX_BLOBSIZE(u.an.pDest);
53127
- REGISTER_TRACE(pOp->p3, u.an.pDest);
53059
+ UPDATE_MAX_BLOBSIZE(u.am.pDest);
53060
+ REGISTER_TRACE(pOp->p3, u.am.pDest);
5312853061
break;
5312953062
}
5313053063
5313153064
/* Opcode: Affinity P1 P2 * P4 *
5313253065
**
@@ -53135,23 +53068,23 @@
5313553068
** P4 is a string that is P2 characters long. The nth character of the
5313653069
** string indicates the column affinity that should be used for the nth
5313753070
** memory cell in the range.
5313853071
*/
5313953072
case OP_Affinity: {
53140
-#if 0 /* local variables moved into u.ao */
53073
+#if 0 /* local variables moved into u.an */
5314153074
char *zAffinity; /* The affinity to be applied */
5314253075
Mem *pData0; /* First register to which to apply affinity */
5314353076
Mem *pLast; /* Last register to which to apply affinity */
5314453077
Mem *pRec; /* Current register */
53145
-#endif /* local variables moved into u.ao */
53146
-
53147
- u.ao.zAffinity = pOp->p4.z;
53148
- u.ao.pData0 = &p->aMem[pOp->p1];
53149
- u.ao.pLast = &u.ao.pData0[pOp->p2-1];
53150
- for(u.ao.pRec=u.ao.pData0; u.ao.pRec<=u.ao.pLast; u.ao.pRec++){
53151
- ExpandBlob(u.ao.pRec);
53152
- applyAffinity(u.ao.pRec, u.ao.zAffinity[u.ao.pRec-u.ao.pData0], encoding);
53078
+#endif /* local variables moved into u.an */
53079
+
53080
+ u.an.zAffinity = pOp->p4.z;
53081
+ u.an.pData0 = &p->aMem[pOp->p1];
53082
+ u.an.pLast = &u.an.pData0[pOp->p2-1];
53083
+ for(u.an.pRec=u.an.pData0; u.an.pRec<=u.an.pLast; u.an.pRec++){
53084
+ ExpandBlob(u.an.pRec);
53085
+ applyAffinity(u.an.pRec, u.an.zAffinity[u.an.pRec-u.an.pData0], encoding);
5315353086
}
5315453087
break;
5315553088
}
5315653089
5315753090
/* Opcode: MakeRecord P1 P2 P3 P4 *
@@ -53171,11 +53104,11 @@
5317153104
** macros defined in sqliteInt.h.
5317253105
**
5317353106
** If P4 is NULL then all index fields have the affinity NONE.
5317453107
*/
5317553108
case OP_MakeRecord: {
53176
-#if 0 /* local variables moved into u.ap */
53109
+#if 0 /* local variables moved into u.ao */
5317753110
u8 *zNewRecord; /* A buffer to hold the data for the new record */
5317853111
Mem *pRec; /* The new record */
5317953112
u64 nData; /* Number of bytes of data space */
5318053113
int nHdr; /* Number of bytes of header space */
5318153114
i64 nByte; /* Data space required for this record */
@@ -53187,11 +53120,11 @@
5318753120
int nField; /* Number of fields in the record */
5318853121
char *zAffinity; /* The affinity string for the record */
5318953122
int file_format; /* File format to use for encoding */
5319053123
int i; /* Space used in zNewRecord[] */
5319153124
int len; /* Length of a field */
53192
-#endif /* local variables moved into u.ap */
53125
+#endif /* local variables moved into u.ao */
5319353126
5319453127
/* Assuming the record contains N fields, the record format looks
5319553128
** like this:
5319653129
**
5319753130
** ------------------------------------------------------------------------
@@ -53204,52 +53137,52 @@
5320453137
** Each type field is a varint representing the serial type of the
5320553138
** corresponding data element (see sqlite3VdbeSerialType()). The
5320653139
** hdr-size field is also a varint which is the offset from the beginning
5320753140
** of the record to data0.
5320853141
*/
53209
- u.ap.nData = 0; /* Number of bytes of data space */
53210
- u.ap.nHdr = 0; /* Number of bytes of header space */
53211
- u.ap.nByte = 0; /* Data space required for this record */
53212
- u.ap.nZero = 0; /* Number of zero bytes at the end of the record */
53213
- u.ap.nField = pOp->p1;
53214
- u.ap.zAffinity = pOp->p4.z;
53215
- assert( u.ap.nField>0 && pOp->p2>0 && pOp->p2+u.ap.nField<=p->nMem+1 );
53216
- u.ap.pData0 = &p->aMem[u.ap.nField];
53217
- u.ap.nField = pOp->p2;
53218
- u.ap.pLast = &u.ap.pData0[u.ap.nField-1];
53219
- u.ap.file_format = p->minWriteFileFormat;
53142
+ u.ao.nData = 0; /* Number of bytes of data space */
53143
+ u.ao.nHdr = 0; /* Number of bytes of header space */
53144
+ u.ao.nByte = 0; /* Data space required for this record */
53145
+ u.ao.nZero = 0; /* Number of zero bytes at the end of the record */
53146
+ u.ao.nField = pOp->p1;
53147
+ u.ao.zAffinity = pOp->p4.z;
53148
+ assert( u.ao.nField>0 && pOp->p2>0 && pOp->p2+u.ao.nField<=p->nMem+1 );
53149
+ u.ao.pData0 = &p->aMem[u.ao.nField];
53150
+ u.ao.nField = pOp->p2;
53151
+ u.ao.pLast = &u.ao.pData0[u.ao.nField-1];
53152
+ u.ao.file_format = p->minWriteFileFormat;
5322053153
5322153154
/* Loop through the elements that will make up the record to figure
5322253155
** out how much space is required for the new record.
5322353156
*/
53224
- for(u.ap.pRec=u.ap.pData0; u.ap.pRec<=u.ap.pLast; u.ap.pRec++){
53225
- if( u.ap.zAffinity ){
53226
- applyAffinity(u.ap.pRec, u.ap.zAffinity[u.ap.pRec-u.ap.pData0], encoding);
53227
- }
53228
- if( u.ap.pRec->flags&MEM_Zero && u.ap.pRec->n>0 ){
53229
- sqlite3VdbeMemExpandBlob(u.ap.pRec);
53230
- }
53231
- u.ap.serial_type = sqlite3VdbeSerialType(u.ap.pRec, u.ap.file_format);
53232
- u.ap.len = sqlite3VdbeSerialTypeLen(u.ap.serial_type);
53233
- u.ap.nData += u.ap.len;
53234
- u.ap.nHdr += sqlite3VarintLen(u.ap.serial_type);
53235
- if( u.ap.pRec->flags & MEM_Zero ){
53157
+ for(u.ao.pRec=u.ao.pData0; u.ao.pRec<=u.ao.pLast; u.ao.pRec++){
53158
+ if( u.ao.zAffinity ){
53159
+ applyAffinity(u.ao.pRec, u.ao.zAffinity[u.ao.pRec-u.ao.pData0], encoding);
53160
+ }
53161
+ if( u.ao.pRec->flags&MEM_Zero && u.ao.pRec->n>0 ){
53162
+ sqlite3VdbeMemExpandBlob(u.ao.pRec);
53163
+ }
53164
+ u.ao.serial_type = sqlite3VdbeSerialType(u.ao.pRec, u.ao.file_format);
53165
+ u.ao.len = sqlite3VdbeSerialTypeLen(u.ao.serial_type);
53166
+ u.ao.nData += u.ao.len;
53167
+ u.ao.nHdr += sqlite3VarintLen(u.ao.serial_type);
53168
+ if( u.ao.pRec->flags & MEM_Zero ){
5323653169
/* Only pure zero-filled BLOBs can be input to this Opcode.
5323753170
** We do not allow blobs with a prefix and a zero-filled tail. */
53238
- u.ap.nZero += u.ap.pRec->u.nZero;
53239
- }else if( u.ap.len ){
53240
- u.ap.nZero = 0;
53171
+ u.ao.nZero += u.ao.pRec->u.nZero;
53172
+ }else if( u.ao.len ){
53173
+ u.ao.nZero = 0;
5324153174
}
5324253175
}
5324353176
5324453177
/* Add the initial header varint and total the size */
53245
- u.ap.nHdr += u.ap.nVarint = sqlite3VarintLen(u.ap.nHdr);
53246
- if( u.ap.nVarint<sqlite3VarintLen(u.ap.nHdr) ){
53247
- u.ap.nHdr++;
53178
+ u.ao.nHdr += u.ao.nVarint = sqlite3VarintLen(u.ao.nHdr);
53179
+ if( u.ao.nVarint<sqlite3VarintLen(u.ao.nHdr) ){
53180
+ u.ao.nHdr++;
5324853181
}
53249
- u.ap.nByte = u.ap.nHdr+u.ap.nData-u.ap.nZero;
53250
- if( u.ap.nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
53182
+ u.ao.nByte = u.ao.nHdr+u.ao.nData-u.ao.nZero;
53183
+ if( u.ao.nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
5325153184
goto too_big;
5325253185
}
5325353186
5325453187
/* Make sure the output register has a buffer large enough to store
5325553188
** the new record. The output register (pOp->p3) is not allowed to
@@ -53256,32 +53189,32 @@
5325653189
** be one of the input registers (because the following call to
5325753190
** sqlite3VdbeMemGrow() could clobber the value before it is used).
5325853191
*/
5325953192
assert( pOp->p3<pOp->p1 || pOp->p3>=pOp->p1+pOp->p2 );
5326053193
pOut = &p->aMem[pOp->p3];
53261
- if( sqlite3VdbeMemGrow(pOut, (int)u.ap.nByte, 0) ){
53194
+ if( sqlite3VdbeMemGrow(pOut, (int)u.ao.nByte, 0) ){
5326253195
goto no_mem;
5326353196
}
53264
- u.ap.zNewRecord = (u8 *)pOut->z;
53197
+ u.ao.zNewRecord = (u8 *)pOut->z;
5326553198
5326653199
/* Write the record */
53267
- u.ap.i = putVarint32(u.ap.zNewRecord, u.ap.nHdr);
53268
- for(u.ap.pRec=u.ap.pData0; u.ap.pRec<=u.ap.pLast; u.ap.pRec++){
53269
- u.ap.serial_type = sqlite3VdbeSerialType(u.ap.pRec, u.ap.file_format);
53270
- u.ap.i += putVarint32(&u.ap.zNewRecord[u.ap.i], u.ap.serial_type); /* serial type */
53271
- }
53272
- for(u.ap.pRec=u.ap.pData0; u.ap.pRec<=u.ap.pLast; u.ap.pRec++){ /* serial data */
53273
- u.ap.i += sqlite3VdbeSerialPut(&u.ap.zNewRecord[u.ap.i], (int)(u.ap.nByte-u.ap.i), u.ap.pRec,u.ap.file_format);
53274
- }
53275
- assert( u.ap.i==u.ap.nByte );
53200
+ u.ao.i = putVarint32(u.ao.zNewRecord, u.ao.nHdr);
53201
+ for(u.ao.pRec=u.ao.pData0; u.ao.pRec<=u.ao.pLast; u.ao.pRec++){
53202
+ u.ao.serial_type = sqlite3VdbeSerialType(u.ao.pRec, u.ao.file_format);
53203
+ u.ao.i += putVarint32(&u.ao.zNewRecord[u.ao.i], u.ao.serial_type); /* serial type */
53204
+ }
53205
+ for(u.ao.pRec=u.ao.pData0; u.ao.pRec<=u.ao.pLast; u.ao.pRec++){ /* serial data */
53206
+ u.ao.i += sqlite3VdbeSerialPut(&u.ao.zNewRecord[u.ao.i], (int)(u.ao.nByte-u.ao.i), u.ao.pRec,u.ao.file_format);
53207
+ }
53208
+ assert( u.ao.i==u.ao.nByte );
5327653209
5327753210
assert( pOp->p3>0 && pOp->p3<=p->nMem );
53278
- pOut->n = (int)u.ap.nByte;
53211
+ pOut->n = (int)u.ao.nByte;
5327953212
pOut->flags = MEM_Blob | MEM_Dyn;
5328053213
pOut->xDel = 0;
53281
- if( u.ap.nZero ){
53282
- pOut->u.nZero = u.ap.nZero;
53214
+ if( u.ao.nZero ){
53215
+ pOut->u.nZero = u.ao.nZero;
5328353216
pOut->flags |= MEM_Zero;
5328453217
}
5328553218
pOut->enc = SQLITE_UTF8; /* In case the blob is ever converted to text */
5328653219
REGISTER_TRACE(pOp->p3, pOut);
5328753220
UPDATE_MAX_BLOBSIZE(pOut);
@@ -53293,23 +53226,23 @@
5329353226
** Store the number of entries (an integer value) in the table or index
5329453227
** opened by cursor P1 in register P2
5329553228
*/
5329653229
#ifndef SQLITE_OMIT_BTREECOUNT
5329753230
case OP_Count: { /* out2-prerelease */
53298
-#if 0 /* local variables moved into u.aq */
53231
+#if 0 /* local variables moved into u.ap */
5329953232
i64 nEntry;
5330053233
BtCursor *pCrsr;
53301
-#endif /* local variables moved into u.aq */
53234
+#endif /* local variables moved into u.ap */
5330253235
53303
- u.aq.pCrsr = p->apCsr[pOp->p1]->pCursor;
53304
- if( u.aq.pCrsr ){
53305
- rc = sqlite3BtreeCount(u.aq.pCrsr, &u.aq.nEntry);
53236
+ u.ap.pCrsr = p->apCsr[pOp->p1]->pCursor;
53237
+ if( u.ap.pCrsr ){
53238
+ rc = sqlite3BtreeCount(u.ap.pCrsr, &u.ap.nEntry);
5330653239
}else{
53307
- u.aq.nEntry = 0;
53240
+ u.ap.nEntry = 0;
5330853241
}
5330953242
pOut->flags = MEM_Int;
53310
- pOut->u.i = u.aq.nEntry;
53243
+ pOut->u.i = u.ap.nEntry;
5331153244
break;
5331253245
}
5331353246
#endif
5331453247
5331553248
/* Opcode: Statement P1 * * * *
@@ -53333,27 +53266,25 @@
5333353266
** The statement is begun on the database file with index P1. The main
5333453267
** database file has an index of 0 and the file used for temporary tables
5333553268
** has an index of 1.
5333653269
*/
5333753270
case OP_Statement: {
53338
-#if 0 /* local variables moved into u.ar */
53339
- int i;
53271
+#if 0 /* local variables moved into u.aq */
5334053272
Btree *pBt;
53341
-#endif /* local variables moved into u.ar */
53273
+#endif /* local variables moved into u.aq */
5334253274
if( db->autoCommit==0 || db->activeVdbeCnt>1 ){
53343
- u.ar.i = pOp->p1;
53344
- assert( u.ar.i>=0 && u.ar.i<db->nDb );
53345
- assert( db->aDb[u.ar.i].pBt!=0 );
53346
- u.ar.pBt = db->aDb[u.ar.i].pBt;
53347
- assert( sqlite3BtreeIsInTrans(u.ar.pBt) );
53348
- assert( (p->btreeMask & (1<<u.ar.i))!=0 );
53275
+ assert( pOp->p1>=0 && pOp->p1<db->nDb );
53276
+ assert( db->aDb[pOp->p1].pBt!=0 );
53277
+ u.aq.pBt = db->aDb[pOp->p1].pBt;
53278
+ assert( sqlite3BtreeIsInTrans(u.aq.pBt) );
53279
+ assert( (p->btreeMask & (1<<pOp->p1))!=0 );
5334953280
if( p->iStatement==0 ){
5335053281
assert( db->nStatement>=0 && db->nSavepoint>=0 );
5335153282
db->nStatement++;
5335253283
p->iStatement = db->nSavepoint + db->nStatement;
5335353284
}
53354
- rc = sqlite3BtreeBeginStmt(u.ar.pBt, p->iStatement);
53285
+ rc = sqlite3BtreeBeginStmt(u.aq.pBt, p->iStatement);
5335553286
}
5335653287
break;
5335753288
}
5335853289
5335953290
/* Opcode: Savepoint P1 * * P4 *
@@ -53361,48 +53292,48 @@
5336153292
** Open, release or rollback the savepoint named by parameter P4, depending
5336253293
** on the value of P1. To open a new savepoint, P1==0. To release (commit) an
5336353294
** existing savepoint, P1==1, or to rollback an existing savepoint P1==2.
5336453295
*/
5336553296
case OP_Savepoint: {
53366
-#if 0 /* local variables moved into u.as */
53297
+#if 0 /* local variables moved into u.ar */
5336753298
int p1; /* Value of P1 operand */
5336853299
char *zName; /* Name of savepoint */
5336953300
int nName;
5337053301
Savepoint *pNew;
5337153302
Savepoint *pSavepoint;
5337253303
Savepoint *pTmp;
5337353304
int iSavepoint;
5337453305
int ii;
53375
-#endif /* local variables moved into u.as */
53306
+#endif /* local variables moved into u.ar */
5337653307
53377
- u.as.p1 = pOp->p1;
53378
- u.as.zName = pOp->p4.z;
53308
+ u.ar.p1 = pOp->p1;
53309
+ u.ar.zName = pOp->p4.z;
5337953310
53380
- /* Assert that the u.as.p1 parameter is valid. Also that if there is no open
53311
+ /* Assert that the u.ar.p1 parameter is valid. Also that if there is no open
5338153312
** transaction, then there cannot be any savepoints.
5338253313
*/
5338353314
assert( db->pSavepoint==0 || db->autoCommit==0 );
53384
- assert( u.as.p1==SAVEPOINT_BEGIN||u.as.p1==SAVEPOINT_RELEASE||u.as.p1==SAVEPOINT_ROLLBACK );
53315
+ assert( u.ar.p1==SAVEPOINT_BEGIN||u.ar.p1==SAVEPOINT_RELEASE||u.ar.p1==SAVEPOINT_ROLLBACK );
5338553316
assert( db->pSavepoint || db->isTransactionSavepoint==0 );
5338653317
assert( checkSavepointCount(db) );
5338753318
53388
- if( u.as.p1==SAVEPOINT_BEGIN ){
53319
+ if( u.ar.p1==SAVEPOINT_BEGIN ){
5338953320
if( db->writeVdbeCnt>0 ){
5339053321
/* A new savepoint cannot be created if there are active write
5339153322
** statements (i.e. open read/write incremental blob handles).
5339253323
*/
5339353324
sqlite3SetString(&p->zErrMsg, db, "cannot open savepoint - "
5339453325
"SQL statements in progress");
5339553326
rc = SQLITE_BUSY;
5339653327
}else{
53397
- u.as.nName = sqlite3Strlen30(u.as.zName);
53328
+ u.ar.nName = sqlite3Strlen30(u.ar.zName);
5339853329
5339953330
/* Create a new savepoint structure. */
53400
- u.as.pNew = sqlite3DbMallocRaw(db, sizeof(Savepoint)+u.as.nName+1);
53401
- if( u.as.pNew ){
53402
- u.as.pNew->zName = (char *)&u.as.pNew[1];
53403
- memcpy(u.as.pNew->zName, u.as.zName, u.as.nName+1);
53331
+ u.ar.pNew = sqlite3DbMallocRaw(db, sizeof(Savepoint)+u.ar.nName+1);
53332
+ if( u.ar.pNew ){
53333
+ u.ar.pNew->zName = (char *)&u.ar.pNew[1];
53334
+ memcpy(u.ar.pNew->zName, u.ar.zName, u.ar.nName+1);
5340453335
5340553336
/* If there is no open transaction, then mark this as a special
5340653337
** "transaction savepoint". */
5340753338
if( db->autoCommit ){
5340853339
db->autoCommit = 0;
@@ -53410,49 +53341,49 @@
5341053341
}else{
5341153342
db->nSavepoint++;
5341253343
}
5341353344
5341453345
/* Link the new savepoint into the database handle's list. */
53415
- u.as.pNew->pNext = db->pSavepoint;
53416
- db->pSavepoint = u.as.pNew;
53346
+ u.ar.pNew->pNext = db->pSavepoint;
53347
+ db->pSavepoint = u.ar.pNew;
5341753348
}
5341853349
}
5341953350
}else{
53420
- u.as.iSavepoint = 0;
53351
+ u.ar.iSavepoint = 0;
5342153352
5342253353
/* Find the named savepoint. If there is no such savepoint, then an
5342353354
** an error is returned to the user. */
5342453355
for(
53425
- u.as.pSavepoint = db->pSavepoint;
53426
- u.as.pSavepoint && sqlite3StrICmp(u.as.pSavepoint->zName, u.as.zName);
53427
- u.as.pSavepoint = u.as.pSavepoint->pNext
53356
+ u.ar.pSavepoint = db->pSavepoint;
53357
+ u.ar.pSavepoint && sqlite3StrICmp(u.ar.pSavepoint->zName, u.ar.zName);
53358
+ u.ar.pSavepoint = u.ar.pSavepoint->pNext
5342853359
){
53429
- u.as.iSavepoint++;
53360
+ u.ar.iSavepoint++;
5343053361
}
53431
- if( !u.as.pSavepoint ){
53432
- sqlite3SetString(&p->zErrMsg, db, "no such savepoint: %s", u.as.zName);
53362
+ if( !u.ar.pSavepoint ){
53363
+ sqlite3SetString(&p->zErrMsg, db, "no such savepoint: %s", u.ar.zName);
5343353364
rc = SQLITE_ERROR;
5343453365
}else if(
53435
- db->writeVdbeCnt>0 || (u.as.p1==SAVEPOINT_ROLLBACK && db->activeVdbeCnt>1)
53366
+ db->writeVdbeCnt>0 || (u.ar.p1==SAVEPOINT_ROLLBACK && db->activeVdbeCnt>1)
5343653367
){
5343753368
/* It is not possible to release (commit) a savepoint if there are
5343853369
** active write statements. It is not possible to rollback a savepoint
5343953370
** if there are any active statements at all.
5344053371
*/
5344153372
sqlite3SetString(&p->zErrMsg, db,
5344253373
"cannot %s savepoint - SQL statements in progress",
53443
- (u.as.p1==SAVEPOINT_ROLLBACK ? "rollback": "release")
53374
+ (u.ar.p1==SAVEPOINT_ROLLBACK ? "rollback": "release")
5344453375
);
5344553376
rc = SQLITE_BUSY;
5344653377
}else{
5344753378
5344853379
/* Determine whether or not this is a transaction savepoint. If so,
5344953380
** and this is a RELEASE command, then the current transaction
5345053381
** is committed.
5345153382
*/
53452
- int isTransaction = u.as.pSavepoint->pNext==0 && db->isTransactionSavepoint;
53453
- if( isTransaction && u.as.p1==SAVEPOINT_RELEASE ){
53383
+ int isTransaction = u.ar.pSavepoint->pNext==0 && db->isTransactionSavepoint;
53384
+ if( isTransaction && u.ar.p1==SAVEPOINT_RELEASE ){
5345453385
db->autoCommit = 1;
5345553386
if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
5345653387
p->pc = pc;
5345753388
db->autoCommit = 0;
5345853389
p->rc = rc = SQLITE_BUSY;
@@ -53459,37 +53390,37 @@
5345953390
goto vdbe_return;
5346053391
}
5346153392
db->isTransactionSavepoint = 0;
5346253393
rc = p->rc;
5346353394
}else{
53464
- u.as.iSavepoint = db->nSavepoint - u.as.iSavepoint - 1;
53465
- for(u.as.ii=0; u.as.ii<db->nDb; u.as.ii++){
53466
- rc = sqlite3BtreeSavepoint(db->aDb[u.as.ii].pBt, u.as.p1, u.as.iSavepoint);
53395
+ u.ar.iSavepoint = db->nSavepoint - u.ar.iSavepoint - 1;
53396
+ for(u.ar.ii=0; u.ar.ii<db->nDb; u.ar.ii++){
53397
+ rc = sqlite3BtreeSavepoint(db->aDb[u.ar.ii].pBt, u.ar.p1, u.ar.iSavepoint);
5346753398
if( rc!=SQLITE_OK ){
5346853399
goto abort_due_to_error;
5346953400
}
5347053401
}
53471
- if( u.as.p1==SAVEPOINT_ROLLBACK && (db->flags&SQLITE_InternChanges)!=0 ){
53402
+ if( u.ar.p1==SAVEPOINT_ROLLBACK && (db->flags&SQLITE_InternChanges)!=0 ){
5347253403
sqlite3ExpirePreparedStatements(db);
5347353404
sqlite3ResetInternalSchema(db, 0);
5347453405
}
5347553406
}
5347653407
5347753408
/* Regardless of whether this is a RELEASE or ROLLBACK, destroy all
5347853409
** savepoints nested inside of the savepoint being operated on. */
53479
- while( db->pSavepoint!=u.as.pSavepoint ){
53480
- u.as.pTmp = db->pSavepoint;
53481
- db->pSavepoint = u.as.pTmp->pNext;
53482
- sqlite3DbFree(db, u.as.pTmp);
53410
+ while( db->pSavepoint!=u.ar.pSavepoint ){
53411
+ u.ar.pTmp = db->pSavepoint;
53412
+ db->pSavepoint = u.ar.pTmp->pNext;
53413
+ sqlite3DbFree(db, u.ar.pTmp);
5348353414
db->nSavepoint--;
5348453415
}
5348553416
5348653417
/* If it is a RELEASE, then destroy the savepoint being operated on too */
53487
- if( u.as.p1==SAVEPOINT_RELEASE ){
53488
- assert( u.as.pSavepoint==db->pSavepoint );
53489
- db->pSavepoint = u.as.pSavepoint->pNext;
53490
- sqlite3DbFree(db, u.as.pSavepoint);
53418
+ if( u.ar.p1==SAVEPOINT_RELEASE ){
53419
+ assert( u.ar.pSavepoint==db->pSavepoint );
53420
+ db->pSavepoint = u.ar.pSavepoint->pNext;
53421
+ sqlite3DbFree(db, u.ar.pSavepoint);
5349153422
if( !isTransaction ){
5349253423
db->nSavepoint--;
5349353424
}
5349453425
}
5349553426
}
@@ -53506,48 +53437,48 @@
5350653437
** there are active writing VMs or active VMs that use shared cache.
5350753438
**
5350853439
** This instruction causes the VM to halt.
5350953440
*/
5351053441
case OP_AutoCommit: {
53511
-#if 0 /* local variables moved into u.at */
53442
+#if 0 /* local variables moved into u.as */
5351253443
int desiredAutoCommit;
53513
- int rollback;
53444
+ int iRollback;
5351453445
int turnOnAC;
53515
-#endif /* local variables moved into u.at */
53446
+#endif /* local variables moved into u.as */
5351653447
53517
- u.at.desiredAutoCommit = pOp->p1;
53518
- u.at.rollback = pOp->p2;
53519
- u.at.turnOnAC = u.at.desiredAutoCommit && !db->autoCommit;
53520
- assert( u.at.desiredAutoCommit==1 || u.at.desiredAutoCommit==0 );
53521
- assert( u.at.desiredAutoCommit==1 || u.at.rollback==0 );
53448
+ u.as.desiredAutoCommit = pOp->p1;
53449
+ u.as.iRollback = pOp->p2;
53450
+ u.as.turnOnAC = u.as.desiredAutoCommit && !db->autoCommit;
53451
+ assert( u.as.desiredAutoCommit==1 || u.as.desiredAutoCommit==0 );
53452
+ assert( u.as.desiredAutoCommit==1 || u.as.iRollback==0 );
5352253453
assert( db->activeVdbeCnt>0 ); /* At least this one VM is active */
5352353454
53524
- if( u.at.turnOnAC && u.at.rollback && db->activeVdbeCnt>1 ){
53455
+ if( u.as.turnOnAC && u.as.iRollback && db->activeVdbeCnt>1 ){
5352553456
/* If this instruction implements a ROLLBACK and other VMs are
5352653457
** still running, and a transaction is active, return an error indicating
5352753458
** that the other VMs must complete first.
5352853459
*/
53529
- sqlite3SetString(&p->zErrMsg, db, "cannot u.at.rollback transaction - "
53460
+ sqlite3SetString(&p->zErrMsg, db, "cannot rollback transaction - "
5353053461
"SQL statements in progress");
5353153462
rc = SQLITE_BUSY;
53532
- }else if( u.at.turnOnAC && !u.at.rollback && db->writeVdbeCnt>1 ){
53463
+ }else if( u.as.turnOnAC && !u.as.iRollback && db->writeVdbeCnt>0 ){
5353353464
/* If this instruction implements a COMMIT and other VMs are writing
5353453465
** return an error indicating that the other VMs must complete first.
5353553466
*/
5353653467
sqlite3SetString(&p->zErrMsg, db, "cannot commit transaction - "
5353753468
"SQL statements in progress");
5353853469
rc = SQLITE_BUSY;
53539
- }else if( u.at.desiredAutoCommit!=db->autoCommit ){
53540
- if( u.at.rollback ){
53541
- assert( u.at.desiredAutoCommit==1 );
53470
+ }else if( u.as.desiredAutoCommit!=db->autoCommit ){
53471
+ if( u.as.iRollback ){
53472
+ assert( u.as.desiredAutoCommit==1 );
5354253473
sqlite3RollbackAll(db);
5354353474
db->autoCommit = 1;
5354453475
}else{
53545
- db->autoCommit = (u8)u.at.desiredAutoCommit;
53476
+ db->autoCommit = (u8)u.as.desiredAutoCommit;
5354653477
if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
5354753478
p->pc = pc;
53548
- db->autoCommit = (u8)(1-u.at.desiredAutoCommit);
53479
+ db->autoCommit = (u8)(1-u.as.desiredAutoCommit);
5354953480
p->rc = rc = SQLITE_BUSY;
5355053481
goto vdbe_return;
5355153482
}
5355253483
}
5355353484
assert( db->nStatement==0 );
@@ -53558,12 +53489,12 @@
5355853489
rc = SQLITE_ERROR;
5355953490
}
5356053491
goto vdbe_return;
5356153492
}else{
5356253493
sqlite3SetString(&p->zErrMsg, db,
53563
- (!u.at.desiredAutoCommit)?"cannot start a transaction within a transaction":(
53564
- (u.at.rollback)?"cannot u.at.rollback - no transaction is active":
53494
+ (!u.as.desiredAutoCommit)?"cannot start a transaction within a transaction":(
53495
+ (u.as.iRollback)?"cannot rollback - no transaction is active":
5356553496
"cannot commit - no transaction is active"));
5356653497
5356753498
rc = SQLITE_ERROR;
5356853499
}
5356953500
break;
@@ -53589,22 +53520,20 @@
5358953520
** on the file.
5359053521
**
5359153522
** If P2 is zero, then a read-lock is obtained on the database file.
5359253523
*/
5359353524
case OP_Transaction: {
53594
-#if 0 /* local variables moved into u.au */
53595
- int i;
53525
+#if 0 /* local variables moved into u.at */
5359653526
Btree *pBt;
53597
-#endif /* local variables moved into u.au */
53598
-
53599
- u.au.i = pOp->p1;
53600
- assert( u.au.i>=0 && u.au.i<db->nDb );
53601
- assert( (p->btreeMask & (1<<u.au.i))!=0 );
53602
- u.au.pBt = db->aDb[u.au.i].pBt;
53603
-
53604
- if( u.au.pBt ){
53605
- rc = sqlite3BtreeBeginTrans(u.au.pBt, pOp->p2);
53527
+#endif /* local variables moved into u.at */
53528
+
53529
+ assert( pOp->p1>=0 && pOp->p1<db->nDb );
53530
+ assert( (p->btreeMask & (1<<pOp->p1))!=0 );
53531
+ u.at.pBt = db->aDb[pOp->p1].pBt;
53532
+
53533
+ if( u.at.pBt ){
53534
+ rc = sqlite3BtreeBeginTrans(u.at.pBt, pOp->p2);
5360653535
if( rc==SQLITE_BUSY ){
5360753536
p->pc = pc;
5360853537
p->rc = rc = SQLITE_BUSY;
5360953538
goto vdbe_return;
5361053539
}
@@ -53626,25 +53555,25 @@
5362653555
** There must be a read-lock on the database (either a transaction
5362753556
** must be started or there must be an open cursor) before
5362853557
** executing this instruction.
5362953558
*/
5363053559
case OP_ReadCookie: { /* out2-prerelease */
53631
-#if 0 /* local variables moved into u.av */
53560
+#if 0 /* local variables moved into u.au */
5363253561
int iMeta;
5363353562
int iDb;
5363453563
int iCookie;
53635
-#endif /* local variables moved into u.av */
53564
+#endif /* local variables moved into u.au */
5363653565
53637
- u.av.iDb = pOp->p1;
53638
- u.av.iCookie = pOp->p3;
53566
+ u.au.iDb = pOp->p1;
53567
+ u.au.iCookie = pOp->p3;
5363953568
assert( pOp->p3<SQLITE_N_BTREE_META );
53640
- assert( u.av.iDb>=0 && u.av.iDb<db->nDb );
53641
- assert( db->aDb[u.av.iDb].pBt!=0 );
53642
- assert( (p->btreeMask & (1<<u.av.iDb))!=0 );
53569
+ assert( u.au.iDb>=0 && u.au.iDb<db->nDb );
53570
+ assert( db->aDb[u.au.iDb].pBt!=0 );
53571
+ assert( (p->btreeMask & (1<<u.au.iDb))!=0 );
5364353572
53644
- rc = sqlite3BtreeGetMeta(db->aDb[u.av.iDb].pBt, u.av.iCookie, (u32 *)&u.av.iMeta);
53645
- pOut->u.i = u.av.iMeta;
53573
+ rc = sqlite3BtreeGetMeta(db->aDb[u.au.iDb].pBt, u.au.iCookie, (u32 *)&u.au.iMeta);
53574
+ pOut->u.i = u.au.iMeta;
5364653575
MemSetTypeFlag(pOut, MEM_Int);
5364753576
break;
5364853577
}
5364953578
5365053579
/* Opcode: SetCookie P1 P2 P3 * *
@@ -53656,28 +53585,28 @@
5365653585
** database file used to store temporary tables.
5365753586
**
5365853587
** A transaction must be started before executing this opcode.
5365953588
*/
5366053589
case OP_SetCookie: { /* in3 */
53661
-#if 0 /* local variables moved into u.aw */
53590
+#if 0 /* local variables moved into u.av */
5366253591
Db *pDb;
53663
-#endif /* local variables moved into u.aw */
53592
+#endif /* local variables moved into u.av */
5366453593
assert( pOp->p2<SQLITE_N_BTREE_META );
5366553594
assert( pOp->p1>=0 && pOp->p1<db->nDb );
5366653595
assert( (p->btreeMask & (1<<pOp->p1))!=0 );
53667
- u.aw.pDb = &db->aDb[pOp->p1];
53668
- assert( u.aw.pDb->pBt!=0 );
53596
+ u.av.pDb = &db->aDb[pOp->p1];
53597
+ assert( u.av.pDb->pBt!=0 );
5366953598
sqlite3VdbeMemIntegerify(pIn3);
5367053599
/* See note about index shifting on OP_ReadCookie */
53671
- rc = sqlite3BtreeUpdateMeta(u.aw.pDb->pBt, pOp->p2, (int)pIn3->u.i);
53600
+ rc = sqlite3BtreeUpdateMeta(u.av.pDb->pBt, pOp->p2, (int)pIn3->u.i);
5367253601
if( pOp->p2==BTREE_SCHEMA_VERSION ){
5367353602
/* When the schema cookie changes, record the new cookie internally */
53674
- u.aw.pDb->pSchema->schema_cookie = (int)pIn3->u.i;
53603
+ u.av.pDb->pSchema->schema_cookie = (int)pIn3->u.i;
5367553604
db->flags |= SQLITE_InternChanges;
5367653605
}else if( pOp->p2==BTREE_FILE_FORMAT ){
5367753606
/* Record changes in the file format */
53678
- u.aw.pDb->pSchema->file_format = (u8)pIn3->u.i;
53607
+ u.av.pDb->pSchema->file_format = (u8)pIn3->u.i;
5367953608
}
5368053609
if( pOp->p1==1 ){
5368153610
/* Invalidate all prepared statements whenever the TEMP database
5368253611
** schema is changed. Ticket #1644 */
5368353612
sqlite3ExpirePreparedStatements(db);
@@ -53700,24 +53629,24 @@
5370053629
** Either a transaction needs to have been started or an OP_Open needs
5370153630
** to be executed (to establish a read lock) before this opcode is
5370253631
** invoked.
5370353632
*/
5370453633
case OP_VerifyCookie: {
53705
-#if 0 /* local variables moved into u.ax */
53634
+#if 0 /* local variables moved into u.aw */
5370653635
int iMeta;
5370753636
Btree *pBt;
53708
-#endif /* local variables moved into u.ax */
53637
+#endif /* local variables moved into u.aw */
5370953638
assert( pOp->p1>=0 && pOp->p1<db->nDb );
5371053639
assert( (p->btreeMask & (1<<pOp->p1))!=0 );
53711
- u.ax.pBt = db->aDb[pOp->p1].pBt;
53712
- if( u.ax.pBt ){
53713
- rc = sqlite3BtreeGetMeta(u.ax.pBt, BTREE_SCHEMA_VERSION, (u32 *)&u.ax.iMeta);
53640
+ u.aw.pBt = db->aDb[pOp->p1].pBt;
53641
+ if( u.aw.pBt ){
53642
+ rc = sqlite3BtreeGetMeta(u.aw.pBt, BTREE_SCHEMA_VERSION, (u32 *)&u.aw.iMeta);
5371453643
}else{
5371553644
rc = SQLITE_OK;
53716
- u.ax.iMeta = 0;
53645
+ u.aw.iMeta = 0;
5371753646
}
53718
- if( rc==SQLITE_OK && u.ax.iMeta!=pOp->p2 ){
53647
+ if( rc==SQLITE_OK && u.aw.iMeta!=pOp->p2 ){
5371953648
sqlite3DbFree(db, p->zErrMsg);
5372053649
p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed");
5372153650
/* If the schema-cookie from the database file matches the cookie
5372253651
** stored with the in-memory representation of the schema, do
5372353652
** not reload the schema from the database file.
@@ -53729,11 +53658,11 @@
5372953658
** discard the database schema, as the user code implementing the
5373053659
** v-table would have to be ready for the sqlite3_vtab structure itself
5373153660
** to be invalidated whenever sqlite3_step() is called from within
5373253661
** a v-table method.
5373353662
*/
53734
- if( db->aDb[pOp->p1].pSchema->schema_cookie!=u.ax.iMeta ){
53663
+ if( db->aDb[pOp->p1].pSchema->schema_cookie!=u.aw.iMeta ){
5373553664
sqlite3ResetInternalSchema(db, pOp->p1);
5373653665
}
5373753666
5373853667
sqlite3ExpirePreparedStatements(db);
5373953668
rc = SQLITE_SCHEMA;
@@ -53790,105 +53719,104 @@
5379053719
**
5379153720
** See also OpenRead.
5379253721
*/
5379353722
case OP_OpenRead:
5379453723
case OP_OpenWrite: {
53795
-#if 0 /* local variables moved into u.ay */
53724
+#if 0 /* local variables moved into u.ax */
5379653725
int nField;
5379753726
KeyInfo *pKeyInfo;
53798
- int i;
5379953727
int p2;
5380053728
int iDb;
5380153729
int wrFlag;
5380253730
Btree *pX;
5380353731
VdbeCursor *pCur;
5380453732
Db *pDb;
5380553733
int flags;
53806
-#endif /* local variables moved into u.ay */
53807
-
53808
- u.ay.nField = 0;
53809
- u.ay.pKeyInfo = 0;
53810
- u.ay.i = pOp->p1;
53811
- u.ay.p2 = pOp->p2;
53812
- u.ay.iDb = pOp->p3;
53813
- assert( u.ay.iDb>=0 && u.ay.iDb<db->nDb );
53814
- assert( (p->btreeMask & (1<<u.ay.iDb))!=0 );
53815
- u.ay.pDb = &db->aDb[u.ay.iDb];
53816
- u.ay.pX = u.ay.pDb->pBt;
53817
- assert( u.ay.pX!=0 );
53818
- if( pOp->opcode==OP_OpenWrite ){
53819
- u.ay.wrFlag = 1;
53820
- if( u.ay.pDb->pSchema->file_format < p->minWriteFileFormat ){
53821
- p->minWriteFileFormat = u.ay.pDb->pSchema->file_format;
53822
- }
53823
- }else{
53824
- u.ay.wrFlag = 0;
53825
- }
53826
- if( pOp->p5 ){
53827
- assert( u.ay.p2>0 );
53828
- assert( u.ay.p2<=p->nMem );
53829
- pIn2 = &p->aMem[u.ay.p2];
53830
- sqlite3VdbeMemIntegerify(pIn2);
53831
- u.ay.p2 = (int)pIn2->u.i;
53832
- if( u.ay.p2<2 ) {
53833
- rc = SQLITE_CORRUPT_BKPT;
53834
- goto abort_due_to_error;
53835
- }
53836
- }
53837
- assert( u.ay.i>=0 );
53838
- if( pOp->p4type==P4_KEYINFO ){
53839
- u.ay.pKeyInfo = pOp->p4.pKeyInfo;
53840
- u.ay.pKeyInfo->enc = ENC(p->db);
53841
- u.ay.nField = u.ay.pKeyInfo->nField+1;
53842
- }else if( pOp->p4type==P4_INT32 ){
53843
- u.ay.nField = pOp->p4.i;
53844
- }
53845
- u.ay.pCur = allocateCursor(p, u.ay.i, u.ay.nField, u.ay.iDb, 1);
53846
- if( u.ay.pCur==0 ) goto no_mem;
53847
- u.ay.pCur->nullRow = 1;
53848
- rc = sqlite3BtreeCursor(u.ay.pX, u.ay.p2, u.ay.wrFlag, u.ay.pKeyInfo, u.ay.pCur->pCursor);
53849
- u.ay.pCur->pKeyInfo = u.ay.pKeyInfo;
53850
-
53851
- switch( rc ){
53852
- case SQLITE_BUSY: {
53853
- p->pc = pc;
53854
- p->rc = rc = SQLITE_BUSY;
53855
- goto vdbe_return;
53856
- }
53857
- case SQLITE_OK: {
53858
- u.ay.flags = sqlite3BtreeFlags(u.ay.pCur->pCursor);
53859
- /* Sanity checking. Only the lower four bits of the u.ay.flags byte should
53860
- ** be used. Bit 3 (mask 0x08) is unpredictable. The lower 3 bits
53861
- ** (mask 0x07) should be either 5 (intkey+leafdata for tables) or
53862
- ** 2 (zerodata for indices). If these conditions are not met it can
53863
- ** only mean that we are dealing with a corrupt database file
53864
- */
53865
- if( (u.ay.flags & 0xf0)!=0 || ((u.ay.flags & 0x07)!=5 && (u.ay.flags & 0x07)!=2) ){
53866
- rc = SQLITE_CORRUPT_BKPT;
53867
- goto abort_due_to_error;
53868
- }
53869
- u.ay.pCur->isTable = (u.ay.flags & BTREE_INTKEY)!=0 ?1:0;
53870
- u.ay.pCur->isIndex = (u.ay.flags & BTREE_ZERODATA)!=0 ?1:0;
53871
- /* If P4==0 it means we are expected to open a table. If P4!=0 then
53872
- ** we expect to be opening an index. If this is not what happened,
53873
- ** then the database is corrupt
53874
- */
53875
- if( (u.ay.pCur->isTable && pOp->p4type==P4_KEYINFO)
53876
- || (u.ay.pCur->isIndex && pOp->p4type!=P4_KEYINFO) ){
53734
+#endif /* local variables moved into u.ax */
53735
+
53736
+ u.ax.nField = 0;
53737
+ u.ax.pKeyInfo = 0;
53738
+ u.ax.p2 = pOp->p2;
53739
+ u.ax.iDb = pOp->p3;
53740
+ assert( u.ax.iDb>=0 && u.ax.iDb<db->nDb );
53741
+ assert( (p->btreeMask & (1<<u.ax.iDb))!=0 );
53742
+ u.ax.pDb = &db->aDb[u.ax.iDb];
53743
+ u.ax.pX = u.ax.pDb->pBt;
53744
+ assert( u.ax.pX!=0 );
53745
+ if( pOp->opcode==OP_OpenWrite ){
53746
+ u.ax.wrFlag = 1;
53747
+ if( u.ax.pDb->pSchema->file_format < p->minWriteFileFormat ){
53748
+ p->minWriteFileFormat = u.ax.pDb->pSchema->file_format;
53749
+ }
53750
+ }else{
53751
+ u.ax.wrFlag = 0;
53752
+ }
53753
+ if( pOp->p5 ){
53754
+ assert( u.ax.p2>0 );
53755
+ assert( u.ax.p2<=p->nMem );
53756
+ pIn2 = &p->aMem[u.ax.p2];
53757
+ sqlite3VdbeMemIntegerify(pIn2);
53758
+ u.ax.p2 = (int)pIn2->u.i;
53759
+ /* The u.ax.p2 value always comes from a prior OP_CreateTable opcode and
53760
+ ** that opcode will always set the u.ax.p2 value to 2 or more or else fail.
53761
+ ** If there were a failure, the prepared statement would have halted
53762
+ ** before reaching this instruction. */
53763
+ if( NEVER(u.ax.p2<2) ) {
53764
+ rc = SQLITE_CORRUPT_BKPT;
53765
+ goto abort_due_to_error;
53766
+ }
53767
+ }
53768
+ if( pOp->p4type==P4_KEYINFO ){
53769
+ u.ax.pKeyInfo = pOp->p4.pKeyInfo;
53770
+ u.ax.pKeyInfo->enc = ENC(p->db);
53771
+ u.ax.nField = u.ax.pKeyInfo->nField+1;
53772
+ }else if( pOp->p4type==P4_INT32 ){
53773
+ u.ax.nField = pOp->p4.i;
53774
+ }
53775
+ assert( pOp->p1>=0 );
53776
+ u.ax.pCur = allocateCursor(p, pOp->p1, u.ax.nField, u.ax.iDb, 1);
53777
+ if( u.ax.pCur==0 ) goto no_mem;
53778
+ u.ax.pCur->nullRow = 1;
53779
+ rc = sqlite3BtreeCursor(u.ax.pX, u.ax.p2, u.ax.wrFlag, u.ax.pKeyInfo, u.ax.pCur->pCursor);
53780
+ u.ax.pCur->pKeyInfo = u.ax.pKeyInfo;
53781
+
53782
+ switch( rc ){
53783
+ case SQLITE_OK: {
53784
+ u.ax.flags = sqlite3BtreeFlags(u.ax.pCur->pCursor);
53785
+
53786
+ /* Sanity checking. Only the lower four bits of the u.ax.flags byte should
53787
+ ** be used. Bit 3 (mask 0x08) is unpredictable. The lower 3 bits
53788
+ ** (mask 0x07) should be either 5 (intkey+leafdata for tables) or
53789
+ ** 2 (zerodata for indices). If these conditions are not met it can
53790
+ ** only mean that we are dealing with a corrupt database file.
53791
+ ** Note: All of the above is checked already in sqlite3BtreeCursor().
53792
+ */
53793
+ assert( (u.ax.flags & 0xf0)==0 );
53794
+ assert( (u.ax.flags & 0x07)==5 || (u.ax.flags & 0x07)==2 );
53795
+
53796
+ u.ax.pCur->isTable = (u.ax.flags & BTREE_INTKEY)!=0 ?1:0;
53797
+ u.ax.pCur->isIndex = (u.ax.flags & BTREE_ZERODATA)!=0 ?1:0;
53798
+ /* If P4==0 it means we are expected to open a table. If P4!=0 then
53799
+ ** we expect to be opening an index. If this is not what happened,
53800
+ ** then the database is corrupt
53801
+ */
53802
+ if( (u.ax.pCur->isTable && pOp->p4type==P4_KEYINFO)
53803
+ || (u.ax.pCur->isIndex && pOp->p4type!=P4_KEYINFO) ){
5387753804
rc = SQLITE_CORRUPT_BKPT;
5387853805
goto abort_due_to_error;
5387953806
}
5388053807
break;
5388153808
}
5388253809
case SQLITE_EMPTY: {
53883
- u.ay.pCur->isTable = pOp->p4type!=P4_KEYINFO;
53884
- u.ay.pCur->isIndex = !u.ay.pCur->isTable;
53885
- u.ay.pCur->pCursor = 0;
53810
+ u.ax.pCur->isTable = pOp->p4type!=P4_KEYINFO;
53811
+ u.ax.pCur->isIndex = !u.ax.pCur->isTable;
53812
+ u.ax.pCur->pCursor = 0;
5388653813
rc = SQLITE_OK;
5388753814
break;
5388853815
}
5388953816
default: {
53817
+ assert( rc!=SQLITE_BUSY ); /* Busy conditions detected earlier */
5389053818
goto abort_due_to_error;
5389153819
}
5389253820
}
5389353821
break;
5389453822
}
@@ -53910,30 +53838,28 @@
5391053838
** to a TEMP table at the SQL level, or to a table opened by
5391153839
** this opcode. Then this opcode was call OpenVirtual. But
5391253840
** that created confusion with the whole virtual-table idea.
5391353841
*/
5391453842
case OP_OpenEphemeral: {
53915
-#if 0 /* local variables moved into u.az */
53916
- int i;
53843
+#if 0 /* local variables moved into u.ay */
5391753844
VdbeCursor *pCx;
53918
-#endif /* local variables moved into u.az */
53845
+#endif /* local variables moved into u.ay */
5391953846
static const int openFlags =
5392053847
SQLITE_OPEN_READWRITE |
5392153848
SQLITE_OPEN_CREATE |
5392253849
SQLITE_OPEN_EXCLUSIVE |
5392353850
SQLITE_OPEN_DELETEONCLOSE |
5392453851
SQLITE_OPEN_TRANSIENT_DB;
5392553852
53926
- u.az.i = pOp->p1;
53927
- assert( u.az.i>=0 );
53928
- u.az.pCx = allocateCursor(p, u.az.i, pOp->p2, -1, 1);
53929
- if( u.az.pCx==0 ) goto no_mem;
53930
- u.az.pCx->nullRow = 1;
53853
+ assert( pOp->p1>=0 );
53854
+ u.ay.pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, 1);
53855
+ if( u.ay.pCx==0 ) goto no_mem;
53856
+ u.ay.pCx->nullRow = 1;
5393153857
rc = sqlite3BtreeFactory(db, 0, 1, SQLITE_DEFAULT_TEMP_CACHE_SIZE, openFlags,
53932
- &u.az.pCx->pBt);
53858
+ &u.ay.pCx->pBt);
5393353859
if( rc==SQLITE_OK ){
53934
- rc = sqlite3BtreeBeginTrans(u.az.pCx->pBt, 1);
53860
+ rc = sqlite3BtreeBeginTrans(u.ay.pCx->pBt, 1);
5393553861
}
5393653862
if( rc==SQLITE_OK ){
5393753863
/* If a transient index is required, create it by calling
5393853864
** sqlite3BtreeCreateTable() with the BTREE_ZERODATA flag before
5393953865
** opening it. If a transient table is required, just use the
@@ -53940,25 +53866,25 @@
5394053866
** automatically created table with root-page 1 (an INTKEY table).
5394153867
*/
5394253868
if( pOp->p4.pKeyInfo ){
5394353869
int pgno;
5394453870
assert( pOp->p4type==P4_KEYINFO );
53945
- rc = sqlite3BtreeCreateTable(u.az.pCx->pBt, &pgno, BTREE_ZERODATA);
53871
+ rc = sqlite3BtreeCreateTable(u.ay.pCx->pBt, &pgno, BTREE_ZERODATA);
5394653872
if( rc==SQLITE_OK ){
5394753873
assert( pgno==MASTER_ROOT+1 );
53948
- rc = sqlite3BtreeCursor(u.az.pCx->pBt, pgno, 1,
53949
- (KeyInfo*)pOp->p4.z, u.az.pCx->pCursor);
53950
- u.az.pCx->pKeyInfo = pOp->p4.pKeyInfo;
53951
- u.az.pCx->pKeyInfo->enc = ENC(p->db);
53952
- }
53953
- u.az.pCx->isTable = 0;
53954
- }else{
53955
- rc = sqlite3BtreeCursor(u.az.pCx->pBt, MASTER_ROOT, 1, 0, u.az.pCx->pCursor);
53956
- u.az.pCx->isTable = 1;
53874
+ rc = sqlite3BtreeCursor(u.ay.pCx->pBt, pgno, 1,
53875
+ (KeyInfo*)pOp->p4.z, u.ay.pCx->pCursor);
53876
+ u.ay.pCx->pKeyInfo = pOp->p4.pKeyInfo;
53877
+ u.ay.pCx->pKeyInfo->enc = ENC(p->db);
53878
+ }
53879
+ u.ay.pCx->isTable = 0;
53880
+ }else{
53881
+ rc = sqlite3BtreeCursor(u.ay.pCx->pBt, MASTER_ROOT, 1, 0, u.ay.pCx->pCursor);
53882
+ u.ay.pCx->isTable = 1;
5395753883
}
5395853884
}
53959
- u.az.pCx->isIndex = !u.az.pCx->isTable;
53885
+ u.ay.pCx->isIndex = !u.ay.pCx->isTable;
5396053886
break;
5396153887
}
5396253888
5396353889
/* Opcode: OpenPseudo P1 P2 P3 * *
5396453890
**
@@ -53982,40 +53908,34 @@
5398253908
**
5398353909
** P3 is the number of fields in the records that will be stored by
5398453910
** the pseudo-table.
5398553911
*/
5398653912
case OP_OpenPseudo: {
53987
-#if 0 /* local variables moved into u.ba */
53988
- int i;
53913
+#if 0 /* local variables moved into u.az */
5398953914
VdbeCursor *pCx;
53990
-#endif /* local variables moved into u.ba */
53991
-
53992
- u.ba.i = pOp->p1;
53993
- assert( u.ba.i>=0 );
53994
- u.ba.pCx = allocateCursor(p, u.ba.i, pOp->p3, -1, 0);
53995
- if( u.ba.pCx==0 ) goto no_mem;
53996
- u.ba.pCx->nullRow = 1;
53997
- u.ba.pCx->pseudoTable = 1;
53998
- u.ba.pCx->ephemPseudoTable = (u8)pOp->p2;
53999
- u.ba.pCx->isTable = 1;
54000
- u.ba.pCx->isIndex = 0;
53915
+#endif /* local variables moved into u.az */
53916
+
53917
+ assert( pOp->p1>=0 );
53918
+ u.az.pCx = allocateCursor(p, pOp->p1, pOp->p3, -1, 0);
53919
+ if( u.az.pCx==0 ) goto no_mem;
53920
+ u.az.pCx->nullRow = 1;
53921
+ u.az.pCx->pseudoTable = 1;
53922
+ u.az.pCx->ephemPseudoTable = (u8)pOp->p2;
53923
+ u.az.pCx->isTable = 1;
53924
+ u.az.pCx->isIndex = 0;
5400153925
break;
5400253926
}
5400353927
5400453928
/* Opcode: Close P1 * * * *
5400553929
**
5400653930
** Close a cursor previously opened as P1. If P1 is not
5400753931
** currently open, this instruction is a no-op.
5400853932
*/
5400953933
case OP_Close: {
54010
-#if 0 /* local variables moved into u.bb */
54011
- int i;
54012
-#endif /* local variables moved into u.bb */
54013
- u.bb.i = pOp->p1;
54014
- assert( u.bb.i>=0 && u.bb.i<p->nCursor );
54015
- sqlite3VdbeFreeCursor(p, p->apCsr[u.bb.i]);
54016
- p->apCsr[u.bb.i] = 0;
53934
+ assert( pOp->p1>=0 && pOp->p1<p->nCursor );
53935
+ sqlite3VdbeFreeCursor(p, p->apCsr[pOp->p1]);
53936
+ p->apCsr[pOp->p1] = 0;
5401753937
break;
5401853938
}
5401953939
5402053940
/* Opcode: SeekGe P1 P2 P3 P4 *
5402153941
**
@@ -54071,35 +53991,33 @@
5407153991
*/
5407253992
case OP_SeekLt: /* jump, in3 */
5407353993
case OP_SeekLe: /* jump, in3 */
5407453994
case OP_SeekGe: /* jump, in3 */
5407553995
case OP_SeekGt: { /* jump, in3 */
54076
-#if 0 /* local variables moved into u.bc */
54077
- int i;
53996
+#if 0 /* local variables moved into u.ba */
5407853997
int res;
5407953998
int oc;
5408053999
VdbeCursor *pC;
5408154000
UnpackedRecord r;
5408254001
int nField;
5408354002
i64 iKey; /* The rowid we are to seek to */
54084
-#endif /* local variables moved into u.bc */
54003
+#endif /* local variables moved into u.ba */
5408554004
54086
- u.bc.i = pOp->p1;
54087
- assert( u.bc.i>=0 && u.bc.i<p->nCursor );
54005
+ assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5408854006
assert( pOp->p2!=0 );
54089
- u.bc.pC = p->apCsr[u.bc.i];
54090
- assert( u.bc.pC!=0 );
54091
- if( u.bc.pC->pCursor!=0 ){
54092
- u.bc.oc = pOp->opcode;
54093
- u.bc.pC->nullRow = 0;
54094
- if( u.bc.pC->isTable ){
54007
+ u.ba.pC = p->apCsr[pOp->p1];
54008
+ assert( u.ba.pC!=0 );
54009
+ if( u.ba.pC->pCursor!=0 ){
54010
+ u.ba.oc = pOp->opcode;
54011
+ u.ba.pC->nullRow = 0;
54012
+ if( u.ba.pC->isTable ){
5409554013
/* The input value in P3 might be of any type: integer, real, string,
5409654014
** blob, or NULL. But it needs to be an integer before we can do
5409754015
** the seek, so covert it. */
5409854016
applyNumericAffinity(pIn3);
54099
- u.bc.iKey = sqlite3VdbeIntValue(pIn3);
54100
- u.bc.pC->rowidIsValid = 0;
54017
+ u.ba.iKey = sqlite3VdbeIntValue(pIn3);
54018
+ u.ba.pC->rowidIsValid = 0;
5410154019
5410254020
/* If the P3 value could not be converted into an integer without
5410354021
** loss of information, then special processing is required... */
5410454022
if( (pIn3->flags & MEM_Int)==0 ){
5410554023
if( (pIn3->flags & MEM_Real)==0 ){
@@ -54110,99 +54028,100 @@
5411054028
}
5411154029
/* If we reach this point, then the P3 value must be a floating
5411254030
** point number. */
5411354031
assert( (pIn3->flags & MEM_Real)!=0 );
5411454032
54115
- if( u.bc.iKey==SMALLEST_INT64 && (pIn3->r<(double)u.bc.iKey || pIn3->r>0) ){
54116
- /* The P3 value is to large in magnitude to be expressed as an
54117
- ** integer. */
54118
- u.bc.res = 1;
54119
- if( pIn3->r<0 ){
54120
- if( u.bc.oc==OP_SeekGt || u.bc.oc==OP_SeekGe ){
54121
- rc = sqlite3BtreeFirst(u.bc.pC->pCursor, &u.bc.res);
54122
- if( rc!=SQLITE_OK ) goto abort_due_to_error;
54123
- }
54124
- }else{
54125
- if( u.bc.oc==OP_SeekLt || u.bc.oc==OP_SeekLe ){
54126
- rc = sqlite3BtreeLast(u.bc.pC->pCursor, &u.bc.res);
54127
- if( rc!=SQLITE_OK ) goto abort_due_to_error;
54128
- }
54129
- }
54130
- if( u.bc.res ){
54131
- pc = pOp->p2 - 1;
54132
- }
54133
- break;
54134
- }else if( u.bc.oc==OP_SeekLt || u.bc.oc==OP_SeekGe ){
54135
- /* Use the ceiling() function to convert real->int */
54136
- if( pIn3->r > (double)u.bc.iKey ) u.bc.iKey++;
54137
- }else{
54138
- /* Use the floor() function to convert real->int */
54139
- assert( u.bc.oc==OP_SeekLe || u.bc.oc==OP_SeekGt );
54140
- if( pIn3->r < (double)u.bc.iKey ) u.bc.iKey--;
54141
- }
54142
- }
54143
- rc = sqlite3BtreeMovetoUnpacked(u.bc.pC->pCursor, 0, (u64)u.bc.iKey, 0, &u.bc.res);
54144
- if( rc!=SQLITE_OK ){
54145
- goto abort_due_to_error;
54146
- }
54147
- if( u.bc.res==0 ){
54148
- u.bc.pC->rowidIsValid = 1;
54149
- u.bc.pC->lastRowid = u.bc.iKey;
54150
- }
54151
- }else{
54152
- u.bc.nField = pOp->p4.i;
54153
- assert( pOp->p4type==P4_INT32 );
54154
- assert( u.bc.nField>0 );
54155
- u.bc.r.pKeyInfo = u.bc.pC->pKeyInfo;
54156
- u.bc.r.nField = (u16)u.bc.nField;
54157
- if( u.bc.oc==OP_SeekGt || u.bc.oc==OP_SeekLe ){
54158
- u.bc.r.flags = UNPACKED_INCRKEY;
54159
- }else{
54160
- u.bc.r.flags = 0;
54161
- }
54162
- u.bc.r.aMem = &p->aMem[pOp->p3];
54163
- rc = sqlite3BtreeMovetoUnpacked(u.bc.pC->pCursor, &u.bc.r, 0, 0, &u.bc.res);
54164
- if( rc!=SQLITE_OK ){
54165
- goto abort_due_to_error;
54166
- }
54167
- u.bc.pC->rowidIsValid = 0;
54168
- }
54169
- u.bc.pC->deferredMoveto = 0;
54170
- u.bc.pC->cacheStatus = CACHE_STALE;
54171
-#ifdef SQLITE_TEST
54172
- sqlite3_search_count++;
54173
-#endif
54174
- if( u.bc.oc==OP_SeekGe || u.bc.oc==OP_SeekGt ){
54175
- if( u.bc.res<0 || (u.bc.res==0 && u.bc.oc==OP_SeekGt) ){
54176
- rc = sqlite3BtreeNext(u.bc.pC->pCursor, &u.bc.res);
54177
- if( rc!=SQLITE_OK ) goto abort_due_to_error;
54178
- u.bc.pC->rowidIsValid = 0;
54179
- }else{
54180
- u.bc.res = 0;
54181
- }
54182
- }else{
54183
- assert( u.bc.oc==OP_SeekLt || u.bc.oc==OP_SeekLe );
54184
- if( u.bc.res>0 || (u.bc.res==0 && u.bc.oc==OP_SeekLt) ){
54185
- rc = sqlite3BtreePrevious(u.bc.pC->pCursor, &u.bc.res);
54186
- if( rc!=SQLITE_OK ) goto abort_due_to_error;
54187
- u.bc.pC->rowidIsValid = 0;
54188
- }else{
54189
- /* u.bc.res might be negative because the table is empty. Check to
54190
- ** see if this is the case.
54191
- */
54192
- u.bc.res = sqlite3BtreeEof(u.bc.pC->pCursor);
54193
- }
54194
- }
54195
- assert( pOp->p2>0 );
54196
- if( u.bc.res ){
54197
- pc = pOp->p2 - 1;
54198
- }
54199
- }else if( !u.bc.pC->pseudoTable ){
54033
+ if( u.ba.iKey==SMALLEST_INT64 && (pIn3->r<(double)u.ba.iKey || pIn3->r>0) ){
54034
+ /* The P3 value is too large in magnitude to be expressed as an
54035
+ ** integer. */
54036
+ u.ba.res = 1;
54037
+ if( pIn3->r<0 ){
54038
+ if( u.ba.oc==OP_SeekGt || u.ba.oc==OP_SeekGe ){
54039
+ rc = sqlite3BtreeFirst(u.ba.pC->pCursor, &u.ba.res);
54040
+ if( rc!=SQLITE_OK ) goto abort_due_to_error;
54041
+ }
54042
+ }else{
54043
+ if( u.ba.oc==OP_SeekLt || u.ba.oc==OP_SeekLe ){
54044
+ rc = sqlite3BtreeLast(u.ba.pC->pCursor, &u.ba.res);
54045
+ if( rc!=SQLITE_OK ) goto abort_due_to_error;
54046
+ }
54047
+ }
54048
+ if( u.ba.res ){
54049
+ pc = pOp->p2 - 1;
54050
+ }
54051
+ break;
54052
+ }else if( u.ba.oc==OP_SeekLt || u.ba.oc==OP_SeekGe ){
54053
+ /* Use the ceiling() function to convert real->int */
54054
+ if( pIn3->r > (double)u.ba.iKey ) u.ba.iKey++;
54055
+ }else{
54056
+ /* Use the floor() function to convert real->int */
54057
+ assert( u.ba.oc==OP_SeekLe || u.ba.oc==OP_SeekGt );
54058
+ if( pIn3->r < (double)u.ba.iKey ) u.ba.iKey--;
54059
+ }
54060
+ }
54061
+ rc = sqlite3BtreeMovetoUnpacked(u.ba.pC->pCursor, 0, (u64)u.ba.iKey, 0, &u.ba.res);
54062
+ if( rc!=SQLITE_OK ){
54063
+ goto abort_due_to_error;
54064
+ }
54065
+ if( u.ba.res==0 ){
54066
+ u.ba.pC->rowidIsValid = 1;
54067
+ u.ba.pC->lastRowid = u.ba.iKey;
54068
+ }
54069
+ }else{
54070
+ u.ba.nField = pOp->p4.i;
54071
+ assert( pOp->p4type==P4_INT32 );
54072
+ assert( u.ba.nField>0 );
54073
+ u.ba.r.pKeyInfo = u.ba.pC->pKeyInfo;
54074
+ u.ba.r.nField = (u16)u.ba.nField;
54075
+ if( u.ba.oc==OP_SeekGt || u.ba.oc==OP_SeekLe ){
54076
+ u.ba.r.flags = UNPACKED_INCRKEY;
54077
+ }else{
54078
+ u.ba.r.flags = 0;
54079
+ }
54080
+ u.ba.r.aMem = &p->aMem[pOp->p3];
54081
+ rc = sqlite3BtreeMovetoUnpacked(u.ba.pC->pCursor, &u.ba.r, 0, 0, &u.ba.res);
54082
+ if( rc!=SQLITE_OK ){
54083
+ goto abort_due_to_error;
54084
+ }
54085
+ u.ba.pC->rowidIsValid = 0;
54086
+ }
54087
+ u.ba.pC->deferredMoveto = 0;
54088
+ u.ba.pC->cacheStatus = CACHE_STALE;
54089
+#ifdef SQLITE_TEST
54090
+ sqlite3_search_count++;
54091
+#endif
54092
+ if( u.ba.oc==OP_SeekGe || u.ba.oc==OP_SeekGt ){
54093
+ if( u.ba.res<0 || (u.ba.res==0 && u.ba.oc==OP_SeekGt) ){
54094
+ rc = sqlite3BtreeNext(u.ba.pC->pCursor, &u.ba.res);
54095
+ if( rc!=SQLITE_OK ) goto abort_due_to_error;
54096
+ u.ba.pC->rowidIsValid = 0;
54097
+ }else{
54098
+ u.ba.res = 0;
54099
+ }
54100
+ }else{
54101
+ assert( u.ba.oc==OP_SeekLt || u.ba.oc==OP_SeekLe );
54102
+ if( u.ba.res>0 || (u.ba.res==0 && u.ba.oc==OP_SeekLt) ){
54103
+ rc = sqlite3BtreePrevious(u.ba.pC->pCursor, &u.ba.res);
54104
+ if( rc!=SQLITE_OK ) goto abort_due_to_error;
54105
+ u.ba.pC->rowidIsValid = 0;
54106
+ }else{
54107
+ /* u.ba.res might be negative because the table is empty. Check to
54108
+ ** see if this is the case.
54109
+ */
54110
+ u.ba.res = sqlite3BtreeEof(u.ba.pC->pCursor);
54111
+ }
54112
+ }
54113
+ assert( pOp->p2>0 );
54114
+ if( u.ba.res ){
54115
+ pc = pOp->p2 - 1;
54116
+ }
54117
+ }else{
5420054118
/* This happens when attempting to open the sqlite3_master table
5420154119
** for read access returns SQLITE_EMPTY. In this case always
5420254120
** take the jump (since there are no records in the table).
5420354121
*/
54122
+ assert( u.ba.pC->pseudoTable==0 );
5420454123
pc = pOp->p2 - 1;
5420554124
}
5420654125
break;
5420754126
}
5420854127
@@ -54214,25 +54133,23 @@
5421454133
** This is actually a deferred seek. Nothing actually happens until
5421554134
** the cursor is used to read a record. That way, if no reads
5421654135
** occur, no unnecessary I/O happens.
5421754136
*/
5421854137
case OP_Seek: { /* in2 */
54219
-#if 0 /* local variables moved into u.bd */
54220
- int i;
54138
+#if 0 /* local variables moved into u.bb */
5422154139
VdbeCursor *pC;
54222
-#endif /* local variables moved into u.bd */
54223
-
54224
- u.bd.i = pOp->p1;
54225
- assert( u.bd.i>=0 && u.bd.i<p->nCursor );
54226
- u.bd.pC = p->apCsr[u.bd.i];
54227
- assert( u.bd.pC!=0 );
54228
- if( u.bd.pC->pCursor!=0 ){
54229
- assert( u.bd.pC->isTable );
54230
- u.bd.pC->nullRow = 0;
54231
- u.bd.pC->movetoTarget = sqlite3VdbeIntValue(pIn2);
54232
- u.bd.pC->rowidIsValid = 0;
54233
- u.bd.pC->deferredMoveto = 1;
54140
+#endif /* local variables moved into u.bb */
54141
+
54142
+ assert( pOp->p1>=0 && pOp->p1<p->nCursor );
54143
+ u.bb.pC = p->apCsr[pOp->p1];
54144
+ assert( u.bb.pC!=0 );
54145
+ if( ALWAYS(u.bb.pC->pCursor!=0) ){
54146
+ assert( u.bb.pC->isTable );
54147
+ u.bb.pC->nullRow = 0;
54148
+ u.bb.pC->movetoTarget = sqlite3VdbeIntValue(pIn2);
54149
+ u.bb.pC->rowidIsValid = 0;
54150
+ u.bb.pC->deferredMoveto = 1;
5423454151
}
5423554152
break;
5423654153
}
5423754154
5423854155
@@ -54266,48 +54183,47 @@
5426654183
**
5426754184
** See also: Found, NotExists, IsUnique
5426854185
*/
5426954186
case OP_NotFound: /* jump, in3 */
5427054187
case OP_Found: { /* jump, in3 */
54271
-#if 0 /* local variables moved into u.be */
54272
- int i;
54188
+#if 0 /* local variables moved into u.bc */
5427354189
int alreadyExists;
5427454190
VdbeCursor *pC;
5427554191
int res;
5427654192
UnpackedRecord *pIdxKey;
5427754193
char aTempRec[ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*3 + 7];
54278
-#endif /* local variables moved into u.be */
54279
-
54280
- u.be.i = pOp->p1;
54281
- u.be.alreadyExists = 0;
54282
- assert( u.be.i>=0 && u.be.i<p->nCursor );
54283
- assert( p->apCsr[u.be.i]!=0 );
54284
- if( (u.be.pC = p->apCsr[u.be.i])->pCursor!=0 ){
54285
-
54286
- assert( u.be.pC->isTable==0 );
54194
+#endif /* local variables moved into u.bc */
54195
+
54196
+ u.bc.alreadyExists = 0;
54197
+ assert( pOp->p1>=0 && pOp->p1<p->nCursor );
54198
+ u.bc.pC = p->apCsr[pOp->p1];
54199
+ assert( u.bc.pC!=0 );
54200
+ if( ALWAYS(u.bc.pC->pCursor!=0) ){
54201
+
54202
+ assert( u.bc.pC->isTable==0 );
5428754203
assert( pIn3->flags & MEM_Blob );
54288
- u.be.pIdxKey = sqlite3VdbeRecordUnpack(u.be.pC->pKeyInfo, pIn3->n, pIn3->z,
54289
- u.be.aTempRec, sizeof(u.be.aTempRec));
54290
- if( u.be.pIdxKey==0 ){
54204
+ u.bc.pIdxKey = sqlite3VdbeRecordUnpack(u.bc.pC->pKeyInfo, pIn3->n, pIn3->z,
54205
+ u.bc.aTempRec, sizeof(u.bc.aTempRec));
54206
+ if( u.bc.pIdxKey==0 ){
5429154207
goto no_mem;
5429254208
}
5429354209
if( pOp->opcode==OP_Found ){
54294
- u.be.pIdxKey->flags |= UNPACKED_PREFIX_MATCH;
54210
+ u.bc.pIdxKey->flags |= UNPACKED_PREFIX_MATCH;
5429554211
}
54296
- rc = sqlite3BtreeMovetoUnpacked(u.be.pC->pCursor, u.be.pIdxKey, 0, 0, &u.be.res);
54297
- sqlite3VdbeDeleteUnpackedRecord(u.be.pIdxKey);
54212
+ rc = sqlite3BtreeMovetoUnpacked(u.bc.pC->pCursor, u.bc.pIdxKey, 0, 0, &u.bc.res);
54213
+ sqlite3VdbeDeleteUnpackedRecord(u.bc.pIdxKey);
5429854214
if( rc!=SQLITE_OK ){
5429954215
break;
5430054216
}
54301
- u.be.alreadyExists = (u.be.res==0);
54302
- u.be.pC->deferredMoveto = 0;
54303
- u.be.pC->cacheStatus = CACHE_STALE;
54217
+ u.bc.alreadyExists = (u.bc.res==0);
54218
+ u.bc.pC->deferredMoveto = 0;
54219
+ u.bc.pC->cacheStatus = CACHE_STALE;
5430454220
}
5430554221
if( pOp->opcode==OP_Found ){
54306
- if( u.be.alreadyExists ) pc = pOp->p2 - 1;
54222
+ if( u.bc.alreadyExists ) pc = pOp->p2 - 1;
5430754223
}else{
54308
- if( !u.be.alreadyExists ) pc = pOp->p2 - 1;
54224
+ if( !u.bc.alreadyExists ) pc = pOp->p2 - 1;
5430954225
}
5431054226
break;
5431154227
}
5431254228
5431354229
/* Opcode: IsUnique P1 P2 P3 P4 *
@@ -54334,63 +54250,63 @@
5433454250
** instruction.
5433554251
**
5433654252
** See also: NotFound, NotExists, Found
5433754253
*/
5433854254
case OP_IsUnique: { /* jump, in3 */
54339
-#if 0 /* local variables moved into u.bf */
54255
+#if 0 /* local variables moved into u.bd */
5434054256
u16 ii;
5434154257
VdbeCursor *pCx;
5434254258
BtCursor *pCrsr;
5434354259
u16 nField;
5434454260
Mem *aMem;
5434554261
UnpackedRecord r; /* B-Tree index search key */
5434654262
i64 R; /* Rowid stored in register P3 */
54347
-#endif /* local variables moved into u.bf */
54263
+#endif /* local variables moved into u.bd */
5434854264
54349
- u.bf.aMem = &p->aMem[pOp->p4.i];
54265
+ u.bd.aMem = &p->aMem[pOp->p4.i];
5435054266
/* Assert that the values of parameters P1 and P4 are in range. */
5435154267
assert( pOp->p4type==P4_INT32 );
5435254268
assert( pOp->p4.i>0 && pOp->p4.i<=p->nMem );
5435354269
assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5435454270
5435554271
/* Find the index cursor. */
54356
- u.bf.pCx = p->apCsr[pOp->p1];
54357
- assert( u.bf.pCx->deferredMoveto==0 );
54358
- u.bf.pCx->seekResult = 0;
54359
- u.bf.pCx->cacheStatus = CACHE_STALE;
54360
- u.bf.pCrsr = u.bf.pCx->pCursor;
54272
+ u.bd.pCx = p->apCsr[pOp->p1];
54273
+ assert( u.bd.pCx->deferredMoveto==0 );
54274
+ u.bd.pCx->seekResult = 0;
54275
+ u.bd.pCx->cacheStatus = CACHE_STALE;
54276
+ u.bd.pCrsr = u.bd.pCx->pCursor;
5436154277
5436254278
/* If any of the values are NULL, take the jump. */
54363
- u.bf.nField = u.bf.pCx->pKeyInfo->nField;
54364
- for(u.bf.ii=0; u.bf.ii<u.bf.nField; u.bf.ii++){
54365
- if( u.bf.aMem[u.bf.ii].flags & MEM_Null ){
54279
+ u.bd.nField = u.bd.pCx->pKeyInfo->nField;
54280
+ for(u.bd.ii=0; u.bd.ii<u.bd.nField; u.bd.ii++){
54281
+ if( u.bd.aMem[u.bd.ii].flags & MEM_Null ){
5436654282
pc = pOp->p2 - 1;
54367
- u.bf.pCrsr = 0;
54283
+ u.bd.pCrsr = 0;
5436854284
break;
5436954285
}
5437054286
}
54371
- assert( (u.bf.aMem[u.bf.nField].flags & MEM_Null)==0 );
54287
+ assert( (u.bd.aMem[u.bd.nField].flags & MEM_Null)==0 );
5437254288
54373
- if( u.bf.pCrsr!=0 ){
54289
+ if( u.bd.pCrsr!=0 ){
5437454290
/* Populate the index search key. */
54375
- u.bf.r.pKeyInfo = u.bf.pCx->pKeyInfo;
54376
- u.bf.r.nField = u.bf.nField + 1;
54377
- u.bf.r.flags = UNPACKED_PREFIX_SEARCH;
54378
- u.bf.r.aMem = u.bf.aMem;
54291
+ u.bd.r.pKeyInfo = u.bd.pCx->pKeyInfo;
54292
+ u.bd.r.nField = u.bd.nField + 1;
54293
+ u.bd.r.flags = UNPACKED_PREFIX_SEARCH;
54294
+ u.bd.r.aMem = u.bd.aMem;
5437954295
54380
- /* Extract the value of u.bf.R from register P3. */
54296
+ /* Extract the value of u.bd.R from register P3. */
5438154297
sqlite3VdbeMemIntegerify(pIn3);
54382
- u.bf.R = pIn3->u.i;
54298
+ u.bd.R = pIn3->u.i;
5438354299
5438454300
/* Search the B-Tree index. If no conflicting record is found, jump
5438554301
** to P2. Otherwise, copy the rowid of the conflicting record to
5438654302
** register P3 and fall through to the next instruction. */
54387
- rc = sqlite3BtreeMovetoUnpacked(u.bf.pCrsr, &u.bf.r, 0, 0, &u.bf.pCx->seekResult);
54388
- if( (u.bf.r.flags & UNPACKED_PREFIX_SEARCH) || u.bf.r.rowid==u.bf.R ){
54303
+ rc = sqlite3BtreeMovetoUnpacked(u.bd.pCrsr, &u.bd.r, 0, 0, &u.bd.pCx->seekResult);
54304
+ if( (u.bd.r.flags & UNPACKED_PREFIX_SEARCH) || u.bd.r.rowid==u.bd.R ){
5438954305
pc = pOp->p2 - 1;
5439054306
}else{
54391
- pIn3->u.i = u.bf.r.rowid;
54307
+ pIn3->u.i = u.bd.r.rowid;
5439254308
}
5439354309
}
5439454310
break;
5439554311
}
5439654312
@@ -54407,45 +54323,46 @@
5440754323
** P1 is an index.
5440854324
**
5440954325
** See also: Found, NotFound, IsUnique
5441054326
*/
5441154327
case OP_NotExists: { /* jump, in3 */
54412
-#if 0 /* local variables moved into u.bg */
54413
- int i;
54328
+#if 0 /* local variables moved into u.be */
5441454329
VdbeCursor *pC;
5441554330
BtCursor *pCrsr;
5441654331
int res;
5441754332
u64 iKey;
54418
-#endif /* local variables moved into u.bg */
54419
-
54420
- u.bg.i = pOp->p1;
54421
- assert( u.bg.i>=0 && u.bg.i<p->nCursor );
54422
- assert( p->apCsr[u.bg.i]!=0 );
54423
- if( (u.bg.pCrsr = (u.bg.pC = p->apCsr[u.bg.i])->pCursor)!=0 ){
54424
- u.bg.res = 0;
54425
- assert( pIn3->flags & MEM_Int );
54426
- assert( p->apCsr[u.bg.i]->isTable );
54427
- u.bg.iKey = intToKey(pIn3->u.i);
54428
- rc = sqlite3BtreeMovetoUnpacked(u.bg.pCrsr, 0, u.bg.iKey, 0, &u.bg.res);
54429
- u.bg.pC->lastRowid = pIn3->u.i;
54430
- u.bg.pC->rowidIsValid = u.bg.res==0 ?1:0;
54431
- u.bg.pC->nullRow = 0;
54432
- u.bg.pC->cacheStatus = CACHE_STALE;
54433
- u.bg.pC->deferredMoveto = 0;
54434
- if( u.bg.res!=0 ){
54333
+#endif /* local variables moved into u.be */
54334
+
54335
+ assert( pIn3->flags & MEM_Int );
54336
+ assert( pOp->p1>=0 && pOp->p1<p->nCursor );
54337
+ u.be.pC = p->apCsr[pOp->p1];
54338
+ assert( u.be.pC!=0 );
54339
+ assert( u.be.pC->isTable );
54340
+ u.be.pCrsr = u.be.pC->pCursor;
54341
+ if( u.be.pCrsr!=0 ){
54342
+ u.be.res = 0;
54343
+ u.be.iKey = pIn3->u.i;
54344
+ rc = sqlite3BtreeMovetoUnpacked(u.be.pCrsr, 0, u.be.iKey, 0, &u.be.res);
54345
+ u.be.pC->lastRowid = pIn3->u.i;
54346
+ u.be.pC->rowidIsValid = u.be.res==0 ?1:0;
54347
+ u.be.pC->nullRow = 0;
54348
+ u.be.pC->cacheStatus = CACHE_STALE;
54349
+ u.be.pC->deferredMoveto = 0;
54350
+ if( u.be.res!=0 ){
5443554351
pc = pOp->p2 - 1;
54436
- assert( u.bg.pC->rowidIsValid==0 );
54352
+ assert( u.be.pC->rowidIsValid==0 );
5443754353
}
54438
- u.bg.pC->seekResult = u.bg.res;
54439
- }else if( !u.bg.pC->pseudoTable ){
54354
+ u.be.pC->seekResult = u.be.res;
54355
+ }else{
5444054356
/* This happens when an attempt to open a read cursor on the
5444154357
** sqlite_master table returns SQLITE_EMPTY.
5444254358
*/
54443
- assert( u.bg.pC->isTable );
54359
+ assert( !u.be.pC->pseudoTable );
54360
+ assert( u.be.pC->isTable );
5444454361
pc = pOp->p2 - 1;
54445
- assert( u.bg.pC->rowidIsValid==0 );
54446
- u.bg.pC->seekResult = 0;
54362
+ assert( u.be.pC->rowidIsValid==0 );
54363
+ u.be.pC->seekResult = 0;
5444754364
}
5444854365
break;
5444954366
}
5445054367
5445154368
/* Opcode: Sequence P1 P2 * * *
@@ -54454,14 +54371,13 @@
5445454371
** Write the sequence number into register P2.
5445554372
** The sequence number on the cursor is incremented after this
5445654373
** instruction.
5445754374
*/
5445854375
case OP_Sequence: { /* out2-prerelease */
54459
- int i = pOp->p1;
54460
- assert( i>=0 && i<p->nCursor );
54461
- assert( p->apCsr[i]!=0 );
54462
- pOut->u.i = p->apCsr[i]->seqCount++;
54376
+ assert( pOp->p1>=0 && pOp->p1<p->nCursor );
54377
+ assert( p->apCsr[pOp->p1]!=0 );
54378
+ pOut->u.i = p->apCsr[pOp->p1]->seqCount++;
5446354379
MemSetTypeFlag(pOut, MEM_Int);
5446454380
break;
5446554381
}
5446654382
5446754383
@@ -54478,28 +54394,24 @@
5447854394
** error is generated. The P3 register is updated with the generated
5447954395
** record number. This P3 mechanism is used to help implement the
5448054396
** AUTOINCREMENT feature.
5448154397
*/
5448254398
case OP_NewRowid: { /* out2-prerelease */
54483
-#if 0 /* local variables moved into u.bh */
54484
- int i;
54485
- i64 v;
54486
- VdbeCursor *pC;
54487
- int res;
54488
- int rx;
54489
- int cnt;
54490
- i64 x;
54491
- Mem *pMem;
54492
-#endif /* local variables moved into u.bh */
54493
-
54494
- u.bh.i = pOp->p1;
54495
- u.bh.v = 0;
54496
- u.bh.res = 0;
54497
- u.bh.rx = SQLITE_OK;
54498
- assert( u.bh.i>=0 && u.bh.i<p->nCursor );
54499
- assert( p->apCsr[u.bh.i]!=0 );
54500
- if( (u.bh.pC = p->apCsr[u.bh.i])->pCursor==0 ){
54399
+#if 0 /* local variables moved into u.bf */
54400
+ i64 v; /* The new rowid */
54401
+ VdbeCursor *pC; /* Cursor of table to get the new rowid */
54402
+ int res; /* Result of an sqlite3BtreeLast() */
54403
+ int cnt; /* Counter to limit the number of searches */
54404
+ Mem *pMem; /* Register holding largest rowid for AUTOINCREMENT */
54405
+#endif /* local variables moved into u.bf */
54406
+
54407
+ u.bf.v = 0;
54408
+ u.bf.res = 0;
54409
+ assert( pOp->p1>=0 && pOp->p1<p->nCursor );
54410
+ u.bf.pC = p->apCsr[pOp->p1];
54411
+ assert( u.bf.pC!=0 );
54412
+ if( NEVER(u.bf.pC->pCursor==0) ){
5450154413
/* The zero initialization above is all that is needed */
5450254414
}else{
5450354415
/* The next rowid or record number (different terms for the same
5450454416
** thing) is obtained in a two-step algorithm.
5450554417
**
@@ -54509,38 +54421,14 @@
5450954421
** probabilistic algorithm
5451054422
**
5451154423
** The second algorithm is to select a rowid at random and see if
5451254424
** it already exists in the table. If it does not exist, we have
5451354425
** succeeded. If the random rowid does exist, we select a new one
54514
- ** and try again, up to 1000 times.
54515
- **
54516
- ** For a table with less than 2 billion entries, the probability
54517
- ** of not finding a unused rowid is about 1.0e-300. This is a
54518
- ** non-zero probability, but it is still vanishingly small and should
54519
- ** never cause a problem. You are much, much more likely to have a
54520
- ** hardware failure than for this algorithm to fail.
54521
- **
54522
- ** The analysis in the previous paragraph assumes that you have a good
54523
- ** source of random numbers. Is a library function like lrand48()
54524
- ** good enough? Maybe. Maybe not. It's hard to know whether there
54525
- ** might be subtle bugs is some implementations of lrand48() that
54526
- ** could cause problems. To avoid uncertainty, SQLite uses its own
54527
- ** random number generator based on the RC4 algorithm.
54528
- **
54529
- ** To promote locality of reference for repetitive inserts, the
54530
- ** first few attempts at choosing a random rowid pick values just a little
54531
- ** larger than the previous rowid. This has been shown experimentally
54532
- ** to double the speed of the COPY operation.
54426
+ ** and try again, up to 100 times.
5453354427
*/
54534
- u.bh.cnt = 0;
54535
- if( (sqlite3BtreeFlags(u.bh.pC->pCursor)&(BTREE_INTKEY|BTREE_ZERODATA)) !=
54536
- BTREE_INTKEY ){
54537
- rc = SQLITE_CORRUPT_BKPT;
54538
- goto abort_due_to_error;
54539
- }
54540
- assert( (sqlite3BtreeFlags(u.bh.pC->pCursor) & BTREE_INTKEY)!=0 );
54541
- assert( (sqlite3BtreeFlags(u.bh.pC->pCursor) & BTREE_ZERODATA)==0 );
54428
+ assert( u.bf.pC->isTable );
54429
+ u.bf.cnt = 0;
5454254430
5454354431
#ifdef SQLITE_32BIT_ROWID
5454454432
# define MAX_ROWID 0x7fffffff
5454554433
#else
5454654434
/* Some compilers complain about constants of the form 0x7fffffffffffffff.
@@ -54548,78 +54436,76 @@
5454854436
** to provide the constant while making all compilers happy.
5454954437
*/
5455054438
# define MAX_ROWID (i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff )
5455154439
#endif
5455254440
54553
- if( !u.bh.pC->useRandomRowid ){
54554
- u.bh.v = sqlite3BtreeGetCachedRowid(u.bh.pC->pCursor);
54555
- if( u.bh.v==0 ){
54556
- rc = sqlite3BtreeLast(u.bh.pC->pCursor, &u.bh.res);
54441
+ if( !u.bf.pC->useRandomRowid ){
54442
+ u.bf.v = sqlite3BtreeGetCachedRowid(u.bf.pC->pCursor);
54443
+ if( u.bf.v==0 ){
54444
+ rc = sqlite3BtreeLast(u.bf.pC->pCursor, &u.bf.res);
5455754445
if( rc!=SQLITE_OK ){
5455854446
goto abort_due_to_error;
5455954447
}
54560
- if( u.bh.res ){
54561
- u.bh.v = 1;
54562
- }else{
54563
- sqlite3BtreeKeySize(u.bh.pC->pCursor, &u.bh.v);
54564
- u.bh.v = keyToInt(u.bh.v);
54565
- if( u.bh.v==MAX_ROWID ){
54566
- u.bh.pC->useRandomRowid = 1;
54567
- }else{
54568
- u.bh.v++;
54448
+ if( u.bf.res ){
54449
+ u.bf.v = 1;
54450
+ }else{
54451
+ sqlite3BtreeKeySize(u.bf.pC->pCursor, &u.bf.v);
54452
+ if( u.bf.v==MAX_ROWID ){
54453
+ u.bf.pC->useRandomRowid = 1;
54454
+ }else{
54455
+ u.bf.v++;
5456954456
}
5457054457
}
5457154458
}
5457254459
5457354460
#ifndef SQLITE_OMIT_AUTOINCREMENT
5457454461
if( pOp->p3 ){
5457554462
assert( pOp->p3>0 && pOp->p3<=p->nMem ); /* P3 is a valid memory cell */
54576
- u.bh.pMem = &p->aMem[pOp->p3];
54577
- REGISTER_TRACE(pOp->p3, u.bh.pMem);
54578
- sqlite3VdbeMemIntegerify(u.bh.pMem);
54579
- assert( (u.bh.pMem->flags & MEM_Int)!=0 ); /* mem(P3) holds an integer */
54580
- if( u.bh.pMem->u.i==MAX_ROWID || u.bh.pC->useRandomRowid ){
54581
- rc = SQLITE_FULL;
54582
- goto abort_due_to_error;
54583
- }
54584
- if( u.bh.v<u.bh.pMem->u.i+1 ){
54585
- u.bh.v = u.bh.pMem->u.i + 1;
54586
- }
54587
- u.bh.pMem->u.i = u.bh.v;
54588
- }
54589
-#endif
54590
-
54591
- sqlite3BtreeSetCachedRowid(u.bh.pC->pCursor, u.bh.v<MAX_ROWID ? u.bh.v+1 : 0);
54592
- }
54593
- if( u.bh.pC->useRandomRowid ){
54594
- assert( pOp->p3==0 ); /* SQLITE_FULL must have occurred prior to this */
54595
- u.bh.v = db->priorNewRowid;
54596
- u.bh.cnt = 0;
54597
- do{
54598
- if( u.bh.cnt==0 && (u.bh.v&0xffffff)==u.bh.v ){
54599
- u.bh.v++;
54600
- }else{
54601
- sqlite3_randomness(sizeof(u.bh.v), &u.bh.v);
54602
- if( u.bh.cnt<5 ) u.bh.v &= 0xffffff;
54603
- }
54604
- if( u.bh.v==0 ) continue;
54605
- u.bh.x = intToKey(u.bh.v);
54606
- u.bh.rx = sqlite3BtreeMovetoUnpacked(u.bh.pC->pCursor, 0, (u64)u.bh.x, 0, &u.bh.res);
54607
- u.bh.cnt++;
54608
- }while( u.bh.cnt<100 && u.bh.rx==SQLITE_OK && u.bh.res==0 );
54609
- db->priorNewRowid = u.bh.v;
54610
- if( u.bh.rx==SQLITE_OK && u.bh.res==0 ){
54611
- rc = SQLITE_FULL;
54612
- goto abort_due_to_error;
54613
- }
54614
- }
54615
- u.bh.pC->rowidIsValid = 0;
54616
- u.bh.pC->deferredMoveto = 0;
54617
- u.bh.pC->cacheStatus = CACHE_STALE;
54618
- }
54619
- MemSetTypeFlag(pOut, MEM_Int);
54620
- pOut->u.i = u.bh.v;
54463
+ u.bf.pMem = &p->aMem[pOp->p3];
54464
+ REGISTER_TRACE(pOp->p3, u.bf.pMem);
54465
+ sqlite3VdbeMemIntegerify(u.bf.pMem);
54466
+ assert( (u.bf.pMem->flags & MEM_Int)!=0 ); /* mem(P3) holds an integer */
54467
+ if( u.bf.pMem->u.i==MAX_ROWID || u.bf.pC->useRandomRowid ){
54468
+ rc = SQLITE_FULL;
54469
+ goto abort_due_to_error;
54470
+ }
54471
+ if( u.bf.v<u.bf.pMem->u.i+1 ){
54472
+ u.bf.v = u.bf.pMem->u.i + 1;
54473
+ }
54474
+ u.bf.pMem->u.i = u.bf.v;
54475
+ }
54476
+#endif
54477
+
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{
54489
+ sqlite3_randomness(sizeof(u.bf.v), &u.bf.v);
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
+ }
54501
+ u.bf.pC->rowidIsValid = 0;
54502
+ u.bf.pC->deferredMoveto = 0;
54503
+ u.bf.pC->cacheStatus = CACHE_STALE;
54504
+ }
54505
+ MemSetTypeFlag(pOut, MEM_Int);
54506
+ pOut->u.i = u.bf.v;
5462154507
break;
5462254508
}
5462354509
5462454510
/* Opcode: Insert P1 P2 P3 P4 P5
5462554511
**
@@ -54646,91 +54532,89 @@
5464654532
**
5464754533
** This instruction only works on tables. The equivalent instruction
5464854534
** for indices is OP_IdxInsert.
5464954535
*/
5465054536
case OP_Insert: {
54651
-#if 0 /* local variables moved into u.bi */
54537
+#if 0 /* local variables moved into u.bg */
5465254538
Mem *pData;
5465354539
Mem *pKey;
5465454540
i64 iKey; /* The integer ROWID or key for the record to be inserted */
54655
- int i;
5465654541
VdbeCursor *pC;
5465754542
int nZero;
5465854543
int seekResult;
5465954544
const char *zDb;
5466054545
const char *zTbl;
5466154546
int op;
54662
-#endif /* local variables moved into u.bi */
54663
-
54664
- u.bi.pData = &p->aMem[pOp->p2];
54665
- u.bi.pKey = &p->aMem[pOp->p3];
54666
- u.bi.i = pOp->p1;
54667
- assert( u.bi.i>=0 && u.bi.i<p->nCursor );
54668
- u.bi.pC = p->apCsr[u.bi.i];
54669
- assert( u.bi.pC!=0 );
54670
- assert( u.bi.pC->pCursor!=0 || u.bi.pC->pseudoTable );
54671
- assert( u.bi.pKey->flags & MEM_Int );
54672
- assert( u.bi.pC->isTable );
54673
- REGISTER_TRACE(pOp->p2, u.bi.pData);
54674
- REGISTER_TRACE(pOp->p3, u.bi.pKey);
54675
-
54676
- u.bi.iKey = intToKey(u.bi.pKey->u.i);
54547
+#endif /* local variables moved into u.bg */
54548
+
54549
+ u.bg.pData = &p->aMem[pOp->p2];
54550
+ u.bg.pKey = &p->aMem[pOp->p3];
54551
+ assert( pOp->p1>=0 && pOp->p1<p->nCursor );
54552
+ u.bg.pC = p->apCsr[pOp->p1];
54553
+ assert( u.bg.pC!=0 );
54554
+ assert( u.bg.pC->pCursor!=0 || u.bg.pC->pseudoTable );
54555
+ assert( u.bg.pKey->flags & MEM_Int );
54556
+ assert( u.bg.pC->isTable );
54557
+ REGISTER_TRACE(pOp->p2, u.bg.pData);
54558
+ REGISTER_TRACE(pOp->p3, u.bg.pKey);
54559
+
54560
+ u.bg.iKey = u.bg.pKey->u.i;
5467754561
if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
54678
- if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = u.bi.pKey->u.i;
54679
- if( u.bi.pData->flags & MEM_Null ){
54680
- u.bi.pData->z = 0;
54681
- u.bi.pData->n = 0;
54682
- }else{
54683
- assert( u.bi.pData->flags & (MEM_Blob|MEM_Str) );
54684
- }
54685
- if( u.bi.pC->pseudoTable ){
54686
- if( !u.bi.pC->ephemPseudoTable ){
54687
- sqlite3DbFree(db, u.bi.pC->pData);
54688
- }
54689
- u.bi.pC->iKey = u.bi.iKey;
54690
- u.bi.pC->nData = u.bi.pData->n;
54691
- if( u.bi.pData->z==u.bi.pData->zMalloc || u.bi.pC->ephemPseudoTable ){
54692
- u.bi.pC->pData = u.bi.pData->z;
54693
- if( !u.bi.pC->ephemPseudoTable ){
54694
- u.bi.pData->flags &= ~MEM_Dyn;
54695
- u.bi.pData->flags |= MEM_Ephem;
54696
- u.bi.pData->zMalloc = 0;
54697
- }
54698
- }else{
54699
- u.bi.pC->pData = sqlite3Malloc( u.bi.pC->nData+2 );
54700
- if( !u.bi.pC->pData ) goto no_mem;
54701
- memcpy(u.bi.pC->pData, u.bi.pData->z, u.bi.pC->nData);
54702
- u.bi.pC->pData[u.bi.pC->nData] = 0;
54703
- u.bi.pC->pData[u.bi.pC->nData+1] = 0;
54704
- }
54705
- u.bi.pC->nullRow = 0;
54706
- }else{
54707
- u.bi.seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? u.bi.pC->seekResult : 0);
54708
- if( u.bi.pData->flags & MEM_Zero ){
54709
- u.bi.nZero = u.bi.pData->u.nZero;
54710
- }else{
54711
- u.bi.nZero = 0;
54712
- }
54713
- sqlite3BtreeSetCachedRowid(u.bi.pC->pCursor, 0);
54714
- rc = sqlite3BtreeInsert(u.bi.pC->pCursor, 0, u.bi.iKey,
54715
- u.bi.pData->z, u.bi.pData->n, u.bi.nZero,
54716
- pOp->p5 & OPFLAG_APPEND, u.bi.seekResult
54562
+ if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = u.bg.pKey->u.i;
54563
+ if( u.bg.pData->flags & MEM_Null ){
54564
+ u.bg.pData->z = 0;
54565
+ u.bg.pData->n = 0;
54566
+ }else{
54567
+ assert( u.bg.pData->flags & (MEM_Blob|MEM_Str) );
54568
+ }
54569
+ if( u.bg.pC->pseudoTable ){
54570
+ if( !u.bg.pC->ephemPseudoTable ){
54571
+ sqlite3DbFree(db, u.bg.pC->pData);
54572
+ }
54573
+ u.bg.pC->iKey = u.bg.iKey;
54574
+ u.bg.pC->nData = u.bg.pData->n;
54575
+ if( u.bg.pC->ephemPseudoTable || u.bg.pData->z==u.bg.pData->zMalloc ){
54576
+ u.bg.pC->pData = u.bg.pData->z;
54577
+ if( !u.bg.pC->ephemPseudoTable ){
54578
+ u.bg.pData->flags &= ~MEM_Dyn;
54579
+ u.bg.pData->flags |= MEM_Ephem;
54580
+ u.bg.pData->zMalloc = 0;
54581
+ }
54582
+ }else{
54583
+ u.bg.pC->pData = sqlite3Malloc( u.bg.pC->nData+2 );
54584
+ if( !u.bg.pC->pData ) goto no_mem;
54585
+ memcpy(u.bg.pC->pData, u.bg.pData->z, u.bg.pC->nData);
54586
+ u.bg.pC->pData[u.bg.pC->nData] = 0;
54587
+ u.bg.pC->pData[u.bg.pC->nData+1] = 0;
54588
+ }
54589
+ u.bg.pC->nullRow = 0;
54590
+ }else{
54591
+ u.bg.seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? u.bg.pC->seekResult : 0);
54592
+ if( u.bg.pData->flags & MEM_Zero ){
54593
+ u.bg.nZero = u.bg.pData->u.nZero;
54594
+ }else{
54595
+ u.bg.nZero = 0;
54596
+ }
54597
+ sqlite3BtreeSetCachedRowid(u.bg.pC->pCursor, 0);
54598
+ rc = sqlite3BtreeInsert(u.bg.pC->pCursor, 0, u.bg.iKey,
54599
+ u.bg.pData->z, u.bg.pData->n, u.bg.nZero,
54600
+ pOp->p5 & OPFLAG_APPEND, u.bg.seekResult
5471754601
);
5471854602
}
5471954603
54720
- u.bi.pC->rowidIsValid = 0;
54721
- u.bi.pC->deferredMoveto = 0;
54722
- u.bi.pC->cacheStatus = CACHE_STALE;
54604
+ u.bg.pC->rowidIsValid = 0;
54605
+ u.bg.pC->deferredMoveto = 0;
54606
+ u.bg.pC->cacheStatus = CACHE_STALE;
5472354607
5472454608
/* Invoke the update-hook if required. */
5472554609
if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z ){
54726
- u.bi.zDb = db->aDb[u.bi.pC->iDb].zName;
54727
- u.bi.zTbl = pOp->p4.z;
54728
- u.bi.op = ((pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT);
54729
- assert( u.bi.pC->isTable );
54730
- db->xUpdateCallback(db->pUpdateArg, u.bi.op, u.bi.zDb, u.bi.zTbl, u.bi.iKey);
54731
- assert( u.bi.pC->iDb>=0 );
54610
+ u.bg.zDb = db->aDb[u.bg.pC->iDb].zName;
54611
+ u.bg.zTbl = pOp->p4.z;
54612
+ u.bg.op = ((pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT);
54613
+ assert( u.bg.pC->isTable );
54614
+ db->xUpdateCallback(db->pUpdateArg, u.bg.op, u.bg.zDb, u.bg.zTbl, u.bg.iKey);
54615
+ assert( u.bg.pC->iDb>=0 );
5473254616
}
5473354617
break;
5473454618
}
5473554619
5473654620
/* Opcode: Delete P1 P2 * P4 *
@@ -54752,44 +54636,51 @@
5475254636
** pointing to. The update hook will be invoked, if it exists.
5475354637
** If P4 is not NULL then the P1 cursor must have been positioned
5475454638
** using OP_NotFound prior to invoking this opcode.
5475554639
*/
5475654640
case OP_Delete: {
54757
-#if 0 /* local variables moved into u.bj */
54758
- int i;
54641
+#if 0 /* local variables moved into u.bh */
5475954642
i64 iKey;
5476054643
VdbeCursor *pC;
54761
-#endif /* local variables moved into u.bj */
54762
-
54763
- u.bj.i = pOp->p1;
54764
- u.bj.iKey = 0;
54765
- assert( u.bj.i>=0 && u.bj.i<p->nCursor );
54766
- u.bj.pC = p->apCsr[u.bj.i];
54767
- assert( u.bj.pC!=0 );
54768
- assert( u.bj.pC->pCursor!=0 ); /* Only valid for real tables, no pseudotables */
54769
-
54770
- /* If the update-hook will be invoked, set u.bj.iKey to the rowid of the
54644
+#endif /* local variables moved into u.bh */
54645
+
54646
+ u.bh.iKey = 0;
54647
+ assert( pOp->p1>=0 && pOp->p1<p->nCursor );
54648
+ u.bh.pC = p->apCsr[pOp->p1];
54649
+ assert( u.bh.pC!=0 );
54650
+ assert( u.bh.pC->pCursor!=0 ); /* Only valid for real tables, no pseudotables */
54651
+
54652
+ /* If the update-hook will be invoked, set u.bh.iKey to the rowid of the
5477154653
** row being deleted.
5477254654
*/
5477354655
if( db->xUpdateCallback && pOp->p4.z ){
54774
- assert( u.bj.pC->isTable );
54775
- assert( u.bj.pC->rowidIsValid ); /* lastRowid set by previous OP_NotFound */
54776
- u.bj.iKey = u.bj.pC->lastRowid;
54656
+ assert( u.bh.pC->isTable );
54657
+ assert( u.bh.pC->rowidIsValid ); /* lastRowid set by previous OP_NotFound */
54658
+ u.bh.iKey = u.bh.pC->lastRowid;
5477754659
}
5477854660
54779
- rc = sqlite3VdbeCursorMoveto(u.bj.pC);
54780
- if( rc ) goto abort_due_to_error;
54781
- sqlite3BtreeSetCachedRowid(u.bj.pC->pCursor, 0);
54782
- rc = sqlite3BtreeDelete(u.bj.pC->pCursor);
54783
- u.bj.pC->cacheStatus = CACHE_STALE;
54661
+ /* The OP_Delete opcode always follows an OP_NotExists or OP_Last or
54662
+ ** OP_Column on the same table without any intervening operations that
54663
+ ** might move or invalidate the cursor. Hence cursor u.bh.pC is always pointing
54664
+ ** to the row to be deleted and the sqlite3VdbeCursorMoveto() operation
54665
+ ** below is always a no-op and cannot fail. We will run it anyhow, though,
54666
+ ** to guard against future changes to the code generator.
54667
+ **/
54668
+ assert( u.bh.pC->deferredMoveto==0 );
54669
+ rc = sqlite3VdbeCursorMoveto(u.bh.pC);
54670
+ if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error;
54671
+
54672
+ sqlite3BtreeSetCachedRowid(u.bh.pC->pCursor, 0);
54673
+ rc = sqlite3BtreeDelete(u.bh.pC->pCursor);
54674
+ u.bh.pC->cacheStatus = CACHE_STALE;
5478454675
5478554676
/* Invoke the update-hook if required. */
5478654677
if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z ){
54787
- const char *zDb = db->aDb[u.bj.pC->iDb].zName;
54678
+ const char *zDb = db->aDb[u.bh.pC->iDb].zName;
5478854679
const char *zTbl = pOp->p4.z;
54789
- db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, zTbl, u.bj.iKey);
54790
- assert( u.bj.pC->iDb>=0 );
54680
+ db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, zTbl, u.bh.iKey);
54681
+ assert( u.bh.pC->iDb>=0 );
5479154682
}
5479254683
if( pOp->p2 & OPFLAG_NCHANGE ) p->nChange++;
5479354684
break;
5479454685
}
5479554686
@@ -54828,55 +54719,61 @@
5482854719
** If the P1 cursor must be pointing to a valid row (not a NULL row)
5482954720
** of a real table, not a pseudo-table.
5483054721
*/
5483154722
case OP_RowKey:
5483254723
case OP_RowData: {
54833
-#if 0 /* local variables moved into u.bk */
54834
- int i;
54724
+#if 0 /* local variables moved into u.bi */
5483554725
VdbeCursor *pC;
5483654726
BtCursor *pCrsr;
5483754727
u32 n;
5483854728
i64 n64;
54839
-#endif /* local variables moved into u.bk */
54729
+#endif /* local variables moved into u.bi */
5484054730
54841
- u.bk.i = pOp->p1;
5484254731
pOut = &p->aMem[pOp->p2];
5484354732
5484454733
/* Note that RowKey and RowData are really exactly the same instruction */
54845
- assert( u.bk.i>=0 && u.bk.i<p->nCursor );
54846
- u.bk.pC = p->apCsr[u.bk.i];
54847
- assert( u.bk.pC->isTable || pOp->opcode==OP_RowKey );
54848
- assert( u.bk.pC->isIndex || pOp->opcode==OP_RowData );
54849
- assert( u.bk.pC!=0 );
54850
- assert( u.bk.pC->nullRow==0 );
54851
- assert( u.bk.pC->pseudoTable==0 );
54852
- assert( u.bk.pC->pCursor!=0 );
54853
- u.bk.pCrsr = u.bk.pC->pCursor;
54854
- rc = sqlite3VdbeCursorMoveto(u.bk.pC);
54855
- if( rc ) goto abort_due_to_error;
54856
- if( u.bk.pC->isIndex ){
54857
- assert( !u.bk.pC->isTable );
54858
- sqlite3BtreeKeySize(u.bk.pCrsr, &u.bk.n64);
54859
- if( u.bk.n64>db->aLimit[SQLITE_LIMIT_LENGTH] ){
54860
- goto too_big;
54861
- }
54862
- u.bk.n = (u32)u.bk.n64;
54863
- }else{
54864
- sqlite3BtreeDataSize(u.bk.pCrsr, &u.bk.n);
54865
- if( u.bk.n>db->aLimit[SQLITE_LIMIT_LENGTH] ){
54866
- goto too_big;
54867
- }
54868
- }
54869
- if( sqlite3VdbeMemGrow(pOut, u.bk.n, 0) ){
54870
- goto no_mem;
54871
- }
54872
- pOut->n = u.bk.n;
54873
- MemSetTypeFlag(pOut, MEM_Blob);
54874
- if( u.bk.pC->isIndex ){
54875
- rc = sqlite3BtreeKey(u.bk.pCrsr, 0, u.bk.n, pOut->z);
54876
- }else{
54877
- rc = sqlite3BtreeData(u.bk.pCrsr, 0, u.bk.n, pOut->z);
54734
+ assert( pOp->p1>=0 && pOp->p1<p->nCursor );
54735
+ u.bi.pC = p->apCsr[pOp->p1];
54736
+ assert( u.bi.pC->isTable || pOp->opcode==OP_RowKey );
54737
+ assert( u.bi.pC->isIndex || pOp->opcode==OP_RowData );
54738
+ assert( u.bi.pC!=0 );
54739
+ assert( u.bi.pC->nullRow==0 );
54740
+ assert( u.bi.pC->pseudoTable==0 );
54741
+ assert( u.bi.pC->pCursor!=0 );
54742
+ u.bi.pCrsr = u.bi.pC->pCursor;
54743
+
54744
+ /* The OP_RowKey and OP_RowData opcodes always follow OP_NotExists or
54745
+ ** OP_Rewind/Op_Next with no intervening instructions that might invalidate
54746
+ ** the cursor. Hence the following sqlite3VdbeCursorMoveto() call is always
54747
+ ** a no-op and can never fail. But we leave it in place as a safety.
54748
+ */
54749
+ assert( u.bi.pC->deferredMoveto==0 );
54750
+ rc = sqlite3VdbeCursorMoveto(u.bi.pC);
54751
+ if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error;
54752
+
54753
+ if( u.bi.pC->isIndex ){
54754
+ assert( !u.bi.pC->isTable );
54755
+ sqlite3BtreeKeySize(u.bi.pCrsr, &u.bi.n64);
54756
+ if( u.bi.n64>db->aLimit[SQLITE_LIMIT_LENGTH] ){
54757
+ goto too_big;
54758
+ }
54759
+ u.bi.n = (u32)u.bi.n64;
54760
+ }else{
54761
+ sqlite3BtreeDataSize(u.bi.pCrsr, &u.bi.n);
54762
+ if( u.bi.n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
54763
+ goto too_big;
54764
+ }
54765
+ }
54766
+ if( sqlite3VdbeMemGrow(pOut, u.bi.n, 0) ){
54767
+ goto no_mem;
54768
+ }
54769
+ pOut->n = u.bi.n;
54770
+ MemSetTypeFlag(pOut, MEM_Blob);
54771
+ if( u.bi.pC->isIndex ){
54772
+ rc = sqlite3BtreeKey(u.bi.pCrsr, 0, u.bi.n, pOut->z);
54773
+ }else{
54774
+ rc = sqlite3BtreeData(u.bi.pCrsr, 0, u.bi.n, pOut->z);
5487854775
}
5487954776
pOut->enc = SQLITE_UTF8; /* In case the blob is ever cast to text */
5488054777
UPDATE_MAX_BLOBSIZE(pOut);
5488154778
break;
5488254779
}
@@ -54889,53 +54786,50 @@
5488954786
** P1 can be either an ordinary table or a virtual table. There used to
5489054787
** be a separate OP_VRowid opcode for use with virtual tables, but this
5489154788
** one opcode now works for both table types.
5489254789
*/
5489354790
case OP_Rowid: { /* out2-prerelease */
54894
-#if 0 /* local variables moved into u.bl */
54895
- int i;
54791
+#if 0 /* local variables moved into u.bj */
5489654792
VdbeCursor *pC;
5489754793
i64 v;
5489854794
sqlite3_vtab *pVtab;
5489954795
const sqlite3_module *pModule;
54900
-#endif /* local variables moved into u.bl */
54796
+#endif /* local variables moved into u.bj */
5490154797
54902
- u.bl.i = pOp->p1;
54903
- assert( u.bl.i>=0 && u.bl.i<p->nCursor );
54904
- u.bl.pC = p->apCsr[u.bl.i];
54905
- assert( u.bl.pC!=0 );
54906
- if( u.bl.pC->nullRow ){
54798
+ assert( pOp->p1>=0 && pOp->p1<p->nCursor );
54799
+ u.bj.pC = p->apCsr[pOp->p1];
54800
+ assert( u.bj.pC!=0 );
54801
+ if( u.bj.pC->nullRow ){
5490754802
/* Do nothing so that reg[P2] remains NULL */
5490854803
break;
54909
- }else if( u.bl.pC->deferredMoveto ){
54910
- u.bl.v = u.bl.pC->movetoTarget;
54911
- }else if( u.bl.pC->pseudoTable ){
54912
- u.bl.v = keyToInt(u.bl.pC->iKey);
54804
+ }else if( u.bj.pC->deferredMoveto ){
54805
+ u.bj.v = u.bj.pC->movetoTarget;
54806
+ }else if( u.bj.pC->pseudoTable ){
54807
+ u.bj.v = u.bj.pC->iKey;
5491354808
#ifndef SQLITE_OMIT_VIRTUALTABLE
54914
- }else if( u.bl.pC->pVtabCursor ){
54915
- u.bl.pVtab = u.bl.pC->pVtabCursor->pVtab;
54916
- u.bl.pModule = u.bl.pVtab->pModule;
54917
- assert( u.bl.pModule->xRowid );
54809
+ }else if( u.bj.pC->pVtabCursor ){
54810
+ u.bj.pVtab = u.bj.pC->pVtabCursor->pVtab;
54811
+ u.bj.pModule = u.bj.pVtab->pModule;
54812
+ assert( u.bj.pModule->xRowid );
5491854813
if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
54919
- rc = u.bl.pModule->xRowid(u.bl.pC->pVtabCursor, &u.bl.v);
54814
+ rc = u.bj.pModule->xRowid(u.bj.pC->pVtabCursor, &u.bj.v);
5492054815
sqlite3DbFree(db, p->zErrMsg);
54921
- p->zErrMsg = u.bl.pVtab->zErrMsg;
54922
- u.bl.pVtab->zErrMsg = 0;
54816
+ p->zErrMsg = u.bj.pVtab->zErrMsg;
54817
+ u.bj.pVtab->zErrMsg = 0;
5492354818
if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
5492454819
#endif /* SQLITE_OMIT_VIRTUALTABLE */
5492554820
}else{
54926
- rc = sqlite3VdbeCursorMoveto(u.bl.pC);
54821
+ rc = sqlite3VdbeCursorMoveto(u.bj.pC);
5492754822
if( rc ) goto abort_due_to_error;
54928
- if( u.bl.pC->rowidIsValid ){
54929
- u.bl.v = u.bl.pC->lastRowid;
54823
+ if( u.bj.pC->rowidIsValid ){
54824
+ u.bj.v = u.bj.pC->lastRowid;
5493054825
}else{
54931
- assert( u.bl.pC->pCursor!=0 );
54932
- sqlite3BtreeKeySize(u.bl.pC->pCursor, &u.bl.v);
54933
- u.bl.v = keyToInt(u.bl.v);
54826
+ assert( u.bj.pC->pCursor!=0 );
54827
+ sqlite3BtreeKeySize(u.bj.pC->pCursor, &u.bj.v);
5493454828
}
5493554829
}
54936
- pOut->u.i = u.bl.v;
54830
+ pOut->u.i = u.bj.v;
5493754831
MemSetTypeFlag(pOut, MEM_Int);
5493854832
break;
5493954833
}
5494054834
5494154835
/* Opcode: NullRow P1 * * * *
@@ -54943,23 +54837,21 @@
5494354837
** Move the cursor P1 to a null row. Any OP_Column operations
5494454838
** that occur while the cursor is on the null row will always
5494554839
** write a NULL.
5494654840
*/
5494754841
case OP_NullRow: {
54948
-#if 0 /* local variables moved into u.bm */
54949
- int i;
54842
+#if 0 /* local variables moved into u.bk */
5495054843
VdbeCursor *pC;
54951
-#endif /* local variables moved into u.bm */
54952
-
54953
- u.bm.i = pOp->p1;
54954
- assert( u.bm.i>=0 && u.bm.i<p->nCursor );
54955
- u.bm.pC = p->apCsr[u.bm.i];
54956
- assert( u.bm.pC!=0 );
54957
- u.bm.pC->nullRow = 1;
54958
- u.bm.pC->rowidIsValid = 0;
54959
- if( u.bm.pC->pCursor ){
54960
- sqlite3BtreeClearCursor(u.bm.pC->pCursor);
54844
+#endif /* local variables moved into u.bk */
54845
+
54846
+ assert( pOp->p1>=0 && pOp->p1<p->nCursor );
54847
+ u.bk.pC = p->apCsr[pOp->p1];
54848
+ assert( u.bk.pC!=0 );
54849
+ u.bk.pC->nullRow = 1;
54850
+ u.bk.pC->rowidIsValid = 0;
54851
+ if( u.bk.pC->pCursor ){
54852
+ sqlite3BtreeClearCursor(u.bk.pC->pCursor);
5496154853
}
5496254854
break;
5496354855
}
5496454856
5496554857
/* Opcode: Last P1 P2 * * *
@@ -54969,29 +54861,30 @@
5496954861
** If the table or index is empty and P2>0, then jump immediately to P2.
5497054862
** If P2 is 0 or if the table or index is not empty, fall through
5497154863
** to the following instruction.
5497254864
*/
5497354865
case OP_Last: { /* jump */
54974
-#if 0 /* local variables moved into u.bn */
54975
- int i;
54866
+#if 0 /* local variables moved into u.bl */
5497654867
VdbeCursor *pC;
5497754868
BtCursor *pCrsr;
5497854869
int res;
54979
-#endif /* local variables moved into u.bn */
54980
-
54981
- u.bn.i = pOp->p1;
54982
- assert( u.bn.i>=0 && u.bn.i<p->nCursor );
54983
- u.bn.pC = p->apCsr[u.bn.i];
54984
- assert( u.bn.pC!=0 );
54985
- u.bn.pCrsr = u.bn.pC->pCursor;
54986
- assert( u.bn.pCrsr!=0 );
54987
- rc = sqlite3BtreeLast(u.bn.pCrsr, &u.bn.res);
54988
- u.bn.pC->nullRow = (u8)u.bn.res;
54989
- u.bn.pC->deferredMoveto = 0;
54990
- u.bn.pC->rowidIsValid = 0;
54991
- u.bn.pC->cacheStatus = CACHE_STALE;
54992
- if( u.bn.res && pOp->p2>0 ){
54870
+#endif /* local variables moved into u.bl */
54871
+
54872
+ assert( pOp->p1>=0 && pOp->p1<p->nCursor );
54873
+ u.bl.pC = p->apCsr[pOp->p1];
54874
+ assert( u.bl.pC!=0 );
54875
+ u.bl.pCrsr = u.bl.pC->pCursor;
54876
+ if( u.bl.pCrsr==0 ){
54877
+ u.bl.res = 1;
54878
+ }else{
54879
+ rc = sqlite3BtreeLast(u.bl.pCrsr, &u.bl.res);
54880
+ }
54881
+ u.bl.pC->nullRow = (u8)u.bl.res;
54882
+ u.bl.pC->deferredMoveto = 0;
54883
+ u.bl.pC->rowidIsValid = 0;
54884
+ u.bl.pC->cacheStatus = CACHE_STALE;
54885
+ if( pOp->p2>0 && u.bl.res ){
5499354886
pc = pOp->p2 - 1;
5499454887
}
5499554888
break;
5499654889
}
5499754890
@@ -55023,33 +54916,31 @@
5502354916
** If the table or index is empty and P2>0, then jump immediately to P2.
5502454917
** If P2 is 0 or if the table or index is not empty, fall through
5502554918
** to the following instruction.
5502654919
*/
5502754920
case OP_Rewind: { /* jump */
55028
-#if 0 /* local variables moved into u.bo */
55029
- int i;
54921
+#if 0 /* local variables moved into u.bm */
5503054922
VdbeCursor *pC;
5503154923
BtCursor *pCrsr;
5503254924
int res;
55033
-#endif /* local variables moved into u.bo */
55034
-
55035
- u.bo.i = pOp->p1;
55036
- assert( u.bo.i>=0 && u.bo.i<p->nCursor );
55037
- u.bo.pC = p->apCsr[u.bo.i];
55038
- assert( u.bo.pC!=0 );
55039
- if( (u.bo.pCrsr = u.bo.pC->pCursor)!=0 ){
55040
- rc = sqlite3BtreeFirst(u.bo.pCrsr, &u.bo.res);
55041
- u.bo.pC->atFirst = u.bo.res==0 ?1:0;
55042
- u.bo.pC->deferredMoveto = 0;
55043
- u.bo.pC->cacheStatus = CACHE_STALE;
55044
- u.bo.pC->rowidIsValid = 0;
55045
- }else{
55046
- u.bo.res = 1;
55047
- }
55048
- u.bo.pC->nullRow = (u8)u.bo.res;
54925
+#endif /* local variables moved into u.bm */
54926
+
54927
+ assert( pOp->p1>=0 && pOp->p1<p->nCursor );
54928
+ u.bm.pC = p->apCsr[pOp->p1];
54929
+ assert( u.bm.pC!=0 );
54930
+ if( (u.bm.pCrsr = u.bm.pC->pCursor)!=0 ){
54931
+ rc = sqlite3BtreeFirst(u.bm.pCrsr, &u.bm.res);
54932
+ u.bm.pC->atFirst = u.bm.res==0 ?1:0;
54933
+ u.bm.pC->deferredMoveto = 0;
54934
+ u.bm.pC->cacheStatus = CACHE_STALE;
54935
+ u.bm.pC->rowidIsValid = 0;
54936
+ }else{
54937
+ u.bm.res = 1;
54938
+ }
54939
+ u.bm.pC->nullRow = (u8)u.bm.res;
5504954940
assert( pOp->p2>0 && pOp->p2<p->nOp );
55050
- if( u.bo.res ){
54941
+ if( u.bm.res ){
5505154942
pc = pOp->p2 - 1;
5505254943
}
5505354944
break;
5505454945
}
5505554946
@@ -55073,38 +54964,41 @@
5507354964
**
5507454965
** The P1 cursor must be for a real table, not a pseudo-table.
5507554966
*/
5507654967
case OP_Prev: /* jump */
5507754968
case OP_Next: { /* jump */
55078
-#if 0 /* local variables moved into u.bp */
54969
+#if 0 /* local variables moved into u.bn */
5507954970
VdbeCursor *pC;
5508054971
BtCursor *pCrsr;
5508154972
int res;
55082
-#endif /* local variables moved into u.bp */
54973
+#endif /* local variables moved into u.bn */
5508354974
5508454975
CHECK_FOR_INTERRUPT;
5508554976
assert( pOp->p1>=0 && pOp->p1<p->nCursor );
55086
- u.bp.pC = p->apCsr[pOp->p1];
55087
- if( u.bp.pC==0 ){
54977
+ u.bn.pC = p->apCsr[pOp->p1];
54978
+ if( u.bn.pC==0 ){
5508854979
break; /* See ticket #2273 */
5508954980
}
55090
- u.bp.pCrsr = u.bp.pC->pCursor;
55091
- assert( u.bp.pCrsr );
55092
- u.bp.res = 1;
55093
- assert( u.bp.pC->deferredMoveto==0 );
55094
- rc = pOp->opcode==OP_Next ? sqlite3BtreeNext(u.bp.pCrsr, &u.bp.res) :
55095
- sqlite3BtreePrevious(u.bp.pCrsr, &u.bp.res);
55096
- u.bp.pC->nullRow = (u8)u.bp.res;
55097
- u.bp.pC->cacheStatus = CACHE_STALE;
55098
- if( u.bp.res==0 ){
54981
+ u.bn.pCrsr = u.bn.pC->pCursor;
54982
+ if( u.bn.pCrsr==0 ){
54983
+ u.bn.pC->nullRow = 1;
54984
+ break;
54985
+ }
54986
+ u.bn.res = 1;
54987
+ assert( u.bn.pC->deferredMoveto==0 );
54988
+ rc = pOp->opcode==OP_Next ? sqlite3BtreeNext(u.bn.pCrsr, &u.bn.res) :
54989
+ sqlite3BtreePrevious(u.bn.pCrsr, &u.bn.res);
54990
+ u.bn.pC->nullRow = (u8)u.bn.res;
54991
+ u.bn.pC->cacheStatus = CACHE_STALE;
54992
+ if( u.bn.res==0 ){
5509954993
pc = pOp->p2 - 1;
5510054994
if( pOp->p5 ) p->aCounter[pOp->p5-1]++;
5510154995
#ifdef SQLITE_TEST
5510254996
sqlite3_search_count++;
5510354997
#endif
5510454998
}
55105
- u.bp.pC->rowidIsValid = 0;
54999
+ u.bn.pC->rowidIsValid = 0;
5510655000
break;
5510755001
}
5510855002
5510955003
/* Opcode: IdxInsert P1 P2 P3 * P5
5511055004
**
@@ -55117,33 +55011,33 @@
5511755011
**
5511855012
** This instruction only works for indices. The equivalent instruction
5511955013
** for tables is OP_Insert.
5512055014
*/
5512155015
case OP_IdxInsert: { /* in2 */
55122
-#if 0 /* local variables moved into u.bq */
55123
- int i;
55016
+#if 0 /* local variables moved into u.bo */
5512455017
VdbeCursor *pC;
5512555018
BtCursor *pCrsr;
5512655019
int nKey;
5512755020
const char *zKey;
55128
-#endif /* local variables moved into u.bq */
55021
+#endif /* local variables moved into u.bo */
5512955022
55130
- u.bq.i = pOp->p1;
55131
- assert( u.bq.i>=0 && u.bq.i<p->nCursor );
55132
- assert( p->apCsr[u.bq.i]!=0 );
55023
+ assert( pOp->p1>=0 && pOp->p1<p->nCursor );
55024
+ u.bo.pC = p->apCsr[pOp->p1];
55025
+ assert( u.bo.pC!=0 );
5513355026
assert( pIn2->flags & MEM_Blob );
55134
- if( (u.bq.pCrsr = (u.bq.pC = p->apCsr[u.bq.i])->pCursor)!=0 ){
55135
- assert( u.bq.pC->isTable==0 );
55027
+ u.bo.pCrsr = u.bo.pC->pCursor;
55028
+ if( ALWAYS(u.bo.pCrsr!=0) ){
55029
+ assert( u.bo.pC->isTable==0 );
5513655030
rc = ExpandBlob(pIn2);
5513755031
if( rc==SQLITE_OK ){
55138
- u.bq.nKey = pIn2->n;
55139
- u.bq.zKey = pIn2->z;
55140
- rc = sqlite3BtreeInsert(u.bq.pCrsr, u.bq.zKey, u.bq.nKey, "", 0, 0, pOp->p3,
55141
- ((pOp->p5 & OPFLAG_USESEEKRESULT) ? u.bq.pC->seekResult : 0)
55032
+ u.bo.nKey = pIn2->n;
55033
+ u.bo.zKey = pIn2->z;
55034
+ rc = sqlite3BtreeInsert(u.bo.pCrsr, u.bo.zKey, u.bo.nKey, "", 0, 0, pOp->p3,
55035
+ ((pOp->p5 & OPFLAG_USESEEKRESULT) ? u.bo.pC->seekResult : 0)
5514255036
);
55143
- assert( u.bq.pC->deferredMoveto==0 );
55144
- u.bq.pC->cacheStatus = CACHE_STALE;
55037
+ assert( u.bo.pC->deferredMoveto==0 );
55038
+ u.bo.pC->cacheStatus = CACHE_STALE;
5514555039
}
5514655040
}
5514755041
break;
5514855042
}
5514955043
@@ -55152,34 +55046,34 @@
5515255046
** The content of P3 registers starting at register P2 form
5515355047
** an unpacked index key. This opcode removes that entry from the
5515455048
** index opened by cursor P1.
5515555049
*/
5515655050
case OP_IdxDelete: {
55157
-#if 0 /* local variables moved into u.br */
55158
- int i;
55051
+#if 0 /* local variables moved into u.bp */
5515955052
VdbeCursor *pC;
5516055053
BtCursor *pCrsr;
55161
-#endif /* local variables moved into u.br */
55054
+ int res;
55055
+ UnpackedRecord r;
55056
+#endif /* local variables moved into u.bp */
5516255057
55163
- u.br.i = pOp->p1;
5516455058
assert( pOp->p3>0 );
5516555059
assert( pOp->p2>0 && pOp->p2+pOp->p3<=p->nMem+1 );
55166
- assert( u.br.i>=0 && u.br.i<p->nCursor );
55167
- assert( p->apCsr[u.br.i]!=0 );
55168
- if( (u.br.pCrsr = (u.br.pC = p->apCsr[u.br.i])->pCursor)!=0 ){
55169
- int res;
55170
- UnpackedRecord r;
55171
- r.pKeyInfo = u.br.pC->pKeyInfo;
55172
- r.nField = (u16)pOp->p3;
55173
- r.flags = 0;
55174
- r.aMem = &p->aMem[pOp->p2];
55175
- rc = sqlite3BtreeMovetoUnpacked(u.br.pCrsr, &r, 0, 0, &res);
55176
- if( rc==SQLITE_OK && res==0 ){
55177
- rc = sqlite3BtreeDelete(u.br.pCrsr);
55178
- }
55179
- assert( u.br.pC->deferredMoveto==0 );
55180
- u.br.pC->cacheStatus = CACHE_STALE;
55060
+ assert( pOp->p1>=0 && pOp->p1<p->nCursor );
55061
+ u.bp.pC = p->apCsr[pOp->p1];
55062
+ assert( u.bp.pC!=0 );
55063
+ u.bp.pCrsr = u.bp.pC->pCursor;
55064
+ if( ALWAYS(u.bp.pCrsr!=0) ){
55065
+ u.bp.r.pKeyInfo = u.bp.pC->pKeyInfo;
55066
+ u.bp.r.nField = (u16)pOp->p3;
55067
+ u.bp.r.flags = 0;
55068
+ u.bp.r.aMem = &p->aMem[pOp->p2];
55069
+ rc = sqlite3BtreeMovetoUnpacked(u.bp.pCrsr, &u.bp.r, 0, 0, &u.bp.res);
55070
+ if( rc==SQLITE_OK && u.bp.res==0 ){
55071
+ rc = sqlite3BtreeDelete(u.bp.pCrsr);
55072
+ }
55073
+ assert( u.bp.pC->deferredMoveto==0 );
55074
+ u.bp.pC->cacheStatus = CACHE_STALE;
5518155075
}
5518255076
break;
5518355077
}
5518455078
5518555079
/* Opcode: IdxRowid P1 P2 * * *
@@ -55189,32 +55083,32 @@
5518955083
** the rowid of the table entry to which this index entry points.
5519055084
**
5519155085
** See also: Rowid, MakeRecord.
5519255086
*/
5519355087
case OP_IdxRowid: { /* out2-prerelease */
55194
-#if 0 /* local variables moved into u.bs */
55195
- int i;
55088
+#if 0 /* local variables moved into u.bq */
5519655089
BtCursor *pCrsr;
5519755090
VdbeCursor *pC;
5519855091
i64 rowid;
55199
-#endif /* local variables moved into u.bs */
55200
-
55201
- u.bs.i = pOp->p1;
55202
- assert( u.bs.i>=0 && u.bs.i<p->nCursor );
55203
- assert( p->apCsr[u.bs.i]!=0 );
55204
- if( (u.bs.pCrsr = (u.bs.pC = p->apCsr[u.bs.i])->pCursor)!=0 ){
55205
- rc = sqlite3VdbeCursorMoveto(u.bs.pC);
55206
- if( rc ) goto abort_due_to_error;
55207
- assert( u.bs.pC->deferredMoveto==0 );
55208
- assert( u.bs.pC->isTable==0 );
55209
- if( !u.bs.pC->nullRow ){
55210
- rc = sqlite3VdbeIdxRowid(u.bs.pCrsr, &u.bs.rowid);
55092
+#endif /* local variables moved into u.bq */
55093
+
55094
+ assert( pOp->p1>=0 && pOp->p1<p->nCursor );
55095
+ u.bq.pC = p->apCsr[pOp->p1];
55096
+ assert( u.bq.pC!=0 );
55097
+ u.bq.pCrsr = u.bq.pC->pCursor;
55098
+ if( ALWAYS(u.bq.pCrsr!=0) ){
55099
+ rc = sqlite3VdbeCursorMoveto(u.bq.pC);
55100
+ if( NEVER(rc) ) goto abort_due_to_error;
55101
+ assert( u.bq.pC->deferredMoveto==0 );
55102
+ assert( u.bq.pC->isTable==0 );
55103
+ if( !u.bq.pC->nullRow ){
55104
+ rc = sqlite3VdbeIdxRowid(db, u.bq.pCrsr, &u.bq.rowid);
5521155105
if( rc!=SQLITE_OK ){
5521255106
goto abort_due_to_error;
5521355107
}
5521455108
MemSetTypeFlag(pOut, MEM_Int);
55215
- pOut->u.i = u.bs.rowid;
55109
+ pOut->u.i = u.bq.rowid;
5521655110
}
5521755111
}
5521855112
break;
5521955113
}
5522055114
@@ -55244,40 +55138,39 @@
5524455138
** If P5 is non-zero then the key value is increased by an epsilon prior
5524555139
** to the comparison. This makes the opcode work like IdxLE.
5524655140
*/
5524755141
case OP_IdxLT: /* jump, in3 */
5524855142
case OP_IdxGE: { /* jump, in3 */
55249
-#if 0 /* local variables moved into u.bt */
55250
- int i;
55143
+#if 0 /* local variables moved into u.br */
5525155144
VdbeCursor *pC;
5525255145
int res;
5525355146
UnpackedRecord r;
55254
-#endif /* local variables moved into u.bt */
55147
+#endif /* local variables moved into u.br */
5525555148
55256
- u.bt.i = pOp->p1;
55257
- assert( u.bt.i>=0 && u.bt.i<p->nCursor );
55258
- assert( p->apCsr[u.bt.i]!=0 );
55259
- if( (u.bt.pC = p->apCsr[u.bt.i])->pCursor!=0 ){
55260
- assert( u.bt.pC->deferredMoveto==0 );
55149
+ assert( pOp->p1>=0 && pOp->p1<p->nCursor );
55150
+ u.br.pC = p->apCsr[pOp->p1];
55151
+ assert( u.br.pC!=0 );
55152
+ if( ALWAYS(u.br.pC->pCursor!=0) ){
55153
+ assert( u.br.pC->deferredMoveto==0 );
5526155154
assert( pOp->p5==0 || pOp->p5==1 );
5526255155
assert( pOp->p4type==P4_INT32 );
55263
- u.bt.r.pKeyInfo = u.bt.pC->pKeyInfo;
55264
- u.bt.r.nField = (u16)pOp->p4.i;
55156
+ u.br.r.pKeyInfo = u.br.pC->pKeyInfo;
55157
+ u.br.r.nField = (u16)pOp->p4.i;
5526555158
if( pOp->p5 ){
55266
- u.bt.r.flags = UNPACKED_INCRKEY | UNPACKED_IGNORE_ROWID;
55159
+ u.br.r.flags = UNPACKED_INCRKEY | UNPACKED_IGNORE_ROWID;
5526755160
}else{
55268
- u.bt.r.flags = UNPACKED_IGNORE_ROWID;
55161
+ u.br.r.flags = UNPACKED_IGNORE_ROWID;
5526955162
}
55270
- u.bt.r.aMem = &p->aMem[pOp->p3];
55271
- rc = sqlite3VdbeIdxKeyCompare(u.bt.pC, &u.bt.r, &u.bt.res);
55163
+ u.br.r.aMem = &p->aMem[pOp->p3];
55164
+ rc = sqlite3VdbeIdxKeyCompare(u.br.pC, &u.br.r, &u.br.res);
5527255165
if( pOp->opcode==OP_IdxLT ){
55273
- u.bt.res = -u.bt.res;
55166
+ u.br.res = -u.br.res;
5527455167
}else{
5527555168
assert( pOp->opcode==OP_IdxGE );
55276
- u.bt.res++;
55169
+ u.br.res++;
5527755170
}
55278
- if( u.bt.res>0 ){
55171
+ if( u.br.res>0 ){
5527955172
pc = pOp->p2 - 1 ;
5528055173
}
5528155174
}
5528255175
break;
5528355176
}
@@ -55301,39 +55194,39 @@
5530155194
** If AUTOVACUUM is disabled then a zero is stored in register P2.
5530255195
**
5530355196
** See also: Clear
5530455197
*/
5530555198
case OP_Destroy: { /* out2-prerelease */
55306
-#if 0 /* local variables moved into u.bu */
55199
+#if 0 /* local variables moved into u.bs */
5530755200
int iMoved;
5530855201
int iCnt;
5530955202
Vdbe *pVdbe;
5531055203
int iDb;
55311
-#endif /* local variables moved into u.bu */
55204
+#endif /* local variables moved into u.bs */
5531255205
#ifndef SQLITE_OMIT_VIRTUALTABLE
55313
- u.bu.iCnt = 0;
55314
- for(u.bu.pVdbe=db->pVdbe; u.bu.pVdbe; u.bu.pVdbe = u.bu.pVdbe->pNext){
55315
- if( u.bu.pVdbe->magic==VDBE_MAGIC_RUN && u.bu.pVdbe->inVtabMethod<2 && u.bu.pVdbe->pc>=0 ){
55316
- u.bu.iCnt++;
55206
+ u.bs.iCnt = 0;
55207
+ for(u.bs.pVdbe=db->pVdbe; u.bs.pVdbe; u.bs.pVdbe = u.bs.pVdbe->pNext){
55208
+ if( u.bs.pVdbe->magic==VDBE_MAGIC_RUN && u.bs.pVdbe->inVtabMethod<2 && u.bs.pVdbe->pc>=0 ){
55209
+ u.bs.iCnt++;
5531755210
}
5531855211
}
5531955212
#else
55320
- u.bu.iCnt = db->activeVdbeCnt;
55213
+ u.bs.iCnt = db->activeVdbeCnt;
5532155214
#endif
55322
- if( u.bu.iCnt>1 ){
55215
+ if( u.bs.iCnt>1 ){
5532355216
rc = SQLITE_LOCKED;
5532455217
p->errorAction = OE_Abort;
5532555218
}else{
55326
- u.bu.iDb = pOp->p3;
55327
- assert( u.bu.iCnt==1 );
55328
- assert( (p->btreeMask & (1<<u.bu.iDb))!=0 );
55329
- rc = sqlite3BtreeDropTable(db->aDb[u.bu.iDb].pBt, pOp->p1, &u.bu.iMoved);
55219
+ u.bs.iDb = pOp->p3;
55220
+ assert( u.bs.iCnt==1 );
55221
+ assert( (p->btreeMask & (1<<u.bs.iDb))!=0 );
55222
+ rc = sqlite3BtreeDropTable(db->aDb[u.bs.iDb].pBt, pOp->p1, &u.bs.iMoved);
5533055223
MemSetTypeFlag(pOut, MEM_Int);
55331
- pOut->u.i = u.bu.iMoved;
55224
+ pOut->u.i = u.bs.iMoved;
5533255225
#ifndef SQLITE_OMIT_AUTOVACUUM
55333
- if( rc==SQLITE_OK && u.bu.iMoved!=0 ){
55334
- sqlite3RootPageMoved(&db->aDb[u.bu.iDb], u.bu.iMoved, pOp->p1);
55226
+ if( rc==SQLITE_OK && u.bs.iMoved!=0 ){
55227
+ sqlite3RootPageMoved(&db->aDb[u.bs.iDb], u.bs.iMoved, pOp->p1);
5533555228
}
5533655229
#endif
5533755230
}
5533855231
break;
5533955232
}
@@ -55355,23 +55248,23 @@
5535555248
** also incremented by the number of rows in the table being cleared.
5535655249
**
5535755250
** See also: Destroy
5535855251
*/
5535955252
case OP_Clear: {
55360
-#if 0 /* local variables moved into u.bv */
55253
+#if 0 /* local variables moved into u.bt */
5536155254
int nChange;
55362
-#endif /* local variables moved into u.bv */
55255
+#endif /* local variables moved into u.bt */
5536355256
55364
- u.bv.nChange = 0;
55257
+ u.bt.nChange = 0;
5536555258
assert( (p->btreeMask & (1<<pOp->p2))!=0 );
5536655259
rc = sqlite3BtreeClearTable(
55367
- db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &u.bv.nChange : 0)
55260
+ db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &u.bt.nChange : 0)
5536855261
);
5536955262
if( pOp->p3 ){
55370
- p->nChange += u.bv.nChange;
55263
+ p->nChange += u.bt.nChange;
5537155264
if( pOp->p3>0 ){
55372
- p->aMem[pOp->p3].u.i += u.bv.nChange;
55265
+ p->aMem[pOp->p3].u.i += u.bt.nChange;
5537355266
}
5537455267
}
5537555268
break;
5537655269
}
5537755270
@@ -55397,29 +55290,29 @@
5539755290
**
5539855291
** See documentation on OP_CreateTable for additional information.
5539955292
*/
5540055293
case OP_CreateIndex: /* out2-prerelease */
5540155294
case OP_CreateTable: { /* out2-prerelease */
55402
-#if 0 /* local variables moved into u.bw */
55295
+#if 0 /* local variables moved into u.bu */
5540355296
int pgno;
5540455297
int flags;
5540555298
Db *pDb;
55406
-#endif /* local variables moved into u.bw */
55299
+#endif /* local variables moved into u.bu */
5540755300
55408
- u.bw.pgno = 0;
55301
+ u.bu.pgno = 0;
5540955302
assert( pOp->p1>=0 && pOp->p1<db->nDb );
5541055303
assert( (p->btreeMask & (1<<pOp->p1))!=0 );
55411
- u.bw.pDb = &db->aDb[pOp->p1];
55412
- assert( u.bw.pDb->pBt!=0 );
55304
+ u.bu.pDb = &db->aDb[pOp->p1];
55305
+ assert( u.bu.pDb->pBt!=0 );
5541355306
if( pOp->opcode==OP_CreateTable ){
55414
- /* u.bw.flags = BTREE_INTKEY; */
55415
- u.bw.flags = BTREE_LEAFDATA|BTREE_INTKEY;
55307
+ /* u.bu.flags = BTREE_INTKEY; */
55308
+ u.bu.flags = BTREE_LEAFDATA|BTREE_INTKEY;
5541655309
}else{
55417
- u.bw.flags = BTREE_ZERODATA;
55310
+ u.bu.flags = BTREE_ZERODATA;
5541855311
}
55419
- rc = sqlite3BtreeCreateTable(u.bw.pDb->pBt, &u.bw.pgno, u.bw.flags);
55420
- pOut->u.i = u.bw.pgno;
55312
+ rc = sqlite3BtreeCreateTable(u.bu.pDb->pBt, &u.bu.pgno, u.bu.flags);
55313
+ pOut->u.i = u.bu.pgno;
5542155314
MemSetTypeFlag(pOut, MEM_Int);
5542255315
break;
5542355316
}
5542455317
5542555318
/* Opcode: ParseSchema P1 P2 * P4 *
@@ -55433,62 +55326,62 @@
5543355326
**
5543455327
** This opcode invokes the parser to create a new virtual machine,
5543555328
** then runs the new virtual machine. It is thus a re-entrant opcode.
5543655329
*/
5543755330
case OP_ParseSchema: {
55438
-#if 0 /* local variables moved into u.bx */
55331
+#if 0 /* local variables moved into u.bv */
5543955332
int iDb;
5544055333
const char *zMaster;
5544155334
char *zSql;
5544255335
InitData initData;
55443
-#endif /* local variables moved into u.bx */
55336
+#endif /* local variables moved into u.bv */
5544455337
55445
- u.bx.iDb = pOp->p1;
55446
- assert( u.bx.iDb>=0 && u.bx.iDb<db->nDb );
55338
+ u.bv.iDb = pOp->p1;
55339
+ assert( u.bv.iDb>=0 && u.bv.iDb<db->nDb );
5544755340
5544855341
/* If pOp->p2 is 0, then this opcode is being executed to read a
5544955342
** single row, for example the row corresponding to a new index
5545055343
** created by this VDBE, from the sqlite_master table. It only
5545155344
** does this if the corresponding in-memory schema is currently
5545255345
** loaded. Otherwise, the new index definition can be loaded along
5545355346
** with the rest of the schema when it is required.
5545455347
**
5545555348
** Although the mutex on the BtShared object that corresponds to
55456
- ** database u.bx.iDb (the database containing the sqlite_master table
55349
+ ** database u.bv.iDb (the database containing the sqlite_master table
5545755350
** read by this instruction) is currently held, it is necessary to
5545855351
** obtain the mutexes on all attached databases before checking if
55459
- ** the schema of u.bx.iDb is loaded. This is because, at the start of
55352
+ ** the schema of u.bv.iDb is loaded. This is because, at the start of
5546055353
** the sqlite3_exec() call below, SQLite will invoke
5546155354
** sqlite3BtreeEnterAll(). If all mutexes are not already held, the
55462
- ** u.bx.iDb mutex may be temporarily released to avoid deadlock. If
55355
+ ** u.bv.iDb mutex may be temporarily released to avoid deadlock. If
5546355356
** this happens, then some other thread may delete the in-memory
55464
- ** schema of database u.bx.iDb before the SQL statement runs. The schema
55357
+ ** schema of database u.bv.iDb before the SQL statement runs. The schema
5546555358
** will not be reloaded becuase the db->init.busy flag is set. This
5546655359
** can result in a "no such table: sqlite_master" or "malformed
5546755360
** database schema" error being returned to the user.
5546855361
*/
55469
- assert( sqlite3BtreeHoldsMutex(db->aDb[u.bx.iDb].pBt) );
55362
+ assert( sqlite3BtreeHoldsMutex(db->aDb[u.bv.iDb].pBt) );
5547055363
sqlite3BtreeEnterAll(db);
55471
- if( pOp->p2 || DbHasProperty(db, u.bx.iDb, DB_SchemaLoaded) ){
55472
- u.bx.zMaster = SCHEMA_TABLE(u.bx.iDb);
55473
- u.bx.initData.db = db;
55474
- u.bx.initData.iDb = pOp->p1;
55475
- u.bx.initData.pzErrMsg = &p->zErrMsg;
55476
- u.bx.zSql = sqlite3MPrintf(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,
5547755370
"SELECT name, rootpage, sql FROM '%q'.%s WHERE %s",
55478
- db->aDb[u.bx.iDb].zName, u.bx.zMaster, pOp->p4.z);
55479
- if( u.bx.zSql==0 ){
55371
+ db->aDb[u.bv.iDb].zName, u.bv.zMaster, pOp->p4.z);
55372
+ if( u.bv.zSql==0 ){
5548055373
rc = SQLITE_NOMEM;
5548155374
}else{
5548255375
(void)sqlite3SafetyOff(db);
5548355376
assert( db->init.busy==0 );
5548455377
db->init.busy = 1;
55485
- u.bx.initData.rc = SQLITE_OK;
55378
+ u.bv.initData.rc = SQLITE_OK;
5548655379
assert( !db->mallocFailed );
55487
- rc = sqlite3_exec(db, u.bx.zSql, sqlite3InitCallback, &u.bx.initData, 0);
55488
- if( rc==SQLITE_OK ) rc = u.bx.initData.rc;
55489
- sqlite3DbFree(db, u.bx.zSql);
55380
+ rc = sqlite3_exec(db, u.bv.zSql, sqlite3InitCallback, &u.bv.initData, 0);
55381
+ if( rc==SQLITE_OK ) rc = u.bv.initData.rc;
55382
+ sqlite3DbFree(db, u.bv.zSql);
5549055383
db->init.busy = 0;
5549155384
(void)sqlite3SafetyOn(db);
5549255385
}
5549355386
}
5549455387
sqlite3BtreeLeaveAll(db);
@@ -55496,11 +55389,11 @@
5549655389
goto no_mem;
5549755390
}
5549855391
break;
5549955392
}
5550055393
55501
-#if !defined(SQLITE_OMIT_ANALYZE) && !defined(SQLITE_OMIT_PARSER)
55394
+#if !defined(SQLITE_OMIT_ANALYZE)
5550255395
/* Opcode: LoadAnalysis P1 * * * *
5550355396
**
5550455397
** Read the sqlite_stat1 table for database P1 and load the content
5550555398
** of that table into the internal index hash table. This will cause
5550655399
** the analysis to be used when preparing all subsequent queries.
@@ -55508,11 +55401,11 @@
5550855401
case OP_LoadAnalysis: {
5550955402
assert( pOp->p1>=0 && pOp->p1<db->nDb );
5551055403
rc = sqlite3AnalysisLoad(db, pOp->p1);
5551155404
break;
5551255405
}
55513
-#endif /* !defined(SQLITE_OMIT_ANALYZE) && !defined(SQLITE_OMIT_PARSER) */
55406
+#endif /* !defined(SQLITE_OMIT_ANALYZE) */
5551455407
5551555408
/* Opcode: DropTable P1 * * P4 *
5551655409
**
5551755410
** Remove the internal (in-memory) data structures that describe
5551855411
** the table named P4 in database P1. This is called after a table
@@ -55569,45 +55462,45 @@
5556955462
** file, not the main database file.
5557055463
**
5557155464
** This opcode is used to implement the integrity_check pragma.
5557255465
*/
5557355466
case OP_IntegrityCk: {
55574
-#if 0 /* local variables moved into u.by */
55467
+#if 0 /* local variables moved into u.bw */
5557555468
int nRoot; /* Number of tables to check. (Number of root pages.) */
5557655469
int *aRoot; /* Array of rootpage numbers for tables to be checked */
5557755470
int j; /* Loop counter */
5557855471
int nErr; /* Number of errors reported */
5557955472
char *z; /* Text of the error report */
5558055473
Mem *pnErr; /* Register keeping track of errors remaining */
55581
-#endif /* local variables moved into u.by */
55474
+#endif /* local variables moved into u.bw */
5558255475
55583
- u.by.nRoot = pOp->p2;
55584
- assert( u.by.nRoot>0 );
55585
- u.by.aRoot = sqlite3DbMallocRaw(db, sizeof(int)*(u.by.nRoot+1) );
55586
- if( u.by.aRoot==0 ) goto no_mem;
55476
+ u.bw.nRoot = pOp->p2;
55477
+ assert( u.bw.nRoot>0 );
55478
+ u.bw.aRoot = sqlite3DbMallocRaw(db, sizeof(int)*(u.bw.nRoot+1) );
55479
+ if( u.bw.aRoot==0 ) goto no_mem;
5558755480
assert( pOp->p3>0 && pOp->p3<=p->nMem );
55588
- u.by.pnErr = &p->aMem[pOp->p3];
55589
- assert( (u.by.pnErr->flags & MEM_Int)!=0 );
55590
- assert( (u.by.pnErr->flags & (MEM_Str|MEM_Blob))==0 );
55481
+ u.bw.pnErr = &p->aMem[pOp->p3];
55482
+ assert( (u.bw.pnErr->flags & MEM_Int)!=0 );
55483
+ assert( (u.bw.pnErr->flags & (MEM_Str|MEM_Blob))==0 );
5559155484
pIn1 = &p->aMem[pOp->p1];
55592
- for(u.by.j=0; u.by.j<u.by.nRoot; u.by.j++){
55593
- u.by.aRoot[u.by.j] = (int)sqlite3VdbeIntValue(&pIn1[u.by.j]);
55485
+ for(u.bw.j=0; u.bw.j<u.bw.nRoot; u.bw.j++){
55486
+ u.bw.aRoot[u.bw.j] = (int)sqlite3VdbeIntValue(&pIn1[u.bw.j]);
5559455487
}
55595
- u.by.aRoot[u.by.j] = 0;
55488
+ u.bw.aRoot[u.bw.j] = 0;
5559655489
assert( pOp->p5<db->nDb );
5559755490
assert( (p->btreeMask & (1<<pOp->p5))!=0 );
55598
- u.by.z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p5].pBt, u.by.aRoot, u.by.nRoot,
55599
- (int)u.by.pnErr->u.i, &u.by.nErr);
55600
- sqlite3DbFree(db, u.by.aRoot);
55601
- u.by.pnErr->u.i -= u.by.nErr;
55491
+ u.bw.z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p5].pBt, u.bw.aRoot, u.bw.nRoot,
55492
+ (int)u.bw.pnErr->u.i, &u.bw.nErr);
55493
+ sqlite3DbFree(db, u.bw.aRoot);
55494
+ u.bw.pnErr->u.i -= u.bw.nErr;
5560255495
sqlite3VdbeMemSetNull(pIn1);
55603
- if( u.by.nErr==0 ){
55604
- assert( u.by.z==0 );
55605
- }else if( u.by.z==0 ){
55496
+ if( u.bw.nErr==0 ){
55497
+ assert( u.bw.z==0 );
55498
+ }else if( u.bw.z==0 ){
5560655499
goto no_mem;
5560755500
}else{
55608
- sqlite3VdbeMemSetStr(pIn1, u.by.z, -1, SQLITE_UTF8, sqlite3_free);
55501
+ sqlite3VdbeMemSetStr(pIn1, u.bw.z, -1, SQLITE_UTF8, sqlite3_free);
5560955502
}
5561055503
UPDATE_MAX_BLOBSIZE(pIn1);
5561155504
sqlite3VdbeChangeEncoding(pIn1, encoding);
5561255505
break;
5561355506
}
@@ -55619,24 +55512,24 @@
5561955512
** held in register P1.
5562055513
**
5562155514
** An assertion fails if P2 is not an integer.
5562255515
*/
5562355516
case OP_RowSetAdd: { /* in2 */
55624
-#if 0 /* local variables moved into u.bz */
55517
+#if 0 /* local variables moved into u.bx */
5562555518
Mem *pIdx;
5562655519
Mem *pVal;
55627
-#endif /* local variables moved into u.bz */
55520
+#endif /* local variables moved into u.bx */
5562855521
assert( pOp->p1>0 && pOp->p1<=p->nMem );
55629
- u.bz.pIdx = &p->aMem[pOp->p1];
55522
+ u.bx.pIdx = &p->aMem[pOp->p1];
5563055523
assert( pOp->p2>0 && pOp->p2<=p->nMem );
55631
- u.bz.pVal = &p->aMem[pOp->p2];
55632
- assert( (u.bz.pVal->flags & MEM_Int)!=0 );
55633
- if( (u.bz.pIdx->flags & MEM_RowSet)==0 ){
55634
- sqlite3VdbeMemSetRowSet(u.bz.pIdx);
55635
- if( (u.bz.pIdx->flags & MEM_RowSet)==0 ) goto no_mem;
55524
+ u.bx.pVal = &p->aMem[pOp->p2];
55525
+ assert( (u.bx.pVal->flags & MEM_Int)!=0 );
55526
+ if( (u.bx.pIdx->flags & MEM_RowSet)==0 ){
55527
+ sqlite3VdbeMemSetRowSet(u.bx.pIdx);
55528
+ if( (u.bx.pIdx->flags & MEM_RowSet)==0 ) goto no_mem;
5563655529
}
55637
- sqlite3RowSetInsert(u.bz.pIdx->u.pRowSet, u.bz.pVal->u.i);
55530
+ sqlite3RowSetInsert(u.bx.pIdx->u.pRowSet, u.bx.pVal->u.i);
5563855531
break;
5563955532
}
5564055533
5564155534
/* Opcode: RowSetRead P1 P2 P3 * *
5564255535
**
@@ -55643,28 +55536,28 @@
5564355536
** Extract the smallest value from boolean index P1 and put that value into
5564455537
** register P3. Or, if boolean index P1 is initially empty, leave P3
5564555538
** unchanged and jump to instruction P2.
5564655539
*/
5564755540
case OP_RowSetRead: { /* jump, out3 */
55648
-#if 0 /* local variables moved into u.ca */
55541
+#if 0 /* local variables moved into u.by */
5564955542
Mem *pIdx;
5565055543
i64 val;
55651
-#endif /* local variables moved into u.ca */
55544
+#endif /* local variables moved into u.by */
5565255545
assert( pOp->p1>0 && pOp->p1<=p->nMem );
5565355546
CHECK_FOR_INTERRUPT;
55654
- u.ca.pIdx = &p->aMem[pOp->p1];
55547
+ u.by.pIdx = &p->aMem[pOp->p1];
5565555548
pOut = &p->aMem[pOp->p3];
55656
- if( (u.ca.pIdx->flags & MEM_RowSet)==0
55657
- || sqlite3RowSetNext(u.ca.pIdx->u.pRowSet, &u.ca.val)==0
55549
+ if( (u.by.pIdx->flags & MEM_RowSet)==0
55550
+ || sqlite3RowSetNext(u.by.pIdx->u.pRowSet, &u.by.val)==0
5565855551
){
5565955552
/* The boolean index is empty */
55660
- sqlite3VdbeMemSetNull(u.ca.pIdx);
55553
+ sqlite3VdbeMemSetNull(u.by.pIdx);
5566155554
pc = pOp->p2 - 1;
5566255555
}else{
5566355556
/* A value was pulled from the index */
5566455557
assert( pOp->p3>0 && pOp->p3<=p->nMem );
55665
- sqlite3VdbeMemSetInt64(pOut, u.ca.val);
55558
+ sqlite3VdbeMemSetInt64(pOut, u.by.val);
5566655559
}
5566755560
break;
5566855561
}
5566955562
5567055563
/* Opcode: RowSetTest P1 P2 P3 P4
@@ -55689,16 +55582,16 @@
5568955582
** inserted, there is no need to search to see if the same value was
5569055583
** previously inserted as part of set X (only if it was previously
5569155584
** inserted as part of some other set).
5569255585
*/
5569355586
case OP_RowSetTest: { /* jump, in1, in3 */
55694
-#if 0 /* local variables moved into u.cb */
55587
+#if 0 /* local variables moved into u.bz */
5569555588
int iSet;
5569655589
int exists;
55697
-#endif /* local variables moved into u.cb */
55590
+#endif /* local variables moved into u.bz */
5569855591
55699
- u.cb.iSet = pOp->p4.i;
55592
+ u.bz.iSet = pOp->p4.i;
5570055593
assert( pIn3->flags&MEM_Int );
5570155594
5570255595
/* If there is anything other than a rowset object in memory cell P1,
5570355596
** delete it now and initialize P1 with an empty rowset
5570455597
*/
@@ -55706,21 +55599,21 @@
5570655599
sqlite3VdbeMemSetRowSet(pIn1);
5570755600
if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem;
5570855601
}
5570955602
5571055603
assert( pOp->p4type==P4_INT32 );
55711
- assert( u.cb.iSet==-1 || u.cb.iSet>=0 );
55712
- if( u.cb.iSet ){
55713
- u.cb.exists = sqlite3RowSetTest(pIn1->u.pRowSet,
55714
- (u8)(u.cb.iSet>=0 ? u.cb.iSet & 0xf : 0xff),
55604
+ assert( u.bz.iSet==-1 || u.bz.iSet>=0 );
55605
+ if( u.bz.iSet ){
55606
+ u.bz.exists = sqlite3RowSetTest(pIn1->u.pRowSet,
55607
+ (u8)(u.bz.iSet>=0 ? u.bz.iSet & 0xf : 0xff),
5571555608
pIn3->u.i);
55716
- if( u.cb.exists ){
55609
+ if( u.bz.exists ){
5571755610
pc = pOp->p2 - 1;
5571855611
break;
5571955612
}
5572055613
}
55721
- if( u.cb.iSet>=0 ){
55614
+ if( u.bz.iSet>=0 ){
5572255615
sqlite3RowSetInsert(pIn1->u.pRowSet, pIn3->u.i);
5572355616
}
5572455617
break;
5572555618
}
5572655619
@@ -55731,27 +55624,27 @@
5573155624
** Save the current Vdbe context such that it can be restored by a ContextPop
5573255625
** opcode. The context stores the last insert row id, the last statement change
5573355626
** count, and the current statement change count.
5573455627
*/
5573555628
case OP_ContextPush: {
55736
-#if 0 /* local variables moved into u.cc */
55629
+#if 0 /* local variables moved into u.ca */
5573755630
int i;
5573855631
Context *pContext;
55739
-#endif /* local variables moved into u.cc */
55632
+#endif /* local variables moved into u.ca */
5574055633
55741
- u.cc.i = p->contextStackTop++;
55742
- assert( u.cc.i>=0 );
55634
+ u.ca.i = p->contextStackTop++;
55635
+ assert( u.ca.i>=0 );
5574355636
/* FIX ME: This should be allocated as part of the vdbe at compile-time */
55744
- if( u.cc.i>=p->contextStackDepth ){
55745
- p->contextStackDepth = u.cc.i+1;
55637
+ if( u.ca.i>=p->contextStackDepth ){
55638
+ p->contextStackDepth = u.ca.i+1;
5574655639
p->contextStack = sqlite3DbReallocOrFree(db, p->contextStack,
55747
- sizeof(Context)*(u.cc.i+1));
55640
+ sizeof(Context)*(u.ca.i+1));
5574855641
if( p->contextStack==0 ) goto no_mem;
5574955642
}
55750
- u.cc.pContext = &p->contextStack[u.cc.i];
55751
- u.cc.pContext->lastRowid = db->lastRowid;
55752
- u.cc.pContext->nChange = p->nChange;
55643
+ u.ca.pContext = &p->contextStack[u.ca.i];
55644
+ u.ca.pContext->lastRowid = db->lastRowid;
55645
+ u.ca.pContext->nChange = p->nChange;
5575355646
break;
5575455647
}
5575555648
5575655649
/* Opcode: ContextPop * * *
5575755650
**
@@ -55758,17 +55651,17 @@
5575855651
** Restore the Vdbe context to the state it was in when contextPush was last
5575955652
** executed. The context stores the last insert row id, the last statement
5576055653
** change count, and the current statement change count.
5576155654
*/
5576255655
case OP_ContextPop: {
55763
-#if 0 /* local variables moved into u.cd */
55656
+#if 0 /* local variables moved into u.cb */
5576455657
Context *pContext;
55765
-#endif /* local variables moved into u.cd */
55766
- u.cd.pContext = &p->contextStack[--p->contextStackTop];
55658
+#endif /* local variables moved into u.cb */
55659
+ u.cb.pContext = &p->contextStack[--p->contextStackTop];
5576755660
assert( p->contextStackTop>=0 );
55768
- db->lastRowid = u.cd.pContext->lastRowid;
55769
- p->nChange = u.cd.pContext->nChange;
55661
+ db->lastRowid = u.cb.pContext->lastRowid;
55662
+ p->nChange = u.cb.pContext->nChange;
5577055663
break;
5577155664
}
5577255665
#endif /* #ifndef SQLITE_OMIT_TRIGGER */
5577355666
5577455667
#ifndef SQLITE_OMIT_AUTOINCREMENT
@@ -55844,51 +55737,51 @@
5584455737
**
5584555738
** The P5 arguments are taken from register P2 and its
5584655739
** successors.
5584755740
*/
5584855741
case OP_AggStep: {
55849
-#if 0 /* local variables moved into u.ce */
55742
+#if 0 /* local variables moved into u.cc */
5585055743
int n;
5585155744
int i;
5585255745
Mem *pMem;
5585355746
Mem *pRec;
5585455747
sqlite3_context ctx;
5585555748
sqlite3_value **apVal;
55856
-#endif /* local variables moved into u.ce */
55857
-
55858
- u.ce.n = pOp->p5;
55859
- assert( u.ce.n>=0 );
55860
- u.ce.pRec = &p->aMem[pOp->p2];
55861
- u.ce.apVal = p->apArg;
55862
- assert( u.ce.apVal || u.ce.n==0 );
55863
- for(u.ce.i=0; u.ce.i<u.ce.n; u.ce.i++, u.ce.pRec++){
55864
- u.ce.apVal[u.ce.i] = u.ce.pRec;
55865
- storeTypeInfo(u.ce.pRec, encoding);
55866
- }
55867
- u.ce.ctx.pFunc = pOp->p4.pFunc;
55749
+#endif /* local variables moved into u.cc */
55750
+
55751
+ u.cc.n = pOp->p5;
55752
+ assert( u.cc.n>=0 );
55753
+ u.cc.pRec = &p->aMem[pOp->p2];
55754
+ u.cc.apVal = p->apArg;
55755
+ assert( u.cc.apVal || u.cc.n==0 );
55756
+ for(u.cc.i=0; u.cc.i<u.cc.n; u.cc.i++, u.cc.pRec++){
55757
+ u.cc.apVal[u.cc.i] = u.cc.pRec;
55758
+ storeTypeInfo(u.cc.pRec, encoding);
55759
+ }
55760
+ u.cc.ctx.pFunc = pOp->p4.pFunc;
5586855761
assert( pOp->p3>0 && pOp->p3<=p->nMem );
55869
- u.ce.ctx.pMem = u.ce.pMem = &p->aMem[pOp->p3];
55870
- u.ce.pMem->n++;
55871
- u.ce.ctx.s.flags = MEM_Null;
55872
- u.ce.ctx.s.z = 0;
55873
- u.ce.ctx.s.zMalloc = 0;
55874
- u.ce.ctx.s.xDel = 0;
55875
- u.ce.ctx.s.db = db;
55876
- u.ce.ctx.isError = 0;
55877
- u.ce.ctx.pColl = 0;
55878
- if( u.ce.ctx.pFunc->flags & SQLITE_FUNC_NEEDCOLL ){
55762
+ u.cc.ctx.pMem = u.cc.pMem = &p->aMem[pOp->p3];
55763
+ u.cc.pMem->n++;
55764
+ u.cc.ctx.s.flags = MEM_Null;
55765
+ u.cc.ctx.s.z = 0;
55766
+ u.cc.ctx.s.zMalloc = 0;
55767
+ u.cc.ctx.s.xDel = 0;
55768
+ u.cc.ctx.s.db = db;
55769
+ u.cc.ctx.isError = 0;
55770
+ u.cc.ctx.pColl = 0;
55771
+ if( u.cc.ctx.pFunc->flags & SQLITE_FUNC_NEEDCOLL ){
5587955772
assert( pOp>p->aOp );
5588055773
assert( pOp[-1].p4type==P4_COLLSEQ );
5588155774
assert( pOp[-1].opcode==OP_CollSeq );
55882
- u.ce.ctx.pColl = pOp[-1].p4.pColl;
55883
- }
55884
- (u.ce.ctx.pFunc->xStep)(&u.ce.ctx, u.ce.n, u.ce.apVal);
55885
- if( u.ce.ctx.isError ){
55886
- sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&u.ce.ctx.s));
55887
- rc = u.ce.ctx.isError;
55888
- }
55889
- sqlite3VdbeMemRelease(&u.ce.ctx.s);
55775
+ u.cc.ctx.pColl = pOp[-1].p4.pColl;
55776
+ }
55777
+ (u.cc.ctx.pFunc->xStep)(&u.cc.ctx, u.cc.n, u.cc.apVal);
55778
+ if( u.cc.ctx.isError ){
55779
+ sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&u.cc.ctx.s));
55780
+ rc = u.cc.ctx.isError;
55781
+ }
55782
+ sqlite3VdbeMemRelease(&u.cc.ctx.s);
5589055783
break;
5589155784
}
5589255785
5589355786
/* Opcode: AggFinal P1 P2 * P4 *
5589455787
**
@@ -55901,23 +55794,23 @@
5590155794
** functions that can take varying numbers of arguments. The
5590255795
** P4 argument is only needed for the degenerate case where
5590355796
** the step function was not previously called.
5590455797
*/
5590555798
case OP_AggFinal: {
55906
-#if 0 /* local variables moved into u.cf */
55799
+#if 0 /* local variables moved into u.cd */
5590755800
Mem *pMem;
55908
-#endif /* local variables moved into u.cf */
55801
+#endif /* local variables moved into u.cd */
5590955802
assert( pOp->p1>0 && pOp->p1<=p->nMem );
55910
- u.cf.pMem = &p->aMem[pOp->p1];
55911
- assert( (u.cf.pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
55912
- rc = sqlite3VdbeMemFinalize(u.cf.pMem, pOp->p4.pFunc);
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);
5591355806
if( rc==SQLITE_ERROR ){
55914
- sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(u.cf.pMem));
55807
+ sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(u.cd.pMem));
5591555808
}
55916
- sqlite3VdbeChangeEncoding(u.cf.pMem, encoding);
55917
- UPDATE_MAX_BLOBSIZE(u.cf.pMem);
55918
- if( sqlite3VdbeMemTooBig(u.cf.pMem) ){
55809
+ sqlite3VdbeChangeEncoding(u.cd.pMem, encoding);
55810
+ UPDATE_MAX_BLOBSIZE(u.cd.pMem);
55811
+ if( sqlite3VdbeMemTooBig(u.cd.pMem) ){
5591955812
goto too_big;
5592055813
}
5592155814
break;
5592255815
}
5592355816
@@ -55943,18 +55836,18 @@
5594355836
** Perform a single step of the incremental vacuum procedure on
5594455837
** the P1 database. If the vacuum has finished, jump to instruction
5594555838
** P2. Otherwise, fall through to the next instruction.
5594655839
*/
5594755840
case OP_IncrVacuum: { /* jump */
55948
-#if 0 /* local variables moved into u.cg */
55841
+#if 0 /* local variables moved into u.ce */
5594955842
Btree *pBt;
55950
-#endif /* local variables moved into u.cg */
55843
+#endif /* local variables moved into u.ce */
5595155844
5595255845
assert( pOp->p1>=0 && pOp->p1<db->nDb );
5595355846
assert( (p->btreeMask & (1<<pOp->p1))!=0 );
55954
- u.cg.pBt = db->aDb[pOp->p1].pBt;
55955
- rc = sqlite3BtreeIncrVacuum(u.cg.pBt);
55847
+ u.ce.pBt = db->aDb[pOp->p1].pBt;
55848
+ rc = sqlite3BtreeIncrVacuum(u.ce.pBt);
5595655849
if( rc==SQLITE_DONE ){
5595755850
pc = pOp->p2 - 1;
5595855851
rc = SQLITE_OK;
5595955852
}
5596055853
break;
@@ -55993,21 +55886,21 @@
5599355886
**
5599455887
** P4 contains a pointer to the name of the table being locked. This is only
5599555888
** used to generate an error message if the lock cannot be obtained.
5599655889
*/
5599755890
case OP_TableLock: {
55998
-#if 0 /* local variables moved into u.ch */
55891
+#if 0 /* local variables moved into u.cf */
5599955892
int p1;
5600055893
u8 isWriteLock;
56001
-#endif /* local variables moved into u.ch */
56002
-
56003
- u.ch.p1 = pOp->p1;
56004
- u.ch.isWriteLock = (u8)pOp->p3;
56005
- assert( u.ch.p1>=0 && u.ch.p1<db->nDb );
56006
- assert( (p->btreeMask & (1<<u.ch.p1))!=0 );
56007
- assert( u.ch.isWriteLock==0 || u.ch.isWriteLock==1 );
56008
- rc = sqlite3BtreeLockTable(db->aDb[u.ch.p1].pBt, pOp->p2, u.ch.isWriteLock);
55894
+#endif /* local variables moved into u.cf */
55895
+
55896
+ u.cf.p1 = pOp->p1;
55897
+ u.cf.isWriteLock = (u8)pOp->p3;
55898
+ assert( u.cf.p1>=0 && u.cf.p1<db->nDb );
55899
+ assert( (p->btreeMask & (1<<u.cf.p1))!=0 );
55900
+ assert( u.cf.isWriteLock==0 || u.cf.isWriteLock==1 );
55901
+ rc = sqlite3BtreeLockTable(db->aDb[u.cf.p1].pBt, pOp->p2, u.cf.isWriteLock);
5600955902
if( (rc&0xFF)==SQLITE_LOCKED ){
5601055903
const char *z = pOp->p4.z;
5601155904
sqlite3SetString(&p->zErrMsg, db, "database table is locked: %s", z);
5601255905
}
5601355906
break;
@@ -56023,19 +55916,19 @@
5602355916
** Also, whether or not P4 is set, check that this is not being called from
5602455917
** within a callback to a virtual table xSync() method. If it is, the error
5602555918
** code will be set to SQLITE_LOCKED.
5602655919
*/
5602755920
case OP_VBegin: {
56028
-#if 0 /* local variables moved into u.ci */
55921
+#if 0 /* local variables moved into u.cg */
5602955922
sqlite3_vtab *pVtab;
56030
-#endif /* local variables moved into u.ci */
56031
- u.ci.pVtab = pOp->p4.pVtab;
56032
- rc = sqlite3VtabBegin(db, u.ci.pVtab);
56033
- if( u.ci.pVtab ){
55923
+#endif /* local variables moved into u.cg */
55924
+ u.cg.pVtab = pOp->p4.pVtab;
55925
+ rc = sqlite3VtabBegin(db, u.cg.pVtab);
55926
+ if( u.cg.pVtab ){
5603455927
sqlite3DbFree(db, p->zErrMsg);
56035
- p->zErrMsg = u.ci.pVtab->zErrMsg;
56036
- u.ci.pVtab->zErrMsg = 0;
55928
+ p->zErrMsg = u.cg.pVtab->zErrMsg;
55929
+ u.cg.pVtab->zErrMsg = 0;
5603755930
}
5603855931
break;
5603955932
}
5604055933
#endif /* SQLITE_OMIT_VIRTUALTABLE */
5604155934
@@ -56071,40 +55964,40 @@
5607155964
** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
5607255965
** P1 is a cursor number. This opcode opens a cursor to the virtual
5607355966
** table and stores that cursor in P1.
5607455967
*/
5607555968
case OP_VOpen: {
56076
-#if 0 /* local variables moved into u.cj */
55969
+#if 0 /* local variables moved into u.ch */
5607755970
VdbeCursor *pCur;
5607855971
sqlite3_vtab_cursor *pVtabCursor;
5607955972
sqlite3_vtab *pVtab;
5608055973
sqlite3_module *pModule;
56081
-#endif /* local variables moved into u.cj */
55974
+#endif /* local variables moved into u.ch */
5608255975
56083
- u.cj.pCur = 0;
56084
- u.cj.pVtabCursor = 0;
56085
- u.cj.pVtab = pOp->p4.pVtab;
56086
- u.cj.pModule = (sqlite3_module *)u.cj.pVtab->pModule;
56087
- assert(u.cj.pVtab && u.cj.pModule);
55976
+ u.ch.pCur = 0;
55977
+ u.ch.pVtabCursor = 0;
55978
+ u.ch.pVtab = pOp->p4.pVtab;
55979
+ u.ch.pModule = (sqlite3_module *)u.ch.pVtab->pModule;
55980
+ assert(u.ch.pVtab && u.ch.pModule);
5608855981
if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
56089
- rc = u.cj.pModule->xOpen(u.cj.pVtab, &u.cj.pVtabCursor);
55982
+ rc = u.ch.pModule->xOpen(u.ch.pVtab, &u.ch.pVtabCursor);
5609055983
sqlite3DbFree(db, p->zErrMsg);
56091
- p->zErrMsg = u.cj.pVtab->zErrMsg;
56092
- u.cj.pVtab->zErrMsg = 0;
55984
+ p->zErrMsg = u.ch.pVtab->zErrMsg;
55985
+ u.ch.pVtab->zErrMsg = 0;
5609355986
if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
5609455987
if( SQLITE_OK==rc ){
5609555988
/* Initialize sqlite3_vtab_cursor base class */
56096
- u.cj.pVtabCursor->pVtab = u.cj.pVtab;
55989
+ u.ch.pVtabCursor->pVtab = u.ch.pVtab;
5609755990
5609855991
/* Initialise vdbe cursor object */
56099
- u.cj.pCur = allocateCursor(p, pOp->p1, 0, -1, 0);
56100
- if( u.cj.pCur ){
56101
- u.cj.pCur->pVtabCursor = u.cj.pVtabCursor;
56102
- u.cj.pCur->pModule = u.cj.pVtabCursor->pVtab->pModule;
55992
+ u.ch.pCur = allocateCursor(p, pOp->p1, 0, -1, 0);
55993
+ if( u.ch.pCur ){
55994
+ u.ch.pCur->pVtabCursor = u.ch.pVtabCursor;
55995
+ u.ch.pCur->pModule = u.ch.pVtabCursor->pVtab->pModule;
5610355996
}else{
5610455997
db->mallocFailed = 1;
56105
- u.cj.pModule->xClose(u.cj.pVtabCursor);
55998
+ u.ch.pModule->xClose(u.ch.pVtabCursor);
5610655999
}
5610756000
}
5610856001
break;
5610956002
}
5611056003
#endif /* SQLITE_OMIT_VIRTUALTABLE */
@@ -56127,11 +56020,11 @@
5612756020
** xFilter as argv. Register P3+2 becomes argv[0] when passed to xFilter.
5612856021
**
5612956022
** A jump is made to P2 if the result set after filtering would be empty.
5613056023
*/
5613156024
case OP_VFilter: { /* jump */
56132
-#if 0 /* local variables moved into u.ck */
56025
+#if 0 /* local variables moved into u.ci */
5613356026
int nArg;
5613456027
int iQuery;
5613556028
const sqlite3_module *pModule;
5613656029
Mem *pQuery;
5613756030
Mem *pArgc;
@@ -56139,54 +56032,54 @@
5613956032
sqlite3_vtab *pVtab;
5614056033
VdbeCursor *pCur;
5614156034
int res;
5614256035
int i;
5614356036
Mem **apArg;
56144
-#endif /* local variables moved into u.ck */
56145
-
56146
- u.ck.pQuery = &p->aMem[pOp->p3];
56147
- u.ck.pArgc = &u.ck.pQuery[1];
56148
- u.ck.pCur = p->apCsr[pOp->p1];
56149
- REGISTER_TRACE(pOp->p3, u.ck.pQuery);
56150
- assert( u.ck.pCur->pVtabCursor );
56151
- u.ck.pVtabCursor = u.ck.pCur->pVtabCursor;
56152
- u.ck.pVtab = u.ck.pVtabCursor->pVtab;
56153
- u.ck.pModule = u.ck.pVtab->pModule;
56037
+#endif /* local variables moved into u.ci */
56038
+
56039
+ u.ci.pQuery = &p->aMem[pOp->p3];
56040
+ u.ci.pArgc = &u.ci.pQuery[1];
56041
+ u.ci.pCur = p->apCsr[pOp->p1];
56042
+ REGISTER_TRACE(pOp->p3, u.ci.pQuery);
56043
+ assert( u.ci.pCur->pVtabCursor );
56044
+ u.ci.pVtabCursor = u.ci.pCur->pVtabCursor;
56045
+ u.ci.pVtab = u.ci.pVtabCursor->pVtab;
56046
+ u.ci.pModule = u.ci.pVtab->pModule;
5615456047
5615556048
/* Grab the index number and argc parameters */
56156
- assert( (u.ck.pQuery->flags&MEM_Int)!=0 && u.ck.pArgc->flags==MEM_Int );
56157
- u.ck.nArg = (int)u.ck.pArgc->u.i;
56158
- u.ck.iQuery = (int)u.ck.pQuery->u.i;
56049
+ assert( (u.ci.pQuery->flags&MEM_Int)!=0 && u.ci.pArgc->flags==MEM_Int );
56050
+ u.ci.nArg = (int)u.ci.pArgc->u.i;
56051
+ u.ci.iQuery = (int)u.ci.pQuery->u.i;
5615956052
5616056053
/* Invoke the xFilter method */
5616156054
{
56162
- u.ck.res = 0;
56163
- u.ck.apArg = p->apArg;
56164
- for(u.ck.i = 0; u.ck.i<u.ck.nArg; u.ck.i++){
56165
- u.ck.apArg[u.ck.i] = &u.ck.pArgc[u.ck.i+1];
56166
- storeTypeInfo(u.ck.apArg[u.ck.i], 0);
56055
+ u.ci.res = 0;
56056
+ u.ci.apArg = p->apArg;
56057
+ for(u.ci.i = 0; u.ci.i<u.ci.nArg; u.ci.i++){
56058
+ u.ci.apArg[u.ci.i] = &u.ci.pArgc[u.ci.i+1];
56059
+ storeTypeInfo(u.ci.apArg[u.ci.i], 0);
5616756060
}
5616856061
5616956062
if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
56170
- sqlite3VtabLock(u.ck.pVtab);
56063
+ sqlite3VtabLock(u.ci.pVtab);
5617156064
p->inVtabMethod = 1;
56172
- rc = u.ck.pModule->xFilter(u.ck.pVtabCursor, u.ck.iQuery, pOp->p4.z, u.ck.nArg, u.ck.apArg);
56065
+ rc = u.ci.pModule->xFilter(u.ci.pVtabCursor, u.ci.iQuery, pOp->p4.z, u.ci.nArg, u.ci.apArg);
5617356066
p->inVtabMethod = 0;
5617456067
sqlite3DbFree(db, p->zErrMsg);
56175
- p->zErrMsg = u.ck.pVtab->zErrMsg;
56176
- u.ck.pVtab->zErrMsg = 0;
56177
- sqlite3VtabUnlock(db, u.ck.pVtab);
56068
+ p->zErrMsg = u.ci.pVtab->zErrMsg;
56069
+ u.ci.pVtab->zErrMsg = 0;
56070
+ sqlite3VtabUnlock(db, u.ci.pVtab);
5617856071
if( rc==SQLITE_OK ){
56179
- u.ck.res = u.ck.pModule->xEof(u.ck.pVtabCursor);
56072
+ u.ci.res = u.ci.pModule->xEof(u.ci.pVtabCursor);
5618056073
}
5618156074
if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
5618256075
56183
- if( u.ck.res ){
56076
+ if( u.ci.res ){
5618456077
pc = pOp->p2 - 1;
5618556078
}
5618656079
}
56187
- u.ck.pCur->nullRow = 0;
56080
+ u.ci.pCur->nullRow = 0;
5618856081
5618956082
break;
5619056083
}
5619156084
#endif /* SQLITE_OMIT_VIRTUALTABLE */
5619256085
@@ -56196,57 +56089,57 @@
5619656089
** Store the value of the P2-th column of
5619756090
** the row of the virtual-table that the
5619856091
** P1 cursor is pointing to into register P3.
5619956092
*/
5620056093
case OP_VColumn: {
56201
-#if 0 /* local variables moved into u.cl */
56094
+#if 0 /* local variables moved into u.cj */
5620256095
sqlite3_vtab *pVtab;
5620356096
const sqlite3_module *pModule;
5620456097
Mem *pDest;
5620556098
sqlite3_context sContext;
56206
-#endif /* local variables moved into u.cl */
56099
+#endif /* local variables moved into u.cj */
5620756100
5620856101
VdbeCursor *pCur = p->apCsr[pOp->p1];
5620956102
assert( pCur->pVtabCursor );
5621056103
assert( pOp->p3>0 && pOp->p3<=p->nMem );
56211
- u.cl.pDest = &p->aMem[pOp->p3];
56104
+ u.cj.pDest = &p->aMem[pOp->p3];
5621256105
if( pCur->nullRow ){
56213
- sqlite3VdbeMemSetNull(u.cl.pDest);
56106
+ sqlite3VdbeMemSetNull(u.cj.pDest);
5621456107
break;
5621556108
}
56216
- u.cl.pVtab = pCur->pVtabCursor->pVtab;
56217
- u.cl.pModule = u.cl.pVtab->pModule;
56218
- assert( u.cl.pModule->xColumn );
56219
- memset(&u.cl.sContext, 0, sizeof(u.cl.sContext));
56109
+ u.cj.pVtab = pCur->pVtabCursor->pVtab;
56110
+ u.cj.pModule = u.cj.pVtab->pModule;
56111
+ assert( u.cj.pModule->xColumn );
56112
+ memset(&u.cj.sContext, 0, sizeof(u.cj.sContext));
5622056113
5622156114
/* The output cell may already have a buffer allocated. Move
56222
- ** the current contents to u.cl.sContext.s so in case the user-function
56115
+ ** the current contents to u.cj.sContext.s so in case the user-function
5622356116
** can use the already allocated buffer instead of allocating a
5622456117
** new one.
5622556118
*/
56226
- sqlite3VdbeMemMove(&u.cl.sContext.s, u.cl.pDest);
56227
- MemSetTypeFlag(&u.cl.sContext.s, MEM_Null);
56119
+ sqlite3VdbeMemMove(&u.cj.sContext.s, u.cj.pDest);
56120
+ MemSetTypeFlag(&u.cj.sContext.s, MEM_Null);
5622856121
5622956122
if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
56230
- rc = u.cl.pModule->xColumn(pCur->pVtabCursor, &u.cl.sContext, pOp->p2);
56123
+ rc = u.cj.pModule->xColumn(pCur->pVtabCursor, &u.cj.sContext, pOp->p2);
5623156124
sqlite3DbFree(db, p->zErrMsg);
56232
- p->zErrMsg = u.cl.pVtab->zErrMsg;
56233
- u.cl.pVtab->zErrMsg = 0;
56125
+ p->zErrMsg = u.cj.pVtab->zErrMsg;
56126
+ u.cj.pVtab->zErrMsg = 0;
5623456127
5623556128
/* Copy the result of the function to the P3 register. We
5623656129
** do this regardless of whether or not an error occurred to ensure any
56237
- ** dynamic allocation in u.cl.sContext.s (a Mem struct) is released.
56130
+ ** dynamic allocation in u.cj.sContext.s (a Mem struct) is released.
5623856131
*/
56239
- sqlite3VdbeChangeEncoding(&u.cl.sContext.s, encoding);
56240
- REGISTER_TRACE(pOp->p3, u.cl.pDest);
56241
- sqlite3VdbeMemMove(u.cl.pDest, &u.cl.sContext.s);
56242
- UPDATE_MAX_BLOBSIZE(u.cl.pDest);
56132
+ sqlite3VdbeChangeEncoding(&u.cj.sContext.s, encoding);
56133
+ REGISTER_TRACE(pOp->p3, u.cj.pDest);
56134
+ sqlite3VdbeMemMove(u.cj.pDest, &u.cj.sContext.s);
56135
+ UPDATE_MAX_BLOBSIZE(u.cj.pDest);
5624356136
5624456137
if( sqlite3SafetyOn(db) ){
5624556138
goto abort_due_to_misuse;
5624656139
}
56247
- if( sqlite3VdbeMemTooBig(u.cl.pDest) ){
56140
+ if( sqlite3VdbeMemTooBig(u.cj.pDest) ){
5624856141
goto too_big;
5624956142
}
5625056143
break;
5625156144
}
5625256145
#endif /* SQLITE_OMIT_VIRTUALTABLE */
@@ -56257,48 +56150,48 @@
5625756150
** Advance virtual table P1 to the next row in its result set and
5625856151
** jump to instruction P2. Or, if the virtual table has reached
5625956152
** the end of its result set, then fall through to the next instruction.
5626056153
*/
5626156154
case OP_VNext: { /* jump */
56262
-#if 0 /* local variables moved into u.cm */
56155
+#if 0 /* local variables moved into u.ck */
5626356156
sqlite3_vtab *pVtab;
5626456157
const sqlite3_module *pModule;
5626556158
int res;
5626656159
VdbeCursor *pCur;
56267
-#endif /* local variables moved into u.cm */
56160
+#endif /* local variables moved into u.ck */
5626856161
56269
- u.cm.res = 0;
56270
- u.cm.pCur = p->apCsr[pOp->p1];
56271
- assert( u.cm.pCur->pVtabCursor );
56272
- if( u.cm.pCur->nullRow ){
56162
+ u.ck.res = 0;
56163
+ u.ck.pCur = p->apCsr[pOp->p1];
56164
+ assert( u.ck.pCur->pVtabCursor );
56165
+ if( u.ck.pCur->nullRow ){
5627356166
break;
5627456167
}
56275
- u.cm.pVtab = u.cm.pCur->pVtabCursor->pVtab;
56276
- u.cm.pModule = u.cm.pVtab->pModule;
56277
- assert( u.cm.pModule->xNext );
56168
+ u.ck.pVtab = u.ck.pCur->pVtabCursor->pVtab;
56169
+ u.ck.pModule = u.ck.pVtab->pModule;
56170
+ assert( u.ck.pModule->xNext );
5627856171
5627956172
/* Invoke the xNext() method of the module. There is no way for the
5628056173
** underlying implementation to return an error if one occurs during
5628156174
** xNext(). Instead, if an error occurs, true is returned (indicating that
5628256175
** data is available) and the error code returned when xColumn or
5628356176
** some other method is next invoked on the save virtual table cursor.
5628456177
*/
5628556178
if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
56286
- sqlite3VtabLock(u.cm.pVtab);
56179
+ sqlite3VtabLock(u.ck.pVtab);
5628756180
p->inVtabMethod = 1;
56288
- rc = u.cm.pModule->xNext(u.cm.pCur->pVtabCursor);
56181
+ rc = u.ck.pModule->xNext(u.ck.pCur->pVtabCursor);
5628956182
p->inVtabMethod = 0;
5629056183
sqlite3DbFree(db, p->zErrMsg);
56291
- p->zErrMsg = u.cm.pVtab->zErrMsg;
56292
- u.cm.pVtab->zErrMsg = 0;
56293
- sqlite3VtabUnlock(db, u.cm.pVtab);
56184
+ p->zErrMsg = u.ck.pVtab->zErrMsg;
56185
+ u.ck.pVtab->zErrMsg = 0;
56186
+ sqlite3VtabUnlock(db, u.ck.pVtab);
5629456187
if( rc==SQLITE_OK ){
56295
- u.cm.res = u.cm.pModule->xEof(u.cm.pCur->pVtabCursor);
56188
+ u.ck.res = u.ck.pModule->xEof(u.ck.pCur->pVtabCursor);
5629656189
}
5629756190
if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
5629856191
56299
- if( !u.cm.res ){
56192
+ if( !u.ck.res ){
5630056193
/* If there is data, jump to P2 */
5630156194
pc = pOp->p2 - 1;
5630256195
}
5630356196
break;
5630456197
}
@@ -56310,29 +56203,27 @@
5631056203
** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
5631156204
** This opcode invokes the corresponding xRename method. The value
5631256205
** in register P1 is passed as the zName argument to the xRename method.
5631356206
*/
5631456207
case OP_VRename: {
56315
-#if 0 /* local variables moved into u.cn */
56208
+#if 0 /* local variables moved into u.cl */
5631656209
sqlite3_vtab *pVtab;
5631756210
Mem *pName;
56318
-#endif /* local variables moved into u.cn */
56319
-
56320
- u.cn.pVtab = pOp->p4.pVtab;
56321
- u.cn.pName = &p->aMem[pOp->p1];
56322
- assert( u.cn.pVtab->pModule->xRename );
56323
- REGISTER_TRACE(pOp->p1, u.cn.pName);
56324
-
56325
- Stringify(u.cn.pName, encoding);
56326
-
56211
+#endif /* local variables moved into u.cl */
56212
+
56213
+ u.cl.pVtab = pOp->p4.pVtab;
56214
+ u.cl.pName = &p->aMem[pOp->p1];
56215
+ assert( u.cl.pVtab->pModule->xRename );
56216
+ REGISTER_TRACE(pOp->p1, u.cl.pName);
56217
+ assert( u.cl.pName->flags & MEM_Str );
5632756218
if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
56328
- sqlite3VtabLock(u.cn.pVtab);
56329
- rc = u.cn.pVtab->pModule->xRename(u.cn.pVtab, u.cn.pName->z);
56219
+ sqlite3VtabLock(u.cl.pVtab);
56220
+ rc = u.cl.pVtab->pModule->xRename(u.cl.pVtab, u.cl.pName->z);
5633056221
sqlite3DbFree(db, p->zErrMsg);
56331
- p->zErrMsg = u.cn.pVtab->zErrMsg;
56332
- u.cn.pVtab->zErrMsg = 0;
56333
- sqlite3VtabUnlock(db, u.cn.pVtab);
56222
+ p->zErrMsg = u.cl.pVtab->zErrMsg;
56223
+ u.cl.pVtab->zErrMsg = 0;
56224
+ sqlite3VtabUnlock(db, u.cl.pVtab);
5633456225
if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
5633556226
5633656227
break;
5633756228
}
5633856229
#endif
@@ -56360,46 +56251,43 @@
5636056251
** P1 is a boolean flag. If it is set to true and the xUpdate call
5636156252
** is successful, then the value returned by sqlite3_last_insert_rowid()
5636256253
** is set to the value of the rowid for the row just inserted.
5636356254
*/
5636456255
case OP_VUpdate: {
56365
-#if 0 /* local variables moved into u.co */
56256
+#if 0 /* local variables moved into u.cm */
5636656257
sqlite3_vtab *pVtab;
5636756258
sqlite3_module *pModule;
5636856259
int nArg;
5636956260
int i;
5637056261
sqlite_int64 rowid;
5637156262
Mem **apArg;
5637256263
Mem *pX;
56373
-#endif /* local variables moved into u.co */
56264
+#endif /* local variables moved into u.cm */
5637456265
56375
- u.co.pVtab = pOp->p4.pVtab;
56376
- u.co.pModule = (sqlite3_module *)u.co.pVtab->pModule;
56377
- u.co.nArg = pOp->p2;
56266
+ u.cm.pVtab = pOp->p4.pVtab;
56267
+ u.cm.pModule = (sqlite3_module *)u.cm.pVtab->pModule;
56268
+ u.cm.nArg = pOp->p2;
5637856269
assert( pOp->p4type==P4_VTAB );
56379
- if( u.co.pModule->xUpdate==0 ){
56380
- sqlite3SetString(&p->zErrMsg, db, "read-only table");
56381
- rc = SQLITE_ERROR;
56382
- }else{
56383
- u.co.apArg = p->apArg;
56384
- u.co.pX = &p->aMem[pOp->p3];
56385
- for(u.co.i=0; u.co.i<u.co.nArg; u.co.i++){
56386
- storeTypeInfo(u.co.pX, 0);
56387
- u.co.apArg[u.co.i] = u.co.pX;
56388
- u.co.pX++;
56270
+ if( ALWAYS(u.cm.pModule->xUpdate) ){
56271
+ u.cm.apArg = p->apArg;
56272
+ u.cm.pX = &p->aMem[pOp->p3];
56273
+ for(u.cm.i=0; u.cm.i<u.cm.nArg; u.cm.i++){
56274
+ storeTypeInfo(u.cm.pX, 0);
56275
+ u.cm.apArg[u.cm.i] = u.cm.pX;
56276
+ u.cm.pX++;
5638956277
}
5639056278
if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
56391
- sqlite3VtabLock(u.co.pVtab);
56392
- rc = u.co.pModule->xUpdate(u.co.pVtab, u.co.nArg, u.co.apArg, &u.co.rowid);
56279
+ sqlite3VtabLock(u.cm.pVtab);
56280
+ rc = u.cm.pModule->xUpdate(u.cm.pVtab, u.cm.nArg, u.cm.apArg, &u.cm.rowid);
5639356281
sqlite3DbFree(db, p->zErrMsg);
56394
- p->zErrMsg = u.co.pVtab->zErrMsg;
56395
- u.co.pVtab->zErrMsg = 0;
56396
- sqlite3VtabUnlock(db, u.co.pVtab);
56282
+ p->zErrMsg = u.cm.pVtab->zErrMsg;
56283
+ u.cm.pVtab->zErrMsg = 0;
56284
+ sqlite3VtabUnlock(db, u.cm.pVtab);
5639756285
if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
56398
- if( pOp->p1 && rc==SQLITE_OK ){
56399
- assert( u.co.nArg>1 && u.co.apArg[0] && (u.co.apArg[0]->flags&MEM_Null) );
56400
- db->lastRowid = u.co.rowid;
56286
+ if( rc==SQLITE_OK && pOp->p1 ){
56287
+ assert( u.cm.nArg>1 && u.cm.apArg[0] && (u.cm.apArg[0]->flags&MEM_Null) );
56288
+ db->lastRowid = u.cm.rowid;
5640156289
}
5640256290
p->nChange++;
5640356291
}
5640456292
break;
5640556293
}
@@ -56409,22 +56297,25 @@
5640956297
/* Opcode: Pagecount P1 P2 * * *
5641056298
**
5641156299
** Write the current number of pages in database P1 to memory cell P2.
5641256300
*/
5641356301
case OP_Pagecount: { /* out2-prerelease */
56414
-#if 0 /* local variables moved into u.cp */
56302
+#if 0 /* local variables moved into u.cn */
5641556303
int p1;
5641656304
int nPage;
5641756305
Pager *pPager;
56418
-#endif /* local variables moved into u.cp */
56306
+#endif /* local variables moved into u.cn */
5641956307
56420
- u.cp.p1 = pOp->p1;
56421
- u.cp.pPager = sqlite3BtreePager(db->aDb[u.cp.p1].pBt);
56422
- rc = sqlite3PagerPagecount(u.cp.pPager, &u.cp.nPage);
56423
- if( rc==SQLITE_OK ){
56308
+ u.cn.p1 = pOp->p1;
56309
+ u.cn.pPager = sqlite3BtreePager(db->aDb[u.cn.p1].pBt);
56310
+ rc = sqlite3PagerPagecount(u.cn.pPager, &u.cn.nPage);
56311
+ /* OP_Pagecount is always called from within a read transaction. The
56312
+ ** page count has already been successfully read and cached. So the
56313
+ ** sqlite3PagerPagecount() call above cannot fail. */
56314
+ if( ALWAYS(rc==SQLITE_OK) ){
5642456315
pOut->flags = MEM_Int;
56425
- pOut->u.i = u.cp.nPage;
56316
+ pOut->u.i = u.cn.nPage;
5642656317
}
5642756318
break;
5642856319
}
5642956320
#endif
5643056321
@@ -56433,22 +56324,22 @@
5643356324
**
5643456325
** If tracing is enabled (by the sqlite3_trace()) interface, then
5643556326
** the UTF-8 string contained in P4 is emitted on the trace callback.
5643656327
*/
5643756328
case OP_Trace: {
56438
-#if 0 /* local variables moved into u.cq */
56329
+#if 0 /* local variables moved into u.co */
5643956330
char *zTrace;
56440
-#endif /* local variables moved into u.cq */
56331
+#endif /* local variables moved into u.co */
5644156332
56442
- u.cq.zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql);
56443
- if( u.cq.zTrace ){
56333
+ u.co.zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql);
56334
+ if( u.co.zTrace ){
5644456335
if( db->xTrace ){
56445
- db->xTrace(db->pTraceArg, u.cq.zTrace);
56336
+ db->xTrace(db->pTraceArg, u.co.zTrace);
5644656337
}
5644756338
#ifdef SQLITE_DEBUG
5644856339
if( (db->flags & SQLITE_SqlTrace)!=0 ){
56449
- sqlite3DebugPrintf("SQL-trace: %s\n", u.cq.zTrace);
56340
+ sqlite3DebugPrintf("SQL-trace: %s\n", u.co.zTrace);
5645056341
}
5645156342
#endif /* SQLITE_DEBUG */
5645256343
}
5645356344
break;
5645456345
}
@@ -57461,11 +57352,11 @@
5746157352
**
5746257353
*************************************************************************
5746357354
** This file contains routines used for walking the parser tree for
5746457355
** an SQL statement.
5746557356
**
57466
-** $Id: walker.c,v 1.6 2009/05/28 01:00:55 drh Exp $
57357
+** $Id: walker.c,v 1.7 2009/06/15 23:15:59 drh Exp $
5746757358
*/
5746857359
5746957360
5747057361
/*
5747157362
** Walk an expression tree. Invoke the callback once for each node
@@ -57508,18 +57399,18 @@
5750857399
/*
5750957400
** Call sqlite3WalkExpr() for every expression in list p or until
5751057401
** an abort request is seen.
5751157402
*/
5751257403
SQLITE_PRIVATE int sqlite3WalkExprList(Walker *pWalker, ExprList *p){
57513
- int i, rc = WRC_Continue;
57404
+ int i;
5751457405
struct ExprList_item *pItem;
5751557406
if( p ){
5751657407
for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){
5751757408
if( sqlite3WalkExpr(pWalker, pItem->pExpr) ) return WRC_Abort;
5751857409
}
5751957410
}
57520
- return rc & WRC_Continue;
57411
+ return WRC_Continue;
5752157412
}
5752257413
5752357414
/*
5752457415
** Walk all expressions associated with SELECT statement p. Do
5752557416
** not invoke the SELECT callback on p, but do (of course) invoke
@@ -57548,11 +57439,11 @@
5754857439
SrcList *pSrc;
5754957440
int i;
5755057441
struct SrcList_item *pItem;
5755157442
5755257443
pSrc = p->pSrc;
57553
- if( pSrc ){
57444
+ if( ALWAYS(pSrc) ){
5755457445
for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){
5755557446
if( sqlite3WalkSelect(pWalker, pItem->pSelect) ){
5755657447
return WRC_Abort;
5755757448
}
5755857449
}
@@ -57601,11 +57492,11 @@
5760157492
**
5760257493
** This file contains routines used for walking the parser tree and
5760357494
** resolve all identifiers by associating them with a particular
5760457495
** table and column.
5760557496
**
57606
-** $Id: resolve.c,v 1.28 2009/06/01 16:53:10 shane Exp $
57497
+** $Id: resolve.c,v 1.30 2009/06/15 23:15:59 drh Exp $
5760757498
*/
5760857499
5760957500
/*
5761057501
** Turn the pExpr expression into an alias for the iCol-th column of the
5761157502
** result set in pEList.
@@ -57660,19 +57551,18 @@
5766057551
}else if( ExprHasProperty(pOrig, EP_IntValue) || pOrig->u.zToken==0 ){
5766157552
pDup = sqlite3ExprDup(db, pOrig, 0);
5766257553
if( pDup==0 ) return;
5766357554
}else{
5766457555
char *zToken = pOrig->u.zToken;
57556
+ assert( zToken!=0 );
5766557557
pOrig->u.zToken = 0;
5766657558
pDup = sqlite3ExprDup(db, pOrig, 0);
5766757559
pOrig->u.zToken = zToken;
5766857560
if( pDup==0 ) return;
57669
- if( zToken ){
57670
- assert( (pDup->flags & (EP_Reduced|EP_TokenOnly))==0 );
57671
- pDup->flags2 |= EP2_MallocedToken;
57672
- pDup->u.zToken = sqlite3DbStrDup(db, zToken);
57673
- }
57561
+ assert( (pDup->flags & (EP_Reduced|EP_TokenOnly))==0 );
57562
+ pDup->flags2 |= EP2_MallocedToken;
57563
+ pDup->u.zToken = sqlite3DbStrDup(db, zToken);
5767457564
}
5767557565
if( pExpr->flags & EP_ExpCollate ){
5767657566
pDup->pColl = pExpr->pColl;
5767757567
pDup->flags |= EP_ExpCollate;
5767857568
}
@@ -57704,11 +57594,11 @@
5770457594
** value can be NULL if zDb is also NULL. If zTable is NULL it
5770557595
** means that the form of the name is Z and that columns from any table
5770657596
** can be used.
5770757597
**
5770857598
** If the name cannot be resolved unambiguously, leave an error message
57709
-** in pParse and return non-zero. Return zero on success.
57599
+** in pParse and return WRC_Abort. Return WRC_Prune on success.
5771057600
*/
5771157601
static int lookupName(
5771257602
Parse *pParse, /* The parsing context */
5771357603
const char *zDb, /* Name of the database containing table, or NULL */
5771457604
const char *zTab, /* Name of table containing column, or NULL */
@@ -57753,11 +57643,13 @@
5775357643
if( pItem->zAlias ){
5775457644
char *zTabName = pItem->zAlias;
5775557645
if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
5775657646
}else{
5775757647
char *zTabName = pTab->zName;
57758
- if( zTabName==0 || sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
57648
+ if( NEVER(zTabName==0) || sqlite3StrICmp(zTabName, zTab)!=0 ){
57649
+ continue;
57650
+ }
5775957651
if( zDb!=0 && sqlite3StrICmp(db->aDb[iDb].zName, zDb)!=0 ){
5776057652
continue;
5776157653
}
5776257654
}
5776357655
}
@@ -57832,18 +57724,16 @@
5783257724
for(iCol=0; iCol < pTab->nCol; iCol++, pCol++) {
5783357725
if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
5783457726
cnt++;
5783557727
pExpr->iColumn = iCol==pTab->iPKey ? -1 : (i16)iCol;
5783657728
pExpr->pTab = pTab;
57837
- if( iCol>=0 ){
57838
- testcase( iCol==31 );
57839
- testcase( iCol==32 );
57840
- if( iCol>=32 ){
57841
- *piColMask = 0xffffffff;
57842
- }else{
57843
- *piColMask |= ((u32)1)<<iCol;
57844
- }
57729
+ testcase( iCol==31 );
57730
+ testcase( iCol==32 );
57731
+ if( iCol>=32 ){
57732
+ *piColMask = 0xffffffff;
57733
+ }else{
57734
+ *piColMask |= ((u32)1)<<iCol;
5784557735
}
5784657736
break;
5784757737
}
5784857738
}
5784957739
}
@@ -57880,11 +57770,11 @@
5788057770
assert( pExpr->x.pList==0 );
5788157771
assert( pExpr->x.pSelect==0 );
5788257772
pOrig = pEList->a[j].pExpr;
5788357773
if( !pNC->allowAgg && ExprHasProperty(pOrig, EP_Agg) ){
5788457774
sqlite3ErrorMsg(pParse, "misuse of aliased aggregate %s", zAs);
57885
- return 2;
57775
+ return WRC_Abort;
5788657776
}
5788757777
resolveAlias(pParse, pEList, j, pExpr, "");
5788857778
cnt = 1;
5788957779
pMatch = 0;
5789057780
assert( zTab==0 && zDb==0 );
@@ -57912,11 +57802,11 @@
5791257802
** fields are not changed in any context.
5791357803
*/
5791457804
if( cnt==0 && zTab==0 && ExprHasProperty(pExpr,EP_DblQuoted) ){
5791557805
pExpr->op = TK_STRING;
5791657806
pExpr->pTab = 0;
57917
- return 0;
57807
+ return WRC_Prune;
5791857808
}
5791957809
5792057810
/*
5792157811
** cnt==0 means there was not match. cnt>1 means there were two or
5792257812
** more matches. Either way, we have an error.
@@ -57967,13 +57857,13 @@
5796757857
assert( pTopNC!=0 );
5796857858
pTopNC->nRef++;
5796957859
if( pTopNC==pNC ) break;
5797057860
pTopNC = pTopNC->pNext;
5797157861
}
57972
- return 0;
57862
+ return WRC_Prune;
5797357863
} else {
57974
- return 1;
57864
+ return WRC_Abort;
5797557865
}
5797657866
}
5797757867
5797857868
/*
5797957869
** This routine is callback for sqlite3WalkExpr().
@@ -58028,12 +57918,11 @@
5802857918
#endif /* defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY) */
5802957919
5803057920
/* A lone identifier is the name of a column.
5803157921
*/
5803257922
case TK_ID: {
58033
- lookupName(pParse, 0, 0, pExpr->u.zToken, pNC, pExpr);
58034
- return WRC_Prune;
57923
+ return lookupName(pParse, 0, 0, pExpr->u.zToken, pNC, pExpr);
5803557924
}
5803657925
5803757926
/* A table name and column name: ID.ID
5803857927
** Or a database, table and column: ID.ID.ID
5803957928
*/
@@ -58053,12 +57942,11 @@
5805357942
assert( pRight->op==TK_DOT );
5805457943
zDb = pExpr->pLeft->u.zToken;
5805557944
zTable = pRight->pLeft->u.zToken;
5805657945
zColumn = pRight->pRight->u.zToken;
5805757946
}
58058
- lookupName(pParse, zDb, zTable, zColumn, pNC, pExpr);
58059
- return WRC_Prune;
57947
+ return lookupName(pParse, zDb, zTable, zColumn, pNC, pExpr);
5806057948
}
5806157949
5806257950
/* Resolve function names
5806357951
*/
5806457952
case TK_CONST_FUNC:
@@ -58072,10 +57960,11 @@
5807257960
int nId; /* Number of characters in function name */
5807357961
const char *zId; /* The function name. */
5807457962
FuncDef *pDef; /* Information about the function */
5807557963
u8 enc = ENC(pParse->db); /* The database encoding */
5807657964
57965
+ testcase( pExpr->op==TK_CONST_FUNC );
5807757966
assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
5807857967
zId = pExpr->u.zToken;
5807957968
nId = sqlite3Strlen30(zId);
5808057969
pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0);
5808157970
if( pDef==0 ){
@@ -58126,13 +58015,14 @@
5812658015
*/
5812758016
return WRC_Prune;
5812858017
}
5812958018
#ifndef SQLITE_OMIT_SUBQUERY
5813058019
case TK_SELECT:
58131
- case TK_EXISTS:
58020
+ case TK_EXISTS: testcase( pExpr->op==TK_EXISTS );
5813258021
#endif
5813358022
case TK_IN: {
58023
+ testcase( pExpr->op==TK_IN );
5813458024
if( ExprHasProperty(pExpr, EP_xIsSelect) ){
5813558025
int nRef = pNC->nRef;
5813658026
#ifndef SQLITE_OMIT_CHECK
5813758027
if( pNC->isCheck ){
5813858028
sqlite3ErrorMsg(pParse,"subqueries prohibited in CHECK constraints");
@@ -58177,11 +58067,11 @@
5817758067
){
5817858068
int i; /* Loop counter */
5817958069
5818058070
UNUSED_PARAMETER(pParse);
5818158071
58182
- if( pE->op==TK_ID || (pE->op==TK_STRING && pE->u.zToken[0]!='\'') ){
58072
+ if( pE->op==TK_ID ){
5818358073
char *zCol = pE->u.zToken;
5818458074
for(i=0; i<pEList->nExpr; i++){
5818558075
char *zAs = pEList->a[i].zName;
5818658076
if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
5818758077
return i+1;
@@ -58313,11 +58203,11 @@
5831358203
int iCol = -1;
5831458204
Expr *pE, *pDup;
5831558205
if( pItem->done ) continue;
5831658206
pE = pItem->pExpr;
5831758207
if( sqlite3ExprIsInteger(pE, &iCol) ){
58318
- if( iCol<0 || iCol>pEList->nExpr ){
58208
+ if( iCol<=0 || iCol>pEList->nExpr ){
5831958209
resolveOutOfRangeError(pParse, "ORDER", i+1, pEList->nExpr);
5832058210
return 1;
5832158211
}
5832258212
}else{
5832358213
iCol = resolveAsName(pParse, pEList, pE);
@@ -58327,13 +58217,10 @@
5832758217
assert(pDup);
5832858218
iCol = resolveOrderByTermToExprList(pParse, pSelect, pDup);
5832958219
}
5833058220
sqlite3ExprDelete(db, pDup);
5833158221
}
58332
- if( iCol<0 ){
58333
- return 1;
58334
- }
5833558222
}
5833658223
if( iCol>0 ){
5833758224
CollSeq *pColl = pE->pColl;
5833858225
int flags = pE->flags & EP_ExpCollate;
5833958226
sqlite3ExprDelete(db, pE);
@@ -58436,13 +58323,10 @@
5843658323
nResult = pSelect->pEList->nExpr;
5843758324
pParse = pNC->pParse;
5843858325
for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
5843958326
Expr *pE = pItem->pExpr;
5844058327
iCol = resolveAsName(pParse, pSelect->pEList, pE);
58441
- if( iCol<0 ){
58442
- return 1; /* OOM error */
58443
- }
5844458328
if( iCol>0 ){
5844558329
/* If an AS-name match is found, mark this ORDER BY column as being
5844658330
** a copy of the iCol-th result-set column. The subsequent call to
5844758331
** sqlite3ResolveOrderGroupBy() will convert the expression to a
5844858332
** copy of the iCol-th result-set expression. */
@@ -58768,11 +58652,11 @@
5876858652
**
5876958653
*************************************************************************
5877058654
** This file contains routines used for analyzing expressions and
5877158655
** for generating VDBE code that evaluates expressions in SQLite.
5877258656
**
58773
-** $Id: expr.c,v 1.445 2009/06/01 16:53:10 shane Exp $
58657
+** $Id: expr.c,v 1.446 2009/06/19 18:32:55 drh Exp $
5877458658
*/
5877558659
5877658660
/*
5877758661
** Return the 'affinity' of the expression pExpr if any.
5877858662
**
@@ -59245,15 +59129,15 @@
5924559129
exprSetHeight(pRoot);
5924659130
}
5924759131
}
5924859132
5924959133
/*
59250
-** Allocate a Expr node which joins up to two subtrees.
59134
+** Allocate a Expr node which joins as many as two subtrees.
5925159135
**
59252
-** The
59253
-** Works like sqlite3Expr() except that it takes an extra Parse*
59254
-** argument and notifies the associated connection object if malloc fails.
59136
+** One or both of the subtrees can be NULL. Return a pointer to the new
59137
+** Expr node. Or, if an OOM error occurs, set pParse->db->mallocFailed,
59138
+** free the subtrees and return NULL.
5925559139
*/
5925659140
SQLITE_PRIVATE Expr *sqlite3PExpr(
5925759141
Parse *pParse, /* Parsing context */
5925859142
int op, /* Expression opcode */
5925959143
Expr *pLeft, /* Left operand */
@@ -64140,11 +64024,11 @@
6414064024
** creating ID lists
6414164025
** BEGIN TRANSACTION
6414264026
** COMMIT
6414364027
** ROLLBACK
6414464028
**
64145
-** $Id: build.c,v 1.549 2009/06/03 11:25:07 danielk1977 Exp $
64029
+** $Id: build.c,v 1.552 2009/06/18 17:22:39 drh Exp $
6414664030
*/
6414764031
6414864032
/*
6414964033
** This routine is called when a new SQL statement is beginning to
6415064034
** be parsed. Initialize the pParse structure as needed.
@@ -65372,11 +65256,10 @@
6537265256
pColl = sqlite3FindCollSeq(db, enc, zName, initbusy);
6537365257
if( !initbusy && (!pColl || !pColl->xCmp) ){
6537465258
pColl = sqlite3GetCollSeq(db, pColl, zName);
6537565259
if( !pColl ){
6537665260
sqlite3ErrorMsg(pParse, "no such collation sequence: %s", zName);
65377
- pColl = 0;
6537865261
}
6537965262
}
6538065263
6538165264
return pColl;
6538265265
}
@@ -67756,11 +67639,11 @@
6775667639
*************************************************************************
6775767640
**
6775867641
** This file contains functions used to access the internal hash tables
6775967642
** of user defined functions and collation sequences.
6776067643
**
67761
-** $Id: callback.c,v 1.41 2009/05/20 02:40:46 drh Exp $
67644
+** $Id: callback.c,v 1.42 2009/06/17 00:35:31 drh Exp $
6776267645
*/
6776367646
6776467647
6776567648
/*
6776667649
** Invoke the 'collation needed' callback to request a collation sequence
@@ -67866,13 +67749,11 @@
6786667749
SQLITE_PRIVATE int sqlite3CheckCollSeq(Parse *pParse, CollSeq *pColl){
6786767750
if( pColl ){
6786867751
const char *zName = pColl->zName;
6786967752
CollSeq *p = sqlite3GetCollSeq(pParse->db, pColl, zName);
6787067753
if( !p ){
67871
- if( pParse->nErr==0 ){
67872
- sqlite3ErrorMsg(pParse, "no such collation sequence: %s", zName);
67873
- }
67754
+ sqlite3ErrorMsg(pParse, "no such collation sequence: %s", zName);
6787467755
pParse->nErr++;
6787567756
return SQLITE_ERROR;
6787667757
}
6787767758
assert( p==pColl );
6787867759
}
@@ -68083,11 +67964,10 @@
6808367964
int bestScore = 0; /* Score of best match */
6808467965
int h; /* Hash value */
6808567966
6808667967
6808767968
assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE );
68088
- if( nArg<-1 ) nArg = -1;
6808967969
h = (sqlite3UpperToLower[(u8)zName[0]] + nName) % ArraySize(db->aFunc.a);
6809067970
6809167971
/* First search for a match amongst the application-defined functions.
6809267972
*/
6809367973
p = functionSearch(&db->aFunc, h, zName, nName);
@@ -68833,11 +68713,11 @@
6883368713
**
6883468714
** There is only one exported symbol in this file - the function
6883568715
** sqliteRegisterBuildinFunctions() found at the bottom of the file.
6883668716
** All other code has file scope.
6883768717
**
68838
-** $Id: func.c,v 1.237 2009/06/03 01:24:54 drh Exp $
68718
+** $Id: func.c,v 1.239 2009/06/19 16:44:41 drh Exp $
6883968719
*/
6884068720
6884168721
/*
6884268722
** Return the collating function associated with a function.
6884368723
*/
@@ -70078,19 +69958,14 @@
7007869958
if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
7007969959
pAccum = (StrAccum*)sqlite3_aggregate_context(context, sizeof(*pAccum));
7008069960
7008169961
if( pAccum ){
7008269962
sqlite3 *db = sqlite3_context_db_handle(context);
70083
- int n;
69963
+ int firstTerm = pAccum->useMalloc==0;
7008469964
pAccum->useMalloc = 1;
7008569965
pAccum->mxAlloc = db->aLimit[SQLITE_LIMIT_LENGTH];
70086
-#ifdef SQLITE_OMIT_DEPRECATED
70087
- n = context->pMem->n;
70088
-#else
70089
- n = sqlite3_aggregate_count(context);
70090
-#endif
70091
- if( n>1 ){
69966
+ if( !firstTerm ){
7009269967
if( argc==2 ){
7009369968
zSep = (char*)sqlite3_value_text(argv[1]);
7009469969
nSep = sqlite3_value_bytes(argv[1]);
7009569970
}else{
7009669971
zSep = ",";
@@ -70132,13 +70007,10 @@
7013270007
assert( rc==SQLITE_NOMEM || rc==SQLITE_OK );
7013370008
if( rc==SQLITE_NOMEM ){
7013470009
db->mallocFailed = 1;
7013570010
}
7013670011
}
70137
-#ifdef SQLITE_SSE
70138
- (void)sqlite3SseFunctions(db);
70139
-#endif
7014070012
}
7014170013
7014270014
/*
7014370015
** Set the LIKEOPT flag on the 2-argument function with the given name.
7014470016
*/
@@ -73213,16 +73085,16 @@
7321373085
** May you share freely, never taking more than you give.
7321473086
**
7321573087
*************************************************************************
7321673088
** This file contains code used to implement the PRAGMA command.
7321773089
**
73218
-** $Id: pragma.c,v 1.212 2009/06/03 11:25:07 danielk1977 Exp $
73090
+** $Id: pragma.c,v 1.213 2009/06/19 14:06:03 drh Exp $
7321973091
*/
7322073092
7322173093
/* Ignore this whole file if pragmas are disabled
7322273094
*/
73223
-#if !defined(SQLITE_OMIT_PRAGMA) && !defined(SQLITE_OMIT_PARSER)
73095
+#if !defined(SQLITE_OMIT_PRAGMA)
7322473096
7322573097
/*
7322673098
** Interpret the given string as a safety level. Return 0 for OFF,
7322773099
** 1 for ON or NORMAL and 2 for FULL. Return 1 for an empty or
7322873100
** unrecognized string argument.
@@ -74549,21 +74421,10 @@
7454974421
}
7455074422
7455174423
}else
7455274424
#endif
7455374425
74554
-#ifdef SQLITE_SSE
74555
- /*
74556
- ** Check to see if the sqlite_statements table exists. Create it
74557
- ** if it does not.
74558
- */
74559
- if( sqlite3StrICmp(zLeft, "create_sqlite_statement_table")==0 ){
74560
- extern int sqlite3CreateStatementsTable(Parse*);
74561
- sqlite3CreateStatementsTable(pParse);
74562
- }else
74563
-#endif
74564
-
7456574426
#if SQLITE_HAS_CODEC
7456674427
if( sqlite3StrICmp(zLeft, "key")==0 && zRight ){
7456774428
sqlite3_key(db, zRight, sqlite3Strlen30(zRight));
7456874429
}else
7456974430
if( sqlite3StrICmp(zLeft, "rekey")==0 && zRight ){
@@ -74624,11 +74485,11 @@
7462474485
pragma_out:
7462574486
sqlite3DbFree(db, zLeft);
7462674487
sqlite3DbFree(db, zRight);
7462774488
}
7462874489
74629
-#endif /* SQLITE_OMIT_PRAGMA || SQLITE_OMIT_PARSER */
74490
+#endif /* SQLITE_OMIT_PRAGMA */
7463074491
7463174492
/************** End of pragma.c **********************************************/
7463274493
/************** Begin file prepare.c *****************************************/
7463374494
/*
7463474495
** 2005 May 25
@@ -74643,11 +74504,11 @@
7464374504
*************************************************************************
7464474505
** This file contains the implementation of the sqlite3_prepare()
7464574506
** interface, and routines that contribute to loading the database schema
7464674507
** from disk.
7464774508
**
74648
-** $Id: prepare.c,v 1.121 2009/06/04 00:11:56 drh Exp $
74509
+** $Id: prepare.c,v 1.124 2009/06/22 12:05:10 drh Exp $
7464974510
*/
7465074511
7465174512
/*
7465274513
** Fill the InitData structure with an error message that indicates
7465374514
** that the database is corrupt.
@@ -74660,16 +74521,16 @@
7466074521
sqlite3 *db = pData->db;
7466174522
if( !db->mallocFailed && (db->flags & SQLITE_RecoveryMode)==0 ){
7466274523
if( zObj==0 ) zObj = "?";
7466374524
sqlite3SetString(pData->pzErrMsg, pData->db,
7466474525
"malformed database schema (%s)", zObj);
74665
- if( zExtra && zExtra[0] ){
74526
+ if( zExtra ){
7466674527
*pData->pzErrMsg = sqlite3MAppendf(pData->db, *pData->pzErrMsg, "%s - %s",
7466774528
*pData->pzErrMsg, zExtra);
7466874529
}
7466974530
}
74670
- pData->rc = SQLITE_CORRUPT;
74531
+ pData->rc = db->mallocFailed ? SQLITE_NOMEM : SQLITE_CORRUPT;
7467174532
}
7467274533
7467374534
/*
7467474535
** This is the callback routine for the code that initializes the
7467574536
** database. See sqlite3Init() below for additional information.
@@ -74691,11 +74552,11 @@
7469174552
UNUSED_PARAMETER2(NotUsed, argc);
7469274553
assert( sqlite3_mutex_held(db->mutex) );
7469374554
DbClearProperty(db, iDb, DB_Empty);
7469474555
if( db->mallocFailed ){
7469574556
corruptSchema(pData, argv[0], 0);
74696
- return SQLITE_NOMEM;
74557
+ return 1;
7469774558
}
7469874559
7469974560
assert( iDb>=0 && iDb<db->nDb );
7470074561
if( argv==0 ) return 0; /* Might happen if EMPTY_RESULT_CALLBACKS are on */
7470174562
if( argv[1]==0 ){
@@ -74716,11 +74577,11 @@
7471674577
assert( rc!=SQLITE_OK || zErr==0 );
7471774578
if( SQLITE_OK!=rc ){
7471874579
pData->rc = rc;
7471974580
if( rc==SQLITE_NOMEM ){
7472074581
db->mallocFailed = 1;
74721
- }else if( rc!=SQLITE_INTERRUPT && (rc&0xff)!=SQLITE_LOCKED ){
74582
+ }else if( rc!=SQLITE_INTERRUPT ){
7472274583
corruptSchema(pData, argv[0], zErr);
7472374584
}
7472474585
sqlite3DbFree(db, zErr);
7472574586
}
7472674587
}else if( argv[0]==0 ){
@@ -74732,19 +74593,19 @@
7473274593
** been created when we processed the CREATE TABLE. All we have
7473374594
** to do here is record the root page number for that index.
7473474595
*/
7473574596
Index *pIndex;
7473674597
pIndex = sqlite3FindIndex(db, argv[0], db->aDb[iDb].zName);
74737
- if( pIndex==0 || pIndex->tnum!=0 ){
74598
+ if( pIndex==0 ){
7473874599
/* This can occur if there exists an index on a TEMP table which
7473974600
** has the same name as another index on a permanent index. Since
7474074601
** the permanent table is hidden by the TEMP table, we can also
7474174602
** safely ignore the index on the permanent table.
7474274603
*/
7474374604
/* Do Nothing */;
74744
- }else{
74745
- pIndex->tnum = atoi(argv[1]);
74605
+ }else if( sqlite3GetInt32(argv[1], &pIndex->tnum)==0 ){
74606
+ corruptSchema(pData, argv[0], "invalid rootpage");
7474674607
}
7474774608
}
7474874609
return 0;
7474974610
}
7475074611
@@ -74826,19 +74687,19 @@
7482674687
if( initData.rc ){
7482774688
rc = initData.rc;
7482874689
goto error_out;
7482974690
}
7483074691
pTab = sqlite3FindTable(db, zMasterName, db->aDb[iDb].zName);
74831
- if( pTab ){
74692
+ if( ALWAYS(pTab) ){
7483274693
pTab->tabFlags |= TF_Readonly;
7483374694
}
7483474695
7483574696
/* Create a cursor to hold the database open
7483674697
*/
7483774698
pDb = &db->aDb[iDb];
7483874699
if( pDb->pBt==0 ){
74839
- if( !OMIT_TEMPDB && iDb==1 ){
74700
+ if( !OMIT_TEMPDB && ALWAYS(iDb==1) ){
7484074701
DbSetProperty(db, 1, DB_SchemaLoaded);
7484174702
}
7484274703
return SQLITE_OK;
7484374704
}
7484474705
curMain = sqlite3MallocZero(sqlite3BtreeCursorSize());
@@ -74854,12 +74715,17 @@
7485474715
**
7485574716
** Meta values are as follows:
7485674717
** meta[0] Schema cookie. Changes with each schema change.
7485774718
** meta[1] File format of schema layer.
7485874719
** meta[2] Size of the page cache.
74859
- ** meta[3] Use freelist if 0. Autovacuum if greater than zero.
74720
+ ** meta[3] Largest rootpage (auto/incr_vacuum mode)
7486074721
** meta[4] Db text encoding. 1:UTF-8 2:UTF-16LE 3:UTF-16BE
74722
+ ** meta[5] User version
74723
+ ** meta[6] Incremental vacuum mode
74724
+ ** meta[7] unused
74725
+ ** meta[8] unused
74726
+ ** meta[9] unused
7486174727
**
7486274728
** Note: The #defined SQLITE_UTF* symbols in sqliteInt.h correspond to
7486374729
** the possible values of meta[4].
7486474730
*/
7486574731
for(i=0; rc==SQLITE_OK && i<ArraySize(meta); i++){
@@ -74932,14 +74798,11 @@
7493274798
}
7493374799
7493474800
/* Read the schema information out of the schema tables
7493574801
*/
7493674802
assert( db->init.busy );
74937
- if( rc==SQLITE_EMPTY ){
74938
- /* For an empty database, there is nothing to read */
74939
- rc = SQLITE_OK;
74940
- }else{
74803
+ {
7494174804
char *zSql;
7494274805
zSql = sqlite3MPrintf(db,
7494374806
"SELECT name, rootpage, sql FROM '%q'.%s",
7494474807
db->aDb[iDb].zName, zMasterName);
7494574808
(void)sqlite3SafetyOff(db);
@@ -75009,11 +74872,10 @@
7500974872
SQLITE_PRIVATE int sqlite3Init(sqlite3 *db, char **pzErrMsg){
7501074873
int i, rc;
7501174874
int commit_internal = !(db->flags&SQLITE_InternChanges);
7501274875
7501374876
assert( sqlite3_mutex_held(db->mutex) );
75014
- if( db->init.busy ) return SQLITE_OK;
7501574877
rc = SQLITE_OK;
7501674878
db->init.busy = 1;
7501774879
for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
7501874880
if( DbHasProperty(db, i, DB_SchemaLoaded) || i==1 ) continue;
7501974881
rc = sqlite3InitOne(db, i, pzErrMsg);
@@ -75025,11 +74887,12 @@
7502574887
/* Once all the other databases have been initialised, load the schema
7502674888
** for the TEMP database. This is loaded last, as the TEMP database
7502774889
** schema may contain references to objects in other databases.
7502874890
*/
7502974891
#ifndef SQLITE_OMIT_TEMPDB
75030
- if( rc==SQLITE_OK && db->nDb>1 && !DbHasProperty(db, 1, DB_SchemaLoaded) ){
74892
+ if( rc==SQLITE_OK && ALWAYS(db->nDb>1)
74893
+ && !DbHasProperty(db, 1, DB_SchemaLoaded) ){
7503174894
rc = sqlite3InitOne(db, 1, pzErrMsg);
7503274895
if( rc ){
7503374896
sqlite3ResetInternalSchema(db, 1);
7503474897
}
7503574898
}
@@ -75082,16 +74945,17 @@
7508274945
if( pBt==0 ) continue;
7508374946
memset(curTemp, 0, sqlite3BtreeCursorSize());
7508474947
rc = sqlite3BtreeCursor(pBt, MASTER_ROOT, 0, 0, curTemp);
7508574948
if( rc==SQLITE_OK ){
7508674949
rc = sqlite3BtreeGetMeta(pBt, BTREE_SCHEMA_VERSION, (u32 *)&cookie);
75087
- if( rc==SQLITE_OK && cookie!=db->aDb[iDb].pSchema->schema_cookie ){
74950
+ if( ALWAYS(rc==SQLITE_OK)
74951
+ && cookie!=db->aDb[iDb].pSchema->schema_cookie ){
7508874952
allOk = 0;
7508974953
}
7509074954
sqlite3BtreeCloseCursor(curTemp);
7509174955
}
75092
- if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
74956
+ if( NEVER(rc==SQLITE_NOMEM) || rc==SQLITE_IOERR_NOMEM ){
7509374957
db->mallocFailed = 1;
7509474958
}
7509574959
}
7509674960
sqlite3_free(curTemp);
7509774961
}else{
@@ -75206,10 +75070,12 @@
7520675070
7520775071
pParse->db = db;
7520875072
if( nBytes>=0 && (nBytes==0 || zSql[nBytes-1]!=0) ){
7520975073
char *zSqlCopy;
7521075074
int mxLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
75075
+ testcase( nBytes==mxLen );
75076
+ testcase( nBytes==mxLen+1 );
7521175077
if( nBytes>mxLen ){
7521275078
sqlite3Error(db, SQLITE_TOOBIG, "statement too long");
7521375079
(void)sqlite3SafetyOff(db);
7521475080
rc = sqlite3ApiExit(db, SQLITE_TOOBIG);
7521575081
goto end_prepare;
@@ -75312,10 +75178,14 @@
7531275178
return SQLITE_MISUSE;
7531375179
}
7531475180
sqlite3_mutex_enter(db->mutex);
7531575181
sqlite3BtreeEnterAll(db);
7531675182
rc = sqlite3Prepare(db, zSql, nBytes, saveSqlFlag, ppStmt, pzTail);
75183
+ if( rc==SQLITE_SCHEMA ){
75184
+ sqlite3_finalize(*ppStmt);
75185
+ rc = sqlite3Prepare(db, zSql, nBytes, saveSqlFlag, ppStmt, pzTail);
75186
+ }
7531775187
sqlite3BtreeLeaveAll(db);
7531875188
sqlite3_mutex_leave(db->mutex);
7531975189
return rc;
7532075190
}
7532175191
@@ -75485,11 +75355,11 @@
7548575355
**
7548675356
*************************************************************************
7548775357
** This file contains C code routines that are called by the parser
7548875358
** to handle SELECT statements in SQLite.
7548975359
**
75490
-** $Id: select.c,v 1.523 2009/06/01 16:53:10 shane Exp $
75360
+** $Id: select.c,v 1.524 2009/06/12 03:27:27 drh Exp $
7549175361
*/
7549275362
7549375363
7549475364
/*
7549575365
** Delete all the content of a Select structure but do not deallocate
@@ -78269,10 +78139,11 @@
7826978139
7827078140
/* Transfer the FROM clause terms from the subquery into the
7827178141
** outer query.
7827278142
*/
7827378143
for(i=0; i<nSubSrc; i++){
78144
+ sqlite3IdListDelete(db, pSrc->a[i+iFrom].pUsing);
7827478145
pSrc->a[i+iFrom] = pSubSrc->a[i];
7827578146
memset(&pSubSrc->a[i], 0, sizeof(pSubSrc->a[i]));
7827678147
}
7827778148
pSrc->a[iFrom].jointype = jointype;
7827878149
@@ -81764,11 +81635,11 @@
8176481635
** May you share freely, never taking more than you give.
8176581636
**
8176681637
*************************************************************************
8176781638
** This file contains code used to help implement virtual tables.
8176881639
**
81769
-** $Id: vtab.c,v 1.90 2009/06/02 21:31:39 drh Exp $
81640
+** $Id: vtab.c,v 1.91 2009/06/15 16:27:08 shane Exp $
8177081641
*/
8177181642
#ifndef SQLITE_OMIT_VIRTUALTABLE
8177281643
8177381644
/*
8177481645
** The actual function that does the work of creating a new module.
@@ -81854,10 +81725,13 @@
8185481725
/*
8185581726
** Unlock a virtual table. When the last lock is removed,
8185681727
** disconnect the virtual table.
8185781728
*/
8185881729
SQLITE_PRIVATE void sqlite3VtabUnlock(sqlite3 *db, sqlite3_vtab *pVtab){
81730
+#ifndef SQLITE_DEBUG
81731
+ UNUSED_PARAMETER(db);
81732
+#endif
8185981733
assert( pVtab->nRef>0 );
8186081734
pVtab->nRef--;
8186181735
assert(db);
8186281736
assert( sqlite3SafetyCheckOk(db) );
8186381737
if( pVtab->nRef==0 ){
@@ -82633,11 +82507,11 @@
8263382507
** generating the code that loops through a table looking for applicable
8263482508
** rows. Indices are selected and used to speed the search when doing
8263582509
** so is applicable. Because this module is responsible for selecting
8263682510
** indices, you might also think of this module as the "query optimizer".
8263782511
**
82638
-** $Id: where.c,v 1.401 2009/06/06 15:17:28 drh Exp $
82512
+** $Id: where.c,v 1.408 2009/06/16 14:15:22 shane Exp $
8263982513
*/
8264082514
8264182515
/*
8264282516
** Trace output macros
8264382517
*/
@@ -83275,23 +83149,24 @@
8327583149
if( pColl==0 ) return 0;
8327683150
if( (pColl->type!=SQLITE_COLL_BINARY || *pnoCase) &&
8327783151
(pColl->type!=SQLITE_COLL_NOCASE || !*pnoCase) ){
8327883152
return 0;
8327983153
}
83154
+ if( sqlite3ExprAffinity(pLeft)!=SQLITE_AFF_TEXT ) return 0;
8328083155
z = pRight->u.zToken;
83281
- cnt = 0;
8328283156
if( ALWAYS(z) ){
83157
+ cnt = 0;
8328383158
while( (c=z[cnt])!=0 && c!=wc[0] && c!=wc[1] && c!=wc[2] ){
8328483159
cnt++;
8328583160
}
83161
+ if( cnt!=0 && c!=0 && 255!=(u8)z[cnt-1] ){
83162
+ *pisComplete = z[cnt]==wc[0] && z[cnt+1]==0;
83163
+ *pnPattern = cnt;
83164
+ return 1;
83165
+ }
8328683166
}
83287
- if( cnt==0 || c==0 || 255==(u8)z[cnt-1] ){
83288
- return 0;
83289
- }
83290
- *pisComplete = z[cnt]==wc[0] && z[cnt+1]==0;
83291
- *pnPattern = cnt;
83292
- return 1;
83167
+ return 0;
8329383168
}
8329483169
#endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */
8329583170
8329683171
8329783172
#ifndef SQLITE_OMIT_VIRTUALTABLE
@@ -83507,10 +83382,26 @@
8350783382
8350883383
/*
8350983384
** chngToIN holds a set of tables that *might* satisfy case 1. But
8351083385
** we have to do some additional checking to see if case 1 really
8351183386
** is satisfied.
83387
+ **
83388
+ ** chngToIN will hold either 0, 1, or 2 bits. The 0-bit case means
83389
+ ** that there is no possibility of transforming the OR clause into an
83390
+ ** IN operator because one or more terms in the OR clause contain
83391
+ ** something other than == on a column in the single table. The 1-bit
83392
+ ** case means that every term of the OR clause is of the form
83393
+ ** "table.column=expr" for some single table. The one bit that is set
83394
+ ** will correspond to the common table. We still need to check to make
83395
+ ** sure the same column is used on all terms. The 2-bit case is when
83396
+ ** the all terms are of the form "table1.column=table2.column". It
83397
+ ** might be possible to form an IN operator with either table1.column
83398
+ ** or table2.column as the LHS if either is common to every term of
83399
+ ** the OR clause.
83400
+ **
83401
+ ** Note that terms of the form "table.column1=table.column2" (the
83402
+ ** same table on both sizes of the ==) cannot be optimized.
8351283403
*/
8351383404
if( chngToIN ){
8351483405
int okToChngToIN = 0; /* True if the conversion to IN is valid */
8351583406
int iColumn = -1; /* Column index on lhs of IN operator */
8351683407
int iCursor = -1; /* Table cursor common to all terms */
@@ -83525,22 +83416,42 @@
8352583416
for(j=0; j<2 && !okToChngToIN; j++){
8352683417
pOrTerm = pOrWc->a;
8352783418
for(i=pOrWc->nTerm-1; i>=0; i--, pOrTerm++){
8352883419
assert( pOrTerm->eOperator==WO_EQ );
8352983420
pOrTerm->wtFlags &= ~TERM_OR_OK;
83530
- if( pOrTerm->leftCursor==iColumn ) continue;
83531
- if( (chngToIN & getMask(pMaskSet, pOrTerm->leftCursor))==0 ) continue;
83421
+ if( pOrTerm->leftCursor==iCursor ){
83422
+ /* This is the 2-bit case and we are on the second iteration and
83423
+ ** current term is from the first iteration. So skip this term. */
83424
+ assert( j==1 );
83425
+ continue;
83426
+ }
83427
+ if( (chngToIN & getMask(pMaskSet, pOrTerm->leftCursor))==0 ){
83428
+ /* This term must be of the form t1.a==t2.b where t2 is in the
83429
+ ** chngToIN set but t1 is not. This term will be either preceeded
83430
+ ** or follwed by an inverted copy (t2.b==t1.a). Skip this term
83431
+ ** and use its inversion. */
83432
+ testcase( pOrTerm->wtFlags & TERM_COPIED );
83433
+ testcase( pOrTerm->wtFlags & TERM_VIRTUAL );
83434
+ assert( pOrTerm->wtFlags & (TERM_COPIED|TERM_VIRTUAL) );
83435
+ continue;
83436
+ }
8353283437
iColumn = pOrTerm->u.leftColumn;
8353383438
iCursor = pOrTerm->leftCursor;
8353483439
break;
8353583440
}
8353683441
if( i<0 ){
83442
+ /* No candidate table+column was found. This can only occur
83443
+ ** on the second iteration */
8353783444
assert( j==1 );
8353883445
assert( (chngToIN&(chngToIN-1))==0 );
83539
- assert( chngToIN==getMask(pMaskSet, iColumn) );
83446
+ assert( chngToIN==getMask(pMaskSet, iCursor) );
8354083447
break;
8354183448
}
83449
+ testcase( j==1 );
83450
+
83451
+ /* We have found a candidate table and column. Check to see if that
83452
+ ** table and column is common to every term in the OR clause */
8354283453
okToChngToIN = 1;
8354383454
for(; i>=0 && okToChngToIN; i--, pOrTerm++){
8354483455
assert( pOrTerm->eOperator==WO_EQ );
8354583456
if( pOrTerm->leftCursor!=iCursor ){
8354683457
pOrTerm->wtFlags &= ~TERM_OR_OK;
@@ -83782,15 +83693,22 @@
8378283693
pRight = pExpr->x.pList->a[0].pExpr;
8378383694
pStr1 = sqlite3Expr(db, TK_STRING, pRight->u.zToken);
8378483695
if( pStr1 ) pStr1->u.zToken[nPattern] = 0;
8378583696
pStr2 = sqlite3ExprDup(db, pStr1, 0);
8378683697
if( !db->mallocFailed ){
83787
- u8 c, *pC;
83698
+ u8 c, *pC; /* Last character before the first wildcard */
8378883699
pC = (u8*)&pStr2->u.zToken[nPattern-1];
8378983700
c = *pC;
8379083701
if( noCase ){
83791
- if( c=='@' ) isComplete = 0;
83702
+ /* The point is to increment the last character before the first
83703
+ ** wildcard. But if we increment '@', that will push it into the
83704
+ ** alphabetic range where case conversions will mess up the
83705
+ ** inequality. To avoid this, make sure to also run the full
83706
+ ** LIKE on all candidate expressions by clearing the isComplete flag
83707
+ */
83708
+ if( c=='A'-1 ) isComplete = 0;
83709
+
8379283710
c = sqlite3UpperToLower[c];
8379383711
}
8379483712
*pC = c + 1;
8379583713
}
8379683714
pNewExpr1 = sqlite3PExpr(pParse, TK_GE, sqlite3ExprDup(db,pLeft,0),pStr1,0);
@@ -84456,11 +84374,11 @@
8445684374
pCost->rCost = (SQLITE_BIG_DBL/((double)2));
8445784375
}else{
8445884376
pCost->rCost = pIdxInfo->estimatedCost;
8445984377
}
8446084378
pCost->plan.u.pVtabIdx = pIdxInfo;
84461
- if( pIdxInfo && pIdxInfo->orderByConsumed ){
84379
+ if( pIdxInfo->orderByConsumed ){
8446284380
pCost->plan.wsFlags |= WHERE_ORDERBY;
8446384381
}
8446484382
pCost->plan.nEq = 0;
8446584383
pIdxInfo->nOrderBy = nOrderBy;
8446684384
@@ -84742,11 +84660,11 @@
8474284660
}
8474384661
}else{
8474484662
cost += cost*estLog(cost);
8474584663
WHERETRACE(("...... orderby increases cost to %.9g\n", cost));
8474684664
}
84747
- }else if( pParse->db->flags & SQLITE_ReverseOrder ){
84665
+ }else if( wsFlags!=0 && (pParse->db->flags & SQLITE_ReverseOrder)!=0 ){
8474884666
/* For application testing, randomly reverse the output order for
8474984667
** SELECT statements that omit the ORDER BY clause. This will help
8475084668
** to find cases where
8475184669
*/
8475284670
wsFlags |= WHERE_REVERSE;
@@ -84805,18 +84723,21 @@
8480584723
struct SrcList_item *pSrc, /* The FROM clause term to search */
8480684724
Bitmask notReady, /* Mask of cursors that are not available */
8480784725
ExprList *pOrderBy, /* The ORDER BY clause */
8480884726
WhereCost *pCost /* Lowest cost query plan */
8480984727
){
84728
+#ifndef SQLITE_OMIT_VIRTUALTABLE
8481084729
if( IsVirtual(pSrc->pTab) ){
8481184730
sqlite3_index_info *p = 0;
8481284731
bestVirtualIndex(pParse, pWC, pSrc, notReady, pOrderBy, pCost, &p);
8481384732
if( p->needToFreeIdxStr ){
8481484733
sqlite3_free(p->idxStr);
8481584734
}
8481684735
sqlite3DbFree(pParse->db, p);
84817
- }else{
84736
+ }else
84737
+#endif
84738
+ {
8481884739
bestBtreeIndex(pParse, pWC, pSrc, notReady, pOrderBy, pCost);
8481984740
}
8482084741
}
8482184742
8482284743
/*
@@ -85448,12 +85369,12 @@
8544885369
WhereClause *pOrWc; /* The OR-clause broken out into subterms */
8544985370
WhereTerm *pFinal; /* Final subterm within the OR-clause. */
8545085371
SrcList oneTab; /* Shortened table list */
8545185372
8545285373
int regReturn = ++pParse->nMem; /* Register used with OP_Gosub */
85453
- int regRowset; /* Register for RowSet object */
85454
- int regRowid; /* Register holding rowid */
85374
+ int regRowset = 0; /* Register for RowSet object */
85375
+ int regRowid = 0; /* Register holding rowid */
8545585376
int iLoopBody = sqlite3VdbeMakeLabel(v); /* Start of loop body */
8545685377
int iRetInit; /* Address of regReturn init */
8545785378
int ii;
8545885379
8545985380
pTerm = pLevel->plan.u.pTerm;
@@ -85488,21 +85409,20 @@
8548885409
8548985410
for(ii=0; ii<pOrWc->nTerm; ii++){
8549085411
WhereTerm *pOrTerm = &pOrWc->a[ii];
8549185412
if( pOrTerm->leftCursor==iCur || pOrTerm->eOperator==WO_AND ){
8549285413
WhereInfo *pSubWInfo; /* Info for single OR-term scan */
85493
-
8549485414
/* Loop through table entries that match term pOrTerm. */
8549585415
pSubWInfo = sqlite3WhereBegin(pParse, &oneTab, pOrTerm->pExpr, 0,
8549685416
WHERE_OMIT_OPEN | WHERE_OMIT_CLOSE | WHERE_FORCE_TABLE);
8549785417
if( pSubWInfo ){
8549885418
if( (wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
8549985419
int iSet = ((ii==pOrWc->nTerm-1)?-1:ii);
8550085420
int r;
8550185421
r = sqlite3ExprCodeGetColumn(pParse, pTabItem->pTab, -1, iCur,
8550285422
regRowid, 0);
85503
- sqlite3VdbeAddOp4(v, OP_RowSetTest, regRowset,
85423
+ sqlite3VdbeAddOp4(v, OP_RowSetTest, regRowset,
8550485424
sqlite3VdbeCurrentAddr(v)+2,
8550585425
r, SQLITE_INT_TO_PTR(iSet), P4_INT32);
8550685426
}
8550785427
sqlite3VdbeAddOp2(v, OP_Gosub, regReturn, iLoopBody);
8550885428
@@ -85789,13 +85709,15 @@
8578985709
** with virtual tables.
8579085710
*/
8579185711
assert( pWC->vmask==0 && pMaskSet->n==0 );
8579285712
for(i=0; i<pTabList->nSrc; i++){
8579385713
createMask(pMaskSet, pTabList->a[i].iCursor);
85794
- if( pTabList->a[i].pTab && IsVirtual(pTabList->a[i].pTab) ){
85714
+#ifndef SQLITE_OMIT_VIRTUALTABLE
85715
+ if( ALWAYS(pTabList->a[i].pTab) && IsVirtual(pTabList->a[i].pTab) ){
8579585716
pWC->vmask |= ((Bitmask)1 << i);
8579685717
}
85718
+#endif
8579785719
}
8579885720
#ifndef NDEBUG
8579985721
{
8580085722
Bitmask toTheLeft = 0;
8580185723
for(i=0; i<pTabList->nSrc; i++){
@@ -86209,10 +86131,21 @@
8620986131
*/
8621086132
/* First off, code is included that follows the "include" declaration
8621186133
** in the input grammar file. */
8621286134
8621386135
86136
+/*
86137
+** Disable all error recovery processing in the parser push-down
86138
+** automaton.
86139
+*/
86140
+#define YYNOERRORRECOVERY 1
86141
+
86142
+/*
86143
+** Make yytestcase() the same as testcase()
86144
+*/
86145
+#define yytestcase(X) testcase(X)
86146
+
8621486147
/*
8621586148
** An instance of this structure holds information about the
8621686149
** LIMIT clause of a SELECT statement.
8621786150
*/
8621886151
struct LimitVal {
@@ -86354,11 +86287,11 @@
8635486287
** YYNSTATE the combined number of states.
8635586288
** YYNRULE the number of rules in the grammar
8635686289
** YYERRORSYMBOL is the code number of the error symbol. If not
8635786290
** defined, then do no error processing.
8635886291
*/
86359
-#define YYCODETYPE unsigned short int
86292
+#define YYCODETYPE unsigned char
8636086293
#define YYNOCODE 252
8636186294
#define YYACTIONTYPE unsigned short int
8636286295
#define YYWILDCARD 65
8636386296
#define sqlite3ParserTOKENTYPE Token
8636486297
typedef union {
@@ -86392,10 +86325,22 @@
8639286325
#define YY_ERROR_ACTION (YYNSTATE+YYNRULE)
8639386326
8639486327
/* The yyzerominor constant is used to initialize instances of
8639586328
** YYMINORTYPE objects to zero. */
8639686329
static const YYMINORTYPE yyzerominor = { 0 };
86330
+
86331
+/* Define the yytestcase() macro to be a no-op if is not already defined
86332
+** otherwise.
86333
+**
86334
+** Applications can choose to define yytestcase() in the %include section
86335
+** to a macro that can assist in verifying code coverage. For production
86336
+** code the yytestcase() macro should be turned off. But it is useful
86337
+** for testing.
86338
+*/
86339
+#ifndef yytestcase
86340
+# define yytestcase(X)
86341
+#endif
8639786342
8639886343
8639986344
/* Next are the tables used to determine what action to take based on the
8640086345
** current state and lookahead token. These tables are used to implement
8640186346
** functions that take a state number and lookahead value and return an
@@ -86970,86 +86915,10 @@
8697086915
26, /* VIEW => ID */
8697186916
26, /* VIRTUAL => ID */
8697286917
26, /* REINDEX => ID */
8697386918
26, /* RENAME => ID */
8697486919
26, /* CTIME_KW => ID */
86975
- 0, /* ANY => nothing */
86976
- 0, /* OR => nothing */
86977
- 0, /* AND => nothing */
86978
- 0, /* IS => nothing */
86979
- 0, /* BETWEEN => nothing */
86980
- 0, /* IN => nothing */
86981
- 0, /* ISNULL => nothing */
86982
- 0, /* NOTNULL => nothing */
86983
- 0, /* NE => nothing */
86984
- 0, /* EQ => nothing */
86985
- 0, /* GT => nothing */
86986
- 0, /* LE => nothing */
86987
- 0, /* LT => nothing */
86988
- 0, /* GE => nothing */
86989
- 0, /* ESCAPE => nothing */
86990
- 0, /* BITAND => nothing */
86991
- 0, /* BITOR => nothing */
86992
- 0, /* LSHIFT => nothing */
86993
- 0, /* RSHIFT => nothing */
86994
- 0, /* PLUS => nothing */
86995
- 0, /* MINUS => nothing */
86996
- 0, /* STAR => nothing */
86997
- 0, /* SLASH => nothing */
86998
- 0, /* REM => nothing */
86999
- 0, /* CONCAT => nothing */
87000
- 0, /* COLLATE => nothing */
87001
- 0, /* UMINUS => nothing */
87002
- 0, /* UPLUS => nothing */
87003
- 0, /* BITNOT => nothing */
87004
- 0, /* STRING => nothing */
87005
- 0, /* JOIN_KW => nothing */
87006
- 0, /* CONSTRAINT => nothing */
87007
- 0, /* DEFAULT => nothing */
87008
- 0, /* NULL => nothing */
87009
- 0, /* PRIMARY => nothing */
87010
- 0, /* UNIQUE => nothing */
87011
- 0, /* CHECK => nothing */
87012
- 0, /* REFERENCES => nothing */
87013
- 0, /* AUTOINCR => nothing */
87014
- 0, /* ON => nothing */
87015
- 0, /* DELETE => nothing */
87016
- 0, /* UPDATE => nothing */
87017
- 0, /* INSERT => nothing */
87018
- 0, /* SET => nothing */
87019
- 0, /* DEFERRABLE => nothing */
87020
- 0, /* FOREIGN => nothing */
87021
- 0, /* DROP => nothing */
87022
- 0, /* UNION => nothing */
87023
- 0, /* ALL => nothing */
87024
- 0, /* EXCEPT => nothing */
87025
- 0, /* INTERSECT => nothing */
87026
- 0, /* SELECT => nothing */
87027
- 0, /* DISTINCT => nothing */
87028
- 0, /* DOT => nothing */
87029
- 0, /* FROM => nothing */
87030
- 0, /* JOIN => nothing */
87031
- 0, /* USING => nothing */
87032
- 0, /* ORDER => nothing */
87033
- 0, /* GROUP => nothing */
87034
- 0, /* HAVING => nothing */
87035
- 0, /* LIMIT => nothing */
87036
- 0, /* WHERE => nothing */
87037
- 0, /* INTO => nothing */
87038
- 0, /* VALUES => nothing */
87039
- 0, /* INTEGER => nothing */
87040
- 0, /* FLOAT => nothing */
87041
- 0, /* BLOB => nothing */
87042
- 0, /* REGISTER => nothing */
87043
- 0, /* VARIABLE => nothing */
87044
- 0, /* CASE => nothing */
87045
- 0, /* WHEN => nothing */
87046
- 0, /* THEN => nothing */
87047
- 0, /* ELSE => nothing */
87048
- 0, /* INDEX => nothing */
87049
- 0, /* ALTER => nothing */
87050
- 0, /* ADD => nothing */
8705186920
};
8705286921
#endif /* YYFALLBACK */
8705386922
8705486923
/* The following structure represents a single element of the
8705586924
** parser's stack. Information stored includes:
@@ -88270,52 +88139,10 @@
8827088139
** #line <lineno> <grammarfile>
8827188140
** { ... } // User supplied code
8827288141
** #line <lineno> <thisfile>
8827388142
** break;
8827488143
*/
88275
- case 0: /* input ::= cmdlist */
88276
- case 1: /* cmdlist ::= cmdlist ecmd */
88277
- case 2: /* cmdlist ::= ecmd */
88278
- case 3: /* ecmd ::= SEMI */
88279
- case 4: /* ecmd ::= explain cmdx SEMI */
88280
- case 10: /* trans_opt ::= */
88281
- case 11: /* trans_opt ::= TRANSACTION */
88282
- case 12: /* trans_opt ::= TRANSACTION nm */
88283
- case 20: /* savepoint_opt ::= SAVEPOINT */
88284
- case 21: /* savepoint_opt ::= */
88285
- case 25: /* cmd ::= create_table create_table_args */
88286
- case 34: /* columnlist ::= columnlist COMMA column */
88287
- case 35: /* columnlist ::= column */
88288
- case 44: /* type ::= */
88289
- case 51: /* signed ::= plus_num */
88290
- case 52: /* signed ::= minus_num */
88291
- case 53: /* carglist ::= carglist carg */
88292
- case 54: /* carglist ::= */
88293
- case 55: /* carg ::= CONSTRAINT nm ccons */
88294
- case 56: /* carg ::= ccons */
88295
- case 62: /* ccons ::= NULL onconf */
88296
- case 89: /* conslist ::= conslist COMMA tcons */
88297
- case 90: /* conslist ::= conslist tcons */
88298
- case 91: /* conslist ::= tcons */
88299
- case 92: /* tcons ::= CONSTRAINT nm */
88300
- case 268: /* plus_opt ::= PLUS */
88301
- case 269: /* plus_opt ::= */
88302
- case 279: /* foreach_clause ::= */
88303
- case 280: /* foreach_clause ::= FOR EACH ROW */
88304
- case 300: /* database_kw_opt ::= DATABASE */
88305
- case 301: /* database_kw_opt ::= */
88306
- case 309: /* kwcolumn_opt ::= */
88307
- case 310: /* kwcolumn_opt ::= COLUMNKW */
88308
- case 314: /* vtabarglist ::= vtabarg */
88309
- case 315: /* vtabarglist ::= vtabarglist COMMA vtabarg */
88310
- case 317: /* vtabarg ::= vtabarg vtabargtoken */
88311
- case 321: /* anylist ::= */
88312
- case 322: /* anylist ::= anylist LP anylist RP */
88313
- case 323: /* anylist ::= anylist ANY */
88314
-{
88315
-}
88316
- break;
8831788144
case 5: /* explain ::= */
8831888145
{ sqlite3BeginParse(pParse, 0); }
8831988146
break;
8832088147
case 6: /* explain ::= EXPLAIN */
8832188148
{ sqlite3BeginParse(pParse, 1); }
@@ -88331,18 +88158,18 @@
8833188158
break;
8833288159
case 13: /* transtype ::= */
8833388160
{yygotominor.yy194 = TK_DEFERRED;}
8833488161
break;
8833588162
case 14: /* transtype ::= DEFERRED */
88336
- case 15: /* transtype ::= IMMEDIATE */
88337
- case 16: /* transtype ::= EXCLUSIVE */
88338
- case 114: /* multiselect_op ::= UNION */
88339
- case 116: /* multiselect_op ::= EXCEPT|INTERSECT */
88163
+ case 15: /* transtype ::= IMMEDIATE */ yytestcase(yyruleno==15);
88164
+ case 16: /* transtype ::= EXCLUSIVE */ yytestcase(yyruleno==16);
88165
+ case 114: /* multiselect_op ::= UNION */ yytestcase(yyruleno==114);
88166
+ case 116: /* multiselect_op ::= EXCEPT|INTERSECT */ yytestcase(yyruleno==116);
8834088167
{yygotominor.yy194 = yymsp[0].major;}
8834188168
break;
8834288169
case 17: /* cmd ::= COMMIT trans_opt */
88343
- case 18: /* cmd ::= END trans_opt */
88170
+ case 18: /* cmd ::= END trans_opt */ yytestcase(yyruleno==18);
8834488171
{sqlite3CommitTransaction(pParse);}
8834588172
break;
8834688173
case 19: /* cmd ::= ROLLBACK trans_opt */
8834788174
{sqlite3RollbackTransaction(pParse);}
8834888175
break;
@@ -88371,30 +88198,30 @@
8837188198
pParse->db->lookaside.bEnabled = 0;
8837288199
yygotominor.yy0 = yymsp[0].minor.yy0;
8837388200
}
8837488201
break;
8837588202
case 28: /* ifnotexists ::= */
88376
- case 31: /* temp ::= */
88377
- case 70: /* autoinc ::= */
88378
- case 84: /* init_deferred_pred_opt ::= */
88379
- case 86: /* init_deferred_pred_opt ::= INITIALLY IMMEDIATE */
88380
- case 97: /* defer_subclause_opt ::= */
88381
- case 108: /* ifexists ::= */
88382
- case 119: /* distinct ::= ALL */
88383
- case 120: /* distinct ::= */
88384
- case 222: /* between_op ::= BETWEEN */
88385
- case 225: /* in_op ::= IN */
88203
+ case 31: /* temp ::= */ yytestcase(yyruleno==31);
88204
+ case 70: /* autoinc ::= */ yytestcase(yyruleno==70);
88205
+ case 84: /* init_deferred_pred_opt ::= */ yytestcase(yyruleno==84);
88206
+ case 86: /* init_deferred_pred_opt ::= INITIALLY IMMEDIATE */ yytestcase(yyruleno==86);
88207
+ case 97: /* defer_subclause_opt ::= */ yytestcase(yyruleno==97);
88208
+ case 108: /* ifexists ::= */ yytestcase(yyruleno==108);
88209
+ case 119: /* distinct ::= ALL */ yytestcase(yyruleno==119);
88210
+ case 120: /* distinct ::= */ yytestcase(yyruleno==120);
88211
+ case 222: /* between_op ::= BETWEEN */ yytestcase(yyruleno==222);
88212
+ case 225: /* in_op ::= IN */ yytestcase(yyruleno==225);
8838688213
{yygotominor.yy194 = 0;}
8838788214
break;
8838888215
case 29: /* ifnotexists ::= IF NOT EXISTS */
88389
- case 30: /* temp ::= TEMP */
88390
- case 71: /* autoinc ::= AUTOINCR */
88391
- case 85: /* init_deferred_pred_opt ::= INITIALLY DEFERRED */
88392
- case 107: /* ifexists ::= IF EXISTS */
88393
- case 118: /* distinct ::= DISTINCT */
88394
- case 223: /* between_op ::= NOT BETWEEN */
88395
- case 226: /* in_op ::= NOT IN */
88216
+ case 30: /* temp ::= TEMP */ yytestcase(yyruleno==30);
88217
+ case 71: /* autoinc ::= AUTOINCR */ yytestcase(yyruleno==71);
88218
+ case 85: /* init_deferred_pred_opt ::= INITIALLY DEFERRED */ yytestcase(yyruleno==85);
88219
+ case 107: /* ifexists ::= IF EXISTS */ yytestcase(yyruleno==107);
88220
+ case 118: /* distinct ::= DISTINCT */ yytestcase(yyruleno==118);
88221
+ case 223: /* between_op ::= NOT BETWEEN */ yytestcase(yyruleno==223);
88222
+ case 226: /* in_op ::= NOT IN */ yytestcase(yyruleno==226);
8839688223
{yygotominor.yy194 = 1;}
8839788224
break;
8839888225
case 32: /* create_table_args ::= LP columnlist conslist_opt RP */
8839988226
{
8840088227
sqlite3EndTable(pParse,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0,0);
@@ -88417,30 +88244,30 @@
8841788244
sqlite3AddColumn(pParse,&yymsp[0].minor.yy0);
8841888245
yygotominor.yy0 = yymsp[0].minor.yy0;
8841988246
}
8842088247
break;
8842188248
case 38: /* id ::= ID */
88422
- case 39: /* id ::= INDEXED */
88423
- case 40: /* ids ::= ID|STRING */
88424
- case 41: /* nm ::= id */
88425
- case 42: /* nm ::= STRING */
88426
- case 43: /* nm ::= JOIN_KW */
88427
- case 46: /* typetoken ::= typename */
88428
- case 49: /* typename ::= ids */
88429
- case 126: /* as ::= AS nm */
88430
- case 127: /* as ::= ids */
88431
- case 137: /* dbnm ::= DOT nm */
88432
- case 146: /* indexed_opt ::= INDEXED BY nm */
88433
- case 251: /* collate ::= COLLATE ids */
88434
- case 260: /* nmnum ::= plus_num */
88435
- case 261: /* nmnum ::= nm */
88436
- case 262: /* nmnum ::= ON */
88437
- case 263: /* nmnum ::= DELETE */
88438
- case 264: /* nmnum ::= DEFAULT */
88439
- case 265: /* plus_num ::= plus_opt number */
88440
- case 266: /* minus_num ::= MINUS number */
88441
- case 267: /* number ::= INTEGER|FLOAT */
88249
+ case 39: /* id ::= INDEXED */ yytestcase(yyruleno==39);
88250
+ case 40: /* ids ::= ID|STRING */ yytestcase(yyruleno==40);
88251
+ case 41: /* nm ::= id */ yytestcase(yyruleno==41);
88252
+ case 42: /* nm ::= STRING */ yytestcase(yyruleno==42);
88253
+ case 43: /* nm ::= JOIN_KW */ yytestcase(yyruleno==43);
88254
+ case 46: /* typetoken ::= typename */ yytestcase(yyruleno==46);
88255
+ case 49: /* typename ::= ids */ yytestcase(yyruleno==49);
88256
+ case 126: /* as ::= AS nm */ yytestcase(yyruleno==126);
88257
+ case 127: /* as ::= ids */ yytestcase(yyruleno==127);
88258
+ case 137: /* dbnm ::= DOT nm */ yytestcase(yyruleno==137);
88259
+ case 146: /* indexed_opt ::= INDEXED BY nm */ yytestcase(yyruleno==146);
88260
+ case 251: /* collate ::= COLLATE ids */ yytestcase(yyruleno==251);
88261
+ case 260: /* nmnum ::= plus_num */ yytestcase(yyruleno==260);
88262
+ case 261: /* nmnum ::= nm */ yytestcase(yyruleno==261);
88263
+ case 262: /* nmnum ::= ON */ yytestcase(yyruleno==262);
88264
+ case 263: /* nmnum ::= DELETE */ yytestcase(yyruleno==263);
88265
+ case 264: /* nmnum ::= DEFAULT */ yytestcase(yyruleno==264);
88266
+ case 265: /* plus_num ::= plus_opt number */ yytestcase(yyruleno==265);
88267
+ case 266: /* minus_num ::= MINUS number */ yytestcase(yyruleno==266);
88268
+ case 267: /* number ::= INTEGER|FLOAT */ yytestcase(yyruleno==267);
8844288269
{yygotominor.yy0 = yymsp[0].minor.yy0;}
8844388270
break;
8844488271
case 45: /* type ::= typetoken */
8844588272
{sqlite3AddColumnType(pParse,&yymsp[0].minor.yy0);}
8844688273
break;
@@ -88458,11 +88285,11 @@
8845888285
break;
8845988286
case 50: /* typename ::= typename ids */
8846088287
{yygotominor.yy0.z=yymsp[-1].minor.yy0.z; yygotominor.yy0.n=yymsp[0].minor.yy0.n+(int)(yymsp[0].minor.yy0.z-yymsp[-1].minor.yy0.z);}
8846188288
break;
8846288289
case 57: /* ccons ::= DEFAULT term */
88463
- case 59: /* ccons ::= DEFAULT PLUS term */
88290
+ case 59: /* ccons ::= DEFAULT PLUS term */ yytestcase(yyruleno==59);
8846488291
{sqlite3AddDefaultValue(pParse,&yymsp[0].minor.yy190);}
8846588292
break;
8846688293
case 58: /* ccons ::= DEFAULT LP expr RP */
8846788294
{sqlite3AddDefaultValue(pParse,&yymsp[-1].minor.yy190);}
8846888295
break;
@@ -88532,16 +88359,16 @@
8853288359
break;
8853388360
case 81: /* refact ::= RESTRICT */
8853488361
{ yygotominor.yy194 = OE_Restrict; }
8853588362
break;
8853688363
case 82: /* defer_subclause ::= NOT DEFERRABLE init_deferred_pred_opt */
88537
- case 83: /* defer_subclause ::= DEFERRABLE init_deferred_pred_opt */
88538
- case 98: /* defer_subclause_opt ::= defer_subclause */
88539
- case 100: /* onconf ::= ON CONFLICT resolvetype */
88540
- case 102: /* orconf ::= OR resolvetype */
88541
- case 103: /* resolvetype ::= raisetype */
88542
- case 175: /* insert_cmd ::= INSERT orconf */
88364
+ case 83: /* defer_subclause ::= DEFERRABLE init_deferred_pred_opt */ yytestcase(yyruleno==83);
88365
+ case 98: /* defer_subclause_opt ::= defer_subclause */ yytestcase(yyruleno==98);
88366
+ case 100: /* onconf ::= ON CONFLICT resolvetype */ yytestcase(yyruleno==100);
88367
+ case 102: /* orconf ::= OR resolvetype */ yytestcase(yyruleno==102);
88368
+ case 103: /* resolvetype ::= raisetype */ yytestcase(yyruleno==103);
88369
+ case 175: /* insert_cmd ::= INSERT orconf */ yytestcase(yyruleno==175);
8854388370
{yygotominor.yy194 = yymsp[0].minor.yy194;}
8854488371
break;
8854588372
case 87: /* conslist_opt ::= */
8854688373
{yygotominor.yy0.n = 0; yygotominor.yy0.z = 0;}
8854788374
break;
@@ -88562,18 +88389,18 @@
8856288389
sqlite3CreateForeignKey(pParse, yymsp[-6].minor.yy148, &yymsp[-3].minor.yy0, yymsp[-2].minor.yy148, yymsp[-1].minor.yy194);
8856388390
sqlite3DeferForeignKey(pParse, yymsp[0].minor.yy194);
8856488391
}
8856588392
break;
8856688393
case 99: /* onconf ::= */
88567
- case 101: /* orconf ::= */
88394
+ case 101: /* orconf ::= */ yytestcase(yyruleno==101);
8856888395
{yygotominor.yy194 = OE_Default;}
8856988396
break;
8857088397
case 104: /* resolvetype ::= IGNORE */
8857188398
{yygotominor.yy194 = OE_Ignore;}
8857288399
break;
8857388400
case 105: /* resolvetype ::= REPLACE */
88574
- case 176: /* insert_cmd ::= REPLACE */
88401
+ case 176: /* insert_cmd ::= REPLACE */ yytestcase(yyruleno==176);
8857588402
{yygotominor.yy194 = OE_Replace;}
8857688403
break;
8857788404
case 106: /* cmd ::= DROP TABLE ifexists fullname */
8857888405
{
8857988406
sqlite3DropTable(pParse, yymsp[0].minor.yy185, 0, yymsp[-1].minor.yy194);
@@ -88617,18 +88444,18 @@
8861788444
{
8861888445
yygotominor.yy243 = sqlite3SelectNew(pParse,yymsp[-6].minor.yy148,yymsp[-5].minor.yy185,yymsp[-4].minor.yy72,yymsp[-3].minor.yy148,yymsp[-2].minor.yy72,yymsp[-1].minor.yy148,yymsp[-7].minor.yy194,yymsp[0].minor.yy354.pLimit,yymsp[0].minor.yy354.pOffset);
8861988446
}
8862088447
break;
8862188448
case 121: /* sclp ::= selcollist COMMA */
88622
- case 247: /* idxlist_opt ::= LP idxlist RP */
88449
+ case 247: /* idxlist_opt ::= LP idxlist RP */ yytestcase(yyruleno==247);
8862388450
{yygotominor.yy148 = yymsp[-1].minor.yy148;}
8862488451
break;
8862588452
case 122: /* sclp ::= */
88626
- case 150: /* orderby_opt ::= */
88627
- case 158: /* groupby_opt ::= */
88628
- case 240: /* exprlist ::= */
88629
- case 246: /* idxlist_opt ::= */
88453
+ case 150: /* orderby_opt ::= */ yytestcase(yyruleno==150);
88454
+ case 158: /* groupby_opt ::= */ yytestcase(yyruleno==158);
88455
+ case 240: /* exprlist ::= */ yytestcase(yyruleno==240);
88456
+ case 246: /* idxlist_opt ::= */ yytestcase(yyruleno==246);
8863088457
{yygotominor.yy148 = 0;}
8863188458
break;
8863288459
case 123: /* selcollist ::= sclp expr as */
8863388460
{
8863488461
yygotominor.yy148 = sqlite3ExprListAppend(pParse, yymsp[-2].minor.yy148, yymsp[-1].minor.yy190.pExpr);
@@ -88663,11 +88490,11 @@
8866388490
}
8866488491
break;
8866588492
case 131: /* stl_prefix ::= seltablist joinop */
8866688493
{
8866788494
yygotominor.yy185 = yymsp[-1].minor.yy185;
88668
- if( yygotominor.yy185 && yygotominor.yy185->nSrc>0 ) yygotominor.yy185->a[yygotominor.yy185->nSrc-1].jointype = (u8)yymsp[0].minor.yy194;
88495
+ if( ALWAYS(yygotominor.yy185 && yygotominor.yy185->nSrc>0) ) yygotominor.yy185->a[yygotominor.yy185->nSrc-1].jointype = (u8)yymsp[0].minor.yy194;
8866988496
}
8867088497
break;
8867188498
case 132: /* stl_prefix ::= */
8867288499
{yygotominor.yy185 = 0;}
8867388500
break;
@@ -88682,11 +88509,13 @@
8868288509
yygotominor.yy185 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-6].minor.yy185,0,0,&yymsp[-2].minor.yy0,yymsp[-4].minor.yy243,yymsp[-1].minor.yy72,yymsp[0].minor.yy254);
8868388510
}
8868488511
break;
8868588512
case 135: /* seltablist ::= stl_prefix LP seltablist RP as on_opt using_opt */
8868688513
{
88687
- if( yymsp[-6].minor.yy185==0 && yymsp[-2].minor.yy0.n==0 && yymsp[-1].minor.yy72==0 && yymsp[0].minor.yy254==0 ){
88514
+ if( yymsp[-6].minor.yy185==0 ){
88515
+ sqlite3ExprDelete(pParse->db, yymsp[-1].minor.yy72);
88516
+ sqlite3IdListDelete(pParse->db, yymsp[0].minor.yy254);
8868888517
yygotominor.yy185 = yymsp[-4].minor.yy185;
8868988518
}else{
8869088519
Select *pSubquery;
8869188520
sqlite3SrcListShiftJoinType(yymsp[-4].minor.yy185);
8869288521
pSubquery = sqlite3SelectNew(pParse,0,yymsp[-4].minor.yy185,0,0,0,0,0,0,0);
@@ -88693,11 +88522,11 @@
8869388522
yygotominor.yy185 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-6].minor.yy185,0,0,&yymsp[-2].minor.yy0,pSubquery,yymsp[-1].minor.yy72,yymsp[0].minor.yy254);
8869488523
}
8869588524
}
8869688525
break;
8869788526
case 136: /* dbnm ::= */
88698
- case 145: /* indexed_opt ::= */
88527
+ case 145: /* indexed_opt ::= */ yytestcase(yyruleno==145);
8869988528
{yygotominor.yy0.z=0; yygotominor.yy0.n=0;}
8870088529
break;
8870188530
case 138: /* fullname ::= nm dbnm */
8870288531
{yygotominor.yy185 = sqlite3SrcListAppend(pParse->db,0,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0);}
8870388532
break;
@@ -88712,38 +88541,38 @@
8871288541
break;
8871388542
case 142: /* joinop ::= JOIN_KW nm nm JOIN */
8871488543
{ yygotominor.yy194 = sqlite3JoinType(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0); }
8871588544
break;
8871688545
case 143: /* on_opt ::= ON expr */
88717
- case 154: /* sortitem ::= expr */
88718
- case 161: /* having_opt ::= HAVING expr */
88719
- case 168: /* where_opt ::= WHERE expr */
88720
- case 235: /* case_else ::= ELSE expr */
88721
- case 237: /* case_operand ::= expr */
88546
+ case 154: /* sortitem ::= expr */ yytestcase(yyruleno==154);
88547
+ case 161: /* having_opt ::= HAVING expr */ yytestcase(yyruleno==161);
88548
+ case 168: /* where_opt ::= WHERE expr */ yytestcase(yyruleno==168);
88549
+ case 235: /* case_else ::= ELSE expr */ yytestcase(yyruleno==235);
88550
+ case 237: /* case_operand ::= expr */ yytestcase(yyruleno==237);
8872288551
{yygotominor.yy72 = yymsp[0].minor.yy190.pExpr;}
8872388552
break;
8872488553
case 144: /* on_opt ::= */
88725
- case 160: /* having_opt ::= */
88726
- case 167: /* where_opt ::= */
88727
- case 236: /* case_else ::= */
88728
- case 238: /* case_operand ::= */
88554
+ case 160: /* having_opt ::= */ yytestcase(yyruleno==160);
88555
+ case 167: /* where_opt ::= */ yytestcase(yyruleno==167);
88556
+ case 236: /* case_else ::= */ yytestcase(yyruleno==236);
88557
+ case 238: /* case_operand ::= */ yytestcase(yyruleno==238);
8872988558
{yygotominor.yy72 = 0;}
8873088559
break;
8873188560
case 147: /* indexed_opt ::= NOT INDEXED */
8873288561
{yygotominor.yy0.z=0; yygotominor.yy0.n=1;}
8873388562
break;
8873488563
case 148: /* using_opt ::= USING LP inscollist RP */
88735
- case 180: /* inscollist_opt ::= LP inscollist RP */
88564
+ case 180: /* inscollist_opt ::= LP inscollist RP */ yytestcase(yyruleno==180);
8873688565
{yygotominor.yy254 = yymsp[-1].minor.yy254;}
8873788566
break;
8873888567
case 149: /* using_opt ::= */
88739
- case 179: /* inscollist_opt ::= */
88568
+ case 179: /* inscollist_opt ::= */ yytestcase(yyruleno==179);
8874088569
{yygotominor.yy254 = 0;}
8874188570
break;
8874288571
case 151: /* orderby_opt ::= ORDER BY sortlist */
88743
- case 159: /* groupby_opt ::= GROUP BY nexprlist */
88744
- case 239: /* exprlist ::= nexprlist */
88572
+ case 159: /* groupby_opt ::= GROUP BY nexprlist */ yytestcase(yyruleno==159);
88573
+ case 239: /* exprlist ::= nexprlist */ yytestcase(yyruleno==239);
8874588574
{yygotominor.yy148 = yymsp[0].minor.yy148;}
8874688575
break;
8874788576
case 152: /* sortlist ::= sortlist COMMA sortitem sortorder */
8874888577
{
8874988578
yygotominor.yy148 = sqlite3ExprListAppend(pParse,yymsp[-3].minor.yy148,yymsp[-1].minor.yy72);
@@ -88751,15 +88580,15 @@
8875188580
}
8875288581
break;
8875388582
case 153: /* sortlist ::= sortitem sortorder */
8875488583
{
8875588584
yygotominor.yy148 = sqlite3ExprListAppend(pParse,0,yymsp[-1].minor.yy72);
88756
- if( yygotominor.yy148 && yygotominor.yy148->a ) yygotominor.yy148->a[0].sortOrder = (u8)yymsp[0].minor.yy194;
88585
+ if( yygotominor.yy148 && ALWAYS(yygotominor.yy148->a) ) yygotominor.yy148->a[0].sortOrder = (u8)yymsp[0].minor.yy194;
8875788586
}
8875888587
break;
8875988588
case 155: /* sortorder ::= ASC */
88760
- case 157: /* sortorder ::= */
88589
+ case 157: /* sortorder ::= */ yytestcase(yyruleno==157);
8876188590
{yygotominor.yy194 = SQLITE_SO_ASC;}
8876288591
break;
8876388592
case 156: /* sortorder ::= DESC */
8876488593
{yygotominor.yy194 = SQLITE_SO_DESC;}
8876588594
break;
@@ -88808,37 +88637,37 @@
8880888637
break;
8880988638
case 174: /* cmd ::= insert_cmd INTO fullname inscollist_opt DEFAULT VALUES */
8881088639
{sqlite3Insert(pParse, yymsp[-3].minor.yy185, 0, 0, yymsp[-2].minor.yy254, yymsp[-5].minor.yy194);}
8881188640
break;
8881288641
case 177: /* itemlist ::= itemlist COMMA expr */
88813
- case 241: /* nexprlist ::= nexprlist COMMA expr */
88642
+ case 241: /* nexprlist ::= nexprlist COMMA expr */ yytestcase(yyruleno==241);
8881488643
{yygotominor.yy148 = sqlite3ExprListAppend(pParse,yymsp[-2].minor.yy148,yymsp[0].minor.yy190.pExpr);}
8881588644
break;
8881688645
case 178: /* itemlist ::= expr */
88817
- case 242: /* nexprlist ::= expr */
88646
+ case 242: /* nexprlist ::= expr */ yytestcase(yyruleno==242);
8881888647
{yygotominor.yy148 = sqlite3ExprListAppend(pParse,0,yymsp[0].minor.yy190.pExpr);}
8881988648
break;
8882088649
case 181: /* inscollist ::= inscollist COMMA nm */
8882188650
{yygotominor.yy254 = sqlite3IdListAppend(pParse->db,yymsp[-2].minor.yy254,&yymsp[0].minor.yy0);}
8882288651
break;
8882388652
case 182: /* inscollist ::= nm */
8882488653
{yygotominor.yy254 = sqlite3IdListAppend(pParse->db,0,&yymsp[0].minor.yy0);}
8882588654
break;
8882688655
case 183: /* expr ::= term */
88827
- case 211: /* escape ::= ESCAPE expr */
88656
+ case 211: /* escape ::= ESCAPE expr */ yytestcase(yyruleno==211);
8882888657
{yygotominor.yy190 = yymsp[0].minor.yy190;}
8882988658
break;
8883088659
case 184: /* expr ::= LP expr RP */
8883188660
{yygotominor.yy190.pExpr = yymsp[-1].minor.yy190.pExpr; spanSet(&yygotominor.yy190,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0);}
8883288661
break;
8883388662
case 185: /* term ::= NULL */
88834
- case 190: /* term ::= INTEGER|FLOAT|BLOB */
88835
- case 191: /* term ::= STRING */
88663
+ case 190: /* term ::= INTEGER|FLOAT|BLOB */ yytestcase(yyruleno==190);
88664
+ case 191: /* term ::= STRING */ yytestcase(yyruleno==191);
8883688665
{spanExpr(&yygotominor.yy190, pParse, yymsp[0].major, &yymsp[0].minor.yy0);}
8883788666
break;
8883888667
case 186: /* expr ::= id */
88839
- case 187: /* expr ::= JOIN_KW */
88668
+ case 187: /* expr ::= JOIN_KW */ yytestcase(yyruleno==187);
8884088669
{spanExpr(&yygotominor.yy190, pParse, TK_ID, &yymsp[0].minor.yy0);}
8884188670
break;
8884288671
case 188: /* expr ::= nm DOT nm */
8884388672
{
8884488673
Expr *temp1 = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[-2].minor.yy0);
@@ -88892,11 +88721,11 @@
8889288721
spanSet(&yygotominor.yy190,&yymsp[-5].minor.yy0,&yymsp[0].minor.yy0);
8889388722
}
8889488723
break;
8889588724
case 196: /* expr ::= ID LP distinct exprlist RP */
8889688725
{
88897
- if( yymsp[-1].minor.yy148 && yymsp[-1].minor.yy148->nExpr>SQLITE_MAX_FUNCTION_ARG ){
88726
+ if( yymsp[-1].minor.yy148 && yymsp[-1].minor.yy148->nExpr>pParse->db->aLimit[SQLITE_LIMIT_FUNCTION_ARG] ){
8889888727
sqlite3ErrorMsg(pParse, "too many arguments on function %T", &yymsp[-4].minor.yy0);
8889988728
}
8890088729
yygotominor.yy190.pExpr = sqlite3ExprFunction(pParse, yymsp[-1].minor.yy148, &yymsp[-4].minor.yy0);
8890188730
spanSet(&yygotominor.yy190,&yymsp[-4].minor.yy0,&yymsp[0].minor.yy0);
8890288731
if( yymsp[-2].minor.yy194 && yygotominor.yy190.pExpr ){
@@ -88920,25 +88749,25 @@
8892088749
}
8892188750
spanSet(&yygotominor.yy190, &yymsp[0].minor.yy0, &yymsp[0].minor.yy0);
8892288751
}
8892388752
break;
8892488753
case 199: /* expr ::= expr AND expr */
88925
- case 200: /* expr ::= expr OR expr */
88926
- case 201: /* expr ::= expr LT|GT|GE|LE expr */
88927
- case 202: /* expr ::= expr EQ|NE expr */
88928
- case 203: /* expr ::= expr BITAND|BITOR|LSHIFT|RSHIFT expr */
88929
- case 204: /* expr ::= expr PLUS|MINUS expr */
88930
- case 205: /* expr ::= expr STAR|SLASH|REM expr */
88931
- case 206: /* expr ::= expr CONCAT expr */
88754
+ case 200: /* expr ::= expr OR expr */ yytestcase(yyruleno==200);
88755
+ case 201: /* expr ::= expr LT|GT|GE|LE expr */ yytestcase(yyruleno==201);
88756
+ case 202: /* expr ::= expr EQ|NE expr */ yytestcase(yyruleno==202);
88757
+ case 203: /* expr ::= expr BITAND|BITOR|LSHIFT|RSHIFT expr */ yytestcase(yyruleno==203);
88758
+ case 204: /* expr ::= expr PLUS|MINUS expr */ yytestcase(yyruleno==204);
88759
+ case 205: /* expr ::= expr STAR|SLASH|REM expr */ yytestcase(yyruleno==205);
88760
+ case 206: /* expr ::= expr CONCAT expr */ yytestcase(yyruleno==206);
8893288761
{spanBinaryExpr(&yygotominor.yy190,pParse,yymsp[-1].major,&yymsp[-2].minor.yy190,&yymsp[0].minor.yy190);}
8893388762
break;
8893488763
case 207: /* likeop ::= LIKE_KW */
88935
- case 209: /* likeop ::= MATCH */
88764
+ case 209: /* likeop ::= MATCH */ yytestcase(yyruleno==209);
8893688765
{yygotominor.yy392.eOperator = yymsp[0].minor.yy0; yygotominor.yy392.not = 0;}
8893788766
break;
8893888767
case 208: /* likeop ::= NOT LIKE_KW */
88939
- case 210: /* likeop ::= NOT MATCH */
88768
+ case 210: /* likeop ::= NOT MATCH */ yytestcase(yyruleno==210);
8894088769
{yygotominor.yy392.eOperator = yymsp[0].minor.yy0; yygotominor.yy392.not = 1;}
8894188770
break;
8894288771
case 212: /* escape ::= */
8894388772
{memset(&yygotominor.yy190,0,sizeof(yygotominor.yy190));}
8894488773
break;
@@ -88968,11 +88797,11 @@
8896888797
break;
8896988798
case 217: /* expr ::= expr IS NOT NULL */
8897088799
{spanUnaryPostfix(&yygotominor.yy190,pParse,TK_NOTNULL,&yymsp[-3].minor.yy190,&yymsp[0].minor.yy0);}
8897188800
break;
8897288801
case 218: /* expr ::= NOT expr */
88973
- case 219: /* expr ::= BITNOT expr */
88802
+ case 219: /* expr ::= BITNOT expr */ yytestcase(yyruleno==219);
8897488803
{spanUnaryPrefix(&yygotominor.yy190,pParse,yymsp[-1].major,&yymsp[0].minor.yy190,&yymsp[-1].minor.yy0);}
8897588804
break;
8897688805
case 220: /* expr ::= MINUS expr */
8897788806
{spanUnaryPrefix(&yygotominor.yy190,pParse,TK_UMINUS,&yymsp[0].minor.yy190,&yymsp[-1].minor.yy0);}
8897888807
break;
@@ -89098,11 +88927,11 @@
8909888927
sqlite3SrcListAppend(pParse->db,0,&yymsp[-3].minor.yy0,0), yymsp[-1].minor.yy148, yymsp[-9].minor.yy194,
8909988928
&yymsp[-10].minor.yy0, &yymsp[0].minor.yy0, SQLITE_SO_ASC, yymsp[-7].minor.yy194);
8910088929
}
8910188930
break;
8910288931
case 244: /* uniqueflag ::= UNIQUE */
89103
- case 293: /* raisetype ::= ABORT */
88932
+ case 293: /* raisetype ::= ABORT */ yytestcase(yyruleno==293);
8910488933
{yygotominor.yy194 = OE_Abort;}
8910588934
break;
8910688935
case 245: /* uniqueflag ::= */
8910788936
{yygotominor.yy194 = OE_None;}
8910888937
break;
@@ -89137,11 +88966,11 @@
8913788966
break;
8913888967
case 252: /* cmd ::= DROP INDEX ifexists fullname */
8913988968
{sqlite3DropIndex(pParse, yymsp[0].minor.yy185, yymsp[-1].minor.yy194);}
8914088969
break;
8914188970
case 253: /* cmd ::= VACUUM */
89142
- case 254: /* cmd ::= VACUUM nm */
88971
+ case 254: /* cmd ::= VACUUM nm */ yytestcase(yyruleno==254);
8914388972
{sqlite3Vacuum(pParse);}
8914488973
break;
8914588974
case 255: /* cmd ::= PRAGMA nm dbnm */
8914688975
{sqlite3Pragma(pParse,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0,0,0);}
8914788976
break;
@@ -89170,32 +88999,32 @@
8917088999
sqlite3BeginTrigger(pParse, &yymsp[-7].minor.yy0, &yymsp[-6].minor.yy0, yymsp[-5].minor.yy194, yymsp[-4].minor.yy332.a, yymsp[-4].minor.yy332.b, yymsp[-2].minor.yy185, yymsp[0].minor.yy72, yymsp[-10].minor.yy194, yymsp[-8].minor.yy194);
8917189000
yygotominor.yy0 = (yymsp[-6].minor.yy0.n==0?yymsp[-7].minor.yy0:yymsp[-6].minor.yy0);
8917289001
}
8917389002
break;
8917489003
case 272: /* trigger_time ::= BEFORE */
89175
- case 275: /* trigger_time ::= */
89004
+ case 275: /* trigger_time ::= */ yytestcase(yyruleno==275);
8917689005
{ yygotominor.yy194 = TK_BEFORE; }
8917789006
break;
8917889007
case 273: /* trigger_time ::= AFTER */
8917989008
{ yygotominor.yy194 = TK_AFTER; }
8918089009
break;
8918189010
case 274: /* trigger_time ::= INSTEAD OF */
8918289011
{ yygotominor.yy194 = TK_INSTEAD;}
8918389012
break;
8918489013
case 276: /* trigger_event ::= DELETE|INSERT */
89185
- case 277: /* trigger_event ::= UPDATE */
89014
+ case 277: /* trigger_event ::= UPDATE */ yytestcase(yyruleno==277);
8918689015
{yygotominor.yy332.a = yymsp[0].major; yygotominor.yy332.b = 0;}
8918789016
break;
8918889017
case 278: /* trigger_event ::= UPDATE OF inscollist */
8918989018
{yygotominor.yy332.a = TK_UPDATE; yygotominor.yy332.b = yymsp[0].minor.yy254;}
8919089019
break;
8919189020
case 281: /* when_clause ::= */
89192
- case 298: /* key_opt ::= */
89021
+ case 298: /* key_opt ::= */ yytestcase(yyruleno==298);
8919389022
{ yygotominor.yy72 = 0; }
8919489023
break;
8919589024
case 282: /* when_clause ::= WHEN expr */
89196
- case 299: /* key_opt ::= KEY expr */
89025
+ case 299: /* key_opt ::= KEY expr */ yytestcase(yyruleno==299);
8919789026
{ yygotominor.yy72 = yymsp[0].minor.yy190.pExpr; }
8919889027
break;
8919989028
case 283: /* trigger_cmd_list ::= trigger_cmd_list trigger_cmd SEMI */
8920089029
{
8920189030
assert( yymsp[-2].minor.yy145!=0 );
@@ -89308,14 +89137,55 @@
8930889137
break;
8930989138
case 316: /* vtabarg ::= */
8931089139
{sqlite3VtabArgInit(pParse);}
8931189140
break;
8931289141
case 318: /* vtabargtoken ::= ANY */
89313
- case 319: /* vtabargtoken ::= lp anylist RP */
89314
- case 320: /* lp ::= LP */
89142
+ case 319: /* vtabargtoken ::= lp anylist RP */ yytestcase(yyruleno==319);
89143
+ case 320: /* lp ::= LP */ yytestcase(yyruleno==320);
8931589144
{sqlite3VtabArgExtend(pParse,&yymsp[0].minor.yy0);}
8931689145
break;
89146
+ default:
89147
+ /* (0) input ::= cmdlist */ yytestcase(yyruleno==0);
89148
+ /* (1) cmdlist ::= cmdlist ecmd */ yytestcase(yyruleno==1);
89149
+ /* (2) cmdlist ::= ecmd */ yytestcase(yyruleno==2);
89150
+ /* (3) ecmd ::= SEMI */ yytestcase(yyruleno==3);
89151
+ /* (4) ecmd ::= explain cmdx SEMI */ yytestcase(yyruleno==4);
89152
+ /* (10) trans_opt ::= */ yytestcase(yyruleno==10);
89153
+ /* (11) trans_opt ::= TRANSACTION */ yytestcase(yyruleno==11);
89154
+ /* (12) trans_opt ::= TRANSACTION nm */ yytestcase(yyruleno==12);
89155
+ /* (20) savepoint_opt ::= SAVEPOINT */ yytestcase(yyruleno==20);
89156
+ /* (21) savepoint_opt ::= */ yytestcase(yyruleno==21);
89157
+ /* (25) cmd ::= create_table create_table_args */ yytestcase(yyruleno==25);
89158
+ /* (34) columnlist ::= columnlist COMMA column */ yytestcase(yyruleno==34);
89159
+ /* (35) columnlist ::= column */ yytestcase(yyruleno==35);
89160
+ /* (44) type ::= */ yytestcase(yyruleno==44);
89161
+ /* (51) signed ::= plus_num */ yytestcase(yyruleno==51);
89162
+ /* (52) signed ::= minus_num */ yytestcase(yyruleno==52);
89163
+ /* (53) carglist ::= carglist carg */ yytestcase(yyruleno==53);
89164
+ /* (54) carglist ::= */ yytestcase(yyruleno==54);
89165
+ /* (55) carg ::= CONSTRAINT nm ccons */ yytestcase(yyruleno==55);
89166
+ /* (56) carg ::= ccons */ yytestcase(yyruleno==56);
89167
+ /* (62) ccons ::= NULL onconf */ yytestcase(yyruleno==62);
89168
+ /* (89) conslist ::= conslist COMMA tcons */ yytestcase(yyruleno==89);
89169
+ /* (90) conslist ::= conslist tcons */ yytestcase(yyruleno==90);
89170
+ /* (91) conslist ::= tcons */ yytestcase(yyruleno==91);
89171
+ /* (92) tcons ::= CONSTRAINT nm */ yytestcase(yyruleno==92);
89172
+ /* (268) plus_opt ::= PLUS */ yytestcase(yyruleno==268);
89173
+ /* (269) plus_opt ::= */ yytestcase(yyruleno==269);
89174
+ /* (279) foreach_clause ::= */ yytestcase(yyruleno==279);
89175
+ /* (280) foreach_clause ::= FOR EACH ROW */ yytestcase(yyruleno==280);
89176
+ /* (300) database_kw_opt ::= DATABASE */ yytestcase(yyruleno==300);
89177
+ /* (301) database_kw_opt ::= */ yytestcase(yyruleno==301);
89178
+ /* (309) kwcolumn_opt ::= */ yytestcase(yyruleno==309);
89179
+ /* (310) kwcolumn_opt ::= COLUMNKW */ yytestcase(yyruleno==310);
89180
+ /* (314) vtabarglist ::= vtabarg */ yytestcase(yyruleno==314);
89181
+ /* (315) vtabarglist ::= vtabarglist COMMA vtabarg */ yytestcase(yyruleno==315);
89182
+ /* (317) vtabarg ::= vtabarg vtabargtoken */ yytestcase(yyruleno==317);
89183
+ /* (321) anylist ::= */ yytestcase(yyruleno==321);
89184
+ /* (322) anylist ::= anylist LP anylist RP */ yytestcase(yyruleno==322);
89185
+ /* (323) anylist ::= anylist ANY */ yytestcase(yyruleno==323);
89186
+ break;
8931789187
};
8931889188
yygoto = yyRuleInfo[yyruleno].lhs;
8931989189
yysize = yyRuleInfo[yyruleno].nrhs;
8932089190
yypParser->yyidx -= yysize;
8932189191
yyact = yy_find_reduce_action(yymsp[-yysize].stateno,(YYCODETYPE)yygoto);
@@ -89343,10 +89213,11 @@
8934389213
}
8934489214
8934589215
/*
8934689216
** The following code executes when the parse fails
8934789217
*/
89218
+#ifndef YYNOERRORRECOVERY
8934889219
static void yy_parse_failed(
8934989220
yyParser *yypParser /* The parser */
8935089221
){
8935189222
sqlite3ParserARG_FETCH;
8935289223
#ifndef NDEBUG
@@ -89357,10 +89228,11 @@
8935789228
while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser);
8935889229
/* Here code is inserted which will be executed whenever the
8935989230
** parser fails */
8936089231
sqlite3ParserARG_STORE; /* Suppress warning about unused %extra_argument variable */
8936189232
}
89233
+#endif /* YYNOERRORRECOVERY */
8936289234
8936389235
/*
8936489236
** The following code executes when a syntax error first occurs.
8936589237
*/
8936689238
static void yy_syntax_error(
@@ -89527,10 +89399,22 @@
8952789399
yy_shift(yypParser,yyact,YYERRORSYMBOL,&u2);
8952889400
}
8952989401
}
8953089402
yypParser->yyerrcnt = 3;
8953189403
yyerrorhit = 1;
89404
+#elif defined(YYNOERRORRECOVERY)
89405
+ /* If the YYNOERRORRECOVERY macro is defined, then do not attempt to
89406
+ ** do any kind of error recovery. Instead, simply invoke the syntax
89407
+ ** error routine and continue going as if nothing had happened.
89408
+ **
89409
+ ** Applications can set this macro (for example inside %include) if
89410
+ ** they intend to abandon the parse upon the first syntax error seen.
89411
+ */
89412
+ yy_syntax_error(yypParser,yymajor,yyminorunion);
89413
+ yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion);
89414
+ yymajor = YYNOCODE;
89415
+
8953289416
#else /* YYERRORSYMBOL is not defined */
8953389417
/* This is what we do if the grammar does not define ERROR:
8953489418
**
8953589419
** * Report an error message, and throw away the input token.
8953689420
**
@@ -89571,11 +89455,11 @@
8957189455
**
8957289456
** This file contains C code that splits an SQL input string up into
8957389457
** individual tokens and sends those tokens one-by-one over to the
8957489458
** parser for analysis.
8957589459
**
89576
-** $Id: tokenize.c,v 1.158 2009/05/28 01:00:55 drh Exp $
89460
+** $Id: tokenize.c,v 1.161 2009/06/17 01:17:13 drh Exp $
8957789461
*/
8957889462
8957989463
/*
8958089464
** The charMap() macro maps alphabetic characters into their
8958189465
** lower-case ASCII equivalent. On ASCII machines, this is just
@@ -89624,11 +89508,11 @@
8962489508
/************** Begin file keywordhash.h *************************************/
8962589509
/***** This file contains automatically generated code ******
8962689510
**
8962789511
** The code in this file has been automatically generated by
8962889512
**
89629
-** $Header: /sqlite/sqlite/tool/mkkeywordhash.c,v 1.37 2009/02/01 00:00:46 drh Exp $
89513
+** $Header: /sqlite/sqlite/tool/mkkeywordhash.c,v 1.38 2009/06/09 14:27:41 drh Exp $
8963089514
**
8963189515
** The code in this file implements a function that determines whether
8963289516
** or not a given identifier is really an SQL keyword. The same thing
8963389517
** might be implemented more directly using a hand-written hash table.
8963489518
** But by using this automatically generated code, the size of the code
@@ -89759,129 +89643,129 @@
8975989643
h = ((charMap(z[0])*4) ^
8976089644
(charMap(z[n-1])*3) ^
8976189645
n) % 127;
8976289646
for(i=((int)aHash[h])-1; i>=0; i=((int)aNext[i])-1){
8976389647
if( aLen[i]==n && sqlite3StrNICmp(&zText[aOffset[i]],z,n)==0 ){
89764
- testcase( i==0 ); /* TK_REINDEX */
89765
- testcase( i==1 ); /* TK_INDEXED */
89766
- testcase( i==2 ); /* TK_INDEX */
89767
- testcase( i==3 ); /* TK_DESC */
89768
- testcase( i==4 ); /* TK_ESCAPE */
89769
- testcase( i==5 ); /* TK_EACH */
89770
- testcase( i==6 ); /* TK_CHECK */
89771
- testcase( i==7 ); /* TK_KEY */
89772
- testcase( i==8 ); /* TK_BEFORE */
89773
- testcase( i==9 ); /* TK_FOREIGN */
89774
- testcase( i==10 ); /* TK_FOR */
89775
- testcase( i==11 ); /* TK_IGNORE */
89776
- testcase( i==12 ); /* TK_LIKE_KW */
89777
- testcase( i==13 ); /* TK_EXPLAIN */
89778
- testcase( i==14 ); /* TK_INSTEAD */
89779
- testcase( i==15 ); /* TK_ADD */
89780
- testcase( i==16 ); /* TK_DATABASE */
89781
- testcase( i==17 ); /* TK_AS */
89782
- testcase( i==18 ); /* TK_SELECT */
89783
- testcase( i==19 ); /* TK_TABLE */
89784
- testcase( i==20 ); /* TK_JOIN_KW */
89785
- testcase( i==21 ); /* TK_THEN */
89786
- testcase( i==22 ); /* TK_END */
89787
- testcase( i==23 ); /* TK_DEFERRABLE */
89788
- testcase( i==24 ); /* TK_ELSE */
89789
- testcase( i==25 ); /* TK_EXCEPT */
89790
- testcase( i==26 ); /* TK_TRANSACTION */
89791
- testcase( i==27 ); /* TK_ON */
89792
- testcase( i==28 ); /* TK_JOIN_KW */
89793
- testcase( i==29 ); /* TK_ALTER */
89794
- testcase( i==30 ); /* TK_RAISE */
89795
- testcase( i==31 ); /* TK_EXCLUSIVE */
89796
- testcase( i==32 ); /* TK_EXISTS */
89797
- testcase( i==33 ); /* TK_SAVEPOINT */
89798
- testcase( i==34 ); /* TK_INTERSECT */
89799
- testcase( i==35 ); /* TK_TRIGGER */
89800
- testcase( i==36 ); /* TK_REFERENCES */
89801
- testcase( i==37 ); /* TK_CONSTRAINT */
89802
- testcase( i==38 ); /* TK_INTO */
89803
- testcase( i==39 ); /* TK_OFFSET */
89804
- testcase( i==40 ); /* TK_OF */
89805
- testcase( i==41 ); /* TK_SET */
89806
- testcase( i==42 ); /* TK_TEMP */
89807
- testcase( i==43 ); /* TK_TEMP */
89808
- testcase( i==44 ); /* TK_OR */
89809
- testcase( i==45 ); /* TK_UNIQUE */
89810
- testcase( i==46 ); /* TK_QUERY */
89811
- testcase( i==47 ); /* TK_ATTACH */
89812
- testcase( i==48 ); /* TK_HAVING */
89813
- testcase( i==49 ); /* TK_GROUP */
89814
- testcase( i==50 ); /* TK_UPDATE */
89815
- testcase( i==51 ); /* TK_BEGIN */
89816
- testcase( i==52 ); /* TK_JOIN_KW */
89817
- testcase( i==53 ); /* TK_RELEASE */
89818
- testcase( i==54 ); /* TK_BETWEEN */
89819
- testcase( i==55 ); /* TK_NOTNULL */
89820
- testcase( i==56 ); /* TK_NOT */
89821
- testcase( i==57 ); /* TK_NULL */
89822
- testcase( i==58 ); /* TK_LIKE_KW */
89823
- testcase( i==59 ); /* TK_CASCADE */
89824
- testcase( i==60 ); /* TK_ASC */
89825
- testcase( i==61 ); /* TK_DELETE */
89826
- testcase( i==62 ); /* TK_CASE */
89827
- testcase( i==63 ); /* TK_COLLATE */
89828
- testcase( i==64 ); /* TK_CREATE */
89829
- testcase( i==65 ); /* TK_CTIME_KW */
89830
- testcase( i==66 ); /* TK_DETACH */
89831
- testcase( i==67 ); /* TK_IMMEDIATE */
89832
- testcase( i==68 ); /* TK_JOIN */
89833
- testcase( i==69 ); /* TK_INSERT */
89834
- testcase( i==70 ); /* TK_MATCH */
89835
- testcase( i==71 ); /* TK_PLAN */
89836
- testcase( i==72 ); /* TK_ANALYZE */
89837
- testcase( i==73 ); /* TK_PRAGMA */
89838
- testcase( i==74 ); /* TK_ABORT */
89839
- testcase( i==75 ); /* TK_VALUES */
89840
- testcase( i==76 ); /* TK_VIRTUAL */
89841
- testcase( i==77 ); /* TK_LIMIT */
89842
- testcase( i==78 ); /* TK_WHEN */
89843
- testcase( i==79 ); /* TK_WHERE */
89844
- testcase( i==80 ); /* TK_RENAME */
89845
- testcase( i==81 ); /* TK_AFTER */
89846
- testcase( i==82 ); /* TK_REPLACE */
89847
- testcase( i==83 ); /* TK_AND */
89848
- testcase( i==84 ); /* TK_DEFAULT */
89849
- testcase( i==85 ); /* TK_AUTOINCR */
89850
- testcase( i==86 ); /* TK_TO */
89851
- testcase( i==87 ); /* TK_IN */
89852
- testcase( i==88 ); /* TK_CAST */
89853
- testcase( i==89 ); /* TK_COLUMNKW */
89854
- testcase( i==90 ); /* TK_COMMIT */
89855
- testcase( i==91 ); /* TK_CONFLICT */
89856
- testcase( i==92 ); /* TK_JOIN_KW */
89857
- testcase( i==93 ); /* TK_CTIME_KW */
89858
- testcase( i==94 ); /* TK_CTIME_KW */
89859
- testcase( i==95 ); /* TK_PRIMARY */
89860
- testcase( i==96 ); /* TK_DEFERRED */
89861
- testcase( i==97 ); /* TK_DISTINCT */
89862
- testcase( i==98 ); /* TK_IS */
89863
- testcase( i==99 ); /* TK_DROP */
89864
- testcase( i==100 ); /* TK_FAIL */
89865
- testcase( i==101 ); /* TK_FROM */
89866
- testcase( i==102 ); /* TK_JOIN_KW */
89867
- testcase( i==103 ); /* TK_LIKE_KW */
89868
- testcase( i==104 ); /* TK_BY */
89869
- testcase( i==105 ); /* TK_IF */
89870
- testcase( i==106 ); /* TK_ISNULL */
89871
- testcase( i==107 ); /* TK_ORDER */
89872
- testcase( i==108 ); /* TK_RESTRICT */
89873
- testcase( i==109 ); /* TK_JOIN_KW */
89874
- testcase( i==110 ); /* TK_JOIN_KW */
89875
- testcase( i==111 ); /* TK_ROLLBACK */
89876
- testcase( i==112 ); /* TK_ROW */
89877
- testcase( i==113 ); /* TK_UNION */
89878
- testcase( i==114 ); /* TK_USING */
89879
- testcase( i==115 ); /* TK_VACUUM */
89880
- testcase( i==116 ); /* TK_VIEW */
89881
- testcase( i==117 ); /* TK_INITIALLY */
89882
- testcase( i==118 ); /* TK_ALL */
89648
+ testcase( i==0 ); /* REINDEX */
89649
+ testcase( i==1 ); /* INDEXED */
89650
+ testcase( i==2 ); /* INDEX */
89651
+ testcase( i==3 ); /* DESC */
89652
+ testcase( i==4 ); /* ESCAPE */
89653
+ testcase( i==5 ); /* EACH */
89654
+ testcase( i==6 ); /* CHECK */
89655
+ testcase( i==7 ); /* KEY */
89656
+ testcase( i==8 ); /* BEFORE */
89657
+ testcase( i==9 ); /* FOREIGN */
89658
+ testcase( i==10 ); /* FOR */
89659
+ testcase( i==11 ); /* IGNORE */
89660
+ testcase( i==12 ); /* REGEXP */
89661
+ testcase( i==13 ); /* EXPLAIN */
89662
+ testcase( i==14 ); /* INSTEAD */
89663
+ testcase( i==15 ); /* ADD */
89664
+ testcase( i==16 ); /* DATABASE */
89665
+ testcase( i==17 ); /* AS */
89666
+ testcase( i==18 ); /* SELECT */
89667
+ testcase( i==19 ); /* TABLE */
89668
+ testcase( i==20 ); /* LEFT */
89669
+ testcase( i==21 ); /* THEN */
89670
+ testcase( i==22 ); /* END */
89671
+ testcase( i==23 ); /* DEFERRABLE */
89672
+ testcase( i==24 ); /* ELSE */
89673
+ testcase( i==25 ); /* EXCEPT */
89674
+ testcase( i==26 ); /* TRANSACTION */
89675
+ testcase( i==27 ); /* ON */
89676
+ testcase( i==28 ); /* NATURAL */
89677
+ testcase( i==29 ); /* ALTER */
89678
+ testcase( i==30 ); /* RAISE */
89679
+ testcase( i==31 ); /* EXCLUSIVE */
89680
+ testcase( i==32 ); /* EXISTS */
89681
+ testcase( i==33 ); /* SAVEPOINT */
89682
+ testcase( i==34 ); /* INTERSECT */
89683
+ testcase( i==35 ); /* TRIGGER */
89684
+ testcase( i==36 ); /* REFERENCES */
89685
+ testcase( i==37 ); /* CONSTRAINT */
89686
+ testcase( i==38 ); /* INTO */
89687
+ testcase( i==39 ); /* OFFSET */
89688
+ testcase( i==40 ); /* OF */
89689
+ testcase( i==41 ); /* SET */
89690
+ testcase( i==42 ); /* TEMP */
89691
+ testcase( i==43 ); /* TEMPORARY */
89692
+ testcase( i==44 ); /* OR */
89693
+ testcase( i==45 ); /* UNIQUE */
89694
+ testcase( i==46 ); /* QUERY */
89695
+ testcase( i==47 ); /* ATTACH */
89696
+ testcase( i==48 ); /* HAVING */
89697
+ testcase( i==49 ); /* GROUP */
89698
+ testcase( i==50 ); /* UPDATE */
89699
+ testcase( i==51 ); /* BEGIN */
89700
+ testcase( i==52 ); /* INNER */
89701
+ testcase( i==53 ); /* RELEASE */
89702
+ testcase( i==54 ); /* BETWEEN */
89703
+ testcase( i==55 ); /* NOTNULL */
89704
+ testcase( i==56 ); /* NOT */
89705
+ testcase( i==57 ); /* NULL */
89706
+ testcase( i==58 ); /* LIKE */
89707
+ testcase( i==59 ); /* CASCADE */
89708
+ testcase( i==60 ); /* ASC */
89709
+ testcase( i==61 ); /* DELETE */
89710
+ testcase( i==62 ); /* CASE */
89711
+ testcase( i==63 ); /* COLLATE */
89712
+ testcase( i==64 ); /* CREATE */
89713
+ testcase( i==65 ); /* CURRENT_DATE */
89714
+ testcase( i==66 ); /* DETACH */
89715
+ testcase( i==67 ); /* IMMEDIATE */
89716
+ testcase( i==68 ); /* JOIN */
89717
+ testcase( i==69 ); /* INSERT */
89718
+ testcase( i==70 ); /* MATCH */
89719
+ testcase( i==71 ); /* PLAN */
89720
+ testcase( i==72 ); /* ANALYZE */
89721
+ testcase( i==73 ); /* PRAGMA */
89722
+ testcase( i==74 ); /* ABORT */
89723
+ testcase( i==75 ); /* VALUES */
89724
+ testcase( i==76 ); /* VIRTUAL */
89725
+ testcase( i==77 ); /* LIMIT */
89726
+ testcase( i==78 ); /* WHEN */
89727
+ testcase( i==79 ); /* WHERE */
89728
+ testcase( i==80 ); /* RENAME */
89729
+ testcase( i==81 ); /* AFTER */
89730
+ testcase( i==82 ); /* REPLACE */
89731
+ testcase( i==83 ); /* AND */
89732
+ testcase( i==84 ); /* DEFAULT */
89733
+ testcase( i==85 ); /* AUTOINCREMENT */
89734
+ testcase( i==86 ); /* TO */
89735
+ testcase( i==87 ); /* IN */
89736
+ testcase( i==88 ); /* CAST */
89737
+ testcase( i==89 ); /* COLUMN */
89738
+ testcase( i==90 ); /* COMMIT */
89739
+ testcase( i==91 ); /* CONFLICT */
89740
+ testcase( i==92 ); /* CROSS */
89741
+ testcase( i==93 ); /* CURRENT_TIMESTAMP */
89742
+ testcase( i==94 ); /* CURRENT_TIME */
89743
+ testcase( i==95 ); /* PRIMARY */
89744
+ testcase( i==96 ); /* DEFERRED */
89745
+ testcase( i==97 ); /* DISTINCT */
89746
+ testcase( i==98 ); /* IS */
89747
+ testcase( i==99 ); /* DROP */
89748
+ testcase( i==100 ); /* FAIL */
89749
+ testcase( i==101 ); /* FROM */
89750
+ testcase( i==102 ); /* FULL */
89751
+ testcase( i==103 ); /* GLOB */
89752
+ testcase( i==104 ); /* BY */
89753
+ testcase( i==105 ); /* IF */
89754
+ testcase( i==106 ); /* ISNULL */
89755
+ testcase( i==107 ); /* ORDER */
89756
+ testcase( i==108 ); /* RESTRICT */
89757
+ testcase( i==109 ); /* OUTER */
89758
+ testcase( i==110 ); /* RIGHT */
89759
+ testcase( i==111 ); /* ROLLBACK */
89760
+ testcase( i==112 ); /* ROW */
89761
+ testcase( i==113 ); /* UNION */
89762
+ testcase( i==114 ); /* USING */
89763
+ testcase( i==115 ); /* VACUUM */
89764
+ testcase( i==116 ); /* VIEW */
89765
+ testcase( i==117 ); /* INITIALLY */
89766
+ testcase( i==118 ); /* ALL */
8988389767
return aCode[i];
8988489768
}
8988589769
}
8988689770
return TK_ID;
8988789771
}
@@ -89947,10 +89831,15 @@
8994789831
*/
8994889832
SQLITE_PRIVATE int sqlite3GetToken(const unsigned char *z, int *tokenType){
8994989833
int i, c;
8995089834
switch( *z ){
8995189835
case ' ': case '\t': case '\n': case '\f': case '\r': {
89836
+ testcase( z[0]==' ' );
89837
+ testcase( z[0]=='\t' );
89838
+ testcase( z[0]=='\n' );
89839
+ testcase( z[0]=='\f' );
89840
+ testcase( z[0]=='\r' );
8995289841
for(i=1; sqlite3Isspace(z[i]); i++){}
8995389842
*tokenType = TK_SPACE;
8995489843
return i;
8995589844
}
8995689845
case '-': {
@@ -90059,10 +89948,13 @@
9005989948
}
9006089949
case '`':
9006189950
case '\'':
9006289951
case '"': {
9006389952
int delim = z[0];
89953
+ testcase( delim=='`' );
89954
+ testcase( delim=='\'' );
89955
+ testcase( delim=='"' );
9006489956
for(i=1; (c=z[i])!=0; i++){
9006589957
if( c==delim ){
9006689958
if( z[i+1]==delim ){
9006789959
i++;
9006889960
}else{
@@ -90092,10 +89984,14 @@
9009289984
/* If the next character is a digit, this is a floating point
9009389985
** number that begins with ".". Fall thru into the next case */
9009489986
}
9009589987
case '0': case '1': case '2': case '3': case '4':
9009689988
case '5': case '6': case '7': case '8': case '9': {
89989
+ testcase( z[0]=='0' ); testcase( z[0]=='1' ); testcase( z[0]=='2' );
89990
+ testcase( z[0]=='3' ); testcase( z[0]=='4' ); testcase( z[0]=='5' );
89991
+ testcase( z[0]=='6' ); testcase( z[0]=='7' ); testcase( z[0]=='8' );
89992
+ testcase( z[0]=='9' );
9009789993
*tokenType = TK_INTEGER;
9009889994
for(i=0; sqlite3Isdigit(z[i]); i++){}
9009989995
#ifndef SQLITE_OMIT_FLOATING_POINT
9010089996
if( z[i]=='.' ){
9010189997
i++;
@@ -90143,10 +90039,11 @@
9014390039
case '$':
9014490040
#endif
9014590041
case '@': /* For compatibility with MS SQL Server */
9014690042
case ':': {
9014790043
int n = 0;
90044
+ testcase( z[0]=='$' ); testcase( z[0]=='@' ); testcase( z[0]==':' );
9014890045
*tokenType = TK_VARIABLE;
9014990046
for(i=1; (c=z[i])!=0; i++){
9015090047
if( IdChar(c) ){
9015190048
n++;
9015290049
#ifndef SQLITE_OMIT_TCL_VARIABLE
@@ -90170,10 +90067,11 @@
9017090067
if( n==0 ) *tokenType = TK_ILLEGAL;
9017190068
return i;
9017290069
}
9017390070
#ifndef SQLITE_OMIT_BLOB_LITERAL
9017490071
case 'x': case 'X': {
90072
+ testcase( z[0]=='x' ); testcase( z[0]=='X' );
9017590073
if( z[1]=='\'' ){
9017690074
*tokenType = TK_BLOB;
9017790075
for(i=2; (c=z[i])!=0 && c!='\''; i++){
9017890076
if( !sqlite3Isxdigit(c) ){
9017990077
*tokenType = TK_ILLEGAL;
@@ -90248,12 +90146,12 @@
9024890146
break;
9024990147
}
9025090148
switch( tokenType ){
9025190149
case TK_SPACE: {
9025290150
if( db->u1.isInterrupted ){
90151
+ sqlite3ErrorMsg(pParse, "interrupt");
9025390152
pParse->rc = SQLITE_INTERRUPT;
90254
- sqlite3SetString(pzErrMsg, db, "interrupt");
9025590153
goto abort_parse;
9025690154
}
9025790155
break;
9025890156
}
9025990157
case TK_ILLEGAL: {
@@ -90296,16 +90194,13 @@
9029690194
pParse->rc = SQLITE_NOMEM;
9029790195
}
9029890196
if( pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE && pParse->zErrMsg==0 ){
9029990197
sqlite3SetString(&pParse->zErrMsg, db, "%s", sqlite3ErrStr(pParse->rc));
9030090198
}
90199
+ assert( pzErrMsg!=0 );
9030190200
if( pParse->zErrMsg ){
90302
- if( *pzErrMsg==0 ){
90303
- *pzErrMsg = pParse->zErrMsg;
90304
- }else{
90305
- sqlite3DbFree(db, pParse->zErrMsg);
90306
- }
90201
+ *pzErrMsg = pParse->zErrMsg;
9030790202
pParse->zErrMsg = 0;
9030890203
nErr++;
9030990204
}
9031090205
if( pParse->pVdbe && pParse->nErr>0 && pParse->nested==0 ){
9031190206
sqlite3VdbeDelete(pParse->pVdbe);
@@ -90336,11 +90231,11 @@
9033690231
while( pParse->pZombieTab ){
9033790232
Table *p = pParse->pZombieTab;
9033890233
pParse->pZombieTab = p->pNextZombie;
9033990234
sqlite3DeleteTable(p);
9034090235
}
90341
- if( nErr>0 && (pParse->rc==SQLITE_OK || pParse->rc==SQLITE_DONE) ){
90236
+ if( nErr>0 && pParse->rc==SQLITE_OK ){
9034290237
pParse->rc = SQLITE_ERROR;
9034390238
}
9034490239
return nErr;
9034590240
}
9034690241
@@ -90639,11 +90534,11 @@
9063990534
** Main file for the SQLite library. The routines in this file
9064090535
** implement the programmer interface to the library. Routines in
9064190536
** other files are for internal use by SQLite and should not be
9064290537
** accessed by users of the library.
9064390538
**
90644
-** $Id: main.c,v 1.555 2009/06/01 16:53:10 shane Exp $
90539
+** $Id: main.c,v 1.558 2009/06/19 14:06:03 drh Exp $
9064590540
*/
9064690541
9064790542
#ifdef SQLITE_ENABLE_FTS3
9064890543
/************** Include fts3.h in the middle of main.c ***********************/
9064990544
/************** Begin file fts3.h ********************************************/
@@ -91061,11 +90956,10 @@
9106190956
memset(&sqlite3GlobalConfig.m, 0, sizeof(sqlite3GlobalConfig.m));
9106290957
}else{
9106390958
/* The heap pointer is not NULL, then install one of the
9106490959
** mem5.c/mem3.c methods. If neither ENABLE_MEMSYS3 nor
9106590960
** ENABLE_MEMSYS5 is defined, return an error.
91066
- ** the default case and return an error.
9106790961
*/
9106890962
#ifdef SQLITE_ENABLE_MEMSYS3
9106990963
sqlite3GlobalConfig.m = *sqlite3MemGetMemsys3();
9107090964
#endif
9107190965
#ifdef SQLITE_ENABLE_MEMSYS5
@@ -91296,17 +91190,10 @@
9129691190
if( !sqlite3SafetyCheckSickOrOk(db) ){
9129791191
return SQLITE_MISUSE;
9129891192
}
9129991193
sqlite3_mutex_enter(db->mutex);
9130091194
91301
-#ifdef SQLITE_SSE
91302
- {
91303
- extern void sqlite3SseCleanup(sqlite3*);
91304
- sqlite3SseCleanup(db);
91305
- }
91306
-#endif
91307
-
9130891195
sqlite3ResetInternalSchema(db, 0);
9130991196
9131091197
/* If a transaction is open, the ResetInternalSchema() call above
9131191198
** will not have called the xDisconnect() method on any virtual
9131291199
** tables in the db->aVTrans[] array. The following sqlite3VtabRollback()
9131391200
--- 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.14. 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.
@@ -9,17 +9,17 @@
9 **
10 ** This file is all you need to compile SQLite. To use SQLite in other
11 ** programs, you need this file and the "sqlite3.h" header file that defines
12 ** the programming interface to the SQLite library. (If you do not have
13 ** the "sqlite3.h" header file at hand, you will find a copy in the first
14 ** 5605 lines past this header comment.) Additional code files may be
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-07 12:34:52 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.882 2009/06/05 14:17:23 drh Exp $
45 */
46 #ifndef _SQLITEINT_H_
47 #define _SQLITEINT_H_
48
49 /*
@@ -545,11 +545,11 @@
545 ** The name of this file under configuration management is "sqlite.h.in".
546 ** The makefile makes some minor changes to this file (such as inserting
547 ** the version number) and changes its name to "sqlite3.h" as
548 ** part of the build process.
549 **
550 ** @(#) $Id: sqlite.h.in,v 1.455 2009/05/24 21:59:28 drh Exp $
551 */
552 #ifndef _SQLITE3_H_
553 #define _SQLITE3_H_
554 #include <stdarg.h> /* Needed for the definition of va_list */
555
@@ -614,12 +614,12 @@
614 **
615 ** See also: [sqlite3_libversion()] and [sqlite3_libversion_number()].
616 **
617 ** Requirements: [H10011] [H10014]
618 */
619 #define SQLITE_VERSION "3.6.14"
620 #define SQLITE_VERSION_NUMBER 3006014
621
622 /*
623 ** CAPI3REF: Run-Time Library Version Numbers {H10020} <S60100>
624 ** KEYWORDS: sqlite3_version
625 **
@@ -1009,10 +1009,16 @@
1009 ** [sqlite3_file] object (or, more commonly, a subclass of the
1010 ** [sqlite3_file] object) with a pointer to an instance of this object.
1011 ** This object defines the methods used to perform various operations
1012 ** against the open file represented by the [sqlite3_file] object.
1013 **
 
 
 
 
 
 
1014 ** The flags argument to xSync may be one of [SQLITE_SYNC_NORMAL] or
1015 ** [SQLITE_SYNC_FULL]. The first choice is the normal fsync().
1016 ** The second choice is a Mac OS X style fullsync. The [SQLITE_SYNC_DATAONLY]
1017 ** flag may be ORed in to indicate that only the data of the file
1018 ** and not its inode needs to be synced.
@@ -1169,15 +1175,15 @@
1169 **
1170 ** SQLite will guarantee that the zFilename parameter to xOpen
1171 ** is either a NULL pointer or string obtained
1172 ** from xFullPathname(). SQLite further guarantees that
1173 ** the string will be valid and unchanged until xClose() is
1174 ** called. Because of the previous sentense,
1175 ** the [sqlite3_file] can safely store a pointer to the
1176 ** filename if it needs to remember the filename for some reason.
1177 ** If the zFilename parameter is xOpen is a NULL pointer then xOpen
1178 ** must invite its own temporary name for the file. Whenever the
1179 ** xFilename parameter is NULL it will also be the case that the
1180 ** flags parameter will include [SQLITE_OPEN_DELETEONCLOSE].
1181 **
1182 ** The flags argument to xOpen() includes all bits set in
1183 ** the flags argument to [sqlite3_open_v2()]. Or if [sqlite3_open()]
@@ -1229,11 +1235,16 @@
1229 ** for exclusive access.
1230 **
1231 ** At least szOsFile bytes of memory are allocated by SQLite
1232 ** to hold the [sqlite3_file] structure passed as the third
1233 ** argument to xOpen. The xOpen method does not have to
1234 ** allocate the structure; it should just fill it in.
 
 
 
 
 
1235 **
1236 ** The flags argument to xAccess() may be [SQLITE_ACCESS_EXISTS]
1237 ** to test for the existence of a file, or [SQLITE_ACCESS_READWRITE] to
1238 ** test whether a file is readable and writable, or [SQLITE_ACCESS_READ]
1239 ** to test whether a file is at least readable. The file can be a
@@ -1551,16 +1562,18 @@
1551 ** </ul>
1552 ** </dd>
1553 **
1554 ** <dt>SQLITE_CONFIG_SCRATCH</dt>
1555 ** <dd>This option specifies a static memory buffer that SQLite can use for
1556 ** scratch memory. There are three arguments: A pointer to the memory, the
1557 ** size of each scratch buffer (sz), and the number of buffers (N). The sz
 
 
1558 ** argument must be a multiple of 16. The sz parameter should be a few bytes
1559 ** larger than the actual scratch space required due internal overhead.
1560 ** The first
1561 ** argument should point to an allocation of at least sz*N bytes of memory.
1562 ** SQLite will use no more than one scratch buffer at once per thread, so
1563 ** N should be set to the expected maximum number of threads. The sz
1564 ** parameter should be 6 times the size of the largest database page size.
1565 ** Scratch buffers are used as part of the btree balance operation. If
1566 ** The btree balancer needs additional memory beyond what is provided by
@@ -1570,33 +1583,41 @@
1570 ** <dt>SQLITE_CONFIG_PAGECACHE</dt>
1571 ** <dd>This option specifies a static memory buffer that SQLite can use for
1572 ** the database page cache with the default page cache implemenation.
1573 ** This configuration should not be used if an application-define page
1574 ** cache implementation is loaded using the SQLITE_CONFIG_PCACHE option.
1575 ** There are three arguments to this option: A pointer to the
1576 ** memory, the size of each page buffer (sz), and the number of pages (N).
1577 ** The sz argument must be a power of two between 512 and 32768. The first
 
 
 
 
1578 ** argument should point to an allocation of at least sz*N bytes of memory.
1579 ** SQLite will use the memory provided by the first argument to satisfy its
1580 ** memory needs for the first N pages that it adds to cache. If additional
1581 ** page cache memory is needed beyond what is provided by this option, then
1582 ** SQLite goes to [sqlite3_malloc()] for the additional storage space.
1583 ** The implementation might use one or more of the N buffers to hold
1584 ** memory accounting information. </dd>
 
 
1585 **
1586 ** <dt>SQLITE_CONFIG_HEAP</dt>
1587 ** <dd>This option specifies a static memory buffer that SQLite will use
1588 ** for all of its dynamic memory allocation needs beyond those provided
1589 ** for by [SQLITE_CONFIG_SCRATCH] and [SQLITE_CONFIG_PAGECACHE].
1590 ** There are three arguments: A pointer to the memory, the number of
1591 ** bytes in the memory buffer, and the minimum allocation size. If
1592 ** the first pointer (the memory pointer) is NULL, then SQLite reverts
1593 ** to using its default memory allocator (the system malloc() implementation),
1594 ** undoing any prior invocation of [SQLITE_CONFIG_MALLOC]. If the
1595 ** memory pointer is not NULL and either [SQLITE_ENABLE_MEMSYS3] or
1596 ** [SQLITE_ENABLE_MEMSYS5] are defined, then the alternative memory
1597 ** allocator is engaged to handle all of SQLites memory allocation needs.</dd>
 
 
1598 **
1599 ** <dt>SQLITE_CONFIG_MUTEX</dt>
1600 ** <dd>This option takes a single argument which is a pointer to an
1601 ** instance of the [sqlite3_mutex_methods] structure. The argument specifies
1602 ** alternative low-level mutex routines to be used in place
@@ -1663,13 +1684,13 @@
1663 ** <dl>
1664 ** <dt>SQLITE_DBCONFIG_LOOKASIDE</dt>
1665 ** <dd>This option takes three additional arguments that determine the
1666 ** [lookaside memory allocator] configuration for the [database connection].
1667 ** The first argument (the third parameter to [sqlite3_db_config()] is a
1668 ** pointer to a memory buffer to use for lookaside memory. The first
1669 ** argument may be NULL in which case SQLite will allocate the lookaside
1670 ** buffer itself using [sqlite3_malloc()]. The second argument is the
1671 ** size of each lookaside buffer slot and the third argument is the number of
1672 ** slots. The size of the buffer in the first argument must be greater than
1673 ** or equal to the product of the second and third arguments.</dd>
1674 **
1675 ** </dl>
@@ -6397,15 +6418,16 @@
6397 */
6398 #ifdef SQLITE_OMIT_FLOATING_POINT
6399 # define double sqlite_int64
6400 # define LONGDOUBLE_TYPE sqlite_int64
6401 # ifndef SQLITE_BIG_DBL
6402 # define SQLITE_BIG_DBL (0x7fffffffffffffff)
6403 # endif
6404 # define SQLITE_OMIT_DATETIME_FUNCS 1
6405 # define SQLITE_OMIT_TRACE 1
6406 # undef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
 
6407 #endif
6408 #ifndef SQLITE_BIG_DBL
6409 # define SQLITE_BIG_DBL (1e99)
6410 #endif
6411
@@ -7375,11 +7397,11 @@
7375 *************************************************************************
7376 ** This header file defines the interface that the sqlite page cache
7377 ** subsystem. The page cache subsystem reads and writes a file a page
7378 ** at a time and provides a journal for rollback.
7379 **
7380 ** @(#) $Id: pager.h,v 1.101 2009/04/30 09:10:38 danielk1977 Exp $
7381 */
7382
7383 #ifndef _PAGER_H_
7384 #define _PAGER_H_
7385
@@ -7455,11 +7477,11 @@
7455 SQLITE_PRIVATE int sqlite3PagerReadFileheader(Pager*, int, unsigned char*);
7456
7457 /* Functions used to configure a Pager object. */
7458 SQLITE_PRIVATE void sqlite3PagerSetBusyhandler(Pager*, int(*)(void *), void *);
7459 SQLITE_PRIVATE void sqlite3PagerSetReiniter(Pager*, void(*)(DbPage*));
7460 SQLITE_PRIVATE int sqlite3PagerSetPagesize(Pager*, u16*);
7461 SQLITE_PRIVATE int sqlite3PagerMaxPageCount(Pager*, int);
7462 SQLITE_PRIVATE void sqlite3PagerSetCachesize(Pager*, int);
7463 SQLITE_PRIVATE void sqlite3PagerSetSafetyLevel(Pager*,int,int);
7464 SQLITE_PRIVATE int sqlite3PagerLockingMode(Pager *, int);
7465 SQLITE_PRIVATE int sqlite3PagerJournalMode(Pager *, int);
@@ -7503,15 +7525,10 @@
7503 SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager*);
7504
7505 /* Functions used to truncate the database file. */
7506 SQLITE_PRIVATE void sqlite3PagerTruncateImage(Pager*,Pgno);
7507
7508 /* Used by encryption extensions. */
7509 #ifdef SQLITE_HAS_CODEC
7510 SQLITE_PRIVATE void sqlite3PagerSetCodec(Pager*,void*(*)(void*,void*,Pgno,int),void*);
7511 #endif
7512
7513 /* Functions to support testing and debugging. */
7514 #if !defined(NDEBUG) || defined(SQLITE_TEST)
7515 SQLITE_PRIVATE Pgno sqlite3PagerPagenumber(DbPage*);
7516 SQLITE_PRIVATE int sqlite3PagerIswriteable(DbPage*);
7517 #endif
@@ -8259,13 +8276,10 @@
8259 FuncDefHash aFunc; /* Hash table of connection functions */
8260 Hash aCollSeq; /* All collating sequences */
8261 BusyHandler busyHandler; /* Busy callback */
8262 int busyTimeout; /* Busy handler timeout, in msec */
8263 Db aDbStatic[2]; /* Static space for the 2 default backends */
8264 #ifdef SQLITE_SSE
8265 sqlite3_stmt *pFetch; /* Used by SSE to fetch stored statements */
8266 #endif
8267 Savepoint *pSavepoint; /* List of active savepoints */
8268 int nSavepoint; /* Number of non-transaction savepoints */
8269 int nStatement; /* Number of nested statement-transactions */
8270 u8 isTransactionSavepoint; /* True if the outermost savepoint is a TS */
8271
@@ -10301,15 +10315,10 @@
10301 #define sqlite3ConnectionBlocked(x,y)
10302 #define sqlite3ConnectionUnlocked(x)
10303 #define sqlite3ConnectionClosed(x)
10304 #endif
10305
10306
10307 #ifdef SQLITE_SSE
10308 #include "sseInt.h"
10309 #endif
10310
10311 #ifdef SQLITE_DEBUG
10312 SQLITE_PRIVATE void sqlite3ParserTrace(FILE*, char *);
10313 #endif
10314
10315 /*
@@ -17073,22 +17082,14 @@
17073 ** VDBE. This information used to all be at the top of the single
17074 ** source code file "vdbe.c". When that file became too big (over
17075 ** 6000 lines long) it was split up into several smaller files and
17076 ** this header information was factored out.
17077 **
17078 ** $Id: vdbeInt.h,v 1.171 2009/06/05 14:17:25 drh Exp $
17079 */
17080 #ifndef _VDBEINT_H_
17081 #define _VDBEINT_H_
17082
17083 /*
17084 ** intToKey() and keyToInt() used to transform the rowid. But with
17085 ** the latest versions of the design they are no-ops.
17086 */
17087 #define keyToInt(X) (X)
17088 #define intToKey(X) (X)
17089
17090
17091 /*
17092 ** SQL is translated into a sequence of instructions to be
17093 ** executed by a virtual machine. Each instruction is an instance
17094 ** of the following structure.
@@ -17320,66 +17321,57 @@
17320 ** "DROP TABLE" statements and to prevent some nasty side effects of
17321 ** malloc failure when SQLite is invoked recursively by a virtual table
17322 ** method function.
17323 */
17324 struct Vdbe {
17325 sqlite3 *db; /* The whole database */
17326 Vdbe *pPrev,*pNext; /* Linked list of VDBEs with the same Vdbe.db */
17327 int nOp; /* Number of instructions in the program */
17328 int nOpAlloc; /* Number of slots allocated for aOp[] */
17329 Op *aOp; /* Space to hold the virtual machine's program */
17330 int nLabel; /* Number of labels used */
17331 int nLabelAlloc; /* Number of slots allocated in aLabel[] */
17332 int *aLabel; /* Space to hold the labels */
17333 Mem **apArg; /* Arguments to currently executing user function */
17334 Mem *aColName; /* Column names to return */
17335 int nCursor; /* Number of slots in apCsr[] */
17336 VdbeCursor **apCsr; /* One element of this array for each open cursor */
17337 int nVar; /* Number of entries in aVar[] */
17338 Mem *aVar; /* Values for the OP_Variable opcode. */
17339 char **azVar; /* Name of variables */
17340 int okVar; /* True if azVar[] has been initialized */
 
 
 
17341 u32 magic; /* Magic number for sanity checking */
17342 int nMem; /* Number of memory locations currently allocated */
17343 Mem *aMem; /* The memory locations */
17344 int cacheCtr; /* VdbeCursor row cache generation counter */
17345 int contextStackTop; /* Index of top element in the context stack */
17346 int contextStackDepth; /* The size of the "context" stack */
17347 Context *contextStack; /* Stack used by opcodes ContextPush & ContextPop*/
17348 int pc; /* The program counter */
17349 int rc; /* Value to return */
17350 int errorAction; /* Recovery action to do in case of an error */
17351 int nResColumn; /* Number of columns in one row of the result set */
17352 char **azResColumn; /* Values for one row of result */
17353 char *zErrMsg; /* Error message written here */
17354 Mem *pResultSet; /* Pointer to an array of results */
17355 u8 explain; /* True if EXPLAIN present on SQL command */
17356 u8 changeCntOn; /* True to update the change-counter */
17357 u8 expired; /* True if the VM needs to be recompiled */
17358 u8 minWriteFileFormat; /* Minimum file format for writable database files */
17359 u8 inVtabMethod; /* See comments above */
17360 u8 usesStmtJournal; /* True if uses a statement journal */
17361 u8 readOnly; /* True for read-only statements */
17362 u8 isPrepareV2; /* True if prepared with prepare_v2() */
17363 int nChange; /* Number of db changes made since last reset */
17364 i64 startTime; /* Time when query started - used for profiling */
17365 int btreeMask; /* Bitmask of db->aDb[] entries referenced */
 
17366 BtreeMutexArray aMutex; /* An array of Btree used here and needing locks */
17367 int aCounter[2]; /* Counters used by sqlite3_stmt_status() */
17368 char *zSql; /* Text of the SQL statement that generated this */
17369 void *pFree; /* Free this when deleting the vdbe */
17370 #ifdef SQLITE_DEBUG
17371 FILE *trace; /* Write an execution trace here, if not NULL */
17372 #endif
17373 int iStatement; /* Statement number (or 0 if has not opened stmt) */
17374 #ifdef SQLITE_SSE
17375 int fetchId; /* Statement number used by sqlite3_fetch_statement */
17376 int lru; /* Counter used for LRU cache replacement */
17377 #endif
17378 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
17379 Vdbe *pLruPrev;
17380 Vdbe *pLruNext;
17381 #endif
17382 };
17383
17384 /*
17385 ** The following are allowed values for Vdbe.magic
@@ -17404,11 +17396,11 @@
17404 SQLITE_PRIVATE u32 sqlite3VdbeSerialGet(const unsigned char*, u32, Mem*);
17405 SQLITE_PRIVATE void sqlite3VdbeDeleteAuxData(VdbeFunc*, int);
17406
17407 int sqlite2BtreeKeyCompare(BtCursor *, const void *, int, int, int *);
17408 SQLITE_PRIVATE int sqlite3VdbeIdxKeyCompare(VdbeCursor*,UnpackedRecord*,int*);
17409 SQLITE_PRIVATE int sqlite3VdbeIdxRowid(BtCursor *, i64 *);
17410 SQLITE_PRIVATE int sqlite3MemCompare(const Mem*, const Mem*, const CollSeq*);
17411 SQLITE_PRIVATE int sqlite3VdbeExec(Vdbe*);
17412 SQLITE_PRIVATE int sqlite3VdbeList(Vdbe*);
17413 SQLITE_PRIVATE int sqlite3VdbeHalt(Vdbe*);
17414 SQLITE_PRIVATE int sqlite3VdbeChangeEncoding(Mem *, int);
@@ -17985,11 +17977,11 @@
17985 ** Utility functions used throughout sqlite.
17986 **
17987 ** This file contains functions for allocating memory, comparing
17988 ** strings, and stuff like that.
17989 **
17990 ** $Id: util.c,v 1.258 2009/06/05 14:17:23 drh Exp $
17991 */
17992 #ifdef SQLITE_HAVE_ISNAN
17993 # include <math.h>
17994 #endif
17995
@@ -18077,11 +18069,11 @@
18077 ** than the actual length of the string. For very long strings (greater
18078 ** than 1GiB) the value returned might be less than the true string length.
18079 */
18080 SQLITE_PRIVATE int sqlite3Strlen30(const char *z){
18081 const char *z2 = z;
18082 if( NEVER(z==0) ) return 0;
18083 while( *z2 ){ z2++; }
18084 return 0x3fffffff & (int)(z2 - z);
18085 }
18086
18087 /*
@@ -18361,11 +18353,11 @@
18361 **
18362 ** will return -8.
18363 */
18364 static int compare2pow63(const char *zNum){
18365 int c;
18366 c = memcmp(zNum,"922337203685477580",18);
18367 if( c==0 ){
18368 c = zNum[18] - '8';
18369 }
18370 return c;
18371 }
@@ -20864,11 +20856,11 @@
20864 ** * sqlite3_vfs method implementations.
20865 ** * Locking primitives for the proxy uber-locking-method. (MacOSX only)
20866 ** * Definitions of sqlite3_vfs objects for all locking methods
20867 ** plus implementations of sqlite3_os_init() and sqlite3_os_end().
20868 **
20869 ** $Id: os_unix.c,v 1.251 2009/05/08 11:34:37 danielk1977 Exp $
20870 */
20871 #if SQLITE_OS_UNIX /* This file is used on unix only */
20872
20873 /*
20874 ** There are various methods for file locking used for concurrency
@@ -21952,11 +21944,11 @@
21952 int rc; /* System call return code */
21953 int fd; /* The file descriptor for pFile */
21954 struct unixLockKey lockKey; /* Lookup key for the unixLockInfo structure */
21955 struct unixFileId fileId; /* Lookup key for the unixOpenCnt struct */
21956 struct stat statbuf; /* Low-level file information */
21957 struct unixLockInfo *pLock; /* Candidate unixLockInfo object */
21958 struct unixOpenCnt *pOpen; /* Candidate unixOpenCnt object */
21959
21960 /* Get low-level information about the file that we can used to
21961 ** create a unique name for the file.
21962 */
@@ -22856,11 +22848,12 @@
22856 }
22857
22858 /* To fully unlock the database, delete the lock file */
22859 assert( locktype==NO_LOCK );
22860 if( unlink(zLockFile) ){
22861 int rc, tErrno = errno;
 
22862 if( ENOENT != tErrno ){
22863 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
22864 }
22865 if( IS_LOCK_ERROR(rc) ){
22866 pFile->lastErrno = tErrno;
@@ -25106,11 +25099,15 @@
25106 ** Find the current time (in Universal Coordinated Time). Write the
25107 ** current time and date as a Julian Day number into *prNow and
25108 ** return 0. Return 1 if the time and date cannot be found.
25109 */
25110 static int unixCurrentTime(sqlite3_vfs *NotUsed, double *prNow){
25111 #if defined(NO_GETTOD)
 
 
 
 
25112 time_t t;
25113 time(&t);
25114 *prNow = t/86400.0 + 2440587.5;
25115 #elif OS_VXWORKS
25116 struct timespec sNow;
@@ -29299,11 +29296,11 @@
29299 ** sqlite3_pcache interface). It also contains part of the implementation
29300 ** of the SQLITE_CONFIG_PAGECACHE and sqlite3_release_memory() features.
29301 ** If the default page cache implementation is overriden, then neither of
29302 ** these two features are available.
29303 **
29304 ** @(#) $Id: pcache1.c,v 1.16 2009/06/03 21:04:36 drh Exp $
29305 */
29306
29307
29308 typedef struct PCache1 PCache1;
29309 typedef struct PgHdr1 PgHdr1;
@@ -29650,11 +29647,11 @@
29650 */
29651 static void pcache1TruncateUnsafe(
29652 PCache1 *pCache,
29653 unsigned int iLimit
29654 ){
29655 TESTONLY( int nPage = 0; ) /* Used to assert pCache->nPage is correct */
29656 unsigned int h;
29657 assert( sqlite3_mutex_held(pcache1.mutex) );
29658 for(h=0; h<pCache->nHash; h++){
29659 PgHdr1 **pp = &pCache->apHash[h];
29660 PgHdr1 *pPage;
@@ -30498,11 +30495,11 @@
30498 ** is separate from the database file. The pager also implements file
30499 ** locking to prevent two processes from writing the same database
30500 ** file simultaneously, or one process from reading the database while
30501 ** another is writing.
30502 **
30503 ** @(#) $Id: pager.c,v 1.591 2009/06/02 21:31:39 drh Exp $
30504 */
30505 #ifndef SQLITE_OMIT_DISKIO
30506
30507 /*
30508 ** Macros for troubleshooting. Normally turned off
@@ -30582,15 +30579,18 @@
30582
30583 /*
30584 ** A macro used for invoking the codec if there is one
30585 */
30586 #ifdef SQLITE_HAS_CODEC
30587 # define CODEC1(P,D,N,X) if( P->xCodec!=0 ){ P->xCodec(P->pCodecArg,D,N,X); }
30588 # define CODEC2(P,D,N,X) ((char*)(P->xCodec!=0?P->xCodec(P->pCodecArg,D,N,X):D))
 
 
 
30589 #else
30590 # define CODEC1(P,D,N,X) /* NO-OP */
30591 # define CODEC2(P,D,N,X) ((char*)D)
30592 #endif
30593
30594 /*
30595 ** The maximum allowed sector size. 16MB. If the xSectorsize() method
30596 ** returns a value larger than this, then MAX_SECTOR_SIZE is used instead.
@@ -30766,11 +30766,12 @@
30766 PagerSavepoint *aSavepoint; /* Array of active savepoints */
30767 int nSavepoint; /* Number of elements in aSavepoint[] */
30768 char dbFileVers[16]; /* Changes whenever database file changes */
30769 u32 sectorSize; /* Assumed sector size during rollback */
30770
30771 int nExtra; /* Add this many bytes to each in-memory page */
 
30772 u32 vfsFlags; /* Flags for sqlite3_vfs.xOpen() */
30773 int pageSize; /* Number of bytes in a page */
30774 Pgno mxPgno; /* Maximum allowed size of the database */
30775 char *zFilename; /* Name of the database file */
30776 char *zJournal; /* Name of the journal file */
@@ -30781,11 +30782,13 @@
30781 int nRead, nWrite; /* Database pages read/written */
30782 #endif
30783 void (*xReiniter)(DbPage*); /* Call this routine when reloading pages */
30784 #ifdef SQLITE_HAS_CODEC
30785 void *(*xCodec)(void*,void*,Pgno,int); /* Routine for en/decoding data */
30786 void *pCodecArg; /* First argument to xCodec() */
 
 
30787 #endif
30788 char *pTmpSpace; /* Pager.pageSize bytes of space for tmp use */
30789 i64 journalSizeLimit; /* Size limit for persistent journal files */
30790 PCache *pPCache; /* Pointer to page cache object */
30791 sqlite3_backup *pBackup; /* Pointer to list of ongoing backup processes */
@@ -31403,11 +31406,11 @@
31403 /* Update the page-size to match the value read from the journal.
31404 ** Use a testcase() macro to make sure that malloc failure within
31405 ** PagerSetPagesize() is tested.
31406 */
31407 iPageSize16 = (u16)iPageSize;
31408 rc = sqlite3PagerSetPagesize(pPager, &iPageSize16);
31409 testcase( rc!=SQLITE_OK );
31410 assert( rc!=SQLITE_OK || iPageSize16==(u16)iPageSize );
31411
31412 /* Update the assumed sector-size to match the value used by
31413 ** the process that created this journal. If this journal was
@@ -32005,11 +32008,15 @@
32005 i64 ofst = (pgno-1)*(i64)pPager->pageSize;
32006 rc = sqlite3OsWrite(pPager->fd, aData, pPager->pageSize, ofst);
32007 if( pgno>pPager->dbFileSize ){
32008 pPager->dbFileSize = pgno;
32009 }
32010 sqlite3BackupUpdate(pPager->pBackup, pgno, aData);
 
 
 
 
32011 }else if( !isMainJrnl && pPg==0 ){
32012 /* If this is a rollback of a savepoint and data was not written to
32013 ** the database and the page is not in-memory, there is a potential
32014 ** problem. When the page is next fetched by the b-tree layer, it
32015 ** will be read from the database file, which may or may not be
@@ -32074,11 +32081,11 @@
32074 if( pgno==1 ){
32075 memcpy(&pPager->dbFileVers, &((u8*)pData)[24],sizeof(pPager->dbFileVers));
32076 }
32077
32078 /* Decode the page just read from disk */
32079 CODEC1(pPager, pData, pPg->pgno, 3);
32080 sqlite3PcacheRelease(pPg);
32081 }
32082 return rc;
32083 }
32084
@@ -32837,10 +32844,25 @@
32837 */
32838 SQLITE_PRIVATE void sqlite3PagerSetReiniter(Pager *pPager, void (*xReinit)(DbPage*)){
32839 pPager->xReiniter = xReinit;
32840 }
32841
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32842 /*
32843 ** Change the page size used by the Pager object. The new page size
32844 ** is passed in *pPageSize.
32845 **
32846 ** If the pager is in the error state when this function is called, it
@@ -32867,11 +32889,11 @@
32867 ** If the page size is not changed, either because one of the enumerated
32868 ** conditions above is not true, the pager was in error state when this
32869 ** function was called, or because the memory allocation attempt failed,
32870 ** then *pPageSize is set to the old, retained page size before returning.
32871 */
32872 SQLITE_PRIVATE int sqlite3PagerSetPagesize(Pager *pPager, u16 *pPageSize){
32873 int rc = pPager->errCode;
32874 if( rc==SQLITE_OK ){
32875 u16 pageSize = *pPageSize;
32876 assert( pageSize==0 || (pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE) );
32877 if( pageSize && pageSize!=pPager->pageSize
@@ -32888,10 +32910,14 @@
32888 pPager->pTmpSpace = pNew;
32889 sqlite3PcacheSetPageSize(pPager->pPCache, pageSize);
32890 }
32891 }
32892 *pPageSize = (u16)pPager->pageSize;
 
 
 
 
32893 }
32894 return rc;
32895 }
32896
32897 /*
@@ -33135,10 +33161,14 @@
33135 PAGERTRACE(("CLOSE %d\n", PAGERID(pPager)));
33136 IOTRACE(("CLOSE %p\n", pPager))
33137 sqlite3OsClose(pPager->fd);
33138 sqlite3PageFree(pPager->pTmpSpace);
33139 sqlite3PcacheClose(pPager->pPCache);
 
 
 
 
33140
33141 assert( !pPager->aSavepoint && !pPager->pInJournal );
33142 assert( !isOpen(pPager->jfd) && !isOpen(pPager->sjfd) );
33143
33144 sqlite3_free(pPager);
@@ -33366,12 +33396,15 @@
33366 **
33367 ** Also, do not write out any page that has the PGHDR_DONT_WRITE flag
33368 ** set (set by sqlite3PagerDontWrite()).
33369 */
33370 if( pgno<=pPager->dbSize && 0==(pList->flags&PGHDR_DONT_WRITE) ){
33371 i64 offset = (pgno-1)*(i64)pPager->pageSize; /* Offset to write */
33372 char *pData = CODEC2(pPager, pList->pData, pgno, 6); /* Data to write */
 
 
 
33373
33374 /* Write out the page data. */
33375 rc = sqlite3OsWrite(pPager->fd, pData, pPager->pageSize, offset);
33376
33377 /* If page 1 was just written, update Pager.dbFileVers to match
@@ -33384,11 +33417,11 @@
33384 if( pgno>pPager->dbFileSize ){
33385 pPager->dbFileSize = pgno;
33386 }
33387
33388 /* Update any backup objects copying the contents of this pager. */
33389 sqlite3BackupUpdate(pPager->pBackup, pgno, (u8 *)pData);
33390
33391 PAGERTRACE(("STORE %d page %d hash(%08x)\n",
33392 PAGERID(pPager), pgno, pager_pagehash(pList)));
33393 IOTRACE(("PGOUT %p %d\n", pPager, pgno));
33394 PAGER_INCR(sqlite3_pager_writedb_count);
@@ -33422,12 +33455,13 @@
33422 int rc = SQLITE_OK;
33423 Pager *pPager = pPg->pPager;
33424 if( isOpen(pPager->sjfd) ){
33425 void *pData = pPg->pData;
33426 i64 offset = pPager->nSubRec*(4+pPager->pageSize);
33427 char *pData2 = CODEC2(pPager, pData, pPg->pgno, 7);
33428
 
33429 PAGERTRACE(("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno));
33430
33431 assert( pageInJournal(pPg) || pPg->pgno>pPager->dbOrigSize );
33432 rc = write32bits(pPager->sjfd, offset, pPg->pgno);
33433 if( rc==SQLITE_OK ){
@@ -33745,18 +33779,19 @@
33745 ** database is the same as a temp-file that is never written out to
33746 ** disk and uses an in-memory rollback journal.
33747 */
33748 tempFile = 1;
33749 pPager->state = PAGER_EXCLUSIVE;
 
33750 }
33751
33752 /* The following call to PagerSetPagesize() serves to set the value of
33753 ** Pager.pageSize and to allocate the Pager.pTmpSpace buffer.
33754 */
33755 if( rc==SQLITE_OK ){
33756 assert( pPager->memDb==0 );
33757 rc = sqlite3PagerSetPagesize(pPager, &szPageDflt);
33758 testcase( rc!=SQLITE_OK );
33759 }
33760
33761 /* If an error occurred in either of the blocks above, free the
33762 ** Pager structure and close the file.
@@ -33767,10 +33802,11 @@
33767 sqlite3_free(pPager);
33768 return rc;
33769 }
33770
33771 /* Initialize the PCache object. */
 
33772 nExtra = ROUND8(nExtra);
33773 sqlite3PcacheOpen(szPageDflt, nExtra, !memDb,
33774 !memDb?pagerStress:0, (void *)pPager, pPager->pPCache);
33775
33776 PAGERTRACE(("OPEN %d %s\n", FILEHANDLEID(pPager->fd), pPager->zFilename));
@@ -33802,11 +33838,11 @@
33802 pPager->fullSync = pPager->noSync ?0:1;
33803 pPager->sync_flags = SQLITE_SYNC_NORMAL;
33804 /* pPager->pFirst = 0; */
33805 /* pPager->pFirstSynced = 0; */
33806 /* pPager->pLast = 0; */
33807 pPager->nExtra = nExtra;
33808 pPager->journalSizeLimit = SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT;
33809 assert( isOpen(pPager->fd) || tempFile );
33810 setSectorSize(pPager);
33811 if( memDb ){
33812 pPager->journalMode = PAGER_JOURNALMODE_MEMORY;
@@ -33966,11 +34002,11 @@
33966 }
33967 if( pgno==1 ){
33968 u8 *dbFileVers = &((u8*)pPg->pData)[24];
33969 memcpy(&pPager->dbFileVers, dbFileVers, sizeof(pPager->dbFileVers));
33970 }
33971 CODEC1(pPager, pPg->pData, pgno, 3);
33972
33973 PAGER_INCR(sqlite3_pager_readdb_count);
33974 PAGER_INCR(pPager->nRead);
33975 IOTRACE(("PGIN %p %d\n", pPager, pgno));
33976 PAGERTRACE(("FETCH %d page %d hash(%08x)\n",
@@ -34011,19 +34047,17 @@
34011 */
34012 static int pagerSharedLock(Pager *pPager){
34013 int rc = SQLITE_OK; /* Return code */
34014 int isErrorReset = 0; /* True if recovering from error state */
34015
34016 /* If this database is opened for exclusive access, has no outstanding
34017 ** page references and is in an error-state, this is a chance to clear
34018 ** the error. Discard the contents of the pager-cache and treat any
34019 ** open journal file as a hot-journal.
34020 */
34021 if( !MEMDB && pPager->exclusiveMode
34022 && sqlite3PcacheRefCount(pPager->pPCache)==0 && pPager->errCode
34023 ){
34024 if( isOpen(pPager->jfd) ){
34025 isErrorReset = 1;
34026 }
34027 pPager->errCode = SQLITE_OK;
34028 pager_reset(pPager);
34029 }
@@ -34102,13 +34136,16 @@
34102 if( rc==SQLITE_OK && fout&SQLITE_OPEN_READONLY ){
34103 rc = SQLITE_CANTOPEN;
34104 sqlite3OsClose(pPager->jfd);
34105 }
34106 }else{
34107 /* If the journal does not exist, that means some other process
34108 ** has already rolled it back */
34109 rc = SQLITE_BUSY;
 
 
 
34110 }
34111 }
34112 }
34113 if( rc!=SQLITE_OK ){
34114 goto failed;
@@ -34123,14 +34160,16 @@
34123 /* Playback and delete the journal. Drop the database write
34124 ** lock and reacquire the read lock. Purge the cache before
34125 ** playing back the hot-journal so that we don't end up with
34126 ** an inconsistent cache.
34127 */
34128 rc = pager_playback(pPager, 1);
34129 if( rc!=SQLITE_OK ){
34130 rc = pager_error(pPager, rc);
34131 goto failed;
 
 
34132 }
34133 assert( (pPager->state==PAGER_SHARED)
34134 || (pPager->exclusiveMode && pPager->state>PAGER_SHARED)
34135 );
34136 }
@@ -34306,10 +34345,11 @@
34306 }
34307 assert( pPager->state!=PAGER_UNLOCK );
34308
34309 rc = sqlite3PcacheFetch(pPager->pPCache, pgno, 1, &pPg);
34310 if( rc!=SQLITE_OK ){
 
34311 return rc;
34312 }
34313 assert( pPg->pgno==pgno );
34314 assert( pPg->pPager==pPager || pPg->pPager==0 );
34315 if( pPg->pPager==0 ){
@@ -34664,11 +34704,11 @@
34664
34665 /* We should never write to the journal file the page that
34666 ** contains the database locks. The following assert verifies
34667 ** that we do not. */
34668 assert( pPg->pgno!=PAGER_MJ_PGNO(pPager) );
34669 pData2 = CODEC2(pPager, pData, pPg->pgno, 7);
34670 cksum = pager_cksum(pPager, (u8*)pData2);
34671 rc = write32bits(pPager->jfd, pPager->journalOff, pPg->pgno);
34672 if( rc==SQLITE_OK ){
34673 rc = sqlite3OsWrite(pPager->jfd, pData2, pPager->pageSize,
34674 pPager->journalOff + 4);
@@ -35515,19 +35555,28 @@
35515 return pPager->noSync;
35516 }
35517
35518 #ifdef SQLITE_HAS_CODEC
35519 /*
35520 ** Set the codec for this pager
35521 */
35522 SQLITE_PRIVATE void sqlite3PagerSetCodec(
35523 Pager *pPager,
35524 void *(*xCodec)(void*,void*,Pgno,int),
35525 void *pCodecArg
 
 
35526 ){
 
35527 pPager->xCodec = xCodec;
35528 pPager->pCodecArg = pCodecArg;
 
 
 
 
 
 
35529 }
35530 #endif
35531
35532 #ifndef SQLITE_OMIT_AUTOVACUUM
35533 /*
@@ -35815,11 +35864,11 @@
35815 ** May you do good and not evil.
35816 ** May you find forgiveness for yourself and forgive others.
35817 ** May you share freely, never taking more than you give.
35818 **
35819 *************************************************************************
35820 ** $Id: btreeInt.h,v 1.46 2009/03/20 14:18:52 danielk1977 Exp $
35821 **
35822 ** This file implements a external (disk-based) database using BTrees.
35823 ** For a detailed discussion of BTrees, refer to
35824 **
35825 ** Donald E. Knuth, THE ART OF COMPUTER PROGRAMMING, Volume 3:
@@ -35876,10 +35925,21 @@
35876 ** 24 4 File change counter
35877 ** 28 4 Reserved for future use
35878 ** 32 4 First freelist page
35879 ** 36 4 Number of freelist pages in the file
35880 ** 40 60 15 4-byte meta values passed to higher layers
 
 
 
 
 
 
 
 
 
 
 
35881 **
35882 ** All of the integer values are big-endian (most significant byte first).
35883 **
35884 ** The file change counter is incremented when the database is changed
35885 ** This counter allows other processes to know when the file has changed
@@ -36441,13 +36501,16 @@
36441 SQLITE_PRIVATE int sqlite3BtreeGetPage(BtShared*, Pgno, MemPage**, int);
36442 SQLITE_PRIVATE int sqlite3BtreeInitPage(MemPage *pPage);
36443 SQLITE_PRIVATE void sqlite3BtreeParseCellPtr(MemPage*, u8*, CellInfo*);
36444 SQLITE_PRIVATE void sqlite3BtreeParseCell(MemPage*, int, CellInfo*);
36445 SQLITE_PRIVATE int sqlite3BtreeRestoreCursorPosition(BtCursor *pCur);
 
 
 
36446 SQLITE_PRIVATE void sqlite3BtreeGetTempCursor(BtCursor *pCur, BtCursor *pTempCur);
36447 SQLITE_PRIVATE void sqlite3BtreeReleaseTempCursor(BtCursor *pCur);
36448 SQLITE_PRIVATE void sqlite3BtreeMoveToParent(BtCursor *pCur);
36449
36450 /************** End of btreeInt.h ********************************************/
36451 /************** Continuing where we left off in btmutex.c ********************/
36452 #ifndef SQLITE_OMIT_SHARED_CACHE
36453 #if SQLITE_THREADSAFE
@@ -36795,11 +36858,11 @@
36795 ** May you do good and not evil.
36796 ** May you find forgiveness for yourself and forgive others.
36797 ** May you share freely, never taking more than you give.
36798 **
36799 *************************************************************************
36800 ** $Id: btree.c,v 1.619 2009/06/05 18:44:15 drh Exp $
36801 **
36802 ** This file implements a external (disk-based) database using BTrees.
36803 ** See the header comment on "btreeInt.h" for additional information.
36804 ** Including a description of file format and an overview of operation.
36805 */
@@ -36813,11 +36876,11 @@
36813 /*
36814 ** Set this global variable to 1 to enable tracing using the TRACE
36815 ** macro.
36816 */
36817 #if 0
36818 int sqlite3BtreeTrace=0; /* True to enable tracing */
36819 # define TRACE(X) if(sqlite3BtreeTrace){printf X;fflush(stdout);}
36820 #else
36821 # define TRACE(X)
36822 #endif
36823
@@ -37423,11 +37486,10 @@
37423 }
37424
37425 #else /* if defined SQLITE_OMIT_AUTOVACUUM */
37426 #define ptrmapPut(w,x,y,z) SQLITE_OK
37427 #define ptrmapGet(w,x,y,z) SQLITE_OK
37428 #define ptrmapPutOvfl(y,z) SQLITE_OK
37429 #endif
37430
37431 /*
37432 ** Given a btree page and a cell index (0 means the first cell on
37433 ** the page, 1 means the second cell, and so forth) return a pointer
@@ -37588,11 +37650,11 @@
37588 if( nSize>pPage->maxLocal ){
37589 nSize = minLocal;
37590 }
37591 nSize += 4;
37592 }
37593 nSize += (pIter - pCell);
37594
37595 /* The minimum size of any cell is 4 bytes. */
37596 if( nSize<4 ){
37597 nSize = 4;
37598 }
@@ -37615,27 +37677,16 @@
37615 static int ptrmapPutOvflPtr(MemPage *pPage, u8 *pCell){
37616 CellInfo info;
37617 assert( pCell!=0 );
37618 sqlite3BtreeParseCellPtr(pPage, pCell, &info);
37619 assert( (info.nData+(pPage->intKey?0:info.nKey))==info.nPayload );
37620 if( (info.nData+(pPage->intKey?0:info.nKey))>info.nLocal ){
37621 Pgno ovfl = get4byte(&pCell[info.iOverflow]);
37622 return ptrmapPut(pPage->pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno);
37623 }
37624 return SQLITE_OK;
37625 }
37626 /*
37627 ** If the cell with index iCell on page pPage contains a pointer
37628 ** to an overflow page, insert an entry into the pointer-map
37629 ** for the overflow page.
37630 */
37631 static int ptrmapPutOvfl(MemPage *pPage, int iCell){
37632 u8 *pCell;
37633 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
37634 pCell = findOverflowCell(pPage, iCell);
37635 return ptrmapPutOvflPtr(pPage, pCell);
37636 }
37637 #endif
37638
37639
37640 /*
37641 ** Defragment the page given. All Cells are moved to the
@@ -37938,11 +37989,11 @@
37938 **
37939 ** The following block of code checks early to see if a cell extends
37940 ** past the end of a page boundary and causes SQLITE_CORRUPT to be
37941 ** returned if it does.
37942 */
37943 #if defined(SQLITE_OVERREAD_CHECK) || 1
37944 {
37945 int iCellFirst; /* First allowable cell index */
37946 int iCellLast; /* Last possible cell index */
37947 int i; /* Index into the cell pointer array */
37948 int sz; /* Size of a cell */
@@ -37976,11 +38027,11 @@
37976 size = get2byte(&data[pc+2]);
37977 if( next>0 && next<=pc+size+3 ){
37978 /* Free blocks must be in accending order */
37979 return SQLITE_CORRUPT_BKPT;
37980 }
37981 nFree += size;
37982 pc = next;
37983 }
37984
37985 /* At this point, nFree contains the sum of the offset to the start
37986 ** of the cell-content area plus the number of free bytes within
@@ -38386,11 +38437,11 @@
38386 #ifndef SQLITE_OMIT_AUTOVACUUM
38387 pBt->autoVacuum = (get4byte(&zDbHeader[36 + 4*4])?1:0);
38388 pBt->incrVacuum = (get4byte(&zDbHeader[36 + 7*4])?1:0);
38389 #endif
38390 }
38391 rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize);
38392 if( rc ) goto btree_open_out;
38393 pBt->usableSize = pBt->pageSize - nReserve;
38394 assert( (pBt->pageSize & 7)==0 ); /* 8-byte alignment of pageSize */
38395
38396 #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
@@ -38677,12 +38728,12 @@
38677 ((pageSize-1)&pageSize)==0 ){
38678 assert( (pageSize & 7)==0 );
38679 assert( !pBt->pPage1 && !pBt->pCursor );
38680 pBt->pageSize = (u16)pageSize;
38681 freeTempSpace(pBt);
38682 rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize);
38683 }
 
38684 pBt->usableSize = pBt->pageSize - (u16)nReserve;
38685 if( iFix ) pBt->pageSizeFixed = 1;
38686 sqlite3BtreeLeave(p);
38687 return rc;
38688 }
@@ -38833,15 +38884,16 @@
38833 */
38834 releasePage(pPage1);
38835 pBt->usableSize = (u16)usableSize;
38836 pBt->pageSize = (u16)pageSize;
38837 freeTempSpace(pBt);
38838 rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize);
 
38839 if( rc ) goto page1_init_failed;
38840 return SQLITE_OK;
38841 }
38842 if( usableSize<500 ){
38843 goto page1_init_failed;
38844 }
38845 pBt->pageSize = (u16)pageSize;
38846 pBt->usableSize = (u16)usableSize;
38847 #ifndef SQLITE_OMIT_AUTOVACUUM
@@ -39494,11 +39546,13 @@
39494
39495 assert( nRef==sqlite3PagerRefcount(pPager) );
39496 return rc;
39497 }
39498
39499 #endif /* ifndef SQLITE_OMIT_AUTOVACUUM */
 
 
39500
39501 /*
39502 ** This routine does the first phase of a two-phase commit. This routine
39503 ** causes a rollback journal to be created (if it does not already exist)
39504 ** and populated with enough information so that if a power loss occurs
@@ -39991,10 +40045,11 @@
39991 sqlite3BtreeLeave(pBtree);
39992 }
39993 return SQLITE_OK;
39994 }
39995
 
39996 /*
39997 ** Make a temporary cursor by filling in the fields of pTempCur.
39998 ** The temporary cursor is not on the cursor list for the Btree.
39999 */
40000 SQLITE_PRIVATE void sqlite3BtreeGetTempCursor(BtCursor *pCur, BtCursor *pTempCur){
@@ -40006,11 +40061,13 @@
40006 for(i=0; i<=pTempCur->iPage; i++){
40007 sqlite3PagerRef(pTempCur->apPage[i]->pDbPage);
40008 }
40009 assert( pTempCur->pKey==0 );
40010 }
 
40011
 
40012 /*
40013 ** Delete a temporary cursor such as was made by the CreateTemporaryCursor()
40014 ** function above.
40015 */
40016 SQLITE_PRIVATE void sqlite3BtreeReleaseTempCursor(BtCursor *pCur){
@@ -40019,12 +40076,11 @@
40019 for(i=0; i<=pCur->iPage; i++){
40020 sqlite3PagerUnref(pCur->apPage[i]->pDbPage);
40021 }
40022 sqlite3_free(pCur->pKey);
40023 }
40024
40025
40026
40027 /*
40028 ** Make sure the BtCursor* given in the argument has a valid
40029 ** BtCursor.info structure. If it is not already valid, call
40030 ** sqlite3BtreeParseCell() to fill it in.
@@ -41182,11 +41238,11 @@
41182 u8 exact
41183 ){
41184 MemPage *pPage1;
41185 int rc;
41186 u32 n; /* Number of pages on the freelist */
41187 int k; /* Number of leaves on the trunk of the freelist */
41188 MemPage *pTrunk = 0;
41189 MemPage *pPrevTrunk = 0;
41190 Pgno mxPage; /* Total size of the database file */
41191
41192 assert( sqlite3_mutex_held(pBt->mutex) );
@@ -41260,11 +41316,11 @@
41260 *pPgno = iTrunk;
41261 memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4);
41262 *ppPage = pTrunk;
41263 pTrunk = 0;
41264 TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1));
41265 }else if( k>pBt->usableSize/4 - 2 ){
41266 /* Value of k is out of range. Database corruption */
41267 rc = SQLITE_CORRUPT_BKPT;
41268 goto end_allocate_page;
41269 #ifndef SQLITE_OMIT_AUTOVACUUM
41270 }else if( searchList && nearby==iTrunk ){
@@ -41320,21 +41376,22 @@
41320 }
41321 }
41322 pTrunk = 0;
41323 TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1));
41324 #endif
41325 }else{
41326 /* Extract a leaf from the trunk */
41327 int closest;
41328 Pgno iPage;
41329 unsigned char *aData = pTrunk->aData;
41330 rc = sqlite3PagerWrite(pTrunk->pDbPage);
41331 if( rc ){
41332 goto end_allocate_page;
41333 }
41334 if( nearby>0 ){
41335 int i, dist;
 
41336 closest = 0;
41337 dist = get4byte(&aData[8]) - nearby;
41338 if( dist<0 ) dist = -dist;
41339 for(i=1; i<k; i++){
41340 int d2 = get4byte(&aData[8+i*4]) - nearby;
@@ -41356,11 +41413,11 @@
41356 if( !searchList || iPage==nearby ){
41357 int noContent;
41358 Pgno nPage;
41359 *pPgno = iPage;
41360 nPage = pagerPagecount(pBt);
41361 if( *pPgno>nPage ){
41362 /* Free page off the end of the file */
41363 rc = SQLITE_CORRUPT_BKPT;
41364 goto end_allocate_page;
41365 }
41366 TRACE(("ALLOCATE: %d was leaf %d of %d on trunk %d"
@@ -41434,10 +41491,12 @@
41434 if( sqlite3PagerPageRefcount((*ppPage)->pDbPage)>1 ){
41435 releasePage(*ppPage);
41436 return SQLITE_CORRUPT_BKPT;
41437 }
41438 (*ppPage)->isInit = 0;
 
 
41439 }
41440 return rc;
41441 }
41442
41443 /*
@@ -41844,11 +41903,11 @@
41844 MemPage *pPage, /* Page into which we are copying */
41845 int i, /* New cell becomes the i-th cell of the page */
41846 u8 *pCell, /* Content of the new cell */
41847 int sz, /* Bytes of content in pCell */
41848 u8 *pTemp, /* Temp storage space for pCell, if needed */
41849 u8 nSkip /* Do not write the first nSkip bytes of the cell */
41850 ){
41851 int idx; /* Where to write new cell content in data[] */
41852 int j; /* Loop counter */
41853 int top; /* First byte of content for any cell in data[] */
41854 int end; /* First byte past the last cell pointer in data[] */
@@ -41855,10 +41914,12 @@
41855 int ins; /* Index in data[] where new cell pointer is inserted */
41856 int hdr; /* Offset into data[] of the page header */
41857 int cellOffset; /* Address of first cell pointer in data[] */
41858 u8 *data; /* The content of the whole page */
41859 u8 *ptr; /* Used for moving information around in data[] */
 
 
41860
41861 assert( i>=0 && i<=pPage->nCell+pPage->nOverflow );
41862 assert( pPage->nCell<=MX_CELL(pPage->pBt) && MX_CELL(pPage->pBt)<=5460 );
41863 assert( pPage->nOverflow<=ArraySize(pPage->aOvfl) );
41864 assert( sz==cellSizePtr(pPage, pCell) );
@@ -41866,15 +41927,17 @@
41866 if( pPage->nOverflow || sz+2>pPage->nFree ){
41867 if( pTemp ){
41868 memcpy(pTemp+nSkip, pCell+nSkip, sz-nSkip);
41869 pCell = pTemp;
41870 }
 
 
 
41871 j = pPage->nOverflow++;
41872 assert( j<(int)(sizeof(pPage->aOvfl)/sizeof(pPage->aOvfl[0])) );
41873 pPage->aOvfl[j].pCell = pCell;
41874 pPage->aOvfl[j].idx = (u16)i;
41875 pPage->nFree = 0;
41876 }else{
41877 int rc = sqlite3PagerWrite(pPage->pDbPage);
41878 if( rc!=SQLITE_OK ){
41879 return rc;
41880 }
@@ -41900,10 +41963,13 @@
41900 return SQLITE_CORRUPT_BKPT;
41901 }
41902 pPage->nCell++;
41903 pPage->nFree -= 2;
41904 memcpy(&data[idx+nSkip], pCell+nSkip, sz-nSkip);
 
 
 
41905 for(j=end-2, ptr=&data[j]; j>ins; j-=2, ptr-=2){
41906 ptr[0] = ptr[-2];
41907 ptr[1] = ptr[-1];
41908 }
41909 put2byte(&data[ins], idx);
@@ -41911,18 +41977,11 @@
41911 #ifndef SQLITE_OMIT_AUTOVACUUM
41912 if( pPage->pBt->autoVacuum ){
41913 /* The cell may contain a pointer to an overflow page. If so, write
41914 ** the entry for the overflow page into the pointer map.
41915 */
41916 CellInfo info;
41917 sqlite3BtreeParseCellPtr(pPage, pCell, &info);
41918 assert( (info.nData+(pPage->intKey?0:info.nKey))==info.nPayload );
41919 if( info.iOverflow ){
41920 Pgno pgnoOvfl = get4byte(&pCell[info.iOverflow]);
41921 rc = ptrmapPut(pPage->pBt, pgnoOvfl, PTRMAP_OVERFLOW1, pPage->pgno);
41922 if( rc!=SQLITE_OK ) return rc;
41923 }
41924 }
41925 #endif
41926 }
41927
41928 return SQLITE_OK;
@@ -41981,12 +42040,10 @@
41981 ** The value of NN appears to give the best results overall.
41982 */
41983 #define NN 1 /* Number of neighbors on either side of pPage */
41984 #define NB (NN*2+1) /* Total pages involved in the balance */
41985
41986 /* Forward reference */
41987 static int balance(BtCursor*, int);
41988
41989 #ifndef SQLITE_OMIT_QUICKBALANCE
41990 /*
41991 ** This version of balance() handles the common special case where
41992 ** a new entry is being inserted on the extreme right-end of the
@@ -42001,42 +42058,62 @@
42001 ** fill up. On average.
42002 **
42003 ** pPage is the leaf page which is the right-most page in the tree.
42004 ** pParent is its parent. pPage must have a single overflow entry
42005 ** which is also the right-most entry on the page.
 
 
 
 
 
 
42006 */
42007 static int balance_quick(BtCursor *pCur){
42008 MemPage *const pPage = pCur->apPage[pCur->iPage];
42009 BtShared *const pBt = pCur->pBt;
42010 MemPage *pNew = 0; /* Newly allocated page */
42011 int rc; /* Return Code */
42012 Pgno pgnoNew; /* Page number of pNew */
42013
42014 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
 
 
 
42015 if( pPage->nCell<=0 ) return SQLITE_CORRUPT_BKPT;
42016
42017 /* Allocate a new page. This page will become the right-sibling of pPage */
 
 
 
42018 rc = allocateBtreePage(pBt, &pNew, &pgnoNew, 0, 0);
42019
42020 if( rc==SQLITE_OK ){
42021 /* The parentCell buffer is used to store a temporary copy of the divider
42022 ** cell that will be inserted into pParent. Such a cell consists of a 4
42023 ** byte page number followed by a variable length integer. In other
42024 ** words, at most 13 bytes. Hence the parentCell buffer must be at
42025 ** least 13 bytes in size.
42026 */
42027 MemPage * const pParent = pCur->apPage[pCur->iPage-1];
42028 u8 parentCell[13];
42029 u8 *pOut = &parentCell[4];
42030 u8 *pCell = pPage->aOvfl[0].pCell;
42031 u16 szCell = cellSizePtr(pPage, pCell);
42032 u8 *pStop;
42033
42034 assert( sqlite3PagerIswriteable(pNew->pDbPage) );
42035 zeroPage(pNew, pPage->aData[0]);
 
42036 assemblePage(pNew, 1, &pCell, &szCell);
42037 pPage->nOverflow = 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42038
42039 /* Create a divider cell to insert into pParent. The divider cell
42040 ** consists of a 4-byte page number (the page number of pPage) and
42041 ** a variable length key value (which must be the same value as the
42042 ** largest key on pPage).
@@ -42045,278 +42122,263 @@
42045 ** cell on pPage. The first two fields of this cell are the
42046 ** record-length (a variable length integer at most 32-bits in size)
42047 ** and the key value (a variable length integer, may have any value).
42048 ** The first of the while(...) loops below skips over the record-length
42049 ** field. The second while(...) loop copies the key value from the
42050 ** cell on pPage into the parentCell buffer.
42051 */
42052 put4byte(parentCell, pPage->pgno);
42053 pCell = findCell(pPage, pPage->nCell-1);
42054 pStop = &pCell[9];
42055 while( (*(pCell++)&0x80) && pCell<pStop );
42056 pStop = &pCell[9];
42057 while( ((*(pOut++) = *(pCell++))&0x80) && pCell<pStop );
42058
42059 /* Insert the new divider cell into pParent */
42060 insertCell(pParent, pParent->nCell, parentCell, pOut-parentCell, 0, 0);
42061
42062 /* Set the right-child pointer of pParent to point to the new page. */
42063 put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew);
42064
42065 /* If this is an auto-vacuum database, update the pointer map
42066 ** with entries for the new page, and any pointer from the
42067 ** cell on the page to an overflow page.
42068 */
42069 if( ISAUTOVACUUM ){
42070 rc = ptrmapPut(pBt, pgnoNew, PTRMAP_BTREE, pParent->pgno);
42071 if( rc==SQLITE_OK ){
42072 rc = ptrmapPutOvfl(pNew, 0);
42073 }
42074 }
42075
42076 /* Release the reference to the new page. */
42077 releasePage(pNew);
42078 }
42079
42080 /* At this point the pPage->nFree variable is not set correctly with
42081 ** respect to the content of the page (because it was set to 0 by
42082 ** insertCell). So call sqlite3BtreeInitPage() to make sure it is
42083 ** correct.
42084 **
42085 ** This has to be done even if an error will be returned. Normally, if
42086 ** an error occurs during tree balancing, the contents of MemPage are
42087 ** not important, as they will be recalculated when the page is rolled
42088 ** back. But here, in balance_quick(), it is possible that pPage has
42089 ** not yet been marked dirty or written into the journal file. Therefore
42090 ** it will not be rolled back and so it is important to make sure that
42091 ** the page data and contents of MemPage are consistent.
42092 */
42093 pPage->isInit = 0;
42094 sqlite3BtreeInitPage(pPage);
42095 assert( pPage->nOverflow==0 );
42096
42097 /* If everything else succeeded, balance the parent page, in
42098 ** case the divider cell inserted caused it to become overfull.
42099 */
42100 if( rc==SQLITE_OK ){
42101 releasePage(pPage);
42102 pCur->iPage--;
42103 rc = balance(pCur, 0);
42104 }
42105 return rc;
42106 }
42107 #endif /* SQLITE_OMIT_QUICKBALANCE */
42108
 
42109 /*
42110 ** This routine redistributes Cells on pPage and up to NN*2 siblings
42111 ** of pPage so that all pages have about the same amount of free space.
42112 ** Usually NN siblings on either side of pPage is used in the balancing,
42113 ** though more siblings might come from one side if pPage is the first
42114 ** or last child of its parent. If pPage has fewer than 2*NN siblings
42115 ** (something which can only happen if pPage is the root page or a
42116 ** child of root) then all available siblings participate in the balancing.
42117 **
42118 ** The number of siblings of pPage might be increased or decreased by one or
42119 ** two in an effort to keep pages nearly full but not over full. The root page
42120 ** is special and is allowed to be nearly empty. If pPage is
42121 ** the root page, then the depth of the tree might be increased
42122 ** or decreased by one, as necessary, to keep the root page from being
42123 ** overfull or completely empty.
42124 **
42125 ** Note that when this routine is called, some of the Cells on pPage
42126 ** might not actually be stored in pPage->aData[]. This can happen
42127 ** if the page is overfull. Part of the job of this routine is to
42128 ** make sure all Cells for pPage once again fit in pPage->aData[].
42129 **
42130 ** In the course of balancing the siblings of pPage, the parent of pPage
42131 ** might become overfull or underfull. If that happens, then this routine
42132 ** is called recursively on the parent.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42133 **
42134 ** If this routine fails for any reason, it might leave the database
42135 ** in a corrupted state. So if this routine fails, the database should
42136 ** be rolled back.
 
 
 
 
 
 
 
 
 
 
 
 
42137 */
42138 static int balance_nonroot(BtCursor *pCur){
42139 MemPage *pPage; /* The over or underfull page to balance */
42140 MemPage *pParent; /* The parent of pPage */
 
 
42141 BtShared *pBt; /* The whole database */
42142 int nCell = 0; /* Number of cells in apCell[] */
42143 int nMaxCells = 0; /* Allocated size of apCell, szCell, aFrom. */
42144 int nOld = 0; /* Number of pages in apOld[] */
42145 int nNew = 0; /* Number of pages in apNew[] */
42146 int nDiv; /* Number of cells in apDiv[] */
42147 int i, j, k; /* Loop counters */
42148 int idx; /* Index of pPage in pParent->aCell[] */
42149 int nxDiv; /* Next divider slot in pParent->aCell[] */
42150 int rc; /* The return code */
42151 int leafCorrection; /* 4 if pPage is a leaf. 0 if not */
42152 int leafData; /* True if pPage is a leaf of a LEAFDATA tree */
42153 int usableSpace; /* Bytes in pPage beyond the header */
42154 int pageFlags; /* Value of pPage->aData[0] */
42155 int subtotal; /* Subtotal of bytes in cells on one page */
42156 int iSpace1 = 0; /* First unused byte of aSpace1[] */
42157 int iSpace2 = 0; /* First unused byte of aSpace2[] */
42158 int szScratch; /* Size of scratch memory requested */
42159 MemPage *apOld[NB]; /* pPage and up to two siblings */
42160 Pgno pgnoOld[NB]; /* Page numbers for each page in apOld[] */
42161 MemPage *apCopy[NB]; /* Private copies of apOld[] pages */
42162 MemPage *apNew[NB+2]; /* pPage and up to NB siblings after balancing */
42163 Pgno pgnoNew[NB+2]; /* Page numbers for each page in apNew[] */
42164 u8 *apDiv[NB]; /* Divider cells in pParent */
42165 int cntNew[NB+2]; /* Index in aCell[] of cell after i-th page */
42166 int szNew[NB+2]; /* Combined size of cells place on i-th page */
42167 u8 **apCell = 0; /* All cells begin balanced */
42168 u16 *szCell; /* Local size of all cells in apCell[] */
42169 u8 *aCopy[NB]; /* Space for holding data of apCopy[] */
42170 u8 *aSpace1; /* Space for copies of dividers cells before balance */
42171 u8 *aSpace2 = 0; /* Space for overflow dividers cells after balance */
42172 u8 *aFrom = 0;
42173
42174 pPage = pCur->apPage[pCur->iPage];
42175 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
42176 VVA_ONLY( pCur->pagesShuffled = 1 );
42177
42178 /*
42179 ** Find the parent page.
42180 */
42181 assert( pCur->iPage>0 );
42182 assert( pPage->isInit );
42183 assert( sqlite3PagerIswriteable(pPage->pDbPage) || pPage->nOverflow==1 );
42184 pBt = pPage->pBt;
42185 pParent = pCur->apPage[pCur->iPage-1];
42186 assert( pParent );
42187 if( SQLITE_OK!=(rc = sqlite3PagerWrite(pParent->pDbPage)) ){
42188 goto balance_cleanup;
42189 }
42190
42191 TRACE(("BALANCE: begin page %d child of %d\n", pPage->pgno, pParent->pgno));
42192
42193 #ifndef SQLITE_OMIT_QUICKBALANCE
42194 /*
42195 ** A special case: If a new entry has just been inserted into a
42196 ** table (that is, a btree with integer keys and all data at the leaves)
42197 ** and the new entry is the right-most entry in the tree (it has the
42198 ** largest key) then use the special balance_quick() routine for
42199 ** balancing. balance_quick() is much faster and results in a tighter
42200 ** packing of data in the common case.
42201 */
42202 if( pPage->leaf &&
42203 pPage->intKey &&
42204 pPage->nOverflow==1 &&
42205 pPage->aOvfl[0].idx==pPage->nCell &&
42206 pParent->pgno!=1 &&
42207 get4byte(&pParent->aData[pParent->hdrOffset+8])==pPage->pgno
42208 ){
42209 assert( pPage->intKey );
42210 /*
42211 ** TODO: Check the siblings to the left of pPage. It may be that
42212 ** they are not full and no new page is required.
42213 */
42214 return balance_quick(pCur);
42215 }
42216 #endif
42217
42218 if( SQLITE_OK!=(rc = sqlite3PagerWrite(pPage->pDbPage)) ){
42219 goto balance_cleanup;
42220 }
42221
42222 /*
42223 ** Find the cell in the parent page whose left child points back
42224 ** to pPage. The "idx" variable is the index of that cell. If pPage
42225 ** is the rightmost child of pParent then set idx to pParent->nCell
42226 */
42227 idx = pCur->aiIdx[pCur->iPage-1];
42228 assertParentIndex(pParent, idx, pPage->pgno);
42229
42230 /*
42231 ** Find sibling pages to pPage and the cells in pParent that divide
42232 ** the siblings. An attempt is made to find NN siblings on either
42233 ** side of pPage. More siblings are taken from one side, however, if
42234 ** pPage there are fewer than NN siblings on the other side. If pParent
42235 ** has NB or fewer children then all children of pParent are taken.
42236 */
42237 nxDiv = idx - NN;
42238 if( nxDiv + NB > pParent->nCell ){
42239 nxDiv = pParent->nCell - NB + 1;
42240 }
42241 if( nxDiv<0 ){
42242 nxDiv = 0;
42243 }
42244 nDiv = 0;
42245 for(i=0, k=nxDiv; i<NB; i++, k++){
42246 if( k<pParent->nCell ){
42247 apDiv[i] = findCell(pParent, k);
42248 nDiv++;
42249 assert( !pParent->leaf );
42250 pgnoOld[i] = get4byte(apDiv[i]);
42251 }else if( k==pParent->nCell ){
42252 pgnoOld[i] = get4byte(&pParent->aData[pParent->hdrOffset+8]);
42253 }else{
42254 break;
42255 }
42256 rc = getAndInitPage(pBt, pgnoOld[i], &apOld[i]);
42257 if( rc ) goto balance_cleanup;
42258 /* apOld[i]->idxParent = k; */
42259 apCopy[i] = 0;
42260 assert( i==nOld );
42261 nOld++;
 
 
 
 
 
42262 nMaxCells += 1+apOld[i]->nCell+apOld[i]->nOverflow;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42263 }
42264
42265 /* Make nMaxCells a multiple of 4 in order to preserve 8-byte
42266 ** alignment */
42267 nMaxCells = (nMaxCells + 3)&~3;
42268
42269 /*
42270 ** Allocate space for memory structures
42271 */
 
42272 szScratch =
42273 nMaxCells*sizeof(u8*) /* apCell */
42274 + nMaxCells*sizeof(u16) /* szCell */
42275 + (ROUND8(sizeof(MemPage))+pBt->pageSize)*NB /* aCopy */
42276 + pBt->pageSize /* aSpace1 */
42277 + (ISAUTOVACUUM ? nMaxCells : 0); /* aFrom */
42278 apCell = sqlite3ScratchMalloc( szScratch );
42279 if( apCell==0 ){
42280 rc = SQLITE_NOMEM;
42281 goto balance_cleanup;
42282 }
42283 szCell = (u16*)&apCell[nMaxCells];
42284 aCopy[0] = (u8*)&szCell[nMaxCells];
42285 assert( EIGHT_BYTE_ALIGNMENT(aCopy[0]) );
42286 for(i=1; i<NB; i++){
42287 aCopy[i] = &aCopy[i-1][pBt->pageSize+ROUND8(sizeof(MemPage))];
42288 assert( ((aCopy[i] - (u8*)0) & 7)==0 ); /* 8-byte alignment required */
42289 }
42290 aSpace1 = &aCopy[NB-1][pBt->pageSize+ROUND8(sizeof(MemPage))];
42291 assert( EIGHT_BYTE_ALIGNMENT(aSpace1) );
42292 if( ISAUTOVACUUM ){
42293 aFrom = &aSpace1[pBt->pageSize];
42294 }
42295 aSpace2 = sqlite3PageMalloc(pBt->pageSize);
42296 if( aSpace2==0 ){
42297 rc = SQLITE_NOMEM;
42298 goto balance_cleanup;
42299 }
42300
42301 /*
42302 ** Make copies of the content of pPage and its siblings into aOld[].
42303 ** The rest of this function will use data from the copies rather
42304 ** that the original pages since the original pages will be in the
42305 ** process of being overwritten.
42306 */
42307 for(i=0; i<nOld; i++){
42308 MemPage *p = apCopy[i] = (MemPage*)aCopy[i];
42309 memcpy(p, apOld[i], sizeof(MemPage));
42310 p->aData = (void*)&p[1];
42311 memcpy(p->aData, apOld[i]->aData, pBt->pageSize);
42312 }
42313
42314 /*
42315 ** Load pointers to all cells on sibling pages and the divider cells
42316 ** into the local apCell[] array. Make copies of the divider cells
42317 ** into space obtained form aSpace1[] and remove the the divider Cells
42318 ** from pParent.
42319 **
42320 ** If the siblings are on leaf pages, then the child pointers of the
42321 ** divider cells are stripped from the cells before they are copied
42322 ** into aSpace1[]. In this way, all cells in apCell[] are without
@@ -42325,72 +42387,58 @@
42325 ** are alike.
42326 **
42327 ** leafCorrection: 4 if pPage is a leaf. 0 if pPage is not a leaf.
42328 ** leafData: 1 if pPage holds key+data and pParent holds only keys.
42329 */
42330 nCell = 0;
42331 leafCorrection = pPage->leaf*4;
42332 leafData = pPage->hasData;
42333 for(i=0; i<nOld; i++){
42334 MemPage *pOld = apCopy[i];
42335 int limit = pOld->nCell+pOld->nOverflow;
 
 
 
 
 
 
 
 
 
 
42336 for(j=0; j<limit; j++){
42337 assert( nCell<nMaxCells );
42338 apCell[nCell] = findOverflowCell(pOld, j);
42339 szCell[nCell] = cellSizePtr(pOld, apCell[nCell]);
42340 if( ISAUTOVACUUM ){
42341 int a;
42342 aFrom[nCell] = (u8)i; assert( i>=0 && i<6 );
42343 for(a=0; a<pOld->nOverflow; a++){
42344 if( pOld->aOvfl[a].pCell==apCell[nCell] ){
42345 aFrom[nCell] = 0xFF;
42346 break;
42347 }
42348 }
42349 }
42350 nCell++;
42351 }
42352 if( i<nOld-1 ){
42353 u16 sz = cellSizePtr(pParent, apDiv[i]);
42354 if( leafData ){
42355 /* With the LEAFDATA flag, pParent cells hold only INTKEYs that
42356 ** are duplicates of keys on the child pages. We need to remove
42357 ** the divider cells from pParent, but the dividers cells are not
42358 ** added to apCell[] because they are duplicates of child cells.
42359 */
42360 dropCell(pParent, nxDiv, sz);
42361 }else{
42362 u8 *pTemp;
42363 assert( nCell<nMaxCells );
42364 szCell[nCell] = sz;
42365 pTemp = &aSpace1[iSpace1];
42366 iSpace1 += sz;
42367 assert( sz<=pBt->pageSize/4 );
42368 assert( iSpace1<=pBt->pageSize );
42369 memcpy(pTemp, apDiv[i], sz);
42370 apCell[nCell] = pTemp+leafCorrection;
42371 if( ISAUTOVACUUM ){
42372 aFrom[nCell] = 0xFF;
42373 }
42374 dropCell(pParent, nxDiv, sz);
42375 assert( leafCorrection==0 || leafCorrection==4 );
42376 szCell[nCell] -= (u16)leafCorrection;
42377 assert( get4byte(pTemp)==pgnoOld[i] );
42378 if( !pOld->leaf ){
42379 assert( leafCorrection==0 );
42380 /* The right pointer of the child page pOld becomes the left
42381 ** pointer of the divider cell */
42382 memcpy(apCell[nCell], &pOld->aData[pOld->hdrOffset+8], 4);
42383 }else{
42384 assert( leafCorrection==4 );
42385 if( szCell[nCell]<4 ){
42386 /* Do not allow any cells smaller than 4 bytes. */
42387 szCell[nCell] = 4;
42388 }
42389 }
42390 nCell++;
42391 }
42392 }
42393 }
42394
42395 /*
42396 ** Figure out the number of pages needed to hold all nCell cells.
@@ -42416,10 +42464,11 @@
42416 szNew[k] = subtotal - szCell[i];
42417 cntNew[k] = i;
42418 if( leafData ){ i--; }
42419 subtotal = 0;
42420 k++;
 
42421 }
42422 }
42423 szNew[k] = subtotal;
42424 cntNew[k] = nCell;
42425 k++;
@@ -42459,30 +42508,46 @@
42459 ** a virtual root page. A virtual root page is when the real root
42460 ** page is page 1 and we are the only child of that page.
42461 */
42462 assert( cntNew[0]>0 || (pParent->pgno==1 && pParent->nCell==0) );
42463
 
 
 
 
 
 
42464 /*
42465 ** Allocate k new pages. Reuse old pages where possible.
42466 */
42467 assert( pPage->pgno>1 );
42468 pageFlags = pPage->aData[0];
 
 
 
42469 for(i=0; i<k; i++){
42470 MemPage *pNew;
42471 if( i<nOld ){
42472 pNew = apNew[i] = apOld[i];
42473 pgnoNew[i] = pgnoOld[i];
42474 apOld[i] = 0;
42475 rc = sqlite3PagerWrite(pNew->pDbPage);
42476 nNew++;
42477 if( rc ) goto balance_cleanup;
42478 }else{
42479 assert( i>0 );
42480 rc = allocateBtreePage(pBt, &pNew, &pgnoNew[i], pgnoNew[i-1], 0);
42481 if( rc ) goto balance_cleanup;
42482 apNew[i] = pNew;
42483 nNew++;
 
 
 
 
 
 
 
 
42484 }
42485 }
42486
42487 /* Free any old pages that were not reused as new pages.
42488 */
@@ -42507,38 +42572,36 @@
42507 **
42508 ** When NB==3, this one optimization makes the database
42509 ** about 25% faster for large insertions and deletions.
42510 */
42511 for(i=0; i<k-1; i++){
42512 int minV = pgnoNew[i];
42513 int minI = i;
42514 for(j=i+1; j<k; j++){
42515 if( pgnoNew[j]<(unsigned)minV ){
42516 minI = j;
42517 minV = pgnoNew[j];
42518 }
42519 }
42520 if( minI>i ){
42521 int t;
42522 MemPage *pT;
42523 t = pgnoNew[i];
42524 pT = apNew[i];
42525 pgnoNew[i] = pgnoNew[minI];
42526 apNew[i] = apNew[minI];
42527 pgnoNew[minI] = t;
42528 apNew[minI] = pT;
42529 }
42530 }
42531 TRACE(("BALANCE: old: %d %d %d new: %d(%d) %d(%d) %d(%d) %d(%d) %d(%d)\n",
42532 pgnoOld[0],
42533 nOld>=2 ? pgnoOld[1] : 0,
42534 nOld>=3 ? pgnoOld[2] : 0,
42535 pgnoNew[0], szNew[0],
42536 nNew>=2 ? pgnoNew[1] : 0, nNew>=2 ? szNew[1] : 0,
42537 nNew>=3 ? pgnoNew[2] : 0, nNew>=3 ? szNew[2] : 0,
42538 nNew>=4 ? pgnoNew[3] : 0, nNew>=4 ? szNew[3] : 0,
42539 nNew>=5 ? pgnoNew[4] : 0, nNew>=5 ? szNew[4] : 0));
42540
42541 /*
42542 ** Evenly distribute the data in apCell[] across the new pages.
42543 ** Insert divider cells into pParent as necessary.
42544 */
@@ -42545,36 +42608,15 @@
42545 j = 0;
42546 for(i=0; i<nNew; i++){
42547 /* Assemble the new sibling page. */
42548 MemPage *pNew = apNew[i];
42549 assert( j<nMaxCells );
42550 assert( pNew->pgno==pgnoNew[i] );
42551 zeroPage(pNew, pageFlags);
42552 assemblePage(pNew, cntNew[i]-j, &apCell[j], &szCell[j]);
42553 assert( pNew->nCell>0 || (nNew==1 && cntNew[0]==0) );
42554 assert( pNew->nOverflow==0 );
42555
42556 /* If this is an auto-vacuum database, update the pointer map entries
42557 ** that point to the siblings that were rearranged. These can be: left
42558 ** children of cells, the right-child of the page, or overflow pages
42559 ** pointed to by cells.
42560 */
42561 if( ISAUTOVACUUM ){
42562 for(k=j; k<cntNew[i]; k++){
42563 assert( k<nMaxCells );
42564 if( aFrom[k]==0xFF || apCopy[aFrom[k]]->pgno!=pNew->pgno ){
42565 rc = ptrmapPutOvfl(pNew, k-j);
42566 if( rc==SQLITE_OK && leafCorrection==0 ){
42567 rc = ptrmapPut(pBt, get4byte(apCell[k]), PTRMAP_BTREE, pNew->pgno);
42568 }
42569 if( rc!=SQLITE_OK ){
42570 goto balance_cleanup;
42571 }
42572 }
42573 }
42574 }
42575
42576 j = cntNew[i];
42577
42578 /* If the sibling page assembled above was not the right-most sibling,
42579 ** insert a divider cell into the parent page.
42580 */
@@ -42584,21 +42626,13 @@
42584 int sz;
42585
42586 assert( j<nMaxCells );
42587 pCell = apCell[j];
42588 sz = szCell[j] + leafCorrection;
42589 pTemp = &aSpace2[iSpace2];
42590 if( !pNew->leaf ){
42591 memcpy(&pNew->aData[8], pCell, 4);
42592 if( ISAUTOVACUUM
42593 && (aFrom[j]==0xFF || apCopy[aFrom[j]]->pgno!=pNew->pgno)
42594 ){
42595 rc = ptrmapPut(pBt, get4byte(pCell), PTRMAP_BTREE, pNew->pgno);
42596 if( rc!=SQLITE_OK ){
42597 goto balance_cleanup;
42598 }
42599 }
42600 }else if( leafData ){
42601 /* If the tree is a leaf-data tree, and the siblings are leaves,
42602 ** then there is no divider cell in apCell[]. Instead, the divider
42603 ** cell consists of the integer key for the right-most cell of
42604 ** the sibling-page assembled above only.
@@ -42605,14 +42639,11 @@
42605 */
42606 CellInfo info;
42607 j--;
42608 sqlite3BtreeParseCellPtr(pNew, apCell[j], &info);
42609 pCell = pTemp;
42610 rc = fillInCell(pParent, pCell, 0, info.nKey, 0, 0, 0, &sz);
42611 if( rc!=SQLITE_OK ){
42612 goto balance_cleanup;
42613 }
42614 pTemp = 0;
42615 }else{
42616 pCell -= 4;
42617 /* Obscure case for non-leaf-data trees: If the cell at pCell was
42618 ** previously stored on a leaf node, and its reported size was 4
@@ -42628,300 +42659,457 @@
42628 if( szCell[j]==4 ){
42629 assert(leafCorrection==4);
42630 sz = cellSizePtr(pParent, pCell);
42631 }
42632 }
42633 iSpace2 += sz;
42634 assert( sz<=pBt->pageSize/4 );
42635 assert( iSpace2<=pBt->pageSize );
42636 rc = insertCell(pParent, nxDiv, pCell, sz, pTemp, 4);
42637 if( rc!=SQLITE_OK ) goto balance_cleanup;
42638 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
42639 put4byte(findOverflowCell(pParent,nxDiv), pNew->pgno);
42640
42641 /* If this is an auto-vacuum database, and not a leaf-data tree,
42642 ** then update the pointer map with an entry for the overflow page
42643 ** that the cell just inserted points to (if any).
42644 */
42645 if( ISAUTOVACUUM && !leafData ){
42646 rc = ptrmapPutOvfl(pParent, nxDiv);
42647 if( rc!=SQLITE_OK ){
42648 goto balance_cleanup;
42649 }
42650 }
42651 j++;
42652 nxDiv++;
42653 }
42654
42655 /* Set the pointer-map entry for the new sibling page. */
42656 if( ISAUTOVACUUM ){
42657 rc = ptrmapPut(pBt, pNew->pgno, PTRMAP_BTREE, pParent->pgno);
42658 if( rc!=SQLITE_OK ){
42659 goto balance_cleanup;
42660 }
42661 }
42662 }
42663 assert( j==nCell );
42664 assert( nOld>0 );
42665 assert( nNew>0 );
42666 if( (pageFlags & PTF_LEAF)==0 ){
42667 u8 *zChild = &apCopy[nOld-1]->aData[8];
42668 memcpy(&apNew[nNew-1]->aData[8], zChild, 4);
42669 if( ISAUTOVACUUM ){
42670 rc = ptrmapPut(pBt, get4byte(zChild), PTRMAP_BTREE, apNew[nNew-1]->pgno);
42671 if( rc!=SQLITE_OK ){
42672 goto balance_cleanup;
42673 }
42674 }
42675 }
42676 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
42677 if( nxDiv==pParent->nCell+pParent->nOverflow ){
42678 /* Right-most sibling is the right-most child of pParent */
42679 put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew[nNew-1]);
42680 }else{
42681 /* Right-most sibling is the left child of the first entry in pParent
42682 ** past the right-most divider entry */
42683 put4byte(findOverflowCell(pParent, nxDiv), pgnoNew[nNew-1]);
42684 }
42685
42686 /*
42687 ** Balance the parent page. Note that the current page (pPage) might
42688 ** have been added to the freelist so it might no longer be initialized.
42689 ** But the parent page will always be initialized.
42690 */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42691 assert( pParent->isInit );
42692 sqlite3ScratchFree(apCell);
42693 apCell = 0;
42694 TRACE(("BALANCE: finished with %d: old=%d new=%d cells=%d\n",
42695 pPage->pgno, nOld, nNew, nCell));
42696 pPage->nOverflow = 0;
42697 releasePage(pPage);
42698 pCur->iPage--;
42699 rc = balance(pCur, 0);
42700
42701 /*
42702 ** Cleanup before returning.
42703 */
42704 balance_cleanup:
42705 sqlite3PageFree(aSpace2);
42706 sqlite3ScratchFree(apCell);
42707 for(i=0; i<nOld; i++){
42708 releasePage(apOld[i]);
42709 }
42710 for(i=0; i<nNew; i++){
42711 releasePage(apNew[i]);
42712 }
42713 pCur->apPage[pCur->iPage]->nOverflow = 0;
42714
42715 return rc;
42716 }
42717
42718 /*
42719 ** This routine is called for the root page of a btree when the root
42720 ** page contains no cells. This is an opportunity to make the tree
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42721 ** shallower by one level.
42722 */
42723 static int balance_shallower(BtCursor *pCur){
42724 MemPage *pPage; /* Root page of B-Tree */
42725 MemPage *pChild; /* The only child page of pPage */
42726 Pgno pgnoChild; /* Page number for pChild */
42727 int rc = SQLITE_OK; /* Return code from subprocedures */
42728 BtShared *pBt; /* The main BTree structure */
42729 int mxCellPerPage; /* Maximum number of cells per page */
42730 u8 **apCell; /* All cells from pages being balanced */
42731 u16 *szCell; /* Local size of all cells */
42732
42733 assert( pCur->iPage==0 );
42734 pPage = pCur->apPage[0];
42735
42736 assert( pPage->nCell==0 );
42737 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
42738 pBt = pPage->pBt;
42739 mxCellPerPage = MX_CELL(pBt);
42740 apCell = sqlite3Malloc( mxCellPerPage*(sizeof(u8*)+sizeof(u16)) );
42741 if( apCell==0 ) return SQLITE_NOMEM;
42742 szCell = (u16*)&apCell[mxCellPerPage];
42743 if( pPage->leaf ){
42744 /* The table is completely empty */
42745 TRACE(("BALANCE: empty table %d\n", pPage->pgno));
42746 }else{
42747 /* The root page is empty but has one child. Transfer the
42748 ** information from that one child into the root page if it
42749 ** will fit. This reduces the depth of the tree by one.
42750 **
42751 ** If the root page is page 1, it has less space available than
42752 ** its child (due to the 100 byte header that occurs at the beginning
42753 ** of the database fle), so it might not be able to hold all of the
42754 ** information currently contained in the child. If this is the
42755 ** case, then do not do the transfer. Leave page 1 empty except
42756 ** for the right-pointer to the child page. The child page becomes
42757 ** the virtual root of the tree.
42758 */
42759 VVA_ONLY( pCur->pagesShuffled = 1 );
42760 pgnoChild = get4byte(&pPage->aData[pPage->hdrOffset+8]);
42761 assert( pgnoChild>0 );
42762 assert( pgnoChild<=pagerPagecount(pPage->pBt) );
42763 rc = sqlite3BtreeGetPage(pPage->pBt, pgnoChild, &pChild, 0);
42764 if( rc ) goto end_shallow_balance;
42765 if( pPage->pgno==1 ){
42766 rc = sqlite3BtreeInitPage(pChild);
42767 if( rc ) goto end_shallow_balance;
42768 assert( pChild->nOverflow==0 );
42769 if( pChild->nFree>=100 ){
42770 /* The child information will fit on the root page, so do the
42771 ** copy */
42772 int i;
42773 zeroPage(pPage, pChild->aData[0]);
42774 for(i=0; i<pChild->nCell; i++){
42775 apCell[i] = findCell(pChild,i);
42776 szCell[i] = cellSizePtr(pChild, apCell[i]);
42777 }
42778 assemblePage(pPage, pChild->nCell, apCell, szCell);
42779 /* Copy the right-pointer of the child to the parent. */
42780 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
42781 put4byte(&pPage->aData[pPage->hdrOffset+8],
42782 get4byte(&pChild->aData[pChild->hdrOffset+8]));
42783 rc = freePage(pChild);
42784 TRACE(("BALANCE: child %d transfer to page 1\n", pChild->pgno));
42785 }else{
42786 /* The child has more information that will fit on the root.
42787 ** The tree is already balanced. Do nothing. */
42788 TRACE(("BALANCE: child %d will not fit on page 1\n", pChild->pgno));
42789 }
42790 }else{
42791 memcpy(pPage->aData, pChild->aData, pPage->pBt->usableSize);
42792 pPage->isInit = 0;
42793 rc = sqlite3BtreeInitPage(pPage);
42794 assert( rc==SQLITE_OK );
42795 freePage(pChild);
42796 TRACE(("BALANCE: transfer child %d into root %d\n",
42797 pChild->pgno, pPage->pgno));
42798 }
42799 assert( pPage->nOverflow==0 );
42800 #ifndef SQLITE_OMIT_AUTOVACUUM
42801 if( ISAUTOVACUUM && rc==SQLITE_OK ){
42802 rc = setChildPtrmaps(pPage);
42803 }
42804 #endif
42805 releasePage(pChild);
42806 }
42807 end_shallow_balance:
42808 sqlite3_free(apCell);
42809 return rc;
42810 }
42811
42812
42813 /*
42814 ** The root page is overfull
42815 **
42816 ** When this happens, Create a new child page and copy the
42817 ** contents of the root into the child. Then make the root
42818 ** page an empty page with rightChild pointing to the new
42819 ** child. Finally, call balance_internal() on the new child
42820 ** to cause it to split.
42821 */
42822 static int balance_deeper(BtCursor *pCur){
42823 int rc; /* Return value from subprocedures */
42824 MemPage *pPage; /* Pointer to the root page */
42825 MemPage *pChild; /* Pointer to a new child page */
42826 Pgno pgnoChild; /* Page number of the new child page */
42827 BtShared *pBt; /* The BTree */
42828 int usableSize; /* Total usable size of a page */
42829 u8 *data; /* Content of the parent page */
42830 u8 *cdata; /* Content of the child page */
42831 int hdr; /* Offset to page header in parent */
42832 int cbrk; /* Offset to content of first cell in parent */
42833
42834 assert( pCur->iPage==0 );
42835 assert( pCur->apPage[0]->nOverflow>0 );
42836
42837 VVA_ONLY( pCur->pagesShuffled = 1 );
42838 pPage = pCur->apPage[0];
42839 pBt = pPage->pBt;
42840 assert( sqlite3_mutex_held(pBt->mutex) );
42841 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
42842 rc = allocateBtreePage(pBt, &pChild, &pgnoChild, pPage->pgno, 0);
42843 if( rc ) return rc;
42844 assert( sqlite3PagerIswriteable(pChild->pDbPage) );
42845 usableSize = pBt->usableSize;
42846 data = pPage->aData;
42847 hdr = pPage->hdrOffset;
42848 cbrk = get2byte(&data[hdr+5]);
42849 cdata = pChild->aData;
42850 memcpy(cdata, &data[hdr], pPage->cellOffset+2*pPage->nCell-hdr);
42851 memcpy(&cdata[cbrk], &data[cbrk], usableSize-cbrk);
42852
42853 assert( pChild->isInit==0 );
42854 rc = sqlite3BtreeInitPage(pChild);
42855 if( rc==SQLITE_OK ){
42856 int nCopy = pPage->nOverflow*sizeof(pPage->aOvfl[0]);
42857 memcpy(pChild->aOvfl, pPage->aOvfl, nCopy);
42858 pChild->nOverflow = pPage->nOverflow;
42859 if( pChild->nOverflow ){
42860 pChild->nFree = 0;
42861 }
42862 assert( pChild->nCell==pPage->nCell );
42863 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
42864 zeroPage(pPage, pChild->aData[0] & ~PTF_LEAF);
42865 put4byte(&pPage->aData[pPage->hdrOffset+8], pgnoChild);
42866 TRACE(("BALANCE: copy root %d into %d\n", pPage->pgno, pChild->pgno));
42867 if( ISAUTOVACUUM ){
42868 rc = ptrmapPut(pBt, pChild->pgno, PTRMAP_BTREE, pPage->pgno);
42869 #ifndef SQLITE_OMIT_AUTOVACUUM
42870 if( rc==SQLITE_OK ){
42871 rc = setChildPtrmaps(pChild);
42872 }
42873 if( rc ){
42874 pChild->nOverflow = 0;
42875 }
42876 #endif
42877 }
42878 }
42879
42880 if( rc==SQLITE_OK ){
42881 pCur->iPage++;
42882 pCur->apPage[1] = pChild;
42883 pCur->aiIdx[0] = 0;
42884 rc = balance_nonroot(pCur);
42885 }else{
42886 releasePage(pChild);
42887 }
42888
42889 return rc;
42890 }
42891
42892 /*
42893 ** The page that pCur currently points to has just been modified in
42894 ** some way. This function figures out if this modification means the
42895 ** tree needs to be balanced, and if so calls the appropriate balancing
42896 ** routine.
42897 **
42898 ** Parameter isInsert is true if a new cell was just inserted into the
42899 ** page, or false otherwise.
 
 
 
 
 
 
 
42900 */
42901 static int balance(BtCursor *pCur, int isInsert){
42902 int rc = SQLITE_OK;
42903 MemPage *pPage = pCur->apPage[pCur->iPage];
42904
42905 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
42906 if( pCur->iPage==0 ){
42907 rc = sqlite3PagerWrite(pPage->pDbPage);
42908 if( rc==SQLITE_OK && pPage->nOverflow>0 ){
42909 rc = balance_deeper(pCur);
42910 assert( pCur->apPage[0]==pPage );
42911 assert( pPage->nOverflow==0 || rc!=SQLITE_OK );
42912 }
42913 if( rc==SQLITE_OK && pPage->nCell==0 ){
42914 rc = balance_shallower(pCur);
42915 assert( pCur->apPage[0]==pPage );
42916 assert( pPage->nOverflow==0 || rc!=SQLITE_OK );
42917 }
42918 }else{
42919 if( pPage->nOverflow>0 ||
42920 (!isInsert && pPage->nFree>pPage->pBt->usableSize*2/3) ){
42921 rc = balance_nonroot(pCur);
42922 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42923 }
42924 return rc;
42925 }
42926
42927 /*
@@ -43061,10 +43249,11 @@
43061 SQLITE_OK!=(rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur)) || (!loc &&
43062 SQLITE_OK!=(rc = sqlite3BtreeMoveto(pCur, pKey, nKey, appendBias, &loc))
43063 )){
43064 return rc;
43065 }
 
43066
43067 pPage = pCur->apPage[pCur->iPage];
43068 assert( pPage->intKey || nKey>=0 );
43069 assert( pPage->leaf || !pPage->intKey );
43070 TRACE(("INSERT: table=%d nkey=%lld ndata=%d page=%d %s\n",
@@ -43077,11 +43266,11 @@
43077 rc = fillInCell(pPage, newCell, pKey, nKey, pData, nData, nZero, &szNew);
43078 if( rc ) goto end_insert;
43079 assert( szNew==cellSizePtr(pPage, newCell) );
43080 assert( szNew<=MX_CELL_SIZE(pBt) );
43081 idx = pCur->aiIdx[pCur->iPage];
43082 if( loc==0 && CURSOR_VALID==pCur->eState ){
43083 u16 szOld;
43084 assert( idx<pPage->nCell );
43085 rc = sqlite3PagerWrite(pPage->pDbPage);
43086 if( rc ){
43087 goto end_insert;
@@ -43098,51 +43287,49 @@
43098 goto end_insert;
43099 }
43100 }else if( loc<0 && pPage->nCell>0 ){
43101 assert( pPage->leaf );
43102 idx = ++pCur->aiIdx[pCur->iPage];
43103 pCur->info.nSize = 0;
43104 pCur->validNKey = 0;
43105 }else{
43106 assert( pPage->leaf );
43107 }
43108 rc = insertCell(pPage, idx, newCell, szNew, 0, 0);
43109 assert( rc!=SQLITE_OK || pPage->nCell>0 || pPage->nOverflow>0 );
43110
43111 /* If no error has occured, call balance() to deal with any overflow and
43112 ** move the cursor to point at the root of the table (since balance may
43113 ** have rearranged the table in such a way as to invalidate BtCursor.apPage[]
43114 ** or BtCursor.aiIdx[]).
43115 **
43116 ** Except, if all of the following are true, do nothing:
43117 **
43118 ** * Inserting the new cell did not cause overflow,
43119 **
43120 ** * Before inserting the new cell the cursor was pointing at the
43121 ** largest key in an intkey B-Tree, and
43122 **
43123 ** * The key value associated with the new cell is now the largest
43124 ** in the B-Tree.
43125 **
43126 ** In this case the cursor can be safely left pointing at the (new)
43127 ** largest key value in the B-Tree. Doing so speeds up inserting a set
43128 ** of entries with increasing integer key values via a single cursor
43129 ** (comes up with "INSERT INTO ... SELECT ..." statements), as
43130 ** the next insert operation is not required to seek the cursor.
43131 */
43132 if( rc==SQLITE_OK
43133 && (pPage->nOverflow || !pCur->atLast || loc>=0 || !pCur->apPage[0]->intKey)
43134 ){
43135 rc = balance(pCur, 1);
43136 if( rc==SQLITE_OK ){
43137 moveToRoot(pCur);
43138 }
43139 }
43140
43141 /* Must make sure nOverflow is reset to zero even if the balance()
43142 ** fails. Internal data structure corruption will result otherwise. */
43143 pCur->apPage[pCur->iPage]->nOverflow = 0;
43144
43145 end_insert:
43146 return rc;
43147 }
43148
@@ -43149,202 +43336,114 @@
43149 /*
43150 ** Delete the entry that the cursor is pointing to. The cursor
43151 ** is left pointing at a arbitrary location.
43152 */
43153 SQLITE_PRIVATE int sqlite3BtreeDelete(BtCursor *pCur){
43154 MemPage *pPage = pCur->apPage[pCur->iPage];
43155 int idx;
43156 unsigned char *pCell;
43157 int rc;
43158 Pgno pgnoChild = 0;
43159 Btree *p = pCur->pBtree;
43160 BtShared *pBt = p->pBt;
 
 
 
 
 
43161
43162 assert( cursorHoldsMutex(pCur) );
43163 assert( pPage->isInit );
43164 assert( pBt->inTransaction==TRANS_WRITE );
43165 assert( !pBt->readOnly );
43166 if( pCur->eState==CURSOR_FAULT ){
43167 return pCur->skip;
43168 }
43169 if( NEVER(pCur->aiIdx[pCur->iPage]>=pPage->nCell) ){
43170 return SQLITE_ERROR; /* The cursor is not pointing to anything */
43171 }
43172 assert( pCur->wrFlag );
 
 
 
 
 
 
43173 rc = checkForReadConflicts(p, pCur->pgnoRoot, pCur, pCur->info.nKey);
43174 if( rc!=SQLITE_OK ){
43175 /* The table pCur points to has a read lock */
43176 assert( rc==SQLITE_LOCKED_SHAREDCACHE );
43177 return rc;
43178 }
43179
43180 /* Restore the current cursor position (a no-op if the cursor is not in
43181 ** CURSOR_REQUIRESEEK state) and save the positions of any other cursors
43182 ** open on the same table. Then call sqlite3PagerWrite() on the page
43183 ** that the entry will be deleted from.
43184 */
43185 if(
43186 (rc = restoreCursorPosition(pCur))!=0 ||
43187 (rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur))!=0 ||
43188 (rc = sqlite3PagerWrite(pPage->pDbPage))!=0
43189 ){
43190 return rc;
43191 }
43192
43193 /* Locate the cell within its page and leave pCell pointing to the
43194 ** data. The clearCell() call frees any overflow pages associated with the
43195 ** cell. The cell itself is still intact.
43196 */
43197 idx = pCur->aiIdx[pCur->iPage];
43198 pCell = findCell(pPage, idx);
43199 if( !pPage->leaf ){
43200 pgnoChild = get4byte(pCell);
43201 }
43202 rc = clearCell(pPage, pCell);
43203 if( rc ){
43204 return rc;
43205 }
43206
43207 if( !pPage->leaf ){
43208 /*
43209 ** The entry we are about to delete is not a leaf so if we do not
43210 ** do something we will leave a hole on an internal page.
43211 ** We have to fill the hole by moving in a cell from a leaf. The
43212 ** next Cell after the one to be deleted is guaranteed to exist and
43213 ** to be a leaf so we can use it.
43214 */
43215 BtCursor leafCur;
43216 MemPage *pLeafPage = 0;
43217
43218 unsigned char *pNext;
43219 int notUsed;
43220 unsigned char *tempCell = 0;
43221 assert( !pPage->intKey );
43222 sqlite3BtreeGetTempCursor(pCur, &leafCur);
43223 rc = sqlite3BtreeNext(&leafCur, &notUsed);
43224 if( rc==SQLITE_OK ){
43225 assert( leafCur.aiIdx[leafCur.iPage]==0 );
43226 pLeafPage = leafCur.apPage[leafCur.iPage];
43227 rc = sqlite3PagerWrite(pLeafPage->pDbPage);
43228 }
43229 if( rc==SQLITE_OK ){
43230 int leafCursorInvalid = 0;
43231 u16 szNext;
43232 TRACE(("DELETE: table=%d delete internal from %d replace from leaf %d\n",
43233 pCur->pgnoRoot, pPage->pgno, pLeafPage->pgno));
43234 dropCell(pPage, idx, cellSizePtr(pPage, pCell));
43235 pNext = findCell(pLeafPage, 0);
43236 szNext = cellSizePtr(pLeafPage, pNext);
43237 assert( MX_CELL_SIZE(pBt)>=szNext+4 );
43238 allocateTempSpace(pBt);
43239 tempCell = pBt->pTmpSpace;
43240 if( tempCell==0 ){
43241 rc = SQLITE_NOMEM;
43242 }
43243 if( rc==SQLITE_OK ){
43244 rc = insertCell(pPage, idx, pNext-4, szNext+4, tempCell, 0);
43245 }
43246
43247
43248 /* The "if" statement in the next code block is critical. The
43249 ** slightest error in that statement would allow SQLite to operate
43250 ** correctly most of the time but produce very rare failures. To
43251 ** guard against this, the following macros help to verify that
43252 ** the "if" statement is well tested.
43253 */
43254 testcase( pPage->nOverflow==0 && pPage->nFree<pBt->usableSize*2/3
43255 && pLeafPage->nFree+2+szNext > pBt->usableSize*2/3 );
43256 testcase( pPage->nOverflow==0 && pPage->nFree==pBt->usableSize*2/3
43257 && pLeafPage->nFree+2+szNext > pBt->usableSize*2/3 );
43258 testcase( pPage->nOverflow==0 && pPage->nFree==pBt->usableSize*2/3+1
43259 && pLeafPage->nFree+2+szNext > pBt->usableSize*2/3 );
43260 testcase( pPage->nOverflow>0 && pPage->nFree<=pBt->usableSize*2/3
43261 && pLeafPage->nFree+2+szNext > pBt->usableSize*2/3 );
43262 testcase( (pPage->nOverflow>0 || (pPage->nFree > pBt->usableSize*2/3))
43263 && pLeafPage->nFree+2+szNext == pBt->usableSize*2/3 );
43264
43265
43266 if( (pPage->nOverflow>0 || (pPage->nFree > pBt->usableSize*2/3)) &&
43267 (pLeafPage->nFree+2+szNext > pBt->usableSize*2/3)
43268 ){
43269 /* This branch is taken if the internal node is now either overflowing
43270 ** or underfull and the leaf node will be underfull after the just cell
43271 ** copied to the internal node is deleted from it. This is a special
43272 ** case because the call to balance() to correct the internal node
43273 ** may change the tree structure and invalidate the contents of
43274 ** the leafCur.apPage[] and leafCur.aiIdx[] arrays, which will be
43275 ** used by the balance() required to correct the underfull leaf
43276 ** node.
43277 **
43278 ** The formula used in the expression above are based on facets of
43279 ** the SQLite file-format that do not change over time.
43280 */
43281 testcase( pPage->nFree==pBt->usableSize*2/3+1 );
43282 testcase( pLeafPage->nFree+2+szNext==pBt->usableSize*2/3+1 );
43283 leafCursorInvalid = 1;
43284 }
43285
43286 if( rc==SQLITE_OK ){
43287 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
43288 put4byte(findOverflowCell(pPage, idx), pgnoChild);
43289 VVA_ONLY( pCur->pagesShuffled = 0 );
43290 rc = balance(pCur, 0);
43291 }
43292
43293 if( rc==SQLITE_OK && leafCursorInvalid ){
43294 /* The leaf-node is now underfull and so the tree needs to be
43295 ** rebalanced. However, the balance() operation on the internal
43296 ** node above may have modified the structure of the B-Tree and
43297 ** so the current contents of leafCur.apPage[] and leafCur.aiIdx[]
43298 ** may not be trusted.
43299 **
43300 ** It is not possible to copy the ancestry from pCur, as the same
43301 ** balance() call has invalidated the pCur->apPage[] and aiIdx[]
43302 ** arrays.
43303 **
43304 ** The call to saveCursorPosition() below internally saves the
43305 ** key that leafCur is currently pointing to. Currently, there
43306 ** are two copies of that key in the tree - one here on the leaf
43307 ** page and one on some internal node in the tree. The copy on
43308 ** the leaf node is always the next key in tree-order after the
43309 ** copy on the internal node. So, the call to sqlite3BtreeNext()
43310 ** calls restoreCursorPosition() to point the cursor to the copy
43311 ** stored on the internal node, then advances to the next entry,
43312 ** which happens to be the copy of the key on the internal node.
43313 ** Net effect: leafCur is pointing back to the duplicate cell
43314 ** that needs to be removed, and the leafCur.apPage[] and
43315 ** leafCur.aiIdx[] arrays are correct.
43316 */
43317 VVA_ONLY( Pgno leafPgno = pLeafPage->pgno );
43318 rc = saveCursorPosition(&leafCur);
43319 if( rc==SQLITE_OK ){
43320 rc = sqlite3BtreeNext(&leafCur, &notUsed);
43321 }
43322 pLeafPage = leafCur.apPage[leafCur.iPage];
43323 assert( rc!=SQLITE_OK || pLeafPage->pgno==leafPgno );
43324 assert( rc!=SQLITE_OK || leafCur.aiIdx[leafCur.iPage]==0 );
43325 }
43326
43327 if( SQLITE_OK==rc
43328 && SQLITE_OK==(rc = sqlite3PagerWrite(pLeafPage->pDbPage))
43329 ){
43330 dropCell(pLeafPage, 0, szNext);
43331 VVA_ONLY( leafCur.pagesShuffled = 0 );
43332 rc = balance(&leafCur, 0);
43333 assert( leafCursorInvalid || !leafCur.pagesShuffled
43334 || !pCur->pagesShuffled );
43335 }
43336 }
43337 sqlite3BtreeReleaseTempCursor(&leafCur);
43338 }else{
43339 TRACE(("DELETE: table=%d delete from leaf %d\n",
43340 pCur->pgnoRoot, pPage->pgno));
43341 rc = dropCell(pPage, idx, cellSizePtr(pPage, pCell));
43342 if( rc==SQLITE_OK ){
43343 rc = balance(pCur, 0);
43344 }
43345 }
43346 if( rc==SQLITE_OK ){
43347 moveToRoot(pCur);
43348 }
43349 return rc;
43350 }
@@ -43431,11 +43530,14 @@
43431 rc = sqlite3BtreeGetPage(pBt, pgnoRoot, &pRoot, 0);
43432 if( rc!=SQLITE_OK ){
43433 return rc;
43434 }
43435 rc = ptrmapGet(pBt, pgnoRoot, &eType, &iPtrPage);
43436 if( rc!=SQLITE_OK || eType==PTRMAP_ROOTPAGE || eType==PTRMAP_FREEPAGE ){
 
 
 
43437 releasePage(pRoot);
43438 return rc;
43439 }
43440 assert( eType!=PTRMAP_ROOTPAGE );
43441 assert( eType!=PTRMAP_FREEPAGE );
@@ -45188,11 +45290,11 @@
45188 ** This file contains code use to manipulate "Mem" structure. A "Mem"
45189 ** stores a single value in the VDBE. Mem is an opaque structure visible
45190 ** only within the VDBE. Interface routines refer to a Mem using the
45191 ** name sqlite_value
45192 **
45193 ** $Id: vdbemem.c,v 1.147 2009/05/28 11:05:57 danielk1977 Exp $
45194 */
45195
45196 /*
45197 ** Call sqlite3VdbeMemExpandBlob() on the supplied value (type Mem*)
45198 ** P if required.
@@ -45582,11 +45684,22 @@
45582 assert( (pMem->flags & MEM_RowSet)==0 );
45583 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
45584 assert( EIGHT_BYTE_ALIGNMENT(pMem) );
45585
45586 pMem->u.i = doubleToInt64(pMem->r);
45587 if( pMem->r==(double)pMem->u.i ){
 
 
 
 
 
 
 
 
 
 
 
45588 pMem->flags |= MEM_Int;
45589 }
45590 }
45591
45592 /*
@@ -45797,10 +45910,16 @@
45797 ** The memory management strategy depends on the value of the xDel
45798 ** parameter. If the value passed is SQLITE_TRANSIENT, then the
45799 ** string is copied into a (possibly existing) buffer managed by the
45800 ** Mem structure. Otherwise, any existing buffer is freed and the
45801 ** pointer copied.
 
 
 
 
 
 
45802 */
45803 SQLITE_PRIVATE int sqlite3VdbeMemSetStr(
45804 Mem *pMem, /* Memory cell to set to string value */
45805 const char *z, /* String pointer */
45806 int n, /* Bytes in string, or negative */
@@ -45860,13 +45979,10 @@
45860 sqlite3VdbeMemRelease(pMem);
45861 pMem->z = (char *)z;
45862 pMem->xDel = xDel;
45863 flags |= ((xDel==SQLITE_STATIC)?MEM_Static:MEM_Dyn);
45864 }
45865 if( nByte>iLimit ){
45866 return SQLITE_TOOBIG;
45867 }
45868
45869 pMem->n = nByte;
45870 pMem->flags = flags;
45871 pMem->enc = (enc==0 ? SQLITE_UTF8 : enc);
45872 pMem->type = (enc==0 ? SQLITE_BLOB : SQLITE_TEXT);
@@ -45874,10 +45990,14 @@
45874 #ifndef SQLITE_OMIT_UTF16
45875 if( pMem->enc!=SQLITE_UTF8 && sqlite3VdbeMemHandleBom(pMem) ){
45876 return SQLITE_NOMEM;
45877 }
45878 #endif
 
 
 
 
45879
45880 return SQLITE_OK;
45881 }
45882
45883 /*
@@ -46250,11 +46370,11 @@
46250 ** This file contains code used for creating, destroying, and populating
46251 ** a VDBE (or an "sqlite3_stmt" as it is known to the outside world.) Prior
46252 ** to version 2.8.7, all this code was combined into the vdbe.c source file.
46253 ** But that file was getting too big so this subroutines were split out.
46254 **
46255 ** $Id: vdbeaux.c,v 1.459 2009/06/05 14:17:25 drh Exp $
46256 */
46257
46258
46259
46260 /*
@@ -46860,15 +46980,28 @@
46860 ** If a memory allocation error has occurred prior to the calling of this
46861 ** routine, then a pointer to a dummy VdbeOp will be returned. That opcode
46862 ** is readable and writable, but it has no effect. The return of a dummy
46863 ** opcode allows the call to continue functioning after a OOM fault without
46864 ** having to check to see if the return from this routine is a valid pointer.
 
 
 
 
 
 
 
 
46865 */
46866 SQLITE_PRIVATE VdbeOp *sqlite3VdbeGetOp(Vdbe *p, int addr){
46867 static VdbeOp dummy;
46868 assert( p->magic==VDBE_MAGIC_INIT );
46869 if( addr<0 ) addr = p->nOp - 1;
 
 
 
 
 
46870 assert( (addr>=0 && addr<p->nOp) || p->db->mallocFailed );
46871 if( p->db->mallocFailed ){
46872 return &dummy;
46873 }else{
46874 return &p->aOp[addr];
@@ -48235,13 +48368,21 @@
48235 sqlite3DbFree(db, p->pFree);
48236 sqlite3DbFree(db, p);
48237 }
48238
48239 /*
 
 
 
 
48240 ** If a MoveTo operation is pending on the given cursor, then do that
48241 ** MoveTo now. Return an error code. If no MoveTo is pending, this
48242 ** routine does nothing and returns SQLITE_OK.
 
 
 
 
48243 */
48244 SQLITE_PRIVATE int sqlite3VdbeCursorMoveto(VdbeCursor *p){
48245 if( p->deferredMoveto ){
48246 int res, rc;
48247 #ifdef SQLITE_TEST
@@ -48248,11 +48389,11 @@
48248 extern int sqlite3_search_count;
48249 #endif
48250 assert( p->isTable );
48251 rc = sqlite3BtreeMovetoUnpacked(p->pCursor, 0, p->movetoTarget, 0, &res);
48252 if( rc ) return rc;
48253 p->lastRowid = keyToInt(p->movetoTarget);
48254 p->rowidIsValid = ALWAYS(res==0) ?1:0;
48255 if( NEVER(res<0) ){
48256 rc = sqlite3BtreeNext(p->pCursor, &res);
48257 if( rc ) return rc;
48258 }
@@ -48450,11 +48591,11 @@
48450 swapMixedEndianFloat(v);
48451 }else{
48452 v = pMem->u.i;
48453 }
48454 len = i = sqlite3VdbeSerialTypeLen(serial_type);
48455 assert( len<=nBuf );
48456 while( i-- ){
48457 buf[i] = (u8)(v&0xFF);
48458 v >>= 8;
48459 }
48460 return len;
@@ -48461,11 +48602,11 @@
48461 }
48462
48463 /* String or blob */
48464 if( serial_type>=12 ){
48465 assert( pMem->n + ((pMem->flags & MEM_Zero)?pMem->u.nZero:0)
48466 == sqlite3VdbeSerialTypeLen(serial_type) );
48467 assert( pMem->n<=nBuf );
48468 len = pMem->n;
48469 memcpy(buf, pMem->z, len);
48470 if( pMem->flags & MEM_Zero ){
48471 len += pMem->u.nZero;
@@ -48790,11 +48931,11 @@
48790 ** Return SQLITE_OK if everything works, or an error code otherwise.
48791 **
48792 ** pCur might be pointing to text obtained from a corrupt database file.
48793 ** So the content cannot be trusted. Do appropriate checks on the content.
48794 */
48795 SQLITE_PRIVATE int sqlite3VdbeIdxRowid(BtCursor *pCur, i64 *rowid){
48796 i64 nCellKey = 0;
48797 int rc;
48798 u32 szHdr; /* Size of the header */
48799 u32 typeRowid; /* Serial type of the rowid */
48800 u32 lenRowid; /* Size of the rowid */
@@ -48807,11 +48948,11 @@
48807 return SQLITE_CORRUPT_BKPT;
48808 }
48809
48810 /* Read in the complete content of the index entry */
48811 m.flags = 0;
48812 m.db = 0;
48813 m.zMalloc = 0;
48814 rc = sqlite3VdbeMemFromBtree(pCur, 0, (int)nCellKey, 1, &m);
48815 if( rc ){
48816 return rc;
48817 }
@@ -48957,168 +49098,12 @@
48957 *************************************************************************
48958 **
48959 ** This file contains code use to implement APIs that are part of the
48960 ** VDBE.
48961 **
48962 ** $Id: vdbeapi.c,v 1.165 2009/06/06 14:13:27 danielk1977 Exp $
48963 */
48964
48965 #if 0 && defined(SQLITE_ENABLE_MEMORY_MANAGEMENT)
48966 /*
48967 ** The following structure contains pointers to the end points of a
48968 ** doubly-linked list of all compiled SQL statements that may be holding
48969 ** buffers eligible for release when the sqlite3_release_memory() interface is
48970 ** invoked. Access to this list is protected by the SQLITE_MUTEX_STATIC_LRU2
48971 ** mutex.
48972 **
48973 ** Statements are added to the end of this list when sqlite3_reset() is
48974 ** called. They are removed either when sqlite3_step() or sqlite3_finalize()
48975 ** is called. When statements are added to this list, the associated
48976 ** register array (p->aMem[1..p->nMem]) may contain dynamic buffers that
48977 ** can be freed using sqlite3VdbeReleaseMemory().
48978 **
48979 ** When statements are added or removed from this list, the mutex
48980 ** associated with the Vdbe being added or removed (Vdbe.db->mutex) is
48981 ** already held. The LRU2 mutex is then obtained, blocking if necessary,
48982 ** the linked-list pointers manipulated and the LRU2 mutex relinquished.
48983 */
48984 struct StatementLruList {
48985 Vdbe *pFirst;
48986 Vdbe *pLast;
48987 };
48988 static struct StatementLruList sqlite3LruStatements;
48989
48990 /*
48991 ** Check that the list looks to be internally consistent. This is used
48992 ** as part of an assert() statement as follows:
48993 **
48994 ** assert( stmtLruCheck() );
48995 */
48996 #ifndef NDEBUG
48997 static int stmtLruCheck(){
48998 Vdbe *p;
48999 for(p=sqlite3LruStatements.pFirst; p; p=p->pLruNext){
49000 assert(p->pLruNext || p==sqlite3LruStatements.pLast);
49001 assert(!p->pLruNext || p->pLruNext->pLruPrev==p);
49002 assert(p->pLruPrev || p==sqlite3LruStatements.pFirst);
49003 assert(!p->pLruPrev || p->pLruPrev->pLruNext==p);
49004 }
49005 return 1;
49006 }
49007 #endif
49008
49009 /*
49010 ** Add vdbe p to the end of the statement lru list. It is assumed that
49011 ** p is not already part of the list when this is called. The lru list
49012 ** is protected by the SQLITE_MUTEX_STATIC_LRU mutex.
49013 */
49014 static void stmtLruAdd(Vdbe *p){
49015 sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_LRU2));
49016
49017 if( p->pLruPrev || p->pLruNext || sqlite3LruStatements.pFirst==p ){
49018 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_LRU2));
49019 return;
49020 }
49021
49022 assert( stmtLruCheck() );
49023
49024 if( !sqlite3LruStatements.pFirst ){
49025 assert( !sqlite3LruStatements.pLast );
49026 sqlite3LruStatements.pFirst = p;
49027 sqlite3LruStatements.pLast = p;
49028 }else{
49029 assert( !sqlite3LruStatements.pLast->pLruNext );
49030 p->pLruPrev = sqlite3LruStatements.pLast;
49031 sqlite3LruStatements.pLast->pLruNext = p;
49032 sqlite3LruStatements.pLast = p;
49033 }
49034
49035 assert( stmtLruCheck() );
49036
49037 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_LRU2));
49038 }
49039
49040 /*
49041 ** Assuming the SQLITE_MUTEX_STATIC_LRU2 mutext is already held, remove
49042 ** statement p from the least-recently-used statement list. If the
49043 ** statement is not currently part of the list, this call is a no-op.
49044 */
49045 static void stmtLruRemoveNomutex(Vdbe *p){
49046 if( p->pLruPrev || p->pLruNext || p==sqlite3LruStatements.pFirst ){
49047 assert( stmtLruCheck() );
49048 if( p->pLruNext ){
49049 p->pLruNext->pLruPrev = p->pLruPrev;
49050 }else{
49051 sqlite3LruStatements.pLast = p->pLruPrev;
49052 }
49053 if( p->pLruPrev ){
49054 p->pLruPrev->pLruNext = p->pLruNext;
49055 }else{
49056 sqlite3LruStatements.pFirst = p->pLruNext;
49057 }
49058 p->pLruNext = 0;
49059 p->pLruPrev = 0;
49060 assert( stmtLruCheck() );
49061 }
49062 }
49063
49064 /*
49065 ** Assuming the SQLITE_MUTEX_STATIC_LRU2 mutext is not held, remove
49066 ** statement p from the least-recently-used statement list. If the
49067 ** statement is not currently part of the list, this call is a no-op.
49068 */
49069 static void stmtLruRemove(Vdbe *p){
49070 sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_LRU2));
49071 stmtLruRemoveNomutex(p);
49072 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_LRU2));
49073 }
49074
49075 /*
49076 ** Try to release n bytes of memory by freeing buffers associated
49077 ** with the memory registers of currently unused vdbes.
49078 */
49079 SQLITE_PRIVATE int sqlite3VdbeReleaseMemory(int n){
49080 Vdbe *p;
49081 Vdbe *pNext;
49082 int nFree = 0;
49083
49084 sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_LRU2));
49085 for(p=sqlite3LruStatements.pFirst; p && nFree<n; p=pNext){
49086 pNext = p->pLruNext;
49087
49088 /* For each statement handle in the lru list, attempt to obtain the
49089 ** associated database mutex. If it cannot be obtained, continue
49090 ** to the next statement handle. It is not possible to block on
49091 ** the database mutex - that could cause deadlock.
49092 */
49093 if( SQLITE_OK==sqlite3_mutex_try(p->db->mutex) ){
49094 nFree += sqlite3VdbeReleaseBuffers(p);
49095 stmtLruRemoveNomutex(p);
49096 sqlite3_mutex_leave(p->db->mutex);
49097 }
49098 }
49099 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_LRU2));
49100
49101 return nFree;
49102 }
49103
49104 /*
49105 ** Call sqlite3Reprepare() on the statement. Remove it from the
49106 ** lru list before doing so, as Reprepare() will free all the
49107 ** memory register buffers anyway.
49108 */
49109 int vdbeReprepare(Vdbe *p){
49110 stmtLruRemove(p);
49111 return sqlite3Reprepare(p);
49112 }
49113
49114 #else /* !SQLITE_ENABLE_MEMORY_MANAGEMENT */
49115 #define stmtLruRemove(x)
49116 #define stmtLruAdd(x)
49117 #define vdbeReprepare(x) sqlite3Reprepare(x)
49118 #endif
49119
49120
49121 #ifndef SQLITE_OMIT_DEPRECATED
49122 /*
49123 ** Return TRUE (non-zero) of the statement supplied as an argument needs
49124 ** to be recompiled. A statement needs to be recompiled whenever the
@@ -49151,11 +49136,10 @@
49151 sqlite3 *db = v->db;
49152 #if SQLITE_THREADSAFE
49153 sqlite3_mutex *mutex = v->db->mutex;
49154 #endif
49155 sqlite3_mutex_enter(mutex);
49156 stmtLruRemove(v);
49157 rc = sqlite3VdbeFinalize(v);
49158 rc = sqlite3ApiExit(db, rc);
49159 sqlite3_mutex_leave(mutex);
49160 }
49161 return rc;
@@ -49175,11 +49159,10 @@
49175 rc = SQLITE_OK;
49176 }else{
49177 Vdbe *v = (Vdbe*)pStmt;
49178 sqlite3_mutex_enter(v->db->mutex);
49179 rc = sqlite3VdbeReset(v);
49180 stmtLruAdd(v);
49181 sqlite3VdbeMakeReady(v, -1, 0, 0, 0);
49182 assert( (rc & (v->db->errMask))==rc );
49183 rc = sqlite3ApiExit(v->db, rc);
49184 sqlite3_mutex_leave(v->db->mutex);
49185 }
@@ -49419,11 +49402,10 @@
49419 #endif
49420
49421 db->activeVdbeCnt++;
49422 if( p->readOnly==0 ) db->writeVdbeCnt++;
49423 p->pc = 0;
49424 stmtLruRemove(p);
49425 }
49426 #ifndef SQLITE_OMIT_EXPLAIN
49427 if( p->explain ){
49428 rc = sqlite3VdbeList(p);
49429 }else
@@ -49479,33 +49461,20 @@
49479 /*
49480 ** This is the top-level implementation of sqlite3_step(). Call
49481 ** sqlite3Step() to do most of the work. If a schema error occurs,
49482 ** call sqlite3Reprepare() and try again.
49483 */
49484 #ifdef SQLITE_OMIT_PARSER
49485 SQLITE_API int sqlite3_step(sqlite3_stmt *pStmt){
49486 int rc = SQLITE_MISUSE;
49487 if( pStmt ){
49488 Vdbe *v;
49489 v = (Vdbe*)pStmt;
49490 sqlite3_mutex_enter(v->db->mutex);
49491 rc = sqlite3Step(v);
49492 sqlite3_mutex_leave(v->db->mutex);
49493 }
49494 return rc;
49495 }
49496 #else
49497 SQLITE_API int sqlite3_step(sqlite3_stmt *pStmt){
49498 int rc = SQLITE_MISUSE;
49499 if( pStmt ){
49500 int cnt = 0;
49501 Vdbe *v = (Vdbe*)pStmt;
49502 sqlite3 *db = v->db;
49503 sqlite3_mutex_enter(db->mutex);
49504 while( (rc = sqlite3Step(v))==SQLITE_SCHEMA
49505 && cnt++ < 5
49506 && (rc = vdbeReprepare(v))==SQLITE_OK ){
49507 sqlite3_reset(pStmt);
49508 v->expired = 0;
49509 }
49510 if( rc==SQLITE_SCHEMA && ALWAYS(v->isPrepareV2) && ALWAYS(db->pErr) ){
49511 /* This case occurs after failing to recompile an sql statement.
@@ -49528,11 +49497,10 @@
49528 rc = sqlite3ApiExit(db, rc);
49529 sqlite3_mutex_leave(db->mutex);
49530 }
49531 return rc;
49532 }
49533 #endif
49534
49535 /*
49536 ** Extract the user data from a sqlite3_context structure and return a
49537 ** pointer to it.
49538 */
@@ -50351,11 +50319,11 @@
50351 ** documentation, headers files, or other derived files. The formatting
50352 ** of the code in this file is, therefore, important. See other comments
50353 ** in this file for details. If in doubt, do not deviate from existing
50354 ** commenting and indentation practices when changing or adding code.
50355 **
50356 ** $Id: vdbe.c,v 1.848 2009/06/05 16:46:53 drh Exp $
50357 */
50358
50359 /*
50360 ** The following global variable is incremented every time a cursor
50361 ** moves, either by the OP_SeekXX, OP_Next, or OP_Prev opcodes. The test
@@ -50495,11 +50463,11 @@
50495 static VdbeCursor *allocateCursor(
50496 Vdbe *p, /* The virtual machine */
50497 int iCur, /* Index of the new VdbeCursor */
50498 int nField, /* Number of fields in the table or index */
50499 int iDb, /* When database the cursor belongs to, or -1 */
50500 int isBtreeCursor /* */
50501 ){
50502 /* Find the memory cell that will be used to store the blob of memory
50503 ** required for this VdbeCursor structure. It is convenient to use a
50504 ** vdbe memory cell to manage the memory allocation required for a
50505 ** VdbeCursor structure for the following reasons:
@@ -50732,12 +50700,14 @@
50732 fprintf(out, " NULL");
50733 }else if( (p->flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){
50734 fprintf(out, " si:%lld", p->u.i);
50735 }else if( p->flags & MEM_Int ){
50736 fprintf(out, " i:%lld", p->u.i);
 
50737 }else if( p->flags & MEM_Real ){
50738 fprintf(out, " r:%g", p->r);
 
50739 }else if( p->flags & MEM_RowSet ){
50740 fprintf(out, " (rowset)");
50741 }else{
50742 char zBuf[200];
50743 sqlite3VdbeMemPrettyPrint(p, zBuf);
@@ -51032,13 +51002,10 @@
51032 int v2; /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
51033 } ak;
51034 struct OP_IfNot_stack_vars {
51035 int c;
51036 } al;
51037 struct OP_IsNull_stack_vars {
51038 int n;
51039 } am;
51040 struct OP_Column_stack_vars {
51041 u32 payloadSize; /* Number of bytes in the record */
51042 i64 payloadSize64; /* Number of bytes in the record */
51043 int p1; /* P1 value of the opcode */
51044 int p2; /* column number to retrieve */
@@ -51057,17 +51024,17 @@
51057 u8 *zEndHdr; /* Pointer to first byte after the header */
51058 u32 offset; /* Offset into the data */
51059 u64 offset64; /* 64-bit offset. 64 bits needed to catch overflow */
51060 int szHdr; /* Size of the header size field at start of record */
51061 int avail; /* Number of bytes of available data */
51062 } an;
51063 struct OP_Affinity_stack_vars {
51064 char *zAffinity; /* The affinity to be applied */
51065 Mem *pData0; /* First register to which to apply affinity */
51066 Mem *pLast; /* Last register to which to apply affinity */
51067 Mem *pRec; /* Current register */
51068 } ao;
51069 struct OP_MakeRecord_stack_vars {
51070 u8 *zNewRecord; /* A buffer to hold the data for the new record */
51071 Mem *pRec; /* The new record */
51072 u64 nData; /* Number of bytes of data space */
51073 int nHdr; /* Number of bytes of header space */
@@ -51080,270 +51047,246 @@
51080 int nField; /* Number of fields in the record */
51081 char *zAffinity; /* The affinity string for the record */
51082 int file_format; /* File format to use for encoding */
51083 int i; /* Space used in zNewRecord[] */
51084 int len; /* Length of a field */
51085 } ap;
51086 struct OP_Count_stack_vars {
51087 i64 nEntry;
51088 BtCursor *pCrsr;
51089 } aq;
51090 struct OP_Statement_stack_vars {
51091 int i;
51092 Btree *pBt;
51093 } ar;
51094 struct OP_Savepoint_stack_vars {
51095 int p1; /* Value of P1 operand */
51096 char *zName; /* Name of savepoint */
51097 int nName;
51098 Savepoint *pNew;
51099 Savepoint *pSavepoint;
51100 Savepoint *pTmp;
51101 int iSavepoint;
51102 int ii;
51103 } as;
51104 struct OP_AutoCommit_stack_vars {
51105 int desiredAutoCommit;
51106 int rollback;
51107 int turnOnAC;
51108 } at;
51109 struct OP_Transaction_stack_vars {
51110 int i;
51111 Btree *pBt;
51112 } au;
51113 struct OP_ReadCookie_stack_vars {
51114 int iMeta;
51115 int iDb;
51116 int iCookie;
51117 } av;
51118 struct OP_SetCookie_stack_vars {
51119 Db *pDb;
51120 } aw;
51121 struct OP_VerifyCookie_stack_vars {
51122 int iMeta;
51123 Btree *pBt;
51124 } ax;
51125 struct OP_OpenWrite_stack_vars {
51126 int nField;
51127 KeyInfo *pKeyInfo;
51128 int i;
51129 int p2;
51130 int iDb;
51131 int wrFlag;
51132 Btree *pX;
51133 VdbeCursor *pCur;
51134 Db *pDb;
51135 int flags;
51136 } ay;
51137 struct OP_OpenEphemeral_stack_vars {
51138 int i;
 
 
51139 VdbeCursor *pCx;
51140 } az;
51141 struct OP_OpenPseudo_stack_vars {
51142 int i;
51143 VdbeCursor *pCx;
51144 } ba;
51145 struct OP_Close_stack_vars {
51146 int i;
51147 } bb;
51148 struct OP_SeekGt_stack_vars {
51149 int i;
51150 int res;
51151 int oc;
51152 VdbeCursor *pC;
51153 UnpackedRecord r;
51154 int nField;
51155 i64 iKey; /* The rowid we are to seek to */
51156 } bc;
51157 struct OP_Seek_stack_vars {
51158 int i;
51159 VdbeCursor *pC;
51160 } bd;
51161 struct OP_Found_stack_vars {
51162 int i;
51163 int alreadyExists;
51164 VdbeCursor *pC;
51165 int res;
51166 UnpackedRecord *pIdxKey;
51167 char aTempRec[ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*3 + 7];
51168 } be;
51169 struct OP_IsUnique_stack_vars {
51170 u16 ii;
51171 VdbeCursor *pCx;
51172 BtCursor *pCrsr;
51173 u16 nField;
51174 Mem *aMem;
51175 UnpackedRecord r; /* B-Tree index search key */
51176 i64 R; /* Rowid stored in register P3 */
51177 } bf;
51178 struct OP_NotExists_stack_vars {
51179 int i;
51180 VdbeCursor *pC;
51181 BtCursor *pCrsr;
51182 int res;
51183 u64 iKey;
51184 } bg;
51185 struct OP_NewRowid_stack_vars {
51186 int i;
51187 i64 v;
51188 VdbeCursor *pC;
51189 int res;
51190 int rx;
51191 int cnt;
51192 i64 x;
51193 Mem *pMem;
51194 } bh;
51195 struct OP_Insert_stack_vars {
51196 Mem *pData;
51197 Mem *pKey;
51198 i64 iKey; /* The integer ROWID or key for the record to be inserted */
51199 int i;
51200 VdbeCursor *pC;
51201 int nZero;
51202 int seekResult;
51203 const char *zDb;
51204 const char *zTbl;
51205 int op;
51206 } bi;
51207 struct OP_Delete_stack_vars {
51208 int i;
51209 i64 iKey;
51210 VdbeCursor *pC;
51211 } bj;
51212 struct OP_RowData_stack_vars {
51213 int i;
51214 VdbeCursor *pC;
51215 BtCursor *pCrsr;
51216 u32 n;
51217 i64 n64;
51218 } bk;
51219 struct OP_Rowid_stack_vars {
51220 int i;
51221 VdbeCursor *pC;
51222 i64 v;
51223 sqlite3_vtab *pVtab;
51224 const sqlite3_module *pModule;
51225 } bl;
51226 struct OP_NullRow_stack_vars {
51227 int i;
51228 VdbeCursor *pC;
51229 } bm;
51230 struct OP_Last_stack_vars {
51231 int i;
 
 
 
 
 
 
 
 
 
51232 VdbeCursor *pC;
51233 BtCursor *pCrsr;
51234 int res;
51235 } bn;
51236 struct OP_Rewind_stack_vars {
51237 int i;
51238 VdbeCursor *pC;
51239 BtCursor *pCrsr;
51240 int res;
51241 } bo;
51242 struct OP_Next_stack_vars {
51243 VdbeCursor *pC;
51244 BtCursor *pCrsr;
51245 int res;
51246 } bp;
51247 struct OP_IdxInsert_stack_vars {
51248 int i;
51249 VdbeCursor *pC;
51250 BtCursor *pCrsr;
51251 int nKey;
51252 const char *zKey;
51253 } bq;
51254 struct OP_IdxDelete_stack_vars {
51255 int i;
51256 VdbeCursor *pC;
51257 BtCursor *pCrsr;
51258 } br;
 
 
51259 struct OP_IdxRowid_stack_vars {
51260 int i;
51261 BtCursor *pCrsr;
51262 VdbeCursor *pC;
51263 i64 rowid;
51264 } bs;
51265 struct OP_IdxGE_stack_vars {
51266 int i;
51267 VdbeCursor *pC;
51268 int res;
51269 UnpackedRecord r;
51270 } bt;
51271 struct OP_Destroy_stack_vars {
51272 int iMoved;
51273 int iCnt;
51274 Vdbe *pVdbe;
51275 int iDb;
51276 } bu;
51277 struct OP_Clear_stack_vars {
51278 int nChange;
51279 } bv;
51280 struct OP_CreateTable_stack_vars {
51281 int pgno;
51282 int flags;
51283 Db *pDb;
51284 } bw;
51285 struct OP_ParseSchema_stack_vars {
51286 int iDb;
51287 const char *zMaster;
51288 char *zSql;
51289 InitData initData;
51290 } bx;
51291 struct OP_IntegrityCk_stack_vars {
51292 int nRoot; /* Number of tables to check. (Number of root pages.) */
51293 int *aRoot; /* Array of rootpage numbers for tables to be checked */
51294 int j; /* Loop counter */
51295 int nErr; /* Number of errors reported */
51296 char *z; /* Text of the error report */
51297 Mem *pnErr; /* Register keeping track of errors remaining */
51298 } by;
51299 struct OP_RowSetAdd_stack_vars {
51300 Mem *pIdx;
51301 Mem *pVal;
51302 } bz;
51303 struct OP_RowSetRead_stack_vars {
51304 Mem *pIdx;
51305 i64 val;
51306 } ca;
51307 struct OP_RowSetTest_stack_vars {
51308 int iSet;
51309 int exists;
51310 } cb;
51311 struct OP_ContextPush_stack_vars {
51312 int i;
51313 Context *pContext;
51314 } cc;
51315 struct OP_ContextPop_stack_vars {
51316 Context *pContext;
51317 } cd;
51318 struct OP_AggStep_stack_vars {
51319 int n;
51320 int i;
51321 Mem *pMem;
51322 Mem *pRec;
51323 sqlite3_context ctx;
51324 sqlite3_value **apVal;
51325 } ce;
51326 struct OP_AggFinal_stack_vars {
51327 Mem *pMem;
51328 } cf;
51329 struct OP_IncrVacuum_stack_vars {
51330 Btree *pBt;
51331 } cg;
51332 struct OP_TableLock_stack_vars {
51333 int p1;
51334 u8 isWriteLock;
51335 } ch;
51336 struct OP_VBegin_stack_vars {
51337 sqlite3_vtab *pVtab;
51338 } ci;
51339 struct OP_VOpen_stack_vars {
51340 VdbeCursor *pCur;
51341 sqlite3_vtab_cursor *pVtabCursor;
51342 sqlite3_vtab *pVtab;
51343 sqlite3_module *pModule;
51344 } cj;
51345 struct OP_VFilter_stack_vars {
51346 int nArg;
51347 int iQuery;
51348 const sqlite3_module *pModule;
51349 Mem *pQuery;
@@ -51352,44 +51295,44 @@
51352 sqlite3_vtab *pVtab;
51353 VdbeCursor *pCur;
51354 int res;
51355 int i;
51356 Mem **apArg;
51357 } ck;
51358 struct OP_VColumn_stack_vars {
51359 sqlite3_vtab *pVtab;
51360 const sqlite3_module *pModule;
51361 Mem *pDest;
51362 sqlite3_context sContext;
51363 } cl;
51364 struct OP_VNext_stack_vars {
51365 sqlite3_vtab *pVtab;
51366 const sqlite3_module *pModule;
51367 int res;
51368 VdbeCursor *pCur;
51369 } cm;
51370 struct OP_VRename_stack_vars {
51371 sqlite3_vtab *pVtab;
51372 Mem *pName;
51373 } cn;
51374 struct OP_VUpdate_stack_vars {
51375 sqlite3_vtab *pVtab;
51376 sqlite3_module *pModule;
51377 int nArg;
51378 int i;
51379 sqlite_int64 rowid;
51380 Mem **apArg;
51381 Mem *pX;
51382 } co;
51383 struct OP_Pagecount_stack_vars {
51384 int p1;
51385 int nPage;
51386 Pager *pPager;
51387 } cp;
51388 struct OP_Trace_stack_vars {
51389 char *zTrace;
51390 } cq;
51391 } u;
51392 /* End automatically generated code
51393 ********************************************************************/
51394
51395 assert( p->magic==VDBE_MAGIC_RUN ); /* sqlite3_step() verifies this */
@@ -51732,27 +51675,24 @@
51732 pOp->opcode = OP_String;
51733 pOp->p1 = sqlite3Strlen30(pOp->p4.z);
51734
51735 #ifndef SQLITE_OMIT_UTF16
51736 if( encoding!=SQLITE_UTF8 ){
51737 sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC);
 
51738 if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pOut, encoding) ) goto no_mem;
51739 if( SQLITE_OK!=sqlite3VdbeMemMakeWriteable(pOut) ) goto no_mem;
 
51740 pOut->zMalloc = 0;
51741 pOut->flags |= MEM_Static;
51742 pOut->flags &= ~MEM_Dyn;
51743 if( pOp->p4type==P4_DYNAMIC ){
51744 sqlite3DbFree(db, pOp->p4.z);
51745 }
51746 pOp->p4type = P4_DYNAMIC;
51747 pOp->p4.z = pOut->z;
51748 pOp->p1 = pOut->n;
51749 if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
51750 goto too_big;
51751 }
51752 UPDATE_MAX_BLOBSIZE(pOut);
51753 break;
51754 }
51755 #endif
51756 if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
51757 goto too_big;
51758 }
@@ -51940,13 +51880,17 @@
51940 ** opened by this VM before returning control to the user. This is to
51941 ** ensure that statement-transactions are always nested, not overlapping.
51942 ** If the open statement-transaction is not closed here, then the user
51943 ** may step another VM that opens its own statement transaction. This
51944 ** may lead to overlapping statement transactions.
 
 
 
51945 */
51946 assert( p->iStatement==0 || db->flags&SQLITE_CountRows );
51947 if( SQLITE_OK!=(rc = sqlite3VdbeCloseStatement(p, SAVEPOINT_RELEASE)) ){
 
51948 break;
51949 }
51950
51951 /* Invalidate all ephemeral cursor row caches */
51952 p->cacheCtr = (p->cacheCtr + 2)|1;
@@ -51990,13 +51934,12 @@
51990 assert( pIn1!=pOut );
51991 if( (pIn1->flags | pIn2->flags) & MEM_Null ){
51992 sqlite3VdbeMemSetNull(pOut);
51993 break;
51994 }
51995 ExpandBlob(pIn1);
51996 Stringify(pIn1, encoding);
51997 ExpandBlob(pIn2);
51998 Stringify(pIn2, encoding);
51999 u.ae.nByte = pIn1->n + pIn2->n;
52000 if( u.ae.nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
52001 goto too_big;
52002 }
@@ -52108,12 +52051,12 @@
52108 if( u.af.rA==(double)0 ) goto arithmetic_result_is_null;
52109 u.af.rB /= u.af.rA;
52110 break;
52111 }
52112 default: {
52113 u.af.iA = u.af.rA;
52114 u.af.iB = u.af.rB;
52115 if( u.af.iA==0 ) goto arithmetic_result_is_null;
52116 if( u.af.iA==-1 ) u.af.iA = 1;
52117 u.af.rB = (double)(u.af.iB % u.af.iA);
52118 break;
52119 }
@@ -52781,30 +52724,18 @@
52781 pc = pOp->p2-1;
52782 }
52783 break;
52784 }
52785
52786 /* Opcode: IsNull P1 P2 P3 * *
52787 **
52788 ** Jump to P2 if the value in register P1 is NULL. If P3 is greater
52789 ** than zero, then check all values reg(P1), reg(P1+1),
52790 ** reg(P1+2), ..., reg(P1+P3-1).
52791 */
52792 case OP_IsNull: { /* same as TK_ISNULL, jump, in1 */
52793 #if 0 /* local variables moved into u.am */
52794 int n;
52795 #endif /* local variables moved into u.am */
52796
52797 u.am.n = pOp->p3;
52798 assert( pOp->p3==0 || pOp->p1>0 );
52799 do{
52800 if( (pIn1->flags & MEM_Null)!=0 ){
52801 pc = pOp->p2 - 1;
52802 break;
52803 }
52804 pIn1++;
52805 }while( --u.am.n > 0 );
52806 break;
52807 }
52808
52809 /* Opcode: NotNull P1 P2 * * *
52810 **
@@ -52852,11 +52783,11 @@
52852 ** If the column contains fewer than P2 fields, then extract a NULL. Or,
52853 ** if the P4 argument is a P4_MEM use the value of the P4 argument as
52854 ** the result.
52855 */
52856 case OP_Column: {
52857 #if 0 /* local variables moved into u.an */
52858 u32 payloadSize; /* Number of bytes in the record */
52859 i64 payloadSize64; /* Number of bytes in the record */
52860 int p1; /* P1 value of the opcode */
52861 int p2; /* column number to retrieve */
52862 VdbeCursor *pC; /* The VDBE cursor */
@@ -52874,123 +52805,125 @@
52874 u8 *zEndHdr; /* Pointer to first byte after the header */
52875 u32 offset; /* Offset into the data */
52876 u64 offset64; /* 64-bit offset. 64 bits needed to catch overflow */
52877 int szHdr; /* Size of the header size field at start of record */
52878 int avail; /* Number of bytes of available data */
52879 #endif /* local variables moved into u.an */
52880
52881
52882 u.an.p1 = pOp->p1;
52883 u.an.p2 = pOp->p2;
52884 u.an.pC = 0;
52885 memset(&u.an.sMem, 0, sizeof(u.an.sMem));
52886 assert( u.an.p1<p->nCursor );
52887 assert( pOp->p3>0 && pOp->p3<=p->nMem );
52888 u.an.pDest = &p->aMem[pOp->p3];
52889 MemSetTypeFlag(u.an.pDest, MEM_Null);
52890
52891 /* This block sets the variable u.an.payloadSize to be the total number of
52892 ** bytes in the record.
52893 **
52894 ** u.an.zRec is set to be the complete text of the record if it is available.
52895 ** The complete record text is always available for pseudo-tables
52896 ** If the record is stored in a cursor, the complete record text
52897 ** might be available in the u.an.pC->aRow cache. Or it might not be.
52898 ** If the data is unavailable, u.an.zRec is set to NULL.
52899 **
52900 ** We also compute the number of columns in the record. For cursors,
52901 ** the number of columns is stored in the VdbeCursor.nField element.
52902 */
52903 u.an.pC = p->apCsr[u.an.p1];
52904 assert( u.an.pC!=0 );
52905 #ifndef SQLITE_OMIT_VIRTUALTABLE
52906 assert( u.an.pC->pVtabCursor==0 );
52907 #endif
52908 if( u.an.pC->pCursor!=0 ){
52909 /* The record is stored in a B-Tree */
52910 rc = sqlite3VdbeCursorMoveto(u.an.pC);
52911 if( rc ) goto abort_due_to_error;
52912 u.an.zRec = 0;
52913 u.an.pCrsr = u.an.pC->pCursor;
52914 if( u.an.pC->nullRow ){
52915 u.an.payloadSize = 0;
52916 }else if( u.an.pC->cacheStatus==p->cacheCtr ){
52917 u.an.payloadSize = u.an.pC->payloadSize;
52918 u.an.zRec = (char*)u.an.pC->aRow;
52919 }else if( u.an.pC->isIndex ){
52920 sqlite3BtreeKeySize(u.an.pCrsr, &u.an.payloadSize64);
52921 if( (u.an.payloadSize64 & SQLITE_MAX_U32)!=(u64)u.an.payloadSize64 ){
52922 rc = SQLITE_CORRUPT_BKPT;
52923 goto abort_due_to_error;
52924 }
52925 u.an.payloadSize = (u32)u.an.payloadSize64;
52926 }else{
52927 sqlite3BtreeDataSize(u.an.pCrsr, &u.an.payloadSize);
52928 }
52929 u.an.nField = u.an.pC->nField;
52930 }else{
52931 assert( u.an.pC->pseudoTable );
52932 /* The record is the sole entry of a pseudo-table */
52933 u.an.payloadSize = u.an.pC->nData;
52934 u.an.zRec = u.an.pC->pData;
52935 u.an.pC->cacheStatus = CACHE_STALE;
52936 assert( u.an.payloadSize==0 || u.an.zRec!=0 );
52937 u.an.nField = u.an.pC->nField;
52938 u.an.pCrsr = 0;
 
 
 
52939 }
52940
52941 /* If u.an.payloadSize is 0, then just store a NULL */
52942 if( u.an.payloadSize==0 ){
52943 assert( u.an.pDest->flags&MEM_Null );
52944 goto op_column_out;
52945 }
52946 assert( db->aLimit[SQLITE_LIMIT_LENGTH]>=0 );
52947 if( u.an.payloadSize > (u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
52948 goto too_big;
52949 }
52950
52951 assert( u.an.p2<u.an.nField );
52952
52953 /* Read and parse the table header. Store the results of the parse
52954 ** into the record header cache fields of the cursor.
52955 */
52956 u.an.aType = u.an.pC->aType;
52957 if( u.an.pC->cacheStatus==p->cacheCtr ){
52958 u.an.aOffset = u.an.pC->aOffset;
52959 }else{
52960 assert(u.an.aType);
52961 u.an.avail = 0;
52962 u.an.pC->aOffset = u.an.aOffset = &u.an.aType[u.an.nField];
52963 u.an.pC->payloadSize = u.an.payloadSize;
52964 u.an.pC->cacheStatus = p->cacheCtr;
52965
52966 /* Figure out how many bytes are in the header */
52967 if( u.an.zRec ){
52968 u.an.zData = u.an.zRec;
52969 }else{
52970 if( u.an.pC->isIndex ){
52971 u.an.zData = (char*)sqlite3BtreeKeyFetch(u.an.pCrsr, &u.an.avail);
52972 }else{
52973 u.an.zData = (char*)sqlite3BtreeDataFetch(u.an.pCrsr, &u.an.avail);
52974 }
52975 /* If KeyFetch()/DataFetch() managed to get the entire payload,
52976 ** save the payload in the u.an.pC->aRow cache. That will save us from
52977 ** having to make additional calls to fetch the content portion of
52978 ** the record.
52979 */
52980 assert( u.an.avail>=0 );
52981 if( u.an.payloadSize <= (u32)u.an.avail ){
52982 u.an.zRec = u.an.zData;
52983 u.an.pC->aRow = (u8*)u.an.zData;
52984 }else{
52985 u.an.pC->aRow = 0;
52986 }
52987 }
52988 /* The following assert is true in all cases accept when
52989 ** the database file has been corrupted externally.
52990 ** assert( u.an.zRec!=0 || u.an.avail>=u.an.payloadSize || u.an.avail>=9 ); */
52991 u.an.szHdr = getVarint32((u8*)u.an.zData, u.an.offset);
52992
52993 /* Make sure a corrupt database has not given us an oversize header.
52994 ** Do this now to avoid an oversize memory allocation.
52995 **
52996 ** Type entries can be between 1 and 5 bytes each. But 4 and 5 byte
@@ -52997,136 +52930,136 @@
52997 ** types use so much data space that there can only be 4096 and 32 of
52998 ** them, respectively. So the maximum header length results from a
52999 ** 3-byte type for each of the maximum of 32768 columns plus three
53000 ** extra bytes for the header length itself. 32768*3 + 3 = 98307.
53001 */
53002 if( u.an.offset > 98307 ){
53003 rc = SQLITE_CORRUPT_BKPT;
53004 goto op_column_out;
53005 }
53006
53007 /* Compute in u.an.len the number of bytes of data we need to read in order
53008 ** to get u.an.nField type values. u.an.offset is an upper bound on this. But
53009 ** u.an.nField might be significantly less than the true number of columns
53010 ** in the table, and in that case, 5*u.an.nField+3 might be smaller than u.an.offset.
53011 ** We want to minimize u.an.len in order to limit the size of the memory
53012 ** allocation, especially if a corrupt database file has caused u.an.offset
53013 ** to be oversized. Offset is limited to 98307 above. But 98307 might
53014 ** still exceed Robson memory allocation limits on some configurations.
53015 ** On systems that cannot tolerate large memory allocations, u.an.nField*5+3
53016 ** will likely be much smaller since u.an.nField will likely be less than
53017 ** 20 or so. This insures that Robson memory allocation limits are
53018 ** not exceeded even for corrupt database files.
53019 */
53020 u.an.len = u.an.nField*5 + 3;
53021 if( u.an.len > u.an.offset ) u.an.len = u.an.offset;
53022
53023 /* The KeyFetch() or DataFetch() above are fast and will get the entire
53024 ** record header in most cases. But they will fail to get the complete
53025 ** record header if the record header does not fit on a single page
53026 ** in the B-Tree. When that happens, use sqlite3VdbeMemFromBtree() to
53027 ** acquire the complete header text.
53028 */
53029 if( !u.an.zRec && u.an.avail<u.an.len ){
53030 u.an.sMem.flags = 0;
53031 u.an.sMem.db = 0;
53032 rc = sqlite3VdbeMemFromBtree(u.an.pCrsr, 0, u.an.len, u.an.pC->isIndex, &u.an.sMem);
53033 if( rc!=SQLITE_OK ){
53034 goto op_column_out;
53035 }
53036 u.an.zData = u.an.sMem.z;
53037 }
53038 u.an.zEndHdr = (u8 *)&u.an.zData[u.an.len];
53039 u.an.zIdx = (u8 *)&u.an.zData[u.an.szHdr];
53040
53041 /* Scan the header and use it to fill in the u.an.aType[] and u.an.aOffset[]
53042 ** arrays. u.an.aType[u.an.i] will contain the type integer for the u.an.i-th
53043 ** column and u.an.aOffset[u.an.i] will contain the u.an.offset from the beginning
53044 ** of the record to the start of the data for the u.an.i-th column
53045 */
53046 u.an.offset64 = u.an.offset;
53047 for(u.an.i=0; u.an.i<u.an.nField; u.an.i++){
53048 if( u.an.zIdx<u.an.zEndHdr ){
53049 u.an.aOffset[u.an.i] = (u32)u.an.offset64;
53050 u.an.zIdx += getVarint32(u.an.zIdx, u.an.aType[u.an.i]);
53051 u.an.offset64 += sqlite3VdbeSerialTypeLen(u.an.aType[u.an.i]);
53052 }else{
53053 /* If u.an.i is less that u.an.nField, then there are less fields in this
53054 ** record than SetNumColumns indicated there are columns in the
53055 ** table. Set the u.an.offset for any extra columns not present in
53056 ** the record to 0. This tells code below to store a NULL
53057 ** instead of deserializing a value from the record.
53058 */
53059 u.an.aOffset[u.an.i] = 0;
53060 }
53061 }
53062 sqlite3VdbeMemRelease(&u.an.sMem);
53063 u.an.sMem.flags = MEM_Null;
53064
53065 /* If we have read more header data than was contained in the header,
53066 ** or if the end of the last field appears to be past the end of the
53067 ** record, or if the end of the last field appears to be before the end
53068 ** of the record (when all fields present), then we must be dealing
53069 ** with a corrupt database.
53070 */
53071 if( (u.an.zIdx > u.an.zEndHdr)|| (u.an.offset64 > u.an.payloadSize)
53072 || (u.an.zIdx==u.an.zEndHdr && u.an.offset64!=(u64)u.an.payloadSize) ){
53073 rc = SQLITE_CORRUPT_BKPT;
53074 goto op_column_out;
53075 }
53076 }
53077
53078 /* Get the column information. If u.an.aOffset[u.an.p2] is non-zero, then
53079 ** deserialize the value from the record. If u.an.aOffset[u.an.p2] is zero,
53080 ** then there are not enough fields in the record to satisfy the
53081 ** request. In this case, set the value NULL or to P4 if P4 is
53082 ** a pointer to a Mem object.
53083 */
53084 if( u.an.aOffset[u.an.p2] ){
53085 assert( rc==SQLITE_OK );
53086 if( u.an.zRec ){
53087 sqlite3VdbeMemReleaseExternal(u.an.pDest);
53088 sqlite3VdbeSerialGet((u8 *)&u.an.zRec[u.an.aOffset[u.an.p2]], u.an.aType[u.an.p2], u.an.pDest);
53089 }else{
53090 u.an.len = sqlite3VdbeSerialTypeLen(u.an.aType[u.an.p2]);
53091 sqlite3VdbeMemMove(&u.an.sMem, u.an.pDest);
53092 rc = sqlite3VdbeMemFromBtree(u.an.pCrsr, u.an.aOffset[u.an.p2], u.an.len, u.an.pC->isIndex, &u.an.sMem);
53093 if( rc!=SQLITE_OK ){
53094 goto op_column_out;
53095 }
53096 u.an.zData = u.an.sMem.z;
53097 sqlite3VdbeSerialGet((u8*)u.an.zData, u.an.aType[u.an.p2], u.an.pDest);
53098 }
53099 u.an.pDest->enc = encoding;
53100 }else{
53101 if( pOp->p4type==P4_MEM ){
53102 sqlite3VdbeMemShallowCopy(u.an.pDest, pOp->p4.pMem, MEM_Static);
53103 }else{
53104 assert( u.an.pDest->flags&MEM_Null );
53105 }
53106 }
53107
53108 /* If we dynamically allocated space to hold the data (in the
53109 ** sqlite3VdbeMemFromBtree() call above) then transfer control of that
53110 ** dynamically allocated space over to the u.an.pDest structure.
53111 ** This prevents a memory copy.
53112 */
53113 if( u.an.sMem.zMalloc ){
53114 assert( u.an.sMem.z==u.an.sMem.zMalloc );
53115 assert( !(u.an.pDest->flags & MEM_Dyn) );
53116 assert( !(u.an.pDest->flags & (MEM_Blob|MEM_Str)) || u.an.pDest->z==u.an.sMem.z );
53117 u.an.pDest->flags &= ~(MEM_Ephem|MEM_Static);
53118 u.an.pDest->flags |= MEM_Term;
53119 u.an.pDest->z = u.an.sMem.z;
53120 u.an.pDest->zMalloc = u.an.sMem.zMalloc;
53121 }
53122
53123 rc = sqlite3VdbeMemMakeWriteable(u.an.pDest);
53124
53125 op_column_out:
53126 UPDATE_MAX_BLOBSIZE(u.an.pDest);
53127 REGISTER_TRACE(pOp->p3, u.an.pDest);
53128 break;
53129 }
53130
53131 /* Opcode: Affinity P1 P2 * P4 *
53132 **
@@ -53135,23 +53068,23 @@
53135 ** P4 is a string that is P2 characters long. The nth character of the
53136 ** string indicates the column affinity that should be used for the nth
53137 ** memory cell in the range.
53138 */
53139 case OP_Affinity: {
53140 #if 0 /* local variables moved into u.ao */
53141 char *zAffinity; /* The affinity to be applied */
53142 Mem *pData0; /* First register to which to apply affinity */
53143 Mem *pLast; /* Last register to which to apply affinity */
53144 Mem *pRec; /* Current register */
53145 #endif /* local variables moved into u.ao */
53146
53147 u.ao.zAffinity = pOp->p4.z;
53148 u.ao.pData0 = &p->aMem[pOp->p1];
53149 u.ao.pLast = &u.ao.pData0[pOp->p2-1];
53150 for(u.ao.pRec=u.ao.pData0; u.ao.pRec<=u.ao.pLast; u.ao.pRec++){
53151 ExpandBlob(u.ao.pRec);
53152 applyAffinity(u.ao.pRec, u.ao.zAffinity[u.ao.pRec-u.ao.pData0], encoding);
53153 }
53154 break;
53155 }
53156
53157 /* Opcode: MakeRecord P1 P2 P3 P4 *
@@ -53171,11 +53104,11 @@
53171 ** macros defined in sqliteInt.h.
53172 **
53173 ** If P4 is NULL then all index fields have the affinity NONE.
53174 */
53175 case OP_MakeRecord: {
53176 #if 0 /* local variables moved into u.ap */
53177 u8 *zNewRecord; /* A buffer to hold the data for the new record */
53178 Mem *pRec; /* The new record */
53179 u64 nData; /* Number of bytes of data space */
53180 int nHdr; /* Number of bytes of header space */
53181 i64 nByte; /* Data space required for this record */
@@ -53187,11 +53120,11 @@
53187 int nField; /* Number of fields in the record */
53188 char *zAffinity; /* The affinity string for the record */
53189 int file_format; /* File format to use for encoding */
53190 int i; /* Space used in zNewRecord[] */
53191 int len; /* Length of a field */
53192 #endif /* local variables moved into u.ap */
53193
53194 /* Assuming the record contains N fields, the record format looks
53195 ** like this:
53196 **
53197 ** ------------------------------------------------------------------------
@@ -53204,52 +53137,52 @@
53204 ** Each type field is a varint representing the serial type of the
53205 ** corresponding data element (see sqlite3VdbeSerialType()). The
53206 ** hdr-size field is also a varint which is the offset from the beginning
53207 ** of the record to data0.
53208 */
53209 u.ap.nData = 0; /* Number of bytes of data space */
53210 u.ap.nHdr = 0; /* Number of bytes of header space */
53211 u.ap.nByte = 0; /* Data space required for this record */
53212 u.ap.nZero = 0; /* Number of zero bytes at the end of the record */
53213 u.ap.nField = pOp->p1;
53214 u.ap.zAffinity = pOp->p4.z;
53215 assert( u.ap.nField>0 && pOp->p2>0 && pOp->p2+u.ap.nField<=p->nMem+1 );
53216 u.ap.pData0 = &p->aMem[u.ap.nField];
53217 u.ap.nField = pOp->p2;
53218 u.ap.pLast = &u.ap.pData0[u.ap.nField-1];
53219 u.ap.file_format = p->minWriteFileFormat;
53220
53221 /* Loop through the elements that will make up the record to figure
53222 ** out how much space is required for the new record.
53223 */
53224 for(u.ap.pRec=u.ap.pData0; u.ap.pRec<=u.ap.pLast; u.ap.pRec++){
53225 if( u.ap.zAffinity ){
53226 applyAffinity(u.ap.pRec, u.ap.zAffinity[u.ap.pRec-u.ap.pData0], encoding);
53227 }
53228 if( u.ap.pRec->flags&MEM_Zero && u.ap.pRec->n>0 ){
53229 sqlite3VdbeMemExpandBlob(u.ap.pRec);
53230 }
53231 u.ap.serial_type = sqlite3VdbeSerialType(u.ap.pRec, u.ap.file_format);
53232 u.ap.len = sqlite3VdbeSerialTypeLen(u.ap.serial_type);
53233 u.ap.nData += u.ap.len;
53234 u.ap.nHdr += sqlite3VarintLen(u.ap.serial_type);
53235 if( u.ap.pRec->flags & MEM_Zero ){
53236 /* Only pure zero-filled BLOBs can be input to this Opcode.
53237 ** We do not allow blobs with a prefix and a zero-filled tail. */
53238 u.ap.nZero += u.ap.pRec->u.nZero;
53239 }else if( u.ap.len ){
53240 u.ap.nZero = 0;
53241 }
53242 }
53243
53244 /* Add the initial header varint and total the size */
53245 u.ap.nHdr += u.ap.nVarint = sqlite3VarintLen(u.ap.nHdr);
53246 if( u.ap.nVarint<sqlite3VarintLen(u.ap.nHdr) ){
53247 u.ap.nHdr++;
53248 }
53249 u.ap.nByte = u.ap.nHdr+u.ap.nData-u.ap.nZero;
53250 if( u.ap.nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
53251 goto too_big;
53252 }
53253
53254 /* Make sure the output register has a buffer large enough to store
53255 ** the new record. The output register (pOp->p3) is not allowed to
@@ -53256,32 +53189,32 @@
53256 ** be one of the input registers (because the following call to
53257 ** sqlite3VdbeMemGrow() could clobber the value before it is used).
53258 */
53259 assert( pOp->p3<pOp->p1 || pOp->p3>=pOp->p1+pOp->p2 );
53260 pOut = &p->aMem[pOp->p3];
53261 if( sqlite3VdbeMemGrow(pOut, (int)u.ap.nByte, 0) ){
53262 goto no_mem;
53263 }
53264 u.ap.zNewRecord = (u8 *)pOut->z;
53265
53266 /* Write the record */
53267 u.ap.i = putVarint32(u.ap.zNewRecord, u.ap.nHdr);
53268 for(u.ap.pRec=u.ap.pData0; u.ap.pRec<=u.ap.pLast; u.ap.pRec++){
53269 u.ap.serial_type = sqlite3VdbeSerialType(u.ap.pRec, u.ap.file_format);
53270 u.ap.i += putVarint32(&u.ap.zNewRecord[u.ap.i], u.ap.serial_type); /* serial type */
53271 }
53272 for(u.ap.pRec=u.ap.pData0; u.ap.pRec<=u.ap.pLast; u.ap.pRec++){ /* serial data */
53273 u.ap.i += sqlite3VdbeSerialPut(&u.ap.zNewRecord[u.ap.i], (int)(u.ap.nByte-u.ap.i), u.ap.pRec,u.ap.file_format);
53274 }
53275 assert( u.ap.i==u.ap.nByte );
53276
53277 assert( pOp->p3>0 && pOp->p3<=p->nMem );
53278 pOut->n = (int)u.ap.nByte;
53279 pOut->flags = MEM_Blob | MEM_Dyn;
53280 pOut->xDel = 0;
53281 if( u.ap.nZero ){
53282 pOut->u.nZero = u.ap.nZero;
53283 pOut->flags |= MEM_Zero;
53284 }
53285 pOut->enc = SQLITE_UTF8; /* In case the blob is ever converted to text */
53286 REGISTER_TRACE(pOp->p3, pOut);
53287 UPDATE_MAX_BLOBSIZE(pOut);
@@ -53293,23 +53226,23 @@
53293 ** Store the number of entries (an integer value) in the table or index
53294 ** opened by cursor P1 in register P2
53295 */
53296 #ifndef SQLITE_OMIT_BTREECOUNT
53297 case OP_Count: { /* out2-prerelease */
53298 #if 0 /* local variables moved into u.aq */
53299 i64 nEntry;
53300 BtCursor *pCrsr;
53301 #endif /* local variables moved into u.aq */
53302
53303 u.aq.pCrsr = p->apCsr[pOp->p1]->pCursor;
53304 if( u.aq.pCrsr ){
53305 rc = sqlite3BtreeCount(u.aq.pCrsr, &u.aq.nEntry);
53306 }else{
53307 u.aq.nEntry = 0;
53308 }
53309 pOut->flags = MEM_Int;
53310 pOut->u.i = u.aq.nEntry;
53311 break;
53312 }
53313 #endif
53314
53315 /* Opcode: Statement P1 * * * *
@@ -53333,27 +53266,25 @@
53333 ** The statement is begun on the database file with index P1. The main
53334 ** database file has an index of 0 and the file used for temporary tables
53335 ** has an index of 1.
53336 */
53337 case OP_Statement: {
53338 #if 0 /* local variables moved into u.ar */
53339 int i;
53340 Btree *pBt;
53341 #endif /* local variables moved into u.ar */
53342 if( db->autoCommit==0 || db->activeVdbeCnt>1 ){
53343 u.ar.i = pOp->p1;
53344 assert( u.ar.i>=0 && u.ar.i<db->nDb );
53345 assert( db->aDb[u.ar.i].pBt!=0 );
53346 u.ar.pBt = db->aDb[u.ar.i].pBt;
53347 assert( sqlite3BtreeIsInTrans(u.ar.pBt) );
53348 assert( (p->btreeMask & (1<<u.ar.i))!=0 );
53349 if( p->iStatement==0 ){
53350 assert( db->nStatement>=0 && db->nSavepoint>=0 );
53351 db->nStatement++;
53352 p->iStatement = db->nSavepoint + db->nStatement;
53353 }
53354 rc = sqlite3BtreeBeginStmt(u.ar.pBt, p->iStatement);
53355 }
53356 break;
53357 }
53358
53359 /* Opcode: Savepoint P1 * * P4 *
@@ -53361,48 +53292,48 @@
53361 ** Open, release or rollback the savepoint named by parameter P4, depending
53362 ** on the value of P1. To open a new savepoint, P1==0. To release (commit) an
53363 ** existing savepoint, P1==1, or to rollback an existing savepoint P1==2.
53364 */
53365 case OP_Savepoint: {
53366 #if 0 /* local variables moved into u.as */
53367 int p1; /* Value of P1 operand */
53368 char *zName; /* Name of savepoint */
53369 int nName;
53370 Savepoint *pNew;
53371 Savepoint *pSavepoint;
53372 Savepoint *pTmp;
53373 int iSavepoint;
53374 int ii;
53375 #endif /* local variables moved into u.as */
53376
53377 u.as.p1 = pOp->p1;
53378 u.as.zName = pOp->p4.z;
53379
53380 /* Assert that the u.as.p1 parameter is valid. Also that if there is no open
53381 ** transaction, then there cannot be any savepoints.
53382 */
53383 assert( db->pSavepoint==0 || db->autoCommit==0 );
53384 assert( u.as.p1==SAVEPOINT_BEGIN||u.as.p1==SAVEPOINT_RELEASE||u.as.p1==SAVEPOINT_ROLLBACK );
53385 assert( db->pSavepoint || db->isTransactionSavepoint==0 );
53386 assert( checkSavepointCount(db) );
53387
53388 if( u.as.p1==SAVEPOINT_BEGIN ){
53389 if( db->writeVdbeCnt>0 ){
53390 /* A new savepoint cannot be created if there are active write
53391 ** statements (i.e. open read/write incremental blob handles).
53392 */
53393 sqlite3SetString(&p->zErrMsg, db, "cannot open savepoint - "
53394 "SQL statements in progress");
53395 rc = SQLITE_BUSY;
53396 }else{
53397 u.as.nName = sqlite3Strlen30(u.as.zName);
53398
53399 /* Create a new savepoint structure. */
53400 u.as.pNew = sqlite3DbMallocRaw(db, sizeof(Savepoint)+u.as.nName+1);
53401 if( u.as.pNew ){
53402 u.as.pNew->zName = (char *)&u.as.pNew[1];
53403 memcpy(u.as.pNew->zName, u.as.zName, u.as.nName+1);
53404
53405 /* If there is no open transaction, then mark this as a special
53406 ** "transaction savepoint". */
53407 if( db->autoCommit ){
53408 db->autoCommit = 0;
@@ -53410,49 +53341,49 @@
53410 }else{
53411 db->nSavepoint++;
53412 }
53413
53414 /* Link the new savepoint into the database handle's list. */
53415 u.as.pNew->pNext = db->pSavepoint;
53416 db->pSavepoint = u.as.pNew;
53417 }
53418 }
53419 }else{
53420 u.as.iSavepoint = 0;
53421
53422 /* Find the named savepoint. If there is no such savepoint, then an
53423 ** an error is returned to the user. */
53424 for(
53425 u.as.pSavepoint = db->pSavepoint;
53426 u.as.pSavepoint && sqlite3StrICmp(u.as.pSavepoint->zName, u.as.zName);
53427 u.as.pSavepoint = u.as.pSavepoint->pNext
53428 ){
53429 u.as.iSavepoint++;
53430 }
53431 if( !u.as.pSavepoint ){
53432 sqlite3SetString(&p->zErrMsg, db, "no such savepoint: %s", u.as.zName);
53433 rc = SQLITE_ERROR;
53434 }else if(
53435 db->writeVdbeCnt>0 || (u.as.p1==SAVEPOINT_ROLLBACK && db->activeVdbeCnt>1)
53436 ){
53437 /* It is not possible to release (commit) a savepoint if there are
53438 ** active write statements. It is not possible to rollback a savepoint
53439 ** if there are any active statements at all.
53440 */
53441 sqlite3SetString(&p->zErrMsg, db,
53442 "cannot %s savepoint - SQL statements in progress",
53443 (u.as.p1==SAVEPOINT_ROLLBACK ? "rollback": "release")
53444 );
53445 rc = SQLITE_BUSY;
53446 }else{
53447
53448 /* Determine whether or not this is a transaction savepoint. If so,
53449 ** and this is a RELEASE command, then the current transaction
53450 ** is committed.
53451 */
53452 int isTransaction = u.as.pSavepoint->pNext==0 && db->isTransactionSavepoint;
53453 if( isTransaction && u.as.p1==SAVEPOINT_RELEASE ){
53454 db->autoCommit = 1;
53455 if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
53456 p->pc = pc;
53457 db->autoCommit = 0;
53458 p->rc = rc = SQLITE_BUSY;
@@ -53459,37 +53390,37 @@
53459 goto vdbe_return;
53460 }
53461 db->isTransactionSavepoint = 0;
53462 rc = p->rc;
53463 }else{
53464 u.as.iSavepoint = db->nSavepoint - u.as.iSavepoint - 1;
53465 for(u.as.ii=0; u.as.ii<db->nDb; u.as.ii++){
53466 rc = sqlite3BtreeSavepoint(db->aDb[u.as.ii].pBt, u.as.p1, u.as.iSavepoint);
53467 if( rc!=SQLITE_OK ){
53468 goto abort_due_to_error;
53469 }
53470 }
53471 if( u.as.p1==SAVEPOINT_ROLLBACK && (db->flags&SQLITE_InternChanges)!=0 ){
53472 sqlite3ExpirePreparedStatements(db);
53473 sqlite3ResetInternalSchema(db, 0);
53474 }
53475 }
53476
53477 /* Regardless of whether this is a RELEASE or ROLLBACK, destroy all
53478 ** savepoints nested inside of the savepoint being operated on. */
53479 while( db->pSavepoint!=u.as.pSavepoint ){
53480 u.as.pTmp = db->pSavepoint;
53481 db->pSavepoint = u.as.pTmp->pNext;
53482 sqlite3DbFree(db, u.as.pTmp);
53483 db->nSavepoint--;
53484 }
53485
53486 /* If it is a RELEASE, then destroy the savepoint being operated on too */
53487 if( u.as.p1==SAVEPOINT_RELEASE ){
53488 assert( u.as.pSavepoint==db->pSavepoint );
53489 db->pSavepoint = u.as.pSavepoint->pNext;
53490 sqlite3DbFree(db, u.as.pSavepoint);
53491 if( !isTransaction ){
53492 db->nSavepoint--;
53493 }
53494 }
53495 }
@@ -53506,48 +53437,48 @@
53506 ** there are active writing VMs or active VMs that use shared cache.
53507 **
53508 ** This instruction causes the VM to halt.
53509 */
53510 case OP_AutoCommit: {
53511 #if 0 /* local variables moved into u.at */
53512 int desiredAutoCommit;
53513 int rollback;
53514 int turnOnAC;
53515 #endif /* local variables moved into u.at */
53516
53517 u.at.desiredAutoCommit = pOp->p1;
53518 u.at.rollback = pOp->p2;
53519 u.at.turnOnAC = u.at.desiredAutoCommit && !db->autoCommit;
53520 assert( u.at.desiredAutoCommit==1 || u.at.desiredAutoCommit==0 );
53521 assert( u.at.desiredAutoCommit==1 || u.at.rollback==0 );
53522 assert( db->activeVdbeCnt>0 ); /* At least this one VM is active */
53523
53524 if( u.at.turnOnAC && u.at.rollback && db->activeVdbeCnt>1 ){
53525 /* If this instruction implements a ROLLBACK and other VMs are
53526 ** still running, and a transaction is active, return an error indicating
53527 ** that the other VMs must complete first.
53528 */
53529 sqlite3SetString(&p->zErrMsg, db, "cannot u.at.rollback transaction - "
53530 "SQL statements in progress");
53531 rc = SQLITE_BUSY;
53532 }else if( u.at.turnOnAC && !u.at.rollback && db->writeVdbeCnt>1 ){
53533 /* If this instruction implements a COMMIT and other VMs are writing
53534 ** return an error indicating that the other VMs must complete first.
53535 */
53536 sqlite3SetString(&p->zErrMsg, db, "cannot commit transaction - "
53537 "SQL statements in progress");
53538 rc = SQLITE_BUSY;
53539 }else if( u.at.desiredAutoCommit!=db->autoCommit ){
53540 if( u.at.rollback ){
53541 assert( u.at.desiredAutoCommit==1 );
53542 sqlite3RollbackAll(db);
53543 db->autoCommit = 1;
53544 }else{
53545 db->autoCommit = (u8)u.at.desiredAutoCommit;
53546 if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
53547 p->pc = pc;
53548 db->autoCommit = (u8)(1-u.at.desiredAutoCommit);
53549 p->rc = rc = SQLITE_BUSY;
53550 goto vdbe_return;
53551 }
53552 }
53553 assert( db->nStatement==0 );
@@ -53558,12 +53489,12 @@
53558 rc = SQLITE_ERROR;
53559 }
53560 goto vdbe_return;
53561 }else{
53562 sqlite3SetString(&p->zErrMsg, db,
53563 (!u.at.desiredAutoCommit)?"cannot start a transaction within a transaction":(
53564 (u.at.rollback)?"cannot u.at.rollback - no transaction is active":
53565 "cannot commit - no transaction is active"));
53566
53567 rc = SQLITE_ERROR;
53568 }
53569 break;
@@ -53589,22 +53520,20 @@
53589 ** on the file.
53590 **
53591 ** If P2 is zero, then a read-lock is obtained on the database file.
53592 */
53593 case OP_Transaction: {
53594 #if 0 /* local variables moved into u.au */
53595 int i;
53596 Btree *pBt;
53597 #endif /* local variables moved into u.au */
53598
53599 u.au.i = pOp->p1;
53600 assert( u.au.i>=0 && u.au.i<db->nDb );
53601 assert( (p->btreeMask & (1<<u.au.i))!=0 );
53602 u.au.pBt = db->aDb[u.au.i].pBt;
53603
53604 if( u.au.pBt ){
53605 rc = sqlite3BtreeBeginTrans(u.au.pBt, pOp->p2);
53606 if( rc==SQLITE_BUSY ){
53607 p->pc = pc;
53608 p->rc = rc = SQLITE_BUSY;
53609 goto vdbe_return;
53610 }
@@ -53626,25 +53555,25 @@
53626 ** There must be a read-lock on the database (either a transaction
53627 ** must be started or there must be an open cursor) before
53628 ** executing this instruction.
53629 */
53630 case OP_ReadCookie: { /* out2-prerelease */
53631 #if 0 /* local variables moved into u.av */
53632 int iMeta;
53633 int iDb;
53634 int iCookie;
53635 #endif /* local variables moved into u.av */
53636
53637 u.av.iDb = pOp->p1;
53638 u.av.iCookie = pOp->p3;
53639 assert( pOp->p3<SQLITE_N_BTREE_META );
53640 assert( u.av.iDb>=0 && u.av.iDb<db->nDb );
53641 assert( db->aDb[u.av.iDb].pBt!=0 );
53642 assert( (p->btreeMask & (1<<u.av.iDb))!=0 );
53643
53644 rc = sqlite3BtreeGetMeta(db->aDb[u.av.iDb].pBt, u.av.iCookie, (u32 *)&u.av.iMeta);
53645 pOut->u.i = u.av.iMeta;
53646 MemSetTypeFlag(pOut, MEM_Int);
53647 break;
53648 }
53649
53650 /* Opcode: SetCookie P1 P2 P3 * *
@@ -53656,28 +53585,28 @@
53656 ** database file used to store temporary tables.
53657 **
53658 ** A transaction must be started before executing this opcode.
53659 */
53660 case OP_SetCookie: { /* in3 */
53661 #if 0 /* local variables moved into u.aw */
53662 Db *pDb;
53663 #endif /* local variables moved into u.aw */
53664 assert( pOp->p2<SQLITE_N_BTREE_META );
53665 assert( pOp->p1>=0 && pOp->p1<db->nDb );
53666 assert( (p->btreeMask & (1<<pOp->p1))!=0 );
53667 u.aw.pDb = &db->aDb[pOp->p1];
53668 assert( u.aw.pDb->pBt!=0 );
53669 sqlite3VdbeMemIntegerify(pIn3);
53670 /* See note about index shifting on OP_ReadCookie */
53671 rc = sqlite3BtreeUpdateMeta(u.aw.pDb->pBt, pOp->p2, (int)pIn3->u.i);
53672 if( pOp->p2==BTREE_SCHEMA_VERSION ){
53673 /* When the schema cookie changes, record the new cookie internally */
53674 u.aw.pDb->pSchema->schema_cookie = (int)pIn3->u.i;
53675 db->flags |= SQLITE_InternChanges;
53676 }else if( pOp->p2==BTREE_FILE_FORMAT ){
53677 /* Record changes in the file format */
53678 u.aw.pDb->pSchema->file_format = (u8)pIn3->u.i;
53679 }
53680 if( pOp->p1==1 ){
53681 /* Invalidate all prepared statements whenever the TEMP database
53682 ** schema is changed. Ticket #1644 */
53683 sqlite3ExpirePreparedStatements(db);
@@ -53700,24 +53629,24 @@
53700 ** Either a transaction needs to have been started or an OP_Open needs
53701 ** to be executed (to establish a read lock) before this opcode is
53702 ** invoked.
53703 */
53704 case OP_VerifyCookie: {
53705 #if 0 /* local variables moved into u.ax */
53706 int iMeta;
53707 Btree *pBt;
53708 #endif /* local variables moved into u.ax */
53709 assert( pOp->p1>=0 && pOp->p1<db->nDb );
53710 assert( (p->btreeMask & (1<<pOp->p1))!=0 );
53711 u.ax.pBt = db->aDb[pOp->p1].pBt;
53712 if( u.ax.pBt ){
53713 rc = sqlite3BtreeGetMeta(u.ax.pBt, BTREE_SCHEMA_VERSION, (u32 *)&u.ax.iMeta);
53714 }else{
53715 rc = SQLITE_OK;
53716 u.ax.iMeta = 0;
53717 }
53718 if( rc==SQLITE_OK && u.ax.iMeta!=pOp->p2 ){
53719 sqlite3DbFree(db, p->zErrMsg);
53720 p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed");
53721 /* If the schema-cookie from the database file matches the cookie
53722 ** stored with the in-memory representation of the schema, do
53723 ** not reload the schema from the database file.
@@ -53729,11 +53658,11 @@
53729 ** discard the database schema, as the user code implementing the
53730 ** v-table would have to be ready for the sqlite3_vtab structure itself
53731 ** to be invalidated whenever sqlite3_step() is called from within
53732 ** a v-table method.
53733 */
53734 if( db->aDb[pOp->p1].pSchema->schema_cookie!=u.ax.iMeta ){
53735 sqlite3ResetInternalSchema(db, pOp->p1);
53736 }
53737
53738 sqlite3ExpirePreparedStatements(db);
53739 rc = SQLITE_SCHEMA;
@@ -53790,105 +53719,104 @@
53790 **
53791 ** See also OpenRead.
53792 */
53793 case OP_OpenRead:
53794 case OP_OpenWrite: {
53795 #if 0 /* local variables moved into u.ay */
53796 int nField;
53797 KeyInfo *pKeyInfo;
53798 int i;
53799 int p2;
53800 int iDb;
53801 int wrFlag;
53802 Btree *pX;
53803 VdbeCursor *pCur;
53804 Db *pDb;
53805 int flags;
53806 #endif /* local variables moved into u.ay */
53807
53808 u.ay.nField = 0;
53809 u.ay.pKeyInfo = 0;
53810 u.ay.i = pOp->p1;
53811 u.ay.p2 = pOp->p2;
53812 u.ay.iDb = pOp->p3;
53813 assert( u.ay.iDb>=0 && u.ay.iDb<db->nDb );
53814 assert( (p->btreeMask & (1<<u.ay.iDb))!=0 );
53815 u.ay.pDb = &db->aDb[u.ay.iDb];
53816 u.ay.pX = u.ay.pDb->pBt;
53817 assert( u.ay.pX!=0 );
53818 if( pOp->opcode==OP_OpenWrite ){
53819 u.ay.wrFlag = 1;
53820 if( u.ay.pDb->pSchema->file_format < p->minWriteFileFormat ){
53821 p->minWriteFileFormat = u.ay.pDb->pSchema->file_format;
53822 }
53823 }else{
53824 u.ay.wrFlag = 0;
53825 }
53826 if( pOp->p5 ){
53827 assert( u.ay.p2>0 );
53828 assert( u.ay.p2<=p->nMem );
53829 pIn2 = &p->aMem[u.ay.p2];
53830 sqlite3VdbeMemIntegerify(pIn2);
53831 u.ay.p2 = (int)pIn2->u.i;
53832 if( u.ay.p2<2 ) {
53833 rc = SQLITE_CORRUPT_BKPT;
53834 goto abort_due_to_error;
53835 }
53836 }
53837 assert( u.ay.i>=0 );
53838 if( pOp->p4type==P4_KEYINFO ){
53839 u.ay.pKeyInfo = pOp->p4.pKeyInfo;
53840 u.ay.pKeyInfo->enc = ENC(p->db);
53841 u.ay.nField = u.ay.pKeyInfo->nField+1;
53842 }else if( pOp->p4type==P4_INT32 ){
53843 u.ay.nField = pOp->p4.i;
53844 }
53845 u.ay.pCur = allocateCursor(p, u.ay.i, u.ay.nField, u.ay.iDb, 1);
53846 if( u.ay.pCur==0 ) goto no_mem;
53847 u.ay.pCur->nullRow = 1;
53848 rc = sqlite3BtreeCursor(u.ay.pX, u.ay.p2, u.ay.wrFlag, u.ay.pKeyInfo, u.ay.pCur->pCursor);
53849 u.ay.pCur->pKeyInfo = u.ay.pKeyInfo;
53850
53851 switch( rc ){
53852 case SQLITE_BUSY: {
53853 p->pc = pc;
53854 p->rc = rc = SQLITE_BUSY;
53855 goto vdbe_return;
53856 }
53857 case SQLITE_OK: {
53858 u.ay.flags = sqlite3BtreeFlags(u.ay.pCur->pCursor);
53859 /* Sanity checking. Only the lower four bits of the u.ay.flags byte should
53860 ** be used. Bit 3 (mask 0x08) is unpredictable. The lower 3 bits
53861 ** (mask 0x07) should be either 5 (intkey+leafdata for tables) or
53862 ** 2 (zerodata for indices). If these conditions are not met it can
53863 ** only mean that we are dealing with a corrupt database file
53864 */
53865 if( (u.ay.flags & 0xf0)!=0 || ((u.ay.flags & 0x07)!=5 && (u.ay.flags & 0x07)!=2) ){
53866 rc = SQLITE_CORRUPT_BKPT;
53867 goto abort_due_to_error;
53868 }
53869 u.ay.pCur->isTable = (u.ay.flags & BTREE_INTKEY)!=0 ?1:0;
53870 u.ay.pCur->isIndex = (u.ay.flags & BTREE_ZERODATA)!=0 ?1:0;
53871 /* If P4==0 it means we are expected to open a table. If P4!=0 then
53872 ** we expect to be opening an index. If this is not what happened,
53873 ** then the database is corrupt
53874 */
53875 if( (u.ay.pCur->isTable && pOp->p4type==P4_KEYINFO)
53876 || (u.ay.pCur->isIndex && pOp->p4type!=P4_KEYINFO) ){
53877 rc = SQLITE_CORRUPT_BKPT;
53878 goto abort_due_to_error;
53879 }
53880 break;
53881 }
53882 case SQLITE_EMPTY: {
53883 u.ay.pCur->isTable = pOp->p4type!=P4_KEYINFO;
53884 u.ay.pCur->isIndex = !u.ay.pCur->isTable;
53885 u.ay.pCur->pCursor = 0;
53886 rc = SQLITE_OK;
53887 break;
53888 }
53889 default: {
 
53890 goto abort_due_to_error;
53891 }
53892 }
53893 break;
53894 }
@@ -53910,30 +53838,28 @@
53910 ** to a TEMP table at the SQL level, or to a table opened by
53911 ** this opcode. Then this opcode was call OpenVirtual. But
53912 ** that created confusion with the whole virtual-table idea.
53913 */
53914 case OP_OpenEphemeral: {
53915 #if 0 /* local variables moved into u.az */
53916 int i;
53917 VdbeCursor *pCx;
53918 #endif /* local variables moved into u.az */
53919 static const int openFlags =
53920 SQLITE_OPEN_READWRITE |
53921 SQLITE_OPEN_CREATE |
53922 SQLITE_OPEN_EXCLUSIVE |
53923 SQLITE_OPEN_DELETEONCLOSE |
53924 SQLITE_OPEN_TRANSIENT_DB;
53925
53926 u.az.i = pOp->p1;
53927 assert( u.az.i>=0 );
53928 u.az.pCx = allocateCursor(p, u.az.i, pOp->p2, -1, 1);
53929 if( u.az.pCx==0 ) goto no_mem;
53930 u.az.pCx->nullRow = 1;
53931 rc = sqlite3BtreeFactory(db, 0, 1, SQLITE_DEFAULT_TEMP_CACHE_SIZE, openFlags,
53932 &u.az.pCx->pBt);
53933 if( rc==SQLITE_OK ){
53934 rc = sqlite3BtreeBeginTrans(u.az.pCx->pBt, 1);
53935 }
53936 if( rc==SQLITE_OK ){
53937 /* If a transient index is required, create it by calling
53938 ** sqlite3BtreeCreateTable() with the BTREE_ZERODATA flag before
53939 ** opening it. If a transient table is required, just use the
@@ -53940,25 +53866,25 @@
53940 ** automatically created table with root-page 1 (an INTKEY table).
53941 */
53942 if( pOp->p4.pKeyInfo ){
53943 int pgno;
53944 assert( pOp->p4type==P4_KEYINFO );
53945 rc = sqlite3BtreeCreateTable(u.az.pCx->pBt, &pgno, BTREE_ZERODATA);
53946 if( rc==SQLITE_OK ){
53947 assert( pgno==MASTER_ROOT+1 );
53948 rc = sqlite3BtreeCursor(u.az.pCx->pBt, pgno, 1,
53949 (KeyInfo*)pOp->p4.z, u.az.pCx->pCursor);
53950 u.az.pCx->pKeyInfo = pOp->p4.pKeyInfo;
53951 u.az.pCx->pKeyInfo->enc = ENC(p->db);
53952 }
53953 u.az.pCx->isTable = 0;
53954 }else{
53955 rc = sqlite3BtreeCursor(u.az.pCx->pBt, MASTER_ROOT, 1, 0, u.az.pCx->pCursor);
53956 u.az.pCx->isTable = 1;
53957 }
53958 }
53959 u.az.pCx->isIndex = !u.az.pCx->isTable;
53960 break;
53961 }
53962
53963 /* Opcode: OpenPseudo P1 P2 P3 * *
53964 **
@@ -53982,40 +53908,34 @@
53982 **
53983 ** P3 is the number of fields in the records that will be stored by
53984 ** the pseudo-table.
53985 */
53986 case OP_OpenPseudo: {
53987 #if 0 /* local variables moved into u.ba */
53988 int i;
53989 VdbeCursor *pCx;
53990 #endif /* local variables moved into u.ba */
53991
53992 u.ba.i = pOp->p1;
53993 assert( u.ba.i>=0 );
53994 u.ba.pCx = allocateCursor(p, u.ba.i, pOp->p3, -1, 0);
53995 if( u.ba.pCx==0 ) goto no_mem;
53996 u.ba.pCx->nullRow = 1;
53997 u.ba.pCx->pseudoTable = 1;
53998 u.ba.pCx->ephemPseudoTable = (u8)pOp->p2;
53999 u.ba.pCx->isTable = 1;
54000 u.ba.pCx->isIndex = 0;
54001 break;
54002 }
54003
54004 /* Opcode: Close P1 * * * *
54005 **
54006 ** Close a cursor previously opened as P1. If P1 is not
54007 ** currently open, this instruction is a no-op.
54008 */
54009 case OP_Close: {
54010 #if 0 /* local variables moved into u.bb */
54011 int i;
54012 #endif /* local variables moved into u.bb */
54013 u.bb.i = pOp->p1;
54014 assert( u.bb.i>=0 && u.bb.i<p->nCursor );
54015 sqlite3VdbeFreeCursor(p, p->apCsr[u.bb.i]);
54016 p->apCsr[u.bb.i] = 0;
54017 break;
54018 }
54019
54020 /* Opcode: SeekGe P1 P2 P3 P4 *
54021 **
@@ -54071,35 +53991,33 @@
54071 */
54072 case OP_SeekLt: /* jump, in3 */
54073 case OP_SeekLe: /* jump, in3 */
54074 case OP_SeekGe: /* jump, in3 */
54075 case OP_SeekGt: { /* jump, in3 */
54076 #if 0 /* local variables moved into u.bc */
54077 int i;
54078 int res;
54079 int oc;
54080 VdbeCursor *pC;
54081 UnpackedRecord r;
54082 int nField;
54083 i64 iKey; /* The rowid we are to seek to */
54084 #endif /* local variables moved into u.bc */
54085
54086 u.bc.i = pOp->p1;
54087 assert( u.bc.i>=0 && u.bc.i<p->nCursor );
54088 assert( pOp->p2!=0 );
54089 u.bc.pC = p->apCsr[u.bc.i];
54090 assert( u.bc.pC!=0 );
54091 if( u.bc.pC->pCursor!=0 ){
54092 u.bc.oc = pOp->opcode;
54093 u.bc.pC->nullRow = 0;
54094 if( u.bc.pC->isTable ){
54095 /* The input value in P3 might be of any type: integer, real, string,
54096 ** blob, or NULL. But it needs to be an integer before we can do
54097 ** the seek, so covert it. */
54098 applyNumericAffinity(pIn3);
54099 u.bc.iKey = sqlite3VdbeIntValue(pIn3);
54100 u.bc.pC->rowidIsValid = 0;
54101
54102 /* If the P3 value could not be converted into an integer without
54103 ** loss of information, then special processing is required... */
54104 if( (pIn3->flags & MEM_Int)==0 ){
54105 if( (pIn3->flags & MEM_Real)==0 ){
@@ -54110,99 +54028,100 @@
54110 }
54111 /* If we reach this point, then the P3 value must be a floating
54112 ** point number. */
54113 assert( (pIn3->flags & MEM_Real)!=0 );
54114
54115 if( u.bc.iKey==SMALLEST_INT64 && (pIn3->r<(double)u.bc.iKey || pIn3->r>0) ){
54116 /* The P3 value is to large in magnitude to be expressed as an
54117 ** integer. */
54118 u.bc.res = 1;
54119 if( pIn3->r<0 ){
54120 if( u.bc.oc==OP_SeekGt || u.bc.oc==OP_SeekGe ){
54121 rc = sqlite3BtreeFirst(u.bc.pC->pCursor, &u.bc.res);
54122 if( rc!=SQLITE_OK ) goto abort_due_to_error;
54123 }
54124 }else{
54125 if( u.bc.oc==OP_SeekLt || u.bc.oc==OP_SeekLe ){
54126 rc = sqlite3BtreeLast(u.bc.pC->pCursor, &u.bc.res);
54127 if( rc!=SQLITE_OK ) goto abort_due_to_error;
54128 }
54129 }
54130 if( u.bc.res ){
54131 pc = pOp->p2 - 1;
54132 }
54133 break;
54134 }else if( u.bc.oc==OP_SeekLt || u.bc.oc==OP_SeekGe ){
54135 /* Use the ceiling() function to convert real->int */
54136 if( pIn3->r > (double)u.bc.iKey ) u.bc.iKey++;
54137 }else{
54138 /* Use the floor() function to convert real->int */
54139 assert( u.bc.oc==OP_SeekLe || u.bc.oc==OP_SeekGt );
54140 if( pIn3->r < (double)u.bc.iKey ) u.bc.iKey--;
54141 }
54142 }
54143 rc = sqlite3BtreeMovetoUnpacked(u.bc.pC->pCursor, 0, (u64)u.bc.iKey, 0, &u.bc.res);
54144 if( rc!=SQLITE_OK ){
54145 goto abort_due_to_error;
54146 }
54147 if( u.bc.res==0 ){
54148 u.bc.pC->rowidIsValid = 1;
54149 u.bc.pC->lastRowid = u.bc.iKey;
54150 }
54151 }else{
54152 u.bc.nField = pOp->p4.i;
54153 assert( pOp->p4type==P4_INT32 );
54154 assert( u.bc.nField>0 );
54155 u.bc.r.pKeyInfo = u.bc.pC->pKeyInfo;
54156 u.bc.r.nField = (u16)u.bc.nField;
54157 if( u.bc.oc==OP_SeekGt || u.bc.oc==OP_SeekLe ){
54158 u.bc.r.flags = UNPACKED_INCRKEY;
54159 }else{
54160 u.bc.r.flags = 0;
54161 }
54162 u.bc.r.aMem = &p->aMem[pOp->p3];
54163 rc = sqlite3BtreeMovetoUnpacked(u.bc.pC->pCursor, &u.bc.r, 0, 0, &u.bc.res);
54164 if( rc!=SQLITE_OK ){
54165 goto abort_due_to_error;
54166 }
54167 u.bc.pC->rowidIsValid = 0;
54168 }
54169 u.bc.pC->deferredMoveto = 0;
54170 u.bc.pC->cacheStatus = CACHE_STALE;
54171 #ifdef SQLITE_TEST
54172 sqlite3_search_count++;
54173 #endif
54174 if( u.bc.oc==OP_SeekGe || u.bc.oc==OP_SeekGt ){
54175 if( u.bc.res<0 || (u.bc.res==0 && u.bc.oc==OP_SeekGt) ){
54176 rc = sqlite3BtreeNext(u.bc.pC->pCursor, &u.bc.res);
54177 if( rc!=SQLITE_OK ) goto abort_due_to_error;
54178 u.bc.pC->rowidIsValid = 0;
54179 }else{
54180 u.bc.res = 0;
54181 }
54182 }else{
54183 assert( u.bc.oc==OP_SeekLt || u.bc.oc==OP_SeekLe );
54184 if( u.bc.res>0 || (u.bc.res==0 && u.bc.oc==OP_SeekLt) ){
54185 rc = sqlite3BtreePrevious(u.bc.pC->pCursor, &u.bc.res);
54186 if( rc!=SQLITE_OK ) goto abort_due_to_error;
54187 u.bc.pC->rowidIsValid = 0;
54188 }else{
54189 /* u.bc.res might be negative because the table is empty. Check to
54190 ** see if this is the case.
54191 */
54192 u.bc.res = sqlite3BtreeEof(u.bc.pC->pCursor);
54193 }
54194 }
54195 assert( pOp->p2>0 );
54196 if( u.bc.res ){
54197 pc = pOp->p2 - 1;
54198 }
54199 }else if( !u.bc.pC->pseudoTable ){
54200 /* This happens when attempting to open the sqlite3_master table
54201 ** for read access returns SQLITE_EMPTY. In this case always
54202 ** take the jump (since there are no records in the table).
54203 */
 
54204 pc = pOp->p2 - 1;
54205 }
54206 break;
54207 }
54208
@@ -54214,25 +54133,23 @@
54214 ** This is actually a deferred seek. Nothing actually happens until
54215 ** the cursor is used to read a record. That way, if no reads
54216 ** occur, no unnecessary I/O happens.
54217 */
54218 case OP_Seek: { /* in2 */
54219 #if 0 /* local variables moved into u.bd */
54220 int i;
54221 VdbeCursor *pC;
54222 #endif /* local variables moved into u.bd */
54223
54224 u.bd.i = pOp->p1;
54225 assert( u.bd.i>=0 && u.bd.i<p->nCursor );
54226 u.bd.pC = p->apCsr[u.bd.i];
54227 assert( u.bd.pC!=0 );
54228 if( u.bd.pC->pCursor!=0 ){
54229 assert( u.bd.pC->isTable );
54230 u.bd.pC->nullRow = 0;
54231 u.bd.pC->movetoTarget = sqlite3VdbeIntValue(pIn2);
54232 u.bd.pC->rowidIsValid = 0;
54233 u.bd.pC->deferredMoveto = 1;
54234 }
54235 break;
54236 }
54237
54238
@@ -54266,48 +54183,47 @@
54266 **
54267 ** See also: Found, NotExists, IsUnique
54268 */
54269 case OP_NotFound: /* jump, in3 */
54270 case OP_Found: { /* jump, in3 */
54271 #if 0 /* local variables moved into u.be */
54272 int i;
54273 int alreadyExists;
54274 VdbeCursor *pC;
54275 int res;
54276 UnpackedRecord *pIdxKey;
54277 char aTempRec[ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*3 + 7];
54278 #endif /* local variables moved into u.be */
54279
54280 u.be.i = pOp->p1;
54281 u.be.alreadyExists = 0;
54282 assert( u.be.i>=0 && u.be.i<p->nCursor );
54283 assert( p->apCsr[u.be.i]!=0 );
54284 if( (u.be.pC = p->apCsr[u.be.i])->pCursor!=0 ){
54285
54286 assert( u.be.pC->isTable==0 );
54287 assert( pIn3->flags & MEM_Blob );
54288 u.be.pIdxKey = sqlite3VdbeRecordUnpack(u.be.pC->pKeyInfo, pIn3->n, pIn3->z,
54289 u.be.aTempRec, sizeof(u.be.aTempRec));
54290 if( u.be.pIdxKey==0 ){
54291 goto no_mem;
54292 }
54293 if( pOp->opcode==OP_Found ){
54294 u.be.pIdxKey->flags |= UNPACKED_PREFIX_MATCH;
54295 }
54296 rc = sqlite3BtreeMovetoUnpacked(u.be.pC->pCursor, u.be.pIdxKey, 0, 0, &u.be.res);
54297 sqlite3VdbeDeleteUnpackedRecord(u.be.pIdxKey);
54298 if( rc!=SQLITE_OK ){
54299 break;
54300 }
54301 u.be.alreadyExists = (u.be.res==0);
54302 u.be.pC->deferredMoveto = 0;
54303 u.be.pC->cacheStatus = CACHE_STALE;
54304 }
54305 if( pOp->opcode==OP_Found ){
54306 if( u.be.alreadyExists ) pc = pOp->p2 - 1;
54307 }else{
54308 if( !u.be.alreadyExists ) pc = pOp->p2 - 1;
54309 }
54310 break;
54311 }
54312
54313 /* Opcode: IsUnique P1 P2 P3 P4 *
@@ -54334,63 +54250,63 @@
54334 ** instruction.
54335 **
54336 ** See also: NotFound, NotExists, Found
54337 */
54338 case OP_IsUnique: { /* jump, in3 */
54339 #if 0 /* local variables moved into u.bf */
54340 u16 ii;
54341 VdbeCursor *pCx;
54342 BtCursor *pCrsr;
54343 u16 nField;
54344 Mem *aMem;
54345 UnpackedRecord r; /* B-Tree index search key */
54346 i64 R; /* Rowid stored in register P3 */
54347 #endif /* local variables moved into u.bf */
54348
54349 u.bf.aMem = &p->aMem[pOp->p4.i];
54350 /* Assert that the values of parameters P1 and P4 are in range. */
54351 assert( pOp->p4type==P4_INT32 );
54352 assert( pOp->p4.i>0 && pOp->p4.i<=p->nMem );
54353 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
54354
54355 /* Find the index cursor. */
54356 u.bf.pCx = p->apCsr[pOp->p1];
54357 assert( u.bf.pCx->deferredMoveto==0 );
54358 u.bf.pCx->seekResult = 0;
54359 u.bf.pCx->cacheStatus = CACHE_STALE;
54360 u.bf.pCrsr = u.bf.pCx->pCursor;
54361
54362 /* If any of the values are NULL, take the jump. */
54363 u.bf.nField = u.bf.pCx->pKeyInfo->nField;
54364 for(u.bf.ii=0; u.bf.ii<u.bf.nField; u.bf.ii++){
54365 if( u.bf.aMem[u.bf.ii].flags & MEM_Null ){
54366 pc = pOp->p2 - 1;
54367 u.bf.pCrsr = 0;
54368 break;
54369 }
54370 }
54371 assert( (u.bf.aMem[u.bf.nField].flags & MEM_Null)==0 );
54372
54373 if( u.bf.pCrsr!=0 ){
54374 /* Populate the index search key. */
54375 u.bf.r.pKeyInfo = u.bf.pCx->pKeyInfo;
54376 u.bf.r.nField = u.bf.nField + 1;
54377 u.bf.r.flags = UNPACKED_PREFIX_SEARCH;
54378 u.bf.r.aMem = u.bf.aMem;
54379
54380 /* Extract the value of u.bf.R from register P3. */
54381 sqlite3VdbeMemIntegerify(pIn3);
54382 u.bf.R = pIn3->u.i;
54383
54384 /* Search the B-Tree index. If no conflicting record is found, jump
54385 ** to P2. Otherwise, copy the rowid of the conflicting record to
54386 ** register P3 and fall through to the next instruction. */
54387 rc = sqlite3BtreeMovetoUnpacked(u.bf.pCrsr, &u.bf.r, 0, 0, &u.bf.pCx->seekResult);
54388 if( (u.bf.r.flags & UNPACKED_PREFIX_SEARCH) || u.bf.r.rowid==u.bf.R ){
54389 pc = pOp->p2 - 1;
54390 }else{
54391 pIn3->u.i = u.bf.r.rowid;
54392 }
54393 }
54394 break;
54395 }
54396
@@ -54407,45 +54323,46 @@
54407 ** P1 is an index.
54408 **
54409 ** See also: Found, NotFound, IsUnique
54410 */
54411 case OP_NotExists: { /* jump, in3 */
54412 #if 0 /* local variables moved into u.bg */
54413 int i;
54414 VdbeCursor *pC;
54415 BtCursor *pCrsr;
54416 int res;
54417 u64 iKey;
54418 #endif /* local variables moved into u.bg */
54419
54420 u.bg.i = pOp->p1;
54421 assert( u.bg.i>=0 && u.bg.i<p->nCursor );
54422 assert( p->apCsr[u.bg.i]!=0 );
54423 if( (u.bg.pCrsr = (u.bg.pC = p->apCsr[u.bg.i])->pCursor)!=0 ){
54424 u.bg.res = 0;
54425 assert( pIn3->flags & MEM_Int );
54426 assert( p->apCsr[u.bg.i]->isTable );
54427 u.bg.iKey = intToKey(pIn3->u.i);
54428 rc = sqlite3BtreeMovetoUnpacked(u.bg.pCrsr, 0, u.bg.iKey, 0, &u.bg.res);
54429 u.bg.pC->lastRowid = pIn3->u.i;
54430 u.bg.pC->rowidIsValid = u.bg.res==0 ?1:0;
54431 u.bg.pC->nullRow = 0;
54432 u.bg.pC->cacheStatus = CACHE_STALE;
54433 u.bg.pC->deferredMoveto = 0;
54434 if( u.bg.res!=0 ){
 
54435 pc = pOp->p2 - 1;
54436 assert( u.bg.pC->rowidIsValid==0 );
54437 }
54438 u.bg.pC->seekResult = u.bg.res;
54439 }else if( !u.bg.pC->pseudoTable ){
54440 /* This happens when an attempt to open a read cursor on the
54441 ** sqlite_master table returns SQLITE_EMPTY.
54442 */
54443 assert( u.bg.pC->isTable );
 
54444 pc = pOp->p2 - 1;
54445 assert( u.bg.pC->rowidIsValid==0 );
54446 u.bg.pC->seekResult = 0;
54447 }
54448 break;
54449 }
54450
54451 /* Opcode: Sequence P1 P2 * * *
@@ -54454,14 +54371,13 @@
54454 ** Write the sequence number into register P2.
54455 ** The sequence number on the cursor is incremented after this
54456 ** instruction.
54457 */
54458 case OP_Sequence: { /* out2-prerelease */
54459 int i = pOp->p1;
54460 assert( i>=0 && i<p->nCursor );
54461 assert( p->apCsr[i]!=0 );
54462 pOut->u.i = p->apCsr[i]->seqCount++;
54463 MemSetTypeFlag(pOut, MEM_Int);
54464 break;
54465 }
54466
54467
@@ -54478,28 +54394,24 @@
54478 ** error is generated. The P3 register is updated with the generated
54479 ** record number. This P3 mechanism is used to help implement the
54480 ** AUTOINCREMENT feature.
54481 */
54482 case OP_NewRowid: { /* out2-prerelease */
54483 #if 0 /* local variables moved into u.bh */
54484 int i;
54485 i64 v;
54486 VdbeCursor *pC;
54487 int res;
54488 int rx;
54489 int cnt;
54490 i64 x;
54491 Mem *pMem;
54492 #endif /* local variables moved into u.bh */
54493
54494 u.bh.i = pOp->p1;
54495 u.bh.v = 0;
54496 u.bh.res = 0;
54497 u.bh.rx = SQLITE_OK;
54498 assert( u.bh.i>=0 && u.bh.i<p->nCursor );
54499 assert( p->apCsr[u.bh.i]!=0 );
54500 if( (u.bh.pC = p->apCsr[u.bh.i])->pCursor==0 ){
54501 /* The zero initialization above is all that is needed */
54502 }else{
54503 /* The next rowid or record number (different terms for the same
54504 ** thing) is obtained in a two-step algorithm.
54505 **
@@ -54509,38 +54421,14 @@
54509 ** probabilistic algorithm
54510 **
54511 ** The second algorithm is to select a rowid at random and see if
54512 ** it already exists in the table. If it does not exist, we have
54513 ** succeeded. If the random rowid does exist, we select a new one
54514 ** and try again, up to 1000 times.
54515 **
54516 ** For a table with less than 2 billion entries, the probability
54517 ** of not finding a unused rowid is about 1.0e-300. This is a
54518 ** non-zero probability, but it is still vanishingly small and should
54519 ** never cause a problem. You are much, much more likely to have a
54520 ** hardware failure than for this algorithm to fail.
54521 **
54522 ** The analysis in the previous paragraph assumes that you have a good
54523 ** source of random numbers. Is a library function like lrand48()
54524 ** good enough? Maybe. Maybe not. It's hard to know whether there
54525 ** might be subtle bugs is some implementations of lrand48() that
54526 ** could cause problems. To avoid uncertainty, SQLite uses its own
54527 ** random number generator based on the RC4 algorithm.
54528 **
54529 ** To promote locality of reference for repetitive inserts, the
54530 ** first few attempts at choosing a random rowid pick values just a little
54531 ** larger than the previous rowid. This has been shown experimentally
54532 ** to double the speed of the COPY operation.
54533 */
54534 u.bh.cnt = 0;
54535 if( (sqlite3BtreeFlags(u.bh.pC->pCursor)&(BTREE_INTKEY|BTREE_ZERODATA)) !=
54536 BTREE_INTKEY ){
54537 rc = SQLITE_CORRUPT_BKPT;
54538 goto abort_due_to_error;
54539 }
54540 assert( (sqlite3BtreeFlags(u.bh.pC->pCursor) & BTREE_INTKEY)!=0 );
54541 assert( (sqlite3BtreeFlags(u.bh.pC->pCursor) & BTREE_ZERODATA)==0 );
54542
54543 #ifdef SQLITE_32BIT_ROWID
54544 # define MAX_ROWID 0x7fffffff
54545 #else
54546 /* Some compilers complain about constants of the form 0x7fffffffffffffff.
@@ -54548,78 +54436,76 @@
54548 ** to provide the constant while making all compilers happy.
54549 */
54550 # define MAX_ROWID (i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff )
54551 #endif
54552
54553 if( !u.bh.pC->useRandomRowid ){
54554 u.bh.v = sqlite3BtreeGetCachedRowid(u.bh.pC->pCursor);
54555 if( u.bh.v==0 ){
54556 rc = sqlite3BtreeLast(u.bh.pC->pCursor, &u.bh.res);
54557 if( rc!=SQLITE_OK ){
54558 goto abort_due_to_error;
54559 }
54560 if( u.bh.res ){
54561 u.bh.v = 1;
54562 }else{
54563 sqlite3BtreeKeySize(u.bh.pC->pCursor, &u.bh.v);
54564 u.bh.v = keyToInt(u.bh.v);
54565 if( u.bh.v==MAX_ROWID ){
54566 u.bh.pC->useRandomRowid = 1;
54567 }else{
54568 u.bh.v++;
54569 }
54570 }
54571 }
54572
54573 #ifndef SQLITE_OMIT_AUTOINCREMENT
54574 if( pOp->p3 ){
54575 assert( pOp->p3>0 && pOp->p3<=p->nMem ); /* P3 is a valid memory cell */
54576 u.bh.pMem = &p->aMem[pOp->p3];
54577 REGISTER_TRACE(pOp->p3, u.bh.pMem);
54578 sqlite3VdbeMemIntegerify(u.bh.pMem);
54579 assert( (u.bh.pMem->flags & MEM_Int)!=0 ); /* mem(P3) holds an integer */
54580 if( u.bh.pMem->u.i==MAX_ROWID || u.bh.pC->useRandomRowid ){
54581 rc = SQLITE_FULL;
54582 goto abort_due_to_error;
54583 }
54584 if( u.bh.v<u.bh.pMem->u.i+1 ){
54585 u.bh.v = u.bh.pMem->u.i + 1;
54586 }
54587 u.bh.pMem->u.i = u.bh.v;
54588 }
54589 #endif
54590
54591 sqlite3BtreeSetCachedRowid(u.bh.pC->pCursor, u.bh.v<MAX_ROWID ? u.bh.v+1 : 0);
54592 }
54593 if( u.bh.pC->useRandomRowid ){
54594 assert( pOp->p3==0 ); /* SQLITE_FULL must have occurred prior to this */
54595 u.bh.v = db->priorNewRowid;
54596 u.bh.cnt = 0;
54597 do{
54598 if( u.bh.cnt==0 && (u.bh.v&0xffffff)==u.bh.v ){
54599 u.bh.v++;
54600 }else{
54601 sqlite3_randomness(sizeof(u.bh.v), &u.bh.v);
54602 if( u.bh.cnt<5 ) u.bh.v &= 0xffffff;
54603 }
54604 if( u.bh.v==0 ) continue;
54605 u.bh.x = intToKey(u.bh.v);
54606 u.bh.rx = sqlite3BtreeMovetoUnpacked(u.bh.pC->pCursor, 0, (u64)u.bh.x, 0, &u.bh.res);
54607 u.bh.cnt++;
54608 }while( u.bh.cnt<100 && u.bh.rx==SQLITE_OK && u.bh.res==0 );
54609 db->priorNewRowid = u.bh.v;
54610 if( u.bh.rx==SQLITE_OK && u.bh.res==0 ){
54611 rc = SQLITE_FULL;
54612 goto abort_due_to_error;
54613 }
54614 }
54615 u.bh.pC->rowidIsValid = 0;
54616 u.bh.pC->deferredMoveto = 0;
54617 u.bh.pC->cacheStatus = CACHE_STALE;
54618 }
54619 MemSetTypeFlag(pOut, MEM_Int);
54620 pOut->u.i = u.bh.v;
54621 break;
54622 }
54623
54624 /* Opcode: Insert P1 P2 P3 P4 P5
54625 **
@@ -54646,91 +54532,89 @@
54646 **
54647 ** This instruction only works on tables. The equivalent instruction
54648 ** for indices is OP_IdxInsert.
54649 */
54650 case OP_Insert: {
54651 #if 0 /* local variables moved into u.bi */
54652 Mem *pData;
54653 Mem *pKey;
54654 i64 iKey; /* The integer ROWID or key for the record to be inserted */
54655 int i;
54656 VdbeCursor *pC;
54657 int nZero;
54658 int seekResult;
54659 const char *zDb;
54660 const char *zTbl;
54661 int op;
54662 #endif /* local variables moved into u.bi */
54663
54664 u.bi.pData = &p->aMem[pOp->p2];
54665 u.bi.pKey = &p->aMem[pOp->p3];
54666 u.bi.i = pOp->p1;
54667 assert( u.bi.i>=0 && u.bi.i<p->nCursor );
54668 u.bi.pC = p->apCsr[u.bi.i];
54669 assert( u.bi.pC!=0 );
54670 assert( u.bi.pC->pCursor!=0 || u.bi.pC->pseudoTable );
54671 assert( u.bi.pKey->flags & MEM_Int );
54672 assert( u.bi.pC->isTable );
54673 REGISTER_TRACE(pOp->p2, u.bi.pData);
54674 REGISTER_TRACE(pOp->p3, u.bi.pKey);
54675
54676 u.bi.iKey = intToKey(u.bi.pKey->u.i);
54677 if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
54678 if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = u.bi.pKey->u.i;
54679 if( u.bi.pData->flags & MEM_Null ){
54680 u.bi.pData->z = 0;
54681 u.bi.pData->n = 0;
54682 }else{
54683 assert( u.bi.pData->flags & (MEM_Blob|MEM_Str) );
54684 }
54685 if( u.bi.pC->pseudoTable ){
54686 if( !u.bi.pC->ephemPseudoTable ){
54687 sqlite3DbFree(db, u.bi.pC->pData);
54688 }
54689 u.bi.pC->iKey = u.bi.iKey;
54690 u.bi.pC->nData = u.bi.pData->n;
54691 if( u.bi.pData->z==u.bi.pData->zMalloc || u.bi.pC->ephemPseudoTable ){
54692 u.bi.pC->pData = u.bi.pData->z;
54693 if( !u.bi.pC->ephemPseudoTable ){
54694 u.bi.pData->flags &= ~MEM_Dyn;
54695 u.bi.pData->flags |= MEM_Ephem;
54696 u.bi.pData->zMalloc = 0;
54697 }
54698 }else{
54699 u.bi.pC->pData = sqlite3Malloc( u.bi.pC->nData+2 );
54700 if( !u.bi.pC->pData ) goto no_mem;
54701 memcpy(u.bi.pC->pData, u.bi.pData->z, u.bi.pC->nData);
54702 u.bi.pC->pData[u.bi.pC->nData] = 0;
54703 u.bi.pC->pData[u.bi.pC->nData+1] = 0;
54704 }
54705 u.bi.pC->nullRow = 0;
54706 }else{
54707 u.bi.seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? u.bi.pC->seekResult : 0);
54708 if( u.bi.pData->flags & MEM_Zero ){
54709 u.bi.nZero = u.bi.pData->u.nZero;
54710 }else{
54711 u.bi.nZero = 0;
54712 }
54713 sqlite3BtreeSetCachedRowid(u.bi.pC->pCursor, 0);
54714 rc = sqlite3BtreeInsert(u.bi.pC->pCursor, 0, u.bi.iKey,
54715 u.bi.pData->z, u.bi.pData->n, u.bi.nZero,
54716 pOp->p5 & OPFLAG_APPEND, u.bi.seekResult
54717 );
54718 }
54719
54720 u.bi.pC->rowidIsValid = 0;
54721 u.bi.pC->deferredMoveto = 0;
54722 u.bi.pC->cacheStatus = CACHE_STALE;
54723
54724 /* Invoke the update-hook if required. */
54725 if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z ){
54726 u.bi.zDb = db->aDb[u.bi.pC->iDb].zName;
54727 u.bi.zTbl = pOp->p4.z;
54728 u.bi.op = ((pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT);
54729 assert( u.bi.pC->isTable );
54730 db->xUpdateCallback(db->pUpdateArg, u.bi.op, u.bi.zDb, u.bi.zTbl, u.bi.iKey);
54731 assert( u.bi.pC->iDb>=0 );
54732 }
54733 break;
54734 }
54735
54736 /* Opcode: Delete P1 P2 * P4 *
@@ -54752,44 +54636,51 @@
54752 ** pointing to. The update hook will be invoked, if it exists.
54753 ** If P4 is not NULL then the P1 cursor must have been positioned
54754 ** using OP_NotFound prior to invoking this opcode.
54755 */
54756 case OP_Delete: {
54757 #if 0 /* local variables moved into u.bj */
54758 int i;
54759 i64 iKey;
54760 VdbeCursor *pC;
54761 #endif /* local variables moved into u.bj */
54762
54763 u.bj.i = pOp->p1;
54764 u.bj.iKey = 0;
54765 assert( u.bj.i>=0 && u.bj.i<p->nCursor );
54766 u.bj.pC = p->apCsr[u.bj.i];
54767 assert( u.bj.pC!=0 );
54768 assert( u.bj.pC->pCursor!=0 ); /* Only valid for real tables, no pseudotables */
54769
54770 /* If the update-hook will be invoked, set u.bj.iKey to the rowid of the
54771 ** row being deleted.
54772 */
54773 if( db->xUpdateCallback && pOp->p4.z ){
54774 assert( u.bj.pC->isTable );
54775 assert( u.bj.pC->rowidIsValid ); /* lastRowid set by previous OP_NotFound */
54776 u.bj.iKey = u.bj.pC->lastRowid;
54777 }
54778
54779 rc = sqlite3VdbeCursorMoveto(u.bj.pC);
54780 if( rc ) goto abort_due_to_error;
54781 sqlite3BtreeSetCachedRowid(u.bj.pC->pCursor, 0);
54782 rc = sqlite3BtreeDelete(u.bj.pC->pCursor);
54783 u.bj.pC->cacheStatus = CACHE_STALE;
 
 
 
 
 
 
 
 
 
54784
54785 /* Invoke the update-hook if required. */
54786 if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z ){
54787 const char *zDb = db->aDb[u.bj.pC->iDb].zName;
54788 const char *zTbl = pOp->p4.z;
54789 db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, zTbl, u.bj.iKey);
54790 assert( u.bj.pC->iDb>=0 );
54791 }
54792 if( pOp->p2 & OPFLAG_NCHANGE ) p->nChange++;
54793 break;
54794 }
54795
@@ -54828,55 +54719,61 @@
54828 ** If the P1 cursor must be pointing to a valid row (not a NULL row)
54829 ** of a real table, not a pseudo-table.
54830 */
54831 case OP_RowKey:
54832 case OP_RowData: {
54833 #if 0 /* local variables moved into u.bk */
54834 int i;
54835 VdbeCursor *pC;
54836 BtCursor *pCrsr;
54837 u32 n;
54838 i64 n64;
54839 #endif /* local variables moved into u.bk */
54840
54841 u.bk.i = pOp->p1;
54842 pOut = &p->aMem[pOp->p2];
54843
54844 /* Note that RowKey and RowData are really exactly the same instruction */
54845 assert( u.bk.i>=0 && u.bk.i<p->nCursor );
54846 u.bk.pC = p->apCsr[u.bk.i];
54847 assert( u.bk.pC->isTable || pOp->opcode==OP_RowKey );
54848 assert( u.bk.pC->isIndex || pOp->opcode==OP_RowData );
54849 assert( u.bk.pC!=0 );
54850 assert( u.bk.pC->nullRow==0 );
54851 assert( u.bk.pC->pseudoTable==0 );
54852 assert( u.bk.pC->pCursor!=0 );
54853 u.bk.pCrsr = u.bk.pC->pCursor;
54854 rc = sqlite3VdbeCursorMoveto(u.bk.pC);
54855 if( rc ) goto abort_due_to_error;
54856 if( u.bk.pC->isIndex ){
54857 assert( !u.bk.pC->isTable );
54858 sqlite3BtreeKeySize(u.bk.pCrsr, &u.bk.n64);
54859 if( u.bk.n64>db->aLimit[SQLITE_LIMIT_LENGTH] ){
54860 goto too_big;
54861 }
54862 u.bk.n = (u32)u.bk.n64;
54863 }else{
54864 sqlite3BtreeDataSize(u.bk.pCrsr, &u.bk.n);
54865 if( u.bk.n>db->aLimit[SQLITE_LIMIT_LENGTH] ){
54866 goto too_big;
54867 }
54868 }
54869 if( sqlite3VdbeMemGrow(pOut, u.bk.n, 0) ){
54870 goto no_mem;
54871 }
54872 pOut->n = u.bk.n;
54873 MemSetTypeFlag(pOut, MEM_Blob);
54874 if( u.bk.pC->isIndex ){
54875 rc = sqlite3BtreeKey(u.bk.pCrsr, 0, u.bk.n, pOut->z);
54876 }else{
54877 rc = sqlite3BtreeData(u.bk.pCrsr, 0, u.bk.n, pOut->z);
 
 
 
 
 
 
 
 
54878 }
54879 pOut->enc = SQLITE_UTF8; /* In case the blob is ever cast to text */
54880 UPDATE_MAX_BLOBSIZE(pOut);
54881 break;
54882 }
@@ -54889,53 +54786,50 @@
54889 ** P1 can be either an ordinary table or a virtual table. There used to
54890 ** be a separate OP_VRowid opcode for use with virtual tables, but this
54891 ** one opcode now works for both table types.
54892 */
54893 case OP_Rowid: { /* out2-prerelease */
54894 #if 0 /* local variables moved into u.bl */
54895 int i;
54896 VdbeCursor *pC;
54897 i64 v;
54898 sqlite3_vtab *pVtab;
54899 const sqlite3_module *pModule;
54900 #endif /* local variables moved into u.bl */
54901
54902 u.bl.i = pOp->p1;
54903 assert( u.bl.i>=0 && u.bl.i<p->nCursor );
54904 u.bl.pC = p->apCsr[u.bl.i];
54905 assert( u.bl.pC!=0 );
54906 if( u.bl.pC->nullRow ){
54907 /* Do nothing so that reg[P2] remains NULL */
54908 break;
54909 }else if( u.bl.pC->deferredMoveto ){
54910 u.bl.v = u.bl.pC->movetoTarget;
54911 }else if( u.bl.pC->pseudoTable ){
54912 u.bl.v = keyToInt(u.bl.pC->iKey);
54913 #ifndef SQLITE_OMIT_VIRTUALTABLE
54914 }else if( u.bl.pC->pVtabCursor ){
54915 u.bl.pVtab = u.bl.pC->pVtabCursor->pVtab;
54916 u.bl.pModule = u.bl.pVtab->pModule;
54917 assert( u.bl.pModule->xRowid );
54918 if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
54919 rc = u.bl.pModule->xRowid(u.bl.pC->pVtabCursor, &u.bl.v);
54920 sqlite3DbFree(db, p->zErrMsg);
54921 p->zErrMsg = u.bl.pVtab->zErrMsg;
54922 u.bl.pVtab->zErrMsg = 0;
54923 if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
54924 #endif /* SQLITE_OMIT_VIRTUALTABLE */
54925 }else{
54926 rc = sqlite3VdbeCursorMoveto(u.bl.pC);
54927 if( rc ) goto abort_due_to_error;
54928 if( u.bl.pC->rowidIsValid ){
54929 u.bl.v = u.bl.pC->lastRowid;
54930 }else{
54931 assert( u.bl.pC->pCursor!=0 );
54932 sqlite3BtreeKeySize(u.bl.pC->pCursor, &u.bl.v);
54933 u.bl.v = keyToInt(u.bl.v);
54934 }
54935 }
54936 pOut->u.i = u.bl.v;
54937 MemSetTypeFlag(pOut, MEM_Int);
54938 break;
54939 }
54940
54941 /* Opcode: NullRow P1 * * * *
@@ -54943,23 +54837,21 @@
54943 ** Move the cursor P1 to a null row. Any OP_Column operations
54944 ** that occur while the cursor is on the null row will always
54945 ** write a NULL.
54946 */
54947 case OP_NullRow: {
54948 #if 0 /* local variables moved into u.bm */
54949 int i;
54950 VdbeCursor *pC;
54951 #endif /* local variables moved into u.bm */
54952
54953 u.bm.i = pOp->p1;
54954 assert( u.bm.i>=0 && u.bm.i<p->nCursor );
54955 u.bm.pC = p->apCsr[u.bm.i];
54956 assert( u.bm.pC!=0 );
54957 u.bm.pC->nullRow = 1;
54958 u.bm.pC->rowidIsValid = 0;
54959 if( u.bm.pC->pCursor ){
54960 sqlite3BtreeClearCursor(u.bm.pC->pCursor);
54961 }
54962 break;
54963 }
54964
54965 /* Opcode: Last P1 P2 * * *
@@ -54969,29 +54861,30 @@
54969 ** If the table or index is empty and P2>0, then jump immediately to P2.
54970 ** If P2 is 0 or if the table or index is not empty, fall through
54971 ** to the following instruction.
54972 */
54973 case OP_Last: { /* jump */
54974 #if 0 /* local variables moved into u.bn */
54975 int i;
54976 VdbeCursor *pC;
54977 BtCursor *pCrsr;
54978 int res;
54979 #endif /* local variables moved into u.bn */
54980
54981 u.bn.i = pOp->p1;
54982 assert( u.bn.i>=0 && u.bn.i<p->nCursor );
54983 u.bn.pC = p->apCsr[u.bn.i];
54984 assert( u.bn.pC!=0 );
54985 u.bn.pCrsr = u.bn.pC->pCursor;
54986 assert( u.bn.pCrsr!=0 );
54987 rc = sqlite3BtreeLast(u.bn.pCrsr, &u.bn.res);
54988 u.bn.pC->nullRow = (u8)u.bn.res;
54989 u.bn.pC->deferredMoveto = 0;
54990 u.bn.pC->rowidIsValid = 0;
54991 u.bn.pC->cacheStatus = CACHE_STALE;
54992 if( u.bn.res && pOp->p2>0 ){
 
 
54993 pc = pOp->p2 - 1;
54994 }
54995 break;
54996 }
54997
@@ -55023,33 +54916,31 @@
55023 ** If the table or index is empty and P2>0, then jump immediately to P2.
55024 ** If P2 is 0 or if the table or index is not empty, fall through
55025 ** to the following instruction.
55026 */
55027 case OP_Rewind: { /* jump */
55028 #if 0 /* local variables moved into u.bo */
55029 int i;
55030 VdbeCursor *pC;
55031 BtCursor *pCrsr;
55032 int res;
55033 #endif /* local variables moved into u.bo */
55034
55035 u.bo.i = pOp->p1;
55036 assert( u.bo.i>=0 && u.bo.i<p->nCursor );
55037 u.bo.pC = p->apCsr[u.bo.i];
55038 assert( u.bo.pC!=0 );
55039 if( (u.bo.pCrsr = u.bo.pC->pCursor)!=0 ){
55040 rc = sqlite3BtreeFirst(u.bo.pCrsr, &u.bo.res);
55041 u.bo.pC->atFirst = u.bo.res==0 ?1:0;
55042 u.bo.pC->deferredMoveto = 0;
55043 u.bo.pC->cacheStatus = CACHE_STALE;
55044 u.bo.pC->rowidIsValid = 0;
55045 }else{
55046 u.bo.res = 1;
55047 }
55048 u.bo.pC->nullRow = (u8)u.bo.res;
55049 assert( pOp->p2>0 && pOp->p2<p->nOp );
55050 if( u.bo.res ){
55051 pc = pOp->p2 - 1;
55052 }
55053 break;
55054 }
55055
@@ -55073,38 +54964,41 @@
55073 **
55074 ** The P1 cursor must be for a real table, not a pseudo-table.
55075 */
55076 case OP_Prev: /* jump */
55077 case OP_Next: { /* jump */
55078 #if 0 /* local variables moved into u.bp */
55079 VdbeCursor *pC;
55080 BtCursor *pCrsr;
55081 int res;
55082 #endif /* local variables moved into u.bp */
55083
55084 CHECK_FOR_INTERRUPT;
55085 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
55086 u.bp.pC = p->apCsr[pOp->p1];
55087 if( u.bp.pC==0 ){
55088 break; /* See ticket #2273 */
55089 }
55090 u.bp.pCrsr = u.bp.pC->pCursor;
55091 assert( u.bp.pCrsr );
55092 u.bp.res = 1;
55093 assert( u.bp.pC->deferredMoveto==0 );
55094 rc = pOp->opcode==OP_Next ? sqlite3BtreeNext(u.bp.pCrsr, &u.bp.res) :
55095 sqlite3BtreePrevious(u.bp.pCrsr, &u.bp.res);
55096 u.bp.pC->nullRow = (u8)u.bp.res;
55097 u.bp.pC->cacheStatus = CACHE_STALE;
55098 if( u.bp.res==0 ){
 
 
 
55099 pc = pOp->p2 - 1;
55100 if( pOp->p5 ) p->aCounter[pOp->p5-1]++;
55101 #ifdef SQLITE_TEST
55102 sqlite3_search_count++;
55103 #endif
55104 }
55105 u.bp.pC->rowidIsValid = 0;
55106 break;
55107 }
55108
55109 /* Opcode: IdxInsert P1 P2 P3 * P5
55110 **
@@ -55117,33 +55011,33 @@
55117 **
55118 ** This instruction only works for indices. The equivalent instruction
55119 ** for tables is OP_Insert.
55120 */
55121 case OP_IdxInsert: { /* in2 */
55122 #if 0 /* local variables moved into u.bq */
55123 int i;
55124 VdbeCursor *pC;
55125 BtCursor *pCrsr;
55126 int nKey;
55127 const char *zKey;
55128 #endif /* local variables moved into u.bq */
55129
55130 u.bq.i = pOp->p1;
55131 assert( u.bq.i>=0 && u.bq.i<p->nCursor );
55132 assert( p->apCsr[u.bq.i]!=0 );
55133 assert( pIn2->flags & MEM_Blob );
55134 if( (u.bq.pCrsr = (u.bq.pC = p->apCsr[u.bq.i])->pCursor)!=0 ){
55135 assert( u.bq.pC->isTable==0 );
 
55136 rc = ExpandBlob(pIn2);
55137 if( rc==SQLITE_OK ){
55138 u.bq.nKey = pIn2->n;
55139 u.bq.zKey = pIn2->z;
55140 rc = sqlite3BtreeInsert(u.bq.pCrsr, u.bq.zKey, u.bq.nKey, "", 0, 0, pOp->p3,
55141 ((pOp->p5 & OPFLAG_USESEEKRESULT) ? u.bq.pC->seekResult : 0)
55142 );
55143 assert( u.bq.pC->deferredMoveto==0 );
55144 u.bq.pC->cacheStatus = CACHE_STALE;
55145 }
55146 }
55147 break;
55148 }
55149
@@ -55152,34 +55046,34 @@
55152 ** The content of P3 registers starting at register P2 form
55153 ** an unpacked index key. This opcode removes that entry from the
55154 ** index opened by cursor P1.
55155 */
55156 case OP_IdxDelete: {
55157 #if 0 /* local variables moved into u.br */
55158 int i;
55159 VdbeCursor *pC;
55160 BtCursor *pCrsr;
55161 #endif /* local variables moved into u.br */
 
 
55162
55163 u.br.i = pOp->p1;
55164 assert( pOp->p3>0 );
55165 assert( pOp->p2>0 && pOp->p2+pOp->p3<=p->nMem+1 );
55166 assert( u.br.i>=0 && u.br.i<p->nCursor );
55167 assert( p->apCsr[u.br.i]!=0 );
55168 if( (u.br.pCrsr = (u.br.pC = p->apCsr[u.br.i])->pCursor)!=0 ){
55169 int res;
55170 UnpackedRecord r;
55171 r.pKeyInfo = u.br.pC->pKeyInfo;
55172 r.nField = (u16)pOp->p3;
55173 r.flags = 0;
55174 r.aMem = &p->aMem[pOp->p2];
55175 rc = sqlite3BtreeMovetoUnpacked(u.br.pCrsr, &r, 0, 0, &res);
55176 if( rc==SQLITE_OK && res==0 ){
55177 rc = sqlite3BtreeDelete(u.br.pCrsr);
55178 }
55179 assert( u.br.pC->deferredMoveto==0 );
55180 u.br.pC->cacheStatus = CACHE_STALE;
55181 }
55182 break;
55183 }
55184
55185 /* Opcode: IdxRowid P1 P2 * * *
@@ -55189,32 +55083,32 @@
55189 ** the rowid of the table entry to which this index entry points.
55190 **
55191 ** See also: Rowid, MakeRecord.
55192 */
55193 case OP_IdxRowid: { /* out2-prerelease */
55194 #if 0 /* local variables moved into u.bs */
55195 int i;
55196 BtCursor *pCrsr;
55197 VdbeCursor *pC;
55198 i64 rowid;
55199 #endif /* local variables moved into u.bs */
55200
55201 u.bs.i = pOp->p1;
55202 assert( u.bs.i>=0 && u.bs.i<p->nCursor );
55203 assert( p->apCsr[u.bs.i]!=0 );
55204 if( (u.bs.pCrsr = (u.bs.pC = p->apCsr[u.bs.i])->pCursor)!=0 ){
55205 rc = sqlite3VdbeCursorMoveto(u.bs.pC);
55206 if( rc ) goto abort_due_to_error;
55207 assert( u.bs.pC->deferredMoveto==0 );
55208 assert( u.bs.pC->isTable==0 );
55209 if( !u.bs.pC->nullRow ){
55210 rc = sqlite3VdbeIdxRowid(u.bs.pCrsr, &u.bs.rowid);
 
55211 if( rc!=SQLITE_OK ){
55212 goto abort_due_to_error;
55213 }
55214 MemSetTypeFlag(pOut, MEM_Int);
55215 pOut->u.i = u.bs.rowid;
55216 }
55217 }
55218 break;
55219 }
55220
@@ -55244,40 +55138,39 @@
55244 ** If P5 is non-zero then the key value is increased by an epsilon prior
55245 ** to the comparison. This makes the opcode work like IdxLE.
55246 */
55247 case OP_IdxLT: /* jump, in3 */
55248 case OP_IdxGE: { /* jump, in3 */
55249 #if 0 /* local variables moved into u.bt */
55250 int i;
55251 VdbeCursor *pC;
55252 int res;
55253 UnpackedRecord r;
55254 #endif /* local variables moved into u.bt */
55255
55256 u.bt.i = pOp->p1;
55257 assert( u.bt.i>=0 && u.bt.i<p->nCursor );
55258 assert( p->apCsr[u.bt.i]!=0 );
55259 if( (u.bt.pC = p->apCsr[u.bt.i])->pCursor!=0 ){
55260 assert( u.bt.pC->deferredMoveto==0 );
55261 assert( pOp->p5==0 || pOp->p5==1 );
55262 assert( pOp->p4type==P4_INT32 );
55263 u.bt.r.pKeyInfo = u.bt.pC->pKeyInfo;
55264 u.bt.r.nField = (u16)pOp->p4.i;
55265 if( pOp->p5 ){
55266 u.bt.r.flags = UNPACKED_INCRKEY | UNPACKED_IGNORE_ROWID;
55267 }else{
55268 u.bt.r.flags = UNPACKED_IGNORE_ROWID;
55269 }
55270 u.bt.r.aMem = &p->aMem[pOp->p3];
55271 rc = sqlite3VdbeIdxKeyCompare(u.bt.pC, &u.bt.r, &u.bt.res);
55272 if( pOp->opcode==OP_IdxLT ){
55273 u.bt.res = -u.bt.res;
55274 }else{
55275 assert( pOp->opcode==OP_IdxGE );
55276 u.bt.res++;
55277 }
55278 if( u.bt.res>0 ){
55279 pc = pOp->p2 - 1 ;
55280 }
55281 }
55282 break;
55283 }
@@ -55301,39 +55194,39 @@
55301 ** If AUTOVACUUM is disabled then a zero is stored in register P2.
55302 **
55303 ** See also: Clear
55304 */
55305 case OP_Destroy: { /* out2-prerelease */
55306 #if 0 /* local variables moved into u.bu */
55307 int iMoved;
55308 int iCnt;
55309 Vdbe *pVdbe;
55310 int iDb;
55311 #endif /* local variables moved into u.bu */
55312 #ifndef SQLITE_OMIT_VIRTUALTABLE
55313 u.bu.iCnt = 0;
55314 for(u.bu.pVdbe=db->pVdbe; u.bu.pVdbe; u.bu.pVdbe = u.bu.pVdbe->pNext){
55315 if( u.bu.pVdbe->magic==VDBE_MAGIC_RUN && u.bu.pVdbe->inVtabMethod<2 && u.bu.pVdbe->pc>=0 ){
55316 u.bu.iCnt++;
55317 }
55318 }
55319 #else
55320 u.bu.iCnt = db->activeVdbeCnt;
55321 #endif
55322 if( u.bu.iCnt>1 ){
55323 rc = SQLITE_LOCKED;
55324 p->errorAction = OE_Abort;
55325 }else{
55326 u.bu.iDb = pOp->p3;
55327 assert( u.bu.iCnt==1 );
55328 assert( (p->btreeMask & (1<<u.bu.iDb))!=0 );
55329 rc = sqlite3BtreeDropTable(db->aDb[u.bu.iDb].pBt, pOp->p1, &u.bu.iMoved);
55330 MemSetTypeFlag(pOut, MEM_Int);
55331 pOut->u.i = u.bu.iMoved;
55332 #ifndef SQLITE_OMIT_AUTOVACUUM
55333 if( rc==SQLITE_OK && u.bu.iMoved!=0 ){
55334 sqlite3RootPageMoved(&db->aDb[u.bu.iDb], u.bu.iMoved, pOp->p1);
55335 }
55336 #endif
55337 }
55338 break;
55339 }
@@ -55355,23 +55248,23 @@
55355 ** also incremented by the number of rows in the table being cleared.
55356 **
55357 ** See also: Destroy
55358 */
55359 case OP_Clear: {
55360 #if 0 /* local variables moved into u.bv */
55361 int nChange;
55362 #endif /* local variables moved into u.bv */
55363
55364 u.bv.nChange = 0;
55365 assert( (p->btreeMask & (1<<pOp->p2))!=0 );
55366 rc = sqlite3BtreeClearTable(
55367 db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &u.bv.nChange : 0)
55368 );
55369 if( pOp->p3 ){
55370 p->nChange += u.bv.nChange;
55371 if( pOp->p3>0 ){
55372 p->aMem[pOp->p3].u.i += u.bv.nChange;
55373 }
55374 }
55375 break;
55376 }
55377
@@ -55397,29 +55290,29 @@
55397 **
55398 ** See documentation on OP_CreateTable for additional information.
55399 */
55400 case OP_CreateIndex: /* out2-prerelease */
55401 case OP_CreateTable: { /* out2-prerelease */
55402 #if 0 /* local variables moved into u.bw */
55403 int pgno;
55404 int flags;
55405 Db *pDb;
55406 #endif /* local variables moved into u.bw */
55407
55408 u.bw.pgno = 0;
55409 assert( pOp->p1>=0 && pOp->p1<db->nDb );
55410 assert( (p->btreeMask & (1<<pOp->p1))!=0 );
55411 u.bw.pDb = &db->aDb[pOp->p1];
55412 assert( u.bw.pDb->pBt!=0 );
55413 if( pOp->opcode==OP_CreateTable ){
55414 /* u.bw.flags = BTREE_INTKEY; */
55415 u.bw.flags = BTREE_LEAFDATA|BTREE_INTKEY;
55416 }else{
55417 u.bw.flags = BTREE_ZERODATA;
55418 }
55419 rc = sqlite3BtreeCreateTable(u.bw.pDb->pBt, &u.bw.pgno, u.bw.flags);
55420 pOut->u.i = u.bw.pgno;
55421 MemSetTypeFlag(pOut, MEM_Int);
55422 break;
55423 }
55424
55425 /* Opcode: ParseSchema P1 P2 * P4 *
@@ -55433,62 +55326,62 @@
55433 **
55434 ** This opcode invokes the parser to create a new virtual machine,
55435 ** then runs the new virtual machine. It is thus a re-entrant opcode.
55436 */
55437 case OP_ParseSchema: {
55438 #if 0 /* local variables moved into u.bx */
55439 int iDb;
55440 const char *zMaster;
55441 char *zSql;
55442 InitData initData;
55443 #endif /* local variables moved into u.bx */
55444
55445 u.bx.iDb = pOp->p1;
55446 assert( u.bx.iDb>=0 && u.bx.iDb<db->nDb );
55447
55448 /* If pOp->p2 is 0, then this opcode is being executed to read a
55449 ** single row, for example the row corresponding to a new index
55450 ** created by this VDBE, from the sqlite_master table. It only
55451 ** does this if the corresponding in-memory schema is currently
55452 ** loaded. Otherwise, the new index definition can be loaded along
55453 ** with the rest of the schema when it is required.
55454 **
55455 ** Although the mutex on the BtShared object that corresponds to
55456 ** database u.bx.iDb (the database containing the sqlite_master table
55457 ** read by this instruction) is currently held, it is necessary to
55458 ** obtain the mutexes on all attached databases before checking if
55459 ** the schema of u.bx.iDb is loaded. This is because, at the start of
55460 ** the sqlite3_exec() call below, SQLite will invoke
55461 ** sqlite3BtreeEnterAll(). If all mutexes are not already held, the
55462 ** u.bx.iDb mutex may be temporarily released to avoid deadlock. If
55463 ** this happens, then some other thread may delete the in-memory
55464 ** schema of database u.bx.iDb before the SQL statement runs. The schema
55465 ** will not be reloaded becuase the db->init.busy flag is set. This
55466 ** can result in a "no such table: sqlite_master" or "malformed
55467 ** database schema" error being returned to the user.
55468 */
55469 assert( sqlite3BtreeHoldsMutex(db->aDb[u.bx.iDb].pBt) );
55470 sqlite3BtreeEnterAll(db);
55471 if( pOp->p2 || DbHasProperty(db, u.bx.iDb, DB_SchemaLoaded) ){
55472 u.bx.zMaster = SCHEMA_TABLE(u.bx.iDb);
55473 u.bx.initData.db = db;
55474 u.bx.initData.iDb = pOp->p1;
55475 u.bx.initData.pzErrMsg = &p->zErrMsg;
55476 u.bx.zSql = sqlite3MPrintf(db,
55477 "SELECT name, rootpage, sql FROM '%q'.%s WHERE %s",
55478 db->aDb[u.bx.iDb].zName, u.bx.zMaster, pOp->p4.z);
55479 if( u.bx.zSql==0 ){
55480 rc = SQLITE_NOMEM;
55481 }else{
55482 (void)sqlite3SafetyOff(db);
55483 assert( db->init.busy==0 );
55484 db->init.busy = 1;
55485 u.bx.initData.rc = SQLITE_OK;
55486 assert( !db->mallocFailed );
55487 rc = sqlite3_exec(db, u.bx.zSql, sqlite3InitCallback, &u.bx.initData, 0);
55488 if( rc==SQLITE_OK ) rc = u.bx.initData.rc;
55489 sqlite3DbFree(db, u.bx.zSql);
55490 db->init.busy = 0;
55491 (void)sqlite3SafetyOn(db);
55492 }
55493 }
55494 sqlite3BtreeLeaveAll(db);
@@ -55496,11 +55389,11 @@
55496 goto no_mem;
55497 }
55498 break;
55499 }
55500
55501 #if !defined(SQLITE_OMIT_ANALYZE) && !defined(SQLITE_OMIT_PARSER)
55502 /* Opcode: LoadAnalysis P1 * * * *
55503 **
55504 ** Read the sqlite_stat1 table for database P1 and load the content
55505 ** of that table into the internal index hash table. This will cause
55506 ** the analysis to be used when preparing all subsequent queries.
@@ -55508,11 +55401,11 @@
55508 case OP_LoadAnalysis: {
55509 assert( pOp->p1>=0 && pOp->p1<db->nDb );
55510 rc = sqlite3AnalysisLoad(db, pOp->p1);
55511 break;
55512 }
55513 #endif /* !defined(SQLITE_OMIT_ANALYZE) && !defined(SQLITE_OMIT_PARSER) */
55514
55515 /* Opcode: DropTable P1 * * P4 *
55516 **
55517 ** Remove the internal (in-memory) data structures that describe
55518 ** the table named P4 in database P1. This is called after a table
@@ -55569,45 +55462,45 @@
55569 ** file, not the main database file.
55570 **
55571 ** This opcode is used to implement the integrity_check pragma.
55572 */
55573 case OP_IntegrityCk: {
55574 #if 0 /* local variables moved into u.by */
55575 int nRoot; /* Number of tables to check. (Number of root pages.) */
55576 int *aRoot; /* Array of rootpage numbers for tables to be checked */
55577 int j; /* Loop counter */
55578 int nErr; /* Number of errors reported */
55579 char *z; /* Text of the error report */
55580 Mem *pnErr; /* Register keeping track of errors remaining */
55581 #endif /* local variables moved into u.by */
55582
55583 u.by.nRoot = pOp->p2;
55584 assert( u.by.nRoot>0 );
55585 u.by.aRoot = sqlite3DbMallocRaw(db, sizeof(int)*(u.by.nRoot+1) );
55586 if( u.by.aRoot==0 ) goto no_mem;
55587 assert( pOp->p3>0 && pOp->p3<=p->nMem );
55588 u.by.pnErr = &p->aMem[pOp->p3];
55589 assert( (u.by.pnErr->flags & MEM_Int)!=0 );
55590 assert( (u.by.pnErr->flags & (MEM_Str|MEM_Blob))==0 );
55591 pIn1 = &p->aMem[pOp->p1];
55592 for(u.by.j=0; u.by.j<u.by.nRoot; u.by.j++){
55593 u.by.aRoot[u.by.j] = (int)sqlite3VdbeIntValue(&pIn1[u.by.j]);
55594 }
55595 u.by.aRoot[u.by.j] = 0;
55596 assert( pOp->p5<db->nDb );
55597 assert( (p->btreeMask & (1<<pOp->p5))!=0 );
55598 u.by.z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p5].pBt, u.by.aRoot, u.by.nRoot,
55599 (int)u.by.pnErr->u.i, &u.by.nErr);
55600 sqlite3DbFree(db, u.by.aRoot);
55601 u.by.pnErr->u.i -= u.by.nErr;
55602 sqlite3VdbeMemSetNull(pIn1);
55603 if( u.by.nErr==0 ){
55604 assert( u.by.z==0 );
55605 }else if( u.by.z==0 ){
55606 goto no_mem;
55607 }else{
55608 sqlite3VdbeMemSetStr(pIn1, u.by.z, -1, SQLITE_UTF8, sqlite3_free);
55609 }
55610 UPDATE_MAX_BLOBSIZE(pIn1);
55611 sqlite3VdbeChangeEncoding(pIn1, encoding);
55612 break;
55613 }
@@ -55619,24 +55512,24 @@
55619 ** held in register P1.
55620 **
55621 ** An assertion fails if P2 is not an integer.
55622 */
55623 case OP_RowSetAdd: { /* in2 */
55624 #if 0 /* local variables moved into u.bz */
55625 Mem *pIdx;
55626 Mem *pVal;
55627 #endif /* local variables moved into u.bz */
55628 assert( pOp->p1>0 && pOp->p1<=p->nMem );
55629 u.bz.pIdx = &p->aMem[pOp->p1];
55630 assert( pOp->p2>0 && pOp->p2<=p->nMem );
55631 u.bz.pVal = &p->aMem[pOp->p2];
55632 assert( (u.bz.pVal->flags & MEM_Int)!=0 );
55633 if( (u.bz.pIdx->flags & MEM_RowSet)==0 ){
55634 sqlite3VdbeMemSetRowSet(u.bz.pIdx);
55635 if( (u.bz.pIdx->flags & MEM_RowSet)==0 ) goto no_mem;
55636 }
55637 sqlite3RowSetInsert(u.bz.pIdx->u.pRowSet, u.bz.pVal->u.i);
55638 break;
55639 }
55640
55641 /* Opcode: RowSetRead P1 P2 P3 * *
55642 **
@@ -55643,28 +55536,28 @@
55643 ** Extract the smallest value from boolean index P1 and put that value into
55644 ** register P3. Or, if boolean index P1 is initially empty, leave P3
55645 ** unchanged and jump to instruction P2.
55646 */
55647 case OP_RowSetRead: { /* jump, out3 */
55648 #if 0 /* local variables moved into u.ca */
55649 Mem *pIdx;
55650 i64 val;
55651 #endif /* local variables moved into u.ca */
55652 assert( pOp->p1>0 && pOp->p1<=p->nMem );
55653 CHECK_FOR_INTERRUPT;
55654 u.ca.pIdx = &p->aMem[pOp->p1];
55655 pOut = &p->aMem[pOp->p3];
55656 if( (u.ca.pIdx->flags & MEM_RowSet)==0
55657 || sqlite3RowSetNext(u.ca.pIdx->u.pRowSet, &u.ca.val)==0
55658 ){
55659 /* The boolean index is empty */
55660 sqlite3VdbeMemSetNull(u.ca.pIdx);
55661 pc = pOp->p2 - 1;
55662 }else{
55663 /* A value was pulled from the index */
55664 assert( pOp->p3>0 && pOp->p3<=p->nMem );
55665 sqlite3VdbeMemSetInt64(pOut, u.ca.val);
55666 }
55667 break;
55668 }
55669
55670 /* Opcode: RowSetTest P1 P2 P3 P4
@@ -55689,16 +55582,16 @@
55689 ** inserted, there is no need to search to see if the same value was
55690 ** previously inserted as part of set X (only if it was previously
55691 ** inserted as part of some other set).
55692 */
55693 case OP_RowSetTest: { /* jump, in1, in3 */
55694 #if 0 /* local variables moved into u.cb */
55695 int iSet;
55696 int exists;
55697 #endif /* local variables moved into u.cb */
55698
55699 u.cb.iSet = pOp->p4.i;
55700 assert( pIn3->flags&MEM_Int );
55701
55702 /* If there is anything other than a rowset object in memory cell P1,
55703 ** delete it now and initialize P1 with an empty rowset
55704 */
@@ -55706,21 +55599,21 @@
55706 sqlite3VdbeMemSetRowSet(pIn1);
55707 if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem;
55708 }
55709
55710 assert( pOp->p4type==P4_INT32 );
55711 assert( u.cb.iSet==-1 || u.cb.iSet>=0 );
55712 if( u.cb.iSet ){
55713 u.cb.exists = sqlite3RowSetTest(pIn1->u.pRowSet,
55714 (u8)(u.cb.iSet>=0 ? u.cb.iSet & 0xf : 0xff),
55715 pIn3->u.i);
55716 if( u.cb.exists ){
55717 pc = pOp->p2 - 1;
55718 break;
55719 }
55720 }
55721 if( u.cb.iSet>=0 ){
55722 sqlite3RowSetInsert(pIn1->u.pRowSet, pIn3->u.i);
55723 }
55724 break;
55725 }
55726
@@ -55731,27 +55624,27 @@
55731 ** Save the current Vdbe context such that it can be restored by a ContextPop
55732 ** opcode. The context stores the last insert row id, the last statement change
55733 ** count, and the current statement change count.
55734 */
55735 case OP_ContextPush: {
55736 #if 0 /* local variables moved into u.cc */
55737 int i;
55738 Context *pContext;
55739 #endif /* local variables moved into u.cc */
55740
55741 u.cc.i = p->contextStackTop++;
55742 assert( u.cc.i>=0 );
55743 /* FIX ME: This should be allocated as part of the vdbe at compile-time */
55744 if( u.cc.i>=p->contextStackDepth ){
55745 p->contextStackDepth = u.cc.i+1;
55746 p->contextStack = sqlite3DbReallocOrFree(db, p->contextStack,
55747 sizeof(Context)*(u.cc.i+1));
55748 if( p->contextStack==0 ) goto no_mem;
55749 }
55750 u.cc.pContext = &p->contextStack[u.cc.i];
55751 u.cc.pContext->lastRowid = db->lastRowid;
55752 u.cc.pContext->nChange = p->nChange;
55753 break;
55754 }
55755
55756 /* Opcode: ContextPop * * *
55757 **
@@ -55758,17 +55651,17 @@
55758 ** Restore the Vdbe context to the state it was in when contextPush was last
55759 ** executed. The context stores the last insert row id, the last statement
55760 ** change count, and the current statement change count.
55761 */
55762 case OP_ContextPop: {
55763 #if 0 /* local variables moved into u.cd */
55764 Context *pContext;
55765 #endif /* local variables moved into u.cd */
55766 u.cd.pContext = &p->contextStack[--p->contextStackTop];
55767 assert( p->contextStackTop>=0 );
55768 db->lastRowid = u.cd.pContext->lastRowid;
55769 p->nChange = u.cd.pContext->nChange;
55770 break;
55771 }
55772 #endif /* #ifndef SQLITE_OMIT_TRIGGER */
55773
55774 #ifndef SQLITE_OMIT_AUTOINCREMENT
@@ -55844,51 +55737,51 @@
55844 **
55845 ** The P5 arguments are taken from register P2 and its
55846 ** successors.
55847 */
55848 case OP_AggStep: {
55849 #if 0 /* local variables moved into u.ce */
55850 int n;
55851 int i;
55852 Mem *pMem;
55853 Mem *pRec;
55854 sqlite3_context ctx;
55855 sqlite3_value **apVal;
55856 #endif /* local variables moved into u.ce */
55857
55858 u.ce.n = pOp->p5;
55859 assert( u.ce.n>=0 );
55860 u.ce.pRec = &p->aMem[pOp->p2];
55861 u.ce.apVal = p->apArg;
55862 assert( u.ce.apVal || u.ce.n==0 );
55863 for(u.ce.i=0; u.ce.i<u.ce.n; u.ce.i++, u.ce.pRec++){
55864 u.ce.apVal[u.ce.i] = u.ce.pRec;
55865 storeTypeInfo(u.ce.pRec, encoding);
55866 }
55867 u.ce.ctx.pFunc = pOp->p4.pFunc;
55868 assert( pOp->p3>0 && pOp->p3<=p->nMem );
55869 u.ce.ctx.pMem = u.ce.pMem = &p->aMem[pOp->p3];
55870 u.ce.pMem->n++;
55871 u.ce.ctx.s.flags = MEM_Null;
55872 u.ce.ctx.s.z = 0;
55873 u.ce.ctx.s.zMalloc = 0;
55874 u.ce.ctx.s.xDel = 0;
55875 u.ce.ctx.s.db = db;
55876 u.ce.ctx.isError = 0;
55877 u.ce.ctx.pColl = 0;
55878 if( u.ce.ctx.pFunc->flags & SQLITE_FUNC_NEEDCOLL ){
55879 assert( pOp>p->aOp );
55880 assert( pOp[-1].p4type==P4_COLLSEQ );
55881 assert( pOp[-1].opcode==OP_CollSeq );
55882 u.ce.ctx.pColl = pOp[-1].p4.pColl;
55883 }
55884 (u.ce.ctx.pFunc->xStep)(&u.ce.ctx, u.ce.n, u.ce.apVal);
55885 if( u.ce.ctx.isError ){
55886 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&u.ce.ctx.s));
55887 rc = u.ce.ctx.isError;
55888 }
55889 sqlite3VdbeMemRelease(&u.ce.ctx.s);
55890 break;
55891 }
55892
55893 /* Opcode: AggFinal P1 P2 * P4 *
55894 **
@@ -55901,23 +55794,23 @@
55901 ** functions that can take varying numbers of arguments. The
55902 ** P4 argument is only needed for the degenerate case where
55903 ** the step function was not previously called.
55904 */
55905 case OP_AggFinal: {
55906 #if 0 /* local variables moved into u.cf */
55907 Mem *pMem;
55908 #endif /* local variables moved into u.cf */
55909 assert( pOp->p1>0 && pOp->p1<=p->nMem );
55910 u.cf.pMem = &p->aMem[pOp->p1];
55911 assert( (u.cf.pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
55912 rc = sqlite3VdbeMemFinalize(u.cf.pMem, pOp->p4.pFunc);
55913 if( rc==SQLITE_ERROR ){
55914 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(u.cf.pMem));
55915 }
55916 sqlite3VdbeChangeEncoding(u.cf.pMem, encoding);
55917 UPDATE_MAX_BLOBSIZE(u.cf.pMem);
55918 if( sqlite3VdbeMemTooBig(u.cf.pMem) ){
55919 goto too_big;
55920 }
55921 break;
55922 }
55923
@@ -55943,18 +55836,18 @@
55943 ** Perform a single step of the incremental vacuum procedure on
55944 ** the P1 database. If the vacuum has finished, jump to instruction
55945 ** P2. Otherwise, fall through to the next instruction.
55946 */
55947 case OP_IncrVacuum: { /* jump */
55948 #if 0 /* local variables moved into u.cg */
55949 Btree *pBt;
55950 #endif /* local variables moved into u.cg */
55951
55952 assert( pOp->p1>=0 && pOp->p1<db->nDb );
55953 assert( (p->btreeMask & (1<<pOp->p1))!=0 );
55954 u.cg.pBt = db->aDb[pOp->p1].pBt;
55955 rc = sqlite3BtreeIncrVacuum(u.cg.pBt);
55956 if( rc==SQLITE_DONE ){
55957 pc = pOp->p2 - 1;
55958 rc = SQLITE_OK;
55959 }
55960 break;
@@ -55993,21 +55886,21 @@
55993 **
55994 ** P4 contains a pointer to the name of the table being locked. This is only
55995 ** used to generate an error message if the lock cannot be obtained.
55996 */
55997 case OP_TableLock: {
55998 #if 0 /* local variables moved into u.ch */
55999 int p1;
56000 u8 isWriteLock;
56001 #endif /* local variables moved into u.ch */
56002
56003 u.ch.p1 = pOp->p1;
56004 u.ch.isWriteLock = (u8)pOp->p3;
56005 assert( u.ch.p1>=0 && u.ch.p1<db->nDb );
56006 assert( (p->btreeMask & (1<<u.ch.p1))!=0 );
56007 assert( u.ch.isWriteLock==0 || u.ch.isWriteLock==1 );
56008 rc = sqlite3BtreeLockTable(db->aDb[u.ch.p1].pBt, pOp->p2, u.ch.isWriteLock);
56009 if( (rc&0xFF)==SQLITE_LOCKED ){
56010 const char *z = pOp->p4.z;
56011 sqlite3SetString(&p->zErrMsg, db, "database table is locked: %s", z);
56012 }
56013 break;
@@ -56023,19 +55916,19 @@
56023 ** Also, whether or not P4 is set, check that this is not being called from
56024 ** within a callback to a virtual table xSync() method. If it is, the error
56025 ** code will be set to SQLITE_LOCKED.
56026 */
56027 case OP_VBegin: {
56028 #if 0 /* local variables moved into u.ci */
56029 sqlite3_vtab *pVtab;
56030 #endif /* local variables moved into u.ci */
56031 u.ci.pVtab = pOp->p4.pVtab;
56032 rc = sqlite3VtabBegin(db, u.ci.pVtab);
56033 if( u.ci.pVtab ){
56034 sqlite3DbFree(db, p->zErrMsg);
56035 p->zErrMsg = u.ci.pVtab->zErrMsg;
56036 u.ci.pVtab->zErrMsg = 0;
56037 }
56038 break;
56039 }
56040 #endif /* SQLITE_OMIT_VIRTUALTABLE */
56041
@@ -56071,40 +55964,40 @@
56071 ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
56072 ** P1 is a cursor number. This opcode opens a cursor to the virtual
56073 ** table and stores that cursor in P1.
56074 */
56075 case OP_VOpen: {
56076 #if 0 /* local variables moved into u.cj */
56077 VdbeCursor *pCur;
56078 sqlite3_vtab_cursor *pVtabCursor;
56079 sqlite3_vtab *pVtab;
56080 sqlite3_module *pModule;
56081 #endif /* local variables moved into u.cj */
56082
56083 u.cj.pCur = 0;
56084 u.cj.pVtabCursor = 0;
56085 u.cj.pVtab = pOp->p4.pVtab;
56086 u.cj.pModule = (sqlite3_module *)u.cj.pVtab->pModule;
56087 assert(u.cj.pVtab && u.cj.pModule);
56088 if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
56089 rc = u.cj.pModule->xOpen(u.cj.pVtab, &u.cj.pVtabCursor);
56090 sqlite3DbFree(db, p->zErrMsg);
56091 p->zErrMsg = u.cj.pVtab->zErrMsg;
56092 u.cj.pVtab->zErrMsg = 0;
56093 if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
56094 if( SQLITE_OK==rc ){
56095 /* Initialize sqlite3_vtab_cursor base class */
56096 u.cj.pVtabCursor->pVtab = u.cj.pVtab;
56097
56098 /* Initialise vdbe cursor object */
56099 u.cj.pCur = allocateCursor(p, pOp->p1, 0, -1, 0);
56100 if( u.cj.pCur ){
56101 u.cj.pCur->pVtabCursor = u.cj.pVtabCursor;
56102 u.cj.pCur->pModule = u.cj.pVtabCursor->pVtab->pModule;
56103 }else{
56104 db->mallocFailed = 1;
56105 u.cj.pModule->xClose(u.cj.pVtabCursor);
56106 }
56107 }
56108 break;
56109 }
56110 #endif /* SQLITE_OMIT_VIRTUALTABLE */
@@ -56127,11 +56020,11 @@
56127 ** xFilter as argv. Register P3+2 becomes argv[0] when passed to xFilter.
56128 **
56129 ** A jump is made to P2 if the result set after filtering would be empty.
56130 */
56131 case OP_VFilter: { /* jump */
56132 #if 0 /* local variables moved into u.ck */
56133 int nArg;
56134 int iQuery;
56135 const sqlite3_module *pModule;
56136 Mem *pQuery;
56137 Mem *pArgc;
@@ -56139,54 +56032,54 @@
56139 sqlite3_vtab *pVtab;
56140 VdbeCursor *pCur;
56141 int res;
56142 int i;
56143 Mem **apArg;
56144 #endif /* local variables moved into u.ck */
56145
56146 u.ck.pQuery = &p->aMem[pOp->p3];
56147 u.ck.pArgc = &u.ck.pQuery[1];
56148 u.ck.pCur = p->apCsr[pOp->p1];
56149 REGISTER_TRACE(pOp->p3, u.ck.pQuery);
56150 assert( u.ck.pCur->pVtabCursor );
56151 u.ck.pVtabCursor = u.ck.pCur->pVtabCursor;
56152 u.ck.pVtab = u.ck.pVtabCursor->pVtab;
56153 u.ck.pModule = u.ck.pVtab->pModule;
56154
56155 /* Grab the index number and argc parameters */
56156 assert( (u.ck.pQuery->flags&MEM_Int)!=0 && u.ck.pArgc->flags==MEM_Int );
56157 u.ck.nArg = (int)u.ck.pArgc->u.i;
56158 u.ck.iQuery = (int)u.ck.pQuery->u.i;
56159
56160 /* Invoke the xFilter method */
56161 {
56162 u.ck.res = 0;
56163 u.ck.apArg = p->apArg;
56164 for(u.ck.i = 0; u.ck.i<u.ck.nArg; u.ck.i++){
56165 u.ck.apArg[u.ck.i] = &u.ck.pArgc[u.ck.i+1];
56166 storeTypeInfo(u.ck.apArg[u.ck.i], 0);
56167 }
56168
56169 if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
56170 sqlite3VtabLock(u.ck.pVtab);
56171 p->inVtabMethod = 1;
56172 rc = u.ck.pModule->xFilter(u.ck.pVtabCursor, u.ck.iQuery, pOp->p4.z, u.ck.nArg, u.ck.apArg);
56173 p->inVtabMethod = 0;
56174 sqlite3DbFree(db, p->zErrMsg);
56175 p->zErrMsg = u.ck.pVtab->zErrMsg;
56176 u.ck.pVtab->zErrMsg = 0;
56177 sqlite3VtabUnlock(db, u.ck.pVtab);
56178 if( rc==SQLITE_OK ){
56179 u.ck.res = u.ck.pModule->xEof(u.ck.pVtabCursor);
56180 }
56181 if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
56182
56183 if( u.ck.res ){
56184 pc = pOp->p2 - 1;
56185 }
56186 }
56187 u.ck.pCur->nullRow = 0;
56188
56189 break;
56190 }
56191 #endif /* SQLITE_OMIT_VIRTUALTABLE */
56192
@@ -56196,57 +56089,57 @@
56196 ** Store the value of the P2-th column of
56197 ** the row of the virtual-table that the
56198 ** P1 cursor is pointing to into register P3.
56199 */
56200 case OP_VColumn: {
56201 #if 0 /* local variables moved into u.cl */
56202 sqlite3_vtab *pVtab;
56203 const sqlite3_module *pModule;
56204 Mem *pDest;
56205 sqlite3_context sContext;
56206 #endif /* local variables moved into u.cl */
56207
56208 VdbeCursor *pCur = p->apCsr[pOp->p1];
56209 assert( pCur->pVtabCursor );
56210 assert( pOp->p3>0 && pOp->p3<=p->nMem );
56211 u.cl.pDest = &p->aMem[pOp->p3];
56212 if( pCur->nullRow ){
56213 sqlite3VdbeMemSetNull(u.cl.pDest);
56214 break;
56215 }
56216 u.cl.pVtab = pCur->pVtabCursor->pVtab;
56217 u.cl.pModule = u.cl.pVtab->pModule;
56218 assert( u.cl.pModule->xColumn );
56219 memset(&u.cl.sContext, 0, sizeof(u.cl.sContext));
56220
56221 /* The output cell may already have a buffer allocated. Move
56222 ** the current contents to u.cl.sContext.s so in case the user-function
56223 ** can use the already allocated buffer instead of allocating a
56224 ** new one.
56225 */
56226 sqlite3VdbeMemMove(&u.cl.sContext.s, u.cl.pDest);
56227 MemSetTypeFlag(&u.cl.sContext.s, MEM_Null);
56228
56229 if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
56230 rc = u.cl.pModule->xColumn(pCur->pVtabCursor, &u.cl.sContext, pOp->p2);
56231 sqlite3DbFree(db, p->zErrMsg);
56232 p->zErrMsg = u.cl.pVtab->zErrMsg;
56233 u.cl.pVtab->zErrMsg = 0;
56234
56235 /* Copy the result of the function to the P3 register. We
56236 ** do this regardless of whether or not an error occurred to ensure any
56237 ** dynamic allocation in u.cl.sContext.s (a Mem struct) is released.
56238 */
56239 sqlite3VdbeChangeEncoding(&u.cl.sContext.s, encoding);
56240 REGISTER_TRACE(pOp->p3, u.cl.pDest);
56241 sqlite3VdbeMemMove(u.cl.pDest, &u.cl.sContext.s);
56242 UPDATE_MAX_BLOBSIZE(u.cl.pDest);
56243
56244 if( sqlite3SafetyOn(db) ){
56245 goto abort_due_to_misuse;
56246 }
56247 if( sqlite3VdbeMemTooBig(u.cl.pDest) ){
56248 goto too_big;
56249 }
56250 break;
56251 }
56252 #endif /* SQLITE_OMIT_VIRTUALTABLE */
@@ -56257,48 +56150,48 @@
56257 ** Advance virtual table P1 to the next row in its result set and
56258 ** jump to instruction P2. Or, if the virtual table has reached
56259 ** the end of its result set, then fall through to the next instruction.
56260 */
56261 case OP_VNext: { /* jump */
56262 #if 0 /* local variables moved into u.cm */
56263 sqlite3_vtab *pVtab;
56264 const sqlite3_module *pModule;
56265 int res;
56266 VdbeCursor *pCur;
56267 #endif /* local variables moved into u.cm */
56268
56269 u.cm.res = 0;
56270 u.cm.pCur = p->apCsr[pOp->p1];
56271 assert( u.cm.pCur->pVtabCursor );
56272 if( u.cm.pCur->nullRow ){
56273 break;
56274 }
56275 u.cm.pVtab = u.cm.pCur->pVtabCursor->pVtab;
56276 u.cm.pModule = u.cm.pVtab->pModule;
56277 assert( u.cm.pModule->xNext );
56278
56279 /* Invoke the xNext() method of the module. There is no way for the
56280 ** underlying implementation to return an error if one occurs during
56281 ** xNext(). Instead, if an error occurs, true is returned (indicating that
56282 ** data is available) and the error code returned when xColumn or
56283 ** some other method is next invoked on the save virtual table cursor.
56284 */
56285 if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
56286 sqlite3VtabLock(u.cm.pVtab);
56287 p->inVtabMethod = 1;
56288 rc = u.cm.pModule->xNext(u.cm.pCur->pVtabCursor);
56289 p->inVtabMethod = 0;
56290 sqlite3DbFree(db, p->zErrMsg);
56291 p->zErrMsg = u.cm.pVtab->zErrMsg;
56292 u.cm.pVtab->zErrMsg = 0;
56293 sqlite3VtabUnlock(db, u.cm.pVtab);
56294 if( rc==SQLITE_OK ){
56295 u.cm.res = u.cm.pModule->xEof(u.cm.pCur->pVtabCursor);
56296 }
56297 if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
56298
56299 if( !u.cm.res ){
56300 /* If there is data, jump to P2 */
56301 pc = pOp->p2 - 1;
56302 }
56303 break;
56304 }
@@ -56310,29 +56203,27 @@
56310 ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
56311 ** This opcode invokes the corresponding xRename method. The value
56312 ** in register P1 is passed as the zName argument to the xRename method.
56313 */
56314 case OP_VRename: {
56315 #if 0 /* local variables moved into u.cn */
56316 sqlite3_vtab *pVtab;
56317 Mem *pName;
56318 #endif /* local variables moved into u.cn */
56319
56320 u.cn.pVtab = pOp->p4.pVtab;
56321 u.cn.pName = &p->aMem[pOp->p1];
56322 assert( u.cn.pVtab->pModule->xRename );
56323 REGISTER_TRACE(pOp->p1, u.cn.pName);
56324
56325 Stringify(u.cn.pName, encoding);
56326
56327 if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
56328 sqlite3VtabLock(u.cn.pVtab);
56329 rc = u.cn.pVtab->pModule->xRename(u.cn.pVtab, u.cn.pName->z);
56330 sqlite3DbFree(db, p->zErrMsg);
56331 p->zErrMsg = u.cn.pVtab->zErrMsg;
56332 u.cn.pVtab->zErrMsg = 0;
56333 sqlite3VtabUnlock(db, u.cn.pVtab);
56334 if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
56335
56336 break;
56337 }
56338 #endif
@@ -56360,46 +56251,43 @@
56360 ** P1 is a boolean flag. If it is set to true and the xUpdate call
56361 ** is successful, then the value returned by sqlite3_last_insert_rowid()
56362 ** is set to the value of the rowid for the row just inserted.
56363 */
56364 case OP_VUpdate: {
56365 #if 0 /* local variables moved into u.co */
56366 sqlite3_vtab *pVtab;
56367 sqlite3_module *pModule;
56368 int nArg;
56369 int i;
56370 sqlite_int64 rowid;
56371 Mem **apArg;
56372 Mem *pX;
56373 #endif /* local variables moved into u.co */
56374
56375 u.co.pVtab = pOp->p4.pVtab;
56376 u.co.pModule = (sqlite3_module *)u.co.pVtab->pModule;
56377 u.co.nArg = pOp->p2;
56378 assert( pOp->p4type==P4_VTAB );
56379 if( u.co.pModule->xUpdate==0 ){
56380 sqlite3SetString(&p->zErrMsg, db, "read-only table");
56381 rc = SQLITE_ERROR;
56382 }else{
56383 u.co.apArg = p->apArg;
56384 u.co.pX = &p->aMem[pOp->p3];
56385 for(u.co.i=0; u.co.i<u.co.nArg; u.co.i++){
56386 storeTypeInfo(u.co.pX, 0);
56387 u.co.apArg[u.co.i] = u.co.pX;
56388 u.co.pX++;
56389 }
56390 if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
56391 sqlite3VtabLock(u.co.pVtab);
56392 rc = u.co.pModule->xUpdate(u.co.pVtab, u.co.nArg, u.co.apArg, &u.co.rowid);
56393 sqlite3DbFree(db, p->zErrMsg);
56394 p->zErrMsg = u.co.pVtab->zErrMsg;
56395 u.co.pVtab->zErrMsg = 0;
56396 sqlite3VtabUnlock(db, u.co.pVtab);
56397 if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
56398 if( pOp->p1 && rc==SQLITE_OK ){
56399 assert( u.co.nArg>1 && u.co.apArg[0] && (u.co.apArg[0]->flags&MEM_Null) );
56400 db->lastRowid = u.co.rowid;
56401 }
56402 p->nChange++;
56403 }
56404 break;
56405 }
@@ -56409,22 +56297,25 @@
56409 /* Opcode: Pagecount P1 P2 * * *
56410 **
56411 ** Write the current number of pages in database P1 to memory cell P2.
56412 */
56413 case OP_Pagecount: { /* out2-prerelease */
56414 #if 0 /* local variables moved into u.cp */
56415 int p1;
56416 int nPage;
56417 Pager *pPager;
56418 #endif /* local variables moved into u.cp */
56419
56420 u.cp.p1 = pOp->p1;
56421 u.cp.pPager = sqlite3BtreePager(db->aDb[u.cp.p1].pBt);
56422 rc = sqlite3PagerPagecount(u.cp.pPager, &u.cp.nPage);
56423 if( rc==SQLITE_OK ){
 
 
 
56424 pOut->flags = MEM_Int;
56425 pOut->u.i = u.cp.nPage;
56426 }
56427 break;
56428 }
56429 #endif
56430
@@ -56433,22 +56324,22 @@
56433 **
56434 ** If tracing is enabled (by the sqlite3_trace()) interface, then
56435 ** the UTF-8 string contained in P4 is emitted on the trace callback.
56436 */
56437 case OP_Trace: {
56438 #if 0 /* local variables moved into u.cq */
56439 char *zTrace;
56440 #endif /* local variables moved into u.cq */
56441
56442 u.cq.zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql);
56443 if( u.cq.zTrace ){
56444 if( db->xTrace ){
56445 db->xTrace(db->pTraceArg, u.cq.zTrace);
56446 }
56447 #ifdef SQLITE_DEBUG
56448 if( (db->flags & SQLITE_SqlTrace)!=0 ){
56449 sqlite3DebugPrintf("SQL-trace: %s\n", u.cq.zTrace);
56450 }
56451 #endif /* SQLITE_DEBUG */
56452 }
56453 break;
56454 }
@@ -57461,11 +57352,11 @@
57461 **
57462 *************************************************************************
57463 ** This file contains routines used for walking the parser tree for
57464 ** an SQL statement.
57465 **
57466 ** $Id: walker.c,v 1.6 2009/05/28 01:00:55 drh Exp $
57467 */
57468
57469
57470 /*
57471 ** Walk an expression tree. Invoke the callback once for each node
@@ -57508,18 +57399,18 @@
57508 /*
57509 ** Call sqlite3WalkExpr() for every expression in list p or until
57510 ** an abort request is seen.
57511 */
57512 SQLITE_PRIVATE int sqlite3WalkExprList(Walker *pWalker, ExprList *p){
57513 int i, rc = WRC_Continue;
57514 struct ExprList_item *pItem;
57515 if( p ){
57516 for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){
57517 if( sqlite3WalkExpr(pWalker, pItem->pExpr) ) return WRC_Abort;
57518 }
57519 }
57520 return rc & WRC_Continue;
57521 }
57522
57523 /*
57524 ** Walk all expressions associated with SELECT statement p. Do
57525 ** not invoke the SELECT callback on p, but do (of course) invoke
@@ -57548,11 +57439,11 @@
57548 SrcList *pSrc;
57549 int i;
57550 struct SrcList_item *pItem;
57551
57552 pSrc = p->pSrc;
57553 if( pSrc ){
57554 for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){
57555 if( sqlite3WalkSelect(pWalker, pItem->pSelect) ){
57556 return WRC_Abort;
57557 }
57558 }
@@ -57601,11 +57492,11 @@
57601 **
57602 ** This file contains routines used for walking the parser tree and
57603 ** resolve all identifiers by associating them with a particular
57604 ** table and column.
57605 **
57606 ** $Id: resolve.c,v 1.28 2009/06/01 16:53:10 shane Exp $
57607 */
57608
57609 /*
57610 ** Turn the pExpr expression into an alias for the iCol-th column of the
57611 ** result set in pEList.
@@ -57660,19 +57551,18 @@
57660 }else if( ExprHasProperty(pOrig, EP_IntValue) || pOrig->u.zToken==0 ){
57661 pDup = sqlite3ExprDup(db, pOrig, 0);
57662 if( pDup==0 ) return;
57663 }else{
57664 char *zToken = pOrig->u.zToken;
 
57665 pOrig->u.zToken = 0;
57666 pDup = sqlite3ExprDup(db, pOrig, 0);
57667 pOrig->u.zToken = zToken;
57668 if( pDup==0 ) return;
57669 if( zToken ){
57670 assert( (pDup->flags & (EP_Reduced|EP_TokenOnly))==0 );
57671 pDup->flags2 |= EP2_MallocedToken;
57672 pDup->u.zToken = sqlite3DbStrDup(db, zToken);
57673 }
57674 }
57675 if( pExpr->flags & EP_ExpCollate ){
57676 pDup->pColl = pExpr->pColl;
57677 pDup->flags |= EP_ExpCollate;
57678 }
@@ -57704,11 +57594,11 @@
57704 ** value can be NULL if zDb is also NULL. If zTable is NULL it
57705 ** means that the form of the name is Z and that columns from any table
57706 ** can be used.
57707 **
57708 ** If the name cannot be resolved unambiguously, leave an error message
57709 ** in pParse and return non-zero. Return zero on success.
57710 */
57711 static int lookupName(
57712 Parse *pParse, /* The parsing context */
57713 const char *zDb, /* Name of the database containing table, or NULL */
57714 const char *zTab, /* Name of table containing column, or NULL */
@@ -57753,11 +57643,13 @@
57753 if( pItem->zAlias ){
57754 char *zTabName = pItem->zAlias;
57755 if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
57756 }else{
57757 char *zTabName = pTab->zName;
57758 if( zTabName==0 || sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
 
 
57759 if( zDb!=0 && sqlite3StrICmp(db->aDb[iDb].zName, zDb)!=0 ){
57760 continue;
57761 }
57762 }
57763 }
@@ -57832,18 +57724,16 @@
57832 for(iCol=0; iCol < pTab->nCol; iCol++, pCol++) {
57833 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
57834 cnt++;
57835 pExpr->iColumn = iCol==pTab->iPKey ? -1 : (i16)iCol;
57836 pExpr->pTab = pTab;
57837 if( iCol>=0 ){
57838 testcase( iCol==31 );
57839 testcase( iCol==32 );
57840 if( iCol>=32 ){
57841 *piColMask = 0xffffffff;
57842 }else{
57843 *piColMask |= ((u32)1)<<iCol;
57844 }
57845 }
57846 break;
57847 }
57848 }
57849 }
@@ -57880,11 +57770,11 @@
57880 assert( pExpr->x.pList==0 );
57881 assert( pExpr->x.pSelect==0 );
57882 pOrig = pEList->a[j].pExpr;
57883 if( !pNC->allowAgg && ExprHasProperty(pOrig, EP_Agg) ){
57884 sqlite3ErrorMsg(pParse, "misuse of aliased aggregate %s", zAs);
57885 return 2;
57886 }
57887 resolveAlias(pParse, pEList, j, pExpr, "");
57888 cnt = 1;
57889 pMatch = 0;
57890 assert( zTab==0 && zDb==0 );
@@ -57912,11 +57802,11 @@
57912 ** fields are not changed in any context.
57913 */
57914 if( cnt==0 && zTab==0 && ExprHasProperty(pExpr,EP_DblQuoted) ){
57915 pExpr->op = TK_STRING;
57916 pExpr->pTab = 0;
57917 return 0;
57918 }
57919
57920 /*
57921 ** cnt==0 means there was not match. cnt>1 means there were two or
57922 ** more matches. Either way, we have an error.
@@ -57967,13 +57857,13 @@
57967 assert( pTopNC!=0 );
57968 pTopNC->nRef++;
57969 if( pTopNC==pNC ) break;
57970 pTopNC = pTopNC->pNext;
57971 }
57972 return 0;
57973 } else {
57974 return 1;
57975 }
57976 }
57977
57978 /*
57979 ** This routine is callback for sqlite3WalkExpr().
@@ -58028,12 +57918,11 @@
58028 #endif /* defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY) */
58029
58030 /* A lone identifier is the name of a column.
58031 */
58032 case TK_ID: {
58033 lookupName(pParse, 0, 0, pExpr->u.zToken, pNC, pExpr);
58034 return WRC_Prune;
58035 }
58036
58037 /* A table name and column name: ID.ID
58038 ** Or a database, table and column: ID.ID.ID
58039 */
@@ -58053,12 +57942,11 @@
58053 assert( pRight->op==TK_DOT );
58054 zDb = pExpr->pLeft->u.zToken;
58055 zTable = pRight->pLeft->u.zToken;
58056 zColumn = pRight->pRight->u.zToken;
58057 }
58058 lookupName(pParse, zDb, zTable, zColumn, pNC, pExpr);
58059 return WRC_Prune;
58060 }
58061
58062 /* Resolve function names
58063 */
58064 case TK_CONST_FUNC:
@@ -58072,10 +57960,11 @@
58072 int nId; /* Number of characters in function name */
58073 const char *zId; /* The function name. */
58074 FuncDef *pDef; /* Information about the function */
58075 u8 enc = ENC(pParse->db); /* The database encoding */
58076
 
58077 assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
58078 zId = pExpr->u.zToken;
58079 nId = sqlite3Strlen30(zId);
58080 pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0);
58081 if( pDef==0 ){
@@ -58126,13 +58015,14 @@
58126 */
58127 return WRC_Prune;
58128 }
58129 #ifndef SQLITE_OMIT_SUBQUERY
58130 case TK_SELECT:
58131 case TK_EXISTS:
58132 #endif
58133 case TK_IN: {
 
58134 if( ExprHasProperty(pExpr, EP_xIsSelect) ){
58135 int nRef = pNC->nRef;
58136 #ifndef SQLITE_OMIT_CHECK
58137 if( pNC->isCheck ){
58138 sqlite3ErrorMsg(pParse,"subqueries prohibited in CHECK constraints");
@@ -58177,11 +58067,11 @@
58177 ){
58178 int i; /* Loop counter */
58179
58180 UNUSED_PARAMETER(pParse);
58181
58182 if( pE->op==TK_ID || (pE->op==TK_STRING && pE->u.zToken[0]!='\'') ){
58183 char *zCol = pE->u.zToken;
58184 for(i=0; i<pEList->nExpr; i++){
58185 char *zAs = pEList->a[i].zName;
58186 if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
58187 return i+1;
@@ -58313,11 +58203,11 @@
58313 int iCol = -1;
58314 Expr *pE, *pDup;
58315 if( pItem->done ) continue;
58316 pE = pItem->pExpr;
58317 if( sqlite3ExprIsInteger(pE, &iCol) ){
58318 if( iCol<0 || iCol>pEList->nExpr ){
58319 resolveOutOfRangeError(pParse, "ORDER", i+1, pEList->nExpr);
58320 return 1;
58321 }
58322 }else{
58323 iCol = resolveAsName(pParse, pEList, pE);
@@ -58327,13 +58217,10 @@
58327 assert(pDup);
58328 iCol = resolveOrderByTermToExprList(pParse, pSelect, pDup);
58329 }
58330 sqlite3ExprDelete(db, pDup);
58331 }
58332 if( iCol<0 ){
58333 return 1;
58334 }
58335 }
58336 if( iCol>0 ){
58337 CollSeq *pColl = pE->pColl;
58338 int flags = pE->flags & EP_ExpCollate;
58339 sqlite3ExprDelete(db, pE);
@@ -58436,13 +58323,10 @@
58436 nResult = pSelect->pEList->nExpr;
58437 pParse = pNC->pParse;
58438 for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
58439 Expr *pE = pItem->pExpr;
58440 iCol = resolveAsName(pParse, pSelect->pEList, pE);
58441 if( iCol<0 ){
58442 return 1; /* OOM error */
58443 }
58444 if( iCol>0 ){
58445 /* If an AS-name match is found, mark this ORDER BY column as being
58446 ** a copy of the iCol-th result-set column. The subsequent call to
58447 ** sqlite3ResolveOrderGroupBy() will convert the expression to a
58448 ** copy of the iCol-th result-set expression. */
@@ -58768,11 +58652,11 @@
58768 **
58769 *************************************************************************
58770 ** This file contains routines used for analyzing expressions and
58771 ** for generating VDBE code that evaluates expressions in SQLite.
58772 **
58773 ** $Id: expr.c,v 1.445 2009/06/01 16:53:10 shane Exp $
58774 */
58775
58776 /*
58777 ** Return the 'affinity' of the expression pExpr if any.
58778 **
@@ -59245,15 +59129,15 @@
59245 exprSetHeight(pRoot);
59246 }
59247 }
59248
59249 /*
59250 ** Allocate a Expr node which joins up to two subtrees.
59251 **
59252 ** The
59253 ** Works like sqlite3Expr() except that it takes an extra Parse*
59254 ** argument and notifies the associated connection object if malloc fails.
59255 */
59256 SQLITE_PRIVATE Expr *sqlite3PExpr(
59257 Parse *pParse, /* Parsing context */
59258 int op, /* Expression opcode */
59259 Expr *pLeft, /* Left operand */
@@ -64140,11 +64024,11 @@
64140 ** creating ID lists
64141 ** BEGIN TRANSACTION
64142 ** COMMIT
64143 ** ROLLBACK
64144 **
64145 ** $Id: build.c,v 1.549 2009/06/03 11:25:07 danielk1977 Exp $
64146 */
64147
64148 /*
64149 ** This routine is called when a new SQL statement is beginning to
64150 ** be parsed. Initialize the pParse structure as needed.
@@ -65372,11 +65256,10 @@
65372 pColl = sqlite3FindCollSeq(db, enc, zName, initbusy);
65373 if( !initbusy && (!pColl || !pColl->xCmp) ){
65374 pColl = sqlite3GetCollSeq(db, pColl, zName);
65375 if( !pColl ){
65376 sqlite3ErrorMsg(pParse, "no such collation sequence: %s", zName);
65377 pColl = 0;
65378 }
65379 }
65380
65381 return pColl;
65382 }
@@ -67756,11 +67639,11 @@
67756 *************************************************************************
67757 **
67758 ** This file contains functions used to access the internal hash tables
67759 ** of user defined functions and collation sequences.
67760 **
67761 ** $Id: callback.c,v 1.41 2009/05/20 02:40:46 drh Exp $
67762 */
67763
67764
67765 /*
67766 ** Invoke the 'collation needed' callback to request a collation sequence
@@ -67866,13 +67749,11 @@
67866 SQLITE_PRIVATE int sqlite3CheckCollSeq(Parse *pParse, CollSeq *pColl){
67867 if( pColl ){
67868 const char *zName = pColl->zName;
67869 CollSeq *p = sqlite3GetCollSeq(pParse->db, pColl, zName);
67870 if( !p ){
67871 if( pParse->nErr==0 ){
67872 sqlite3ErrorMsg(pParse, "no such collation sequence: %s", zName);
67873 }
67874 pParse->nErr++;
67875 return SQLITE_ERROR;
67876 }
67877 assert( p==pColl );
67878 }
@@ -68083,11 +67964,10 @@
68083 int bestScore = 0; /* Score of best match */
68084 int h; /* Hash value */
68085
68086
68087 assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE );
68088 if( nArg<-1 ) nArg = -1;
68089 h = (sqlite3UpperToLower[(u8)zName[0]] + nName) % ArraySize(db->aFunc.a);
68090
68091 /* First search for a match amongst the application-defined functions.
68092 */
68093 p = functionSearch(&db->aFunc, h, zName, nName);
@@ -68833,11 +68713,11 @@
68833 **
68834 ** There is only one exported symbol in this file - the function
68835 ** sqliteRegisterBuildinFunctions() found at the bottom of the file.
68836 ** All other code has file scope.
68837 **
68838 ** $Id: func.c,v 1.237 2009/06/03 01:24:54 drh Exp $
68839 */
68840
68841 /*
68842 ** Return the collating function associated with a function.
68843 */
@@ -70078,19 +69958,14 @@
70078 if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
70079 pAccum = (StrAccum*)sqlite3_aggregate_context(context, sizeof(*pAccum));
70080
70081 if( pAccum ){
70082 sqlite3 *db = sqlite3_context_db_handle(context);
70083 int n;
70084 pAccum->useMalloc = 1;
70085 pAccum->mxAlloc = db->aLimit[SQLITE_LIMIT_LENGTH];
70086 #ifdef SQLITE_OMIT_DEPRECATED
70087 n = context->pMem->n;
70088 #else
70089 n = sqlite3_aggregate_count(context);
70090 #endif
70091 if( n>1 ){
70092 if( argc==2 ){
70093 zSep = (char*)sqlite3_value_text(argv[1]);
70094 nSep = sqlite3_value_bytes(argv[1]);
70095 }else{
70096 zSep = ",";
@@ -70132,13 +70007,10 @@
70132 assert( rc==SQLITE_NOMEM || rc==SQLITE_OK );
70133 if( rc==SQLITE_NOMEM ){
70134 db->mallocFailed = 1;
70135 }
70136 }
70137 #ifdef SQLITE_SSE
70138 (void)sqlite3SseFunctions(db);
70139 #endif
70140 }
70141
70142 /*
70143 ** Set the LIKEOPT flag on the 2-argument function with the given name.
70144 */
@@ -73213,16 +73085,16 @@
73213 ** May you share freely, never taking more than you give.
73214 **
73215 *************************************************************************
73216 ** This file contains code used to implement the PRAGMA command.
73217 **
73218 ** $Id: pragma.c,v 1.212 2009/06/03 11:25:07 danielk1977 Exp $
73219 */
73220
73221 /* Ignore this whole file if pragmas are disabled
73222 */
73223 #if !defined(SQLITE_OMIT_PRAGMA) && !defined(SQLITE_OMIT_PARSER)
73224
73225 /*
73226 ** Interpret the given string as a safety level. Return 0 for OFF,
73227 ** 1 for ON or NORMAL and 2 for FULL. Return 1 for an empty or
73228 ** unrecognized string argument.
@@ -74549,21 +74421,10 @@
74549 }
74550
74551 }else
74552 #endif
74553
74554 #ifdef SQLITE_SSE
74555 /*
74556 ** Check to see if the sqlite_statements table exists. Create it
74557 ** if it does not.
74558 */
74559 if( sqlite3StrICmp(zLeft, "create_sqlite_statement_table")==0 ){
74560 extern int sqlite3CreateStatementsTable(Parse*);
74561 sqlite3CreateStatementsTable(pParse);
74562 }else
74563 #endif
74564
74565 #if SQLITE_HAS_CODEC
74566 if( sqlite3StrICmp(zLeft, "key")==0 && zRight ){
74567 sqlite3_key(db, zRight, sqlite3Strlen30(zRight));
74568 }else
74569 if( sqlite3StrICmp(zLeft, "rekey")==0 && zRight ){
@@ -74624,11 +74485,11 @@
74624 pragma_out:
74625 sqlite3DbFree(db, zLeft);
74626 sqlite3DbFree(db, zRight);
74627 }
74628
74629 #endif /* SQLITE_OMIT_PRAGMA || SQLITE_OMIT_PARSER */
74630
74631 /************** End of pragma.c **********************************************/
74632 /************** Begin file prepare.c *****************************************/
74633 /*
74634 ** 2005 May 25
@@ -74643,11 +74504,11 @@
74643 *************************************************************************
74644 ** This file contains the implementation of the sqlite3_prepare()
74645 ** interface, and routines that contribute to loading the database schema
74646 ** from disk.
74647 **
74648 ** $Id: prepare.c,v 1.121 2009/06/04 00:11:56 drh Exp $
74649 */
74650
74651 /*
74652 ** Fill the InitData structure with an error message that indicates
74653 ** that the database is corrupt.
@@ -74660,16 +74521,16 @@
74660 sqlite3 *db = pData->db;
74661 if( !db->mallocFailed && (db->flags & SQLITE_RecoveryMode)==0 ){
74662 if( zObj==0 ) zObj = "?";
74663 sqlite3SetString(pData->pzErrMsg, pData->db,
74664 "malformed database schema (%s)", zObj);
74665 if( zExtra && zExtra[0] ){
74666 *pData->pzErrMsg = sqlite3MAppendf(pData->db, *pData->pzErrMsg, "%s - %s",
74667 *pData->pzErrMsg, zExtra);
74668 }
74669 }
74670 pData->rc = SQLITE_CORRUPT;
74671 }
74672
74673 /*
74674 ** This is the callback routine for the code that initializes the
74675 ** database. See sqlite3Init() below for additional information.
@@ -74691,11 +74552,11 @@
74691 UNUSED_PARAMETER2(NotUsed, argc);
74692 assert( sqlite3_mutex_held(db->mutex) );
74693 DbClearProperty(db, iDb, DB_Empty);
74694 if( db->mallocFailed ){
74695 corruptSchema(pData, argv[0], 0);
74696 return SQLITE_NOMEM;
74697 }
74698
74699 assert( iDb>=0 && iDb<db->nDb );
74700 if( argv==0 ) return 0; /* Might happen if EMPTY_RESULT_CALLBACKS are on */
74701 if( argv[1]==0 ){
@@ -74716,11 +74577,11 @@
74716 assert( rc!=SQLITE_OK || zErr==0 );
74717 if( SQLITE_OK!=rc ){
74718 pData->rc = rc;
74719 if( rc==SQLITE_NOMEM ){
74720 db->mallocFailed = 1;
74721 }else if( rc!=SQLITE_INTERRUPT && (rc&0xff)!=SQLITE_LOCKED ){
74722 corruptSchema(pData, argv[0], zErr);
74723 }
74724 sqlite3DbFree(db, zErr);
74725 }
74726 }else if( argv[0]==0 ){
@@ -74732,19 +74593,19 @@
74732 ** been created when we processed the CREATE TABLE. All we have
74733 ** to do here is record the root page number for that index.
74734 */
74735 Index *pIndex;
74736 pIndex = sqlite3FindIndex(db, argv[0], db->aDb[iDb].zName);
74737 if( pIndex==0 || pIndex->tnum!=0 ){
74738 /* This can occur if there exists an index on a TEMP table which
74739 ** has the same name as another index on a permanent index. Since
74740 ** the permanent table is hidden by the TEMP table, we can also
74741 ** safely ignore the index on the permanent table.
74742 */
74743 /* Do Nothing */;
74744 }else{
74745 pIndex->tnum = atoi(argv[1]);
74746 }
74747 }
74748 return 0;
74749 }
74750
@@ -74826,19 +74687,19 @@
74826 if( initData.rc ){
74827 rc = initData.rc;
74828 goto error_out;
74829 }
74830 pTab = sqlite3FindTable(db, zMasterName, db->aDb[iDb].zName);
74831 if( pTab ){
74832 pTab->tabFlags |= TF_Readonly;
74833 }
74834
74835 /* Create a cursor to hold the database open
74836 */
74837 pDb = &db->aDb[iDb];
74838 if( pDb->pBt==0 ){
74839 if( !OMIT_TEMPDB && iDb==1 ){
74840 DbSetProperty(db, 1, DB_SchemaLoaded);
74841 }
74842 return SQLITE_OK;
74843 }
74844 curMain = sqlite3MallocZero(sqlite3BtreeCursorSize());
@@ -74854,12 +74715,17 @@
74854 **
74855 ** Meta values are as follows:
74856 ** meta[0] Schema cookie. Changes with each schema change.
74857 ** meta[1] File format of schema layer.
74858 ** meta[2] Size of the page cache.
74859 ** meta[3] Use freelist if 0. Autovacuum if greater than zero.
74860 ** meta[4] Db text encoding. 1:UTF-8 2:UTF-16LE 3:UTF-16BE
 
 
 
 
 
74861 **
74862 ** Note: The #defined SQLITE_UTF* symbols in sqliteInt.h correspond to
74863 ** the possible values of meta[4].
74864 */
74865 for(i=0; rc==SQLITE_OK && i<ArraySize(meta); i++){
@@ -74932,14 +74798,11 @@
74932 }
74933
74934 /* Read the schema information out of the schema tables
74935 */
74936 assert( db->init.busy );
74937 if( rc==SQLITE_EMPTY ){
74938 /* For an empty database, there is nothing to read */
74939 rc = SQLITE_OK;
74940 }else{
74941 char *zSql;
74942 zSql = sqlite3MPrintf(db,
74943 "SELECT name, rootpage, sql FROM '%q'.%s",
74944 db->aDb[iDb].zName, zMasterName);
74945 (void)sqlite3SafetyOff(db);
@@ -75009,11 +74872,10 @@
75009 SQLITE_PRIVATE int sqlite3Init(sqlite3 *db, char **pzErrMsg){
75010 int i, rc;
75011 int commit_internal = !(db->flags&SQLITE_InternChanges);
75012
75013 assert( sqlite3_mutex_held(db->mutex) );
75014 if( db->init.busy ) return SQLITE_OK;
75015 rc = SQLITE_OK;
75016 db->init.busy = 1;
75017 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
75018 if( DbHasProperty(db, i, DB_SchemaLoaded) || i==1 ) continue;
75019 rc = sqlite3InitOne(db, i, pzErrMsg);
@@ -75025,11 +74887,12 @@
75025 /* Once all the other databases have been initialised, load the schema
75026 ** for the TEMP database. This is loaded last, as the TEMP database
75027 ** schema may contain references to objects in other databases.
75028 */
75029 #ifndef SQLITE_OMIT_TEMPDB
75030 if( rc==SQLITE_OK && db->nDb>1 && !DbHasProperty(db, 1, DB_SchemaLoaded) ){
 
75031 rc = sqlite3InitOne(db, 1, pzErrMsg);
75032 if( rc ){
75033 sqlite3ResetInternalSchema(db, 1);
75034 }
75035 }
@@ -75082,16 +74945,17 @@
75082 if( pBt==0 ) continue;
75083 memset(curTemp, 0, sqlite3BtreeCursorSize());
75084 rc = sqlite3BtreeCursor(pBt, MASTER_ROOT, 0, 0, curTemp);
75085 if( rc==SQLITE_OK ){
75086 rc = sqlite3BtreeGetMeta(pBt, BTREE_SCHEMA_VERSION, (u32 *)&cookie);
75087 if( rc==SQLITE_OK && cookie!=db->aDb[iDb].pSchema->schema_cookie ){
 
75088 allOk = 0;
75089 }
75090 sqlite3BtreeCloseCursor(curTemp);
75091 }
75092 if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
75093 db->mallocFailed = 1;
75094 }
75095 }
75096 sqlite3_free(curTemp);
75097 }else{
@@ -75206,10 +75070,12 @@
75206
75207 pParse->db = db;
75208 if( nBytes>=0 && (nBytes==0 || zSql[nBytes-1]!=0) ){
75209 char *zSqlCopy;
75210 int mxLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
 
 
75211 if( nBytes>mxLen ){
75212 sqlite3Error(db, SQLITE_TOOBIG, "statement too long");
75213 (void)sqlite3SafetyOff(db);
75214 rc = sqlite3ApiExit(db, SQLITE_TOOBIG);
75215 goto end_prepare;
@@ -75312,10 +75178,14 @@
75312 return SQLITE_MISUSE;
75313 }
75314 sqlite3_mutex_enter(db->mutex);
75315 sqlite3BtreeEnterAll(db);
75316 rc = sqlite3Prepare(db, zSql, nBytes, saveSqlFlag, ppStmt, pzTail);
 
 
 
 
75317 sqlite3BtreeLeaveAll(db);
75318 sqlite3_mutex_leave(db->mutex);
75319 return rc;
75320 }
75321
@@ -75485,11 +75355,11 @@
75485 **
75486 *************************************************************************
75487 ** This file contains C code routines that are called by the parser
75488 ** to handle SELECT statements in SQLite.
75489 **
75490 ** $Id: select.c,v 1.523 2009/06/01 16:53:10 shane Exp $
75491 */
75492
75493
75494 /*
75495 ** Delete all the content of a Select structure but do not deallocate
@@ -78269,10 +78139,11 @@
78269
78270 /* Transfer the FROM clause terms from the subquery into the
78271 ** outer query.
78272 */
78273 for(i=0; i<nSubSrc; i++){
 
78274 pSrc->a[i+iFrom] = pSubSrc->a[i];
78275 memset(&pSubSrc->a[i], 0, sizeof(pSubSrc->a[i]));
78276 }
78277 pSrc->a[iFrom].jointype = jointype;
78278
@@ -81764,11 +81635,11 @@
81764 ** May you share freely, never taking more than you give.
81765 **
81766 *************************************************************************
81767 ** This file contains code used to help implement virtual tables.
81768 **
81769 ** $Id: vtab.c,v 1.90 2009/06/02 21:31:39 drh Exp $
81770 */
81771 #ifndef SQLITE_OMIT_VIRTUALTABLE
81772
81773 /*
81774 ** The actual function that does the work of creating a new module.
@@ -81854,10 +81725,13 @@
81854 /*
81855 ** Unlock a virtual table. When the last lock is removed,
81856 ** disconnect the virtual table.
81857 */
81858 SQLITE_PRIVATE void sqlite3VtabUnlock(sqlite3 *db, sqlite3_vtab *pVtab){
 
 
 
81859 assert( pVtab->nRef>0 );
81860 pVtab->nRef--;
81861 assert(db);
81862 assert( sqlite3SafetyCheckOk(db) );
81863 if( pVtab->nRef==0 ){
@@ -82633,11 +82507,11 @@
82633 ** generating the code that loops through a table looking for applicable
82634 ** rows. Indices are selected and used to speed the search when doing
82635 ** so is applicable. Because this module is responsible for selecting
82636 ** indices, you might also think of this module as the "query optimizer".
82637 **
82638 ** $Id: where.c,v 1.401 2009/06/06 15:17:28 drh Exp $
82639 */
82640
82641 /*
82642 ** Trace output macros
82643 */
@@ -83275,23 +83149,24 @@
83275 if( pColl==0 ) return 0;
83276 if( (pColl->type!=SQLITE_COLL_BINARY || *pnoCase) &&
83277 (pColl->type!=SQLITE_COLL_NOCASE || !*pnoCase) ){
83278 return 0;
83279 }
 
83280 z = pRight->u.zToken;
83281 cnt = 0;
83282 if( ALWAYS(z) ){
 
83283 while( (c=z[cnt])!=0 && c!=wc[0] && c!=wc[1] && c!=wc[2] ){
83284 cnt++;
83285 }
 
 
 
 
 
83286 }
83287 if( cnt==0 || c==0 || 255==(u8)z[cnt-1] ){
83288 return 0;
83289 }
83290 *pisComplete = z[cnt]==wc[0] && z[cnt+1]==0;
83291 *pnPattern = cnt;
83292 return 1;
83293 }
83294 #endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */
83295
83296
83297 #ifndef SQLITE_OMIT_VIRTUALTABLE
@@ -83507,10 +83382,26 @@
83507
83508 /*
83509 ** chngToIN holds a set of tables that *might* satisfy case 1. But
83510 ** we have to do some additional checking to see if case 1 really
83511 ** is satisfied.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83512 */
83513 if( chngToIN ){
83514 int okToChngToIN = 0; /* True if the conversion to IN is valid */
83515 int iColumn = -1; /* Column index on lhs of IN operator */
83516 int iCursor = -1; /* Table cursor common to all terms */
@@ -83525,22 +83416,42 @@
83525 for(j=0; j<2 && !okToChngToIN; j++){
83526 pOrTerm = pOrWc->a;
83527 for(i=pOrWc->nTerm-1; i>=0; i--, pOrTerm++){
83528 assert( pOrTerm->eOperator==WO_EQ );
83529 pOrTerm->wtFlags &= ~TERM_OR_OK;
83530 if( pOrTerm->leftCursor==iColumn ) continue;
83531 if( (chngToIN & getMask(pMaskSet, pOrTerm->leftCursor))==0 ) continue;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83532 iColumn = pOrTerm->u.leftColumn;
83533 iCursor = pOrTerm->leftCursor;
83534 break;
83535 }
83536 if( i<0 ){
 
 
83537 assert( j==1 );
83538 assert( (chngToIN&(chngToIN-1))==0 );
83539 assert( chngToIN==getMask(pMaskSet, iColumn) );
83540 break;
83541 }
 
 
 
 
83542 okToChngToIN = 1;
83543 for(; i>=0 && okToChngToIN; i--, pOrTerm++){
83544 assert( pOrTerm->eOperator==WO_EQ );
83545 if( pOrTerm->leftCursor!=iCursor ){
83546 pOrTerm->wtFlags &= ~TERM_OR_OK;
@@ -83782,15 +83693,22 @@
83782 pRight = pExpr->x.pList->a[0].pExpr;
83783 pStr1 = sqlite3Expr(db, TK_STRING, pRight->u.zToken);
83784 if( pStr1 ) pStr1->u.zToken[nPattern] = 0;
83785 pStr2 = sqlite3ExprDup(db, pStr1, 0);
83786 if( !db->mallocFailed ){
83787 u8 c, *pC;
83788 pC = (u8*)&pStr2->u.zToken[nPattern-1];
83789 c = *pC;
83790 if( noCase ){
83791 if( c=='@' ) isComplete = 0;
 
 
 
 
 
 
 
83792 c = sqlite3UpperToLower[c];
83793 }
83794 *pC = c + 1;
83795 }
83796 pNewExpr1 = sqlite3PExpr(pParse, TK_GE, sqlite3ExprDup(db,pLeft,0),pStr1,0);
@@ -84456,11 +84374,11 @@
84456 pCost->rCost = (SQLITE_BIG_DBL/((double)2));
84457 }else{
84458 pCost->rCost = pIdxInfo->estimatedCost;
84459 }
84460 pCost->plan.u.pVtabIdx = pIdxInfo;
84461 if( pIdxInfo && pIdxInfo->orderByConsumed ){
84462 pCost->plan.wsFlags |= WHERE_ORDERBY;
84463 }
84464 pCost->plan.nEq = 0;
84465 pIdxInfo->nOrderBy = nOrderBy;
84466
@@ -84742,11 +84660,11 @@
84742 }
84743 }else{
84744 cost += cost*estLog(cost);
84745 WHERETRACE(("...... orderby increases cost to %.9g\n", cost));
84746 }
84747 }else if( pParse->db->flags & SQLITE_ReverseOrder ){
84748 /* For application testing, randomly reverse the output order for
84749 ** SELECT statements that omit the ORDER BY clause. This will help
84750 ** to find cases where
84751 */
84752 wsFlags |= WHERE_REVERSE;
@@ -84805,18 +84723,21 @@
84805 struct SrcList_item *pSrc, /* The FROM clause term to search */
84806 Bitmask notReady, /* Mask of cursors that are not available */
84807 ExprList *pOrderBy, /* The ORDER BY clause */
84808 WhereCost *pCost /* Lowest cost query plan */
84809 ){
 
84810 if( IsVirtual(pSrc->pTab) ){
84811 sqlite3_index_info *p = 0;
84812 bestVirtualIndex(pParse, pWC, pSrc, notReady, pOrderBy, pCost, &p);
84813 if( p->needToFreeIdxStr ){
84814 sqlite3_free(p->idxStr);
84815 }
84816 sqlite3DbFree(pParse->db, p);
84817 }else{
 
 
84818 bestBtreeIndex(pParse, pWC, pSrc, notReady, pOrderBy, pCost);
84819 }
84820 }
84821
84822 /*
@@ -85448,12 +85369,12 @@
85448 WhereClause *pOrWc; /* The OR-clause broken out into subterms */
85449 WhereTerm *pFinal; /* Final subterm within the OR-clause. */
85450 SrcList oneTab; /* Shortened table list */
85451
85452 int regReturn = ++pParse->nMem; /* Register used with OP_Gosub */
85453 int regRowset; /* Register for RowSet object */
85454 int regRowid; /* Register holding rowid */
85455 int iLoopBody = sqlite3VdbeMakeLabel(v); /* Start of loop body */
85456 int iRetInit; /* Address of regReturn init */
85457 int ii;
85458
85459 pTerm = pLevel->plan.u.pTerm;
@@ -85488,21 +85409,20 @@
85488
85489 for(ii=0; ii<pOrWc->nTerm; ii++){
85490 WhereTerm *pOrTerm = &pOrWc->a[ii];
85491 if( pOrTerm->leftCursor==iCur || pOrTerm->eOperator==WO_AND ){
85492 WhereInfo *pSubWInfo; /* Info for single OR-term scan */
85493
85494 /* Loop through table entries that match term pOrTerm. */
85495 pSubWInfo = sqlite3WhereBegin(pParse, &oneTab, pOrTerm->pExpr, 0,
85496 WHERE_OMIT_OPEN | WHERE_OMIT_CLOSE | WHERE_FORCE_TABLE);
85497 if( pSubWInfo ){
85498 if( (wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
85499 int iSet = ((ii==pOrWc->nTerm-1)?-1:ii);
85500 int r;
85501 r = sqlite3ExprCodeGetColumn(pParse, pTabItem->pTab, -1, iCur,
85502 regRowid, 0);
85503 sqlite3VdbeAddOp4(v, OP_RowSetTest, regRowset,
85504 sqlite3VdbeCurrentAddr(v)+2,
85505 r, SQLITE_INT_TO_PTR(iSet), P4_INT32);
85506 }
85507 sqlite3VdbeAddOp2(v, OP_Gosub, regReturn, iLoopBody);
85508
@@ -85789,13 +85709,15 @@
85789 ** with virtual tables.
85790 */
85791 assert( pWC->vmask==0 && pMaskSet->n==0 );
85792 for(i=0; i<pTabList->nSrc; i++){
85793 createMask(pMaskSet, pTabList->a[i].iCursor);
85794 if( pTabList->a[i].pTab && IsVirtual(pTabList->a[i].pTab) ){
 
85795 pWC->vmask |= ((Bitmask)1 << i);
85796 }
 
85797 }
85798 #ifndef NDEBUG
85799 {
85800 Bitmask toTheLeft = 0;
85801 for(i=0; i<pTabList->nSrc; i++){
@@ -86209,10 +86131,21 @@
86209 */
86210 /* First off, code is included that follows the "include" declaration
86211 ** in the input grammar file. */
86212
86213
 
 
 
 
 
 
 
 
 
 
 
86214 /*
86215 ** An instance of this structure holds information about the
86216 ** LIMIT clause of a SELECT statement.
86217 */
86218 struct LimitVal {
@@ -86354,11 +86287,11 @@
86354 ** YYNSTATE the combined number of states.
86355 ** YYNRULE the number of rules in the grammar
86356 ** YYERRORSYMBOL is the code number of the error symbol. If not
86357 ** defined, then do no error processing.
86358 */
86359 #define YYCODETYPE unsigned short int
86360 #define YYNOCODE 252
86361 #define YYACTIONTYPE unsigned short int
86362 #define YYWILDCARD 65
86363 #define sqlite3ParserTOKENTYPE Token
86364 typedef union {
@@ -86392,10 +86325,22 @@
86392 #define YY_ERROR_ACTION (YYNSTATE+YYNRULE)
86393
86394 /* The yyzerominor constant is used to initialize instances of
86395 ** YYMINORTYPE objects to zero. */
86396 static const YYMINORTYPE yyzerominor = { 0 };
 
 
 
 
 
 
 
 
 
 
 
 
86397
86398
86399 /* Next are the tables used to determine what action to take based on the
86400 ** current state and lookahead token. These tables are used to implement
86401 ** functions that take a state number and lookahead value and return an
@@ -86970,86 +86915,10 @@
86970 26, /* VIEW => ID */
86971 26, /* VIRTUAL => ID */
86972 26, /* REINDEX => ID */
86973 26, /* RENAME => ID */
86974 26, /* CTIME_KW => ID */
86975 0, /* ANY => nothing */
86976 0, /* OR => nothing */
86977 0, /* AND => nothing */
86978 0, /* IS => nothing */
86979 0, /* BETWEEN => nothing */
86980 0, /* IN => nothing */
86981 0, /* ISNULL => nothing */
86982 0, /* NOTNULL => nothing */
86983 0, /* NE => nothing */
86984 0, /* EQ => nothing */
86985 0, /* GT => nothing */
86986 0, /* LE => nothing */
86987 0, /* LT => nothing */
86988 0, /* GE => nothing */
86989 0, /* ESCAPE => nothing */
86990 0, /* BITAND => nothing */
86991 0, /* BITOR => nothing */
86992 0, /* LSHIFT => nothing */
86993 0, /* RSHIFT => nothing */
86994 0, /* PLUS => nothing */
86995 0, /* MINUS => nothing */
86996 0, /* STAR => nothing */
86997 0, /* SLASH => nothing */
86998 0, /* REM => nothing */
86999 0, /* CONCAT => nothing */
87000 0, /* COLLATE => nothing */
87001 0, /* UMINUS => nothing */
87002 0, /* UPLUS => nothing */
87003 0, /* BITNOT => nothing */
87004 0, /* STRING => nothing */
87005 0, /* JOIN_KW => nothing */
87006 0, /* CONSTRAINT => nothing */
87007 0, /* DEFAULT => nothing */
87008 0, /* NULL => nothing */
87009 0, /* PRIMARY => nothing */
87010 0, /* UNIQUE => nothing */
87011 0, /* CHECK => nothing */
87012 0, /* REFERENCES => nothing */
87013 0, /* AUTOINCR => nothing */
87014 0, /* ON => nothing */
87015 0, /* DELETE => nothing */
87016 0, /* UPDATE => nothing */
87017 0, /* INSERT => nothing */
87018 0, /* SET => nothing */
87019 0, /* DEFERRABLE => nothing */
87020 0, /* FOREIGN => nothing */
87021 0, /* DROP => nothing */
87022 0, /* UNION => nothing */
87023 0, /* ALL => nothing */
87024 0, /* EXCEPT => nothing */
87025 0, /* INTERSECT => nothing */
87026 0, /* SELECT => nothing */
87027 0, /* DISTINCT => nothing */
87028 0, /* DOT => nothing */
87029 0, /* FROM => nothing */
87030 0, /* JOIN => nothing */
87031 0, /* USING => nothing */
87032 0, /* ORDER => nothing */
87033 0, /* GROUP => nothing */
87034 0, /* HAVING => nothing */
87035 0, /* LIMIT => nothing */
87036 0, /* WHERE => nothing */
87037 0, /* INTO => nothing */
87038 0, /* VALUES => nothing */
87039 0, /* INTEGER => nothing */
87040 0, /* FLOAT => nothing */
87041 0, /* BLOB => nothing */
87042 0, /* REGISTER => nothing */
87043 0, /* VARIABLE => nothing */
87044 0, /* CASE => nothing */
87045 0, /* WHEN => nothing */
87046 0, /* THEN => nothing */
87047 0, /* ELSE => nothing */
87048 0, /* INDEX => nothing */
87049 0, /* ALTER => nothing */
87050 0, /* ADD => nothing */
87051 };
87052 #endif /* YYFALLBACK */
87053
87054 /* The following structure represents a single element of the
87055 ** parser's stack. Information stored includes:
@@ -88270,52 +88139,10 @@
88270 ** #line <lineno> <grammarfile>
88271 ** { ... } // User supplied code
88272 ** #line <lineno> <thisfile>
88273 ** break;
88274 */
88275 case 0: /* input ::= cmdlist */
88276 case 1: /* cmdlist ::= cmdlist ecmd */
88277 case 2: /* cmdlist ::= ecmd */
88278 case 3: /* ecmd ::= SEMI */
88279 case 4: /* ecmd ::= explain cmdx SEMI */
88280 case 10: /* trans_opt ::= */
88281 case 11: /* trans_opt ::= TRANSACTION */
88282 case 12: /* trans_opt ::= TRANSACTION nm */
88283 case 20: /* savepoint_opt ::= SAVEPOINT */
88284 case 21: /* savepoint_opt ::= */
88285 case 25: /* cmd ::= create_table create_table_args */
88286 case 34: /* columnlist ::= columnlist COMMA column */
88287 case 35: /* columnlist ::= column */
88288 case 44: /* type ::= */
88289 case 51: /* signed ::= plus_num */
88290 case 52: /* signed ::= minus_num */
88291 case 53: /* carglist ::= carglist carg */
88292 case 54: /* carglist ::= */
88293 case 55: /* carg ::= CONSTRAINT nm ccons */
88294 case 56: /* carg ::= ccons */
88295 case 62: /* ccons ::= NULL onconf */
88296 case 89: /* conslist ::= conslist COMMA tcons */
88297 case 90: /* conslist ::= conslist tcons */
88298 case 91: /* conslist ::= tcons */
88299 case 92: /* tcons ::= CONSTRAINT nm */
88300 case 268: /* plus_opt ::= PLUS */
88301 case 269: /* plus_opt ::= */
88302 case 279: /* foreach_clause ::= */
88303 case 280: /* foreach_clause ::= FOR EACH ROW */
88304 case 300: /* database_kw_opt ::= DATABASE */
88305 case 301: /* database_kw_opt ::= */
88306 case 309: /* kwcolumn_opt ::= */
88307 case 310: /* kwcolumn_opt ::= COLUMNKW */
88308 case 314: /* vtabarglist ::= vtabarg */
88309 case 315: /* vtabarglist ::= vtabarglist COMMA vtabarg */
88310 case 317: /* vtabarg ::= vtabarg vtabargtoken */
88311 case 321: /* anylist ::= */
88312 case 322: /* anylist ::= anylist LP anylist RP */
88313 case 323: /* anylist ::= anylist ANY */
88314 {
88315 }
88316 break;
88317 case 5: /* explain ::= */
88318 { sqlite3BeginParse(pParse, 0); }
88319 break;
88320 case 6: /* explain ::= EXPLAIN */
88321 { sqlite3BeginParse(pParse, 1); }
@@ -88331,18 +88158,18 @@
88331 break;
88332 case 13: /* transtype ::= */
88333 {yygotominor.yy194 = TK_DEFERRED;}
88334 break;
88335 case 14: /* transtype ::= DEFERRED */
88336 case 15: /* transtype ::= IMMEDIATE */
88337 case 16: /* transtype ::= EXCLUSIVE */
88338 case 114: /* multiselect_op ::= UNION */
88339 case 116: /* multiselect_op ::= EXCEPT|INTERSECT */
88340 {yygotominor.yy194 = yymsp[0].major;}
88341 break;
88342 case 17: /* cmd ::= COMMIT trans_opt */
88343 case 18: /* cmd ::= END trans_opt */
88344 {sqlite3CommitTransaction(pParse);}
88345 break;
88346 case 19: /* cmd ::= ROLLBACK trans_opt */
88347 {sqlite3RollbackTransaction(pParse);}
88348 break;
@@ -88371,30 +88198,30 @@
88371 pParse->db->lookaside.bEnabled = 0;
88372 yygotominor.yy0 = yymsp[0].minor.yy0;
88373 }
88374 break;
88375 case 28: /* ifnotexists ::= */
88376 case 31: /* temp ::= */
88377 case 70: /* autoinc ::= */
88378 case 84: /* init_deferred_pred_opt ::= */
88379 case 86: /* init_deferred_pred_opt ::= INITIALLY IMMEDIATE */
88380 case 97: /* defer_subclause_opt ::= */
88381 case 108: /* ifexists ::= */
88382 case 119: /* distinct ::= ALL */
88383 case 120: /* distinct ::= */
88384 case 222: /* between_op ::= BETWEEN */
88385 case 225: /* in_op ::= IN */
88386 {yygotominor.yy194 = 0;}
88387 break;
88388 case 29: /* ifnotexists ::= IF NOT EXISTS */
88389 case 30: /* temp ::= TEMP */
88390 case 71: /* autoinc ::= AUTOINCR */
88391 case 85: /* init_deferred_pred_opt ::= INITIALLY DEFERRED */
88392 case 107: /* ifexists ::= IF EXISTS */
88393 case 118: /* distinct ::= DISTINCT */
88394 case 223: /* between_op ::= NOT BETWEEN */
88395 case 226: /* in_op ::= NOT IN */
88396 {yygotominor.yy194 = 1;}
88397 break;
88398 case 32: /* create_table_args ::= LP columnlist conslist_opt RP */
88399 {
88400 sqlite3EndTable(pParse,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0,0);
@@ -88417,30 +88244,30 @@
88417 sqlite3AddColumn(pParse,&yymsp[0].minor.yy0);
88418 yygotominor.yy0 = yymsp[0].minor.yy0;
88419 }
88420 break;
88421 case 38: /* id ::= ID */
88422 case 39: /* id ::= INDEXED */
88423 case 40: /* ids ::= ID|STRING */
88424 case 41: /* nm ::= id */
88425 case 42: /* nm ::= STRING */
88426 case 43: /* nm ::= JOIN_KW */
88427 case 46: /* typetoken ::= typename */
88428 case 49: /* typename ::= ids */
88429 case 126: /* as ::= AS nm */
88430 case 127: /* as ::= ids */
88431 case 137: /* dbnm ::= DOT nm */
88432 case 146: /* indexed_opt ::= INDEXED BY nm */
88433 case 251: /* collate ::= COLLATE ids */
88434 case 260: /* nmnum ::= plus_num */
88435 case 261: /* nmnum ::= nm */
88436 case 262: /* nmnum ::= ON */
88437 case 263: /* nmnum ::= DELETE */
88438 case 264: /* nmnum ::= DEFAULT */
88439 case 265: /* plus_num ::= plus_opt number */
88440 case 266: /* minus_num ::= MINUS number */
88441 case 267: /* number ::= INTEGER|FLOAT */
88442 {yygotominor.yy0 = yymsp[0].minor.yy0;}
88443 break;
88444 case 45: /* type ::= typetoken */
88445 {sqlite3AddColumnType(pParse,&yymsp[0].minor.yy0);}
88446 break;
@@ -88458,11 +88285,11 @@
88458 break;
88459 case 50: /* typename ::= typename ids */
88460 {yygotominor.yy0.z=yymsp[-1].minor.yy0.z; yygotominor.yy0.n=yymsp[0].minor.yy0.n+(int)(yymsp[0].minor.yy0.z-yymsp[-1].minor.yy0.z);}
88461 break;
88462 case 57: /* ccons ::= DEFAULT term */
88463 case 59: /* ccons ::= DEFAULT PLUS term */
88464 {sqlite3AddDefaultValue(pParse,&yymsp[0].minor.yy190);}
88465 break;
88466 case 58: /* ccons ::= DEFAULT LP expr RP */
88467 {sqlite3AddDefaultValue(pParse,&yymsp[-1].minor.yy190);}
88468 break;
@@ -88532,16 +88359,16 @@
88532 break;
88533 case 81: /* refact ::= RESTRICT */
88534 { yygotominor.yy194 = OE_Restrict; }
88535 break;
88536 case 82: /* defer_subclause ::= NOT DEFERRABLE init_deferred_pred_opt */
88537 case 83: /* defer_subclause ::= DEFERRABLE init_deferred_pred_opt */
88538 case 98: /* defer_subclause_opt ::= defer_subclause */
88539 case 100: /* onconf ::= ON CONFLICT resolvetype */
88540 case 102: /* orconf ::= OR resolvetype */
88541 case 103: /* resolvetype ::= raisetype */
88542 case 175: /* insert_cmd ::= INSERT orconf */
88543 {yygotominor.yy194 = yymsp[0].minor.yy194;}
88544 break;
88545 case 87: /* conslist_opt ::= */
88546 {yygotominor.yy0.n = 0; yygotominor.yy0.z = 0;}
88547 break;
@@ -88562,18 +88389,18 @@
88562 sqlite3CreateForeignKey(pParse, yymsp[-6].minor.yy148, &yymsp[-3].minor.yy0, yymsp[-2].minor.yy148, yymsp[-1].minor.yy194);
88563 sqlite3DeferForeignKey(pParse, yymsp[0].minor.yy194);
88564 }
88565 break;
88566 case 99: /* onconf ::= */
88567 case 101: /* orconf ::= */
88568 {yygotominor.yy194 = OE_Default;}
88569 break;
88570 case 104: /* resolvetype ::= IGNORE */
88571 {yygotominor.yy194 = OE_Ignore;}
88572 break;
88573 case 105: /* resolvetype ::= REPLACE */
88574 case 176: /* insert_cmd ::= REPLACE */
88575 {yygotominor.yy194 = OE_Replace;}
88576 break;
88577 case 106: /* cmd ::= DROP TABLE ifexists fullname */
88578 {
88579 sqlite3DropTable(pParse, yymsp[0].minor.yy185, 0, yymsp[-1].minor.yy194);
@@ -88617,18 +88444,18 @@
88617 {
88618 yygotominor.yy243 = sqlite3SelectNew(pParse,yymsp[-6].minor.yy148,yymsp[-5].minor.yy185,yymsp[-4].minor.yy72,yymsp[-3].minor.yy148,yymsp[-2].minor.yy72,yymsp[-1].minor.yy148,yymsp[-7].minor.yy194,yymsp[0].minor.yy354.pLimit,yymsp[0].minor.yy354.pOffset);
88619 }
88620 break;
88621 case 121: /* sclp ::= selcollist COMMA */
88622 case 247: /* idxlist_opt ::= LP idxlist RP */
88623 {yygotominor.yy148 = yymsp[-1].minor.yy148;}
88624 break;
88625 case 122: /* sclp ::= */
88626 case 150: /* orderby_opt ::= */
88627 case 158: /* groupby_opt ::= */
88628 case 240: /* exprlist ::= */
88629 case 246: /* idxlist_opt ::= */
88630 {yygotominor.yy148 = 0;}
88631 break;
88632 case 123: /* selcollist ::= sclp expr as */
88633 {
88634 yygotominor.yy148 = sqlite3ExprListAppend(pParse, yymsp[-2].minor.yy148, yymsp[-1].minor.yy190.pExpr);
@@ -88663,11 +88490,11 @@
88663 }
88664 break;
88665 case 131: /* stl_prefix ::= seltablist joinop */
88666 {
88667 yygotominor.yy185 = yymsp[-1].minor.yy185;
88668 if( yygotominor.yy185 && yygotominor.yy185->nSrc>0 ) yygotominor.yy185->a[yygotominor.yy185->nSrc-1].jointype = (u8)yymsp[0].minor.yy194;
88669 }
88670 break;
88671 case 132: /* stl_prefix ::= */
88672 {yygotominor.yy185 = 0;}
88673 break;
@@ -88682,11 +88509,13 @@
88682 yygotominor.yy185 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-6].minor.yy185,0,0,&yymsp[-2].minor.yy0,yymsp[-4].minor.yy243,yymsp[-1].minor.yy72,yymsp[0].minor.yy254);
88683 }
88684 break;
88685 case 135: /* seltablist ::= stl_prefix LP seltablist RP as on_opt using_opt */
88686 {
88687 if( yymsp[-6].minor.yy185==0 && yymsp[-2].minor.yy0.n==0 && yymsp[-1].minor.yy72==0 && yymsp[0].minor.yy254==0 ){
 
 
88688 yygotominor.yy185 = yymsp[-4].minor.yy185;
88689 }else{
88690 Select *pSubquery;
88691 sqlite3SrcListShiftJoinType(yymsp[-4].minor.yy185);
88692 pSubquery = sqlite3SelectNew(pParse,0,yymsp[-4].minor.yy185,0,0,0,0,0,0,0);
@@ -88693,11 +88522,11 @@
88693 yygotominor.yy185 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-6].minor.yy185,0,0,&yymsp[-2].minor.yy0,pSubquery,yymsp[-1].minor.yy72,yymsp[0].minor.yy254);
88694 }
88695 }
88696 break;
88697 case 136: /* dbnm ::= */
88698 case 145: /* indexed_opt ::= */
88699 {yygotominor.yy0.z=0; yygotominor.yy0.n=0;}
88700 break;
88701 case 138: /* fullname ::= nm dbnm */
88702 {yygotominor.yy185 = sqlite3SrcListAppend(pParse->db,0,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0);}
88703 break;
@@ -88712,38 +88541,38 @@
88712 break;
88713 case 142: /* joinop ::= JOIN_KW nm nm JOIN */
88714 { yygotominor.yy194 = sqlite3JoinType(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0); }
88715 break;
88716 case 143: /* on_opt ::= ON expr */
88717 case 154: /* sortitem ::= expr */
88718 case 161: /* having_opt ::= HAVING expr */
88719 case 168: /* where_opt ::= WHERE expr */
88720 case 235: /* case_else ::= ELSE expr */
88721 case 237: /* case_operand ::= expr */
88722 {yygotominor.yy72 = yymsp[0].minor.yy190.pExpr;}
88723 break;
88724 case 144: /* on_opt ::= */
88725 case 160: /* having_opt ::= */
88726 case 167: /* where_opt ::= */
88727 case 236: /* case_else ::= */
88728 case 238: /* case_operand ::= */
88729 {yygotominor.yy72 = 0;}
88730 break;
88731 case 147: /* indexed_opt ::= NOT INDEXED */
88732 {yygotominor.yy0.z=0; yygotominor.yy0.n=1;}
88733 break;
88734 case 148: /* using_opt ::= USING LP inscollist RP */
88735 case 180: /* inscollist_opt ::= LP inscollist RP */
88736 {yygotominor.yy254 = yymsp[-1].minor.yy254;}
88737 break;
88738 case 149: /* using_opt ::= */
88739 case 179: /* inscollist_opt ::= */
88740 {yygotominor.yy254 = 0;}
88741 break;
88742 case 151: /* orderby_opt ::= ORDER BY sortlist */
88743 case 159: /* groupby_opt ::= GROUP BY nexprlist */
88744 case 239: /* exprlist ::= nexprlist */
88745 {yygotominor.yy148 = yymsp[0].minor.yy148;}
88746 break;
88747 case 152: /* sortlist ::= sortlist COMMA sortitem sortorder */
88748 {
88749 yygotominor.yy148 = sqlite3ExprListAppend(pParse,yymsp[-3].minor.yy148,yymsp[-1].minor.yy72);
@@ -88751,15 +88580,15 @@
88751 }
88752 break;
88753 case 153: /* sortlist ::= sortitem sortorder */
88754 {
88755 yygotominor.yy148 = sqlite3ExprListAppend(pParse,0,yymsp[-1].minor.yy72);
88756 if( yygotominor.yy148 && yygotominor.yy148->a ) yygotominor.yy148->a[0].sortOrder = (u8)yymsp[0].minor.yy194;
88757 }
88758 break;
88759 case 155: /* sortorder ::= ASC */
88760 case 157: /* sortorder ::= */
88761 {yygotominor.yy194 = SQLITE_SO_ASC;}
88762 break;
88763 case 156: /* sortorder ::= DESC */
88764 {yygotominor.yy194 = SQLITE_SO_DESC;}
88765 break;
@@ -88808,37 +88637,37 @@
88808 break;
88809 case 174: /* cmd ::= insert_cmd INTO fullname inscollist_opt DEFAULT VALUES */
88810 {sqlite3Insert(pParse, yymsp[-3].minor.yy185, 0, 0, yymsp[-2].minor.yy254, yymsp[-5].minor.yy194);}
88811 break;
88812 case 177: /* itemlist ::= itemlist COMMA expr */
88813 case 241: /* nexprlist ::= nexprlist COMMA expr */
88814 {yygotominor.yy148 = sqlite3ExprListAppend(pParse,yymsp[-2].minor.yy148,yymsp[0].minor.yy190.pExpr);}
88815 break;
88816 case 178: /* itemlist ::= expr */
88817 case 242: /* nexprlist ::= expr */
88818 {yygotominor.yy148 = sqlite3ExprListAppend(pParse,0,yymsp[0].minor.yy190.pExpr);}
88819 break;
88820 case 181: /* inscollist ::= inscollist COMMA nm */
88821 {yygotominor.yy254 = sqlite3IdListAppend(pParse->db,yymsp[-2].minor.yy254,&yymsp[0].minor.yy0);}
88822 break;
88823 case 182: /* inscollist ::= nm */
88824 {yygotominor.yy254 = sqlite3IdListAppend(pParse->db,0,&yymsp[0].minor.yy0);}
88825 break;
88826 case 183: /* expr ::= term */
88827 case 211: /* escape ::= ESCAPE expr */
88828 {yygotominor.yy190 = yymsp[0].minor.yy190;}
88829 break;
88830 case 184: /* expr ::= LP expr RP */
88831 {yygotominor.yy190.pExpr = yymsp[-1].minor.yy190.pExpr; spanSet(&yygotominor.yy190,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0);}
88832 break;
88833 case 185: /* term ::= NULL */
88834 case 190: /* term ::= INTEGER|FLOAT|BLOB */
88835 case 191: /* term ::= STRING */
88836 {spanExpr(&yygotominor.yy190, pParse, yymsp[0].major, &yymsp[0].minor.yy0);}
88837 break;
88838 case 186: /* expr ::= id */
88839 case 187: /* expr ::= JOIN_KW */
88840 {spanExpr(&yygotominor.yy190, pParse, TK_ID, &yymsp[0].minor.yy0);}
88841 break;
88842 case 188: /* expr ::= nm DOT nm */
88843 {
88844 Expr *temp1 = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[-2].minor.yy0);
@@ -88892,11 +88721,11 @@
88892 spanSet(&yygotominor.yy190,&yymsp[-5].minor.yy0,&yymsp[0].minor.yy0);
88893 }
88894 break;
88895 case 196: /* expr ::= ID LP distinct exprlist RP */
88896 {
88897 if( yymsp[-1].minor.yy148 && yymsp[-1].minor.yy148->nExpr>SQLITE_MAX_FUNCTION_ARG ){
88898 sqlite3ErrorMsg(pParse, "too many arguments on function %T", &yymsp[-4].minor.yy0);
88899 }
88900 yygotominor.yy190.pExpr = sqlite3ExprFunction(pParse, yymsp[-1].minor.yy148, &yymsp[-4].minor.yy0);
88901 spanSet(&yygotominor.yy190,&yymsp[-4].minor.yy0,&yymsp[0].minor.yy0);
88902 if( yymsp[-2].minor.yy194 && yygotominor.yy190.pExpr ){
@@ -88920,25 +88749,25 @@
88920 }
88921 spanSet(&yygotominor.yy190, &yymsp[0].minor.yy0, &yymsp[0].minor.yy0);
88922 }
88923 break;
88924 case 199: /* expr ::= expr AND expr */
88925 case 200: /* expr ::= expr OR expr */
88926 case 201: /* expr ::= expr LT|GT|GE|LE expr */
88927 case 202: /* expr ::= expr EQ|NE expr */
88928 case 203: /* expr ::= expr BITAND|BITOR|LSHIFT|RSHIFT expr */
88929 case 204: /* expr ::= expr PLUS|MINUS expr */
88930 case 205: /* expr ::= expr STAR|SLASH|REM expr */
88931 case 206: /* expr ::= expr CONCAT expr */
88932 {spanBinaryExpr(&yygotominor.yy190,pParse,yymsp[-1].major,&yymsp[-2].minor.yy190,&yymsp[0].minor.yy190);}
88933 break;
88934 case 207: /* likeop ::= LIKE_KW */
88935 case 209: /* likeop ::= MATCH */
88936 {yygotominor.yy392.eOperator = yymsp[0].minor.yy0; yygotominor.yy392.not = 0;}
88937 break;
88938 case 208: /* likeop ::= NOT LIKE_KW */
88939 case 210: /* likeop ::= NOT MATCH */
88940 {yygotominor.yy392.eOperator = yymsp[0].minor.yy0; yygotominor.yy392.not = 1;}
88941 break;
88942 case 212: /* escape ::= */
88943 {memset(&yygotominor.yy190,0,sizeof(yygotominor.yy190));}
88944 break;
@@ -88968,11 +88797,11 @@
88968 break;
88969 case 217: /* expr ::= expr IS NOT NULL */
88970 {spanUnaryPostfix(&yygotominor.yy190,pParse,TK_NOTNULL,&yymsp[-3].minor.yy190,&yymsp[0].minor.yy0);}
88971 break;
88972 case 218: /* expr ::= NOT expr */
88973 case 219: /* expr ::= BITNOT expr */
88974 {spanUnaryPrefix(&yygotominor.yy190,pParse,yymsp[-1].major,&yymsp[0].minor.yy190,&yymsp[-1].minor.yy0);}
88975 break;
88976 case 220: /* expr ::= MINUS expr */
88977 {spanUnaryPrefix(&yygotominor.yy190,pParse,TK_UMINUS,&yymsp[0].minor.yy190,&yymsp[-1].minor.yy0);}
88978 break;
@@ -89098,11 +88927,11 @@
89098 sqlite3SrcListAppend(pParse->db,0,&yymsp[-3].minor.yy0,0), yymsp[-1].minor.yy148, yymsp[-9].minor.yy194,
89099 &yymsp[-10].minor.yy0, &yymsp[0].minor.yy0, SQLITE_SO_ASC, yymsp[-7].minor.yy194);
89100 }
89101 break;
89102 case 244: /* uniqueflag ::= UNIQUE */
89103 case 293: /* raisetype ::= ABORT */
89104 {yygotominor.yy194 = OE_Abort;}
89105 break;
89106 case 245: /* uniqueflag ::= */
89107 {yygotominor.yy194 = OE_None;}
89108 break;
@@ -89137,11 +88966,11 @@
89137 break;
89138 case 252: /* cmd ::= DROP INDEX ifexists fullname */
89139 {sqlite3DropIndex(pParse, yymsp[0].minor.yy185, yymsp[-1].minor.yy194);}
89140 break;
89141 case 253: /* cmd ::= VACUUM */
89142 case 254: /* cmd ::= VACUUM nm */
89143 {sqlite3Vacuum(pParse);}
89144 break;
89145 case 255: /* cmd ::= PRAGMA nm dbnm */
89146 {sqlite3Pragma(pParse,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0,0,0);}
89147 break;
@@ -89170,32 +88999,32 @@
89170 sqlite3BeginTrigger(pParse, &yymsp[-7].minor.yy0, &yymsp[-6].minor.yy0, yymsp[-5].minor.yy194, yymsp[-4].minor.yy332.a, yymsp[-4].minor.yy332.b, yymsp[-2].minor.yy185, yymsp[0].minor.yy72, yymsp[-10].minor.yy194, yymsp[-8].minor.yy194);
89171 yygotominor.yy0 = (yymsp[-6].minor.yy0.n==0?yymsp[-7].minor.yy0:yymsp[-6].minor.yy0);
89172 }
89173 break;
89174 case 272: /* trigger_time ::= BEFORE */
89175 case 275: /* trigger_time ::= */
89176 { yygotominor.yy194 = TK_BEFORE; }
89177 break;
89178 case 273: /* trigger_time ::= AFTER */
89179 { yygotominor.yy194 = TK_AFTER; }
89180 break;
89181 case 274: /* trigger_time ::= INSTEAD OF */
89182 { yygotominor.yy194 = TK_INSTEAD;}
89183 break;
89184 case 276: /* trigger_event ::= DELETE|INSERT */
89185 case 277: /* trigger_event ::= UPDATE */
89186 {yygotominor.yy332.a = yymsp[0].major; yygotominor.yy332.b = 0;}
89187 break;
89188 case 278: /* trigger_event ::= UPDATE OF inscollist */
89189 {yygotominor.yy332.a = TK_UPDATE; yygotominor.yy332.b = yymsp[0].minor.yy254;}
89190 break;
89191 case 281: /* when_clause ::= */
89192 case 298: /* key_opt ::= */
89193 { yygotominor.yy72 = 0; }
89194 break;
89195 case 282: /* when_clause ::= WHEN expr */
89196 case 299: /* key_opt ::= KEY expr */
89197 { yygotominor.yy72 = yymsp[0].minor.yy190.pExpr; }
89198 break;
89199 case 283: /* trigger_cmd_list ::= trigger_cmd_list trigger_cmd SEMI */
89200 {
89201 assert( yymsp[-2].minor.yy145!=0 );
@@ -89308,14 +89137,55 @@
89308 break;
89309 case 316: /* vtabarg ::= */
89310 {sqlite3VtabArgInit(pParse);}
89311 break;
89312 case 318: /* vtabargtoken ::= ANY */
89313 case 319: /* vtabargtoken ::= lp anylist RP */
89314 case 320: /* lp ::= LP */
89315 {sqlite3VtabArgExtend(pParse,&yymsp[0].minor.yy0);}
89316 break;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89317 };
89318 yygoto = yyRuleInfo[yyruleno].lhs;
89319 yysize = yyRuleInfo[yyruleno].nrhs;
89320 yypParser->yyidx -= yysize;
89321 yyact = yy_find_reduce_action(yymsp[-yysize].stateno,(YYCODETYPE)yygoto);
@@ -89343,10 +89213,11 @@
89343 }
89344
89345 /*
89346 ** The following code executes when the parse fails
89347 */
 
89348 static void yy_parse_failed(
89349 yyParser *yypParser /* The parser */
89350 ){
89351 sqlite3ParserARG_FETCH;
89352 #ifndef NDEBUG
@@ -89357,10 +89228,11 @@
89357 while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser);
89358 /* Here code is inserted which will be executed whenever the
89359 ** parser fails */
89360 sqlite3ParserARG_STORE; /* Suppress warning about unused %extra_argument variable */
89361 }
 
89362
89363 /*
89364 ** The following code executes when a syntax error first occurs.
89365 */
89366 static void yy_syntax_error(
@@ -89527,10 +89399,22 @@
89527 yy_shift(yypParser,yyact,YYERRORSYMBOL,&u2);
89528 }
89529 }
89530 yypParser->yyerrcnt = 3;
89531 yyerrorhit = 1;
 
 
 
 
 
 
 
 
 
 
 
 
89532 #else /* YYERRORSYMBOL is not defined */
89533 /* This is what we do if the grammar does not define ERROR:
89534 **
89535 ** * Report an error message, and throw away the input token.
89536 **
@@ -89571,11 +89455,11 @@
89571 **
89572 ** This file contains C code that splits an SQL input string up into
89573 ** individual tokens and sends those tokens one-by-one over to the
89574 ** parser for analysis.
89575 **
89576 ** $Id: tokenize.c,v 1.158 2009/05/28 01:00:55 drh Exp $
89577 */
89578
89579 /*
89580 ** The charMap() macro maps alphabetic characters into their
89581 ** lower-case ASCII equivalent. On ASCII machines, this is just
@@ -89624,11 +89508,11 @@
89624 /************** Begin file keywordhash.h *************************************/
89625 /***** This file contains automatically generated code ******
89626 **
89627 ** The code in this file has been automatically generated by
89628 **
89629 ** $Header: /sqlite/sqlite/tool/mkkeywordhash.c,v 1.37 2009/02/01 00:00:46 drh Exp $
89630 **
89631 ** The code in this file implements a function that determines whether
89632 ** or not a given identifier is really an SQL keyword. The same thing
89633 ** might be implemented more directly using a hand-written hash table.
89634 ** But by using this automatically generated code, the size of the code
@@ -89759,129 +89643,129 @@
89759 h = ((charMap(z[0])*4) ^
89760 (charMap(z[n-1])*3) ^
89761 n) % 127;
89762 for(i=((int)aHash[h])-1; i>=0; i=((int)aNext[i])-1){
89763 if( aLen[i]==n && sqlite3StrNICmp(&zText[aOffset[i]],z,n)==0 ){
89764 testcase( i==0 ); /* TK_REINDEX */
89765 testcase( i==1 ); /* TK_INDEXED */
89766 testcase( i==2 ); /* TK_INDEX */
89767 testcase( i==3 ); /* TK_DESC */
89768 testcase( i==4 ); /* TK_ESCAPE */
89769 testcase( i==5 ); /* TK_EACH */
89770 testcase( i==6 ); /* TK_CHECK */
89771 testcase( i==7 ); /* TK_KEY */
89772 testcase( i==8 ); /* TK_BEFORE */
89773 testcase( i==9 ); /* TK_FOREIGN */
89774 testcase( i==10 ); /* TK_FOR */
89775 testcase( i==11 ); /* TK_IGNORE */
89776 testcase( i==12 ); /* TK_LIKE_KW */
89777 testcase( i==13 ); /* TK_EXPLAIN */
89778 testcase( i==14 ); /* TK_INSTEAD */
89779 testcase( i==15 ); /* TK_ADD */
89780 testcase( i==16 ); /* TK_DATABASE */
89781 testcase( i==17 ); /* TK_AS */
89782 testcase( i==18 ); /* TK_SELECT */
89783 testcase( i==19 ); /* TK_TABLE */
89784 testcase( i==20 ); /* TK_JOIN_KW */
89785 testcase( i==21 ); /* TK_THEN */
89786 testcase( i==22 ); /* TK_END */
89787 testcase( i==23 ); /* TK_DEFERRABLE */
89788 testcase( i==24 ); /* TK_ELSE */
89789 testcase( i==25 ); /* TK_EXCEPT */
89790 testcase( i==26 ); /* TK_TRANSACTION */
89791 testcase( i==27 ); /* TK_ON */
89792 testcase( i==28 ); /* TK_JOIN_KW */
89793 testcase( i==29 ); /* TK_ALTER */
89794 testcase( i==30 ); /* TK_RAISE */
89795 testcase( i==31 ); /* TK_EXCLUSIVE */
89796 testcase( i==32 ); /* TK_EXISTS */
89797 testcase( i==33 ); /* TK_SAVEPOINT */
89798 testcase( i==34 ); /* TK_INTERSECT */
89799 testcase( i==35 ); /* TK_TRIGGER */
89800 testcase( i==36 ); /* TK_REFERENCES */
89801 testcase( i==37 ); /* TK_CONSTRAINT */
89802 testcase( i==38 ); /* TK_INTO */
89803 testcase( i==39 ); /* TK_OFFSET */
89804 testcase( i==40 ); /* TK_OF */
89805 testcase( i==41 ); /* TK_SET */
89806 testcase( i==42 ); /* TK_TEMP */
89807 testcase( i==43 ); /* TK_TEMP */
89808 testcase( i==44 ); /* TK_OR */
89809 testcase( i==45 ); /* TK_UNIQUE */
89810 testcase( i==46 ); /* TK_QUERY */
89811 testcase( i==47 ); /* TK_ATTACH */
89812 testcase( i==48 ); /* TK_HAVING */
89813 testcase( i==49 ); /* TK_GROUP */
89814 testcase( i==50 ); /* TK_UPDATE */
89815 testcase( i==51 ); /* TK_BEGIN */
89816 testcase( i==52 ); /* TK_JOIN_KW */
89817 testcase( i==53 ); /* TK_RELEASE */
89818 testcase( i==54 ); /* TK_BETWEEN */
89819 testcase( i==55 ); /* TK_NOTNULL */
89820 testcase( i==56 ); /* TK_NOT */
89821 testcase( i==57 ); /* TK_NULL */
89822 testcase( i==58 ); /* TK_LIKE_KW */
89823 testcase( i==59 ); /* TK_CASCADE */
89824 testcase( i==60 ); /* TK_ASC */
89825 testcase( i==61 ); /* TK_DELETE */
89826 testcase( i==62 ); /* TK_CASE */
89827 testcase( i==63 ); /* TK_COLLATE */
89828 testcase( i==64 ); /* TK_CREATE */
89829 testcase( i==65 ); /* TK_CTIME_KW */
89830 testcase( i==66 ); /* TK_DETACH */
89831 testcase( i==67 ); /* TK_IMMEDIATE */
89832 testcase( i==68 ); /* TK_JOIN */
89833 testcase( i==69 ); /* TK_INSERT */
89834 testcase( i==70 ); /* TK_MATCH */
89835 testcase( i==71 ); /* TK_PLAN */
89836 testcase( i==72 ); /* TK_ANALYZE */
89837 testcase( i==73 ); /* TK_PRAGMA */
89838 testcase( i==74 ); /* TK_ABORT */
89839 testcase( i==75 ); /* TK_VALUES */
89840 testcase( i==76 ); /* TK_VIRTUAL */
89841 testcase( i==77 ); /* TK_LIMIT */
89842 testcase( i==78 ); /* TK_WHEN */
89843 testcase( i==79 ); /* TK_WHERE */
89844 testcase( i==80 ); /* TK_RENAME */
89845 testcase( i==81 ); /* TK_AFTER */
89846 testcase( i==82 ); /* TK_REPLACE */
89847 testcase( i==83 ); /* TK_AND */
89848 testcase( i==84 ); /* TK_DEFAULT */
89849 testcase( i==85 ); /* TK_AUTOINCR */
89850 testcase( i==86 ); /* TK_TO */
89851 testcase( i==87 ); /* TK_IN */
89852 testcase( i==88 ); /* TK_CAST */
89853 testcase( i==89 ); /* TK_COLUMNKW */
89854 testcase( i==90 ); /* TK_COMMIT */
89855 testcase( i==91 ); /* TK_CONFLICT */
89856 testcase( i==92 ); /* TK_JOIN_KW */
89857 testcase( i==93 ); /* TK_CTIME_KW */
89858 testcase( i==94 ); /* TK_CTIME_KW */
89859 testcase( i==95 ); /* TK_PRIMARY */
89860 testcase( i==96 ); /* TK_DEFERRED */
89861 testcase( i==97 ); /* TK_DISTINCT */
89862 testcase( i==98 ); /* TK_IS */
89863 testcase( i==99 ); /* TK_DROP */
89864 testcase( i==100 ); /* TK_FAIL */
89865 testcase( i==101 ); /* TK_FROM */
89866 testcase( i==102 ); /* TK_JOIN_KW */
89867 testcase( i==103 ); /* TK_LIKE_KW */
89868 testcase( i==104 ); /* TK_BY */
89869 testcase( i==105 ); /* TK_IF */
89870 testcase( i==106 ); /* TK_ISNULL */
89871 testcase( i==107 ); /* TK_ORDER */
89872 testcase( i==108 ); /* TK_RESTRICT */
89873 testcase( i==109 ); /* TK_JOIN_KW */
89874 testcase( i==110 ); /* TK_JOIN_KW */
89875 testcase( i==111 ); /* TK_ROLLBACK */
89876 testcase( i==112 ); /* TK_ROW */
89877 testcase( i==113 ); /* TK_UNION */
89878 testcase( i==114 ); /* TK_USING */
89879 testcase( i==115 ); /* TK_VACUUM */
89880 testcase( i==116 ); /* TK_VIEW */
89881 testcase( i==117 ); /* TK_INITIALLY */
89882 testcase( i==118 ); /* TK_ALL */
89883 return aCode[i];
89884 }
89885 }
89886 return TK_ID;
89887 }
@@ -89947,10 +89831,15 @@
89947 */
89948 SQLITE_PRIVATE int sqlite3GetToken(const unsigned char *z, int *tokenType){
89949 int i, c;
89950 switch( *z ){
89951 case ' ': case '\t': case '\n': case '\f': case '\r': {
 
 
 
 
 
89952 for(i=1; sqlite3Isspace(z[i]); i++){}
89953 *tokenType = TK_SPACE;
89954 return i;
89955 }
89956 case '-': {
@@ -90059,10 +89948,13 @@
90059 }
90060 case '`':
90061 case '\'':
90062 case '"': {
90063 int delim = z[0];
 
 
 
90064 for(i=1; (c=z[i])!=0; i++){
90065 if( c==delim ){
90066 if( z[i+1]==delim ){
90067 i++;
90068 }else{
@@ -90092,10 +89984,14 @@
90092 /* If the next character is a digit, this is a floating point
90093 ** number that begins with ".". Fall thru into the next case */
90094 }
90095 case '0': case '1': case '2': case '3': case '4':
90096 case '5': case '6': case '7': case '8': case '9': {
 
 
 
 
90097 *tokenType = TK_INTEGER;
90098 for(i=0; sqlite3Isdigit(z[i]); i++){}
90099 #ifndef SQLITE_OMIT_FLOATING_POINT
90100 if( z[i]=='.' ){
90101 i++;
@@ -90143,10 +90039,11 @@
90143 case '$':
90144 #endif
90145 case '@': /* For compatibility with MS SQL Server */
90146 case ':': {
90147 int n = 0;
 
90148 *tokenType = TK_VARIABLE;
90149 for(i=1; (c=z[i])!=0; i++){
90150 if( IdChar(c) ){
90151 n++;
90152 #ifndef SQLITE_OMIT_TCL_VARIABLE
@@ -90170,10 +90067,11 @@
90170 if( n==0 ) *tokenType = TK_ILLEGAL;
90171 return i;
90172 }
90173 #ifndef SQLITE_OMIT_BLOB_LITERAL
90174 case 'x': case 'X': {
 
90175 if( z[1]=='\'' ){
90176 *tokenType = TK_BLOB;
90177 for(i=2; (c=z[i])!=0 && c!='\''; i++){
90178 if( !sqlite3Isxdigit(c) ){
90179 *tokenType = TK_ILLEGAL;
@@ -90248,12 +90146,12 @@
90248 break;
90249 }
90250 switch( tokenType ){
90251 case TK_SPACE: {
90252 if( db->u1.isInterrupted ){
 
90253 pParse->rc = SQLITE_INTERRUPT;
90254 sqlite3SetString(pzErrMsg, db, "interrupt");
90255 goto abort_parse;
90256 }
90257 break;
90258 }
90259 case TK_ILLEGAL: {
@@ -90296,16 +90194,13 @@
90296 pParse->rc = SQLITE_NOMEM;
90297 }
90298 if( pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE && pParse->zErrMsg==0 ){
90299 sqlite3SetString(&pParse->zErrMsg, db, "%s", sqlite3ErrStr(pParse->rc));
90300 }
 
90301 if( pParse->zErrMsg ){
90302 if( *pzErrMsg==0 ){
90303 *pzErrMsg = pParse->zErrMsg;
90304 }else{
90305 sqlite3DbFree(db, pParse->zErrMsg);
90306 }
90307 pParse->zErrMsg = 0;
90308 nErr++;
90309 }
90310 if( pParse->pVdbe && pParse->nErr>0 && pParse->nested==0 ){
90311 sqlite3VdbeDelete(pParse->pVdbe);
@@ -90336,11 +90231,11 @@
90336 while( pParse->pZombieTab ){
90337 Table *p = pParse->pZombieTab;
90338 pParse->pZombieTab = p->pNextZombie;
90339 sqlite3DeleteTable(p);
90340 }
90341 if( nErr>0 && (pParse->rc==SQLITE_OK || pParse->rc==SQLITE_DONE) ){
90342 pParse->rc = SQLITE_ERROR;
90343 }
90344 return nErr;
90345 }
90346
@@ -90639,11 +90534,11 @@
90639 ** Main file for the SQLite library. The routines in this file
90640 ** implement the programmer interface to the library. Routines in
90641 ** other files are for internal use by SQLite and should not be
90642 ** accessed by users of the library.
90643 **
90644 ** $Id: main.c,v 1.555 2009/06/01 16:53:10 shane Exp $
90645 */
90646
90647 #ifdef SQLITE_ENABLE_FTS3
90648 /************** Include fts3.h in the middle of main.c ***********************/
90649 /************** Begin file fts3.h ********************************************/
@@ -91061,11 +90956,10 @@
91061 memset(&sqlite3GlobalConfig.m, 0, sizeof(sqlite3GlobalConfig.m));
91062 }else{
91063 /* The heap pointer is not NULL, then install one of the
91064 ** mem5.c/mem3.c methods. If neither ENABLE_MEMSYS3 nor
91065 ** ENABLE_MEMSYS5 is defined, return an error.
91066 ** the default case and return an error.
91067 */
91068 #ifdef SQLITE_ENABLE_MEMSYS3
91069 sqlite3GlobalConfig.m = *sqlite3MemGetMemsys3();
91070 #endif
91071 #ifdef SQLITE_ENABLE_MEMSYS5
@@ -91296,17 +91190,10 @@
91296 if( !sqlite3SafetyCheckSickOrOk(db) ){
91297 return SQLITE_MISUSE;
91298 }
91299 sqlite3_mutex_enter(db->mutex);
91300
91301 #ifdef SQLITE_SSE
91302 {
91303 extern void sqlite3SseCleanup(sqlite3*);
91304 sqlite3SseCleanup(db);
91305 }
91306 #endif
91307
91308 sqlite3ResetInternalSchema(db, 0);
91309
91310 /* If a transaction is open, the ResetInternalSchema() call above
91311 ** will not have called the xDisconnect() method on any virtual
91312 ** tables in the db->aVTrans[] array. The following sqlite3VtabRollback()
91313
--- 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.
@@ -9,17 +9,17 @@
9 **
10 ** This file is all you need to compile SQLite. To use SQLite in other
11 ** programs, you need this file and the "sqlite3.h" header file that defines
12 ** the programming interface to the SQLite library. (If you do not have
13 ** the "sqlite3.h" header file at hand, you will find a copy in the first
14 ** 5626 lines past this header comment.) Additional code files may be
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 /*
@@ -545,11 +545,11 @@
545 ** The name of this file under configuration management is "sqlite.h.in".
546 ** The makefile makes some minor changes to this file (such as inserting
547 ** the version number) and changes its name to "sqlite3.h" as
548 ** part of the build process.
549 **
550 ** @(#) $Id: sqlite.h.in,v 1.458 2009/06/19 22:50:31 drh Exp $
551 */
552 #ifndef _SQLITE3_H_
553 #define _SQLITE3_H_
554 #include <stdarg.h> /* Needed for the definition of va_list */
555
@@ -614,12 +614,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 **
@@ -1009,10 +1009,16 @@
1009 ** [sqlite3_file] object (or, more commonly, a subclass of the
1010 ** [sqlite3_file] object) with a pointer to an instance of this object.
1011 ** This object defines the methods used to perform various operations
1012 ** against the open file represented by the [sqlite3_file] object.
1013 **
1014 ** If the xOpen method sets the sqlite3_file.pMethods element
1015 ** to a non-NULL pointer, then the sqlite3_io_methods.xClose method
1016 ** may be invoked even if the xOpen reported that it failed. The
1017 ** only way to prevent a call to xClose following a failed xOpen
1018 ** is for the xOpen to set the sqlite3_file.pMethods element to NULL.
1019 **
1020 ** The flags argument to xSync may be one of [SQLITE_SYNC_NORMAL] or
1021 ** [SQLITE_SYNC_FULL]. The first choice is the normal fsync().
1022 ** The second choice is a Mac OS X style fullsync. The [SQLITE_SYNC_DATAONLY]
1023 ** flag may be ORed in to indicate that only the data of the file
1024 ** and not its inode needs to be synced.
@@ -1169,15 +1175,15 @@
1175 **
1176 ** SQLite will guarantee that the zFilename parameter to xOpen
1177 ** is either a NULL pointer or string obtained
1178 ** from xFullPathname(). SQLite further guarantees that
1179 ** the string will be valid and unchanged until xClose() is
1180 ** called. Because of the previous sentence,
1181 ** the [sqlite3_file] can safely store a pointer to the
1182 ** filename if it needs to remember the filename for some reason.
1183 ** If the zFilename parameter is xOpen is a NULL pointer then xOpen
1184 ** must invent its own temporary name for the file. Whenever the
1185 ** xFilename parameter is NULL it will also be the case that the
1186 ** flags parameter will include [SQLITE_OPEN_DELETEONCLOSE].
1187 **
1188 ** The flags argument to xOpen() includes all bits set in
1189 ** the flags argument to [sqlite3_open_v2()]. Or if [sqlite3_open()]
@@ -1229,11 +1235,16 @@
1235 ** for exclusive access.
1236 **
1237 ** At least szOsFile bytes of memory are allocated by SQLite
1238 ** to hold the [sqlite3_file] structure passed as the third
1239 ** argument to xOpen. The xOpen method does not have to
1240 ** allocate the structure; it should just fill it in. Note that
1241 ** the xOpen method must set the sqlite3_file.pMethods to either
1242 ** a valid [sqlite3_io_methods] object or to NULL. xOpen must do
1243 ** this even if the open fails. SQLite expects that the sqlite3_file.pMethods
1244 ** element will be valid after xOpen returns regardless of the success
1245 ** or failure of the xOpen call.
1246 **
1247 ** The flags argument to xAccess() may be [SQLITE_ACCESS_EXISTS]
1248 ** to test for the existence of a file, or [SQLITE_ACCESS_READWRITE] to
1249 ** test whether a file is readable and writable, or [SQLITE_ACCESS_READ]
1250 ** to test whether a file is at least readable. The file can be a
@@ -1551,16 +1562,18 @@
1562 ** </ul>
1563 ** </dd>
1564 **
1565 ** <dt>SQLITE_CONFIG_SCRATCH</dt>
1566 ** <dd>This option specifies a static memory buffer that SQLite can use for
1567 ** scratch memory. There are three arguments: A pointer an 8-byte
1568 ** aligned memory buffer from which the scrach allocations will be
1569 ** drawn, the size of each scratch allocation (sz),
1570 ** and the maximum number of scratch allocations (N). The sz
1571 ** argument must be a multiple of 16. The sz parameter should be a few bytes
1572 ** larger than the actual scratch space required due to internal overhead.
1573 ** The first argument should pointer to an 8-byte aligned buffer
1574 ** of at least sz*N bytes of memory.
1575 ** SQLite will use no more than one scratch buffer at once per thread, so
1576 ** N should be set to the expected maximum number of threads. The sz
1577 ** parameter should be 6 times the size of the largest database page size.
1578 ** Scratch buffers are used as part of the btree balance operation. If
1579 ** The btree balancer needs additional memory beyond what is provided by
@@ -1570,33 +1583,41 @@
1583 ** <dt>SQLITE_CONFIG_PAGECACHE</dt>
1584 ** <dd>This option specifies a static memory buffer that SQLite can use for
1585 ** the database page cache with the default page cache implemenation.
1586 ** This configuration should not be used if an application-define page
1587 ** cache implementation is loaded using the SQLITE_CONFIG_PCACHE option.
1588 ** There are three arguments to this option: A pointer to 8-byte aligned
1589 ** memory, the size of each page buffer (sz), and the number of pages (N).
1590 ** The sz argument should be the size of the largest database page
1591 ** (a power of two between 512 and 32768) plus a little extra for each
1592 ** page header. The page header size is 20 to 40 bytes depending on
1593 ** the host architecture. It is harmless, apart from the wasted memory,
1594 ** to make sz a little too large. The first
1595 ** argument should point to an allocation of at least sz*N bytes of memory.
1596 ** SQLite will use the memory provided by the first argument to satisfy its
1597 ** memory needs for the first N pages that it adds to cache. If additional
1598 ** page cache memory is needed beyond what is provided by this option, then
1599 ** SQLite goes to [sqlite3_malloc()] for the additional storage space.
1600 ** The implementation might use one or more of the N buffers to hold
1601 ** memory accounting information. The pointer in the first argument must
1602 ** be aligned to an 8-byte boundary or subsequent behavior of SQLite
1603 ** will be undefined.</dd>
1604 **
1605 ** <dt>SQLITE_CONFIG_HEAP</dt>
1606 ** <dd>This option specifies a static memory buffer that SQLite will use
1607 ** for all of its dynamic memory allocation needs beyond those provided
1608 ** for by [SQLITE_CONFIG_SCRATCH] and [SQLITE_CONFIG_PAGECACHE].
1609 ** There are three arguments: An 8-byte aligned pointer to the memory,
1610 ** the number of bytes in the memory buffer, and the minimum allocation size.
1611 ** If the first pointer (the memory pointer) is NULL, then SQLite reverts
1612 ** to using its default memory allocator (the system malloc() implementation),
1613 ** undoing any prior invocation of [SQLITE_CONFIG_MALLOC]. If the
1614 ** memory pointer is not NULL and either [SQLITE_ENABLE_MEMSYS3] or
1615 ** [SQLITE_ENABLE_MEMSYS5] are defined, then the alternative memory
1616 ** allocator is engaged to handle all of SQLites memory allocation needs.
1617 ** The first pointer (the memory pointer) must be aligned to an 8-byte
1618 ** boundary or subsequent behavior of SQLite will be undefined.</dd>
1619 **
1620 ** <dt>SQLITE_CONFIG_MUTEX</dt>
1621 ** <dd>This option takes a single argument which is a pointer to an
1622 ** instance of the [sqlite3_mutex_methods] structure. The argument specifies
1623 ** alternative low-level mutex routines to be used in place
@@ -1663,13 +1684,13 @@
1684 ** <dl>
1685 ** <dt>SQLITE_DBCONFIG_LOOKASIDE</dt>
1686 ** <dd>This option takes three additional arguments that determine the
1687 ** [lookaside memory allocator] configuration for the [database connection].
1688 ** The first argument (the third parameter to [sqlite3_db_config()] is a
1689 ** pointer to an 8-byte aligned memory buffer to use for lookaside memory.
1690 ** The first argument may be NULL in which case SQLite will allocate the
1691 ** lookaside buffer itself using [sqlite3_malloc()]. The second argument is the
1692 ** size of each lookaside buffer slot and the third argument is the number of
1693 ** slots. The size of the buffer in the first argument must be greater than
1694 ** or equal to the product of the second and third arguments.</dd>
1695 **
1696 ** </dl>
@@ -6397,15 +6418,16 @@
6418 */
6419 #ifdef SQLITE_OMIT_FLOATING_POINT
6420 # define double sqlite_int64
6421 # define LONGDOUBLE_TYPE sqlite_int64
6422 # ifndef SQLITE_BIG_DBL
6423 # define SQLITE_BIG_DBL (((sqlite3_int64)1)<<60)
6424 # endif
6425 # define SQLITE_OMIT_DATETIME_FUNCS 1
6426 # define SQLITE_OMIT_TRACE 1
6427 # undef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
6428 # undef SQLITE_HAVE_ISNAN
6429 #endif
6430 #ifndef SQLITE_BIG_DBL
6431 # define SQLITE_BIG_DBL (1e99)
6432 #endif
6433
@@ -7375,11 +7397,11 @@
7397 *************************************************************************
7398 ** This header file defines the interface that the sqlite page cache
7399 ** subsystem. The page cache subsystem reads and writes a file a page
7400 ** at a time and provides a journal for rollback.
7401 **
7402 ** @(#) $Id: pager.h,v 1.102 2009/06/18 17:22:39 drh Exp $
7403 */
7404
7405 #ifndef _PAGER_H_
7406 #define _PAGER_H_
7407
@@ -7455,11 +7477,11 @@
7477 SQLITE_PRIVATE int sqlite3PagerReadFileheader(Pager*, int, unsigned char*);
7478
7479 /* Functions used to configure a Pager object. */
7480 SQLITE_PRIVATE void sqlite3PagerSetBusyhandler(Pager*, int(*)(void *), void *);
7481 SQLITE_PRIVATE void sqlite3PagerSetReiniter(Pager*, void(*)(DbPage*));
7482 SQLITE_PRIVATE int sqlite3PagerSetPagesize(Pager*, u16*, int);
7483 SQLITE_PRIVATE int sqlite3PagerMaxPageCount(Pager*, int);
7484 SQLITE_PRIVATE void sqlite3PagerSetCachesize(Pager*, int);
7485 SQLITE_PRIVATE void sqlite3PagerSetSafetyLevel(Pager*,int,int);
7486 SQLITE_PRIVATE int sqlite3PagerLockingMode(Pager *, int);
7487 SQLITE_PRIVATE int sqlite3PagerJournalMode(Pager *, int);
@@ -7503,15 +7525,10 @@
7525 SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager*);
7526
7527 /* Functions used to truncate the database file. */
7528 SQLITE_PRIVATE void sqlite3PagerTruncateImage(Pager*,Pgno);
7529
 
 
 
 
 
7530 /* Functions to support testing and debugging. */
7531 #if !defined(NDEBUG) || defined(SQLITE_TEST)
7532 SQLITE_PRIVATE Pgno sqlite3PagerPagenumber(DbPage*);
7533 SQLITE_PRIVATE int sqlite3PagerIswriteable(DbPage*);
7534 #endif
@@ -8259,13 +8276,10 @@
8276 FuncDefHash aFunc; /* Hash table of connection functions */
8277 Hash aCollSeq; /* All collating sequences */
8278 BusyHandler busyHandler; /* Busy callback */
8279 int busyTimeout; /* Busy handler timeout, in msec */
8280 Db aDbStatic[2]; /* Static space for the 2 default backends */
 
 
 
8281 Savepoint *pSavepoint; /* List of active savepoints */
8282 int nSavepoint; /* Number of non-transaction savepoints */
8283 int nStatement; /* Number of nested statement-transactions */
8284 u8 isTransactionSavepoint; /* True if the outermost savepoint is a TS */
8285
@@ -10301,15 +10315,10 @@
10315 #define sqlite3ConnectionBlocked(x,y)
10316 #define sqlite3ConnectionUnlocked(x)
10317 #define sqlite3ConnectionClosed(x)
10318 #endif
10319
 
 
 
 
 
10320 #ifdef SQLITE_DEBUG
10321 SQLITE_PRIVATE void sqlite3ParserTrace(FILE*, char *);
10322 #endif
10323
10324 /*
@@ -17073,22 +17082,14 @@
17082 ** VDBE. This information used to all be at the top of the single
17083 ** source code file "vdbe.c". When that file became too big (over
17084 ** 6000 lines long) it was split up into several smaller files and
17085 ** this header information was factored out.
17086 **
17087 ** $Id: vdbeInt.h,v 1.174 2009/06/23 14:15:04 drh Exp $
17088 */
17089 #ifndef _VDBEINT_H_
17090 #define _VDBEINT_H_
 
 
 
 
 
 
 
 
17091
17092 /*
17093 ** SQL is translated into a sequence of instructions to be
17094 ** executed by a virtual machine. Each instruction is an instance
17095 ** of the following structure.
@@ -17320,66 +17321,57 @@
17321 ** "DROP TABLE" statements and to prevent some nasty side effects of
17322 ** malloc failure when SQLite is invoked recursively by a virtual table
17323 ** method function.
17324 */
17325 struct Vdbe {
17326 sqlite3 *db; /* The database connection that owns this statement */
17327 Vdbe *pPrev,*pNext; /* Linked list of VDBEs with the same Vdbe.db */
17328 int nOp; /* Number of instructions in the program */
17329 int nOpAlloc; /* Number of slots allocated for aOp[] */
17330 Op *aOp; /* Space to hold the virtual machine's program */
17331 int nLabel; /* Number of labels used */
17332 int nLabelAlloc; /* Number of slots allocated in aLabel[] */
17333 int *aLabel; /* Space to hold the labels */
17334 Mem **apArg; /* Arguments to currently executing user function */
17335 Mem *aColName; /* Column names to return */
17336 Mem *pResultSet; /* Pointer to an array of results */
17337 u16 nResColumn; /* Number of columns in one row of the result set */
17338 u16 nCursor; /* Number of slots in apCsr[] */
17339 VdbeCursor **apCsr; /* One element of this array for each open cursor */
17340 u8 errorAction; /* Recovery action to do in case of an error */
17341 u8 okVar; /* True if azVar[] has been initialized */
17342 u16 nVar; /* Number of entries in aVar[] */
17343 Mem *aVar; /* Values for the OP_Variable opcode. */
17344 char **azVar; /* Name of variables */
17345 u32 magic; /* Magic number for sanity checking */
17346 int nMem; /* Number of memory locations currently allocated */
17347 Mem *aMem; /* The memory locations */
17348 int cacheCtr; /* VdbeCursor row cache generation counter */
17349 int contextStackTop; /* Index of top element in the context stack */
17350 int contextStackDepth; /* The size of the "context" stack */
17351 Context *contextStack; /* Stack used by opcodes ContextPush & ContextPop*/
17352 int pc; /* The program counter */
17353 int rc; /* Value to return */
 
 
 
17354 char *zErrMsg; /* Error message written here */
 
17355 u8 explain; /* True if EXPLAIN present on SQL command */
17356 u8 changeCntOn; /* True to update the change-counter */
17357 u8 expired; /* True if the VM needs to be recompiled */
17358 u8 minWriteFileFormat; /* Minimum file format for writable database files */
17359 u8 inVtabMethod; /* See comments above */
17360 u8 usesStmtJournal; /* True if uses a statement journal */
17361 u8 readOnly; /* True for read-only statements */
17362 u8 isPrepareV2; /* True if prepared with prepare_v2() */
17363 int nChange; /* Number of db changes made since last reset */
 
17364 int btreeMask; /* Bitmask of db->aDb[] entries referenced */
17365 i64 startTime; /* Time when query started - used for profiling */
17366 BtreeMutexArray aMutex; /* An array of Btree used here and needing locks */
17367 int aCounter[2]; /* Counters used by sqlite3_stmt_status() */
17368 char *zSql; /* Text of the SQL statement that generated this */
17369 void *pFree; /* Free this when deleting the vdbe */
 
 
 
17370 int iStatement; /* Statement number (or 0 if has not opened stmt) */
17371 #ifdef SQLITE_DEBUG
17372 FILE *trace; /* Write an execution trace here, if not NULL */
 
 
 
 
 
17373 #endif
17374 };
17375
17376 /*
17377 ** The following are allowed values for Vdbe.magic
@@ -17404,11 +17396,11 @@
17396 SQLITE_PRIVATE u32 sqlite3VdbeSerialGet(const unsigned char*, u32, Mem*);
17397 SQLITE_PRIVATE void sqlite3VdbeDeleteAuxData(VdbeFunc*, int);
17398
17399 int sqlite2BtreeKeyCompare(BtCursor *, const void *, int, int, int *);
17400 SQLITE_PRIVATE int sqlite3VdbeIdxKeyCompare(VdbeCursor*,UnpackedRecord*,int*);
17401 SQLITE_PRIVATE int sqlite3VdbeIdxRowid(sqlite3*, BtCursor *, i64 *);
17402 SQLITE_PRIVATE int sqlite3MemCompare(const Mem*, const Mem*, const CollSeq*);
17403 SQLITE_PRIVATE int sqlite3VdbeExec(Vdbe*);
17404 SQLITE_PRIVATE int sqlite3VdbeList(Vdbe*);
17405 SQLITE_PRIVATE int sqlite3VdbeHalt(Vdbe*);
17406 SQLITE_PRIVATE int sqlite3VdbeChangeEncoding(Mem *, int);
@@ -17985,11 +17977,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
@@ -18077,11 +18069,11 @@
18069 ** than the actual length of the string. For very long strings (greater
18070 ** than 1GiB) the value returned might be less than the true string length.
18071 */
18072 SQLITE_PRIVATE int sqlite3Strlen30(const char *z){
18073 const char *z2 = z;
18074 if( z==0 ) return 0;
18075 while( *z2 ){ z2++; }
18076 return 0x3fffffff & (int)(z2 - z);
18077 }
18078
18079 /*
@@ -18361,11 +18353,11 @@
18353 **
18354 ** will return -8.
18355 */
18356 static int compare2pow63(const char *zNum){
18357 int c;
18358 c = memcmp(zNum,"922337203685477580",18)*10;
18359 if( c==0 ){
18360 c = zNum[18] - '8';
18361 }
18362 return c;
18363 }
@@ -20864,11 +20856,11 @@
20856 ** * sqlite3_vfs method implementations.
20857 ** * Locking primitives for the proxy uber-locking-method. (MacOSX only)
20858 ** * Definitions of sqlite3_vfs objects for all locking methods
20859 ** plus implementations of sqlite3_os_init() and sqlite3_os_end().
20860 **
20861 ** $Id: os_unix.c,v 1.253 2009/06/17 13:09:39 drh Exp $
20862 */
20863 #if SQLITE_OS_UNIX /* This file is used on unix only */
20864
20865 /*
20866 ** There are various methods for file locking used for concurrency
@@ -21952,11 +21944,11 @@
21944 int rc; /* System call return code */
21945 int fd; /* The file descriptor for pFile */
21946 struct unixLockKey lockKey; /* Lookup key for the unixLockInfo structure */
21947 struct unixFileId fileId; /* Lookup key for the unixOpenCnt struct */
21948 struct stat statbuf; /* Low-level file information */
21949 struct unixLockInfo *pLock = 0;/* Candidate unixLockInfo object */
21950 struct unixOpenCnt *pOpen; /* Candidate unixOpenCnt object */
21951
21952 /* Get low-level information about the file that we can used to
21953 ** create a unique name for the file.
21954 */
@@ -22856,11 +22848,12 @@
22848 }
22849
22850 /* To fully unlock the database, delete the lock file */
22851 assert( locktype==NO_LOCK );
22852 if( unlink(zLockFile) ){
22853 int rc = 0;
22854 int tErrno = errno;
22855 if( ENOENT != tErrno ){
22856 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
22857 }
22858 if( IS_LOCK_ERROR(rc) ){
22859 pFile->lastErrno = tErrno;
@@ -25106,11 +25099,15 @@
25099 ** Find the current time (in Universal Coordinated Time). Write the
25100 ** current time and date as a Julian Day number into *prNow and
25101 ** return 0. Return 1 if the time and date cannot be found.
25102 */
25103 static int unixCurrentTime(sqlite3_vfs *NotUsed, double *prNow){
25104 #if defined(SQLITE_OMIT_FLOATING_POINT)
25105 time_t t;
25106 time(&t);
25107 *prNow = (((sqlite3_int64)t)/8640 + 24405875)/10;
25108 #elif defined(NO_GETTOD)
25109 time_t t;
25110 time(&t);
25111 *prNow = t/86400.0 + 2440587.5;
25112 #elif OS_VXWORKS
25113 struct timespec sNow;
@@ -29299,11 +29296,11 @@
29296 ** sqlite3_pcache interface). It also contains part of the implementation
29297 ** of the SQLITE_CONFIG_PAGECACHE and sqlite3_release_memory() features.
29298 ** If the default page cache implementation is overriden, then neither of
29299 ** these two features are available.
29300 **
29301 ** @(#) $Id: pcache1.c,v 1.17 2009/06/09 18:58:53 shane Exp $
29302 */
29303
29304
29305 typedef struct PCache1 PCache1;
29306 typedef struct PgHdr1 PgHdr1;
@@ -29650,11 +29647,11 @@
29647 */
29648 static void pcache1TruncateUnsafe(
29649 PCache1 *pCache,
29650 unsigned int iLimit
29651 ){
29652 TESTONLY( unsigned int nPage = 0; ) /* Used to assert pCache->nPage is correct */
29653 unsigned int h;
29654 assert( sqlite3_mutex_held(pcache1.mutex) );
29655 for(h=0; h<pCache->nHash; h++){
29656 PgHdr1 **pp = &pCache->apHash[h];
29657 PgHdr1 *pPage;
@@ -30498,11 +30495,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
@@ -30582,15 +30579,18 @@
30579
30580 /*
30581 ** A macro used for invoking the codec if there is one
30582 */
30583 #ifdef SQLITE_HAS_CODEC
30584 # define CODEC1(P,D,N,X,E) \
30585 if( P->xCodec && P->xCodec(P->pCodec,D,N,X)==0 ){ E; }
30586 # define CODEC2(P,D,N,X,E,O) \
30587 if( P->xCodec==0 ){ O=(char*)D; }else \
30588 if( (O=(char*)(P->xCodec(P->pCodec,D,N,X)))==0 ){ E; }
30589 #else
30590 # define CODEC1(P,D,N,X,E) /* NO-OP */
30591 # define CODEC2(P,D,N,X,E,O) O=(char*)D
30592 #endif
30593
30594 /*
30595 ** The maximum allowed sector size. 16MB. If the xSectorsize() method
30596 ** returns a value larger than this, then MAX_SECTOR_SIZE is used instead.
@@ -30766,11 +30766,12 @@
30766 PagerSavepoint *aSavepoint; /* Array of active savepoints */
30767 int nSavepoint; /* Number of elements in aSavepoint[] */
30768 char dbFileVers[16]; /* Changes whenever database file changes */
30769 u32 sectorSize; /* Assumed sector size during rollback */
30770
30771 u16 nExtra; /* Add this many bytes to each in-memory page */
30772 i16 nReserve; /* Number of unused bytes at end of each page */
30773 u32 vfsFlags; /* Flags for sqlite3_vfs.xOpen() */
30774 int pageSize; /* Number of bytes in a page */
30775 Pgno mxPgno; /* Maximum allowed size of the database */
30776 char *zFilename; /* Name of the database file */
30777 char *zJournal; /* Name of the journal file */
@@ -30781,11 +30782,13 @@
30782 int nRead, nWrite; /* Database pages read/written */
30783 #endif
30784 void (*xReiniter)(DbPage*); /* Call this routine when reloading pages */
30785 #ifdef SQLITE_HAS_CODEC
30786 void *(*xCodec)(void*,void*,Pgno,int); /* Routine for en/decoding data */
30787 void (*xCodecSizeChng)(void*,int,int); /* Notify of page size changes */
30788 void (*xCodecFree)(void*); /* Destructor for the codec */
30789 void *pCodec; /* First argument to xCodec... methods */
30790 #endif
30791 char *pTmpSpace; /* Pager.pageSize bytes of space for tmp use */
30792 i64 journalSizeLimit; /* Size limit for persistent journal files */
30793 PCache *pPCache; /* Pointer to page cache object */
30794 sqlite3_backup *pBackup; /* Pointer to list of ongoing backup processes */
@@ -31403,11 +31406,11 @@
31406 /* Update the page-size to match the value read from the journal.
31407 ** Use a testcase() macro to make sure that malloc failure within
31408 ** PagerSetPagesize() is tested.
31409 */
31410 iPageSize16 = (u16)iPageSize;
31411 rc = sqlite3PagerSetPagesize(pPager, &iPageSize16, -1);
31412 testcase( rc!=SQLITE_OK );
31413 assert( rc!=SQLITE_OK || iPageSize16==(u16)iPageSize );
31414
31415 /* Update the assumed sector-size to match the value used by
31416 ** the process that created this journal. If this journal was
@@ -32005,11 +32008,15 @@
32008 i64 ofst = (pgno-1)*(i64)pPager->pageSize;
32009 rc = sqlite3OsWrite(pPager->fd, aData, pPager->pageSize, ofst);
32010 if( pgno>pPager->dbFileSize ){
32011 pPager->dbFileSize = pgno;
32012 }
32013 if( pPager->pBackup ){
32014 CODEC1(pPager, aData, pgno, 3, rc=SQLITE_NOMEM);
32015 sqlite3BackupUpdate(pPager->pBackup, pgno, aData);
32016 CODEC1(pPager, aData, pgno, 0, rc=SQLITE_NOMEM);
32017 }
32018 }else if( !isMainJrnl && pPg==0 ){
32019 /* If this is a rollback of a savepoint and data was not written to
32020 ** the database and the page is not in-memory, there is a potential
32021 ** problem. When the page is next fetched by the b-tree layer, it
32022 ** will be read from the database file, which may or may not be
@@ -32074,11 +32081,11 @@
32081 if( pgno==1 ){
32082 memcpy(&pPager->dbFileVers, &((u8*)pData)[24],sizeof(pPager->dbFileVers));
32083 }
32084
32085 /* Decode the page just read from disk */
32086 CODEC1(pPager, pData, pPg->pgno, 3, rc=SQLITE_NOMEM);
32087 sqlite3PcacheRelease(pPg);
32088 }
32089 return rc;
32090 }
32091
@@ -32837,10 +32844,25 @@
32844 */
32845 SQLITE_PRIVATE void sqlite3PagerSetReiniter(Pager *pPager, void (*xReinit)(DbPage*)){
32846 pPager->xReiniter = xReinit;
32847 }
32848
32849 /*
32850 ** Report the current page size and number of reserved bytes back
32851 ** to the codec.
32852 */
32853 #ifdef SQLITE_HAS_CODEC
32854 static void pagerReportSize(Pager *pPager){
32855 if( pPager->xCodecSizeChng ){
32856 pPager->xCodecSizeChng(pPager->pCodec, pPager->pageSize,
32857 (int)pPager->nReserve);
32858 }
32859 }
32860 #else
32861 # define pagerReportSize(X) /* No-op if we do not support a codec */
32862 #endif
32863
32864 /*
32865 ** Change the page size used by the Pager object. The new page size
32866 ** is passed in *pPageSize.
32867 **
32868 ** If the pager is in the error state when this function is called, it
@@ -32867,11 +32889,11 @@
32889 ** If the page size is not changed, either because one of the enumerated
32890 ** conditions above is not true, the pager was in error state when this
32891 ** function was called, or because the memory allocation attempt failed,
32892 ** then *pPageSize is set to the old, retained page size before returning.
32893 */
32894 SQLITE_PRIVATE int sqlite3PagerSetPagesize(Pager *pPager, u16 *pPageSize, int nReserve){
32895 int rc = pPager->errCode;
32896 if( rc==SQLITE_OK ){
32897 u16 pageSize = *pPageSize;
32898 assert( pageSize==0 || (pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE) );
32899 if( pageSize && pageSize!=pPager->pageSize
@@ -32888,10 +32910,14 @@
32910 pPager->pTmpSpace = pNew;
32911 sqlite3PcacheSetPageSize(pPager->pPCache, pageSize);
32912 }
32913 }
32914 *pPageSize = (u16)pPager->pageSize;
32915 if( nReserve<0 ) nReserve = pPager->nReserve;
32916 assert( nReserve>=0 && nReserve<1000 );
32917 pPager->nReserve = (i16)nReserve;
32918 pagerReportSize(pPager);
32919 }
32920 return rc;
32921 }
32922
32923 /*
@@ -33135,10 +33161,14 @@
33161 PAGERTRACE(("CLOSE %d\n", PAGERID(pPager)));
33162 IOTRACE(("CLOSE %p\n", pPager))
33163 sqlite3OsClose(pPager->fd);
33164 sqlite3PageFree(pPager->pTmpSpace);
33165 sqlite3PcacheClose(pPager->pPCache);
33166
33167 #ifdef SQLITE_HAS_CODEC
33168 if( pPager->xCodecFree ) pPager->xCodecFree(pPager->pCodec);
33169 #endif
33170
33171 assert( !pPager->aSavepoint && !pPager->pInJournal );
33172 assert( !isOpen(pPager->jfd) && !isOpen(pPager->sjfd) );
33173
33174 sqlite3_free(pPager);
@@ -33366,12 +33396,15 @@
33396 **
33397 ** Also, do not write out any page that has the PGHDR_DONT_WRITE flag
33398 ** set (set by sqlite3PagerDontWrite()).
33399 */
33400 if( pgno<=pPager->dbSize && 0==(pList->flags&PGHDR_DONT_WRITE) ){
33401 i64 offset = (pgno-1)*(i64)pPager->pageSize; /* Offset to write */
33402 char *pData; /* Data to write */
33403
33404 /* Encode the database */
33405 CODEC2(pPager, pList->pData, pgno, 6, return SQLITE_NOMEM, pData);
33406
33407 /* Write out the page data. */
33408 rc = sqlite3OsWrite(pPager->fd, pData, pPager->pageSize, offset);
33409
33410 /* If page 1 was just written, update Pager.dbFileVers to match
@@ -33384,11 +33417,11 @@
33417 if( pgno>pPager->dbFileSize ){
33418 pPager->dbFileSize = pgno;
33419 }
33420
33421 /* Update any backup objects copying the contents of this pager. */
33422 sqlite3BackupUpdate(pPager->pBackup, pgno, (u8*)pList->pData);
33423
33424 PAGERTRACE(("STORE %d page %d hash(%08x)\n",
33425 PAGERID(pPager), pgno, pager_pagehash(pList)));
33426 IOTRACE(("PGOUT %p %d\n", pPager, pgno));
33427 PAGER_INCR(sqlite3_pager_writedb_count);
@@ -33422,12 +33455,13 @@
33455 int rc = SQLITE_OK;
33456 Pager *pPager = pPg->pPager;
33457 if( isOpen(pPager->sjfd) ){
33458 void *pData = pPg->pData;
33459 i64 offset = pPager->nSubRec*(4+pPager->pageSize);
33460 char *pData2;
33461
33462 CODEC2(pPager, pData, pPg->pgno, 7, return SQLITE_NOMEM, pData2);
33463 PAGERTRACE(("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno));
33464
33465 assert( pageInJournal(pPg) || pPg->pgno>pPager->dbOrigSize );
33466 rc = write32bits(pPager->sjfd, offset, pPg->pgno);
33467 if( rc==SQLITE_OK ){
@@ -33745,18 +33779,19 @@
33779 ** database is the same as a temp-file that is never written out to
33780 ** disk and uses an in-memory rollback journal.
33781 */
33782 tempFile = 1;
33783 pPager->state = PAGER_EXCLUSIVE;
33784 readOnly = (vfsFlags&SQLITE_OPEN_READONLY);
33785 }
33786
33787 /* The following call to PagerSetPagesize() serves to set the value of
33788 ** Pager.pageSize and to allocate the Pager.pTmpSpace buffer.
33789 */
33790 if( rc==SQLITE_OK ){
33791 assert( pPager->memDb==0 );
33792 rc = sqlite3PagerSetPagesize(pPager, &szPageDflt, -1);
33793 testcase( rc!=SQLITE_OK );
33794 }
33795
33796 /* If an error occurred in either of the blocks above, free the
33797 ** Pager structure and close the file.
@@ -33767,10 +33802,11 @@
33802 sqlite3_free(pPager);
33803 return rc;
33804 }
33805
33806 /* Initialize the PCache object. */
33807 assert( nExtra<1000 );
33808 nExtra = ROUND8(nExtra);
33809 sqlite3PcacheOpen(szPageDflt, nExtra, !memDb,
33810 !memDb?pagerStress:0, (void *)pPager, pPager->pPCache);
33811
33812 PAGERTRACE(("OPEN %d %s\n", FILEHANDLEID(pPager->fd), pPager->zFilename));
@@ -33802,11 +33838,11 @@
33838 pPager->fullSync = pPager->noSync ?0:1;
33839 pPager->sync_flags = SQLITE_SYNC_NORMAL;
33840 /* pPager->pFirst = 0; */
33841 /* pPager->pFirstSynced = 0; */
33842 /* pPager->pLast = 0; */
33843 pPager->nExtra = (u16)nExtra;
33844 pPager->journalSizeLimit = SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT;
33845 assert( isOpen(pPager->fd) || tempFile );
33846 setSectorSize(pPager);
33847 if( memDb ){
33848 pPager->journalMode = PAGER_JOURNALMODE_MEMORY;
@@ -33966,11 +34002,11 @@
34002 }
34003 if( pgno==1 ){
34004 u8 *dbFileVers = &((u8*)pPg->pData)[24];
34005 memcpy(&pPager->dbFileVers, dbFileVers, sizeof(pPager->dbFileVers));
34006 }
34007 CODEC1(pPager, pPg->pData, pgno, 3, rc = SQLITE_NOMEM);
34008
34009 PAGER_INCR(sqlite3_pager_readdb_count);
34010 PAGER_INCR(pPager->nRead);
34011 IOTRACE(("PGIN %p %d\n", pPager, pgno));
34012 PAGERTRACE(("FETCH %d page %d hash(%08x)\n",
@@ -34011,19 +34047,17 @@
34047 */
34048 static int pagerSharedLock(Pager *pPager){
34049 int rc = SQLITE_OK; /* Return code */
34050 int isErrorReset = 0; /* True if recovering from error state */
34051
34052 /* If this database has no outstanding page references and is in an
34053 ** error-state, this is a chance to clear the error. Discard the
34054 ** contents of the pager-cache and rollback any hot journal in the
34055 ** file-system.
34056 */
34057 if( !MEMDB && sqlite3PcacheRefCount(pPager->pPCache)==0 && pPager->errCode ){
34058 if( isOpen(pPager->jfd) || pPager->zJournal ){
 
 
34059 isErrorReset = 1;
34060 }
34061 pPager->errCode = SQLITE_OK;
34062 pager_reset(pPager);
34063 }
@@ -34102,13 +34136,16 @@
34136 if( rc==SQLITE_OK && fout&SQLITE_OPEN_READONLY ){
34137 rc = SQLITE_CANTOPEN;
34138 sqlite3OsClose(pPager->jfd);
34139 }
34140 }else{
34141 /* If the journal does not exist, it usually means that some
34142 ** other connection managed to get in and roll it back before
34143 ** this connection obtained the exclusive lock above. Or, it
34144 ** may mean that the pager was in the error-state when this
34145 ** function was called and the journal file does not exist. */
34146 rc = pager_end_transaction(pPager, 0);
34147 }
34148 }
34149 }
34150 if( rc!=SQLITE_OK ){
34151 goto failed;
@@ -34123,14 +34160,16 @@
34160 /* Playback and delete the journal. Drop the database write
34161 ** lock and reacquire the read lock. Purge the cache before
34162 ** playing back the hot-journal so that we don't end up with
34163 ** an inconsistent cache.
34164 */
34165 if( isOpen(pPager->jfd) ){
34166 rc = pager_playback(pPager, 1);
34167 if( rc!=SQLITE_OK ){
34168 rc = pager_error(pPager, rc);
34169 goto failed;
34170 }
34171 }
34172 assert( (pPager->state==PAGER_SHARED)
34173 || (pPager->exclusiveMode && pPager->state>PAGER_SHARED)
34174 );
34175 }
@@ -34306,10 +34345,11 @@
34345 }
34346 assert( pPager->state!=PAGER_UNLOCK );
34347
34348 rc = sqlite3PcacheFetch(pPager->pPCache, pgno, 1, &pPg);
34349 if( rc!=SQLITE_OK ){
34350 pagerUnlockIfUnused(pPager);
34351 return rc;
34352 }
34353 assert( pPg->pgno==pgno );
34354 assert( pPg->pPager==pPager || pPg->pPager==0 );
34355 if( pPg->pPager==0 ){
@@ -34664,11 +34704,11 @@
34704
34705 /* We should never write to the journal file the page that
34706 ** contains the database locks. The following assert verifies
34707 ** that we do not. */
34708 assert( pPg->pgno!=PAGER_MJ_PGNO(pPager) );
34709 CODEC2(pPager, pData, pPg->pgno, 7, return SQLITE_NOMEM, pData2);
34710 cksum = pager_cksum(pPager, (u8*)pData2);
34711 rc = write32bits(pPager->jfd, pPager->journalOff, pPg->pgno);
34712 if( rc==SQLITE_OK ){
34713 rc = sqlite3OsWrite(pPager->jfd, pData2, pPager->pageSize,
34714 pPager->journalOff + 4);
@@ -35515,19 +35555,28 @@
35555 return pPager->noSync;
35556 }
35557
35558 #ifdef SQLITE_HAS_CODEC
35559 /*
35560 ** Set or retrieve the codec for this pager
35561 */
35562 static void sqlite3PagerSetCodec(
35563 Pager *pPager,
35564 void *(*xCodec)(void*,void*,Pgno,int),
35565 void (*xCodecSizeChng)(void*,int,int),
35566 void (*xCodecFree)(void*),
35567 void *pCodec
35568 ){
35569 if( pPager->xCodecFree ) pPager->xCodecFree(pPager->pCodec);
35570 pPager->xCodec = xCodec;
35571 pPager->xCodecSizeChng = xCodecSizeChng;
35572 pPager->xCodecFree = xCodecFree;
35573 pPager->pCodec = pCodec;
35574 pagerReportSize(pPager);
35575 }
35576 static void *sqlite3PagerGetCodec(Pager *pPager){
35577 return pPager->pCodec;
35578 }
35579 #endif
35580
35581 #ifndef SQLITE_OMIT_AUTOVACUUM
35582 /*
@@ -35815,11 +35864,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:
@@ -35876,10 +35925,21 @@
35925 ** 24 4 File change counter
35926 ** 28 4 Reserved for future use
35927 ** 32 4 First freelist page
35928 ** 36 4 Number of freelist pages in the file
35929 ** 40 60 15 4-byte meta values passed to higher layers
35930 **
35931 ** 40 4 Schema cookie
35932 ** 44 4 File format of schema layer
35933 ** 48 4 Size of page cache
35934 ** 52 4 Largest root-page (auto/incr_vacuum)
35935 ** 56 4 1=UTF-8 2=UTF16le 3=UTF16be
35936 ** 60 4 User version
35937 ** 64 4 Incremental vacuum mode
35938 ** 68 4 unused
35939 ** 72 4 unused
35940 ** 76 4 unused
35941 **
35942 ** All of the integer values are big-endian (most significant byte first).
35943 **
35944 ** The file change counter is incremented when the database is changed
35945 ** This counter allows other processes to know when the file has changed
@@ -36441,13 +36501,16 @@
36501 SQLITE_PRIVATE int sqlite3BtreeGetPage(BtShared*, Pgno, MemPage**, int);
36502 SQLITE_PRIVATE int sqlite3BtreeInitPage(MemPage *pPage);
36503 SQLITE_PRIVATE void sqlite3BtreeParseCellPtr(MemPage*, u8*, CellInfo*);
36504 SQLITE_PRIVATE void sqlite3BtreeParseCell(MemPage*, int, CellInfo*);
36505 SQLITE_PRIVATE int sqlite3BtreeRestoreCursorPosition(BtCursor *pCur);
36506 SQLITE_PRIVATE void sqlite3BtreeMoveToParent(BtCursor *pCur);
36507
36508 #ifdef SQLITE_TEST
36509 SQLITE_PRIVATE void sqlite3BtreeGetTempCursor(BtCursor *pCur, BtCursor *pTempCur);
36510 SQLITE_PRIVATE void sqlite3BtreeReleaseTempCursor(BtCursor *pCur);
36511 #endif
36512
36513 /************** End of btreeInt.h ********************************************/
36514 /************** Continuing where we left off in btmutex.c ********************/
36515 #ifndef SQLITE_OMIT_SHARED_CACHE
36516 #if SQLITE_THREADSAFE
@@ -36795,11 +36858,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 */
@@ -36813,11 +36876,11 @@
36876 /*
36877 ** Set this global variable to 1 to enable tracing using the TRACE
36878 ** macro.
36879 */
36880 #if 0
36881 int sqlite3BtreeTrace=1; /* True to enable tracing */
36882 # define TRACE(X) if(sqlite3BtreeTrace){printf X;fflush(stdout);}
36883 #else
36884 # define TRACE(X)
36885 #endif
36886
@@ -37423,11 +37486,10 @@
37486 }
37487
37488 #else /* if defined SQLITE_OMIT_AUTOVACUUM */
37489 #define ptrmapPut(w,x,y,z) SQLITE_OK
37490 #define ptrmapGet(w,x,y,z) SQLITE_OK
 
37491 #endif
37492
37493 /*
37494 ** Given a btree page and a cell index (0 means the first cell on
37495 ** the page, 1 means the second cell, and so forth) return a pointer
@@ -37588,11 +37650,11 @@
37650 if( nSize>pPage->maxLocal ){
37651 nSize = minLocal;
37652 }
37653 nSize += 4;
37654 }
37655 nSize += (u32)(pIter - pCell);
37656
37657 /* The minimum size of any cell is 4 bytes. */
37658 if( nSize<4 ){
37659 nSize = 4;
37660 }
@@ -37615,27 +37677,16 @@
37677 static int ptrmapPutOvflPtr(MemPage *pPage, u8 *pCell){
37678 CellInfo info;
37679 assert( pCell!=0 );
37680 sqlite3BtreeParseCellPtr(pPage, pCell, &info);
37681 assert( (info.nData+(pPage->intKey?0:info.nKey))==info.nPayload );
37682 if( info.iOverflow ){
37683 Pgno ovfl = get4byte(&pCell[info.iOverflow]);
37684 return ptrmapPut(pPage->pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno);
37685 }
37686 return SQLITE_OK;
37687 }
 
 
 
 
 
 
 
 
 
 
 
37688 #endif
37689
37690
37691 /*
37692 ** Defragment the page given. All Cells are moved to the
@@ -37938,11 +37989,11 @@
37989 **
37990 ** The following block of code checks early to see if a cell extends
37991 ** past the end of a page boundary and causes SQLITE_CORRUPT to be
37992 ** returned if it does.
37993 */
37994 #if defined(SQLITE_ENABLE_OVERSIZE_CELL_CHECK)
37995 {
37996 int iCellFirst; /* First allowable cell index */
37997 int iCellLast; /* Last possible cell index */
37998 int i; /* Index into the cell pointer array */
37999 int sz; /* Size of a cell */
@@ -37976,11 +38027,11 @@
38027 size = get2byte(&data[pc+2]);
38028 if( next>0 && next<=pc+size+3 ){
38029 /* Free blocks must be in accending order */
38030 return SQLITE_CORRUPT_BKPT;
38031 }
38032 nFree = nFree + size;
38033 pc = next;
38034 }
38035
38036 /* At this point, nFree contains the sum of the offset to the start
38037 ** of the cell-content area plus the number of free bytes within
@@ -38386,11 +38437,11 @@
38437 #ifndef SQLITE_OMIT_AUTOVACUUM
38438 pBt->autoVacuum = (get4byte(&zDbHeader[36 + 4*4])?1:0);
38439 pBt->incrVacuum = (get4byte(&zDbHeader[36 + 7*4])?1:0);
38440 #endif
38441 }
38442 rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve);
38443 if( rc ) goto btree_open_out;
38444 pBt->usableSize = pBt->pageSize - nReserve;
38445 assert( (pBt->pageSize & 7)==0 ); /* 8-byte alignment of pageSize */
38446
38447 #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
@@ -38677,12 +38728,12 @@
38728 ((pageSize-1)&pageSize)==0 ){
38729 assert( (pageSize & 7)==0 );
38730 assert( !pBt->pPage1 && !pBt->pCursor );
38731 pBt->pageSize = (u16)pageSize;
38732 freeTempSpace(pBt);
 
38733 }
38734 rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve);
38735 pBt->usableSize = pBt->pageSize - (u16)nReserve;
38736 if( iFix ) pBt->pageSizeFixed = 1;
38737 sqlite3BtreeLeave(p);
38738 return rc;
38739 }
@@ -38833,15 +38884,16 @@
38884 */
38885 releasePage(pPage1);
38886 pBt->usableSize = (u16)usableSize;
38887 pBt->pageSize = (u16)pageSize;
38888 freeTempSpace(pBt);
38889 rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize,
38890 pageSize-usableSize);
38891 if( rc ) goto page1_init_failed;
38892 return SQLITE_OK;
38893 }
38894 if( usableSize<480 ){
38895 goto page1_init_failed;
38896 }
38897 pBt->pageSize = (u16)pageSize;
38898 pBt->usableSize = (u16)usableSize;
38899 #ifndef SQLITE_OMIT_AUTOVACUUM
@@ -39494,11 +39546,13 @@
39546
39547 assert( nRef==sqlite3PagerRefcount(pPager) );
39548 return rc;
39549 }
39550
39551 #else /* ifndef SQLITE_OMIT_AUTOVACUUM */
39552 # define setChildPtrmaps(x) SQLITE_OK
39553 #endif
39554
39555 /*
39556 ** This routine does the first phase of a two-phase commit. This routine
39557 ** causes a rollback journal to be created (if it does not already exist)
39558 ** and populated with enough information so that if a power loss occurs
@@ -39991,10 +40045,11 @@
40045 sqlite3BtreeLeave(pBtree);
40046 }
40047 return SQLITE_OK;
40048 }
40049
40050 #ifdef SQLITE_TEST
40051 /*
40052 ** Make a temporary cursor by filling in the fields of pTempCur.
40053 ** The temporary cursor is not on the cursor list for the Btree.
40054 */
40055 SQLITE_PRIVATE void sqlite3BtreeGetTempCursor(BtCursor *pCur, BtCursor *pTempCur){
@@ -40006,11 +40061,13 @@
40061 for(i=0; i<=pTempCur->iPage; i++){
40062 sqlite3PagerRef(pTempCur->apPage[i]->pDbPage);
40063 }
40064 assert( pTempCur->pKey==0 );
40065 }
40066 #endif /* SQLITE_TEST */
40067
40068 #ifdef SQLITE_TEST
40069 /*
40070 ** Delete a temporary cursor such as was made by the CreateTemporaryCursor()
40071 ** function above.
40072 */
40073 SQLITE_PRIVATE void sqlite3BtreeReleaseTempCursor(BtCursor *pCur){
@@ -40019,12 +40076,11 @@
40076 for(i=0; i<=pCur->iPage; i++){
40077 sqlite3PagerUnref(pCur->apPage[i]->pDbPage);
40078 }
40079 sqlite3_free(pCur->pKey);
40080 }
40081 #endif /* SQLITE_TEST */
 
40082
40083 /*
40084 ** Make sure the BtCursor* given in the argument has a valid
40085 ** BtCursor.info structure. If it is not already valid, call
40086 ** sqlite3BtreeParseCell() to fill it in.
@@ -41182,11 +41238,11 @@
41238 u8 exact
41239 ){
41240 MemPage *pPage1;
41241 int rc;
41242 u32 n; /* Number of pages on the freelist */
41243 u32 k; /* Number of leaves on the trunk of the freelist */
41244 MemPage *pTrunk = 0;
41245 MemPage *pPrevTrunk = 0;
41246 Pgno mxPage; /* Total size of the database file */
41247
41248 assert( sqlite3_mutex_held(pBt->mutex) );
@@ -41260,11 +41316,11 @@
41316 *pPgno = iTrunk;
41317 memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4);
41318 *ppPage = pTrunk;
41319 pTrunk = 0;
41320 TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1));
41321 }else if( k>(u32)(pBt->usableSize/4 - 2) ){
41322 /* Value of k is out of range. Database corruption */
41323 rc = SQLITE_CORRUPT_BKPT;
41324 goto end_allocate_page;
41325 #ifndef SQLITE_OMIT_AUTOVACUUM
41326 }else if( searchList && nearby==iTrunk ){
@@ -41320,21 +41376,22 @@
41376 }
41377 }
41378 pTrunk = 0;
41379 TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1));
41380 #endif
41381 }else if( k>0 ){
41382 /* Extract a leaf from the trunk */
41383 u32 closest;
41384 Pgno iPage;
41385 unsigned char *aData = pTrunk->aData;
41386 rc = sqlite3PagerWrite(pTrunk->pDbPage);
41387 if( rc ){
41388 goto end_allocate_page;
41389 }
41390 if( nearby>0 ){
41391 u32 i;
41392 int dist;
41393 closest = 0;
41394 dist = get4byte(&aData[8]) - nearby;
41395 if( dist<0 ) dist = -dist;
41396 for(i=1; i<k; i++){
41397 int d2 = get4byte(&aData[8+i*4]) - nearby;
@@ -41356,11 +41413,11 @@
41413 if( !searchList || iPage==nearby ){
41414 int noContent;
41415 Pgno nPage;
41416 *pPgno = iPage;
41417 nPage = pagerPagecount(pBt);
41418 if( iPage>nPage ){
41419 /* Free page off the end of the file */
41420 rc = SQLITE_CORRUPT_BKPT;
41421 goto end_allocate_page;
41422 }
41423 TRACE(("ALLOCATE: %d was leaf %d of %d on trunk %d"
@@ -41434,10 +41491,12 @@
41491 if( sqlite3PagerPageRefcount((*ppPage)->pDbPage)>1 ){
41492 releasePage(*ppPage);
41493 return SQLITE_CORRUPT_BKPT;
41494 }
41495 (*ppPage)->isInit = 0;
41496 }else{
41497 *ppPage = 0;
41498 }
41499 return rc;
41500 }
41501
41502 /*
@@ -41844,11 +41903,11 @@
41903 MemPage *pPage, /* Page into which we are copying */
41904 int i, /* New cell becomes the i-th cell of the page */
41905 u8 *pCell, /* Content of the new cell */
41906 int sz, /* Bytes of content in pCell */
41907 u8 *pTemp, /* Temp storage space for pCell, if needed */
41908 Pgno iChild /* If non-zero, replace first 4 bytes with this value */
41909 ){
41910 int idx; /* Where to write new cell content in data[] */
41911 int j; /* Loop counter */
41912 int top; /* First byte of content for any cell in data[] */
41913 int end; /* First byte past the last cell pointer in data[] */
@@ -41855,10 +41914,12 @@
41914 int ins; /* Index in data[] where new cell pointer is inserted */
41915 int hdr; /* Offset into data[] of the page header */
41916 int cellOffset; /* Address of first cell pointer in data[] */
41917 u8 *data; /* The content of the whole page */
41918 u8 *ptr; /* Used for moving information around in data[] */
41919
41920 int nSkip = (iChild ? 4 : 0);
41921
41922 assert( i>=0 && i<=pPage->nCell+pPage->nOverflow );
41923 assert( pPage->nCell<=MX_CELL(pPage->pBt) && MX_CELL(pPage->pBt)<=5460 );
41924 assert( pPage->nOverflow<=ArraySize(pPage->aOvfl) );
41925 assert( sz==cellSizePtr(pPage, pCell) );
@@ -41866,15 +41927,17 @@
41927 if( pPage->nOverflow || sz+2>pPage->nFree ){
41928 if( pTemp ){
41929 memcpy(pTemp+nSkip, pCell+nSkip, sz-nSkip);
41930 pCell = pTemp;
41931 }
41932 if( iChild ){
41933 put4byte(pCell, iChild);
41934 }
41935 j = pPage->nOverflow++;
41936 assert( j<(int)(sizeof(pPage->aOvfl)/sizeof(pPage->aOvfl[0])) );
41937 pPage->aOvfl[j].pCell = pCell;
41938 pPage->aOvfl[j].idx = (u16)i;
 
41939 }else{
41940 int rc = sqlite3PagerWrite(pPage->pDbPage);
41941 if( rc!=SQLITE_OK ){
41942 return rc;
41943 }
@@ -41900,10 +41963,13 @@
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){
41972 ptr[0] = ptr[-2];
41973 ptr[1] = ptr[-1];
41974 }
41975 put2byte(&data[ins], idx);
@@ -41911,18 +41977,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;
@@ -41981,12 +42040,10 @@
42040 ** The value of NN appears to give the best results overall.
42041 */
42042 #define NN 1 /* Number of neighbors on either side of pPage */
42043 #define NB (NN*2+1) /* Total pages involved in the balance */
42044
 
 
42045
42046 #ifndef SQLITE_OMIT_QUICKBALANCE
42047 /*
42048 ** This version of balance() handles the common special case where
42049 ** a new entry is being inserted on the extreme right-end of the
@@ -42001,42 +42058,62 @@
42058 ** fill up. On average.
42059 **
42060 ** pPage is the leaf page which is the right-most page in the tree.
42061 ** pParent is its parent. pPage must have a single overflow entry
42062 ** which is also the right-most entry on the page.
42063 **
42064 ** The pSpace buffer is used to store a temporary copy of the divider
42065 ** cell that will be inserted into pParent. Such a cell consists of a 4
42066 ** byte page number followed by a variable length integer. In other
42067 ** words, at most 13 bytes. Hence the pSpace buffer must be at
42068 ** least 13 bytes in size.
42069 */
42070 static int balance_quick(MemPage *pParent, MemPage *pPage, u8 *pSpace){
42071 BtShared *const pBt = pPage->pBt; /* B-Tree Database */
42072 MemPage *pNew; /* Newly allocated page */
 
42073 int rc; /* Return Code */
42074 Pgno pgnoNew; /* Page number of pNew */
42075
42076 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
42077 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
42078 assert( pPage->nOverflow==1 );
42079
42080 if( pPage->nCell<=0 ) return SQLITE_CORRUPT_BKPT;
42081
42082 /* Allocate a new page. This page will become the right-sibling of
42083 ** pPage. Make the parent page writable, so that the new divider cell
42084 ** may be inserted. If both these operations are successful, proceed.
42085 */
42086 rc = allocateBtreePage(pBt, &pNew, &pgnoNew, 0, 0);
42087
42088 if( rc==SQLITE_OK ){
42089
42090 u8 *pOut = &pSpace[4];
 
 
 
 
 
 
 
42091 u8 *pCell = pPage->aOvfl[0].pCell;
42092 u16 szCell = cellSizePtr(pPage, pCell);
42093 u8 *pStop;
42094
42095 assert( sqlite3PagerIswriteable(pNew->pDbPage) );
42096 assert( pPage->aData[0]==(PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF) );
42097 zeroPage(pNew, PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF);
42098 assemblePage(pNew, 1, &pCell, &szCell);
42099
42100 /* If this is an auto-vacuum database, update the pointer map
42101 ** with entries for the new page, and any pointer from the
42102 ** cell on the page to an overflow page. If either of these
42103 ** operations fails, the return code is set, but the contents
42104 ** of the parent page are still manipulated by thh code below.
42105 ** That is Ok, at this point the parent page is guaranteed to
42106 ** be marked as dirty. Returning an error code will cause a
42107 ** rollback, undoing any changes made to the parent page.
42108 */
42109 if( ISAUTOVACUUM ){
42110 rc = ptrmapPut(pBt, pgnoNew, PTRMAP_BTREE, pParent->pgno);
42111 if( szCell>pNew->minLocal && rc==SQLITE_OK ){
42112 rc = ptrmapPutOvflPtr(pNew, pCell);
42113 }
42114 }
42115
42116 /* Create a divider cell to insert into pParent. The divider cell
42117 ** consists of a 4-byte page number (the page number of pPage) and
42118 ** a variable length key value (which must be the same value as the
42119 ** largest key on pPage).
@@ -42045,278 +42122,263 @@
42122 ** cell on pPage. The first two fields of this cell are the
42123 ** record-length (a variable length integer at most 32-bits in size)
42124 ** and the key value (a variable length integer, may have any value).
42125 ** The first of the while(...) loops below skips over the record-length
42126 ** field. The second while(...) loop copies the key value from the
42127 ** cell on pPage into the pSpace buffer.
42128 */
 
42129 pCell = findCell(pPage, pPage->nCell-1);
42130 pStop = &pCell[9];
42131 while( (*(pCell++)&0x80) && pCell<pStop );
42132 pStop = &pCell[9];
42133 while( ((*(pOut++) = *(pCell++))&0x80) && pCell<pStop );
42134
42135 /* Insert the new divider cell into pParent. */
42136 insertCell(pParent,pParent->nCell,pSpace,(int)(pOut-pSpace),0,pPage->pgno);
42137
42138 /* Set the right-child pointer of pParent to point to the new page. */
42139 put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew);
42140
 
 
 
 
 
 
 
 
 
 
 
42141 /* Release the reference to the new page. */
42142 releasePage(pNew);
42143 }
42144
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42145 return rc;
42146 }
42147 #endif /* SQLITE_OMIT_QUICKBALANCE */
42148
42149 #if 0
42150 /*
42151 ** This function does not contribute anything to the operation of SQLite.
42152 ** it is sometimes activated temporarily while debugging code responsible
42153 ** for setting pointer-map entries.
42154 */
42155 static int ptrmapCheckPages(MemPage **apPage, int nPage){
42156 int i, j;
42157 for(i=0; i<nPage; i++){
42158 Pgno n;
42159 u8 e;
42160 MemPage *pPage = apPage[i];
42161 BtShared *pBt = pPage->pBt;
42162 assert( pPage->isInit );
42163
42164 for(j=0; j<pPage->nCell; j++){
42165 CellInfo info;
42166 u8 *z;
42167
42168 z = findCell(pPage, j);
42169 sqlite3BtreeParseCellPtr(pPage, z, &info);
42170 if( info.iOverflow ){
42171 Pgno ovfl = get4byte(&z[info.iOverflow]);
42172 ptrmapGet(pBt, ovfl, &e, &n);
42173 assert( n==pPage->pgno && e==PTRMAP_OVERFLOW1 );
42174 }
42175 if( !pPage->leaf ){
42176 Pgno child = get4byte(z);
42177 ptrmapGet(pBt, child, &e, &n);
42178 assert( n==pPage->pgno && e==PTRMAP_BTREE );
42179 }
42180 }
42181 if( !pPage->leaf ){
42182 Pgno child = get4byte(&pPage->aData[pPage->hdrOffset+8]);
42183 ptrmapGet(pBt, child, &e, &n);
42184 assert( n==pPage->pgno && e==PTRMAP_BTREE );
42185 }
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
42196 ** page are used in the balancing, though both siblings might come from one
42197 ** side if the page is the first or last child of its parent. If the page
42198 ** has fewer than 2 siblings (something which can only happen if the page
42199 ** is a root page or a child of a root page) then all available siblings
42200 ** participate in the balancing.
42201 **
42202 ** The number of siblings of the page might be increased or decreased by
42203 ** one or two in an effort to keep pages nearly full but not over full.
42204 **
42205 ** Note that when this routine is called, some of the cells on the page
42206 ** might not actually be stored in MemPage.aData[]. This can happen
42207 ** if the page is overfull. This routine ensures that all cells allocated
42208 ** to the page and its siblings fit into MemPage.aData[] before returning.
42209 **
42210 ** In the course of balancing the page and its siblings, cells may be
42211 ** inserted into or removed from the parent page (pParent). Doing so
42212 ** may cause the parent page to become overfull or underfull. If this
42213 ** happens, it is the responsibility of the caller to invoke the correct
42214 ** balancing routine to fix this problem (see the balance() routine).
42215 **
42216 ** If this routine fails for any reason, it might leave the database
42217 ** in a corrupted state. So if this routine fails, the database should
42218 ** be rolled back.
42219 **
42220 ** The third argument to this function, aOvflSpace, is a pointer to a
42221 ** buffer page-size bytes in size. If, in inserting cells into the parent
42222 ** page (pParent), the parent page becomes overfull, this buffer is
42223 ** used to store the parents overflow cells. Because this function inserts
42224 ** a maximum of four divider cells into the parent page, and the maximum
42225 ** size of a cell stored within an internal node is always less than 1/4
42226 ** of the page-size, the aOvflSpace[] buffer is guaranteed to be large
42227 ** enough for all overflow cells.
42228 **
42229 ** If aOvflSpace is set to a null pointer, this function returns
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[] */
42251 int iOvflSpace = 0; /* First unused byte of aOvflSpace[] */
42252 int szScratch; /* Size of scratch memory requested */
42253 MemPage *apOld[NB]; /* pPage and up to two siblings */
 
42254 MemPage *apCopy[NB]; /* Private copies of apOld[] pages */
42255 MemPage *apNew[NB+2]; /* pPage and up to NB siblings after balancing */
42256 u8 *pRight; /* Location in parent of right-sibling pointer */
42257 u8 *apDiv[NB-1]; /* Divider cells in pParent */
42258 int cntNew[NB+2]; /* Index in aCell[] of cell after i-th page */
42259 int szNew[NB+2]; /* Combined size of cells place on i-th page */
42260 u8 **apCell = 0; /* All cells begin balanced */
42261 u16 *szCell; /* Local size of all cells in apCell[] */
42262 u8 *aSpace1; /* Space for copies of dividers cells */
42263 Pgno pgno; /* Temp var to store a page number in */
42264
42265 pBt = pParent->pBt;
42266 assert( sqlite3_mutex_held(pBt->mutex) );
42267 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
42268
42269 #if 0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42270 TRACE(("BALANCE: begin page %d child of %d\n", pPage->pgno, pParent->pgno));
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42271 #endif
42272
42273 /* At this point pParent may have at most one overflow cell. And if
42274 ** this overflow cell is present, it must be the cell with
42275 ** index iParentIdx. This scenario comes about when this function
42276 ** is called (indirectly) from sqlite3BtreeDelete(). */
42277 assert( pParent->nOverflow==0 || pParent->nOverflow==1 );
42278 assert( pParent->nOverflow==0 || pParent->aOvfl[0].idx==iParentIdx );
42279
42280 if( !aOvflSpace ){
42281 return SQLITE_NOMEM;
42282 }
42283
42284 /* Find the sibling pages to balance. Also locate the cells in pParent
42285 ** that divide the siblings. An attempt is made to find NN siblings on
42286 ** either side of pPage. More siblings are taken from one side, however,
42287 ** if there are fewer than NN siblings on the other side. If pParent
42288 ** has NB or fewer children then all children of pParent are taken.
42289 **
42290 ** This loop also drops the divider cells from the parent page. This
42291 ** way, the remainder of the function does not have to deal with any
42292 ** overflow cells in the parent page, as if one existed it has already
42293 ** been removed. */
42294 i = pParent->nOverflow + pParent->nCell;
42295 if( i<2 ){
 
42296 nxDiv = 0;
42297 nOld = i+1;
42298 }else{
42299 nOld = 3;
42300 if( iParentIdx==0 ){
42301 nxDiv = 0;
42302 }else if( iParentIdx==i ){
42303 nxDiv = i-2;
42304 }else{
42305 nxDiv = iParentIdx-1;
42306 }
42307 i = 2;
42308 }
42309 if( (i+nxDiv-pParent->nOverflow)==pParent->nCell ){
42310 pRight = &pParent->aData[pParent->hdrOffset+8];
42311 }else{
42312 pRight = findCell(pParent, i+nxDiv-pParent->nOverflow);
42313 }
42314 pgno = get4byte(pRight);
42315 while( 1 ){
42316 rc = getAndInitPage(pBt, pgno, &apOld[i]);
42317 if( rc ){
42318 memset(apOld, 0, i*sizeof(MemPage*));
42319 goto balance_cleanup;
42320 }
42321 nMaxCells += 1+apOld[i]->nCell+apOld[i]->nOverflow;
42322 if( (i--)==0 ) break;
42323
42324 if( pParent->nOverflow && i+nxDiv==pParent->aOvfl[0].idx ){
42325 apDiv[i] = pParent->aOvfl[0].pCell;
42326 pgno = get4byte(apDiv[i]);
42327 szNew[i] = cellSizePtr(pParent, apDiv[i]);
42328 pParent->nOverflow = 0;
42329 }else{
42330 apDiv[i] = findCell(pParent, i+nxDiv-pParent->nOverflow);
42331 pgno = get4byte(apDiv[i]);
42332 szNew[i] = cellSizePtr(pParent, apDiv[i]);
42333
42334 /* Drop the cell from the parent page. apDiv[i] still points to
42335 ** the cell within the parent, even though it has been dropped.
42336 ** This is safe because dropping a cell only overwrites the first
42337 ** four bytes of it, and this function does not need the first
42338 ** four bytes of the divider cell. So the pointer is safe to use
42339 ** later on.
42340 **
42341 ** Unless SQLite is compiled in secure-delete mode. In this case,
42342 ** the dropCell() routine will overwrite the entire cell with zeroes.
42343 ** In this case, temporarily copy the cell into the aOvflSpace[]
42344 ** buffer. It will be copied out again as soon as the aSpace[] buffer
42345 ** is allocated. */
42346 #ifdef SQLITE_SECURE_DELETE
42347 memcpy(&aOvflSpace[apDiv[i]-pParent->aData], apDiv[i], szNew[i]);
42348 apDiv[i] = &aOvflSpace[apDiv[i]-pParent->aData];
42349 #endif
42350 dropCell(pParent, i+nxDiv-pParent->nOverflow, szNew[i]);
42351 }
42352 }
42353
42354 /* Make nMaxCells a multiple of 4 in order to preserve 8-byte
42355 ** alignment */
42356 nMaxCells = (nMaxCells + 3)&~3;
42357
42358 /*
42359 ** Allocate space for memory structures
42360 */
42361 k = pBt->pageSize + ROUND8(sizeof(MemPage));
42362 szScratch =
42363 nMaxCells*sizeof(u8*) /* apCell */
42364 + nMaxCells*sizeof(u16) /* szCell */
 
42365 + pBt->pageSize /* aSpace1 */
42366 + k*nOld; /* Page copies (apCopy) */
42367 apCell = sqlite3ScratchMalloc( szScratch );
42368 if( apCell==0 ){
42369 rc = SQLITE_NOMEM;
42370 goto balance_cleanup;
42371 }
42372 szCell = (u16*)&apCell[nMaxCells];
42373 aSpace1 = (u8*)&szCell[nMaxCells];
 
 
 
 
 
 
42374 assert( EIGHT_BYTE_ALIGNMENT(aSpace1) );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42375
42376 /*
42377 ** Load pointers to all cells on sibling pages and the divider cells
42378 ** into the local apCell[] array. Make copies of the divider cells
42379 ** into space obtained from aSpace1[] and remove the the divider Cells
42380 ** from pParent.
42381 **
42382 ** If the siblings are on leaf pages, then the child pointers of the
42383 ** divider cells are stripped from the cells before they are copied
42384 ** into aSpace1[]. In this way, all cells in apCell[] are without
@@ -42325,72 +42387,58 @@
42387 ** are alike.
42388 **
42389 ** leafCorrection: 4 if pPage is a leaf. 0 if pPage is not a leaf.
42390 ** leafData: 1 if pPage holds key+data and pParent holds only keys.
42391 */
42392 leafCorrection = apOld[0]->leaf*4;
42393 leafData = apOld[0]->hasData;
 
42394 for(i=0; i<nOld; i++){
42395 int limit;
42396
42397 /* Before doing anything else, take a copy of the i'th original sibling
42398 ** The rest of this function will use data from the copies rather
42399 ** that the original pages since the original pages will be in the
42400 ** process of being overwritten. */
42401 MemPage *pOld = apCopy[i] = (MemPage*)&aSpace1[pBt->pageSize + k*i];
42402 memcpy(pOld, apOld[i], sizeof(MemPage));
42403 pOld->aData = (void*)&pOld[1];
42404 memcpy(pOld->aData, apOld[i]->aData, pBt->pageSize);
42405
42406 limit = pOld->nCell+pOld->nOverflow;
42407 for(j=0; j<limit; j++){
42408 assert( nCell<nMaxCells );
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 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 */
42431 memcpy(apCell[nCell], &pOld->aData[8], 4);
42432 }else{
42433 assert( leafCorrection==4 );
42434 if( szCell[nCell]<4 ){
42435 /* Do not allow any cells smaller than 4 bytes. */
42436 szCell[nCell] = 4;
42437 }
42438 }
42439 nCell++;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42440 }
42441 }
42442
42443 /*
42444 ** Figure out the number of pages needed to hold all nCell cells.
@@ -42416,10 +42464,11 @@
42464 szNew[k] = subtotal - szCell[i];
42465 cntNew[k] = i;
42466 if( leafData ){ i--; }
42467 subtotal = 0;
42468 k++;
42469 if( k>NB+1 ){ rc = SQLITE_CORRUPT; goto balance_cleanup; }
42470 }
42471 }
42472 szNew[k] = subtotal;
42473 cntNew[k] = nCell;
42474 k++;
@@ -42459,30 +42508,46 @@
42508 ** a virtual root page. A virtual root page is when the real root
42509 ** page is page 1 and we are the only child of that page.
42510 */
42511 assert( cntNew[0]>0 || (pParent->pgno==1 && pParent->nCell==0) );
42512
42513 TRACE(("BALANCE: old: %d %d %d ",
42514 apOld[0]->pgno,
42515 nOld>=2 ? apOld[1]->pgno : 0,
42516 nOld>=3 ? apOld[2]->pgno : 0
42517 ));
42518
42519 /*
42520 ** Allocate k new pages. Reuse old pages where possible.
42521 */
42522 if( apOld[0]->pgno<=1 ){
42523 rc = SQLITE_CORRUPT;
42524 goto balance_cleanup;
42525 }
42526 pageFlags = apOld[0]->aData[0];
42527 for(i=0; i<k; i++){
42528 MemPage *pNew;
42529 if( i<nOld ){
42530 pNew = apNew[i] = apOld[i];
 
42531 apOld[i] = 0;
42532 rc = sqlite3PagerWrite(pNew->pDbPage);
42533 nNew++;
42534 if( rc ) goto balance_cleanup;
42535 }else{
42536 assert( i>0 );
42537 rc = allocateBtreePage(pBt, &pNew, &pgno, pgno, 0);
42538 if( rc ) goto balance_cleanup;
42539 apNew[i] = pNew;
42540 nNew++;
42541
42542 /* Set the pointer-map entry for the new sibling page. */
42543 if( ISAUTOVACUUM ){
42544 rc = ptrmapPut(pBt, pNew->pgno, PTRMAP_BTREE, pParent->pgno);
42545 if( rc!=SQLITE_OK ){
42546 goto balance_cleanup;
42547 }
42548 }
42549 }
42550 }
42551
42552 /* Free any old pages that were not reused as new pages.
42553 */
@@ -42507,38 +42572,36 @@
42572 **
42573 ** When NB==3, this one optimization makes the database
42574 ** about 25% faster for large insertions and deletions.
42575 */
42576 for(i=0; i<k-1; i++){
42577 int minV = apNew[i]->pgno;
42578 int minI = i;
42579 for(j=i+1; j<k; j++){
42580 if( apNew[j]->pgno<(unsigned)minV ){
42581 minI = j;
42582 minV = apNew[j]->pgno;
42583 }
42584 }
42585 if( minI>i ){
42586 int t;
42587 MemPage *pT;
42588 t = apNew[i]->pgno;
42589 pT = apNew[i];
 
42590 apNew[i] = apNew[minI];
 
42591 apNew[minI] = pT;
42592 }
42593 }
42594 TRACE(("new: %d(%d) %d(%d) %d(%d) %d(%d) %d(%d)\n",
42595 apNew[0]->pgno, szNew[0],
42596 nNew>=2 ? apNew[1]->pgno : 0, nNew>=2 ? szNew[1] : 0,
42597 nNew>=3 ? apNew[2]->pgno : 0, nNew>=3 ? szNew[2] : 0,
42598 nNew>=4 ? apNew[3]->pgno : 0, nNew>=4 ? szNew[3] : 0,
42599 nNew>=5 ? apNew[4]->pgno : 0, nNew>=5 ? szNew[4] : 0));
42600
42601 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
42602 put4byte(pRight, apNew[nNew-1]->pgno);
42603
42604 /*
42605 ** Evenly distribute the data in apCell[] across the new pages.
42606 ** Insert divider cells into pParent as necessary.
42607 */
@@ -42545,36 +42608,15 @@
42608 j = 0;
42609 for(i=0; i<nNew; i++){
42610 /* Assemble the new sibling page. */
42611 MemPage *pNew = apNew[i];
42612 assert( j<nMaxCells );
 
42613 zeroPage(pNew, pageFlags);
42614 assemblePage(pNew, cntNew[i]-j, &apCell[j], &szCell[j]);
42615 assert( pNew->nCell>0 || (nNew==1 && cntNew[0]==0) );
42616 assert( pNew->nOverflow==0 );
42617
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 */
@@ -42584,21 +42626,13 @@
42626 int sz;
42627
42628 assert( j<nMaxCells );
42629 pCell = apCell[j];
42630 sz = szCell[j] + leafCorrection;
42631 pTemp = &aOvflSpace[iOvflSpace];
42632 if( !pNew->leaf ){
42633 memcpy(&pNew->aData[8], pCell, 4);
 
 
 
 
 
 
 
 
42634 }else if( leafData ){
42635 /* If the tree is a leaf-data tree, and the siblings are leaves,
42636 ** then there is no divider cell in apCell[]. Instead, the divider
42637 ** cell consists of the integer key for the right-most cell of
42638 ** the sibling-page assembled above only.
@@ -42605,14 +42639,11 @@
42639 */
42640 CellInfo info;
42641 j--;
42642 sqlite3BtreeParseCellPtr(pNew, apCell[j], &info);
42643 pCell = pTemp;
42644 sz = 4 + putVarint(&pCell[4], info.nKey);
 
 
 
42645 pTemp = 0;
42646 }else{
42647 pCell -= 4;
42648 /* Obscure case for non-leaf-data trees: If the cell at pCell was
42649 ** previously stored on a leaf node, and its reported size was 4
@@ -42628,300 +42659,457 @@
42659 if( szCell[j]==4 ){
42660 assert(leafCorrection==4);
42661 sz = cellSizePtr(pParent, pCell);
42662 }
42663 }
42664 iOvflSpace += sz;
42665 assert( sz<=pBt->pageSize/4 );
42666 assert( iOvflSpace<=pBt->pageSize );
42667 rc = insertCell(pParent, nxDiv, pCell, sz, pTemp, pNew->pgno);
42668 if( rc!=SQLITE_OK ) goto balance_cleanup;
42669 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
42670
 
 
 
 
 
 
 
 
 
 
 
42671 j++;
42672 nxDiv++;
42673 }
 
 
 
 
 
 
 
 
42674 }
42675 assert( j==nCell );
42676 assert( nOld>0 );
42677 assert( nNew>0 );
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);
42720 j = 0; /* Current 'old' sibling page */
42721 k = 0; /* Current 'new' sibling page */
42722 for(i=0; i<nCell && rc==SQLITE_OK; i++){
42723 int isDivider = 0;
42724 while( i==iNextOld ){
42725 /* Cell i is the cell immediately following the last cell on old
42726 ** sibling page j. If the siblings are not leaf pages of an
42727 ** intkey b-tree, then cell i was a divider cell. */
42728 pOld = apCopy[++j];
42729 iNextOld = i + !leafData + pOld->nCell + pOld->nOverflow;
42730 if( pOld->nOverflow ){
42731 nOverflow = pOld->nOverflow;
42732 iOverflow = i + !leafData + pOld->aOvfl[0].idx;
42733 }
42734 isDivider = !leafData;
42735 }
42736
42737 assert(nOverflow>0 || iOverflow<i );
42738 assert(nOverflow<2 || pOld->aOvfl[0].idx==pOld->aOvfl[1].idx-1);
42739 assert(nOverflow<3 || pOld->aOvfl[1].idx==pOld->aOvfl[2].idx-1);
42740 if( i==iOverflow ){
42741 isDivider = 1;
42742 if( (--nOverflow)>0 ){
42743 iOverflow++;
42744 }
42745 }
42746
42747 if( i==cntNew[k] ){
42748 /* Cell i is the cell immediately following the last cell on new
42749 ** sibling page k. If the siblings are not leaf pages of an
42750 ** intkey b-tree, then cell i is a divider cell. */
42751 pNew = apNew[++k];
42752 if( !leafData ) continue;
42753 }
42754 assert( rc==SQLITE_OK );
42755 assert( j<nOld );
42756 assert( k<nNew );
42757
42758 /* If the cell was originally divider cell (and is not now) or
42759 ** an overflow cell, or if the cell was located on a different sibling
42760 ** page before the balancing, then the pointer map entries associated
42761 ** with any child or overflow pages need to be updated. */
42762 if( isDivider || pOld->pgno!=pNew->pgno ){
42763 if( !leafCorrection ){
42764 rc = ptrmapPut(pBt, get4byte(apCell[i]), PTRMAP_BTREE, pNew->pgno);
42765 }
42766 if( szCell[i]>pNew->minLocal && rc==SQLITE_OK ){
42767 rc = ptrmapPutOvflPtr(pNew, apCell[i]);
42768 }
42769 }
42770 }
42771
42772 if( !leafCorrection ){
42773 for(i=0; rc==SQLITE_OK && i<nNew; i++){
42774 rc = ptrmapPut(
42775 pBt, get4byte(&apNew[i]->aData[8]), PTRMAP_BTREE, apNew[i]->pgno);
42776 }
42777 }
42778
42779 #if 0
42780 /* The ptrmapCheckPages() contains assert() statements that verify that
42781 ** all pointer map pages are set correctly. This is helpful while
42782 ** debugging. This is usually disabled because a corrupt database may
42783 ** cause an assert() statement to fail. */
42784 ptrmapCheckPages(apNew, nNew);
42785 ptrmapCheckPages(&pParent, 1);
42786 #endif
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);
42798 for(i=0; i<nOld; i++){
42799 releasePage(apOld[i]);
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 /*
42913 ** This function is called when the root page of a b-tree structure is
42914 ** overfull (has one or more overflow pages).
42915 **
42916 ** A new child page is allocated and the contents of the current root
42917 ** page, including overflow cells, are copied into the child. The root
42918 ** page is then overwritten to make it an empty page with the right-child
42919 ** pointer pointing to the new page.
42920 **
42921 ** Before returning, all pointer-map entries corresponding to pages
42922 ** that the new child-page now contains pointers to are updated. The
42923 ** entry corresponding to the new right-child pointer of the root
42924 ** page is also updated.
42925 **
42926 ** If successful, *ppChild is set to contain a reference to the child
42927 ** page and SQLITE_OK is returned. In this case the caller is required
42928 ** to call releasePage() on *ppChild exactly once. If an error occurs,
42929 ** an error code is returned and *ppChild is set to 0.
42930 */
42931 static int balance_deeper(MemPage *pRoot, MemPage **ppChild){
42932 int rc; /* Return value from subprocedures */
42933 MemPage *pChild = 0; /* Pointer to a new child page */
42934 Pgno pgnoChild; /* Page number of the new child page */
42935 BtShared *pBt = pRoot->pBt; /* The BTree */
42936
42937 assert( pRoot->nOverflow>0 );
42938 assert( sqlite3_mutex_held(pBt->mutex) );
42939
42940 /* Make pRoot, the root page of the b-tree, writable. Allocate a new
42941 ** page that will become the new right-child of pPage. Copy the contents
42942 ** of the node stored on pRoot into the new child page.
42943 */
42944 if( SQLITE_OK!=(rc = sqlite3PagerWrite(pRoot->pDbPage))
42945 || SQLITE_OK!=(rc = allocateBtreePage(pBt,&pChild,&pgnoChild,pRoot->pgno,0))
42946 || SQLITE_OK!=(rc = copyNodeContent(pRoot, pChild))
42947 || (ISAUTOVACUUM &&
42948 SQLITE_OK!=(rc = ptrmapPut(pBt, pgnoChild, PTRMAP_BTREE, pRoot->pgno)))
42949 ){
42950 *ppChild = 0;
42951 releasePage(pChild);
42952 return rc;
42953 }
42954 assert( sqlite3PagerIswriteable(pChild->pDbPage) );
42955 assert( sqlite3PagerIswriteable(pRoot->pDbPage) );
42956 assert( pChild->nCell==pRoot->nCell );
42957
42958 TRACE(("BALANCE: copy root %d into %d\n", pRoot->pgno, pChild->pgno));
42959
42960 /* Copy the overflow cells from pRoot to pChild */
42961 memcpy(pChild->aOvfl, pRoot->aOvfl, pRoot->nOverflow*sizeof(pRoot->aOvfl[0]));
42962 pChild->nOverflow = pRoot->nOverflow;
42963
42964 /* Zero the contents of pRoot. Then install pChild as the right-child. */
42965 zeroPage(pRoot, pChild->aData[0] & ~PTF_LEAF);
42966 put4byte(&pRoot->aData[pRoot->hdrOffset+8], pgnoChild);
42967
42968 *ppChild = pChild;
42969 return SQLITE_OK;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42970 }
42971
42972 /*
42973 ** The page that pCur currently points to has just been modified in
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];
42992 u8 *pFree = 0;
42993
42994 TESTONLY( int balance_quick_called = 0 );
42995 TESTONLY( int balance_deeper_called = 0 );
42996
42997 do {
42998 int iPage = pCur->iPage;
42999 MemPage *pPage = pCur->apPage[iPage];
43000
43001 if( iPage==0 ){
43002 if( pPage->nOverflow ){
43003 /* The root page of the b-tree is overfull. In this case call the
43004 ** balance_deeper() function to create a new child for the root-page
43005 ** and copy the current contents of the root-page to it. The
43006 ** next iteration of the do-loop will balance the child page.
43007 */
43008 assert( (balance_deeper_called++)==0 );
43009 rc = balance_deeper(pPage, &pCur->apPage[1]);
43010 if( rc==SQLITE_OK ){
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{
43036 MemPage * const pParent = pCur->apPage[iPage-1];
43037 int const iIdx = pCur->aiIdx[iPage-1];
43038
43039 rc = sqlite3PagerWrite(pParent->pDbPage);
43040 if( rc==SQLITE_OK ){
43041 #ifndef SQLITE_OMIT_QUICKBALANCE
43042 if( pPage->hasData
43043 && pPage->nOverflow==1
43044 && pPage->aOvfl[0].idx==pPage->nCell
43045 && pParent->pgno!=1
43046 && pParent->nCell==iIdx
43047 ){
43048 /* Call balance_quick() to create a new sibling of pPage on which
43049 ** to store the overflow cell. balance_quick() inserts a new cell
43050 ** into pParent, which may cause pParent overflow. If this
43051 ** happens, the next interation of the do-loop will balance pParent
43052 ** use either balance_nonroot() or balance_deeper(). Until this
43053 ** happens, the overflow cell is stored in the aBalanceQuickSpace[]
43054 ** buffer.
43055 **
43056 ** The purpose of the following assert() is to check that only a
43057 ** single call to balance_quick() is made for each call to this
43058 ** function. If this were not verified, a subtle bug involving reuse
43059 ** of the aBalanceQuickSpace[] might sneak in.
43060 */
43061 assert( (balance_quick_called++)==0 );
43062 rc = balance_quick(pParent, pPage, aBalanceQuickSpace);
43063 }else
43064 #endif
43065 {
43066 /* In this case, call balance_nonroot() to redistribute cells
43067 ** between pPage and up to 2 of its sibling pages. This involves
43068 ** modifying the contents of pParent, which may cause pParent to
43069 ** become overfull or underfull. The next iteration of the do-loop
43070 ** will balance the parent page to correct this.
43071 **
43072 ** If the parent page becomes overfull, the overflow cell or cells
43073 ** are stored in the pSpace buffer allocated immediately below.
43074 ** A subsequent iteration of the do-loop will deal with this by
43075 ** calling balance_nonroot() (balance_deeper() may be called first,
43076 ** but it doesn't deal with overflow cells - just moves them to a
43077 ** different page). Once this subsequent call to balance_nonroot()
43078 ** has completed, it is safe to release the pSpace buffer used by
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. */
43090 sqlite3PageFree(pFree);
43091 }
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
43103 /* The next iteration of the do-loop balances the parent page. */
43104 releasePage(pPage);
43105 pCur->iPage--;
43106 }
43107 }while( rc==SQLITE_OK );
43108
43109 if( pFree ){
43110 sqlite3PageFree(pFree);
43111 }
43112 return rc;
43113 }
43114
43115 /*
@@ -43061,10 +43249,11 @@
43249 SQLITE_OK!=(rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur)) || (!loc &&
43250 SQLITE_OK!=(rc = sqlite3BtreeMoveto(pCur, pKey, nKey, appendBias, &loc))
43251 )){
43252 return rc;
43253 }
43254 assert( pCur->eState==CURSOR_VALID || (pCur->eState==CURSOR_INVALID && loc) );
43255
43256 pPage = pCur->apPage[pCur->iPage];
43257 assert( pPage->intKey || nKey>=0 );
43258 assert( pPage->leaf || !pPage->intKey );
43259 TRACE(("INSERT: table=%d nkey=%lld ndata=%d page=%d %s\n",
@@ -43077,11 +43266,11 @@
43266 rc = fillInCell(pPage, newCell, pKey, nKey, pData, nData, nZero, &szNew);
43267 if( rc ) goto end_insert;
43268 assert( szNew==cellSizePtr(pPage, newCell) );
43269 assert( szNew<=MX_CELL_SIZE(pBt) );
43270 idx = pCur->aiIdx[pCur->iPage];
43271 if( loc==0 ){
43272 u16 szOld;
43273 assert( idx<pPage->nCell );
43274 rc = sqlite3PagerWrite(pPage->pDbPage);
43275 if( rc ){
43276 goto end_insert;
@@ -43098,51 +43287,49 @@
43287 goto end_insert;
43288 }
43289 }else if( loc<0 && pPage->nCell>0 ){
43290 assert( pPage->leaf );
43291 idx = ++pCur->aiIdx[pCur->iPage];
 
 
43292 }else{
43293 assert( pPage->leaf );
43294 }
43295 rc = insertCell(pPage, idx, newCell, szNew, 0, 0);
43296 assert( rc!=SQLITE_OK || pPage->nCell>0 || pPage->nOverflow>0 );
43297
43298 /* If no error has occured and pPage has an overflow cell, call balance()
43299 ** to redistribute the cells within the tree. Since balance() may move
43300 ** the cursor, zero the BtCursor.info.nSize and BtCursor.validNKey
43301 ** variables.
43302 **
43303 ** Previous versions of SQLite called moveToRoot() to move the cursor
43304 ** back to the root page as balance() used to invalidate the contents
43305 ** of BtCursor.apPage[] and BtCursor.aiIdx[]. Instead of doing that,
43306 ** set the cursor state to "invalid". This makes common insert operations
43307 ** slightly faster.
43308 **
43309 ** There is a subtle but important optimization here too. When inserting
43310 ** multiple records into an intkey b-tree using a single cursor (as can
43311 ** happen while processing an "INSERT INTO ... SELECT" statement), it
43312 ** is advantageous to leave the cursor pointing to the last entry in
43313 ** the b-tree if possible. If the cursor is left pointing to the last
43314 ** entry in the table, and the next row inserted has an integer key
43315 ** larger than the largest existing key, it is possible to insert the
43316 ** row without seeking the cursor. This can be a big performance boost.
43317 */
43318 pCur->info.nSize = 0;
43319 pCur->validNKey = 0;
43320 if( rc==SQLITE_OK && pPage->nOverflow ){
43321 rc = balance(pCur);
43322
43323 /* Must make sure nOverflow is reset to zero even if the balance()
43324 ** fails. Internal data structure corruption will result otherwise.
43325 ** Also, set the cursor state to invalid. This stops saveCursorPosition()
43326 ** from trying to save the current position of the cursor. */
43327 pCur->apPage[pCur->iPage]->nOverflow = 0;
43328 pCur->eState = CURSOR_INVALID;
43329 }
43330 assert( pCur->apPage[pCur->iPage]->nOverflow==0 );
43331
43332 end_insert:
43333 return rc;
43334 }
43335
@@ -43149,202 +43336,114 @@
43336 /*
43337 ** Delete the entry that the cursor is pointing to. The cursor
43338 ** is left pointing at a arbitrary location.
43339 */
43340 SQLITE_PRIVATE int sqlite3BtreeDelete(BtCursor *pCur){
 
 
 
 
 
43341 Btree *p = pCur->pBtree;
43342 BtShared *pBt = p->pBt;
43343 int rc; /* Return code */
43344 MemPage *pPage; /* Page to delete cell from */
43345 unsigned char *pCell; /* Pointer to cell to delete */
43346 int iCellIdx; /* Index of cell to delete */
43347 int iCellDepth; /* Depth of node containing pCell */
43348
43349 assert( cursorHoldsMutex(pCur) );
 
43350 assert( pBt->inTransaction==TRANS_WRITE );
43351 assert( !pBt->readOnly );
 
 
 
 
 
 
43352 assert( pCur->wrFlag );
43353 if( NEVER(pCur->aiIdx[pCur->iPage]>=pCur->apPage[pCur->iPage]->nCell)
43354 || NEVER(pCur->eState!=CURSOR_VALID)
43355 ){
43356 return SQLITE_ERROR; /* Something has gone awry. */
43357 }
43358
43359 rc = checkForReadConflicts(p, pCur->pgnoRoot, pCur, pCur->info.nKey);
43360 if( rc!=SQLITE_OK ){
43361 assert( rc==SQLITE_LOCKED_SHAREDCACHE );
43362 return rc; /* The table pCur points to has a read lock */
43363 }
43364
43365 iCellDepth = pCur->iPage;
43366 iCellIdx = pCur->aiIdx[iCellDepth];
43367 pPage = pCur->apPage[iCellDepth];
43368 pCell = findCell(pPage, iCellIdx);
43369
43370 /* If the page containing the entry to delete is not a leaf page, move
43371 ** the cursor to the largest entry in the tree that is smaller than
43372 ** the entry being deleted. This cell will replace the cell being deleted
43373 ** from the internal node. The 'previous' entry is used for this instead
43374 ** of the 'next' entry, as the previous entry is always a part of the
43375 ** sub-tree headed by the child page of the cell being deleted. This makes
43376 ** balancing the tree following the delete operation easier. */
43377 if( !pPage->leaf ){
43378 int notUsed;
43379 if( SQLITE_OK!=(rc = sqlite3BtreePrevious(pCur, &notUsed)) ){
43380 return rc;
43381 }
43382 }
43383
43384 /* Save the positions of any other cursors open on this table before
43385 ** making any modifications. Make the page containing the entry to be
43386 ** deleted writable. Then free any overflow pages associated with the
43387 ** entry and finally remove the cell itself from within the page. */
43388 if( SQLITE_OK!=(rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur))
43389 || SQLITE_OK!=(rc = sqlite3PagerWrite(pPage->pDbPage))
43390 || SQLITE_OK!=(rc = clearCell(pPage, pCell))
43391 || SQLITE_OK!=(rc = dropCell(pPage, iCellIdx, cellSizePtr(pPage, pCell)))
43392 ){
43393 return rc;
43394 }
43395
43396 /* If the cell deleted was not located on a leaf page, then the cursor
43397 ** is currently pointing to the largest entry in the sub-tree headed
43398 ** by the child-page of the cell that was just deleted from an internal
43399 ** node. The cell from the leaf node needs to be moved to the internal
43400 ** node to replace the deleted cell. */
43401 if( !pPage->leaf ){
43402 MemPage *pLeaf = pCur->apPage[pCur->iPage];
43403 int nCell;
43404 Pgno n = pCur->apPage[iCellDepth+1]->pgno;
43405 unsigned char *pTmp;
43406
43407 pCell = findCell(pLeaf, pLeaf->nCell-1);
43408 nCell = cellSizePtr(pLeaf, pCell);
43409 assert( MX_CELL_SIZE(pBt)>=nCell );
43410
43411 allocateTempSpace(pBt);
43412 pTmp = pBt->pTmpSpace;
43413
43414 if( SQLITE_OK!=(rc = sqlite3PagerWrite(pLeaf->pDbPage))
43415 || SQLITE_OK!=(rc = insertCell(pPage, iCellIdx, pCell-4, nCell+4, pTmp, n))
43416 || SQLITE_OK!=(rc = dropCell(pLeaf, pLeaf->nCell-1, nCell))
43417 ){
43418 return rc;
43419 }
43420 }
43421
43422 /* Balance the tree. If the entry deleted was located on a leaf page,
43423 ** then the cursor still points to that page. In this case the first
43424 ** call to balance() repairs the tree, and the if(...) condition is
43425 ** never true.
43426 **
43427 ** Otherwise, if the entry deleted was on an internal node page, then
43428 ** pCur is pointing to the leaf page from which a cell was removed to
43429 ** replace the cell deleted from the internal node. This is slightly
43430 ** tricky as the leaf node may be underfull, and the internal node may
43431 ** be either under or overfull. In this case run the balancing algorithm
43432 ** on the leaf node first. If the balance proceeds far enough up the
43433 ** tree that we can be sure that any problem in the internal node has
43434 ** been corrected, so be it. Otherwise, after balancing the leaf node,
43435 ** walk the cursor up the tree to the internal node and balance it as
43436 ** well. */
43437 rc = balance(pCur);
43438 if( rc==SQLITE_OK && pCur->iPage>iCellDepth ){
43439 while( pCur->iPage>iCellDepth ){
43440 releasePage(pCur->apPage[pCur->iPage--]);
43441 }
43442 rc = balance(pCur);
43443 }
43444
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43445 if( rc==SQLITE_OK ){
43446 moveToRoot(pCur);
43447 }
43448 return rc;
43449 }
@@ -43431,11 +43530,14 @@
43530 rc = sqlite3BtreeGetPage(pBt, pgnoRoot, &pRoot, 0);
43531 if( rc!=SQLITE_OK ){
43532 return rc;
43533 }
43534 rc = ptrmapGet(pBt, pgnoRoot, &eType, &iPtrPage);
43535 if( eType==PTRMAP_ROOTPAGE || eType==PTRMAP_FREEPAGE ){
43536 rc = SQLITE_CORRUPT_BKPT;
43537 }
43538 if( rc!=SQLITE_OK ){
43539 releasePage(pRoot);
43540 return rc;
43541 }
43542 assert( eType!=PTRMAP_ROOTPAGE );
43543 assert( eType!=PTRMAP_FREEPAGE );
@@ -45188,11 +45290,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.
@@ -45582,11 +45684,22 @@
45684 assert( (pMem->flags & MEM_RowSet)==0 );
45685 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
45686 assert( EIGHT_BYTE_ALIGNMENT(pMem) );
45687
45688 pMem->u.i = doubleToInt64(pMem->r);
45689
45690 /* Only mark the value as an integer if
45691 **
45692 ** (1) the round-trip conversion real->int->real is a no-op, and
45693 ** (2) The integer is neither the largest nor the smallest
45694 ** possible integer (ticket #3922)
45695 **
45696 ** The second term in the following conditional enforces the second
45697 ** condition under the assumption that additional overflow causes
45698 ** values to wrap around.
45699 */
45700 if( pMem->r==(double)pMem->u.i && (pMem->u.i-1) < (pMem->u.i+1) ){
45701 pMem->flags |= MEM_Int;
45702 }
45703 }
45704
45705 /*
@@ -45797,10 +45910,16 @@
45910 ** The memory management strategy depends on the value of the xDel
45911 ** parameter. If the value passed is SQLITE_TRANSIENT, then the
45912 ** string is copied into a (possibly existing) buffer managed by the
45913 ** Mem structure. Otherwise, any existing buffer is freed and the
45914 ** pointer copied.
45915 **
45916 ** If the string is too large (if it exceeds the SQLITE_LIMIT_LENGTH
45917 ** size limit) then no memory allocation occurs. If the string can be
45918 ** stored without allocating memory, then it is. If a memory allocation
45919 ** is required to store the string, then value of pMem is unchanged. In
45920 ** either case, SQLITE_TOOBIG is returned.
45921 */
45922 SQLITE_PRIVATE int sqlite3VdbeMemSetStr(
45923 Mem *pMem, /* Memory cell to set to string value */
45924 const char *z, /* String pointer */
45925 int n, /* Bytes in string, or negative */
@@ -45860,13 +45979,10 @@
45979 sqlite3VdbeMemRelease(pMem);
45980 pMem->z = (char *)z;
45981 pMem->xDel = xDel;
45982 flags |= ((xDel==SQLITE_STATIC)?MEM_Static:MEM_Dyn);
45983 }
 
 
 
45984
45985 pMem->n = nByte;
45986 pMem->flags = flags;
45987 pMem->enc = (enc==0 ? SQLITE_UTF8 : enc);
45988 pMem->type = (enc==0 ? SQLITE_BLOB : SQLITE_TEXT);
@@ -45874,10 +45990,14 @@
45990 #ifndef SQLITE_OMIT_UTF16
45991 if( pMem->enc!=SQLITE_UTF8 && sqlite3VdbeMemHandleBom(pMem) ){
45992 return SQLITE_NOMEM;
45993 }
45994 #endif
45995
45996 if( nByte>iLimit ){
45997 return SQLITE_TOOBIG;
45998 }
45999
46000 return SQLITE_OK;
46001 }
46002
46003 /*
@@ -46250,11 +46370,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 /*
@@ -46860,15 +46980,28 @@
46980 ** If a memory allocation error has occurred prior to the calling of this
46981 ** routine, then a pointer to a dummy VdbeOp will be returned. That opcode
46982 ** is readable and writable, but it has no effect. The return of a dummy
46983 ** opcode allows the call to continue functioning after a OOM fault without
46984 ** having to check to see if the return from this routine is a valid pointer.
46985 **
46986 ** About the #ifdef SQLITE_OMIT_TRACE: Normally, this routine is never called
46987 ** unless p->nOp>0. This is because in the absense of SQLITE_OMIT_TRACE,
46988 ** an OP_Trace instruction is always inserted by sqlite3VdbeGet() as soon as
46989 ** a new VDBE is created. So we are free to set addr to p->nOp-1 without
46990 ** having to double-check to make sure that the result is non-negative. But
46991 ** if SQLITE_OMIT_TRACE is defined, the OP_Trace is omitted and we do need to
46992 ** check the value of p->nOp-1 before continuing.
46993 */
46994 SQLITE_PRIVATE VdbeOp *sqlite3VdbeGetOp(Vdbe *p, int addr){
46995 static VdbeOp dummy;
46996 assert( p->magic==VDBE_MAGIC_INIT );
46997 if( addr<0 ){
46998 #ifdef SQLITE_OMIT_TRACE
46999 if( p->nOp==0 ) return &dummy;
47000 #endif
47001 addr = p->nOp - 1;
47002 }
47003 assert( (addr>=0 && addr<p->nOp) || p->db->mallocFailed );
47004 if( p->db->mallocFailed ){
47005 return &dummy;
47006 }else{
47007 return &p->aOp[addr];
@@ -48235,13 +48368,21 @@
48368 sqlite3DbFree(db, p->pFree);
48369 sqlite3DbFree(db, p);
48370 }
48371
48372 /*
48373 ** Make sure the cursor p is ready to read or write the row to which it
48374 ** was last positioned. Return an error code if an OOM fault or I/O error
48375 ** prevents us from positioning the cursor to its correct position.
48376 **
48377 ** If a MoveTo operation is pending on the given cursor, then do that
48378 ** MoveTo now. If no move is pending, check to see if the row has been
48379 ** deleted out from under the cursor and if it has, mark the row as
48380 ** a NULL row.
48381 **
48382 ** If the cursor is already pointing to the correct row and that row has
48383 ** not been deleted out from under the cursor, then this routine is a no-op.
48384 */
48385 SQLITE_PRIVATE int sqlite3VdbeCursorMoveto(VdbeCursor *p){
48386 if( p->deferredMoveto ){
48387 int res, rc;
48388 #ifdef SQLITE_TEST
@@ -48248,11 +48389,11 @@
48389 extern int sqlite3_search_count;
48390 #endif
48391 assert( p->isTable );
48392 rc = sqlite3BtreeMovetoUnpacked(p->pCursor, 0, p->movetoTarget, 0, &res);
48393 if( rc ) return rc;
48394 p->lastRowid = p->movetoTarget;
48395 p->rowidIsValid = ALWAYS(res==0) ?1:0;
48396 if( NEVER(res<0) ){
48397 rc = sqlite3BtreeNext(p->pCursor, &res);
48398 if( rc ) return rc;
48399 }
@@ -48450,11 +48591,11 @@
48591 swapMixedEndianFloat(v);
48592 }else{
48593 v = pMem->u.i;
48594 }
48595 len = i = sqlite3VdbeSerialTypeLen(serial_type);
48596 assert( len<=(u32)nBuf );
48597 while( i-- ){
48598 buf[i] = (u8)(v&0xFF);
48599 v >>= 8;
48600 }
48601 return len;
@@ -48461,11 +48602,11 @@
48602 }
48603
48604 /* String or blob */
48605 if( serial_type>=12 ){
48606 assert( pMem->n + ((pMem->flags & MEM_Zero)?pMem->u.nZero:0)
48607 == (int)sqlite3VdbeSerialTypeLen(serial_type) );
48608 assert( pMem->n<=nBuf );
48609 len = pMem->n;
48610 memcpy(buf, pMem->z, len);
48611 if( pMem->flags & MEM_Zero ){
48612 len += pMem->u.nZero;
@@ -48790,11 +48931,11 @@
48931 ** Return SQLITE_OK if everything works, or an error code otherwise.
48932 **
48933 ** pCur might be pointing to text obtained from a corrupt database file.
48934 ** So the content cannot be trusted. Do appropriate checks on the content.
48935 */
48936 SQLITE_PRIVATE int sqlite3VdbeIdxRowid(sqlite3 *db, BtCursor *pCur, i64 *rowid){
48937 i64 nCellKey = 0;
48938 int rc;
48939 u32 szHdr; /* Size of the header */
48940 u32 typeRowid; /* Serial type of the rowid */
48941 u32 lenRowid; /* Size of the rowid */
@@ -48807,11 +48948,11 @@
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;
48955 rc = sqlite3VdbeMemFromBtree(pCur, 0, (int)nCellKey, 1, &m);
48956 if( rc ){
48957 return rc;
48958 }
@@ -48957,168 +49098,12 @@
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
49109 ** to be recompiled. A statement needs to be recompiled whenever the
@@ -49151,11 +49136,10 @@
49136 sqlite3 *db = v->db;
49137 #if SQLITE_THREADSAFE
49138 sqlite3_mutex *mutex = v->db->mutex;
49139 #endif
49140 sqlite3_mutex_enter(mutex);
 
49141 rc = sqlite3VdbeFinalize(v);
49142 rc = sqlite3ApiExit(db, rc);
49143 sqlite3_mutex_leave(mutex);
49144 }
49145 return rc;
@@ -49175,11 +49159,10 @@
49159 rc = SQLITE_OK;
49160 }else{
49161 Vdbe *v = (Vdbe*)pStmt;
49162 sqlite3_mutex_enter(v->db->mutex);
49163 rc = sqlite3VdbeReset(v);
 
49164 sqlite3VdbeMakeReady(v, -1, 0, 0, 0);
49165 assert( (rc & (v->db->errMask))==rc );
49166 rc = sqlite3ApiExit(v->db, rc);
49167 sqlite3_mutex_leave(v->db->mutex);
49168 }
@@ -49419,11 +49402,10 @@
49402 #endif
49403
49404 db->activeVdbeCnt++;
49405 if( p->readOnly==0 ) db->writeVdbeCnt++;
49406 p->pc = 0;
 
49407 }
49408 #ifndef SQLITE_OMIT_EXPLAIN
49409 if( p->explain ){
49410 rc = sqlite3VdbeList(p);
49411 }else
@@ -49479,33 +49461,20 @@
49461 /*
49462 ** This is the top-level implementation of sqlite3_step(). Call
49463 ** sqlite3Step() to do most of the work. If a schema error occurs,
49464 ** call sqlite3Reprepare() and try again.
49465 */
 
 
 
 
 
 
 
 
 
 
 
 
 
49466 SQLITE_API int sqlite3_step(sqlite3_stmt *pStmt){
49467 int rc = SQLITE_MISUSE;
49468 if( pStmt ){
49469 int cnt = 0;
49470 Vdbe *v = (Vdbe*)pStmt;
49471 sqlite3 *db = v->db;
49472 sqlite3_mutex_enter(db->mutex);
49473 while( (rc = sqlite3Step(v))==SQLITE_SCHEMA
49474 && cnt++ < 5
49475 && (rc = sqlite3Reprepare(v))==SQLITE_OK ){
49476 sqlite3_reset(pStmt);
49477 v->expired = 0;
49478 }
49479 if( rc==SQLITE_SCHEMA && ALWAYS(v->isPrepareV2) && ALWAYS(db->pErr) ){
49480 /* This case occurs after failing to recompile an sql statement.
@@ -49528,11 +49497,10 @@
49497 rc = sqlite3ApiExit(db, rc);
49498 sqlite3_mutex_leave(db->mutex);
49499 }
49500 return rc;
49501 }
 
49502
49503 /*
49504 ** Extract the user data from a sqlite3_context structure and return a
49505 ** pointer to it.
49506 */
@@ -50351,11 +50319,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
@@ -50495,11 +50463,11 @@
50463 static VdbeCursor *allocateCursor(
50464 Vdbe *p, /* The virtual machine */
50465 int iCur, /* Index of the new VdbeCursor */
50466 int nField, /* Number of fields in the table or index */
50467 int iDb, /* When database the cursor belongs to, or -1 */
50468 int isBtreeCursor /* True for B-Tree vs. pseudo-table or vtab */
50469 ){
50470 /* Find the memory cell that will be used to store the blob of memory
50471 ** required for this VdbeCursor structure. It is convenient to use a
50472 ** vdbe memory cell to manage the memory allocation required for a
50473 ** VdbeCursor structure for the following reasons:
@@ -50732,12 +50700,14 @@
50700 fprintf(out, " NULL");
50701 }else if( (p->flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){
50702 fprintf(out, " si:%lld", p->u.i);
50703 }else if( p->flags & MEM_Int ){
50704 fprintf(out, " i:%lld", p->u.i);
50705 #ifndef SQLITE_OMIT_FLOATING_POINT
50706 }else if( p->flags & MEM_Real ){
50707 fprintf(out, " r:%g", p->r);
50708 #endif
50709 }else if( p->flags & MEM_RowSet ){
50710 fprintf(out, " (rowset)");
50711 }else{
50712 char zBuf[200];
50713 sqlite3VdbeMemPrettyPrint(p, zBuf);
@@ -51032,13 +51002,10 @@
51002 int v2; /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
51003 } ak;
51004 struct OP_IfNot_stack_vars {
51005 int c;
51006 } al;
 
 
 
51007 struct OP_Column_stack_vars {
51008 u32 payloadSize; /* Number of bytes in the record */
51009 i64 payloadSize64; /* Number of bytes in the record */
51010 int p1; /* P1 value of the opcode */
51011 int p2; /* column number to retrieve */
@@ -51057,17 +51024,17 @@
51024 u8 *zEndHdr; /* Pointer to first byte after the header */
51025 u32 offset; /* Offset into the data */
51026 u64 offset64; /* 64-bit offset. 64 bits needed to catch overflow */
51027 int szHdr; /* Size of the header size field at start of record */
51028 int avail; /* Number of bytes of available data */
51029 } am;
51030 struct OP_Affinity_stack_vars {
51031 char *zAffinity; /* The affinity to be applied */
51032 Mem *pData0; /* First register to which to apply affinity */
51033 Mem *pLast; /* Last register to which to apply affinity */
51034 Mem *pRec; /* Current register */
51035 } an;
51036 struct OP_MakeRecord_stack_vars {
51037 u8 *zNewRecord; /* A buffer to hold the data for the new record */
51038 Mem *pRec; /* The new record */
51039 u64 nData; /* Number of bytes of data space */
51040 int nHdr; /* Number of bytes of header space */
@@ -51080,270 +51047,246 @@
51047 int nField; /* Number of fields in the record */
51048 char *zAffinity; /* The affinity string for the record */
51049 int file_format; /* File format to use for encoding */
51050 int i; /* Space used in zNewRecord[] */
51051 int len; /* Length of a field */
51052 } ao;
51053 struct OP_Count_stack_vars {
51054 i64 nEntry;
51055 BtCursor *pCrsr;
51056 } ap;
51057 struct OP_Statement_stack_vars {
 
51058 Btree *pBt;
51059 } aq;
51060 struct OP_Savepoint_stack_vars {
51061 int p1; /* Value of P1 operand */
51062 char *zName; /* Name of savepoint */
51063 int nName;
51064 Savepoint *pNew;
51065 Savepoint *pSavepoint;
51066 Savepoint *pTmp;
51067 int iSavepoint;
51068 int ii;
51069 } ar;
51070 struct OP_AutoCommit_stack_vars {
51071 int desiredAutoCommit;
51072 int iRollback;
51073 int turnOnAC;
51074 } as;
51075 struct OP_Transaction_stack_vars {
 
51076 Btree *pBt;
51077 } at;
51078 struct OP_ReadCookie_stack_vars {
51079 int iMeta;
51080 int iDb;
51081 int iCookie;
51082 } au;
51083 struct OP_SetCookie_stack_vars {
51084 Db *pDb;
51085 } av;
51086 struct OP_VerifyCookie_stack_vars {
51087 int iMeta;
51088 Btree *pBt;
51089 } aw;
51090 struct OP_OpenWrite_stack_vars {
51091 int nField;
51092 KeyInfo *pKeyInfo;
 
51093 int p2;
51094 int iDb;
51095 int wrFlag;
51096 Btree *pX;
51097 VdbeCursor *pCur;
51098 Db *pDb;
51099 int flags;
51100 } ax;
51101 struct OP_OpenEphemeral_stack_vars {
51102 VdbeCursor *pCx;
51103 } ay;
51104 struct OP_OpenPseudo_stack_vars {
51105 VdbeCursor *pCx;
51106 } az;
 
 
 
 
 
 
 
51107 struct OP_SeekGt_stack_vars {
 
51108 int res;
51109 int oc;
51110 VdbeCursor *pC;
51111 UnpackedRecord r;
51112 int nField;
51113 i64 iKey; /* The rowid we are to seek to */
51114 } ba;
51115 struct OP_Seek_stack_vars {
 
51116 VdbeCursor *pC;
51117 } bb;
51118 struct OP_Found_stack_vars {
 
51119 int alreadyExists;
51120 VdbeCursor *pC;
51121 int res;
51122 UnpackedRecord *pIdxKey;
51123 char aTempRec[ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*3 + 7];
51124 } bc;
51125 struct OP_IsUnique_stack_vars {
51126 u16 ii;
51127 VdbeCursor *pCx;
51128 BtCursor *pCrsr;
51129 u16 nField;
51130 Mem *aMem;
51131 UnpackedRecord r; /* B-Tree index search key */
51132 i64 R; /* Rowid stored in register P3 */
51133 } bd;
51134 struct OP_NotExists_stack_vars {
 
51135 VdbeCursor *pC;
51136 BtCursor *pCrsr;
51137 int res;
51138 u64 iKey;
51139 } be;
51140 struct OP_NewRowid_stack_vars {
51141 i64 v; /* The new rowid */
51142 VdbeCursor *pC; /* Cursor of table to get the new rowid */
51143 int res; /* Result of an sqlite3BtreeLast() */
51144 int cnt; /* Counter to limit the number of searches */
51145 Mem *pMem; /* Register holding largest rowid for AUTOINCREMENT */
51146 } bf;
 
 
 
51147 struct OP_Insert_stack_vars {
51148 Mem *pData;
51149 Mem *pKey;
51150 i64 iKey; /* The integer ROWID or key for the record to be inserted */
 
51151 VdbeCursor *pC;
51152 int nZero;
51153 int seekResult;
51154 const char *zDb;
51155 const char *zTbl;
51156 int op;
51157 } bg;
51158 struct OP_Delete_stack_vars {
 
51159 i64 iKey;
51160 VdbeCursor *pC;
51161 } bh;
51162 struct OP_RowData_stack_vars {
 
51163 VdbeCursor *pC;
51164 BtCursor *pCrsr;
51165 u32 n;
51166 i64 n64;
51167 } bi;
51168 struct OP_Rowid_stack_vars {
 
51169 VdbeCursor *pC;
51170 i64 v;
51171 sqlite3_vtab *pVtab;
51172 const sqlite3_module *pModule;
51173 } bj;
51174 struct OP_NullRow_stack_vars {
 
51175 VdbeCursor *pC;
51176 } bk;
51177 struct OP_Last_stack_vars {
51178 VdbeCursor *pC;
51179 BtCursor *pCrsr;
51180 int res;
51181 } bl;
51182 struct OP_Rewind_stack_vars {
51183 VdbeCursor *pC;
51184 BtCursor *pCrsr;
51185 int res;
51186 } bm;
51187 struct OP_Next_stack_vars {
51188 VdbeCursor *pC;
51189 BtCursor *pCrsr;
51190 int res;
51191 } bn;
 
 
 
 
 
 
 
 
 
 
 
51192 struct OP_IdxInsert_stack_vars {
 
51193 VdbeCursor *pC;
51194 BtCursor *pCrsr;
51195 int nKey;
51196 const char *zKey;
51197 } bo;
51198 struct OP_IdxDelete_stack_vars {
 
51199 VdbeCursor *pC;
51200 BtCursor *pCrsr;
51201 int res;
51202 UnpackedRecord r;
51203 } bp;
51204 struct OP_IdxRowid_stack_vars {
 
51205 BtCursor *pCrsr;
51206 VdbeCursor *pC;
51207 i64 rowid;
51208 } bq;
51209 struct OP_IdxGE_stack_vars {
 
51210 VdbeCursor *pC;
51211 int res;
51212 UnpackedRecord r;
51213 } br;
51214 struct OP_Destroy_stack_vars {
51215 int iMoved;
51216 int iCnt;
51217 Vdbe *pVdbe;
51218 int iDb;
51219 } bs;
51220 struct OP_Clear_stack_vars {
51221 int nChange;
51222 } bt;
51223 struct OP_CreateTable_stack_vars {
51224 int pgno;
51225 int flags;
51226 Db *pDb;
51227 } bu;
51228 struct OP_ParseSchema_stack_vars {
51229 int iDb;
51230 const char *zMaster;
51231 char *zSql;
51232 InitData initData;
51233 } bv;
51234 struct OP_IntegrityCk_stack_vars {
51235 int nRoot; /* Number of tables to check. (Number of root pages.) */
51236 int *aRoot; /* Array of rootpage numbers for tables to be checked */
51237 int j; /* Loop counter */
51238 int nErr; /* Number of errors reported */
51239 char *z; /* Text of the error report */
51240 Mem *pnErr; /* Register keeping track of errors remaining */
51241 } bw;
51242 struct OP_RowSetAdd_stack_vars {
51243 Mem *pIdx;
51244 Mem *pVal;
51245 } bx;
51246 struct OP_RowSetRead_stack_vars {
51247 Mem *pIdx;
51248 i64 val;
51249 } by;
51250 struct OP_RowSetTest_stack_vars {
51251 int iSet;
51252 int exists;
51253 } bz;
51254 struct OP_ContextPush_stack_vars {
51255 int i;
51256 Context *pContext;
51257 } ca;
51258 struct OP_ContextPop_stack_vars {
51259 Context *pContext;
51260 } cb;
51261 struct OP_AggStep_stack_vars {
51262 int n;
51263 int i;
51264 Mem *pMem;
51265 Mem *pRec;
51266 sqlite3_context ctx;
51267 sqlite3_value **apVal;
51268 } cc;
51269 struct OP_AggFinal_stack_vars {
51270 Mem *pMem;
51271 } cd;
51272 struct OP_IncrVacuum_stack_vars {
51273 Btree *pBt;
51274 } ce;
51275 struct OP_TableLock_stack_vars {
51276 int p1;
51277 u8 isWriteLock;
51278 } cf;
51279 struct OP_VBegin_stack_vars {
51280 sqlite3_vtab *pVtab;
51281 } cg;
51282 struct OP_VOpen_stack_vars {
51283 VdbeCursor *pCur;
51284 sqlite3_vtab_cursor *pVtabCursor;
51285 sqlite3_vtab *pVtab;
51286 sqlite3_module *pModule;
51287 } ch;
51288 struct OP_VFilter_stack_vars {
51289 int nArg;
51290 int iQuery;
51291 const sqlite3_module *pModule;
51292 Mem *pQuery;
@@ -51352,44 +51295,44 @@
51295 sqlite3_vtab *pVtab;
51296 VdbeCursor *pCur;
51297 int res;
51298 int i;
51299 Mem **apArg;
51300 } ci;
51301 struct OP_VColumn_stack_vars {
51302 sqlite3_vtab *pVtab;
51303 const sqlite3_module *pModule;
51304 Mem *pDest;
51305 sqlite3_context sContext;
51306 } cj;
51307 struct OP_VNext_stack_vars {
51308 sqlite3_vtab *pVtab;
51309 const sqlite3_module *pModule;
51310 int res;
51311 VdbeCursor *pCur;
51312 } ck;
51313 struct OP_VRename_stack_vars {
51314 sqlite3_vtab *pVtab;
51315 Mem *pName;
51316 } cl;
51317 struct OP_VUpdate_stack_vars {
51318 sqlite3_vtab *pVtab;
51319 sqlite3_module *pModule;
51320 int nArg;
51321 int i;
51322 sqlite_int64 rowid;
51323 Mem **apArg;
51324 Mem *pX;
51325 } cm;
51326 struct OP_Pagecount_stack_vars {
51327 int p1;
51328 int nPage;
51329 Pager *pPager;
51330 } cn;
51331 struct OP_Trace_stack_vars {
51332 char *zTrace;
51333 } co;
51334 } u;
51335 /* End automatically generated code
51336 ********************************************************************/
51337
51338 assert( p->magic==VDBE_MAGIC_RUN ); /* sqlite3_step() verifies this */
@@ -51732,27 +51675,24 @@
51675 pOp->opcode = OP_String;
51676 pOp->p1 = sqlite3Strlen30(pOp->p4.z);
51677
51678 #ifndef SQLITE_OMIT_UTF16
51679 if( encoding!=SQLITE_UTF8 ){
51680 rc = sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC);
51681 if( rc==SQLITE_TOOBIG ) goto too_big;
51682 if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pOut, encoding) ) goto no_mem;
51683 assert( pOut->zMalloc==pOut->z );
51684 assert( pOut->flags & MEM_Dyn );
51685 pOut->zMalloc = 0;
51686 pOut->flags |= MEM_Static;
51687 pOut->flags &= ~MEM_Dyn;
51688 if( pOp->p4type==P4_DYNAMIC ){
51689 sqlite3DbFree(db, pOp->p4.z);
51690 }
51691 pOp->p4type = P4_DYNAMIC;
51692 pOp->p4.z = pOut->z;
51693 pOp->p1 = pOut->n;
 
 
 
 
 
51694 }
51695 #endif
51696 if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
51697 goto too_big;
51698 }
@@ -51940,13 +51880,17 @@
51880 ** opened by this VM before returning control to the user. This is to
51881 ** ensure that statement-transactions are always nested, not overlapping.
51882 ** If the open statement-transaction is not closed here, then the user
51883 ** may step another VM that opens its own statement transaction. This
51884 ** may lead to overlapping statement transactions.
51885 **
51886 ** The statement transaction is never a top-level transaction. Hence
51887 ** the RELEASE call below can never fail.
51888 */
51889 assert( p->iStatement==0 || db->flags&SQLITE_CountRows );
51890 rc = sqlite3VdbeCloseStatement(p, SAVEPOINT_RELEASE);
51891 if( NEVER(rc!=SQLITE_OK) ){
51892 break;
51893 }
51894
51895 /* Invalidate all ephemeral cursor row caches */
51896 p->cacheCtr = (p->cacheCtr + 2)|1;
@@ -51990,13 +51934,12 @@
51934 assert( pIn1!=pOut );
51935 if( (pIn1->flags | pIn2->flags) & MEM_Null ){
51936 sqlite3VdbeMemSetNull(pOut);
51937 break;
51938 }
51939 if( ExpandBlob(pIn1) || ExpandBlob(pIn2) ) goto no_mem;
51940 Stringify(pIn1, encoding);
 
51941 Stringify(pIn2, encoding);
51942 u.ae.nByte = pIn1->n + pIn2->n;
51943 if( u.ae.nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
51944 goto too_big;
51945 }
@@ -52108,12 +52051,12 @@
52051 if( u.af.rA==(double)0 ) goto arithmetic_result_is_null;
52052 u.af.rB /= u.af.rA;
52053 break;
52054 }
52055 default: {
52056 u.af.iA = (i64)u.af.rA;
52057 u.af.iB = (i64)u.af.rB;
52058 if( u.af.iA==0 ) goto arithmetic_result_is_null;
52059 if( u.af.iA==-1 ) u.af.iA = 1;
52060 u.af.rB = (double)(u.af.iB % u.af.iA);
52061 break;
52062 }
@@ -52781,30 +52724,18 @@
52724 pc = pOp->p2-1;
52725 }
52726 break;
52727 }
52728
52729 /* Opcode: IsNull P1 P2 * * *
52730 **
52731 ** Jump to P2 if the value in register P1 is NULL.
 
 
52732 */
52733 case OP_IsNull: { /* same as TK_ISNULL, jump, in1 */
52734 if( (pIn1->flags & MEM_Null)!=0 ){
52735 pc = pOp->p2 - 1;
52736 }
 
 
 
 
 
 
 
 
 
 
52737 break;
52738 }
52739
52740 /* Opcode: NotNull P1 P2 * * *
52741 **
@@ -52852,11 +52783,11 @@
52783 ** If the column contains fewer than P2 fields, then extract a NULL. Or,
52784 ** if the P4 argument is a P4_MEM use the value of the P4 argument as
52785 ** the result.
52786 */
52787 case OP_Column: {
52788 #if 0 /* local variables moved into u.am */
52789 u32 payloadSize; /* Number of bytes in the record */
52790 i64 payloadSize64; /* Number of bytes in the record */
52791 int p1; /* P1 value of the opcode */
52792 int p2; /* column number to retrieve */
52793 VdbeCursor *pC; /* The VDBE cursor */
@@ -52874,123 +52805,125 @@
52805 u8 *zEndHdr; /* Pointer to first byte after the header */
52806 u32 offset; /* Offset into the data */
52807 u64 offset64; /* 64-bit offset. 64 bits needed to catch overflow */
52808 int szHdr; /* Size of the header size field at start of record */
52809 int avail; /* Number of bytes of available data */
52810 #endif /* local variables moved into u.am */
52811
52812
52813 u.am.p1 = pOp->p1;
52814 u.am.p2 = pOp->p2;
52815 u.am.pC = 0;
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.
52826 ** The complete record text is always available for pseudo-tables
52827 ** If the record is stored in a cursor, the complete record text
52828 ** might be available in the u.am.pC->aRow cache. Or it might not be.
52829 ** If the data is unavailable, u.am.zRec is set to NULL.
52830 **
52831 ** We also compute the number of columns in the record. For cursors,
52832 ** the number of columns is stored in the VdbeCursor.nField element.
52833 */
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;
52850 }else if( u.am.pC->isIndex ){
52851 sqlite3BtreeKeySize(u.am.pCrsr, &u.am.payloadSize64);
52852 /* sqlite3BtreeParseCellPtr() uses getVarint32() to extract the
52853 ** payload size, so it is impossible for u.am.payloadSize64 to be
52854 ** larger than 32 bits. */
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
52874 /* If u.am.payloadSize is 0, then just store a NULL */
52875 if( u.am.payloadSize==0 ){
52876 assert( u.am.pDest->flags&MEM_Null );
52877 goto op_column_out;
52878 }
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 */
52889 u.am.aType = u.am.pC->aType;
52890 if( u.am.pC->cacheStatus==p->cacheCtr ){
52891 u.am.aOffset = u.am.pC->aOffset;
52892 }else{
52893 assert(u.am.aType);
52894 u.am.avail = 0;
52895 u.am.pC->aOffset = u.am.aOffset = &u.am.aType[u.am.nField];
52896 u.am.pC->payloadSize = u.am.payloadSize;
52897 u.am.pC->cacheStatus = p->cacheCtr;
52898
52899 /* Figure out how many bytes are in the header */
52900 if( u.am.zRec ){
52901 u.am.zData = u.am.zRec;
52902 }else{
52903 if( u.am.pC->isIndex ){
52904 u.am.zData = (char*)sqlite3BtreeKeyFetch(u.am.pCrsr, &u.am.avail);
52905 }else{
52906 u.am.zData = (char*)sqlite3BtreeDataFetch(u.am.pCrsr, &u.am.avail);
52907 }
52908 /* If KeyFetch()/DataFetch() managed to get the entire payload,
52909 ** save the payload in the u.am.pC->aRow cache. That will save us from
52910 ** having to make additional calls to fetch the content portion of
52911 ** the record.
52912 */
52913 assert( u.am.avail>=0 );
52914 if( u.am.payloadSize <= (u32)u.am.avail ){
52915 u.am.zRec = u.am.zData;
52916 u.am.pC->aRow = (u8*)u.am.zData;
52917 }else{
52918 u.am.pC->aRow = 0;
52919 }
52920 }
52921 /* The following assert is true in all cases accept when
52922 ** the database file has been corrupted externally.
52923 ** assert( u.am.zRec!=0 || u.am.avail>=u.am.payloadSize || u.am.avail>=9 ); */
52924 u.am.szHdr = getVarint32((u8*)u.am.zData, u.am.offset);
52925
52926 /* Make sure a corrupt database has not given us an oversize header.
52927 ** Do this now to avoid an oversize memory allocation.
52928 **
52929 ** Type entries can be between 1 and 5 bytes each. But 4 and 5 byte
@@ -52997,136 +52930,136 @@
52930 ** types use so much data space that there can only be 4096 and 32 of
52931 ** them, respectively. So the maximum header length results from a
52932 ** 3-byte type for each of the maximum of 32768 columns plus three
52933 ** extra bytes for the header length itself. 32768*3 + 3 = 98307.
52934 */
52935 if( u.am.offset > 98307 ){
52936 rc = SQLITE_CORRUPT_BKPT;
52937 goto op_column_out;
52938 }
52939
52940 /* Compute in u.am.len the number of bytes of data we need to read in order
52941 ** to get u.am.nField type values. u.am.offset is an upper bound on this. But
52942 ** u.am.nField might be significantly less than the true number of columns
52943 ** in the table, and in that case, 5*u.am.nField+3 might be smaller than u.am.offset.
52944 ** We want to minimize u.am.len in order to limit the size of the memory
52945 ** allocation, especially if a corrupt database file has caused u.am.offset
52946 ** to be oversized. Offset is limited to 98307 above. But 98307 might
52947 ** still exceed Robson memory allocation limits on some configurations.
52948 ** On systems that cannot tolerate large memory allocations, u.am.nField*5+3
52949 ** will likely be much smaller since u.am.nField will likely be less than
52950 ** 20 or so. This insures that Robson memory allocation limits are
52951 ** not exceeded even for corrupt database files.
52952 */
52953 u.am.len = u.am.nField*5 + 3;
52954 if( u.am.len > (int)u.am.offset ) u.am.len = (int)u.am.offset;
52955
52956 /* The KeyFetch() or DataFetch() above are fast and will get the entire
52957 ** record header in most cases. But they will fail to get the complete
52958 ** record header if the record header does not fit on a single page
52959 ** in the B-Tree. When that happens, use sqlite3VdbeMemFromBtree() to
52960 ** acquire the complete header text.
52961 */
52962 if( !u.am.zRec && u.am.avail<u.am.len ){
52963 u.am.sMem.flags = 0;
52964 u.am.sMem.db = 0;
52965 rc = sqlite3VdbeMemFromBtree(u.am.pCrsr, 0, u.am.len, u.am.pC->isIndex, &u.am.sMem);
52966 if( rc!=SQLITE_OK ){
52967 goto op_column_out;
52968 }
52969 u.am.zData = u.am.sMem.z;
52970 }
52971 u.am.zEndHdr = (u8 *)&u.am.zData[u.am.len];
52972 u.am.zIdx = (u8 *)&u.am.zData[u.am.szHdr];
52973
52974 /* Scan the header and use it to fill in the u.am.aType[] and u.am.aOffset[]
52975 ** arrays. u.am.aType[u.am.i] will contain the type integer for the u.am.i-th
52976 ** column and u.am.aOffset[u.am.i] will contain the u.am.offset from the beginning
52977 ** of the record to the start of the data for the u.am.i-th column
52978 */
52979 u.am.offset64 = u.am.offset;
52980 for(u.am.i=0; u.am.i<u.am.nField; u.am.i++){
52981 if( u.am.zIdx<u.am.zEndHdr ){
52982 u.am.aOffset[u.am.i] = (u32)u.am.offset64;
52983 u.am.zIdx += getVarint32(u.am.zIdx, u.am.aType[u.am.i]);
52984 u.am.offset64 += sqlite3VdbeSerialTypeLen(u.am.aType[u.am.i]);
52985 }else{
52986 /* If u.am.i is less that u.am.nField, then there are less fields in this
52987 ** record than SetNumColumns indicated there are columns in the
52988 ** table. Set the u.am.offset for any extra columns not present in
52989 ** the record to 0. This tells code below to store a NULL
52990 ** instead of deserializing a value from the record.
52991 */
52992 u.am.aOffset[u.am.i] = 0;
52993 }
52994 }
52995 sqlite3VdbeMemRelease(&u.am.sMem);
52996 u.am.sMem.flags = MEM_Null;
52997
52998 /* If we have read more header data than was contained in the header,
52999 ** or if the end of the last field appears to be past the end of the
53000 ** record, or if the end of the last field appears to be before the end
53001 ** of the record (when all fields present), then we must be dealing
53002 ** with a corrupt database.
53003 */
53004 if( (u.am.zIdx > u.am.zEndHdr)|| (u.am.offset64 > u.am.payloadSize)
53005 || (u.am.zIdx==u.am.zEndHdr && u.am.offset64!=(u64)u.am.payloadSize) ){
53006 rc = SQLITE_CORRUPT_BKPT;
53007 goto op_column_out;
53008 }
53009 }
53010
53011 /* Get the column information. If u.am.aOffset[u.am.p2] is non-zero, then
53012 ** deserialize the value from the record. If u.am.aOffset[u.am.p2] is zero,
53013 ** then there are not enough fields in the record to satisfy the
53014 ** request. In this case, set the value NULL or to P4 if P4 is
53015 ** a pointer to a Mem object.
53016 */
53017 if( u.am.aOffset[u.am.p2] ){
53018 assert( rc==SQLITE_OK );
53019 if( u.am.zRec ){
53020 sqlite3VdbeMemReleaseExternal(u.am.pDest);
53021 sqlite3VdbeSerialGet((u8 *)&u.am.zRec[u.am.aOffset[u.am.p2]], u.am.aType[u.am.p2], u.am.pDest);
53022 }else{
53023 u.am.len = sqlite3VdbeSerialTypeLen(u.am.aType[u.am.p2]);
53024 sqlite3VdbeMemMove(&u.am.sMem, u.am.pDest);
53025 rc = sqlite3VdbeMemFromBtree(u.am.pCrsr, u.am.aOffset[u.am.p2], u.am.len, u.am.pC->isIndex, &u.am.sMem);
53026 if( rc!=SQLITE_OK ){
53027 goto op_column_out;
53028 }
53029 u.am.zData = u.am.sMem.z;
53030 sqlite3VdbeSerialGet((u8*)u.am.zData, u.am.aType[u.am.p2], u.am.pDest);
53031 }
53032 u.am.pDest->enc = encoding;
53033 }else{
53034 if( pOp->p4type==P4_MEM ){
53035 sqlite3VdbeMemShallowCopy(u.am.pDest, pOp->p4.pMem, MEM_Static);
53036 }else{
53037 assert( u.am.pDest->flags&MEM_Null );
53038 }
53039 }
53040
53041 /* If we dynamically allocated space to hold the data (in the
53042 ** sqlite3VdbeMemFromBtree() call above) then transfer control of that
53043 ** dynamically allocated space over to the u.am.pDest structure.
53044 ** This prevents a memory copy.
53045 */
53046 if( u.am.sMem.zMalloc ){
53047 assert( u.am.sMem.z==u.am.sMem.zMalloc );
53048 assert( !(u.am.pDest->flags & MEM_Dyn) );
53049 assert( !(u.am.pDest->flags & (MEM_Blob|MEM_Str)) || u.am.pDest->z==u.am.sMem.z );
53050 u.am.pDest->flags &= ~(MEM_Ephem|MEM_Static);
53051 u.am.pDest->flags |= MEM_Term;
53052 u.am.pDest->z = u.am.sMem.z;
53053 u.am.pDest->zMalloc = u.am.sMem.zMalloc;
53054 }
53055
53056 rc = sqlite3VdbeMemMakeWriteable(u.am.pDest);
53057
53058 op_column_out:
53059 UPDATE_MAX_BLOBSIZE(u.am.pDest);
53060 REGISTER_TRACE(pOp->p3, u.am.pDest);
53061 break;
53062 }
53063
53064 /* Opcode: Affinity P1 P2 * P4 *
53065 **
@@ -53135,23 +53068,23 @@
53068 ** P4 is a string that is P2 characters long. The nth character of the
53069 ** string indicates the column affinity that should be used for the nth
53070 ** memory cell in the range.
53071 */
53072 case OP_Affinity: {
53073 #if 0 /* local variables moved into u.an */
53074 char *zAffinity; /* The affinity to be applied */
53075 Mem *pData0; /* First register to which to apply affinity */
53076 Mem *pLast; /* Last register to which to apply affinity */
53077 Mem *pRec; /* Current register */
53078 #endif /* local variables moved into u.an */
53079
53080 u.an.zAffinity = pOp->p4.z;
53081 u.an.pData0 = &p->aMem[pOp->p1];
53082 u.an.pLast = &u.an.pData0[pOp->p2-1];
53083 for(u.an.pRec=u.an.pData0; u.an.pRec<=u.an.pLast; u.an.pRec++){
53084 ExpandBlob(u.an.pRec);
53085 applyAffinity(u.an.pRec, u.an.zAffinity[u.an.pRec-u.an.pData0], encoding);
53086 }
53087 break;
53088 }
53089
53090 /* Opcode: MakeRecord P1 P2 P3 P4 *
@@ -53171,11 +53104,11 @@
53104 ** macros defined in sqliteInt.h.
53105 **
53106 ** If P4 is NULL then all index fields have the affinity NONE.
53107 */
53108 case OP_MakeRecord: {
53109 #if 0 /* local variables moved into u.ao */
53110 u8 *zNewRecord; /* A buffer to hold the data for the new record */
53111 Mem *pRec; /* The new record */
53112 u64 nData; /* Number of bytes of data space */
53113 int nHdr; /* Number of bytes of header space */
53114 i64 nByte; /* Data space required for this record */
@@ -53187,11 +53120,11 @@
53120 int nField; /* Number of fields in the record */
53121 char *zAffinity; /* The affinity string for the record */
53122 int file_format; /* File format to use for encoding */
53123 int i; /* Space used in zNewRecord[] */
53124 int len; /* Length of a field */
53125 #endif /* local variables moved into u.ao */
53126
53127 /* Assuming the record contains N fields, the record format looks
53128 ** like this:
53129 **
53130 ** ------------------------------------------------------------------------
@@ -53204,52 +53137,52 @@
53137 ** Each type field is a varint representing the serial type of the
53138 ** corresponding data element (see sqlite3VdbeSerialType()). The
53139 ** hdr-size field is also a varint which is the offset from the beginning
53140 ** of the record to data0.
53141 */
53142 u.ao.nData = 0; /* Number of bytes of data space */
53143 u.ao.nHdr = 0; /* Number of bytes of header space */
53144 u.ao.nByte = 0; /* Data space required for this record */
53145 u.ao.nZero = 0; /* Number of zero bytes at the end of the record */
53146 u.ao.nField = pOp->p1;
53147 u.ao.zAffinity = pOp->p4.z;
53148 assert( u.ao.nField>0 && pOp->p2>0 && pOp->p2+u.ao.nField<=p->nMem+1 );
53149 u.ao.pData0 = &p->aMem[u.ao.nField];
53150 u.ao.nField = pOp->p2;
53151 u.ao.pLast = &u.ao.pData0[u.ao.nField-1];
53152 u.ao.file_format = p->minWriteFileFormat;
53153
53154 /* Loop through the elements that will make up the record to figure
53155 ** out how much space is required for the new record.
53156 */
53157 for(u.ao.pRec=u.ao.pData0; u.ao.pRec<=u.ao.pLast; u.ao.pRec++){
53158 if( u.ao.zAffinity ){
53159 applyAffinity(u.ao.pRec, u.ao.zAffinity[u.ao.pRec-u.ao.pData0], encoding);
53160 }
53161 if( u.ao.pRec->flags&MEM_Zero && u.ao.pRec->n>0 ){
53162 sqlite3VdbeMemExpandBlob(u.ao.pRec);
53163 }
53164 u.ao.serial_type = sqlite3VdbeSerialType(u.ao.pRec, u.ao.file_format);
53165 u.ao.len = sqlite3VdbeSerialTypeLen(u.ao.serial_type);
53166 u.ao.nData += u.ao.len;
53167 u.ao.nHdr += sqlite3VarintLen(u.ao.serial_type);
53168 if( u.ao.pRec->flags & MEM_Zero ){
53169 /* Only pure zero-filled BLOBs can be input to this Opcode.
53170 ** We do not allow blobs with a prefix and a zero-filled tail. */
53171 u.ao.nZero += u.ao.pRec->u.nZero;
53172 }else if( u.ao.len ){
53173 u.ao.nZero = 0;
53174 }
53175 }
53176
53177 /* Add the initial header varint and total the size */
53178 u.ao.nHdr += u.ao.nVarint = sqlite3VarintLen(u.ao.nHdr);
53179 if( u.ao.nVarint<sqlite3VarintLen(u.ao.nHdr) ){
53180 u.ao.nHdr++;
53181 }
53182 u.ao.nByte = u.ao.nHdr+u.ao.nData-u.ao.nZero;
53183 if( u.ao.nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
53184 goto too_big;
53185 }
53186
53187 /* Make sure the output register has a buffer large enough to store
53188 ** the new record. The output register (pOp->p3) is not allowed to
@@ -53256,32 +53189,32 @@
53189 ** be one of the input registers (because the following call to
53190 ** sqlite3VdbeMemGrow() could clobber the value before it is used).
53191 */
53192 assert( pOp->p3<pOp->p1 || pOp->p3>=pOp->p1+pOp->p2 );
53193 pOut = &p->aMem[pOp->p3];
53194 if( sqlite3VdbeMemGrow(pOut, (int)u.ao.nByte, 0) ){
53195 goto no_mem;
53196 }
53197 u.ao.zNewRecord = (u8 *)pOut->z;
53198
53199 /* Write the record */
53200 u.ao.i = putVarint32(u.ao.zNewRecord, u.ao.nHdr);
53201 for(u.ao.pRec=u.ao.pData0; u.ao.pRec<=u.ao.pLast; u.ao.pRec++){
53202 u.ao.serial_type = sqlite3VdbeSerialType(u.ao.pRec, u.ao.file_format);
53203 u.ao.i += putVarint32(&u.ao.zNewRecord[u.ao.i], u.ao.serial_type); /* serial type */
53204 }
53205 for(u.ao.pRec=u.ao.pData0; u.ao.pRec<=u.ao.pLast; u.ao.pRec++){ /* serial data */
53206 u.ao.i += sqlite3VdbeSerialPut(&u.ao.zNewRecord[u.ao.i], (int)(u.ao.nByte-u.ao.i), u.ao.pRec,u.ao.file_format);
53207 }
53208 assert( u.ao.i==u.ao.nByte );
53209
53210 assert( pOp->p3>0 && pOp->p3<=p->nMem );
53211 pOut->n = (int)u.ao.nByte;
53212 pOut->flags = MEM_Blob | MEM_Dyn;
53213 pOut->xDel = 0;
53214 if( u.ao.nZero ){
53215 pOut->u.nZero = u.ao.nZero;
53216 pOut->flags |= MEM_Zero;
53217 }
53218 pOut->enc = SQLITE_UTF8; /* In case the blob is ever converted to text */
53219 REGISTER_TRACE(pOp->p3, pOut);
53220 UPDATE_MAX_BLOBSIZE(pOut);
@@ -53293,23 +53226,23 @@
53226 ** Store the number of entries (an integer value) in the table or index
53227 ** opened by cursor P1 in register P2
53228 */
53229 #ifndef SQLITE_OMIT_BTREECOUNT
53230 case OP_Count: { /* out2-prerelease */
53231 #if 0 /* local variables moved into u.ap */
53232 i64 nEntry;
53233 BtCursor *pCrsr;
53234 #endif /* local variables moved into u.ap */
53235
53236 u.ap.pCrsr = p->apCsr[pOp->p1]->pCursor;
53237 if( u.ap.pCrsr ){
53238 rc = sqlite3BtreeCount(u.ap.pCrsr, &u.ap.nEntry);
53239 }else{
53240 u.ap.nEntry = 0;
53241 }
53242 pOut->flags = MEM_Int;
53243 pOut->u.i = u.ap.nEntry;
53244 break;
53245 }
53246 #endif
53247
53248 /* Opcode: Statement P1 * * * *
@@ -53333,27 +53266,25 @@
53266 ** The statement is begun on the database file with index P1. The main
53267 ** database file has an index of 0 and the file used for temporary tables
53268 ** has an index of 1.
53269 */
53270 case OP_Statement: {
53271 #if 0 /* local variables moved into u.aq */
 
53272 Btree *pBt;
53273 #endif /* local variables moved into u.aq */
53274 if( db->autoCommit==0 || db->activeVdbeCnt>1 ){
53275 assert( pOp->p1>=0 && pOp->p1<db->nDb );
53276 assert( db->aDb[pOp->p1].pBt!=0 );
53277 u.aq.pBt = db->aDb[pOp->p1].pBt;
53278 assert( sqlite3BtreeIsInTrans(u.aq.pBt) );
53279 assert( (p->btreeMask & (1<<pOp->p1))!=0 );
 
53280 if( p->iStatement==0 ){
53281 assert( db->nStatement>=0 && db->nSavepoint>=0 );
53282 db->nStatement++;
53283 p->iStatement = db->nSavepoint + db->nStatement;
53284 }
53285 rc = sqlite3BtreeBeginStmt(u.aq.pBt, p->iStatement);
53286 }
53287 break;
53288 }
53289
53290 /* Opcode: Savepoint P1 * * P4 *
@@ -53361,48 +53292,48 @@
53292 ** Open, release or rollback the savepoint named by parameter P4, depending
53293 ** on the value of P1. To open a new savepoint, P1==0. To release (commit) an
53294 ** existing savepoint, P1==1, or to rollback an existing savepoint P1==2.
53295 */
53296 case OP_Savepoint: {
53297 #if 0 /* local variables moved into u.ar */
53298 int p1; /* Value of P1 operand */
53299 char *zName; /* Name of savepoint */
53300 int nName;
53301 Savepoint *pNew;
53302 Savepoint *pSavepoint;
53303 Savepoint *pTmp;
53304 int iSavepoint;
53305 int ii;
53306 #endif /* local variables moved into u.ar */
53307
53308 u.ar.p1 = pOp->p1;
53309 u.ar.zName = pOp->p4.z;
53310
53311 /* Assert that the u.ar.p1 parameter is valid. Also that if there is no open
53312 ** transaction, then there cannot be any savepoints.
53313 */
53314 assert( db->pSavepoint==0 || db->autoCommit==0 );
53315 assert( u.ar.p1==SAVEPOINT_BEGIN||u.ar.p1==SAVEPOINT_RELEASE||u.ar.p1==SAVEPOINT_ROLLBACK );
53316 assert( db->pSavepoint || db->isTransactionSavepoint==0 );
53317 assert( checkSavepointCount(db) );
53318
53319 if( u.ar.p1==SAVEPOINT_BEGIN ){
53320 if( db->writeVdbeCnt>0 ){
53321 /* A new savepoint cannot be created if there are active write
53322 ** statements (i.e. open read/write incremental blob handles).
53323 */
53324 sqlite3SetString(&p->zErrMsg, db, "cannot open savepoint - "
53325 "SQL statements in progress");
53326 rc = SQLITE_BUSY;
53327 }else{
53328 u.ar.nName = sqlite3Strlen30(u.ar.zName);
53329
53330 /* Create a new savepoint structure. */
53331 u.ar.pNew = sqlite3DbMallocRaw(db, sizeof(Savepoint)+u.ar.nName+1);
53332 if( u.ar.pNew ){
53333 u.ar.pNew->zName = (char *)&u.ar.pNew[1];
53334 memcpy(u.ar.pNew->zName, u.ar.zName, u.ar.nName+1);
53335
53336 /* If there is no open transaction, then mark this as a special
53337 ** "transaction savepoint". */
53338 if( db->autoCommit ){
53339 db->autoCommit = 0;
@@ -53410,49 +53341,49 @@
53341 }else{
53342 db->nSavepoint++;
53343 }
53344
53345 /* Link the new savepoint into the database handle's list. */
53346 u.ar.pNew->pNext = db->pSavepoint;
53347 db->pSavepoint = u.ar.pNew;
53348 }
53349 }
53350 }else{
53351 u.ar.iSavepoint = 0;
53352
53353 /* Find the named savepoint. If there is no such savepoint, then an
53354 ** an error is returned to the user. */
53355 for(
53356 u.ar.pSavepoint = db->pSavepoint;
53357 u.ar.pSavepoint && sqlite3StrICmp(u.ar.pSavepoint->zName, u.ar.zName);
53358 u.ar.pSavepoint = u.ar.pSavepoint->pNext
53359 ){
53360 u.ar.iSavepoint++;
53361 }
53362 if( !u.ar.pSavepoint ){
53363 sqlite3SetString(&p->zErrMsg, db, "no such savepoint: %s", u.ar.zName);
53364 rc = SQLITE_ERROR;
53365 }else if(
53366 db->writeVdbeCnt>0 || (u.ar.p1==SAVEPOINT_ROLLBACK && db->activeVdbeCnt>1)
53367 ){
53368 /* It is not possible to release (commit) a savepoint if there are
53369 ** active write statements. It is not possible to rollback a savepoint
53370 ** if there are any active statements at all.
53371 */
53372 sqlite3SetString(&p->zErrMsg, db,
53373 "cannot %s savepoint - SQL statements in progress",
53374 (u.ar.p1==SAVEPOINT_ROLLBACK ? "rollback": "release")
53375 );
53376 rc = SQLITE_BUSY;
53377 }else{
53378
53379 /* Determine whether or not this is a transaction savepoint. If so,
53380 ** and this is a RELEASE command, then the current transaction
53381 ** is committed.
53382 */
53383 int isTransaction = u.ar.pSavepoint->pNext==0 && db->isTransactionSavepoint;
53384 if( isTransaction && u.ar.p1==SAVEPOINT_RELEASE ){
53385 db->autoCommit = 1;
53386 if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
53387 p->pc = pc;
53388 db->autoCommit = 0;
53389 p->rc = rc = SQLITE_BUSY;
@@ -53459,37 +53390,37 @@
53390 goto vdbe_return;
53391 }
53392 db->isTransactionSavepoint = 0;
53393 rc = p->rc;
53394 }else{
53395 u.ar.iSavepoint = db->nSavepoint - u.ar.iSavepoint - 1;
53396 for(u.ar.ii=0; u.ar.ii<db->nDb; u.ar.ii++){
53397 rc = sqlite3BtreeSavepoint(db->aDb[u.ar.ii].pBt, u.ar.p1, u.ar.iSavepoint);
53398 if( rc!=SQLITE_OK ){
53399 goto abort_due_to_error;
53400 }
53401 }
53402 if( u.ar.p1==SAVEPOINT_ROLLBACK && (db->flags&SQLITE_InternChanges)!=0 ){
53403 sqlite3ExpirePreparedStatements(db);
53404 sqlite3ResetInternalSchema(db, 0);
53405 }
53406 }
53407
53408 /* Regardless of whether this is a RELEASE or ROLLBACK, destroy all
53409 ** savepoints nested inside of the savepoint being operated on. */
53410 while( db->pSavepoint!=u.ar.pSavepoint ){
53411 u.ar.pTmp = db->pSavepoint;
53412 db->pSavepoint = u.ar.pTmp->pNext;
53413 sqlite3DbFree(db, u.ar.pTmp);
53414 db->nSavepoint--;
53415 }
53416
53417 /* If it is a RELEASE, then destroy the savepoint being operated on too */
53418 if( u.ar.p1==SAVEPOINT_RELEASE ){
53419 assert( u.ar.pSavepoint==db->pSavepoint );
53420 db->pSavepoint = u.ar.pSavepoint->pNext;
53421 sqlite3DbFree(db, u.ar.pSavepoint);
53422 if( !isTransaction ){
53423 db->nSavepoint--;
53424 }
53425 }
53426 }
@@ -53506,48 +53437,48 @@
53437 ** there are active writing VMs or active VMs that use shared cache.
53438 **
53439 ** This instruction causes the VM to halt.
53440 */
53441 case OP_AutoCommit: {
53442 #if 0 /* local variables moved into u.as */
53443 int desiredAutoCommit;
53444 int iRollback;
53445 int turnOnAC;
53446 #endif /* local variables moved into u.as */
53447
53448 u.as.desiredAutoCommit = pOp->p1;
53449 u.as.iRollback = pOp->p2;
53450 u.as.turnOnAC = u.as.desiredAutoCommit && !db->autoCommit;
53451 assert( u.as.desiredAutoCommit==1 || u.as.desiredAutoCommit==0 );
53452 assert( u.as.desiredAutoCommit==1 || u.as.iRollback==0 );
53453 assert( db->activeVdbeCnt>0 ); /* At least this one VM is active */
53454
53455 if( u.as.turnOnAC && u.as.iRollback && db->activeVdbeCnt>1 ){
53456 /* If this instruction implements a ROLLBACK and other VMs are
53457 ** still running, and a transaction is active, return an error indicating
53458 ** that the other VMs must complete first.
53459 */
53460 sqlite3SetString(&p->zErrMsg, db, "cannot rollback transaction - "
53461 "SQL statements in progress");
53462 rc = SQLITE_BUSY;
53463 }else if( u.as.turnOnAC && !u.as.iRollback && db->writeVdbeCnt>0 ){
53464 /* If this instruction implements a COMMIT and other VMs are writing
53465 ** return an error indicating that the other VMs must complete first.
53466 */
53467 sqlite3SetString(&p->zErrMsg, db, "cannot commit transaction - "
53468 "SQL statements in progress");
53469 rc = SQLITE_BUSY;
53470 }else if( u.as.desiredAutoCommit!=db->autoCommit ){
53471 if( u.as.iRollback ){
53472 assert( u.as.desiredAutoCommit==1 );
53473 sqlite3RollbackAll(db);
53474 db->autoCommit = 1;
53475 }else{
53476 db->autoCommit = (u8)u.as.desiredAutoCommit;
53477 if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
53478 p->pc = pc;
53479 db->autoCommit = (u8)(1-u.as.desiredAutoCommit);
53480 p->rc = rc = SQLITE_BUSY;
53481 goto vdbe_return;
53482 }
53483 }
53484 assert( db->nStatement==0 );
@@ -53558,12 +53489,12 @@
53489 rc = SQLITE_ERROR;
53490 }
53491 goto vdbe_return;
53492 }else{
53493 sqlite3SetString(&p->zErrMsg, db,
53494 (!u.as.desiredAutoCommit)?"cannot start a transaction within a transaction":(
53495 (u.as.iRollback)?"cannot rollback - no transaction is active":
53496 "cannot commit - no transaction is active"));
53497
53498 rc = SQLITE_ERROR;
53499 }
53500 break;
@@ -53589,22 +53520,20 @@
53520 ** on the file.
53521 **
53522 ** If P2 is zero, then a read-lock is obtained on the database file.
53523 */
53524 case OP_Transaction: {
53525 #if 0 /* local variables moved into u.at */
 
53526 Btree *pBt;
53527 #endif /* local variables moved into u.at */
53528
53529 assert( pOp->p1>=0 && pOp->p1<db->nDb );
53530 assert( (p->btreeMask & (1<<pOp->p1))!=0 );
53531 u.at.pBt = db->aDb[pOp->p1].pBt;
53532
53533 if( u.at.pBt ){
53534 rc = sqlite3BtreeBeginTrans(u.at.pBt, pOp->p2);
 
53535 if( rc==SQLITE_BUSY ){
53536 p->pc = pc;
53537 p->rc = rc = SQLITE_BUSY;
53538 goto vdbe_return;
53539 }
@@ -53626,25 +53555,25 @@
53555 ** There must be a read-lock on the database (either a transaction
53556 ** must be started or there must be an open cursor) before
53557 ** executing this instruction.
53558 */
53559 case OP_ReadCookie: { /* out2-prerelease */
53560 #if 0 /* local variables moved into u.au */
53561 int iMeta;
53562 int iDb;
53563 int iCookie;
53564 #endif /* local variables moved into u.au */
53565
53566 u.au.iDb = pOp->p1;
53567 u.au.iCookie = pOp->p3;
53568 assert( pOp->p3<SQLITE_N_BTREE_META );
53569 assert( u.au.iDb>=0 && u.au.iDb<db->nDb );
53570 assert( db->aDb[u.au.iDb].pBt!=0 );
53571 assert( (p->btreeMask & (1<<u.au.iDb))!=0 );
53572
53573 rc = sqlite3BtreeGetMeta(db->aDb[u.au.iDb].pBt, u.au.iCookie, (u32 *)&u.au.iMeta);
53574 pOut->u.i = u.au.iMeta;
53575 MemSetTypeFlag(pOut, MEM_Int);
53576 break;
53577 }
53578
53579 /* Opcode: SetCookie P1 P2 P3 * *
@@ -53656,28 +53585,28 @@
53585 ** database file used to store temporary tables.
53586 **
53587 ** A transaction must be started before executing this opcode.
53588 */
53589 case OP_SetCookie: { /* in3 */
53590 #if 0 /* local variables moved into u.av */
53591 Db *pDb;
53592 #endif /* local variables moved into u.av */
53593 assert( pOp->p2<SQLITE_N_BTREE_META );
53594 assert( pOp->p1>=0 && pOp->p1<db->nDb );
53595 assert( (p->btreeMask & (1<<pOp->p1))!=0 );
53596 u.av.pDb = &db->aDb[pOp->p1];
53597 assert( u.av.pDb->pBt!=0 );
53598 sqlite3VdbeMemIntegerify(pIn3);
53599 /* See note about index shifting on OP_ReadCookie */
53600 rc = sqlite3BtreeUpdateMeta(u.av.pDb->pBt, pOp->p2, (int)pIn3->u.i);
53601 if( pOp->p2==BTREE_SCHEMA_VERSION ){
53602 /* When the schema cookie changes, record the new cookie internally */
53603 u.av.pDb->pSchema->schema_cookie = (int)pIn3->u.i;
53604 db->flags |= SQLITE_InternChanges;
53605 }else if( pOp->p2==BTREE_FILE_FORMAT ){
53606 /* Record changes in the file format */
53607 u.av.pDb->pSchema->file_format = (u8)pIn3->u.i;
53608 }
53609 if( pOp->p1==1 ){
53610 /* Invalidate all prepared statements whenever the TEMP database
53611 ** schema is changed. Ticket #1644 */
53612 sqlite3ExpirePreparedStatements(db);
@@ -53700,24 +53629,24 @@
53629 ** Either a transaction needs to have been started or an OP_Open needs
53630 ** to be executed (to establish a read lock) before this opcode is
53631 ** invoked.
53632 */
53633 case OP_VerifyCookie: {
53634 #if 0 /* local variables moved into u.aw */
53635 int iMeta;
53636 Btree *pBt;
53637 #endif /* local variables moved into u.aw */
53638 assert( pOp->p1>=0 && pOp->p1<db->nDb );
53639 assert( (p->btreeMask & (1<<pOp->p1))!=0 );
53640 u.aw.pBt = db->aDb[pOp->p1].pBt;
53641 if( u.aw.pBt ){
53642 rc = sqlite3BtreeGetMeta(u.aw.pBt, BTREE_SCHEMA_VERSION, (u32 *)&u.aw.iMeta);
53643 }else{
53644 rc = SQLITE_OK;
53645 u.aw.iMeta = 0;
53646 }
53647 if( rc==SQLITE_OK && u.aw.iMeta!=pOp->p2 ){
53648 sqlite3DbFree(db, p->zErrMsg);
53649 p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed");
53650 /* If the schema-cookie from the database file matches the cookie
53651 ** stored with the in-memory representation of the schema, do
53652 ** not reload the schema from the database file.
@@ -53729,11 +53658,11 @@
53658 ** discard the database schema, as the user code implementing the
53659 ** v-table would have to be ready for the sqlite3_vtab structure itself
53660 ** to be invalidated whenever sqlite3_step() is called from within
53661 ** a v-table method.
53662 */
53663 if( db->aDb[pOp->p1].pSchema->schema_cookie!=u.aw.iMeta ){
53664 sqlite3ResetInternalSchema(db, pOp->p1);
53665 }
53666
53667 sqlite3ExpirePreparedStatements(db);
53668 rc = SQLITE_SCHEMA;
@@ -53790,105 +53719,104 @@
53719 **
53720 ** See also OpenRead.
53721 */
53722 case OP_OpenRead:
53723 case OP_OpenWrite: {
53724 #if 0 /* local variables moved into u.ax */
53725 int nField;
53726 KeyInfo *pKeyInfo;
 
53727 int p2;
53728 int iDb;
53729 int wrFlag;
53730 Btree *pX;
53731 VdbeCursor *pCur;
53732 Db *pDb;
53733 int flags;
53734 #endif /* local variables moved into u.ax */
53735
53736 u.ax.nField = 0;
53737 u.ax.pKeyInfo = 0;
53738 u.ax.p2 = pOp->p2;
53739 u.ax.iDb = pOp->p3;
53740 assert( u.ax.iDb>=0 && u.ax.iDb<db->nDb );
53741 assert( (p->btreeMask & (1<<u.ax.iDb))!=0 );
53742 u.ax.pDb = &db->aDb[u.ax.iDb];
53743 u.ax.pX = u.ax.pDb->pBt;
53744 assert( u.ax.pX!=0 );
53745 if( pOp->opcode==OP_OpenWrite ){
53746 u.ax.wrFlag = 1;
53747 if( u.ax.pDb->pSchema->file_format < p->minWriteFileFormat ){
53748 p->minWriteFileFormat = u.ax.pDb->pSchema->file_format;
53749 }
53750 }else{
53751 u.ax.wrFlag = 0;
53752 }
53753 if( pOp->p5 ){
53754 assert( u.ax.p2>0 );
53755 assert( u.ax.p2<=p->nMem );
53756 pIn2 = &p->aMem[u.ax.p2];
53757 sqlite3VdbeMemIntegerify(pIn2);
53758 u.ax.p2 = (int)pIn2->u.i;
53759 /* The u.ax.p2 value always comes from a prior OP_CreateTable opcode and
53760 ** that opcode will always set the u.ax.p2 value to 2 or more or else fail.
53761 ** If there were a failure, the prepared statement would have halted
53762 ** before reaching this instruction. */
53763 if( NEVER(u.ax.p2<2) ) {
53764 rc = SQLITE_CORRUPT_BKPT;
53765 goto abort_due_to_error;
53766 }
53767 }
53768 if( pOp->p4type==P4_KEYINFO ){
53769 u.ax.pKeyInfo = pOp->p4.pKeyInfo;
53770 u.ax.pKeyInfo->enc = ENC(p->db);
53771 u.ax.nField = u.ax.pKeyInfo->nField+1;
53772 }else if( pOp->p4type==P4_INT32 ){
53773 u.ax.nField = pOp->p4.i;
53774 }
53775 assert( pOp->p1>=0 );
53776 u.ax.pCur = allocateCursor(p, pOp->p1, u.ax.nField, u.ax.iDb, 1);
53777 if( u.ax.pCur==0 ) goto no_mem;
53778 u.ax.pCur->nullRow = 1;
53779 rc = sqlite3BtreeCursor(u.ax.pX, u.ax.p2, u.ax.wrFlag, u.ax.pKeyInfo, u.ax.pCur->pCursor);
53780 u.ax.pCur->pKeyInfo = u.ax.pKeyInfo;
53781
53782 switch( rc ){
53783 case SQLITE_OK: {
53784 u.ax.flags = sqlite3BtreeFlags(u.ax.pCur->pCursor);
53785
53786 /* Sanity checking. Only the lower four bits of the u.ax.flags byte should
53787 ** be used. Bit 3 (mask 0x08) is unpredictable. The lower 3 bits
53788 ** (mask 0x07) should be either 5 (intkey+leafdata for tables) or
53789 ** 2 (zerodata for indices). If these conditions are not met it can
53790 ** only mean that we are dealing with a corrupt database file.
53791 ** Note: All of the above is checked already in sqlite3BtreeCursor().
53792 */
53793 assert( (u.ax.flags & 0xf0)==0 );
53794 assert( (u.ax.flags & 0x07)==5 || (u.ax.flags & 0x07)==2 );
53795
53796 u.ax.pCur->isTable = (u.ax.flags & BTREE_INTKEY)!=0 ?1:0;
53797 u.ax.pCur->isIndex = (u.ax.flags & BTREE_ZERODATA)!=0 ?1:0;
53798 /* If P4==0 it means we are expected to open a table. If P4!=0 then
53799 ** we expect to be opening an index. If this is not what happened,
53800 ** then the database is corrupt
53801 */
53802 if( (u.ax.pCur->isTable && pOp->p4type==P4_KEYINFO)
53803 || (u.ax.pCur->isIndex && pOp->p4type!=P4_KEYINFO) ){
 
53804 rc = SQLITE_CORRUPT_BKPT;
53805 goto abort_due_to_error;
53806 }
53807 break;
53808 }
53809 case SQLITE_EMPTY: {
53810 u.ax.pCur->isTable = pOp->p4type!=P4_KEYINFO;
53811 u.ax.pCur->isIndex = !u.ax.pCur->isTable;
53812 u.ax.pCur->pCursor = 0;
53813 rc = SQLITE_OK;
53814 break;
53815 }
53816 default: {
53817 assert( rc!=SQLITE_BUSY ); /* Busy conditions detected earlier */
53818 goto abort_due_to_error;
53819 }
53820 }
53821 break;
53822 }
@@ -53910,30 +53838,28 @@
53838 ** to a TEMP table at the SQL level, or to a table opened by
53839 ** this opcode. Then this opcode was call OpenVirtual. But
53840 ** that created confusion with the whole virtual-table idea.
53841 */
53842 case OP_OpenEphemeral: {
53843 #if 0 /* local variables moved into u.ay */
 
53844 VdbeCursor *pCx;
53845 #endif /* local variables moved into u.ay */
53846 static const int openFlags =
53847 SQLITE_OPEN_READWRITE |
53848 SQLITE_OPEN_CREATE |
53849 SQLITE_OPEN_EXCLUSIVE |
53850 SQLITE_OPEN_DELETEONCLOSE |
53851 SQLITE_OPEN_TRANSIENT_DB;
53852
53853 assert( pOp->p1>=0 );
53854 u.ay.pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, 1);
53855 if( u.ay.pCx==0 ) goto no_mem;
53856 u.ay.pCx->nullRow = 1;
 
53857 rc = sqlite3BtreeFactory(db, 0, 1, SQLITE_DEFAULT_TEMP_CACHE_SIZE, openFlags,
53858 &u.ay.pCx->pBt);
53859 if( rc==SQLITE_OK ){
53860 rc = sqlite3BtreeBeginTrans(u.ay.pCx->pBt, 1);
53861 }
53862 if( rc==SQLITE_OK ){
53863 /* If a transient index is required, create it by calling
53864 ** sqlite3BtreeCreateTable() with the BTREE_ZERODATA flag before
53865 ** opening it. If a transient table is required, just use the
@@ -53940,25 +53866,25 @@
53866 ** automatically created table with root-page 1 (an INTKEY table).
53867 */
53868 if( pOp->p4.pKeyInfo ){
53869 int pgno;
53870 assert( pOp->p4type==P4_KEYINFO );
53871 rc = sqlite3BtreeCreateTable(u.ay.pCx->pBt, &pgno, BTREE_ZERODATA);
53872 if( rc==SQLITE_OK ){
53873 assert( pgno==MASTER_ROOT+1 );
53874 rc = sqlite3BtreeCursor(u.ay.pCx->pBt, pgno, 1,
53875 (KeyInfo*)pOp->p4.z, u.ay.pCx->pCursor);
53876 u.ay.pCx->pKeyInfo = pOp->p4.pKeyInfo;
53877 u.ay.pCx->pKeyInfo->enc = ENC(p->db);
53878 }
53879 u.ay.pCx->isTable = 0;
53880 }else{
53881 rc = sqlite3BtreeCursor(u.ay.pCx->pBt, MASTER_ROOT, 1, 0, u.ay.pCx->pCursor);
53882 u.ay.pCx->isTable = 1;
53883 }
53884 }
53885 u.ay.pCx->isIndex = !u.ay.pCx->isTable;
53886 break;
53887 }
53888
53889 /* Opcode: OpenPseudo P1 P2 P3 * *
53890 **
@@ -53982,40 +53908,34 @@
53908 **
53909 ** P3 is the number of fields in the records that will be stored by
53910 ** the pseudo-table.
53911 */
53912 case OP_OpenPseudo: {
53913 #if 0 /* local variables moved into u.az */
 
53914 VdbeCursor *pCx;
53915 #endif /* local variables moved into u.az */
53916
53917 assert( pOp->p1>=0 );
53918 u.az.pCx = allocateCursor(p, pOp->p1, pOp->p3, -1, 0);
53919 if( u.az.pCx==0 ) goto no_mem;
53920 u.az.pCx->nullRow = 1;
53921 u.az.pCx->pseudoTable = 1;
53922 u.az.pCx->ephemPseudoTable = (u8)pOp->p2;
53923 u.az.pCx->isTable = 1;
53924 u.az.pCx->isIndex = 0;
 
53925 break;
53926 }
53927
53928 /* Opcode: Close P1 * * * *
53929 **
53930 ** Close a cursor previously opened as P1. If P1 is not
53931 ** currently open, this instruction is a no-op.
53932 */
53933 case OP_Close: {
53934 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
53935 sqlite3VdbeFreeCursor(p, p->apCsr[pOp->p1]);
53936 p->apCsr[pOp->p1] = 0;
 
 
 
 
53937 break;
53938 }
53939
53940 /* Opcode: SeekGe P1 P2 P3 P4 *
53941 **
@@ -54071,35 +53991,33 @@
53991 */
53992 case OP_SeekLt: /* jump, in3 */
53993 case OP_SeekLe: /* jump, in3 */
53994 case OP_SeekGe: /* jump, in3 */
53995 case OP_SeekGt: { /* jump, in3 */
53996 #if 0 /* local variables moved into u.ba */
 
53997 int res;
53998 int oc;
53999 VdbeCursor *pC;
54000 UnpackedRecord r;
54001 int nField;
54002 i64 iKey; /* The rowid we are to seek to */
54003 #endif /* local variables moved into u.ba */
54004
54005 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 
54006 assert( pOp->p2!=0 );
54007 u.ba.pC = p->apCsr[pOp->p1];
54008 assert( u.ba.pC!=0 );
54009 if( u.ba.pC->pCursor!=0 ){
54010 u.ba.oc = pOp->opcode;
54011 u.ba.pC->nullRow = 0;
54012 if( u.ba.pC->isTable ){
54013 /* The input value in P3 might be of any type: integer, real, string,
54014 ** blob, or NULL. But it needs to be an integer before we can do
54015 ** the seek, so covert it. */
54016 applyNumericAffinity(pIn3);
54017 u.ba.iKey = sqlite3VdbeIntValue(pIn3);
54018 u.ba.pC->rowidIsValid = 0;
54019
54020 /* If the P3 value could not be converted into an integer without
54021 ** loss of information, then special processing is required... */
54022 if( (pIn3->flags & MEM_Int)==0 ){
54023 if( (pIn3->flags & MEM_Real)==0 ){
@@ -54110,99 +54028,100 @@
54028 }
54029 /* If we reach this point, then the P3 value must be a floating
54030 ** point number. */
54031 assert( (pIn3->flags & MEM_Real)!=0 );
54032
54033 if( u.ba.iKey==SMALLEST_INT64 && (pIn3->r<(double)u.ba.iKey || pIn3->r>0) ){
54034 /* The P3 value is too large in magnitude to be expressed as an
54035 ** integer. */
54036 u.ba.res = 1;
54037 if( pIn3->r<0 ){
54038 if( u.ba.oc==OP_SeekGt || u.ba.oc==OP_SeekGe ){
54039 rc = sqlite3BtreeFirst(u.ba.pC->pCursor, &u.ba.res);
54040 if( rc!=SQLITE_OK ) goto abort_due_to_error;
54041 }
54042 }else{
54043 if( u.ba.oc==OP_SeekLt || u.ba.oc==OP_SeekLe ){
54044 rc = sqlite3BtreeLast(u.ba.pC->pCursor, &u.ba.res);
54045 if( rc!=SQLITE_OK ) goto abort_due_to_error;
54046 }
54047 }
54048 if( u.ba.res ){
54049 pc = pOp->p2 - 1;
54050 }
54051 break;
54052 }else if( u.ba.oc==OP_SeekLt || u.ba.oc==OP_SeekGe ){
54053 /* Use the ceiling() function to convert real->int */
54054 if( pIn3->r > (double)u.ba.iKey ) u.ba.iKey++;
54055 }else{
54056 /* Use the floor() function to convert real->int */
54057 assert( u.ba.oc==OP_SeekLe || u.ba.oc==OP_SeekGt );
54058 if( pIn3->r < (double)u.ba.iKey ) u.ba.iKey--;
54059 }
54060 }
54061 rc = sqlite3BtreeMovetoUnpacked(u.ba.pC->pCursor, 0, (u64)u.ba.iKey, 0, &u.ba.res);
54062 if( rc!=SQLITE_OK ){
54063 goto abort_due_to_error;
54064 }
54065 if( u.ba.res==0 ){
54066 u.ba.pC->rowidIsValid = 1;
54067 u.ba.pC->lastRowid = u.ba.iKey;
54068 }
54069 }else{
54070 u.ba.nField = pOp->p4.i;
54071 assert( pOp->p4type==P4_INT32 );
54072 assert( u.ba.nField>0 );
54073 u.ba.r.pKeyInfo = u.ba.pC->pKeyInfo;
54074 u.ba.r.nField = (u16)u.ba.nField;
54075 if( u.ba.oc==OP_SeekGt || u.ba.oc==OP_SeekLe ){
54076 u.ba.r.flags = UNPACKED_INCRKEY;
54077 }else{
54078 u.ba.r.flags = 0;
54079 }
54080 u.ba.r.aMem = &p->aMem[pOp->p3];
54081 rc = sqlite3BtreeMovetoUnpacked(u.ba.pC->pCursor, &u.ba.r, 0, 0, &u.ba.res);
54082 if( rc!=SQLITE_OK ){
54083 goto abort_due_to_error;
54084 }
54085 u.ba.pC->rowidIsValid = 0;
54086 }
54087 u.ba.pC->deferredMoveto = 0;
54088 u.ba.pC->cacheStatus = CACHE_STALE;
54089 #ifdef SQLITE_TEST
54090 sqlite3_search_count++;
54091 #endif
54092 if( u.ba.oc==OP_SeekGe || u.ba.oc==OP_SeekGt ){
54093 if( u.ba.res<0 || (u.ba.res==0 && u.ba.oc==OP_SeekGt) ){
54094 rc = sqlite3BtreeNext(u.ba.pC->pCursor, &u.ba.res);
54095 if( rc!=SQLITE_OK ) goto abort_due_to_error;
54096 u.ba.pC->rowidIsValid = 0;
54097 }else{
54098 u.ba.res = 0;
54099 }
54100 }else{
54101 assert( u.ba.oc==OP_SeekLt || u.ba.oc==OP_SeekLe );
54102 if( u.ba.res>0 || (u.ba.res==0 && u.ba.oc==OP_SeekLt) ){
54103 rc = sqlite3BtreePrevious(u.ba.pC->pCursor, &u.ba.res);
54104 if( rc!=SQLITE_OK ) goto abort_due_to_error;
54105 u.ba.pC->rowidIsValid = 0;
54106 }else{
54107 /* u.ba.res might be negative because the table is empty. Check to
54108 ** see if this is the case.
54109 */
54110 u.ba.res = sqlite3BtreeEof(u.ba.pC->pCursor);
54111 }
54112 }
54113 assert( pOp->p2>0 );
54114 if( u.ba.res ){
54115 pc = pOp->p2 - 1;
54116 }
54117 }else{
54118 /* This happens when attempting to open the sqlite3_master table
54119 ** for read access returns SQLITE_EMPTY. In this case always
54120 ** take the jump (since there are no records in the table).
54121 */
54122 assert( u.ba.pC->pseudoTable==0 );
54123 pc = pOp->p2 - 1;
54124 }
54125 break;
54126 }
54127
@@ -54214,25 +54133,23 @@
54133 ** This is actually a deferred seek. Nothing actually happens until
54134 ** the cursor is used to read a record. That way, if no reads
54135 ** occur, no unnecessary I/O happens.
54136 */
54137 case OP_Seek: { /* in2 */
54138 #if 0 /* local variables moved into u.bb */
 
54139 VdbeCursor *pC;
54140 #endif /* local variables moved into u.bb */
54141
54142 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
54143 u.bb.pC = p->apCsr[pOp->p1];
54144 assert( u.bb.pC!=0 );
54145 if( ALWAYS(u.bb.pC->pCursor!=0) ){
54146 assert( u.bb.pC->isTable );
54147 u.bb.pC->nullRow = 0;
54148 u.bb.pC->movetoTarget = sqlite3VdbeIntValue(pIn2);
54149 u.bb.pC->rowidIsValid = 0;
54150 u.bb.pC->deferredMoveto = 1;
 
54151 }
54152 break;
54153 }
54154
54155
@@ -54266,48 +54183,47 @@
54183 **
54184 ** See also: Found, NotExists, IsUnique
54185 */
54186 case OP_NotFound: /* jump, in3 */
54187 case OP_Found: { /* jump, in3 */
54188 #if 0 /* local variables moved into u.bc */
 
54189 int alreadyExists;
54190 VdbeCursor *pC;
54191 int res;
54192 UnpackedRecord *pIdxKey;
54193 char aTempRec[ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*3 + 7];
54194 #endif /* local variables moved into u.bc */
54195
54196 u.bc.alreadyExists = 0;
54197 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
54198 u.bc.pC = p->apCsr[pOp->p1];
54199 assert( u.bc.pC!=0 );
54200 if( ALWAYS(u.bc.pC->pCursor!=0) ){
54201
54202 assert( u.bc.pC->isTable==0 );
54203 assert( pIn3->flags & MEM_Blob );
54204 u.bc.pIdxKey = sqlite3VdbeRecordUnpack(u.bc.pC->pKeyInfo, pIn3->n, pIn3->z,
54205 u.bc.aTempRec, sizeof(u.bc.aTempRec));
54206 if( u.bc.pIdxKey==0 ){
54207 goto no_mem;
54208 }
54209 if( pOp->opcode==OP_Found ){
54210 u.bc.pIdxKey->flags |= UNPACKED_PREFIX_MATCH;
54211 }
54212 rc = sqlite3BtreeMovetoUnpacked(u.bc.pC->pCursor, u.bc.pIdxKey, 0, 0, &u.bc.res);
54213 sqlite3VdbeDeleteUnpackedRecord(u.bc.pIdxKey);
54214 if( rc!=SQLITE_OK ){
54215 break;
54216 }
54217 u.bc.alreadyExists = (u.bc.res==0);
54218 u.bc.pC->deferredMoveto = 0;
54219 u.bc.pC->cacheStatus = CACHE_STALE;
54220 }
54221 if( pOp->opcode==OP_Found ){
54222 if( u.bc.alreadyExists ) pc = pOp->p2 - 1;
54223 }else{
54224 if( !u.bc.alreadyExists ) pc = pOp->p2 - 1;
54225 }
54226 break;
54227 }
54228
54229 /* Opcode: IsUnique P1 P2 P3 P4 *
@@ -54334,63 +54250,63 @@
54250 ** instruction.
54251 **
54252 ** See also: NotFound, NotExists, Found
54253 */
54254 case OP_IsUnique: { /* jump, in3 */
54255 #if 0 /* local variables moved into u.bd */
54256 u16 ii;
54257 VdbeCursor *pCx;
54258 BtCursor *pCrsr;
54259 u16 nField;
54260 Mem *aMem;
54261 UnpackedRecord r; /* B-Tree index search key */
54262 i64 R; /* Rowid stored in register P3 */
54263 #endif /* local variables moved into u.bd */
54264
54265 u.bd.aMem = &p->aMem[pOp->p4.i];
54266 /* Assert that the values of parameters P1 and P4 are in range. */
54267 assert( pOp->p4type==P4_INT32 );
54268 assert( pOp->p4.i>0 && pOp->p4.i<=p->nMem );
54269 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
54270
54271 /* Find the index cursor. */
54272 u.bd.pCx = p->apCsr[pOp->p1];
54273 assert( u.bd.pCx->deferredMoveto==0 );
54274 u.bd.pCx->seekResult = 0;
54275 u.bd.pCx->cacheStatus = CACHE_STALE;
54276 u.bd.pCrsr = u.bd.pCx->pCursor;
54277
54278 /* If any of the values are NULL, take the jump. */
54279 u.bd.nField = u.bd.pCx->pKeyInfo->nField;
54280 for(u.bd.ii=0; u.bd.ii<u.bd.nField; u.bd.ii++){
54281 if( u.bd.aMem[u.bd.ii].flags & MEM_Null ){
54282 pc = pOp->p2 - 1;
54283 u.bd.pCrsr = 0;
54284 break;
54285 }
54286 }
54287 assert( (u.bd.aMem[u.bd.nField].flags & MEM_Null)==0 );
54288
54289 if( u.bd.pCrsr!=0 ){
54290 /* Populate the index search key. */
54291 u.bd.r.pKeyInfo = u.bd.pCx->pKeyInfo;
54292 u.bd.r.nField = u.bd.nField + 1;
54293 u.bd.r.flags = UNPACKED_PREFIX_SEARCH;
54294 u.bd.r.aMem = u.bd.aMem;
54295
54296 /* Extract the value of u.bd.R from register P3. */
54297 sqlite3VdbeMemIntegerify(pIn3);
54298 u.bd.R = pIn3->u.i;
54299
54300 /* Search the B-Tree index. If no conflicting record is found, jump
54301 ** to P2. Otherwise, copy the rowid of the conflicting record to
54302 ** register P3 and fall through to the next instruction. */
54303 rc = sqlite3BtreeMovetoUnpacked(u.bd.pCrsr, &u.bd.r, 0, 0, &u.bd.pCx->seekResult);
54304 if( (u.bd.r.flags & UNPACKED_PREFIX_SEARCH) || u.bd.r.rowid==u.bd.R ){
54305 pc = pOp->p2 - 1;
54306 }else{
54307 pIn3->u.i = u.bd.r.rowid;
54308 }
54309 }
54310 break;
54311 }
54312
@@ -54407,45 +54323,46 @@
54323 ** P1 is an index.
54324 **
54325 ** See also: Found, NotFound, IsUnique
54326 */
54327 case OP_NotExists: { /* jump, in3 */
54328 #if 0 /* local variables moved into u.be */
 
54329 VdbeCursor *pC;
54330 BtCursor *pCrsr;
54331 int res;
54332 u64 iKey;
54333 #endif /* local variables moved into u.be */
54334
54335 assert( pIn3->flags & MEM_Int );
54336 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
54337 u.be.pC = p->apCsr[pOp->p1];
54338 assert( u.be.pC!=0 );
54339 assert( u.be.pC->isTable );
54340 u.be.pCrsr = u.be.pC->pCursor;
54341 if( u.be.pCrsr!=0 ){
54342 u.be.res = 0;
54343 u.be.iKey = pIn3->u.i;
54344 rc = sqlite3BtreeMovetoUnpacked(u.be.pCrsr, 0, u.be.iKey, 0, &u.be.res);
54345 u.be.pC->lastRowid = pIn3->u.i;
54346 u.be.pC->rowidIsValid = u.be.res==0 ?1:0;
54347 u.be.pC->nullRow = 0;
54348 u.be.pC->cacheStatus = CACHE_STALE;
54349 u.be.pC->deferredMoveto = 0;
54350 if( u.be.res!=0 ){
54351 pc = pOp->p2 - 1;
54352 assert( u.be.pC->rowidIsValid==0 );
54353 }
54354 u.be.pC->seekResult = u.be.res;
54355 }else{
54356 /* This happens when an attempt to open a read cursor on the
54357 ** sqlite_master table returns SQLITE_EMPTY.
54358 */
54359 assert( !u.be.pC->pseudoTable );
54360 assert( u.be.pC->isTable );
54361 pc = pOp->p2 - 1;
54362 assert( u.be.pC->rowidIsValid==0 );
54363 u.be.pC->seekResult = 0;
54364 }
54365 break;
54366 }
54367
54368 /* Opcode: Sequence P1 P2 * * *
@@ -54454,14 +54371,13 @@
54371 ** Write the sequence number into register P2.
54372 ** The sequence number on the cursor is incremented after this
54373 ** instruction.
54374 */
54375 case OP_Sequence: { /* out2-prerelease */
54376 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
54377 assert( p->apCsr[pOp->p1]!=0 );
54378 pOut->u.i = p->apCsr[pOp->p1]->seqCount++;
 
54379 MemSetTypeFlag(pOut, MEM_Int);
54380 break;
54381 }
54382
54383
@@ -54478,28 +54394,24 @@
54394 ** error is generated. The P3 register is updated with the generated
54395 ** record number. This P3 mechanism is used to help implement the
54396 ** AUTOINCREMENT feature.
54397 */
54398 case OP_NewRowid: { /* out2-prerelease */
54399 #if 0 /* local variables moved into u.bf */
54400 i64 v; /* The new rowid */
54401 VdbeCursor *pC; /* Cursor of table to get the new rowid */
54402 int res; /* Result of an sqlite3BtreeLast() */
54403 int cnt; /* Counter to limit the number of searches */
54404 Mem *pMem; /* Register holding largest rowid for AUTOINCREMENT */
54405 #endif /* local variables moved into u.bf */
54406
54407 u.bf.v = 0;
54408 u.bf.res = 0;
54409 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
54410 u.bf.pC = p->apCsr[pOp->p1];
54411 assert( u.bf.pC!=0 );
54412 if( NEVER(u.bf.pC->pCursor==0) ){
 
 
 
 
54413 /* The zero initialization above is all that is needed */
54414 }else{
54415 /* The next rowid or record number (different terms for the same
54416 ** thing) is obtained in a two-step algorithm.
54417 **
@@ -54509,38 +54421,14 @@
54421 ** probabilistic algorithm
54422 **
54423 ** The second algorithm is to select a rowid at random and see if
54424 ** it already exists in the table. If it does not exist, we have
54425 ** succeeded. If the random rowid does exist, we select a new one
54426 ** and try again, up to 100 times.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54427 */
54428 assert( u.bf.pC->isTable );
54429 u.bf.cnt = 0;
 
 
 
 
 
 
54430
54431 #ifdef SQLITE_32BIT_ROWID
54432 # define MAX_ROWID 0x7fffffff
54433 #else
54434 /* Some compilers complain about constants of the form 0x7fffffffffffffff.
@@ -54548,78 +54436,76 @@
54436 ** to provide the constant while making all compilers happy.
54437 */
54438 # define MAX_ROWID (i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff )
54439 #endif
54440
54441 if( !u.bf.pC->useRandomRowid ){
54442 u.bf.v = sqlite3BtreeGetCachedRowid(u.bf.pC->pCursor);
54443 if( u.bf.v==0 ){
54444 rc = sqlite3BtreeLast(u.bf.pC->pCursor, &u.bf.res);
54445 if( rc!=SQLITE_OK ){
54446 goto abort_due_to_error;
54447 }
54448 if( u.bf.res ){
54449 u.bf.v = 1;
54450 }else{
54451 sqlite3BtreeKeySize(u.bf.pC->pCursor, &u.bf.v);
54452 if( u.bf.v==MAX_ROWID ){
54453 u.bf.pC->useRandomRowid = 1;
54454 }else{
54455 u.bf.v++;
 
54456 }
54457 }
54458 }
54459
54460 #ifndef SQLITE_OMIT_AUTOINCREMENT
54461 if( pOp->p3 ){
54462 assert( pOp->p3>0 && pOp->p3<=p->nMem ); /* P3 is a valid memory cell */
54463 u.bf.pMem = &p->aMem[pOp->p3];
54464 REGISTER_TRACE(pOp->p3, u.bf.pMem);
54465 sqlite3VdbeMemIntegerify(u.bf.pMem);
54466 assert( (u.bf.pMem->flags & MEM_Int)!=0 ); /* mem(P3) holds an integer */
54467 if( u.bf.pMem->u.i==MAX_ROWID || u.bf.pC->useRandomRowid ){
54468 rc = SQLITE_FULL;
54469 goto abort_due_to_error;
54470 }
54471 if( u.bf.v<u.bf.pMem->u.i+1 ){
54472 u.bf.v = u.bf.pMem->u.i + 1;
54473 }
54474 u.bf.pMem->u.i = u.bf.v;
54475 }
54476 #endif
54477
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{
54489 sqlite3_randomness(sizeof(u.bf.v), &u.bf.v);
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 }
54501 u.bf.pC->rowidIsValid = 0;
54502 u.bf.pC->deferredMoveto = 0;
54503 u.bf.pC->cacheStatus = CACHE_STALE;
54504 }
54505 MemSetTypeFlag(pOut, MEM_Int);
54506 pOut->u.i = u.bf.v;
 
54507 break;
54508 }
54509
54510 /* Opcode: Insert P1 P2 P3 P4 P5
54511 **
@@ -54646,91 +54532,89 @@
54532 **
54533 ** This instruction only works on tables. The equivalent instruction
54534 ** for indices is OP_IdxInsert.
54535 */
54536 case OP_Insert: {
54537 #if 0 /* local variables moved into u.bg */
54538 Mem *pData;
54539 Mem *pKey;
54540 i64 iKey; /* The integer ROWID or key for the record to be inserted */
 
54541 VdbeCursor *pC;
54542 int nZero;
54543 int seekResult;
54544 const char *zDb;
54545 const char *zTbl;
54546 int op;
54547 #endif /* local variables moved into u.bg */
54548
54549 u.bg.pData = &p->aMem[pOp->p2];
54550 u.bg.pKey = &p->aMem[pOp->p3];
54551 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
54552 u.bg.pC = p->apCsr[pOp->p1];
54553 assert( u.bg.pC!=0 );
54554 assert( u.bg.pC->pCursor!=0 || u.bg.pC->pseudoTable );
54555 assert( u.bg.pKey->flags & MEM_Int );
54556 assert( u.bg.pC->isTable );
54557 REGISTER_TRACE(pOp->p2, u.bg.pData);
54558 REGISTER_TRACE(pOp->p3, u.bg.pKey);
54559
54560 u.bg.iKey = u.bg.pKey->u.i;
 
54561 if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
54562 if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = u.bg.pKey->u.i;
54563 if( u.bg.pData->flags & MEM_Null ){
54564 u.bg.pData->z = 0;
54565 u.bg.pData->n = 0;
54566 }else{
54567 assert( u.bg.pData->flags & (MEM_Blob|MEM_Str) );
54568 }
54569 if( u.bg.pC->pseudoTable ){
54570 if( !u.bg.pC->ephemPseudoTable ){
54571 sqlite3DbFree(db, u.bg.pC->pData);
54572 }
54573 u.bg.pC->iKey = u.bg.iKey;
54574 u.bg.pC->nData = u.bg.pData->n;
54575 if( u.bg.pC->ephemPseudoTable || u.bg.pData->z==u.bg.pData->zMalloc ){
54576 u.bg.pC->pData = u.bg.pData->z;
54577 if( !u.bg.pC->ephemPseudoTable ){
54578 u.bg.pData->flags &= ~MEM_Dyn;
54579 u.bg.pData->flags |= MEM_Ephem;
54580 u.bg.pData->zMalloc = 0;
54581 }
54582 }else{
54583 u.bg.pC->pData = sqlite3Malloc( u.bg.pC->nData+2 );
54584 if( !u.bg.pC->pData ) goto no_mem;
54585 memcpy(u.bg.pC->pData, u.bg.pData->z, u.bg.pC->nData);
54586 u.bg.pC->pData[u.bg.pC->nData] = 0;
54587 u.bg.pC->pData[u.bg.pC->nData+1] = 0;
54588 }
54589 u.bg.pC->nullRow = 0;
54590 }else{
54591 u.bg.seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? u.bg.pC->seekResult : 0);
54592 if( u.bg.pData->flags & MEM_Zero ){
54593 u.bg.nZero = u.bg.pData->u.nZero;
54594 }else{
54595 u.bg.nZero = 0;
54596 }
54597 sqlite3BtreeSetCachedRowid(u.bg.pC->pCursor, 0);
54598 rc = sqlite3BtreeInsert(u.bg.pC->pCursor, 0, u.bg.iKey,
54599 u.bg.pData->z, u.bg.pData->n, u.bg.nZero,
54600 pOp->p5 & OPFLAG_APPEND, u.bg.seekResult
54601 );
54602 }
54603
54604 u.bg.pC->rowidIsValid = 0;
54605 u.bg.pC->deferredMoveto = 0;
54606 u.bg.pC->cacheStatus = CACHE_STALE;
54607
54608 /* Invoke the update-hook if required. */
54609 if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z ){
54610 u.bg.zDb = db->aDb[u.bg.pC->iDb].zName;
54611 u.bg.zTbl = pOp->p4.z;
54612 u.bg.op = ((pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT);
54613 assert( u.bg.pC->isTable );
54614 db->xUpdateCallback(db->pUpdateArg, u.bg.op, u.bg.zDb, u.bg.zTbl, u.bg.iKey);
54615 assert( u.bg.pC->iDb>=0 );
54616 }
54617 break;
54618 }
54619
54620 /* Opcode: Delete P1 P2 * P4 *
@@ -54752,44 +54636,51 @@
54636 ** pointing to. The update hook will be invoked, if it exists.
54637 ** If P4 is not NULL then the P1 cursor must have been positioned
54638 ** using OP_NotFound prior to invoking this opcode.
54639 */
54640 case OP_Delete: {
54641 #if 0 /* local variables moved into u.bh */
 
54642 i64 iKey;
54643 VdbeCursor *pC;
54644 #endif /* local variables moved into u.bh */
54645
54646 u.bh.iKey = 0;
54647 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
54648 u.bh.pC = p->apCsr[pOp->p1];
54649 assert( u.bh.pC!=0 );
54650 assert( u.bh.pC->pCursor!=0 ); /* Only valid for real tables, no pseudotables */
54651
54652 /* If the update-hook will be invoked, set u.bh.iKey to the rowid of the
 
54653 ** row being deleted.
54654 */
54655 if( db->xUpdateCallback && pOp->p4.z ){
54656 assert( u.bh.pC->isTable );
54657 assert( u.bh.pC->rowidIsValid ); /* lastRowid set by previous OP_NotFound */
54658 u.bh.iKey = u.bh.pC->lastRowid;
54659 }
54660
54661 /* The OP_Delete opcode always follows an OP_NotExists or OP_Last or
54662 ** OP_Column on the same table without any intervening operations that
54663 ** might move or invalidate the cursor. Hence cursor u.bh.pC is always pointing
54664 ** to the row to be deleted and the sqlite3VdbeCursorMoveto() operation
54665 ** below is always a no-op and cannot fail. We will run it anyhow, though,
54666 ** to guard against future changes to the code generator.
54667 **/
54668 assert( u.bh.pC->deferredMoveto==0 );
54669 rc = sqlite3VdbeCursorMoveto(u.bh.pC);
54670 if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error;
54671
54672 sqlite3BtreeSetCachedRowid(u.bh.pC->pCursor, 0);
54673 rc = sqlite3BtreeDelete(u.bh.pC->pCursor);
54674 u.bh.pC->cacheStatus = CACHE_STALE;
54675
54676 /* Invoke the update-hook if required. */
54677 if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z ){
54678 const char *zDb = db->aDb[u.bh.pC->iDb].zName;
54679 const char *zTbl = pOp->p4.z;
54680 db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, zTbl, u.bh.iKey);
54681 assert( u.bh.pC->iDb>=0 );
54682 }
54683 if( pOp->p2 & OPFLAG_NCHANGE ) p->nChange++;
54684 break;
54685 }
54686
@@ -54828,55 +54719,61 @@
54719 ** If the P1 cursor must be pointing to a valid row (not a NULL row)
54720 ** of a real table, not a pseudo-table.
54721 */
54722 case OP_RowKey:
54723 case OP_RowData: {
54724 #if 0 /* local variables moved into u.bi */
 
54725 VdbeCursor *pC;
54726 BtCursor *pCrsr;
54727 u32 n;
54728 i64 n64;
54729 #endif /* local variables moved into u.bi */
54730
 
54731 pOut = &p->aMem[pOp->p2];
54732
54733 /* Note that RowKey and RowData are really exactly the same instruction */
54734 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
54735 u.bi.pC = p->apCsr[pOp->p1];
54736 assert( u.bi.pC->isTable || pOp->opcode==OP_RowKey );
54737 assert( u.bi.pC->isIndex || pOp->opcode==OP_RowData );
54738 assert( u.bi.pC!=0 );
54739 assert( u.bi.pC->nullRow==0 );
54740 assert( u.bi.pC->pseudoTable==0 );
54741 assert( u.bi.pC->pCursor!=0 );
54742 u.bi.pCrsr = u.bi.pC->pCursor;
54743
54744 /* The OP_RowKey and OP_RowData opcodes always follow OP_NotExists or
54745 ** OP_Rewind/Op_Next with no intervening instructions that might invalidate
54746 ** the cursor. Hence the following sqlite3VdbeCursorMoveto() call is always
54747 ** a no-op and can never fail. But we leave it in place as a safety.
54748 */
54749 assert( u.bi.pC->deferredMoveto==0 );
54750 rc = sqlite3VdbeCursorMoveto(u.bi.pC);
54751 if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error;
54752
54753 if( u.bi.pC->isIndex ){
54754 assert( !u.bi.pC->isTable );
54755 sqlite3BtreeKeySize(u.bi.pCrsr, &u.bi.n64);
54756 if( u.bi.n64>db->aLimit[SQLITE_LIMIT_LENGTH] ){
54757 goto too_big;
54758 }
54759 u.bi.n = (u32)u.bi.n64;
54760 }else{
54761 sqlite3BtreeDataSize(u.bi.pCrsr, &u.bi.n);
54762 if( u.bi.n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
54763 goto too_big;
54764 }
54765 }
54766 if( sqlite3VdbeMemGrow(pOut, u.bi.n, 0) ){
54767 goto no_mem;
54768 }
54769 pOut->n = u.bi.n;
54770 MemSetTypeFlag(pOut, MEM_Blob);
54771 if( u.bi.pC->isIndex ){
54772 rc = sqlite3BtreeKey(u.bi.pCrsr, 0, u.bi.n, pOut->z);
54773 }else{
54774 rc = sqlite3BtreeData(u.bi.pCrsr, 0, u.bi.n, pOut->z);
54775 }
54776 pOut->enc = SQLITE_UTF8; /* In case the blob is ever cast to text */
54777 UPDATE_MAX_BLOBSIZE(pOut);
54778 break;
54779 }
@@ -54889,53 +54786,50 @@
54786 ** P1 can be either an ordinary table or a virtual table. There used to
54787 ** be a separate OP_VRowid opcode for use with virtual tables, but this
54788 ** one opcode now works for both table types.
54789 */
54790 case OP_Rowid: { /* out2-prerelease */
54791 #if 0 /* local variables moved into u.bj */
 
54792 VdbeCursor *pC;
54793 i64 v;
54794 sqlite3_vtab *pVtab;
54795 const sqlite3_module *pModule;
54796 #endif /* local variables moved into u.bj */
54797
54798 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
54799 u.bj.pC = p->apCsr[pOp->p1];
54800 assert( u.bj.pC!=0 );
54801 if( u.bj.pC->nullRow ){
 
54802 /* Do nothing so that reg[P2] remains NULL */
54803 break;
54804 }else if( u.bj.pC->deferredMoveto ){
54805 u.bj.v = u.bj.pC->movetoTarget;
54806 }else if( u.bj.pC->pseudoTable ){
54807 u.bj.v = u.bj.pC->iKey;
54808 #ifndef SQLITE_OMIT_VIRTUALTABLE
54809 }else if( u.bj.pC->pVtabCursor ){
54810 u.bj.pVtab = u.bj.pC->pVtabCursor->pVtab;
54811 u.bj.pModule = u.bj.pVtab->pModule;
54812 assert( u.bj.pModule->xRowid );
54813 if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
54814 rc = u.bj.pModule->xRowid(u.bj.pC->pVtabCursor, &u.bj.v);
54815 sqlite3DbFree(db, p->zErrMsg);
54816 p->zErrMsg = u.bj.pVtab->zErrMsg;
54817 u.bj.pVtab->zErrMsg = 0;
54818 if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
54819 #endif /* SQLITE_OMIT_VIRTUALTABLE */
54820 }else{
54821 rc = sqlite3VdbeCursorMoveto(u.bj.pC);
54822 if( rc ) goto abort_due_to_error;
54823 if( u.bj.pC->rowidIsValid ){
54824 u.bj.v = u.bj.pC->lastRowid;
54825 }else{
54826 assert( u.bj.pC->pCursor!=0 );
54827 sqlite3BtreeKeySize(u.bj.pC->pCursor, &u.bj.v);
 
54828 }
54829 }
54830 pOut->u.i = u.bj.v;
54831 MemSetTypeFlag(pOut, MEM_Int);
54832 break;
54833 }
54834
54835 /* Opcode: NullRow P1 * * * *
@@ -54943,23 +54837,21 @@
54837 ** Move the cursor P1 to a null row. Any OP_Column operations
54838 ** that occur while the cursor is on the null row will always
54839 ** write a NULL.
54840 */
54841 case OP_NullRow: {
54842 #if 0 /* local variables moved into u.bk */
 
54843 VdbeCursor *pC;
54844 #endif /* local variables moved into u.bk */
54845
54846 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
54847 u.bk.pC = p->apCsr[pOp->p1];
54848 assert( u.bk.pC!=0 );
54849 u.bk.pC->nullRow = 1;
54850 u.bk.pC->rowidIsValid = 0;
54851 if( u.bk.pC->pCursor ){
54852 sqlite3BtreeClearCursor(u.bk.pC->pCursor);
 
54853 }
54854 break;
54855 }
54856
54857 /* Opcode: Last P1 P2 * * *
@@ -54969,29 +54861,30 @@
54861 ** If the table or index is empty and P2>0, then jump immediately to P2.
54862 ** If P2 is 0 or if the table or index is not empty, fall through
54863 ** to the following instruction.
54864 */
54865 case OP_Last: { /* jump */
54866 #if 0 /* local variables moved into u.bl */
 
54867 VdbeCursor *pC;
54868 BtCursor *pCrsr;
54869 int res;
54870 #endif /* local variables moved into u.bl */
54871
54872 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
54873 u.bl.pC = p->apCsr[pOp->p1];
54874 assert( u.bl.pC!=0 );
54875 u.bl.pCrsr = u.bl.pC->pCursor;
54876 if( u.bl.pCrsr==0 ){
54877 u.bl.res = 1;
54878 }else{
54879 rc = sqlite3BtreeLast(u.bl.pCrsr, &u.bl.res);
54880 }
54881 u.bl.pC->nullRow = (u8)u.bl.res;
54882 u.bl.pC->deferredMoveto = 0;
54883 u.bl.pC->rowidIsValid = 0;
54884 u.bl.pC->cacheStatus = CACHE_STALE;
54885 if( pOp->p2>0 && u.bl.res ){
54886 pc = pOp->p2 - 1;
54887 }
54888 break;
54889 }
54890
@@ -55023,33 +54916,31 @@
54916 ** If the table or index is empty and P2>0, then jump immediately to P2.
54917 ** If P2 is 0 or if the table or index is not empty, fall through
54918 ** to the following instruction.
54919 */
54920 case OP_Rewind: { /* jump */
54921 #if 0 /* local variables moved into u.bm */
 
54922 VdbeCursor *pC;
54923 BtCursor *pCrsr;
54924 int res;
54925 #endif /* local variables moved into u.bm */
54926
54927 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
54928 u.bm.pC = p->apCsr[pOp->p1];
54929 assert( u.bm.pC!=0 );
54930 if( (u.bm.pCrsr = u.bm.pC->pCursor)!=0 ){
54931 rc = sqlite3BtreeFirst(u.bm.pCrsr, &u.bm.res);
54932 u.bm.pC->atFirst = u.bm.res==0 ?1:0;
54933 u.bm.pC->deferredMoveto = 0;
54934 u.bm.pC->cacheStatus = CACHE_STALE;
54935 u.bm.pC->rowidIsValid = 0;
54936 }else{
54937 u.bm.res = 1;
54938 }
54939 u.bm.pC->nullRow = (u8)u.bm.res;
 
54940 assert( pOp->p2>0 && pOp->p2<p->nOp );
54941 if( u.bm.res ){
54942 pc = pOp->p2 - 1;
54943 }
54944 break;
54945 }
54946
@@ -55073,38 +54964,41 @@
54964 **
54965 ** The P1 cursor must be for a real table, not a pseudo-table.
54966 */
54967 case OP_Prev: /* jump */
54968 case OP_Next: { /* jump */
54969 #if 0 /* local variables moved into u.bn */
54970 VdbeCursor *pC;
54971 BtCursor *pCrsr;
54972 int res;
54973 #endif /* local variables moved into u.bn */
54974
54975 CHECK_FOR_INTERRUPT;
54976 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
54977 u.bn.pC = p->apCsr[pOp->p1];
54978 if( u.bn.pC==0 ){
54979 break; /* See ticket #2273 */
54980 }
54981 u.bn.pCrsr = u.bn.pC->pCursor;
54982 if( u.bn.pCrsr==0 ){
54983 u.bn.pC->nullRow = 1;
54984 break;
54985 }
54986 u.bn.res = 1;
54987 assert( u.bn.pC->deferredMoveto==0 );
54988 rc = pOp->opcode==OP_Next ? sqlite3BtreeNext(u.bn.pCrsr, &u.bn.res) :
54989 sqlite3BtreePrevious(u.bn.pCrsr, &u.bn.res);
54990 u.bn.pC->nullRow = (u8)u.bn.res;
54991 u.bn.pC->cacheStatus = CACHE_STALE;
54992 if( u.bn.res==0 ){
54993 pc = pOp->p2 - 1;
54994 if( pOp->p5 ) p->aCounter[pOp->p5-1]++;
54995 #ifdef SQLITE_TEST
54996 sqlite3_search_count++;
54997 #endif
54998 }
54999 u.bn.pC->rowidIsValid = 0;
55000 break;
55001 }
55002
55003 /* Opcode: IdxInsert P1 P2 P3 * P5
55004 **
@@ -55117,33 +55011,33 @@
55011 **
55012 ** This instruction only works for indices. The equivalent instruction
55013 ** for tables is OP_Insert.
55014 */
55015 case OP_IdxInsert: { /* in2 */
55016 #if 0 /* local variables moved into u.bo */
 
55017 VdbeCursor *pC;
55018 BtCursor *pCrsr;
55019 int nKey;
55020 const char *zKey;
55021 #endif /* local variables moved into u.bo */
55022
55023 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
55024 u.bo.pC = p->apCsr[pOp->p1];
55025 assert( u.bo.pC!=0 );
55026 assert( pIn2->flags & MEM_Blob );
55027 u.bo.pCrsr = u.bo.pC->pCursor;
55028 if( ALWAYS(u.bo.pCrsr!=0) ){
55029 assert( u.bo.pC->isTable==0 );
55030 rc = ExpandBlob(pIn2);
55031 if( rc==SQLITE_OK ){
55032 u.bo.nKey = pIn2->n;
55033 u.bo.zKey = pIn2->z;
55034 rc = sqlite3BtreeInsert(u.bo.pCrsr, u.bo.zKey, u.bo.nKey, "", 0, 0, pOp->p3,
55035 ((pOp->p5 & OPFLAG_USESEEKRESULT) ? u.bo.pC->seekResult : 0)
55036 );
55037 assert( u.bo.pC->deferredMoveto==0 );
55038 u.bo.pC->cacheStatus = CACHE_STALE;
55039 }
55040 }
55041 break;
55042 }
55043
@@ -55152,34 +55046,34 @@
55046 ** The content of P3 registers starting at register P2 form
55047 ** an unpacked index key. This opcode removes that entry from the
55048 ** index opened by cursor P1.
55049 */
55050 case OP_IdxDelete: {
55051 #if 0 /* local variables moved into u.bp */
 
55052 VdbeCursor *pC;
55053 BtCursor *pCrsr;
55054 int res;
55055 UnpackedRecord r;
55056 #endif /* local variables moved into u.bp */
55057
 
55058 assert( pOp->p3>0 );
55059 assert( pOp->p2>0 && pOp->p2+pOp->p3<=p->nMem+1 );
55060 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
55061 u.bp.pC = p->apCsr[pOp->p1];
55062 assert( u.bp.pC!=0 );
55063 u.bp.pCrsr = u.bp.pC->pCursor;
55064 if( ALWAYS(u.bp.pCrsr!=0) ){
55065 u.bp.r.pKeyInfo = u.bp.pC->pKeyInfo;
55066 u.bp.r.nField = (u16)pOp->p3;
55067 u.bp.r.flags = 0;
55068 u.bp.r.aMem = &p->aMem[pOp->p2];
55069 rc = sqlite3BtreeMovetoUnpacked(u.bp.pCrsr, &u.bp.r, 0, 0, &u.bp.res);
55070 if( rc==SQLITE_OK && u.bp.res==0 ){
55071 rc = sqlite3BtreeDelete(u.bp.pCrsr);
55072 }
55073 assert( u.bp.pC->deferredMoveto==0 );
55074 u.bp.pC->cacheStatus = CACHE_STALE;
55075 }
55076 break;
55077 }
55078
55079 /* Opcode: IdxRowid P1 P2 * * *
@@ -55189,32 +55083,32 @@
55083 ** the rowid of the table entry to which this index entry points.
55084 **
55085 ** See also: Rowid, MakeRecord.
55086 */
55087 case OP_IdxRowid: { /* out2-prerelease */
55088 #if 0 /* local variables moved into u.bq */
 
55089 BtCursor *pCrsr;
55090 VdbeCursor *pC;
55091 i64 rowid;
55092 #endif /* local variables moved into u.bq */
55093
55094 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
55095 u.bq.pC = p->apCsr[pOp->p1];
55096 assert( u.bq.pC!=0 );
55097 u.bq.pCrsr = u.bq.pC->pCursor;
55098 if( ALWAYS(u.bq.pCrsr!=0) ){
55099 rc = sqlite3VdbeCursorMoveto(u.bq.pC);
55100 if( NEVER(rc) ) goto abort_due_to_error;
55101 assert( u.bq.pC->deferredMoveto==0 );
55102 assert( u.bq.pC->isTable==0 );
55103 if( !u.bq.pC->nullRow ){
55104 rc = sqlite3VdbeIdxRowid(db, u.bq.pCrsr, &u.bq.rowid);
55105 if( rc!=SQLITE_OK ){
55106 goto abort_due_to_error;
55107 }
55108 MemSetTypeFlag(pOut, MEM_Int);
55109 pOut->u.i = u.bq.rowid;
55110 }
55111 }
55112 break;
55113 }
55114
@@ -55244,40 +55138,39 @@
55138 ** If P5 is non-zero then the key value is increased by an epsilon prior
55139 ** to the comparison. This makes the opcode work like IdxLE.
55140 */
55141 case OP_IdxLT: /* jump, in3 */
55142 case OP_IdxGE: { /* jump, in3 */
55143 #if 0 /* local variables moved into u.br */
 
55144 VdbeCursor *pC;
55145 int res;
55146 UnpackedRecord r;
55147 #endif /* local variables moved into u.br */
55148
55149 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
55150 u.br.pC = p->apCsr[pOp->p1];
55151 assert( u.br.pC!=0 );
55152 if( ALWAYS(u.br.pC->pCursor!=0) ){
55153 assert( u.br.pC->deferredMoveto==0 );
55154 assert( pOp->p5==0 || pOp->p5==1 );
55155 assert( pOp->p4type==P4_INT32 );
55156 u.br.r.pKeyInfo = u.br.pC->pKeyInfo;
55157 u.br.r.nField = (u16)pOp->p4.i;
55158 if( pOp->p5 ){
55159 u.br.r.flags = UNPACKED_INCRKEY | UNPACKED_IGNORE_ROWID;
55160 }else{
55161 u.br.r.flags = UNPACKED_IGNORE_ROWID;
55162 }
55163 u.br.r.aMem = &p->aMem[pOp->p3];
55164 rc = sqlite3VdbeIdxKeyCompare(u.br.pC, &u.br.r, &u.br.res);
55165 if( pOp->opcode==OP_IdxLT ){
55166 u.br.res = -u.br.res;
55167 }else{
55168 assert( pOp->opcode==OP_IdxGE );
55169 u.br.res++;
55170 }
55171 if( u.br.res>0 ){
55172 pc = pOp->p2 - 1 ;
55173 }
55174 }
55175 break;
55176 }
@@ -55301,39 +55194,39 @@
55194 ** If AUTOVACUUM is disabled then a zero is stored in register P2.
55195 **
55196 ** See also: Clear
55197 */
55198 case OP_Destroy: { /* out2-prerelease */
55199 #if 0 /* local variables moved into u.bs */
55200 int iMoved;
55201 int iCnt;
55202 Vdbe *pVdbe;
55203 int iDb;
55204 #endif /* local variables moved into u.bs */
55205 #ifndef SQLITE_OMIT_VIRTUALTABLE
55206 u.bs.iCnt = 0;
55207 for(u.bs.pVdbe=db->pVdbe; u.bs.pVdbe; u.bs.pVdbe = u.bs.pVdbe->pNext){
55208 if( u.bs.pVdbe->magic==VDBE_MAGIC_RUN && u.bs.pVdbe->inVtabMethod<2 && u.bs.pVdbe->pc>=0 ){
55209 u.bs.iCnt++;
55210 }
55211 }
55212 #else
55213 u.bs.iCnt = db->activeVdbeCnt;
55214 #endif
55215 if( u.bs.iCnt>1 ){
55216 rc = SQLITE_LOCKED;
55217 p->errorAction = OE_Abort;
55218 }else{
55219 u.bs.iDb = pOp->p3;
55220 assert( u.bs.iCnt==1 );
55221 assert( (p->btreeMask & (1<<u.bs.iDb))!=0 );
55222 rc = sqlite3BtreeDropTable(db->aDb[u.bs.iDb].pBt, pOp->p1, &u.bs.iMoved);
55223 MemSetTypeFlag(pOut, MEM_Int);
55224 pOut->u.i = u.bs.iMoved;
55225 #ifndef SQLITE_OMIT_AUTOVACUUM
55226 if( rc==SQLITE_OK && u.bs.iMoved!=0 ){
55227 sqlite3RootPageMoved(&db->aDb[u.bs.iDb], u.bs.iMoved, pOp->p1);
55228 }
55229 #endif
55230 }
55231 break;
55232 }
@@ -55355,23 +55248,23 @@
55248 ** also incremented by the number of rows in the table being cleared.
55249 **
55250 ** See also: Destroy
55251 */
55252 case OP_Clear: {
55253 #if 0 /* local variables moved into u.bt */
55254 int nChange;
55255 #endif /* local variables moved into u.bt */
55256
55257 u.bt.nChange = 0;
55258 assert( (p->btreeMask & (1<<pOp->p2))!=0 );
55259 rc = sqlite3BtreeClearTable(
55260 db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &u.bt.nChange : 0)
55261 );
55262 if( pOp->p3 ){
55263 p->nChange += u.bt.nChange;
55264 if( pOp->p3>0 ){
55265 p->aMem[pOp->p3].u.i += u.bt.nChange;
55266 }
55267 }
55268 break;
55269 }
55270
@@ -55397,29 +55290,29 @@
55290 **
55291 ** See documentation on OP_CreateTable for additional information.
55292 */
55293 case OP_CreateIndex: /* out2-prerelease */
55294 case OP_CreateTable: { /* out2-prerelease */
55295 #if 0 /* local variables moved into u.bu */
55296 int pgno;
55297 int flags;
55298 Db *pDb;
55299 #endif /* local variables moved into u.bu */
55300
55301 u.bu.pgno = 0;
55302 assert( pOp->p1>=0 && pOp->p1<db->nDb );
55303 assert( (p->btreeMask & (1<<pOp->p1))!=0 );
55304 u.bu.pDb = &db->aDb[pOp->p1];
55305 assert( u.bu.pDb->pBt!=0 );
55306 if( pOp->opcode==OP_CreateTable ){
55307 /* u.bu.flags = BTREE_INTKEY; */
55308 u.bu.flags = BTREE_LEAFDATA|BTREE_INTKEY;
55309 }else{
55310 u.bu.flags = BTREE_ZERODATA;
55311 }
55312 rc = sqlite3BtreeCreateTable(u.bu.pDb->pBt, &u.bu.pgno, u.bu.flags);
55313 pOut->u.i = u.bu.pgno;
55314 MemSetTypeFlag(pOut, MEM_Int);
55315 break;
55316 }
55317
55318 /* Opcode: ParseSchema P1 P2 * P4 *
@@ -55433,62 +55326,62 @@
55326 **
55327 ** This opcode invokes the parser to create a new virtual machine,
55328 ** then runs the new virtual machine. It is thus a re-entrant opcode.
55329 */
55330 case OP_ParseSchema: {
55331 #if 0 /* local variables moved into u.bv */
55332 int iDb;
55333 const char *zMaster;
55334 char *zSql;
55335 InitData initData;
55336 #endif /* local variables moved into u.bv */
55337
55338 u.bv.iDb = pOp->p1;
55339 assert( u.bv.iDb>=0 && u.bv.iDb<db->nDb );
55340
55341 /* If pOp->p2 is 0, then this opcode is being executed to read a
55342 ** single row, for example the row corresponding to a new index
55343 ** created by this VDBE, from the sqlite_master table. It only
55344 ** does this if the corresponding in-memory schema is currently
55345 ** loaded. Otherwise, the new index definition can be loaded along
55346 ** with the rest of the schema when it is required.
55347 **
55348 ** Although the mutex on the BtShared object that corresponds to
55349 ** database u.bv.iDb (the database containing the sqlite_master table
55350 ** read by this instruction) is currently held, it is necessary to
55351 ** obtain the mutexes on all attached databases before checking if
55352 ** the schema of u.bv.iDb is loaded. This is because, at the start of
55353 ** the sqlite3_exec() call below, SQLite will invoke
55354 ** sqlite3BtreeEnterAll(). If all mutexes are not already held, the
55355 ** u.bv.iDb mutex may be temporarily released to avoid deadlock. If
55356 ** this happens, then some other thread may delete the in-memory
55357 ** schema of database u.bv.iDb before the SQL statement runs. The schema
55358 ** will not be reloaded becuase the db->init.busy flag is set. This
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,
55370 "SELECT name, rootpage, sql FROM '%q'.%s WHERE %s",
55371 db->aDb[u.bv.iDb].zName, u.bv.zMaster, pOp->p4.z);
55372 if( u.bv.zSql==0 ){
55373 rc = SQLITE_NOMEM;
55374 }else{
55375 (void)sqlite3SafetyOff(db);
55376 assert( db->init.busy==0 );
55377 db->init.busy = 1;
55378 u.bv.initData.rc = SQLITE_OK;
55379 assert( !db->mallocFailed );
55380 rc = sqlite3_exec(db, u.bv.zSql, sqlite3InitCallback, &u.bv.initData, 0);
55381 if( rc==SQLITE_OK ) rc = u.bv.initData.rc;
55382 sqlite3DbFree(db, u.bv.zSql);
55383 db->init.busy = 0;
55384 (void)sqlite3SafetyOn(db);
55385 }
55386 }
55387 sqlite3BtreeLeaveAll(db);
@@ -55496,11 +55389,11 @@
55389 goto no_mem;
55390 }
55391 break;
55392 }
55393
55394 #if !defined(SQLITE_OMIT_ANALYZE)
55395 /* Opcode: LoadAnalysis P1 * * * *
55396 **
55397 ** Read the sqlite_stat1 table for database P1 and load the content
55398 ** of that table into the internal index hash table. This will cause
55399 ** the analysis to be used when preparing all subsequent queries.
@@ -55508,11 +55401,11 @@
55401 case OP_LoadAnalysis: {
55402 assert( pOp->p1>=0 && pOp->p1<db->nDb );
55403 rc = sqlite3AnalysisLoad(db, pOp->p1);
55404 break;
55405 }
55406 #endif /* !defined(SQLITE_OMIT_ANALYZE) */
55407
55408 /* Opcode: DropTable P1 * * P4 *
55409 **
55410 ** Remove the internal (in-memory) data structures that describe
55411 ** the table named P4 in database P1. This is called after a table
@@ -55569,45 +55462,45 @@
55462 ** file, not the main database file.
55463 **
55464 ** This opcode is used to implement the integrity_check pragma.
55465 */
55466 case OP_IntegrityCk: {
55467 #if 0 /* local variables moved into u.bw */
55468 int nRoot; /* Number of tables to check. (Number of root pages.) */
55469 int *aRoot; /* Array of rootpage numbers for tables to be checked */
55470 int j; /* Loop counter */
55471 int nErr; /* Number of errors reported */
55472 char *z; /* Text of the error report */
55473 Mem *pnErr; /* Register keeping track of errors remaining */
55474 #endif /* local variables moved into u.bw */
55475
55476 u.bw.nRoot = pOp->p2;
55477 assert( u.bw.nRoot>0 );
55478 u.bw.aRoot = sqlite3DbMallocRaw(db, sizeof(int)*(u.bw.nRoot+1) );
55479 if( u.bw.aRoot==0 ) goto no_mem;
55480 assert( pOp->p3>0 && pOp->p3<=p->nMem );
55481 u.bw.pnErr = &p->aMem[pOp->p3];
55482 assert( (u.bw.pnErr->flags & MEM_Int)!=0 );
55483 assert( (u.bw.pnErr->flags & (MEM_Str|MEM_Blob))==0 );
55484 pIn1 = &p->aMem[pOp->p1];
55485 for(u.bw.j=0; u.bw.j<u.bw.nRoot; u.bw.j++){
55486 u.bw.aRoot[u.bw.j] = (int)sqlite3VdbeIntValue(&pIn1[u.bw.j]);
55487 }
55488 u.bw.aRoot[u.bw.j] = 0;
55489 assert( pOp->p5<db->nDb );
55490 assert( (p->btreeMask & (1<<pOp->p5))!=0 );
55491 u.bw.z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p5].pBt, u.bw.aRoot, u.bw.nRoot,
55492 (int)u.bw.pnErr->u.i, &u.bw.nErr);
55493 sqlite3DbFree(db, u.bw.aRoot);
55494 u.bw.pnErr->u.i -= u.bw.nErr;
55495 sqlite3VdbeMemSetNull(pIn1);
55496 if( u.bw.nErr==0 ){
55497 assert( u.bw.z==0 );
55498 }else if( u.bw.z==0 ){
55499 goto no_mem;
55500 }else{
55501 sqlite3VdbeMemSetStr(pIn1, u.bw.z, -1, SQLITE_UTF8, sqlite3_free);
55502 }
55503 UPDATE_MAX_BLOBSIZE(pIn1);
55504 sqlite3VdbeChangeEncoding(pIn1, encoding);
55505 break;
55506 }
@@ -55619,24 +55512,24 @@
55512 ** held in register P1.
55513 **
55514 ** An assertion fails if P2 is not an integer.
55515 */
55516 case OP_RowSetAdd: { /* in2 */
55517 #if 0 /* local variables moved into u.bx */
55518 Mem *pIdx;
55519 Mem *pVal;
55520 #endif /* local variables moved into u.bx */
55521 assert( pOp->p1>0 && pOp->p1<=p->nMem );
55522 u.bx.pIdx = &p->aMem[pOp->p1];
55523 assert( pOp->p2>0 && pOp->p2<=p->nMem );
55524 u.bx.pVal = &p->aMem[pOp->p2];
55525 assert( (u.bx.pVal->flags & MEM_Int)!=0 );
55526 if( (u.bx.pIdx->flags & MEM_RowSet)==0 ){
55527 sqlite3VdbeMemSetRowSet(u.bx.pIdx);
55528 if( (u.bx.pIdx->flags & MEM_RowSet)==0 ) goto no_mem;
55529 }
55530 sqlite3RowSetInsert(u.bx.pIdx->u.pRowSet, u.bx.pVal->u.i);
55531 break;
55532 }
55533
55534 /* Opcode: RowSetRead P1 P2 P3 * *
55535 **
@@ -55643,28 +55536,28 @@
55536 ** Extract the smallest value from boolean index P1 and put that value into
55537 ** register P3. Or, if boolean index P1 is initially empty, leave P3
55538 ** unchanged and jump to instruction P2.
55539 */
55540 case OP_RowSetRead: { /* jump, out3 */
55541 #if 0 /* local variables moved into u.by */
55542 Mem *pIdx;
55543 i64 val;
55544 #endif /* local variables moved into u.by */
55545 assert( pOp->p1>0 && pOp->p1<=p->nMem );
55546 CHECK_FOR_INTERRUPT;
55547 u.by.pIdx = &p->aMem[pOp->p1];
55548 pOut = &p->aMem[pOp->p3];
55549 if( (u.by.pIdx->flags & MEM_RowSet)==0
55550 || sqlite3RowSetNext(u.by.pIdx->u.pRowSet, &u.by.val)==0
55551 ){
55552 /* The boolean index is empty */
55553 sqlite3VdbeMemSetNull(u.by.pIdx);
55554 pc = pOp->p2 - 1;
55555 }else{
55556 /* A value was pulled from the index */
55557 assert( pOp->p3>0 && pOp->p3<=p->nMem );
55558 sqlite3VdbeMemSetInt64(pOut, u.by.val);
55559 }
55560 break;
55561 }
55562
55563 /* Opcode: RowSetTest P1 P2 P3 P4
@@ -55689,16 +55582,16 @@
55582 ** inserted, there is no need to search to see if the same value was
55583 ** previously inserted as part of set X (only if it was previously
55584 ** inserted as part of some other set).
55585 */
55586 case OP_RowSetTest: { /* jump, in1, in3 */
55587 #if 0 /* local variables moved into u.bz */
55588 int iSet;
55589 int exists;
55590 #endif /* local variables moved into u.bz */
55591
55592 u.bz.iSet = pOp->p4.i;
55593 assert( pIn3->flags&MEM_Int );
55594
55595 /* If there is anything other than a rowset object in memory cell P1,
55596 ** delete it now and initialize P1 with an empty rowset
55597 */
@@ -55706,21 +55599,21 @@
55599 sqlite3VdbeMemSetRowSet(pIn1);
55600 if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem;
55601 }
55602
55603 assert( pOp->p4type==P4_INT32 );
55604 assert( u.bz.iSet==-1 || u.bz.iSet>=0 );
55605 if( u.bz.iSet ){
55606 u.bz.exists = sqlite3RowSetTest(pIn1->u.pRowSet,
55607 (u8)(u.bz.iSet>=0 ? u.bz.iSet & 0xf : 0xff),
55608 pIn3->u.i);
55609 if( u.bz.exists ){
55610 pc = pOp->p2 - 1;
55611 break;
55612 }
55613 }
55614 if( u.bz.iSet>=0 ){
55615 sqlite3RowSetInsert(pIn1->u.pRowSet, pIn3->u.i);
55616 }
55617 break;
55618 }
55619
@@ -55731,27 +55624,27 @@
55624 ** Save the current Vdbe context such that it can be restored by a ContextPop
55625 ** opcode. The context stores the last insert row id, the last statement change
55626 ** count, and the current statement change count.
55627 */
55628 case OP_ContextPush: {
55629 #if 0 /* local variables moved into u.ca */
55630 int i;
55631 Context *pContext;
55632 #endif /* local variables moved into u.ca */
55633
55634 u.ca.i = p->contextStackTop++;
55635 assert( u.ca.i>=0 );
55636 /* FIX ME: This should be allocated as part of the vdbe at compile-time */
55637 if( u.ca.i>=p->contextStackDepth ){
55638 p->contextStackDepth = u.ca.i+1;
55639 p->contextStack = sqlite3DbReallocOrFree(db, p->contextStack,
55640 sizeof(Context)*(u.ca.i+1));
55641 if( p->contextStack==0 ) goto no_mem;
55642 }
55643 u.ca.pContext = &p->contextStack[u.ca.i];
55644 u.ca.pContext->lastRowid = db->lastRowid;
55645 u.ca.pContext->nChange = p->nChange;
55646 break;
55647 }
55648
55649 /* Opcode: ContextPop * * *
55650 **
@@ -55758,17 +55651,17 @@
55651 ** Restore the Vdbe context to the state it was in when contextPush was last
55652 ** executed. The context stores the last insert row id, the last statement
55653 ** change count, and the current statement change count.
55654 */
55655 case OP_ContextPop: {
55656 #if 0 /* local variables moved into u.cb */
55657 Context *pContext;
55658 #endif /* local variables moved into u.cb */
55659 u.cb.pContext = &p->contextStack[--p->contextStackTop];
55660 assert( p->contextStackTop>=0 );
55661 db->lastRowid = u.cb.pContext->lastRowid;
55662 p->nChange = u.cb.pContext->nChange;
55663 break;
55664 }
55665 #endif /* #ifndef SQLITE_OMIT_TRIGGER */
55666
55667 #ifndef SQLITE_OMIT_AUTOINCREMENT
@@ -55844,51 +55737,51 @@
55737 **
55738 ** The P5 arguments are taken from register P2 and its
55739 ** successors.
55740 */
55741 case OP_AggStep: {
55742 #if 0 /* local variables moved into u.cc */
55743 int n;
55744 int i;
55745 Mem *pMem;
55746 Mem *pRec;
55747 sqlite3_context ctx;
55748 sqlite3_value **apVal;
55749 #endif /* local variables moved into u.cc */
55750
55751 u.cc.n = pOp->p5;
55752 assert( u.cc.n>=0 );
55753 u.cc.pRec = &p->aMem[pOp->p2];
55754 u.cc.apVal = p->apArg;
55755 assert( u.cc.apVal || u.cc.n==0 );
55756 for(u.cc.i=0; u.cc.i<u.cc.n; u.cc.i++, u.cc.pRec++){
55757 u.cc.apVal[u.cc.i] = u.cc.pRec;
55758 storeTypeInfo(u.cc.pRec, encoding);
55759 }
55760 u.cc.ctx.pFunc = pOp->p4.pFunc;
55761 assert( pOp->p3>0 && pOp->p3<=p->nMem );
55762 u.cc.ctx.pMem = u.cc.pMem = &p->aMem[pOp->p3];
55763 u.cc.pMem->n++;
55764 u.cc.ctx.s.flags = MEM_Null;
55765 u.cc.ctx.s.z = 0;
55766 u.cc.ctx.s.zMalloc = 0;
55767 u.cc.ctx.s.xDel = 0;
55768 u.cc.ctx.s.db = db;
55769 u.cc.ctx.isError = 0;
55770 u.cc.ctx.pColl = 0;
55771 if( u.cc.ctx.pFunc->flags & SQLITE_FUNC_NEEDCOLL ){
55772 assert( pOp>p->aOp );
55773 assert( pOp[-1].p4type==P4_COLLSEQ );
55774 assert( pOp[-1].opcode==OP_CollSeq );
55775 u.cc.ctx.pColl = pOp[-1].p4.pColl;
55776 }
55777 (u.cc.ctx.pFunc->xStep)(&u.cc.ctx, u.cc.n, u.cc.apVal);
55778 if( u.cc.ctx.isError ){
55779 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&u.cc.ctx.s));
55780 rc = u.cc.ctx.isError;
55781 }
55782 sqlite3VdbeMemRelease(&u.cc.ctx.s);
55783 break;
55784 }
55785
55786 /* Opcode: AggFinal P1 P2 * P4 *
55787 **
@@ -55901,23 +55794,23 @@
55794 ** functions that can take varying numbers of arguments. The
55795 ** P4 argument is only needed for the degenerate case where
55796 ** the step function was not previously called.
55797 */
55798 case OP_AggFinal: {
55799 #if 0 /* local variables moved into u.cd */
55800 Mem *pMem;
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) ){
55812 goto too_big;
55813 }
55814 break;
55815 }
55816
@@ -55943,18 +55836,18 @@
55836 ** Perform a single step of the incremental vacuum procedure on
55837 ** the P1 database. If the vacuum has finished, jump to instruction
55838 ** P2. Otherwise, fall through to the next instruction.
55839 */
55840 case OP_IncrVacuum: { /* jump */
55841 #if 0 /* local variables moved into u.ce */
55842 Btree *pBt;
55843 #endif /* local variables moved into u.ce */
55844
55845 assert( pOp->p1>=0 && pOp->p1<db->nDb );
55846 assert( (p->btreeMask & (1<<pOp->p1))!=0 );
55847 u.ce.pBt = db->aDb[pOp->p1].pBt;
55848 rc = sqlite3BtreeIncrVacuum(u.ce.pBt);
55849 if( rc==SQLITE_DONE ){
55850 pc = pOp->p2 - 1;
55851 rc = SQLITE_OK;
55852 }
55853 break;
@@ -55993,21 +55886,21 @@
55886 **
55887 ** P4 contains a pointer to the name of the table being locked. This is only
55888 ** used to generate an error message if the lock cannot be obtained.
55889 */
55890 case OP_TableLock: {
55891 #if 0 /* local variables moved into u.cf */
55892 int p1;
55893 u8 isWriteLock;
55894 #endif /* local variables moved into u.cf */
55895
55896 u.cf.p1 = pOp->p1;
55897 u.cf.isWriteLock = (u8)pOp->p3;
55898 assert( u.cf.p1>=0 && u.cf.p1<db->nDb );
55899 assert( (p->btreeMask & (1<<u.cf.p1))!=0 );
55900 assert( u.cf.isWriteLock==0 || u.cf.isWriteLock==1 );
55901 rc = sqlite3BtreeLockTable(db->aDb[u.cf.p1].pBt, pOp->p2, u.cf.isWriteLock);
55902 if( (rc&0xFF)==SQLITE_LOCKED ){
55903 const char *z = pOp->p4.z;
55904 sqlite3SetString(&p->zErrMsg, db, "database table is locked: %s", z);
55905 }
55906 break;
@@ -56023,19 +55916,19 @@
55916 ** Also, whether or not P4 is set, check that this is not being called from
55917 ** within a callback to a virtual table xSync() method. If it is, the error
55918 ** code will be set to SQLITE_LOCKED.
55919 */
55920 case OP_VBegin: {
55921 #if 0 /* local variables moved into u.cg */
55922 sqlite3_vtab *pVtab;
55923 #endif /* local variables moved into u.cg */
55924 u.cg.pVtab = pOp->p4.pVtab;
55925 rc = sqlite3VtabBegin(db, u.cg.pVtab);
55926 if( u.cg.pVtab ){
55927 sqlite3DbFree(db, p->zErrMsg);
55928 p->zErrMsg = u.cg.pVtab->zErrMsg;
55929 u.cg.pVtab->zErrMsg = 0;
55930 }
55931 break;
55932 }
55933 #endif /* SQLITE_OMIT_VIRTUALTABLE */
55934
@@ -56071,40 +55964,40 @@
55964 ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
55965 ** P1 is a cursor number. This opcode opens a cursor to the virtual
55966 ** table and stores that cursor in P1.
55967 */
55968 case OP_VOpen: {
55969 #if 0 /* local variables moved into u.ch */
55970 VdbeCursor *pCur;
55971 sqlite3_vtab_cursor *pVtabCursor;
55972 sqlite3_vtab *pVtab;
55973 sqlite3_module *pModule;
55974 #endif /* local variables moved into u.ch */
55975
55976 u.ch.pCur = 0;
55977 u.ch.pVtabCursor = 0;
55978 u.ch.pVtab = pOp->p4.pVtab;
55979 u.ch.pModule = (sqlite3_module *)u.ch.pVtab->pModule;
55980 assert(u.ch.pVtab && u.ch.pModule);
55981 if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
55982 rc = u.ch.pModule->xOpen(u.ch.pVtab, &u.ch.pVtabCursor);
55983 sqlite3DbFree(db, p->zErrMsg);
55984 p->zErrMsg = u.ch.pVtab->zErrMsg;
55985 u.ch.pVtab->zErrMsg = 0;
55986 if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
55987 if( SQLITE_OK==rc ){
55988 /* Initialize sqlite3_vtab_cursor base class */
55989 u.ch.pVtabCursor->pVtab = u.ch.pVtab;
55990
55991 /* Initialise vdbe cursor object */
55992 u.ch.pCur = allocateCursor(p, pOp->p1, 0, -1, 0);
55993 if( u.ch.pCur ){
55994 u.ch.pCur->pVtabCursor = u.ch.pVtabCursor;
55995 u.ch.pCur->pModule = u.ch.pVtabCursor->pVtab->pModule;
55996 }else{
55997 db->mallocFailed = 1;
55998 u.ch.pModule->xClose(u.ch.pVtabCursor);
55999 }
56000 }
56001 break;
56002 }
56003 #endif /* SQLITE_OMIT_VIRTUALTABLE */
@@ -56127,11 +56020,11 @@
56020 ** xFilter as argv. Register P3+2 becomes argv[0] when passed to xFilter.
56021 **
56022 ** A jump is made to P2 if the result set after filtering would be empty.
56023 */
56024 case OP_VFilter: { /* jump */
56025 #if 0 /* local variables moved into u.ci */
56026 int nArg;
56027 int iQuery;
56028 const sqlite3_module *pModule;
56029 Mem *pQuery;
56030 Mem *pArgc;
@@ -56139,54 +56032,54 @@
56032 sqlite3_vtab *pVtab;
56033 VdbeCursor *pCur;
56034 int res;
56035 int i;
56036 Mem **apArg;
56037 #endif /* local variables moved into u.ci */
56038
56039 u.ci.pQuery = &p->aMem[pOp->p3];
56040 u.ci.pArgc = &u.ci.pQuery[1];
56041 u.ci.pCur = p->apCsr[pOp->p1];
56042 REGISTER_TRACE(pOp->p3, u.ci.pQuery);
56043 assert( u.ci.pCur->pVtabCursor );
56044 u.ci.pVtabCursor = u.ci.pCur->pVtabCursor;
56045 u.ci.pVtab = u.ci.pVtabCursor->pVtab;
56046 u.ci.pModule = u.ci.pVtab->pModule;
56047
56048 /* Grab the index number and argc parameters */
56049 assert( (u.ci.pQuery->flags&MEM_Int)!=0 && u.ci.pArgc->flags==MEM_Int );
56050 u.ci.nArg = (int)u.ci.pArgc->u.i;
56051 u.ci.iQuery = (int)u.ci.pQuery->u.i;
56052
56053 /* Invoke the xFilter method */
56054 {
56055 u.ci.res = 0;
56056 u.ci.apArg = p->apArg;
56057 for(u.ci.i = 0; u.ci.i<u.ci.nArg; u.ci.i++){
56058 u.ci.apArg[u.ci.i] = &u.ci.pArgc[u.ci.i+1];
56059 storeTypeInfo(u.ci.apArg[u.ci.i], 0);
56060 }
56061
56062 if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
56063 sqlite3VtabLock(u.ci.pVtab);
56064 p->inVtabMethod = 1;
56065 rc = u.ci.pModule->xFilter(u.ci.pVtabCursor, u.ci.iQuery, pOp->p4.z, u.ci.nArg, u.ci.apArg);
56066 p->inVtabMethod = 0;
56067 sqlite3DbFree(db, p->zErrMsg);
56068 p->zErrMsg = u.ci.pVtab->zErrMsg;
56069 u.ci.pVtab->zErrMsg = 0;
56070 sqlite3VtabUnlock(db, u.ci.pVtab);
56071 if( rc==SQLITE_OK ){
56072 u.ci.res = u.ci.pModule->xEof(u.ci.pVtabCursor);
56073 }
56074 if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
56075
56076 if( u.ci.res ){
56077 pc = pOp->p2 - 1;
56078 }
56079 }
56080 u.ci.pCur->nullRow = 0;
56081
56082 break;
56083 }
56084 #endif /* SQLITE_OMIT_VIRTUALTABLE */
56085
@@ -56196,57 +56089,57 @@
56089 ** Store the value of the P2-th column of
56090 ** the row of the virtual-table that the
56091 ** P1 cursor is pointing to into register P3.
56092 */
56093 case OP_VColumn: {
56094 #if 0 /* local variables moved into u.cj */
56095 sqlite3_vtab *pVtab;
56096 const sqlite3_module *pModule;
56097 Mem *pDest;
56098 sqlite3_context sContext;
56099 #endif /* local variables moved into u.cj */
56100
56101 VdbeCursor *pCur = p->apCsr[pOp->p1];
56102 assert( pCur->pVtabCursor );
56103 assert( pOp->p3>0 && pOp->p3<=p->nMem );
56104 u.cj.pDest = &p->aMem[pOp->p3];
56105 if( pCur->nullRow ){
56106 sqlite3VdbeMemSetNull(u.cj.pDest);
56107 break;
56108 }
56109 u.cj.pVtab = pCur->pVtabCursor->pVtab;
56110 u.cj.pModule = u.cj.pVtab->pModule;
56111 assert( u.cj.pModule->xColumn );
56112 memset(&u.cj.sContext, 0, sizeof(u.cj.sContext));
56113
56114 /* The output cell may already have a buffer allocated. Move
56115 ** the current contents to u.cj.sContext.s so in case the user-function
56116 ** can use the already allocated buffer instead of allocating a
56117 ** new one.
56118 */
56119 sqlite3VdbeMemMove(&u.cj.sContext.s, u.cj.pDest);
56120 MemSetTypeFlag(&u.cj.sContext.s, MEM_Null);
56121
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 */
56132 sqlite3VdbeChangeEncoding(&u.cj.sContext.s, encoding);
56133 REGISTER_TRACE(pOp->p3, u.cj.pDest);
56134 sqlite3VdbeMemMove(u.cj.pDest, &u.cj.sContext.s);
56135 UPDATE_MAX_BLOBSIZE(u.cj.pDest);
56136
56137 if( sqlite3SafetyOn(db) ){
56138 goto abort_due_to_misuse;
56139 }
56140 if( sqlite3VdbeMemTooBig(u.cj.pDest) ){
56141 goto too_big;
56142 }
56143 break;
56144 }
56145 #endif /* SQLITE_OMIT_VIRTUALTABLE */
@@ -56257,48 +56150,48 @@
56150 ** Advance virtual table P1 to the next row in its result set and
56151 ** jump to instruction P2. Or, if the virtual table has reached
56152 ** the end of its result set, then fall through to the next instruction.
56153 */
56154 case OP_VNext: { /* jump */
56155 #if 0 /* local variables moved into u.ck */
56156 sqlite3_vtab *pVtab;
56157 const sqlite3_module *pModule;
56158 int res;
56159 VdbeCursor *pCur;
56160 #endif /* local variables moved into u.ck */
56161
56162 u.ck.res = 0;
56163 u.ck.pCur = p->apCsr[pOp->p1];
56164 assert( u.ck.pCur->pVtabCursor );
56165 if( u.ck.pCur->nullRow ){
56166 break;
56167 }
56168 u.ck.pVtab = u.ck.pCur->pVtabCursor->pVtab;
56169 u.ck.pModule = u.ck.pVtab->pModule;
56170 assert( u.ck.pModule->xNext );
56171
56172 /* Invoke the xNext() method of the module. There is no way for the
56173 ** underlying implementation to return an error if one occurs during
56174 ** xNext(). Instead, if an error occurs, true is returned (indicating that
56175 ** data is available) and the error code returned when xColumn or
56176 ** some other method is next invoked on the save virtual table cursor.
56177 */
56178 if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
56179 sqlite3VtabLock(u.ck.pVtab);
56180 p->inVtabMethod = 1;
56181 rc = u.ck.pModule->xNext(u.ck.pCur->pVtabCursor);
56182 p->inVtabMethod = 0;
56183 sqlite3DbFree(db, p->zErrMsg);
56184 p->zErrMsg = u.ck.pVtab->zErrMsg;
56185 u.ck.pVtab->zErrMsg = 0;
56186 sqlite3VtabUnlock(db, u.ck.pVtab);
56187 if( rc==SQLITE_OK ){
56188 u.ck.res = u.ck.pModule->xEof(u.ck.pCur->pVtabCursor);
56189 }
56190 if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
56191
56192 if( !u.ck.res ){
56193 /* If there is data, jump to P2 */
56194 pc = pOp->p2 - 1;
56195 }
56196 break;
56197 }
@@ -56310,29 +56203,27 @@
56203 ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
56204 ** This opcode invokes the corresponding xRename method. The value
56205 ** in register P1 is passed as the zName argument to the xRename method.
56206 */
56207 case OP_VRename: {
56208 #if 0 /* local variables moved into u.cl */
56209 sqlite3_vtab *pVtab;
56210 Mem *pName;
56211 #endif /* local variables moved into u.cl */
56212
56213 u.cl.pVtab = pOp->p4.pVtab;
56214 u.cl.pName = &p->aMem[pOp->p1];
56215 assert( u.cl.pVtab->pModule->xRename );
56216 REGISTER_TRACE(pOp->p1, u.cl.pName);
56217 assert( u.cl.pName->flags & MEM_Str );
 
 
56218 if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
56219 sqlite3VtabLock(u.cl.pVtab);
56220 rc = u.cl.pVtab->pModule->xRename(u.cl.pVtab, u.cl.pName->z);
56221 sqlite3DbFree(db, p->zErrMsg);
56222 p->zErrMsg = u.cl.pVtab->zErrMsg;
56223 u.cl.pVtab->zErrMsg = 0;
56224 sqlite3VtabUnlock(db, u.cl.pVtab);
56225 if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
56226
56227 break;
56228 }
56229 #endif
@@ -56360,46 +56251,43 @@
56251 ** P1 is a boolean flag. If it is set to true and the xUpdate call
56252 ** is successful, then the value returned by sqlite3_last_insert_rowid()
56253 ** is set to the value of the rowid for the row just inserted.
56254 */
56255 case OP_VUpdate: {
56256 #if 0 /* local variables moved into u.cm */
56257 sqlite3_vtab *pVtab;
56258 sqlite3_module *pModule;
56259 int nArg;
56260 int i;
56261 sqlite_int64 rowid;
56262 Mem **apArg;
56263 Mem *pX;
56264 #endif /* local variables moved into u.cm */
56265
56266 u.cm.pVtab = pOp->p4.pVtab;
56267 u.cm.pModule = (sqlite3_module *)u.cm.pVtab->pModule;
56268 u.cm.nArg = pOp->p2;
56269 assert( pOp->p4type==P4_VTAB );
56270 if( ALWAYS(u.cm.pModule->xUpdate) ){
56271 u.cm.apArg = p->apArg;
56272 u.cm.pX = &p->aMem[pOp->p3];
56273 for(u.cm.i=0; u.cm.i<u.cm.nArg; u.cm.i++){
56274 storeTypeInfo(u.cm.pX, 0);
56275 u.cm.apArg[u.cm.i] = u.cm.pX;
56276 u.cm.pX++;
 
 
 
56277 }
56278 if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
56279 sqlite3VtabLock(u.cm.pVtab);
56280 rc = u.cm.pModule->xUpdate(u.cm.pVtab, u.cm.nArg, u.cm.apArg, &u.cm.rowid);
56281 sqlite3DbFree(db, p->zErrMsg);
56282 p->zErrMsg = u.cm.pVtab->zErrMsg;
56283 u.cm.pVtab->zErrMsg = 0;
56284 sqlite3VtabUnlock(db, u.cm.pVtab);
56285 if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
56286 if( rc==SQLITE_OK && pOp->p1 ){
56287 assert( u.cm.nArg>1 && u.cm.apArg[0] && (u.cm.apArg[0]->flags&MEM_Null) );
56288 db->lastRowid = u.cm.rowid;
56289 }
56290 p->nChange++;
56291 }
56292 break;
56293 }
@@ -56409,22 +56297,25 @@
56297 /* Opcode: Pagecount P1 P2 * * *
56298 **
56299 ** Write the current number of pages in database P1 to memory cell P2.
56300 */
56301 case OP_Pagecount: { /* out2-prerelease */
56302 #if 0 /* local variables moved into u.cn */
56303 int p1;
56304 int nPage;
56305 Pager *pPager;
56306 #endif /* local variables moved into u.cn */
56307
56308 u.cn.p1 = pOp->p1;
56309 u.cn.pPager = sqlite3BtreePager(db->aDb[u.cn.p1].pBt);
56310 rc = sqlite3PagerPagecount(u.cn.pPager, &u.cn.nPage);
56311 /* OP_Pagecount is always called from within a read transaction. The
56312 ** page count has already been successfully read and cached. So the
56313 ** sqlite3PagerPagecount() call above cannot fail. */
56314 if( ALWAYS(rc==SQLITE_OK) ){
56315 pOut->flags = MEM_Int;
56316 pOut->u.i = u.cn.nPage;
56317 }
56318 break;
56319 }
56320 #endif
56321
@@ -56433,22 +56324,22 @@
56324 **
56325 ** If tracing is enabled (by the sqlite3_trace()) interface, then
56326 ** the UTF-8 string contained in P4 is emitted on the trace callback.
56327 */
56328 case OP_Trace: {
56329 #if 0 /* local variables moved into u.co */
56330 char *zTrace;
56331 #endif /* local variables moved into u.co */
56332
56333 u.co.zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql);
56334 if( u.co.zTrace ){
56335 if( db->xTrace ){
56336 db->xTrace(db->pTraceArg, u.co.zTrace);
56337 }
56338 #ifdef SQLITE_DEBUG
56339 if( (db->flags & SQLITE_SqlTrace)!=0 ){
56340 sqlite3DebugPrintf("SQL-trace: %s\n", u.co.zTrace);
56341 }
56342 #endif /* SQLITE_DEBUG */
56343 }
56344 break;
56345 }
@@ -57461,11 +57352,11 @@
57352 **
57353 *************************************************************************
57354 ** This file contains routines used for walking the parser tree for
57355 ** an SQL statement.
57356 **
57357 ** $Id: walker.c,v 1.7 2009/06/15 23:15:59 drh Exp $
57358 */
57359
57360
57361 /*
57362 ** Walk an expression tree. Invoke the callback once for each node
@@ -57508,18 +57399,18 @@
57399 /*
57400 ** Call sqlite3WalkExpr() for every expression in list p or until
57401 ** an abort request is seen.
57402 */
57403 SQLITE_PRIVATE int sqlite3WalkExprList(Walker *pWalker, ExprList *p){
57404 int i;
57405 struct ExprList_item *pItem;
57406 if( p ){
57407 for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){
57408 if( sqlite3WalkExpr(pWalker, pItem->pExpr) ) return WRC_Abort;
57409 }
57410 }
57411 return WRC_Continue;
57412 }
57413
57414 /*
57415 ** Walk all expressions associated with SELECT statement p. Do
57416 ** not invoke the SELECT callback on p, but do (of course) invoke
@@ -57548,11 +57439,11 @@
57439 SrcList *pSrc;
57440 int i;
57441 struct SrcList_item *pItem;
57442
57443 pSrc = p->pSrc;
57444 if( ALWAYS(pSrc) ){
57445 for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){
57446 if( sqlite3WalkSelect(pWalker, pItem->pSelect) ){
57447 return WRC_Abort;
57448 }
57449 }
@@ -57601,11 +57492,11 @@
57492 **
57493 ** This file contains routines used for walking the parser tree and
57494 ** resolve all identifiers by associating them with a particular
57495 ** table and column.
57496 **
57497 ** $Id: resolve.c,v 1.30 2009/06/15 23:15:59 drh Exp $
57498 */
57499
57500 /*
57501 ** Turn the pExpr expression into an alias for the iCol-th column of the
57502 ** result set in pEList.
@@ -57660,19 +57551,18 @@
57551 }else if( ExprHasProperty(pOrig, EP_IntValue) || pOrig->u.zToken==0 ){
57552 pDup = sqlite3ExprDup(db, pOrig, 0);
57553 if( pDup==0 ) return;
57554 }else{
57555 char *zToken = pOrig->u.zToken;
57556 assert( zToken!=0 );
57557 pOrig->u.zToken = 0;
57558 pDup = sqlite3ExprDup(db, pOrig, 0);
57559 pOrig->u.zToken = zToken;
57560 if( pDup==0 ) return;
57561 assert( (pDup->flags & (EP_Reduced|EP_TokenOnly))==0 );
57562 pDup->flags2 |= EP2_MallocedToken;
57563 pDup->u.zToken = sqlite3DbStrDup(db, zToken);
 
 
57564 }
57565 if( pExpr->flags & EP_ExpCollate ){
57566 pDup->pColl = pExpr->pColl;
57567 pDup->flags |= EP_ExpCollate;
57568 }
@@ -57704,11 +57594,11 @@
57594 ** value can be NULL if zDb is also NULL. If zTable is NULL it
57595 ** means that the form of the name is Z and that columns from any table
57596 ** can be used.
57597 **
57598 ** If the name cannot be resolved unambiguously, leave an error message
57599 ** in pParse and return WRC_Abort. Return WRC_Prune on success.
57600 */
57601 static int lookupName(
57602 Parse *pParse, /* The parsing context */
57603 const char *zDb, /* Name of the database containing table, or NULL */
57604 const char *zTab, /* Name of table containing column, or NULL */
@@ -57753,11 +57643,13 @@
57643 if( pItem->zAlias ){
57644 char *zTabName = pItem->zAlias;
57645 if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
57646 }else{
57647 char *zTabName = pTab->zName;
57648 if( NEVER(zTabName==0) || sqlite3StrICmp(zTabName, zTab)!=0 ){
57649 continue;
57650 }
57651 if( zDb!=0 && sqlite3StrICmp(db->aDb[iDb].zName, zDb)!=0 ){
57652 continue;
57653 }
57654 }
57655 }
@@ -57832,18 +57724,16 @@
57724 for(iCol=0; iCol < pTab->nCol; iCol++, pCol++) {
57725 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
57726 cnt++;
57727 pExpr->iColumn = iCol==pTab->iPKey ? -1 : (i16)iCol;
57728 pExpr->pTab = pTab;
57729 testcase( iCol==31 );
57730 testcase( iCol==32 );
57731 if( iCol>=32 ){
57732 *piColMask = 0xffffffff;
57733 }else{
57734 *piColMask |= ((u32)1)<<iCol;
 
 
57735 }
57736 break;
57737 }
57738 }
57739 }
@@ -57880,11 +57770,11 @@
57770 assert( pExpr->x.pList==0 );
57771 assert( pExpr->x.pSelect==0 );
57772 pOrig = pEList->a[j].pExpr;
57773 if( !pNC->allowAgg && ExprHasProperty(pOrig, EP_Agg) ){
57774 sqlite3ErrorMsg(pParse, "misuse of aliased aggregate %s", zAs);
57775 return WRC_Abort;
57776 }
57777 resolveAlias(pParse, pEList, j, pExpr, "");
57778 cnt = 1;
57779 pMatch = 0;
57780 assert( zTab==0 && zDb==0 );
@@ -57912,11 +57802,11 @@
57802 ** fields are not changed in any context.
57803 */
57804 if( cnt==0 && zTab==0 && ExprHasProperty(pExpr,EP_DblQuoted) ){
57805 pExpr->op = TK_STRING;
57806 pExpr->pTab = 0;
57807 return WRC_Prune;
57808 }
57809
57810 /*
57811 ** cnt==0 means there was not match. cnt>1 means there were two or
57812 ** more matches. Either way, we have an error.
@@ -57967,13 +57857,13 @@
57857 assert( pTopNC!=0 );
57858 pTopNC->nRef++;
57859 if( pTopNC==pNC ) break;
57860 pTopNC = pTopNC->pNext;
57861 }
57862 return WRC_Prune;
57863 } else {
57864 return WRC_Abort;
57865 }
57866 }
57867
57868 /*
57869 ** This routine is callback for sqlite3WalkExpr().
@@ -58028,12 +57918,11 @@
57918 #endif /* defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY) */
57919
57920 /* A lone identifier is the name of a column.
57921 */
57922 case TK_ID: {
57923 return lookupName(pParse, 0, 0, pExpr->u.zToken, pNC, pExpr);
 
57924 }
57925
57926 /* A table name and column name: ID.ID
57927 ** Or a database, table and column: ID.ID.ID
57928 */
@@ -58053,12 +57942,11 @@
57942 assert( pRight->op==TK_DOT );
57943 zDb = pExpr->pLeft->u.zToken;
57944 zTable = pRight->pLeft->u.zToken;
57945 zColumn = pRight->pRight->u.zToken;
57946 }
57947 return lookupName(pParse, zDb, zTable, zColumn, pNC, pExpr);
 
57948 }
57949
57950 /* Resolve function names
57951 */
57952 case TK_CONST_FUNC:
@@ -58072,10 +57960,11 @@
57960 int nId; /* Number of characters in function name */
57961 const char *zId; /* The function name. */
57962 FuncDef *pDef; /* Information about the function */
57963 u8 enc = ENC(pParse->db); /* The database encoding */
57964
57965 testcase( pExpr->op==TK_CONST_FUNC );
57966 assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
57967 zId = pExpr->u.zToken;
57968 nId = sqlite3Strlen30(zId);
57969 pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0);
57970 if( pDef==0 ){
@@ -58126,13 +58015,14 @@
58015 */
58016 return WRC_Prune;
58017 }
58018 #ifndef SQLITE_OMIT_SUBQUERY
58019 case TK_SELECT:
58020 case TK_EXISTS: testcase( pExpr->op==TK_EXISTS );
58021 #endif
58022 case TK_IN: {
58023 testcase( pExpr->op==TK_IN );
58024 if( ExprHasProperty(pExpr, EP_xIsSelect) ){
58025 int nRef = pNC->nRef;
58026 #ifndef SQLITE_OMIT_CHECK
58027 if( pNC->isCheck ){
58028 sqlite3ErrorMsg(pParse,"subqueries prohibited in CHECK constraints");
@@ -58177,11 +58067,11 @@
58067 ){
58068 int i; /* Loop counter */
58069
58070 UNUSED_PARAMETER(pParse);
58071
58072 if( pE->op==TK_ID ){
58073 char *zCol = pE->u.zToken;
58074 for(i=0; i<pEList->nExpr; i++){
58075 char *zAs = pEList->a[i].zName;
58076 if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
58077 return i+1;
@@ -58313,11 +58203,11 @@
58203 int iCol = -1;
58204 Expr *pE, *pDup;
58205 if( pItem->done ) continue;
58206 pE = pItem->pExpr;
58207 if( sqlite3ExprIsInteger(pE, &iCol) ){
58208 if( iCol<=0 || iCol>pEList->nExpr ){
58209 resolveOutOfRangeError(pParse, "ORDER", i+1, pEList->nExpr);
58210 return 1;
58211 }
58212 }else{
58213 iCol = resolveAsName(pParse, pEList, pE);
@@ -58327,13 +58217,10 @@
58217 assert(pDup);
58218 iCol = resolveOrderByTermToExprList(pParse, pSelect, pDup);
58219 }
58220 sqlite3ExprDelete(db, pDup);
58221 }
 
 
 
58222 }
58223 if( iCol>0 ){
58224 CollSeq *pColl = pE->pColl;
58225 int flags = pE->flags & EP_ExpCollate;
58226 sqlite3ExprDelete(db, pE);
@@ -58436,13 +58323,10 @@
58323 nResult = pSelect->pEList->nExpr;
58324 pParse = pNC->pParse;
58325 for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
58326 Expr *pE = pItem->pExpr;
58327 iCol = resolveAsName(pParse, pSelect->pEList, pE);
 
 
 
58328 if( iCol>0 ){
58329 /* If an AS-name match is found, mark this ORDER BY column as being
58330 ** a copy of the iCol-th result-set column. The subsequent call to
58331 ** sqlite3ResolveOrderGroupBy() will convert the expression to a
58332 ** copy of the iCol-th result-set expression. */
@@ -58768,11 +58652,11 @@
58652 **
58653 *************************************************************************
58654 ** This file contains routines used for analyzing expressions and
58655 ** for generating VDBE code that evaluates expressions in SQLite.
58656 **
58657 ** $Id: expr.c,v 1.446 2009/06/19 18:32:55 drh Exp $
58658 */
58659
58660 /*
58661 ** Return the 'affinity' of the expression pExpr if any.
58662 **
@@ -59245,15 +59129,15 @@
59129 exprSetHeight(pRoot);
59130 }
59131 }
59132
59133 /*
59134 ** Allocate a Expr node which joins as many as two subtrees.
59135 **
59136 ** One or both of the subtrees can be NULL. Return a pointer to the new
59137 ** Expr node. Or, if an OOM error occurs, set pParse->db->mallocFailed,
59138 ** free the subtrees and return NULL.
59139 */
59140 SQLITE_PRIVATE Expr *sqlite3PExpr(
59141 Parse *pParse, /* Parsing context */
59142 int op, /* Expression opcode */
59143 Expr *pLeft, /* Left operand */
@@ -64140,11 +64024,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.
@@ -65372,11 +65256,10 @@
65256 pColl = sqlite3FindCollSeq(db, enc, zName, initbusy);
65257 if( !initbusy && (!pColl || !pColl->xCmp) ){
65258 pColl = sqlite3GetCollSeq(db, pColl, zName);
65259 if( !pColl ){
65260 sqlite3ErrorMsg(pParse, "no such collation sequence: %s", zName);
 
65261 }
65262 }
65263
65264 return pColl;
65265 }
@@ -67756,11 +67639,11 @@
67639 *************************************************************************
67640 **
67641 ** This file contains functions used to access the internal hash tables
67642 ** of user defined functions and collation sequences.
67643 **
67644 ** $Id: callback.c,v 1.42 2009/06/17 00:35:31 drh Exp $
67645 */
67646
67647
67648 /*
67649 ** Invoke the 'collation needed' callback to request a collation sequence
@@ -67866,13 +67749,11 @@
67749 SQLITE_PRIVATE int sqlite3CheckCollSeq(Parse *pParse, CollSeq *pColl){
67750 if( pColl ){
67751 const char *zName = pColl->zName;
67752 CollSeq *p = sqlite3GetCollSeq(pParse->db, pColl, zName);
67753 if( !p ){
67754 sqlite3ErrorMsg(pParse, "no such collation sequence: %s", zName);
 
 
67755 pParse->nErr++;
67756 return SQLITE_ERROR;
67757 }
67758 assert( p==pColl );
67759 }
@@ -68083,11 +67964,10 @@
67964 int bestScore = 0; /* Score of best match */
67965 int h; /* Hash value */
67966
67967
67968 assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE );
 
67969 h = (sqlite3UpperToLower[(u8)zName[0]] + nName) % ArraySize(db->aFunc.a);
67970
67971 /* First search for a match amongst the application-defined functions.
67972 */
67973 p = functionSearch(&db->aFunc, h, zName, nName);
@@ -68833,11 +68713,11 @@
68713 **
68714 ** There is only one exported symbol in this file - the function
68715 ** sqliteRegisterBuildinFunctions() found at the bottom of the file.
68716 ** All other code has file scope.
68717 **
68718 ** $Id: func.c,v 1.239 2009/06/19 16:44:41 drh Exp $
68719 */
68720
68721 /*
68722 ** Return the collating function associated with a function.
68723 */
@@ -70078,19 +69958,14 @@
69958 if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
69959 pAccum = (StrAccum*)sqlite3_aggregate_context(context, sizeof(*pAccum));
69960
69961 if( pAccum ){
69962 sqlite3 *db = sqlite3_context_db_handle(context);
69963 int firstTerm = pAccum->useMalloc==0;
69964 pAccum->useMalloc = 1;
69965 pAccum->mxAlloc = db->aLimit[SQLITE_LIMIT_LENGTH];
69966 if( !firstTerm ){
 
 
 
 
 
69967 if( argc==2 ){
69968 zSep = (char*)sqlite3_value_text(argv[1]);
69969 nSep = sqlite3_value_bytes(argv[1]);
69970 }else{
69971 zSep = ",";
@@ -70132,13 +70007,10 @@
70007 assert( rc==SQLITE_NOMEM || rc==SQLITE_OK );
70008 if( rc==SQLITE_NOMEM ){
70009 db->mallocFailed = 1;
70010 }
70011 }
 
 
 
70012 }
70013
70014 /*
70015 ** Set the LIKEOPT flag on the 2-argument function with the given name.
70016 */
@@ -73213,16 +73085,16 @@
73085 ** May you share freely, never taking more than you give.
73086 **
73087 *************************************************************************
73088 ** This file contains code used to implement the PRAGMA command.
73089 **
73090 ** $Id: pragma.c,v 1.213 2009/06/19 14:06:03 drh Exp $
73091 */
73092
73093 /* Ignore this whole file if pragmas are disabled
73094 */
73095 #if !defined(SQLITE_OMIT_PRAGMA)
73096
73097 /*
73098 ** Interpret the given string as a safety level. Return 0 for OFF,
73099 ** 1 for ON or NORMAL and 2 for FULL. Return 1 for an empty or
73100 ** unrecognized string argument.
@@ -74549,21 +74421,10 @@
74421 }
74422
74423 }else
74424 #endif
74425
 
 
 
 
 
 
 
 
 
 
 
74426 #if SQLITE_HAS_CODEC
74427 if( sqlite3StrICmp(zLeft, "key")==0 && zRight ){
74428 sqlite3_key(db, zRight, sqlite3Strlen30(zRight));
74429 }else
74430 if( sqlite3StrICmp(zLeft, "rekey")==0 && zRight ){
@@ -74624,11 +74485,11 @@
74485 pragma_out:
74486 sqlite3DbFree(db, zLeft);
74487 sqlite3DbFree(db, zRight);
74488 }
74489
74490 #endif /* SQLITE_OMIT_PRAGMA */
74491
74492 /************** End of pragma.c **********************************************/
74493 /************** Begin file prepare.c *****************************************/
74494 /*
74495 ** 2005 May 25
@@ -74643,11 +74504,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.
@@ -74660,16 +74521,16 @@
74521 sqlite3 *db = pData->db;
74522 if( !db->mallocFailed && (db->flags & SQLITE_RecoveryMode)==0 ){
74523 if( zObj==0 ) zObj = "?";
74524 sqlite3SetString(pData->pzErrMsg, pData->db,
74525 "malformed database schema (%s)", zObj);
74526 if( zExtra ){
74527 *pData->pzErrMsg = sqlite3MAppendf(pData->db, *pData->pzErrMsg, "%s - %s",
74528 *pData->pzErrMsg, zExtra);
74529 }
74530 }
74531 pData->rc = db->mallocFailed ? SQLITE_NOMEM : SQLITE_CORRUPT;
74532 }
74533
74534 /*
74535 ** This is the callback routine for the code that initializes the
74536 ** database. See sqlite3Init() below for additional information.
@@ -74691,11 +74552,11 @@
74552 UNUSED_PARAMETER2(NotUsed, argc);
74553 assert( sqlite3_mutex_held(db->mutex) );
74554 DbClearProperty(db, iDb, DB_Empty);
74555 if( db->mallocFailed ){
74556 corruptSchema(pData, argv[0], 0);
74557 return 1;
74558 }
74559
74560 assert( iDb>=0 && iDb<db->nDb );
74561 if( argv==0 ) return 0; /* Might happen if EMPTY_RESULT_CALLBACKS are on */
74562 if( argv[1]==0 ){
@@ -74716,11 +74577,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 ){
@@ -74732,19 +74593,19 @@
74593 ** been created when we processed the CREATE TABLE. All we have
74594 ** to do here is record the root page number for that index.
74595 */
74596 Index *pIndex;
74597 pIndex = sqlite3FindIndex(db, argv[0], db->aDb[iDb].zName);
74598 if( pIndex==0 ){
74599 /* This can occur if there exists an index on a TEMP table which
74600 ** has the same name as another index on a permanent index. Since
74601 ** the permanent table is hidden by the TEMP table, we can also
74602 ** safely ignore the index on the permanent table.
74603 */
74604 /* Do Nothing */;
74605 }else if( sqlite3GetInt32(argv[1], &pIndex->tnum)==0 ){
74606 corruptSchema(pData, argv[0], "invalid rootpage");
74607 }
74608 }
74609 return 0;
74610 }
74611
@@ -74826,19 +74687,19 @@
74687 if( initData.rc ){
74688 rc = initData.rc;
74689 goto error_out;
74690 }
74691 pTab = sqlite3FindTable(db, zMasterName, db->aDb[iDb].zName);
74692 if( ALWAYS(pTab) ){
74693 pTab->tabFlags |= TF_Readonly;
74694 }
74695
74696 /* Create a cursor to hold the database open
74697 */
74698 pDb = &db->aDb[iDb];
74699 if( pDb->pBt==0 ){
74700 if( !OMIT_TEMPDB && ALWAYS(iDb==1) ){
74701 DbSetProperty(db, 1, DB_SchemaLoaded);
74702 }
74703 return SQLITE_OK;
74704 }
74705 curMain = sqlite3MallocZero(sqlite3BtreeCursorSize());
@@ -74854,12 +74715,17 @@
74715 **
74716 ** Meta values are as follows:
74717 ** meta[0] Schema cookie. Changes with each schema change.
74718 ** meta[1] File format of schema layer.
74719 ** meta[2] Size of the page cache.
74720 ** meta[3] Largest rootpage (auto/incr_vacuum mode)
74721 ** meta[4] Db text encoding. 1:UTF-8 2:UTF-16LE 3:UTF-16BE
74722 ** meta[5] User version
74723 ** meta[6] Incremental vacuum mode
74724 ** meta[7] unused
74725 ** meta[8] unused
74726 ** meta[9] unused
74727 **
74728 ** Note: The #defined SQLITE_UTF* symbols in sqliteInt.h correspond to
74729 ** the possible values of meta[4].
74730 */
74731 for(i=0; rc==SQLITE_OK && i<ArraySize(meta); i++){
@@ -74932,14 +74798,11 @@
74798 }
74799
74800 /* Read the schema information out of the schema tables
74801 */
74802 assert( db->init.busy );
74803 {
 
 
 
74804 char *zSql;
74805 zSql = sqlite3MPrintf(db,
74806 "SELECT name, rootpage, sql FROM '%q'.%s",
74807 db->aDb[iDb].zName, zMasterName);
74808 (void)sqlite3SafetyOff(db);
@@ -75009,11 +74872,10 @@
74872 SQLITE_PRIVATE int sqlite3Init(sqlite3 *db, char **pzErrMsg){
74873 int i, rc;
74874 int commit_internal = !(db->flags&SQLITE_InternChanges);
74875
74876 assert( sqlite3_mutex_held(db->mutex) );
 
74877 rc = SQLITE_OK;
74878 db->init.busy = 1;
74879 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
74880 if( DbHasProperty(db, i, DB_SchemaLoaded) || i==1 ) continue;
74881 rc = sqlite3InitOne(db, i, pzErrMsg);
@@ -75025,11 +74887,12 @@
74887 /* Once all the other databases have been initialised, load the schema
74888 ** for the TEMP database. This is loaded last, as the TEMP database
74889 ** schema may contain references to objects in other databases.
74890 */
74891 #ifndef SQLITE_OMIT_TEMPDB
74892 if( rc==SQLITE_OK && ALWAYS(db->nDb>1)
74893 && !DbHasProperty(db, 1, DB_SchemaLoaded) ){
74894 rc = sqlite3InitOne(db, 1, pzErrMsg);
74895 if( rc ){
74896 sqlite3ResetInternalSchema(db, 1);
74897 }
74898 }
@@ -75082,16 +74945,17 @@
74945 if( pBt==0 ) continue;
74946 memset(curTemp, 0, sqlite3BtreeCursorSize());
74947 rc = sqlite3BtreeCursor(pBt, MASTER_ROOT, 0, 0, curTemp);
74948 if( rc==SQLITE_OK ){
74949 rc = sqlite3BtreeGetMeta(pBt, BTREE_SCHEMA_VERSION, (u32 *)&cookie);
74950 if( ALWAYS(rc==SQLITE_OK)
74951 && cookie!=db->aDb[iDb].pSchema->schema_cookie ){
74952 allOk = 0;
74953 }
74954 sqlite3BtreeCloseCursor(curTemp);
74955 }
74956 if( NEVER(rc==SQLITE_NOMEM) || rc==SQLITE_IOERR_NOMEM ){
74957 db->mallocFailed = 1;
74958 }
74959 }
74960 sqlite3_free(curTemp);
74961 }else{
@@ -75206,10 +75070,12 @@
75070
75071 pParse->db = db;
75072 if( nBytes>=0 && (nBytes==0 || zSql[nBytes-1]!=0) ){
75073 char *zSqlCopy;
75074 int mxLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
75075 testcase( nBytes==mxLen );
75076 testcase( nBytes==mxLen+1 );
75077 if( nBytes>mxLen ){
75078 sqlite3Error(db, SQLITE_TOOBIG, "statement too long");
75079 (void)sqlite3SafetyOff(db);
75080 rc = sqlite3ApiExit(db, SQLITE_TOOBIG);
75081 goto end_prepare;
@@ -75312,10 +75178,14 @@
75178 return SQLITE_MISUSE;
75179 }
75180 sqlite3_mutex_enter(db->mutex);
75181 sqlite3BtreeEnterAll(db);
75182 rc = sqlite3Prepare(db, zSql, nBytes, saveSqlFlag, ppStmt, pzTail);
75183 if( rc==SQLITE_SCHEMA ){
75184 sqlite3_finalize(*ppStmt);
75185 rc = sqlite3Prepare(db, zSql, nBytes, saveSqlFlag, ppStmt, pzTail);
75186 }
75187 sqlite3BtreeLeaveAll(db);
75188 sqlite3_mutex_leave(db->mutex);
75189 return rc;
75190 }
75191
@@ -75485,11 +75355,11 @@
75355 **
75356 *************************************************************************
75357 ** This file contains C code routines that are called by the parser
75358 ** to handle SELECT statements in SQLite.
75359 **
75360 ** $Id: select.c,v 1.524 2009/06/12 03:27:27 drh Exp $
75361 */
75362
75363
75364 /*
75365 ** Delete all the content of a Select structure but do not deallocate
@@ -78269,10 +78139,11 @@
78139
78140 /* Transfer the FROM clause terms from the subquery into the
78141 ** outer query.
78142 */
78143 for(i=0; i<nSubSrc; i++){
78144 sqlite3IdListDelete(db, pSrc->a[i+iFrom].pUsing);
78145 pSrc->a[i+iFrom] = pSubSrc->a[i];
78146 memset(&pSubSrc->a[i], 0, sizeof(pSubSrc->a[i]));
78147 }
78148 pSrc->a[iFrom].jointype = jointype;
78149
@@ -81764,11 +81635,11 @@
81635 ** May you share freely, never taking more than you give.
81636 **
81637 *************************************************************************
81638 ** This file contains code used to help implement virtual tables.
81639 **
81640 ** $Id: vtab.c,v 1.91 2009/06/15 16:27:08 shane Exp $
81641 */
81642 #ifndef SQLITE_OMIT_VIRTUALTABLE
81643
81644 /*
81645 ** The actual function that does the work of creating a new module.
@@ -81854,10 +81725,13 @@
81725 /*
81726 ** Unlock a virtual table. When the last lock is removed,
81727 ** disconnect the virtual table.
81728 */
81729 SQLITE_PRIVATE void sqlite3VtabUnlock(sqlite3 *db, sqlite3_vtab *pVtab){
81730 #ifndef SQLITE_DEBUG
81731 UNUSED_PARAMETER(db);
81732 #endif
81733 assert( pVtab->nRef>0 );
81734 pVtab->nRef--;
81735 assert(db);
81736 assert( sqlite3SafetyCheckOk(db) );
81737 if( pVtab->nRef==0 ){
@@ -82633,11 +82507,11 @@
82507 ** generating the code that loops through a table looking for applicable
82508 ** rows. Indices are selected and used to speed the search when doing
82509 ** so is applicable. Because this module is responsible for selecting
82510 ** indices, you might also think of this module as the "query optimizer".
82511 **
82512 ** $Id: where.c,v 1.408 2009/06/16 14:15:22 shane Exp $
82513 */
82514
82515 /*
82516 ** Trace output macros
82517 */
@@ -83275,23 +83149,24 @@
83149 if( pColl==0 ) return 0;
83150 if( (pColl->type!=SQLITE_COLL_BINARY || *pnoCase) &&
83151 (pColl->type!=SQLITE_COLL_NOCASE || !*pnoCase) ){
83152 return 0;
83153 }
83154 if( sqlite3ExprAffinity(pLeft)!=SQLITE_AFF_TEXT ) return 0;
83155 z = pRight->u.zToken;
 
83156 if( ALWAYS(z) ){
83157 cnt = 0;
83158 while( (c=z[cnt])!=0 && c!=wc[0] && c!=wc[1] && c!=wc[2] ){
83159 cnt++;
83160 }
83161 if( cnt!=0 && c!=0 && 255!=(u8)z[cnt-1] ){
83162 *pisComplete = z[cnt]==wc[0] && z[cnt+1]==0;
83163 *pnPattern = cnt;
83164 return 1;
83165 }
83166 }
83167 return 0;
 
 
 
 
 
83168 }
83169 #endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */
83170
83171
83172 #ifndef SQLITE_OMIT_VIRTUALTABLE
@@ -83507,10 +83382,26 @@
83382
83383 /*
83384 ** chngToIN holds a set of tables that *might* satisfy case 1. But
83385 ** we have to do some additional checking to see if case 1 really
83386 ** is satisfied.
83387 **
83388 ** chngToIN will hold either 0, 1, or 2 bits. The 0-bit case means
83389 ** that there is no possibility of transforming the OR clause into an
83390 ** IN operator because one or more terms in the OR clause contain
83391 ** something other than == on a column in the single table. The 1-bit
83392 ** case means that every term of the OR clause is of the form
83393 ** "table.column=expr" for some single table. The one bit that is set
83394 ** will correspond to the common table. We still need to check to make
83395 ** sure the same column is used on all terms. The 2-bit case is when
83396 ** the all terms are of the form "table1.column=table2.column". It
83397 ** might be possible to form an IN operator with either table1.column
83398 ** or table2.column as the LHS if either is common to every term of
83399 ** the OR clause.
83400 **
83401 ** Note that terms of the form "table.column1=table.column2" (the
83402 ** same table on both sizes of the ==) cannot be optimized.
83403 */
83404 if( chngToIN ){
83405 int okToChngToIN = 0; /* True if the conversion to IN is valid */
83406 int iColumn = -1; /* Column index on lhs of IN operator */
83407 int iCursor = -1; /* Table cursor common to all terms */
@@ -83525,22 +83416,42 @@
83416 for(j=0; j<2 && !okToChngToIN; j++){
83417 pOrTerm = pOrWc->a;
83418 for(i=pOrWc->nTerm-1; i>=0; i--, pOrTerm++){
83419 assert( pOrTerm->eOperator==WO_EQ );
83420 pOrTerm->wtFlags &= ~TERM_OR_OK;
83421 if( pOrTerm->leftCursor==iCursor ){
83422 /* This is the 2-bit case and we are on the second iteration and
83423 ** current term is from the first iteration. So skip this term. */
83424 assert( j==1 );
83425 continue;
83426 }
83427 if( (chngToIN & getMask(pMaskSet, pOrTerm->leftCursor))==0 ){
83428 /* This term must be of the form t1.a==t2.b where t2 is in the
83429 ** chngToIN set but t1 is not. This term will be either preceeded
83430 ** or follwed by an inverted copy (t2.b==t1.a). Skip this term
83431 ** and use its inversion. */
83432 testcase( pOrTerm->wtFlags & TERM_COPIED );
83433 testcase( pOrTerm->wtFlags & TERM_VIRTUAL );
83434 assert( pOrTerm->wtFlags & (TERM_COPIED|TERM_VIRTUAL) );
83435 continue;
83436 }
83437 iColumn = pOrTerm->u.leftColumn;
83438 iCursor = pOrTerm->leftCursor;
83439 break;
83440 }
83441 if( i<0 ){
83442 /* No candidate table+column was found. This can only occur
83443 ** on the second iteration */
83444 assert( j==1 );
83445 assert( (chngToIN&(chngToIN-1))==0 );
83446 assert( chngToIN==getMask(pMaskSet, iCursor) );
83447 break;
83448 }
83449 testcase( j==1 );
83450
83451 /* We have found a candidate table and column. Check to see if that
83452 ** table and column is common to every term in the OR clause */
83453 okToChngToIN = 1;
83454 for(; i>=0 && okToChngToIN; i--, pOrTerm++){
83455 assert( pOrTerm->eOperator==WO_EQ );
83456 if( pOrTerm->leftCursor!=iCursor ){
83457 pOrTerm->wtFlags &= ~TERM_OR_OK;
@@ -83782,15 +83693,22 @@
83693 pRight = pExpr->x.pList->a[0].pExpr;
83694 pStr1 = sqlite3Expr(db, TK_STRING, pRight->u.zToken);
83695 if( pStr1 ) pStr1->u.zToken[nPattern] = 0;
83696 pStr2 = sqlite3ExprDup(db, pStr1, 0);
83697 if( !db->mallocFailed ){
83698 u8 c, *pC; /* Last character before the first wildcard */
83699 pC = (u8*)&pStr2->u.zToken[nPattern-1];
83700 c = *pC;
83701 if( noCase ){
83702 /* The point is to increment the last character before the first
83703 ** wildcard. But if we increment '@', that will push it into the
83704 ** alphabetic range where case conversions will mess up the
83705 ** inequality. To avoid this, make sure to also run the full
83706 ** LIKE on all candidate expressions by clearing the isComplete flag
83707 */
83708 if( c=='A'-1 ) isComplete = 0;
83709
83710 c = sqlite3UpperToLower[c];
83711 }
83712 *pC = c + 1;
83713 }
83714 pNewExpr1 = sqlite3PExpr(pParse, TK_GE, sqlite3ExprDup(db,pLeft,0),pStr1,0);
@@ -84456,11 +84374,11 @@
84374 pCost->rCost = (SQLITE_BIG_DBL/((double)2));
84375 }else{
84376 pCost->rCost = pIdxInfo->estimatedCost;
84377 }
84378 pCost->plan.u.pVtabIdx = pIdxInfo;
84379 if( pIdxInfo->orderByConsumed ){
84380 pCost->plan.wsFlags |= WHERE_ORDERBY;
84381 }
84382 pCost->plan.nEq = 0;
84383 pIdxInfo->nOrderBy = nOrderBy;
84384
@@ -84742,11 +84660,11 @@
84660 }
84661 }else{
84662 cost += cost*estLog(cost);
84663 WHERETRACE(("...... orderby increases cost to %.9g\n", cost));
84664 }
84665 }else if( wsFlags!=0 && (pParse->db->flags & SQLITE_ReverseOrder)!=0 ){
84666 /* For application testing, randomly reverse the output order for
84667 ** SELECT statements that omit the ORDER BY clause. This will help
84668 ** to find cases where
84669 */
84670 wsFlags |= WHERE_REVERSE;
@@ -84805,18 +84723,21 @@
84723 struct SrcList_item *pSrc, /* The FROM clause term to search */
84724 Bitmask notReady, /* Mask of cursors that are not available */
84725 ExprList *pOrderBy, /* The ORDER BY clause */
84726 WhereCost *pCost /* Lowest cost query plan */
84727 ){
84728 #ifndef SQLITE_OMIT_VIRTUALTABLE
84729 if( IsVirtual(pSrc->pTab) ){
84730 sqlite3_index_info *p = 0;
84731 bestVirtualIndex(pParse, pWC, pSrc, notReady, pOrderBy, pCost, &p);
84732 if( p->needToFreeIdxStr ){
84733 sqlite3_free(p->idxStr);
84734 }
84735 sqlite3DbFree(pParse->db, p);
84736 }else
84737 #endif
84738 {
84739 bestBtreeIndex(pParse, pWC, pSrc, notReady, pOrderBy, pCost);
84740 }
84741 }
84742
84743 /*
@@ -85448,12 +85369,12 @@
85369 WhereClause *pOrWc; /* The OR-clause broken out into subterms */
85370 WhereTerm *pFinal; /* Final subterm within the OR-clause. */
85371 SrcList oneTab; /* Shortened table list */
85372
85373 int regReturn = ++pParse->nMem; /* Register used with OP_Gosub */
85374 int regRowset = 0; /* Register for RowSet object */
85375 int regRowid = 0; /* Register holding rowid */
85376 int iLoopBody = sqlite3VdbeMakeLabel(v); /* Start of loop body */
85377 int iRetInit; /* Address of regReturn init */
85378 int ii;
85379
85380 pTerm = pLevel->plan.u.pTerm;
@@ -85488,21 +85409,20 @@
85409
85410 for(ii=0; ii<pOrWc->nTerm; ii++){
85411 WhereTerm *pOrTerm = &pOrWc->a[ii];
85412 if( pOrTerm->leftCursor==iCur || pOrTerm->eOperator==WO_AND ){
85413 WhereInfo *pSubWInfo; /* Info for single OR-term scan */
 
85414 /* Loop through table entries that match term pOrTerm. */
85415 pSubWInfo = sqlite3WhereBegin(pParse, &oneTab, pOrTerm->pExpr, 0,
85416 WHERE_OMIT_OPEN | WHERE_OMIT_CLOSE | WHERE_FORCE_TABLE);
85417 if( pSubWInfo ){
85418 if( (wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
85419 int iSet = ((ii==pOrWc->nTerm-1)?-1:ii);
85420 int r;
85421 r = sqlite3ExprCodeGetColumn(pParse, pTabItem->pTab, -1, iCur,
85422 regRowid, 0);
85423 sqlite3VdbeAddOp4(v, OP_RowSetTest, regRowset,
85424 sqlite3VdbeCurrentAddr(v)+2,
85425 r, SQLITE_INT_TO_PTR(iSet), P4_INT32);
85426 }
85427 sqlite3VdbeAddOp2(v, OP_Gosub, regReturn, iLoopBody);
85428
@@ -85789,13 +85709,15 @@
85709 ** with virtual tables.
85710 */
85711 assert( pWC->vmask==0 && pMaskSet->n==0 );
85712 for(i=0; i<pTabList->nSrc; i++){
85713 createMask(pMaskSet, pTabList->a[i].iCursor);
85714 #ifndef SQLITE_OMIT_VIRTUALTABLE
85715 if( ALWAYS(pTabList->a[i].pTab) && IsVirtual(pTabList->a[i].pTab) ){
85716 pWC->vmask |= ((Bitmask)1 << i);
85717 }
85718 #endif
85719 }
85720 #ifndef NDEBUG
85721 {
85722 Bitmask toTheLeft = 0;
85723 for(i=0; i<pTabList->nSrc; i++){
@@ -86209,10 +86131,21 @@
86131 */
86132 /* First off, code is included that follows the "include" declaration
86133 ** in the input grammar file. */
86134
86135
86136 /*
86137 ** Disable all error recovery processing in the parser push-down
86138 ** automaton.
86139 */
86140 #define YYNOERRORRECOVERY 1
86141
86142 /*
86143 ** Make yytestcase() the same as testcase()
86144 */
86145 #define yytestcase(X) testcase(X)
86146
86147 /*
86148 ** An instance of this structure holds information about the
86149 ** LIMIT clause of a SELECT statement.
86150 */
86151 struct LimitVal {
@@ -86354,11 +86287,11 @@
86287 ** YYNSTATE the combined number of states.
86288 ** YYNRULE the number of rules in the grammar
86289 ** YYERRORSYMBOL is the code number of the error symbol. If not
86290 ** defined, then do no error processing.
86291 */
86292 #define YYCODETYPE unsigned char
86293 #define YYNOCODE 252
86294 #define YYACTIONTYPE unsigned short int
86295 #define YYWILDCARD 65
86296 #define sqlite3ParserTOKENTYPE Token
86297 typedef union {
@@ -86392,10 +86325,22 @@
86325 #define YY_ERROR_ACTION (YYNSTATE+YYNRULE)
86326
86327 /* The yyzerominor constant is used to initialize instances of
86328 ** YYMINORTYPE objects to zero. */
86329 static const YYMINORTYPE yyzerominor = { 0 };
86330
86331 /* Define the yytestcase() macro to be a no-op if is not already defined
86332 ** otherwise.
86333 **
86334 ** Applications can choose to define yytestcase() in the %include section
86335 ** to a macro that can assist in verifying code coverage. For production
86336 ** code the yytestcase() macro should be turned off. But it is useful
86337 ** for testing.
86338 */
86339 #ifndef yytestcase
86340 # define yytestcase(X)
86341 #endif
86342
86343
86344 /* Next are the tables used to determine what action to take based on the
86345 ** current state and lookahead token. These tables are used to implement
86346 ** functions that take a state number and lookahead value and return an
@@ -86970,86 +86915,10 @@
86915 26, /* VIEW => ID */
86916 26, /* VIRTUAL => ID */
86917 26, /* REINDEX => ID */
86918 26, /* RENAME => ID */
86919 26, /* CTIME_KW => ID */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86920 };
86921 #endif /* YYFALLBACK */
86922
86923 /* The following structure represents a single element of the
86924 ** parser's stack. Information stored includes:
@@ -88270,52 +88139,10 @@
88139 ** #line <lineno> <grammarfile>
88140 ** { ... } // User supplied code
88141 ** #line <lineno> <thisfile>
88142 ** break;
88143 */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
88144 case 5: /* explain ::= */
88145 { sqlite3BeginParse(pParse, 0); }
88146 break;
88147 case 6: /* explain ::= EXPLAIN */
88148 { sqlite3BeginParse(pParse, 1); }
@@ -88331,18 +88158,18 @@
88158 break;
88159 case 13: /* transtype ::= */
88160 {yygotominor.yy194 = TK_DEFERRED;}
88161 break;
88162 case 14: /* transtype ::= DEFERRED */
88163 case 15: /* transtype ::= IMMEDIATE */ yytestcase(yyruleno==15);
88164 case 16: /* transtype ::= EXCLUSIVE */ yytestcase(yyruleno==16);
88165 case 114: /* multiselect_op ::= UNION */ yytestcase(yyruleno==114);
88166 case 116: /* multiselect_op ::= EXCEPT|INTERSECT */ yytestcase(yyruleno==116);
88167 {yygotominor.yy194 = yymsp[0].major;}
88168 break;
88169 case 17: /* cmd ::= COMMIT trans_opt */
88170 case 18: /* cmd ::= END trans_opt */ yytestcase(yyruleno==18);
88171 {sqlite3CommitTransaction(pParse);}
88172 break;
88173 case 19: /* cmd ::= ROLLBACK trans_opt */
88174 {sqlite3RollbackTransaction(pParse);}
88175 break;
@@ -88371,30 +88198,30 @@
88198 pParse->db->lookaside.bEnabled = 0;
88199 yygotominor.yy0 = yymsp[0].minor.yy0;
88200 }
88201 break;
88202 case 28: /* ifnotexists ::= */
88203 case 31: /* temp ::= */ yytestcase(yyruleno==31);
88204 case 70: /* autoinc ::= */ yytestcase(yyruleno==70);
88205 case 84: /* init_deferred_pred_opt ::= */ yytestcase(yyruleno==84);
88206 case 86: /* init_deferred_pred_opt ::= INITIALLY IMMEDIATE */ yytestcase(yyruleno==86);
88207 case 97: /* defer_subclause_opt ::= */ yytestcase(yyruleno==97);
88208 case 108: /* ifexists ::= */ yytestcase(yyruleno==108);
88209 case 119: /* distinct ::= ALL */ yytestcase(yyruleno==119);
88210 case 120: /* distinct ::= */ yytestcase(yyruleno==120);
88211 case 222: /* between_op ::= BETWEEN */ yytestcase(yyruleno==222);
88212 case 225: /* in_op ::= IN */ yytestcase(yyruleno==225);
88213 {yygotominor.yy194 = 0;}
88214 break;
88215 case 29: /* ifnotexists ::= IF NOT EXISTS */
88216 case 30: /* temp ::= TEMP */ yytestcase(yyruleno==30);
88217 case 71: /* autoinc ::= AUTOINCR */ yytestcase(yyruleno==71);
88218 case 85: /* init_deferred_pred_opt ::= INITIALLY DEFERRED */ yytestcase(yyruleno==85);
88219 case 107: /* ifexists ::= IF EXISTS */ yytestcase(yyruleno==107);
88220 case 118: /* distinct ::= DISTINCT */ yytestcase(yyruleno==118);
88221 case 223: /* between_op ::= NOT BETWEEN */ yytestcase(yyruleno==223);
88222 case 226: /* in_op ::= NOT IN */ yytestcase(yyruleno==226);
88223 {yygotominor.yy194 = 1;}
88224 break;
88225 case 32: /* create_table_args ::= LP columnlist conslist_opt RP */
88226 {
88227 sqlite3EndTable(pParse,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0,0);
@@ -88417,30 +88244,30 @@
88244 sqlite3AddColumn(pParse,&yymsp[0].minor.yy0);
88245 yygotominor.yy0 = yymsp[0].minor.yy0;
88246 }
88247 break;
88248 case 38: /* id ::= ID */
88249 case 39: /* id ::= INDEXED */ yytestcase(yyruleno==39);
88250 case 40: /* ids ::= ID|STRING */ yytestcase(yyruleno==40);
88251 case 41: /* nm ::= id */ yytestcase(yyruleno==41);
88252 case 42: /* nm ::= STRING */ yytestcase(yyruleno==42);
88253 case 43: /* nm ::= JOIN_KW */ yytestcase(yyruleno==43);
88254 case 46: /* typetoken ::= typename */ yytestcase(yyruleno==46);
88255 case 49: /* typename ::= ids */ yytestcase(yyruleno==49);
88256 case 126: /* as ::= AS nm */ yytestcase(yyruleno==126);
88257 case 127: /* as ::= ids */ yytestcase(yyruleno==127);
88258 case 137: /* dbnm ::= DOT nm */ yytestcase(yyruleno==137);
88259 case 146: /* indexed_opt ::= INDEXED BY nm */ yytestcase(yyruleno==146);
88260 case 251: /* collate ::= COLLATE ids */ yytestcase(yyruleno==251);
88261 case 260: /* nmnum ::= plus_num */ yytestcase(yyruleno==260);
88262 case 261: /* nmnum ::= nm */ yytestcase(yyruleno==261);
88263 case 262: /* nmnum ::= ON */ yytestcase(yyruleno==262);
88264 case 263: /* nmnum ::= DELETE */ yytestcase(yyruleno==263);
88265 case 264: /* nmnum ::= DEFAULT */ yytestcase(yyruleno==264);
88266 case 265: /* plus_num ::= plus_opt number */ yytestcase(yyruleno==265);
88267 case 266: /* minus_num ::= MINUS number */ yytestcase(yyruleno==266);
88268 case 267: /* number ::= INTEGER|FLOAT */ yytestcase(yyruleno==267);
88269 {yygotominor.yy0 = yymsp[0].minor.yy0;}
88270 break;
88271 case 45: /* type ::= typetoken */
88272 {sqlite3AddColumnType(pParse,&yymsp[0].minor.yy0);}
88273 break;
@@ -88458,11 +88285,11 @@
88285 break;
88286 case 50: /* typename ::= typename ids */
88287 {yygotominor.yy0.z=yymsp[-1].minor.yy0.z; yygotominor.yy0.n=yymsp[0].minor.yy0.n+(int)(yymsp[0].minor.yy0.z-yymsp[-1].minor.yy0.z);}
88288 break;
88289 case 57: /* ccons ::= DEFAULT term */
88290 case 59: /* ccons ::= DEFAULT PLUS term */ yytestcase(yyruleno==59);
88291 {sqlite3AddDefaultValue(pParse,&yymsp[0].minor.yy190);}
88292 break;
88293 case 58: /* ccons ::= DEFAULT LP expr RP */
88294 {sqlite3AddDefaultValue(pParse,&yymsp[-1].minor.yy190);}
88295 break;
@@ -88532,16 +88359,16 @@
88359 break;
88360 case 81: /* refact ::= RESTRICT */
88361 { yygotominor.yy194 = OE_Restrict; }
88362 break;
88363 case 82: /* defer_subclause ::= NOT DEFERRABLE init_deferred_pred_opt */
88364 case 83: /* defer_subclause ::= DEFERRABLE init_deferred_pred_opt */ yytestcase(yyruleno==83);
88365 case 98: /* defer_subclause_opt ::= defer_subclause */ yytestcase(yyruleno==98);
88366 case 100: /* onconf ::= ON CONFLICT resolvetype */ yytestcase(yyruleno==100);
88367 case 102: /* orconf ::= OR resolvetype */ yytestcase(yyruleno==102);
88368 case 103: /* resolvetype ::= raisetype */ yytestcase(yyruleno==103);
88369 case 175: /* insert_cmd ::= INSERT orconf */ yytestcase(yyruleno==175);
88370 {yygotominor.yy194 = yymsp[0].minor.yy194;}
88371 break;
88372 case 87: /* conslist_opt ::= */
88373 {yygotominor.yy0.n = 0; yygotominor.yy0.z = 0;}
88374 break;
@@ -88562,18 +88389,18 @@
88389 sqlite3CreateForeignKey(pParse, yymsp[-6].minor.yy148, &yymsp[-3].minor.yy0, yymsp[-2].minor.yy148, yymsp[-1].minor.yy194);
88390 sqlite3DeferForeignKey(pParse, yymsp[0].minor.yy194);
88391 }
88392 break;
88393 case 99: /* onconf ::= */
88394 case 101: /* orconf ::= */ yytestcase(yyruleno==101);
88395 {yygotominor.yy194 = OE_Default;}
88396 break;
88397 case 104: /* resolvetype ::= IGNORE */
88398 {yygotominor.yy194 = OE_Ignore;}
88399 break;
88400 case 105: /* resolvetype ::= REPLACE */
88401 case 176: /* insert_cmd ::= REPLACE */ yytestcase(yyruleno==176);
88402 {yygotominor.yy194 = OE_Replace;}
88403 break;
88404 case 106: /* cmd ::= DROP TABLE ifexists fullname */
88405 {
88406 sqlite3DropTable(pParse, yymsp[0].minor.yy185, 0, yymsp[-1].minor.yy194);
@@ -88617,18 +88444,18 @@
88444 {
88445 yygotominor.yy243 = sqlite3SelectNew(pParse,yymsp[-6].minor.yy148,yymsp[-5].minor.yy185,yymsp[-4].minor.yy72,yymsp[-3].minor.yy148,yymsp[-2].minor.yy72,yymsp[-1].minor.yy148,yymsp[-7].minor.yy194,yymsp[0].minor.yy354.pLimit,yymsp[0].minor.yy354.pOffset);
88446 }
88447 break;
88448 case 121: /* sclp ::= selcollist COMMA */
88449 case 247: /* idxlist_opt ::= LP idxlist RP */ yytestcase(yyruleno==247);
88450 {yygotominor.yy148 = yymsp[-1].minor.yy148;}
88451 break;
88452 case 122: /* sclp ::= */
88453 case 150: /* orderby_opt ::= */ yytestcase(yyruleno==150);
88454 case 158: /* groupby_opt ::= */ yytestcase(yyruleno==158);
88455 case 240: /* exprlist ::= */ yytestcase(yyruleno==240);
88456 case 246: /* idxlist_opt ::= */ yytestcase(yyruleno==246);
88457 {yygotominor.yy148 = 0;}
88458 break;
88459 case 123: /* selcollist ::= sclp expr as */
88460 {
88461 yygotominor.yy148 = sqlite3ExprListAppend(pParse, yymsp[-2].minor.yy148, yymsp[-1].minor.yy190.pExpr);
@@ -88663,11 +88490,11 @@
88490 }
88491 break;
88492 case 131: /* stl_prefix ::= seltablist joinop */
88493 {
88494 yygotominor.yy185 = yymsp[-1].minor.yy185;
88495 if( ALWAYS(yygotominor.yy185 && yygotominor.yy185->nSrc>0) ) yygotominor.yy185->a[yygotominor.yy185->nSrc-1].jointype = (u8)yymsp[0].minor.yy194;
88496 }
88497 break;
88498 case 132: /* stl_prefix ::= */
88499 {yygotominor.yy185 = 0;}
88500 break;
@@ -88682,11 +88509,13 @@
88509 yygotominor.yy185 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-6].minor.yy185,0,0,&yymsp[-2].minor.yy0,yymsp[-4].minor.yy243,yymsp[-1].minor.yy72,yymsp[0].minor.yy254);
88510 }
88511 break;
88512 case 135: /* seltablist ::= stl_prefix LP seltablist RP as on_opt using_opt */
88513 {
88514 if( yymsp[-6].minor.yy185==0 ){
88515 sqlite3ExprDelete(pParse->db, yymsp[-1].minor.yy72);
88516 sqlite3IdListDelete(pParse->db, yymsp[0].minor.yy254);
88517 yygotominor.yy185 = yymsp[-4].minor.yy185;
88518 }else{
88519 Select *pSubquery;
88520 sqlite3SrcListShiftJoinType(yymsp[-4].minor.yy185);
88521 pSubquery = sqlite3SelectNew(pParse,0,yymsp[-4].minor.yy185,0,0,0,0,0,0,0);
@@ -88693,11 +88522,11 @@
88522 yygotominor.yy185 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-6].minor.yy185,0,0,&yymsp[-2].minor.yy0,pSubquery,yymsp[-1].minor.yy72,yymsp[0].minor.yy254);
88523 }
88524 }
88525 break;
88526 case 136: /* dbnm ::= */
88527 case 145: /* indexed_opt ::= */ yytestcase(yyruleno==145);
88528 {yygotominor.yy0.z=0; yygotominor.yy0.n=0;}
88529 break;
88530 case 138: /* fullname ::= nm dbnm */
88531 {yygotominor.yy185 = sqlite3SrcListAppend(pParse->db,0,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0);}
88532 break;
@@ -88712,38 +88541,38 @@
88541 break;
88542 case 142: /* joinop ::= JOIN_KW nm nm JOIN */
88543 { yygotominor.yy194 = sqlite3JoinType(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0); }
88544 break;
88545 case 143: /* on_opt ::= ON expr */
88546 case 154: /* sortitem ::= expr */ yytestcase(yyruleno==154);
88547 case 161: /* having_opt ::= HAVING expr */ yytestcase(yyruleno==161);
88548 case 168: /* where_opt ::= WHERE expr */ yytestcase(yyruleno==168);
88549 case 235: /* case_else ::= ELSE expr */ yytestcase(yyruleno==235);
88550 case 237: /* case_operand ::= expr */ yytestcase(yyruleno==237);
88551 {yygotominor.yy72 = yymsp[0].minor.yy190.pExpr;}
88552 break;
88553 case 144: /* on_opt ::= */
88554 case 160: /* having_opt ::= */ yytestcase(yyruleno==160);
88555 case 167: /* where_opt ::= */ yytestcase(yyruleno==167);
88556 case 236: /* case_else ::= */ yytestcase(yyruleno==236);
88557 case 238: /* case_operand ::= */ yytestcase(yyruleno==238);
88558 {yygotominor.yy72 = 0;}
88559 break;
88560 case 147: /* indexed_opt ::= NOT INDEXED */
88561 {yygotominor.yy0.z=0; yygotominor.yy0.n=1;}
88562 break;
88563 case 148: /* using_opt ::= USING LP inscollist RP */
88564 case 180: /* inscollist_opt ::= LP inscollist RP */ yytestcase(yyruleno==180);
88565 {yygotominor.yy254 = yymsp[-1].minor.yy254;}
88566 break;
88567 case 149: /* using_opt ::= */
88568 case 179: /* inscollist_opt ::= */ yytestcase(yyruleno==179);
88569 {yygotominor.yy254 = 0;}
88570 break;
88571 case 151: /* orderby_opt ::= ORDER BY sortlist */
88572 case 159: /* groupby_opt ::= GROUP BY nexprlist */ yytestcase(yyruleno==159);
88573 case 239: /* exprlist ::= nexprlist */ yytestcase(yyruleno==239);
88574 {yygotominor.yy148 = yymsp[0].minor.yy148;}
88575 break;
88576 case 152: /* sortlist ::= sortlist COMMA sortitem sortorder */
88577 {
88578 yygotominor.yy148 = sqlite3ExprListAppend(pParse,yymsp[-3].minor.yy148,yymsp[-1].minor.yy72);
@@ -88751,15 +88580,15 @@
88580 }
88581 break;
88582 case 153: /* sortlist ::= sortitem sortorder */
88583 {
88584 yygotominor.yy148 = sqlite3ExprListAppend(pParse,0,yymsp[-1].minor.yy72);
88585 if( yygotominor.yy148 && ALWAYS(yygotominor.yy148->a) ) yygotominor.yy148->a[0].sortOrder = (u8)yymsp[0].minor.yy194;
88586 }
88587 break;
88588 case 155: /* sortorder ::= ASC */
88589 case 157: /* sortorder ::= */ yytestcase(yyruleno==157);
88590 {yygotominor.yy194 = SQLITE_SO_ASC;}
88591 break;
88592 case 156: /* sortorder ::= DESC */
88593 {yygotominor.yy194 = SQLITE_SO_DESC;}
88594 break;
@@ -88808,37 +88637,37 @@
88637 break;
88638 case 174: /* cmd ::= insert_cmd INTO fullname inscollist_opt DEFAULT VALUES */
88639 {sqlite3Insert(pParse, yymsp[-3].minor.yy185, 0, 0, yymsp[-2].minor.yy254, yymsp[-5].minor.yy194);}
88640 break;
88641 case 177: /* itemlist ::= itemlist COMMA expr */
88642 case 241: /* nexprlist ::= nexprlist COMMA expr */ yytestcase(yyruleno==241);
88643 {yygotominor.yy148 = sqlite3ExprListAppend(pParse,yymsp[-2].minor.yy148,yymsp[0].minor.yy190.pExpr);}
88644 break;
88645 case 178: /* itemlist ::= expr */
88646 case 242: /* nexprlist ::= expr */ yytestcase(yyruleno==242);
88647 {yygotominor.yy148 = sqlite3ExprListAppend(pParse,0,yymsp[0].minor.yy190.pExpr);}
88648 break;
88649 case 181: /* inscollist ::= inscollist COMMA nm */
88650 {yygotominor.yy254 = sqlite3IdListAppend(pParse->db,yymsp[-2].minor.yy254,&yymsp[0].minor.yy0);}
88651 break;
88652 case 182: /* inscollist ::= nm */
88653 {yygotominor.yy254 = sqlite3IdListAppend(pParse->db,0,&yymsp[0].minor.yy0);}
88654 break;
88655 case 183: /* expr ::= term */
88656 case 211: /* escape ::= ESCAPE expr */ yytestcase(yyruleno==211);
88657 {yygotominor.yy190 = yymsp[0].minor.yy190;}
88658 break;
88659 case 184: /* expr ::= LP expr RP */
88660 {yygotominor.yy190.pExpr = yymsp[-1].minor.yy190.pExpr; spanSet(&yygotominor.yy190,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0);}
88661 break;
88662 case 185: /* term ::= NULL */
88663 case 190: /* term ::= INTEGER|FLOAT|BLOB */ yytestcase(yyruleno==190);
88664 case 191: /* term ::= STRING */ yytestcase(yyruleno==191);
88665 {spanExpr(&yygotominor.yy190, pParse, yymsp[0].major, &yymsp[0].minor.yy0);}
88666 break;
88667 case 186: /* expr ::= id */
88668 case 187: /* expr ::= JOIN_KW */ yytestcase(yyruleno==187);
88669 {spanExpr(&yygotominor.yy190, pParse, TK_ID, &yymsp[0].minor.yy0);}
88670 break;
88671 case 188: /* expr ::= nm DOT nm */
88672 {
88673 Expr *temp1 = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[-2].minor.yy0);
@@ -88892,11 +88721,11 @@
88721 spanSet(&yygotominor.yy190,&yymsp[-5].minor.yy0,&yymsp[0].minor.yy0);
88722 }
88723 break;
88724 case 196: /* expr ::= ID LP distinct exprlist RP */
88725 {
88726 if( yymsp[-1].minor.yy148 && yymsp[-1].minor.yy148->nExpr>pParse->db->aLimit[SQLITE_LIMIT_FUNCTION_ARG] ){
88727 sqlite3ErrorMsg(pParse, "too many arguments on function %T", &yymsp[-4].minor.yy0);
88728 }
88729 yygotominor.yy190.pExpr = sqlite3ExprFunction(pParse, yymsp[-1].minor.yy148, &yymsp[-4].minor.yy0);
88730 spanSet(&yygotominor.yy190,&yymsp[-4].minor.yy0,&yymsp[0].minor.yy0);
88731 if( yymsp[-2].minor.yy194 && yygotominor.yy190.pExpr ){
@@ -88920,25 +88749,25 @@
88749 }
88750 spanSet(&yygotominor.yy190, &yymsp[0].minor.yy0, &yymsp[0].minor.yy0);
88751 }
88752 break;
88753 case 199: /* expr ::= expr AND expr */
88754 case 200: /* expr ::= expr OR expr */ yytestcase(yyruleno==200);
88755 case 201: /* expr ::= expr LT|GT|GE|LE expr */ yytestcase(yyruleno==201);
88756 case 202: /* expr ::= expr EQ|NE expr */ yytestcase(yyruleno==202);
88757 case 203: /* expr ::= expr BITAND|BITOR|LSHIFT|RSHIFT expr */ yytestcase(yyruleno==203);
88758 case 204: /* expr ::= expr PLUS|MINUS expr */ yytestcase(yyruleno==204);
88759 case 205: /* expr ::= expr STAR|SLASH|REM expr */ yytestcase(yyruleno==205);
88760 case 206: /* expr ::= expr CONCAT expr */ yytestcase(yyruleno==206);
88761 {spanBinaryExpr(&yygotominor.yy190,pParse,yymsp[-1].major,&yymsp[-2].minor.yy190,&yymsp[0].minor.yy190);}
88762 break;
88763 case 207: /* likeop ::= LIKE_KW */
88764 case 209: /* likeop ::= MATCH */ yytestcase(yyruleno==209);
88765 {yygotominor.yy392.eOperator = yymsp[0].minor.yy0; yygotominor.yy392.not = 0;}
88766 break;
88767 case 208: /* likeop ::= NOT LIKE_KW */
88768 case 210: /* likeop ::= NOT MATCH */ yytestcase(yyruleno==210);
88769 {yygotominor.yy392.eOperator = yymsp[0].minor.yy0; yygotominor.yy392.not = 1;}
88770 break;
88771 case 212: /* escape ::= */
88772 {memset(&yygotominor.yy190,0,sizeof(yygotominor.yy190));}
88773 break;
@@ -88968,11 +88797,11 @@
88797 break;
88798 case 217: /* expr ::= expr IS NOT NULL */
88799 {spanUnaryPostfix(&yygotominor.yy190,pParse,TK_NOTNULL,&yymsp[-3].minor.yy190,&yymsp[0].minor.yy0);}
88800 break;
88801 case 218: /* expr ::= NOT expr */
88802 case 219: /* expr ::= BITNOT expr */ yytestcase(yyruleno==219);
88803 {spanUnaryPrefix(&yygotominor.yy190,pParse,yymsp[-1].major,&yymsp[0].minor.yy190,&yymsp[-1].minor.yy0);}
88804 break;
88805 case 220: /* expr ::= MINUS expr */
88806 {spanUnaryPrefix(&yygotominor.yy190,pParse,TK_UMINUS,&yymsp[0].minor.yy190,&yymsp[-1].minor.yy0);}
88807 break;
@@ -89098,11 +88927,11 @@
88927 sqlite3SrcListAppend(pParse->db,0,&yymsp[-3].minor.yy0,0), yymsp[-1].minor.yy148, yymsp[-9].minor.yy194,
88928 &yymsp[-10].minor.yy0, &yymsp[0].minor.yy0, SQLITE_SO_ASC, yymsp[-7].minor.yy194);
88929 }
88930 break;
88931 case 244: /* uniqueflag ::= UNIQUE */
88932 case 293: /* raisetype ::= ABORT */ yytestcase(yyruleno==293);
88933 {yygotominor.yy194 = OE_Abort;}
88934 break;
88935 case 245: /* uniqueflag ::= */
88936 {yygotominor.yy194 = OE_None;}
88937 break;
@@ -89137,11 +88966,11 @@
88966 break;
88967 case 252: /* cmd ::= DROP INDEX ifexists fullname */
88968 {sqlite3DropIndex(pParse, yymsp[0].minor.yy185, yymsp[-1].minor.yy194);}
88969 break;
88970 case 253: /* cmd ::= VACUUM */
88971 case 254: /* cmd ::= VACUUM nm */ yytestcase(yyruleno==254);
88972 {sqlite3Vacuum(pParse);}
88973 break;
88974 case 255: /* cmd ::= PRAGMA nm dbnm */
88975 {sqlite3Pragma(pParse,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0,0,0);}
88976 break;
@@ -89170,32 +88999,32 @@
88999 sqlite3BeginTrigger(pParse, &yymsp[-7].minor.yy0, &yymsp[-6].minor.yy0, yymsp[-5].minor.yy194, yymsp[-4].minor.yy332.a, yymsp[-4].minor.yy332.b, yymsp[-2].minor.yy185, yymsp[0].minor.yy72, yymsp[-10].minor.yy194, yymsp[-8].minor.yy194);
89000 yygotominor.yy0 = (yymsp[-6].minor.yy0.n==0?yymsp[-7].minor.yy0:yymsp[-6].minor.yy0);
89001 }
89002 break;
89003 case 272: /* trigger_time ::= BEFORE */
89004 case 275: /* trigger_time ::= */ yytestcase(yyruleno==275);
89005 { yygotominor.yy194 = TK_BEFORE; }
89006 break;
89007 case 273: /* trigger_time ::= AFTER */
89008 { yygotominor.yy194 = TK_AFTER; }
89009 break;
89010 case 274: /* trigger_time ::= INSTEAD OF */
89011 { yygotominor.yy194 = TK_INSTEAD;}
89012 break;
89013 case 276: /* trigger_event ::= DELETE|INSERT */
89014 case 277: /* trigger_event ::= UPDATE */ yytestcase(yyruleno==277);
89015 {yygotominor.yy332.a = yymsp[0].major; yygotominor.yy332.b = 0;}
89016 break;
89017 case 278: /* trigger_event ::= UPDATE OF inscollist */
89018 {yygotominor.yy332.a = TK_UPDATE; yygotominor.yy332.b = yymsp[0].minor.yy254;}
89019 break;
89020 case 281: /* when_clause ::= */
89021 case 298: /* key_opt ::= */ yytestcase(yyruleno==298);
89022 { yygotominor.yy72 = 0; }
89023 break;
89024 case 282: /* when_clause ::= WHEN expr */
89025 case 299: /* key_opt ::= KEY expr */ yytestcase(yyruleno==299);
89026 { yygotominor.yy72 = yymsp[0].minor.yy190.pExpr; }
89027 break;
89028 case 283: /* trigger_cmd_list ::= trigger_cmd_list trigger_cmd SEMI */
89029 {
89030 assert( yymsp[-2].minor.yy145!=0 );
@@ -89308,14 +89137,55 @@
89137 break;
89138 case 316: /* vtabarg ::= */
89139 {sqlite3VtabArgInit(pParse);}
89140 break;
89141 case 318: /* vtabargtoken ::= ANY */
89142 case 319: /* vtabargtoken ::= lp anylist RP */ yytestcase(yyruleno==319);
89143 case 320: /* lp ::= LP */ yytestcase(yyruleno==320);
89144 {sqlite3VtabArgExtend(pParse,&yymsp[0].minor.yy0);}
89145 break;
89146 default:
89147 /* (0) input ::= cmdlist */ yytestcase(yyruleno==0);
89148 /* (1) cmdlist ::= cmdlist ecmd */ yytestcase(yyruleno==1);
89149 /* (2) cmdlist ::= ecmd */ yytestcase(yyruleno==2);
89150 /* (3) ecmd ::= SEMI */ yytestcase(yyruleno==3);
89151 /* (4) ecmd ::= explain cmdx SEMI */ yytestcase(yyruleno==4);
89152 /* (10) trans_opt ::= */ yytestcase(yyruleno==10);
89153 /* (11) trans_opt ::= TRANSACTION */ yytestcase(yyruleno==11);
89154 /* (12) trans_opt ::= TRANSACTION nm */ yytestcase(yyruleno==12);
89155 /* (20) savepoint_opt ::= SAVEPOINT */ yytestcase(yyruleno==20);
89156 /* (21) savepoint_opt ::= */ yytestcase(yyruleno==21);
89157 /* (25) cmd ::= create_table create_table_args */ yytestcase(yyruleno==25);
89158 /* (34) columnlist ::= columnlist COMMA column */ yytestcase(yyruleno==34);
89159 /* (35) columnlist ::= column */ yytestcase(yyruleno==35);
89160 /* (44) type ::= */ yytestcase(yyruleno==44);
89161 /* (51) signed ::= plus_num */ yytestcase(yyruleno==51);
89162 /* (52) signed ::= minus_num */ yytestcase(yyruleno==52);
89163 /* (53) carglist ::= carglist carg */ yytestcase(yyruleno==53);
89164 /* (54) carglist ::= */ yytestcase(yyruleno==54);
89165 /* (55) carg ::= CONSTRAINT nm ccons */ yytestcase(yyruleno==55);
89166 /* (56) carg ::= ccons */ yytestcase(yyruleno==56);
89167 /* (62) ccons ::= NULL onconf */ yytestcase(yyruleno==62);
89168 /* (89) conslist ::= conslist COMMA tcons */ yytestcase(yyruleno==89);
89169 /* (90) conslist ::= conslist tcons */ yytestcase(yyruleno==90);
89170 /* (91) conslist ::= tcons */ yytestcase(yyruleno==91);
89171 /* (92) tcons ::= CONSTRAINT nm */ yytestcase(yyruleno==92);
89172 /* (268) plus_opt ::= PLUS */ yytestcase(yyruleno==268);
89173 /* (269) plus_opt ::= */ yytestcase(yyruleno==269);
89174 /* (279) foreach_clause ::= */ yytestcase(yyruleno==279);
89175 /* (280) foreach_clause ::= FOR EACH ROW */ yytestcase(yyruleno==280);
89176 /* (300) database_kw_opt ::= DATABASE */ yytestcase(yyruleno==300);
89177 /* (301) database_kw_opt ::= */ yytestcase(yyruleno==301);
89178 /* (309) kwcolumn_opt ::= */ yytestcase(yyruleno==309);
89179 /* (310) kwcolumn_opt ::= COLUMNKW */ yytestcase(yyruleno==310);
89180 /* (314) vtabarglist ::= vtabarg */ yytestcase(yyruleno==314);
89181 /* (315) vtabarglist ::= vtabarglist COMMA vtabarg */ yytestcase(yyruleno==315);
89182 /* (317) vtabarg ::= vtabarg vtabargtoken */ yytestcase(yyruleno==317);
89183 /* (321) anylist ::= */ yytestcase(yyruleno==321);
89184 /* (322) anylist ::= anylist LP anylist RP */ yytestcase(yyruleno==322);
89185 /* (323) anylist ::= anylist ANY */ yytestcase(yyruleno==323);
89186 break;
89187 };
89188 yygoto = yyRuleInfo[yyruleno].lhs;
89189 yysize = yyRuleInfo[yyruleno].nrhs;
89190 yypParser->yyidx -= yysize;
89191 yyact = yy_find_reduce_action(yymsp[-yysize].stateno,(YYCODETYPE)yygoto);
@@ -89343,10 +89213,11 @@
89213 }
89214
89215 /*
89216 ** The following code executes when the parse fails
89217 */
89218 #ifndef YYNOERRORRECOVERY
89219 static void yy_parse_failed(
89220 yyParser *yypParser /* The parser */
89221 ){
89222 sqlite3ParserARG_FETCH;
89223 #ifndef NDEBUG
@@ -89357,10 +89228,11 @@
89228 while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser);
89229 /* Here code is inserted which will be executed whenever the
89230 ** parser fails */
89231 sqlite3ParserARG_STORE; /* Suppress warning about unused %extra_argument variable */
89232 }
89233 #endif /* YYNOERRORRECOVERY */
89234
89235 /*
89236 ** The following code executes when a syntax error first occurs.
89237 */
89238 static void yy_syntax_error(
@@ -89527,10 +89399,22 @@
89399 yy_shift(yypParser,yyact,YYERRORSYMBOL,&u2);
89400 }
89401 }
89402 yypParser->yyerrcnt = 3;
89403 yyerrorhit = 1;
89404 #elif defined(YYNOERRORRECOVERY)
89405 /* If the YYNOERRORRECOVERY macro is defined, then do not attempt to
89406 ** do any kind of error recovery. Instead, simply invoke the syntax
89407 ** error routine and continue going as if nothing had happened.
89408 **
89409 ** Applications can set this macro (for example inside %include) if
89410 ** they intend to abandon the parse upon the first syntax error seen.
89411 */
89412 yy_syntax_error(yypParser,yymajor,yyminorunion);
89413 yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion);
89414 yymajor = YYNOCODE;
89415
89416 #else /* YYERRORSYMBOL is not defined */
89417 /* This is what we do if the grammar does not define ERROR:
89418 **
89419 ** * Report an error message, and throw away the input token.
89420 **
@@ -89571,11 +89455,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
@@ -89624,11 +89508,11 @@
89508 /************** Begin file keywordhash.h *************************************/
89509 /***** This file contains automatically generated code ******
89510 **
89511 ** The code in this file has been automatically generated by
89512 **
89513 ** $Header: /sqlite/sqlite/tool/mkkeywordhash.c,v 1.38 2009/06/09 14:27:41 drh Exp $
89514 **
89515 ** The code in this file implements a function that determines whether
89516 ** or not a given identifier is really an SQL keyword. The same thing
89517 ** might be implemented more directly using a hand-written hash table.
89518 ** But by using this automatically generated code, the size of the code
@@ -89759,129 +89643,129 @@
89643 h = ((charMap(z[0])*4) ^
89644 (charMap(z[n-1])*3) ^
89645 n) % 127;
89646 for(i=((int)aHash[h])-1; i>=0; i=((int)aNext[i])-1){
89647 if( aLen[i]==n && sqlite3StrNICmp(&zText[aOffset[i]],z,n)==0 ){
89648 testcase( i==0 ); /* REINDEX */
89649 testcase( i==1 ); /* INDEXED */
89650 testcase( i==2 ); /* INDEX */
89651 testcase( i==3 ); /* DESC */
89652 testcase( i==4 ); /* ESCAPE */
89653 testcase( i==5 ); /* EACH */
89654 testcase( i==6 ); /* CHECK */
89655 testcase( i==7 ); /* KEY */
89656 testcase( i==8 ); /* BEFORE */
89657 testcase( i==9 ); /* FOREIGN */
89658 testcase( i==10 ); /* FOR */
89659 testcase( i==11 ); /* IGNORE */
89660 testcase( i==12 ); /* REGEXP */
89661 testcase( i==13 ); /* EXPLAIN */
89662 testcase( i==14 ); /* INSTEAD */
89663 testcase( i==15 ); /* ADD */
89664 testcase( i==16 ); /* DATABASE */
89665 testcase( i==17 ); /* AS */
89666 testcase( i==18 ); /* SELECT */
89667 testcase( i==19 ); /* TABLE */
89668 testcase( i==20 ); /* LEFT */
89669 testcase( i==21 ); /* THEN */
89670 testcase( i==22 ); /* END */
89671 testcase( i==23 ); /* DEFERRABLE */
89672 testcase( i==24 ); /* ELSE */
89673 testcase( i==25 ); /* EXCEPT */
89674 testcase( i==26 ); /* TRANSACTION */
89675 testcase( i==27 ); /* ON */
89676 testcase( i==28 ); /* NATURAL */
89677 testcase( i==29 ); /* ALTER */
89678 testcase( i==30 ); /* RAISE */
89679 testcase( i==31 ); /* EXCLUSIVE */
89680 testcase( i==32 ); /* EXISTS */
89681 testcase( i==33 ); /* SAVEPOINT */
89682 testcase( i==34 ); /* INTERSECT */
89683 testcase( i==35 ); /* TRIGGER */
89684 testcase( i==36 ); /* REFERENCES */
89685 testcase( i==37 ); /* CONSTRAINT */
89686 testcase( i==38 ); /* INTO */
89687 testcase( i==39 ); /* OFFSET */
89688 testcase( i==40 ); /* OF */
89689 testcase( i==41 ); /* SET */
89690 testcase( i==42 ); /* TEMP */
89691 testcase( i==43 ); /* TEMPORARY */
89692 testcase( i==44 ); /* OR */
89693 testcase( i==45 ); /* UNIQUE */
89694 testcase( i==46 ); /* QUERY */
89695 testcase( i==47 ); /* ATTACH */
89696 testcase( i==48 ); /* HAVING */
89697 testcase( i==49 ); /* GROUP */
89698 testcase( i==50 ); /* UPDATE */
89699 testcase( i==51 ); /* BEGIN */
89700 testcase( i==52 ); /* INNER */
89701 testcase( i==53 ); /* RELEASE */
89702 testcase( i==54 ); /* BETWEEN */
89703 testcase( i==55 ); /* NOTNULL */
89704 testcase( i==56 ); /* NOT */
89705 testcase( i==57 ); /* NULL */
89706 testcase( i==58 ); /* LIKE */
89707 testcase( i==59 ); /* CASCADE */
89708 testcase( i==60 ); /* ASC */
89709 testcase( i==61 ); /* DELETE */
89710 testcase( i==62 ); /* CASE */
89711 testcase( i==63 ); /* COLLATE */
89712 testcase( i==64 ); /* CREATE */
89713 testcase( i==65 ); /* CURRENT_DATE */
89714 testcase( i==66 ); /* DETACH */
89715 testcase( i==67 ); /* IMMEDIATE */
89716 testcase( i==68 ); /* JOIN */
89717 testcase( i==69 ); /* INSERT */
89718 testcase( i==70 ); /* MATCH */
89719 testcase( i==71 ); /* PLAN */
89720 testcase( i==72 ); /* ANALYZE */
89721 testcase( i==73 ); /* PRAGMA */
89722 testcase( i==74 ); /* ABORT */
89723 testcase( i==75 ); /* VALUES */
89724 testcase( i==76 ); /* VIRTUAL */
89725 testcase( i==77 ); /* LIMIT */
89726 testcase( i==78 ); /* WHEN */
89727 testcase( i==79 ); /* WHERE */
89728 testcase( i==80 ); /* RENAME */
89729 testcase( i==81 ); /* AFTER */
89730 testcase( i==82 ); /* REPLACE */
89731 testcase( i==83 ); /* AND */
89732 testcase( i==84 ); /* DEFAULT */
89733 testcase( i==85 ); /* AUTOINCREMENT */
89734 testcase( i==86 ); /* TO */
89735 testcase( i==87 ); /* IN */
89736 testcase( i==88 ); /* CAST */
89737 testcase( i==89 ); /* COLUMN */
89738 testcase( i==90 ); /* COMMIT */
89739 testcase( i==91 ); /* CONFLICT */
89740 testcase( i==92 ); /* CROSS */
89741 testcase( i==93 ); /* CURRENT_TIMESTAMP */
89742 testcase( i==94 ); /* CURRENT_TIME */
89743 testcase( i==95 ); /* PRIMARY */
89744 testcase( i==96 ); /* DEFERRED */
89745 testcase( i==97 ); /* DISTINCT */
89746 testcase( i==98 ); /* IS */
89747 testcase( i==99 ); /* DROP */
89748 testcase( i==100 ); /* FAIL */
89749 testcase( i==101 ); /* FROM */
89750 testcase( i==102 ); /* FULL */
89751 testcase( i==103 ); /* GLOB */
89752 testcase( i==104 ); /* BY */
89753 testcase( i==105 ); /* IF */
89754 testcase( i==106 ); /* ISNULL */
89755 testcase( i==107 ); /* ORDER */
89756 testcase( i==108 ); /* RESTRICT */
89757 testcase( i==109 ); /* OUTER */
89758 testcase( i==110 ); /* RIGHT */
89759 testcase( i==111 ); /* ROLLBACK */
89760 testcase( i==112 ); /* ROW */
89761 testcase( i==113 ); /* UNION */
89762 testcase( i==114 ); /* USING */
89763 testcase( i==115 ); /* VACUUM */
89764 testcase( i==116 ); /* VIEW */
89765 testcase( i==117 ); /* INITIALLY */
89766 testcase( i==118 ); /* ALL */
89767 return aCode[i];
89768 }
89769 }
89770 return TK_ID;
89771 }
@@ -89947,10 +89831,15 @@
89831 */
89832 SQLITE_PRIVATE int sqlite3GetToken(const unsigned char *z, int *tokenType){
89833 int i, c;
89834 switch( *z ){
89835 case ' ': case '\t': case '\n': case '\f': case '\r': {
89836 testcase( z[0]==' ' );
89837 testcase( z[0]=='\t' );
89838 testcase( z[0]=='\n' );
89839 testcase( z[0]=='\f' );
89840 testcase( z[0]=='\r' );
89841 for(i=1; sqlite3Isspace(z[i]); i++){}
89842 *tokenType = TK_SPACE;
89843 return i;
89844 }
89845 case '-': {
@@ -90059,10 +89948,13 @@
89948 }
89949 case '`':
89950 case '\'':
89951 case '"': {
89952 int delim = z[0];
89953 testcase( delim=='`' );
89954 testcase( delim=='\'' );
89955 testcase( delim=='"' );
89956 for(i=1; (c=z[i])!=0; i++){
89957 if( c==delim ){
89958 if( z[i+1]==delim ){
89959 i++;
89960 }else{
@@ -90092,10 +89984,14 @@
89984 /* If the next character is a digit, this is a floating point
89985 ** number that begins with ".". Fall thru into the next case */
89986 }
89987 case '0': case '1': case '2': case '3': case '4':
89988 case '5': case '6': case '7': case '8': case '9': {
89989 testcase( z[0]=='0' ); testcase( z[0]=='1' ); testcase( z[0]=='2' );
89990 testcase( z[0]=='3' ); testcase( z[0]=='4' ); testcase( z[0]=='5' );
89991 testcase( z[0]=='6' ); testcase( z[0]=='7' ); testcase( z[0]=='8' );
89992 testcase( z[0]=='9' );
89993 *tokenType = TK_INTEGER;
89994 for(i=0; sqlite3Isdigit(z[i]); i++){}
89995 #ifndef SQLITE_OMIT_FLOATING_POINT
89996 if( z[i]=='.' ){
89997 i++;
@@ -90143,10 +90039,11 @@
90039 case '$':
90040 #endif
90041 case '@': /* For compatibility with MS SQL Server */
90042 case ':': {
90043 int n = 0;
90044 testcase( z[0]=='$' ); testcase( z[0]=='@' ); testcase( z[0]==':' );
90045 *tokenType = TK_VARIABLE;
90046 for(i=1; (c=z[i])!=0; i++){
90047 if( IdChar(c) ){
90048 n++;
90049 #ifndef SQLITE_OMIT_TCL_VARIABLE
@@ -90170,10 +90067,11 @@
90067 if( n==0 ) *tokenType = TK_ILLEGAL;
90068 return i;
90069 }
90070 #ifndef SQLITE_OMIT_BLOB_LITERAL
90071 case 'x': case 'X': {
90072 testcase( z[0]=='x' ); testcase( z[0]=='X' );
90073 if( z[1]=='\'' ){
90074 *tokenType = TK_BLOB;
90075 for(i=2; (c=z[i])!=0 && c!='\''; i++){
90076 if( !sqlite3Isxdigit(c) ){
90077 *tokenType = TK_ILLEGAL;
@@ -90248,12 +90146,12 @@
90146 break;
90147 }
90148 switch( tokenType ){
90149 case TK_SPACE: {
90150 if( db->u1.isInterrupted ){
90151 sqlite3ErrorMsg(pParse, "interrupt");
90152 pParse->rc = SQLITE_INTERRUPT;
 
90153 goto abort_parse;
90154 }
90155 break;
90156 }
90157 case TK_ILLEGAL: {
@@ -90296,16 +90194,13 @@
90194 pParse->rc = SQLITE_NOMEM;
90195 }
90196 if( pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE && pParse->zErrMsg==0 ){
90197 sqlite3SetString(&pParse->zErrMsg, db, "%s", sqlite3ErrStr(pParse->rc));
90198 }
90199 assert( pzErrMsg!=0 );
90200 if( pParse->zErrMsg ){
90201 *pzErrMsg = pParse->zErrMsg;
 
 
 
 
90202 pParse->zErrMsg = 0;
90203 nErr++;
90204 }
90205 if( pParse->pVdbe && pParse->nErr>0 && pParse->nested==0 ){
90206 sqlite3VdbeDelete(pParse->pVdbe);
@@ -90336,11 +90231,11 @@
90231 while( pParse->pZombieTab ){
90232 Table *p = pParse->pZombieTab;
90233 pParse->pZombieTab = p->pNextZombie;
90234 sqlite3DeleteTable(p);
90235 }
90236 if( nErr>0 && pParse->rc==SQLITE_OK ){
90237 pParse->rc = SQLITE_ERROR;
90238 }
90239 return nErr;
90240 }
90241
@@ -90639,11 +90534,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 ********************************************/
@@ -91061,11 +90956,10 @@
90956 memset(&sqlite3GlobalConfig.m, 0, sizeof(sqlite3GlobalConfig.m));
90957 }else{
90958 /* The heap pointer is not NULL, then install one of the
90959 ** mem5.c/mem3.c methods. If neither ENABLE_MEMSYS3 nor
90960 ** ENABLE_MEMSYS5 is defined, return an error.
 
90961 */
90962 #ifdef SQLITE_ENABLE_MEMSYS3
90963 sqlite3GlobalConfig.m = *sqlite3MemGetMemsys3();
90964 #endif
90965 #ifdef SQLITE_ENABLE_MEMSYS5
@@ -91296,17 +91190,10 @@
91190 if( !sqlite3SafetyCheckSickOrOk(db) ){
91191 return SQLITE_MISUSE;
91192 }
91193 sqlite3_mutex_enter(db->mutex);
91194
 
 
 
 
 
 
 
91195 sqlite3ResetInternalSchema(db, 0);
91196
91197 /* If a transaction is open, the ResetInternalSchema() call above
91198 ** will not have called the xDisconnect() method on any virtual
91199 ** tables in the db->aVTrans[] array. The following sqlite3VtabRollback()
91200
+42 -21
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -28,11 +28,11 @@
2828
** The name of this file under configuration management is "sqlite.h.in".
2929
** The makefile makes some minor changes to this file (such as inserting
3030
** the version number) and changes its name to "sqlite3.h" as
3131
** part of the build process.
3232
**
33
-** @(#) $Id: sqlite.h.in,v 1.455 2009/05/24 21:59:28 drh Exp $
33
+** @(#) $Id: sqlite.h.in,v 1.458 2009/06/19 22:50:31 drh Exp $
3434
*/
3535
#ifndef _SQLITE3_H_
3636
#define _SQLITE3_H_
3737
#include <stdarg.h> /* Needed for the definition of va_list */
3838
@@ -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.14"
103
-#define SQLITE_VERSION_NUMBER 3006014
102
+#define SQLITE_VERSION "3.6.15"
103
+#define SQLITE_VERSION_NUMBER 3006015
104104
105105
/*
106106
** CAPI3REF: Run-Time Library Version Numbers {H10020} <S60100>
107107
** KEYWORDS: sqlite3_version
108108
**
@@ -492,10 +492,16 @@
492492
** [sqlite3_file] object (or, more commonly, a subclass of the
493493
** [sqlite3_file] object) with a pointer to an instance of this object.
494494
** This object defines the methods used to perform various operations
495495
** against the open file represented by the [sqlite3_file] object.
496496
**
497
+** If the xOpen method sets the sqlite3_file.pMethods element
498
+** to a non-NULL pointer, then the sqlite3_io_methods.xClose method
499
+** may be invoked even if the xOpen reported that it failed. The
500
+** only way to prevent a call to xClose following a failed xOpen
501
+** is for the xOpen to set the sqlite3_file.pMethods element to NULL.
502
+**
497503
** The flags argument to xSync may be one of [SQLITE_SYNC_NORMAL] or
498504
** [SQLITE_SYNC_FULL]. The first choice is the normal fsync().
499505
** The second choice is a Mac OS X style fullsync. The [SQLITE_SYNC_DATAONLY]
500506
** flag may be ORed in to indicate that only the data of the file
501507
** and not its inode needs to be synced.
@@ -652,15 +658,15 @@
652658
**
653659
** SQLite will guarantee that the zFilename parameter to xOpen
654660
** is either a NULL pointer or string obtained
655661
** from xFullPathname(). SQLite further guarantees that
656662
** the string will be valid and unchanged until xClose() is
657
-** called. Because of the previous sentense,
663
+** called. Because of the previous sentence,
658664
** the [sqlite3_file] can safely store a pointer to the
659665
** filename if it needs to remember the filename for some reason.
660666
** If the zFilename parameter is xOpen is a NULL pointer then xOpen
661
-** must invite its own temporary name for the file. Whenever the
667
+** must invent its own temporary name for the file. Whenever the
662668
** xFilename parameter is NULL it will also be the case that the
663669
** flags parameter will include [SQLITE_OPEN_DELETEONCLOSE].
664670
**
665671
** The flags argument to xOpen() includes all bits set in
666672
** the flags argument to [sqlite3_open_v2()]. Or if [sqlite3_open()]
@@ -712,11 +718,16 @@
712718
** for exclusive access.
713719
**
714720
** At least szOsFile bytes of memory are allocated by SQLite
715721
** to hold the [sqlite3_file] structure passed as the third
716722
** argument to xOpen. The xOpen method does not have to
717
-** allocate the structure; it should just fill it in.
723
+** allocate the structure; it should just fill it in. Note that
724
+** the xOpen method must set the sqlite3_file.pMethods to either
725
+** a valid [sqlite3_io_methods] object or to NULL. xOpen must do
726
+** this even if the open fails. SQLite expects that the sqlite3_file.pMethods
727
+** element will be valid after xOpen returns regardless of the success
728
+** or failure of the xOpen call.
718729
**
719730
** The flags argument to xAccess() may be [SQLITE_ACCESS_EXISTS]
720731
** to test for the existence of a file, or [SQLITE_ACCESS_READWRITE] to
721732
** test whether a file is readable and writable, or [SQLITE_ACCESS_READ]
722733
** to test whether a file is at least readable. The file can be a
@@ -1034,16 +1045,18 @@
10341045
** </ul>
10351046
** </dd>
10361047
**
10371048
** <dt>SQLITE_CONFIG_SCRATCH</dt>
10381049
** <dd>This option specifies a static memory buffer that SQLite can use for
1039
-** scratch memory. There are three arguments: A pointer to the memory, the
1040
-** size of each scratch buffer (sz), and the number of buffers (N). The sz
1050
+** scratch memory. There are three arguments: A pointer an 8-byte
1051
+** aligned memory buffer from which the scrach allocations will be
1052
+** drawn, the size of each scratch allocation (sz),
1053
+** and the maximum number of scratch allocations (N). The sz
10411054
** argument must be a multiple of 16. The sz parameter should be a few bytes
1042
-** larger than the actual scratch space required due internal overhead.
1043
-** The first
1044
-** argument should point to an allocation of at least sz*N bytes of memory.
1055
+** larger than the actual scratch space required due to internal overhead.
1056
+** The first argument should pointer to an 8-byte aligned buffer
1057
+** of at least sz*N bytes of memory.
10451058
** SQLite will use no more than one scratch buffer at once per thread, so
10461059
** N should be set to the expected maximum number of threads. The sz
10471060
** parameter should be 6 times the size of the largest database page size.
10481061
** Scratch buffers are used as part of the btree balance operation. If
10491062
** The btree balancer needs additional memory beyond what is provided by
@@ -1053,33 +1066,41 @@
10531066
** <dt>SQLITE_CONFIG_PAGECACHE</dt>
10541067
** <dd>This option specifies a static memory buffer that SQLite can use for
10551068
** the database page cache with the default page cache implemenation.
10561069
** This configuration should not be used if an application-define page
10571070
** cache implementation is loaded using the SQLITE_CONFIG_PCACHE option.
1058
-** There are three arguments to this option: A pointer to the
1071
+** There are three arguments to this option: A pointer to 8-byte aligned
10591072
** memory, the size of each page buffer (sz), and the number of pages (N).
1060
-** The sz argument must be a power of two between 512 and 32768. The first
1073
+** The sz argument should be the size of the largest database page
1074
+** (a power of two between 512 and 32768) plus a little extra for each
1075
+** page header. The page header size is 20 to 40 bytes depending on
1076
+** the host architecture. It is harmless, apart from the wasted memory,
1077
+** to make sz a little too large. The first
10611078
** argument should point to an allocation of at least sz*N bytes of memory.
10621079
** SQLite will use the memory provided by the first argument to satisfy its
10631080
** memory needs for the first N pages that it adds to cache. If additional
10641081
** page cache memory is needed beyond what is provided by this option, then
10651082
** SQLite goes to [sqlite3_malloc()] for the additional storage space.
10661083
** The implementation might use one or more of the N buffers to hold
1067
-** memory accounting information. </dd>
1084
+** memory accounting information. The pointer in the first argument must
1085
+** be aligned to an 8-byte boundary or subsequent behavior of SQLite
1086
+** will be undefined.</dd>
10681087
**
10691088
** <dt>SQLITE_CONFIG_HEAP</dt>
10701089
** <dd>This option specifies a static memory buffer that SQLite will use
10711090
** for all of its dynamic memory allocation needs beyond those provided
10721091
** for by [SQLITE_CONFIG_SCRATCH] and [SQLITE_CONFIG_PAGECACHE].
1073
-** There are three arguments: A pointer to the memory, the number of
1074
-** bytes in the memory buffer, and the minimum allocation size. If
1075
-** the first pointer (the memory pointer) is NULL, then SQLite reverts
1092
+** There are three arguments: An 8-byte aligned pointer to the memory,
1093
+** the number of bytes in the memory buffer, and the minimum allocation size.
1094
+** If the first pointer (the memory pointer) is NULL, then SQLite reverts
10761095
** to using its default memory allocator (the system malloc() implementation),
10771096
** undoing any prior invocation of [SQLITE_CONFIG_MALLOC]. If the
10781097
** memory pointer is not NULL and either [SQLITE_ENABLE_MEMSYS3] or
10791098
** [SQLITE_ENABLE_MEMSYS5] are defined, then the alternative memory
1080
-** allocator is engaged to handle all of SQLites memory allocation needs.</dd>
1099
+** allocator is engaged to handle all of SQLites memory allocation needs.
1100
+** The first pointer (the memory pointer) must be aligned to an 8-byte
1101
+** boundary or subsequent behavior of SQLite will be undefined.</dd>
10811102
**
10821103
** <dt>SQLITE_CONFIG_MUTEX</dt>
10831104
** <dd>This option takes a single argument which is a pointer to an
10841105
** instance of the [sqlite3_mutex_methods] structure. The argument specifies
10851106
** alternative low-level mutex routines to be used in place
@@ -1146,13 +1167,13 @@
11461167
** <dl>
11471168
** <dt>SQLITE_DBCONFIG_LOOKASIDE</dt>
11481169
** <dd>This option takes three additional arguments that determine the
11491170
** [lookaside memory allocator] configuration for the [database connection].
11501171
** The first argument (the third parameter to [sqlite3_db_config()] is a
1151
-** pointer to a memory buffer to use for lookaside memory. The first
1152
-** argument may be NULL in which case SQLite will allocate the lookaside
1153
-** buffer itself using [sqlite3_malloc()]. The second argument is the
1172
+** pointer to an 8-byte aligned memory buffer to use for lookaside memory.
1173
+** The first argument may be NULL in which case SQLite will allocate the
1174
+** lookaside buffer itself using [sqlite3_malloc()]. The second argument is the
11541175
** size of each lookaside buffer slot and the third argument is the number of
11551176
** slots. The size of the buffer in the first argument must be greater than
11561177
** or equal to the product of the second and third arguments.</dd>
11571178
**
11581179
** </dl>
11591180
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -28,11 +28,11 @@
28 ** The name of this file under configuration management is "sqlite.h.in".
29 ** The makefile makes some minor changes to this file (such as inserting
30 ** the version number) and changes its name to "sqlite3.h" as
31 ** part of the build process.
32 **
33 ** @(#) $Id: sqlite.h.in,v 1.455 2009/05/24 21:59:28 drh Exp $
34 */
35 #ifndef _SQLITE3_H_
36 #define _SQLITE3_H_
37 #include <stdarg.h> /* Needed for the definition of va_list */
38
@@ -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.14"
103 #define SQLITE_VERSION_NUMBER 3006014
104
105 /*
106 ** CAPI3REF: Run-Time Library Version Numbers {H10020} <S60100>
107 ** KEYWORDS: sqlite3_version
108 **
@@ -492,10 +492,16 @@
492 ** [sqlite3_file] object (or, more commonly, a subclass of the
493 ** [sqlite3_file] object) with a pointer to an instance of this object.
494 ** This object defines the methods used to perform various operations
495 ** against the open file represented by the [sqlite3_file] object.
496 **
 
 
 
 
 
 
497 ** The flags argument to xSync may be one of [SQLITE_SYNC_NORMAL] or
498 ** [SQLITE_SYNC_FULL]. The first choice is the normal fsync().
499 ** The second choice is a Mac OS X style fullsync. The [SQLITE_SYNC_DATAONLY]
500 ** flag may be ORed in to indicate that only the data of the file
501 ** and not its inode needs to be synced.
@@ -652,15 +658,15 @@
652 **
653 ** SQLite will guarantee that the zFilename parameter to xOpen
654 ** is either a NULL pointer or string obtained
655 ** from xFullPathname(). SQLite further guarantees that
656 ** the string will be valid and unchanged until xClose() is
657 ** called. Because of the previous sentense,
658 ** the [sqlite3_file] can safely store a pointer to the
659 ** filename if it needs to remember the filename for some reason.
660 ** If the zFilename parameter is xOpen is a NULL pointer then xOpen
661 ** must invite its own temporary name for the file. Whenever the
662 ** xFilename parameter is NULL it will also be the case that the
663 ** flags parameter will include [SQLITE_OPEN_DELETEONCLOSE].
664 **
665 ** The flags argument to xOpen() includes all bits set in
666 ** the flags argument to [sqlite3_open_v2()]. Or if [sqlite3_open()]
@@ -712,11 +718,16 @@
712 ** for exclusive access.
713 **
714 ** At least szOsFile bytes of memory are allocated by SQLite
715 ** to hold the [sqlite3_file] structure passed as the third
716 ** argument to xOpen. The xOpen method does not have to
717 ** allocate the structure; it should just fill it in.
 
 
 
 
 
718 **
719 ** The flags argument to xAccess() may be [SQLITE_ACCESS_EXISTS]
720 ** to test for the existence of a file, or [SQLITE_ACCESS_READWRITE] to
721 ** test whether a file is readable and writable, or [SQLITE_ACCESS_READ]
722 ** to test whether a file is at least readable. The file can be a
@@ -1034,16 +1045,18 @@
1034 ** </ul>
1035 ** </dd>
1036 **
1037 ** <dt>SQLITE_CONFIG_SCRATCH</dt>
1038 ** <dd>This option specifies a static memory buffer that SQLite can use for
1039 ** scratch memory. There are three arguments: A pointer to the memory, the
1040 ** size of each scratch buffer (sz), and the number of buffers (N). The sz
 
 
1041 ** argument must be a multiple of 16. The sz parameter should be a few bytes
1042 ** larger than the actual scratch space required due internal overhead.
1043 ** The first
1044 ** argument should point to an allocation of at least sz*N bytes of memory.
1045 ** SQLite will use no more than one scratch buffer at once per thread, so
1046 ** N should be set to the expected maximum number of threads. The sz
1047 ** parameter should be 6 times the size of the largest database page size.
1048 ** Scratch buffers are used as part of the btree balance operation. If
1049 ** The btree balancer needs additional memory beyond what is provided by
@@ -1053,33 +1066,41 @@
1053 ** <dt>SQLITE_CONFIG_PAGECACHE</dt>
1054 ** <dd>This option specifies a static memory buffer that SQLite can use for
1055 ** the database page cache with the default page cache implemenation.
1056 ** This configuration should not be used if an application-define page
1057 ** cache implementation is loaded using the SQLITE_CONFIG_PCACHE option.
1058 ** There are three arguments to this option: A pointer to the
1059 ** memory, the size of each page buffer (sz), and the number of pages (N).
1060 ** The sz argument must be a power of two between 512 and 32768. The first
 
 
 
 
1061 ** argument should point to an allocation of at least sz*N bytes of memory.
1062 ** SQLite will use the memory provided by the first argument to satisfy its
1063 ** memory needs for the first N pages that it adds to cache. If additional
1064 ** page cache memory is needed beyond what is provided by this option, then
1065 ** SQLite goes to [sqlite3_malloc()] for the additional storage space.
1066 ** The implementation might use one or more of the N buffers to hold
1067 ** memory accounting information. </dd>
 
 
1068 **
1069 ** <dt>SQLITE_CONFIG_HEAP</dt>
1070 ** <dd>This option specifies a static memory buffer that SQLite will use
1071 ** for all of its dynamic memory allocation needs beyond those provided
1072 ** for by [SQLITE_CONFIG_SCRATCH] and [SQLITE_CONFIG_PAGECACHE].
1073 ** There are three arguments: A pointer to the memory, the number of
1074 ** bytes in the memory buffer, and the minimum allocation size. If
1075 ** the first pointer (the memory pointer) is NULL, then SQLite reverts
1076 ** to using its default memory allocator (the system malloc() implementation),
1077 ** undoing any prior invocation of [SQLITE_CONFIG_MALLOC]. If the
1078 ** memory pointer is not NULL and either [SQLITE_ENABLE_MEMSYS3] or
1079 ** [SQLITE_ENABLE_MEMSYS5] are defined, then the alternative memory
1080 ** allocator is engaged to handle all of SQLites memory allocation needs.</dd>
 
 
1081 **
1082 ** <dt>SQLITE_CONFIG_MUTEX</dt>
1083 ** <dd>This option takes a single argument which is a pointer to an
1084 ** instance of the [sqlite3_mutex_methods] structure. The argument specifies
1085 ** alternative low-level mutex routines to be used in place
@@ -1146,13 +1167,13 @@
1146 ** <dl>
1147 ** <dt>SQLITE_DBCONFIG_LOOKASIDE</dt>
1148 ** <dd>This option takes three additional arguments that determine the
1149 ** [lookaside memory allocator] configuration for the [database connection].
1150 ** The first argument (the third parameter to [sqlite3_db_config()] is a
1151 ** pointer to a memory buffer to use for lookaside memory. The first
1152 ** argument may be NULL in which case SQLite will allocate the lookaside
1153 ** buffer itself using [sqlite3_malloc()]. The second argument is the
1154 ** size of each lookaside buffer slot and the third argument is the number of
1155 ** slots. The size of the buffer in the first argument must be greater than
1156 ** or equal to the product of the second and third arguments.</dd>
1157 **
1158 ** </dl>
1159
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -28,11 +28,11 @@
28 ** The name of this file under configuration management is "sqlite.h.in".
29 ** The makefile makes some minor changes to this file (such as inserting
30 ** the version number) and changes its name to "sqlite3.h" as
31 ** part of the build process.
32 **
33 ** @(#) $Id: sqlite.h.in,v 1.458 2009/06/19 22:50:31 drh Exp $
34 */
35 #ifndef _SQLITE3_H_
36 #define _SQLITE3_H_
37 #include <stdarg.h> /* Needed for the definition of va_list */
38
@@ -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 **
@@ -492,10 +492,16 @@
492 ** [sqlite3_file] object (or, more commonly, a subclass of the
493 ** [sqlite3_file] object) with a pointer to an instance of this object.
494 ** This object defines the methods used to perform various operations
495 ** against the open file represented by the [sqlite3_file] object.
496 **
497 ** If the xOpen method sets the sqlite3_file.pMethods element
498 ** to a non-NULL pointer, then the sqlite3_io_methods.xClose method
499 ** may be invoked even if the xOpen reported that it failed. The
500 ** only way to prevent a call to xClose following a failed xOpen
501 ** is for the xOpen to set the sqlite3_file.pMethods element to NULL.
502 **
503 ** The flags argument to xSync may be one of [SQLITE_SYNC_NORMAL] or
504 ** [SQLITE_SYNC_FULL]. The first choice is the normal fsync().
505 ** The second choice is a Mac OS X style fullsync. The [SQLITE_SYNC_DATAONLY]
506 ** flag may be ORed in to indicate that only the data of the file
507 ** and not its inode needs to be synced.
@@ -652,15 +658,15 @@
658 **
659 ** SQLite will guarantee that the zFilename parameter to xOpen
660 ** is either a NULL pointer or string obtained
661 ** from xFullPathname(). SQLite further guarantees that
662 ** the string will be valid and unchanged until xClose() is
663 ** called. Because of the previous sentence,
664 ** the [sqlite3_file] can safely store a pointer to the
665 ** filename if it needs to remember the filename for some reason.
666 ** If the zFilename parameter is xOpen is a NULL pointer then xOpen
667 ** must invent its own temporary name for the file. Whenever the
668 ** xFilename parameter is NULL it will also be the case that the
669 ** flags parameter will include [SQLITE_OPEN_DELETEONCLOSE].
670 **
671 ** The flags argument to xOpen() includes all bits set in
672 ** the flags argument to [sqlite3_open_v2()]. Or if [sqlite3_open()]
@@ -712,11 +718,16 @@
718 ** for exclusive access.
719 **
720 ** At least szOsFile bytes of memory are allocated by SQLite
721 ** to hold the [sqlite3_file] structure passed as the third
722 ** argument to xOpen. The xOpen method does not have to
723 ** allocate the structure; it should just fill it in. Note that
724 ** the xOpen method must set the sqlite3_file.pMethods to either
725 ** a valid [sqlite3_io_methods] object or to NULL. xOpen must do
726 ** this even if the open fails. SQLite expects that the sqlite3_file.pMethods
727 ** element will be valid after xOpen returns regardless of the success
728 ** or failure of the xOpen call.
729 **
730 ** The flags argument to xAccess() may be [SQLITE_ACCESS_EXISTS]
731 ** to test for the existence of a file, or [SQLITE_ACCESS_READWRITE] to
732 ** test whether a file is readable and writable, or [SQLITE_ACCESS_READ]
733 ** to test whether a file is at least readable. The file can be a
@@ -1034,16 +1045,18 @@
1045 ** </ul>
1046 ** </dd>
1047 **
1048 ** <dt>SQLITE_CONFIG_SCRATCH</dt>
1049 ** <dd>This option specifies a static memory buffer that SQLite can use for
1050 ** scratch memory. There are three arguments: A pointer an 8-byte
1051 ** aligned memory buffer from which the scrach allocations will be
1052 ** drawn, the size of each scratch allocation (sz),
1053 ** and the maximum number of scratch allocations (N). The sz
1054 ** argument must be a multiple of 16. The sz parameter should be a few bytes
1055 ** larger than the actual scratch space required due to internal overhead.
1056 ** The first argument should pointer to an 8-byte aligned buffer
1057 ** of at least sz*N bytes of memory.
1058 ** SQLite will use no more than one scratch buffer at once per thread, so
1059 ** N should be set to the expected maximum number of threads. The sz
1060 ** parameter should be 6 times the size of the largest database page size.
1061 ** Scratch buffers are used as part of the btree balance operation. If
1062 ** The btree balancer needs additional memory beyond what is provided by
@@ -1053,33 +1066,41 @@
1066 ** <dt>SQLITE_CONFIG_PAGECACHE</dt>
1067 ** <dd>This option specifies a static memory buffer that SQLite can use for
1068 ** the database page cache with the default page cache implemenation.
1069 ** This configuration should not be used if an application-define page
1070 ** cache implementation is loaded using the SQLITE_CONFIG_PCACHE option.
1071 ** There are three arguments to this option: A pointer to 8-byte aligned
1072 ** memory, the size of each page buffer (sz), and the number of pages (N).
1073 ** The sz argument should be the size of the largest database page
1074 ** (a power of two between 512 and 32768) plus a little extra for each
1075 ** page header. The page header size is 20 to 40 bytes depending on
1076 ** the host architecture. It is harmless, apart from the wasted memory,
1077 ** to make sz a little too large. The first
1078 ** argument should point to an allocation of at least sz*N bytes of memory.
1079 ** SQLite will use the memory provided by the first argument to satisfy its
1080 ** memory needs for the first N pages that it adds to cache. If additional
1081 ** page cache memory is needed beyond what is provided by this option, then
1082 ** SQLite goes to [sqlite3_malloc()] for the additional storage space.
1083 ** The implementation might use one or more of the N buffers to hold
1084 ** memory accounting information. The pointer in the first argument must
1085 ** be aligned to an 8-byte boundary or subsequent behavior of SQLite
1086 ** will be undefined.</dd>
1087 **
1088 ** <dt>SQLITE_CONFIG_HEAP</dt>
1089 ** <dd>This option specifies a static memory buffer that SQLite will use
1090 ** for all of its dynamic memory allocation needs beyond those provided
1091 ** for by [SQLITE_CONFIG_SCRATCH] and [SQLITE_CONFIG_PAGECACHE].
1092 ** There are three arguments: An 8-byte aligned pointer to the memory,
1093 ** the number of bytes in the memory buffer, and the minimum allocation size.
1094 ** If the first pointer (the memory pointer) is NULL, then SQLite reverts
1095 ** to using its default memory allocator (the system malloc() implementation),
1096 ** undoing any prior invocation of [SQLITE_CONFIG_MALLOC]. If the
1097 ** memory pointer is not NULL and either [SQLITE_ENABLE_MEMSYS3] or
1098 ** [SQLITE_ENABLE_MEMSYS5] are defined, then the alternative memory
1099 ** allocator is engaged to handle all of SQLites memory allocation needs.
1100 ** The first pointer (the memory pointer) must be aligned to an 8-byte
1101 ** boundary or subsequent behavior of SQLite will be undefined.</dd>
1102 **
1103 ** <dt>SQLITE_CONFIG_MUTEX</dt>
1104 ** <dd>This option takes a single argument which is a pointer to an
1105 ** instance of the [sqlite3_mutex_methods] structure. The argument specifies
1106 ** alternative low-level mutex routines to be used in place
@@ -1146,13 +1167,13 @@
1167 ** <dl>
1168 ** <dt>SQLITE_DBCONFIG_LOOKASIDE</dt>
1169 ** <dd>This option takes three additional arguments that determine the
1170 ** [lookaside memory allocator] configuration for the [database connection].
1171 ** The first argument (the third parameter to [sqlite3_db_config()] is a
1172 ** pointer to an 8-byte aligned memory buffer to use for lookaside memory.
1173 ** The first argument may be NULL in which case SQLite will allocate the
1174 ** lookaside buffer itself using [sqlite3_malloc()]. The second argument is the
1175 ** size of each lookaside buffer slot and the third argument is the number of
1176 ** slots. The size of the buffer in the first argument must be greater than
1177 ** or equal to the product of the second and third arguments.</dd>
1178 **
1179 ** </dl>
1180

Keyboard Shortcuts

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