aboutsummaryrefslogtreecommitdiff
path: root/src/insert.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/insert.c')
-rw-r--r--src/insert.c86
1 files changed, 31 insertions, 55 deletions
diff --git a/src/insert.c b/src/insert.c
index 728c06ed9..7afdd97c5 100644
--- a/src/insert.c
+++ b/src/insert.c
@@ -197,20 +197,21 @@ static int autoIncBegin(
){
int memId = 0; /* Register holding maximum rowid */
if( pTab->tabFlags & TF_Autoincrement ){
+ Parse *pRoot = (pParse->pRoot ? pParse->pRoot : pParse);
AutoincInfo *pInfo;
- pInfo = pParse->pAinc;
+ pInfo = pRoot->pAinc;
while( pInfo && pInfo->pTab!=pTab ){ pInfo = pInfo->pNext; }
if( pInfo==0 ){
pInfo = sqlite3DbMallocRaw(pParse->db, sizeof(*pInfo));
if( pInfo==0 ) return 0;
- pInfo->pNext = pParse->pAinc;
- pParse->pAinc = pInfo;
+ pInfo->pNext = pRoot->pAinc;
+ pRoot->pAinc = pInfo;
pInfo->pTab = pTab;
pInfo->iDb = iDb;
- pParse->nMem++; /* Register to hold name of table */
- pInfo->regCtr = ++pParse->nMem; /* Max rowid register */
- pParse->nMem++; /* Rowid in sqlite_sequence */
+ pRoot->nMem++; /* Register to hold name of table */
+ pInfo->regCtr = ++pRoot->nMem; /* Max rowid register */
+ pRoot->nMem++; /* Rowid in sqlite_sequence */
}
memId = pInfo->regCtr;
}
@@ -229,6 +230,9 @@ void sqlite3AutoincrementBegin(Parse *pParse){
int addr; /* A VDBE address */
Vdbe *v = pParse->pVdbe; /* VDBE under construction */
+ /* If currently generating a trigger program, this call is a no-op */
+ if( pParse->pTriggerTab ) return;
+
assert( v ); /* We failed long ago if this is not so */
for(p = pParse->pAinc; p; p = p->pNext){
pDb = &db->aDb[p->iDb];
@@ -536,11 +540,6 @@ void sqlite3Insert(
if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
sqlite3BeginWriteOperation(pParse, pSelect || pTrigger, iDb);
- /* if there are row triggers, allocate a temp table for new.* references. */
- if( pTrigger ){
- newIdx = pParse->nTab++;
- }
-
#ifndef SQLITE_OMIT_XFER_OPT
/* If the statement is of the form
**
@@ -744,12 +743,6 @@ void sqlite3Insert(
if( pColumn==0 && nColumn>0 ){
keyColumn = pTab->iPKey;
}
-
- /* Open the temp table for FOR EACH ROW triggers
- */
- if( pTrigger ){
- sqlite3VdbeAddOp3(v, OP_OpenPseudo, newIdx, 0, pTab->nCol);
- }
/* Initialize the count of rows to be inserted
*/
@@ -816,9 +809,7 @@ void sqlite3Insert(
*/
endOfLoop = sqlite3VdbeMakeLabel(v);
if( tmask & TRIGGER_BEFORE ){
- int regTrigRowid;
- int regCols;
- int regRec;
+ int regCols = sqlite3GetTempRange(pParse, pTab->nCol+1);
/* build the NEW.* reference row. Note that if there is an INTEGER
** PRIMARY KEY into which a NULL is being inserted, that NULL will be
@@ -826,31 +817,29 @@ void sqlite3Insert(
** we do not know what the unique ID will be (because the insert has
** not happened yet) so we substitute a rowid of -1
*/
- regTrigRowid = sqlite3GetTempReg(pParse);
if( keyColumn<0 ){
- sqlite3VdbeAddOp2(v, OP_Integer, -1, regTrigRowid);
+ sqlite3VdbeAddOp2(v, OP_Integer, -1, regCols);
}else{
int j1;
if( useTempTable ){
- sqlite3VdbeAddOp3(v, OP_Column, srcTab, keyColumn, regTrigRowid);
+ sqlite3VdbeAddOp3(v, OP_Column, srcTab, keyColumn, regCols);
}else{
assert( pSelect==0 ); /* Otherwise useTempTable is true */
- sqlite3ExprCode(pParse, pList->a[keyColumn].pExpr, regTrigRowid);
+ sqlite3ExprCode(pParse, pList->a[keyColumn].pExpr, regCols);
}
- j1 = sqlite3VdbeAddOp1(v, OP_NotNull, regTrigRowid);
- sqlite3VdbeAddOp2(v, OP_Integer, -1, regTrigRowid);
+ j1 = sqlite3VdbeAddOp1(v, OP_NotNull, regCols);
+ sqlite3VdbeAddOp2(v, OP_Integer, -1, regCols);
sqlite3VdbeJumpHere(v, j1);
- sqlite3VdbeAddOp1(v, OP_MustBeInt, regTrigRowid);
+ sqlite3VdbeAddOp1(v, OP_MustBeInt, regCols);
}
/* Cannot have triggers on a virtual table. If it were possible,
** this block would have to account for hidden column.
*/
- assert(!IsVirtual(pTab));
+ assert( !IsVirtual(pTab) );
/* Create the new column data
*/
- regCols = sqlite3GetTempRange(pParse, pTab->nCol);
for(i=0; i<pTab->nCol; i++){
if( pColumn==0 ){
j = i;
@@ -860,16 +849,14 @@ void sqlite3Insert(
}
}
if( pColumn && j>=pColumn->nId ){
- sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, regCols+i);
+ sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, regCols+i+1);
}else if( useTempTable ){
- sqlite3VdbeAddOp3(v, OP_Column, srcTab, j, regCols+i);
+ sqlite3VdbeAddOp3(v, OP_Column, srcTab, j, regCols+i+1);
}else{
assert( pSelect==0 ); /* Otherwise useTempTable is true */
- sqlite3ExprCodeAndCache(pParse, pList->a[j].pExpr, regCols+i);
+ sqlite3ExprCodeAndCache(pParse, pList->a[j].pExpr, regCols+i+1);
}
}
- regRec = sqlite3GetTempReg(pParse);
- sqlite3VdbeAddOp3(v, OP_MakeRecord, regCols, pTab->nCol, regRec);
/* If this is an INSERT on a view with an INSTEAD OF INSERT trigger,
** do not attempt any conversions before assembling the record.
@@ -877,18 +864,15 @@ void sqlite3Insert(
** table column affinities.
*/
if( !isView ){
+ sqlite3VdbeAddOp2(v, OP_Affinity, regCols+1, pTab->nCol);
sqlite3TableAffinityStr(v, pTab);
}
- sqlite3VdbeAddOp3(v, OP_Insert, newIdx, regRec, regTrigRowid);
- sqlite3ReleaseTempReg(pParse, regRec);
- sqlite3ReleaseTempReg(pParse, regTrigRowid);
- sqlite3ReleaseTempRange(pParse, regCols, pTab->nCol);
/* Fire BEFORE or INSTEAD OF triggers */
- if( sqlite3CodeRowTrigger(pParse, pTrigger, TK_INSERT, 0, TRIGGER_BEFORE,
- pTab, newIdx, -1, onError, endOfLoop, 0, 0) ){
- goto insert_cleanup;
- }
+ sqlite3CodeRowTrigger(pParse, pTrigger, TK_INSERT, 0, TRIGGER_BEFORE,
+ pTab, -1, regCols-pTab->nCol-1, onError, endOfLoop);
+
+ sqlite3ReleaseTempRange(pParse, regCols, pTab->nCol+1);
}
/* Push the record number for the new entry onto the stack. The
@@ -1009,10 +993,8 @@ void sqlite3Insert(
if( pTrigger ){
/* Code AFTER triggers */
- if( sqlite3CodeRowTrigger(pParse, pTrigger, TK_INSERT, 0, TRIGGER_AFTER,
- pTab, newIdx, -1, onError, endOfLoop, 0, 0) ){
- goto insert_cleanup;
- }
+ sqlite3CodeRowTrigger(pParse, pTrigger, TK_INSERT, 0, TRIGGER_AFTER,
+ pTab, -1, regData-2-pTab->nCol, onError, endOfLoop);
}
/* The bottom of the main insertion loop, if the data source
@@ -1041,7 +1023,7 @@ insert_end:
** maximum rowid counter values recorded while inserting into
** autoincrement tables.
*/
- if( pParse->nested==0 && pParse->trigStack==0 ){
+ if( pParse->nested==0 && pParse->pTriggerTab==0 ){
sqlite3AutoincrementEnd(pParse);
}
@@ -1050,7 +1032,7 @@ insert_end:
** generating code because of a call to sqlite3NestedParse(), do not
** invoke the callback function.
*/
- if( db->flags & SQLITE_CountRows && pParse->nested==0 && !pParse->trigStack ){
+ if( (db->flags&SQLITE_CountRows) && !pParse->nested && !pParse->pTriggerTab ){
sqlite3VdbeAddOp2(v, OP_ResultRow, regRowCount, 1);
sqlite3VdbeSetNumCols(v, 1);
sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows inserted", SQLITE_STATIC);
@@ -1171,7 +1153,6 @@ void sqlite3GenerateConstraintChecks(
nCol = pTab->nCol;
regData = regRowid + 1;
-
/* Test all NOT NULL constraints.
*/
for(i=0; i<nCol; i++){
@@ -1247,7 +1228,7 @@ void sqlite3GenerateConstraintChecks(
if( onError!=OE_Replace || pTab->pIndex ){
if( isUpdate ){
- j2 = sqlite3VdbeAddOp3(v, OP_Eq, regRowid, 0, regRowid-1);
+ j2 = sqlite3VdbeAddOp3(v, OP_Eq, regRowid, 0, rowidChng);
}
j3 = sqlite3VdbeAddOp3(v, OP_NotExists, baseCur, 0, regRowid);
switch( onError ){
@@ -1423,11 +1404,6 @@ void sqlite3CompleteInsertion(
sqlite3VdbeAddOp3(v, OP_MakeRecord, regData, pTab->nCol, regRec);
sqlite3TableAffinityStr(v, pTab);
sqlite3ExprCacheAffinityChange(pParse, regData, pTab->nCol);
-#ifndef SQLITE_OMIT_TRIGGER
- if( newIdx>=0 ){
- sqlite3VdbeAddOp3(v, OP_Insert, newIdx, regRec, regRowid);
- }
-#endif
if( pParse->nested ){
pik_flags = 0;
}else{