Fossil SCM

Update the build-in SQLite to the first 3.24.0 beta.

drh 2018-05-30 01:28 trunk
Commit bbdfec3f3150c66c3ebb31269ee5196a14c0f6fb1befb3667f8754388f0d33d3
3 files changed +91 -40 +499 -179 +21 -7
+91 -40
--- src/shell.c
+++ src/shell.c
@@ -7646,11 +7646,13 @@
76467646
}
76477647
break;
76487648
}
76497649
}
76507650
7651
- pStmt->zEQP = idxAppendText(&rc, pStmt->zEQP, "%s\n", zDetail);
7651
+ if( zDetail[0]!='-' ){
7652
+ pStmt->zEQP = idxAppendText(&rc, pStmt->zEQP, "%s\n", zDetail);
7653
+ }
76527654
}
76537655
76547656
for(pEntry=hIdx.pFirst; pEntry; pEntry=pEntry->pNext){
76557657
pStmt->zIdx = idxAppendText(&rc, pStmt->zIdx, "%s;\n", pEntry->zKey);
76567658
}
@@ -11045,12 +11047,15 @@
1104511047
FILE *f = fopen(zName, "rb");
1104611048
size_t n;
1104711049
int rc = SHELL_OPEN_UNSPEC;
1104811050
char zBuf[100];
1104911051
if( f==0 ){
11050
- if( dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ) return SHELL_OPEN_ZIPFILE;
11051
- return SHELL_OPEN_NORMAL;
11052
+ if( dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
11053
+ return SHELL_OPEN_ZIPFILE;
11054
+ }else{
11055
+ return SHELL_OPEN_NORMAL;
11056
+ }
1105211057
}
1105311058
fseek(f, -25, SEEK_END);
1105411059
n = fread(zBuf, 25, 1, f);
1105511060
if( n==1 && memcmp(zBuf, "Start-Of-SQLite3-", 17)==0 ){
1105611061
rc = SHELL_OPEN_APPENDVFS;
@@ -11059,28 +11064,42 @@
1105911064
n = fread(zBuf, 22, 1, f);
1106011065
if( n==1 && zBuf[0]==0x50 && zBuf[1]==0x4b && zBuf[2]==0x05
1106111066
&& zBuf[3]==0x06 ){
1106211067
rc = SHELL_OPEN_ZIPFILE;
1106311068
}else if( n==0 && dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
11064
- return SHELL_OPEN_ZIPFILE;
11069
+ rc = SHELL_OPEN_ZIPFILE;
1106511070
}
1106611071
}
1106711072
fclose(f);
1106811073
return rc;
1106911074
}
1107011075
11076
+/* Flags for open_db().
11077
+**
11078
+** The default behavior of open_db() is to exit(1) if the database fails to
11079
+** open. The OPEN_DB_KEEPALIVE flag changes that so that it prints an error
11080
+** but still returns without calling exit.
11081
+**
11082
+** The OPEN_DB_ZIPFILE flag causes open_db() to prefer to open files as a
11083
+** ZIP archive if the file does not exist or is empty and its name matches
11084
+** the *.zip pattern.
11085
+*/
11086
+#define OPEN_DB_KEEPALIVE 0x001 /* Return after error if true */
11087
+#define OPEN_DB_ZIPFILE 0x002 /* Open as ZIP if name matches *.zip */
11088
+
1107111089
/*
1107211090
** Make sure the database is open. If it is not, then open it. If
1107311091
** the database fails to open, print an error message and exit.
1107411092
*/
11075
-static void open_db(ShellState *p, int keepAlive){
11093
+static void open_db(ShellState *p, int openFlags){
1107611094
if( p->db==0 ){
1107711095
if( p->openMode==SHELL_OPEN_UNSPEC ){
1107811096
if( p->zDbFilename==0 || p->zDbFilename[0]==0 ){
1107911097
p->openMode = SHELL_OPEN_NORMAL;
11080
- }else if( access(p->zDbFilename,0)==0 ){
11081
- p->openMode = (u8)deduceDatabaseType(p->zDbFilename, 0);
11098
+ }else{
11099
+ p->openMode = (u8)deduceDatabaseType(p->zDbFilename,
11100
+ (openFlags & OPEN_DB_ZIPFILE)!=0);
1108211101
}
1108311102
}
1108411103
switch( p->openMode ){
1108511104
case SHELL_OPEN_APPENDVFS: {
1108611105
sqlite3_open_v2(p->zDbFilename, &p->db,
@@ -11103,11 +11122,11 @@
1110311122
}
1110411123
globalDb = p->db;
1110511124
if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){
1110611125
utf8_printf(stderr,"Error: unable to open database \"%s\": %s\n",
1110711126
p->zDbFilename, sqlite3_errmsg(p->db));
11108
- if( keepAlive ) return;
11127
+ if( openFlags & OPEN_DB_KEEPALIVE ) return;
1110911128
exit(1);
1111011129
}
1111111130
#ifndef SQLITE_OMIT_LOAD_EXTENSION
1111211131
sqlite3_enable_load_extension(p->db, 1);
1111311132
#endif
@@ -11136,10 +11155,21 @@
1113611155
sqlite3_exec(p->db, zSql, 0, 0, 0);
1113711156
sqlite3_free(zSql);
1113811157
}
1113911158
}
1114011159
}
11160
+
11161
+/*
11162
+** Attempt to close the databaes connection. Report errors.
11163
+*/
11164
+void close_db(sqlite3 *db){
11165
+ int rc = sqlite3_close(db);
11166
+ if( rc ){
11167
+ utf8_printf(stderr, "Error: sqlite3_close() returns %d: %s\n",
11168
+ rc, sqlite3_errmsg(db));
11169
+ }
11170
+}
1114111171
1114211172
#if HAVE_READLINE || HAVE_EDITLINE
1114311173
/*
1114411174
** Readline completion callbacks
1114511175
*/
@@ -11717,11 +11747,11 @@
1171711747
tryToCloneSchema(p, newDb, "type='table'", tryToCloneData);
1171811748
tryToCloneSchema(p, newDb, "type!='table'", 0);
1171911749
sqlite3_exec(newDb, "COMMIT;", 0, 0, 0);
1172011750
sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
1172111751
}
11722
- sqlite3_close(newDb);
11752
+ close_db(newDb);
1172311753
}
1172411754
1172511755
/*
1172611756
** Change the output file back to stdout.
1172711757
**
@@ -12372,10 +12402,11 @@
1237212402
u8 eCmd; /* An AR_CMD_* value */
1237312403
u8 bVerbose; /* True if --verbose */
1237412404
u8 bZip; /* True if the archive is a ZIP */
1237512405
u8 bDryRun; /* True if --dry-run */
1237612406
u8 bAppend; /* True if --append */
12407
+ u8 fromCmdLine; /* Run from -A instead of .archive */
1237712408
int nArg; /* Number of command arguments */
1237812409
char *zSrcTable; /* "sqlar", "zipfile($file)" or "zip" */
1237912410
const char *zFile; /* --file argument, or NULL */
1238012411
const char *zDir; /* --directory argument, or NULL */
1238112412
char **azArg; /* Array of command arguments */
@@ -12418,17 +12449,22 @@
1241812449
1241912450
/*
1242012451
** Print an error message for the .ar command to stderr and return
1242112452
** SQLITE_ERROR.
1242212453
*/
12423
-static int arErrorMsg(const char *zFmt, ...){
12454
+static int arErrorMsg(ArCommand *pAr, const char *zFmt, ...){
1242412455
va_list ap;
1242512456
char *z;
1242612457
va_start(ap, zFmt);
1242712458
z = sqlite3_vmprintf(zFmt, ap);
1242812459
va_end(ap);
12429
- raw_printf(stderr, "Error: %s (try \".ar --help\")\n", z);
12460
+ utf8_printf(stderr, "Error: %s\n", z);
12461
+ if( pAr->fromCmdLine ){
12462
+ utf8_printf(stderr, "Use \"-A\" for more help\n");
12463
+ }else{
12464
+ utf8_printf(stderr, "Use \".archive --help\" for more help\n");
12465
+ }
1243012466
sqlite3_free(z);
1243112467
return SQLITE_ERROR;
1243212468
}
1243312469
1243412470
/*
@@ -12455,11 +12491,11 @@
1245512491
case AR_CMD_EXTRACT:
1245612492
case AR_CMD_LIST:
1245712493
case AR_CMD_UPDATE:
1245812494
case AR_CMD_HELP:
1245912495
if( pAr->eCmd ){
12460
- return arErrorMsg("multiple command options");
12496
+ return arErrorMsg(pAr, "multiple command options");
1246112497
}
1246212498
pAr->eCmd = eSwitch;
1246312499
break;
1246412500
1246512501
case AR_SWITCH_DRYRUN:
@@ -12515,12 +12551,10 @@
1251512551
1251612552
if( nArg<=1 ){
1251712553
return arUsage(stderr);
1251812554
}else{
1251912555
char *z = azArg[1];
12520
- memset(pAr, 0, sizeof(ArCommand));
12521
-
1252212556
if( z[0]!='-' ){
1252312557
/* Traditional style [tar] invocation */
1252412558
int i;
1252512559
int iArg = 2;
1252612560
for(i=0; z[i]; i++){
@@ -12528,15 +12562,15 @@
1252812562
struct ArSwitch *pOpt;
1252912563
for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
1253012564
if( z[i]==pOpt->cShort ) break;
1253112565
}
1253212566
if( pOpt==pEnd ){
12533
- return arErrorMsg("unrecognized option: %c", z[i]);
12567
+ return arErrorMsg(pAr, "unrecognized option: %c", z[i]);
1253412568
}
1253512569
if( pOpt->bArg ){
1253612570
if( iArg>=nArg ){
12537
- return arErrorMsg("option requires an argument: %c",z[i]);
12571
+ return arErrorMsg(pAr, "option requires an argument: %c",z[i]);
1253812572
}
1253912573
zArg = azArg[iArg++];
1254012574
}
1254112575
if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
1254212576
}
@@ -12566,19 +12600,19 @@
1256612600
struct ArSwitch *pOpt;
1256712601
for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
1256812602
if( z[i]==pOpt->cShort ) break;
1256912603
}
1257012604
if( pOpt==pEnd ){
12571
- return arErrorMsg("unrecognized option: %c\n", z[i]);
12605
+ return arErrorMsg(pAr, "unrecognized option: %c", z[i]);
1257212606
}
1257312607
if( pOpt->bArg ){
1257412608
if( i<(n-1) ){
1257512609
zArg = &z[i+1];
1257612610
i = n;
1257712611
}else{
1257812612
if( iArg>=(nArg-1) ){
12579
- return arErrorMsg("option requires an argument: %c\n",z[i]);
12613
+ return arErrorMsg(pAr, "option requires an argument: %c",z[i]);
1258012614
}
1258112615
zArg = azArg[++iArg];
1258212616
}
1258312617
}
1258412618
if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
@@ -12596,23 +12630,23 @@
1259612630
struct ArSwitch *pOpt; /* Iterator */
1259712631
for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
1259812632
const char *zLong = pOpt->zLong;
1259912633
if( (n-2)<=strlen30(zLong) && 0==memcmp(&z[2], zLong, n-2) ){
1260012634
if( pMatch ){
12601
- return arErrorMsg("ambiguous option: %s",z);
12635
+ return arErrorMsg(pAr, "ambiguous option: %s",z);
1260212636
}else{
1260312637
pMatch = pOpt;
1260412638
}
1260512639
}
1260612640
}
1260712641
1260812642
if( pMatch==0 ){
12609
- return arErrorMsg("unrecognized option: %s", z);
12643
+ return arErrorMsg(pAr, "unrecognized option: %s", z);
1261012644
}
1261112645
if( pMatch->bArg ){
1261212646
if( iArg>=(nArg-1) ){
12613
- return arErrorMsg("option requires an argument: %s", z);
12647
+ return arErrorMsg(pAr, "option requires an argument: %s", z);
1261412648
}
1261512649
zArg = azArg[++iArg];
1261612650
}
1261712651
if( arProcessSwitch(pAr, pMatch->eSwitch, zArg) ) return SQLITE_ERROR;
1261812652
}
@@ -12737,10 +12771,11 @@
1273712771
utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
1273812772
}
1273912773
}
1274012774
}
1274112775
shellFinalize(&rc, pSql);
12776
+ sqlite3_free(zWhere);
1274212777
return rc;
1274312778
}
1274412779
1274512780
1274612781
/*
@@ -12939,16 +12974,18 @@
1293912974
/*
1294012975
** Implementation of ".ar" dot command.
1294112976
*/
1294212977
static int arDotCommand(
1294312978
ShellState *pState, /* Current shell tool state */
12979
+ int fromCmdLine, /* True if -A command-line option, not .ar cmd */
1294412980
char **azArg, /* Array of arguments passed to dot command */
1294512981
int nArg /* Number of entries in azArg[] */
1294612982
){
1294712983
ArCommand cmd;
1294812984
int rc;
1294912985
memset(&cmd, 0, sizeof(cmd));
12986
+ cmd.fromCmdLine = fromCmdLine;
1295012987
rc = arParseCommand(azArg, nArg, &cmd);
1295112988
if( rc==SQLITE_OK ){
1295212989
int eDbType = SHELL_OPEN_UNSPEC;
1295312990
cmd.p = pState;
1295412991
cmd.db = pState->db;
@@ -12991,11 +13028,11 @@
1299113028
sqlite3_sqlar_init(cmd.db, 0, 0);
1299213029
sqlite3_create_function(cmd.db, "shell_putsnl", 1, SQLITE_UTF8, cmd.p,
1299313030
shellPutsFunc, 0, 0);
1299413031
1299513032
}
12996
- if( cmd.zSrcTable==0 && cmd.bZip==0 ){
13033
+ if( cmd.zSrcTable==0 && cmd.bZip==0 && cmd.eCmd!=AR_CMD_HELP ){
1299713034
if( cmd.eCmd!=AR_CMD_CREATE
1299813035
&& sqlite3_table_column_metadata(cmd.db,0,"sqlar","name",0,0,0,0,0)
1299913036
){
1300013037
utf8_printf(stderr, "database does not contain an 'sqlar' table\n");
1300113038
rc = SQLITE_ERROR;
@@ -13027,11 +13064,11 @@
1302713064
break;
1302813065
}
1302913066
}
1303013067
end_ar_command:
1303113068
if( cmd.db!=pState->db ){
13032
- sqlite3_close(cmd.db);
13069
+ close_db(cmd.db);
1303313070
}
1303413071
sqlite3_free(cmd.zSrcTable);
1303513072
1303613073
return rc;
1303713074
}
@@ -13107,11 +13144,11 @@
1310713144
#endif
1310813145
1310913146
#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
1311013147
if( c=='a' && strncmp(azArg[0], "archive", n)==0 ){
1311113148
open_db(p, 0);
13112
- rc = arDotCommand(p, azArg, nArg);
13149
+ rc = arDotCommand(p, 0, azArg, nArg);
1311313150
}else
1311413151
#endif
1311513152
1311613153
if( (c=='b' && n>=3 && strncmp(azArg[0], "backup", n)==0)
1311713154
|| (c=='s' && n>=3 && strncmp(azArg[0], "save", n)==0)
@@ -13150,18 +13187,18 @@
1315013187
if( zDb==0 ) zDb = "main";
1315113188
rc = sqlite3_open_v2(zDestFile, &pDest,
1315213189
SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, zVfs);
1315313190
if( rc!=SQLITE_OK ){
1315413191
utf8_printf(stderr, "Error: cannot open \"%s\"\n", zDestFile);
13155
- sqlite3_close(pDest);
13192
+ close_db(pDest);
1315613193
return 1;
1315713194
}
1315813195
open_db(p, 0);
1315913196
pBackup = sqlite3_backup_init(pDest, "main", p->db, zDb);
1316013197
if( pBackup==0 ){
1316113198
utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
13162
- sqlite3_close(pDest);
13199
+ close_db(pDest);
1316313200
return 1;
1316413201
}
1316513202
while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){}
1316613203
sqlite3_backup_finish(pBackup);
1316713204
if( rc==SQLITE_DONE ){
@@ -13168,11 +13205,11 @@
1316813205
rc = 0;
1316913206
}else{
1317013207
utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
1317113208
rc = 1;
1317213209
}
13173
- sqlite3_close(pDest);
13210
+ close_db(pDest);
1317413211
}else
1317513212
1317613213
if( c=='b' && n>=3 && strncmp(azArg[0], "bail", n)==0 ){
1317713214
if( nArg==2 ){
1317813215
bail_on_error = booleanValue(azArg[1]);
@@ -13984,11 +14021,11 @@
1398414021
char *zNewFilename; /* Name of the database file to open */
1398514022
int iName = 1; /* Index in azArg[] of the filename */
1398614023
int newFlag = 0; /* True to delete file before opening */
1398714024
/* Close the existing database */
1398814025
session_close_all(p);
13989
- sqlite3_close(p->db);
14026
+ close_db(p->db);
1399014027
p->db = 0;
1399114028
p->zDbFilename = 0;
1399214029
sqlite3_free(p->zFreeOnClose);
1399314030
p->zFreeOnClose = 0;
1399414031
p->openMode = SHELL_OPEN_UNSPEC;
@@ -14014,11 +14051,11 @@
1401414051
/* If a filename is specified, try to open it first */
1401514052
zNewFilename = nArg>iName ? sqlite3_mprintf("%s", azArg[iName]) : 0;
1401614053
if( zNewFilename ){
1401714054
if( newFlag ) shellDeleteFile(zNewFilename);
1401814055
p->zDbFilename = zNewFilename;
14019
- open_db(p, 1);
14056
+ open_db(p, OPEN_DB_KEEPALIVE);
1402014057
if( p->db==0 ){
1402114058
utf8_printf(stderr, "Error: cannot open '%s'\n", zNewFilename);
1402214059
sqlite3_free(zNewFilename);
1402314060
}else{
1402414061
p->zFreeOnClose = zNewFilename;
@@ -14164,18 +14201,18 @@
1416414201
goto meta_command_exit;
1416514202
}
1416614203
rc = sqlite3_open(zSrcFile, &pSrc);
1416714204
if( rc!=SQLITE_OK ){
1416814205
utf8_printf(stderr, "Error: cannot open \"%s\"\n", zSrcFile);
14169
- sqlite3_close(pSrc);
14206
+ close_db(pSrc);
1417014207
return 1;
1417114208
}
1417214209
open_db(p, 0);
1417314210
pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main");
1417414211
if( pBackup==0 ){
1417514212
utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
14176
- sqlite3_close(pSrc);
14213
+ close_db(pSrc);
1417714214
return 1;
1417814215
}
1417914216
while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK
1418014217
|| rc==SQLITE_BUSY ){
1418114218
if( rc==SQLITE_BUSY ){
@@ -14191,11 +14228,11 @@
1419114228
rc = 1;
1419214229
}else{
1419314230
utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
1419414231
rc = 1;
1419514232
}
14196
- sqlite3_close(pSrc);
14233
+ close_db(pSrc);
1419714234
}else
1419814235
1419914236
if( c=='s' && strncmp(azArg[0], "scanstats", n)==0 ){
1420014237
if( nArg==2 ){
1420114238
p->scanstatsOn = (u8)booleanValue(azArg[1]);
@@ -14875,18 +14912,22 @@
1487514912
int ii;
1487614913
ShellText s;
1487714914
initText(&s);
1487814915
open_db(p, 0);
1487914916
rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
14880
- if( rc ) return shellDatabaseError(p->db);
14917
+ if( rc ){
14918
+ sqlite3_finalize(pStmt);
14919
+ return shellDatabaseError(p->db);
14920
+ }
1488114921
1488214922
if( nArg>2 && c=='i' ){
1488314923
/* It is an historical accident that the .indexes command shows an error
1488414924
** when called with the wrong number of arguments whereas the .tables
1488514925
** command does not. */
1488614926
raw_printf(stderr, "Usage: .indexes ?LIKE-PATTERN?\n");
1488714927
rc = 1;
14928
+ sqlite3_finalize(pStmt);
1488814929
goto meta_command_exit;
1488914930
}
1489014931
for(ii=0; sqlite3_step(pStmt)==SQLITE_ROW; ii++){
1489114932
const char *zDbName = (const char*)sqlite3_column_text(pStmt, 1);
1489214933
if( zDbName==0 ) continue;
@@ -15783,10 +15824,14 @@
1578315824
int warnInmemoryDb = 0;
1578415825
int readStdin = 1;
1578515826
int nCmd = 0;
1578615827
char **azCmd = 0;
1578715828
const char *zVfs = 0; /* Value of -vfs command-line option */
15829
+#if !SQLITE_SHELL_IS_UTF8
15830
+ char **argvToFree = 0;
15831
+ int argcToFree = 0;
15832
+#endif
1578815833
1578915834
setBinaryMode(stdin, 0);
1579015835
setvbuf(stderr, 0, _IONBF, 0); /* Make sure stderr is unbuffered */
1579115836
stdin_is_interactive = isatty(0);
1579215837
stdout_is_console = isatty(1);
@@ -15806,20 +15851,23 @@
1580615851
** subsequent sqlite3_config() calls will work. So copy all results into
1580715852
** memory that does not come from the SQLite memory allocator.
1580815853
*/
1580915854
#if !SQLITE_SHELL_IS_UTF8
1581015855
sqlite3_initialize();
15811
- argv = malloc(sizeof(argv[0])*argc);
15856
+ argvToFree = malloc(sizeof(argv[0])*argc*2);
15857
+ argcToFree = argc;
15858
+ argv = argvToFree + argc;
1581215859
if( argv==0 ) shell_out_of_memory();
1581315860
for(i=0; i<argc; i++){
1581415861
char *z = sqlite3_win32_unicode_to_utf8(wargv[i]);
1581515862
int n;
1581615863
if( z==0 ) shell_out_of_memory();
1581715864
n = (int)strlen(z);
1581815865
argv[i] = malloc( n+1 );
1581915866
if( argv[i]==0 ) shell_out_of_memory();
1582015867
memcpy(argv[i], z, n+1);
15868
+ argvToFree[i] = argv[i];
1582115869
sqlite3_free(z);
1582215870
}
1582315871
sqlite3_shutdown();
1582415872
#endif
1582515873
@@ -16137,16 +16185,16 @@
1613716185
if( nCmd>0 ){
1613816186
utf8_printf(stderr, "Error: cannot mix regular SQL or dot-commands"
1613916187
" with \"%s\"\n", z);
1614016188
return 1;
1614116189
}
16142
- open_db(&data, 0);
16190
+ open_db(&data, OPEN_DB_ZIPFILE);
1614316191
if( z[2] ){
1614416192
argv[i] = &z[2];
16145
- arDotCommand(&data, argv+(i-1), argc-(i-1));
16193
+ arDotCommand(&data, 1, argv+(i-1), argc-(i-1));
1614616194
}else{
16147
- arDotCommand(&data, argv+i, argc-i);
16195
+ arDotCommand(&data, 1, argv+i, argc-i);
1614816196
}
1614916197
readStdin = 0;
1615016198
break;
1615116199
#endif
1615216200
}else{
@@ -16221,18 +16269,21 @@
1622116269
}
1622216270
}
1622316271
set_table_name(&data, 0);
1622416272
if( data.db ){
1622516273
session_close_all(&data);
16226
- sqlite3_close(data.db);
16274
+ close_db(data.db);
1622716275
}
1622816276
sqlite3_free(data.zFreeOnClose);
1622916277
find_home_dir(1);
1623016278
output_reset(&data);
1623116279
data.doXdgOpen = 0;
1623216280
clearTempFile(&data);
1623316281
#if !SQLITE_SHELL_IS_UTF8
16234
- for(i=0; i<argc; i++) free(argv[i]);
16235
- free(argv);
16282
+ for(i=0; i<argcToFree; i++) free(argvToFree[i]);
16283
+ free(argvToFree);
1623616284
#endif
16285
+ /* Clear the global data structure so that valgrind will detect memory
16286
+ ** leaks */
16287
+ memset(&data, 0, sizeof(data));
1623716288
return rc;
1623816289
}
1623916290
--- src/shell.c
+++ src/shell.c
@@ -7646,11 +7646,13 @@
7646 }
7647 break;
7648 }
7649 }
7650
7651 pStmt->zEQP = idxAppendText(&rc, pStmt->zEQP, "%s\n", zDetail);
 
 
7652 }
7653
7654 for(pEntry=hIdx.pFirst; pEntry; pEntry=pEntry->pNext){
7655 pStmt->zIdx = idxAppendText(&rc, pStmt->zIdx, "%s;\n", pEntry->zKey);
7656 }
@@ -11045,12 +11047,15 @@
11045 FILE *f = fopen(zName, "rb");
11046 size_t n;
11047 int rc = SHELL_OPEN_UNSPEC;
11048 char zBuf[100];
11049 if( f==0 ){
11050 if( dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ) return SHELL_OPEN_ZIPFILE;
11051 return SHELL_OPEN_NORMAL;
 
 
 
11052 }
11053 fseek(f, -25, SEEK_END);
11054 n = fread(zBuf, 25, 1, f);
11055 if( n==1 && memcmp(zBuf, "Start-Of-SQLite3-", 17)==0 ){
11056 rc = SHELL_OPEN_APPENDVFS;
@@ -11059,28 +11064,42 @@
11059 n = fread(zBuf, 22, 1, f);
11060 if( n==1 && zBuf[0]==0x50 && zBuf[1]==0x4b && zBuf[2]==0x05
11061 && zBuf[3]==0x06 ){
11062 rc = SHELL_OPEN_ZIPFILE;
11063 }else if( n==0 && dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
11064 return SHELL_OPEN_ZIPFILE;
11065 }
11066 }
11067 fclose(f);
11068 return rc;
11069 }
11070
 
 
 
 
 
 
 
 
 
 
 
 
 
11071 /*
11072 ** Make sure the database is open. If it is not, then open it. If
11073 ** the database fails to open, print an error message and exit.
11074 */
11075 static void open_db(ShellState *p, int keepAlive){
11076 if( p->db==0 ){
11077 if( p->openMode==SHELL_OPEN_UNSPEC ){
11078 if( p->zDbFilename==0 || p->zDbFilename[0]==0 ){
11079 p->openMode = SHELL_OPEN_NORMAL;
11080 }else if( access(p->zDbFilename,0)==0 ){
11081 p->openMode = (u8)deduceDatabaseType(p->zDbFilename, 0);
 
11082 }
11083 }
11084 switch( p->openMode ){
11085 case SHELL_OPEN_APPENDVFS: {
11086 sqlite3_open_v2(p->zDbFilename, &p->db,
@@ -11103,11 +11122,11 @@
11103 }
11104 globalDb = p->db;
11105 if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){
11106 utf8_printf(stderr,"Error: unable to open database \"%s\": %s\n",
11107 p->zDbFilename, sqlite3_errmsg(p->db));
11108 if( keepAlive ) return;
11109 exit(1);
11110 }
11111 #ifndef SQLITE_OMIT_LOAD_EXTENSION
11112 sqlite3_enable_load_extension(p->db, 1);
11113 #endif
@@ -11136,10 +11155,21 @@
11136 sqlite3_exec(p->db, zSql, 0, 0, 0);
11137 sqlite3_free(zSql);
11138 }
11139 }
11140 }
 
 
 
 
 
 
 
 
 
 
 
11141
11142 #if HAVE_READLINE || HAVE_EDITLINE
11143 /*
11144 ** Readline completion callbacks
11145 */
@@ -11717,11 +11747,11 @@
11717 tryToCloneSchema(p, newDb, "type='table'", tryToCloneData);
11718 tryToCloneSchema(p, newDb, "type!='table'", 0);
11719 sqlite3_exec(newDb, "COMMIT;", 0, 0, 0);
11720 sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
11721 }
11722 sqlite3_close(newDb);
11723 }
11724
11725 /*
11726 ** Change the output file back to stdout.
11727 **
@@ -12372,10 +12402,11 @@
12372 u8 eCmd; /* An AR_CMD_* value */
12373 u8 bVerbose; /* True if --verbose */
12374 u8 bZip; /* True if the archive is a ZIP */
12375 u8 bDryRun; /* True if --dry-run */
12376 u8 bAppend; /* True if --append */
 
12377 int nArg; /* Number of command arguments */
12378 char *zSrcTable; /* "sqlar", "zipfile($file)" or "zip" */
12379 const char *zFile; /* --file argument, or NULL */
12380 const char *zDir; /* --directory argument, or NULL */
12381 char **azArg; /* Array of command arguments */
@@ -12418,17 +12449,22 @@
12418
12419 /*
12420 ** Print an error message for the .ar command to stderr and return
12421 ** SQLITE_ERROR.
12422 */
12423 static int arErrorMsg(const char *zFmt, ...){
12424 va_list ap;
12425 char *z;
12426 va_start(ap, zFmt);
12427 z = sqlite3_vmprintf(zFmt, ap);
12428 va_end(ap);
12429 raw_printf(stderr, "Error: %s (try \".ar --help\")\n", z);
 
 
 
 
 
12430 sqlite3_free(z);
12431 return SQLITE_ERROR;
12432 }
12433
12434 /*
@@ -12455,11 +12491,11 @@
12455 case AR_CMD_EXTRACT:
12456 case AR_CMD_LIST:
12457 case AR_CMD_UPDATE:
12458 case AR_CMD_HELP:
12459 if( pAr->eCmd ){
12460 return arErrorMsg("multiple command options");
12461 }
12462 pAr->eCmd = eSwitch;
12463 break;
12464
12465 case AR_SWITCH_DRYRUN:
@@ -12515,12 +12551,10 @@
12515
12516 if( nArg<=1 ){
12517 return arUsage(stderr);
12518 }else{
12519 char *z = azArg[1];
12520 memset(pAr, 0, sizeof(ArCommand));
12521
12522 if( z[0]!='-' ){
12523 /* Traditional style [tar] invocation */
12524 int i;
12525 int iArg = 2;
12526 for(i=0; z[i]; i++){
@@ -12528,15 +12562,15 @@
12528 struct ArSwitch *pOpt;
12529 for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
12530 if( z[i]==pOpt->cShort ) break;
12531 }
12532 if( pOpt==pEnd ){
12533 return arErrorMsg("unrecognized option: %c", z[i]);
12534 }
12535 if( pOpt->bArg ){
12536 if( iArg>=nArg ){
12537 return arErrorMsg("option requires an argument: %c",z[i]);
12538 }
12539 zArg = azArg[iArg++];
12540 }
12541 if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
12542 }
@@ -12566,19 +12600,19 @@
12566 struct ArSwitch *pOpt;
12567 for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
12568 if( z[i]==pOpt->cShort ) break;
12569 }
12570 if( pOpt==pEnd ){
12571 return arErrorMsg("unrecognized option: %c\n", z[i]);
12572 }
12573 if( pOpt->bArg ){
12574 if( i<(n-1) ){
12575 zArg = &z[i+1];
12576 i = n;
12577 }else{
12578 if( iArg>=(nArg-1) ){
12579 return arErrorMsg("option requires an argument: %c\n",z[i]);
12580 }
12581 zArg = azArg[++iArg];
12582 }
12583 }
12584 if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
@@ -12596,23 +12630,23 @@
12596 struct ArSwitch *pOpt; /* Iterator */
12597 for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
12598 const char *zLong = pOpt->zLong;
12599 if( (n-2)<=strlen30(zLong) && 0==memcmp(&z[2], zLong, n-2) ){
12600 if( pMatch ){
12601 return arErrorMsg("ambiguous option: %s",z);
12602 }else{
12603 pMatch = pOpt;
12604 }
12605 }
12606 }
12607
12608 if( pMatch==0 ){
12609 return arErrorMsg("unrecognized option: %s", z);
12610 }
12611 if( pMatch->bArg ){
12612 if( iArg>=(nArg-1) ){
12613 return arErrorMsg("option requires an argument: %s", z);
12614 }
12615 zArg = azArg[++iArg];
12616 }
12617 if( arProcessSwitch(pAr, pMatch->eSwitch, zArg) ) return SQLITE_ERROR;
12618 }
@@ -12737,10 +12771,11 @@
12737 utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
12738 }
12739 }
12740 }
12741 shellFinalize(&rc, pSql);
 
12742 return rc;
12743 }
12744
12745
12746 /*
@@ -12939,16 +12974,18 @@
12939 /*
12940 ** Implementation of ".ar" dot command.
12941 */
12942 static int arDotCommand(
12943 ShellState *pState, /* Current shell tool state */
 
12944 char **azArg, /* Array of arguments passed to dot command */
12945 int nArg /* Number of entries in azArg[] */
12946 ){
12947 ArCommand cmd;
12948 int rc;
12949 memset(&cmd, 0, sizeof(cmd));
 
12950 rc = arParseCommand(azArg, nArg, &cmd);
12951 if( rc==SQLITE_OK ){
12952 int eDbType = SHELL_OPEN_UNSPEC;
12953 cmd.p = pState;
12954 cmd.db = pState->db;
@@ -12991,11 +13028,11 @@
12991 sqlite3_sqlar_init(cmd.db, 0, 0);
12992 sqlite3_create_function(cmd.db, "shell_putsnl", 1, SQLITE_UTF8, cmd.p,
12993 shellPutsFunc, 0, 0);
12994
12995 }
12996 if( cmd.zSrcTable==0 && cmd.bZip==0 ){
12997 if( cmd.eCmd!=AR_CMD_CREATE
12998 && sqlite3_table_column_metadata(cmd.db,0,"sqlar","name",0,0,0,0,0)
12999 ){
13000 utf8_printf(stderr, "database does not contain an 'sqlar' table\n");
13001 rc = SQLITE_ERROR;
@@ -13027,11 +13064,11 @@
13027 break;
13028 }
13029 }
13030 end_ar_command:
13031 if( cmd.db!=pState->db ){
13032 sqlite3_close(cmd.db);
13033 }
13034 sqlite3_free(cmd.zSrcTable);
13035
13036 return rc;
13037 }
@@ -13107,11 +13144,11 @@
13107 #endif
13108
13109 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
13110 if( c=='a' && strncmp(azArg[0], "archive", n)==0 ){
13111 open_db(p, 0);
13112 rc = arDotCommand(p, azArg, nArg);
13113 }else
13114 #endif
13115
13116 if( (c=='b' && n>=3 && strncmp(azArg[0], "backup", n)==0)
13117 || (c=='s' && n>=3 && strncmp(azArg[0], "save", n)==0)
@@ -13150,18 +13187,18 @@
13150 if( zDb==0 ) zDb = "main";
13151 rc = sqlite3_open_v2(zDestFile, &pDest,
13152 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, zVfs);
13153 if( rc!=SQLITE_OK ){
13154 utf8_printf(stderr, "Error: cannot open \"%s\"\n", zDestFile);
13155 sqlite3_close(pDest);
13156 return 1;
13157 }
13158 open_db(p, 0);
13159 pBackup = sqlite3_backup_init(pDest, "main", p->db, zDb);
13160 if( pBackup==0 ){
13161 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
13162 sqlite3_close(pDest);
13163 return 1;
13164 }
13165 while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){}
13166 sqlite3_backup_finish(pBackup);
13167 if( rc==SQLITE_DONE ){
@@ -13168,11 +13205,11 @@
13168 rc = 0;
13169 }else{
13170 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
13171 rc = 1;
13172 }
13173 sqlite3_close(pDest);
13174 }else
13175
13176 if( c=='b' && n>=3 && strncmp(azArg[0], "bail", n)==0 ){
13177 if( nArg==2 ){
13178 bail_on_error = booleanValue(azArg[1]);
@@ -13984,11 +14021,11 @@
13984 char *zNewFilename; /* Name of the database file to open */
13985 int iName = 1; /* Index in azArg[] of the filename */
13986 int newFlag = 0; /* True to delete file before opening */
13987 /* Close the existing database */
13988 session_close_all(p);
13989 sqlite3_close(p->db);
13990 p->db = 0;
13991 p->zDbFilename = 0;
13992 sqlite3_free(p->zFreeOnClose);
13993 p->zFreeOnClose = 0;
13994 p->openMode = SHELL_OPEN_UNSPEC;
@@ -14014,11 +14051,11 @@
14014 /* If a filename is specified, try to open it first */
14015 zNewFilename = nArg>iName ? sqlite3_mprintf("%s", azArg[iName]) : 0;
14016 if( zNewFilename ){
14017 if( newFlag ) shellDeleteFile(zNewFilename);
14018 p->zDbFilename = zNewFilename;
14019 open_db(p, 1);
14020 if( p->db==0 ){
14021 utf8_printf(stderr, "Error: cannot open '%s'\n", zNewFilename);
14022 sqlite3_free(zNewFilename);
14023 }else{
14024 p->zFreeOnClose = zNewFilename;
@@ -14164,18 +14201,18 @@
14164 goto meta_command_exit;
14165 }
14166 rc = sqlite3_open(zSrcFile, &pSrc);
14167 if( rc!=SQLITE_OK ){
14168 utf8_printf(stderr, "Error: cannot open \"%s\"\n", zSrcFile);
14169 sqlite3_close(pSrc);
14170 return 1;
14171 }
14172 open_db(p, 0);
14173 pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main");
14174 if( pBackup==0 ){
14175 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
14176 sqlite3_close(pSrc);
14177 return 1;
14178 }
14179 while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK
14180 || rc==SQLITE_BUSY ){
14181 if( rc==SQLITE_BUSY ){
@@ -14191,11 +14228,11 @@
14191 rc = 1;
14192 }else{
14193 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
14194 rc = 1;
14195 }
14196 sqlite3_close(pSrc);
14197 }else
14198
14199 if( c=='s' && strncmp(azArg[0], "scanstats", n)==0 ){
14200 if( nArg==2 ){
14201 p->scanstatsOn = (u8)booleanValue(azArg[1]);
@@ -14875,18 +14912,22 @@
14875 int ii;
14876 ShellText s;
14877 initText(&s);
14878 open_db(p, 0);
14879 rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
14880 if( rc ) return shellDatabaseError(p->db);
 
 
 
14881
14882 if( nArg>2 && c=='i' ){
14883 /* It is an historical accident that the .indexes command shows an error
14884 ** when called with the wrong number of arguments whereas the .tables
14885 ** command does not. */
14886 raw_printf(stderr, "Usage: .indexes ?LIKE-PATTERN?\n");
14887 rc = 1;
 
14888 goto meta_command_exit;
14889 }
14890 for(ii=0; sqlite3_step(pStmt)==SQLITE_ROW; ii++){
14891 const char *zDbName = (const char*)sqlite3_column_text(pStmt, 1);
14892 if( zDbName==0 ) continue;
@@ -15783,10 +15824,14 @@
15783 int warnInmemoryDb = 0;
15784 int readStdin = 1;
15785 int nCmd = 0;
15786 char **azCmd = 0;
15787 const char *zVfs = 0; /* Value of -vfs command-line option */
 
 
 
 
15788
15789 setBinaryMode(stdin, 0);
15790 setvbuf(stderr, 0, _IONBF, 0); /* Make sure stderr is unbuffered */
15791 stdin_is_interactive = isatty(0);
15792 stdout_is_console = isatty(1);
@@ -15806,20 +15851,23 @@
15806 ** subsequent sqlite3_config() calls will work. So copy all results into
15807 ** memory that does not come from the SQLite memory allocator.
15808 */
15809 #if !SQLITE_SHELL_IS_UTF8
15810 sqlite3_initialize();
15811 argv = malloc(sizeof(argv[0])*argc);
 
 
15812 if( argv==0 ) shell_out_of_memory();
15813 for(i=0; i<argc; i++){
15814 char *z = sqlite3_win32_unicode_to_utf8(wargv[i]);
15815 int n;
15816 if( z==0 ) shell_out_of_memory();
15817 n = (int)strlen(z);
15818 argv[i] = malloc( n+1 );
15819 if( argv[i]==0 ) shell_out_of_memory();
15820 memcpy(argv[i], z, n+1);
 
15821 sqlite3_free(z);
15822 }
15823 sqlite3_shutdown();
15824 #endif
15825
@@ -16137,16 +16185,16 @@
16137 if( nCmd>0 ){
16138 utf8_printf(stderr, "Error: cannot mix regular SQL or dot-commands"
16139 " with \"%s\"\n", z);
16140 return 1;
16141 }
16142 open_db(&data, 0);
16143 if( z[2] ){
16144 argv[i] = &z[2];
16145 arDotCommand(&data, argv+(i-1), argc-(i-1));
16146 }else{
16147 arDotCommand(&data, argv+i, argc-i);
16148 }
16149 readStdin = 0;
16150 break;
16151 #endif
16152 }else{
@@ -16221,18 +16269,21 @@
16221 }
16222 }
16223 set_table_name(&data, 0);
16224 if( data.db ){
16225 session_close_all(&data);
16226 sqlite3_close(data.db);
16227 }
16228 sqlite3_free(data.zFreeOnClose);
16229 find_home_dir(1);
16230 output_reset(&data);
16231 data.doXdgOpen = 0;
16232 clearTempFile(&data);
16233 #if !SQLITE_SHELL_IS_UTF8
16234 for(i=0; i<argc; i++) free(argv[i]);
16235 free(argv);
16236 #endif
 
 
 
16237 return rc;
16238 }
16239
--- src/shell.c
+++ src/shell.c
@@ -7646,11 +7646,13 @@
7646 }
7647 break;
7648 }
7649 }
7650
7651 if( zDetail[0]!='-' ){
7652 pStmt->zEQP = idxAppendText(&rc, pStmt->zEQP, "%s\n", zDetail);
7653 }
7654 }
7655
7656 for(pEntry=hIdx.pFirst; pEntry; pEntry=pEntry->pNext){
7657 pStmt->zIdx = idxAppendText(&rc, pStmt->zIdx, "%s;\n", pEntry->zKey);
7658 }
@@ -11045,12 +11047,15 @@
11047 FILE *f = fopen(zName, "rb");
11048 size_t n;
11049 int rc = SHELL_OPEN_UNSPEC;
11050 char zBuf[100];
11051 if( f==0 ){
11052 if( dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
11053 return SHELL_OPEN_ZIPFILE;
11054 }else{
11055 return SHELL_OPEN_NORMAL;
11056 }
11057 }
11058 fseek(f, -25, SEEK_END);
11059 n = fread(zBuf, 25, 1, f);
11060 if( n==1 && memcmp(zBuf, "Start-Of-SQLite3-", 17)==0 ){
11061 rc = SHELL_OPEN_APPENDVFS;
@@ -11059,28 +11064,42 @@
11064 n = fread(zBuf, 22, 1, f);
11065 if( n==1 && zBuf[0]==0x50 && zBuf[1]==0x4b && zBuf[2]==0x05
11066 && zBuf[3]==0x06 ){
11067 rc = SHELL_OPEN_ZIPFILE;
11068 }else if( n==0 && dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
11069 rc = SHELL_OPEN_ZIPFILE;
11070 }
11071 }
11072 fclose(f);
11073 return rc;
11074 }
11075
11076 /* Flags for open_db().
11077 **
11078 ** The default behavior of open_db() is to exit(1) if the database fails to
11079 ** open. The OPEN_DB_KEEPALIVE flag changes that so that it prints an error
11080 ** but still returns without calling exit.
11081 **
11082 ** The OPEN_DB_ZIPFILE flag causes open_db() to prefer to open files as a
11083 ** ZIP archive if the file does not exist or is empty and its name matches
11084 ** the *.zip pattern.
11085 */
11086 #define OPEN_DB_KEEPALIVE 0x001 /* Return after error if true */
11087 #define OPEN_DB_ZIPFILE 0x002 /* Open as ZIP if name matches *.zip */
11088
11089 /*
11090 ** Make sure the database is open. If it is not, then open it. If
11091 ** the database fails to open, print an error message and exit.
11092 */
11093 static void open_db(ShellState *p, int openFlags){
11094 if( p->db==0 ){
11095 if( p->openMode==SHELL_OPEN_UNSPEC ){
11096 if( p->zDbFilename==0 || p->zDbFilename[0]==0 ){
11097 p->openMode = SHELL_OPEN_NORMAL;
11098 }else{
11099 p->openMode = (u8)deduceDatabaseType(p->zDbFilename,
11100 (openFlags & OPEN_DB_ZIPFILE)!=0);
11101 }
11102 }
11103 switch( p->openMode ){
11104 case SHELL_OPEN_APPENDVFS: {
11105 sqlite3_open_v2(p->zDbFilename, &p->db,
@@ -11103,11 +11122,11 @@
11122 }
11123 globalDb = p->db;
11124 if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){
11125 utf8_printf(stderr,"Error: unable to open database \"%s\": %s\n",
11126 p->zDbFilename, sqlite3_errmsg(p->db));
11127 if( openFlags & OPEN_DB_KEEPALIVE ) return;
11128 exit(1);
11129 }
11130 #ifndef SQLITE_OMIT_LOAD_EXTENSION
11131 sqlite3_enable_load_extension(p->db, 1);
11132 #endif
@@ -11136,10 +11155,21 @@
11155 sqlite3_exec(p->db, zSql, 0, 0, 0);
11156 sqlite3_free(zSql);
11157 }
11158 }
11159 }
11160
11161 /*
11162 ** Attempt to close the databaes connection. Report errors.
11163 */
11164 void close_db(sqlite3 *db){
11165 int rc = sqlite3_close(db);
11166 if( rc ){
11167 utf8_printf(stderr, "Error: sqlite3_close() returns %d: %s\n",
11168 rc, sqlite3_errmsg(db));
11169 }
11170 }
11171
11172 #if HAVE_READLINE || HAVE_EDITLINE
11173 /*
11174 ** Readline completion callbacks
11175 */
@@ -11717,11 +11747,11 @@
11747 tryToCloneSchema(p, newDb, "type='table'", tryToCloneData);
11748 tryToCloneSchema(p, newDb, "type!='table'", 0);
11749 sqlite3_exec(newDb, "COMMIT;", 0, 0, 0);
11750 sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
11751 }
11752 close_db(newDb);
11753 }
11754
11755 /*
11756 ** Change the output file back to stdout.
11757 **
@@ -12372,10 +12402,11 @@
12402 u8 eCmd; /* An AR_CMD_* value */
12403 u8 bVerbose; /* True if --verbose */
12404 u8 bZip; /* True if the archive is a ZIP */
12405 u8 bDryRun; /* True if --dry-run */
12406 u8 bAppend; /* True if --append */
12407 u8 fromCmdLine; /* Run from -A instead of .archive */
12408 int nArg; /* Number of command arguments */
12409 char *zSrcTable; /* "sqlar", "zipfile($file)" or "zip" */
12410 const char *zFile; /* --file argument, or NULL */
12411 const char *zDir; /* --directory argument, or NULL */
12412 char **azArg; /* Array of command arguments */
@@ -12418,17 +12449,22 @@
12449
12450 /*
12451 ** Print an error message for the .ar command to stderr and return
12452 ** SQLITE_ERROR.
12453 */
12454 static int arErrorMsg(ArCommand *pAr, const char *zFmt, ...){
12455 va_list ap;
12456 char *z;
12457 va_start(ap, zFmt);
12458 z = sqlite3_vmprintf(zFmt, ap);
12459 va_end(ap);
12460 utf8_printf(stderr, "Error: %s\n", z);
12461 if( pAr->fromCmdLine ){
12462 utf8_printf(stderr, "Use \"-A\" for more help\n");
12463 }else{
12464 utf8_printf(stderr, "Use \".archive --help\" for more help\n");
12465 }
12466 sqlite3_free(z);
12467 return SQLITE_ERROR;
12468 }
12469
12470 /*
@@ -12455,11 +12491,11 @@
12491 case AR_CMD_EXTRACT:
12492 case AR_CMD_LIST:
12493 case AR_CMD_UPDATE:
12494 case AR_CMD_HELP:
12495 if( pAr->eCmd ){
12496 return arErrorMsg(pAr, "multiple command options");
12497 }
12498 pAr->eCmd = eSwitch;
12499 break;
12500
12501 case AR_SWITCH_DRYRUN:
@@ -12515,12 +12551,10 @@
12551
12552 if( nArg<=1 ){
12553 return arUsage(stderr);
12554 }else{
12555 char *z = azArg[1];
 
 
12556 if( z[0]!='-' ){
12557 /* Traditional style [tar] invocation */
12558 int i;
12559 int iArg = 2;
12560 for(i=0; z[i]; i++){
@@ -12528,15 +12562,15 @@
12562 struct ArSwitch *pOpt;
12563 for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
12564 if( z[i]==pOpt->cShort ) break;
12565 }
12566 if( pOpt==pEnd ){
12567 return arErrorMsg(pAr, "unrecognized option: %c", z[i]);
12568 }
12569 if( pOpt->bArg ){
12570 if( iArg>=nArg ){
12571 return arErrorMsg(pAr, "option requires an argument: %c",z[i]);
12572 }
12573 zArg = azArg[iArg++];
12574 }
12575 if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
12576 }
@@ -12566,19 +12600,19 @@
12600 struct ArSwitch *pOpt;
12601 for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
12602 if( z[i]==pOpt->cShort ) break;
12603 }
12604 if( pOpt==pEnd ){
12605 return arErrorMsg(pAr, "unrecognized option: %c", z[i]);
12606 }
12607 if( pOpt->bArg ){
12608 if( i<(n-1) ){
12609 zArg = &z[i+1];
12610 i = n;
12611 }else{
12612 if( iArg>=(nArg-1) ){
12613 return arErrorMsg(pAr, "option requires an argument: %c",z[i]);
12614 }
12615 zArg = azArg[++iArg];
12616 }
12617 }
12618 if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
@@ -12596,23 +12630,23 @@
12630 struct ArSwitch *pOpt; /* Iterator */
12631 for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
12632 const char *zLong = pOpt->zLong;
12633 if( (n-2)<=strlen30(zLong) && 0==memcmp(&z[2], zLong, n-2) ){
12634 if( pMatch ){
12635 return arErrorMsg(pAr, "ambiguous option: %s",z);
12636 }else{
12637 pMatch = pOpt;
12638 }
12639 }
12640 }
12641
12642 if( pMatch==0 ){
12643 return arErrorMsg(pAr, "unrecognized option: %s", z);
12644 }
12645 if( pMatch->bArg ){
12646 if( iArg>=(nArg-1) ){
12647 return arErrorMsg(pAr, "option requires an argument: %s", z);
12648 }
12649 zArg = azArg[++iArg];
12650 }
12651 if( arProcessSwitch(pAr, pMatch->eSwitch, zArg) ) return SQLITE_ERROR;
12652 }
@@ -12737,10 +12771,11 @@
12771 utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
12772 }
12773 }
12774 }
12775 shellFinalize(&rc, pSql);
12776 sqlite3_free(zWhere);
12777 return rc;
12778 }
12779
12780
12781 /*
@@ -12939,16 +12974,18 @@
12974 /*
12975 ** Implementation of ".ar" dot command.
12976 */
12977 static int arDotCommand(
12978 ShellState *pState, /* Current shell tool state */
12979 int fromCmdLine, /* True if -A command-line option, not .ar cmd */
12980 char **azArg, /* Array of arguments passed to dot command */
12981 int nArg /* Number of entries in azArg[] */
12982 ){
12983 ArCommand cmd;
12984 int rc;
12985 memset(&cmd, 0, sizeof(cmd));
12986 cmd.fromCmdLine = fromCmdLine;
12987 rc = arParseCommand(azArg, nArg, &cmd);
12988 if( rc==SQLITE_OK ){
12989 int eDbType = SHELL_OPEN_UNSPEC;
12990 cmd.p = pState;
12991 cmd.db = pState->db;
@@ -12991,11 +13028,11 @@
13028 sqlite3_sqlar_init(cmd.db, 0, 0);
13029 sqlite3_create_function(cmd.db, "shell_putsnl", 1, SQLITE_UTF8, cmd.p,
13030 shellPutsFunc, 0, 0);
13031
13032 }
13033 if( cmd.zSrcTable==0 && cmd.bZip==0 && cmd.eCmd!=AR_CMD_HELP ){
13034 if( cmd.eCmd!=AR_CMD_CREATE
13035 && sqlite3_table_column_metadata(cmd.db,0,"sqlar","name",0,0,0,0,0)
13036 ){
13037 utf8_printf(stderr, "database does not contain an 'sqlar' table\n");
13038 rc = SQLITE_ERROR;
@@ -13027,11 +13064,11 @@
13064 break;
13065 }
13066 }
13067 end_ar_command:
13068 if( cmd.db!=pState->db ){
13069 close_db(cmd.db);
13070 }
13071 sqlite3_free(cmd.zSrcTable);
13072
13073 return rc;
13074 }
@@ -13107,11 +13144,11 @@
13144 #endif
13145
13146 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
13147 if( c=='a' && strncmp(azArg[0], "archive", n)==0 ){
13148 open_db(p, 0);
13149 rc = arDotCommand(p, 0, azArg, nArg);
13150 }else
13151 #endif
13152
13153 if( (c=='b' && n>=3 && strncmp(azArg[0], "backup", n)==0)
13154 || (c=='s' && n>=3 && strncmp(azArg[0], "save", n)==0)
@@ -13150,18 +13187,18 @@
13187 if( zDb==0 ) zDb = "main";
13188 rc = sqlite3_open_v2(zDestFile, &pDest,
13189 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, zVfs);
13190 if( rc!=SQLITE_OK ){
13191 utf8_printf(stderr, "Error: cannot open \"%s\"\n", zDestFile);
13192 close_db(pDest);
13193 return 1;
13194 }
13195 open_db(p, 0);
13196 pBackup = sqlite3_backup_init(pDest, "main", p->db, zDb);
13197 if( pBackup==0 ){
13198 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
13199 close_db(pDest);
13200 return 1;
13201 }
13202 while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){}
13203 sqlite3_backup_finish(pBackup);
13204 if( rc==SQLITE_DONE ){
@@ -13168,11 +13205,11 @@
13205 rc = 0;
13206 }else{
13207 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
13208 rc = 1;
13209 }
13210 close_db(pDest);
13211 }else
13212
13213 if( c=='b' && n>=3 && strncmp(azArg[0], "bail", n)==0 ){
13214 if( nArg==2 ){
13215 bail_on_error = booleanValue(azArg[1]);
@@ -13984,11 +14021,11 @@
14021 char *zNewFilename; /* Name of the database file to open */
14022 int iName = 1; /* Index in azArg[] of the filename */
14023 int newFlag = 0; /* True to delete file before opening */
14024 /* Close the existing database */
14025 session_close_all(p);
14026 close_db(p->db);
14027 p->db = 0;
14028 p->zDbFilename = 0;
14029 sqlite3_free(p->zFreeOnClose);
14030 p->zFreeOnClose = 0;
14031 p->openMode = SHELL_OPEN_UNSPEC;
@@ -14014,11 +14051,11 @@
14051 /* If a filename is specified, try to open it first */
14052 zNewFilename = nArg>iName ? sqlite3_mprintf("%s", azArg[iName]) : 0;
14053 if( zNewFilename ){
14054 if( newFlag ) shellDeleteFile(zNewFilename);
14055 p->zDbFilename = zNewFilename;
14056 open_db(p, OPEN_DB_KEEPALIVE);
14057 if( p->db==0 ){
14058 utf8_printf(stderr, "Error: cannot open '%s'\n", zNewFilename);
14059 sqlite3_free(zNewFilename);
14060 }else{
14061 p->zFreeOnClose = zNewFilename;
@@ -14164,18 +14201,18 @@
14201 goto meta_command_exit;
14202 }
14203 rc = sqlite3_open(zSrcFile, &pSrc);
14204 if( rc!=SQLITE_OK ){
14205 utf8_printf(stderr, "Error: cannot open \"%s\"\n", zSrcFile);
14206 close_db(pSrc);
14207 return 1;
14208 }
14209 open_db(p, 0);
14210 pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main");
14211 if( pBackup==0 ){
14212 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
14213 close_db(pSrc);
14214 return 1;
14215 }
14216 while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK
14217 || rc==SQLITE_BUSY ){
14218 if( rc==SQLITE_BUSY ){
@@ -14191,11 +14228,11 @@
14228 rc = 1;
14229 }else{
14230 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
14231 rc = 1;
14232 }
14233 close_db(pSrc);
14234 }else
14235
14236 if( c=='s' && strncmp(azArg[0], "scanstats", n)==0 ){
14237 if( nArg==2 ){
14238 p->scanstatsOn = (u8)booleanValue(azArg[1]);
@@ -14875,18 +14912,22 @@
14912 int ii;
14913 ShellText s;
14914 initText(&s);
14915 open_db(p, 0);
14916 rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
14917 if( rc ){
14918 sqlite3_finalize(pStmt);
14919 return shellDatabaseError(p->db);
14920 }
14921
14922 if( nArg>2 && c=='i' ){
14923 /* It is an historical accident that the .indexes command shows an error
14924 ** when called with the wrong number of arguments whereas the .tables
14925 ** command does not. */
14926 raw_printf(stderr, "Usage: .indexes ?LIKE-PATTERN?\n");
14927 rc = 1;
14928 sqlite3_finalize(pStmt);
14929 goto meta_command_exit;
14930 }
14931 for(ii=0; sqlite3_step(pStmt)==SQLITE_ROW; ii++){
14932 const char *zDbName = (const char*)sqlite3_column_text(pStmt, 1);
14933 if( zDbName==0 ) continue;
@@ -15783,10 +15824,14 @@
15824 int warnInmemoryDb = 0;
15825 int readStdin = 1;
15826 int nCmd = 0;
15827 char **azCmd = 0;
15828 const char *zVfs = 0; /* Value of -vfs command-line option */
15829 #if !SQLITE_SHELL_IS_UTF8
15830 char **argvToFree = 0;
15831 int argcToFree = 0;
15832 #endif
15833
15834 setBinaryMode(stdin, 0);
15835 setvbuf(stderr, 0, _IONBF, 0); /* Make sure stderr is unbuffered */
15836 stdin_is_interactive = isatty(0);
15837 stdout_is_console = isatty(1);
@@ -15806,20 +15851,23 @@
15851 ** subsequent sqlite3_config() calls will work. So copy all results into
15852 ** memory that does not come from the SQLite memory allocator.
15853 */
15854 #if !SQLITE_SHELL_IS_UTF8
15855 sqlite3_initialize();
15856 argvToFree = malloc(sizeof(argv[0])*argc*2);
15857 argcToFree = argc;
15858 argv = argvToFree + argc;
15859 if( argv==0 ) shell_out_of_memory();
15860 for(i=0; i<argc; i++){
15861 char *z = sqlite3_win32_unicode_to_utf8(wargv[i]);
15862 int n;
15863 if( z==0 ) shell_out_of_memory();
15864 n = (int)strlen(z);
15865 argv[i] = malloc( n+1 );
15866 if( argv[i]==0 ) shell_out_of_memory();
15867 memcpy(argv[i], z, n+1);
15868 argvToFree[i] = argv[i];
15869 sqlite3_free(z);
15870 }
15871 sqlite3_shutdown();
15872 #endif
15873
@@ -16137,16 +16185,16 @@
16185 if( nCmd>0 ){
16186 utf8_printf(stderr, "Error: cannot mix regular SQL or dot-commands"
16187 " with \"%s\"\n", z);
16188 return 1;
16189 }
16190 open_db(&data, OPEN_DB_ZIPFILE);
16191 if( z[2] ){
16192 argv[i] = &z[2];
16193 arDotCommand(&data, 1, argv+(i-1), argc-(i-1));
16194 }else{
16195 arDotCommand(&data, 1, argv+i, argc-i);
16196 }
16197 readStdin = 0;
16198 break;
16199 #endif
16200 }else{
@@ -16221,18 +16269,21 @@
16269 }
16270 }
16271 set_table_name(&data, 0);
16272 if( data.db ){
16273 session_close_all(&data);
16274 close_db(data.db);
16275 }
16276 sqlite3_free(data.zFreeOnClose);
16277 find_home_dir(1);
16278 output_reset(&data);
16279 data.doXdgOpen = 0;
16280 clearTempFile(&data);
16281 #if !SQLITE_SHELL_IS_UTF8
16282 for(i=0; i<argcToFree; i++) free(argvToFree[i]);
16283 free(argvToFree);
16284 #endif
16285 /* Clear the global data structure so that valgrind will detect memory
16286 ** leaks */
16287 memset(&data, 0, sizeof(data));
16288 return rc;
16289 }
16290
+499 -179
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -1150,11 +1150,11 @@
11501150
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
11511151
** [sqlite_version()] and [sqlite_source_id()].
11521152
*/
11531153
#define SQLITE_VERSION "3.24.0"
11541154
#define SQLITE_VERSION_NUMBER 3024000
1155
-#define SQLITE_SOURCE_ID "2018-05-14 00:41:12 d0f35739af3b226c8eef39676407293650cde551acef06fe8628fdd5b59bd66a"
1155
+#define SQLITE_SOURCE_ID "2018-05-30 01:14:20 86ee267ee86f5264774a9f215b1158aeaa2d605e77c205731b5ee3945d7de4c2"
11561156
11571157
/*
11581158
** CAPI3REF: Run-Time Library Version Numbers
11591159
** KEYWORDS: sqlite3_version sqlite3_sourceid
11601160
**
@@ -1529,17 +1529,19 @@
15291529
#define SQLITE_IOERR_AUTH (SQLITE_IOERR | (28<<8))
15301530
#define SQLITE_IOERR_BEGIN_ATOMIC (SQLITE_IOERR | (29<<8))
15311531
#define SQLITE_IOERR_COMMIT_ATOMIC (SQLITE_IOERR | (30<<8))
15321532
#define SQLITE_IOERR_ROLLBACK_ATOMIC (SQLITE_IOERR | (31<<8))
15331533
#define SQLITE_LOCKED_SHAREDCACHE (SQLITE_LOCKED | (1<<8))
1534
+#define SQLITE_LOCKED_VTAB (SQLITE_LOCKED | (2<<8))
15341535
#define SQLITE_BUSY_RECOVERY (SQLITE_BUSY | (1<<8))
15351536
#define SQLITE_BUSY_SNAPSHOT (SQLITE_BUSY | (2<<8))
15361537
#define SQLITE_CANTOPEN_NOTEMPDIR (SQLITE_CANTOPEN | (1<<8))
15371538
#define SQLITE_CANTOPEN_ISDIR (SQLITE_CANTOPEN | (2<<8))
15381539
#define SQLITE_CANTOPEN_FULLPATH (SQLITE_CANTOPEN | (3<<8))
15391540
#define SQLITE_CANTOPEN_CONVPATH (SQLITE_CANTOPEN | (4<<8))
15401541
#define SQLITE_CORRUPT_VTAB (SQLITE_CORRUPT | (1<<8))
1542
+#define SQLITE_CORRUPT_SEQUENCE (SQLITE_CORRUPT | (2<<8))
15411543
#define SQLITE_READONLY_RECOVERY (SQLITE_READONLY | (1<<8))
15421544
#define SQLITE_READONLY_CANTLOCK (SQLITE_READONLY | (2<<8))
15431545
#define SQLITE_READONLY_ROLLBACK (SQLITE_READONLY | (3<<8))
15441546
#define SQLITE_READONLY_DBMOVED (SQLITE_READONLY | (4<<8))
15451547
#define SQLITE_READONLY_CANTINIT (SQLITE_READONLY | (5<<8))
@@ -7317,10 +7319,14 @@
73177319
sqlite3_uint64 colUsed; /* Input: Mask of columns used by statement */
73187320
};
73197321
73207322
/*
73217323
** CAPI3REF: Virtual Table Scan Flags
7324
+**
7325
+** Virtual table implementations are allowed to set the
7326
+** [sqlite3_index_info].idxFlags field to some combination of
7327
+** these bits.
73227328
*/
73237329
#define SQLITE_INDEX_SCAN_UNIQUE 1 /* Scan visits at most 1 row */
73247330
73257331
/*
73267332
** CAPI3REF: Virtual Table Constraint Operator Codes
@@ -8180,15 +8186,23 @@
81808186
/*
81818187
** CAPI3REF: Create A New Dynamic String Object
81828188
** CONSTRUCTOR: sqlite3_str
81838189
**
81848190
** ^The [sqlite3_str_new(D)] interface allocates and initializes
8185
-** a new [sqlite3_str]
8186
-** object. ^The [sqlite3_str_new()] interface returns NULL on an out-of-memory
8187
-** condition. To avoid memory leaks, the object returned by
8191
+** a new [sqlite3_str] object. To avoid memory leaks, the object returned by
81888192
** [sqlite3_str_new()] must be freed by a subsequent call to
81898193
** [sqlite3_str_finish(X)].
8194
+**
8195
+** ^The [sqlite3_str_new(D)] interface always returns a pointer to a
8196
+** valid [sqlite3_str] object, though in the event of an out-of-memory
8197
+** error the returned object might be a special singleton that will
8198
+** silently reject new text, always return SQLITE_NOMEM from
8199
+** [sqlite3_str_errcode()], always return 0 for
8200
+** [sqlite3_str_length()], and always return NULL from
8201
+** [sqlite3_str_finish(X)]. It is always safe to use the value
8202
+** returned by [sqlite3_str_new(D)] as the sqlite3_str parameter
8203
+** to any of the other [sqlite3_str] methods.
81908204
**
81918205
** The D parameter to [sqlite3_str_new(D)] may be NULL. If the
81928206
** D parameter in [sqlite3_str_new(D)] is not NULL, then the maximum
81938207
** length of the string contained in the [sqlite3_str] object will be
81948208
** the value set for [sqlite3_limit](D,[SQLITE_LIMIT_LENGTH]) instead
@@ -9550,15 +9564,15 @@
95509564
**
95519565
** If the sqlite3_vtab_nochange(X) routine is called within the [xColumn]
95529566
** method of a [virtual table], then it returns true if and only if the
95539567
** column is being fetched as part of an UPDATE operation during which the
95549568
** column value will not change. Applications might use this to substitute
9555
-** a lighter-weight value to return that the corresponding [xUpdate] method
9556
-** understands as a "no-change" value.
9569
+** a return value that is less expensive to compute and that the corresponding
9570
+** [xUpdate] method understands as a "no-change" value.
95579571
**
95589572
** If the [xColumn] method calls sqlite3_vtab_nochange() and finds that
9559
-** the column is not changed by the UPDATE statement, they the xColumn
9573
+** the column is not changed by the UPDATE statement, then the xColumn
95609574
** method can optionally return without setting a result, without calling
95619575
** any of the [sqlite3_result_int|sqlite3_result_xxxxx() interfaces].
95629576
** In that case, [sqlite3_value_nochange(X)] will return true for the
95639577
** same column in the [xUpdate] method.
95649578
*/
@@ -14632,10 +14646,11 @@
1463214646
#define OP_Function 166 /* synopsis: r[P3]=func(r[P2@P5]) */
1463314647
#define OP_Trace 167
1463414648
#define OP_CursorHint 168
1463514649
#define OP_Noop 169
1463614650
#define OP_Explain 170
14651
+#define OP_Abortable 171
1463714652
1463814653
/* Properties such as "out2" or "jump" that are specified in
1463914654
** comments following the "case" for each opcode in the vdbe.c
1464014655
** are encoded into bitvectors as follows:
1464114656
*/
@@ -14665,11 +14680,11 @@
1466514680
/* 128 */ 0x04, 0x04, 0x00, 0x00, 0x10, 0x10, 0x10, 0x00,\
1466614681
/* 136 */ 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
1466714682
/* 144 */ 0x00, 0x06, 0x10, 0x00, 0x04, 0x1a, 0x00, 0x00,\
1466814683
/* 152 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
1466914684
/* 160 */ 0x00, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,\
14670
-/* 168 */ 0x00, 0x00, 0x00,}
14685
+/* 168 */ 0x00, 0x00, 0x00, 0x00,}
1467114686
1467214687
/* The sqlite3P2Values() routine is able to run faster if it knows
1467314688
** the value of the largest JUMP opcode. The smaller the maximum
1467414689
** JUMP opcode the better, so the mkopcodeh.tcl script that
1467514690
** generated this include file strives to group all JUMP opcodes
@@ -14707,10 +14722,15 @@
1470714722
SQLITE_PRIVATE void sqlite3VdbeVerifyNoResultRow(Vdbe *p);
1470814723
#else
1470914724
# define sqlite3VdbeVerifyNoMallocRequired(A,B)
1471014725
# define sqlite3VdbeVerifyNoResultRow(A)
1471114726
#endif
14727
+#if defined(SQLITE_DEBUG)
14728
+SQLITE_PRIVATE void sqlite3VdbeVerifyAbortable(Vdbe *p, int);
14729
+#else
14730
+# define sqlite3VdbeVerifyAbortable(A,B)
14731
+#endif
1471214732
SQLITE_PRIVATE VdbeOp *sqlite3VdbeAddOpList(Vdbe*, int nOp, VdbeOpList const *aOp,int iLineno);
1471314733
#ifndef SQLITE_OMIT_EXPLAIN
1471414734
SQLITE_PRIVATE void sqlite3VdbeExplain(Parse*,u8,const char*,...);
1471514735
SQLITE_PRIVATE void sqlite3VdbeExplainPop(Parse*);
1471614736
SQLITE_PRIVATE int sqlite3VdbeExplainParent(Parse*);
@@ -18683,10 +18703,11 @@
1868318703
SQLITE_PRIVATE int sqlite3CreateFunc(sqlite3 *, const char *, int, int, void *,
1868418704
void (*)(sqlite3_context*,int,sqlite3_value **),
1868518705
void (*)(sqlite3_context*,int,sqlite3_value **), void (*)(sqlite3_context*),
1868618706
FuncDestructor *pDestructor
1868718707
);
18708
+SQLITE_PRIVATE void sqlite3NoopDestructor(void*);
1868818709
SQLITE_PRIVATE void sqlite3OomFault(sqlite3*);
1868918710
SQLITE_PRIVATE void sqlite3OomClear(sqlite3*);
1869018711
SQLITE_PRIVATE int sqlite3ApiExit(sqlite3 *db, int);
1869118712
SQLITE_PRIVATE int sqlite3OpenTempDatabase(Parse *);
1869218713
@@ -18785,11 +18806,10 @@
1878518806
SQLITE_PRIVATE int sqlite3VtabCallCreate(sqlite3*, int, const char *, char **);
1878618807
SQLITE_PRIVATE int sqlite3VtabCallConnect(Parse*, Table*);
1878718808
SQLITE_PRIVATE int sqlite3VtabCallDestroy(sqlite3*, int, const char *);
1878818809
SQLITE_PRIVATE int sqlite3VtabBegin(sqlite3 *, VTable *);
1878918810
SQLITE_PRIVATE FuncDef *sqlite3VtabOverloadFunction(sqlite3 *,FuncDef*, int nArg, Expr*);
18790
-SQLITE_PRIVATE void sqlite3InvalidFunction(sqlite3_context*,int,sqlite3_value**);
1879118811
SQLITE_PRIVATE sqlite3_int64 sqlite3StmtCurrentTime(sqlite3_context*);
1879218812
SQLITE_PRIVATE int sqlite3VdbeParameterIndex(Vdbe*, const char*, int);
1879318813
SQLITE_PRIVATE int sqlite3TransferBindings(sqlite3_stmt *, sqlite3_stmt *);
1879418814
SQLITE_PRIVATE void sqlite3ParserReset(Parse*);
1879518815
SQLITE_PRIVATE int sqlite3Reprepare(Vdbe*);
@@ -19715,10 +19735,11 @@
1971519735
i64 startTime; /* Time when query started - used for profiling */
1971619736
#endif
1971719737
int nOp; /* Number of instructions in the program */
1971819738
#ifdef SQLITE_DEBUG
1971919739
int rcApp; /* errcode set by sqlite3_result_error_code() */
19740
+ u32 nWrite; /* Number of write operations that have occurred */
1972019741
#endif
1972119742
u16 nResColumn; /* Number of columns in one row of the result set */
1972219743
u8 errorAction; /* Recovery action to do in case of an error */
1972319744
u8 minWriteFileFormat; /* Minimum file format for writable database files */
1972419745
u8 prepFlags; /* SQLITE_PREPARE_* flags */
@@ -19849,10 +19870,18 @@
1984919870
SQLITE_PRIVATE int sqlite3VdbeSorterRowkey(const VdbeCursor *, Mem *);
1985019871
SQLITE_PRIVATE int sqlite3VdbeSorterNext(sqlite3 *, const VdbeCursor *);
1985119872
SQLITE_PRIVATE int sqlite3VdbeSorterRewind(const VdbeCursor *, int *);
1985219873
SQLITE_PRIVATE int sqlite3VdbeSorterWrite(const VdbeCursor *, Mem *);
1985319874
SQLITE_PRIVATE int sqlite3VdbeSorterCompare(const VdbeCursor *, Mem *, int, int *);
19875
+
19876
+#ifdef SQLITE_DEBUG
19877
+SQLITE_PRIVATE void sqlite3VdbeIncrWriteCounter(Vdbe*, VdbeCursor*);
19878
+SQLITE_PRIVATE void sqlite3VdbeAssertAbortable(Vdbe*);
19879
+#else
19880
+# define sqlite3VdbeIncrWriteCounter(V,C)
19881
+# define sqlite3VdbeAssertAbortable(V)
19882
+#endif
1985419883
1985519884
#if !defined(SQLITE_OMIT_SHARED_CACHE)
1985619885
SQLITE_PRIVATE void sqlite3VdbeEnter(Vdbe*);
1985719886
#else
1985819887
# define sqlite3VdbeEnter(X)
@@ -27537,16 +27566,26 @@
2753727566
return strAccumFinishRealloc(p);
2753827567
}
2753927568
}
2754027569
return p->zText;
2754127570
}
27571
+
27572
+/*
27573
+** This singleton is an sqlite3_str object that is returned if
27574
+** sqlite3_malloc() fails to provide space for a real one. This
27575
+** sqlite3_str object accepts no new text and always returns
27576
+** an SQLITE_NOMEM error.
27577
+*/
27578
+static sqlite3_str sqlite3OomStr = {
27579
+ 0, 0, 0, 0, 0, SQLITE_NOMEM, 0
27580
+};
2754227581
2754327582
/* Finalize a string created using sqlite3_str_new().
2754427583
*/
2754527584
SQLITE_API char *sqlite3_str_finish(sqlite3_str *p){
2754627585
char *z;
27547
- if( p ){
27586
+ if( p!=0 && p!=&sqlite3OomStr ){
2754827587
z = sqlite3StrAccumFinish(p);
2754927588
sqlite3_free(p);
2755027589
}else{
2755127590
z = 0;
2755227591
}
@@ -27611,10 +27650,12 @@
2761127650
SQLITE_API sqlite3_str *sqlite3_str_new(sqlite3 *db){
2761227651
sqlite3_str *p = sqlite3_malloc64(sizeof(*p));
2761327652
if( p ){
2761427653
sqlite3StrAccumInit(p, 0, 0, 0,
2761527654
db ? db->aLimit[SQLITE_LIMIT_LENGTH] : SQLITE_MAX_LENGTH);
27655
+ }else{
27656
+ p = &sqlite3OomStr;
2761627657
}
2761727658
return p;
2761827659
}
2761927660
2762027661
/*
@@ -31409,10 +31450,11 @@
3140931450
/* 166 */ "Function" OpHelp("r[P3]=func(r[P2@P5])"),
3141031451
/* 167 */ "Trace" OpHelp(""),
3141131452
/* 168 */ "CursorHint" OpHelp(""),
3141231453
/* 169 */ "Noop" OpHelp(""),
3141331454
/* 170 */ "Explain" OpHelp(""),
31455
+ /* 171 */ "Abortable" OpHelp(""),
3141431456
};
3141531457
return azName[i];
3141631458
}
3141731459
#endif
3141831460
@@ -73732,11 +73774,11 @@
7373273774
pMem->flags = MEM_Int;
7373373775
}
7373473776
}
7373573777
7373673778
/* A no-op destructor */
73737
-static void sqlite3NoopDestructor(void *p){ UNUSED_PARAMETER(p); }
73779
+SQLITE_PRIVATE void sqlite3NoopDestructor(void *p){ UNUSED_PARAMETER(p); }
7373873780
7373973781
/*
7374073782
** Set the value stored in *pMem should already be a NULL.
7374173783
** Also store a pointer to go with it.
7374273784
*/
@@ -75378,10 +75420,36 @@
7537875420
return ( v->db->mallocFailed || hasAbort==mayAbort || hasFkCounter
7537975421
|| (hasCreateTable && hasInitCoroutine) );
7538075422
}
7538175423
#endif /* SQLITE_DEBUG - the sqlite3AssertMayAbort() function */
7538275424
75425
+#ifdef SQLITE_DEBUG
75426
+/*
75427
+** Increment the nWrite counter in the VDBE if the cursor is not an
75428
+** ephemeral cursor, or if the cursor argument is NULL.
75429
+*/
75430
+SQLITE_PRIVATE void sqlite3VdbeIncrWriteCounter(Vdbe *p, VdbeCursor *pC){
75431
+ if( pC==0
75432
+ || (pC->eCurType!=CURTYPE_SORTER
75433
+ && pC->eCurType!=CURTYPE_PSEUDO
75434
+ && !pC->isEphemeral)
75435
+ ){
75436
+ p->nWrite++;
75437
+ }
75438
+}
75439
+#endif
75440
+
75441
+#ifdef SQLITE_DEBUG
75442
+/*
75443
+** Assert if an Abort at this point in time might result in a corrupt
75444
+** database.
75445
+*/
75446
+SQLITE_PRIVATE void sqlite3VdbeAssertAbortable(Vdbe *p){
75447
+ assert( p->nWrite==0 || p->usesStmtJournal );
75448
+}
75449
+#endif
75450
+
7538375451
/*
7538475452
** This routine is called after all opcodes have been inserted. It loops
7538575453
** through all the opcodes and fixes up some details.
7538675454
**
7538775455
** (1) For each jump instruction with a negative P2 value (a label)
@@ -75537,10 +75605,21 @@
7553775605
assert( p->aOp[i].opcode!=OP_ResultRow );
7553875606
}
7553975607
}
7554075608
#endif
7554175609
75610
+/*
75611
+** Generate code (a single OP_Abortable opcode) that will
75612
+** verify that the VDBE program can safely call Abort in the current
75613
+** context.
75614
+*/
75615
+#if defined(SQLITE_DEBUG)
75616
+SQLITE_PRIVATE void sqlite3VdbeVerifyAbortable(Vdbe *p, int onError){
75617
+ if( onError==OE_Abort ) sqlite3VdbeAddOp0(p, OP_Abortable);
75618
+}
75619
+#endif
75620
+
7554275621
/*
7554375622
** This function returns a pointer to the array of opcodes associated with
7554475623
** the Vdbe passed as the first argument. It is the callers responsibility
7554575624
** to arrange for the returned array to be eventually freed using the
7554675625
** vdbeFreeOpArray() function.
@@ -77770,10 +77849,13 @@
7777077849
}
7777177850
#endif
7777277851
sqlite3DbFree(db, p->zErrMsg);
7777377852
p->zErrMsg = 0;
7777477853
p->pResultSet = 0;
77854
+#ifdef SQLITE_DEBUG
77855
+ p->nWrite = 0;
77856
+#endif
7777577857
7777677858
/* Save profiling information from this VDBE run.
7777777859
*/
7777877860
#ifdef VDBE_PROFILE
7777977861
{
@@ -78692,17 +78774,14 @@
7869278774
return 0;
7869378775
}else{
7869478776
i64 y;
7869578777
double s;
7869678778
if( r<-9223372036854775808.0 ) return +1;
78697
- if( r>9223372036854775807.0 ) return -1;
78779
+ if( r>=9223372036854775808.0 ) return -1;
7869878780
y = (i64)r;
7869978781
if( i<y ) return -1;
78700
- if( i>y ){
78701
- if( y==SMALLEST_INT64 && r>0.0 ) return -1;
78702
- return +1;
78703
- }
78782
+ if( i>y ) return +1;
7870478783
s = (double)i;
7870578784
if( s<r ) return -1;
7870678785
if( s>r ) return +1;
7870778786
return 0;
7870878787
}
@@ -80376,32 +80455,10 @@
8037680455
if( rc ) *piTime = 0;
8037780456
}
8037880457
return *piTime;
8037980458
}
8038080459
80381
-/*
80382
-** The following is the implementation of an SQL function that always
80383
-** fails with an error message stating that the function is used in the
80384
-** wrong context. The sqlite3_overload_function() API might construct
80385
-** SQL function that use this routine so that the functions will exist
80386
-** for name resolution but are actually overloaded by the xFindFunction
80387
-** method of virtual tables.
80388
-*/
80389
-SQLITE_PRIVATE void sqlite3InvalidFunction(
80390
- sqlite3_context *context, /* The function calling context */
80391
- int NotUsed, /* Number of arguments to the function */
80392
- sqlite3_value **NotUsed2 /* Value of each argument */
80393
-){
80394
- const char *zName = context->pFunc->zName;
80395
- char *zErr;
80396
- UNUSED_PARAMETER2(NotUsed, NotUsed2);
80397
- zErr = sqlite3_mprintf(
80398
- "unable to use function %s in the requested context", zName);
80399
- sqlite3_result_error(context, zErr, -1);
80400
- sqlite3_free(zErr);
80401
-}
80402
-
8040380460
/*
8040480461
** Create a new aggregate context for p and return a pointer to
8040580462
** its pMem->z element.
8040680463
*/
8040780464
static SQLITE_NOINLINE void *createAggContext(sqlite3_context *p, int nByte){
@@ -82773,10 +82830,13 @@
8277382830
** value in register P3 is not NULL, then this routine is a no-op.
8277482831
** The P5 parameter should be 1.
8277582832
*/
8277682833
case OP_HaltIfNull: { /* in3 */
8277782834
pIn3 = &aMem[pOp->p3];
82835
+#ifdef SQLITE_DEBUG
82836
+ if( pOp->p2==OE_Abort ){ sqlite3VdbeAssertAbortable(p); }
82837
+#endif
8277882838
if( (pIn3->flags & MEM_Null)==0 ) break;
8277982839
/* Fall through into OP_Halt */
8278082840
}
8278182841
8278282842
/* Opcode: Halt P1 P2 * P4 P5
@@ -82812,10 +82872,13 @@
8281282872
case OP_Halt: {
8281382873
VdbeFrame *pFrame;
8281482874
int pcx;
8281582875
8281682876
pcx = (int)(pOp - aOp);
82877
+#ifdef SQLITE_DEBUG
82878
+ if( pOp->p2==OE_Abort ){ sqlite3VdbeAssertAbortable(p); }
82879
+#endif
8281782880
if( pOp->p1==SQLITE_OK && p->pFrame ){
8281882881
/* Halt the sub-program. Return control to the parent frame. */
8281982882
pFrame = p->pFrame;
8282082883
p->pFrame = pFrame->pParent;
8282182884
p->nFrame--;
@@ -85182,10 +85245,12 @@
8518285245
**
8518385246
** A transaction must be started before executing this opcode.
8518485247
*/
8518585248
case OP_SetCookie: {
8518685249
Db *pDb;
85250
+
85251
+ sqlite3VdbeIncrWriteCounter(p, 0);
8518785252
assert( pOp->p2<SQLITE_N_BTREE_META );
8518885253
assert( pOp->p1>=0 && pOp->p1<db->nDb );
8518985254
assert( DbMaskTest(p->btreeMask, pOp->p1) );
8519085255
assert( p->readOnly==0 );
8519185256
pDb = &db->aDb[pOp->p1];
@@ -86145,15 +86210,12 @@
8614586210
v = 0;
8614686211
res = 0;
8614786212
pOut = out2Prerelease(p, pOp);
8614886213
assert( pOp->p1>=0 && pOp->p1<p->nCursor );
8614986214
pC = p->apCsr[pOp->p1];
86150
- if( !pC->isTable ){
86151
- rc = SQLITE_CORRUPT_BKPT;
86152
- goto abort_due_to_error;
86153
- }
8615486215
assert( pC!=0 );
86216
+ assert( pC->isTable );
8615586217
assert( pC->eCurType==CURTYPE_BTREE );
8615686218
assert( pC->uc.pCursor!=0 );
8615786219
{
8615886220
/* The next rowid or record number (different terms for the same
8615986221
** thing) is obtained in a two-step algorithm.
@@ -86318,10 +86380,11 @@
8631886380
assert( pC->eCurType==CURTYPE_BTREE );
8631986381
assert( pC->uc.pCursor!=0 );
8632086382
assert( (pOp->p5 & OPFLAG_ISNOOP) || pC->isTable );
8632186383
assert( pOp->p4type==P4_TABLE || pOp->p4type>=P4_STATIC );
8632286384
REGISTER_TRACE(pOp->p2, pData);
86385
+ sqlite3VdbeIncrWriteCounter(p, pC);
8632386386
8632486387
if( pOp->opcode==OP_Insert ){
8632586388
pKey = &aMem[pOp->p3];
8632686389
assert( pKey->flags & MEM_Int );
8632786390
assert( memIsValid(pKey) );
@@ -86432,10 +86495,11 @@
8643286495
pC = p->apCsr[pOp->p1];
8643386496
assert( pC!=0 );
8643486497
assert( pC->eCurType==CURTYPE_BTREE );
8643586498
assert( pC->uc.pCursor!=0 );
8643686499
assert( pC->deferredMoveto==0 );
86500
+ sqlite3VdbeIncrWriteCounter(p, pC);
8643786501
8643886502
#ifdef SQLITE_DEBUG
8643986503
if( pOp->p4type==P4_TABLE && HasRowid(pOp->p4.pTab) && pOp->p5==0 ){
8644086504
/* If p5 is zero, the seek operation that positioned the cursor prior to
8644186505
** OP_Delete will have also set the pC->movetoTarget field to the rowid of
@@ -87050,10 +87114,11 @@
8705087114
VdbeCursor *pC;
8705187115
BtreePayload x;
8705287116
8705387117
assert( pOp->p1>=0 && pOp->p1<p->nCursor );
8705487118
pC = p->apCsr[pOp->p1];
87119
+ sqlite3VdbeIncrWriteCounter(p, pC);
8705587120
assert( pC!=0 );
8705687121
assert( isSorter(pC)==(pOp->opcode==OP_SorterInsert) );
8705787122
pIn2 = &aMem[pOp->p2];
8705887123
assert( pIn2->flags & MEM_Blob );
8705987124
if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
@@ -87096,10 +87161,11 @@
8709687161
assert( pOp->p2>0 && pOp->p2+pOp->p3<=(p->nMem+1 - p->nCursor)+1 );
8709787162
assert( pOp->p1>=0 && pOp->p1<p->nCursor );
8709887163
pC = p->apCsr[pOp->p1];
8709987164
assert( pC!=0 );
8710087165
assert( pC->eCurType==CURTYPE_BTREE );
87166
+ sqlite3VdbeIncrWriteCounter(p, pC);
8710187167
pCrsr = pC->uc.pCursor;
8710287168
assert( pCrsr!=0 );
8710387169
assert( pOp->p5==0 );
8710487170
r.pKeyInfo = pC->pKeyInfo;
8710587171
r.nField = (u16)pOp->p3;
@@ -87318,10 +87384,11 @@
8731887384
*/
8731987385
case OP_Destroy: { /* out2 */
8732087386
int iMoved;
8732187387
int iDb;
8732287388
87389
+ sqlite3VdbeIncrWriteCounter(p, 0);
8732387390
assert( p->readOnly==0 );
8732487391
assert( pOp->p1>1 );
8732587392
pOut = out2Prerelease(p, pOp);
8732687393
pOut->flags = MEM_Null;
8732787394
if( db->nVdbeRead > db->nVDestroy+1 ){
@@ -87367,10 +87434,11 @@
8736787434
** See also: Destroy
8736887435
*/
8736987436
case OP_Clear: {
8737087437
int nChange;
8737187438
87439
+ sqlite3VdbeIncrWriteCounter(p, 0);
8737287440
nChange = 0;
8737387441
assert( p->readOnly==0 );
8737487442
assert( DbMaskTest(p->btreeMask, pOp->p2) );
8737587443
rc = sqlite3BtreeClearTable(
8737687444
db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &nChange : 0)
@@ -87423,10 +87491,11 @@
8742387491
*/
8742487492
case OP_CreateBtree: { /* out2 */
8742587493
int pgno;
8742687494
Db *pDb;
8742787495
87496
+ sqlite3VdbeIncrWriteCounter(p, 0);
8742887497
pOut = out2Prerelease(p, pOp);
8742987498
pgno = 0;
8743087499
assert( pOp->p3==BTREE_INTKEY || pOp->p3==BTREE_BLOBKEY );
8743187500
assert( pOp->p1>=0 && pOp->p1<db->nDb );
8743287501
assert( DbMaskTest(p->btreeMask, pOp->p1) );
@@ -87442,10 +87511,11 @@
8744287511
/* Opcode: SqlExec * * * P4 *
8744387512
**
8744487513
** Run the SQL statement or statements specified in the P4 string.
8744587514
*/
8744687515
case OP_SqlExec: {
87516
+ sqlite3VdbeIncrWriteCounter(p, 0);
8744787517
db->nSqlExec++;
8744887518
rc = sqlite3_exec(db, pOp->p4.z, 0, 0, 0);
8744987519
db->nSqlExec--;
8745087520
if( rc ) goto abort_due_to_error;
8745187521
break;
@@ -87531,10 +87601,11 @@
8753187601
** is dropped from disk (using the Destroy opcode) in order to keep
8753287602
** the internal representation of the
8753387603
** schema consistent with what is on disk.
8753487604
*/
8753587605
case OP_DropTable: {
87606
+ sqlite3VdbeIncrWriteCounter(p, 0);
8753687607
sqlite3UnlinkAndDeleteTable(db, pOp->p1, pOp->p4.z);
8753787608
break;
8753887609
}
8753987610
8754087611
/* Opcode: DropIndex P1 * * P4 *
@@ -87544,10 +87615,11 @@
8754487615
** is dropped from disk (using the Destroy opcode)
8754587616
** in order to keep the internal representation of the
8754687617
** schema consistent with what is on disk.
8754787618
*/
8754887619
case OP_DropIndex: {
87620
+ sqlite3VdbeIncrWriteCounter(p, 0);
8754987621
sqlite3UnlinkAndDeleteIndex(db, pOp->p1, pOp->p4.z);
8755087622
break;
8755187623
}
8755287624
8755387625
/* Opcode: DropTrigger P1 * * P4 *
@@ -87557,10 +87629,11 @@
8755787629
** is dropped from disk (using the Destroy opcode) in order to keep
8755887630
** the internal representation of the
8755987631
** schema consistent with what is on disk.
8756087632
*/
8756187633
case OP_DropTrigger: {
87634
+ sqlite3VdbeIncrWriteCounter(p, 0);
8756287635
sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p4.z);
8756387636
break;
8756487637
}
8756587638
8756687639
@@ -88603,11 +88676,11 @@
8860388676
** the current row of the virtual-table of cursor P1.
8860488677
**
8860588678
** If the VColumn opcode is being used to fetch the value of
8860688679
** an unchanging column during an UPDATE operation, then the P5
8860788680
** value is 1. Otherwise, P5 is 0. The P5 value is returned
88608
-** by sqlite3_vtab_nochange() routine can can be used
88681
+** by sqlite3_vtab_nochange() routine and can be used
8860988682
** by virtual table implementations to return special "no-change"
8861088683
** marks which can be more efficient, depending on the virtual table.
8861188684
*/
8861288685
case OP_VColumn: {
8861388686
sqlite3_vtab *pVtab;
@@ -88766,10 +88839,11 @@
8876688839
8876788840
assert( pOp->p2==1 || pOp->p5==OE_Fail || pOp->p5==OE_Rollback
8876888841
|| pOp->p5==OE_Abort || pOp->p5==OE_Ignore || pOp->p5==OE_Replace
8876988842
);
8877088843
assert( p->readOnly==0 );
88844
+ sqlite3VdbeIncrWriteCounter(p, 0);
8877188845
pVtab = pOp->p4.pVtab->pVtab;
8877288846
if( pVtab==0 || NEVER(pVtab->pModule==0) ){
8877388847
rc = SQLITE_LOCKED;
8877488848
goto abort_due_to_error;
8877588849
}
@@ -89082,10 +89156,26 @@
8908289156
pOp->p4.pExpr, aMem);
8908389157
}
8908489158
break;
8908589159
}
8908689160
#endif /* SQLITE_ENABLE_CURSOR_HINTS */
89161
+
89162
+#ifdef SQLITE_DEBUG
89163
+/* Opcode: Abortable * * * * *
89164
+**
89165
+** Verify that an Abort can happen. Assert if an Abort at this point
89166
+** might cause database corruption. This opcode only appears in debugging
89167
+** builds.
89168
+**
89169
+** An Abort is safe if either there have been no writes, or if there is
89170
+** an active statement journal.
89171
+*/
89172
+case OP_Abortable: {
89173
+ sqlite3VdbeAssertAbortable(p);
89174
+ break;
89175
+}
89176
+#endif
8908789177
8908889178
/* Opcode: Noop * * * * *
8908989179
**
8909089180
** Do nothing. This instruction is often useful as a jump
8909189181
** destination.
@@ -89094,12 +89184,13 @@
8909489184
** The magic Explain opcode are only inserted when explain==2 (which
8909589185
** is to say when the EXPLAIN QUERY PLAN syntax is used.)
8909689186
** This opcode records information from the optimizer. It is the
8909789187
** the same as a no-op. This opcodesnever appears in a real VM program.
8909889188
*/
89099
-default: { /* This is really OP_Noop and OP_Explain */
89189
+default: { /* This is really OP_Noop, OP_Explain */
8910089190
assert( pOp->opcode==OP_Noop || pOp->opcode==OP_Explain );
89191
+
8910189192
break;
8910289193
}
8910389194
8910489195
/*****************************************************************************
8910589196
** The cases of the switch statement above this line should all be indented
@@ -98641,11 +98732,11 @@
9864198732
}
9864298733
case TK_SPAN:
9864398734
case TK_COLLATE:
9864498735
case TK_UPLUS: {
9864598736
pExpr = pExpr->pLeft;
98646
- goto expr_code_doover;
98737
+ goto expr_code_doover; /* 2018-04-28: Prevent deep recursion. OSSFuzz. */
9864798738
}
9864898739
9864998740
case TK_TRIGGER: {
9865098741
/* If the opcode is TK_TRIGGER, then the expression is a reference
9865198742
** to a column in the new.* or old.* pseudo-tables available to
@@ -106664,10 +106755,11 @@
106664106755
/* Open the table. Loop through all rows of the table, inserting index
106665106756
** records into the sorter. */
106666106757
sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead);
106667106758
addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iTab, 0); VdbeCoverage(v);
106668106759
regRecord = sqlite3GetTempReg(pParse);
106760
+ sqlite3MultiWrite(pParse);
106669106761
106670106762
sqlite3GenerateIndexKey(pParse,pIndex,iTab,regRecord,0,&iPartIdxLabel,0,0);
106671106763
sqlite3VdbeAddOp2(v, OP_SorterInsert, iSorter, regRecord);
106672106764
sqlite3ResolvePartIdxLabel(pParse, iPartIdxLabel);
106673106765
sqlite3VdbeAddOp2(v, OP_Next, iTab, addr1+1); VdbeCoverage(v);
@@ -106677,16 +106769,17 @@
106677106769
(char *)pKey, P4_KEYINFO);
106678106770
sqlite3VdbeChangeP5(v, OPFLAG_BULKCSR|((memRootPage>=0)?OPFLAG_P2ISREG:0));
106679106771
106680106772
addr1 = sqlite3VdbeAddOp2(v, OP_SorterSort, iSorter, 0); VdbeCoverage(v);
106681106773
if( IsUniqueIndex(pIndex) ){
106682
- int j2 = sqlite3VdbeCurrentAddr(v) + 3;
106683
- sqlite3VdbeGoto(v, j2);
106774
+ int j2 = sqlite3VdbeGoto(v, 1);
106684106775
addr2 = sqlite3VdbeCurrentAddr(v);
106776
+ sqlite3VdbeVerifyAbortable(v, OE_Abort);
106685106777
sqlite3VdbeAddOp4Int(v, OP_SorterCompare, iSorter, j2, regRecord,
106686106778
pIndex->nKeyCol); VdbeCoverage(v);
106687106779
sqlite3UniqueConstraint(pParse, OE_Abort, pIndex);
106780
+ sqlite3VdbeJumpHere(v, j2);
106688106781
}else{
106689106782
addr2 = sqlite3VdbeCurrentAddr(v);
106690106783
}
106691106784
sqlite3VdbeAddOp3(v, OP_SorterData, iSorter, regRecord, iIdx);
106692106785
sqlite3VdbeAddOp1(v, OP_SeekEnd, iIdx);
@@ -108723,14 +108816,16 @@
108723108816
** new entry to the hash table and return it.
108724108817
*/
108725108818
if( createFlag && bestScore<FUNC_PERFECT_MATCH &&
108726108819
(pBest = sqlite3DbMallocZero(db, sizeof(*pBest)+nName+1))!=0 ){
108727108820
FuncDef *pOther;
108821
+ u8 *z;
108728108822
pBest->zName = (const char*)&pBest[1];
108729108823
pBest->nArg = (u16)nArg;
108730108824
pBest->funcFlags = enc;
108731108825
memcpy((char*)&pBest[1], zName, nName+1);
108826
+ for(z=(u8*)pBest->zName; *z; z++) *z = sqlite3UpperToLower[*z];
108732108827
pOther = (FuncDef*)sqlite3HashInsert(&db->aFunc, pBest->zName, pBest);
108733108828
if( pOther==pBest ){
108734108829
sqlite3DbFree(db, pBest);
108735108830
sqlite3OomFault(db);
108736108831
return 0;
@@ -109359,17 +109454,20 @@
109359109454
/* Delete the row */
109360109455
#ifndef SQLITE_OMIT_VIRTUALTABLE
109361109456
if( IsVirtual(pTab) ){
109362109457
const char *pVTab = (const char *)sqlite3GetVTable(db, pTab);
109363109458
sqlite3VtabMakeWritable(pParse, pTab);
109364
- sqlite3VdbeAddOp4(v, OP_VUpdate, 0, 1, iKey, pVTab, P4_VTAB);
109365
- sqlite3VdbeChangeP5(v, OE_Abort);
109366109459
assert( eOnePass==ONEPASS_OFF || eOnePass==ONEPASS_SINGLE );
109367109460
sqlite3MayAbort(pParse);
109368
- if( eOnePass==ONEPASS_SINGLE && sqlite3IsToplevel(pParse) ){
109369
- pParse->isMultiWrite = 0;
109461
+ if( eOnePass==ONEPASS_SINGLE ){
109462
+ sqlite3VdbeAddOp1(v, OP_Close, iTabCur);
109463
+ if( sqlite3IsToplevel(pParse) ){
109464
+ pParse->isMultiWrite = 0;
109465
+ }
109370109466
}
109467
+ sqlite3VdbeAddOp4(v, OP_VUpdate, 0, 1, iKey, pVTab, P4_VTAB);
109468
+ sqlite3VdbeChangeP5(v, OE_Abort);
109371109469
}else
109372109470
#endif
109373109471
{
109374109472
int count = (pParse->nested==0); /* True to count changes */
109375109473
sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur,
@@ -112000,10 +112098,16 @@
112000112098
int i; /* Iterator variable */
112001112099
Vdbe *v = sqlite3GetVdbe(pParse); /* Vdbe to add code to */
112002112100
int iCur = pParse->nTab - 1; /* Cursor number to use */
112003112101
int iOk = sqlite3VdbeMakeLabel(v); /* jump here if parent key found */
112004112102
112103
+ sqlite3VdbeVerifyAbortable(v,
112104
+ (!pFKey->isDeferred
112105
+ && !(pParse->db->flags & SQLITE_DeferFKs)
112106
+ && !pParse->pToplevel
112107
+ && !pParse->isMultiWrite) ? OE_Abort : OE_Ignore);
112108
+
112005112109
/* If nIncr is less than zero, then check at runtime if there are any
112006112110
** outstanding constraints to resolve. If there are not, there is no need
112007112111
** to check if deleting this row resolves any outstanding violations.
112008112112
**
112009112113
** Check if any of the key columns in the child table row are NULL. If
@@ -112407,10 +112511,11 @@
112407112511
** If the SQLITE_DeferFKs flag is set, then this is not required, as
112408112512
** the statement transaction will not be rolled back even if FK
112409112513
** constraints are violated.
112410112514
*/
112411112515
if( (db->flags & SQLITE_DeferFKs)==0 ){
112516
+ sqlite3VdbeVerifyAbortable(v, OE_Abort);
112412112517
sqlite3VdbeAddOp2(v, OP_FkIfZero, 0, sqlite3VdbeCurrentAddr(v)+2);
112413112518
VdbeCoverage(v);
112414112519
sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_FOREIGNKEY,
112415112520
OE_Abort, 0, P4_STATIC, P5_ConstraintFK);
112416112521
}
@@ -113319,15 +113424,30 @@
113319113424
Parse *pParse, /* Parsing context */
113320113425
int iDb, /* Index of the database holding pTab */
113321113426
Table *pTab /* The table we are writing to */
113322113427
){
113323113428
int memId = 0; /* Register holding maximum rowid */
113429
+ assert( pParse->db->aDb[iDb].pSchema!=0 );
113324113430
if( (pTab->tabFlags & TF_Autoincrement)!=0
113325113431
&& (pParse->db->mDbFlags & DBFLAG_Vacuum)==0
113326113432
){
113327113433
Parse *pToplevel = sqlite3ParseToplevel(pParse);
113328113434
AutoincInfo *pInfo;
113435
+ Table *pSeqTab = pParse->db->aDb[iDb].pSchema->pSeqTab;
113436
+
113437
+ /* Verify that the sqlite_sequence table exists and is an ordinary
113438
+ ** rowid table with exactly two columns.
113439
+ ** Ticket d8dc2b3a58cd5dc2918a1d4acb 2018-05-23 */
113440
+ if( pSeqTab==0
113441
+ || !HasRowid(pSeqTab)
113442
+ || IsVirtual(pSeqTab)
113443
+ || pSeqTab->nCol!=2
113444
+ ){
113445
+ pParse->nErr++;
113446
+ pParse->rc = SQLITE_CORRUPT_SEQUENCE;
113447
+ return 0;
113448
+ }
113329113449
113330113450
pInfo = pToplevel->pAinc;
113331113451
while( pInfo && pInfo->pTab!=pTab ){ pInfo = pInfo->pNext; }
113332113452
if( pInfo==0 ){
113333113453
pInfo = sqlite3DbMallocRawNN(pParse->db, sizeof(*pInfo));
@@ -114498,10 +114618,11 @@
114498114618
for(i=0; i<pCheck->nExpr; i++){
114499114619
int allOk;
114500114620
Expr *pExpr = pCheck->a[i].pExpr;
114501114621
if( aiChng && checkConstraintUnchanged(pExpr, aiChng, pkChng) ) continue;
114502114622
allOk = sqlite3VdbeMakeLabel(v);
114623
+ sqlite3VdbeVerifyAbortable(v, onError);
114503114624
sqlite3ExprIfTrue(pParse, pExpr, allOk, SQLITE_JUMPIFNULL);
114504114625
if( onError==OE_Ignore ){
114505114626
sqlite3VdbeGoto(v, ignoreDest);
114506114627
}else{
114507114628
char *zName = pCheck->a[i].zName;
@@ -114607,10 +114728,11 @@
114607114728
}
114608114729
114609114730
/* Check to see if the new rowid already exists in the table. Skip
114610114731
** the following conflict logic if it does not. */
114611114732
VdbeNoopComment((v, "uniqueness check for ROWID"));
114733
+ sqlite3VdbeVerifyAbortable(v, onError);
114612114734
sqlite3VdbeAddOp3(v, OP_NotExists, iDataCur, addrRowidOk, regNewData);
114613114735
VdbeCoverage(v);
114614114736
114615114737
switch( onError ){
114616114738
default: {
@@ -114819,10 +114941,11 @@
114819114941
continue;
114820114942
}
114821114943
114822114944
/* Check to see if the new index entry will be unique */
114823114945
sqlite3ExprCachePush(pParse);
114946
+ sqlite3VdbeVerifyAbortable(v, onError);
114824114947
sqlite3VdbeAddOp4Int(v, OP_NoConflict, iThisCur, addrUniqueOk,
114825114948
regIdx, pIdx->nKeyCol); VdbeCoverage(v);
114826114949
114827114950
/* Generate code to handle collisions */
114828114951
regR = (pIdx==pPk) ? regIdx : sqlite3GetTempRange(pParse, nPkField);
@@ -114905,13 +115028,15 @@
114905115028
break;
114906115029
}
114907115030
default: {
114908115031
Trigger *pTrigger = 0;
114909115032
assert( onError==OE_Replace );
114910
- sqlite3MultiWrite(pParse);
114911115033
if( db->flags&SQLITE_RecTriggers ){
114912115034
pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0);
115035
+ }
115036
+ if( pTrigger || sqlite3FkRequired(pParse, pTab, 0, 0) ){
115037
+ sqlite3MultiWrite(pParse);
114913115038
}
114914115039
sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur,
114915115040
regR, nPkField, 0, OE_Replace,
114916115041
(pIdx==pPk ? ONEPASS_SINGLE : ONEPASS_OFF), iThisCur);
114917115042
seenReplace = 1;
@@ -115428,10 +115553,11 @@
115428115553
u8 insFlags;
115429115554
sqlite3OpenTable(pParse, iSrc, iDbSrc, pSrc, OP_OpenRead);
115430115555
emptySrcTest = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0); VdbeCoverage(v);
115431115556
if( pDest->iPKey>=0 ){
115432115557
addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid);
115558
+ sqlite3VdbeVerifyAbortable(v, onError);
115433115559
addr2 = sqlite3VdbeAddOp3(v, OP_NotExists, iDest, 0, regRowid);
115434115560
VdbeCoverage(v);
115435115561
sqlite3RowidConstraint(pParse, onError, pDest);
115436115562
sqlite3VdbeJumpHere(v, addr2);
115437115563
autoIncStep(pParse, regAutoinc, regRowid);
@@ -129709,11 +129835,11 @@
129709129835
int regArg; /* First register in VUpdate arg array */
129710129836
int regRec; /* Register in which to assemble record */
129711129837
int regRowid; /* Register for ephem table rowid */
129712129838
int iCsr = pSrc->a[0].iCursor; /* Cursor used for virtual table scan */
129713129839
int aDummy[2]; /* Unused arg for sqlite3WhereOkOnePass() */
129714
- int bOnePass; /* True to use onepass strategy */
129840
+ int eOnePass; /* True to use onepass strategy */
129715129841
int addr; /* Address of OP_OpenEphemeral */
129716129842
129717129843
/* Allocate nArg registers in which to gather the arguments for VUpdate. Then
129718129844
** create and open the ephemeral table in which the records created from
129719129845
** these arguments will be temporarily stored. */
@@ -129754,16 +129880,20 @@
129754129880
iPk = pPk->aiColumn[0];
129755129881
sqlite3VdbeAddOp3(v, OP_VColumn, iCsr, iPk, regArg);
129756129882
sqlite3VdbeAddOp2(v, OP_SCopy, regArg+2+iPk, regArg+1);
129757129883
}
129758129884
129759
- bOnePass = sqlite3WhereOkOnePass(pWInfo, aDummy);
129885
+ eOnePass = sqlite3WhereOkOnePass(pWInfo, aDummy);
129760129886
129761
- if( bOnePass ){
129887
+ /* There is no ONEPASS_MULTI on virtual tables */
129888
+ assert( eOnePass==ONEPASS_OFF || eOnePass==ONEPASS_SINGLE );
129889
+
129890
+ if( eOnePass ){
129762129891
/* If using the onepass strategy, no-op out the OP_OpenEphemeral coded
129763129892
** above. */
129764129893
sqlite3VdbeChangeToNoop(v, addr);
129894
+ sqlite3VdbeAddOp1(v, OP_Close, iCsr);
129765129895
}else{
129766129896
/* Create a record from the argument register contents and insert it into
129767129897
** the ephemeral table. */
129768129898
sqlite3MultiWrite(pParse);
129769129899
sqlite3VdbeAddOp3(v, OP_MakeRecord, regArg, nArg, regRec);
@@ -129775,11 +129905,11 @@
129775129905
sqlite3VdbeAddOp2(v, OP_NewRowid, ephemTab, regRowid);
129776129906
sqlite3VdbeAddOp3(v, OP_Insert, ephemTab, regRec, regRowid);
129777129907
}
129778129908
129779129909
129780
- if( bOnePass==0 ){
129910
+ if( eOnePass==ONEPASS_OFF ){
129781129911
/* End the virtual table scan */
129782129912
sqlite3WhereEnd(pWInfo);
129783129913
129784129914
/* Begin scannning through the ephemeral table. */
129785129915
addr = sqlite3VdbeAddOp1(v, OP_Rewind, ephemTab); VdbeCoverage(v);
@@ -129795,11 +129925,11 @@
129795129925
sqlite3VdbeChangeP5(v, onError==OE_Default ? OE_Abort : onError);
129796129926
sqlite3MayAbort(pParse);
129797129927
129798129928
/* End of the ephemeral table scan. Or, if using the onepass strategy,
129799129929
** jump to here if the scan visited zero rows. */
129800
- if( bOnePass==0 ){
129930
+ if( eOnePass==ONEPASS_OFF ){
129801129931
sqlite3VdbeAddOp2(v, OP_Next, ephemTab, addr+1); VdbeCoverage(v);
129802129932
sqlite3VdbeJumpHere(v, addr);
129803129933
sqlite3VdbeAddOp2(v, OP_Close, ephemTab, 0);
129804129934
}else{
129805129935
sqlite3WhereEnd(pWInfo);
@@ -130038,10 +130168,11 @@
130038130168
k = sqlite3ColumnOfIndex(pIdx, pPk->aiColumn[i]);
130039130169
sqlite3VdbeAddOp3(v, OP_Column, iCur, k, iPk+i);
130040130170
VdbeComment((v, "%s.%s", pIdx->zName,
130041130171
pTab->aCol[pPk->aiColumn[i]].zName));
130042130172
}
130173
+ sqlite3VdbeVerifyAbortable(v, OE_Abort);
130043130174
i = sqlite3VdbeAddOp4Int(v, OP_Found, iDataCur, 0, iPk, nPk);
130044130175
VdbeCoverage(v);
130045130176
sqlite3VdbeAddOp4(v, OP_Halt, SQLITE_CORRUPT, OE_Abort, 0,
130046130177
"corrupt database", P4_STATIC);
130047130178
sqlite3VdbeJumpHere(v, i);
@@ -131490,13 +131621,10 @@
131490131621
sqlite3_module *pMod;
131491131622
void (*xSFunc)(sqlite3_context*,int,sqlite3_value**) = 0;
131492131623
void *pArg = 0;
131493131624
FuncDef *pNew;
131494131625
int rc = 0;
131495
- char *zLowerName;
131496
- unsigned char *z;
131497
-
131498131626
131499131627
/* Check to see the left operand is a column in a virtual table */
131500131628
if( NEVER(pExpr==0) ) return pDef;
131501131629
if( pExpr->op!=TK_COLUMN ) return pDef;
131502131630
pTab = pExpr->pTab;
@@ -131507,20 +131635,26 @@
131507131635
assert( pVtab->pModule!=0 );
131508131636
pMod = (sqlite3_module *)pVtab->pModule;
131509131637
if( pMod->xFindFunction==0 ) return pDef;
131510131638
131511131639
/* Call the xFindFunction method on the virtual table implementation
131512
- ** to see if the implementation wants to overload this function
131640
+ ** to see if the implementation wants to overload this function.
131641
+ **
131642
+ ** Though undocumented, we have historically always invoked xFindFunction
131643
+ ** with an all lower-case function name. Continue in this tradition to
131644
+ ** avoid any chance of an incompatibility.
131513131645
*/
131514
- zLowerName = sqlite3DbStrDup(db, pDef->zName);
131515
- if( zLowerName ){
131516
- for(z=(unsigned char*)zLowerName; *z; z++){
131517
- *z = sqlite3UpperToLower[*z];
131518
- }
131519
- rc = pMod->xFindFunction(pVtab, nArg, zLowerName, &xSFunc, &pArg);
131520
- sqlite3DbFree(db, zLowerName);
131521
- }
131646
+#ifdef SQLITE_DEBUG
131647
+ {
131648
+ int i;
131649
+ for(i=0; pDef->zName[i]; i++){
131650
+ unsigned char x = (unsigned char)pDef->zName[i];
131651
+ assert( x==sqlite3UpperToLower[x] );
131652
+ }
131653
+ }
131654
+#endif
131655
+ rc = pMod->xFindFunction(pVtab, nArg, pDef->zName, &xSFunc, &pArg);
131522131656
if( rc==0 ){
131523131657
return pDef;
131524131658
}
131525131659
131526131660
/* Create a new ephemeral function definition for the overloaded
@@ -138881,18 +139015,20 @@
138881139015
pNew->nLTerm = 1;
138882139016
pNew->aLTerm[0] = pTerm;
138883139017
/* TUNING: One-time cost for computing the automatic index is
138884139018
** estimated to be X*N*log2(N) where N is the number of rows in
138885139019
** the table being indexed and where X is 7 (LogEst=28) for normal
138886
- ** tables or 1.375 (LogEst=4) for views and subqueries. The value
139020
+ ** tables or 0.5 (LogEst=-10) for views and subqueries. The value
138887139021
** of X is smaller for views and subqueries so that the query planner
138888139022
** will be more aggressive about generating automatic indexes for
138889139023
** those objects, since there is no opportunity to add schema
138890139024
** indexes on subqueries and views. */
138891
- pNew->rSetup = rLogSize + rSize + 4;
139025
+ pNew->rSetup = rLogSize + rSize;
138892139026
if( pTab->pSelect==0 && (pTab->tabFlags & TF_Ephemeral)==0 ){
138893
- pNew->rSetup += 24;
139027
+ pNew->rSetup += 28;
139028
+ }else{
139029
+ pNew->rSetup -= 10;
138894139030
}
138895139031
ApplyCostMultiplier(pNew->rSetup, pTab->costMult);
138896139032
if( pNew->rSetup<0 ) pNew->rSetup = 0;
138897139033
/* TUNING: Each index lookup yields 20 rows in the table. This
138898139034
** is more than the usual guess of 10 rows, since we have no way
@@ -140024,16 +140160,19 @@
140024140160
Bitmask maskNew; /* Mask of src visited by (..) */
140025140161
Bitmask revMask = 0; /* Mask of rev-order loops for (..) */
140026140162
140027140163
if( (pWLoop->prereq & ~pFrom->maskLoop)!=0 ) continue;
140028140164
if( (pWLoop->maskSelf & pFrom->maskLoop)!=0 ) continue;
140029
- if( (pWLoop->wsFlags & WHERE_AUTO_INDEX)!=0 && pFrom->nRow<10 ){
140165
+ if( (pWLoop->wsFlags & WHERE_AUTO_INDEX)!=0 && pFrom->nRow<3 ){
140030140166
/* Do not use an automatic index if the this loop is expected
140031
- ** to run less than 2 times. */
140167
+ ** to run less than 1.25 times. It is tempting to also exclude
140168
+ ** automatic index usage on an outer loop, but sometimes an automatic
140169
+ ** index is useful in the outer loop of a correlated subquery. */
140032140170
assert( 10==sqlite3LogEst(2) );
140033140171
continue;
140034140172
}
140173
+
140035140174
/* At this point, pWLoop is a candidate to be the next loop.
140036140175
** Compute its cost */
140037140176
rUnsorted = sqlite3LogEstAdd(pWLoop->rSetup,pWLoop->rRun + pFrom->nRow);
140038140177
rUnsorted = sqlite3LogEstAdd(rUnsorted, pFrom->rUnsorted);
140039140178
nOut = pFrom->nRow + pWLoop->nOut;
@@ -148446,23 +148585,25 @@
148446148585
return SQLITE_MISUSE_BKPT;
148447148586
}
148448148587
#endif
148449148588
sqlite3_mutex_enter(db->mutex);
148450148589
if( xDestroy ){
148451
- pArg = (FuncDestructor *)sqlite3DbMallocZero(db, sizeof(FuncDestructor));
148590
+ pArg = (FuncDestructor *)sqlite3Malloc(sizeof(FuncDestructor));
148452148591
if( !pArg ){
148592
+ sqlite3OomFault(db);
148453148593
xDestroy(p);
148454148594
goto out;
148455148595
}
148596
+ pArg->nRef = 0;
148456148597
pArg->xDestroy = xDestroy;
148457148598
pArg->pUserData = p;
148458148599
}
148459148600
rc = sqlite3CreateFunc(db, zFunc, nArg, enc, p, xSFunc, xStep, xFinal, pArg);
148460148601
if( pArg && pArg->nRef==0 ){
148461148602
assert( rc!=SQLITE_OK );
148462148603
xDestroy(p);
148463
- sqlite3DbFree(db, pArg);
148604
+ sqlite3_free(pArg);
148464148605
}
148465148606
148466148607
out:
148467148608
rc = sqlite3ApiExit(db, rc);
148468148609
sqlite3_mutex_leave(db->mutex);
@@ -148495,10 +148636,32 @@
148495148636
sqlite3_mutex_leave(db->mutex);
148496148637
return rc;
148497148638
}
148498148639
#endif
148499148640
148641
+
148642
+/*
148643
+** The following is the implementation of an SQL function that always
148644
+** fails with an error message stating that the function is used in the
148645
+** wrong context. The sqlite3_overload_function() API might construct
148646
+** SQL function that use this routine so that the functions will exist
148647
+** for name resolution but are actually overloaded by the xFindFunction
148648
+** method of virtual tables.
148649
+*/
148650
+static void sqlite3InvalidFunction(
148651
+ sqlite3_context *context, /* The function calling context */
148652
+ int NotUsed, /* Number of arguments to the function */
148653
+ sqlite3_value **NotUsed2 /* Value of each argument */
148654
+){
148655
+ const char *zName = (const char*)sqlite3_user_data(context);
148656
+ char *zErr;
148657
+ UNUSED_PARAMETER2(NotUsed, NotUsed2);
148658
+ zErr = sqlite3_mprintf(
148659
+ "unable to use function %s in the requested context", zName);
148660
+ sqlite3_result_error(context, zErr, -1);
148661
+ sqlite3_free(zErr);
148662
+}
148500148663
148501148664
/*
148502148665
** Declare that a function has been overloaded by a virtual table.
148503148666
**
148504148667
** If the function already exists as a regular global function, then
@@ -148513,25 +148676,26 @@
148513148676
SQLITE_API int sqlite3_overload_function(
148514148677
sqlite3 *db,
148515148678
const char *zName,
148516148679
int nArg
148517148680
){
148518
- int rc = SQLITE_OK;
148681
+ int rc;
148682
+ char *zCopy;
148519148683
148520148684
#ifdef SQLITE_ENABLE_API_ARMOR
148521148685
if( !sqlite3SafetyCheckOk(db) || zName==0 || nArg<-2 ){
148522148686
return SQLITE_MISUSE_BKPT;
148523148687
}
148524148688
#endif
148525148689
sqlite3_mutex_enter(db->mutex);
148526
- if( sqlite3FindFunction(db, zName, nArg, SQLITE_UTF8, 0)==0 ){
148527
- rc = sqlite3CreateFunc(db, zName, nArg, SQLITE_UTF8,
148528
- 0, sqlite3InvalidFunction, 0, 0, 0);
148529
- }
148530
- rc = sqlite3ApiExit(db, rc);
148690
+ rc = sqlite3FindFunction(db, zName, nArg, SQLITE_UTF8, 0)!=0;
148531148691
sqlite3_mutex_leave(db->mutex);
148532
- return rc;
148692
+ if( rc ) return SQLITE_OK;
148693
+ zCopy = sqlite3_mprintf(zName);
148694
+ if( zCopy==0 ) return SQLITE_NOMEM;
148695
+ return sqlite3_create_function_v2(db, zName, nArg, SQLITE_UTF8,
148696
+ zCopy, sqlite3InvalidFunction, 0, 0, sqlite3_free);
148533148697
}
148534148698
148535148699
#ifndef SQLITE_OMIT_TRACE
148536148700
/*
148537148701
** Register a trace function. The pArg from the previously registered trace
@@ -170371,18 +170535,19 @@
170371170535
** in the table name is replaced with the user-supplied name of the r-tree
170372170536
** table.
170373170537
**
170374170538
** CREATE TABLE %_node(nodeno INTEGER PRIMARY KEY, data BLOB)
170375170539
** CREATE TABLE %_parent(nodeno INTEGER PRIMARY KEY, parentnode INTEGER)
170376
-** CREATE TABLE %_rowid(rowid INTEGER PRIMARY KEY, nodeno INTEGER)
170540
+** CREATE TABLE %_rowid(rowid INTEGER PRIMARY KEY, nodeno INTEGER, ...)
170377170541
**
170378170542
** The data for each node of the r-tree structure is stored in the %_node
170379170543
** table. For each node that is not the root node of the r-tree, there is
170380170544
** an entry in the %_parent table associating the node with its parent.
170381170545
** And for each row of data in the table, there is an entry in the %_rowid
170382170546
** table that maps from the entries rowid to the id of the node that it
170383
-** is stored on.
170547
+** is stored on. If the r-tree contains auxiliary columns, those are stored
170548
+** on the end of the %_rowid table.
170384170549
**
170385170550
** The root node of an r-tree always exists, even if the r-tree table is
170386170551
** empty. The nodeno of the root node is always 1. All other nodes in the
170387170552
** table must be the same size as the root node. The content of each node
170388170553
** is formatted as follows:
@@ -170440,10 +170605,13 @@
170440170605
typedef union RtreeCoord RtreeCoord;
170441170606
typedef struct RtreeSearchPoint RtreeSearchPoint;
170442170607
170443170608
/* The rtree may have between 1 and RTREE_MAX_DIMENSIONS dimensions. */
170444170609
#define RTREE_MAX_DIMENSIONS 5
170610
+
170611
+/* Maximum number of auxiliary columns */
170612
+#define RTREE_MAX_AUX_COLUMN 100
170445170613
170446170614
/* Size of hash table Rtree.aHash. This hash table is not expected to
170447170615
** ever contain very many entries, so a fixed number of buckets is
170448170616
** used.
170449170617
*/
@@ -170469,16 +170637,19 @@
170469170637
u8 nDim; /* Number of dimensions */
170470170638
u8 nDim2; /* Twice the number of dimensions */
170471170639
u8 eCoordType; /* RTREE_COORD_REAL32 or RTREE_COORD_INT32 */
170472170640
u8 nBytesPerCell; /* Bytes consumed per cell */
170473170641
u8 inWrTrans; /* True if inside write transaction */
170642
+ u8 nAux; /* # of auxiliary columns in %_rowid */
170474170643
int iDepth; /* Current depth of the r-tree structure */
170475170644
char *zDb; /* Name of database containing r-tree table */
170476170645
char *zName; /* Name of r-tree table */
170477170646
u32 nBusy; /* Current number of users of this structure */
170478170647
i64 nRowEst; /* Estimated number of rows in this table */
170479170648
u32 nCursor; /* Number of open cursors */
170649
+ u32 nNodeRef; /* Number RtreeNodes with positive nRef */
170650
+ char *zReadAuxSql; /* SQL for statement to read aux data */
170480170651
170481170652
/* List of nodes removed during a CondenseTree operation. List is
170482170653
** linked together via the pointer normally used for hash chains -
170483170654
** RtreeNode.pNext. RtreeNode.iNode stores the depth of the sub-tree
170484170655
** headed by the node (leaf nodes have RtreeNode.iNode==0).
@@ -170500,10 +170671,13 @@
170500170671
170501170672
/* Statements to read/write/delete a record from xxx_parent */
170502170673
sqlite3_stmt *pReadParent;
170503170674
sqlite3_stmt *pWriteParent;
170504170675
sqlite3_stmt *pDeleteParent;
170676
+
170677
+ /* Statement for writing to the "aux:" fields, if there are any */
170678
+ sqlite3_stmt *pWriteAux;
170505170679
170506170680
RtreeNode *aHash[HASHSIZE]; /* Hash table of in-memory nodes. */
170507170681
};
170508170682
170509170683
/* Possible values for Rtree.eCoordType: */
@@ -170577,17 +170751,19 @@
170577170751
*/
170578170752
struct RtreeCursor {
170579170753
sqlite3_vtab_cursor base; /* Base class. Must be first */
170580170754
u8 atEOF; /* True if at end of search */
170581170755
u8 bPoint; /* True if sPoint is valid */
170756
+ u8 bAuxValid; /* True if pReadAux is valid */
170582170757
int iStrategy; /* Copy of idxNum search parameter */
170583170758
int nConstraint; /* Number of entries in aConstraint */
170584170759
RtreeConstraint *aConstraint; /* Search constraints. */
170585170760
int nPointAlloc; /* Number of slots allocated for aPoint[] */
170586170761
int nPoint; /* Number of slots used in aPoint[] */
170587170762
int mxLevel; /* iLevel value for root of the tree */
170588170763
RtreeSearchPoint *aPoint; /* Priority queue for search points */
170764
+ sqlite3_stmt *pReadAux; /* Statement to read aux-data */
170589170765
RtreeSearchPoint sPoint; /* Cached next search point */
170590170766
RtreeNode *aNode[RTREE_CACHE_SZ]; /* Rtree node cache */
170591170767
u32 anQueue[RTREE_MAX_DEPTH+1]; /* Number of queued entries by iLevel */
170592170768
};
170593170769
@@ -170870,10 +171046,11 @@
170870171046
/*
170871171047
** Increment the reference count of node p.
170872171048
*/
170873171049
static void nodeReference(RtreeNode *p){
170874171050
if( p ){
171051
+ assert( p->nRef>0 );
170875171052
p->nRef++;
170876171053
}
170877171054
}
170878171055
170879171056
/*
@@ -170937,10 +171114,11 @@
170937171114
pNode = (RtreeNode *)sqlite3_malloc(sizeof(RtreeNode) + pRtree->iNodeSize);
170938171115
if( pNode ){
170939171116
memset(pNode, 0, sizeof(RtreeNode) + pRtree->iNodeSize);
170940171117
pNode->zData = (u8 *)&pNode[1];
170941171118
pNode->nRef = 1;
171119
+ pRtree->nNodeRef++;
170942171120
pNode->pParent = pParent;
170943171121
pNode->isDirty = 1;
170944171122
nodeReference(pParent);
170945171123
}
170946171124
return pNode;
@@ -170970,14 +171148,14 @@
170970171148
RtreeNode *pNode = 0;
170971171149
170972171150
/* Check if the requested node is already in the hash table. If so,
170973171151
** increase its reference count and return it.
170974171152
*/
170975
- if( (pNode = nodeHashLookup(pRtree, iNode)) ){
171153
+ if( (pNode = nodeHashLookup(pRtree, iNode))!=0 ){
170976171154
assert( !pParent || !pNode->pParent || pNode->pParent==pParent );
170977171155
if( pParent && !pNode->pParent ){
170978
- nodeReference(pParent);
171156
+ pParent->nRef++;
170979171157
pNode->pParent = pParent;
170980171158
}
170981171159
pNode->nRef++;
170982171160
*ppNode = pNode;
170983171161
return SQLITE_OK;
@@ -171012,10 +171190,11 @@
171012171190
rc = SQLITE_NOMEM;
171013171191
}else{
171014171192
pNode->pParent = pParent;
171015171193
pNode->zData = (u8 *)&pNode[1];
171016171194
pNode->nRef = 1;
171195
+ pRtree->nNodeRef++;
171017171196
pNode->iNode = iNode;
171018171197
pNode->isDirty = 0;
171019171198
pNode->pNext = 0;
171020171199
rc = sqlite3_blob_read(pRtree->pNodeBlob, pNode->zData,
171021171200
pRtree->iNodeSize, 0);
@@ -171052,11 +171231,14 @@
171052171231
}else{
171053171232
rc = SQLITE_CORRUPT_VTAB;
171054171233
}
171055171234
*ppNode = pNode;
171056171235
}else{
171057
- sqlite3_free(pNode);
171236
+ if( pNode ){
171237
+ pRtree->nNodeRef--;
171238
+ sqlite3_free(pNode);
171239
+ }
171058171240
*ppNode = 0;
171059171241
}
171060171242
171061171243
return rc;
171062171244
}
@@ -171149,12 +171331,14 @@
171149171331
*/
171150171332
static int nodeRelease(Rtree *pRtree, RtreeNode *pNode){
171151171333
int rc = SQLITE_OK;
171152171334
if( pNode ){
171153171335
assert( pNode->nRef>0 );
171336
+ assert( pRtree->nNodeRef>0 );
171154171337
pNode->nRef--;
171155171338
if( pNode->nRef==0 ){
171339
+ pRtree->nNodeRef--;
171156171340
if( pNode->iNode==1 ){
171157171341
pRtree->iDepth = -1;
171158171342
}
171159171343
if( pNode->pParent ){
171160171344
rc = nodeRelease(pRtree, pNode->pParent);
@@ -171267,20 +171451,23 @@
171267171451
*/
171268171452
static void rtreeRelease(Rtree *pRtree){
171269171453
pRtree->nBusy--;
171270171454
if( pRtree->nBusy==0 ){
171271171455
pRtree->inWrTrans = 0;
171272
- pRtree->nCursor = 0;
171456
+ assert( pRtree->nCursor==0 );
171273171457
nodeBlobReset(pRtree);
171458
+ assert( pRtree->nNodeRef==0 );
171274171459
sqlite3_finalize(pRtree->pWriteNode);
171275171460
sqlite3_finalize(pRtree->pDeleteNode);
171276171461
sqlite3_finalize(pRtree->pReadRowid);
171277171462
sqlite3_finalize(pRtree->pWriteRowid);
171278171463
sqlite3_finalize(pRtree->pDeleteRowid);
171279171464
sqlite3_finalize(pRtree->pReadParent);
171280171465
sqlite3_finalize(pRtree->pWriteParent);
171281171466
sqlite3_finalize(pRtree->pDeleteParent);
171467
+ sqlite3_finalize(pRtree->pWriteAux);
171468
+ sqlite3_free(pRtree->zReadAuxSql);
171282171469
sqlite3_free(pRtree);
171283171470
}
171284171471
}
171285171472
171286171473
/*
@@ -171365,10 +171552,11 @@
171365171552
Rtree *pRtree = (Rtree *)(cur->pVtab);
171366171553
int ii;
171367171554
RtreeCursor *pCsr = (RtreeCursor *)cur;
171368171555
assert( pRtree->nCursor>0 );
171369171556
freeCursorConstraints(pCsr);
171557
+ sqlite3_finalize(pCsr->pReadAux);
171370171558
sqlite3_free(pCsr->aPoint);
171371171559
for(ii=0; ii<RTREE_CACHE_SZ; ii++) nodeRelease(pRtree, pCsr->aNode[ii]);
171372171560
sqlite3_free(pCsr);
171373171561
pRtree->nCursor--;
171374171562
nodeBlobReset(pRtree);
@@ -171736,11 +171924,11 @@
171736171924
if( pNew==0 ) return 0;
171737171925
ii = (int)(pNew - pCur->aPoint) + 1;
171738171926
if( ii<RTREE_CACHE_SZ ){
171739171927
assert( pCur->aNode[ii]==0 );
171740171928
pCur->aNode[ii] = pCur->aNode[0];
171741
- }else{
171929
+ }else{
171742171930
nodeRelease(RTREE_OF_CURSOR(pCur), pCur->aNode[0]);
171743171931
}
171744171932
pCur->aNode[0] = 0;
171745171933
*pNew = pCur->sPoint;
171746171934
}
@@ -171907,10 +172095,14 @@
171907172095
RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor;
171908172096
int rc = SQLITE_OK;
171909172097
171910172098
/* Move to the next entry that matches the configured constraints. */
171911172099
RTREE_QUEUE_TRACE(pCsr, "POP-Nx:");
172100
+ if( pCsr->bAuxValid ){
172101
+ pCsr->bAuxValid = 0;
172102
+ sqlite3_reset(pCsr->pReadAux);
172103
+ }
171912172104
rtreeSearchPointPop(pCsr);
171913172105
rc = rtreeStepToLeaf(pCsr);
171914172106
return rc;
171915172107
}
171916172108
@@ -171941,11 +172133,11 @@
171941172133
171942172134
if( rc ) return rc;
171943172135
if( p==0 ) return SQLITE_OK;
171944172136
if( i==0 ){
171945172137
sqlite3_result_int64(ctx, nodeGetRowid(pRtree, pNode, p->iCell));
171946
- }else{
172138
+ }else if( i<=pRtree->nDim2 ){
171947172139
nodeGetCoord(pRtree, pNode, p->iCell, i-1, &c);
171948172140
#ifndef SQLITE_RTREE_INT_ONLY
171949172141
if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
171950172142
sqlite3_result_double(ctx, c.f);
171951172143
}else
@@ -171952,11 +172144,31 @@
171952172144
#endif
171953172145
{
171954172146
assert( pRtree->eCoordType==RTREE_COORD_INT32 );
171955172147
sqlite3_result_int(ctx, c.i);
171956172148
}
171957
- }
172149
+ }else{
172150
+ if( !pCsr->bAuxValid ){
172151
+ if( pCsr->pReadAux==0 ){
172152
+ rc = sqlite3_prepare_v3(pRtree->db, pRtree->zReadAuxSql, -1, 0,
172153
+ &pCsr->pReadAux, 0);
172154
+ if( rc ) return rc;
172155
+ }
172156
+ sqlite3_bind_int64(pCsr->pReadAux, 1,
172157
+ nodeGetRowid(pRtree, pNode, p->iCell));
172158
+ rc = sqlite3_step(pCsr->pReadAux);
172159
+ if( rc==SQLITE_ROW ){
172160
+ pCsr->bAuxValid = 1;
172161
+ }else{
172162
+ sqlite3_reset(pCsr->pReadAux);
172163
+ if( rc==SQLITE_DONE ) rc = SQLITE_OK;
172164
+ return rc;
172165
+ }
172166
+ }
172167
+ sqlite3_result_value(ctx,
172168
+ sqlite3_column_value(pCsr->pReadAux, i - pRtree->nDim2 + 1));
172169
+ }
171958172170
return SQLITE_OK;
171959172171
}
171960172172
171961172173
/*
171962172174
** Use nodeAcquire() to obtain the leaf node containing the record with
@@ -172030,18 +172242,21 @@
172030172242
RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor;
172031172243
RtreeNode *pRoot = 0;
172032172244
int ii;
172033172245
int rc = SQLITE_OK;
172034172246
int iCell = 0;
172247
+ sqlite3_stmt *pStmt;
172035172248
172036172249
rtreeReference(pRtree);
172037172250
172038172251
/* Reset the cursor to the same state as rtreeOpen() leaves it in. */
172039172252
freeCursorConstraints(pCsr);
172040172253
sqlite3_free(pCsr->aPoint);
172254
+ pStmt = pCsr->pReadAux;
172041172255
memset(pCsr, 0, sizeof(RtreeCursor));
172042172256
pCsr->base.pVtab = (sqlite3_vtab*)pRtree;
172257
+ pCsr->pReadAux = pStmt;
172043172258
172044172259
pCsr->iStrategy = idxNum;
172045172260
if( idxNum==1 ){
172046172261
/* Special case - lookup by rowid. */
172047172262
RtreeNode *pLeaf; /* Leaf on which the required cell resides */
@@ -172200,14 +172415,18 @@
172200172415
** sqlite uses an internal cost of 0.0). It is expected to return
172201172416
** a single row.
172202172417
*/
172203172418
pIdxInfo->estimatedCost = 30.0;
172204172419
pIdxInfo->estimatedRows = 1;
172420
+ pIdxInfo->idxFlags = SQLITE_INDEX_SCAN_UNIQUE;
172205172421
return SQLITE_OK;
172206172422
}
172207172423
172208
- if( p->usable && (p->iColumn>0 || p->op==SQLITE_INDEX_CONSTRAINT_MATCH) ){
172424
+ if( p->usable
172425
+ && ((p->iColumn>0 && p->iColumn<=pRtree->nDim2)
172426
+ || p->op==SQLITE_INDEX_CONSTRAINT_MATCH)
172427
+ ){
172209172428
u8 op;
172210172429
switch( p->op ){
172211172430
case SQLITE_INDEX_CONSTRAINT_EQ: op = RTREE_EQ; break;
172212172431
case SQLITE_INDEX_CONSTRAINT_GT: op = RTREE_GT; break;
172213172432
case SQLITE_INDEX_CONSTRAINT_LE: op = RTREE_LE; break;
@@ -172776,11 +172995,11 @@
172776172995
pNode->isDirty = 1;
172777172996
writeInt16(pNode->zData, pRtree->iDepth);
172778172997
}else{
172779172998
pLeft = pNode;
172780172999
pRight = nodeNew(pRtree, pLeft->pParent);
172781
- nodeReference(pLeft);
173000
+ pLeft->nRef++;
172782173001
}
172783173002
172784173003
if( !pLeft || !pRight ){
172785173004
rc = SQLITE_NOMEM;
172786173005
goto splitnode_out;
@@ -173266,10 +173485,11 @@
173266173485
for(pLeaf=pRtree->pDeleted; pLeaf; pLeaf=pRtree->pDeleted){
173267173486
if( rc==SQLITE_OK ){
173268173487
rc = reinsertNodeContent(pRtree, pLeaf);
173269173488
}
173270173489
pRtree->pDeleted = pLeaf->pNext;
173490
+ pRtree->nNodeRef--;
173271173491
sqlite3_free(pLeaf);
173272173492
}
173273173493
173274173494
/* Release the reference to the root node. */
173275173495
if( rc==SQLITE_OK ){
@@ -173362,18 +173582,24 @@
173362173582
** The xUpdate method for rtree module virtual tables.
173363173583
*/
173364173584
static int rtreeUpdate(
173365173585
sqlite3_vtab *pVtab,
173366173586
int nData,
173367
- sqlite3_value **azData,
173587
+ sqlite3_value **aData,
173368173588
sqlite_int64 *pRowid
173369173589
){
173370173590
Rtree *pRtree = (Rtree *)pVtab;
173371173591
int rc = SQLITE_OK;
173372173592
RtreeCell cell; /* New cell to insert if nData>1 */
173373173593
int bHaveRowid = 0; /* Set to 1 after new rowid is determined */
173374173594
173595
+ if( pRtree->nNodeRef ){
173596
+ /* Unable to write to the btree while another cursor is reading from it,
173597
+ ** since the write might do a rebalance which would disrupt the read
173598
+ ** cursor. */
173599
+ return SQLITE_LOCKED_VTAB;
173600
+ }
173375173601
rtreeReference(pRtree);
173376173602
assert(nData>=1);
173377173603
173378173604
cell.iRowid = 0; /* Used only to suppress a compiler warning */
173379173605
@@ -173388,50 +173614,51 @@
173388173614
** case, SQLITE_CONSTRAINT must be returned regardless of the
173389173615
** conflict-handling mode specified by the user.
173390173616
*/
173391173617
if( nData>1 ){
173392173618
int ii;
173619
+ int nn = nData - 4;
173393173620
173394
- /* Populate the cell.aCoord[] array. The first coordinate is azData[3].
173621
+ if( nn > pRtree->nDim2 ) nn = pRtree->nDim2;
173622
+ /* Populate the cell.aCoord[] array. The first coordinate is aData[3].
173395173623
**
173396173624
** NB: nData can only be less than nDim*2+3 if the rtree is mis-declared
173397173625
** with "column" that are interpreted as table constraints.
173398173626
** Example: CREATE VIRTUAL TABLE bad USING rtree(x,y,CHECK(y>5));
173399173627
** This problem was discovered after years of use, so we silently ignore
173400173628
** these kinds of misdeclared tables to avoid breaking any legacy.
173401173629
*/
173402
- assert( nData<=(pRtree->nDim2 + 3) );
173403173630
173404173631
#ifndef SQLITE_RTREE_INT_ONLY
173405173632
if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
173406
- for(ii=0; ii<nData-4; ii+=2){
173407
- cell.aCoord[ii].f = rtreeValueDown(azData[ii+3]);
173408
- cell.aCoord[ii+1].f = rtreeValueUp(azData[ii+4]);
173633
+ for(ii=0; ii<nn; ii+=2){
173634
+ cell.aCoord[ii].f = rtreeValueDown(aData[ii+3]);
173635
+ cell.aCoord[ii+1].f = rtreeValueUp(aData[ii+4]);
173409173636
if( cell.aCoord[ii].f>cell.aCoord[ii+1].f ){
173410173637
rc = rtreeConstraintError(pRtree, ii+1);
173411173638
goto constraint;
173412173639
}
173413173640
}
173414173641
}else
173415173642
#endif
173416173643
{
173417
- for(ii=0; ii<nData-4; ii+=2){
173418
- cell.aCoord[ii].i = sqlite3_value_int(azData[ii+3]);
173419
- cell.aCoord[ii+1].i = sqlite3_value_int(azData[ii+4]);
173644
+ for(ii=0; ii<nn; ii+=2){
173645
+ cell.aCoord[ii].i = sqlite3_value_int(aData[ii+3]);
173646
+ cell.aCoord[ii+1].i = sqlite3_value_int(aData[ii+4]);
173420173647
if( cell.aCoord[ii].i>cell.aCoord[ii+1].i ){
173421173648
rc = rtreeConstraintError(pRtree, ii+1);
173422173649
goto constraint;
173423173650
}
173424173651
}
173425173652
}
173426173653
173427173654
/* If a rowid value was supplied, check if it is already present in
173428173655
** the table. If so, the constraint has failed. */
173429
- if( sqlite3_value_type(azData[2])!=SQLITE_NULL ){
173430
- cell.iRowid = sqlite3_value_int64(azData[2]);
173431
- if( sqlite3_value_type(azData[0])==SQLITE_NULL
173432
- || sqlite3_value_int64(azData[0])!=cell.iRowid
173656
+ if( sqlite3_value_type(aData[2])!=SQLITE_NULL ){
173657
+ cell.iRowid = sqlite3_value_int64(aData[2]);
173658
+ if( sqlite3_value_type(aData[0])==SQLITE_NULL
173659
+ || sqlite3_value_int64(aData[0])!=cell.iRowid
173433173660
){
173434173661
int steprc;
173435173662
sqlite3_bind_int64(pRtree->pReadRowid, 1, cell.iRowid);
173436173663
steprc = sqlite3_step(pRtree->pReadRowid);
173437173664
rc = sqlite3_reset(pRtree->pReadRowid);
@@ -173446,20 +173673,20 @@
173446173673
}
173447173674
bHaveRowid = 1;
173448173675
}
173449173676
}
173450173677
173451
- /* If azData[0] is not an SQL NULL value, it is the rowid of a
173678
+ /* If aData[0] is not an SQL NULL value, it is the rowid of a
173452173679
** record to delete from the r-tree table. The following block does
173453173680
** just that.
173454173681
*/
173455
- if( sqlite3_value_type(azData[0])!=SQLITE_NULL ){
173456
- rc = rtreeDeleteRowid(pRtree, sqlite3_value_int64(azData[0]));
173682
+ if( sqlite3_value_type(aData[0])!=SQLITE_NULL ){
173683
+ rc = rtreeDeleteRowid(pRtree, sqlite3_value_int64(aData[0]));
173457173684
}
173458173685
173459
- /* If the azData[] array contains more than one element, elements
173460
- ** (azData[2]..azData[argc-1]) contain a new record to insert into
173686
+ /* If the aData[] array contains more than one element, elements
173687
+ ** (aData[2]..aData[argc-1]) contain a new record to insert into
173461173688
** the r-tree structure.
173462173689
*/
173463173690
if( rc==SQLITE_OK && nData>1 ){
173464173691
/* Insert the new record into the r-tree */
173465173692
RtreeNode *pLeaf = 0;
@@ -173480,10 +173707,20 @@
173480173707
rc2 = nodeRelease(pRtree, pLeaf);
173481173708
if( rc==SQLITE_OK ){
173482173709
rc = rc2;
173483173710
}
173484173711
}
173712
+ if( pRtree->nAux ){
173713
+ sqlite3_stmt *pUp = pRtree->pWriteAux;
173714
+ int jj;
173715
+ sqlite3_bind_int64(pUp, 1, *pRowid);
173716
+ for(jj=0; jj<pRtree->nAux; jj++){
173717
+ sqlite3_bind_value(pUp, jj+2, aData[pRtree->nDim2+3+jj]);
173718
+ }
173719
+ sqlite3_step(pUp);
173720
+ rc = sqlite3_reset(pUp);
173721
+ }
173485173722
}
173486173723
173487173724
constraint:
173488173725
rtreeRelease(pRtree);
173489173726
return rc;
@@ -173636,37 +173873,48 @@
173636173873
int rc = SQLITE_OK;
173637173874
173638173875
#define N_STATEMENT 8
173639173876
static const char *azSql[N_STATEMENT] = {
173640173877
/* Write the xxx_node table */
173641
- "INSERT OR REPLACE INTO '%q'.'%q_node' VALUES(:1, :2)",
173642
- "DELETE FROM '%q'.'%q_node' WHERE nodeno = :1",
173878
+ "INSERT OR REPLACE INTO '%q'.'%q_node' VALUES(?1, ?2)",
173879
+ "DELETE FROM '%q'.'%q_node' WHERE nodeno = ?1",
173643173880
173644173881
/* Read and write the xxx_rowid table */
173645
- "SELECT nodeno FROM '%q'.'%q_rowid' WHERE rowid = :1",
173646
- "INSERT OR REPLACE INTO '%q'.'%q_rowid' VALUES(:1, :2)",
173647
- "DELETE FROM '%q'.'%q_rowid' WHERE rowid = :1",
173882
+ "SELECT nodeno FROM '%q'.'%q_rowid' WHERE rowid = ?1",
173883
+ "INSERT OR REPLACE INTO '%q'.'%q_rowid' VALUES(?1, ?2)",
173884
+ "DELETE FROM '%q'.'%q_rowid' WHERE rowid = ?1",
173648173885
173649173886
/* Read and write the xxx_parent table */
173650
- "SELECT parentnode FROM '%q'.'%q_parent' WHERE nodeno = :1",
173651
- "INSERT OR REPLACE INTO '%q'.'%q_parent' VALUES(:1, :2)",
173652
- "DELETE FROM '%q'.'%q_parent' WHERE nodeno = :1"
173887
+ "SELECT parentnode FROM '%q'.'%q_parent' WHERE nodeno = ?1",
173888
+ "INSERT OR REPLACE INTO '%q'.'%q_parent' VALUES(?1, ?2)",
173889
+ "DELETE FROM '%q'.'%q_parent' WHERE nodeno = ?1"
173653173890
};
173654173891
sqlite3_stmt **appStmt[N_STATEMENT];
173655173892
int i;
173656173893
173657173894
pRtree->db = db;
173658173895
173659173896
if( isCreate ){
173660
- char *zCreate = sqlite3_mprintf(
173661
-"CREATE TABLE \"%w\".\"%w_node\"(nodeno INTEGER PRIMARY KEY, data BLOB);"
173662
-"CREATE TABLE \"%w\".\"%w_rowid\"(rowid INTEGER PRIMARY KEY, nodeno INTEGER);"
173663
-"CREATE TABLE \"%w\".\"%w_parent\"(nodeno INTEGER PRIMARY KEY,"
173664
- " parentnode INTEGER);"
173665
-"INSERT INTO '%q'.'%q_node' VALUES(1, zeroblob(%d))",
173666
- zDb, zPrefix, zDb, zPrefix, zDb, zPrefix, zDb, zPrefix, pRtree->iNodeSize
173667
- );
173897
+ char *zCreate;
173898
+ sqlite3_str *p = sqlite3_str_new(db);
173899
+ int ii;
173900
+ sqlite3_str_appendf(p,
173901
+ "CREATE TABLE \"%w\".\"%w_rowid\"(rowid INTEGER PRIMARY KEY,nodeno",
173902
+ zDb, zPrefix);
173903
+ for(ii=0; ii<pRtree->nAux; ii++){
173904
+ sqlite3_str_appendf(p,",a%d",ii);
173905
+ }
173906
+ sqlite3_str_appendf(p,
173907
+ ");CREATE TABLE \"%w\".\"%w_node\"(nodeno INTEGER PRIMARY KEY,data);",
173908
+ zDb, zPrefix);
173909
+ sqlite3_str_appendf(p,
173910
+ "CREATE TABLE \"%w\".\"%w_parent\"(nodeno INTEGER PRIMARY KEY,parentnode);",
173911
+ zDb, zPrefix);
173912
+ sqlite3_str_appendf(p,
173913
+ "INSERT INTO \"%w\".\"%w_node\"VALUES(1,zeroblob(%d))",
173914
+ zDb, zPrefix, pRtree->iNodeSize);
173915
+ zCreate = sqlite3_str_finish(p);
173668173916
if( !zCreate ){
173669173917
return SQLITE_NOMEM;
173670173918
}
173671173919
rc = sqlite3_exec(db, zCreate, 0, 0, 0);
173672173920
sqlite3_free(zCreate);
@@ -173684,18 +173932,54 @@
173684173932
appStmt[6] = &pRtree->pWriteParent;
173685173933
appStmt[7] = &pRtree->pDeleteParent;
173686173934
173687173935
rc = rtreeQueryStat1(db, pRtree);
173688173936
for(i=0; i<N_STATEMENT && rc==SQLITE_OK; i++){
173689
- char *zSql = sqlite3_mprintf(azSql[i], zDb, zPrefix);
173937
+ char *zSql;
173938
+ const char *zFormat;
173939
+ if( i!=3 || pRtree->nAux==0 ){
173940
+ zFormat = azSql[i];
173941
+ }else {
173942
+ /* An UPSERT is very slightly slower than REPLACE, but it is needed
173943
+ ** if there are auxiliary columns */
173944
+ zFormat = "INSERT INTO\"%w\".\"%w_rowid\"(rowid,nodeno)VALUES(?1,?2)"
173945
+ "ON CONFLICT(rowid)DO UPDATE SET nodeno=excluded.nodeno";
173946
+ }
173947
+ zSql = sqlite3_mprintf(zFormat, zDb, zPrefix);
173690173948
if( zSql ){
173691173949
rc = sqlite3_prepare_v3(db, zSql, -1, SQLITE_PREPARE_PERSISTENT,
173692173950
appStmt[i], 0);
173693173951
}else{
173694173952
rc = SQLITE_NOMEM;
173695173953
}
173696173954
sqlite3_free(zSql);
173955
+ }
173956
+ if( pRtree->nAux ){
173957
+ pRtree->zReadAuxSql = sqlite3_mprintf(
173958
+ "SELECT * FROM \"%w\".\"%w_rowid\" WHERE rowid=?1",
173959
+ zDb, zPrefix);
173960
+ if( pRtree->zReadAuxSql==0 ){
173961
+ rc = SQLITE_NOMEM;
173962
+ }else{
173963
+ sqlite3_str *p = sqlite3_str_new(db);
173964
+ int ii;
173965
+ char *zSql;
173966
+ sqlite3_str_appendf(p, "UPDATE \"%w\".\"%w_rowid\"SET ", zDb, zPrefix);
173967
+ for(ii=0; ii<pRtree->nAux; ii++){
173968
+ if( ii ) sqlite3_str_append(p, ",", 1);
173969
+ sqlite3_str_appendf(p,"a%d=?%d",ii,ii+2);
173970
+ }
173971
+ sqlite3_str_appendf(p, " WHERE rowid=?1");
173972
+ zSql = sqlite3_str_finish(p);
173973
+ if( zSql==0 ){
173974
+ rc = SQLITE_NOMEM;
173975
+ }else{
173976
+ rc = sqlite3_prepare_v3(db, zSql, -1, SQLITE_PREPARE_PERSISTENT,
173977
+ &pRtree->pWriteAux, 0);
173978
+ sqlite3_free(zSql);
173979
+ }
173980
+ }
173697173981
}
173698173982
173699173983
return rc;
173700173984
}
173701173985
@@ -173795,21 +174079,26 @@
173795174079
int rc = SQLITE_OK;
173796174080
Rtree *pRtree;
173797174081
int nDb; /* Length of string argv[1] */
173798174082
int nName; /* Length of string argv[2] */
173799174083
int eCoordType = (pAux ? RTREE_COORD_INT32 : RTREE_COORD_REAL32);
174084
+ sqlite3_str *pSql;
174085
+ char *zSql;
174086
+ int ii = 4;
174087
+ int iErr;
173800174088
173801174089
const char *aErrMsg[] = {
173802174090
0, /* 0 */
173803174091
"Wrong number of columns for an rtree table", /* 1 */
173804174092
"Too few columns for an rtree table", /* 2 */
173805
- "Too many columns for an rtree table" /* 3 */
174093
+ "Too many columns for an rtree table", /* 3 */
174094
+ "Auxiliary rtree columns must be last" /* 4 */
173806174095
};
173807174096
173808
- int iErr = (argc<6) ? 2 : argc>(RTREE_MAX_DIMENSIONS*2+4) ? 3 : argc%2;
173809
- if( aErrMsg[iErr] ){
173810
- *pzErr = sqlite3_mprintf("%s", aErrMsg[iErr]);
174097
+ assert( RTREE_MAX_AUX_COLUMN<256 ); /* Aux columns counted by a u8 */
174098
+ if( argc>RTREE_MAX_AUX_COLUMN+3 ){
174099
+ *pzErr = sqlite3_mprintf("%s", aErrMsg[3]);
173811174100
return SQLITE_ERROR;
173812174101
}
173813174102
173814174103
sqlite3_vtab_config(db, SQLITE_VTAB_CONSTRAINT_SUPPORT, 1);
173815174104
@@ -173823,57 +174112,77 @@
173823174112
memset(pRtree, 0, sizeof(Rtree)+nDb+nName+2);
173824174113
pRtree->nBusy = 1;
173825174114
pRtree->base.pModule = &rtreeModule;
173826174115
pRtree->zDb = (char *)&pRtree[1];
173827174116
pRtree->zName = &pRtree->zDb[nDb+1];
173828
- pRtree->nDim = (u8)((argc-4)/2);
173829
- pRtree->nDim2 = pRtree->nDim*2;
173830
- pRtree->nBytesPerCell = 8 + pRtree->nDim2*4;
173831174117
pRtree->eCoordType = (u8)eCoordType;
173832174118
memcpy(pRtree->zDb, argv[1], nDb);
173833174119
memcpy(pRtree->zName, argv[2], nName);
173834174120
173835
- /* Figure out the node size to use. */
173836
- rc = getNodeSize(db, pRtree, isCreate, pzErr);
173837174121
173838174122
/* Create/Connect to the underlying relational database schema. If
173839174123
** that is successful, call sqlite3_declare_vtab() to configure
173840174124
** the r-tree table schema.
173841174125
*/
173842
- if( rc==SQLITE_OK ){
173843
- if( (rc = rtreeSqlInit(pRtree, db, argv[1], argv[2], isCreate)) ){
173844
- *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
173845
- }else{
173846
- char *zSql = sqlite3_mprintf("CREATE TABLE x(%s", argv[3]);
173847
- char *zTmp;
173848
- int ii;
173849
- for(ii=4; zSql && ii<argc; ii++){
173850
- zTmp = zSql;
173851
- zSql = sqlite3_mprintf("%s, %s", zTmp, argv[ii]);
173852
- sqlite3_free(zTmp);
173853
- }
173854
- if( zSql ){
173855
- zTmp = zSql;
173856
- zSql = sqlite3_mprintf("%s);", zTmp);
173857
- sqlite3_free(zTmp);
173858
- }
173859
- if( !zSql ){
173860
- rc = SQLITE_NOMEM;
173861
- }else if( SQLITE_OK!=(rc = sqlite3_declare_vtab(db, zSql)) ){
173862
- *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
173863
- }
173864
- sqlite3_free(zSql);
173865
- }
173866
- }
173867
-
173868
- if( rc==SQLITE_OK ){
173869
- *ppVtab = (sqlite3_vtab *)pRtree;
173870
- }else{
173871
- assert( *ppVtab==0 );
173872
- assert( pRtree->nBusy==1 );
173873
- rtreeRelease(pRtree);
173874
- }
174126
+ pSql = sqlite3_str_new(db);
174127
+ sqlite3_str_appendf(pSql, "CREATE TABLE x(%s", argv[3]);
174128
+ for(ii=4; ii<argc; ii++){
174129
+ if( argv[ii][0]=='+' ){
174130
+ pRtree->nAux++;
174131
+ sqlite3_str_appendf(pSql, ",%s", argv[ii]+1);
174132
+ }else if( pRtree->nAux>0 ){
174133
+ break;
174134
+ }else{
174135
+ pRtree->nDim2++;
174136
+ sqlite3_str_appendf(pSql, ",%s", argv[ii]);
174137
+ }
174138
+ }
174139
+ sqlite3_str_appendf(pSql, ");");
174140
+ zSql = sqlite3_str_finish(pSql);
174141
+ if( !zSql ){
174142
+ rc = SQLITE_NOMEM;
174143
+ }else if( ii<argc ){
174144
+ *pzErr = sqlite3_mprintf("%s", aErrMsg[4]);
174145
+ rc = SQLITE_ERROR;
174146
+ }else if( SQLITE_OK!=(rc = sqlite3_declare_vtab(db, zSql)) ){
174147
+ *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
174148
+ }
174149
+ sqlite3_free(zSql);
174150
+ if( rc ) goto rtreeInit_fail;
174151
+ pRtree->nDim = pRtree->nDim2/2;
174152
+ if( pRtree->nDim<1 ){
174153
+ iErr = 2;
174154
+ }else if( pRtree->nDim2>RTREE_MAX_DIMENSIONS*2 ){
174155
+ iErr = 3;
174156
+ }else if( pRtree->nDim2 % 2 ){
174157
+ iErr = 1;
174158
+ }else{
174159
+ iErr = 0;
174160
+ }
174161
+ if( iErr ){
174162
+ *pzErr = sqlite3_mprintf("%s", aErrMsg[iErr]);
174163
+ goto rtreeInit_fail;
174164
+ }
174165
+ pRtree->nBytesPerCell = 8 + pRtree->nDim2*4;
174166
+
174167
+ /* Figure out the node size to use. */
174168
+ rc = getNodeSize(db, pRtree, isCreate, pzErr);
174169
+ if( rc ) goto rtreeInit_fail;
174170
+ rc = rtreeSqlInit(pRtree, db, argv[1], argv[2], isCreate);
174171
+ if( rc ){
174172
+ *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
174173
+ goto rtreeInit_fail;
174174
+ }
174175
+
174176
+ *ppVtab = (sqlite3_vtab *)pRtree;
174177
+ return SQLITE_OK;
174178
+
174179
+rtreeInit_fail:
174180
+ if( rc==SQLITE_OK ) rc = SQLITE_ERROR;
174181
+ assert( *ppVtab==0 );
174182
+ assert( pRtree->nBusy==1 );
174183
+ rtreeRelease(pRtree);
173875174184
return rc;
173876174185
}
173877174186
173878174187
173879174188
/*
@@ -174098,11 +174407,11 @@
174098174407
** This function is used to check that the %_parent (if bLeaf==0) or %_rowid
174099174408
** (if bLeaf==1) table contains a specified entry. The schemas of the
174100174409
** two tables are:
174101174410
**
174102174411
** CREATE TABLE %_parent(nodeno INTEGER PRIMARY KEY, parentnode INTEGER)
174103
-** CREATE TABLE %_rowid(rowid INTEGER PRIMARY KEY, nodeno INTEGER)
174412
+** CREATE TABLE %_rowid(rowid INTEGER PRIMARY KEY, nodeno INTEGER, ...)
174104174413
**
174105174414
** In both cases, this function checks that there exists an entry with
174106174415
** IPK value iKey and the second column set to iVal.
174107174416
**
174108174417
*/
@@ -174113,12 +174422,12 @@
174113174422
i64 iVal /* Expected value for mapping */
174114174423
){
174115174424
int rc;
174116174425
sqlite3_stmt *pStmt;
174117174426
const char *azSql[2] = {
174118
- "SELECT parentnode FROM %Q.'%q_parent' WHERE nodeno=?",
174119
- "SELECT nodeno FROM %Q.'%q_rowid' WHERE rowid=?"
174427
+ "SELECT parentnode FROM %Q.'%q_parent' WHERE nodeno=?1",
174428
+ "SELECT nodeno FROM %Q.'%q_rowid' WHERE rowid=?1"
174120174429
};
174121174430
174122174431
assert( bLeaf==0 || bLeaf==1 );
174123174432
if( pCheck->aCheckMapping[bLeaf]==0 ){
174124174433
pCheck->aCheckMapping[bLeaf] = rtreeCheckPrepare(pCheck,
@@ -174298,10 +174607,11 @@
174298174607
char **pzReport /* OUT: sqlite3_malloc'd report text */
174299174608
){
174300174609
RtreeCheck check; /* Common context for various routines */
174301174610
sqlite3_stmt *pStmt = 0; /* Used to find column count of rtree table */
174302174611
int bEnd = 0; /* True if transaction should be closed */
174612
+ int nAux = 0; /* Number of extra columns. */
174303174613
174304174614
/* Initialize the context object */
174305174615
memset(&check, 0, sizeof(check));
174306174616
check.db = db;
174307174617
check.zDb = zDb;
@@ -174312,16 +174622,26 @@
174312174622
** on a consistent snapshot. */
174313174623
if( sqlite3_get_autocommit(db) ){
174314174624
check.rc = sqlite3_exec(db, "BEGIN", 0, 0, 0);
174315174625
bEnd = 1;
174316174626
}
174627
+
174628
+ /* Find the number of auxiliary columns */
174629
+ if( check.rc==SQLITE_OK ){
174630
+ pStmt = rtreeCheckPrepare(&check, "SELECT * FROM %Q.'%q_rowid'", zDb, zTab);
174631
+ if( pStmt ){
174632
+ nAux = sqlite3_column_count(pStmt) - 2;
174633
+ sqlite3_finalize(pStmt);
174634
+ }
174635
+ check.rc = SQLITE_OK;
174636
+ }
174317174637
174318174638
/* Find number of dimensions in the rtree table. */
174319174639
pStmt = rtreeCheckPrepare(&check, "SELECT * FROM %Q.%Q", zDb, zTab);
174320174640
if( pStmt ){
174321174641
int rc;
174322
- check.nDim = (sqlite3_column_count(pStmt) - 1) / 2;
174642
+ check.nDim = (sqlite3_column_count(pStmt) - 1 - nAux) / 2;
174323174643
if( check.nDim<1 ){
174324174644
rtreeCheckAppendMsg(&check, "Schema corrupt or not an rtree");
174325174645
}else if( SQLITE_ROW==sqlite3_step(pStmt) ){
174326174646
check.bInt = (sqlite3_column_type(pStmt, 1)==SQLITE_INTEGER);
174327174647
}
@@ -189525,11 +189845,11 @@
189525189845
}else{
189526189846
jsonAppendChar(&x, '$');
189527189847
}
189528189848
if( p->eType==JSON_ARRAY ){
189529189849
jsonPrintf(30, &x, "[%d]", p->iRowid);
189530
- }else{
189850
+ }else if( p->eType==JSON_OBJECT ){
189531189851
jsonPrintf(pThis->n, &x, ".%.*s", pThis->n-2, pThis->u.zJContent+1);
189532189852
}
189533189853
}
189534189854
jsonResult(&x);
189535189855
break;
@@ -207256,11 +207576,11 @@
207256207576
int nArg, /* Number of args */
207257207577
sqlite3_value **apUnused /* Function arguments */
207258207578
){
207259207579
assert( nArg==0 );
207260207580
UNUSED_PARAM2(nArg, apUnused);
207261
- sqlite3_result_text(pCtx, "fts5: 2018-05-14 00:41:12 d0f35739af3b226c8eef39676407293650cde551acef06fe8628fdd5b59bd66a", -1, SQLITE_TRANSIENT);
207581
+ sqlite3_result_text(pCtx, "fts5: 2018-05-30 01:14:20 86ee267ee86f5264774a9f215b1158aeaa2d605e77c205731b5ee3945d7de4c2", -1, SQLITE_TRANSIENT);
207262207582
}
207263207583
207264207584
static int fts5Init(sqlite3 *db){
207265207585
static const sqlite3_module fts5Mod = {
207266207586
/* iVersion */ 2,
@@ -211526,12 +211846,12 @@
211526211846
}
211527211847
#endif /* SQLITE_CORE */
211528211848
#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_STMTVTAB) */
211529211849
211530211850
/************** End of stmt.c ************************************************/
211531
-#if __LINE__!=211531
211851
+#if __LINE__!=211851
211532211852
#undef SQLITE_SOURCE_ID
211533
-#define SQLITE_SOURCE_ID "2018-05-14 00:41:12 d0f35739af3b226c8eef39676407293650cde551acef06fe8628fdd5b59balt2"
211853
+#define SQLITE_SOURCE_ID "2018-05-30 01:14:20 86ee267ee86f5264774a9f215b1158aeaa2d605e77c205731b5ee3945d7dalt2"
211534211854
#endif
211535211855
/* Return the source-id for this library */
211536211856
SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; }
211537211857
/************************** End of sqlite3.c ******************************/
211538211858
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -1150,11 +1150,11 @@
1150 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
1151 ** [sqlite_version()] and [sqlite_source_id()].
1152 */
1153 #define SQLITE_VERSION "3.24.0"
1154 #define SQLITE_VERSION_NUMBER 3024000
1155 #define SQLITE_SOURCE_ID "2018-05-14 00:41:12 d0f35739af3b226c8eef39676407293650cde551acef06fe8628fdd5b59bd66a"
1156
1157 /*
1158 ** CAPI3REF: Run-Time Library Version Numbers
1159 ** KEYWORDS: sqlite3_version sqlite3_sourceid
1160 **
@@ -1529,17 +1529,19 @@
1529 #define SQLITE_IOERR_AUTH (SQLITE_IOERR | (28<<8))
1530 #define SQLITE_IOERR_BEGIN_ATOMIC (SQLITE_IOERR | (29<<8))
1531 #define SQLITE_IOERR_COMMIT_ATOMIC (SQLITE_IOERR | (30<<8))
1532 #define SQLITE_IOERR_ROLLBACK_ATOMIC (SQLITE_IOERR | (31<<8))
1533 #define SQLITE_LOCKED_SHAREDCACHE (SQLITE_LOCKED | (1<<8))
 
1534 #define SQLITE_BUSY_RECOVERY (SQLITE_BUSY | (1<<8))
1535 #define SQLITE_BUSY_SNAPSHOT (SQLITE_BUSY | (2<<8))
1536 #define SQLITE_CANTOPEN_NOTEMPDIR (SQLITE_CANTOPEN | (1<<8))
1537 #define SQLITE_CANTOPEN_ISDIR (SQLITE_CANTOPEN | (2<<8))
1538 #define SQLITE_CANTOPEN_FULLPATH (SQLITE_CANTOPEN | (3<<8))
1539 #define SQLITE_CANTOPEN_CONVPATH (SQLITE_CANTOPEN | (4<<8))
1540 #define SQLITE_CORRUPT_VTAB (SQLITE_CORRUPT | (1<<8))
 
1541 #define SQLITE_READONLY_RECOVERY (SQLITE_READONLY | (1<<8))
1542 #define SQLITE_READONLY_CANTLOCK (SQLITE_READONLY | (2<<8))
1543 #define SQLITE_READONLY_ROLLBACK (SQLITE_READONLY | (3<<8))
1544 #define SQLITE_READONLY_DBMOVED (SQLITE_READONLY | (4<<8))
1545 #define SQLITE_READONLY_CANTINIT (SQLITE_READONLY | (5<<8))
@@ -7317,10 +7319,14 @@
7317 sqlite3_uint64 colUsed; /* Input: Mask of columns used by statement */
7318 };
7319
7320 /*
7321 ** CAPI3REF: Virtual Table Scan Flags
 
 
 
 
7322 */
7323 #define SQLITE_INDEX_SCAN_UNIQUE 1 /* Scan visits at most 1 row */
7324
7325 /*
7326 ** CAPI3REF: Virtual Table Constraint Operator Codes
@@ -8180,15 +8186,23 @@
8180 /*
8181 ** CAPI3REF: Create A New Dynamic String Object
8182 ** CONSTRUCTOR: sqlite3_str
8183 **
8184 ** ^The [sqlite3_str_new(D)] interface allocates and initializes
8185 ** a new [sqlite3_str]
8186 ** object. ^The [sqlite3_str_new()] interface returns NULL on an out-of-memory
8187 ** condition. To avoid memory leaks, the object returned by
8188 ** [sqlite3_str_new()] must be freed by a subsequent call to
8189 ** [sqlite3_str_finish(X)].
 
 
 
 
 
 
 
 
 
 
8190 **
8191 ** The D parameter to [sqlite3_str_new(D)] may be NULL. If the
8192 ** D parameter in [sqlite3_str_new(D)] is not NULL, then the maximum
8193 ** length of the string contained in the [sqlite3_str] object will be
8194 ** the value set for [sqlite3_limit](D,[SQLITE_LIMIT_LENGTH]) instead
@@ -9550,15 +9564,15 @@
9550 **
9551 ** If the sqlite3_vtab_nochange(X) routine is called within the [xColumn]
9552 ** method of a [virtual table], then it returns true if and only if the
9553 ** column is being fetched as part of an UPDATE operation during which the
9554 ** column value will not change. Applications might use this to substitute
9555 ** a lighter-weight value to return that the corresponding [xUpdate] method
9556 ** understands as a "no-change" value.
9557 **
9558 ** If the [xColumn] method calls sqlite3_vtab_nochange() and finds that
9559 ** the column is not changed by the UPDATE statement, they the xColumn
9560 ** method can optionally return without setting a result, without calling
9561 ** any of the [sqlite3_result_int|sqlite3_result_xxxxx() interfaces].
9562 ** In that case, [sqlite3_value_nochange(X)] will return true for the
9563 ** same column in the [xUpdate] method.
9564 */
@@ -14632,10 +14646,11 @@
14632 #define OP_Function 166 /* synopsis: r[P3]=func(r[P2@P5]) */
14633 #define OP_Trace 167
14634 #define OP_CursorHint 168
14635 #define OP_Noop 169
14636 #define OP_Explain 170
 
14637
14638 /* Properties such as "out2" or "jump" that are specified in
14639 ** comments following the "case" for each opcode in the vdbe.c
14640 ** are encoded into bitvectors as follows:
14641 */
@@ -14665,11 +14680,11 @@
14665 /* 128 */ 0x04, 0x04, 0x00, 0x00, 0x10, 0x10, 0x10, 0x00,\
14666 /* 136 */ 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
14667 /* 144 */ 0x00, 0x06, 0x10, 0x00, 0x04, 0x1a, 0x00, 0x00,\
14668 /* 152 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
14669 /* 160 */ 0x00, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,\
14670 /* 168 */ 0x00, 0x00, 0x00,}
14671
14672 /* The sqlite3P2Values() routine is able to run faster if it knows
14673 ** the value of the largest JUMP opcode. The smaller the maximum
14674 ** JUMP opcode the better, so the mkopcodeh.tcl script that
14675 ** generated this include file strives to group all JUMP opcodes
@@ -14707,10 +14722,15 @@
14707 SQLITE_PRIVATE void sqlite3VdbeVerifyNoResultRow(Vdbe *p);
14708 #else
14709 # define sqlite3VdbeVerifyNoMallocRequired(A,B)
14710 # define sqlite3VdbeVerifyNoResultRow(A)
14711 #endif
 
 
 
 
 
14712 SQLITE_PRIVATE VdbeOp *sqlite3VdbeAddOpList(Vdbe*, int nOp, VdbeOpList const *aOp,int iLineno);
14713 #ifndef SQLITE_OMIT_EXPLAIN
14714 SQLITE_PRIVATE void sqlite3VdbeExplain(Parse*,u8,const char*,...);
14715 SQLITE_PRIVATE void sqlite3VdbeExplainPop(Parse*);
14716 SQLITE_PRIVATE int sqlite3VdbeExplainParent(Parse*);
@@ -18683,10 +18703,11 @@
18683 SQLITE_PRIVATE int sqlite3CreateFunc(sqlite3 *, const char *, int, int, void *,
18684 void (*)(sqlite3_context*,int,sqlite3_value **),
18685 void (*)(sqlite3_context*,int,sqlite3_value **), void (*)(sqlite3_context*),
18686 FuncDestructor *pDestructor
18687 );
 
18688 SQLITE_PRIVATE void sqlite3OomFault(sqlite3*);
18689 SQLITE_PRIVATE void sqlite3OomClear(sqlite3*);
18690 SQLITE_PRIVATE int sqlite3ApiExit(sqlite3 *db, int);
18691 SQLITE_PRIVATE int sqlite3OpenTempDatabase(Parse *);
18692
@@ -18785,11 +18806,10 @@
18785 SQLITE_PRIVATE int sqlite3VtabCallCreate(sqlite3*, int, const char *, char **);
18786 SQLITE_PRIVATE int sqlite3VtabCallConnect(Parse*, Table*);
18787 SQLITE_PRIVATE int sqlite3VtabCallDestroy(sqlite3*, int, const char *);
18788 SQLITE_PRIVATE int sqlite3VtabBegin(sqlite3 *, VTable *);
18789 SQLITE_PRIVATE FuncDef *sqlite3VtabOverloadFunction(sqlite3 *,FuncDef*, int nArg, Expr*);
18790 SQLITE_PRIVATE void sqlite3InvalidFunction(sqlite3_context*,int,sqlite3_value**);
18791 SQLITE_PRIVATE sqlite3_int64 sqlite3StmtCurrentTime(sqlite3_context*);
18792 SQLITE_PRIVATE int sqlite3VdbeParameterIndex(Vdbe*, const char*, int);
18793 SQLITE_PRIVATE int sqlite3TransferBindings(sqlite3_stmt *, sqlite3_stmt *);
18794 SQLITE_PRIVATE void sqlite3ParserReset(Parse*);
18795 SQLITE_PRIVATE int sqlite3Reprepare(Vdbe*);
@@ -19715,10 +19735,11 @@
19715 i64 startTime; /* Time when query started - used for profiling */
19716 #endif
19717 int nOp; /* Number of instructions in the program */
19718 #ifdef SQLITE_DEBUG
19719 int rcApp; /* errcode set by sqlite3_result_error_code() */
 
19720 #endif
19721 u16 nResColumn; /* Number of columns in one row of the result set */
19722 u8 errorAction; /* Recovery action to do in case of an error */
19723 u8 minWriteFileFormat; /* Minimum file format for writable database files */
19724 u8 prepFlags; /* SQLITE_PREPARE_* flags */
@@ -19849,10 +19870,18 @@
19849 SQLITE_PRIVATE int sqlite3VdbeSorterRowkey(const VdbeCursor *, Mem *);
19850 SQLITE_PRIVATE int sqlite3VdbeSorterNext(sqlite3 *, const VdbeCursor *);
19851 SQLITE_PRIVATE int sqlite3VdbeSorterRewind(const VdbeCursor *, int *);
19852 SQLITE_PRIVATE int sqlite3VdbeSorterWrite(const VdbeCursor *, Mem *);
19853 SQLITE_PRIVATE int sqlite3VdbeSorterCompare(const VdbeCursor *, Mem *, int, int *);
 
 
 
 
 
 
 
 
19854
19855 #if !defined(SQLITE_OMIT_SHARED_CACHE)
19856 SQLITE_PRIVATE void sqlite3VdbeEnter(Vdbe*);
19857 #else
19858 # define sqlite3VdbeEnter(X)
@@ -27537,16 +27566,26 @@
27537 return strAccumFinishRealloc(p);
27538 }
27539 }
27540 return p->zText;
27541 }
 
 
 
 
 
 
 
 
 
 
27542
27543 /* Finalize a string created using sqlite3_str_new().
27544 */
27545 SQLITE_API char *sqlite3_str_finish(sqlite3_str *p){
27546 char *z;
27547 if( p ){
27548 z = sqlite3StrAccumFinish(p);
27549 sqlite3_free(p);
27550 }else{
27551 z = 0;
27552 }
@@ -27611,10 +27650,12 @@
27611 SQLITE_API sqlite3_str *sqlite3_str_new(sqlite3 *db){
27612 sqlite3_str *p = sqlite3_malloc64(sizeof(*p));
27613 if( p ){
27614 sqlite3StrAccumInit(p, 0, 0, 0,
27615 db ? db->aLimit[SQLITE_LIMIT_LENGTH] : SQLITE_MAX_LENGTH);
 
 
27616 }
27617 return p;
27618 }
27619
27620 /*
@@ -31409,10 +31450,11 @@
31409 /* 166 */ "Function" OpHelp("r[P3]=func(r[P2@P5])"),
31410 /* 167 */ "Trace" OpHelp(""),
31411 /* 168 */ "CursorHint" OpHelp(""),
31412 /* 169 */ "Noop" OpHelp(""),
31413 /* 170 */ "Explain" OpHelp(""),
 
31414 };
31415 return azName[i];
31416 }
31417 #endif
31418
@@ -73732,11 +73774,11 @@
73732 pMem->flags = MEM_Int;
73733 }
73734 }
73735
73736 /* A no-op destructor */
73737 static void sqlite3NoopDestructor(void *p){ UNUSED_PARAMETER(p); }
73738
73739 /*
73740 ** Set the value stored in *pMem should already be a NULL.
73741 ** Also store a pointer to go with it.
73742 */
@@ -75378,10 +75420,36 @@
75378 return ( v->db->mallocFailed || hasAbort==mayAbort || hasFkCounter
75379 || (hasCreateTable && hasInitCoroutine) );
75380 }
75381 #endif /* SQLITE_DEBUG - the sqlite3AssertMayAbort() function */
75382
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75383 /*
75384 ** This routine is called after all opcodes have been inserted. It loops
75385 ** through all the opcodes and fixes up some details.
75386 **
75387 ** (1) For each jump instruction with a negative P2 value (a label)
@@ -75537,10 +75605,21 @@
75537 assert( p->aOp[i].opcode!=OP_ResultRow );
75538 }
75539 }
75540 #endif
75541
 
 
 
 
 
 
 
 
 
 
 
75542 /*
75543 ** This function returns a pointer to the array of opcodes associated with
75544 ** the Vdbe passed as the first argument. It is the callers responsibility
75545 ** to arrange for the returned array to be eventually freed using the
75546 ** vdbeFreeOpArray() function.
@@ -77770,10 +77849,13 @@
77770 }
77771 #endif
77772 sqlite3DbFree(db, p->zErrMsg);
77773 p->zErrMsg = 0;
77774 p->pResultSet = 0;
 
 
 
77775
77776 /* Save profiling information from this VDBE run.
77777 */
77778 #ifdef VDBE_PROFILE
77779 {
@@ -78692,17 +78774,14 @@
78692 return 0;
78693 }else{
78694 i64 y;
78695 double s;
78696 if( r<-9223372036854775808.0 ) return +1;
78697 if( r>9223372036854775807.0 ) return -1;
78698 y = (i64)r;
78699 if( i<y ) return -1;
78700 if( i>y ){
78701 if( y==SMALLEST_INT64 && r>0.0 ) return -1;
78702 return +1;
78703 }
78704 s = (double)i;
78705 if( s<r ) return -1;
78706 if( s>r ) return +1;
78707 return 0;
78708 }
@@ -80376,32 +80455,10 @@
80376 if( rc ) *piTime = 0;
80377 }
80378 return *piTime;
80379 }
80380
80381 /*
80382 ** The following is the implementation of an SQL function that always
80383 ** fails with an error message stating that the function is used in the
80384 ** wrong context. The sqlite3_overload_function() API might construct
80385 ** SQL function that use this routine so that the functions will exist
80386 ** for name resolution but are actually overloaded by the xFindFunction
80387 ** method of virtual tables.
80388 */
80389 SQLITE_PRIVATE void sqlite3InvalidFunction(
80390 sqlite3_context *context, /* The function calling context */
80391 int NotUsed, /* Number of arguments to the function */
80392 sqlite3_value **NotUsed2 /* Value of each argument */
80393 ){
80394 const char *zName = context->pFunc->zName;
80395 char *zErr;
80396 UNUSED_PARAMETER2(NotUsed, NotUsed2);
80397 zErr = sqlite3_mprintf(
80398 "unable to use function %s in the requested context", zName);
80399 sqlite3_result_error(context, zErr, -1);
80400 sqlite3_free(zErr);
80401 }
80402
80403 /*
80404 ** Create a new aggregate context for p and return a pointer to
80405 ** its pMem->z element.
80406 */
80407 static SQLITE_NOINLINE void *createAggContext(sqlite3_context *p, int nByte){
@@ -82773,10 +82830,13 @@
82773 ** value in register P3 is not NULL, then this routine is a no-op.
82774 ** The P5 parameter should be 1.
82775 */
82776 case OP_HaltIfNull: { /* in3 */
82777 pIn3 = &aMem[pOp->p3];
 
 
 
82778 if( (pIn3->flags & MEM_Null)==0 ) break;
82779 /* Fall through into OP_Halt */
82780 }
82781
82782 /* Opcode: Halt P1 P2 * P4 P5
@@ -82812,10 +82872,13 @@
82812 case OP_Halt: {
82813 VdbeFrame *pFrame;
82814 int pcx;
82815
82816 pcx = (int)(pOp - aOp);
 
 
 
82817 if( pOp->p1==SQLITE_OK && p->pFrame ){
82818 /* Halt the sub-program. Return control to the parent frame. */
82819 pFrame = p->pFrame;
82820 p->pFrame = pFrame->pParent;
82821 p->nFrame--;
@@ -85182,10 +85245,12 @@
85182 **
85183 ** A transaction must be started before executing this opcode.
85184 */
85185 case OP_SetCookie: {
85186 Db *pDb;
 
 
85187 assert( pOp->p2<SQLITE_N_BTREE_META );
85188 assert( pOp->p1>=0 && pOp->p1<db->nDb );
85189 assert( DbMaskTest(p->btreeMask, pOp->p1) );
85190 assert( p->readOnly==0 );
85191 pDb = &db->aDb[pOp->p1];
@@ -86145,15 +86210,12 @@
86145 v = 0;
86146 res = 0;
86147 pOut = out2Prerelease(p, pOp);
86148 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
86149 pC = p->apCsr[pOp->p1];
86150 if( !pC->isTable ){
86151 rc = SQLITE_CORRUPT_BKPT;
86152 goto abort_due_to_error;
86153 }
86154 assert( pC!=0 );
 
86155 assert( pC->eCurType==CURTYPE_BTREE );
86156 assert( pC->uc.pCursor!=0 );
86157 {
86158 /* The next rowid or record number (different terms for the same
86159 ** thing) is obtained in a two-step algorithm.
@@ -86318,10 +86380,11 @@
86318 assert( pC->eCurType==CURTYPE_BTREE );
86319 assert( pC->uc.pCursor!=0 );
86320 assert( (pOp->p5 & OPFLAG_ISNOOP) || pC->isTable );
86321 assert( pOp->p4type==P4_TABLE || pOp->p4type>=P4_STATIC );
86322 REGISTER_TRACE(pOp->p2, pData);
 
86323
86324 if( pOp->opcode==OP_Insert ){
86325 pKey = &aMem[pOp->p3];
86326 assert( pKey->flags & MEM_Int );
86327 assert( memIsValid(pKey) );
@@ -86432,10 +86495,11 @@
86432 pC = p->apCsr[pOp->p1];
86433 assert( pC!=0 );
86434 assert( pC->eCurType==CURTYPE_BTREE );
86435 assert( pC->uc.pCursor!=0 );
86436 assert( pC->deferredMoveto==0 );
 
86437
86438 #ifdef SQLITE_DEBUG
86439 if( pOp->p4type==P4_TABLE && HasRowid(pOp->p4.pTab) && pOp->p5==0 ){
86440 /* If p5 is zero, the seek operation that positioned the cursor prior to
86441 ** OP_Delete will have also set the pC->movetoTarget field to the rowid of
@@ -87050,10 +87114,11 @@
87050 VdbeCursor *pC;
87051 BtreePayload x;
87052
87053 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
87054 pC = p->apCsr[pOp->p1];
 
87055 assert( pC!=0 );
87056 assert( isSorter(pC)==(pOp->opcode==OP_SorterInsert) );
87057 pIn2 = &aMem[pOp->p2];
87058 assert( pIn2->flags & MEM_Blob );
87059 if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
@@ -87096,10 +87161,11 @@
87096 assert( pOp->p2>0 && pOp->p2+pOp->p3<=(p->nMem+1 - p->nCursor)+1 );
87097 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
87098 pC = p->apCsr[pOp->p1];
87099 assert( pC!=0 );
87100 assert( pC->eCurType==CURTYPE_BTREE );
 
87101 pCrsr = pC->uc.pCursor;
87102 assert( pCrsr!=0 );
87103 assert( pOp->p5==0 );
87104 r.pKeyInfo = pC->pKeyInfo;
87105 r.nField = (u16)pOp->p3;
@@ -87318,10 +87384,11 @@
87318 */
87319 case OP_Destroy: { /* out2 */
87320 int iMoved;
87321 int iDb;
87322
 
87323 assert( p->readOnly==0 );
87324 assert( pOp->p1>1 );
87325 pOut = out2Prerelease(p, pOp);
87326 pOut->flags = MEM_Null;
87327 if( db->nVdbeRead > db->nVDestroy+1 ){
@@ -87367,10 +87434,11 @@
87367 ** See also: Destroy
87368 */
87369 case OP_Clear: {
87370 int nChange;
87371
 
87372 nChange = 0;
87373 assert( p->readOnly==0 );
87374 assert( DbMaskTest(p->btreeMask, pOp->p2) );
87375 rc = sqlite3BtreeClearTable(
87376 db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &nChange : 0)
@@ -87423,10 +87491,11 @@
87423 */
87424 case OP_CreateBtree: { /* out2 */
87425 int pgno;
87426 Db *pDb;
87427
 
87428 pOut = out2Prerelease(p, pOp);
87429 pgno = 0;
87430 assert( pOp->p3==BTREE_INTKEY || pOp->p3==BTREE_BLOBKEY );
87431 assert( pOp->p1>=0 && pOp->p1<db->nDb );
87432 assert( DbMaskTest(p->btreeMask, pOp->p1) );
@@ -87442,10 +87511,11 @@
87442 /* Opcode: SqlExec * * * P4 *
87443 **
87444 ** Run the SQL statement or statements specified in the P4 string.
87445 */
87446 case OP_SqlExec: {
 
87447 db->nSqlExec++;
87448 rc = sqlite3_exec(db, pOp->p4.z, 0, 0, 0);
87449 db->nSqlExec--;
87450 if( rc ) goto abort_due_to_error;
87451 break;
@@ -87531,10 +87601,11 @@
87531 ** is dropped from disk (using the Destroy opcode) in order to keep
87532 ** the internal representation of the
87533 ** schema consistent with what is on disk.
87534 */
87535 case OP_DropTable: {
 
87536 sqlite3UnlinkAndDeleteTable(db, pOp->p1, pOp->p4.z);
87537 break;
87538 }
87539
87540 /* Opcode: DropIndex P1 * * P4 *
@@ -87544,10 +87615,11 @@
87544 ** is dropped from disk (using the Destroy opcode)
87545 ** in order to keep the internal representation of the
87546 ** schema consistent with what is on disk.
87547 */
87548 case OP_DropIndex: {
 
87549 sqlite3UnlinkAndDeleteIndex(db, pOp->p1, pOp->p4.z);
87550 break;
87551 }
87552
87553 /* Opcode: DropTrigger P1 * * P4 *
@@ -87557,10 +87629,11 @@
87557 ** is dropped from disk (using the Destroy opcode) in order to keep
87558 ** the internal representation of the
87559 ** schema consistent with what is on disk.
87560 */
87561 case OP_DropTrigger: {
 
87562 sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p4.z);
87563 break;
87564 }
87565
87566
@@ -88603,11 +88676,11 @@
88603 ** the current row of the virtual-table of cursor P1.
88604 **
88605 ** If the VColumn opcode is being used to fetch the value of
88606 ** an unchanging column during an UPDATE operation, then the P5
88607 ** value is 1. Otherwise, P5 is 0. The P5 value is returned
88608 ** by sqlite3_vtab_nochange() routine can can be used
88609 ** by virtual table implementations to return special "no-change"
88610 ** marks which can be more efficient, depending on the virtual table.
88611 */
88612 case OP_VColumn: {
88613 sqlite3_vtab *pVtab;
@@ -88766,10 +88839,11 @@
88766
88767 assert( pOp->p2==1 || pOp->p5==OE_Fail || pOp->p5==OE_Rollback
88768 || pOp->p5==OE_Abort || pOp->p5==OE_Ignore || pOp->p5==OE_Replace
88769 );
88770 assert( p->readOnly==0 );
 
88771 pVtab = pOp->p4.pVtab->pVtab;
88772 if( pVtab==0 || NEVER(pVtab->pModule==0) ){
88773 rc = SQLITE_LOCKED;
88774 goto abort_due_to_error;
88775 }
@@ -89082,10 +89156,26 @@
89082 pOp->p4.pExpr, aMem);
89083 }
89084 break;
89085 }
89086 #endif /* SQLITE_ENABLE_CURSOR_HINTS */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89087
89088 /* Opcode: Noop * * * * *
89089 **
89090 ** Do nothing. This instruction is often useful as a jump
89091 ** destination.
@@ -89094,12 +89184,13 @@
89094 ** The magic Explain opcode are only inserted when explain==2 (which
89095 ** is to say when the EXPLAIN QUERY PLAN syntax is used.)
89096 ** This opcode records information from the optimizer. It is the
89097 ** the same as a no-op. This opcodesnever appears in a real VM program.
89098 */
89099 default: { /* This is really OP_Noop and OP_Explain */
89100 assert( pOp->opcode==OP_Noop || pOp->opcode==OP_Explain );
 
89101 break;
89102 }
89103
89104 /*****************************************************************************
89105 ** The cases of the switch statement above this line should all be indented
@@ -98641,11 +98732,11 @@
98641 }
98642 case TK_SPAN:
98643 case TK_COLLATE:
98644 case TK_UPLUS: {
98645 pExpr = pExpr->pLeft;
98646 goto expr_code_doover;
98647 }
98648
98649 case TK_TRIGGER: {
98650 /* If the opcode is TK_TRIGGER, then the expression is a reference
98651 ** to a column in the new.* or old.* pseudo-tables available to
@@ -106664,10 +106755,11 @@
106664 /* Open the table. Loop through all rows of the table, inserting index
106665 ** records into the sorter. */
106666 sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead);
106667 addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iTab, 0); VdbeCoverage(v);
106668 regRecord = sqlite3GetTempReg(pParse);
 
106669
106670 sqlite3GenerateIndexKey(pParse,pIndex,iTab,regRecord,0,&iPartIdxLabel,0,0);
106671 sqlite3VdbeAddOp2(v, OP_SorterInsert, iSorter, regRecord);
106672 sqlite3ResolvePartIdxLabel(pParse, iPartIdxLabel);
106673 sqlite3VdbeAddOp2(v, OP_Next, iTab, addr1+1); VdbeCoverage(v);
@@ -106677,16 +106769,17 @@
106677 (char *)pKey, P4_KEYINFO);
106678 sqlite3VdbeChangeP5(v, OPFLAG_BULKCSR|((memRootPage>=0)?OPFLAG_P2ISREG:0));
106679
106680 addr1 = sqlite3VdbeAddOp2(v, OP_SorterSort, iSorter, 0); VdbeCoverage(v);
106681 if( IsUniqueIndex(pIndex) ){
106682 int j2 = sqlite3VdbeCurrentAddr(v) + 3;
106683 sqlite3VdbeGoto(v, j2);
106684 addr2 = sqlite3VdbeCurrentAddr(v);
 
106685 sqlite3VdbeAddOp4Int(v, OP_SorterCompare, iSorter, j2, regRecord,
106686 pIndex->nKeyCol); VdbeCoverage(v);
106687 sqlite3UniqueConstraint(pParse, OE_Abort, pIndex);
 
106688 }else{
106689 addr2 = sqlite3VdbeCurrentAddr(v);
106690 }
106691 sqlite3VdbeAddOp3(v, OP_SorterData, iSorter, regRecord, iIdx);
106692 sqlite3VdbeAddOp1(v, OP_SeekEnd, iIdx);
@@ -108723,14 +108816,16 @@
108723 ** new entry to the hash table and return it.
108724 */
108725 if( createFlag && bestScore<FUNC_PERFECT_MATCH &&
108726 (pBest = sqlite3DbMallocZero(db, sizeof(*pBest)+nName+1))!=0 ){
108727 FuncDef *pOther;
 
108728 pBest->zName = (const char*)&pBest[1];
108729 pBest->nArg = (u16)nArg;
108730 pBest->funcFlags = enc;
108731 memcpy((char*)&pBest[1], zName, nName+1);
 
108732 pOther = (FuncDef*)sqlite3HashInsert(&db->aFunc, pBest->zName, pBest);
108733 if( pOther==pBest ){
108734 sqlite3DbFree(db, pBest);
108735 sqlite3OomFault(db);
108736 return 0;
@@ -109359,17 +109454,20 @@
109359 /* Delete the row */
109360 #ifndef SQLITE_OMIT_VIRTUALTABLE
109361 if( IsVirtual(pTab) ){
109362 const char *pVTab = (const char *)sqlite3GetVTable(db, pTab);
109363 sqlite3VtabMakeWritable(pParse, pTab);
109364 sqlite3VdbeAddOp4(v, OP_VUpdate, 0, 1, iKey, pVTab, P4_VTAB);
109365 sqlite3VdbeChangeP5(v, OE_Abort);
109366 assert( eOnePass==ONEPASS_OFF || eOnePass==ONEPASS_SINGLE );
109367 sqlite3MayAbort(pParse);
109368 if( eOnePass==ONEPASS_SINGLE && sqlite3IsToplevel(pParse) ){
109369 pParse->isMultiWrite = 0;
 
 
 
109370 }
 
 
109371 }else
109372 #endif
109373 {
109374 int count = (pParse->nested==0); /* True to count changes */
109375 sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur,
@@ -112000,10 +112098,16 @@
112000 int i; /* Iterator variable */
112001 Vdbe *v = sqlite3GetVdbe(pParse); /* Vdbe to add code to */
112002 int iCur = pParse->nTab - 1; /* Cursor number to use */
112003 int iOk = sqlite3VdbeMakeLabel(v); /* jump here if parent key found */
112004
 
 
 
 
 
 
112005 /* If nIncr is less than zero, then check at runtime if there are any
112006 ** outstanding constraints to resolve. If there are not, there is no need
112007 ** to check if deleting this row resolves any outstanding violations.
112008 **
112009 ** Check if any of the key columns in the child table row are NULL. If
@@ -112407,10 +112511,11 @@
112407 ** If the SQLITE_DeferFKs flag is set, then this is not required, as
112408 ** the statement transaction will not be rolled back even if FK
112409 ** constraints are violated.
112410 */
112411 if( (db->flags & SQLITE_DeferFKs)==0 ){
 
112412 sqlite3VdbeAddOp2(v, OP_FkIfZero, 0, sqlite3VdbeCurrentAddr(v)+2);
112413 VdbeCoverage(v);
112414 sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_FOREIGNKEY,
112415 OE_Abort, 0, P4_STATIC, P5_ConstraintFK);
112416 }
@@ -113319,15 +113424,30 @@
113319 Parse *pParse, /* Parsing context */
113320 int iDb, /* Index of the database holding pTab */
113321 Table *pTab /* The table we are writing to */
113322 ){
113323 int memId = 0; /* Register holding maximum rowid */
 
113324 if( (pTab->tabFlags & TF_Autoincrement)!=0
113325 && (pParse->db->mDbFlags & DBFLAG_Vacuum)==0
113326 ){
113327 Parse *pToplevel = sqlite3ParseToplevel(pParse);
113328 AutoincInfo *pInfo;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
113329
113330 pInfo = pToplevel->pAinc;
113331 while( pInfo && pInfo->pTab!=pTab ){ pInfo = pInfo->pNext; }
113332 if( pInfo==0 ){
113333 pInfo = sqlite3DbMallocRawNN(pParse->db, sizeof(*pInfo));
@@ -114498,10 +114618,11 @@
114498 for(i=0; i<pCheck->nExpr; i++){
114499 int allOk;
114500 Expr *pExpr = pCheck->a[i].pExpr;
114501 if( aiChng && checkConstraintUnchanged(pExpr, aiChng, pkChng) ) continue;
114502 allOk = sqlite3VdbeMakeLabel(v);
 
114503 sqlite3ExprIfTrue(pParse, pExpr, allOk, SQLITE_JUMPIFNULL);
114504 if( onError==OE_Ignore ){
114505 sqlite3VdbeGoto(v, ignoreDest);
114506 }else{
114507 char *zName = pCheck->a[i].zName;
@@ -114607,10 +114728,11 @@
114607 }
114608
114609 /* Check to see if the new rowid already exists in the table. Skip
114610 ** the following conflict logic if it does not. */
114611 VdbeNoopComment((v, "uniqueness check for ROWID"));
 
114612 sqlite3VdbeAddOp3(v, OP_NotExists, iDataCur, addrRowidOk, regNewData);
114613 VdbeCoverage(v);
114614
114615 switch( onError ){
114616 default: {
@@ -114819,10 +114941,11 @@
114819 continue;
114820 }
114821
114822 /* Check to see if the new index entry will be unique */
114823 sqlite3ExprCachePush(pParse);
 
114824 sqlite3VdbeAddOp4Int(v, OP_NoConflict, iThisCur, addrUniqueOk,
114825 regIdx, pIdx->nKeyCol); VdbeCoverage(v);
114826
114827 /* Generate code to handle collisions */
114828 regR = (pIdx==pPk) ? regIdx : sqlite3GetTempRange(pParse, nPkField);
@@ -114905,13 +115028,15 @@
114905 break;
114906 }
114907 default: {
114908 Trigger *pTrigger = 0;
114909 assert( onError==OE_Replace );
114910 sqlite3MultiWrite(pParse);
114911 if( db->flags&SQLITE_RecTriggers ){
114912 pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0);
 
 
 
114913 }
114914 sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur,
114915 regR, nPkField, 0, OE_Replace,
114916 (pIdx==pPk ? ONEPASS_SINGLE : ONEPASS_OFF), iThisCur);
114917 seenReplace = 1;
@@ -115428,10 +115553,11 @@
115428 u8 insFlags;
115429 sqlite3OpenTable(pParse, iSrc, iDbSrc, pSrc, OP_OpenRead);
115430 emptySrcTest = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0); VdbeCoverage(v);
115431 if( pDest->iPKey>=0 ){
115432 addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid);
 
115433 addr2 = sqlite3VdbeAddOp3(v, OP_NotExists, iDest, 0, regRowid);
115434 VdbeCoverage(v);
115435 sqlite3RowidConstraint(pParse, onError, pDest);
115436 sqlite3VdbeJumpHere(v, addr2);
115437 autoIncStep(pParse, regAutoinc, regRowid);
@@ -129709,11 +129835,11 @@
129709 int regArg; /* First register in VUpdate arg array */
129710 int regRec; /* Register in which to assemble record */
129711 int regRowid; /* Register for ephem table rowid */
129712 int iCsr = pSrc->a[0].iCursor; /* Cursor used for virtual table scan */
129713 int aDummy[2]; /* Unused arg for sqlite3WhereOkOnePass() */
129714 int bOnePass; /* True to use onepass strategy */
129715 int addr; /* Address of OP_OpenEphemeral */
129716
129717 /* Allocate nArg registers in which to gather the arguments for VUpdate. Then
129718 ** create and open the ephemeral table in which the records created from
129719 ** these arguments will be temporarily stored. */
@@ -129754,16 +129880,20 @@
129754 iPk = pPk->aiColumn[0];
129755 sqlite3VdbeAddOp3(v, OP_VColumn, iCsr, iPk, regArg);
129756 sqlite3VdbeAddOp2(v, OP_SCopy, regArg+2+iPk, regArg+1);
129757 }
129758
129759 bOnePass = sqlite3WhereOkOnePass(pWInfo, aDummy);
129760
129761 if( bOnePass ){
 
 
 
129762 /* If using the onepass strategy, no-op out the OP_OpenEphemeral coded
129763 ** above. */
129764 sqlite3VdbeChangeToNoop(v, addr);
 
129765 }else{
129766 /* Create a record from the argument register contents and insert it into
129767 ** the ephemeral table. */
129768 sqlite3MultiWrite(pParse);
129769 sqlite3VdbeAddOp3(v, OP_MakeRecord, regArg, nArg, regRec);
@@ -129775,11 +129905,11 @@
129775 sqlite3VdbeAddOp2(v, OP_NewRowid, ephemTab, regRowid);
129776 sqlite3VdbeAddOp3(v, OP_Insert, ephemTab, regRec, regRowid);
129777 }
129778
129779
129780 if( bOnePass==0 ){
129781 /* End the virtual table scan */
129782 sqlite3WhereEnd(pWInfo);
129783
129784 /* Begin scannning through the ephemeral table. */
129785 addr = sqlite3VdbeAddOp1(v, OP_Rewind, ephemTab); VdbeCoverage(v);
@@ -129795,11 +129925,11 @@
129795 sqlite3VdbeChangeP5(v, onError==OE_Default ? OE_Abort : onError);
129796 sqlite3MayAbort(pParse);
129797
129798 /* End of the ephemeral table scan. Or, if using the onepass strategy,
129799 ** jump to here if the scan visited zero rows. */
129800 if( bOnePass==0 ){
129801 sqlite3VdbeAddOp2(v, OP_Next, ephemTab, addr+1); VdbeCoverage(v);
129802 sqlite3VdbeJumpHere(v, addr);
129803 sqlite3VdbeAddOp2(v, OP_Close, ephemTab, 0);
129804 }else{
129805 sqlite3WhereEnd(pWInfo);
@@ -130038,10 +130168,11 @@
130038 k = sqlite3ColumnOfIndex(pIdx, pPk->aiColumn[i]);
130039 sqlite3VdbeAddOp3(v, OP_Column, iCur, k, iPk+i);
130040 VdbeComment((v, "%s.%s", pIdx->zName,
130041 pTab->aCol[pPk->aiColumn[i]].zName));
130042 }
 
130043 i = sqlite3VdbeAddOp4Int(v, OP_Found, iDataCur, 0, iPk, nPk);
130044 VdbeCoverage(v);
130045 sqlite3VdbeAddOp4(v, OP_Halt, SQLITE_CORRUPT, OE_Abort, 0,
130046 "corrupt database", P4_STATIC);
130047 sqlite3VdbeJumpHere(v, i);
@@ -131490,13 +131621,10 @@
131490 sqlite3_module *pMod;
131491 void (*xSFunc)(sqlite3_context*,int,sqlite3_value**) = 0;
131492 void *pArg = 0;
131493 FuncDef *pNew;
131494 int rc = 0;
131495 char *zLowerName;
131496 unsigned char *z;
131497
131498
131499 /* Check to see the left operand is a column in a virtual table */
131500 if( NEVER(pExpr==0) ) return pDef;
131501 if( pExpr->op!=TK_COLUMN ) return pDef;
131502 pTab = pExpr->pTab;
@@ -131507,20 +131635,26 @@
131507 assert( pVtab->pModule!=0 );
131508 pMod = (sqlite3_module *)pVtab->pModule;
131509 if( pMod->xFindFunction==0 ) return pDef;
131510
131511 /* Call the xFindFunction method on the virtual table implementation
131512 ** to see if the implementation wants to overload this function
 
 
 
 
131513 */
131514 zLowerName = sqlite3DbStrDup(db, pDef->zName);
131515 if( zLowerName ){
131516 for(z=(unsigned char*)zLowerName; *z; z++){
131517 *z = sqlite3UpperToLower[*z];
131518 }
131519 rc = pMod->xFindFunction(pVtab, nArg, zLowerName, &xSFunc, &pArg);
131520 sqlite3DbFree(db, zLowerName);
131521 }
 
 
131522 if( rc==0 ){
131523 return pDef;
131524 }
131525
131526 /* Create a new ephemeral function definition for the overloaded
@@ -138881,18 +139015,20 @@
138881 pNew->nLTerm = 1;
138882 pNew->aLTerm[0] = pTerm;
138883 /* TUNING: One-time cost for computing the automatic index is
138884 ** estimated to be X*N*log2(N) where N is the number of rows in
138885 ** the table being indexed and where X is 7 (LogEst=28) for normal
138886 ** tables or 1.375 (LogEst=4) for views and subqueries. The value
138887 ** of X is smaller for views and subqueries so that the query planner
138888 ** will be more aggressive about generating automatic indexes for
138889 ** those objects, since there is no opportunity to add schema
138890 ** indexes on subqueries and views. */
138891 pNew->rSetup = rLogSize + rSize + 4;
138892 if( pTab->pSelect==0 && (pTab->tabFlags & TF_Ephemeral)==0 ){
138893 pNew->rSetup += 24;
 
 
138894 }
138895 ApplyCostMultiplier(pNew->rSetup, pTab->costMult);
138896 if( pNew->rSetup<0 ) pNew->rSetup = 0;
138897 /* TUNING: Each index lookup yields 20 rows in the table. This
138898 ** is more than the usual guess of 10 rows, since we have no way
@@ -140024,16 +140160,19 @@
140024 Bitmask maskNew; /* Mask of src visited by (..) */
140025 Bitmask revMask = 0; /* Mask of rev-order loops for (..) */
140026
140027 if( (pWLoop->prereq & ~pFrom->maskLoop)!=0 ) continue;
140028 if( (pWLoop->maskSelf & pFrom->maskLoop)!=0 ) continue;
140029 if( (pWLoop->wsFlags & WHERE_AUTO_INDEX)!=0 && pFrom->nRow<10 ){
140030 /* Do not use an automatic index if the this loop is expected
140031 ** to run less than 2 times. */
 
 
140032 assert( 10==sqlite3LogEst(2) );
140033 continue;
140034 }
 
140035 /* At this point, pWLoop is a candidate to be the next loop.
140036 ** Compute its cost */
140037 rUnsorted = sqlite3LogEstAdd(pWLoop->rSetup,pWLoop->rRun + pFrom->nRow);
140038 rUnsorted = sqlite3LogEstAdd(rUnsorted, pFrom->rUnsorted);
140039 nOut = pFrom->nRow + pWLoop->nOut;
@@ -148446,23 +148585,25 @@
148446 return SQLITE_MISUSE_BKPT;
148447 }
148448 #endif
148449 sqlite3_mutex_enter(db->mutex);
148450 if( xDestroy ){
148451 pArg = (FuncDestructor *)sqlite3DbMallocZero(db, sizeof(FuncDestructor));
148452 if( !pArg ){
 
148453 xDestroy(p);
148454 goto out;
148455 }
 
148456 pArg->xDestroy = xDestroy;
148457 pArg->pUserData = p;
148458 }
148459 rc = sqlite3CreateFunc(db, zFunc, nArg, enc, p, xSFunc, xStep, xFinal, pArg);
148460 if( pArg && pArg->nRef==0 ){
148461 assert( rc!=SQLITE_OK );
148462 xDestroy(p);
148463 sqlite3DbFree(db, pArg);
148464 }
148465
148466 out:
148467 rc = sqlite3ApiExit(db, rc);
148468 sqlite3_mutex_leave(db->mutex);
@@ -148495,10 +148636,32 @@
148495 sqlite3_mutex_leave(db->mutex);
148496 return rc;
148497 }
148498 #endif
148499
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
148500
148501 /*
148502 ** Declare that a function has been overloaded by a virtual table.
148503 **
148504 ** If the function already exists as a regular global function, then
@@ -148513,25 +148676,26 @@
148513 SQLITE_API int sqlite3_overload_function(
148514 sqlite3 *db,
148515 const char *zName,
148516 int nArg
148517 ){
148518 int rc = SQLITE_OK;
 
148519
148520 #ifdef SQLITE_ENABLE_API_ARMOR
148521 if( !sqlite3SafetyCheckOk(db) || zName==0 || nArg<-2 ){
148522 return SQLITE_MISUSE_BKPT;
148523 }
148524 #endif
148525 sqlite3_mutex_enter(db->mutex);
148526 if( sqlite3FindFunction(db, zName, nArg, SQLITE_UTF8, 0)==0 ){
148527 rc = sqlite3CreateFunc(db, zName, nArg, SQLITE_UTF8,
148528 0, sqlite3InvalidFunction, 0, 0, 0);
148529 }
148530 rc = sqlite3ApiExit(db, rc);
148531 sqlite3_mutex_leave(db->mutex);
148532 return rc;
 
 
 
 
148533 }
148534
148535 #ifndef SQLITE_OMIT_TRACE
148536 /*
148537 ** Register a trace function. The pArg from the previously registered trace
@@ -170371,18 +170535,19 @@
170371 ** in the table name is replaced with the user-supplied name of the r-tree
170372 ** table.
170373 **
170374 ** CREATE TABLE %_node(nodeno INTEGER PRIMARY KEY, data BLOB)
170375 ** CREATE TABLE %_parent(nodeno INTEGER PRIMARY KEY, parentnode INTEGER)
170376 ** CREATE TABLE %_rowid(rowid INTEGER PRIMARY KEY, nodeno INTEGER)
170377 **
170378 ** The data for each node of the r-tree structure is stored in the %_node
170379 ** table. For each node that is not the root node of the r-tree, there is
170380 ** an entry in the %_parent table associating the node with its parent.
170381 ** And for each row of data in the table, there is an entry in the %_rowid
170382 ** table that maps from the entries rowid to the id of the node that it
170383 ** is stored on.
 
170384 **
170385 ** The root node of an r-tree always exists, even if the r-tree table is
170386 ** empty. The nodeno of the root node is always 1. All other nodes in the
170387 ** table must be the same size as the root node. The content of each node
170388 ** is formatted as follows:
@@ -170440,10 +170605,13 @@
170440 typedef union RtreeCoord RtreeCoord;
170441 typedef struct RtreeSearchPoint RtreeSearchPoint;
170442
170443 /* The rtree may have between 1 and RTREE_MAX_DIMENSIONS dimensions. */
170444 #define RTREE_MAX_DIMENSIONS 5
 
 
 
170445
170446 /* Size of hash table Rtree.aHash. This hash table is not expected to
170447 ** ever contain very many entries, so a fixed number of buckets is
170448 ** used.
170449 */
@@ -170469,16 +170637,19 @@
170469 u8 nDim; /* Number of dimensions */
170470 u8 nDim2; /* Twice the number of dimensions */
170471 u8 eCoordType; /* RTREE_COORD_REAL32 or RTREE_COORD_INT32 */
170472 u8 nBytesPerCell; /* Bytes consumed per cell */
170473 u8 inWrTrans; /* True if inside write transaction */
 
170474 int iDepth; /* Current depth of the r-tree structure */
170475 char *zDb; /* Name of database containing r-tree table */
170476 char *zName; /* Name of r-tree table */
170477 u32 nBusy; /* Current number of users of this structure */
170478 i64 nRowEst; /* Estimated number of rows in this table */
170479 u32 nCursor; /* Number of open cursors */
 
 
170480
170481 /* List of nodes removed during a CondenseTree operation. List is
170482 ** linked together via the pointer normally used for hash chains -
170483 ** RtreeNode.pNext. RtreeNode.iNode stores the depth of the sub-tree
170484 ** headed by the node (leaf nodes have RtreeNode.iNode==0).
@@ -170500,10 +170671,13 @@
170500
170501 /* Statements to read/write/delete a record from xxx_parent */
170502 sqlite3_stmt *pReadParent;
170503 sqlite3_stmt *pWriteParent;
170504 sqlite3_stmt *pDeleteParent;
 
 
 
170505
170506 RtreeNode *aHash[HASHSIZE]; /* Hash table of in-memory nodes. */
170507 };
170508
170509 /* Possible values for Rtree.eCoordType: */
@@ -170577,17 +170751,19 @@
170577 */
170578 struct RtreeCursor {
170579 sqlite3_vtab_cursor base; /* Base class. Must be first */
170580 u8 atEOF; /* True if at end of search */
170581 u8 bPoint; /* True if sPoint is valid */
 
170582 int iStrategy; /* Copy of idxNum search parameter */
170583 int nConstraint; /* Number of entries in aConstraint */
170584 RtreeConstraint *aConstraint; /* Search constraints. */
170585 int nPointAlloc; /* Number of slots allocated for aPoint[] */
170586 int nPoint; /* Number of slots used in aPoint[] */
170587 int mxLevel; /* iLevel value for root of the tree */
170588 RtreeSearchPoint *aPoint; /* Priority queue for search points */
 
170589 RtreeSearchPoint sPoint; /* Cached next search point */
170590 RtreeNode *aNode[RTREE_CACHE_SZ]; /* Rtree node cache */
170591 u32 anQueue[RTREE_MAX_DEPTH+1]; /* Number of queued entries by iLevel */
170592 };
170593
@@ -170870,10 +171046,11 @@
170870 /*
170871 ** Increment the reference count of node p.
170872 */
170873 static void nodeReference(RtreeNode *p){
170874 if( p ){
 
170875 p->nRef++;
170876 }
170877 }
170878
170879 /*
@@ -170937,10 +171114,11 @@
170937 pNode = (RtreeNode *)sqlite3_malloc(sizeof(RtreeNode) + pRtree->iNodeSize);
170938 if( pNode ){
170939 memset(pNode, 0, sizeof(RtreeNode) + pRtree->iNodeSize);
170940 pNode->zData = (u8 *)&pNode[1];
170941 pNode->nRef = 1;
 
170942 pNode->pParent = pParent;
170943 pNode->isDirty = 1;
170944 nodeReference(pParent);
170945 }
170946 return pNode;
@@ -170970,14 +171148,14 @@
170970 RtreeNode *pNode = 0;
170971
170972 /* Check if the requested node is already in the hash table. If so,
170973 ** increase its reference count and return it.
170974 */
170975 if( (pNode = nodeHashLookup(pRtree, iNode)) ){
170976 assert( !pParent || !pNode->pParent || pNode->pParent==pParent );
170977 if( pParent && !pNode->pParent ){
170978 nodeReference(pParent);
170979 pNode->pParent = pParent;
170980 }
170981 pNode->nRef++;
170982 *ppNode = pNode;
170983 return SQLITE_OK;
@@ -171012,10 +171190,11 @@
171012 rc = SQLITE_NOMEM;
171013 }else{
171014 pNode->pParent = pParent;
171015 pNode->zData = (u8 *)&pNode[1];
171016 pNode->nRef = 1;
 
171017 pNode->iNode = iNode;
171018 pNode->isDirty = 0;
171019 pNode->pNext = 0;
171020 rc = sqlite3_blob_read(pRtree->pNodeBlob, pNode->zData,
171021 pRtree->iNodeSize, 0);
@@ -171052,11 +171231,14 @@
171052 }else{
171053 rc = SQLITE_CORRUPT_VTAB;
171054 }
171055 *ppNode = pNode;
171056 }else{
171057 sqlite3_free(pNode);
 
 
 
171058 *ppNode = 0;
171059 }
171060
171061 return rc;
171062 }
@@ -171149,12 +171331,14 @@
171149 */
171150 static int nodeRelease(Rtree *pRtree, RtreeNode *pNode){
171151 int rc = SQLITE_OK;
171152 if( pNode ){
171153 assert( pNode->nRef>0 );
 
171154 pNode->nRef--;
171155 if( pNode->nRef==0 ){
 
171156 if( pNode->iNode==1 ){
171157 pRtree->iDepth = -1;
171158 }
171159 if( pNode->pParent ){
171160 rc = nodeRelease(pRtree, pNode->pParent);
@@ -171267,20 +171451,23 @@
171267 */
171268 static void rtreeRelease(Rtree *pRtree){
171269 pRtree->nBusy--;
171270 if( pRtree->nBusy==0 ){
171271 pRtree->inWrTrans = 0;
171272 pRtree->nCursor = 0;
171273 nodeBlobReset(pRtree);
 
171274 sqlite3_finalize(pRtree->pWriteNode);
171275 sqlite3_finalize(pRtree->pDeleteNode);
171276 sqlite3_finalize(pRtree->pReadRowid);
171277 sqlite3_finalize(pRtree->pWriteRowid);
171278 sqlite3_finalize(pRtree->pDeleteRowid);
171279 sqlite3_finalize(pRtree->pReadParent);
171280 sqlite3_finalize(pRtree->pWriteParent);
171281 sqlite3_finalize(pRtree->pDeleteParent);
 
 
171282 sqlite3_free(pRtree);
171283 }
171284 }
171285
171286 /*
@@ -171365,10 +171552,11 @@
171365 Rtree *pRtree = (Rtree *)(cur->pVtab);
171366 int ii;
171367 RtreeCursor *pCsr = (RtreeCursor *)cur;
171368 assert( pRtree->nCursor>0 );
171369 freeCursorConstraints(pCsr);
 
171370 sqlite3_free(pCsr->aPoint);
171371 for(ii=0; ii<RTREE_CACHE_SZ; ii++) nodeRelease(pRtree, pCsr->aNode[ii]);
171372 sqlite3_free(pCsr);
171373 pRtree->nCursor--;
171374 nodeBlobReset(pRtree);
@@ -171736,11 +171924,11 @@
171736 if( pNew==0 ) return 0;
171737 ii = (int)(pNew - pCur->aPoint) + 1;
171738 if( ii<RTREE_CACHE_SZ ){
171739 assert( pCur->aNode[ii]==0 );
171740 pCur->aNode[ii] = pCur->aNode[0];
171741 }else{
171742 nodeRelease(RTREE_OF_CURSOR(pCur), pCur->aNode[0]);
171743 }
171744 pCur->aNode[0] = 0;
171745 *pNew = pCur->sPoint;
171746 }
@@ -171907,10 +172095,14 @@
171907 RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor;
171908 int rc = SQLITE_OK;
171909
171910 /* Move to the next entry that matches the configured constraints. */
171911 RTREE_QUEUE_TRACE(pCsr, "POP-Nx:");
 
 
 
 
171912 rtreeSearchPointPop(pCsr);
171913 rc = rtreeStepToLeaf(pCsr);
171914 return rc;
171915 }
171916
@@ -171941,11 +172133,11 @@
171941
171942 if( rc ) return rc;
171943 if( p==0 ) return SQLITE_OK;
171944 if( i==0 ){
171945 sqlite3_result_int64(ctx, nodeGetRowid(pRtree, pNode, p->iCell));
171946 }else{
171947 nodeGetCoord(pRtree, pNode, p->iCell, i-1, &c);
171948 #ifndef SQLITE_RTREE_INT_ONLY
171949 if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
171950 sqlite3_result_double(ctx, c.f);
171951 }else
@@ -171952,11 +172144,31 @@
171952 #endif
171953 {
171954 assert( pRtree->eCoordType==RTREE_COORD_INT32 );
171955 sqlite3_result_int(ctx, c.i);
171956 }
171957 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
171958 return SQLITE_OK;
171959 }
171960
171961 /*
171962 ** Use nodeAcquire() to obtain the leaf node containing the record with
@@ -172030,18 +172242,21 @@
172030 RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor;
172031 RtreeNode *pRoot = 0;
172032 int ii;
172033 int rc = SQLITE_OK;
172034 int iCell = 0;
 
172035
172036 rtreeReference(pRtree);
172037
172038 /* Reset the cursor to the same state as rtreeOpen() leaves it in. */
172039 freeCursorConstraints(pCsr);
172040 sqlite3_free(pCsr->aPoint);
 
172041 memset(pCsr, 0, sizeof(RtreeCursor));
172042 pCsr->base.pVtab = (sqlite3_vtab*)pRtree;
 
172043
172044 pCsr->iStrategy = idxNum;
172045 if( idxNum==1 ){
172046 /* Special case - lookup by rowid. */
172047 RtreeNode *pLeaf; /* Leaf on which the required cell resides */
@@ -172200,14 +172415,18 @@
172200 ** sqlite uses an internal cost of 0.0). It is expected to return
172201 ** a single row.
172202 */
172203 pIdxInfo->estimatedCost = 30.0;
172204 pIdxInfo->estimatedRows = 1;
 
172205 return SQLITE_OK;
172206 }
172207
172208 if( p->usable && (p->iColumn>0 || p->op==SQLITE_INDEX_CONSTRAINT_MATCH) ){
 
 
 
172209 u8 op;
172210 switch( p->op ){
172211 case SQLITE_INDEX_CONSTRAINT_EQ: op = RTREE_EQ; break;
172212 case SQLITE_INDEX_CONSTRAINT_GT: op = RTREE_GT; break;
172213 case SQLITE_INDEX_CONSTRAINT_LE: op = RTREE_LE; break;
@@ -172776,11 +172995,11 @@
172776 pNode->isDirty = 1;
172777 writeInt16(pNode->zData, pRtree->iDepth);
172778 }else{
172779 pLeft = pNode;
172780 pRight = nodeNew(pRtree, pLeft->pParent);
172781 nodeReference(pLeft);
172782 }
172783
172784 if( !pLeft || !pRight ){
172785 rc = SQLITE_NOMEM;
172786 goto splitnode_out;
@@ -173266,10 +173485,11 @@
173266 for(pLeaf=pRtree->pDeleted; pLeaf; pLeaf=pRtree->pDeleted){
173267 if( rc==SQLITE_OK ){
173268 rc = reinsertNodeContent(pRtree, pLeaf);
173269 }
173270 pRtree->pDeleted = pLeaf->pNext;
 
173271 sqlite3_free(pLeaf);
173272 }
173273
173274 /* Release the reference to the root node. */
173275 if( rc==SQLITE_OK ){
@@ -173362,18 +173582,24 @@
173362 ** The xUpdate method for rtree module virtual tables.
173363 */
173364 static int rtreeUpdate(
173365 sqlite3_vtab *pVtab,
173366 int nData,
173367 sqlite3_value **azData,
173368 sqlite_int64 *pRowid
173369 ){
173370 Rtree *pRtree = (Rtree *)pVtab;
173371 int rc = SQLITE_OK;
173372 RtreeCell cell; /* New cell to insert if nData>1 */
173373 int bHaveRowid = 0; /* Set to 1 after new rowid is determined */
173374
 
 
 
 
 
 
173375 rtreeReference(pRtree);
173376 assert(nData>=1);
173377
173378 cell.iRowid = 0; /* Used only to suppress a compiler warning */
173379
@@ -173388,50 +173614,51 @@
173388 ** case, SQLITE_CONSTRAINT must be returned regardless of the
173389 ** conflict-handling mode specified by the user.
173390 */
173391 if( nData>1 ){
173392 int ii;
 
173393
173394 /* Populate the cell.aCoord[] array. The first coordinate is azData[3].
 
173395 **
173396 ** NB: nData can only be less than nDim*2+3 if the rtree is mis-declared
173397 ** with "column" that are interpreted as table constraints.
173398 ** Example: CREATE VIRTUAL TABLE bad USING rtree(x,y,CHECK(y>5));
173399 ** This problem was discovered after years of use, so we silently ignore
173400 ** these kinds of misdeclared tables to avoid breaking any legacy.
173401 */
173402 assert( nData<=(pRtree->nDim2 + 3) );
173403
173404 #ifndef SQLITE_RTREE_INT_ONLY
173405 if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
173406 for(ii=0; ii<nData-4; ii+=2){
173407 cell.aCoord[ii].f = rtreeValueDown(azData[ii+3]);
173408 cell.aCoord[ii+1].f = rtreeValueUp(azData[ii+4]);
173409 if( cell.aCoord[ii].f>cell.aCoord[ii+1].f ){
173410 rc = rtreeConstraintError(pRtree, ii+1);
173411 goto constraint;
173412 }
173413 }
173414 }else
173415 #endif
173416 {
173417 for(ii=0; ii<nData-4; ii+=2){
173418 cell.aCoord[ii].i = sqlite3_value_int(azData[ii+3]);
173419 cell.aCoord[ii+1].i = sqlite3_value_int(azData[ii+4]);
173420 if( cell.aCoord[ii].i>cell.aCoord[ii+1].i ){
173421 rc = rtreeConstraintError(pRtree, ii+1);
173422 goto constraint;
173423 }
173424 }
173425 }
173426
173427 /* If a rowid value was supplied, check if it is already present in
173428 ** the table. If so, the constraint has failed. */
173429 if( sqlite3_value_type(azData[2])!=SQLITE_NULL ){
173430 cell.iRowid = sqlite3_value_int64(azData[2]);
173431 if( sqlite3_value_type(azData[0])==SQLITE_NULL
173432 || sqlite3_value_int64(azData[0])!=cell.iRowid
173433 ){
173434 int steprc;
173435 sqlite3_bind_int64(pRtree->pReadRowid, 1, cell.iRowid);
173436 steprc = sqlite3_step(pRtree->pReadRowid);
173437 rc = sqlite3_reset(pRtree->pReadRowid);
@@ -173446,20 +173673,20 @@
173446 }
173447 bHaveRowid = 1;
173448 }
173449 }
173450
173451 /* If azData[0] is not an SQL NULL value, it is the rowid of a
173452 ** record to delete from the r-tree table. The following block does
173453 ** just that.
173454 */
173455 if( sqlite3_value_type(azData[0])!=SQLITE_NULL ){
173456 rc = rtreeDeleteRowid(pRtree, sqlite3_value_int64(azData[0]));
173457 }
173458
173459 /* If the azData[] array contains more than one element, elements
173460 ** (azData[2]..azData[argc-1]) contain a new record to insert into
173461 ** the r-tree structure.
173462 */
173463 if( rc==SQLITE_OK && nData>1 ){
173464 /* Insert the new record into the r-tree */
173465 RtreeNode *pLeaf = 0;
@@ -173480,10 +173707,20 @@
173480 rc2 = nodeRelease(pRtree, pLeaf);
173481 if( rc==SQLITE_OK ){
173482 rc = rc2;
173483 }
173484 }
 
 
 
 
 
 
 
 
 
 
173485 }
173486
173487 constraint:
173488 rtreeRelease(pRtree);
173489 return rc;
@@ -173636,37 +173873,48 @@
173636 int rc = SQLITE_OK;
173637
173638 #define N_STATEMENT 8
173639 static const char *azSql[N_STATEMENT] = {
173640 /* Write the xxx_node table */
173641 "INSERT OR REPLACE INTO '%q'.'%q_node' VALUES(:1, :2)",
173642 "DELETE FROM '%q'.'%q_node' WHERE nodeno = :1",
173643
173644 /* Read and write the xxx_rowid table */
173645 "SELECT nodeno FROM '%q'.'%q_rowid' WHERE rowid = :1",
173646 "INSERT OR REPLACE INTO '%q'.'%q_rowid' VALUES(:1, :2)",
173647 "DELETE FROM '%q'.'%q_rowid' WHERE rowid = :1",
173648
173649 /* Read and write the xxx_parent table */
173650 "SELECT parentnode FROM '%q'.'%q_parent' WHERE nodeno = :1",
173651 "INSERT OR REPLACE INTO '%q'.'%q_parent' VALUES(:1, :2)",
173652 "DELETE FROM '%q'.'%q_parent' WHERE nodeno = :1"
173653 };
173654 sqlite3_stmt **appStmt[N_STATEMENT];
173655 int i;
173656
173657 pRtree->db = db;
173658
173659 if( isCreate ){
173660 char *zCreate = sqlite3_mprintf(
173661 "CREATE TABLE \"%w\".\"%w_node\"(nodeno INTEGER PRIMARY KEY, data BLOB);"
173662 "CREATE TABLE \"%w\".\"%w_rowid\"(rowid INTEGER PRIMARY KEY, nodeno INTEGER);"
173663 "CREATE TABLE \"%w\".\"%w_parent\"(nodeno INTEGER PRIMARY KEY,"
173664 " parentnode INTEGER);"
173665 "INSERT INTO '%q'.'%q_node' VALUES(1, zeroblob(%d))",
173666 zDb, zPrefix, zDb, zPrefix, zDb, zPrefix, zDb, zPrefix, pRtree->iNodeSize
173667 );
 
 
 
 
 
 
 
 
 
 
 
173668 if( !zCreate ){
173669 return SQLITE_NOMEM;
173670 }
173671 rc = sqlite3_exec(db, zCreate, 0, 0, 0);
173672 sqlite3_free(zCreate);
@@ -173684,18 +173932,54 @@
173684 appStmt[6] = &pRtree->pWriteParent;
173685 appStmt[7] = &pRtree->pDeleteParent;
173686
173687 rc = rtreeQueryStat1(db, pRtree);
173688 for(i=0; i<N_STATEMENT && rc==SQLITE_OK; i++){
173689 char *zSql = sqlite3_mprintf(azSql[i], zDb, zPrefix);
 
 
 
 
 
 
 
 
 
 
173690 if( zSql ){
173691 rc = sqlite3_prepare_v3(db, zSql, -1, SQLITE_PREPARE_PERSISTENT,
173692 appStmt[i], 0);
173693 }else{
173694 rc = SQLITE_NOMEM;
173695 }
173696 sqlite3_free(zSql);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
173697 }
173698
173699 return rc;
173700 }
173701
@@ -173795,21 +174079,26 @@
173795 int rc = SQLITE_OK;
173796 Rtree *pRtree;
173797 int nDb; /* Length of string argv[1] */
173798 int nName; /* Length of string argv[2] */
173799 int eCoordType = (pAux ? RTREE_COORD_INT32 : RTREE_COORD_REAL32);
 
 
 
 
173800
173801 const char *aErrMsg[] = {
173802 0, /* 0 */
173803 "Wrong number of columns for an rtree table", /* 1 */
173804 "Too few columns for an rtree table", /* 2 */
173805 "Too many columns for an rtree table" /* 3 */
 
173806 };
173807
173808 int iErr = (argc<6) ? 2 : argc>(RTREE_MAX_DIMENSIONS*2+4) ? 3 : argc%2;
173809 if( aErrMsg[iErr] ){
173810 *pzErr = sqlite3_mprintf("%s", aErrMsg[iErr]);
173811 return SQLITE_ERROR;
173812 }
173813
173814 sqlite3_vtab_config(db, SQLITE_VTAB_CONSTRAINT_SUPPORT, 1);
173815
@@ -173823,57 +174112,77 @@
173823 memset(pRtree, 0, sizeof(Rtree)+nDb+nName+2);
173824 pRtree->nBusy = 1;
173825 pRtree->base.pModule = &rtreeModule;
173826 pRtree->zDb = (char *)&pRtree[1];
173827 pRtree->zName = &pRtree->zDb[nDb+1];
173828 pRtree->nDim = (u8)((argc-4)/2);
173829 pRtree->nDim2 = pRtree->nDim*2;
173830 pRtree->nBytesPerCell = 8 + pRtree->nDim2*4;
173831 pRtree->eCoordType = (u8)eCoordType;
173832 memcpy(pRtree->zDb, argv[1], nDb);
173833 memcpy(pRtree->zName, argv[2], nName);
173834
173835 /* Figure out the node size to use. */
173836 rc = getNodeSize(db, pRtree, isCreate, pzErr);
173837
173838 /* Create/Connect to the underlying relational database schema. If
173839 ** that is successful, call sqlite3_declare_vtab() to configure
173840 ** the r-tree table schema.
173841 */
173842 if( rc==SQLITE_OK ){
173843 if( (rc = rtreeSqlInit(pRtree, db, argv[1], argv[2], isCreate)) ){
173844 *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
173845 }else{
173846 char *zSql = sqlite3_mprintf("CREATE TABLE x(%s", argv[3]);
173847 char *zTmp;
173848 int ii;
173849 for(ii=4; zSql && ii<argc; ii++){
173850 zTmp = zSql;
173851 zSql = sqlite3_mprintf("%s, %s", zTmp, argv[ii]);
173852 sqlite3_free(zTmp);
173853 }
173854 if( zSql ){
173855 zTmp = zSql;
173856 zSql = sqlite3_mprintf("%s);", zTmp);
173857 sqlite3_free(zTmp);
173858 }
173859 if( !zSql ){
173860 rc = SQLITE_NOMEM;
173861 }else if( SQLITE_OK!=(rc = sqlite3_declare_vtab(db, zSql)) ){
173862 *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
173863 }
173864 sqlite3_free(zSql);
173865 }
173866 }
173867
173868 if( rc==SQLITE_OK ){
173869 *ppVtab = (sqlite3_vtab *)pRtree;
173870 }else{
173871 assert( *ppVtab==0 );
173872 assert( pRtree->nBusy==1 );
173873 rtreeRelease(pRtree);
173874 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
173875 return rc;
173876 }
173877
173878
173879 /*
@@ -174098,11 +174407,11 @@
174098 ** This function is used to check that the %_parent (if bLeaf==0) or %_rowid
174099 ** (if bLeaf==1) table contains a specified entry. The schemas of the
174100 ** two tables are:
174101 **
174102 ** CREATE TABLE %_parent(nodeno INTEGER PRIMARY KEY, parentnode INTEGER)
174103 ** CREATE TABLE %_rowid(rowid INTEGER PRIMARY KEY, nodeno INTEGER)
174104 **
174105 ** In both cases, this function checks that there exists an entry with
174106 ** IPK value iKey and the second column set to iVal.
174107 **
174108 */
@@ -174113,12 +174422,12 @@
174113 i64 iVal /* Expected value for mapping */
174114 ){
174115 int rc;
174116 sqlite3_stmt *pStmt;
174117 const char *azSql[2] = {
174118 "SELECT parentnode FROM %Q.'%q_parent' WHERE nodeno=?",
174119 "SELECT nodeno FROM %Q.'%q_rowid' WHERE rowid=?"
174120 };
174121
174122 assert( bLeaf==0 || bLeaf==1 );
174123 if( pCheck->aCheckMapping[bLeaf]==0 ){
174124 pCheck->aCheckMapping[bLeaf] = rtreeCheckPrepare(pCheck,
@@ -174298,10 +174607,11 @@
174298 char **pzReport /* OUT: sqlite3_malloc'd report text */
174299 ){
174300 RtreeCheck check; /* Common context for various routines */
174301 sqlite3_stmt *pStmt = 0; /* Used to find column count of rtree table */
174302 int bEnd = 0; /* True if transaction should be closed */
 
174303
174304 /* Initialize the context object */
174305 memset(&check, 0, sizeof(check));
174306 check.db = db;
174307 check.zDb = zDb;
@@ -174312,16 +174622,26 @@
174312 ** on a consistent snapshot. */
174313 if( sqlite3_get_autocommit(db) ){
174314 check.rc = sqlite3_exec(db, "BEGIN", 0, 0, 0);
174315 bEnd = 1;
174316 }
 
 
 
 
 
 
 
 
 
 
174317
174318 /* Find number of dimensions in the rtree table. */
174319 pStmt = rtreeCheckPrepare(&check, "SELECT * FROM %Q.%Q", zDb, zTab);
174320 if( pStmt ){
174321 int rc;
174322 check.nDim = (sqlite3_column_count(pStmt) - 1) / 2;
174323 if( check.nDim<1 ){
174324 rtreeCheckAppendMsg(&check, "Schema corrupt or not an rtree");
174325 }else if( SQLITE_ROW==sqlite3_step(pStmt) ){
174326 check.bInt = (sqlite3_column_type(pStmt, 1)==SQLITE_INTEGER);
174327 }
@@ -189525,11 +189845,11 @@
189525 }else{
189526 jsonAppendChar(&x, '$');
189527 }
189528 if( p->eType==JSON_ARRAY ){
189529 jsonPrintf(30, &x, "[%d]", p->iRowid);
189530 }else{
189531 jsonPrintf(pThis->n, &x, ".%.*s", pThis->n-2, pThis->u.zJContent+1);
189532 }
189533 }
189534 jsonResult(&x);
189535 break;
@@ -207256,11 +207576,11 @@
207256 int nArg, /* Number of args */
207257 sqlite3_value **apUnused /* Function arguments */
207258 ){
207259 assert( nArg==0 );
207260 UNUSED_PARAM2(nArg, apUnused);
207261 sqlite3_result_text(pCtx, "fts5: 2018-05-14 00:41:12 d0f35739af3b226c8eef39676407293650cde551acef06fe8628fdd5b59bd66a", -1, SQLITE_TRANSIENT);
207262 }
207263
207264 static int fts5Init(sqlite3 *db){
207265 static const sqlite3_module fts5Mod = {
207266 /* iVersion */ 2,
@@ -211526,12 +211846,12 @@
211526 }
211527 #endif /* SQLITE_CORE */
211528 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_STMTVTAB) */
211529
211530 /************** End of stmt.c ************************************************/
211531 #if __LINE__!=211531
211532 #undef SQLITE_SOURCE_ID
211533 #define SQLITE_SOURCE_ID "2018-05-14 00:41:12 d0f35739af3b226c8eef39676407293650cde551acef06fe8628fdd5b59balt2"
211534 #endif
211535 /* Return the source-id for this library */
211536 SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; }
211537 /************************** End of sqlite3.c ******************************/
211538
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -1150,11 +1150,11 @@
1150 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
1151 ** [sqlite_version()] and [sqlite_source_id()].
1152 */
1153 #define SQLITE_VERSION "3.24.0"
1154 #define SQLITE_VERSION_NUMBER 3024000
1155 #define SQLITE_SOURCE_ID "2018-05-30 01:14:20 86ee267ee86f5264774a9f215b1158aeaa2d605e77c205731b5ee3945d7de4c2"
1156
1157 /*
1158 ** CAPI3REF: Run-Time Library Version Numbers
1159 ** KEYWORDS: sqlite3_version sqlite3_sourceid
1160 **
@@ -1529,17 +1529,19 @@
1529 #define SQLITE_IOERR_AUTH (SQLITE_IOERR | (28<<8))
1530 #define SQLITE_IOERR_BEGIN_ATOMIC (SQLITE_IOERR | (29<<8))
1531 #define SQLITE_IOERR_COMMIT_ATOMIC (SQLITE_IOERR | (30<<8))
1532 #define SQLITE_IOERR_ROLLBACK_ATOMIC (SQLITE_IOERR | (31<<8))
1533 #define SQLITE_LOCKED_SHAREDCACHE (SQLITE_LOCKED | (1<<8))
1534 #define SQLITE_LOCKED_VTAB (SQLITE_LOCKED | (2<<8))
1535 #define SQLITE_BUSY_RECOVERY (SQLITE_BUSY | (1<<8))
1536 #define SQLITE_BUSY_SNAPSHOT (SQLITE_BUSY | (2<<8))
1537 #define SQLITE_CANTOPEN_NOTEMPDIR (SQLITE_CANTOPEN | (1<<8))
1538 #define SQLITE_CANTOPEN_ISDIR (SQLITE_CANTOPEN | (2<<8))
1539 #define SQLITE_CANTOPEN_FULLPATH (SQLITE_CANTOPEN | (3<<8))
1540 #define SQLITE_CANTOPEN_CONVPATH (SQLITE_CANTOPEN | (4<<8))
1541 #define SQLITE_CORRUPT_VTAB (SQLITE_CORRUPT | (1<<8))
1542 #define SQLITE_CORRUPT_SEQUENCE (SQLITE_CORRUPT | (2<<8))
1543 #define SQLITE_READONLY_RECOVERY (SQLITE_READONLY | (1<<8))
1544 #define SQLITE_READONLY_CANTLOCK (SQLITE_READONLY | (2<<8))
1545 #define SQLITE_READONLY_ROLLBACK (SQLITE_READONLY | (3<<8))
1546 #define SQLITE_READONLY_DBMOVED (SQLITE_READONLY | (4<<8))
1547 #define SQLITE_READONLY_CANTINIT (SQLITE_READONLY | (5<<8))
@@ -7317,10 +7319,14 @@
7319 sqlite3_uint64 colUsed; /* Input: Mask of columns used by statement */
7320 };
7321
7322 /*
7323 ** CAPI3REF: Virtual Table Scan Flags
7324 **
7325 ** Virtual table implementations are allowed to set the
7326 ** [sqlite3_index_info].idxFlags field to some combination of
7327 ** these bits.
7328 */
7329 #define SQLITE_INDEX_SCAN_UNIQUE 1 /* Scan visits at most 1 row */
7330
7331 /*
7332 ** CAPI3REF: Virtual Table Constraint Operator Codes
@@ -8180,15 +8186,23 @@
8186 /*
8187 ** CAPI3REF: Create A New Dynamic String Object
8188 ** CONSTRUCTOR: sqlite3_str
8189 **
8190 ** ^The [sqlite3_str_new(D)] interface allocates and initializes
8191 ** a new [sqlite3_str] object. To avoid memory leaks, the object returned by
 
 
8192 ** [sqlite3_str_new()] must be freed by a subsequent call to
8193 ** [sqlite3_str_finish(X)].
8194 **
8195 ** ^The [sqlite3_str_new(D)] interface always returns a pointer to a
8196 ** valid [sqlite3_str] object, though in the event of an out-of-memory
8197 ** error the returned object might be a special singleton that will
8198 ** silently reject new text, always return SQLITE_NOMEM from
8199 ** [sqlite3_str_errcode()], always return 0 for
8200 ** [sqlite3_str_length()], and always return NULL from
8201 ** [sqlite3_str_finish(X)]. It is always safe to use the value
8202 ** returned by [sqlite3_str_new(D)] as the sqlite3_str parameter
8203 ** to any of the other [sqlite3_str] methods.
8204 **
8205 ** The D parameter to [sqlite3_str_new(D)] may be NULL. If the
8206 ** D parameter in [sqlite3_str_new(D)] is not NULL, then the maximum
8207 ** length of the string contained in the [sqlite3_str] object will be
8208 ** the value set for [sqlite3_limit](D,[SQLITE_LIMIT_LENGTH]) instead
@@ -9550,15 +9564,15 @@
9564 **
9565 ** If the sqlite3_vtab_nochange(X) routine is called within the [xColumn]
9566 ** method of a [virtual table], then it returns true if and only if the
9567 ** column is being fetched as part of an UPDATE operation during which the
9568 ** column value will not change. Applications might use this to substitute
9569 ** a return value that is less expensive to compute and that the corresponding
9570 ** [xUpdate] method understands as a "no-change" value.
9571 **
9572 ** If the [xColumn] method calls sqlite3_vtab_nochange() and finds that
9573 ** the column is not changed by the UPDATE statement, then the xColumn
9574 ** method can optionally return without setting a result, without calling
9575 ** any of the [sqlite3_result_int|sqlite3_result_xxxxx() interfaces].
9576 ** In that case, [sqlite3_value_nochange(X)] will return true for the
9577 ** same column in the [xUpdate] method.
9578 */
@@ -14632,10 +14646,11 @@
14646 #define OP_Function 166 /* synopsis: r[P3]=func(r[P2@P5]) */
14647 #define OP_Trace 167
14648 #define OP_CursorHint 168
14649 #define OP_Noop 169
14650 #define OP_Explain 170
14651 #define OP_Abortable 171
14652
14653 /* Properties such as "out2" or "jump" that are specified in
14654 ** comments following the "case" for each opcode in the vdbe.c
14655 ** are encoded into bitvectors as follows:
14656 */
@@ -14665,11 +14680,11 @@
14680 /* 128 */ 0x04, 0x04, 0x00, 0x00, 0x10, 0x10, 0x10, 0x00,\
14681 /* 136 */ 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
14682 /* 144 */ 0x00, 0x06, 0x10, 0x00, 0x04, 0x1a, 0x00, 0x00,\
14683 /* 152 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
14684 /* 160 */ 0x00, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,\
14685 /* 168 */ 0x00, 0x00, 0x00, 0x00,}
14686
14687 /* The sqlite3P2Values() routine is able to run faster if it knows
14688 ** the value of the largest JUMP opcode. The smaller the maximum
14689 ** JUMP opcode the better, so the mkopcodeh.tcl script that
14690 ** generated this include file strives to group all JUMP opcodes
@@ -14707,10 +14722,15 @@
14722 SQLITE_PRIVATE void sqlite3VdbeVerifyNoResultRow(Vdbe *p);
14723 #else
14724 # define sqlite3VdbeVerifyNoMallocRequired(A,B)
14725 # define sqlite3VdbeVerifyNoResultRow(A)
14726 #endif
14727 #if defined(SQLITE_DEBUG)
14728 SQLITE_PRIVATE void sqlite3VdbeVerifyAbortable(Vdbe *p, int);
14729 #else
14730 # define sqlite3VdbeVerifyAbortable(A,B)
14731 #endif
14732 SQLITE_PRIVATE VdbeOp *sqlite3VdbeAddOpList(Vdbe*, int nOp, VdbeOpList const *aOp,int iLineno);
14733 #ifndef SQLITE_OMIT_EXPLAIN
14734 SQLITE_PRIVATE void sqlite3VdbeExplain(Parse*,u8,const char*,...);
14735 SQLITE_PRIVATE void sqlite3VdbeExplainPop(Parse*);
14736 SQLITE_PRIVATE int sqlite3VdbeExplainParent(Parse*);
@@ -18683,10 +18703,11 @@
18703 SQLITE_PRIVATE int sqlite3CreateFunc(sqlite3 *, const char *, int, int, void *,
18704 void (*)(sqlite3_context*,int,sqlite3_value **),
18705 void (*)(sqlite3_context*,int,sqlite3_value **), void (*)(sqlite3_context*),
18706 FuncDestructor *pDestructor
18707 );
18708 SQLITE_PRIVATE void sqlite3NoopDestructor(void*);
18709 SQLITE_PRIVATE void sqlite3OomFault(sqlite3*);
18710 SQLITE_PRIVATE void sqlite3OomClear(sqlite3*);
18711 SQLITE_PRIVATE int sqlite3ApiExit(sqlite3 *db, int);
18712 SQLITE_PRIVATE int sqlite3OpenTempDatabase(Parse *);
18713
@@ -18785,11 +18806,10 @@
18806 SQLITE_PRIVATE int sqlite3VtabCallCreate(sqlite3*, int, const char *, char **);
18807 SQLITE_PRIVATE int sqlite3VtabCallConnect(Parse*, Table*);
18808 SQLITE_PRIVATE int sqlite3VtabCallDestroy(sqlite3*, int, const char *);
18809 SQLITE_PRIVATE int sqlite3VtabBegin(sqlite3 *, VTable *);
18810 SQLITE_PRIVATE FuncDef *sqlite3VtabOverloadFunction(sqlite3 *,FuncDef*, int nArg, Expr*);
 
18811 SQLITE_PRIVATE sqlite3_int64 sqlite3StmtCurrentTime(sqlite3_context*);
18812 SQLITE_PRIVATE int sqlite3VdbeParameterIndex(Vdbe*, const char*, int);
18813 SQLITE_PRIVATE int sqlite3TransferBindings(sqlite3_stmt *, sqlite3_stmt *);
18814 SQLITE_PRIVATE void sqlite3ParserReset(Parse*);
18815 SQLITE_PRIVATE int sqlite3Reprepare(Vdbe*);
@@ -19715,10 +19735,11 @@
19735 i64 startTime; /* Time when query started - used for profiling */
19736 #endif
19737 int nOp; /* Number of instructions in the program */
19738 #ifdef SQLITE_DEBUG
19739 int rcApp; /* errcode set by sqlite3_result_error_code() */
19740 u32 nWrite; /* Number of write operations that have occurred */
19741 #endif
19742 u16 nResColumn; /* Number of columns in one row of the result set */
19743 u8 errorAction; /* Recovery action to do in case of an error */
19744 u8 minWriteFileFormat; /* Minimum file format for writable database files */
19745 u8 prepFlags; /* SQLITE_PREPARE_* flags */
@@ -19849,10 +19870,18 @@
19870 SQLITE_PRIVATE int sqlite3VdbeSorterRowkey(const VdbeCursor *, Mem *);
19871 SQLITE_PRIVATE int sqlite3VdbeSorterNext(sqlite3 *, const VdbeCursor *);
19872 SQLITE_PRIVATE int sqlite3VdbeSorterRewind(const VdbeCursor *, int *);
19873 SQLITE_PRIVATE int sqlite3VdbeSorterWrite(const VdbeCursor *, Mem *);
19874 SQLITE_PRIVATE int sqlite3VdbeSorterCompare(const VdbeCursor *, Mem *, int, int *);
19875
19876 #ifdef SQLITE_DEBUG
19877 SQLITE_PRIVATE void sqlite3VdbeIncrWriteCounter(Vdbe*, VdbeCursor*);
19878 SQLITE_PRIVATE void sqlite3VdbeAssertAbortable(Vdbe*);
19879 #else
19880 # define sqlite3VdbeIncrWriteCounter(V,C)
19881 # define sqlite3VdbeAssertAbortable(V)
19882 #endif
19883
19884 #if !defined(SQLITE_OMIT_SHARED_CACHE)
19885 SQLITE_PRIVATE void sqlite3VdbeEnter(Vdbe*);
19886 #else
19887 # define sqlite3VdbeEnter(X)
@@ -27537,16 +27566,26 @@
27566 return strAccumFinishRealloc(p);
27567 }
27568 }
27569 return p->zText;
27570 }
27571
27572 /*
27573 ** This singleton is an sqlite3_str object that is returned if
27574 ** sqlite3_malloc() fails to provide space for a real one. This
27575 ** sqlite3_str object accepts no new text and always returns
27576 ** an SQLITE_NOMEM error.
27577 */
27578 static sqlite3_str sqlite3OomStr = {
27579 0, 0, 0, 0, 0, SQLITE_NOMEM, 0
27580 };
27581
27582 /* Finalize a string created using sqlite3_str_new().
27583 */
27584 SQLITE_API char *sqlite3_str_finish(sqlite3_str *p){
27585 char *z;
27586 if( p!=0 && p!=&sqlite3OomStr ){
27587 z = sqlite3StrAccumFinish(p);
27588 sqlite3_free(p);
27589 }else{
27590 z = 0;
27591 }
@@ -27611,10 +27650,12 @@
27650 SQLITE_API sqlite3_str *sqlite3_str_new(sqlite3 *db){
27651 sqlite3_str *p = sqlite3_malloc64(sizeof(*p));
27652 if( p ){
27653 sqlite3StrAccumInit(p, 0, 0, 0,
27654 db ? db->aLimit[SQLITE_LIMIT_LENGTH] : SQLITE_MAX_LENGTH);
27655 }else{
27656 p = &sqlite3OomStr;
27657 }
27658 return p;
27659 }
27660
27661 /*
@@ -31409,10 +31450,11 @@
31450 /* 166 */ "Function" OpHelp("r[P3]=func(r[P2@P5])"),
31451 /* 167 */ "Trace" OpHelp(""),
31452 /* 168 */ "CursorHint" OpHelp(""),
31453 /* 169 */ "Noop" OpHelp(""),
31454 /* 170 */ "Explain" OpHelp(""),
31455 /* 171 */ "Abortable" OpHelp(""),
31456 };
31457 return azName[i];
31458 }
31459 #endif
31460
@@ -73732,11 +73774,11 @@
73774 pMem->flags = MEM_Int;
73775 }
73776 }
73777
73778 /* A no-op destructor */
73779 SQLITE_PRIVATE void sqlite3NoopDestructor(void *p){ UNUSED_PARAMETER(p); }
73780
73781 /*
73782 ** Set the value stored in *pMem should already be a NULL.
73783 ** Also store a pointer to go with it.
73784 */
@@ -75378,10 +75420,36 @@
75420 return ( v->db->mallocFailed || hasAbort==mayAbort || hasFkCounter
75421 || (hasCreateTable && hasInitCoroutine) );
75422 }
75423 #endif /* SQLITE_DEBUG - the sqlite3AssertMayAbort() function */
75424
75425 #ifdef SQLITE_DEBUG
75426 /*
75427 ** Increment the nWrite counter in the VDBE if the cursor is not an
75428 ** ephemeral cursor, or if the cursor argument is NULL.
75429 */
75430 SQLITE_PRIVATE void sqlite3VdbeIncrWriteCounter(Vdbe *p, VdbeCursor *pC){
75431 if( pC==0
75432 || (pC->eCurType!=CURTYPE_SORTER
75433 && pC->eCurType!=CURTYPE_PSEUDO
75434 && !pC->isEphemeral)
75435 ){
75436 p->nWrite++;
75437 }
75438 }
75439 #endif
75440
75441 #ifdef SQLITE_DEBUG
75442 /*
75443 ** Assert if an Abort at this point in time might result in a corrupt
75444 ** database.
75445 */
75446 SQLITE_PRIVATE void sqlite3VdbeAssertAbortable(Vdbe *p){
75447 assert( p->nWrite==0 || p->usesStmtJournal );
75448 }
75449 #endif
75450
75451 /*
75452 ** This routine is called after all opcodes have been inserted. It loops
75453 ** through all the opcodes and fixes up some details.
75454 **
75455 ** (1) For each jump instruction with a negative P2 value (a label)
@@ -75537,10 +75605,21 @@
75605 assert( p->aOp[i].opcode!=OP_ResultRow );
75606 }
75607 }
75608 #endif
75609
75610 /*
75611 ** Generate code (a single OP_Abortable opcode) that will
75612 ** verify that the VDBE program can safely call Abort in the current
75613 ** context.
75614 */
75615 #if defined(SQLITE_DEBUG)
75616 SQLITE_PRIVATE void sqlite3VdbeVerifyAbortable(Vdbe *p, int onError){
75617 if( onError==OE_Abort ) sqlite3VdbeAddOp0(p, OP_Abortable);
75618 }
75619 #endif
75620
75621 /*
75622 ** This function returns a pointer to the array of opcodes associated with
75623 ** the Vdbe passed as the first argument. It is the callers responsibility
75624 ** to arrange for the returned array to be eventually freed using the
75625 ** vdbeFreeOpArray() function.
@@ -77770,10 +77849,13 @@
77849 }
77850 #endif
77851 sqlite3DbFree(db, p->zErrMsg);
77852 p->zErrMsg = 0;
77853 p->pResultSet = 0;
77854 #ifdef SQLITE_DEBUG
77855 p->nWrite = 0;
77856 #endif
77857
77858 /* Save profiling information from this VDBE run.
77859 */
77860 #ifdef VDBE_PROFILE
77861 {
@@ -78692,17 +78774,14 @@
78774 return 0;
78775 }else{
78776 i64 y;
78777 double s;
78778 if( r<-9223372036854775808.0 ) return +1;
78779 if( r>=9223372036854775808.0 ) return -1;
78780 y = (i64)r;
78781 if( i<y ) return -1;
78782 if( i>y ) return +1;
 
 
 
78783 s = (double)i;
78784 if( s<r ) return -1;
78785 if( s>r ) return +1;
78786 return 0;
78787 }
@@ -80376,32 +80455,10 @@
80455 if( rc ) *piTime = 0;
80456 }
80457 return *piTime;
80458 }
80459
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80460 /*
80461 ** Create a new aggregate context for p and return a pointer to
80462 ** its pMem->z element.
80463 */
80464 static SQLITE_NOINLINE void *createAggContext(sqlite3_context *p, int nByte){
@@ -82773,10 +82830,13 @@
82830 ** value in register P3 is not NULL, then this routine is a no-op.
82831 ** The P5 parameter should be 1.
82832 */
82833 case OP_HaltIfNull: { /* in3 */
82834 pIn3 = &aMem[pOp->p3];
82835 #ifdef SQLITE_DEBUG
82836 if( pOp->p2==OE_Abort ){ sqlite3VdbeAssertAbortable(p); }
82837 #endif
82838 if( (pIn3->flags & MEM_Null)==0 ) break;
82839 /* Fall through into OP_Halt */
82840 }
82841
82842 /* Opcode: Halt P1 P2 * P4 P5
@@ -82812,10 +82872,13 @@
82872 case OP_Halt: {
82873 VdbeFrame *pFrame;
82874 int pcx;
82875
82876 pcx = (int)(pOp - aOp);
82877 #ifdef SQLITE_DEBUG
82878 if( pOp->p2==OE_Abort ){ sqlite3VdbeAssertAbortable(p); }
82879 #endif
82880 if( pOp->p1==SQLITE_OK && p->pFrame ){
82881 /* Halt the sub-program. Return control to the parent frame. */
82882 pFrame = p->pFrame;
82883 p->pFrame = pFrame->pParent;
82884 p->nFrame--;
@@ -85182,10 +85245,12 @@
85245 **
85246 ** A transaction must be started before executing this opcode.
85247 */
85248 case OP_SetCookie: {
85249 Db *pDb;
85250
85251 sqlite3VdbeIncrWriteCounter(p, 0);
85252 assert( pOp->p2<SQLITE_N_BTREE_META );
85253 assert( pOp->p1>=0 && pOp->p1<db->nDb );
85254 assert( DbMaskTest(p->btreeMask, pOp->p1) );
85255 assert( p->readOnly==0 );
85256 pDb = &db->aDb[pOp->p1];
@@ -86145,15 +86210,12 @@
86210 v = 0;
86211 res = 0;
86212 pOut = out2Prerelease(p, pOp);
86213 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
86214 pC = p->apCsr[pOp->p1];
 
 
 
 
86215 assert( pC!=0 );
86216 assert( pC->isTable );
86217 assert( pC->eCurType==CURTYPE_BTREE );
86218 assert( pC->uc.pCursor!=0 );
86219 {
86220 /* The next rowid or record number (different terms for the same
86221 ** thing) is obtained in a two-step algorithm.
@@ -86318,10 +86380,11 @@
86380 assert( pC->eCurType==CURTYPE_BTREE );
86381 assert( pC->uc.pCursor!=0 );
86382 assert( (pOp->p5 & OPFLAG_ISNOOP) || pC->isTable );
86383 assert( pOp->p4type==P4_TABLE || pOp->p4type>=P4_STATIC );
86384 REGISTER_TRACE(pOp->p2, pData);
86385 sqlite3VdbeIncrWriteCounter(p, pC);
86386
86387 if( pOp->opcode==OP_Insert ){
86388 pKey = &aMem[pOp->p3];
86389 assert( pKey->flags & MEM_Int );
86390 assert( memIsValid(pKey) );
@@ -86432,10 +86495,11 @@
86495 pC = p->apCsr[pOp->p1];
86496 assert( pC!=0 );
86497 assert( pC->eCurType==CURTYPE_BTREE );
86498 assert( pC->uc.pCursor!=0 );
86499 assert( pC->deferredMoveto==0 );
86500 sqlite3VdbeIncrWriteCounter(p, pC);
86501
86502 #ifdef SQLITE_DEBUG
86503 if( pOp->p4type==P4_TABLE && HasRowid(pOp->p4.pTab) && pOp->p5==0 ){
86504 /* If p5 is zero, the seek operation that positioned the cursor prior to
86505 ** OP_Delete will have also set the pC->movetoTarget field to the rowid of
@@ -87050,10 +87114,11 @@
87114 VdbeCursor *pC;
87115 BtreePayload x;
87116
87117 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
87118 pC = p->apCsr[pOp->p1];
87119 sqlite3VdbeIncrWriteCounter(p, pC);
87120 assert( pC!=0 );
87121 assert( isSorter(pC)==(pOp->opcode==OP_SorterInsert) );
87122 pIn2 = &aMem[pOp->p2];
87123 assert( pIn2->flags & MEM_Blob );
87124 if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
@@ -87096,10 +87161,11 @@
87161 assert( pOp->p2>0 && pOp->p2+pOp->p3<=(p->nMem+1 - p->nCursor)+1 );
87162 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
87163 pC = p->apCsr[pOp->p1];
87164 assert( pC!=0 );
87165 assert( pC->eCurType==CURTYPE_BTREE );
87166 sqlite3VdbeIncrWriteCounter(p, pC);
87167 pCrsr = pC->uc.pCursor;
87168 assert( pCrsr!=0 );
87169 assert( pOp->p5==0 );
87170 r.pKeyInfo = pC->pKeyInfo;
87171 r.nField = (u16)pOp->p3;
@@ -87318,10 +87384,11 @@
87384 */
87385 case OP_Destroy: { /* out2 */
87386 int iMoved;
87387 int iDb;
87388
87389 sqlite3VdbeIncrWriteCounter(p, 0);
87390 assert( p->readOnly==0 );
87391 assert( pOp->p1>1 );
87392 pOut = out2Prerelease(p, pOp);
87393 pOut->flags = MEM_Null;
87394 if( db->nVdbeRead > db->nVDestroy+1 ){
@@ -87367,10 +87434,11 @@
87434 ** See also: Destroy
87435 */
87436 case OP_Clear: {
87437 int nChange;
87438
87439 sqlite3VdbeIncrWriteCounter(p, 0);
87440 nChange = 0;
87441 assert( p->readOnly==0 );
87442 assert( DbMaskTest(p->btreeMask, pOp->p2) );
87443 rc = sqlite3BtreeClearTable(
87444 db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &nChange : 0)
@@ -87423,10 +87491,11 @@
87491 */
87492 case OP_CreateBtree: { /* out2 */
87493 int pgno;
87494 Db *pDb;
87495
87496 sqlite3VdbeIncrWriteCounter(p, 0);
87497 pOut = out2Prerelease(p, pOp);
87498 pgno = 0;
87499 assert( pOp->p3==BTREE_INTKEY || pOp->p3==BTREE_BLOBKEY );
87500 assert( pOp->p1>=0 && pOp->p1<db->nDb );
87501 assert( DbMaskTest(p->btreeMask, pOp->p1) );
@@ -87442,10 +87511,11 @@
87511 /* Opcode: SqlExec * * * P4 *
87512 **
87513 ** Run the SQL statement or statements specified in the P4 string.
87514 */
87515 case OP_SqlExec: {
87516 sqlite3VdbeIncrWriteCounter(p, 0);
87517 db->nSqlExec++;
87518 rc = sqlite3_exec(db, pOp->p4.z, 0, 0, 0);
87519 db->nSqlExec--;
87520 if( rc ) goto abort_due_to_error;
87521 break;
@@ -87531,10 +87601,11 @@
87601 ** is dropped from disk (using the Destroy opcode) in order to keep
87602 ** the internal representation of the
87603 ** schema consistent with what is on disk.
87604 */
87605 case OP_DropTable: {
87606 sqlite3VdbeIncrWriteCounter(p, 0);
87607 sqlite3UnlinkAndDeleteTable(db, pOp->p1, pOp->p4.z);
87608 break;
87609 }
87610
87611 /* Opcode: DropIndex P1 * * P4 *
@@ -87544,10 +87615,11 @@
87615 ** is dropped from disk (using the Destroy opcode)
87616 ** in order to keep the internal representation of the
87617 ** schema consistent with what is on disk.
87618 */
87619 case OP_DropIndex: {
87620 sqlite3VdbeIncrWriteCounter(p, 0);
87621 sqlite3UnlinkAndDeleteIndex(db, pOp->p1, pOp->p4.z);
87622 break;
87623 }
87624
87625 /* Opcode: DropTrigger P1 * * P4 *
@@ -87557,10 +87629,11 @@
87629 ** is dropped from disk (using the Destroy opcode) in order to keep
87630 ** the internal representation of the
87631 ** schema consistent with what is on disk.
87632 */
87633 case OP_DropTrigger: {
87634 sqlite3VdbeIncrWriteCounter(p, 0);
87635 sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p4.z);
87636 break;
87637 }
87638
87639
@@ -88603,11 +88676,11 @@
88676 ** the current row of the virtual-table of cursor P1.
88677 **
88678 ** If the VColumn opcode is being used to fetch the value of
88679 ** an unchanging column during an UPDATE operation, then the P5
88680 ** value is 1. Otherwise, P5 is 0. The P5 value is returned
88681 ** by sqlite3_vtab_nochange() routine and can be used
88682 ** by virtual table implementations to return special "no-change"
88683 ** marks which can be more efficient, depending on the virtual table.
88684 */
88685 case OP_VColumn: {
88686 sqlite3_vtab *pVtab;
@@ -88766,10 +88839,11 @@
88839
88840 assert( pOp->p2==1 || pOp->p5==OE_Fail || pOp->p5==OE_Rollback
88841 || pOp->p5==OE_Abort || pOp->p5==OE_Ignore || pOp->p5==OE_Replace
88842 );
88843 assert( p->readOnly==0 );
88844 sqlite3VdbeIncrWriteCounter(p, 0);
88845 pVtab = pOp->p4.pVtab->pVtab;
88846 if( pVtab==0 || NEVER(pVtab->pModule==0) ){
88847 rc = SQLITE_LOCKED;
88848 goto abort_due_to_error;
88849 }
@@ -89082,10 +89156,26 @@
89156 pOp->p4.pExpr, aMem);
89157 }
89158 break;
89159 }
89160 #endif /* SQLITE_ENABLE_CURSOR_HINTS */
89161
89162 #ifdef SQLITE_DEBUG
89163 /* Opcode: Abortable * * * * *
89164 **
89165 ** Verify that an Abort can happen. Assert if an Abort at this point
89166 ** might cause database corruption. This opcode only appears in debugging
89167 ** builds.
89168 **
89169 ** An Abort is safe if either there have been no writes, or if there is
89170 ** an active statement journal.
89171 */
89172 case OP_Abortable: {
89173 sqlite3VdbeAssertAbortable(p);
89174 break;
89175 }
89176 #endif
89177
89178 /* Opcode: Noop * * * * *
89179 **
89180 ** Do nothing. This instruction is often useful as a jump
89181 ** destination.
@@ -89094,12 +89184,13 @@
89184 ** The magic Explain opcode are only inserted when explain==2 (which
89185 ** is to say when the EXPLAIN QUERY PLAN syntax is used.)
89186 ** This opcode records information from the optimizer. It is the
89187 ** the same as a no-op. This opcodesnever appears in a real VM program.
89188 */
89189 default: { /* This is really OP_Noop, OP_Explain */
89190 assert( pOp->opcode==OP_Noop || pOp->opcode==OP_Explain );
89191
89192 break;
89193 }
89194
89195 /*****************************************************************************
89196 ** The cases of the switch statement above this line should all be indented
@@ -98641,11 +98732,11 @@
98732 }
98733 case TK_SPAN:
98734 case TK_COLLATE:
98735 case TK_UPLUS: {
98736 pExpr = pExpr->pLeft;
98737 goto expr_code_doover; /* 2018-04-28: Prevent deep recursion. OSSFuzz. */
98738 }
98739
98740 case TK_TRIGGER: {
98741 /* If the opcode is TK_TRIGGER, then the expression is a reference
98742 ** to a column in the new.* or old.* pseudo-tables available to
@@ -106664,10 +106755,11 @@
106755 /* Open the table. Loop through all rows of the table, inserting index
106756 ** records into the sorter. */
106757 sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead);
106758 addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iTab, 0); VdbeCoverage(v);
106759 regRecord = sqlite3GetTempReg(pParse);
106760 sqlite3MultiWrite(pParse);
106761
106762 sqlite3GenerateIndexKey(pParse,pIndex,iTab,regRecord,0,&iPartIdxLabel,0,0);
106763 sqlite3VdbeAddOp2(v, OP_SorterInsert, iSorter, regRecord);
106764 sqlite3ResolvePartIdxLabel(pParse, iPartIdxLabel);
106765 sqlite3VdbeAddOp2(v, OP_Next, iTab, addr1+1); VdbeCoverage(v);
@@ -106677,16 +106769,17 @@
106769 (char *)pKey, P4_KEYINFO);
106770 sqlite3VdbeChangeP5(v, OPFLAG_BULKCSR|((memRootPage>=0)?OPFLAG_P2ISREG:0));
106771
106772 addr1 = sqlite3VdbeAddOp2(v, OP_SorterSort, iSorter, 0); VdbeCoverage(v);
106773 if( IsUniqueIndex(pIndex) ){
106774 int j2 = sqlite3VdbeGoto(v, 1);
 
106775 addr2 = sqlite3VdbeCurrentAddr(v);
106776 sqlite3VdbeVerifyAbortable(v, OE_Abort);
106777 sqlite3VdbeAddOp4Int(v, OP_SorterCompare, iSorter, j2, regRecord,
106778 pIndex->nKeyCol); VdbeCoverage(v);
106779 sqlite3UniqueConstraint(pParse, OE_Abort, pIndex);
106780 sqlite3VdbeJumpHere(v, j2);
106781 }else{
106782 addr2 = sqlite3VdbeCurrentAddr(v);
106783 }
106784 sqlite3VdbeAddOp3(v, OP_SorterData, iSorter, regRecord, iIdx);
106785 sqlite3VdbeAddOp1(v, OP_SeekEnd, iIdx);
@@ -108723,14 +108816,16 @@
108816 ** new entry to the hash table and return it.
108817 */
108818 if( createFlag && bestScore<FUNC_PERFECT_MATCH &&
108819 (pBest = sqlite3DbMallocZero(db, sizeof(*pBest)+nName+1))!=0 ){
108820 FuncDef *pOther;
108821 u8 *z;
108822 pBest->zName = (const char*)&pBest[1];
108823 pBest->nArg = (u16)nArg;
108824 pBest->funcFlags = enc;
108825 memcpy((char*)&pBest[1], zName, nName+1);
108826 for(z=(u8*)pBest->zName; *z; z++) *z = sqlite3UpperToLower[*z];
108827 pOther = (FuncDef*)sqlite3HashInsert(&db->aFunc, pBest->zName, pBest);
108828 if( pOther==pBest ){
108829 sqlite3DbFree(db, pBest);
108830 sqlite3OomFault(db);
108831 return 0;
@@ -109359,17 +109454,20 @@
109454 /* Delete the row */
109455 #ifndef SQLITE_OMIT_VIRTUALTABLE
109456 if( IsVirtual(pTab) ){
109457 const char *pVTab = (const char *)sqlite3GetVTable(db, pTab);
109458 sqlite3VtabMakeWritable(pParse, pTab);
 
 
109459 assert( eOnePass==ONEPASS_OFF || eOnePass==ONEPASS_SINGLE );
109460 sqlite3MayAbort(pParse);
109461 if( eOnePass==ONEPASS_SINGLE ){
109462 sqlite3VdbeAddOp1(v, OP_Close, iTabCur);
109463 if( sqlite3IsToplevel(pParse) ){
109464 pParse->isMultiWrite = 0;
109465 }
109466 }
109467 sqlite3VdbeAddOp4(v, OP_VUpdate, 0, 1, iKey, pVTab, P4_VTAB);
109468 sqlite3VdbeChangeP5(v, OE_Abort);
109469 }else
109470 #endif
109471 {
109472 int count = (pParse->nested==0); /* True to count changes */
109473 sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur,
@@ -112000,10 +112098,16 @@
112098 int i; /* Iterator variable */
112099 Vdbe *v = sqlite3GetVdbe(pParse); /* Vdbe to add code to */
112100 int iCur = pParse->nTab - 1; /* Cursor number to use */
112101 int iOk = sqlite3VdbeMakeLabel(v); /* jump here if parent key found */
112102
112103 sqlite3VdbeVerifyAbortable(v,
112104 (!pFKey->isDeferred
112105 && !(pParse->db->flags & SQLITE_DeferFKs)
112106 && !pParse->pToplevel
112107 && !pParse->isMultiWrite) ? OE_Abort : OE_Ignore);
112108
112109 /* If nIncr is less than zero, then check at runtime if there are any
112110 ** outstanding constraints to resolve. If there are not, there is no need
112111 ** to check if deleting this row resolves any outstanding violations.
112112 **
112113 ** Check if any of the key columns in the child table row are NULL. If
@@ -112407,10 +112511,11 @@
112511 ** If the SQLITE_DeferFKs flag is set, then this is not required, as
112512 ** the statement transaction will not be rolled back even if FK
112513 ** constraints are violated.
112514 */
112515 if( (db->flags & SQLITE_DeferFKs)==0 ){
112516 sqlite3VdbeVerifyAbortable(v, OE_Abort);
112517 sqlite3VdbeAddOp2(v, OP_FkIfZero, 0, sqlite3VdbeCurrentAddr(v)+2);
112518 VdbeCoverage(v);
112519 sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_FOREIGNKEY,
112520 OE_Abort, 0, P4_STATIC, P5_ConstraintFK);
112521 }
@@ -113319,15 +113424,30 @@
113424 Parse *pParse, /* Parsing context */
113425 int iDb, /* Index of the database holding pTab */
113426 Table *pTab /* The table we are writing to */
113427 ){
113428 int memId = 0; /* Register holding maximum rowid */
113429 assert( pParse->db->aDb[iDb].pSchema!=0 );
113430 if( (pTab->tabFlags & TF_Autoincrement)!=0
113431 && (pParse->db->mDbFlags & DBFLAG_Vacuum)==0
113432 ){
113433 Parse *pToplevel = sqlite3ParseToplevel(pParse);
113434 AutoincInfo *pInfo;
113435 Table *pSeqTab = pParse->db->aDb[iDb].pSchema->pSeqTab;
113436
113437 /* Verify that the sqlite_sequence table exists and is an ordinary
113438 ** rowid table with exactly two columns.
113439 ** Ticket d8dc2b3a58cd5dc2918a1d4acb 2018-05-23 */
113440 if( pSeqTab==0
113441 || !HasRowid(pSeqTab)
113442 || IsVirtual(pSeqTab)
113443 || pSeqTab->nCol!=2
113444 ){
113445 pParse->nErr++;
113446 pParse->rc = SQLITE_CORRUPT_SEQUENCE;
113447 return 0;
113448 }
113449
113450 pInfo = pToplevel->pAinc;
113451 while( pInfo && pInfo->pTab!=pTab ){ pInfo = pInfo->pNext; }
113452 if( pInfo==0 ){
113453 pInfo = sqlite3DbMallocRawNN(pParse->db, sizeof(*pInfo));
@@ -114498,10 +114618,11 @@
114618 for(i=0; i<pCheck->nExpr; i++){
114619 int allOk;
114620 Expr *pExpr = pCheck->a[i].pExpr;
114621 if( aiChng && checkConstraintUnchanged(pExpr, aiChng, pkChng) ) continue;
114622 allOk = sqlite3VdbeMakeLabel(v);
114623 sqlite3VdbeVerifyAbortable(v, onError);
114624 sqlite3ExprIfTrue(pParse, pExpr, allOk, SQLITE_JUMPIFNULL);
114625 if( onError==OE_Ignore ){
114626 sqlite3VdbeGoto(v, ignoreDest);
114627 }else{
114628 char *zName = pCheck->a[i].zName;
@@ -114607,10 +114728,11 @@
114728 }
114729
114730 /* Check to see if the new rowid already exists in the table. Skip
114731 ** the following conflict logic if it does not. */
114732 VdbeNoopComment((v, "uniqueness check for ROWID"));
114733 sqlite3VdbeVerifyAbortable(v, onError);
114734 sqlite3VdbeAddOp3(v, OP_NotExists, iDataCur, addrRowidOk, regNewData);
114735 VdbeCoverage(v);
114736
114737 switch( onError ){
114738 default: {
@@ -114819,10 +114941,11 @@
114941 continue;
114942 }
114943
114944 /* Check to see if the new index entry will be unique */
114945 sqlite3ExprCachePush(pParse);
114946 sqlite3VdbeVerifyAbortable(v, onError);
114947 sqlite3VdbeAddOp4Int(v, OP_NoConflict, iThisCur, addrUniqueOk,
114948 regIdx, pIdx->nKeyCol); VdbeCoverage(v);
114949
114950 /* Generate code to handle collisions */
114951 regR = (pIdx==pPk) ? regIdx : sqlite3GetTempRange(pParse, nPkField);
@@ -114905,13 +115028,15 @@
115028 break;
115029 }
115030 default: {
115031 Trigger *pTrigger = 0;
115032 assert( onError==OE_Replace );
 
115033 if( db->flags&SQLITE_RecTriggers ){
115034 pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0);
115035 }
115036 if( pTrigger || sqlite3FkRequired(pParse, pTab, 0, 0) ){
115037 sqlite3MultiWrite(pParse);
115038 }
115039 sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur,
115040 regR, nPkField, 0, OE_Replace,
115041 (pIdx==pPk ? ONEPASS_SINGLE : ONEPASS_OFF), iThisCur);
115042 seenReplace = 1;
@@ -115428,10 +115553,11 @@
115553 u8 insFlags;
115554 sqlite3OpenTable(pParse, iSrc, iDbSrc, pSrc, OP_OpenRead);
115555 emptySrcTest = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0); VdbeCoverage(v);
115556 if( pDest->iPKey>=0 ){
115557 addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid);
115558 sqlite3VdbeVerifyAbortable(v, onError);
115559 addr2 = sqlite3VdbeAddOp3(v, OP_NotExists, iDest, 0, regRowid);
115560 VdbeCoverage(v);
115561 sqlite3RowidConstraint(pParse, onError, pDest);
115562 sqlite3VdbeJumpHere(v, addr2);
115563 autoIncStep(pParse, regAutoinc, regRowid);
@@ -129709,11 +129835,11 @@
129835 int regArg; /* First register in VUpdate arg array */
129836 int regRec; /* Register in which to assemble record */
129837 int regRowid; /* Register for ephem table rowid */
129838 int iCsr = pSrc->a[0].iCursor; /* Cursor used for virtual table scan */
129839 int aDummy[2]; /* Unused arg for sqlite3WhereOkOnePass() */
129840 int eOnePass; /* True to use onepass strategy */
129841 int addr; /* Address of OP_OpenEphemeral */
129842
129843 /* Allocate nArg registers in which to gather the arguments for VUpdate. Then
129844 ** create and open the ephemeral table in which the records created from
129845 ** these arguments will be temporarily stored. */
@@ -129754,16 +129880,20 @@
129880 iPk = pPk->aiColumn[0];
129881 sqlite3VdbeAddOp3(v, OP_VColumn, iCsr, iPk, regArg);
129882 sqlite3VdbeAddOp2(v, OP_SCopy, regArg+2+iPk, regArg+1);
129883 }
129884
129885 eOnePass = sqlite3WhereOkOnePass(pWInfo, aDummy);
129886
129887 /* There is no ONEPASS_MULTI on virtual tables */
129888 assert( eOnePass==ONEPASS_OFF || eOnePass==ONEPASS_SINGLE );
129889
129890 if( eOnePass ){
129891 /* If using the onepass strategy, no-op out the OP_OpenEphemeral coded
129892 ** above. */
129893 sqlite3VdbeChangeToNoop(v, addr);
129894 sqlite3VdbeAddOp1(v, OP_Close, iCsr);
129895 }else{
129896 /* Create a record from the argument register contents and insert it into
129897 ** the ephemeral table. */
129898 sqlite3MultiWrite(pParse);
129899 sqlite3VdbeAddOp3(v, OP_MakeRecord, regArg, nArg, regRec);
@@ -129775,11 +129905,11 @@
129905 sqlite3VdbeAddOp2(v, OP_NewRowid, ephemTab, regRowid);
129906 sqlite3VdbeAddOp3(v, OP_Insert, ephemTab, regRec, regRowid);
129907 }
129908
129909
129910 if( eOnePass==ONEPASS_OFF ){
129911 /* End the virtual table scan */
129912 sqlite3WhereEnd(pWInfo);
129913
129914 /* Begin scannning through the ephemeral table. */
129915 addr = sqlite3VdbeAddOp1(v, OP_Rewind, ephemTab); VdbeCoverage(v);
@@ -129795,11 +129925,11 @@
129925 sqlite3VdbeChangeP5(v, onError==OE_Default ? OE_Abort : onError);
129926 sqlite3MayAbort(pParse);
129927
129928 /* End of the ephemeral table scan. Or, if using the onepass strategy,
129929 ** jump to here if the scan visited zero rows. */
129930 if( eOnePass==ONEPASS_OFF ){
129931 sqlite3VdbeAddOp2(v, OP_Next, ephemTab, addr+1); VdbeCoverage(v);
129932 sqlite3VdbeJumpHere(v, addr);
129933 sqlite3VdbeAddOp2(v, OP_Close, ephemTab, 0);
129934 }else{
129935 sqlite3WhereEnd(pWInfo);
@@ -130038,10 +130168,11 @@
130168 k = sqlite3ColumnOfIndex(pIdx, pPk->aiColumn[i]);
130169 sqlite3VdbeAddOp3(v, OP_Column, iCur, k, iPk+i);
130170 VdbeComment((v, "%s.%s", pIdx->zName,
130171 pTab->aCol[pPk->aiColumn[i]].zName));
130172 }
130173 sqlite3VdbeVerifyAbortable(v, OE_Abort);
130174 i = sqlite3VdbeAddOp4Int(v, OP_Found, iDataCur, 0, iPk, nPk);
130175 VdbeCoverage(v);
130176 sqlite3VdbeAddOp4(v, OP_Halt, SQLITE_CORRUPT, OE_Abort, 0,
130177 "corrupt database", P4_STATIC);
130178 sqlite3VdbeJumpHere(v, i);
@@ -131490,13 +131621,10 @@
131621 sqlite3_module *pMod;
131622 void (*xSFunc)(sqlite3_context*,int,sqlite3_value**) = 0;
131623 void *pArg = 0;
131624 FuncDef *pNew;
131625 int rc = 0;
 
 
 
131626
131627 /* Check to see the left operand is a column in a virtual table */
131628 if( NEVER(pExpr==0) ) return pDef;
131629 if( pExpr->op!=TK_COLUMN ) return pDef;
131630 pTab = pExpr->pTab;
@@ -131507,20 +131635,26 @@
131635 assert( pVtab->pModule!=0 );
131636 pMod = (sqlite3_module *)pVtab->pModule;
131637 if( pMod->xFindFunction==0 ) return pDef;
131638
131639 /* Call the xFindFunction method on the virtual table implementation
131640 ** to see if the implementation wants to overload this function.
131641 **
131642 ** Though undocumented, we have historically always invoked xFindFunction
131643 ** with an all lower-case function name. Continue in this tradition to
131644 ** avoid any chance of an incompatibility.
131645 */
131646 #ifdef SQLITE_DEBUG
131647 {
131648 int i;
131649 for(i=0; pDef->zName[i]; i++){
131650 unsigned char x = (unsigned char)pDef->zName[i];
131651 assert( x==sqlite3UpperToLower[x] );
131652 }
131653 }
131654 #endif
131655 rc = pMod->xFindFunction(pVtab, nArg, pDef->zName, &xSFunc, &pArg);
131656 if( rc==0 ){
131657 return pDef;
131658 }
131659
131660 /* Create a new ephemeral function definition for the overloaded
@@ -138881,18 +139015,20 @@
139015 pNew->nLTerm = 1;
139016 pNew->aLTerm[0] = pTerm;
139017 /* TUNING: One-time cost for computing the automatic index is
139018 ** estimated to be X*N*log2(N) where N is the number of rows in
139019 ** the table being indexed and where X is 7 (LogEst=28) for normal
139020 ** tables or 0.5 (LogEst=-10) for views and subqueries. The value
139021 ** of X is smaller for views and subqueries so that the query planner
139022 ** will be more aggressive about generating automatic indexes for
139023 ** those objects, since there is no opportunity to add schema
139024 ** indexes on subqueries and views. */
139025 pNew->rSetup = rLogSize + rSize;
139026 if( pTab->pSelect==0 && (pTab->tabFlags & TF_Ephemeral)==0 ){
139027 pNew->rSetup += 28;
139028 }else{
139029 pNew->rSetup -= 10;
139030 }
139031 ApplyCostMultiplier(pNew->rSetup, pTab->costMult);
139032 if( pNew->rSetup<0 ) pNew->rSetup = 0;
139033 /* TUNING: Each index lookup yields 20 rows in the table. This
139034 ** is more than the usual guess of 10 rows, since we have no way
@@ -140024,16 +140160,19 @@
140160 Bitmask maskNew; /* Mask of src visited by (..) */
140161 Bitmask revMask = 0; /* Mask of rev-order loops for (..) */
140162
140163 if( (pWLoop->prereq & ~pFrom->maskLoop)!=0 ) continue;
140164 if( (pWLoop->maskSelf & pFrom->maskLoop)!=0 ) continue;
140165 if( (pWLoop->wsFlags & WHERE_AUTO_INDEX)!=0 && pFrom->nRow<3 ){
140166 /* Do not use an automatic index if the this loop is expected
140167 ** to run less than 1.25 times. It is tempting to also exclude
140168 ** automatic index usage on an outer loop, but sometimes an automatic
140169 ** index is useful in the outer loop of a correlated subquery. */
140170 assert( 10==sqlite3LogEst(2) );
140171 continue;
140172 }
140173
140174 /* At this point, pWLoop is a candidate to be the next loop.
140175 ** Compute its cost */
140176 rUnsorted = sqlite3LogEstAdd(pWLoop->rSetup,pWLoop->rRun + pFrom->nRow);
140177 rUnsorted = sqlite3LogEstAdd(rUnsorted, pFrom->rUnsorted);
140178 nOut = pFrom->nRow + pWLoop->nOut;
@@ -148446,23 +148585,25 @@
148585 return SQLITE_MISUSE_BKPT;
148586 }
148587 #endif
148588 sqlite3_mutex_enter(db->mutex);
148589 if( xDestroy ){
148590 pArg = (FuncDestructor *)sqlite3Malloc(sizeof(FuncDestructor));
148591 if( !pArg ){
148592 sqlite3OomFault(db);
148593 xDestroy(p);
148594 goto out;
148595 }
148596 pArg->nRef = 0;
148597 pArg->xDestroy = xDestroy;
148598 pArg->pUserData = p;
148599 }
148600 rc = sqlite3CreateFunc(db, zFunc, nArg, enc, p, xSFunc, xStep, xFinal, pArg);
148601 if( pArg && pArg->nRef==0 ){
148602 assert( rc!=SQLITE_OK );
148603 xDestroy(p);
148604 sqlite3_free(pArg);
148605 }
148606
148607 out:
148608 rc = sqlite3ApiExit(db, rc);
148609 sqlite3_mutex_leave(db->mutex);
@@ -148495,10 +148636,32 @@
148636 sqlite3_mutex_leave(db->mutex);
148637 return rc;
148638 }
148639 #endif
148640
148641
148642 /*
148643 ** The following is the implementation of an SQL function that always
148644 ** fails with an error message stating that the function is used in the
148645 ** wrong context. The sqlite3_overload_function() API might construct
148646 ** SQL function that use this routine so that the functions will exist
148647 ** for name resolution but are actually overloaded by the xFindFunction
148648 ** method of virtual tables.
148649 */
148650 static void sqlite3InvalidFunction(
148651 sqlite3_context *context, /* The function calling context */
148652 int NotUsed, /* Number of arguments to the function */
148653 sqlite3_value **NotUsed2 /* Value of each argument */
148654 ){
148655 const char *zName = (const char*)sqlite3_user_data(context);
148656 char *zErr;
148657 UNUSED_PARAMETER2(NotUsed, NotUsed2);
148658 zErr = sqlite3_mprintf(
148659 "unable to use function %s in the requested context", zName);
148660 sqlite3_result_error(context, zErr, -1);
148661 sqlite3_free(zErr);
148662 }
148663
148664 /*
148665 ** Declare that a function has been overloaded by a virtual table.
148666 **
148667 ** If the function already exists as a regular global function, then
@@ -148513,25 +148676,26 @@
148676 SQLITE_API int sqlite3_overload_function(
148677 sqlite3 *db,
148678 const char *zName,
148679 int nArg
148680 ){
148681 int rc;
148682 char *zCopy;
148683
148684 #ifdef SQLITE_ENABLE_API_ARMOR
148685 if( !sqlite3SafetyCheckOk(db) || zName==0 || nArg<-2 ){
148686 return SQLITE_MISUSE_BKPT;
148687 }
148688 #endif
148689 sqlite3_mutex_enter(db->mutex);
148690 rc = sqlite3FindFunction(db, zName, nArg, SQLITE_UTF8, 0)!=0;
 
 
 
 
148691 sqlite3_mutex_leave(db->mutex);
148692 if( rc ) return SQLITE_OK;
148693 zCopy = sqlite3_mprintf(zName);
148694 if( zCopy==0 ) return SQLITE_NOMEM;
148695 return sqlite3_create_function_v2(db, zName, nArg, SQLITE_UTF8,
148696 zCopy, sqlite3InvalidFunction, 0, 0, sqlite3_free);
148697 }
148698
148699 #ifndef SQLITE_OMIT_TRACE
148700 /*
148701 ** Register a trace function. The pArg from the previously registered trace
@@ -170371,18 +170535,19 @@
170535 ** in the table name is replaced with the user-supplied name of the r-tree
170536 ** table.
170537 **
170538 ** CREATE TABLE %_node(nodeno INTEGER PRIMARY KEY, data BLOB)
170539 ** CREATE TABLE %_parent(nodeno INTEGER PRIMARY KEY, parentnode INTEGER)
170540 ** CREATE TABLE %_rowid(rowid INTEGER PRIMARY KEY, nodeno INTEGER, ...)
170541 **
170542 ** The data for each node of the r-tree structure is stored in the %_node
170543 ** table. For each node that is not the root node of the r-tree, there is
170544 ** an entry in the %_parent table associating the node with its parent.
170545 ** And for each row of data in the table, there is an entry in the %_rowid
170546 ** table that maps from the entries rowid to the id of the node that it
170547 ** is stored on. If the r-tree contains auxiliary columns, those are stored
170548 ** on the end of the %_rowid table.
170549 **
170550 ** The root node of an r-tree always exists, even if the r-tree table is
170551 ** empty. The nodeno of the root node is always 1. All other nodes in the
170552 ** table must be the same size as the root node. The content of each node
170553 ** is formatted as follows:
@@ -170440,10 +170605,13 @@
170605 typedef union RtreeCoord RtreeCoord;
170606 typedef struct RtreeSearchPoint RtreeSearchPoint;
170607
170608 /* The rtree may have between 1 and RTREE_MAX_DIMENSIONS dimensions. */
170609 #define RTREE_MAX_DIMENSIONS 5
170610
170611 /* Maximum number of auxiliary columns */
170612 #define RTREE_MAX_AUX_COLUMN 100
170613
170614 /* Size of hash table Rtree.aHash. This hash table is not expected to
170615 ** ever contain very many entries, so a fixed number of buckets is
170616 ** used.
170617 */
@@ -170469,16 +170637,19 @@
170637 u8 nDim; /* Number of dimensions */
170638 u8 nDim2; /* Twice the number of dimensions */
170639 u8 eCoordType; /* RTREE_COORD_REAL32 or RTREE_COORD_INT32 */
170640 u8 nBytesPerCell; /* Bytes consumed per cell */
170641 u8 inWrTrans; /* True if inside write transaction */
170642 u8 nAux; /* # of auxiliary columns in %_rowid */
170643 int iDepth; /* Current depth of the r-tree structure */
170644 char *zDb; /* Name of database containing r-tree table */
170645 char *zName; /* Name of r-tree table */
170646 u32 nBusy; /* Current number of users of this structure */
170647 i64 nRowEst; /* Estimated number of rows in this table */
170648 u32 nCursor; /* Number of open cursors */
170649 u32 nNodeRef; /* Number RtreeNodes with positive nRef */
170650 char *zReadAuxSql; /* SQL for statement to read aux data */
170651
170652 /* List of nodes removed during a CondenseTree operation. List is
170653 ** linked together via the pointer normally used for hash chains -
170654 ** RtreeNode.pNext. RtreeNode.iNode stores the depth of the sub-tree
170655 ** headed by the node (leaf nodes have RtreeNode.iNode==0).
@@ -170500,10 +170671,13 @@
170671
170672 /* Statements to read/write/delete a record from xxx_parent */
170673 sqlite3_stmt *pReadParent;
170674 sqlite3_stmt *pWriteParent;
170675 sqlite3_stmt *pDeleteParent;
170676
170677 /* Statement for writing to the "aux:" fields, if there are any */
170678 sqlite3_stmt *pWriteAux;
170679
170680 RtreeNode *aHash[HASHSIZE]; /* Hash table of in-memory nodes. */
170681 };
170682
170683 /* Possible values for Rtree.eCoordType: */
@@ -170577,17 +170751,19 @@
170751 */
170752 struct RtreeCursor {
170753 sqlite3_vtab_cursor base; /* Base class. Must be first */
170754 u8 atEOF; /* True if at end of search */
170755 u8 bPoint; /* True if sPoint is valid */
170756 u8 bAuxValid; /* True if pReadAux is valid */
170757 int iStrategy; /* Copy of idxNum search parameter */
170758 int nConstraint; /* Number of entries in aConstraint */
170759 RtreeConstraint *aConstraint; /* Search constraints. */
170760 int nPointAlloc; /* Number of slots allocated for aPoint[] */
170761 int nPoint; /* Number of slots used in aPoint[] */
170762 int mxLevel; /* iLevel value for root of the tree */
170763 RtreeSearchPoint *aPoint; /* Priority queue for search points */
170764 sqlite3_stmt *pReadAux; /* Statement to read aux-data */
170765 RtreeSearchPoint sPoint; /* Cached next search point */
170766 RtreeNode *aNode[RTREE_CACHE_SZ]; /* Rtree node cache */
170767 u32 anQueue[RTREE_MAX_DEPTH+1]; /* Number of queued entries by iLevel */
170768 };
170769
@@ -170870,10 +171046,11 @@
171046 /*
171047 ** Increment the reference count of node p.
171048 */
171049 static void nodeReference(RtreeNode *p){
171050 if( p ){
171051 assert( p->nRef>0 );
171052 p->nRef++;
171053 }
171054 }
171055
171056 /*
@@ -170937,10 +171114,11 @@
171114 pNode = (RtreeNode *)sqlite3_malloc(sizeof(RtreeNode) + pRtree->iNodeSize);
171115 if( pNode ){
171116 memset(pNode, 0, sizeof(RtreeNode) + pRtree->iNodeSize);
171117 pNode->zData = (u8 *)&pNode[1];
171118 pNode->nRef = 1;
171119 pRtree->nNodeRef++;
171120 pNode->pParent = pParent;
171121 pNode->isDirty = 1;
171122 nodeReference(pParent);
171123 }
171124 return pNode;
@@ -170970,14 +171148,14 @@
171148 RtreeNode *pNode = 0;
171149
171150 /* Check if the requested node is already in the hash table. If so,
171151 ** increase its reference count and return it.
171152 */
171153 if( (pNode = nodeHashLookup(pRtree, iNode))!=0 ){
171154 assert( !pParent || !pNode->pParent || pNode->pParent==pParent );
171155 if( pParent && !pNode->pParent ){
171156 pParent->nRef++;
171157 pNode->pParent = pParent;
171158 }
171159 pNode->nRef++;
171160 *ppNode = pNode;
171161 return SQLITE_OK;
@@ -171012,10 +171190,11 @@
171190 rc = SQLITE_NOMEM;
171191 }else{
171192 pNode->pParent = pParent;
171193 pNode->zData = (u8 *)&pNode[1];
171194 pNode->nRef = 1;
171195 pRtree->nNodeRef++;
171196 pNode->iNode = iNode;
171197 pNode->isDirty = 0;
171198 pNode->pNext = 0;
171199 rc = sqlite3_blob_read(pRtree->pNodeBlob, pNode->zData,
171200 pRtree->iNodeSize, 0);
@@ -171052,11 +171231,14 @@
171231 }else{
171232 rc = SQLITE_CORRUPT_VTAB;
171233 }
171234 *ppNode = pNode;
171235 }else{
171236 if( pNode ){
171237 pRtree->nNodeRef--;
171238 sqlite3_free(pNode);
171239 }
171240 *ppNode = 0;
171241 }
171242
171243 return rc;
171244 }
@@ -171149,12 +171331,14 @@
171331 */
171332 static int nodeRelease(Rtree *pRtree, RtreeNode *pNode){
171333 int rc = SQLITE_OK;
171334 if( pNode ){
171335 assert( pNode->nRef>0 );
171336 assert( pRtree->nNodeRef>0 );
171337 pNode->nRef--;
171338 if( pNode->nRef==0 ){
171339 pRtree->nNodeRef--;
171340 if( pNode->iNode==1 ){
171341 pRtree->iDepth = -1;
171342 }
171343 if( pNode->pParent ){
171344 rc = nodeRelease(pRtree, pNode->pParent);
@@ -171267,20 +171451,23 @@
171451 */
171452 static void rtreeRelease(Rtree *pRtree){
171453 pRtree->nBusy--;
171454 if( pRtree->nBusy==0 ){
171455 pRtree->inWrTrans = 0;
171456 assert( pRtree->nCursor==0 );
171457 nodeBlobReset(pRtree);
171458 assert( pRtree->nNodeRef==0 );
171459 sqlite3_finalize(pRtree->pWriteNode);
171460 sqlite3_finalize(pRtree->pDeleteNode);
171461 sqlite3_finalize(pRtree->pReadRowid);
171462 sqlite3_finalize(pRtree->pWriteRowid);
171463 sqlite3_finalize(pRtree->pDeleteRowid);
171464 sqlite3_finalize(pRtree->pReadParent);
171465 sqlite3_finalize(pRtree->pWriteParent);
171466 sqlite3_finalize(pRtree->pDeleteParent);
171467 sqlite3_finalize(pRtree->pWriteAux);
171468 sqlite3_free(pRtree->zReadAuxSql);
171469 sqlite3_free(pRtree);
171470 }
171471 }
171472
171473 /*
@@ -171365,10 +171552,11 @@
171552 Rtree *pRtree = (Rtree *)(cur->pVtab);
171553 int ii;
171554 RtreeCursor *pCsr = (RtreeCursor *)cur;
171555 assert( pRtree->nCursor>0 );
171556 freeCursorConstraints(pCsr);
171557 sqlite3_finalize(pCsr->pReadAux);
171558 sqlite3_free(pCsr->aPoint);
171559 for(ii=0; ii<RTREE_CACHE_SZ; ii++) nodeRelease(pRtree, pCsr->aNode[ii]);
171560 sqlite3_free(pCsr);
171561 pRtree->nCursor--;
171562 nodeBlobReset(pRtree);
@@ -171736,11 +171924,11 @@
171924 if( pNew==0 ) return 0;
171925 ii = (int)(pNew - pCur->aPoint) + 1;
171926 if( ii<RTREE_CACHE_SZ ){
171927 assert( pCur->aNode[ii]==0 );
171928 pCur->aNode[ii] = pCur->aNode[0];
171929 }else{
171930 nodeRelease(RTREE_OF_CURSOR(pCur), pCur->aNode[0]);
171931 }
171932 pCur->aNode[0] = 0;
171933 *pNew = pCur->sPoint;
171934 }
@@ -171907,10 +172095,14 @@
172095 RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor;
172096 int rc = SQLITE_OK;
172097
172098 /* Move to the next entry that matches the configured constraints. */
172099 RTREE_QUEUE_TRACE(pCsr, "POP-Nx:");
172100 if( pCsr->bAuxValid ){
172101 pCsr->bAuxValid = 0;
172102 sqlite3_reset(pCsr->pReadAux);
172103 }
172104 rtreeSearchPointPop(pCsr);
172105 rc = rtreeStepToLeaf(pCsr);
172106 return rc;
172107 }
172108
@@ -171941,11 +172133,11 @@
172133
172134 if( rc ) return rc;
172135 if( p==0 ) return SQLITE_OK;
172136 if( i==0 ){
172137 sqlite3_result_int64(ctx, nodeGetRowid(pRtree, pNode, p->iCell));
172138 }else if( i<=pRtree->nDim2 ){
172139 nodeGetCoord(pRtree, pNode, p->iCell, i-1, &c);
172140 #ifndef SQLITE_RTREE_INT_ONLY
172141 if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
172142 sqlite3_result_double(ctx, c.f);
172143 }else
@@ -171952,11 +172144,31 @@
172144 #endif
172145 {
172146 assert( pRtree->eCoordType==RTREE_COORD_INT32 );
172147 sqlite3_result_int(ctx, c.i);
172148 }
172149 }else{
172150 if( !pCsr->bAuxValid ){
172151 if( pCsr->pReadAux==0 ){
172152 rc = sqlite3_prepare_v3(pRtree->db, pRtree->zReadAuxSql, -1, 0,
172153 &pCsr->pReadAux, 0);
172154 if( rc ) return rc;
172155 }
172156 sqlite3_bind_int64(pCsr->pReadAux, 1,
172157 nodeGetRowid(pRtree, pNode, p->iCell));
172158 rc = sqlite3_step(pCsr->pReadAux);
172159 if( rc==SQLITE_ROW ){
172160 pCsr->bAuxValid = 1;
172161 }else{
172162 sqlite3_reset(pCsr->pReadAux);
172163 if( rc==SQLITE_DONE ) rc = SQLITE_OK;
172164 return rc;
172165 }
172166 }
172167 sqlite3_result_value(ctx,
172168 sqlite3_column_value(pCsr->pReadAux, i - pRtree->nDim2 + 1));
172169 }
172170 return SQLITE_OK;
172171 }
172172
172173 /*
172174 ** Use nodeAcquire() to obtain the leaf node containing the record with
@@ -172030,18 +172242,21 @@
172242 RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor;
172243 RtreeNode *pRoot = 0;
172244 int ii;
172245 int rc = SQLITE_OK;
172246 int iCell = 0;
172247 sqlite3_stmt *pStmt;
172248
172249 rtreeReference(pRtree);
172250
172251 /* Reset the cursor to the same state as rtreeOpen() leaves it in. */
172252 freeCursorConstraints(pCsr);
172253 sqlite3_free(pCsr->aPoint);
172254 pStmt = pCsr->pReadAux;
172255 memset(pCsr, 0, sizeof(RtreeCursor));
172256 pCsr->base.pVtab = (sqlite3_vtab*)pRtree;
172257 pCsr->pReadAux = pStmt;
172258
172259 pCsr->iStrategy = idxNum;
172260 if( idxNum==1 ){
172261 /* Special case - lookup by rowid. */
172262 RtreeNode *pLeaf; /* Leaf on which the required cell resides */
@@ -172200,14 +172415,18 @@
172415 ** sqlite uses an internal cost of 0.0). It is expected to return
172416 ** a single row.
172417 */
172418 pIdxInfo->estimatedCost = 30.0;
172419 pIdxInfo->estimatedRows = 1;
172420 pIdxInfo->idxFlags = SQLITE_INDEX_SCAN_UNIQUE;
172421 return SQLITE_OK;
172422 }
172423
172424 if( p->usable
172425 && ((p->iColumn>0 && p->iColumn<=pRtree->nDim2)
172426 || p->op==SQLITE_INDEX_CONSTRAINT_MATCH)
172427 ){
172428 u8 op;
172429 switch( p->op ){
172430 case SQLITE_INDEX_CONSTRAINT_EQ: op = RTREE_EQ; break;
172431 case SQLITE_INDEX_CONSTRAINT_GT: op = RTREE_GT; break;
172432 case SQLITE_INDEX_CONSTRAINT_LE: op = RTREE_LE; break;
@@ -172776,11 +172995,11 @@
172995 pNode->isDirty = 1;
172996 writeInt16(pNode->zData, pRtree->iDepth);
172997 }else{
172998 pLeft = pNode;
172999 pRight = nodeNew(pRtree, pLeft->pParent);
173000 pLeft->nRef++;
173001 }
173002
173003 if( !pLeft || !pRight ){
173004 rc = SQLITE_NOMEM;
173005 goto splitnode_out;
@@ -173266,10 +173485,11 @@
173485 for(pLeaf=pRtree->pDeleted; pLeaf; pLeaf=pRtree->pDeleted){
173486 if( rc==SQLITE_OK ){
173487 rc = reinsertNodeContent(pRtree, pLeaf);
173488 }
173489 pRtree->pDeleted = pLeaf->pNext;
173490 pRtree->nNodeRef--;
173491 sqlite3_free(pLeaf);
173492 }
173493
173494 /* Release the reference to the root node. */
173495 if( rc==SQLITE_OK ){
@@ -173362,18 +173582,24 @@
173582 ** The xUpdate method for rtree module virtual tables.
173583 */
173584 static int rtreeUpdate(
173585 sqlite3_vtab *pVtab,
173586 int nData,
173587 sqlite3_value **aData,
173588 sqlite_int64 *pRowid
173589 ){
173590 Rtree *pRtree = (Rtree *)pVtab;
173591 int rc = SQLITE_OK;
173592 RtreeCell cell; /* New cell to insert if nData>1 */
173593 int bHaveRowid = 0; /* Set to 1 after new rowid is determined */
173594
173595 if( pRtree->nNodeRef ){
173596 /* Unable to write to the btree while another cursor is reading from it,
173597 ** since the write might do a rebalance which would disrupt the read
173598 ** cursor. */
173599 return SQLITE_LOCKED_VTAB;
173600 }
173601 rtreeReference(pRtree);
173602 assert(nData>=1);
173603
173604 cell.iRowid = 0; /* Used only to suppress a compiler warning */
173605
@@ -173388,50 +173614,51 @@
173614 ** case, SQLITE_CONSTRAINT must be returned regardless of the
173615 ** conflict-handling mode specified by the user.
173616 */
173617 if( nData>1 ){
173618 int ii;
173619 int nn = nData - 4;
173620
173621 if( nn > pRtree->nDim2 ) nn = pRtree->nDim2;
173622 /* Populate the cell.aCoord[] array. The first coordinate is aData[3].
173623 **
173624 ** NB: nData can only be less than nDim*2+3 if the rtree is mis-declared
173625 ** with "column" that are interpreted as table constraints.
173626 ** Example: CREATE VIRTUAL TABLE bad USING rtree(x,y,CHECK(y>5));
173627 ** This problem was discovered after years of use, so we silently ignore
173628 ** these kinds of misdeclared tables to avoid breaking any legacy.
173629 */
 
173630
173631 #ifndef SQLITE_RTREE_INT_ONLY
173632 if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
173633 for(ii=0; ii<nn; ii+=2){
173634 cell.aCoord[ii].f = rtreeValueDown(aData[ii+3]);
173635 cell.aCoord[ii+1].f = rtreeValueUp(aData[ii+4]);
173636 if( cell.aCoord[ii].f>cell.aCoord[ii+1].f ){
173637 rc = rtreeConstraintError(pRtree, ii+1);
173638 goto constraint;
173639 }
173640 }
173641 }else
173642 #endif
173643 {
173644 for(ii=0; ii<nn; ii+=2){
173645 cell.aCoord[ii].i = sqlite3_value_int(aData[ii+3]);
173646 cell.aCoord[ii+1].i = sqlite3_value_int(aData[ii+4]);
173647 if( cell.aCoord[ii].i>cell.aCoord[ii+1].i ){
173648 rc = rtreeConstraintError(pRtree, ii+1);
173649 goto constraint;
173650 }
173651 }
173652 }
173653
173654 /* If a rowid value was supplied, check if it is already present in
173655 ** the table. If so, the constraint has failed. */
173656 if( sqlite3_value_type(aData[2])!=SQLITE_NULL ){
173657 cell.iRowid = sqlite3_value_int64(aData[2]);
173658 if( sqlite3_value_type(aData[0])==SQLITE_NULL
173659 || sqlite3_value_int64(aData[0])!=cell.iRowid
173660 ){
173661 int steprc;
173662 sqlite3_bind_int64(pRtree->pReadRowid, 1, cell.iRowid);
173663 steprc = sqlite3_step(pRtree->pReadRowid);
173664 rc = sqlite3_reset(pRtree->pReadRowid);
@@ -173446,20 +173673,20 @@
173673 }
173674 bHaveRowid = 1;
173675 }
173676 }
173677
173678 /* If aData[0] is not an SQL NULL value, it is the rowid of a
173679 ** record to delete from the r-tree table. The following block does
173680 ** just that.
173681 */
173682 if( sqlite3_value_type(aData[0])!=SQLITE_NULL ){
173683 rc = rtreeDeleteRowid(pRtree, sqlite3_value_int64(aData[0]));
173684 }
173685
173686 /* If the aData[] array contains more than one element, elements
173687 ** (aData[2]..aData[argc-1]) contain a new record to insert into
173688 ** the r-tree structure.
173689 */
173690 if( rc==SQLITE_OK && nData>1 ){
173691 /* Insert the new record into the r-tree */
173692 RtreeNode *pLeaf = 0;
@@ -173480,10 +173707,20 @@
173707 rc2 = nodeRelease(pRtree, pLeaf);
173708 if( rc==SQLITE_OK ){
173709 rc = rc2;
173710 }
173711 }
173712 if( pRtree->nAux ){
173713 sqlite3_stmt *pUp = pRtree->pWriteAux;
173714 int jj;
173715 sqlite3_bind_int64(pUp, 1, *pRowid);
173716 for(jj=0; jj<pRtree->nAux; jj++){
173717 sqlite3_bind_value(pUp, jj+2, aData[pRtree->nDim2+3+jj]);
173718 }
173719 sqlite3_step(pUp);
173720 rc = sqlite3_reset(pUp);
173721 }
173722 }
173723
173724 constraint:
173725 rtreeRelease(pRtree);
173726 return rc;
@@ -173636,37 +173873,48 @@
173873 int rc = SQLITE_OK;
173874
173875 #define N_STATEMENT 8
173876 static const char *azSql[N_STATEMENT] = {
173877 /* Write the xxx_node table */
173878 "INSERT OR REPLACE INTO '%q'.'%q_node' VALUES(?1, ?2)",
173879 "DELETE FROM '%q'.'%q_node' WHERE nodeno = ?1",
173880
173881 /* Read and write the xxx_rowid table */
173882 "SELECT nodeno FROM '%q'.'%q_rowid' WHERE rowid = ?1",
173883 "INSERT OR REPLACE INTO '%q'.'%q_rowid' VALUES(?1, ?2)",
173884 "DELETE FROM '%q'.'%q_rowid' WHERE rowid = ?1",
173885
173886 /* Read and write the xxx_parent table */
173887 "SELECT parentnode FROM '%q'.'%q_parent' WHERE nodeno = ?1",
173888 "INSERT OR REPLACE INTO '%q'.'%q_parent' VALUES(?1, ?2)",
173889 "DELETE FROM '%q'.'%q_parent' WHERE nodeno = ?1"
173890 };
173891 sqlite3_stmt **appStmt[N_STATEMENT];
173892 int i;
173893
173894 pRtree->db = db;
173895
173896 if( isCreate ){
173897 char *zCreate;
173898 sqlite3_str *p = sqlite3_str_new(db);
173899 int ii;
173900 sqlite3_str_appendf(p,
173901 "CREATE TABLE \"%w\".\"%w_rowid\"(rowid INTEGER PRIMARY KEY,nodeno",
173902 zDb, zPrefix);
173903 for(ii=0; ii<pRtree->nAux; ii++){
173904 sqlite3_str_appendf(p,",a%d",ii);
173905 }
173906 sqlite3_str_appendf(p,
173907 ");CREATE TABLE \"%w\".\"%w_node\"(nodeno INTEGER PRIMARY KEY,data);",
173908 zDb, zPrefix);
173909 sqlite3_str_appendf(p,
173910 "CREATE TABLE \"%w\".\"%w_parent\"(nodeno INTEGER PRIMARY KEY,parentnode);",
173911 zDb, zPrefix);
173912 sqlite3_str_appendf(p,
173913 "INSERT INTO \"%w\".\"%w_node\"VALUES(1,zeroblob(%d))",
173914 zDb, zPrefix, pRtree->iNodeSize);
173915 zCreate = sqlite3_str_finish(p);
173916 if( !zCreate ){
173917 return SQLITE_NOMEM;
173918 }
173919 rc = sqlite3_exec(db, zCreate, 0, 0, 0);
173920 sqlite3_free(zCreate);
@@ -173684,18 +173932,54 @@
173932 appStmt[6] = &pRtree->pWriteParent;
173933 appStmt[7] = &pRtree->pDeleteParent;
173934
173935 rc = rtreeQueryStat1(db, pRtree);
173936 for(i=0; i<N_STATEMENT && rc==SQLITE_OK; i++){
173937 char *zSql;
173938 const char *zFormat;
173939 if( i!=3 || pRtree->nAux==0 ){
173940 zFormat = azSql[i];
173941 }else {
173942 /* An UPSERT is very slightly slower than REPLACE, but it is needed
173943 ** if there are auxiliary columns */
173944 zFormat = "INSERT INTO\"%w\".\"%w_rowid\"(rowid,nodeno)VALUES(?1,?2)"
173945 "ON CONFLICT(rowid)DO UPDATE SET nodeno=excluded.nodeno";
173946 }
173947 zSql = sqlite3_mprintf(zFormat, zDb, zPrefix);
173948 if( zSql ){
173949 rc = sqlite3_prepare_v3(db, zSql, -1, SQLITE_PREPARE_PERSISTENT,
173950 appStmt[i], 0);
173951 }else{
173952 rc = SQLITE_NOMEM;
173953 }
173954 sqlite3_free(zSql);
173955 }
173956 if( pRtree->nAux ){
173957 pRtree->zReadAuxSql = sqlite3_mprintf(
173958 "SELECT * FROM \"%w\".\"%w_rowid\" WHERE rowid=?1",
173959 zDb, zPrefix);
173960 if( pRtree->zReadAuxSql==0 ){
173961 rc = SQLITE_NOMEM;
173962 }else{
173963 sqlite3_str *p = sqlite3_str_new(db);
173964 int ii;
173965 char *zSql;
173966 sqlite3_str_appendf(p, "UPDATE \"%w\".\"%w_rowid\"SET ", zDb, zPrefix);
173967 for(ii=0; ii<pRtree->nAux; ii++){
173968 if( ii ) sqlite3_str_append(p, ",", 1);
173969 sqlite3_str_appendf(p,"a%d=?%d",ii,ii+2);
173970 }
173971 sqlite3_str_appendf(p, " WHERE rowid=?1");
173972 zSql = sqlite3_str_finish(p);
173973 if( zSql==0 ){
173974 rc = SQLITE_NOMEM;
173975 }else{
173976 rc = sqlite3_prepare_v3(db, zSql, -1, SQLITE_PREPARE_PERSISTENT,
173977 &pRtree->pWriteAux, 0);
173978 sqlite3_free(zSql);
173979 }
173980 }
173981 }
173982
173983 return rc;
173984 }
173985
@@ -173795,21 +174079,26 @@
174079 int rc = SQLITE_OK;
174080 Rtree *pRtree;
174081 int nDb; /* Length of string argv[1] */
174082 int nName; /* Length of string argv[2] */
174083 int eCoordType = (pAux ? RTREE_COORD_INT32 : RTREE_COORD_REAL32);
174084 sqlite3_str *pSql;
174085 char *zSql;
174086 int ii = 4;
174087 int iErr;
174088
174089 const char *aErrMsg[] = {
174090 0, /* 0 */
174091 "Wrong number of columns for an rtree table", /* 1 */
174092 "Too few columns for an rtree table", /* 2 */
174093 "Too many columns for an rtree table", /* 3 */
174094 "Auxiliary rtree columns must be last" /* 4 */
174095 };
174096
174097 assert( RTREE_MAX_AUX_COLUMN<256 ); /* Aux columns counted by a u8 */
174098 if( argc>RTREE_MAX_AUX_COLUMN+3 ){
174099 *pzErr = sqlite3_mprintf("%s", aErrMsg[3]);
174100 return SQLITE_ERROR;
174101 }
174102
174103 sqlite3_vtab_config(db, SQLITE_VTAB_CONSTRAINT_SUPPORT, 1);
174104
@@ -173823,57 +174112,77 @@
174112 memset(pRtree, 0, sizeof(Rtree)+nDb+nName+2);
174113 pRtree->nBusy = 1;
174114 pRtree->base.pModule = &rtreeModule;
174115 pRtree->zDb = (char *)&pRtree[1];
174116 pRtree->zName = &pRtree->zDb[nDb+1];
 
 
 
174117 pRtree->eCoordType = (u8)eCoordType;
174118 memcpy(pRtree->zDb, argv[1], nDb);
174119 memcpy(pRtree->zName, argv[2], nName);
174120
 
 
174121
174122 /* Create/Connect to the underlying relational database schema. If
174123 ** that is successful, call sqlite3_declare_vtab() to configure
174124 ** the r-tree table schema.
174125 */
174126 pSql = sqlite3_str_new(db);
174127 sqlite3_str_appendf(pSql, "CREATE TABLE x(%s", argv[3]);
174128 for(ii=4; ii<argc; ii++){
174129 if( argv[ii][0]=='+' ){
174130 pRtree->nAux++;
174131 sqlite3_str_appendf(pSql, ",%s", argv[ii]+1);
174132 }else if( pRtree->nAux>0 ){
174133 break;
174134 }else{
174135 pRtree->nDim2++;
174136 sqlite3_str_appendf(pSql, ",%s", argv[ii]);
174137 }
174138 }
174139 sqlite3_str_appendf(pSql, ");");
174140 zSql = sqlite3_str_finish(pSql);
174141 if( !zSql ){
174142 rc = SQLITE_NOMEM;
174143 }else if( ii<argc ){
174144 *pzErr = sqlite3_mprintf("%s", aErrMsg[4]);
174145 rc = SQLITE_ERROR;
174146 }else if( SQLITE_OK!=(rc = sqlite3_declare_vtab(db, zSql)) ){
174147 *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
174148 }
174149 sqlite3_free(zSql);
174150 if( rc ) goto rtreeInit_fail;
174151 pRtree->nDim = pRtree->nDim2/2;
174152 if( pRtree->nDim<1 ){
174153 iErr = 2;
174154 }else if( pRtree->nDim2>RTREE_MAX_DIMENSIONS*2 ){
174155 iErr = 3;
174156 }else if( pRtree->nDim2 % 2 ){
174157 iErr = 1;
174158 }else{
174159 iErr = 0;
174160 }
174161 if( iErr ){
174162 *pzErr = sqlite3_mprintf("%s", aErrMsg[iErr]);
174163 goto rtreeInit_fail;
174164 }
174165 pRtree->nBytesPerCell = 8 + pRtree->nDim2*4;
174166
174167 /* Figure out the node size to use. */
174168 rc = getNodeSize(db, pRtree, isCreate, pzErr);
174169 if( rc ) goto rtreeInit_fail;
174170 rc = rtreeSqlInit(pRtree, db, argv[1], argv[2], isCreate);
174171 if( rc ){
174172 *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
174173 goto rtreeInit_fail;
174174 }
174175
174176 *ppVtab = (sqlite3_vtab *)pRtree;
174177 return SQLITE_OK;
174178
174179 rtreeInit_fail:
174180 if( rc==SQLITE_OK ) rc = SQLITE_ERROR;
174181 assert( *ppVtab==0 );
174182 assert( pRtree->nBusy==1 );
174183 rtreeRelease(pRtree);
174184 return rc;
174185 }
174186
174187
174188 /*
@@ -174098,11 +174407,11 @@
174407 ** This function is used to check that the %_parent (if bLeaf==0) or %_rowid
174408 ** (if bLeaf==1) table contains a specified entry. The schemas of the
174409 ** two tables are:
174410 **
174411 ** CREATE TABLE %_parent(nodeno INTEGER PRIMARY KEY, parentnode INTEGER)
174412 ** CREATE TABLE %_rowid(rowid INTEGER PRIMARY KEY, nodeno INTEGER, ...)
174413 **
174414 ** In both cases, this function checks that there exists an entry with
174415 ** IPK value iKey and the second column set to iVal.
174416 **
174417 */
@@ -174113,12 +174422,12 @@
174422 i64 iVal /* Expected value for mapping */
174423 ){
174424 int rc;
174425 sqlite3_stmt *pStmt;
174426 const char *azSql[2] = {
174427 "SELECT parentnode FROM %Q.'%q_parent' WHERE nodeno=?1",
174428 "SELECT nodeno FROM %Q.'%q_rowid' WHERE rowid=?1"
174429 };
174430
174431 assert( bLeaf==0 || bLeaf==1 );
174432 if( pCheck->aCheckMapping[bLeaf]==0 ){
174433 pCheck->aCheckMapping[bLeaf] = rtreeCheckPrepare(pCheck,
@@ -174298,10 +174607,11 @@
174607 char **pzReport /* OUT: sqlite3_malloc'd report text */
174608 ){
174609 RtreeCheck check; /* Common context for various routines */
174610 sqlite3_stmt *pStmt = 0; /* Used to find column count of rtree table */
174611 int bEnd = 0; /* True if transaction should be closed */
174612 int nAux = 0; /* Number of extra columns. */
174613
174614 /* Initialize the context object */
174615 memset(&check, 0, sizeof(check));
174616 check.db = db;
174617 check.zDb = zDb;
@@ -174312,16 +174622,26 @@
174622 ** on a consistent snapshot. */
174623 if( sqlite3_get_autocommit(db) ){
174624 check.rc = sqlite3_exec(db, "BEGIN", 0, 0, 0);
174625 bEnd = 1;
174626 }
174627
174628 /* Find the number of auxiliary columns */
174629 if( check.rc==SQLITE_OK ){
174630 pStmt = rtreeCheckPrepare(&check, "SELECT * FROM %Q.'%q_rowid'", zDb, zTab);
174631 if( pStmt ){
174632 nAux = sqlite3_column_count(pStmt) - 2;
174633 sqlite3_finalize(pStmt);
174634 }
174635 check.rc = SQLITE_OK;
174636 }
174637
174638 /* Find number of dimensions in the rtree table. */
174639 pStmt = rtreeCheckPrepare(&check, "SELECT * FROM %Q.%Q", zDb, zTab);
174640 if( pStmt ){
174641 int rc;
174642 check.nDim = (sqlite3_column_count(pStmt) - 1 - nAux) / 2;
174643 if( check.nDim<1 ){
174644 rtreeCheckAppendMsg(&check, "Schema corrupt or not an rtree");
174645 }else if( SQLITE_ROW==sqlite3_step(pStmt) ){
174646 check.bInt = (sqlite3_column_type(pStmt, 1)==SQLITE_INTEGER);
174647 }
@@ -189525,11 +189845,11 @@
189845 }else{
189846 jsonAppendChar(&x, '$');
189847 }
189848 if( p->eType==JSON_ARRAY ){
189849 jsonPrintf(30, &x, "[%d]", p->iRowid);
189850 }else if( p->eType==JSON_OBJECT ){
189851 jsonPrintf(pThis->n, &x, ".%.*s", pThis->n-2, pThis->u.zJContent+1);
189852 }
189853 }
189854 jsonResult(&x);
189855 break;
@@ -207256,11 +207576,11 @@
207576 int nArg, /* Number of args */
207577 sqlite3_value **apUnused /* Function arguments */
207578 ){
207579 assert( nArg==0 );
207580 UNUSED_PARAM2(nArg, apUnused);
207581 sqlite3_result_text(pCtx, "fts5: 2018-05-30 01:14:20 86ee267ee86f5264774a9f215b1158aeaa2d605e77c205731b5ee3945d7de4c2", -1, SQLITE_TRANSIENT);
207582 }
207583
207584 static int fts5Init(sqlite3 *db){
207585 static const sqlite3_module fts5Mod = {
207586 /* iVersion */ 2,
@@ -211526,12 +211846,12 @@
211846 }
211847 #endif /* SQLITE_CORE */
211848 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_STMTVTAB) */
211849
211850 /************** End of stmt.c ************************************************/
211851 #if __LINE__!=211851
211852 #undef SQLITE_SOURCE_ID
211853 #define SQLITE_SOURCE_ID "2018-05-30 01:14:20 86ee267ee86f5264774a9f215b1158aeaa2d605e77c205731b5ee3945d7dalt2"
211854 #endif
211855 /* Return the source-id for this library */
211856 SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; }
211857 /************************** End of sqlite3.c ******************************/
211858
+21 -7
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -123,11 +123,11 @@
123123
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
124124
** [sqlite_version()] and [sqlite_source_id()].
125125
*/
126126
#define SQLITE_VERSION "3.24.0"
127127
#define SQLITE_VERSION_NUMBER 3024000
128
-#define SQLITE_SOURCE_ID "2018-05-14 00:41:12 d0f35739af3b226c8eef39676407293650cde551acef06fe8628fdd5b59bd66a"
128
+#define SQLITE_SOURCE_ID "2018-05-30 01:14:20 86ee267ee86f5264774a9f215b1158aeaa2d605e77c205731b5ee3945d7de4c2"
129129
130130
/*
131131
** CAPI3REF: Run-Time Library Version Numbers
132132
** KEYWORDS: sqlite3_version sqlite3_sourceid
133133
**
@@ -502,17 +502,19 @@
502502
#define SQLITE_IOERR_AUTH (SQLITE_IOERR | (28<<8))
503503
#define SQLITE_IOERR_BEGIN_ATOMIC (SQLITE_IOERR | (29<<8))
504504
#define SQLITE_IOERR_COMMIT_ATOMIC (SQLITE_IOERR | (30<<8))
505505
#define SQLITE_IOERR_ROLLBACK_ATOMIC (SQLITE_IOERR | (31<<8))
506506
#define SQLITE_LOCKED_SHAREDCACHE (SQLITE_LOCKED | (1<<8))
507
+#define SQLITE_LOCKED_VTAB (SQLITE_LOCKED | (2<<8))
507508
#define SQLITE_BUSY_RECOVERY (SQLITE_BUSY | (1<<8))
508509
#define SQLITE_BUSY_SNAPSHOT (SQLITE_BUSY | (2<<8))
509510
#define SQLITE_CANTOPEN_NOTEMPDIR (SQLITE_CANTOPEN | (1<<8))
510511
#define SQLITE_CANTOPEN_ISDIR (SQLITE_CANTOPEN | (2<<8))
511512
#define SQLITE_CANTOPEN_FULLPATH (SQLITE_CANTOPEN | (3<<8))
512513
#define SQLITE_CANTOPEN_CONVPATH (SQLITE_CANTOPEN | (4<<8))
513514
#define SQLITE_CORRUPT_VTAB (SQLITE_CORRUPT | (1<<8))
515
+#define SQLITE_CORRUPT_SEQUENCE (SQLITE_CORRUPT | (2<<8))
514516
#define SQLITE_READONLY_RECOVERY (SQLITE_READONLY | (1<<8))
515517
#define SQLITE_READONLY_CANTLOCK (SQLITE_READONLY | (2<<8))
516518
#define SQLITE_READONLY_ROLLBACK (SQLITE_READONLY | (3<<8))
517519
#define SQLITE_READONLY_DBMOVED (SQLITE_READONLY | (4<<8))
518520
#define SQLITE_READONLY_CANTINIT (SQLITE_READONLY | (5<<8))
@@ -6290,10 +6292,14 @@
62906292
sqlite3_uint64 colUsed; /* Input: Mask of columns used by statement */
62916293
};
62926294
62936295
/*
62946296
** CAPI3REF: Virtual Table Scan Flags
6297
+**
6298
+** Virtual table implementations are allowed to set the
6299
+** [sqlite3_index_info].idxFlags field to some combination of
6300
+** these bits.
62956301
*/
62966302
#define SQLITE_INDEX_SCAN_UNIQUE 1 /* Scan visits at most 1 row */
62976303
62986304
/*
62996305
** CAPI3REF: Virtual Table Constraint Operator Codes
@@ -7153,15 +7159,23 @@
71537159
/*
71547160
** CAPI3REF: Create A New Dynamic String Object
71557161
** CONSTRUCTOR: sqlite3_str
71567162
**
71577163
** ^The [sqlite3_str_new(D)] interface allocates and initializes
7158
-** a new [sqlite3_str]
7159
-** object. ^The [sqlite3_str_new()] interface returns NULL on an out-of-memory
7160
-** condition. To avoid memory leaks, the object returned by
7164
+** a new [sqlite3_str] object. To avoid memory leaks, the object returned by
71617165
** [sqlite3_str_new()] must be freed by a subsequent call to
71627166
** [sqlite3_str_finish(X)].
7167
+**
7168
+** ^The [sqlite3_str_new(D)] interface always returns a pointer to a
7169
+** valid [sqlite3_str] object, though in the event of an out-of-memory
7170
+** error the returned object might be a special singleton that will
7171
+** silently reject new text, always return SQLITE_NOMEM from
7172
+** [sqlite3_str_errcode()], always return 0 for
7173
+** [sqlite3_str_length()], and always return NULL from
7174
+** [sqlite3_str_finish(X)]. It is always safe to use the value
7175
+** returned by [sqlite3_str_new(D)] as the sqlite3_str parameter
7176
+** to any of the other [sqlite3_str] methods.
71637177
**
71647178
** The D parameter to [sqlite3_str_new(D)] may be NULL. If the
71657179
** D parameter in [sqlite3_str_new(D)] is not NULL, then the maximum
71667180
** length of the string contained in the [sqlite3_str] object will be
71677181
** the value set for [sqlite3_limit](D,[SQLITE_LIMIT_LENGTH]) instead
@@ -8523,15 +8537,15 @@
85238537
**
85248538
** If the sqlite3_vtab_nochange(X) routine is called within the [xColumn]
85258539
** method of a [virtual table], then it returns true if and only if the
85268540
** column is being fetched as part of an UPDATE operation during which the
85278541
** column value will not change. Applications might use this to substitute
8528
-** a lighter-weight value to return that the corresponding [xUpdate] method
8529
-** understands as a "no-change" value.
8542
+** a return value that is less expensive to compute and that the corresponding
8543
+** [xUpdate] method understands as a "no-change" value.
85308544
**
85318545
** If the [xColumn] method calls sqlite3_vtab_nochange() and finds that
8532
-** the column is not changed by the UPDATE statement, they the xColumn
8546
+** the column is not changed by the UPDATE statement, then the xColumn
85338547
** method can optionally return without setting a result, without calling
85348548
** any of the [sqlite3_result_int|sqlite3_result_xxxxx() interfaces].
85358549
** In that case, [sqlite3_value_nochange(X)] will return true for the
85368550
** same column in the [xUpdate] method.
85378551
*/
85388552
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -123,11 +123,11 @@
123 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
124 ** [sqlite_version()] and [sqlite_source_id()].
125 */
126 #define SQLITE_VERSION "3.24.0"
127 #define SQLITE_VERSION_NUMBER 3024000
128 #define SQLITE_SOURCE_ID "2018-05-14 00:41:12 d0f35739af3b226c8eef39676407293650cde551acef06fe8628fdd5b59bd66a"
129
130 /*
131 ** CAPI3REF: Run-Time Library Version Numbers
132 ** KEYWORDS: sqlite3_version sqlite3_sourceid
133 **
@@ -502,17 +502,19 @@
502 #define SQLITE_IOERR_AUTH (SQLITE_IOERR | (28<<8))
503 #define SQLITE_IOERR_BEGIN_ATOMIC (SQLITE_IOERR | (29<<8))
504 #define SQLITE_IOERR_COMMIT_ATOMIC (SQLITE_IOERR | (30<<8))
505 #define SQLITE_IOERR_ROLLBACK_ATOMIC (SQLITE_IOERR | (31<<8))
506 #define SQLITE_LOCKED_SHAREDCACHE (SQLITE_LOCKED | (1<<8))
 
507 #define SQLITE_BUSY_RECOVERY (SQLITE_BUSY | (1<<8))
508 #define SQLITE_BUSY_SNAPSHOT (SQLITE_BUSY | (2<<8))
509 #define SQLITE_CANTOPEN_NOTEMPDIR (SQLITE_CANTOPEN | (1<<8))
510 #define SQLITE_CANTOPEN_ISDIR (SQLITE_CANTOPEN | (2<<8))
511 #define SQLITE_CANTOPEN_FULLPATH (SQLITE_CANTOPEN | (3<<8))
512 #define SQLITE_CANTOPEN_CONVPATH (SQLITE_CANTOPEN | (4<<8))
513 #define SQLITE_CORRUPT_VTAB (SQLITE_CORRUPT | (1<<8))
 
514 #define SQLITE_READONLY_RECOVERY (SQLITE_READONLY | (1<<8))
515 #define SQLITE_READONLY_CANTLOCK (SQLITE_READONLY | (2<<8))
516 #define SQLITE_READONLY_ROLLBACK (SQLITE_READONLY | (3<<8))
517 #define SQLITE_READONLY_DBMOVED (SQLITE_READONLY | (4<<8))
518 #define SQLITE_READONLY_CANTINIT (SQLITE_READONLY | (5<<8))
@@ -6290,10 +6292,14 @@
6290 sqlite3_uint64 colUsed; /* Input: Mask of columns used by statement */
6291 };
6292
6293 /*
6294 ** CAPI3REF: Virtual Table Scan Flags
 
 
 
 
6295 */
6296 #define SQLITE_INDEX_SCAN_UNIQUE 1 /* Scan visits at most 1 row */
6297
6298 /*
6299 ** CAPI3REF: Virtual Table Constraint Operator Codes
@@ -7153,15 +7159,23 @@
7153 /*
7154 ** CAPI3REF: Create A New Dynamic String Object
7155 ** CONSTRUCTOR: sqlite3_str
7156 **
7157 ** ^The [sqlite3_str_new(D)] interface allocates and initializes
7158 ** a new [sqlite3_str]
7159 ** object. ^The [sqlite3_str_new()] interface returns NULL on an out-of-memory
7160 ** condition. To avoid memory leaks, the object returned by
7161 ** [sqlite3_str_new()] must be freed by a subsequent call to
7162 ** [sqlite3_str_finish(X)].
 
 
 
 
 
 
 
 
 
 
7163 **
7164 ** The D parameter to [sqlite3_str_new(D)] may be NULL. If the
7165 ** D parameter in [sqlite3_str_new(D)] is not NULL, then the maximum
7166 ** length of the string contained in the [sqlite3_str] object will be
7167 ** the value set for [sqlite3_limit](D,[SQLITE_LIMIT_LENGTH]) instead
@@ -8523,15 +8537,15 @@
8523 **
8524 ** If the sqlite3_vtab_nochange(X) routine is called within the [xColumn]
8525 ** method of a [virtual table], then it returns true if and only if the
8526 ** column is being fetched as part of an UPDATE operation during which the
8527 ** column value will not change. Applications might use this to substitute
8528 ** a lighter-weight value to return that the corresponding [xUpdate] method
8529 ** understands as a "no-change" value.
8530 **
8531 ** If the [xColumn] method calls sqlite3_vtab_nochange() and finds that
8532 ** the column is not changed by the UPDATE statement, they the xColumn
8533 ** method can optionally return without setting a result, without calling
8534 ** any of the [sqlite3_result_int|sqlite3_result_xxxxx() interfaces].
8535 ** In that case, [sqlite3_value_nochange(X)] will return true for the
8536 ** same column in the [xUpdate] method.
8537 */
8538
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -123,11 +123,11 @@
123 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
124 ** [sqlite_version()] and [sqlite_source_id()].
125 */
126 #define SQLITE_VERSION "3.24.0"
127 #define SQLITE_VERSION_NUMBER 3024000
128 #define SQLITE_SOURCE_ID "2018-05-30 01:14:20 86ee267ee86f5264774a9f215b1158aeaa2d605e77c205731b5ee3945d7de4c2"
129
130 /*
131 ** CAPI3REF: Run-Time Library Version Numbers
132 ** KEYWORDS: sqlite3_version sqlite3_sourceid
133 **
@@ -502,17 +502,19 @@
502 #define SQLITE_IOERR_AUTH (SQLITE_IOERR | (28<<8))
503 #define SQLITE_IOERR_BEGIN_ATOMIC (SQLITE_IOERR | (29<<8))
504 #define SQLITE_IOERR_COMMIT_ATOMIC (SQLITE_IOERR | (30<<8))
505 #define SQLITE_IOERR_ROLLBACK_ATOMIC (SQLITE_IOERR | (31<<8))
506 #define SQLITE_LOCKED_SHAREDCACHE (SQLITE_LOCKED | (1<<8))
507 #define SQLITE_LOCKED_VTAB (SQLITE_LOCKED | (2<<8))
508 #define SQLITE_BUSY_RECOVERY (SQLITE_BUSY | (1<<8))
509 #define SQLITE_BUSY_SNAPSHOT (SQLITE_BUSY | (2<<8))
510 #define SQLITE_CANTOPEN_NOTEMPDIR (SQLITE_CANTOPEN | (1<<8))
511 #define SQLITE_CANTOPEN_ISDIR (SQLITE_CANTOPEN | (2<<8))
512 #define SQLITE_CANTOPEN_FULLPATH (SQLITE_CANTOPEN | (3<<8))
513 #define SQLITE_CANTOPEN_CONVPATH (SQLITE_CANTOPEN | (4<<8))
514 #define SQLITE_CORRUPT_VTAB (SQLITE_CORRUPT | (1<<8))
515 #define SQLITE_CORRUPT_SEQUENCE (SQLITE_CORRUPT | (2<<8))
516 #define SQLITE_READONLY_RECOVERY (SQLITE_READONLY | (1<<8))
517 #define SQLITE_READONLY_CANTLOCK (SQLITE_READONLY | (2<<8))
518 #define SQLITE_READONLY_ROLLBACK (SQLITE_READONLY | (3<<8))
519 #define SQLITE_READONLY_DBMOVED (SQLITE_READONLY | (4<<8))
520 #define SQLITE_READONLY_CANTINIT (SQLITE_READONLY | (5<<8))
@@ -6290,10 +6292,14 @@
6292 sqlite3_uint64 colUsed; /* Input: Mask of columns used by statement */
6293 };
6294
6295 /*
6296 ** CAPI3REF: Virtual Table Scan Flags
6297 **
6298 ** Virtual table implementations are allowed to set the
6299 ** [sqlite3_index_info].idxFlags field to some combination of
6300 ** these bits.
6301 */
6302 #define SQLITE_INDEX_SCAN_UNIQUE 1 /* Scan visits at most 1 row */
6303
6304 /*
6305 ** CAPI3REF: Virtual Table Constraint Operator Codes
@@ -7153,15 +7159,23 @@
7159 /*
7160 ** CAPI3REF: Create A New Dynamic String Object
7161 ** CONSTRUCTOR: sqlite3_str
7162 **
7163 ** ^The [sqlite3_str_new(D)] interface allocates and initializes
7164 ** a new [sqlite3_str] object. To avoid memory leaks, the object returned by
 
 
7165 ** [sqlite3_str_new()] must be freed by a subsequent call to
7166 ** [sqlite3_str_finish(X)].
7167 **
7168 ** ^The [sqlite3_str_new(D)] interface always returns a pointer to a
7169 ** valid [sqlite3_str] object, though in the event of an out-of-memory
7170 ** error the returned object might be a special singleton that will
7171 ** silently reject new text, always return SQLITE_NOMEM from
7172 ** [sqlite3_str_errcode()], always return 0 for
7173 ** [sqlite3_str_length()], and always return NULL from
7174 ** [sqlite3_str_finish(X)]. It is always safe to use the value
7175 ** returned by [sqlite3_str_new(D)] as the sqlite3_str parameter
7176 ** to any of the other [sqlite3_str] methods.
7177 **
7178 ** The D parameter to [sqlite3_str_new(D)] may be NULL. If the
7179 ** D parameter in [sqlite3_str_new(D)] is not NULL, then the maximum
7180 ** length of the string contained in the [sqlite3_str] object will be
7181 ** the value set for [sqlite3_limit](D,[SQLITE_LIMIT_LENGTH]) instead
@@ -8523,15 +8537,15 @@
8537 **
8538 ** If the sqlite3_vtab_nochange(X) routine is called within the [xColumn]
8539 ** method of a [virtual table], then it returns true if and only if the
8540 ** column is being fetched as part of an UPDATE operation during which the
8541 ** column value will not change. Applications might use this to substitute
8542 ** a return value that is less expensive to compute and that the corresponding
8543 ** [xUpdate] method understands as a "no-change" value.
8544 **
8545 ** If the [xColumn] method calls sqlite3_vtab_nochange() and finds that
8546 ** the column is not changed by the UPDATE statement, then the xColumn
8547 ** method can optionally return without setting a result, without calling
8548 ** any of the [sqlite3_result_int|sqlite3_result_xxxxx() interfaces].
8549 ** In that case, [sqlite3_value_nochange(X)] will return true for the
8550 ** same column in the [xUpdate] method.
8551 */
8552

Keyboard Shortcuts

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