Fossil SCM

Update the built-in SQLite to the version that includes the DELETE with ONEPASS optimization and the enhanced EXPLAIN indentation in the shell.

drh 2013-11-19 18:25 trunk
Commit 0830c352ff3ddded8ed454fcf4640dc26d488ec4
3 files changed +24 -12 +397 -426 +7 -8
+24 -12
--- src/shell.c
+++ src/shell.c
@@ -464,10 +464,11 @@
464464
const char *zVfs; /* Name of VFS to use */
465465
sqlite3_stmt *pStmt; /* Current statement if any. */
466466
FILE *pLog; /* Write log output here */
467467
int *aiIndent; /* Array of indents used in MODE_Explain */
468468
int nIndent; /* Size of array aiIndent[] */
469
+ int iIndent; /* Index of current op in aiIndent[] */
469470
};
470471
471472
/*
472473
** These are the allowed modes.
473474
*/
@@ -769,14 +770,14 @@
769770
}
770771
if( p->mode==MODE_Explain && azArg[i] && strlen30(azArg[i])>w ){
771772
w = strlen30(azArg[i]);
772773
}
773774
if( i==1 && p->aiIndent && p->pStmt ){
774
- int iOp = sqlite3_column_int(p->pStmt, 0);
775
- if( iOp<p->nIndent ){
776
- fprintf(p->out, "%*.s", p->aiIndent[iOp], "");
775
+ if( p->iIndent<p->nIndent ){
776
+ fprintf(p->out, "%*.s", p->aiIndent[p->iIndent], "");
777777
}
778
+ p->iIndent++;
778779
}
779780
if( w<0 ){
780781
fprintf(p->out,"%*.*s%s",-w,-w,
781782
azArg[i] ? azArg[i] : p->nullvalue, i==nArg-1 ? "\n": " ");
782783
}else{
@@ -1171,24 +1172,25 @@
11711172
**
11721173
** * For each "Next", "Prev", "VNext" or "VPrev" instruction, indent
11731174
** all opcodes that occur between the p2 jump destination and the opcode
11741175
** itself by 2 spaces.
11751176
**
1176
-** * For each "Goto", if the jump destination is a "Yield", "SeekGt",
1177
-** or "SeekLt" instruction that occurs earlier in the program than
1178
-** the Goto itself, indent all opcodes between the earlier instruction
1177
+** * For each "Goto", if the jump destination is earlier in the program
1178
+** and ends on one of:
1179
+** Yield SeekGt SeekLt RowSetRead
1180
+** then indent all opcodes between the earlier instruction
11791181
** and "Goto" by 2 spaces.
11801182
*/
11811183
static void explain_data_prepare(struct callback_data *p, sqlite3_stmt *pSql){
11821184
const char *zSql; /* The text of the SQL statement */
11831185
const char *z; /* Used to check if this is an EXPLAIN */
11841186
int *abYield = 0; /* True if op is an OP_Yield */
11851187
int nAlloc = 0; /* Allocated size of p->aiIndent[], abYield */
1186
- int iOp;
1188
+ int iOp; /* Index of operation in p->aiIndent[] */
11871189
1188
- const char *azNext[] = { "Next", "Prev", "VPrev", "VNext", 0 };
1189
- const char *azYield[] = { "Yield", "SeekLt", "SeekGt", 0 };
1190
+ const char *azNext[] = { "Next", "Prev", "VPrev", "VNext", "SorterNext", 0 };
1191
+ const char *azYield[] = { "Yield", "SeekLt", "SeekGt", "RowSetRead", 0 };
11901192
const char *azGoto[] = { "Goto", 0 };
11911193
11921194
/* Try to figure out if this is really an EXPLAIN statement. If this
11931195
** cannot be verified, return early. */
11941196
zSql = sqlite3_sql(pSql);
@@ -1196,12 +1198,20 @@
11961198
for(z=zSql; *z==' ' || *z=='\t' || *z=='\n' || *z=='\f' || *z=='\r'; z++);
11971199
if( sqlite3_strnicmp(z, "explain", 7) ) return;
11981200
11991201
for(iOp=0; SQLITE_ROW==sqlite3_step(pSql); iOp++){
12001202
int i;
1203
+ int iAddr = sqlite3_column_int(pSql, 0);
12011204
const char *zOp = (const char*)sqlite3_column_text(pSql, 1);
1205
+
1206
+ /* Set p2 to the P2 field of the current opcode. Then, assuming that
1207
+ ** p2 is an instruction address, set variable p2op to the index of that
1208
+ ** instruction in the aiIndent[] array. p2 and p2op may be different if
1209
+ ** the current instruction is part of a sub-program generated by an
1210
+ ** SQL trigger or foreign key. */
12021211
int p2 = sqlite3_column_int(pSql, 3);
1212
+ int p2op = (p2 + (iOp-iAddr));
12031213
12041214
/* Grow the p->aiIndent array as required */
12051215
if( iOp>=nAlloc ){
12061216
nAlloc += 100;
12071217
p->aiIndent = (int*)sqlite3_realloc(p->aiIndent, nAlloc*sizeof(int));
@@ -1210,17 +1220,18 @@
12101220
abYield[iOp] = str_in_array(zOp, azYield);
12111221
p->aiIndent[iOp] = 0;
12121222
p->nIndent = iOp+1;
12131223
12141224
if( str_in_array(zOp, azNext) ){
1215
- for(i=p2; i<iOp; i++) p->aiIndent[i] += 2;
1225
+ for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
12161226
}
1217
- if( str_in_array(zOp, azGoto) && p2<p->nIndent && abYield[p2] ){
1218
- for(i=p2+1; i<iOp; i++) p->aiIndent[i] += 2;
1227
+ if( str_in_array(zOp, azGoto) && p2op<p->nIndent && abYield[p2op] ){
1228
+ for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
12191229
}
12201230
}
12211231
1232
+ p->iIndent = 0;
12221233
sqlite3_free(abYield);
12231234
sqlite3_reset(pSql);
12241235
}
12251236
12261237
/*
@@ -1228,10 +1239,11 @@
12281239
*/
12291240
static void explain_data_delete(struct callback_data *p){
12301241
sqlite3_free(p->aiIndent);
12311242
p->aiIndent = 0;
12321243
p->nIndent = 0;
1244
+ p->iIndent = 0;
12331245
}
12341246
12351247
/*
12361248
** Execute a statement or set of statements. Print
12371249
** any result rows/columns depending on the current mode
12381250
--- src/shell.c
+++ src/shell.c
@@ -464,10 +464,11 @@
464 const char *zVfs; /* Name of VFS to use */
465 sqlite3_stmt *pStmt; /* Current statement if any. */
466 FILE *pLog; /* Write log output here */
467 int *aiIndent; /* Array of indents used in MODE_Explain */
468 int nIndent; /* Size of array aiIndent[] */
 
469 };
470
471 /*
472 ** These are the allowed modes.
473 */
@@ -769,14 +770,14 @@
769 }
770 if( p->mode==MODE_Explain && azArg[i] && strlen30(azArg[i])>w ){
771 w = strlen30(azArg[i]);
772 }
773 if( i==1 && p->aiIndent && p->pStmt ){
774 int iOp = sqlite3_column_int(p->pStmt, 0);
775 if( iOp<p->nIndent ){
776 fprintf(p->out, "%*.s", p->aiIndent[iOp], "");
777 }
 
778 }
779 if( w<0 ){
780 fprintf(p->out,"%*.*s%s",-w,-w,
781 azArg[i] ? azArg[i] : p->nullvalue, i==nArg-1 ? "\n": " ");
782 }else{
@@ -1171,24 +1172,25 @@
1171 **
1172 ** * For each "Next", "Prev", "VNext" or "VPrev" instruction, indent
1173 ** all opcodes that occur between the p2 jump destination and the opcode
1174 ** itself by 2 spaces.
1175 **
1176 ** * For each "Goto", if the jump destination is a "Yield", "SeekGt",
1177 ** or "SeekLt" instruction that occurs earlier in the program than
1178 ** the Goto itself, indent all opcodes between the earlier instruction
 
1179 ** and "Goto" by 2 spaces.
1180 */
1181 static void explain_data_prepare(struct callback_data *p, sqlite3_stmt *pSql){
1182 const char *zSql; /* The text of the SQL statement */
1183 const char *z; /* Used to check if this is an EXPLAIN */
1184 int *abYield = 0; /* True if op is an OP_Yield */
1185 int nAlloc = 0; /* Allocated size of p->aiIndent[], abYield */
1186 int iOp;
1187
1188 const char *azNext[] = { "Next", "Prev", "VPrev", "VNext", 0 };
1189 const char *azYield[] = { "Yield", "SeekLt", "SeekGt", 0 };
1190 const char *azGoto[] = { "Goto", 0 };
1191
1192 /* Try to figure out if this is really an EXPLAIN statement. If this
1193 ** cannot be verified, return early. */
1194 zSql = sqlite3_sql(pSql);
@@ -1196,12 +1198,20 @@
1196 for(z=zSql; *z==' ' || *z=='\t' || *z=='\n' || *z=='\f' || *z=='\r'; z++);
1197 if( sqlite3_strnicmp(z, "explain", 7) ) return;
1198
1199 for(iOp=0; SQLITE_ROW==sqlite3_step(pSql); iOp++){
1200 int i;
 
1201 const char *zOp = (const char*)sqlite3_column_text(pSql, 1);
 
 
 
 
 
 
1202 int p2 = sqlite3_column_int(pSql, 3);
 
1203
1204 /* Grow the p->aiIndent array as required */
1205 if( iOp>=nAlloc ){
1206 nAlloc += 100;
1207 p->aiIndent = (int*)sqlite3_realloc(p->aiIndent, nAlloc*sizeof(int));
@@ -1210,17 +1220,18 @@
1210 abYield[iOp] = str_in_array(zOp, azYield);
1211 p->aiIndent[iOp] = 0;
1212 p->nIndent = iOp+1;
1213
1214 if( str_in_array(zOp, azNext) ){
1215 for(i=p2; i<iOp; i++) p->aiIndent[i] += 2;
1216 }
1217 if( str_in_array(zOp, azGoto) && p2<p->nIndent && abYield[p2] ){
1218 for(i=p2+1; i<iOp; i++) p->aiIndent[i] += 2;
1219 }
1220 }
1221
 
1222 sqlite3_free(abYield);
1223 sqlite3_reset(pSql);
1224 }
1225
1226 /*
@@ -1228,10 +1239,11 @@
1228 */
1229 static void explain_data_delete(struct callback_data *p){
1230 sqlite3_free(p->aiIndent);
1231 p->aiIndent = 0;
1232 p->nIndent = 0;
 
1233 }
1234
1235 /*
1236 ** Execute a statement or set of statements. Print
1237 ** any result rows/columns depending on the current mode
1238
--- src/shell.c
+++ src/shell.c
@@ -464,10 +464,11 @@
464 const char *zVfs; /* Name of VFS to use */
465 sqlite3_stmt *pStmt; /* Current statement if any. */
466 FILE *pLog; /* Write log output here */
467 int *aiIndent; /* Array of indents used in MODE_Explain */
468 int nIndent; /* Size of array aiIndent[] */
469 int iIndent; /* Index of current op in aiIndent[] */
470 };
471
472 /*
473 ** These are the allowed modes.
474 */
@@ -769,14 +770,14 @@
770 }
771 if( p->mode==MODE_Explain && azArg[i] && strlen30(azArg[i])>w ){
772 w = strlen30(azArg[i]);
773 }
774 if( i==1 && p->aiIndent && p->pStmt ){
775 if( p->iIndent<p->nIndent ){
776 fprintf(p->out, "%*.s", p->aiIndent[p->iIndent], "");
 
777 }
778 p->iIndent++;
779 }
780 if( w<0 ){
781 fprintf(p->out,"%*.*s%s",-w,-w,
782 azArg[i] ? azArg[i] : p->nullvalue, i==nArg-1 ? "\n": " ");
783 }else{
@@ -1171,24 +1172,25 @@
1172 **
1173 ** * For each "Next", "Prev", "VNext" or "VPrev" instruction, indent
1174 ** all opcodes that occur between the p2 jump destination and the opcode
1175 ** itself by 2 spaces.
1176 **
1177 ** * For each "Goto", if the jump destination is earlier in the program
1178 ** and ends on one of:
1179 ** Yield SeekGt SeekLt RowSetRead
1180 ** then indent all opcodes between the earlier instruction
1181 ** and "Goto" by 2 spaces.
1182 */
1183 static void explain_data_prepare(struct callback_data *p, sqlite3_stmt *pSql){
1184 const char *zSql; /* The text of the SQL statement */
1185 const char *z; /* Used to check if this is an EXPLAIN */
1186 int *abYield = 0; /* True if op is an OP_Yield */
1187 int nAlloc = 0; /* Allocated size of p->aiIndent[], abYield */
1188 int iOp; /* Index of operation in p->aiIndent[] */
1189
1190 const char *azNext[] = { "Next", "Prev", "VPrev", "VNext", "SorterNext", 0 };
1191 const char *azYield[] = { "Yield", "SeekLt", "SeekGt", "RowSetRead", 0 };
1192 const char *azGoto[] = { "Goto", 0 };
1193
1194 /* Try to figure out if this is really an EXPLAIN statement. If this
1195 ** cannot be verified, return early. */
1196 zSql = sqlite3_sql(pSql);
@@ -1196,12 +1198,20 @@
1198 for(z=zSql; *z==' ' || *z=='\t' || *z=='\n' || *z=='\f' || *z=='\r'; z++);
1199 if( sqlite3_strnicmp(z, "explain", 7) ) return;
1200
1201 for(iOp=0; SQLITE_ROW==sqlite3_step(pSql); iOp++){
1202 int i;
1203 int iAddr = sqlite3_column_int(pSql, 0);
1204 const char *zOp = (const char*)sqlite3_column_text(pSql, 1);
1205
1206 /* Set p2 to the P2 field of the current opcode. Then, assuming that
1207 ** p2 is an instruction address, set variable p2op to the index of that
1208 ** instruction in the aiIndent[] array. p2 and p2op may be different if
1209 ** the current instruction is part of a sub-program generated by an
1210 ** SQL trigger or foreign key. */
1211 int p2 = sqlite3_column_int(pSql, 3);
1212 int p2op = (p2 + (iOp-iAddr));
1213
1214 /* Grow the p->aiIndent array as required */
1215 if( iOp>=nAlloc ){
1216 nAlloc += 100;
1217 p->aiIndent = (int*)sqlite3_realloc(p->aiIndent, nAlloc*sizeof(int));
@@ -1210,17 +1220,18 @@
1220 abYield[iOp] = str_in_array(zOp, azYield);
1221 p->aiIndent[iOp] = 0;
1222 p->nIndent = iOp+1;
1223
1224 if( str_in_array(zOp, azNext) ){
1225 for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
1226 }
1227 if( str_in_array(zOp, azGoto) && p2op<p->nIndent && abYield[p2op] ){
1228 for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
1229 }
1230 }
1231
1232 p->iIndent = 0;
1233 sqlite3_free(abYield);
1234 sqlite3_reset(pSql);
1235 }
1236
1237 /*
@@ -1228,10 +1239,11 @@
1239 */
1240 static void explain_data_delete(struct callback_data *p){
1241 sqlite3_free(p->aiIndent);
1242 p->aiIndent = 0;
1243 p->nIndent = 0;
1244 p->iIndent = 0;
1245 }
1246
1247 /*
1248 ** Execute a statement or set of statements. Print
1249 ** any result rows/columns depending on the current mode
1250
+397 -426
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -135,11 +135,11 @@
135135
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
136136
** [sqlite_version()] and [sqlite_source_id()].
137137
*/
138138
#define SQLITE_VERSION "3.8.2"
139139
#define SQLITE_VERSION_NUMBER 3008002
140
-#define SQLITE_SOURCE_ID "2013-11-14 19:34:10 10d59226382adcb8016fc2d927e5a0c0b36f3980"
140
+#define SQLITE_SOURCE_ID "2013-11-19 13:55:34 17e8524fc05aa1e6074c19a8ccccc5ab5883103a"
141141
142142
/*
143143
** CAPI3REF: Run-Time Library Version Numbers
144144
** KEYWORDS: sqlite3_version, sqlite3_sourceid
145145
**
@@ -396,11 +396,11 @@
396396
** Restrictions:
397397
**
398398
** <ul>
399399
** <li> The application must insure that the 1st parameter to sqlite3_exec()
400400
** is a valid and open [database connection].
401
-** <li> The application must not close [database connection] specified by
401
+** <li> The application must not close the [database connection] specified by
402402
** the 1st parameter to sqlite3_exec() while sqlite3_exec() is running.
403403
** <li> The application must not modify the SQL statement text passed into
404404
** the 2nd parameter of sqlite3_exec() while sqlite3_exec() is running.
405405
** </ul>
406406
*/
@@ -473,11 +473,11 @@
473473
** about errors. The extended result codes are enabled or disabled
474474
** on a per database connection basis using the
475475
** [sqlite3_extended_result_codes()] API.
476476
**
477477
** Some of the available extended result codes are listed here.
478
-** One may expect the number of extended result codes will be expand
478
+** One may expect the number of extended result codes will increase
479479
** over time. Software that uses extended result codes should expect
480480
** to see new result codes in future releases of SQLite.
481481
**
482482
** The SQLITE_OK result code will never be extended. It will always
483483
** be exactly zero.
@@ -1411,11 +1411,11 @@
14111411
** of 8. Some allocators round up to a larger multiple or to a power of 2.
14121412
** Every memory allocation request coming in through [sqlite3_malloc()]
14131413
** or [sqlite3_realloc()] first calls xRoundup. If xRoundup returns 0,
14141414
** that causes the corresponding memory allocation to fail.
14151415
**
1416
-** The xInit method initializes the memory allocator. (For example,
1416
+** The xInit method initializes the memory allocator. For example,
14171417
** it might allocate any require mutexes or initialize internal data
14181418
** structures. The xShutdown method is invoked (indirectly) by
14191419
** [sqlite3_shutdown()] and should deallocate any resources acquired
14201420
** by xInit. The pAppData pointer is used as the only parameter to
14211421
** xInit and xShutdown.
@@ -3137,11 +3137,10 @@
31373137
** to the [sqlite3_bind_text | bindings] of that [parameter].
31383138
** ^The specific value of WHERE-clause [parameter] might influence the
31393139
** choice of query plan if the parameter is the left-hand side of a [LIKE]
31403140
** or [GLOB] operator or if the parameter is compared to an indexed column
31413141
** and the [SQLITE_ENABLE_STAT3] compile-time option is enabled.
3142
-** the
31433142
** </li>
31443143
** </ol>
31453144
*/
31463145
SQLITE_API int sqlite3_prepare(
31473146
sqlite3 *db, /* Database handle */
@@ -3867,11 +3866,11 @@
38673866
**
38683867
** ^The pointers returned are valid until a type conversion occurs as
38693868
** described above, or until [sqlite3_step()] or [sqlite3_reset()] or
38703869
** [sqlite3_finalize()] is called. ^The memory space used to hold strings
38713870
** and BLOBs is freed automatically. Do <b>not</b> pass the pointers returned
3872
-** [sqlite3_column_blob()], [sqlite3_column_text()], etc. into
3871
+** from [sqlite3_column_blob()], [sqlite3_column_text()], etc. into
38733872
** [sqlite3_free()].
38743873
**
38753874
** ^(If a memory allocation error occurs during the evaluation of any
38763875
** of these routines, a default value is returned. The default value
38773876
** is either the integer 0, the floating point number 0.0, or a NULL
@@ -4945,12 +4944,12 @@
49454944
/*
49464945
** CAPI3REF: Free Memory Used By A Database Connection
49474946
**
49484947
** ^The sqlite3_db_release_memory(D) interface attempts to free as much heap
49494948
** memory as possible from database connection D. Unlike the
4950
-** [sqlite3_release_memory()] interface, this interface is effect even
4951
-** when then [SQLITE_ENABLE_MEMORY_MANAGEMENT] compile-time option is
4949
+** [sqlite3_release_memory()] interface, this interface is in effect even
4950
+** when the [SQLITE_ENABLE_MEMORY_MANAGEMENT] compile-time option is
49524951
** omitted.
49534952
**
49544953
** See also: [sqlite3_release_memory()]
49554954
*/
49564955
SQLITE_API int sqlite3_db_release_memory(sqlite3*);
@@ -9096,22 +9095,22 @@
90969095
#define OP_NotNull 75 /* same as TK_NOTNULL, synopsis: if r[P1]!=NULL goto P2 */
90979096
#define OP_Ne 76 /* same as TK_NE, synopsis: if r[P1]!=r[P3] goto P2 */
90989097
#define OP_Eq 77 /* same as TK_EQ, synopsis: if r[P1]==r[P3] goto P2 */
90999098
#define OP_Gt 78 /* same as TK_GT, synopsis: if r[P1]>r[P3] goto P2 */
91009099
#define OP_Le 79 /* same as TK_LE, synopsis: if r[P1]<=r[P3] goto P2 */
9101
-#define OP_Lt 80 /* same as TK_LT, synopsis: if r[P1]<r[P3] goto P3 */
9100
+#define OP_Lt 80 /* same as TK_LT, synopsis: if r[P1]<r[P3] goto P2 */
91029101
#define OP_Ge 81 /* same as TK_GE, synopsis: if r[P1]>=r[P3] goto P2 */
91039102
#define OP_RowKey 82 /* synopsis: r[P2]=key */
91049103
#define OP_BitAnd 83 /* same as TK_BITAND, synopsis: r[P3]=r[P1]&r[P2] */
91059104
#define OP_BitOr 84 /* same as TK_BITOR, synopsis: r[P3]=r[P1]|r[P2] */
91069105
#define OP_ShiftLeft 85 /* same as TK_LSHIFT, synopsis: r[P3]=r[P2]<<r[P1] */
91079106
#define OP_ShiftRight 86 /* same as TK_RSHIFT, synopsis: r[P3]=r[P2]>>r[P1] */
91089107
#define OP_Add 87 /* same as TK_PLUS, synopsis: r[P3]=r[P1]+r[P2] */
91099108
#define OP_Subtract 88 /* same as TK_MINUS, synopsis: r[P3]=r[P2]-r[P1] */
91109109
#define OP_Multiply 89 /* same as TK_STAR, synopsis: r[P3]=r[P1]*r[P2] */
9111
-#define OP_Divide 90 /* same as TK_SLASH, synopsis: r[P3]=r[P1]/r[P2] */
9112
-#define OP_Remainder 91 /* same as TK_REM, synopsis: r[P3]=r[P1]%r[P2] */
9110
+#define OP_Divide 90 /* same as TK_SLASH, synopsis: r[P3]=r[P2]/r[P1] */
9111
+#define OP_Remainder 91 /* same as TK_REM, synopsis: r[P3]=r[P2]%r[P1] */
91139112
#define OP_Concat 92 /* same as TK_CONCAT, synopsis: r[P3]=r[P2]+r[P1] */
91149113
#define OP_RowData 93 /* synopsis: r[P2]=data */
91159114
#define OP_BitNot 94 /* same as TK_BITNOT, synopsis: r[P1]= ~r[P1] */
91169115
#define OP_String8 95 /* same as TK_STRING, synopsis: r[P2]='P4' */
91179116
#define OP_Rowid 96 /* synopsis: r[P2]=rowid */
@@ -11089,11 +11088,11 @@
1108911088
#define EP_Distinct 0x000010 /* Aggregate function with DISTINCT keyword */
1109011089
#define EP_VarSelect 0x000020 /* pSelect is correlated, not constant */
1109111090
#define EP_DblQuoted 0x000040 /* token.z was originally in "..." */
1109211091
#define EP_InfixFunc 0x000080 /* True for an infix function: LIKE, GLOB, etc */
1109311092
#define EP_Collate 0x000100 /* Tree contains a TK_COLLATE opeartor */
11094
-#define EP_FixedDest 0x000200 /* Result needed in a specific register */
11093
+ /* unused 0x000200 */
1109511094
#define EP_IntValue 0x000400 /* Integer value contained in u.iValue */
1109611095
#define EP_xIsSelect 0x000800 /* x.pSelect is valid (otherwise x.pList is) */
1109711096
#define EP_Skip 0x001000 /* COLLATE, AS, or UNLIKELY */
1109811097
#define EP_Reduced 0x002000 /* Expr struct EXPR_REDUCEDSIZE bytes only */
1109911098
#define EP_TokenOnly 0x004000 /* Expr struct EXPR_TOKENONLYSIZE bytes only */
@@ -11160,12 +11159,17 @@
1116011159
char *zName; /* Token associated with this expression */
1116111160
char *zSpan; /* Original text of the expression */
1116211161
u8 sortOrder; /* 1 for DESC or 0 for ASC */
1116311162
unsigned done :1; /* A flag to indicate when processing is finished */
1116411163
unsigned bSpanIsTab :1; /* zSpan holds DB.TABLE.COLUMN */
11165
- u16 iOrderByCol; /* For ORDER BY, column number in result set */
11166
- u16 iAlias; /* Index into Parse.aAlias[] for zName */
11164
+ union {
11165
+ struct {
11166
+ u16 iOrderByCol; /* For ORDER BY, column number in result set */
11167
+ u16 iAlias; /* Index into Parse.aAlias[] for zName */
11168
+ } x;
11169
+ int iConstExprReg; /* Register in which Expr value is cached */
11170
+ } u;
1116711171
} *a; /* Alloc a power of two greater or equal to nExpr */
1116811172
};
1116911173
1117011174
/*
1117111175
** An instance of this structure is used by the parser to record both
@@ -11538,10 +11542,11 @@
1153811542
u8 tempReg; /* iReg is a temp register that needs to be freed */
1153911543
int iLevel; /* Nesting level */
1154011544
int iReg; /* Reg with value of this column. 0 means none. */
1154111545
int lru; /* Least recently used entry has the smallest value */
1154211546
} aColCache[SQLITE_N_COLCACHE]; /* One for each column cache entry */
11547
+ ExprList *pConstExpr;/* Constant expressions */
1154311548
yDbMask writeMask; /* Start a write transaction on these databases */
1154411549
yDbMask cookieMask; /* Bitmask of schema verified databases */
1154511550
int cookieGoto; /* Address of OP_Goto to cookie verifier subroutine */
1154611551
int cookieValue[SQLITE_MAX_ATTACHED+2]; /* Values of cookies to verify */
1154711552
int regRowid; /* Register holding rowid of CREATE TABLE entry */
@@ -12151,11 +12156,10 @@
1215112156
SQLITE_PRIVATE void sqlite3ExprCacheAffinityChange(Parse*, int, int);
1215212157
SQLITE_PRIVATE int sqlite3ExprCode(Parse*, Expr*, int);
1215312158
SQLITE_PRIVATE int sqlite3ExprCodeTemp(Parse*, Expr*, int*);
1215412159
SQLITE_PRIVATE int sqlite3ExprCodeTarget(Parse*, Expr*, int);
1215512160
SQLITE_PRIVATE int sqlite3ExprCodeAndCache(Parse*, Expr*, int);
12156
-SQLITE_PRIVATE void sqlite3ExprCodeConstants(Parse*, Expr*);
1215712161
SQLITE_PRIVATE int sqlite3ExprCodeExprList(Parse*, ExprList*, int, int);
1215812162
SQLITE_PRIVATE void sqlite3ExprIfTrue(Parse*, Expr*, int, int);
1215912163
SQLITE_PRIVATE void sqlite3ExprIfFalse(Parse*, Expr*, int, int);
1216012164
SQLITE_PRIVATE Table *sqlite3FindTable(sqlite3*,const char*, const char*);
1216112165
SQLITE_PRIVATE Table *sqlite3LocateTable(Parse*,int isView,const char*, const char*);
@@ -12197,11 +12201,11 @@
1219712201
SQLITE_PRIVATE void sqlite3GenerateRowIndexDelete(Parse*, Table*, int, int, int*);
1219812202
SQLITE_PRIVATE int sqlite3GenerateIndexKey(Parse*, Index*, int, int, int, int*);
1219912203
SQLITE_PRIVATE void sqlite3GenerateConstraintChecks(Parse*,Table*,int*,int,int,int,int,
1220012204
u8,u8,int,int*);
1220112205
SQLITE_PRIVATE void sqlite3CompleteInsertion(Parse*,Table*,int,int,int,int*,int,int,int);
12202
-SQLITE_PRIVATE int sqlite3OpenTableAndIndices(Parse*, Table*, int, int, int*, int*);
12206
+SQLITE_PRIVATE int sqlite3OpenTableAndIndices(Parse*, Table*, int, int, u8*, int*, int*);
1220312207
SQLITE_PRIVATE void sqlite3BeginWriteOperation(Parse*, int, int);
1220412208
SQLITE_PRIVATE void sqlite3MultiWrite(Parse*);
1220512209
SQLITE_PRIVATE void sqlite3MayAbort(Parse*);
1220612210
SQLITE_PRIVATE void sqlite3HaltConstraint(Parse*, int, int, char*, i8, u8);
1220712211
SQLITE_PRIVATE void sqlite3UniqueConstraint(Parse*, int, Index*);
@@ -12519,10 +12523,11 @@
1251912523
SQLITE_PRIVATE FuncDef *sqlite3VtabOverloadFunction(sqlite3 *,FuncDef*, int nArg, Expr*);
1252012524
SQLITE_PRIVATE void sqlite3InvalidFunction(sqlite3_context*,int,sqlite3_value**);
1252112525
SQLITE_PRIVATE sqlite3_int64 sqlite3StmtCurrentTime(sqlite3_context*);
1252212526
SQLITE_PRIVATE int sqlite3VdbeParameterIndex(Vdbe*, const char*, int);
1252312527
SQLITE_PRIVATE int sqlite3TransferBindings(sqlite3_stmt *, sqlite3_stmt *);
12528
+SQLITE_PRIVATE void sqlite3ParserReset(Parse*);
1252412529
SQLITE_PRIVATE int sqlite3Reprepare(Vdbe*);
1252512530
SQLITE_PRIVATE void sqlite3ExprListCheckLength(Parse*, ExprList*, const char*);
1252612531
SQLITE_PRIVATE CollSeq *sqlite3BinaryCompareCollSeq(Parse *, Expr *, Expr *);
1252712532
SQLITE_PRIVATE int sqlite3TempInMemory(const sqlite3*);
1252812533
SQLITE_PRIVATE const char *sqlite3JournalModename(int);
@@ -22970,22 +22975,22 @@
2297022975
/* 75 */ "NotNull" OpHelp("if r[P1]!=NULL goto P2"),
2297122976
/* 76 */ "Ne" OpHelp("if r[P1]!=r[P3] goto P2"),
2297222977
/* 77 */ "Eq" OpHelp("if r[P1]==r[P3] goto P2"),
2297322978
/* 78 */ "Gt" OpHelp("if r[P1]>r[P3] goto P2"),
2297422979
/* 79 */ "Le" OpHelp("if r[P1]<=r[P3] goto P2"),
22975
- /* 80 */ "Lt" OpHelp("if r[P1]<r[P3] goto P3"),
22980
+ /* 80 */ "Lt" OpHelp("if r[P1]<r[P3] goto P2"),
2297622981
/* 81 */ "Ge" OpHelp("if r[P1]>=r[P3] goto P2"),
2297722982
/* 82 */ "RowKey" OpHelp("r[P2]=key"),
2297822983
/* 83 */ "BitAnd" OpHelp("r[P3]=r[P1]&r[P2]"),
2297922984
/* 84 */ "BitOr" OpHelp("r[P3]=r[P1]|r[P2]"),
2298022985
/* 85 */ "ShiftLeft" OpHelp("r[P3]=r[P2]<<r[P1]"),
2298122986
/* 86 */ "ShiftRight" OpHelp("r[P3]=r[P2]>>r[P1]"),
2298222987
/* 87 */ "Add" OpHelp("r[P3]=r[P1]+r[P2]"),
2298322988
/* 88 */ "Subtract" OpHelp("r[P3]=r[P2]-r[P1]"),
2298422989
/* 89 */ "Multiply" OpHelp("r[P3]=r[P1]*r[P2]"),
22985
- /* 90 */ "Divide" OpHelp("r[P3]=r[P1]/r[P2]"),
22986
- /* 91 */ "Remainder" OpHelp("r[P3]=r[P1]%r[P2]"),
22990
+ /* 90 */ "Divide" OpHelp("r[P3]=r[P2]/r[P1]"),
22991
+ /* 91 */ "Remainder" OpHelp("r[P3]=r[P2]%r[P1]"),
2298722992
/* 92 */ "Concat" OpHelp("r[P3]=r[P2]+r[P1]"),
2298822993
/* 93 */ "RowData" OpHelp("r[P2]=data"),
2298922994
/* 94 */ "BitNot" OpHelp("r[P1]= ~r[P1]"),
2299022995
/* 95 */ "String8" OpHelp("r[P2]='P4'"),
2299122996
/* 96 */ "Rowid" OpHelp("r[P2]=rowid"),
@@ -58866,10 +58871,11 @@
5886658871
if( sqlite3OpenTempDatabase(pParse) ){
5886758872
sqlite3Error(pErrorDb, pParse->rc, "%s", pParse->zErrMsg);
5886858873
rc = SQLITE_ERROR;
5886958874
}
5887058875
sqlite3DbFree(pErrorDb, pParse->zErrMsg);
58876
+ sqlite3ParserReset(pParse);
5887158877
sqlite3StackFree(pErrorDb, pParse);
5887258878
}
5887358879
if( rc ){
5887458880
return 0;
5887558881
}
@@ -63898,19 +63904,16 @@
6389863904
pMem->u.i = serial_type-8;
6389963905
pMem->flags = MEM_Int;
6390063906
return 0;
6390163907
}
6390263908
default: {
63909
+ static const u16 aFlag[] = { MEM_Blob|MEM_Ephem, MEM_Str|MEM_Ephem };
6390363910
u32 len = (serial_type-12)/2;
6390463911
pMem->z = (char *)buf;
6390563912
pMem->n = len;
6390663913
pMem->xDel = 0;
63907
- if( serial_type&0x01 ){
63908
- pMem->flags = MEM_Str | MEM_Ephem;
63909
- }else{
63910
- pMem->flags = MEM_Blob | MEM_Ephem;
63911
- }
63914
+ pMem->flags = aFlag[serial_type&1];
6391263915
return len;
6391363916
}
6391463917
}
6391563918
return 0;
6391663919
}
@@ -67793,23 +67796,23 @@
6779367796
** Subtract the value in register P1 from the value in register P2
6779467797
** and store the result in register P3.
6779567798
** If either input is NULL, the result is NULL.
6779667799
*/
6779767800
/* Opcode: Divide P1 P2 P3 * *
67798
-** Synopsis: r[P3]=r[P1]/r[P2]
67801
+** Synopsis: r[P3]=r[P2]/r[P1]
6779967802
**
6780067803
** Divide the value in register P1 by the value in register P2
6780167804
** and store the result in register P3 (P3=P2/P1). If the value in
6780267805
** register P1 is zero, then the result is NULL. If either input is
6780367806
** NULL, the result is NULL.
6780467807
*/
6780567808
/* Opcode: Remainder P1 P2 P3 * *
67806
-** Synopsis: r[P3]=r[P1]%r[P2]
67809
+** Synopsis: r[P3]=r[P2]%r[P1]
6780767810
**
67808
-** Compute the remainder after integer division of the value in
67809
-** register P1 by the value in register P2 and store the result in P3.
67810
-** If the value in register P2 is zero the result is NULL.
67811
+** Compute the remainder after integer register P2 is divided by
67812
+** register P1 and store the result in register P3.
67813
+** If the value in register P1 is zero the result is NULL.
6781167814
** If either operand is NULL, the result is NULL.
6781267815
*/
6781367816
case OP_Add: /* same as TK_PLUS, in1, in2, out3 */
6781467817
case OP_Subtract: /* same as TK_MINUS, in1, in2, out3 */
6781567818
case OP_Multiply: /* same as TK_STAR, in1, in2, out3 */
@@ -68274,11 +68277,11 @@
6827468277
break;
6827568278
}
6827668279
#endif /* !defined(SQLITE_OMIT_CAST) && !defined(SQLITE_OMIT_FLOATING_POINT) */
6827768280
6827868281
/* Opcode: Lt P1 P2 P3 P4 P5
68279
-** Synopsis: if r[P1]<r[P3] goto P3
68282
+** Synopsis: if r[P1]<r[P3] goto P2
6828068283
**
6828168284
** Compare the values in register P1 and P3. If reg(P3)<reg(P1) then
6828268285
** jump to address P2.
6828368286
**
6828468287
** If the SQLITE_JUMPIFNULL bit of P5 is set and either reg(P1) or
@@ -73278,10 +73281,11 @@
7327873281
if( pBlob && pBlob->pStmt ) sqlite3VdbeFinalize((Vdbe *)pBlob->pStmt);
7327973282
sqlite3DbFree(db, pBlob);
7328073283
}
7328173284
sqlite3Error(db, rc, (zErr ? "%s" : 0), zErr);
7328273285
sqlite3DbFree(db, zErr);
73286
+ sqlite3ParserReset(pParse);
7328373287
sqlite3StackFree(db, pParse);
7328473288
rc = sqlite3ApiExit(db, rc);
7328573289
sqlite3_mutex_leave(db->mutex);
7328673290
return rc;
7328773291
}
@@ -75243,14 +75247,14 @@
7524375247
if( pOrig->op!=TK_COLUMN && zType[0]!='G' ){
7524475248
incrAggFunctionDepth(pDup, nSubquery);
7524575249
pDup = sqlite3PExpr(pParse, TK_AS, pDup, 0, 0);
7524675250
if( pDup==0 ) return;
7524775251
ExprSetProperty(pDup, EP_Skip);
75248
- if( pEList->a[iCol].iAlias==0 ){
75249
- pEList->a[iCol].iAlias = (u16)(++pParse->nAlias);
75252
+ if( pEList->a[iCol].u.x.iAlias==0 ){
75253
+ pEList->a[iCol].u.x.iAlias = (u16)(++pParse->nAlias);
7525075254
}
75251
- pDup->iTable = pEList->a[iCol].iAlias;
75255
+ pDup->iTable = pEList->a[iCol].u.x.iAlias;
7525275256
}
7525375257
if( pExpr->op==TK_COLLATE ){
7525475258
pDup = sqlite3ExprAddCollateString(pParse, pDup, pExpr->u.zToken);
7525575259
}
7525675260
@@ -76111,11 +76115,11 @@
7611176115
assert( pItem->pExpr->op==TK_COLLATE );
7611276116
assert( pItem->pExpr->pLeft==pE );
7611376117
pItem->pExpr->pLeft = pNew;
7611476118
}
7611576119
sqlite3ExprDelete(db, pE);
76116
- pItem->iOrderByCol = (u16)iCol;
76120
+ pItem->u.x.iOrderByCol = (u16)iCol;
7611776121
pItem->done = 1;
7611876122
}else{
7611976123
moreToDo = 1;
7612076124
}
7612176125
}
@@ -76132,12 +76136,12 @@
7613276136
}
7613376137
7613476138
/*
7613576139
** Check every term in the ORDER BY or GROUP BY clause pOrderBy of
7613676140
** the SELECT statement pSelect. If any term is reference to a
76137
-** result set expression (as determined by the ExprList.a.iOrderByCol field)
76138
-** then convert that term into a copy of the corresponding result set
76141
+** result set expression (as determined by the ExprList.a.u.x.iOrderByCol
76142
+** field) then convert that term into a copy of the corresponding result set
7613976143
** column.
7614076144
**
7614176145
** If any errors are detected, add an error message to pParse and
7614276146
** return non-zero. Return zero if no errors are seen.
7614376147
*/
@@ -76160,16 +76164,16 @@
7616076164
}
7616176165
#endif
7616276166
pEList = pSelect->pEList;
7616376167
assert( pEList!=0 ); /* sqlite3SelectNew() guarantees this */
7616476168
for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
76165
- if( pItem->iOrderByCol ){
76166
- if( pItem->iOrderByCol>pEList->nExpr ){
76169
+ if( pItem->u.x.iOrderByCol ){
76170
+ if( pItem->u.x.iOrderByCol>pEList->nExpr ){
7616776171
resolveOutOfRangeError(pParse, zType, i+1, pEList->nExpr);
7616876172
return 1;
7616976173
}
76170
- resolveAlias(pParse, pEList, pItem->iOrderByCol-1, pItem->pExpr, zType,0);
76174
+ resolveAlias(pParse, pEList, pItem->u.x.iOrderByCol-1, pItem->pExpr, zType,0);
7617176175
}
7617276176
}
7617376177
return 0;
7617476178
}
7617576179
@@ -76214,11 +76218,11 @@
7621476218
if( iCol>0 ){
7621576219
/* If an AS-name match is found, mark this ORDER BY column as being
7621676220
** a copy of the iCol-th result-set column. The subsequent call to
7621776221
** sqlite3ResolveOrderGroupBy() will convert the expression to a
7621876222
** copy of the iCol-th result-set expression. */
76219
- pItem->iOrderByCol = (u16)iCol;
76223
+ pItem->u.x.iOrderByCol = (u16)iCol;
7622076224
continue;
7622176225
}
7622276226
}
7622376227
if( sqlite3ExprIsInteger(pE2, &iCol) ){
7622476228
/* The ORDER BY term is an integer constant. Again, set the column
@@ -76226,22 +76230,22 @@
7622676230
** order-by term to a copy of the result-set expression */
7622776231
if( iCol<1 || iCol>0xffff ){
7622876232
resolveOutOfRangeError(pParse, zType, i+1, nResult);
7622976233
return 1;
7623076234
}
76231
- pItem->iOrderByCol = (u16)iCol;
76235
+ pItem->u.x.iOrderByCol = (u16)iCol;
7623276236
continue;
7623376237
}
7623476238
7623576239
/* Otherwise, treat the ORDER BY term as an ordinary expression */
76236
- pItem->iOrderByCol = 0;
76240
+ pItem->u.x.iOrderByCol = 0;
7623776241
if( sqlite3ResolveExprNames(pNC, pE) ){
7623876242
return 1;
7623976243
}
7624076244
for(j=0; j<pSelect->pEList->nExpr; j++){
7624176245
if( sqlite3ExprCompare(pE, pSelect->pEList->a[j].pExpr, -1)==0 ){
76242
- pItem->iOrderByCol = j+1;
76246
+ pItem->u.x.iOrderByCol = j+1;
7624376247
}
7624476248
}
7624576249
}
7624676250
return sqlite3ResolveOrderGroupBy(pParse, pSelect, pOrderBy, zType);
7624776251
}
@@ -77516,12 +77520,11 @@
7751677520
pItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
7751777521
pItem->zSpan = sqlite3DbStrDup(db, pOldItem->zSpan);
7751877522
pItem->sortOrder = pOldItem->sortOrder;
7751977523
pItem->done = 0;
7752077524
pItem->bSpanIsTab = pOldItem->bSpanIsTab;
77521
- pItem->iOrderByCol = pOldItem->iOrderByCol;
77522
- pItem->iAlias = pOldItem->iAlias;
77525
+ pItem->u = pOldItem->u;
7752377526
}
7752477527
return pNew;
7752577528
}
7752677529
7752777530
/*
@@ -78942,10 +78945,11 @@
7894278945
int inReg = target; /* Results stored in register inReg */
7894378946
int regFree1 = 0; /* If non-zero free this temporary register */
7894478947
int regFree2 = 0; /* If non-zero free this temporary register */
7894578948
int r1, r2, r3, r4; /* Various register numbers */
7894678949
sqlite3 *db = pParse->db; /* The database connection */
78950
+ Expr tempX; /* Temporary expression node */
7894778951
7894878952
assert( target>0 && target<=pParse->nMem );
7894978953
if( v==0 ){
7895078954
assert( pParse->db->mallocFailed );
7895178955
return 0;
@@ -79161,12 +79165,14 @@
7916179165
}else if( pLeft->op==TK_FLOAT ){
7916279166
assert( !ExprHasProperty(pExpr, EP_IntValue) );
7916379167
codeReal(v, pLeft->u.zToken, 1, target);
7916479168
#endif
7916579169
}else{
79166
- regFree1 = r1 = sqlite3GetTempReg(pParse);
79167
- sqlite3VdbeAddOp2(v, OP_Integer, 0, r1);
79170
+ tempX.op = TK_INTEGER;
79171
+ tempX.flags = EP_IntValue|EP_TokenOnly;
79172
+ tempX.u.iValue = 0;
79173
+ r1 = sqlite3ExprCodeTemp(pParse, &tempX, &regFree1);
7916879174
r2 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree2);
7916979175
sqlite3VdbeAddOp3(v, OP_Subtract, r2, r1, target);
7917079176
testcase( regFree2==0 );
7917179177
}
7917279178
inReg = target;
@@ -79478,11 +79484,10 @@
7947879484
int nExpr; /* 2x number of WHEN terms */
7947979485
int i; /* Loop counter */
7948079486
ExprList *pEList; /* List of WHEN terms */
7948179487
struct ExprList_item *aListelem; /* Array of WHEN terms */
7948279488
Expr opCompare; /* The X==Ei expression */
79483
- Expr cacheX; /* Cached expression X */
7948479489
Expr *pX; /* The X expression */
7948579490
Expr *pTest = 0; /* X==Ei (form A) or just Ei (form B) */
7948679491
VVA_ONLY( int iCacheLevel = pParse->iCacheLevel; )
7948779492
7948879493
assert( !ExprHasProperty(pExpr, EP_xIsSelect) && pExpr->x.pList );
@@ -79490,17 +79495,16 @@
7949079495
pEList = pExpr->x.pList;
7949179496
aListelem = pEList->a;
7949279497
nExpr = pEList->nExpr;
7949379498
endLabel = sqlite3VdbeMakeLabel(v);
7949479499
if( (pX = pExpr->pLeft)!=0 ){
79495
- cacheX = *pX;
79500
+ tempX = *pX;
7949679501
testcase( pX->op==TK_COLUMN );
79497
- testcase( pX->op==TK_REGISTER );
79498
- exprToRegister(&cacheX, sqlite3ExprCodeTemp(pParse, pX, &regFree1));
79502
+ exprToRegister(&tempX, sqlite3ExprCodeTemp(pParse, pX, &regFree1));
7949979503
testcase( regFree1==0 );
7950079504
opCompare.op = TK_EQ;
79501
- opCompare.pLeft = &cacheX;
79505
+ opCompare.pLeft = &tempX;
7950279506
pTest = &opCompare;
7950379507
/* Ticket b351d95f9cd5ef17e9d9dbae18f5ca8611190001:
7950479508
** The value in regFree1 might get SCopy-ed into the file result.
7950579509
** So make sure that the regFree1 register is not reused for other
7950679510
** purposes and possibly overwritten. */
@@ -79516,11 +79520,10 @@
7951679520
}
7951779521
nextCase = sqlite3VdbeMakeLabel(v);
7951879522
testcase( pTest->op==TK_COLUMN );
7951979523
sqlite3ExprIfFalse(pParse, pTest, nextCase, SQLITE_JUMPIFNULL);
7952079524
testcase( aListelem[i+1].pExpr->op==TK_COLUMN );
79521
- testcase( aListelem[i+1].pExpr->op==TK_REGISTER );
7952279525
sqlite3ExprCode(pParse, aListelem[i+1].pExpr, target);
7952379526
sqlite3VdbeAddOp2(v, OP_Goto, 0, endLabel);
7952479527
sqlite3ExprCachePop(pParse, 1);
7952579528
sqlite3VdbeResolveLabel(v, nextCase);
7952679529
}
@@ -79575,19 +79578,45 @@
7957579578
** are stored.
7957679579
**
7957779580
** If the register is a temporary register that can be deallocated,
7957879581
** then write its number into *pReg. If the result register is not
7957979582
** a temporary, then set *pReg to zero.
79583
+**
79584
+** If pExpr is a constant, then this routine might generate this
79585
+** code to fill the register in the initialization section of the
79586
+** VDBE program, in order to factor it out of the evaluation loop.
7958079587
*/
7958179588
SQLITE_PRIVATE int sqlite3ExprCodeTemp(Parse *pParse, Expr *pExpr, int *pReg){
79582
- int r1 = sqlite3GetTempReg(pParse);
79583
- int r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1);
79584
- if( r2==r1 ){
79585
- *pReg = r1;
79589
+ int r2;
79590
+ pExpr = sqlite3ExprSkipCollate(pExpr);
79591
+ if( pParse->cookieGoto>0
79592
+ && pExpr->op!=TK_REGISTER
79593
+ && sqlite3ExprIsConstantNotJoin(pExpr)
79594
+ ){
79595
+ ExprList *p = pParse->pConstExpr;
79596
+ int i;
79597
+ *pReg = 0;
79598
+ if( p ){
79599
+ for(i=0; i<p->nExpr; i++){
79600
+ if( sqlite3ExprCompare(p->a[i].pExpr, pExpr, -1)==0 ){
79601
+ return p->a[i].u.iConstExprReg;
79602
+ }
79603
+ }
79604
+ }
79605
+ p = sqlite3ExprListAppend(pParse, p, sqlite3ExprDup(pParse->db, pExpr, 0));
79606
+ pParse->pConstExpr = p;
79607
+ r2 = ++pParse->nMem;
79608
+ if( p ) p->a[p->nExpr-1].u.iConstExprReg = r2;
7958679609
}else{
79587
- sqlite3ReleaseTempReg(pParse, r1);
79588
- *pReg = 0;
79610
+ int r1 = sqlite3GetTempReg(pParse);
79611
+ r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1);
79612
+ if( r2==r1 ){
79613
+ *pReg = r1;
79614
+ }else{
79615
+ sqlite3ReleaseTempReg(pParse, r1);
79616
+ *pReg = 0;
79617
+ }
7958979618
}
7959079619
return r2;
7959179620
}
7959279621
7959379622
/*
@@ -79626,16 +79655,17 @@
7962679655
SQLITE_PRIVATE int sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr, int target){
7962779656
Vdbe *v = pParse->pVdbe;
7962879657
int inReg;
7962979658
inReg = sqlite3ExprCode(pParse, pExpr, target);
7963079659
assert( target>0 );
79631
- /* This routine is called for terms to INSERT or UPDATE. And the only
79632
- ** other place where expressions can be converted into TK_REGISTER is
79633
- ** in WHERE clause processing. So as currently implemented, there is
79634
- ** no way for a TK_REGISTER to exist here. But it seems prudent to
79635
- ** keep the ALWAYS() in case the conditions above change with future
79636
- ** modifications or enhancements. */
79660
+ /* The only place, other than this routine, where expressions can be
79661
+ ** converted to TK_REGISTER is internal subexpressions in BETWEEN and
79662
+ ** CASE operators. Neither ever calls this routine. And this routine
79663
+ ** is never called twice on the same expression. Hence it is impossible
79664
+ ** for the input to this routine to already be a register. Nevertheless,
79665
+ ** it seems prudent to keep the ALWAYS() in case the conditions above
79666
+ ** change with future modifications or enhancements. */
7963779667
if( ALWAYS(pExpr->op!=TK_REGISTER) ){
7963879668
int iMem;
7963979669
iMem = ++pParse->nMem;
7964079670
sqlite3VdbeAddOp2(v, OP_Copy, inReg, iMem);
7964179671
exprToRegister(pExpr, iMem);
@@ -79913,144 +79943,10 @@
7991379943
}
7991479944
sqlite3ExplainPop(pOut);
7991579945
}
7991679946
}
7991779947
#endif /* SQLITE_DEBUG */
79918
-
79919
-/*
79920
-** Return TRUE if pExpr is an constant expression that is appropriate
79921
-** for factoring out of a loop. Appropriate expressions are:
79922
-**
79923
-** * Any expression that evaluates to two or more opcodes.
79924
-**
79925
-** * Any OP_Integer, OP_Real, OP_String, OP_Blob, OP_Null,
79926
-** or OP_Variable that does not need to be placed in a
79927
-** specific register.
79928
-**
79929
-** There is no point in factoring out single-instruction constant
79930
-** expressions that need to be placed in a particular register.
79931
-** We could factor them out, but then we would end up adding an
79932
-** OP_SCopy instruction to move the value into the correct register
79933
-** later. We might as well just use the original instruction and
79934
-** avoid the OP_SCopy.
79935
-*/
79936
-static int isAppropriateForFactoring(Expr *p){
79937
- if( !sqlite3ExprIsConstantNotJoin(p) ){
79938
- return 0; /* Only constant expressions are appropriate for factoring */
79939
- }
79940
- if( (p->flags & EP_FixedDest)==0 ){
79941
- return 1; /* Any constant without a fixed destination is appropriate */
79942
- }
79943
- while( p->op==TK_UPLUS ) p = p->pLeft;
79944
- switch( p->op ){
79945
-#ifndef SQLITE_OMIT_BLOB_LITERAL
79946
- case TK_BLOB:
79947
-#endif
79948
- case TK_VARIABLE:
79949
- case TK_INTEGER:
79950
- case TK_FLOAT:
79951
- case TK_NULL:
79952
- case TK_STRING: {
79953
- testcase( p->op==TK_BLOB );
79954
- testcase( p->op==TK_VARIABLE );
79955
- testcase( p->op==TK_INTEGER );
79956
- testcase( p->op==TK_FLOAT );
79957
- testcase( p->op==TK_NULL );
79958
- testcase( p->op==TK_STRING );
79959
- /* Single-instruction constants with a fixed destination are
79960
- ** better done in-line. If we factor them, they will just end
79961
- ** up generating an OP_SCopy to move the value to the destination
79962
- ** register. */
79963
- return 0;
79964
- }
79965
- case TK_UMINUS: {
79966
- if( p->pLeft->op==TK_FLOAT || p->pLeft->op==TK_INTEGER ){
79967
- return 0;
79968
- }
79969
- break;
79970
- }
79971
- default: {
79972
- break;
79973
- }
79974
- }
79975
- return 1;
79976
-}
79977
-
79978
-/*
79979
-** If pExpr is a constant expression that is appropriate for
79980
-** factoring out of a loop, then evaluate the expression
79981
-** into a register and convert the expression into a TK_REGISTER
79982
-** expression.
79983
-*/
79984
-static int evalConstExpr(Walker *pWalker, Expr *pExpr){
79985
- Parse *pParse = pWalker->pParse;
79986
- switch( pExpr->op ){
79987
- case TK_IN:
79988
- case TK_REGISTER: {
79989
- return WRC_Prune;
79990
- }
79991
- case TK_COLLATE: {
79992
- return WRC_Continue;
79993
- }
79994
- case TK_FUNCTION:
79995
- case TK_AGG_FUNCTION:
79996
- case TK_CONST_FUNC: {
79997
- /* The arguments to a function have a fixed destination.
79998
- ** Mark them this way to avoid generated unneeded OP_SCopy
79999
- ** instructions.
80000
- */
80001
- ExprList *pList = pExpr->x.pList;
80002
- assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
80003
- if( pList ){
80004
- int i = pList->nExpr;
80005
- struct ExprList_item *pItem = pList->a;
80006
- for(; i>0; i--, pItem++){
80007
- if( ALWAYS(pItem->pExpr) ) pItem->pExpr->flags |= EP_FixedDest;
80008
- }
80009
- }
80010
- break;
80011
- }
80012
- }
80013
- if( isAppropriateForFactoring(pExpr) ){
80014
- int r1 = ++pParse->nMem;
80015
- int r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1);
80016
- /* If r2!=r1, it means that register r1 is never used. That is harmless
80017
- ** but suboptimal, so we want to know about the situation to fix it.
80018
- ** Hence the following assert: */
80019
- assert( r2==r1 );
80020
- exprToRegister(pExpr, r2);
80021
- return WRC_Prune;
80022
- }
80023
- return WRC_Continue;
80024
-}
80025
-
80026
-/*
80027
-** Preevaluate constant subexpressions within pExpr and store the
80028
-** results in registers. Modify pExpr so that the constant subexpresions
80029
-** are TK_REGISTER opcodes that refer to the precomputed values.
80030
-**
80031
-** This routine is a no-op if the jump to the cookie-check code has
80032
-** already occur. Since the cookie-check jump is generated prior to
80033
-** any other serious processing, this check ensures that there is no
80034
-** way to accidently bypass the constant initializations.
80035
-**
80036
-** This routine is also a no-op if the SQLITE_FactorOutConst optimization
80037
-** is disabled via the sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS)
80038
-** interface. This allows test logic to verify that the same answer is
80039
-** obtained for queries regardless of whether or not constants are
80040
-** precomputed into registers or if they are inserted in-line.
80041
-*/
80042
-SQLITE_PRIVATE void sqlite3ExprCodeConstants(Parse *pParse, Expr *pExpr){
80043
- Walker w;
80044
- if( pParse->cookieGoto ) return;
80045
- if( OptimizationDisabled(pParse->db, SQLITE_FactorOutConst) ) return;
80046
- memset(&w, 0, sizeof(w));
80047
- w.xExprCallback = evalConstExpr;
80048
- w.pParse = pParse;
80049
- sqlite3WalkExpr(&w, pExpr);
80050
-}
80051
-
8005279948
8005379949
/*
8005479950
** Generate code that pushes the value of every element of the given
8005579951
** expression list into a sequence of registers beginning at target.
8005679952
**
@@ -80425,44 +80321,46 @@
8042580321
** this routine is used, it does not hurt to get an extra 2 - that
8042680322
** just might result in some slightly slower code. But returning
8042780323
** an incorrect 0 or 1 could lead to a malfunction.
8042880324
*/
8042980325
SQLITE_PRIVATE int sqlite3ExprCompare(Expr *pA, Expr *pB, int iTab){
80430
- if( pA==0||pB==0 ){
80326
+ u32 combinedFlags;
80327
+ if( pA==0 || pB==0 ){
8043180328
return pB==pA ? 0 : 2;
8043280329
}
80433
- assert( !ExprHasProperty(pA, EP_TokenOnly|EP_Reduced) );
80434
- assert( !ExprHasProperty(pB, EP_TokenOnly|EP_Reduced) );
80435
- if( ExprHasProperty(pA, EP_xIsSelect) || ExprHasProperty(pB, EP_xIsSelect) ){
80330
+ combinedFlags = pA->flags | pB->flags;
80331
+ if( combinedFlags & EP_IntValue ){
80332
+ if( (pA->flags&pB->flags&EP_IntValue)!=0 && pA->u.iValue==pB->u.iValue ){
80333
+ return 0;
80334
+ }
8043680335
return 2;
8043780336
}
80438
- if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 2;
80439
- if( pA->op!=pB->op && (pA->op!=TK_REGISTER || pA->op2!=pB->op) ){
80337
+ if( pA->op!=pB->op ){
8044080338
if( pA->op==TK_COLLATE && sqlite3ExprCompare(pA->pLeft, pB, iTab)<2 ){
8044180339
return 1;
8044280340
}
8044380341
if( pB->op==TK_COLLATE && sqlite3ExprCompare(pA, pB->pLeft, iTab)<2 ){
8044480342
return 1;
8044580343
}
8044680344
return 2;
8044780345
}
80448
- if( sqlite3ExprCompare(pA->pLeft, pB->pLeft, iTab) ) return 2;
80449
- if( sqlite3ExprCompare(pA->pRight, pB->pRight, iTab) ) return 2;
80450
- if( sqlite3ExprListCompare(pA->x.pList, pB->x.pList, iTab) ) return 2;
80451
- if( pA->iColumn!=pB->iColumn ) return 2;
80452
- if( pA->iTable!=pB->iTable
80453
- && pA->op!=TK_REGISTER
80454
- && (pA->iTable!=iTab || NEVER(pB->iTable>=0)) ) return 2;
80455
- if( ExprHasProperty(pA, EP_IntValue) ){
80456
- if( !ExprHasProperty(pB, EP_IntValue) || pA->u.iValue!=pB->u.iValue ){
80457
- return 2;
80458
- }
80459
- }else if( pA->op!=TK_COLUMN && ALWAYS(pA->op!=TK_AGG_COLUMN) && pA->u.zToken){
80460
- if( ExprHasProperty(pB, EP_IntValue) || NEVER(pB->u.zToken==0) ) return 2;
80346
+ if( pA->op!=TK_COLUMN && ALWAYS(pA->op!=TK_AGG_COLUMN) && pA->u.zToken ){
8046180347
if( strcmp(pA->u.zToken,pB->u.zToken)!=0 ){
8046280348
return pA->op==TK_COLLATE ? 1 : 2;
8046380349
}
80350
+ }
80351
+ if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 2;
80352
+ if( ALWAYS((combinedFlags & EP_TokenOnly)==0) ){
80353
+ if( combinedFlags & EP_xIsSelect ) return 2;
80354
+ if( sqlite3ExprCompare(pA->pLeft, pB->pLeft, iTab) ) return 2;
80355
+ if( sqlite3ExprCompare(pA->pRight, pB->pRight, iTab) ) return 2;
80356
+ if( sqlite3ExprListCompare(pA->x.pList, pB->x.pList, iTab) ) return 2;
80357
+ if( ALWAYS((combinedFlags & EP_Reduced)==0) ){
80358
+ if( pA->iColumn!=pB->iColumn ) return 2;
80359
+ if( pA->iTable!=pB->iTable
80360
+ && (pA->iTable!=iTab || NEVER(pB->iTable>=0)) ) return 2;
80361
+ }
8046480362
}
8046580363
return 0;
8046680364
}
8046780365
8046880366
/*
@@ -84435,11 +84333,11 @@
8443584333
** transaction on each used database and to verify the schema cookie
8443684334
** on each used database.
8443784335
*/
8443884336
if( pParse->cookieGoto>0 ){
8443984337
yDbMask mask;
84440
- int iDb;
84338
+ int iDb, i, addr;
8444184339
sqlite3VdbeJumpHere(v, pParse->cookieGoto-1);
8444284340
for(iDb=0, mask=1; iDb<db->nDb; mask<<=1, iDb++){
8444384341
if( (mask & pParse->cookieMask)==0 ) continue;
8444484342
sqlite3VdbeUsesBtree(v, iDb);
8444584343
sqlite3VdbeAddOp2(v,OP_Transaction, iDb, (mask & pParse->writeMask)!=0);
@@ -84449,18 +84347,15 @@
8444984347
iDb, pParse->cookieValue[iDb],
8445084348
db->aDb[iDb].pSchema->iGeneration);
8445184349
}
8445284350
}
8445384351
#ifndef SQLITE_OMIT_VIRTUALTABLE
84454
- {
84455
- int i;
84456
- for(i=0; i<pParse->nVtabLock; i++){
84457
- char *vtab = (char *)sqlite3GetVTable(db, pParse->apVtabLock[i]);
84458
- sqlite3VdbeAddOp4(v, OP_VBegin, 0, 0, 0, vtab, P4_VTAB);
84459
- }
84460
- pParse->nVtabLock = 0;
84461
- }
84352
+ for(i=0; i<pParse->nVtabLock; i++){
84353
+ char *vtab = (char *)sqlite3GetVTable(db, pParse->apVtabLock[i]);
84354
+ sqlite3VdbeAddOp4(v, OP_VBegin, 0, 0, 0, vtab, P4_VTAB);
84355
+ }
84356
+ pParse->nVtabLock = 0;
8446284357
#endif
8446384358
8446484359
/* Once all the cookies have been verified and transactions opened,
8446584360
** obtain the required table-locks. This is a no-op unless the
8446684361
** shared-cache feature is enabled.
@@ -84468,13 +84363,23 @@
8446884363
codeTableLocks(pParse);
8446984364
8447084365
/* Initialize any AUTOINCREMENT data structures required.
8447184366
*/
8447284367
sqlite3AutoincrementBegin(pParse);
84368
+
84369
+ /* Code constant expressions that where factored out of inner loops */
84370
+ addr = pParse->cookieGoto;
84371
+ if( pParse->pConstExpr ){
84372
+ ExprList *pEL = pParse->pConstExpr;
84373
+ pParse->cookieGoto = 0;
84374
+ for(i=0; i<pEL->nExpr; i++){
84375
+ sqlite3ExprCode(pParse, pEL->a[i].pExpr, pEL->a[i].u.iConstExprReg);
84376
+ }
84377
+ }
8447384378
8447484379
/* Finally, jump back to the beginning of the executable code. */
84475
- sqlite3VdbeAddOp2(v, OP_Goto, 0, pParse->cookieGoto);
84380
+ sqlite3VdbeAddOp2(v, OP_Goto, 0, addr);
8447684381
}
8447784382
}
8447884383
8447984384
8448084385
/* Get the VDBE program ready for execution
@@ -89185,24 +89090,38 @@
8918589090
Expr *pWhere /* The WHERE clause. May be null */
8918689091
){
8918789092
Vdbe *v; /* The virtual database engine */
8918889093
Table *pTab; /* The table from which records will be deleted */
8918989094
const char *zDb; /* Name of database holding pTab */
89190
- int end, addr = 0; /* A couple addresses of generated code */
8919189095
int i; /* Loop counter */
8919289096
WhereInfo *pWInfo; /* Information about the WHERE clause */
8919389097
Index *pIdx; /* For looping over indices of the table */
8919489098
int iTabCur; /* Cursor number for the table */
8919589099
int iDataCur; /* VDBE cursor for the canonical data source */
8919689100
int iIdxCur; /* Cursor number of the first index */
89101
+ int nIdx; /* Number of indices */
8919789102
sqlite3 *db; /* Main database structure */
8919889103
AuthContext sContext; /* Authorization context */
8919989104
NameContext sNC; /* Name context to resolve expressions in */
8920089105
int iDb; /* Database number */
8920189106
int memCnt = -1; /* Memory cell used for change counting */
8920289107
int rcauth; /* Value returned by authorization callback */
89203
-
89108
+ int okOnePass; /* True for one-pass algorithm without the FIFO */
89109
+ int aiCurOnePass[2]; /* The write cursors opened by WHERE_ONEPASS */
89110
+ u8 *aToOpen = 0; /* Open cursor iTabCur+j if aToOpen[j] is true */
89111
+ Index *pPk; /* The PRIMARY KEY index on the table */
89112
+ int iPk; /* First of nPk registers holding PRIMARY KEY value */
89113
+ i16 nPk = 1; /* Number of columns in the PRIMARY KEY */
89114
+ int iKey; /* Memory cell holding key of row to be deleted */
89115
+ i16 nKey; /* Number of memory cells in the row key */
89116
+ int iEphCur = 0; /* Ephemeral table holding all primary key values */
89117
+ int iRowSet = 0; /* Register for rowset of rows to delete */
89118
+ int addrBypass = 0; /* Address of jump over the delete logic */
89119
+ int addrLoop = 0; /* Top of the delete loop */
89120
+ int addrDelete = 0; /* Jump directly to the delete logic */
89121
+ int addrEphOpen = 0; /* Instruction to open the Ephermeral table */
89122
+
8920489123
#ifndef SQLITE_OMIT_TRIGGER
8920589124
int isView; /* True if attempting to delete from a view */
8920689125
Trigger *pTrigger; /* List of table triggers, if required */
8920789126
#endif
8920889127
@@ -89253,15 +89172,15 @@
8925389172
if( rcauth==SQLITE_DENY ){
8925489173
goto delete_from_cleanup;
8925589174
}
8925689175
assert(!isView || pTrigger);
8925789176
89258
- /* Assign cursor number to the table and all its indices.
89177
+ /* Assign cursor numbers to the table and all its indices.
8925989178
*/
8926089179
assert( pTabList->nSrc==1 );
8926189180
iTabCur = pTabList->a[0].iCursor = pParse->nTab++;
89262
- for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
89181
+ for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){
8926389182
pParse->nTab++;
8926489183
}
8926589184
8926689185
/* Start the view context
8926789186
*/
@@ -89323,132 +89242,162 @@
8932389242
assert( pIdx->pSchema==pTab->pSchema );
8932489243
sqlite3VdbeAddOp2(v, OP_Clear, pIdx->tnum, iDb);
8932589244
}
8932689245
}else
8932789246
#endif /* SQLITE_OMIT_TRUNCATE_OPTIMIZATION */
89328
- if( !HasRowid(pTab) ){
89329
- /* There is a WHERE clause on a WITHOUT ROWID table.
89330
- */
89331
- Index *pPk; /* The PRIMARY KEY index on the table */
89332
- int iPk; /* First of nPk memory cells holding PRIMARY KEY value */
89333
- int iEph; /* Ephemeral table holding all primary key values */
89334
- int iKey; /* Key value inserting into iEph */
89335
- i16 nPk; /* Number of components of the PRIMARY KEY */
89336
-
89337
- pPk = sqlite3PrimaryKeyIndex(pTab);
89338
- assert( pPk!=0 );
89339
- nPk = pPk->nKeyCol;
89340
- iPk = pParse->nMem+1;
89341
- pParse->nMem += nPk;
89342
- iKey = ++pParse->nMem;
89343
- iEph = pParse->nTab++;
89344
-
89345
- sqlite3VdbeAddOp2(v, OP_OpenEphemeral, iEph, nPk);
89346
- sqlite3VdbeSetP4KeyInfo(pParse, pPk);
89347
- pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0, 0, 0, 0);
89348
- if( pWInfo==0 ) goto delete_from_cleanup;
89349
- for(i=0; i<nPk; i++){
89350
- sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur, pPk->aiColumn[i],iPk+i);
89351
- }
89352
- sqlite3VdbeAddOp4(v, OP_MakeRecord, iPk, nPk, iKey,
89353
- sqlite3IndexAffinityStr(v, pPk), P4_TRANSIENT);
89354
- sqlite3VdbeAddOp2(v, OP_IdxInsert, iEph, iKey);
89355
- if( db->flags & SQLITE_CountRows ){
89356
- sqlite3VdbeAddOp2(v, OP_AddImm, memCnt, 1);
89357
- }
89358
- sqlite3WhereEnd(pWInfo);
89359
-
89360
- /* Open cursors for all indices of the table.
89361
- */
89362
- sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite,
89363
- iTabCur, &iDataCur, &iIdxCur);
89364
-
89365
- /* Loop over the primary keys to be deleted. */
89366
- addr = sqlite3VdbeAddOp1(v, OP_Rewind, iEph);
89367
- sqlite3VdbeAddOp2(v, OP_RowKey, iEph, iPk);
89368
-
89369
- /* Delete the row */
89370
- sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur,
89371
- iPk, 0, 1, OE_Default, 0);
89372
-
89373
- /* End of the delete loop */
89374
- sqlite3VdbeAddOp2(v, OP_Next, iEph, addr+1);
89375
- sqlite3VdbeJumpHere(v, addr);
89376
-
89377
- /* Close the cursors open on the table and its indexes. */
89378
- assert( iDataCur>=iIdxCur );
89379
- for(i=0, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){
89380
- sqlite3VdbeAddOp1(v, OP_Close, iIdxCur+i);
89381
- }
89382
- }else{
89383
- /* There is a WHERE clause on a rowid table. Run a loop that extracts
89384
- ** all rowids to be deleted into a RowSet.
89385
- */
89386
- int iRowSet = ++pParse->nMem; /* Register for rowset of rows to delete */
89387
- int iRowid = ++pParse->nMem; /* Used for storing rowid values. */
89388
- int regRowid; /* Actual register containing rowids */
89389
-
89390
- /* Collect rowids of every row to be deleted.
89391
- */
89392
- sqlite3VdbeAddOp2(v, OP_Null, 0, iRowSet);
89393
- pWInfo = sqlite3WhereBegin(
89394
- pParse, pTabList, pWhere, 0, 0, WHERE_DUPLICATES_OK, 0
89395
- );
89396
- if( pWInfo==0 ) goto delete_from_cleanup;
89397
- regRowid = sqlite3ExprCodeGetColumn(pParse, pTab, -1, iTabCur, iRowid, 0);
89398
- sqlite3VdbeAddOp2(v, OP_RowSetAdd, iRowSet, regRowid);
89399
- if( db->flags & SQLITE_CountRows ){
89400
- sqlite3VdbeAddOp2(v, OP_AddImm, memCnt, 1);
89401
- }
89402
- sqlite3WhereEnd(pWInfo);
89403
-
89404
- /* Delete every item whose key was written to the list during the
89405
- ** database scan. We have to delete items after the scan is complete
89406
- ** because deleting an item can change the scan order. */
89407
- end = sqlite3VdbeMakeLabel(v);
89408
-
89409
- /* Unless this is a view, open cursors for the table we are
89410
- ** deleting from and all its indices. If this is a view, then the
89411
- ** only effect this statement has is to fire the INSTEAD OF
89412
- ** triggers. */
89413
- if( !isView ){
89414
- sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, iTabCur,
89415
- &iDataCur, &iIdxCur);
89416
- assert( iDataCur==iTabCur );
89417
- assert( iIdxCur==iDataCur+1 );
89418
- }
89419
-
89420
- addr = sqlite3VdbeAddOp3(v, OP_RowSetRead, iRowSet, end, iRowid);
89421
-
89247
+ {
89248
+ if( HasRowid(pTab) ){
89249
+ /* For a rowid table, initialize the RowSet to an empty set */
89250
+ pPk = 0;
89251
+ nPk = 1;
89252
+ iRowSet = ++pParse->nMem;
89253
+ sqlite3VdbeAddOp2(v, OP_Null, 0, iRowSet);
89254
+ }else{
89255
+ /* For a WITHOUT ROWID table, create an ephermeral table used to
89256
+ ** hold all primary keys for rows to be deleted. */
89257
+ pPk = sqlite3PrimaryKeyIndex(pTab);
89258
+ assert( pPk!=0 );
89259
+ nPk = pPk->nKeyCol;
89260
+ iPk = pParse->nMem+1;
89261
+ pParse->nMem += nPk;
89262
+ iEphCur = pParse->nTab++;
89263
+ addrEphOpen = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, iEphCur, nPk);
89264
+ sqlite3VdbeSetP4KeyInfo(pParse, pPk);
89265
+ }
89266
+
89267
+ /* Construct a query to find the rowid or primary key for every row
89268
+ ** to be deleted, based on the WHERE clause.
89269
+ */
89270
+ pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0, 0,
89271
+ WHERE_ONEPASS_DESIRED|WHERE_DUPLICATES_OK,
89272
+ iTabCur+1);
89273
+ if( pWInfo==0 ) goto delete_from_cleanup;
89274
+ okOnePass = sqlite3WhereOkOnePass(pWInfo, aiCurOnePass);
89275
+
89276
+ /* Keep track of the number of rows to be deleted */
89277
+ if( db->flags & SQLITE_CountRows ){
89278
+ sqlite3VdbeAddOp2(v, OP_AddImm, memCnt, 1);
89279
+ }
89280
+
89281
+ /* Extract the rowid or primary key for the current row */
89282
+ if( pPk ){
89283
+ for(i=0; i<nPk; i++){
89284
+ sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur,
89285
+ pPk->aiColumn[i], iPk+i);
89286
+ }
89287
+ iKey = iPk;
89288
+ }else{
89289
+ iKey = pParse->nMem + 1;
89290
+ iKey = sqlite3ExprCodeGetColumn(pParse, pTab, -1, iTabCur, iKey, 0);
89291
+ if( iKey>pParse->nMem ) pParse->nMem = iKey;
89292
+ }
89293
+
89294
+ if( okOnePass ){
89295
+ /* For ONEPASS, no need to store the rowid/primary-key. There is only
89296
+ ** one, so just keep it in its register(s) and fall through to the
89297
+ ** delete code.
89298
+ */
89299
+ nKey = nPk; /* OP_Found will use an unpacked key */
89300
+ aToOpen = sqlite3DbMallocRaw(db, nIdx+2);
89301
+ if( aToOpen==0 ){
89302
+ sqlite3WhereEnd(pWInfo);
89303
+ goto delete_from_cleanup;
89304
+ }
89305
+ memset(aToOpen, 1, nIdx+1);
89306
+ aToOpen[nIdx+1] = 0;
89307
+ if( aiCurOnePass[0]>=0 ) aToOpen[aiCurOnePass[0]-iTabCur] = 0;
89308
+ if( aiCurOnePass[1]>=0 ) aToOpen[aiCurOnePass[1]-iTabCur] = 0;
89309
+ if( addrEphOpen ) sqlite3VdbeChangeToNoop(v, addrEphOpen);
89310
+ addrDelete = sqlite3VdbeAddOp0(v, OP_Goto); /* Jump to DELETE logic */
89311
+ }else if( pPk ){
89312
+ /* Construct a composite key for the row to be deleted and remember it */
89313
+ iKey = ++pParse->nMem;
89314
+ nKey = 0; /* Zero tells OP_Found to use a composite key */
89315
+ sqlite3VdbeAddOp4(v, OP_MakeRecord, iPk, nPk, iKey,
89316
+ sqlite3IndexAffinityStr(v, pPk), P4_TRANSIENT);
89317
+ sqlite3VdbeAddOp2(v, OP_IdxInsert, iEphCur, iKey);
89318
+ }else{
89319
+ /* Get the rowid of the row to be deleted and remember it in the RowSet */
89320
+ nKey = 1; /* OP_Seek always uses a single rowid */
89321
+ sqlite3VdbeAddOp2(v, OP_RowSetAdd, iRowSet, iKey);
89322
+ }
89323
+
89324
+ /* End of the WHERE loop */
89325
+ sqlite3WhereEnd(pWInfo);
89326
+ if( okOnePass ){
89327
+ /* Bypass the delete logic below if the WHERE loop found zero rows */
89328
+ addrBypass = sqlite3VdbeMakeLabel(v);
89329
+ sqlite3VdbeAddOp2(v, OP_Goto, 0, addrBypass);
89330
+ sqlite3VdbeJumpHere(v, addrDelete);
89331
+ }
89332
+
89333
+ /* Unless this is a view, open cursors for the table we are
89334
+ ** deleting from and all its indices. If this is a view, then the
89335
+ ** only effect this statement has is to fire the INSTEAD OF
89336
+ ** triggers.
89337
+ */
89338
+ if( !isView ){
89339
+ sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, iTabCur, aToOpen,
89340
+ &iDataCur, &iIdxCur);
89341
+ assert( pPk || iDataCur==iTabCur );
89342
+ assert( pPk || iIdxCur==iDataCur+1 );
89343
+ }
89344
+
89345
+ /* Set up a loop over the rowids/primary-keys that were found in the
89346
+ ** where-clause loop above.
89347
+ */
89348
+ if( okOnePass ){
89349
+ /* Just one row. Hence the top-of-loop is a no-op */
89350
+ assert( nKey==nPk ); /* OP_Found will use an unpacked key */
89351
+ if( aToOpen[iDataCur-iTabCur] ){
89352
+ assert( pPk!=0 );
89353
+ sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, addrBypass, iKey, nKey);
89354
+ }
89355
+ }else if( pPk ){
89356
+ addrLoop = sqlite3VdbeAddOp1(v, OP_Rewind, iEphCur);
89357
+ sqlite3VdbeAddOp2(v, OP_RowKey, iEphCur, iKey);
89358
+ assert( nKey==0 ); /* OP_Found will use a composite key */
89359
+ }else{
89360
+ addrLoop = sqlite3VdbeAddOp3(v, OP_RowSetRead, iRowSet, 0, iKey);
89361
+ assert( nKey==1 );
89362
+ }
89363
+
8942289364
/* Delete the row */
8942389365
#ifndef SQLITE_OMIT_VIRTUALTABLE
8942489366
if( IsVirtual(pTab) ){
8942589367
const char *pVTab = (const char *)sqlite3GetVTable(db, pTab);
8942689368
sqlite3VtabMakeWritable(pParse, pTab);
89427
- sqlite3VdbeAddOp4(v, OP_VUpdate, 0, 1, iRowid, pVTab, P4_VTAB);
89369
+ sqlite3VdbeAddOp4(v, OP_VUpdate, 0, 1, iKey, pVTab, P4_VTAB);
8942889370
sqlite3VdbeChangeP5(v, OE_Abort);
8942989371
sqlite3MayAbort(pParse);
8943089372
}else
8943189373
#endif
8943289374
{
8943389375
int count = (pParse->nested==0); /* True to count changes */
8943489376
sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur,
89435
- iRowid, 1, count, OE_Default, 0);
89377
+ iKey, nKey, count, OE_Default, okOnePass);
8943689378
}
89437
-
89438
- /* End of the delete loop */
89439
- sqlite3VdbeAddOp2(v, OP_Goto, 0, addr);
89440
- sqlite3VdbeResolveLabel(v, end);
89441
-
89379
+
89380
+ /* End of the loop over all rowids/primary-keys. */
89381
+ if( okOnePass ){
89382
+ sqlite3VdbeResolveLabel(v, addrBypass);
89383
+ }else if( pPk ){
89384
+ sqlite3VdbeAddOp2(v, OP_Next, iEphCur, addrLoop+1);
89385
+ sqlite3VdbeJumpHere(v, addrLoop);
89386
+ }else{
89387
+ sqlite3VdbeAddOp2(v, OP_Goto, 0, addrLoop);
89388
+ sqlite3VdbeJumpHere(v, addrLoop);
89389
+ }
89390
+
8944289391
/* Close the cursors open on the table and its indexes. */
8944389392
if( !isView && !IsVirtual(pTab) ){
89444
- sqlite3VdbeAddOp1(v, OP_Close, iDataCur);
89393
+ if( !pPk ) sqlite3VdbeAddOp1(v, OP_Close, iDataCur);
8944589394
for(i=0, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){
8944689395
sqlite3VdbeAddOp1(v, OP_Close, iIdxCur + i);
8944789396
}
8944889397
}
89449
- }
89398
+ } /* End non-truncate path */
8945089399
8945189400
/* Update the sqlite_sequence table by storing the content of the
8945289401
** maximum rowid counter values recorded while inserting into
8945389402
** autoincrement tables.
8945489403
*/
@@ -89468,10 +89417,11 @@
8946889417
8946989418
delete_from_cleanup:
8947089419
sqlite3AuthContextPop(&sContext);
8947189420
sqlite3SrcListDelete(db, pTabList);
8947289421
sqlite3ExprDelete(db, pWhere);
89422
+ sqlite3DbFree(db, aToOpen);
8947389423
return;
8947489424
}
8947589425
/* Make sure "isView" and other macros defined above are undefined. Otherwise
8947689426
** thely may interfere with compilation of other functions in this file
8947789427
** (or in another file, if this file becomes part of the amalgamation). */
@@ -89534,10 +89484,11 @@
8953489484
/* If there are any triggers to fire, allocate a range of registers to
8953589485
** use for the old.* references in the triggers. */
8953689486
if( sqlite3FkRequired(pParse, pTab, 0, 0) || pTrigger ){
8953789487
u32 mask; /* Mask of OLD.* columns in use */
8953889488
int iCol; /* Iterator used while populating OLD.* */
89489
+ int addrStart; /* Start of BEFORE trigger programs */
8953989490
8954089491
/* TODO: Could use temporary registers here. Also could attempt to
8954189492
** avoid copying the contents of the rowid register. */
8954289493
mask = sqlite3TriggerColmask(
8954389494
pParse, pTrigger, 0, 0, TRIGGER_BEFORE|TRIGGER_AFTER, pTab, onconf
@@ -89554,19 +89505,23 @@
8955489505
sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, iCol, iOld+iCol+1);
8955589506
}
8955689507
}
8955789508
8955889509
/* Invoke BEFORE DELETE trigger programs. */
89510
+ addrStart = sqlite3VdbeCurrentAddr(v);
8955989511
sqlite3CodeRowTrigger(pParse, pTrigger,
8956089512
TK_DELETE, 0, TRIGGER_BEFORE, pTab, iOld, onconf, iLabel
8956189513
);
8956289514
89563
- /* Seek the cursor to the row to be deleted again. It may be that
89564
- ** the BEFORE triggers coded above have already removed the row
89565
- ** being deleted. Do not attempt to delete the row a second time, and
89566
- ** do not fire AFTER triggers. */
89567
- sqlite3VdbeAddOp4Int(v, opSeek, iDataCur, iLabel, iPk, nPk);
89515
+ /* If any BEFORE triggers were coded, then seek the cursor to the
89516
+ ** row to be deleted again. It may be that the BEFORE triggers moved
89517
+ ** the cursor or of already deleted the row that the cursor was
89518
+ ** pointing to.
89519
+ */
89520
+ if( addrStart<sqlite3VdbeCurrentAddr(v) ){
89521
+ sqlite3VdbeAddOp4Int(v, opSeek, iDataCur, iLabel, iPk, nPk);
89522
+ }
8956889523
8956989524
/* Do FK processing. This call checks that any FK constraints that
8957089525
** refer to this table (i.e. constraints attached to other tables)
8957189526
** are not violated by deleting this row. */
8957289527
sqlite3FkCheck(pParse, pTab, iOld, 0, 0, 0);
@@ -91992,10 +91947,11 @@
9199291947
Vdbe *v = sqlite3GetVdbe(pParse);
9199391948
9199491949
assert( pIdx==0 || pIdx->pTable==pTab );
9199591950
assert( pIdx==0 || pIdx->nKeyCol==pFKey->nCol );
9199691951
assert( pIdx!=0 || pFKey->nCol==1 );
91952
+ assert( pIdx!=0 || HasRowid(pTab) );
9199791953
9199891954
if( nIncr<0 ){
9199991955
iFkIfZero = sqlite3VdbeAddOp2(v, OP_FkIfZero, pFKey->isDeferred, 0);
9200091956
}
9200191957
@@ -92044,10 +92000,11 @@
9204492000
pRight = exprTableColumn(db, pTab, pSrc->a[0].iCursor, -1);
9204592001
pNe = sqlite3PExpr(pParse, TK_NE, pLeft, pRight, 0);
9204692002
}else{
9204792003
Expr *pEq, *pAll = 0;
9204892004
Index *pPk = sqlite3PrimaryKeyIndex(pTab);
92005
+ assert( pIdx!=0 );
9204992006
for(i=0; i<pPk->nKeyCol; i++){
9205092007
i16 iCol = pIdx->aiColumn[i];
9205192008
pLeft = exprTableRegister(pParse, pTab, regData, iCol);
9205292009
pRight = exprTableColumn(db, pTab, pSrc->a[0].iCursor, iCol);
9205392010
pEq = sqlite3PExpr(pParse, TK_EQ, pLeft, pRight, 0);
@@ -93622,11 +93579,11 @@
9362293579
}
9362393580
9362493581
/* If this is not a view, open the table and and all indices */
9362593582
if( !isView ){
9362693583
int nIdx;
93627
- nIdx = sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, -1,
93584
+ nIdx = sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, -1, 0,
9362893585
&iDataCur, &iIdxCur);
9362993586
aRegIdx = sqlite3DbMallocRaw(db, sizeof(int)*(nIdx+1));
9363093587
if( aRegIdx==0 ){
9363193588
goto insert_cleanup;
9363293589
}
@@ -94482,42 +94439,50 @@
9448294439
SQLITE_PRIVATE int sqlite3OpenTableAndIndices(
9448394440
Parse *pParse, /* Parsing context */
9448494441
Table *pTab, /* Table to be opened */
9448594442
int op, /* OP_OpenRead or OP_OpenWrite */
9448694443
int iBase, /* Use this for the table cursor, if there is one */
94444
+ u8 *aToOpen, /* If not NULL: boolean for each table and index */
9448794445
int *piDataCur, /* Write the database source cursor number here */
9448894446
int *piIdxCur /* Write the first index cursor number here */
9448994447
){
9449094448
int i;
9449194449
int iDb;
94450
+ int iDataCur;
9449294451
Index *pIdx;
9449394452
Vdbe *v;
9449494453
9449594454
assert( op==OP_OpenRead || op==OP_OpenWrite );
9449694455
if( IsVirtual(pTab) ){
94456
+ assert( aToOpen==0 );
9449794457
*piDataCur = 0;
9449894458
*piIdxCur = 1;
9449994459
return 0;
9450094460
}
9450194461
iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
9450294462
v = sqlite3GetVdbe(pParse);
9450394463
assert( v!=0 );
9450494464
if( iBase<0 ) iBase = pParse->nTab;
94505
- if( HasRowid(pTab) ){
94506
- *piDataCur = iBase++;
94507
- sqlite3OpenTable(pParse, *piDataCur, iDb, pTab, op);
94465
+ iDataCur = iBase++;
94466
+ if( piDataCur ) *piDataCur = iDataCur;
94467
+ if( HasRowid(pTab) && (aToOpen==0 || aToOpen[0]) ){
94468
+ sqlite3OpenTable(pParse, iDataCur, iDb, pTab, op);
9450894469
}else{
9450994470
sqlite3TableLock(pParse, iDb, pTab->tnum, op==OP_OpenWrite, pTab->zName);
9451094471
}
94511
- *piIdxCur = iBase;
94472
+ if( piIdxCur ) *piIdxCur = iBase;
9451294473
for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
9451394474
int iIdxCur = iBase++;
9451494475
assert( pIdx->pSchema==pTab->pSchema );
94515
- if( pIdx->autoIndex==2 && !HasRowid(pTab) ) *piDataCur = iIdxCur;
94516
- sqlite3VdbeAddOp3(v, op, iIdxCur, pIdx->tnum, iDb);
94517
- sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
94518
- VdbeComment((v, "%s", pIdx->zName));
94476
+ if( pIdx->autoIndex==2 && !HasRowid(pTab) && piDataCur ){
94477
+ *piDataCur = iIdxCur;
94478
+ }
94479
+ if( aToOpen==0 || aToOpen[i+1] ){
94480
+ sqlite3VdbeAddOp3(v, op, iIdxCur, pIdx->tnum, iDb);
94481
+ sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
94482
+ VdbeComment((v, "%s", pIdx->zName));
94483
+ }
9451994484
}
9452094485
if( iBase>pParse->nTab ) pParse->nTab = iBase;
9452194486
return i;
9452294487
}
9452394488
@@ -98144,11 +98109,11 @@
9814498109
addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1); /* Stop if out of errors */
9814598110
sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
9814698111
sqlite3VdbeJumpHere(v, addr);
9814798112
sqlite3ExprCacheClear(pParse);
9814898113
sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenRead,
98149
- 1, &iDataCur, &iIdxCur);
98114
+ 1, 0, &iDataCur, &iIdxCur);
9815098115
sqlite3VdbeAddOp2(v, OP_Integer, 0, 7);
9815198116
for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
9815298117
sqlite3VdbeAddOp2(v, OP_Integer, 0, 8+j); /* index entries counter */
9815398118
}
9815498119
pParse->nMem = MAX(pParse->nMem, 8+j);
@@ -99079,10 +99044,17 @@
9907999044
}
9908099045
assert( i>=0 && i<db->nDb );
9908199046
}
9908299047
return i;
9908399048
}
99049
+
99050
+/*
99051
+** Free all memory allocations in the pParse object
99052
+*/
99053
+SQLITE_PRIVATE void sqlite3ParserReset(Parse *pParse){
99054
+ if( pParse ) sqlite3ExprListDelete(pParse->db, pParse->pConstExpr);
99055
+}
9908499056
9908599057
/*
9908699058
** Compile the UTF-8 encoded SQL statement zSql into a statement handle.
9908799059
*/
9908899060
static int sqlite3Prepare(
@@ -99237,10 +99209,11 @@
9923799209
sqlite3DbFree(db, pT);
9923899210
}
9923999211
9924099212
end_prepare:
9924199213
99214
+ sqlite3ParserReset(pParse);
9924299215
sqlite3StackFree(db, pParse);
9924399216
rc = sqlite3ApiExit(db, rc);
9924499217
assert( (rc&db->errMask)==rc );
9924599218
return rc;
9924699219
}
@@ -101803,20 +101776,20 @@
101803101776
*/
101804101777
if( op!=TK_ALL ){
101805101778
for(i=1; db->mallocFailed==0 && i<=p->pEList->nExpr; i++){
101806101779
struct ExprList_item *pItem;
101807101780
for(j=0, pItem=pOrderBy->a; j<nOrderBy; j++, pItem++){
101808
- assert( pItem->iOrderByCol>0 );
101809
- if( pItem->iOrderByCol==i ) break;
101781
+ assert( pItem->u.x.iOrderByCol>0 );
101782
+ if( pItem->u.x.iOrderByCol==i ) break;
101810101783
}
101811101784
if( j==nOrderBy ){
101812101785
Expr *pNew = sqlite3Expr(db, TK_INTEGER, 0);
101813101786
if( pNew==0 ) return SQLITE_NOMEM;
101814101787
pNew->flags |= EP_IntValue;
101815101788
pNew->u.iValue = i;
101816101789
pOrderBy = sqlite3ExprListAppend(pParse, pOrderBy, pNew);
101817
- if( pOrderBy ) pOrderBy->a[nOrderBy++].iOrderByCol = (u16)i;
101790
+ if( pOrderBy ) pOrderBy->a[nOrderBy++].u.x.iOrderByCol = (u16)i;
101818101791
}
101819101792
}
101820101793
}
101821101794
101822101795
/* Compute the comparison permutation and keyinfo that is used with
@@ -101828,12 +101801,13 @@
101828101801
*/
101829101802
aPermute = sqlite3DbMallocRaw(db, sizeof(int)*nOrderBy);
101830101803
if( aPermute ){
101831101804
struct ExprList_item *pItem;
101832101805
for(i=0, pItem=pOrderBy->a; i<nOrderBy; i++, pItem++){
101833
- assert( pItem->iOrderByCol>0 && pItem->iOrderByCol<=p->pEList->nExpr );
101834
- aPermute[i] = pItem->iOrderByCol - 1;
101806
+ assert( pItem->u.x.iOrderByCol>0
101807
+ && pItem->u.x.iOrderByCol<=p->pEList->nExpr );
101808
+ aPermute[i] = pItem->u.x.iOrderByCol - 1;
101835101809
}
101836101810
pKeyMerge = sqlite3KeyInfoAlloc(db, nOrderBy, 1);
101837101811
if( pKeyMerge ){
101838101812
for(i=0; i<nOrderBy; i++){
101839101813
CollSeq *pColl;
@@ -102409,11 +102383,11 @@
102409102383
102410102384
/* Restriction 18. */
102411102385
if( p->pOrderBy ){
102412102386
int ii;
102413102387
for(ii=0; ii<p->pOrderBy->nExpr; ii++){
102414
- if( p->pOrderBy->a[ii].iOrderByCol==0 ) return 0;
102388
+ if( p->pOrderBy->a[ii].u.x.iOrderByCol==0 ) return 0;
102415102389
}
102416102390
}
102417102391
}
102418102392
102419102393
/***** If we reach this point, flattening is permitted. *****/
@@ -103816,14 +103790,14 @@
103816103790
if( pGroupBy ){
103817103791
int k; /* Loop counter */
103818103792
struct ExprList_item *pItem; /* For looping over expression in a list */
103819103793
103820103794
for(k=p->pEList->nExpr, pItem=p->pEList->a; k>0; k--, pItem++){
103821
- pItem->iAlias = 0;
103795
+ pItem->u.x.iAlias = 0;
103822103796
}
103823103797
for(k=pGroupBy->nExpr, pItem=pGroupBy->a; k>0; k--, pItem++){
103824
- pItem->iAlias = 0;
103798
+ pItem->u.x.iAlias = 0;
103825103799
}
103826103800
if( p->nSelectRow>100 ) p->nSelectRow = 100;
103827103801
}else{
103828103802
p->nSelectRow = 1;
103829103803
}
@@ -105470,10 +105444,11 @@
105470105444
sqlite3VdbeDelete(v);
105471105445
}
105472105446
105473105447
assert( !pSubParse->pAinc && !pSubParse->pZombieTab );
105474105448
assert( !pSubParse->pTriggerPrg && !pSubParse->nMaxArg );
105449
+ sqlite3ParserReset(pSubParse);
105475105450
sqlite3StackFree(db, pSubParse);
105476105451
105477105452
return pPrg;
105478105453
}
105479105454
@@ -105784,22 +105759,23 @@
105784105759
WhereInfo *pWInfo; /* Information about the WHERE clause */
105785105760
Vdbe *v; /* The virtual database engine */
105786105761
Index *pIdx; /* For looping over indices */
105787105762
Index *pPk; /* The PRIMARY KEY index for WITHOUT ROWID tables */
105788105763
int nIdx; /* Number of indices that need updating */
105764
+ int iBaseCur; /* Base cursor number */
105789105765
int iDataCur; /* Cursor for the canonical data btree */
105790105766
int iIdxCur; /* Cursor for the first index */
105791105767
sqlite3 *db; /* The database structure */
105792105768
int *aRegIdx = 0; /* One register assigned to each index to be updated */
105793105769
int *aXRef = 0; /* aXRef[i] is the index in pChanges->a[] of the
105794105770
** an expression for the i-th column of the table.
105795105771
** aXRef[i]==-1 if the i-th column is not changed. */
105772
+ u8 *aToOpen; /* 1 for tables and indices to be opened */
105796105773
u8 chngPk; /* PRIMARY KEY changed in a WITHOUT ROWID table */
105797105774
u8 chngRowid; /* Rowid changed in a normal table */
105798105775
u8 chngKey; /* Either chngPk or chngRowid */
105799105776
Expr *pRowidExpr = 0; /* Expression defining the new record number */
105800
- int openAll = 0; /* True if all indices need to be opened */
105801105777
AuthContext sContext; /* The authorization context */
105802105778
NameContext sNC; /* The name-context to resolve expressions in */
105803105779
int iDb; /* Database containing the table being updated */
105804105780
int okOnePass; /* True for one-pass algorithm without the FIFO */
105805105781
int hasFK; /* True if foreign key processing is required */
@@ -105859,29 +105835,37 @@
105859105835
goto update_cleanup;
105860105836
}
105861105837
if( sqlite3IsReadOnly(pParse, pTab, tmask) ){
105862105838
goto update_cleanup;
105863105839
}
105864
- aXRef = sqlite3DbMallocRaw(db, sizeof(int) * pTab->nCol );
105865
- if( aXRef==0 ) goto update_cleanup;
105866
- for(i=0; i<pTab->nCol; i++) aXRef[i] = -1;
105867105840
105868105841
/* Allocate a cursors for the main database table and for all indices.
105869105842
** The index cursors might not be used, but if they are used they
105870105843
** need to occur right after the database cursor. So go ahead and
105871105844
** allocate enough space, just in case.
105872105845
*/
105873
- pTabList->a[0].iCursor = iDataCur = pParse->nTab++;
105846
+ pTabList->a[0].iCursor = iBaseCur = iDataCur = pParse->nTab++;
105874105847
iIdxCur = iDataCur+1;
105875105848
pPk = HasRowid(pTab) ? 0 : sqlite3PrimaryKeyIndex(pTab);
105876105849
for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){
105877105850
if( pIdx->autoIndex==2 && pPk!=0 ){
105878105851
iDataCur = pParse->nTab;
105879105852
pTabList->a[0].iCursor = iDataCur;
105880105853
}
105881105854
pParse->nTab++;
105882105855
}
105856
+
105857
+ /* Allocate space for aXRef[], aRegIdx[], and aToOpen[].
105858
+ ** Initialize aXRef[] and aToOpen[] to their default values.
105859
+ */
105860
+ aXRef = sqlite3DbMallocRaw(db, sizeof(int) * (pTab->nCol+nIdx) + nIdx+2 );
105861
+ if( aXRef==0 ) goto update_cleanup;
105862
+ aRegIdx = aXRef+pTab->nCol;
105863
+ aToOpen = (u8*)(aRegIdx+nIdx);
105864
+ memset(aToOpen, 1, nIdx+1);
105865
+ aToOpen[nIdx+1] = 0;
105866
+ for(i=0; i<pTab->nCol; i++) aXRef[i] = -1;
105883105867
105884105868
/* Initialize the name-context */
105885105869
memset(&sNC, 0, sizeof(sNC));
105886105870
sNC.pParse = pParse;
105887105871
sNC.pSrcList = pTabList;
@@ -105936,22 +105920,22 @@
105936105920
}
105937105921
assert( (chngRowid & chngPk)==0 );
105938105922
assert( chngRowid==0 || chngRowid==1 );
105939105923
assert( chngPk==0 || chngPk==1 );
105940105924
chngKey = chngRowid + chngPk;
105925
+
105926
+ /* The SET expressions are not actually used inside the WHERE loop.
105927
+ ** So reset the colUsed mask
105928
+ */
105929
+ pTabList->a[0].colUsed = 0;
105941105930
105942105931
hasFK = sqlite3FkRequired(pParse, pTab, aXRef, chngKey);
105943105932
105944
- /* Allocate memory for the array aRegIdx[]. There is one entry in the
105945
- ** array for each index associated with table being updated. Fill in
105946
- ** the value with a register number for indices that are to be used
105947
- ** and with zero for unused indices.
105933
+ /* There is one entry in the aRegIdx[] array for each index on the table
105934
+ ** being updated. Fill in aRegIdx[] with a register number that will hold
105935
+ ** the key for accessing each index.
105948105936
*/
105949
- if( nIdx>0 ){
105950
- aRegIdx = sqlite3DbMallocRaw(db, sizeof(Index*) * nIdx );
105951
- if( aRegIdx==0 ) goto update_cleanup;
105952
- }
105953105937
for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
105954105938
int reg;
105955105939
if( chngKey || hasFK || pIdx->pPartIdxWhere || pIdx==pPk ){
105956105940
reg = ++pParse->nMem;
105957105941
}else{
@@ -105961,10 +105945,11 @@
105961105945
reg = ++pParse->nMem;
105962105946
break;
105963105947
}
105964105948
}
105965105949
}
105950
+ if( reg==0 ) aToOpen[j+1] = 0;
105966105951
aRegIdx[j] = reg;
105967105952
}
105968105953
105969105954
/* Begin generating code. */
105970105955
v = sqlite3GetVdbe(pParse);
@@ -106084,46 +106069,34 @@
106084106069
** Open every index that needs updating. Note that if any
106085106070
** index could potentially invoke a REPLACE conflict resolution
106086106071
** action, then we need to open all indices because we might need
106087106072
** to be deleting some records.
106088106073
*/
106089
- if( !okOnePass && HasRowid(pTab) ){
106090
- sqlite3OpenTable(pParse, iDataCur, iDb, pTab, OP_OpenWrite);
106091
- }
106092
- sqlite3TableLock(pParse, iDb, pTab->tnum, 1, pTab->zName);
106093106074
if( onError==OE_Replace ){
106094
- openAll = 1;
106075
+ memset(aToOpen, 1, nIdx+1);
106095106076
}else{
106096
- openAll = 0;
106097106077
for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
106098106078
if( pIdx->onError==OE_Replace ){
106099
- openAll = 1;
106079
+ memset(aToOpen, 1, nIdx+1);
106100106080
break;
106101106081
}
106102106082
}
106103106083
}
106104
- for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
106105
- int iThisCur = iIdxCur+i;
106106
- assert( aRegIdx );
106107
- if( (openAll || aRegIdx[i]>0)
106108
- && iThisCur!=aiCurOnePass[1]
106109
- ){
106110
- assert( iThisCur!=aiCurOnePass[0] );
106111
- sqlite3VdbeAddOp3(v, OP_OpenWrite, iThisCur, pIdx->tnum, iDb);
106112
- sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
106113
- assert( pParse->nTab>iThisCur );
106114
- VdbeComment((v, "%s", pIdx->zName));
106115
- if( okOnePass && pPk && iThisCur==iDataCur ){
106116
- sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, labelBreak,
106117
- regKey, nKey);
106118
- }
106119
- }
106120
- }
106084
+ if( okOnePass ){
106085
+ if( aiCurOnePass[0]>=0 ) aToOpen[aiCurOnePass[0]-iBaseCur] = 0;
106086
+ if( aiCurOnePass[1]>=0 ) aToOpen[aiCurOnePass[1]-iBaseCur] = 0;
106087
+ }
106088
+ sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, iBaseCur, aToOpen,
106089
+ 0, 0);
106121106090
}
106122106091
106123106092
/* Top of the update loop */
106124106093
if( okOnePass ){
106094
+ if( aToOpen[iDataCur-iBaseCur] ){
106095
+ assert( pPk!=0 );
106096
+ sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, labelBreak, regKey, nKey);
106097
+ }
106125106098
labelContinue = labelBreak;
106126106099
sqlite3VdbeAddOp2(v, OP_IsNull, pPk ? regKey : regOldRowid, labelBreak);
106127106100
}else if( pPk ){
106128106101
labelContinue = sqlite3VdbeMakeLabel(v);
106129106102
sqlite3VdbeAddOp2(v, OP_Rewind, iEph, labelBreak);
@@ -106312,11 +106285,11 @@
106312106285
sqlite3VdbeResolveLabel(v, labelBreak);
106313106286
106314106287
/* Close all tables */
106315106288
for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
106316106289
assert( aRegIdx );
106317
- if( openAll || aRegIdx[i]>0 ){
106290
+ if( aToOpen[i+1] ){
106318106291
sqlite3VdbeAddOp2(v, OP_Close, iIdxCur+i, 0);
106319106292
}
106320106293
}
106321106294
if( iDataCur<iIdxCur ) sqlite3VdbeAddOp2(v, OP_Close, iDataCur, 0);
106322106295
@@ -106339,12 +106312,11 @@
106339106312
sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows updated", SQLITE_STATIC);
106340106313
}
106341106314
106342106315
update_cleanup:
106343106316
sqlite3AuthContextPop(&sContext);
106344
- sqlite3DbFree(db, aRegIdx);
106345
- sqlite3DbFree(db, aXRef);
106317
+ sqlite3DbFree(db, aXRef); /* Also frees aRegIdx[] and aToOpen[] */
106346106318
sqlite3SrcListDelete(db, pTabList);
106347106319
sqlite3ExprListDelete(db, pChanges);
106348106320
sqlite3ExprDelete(db, pWhere);
106349106321
return;
106350106322
}
@@ -107563,10 +107535,11 @@
107563107535
107564107536
if( pParse->pVdbe ){
107565107537
sqlite3VdbeFinalize(pParse->pVdbe);
107566107538
}
107567107539
sqlite3DeleteTable(db, pParse->pNewTable);
107540
+ sqlite3ParserReset(pParse);
107568107541
sqlite3StackFree(db, pParse);
107569107542
}
107570107543
107571107544
assert( (rc&0xff)==rc );
107572107545
rc = sqlite3ApiExit(db, rc);
@@ -109055,13 +109028,10 @@
109055109028
}
109056109029
assert( pLeft->iColumn!=(-1) ); /* Because IPK never has AFF_TEXT */
109057109030
109058109031
pRight = pList->a[0].pExpr;
109059109032
op = pRight->op;
109060
- if( op==TK_REGISTER ){
109061
- op = pRight->op2;
109062
- }
109063109033
if( op==TK_VARIABLE ){
109064109034
Vdbe *pReprepare = pParse->pReprepare;
109065109035
int iCol = pRight->iColumn;
109066109036
pVal = sqlite3VdbeGetBoundValue(pReprepare, iCol, SQLITE_AFF_NONE);
109067109037
if( pVal && sqlite3_value_type(pVal)==SQLITE_TEXT ){
@@ -111145,11 +111115,11 @@
111145111115
iCur = pTabItem->iCursor;
111146111116
pLevel->notReady = notReady & ~getMask(&pWInfo->sMaskSet, iCur);
111147111117
bRev = (pWInfo->revMask>>iLevel)&1;
111148111118
omitTable = (pLoop->wsFlags & WHERE_IDX_ONLY)!=0
111149111119
&& (pWInfo->wctrlFlags & WHERE_FORCE_TABLE)==0;
111150
- VdbeNoopComment((v, "Begin WHERE-loop%d: %s",iLevel,pTabItem->pTab->zName));
111120
+ VdbeModuleComment((v, "Begin WHERE-loop%d: %s",iLevel,pTabItem->pTab->zName));
111151111121
111152111122
/* Create labels for the "break" and "continue" instructions
111153111123
** for the current loop. Jump to addrBrk to break out of a loop.
111154111124
** Jump to cont to go immediately to the next iteration of the
111155111125
** loop.
@@ -111387,11 +111357,11 @@
111387111357
Index *pIdx; /* The index we will be using */
111388111358
int iIdxCur; /* The VDBE cursor for the index */
111389111359
int nExtraReg = 0; /* Number of extra registers needed */
111390111360
int op; /* Instruction opcode */
111391111361
char *zStartAff; /* Affinity for start of range constraint */
111392
- char *zEndAff; /* Affinity for end of range constraint */
111362
+ char cEndAff = 0; /* Affinity for end of range constraint */
111393111363
111394111364
pIdx = pLoop->u.btree.pIndex;
111395111365
iIdxCur = pLevel->iIdxCur;
111396111366
assert( nEq>=pLoop->u.btree.nSkip );
111397111367
@@ -111428,11 +111398,12 @@
111428111398
/* Generate code to evaluate all constraint terms using == or IN
111429111399
** and store the values of those terms in an array of registers
111430111400
** starting at regBase.
111431111401
*/
111432111402
regBase = codeAllEqualityTerms(pParse,pLevel,bRev,nExtraReg,&zStartAff);
111433
- zEndAff = sqlite3DbStrDup(db, zStartAff);
111403
+ assert( zStartAff==0 || sqlite3Strlen30(zStartAff)>=nEq );
111404
+ if( zStartAff ) cEndAff = zStartAff[nEq];
111434111405
addrNxt = pLevel->addrNxt;
111435111406
111436111407
/* If we are doing a reverse order scan on an ascending index, or
111437111408
** a forward order scan on a descending index, interchange the
111438111409
** start and end terms (pRangeStart and pRangeEnd).
@@ -111498,27 +111469,19 @@
111498111469
sqlite3ExprCacheRemove(pParse, regBase+nEq, 1);
111499111470
sqlite3ExprCode(pParse, pRight, regBase+nEq);
111500111471
if( (pRangeEnd->wtFlags & TERM_VNULL)==0 ){
111501111472
sqlite3ExprCodeIsNullJump(v, pRight, regBase+nEq, addrNxt);
111502111473
}
111503
- if( zEndAff ){
111504
- if( sqlite3CompareAffinity(pRight, zEndAff[nEq])==SQLITE_AFF_NONE){
111505
- /* Since the comparison is to be performed with no conversions
111506
- ** applied to the operands, set the affinity to apply to pRight to
111507
- ** SQLITE_AFF_NONE. */
111508
- zEndAff[nEq] = SQLITE_AFF_NONE;
111509
- }
111510
- if( sqlite3ExprNeedsNoAffinityChange(pRight, zEndAff[nEq]) ){
111511
- zEndAff[nEq] = SQLITE_AFF_NONE;
111512
- }
111513
- }
111514
- codeApplyAffinity(pParse, regBase, nEq+1, zEndAff);
111474
+ if( sqlite3CompareAffinity(pRight, cEndAff)!=SQLITE_AFF_NONE
111475
+ && !sqlite3ExprNeedsNoAffinityChange(pRight, cEndAff)
111476
+ ){
111477
+ codeApplyAffinity(pParse, regBase+nEq, 1, &cEndAff);
111478
+ }
111515111479
nConstraint++;
111516111480
testcase( pRangeEnd->wtFlags & TERM_VIRTUAL );
111517111481
}
111518111482
sqlite3DbFree(db, zStartAff);
111519
- sqlite3DbFree(db, zEndAff);
111520111483
111521111484
/* Top of the loop body */
111522111485
pLevel->p2 = sqlite3VdbeCurrentAddr(v);
111523111486
111524111487
/* Check if the index cursor is past the end of the range. */
@@ -111539,10 +111502,11 @@
111539111502
testcase( pLoop->wsFlags & WHERE_BTM_LIMIT );
111540111503
testcase( pLoop->wsFlags & WHERE_TOP_LIMIT );
111541111504
if( (pLoop->wsFlags & (WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0
111542111505
&& (j = pIdx->aiColumn[nEq])>=0
111543111506
&& pIdx->pTable->aCol[j].notNull==0
111507
+ && (nEq || (pLoop->wsFlags & WHERE_BTM_LIMIT)==0)
111544111508
){
111545111509
sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, nEq, r1);
111546111510
VdbeComment((v, "%s", pIdx->pTable->aCol[j].zName));
111547111511
sqlite3VdbeAddOp2(v, OP_IsNull, r1, addrCont);
111548111512
}
@@ -111856,11 +111820,11 @@
111856111820
pAlt = findTerm(pWC, iCur, pTerm->u.leftColumn, notReady, WO_EQ|WO_IN, 0);
111857111821
if( pAlt==0 ) continue;
111858111822
if( pAlt->wtFlags & (TERM_CODED) ) continue;
111859111823
testcase( pAlt->eOperator & WO_EQ );
111860111824
testcase( pAlt->eOperator & WO_IN );
111861
- VdbeNoopComment((v, "begin transitive constraint"));
111825
+ VdbeModuleComment((v, "begin transitive constraint"));
111862111826
pEAlt = sqlite3StackAllocRaw(db, sizeof(*pEAlt));
111863111827
if( pEAlt ){
111864111828
*pEAlt = *pAlt->pExpr;
111865111829
pEAlt->pLeft = pE->pLeft;
111866111830
sqlite3ExprIfFalse(pParse, pEAlt, addrCont, SQLITE_JUMPIFNULL);
@@ -112313,14 +112277,19 @@
112313112277
saved_wsFlags = pNew->wsFlags;
112314112278
saved_prereq = pNew->prereq;
112315112279
saved_nOut = pNew->nOut;
112316112280
pNew->rSetup = 0;
112317112281
rLogSize = estLog(sqlite3LogEst(pProbe->aiRowEst[0]));
112282
+
112283
+ /* Consider using a skip-scan if there are no WHERE clause constraints
112284
+ ** available for the left-most terms of the index, and if the average
112285
+ ** number of repeats in the left-most terms is at least 50.
112286
+ */
112318112287
if( pTerm==0
112319112288
&& saved_nEq==saved_nSkip
112320112289
&& saved_nEq+1<pProbe->nKeyCol
112321
- && pProbe->aiRowEst[saved_nEq+1]>50
112290
+ && pProbe->aiRowEst[saved_nEq+1]>50 /* TUNING: Minimum for skip-scan */
112322112291
){
112323112292
LogEst nIter;
112324112293
pNew->u.btree.nEq++;
112325112294
pNew->u.btree.nSkip++;
112326112295
pNew->aLTerm[pNew->nLTerm++] = 0;
@@ -113812,11 +113781,10 @@
113812113781
/* Split the WHERE clause into separate subexpressions where each
113813113782
** subexpression is separated by an AND operator.
113814113783
*/
113815113784
initMaskSet(pMaskSet);
113816113785
whereClauseInit(&pWInfo->sWC, pWInfo);
113817
- sqlite3ExprCodeConstants(pParse, pWhere);
113818113786
whereSplit(&pWInfo->sWC, pWhere, TK_AND);
113819113787
sqlite3CodeVerifySchema(pParse, -1); /* Insert the cookie verifier Goto */
113820113788
113821113789
/* Special case: a WHERE clause that is constant. Evaluate the
113822113790
** expression and either jump over all of the code or fall thru.
@@ -114127,11 +114095,11 @@
114127114095
notReady = codeOneLoopStart(pWInfo, ii, notReady);
114128114096
pWInfo->iContinue = pLevel->addrCont;
114129114097
}
114130114098
114131114099
/* Done. */
114132
- VdbeNoopComment((v, "Begin WHERE-core"));
114100
+ VdbeModuleComment((v, "Begin WHERE-core"));
114133114101
return pWInfo;
114134114102
114135114103
/* Jump here if malloc fails */
114136114104
whereBeginError:
114137114105
if( pWInfo ){
@@ -114154,11 +114122,11 @@
114154114122
SrcList *pTabList = pWInfo->pTabList;
114155114123
sqlite3 *db = pParse->db;
114156114124
114157114125
/* Generate loop termination code.
114158114126
*/
114159
- VdbeNoopComment((v, "End WHERE-core"));
114127
+ VdbeModuleComment((v, "End WHERE-core"));
114160114128
sqlite3ExprCacheClear(pParse);
114161114129
for(i=pWInfo->nLevel-1; i>=0; i--){
114162114130
int addr;
114163114131
pLevel = &pWInfo->a[i];
114164114132
pLoop = pLevel->pWLoop;
@@ -114200,11 +114168,11 @@
114200114168
}else{
114201114169
sqlite3VdbeAddOp2(v, OP_Goto, 0, pLevel->addrFirst);
114202114170
}
114203114171
sqlite3VdbeJumpHere(v, addr);
114204114172
}
114205
- VdbeNoopComment((v, "End WHERE-loop%d: %s", i,
114173
+ VdbeModuleComment((v, "End WHERE-loop%d: %s", i,
114206114174
pWInfo->pTabList->a[pLevel->iFrom].pTab->zName));
114207114175
}
114208114176
114209114177
/* The "break" point is here, just past the end of the outer loop.
114210114178
** Set it.
@@ -123900,10 +123868,13 @@
123900123868
}
123901123869
123902123870
#define GETVARINT_STEP(v, ptr, shift, mask1, mask2, var, ret) \
123903123871
v = (v & mask1) | ( (*ptr++) << shift ); \
123904123872
if( (v & mask2)==0 ){ var = v; return ret; }
123873
+#define GETVARINT_INIT(v, ptr, shift, mask1, mask2, var, ret) \
123874
+ v = (*ptr++); \
123875
+ if( (v & mask2)==0 ){ var = v; return ret; }
123905123876
123906123877
/*
123907123878
** Read a 64-bit variable-length integer from memory starting at p[0].
123908123879
** Return the number of bytes read, or 0 on error.
123909123880
** The value is stored in *v.
@@ -123912,11 +123883,11 @@
123912123883
const char *pStart = p;
123913123884
u32 a;
123914123885
u64 b;
123915123886
int shift;
123916123887
123917
- GETVARINT_STEP(a, p, 0, 0x00, 0x80, *v, 1);
123888
+ GETVARINT_INIT(a, p, 0, 0x00, 0x80, *v, 1);
123918123889
GETVARINT_STEP(a, p, 7, 0x7F, 0x4000, *v, 2);
123919123890
GETVARINT_STEP(a, p, 14, 0x3FFF, 0x200000, *v, 3);
123920123891
GETVARINT_STEP(a, p, 21, 0x1FFFFF, 0x10000000, *v, 4);
123921123892
b = (a & 0x0FFFFFFF );
123922123893
@@ -123924,11 +123895,11 @@
123924123895
u64 c = *p++;
123925123896
b += (c&0x7F) << shift;
123926123897
if( (c & 0x80)==0 ) break;
123927123898
}
123928123899
*v = b;
123929
- return p - pStart;
123900
+ return (int)(p - pStart);
123930123901
}
123931123902
123932123903
/*
123933123904
** Similar to sqlite3Fts3GetVarint(), except that the output is truncated to a
123934123905
** 32-bit integer before it is returned.
@@ -123935,11 +123906,11 @@
123935123906
*/
123936123907
SQLITE_PRIVATE int sqlite3Fts3GetVarint32(const char *p, int *pi){
123937123908
u32 a;
123938123909
123939123910
#ifndef fts3GetVarint32
123940
- GETVARINT_STEP(a, p, 0, 0x00, 0x80, *pi, 1);
123911
+ GETVARINT_INIT(a, p, 0, 0x00, 0x80, *pi, 1);
123941123912
#else
123942123913
a = (*p++);
123943123914
assert( a & 0x80 );
123944123915
#endif
123945123916
123946123917
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -135,11 +135,11 @@
135 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
136 ** [sqlite_version()] and [sqlite_source_id()].
137 */
138 #define SQLITE_VERSION "3.8.2"
139 #define SQLITE_VERSION_NUMBER 3008002
140 #define SQLITE_SOURCE_ID "2013-11-14 19:34:10 10d59226382adcb8016fc2d927e5a0c0b36f3980"
141
142 /*
143 ** CAPI3REF: Run-Time Library Version Numbers
144 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
145 **
@@ -396,11 +396,11 @@
396 ** Restrictions:
397 **
398 ** <ul>
399 ** <li> The application must insure that the 1st parameter to sqlite3_exec()
400 ** is a valid and open [database connection].
401 ** <li> The application must not close [database connection] specified by
402 ** the 1st parameter to sqlite3_exec() while sqlite3_exec() is running.
403 ** <li> The application must not modify the SQL statement text passed into
404 ** the 2nd parameter of sqlite3_exec() while sqlite3_exec() is running.
405 ** </ul>
406 */
@@ -473,11 +473,11 @@
473 ** about errors. The extended result codes are enabled or disabled
474 ** on a per database connection basis using the
475 ** [sqlite3_extended_result_codes()] API.
476 **
477 ** Some of the available extended result codes are listed here.
478 ** One may expect the number of extended result codes will be expand
479 ** over time. Software that uses extended result codes should expect
480 ** to see new result codes in future releases of SQLite.
481 **
482 ** The SQLITE_OK result code will never be extended. It will always
483 ** be exactly zero.
@@ -1411,11 +1411,11 @@
1411 ** of 8. Some allocators round up to a larger multiple or to a power of 2.
1412 ** Every memory allocation request coming in through [sqlite3_malloc()]
1413 ** or [sqlite3_realloc()] first calls xRoundup. If xRoundup returns 0,
1414 ** that causes the corresponding memory allocation to fail.
1415 **
1416 ** The xInit method initializes the memory allocator. (For example,
1417 ** it might allocate any require mutexes or initialize internal data
1418 ** structures. The xShutdown method is invoked (indirectly) by
1419 ** [sqlite3_shutdown()] and should deallocate any resources acquired
1420 ** by xInit. The pAppData pointer is used as the only parameter to
1421 ** xInit and xShutdown.
@@ -3137,11 +3137,10 @@
3137 ** to the [sqlite3_bind_text | bindings] of that [parameter].
3138 ** ^The specific value of WHERE-clause [parameter] might influence the
3139 ** choice of query plan if the parameter is the left-hand side of a [LIKE]
3140 ** or [GLOB] operator or if the parameter is compared to an indexed column
3141 ** and the [SQLITE_ENABLE_STAT3] compile-time option is enabled.
3142 ** the
3143 ** </li>
3144 ** </ol>
3145 */
3146 SQLITE_API int sqlite3_prepare(
3147 sqlite3 *db, /* Database handle */
@@ -3867,11 +3866,11 @@
3867 **
3868 ** ^The pointers returned are valid until a type conversion occurs as
3869 ** described above, or until [sqlite3_step()] or [sqlite3_reset()] or
3870 ** [sqlite3_finalize()] is called. ^The memory space used to hold strings
3871 ** and BLOBs is freed automatically. Do <b>not</b> pass the pointers returned
3872 ** [sqlite3_column_blob()], [sqlite3_column_text()], etc. into
3873 ** [sqlite3_free()].
3874 **
3875 ** ^(If a memory allocation error occurs during the evaluation of any
3876 ** of these routines, a default value is returned. The default value
3877 ** is either the integer 0, the floating point number 0.0, or a NULL
@@ -4945,12 +4944,12 @@
4945 /*
4946 ** CAPI3REF: Free Memory Used By A Database Connection
4947 **
4948 ** ^The sqlite3_db_release_memory(D) interface attempts to free as much heap
4949 ** memory as possible from database connection D. Unlike the
4950 ** [sqlite3_release_memory()] interface, this interface is effect even
4951 ** when then [SQLITE_ENABLE_MEMORY_MANAGEMENT] compile-time option is
4952 ** omitted.
4953 **
4954 ** See also: [sqlite3_release_memory()]
4955 */
4956 SQLITE_API int sqlite3_db_release_memory(sqlite3*);
@@ -9096,22 +9095,22 @@
9096 #define OP_NotNull 75 /* same as TK_NOTNULL, synopsis: if r[P1]!=NULL goto P2 */
9097 #define OP_Ne 76 /* same as TK_NE, synopsis: if r[P1]!=r[P3] goto P2 */
9098 #define OP_Eq 77 /* same as TK_EQ, synopsis: if r[P1]==r[P3] goto P2 */
9099 #define OP_Gt 78 /* same as TK_GT, synopsis: if r[P1]>r[P3] goto P2 */
9100 #define OP_Le 79 /* same as TK_LE, synopsis: if r[P1]<=r[P3] goto P2 */
9101 #define OP_Lt 80 /* same as TK_LT, synopsis: if r[P1]<r[P3] goto P3 */
9102 #define OP_Ge 81 /* same as TK_GE, synopsis: if r[P1]>=r[P3] goto P2 */
9103 #define OP_RowKey 82 /* synopsis: r[P2]=key */
9104 #define OP_BitAnd 83 /* same as TK_BITAND, synopsis: r[P3]=r[P1]&r[P2] */
9105 #define OP_BitOr 84 /* same as TK_BITOR, synopsis: r[P3]=r[P1]|r[P2] */
9106 #define OP_ShiftLeft 85 /* same as TK_LSHIFT, synopsis: r[P3]=r[P2]<<r[P1] */
9107 #define OP_ShiftRight 86 /* same as TK_RSHIFT, synopsis: r[P3]=r[P2]>>r[P1] */
9108 #define OP_Add 87 /* same as TK_PLUS, synopsis: r[P3]=r[P1]+r[P2] */
9109 #define OP_Subtract 88 /* same as TK_MINUS, synopsis: r[P3]=r[P2]-r[P1] */
9110 #define OP_Multiply 89 /* same as TK_STAR, synopsis: r[P3]=r[P1]*r[P2] */
9111 #define OP_Divide 90 /* same as TK_SLASH, synopsis: r[P3]=r[P1]/r[P2] */
9112 #define OP_Remainder 91 /* same as TK_REM, synopsis: r[P3]=r[P1]%r[P2] */
9113 #define OP_Concat 92 /* same as TK_CONCAT, synopsis: r[P3]=r[P2]+r[P1] */
9114 #define OP_RowData 93 /* synopsis: r[P2]=data */
9115 #define OP_BitNot 94 /* same as TK_BITNOT, synopsis: r[P1]= ~r[P1] */
9116 #define OP_String8 95 /* same as TK_STRING, synopsis: r[P2]='P4' */
9117 #define OP_Rowid 96 /* synopsis: r[P2]=rowid */
@@ -11089,11 +11088,11 @@
11089 #define EP_Distinct 0x000010 /* Aggregate function with DISTINCT keyword */
11090 #define EP_VarSelect 0x000020 /* pSelect is correlated, not constant */
11091 #define EP_DblQuoted 0x000040 /* token.z was originally in "..." */
11092 #define EP_InfixFunc 0x000080 /* True for an infix function: LIKE, GLOB, etc */
11093 #define EP_Collate 0x000100 /* Tree contains a TK_COLLATE opeartor */
11094 #define EP_FixedDest 0x000200 /* Result needed in a specific register */
11095 #define EP_IntValue 0x000400 /* Integer value contained in u.iValue */
11096 #define EP_xIsSelect 0x000800 /* x.pSelect is valid (otherwise x.pList is) */
11097 #define EP_Skip 0x001000 /* COLLATE, AS, or UNLIKELY */
11098 #define EP_Reduced 0x002000 /* Expr struct EXPR_REDUCEDSIZE bytes only */
11099 #define EP_TokenOnly 0x004000 /* Expr struct EXPR_TOKENONLYSIZE bytes only */
@@ -11160,12 +11159,17 @@
11160 char *zName; /* Token associated with this expression */
11161 char *zSpan; /* Original text of the expression */
11162 u8 sortOrder; /* 1 for DESC or 0 for ASC */
11163 unsigned done :1; /* A flag to indicate when processing is finished */
11164 unsigned bSpanIsTab :1; /* zSpan holds DB.TABLE.COLUMN */
11165 u16 iOrderByCol; /* For ORDER BY, column number in result set */
11166 u16 iAlias; /* Index into Parse.aAlias[] for zName */
 
 
 
 
 
11167 } *a; /* Alloc a power of two greater or equal to nExpr */
11168 };
11169
11170 /*
11171 ** An instance of this structure is used by the parser to record both
@@ -11538,10 +11542,11 @@
11538 u8 tempReg; /* iReg is a temp register that needs to be freed */
11539 int iLevel; /* Nesting level */
11540 int iReg; /* Reg with value of this column. 0 means none. */
11541 int lru; /* Least recently used entry has the smallest value */
11542 } aColCache[SQLITE_N_COLCACHE]; /* One for each column cache entry */
 
11543 yDbMask writeMask; /* Start a write transaction on these databases */
11544 yDbMask cookieMask; /* Bitmask of schema verified databases */
11545 int cookieGoto; /* Address of OP_Goto to cookie verifier subroutine */
11546 int cookieValue[SQLITE_MAX_ATTACHED+2]; /* Values of cookies to verify */
11547 int regRowid; /* Register holding rowid of CREATE TABLE entry */
@@ -12151,11 +12156,10 @@
12151 SQLITE_PRIVATE void sqlite3ExprCacheAffinityChange(Parse*, int, int);
12152 SQLITE_PRIVATE int sqlite3ExprCode(Parse*, Expr*, int);
12153 SQLITE_PRIVATE int sqlite3ExprCodeTemp(Parse*, Expr*, int*);
12154 SQLITE_PRIVATE int sqlite3ExprCodeTarget(Parse*, Expr*, int);
12155 SQLITE_PRIVATE int sqlite3ExprCodeAndCache(Parse*, Expr*, int);
12156 SQLITE_PRIVATE void sqlite3ExprCodeConstants(Parse*, Expr*);
12157 SQLITE_PRIVATE int sqlite3ExprCodeExprList(Parse*, ExprList*, int, int);
12158 SQLITE_PRIVATE void sqlite3ExprIfTrue(Parse*, Expr*, int, int);
12159 SQLITE_PRIVATE void sqlite3ExprIfFalse(Parse*, Expr*, int, int);
12160 SQLITE_PRIVATE Table *sqlite3FindTable(sqlite3*,const char*, const char*);
12161 SQLITE_PRIVATE Table *sqlite3LocateTable(Parse*,int isView,const char*, const char*);
@@ -12197,11 +12201,11 @@
12197 SQLITE_PRIVATE void sqlite3GenerateRowIndexDelete(Parse*, Table*, int, int, int*);
12198 SQLITE_PRIVATE int sqlite3GenerateIndexKey(Parse*, Index*, int, int, int, int*);
12199 SQLITE_PRIVATE void sqlite3GenerateConstraintChecks(Parse*,Table*,int*,int,int,int,int,
12200 u8,u8,int,int*);
12201 SQLITE_PRIVATE void sqlite3CompleteInsertion(Parse*,Table*,int,int,int,int*,int,int,int);
12202 SQLITE_PRIVATE int sqlite3OpenTableAndIndices(Parse*, Table*, int, int, int*, int*);
12203 SQLITE_PRIVATE void sqlite3BeginWriteOperation(Parse*, int, int);
12204 SQLITE_PRIVATE void sqlite3MultiWrite(Parse*);
12205 SQLITE_PRIVATE void sqlite3MayAbort(Parse*);
12206 SQLITE_PRIVATE void sqlite3HaltConstraint(Parse*, int, int, char*, i8, u8);
12207 SQLITE_PRIVATE void sqlite3UniqueConstraint(Parse*, int, Index*);
@@ -12519,10 +12523,11 @@
12519 SQLITE_PRIVATE FuncDef *sqlite3VtabOverloadFunction(sqlite3 *,FuncDef*, int nArg, Expr*);
12520 SQLITE_PRIVATE void sqlite3InvalidFunction(sqlite3_context*,int,sqlite3_value**);
12521 SQLITE_PRIVATE sqlite3_int64 sqlite3StmtCurrentTime(sqlite3_context*);
12522 SQLITE_PRIVATE int sqlite3VdbeParameterIndex(Vdbe*, const char*, int);
12523 SQLITE_PRIVATE int sqlite3TransferBindings(sqlite3_stmt *, sqlite3_stmt *);
 
12524 SQLITE_PRIVATE int sqlite3Reprepare(Vdbe*);
12525 SQLITE_PRIVATE void sqlite3ExprListCheckLength(Parse*, ExprList*, const char*);
12526 SQLITE_PRIVATE CollSeq *sqlite3BinaryCompareCollSeq(Parse *, Expr *, Expr *);
12527 SQLITE_PRIVATE int sqlite3TempInMemory(const sqlite3*);
12528 SQLITE_PRIVATE const char *sqlite3JournalModename(int);
@@ -22970,22 +22975,22 @@
22970 /* 75 */ "NotNull" OpHelp("if r[P1]!=NULL goto P2"),
22971 /* 76 */ "Ne" OpHelp("if r[P1]!=r[P3] goto P2"),
22972 /* 77 */ "Eq" OpHelp("if r[P1]==r[P3] goto P2"),
22973 /* 78 */ "Gt" OpHelp("if r[P1]>r[P3] goto P2"),
22974 /* 79 */ "Le" OpHelp("if r[P1]<=r[P3] goto P2"),
22975 /* 80 */ "Lt" OpHelp("if r[P1]<r[P3] goto P3"),
22976 /* 81 */ "Ge" OpHelp("if r[P1]>=r[P3] goto P2"),
22977 /* 82 */ "RowKey" OpHelp("r[P2]=key"),
22978 /* 83 */ "BitAnd" OpHelp("r[P3]=r[P1]&r[P2]"),
22979 /* 84 */ "BitOr" OpHelp("r[P3]=r[P1]|r[P2]"),
22980 /* 85 */ "ShiftLeft" OpHelp("r[P3]=r[P2]<<r[P1]"),
22981 /* 86 */ "ShiftRight" OpHelp("r[P3]=r[P2]>>r[P1]"),
22982 /* 87 */ "Add" OpHelp("r[P3]=r[P1]+r[P2]"),
22983 /* 88 */ "Subtract" OpHelp("r[P3]=r[P2]-r[P1]"),
22984 /* 89 */ "Multiply" OpHelp("r[P3]=r[P1]*r[P2]"),
22985 /* 90 */ "Divide" OpHelp("r[P3]=r[P1]/r[P2]"),
22986 /* 91 */ "Remainder" OpHelp("r[P3]=r[P1]%r[P2]"),
22987 /* 92 */ "Concat" OpHelp("r[P3]=r[P2]+r[P1]"),
22988 /* 93 */ "RowData" OpHelp("r[P2]=data"),
22989 /* 94 */ "BitNot" OpHelp("r[P1]= ~r[P1]"),
22990 /* 95 */ "String8" OpHelp("r[P2]='P4'"),
22991 /* 96 */ "Rowid" OpHelp("r[P2]=rowid"),
@@ -58866,10 +58871,11 @@
58866 if( sqlite3OpenTempDatabase(pParse) ){
58867 sqlite3Error(pErrorDb, pParse->rc, "%s", pParse->zErrMsg);
58868 rc = SQLITE_ERROR;
58869 }
58870 sqlite3DbFree(pErrorDb, pParse->zErrMsg);
 
58871 sqlite3StackFree(pErrorDb, pParse);
58872 }
58873 if( rc ){
58874 return 0;
58875 }
@@ -63898,19 +63904,16 @@
63898 pMem->u.i = serial_type-8;
63899 pMem->flags = MEM_Int;
63900 return 0;
63901 }
63902 default: {
 
63903 u32 len = (serial_type-12)/2;
63904 pMem->z = (char *)buf;
63905 pMem->n = len;
63906 pMem->xDel = 0;
63907 if( serial_type&0x01 ){
63908 pMem->flags = MEM_Str | MEM_Ephem;
63909 }else{
63910 pMem->flags = MEM_Blob | MEM_Ephem;
63911 }
63912 return len;
63913 }
63914 }
63915 return 0;
63916 }
@@ -67793,23 +67796,23 @@
67793 ** Subtract the value in register P1 from the value in register P2
67794 ** and store the result in register P3.
67795 ** If either input is NULL, the result is NULL.
67796 */
67797 /* Opcode: Divide P1 P2 P3 * *
67798 ** Synopsis: r[P3]=r[P1]/r[P2]
67799 **
67800 ** Divide the value in register P1 by the value in register P2
67801 ** and store the result in register P3 (P3=P2/P1). If the value in
67802 ** register P1 is zero, then the result is NULL. If either input is
67803 ** NULL, the result is NULL.
67804 */
67805 /* Opcode: Remainder P1 P2 P3 * *
67806 ** Synopsis: r[P3]=r[P1]%r[P2]
67807 **
67808 ** Compute the remainder after integer division of the value in
67809 ** register P1 by the value in register P2 and store the result in P3.
67810 ** If the value in register P2 is zero the result is NULL.
67811 ** If either operand is NULL, the result is NULL.
67812 */
67813 case OP_Add: /* same as TK_PLUS, in1, in2, out3 */
67814 case OP_Subtract: /* same as TK_MINUS, in1, in2, out3 */
67815 case OP_Multiply: /* same as TK_STAR, in1, in2, out3 */
@@ -68274,11 +68277,11 @@
68274 break;
68275 }
68276 #endif /* !defined(SQLITE_OMIT_CAST) && !defined(SQLITE_OMIT_FLOATING_POINT) */
68277
68278 /* Opcode: Lt P1 P2 P3 P4 P5
68279 ** Synopsis: if r[P1]<r[P3] goto P3
68280 **
68281 ** Compare the values in register P1 and P3. If reg(P3)<reg(P1) then
68282 ** jump to address P2.
68283 **
68284 ** If the SQLITE_JUMPIFNULL bit of P5 is set and either reg(P1) or
@@ -73278,10 +73281,11 @@
73278 if( pBlob && pBlob->pStmt ) sqlite3VdbeFinalize((Vdbe *)pBlob->pStmt);
73279 sqlite3DbFree(db, pBlob);
73280 }
73281 sqlite3Error(db, rc, (zErr ? "%s" : 0), zErr);
73282 sqlite3DbFree(db, zErr);
 
73283 sqlite3StackFree(db, pParse);
73284 rc = sqlite3ApiExit(db, rc);
73285 sqlite3_mutex_leave(db->mutex);
73286 return rc;
73287 }
@@ -75243,14 +75247,14 @@
75243 if( pOrig->op!=TK_COLUMN && zType[0]!='G' ){
75244 incrAggFunctionDepth(pDup, nSubquery);
75245 pDup = sqlite3PExpr(pParse, TK_AS, pDup, 0, 0);
75246 if( pDup==0 ) return;
75247 ExprSetProperty(pDup, EP_Skip);
75248 if( pEList->a[iCol].iAlias==0 ){
75249 pEList->a[iCol].iAlias = (u16)(++pParse->nAlias);
75250 }
75251 pDup->iTable = pEList->a[iCol].iAlias;
75252 }
75253 if( pExpr->op==TK_COLLATE ){
75254 pDup = sqlite3ExprAddCollateString(pParse, pDup, pExpr->u.zToken);
75255 }
75256
@@ -76111,11 +76115,11 @@
76111 assert( pItem->pExpr->op==TK_COLLATE );
76112 assert( pItem->pExpr->pLeft==pE );
76113 pItem->pExpr->pLeft = pNew;
76114 }
76115 sqlite3ExprDelete(db, pE);
76116 pItem->iOrderByCol = (u16)iCol;
76117 pItem->done = 1;
76118 }else{
76119 moreToDo = 1;
76120 }
76121 }
@@ -76132,12 +76136,12 @@
76132 }
76133
76134 /*
76135 ** Check every term in the ORDER BY or GROUP BY clause pOrderBy of
76136 ** the SELECT statement pSelect. If any term is reference to a
76137 ** result set expression (as determined by the ExprList.a.iOrderByCol field)
76138 ** then convert that term into a copy of the corresponding result set
76139 ** column.
76140 **
76141 ** If any errors are detected, add an error message to pParse and
76142 ** return non-zero. Return zero if no errors are seen.
76143 */
@@ -76160,16 +76164,16 @@
76160 }
76161 #endif
76162 pEList = pSelect->pEList;
76163 assert( pEList!=0 ); /* sqlite3SelectNew() guarantees this */
76164 for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
76165 if( pItem->iOrderByCol ){
76166 if( pItem->iOrderByCol>pEList->nExpr ){
76167 resolveOutOfRangeError(pParse, zType, i+1, pEList->nExpr);
76168 return 1;
76169 }
76170 resolveAlias(pParse, pEList, pItem->iOrderByCol-1, pItem->pExpr, zType,0);
76171 }
76172 }
76173 return 0;
76174 }
76175
@@ -76214,11 +76218,11 @@
76214 if( iCol>0 ){
76215 /* If an AS-name match is found, mark this ORDER BY column as being
76216 ** a copy of the iCol-th result-set column. The subsequent call to
76217 ** sqlite3ResolveOrderGroupBy() will convert the expression to a
76218 ** copy of the iCol-th result-set expression. */
76219 pItem->iOrderByCol = (u16)iCol;
76220 continue;
76221 }
76222 }
76223 if( sqlite3ExprIsInteger(pE2, &iCol) ){
76224 /* The ORDER BY term is an integer constant. Again, set the column
@@ -76226,22 +76230,22 @@
76226 ** order-by term to a copy of the result-set expression */
76227 if( iCol<1 || iCol>0xffff ){
76228 resolveOutOfRangeError(pParse, zType, i+1, nResult);
76229 return 1;
76230 }
76231 pItem->iOrderByCol = (u16)iCol;
76232 continue;
76233 }
76234
76235 /* Otherwise, treat the ORDER BY term as an ordinary expression */
76236 pItem->iOrderByCol = 0;
76237 if( sqlite3ResolveExprNames(pNC, pE) ){
76238 return 1;
76239 }
76240 for(j=0; j<pSelect->pEList->nExpr; j++){
76241 if( sqlite3ExprCompare(pE, pSelect->pEList->a[j].pExpr, -1)==0 ){
76242 pItem->iOrderByCol = j+1;
76243 }
76244 }
76245 }
76246 return sqlite3ResolveOrderGroupBy(pParse, pSelect, pOrderBy, zType);
76247 }
@@ -77516,12 +77520,11 @@
77516 pItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
77517 pItem->zSpan = sqlite3DbStrDup(db, pOldItem->zSpan);
77518 pItem->sortOrder = pOldItem->sortOrder;
77519 pItem->done = 0;
77520 pItem->bSpanIsTab = pOldItem->bSpanIsTab;
77521 pItem->iOrderByCol = pOldItem->iOrderByCol;
77522 pItem->iAlias = pOldItem->iAlias;
77523 }
77524 return pNew;
77525 }
77526
77527 /*
@@ -78942,10 +78945,11 @@
78942 int inReg = target; /* Results stored in register inReg */
78943 int regFree1 = 0; /* If non-zero free this temporary register */
78944 int regFree2 = 0; /* If non-zero free this temporary register */
78945 int r1, r2, r3, r4; /* Various register numbers */
78946 sqlite3 *db = pParse->db; /* The database connection */
 
78947
78948 assert( target>0 && target<=pParse->nMem );
78949 if( v==0 ){
78950 assert( pParse->db->mallocFailed );
78951 return 0;
@@ -79161,12 +79165,14 @@
79161 }else if( pLeft->op==TK_FLOAT ){
79162 assert( !ExprHasProperty(pExpr, EP_IntValue) );
79163 codeReal(v, pLeft->u.zToken, 1, target);
79164 #endif
79165 }else{
79166 regFree1 = r1 = sqlite3GetTempReg(pParse);
79167 sqlite3VdbeAddOp2(v, OP_Integer, 0, r1);
 
 
79168 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree2);
79169 sqlite3VdbeAddOp3(v, OP_Subtract, r2, r1, target);
79170 testcase( regFree2==0 );
79171 }
79172 inReg = target;
@@ -79478,11 +79484,10 @@
79478 int nExpr; /* 2x number of WHEN terms */
79479 int i; /* Loop counter */
79480 ExprList *pEList; /* List of WHEN terms */
79481 struct ExprList_item *aListelem; /* Array of WHEN terms */
79482 Expr opCompare; /* The X==Ei expression */
79483 Expr cacheX; /* Cached expression X */
79484 Expr *pX; /* The X expression */
79485 Expr *pTest = 0; /* X==Ei (form A) or just Ei (form B) */
79486 VVA_ONLY( int iCacheLevel = pParse->iCacheLevel; )
79487
79488 assert( !ExprHasProperty(pExpr, EP_xIsSelect) && pExpr->x.pList );
@@ -79490,17 +79495,16 @@
79490 pEList = pExpr->x.pList;
79491 aListelem = pEList->a;
79492 nExpr = pEList->nExpr;
79493 endLabel = sqlite3VdbeMakeLabel(v);
79494 if( (pX = pExpr->pLeft)!=0 ){
79495 cacheX = *pX;
79496 testcase( pX->op==TK_COLUMN );
79497 testcase( pX->op==TK_REGISTER );
79498 exprToRegister(&cacheX, sqlite3ExprCodeTemp(pParse, pX, &regFree1));
79499 testcase( regFree1==0 );
79500 opCompare.op = TK_EQ;
79501 opCompare.pLeft = &cacheX;
79502 pTest = &opCompare;
79503 /* Ticket b351d95f9cd5ef17e9d9dbae18f5ca8611190001:
79504 ** The value in regFree1 might get SCopy-ed into the file result.
79505 ** So make sure that the regFree1 register is not reused for other
79506 ** purposes and possibly overwritten. */
@@ -79516,11 +79520,10 @@
79516 }
79517 nextCase = sqlite3VdbeMakeLabel(v);
79518 testcase( pTest->op==TK_COLUMN );
79519 sqlite3ExprIfFalse(pParse, pTest, nextCase, SQLITE_JUMPIFNULL);
79520 testcase( aListelem[i+1].pExpr->op==TK_COLUMN );
79521 testcase( aListelem[i+1].pExpr->op==TK_REGISTER );
79522 sqlite3ExprCode(pParse, aListelem[i+1].pExpr, target);
79523 sqlite3VdbeAddOp2(v, OP_Goto, 0, endLabel);
79524 sqlite3ExprCachePop(pParse, 1);
79525 sqlite3VdbeResolveLabel(v, nextCase);
79526 }
@@ -79575,19 +79578,45 @@
79575 ** are stored.
79576 **
79577 ** If the register is a temporary register that can be deallocated,
79578 ** then write its number into *pReg. If the result register is not
79579 ** a temporary, then set *pReg to zero.
 
 
 
 
79580 */
79581 SQLITE_PRIVATE int sqlite3ExprCodeTemp(Parse *pParse, Expr *pExpr, int *pReg){
79582 int r1 = sqlite3GetTempReg(pParse);
79583 int r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1);
79584 if( r2==r1 ){
79585 *pReg = r1;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79586 }else{
79587 sqlite3ReleaseTempReg(pParse, r1);
79588 *pReg = 0;
 
 
 
 
 
 
79589 }
79590 return r2;
79591 }
79592
79593 /*
@@ -79626,16 +79655,17 @@
79626 SQLITE_PRIVATE int sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr, int target){
79627 Vdbe *v = pParse->pVdbe;
79628 int inReg;
79629 inReg = sqlite3ExprCode(pParse, pExpr, target);
79630 assert( target>0 );
79631 /* This routine is called for terms to INSERT or UPDATE. And the only
79632 ** other place where expressions can be converted into TK_REGISTER is
79633 ** in WHERE clause processing. So as currently implemented, there is
79634 ** no way for a TK_REGISTER to exist here. But it seems prudent to
79635 ** keep the ALWAYS() in case the conditions above change with future
79636 ** modifications or enhancements. */
 
79637 if( ALWAYS(pExpr->op!=TK_REGISTER) ){
79638 int iMem;
79639 iMem = ++pParse->nMem;
79640 sqlite3VdbeAddOp2(v, OP_Copy, inReg, iMem);
79641 exprToRegister(pExpr, iMem);
@@ -79913,144 +79943,10 @@
79913 }
79914 sqlite3ExplainPop(pOut);
79915 }
79916 }
79917 #endif /* SQLITE_DEBUG */
79918
79919 /*
79920 ** Return TRUE if pExpr is an constant expression that is appropriate
79921 ** for factoring out of a loop. Appropriate expressions are:
79922 **
79923 ** * Any expression that evaluates to two or more opcodes.
79924 **
79925 ** * Any OP_Integer, OP_Real, OP_String, OP_Blob, OP_Null,
79926 ** or OP_Variable that does not need to be placed in a
79927 ** specific register.
79928 **
79929 ** There is no point in factoring out single-instruction constant
79930 ** expressions that need to be placed in a particular register.
79931 ** We could factor them out, but then we would end up adding an
79932 ** OP_SCopy instruction to move the value into the correct register
79933 ** later. We might as well just use the original instruction and
79934 ** avoid the OP_SCopy.
79935 */
79936 static int isAppropriateForFactoring(Expr *p){
79937 if( !sqlite3ExprIsConstantNotJoin(p) ){
79938 return 0; /* Only constant expressions are appropriate for factoring */
79939 }
79940 if( (p->flags & EP_FixedDest)==0 ){
79941 return 1; /* Any constant without a fixed destination is appropriate */
79942 }
79943 while( p->op==TK_UPLUS ) p = p->pLeft;
79944 switch( p->op ){
79945 #ifndef SQLITE_OMIT_BLOB_LITERAL
79946 case TK_BLOB:
79947 #endif
79948 case TK_VARIABLE:
79949 case TK_INTEGER:
79950 case TK_FLOAT:
79951 case TK_NULL:
79952 case TK_STRING: {
79953 testcase( p->op==TK_BLOB );
79954 testcase( p->op==TK_VARIABLE );
79955 testcase( p->op==TK_INTEGER );
79956 testcase( p->op==TK_FLOAT );
79957 testcase( p->op==TK_NULL );
79958 testcase( p->op==TK_STRING );
79959 /* Single-instruction constants with a fixed destination are
79960 ** better done in-line. If we factor them, they will just end
79961 ** up generating an OP_SCopy to move the value to the destination
79962 ** register. */
79963 return 0;
79964 }
79965 case TK_UMINUS: {
79966 if( p->pLeft->op==TK_FLOAT || p->pLeft->op==TK_INTEGER ){
79967 return 0;
79968 }
79969 break;
79970 }
79971 default: {
79972 break;
79973 }
79974 }
79975 return 1;
79976 }
79977
79978 /*
79979 ** If pExpr is a constant expression that is appropriate for
79980 ** factoring out of a loop, then evaluate the expression
79981 ** into a register and convert the expression into a TK_REGISTER
79982 ** expression.
79983 */
79984 static int evalConstExpr(Walker *pWalker, Expr *pExpr){
79985 Parse *pParse = pWalker->pParse;
79986 switch( pExpr->op ){
79987 case TK_IN:
79988 case TK_REGISTER: {
79989 return WRC_Prune;
79990 }
79991 case TK_COLLATE: {
79992 return WRC_Continue;
79993 }
79994 case TK_FUNCTION:
79995 case TK_AGG_FUNCTION:
79996 case TK_CONST_FUNC: {
79997 /* The arguments to a function have a fixed destination.
79998 ** Mark them this way to avoid generated unneeded OP_SCopy
79999 ** instructions.
80000 */
80001 ExprList *pList = pExpr->x.pList;
80002 assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
80003 if( pList ){
80004 int i = pList->nExpr;
80005 struct ExprList_item *pItem = pList->a;
80006 for(; i>0; i--, pItem++){
80007 if( ALWAYS(pItem->pExpr) ) pItem->pExpr->flags |= EP_FixedDest;
80008 }
80009 }
80010 break;
80011 }
80012 }
80013 if( isAppropriateForFactoring(pExpr) ){
80014 int r1 = ++pParse->nMem;
80015 int r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1);
80016 /* If r2!=r1, it means that register r1 is never used. That is harmless
80017 ** but suboptimal, so we want to know about the situation to fix it.
80018 ** Hence the following assert: */
80019 assert( r2==r1 );
80020 exprToRegister(pExpr, r2);
80021 return WRC_Prune;
80022 }
80023 return WRC_Continue;
80024 }
80025
80026 /*
80027 ** Preevaluate constant subexpressions within pExpr and store the
80028 ** results in registers. Modify pExpr so that the constant subexpresions
80029 ** are TK_REGISTER opcodes that refer to the precomputed values.
80030 **
80031 ** This routine is a no-op if the jump to the cookie-check code has
80032 ** already occur. Since the cookie-check jump is generated prior to
80033 ** any other serious processing, this check ensures that there is no
80034 ** way to accidently bypass the constant initializations.
80035 **
80036 ** This routine is also a no-op if the SQLITE_FactorOutConst optimization
80037 ** is disabled via the sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS)
80038 ** interface. This allows test logic to verify that the same answer is
80039 ** obtained for queries regardless of whether or not constants are
80040 ** precomputed into registers or if they are inserted in-line.
80041 */
80042 SQLITE_PRIVATE void sqlite3ExprCodeConstants(Parse *pParse, Expr *pExpr){
80043 Walker w;
80044 if( pParse->cookieGoto ) return;
80045 if( OptimizationDisabled(pParse->db, SQLITE_FactorOutConst) ) return;
80046 memset(&w, 0, sizeof(w));
80047 w.xExprCallback = evalConstExpr;
80048 w.pParse = pParse;
80049 sqlite3WalkExpr(&w, pExpr);
80050 }
80051
80052
80053 /*
80054 ** Generate code that pushes the value of every element of the given
80055 ** expression list into a sequence of registers beginning at target.
80056 **
@@ -80425,44 +80321,46 @@
80425 ** this routine is used, it does not hurt to get an extra 2 - that
80426 ** just might result in some slightly slower code. But returning
80427 ** an incorrect 0 or 1 could lead to a malfunction.
80428 */
80429 SQLITE_PRIVATE int sqlite3ExprCompare(Expr *pA, Expr *pB, int iTab){
80430 if( pA==0||pB==0 ){
 
80431 return pB==pA ? 0 : 2;
80432 }
80433 assert( !ExprHasProperty(pA, EP_TokenOnly|EP_Reduced) );
80434 assert( !ExprHasProperty(pB, EP_TokenOnly|EP_Reduced) );
80435 if( ExprHasProperty(pA, EP_xIsSelect) || ExprHasProperty(pB, EP_xIsSelect) ){
 
 
80436 return 2;
80437 }
80438 if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 2;
80439 if( pA->op!=pB->op && (pA->op!=TK_REGISTER || pA->op2!=pB->op) ){
80440 if( pA->op==TK_COLLATE && sqlite3ExprCompare(pA->pLeft, pB, iTab)<2 ){
80441 return 1;
80442 }
80443 if( pB->op==TK_COLLATE && sqlite3ExprCompare(pA, pB->pLeft, iTab)<2 ){
80444 return 1;
80445 }
80446 return 2;
80447 }
80448 if( sqlite3ExprCompare(pA->pLeft, pB->pLeft, iTab) ) return 2;
80449 if( sqlite3ExprCompare(pA->pRight, pB->pRight, iTab) ) return 2;
80450 if( sqlite3ExprListCompare(pA->x.pList, pB->x.pList, iTab) ) return 2;
80451 if( pA->iColumn!=pB->iColumn ) return 2;
80452 if( pA->iTable!=pB->iTable
80453 && pA->op!=TK_REGISTER
80454 && (pA->iTable!=iTab || NEVER(pB->iTable>=0)) ) return 2;
80455 if( ExprHasProperty(pA, EP_IntValue) ){
80456 if( !ExprHasProperty(pB, EP_IntValue) || pA->u.iValue!=pB->u.iValue ){
80457 return 2;
80458 }
80459 }else if( pA->op!=TK_COLUMN && ALWAYS(pA->op!=TK_AGG_COLUMN) && pA->u.zToken){
80460 if( ExprHasProperty(pB, EP_IntValue) || NEVER(pB->u.zToken==0) ) return 2;
80461 if( strcmp(pA->u.zToken,pB->u.zToken)!=0 ){
80462 return pA->op==TK_COLLATE ? 1 : 2;
80463 }
 
 
 
 
 
 
 
 
 
 
 
 
80464 }
80465 return 0;
80466 }
80467
80468 /*
@@ -84435,11 +84333,11 @@
84435 ** transaction on each used database and to verify the schema cookie
84436 ** on each used database.
84437 */
84438 if( pParse->cookieGoto>0 ){
84439 yDbMask mask;
84440 int iDb;
84441 sqlite3VdbeJumpHere(v, pParse->cookieGoto-1);
84442 for(iDb=0, mask=1; iDb<db->nDb; mask<<=1, iDb++){
84443 if( (mask & pParse->cookieMask)==0 ) continue;
84444 sqlite3VdbeUsesBtree(v, iDb);
84445 sqlite3VdbeAddOp2(v,OP_Transaction, iDb, (mask & pParse->writeMask)!=0);
@@ -84449,18 +84347,15 @@
84449 iDb, pParse->cookieValue[iDb],
84450 db->aDb[iDb].pSchema->iGeneration);
84451 }
84452 }
84453 #ifndef SQLITE_OMIT_VIRTUALTABLE
84454 {
84455 int i;
84456 for(i=0; i<pParse->nVtabLock; i++){
84457 char *vtab = (char *)sqlite3GetVTable(db, pParse->apVtabLock[i]);
84458 sqlite3VdbeAddOp4(v, OP_VBegin, 0, 0, 0, vtab, P4_VTAB);
84459 }
84460 pParse->nVtabLock = 0;
84461 }
84462 #endif
84463
84464 /* Once all the cookies have been verified and transactions opened,
84465 ** obtain the required table-locks. This is a no-op unless the
84466 ** shared-cache feature is enabled.
@@ -84468,13 +84363,23 @@
84468 codeTableLocks(pParse);
84469
84470 /* Initialize any AUTOINCREMENT data structures required.
84471 */
84472 sqlite3AutoincrementBegin(pParse);
 
 
 
 
 
 
 
 
 
 
84473
84474 /* Finally, jump back to the beginning of the executable code. */
84475 sqlite3VdbeAddOp2(v, OP_Goto, 0, pParse->cookieGoto);
84476 }
84477 }
84478
84479
84480 /* Get the VDBE program ready for execution
@@ -89185,24 +89090,38 @@
89185 Expr *pWhere /* The WHERE clause. May be null */
89186 ){
89187 Vdbe *v; /* The virtual database engine */
89188 Table *pTab; /* The table from which records will be deleted */
89189 const char *zDb; /* Name of database holding pTab */
89190 int end, addr = 0; /* A couple addresses of generated code */
89191 int i; /* Loop counter */
89192 WhereInfo *pWInfo; /* Information about the WHERE clause */
89193 Index *pIdx; /* For looping over indices of the table */
89194 int iTabCur; /* Cursor number for the table */
89195 int iDataCur; /* VDBE cursor for the canonical data source */
89196 int iIdxCur; /* Cursor number of the first index */
 
89197 sqlite3 *db; /* Main database structure */
89198 AuthContext sContext; /* Authorization context */
89199 NameContext sNC; /* Name context to resolve expressions in */
89200 int iDb; /* Database number */
89201 int memCnt = -1; /* Memory cell used for change counting */
89202 int rcauth; /* Value returned by authorization callback */
89203
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89204 #ifndef SQLITE_OMIT_TRIGGER
89205 int isView; /* True if attempting to delete from a view */
89206 Trigger *pTrigger; /* List of table triggers, if required */
89207 #endif
89208
@@ -89253,15 +89172,15 @@
89253 if( rcauth==SQLITE_DENY ){
89254 goto delete_from_cleanup;
89255 }
89256 assert(!isView || pTrigger);
89257
89258 /* Assign cursor number to the table and all its indices.
89259 */
89260 assert( pTabList->nSrc==1 );
89261 iTabCur = pTabList->a[0].iCursor = pParse->nTab++;
89262 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
89263 pParse->nTab++;
89264 }
89265
89266 /* Start the view context
89267 */
@@ -89323,132 +89242,162 @@
89323 assert( pIdx->pSchema==pTab->pSchema );
89324 sqlite3VdbeAddOp2(v, OP_Clear, pIdx->tnum, iDb);
89325 }
89326 }else
89327 #endif /* SQLITE_OMIT_TRUNCATE_OPTIMIZATION */
89328 if( !HasRowid(pTab) ){
89329 /* There is a WHERE clause on a WITHOUT ROWID table.
89330 */
89331 Index *pPk; /* The PRIMARY KEY index on the table */
89332 int iPk; /* First of nPk memory cells holding PRIMARY KEY value */
89333 int iEph; /* Ephemeral table holding all primary key values */
89334 int iKey; /* Key value inserting into iEph */
89335 i16 nPk; /* Number of components of the PRIMARY KEY */
89336
89337 pPk = sqlite3PrimaryKeyIndex(pTab);
89338 assert( pPk!=0 );
89339 nPk = pPk->nKeyCol;
89340 iPk = pParse->nMem+1;
89341 pParse->nMem += nPk;
89342 iKey = ++pParse->nMem;
89343 iEph = pParse->nTab++;
89344
89345 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, iEph, nPk);
89346 sqlite3VdbeSetP4KeyInfo(pParse, pPk);
89347 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0, 0, 0, 0);
89348 if( pWInfo==0 ) goto delete_from_cleanup;
89349 for(i=0; i<nPk; i++){
89350 sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur, pPk->aiColumn[i],iPk+i);
89351 }
89352 sqlite3VdbeAddOp4(v, OP_MakeRecord, iPk, nPk, iKey,
89353 sqlite3IndexAffinityStr(v, pPk), P4_TRANSIENT);
89354 sqlite3VdbeAddOp2(v, OP_IdxInsert, iEph, iKey);
89355 if( db->flags & SQLITE_CountRows ){
89356 sqlite3VdbeAddOp2(v, OP_AddImm, memCnt, 1);
89357 }
89358 sqlite3WhereEnd(pWInfo);
89359
89360 /* Open cursors for all indices of the table.
89361 */
89362 sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite,
89363 iTabCur, &iDataCur, &iIdxCur);
89364
89365 /* Loop over the primary keys to be deleted. */
89366 addr = sqlite3VdbeAddOp1(v, OP_Rewind, iEph);
89367 sqlite3VdbeAddOp2(v, OP_RowKey, iEph, iPk);
89368
89369 /* Delete the row */
89370 sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur,
89371 iPk, 0, 1, OE_Default, 0);
89372
89373 /* End of the delete loop */
89374 sqlite3VdbeAddOp2(v, OP_Next, iEph, addr+1);
89375 sqlite3VdbeJumpHere(v, addr);
89376
89377 /* Close the cursors open on the table and its indexes. */
89378 assert( iDataCur>=iIdxCur );
89379 for(i=0, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){
89380 sqlite3VdbeAddOp1(v, OP_Close, iIdxCur+i);
89381 }
89382 }else{
89383 /* There is a WHERE clause on a rowid table. Run a loop that extracts
89384 ** all rowids to be deleted into a RowSet.
89385 */
89386 int iRowSet = ++pParse->nMem; /* Register for rowset of rows to delete */
89387 int iRowid = ++pParse->nMem; /* Used for storing rowid values. */
89388 int regRowid; /* Actual register containing rowids */
89389
89390 /* Collect rowids of every row to be deleted.
89391 */
89392 sqlite3VdbeAddOp2(v, OP_Null, 0, iRowSet);
89393 pWInfo = sqlite3WhereBegin(
89394 pParse, pTabList, pWhere, 0, 0, WHERE_DUPLICATES_OK, 0
89395 );
89396 if( pWInfo==0 ) goto delete_from_cleanup;
89397 regRowid = sqlite3ExprCodeGetColumn(pParse, pTab, -1, iTabCur, iRowid, 0);
89398 sqlite3VdbeAddOp2(v, OP_RowSetAdd, iRowSet, regRowid);
89399 if( db->flags & SQLITE_CountRows ){
89400 sqlite3VdbeAddOp2(v, OP_AddImm, memCnt, 1);
89401 }
89402 sqlite3WhereEnd(pWInfo);
89403
89404 /* Delete every item whose key was written to the list during the
89405 ** database scan. We have to delete items after the scan is complete
89406 ** because deleting an item can change the scan order. */
89407 end = sqlite3VdbeMakeLabel(v);
89408
89409 /* Unless this is a view, open cursors for the table we are
89410 ** deleting from and all its indices. If this is a view, then the
89411 ** only effect this statement has is to fire the INSTEAD OF
89412 ** triggers. */
89413 if( !isView ){
89414 sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, iTabCur,
89415 &iDataCur, &iIdxCur);
89416 assert( iDataCur==iTabCur );
89417 assert( iIdxCur==iDataCur+1 );
89418 }
89419
89420 addr = sqlite3VdbeAddOp3(v, OP_RowSetRead, iRowSet, end, iRowid);
89421
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89422 /* Delete the row */
89423 #ifndef SQLITE_OMIT_VIRTUALTABLE
89424 if( IsVirtual(pTab) ){
89425 const char *pVTab = (const char *)sqlite3GetVTable(db, pTab);
89426 sqlite3VtabMakeWritable(pParse, pTab);
89427 sqlite3VdbeAddOp4(v, OP_VUpdate, 0, 1, iRowid, pVTab, P4_VTAB);
89428 sqlite3VdbeChangeP5(v, OE_Abort);
89429 sqlite3MayAbort(pParse);
89430 }else
89431 #endif
89432 {
89433 int count = (pParse->nested==0); /* True to count changes */
89434 sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur,
89435 iRowid, 1, count, OE_Default, 0);
89436 }
89437
89438 /* End of the delete loop */
89439 sqlite3VdbeAddOp2(v, OP_Goto, 0, addr);
89440 sqlite3VdbeResolveLabel(v, end);
89441
 
 
 
 
 
 
 
89442 /* Close the cursors open on the table and its indexes. */
89443 if( !isView && !IsVirtual(pTab) ){
89444 sqlite3VdbeAddOp1(v, OP_Close, iDataCur);
89445 for(i=0, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){
89446 sqlite3VdbeAddOp1(v, OP_Close, iIdxCur + i);
89447 }
89448 }
89449 }
89450
89451 /* Update the sqlite_sequence table by storing the content of the
89452 ** maximum rowid counter values recorded while inserting into
89453 ** autoincrement tables.
89454 */
@@ -89468,10 +89417,11 @@
89468
89469 delete_from_cleanup:
89470 sqlite3AuthContextPop(&sContext);
89471 sqlite3SrcListDelete(db, pTabList);
89472 sqlite3ExprDelete(db, pWhere);
 
89473 return;
89474 }
89475 /* Make sure "isView" and other macros defined above are undefined. Otherwise
89476 ** thely may interfere with compilation of other functions in this file
89477 ** (or in another file, if this file becomes part of the amalgamation). */
@@ -89534,10 +89484,11 @@
89534 /* If there are any triggers to fire, allocate a range of registers to
89535 ** use for the old.* references in the triggers. */
89536 if( sqlite3FkRequired(pParse, pTab, 0, 0) || pTrigger ){
89537 u32 mask; /* Mask of OLD.* columns in use */
89538 int iCol; /* Iterator used while populating OLD.* */
 
89539
89540 /* TODO: Could use temporary registers here. Also could attempt to
89541 ** avoid copying the contents of the rowid register. */
89542 mask = sqlite3TriggerColmask(
89543 pParse, pTrigger, 0, 0, TRIGGER_BEFORE|TRIGGER_AFTER, pTab, onconf
@@ -89554,19 +89505,23 @@
89554 sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, iCol, iOld+iCol+1);
89555 }
89556 }
89557
89558 /* Invoke BEFORE DELETE trigger programs. */
 
89559 sqlite3CodeRowTrigger(pParse, pTrigger,
89560 TK_DELETE, 0, TRIGGER_BEFORE, pTab, iOld, onconf, iLabel
89561 );
89562
89563 /* Seek the cursor to the row to be deleted again. It may be that
89564 ** the BEFORE triggers coded above have already removed the row
89565 ** being deleted. Do not attempt to delete the row a second time, and
89566 ** do not fire AFTER triggers. */
89567 sqlite3VdbeAddOp4Int(v, opSeek, iDataCur, iLabel, iPk, nPk);
 
 
 
89568
89569 /* Do FK processing. This call checks that any FK constraints that
89570 ** refer to this table (i.e. constraints attached to other tables)
89571 ** are not violated by deleting this row. */
89572 sqlite3FkCheck(pParse, pTab, iOld, 0, 0, 0);
@@ -91992,10 +91947,11 @@
91992 Vdbe *v = sqlite3GetVdbe(pParse);
91993
91994 assert( pIdx==0 || pIdx->pTable==pTab );
91995 assert( pIdx==0 || pIdx->nKeyCol==pFKey->nCol );
91996 assert( pIdx!=0 || pFKey->nCol==1 );
 
91997
91998 if( nIncr<0 ){
91999 iFkIfZero = sqlite3VdbeAddOp2(v, OP_FkIfZero, pFKey->isDeferred, 0);
92000 }
92001
@@ -92044,10 +92000,11 @@
92044 pRight = exprTableColumn(db, pTab, pSrc->a[0].iCursor, -1);
92045 pNe = sqlite3PExpr(pParse, TK_NE, pLeft, pRight, 0);
92046 }else{
92047 Expr *pEq, *pAll = 0;
92048 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
 
92049 for(i=0; i<pPk->nKeyCol; i++){
92050 i16 iCol = pIdx->aiColumn[i];
92051 pLeft = exprTableRegister(pParse, pTab, regData, iCol);
92052 pRight = exprTableColumn(db, pTab, pSrc->a[0].iCursor, iCol);
92053 pEq = sqlite3PExpr(pParse, TK_EQ, pLeft, pRight, 0);
@@ -93622,11 +93579,11 @@
93622 }
93623
93624 /* If this is not a view, open the table and and all indices */
93625 if( !isView ){
93626 int nIdx;
93627 nIdx = sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, -1,
93628 &iDataCur, &iIdxCur);
93629 aRegIdx = sqlite3DbMallocRaw(db, sizeof(int)*(nIdx+1));
93630 if( aRegIdx==0 ){
93631 goto insert_cleanup;
93632 }
@@ -94482,42 +94439,50 @@
94482 SQLITE_PRIVATE int sqlite3OpenTableAndIndices(
94483 Parse *pParse, /* Parsing context */
94484 Table *pTab, /* Table to be opened */
94485 int op, /* OP_OpenRead or OP_OpenWrite */
94486 int iBase, /* Use this for the table cursor, if there is one */
 
94487 int *piDataCur, /* Write the database source cursor number here */
94488 int *piIdxCur /* Write the first index cursor number here */
94489 ){
94490 int i;
94491 int iDb;
 
94492 Index *pIdx;
94493 Vdbe *v;
94494
94495 assert( op==OP_OpenRead || op==OP_OpenWrite );
94496 if( IsVirtual(pTab) ){
 
94497 *piDataCur = 0;
94498 *piIdxCur = 1;
94499 return 0;
94500 }
94501 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
94502 v = sqlite3GetVdbe(pParse);
94503 assert( v!=0 );
94504 if( iBase<0 ) iBase = pParse->nTab;
94505 if( HasRowid(pTab) ){
94506 *piDataCur = iBase++;
94507 sqlite3OpenTable(pParse, *piDataCur, iDb, pTab, op);
 
94508 }else{
94509 sqlite3TableLock(pParse, iDb, pTab->tnum, op==OP_OpenWrite, pTab->zName);
94510 }
94511 *piIdxCur = iBase;
94512 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
94513 int iIdxCur = iBase++;
94514 assert( pIdx->pSchema==pTab->pSchema );
94515 if( pIdx->autoIndex==2 && !HasRowid(pTab) ) *piDataCur = iIdxCur;
94516 sqlite3VdbeAddOp3(v, op, iIdxCur, pIdx->tnum, iDb);
94517 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
94518 VdbeComment((v, "%s", pIdx->zName));
 
 
 
 
94519 }
94520 if( iBase>pParse->nTab ) pParse->nTab = iBase;
94521 return i;
94522 }
94523
@@ -98144,11 +98109,11 @@
98144 addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1); /* Stop if out of errors */
98145 sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
98146 sqlite3VdbeJumpHere(v, addr);
98147 sqlite3ExprCacheClear(pParse);
98148 sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenRead,
98149 1, &iDataCur, &iIdxCur);
98150 sqlite3VdbeAddOp2(v, OP_Integer, 0, 7);
98151 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
98152 sqlite3VdbeAddOp2(v, OP_Integer, 0, 8+j); /* index entries counter */
98153 }
98154 pParse->nMem = MAX(pParse->nMem, 8+j);
@@ -99079,10 +99044,17 @@
99079 }
99080 assert( i>=0 && i<db->nDb );
99081 }
99082 return i;
99083 }
 
 
 
 
 
 
 
99084
99085 /*
99086 ** Compile the UTF-8 encoded SQL statement zSql into a statement handle.
99087 */
99088 static int sqlite3Prepare(
@@ -99237,10 +99209,11 @@
99237 sqlite3DbFree(db, pT);
99238 }
99239
99240 end_prepare:
99241
 
99242 sqlite3StackFree(db, pParse);
99243 rc = sqlite3ApiExit(db, rc);
99244 assert( (rc&db->errMask)==rc );
99245 return rc;
99246 }
@@ -101803,20 +101776,20 @@
101803 */
101804 if( op!=TK_ALL ){
101805 for(i=1; db->mallocFailed==0 && i<=p->pEList->nExpr; i++){
101806 struct ExprList_item *pItem;
101807 for(j=0, pItem=pOrderBy->a; j<nOrderBy; j++, pItem++){
101808 assert( pItem->iOrderByCol>0 );
101809 if( pItem->iOrderByCol==i ) break;
101810 }
101811 if( j==nOrderBy ){
101812 Expr *pNew = sqlite3Expr(db, TK_INTEGER, 0);
101813 if( pNew==0 ) return SQLITE_NOMEM;
101814 pNew->flags |= EP_IntValue;
101815 pNew->u.iValue = i;
101816 pOrderBy = sqlite3ExprListAppend(pParse, pOrderBy, pNew);
101817 if( pOrderBy ) pOrderBy->a[nOrderBy++].iOrderByCol = (u16)i;
101818 }
101819 }
101820 }
101821
101822 /* Compute the comparison permutation and keyinfo that is used with
@@ -101828,12 +101801,13 @@
101828 */
101829 aPermute = sqlite3DbMallocRaw(db, sizeof(int)*nOrderBy);
101830 if( aPermute ){
101831 struct ExprList_item *pItem;
101832 for(i=0, pItem=pOrderBy->a; i<nOrderBy; i++, pItem++){
101833 assert( pItem->iOrderByCol>0 && pItem->iOrderByCol<=p->pEList->nExpr );
101834 aPermute[i] = pItem->iOrderByCol - 1;
 
101835 }
101836 pKeyMerge = sqlite3KeyInfoAlloc(db, nOrderBy, 1);
101837 if( pKeyMerge ){
101838 for(i=0; i<nOrderBy; i++){
101839 CollSeq *pColl;
@@ -102409,11 +102383,11 @@
102409
102410 /* Restriction 18. */
102411 if( p->pOrderBy ){
102412 int ii;
102413 for(ii=0; ii<p->pOrderBy->nExpr; ii++){
102414 if( p->pOrderBy->a[ii].iOrderByCol==0 ) return 0;
102415 }
102416 }
102417 }
102418
102419 /***** If we reach this point, flattening is permitted. *****/
@@ -103816,14 +103790,14 @@
103816 if( pGroupBy ){
103817 int k; /* Loop counter */
103818 struct ExprList_item *pItem; /* For looping over expression in a list */
103819
103820 for(k=p->pEList->nExpr, pItem=p->pEList->a; k>0; k--, pItem++){
103821 pItem->iAlias = 0;
103822 }
103823 for(k=pGroupBy->nExpr, pItem=pGroupBy->a; k>0; k--, pItem++){
103824 pItem->iAlias = 0;
103825 }
103826 if( p->nSelectRow>100 ) p->nSelectRow = 100;
103827 }else{
103828 p->nSelectRow = 1;
103829 }
@@ -105470,10 +105444,11 @@
105470 sqlite3VdbeDelete(v);
105471 }
105472
105473 assert( !pSubParse->pAinc && !pSubParse->pZombieTab );
105474 assert( !pSubParse->pTriggerPrg && !pSubParse->nMaxArg );
 
105475 sqlite3StackFree(db, pSubParse);
105476
105477 return pPrg;
105478 }
105479
@@ -105784,22 +105759,23 @@
105784 WhereInfo *pWInfo; /* Information about the WHERE clause */
105785 Vdbe *v; /* The virtual database engine */
105786 Index *pIdx; /* For looping over indices */
105787 Index *pPk; /* The PRIMARY KEY index for WITHOUT ROWID tables */
105788 int nIdx; /* Number of indices that need updating */
 
105789 int iDataCur; /* Cursor for the canonical data btree */
105790 int iIdxCur; /* Cursor for the first index */
105791 sqlite3 *db; /* The database structure */
105792 int *aRegIdx = 0; /* One register assigned to each index to be updated */
105793 int *aXRef = 0; /* aXRef[i] is the index in pChanges->a[] of the
105794 ** an expression for the i-th column of the table.
105795 ** aXRef[i]==-1 if the i-th column is not changed. */
 
105796 u8 chngPk; /* PRIMARY KEY changed in a WITHOUT ROWID table */
105797 u8 chngRowid; /* Rowid changed in a normal table */
105798 u8 chngKey; /* Either chngPk or chngRowid */
105799 Expr *pRowidExpr = 0; /* Expression defining the new record number */
105800 int openAll = 0; /* True if all indices need to be opened */
105801 AuthContext sContext; /* The authorization context */
105802 NameContext sNC; /* The name-context to resolve expressions in */
105803 int iDb; /* Database containing the table being updated */
105804 int okOnePass; /* True for one-pass algorithm without the FIFO */
105805 int hasFK; /* True if foreign key processing is required */
@@ -105859,29 +105835,37 @@
105859 goto update_cleanup;
105860 }
105861 if( sqlite3IsReadOnly(pParse, pTab, tmask) ){
105862 goto update_cleanup;
105863 }
105864 aXRef = sqlite3DbMallocRaw(db, sizeof(int) * pTab->nCol );
105865 if( aXRef==0 ) goto update_cleanup;
105866 for(i=0; i<pTab->nCol; i++) aXRef[i] = -1;
105867
105868 /* Allocate a cursors for the main database table and for all indices.
105869 ** The index cursors might not be used, but if they are used they
105870 ** need to occur right after the database cursor. So go ahead and
105871 ** allocate enough space, just in case.
105872 */
105873 pTabList->a[0].iCursor = iDataCur = pParse->nTab++;
105874 iIdxCur = iDataCur+1;
105875 pPk = HasRowid(pTab) ? 0 : sqlite3PrimaryKeyIndex(pTab);
105876 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){
105877 if( pIdx->autoIndex==2 && pPk!=0 ){
105878 iDataCur = pParse->nTab;
105879 pTabList->a[0].iCursor = iDataCur;
105880 }
105881 pParse->nTab++;
105882 }
 
 
 
 
 
 
 
 
 
 
 
105883
105884 /* Initialize the name-context */
105885 memset(&sNC, 0, sizeof(sNC));
105886 sNC.pParse = pParse;
105887 sNC.pSrcList = pTabList;
@@ -105936,22 +105920,22 @@
105936 }
105937 assert( (chngRowid & chngPk)==0 );
105938 assert( chngRowid==0 || chngRowid==1 );
105939 assert( chngPk==0 || chngPk==1 );
105940 chngKey = chngRowid + chngPk;
 
 
 
 
 
105941
105942 hasFK = sqlite3FkRequired(pParse, pTab, aXRef, chngKey);
105943
105944 /* Allocate memory for the array aRegIdx[]. There is one entry in the
105945 ** array for each index associated with table being updated. Fill in
105946 ** the value with a register number for indices that are to be used
105947 ** and with zero for unused indices.
105948 */
105949 if( nIdx>0 ){
105950 aRegIdx = sqlite3DbMallocRaw(db, sizeof(Index*) * nIdx );
105951 if( aRegIdx==0 ) goto update_cleanup;
105952 }
105953 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
105954 int reg;
105955 if( chngKey || hasFK || pIdx->pPartIdxWhere || pIdx==pPk ){
105956 reg = ++pParse->nMem;
105957 }else{
@@ -105961,10 +105945,11 @@
105961 reg = ++pParse->nMem;
105962 break;
105963 }
105964 }
105965 }
 
105966 aRegIdx[j] = reg;
105967 }
105968
105969 /* Begin generating code. */
105970 v = sqlite3GetVdbe(pParse);
@@ -106084,46 +106069,34 @@
106084 ** Open every index that needs updating. Note that if any
106085 ** index could potentially invoke a REPLACE conflict resolution
106086 ** action, then we need to open all indices because we might need
106087 ** to be deleting some records.
106088 */
106089 if( !okOnePass && HasRowid(pTab) ){
106090 sqlite3OpenTable(pParse, iDataCur, iDb, pTab, OP_OpenWrite);
106091 }
106092 sqlite3TableLock(pParse, iDb, pTab->tnum, 1, pTab->zName);
106093 if( onError==OE_Replace ){
106094 openAll = 1;
106095 }else{
106096 openAll = 0;
106097 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
106098 if( pIdx->onError==OE_Replace ){
106099 openAll = 1;
106100 break;
106101 }
106102 }
106103 }
106104 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
106105 int iThisCur = iIdxCur+i;
106106 assert( aRegIdx );
106107 if( (openAll || aRegIdx[i]>0)
106108 && iThisCur!=aiCurOnePass[1]
106109 ){
106110 assert( iThisCur!=aiCurOnePass[0] );
106111 sqlite3VdbeAddOp3(v, OP_OpenWrite, iThisCur, pIdx->tnum, iDb);
106112 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
106113 assert( pParse->nTab>iThisCur );
106114 VdbeComment((v, "%s", pIdx->zName));
106115 if( okOnePass && pPk && iThisCur==iDataCur ){
106116 sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, labelBreak,
106117 regKey, nKey);
106118 }
106119 }
106120 }
106121 }
106122
106123 /* Top of the update loop */
106124 if( okOnePass ){
 
 
 
 
106125 labelContinue = labelBreak;
106126 sqlite3VdbeAddOp2(v, OP_IsNull, pPk ? regKey : regOldRowid, labelBreak);
106127 }else if( pPk ){
106128 labelContinue = sqlite3VdbeMakeLabel(v);
106129 sqlite3VdbeAddOp2(v, OP_Rewind, iEph, labelBreak);
@@ -106312,11 +106285,11 @@
106312 sqlite3VdbeResolveLabel(v, labelBreak);
106313
106314 /* Close all tables */
106315 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
106316 assert( aRegIdx );
106317 if( openAll || aRegIdx[i]>0 ){
106318 sqlite3VdbeAddOp2(v, OP_Close, iIdxCur+i, 0);
106319 }
106320 }
106321 if( iDataCur<iIdxCur ) sqlite3VdbeAddOp2(v, OP_Close, iDataCur, 0);
106322
@@ -106339,12 +106312,11 @@
106339 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows updated", SQLITE_STATIC);
106340 }
106341
106342 update_cleanup:
106343 sqlite3AuthContextPop(&sContext);
106344 sqlite3DbFree(db, aRegIdx);
106345 sqlite3DbFree(db, aXRef);
106346 sqlite3SrcListDelete(db, pTabList);
106347 sqlite3ExprListDelete(db, pChanges);
106348 sqlite3ExprDelete(db, pWhere);
106349 return;
106350 }
@@ -107563,10 +107535,11 @@
107563
107564 if( pParse->pVdbe ){
107565 sqlite3VdbeFinalize(pParse->pVdbe);
107566 }
107567 sqlite3DeleteTable(db, pParse->pNewTable);
 
107568 sqlite3StackFree(db, pParse);
107569 }
107570
107571 assert( (rc&0xff)==rc );
107572 rc = sqlite3ApiExit(db, rc);
@@ -109055,13 +109028,10 @@
109055 }
109056 assert( pLeft->iColumn!=(-1) ); /* Because IPK never has AFF_TEXT */
109057
109058 pRight = pList->a[0].pExpr;
109059 op = pRight->op;
109060 if( op==TK_REGISTER ){
109061 op = pRight->op2;
109062 }
109063 if( op==TK_VARIABLE ){
109064 Vdbe *pReprepare = pParse->pReprepare;
109065 int iCol = pRight->iColumn;
109066 pVal = sqlite3VdbeGetBoundValue(pReprepare, iCol, SQLITE_AFF_NONE);
109067 if( pVal && sqlite3_value_type(pVal)==SQLITE_TEXT ){
@@ -111145,11 +111115,11 @@
111145 iCur = pTabItem->iCursor;
111146 pLevel->notReady = notReady & ~getMask(&pWInfo->sMaskSet, iCur);
111147 bRev = (pWInfo->revMask>>iLevel)&1;
111148 omitTable = (pLoop->wsFlags & WHERE_IDX_ONLY)!=0
111149 && (pWInfo->wctrlFlags & WHERE_FORCE_TABLE)==0;
111150 VdbeNoopComment((v, "Begin WHERE-loop%d: %s",iLevel,pTabItem->pTab->zName));
111151
111152 /* Create labels for the "break" and "continue" instructions
111153 ** for the current loop. Jump to addrBrk to break out of a loop.
111154 ** Jump to cont to go immediately to the next iteration of the
111155 ** loop.
@@ -111387,11 +111357,11 @@
111387 Index *pIdx; /* The index we will be using */
111388 int iIdxCur; /* The VDBE cursor for the index */
111389 int nExtraReg = 0; /* Number of extra registers needed */
111390 int op; /* Instruction opcode */
111391 char *zStartAff; /* Affinity for start of range constraint */
111392 char *zEndAff; /* Affinity for end of range constraint */
111393
111394 pIdx = pLoop->u.btree.pIndex;
111395 iIdxCur = pLevel->iIdxCur;
111396 assert( nEq>=pLoop->u.btree.nSkip );
111397
@@ -111428,11 +111398,12 @@
111428 /* Generate code to evaluate all constraint terms using == or IN
111429 ** and store the values of those terms in an array of registers
111430 ** starting at regBase.
111431 */
111432 regBase = codeAllEqualityTerms(pParse,pLevel,bRev,nExtraReg,&zStartAff);
111433 zEndAff = sqlite3DbStrDup(db, zStartAff);
 
111434 addrNxt = pLevel->addrNxt;
111435
111436 /* If we are doing a reverse order scan on an ascending index, or
111437 ** a forward order scan on a descending index, interchange the
111438 ** start and end terms (pRangeStart and pRangeEnd).
@@ -111498,27 +111469,19 @@
111498 sqlite3ExprCacheRemove(pParse, regBase+nEq, 1);
111499 sqlite3ExprCode(pParse, pRight, regBase+nEq);
111500 if( (pRangeEnd->wtFlags & TERM_VNULL)==0 ){
111501 sqlite3ExprCodeIsNullJump(v, pRight, regBase+nEq, addrNxt);
111502 }
111503 if( zEndAff ){
111504 if( sqlite3CompareAffinity(pRight, zEndAff[nEq])==SQLITE_AFF_NONE){
111505 /* Since the comparison is to be performed with no conversions
111506 ** applied to the operands, set the affinity to apply to pRight to
111507 ** SQLITE_AFF_NONE. */
111508 zEndAff[nEq] = SQLITE_AFF_NONE;
111509 }
111510 if( sqlite3ExprNeedsNoAffinityChange(pRight, zEndAff[nEq]) ){
111511 zEndAff[nEq] = SQLITE_AFF_NONE;
111512 }
111513 }
111514 codeApplyAffinity(pParse, regBase, nEq+1, zEndAff);
111515 nConstraint++;
111516 testcase( pRangeEnd->wtFlags & TERM_VIRTUAL );
111517 }
111518 sqlite3DbFree(db, zStartAff);
111519 sqlite3DbFree(db, zEndAff);
111520
111521 /* Top of the loop body */
111522 pLevel->p2 = sqlite3VdbeCurrentAddr(v);
111523
111524 /* Check if the index cursor is past the end of the range. */
@@ -111539,10 +111502,11 @@
111539 testcase( pLoop->wsFlags & WHERE_BTM_LIMIT );
111540 testcase( pLoop->wsFlags & WHERE_TOP_LIMIT );
111541 if( (pLoop->wsFlags & (WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0
111542 && (j = pIdx->aiColumn[nEq])>=0
111543 && pIdx->pTable->aCol[j].notNull==0
 
111544 ){
111545 sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, nEq, r1);
111546 VdbeComment((v, "%s", pIdx->pTable->aCol[j].zName));
111547 sqlite3VdbeAddOp2(v, OP_IsNull, r1, addrCont);
111548 }
@@ -111856,11 +111820,11 @@
111856 pAlt = findTerm(pWC, iCur, pTerm->u.leftColumn, notReady, WO_EQ|WO_IN, 0);
111857 if( pAlt==0 ) continue;
111858 if( pAlt->wtFlags & (TERM_CODED) ) continue;
111859 testcase( pAlt->eOperator & WO_EQ );
111860 testcase( pAlt->eOperator & WO_IN );
111861 VdbeNoopComment((v, "begin transitive constraint"));
111862 pEAlt = sqlite3StackAllocRaw(db, sizeof(*pEAlt));
111863 if( pEAlt ){
111864 *pEAlt = *pAlt->pExpr;
111865 pEAlt->pLeft = pE->pLeft;
111866 sqlite3ExprIfFalse(pParse, pEAlt, addrCont, SQLITE_JUMPIFNULL);
@@ -112313,14 +112277,19 @@
112313 saved_wsFlags = pNew->wsFlags;
112314 saved_prereq = pNew->prereq;
112315 saved_nOut = pNew->nOut;
112316 pNew->rSetup = 0;
112317 rLogSize = estLog(sqlite3LogEst(pProbe->aiRowEst[0]));
 
 
 
 
 
112318 if( pTerm==0
112319 && saved_nEq==saved_nSkip
112320 && saved_nEq+1<pProbe->nKeyCol
112321 && pProbe->aiRowEst[saved_nEq+1]>50
112322 ){
112323 LogEst nIter;
112324 pNew->u.btree.nEq++;
112325 pNew->u.btree.nSkip++;
112326 pNew->aLTerm[pNew->nLTerm++] = 0;
@@ -113812,11 +113781,10 @@
113812 /* Split the WHERE clause into separate subexpressions where each
113813 ** subexpression is separated by an AND operator.
113814 */
113815 initMaskSet(pMaskSet);
113816 whereClauseInit(&pWInfo->sWC, pWInfo);
113817 sqlite3ExprCodeConstants(pParse, pWhere);
113818 whereSplit(&pWInfo->sWC, pWhere, TK_AND);
113819 sqlite3CodeVerifySchema(pParse, -1); /* Insert the cookie verifier Goto */
113820
113821 /* Special case: a WHERE clause that is constant. Evaluate the
113822 ** expression and either jump over all of the code or fall thru.
@@ -114127,11 +114095,11 @@
114127 notReady = codeOneLoopStart(pWInfo, ii, notReady);
114128 pWInfo->iContinue = pLevel->addrCont;
114129 }
114130
114131 /* Done. */
114132 VdbeNoopComment((v, "Begin WHERE-core"));
114133 return pWInfo;
114134
114135 /* Jump here if malloc fails */
114136 whereBeginError:
114137 if( pWInfo ){
@@ -114154,11 +114122,11 @@
114154 SrcList *pTabList = pWInfo->pTabList;
114155 sqlite3 *db = pParse->db;
114156
114157 /* Generate loop termination code.
114158 */
114159 VdbeNoopComment((v, "End WHERE-core"));
114160 sqlite3ExprCacheClear(pParse);
114161 for(i=pWInfo->nLevel-1; i>=0; i--){
114162 int addr;
114163 pLevel = &pWInfo->a[i];
114164 pLoop = pLevel->pWLoop;
@@ -114200,11 +114168,11 @@
114200 }else{
114201 sqlite3VdbeAddOp2(v, OP_Goto, 0, pLevel->addrFirst);
114202 }
114203 sqlite3VdbeJumpHere(v, addr);
114204 }
114205 VdbeNoopComment((v, "End WHERE-loop%d: %s", i,
114206 pWInfo->pTabList->a[pLevel->iFrom].pTab->zName));
114207 }
114208
114209 /* The "break" point is here, just past the end of the outer loop.
114210 ** Set it.
@@ -123900,10 +123868,13 @@
123900 }
123901
123902 #define GETVARINT_STEP(v, ptr, shift, mask1, mask2, var, ret) \
123903 v = (v & mask1) | ( (*ptr++) << shift ); \
123904 if( (v & mask2)==0 ){ var = v; return ret; }
 
 
 
123905
123906 /*
123907 ** Read a 64-bit variable-length integer from memory starting at p[0].
123908 ** Return the number of bytes read, or 0 on error.
123909 ** The value is stored in *v.
@@ -123912,11 +123883,11 @@
123912 const char *pStart = p;
123913 u32 a;
123914 u64 b;
123915 int shift;
123916
123917 GETVARINT_STEP(a, p, 0, 0x00, 0x80, *v, 1);
123918 GETVARINT_STEP(a, p, 7, 0x7F, 0x4000, *v, 2);
123919 GETVARINT_STEP(a, p, 14, 0x3FFF, 0x200000, *v, 3);
123920 GETVARINT_STEP(a, p, 21, 0x1FFFFF, 0x10000000, *v, 4);
123921 b = (a & 0x0FFFFFFF );
123922
@@ -123924,11 +123895,11 @@
123924 u64 c = *p++;
123925 b += (c&0x7F) << shift;
123926 if( (c & 0x80)==0 ) break;
123927 }
123928 *v = b;
123929 return p - pStart;
123930 }
123931
123932 /*
123933 ** Similar to sqlite3Fts3GetVarint(), except that the output is truncated to a
123934 ** 32-bit integer before it is returned.
@@ -123935,11 +123906,11 @@
123935 */
123936 SQLITE_PRIVATE int sqlite3Fts3GetVarint32(const char *p, int *pi){
123937 u32 a;
123938
123939 #ifndef fts3GetVarint32
123940 GETVARINT_STEP(a, p, 0, 0x00, 0x80, *pi, 1);
123941 #else
123942 a = (*p++);
123943 assert( a & 0x80 );
123944 #endif
123945
123946
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -135,11 +135,11 @@
135 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
136 ** [sqlite_version()] and [sqlite_source_id()].
137 */
138 #define SQLITE_VERSION "3.8.2"
139 #define SQLITE_VERSION_NUMBER 3008002
140 #define SQLITE_SOURCE_ID "2013-11-19 13:55:34 17e8524fc05aa1e6074c19a8ccccc5ab5883103a"
141
142 /*
143 ** CAPI3REF: Run-Time Library Version Numbers
144 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
145 **
@@ -396,11 +396,11 @@
396 ** Restrictions:
397 **
398 ** <ul>
399 ** <li> The application must insure that the 1st parameter to sqlite3_exec()
400 ** is a valid and open [database connection].
401 ** <li> The application must not close the [database connection] specified by
402 ** the 1st parameter to sqlite3_exec() while sqlite3_exec() is running.
403 ** <li> The application must not modify the SQL statement text passed into
404 ** the 2nd parameter of sqlite3_exec() while sqlite3_exec() is running.
405 ** </ul>
406 */
@@ -473,11 +473,11 @@
473 ** about errors. The extended result codes are enabled or disabled
474 ** on a per database connection basis using the
475 ** [sqlite3_extended_result_codes()] API.
476 **
477 ** Some of the available extended result codes are listed here.
478 ** One may expect the number of extended result codes will increase
479 ** over time. Software that uses extended result codes should expect
480 ** to see new result codes in future releases of SQLite.
481 **
482 ** The SQLITE_OK result code will never be extended. It will always
483 ** be exactly zero.
@@ -1411,11 +1411,11 @@
1411 ** of 8. Some allocators round up to a larger multiple or to a power of 2.
1412 ** Every memory allocation request coming in through [sqlite3_malloc()]
1413 ** or [sqlite3_realloc()] first calls xRoundup. If xRoundup returns 0,
1414 ** that causes the corresponding memory allocation to fail.
1415 **
1416 ** The xInit method initializes the memory allocator. For example,
1417 ** it might allocate any require mutexes or initialize internal data
1418 ** structures. The xShutdown method is invoked (indirectly) by
1419 ** [sqlite3_shutdown()] and should deallocate any resources acquired
1420 ** by xInit. The pAppData pointer is used as the only parameter to
1421 ** xInit and xShutdown.
@@ -3137,11 +3137,10 @@
3137 ** to the [sqlite3_bind_text | bindings] of that [parameter].
3138 ** ^The specific value of WHERE-clause [parameter] might influence the
3139 ** choice of query plan if the parameter is the left-hand side of a [LIKE]
3140 ** or [GLOB] operator or if the parameter is compared to an indexed column
3141 ** and the [SQLITE_ENABLE_STAT3] compile-time option is enabled.
 
3142 ** </li>
3143 ** </ol>
3144 */
3145 SQLITE_API int sqlite3_prepare(
3146 sqlite3 *db, /* Database handle */
@@ -3867,11 +3866,11 @@
3866 **
3867 ** ^The pointers returned are valid until a type conversion occurs as
3868 ** described above, or until [sqlite3_step()] or [sqlite3_reset()] or
3869 ** [sqlite3_finalize()] is called. ^The memory space used to hold strings
3870 ** and BLOBs is freed automatically. Do <b>not</b> pass the pointers returned
3871 ** from [sqlite3_column_blob()], [sqlite3_column_text()], etc. into
3872 ** [sqlite3_free()].
3873 **
3874 ** ^(If a memory allocation error occurs during the evaluation of any
3875 ** of these routines, a default value is returned. The default value
3876 ** is either the integer 0, the floating point number 0.0, or a NULL
@@ -4945,12 +4944,12 @@
4944 /*
4945 ** CAPI3REF: Free Memory Used By A Database Connection
4946 **
4947 ** ^The sqlite3_db_release_memory(D) interface attempts to free as much heap
4948 ** memory as possible from database connection D. Unlike the
4949 ** [sqlite3_release_memory()] interface, this interface is in effect even
4950 ** when the [SQLITE_ENABLE_MEMORY_MANAGEMENT] compile-time option is
4951 ** omitted.
4952 **
4953 ** See also: [sqlite3_release_memory()]
4954 */
4955 SQLITE_API int sqlite3_db_release_memory(sqlite3*);
@@ -9096,22 +9095,22 @@
9095 #define OP_NotNull 75 /* same as TK_NOTNULL, synopsis: if r[P1]!=NULL goto P2 */
9096 #define OP_Ne 76 /* same as TK_NE, synopsis: if r[P1]!=r[P3] goto P2 */
9097 #define OP_Eq 77 /* same as TK_EQ, synopsis: if r[P1]==r[P3] goto P2 */
9098 #define OP_Gt 78 /* same as TK_GT, synopsis: if r[P1]>r[P3] goto P2 */
9099 #define OP_Le 79 /* same as TK_LE, synopsis: if r[P1]<=r[P3] goto P2 */
9100 #define OP_Lt 80 /* same as TK_LT, synopsis: if r[P1]<r[P3] goto P2 */
9101 #define OP_Ge 81 /* same as TK_GE, synopsis: if r[P1]>=r[P3] goto P2 */
9102 #define OP_RowKey 82 /* synopsis: r[P2]=key */
9103 #define OP_BitAnd 83 /* same as TK_BITAND, synopsis: r[P3]=r[P1]&r[P2] */
9104 #define OP_BitOr 84 /* same as TK_BITOR, synopsis: r[P3]=r[P1]|r[P2] */
9105 #define OP_ShiftLeft 85 /* same as TK_LSHIFT, synopsis: r[P3]=r[P2]<<r[P1] */
9106 #define OP_ShiftRight 86 /* same as TK_RSHIFT, synopsis: r[P3]=r[P2]>>r[P1] */
9107 #define OP_Add 87 /* same as TK_PLUS, synopsis: r[P3]=r[P1]+r[P2] */
9108 #define OP_Subtract 88 /* same as TK_MINUS, synopsis: r[P3]=r[P2]-r[P1] */
9109 #define OP_Multiply 89 /* same as TK_STAR, synopsis: r[P3]=r[P1]*r[P2] */
9110 #define OP_Divide 90 /* same as TK_SLASH, synopsis: r[P3]=r[P2]/r[P1] */
9111 #define OP_Remainder 91 /* same as TK_REM, synopsis: r[P3]=r[P2]%r[P1] */
9112 #define OP_Concat 92 /* same as TK_CONCAT, synopsis: r[P3]=r[P2]+r[P1] */
9113 #define OP_RowData 93 /* synopsis: r[P2]=data */
9114 #define OP_BitNot 94 /* same as TK_BITNOT, synopsis: r[P1]= ~r[P1] */
9115 #define OP_String8 95 /* same as TK_STRING, synopsis: r[P2]='P4' */
9116 #define OP_Rowid 96 /* synopsis: r[P2]=rowid */
@@ -11089,11 +11088,11 @@
11088 #define EP_Distinct 0x000010 /* Aggregate function with DISTINCT keyword */
11089 #define EP_VarSelect 0x000020 /* pSelect is correlated, not constant */
11090 #define EP_DblQuoted 0x000040 /* token.z was originally in "..." */
11091 #define EP_InfixFunc 0x000080 /* True for an infix function: LIKE, GLOB, etc */
11092 #define EP_Collate 0x000100 /* Tree contains a TK_COLLATE opeartor */
11093 /* unused 0x000200 */
11094 #define EP_IntValue 0x000400 /* Integer value contained in u.iValue */
11095 #define EP_xIsSelect 0x000800 /* x.pSelect is valid (otherwise x.pList is) */
11096 #define EP_Skip 0x001000 /* COLLATE, AS, or UNLIKELY */
11097 #define EP_Reduced 0x002000 /* Expr struct EXPR_REDUCEDSIZE bytes only */
11098 #define EP_TokenOnly 0x004000 /* Expr struct EXPR_TOKENONLYSIZE bytes only */
@@ -11160,12 +11159,17 @@
11159 char *zName; /* Token associated with this expression */
11160 char *zSpan; /* Original text of the expression */
11161 u8 sortOrder; /* 1 for DESC or 0 for ASC */
11162 unsigned done :1; /* A flag to indicate when processing is finished */
11163 unsigned bSpanIsTab :1; /* zSpan holds DB.TABLE.COLUMN */
11164 union {
11165 struct {
11166 u16 iOrderByCol; /* For ORDER BY, column number in result set */
11167 u16 iAlias; /* Index into Parse.aAlias[] for zName */
11168 } x;
11169 int iConstExprReg; /* Register in which Expr value is cached */
11170 } u;
11171 } *a; /* Alloc a power of two greater or equal to nExpr */
11172 };
11173
11174 /*
11175 ** An instance of this structure is used by the parser to record both
@@ -11538,10 +11542,11 @@
11542 u8 tempReg; /* iReg is a temp register that needs to be freed */
11543 int iLevel; /* Nesting level */
11544 int iReg; /* Reg with value of this column. 0 means none. */
11545 int lru; /* Least recently used entry has the smallest value */
11546 } aColCache[SQLITE_N_COLCACHE]; /* One for each column cache entry */
11547 ExprList *pConstExpr;/* Constant expressions */
11548 yDbMask writeMask; /* Start a write transaction on these databases */
11549 yDbMask cookieMask; /* Bitmask of schema verified databases */
11550 int cookieGoto; /* Address of OP_Goto to cookie verifier subroutine */
11551 int cookieValue[SQLITE_MAX_ATTACHED+2]; /* Values of cookies to verify */
11552 int regRowid; /* Register holding rowid of CREATE TABLE entry */
@@ -12151,11 +12156,10 @@
12156 SQLITE_PRIVATE void sqlite3ExprCacheAffinityChange(Parse*, int, int);
12157 SQLITE_PRIVATE int sqlite3ExprCode(Parse*, Expr*, int);
12158 SQLITE_PRIVATE int sqlite3ExprCodeTemp(Parse*, Expr*, int*);
12159 SQLITE_PRIVATE int sqlite3ExprCodeTarget(Parse*, Expr*, int);
12160 SQLITE_PRIVATE int sqlite3ExprCodeAndCache(Parse*, Expr*, int);
 
12161 SQLITE_PRIVATE int sqlite3ExprCodeExprList(Parse*, ExprList*, int, int);
12162 SQLITE_PRIVATE void sqlite3ExprIfTrue(Parse*, Expr*, int, int);
12163 SQLITE_PRIVATE void sqlite3ExprIfFalse(Parse*, Expr*, int, int);
12164 SQLITE_PRIVATE Table *sqlite3FindTable(sqlite3*,const char*, const char*);
12165 SQLITE_PRIVATE Table *sqlite3LocateTable(Parse*,int isView,const char*, const char*);
@@ -12197,11 +12201,11 @@
12201 SQLITE_PRIVATE void sqlite3GenerateRowIndexDelete(Parse*, Table*, int, int, int*);
12202 SQLITE_PRIVATE int sqlite3GenerateIndexKey(Parse*, Index*, int, int, int, int*);
12203 SQLITE_PRIVATE void sqlite3GenerateConstraintChecks(Parse*,Table*,int*,int,int,int,int,
12204 u8,u8,int,int*);
12205 SQLITE_PRIVATE void sqlite3CompleteInsertion(Parse*,Table*,int,int,int,int*,int,int,int);
12206 SQLITE_PRIVATE int sqlite3OpenTableAndIndices(Parse*, Table*, int, int, u8*, int*, int*);
12207 SQLITE_PRIVATE void sqlite3BeginWriteOperation(Parse*, int, int);
12208 SQLITE_PRIVATE void sqlite3MultiWrite(Parse*);
12209 SQLITE_PRIVATE void sqlite3MayAbort(Parse*);
12210 SQLITE_PRIVATE void sqlite3HaltConstraint(Parse*, int, int, char*, i8, u8);
12211 SQLITE_PRIVATE void sqlite3UniqueConstraint(Parse*, int, Index*);
@@ -12519,10 +12523,11 @@
12523 SQLITE_PRIVATE FuncDef *sqlite3VtabOverloadFunction(sqlite3 *,FuncDef*, int nArg, Expr*);
12524 SQLITE_PRIVATE void sqlite3InvalidFunction(sqlite3_context*,int,sqlite3_value**);
12525 SQLITE_PRIVATE sqlite3_int64 sqlite3StmtCurrentTime(sqlite3_context*);
12526 SQLITE_PRIVATE int sqlite3VdbeParameterIndex(Vdbe*, const char*, int);
12527 SQLITE_PRIVATE int sqlite3TransferBindings(sqlite3_stmt *, sqlite3_stmt *);
12528 SQLITE_PRIVATE void sqlite3ParserReset(Parse*);
12529 SQLITE_PRIVATE int sqlite3Reprepare(Vdbe*);
12530 SQLITE_PRIVATE void sqlite3ExprListCheckLength(Parse*, ExprList*, const char*);
12531 SQLITE_PRIVATE CollSeq *sqlite3BinaryCompareCollSeq(Parse *, Expr *, Expr *);
12532 SQLITE_PRIVATE int sqlite3TempInMemory(const sqlite3*);
12533 SQLITE_PRIVATE const char *sqlite3JournalModename(int);
@@ -22970,22 +22975,22 @@
22975 /* 75 */ "NotNull" OpHelp("if r[P1]!=NULL goto P2"),
22976 /* 76 */ "Ne" OpHelp("if r[P1]!=r[P3] goto P2"),
22977 /* 77 */ "Eq" OpHelp("if r[P1]==r[P3] goto P2"),
22978 /* 78 */ "Gt" OpHelp("if r[P1]>r[P3] goto P2"),
22979 /* 79 */ "Le" OpHelp("if r[P1]<=r[P3] goto P2"),
22980 /* 80 */ "Lt" OpHelp("if r[P1]<r[P3] goto P2"),
22981 /* 81 */ "Ge" OpHelp("if r[P1]>=r[P3] goto P2"),
22982 /* 82 */ "RowKey" OpHelp("r[P2]=key"),
22983 /* 83 */ "BitAnd" OpHelp("r[P3]=r[P1]&r[P2]"),
22984 /* 84 */ "BitOr" OpHelp("r[P3]=r[P1]|r[P2]"),
22985 /* 85 */ "ShiftLeft" OpHelp("r[P3]=r[P2]<<r[P1]"),
22986 /* 86 */ "ShiftRight" OpHelp("r[P3]=r[P2]>>r[P1]"),
22987 /* 87 */ "Add" OpHelp("r[P3]=r[P1]+r[P2]"),
22988 /* 88 */ "Subtract" OpHelp("r[P3]=r[P2]-r[P1]"),
22989 /* 89 */ "Multiply" OpHelp("r[P3]=r[P1]*r[P2]"),
22990 /* 90 */ "Divide" OpHelp("r[P3]=r[P2]/r[P1]"),
22991 /* 91 */ "Remainder" OpHelp("r[P3]=r[P2]%r[P1]"),
22992 /* 92 */ "Concat" OpHelp("r[P3]=r[P2]+r[P1]"),
22993 /* 93 */ "RowData" OpHelp("r[P2]=data"),
22994 /* 94 */ "BitNot" OpHelp("r[P1]= ~r[P1]"),
22995 /* 95 */ "String8" OpHelp("r[P2]='P4'"),
22996 /* 96 */ "Rowid" OpHelp("r[P2]=rowid"),
@@ -58866,10 +58871,11 @@
58871 if( sqlite3OpenTempDatabase(pParse) ){
58872 sqlite3Error(pErrorDb, pParse->rc, "%s", pParse->zErrMsg);
58873 rc = SQLITE_ERROR;
58874 }
58875 sqlite3DbFree(pErrorDb, pParse->zErrMsg);
58876 sqlite3ParserReset(pParse);
58877 sqlite3StackFree(pErrorDb, pParse);
58878 }
58879 if( rc ){
58880 return 0;
58881 }
@@ -63898,19 +63904,16 @@
63904 pMem->u.i = serial_type-8;
63905 pMem->flags = MEM_Int;
63906 return 0;
63907 }
63908 default: {
63909 static const u16 aFlag[] = { MEM_Blob|MEM_Ephem, MEM_Str|MEM_Ephem };
63910 u32 len = (serial_type-12)/2;
63911 pMem->z = (char *)buf;
63912 pMem->n = len;
63913 pMem->xDel = 0;
63914 pMem->flags = aFlag[serial_type&1];
 
 
 
 
63915 return len;
63916 }
63917 }
63918 return 0;
63919 }
@@ -67793,23 +67796,23 @@
67796 ** Subtract the value in register P1 from the value in register P2
67797 ** and store the result in register P3.
67798 ** If either input is NULL, the result is NULL.
67799 */
67800 /* Opcode: Divide P1 P2 P3 * *
67801 ** Synopsis: r[P3]=r[P2]/r[P1]
67802 **
67803 ** Divide the value in register P1 by the value in register P2
67804 ** and store the result in register P3 (P3=P2/P1). If the value in
67805 ** register P1 is zero, then the result is NULL. If either input is
67806 ** NULL, the result is NULL.
67807 */
67808 /* Opcode: Remainder P1 P2 P3 * *
67809 ** Synopsis: r[P3]=r[P2]%r[P1]
67810 **
67811 ** Compute the remainder after integer register P2 is divided by
67812 ** register P1 and store the result in register P3.
67813 ** If the value in register P1 is zero the result is NULL.
67814 ** If either operand is NULL, the result is NULL.
67815 */
67816 case OP_Add: /* same as TK_PLUS, in1, in2, out3 */
67817 case OP_Subtract: /* same as TK_MINUS, in1, in2, out3 */
67818 case OP_Multiply: /* same as TK_STAR, in1, in2, out3 */
@@ -68274,11 +68277,11 @@
68277 break;
68278 }
68279 #endif /* !defined(SQLITE_OMIT_CAST) && !defined(SQLITE_OMIT_FLOATING_POINT) */
68280
68281 /* Opcode: Lt P1 P2 P3 P4 P5
68282 ** Synopsis: if r[P1]<r[P3] goto P2
68283 **
68284 ** Compare the values in register P1 and P3. If reg(P3)<reg(P1) then
68285 ** jump to address P2.
68286 **
68287 ** If the SQLITE_JUMPIFNULL bit of P5 is set and either reg(P1) or
@@ -73278,10 +73281,11 @@
73281 if( pBlob && pBlob->pStmt ) sqlite3VdbeFinalize((Vdbe *)pBlob->pStmt);
73282 sqlite3DbFree(db, pBlob);
73283 }
73284 sqlite3Error(db, rc, (zErr ? "%s" : 0), zErr);
73285 sqlite3DbFree(db, zErr);
73286 sqlite3ParserReset(pParse);
73287 sqlite3StackFree(db, pParse);
73288 rc = sqlite3ApiExit(db, rc);
73289 sqlite3_mutex_leave(db->mutex);
73290 return rc;
73291 }
@@ -75243,14 +75247,14 @@
75247 if( pOrig->op!=TK_COLUMN && zType[0]!='G' ){
75248 incrAggFunctionDepth(pDup, nSubquery);
75249 pDup = sqlite3PExpr(pParse, TK_AS, pDup, 0, 0);
75250 if( pDup==0 ) return;
75251 ExprSetProperty(pDup, EP_Skip);
75252 if( pEList->a[iCol].u.x.iAlias==0 ){
75253 pEList->a[iCol].u.x.iAlias = (u16)(++pParse->nAlias);
75254 }
75255 pDup->iTable = pEList->a[iCol].u.x.iAlias;
75256 }
75257 if( pExpr->op==TK_COLLATE ){
75258 pDup = sqlite3ExprAddCollateString(pParse, pDup, pExpr->u.zToken);
75259 }
75260
@@ -76111,11 +76115,11 @@
76115 assert( pItem->pExpr->op==TK_COLLATE );
76116 assert( pItem->pExpr->pLeft==pE );
76117 pItem->pExpr->pLeft = pNew;
76118 }
76119 sqlite3ExprDelete(db, pE);
76120 pItem->u.x.iOrderByCol = (u16)iCol;
76121 pItem->done = 1;
76122 }else{
76123 moreToDo = 1;
76124 }
76125 }
@@ -76132,12 +76136,12 @@
76136 }
76137
76138 /*
76139 ** Check every term in the ORDER BY or GROUP BY clause pOrderBy of
76140 ** the SELECT statement pSelect. If any term is reference to a
76141 ** result set expression (as determined by the ExprList.a.u.x.iOrderByCol
76142 ** field) then convert that term into a copy of the corresponding result set
76143 ** column.
76144 **
76145 ** If any errors are detected, add an error message to pParse and
76146 ** return non-zero. Return zero if no errors are seen.
76147 */
@@ -76160,16 +76164,16 @@
76164 }
76165 #endif
76166 pEList = pSelect->pEList;
76167 assert( pEList!=0 ); /* sqlite3SelectNew() guarantees this */
76168 for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
76169 if( pItem->u.x.iOrderByCol ){
76170 if( pItem->u.x.iOrderByCol>pEList->nExpr ){
76171 resolveOutOfRangeError(pParse, zType, i+1, pEList->nExpr);
76172 return 1;
76173 }
76174 resolveAlias(pParse, pEList, pItem->u.x.iOrderByCol-1, pItem->pExpr, zType,0);
76175 }
76176 }
76177 return 0;
76178 }
76179
@@ -76214,11 +76218,11 @@
76218 if( iCol>0 ){
76219 /* If an AS-name match is found, mark this ORDER BY column as being
76220 ** a copy of the iCol-th result-set column. The subsequent call to
76221 ** sqlite3ResolveOrderGroupBy() will convert the expression to a
76222 ** copy of the iCol-th result-set expression. */
76223 pItem->u.x.iOrderByCol = (u16)iCol;
76224 continue;
76225 }
76226 }
76227 if( sqlite3ExprIsInteger(pE2, &iCol) ){
76228 /* The ORDER BY term is an integer constant. Again, set the column
@@ -76226,22 +76230,22 @@
76230 ** order-by term to a copy of the result-set expression */
76231 if( iCol<1 || iCol>0xffff ){
76232 resolveOutOfRangeError(pParse, zType, i+1, nResult);
76233 return 1;
76234 }
76235 pItem->u.x.iOrderByCol = (u16)iCol;
76236 continue;
76237 }
76238
76239 /* Otherwise, treat the ORDER BY term as an ordinary expression */
76240 pItem->u.x.iOrderByCol = 0;
76241 if( sqlite3ResolveExprNames(pNC, pE) ){
76242 return 1;
76243 }
76244 for(j=0; j<pSelect->pEList->nExpr; j++){
76245 if( sqlite3ExprCompare(pE, pSelect->pEList->a[j].pExpr, -1)==0 ){
76246 pItem->u.x.iOrderByCol = j+1;
76247 }
76248 }
76249 }
76250 return sqlite3ResolveOrderGroupBy(pParse, pSelect, pOrderBy, zType);
76251 }
@@ -77516,12 +77520,11 @@
77520 pItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
77521 pItem->zSpan = sqlite3DbStrDup(db, pOldItem->zSpan);
77522 pItem->sortOrder = pOldItem->sortOrder;
77523 pItem->done = 0;
77524 pItem->bSpanIsTab = pOldItem->bSpanIsTab;
77525 pItem->u = pOldItem->u;
 
77526 }
77527 return pNew;
77528 }
77529
77530 /*
@@ -78942,10 +78945,11 @@
78945 int inReg = target; /* Results stored in register inReg */
78946 int regFree1 = 0; /* If non-zero free this temporary register */
78947 int regFree2 = 0; /* If non-zero free this temporary register */
78948 int r1, r2, r3, r4; /* Various register numbers */
78949 sqlite3 *db = pParse->db; /* The database connection */
78950 Expr tempX; /* Temporary expression node */
78951
78952 assert( target>0 && target<=pParse->nMem );
78953 if( v==0 ){
78954 assert( pParse->db->mallocFailed );
78955 return 0;
@@ -79161,12 +79165,14 @@
79165 }else if( pLeft->op==TK_FLOAT ){
79166 assert( !ExprHasProperty(pExpr, EP_IntValue) );
79167 codeReal(v, pLeft->u.zToken, 1, target);
79168 #endif
79169 }else{
79170 tempX.op = TK_INTEGER;
79171 tempX.flags = EP_IntValue|EP_TokenOnly;
79172 tempX.u.iValue = 0;
79173 r1 = sqlite3ExprCodeTemp(pParse, &tempX, &regFree1);
79174 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree2);
79175 sqlite3VdbeAddOp3(v, OP_Subtract, r2, r1, target);
79176 testcase( regFree2==0 );
79177 }
79178 inReg = target;
@@ -79478,11 +79484,10 @@
79484 int nExpr; /* 2x number of WHEN terms */
79485 int i; /* Loop counter */
79486 ExprList *pEList; /* List of WHEN terms */
79487 struct ExprList_item *aListelem; /* Array of WHEN terms */
79488 Expr opCompare; /* The X==Ei expression */
 
79489 Expr *pX; /* The X expression */
79490 Expr *pTest = 0; /* X==Ei (form A) or just Ei (form B) */
79491 VVA_ONLY( int iCacheLevel = pParse->iCacheLevel; )
79492
79493 assert( !ExprHasProperty(pExpr, EP_xIsSelect) && pExpr->x.pList );
@@ -79490,17 +79495,16 @@
79495 pEList = pExpr->x.pList;
79496 aListelem = pEList->a;
79497 nExpr = pEList->nExpr;
79498 endLabel = sqlite3VdbeMakeLabel(v);
79499 if( (pX = pExpr->pLeft)!=0 ){
79500 tempX = *pX;
79501 testcase( pX->op==TK_COLUMN );
79502 exprToRegister(&tempX, sqlite3ExprCodeTemp(pParse, pX, &regFree1));
 
79503 testcase( regFree1==0 );
79504 opCompare.op = TK_EQ;
79505 opCompare.pLeft = &tempX;
79506 pTest = &opCompare;
79507 /* Ticket b351d95f9cd5ef17e9d9dbae18f5ca8611190001:
79508 ** The value in regFree1 might get SCopy-ed into the file result.
79509 ** So make sure that the regFree1 register is not reused for other
79510 ** purposes and possibly overwritten. */
@@ -79516,11 +79520,10 @@
79520 }
79521 nextCase = sqlite3VdbeMakeLabel(v);
79522 testcase( pTest->op==TK_COLUMN );
79523 sqlite3ExprIfFalse(pParse, pTest, nextCase, SQLITE_JUMPIFNULL);
79524 testcase( aListelem[i+1].pExpr->op==TK_COLUMN );
 
79525 sqlite3ExprCode(pParse, aListelem[i+1].pExpr, target);
79526 sqlite3VdbeAddOp2(v, OP_Goto, 0, endLabel);
79527 sqlite3ExprCachePop(pParse, 1);
79528 sqlite3VdbeResolveLabel(v, nextCase);
79529 }
@@ -79575,19 +79578,45 @@
79578 ** are stored.
79579 **
79580 ** If the register is a temporary register that can be deallocated,
79581 ** then write its number into *pReg. If the result register is not
79582 ** a temporary, then set *pReg to zero.
79583 **
79584 ** If pExpr is a constant, then this routine might generate this
79585 ** code to fill the register in the initialization section of the
79586 ** VDBE program, in order to factor it out of the evaluation loop.
79587 */
79588 SQLITE_PRIVATE int sqlite3ExprCodeTemp(Parse *pParse, Expr *pExpr, int *pReg){
79589 int r2;
79590 pExpr = sqlite3ExprSkipCollate(pExpr);
79591 if( pParse->cookieGoto>0
79592 && pExpr->op!=TK_REGISTER
79593 && sqlite3ExprIsConstantNotJoin(pExpr)
79594 ){
79595 ExprList *p = pParse->pConstExpr;
79596 int i;
79597 *pReg = 0;
79598 if( p ){
79599 for(i=0; i<p->nExpr; i++){
79600 if( sqlite3ExprCompare(p->a[i].pExpr, pExpr, -1)==0 ){
79601 return p->a[i].u.iConstExprReg;
79602 }
79603 }
79604 }
79605 p = sqlite3ExprListAppend(pParse, p, sqlite3ExprDup(pParse->db, pExpr, 0));
79606 pParse->pConstExpr = p;
79607 r2 = ++pParse->nMem;
79608 if( p ) p->a[p->nExpr-1].u.iConstExprReg = r2;
79609 }else{
79610 int r1 = sqlite3GetTempReg(pParse);
79611 r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1);
79612 if( r2==r1 ){
79613 *pReg = r1;
79614 }else{
79615 sqlite3ReleaseTempReg(pParse, r1);
79616 *pReg = 0;
79617 }
79618 }
79619 return r2;
79620 }
79621
79622 /*
@@ -79626,16 +79655,17 @@
79655 SQLITE_PRIVATE int sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr, int target){
79656 Vdbe *v = pParse->pVdbe;
79657 int inReg;
79658 inReg = sqlite3ExprCode(pParse, pExpr, target);
79659 assert( target>0 );
79660 /* The only place, other than this routine, where expressions can be
79661 ** converted to TK_REGISTER is internal subexpressions in BETWEEN and
79662 ** CASE operators. Neither ever calls this routine. And this routine
79663 ** is never called twice on the same expression. Hence it is impossible
79664 ** for the input to this routine to already be a register. Nevertheless,
79665 ** it seems prudent to keep the ALWAYS() in case the conditions above
79666 ** change with future modifications or enhancements. */
79667 if( ALWAYS(pExpr->op!=TK_REGISTER) ){
79668 int iMem;
79669 iMem = ++pParse->nMem;
79670 sqlite3VdbeAddOp2(v, OP_Copy, inReg, iMem);
79671 exprToRegister(pExpr, iMem);
@@ -79913,144 +79943,10 @@
79943 }
79944 sqlite3ExplainPop(pOut);
79945 }
79946 }
79947 #endif /* SQLITE_DEBUG */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79948
79949 /*
79950 ** Generate code that pushes the value of every element of the given
79951 ** expression list into a sequence of registers beginning at target.
79952 **
@@ -80425,44 +80321,46 @@
80321 ** this routine is used, it does not hurt to get an extra 2 - that
80322 ** just might result in some slightly slower code. But returning
80323 ** an incorrect 0 or 1 could lead to a malfunction.
80324 */
80325 SQLITE_PRIVATE int sqlite3ExprCompare(Expr *pA, Expr *pB, int iTab){
80326 u32 combinedFlags;
80327 if( pA==0 || pB==0 ){
80328 return pB==pA ? 0 : 2;
80329 }
80330 combinedFlags = pA->flags | pB->flags;
80331 if( combinedFlags & EP_IntValue ){
80332 if( (pA->flags&pB->flags&EP_IntValue)!=0 && pA->u.iValue==pB->u.iValue ){
80333 return 0;
80334 }
80335 return 2;
80336 }
80337 if( pA->op!=pB->op ){
 
80338 if( pA->op==TK_COLLATE && sqlite3ExprCompare(pA->pLeft, pB, iTab)<2 ){
80339 return 1;
80340 }
80341 if( pB->op==TK_COLLATE && sqlite3ExprCompare(pA, pB->pLeft, iTab)<2 ){
80342 return 1;
80343 }
80344 return 2;
80345 }
80346 if( pA->op!=TK_COLUMN && ALWAYS(pA->op!=TK_AGG_COLUMN) && pA->u.zToken ){
 
 
 
 
 
 
 
 
 
 
 
 
80347 if( strcmp(pA->u.zToken,pB->u.zToken)!=0 ){
80348 return pA->op==TK_COLLATE ? 1 : 2;
80349 }
80350 }
80351 if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 2;
80352 if( ALWAYS((combinedFlags & EP_TokenOnly)==0) ){
80353 if( combinedFlags & EP_xIsSelect ) return 2;
80354 if( sqlite3ExprCompare(pA->pLeft, pB->pLeft, iTab) ) return 2;
80355 if( sqlite3ExprCompare(pA->pRight, pB->pRight, iTab) ) return 2;
80356 if( sqlite3ExprListCompare(pA->x.pList, pB->x.pList, iTab) ) return 2;
80357 if( ALWAYS((combinedFlags & EP_Reduced)==0) ){
80358 if( pA->iColumn!=pB->iColumn ) return 2;
80359 if( pA->iTable!=pB->iTable
80360 && (pA->iTable!=iTab || NEVER(pB->iTable>=0)) ) return 2;
80361 }
80362 }
80363 return 0;
80364 }
80365
80366 /*
@@ -84435,11 +84333,11 @@
84333 ** transaction on each used database and to verify the schema cookie
84334 ** on each used database.
84335 */
84336 if( pParse->cookieGoto>0 ){
84337 yDbMask mask;
84338 int iDb, i, addr;
84339 sqlite3VdbeJumpHere(v, pParse->cookieGoto-1);
84340 for(iDb=0, mask=1; iDb<db->nDb; mask<<=1, iDb++){
84341 if( (mask & pParse->cookieMask)==0 ) continue;
84342 sqlite3VdbeUsesBtree(v, iDb);
84343 sqlite3VdbeAddOp2(v,OP_Transaction, iDb, (mask & pParse->writeMask)!=0);
@@ -84449,18 +84347,15 @@
84347 iDb, pParse->cookieValue[iDb],
84348 db->aDb[iDb].pSchema->iGeneration);
84349 }
84350 }
84351 #ifndef SQLITE_OMIT_VIRTUALTABLE
84352 for(i=0; i<pParse->nVtabLock; i++){
84353 char *vtab = (char *)sqlite3GetVTable(db, pParse->apVtabLock[i]);
84354 sqlite3VdbeAddOp4(v, OP_VBegin, 0, 0, 0, vtab, P4_VTAB);
84355 }
84356 pParse->nVtabLock = 0;
 
 
 
84357 #endif
84358
84359 /* Once all the cookies have been verified and transactions opened,
84360 ** obtain the required table-locks. This is a no-op unless the
84361 ** shared-cache feature is enabled.
@@ -84468,13 +84363,23 @@
84363 codeTableLocks(pParse);
84364
84365 /* Initialize any AUTOINCREMENT data structures required.
84366 */
84367 sqlite3AutoincrementBegin(pParse);
84368
84369 /* Code constant expressions that where factored out of inner loops */
84370 addr = pParse->cookieGoto;
84371 if( pParse->pConstExpr ){
84372 ExprList *pEL = pParse->pConstExpr;
84373 pParse->cookieGoto = 0;
84374 for(i=0; i<pEL->nExpr; i++){
84375 sqlite3ExprCode(pParse, pEL->a[i].pExpr, pEL->a[i].u.iConstExprReg);
84376 }
84377 }
84378
84379 /* Finally, jump back to the beginning of the executable code. */
84380 sqlite3VdbeAddOp2(v, OP_Goto, 0, addr);
84381 }
84382 }
84383
84384
84385 /* Get the VDBE program ready for execution
@@ -89185,24 +89090,38 @@
89090 Expr *pWhere /* The WHERE clause. May be null */
89091 ){
89092 Vdbe *v; /* The virtual database engine */
89093 Table *pTab; /* The table from which records will be deleted */
89094 const char *zDb; /* Name of database holding pTab */
 
89095 int i; /* Loop counter */
89096 WhereInfo *pWInfo; /* Information about the WHERE clause */
89097 Index *pIdx; /* For looping over indices of the table */
89098 int iTabCur; /* Cursor number for the table */
89099 int iDataCur; /* VDBE cursor for the canonical data source */
89100 int iIdxCur; /* Cursor number of the first index */
89101 int nIdx; /* Number of indices */
89102 sqlite3 *db; /* Main database structure */
89103 AuthContext sContext; /* Authorization context */
89104 NameContext sNC; /* Name context to resolve expressions in */
89105 int iDb; /* Database number */
89106 int memCnt = -1; /* Memory cell used for change counting */
89107 int rcauth; /* Value returned by authorization callback */
89108 int okOnePass; /* True for one-pass algorithm without the FIFO */
89109 int aiCurOnePass[2]; /* The write cursors opened by WHERE_ONEPASS */
89110 u8 *aToOpen = 0; /* Open cursor iTabCur+j if aToOpen[j] is true */
89111 Index *pPk; /* The PRIMARY KEY index on the table */
89112 int iPk; /* First of nPk registers holding PRIMARY KEY value */
89113 i16 nPk = 1; /* Number of columns in the PRIMARY KEY */
89114 int iKey; /* Memory cell holding key of row to be deleted */
89115 i16 nKey; /* Number of memory cells in the row key */
89116 int iEphCur = 0; /* Ephemeral table holding all primary key values */
89117 int iRowSet = 0; /* Register for rowset of rows to delete */
89118 int addrBypass = 0; /* Address of jump over the delete logic */
89119 int addrLoop = 0; /* Top of the delete loop */
89120 int addrDelete = 0; /* Jump directly to the delete logic */
89121 int addrEphOpen = 0; /* Instruction to open the Ephermeral table */
89122
89123 #ifndef SQLITE_OMIT_TRIGGER
89124 int isView; /* True if attempting to delete from a view */
89125 Trigger *pTrigger; /* List of table triggers, if required */
89126 #endif
89127
@@ -89253,15 +89172,15 @@
89172 if( rcauth==SQLITE_DENY ){
89173 goto delete_from_cleanup;
89174 }
89175 assert(!isView || pTrigger);
89176
89177 /* Assign cursor numbers to the table and all its indices.
89178 */
89179 assert( pTabList->nSrc==1 );
89180 iTabCur = pTabList->a[0].iCursor = pParse->nTab++;
89181 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){
89182 pParse->nTab++;
89183 }
89184
89185 /* Start the view context
89186 */
@@ -89323,132 +89242,162 @@
89242 assert( pIdx->pSchema==pTab->pSchema );
89243 sqlite3VdbeAddOp2(v, OP_Clear, pIdx->tnum, iDb);
89244 }
89245 }else
89246 #endif /* SQLITE_OMIT_TRUNCATE_OPTIMIZATION */
89247 {
89248 if( HasRowid(pTab) ){
89249 /* For a rowid table, initialize the RowSet to an empty set */
89250 pPk = 0;
89251 nPk = 1;
89252 iRowSet = ++pParse->nMem;
89253 sqlite3VdbeAddOp2(v, OP_Null, 0, iRowSet);
89254 }else{
89255 /* For a WITHOUT ROWID table, create an ephermeral table used to
89256 ** hold all primary keys for rows to be deleted. */
89257 pPk = sqlite3PrimaryKeyIndex(pTab);
89258 assert( pPk!=0 );
89259 nPk = pPk->nKeyCol;
89260 iPk = pParse->nMem+1;
89261 pParse->nMem += nPk;
89262 iEphCur = pParse->nTab++;
89263 addrEphOpen = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, iEphCur, nPk);
89264 sqlite3VdbeSetP4KeyInfo(pParse, pPk);
89265 }
89266
89267 /* Construct a query to find the rowid or primary key for every row
89268 ** to be deleted, based on the WHERE clause.
89269 */
89270 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0, 0,
89271 WHERE_ONEPASS_DESIRED|WHERE_DUPLICATES_OK,
89272 iTabCur+1);
89273 if( pWInfo==0 ) goto delete_from_cleanup;
89274 okOnePass = sqlite3WhereOkOnePass(pWInfo, aiCurOnePass);
89275
89276 /* Keep track of the number of rows to be deleted */
89277 if( db->flags & SQLITE_CountRows ){
89278 sqlite3VdbeAddOp2(v, OP_AddImm, memCnt, 1);
89279 }
89280
89281 /* Extract the rowid or primary key for the current row */
89282 if( pPk ){
89283 for(i=0; i<nPk; i++){
89284 sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur,
89285 pPk->aiColumn[i], iPk+i);
89286 }
89287 iKey = iPk;
89288 }else{
89289 iKey = pParse->nMem + 1;
89290 iKey = sqlite3ExprCodeGetColumn(pParse, pTab, -1, iTabCur, iKey, 0);
89291 if( iKey>pParse->nMem ) pParse->nMem = iKey;
89292 }
89293
89294 if( okOnePass ){
89295 /* For ONEPASS, no need to store the rowid/primary-key. There is only
89296 ** one, so just keep it in its register(s) and fall through to the
89297 ** delete code.
89298 */
89299 nKey = nPk; /* OP_Found will use an unpacked key */
89300 aToOpen = sqlite3DbMallocRaw(db, nIdx+2);
89301 if( aToOpen==0 ){
89302 sqlite3WhereEnd(pWInfo);
89303 goto delete_from_cleanup;
89304 }
89305 memset(aToOpen, 1, nIdx+1);
89306 aToOpen[nIdx+1] = 0;
89307 if( aiCurOnePass[0]>=0 ) aToOpen[aiCurOnePass[0]-iTabCur] = 0;
89308 if( aiCurOnePass[1]>=0 ) aToOpen[aiCurOnePass[1]-iTabCur] = 0;
89309 if( addrEphOpen ) sqlite3VdbeChangeToNoop(v, addrEphOpen);
89310 addrDelete = sqlite3VdbeAddOp0(v, OP_Goto); /* Jump to DELETE logic */
89311 }else if( pPk ){
89312 /* Construct a composite key for the row to be deleted and remember it */
89313 iKey = ++pParse->nMem;
89314 nKey = 0; /* Zero tells OP_Found to use a composite key */
89315 sqlite3VdbeAddOp4(v, OP_MakeRecord, iPk, nPk, iKey,
89316 sqlite3IndexAffinityStr(v, pPk), P4_TRANSIENT);
89317 sqlite3VdbeAddOp2(v, OP_IdxInsert, iEphCur, iKey);
89318 }else{
89319 /* Get the rowid of the row to be deleted and remember it in the RowSet */
89320 nKey = 1; /* OP_Seek always uses a single rowid */
89321 sqlite3VdbeAddOp2(v, OP_RowSetAdd, iRowSet, iKey);
89322 }
89323
89324 /* End of the WHERE loop */
89325 sqlite3WhereEnd(pWInfo);
89326 if( okOnePass ){
89327 /* Bypass the delete logic below if the WHERE loop found zero rows */
89328 addrBypass = sqlite3VdbeMakeLabel(v);
89329 sqlite3VdbeAddOp2(v, OP_Goto, 0, addrBypass);
89330 sqlite3VdbeJumpHere(v, addrDelete);
89331 }
89332
89333 /* Unless this is a view, open cursors for the table we are
89334 ** deleting from and all its indices. If this is a view, then the
89335 ** only effect this statement has is to fire the INSTEAD OF
89336 ** triggers.
89337 */
89338 if( !isView ){
89339 sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, iTabCur, aToOpen,
89340 &iDataCur, &iIdxCur);
89341 assert( pPk || iDataCur==iTabCur );
89342 assert( pPk || iIdxCur==iDataCur+1 );
89343 }
89344
89345 /* Set up a loop over the rowids/primary-keys that were found in the
89346 ** where-clause loop above.
89347 */
89348 if( okOnePass ){
89349 /* Just one row. Hence the top-of-loop is a no-op */
89350 assert( nKey==nPk ); /* OP_Found will use an unpacked key */
89351 if( aToOpen[iDataCur-iTabCur] ){
89352 assert( pPk!=0 );
89353 sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, addrBypass, iKey, nKey);
89354 }
89355 }else if( pPk ){
89356 addrLoop = sqlite3VdbeAddOp1(v, OP_Rewind, iEphCur);
89357 sqlite3VdbeAddOp2(v, OP_RowKey, iEphCur, iKey);
89358 assert( nKey==0 ); /* OP_Found will use a composite key */
89359 }else{
89360 addrLoop = sqlite3VdbeAddOp3(v, OP_RowSetRead, iRowSet, 0, iKey);
89361 assert( nKey==1 );
89362 }
89363
89364 /* Delete the row */
89365 #ifndef SQLITE_OMIT_VIRTUALTABLE
89366 if( IsVirtual(pTab) ){
89367 const char *pVTab = (const char *)sqlite3GetVTable(db, pTab);
89368 sqlite3VtabMakeWritable(pParse, pTab);
89369 sqlite3VdbeAddOp4(v, OP_VUpdate, 0, 1, iKey, pVTab, P4_VTAB);
89370 sqlite3VdbeChangeP5(v, OE_Abort);
89371 sqlite3MayAbort(pParse);
89372 }else
89373 #endif
89374 {
89375 int count = (pParse->nested==0); /* True to count changes */
89376 sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur,
89377 iKey, nKey, count, OE_Default, okOnePass);
89378 }
89379
89380 /* End of the loop over all rowids/primary-keys. */
89381 if( okOnePass ){
89382 sqlite3VdbeResolveLabel(v, addrBypass);
89383 }else if( pPk ){
89384 sqlite3VdbeAddOp2(v, OP_Next, iEphCur, addrLoop+1);
89385 sqlite3VdbeJumpHere(v, addrLoop);
89386 }else{
89387 sqlite3VdbeAddOp2(v, OP_Goto, 0, addrLoop);
89388 sqlite3VdbeJumpHere(v, addrLoop);
89389 }
89390
89391 /* Close the cursors open on the table and its indexes. */
89392 if( !isView && !IsVirtual(pTab) ){
89393 if( !pPk ) sqlite3VdbeAddOp1(v, OP_Close, iDataCur);
89394 for(i=0, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){
89395 sqlite3VdbeAddOp1(v, OP_Close, iIdxCur + i);
89396 }
89397 }
89398 } /* End non-truncate path */
89399
89400 /* Update the sqlite_sequence table by storing the content of the
89401 ** maximum rowid counter values recorded while inserting into
89402 ** autoincrement tables.
89403 */
@@ -89468,10 +89417,11 @@
89417
89418 delete_from_cleanup:
89419 sqlite3AuthContextPop(&sContext);
89420 sqlite3SrcListDelete(db, pTabList);
89421 sqlite3ExprDelete(db, pWhere);
89422 sqlite3DbFree(db, aToOpen);
89423 return;
89424 }
89425 /* Make sure "isView" and other macros defined above are undefined. Otherwise
89426 ** thely may interfere with compilation of other functions in this file
89427 ** (or in another file, if this file becomes part of the amalgamation). */
@@ -89534,10 +89484,11 @@
89484 /* If there are any triggers to fire, allocate a range of registers to
89485 ** use for the old.* references in the triggers. */
89486 if( sqlite3FkRequired(pParse, pTab, 0, 0) || pTrigger ){
89487 u32 mask; /* Mask of OLD.* columns in use */
89488 int iCol; /* Iterator used while populating OLD.* */
89489 int addrStart; /* Start of BEFORE trigger programs */
89490
89491 /* TODO: Could use temporary registers here. Also could attempt to
89492 ** avoid copying the contents of the rowid register. */
89493 mask = sqlite3TriggerColmask(
89494 pParse, pTrigger, 0, 0, TRIGGER_BEFORE|TRIGGER_AFTER, pTab, onconf
@@ -89554,19 +89505,23 @@
89505 sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, iCol, iOld+iCol+1);
89506 }
89507 }
89508
89509 /* Invoke BEFORE DELETE trigger programs. */
89510 addrStart = sqlite3VdbeCurrentAddr(v);
89511 sqlite3CodeRowTrigger(pParse, pTrigger,
89512 TK_DELETE, 0, TRIGGER_BEFORE, pTab, iOld, onconf, iLabel
89513 );
89514
89515 /* If any BEFORE triggers were coded, then seek the cursor to the
89516 ** row to be deleted again. It may be that the BEFORE triggers moved
89517 ** the cursor or of already deleted the row that the cursor was
89518 ** pointing to.
89519 */
89520 if( addrStart<sqlite3VdbeCurrentAddr(v) ){
89521 sqlite3VdbeAddOp4Int(v, opSeek, iDataCur, iLabel, iPk, nPk);
89522 }
89523
89524 /* Do FK processing. This call checks that any FK constraints that
89525 ** refer to this table (i.e. constraints attached to other tables)
89526 ** are not violated by deleting this row. */
89527 sqlite3FkCheck(pParse, pTab, iOld, 0, 0, 0);
@@ -91992,10 +91947,11 @@
91947 Vdbe *v = sqlite3GetVdbe(pParse);
91948
91949 assert( pIdx==0 || pIdx->pTable==pTab );
91950 assert( pIdx==0 || pIdx->nKeyCol==pFKey->nCol );
91951 assert( pIdx!=0 || pFKey->nCol==1 );
91952 assert( pIdx!=0 || HasRowid(pTab) );
91953
91954 if( nIncr<0 ){
91955 iFkIfZero = sqlite3VdbeAddOp2(v, OP_FkIfZero, pFKey->isDeferred, 0);
91956 }
91957
@@ -92044,10 +92000,11 @@
92000 pRight = exprTableColumn(db, pTab, pSrc->a[0].iCursor, -1);
92001 pNe = sqlite3PExpr(pParse, TK_NE, pLeft, pRight, 0);
92002 }else{
92003 Expr *pEq, *pAll = 0;
92004 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
92005 assert( pIdx!=0 );
92006 for(i=0; i<pPk->nKeyCol; i++){
92007 i16 iCol = pIdx->aiColumn[i];
92008 pLeft = exprTableRegister(pParse, pTab, regData, iCol);
92009 pRight = exprTableColumn(db, pTab, pSrc->a[0].iCursor, iCol);
92010 pEq = sqlite3PExpr(pParse, TK_EQ, pLeft, pRight, 0);
@@ -93622,11 +93579,11 @@
93579 }
93580
93581 /* If this is not a view, open the table and and all indices */
93582 if( !isView ){
93583 int nIdx;
93584 nIdx = sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, -1, 0,
93585 &iDataCur, &iIdxCur);
93586 aRegIdx = sqlite3DbMallocRaw(db, sizeof(int)*(nIdx+1));
93587 if( aRegIdx==0 ){
93588 goto insert_cleanup;
93589 }
@@ -94482,42 +94439,50 @@
94439 SQLITE_PRIVATE int sqlite3OpenTableAndIndices(
94440 Parse *pParse, /* Parsing context */
94441 Table *pTab, /* Table to be opened */
94442 int op, /* OP_OpenRead or OP_OpenWrite */
94443 int iBase, /* Use this for the table cursor, if there is one */
94444 u8 *aToOpen, /* If not NULL: boolean for each table and index */
94445 int *piDataCur, /* Write the database source cursor number here */
94446 int *piIdxCur /* Write the first index cursor number here */
94447 ){
94448 int i;
94449 int iDb;
94450 int iDataCur;
94451 Index *pIdx;
94452 Vdbe *v;
94453
94454 assert( op==OP_OpenRead || op==OP_OpenWrite );
94455 if( IsVirtual(pTab) ){
94456 assert( aToOpen==0 );
94457 *piDataCur = 0;
94458 *piIdxCur = 1;
94459 return 0;
94460 }
94461 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
94462 v = sqlite3GetVdbe(pParse);
94463 assert( v!=0 );
94464 if( iBase<0 ) iBase = pParse->nTab;
94465 iDataCur = iBase++;
94466 if( piDataCur ) *piDataCur = iDataCur;
94467 if( HasRowid(pTab) && (aToOpen==0 || aToOpen[0]) ){
94468 sqlite3OpenTable(pParse, iDataCur, iDb, pTab, op);
94469 }else{
94470 sqlite3TableLock(pParse, iDb, pTab->tnum, op==OP_OpenWrite, pTab->zName);
94471 }
94472 if( piIdxCur ) *piIdxCur = iBase;
94473 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
94474 int iIdxCur = iBase++;
94475 assert( pIdx->pSchema==pTab->pSchema );
94476 if( pIdx->autoIndex==2 && !HasRowid(pTab) && piDataCur ){
94477 *piDataCur = iIdxCur;
94478 }
94479 if( aToOpen==0 || aToOpen[i+1] ){
94480 sqlite3VdbeAddOp3(v, op, iIdxCur, pIdx->tnum, iDb);
94481 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
94482 VdbeComment((v, "%s", pIdx->zName));
94483 }
94484 }
94485 if( iBase>pParse->nTab ) pParse->nTab = iBase;
94486 return i;
94487 }
94488
@@ -98144,11 +98109,11 @@
98109 addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1); /* Stop if out of errors */
98110 sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
98111 sqlite3VdbeJumpHere(v, addr);
98112 sqlite3ExprCacheClear(pParse);
98113 sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenRead,
98114 1, 0, &iDataCur, &iIdxCur);
98115 sqlite3VdbeAddOp2(v, OP_Integer, 0, 7);
98116 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
98117 sqlite3VdbeAddOp2(v, OP_Integer, 0, 8+j); /* index entries counter */
98118 }
98119 pParse->nMem = MAX(pParse->nMem, 8+j);
@@ -99079,10 +99044,17 @@
99044 }
99045 assert( i>=0 && i<db->nDb );
99046 }
99047 return i;
99048 }
99049
99050 /*
99051 ** Free all memory allocations in the pParse object
99052 */
99053 SQLITE_PRIVATE void sqlite3ParserReset(Parse *pParse){
99054 if( pParse ) sqlite3ExprListDelete(pParse->db, pParse->pConstExpr);
99055 }
99056
99057 /*
99058 ** Compile the UTF-8 encoded SQL statement zSql into a statement handle.
99059 */
99060 static int sqlite3Prepare(
@@ -99237,10 +99209,11 @@
99209 sqlite3DbFree(db, pT);
99210 }
99211
99212 end_prepare:
99213
99214 sqlite3ParserReset(pParse);
99215 sqlite3StackFree(db, pParse);
99216 rc = sqlite3ApiExit(db, rc);
99217 assert( (rc&db->errMask)==rc );
99218 return rc;
99219 }
@@ -101803,20 +101776,20 @@
101776 */
101777 if( op!=TK_ALL ){
101778 for(i=1; db->mallocFailed==0 && i<=p->pEList->nExpr; i++){
101779 struct ExprList_item *pItem;
101780 for(j=0, pItem=pOrderBy->a; j<nOrderBy; j++, pItem++){
101781 assert( pItem->u.x.iOrderByCol>0 );
101782 if( pItem->u.x.iOrderByCol==i ) break;
101783 }
101784 if( j==nOrderBy ){
101785 Expr *pNew = sqlite3Expr(db, TK_INTEGER, 0);
101786 if( pNew==0 ) return SQLITE_NOMEM;
101787 pNew->flags |= EP_IntValue;
101788 pNew->u.iValue = i;
101789 pOrderBy = sqlite3ExprListAppend(pParse, pOrderBy, pNew);
101790 if( pOrderBy ) pOrderBy->a[nOrderBy++].u.x.iOrderByCol = (u16)i;
101791 }
101792 }
101793 }
101794
101795 /* Compute the comparison permutation and keyinfo that is used with
@@ -101828,12 +101801,13 @@
101801 */
101802 aPermute = sqlite3DbMallocRaw(db, sizeof(int)*nOrderBy);
101803 if( aPermute ){
101804 struct ExprList_item *pItem;
101805 for(i=0, pItem=pOrderBy->a; i<nOrderBy; i++, pItem++){
101806 assert( pItem->u.x.iOrderByCol>0
101807 && pItem->u.x.iOrderByCol<=p->pEList->nExpr );
101808 aPermute[i] = pItem->u.x.iOrderByCol - 1;
101809 }
101810 pKeyMerge = sqlite3KeyInfoAlloc(db, nOrderBy, 1);
101811 if( pKeyMerge ){
101812 for(i=0; i<nOrderBy; i++){
101813 CollSeq *pColl;
@@ -102409,11 +102383,11 @@
102383
102384 /* Restriction 18. */
102385 if( p->pOrderBy ){
102386 int ii;
102387 for(ii=0; ii<p->pOrderBy->nExpr; ii++){
102388 if( p->pOrderBy->a[ii].u.x.iOrderByCol==0 ) return 0;
102389 }
102390 }
102391 }
102392
102393 /***** If we reach this point, flattening is permitted. *****/
@@ -103816,14 +103790,14 @@
103790 if( pGroupBy ){
103791 int k; /* Loop counter */
103792 struct ExprList_item *pItem; /* For looping over expression in a list */
103793
103794 for(k=p->pEList->nExpr, pItem=p->pEList->a; k>0; k--, pItem++){
103795 pItem->u.x.iAlias = 0;
103796 }
103797 for(k=pGroupBy->nExpr, pItem=pGroupBy->a; k>0; k--, pItem++){
103798 pItem->u.x.iAlias = 0;
103799 }
103800 if( p->nSelectRow>100 ) p->nSelectRow = 100;
103801 }else{
103802 p->nSelectRow = 1;
103803 }
@@ -105470,10 +105444,11 @@
105444 sqlite3VdbeDelete(v);
105445 }
105446
105447 assert( !pSubParse->pAinc && !pSubParse->pZombieTab );
105448 assert( !pSubParse->pTriggerPrg && !pSubParse->nMaxArg );
105449 sqlite3ParserReset(pSubParse);
105450 sqlite3StackFree(db, pSubParse);
105451
105452 return pPrg;
105453 }
105454
@@ -105784,22 +105759,23 @@
105759 WhereInfo *pWInfo; /* Information about the WHERE clause */
105760 Vdbe *v; /* The virtual database engine */
105761 Index *pIdx; /* For looping over indices */
105762 Index *pPk; /* The PRIMARY KEY index for WITHOUT ROWID tables */
105763 int nIdx; /* Number of indices that need updating */
105764 int iBaseCur; /* Base cursor number */
105765 int iDataCur; /* Cursor for the canonical data btree */
105766 int iIdxCur; /* Cursor for the first index */
105767 sqlite3 *db; /* The database structure */
105768 int *aRegIdx = 0; /* One register assigned to each index to be updated */
105769 int *aXRef = 0; /* aXRef[i] is the index in pChanges->a[] of the
105770 ** an expression for the i-th column of the table.
105771 ** aXRef[i]==-1 if the i-th column is not changed. */
105772 u8 *aToOpen; /* 1 for tables and indices to be opened */
105773 u8 chngPk; /* PRIMARY KEY changed in a WITHOUT ROWID table */
105774 u8 chngRowid; /* Rowid changed in a normal table */
105775 u8 chngKey; /* Either chngPk or chngRowid */
105776 Expr *pRowidExpr = 0; /* Expression defining the new record number */
 
105777 AuthContext sContext; /* The authorization context */
105778 NameContext sNC; /* The name-context to resolve expressions in */
105779 int iDb; /* Database containing the table being updated */
105780 int okOnePass; /* True for one-pass algorithm without the FIFO */
105781 int hasFK; /* True if foreign key processing is required */
@@ -105859,29 +105835,37 @@
105835 goto update_cleanup;
105836 }
105837 if( sqlite3IsReadOnly(pParse, pTab, tmask) ){
105838 goto update_cleanup;
105839 }
 
 
 
105840
105841 /* Allocate a cursors for the main database table and for all indices.
105842 ** The index cursors might not be used, but if they are used they
105843 ** need to occur right after the database cursor. So go ahead and
105844 ** allocate enough space, just in case.
105845 */
105846 pTabList->a[0].iCursor = iBaseCur = iDataCur = pParse->nTab++;
105847 iIdxCur = iDataCur+1;
105848 pPk = HasRowid(pTab) ? 0 : sqlite3PrimaryKeyIndex(pTab);
105849 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){
105850 if( pIdx->autoIndex==2 && pPk!=0 ){
105851 iDataCur = pParse->nTab;
105852 pTabList->a[0].iCursor = iDataCur;
105853 }
105854 pParse->nTab++;
105855 }
105856
105857 /* Allocate space for aXRef[], aRegIdx[], and aToOpen[].
105858 ** Initialize aXRef[] and aToOpen[] to their default values.
105859 */
105860 aXRef = sqlite3DbMallocRaw(db, sizeof(int) * (pTab->nCol+nIdx) + nIdx+2 );
105861 if( aXRef==0 ) goto update_cleanup;
105862 aRegIdx = aXRef+pTab->nCol;
105863 aToOpen = (u8*)(aRegIdx+nIdx);
105864 memset(aToOpen, 1, nIdx+1);
105865 aToOpen[nIdx+1] = 0;
105866 for(i=0; i<pTab->nCol; i++) aXRef[i] = -1;
105867
105868 /* Initialize the name-context */
105869 memset(&sNC, 0, sizeof(sNC));
105870 sNC.pParse = pParse;
105871 sNC.pSrcList = pTabList;
@@ -105936,22 +105920,22 @@
105920 }
105921 assert( (chngRowid & chngPk)==0 );
105922 assert( chngRowid==0 || chngRowid==1 );
105923 assert( chngPk==0 || chngPk==1 );
105924 chngKey = chngRowid + chngPk;
105925
105926 /* The SET expressions are not actually used inside the WHERE loop.
105927 ** So reset the colUsed mask
105928 */
105929 pTabList->a[0].colUsed = 0;
105930
105931 hasFK = sqlite3FkRequired(pParse, pTab, aXRef, chngKey);
105932
105933 /* There is one entry in the aRegIdx[] array for each index on the table
105934 ** being updated. Fill in aRegIdx[] with a register number that will hold
105935 ** the key for accessing each index.
 
105936 */
 
 
 
 
105937 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
105938 int reg;
105939 if( chngKey || hasFK || pIdx->pPartIdxWhere || pIdx==pPk ){
105940 reg = ++pParse->nMem;
105941 }else{
@@ -105961,10 +105945,11 @@
105945 reg = ++pParse->nMem;
105946 break;
105947 }
105948 }
105949 }
105950 if( reg==0 ) aToOpen[j+1] = 0;
105951 aRegIdx[j] = reg;
105952 }
105953
105954 /* Begin generating code. */
105955 v = sqlite3GetVdbe(pParse);
@@ -106084,46 +106069,34 @@
106069 ** Open every index that needs updating. Note that if any
106070 ** index could potentially invoke a REPLACE conflict resolution
106071 ** action, then we need to open all indices because we might need
106072 ** to be deleting some records.
106073 */
 
 
 
 
106074 if( onError==OE_Replace ){
106075 memset(aToOpen, 1, nIdx+1);
106076 }else{
 
106077 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
106078 if( pIdx->onError==OE_Replace ){
106079 memset(aToOpen, 1, nIdx+1);
106080 break;
106081 }
106082 }
106083 }
106084 if( okOnePass ){
106085 if( aiCurOnePass[0]>=0 ) aToOpen[aiCurOnePass[0]-iBaseCur] = 0;
106086 if( aiCurOnePass[1]>=0 ) aToOpen[aiCurOnePass[1]-iBaseCur] = 0;
106087 }
106088 sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, iBaseCur, aToOpen,
106089 0, 0);
 
 
 
 
 
 
 
 
 
 
 
106090 }
106091
106092 /* Top of the update loop */
106093 if( okOnePass ){
106094 if( aToOpen[iDataCur-iBaseCur] ){
106095 assert( pPk!=0 );
106096 sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, labelBreak, regKey, nKey);
106097 }
106098 labelContinue = labelBreak;
106099 sqlite3VdbeAddOp2(v, OP_IsNull, pPk ? regKey : regOldRowid, labelBreak);
106100 }else if( pPk ){
106101 labelContinue = sqlite3VdbeMakeLabel(v);
106102 sqlite3VdbeAddOp2(v, OP_Rewind, iEph, labelBreak);
@@ -106312,11 +106285,11 @@
106285 sqlite3VdbeResolveLabel(v, labelBreak);
106286
106287 /* Close all tables */
106288 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
106289 assert( aRegIdx );
106290 if( aToOpen[i+1] ){
106291 sqlite3VdbeAddOp2(v, OP_Close, iIdxCur+i, 0);
106292 }
106293 }
106294 if( iDataCur<iIdxCur ) sqlite3VdbeAddOp2(v, OP_Close, iDataCur, 0);
106295
@@ -106339,12 +106312,11 @@
106312 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows updated", SQLITE_STATIC);
106313 }
106314
106315 update_cleanup:
106316 sqlite3AuthContextPop(&sContext);
106317 sqlite3DbFree(db, aXRef); /* Also frees aRegIdx[] and aToOpen[] */
 
106318 sqlite3SrcListDelete(db, pTabList);
106319 sqlite3ExprListDelete(db, pChanges);
106320 sqlite3ExprDelete(db, pWhere);
106321 return;
106322 }
@@ -107563,10 +107535,11 @@
107535
107536 if( pParse->pVdbe ){
107537 sqlite3VdbeFinalize(pParse->pVdbe);
107538 }
107539 sqlite3DeleteTable(db, pParse->pNewTable);
107540 sqlite3ParserReset(pParse);
107541 sqlite3StackFree(db, pParse);
107542 }
107543
107544 assert( (rc&0xff)==rc );
107545 rc = sqlite3ApiExit(db, rc);
@@ -109055,13 +109028,10 @@
109028 }
109029 assert( pLeft->iColumn!=(-1) ); /* Because IPK never has AFF_TEXT */
109030
109031 pRight = pList->a[0].pExpr;
109032 op = pRight->op;
 
 
 
109033 if( op==TK_VARIABLE ){
109034 Vdbe *pReprepare = pParse->pReprepare;
109035 int iCol = pRight->iColumn;
109036 pVal = sqlite3VdbeGetBoundValue(pReprepare, iCol, SQLITE_AFF_NONE);
109037 if( pVal && sqlite3_value_type(pVal)==SQLITE_TEXT ){
@@ -111145,11 +111115,11 @@
111115 iCur = pTabItem->iCursor;
111116 pLevel->notReady = notReady & ~getMask(&pWInfo->sMaskSet, iCur);
111117 bRev = (pWInfo->revMask>>iLevel)&1;
111118 omitTable = (pLoop->wsFlags & WHERE_IDX_ONLY)!=0
111119 && (pWInfo->wctrlFlags & WHERE_FORCE_TABLE)==0;
111120 VdbeModuleComment((v, "Begin WHERE-loop%d: %s",iLevel,pTabItem->pTab->zName));
111121
111122 /* Create labels for the "break" and "continue" instructions
111123 ** for the current loop. Jump to addrBrk to break out of a loop.
111124 ** Jump to cont to go immediately to the next iteration of the
111125 ** loop.
@@ -111387,11 +111357,11 @@
111357 Index *pIdx; /* The index we will be using */
111358 int iIdxCur; /* The VDBE cursor for the index */
111359 int nExtraReg = 0; /* Number of extra registers needed */
111360 int op; /* Instruction opcode */
111361 char *zStartAff; /* Affinity for start of range constraint */
111362 char cEndAff = 0; /* Affinity for end of range constraint */
111363
111364 pIdx = pLoop->u.btree.pIndex;
111365 iIdxCur = pLevel->iIdxCur;
111366 assert( nEq>=pLoop->u.btree.nSkip );
111367
@@ -111428,11 +111398,12 @@
111398 /* Generate code to evaluate all constraint terms using == or IN
111399 ** and store the values of those terms in an array of registers
111400 ** starting at regBase.
111401 */
111402 regBase = codeAllEqualityTerms(pParse,pLevel,bRev,nExtraReg,&zStartAff);
111403 assert( zStartAff==0 || sqlite3Strlen30(zStartAff)>=nEq );
111404 if( zStartAff ) cEndAff = zStartAff[nEq];
111405 addrNxt = pLevel->addrNxt;
111406
111407 /* If we are doing a reverse order scan on an ascending index, or
111408 ** a forward order scan on a descending index, interchange the
111409 ** start and end terms (pRangeStart and pRangeEnd).
@@ -111498,27 +111469,19 @@
111469 sqlite3ExprCacheRemove(pParse, regBase+nEq, 1);
111470 sqlite3ExprCode(pParse, pRight, regBase+nEq);
111471 if( (pRangeEnd->wtFlags & TERM_VNULL)==0 ){
111472 sqlite3ExprCodeIsNullJump(v, pRight, regBase+nEq, addrNxt);
111473 }
111474 if( sqlite3CompareAffinity(pRight, cEndAff)!=SQLITE_AFF_NONE
111475 && !sqlite3ExprNeedsNoAffinityChange(pRight, cEndAff)
111476 ){
111477 codeApplyAffinity(pParse, regBase+nEq, 1, &cEndAff);
111478 }
 
 
 
 
 
 
 
111479 nConstraint++;
111480 testcase( pRangeEnd->wtFlags & TERM_VIRTUAL );
111481 }
111482 sqlite3DbFree(db, zStartAff);
 
111483
111484 /* Top of the loop body */
111485 pLevel->p2 = sqlite3VdbeCurrentAddr(v);
111486
111487 /* Check if the index cursor is past the end of the range. */
@@ -111539,10 +111502,11 @@
111502 testcase( pLoop->wsFlags & WHERE_BTM_LIMIT );
111503 testcase( pLoop->wsFlags & WHERE_TOP_LIMIT );
111504 if( (pLoop->wsFlags & (WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0
111505 && (j = pIdx->aiColumn[nEq])>=0
111506 && pIdx->pTable->aCol[j].notNull==0
111507 && (nEq || (pLoop->wsFlags & WHERE_BTM_LIMIT)==0)
111508 ){
111509 sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, nEq, r1);
111510 VdbeComment((v, "%s", pIdx->pTable->aCol[j].zName));
111511 sqlite3VdbeAddOp2(v, OP_IsNull, r1, addrCont);
111512 }
@@ -111856,11 +111820,11 @@
111820 pAlt = findTerm(pWC, iCur, pTerm->u.leftColumn, notReady, WO_EQ|WO_IN, 0);
111821 if( pAlt==0 ) continue;
111822 if( pAlt->wtFlags & (TERM_CODED) ) continue;
111823 testcase( pAlt->eOperator & WO_EQ );
111824 testcase( pAlt->eOperator & WO_IN );
111825 VdbeModuleComment((v, "begin transitive constraint"));
111826 pEAlt = sqlite3StackAllocRaw(db, sizeof(*pEAlt));
111827 if( pEAlt ){
111828 *pEAlt = *pAlt->pExpr;
111829 pEAlt->pLeft = pE->pLeft;
111830 sqlite3ExprIfFalse(pParse, pEAlt, addrCont, SQLITE_JUMPIFNULL);
@@ -112313,14 +112277,19 @@
112277 saved_wsFlags = pNew->wsFlags;
112278 saved_prereq = pNew->prereq;
112279 saved_nOut = pNew->nOut;
112280 pNew->rSetup = 0;
112281 rLogSize = estLog(sqlite3LogEst(pProbe->aiRowEst[0]));
112282
112283 /* Consider using a skip-scan if there are no WHERE clause constraints
112284 ** available for the left-most terms of the index, and if the average
112285 ** number of repeats in the left-most terms is at least 50.
112286 */
112287 if( pTerm==0
112288 && saved_nEq==saved_nSkip
112289 && saved_nEq+1<pProbe->nKeyCol
112290 && pProbe->aiRowEst[saved_nEq+1]>50 /* TUNING: Minimum for skip-scan */
112291 ){
112292 LogEst nIter;
112293 pNew->u.btree.nEq++;
112294 pNew->u.btree.nSkip++;
112295 pNew->aLTerm[pNew->nLTerm++] = 0;
@@ -113812,11 +113781,10 @@
113781 /* Split the WHERE clause into separate subexpressions where each
113782 ** subexpression is separated by an AND operator.
113783 */
113784 initMaskSet(pMaskSet);
113785 whereClauseInit(&pWInfo->sWC, pWInfo);
 
113786 whereSplit(&pWInfo->sWC, pWhere, TK_AND);
113787 sqlite3CodeVerifySchema(pParse, -1); /* Insert the cookie verifier Goto */
113788
113789 /* Special case: a WHERE clause that is constant. Evaluate the
113790 ** expression and either jump over all of the code or fall thru.
@@ -114127,11 +114095,11 @@
114095 notReady = codeOneLoopStart(pWInfo, ii, notReady);
114096 pWInfo->iContinue = pLevel->addrCont;
114097 }
114098
114099 /* Done. */
114100 VdbeModuleComment((v, "Begin WHERE-core"));
114101 return pWInfo;
114102
114103 /* Jump here if malloc fails */
114104 whereBeginError:
114105 if( pWInfo ){
@@ -114154,11 +114122,11 @@
114122 SrcList *pTabList = pWInfo->pTabList;
114123 sqlite3 *db = pParse->db;
114124
114125 /* Generate loop termination code.
114126 */
114127 VdbeModuleComment((v, "End WHERE-core"));
114128 sqlite3ExprCacheClear(pParse);
114129 for(i=pWInfo->nLevel-1; i>=0; i--){
114130 int addr;
114131 pLevel = &pWInfo->a[i];
114132 pLoop = pLevel->pWLoop;
@@ -114200,11 +114168,11 @@
114168 }else{
114169 sqlite3VdbeAddOp2(v, OP_Goto, 0, pLevel->addrFirst);
114170 }
114171 sqlite3VdbeJumpHere(v, addr);
114172 }
114173 VdbeModuleComment((v, "End WHERE-loop%d: %s", i,
114174 pWInfo->pTabList->a[pLevel->iFrom].pTab->zName));
114175 }
114176
114177 /* The "break" point is here, just past the end of the outer loop.
114178 ** Set it.
@@ -123900,10 +123868,13 @@
123868 }
123869
123870 #define GETVARINT_STEP(v, ptr, shift, mask1, mask2, var, ret) \
123871 v = (v & mask1) | ( (*ptr++) << shift ); \
123872 if( (v & mask2)==0 ){ var = v; return ret; }
123873 #define GETVARINT_INIT(v, ptr, shift, mask1, mask2, var, ret) \
123874 v = (*ptr++); \
123875 if( (v & mask2)==0 ){ var = v; return ret; }
123876
123877 /*
123878 ** Read a 64-bit variable-length integer from memory starting at p[0].
123879 ** Return the number of bytes read, or 0 on error.
123880 ** The value is stored in *v.
@@ -123912,11 +123883,11 @@
123883 const char *pStart = p;
123884 u32 a;
123885 u64 b;
123886 int shift;
123887
123888 GETVARINT_INIT(a, p, 0, 0x00, 0x80, *v, 1);
123889 GETVARINT_STEP(a, p, 7, 0x7F, 0x4000, *v, 2);
123890 GETVARINT_STEP(a, p, 14, 0x3FFF, 0x200000, *v, 3);
123891 GETVARINT_STEP(a, p, 21, 0x1FFFFF, 0x10000000, *v, 4);
123892 b = (a & 0x0FFFFFFF );
123893
@@ -123924,11 +123895,11 @@
123895 u64 c = *p++;
123896 b += (c&0x7F) << shift;
123897 if( (c & 0x80)==0 ) break;
123898 }
123899 *v = b;
123900 return (int)(p - pStart);
123901 }
123902
123903 /*
123904 ** Similar to sqlite3Fts3GetVarint(), except that the output is truncated to a
123905 ** 32-bit integer before it is returned.
@@ -123935,11 +123906,11 @@
123906 */
123907 SQLITE_PRIVATE int sqlite3Fts3GetVarint32(const char *p, int *pi){
123908 u32 a;
123909
123910 #ifndef fts3GetVarint32
123911 GETVARINT_INIT(a, p, 0, 0x00, 0x80, *pi, 1);
123912 #else
123913 a = (*p++);
123914 assert( a & 0x80 );
123915 #endif
123916
123917
+7 -8
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -107,11 +107,11 @@
107107
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
108108
** [sqlite_version()] and [sqlite_source_id()].
109109
*/
110110
#define SQLITE_VERSION "3.8.2"
111111
#define SQLITE_VERSION_NUMBER 3008002
112
-#define SQLITE_SOURCE_ID "2013-11-14 19:34:10 10d59226382adcb8016fc2d927e5a0c0b36f3980"
112
+#define SQLITE_SOURCE_ID "2013-11-19 13:55:34 17e8524fc05aa1e6074c19a8ccccc5ab5883103a"
113113
114114
/*
115115
** CAPI3REF: Run-Time Library Version Numbers
116116
** KEYWORDS: sqlite3_version, sqlite3_sourceid
117117
**
@@ -368,11 +368,11 @@
368368
** Restrictions:
369369
**
370370
** <ul>
371371
** <li> The application must insure that the 1st parameter to sqlite3_exec()
372372
** is a valid and open [database connection].
373
-** <li> The application must not close [database connection] specified by
373
+** <li> The application must not close the [database connection] specified by
374374
** the 1st parameter to sqlite3_exec() while sqlite3_exec() is running.
375375
** <li> The application must not modify the SQL statement text passed into
376376
** the 2nd parameter of sqlite3_exec() while sqlite3_exec() is running.
377377
** </ul>
378378
*/
@@ -445,11 +445,11 @@
445445
** about errors. The extended result codes are enabled or disabled
446446
** on a per database connection basis using the
447447
** [sqlite3_extended_result_codes()] API.
448448
**
449449
** Some of the available extended result codes are listed here.
450
-** One may expect the number of extended result codes will be expand
450
+** One may expect the number of extended result codes will increase
451451
** over time. Software that uses extended result codes should expect
452452
** to see new result codes in future releases of SQLite.
453453
**
454454
** The SQLITE_OK result code will never be extended. It will always
455455
** be exactly zero.
@@ -1383,11 +1383,11 @@
13831383
** of 8. Some allocators round up to a larger multiple or to a power of 2.
13841384
** Every memory allocation request coming in through [sqlite3_malloc()]
13851385
** or [sqlite3_realloc()] first calls xRoundup. If xRoundup returns 0,
13861386
** that causes the corresponding memory allocation to fail.
13871387
**
1388
-** The xInit method initializes the memory allocator. (For example,
1388
+** The xInit method initializes the memory allocator. For example,
13891389
** it might allocate any require mutexes or initialize internal data
13901390
** structures. The xShutdown method is invoked (indirectly) by
13911391
** [sqlite3_shutdown()] and should deallocate any resources acquired
13921392
** by xInit. The pAppData pointer is used as the only parameter to
13931393
** xInit and xShutdown.
@@ -3109,11 +3109,10 @@
31093109
** to the [sqlite3_bind_text | bindings] of that [parameter].
31103110
** ^The specific value of WHERE-clause [parameter] might influence the
31113111
** choice of query plan if the parameter is the left-hand side of a [LIKE]
31123112
** or [GLOB] operator or if the parameter is compared to an indexed column
31133113
** and the [SQLITE_ENABLE_STAT3] compile-time option is enabled.
3114
-** the
31153114
** </li>
31163115
** </ol>
31173116
*/
31183117
SQLITE_API int sqlite3_prepare(
31193118
sqlite3 *db, /* Database handle */
@@ -3839,11 +3838,11 @@
38393838
**
38403839
** ^The pointers returned are valid until a type conversion occurs as
38413840
** described above, or until [sqlite3_step()] or [sqlite3_reset()] or
38423841
** [sqlite3_finalize()] is called. ^The memory space used to hold strings
38433842
** and BLOBs is freed automatically. Do <b>not</b> pass the pointers returned
3844
-** [sqlite3_column_blob()], [sqlite3_column_text()], etc. into
3843
+** from [sqlite3_column_blob()], [sqlite3_column_text()], etc. into
38453844
** [sqlite3_free()].
38463845
**
38473846
** ^(If a memory allocation error occurs during the evaluation of any
38483847
** of these routines, a default value is returned. The default value
38493848
** is either the integer 0, the floating point number 0.0, or a NULL
@@ -4917,12 +4916,12 @@
49174916
/*
49184917
** CAPI3REF: Free Memory Used By A Database Connection
49194918
**
49204919
** ^The sqlite3_db_release_memory(D) interface attempts to free as much heap
49214920
** memory as possible from database connection D. Unlike the
4922
-** [sqlite3_release_memory()] interface, this interface is effect even
4923
-** when then [SQLITE_ENABLE_MEMORY_MANAGEMENT] compile-time option is
4921
+** [sqlite3_release_memory()] interface, this interface is in effect even
4922
+** when the [SQLITE_ENABLE_MEMORY_MANAGEMENT] compile-time option is
49244923
** omitted.
49254924
**
49264925
** See also: [sqlite3_release_memory()]
49274926
*/
49284927
SQLITE_API int sqlite3_db_release_memory(sqlite3*);
49294928
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -107,11 +107,11 @@
107 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
108 ** [sqlite_version()] and [sqlite_source_id()].
109 */
110 #define SQLITE_VERSION "3.8.2"
111 #define SQLITE_VERSION_NUMBER 3008002
112 #define SQLITE_SOURCE_ID "2013-11-14 19:34:10 10d59226382adcb8016fc2d927e5a0c0b36f3980"
113
114 /*
115 ** CAPI3REF: Run-Time Library Version Numbers
116 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
117 **
@@ -368,11 +368,11 @@
368 ** Restrictions:
369 **
370 ** <ul>
371 ** <li> The application must insure that the 1st parameter to sqlite3_exec()
372 ** is a valid and open [database connection].
373 ** <li> The application must not close [database connection] specified by
374 ** the 1st parameter to sqlite3_exec() while sqlite3_exec() is running.
375 ** <li> The application must not modify the SQL statement text passed into
376 ** the 2nd parameter of sqlite3_exec() while sqlite3_exec() is running.
377 ** </ul>
378 */
@@ -445,11 +445,11 @@
445 ** about errors. The extended result codes are enabled or disabled
446 ** on a per database connection basis using the
447 ** [sqlite3_extended_result_codes()] API.
448 **
449 ** Some of the available extended result codes are listed here.
450 ** One may expect the number of extended result codes will be expand
451 ** over time. Software that uses extended result codes should expect
452 ** to see new result codes in future releases of SQLite.
453 **
454 ** The SQLITE_OK result code will never be extended. It will always
455 ** be exactly zero.
@@ -1383,11 +1383,11 @@
1383 ** of 8. Some allocators round up to a larger multiple or to a power of 2.
1384 ** Every memory allocation request coming in through [sqlite3_malloc()]
1385 ** or [sqlite3_realloc()] first calls xRoundup. If xRoundup returns 0,
1386 ** that causes the corresponding memory allocation to fail.
1387 **
1388 ** The xInit method initializes the memory allocator. (For example,
1389 ** it might allocate any require mutexes or initialize internal data
1390 ** structures. The xShutdown method is invoked (indirectly) by
1391 ** [sqlite3_shutdown()] and should deallocate any resources acquired
1392 ** by xInit. The pAppData pointer is used as the only parameter to
1393 ** xInit and xShutdown.
@@ -3109,11 +3109,10 @@
3109 ** to the [sqlite3_bind_text | bindings] of that [parameter].
3110 ** ^The specific value of WHERE-clause [parameter] might influence the
3111 ** choice of query plan if the parameter is the left-hand side of a [LIKE]
3112 ** or [GLOB] operator or if the parameter is compared to an indexed column
3113 ** and the [SQLITE_ENABLE_STAT3] compile-time option is enabled.
3114 ** the
3115 ** </li>
3116 ** </ol>
3117 */
3118 SQLITE_API int sqlite3_prepare(
3119 sqlite3 *db, /* Database handle */
@@ -3839,11 +3838,11 @@
3839 **
3840 ** ^The pointers returned are valid until a type conversion occurs as
3841 ** described above, or until [sqlite3_step()] or [sqlite3_reset()] or
3842 ** [sqlite3_finalize()] is called. ^The memory space used to hold strings
3843 ** and BLOBs is freed automatically. Do <b>not</b> pass the pointers returned
3844 ** [sqlite3_column_blob()], [sqlite3_column_text()], etc. into
3845 ** [sqlite3_free()].
3846 **
3847 ** ^(If a memory allocation error occurs during the evaluation of any
3848 ** of these routines, a default value is returned. The default value
3849 ** is either the integer 0, the floating point number 0.0, or a NULL
@@ -4917,12 +4916,12 @@
4917 /*
4918 ** CAPI3REF: Free Memory Used By A Database Connection
4919 **
4920 ** ^The sqlite3_db_release_memory(D) interface attempts to free as much heap
4921 ** memory as possible from database connection D. Unlike the
4922 ** [sqlite3_release_memory()] interface, this interface is effect even
4923 ** when then [SQLITE_ENABLE_MEMORY_MANAGEMENT] compile-time option is
4924 ** omitted.
4925 **
4926 ** See also: [sqlite3_release_memory()]
4927 */
4928 SQLITE_API int sqlite3_db_release_memory(sqlite3*);
4929
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -107,11 +107,11 @@
107 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
108 ** [sqlite_version()] and [sqlite_source_id()].
109 */
110 #define SQLITE_VERSION "3.8.2"
111 #define SQLITE_VERSION_NUMBER 3008002
112 #define SQLITE_SOURCE_ID "2013-11-19 13:55:34 17e8524fc05aa1e6074c19a8ccccc5ab5883103a"
113
114 /*
115 ** CAPI3REF: Run-Time Library Version Numbers
116 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
117 **
@@ -368,11 +368,11 @@
368 ** Restrictions:
369 **
370 ** <ul>
371 ** <li> The application must insure that the 1st parameter to sqlite3_exec()
372 ** is a valid and open [database connection].
373 ** <li> The application must not close the [database connection] specified by
374 ** the 1st parameter to sqlite3_exec() while sqlite3_exec() is running.
375 ** <li> The application must not modify the SQL statement text passed into
376 ** the 2nd parameter of sqlite3_exec() while sqlite3_exec() is running.
377 ** </ul>
378 */
@@ -445,11 +445,11 @@
445 ** about errors. The extended result codes are enabled or disabled
446 ** on a per database connection basis using the
447 ** [sqlite3_extended_result_codes()] API.
448 **
449 ** Some of the available extended result codes are listed here.
450 ** One may expect the number of extended result codes will increase
451 ** over time. Software that uses extended result codes should expect
452 ** to see new result codes in future releases of SQLite.
453 **
454 ** The SQLITE_OK result code will never be extended. It will always
455 ** be exactly zero.
@@ -1383,11 +1383,11 @@
1383 ** of 8. Some allocators round up to a larger multiple or to a power of 2.
1384 ** Every memory allocation request coming in through [sqlite3_malloc()]
1385 ** or [sqlite3_realloc()] first calls xRoundup. If xRoundup returns 0,
1386 ** that causes the corresponding memory allocation to fail.
1387 **
1388 ** The xInit method initializes the memory allocator. For example,
1389 ** it might allocate any require mutexes or initialize internal data
1390 ** structures. The xShutdown method is invoked (indirectly) by
1391 ** [sqlite3_shutdown()] and should deallocate any resources acquired
1392 ** by xInit. The pAppData pointer is used as the only parameter to
1393 ** xInit and xShutdown.
@@ -3109,11 +3109,10 @@
3109 ** to the [sqlite3_bind_text | bindings] of that [parameter].
3110 ** ^The specific value of WHERE-clause [parameter] might influence the
3111 ** choice of query plan if the parameter is the left-hand side of a [LIKE]
3112 ** or [GLOB] operator or if the parameter is compared to an indexed column
3113 ** and the [SQLITE_ENABLE_STAT3] compile-time option is enabled.
 
3114 ** </li>
3115 ** </ol>
3116 */
3117 SQLITE_API int sqlite3_prepare(
3118 sqlite3 *db, /* Database handle */
@@ -3839,11 +3838,11 @@
3838 **
3839 ** ^The pointers returned are valid until a type conversion occurs as
3840 ** described above, or until [sqlite3_step()] or [sqlite3_reset()] or
3841 ** [sqlite3_finalize()] is called. ^The memory space used to hold strings
3842 ** and BLOBs is freed automatically. Do <b>not</b> pass the pointers returned
3843 ** from [sqlite3_column_blob()], [sqlite3_column_text()], etc. into
3844 ** [sqlite3_free()].
3845 **
3846 ** ^(If a memory allocation error occurs during the evaluation of any
3847 ** of these routines, a default value is returned. The default value
3848 ** is either the integer 0, the floating point number 0.0, or a NULL
@@ -4917,12 +4916,12 @@
4916 /*
4917 ** CAPI3REF: Free Memory Used By A Database Connection
4918 **
4919 ** ^The sqlite3_db_release_memory(D) interface attempts to free as much heap
4920 ** memory as possible from database connection D. Unlike the
4921 ** [sqlite3_release_memory()] interface, this interface is in effect even
4922 ** when the [SQLITE_ENABLE_MEMORY_MANAGEMENT] compile-time option is
4923 ** omitted.
4924 **
4925 ** See also: [sqlite3_release_memory()]
4926 */
4927 SQLITE_API int sqlite3_db_release_memory(sqlite3*);
4928

Keyboard Shortcuts

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