aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authordrh <>2023-03-28 11:18:04 +0000
committerdrh <>2023-03-28 11:18:04 +0000
commit3cbf38c7832e234aab1848e32c104fe345e19e4f (patch)
tree6821e5e42b3e349d1c92cd308aed7df4a655cf32 /src
parentfc6c3936aac93d7c47c2780b0254422737a23090 (diff)
downloadsqlite-3cbf38c7832e234aab1848e32c104fe345e19e4f.tar.gz
sqlite-3cbf38c7832e234aab1848e32c104fe345e19e4f.zip
Fix multiple problems with RETURNING on a DML statement against a view,
all inspired by [forum:/forumpost/dc3b92cfa0|forum post dc3b92cfa0]. (1) Do not allow a RETURNING clause to trick the code generator into thinking that the view being updated has an INSTEAD OF trigger. (2) Generate all result columns for a view in a DML statement. (3) The automatic covering index for a view should cover all result columns of the view. FossilOrigin-Name: c8bedef0d61731c29ae34de1594222d15b578f9e2cddbbd5b74fb3059644fe0f
Diffstat (limited to 'src')
-rw-r--r--src/delete.c8
-rw-r--r--src/insert.c4
-rw-r--r--src/sqliteInt.h2
-rw-r--r--src/trigger.c3
-rw-r--r--src/update.c2
-rw-r--r--src/where.c6
6 files changed, 17 insertions, 8 deletions
diff --git a/src/delete.c b/src/delete.c
index 22ef7ab65..27a554916 100644
--- a/src/delete.c
+++ b/src/delete.c
@@ -114,13 +114,15 @@ static int tabIsReadOnly(Parse *pParse, Table *pTab){
** If pTab is writable but other errors have occurred -> return 1.
** If pTab is writable and no prior errors -> return 0;
*/
-int sqlite3IsReadOnly(Parse *pParse, Table *pTab, int viewOk){
+int sqlite3IsReadOnly(Parse *pParse, Table *pTab, Trigger *pTrigger){
if( tabIsReadOnly(pParse, pTab) ){
sqlite3ErrorMsg(pParse, "table %s may not be modified", pTab->zName);
return 1;
}
#ifndef SQLITE_OMIT_VIEW
- if( !viewOk && IsView(pTab) ){
+ if( IsView(pTab)
+ && (pTrigger==0 || (pTrigger->bReturning && pTrigger->pNext==0))
+ ){
sqlite3ErrorMsg(pParse,"cannot modify %s because it is a view",pTab->zName);
return 1;
}
@@ -374,7 +376,7 @@ void sqlite3DeleteFrom(
goto delete_from_cleanup;
}
- if( sqlite3IsReadOnly(pParse, pTab, (pTrigger?1:0)) ){
+ if( sqlite3IsReadOnly(pParse, pTab, pTrigger) ){
goto delete_from_cleanup;
}
iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
diff --git a/src/insert.c b/src/insert.c
index 3abf715c9..423af7723 100644
--- a/src/insert.c
+++ b/src/insert.c
@@ -796,7 +796,7 @@ void sqlite3Insert(
/* Cannot insert into a read-only table.
*/
- if( sqlite3IsReadOnly(pParse, pTab, tmask) ){
+ if( sqlite3IsReadOnly(pParse, pTab, pTrigger) ){
goto insert_cleanup;
}
@@ -1243,7 +1243,7 @@ void sqlite3Insert(
}
/* Copy the new data already generated. */
- assert( pTab->nNVCol>0 );
+ assert( pTab->nNVCol>0 || pParse->nErr>0 );
sqlite3VdbeAddOp3(v, OP_Copy, regRowid+1, regCols+1, pTab->nNVCol-1);
#ifndef SQLITE_OMIT_GENERATED_COLUMNS
diff --git a/src/sqliteInt.h b/src/sqliteInt.h
index d6560c51b..dcf17d963 100644
--- a/src/sqliteInt.h
+++ b/src/sqliteInt.h
@@ -4801,7 +4801,7 @@ Select *sqlite3SelectNew(Parse*,ExprList*,SrcList*,Expr*,ExprList*,
Expr*,ExprList*,u32,Expr*);
void sqlite3SelectDelete(sqlite3*, Select*);
Table *sqlite3SrcListLookup(Parse*, SrcList*);
-int sqlite3IsReadOnly(Parse*, Table*, int);
+int sqlite3IsReadOnly(Parse*, Table*, Trigger*);
void sqlite3OpenTable(Parse*, int iCur, int iDb, Table*, int);
#if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY)
Expr *sqlite3LimitWhere(Parse*,SrcList*,Expr*,ExprList*,Expr*,char*);
diff --git a/src/trigger.c b/src/trigger.c
index d179d747a..bcb2132f0 100644
--- a/src/trigger.c
+++ b/src/trigger.c
@@ -1453,6 +1453,9 @@ u32 sqlite3TriggerColmask(
Trigger *p;
assert( isNew==1 || isNew==0 );
+ if( IsView(pTab) ){
+ return 0xffffffff;
+ }
for(p=pTrigger; p; p=p->pNext){
if( p->op==op
&& (tr_tm&p->tr_tm)
diff --git a/src/update.c b/src/update.c
index bd1ddb1a3..c1f136398 100644
--- a/src/update.c
+++ b/src/update.c
@@ -408,7 +408,7 @@ void sqlite3Update(
if( sqlite3ViewGetColumnNames(pParse, pTab) ){
goto update_cleanup;
}
- if( sqlite3IsReadOnly(pParse, pTab, tmask) ){
+ if( sqlite3IsReadOnly(pParse, pTab, pTrigger) ){
goto update_cleanup;
}
diff --git a/src/where.c b/src/where.c
index dd2b8b73c..509d14ff7 100644
--- a/src/where.c
+++ b/src/where.c
@@ -963,7 +963,11 @@ static SQLITE_NOINLINE void constructAutomaticIndex(
** original table changes and the index and table cannot both be used
** if they go out of sync.
*/
- extraCols = pSrc->colUsed & (~idxCols | MASKBIT(BMS-1));
+ if( IsView(pTable) ){
+ extraCols = ALLBITS;
+ }else{
+ extraCols = pSrc->colUsed & (~idxCols | MASKBIT(BMS-1));
+ }
mxBitCol = MIN(BMS-1,pTable->nCol);
testcase( pTable->nCol==BMS-1 );
testcase( pTable->nCol==BMS-2 );