| 1 |
|
| /* |
| 2 |
|
| ** 2001 September 15 |
| 3 |
|
| ** |
| 4 |
|
| ** The author disclaims copyright to this source code. In place of |
| 5 |
|
| ** a legal notice, here is a blessing: |
| 6 |
|
| ** |
| 7 |
|
| ** May you do good and not evil. |
| 8 |
|
| ** May you find forgiveness for yourself and forgive others. |
| 9 |
|
| ** May you share freely, never taking more than you give. |
| 10 |
|
| ** |
| 11 |
|
| ************************************************************************* |
| 12 |
|
| ** This file contains C code routines that are called by the parser |
| 13 |
|
| ** to handle INSERT statements in SQLite. |
| 14 |
|
| ** |
| 15 |
|
| ** $Id: insert.c,v 1.1.1.1 2004/08/08 15:03:57 matt Exp $ |
| 16 |
|
| */ |
| 17 |
|
| #include "sqliteInt.h" |
| 18 |
|
| |
| 19 |
|
| /* |
| 20 |
|
| ** This routine is call to handle SQL of the following forms: |
| 21 |
|
| ** |
| 22 |
|
| ** insert into TABLE (IDLIST) values(EXPRLIST) |
| 23 |
|
| ** insert into TABLE (IDLIST) select |
| 24 |
|
| ** |
| 25 |
|
| ** The IDLIST following the table name is always optional. If omitted, |
| 26 |
|
| ** then a list of all columns for the table is substituted. The IDLIST |
| 27 |
|
| ** appears in the pColumn parameter. pColumn is NULL if IDLIST is omitted. |
| 28 |
|
| ** |
| 29 |
|
| ** The pList parameter holds EXPRLIST in the first form of the INSERT |
| 30 |
|
| ** statement above, and pSelect is NULL. For the second form, pList is |
| 31 |
|
| ** NULL and pSelect is a pointer to the select statement used to generate |
| 32 |
|
| ** data for the insert. |
| 33 |
|
| ** |
| 34 |
|
| ** The code generated follows one of three templates. For a simple |
| 35 |
|
| ** select with data coming from a VALUES clause, the code executes |
| 36 |
|
| ** once straight down through. The template looks like this: |
| 37 |
|
| ** |
| 38 |
|
| ** open write cursor to <table> and its indices |
| 39 |
|
| ** puts VALUES clause expressions onto the stack |
| 40 |
|
| ** write the resulting record into <table> |
| 41 |
|
| ** cleanup |
| 42 |
|
| ** |
| 43 |
|
| ** If the statement is of the form |
| 44 |
|
| ** |
| 45 |
|
| ** INSERT INTO <table> SELECT ... |
| 46 |
|
| ** |
| 47 |
|
| ** And the SELECT clause does not read from <table> at any time, then |
| 48 |
|
| ** the generated code follows this template: |
| 49 |
|
| ** |
| 50 |
|
| ** goto B |
| 51 |
|
| ** A: setup for the SELECT |
| 52 |
|
| ** loop over the tables in the SELECT |
| 53 |
|
| ** gosub C |
| 54 |
|
| ** end loop |
| 55 |
|
| ** cleanup after the SELECT |
| 56 |
|
| ** goto D |
| 57 |
|
| ** B: open write cursor to <table> and its indices |
| 58 |
|
| ** goto A |
| 59 |
|
| ** C: insert the select result into <table> |
| 60 |
|
| ** return |
| 61 |
|
| ** D: cleanup |
| 62 |
|
| ** |
| 63 |
|
| ** The third template is used if the insert statement takes its |
| 64 |
|
| ** values from a SELECT but the data is being inserted into a table |
| 65 |
|
| ** that is also read as part of the SELECT. In the third form, |
| 66 |
|
| ** we have to use a intermediate table to store the results of |
| 67 |
|
| ** the select. The template is like this: |
| 68 |
|
| ** |
| 69 |
|
| ** goto B |
| 70 |
|
| ** A: setup for the SELECT |
| 71 |
|
| ** loop over the tables in the SELECT |
| 72 |
|
| ** gosub C |
| 73 |
|
| ** end loop |
| 74 |
|
| ** cleanup after the SELECT |
| 75 |
|
| ** goto D |
| 76 |
|
| ** C: insert the select result into the intermediate table |
| 77 |
|
| ** return |
| 78 |
|
| ** B: open a cursor to an intermediate table |
| 79 |
|
| ** goto A |
| 80 |
|
| ** D: open write cursor to <table> and its indices |
| 81 |
|
| ** loop over the intermediate table |
| 82 |
|
| ** transfer values form intermediate table into <table> |
| 83 |
|
| ** end the loop |
| 84 |
|
| ** cleanup |
| 85 |
|
| */ |
| 86 |
44 |
| void sqliteInsert( |
| 87 |
|
| Parse *pParse, /* Parser context */ |
| 88 |
|
| SrcList *pTabList, /* Name of table into which we are inserting */ |
| 89 |
|
| ExprList *pList, /* List of values to be inserted */ |
| 90 |
|
| Select *pSelect, /* A SELECT statement to use as the data source */ |
| 91 |
|
| IdList *pColumn, /* Column names corresponding to IDLIST. */ |
| 92 |
|
| int onError /* How to handle constraint errors */ |
| 93 |
|
| ){ |
| 94 |
|
| Table *pTab; /* The table to insert into */ |
| 95 |
|
| char *zTab; /* Name of the table into which we are inserting */ |
| 96 |
|
| const char *zDb; /* Name of the database holding this table */ |
| 97 |
|
| int i, j, idx; /* Loop counters */ |
| 98 |
|
| Vdbe *v; /* Generate code into this virtual machine */ |
| 99 |
|
| Index *pIdx; /* For looping over indices of the table */ |
| 100 |
|
| int nColumn; /* Number of columns in the data */ |
| 101 |
|
| int base; /* VDBE Cursor number for pTab */ |
| 102 |
|
| int iCont, iBreak; /* Beginning and end of the loop over srcTab */ |
| 103 |
|
| sqlite *db; /* The main database structure */ |
| 104 |
44 |
| int keyColumn = -1; /* Column that is the INTEGER PRIMARY KEY */ |
| 105 |
|
| int endOfLoop; /* Label for the end of the insertion loop */ |
| 106 |
|
| int useTempTable; /* Store SELECT results in intermediate table */ |
| 107 |
|
| int srcTab; /* Data comes from this temporary cursor if >=0 */ |
| 108 |
|
| int iSelectLoop; /* Address of code that implements the SELECT */ |
| 109 |
|
| int iCleanup; /* Address of the cleanup code */ |
| 110 |
|
| int iInsertBlock; /* Address of the subroutine used to insert data */ |
| 111 |
|
| int iCntMem; /* Memory cell used for the row counter */ |
| 112 |
|
| int isView; /* True if attempting to insert into a view */ |
| 113 |
|
| |
| 114 |
44 |
| int row_triggers_exist = 0; /* True if there are FOR EACH ROW triggers */ |
| 115 |
|
| int before_triggers; /* True if there are BEFORE triggers */ |
| 116 |
|
| int after_triggers; /* True if there are AFTER triggers */ |
| 117 |
44 |
| int newIdx = -1; /* Cursor for the NEW table */ |
| 118 |
|
| |
| 119 |
44 |
▶ | if( pParse->nErr || sqlite_malloc_failed ) goto insert_cleanup; |
Branch: 2 branches
| Branch | True | False | Total |
| if( pParse->nErr || sqlite_malloc_failed ) goto insert_cleanup; |
44 |
0 |
44 |
| if( pParse->nErr || sqlite_malloc_failed ) goto insert_cleanup; |
0 |
44 |
44 |
|
| 120 |
44 |
| db = pParse->db; |
| 121 |
|
| |
| 122 |
|
| /* Locate the table into which we will be inserting new information. |
| 123 |
|
| */ |
| 124 |
|
| assert( pTabList->nSrc==1 ); |
| 125 |
44 |
| zTab = pTabList->a[0].zName; |
| 126 |
44 |
▶ | if( zTab==0 ) goto insert_cleanup; |
Branch: 1 branch
| Branch | True | False | Total |
| if( zTab==0 ) goto insert_cleanup; |
0 |
44 |
44 |
|
| 127 |
44 |
| pTab = sqliteSrcListLookup(pParse, pTabList); |
| 128 |
44 |
▶ | if( pTab==0 ){ |
Branch: 1 branch
| Branch | True | False | Total |
| if( pTab==0 ){ |
0 |
44 |
44 |
|
| 129 |
0 |
| goto insert_cleanup; |
| 130 |
|
| } |
| 131 |
|
| assert( pTab->iDb<db->nDb ); |
| 132 |
44 |
| zDb = db->aDb[pTab->iDb].zName; |
| 133 |
44 |
▶ | if( sqliteAuthCheck(pParse, SQLITE_INSERT, pTab->zName, 0, zDb) ){ |
Branch: 1 branch
| Branch | True | False | Total |
| if( sqliteAuthCheck(pParse, SQLITE_INSERT, pTab->zName, 0, zDb) ){ |
0 |
44 |
44 |
|
| 134 |
0 |
| goto insert_cleanup; |
| 135 |
|
| } |
| 136 |
|
| |
| 137 |
|
| /* Ensure that: |
| 138 |
|
| * (a) the table is not read-only, |
| 139 |
|
| * (b) that if it is a view then ON INSERT triggers exist |
| 140 |
|
| */ |
| 141 |
44 |
| before_triggers = sqliteTriggersExist(pParse, pTab->pTrigger, TK_INSERT, |
| 142 |
|
| TK_BEFORE, TK_ROW, 0); |
| 143 |
44 |
| after_triggers = sqliteTriggersExist(pParse, pTab->pTrigger, TK_INSERT, |
| 144 |
|
| TK_AFTER, TK_ROW, 0); |
| 145 |
44 |
▶ | row_triggers_exist = before_triggers || after_triggers; |
Branch: 2 branches
| Branch | True | False | Total |
| row_triggers_exist = before_triggers || after_triggers; |
44 |
0 |
44 |
| row_triggers_exist = before_triggers || after_triggers; |
0 |
44 |
44 |
|
| 146 |
44 |
| isView = pTab->pSelect!=0; |
| 147 |
44 |
▶ | if( sqliteIsReadOnly(pParse, pTab, before_triggers) ){ |
Branch: 1 branch
| Branch | True | False | Total |
| if( sqliteIsReadOnly(pParse, pTab, before_triggers) ){ |
0 |
44 |
44 |
|
| 148 |
0 |
| goto insert_cleanup; |
| 149 |
|
| } |
| 150 |
44 |
▶ | if( pTab==0 ) goto insert_cleanup; |
Branch: 1 branch
| Branch | True | False | Total |
| if( pTab==0 ) goto insert_cleanup; |
0 |
44 |
44 |
|
| 151 |
|
| |
| 152 |
|
| /* If pTab is really a view, make sure it has been initialized. |
| 153 |
|
| */ |
| 154 |
44 |
▶ | if( isView && sqliteViewGetColumnNames(pParse, pTab) ){ |
Branch: 2 branches
| Branch | True | False | Total |
| if( isView && sqliteViewGetColumnNames(pParse, pTab) ){ |
0 |
44 |
44 |
| if( isView && sqliteViewGetColumnNames(pParse, pTab) ){ |
0 |
0 |
0 |
|
| 155 |
0 |
| goto insert_cleanup; |
| 156 |
|
| } |
| 157 |
|
| |
| 158 |
|
| /* Allocate a VDBE |
| 159 |
|
| */ |
| 160 |
44 |
| v = sqliteGetVdbe(pParse); |
| 161 |
44 |
▶ | if( v==0 ) goto insert_cleanup; |
Branch: 1 branch
| Branch | True | False | Total |
| if( v==0 ) goto insert_cleanup; |
0 |
44 |
44 |
|
| 162 |
44 |
▶ | sqliteBeginWriteOperation(pParse, pSelect || row_triggers_exist, pTab->iDb); |
Branch: 2 branches
| Branch | True | False | Total |
| sqliteBeginWriteOperation(pParse, pSelect || row_triggers_exist, pTab->iDb); |
44 |
0 |
44 |
| sqliteBeginWriteOperation(pParse, pSelect || row_triggers_exist, pTab->iDb); |
0 |
44 |
44 |
|
| 163 |
|
| |
| 164 |
|
| /* if there are row triggers, allocate a temp table for new.* references. */ |
| 165 |
44 |
▶ | if( row_triggers_exist ){ |
Branch: 1 branch
| Branch | True | False | Total |
| if( row_triggers_exist ){ |
0 |
44 |
44 |
|
| 166 |
0 |
| newIdx = pParse->nTab++; |
| 167 |
|
| } |
| 168 |
|
| |
| 169 |
|
| /* Figure out how many columns of data are supplied. If the data |
| 170 |
|
| ** is coming from a SELECT statement, then this step also generates |
| 171 |
|
| ** all the code to implement the SELECT statement and invoke a subroutine |
| 172 |
|
| ** to process each row of the result. (Template 2.) If the SELECT |
| 173 |
|
| ** statement uses the the table that is being inserted into, then the |
| 174 |
|
| ** subroutine is also coded here. That subroutine stores the SELECT |
| 175 |
|
| ** results in a temporary table. (Template 3.) |
| 176 |
|
| */ |
| 177 |
44 |
▶ | if( pSelect ){ |
Branch: 1 branch
| Branch | True | False | Total |
| if( pSelect ){ |
0 |
44 |
44 |
|
| 178 |
|
| /* Data is coming from a SELECT. Generate code to implement that SELECT |
| 179 |
|
| */ |
| 180 |
|
| int rc, iInitCode; |
| 181 |
0 |
| iInitCode = sqliteVdbeAddOp(v, OP_Goto, 0, 0); |
| 182 |
0 |
| iSelectLoop = sqliteVdbeCurrentAddr(v); |
| 183 |
0 |
| iInsertBlock = sqliteVdbeMakeLabel(v); |
| 184 |
0 |
| rc = sqliteSelect(pParse, pSelect, SRT_Subroutine, iInsertBlock, 0,0,0); |
| 185 |
0 |
▶ | if( rc || pParse->nErr || sqlite_malloc_failed ) goto insert_cleanup; |
Branch: 3 branches
| Branch | True | False | Total |
| if( rc || pParse->nErr || sqlite_malloc_failed ) goto insert_cleanup; |
0 |
0 |
0 |
| if( rc || pParse->nErr || sqlite_malloc_failed ) goto insert_cleanup; |
0 |
0 |
0 |
| if( rc || pParse->nErr || sqlite_malloc_failed ) goto insert_cleanup; |
0 |
0 |
0 |
|
| 186 |
0 |
| iCleanup = sqliteVdbeMakeLabel(v); |
| 187 |
0 |
| sqliteVdbeAddOp(v, OP_Goto, 0, iCleanup); |
| 188 |
|
| assert( pSelect->pEList ); |
| 189 |
0 |
| nColumn = pSelect->pEList->nExpr; |
| 190 |
|
| |
| 191 |
|
| /* Set useTempTable to TRUE if the result of the SELECT statement |
| 192 |
|
| ** should be written into a temporary table. Set to FALSE if each |
| 193 |
|
| ** row of the SELECT can be written directly into the result table. |
| 194 |
|
| ** |
| 195 |
|
| ** A temp table must be used if the table being updated is also one |
| 196 |
|
| ** of the tables being read by the SELECT statement. Also use a |
| 197 |
|
| ** temp table in the case of row triggers. |
| 198 |
|
| */ |
| 199 |
0 |
▶ | if( row_triggers_exist ){ |
Branch: 1 branch
| Branch | True | False | Total |
| if( row_triggers_exist ){ |
0 |
0 |
0 |
|
| 200 |
0 |
| useTempTable = 1; |
| 201 |
|
| }else{ |
| 202 |
0 |
| int addr = sqliteVdbeFindOp(v, OP_OpenRead, pTab->tnum); |
| 203 |
0 |
| useTempTable = 0; |
| 204 |
0 |
▶ | if( addr>0 ){ |
Branch: 1 branch
| Branch | True | False | Total |
| if( addr>0 ){ |
0 |
0 |
0 |
|
| 205 |
0 |
| VdbeOp *pOp = sqliteVdbeGetOp(v, addr-2); |
| 206 |
0 |
▶ | if( pOp->opcode==OP_Integer && pOp->p1==pTab->iDb ){ |
Branch: 2 branches
| Branch | True | False | Total |
| if( pOp->opcode==OP_Integer && pOp->p1==pTab->iDb ){ |
0 |
0 |
0 |
| if( pOp->opcode==OP_Integer && pOp->p1==pTab->iDb ){ |
0 |
0 |
0 |
|
| 207 |
0 |
| useTempTable = 1; |
| 208 |
|
| } |
| 209 |
|
| } |
| 210 |
|
| } |
| 211 |
|
| |
| 212 |
0 |
▶ | if( useTempTable ){ |
Branch: 1 branch
| Branch | True | False | Total |
| if( useTempTable ){ |
0 |
0 |
0 |
|
| 213 |
|
| /* Generate the subroutine that SELECT calls to process each row of |
| 214 |
|
| ** the result. Store the result in a temporary table |
| 215 |
|
| */ |
| 216 |
0 |
| srcTab = pParse->nTab++; |
| 217 |
0 |
| sqliteVdbeResolveLabel(v, iInsertBlock); |
| 218 |
0 |
| sqliteVdbeAddOp(v, OP_MakeRecord, nColumn, 0); |
| 219 |
0 |
| sqliteVdbeAddOp(v, OP_NewRecno, srcTab, 0); |
| 220 |
0 |
| sqliteVdbeAddOp(v, OP_Pull, 1, 0); |
| 221 |
0 |
| sqliteVdbeAddOp(v, OP_PutIntKey, srcTab, 0); |
| 222 |
0 |
| sqliteVdbeAddOp(v, OP_Return, 0, 0); |
| 223 |
|
| |
| 224 |
|
| /* The following code runs first because the GOTO at the very top |
| 225 |
|
| ** of the program jumps to it. Create the temporary table, then jump |
| 226 |
|
| ** back up and execute the SELECT code above. |
| 227 |
|
| */ |
| 228 |
0 |
| sqliteVdbeChangeP2(v, iInitCode, sqliteVdbeCurrentAddr(v)); |
| 229 |
0 |
| sqliteVdbeAddOp(v, OP_OpenTemp, srcTab, 0); |
| 230 |
0 |
| sqliteVdbeAddOp(v, OP_Goto, 0, iSelectLoop); |
| 231 |
0 |
| sqliteVdbeResolveLabel(v, iCleanup); |
| 232 |
|
| }else{ |
| 233 |
0 |
| sqliteVdbeChangeP2(v, iInitCode, sqliteVdbeCurrentAddr(v)); |
| 234 |
|
| } |
| 235 |
|
| }else{ |
| 236 |
|
| /* This is the case if the data for the INSERT is coming from a VALUES |
| 237 |
|
| ** clause |
| 238 |
|
| */ |
| 239 |
|
| SrcList dummy; |
| 240 |
|
| assert( pList!=0 ); |
| 241 |
44 |
| srcTab = -1; |
| 242 |
44 |
| useTempTable = 0; |
| 243 |
|
| assert( pList ); |
| 244 |
44 |
| nColumn = pList->nExpr; |
| 245 |
44 |
| dummy.nSrc = 0; |
| 246 |
139 |
▶ | for(i=0; i<nColumn; i++){ |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0; i<nColumn; i++){ |
95 |
44 |
139 |
|
| 247 |
95 |
▶ | if( sqliteExprResolveIds(pParse, &dummy, 0, pList->a[i].pExpr) ){ |
Branch: 1 branch
| Branch | True | False | Total |
| if( sqliteExprResolveIds(pParse, &dummy, 0, pList->a[i].pExpr) ){ |
0 |
95 |
95 |
|
| 248 |
0 |
| goto insert_cleanup; |
| 249 |
|
| } |
| 250 |
95 |
▶ | if( sqliteExprCheck(pParse, pList->a[i].pExpr, 0, 0) ){ |
Branch: 1 branch
| Branch | True | False | Total |
| if( sqliteExprCheck(pParse, pList->a[i].pExpr, 0, 0) ){ |
0 |
95 |
95 |
|
| 251 |
0 |
| goto insert_cleanup; |
| 252 |
|
| } |
| 253 |
|
| } |
| 254 |
|
| } |
| 255 |
|
| |
| 256 |
|
| /* Make sure the number of columns in the source data matches the number |
| 257 |
|
| ** of columns to be inserted into the table. |
| 258 |
|
| */ |
| 259 |
44 |
▶ | if( pColumn==0 && nColumn!=pTab->nCol ){ |
Branch: 2 branches
| Branch | True | False | Total |
| if( pColumn==0 && nColumn!=pTab->nCol ){ |
41 |
3 |
44 |
| if( pColumn==0 && nColumn!=pTab->nCol ){ |
0 |
41 |
41 |
|
| 260 |
0 |
| sqliteErrorMsg(pParse, |
| 261 |
|
| "table %S has %d columns but %d values were supplied", |
| 262 |
|
| pTabList, 0, pTab->nCol, nColumn); |
| 263 |
0 |
| goto insert_cleanup; |
| 264 |
|
| } |
| 265 |
44 |
▶ | if( pColumn!=0 && nColumn!=pColumn->nId ){ |
Branch: 2 branches
| Branch | True | False | Total |
| if( pColumn!=0 && nColumn!=pColumn->nId ){ |
3 |
41 |
44 |
| if( pColumn!=0 && nColumn!=pColumn->nId ){ |
0 |
3 |
3 |
|
| 266 |
0 |
| sqliteErrorMsg(pParse, "%d values for %d columns", nColumn, pColumn->nId); |
| 267 |
0 |
| goto insert_cleanup; |
| 268 |
|
| } |
| 269 |
|
| |
| 270 |
|
| /* If the INSERT statement included an IDLIST term, then make sure |
| 271 |
|
| ** all elements of the IDLIST really are columns of the table and |
| 272 |
|
| ** remember the column indices. |
| 273 |
|
| ** |
| 274 |
|
| ** If the table has an INTEGER PRIMARY KEY column and that column |
| 275 |
|
| ** is named in the IDLIST, then record in the keyColumn variable |
| 276 |
|
| ** the index into IDLIST of the primary key column. keyColumn is |
| 277 |
|
| ** the index of the primary key as it appears in IDLIST, not as |
| 278 |
|
| ** is appears in the original table. (The index of the primary |
| 279 |
|
| ** key in the original table is pTab->iPKey.) |
| 280 |
|
| */ |
| 281 |
44 |
▶ | if( pColumn ){ |
Branch: 1 branch
| Branch | True | False | Total |
| if( pColumn ){ |
3 |
41 |
44 |
|
| 282 |
9 |
▶ | for(i=0; i<pColumn->nId; i++){ |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0; i<pColumn->nId; i++){ |
6 |
3 |
9 |
|
| 283 |
6 |
| pColumn->a[i].idx = -1; |
| 284 |
|
| } |
| 285 |
9 |
▶ | for(i=0; i<pColumn->nId; i++){ |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0; i<pColumn->nId; i++){ |
6 |
3 |
9 |
|
| 286 |
9 |
▶ | for(j=0; j<pTab->nCol; j++){ |
Branch: 1 branch
| Branch | True | False | Total |
| for(j=0; j<pTab->nCol; j++){ |
9 |
0 |
9 |
|
| 287 |
9 |
▶ | if( sqliteStrICmp(pColumn->a[i].zName, pTab->aCol[j].zName)==0 ){ |
Branch: 1 branch
| Branch | True | False | Total |
| if( sqliteStrICmp(pColumn->a[i].zName, pTab->aCol[j].zName)==0 ){ |
6 |
3 |
9 |
|
| 288 |
6 |
| pColumn->a[i].idx = j; |
| 289 |
6 |
▶ | if( j==pTab->iPKey ){ |
Branch: 1 branch
| Branch | True | False | Total |
| if( j==pTab->iPKey ){ |
0 |
6 |
6 |
|
| 290 |
0 |
| keyColumn = i; |
| 291 |
|
| } |
| 292 |
6 |
| break; |
| 293 |
|
| } |
| 294 |
|
| } |
| 295 |
6 |
▶ | if( j>=pTab->nCol ){ |
Branch: 1 branch
| Branch | True | False | Total |
| if( j>=pTab->nCol ){ |
0 |
6 |
6 |
|
| 296 |
0 |
▶ | if( sqliteIsRowid(pColumn->a[i].zName) ){ |
Branch: 1 branch
| Branch | True | False | Total |
| if( sqliteIsRowid(pColumn->a[i].zName) ){ |
0 |
0 |
0 |
|
| 297 |
0 |
| keyColumn = i; |
| 298 |
|
| }else{ |
| 299 |
0 |
| sqliteErrorMsg(pParse, "table %S has no column named %s", |
| 300 |
0 |
| pTabList, 0, pColumn->a[i].zName); |
| 301 |
0 |
| pParse->nErr++; |
| 302 |
0 |
| goto insert_cleanup; |
| 303 |
|
| } |
| 304 |
|
| } |
| 305 |
|
| } |
| 306 |
|
| } |
| 307 |
|
| |
| 308 |
|
| /* If there is no IDLIST term but the table has an integer primary |
| 309 |
|
| ** key, the set the keyColumn variable to the primary key column index |
| 310 |
|
| ** in the original table definition. |
| 311 |
|
| */ |
| 312 |
44 |
▶ | if( pColumn==0 ){ |
Branch: 1 branch
| Branch | True | False | Total |
| if( pColumn==0 ){ |
41 |
3 |
44 |
|
| 313 |
41 |
| keyColumn = pTab->iPKey; |
| 314 |
|
| } |
| 315 |
|
| |
| 316 |
|
| /* Open the temp table for FOR EACH ROW triggers |
| 317 |
|
| */ |
| 318 |
44 |
▶ | if( row_triggers_exist ){ |
Branch: 1 branch
| Branch | True | False | Total |
| if( row_triggers_exist ){ |
0 |
44 |
44 |
|
| 319 |
0 |
| sqliteVdbeAddOp(v, OP_OpenPseudo, newIdx, 0); |
| 320 |
|
| } |
| 321 |
|
| |
| 322 |
|
| /* Initialize the count of rows to be inserted |
| 323 |
|
| */ |
| 324 |
44 |
▶ | if( db->flags & SQLITE_CountRows ){ |
Branch: 1 branch
| Branch | True | False | Total |
| if( db->flags & SQLITE_CountRows ){ |
0 |
44 |
44 |
|
| 325 |
0 |
| iCntMem = pParse->nMem++; |
| 326 |
0 |
| sqliteVdbeAddOp(v, OP_Integer, 0, 0); |
| 327 |
0 |
| sqliteVdbeAddOp(v, OP_MemStore, iCntMem, 1); |
| 328 |
|
| } |
| 329 |
|
| |
| 330 |
|
| /* Open tables and indices if there are no row triggers */ |
| 331 |
44 |
▶ | if( !row_triggers_exist ){ |
Branch: 1 branch
| Branch | True | False | Total |
| if( !row_triggers_exist ){ |
44 |
0 |
44 |
|
| 332 |
44 |
| base = pParse->nTab; |
| 333 |
44 |
| idx = sqliteOpenTableAndIndices(pParse, pTab, base); |
| 334 |
44 |
| pParse->nTab += idx; |
| 335 |
|
| } |
| 336 |
|
| |
| 337 |
|
| /* If the data source is a temporary table, then we have to create |
| 338 |
|
| ** a loop because there might be multiple rows of data. If the data |
| 339 |
|
| ** source is a subroutine call from the SELECT statement, then we need |
| 340 |
|
| ** to launch the SELECT statement processing. |
| 341 |
|
| */ |
| 342 |
44 |
▶ | if( useTempTable ){ |
Branch: 1 branch
| Branch | True | False | Total |
| if( useTempTable ){ |
0 |
44 |
44 |
|
| 343 |
0 |
| iBreak = sqliteVdbeMakeLabel(v); |
| 344 |
0 |
| sqliteVdbeAddOp(v, OP_Rewind, srcTab, iBreak); |
| 345 |
0 |
| iCont = sqliteVdbeCurrentAddr(v); |
| 346 |
44 |
▶ | }else if( pSelect ){ |
Branch: 1 branch
| Branch | True | False | Total |
| }else if( pSelect ){ |
0 |
44 |
44 |
|
| 347 |
0 |
| sqliteVdbeAddOp(v, OP_Goto, 0, iSelectLoop); |
| 348 |
0 |
| sqliteVdbeResolveLabel(v, iInsertBlock); |
| 349 |
|
| } |
| 350 |
|
| |
| 351 |
|
| /* Run the BEFORE and INSTEAD OF triggers, if there are any |
| 352 |
|
| */ |
| 353 |
44 |
| endOfLoop = sqliteVdbeMakeLabel(v); |
| 354 |
44 |
▶ | if( before_triggers ){ |
Branch: 1 branch
| Branch | True | False | Total |
| if( before_triggers ){ |
0 |
44 |
44 |
|
| 355 |
|
| |
| 356 |
|
| /* build the NEW.* reference row. Note that if there is an INTEGER |
| 357 |
|
| ** PRIMARY KEY into which a NULL is being inserted, that NULL will be |
| 358 |
|
| ** translated into a unique ID for the row. But on a BEFORE trigger, |
| 359 |
|
| ** we do not know what the unique ID will be (because the insert has |
| 360 |
|
| ** not happened yet) so we substitute a rowid of -1 |
| 361 |
|
| */ |
| 362 |
0 |
▶ | if( keyColumn<0 ){ |
Branch: 1 branch
| Branch | True | False | Total |
| if( keyColumn<0 ){ |
0 |
0 |
0 |
|
| 363 |
0 |
| sqliteVdbeAddOp(v, OP_Integer, -1, 0); |
| 364 |
0 |
▶ | }else if( useTempTable ){ |
Branch: 1 branch
| Branch | True | False | Total |
| }else if( useTempTable ){ |
0 |
0 |
0 |
|
| 365 |
0 |
| sqliteVdbeAddOp(v, OP_Column, srcTab, keyColumn); |
| 366 |
0 |
▶ | }else if( pSelect ){ |
Branch: 1 branch
| Branch | True | False | Total |
| }else if( pSelect ){ |
0 |
0 |
0 |
|
| 367 |
0 |
| sqliteVdbeAddOp(v, OP_Dup, nColumn - keyColumn - 1, 1); |
| 368 |
|
| }else{ |
| 369 |
0 |
| sqliteExprCode(pParse, pList->a[keyColumn].pExpr); |
| 370 |
0 |
| sqliteVdbeAddOp(v, OP_NotNull, -1, sqliteVdbeCurrentAddr(v)+3); |
| 371 |
0 |
| sqliteVdbeAddOp(v, OP_Pop, 1, 0); |
| 372 |
0 |
| sqliteVdbeAddOp(v, OP_Integer, -1, 0); |
| 373 |
0 |
| sqliteVdbeAddOp(v, OP_MustBeInt, 0, 0); |
| 374 |
|
| } |
| 375 |
|
| |
| 376 |
|
| /* Create the new column data |
| 377 |
|
| */ |
| 378 |
0 |
▶ | for(i=0; i<pTab->nCol; i++){ |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0; i<pTab->nCol; i++){ |
0 |
0 |
0 |
|
| 379 |
0 |
▶ | if( pColumn==0 ){ |
Branch: 1 branch
| Branch | True | False | Total |
| if( pColumn==0 ){ |
0 |
0 |
0 |
|
| 380 |
0 |
| j = i; |
| 381 |
|
| }else{ |
| 382 |
0 |
▶ | for(j=0; j<pColumn->nId; j++){ |
Branch: 1 branch
| Branch | True | False | Total |
| for(j=0; j<pColumn->nId; j++){ |
0 |
0 |
0 |
|
| 383 |
0 |
▶ | if( pColumn->a[j].idx==i ) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if( pColumn->a[j].idx==i ) break; |
0 |
0 |
0 |
|
| 384 |
|
| } |
| 385 |
|
| } |
| 386 |
0 |
▶ | if( pColumn && j>=pColumn->nId ){ |
Branch: 2 branches
| Branch | True | False | Total |
| if( pColumn && j>=pColumn->nId ){ |
0 |
0 |
0 |
| if( pColumn && j>=pColumn->nId ){ |
0 |
0 |
0 |
|
| 387 |
0 |
| sqliteVdbeOp3(v, OP_String, 0, 0, pTab->aCol[i].zDflt, P3_STATIC); |
| 388 |
0 |
▶ | }else if( useTempTable ){ |
Branch: 1 branch
| Branch | True | False | Total |
| }else if( useTempTable ){ |
0 |
0 |
0 |
|
| 389 |
0 |
| sqliteVdbeAddOp(v, OP_Column, srcTab, j); |
| 390 |
0 |
▶ | }else if( pSelect ){ |
Branch: 1 branch
| Branch | True | False | Total |
| }else if( pSelect ){ |
0 |
0 |
0 |
|
| 391 |
0 |
| sqliteVdbeAddOp(v, OP_Dup, nColumn-j-1, 1); |
| 392 |
|
| }else{ |
| 393 |
0 |
| sqliteExprCode(pParse, pList->a[j].pExpr); |
| 394 |
|
| } |
| 395 |
|
| } |
| 396 |
0 |
| sqliteVdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0); |
| 397 |
0 |
| sqliteVdbeAddOp(v, OP_PutIntKey, newIdx, 0); |
| 398 |
|
| |
| 399 |
|
| /* Fire BEFORE or INSTEAD OF triggers */ |
| 400 |
0 |
▶ | if( sqliteCodeRowTrigger(pParse, TK_INSERT, 0, TK_BEFORE, pTab, |
Branch: 1 branch
| Branch | True | False | Total |
| if( sqliteCodeRowTrigger(pParse, TK_INSERT, 0, TK_BEFORE, pTab, |
0 |
0 |
0 |
|
| 401 |
|
| newIdx, -1, onError, endOfLoop) ){ |
| 402 |
0 |
| goto insert_cleanup; |
| 403 |
|
| } |
| 404 |
|
| } |
| 405 |
|
| |
| 406 |
|
| /* If any triggers exists, the opening of tables and indices is deferred |
| 407 |
|
| ** until now. |
| 408 |
|
| */ |
| 409 |
44 |
▶ | if( row_triggers_exist && !isView ){ |
Branch: 2 branches
| Branch | True | False | Total |
| if( row_triggers_exist && !isView ){ |
0 |
44 |
44 |
| if( row_triggers_exist && !isView ){ |
0 |
0 |
0 |
|
| 410 |
0 |
| base = pParse->nTab; |
| 411 |
0 |
| idx = sqliteOpenTableAndIndices(pParse, pTab, base); |
| 412 |
0 |
| pParse->nTab += idx; |
| 413 |
|
| } |
| 414 |
|
| |
| 415 |
|
| /* Push the record number for the new entry onto the stack. The |
| 416 |
|
| ** record number is a randomly generate integer created by NewRecno |
| 417 |
|
| ** except when the table has an INTEGER PRIMARY KEY column, in which |
| 418 |
|
| ** case the record number is the same as that column. |
| 419 |
|
| */ |
| 420 |
44 |
▶ | if( !isView ){ |
Branch: 1 branch
| Branch | True | False | Total |
| if( !isView ){ |
44 |
0 |
44 |
|
| 421 |
44 |
▶ | if( keyColumn>=0 ){ |
Branch: 1 branch
| Branch | True | False | Total |
| if( keyColumn>=0 ){ |
0 |
44 |
44 |
|
| 422 |
0 |
▶ | if( useTempTable ){ |
Branch: 1 branch
| Branch | True | False | Total |
| if( useTempTable ){ |
0 |
0 |
0 |
|
| 423 |
0 |
| sqliteVdbeAddOp(v, OP_Column, srcTab, keyColumn); |
| 424 |
0 |
▶ | }else if( pSelect ){ |
Branch: 1 branch
| Branch | True | False | Total |
| }else if( pSelect ){ |
0 |
0 |
0 |
|
| 425 |
0 |
| sqliteVdbeAddOp(v, OP_Dup, nColumn - keyColumn - 1, 1); |
| 426 |
|
| }else{ |
| 427 |
0 |
| sqliteExprCode(pParse, pList->a[keyColumn].pExpr); |
| 428 |
|
| } |
| 429 |
|
| /* If the PRIMARY KEY expression is NULL, then use OP_NewRecno |
| 430 |
|
| ** to generate a unique primary key value. |
| 431 |
|
| */ |
| 432 |
0 |
| sqliteVdbeAddOp(v, OP_NotNull, -1, sqliteVdbeCurrentAddr(v)+3); |
| 433 |
0 |
| sqliteVdbeAddOp(v, OP_Pop, 1, 0); |
| 434 |
0 |
| sqliteVdbeAddOp(v, OP_NewRecno, base, 0); |
| 435 |
0 |
| sqliteVdbeAddOp(v, OP_MustBeInt, 0, 0); |
| 436 |
|
| }else{ |
| 437 |
44 |
| sqliteVdbeAddOp(v, OP_NewRecno, base, 0); |
| 438 |
|
| } |
| 439 |
|
| |
| 440 |
|
| /* Push onto the stack, data for all columns of the new entry, beginning |
| 441 |
|
| ** with the first column. |
| 442 |
|
| */ |
| 443 |
139 |
▶ | for(i=0; i<pTab->nCol; i++){ |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0; i<pTab->nCol; i++){ |
95 |
44 |
139 |
|
| 444 |
95 |
▶ | if( i==pTab->iPKey ){ |
Branch: 1 branch
| Branch | True | False | Total |
| if( i==pTab->iPKey ){ |
0 |
95 |
95 |
|
| 445 |
|
| /* The value of the INTEGER PRIMARY KEY column is always a NULL. |
| 446 |
|
| ** Whenever this column is read, the record number will be substituted |
| 447 |
|
| ** in its place. So will fill this column with a NULL to avoid |
| 448 |
|
| ** taking up data space with information that will never be used. */ |
| 449 |
0 |
| sqliteVdbeAddOp(v, OP_String, 0, 0); |
| 450 |
0 |
| continue; |
| 451 |
|
| } |
| 452 |
95 |
▶ | if( pColumn==0 ){ |
Branch: 1 branch
| Branch | True | False | Total |
| if( pColumn==0 ){ |
89 |
6 |
95 |
|
| 453 |
89 |
| j = i; |
| 454 |
|
| }else{ |
| 455 |
9 |
▶ | for(j=0; j<pColumn->nId; j++){ |
Branch: 1 branch
| Branch | True | False | Total |
| for(j=0; j<pColumn->nId; j++){ |
9 |
0 |
9 |
|
| 456 |
9 |
▶ | if( pColumn->a[j].idx==i ) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if( pColumn->a[j].idx==i ) break; |
6 |
3 |
9 |
|
| 457 |
|
| } |
| 458 |
|
| } |
| 459 |
95 |
▶ | if( pColumn && j>=pColumn->nId ){ |
Branch: 2 branches
| Branch | True | False | Total |
| if( pColumn && j>=pColumn->nId ){ |
6 |
89 |
95 |
| if( pColumn && j>=pColumn->nId ){ |
0 |
6 |
6 |
|
| 460 |
0 |
| sqliteVdbeOp3(v, OP_String, 0, 0, pTab->aCol[i].zDflt, P3_STATIC); |
| 461 |
95 |
▶ | }else if( useTempTable ){ |
Branch: 1 branch
| Branch | True | False | Total |
| }else if( useTempTable ){ |
0 |
95 |
95 |
|
| 462 |
0 |
| sqliteVdbeAddOp(v, OP_Column, srcTab, j); |
| 463 |
95 |
▶ | }else if( pSelect ){ |
Branch: 1 branch
| Branch | True | False | Total |
| }else if( pSelect ){ |
0 |
95 |
95 |
|
| 464 |
0 |
| sqliteVdbeAddOp(v, OP_Dup, i+nColumn-j, 1); |
| 465 |
|
| }else{ |
| 466 |
95 |
| sqliteExprCode(pParse, pList->a[j].pExpr); |
| 467 |
|
| } |
| 468 |
|
| } |
| 469 |
|
| |
| 470 |
|
| /* Generate code to check constraints and generate index keys and |
| 471 |
|
| ** do the insertion. |
| 472 |
|
| */ |
| 473 |
44 |
| sqliteGenerateConstraintChecks(pParse, pTab, base, 0, keyColumn>=0, |
| 474 |
|
| 0, onError, endOfLoop); |
| 475 |
44 |
▶ | sqliteCompleteInsertion(pParse, pTab, base, 0,0,0, |
Branch: 1 branch
| Branch | True | False | Total |
| sqliteCompleteInsertion(pParse, pTab, base, 0,0,0, |
0 |
44 |
44 |
|
| 476 |
|
| after_triggers ? newIdx : -1); |
| 477 |
|
| } |
| 478 |
|
| |
| 479 |
|
| /* Update the count of rows that are inserted |
| 480 |
|
| */ |
| 481 |
44 |
▶ | if( (db->flags & SQLITE_CountRows)!=0 ){ |
Branch: 1 branch
| Branch | True | False | Total |
| if( (db->flags & SQLITE_CountRows)!=0 ){ |
0 |
44 |
44 |
|
| 482 |
0 |
| sqliteVdbeAddOp(v, OP_MemIncr, iCntMem, 0); |
| 483 |
|
| } |
| 484 |
|
| |
| 485 |
44 |
▶ | if( row_triggers_exist ){ |
Branch: 1 branch
| Branch | True | False | Total |
| if( row_triggers_exist ){ |
0 |
44 |
44 |
|
| 486 |
|
| /* Close all tables opened */ |
| 487 |
0 |
▶ | if( !isView ){ |
Branch: 1 branch
| Branch | True | False | Total |
| if( !isView ){ |
0 |
0 |
0 |
|
| 488 |
0 |
| sqliteVdbeAddOp(v, OP_Close, base, 0); |
| 489 |
0 |
▶ | for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){ |
Branch: 1 branch
| Branch | True | False | Total |
| for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){ |
0 |
0 |
0 |
|
| 490 |
0 |
| sqliteVdbeAddOp(v, OP_Close, idx+base, 0); |
| 491 |
|
| } |
| 492 |
|
| } |
| 493 |
|
| |
| 494 |
|
| /* Code AFTER triggers */ |
| 495 |
0 |
▶ | if( sqliteCodeRowTrigger(pParse, TK_INSERT, 0, TK_AFTER, pTab, newIdx, -1, |
Branch: 1 branch
| Branch | True | False | Total |
| if( sqliteCodeRowTrigger(pParse, TK_INSERT, 0, TK_AFTER, pTab, newIdx, -1, |
0 |
0 |
0 |
|
| 496 |
|
| onError, endOfLoop) ){ |
| 497 |
0 |
| goto insert_cleanup; |
| 498 |
|
| } |
| 499 |
|
| } |
| 500 |
|
| |
| 501 |
|
| /* The bottom of the loop, if the data source is a SELECT statement |
| 502 |
|
| */ |
| 503 |
44 |
| sqliteVdbeResolveLabel(v, endOfLoop); |
| 504 |
44 |
▶ | if( useTempTable ){ |
Branch: 1 branch
| Branch | True | False | Total |
| if( useTempTable ){ |
0 |
44 |
44 |
|
| 505 |
0 |
| sqliteVdbeAddOp(v, OP_Next, srcTab, iCont); |
| 506 |
0 |
| sqliteVdbeResolveLabel(v, iBreak); |
| 507 |
0 |
| sqliteVdbeAddOp(v, OP_Close, srcTab, 0); |
| 508 |
44 |
▶ | }else if( pSelect ){ |
Branch: 1 branch
| Branch | True | False | Total |
| }else if( pSelect ){ |
0 |
44 |
44 |
|
| 509 |
0 |
| sqliteVdbeAddOp(v, OP_Pop, nColumn, 0); |
| 510 |
0 |
| sqliteVdbeAddOp(v, OP_Return, 0, 0); |
| 511 |
0 |
| sqliteVdbeResolveLabel(v, iCleanup); |
| 512 |
|
| } |
| 513 |
|
| |
| 514 |
44 |
▶ | if( !row_triggers_exist ){ |
Branch: 1 branch
| Branch | True | False | Total |
| if( !row_triggers_exist ){ |
44 |
0 |
44 |
|
| 515 |
|
| /* Close all tables opened */ |
| 516 |
44 |
| sqliteVdbeAddOp(v, OP_Close, base, 0); |
| 517 |
46 |
▶ | for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){ |
Branch: 1 branch
| Branch | True | False | Total |
| for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){ |
2 |
44 |
46 |
|
| 518 |
2 |
| sqliteVdbeAddOp(v, OP_Close, idx+base, 0); |
| 519 |
|
| } |
| 520 |
|
| } |
| 521 |
|
| |
| 522 |
44 |
| sqliteVdbeAddOp(v, OP_SetCounts, 0, 0); |
| 523 |
44 |
| sqliteEndWriteOperation(pParse); |
| 524 |
|
| |
| 525 |
|
| /* |
| 526 |
|
| ** Return the number of rows inserted. |
| 527 |
|
| */ |
| 528 |
44 |
▶ | if( db->flags & SQLITE_CountRows ){ |
Branch: 1 branch
| Branch | True | False | Total |
| if( db->flags & SQLITE_CountRows ){ |
44 |
0 |
44 |
|
| 529 |
0 |
| sqliteVdbeOp3(v, OP_ColumnName, 0, 1, "rows inserted", P3_STATIC); |
| 530 |
0 |
| sqliteVdbeAddOp(v, OP_MemLoad, iCntMem, 0); |
| 531 |
0 |
| sqliteVdbeAddOp(v, OP_Callback, 1, 0); |
| 532 |
|
| } |
| 533 |
|
| |
| 534 |
44 |
| insert_cleanup: |
| 535 |
44 |
| sqliteSrcListDelete(pTabList); |
| 536 |
44 |
▶ | if( pList ) sqliteExprListDelete(pList); |
Branch: 1 branch
| Branch | True | False | Total |
| if( pList ) sqliteExprListDelete(pList); |
44 |
0 |
44 |
|
| 537 |
44 |
▶ | if( pSelect ) sqliteSelectDelete(pSelect); |
Branch: 1 branch
| Branch | True | False | Total |
| if( pSelect ) sqliteSelectDelete(pSelect); |
0 |
44 |
44 |
|
| 538 |
44 |
| sqliteIdListDelete(pColumn); |
| 539 |
44 |
| } |
| 540 |
|
| |
| 541 |
|
| /* |
| 542 |
|
| ** Generate code to do a constraint check prior to an INSERT or an UPDATE. |
| 543 |
|
| ** |
| 544 |
|
| ** When this routine is called, the stack contains (from bottom to top) |
| 545 |
|
| ** the following values: |
| 546 |
|
| ** |
| 547 |
|
| ** 1. The recno of the row to be updated before the update. This |
| 548 |
|
| ** value is omitted unless we are doing an UPDATE that involves a |
| 549 |
|
| ** change to the record number. |
| 550 |
|
| ** |
| 551 |
|
| ** 2. The recno of the row after the update. |
| 552 |
|
| ** |
| 553 |
|
| ** 3. The data in the first column of the entry after the update. |
| 554 |
|
| ** |
| 555 |
|
| ** i. Data from middle columns... |
| 556 |
|
| ** |
| 557 |
|
| ** N. The data in the last column of the entry after the update. |
| 558 |
|
| ** |
| 559 |
|
| ** The old recno shown as entry (1) above is omitted unless both isUpdate |
| 560 |
|
| ** and recnoChng are 1. isUpdate is true for UPDATEs and false for |
| 561 |
|
| ** INSERTs and recnoChng is true if the record number is being changed. |
| 562 |
|
| ** |
| 563 |
|
| ** The code generated by this routine pushes additional entries onto |
| 564 |
|
| ** the stack which are the keys for new index entries for the new record. |
| 565 |
|
| ** The order of index keys is the same as the order of the indices on |
| 566 |
|
| ** the pTable->pIndex list. A key is only created for index i if |
| 567 |
|
| ** aIdxUsed!=0 and aIdxUsed[i]!=0. |
| 568 |
|
| ** |
| 569 |
|
| ** This routine also generates code to check constraints. NOT NULL, |
| 570 |
|
| ** CHECK, and UNIQUE constraints are all checked. If a constraint fails, |
| 571 |
|
| ** then the appropriate action is performed. There are five possible |
| 572 |
|
| ** actions: ROLLBACK, ABORT, FAIL, REPLACE, and IGNORE. |
| 573 |
|
| ** |
| 574 |
|
| ** Constraint type Action What Happens |
| 575 |
|
| ** --------------- ---------- ---------------------------------------- |
| 576 |
|
| ** any ROLLBACK The current transaction is rolled back and |
| 577 |
|
| ** sqlite_exec() returns immediately with a |
| 578 |
|
| ** return code of SQLITE_CONSTRAINT. |
| 579 |
|
| ** |
| 580 |
|
| ** any ABORT Back out changes from the current command |
| 581 |
|
| ** only (do not do a complete rollback) then |
| 582 |
|
| ** cause sqlite_exec() to return immediately |
| 583 |
|
| ** with SQLITE_CONSTRAINT. |
| 584 |
|
| ** |
| 585 |
|
| ** any FAIL Sqlite_exec() returns immediately with a |
| 586 |
|
| ** return code of SQLITE_CONSTRAINT. The |
| 587 |
|
| ** transaction is not rolled back and any |
| 588 |
|
| ** prior changes are retained. |
| 589 |
|
| ** |
| 590 |
|
| ** any IGNORE The record number and data is popped from |
| 591 |
|
| ** the stack and there is an immediate jump |
| 592 |
|
| ** to label ignoreDest. |
| 593 |
|
| ** |
| 594 |
|
| ** NOT NULL REPLACE The NULL value is replace by the default |
| 595 |
|
| ** value for that column. If the default value |
| 596 |
|
| ** is NULL, the action is the same as ABORT. |
| 597 |
|
| ** |
| 598 |
|
| ** UNIQUE REPLACE The other row that conflicts with the row |
| 599 |
|
| ** being inserted is removed. |
| 600 |
|
| ** |
| 601 |
|
| ** CHECK REPLACE Illegal. The results in an exception. |
| 602 |
|
| ** |
| 603 |
|
| ** Which action to take is determined by the overrideError parameter. |
| 604 |
|
| ** Or if overrideError==OE_Default, then the pParse->onError parameter |
| 605 |
|
| ** is used. Or if pParse->onError==OE_Default then the onError value |
| 606 |
|
| ** for the constraint is used. |
| 607 |
|
| ** |
| 608 |
|
| ** The calling routine must open a read/write cursor for pTab with |
| 609 |
|
| ** cursor number "base". All indices of pTab must also have open |
| 610 |
|
| ** read/write cursors with cursor number base+i for the i-th cursor. |
| 611 |
|
| ** Except, if there is no possibility of a REPLACE action then |
| 612 |
|
| ** cursors do not need to be open for indices where aIdxUsed[i]==0. |
| 613 |
|
| ** |
| 614 |
|
| ** If the isUpdate flag is true, it means that the "base" cursor is |
| 615 |
|
| ** initially pointing to an entry that is being updated. The isUpdate |
| 616 |
|
| ** flag causes extra code to be generated so that the "base" cursor |
| 617 |
|
| ** is still pointing at the same entry after the routine returns. |
| 618 |
|
| ** Without the isUpdate flag, the "base" cursor might be moved. |
| 619 |
|
| */ |
| 620 |
44 |
| void sqliteGenerateConstraintChecks( |
| 621 |
|
| Parse *pParse, /* The parser context */ |
| 622 |
|
| Table *pTab, /* the table into which we are inserting */ |
| 623 |
|
| int base, /* Index of a read/write cursor pointing at pTab */ |
| 624 |
|
| char *aIdxUsed, /* Which indices are used. NULL means all are used */ |
| 625 |
|
| int recnoChng, /* True if the record number will change */ |
| 626 |
|
| int isUpdate, /* True for UPDATE, False for INSERT */ |
| 627 |
|
| int overrideError, /* Override onError to this if not OE_Default */ |
| 628 |
|
| int ignoreDest /* Jump to this label on an OE_Ignore resolution */ |
| 629 |
|
| ){ |
| 630 |
|
| int i; |
| 631 |
|
| Vdbe *v; |
| 632 |
|
| int nCol; |
| 633 |
|
| int onError; |
| 634 |
|
| int addr; |
| 635 |
|
| int extra; |
| 636 |
|
| int iCur; |
| 637 |
|
| Index *pIdx; |
| 638 |
44 |
| int seenReplace = 0; |
| 639 |
|
| int jumpInst1, jumpInst2; |
| 640 |
|
| int contAddr; |
| 641 |
44 |
▶ | int hasTwoRecnos = (isUpdate && recnoChng); |
Branch: 2 branches
| Branch | True | False | Total |
| int hasTwoRecnos = (isUpdate && recnoChng); |
0 |
44 |
44 |
| int hasTwoRecnos = (isUpdate && recnoChng); |
0 |
0 |
0 |
|
| 642 |
|
| |
| 643 |
44 |
| v = sqliteGetVdbe(pParse); |
| 644 |
|
| assert( v!=0 ); |
| 645 |
|
| assert( pTab->pSelect==0 ); /* This table is not a VIEW */ |
| 646 |
44 |
| nCol = pTab->nCol; |
| 647 |
|
| |
| 648 |
|
| /* Test all NOT NULL constraints. |
| 649 |
|
| */ |
| 650 |
139 |
▶ | for(i=0; i<nCol; i++){ |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0; i<nCol; i++){ |
95 |
44 |
139 |
|
| 651 |
95 |
▶ | if( i==pTab->iPKey ){ |
Branch: 1 branch
| Branch | True | False | Total |
| if( i==pTab->iPKey ){ |
0 |
95 |
95 |
|
| 652 |
0 |
| continue; |
| 653 |
|
| } |
| 654 |
95 |
| onError = pTab->aCol[i].notNull; |
| 655 |
95 |
▶ | if( onError==OE_None ) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if( onError==OE_None ) continue; |
67 |
28 |
95 |
|
| 656 |
28 |
▶ | if( overrideError!=OE_Default ){ |
Branch: 1 branch
| Branch | True | False | Total |
| if( overrideError!=OE_Default ){ |
0 |
28 |
28 |
|
| 657 |
0 |
| onError = overrideError; |
| 658 |
28 |
▶ | }else if( pParse->db->onError!=OE_Default ){ |
Branch: 1 branch
| Branch | True | False | Total |
| }else if( pParse->db->onError!=OE_Default ){ |
0 |
28 |
28 |
|
| 659 |
0 |
| onError = pParse->db->onError; |
| 660 |
28 |
▶ | }else if( onError==OE_Default ){ |
Branch: 1 branch
| Branch | True | False | Total |
| }else if( onError==OE_Default ){ |
28 |
0 |
28 |
|
| 661 |
28 |
| onError = OE_Abort; |
| 662 |
|
| } |
| 663 |
28 |
▶ | if( onError==OE_Replace && pTab->aCol[i].zDflt==0 ){ |
Branch: 2 branches
| Branch | True | False | Total |
| if( onError==OE_Replace && pTab->aCol[i].zDflt==0 ){ |
0 |
28 |
28 |
| if( onError==OE_Replace && pTab->aCol[i].zDflt==0 ){ |
0 |
0 |
0 |
|
| 664 |
0 |
| onError = OE_Abort; |
| 665 |
|
| } |
| 666 |
28 |
| sqliteVdbeAddOp(v, OP_Dup, nCol-1-i, 1); |
| 667 |
28 |
| addr = sqliteVdbeAddOp(v, OP_NotNull, 1, 0); |
| 668 |
28 |
| switch( onError ){ |
| 669 |
28 |
| case OE_Rollback: |
| 670 |
|
| case OE_Abort: |
| 671 |
|
| case OE_Fail: { |
| 672 |
28 |
| char *zMsg = 0; |
| 673 |
28 |
| sqliteVdbeAddOp(v, OP_Halt, SQLITE_CONSTRAINT, onError); |
| 674 |
28 |
| sqliteSetString(&zMsg, pTab->zName, ".", pTab->aCol[i].zName, |
| 675 |
|
| " may not be NULL", (char*)0); |
| 676 |
28 |
| sqliteVdbeChangeP3(v, -1, zMsg, P3_DYNAMIC); |
| 677 |
28 |
| break; |
| 678 |
|
| } |
| 679 |
0 |
| case OE_Ignore: { |
| 680 |
0 |
| sqliteVdbeAddOp(v, OP_Pop, nCol+1+hasTwoRecnos, 0); |
| 681 |
0 |
| sqliteVdbeAddOp(v, OP_Goto, 0, ignoreDest); |
| 682 |
0 |
| break; |
| 683 |
|
| } |
| 684 |
0 |
| case OE_Replace: { |
| 685 |
0 |
| sqliteVdbeOp3(v, OP_String, 0, 0, pTab->aCol[i].zDflt, P3_STATIC); |
| 686 |
0 |
| sqliteVdbeAddOp(v, OP_Push, nCol-i, 0); |
| 687 |
0 |
| break; |
| 688 |
|
| } |
| 689 |
28 |
| default: assert(0); |
| 690 |
|
| } |
| 691 |
28 |
| sqliteVdbeChangeP2(v, addr, sqliteVdbeCurrentAddr(v)); |
| 692 |
|
| } |
| 693 |
|
| |
| 694 |
|
| /* Test all CHECK constraints |
| 695 |
|
| */ |
| 696 |
|
| /**** TBD ****/ |
| 697 |
|
| |
| 698 |
|
| /* If we have an INTEGER PRIMARY KEY, make sure the primary key |
| 699 |
|
| ** of the new record does not previously exist. Except, if this |
| 700 |
|
| ** is an UPDATE and the primary key is not changing, that is OK. |
| 701 |
|
| */ |
| 702 |
44 |
▶ | if( recnoChng ){ |
Branch: 1 branch
| Branch | True | False | Total |
| if( recnoChng ){ |
0 |
44 |
44 |
|
| 703 |
0 |
| onError = pTab->keyConf; |
| 704 |
0 |
▶ | if( overrideError!=OE_Default ){ |
Branch: 1 branch
| Branch | True | False | Total |
| if( overrideError!=OE_Default ){ |
0 |
0 |
0 |
|
| 705 |
0 |
| onError = overrideError; |
| 706 |
0 |
▶ | }else if( pParse->db->onError!=OE_Default ){ |
Branch: 1 branch
| Branch | True | False | Total |
| }else if( pParse->db->onError!=OE_Default ){ |
0 |
0 |
0 |
|
| 707 |
0 |
| onError = pParse->db->onError; |
| 708 |
0 |
▶ | }else if( onError==OE_Default ){ |
Branch: 1 branch
| Branch | True | False | Total |
| }else if( onError==OE_Default ){ |
0 |
0 |
0 |
|
| 709 |
0 |
| onError = OE_Abort; |
| 710 |
|
| } |
| 711 |
|
| |
| 712 |
0 |
▶ | if( isUpdate ){ |
Branch: 1 branch
| Branch | True | False | Total |
| if( isUpdate ){ |
0 |
0 |
0 |
|
| 713 |
0 |
| sqliteVdbeAddOp(v, OP_Dup, nCol+1, 1); |
| 714 |
0 |
| sqliteVdbeAddOp(v, OP_Dup, nCol+1, 1); |
| 715 |
0 |
| jumpInst1 = sqliteVdbeAddOp(v, OP_Eq, 0, 0); |
| 716 |
|
| } |
| 717 |
0 |
| sqliteVdbeAddOp(v, OP_Dup, nCol, 1); |
| 718 |
0 |
| jumpInst2 = sqliteVdbeAddOp(v, OP_NotExists, base, 0); |
| 719 |
0 |
| switch( onError ){ |
| 720 |
0 |
| default: { |
| 721 |
0 |
| onError = OE_Abort; |
| 722 |
|
| /* Fall thru into the next case */ |
| 723 |
|
| } |
| 724 |
0 |
| case OE_Rollback: |
| 725 |
|
| case OE_Abort: |
| 726 |
|
| case OE_Fail: { |
| 727 |
0 |
| sqliteVdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, onError, |
| 728 |
|
| "PRIMARY KEY must be unique", P3_STATIC); |
| 729 |
0 |
| break; |
| 730 |
|
| } |
| 731 |
0 |
| case OE_Replace: { |
| 732 |
0 |
| sqliteGenerateRowIndexDelete(pParse->db, v, pTab, base, 0); |
| 733 |
0 |
▶ | if( isUpdate ){ |
Branch: 1 branch
| Branch | True | False | Total |
| if( isUpdate ){ |
0 |
0 |
0 |
|
| 734 |
0 |
| sqliteVdbeAddOp(v, OP_Dup, nCol+hasTwoRecnos, 1); |
| 735 |
0 |
| sqliteVdbeAddOp(v, OP_MoveTo, base, 0); |
| 736 |
|
| } |
| 737 |
0 |
| seenReplace = 1; |
| 738 |
0 |
| break; |
| 739 |
|
| } |
| 740 |
0 |
| case OE_Ignore: { |
| 741 |
|
| assert( seenReplace==0 ); |
| 742 |
0 |
| sqliteVdbeAddOp(v, OP_Pop, nCol+1+hasTwoRecnos, 0); |
| 743 |
0 |
| sqliteVdbeAddOp(v, OP_Goto, 0, ignoreDest); |
| 744 |
0 |
| break; |
| 745 |
|
| } |
| 746 |
|
| } |
| 747 |
0 |
| contAddr = sqliteVdbeCurrentAddr(v); |
| 748 |
0 |
| sqliteVdbeChangeP2(v, jumpInst2, contAddr); |
| 749 |
0 |
▶ | if( isUpdate ){ |
Branch: 1 branch
| Branch | True | False | Total |
| if( isUpdate ){ |
0 |
0 |
0 |
|
| 750 |
0 |
| sqliteVdbeChangeP2(v, jumpInst1, contAddr); |
| 751 |
0 |
| sqliteVdbeAddOp(v, OP_Dup, nCol+1, 1); |
| 752 |
0 |
| sqliteVdbeAddOp(v, OP_MoveTo, base, 0); |
| 753 |
|
| } |
| 754 |
|
| } |
| 755 |
|
| |
| 756 |
|
| /* Test all UNIQUE constraints by creating entries for each UNIQUE |
| 757 |
|
| ** index and making sure that duplicate entries do not already exist. |
| 758 |
|
| ** Add the new records to the indices as we go. |
| 759 |
|
| */ |
| 760 |
44 |
| extra = -1; |
| 761 |
46 |
▶ | for(iCur=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, iCur++){ |
Branch: 1 branch
| Branch | True | False | Total |
| for(iCur=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, iCur++){ |
2 |
44 |
46 |
|
| 762 |
2 |
▶ | if( aIdxUsed && aIdxUsed[iCur]==0 ) continue; /* Skip unused indices */ |
Branch: 2 branches
| Branch | True | False | Total |
| if( aIdxUsed && aIdxUsed[iCur]==0 ) continue; /* Skip unused indices */ |
0 |
2 |
2 |
| if( aIdxUsed && aIdxUsed[iCur]==0 ) continue; /* Skip unused indices */ |
0 |
0 |
0 |
|
| 763 |
2 |
| extra++; |
| 764 |
|
| |
| 765 |
|
| /* Create a key for accessing the index entry */ |
| 766 |
2 |
| sqliteVdbeAddOp(v, OP_Dup, nCol+extra, 1); |
| 767 |
5 |
▶ | for(i=0; i<pIdx->nColumn; i++){ |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0; i<pIdx->nColumn; i++){ |
3 |
2 |
5 |
|
| 768 |
3 |
| int idx = pIdx->aiColumn[i]; |
| 769 |
3 |
▶ | if( idx==pTab->iPKey ){ |
Branch: 1 branch
| Branch | True | False | Total |
| if( idx==pTab->iPKey ){ |
0 |
3 |
3 |
|
| 770 |
0 |
| sqliteVdbeAddOp(v, OP_Dup, i+extra+nCol+1, 1); |
| 771 |
|
| }else{ |
| 772 |
3 |
| sqliteVdbeAddOp(v, OP_Dup, i+extra+nCol-idx, 1); |
| 773 |
|
| } |
| 774 |
|
| } |
| 775 |
2 |
| jumpInst1 = sqliteVdbeAddOp(v, OP_MakeIdxKey, pIdx->nColumn, 0); |
| 776 |
2 |
▶ | if( pParse->db->file_format>=4 ) sqliteAddIdxKeyType(v, pIdx); |
Branch: 1 branch
| Branch | True | False | Total |
| if( pParse->db->file_format>=4 ) sqliteAddIdxKeyType(v, pIdx); |
2 |
0 |
2 |
|
| 777 |
|
| |
| 778 |
|
| /* Find out what action to take in case there is an indexing conflict */ |
| 779 |
2 |
| onError = pIdx->onError; |
| 780 |
2 |
▶ | if( onError==OE_None ) continue; /* pIdx is not a UNIQUE index */ |
Branch: 1 branch
| Branch | True | False | Total |
| if( onError==OE_None ) continue; /* pIdx is not a UNIQUE index */ |
0 |
2 |
2 |
|
| 781 |
2 |
▶ | if( overrideError!=OE_Default ){ |
Branch: 1 branch
| Branch | True | False | Total |
| if( overrideError!=OE_Default ){ |
0 |
2 |
2 |
|
| 782 |
0 |
| onError = overrideError; |
| 783 |
2 |
▶ | }else if( pParse->db->onError!=OE_Default ){ |
Branch: 1 branch
| Branch | True | False | Total |
| }else if( pParse->db->onError!=OE_Default ){ |
0 |
2 |
2 |
|
| 784 |
0 |
| onError = pParse->db->onError; |
| 785 |
2 |
▶ | }else if( onError==OE_Default ){ |
Branch: 1 branch
| Branch | True | False | Total |
| }else if( onError==OE_Default ){ |
1 |
1 |
2 |
|
| 786 |
1 |
| onError = OE_Abort; |
| 787 |
|
| } |
| 788 |
2 |
▶ | if( seenReplace ){ |
Branch: 1 branch
| Branch | True | False | Total |
| if( seenReplace ){ |
0 |
2 |
2 |
|
| 789 |
0 |
▶ | if( onError==OE_Ignore ) onError = OE_Replace; |
Branch: 1 branch
| Branch | True | False | Total |
| if( onError==OE_Ignore ) onError = OE_Replace; |
0 |
0 |
0 |
|
| 790 |
0 |
▶ | else if( onError==OE_Fail ) onError = OE_Abort; |
Branch: 1 branch
| Branch | True | False | Total |
| else if( onError==OE_Fail ) onError = OE_Abort; |
0 |
0 |
0 |
|
| 791 |
|
| } |
| 792 |
|
| |
| 793 |
|
| |
| 794 |
|
| /* Check to see if the new index entry will be unique */ |
| 795 |
2 |
| sqliteVdbeAddOp(v, OP_Dup, extra+nCol+1+hasTwoRecnos, 1); |
| 796 |
2 |
| jumpInst2 = sqliteVdbeAddOp(v, OP_IsUnique, base+iCur+1, 0); |
| 797 |
|
| |
| 798 |
|
| /* Generate code that executes if the new index entry is not unique */ |
| 799 |
2 |
| switch( onError ){ |
| 800 |
2 |
| case OE_Rollback: |
| 801 |
|
| case OE_Abort: |
| 802 |
|
| case OE_Fail: { |
| 803 |
|
| int j, n1, n2; |
| 804 |
|
| char zErrMsg[200]; |
| 805 |
2 |
▶ | strcpy(zErrMsg, pIdx->nColumn>1 ? "columns " : "column "); |
Branch: 1 branch
| Branch | True | False | Total |
| strcpy(zErrMsg, pIdx->nColumn>1 ? "columns " : "column "); |
1 |
1 |
2 |
|
| 806 |
2 |
| n1 = strlen(zErrMsg); |
| 807 |
5 |
▶ | for(j=0; j<pIdx->nColumn && n1<sizeof(zErrMsg)-30; j++){ |
Branch: 2 branches
| Branch | True | False | Total |
| for(j=0; j<pIdx->nColumn && n1<sizeof(zErrMsg)-30; j++){ |
3 |
2 |
5 |
| for(j=0; j<pIdx->nColumn && n1<sizeof(zErrMsg)-30; j++){ |
3 |
0 |
3 |
|
| 808 |
3 |
| char *zCol = pTab->aCol[pIdx->aiColumn[j]].zName; |
| 809 |
3 |
| n2 = strlen(zCol); |
| 810 |
3 |
▶ | if( j>0 ){ |
Branch: 1 branch
| Branch | True | False | Total |
| if( j>0 ){ |
1 |
2 |
3 |
|
| 811 |
1 |
| strcpy(&zErrMsg[n1], ", "); |
| 812 |
1 |
| n1 += 2; |
| 813 |
|
| } |
| 814 |
3 |
▶ | if( n1+n2>sizeof(zErrMsg)-30 ){ |
Branch: 1 branch
| Branch | True | False | Total |
| if( n1+n2>sizeof(zErrMsg)-30 ){ |
0 |
3 |
3 |
|
| 815 |
0 |
| strcpy(&zErrMsg[n1], "..."); |
| 816 |
0 |
| n1 += 3; |
| 817 |
0 |
| break; |
| 818 |
|
| }else{ |
| 819 |
3 |
| strcpy(&zErrMsg[n1], zCol); |
| 820 |
3 |
| n1 += n2; |
| 821 |
|
| } |
| 822 |
|
| } |
| 823 |
2 |
| strcpy(&zErrMsg[n1], |
| 824 |
2 |
▶ | pIdx->nColumn>1 ? " are not unique" : " is not unique"); |
Branch: 1 branch
| Branch | True | False | Total |
| pIdx->nColumn>1 ? " are not unique" : " is not unique"); |
1 |
1 |
2 |
|
| 825 |
2 |
| sqliteVdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, onError, zErrMsg, 0); |
| 826 |
2 |
| break; |
| 827 |
|
| } |
| 828 |
0 |
| case OE_Ignore: { |
| 829 |
|
| assert( seenReplace==0 ); |
| 830 |
0 |
| sqliteVdbeAddOp(v, OP_Pop, nCol+extra+3+hasTwoRecnos, 0); |
| 831 |
0 |
| sqliteVdbeAddOp(v, OP_Goto, 0, ignoreDest); |
| 832 |
0 |
| break; |
| 833 |
|
| } |
| 834 |
0 |
| case OE_Replace: { |
| 835 |
0 |
| sqliteGenerateRowDelete(pParse->db, v, pTab, base, 0); |
| 836 |
0 |
▶ | if( isUpdate ){ |
Branch: 1 branch
| Branch | True | False | Total |
| if( isUpdate ){ |
0 |
0 |
0 |
|
| 837 |
0 |
| sqliteVdbeAddOp(v, OP_Dup, nCol+extra+1+hasTwoRecnos, 1); |
| 838 |
0 |
| sqliteVdbeAddOp(v, OP_MoveTo, base, 0); |
| 839 |
|
| } |
| 840 |
0 |
| seenReplace = 1; |
| 841 |
0 |
| break; |
| 842 |
|
| } |
| 843 |
2 |
| default: assert(0); |
| 844 |
|
| } |
| 845 |
2 |
| contAddr = sqliteVdbeCurrentAddr(v); |
| 846 |
|
| #if NULL_DISTINCT_FOR_UNIQUE |
| 847 |
2 |
| sqliteVdbeChangeP2(v, jumpInst1, contAddr); |
| 848 |
|
| #endif |
| 849 |
2 |
| sqliteVdbeChangeP2(v, jumpInst2, contAddr); |
| 850 |
|
| } |
| 851 |
44 |
| } |
| 852 |
|
| |
| 853 |
|
| /* |
| 854 |
|
| ** This routine generates code to finish the INSERT or UPDATE operation |
| 855 |
|
| ** that was started by a prior call to sqliteGenerateConstraintChecks. |
| 856 |
|
| ** The stack must contain keys for all active indices followed by data |
| 857 |
|
| ** and the recno for the new entry. This routine creates the new |
| 858 |
|
| ** entries in all indices and in the main table. |
| 859 |
|
| ** |
| 860 |
|
| ** The arguments to this routine should be the same as the first six |
| 861 |
|
| ** arguments to sqliteGenerateConstraintChecks. |
| 862 |
|
| */ |
| 863 |
44 |
| void sqliteCompleteInsertion( |
| 864 |
|
| Parse *pParse, /* The parser context */ |
| 865 |
|
| Table *pTab, /* the table into which we are inserting */ |
| 866 |
|
| int base, /* Index of a read/write cursor pointing at pTab */ |
| 867 |
|
| char *aIdxUsed, /* Which indices are used. NULL means all are used */ |
| 868 |
|
| int recnoChng, /* True if the record number will change */ |
| 869 |
|
| int isUpdate, /* True for UPDATE, False for INSERT */ |
| 870 |
|
| int newIdx /* Index of NEW table for triggers. -1 if none */ |
| 871 |
|
| ){ |
| 872 |
|
| int i; |
| 873 |
|
| Vdbe *v; |
| 874 |
|
| int nIdx; |
| 875 |
|
| Index *pIdx; |
| 876 |
|
| |
| 877 |
44 |
| v = sqliteGetVdbe(pParse); |
| 878 |
|
| assert( v!=0 ); |
| 879 |
|
| assert( pTab->pSelect==0 ); /* This table is not a VIEW */ |
| 880 |
46 |
▶ | for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){} |
Branch: 1 branch
| Branch | True | False | Total |
| for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){} |
2 |
44 |
46 |
|
| 881 |
46 |
▶ | for(i=nIdx-1; i>=0; i--){ |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=nIdx-1; i>=0; i--){ |
2 |
44 |
46 |
|
| 882 |
2 |
▶ | if( aIdxUsed && aIdxUsed[i]==0 ) continue; |
Branch: 2 branches
| Branch | True | False | Total |
| if( aIdxUsed && aIdxUsed[i]==0 ) continue; |
0 |
2 |
2 |
| if( aIdxUsed && aIdxUsed[i]==0 ) continue; |
0 |
0 |
0 |
|
| 883 |
2 |
| sqliteVdbeAddOp(v, OP_IdxPut, base+i+1, 0); |
| 884 |
|
| } |
| 885 |
44 |
| sqliteVdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0); |
| 886 |
44 |
▶ | if( newIdx>=0 ){ |
Branch: 1 branch
| Branch | True | False | Total |
| if( newIdx>=0 ){ |
0 |
44 |
44 |
|
| 887 |
0 |
| sqliteVdbeAddOp(v, OP_Dup, 1, 0); |
| 888 |
0 |
| sqliteVdbeAddOp(v, OP_Dup, 1, 0); |
| 889 |
0 |
| sqliteVdbeAddOp(v, OP_PutIntKey, newIdx, 0); |
| 890 |
|
| } |
| 891 |
44 |
| sqliteVdbeAddOp(v, OP_PutIntKey, base, |
| 892 |
44 |
| (pParse->trigStack?0:OPFLAG_NCHANGE) | |
| 893 |
44 |
▶ | (isUpdate?0:OPFLAG_LASTROWID) | OPFLAG_CSCHANGE); |
Branch: 1 branch
| Branch | True | False | Total |
| (isUpdate?0:OPFLAG_LASTROWID) | OPFLAG_CSCHANGE); |
0 |
44 |
44 |
|
| 894 |
44 |
▶ | if( isUpdate && recnoChng ){ |
Branch: 2 branches
| Branch | True | False | Total |
| if( isUpdate && recnoChng ){ |
0 |
44 |
44 |
| if( isUpdate && recnoChng ){ |
0 |
0 |
0 |
|
| 895 |
0 |
| sqliteVdbeAddOp(v, OP_Pop, 1, 0); |
| 896 |
|
| } |
| 897 |
44 |
| } |
| 898 |
|
| |
| 899 |
|
| /* |
| 900 |
|
| ** Generate code that will open write cursors for a table and for all |
| 901 |
|
| ** indices of that table. The "base" parameter is the cursor number used |
| 902 |
|
| ** for the table. Indices are opened on subsequent cursors. |
| 903 |
|
| ** |
| 904 |
|
| ** Return the total number of cursors opened. This is always at least |
| 905 |
|
| ** 1 (for the main table) plus more for each cursor. |
| 906 |
|
| */ |
| 907 |
50 |
| int sqliteOpenTableAndIndices(Parse *pParse, Table *pTab, int base){ |
| 908 |
|
| int i; |
| 909 |
|
| Index *pIdx; |
| 910 |
50 |
| Vdbe *v = sqliteGetVdbe(pParse); |
| 911 |
|
| assert( v!=0 ); |
| 912 |
50 |
| sqliteVdbeAddOp(v, OP_Integer, pTab->iDb, 0); |
| 913 |
50 |
| sqliteVdbeOp3(v, OP_OpenWrite, base, pTab->tnum, pTab->zName, P3_STATIC); |
| 914 |
52 |
▶ | for(i=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){ |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){ |
2 |
50 |
52 |
|
| 915 |
2 |
| sqliteVdbeAddOp(v, OP_Integer, pIdx->iDb, 0); |
| 916 |
2 |
| sqliteVdbeOp3(v, OP_OpenWrite, i+base, pIdx->tnum, pIdx->zName, P3_STATIC); |
| 917 |
|
| } |
| 918 |
50 |
| return i; |
| 919 |
|
| } |