blob: f9b8b1791c4e1182f2353a56ddd521ab9a8e55f4 [file] [log] [blame]
drhcce7d172000-05-31 15:34:51 +00001/*
drhb19a2bc2001-09-16 00:13:26 +00002** 2001 September 15
drhcce7d172000-05-31 15:34:51 +00003**
drhb19a2bc2001-09-16 00:13:26 +00004** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
drhcce7d172000-05-31 15:34:51 +00006**
drhb19a2bc2001-09-16 00:13:26 +00007** 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.
drhcce7d172000-05-31 15:34:51 +000010**
11*************************************************************************
12** This file contains C code routines that are called by the parser
drhb19a2bc2001-09-16 00:13:26 +000013** to handle INSERT statements in SQLite.
drhcce7d172000-05-31 15:34:51 +000014**
drh9cfcf5d2002-01-29 18:41:24 +000015** $Id: insert.c,v 1.34 2002/01/29 18:41:25 drh Exp $
drhcce7d172000-05-31 15:34:51 +000016*/
17#include "sqliteInt.h"
18
19/*
drh1ccde152000-06-17 13:12:39 +000020** This routine is call to handle SQL of the following forms:
drhcce7d172000-05-31 15:34:51 +000021**
22** insert into TABLE (IDLIST) values(EXPRLIST)
drh1ccde152000-06-17 13:12:39 +000023** insert into TABLE (IDLIST) select
drhcce7d172000-05-31 15:34:51 +000024**
drh1ccde152000-06-17 13:12:39 +000025** 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
drh967e8b72000-06-21 13:59:10 +000027** appears in the pColumn parameter. pColumn is NULL if IDLIST is omitted.
drh1ccde152000-06-17 13:12:39 +000028**
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.
drhcce7d172000-05-31 15:34:51 +000033*/
34void sqliteInsert(
35 Parse *pParse, /* Parser context */
36 Token *pTableName, /* Name of table into which we are inserting */
37 ExprList *pList, /* List of values to be inserted */
drh5974a302000-06-07 14:42:26 +000038 Select *pSelect, /* A SELECT statement to use as the data source */
drh9cfcf5d2002-01-29 18:41:24 +000039 IdList *pColumn, /* Column names corresponding to IDLIST. */
40 int onError /* How to handle constraint errors */
drhcce7d172000-05-31 15:34:51 +000041){
drh5974a302000-06-07 14:42:26 +000042 Table *pTab; /* The table to insert into */
43 char *zTab; /* Name of the table into which we are inserting */
44 int i, j, idx; /* Loop counters */
45 Vdbe *v; /* Generate code into this virtual machine */
46 Index *pIdx; /* For looping over indices of the table */
47 int srcTab; /* Date comes from this temporary cursor if >=0 */
drh967e8b72000-06-21 13:59:10 +000048 int nColumn; /* Number of columns in the data */
drh5974a302000-06-07 14:42:26 +000049 int base; /* First available cursor */
50 int iCont, iBreak; /* Beginning and end of the loop over srcTab */
drhecdc7532001-09-23 02:35:53 +000051 sqlite *db; /* The main database structure */
drhf57b3392001-10-08 13:22:32 +000052 int openOp; /* Opcode used to open cursors */
drh4a324312001-12-21 14:30:42 +000053 int keyColumn = -1; /* Column that is the INTEGER PRIMARY KEY */
drhcce7d172000-05-31 15:34:51 +000054
drhdaffd0e2001-04-11 14:28:42 +000055 if( pParse->nErr || sqlite_malloc_failed ) goto insert_cleanup;
drhecdc7532001-09-23 02:35:53 +000056 db = pParse->db;
drhdaffd0e2001-04-11 14:28:42 +000057
drh1ccde152000-06-17 13:12:39 +000058 /* Locate the table into which we will be inserting new information.
59 */
drhcce7d172000-05-31 15:34:51 +000060 zTab = sqliteTableNameFromToken(pTableName);
drhdaffd0e2001-04-11 14:28:42 +000061 if( zTab==0 ) goto insert_cleanup;
drhecdc7532001-09-23 02:35:53 +000062 pTab = sqliteFindTable(db, zTab);
drhcce7d172000-05-31 15:34:51 +000063 sqliteFree(zTab);
64 if( pTab==0 ){
65 sqliteSetNString(&pParse->zErrMsg, "no such table: ", 0,
66 pTableName->z, pTableName->n, 0);
67 pParse->nErr++;
68 goto insert_cleanup;
69 }
70 if( pTab->readOnly ){
71 sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName,
72 " may not be modified", 0);
73 pParse->nErr++;
74 goto insert_cleanup;
75 }
drh1ccde152000-06-17 13:12:39 +000076
77 /* Allocate a VDBE
78 */
drhd8bc7082000-06-07 23:51:50 +000079 v = sqliteGetVdbe(pParse);
drh5974a302000-06-07 14:42:26 +000080 if( v==0 ) goto insert_cleanup;
drhecdc7532001-09-23 02:35:53 +000081 if( (db->flags & SQLITE_InTrans)==0 ){
drh99fcd712001-10-13 01:06:47 +000082 sqliteVdbeAddOp(v, OP_Transaction, 0, 0);
83 sqliteVdbeAddOp(v, OP_VerifyCookie, db->schema_cookie, 0);
drhecdc7532001-09-23 02:35:53 +000084 pParse->schemaVerified = 1;
drh5e00f6c2001-09-13 13:46:56 +000085 }
drh1ccde152000-06-17 13:12:39 +000086
87 /* Figure out how many columns of data are supplied. If the data
drhc6b52df2002-01-04 03:09:29 +000088 ** is coming from a SELECT statement, then this step has to generate
drh1ccde152000-06-17 13:12:39 +000089 ** all the code to implement the SELECT statement and leave the data
90 ** in a temporary table. If data is coming from an expression list,
91 ** then we just have to count the number of expressions.
92 */
drh5974a302000-06-07 14:42:26 +000093 if( pSelect ){
94 int rc;
95 srcTab = pParse->nTab++;
drh99fcd712001-10-13 01:06:47 +000096 sqliteVdbeAddOp(v, OP_OpenTemp, srcTab, 0);
drh5974a302000-06-07 14:42:26 +000097 rc = sqliteSelect(pParse, pSelect, SRT_Table, srcTab);
drhdaffd0e2001-04-11 14:28:42 +000098 if( rc || pParse->nErr || sqlite_malloc_failed ) goto insert_cleanup;
drh5974a302000-06-07 14:42:26 +000099 assert( pSelect->pEList );
drh967e8b72000-06-21 13:59:10 +0000100 nColumn = pSelect->pEList->nExpr;
drh5974a302000-06-07 14:42:26 +0000101 }else{
drhdaffd0e2001-04-11 14:28:42 +0000102 assert( pList!=0 );
drh5974a302000-06-07 14:42:26 +0000103 srcTab = -1;
104 assert( pList );
drh967e8b72000-06-21 13:59:10 +0000105 nColumn = pList->nExpr;
drh5974a302000-06-07 14:42:26 +0000106 }
drh1ccde152000-06-17 13:12:39 +0000107
108 /* Make sure the number of columns in the source data matches the number
109 ** of columns to be inserted into the table.
110 */
drh967e8b72000-06-21 13:59:10 +0000111 if( pColumn==0 && nColumn!=pTab->nCol ){
drhcce7d172000-05-31 15:34:51 +0000112 char zNum1[30];
113 char zNum2[30];
drh967e8b72000-06-21 13:59:10 +0000114 sprintf(zNum1,"%d", nColumn);
drhcce7d172000-05-31 15:34:51 +0000115 sprintf(zNum2,"%d", pTab->nCol);
116 sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName,
117 " has ", zNum2, " columns but ",
118 zNum1, " values were supplied", 0);
119 pParse->nErr++;
120 goto insert_cleanup;
121 }
drh967e8b72000-06-21 13:59:10 +0000122 if( pColumn!=0 && nColumn!=pColumn->nId ){
drhcce7d172000-05-31 15:34:51 +0000123 char zNum1[30];
124 char zNum2[30];
drh967e8b72000-06-21 13:59:10 +0000125 sprintf(zNum1,"%d", nColumn);
126 sprintf(zNum2,"%d", pColumn->nId);
drhcce7d172000-05-31 15:34:51 +0000127 sqliteSetString(&pParse->zErrMsg, zNum1, " values for ",
128 zNum2, " columns", 0);
129 pParse->nErr++;
130 goto insert_cleanup;
131 }
drh1ccde152000-06-17 13:12:39 +0000132
133 /* If the INSERT statement included an IDLIST term, then make sure
134 ** all elements of the IDLIST really are columns of the table and
135 ** remember the column indices.
drhc8392582001-12-31 02:48:51 +0000136 **
137 ** If the table has an INTEGER PRIMARY KEY column and that column
138 ** is named in the IDLIST, then record in the keyColumn variable
139 ** the index into IDLIST of the primary key column. keyColumn is
140 ** the index of the primary key as it appears in IDLIST, not as
141 ** is appears in the original table. (The index of the primary
142 ** key in the original table is pTab->iPKey.)
drh1ccde152000-06-17 13:12:39 +0000143 */
drh967e8b72000-06-21 13:59:10 +0000144 if( pColumn ){
145 for(i=0; i<pColumn->nId; i++){
146 pColumn->a[i].idx = -1;
drhcce7d172000-05-31 15:34:51 +0000147 }
drh967e8b72000-06-21 13:59:10 +0000148 for(i=0; i<pColumn->nId; i++){
drhcce7d172000-05-31 15:34:51 +0000149 for(j=0; j<pTab->nCol; j++){
drh967e8b72000-06-21 13:59:10 +0000150 if( sqliteStrICmp(pColumn->a[i].zName, pTab->aCol[j].zName)==0 ){
151 pColumn->a[i].idx = j;
drh4a324312001-12-21 14:30:42 +0000152 if( j==pTab->iPKey ){
drh9aa028d2001-12-22 21:48:29 +0000153 keyColumn = i;
drh4a324312001-12-21 14:30:42 +0000154 }
drhcce7d172000-05-31 15:34:51 +0000155 break;
156 }
157 }
158 if( j>=pTab->nCol ){
159 sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName,
drh967e8b72000-06-21 13:59:10 +0000160 " has no column named ", pColumn->a[i].zName, 0);
drhcce7d172000-05-31 15:34:51 +0000161 pParse->nErr++;
162 goto insert_cleanup;
163 }
164 }
165 }
drh1ccde152000-06-17 13:12:39 +0000166
drhaacc5432002-01-06 17:07:40 +0000167 /* If there is no IDLIST term but the table has an integer primary
drhc8392582001-12-31 02:48:51 +0000168 ** key, the set the keyColumn variable to the primary key column index
169 ** in the original table definition.
drh4a324312001-12-21 14:30:42 +0000170 */
171 if( pColumn==0 ){
172 keyColumn = pTab->iPKey;
173 }
174
drh1ccde152000-06-17 13:12:39 +0000175 /* Open cursors into the table that is received the new data and
176 ** all indices of that table.
177 */
drh5974a302000-06-07 14:42:26 +0000178 base = pParse->nTab;
drhf57b3392001-10-08 13:22:32 +0000179 openOp = pTab->isTemp ? OP_OpenWrAux : OP_OpenWrite;
drh99fcd712001-10-13 01:06:47 +0000180 sqliteVdbeAddOp(v, openOp, base, pTab->tnum);
181 sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC);
drh5974a302000-06-07 14:42:26 +0000182 for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
drh99fcd712001-10-13 01:06:47 +0000183 sqliteVdbeAddOp(v, openOp, idx+base, pIdx->tnum);
184 sqliteVdbeChangeP3(v, -1, pIdx->zName, P3_STATIC);
drh19a775c2000-06-05 18:54:46 +0000185 }
drh1ccde152000-06-17 13:12:39 +0000186
187 /* If the data source is a SELECT statement, then we have to create
188 ** a loop because there might be multiple rows of data. If the data
189 ** source is an expression list, then exactly one row will be inserted
190 ** and the loop is not used.
191 */
drh5974a302000-06-07 14:42:26 +0000192 if( srcTab>=0 ){
drh1bee3d72001-10-15 00:44:35 +0000193 if( db->flags & SQLITE_CountRows ){
194 sqliteVdbeAddOp(v, OP_Integer, 0, 0); /* Initialize the row count */
195 }
drh5974a302000-06-07 14:42:26 +0000196 iBreak = sqliteVdbeMakeLabel(v);
drh6b563442001-11-07 16:48:26 +0000197 sqliteVdbeAddOp(v, OP_Rewind, srcTab, iBreak);
198 iCont = sqliteVdbeCurrentAddr(v);
drh5974a302000-06-07 14:42:26 +0000199 }
drh1ccde152000-06-17 13:12:39 +0000200
drh4a324312001-12-21 14:30:42 +0000201 /* Push the record number for the new entry onto the stack. The
202 ** record number is a randomly generate integer created by NewRecno
203 ** except when the table has an INTEGER PRIMARY KEY column, in which
204 ** case the record number is the same as that column.
drh1ccde152000-06-17 13:12:39 +0000205 */
drh4a324312001-12-21 14:30:42 +0000206 if( keyColumn>=0 ){
207 if( srcTab>=0 ){
208 sqliteVdbeAddOp(v, OP_Column, srcTab, keyColumn);
209 }else{
210 sqliteExprCode(pParse, pList->a[keyColumn].pExpr);
211 }
drh8aff1012001-12-22 14:49:24 +0000212 sqliteVdbeAddOp(v, OP_MustBeInt, 0, 0);
drh4a324312001-12-21 14:30:42 +0000213 }else{
214 sqliteVdbeAddOp(v, OP_NewRecno, base, 0);
215 }
216
drhc8392582001-12-31 02:48:51 +0000217 /* If there are indices, we'll need the new record number again, so make
drh4a324312001-12-21 14:30:42 +0000218 ** a copy.
219 */
drh5974a302000-06-07 14:42:26 +0000220 if( pTab->pIndex ){
drh99fcd712001-10-13 01:06:47 +0000221 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
drh5974a302000-06-07 14:42:26 +0000222 }
drh4a324312001-12-21 14:30:42 +0000223
drhaacc5432002-01-06 17:07:40 +0000224 /* Push onto the stack, data for all columns of the new entry, beginning
drh4a324312001-12-21 14:30:42 +0000225 ** with the first column.
226 */
drh5974a302000-06-07 14:42:26 +0000227 for(i=0; i<pTab->nCol; i++){
drh4a324312001-12-21 14:30:42 +0000228 if( i==pTab->iPKey ){
229 /* The value of the INTEGER PRIMARY KEY column is always a NULL.
drhaacc5432002-01-06 17:07:40 +0000230 ** Whenever this column is read, the record number will be substituted
231 ** in its place. So will fill this column with a NULL to avoid
232 ** taking up data space with information that will never be used. */
drh4a324312001-12-21 14:30:42 +0000233 sqliteVdbeAddOp(v, OP_String, 0, 0);
234 continue;
235 }
drh967e8b72000-06-21 13:59:10 +0000236 if( pColumn==0 ){
drh5974a302000-06-07 14:42:26 +0000237 j = i;
238 }else{
drh967e8b72000-06-21 13:59:10 +0000239 for(j=0; j<pColumn->nId; j++){
240 if( pColumn->a[j].idx==i ) break;
drh5974a302000-06-07 14:42:26 +0000241 }
drhbed86902000-06-02 13:27:59 +0000242 }
drh967e8b72000-06-21 13:59:10 +0000243 if( pColumn && j>=pColumn->nId ){
drh99fcd712001-10-13 01:06:47 +0000244 sqliteVdbeAddOp(v, OP_String, 0, 0);
245 sqliteVdbeChangeP3(v, -1, pTab->aCol[i].zDflt, P3_STATIC);
drh5974a302000-06-07 14:42:26 +0000246 }else if( srcTab>=0 ){
drh99fcd712001-10-13 01:06:47 +0000247 sqliteVdbeAddOp(v, OP_Column, srcTab, i);
drh5974a302000-06-07 14:42:26 +0000248 }else{
249 sqliteExprCode(pParse, pList->a[j].pExpr);
250 }
251 }
drh1ccde152000-06-17 13:12:39 +0000252
drh4a324312001-12-21 14:30:42 +0000253 /* Create the new record and put it into the database.
254 */
255 sqliteVdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0);
drh6b125452002-01-28 15:53:03 +0000256 sqliteVdbeAddOp(v, OP_PutIntKey, base, keyColumn>=0);
drh4a324312001-12-21 14:30:42 +0000257
drh1ccde152000-06-17 13:12:39 +0000258 /* Create appropriate entries for the new data row in all indices
259 ** of the table.
260 */
drh5974a302000-06-07 14:42:26 +0000261 for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
262 if( pIdx->pNext ){
drh99fcd712001-10-13 01:06:47 +0000263 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
drhcce7d172000-05-31 15:34:51 +0000264 }
drh967e8b72000-06-21 13:59:10 +0000265 for(i=0; i<pIdx->nColumn; i++){
266 int idx = pIdx->aiColumn[i];
drh4a324312001-12-21 14:30:42 +0000267 if( idx==pTab->iPKey ){
268 /* Copy the record number in place of the INTEGER PRIMARY KEY column */
269 sqliteVdbeAddOp(v, OP_Dup, i, 0);
270 continue;
271 }
drh967e8b72000-06-21 13:59:10 +0000272 if( pColumn==0 ){
drh5974a302000-06-07 14:42:26 +0000273 j = idx;
drhcce7d172000-05-31 15:34:51 +0000274 }else{
drh967e8b72000-06-21 13:59:10 +0000275 for(j=0; j<pColumn->nId; j++){
276 if( pColumn->a[j].idx==idx ) break;
drhcce7d172000-05-31 15:34:51 +0000277 }
278 }
drh967e8b72000-06-21 13:59:10 +0000279 if( pColumn && j>=pColumn->nId ){
drh99fcd712001-10-13 01:06:47 +0000280 sqliteVdbeAddOp(v, OP_String, 0, 0);
281 sqliteVdbeChangeP3(v, -1, pTab->aCol[idx].zDflt, P3_STATIC);
drh5974a302000-06-07 14:42:26 +0000282 }else if( srcTab>=0 ){
drh99fcd712001-10-13 01:06:47 +0000283 sqliteVdbeAddOp(v, OP_Column, srcTab, idx);
drhcce7d172000-05-31 15:34:51 +0000284 }else{
285 sqliteExprCode(pParse, pList->a[j].pExpr);
286 }
287 }
drh99fcd712001-10-13 01:06:47 +0000288 sqliteVdbeAddOp(v, OP_MakeIdxKey, pIdx->nColumn, 0);
drh8721ce42001-11-07 14:22:00 +0000289 sqliteVdbeAddOp(v, OP_IdxPut, idx+base, pIdx->isUnique);
drh5974a302000-06-07 14:42:26 +0000290 }
drh1ccde152000-06-17 13:12:39 +0000291
drh1bee3d72001-10-15 00:44:35 +0000292
293 /* If inserting from a SELECT, keep a count of the number of
294 ** rows inserted.
295 */
296 if( srcTab>=0 && (db->flags & SQLITE_CountRows)!=0 ){
297 sqliteVdbeAddOp(v, OP_AddImm, 1, 0);
298 }
299
drh1ccde152000-06-17 13:12:39 +0000300 /* The bottom of the loop, if the data source is a SELECT statement
301 */
drh5974a302000-06-07 14:42:26 +0000302 if( srcTab>=0 ){
drh6b563442001-11-07 16:48:26 +0000303 sqliteVdbeAddOp(v, OP_Next, srcTab, iCont);
drh99fcd712001-10-13 01:06:47 +0000304 sqliteVdbeResolveLabel(v, iBreak);
drh6b563442001-11-07 16:48:26 +0000305 sqliteVdbeAddOp(v, OP_Close, srcTab, 0);
306 }
307 sqliteVdbeAddOp(v, OP_Close, base, 0);
308 for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
309 sqliteVdbeAddOp(v, OP_Close, idx+base, 0);
drhcce7d172000-05-31 15:34:51 +0000310 }
drhecdc7532001-09-23 02:35:53 +0000311 if( (db->flags & SQLITE_InTrans)==0 ){
drh99fcd712001-10-13 01:06:47 +0000312 sqliteVdbeAddOp(v, OP_Commit, 0, 0);
drh5e00f6c2001-09-13 13:46:56 +0000313 }
314
drh1bee3d72001-10-15 00:44:35 +0000315 /*
316 ** Return the number of rows inserted.
317 */
318 if( db->flags & SQLITE_CountRows ){
319 sqliteVdbeAddOp(v, OP_ColumnCount, 1, 0);
320 sqliteVdbeAddOp(v, OP_ColumnName, 0, 0);
321 sqliteVdbeChangeP3(v, -1, "rows inserted", P3_STATIC);
322 if( srcTab<0 ){
323 sqliteVdbeAddOp(v, OP_Integer, 1, 0);
324 }
325 sqliteVdbeAddOp(v, OP_Callback, 1, 0);
326 }
drhcce7d172000-05-31 15:34:51 +0000327
328insert_cleanup:
drh5974a302000-06-07 14:42:26 +0000329 if( pList ) sqliteExprListDelete(pList);
330 if( pSelect ) sqliteSelectDelete(pSelect);
drh967e8b72000-06-21 13:59:10 +0000331 sqliteIdListDelete(pColumn);
drhcce7d172000-05-31 15:34:51 +0000332}
drh9cfcf5d2002-01-29 18:41:24 +0000333
334#if 0
335/*
336** Generate code to do a constraint check prior to an INSERT or an UPDATE.
337**
338** When this routine is called, the stack contains (from bottom to top)
339** the recno of the row to be updated and each column of new data beginning
340** with the first column. The code generated by this routine pushes addition
341** entries onto the stack which are the keys for new index entries for
342** the new record. The order of index keys is the same as the order of
343** the indices on the pTable->pIndex list. A key is only created for
344** index i if aIdxUsed!=0 and aIdxUsed[i]!=0.
345**
346** This routine also generates code to check constraints. NOT NULL,
347** CHECK, and UNIQUE constraints are all checked. If a constraint fails,
348** then the appropriate action is performed. The default action is to
349** execute OP_Halt to abort the transaction and cause sqlite_exec() to
350** return SQLITE_CONSTRAINT. This is the so-called "ABORT" action.
351** Other actions are REPLACE and IGNORE. The following table summarizes
352** what happens.
353**
354** Constraint type Action What Happens
355** --------------- ---------- ----------------------------------------
356** any ABORT The current transaction is rolled back and
357** sqlite_exec() returns immediately with a
358** return code of SQLITE_CONSTRAINT.
359**
360** any IGNORE The record number and data is popped from
361** the stack and there is an immediate jump
362** to label ignoreDest.
363**
364** NOT NULL REPLACE The NULL value is replace by the default
365** value for that column. If the default value
366** is NULL, the action is the same as ABORT.
367**
368** UNIQUE REPLACE The other row that conflicts with the row
369** being inserted is removed.
370**
371** CHECK REPLACE Illegal. The results in an exception.
372**
373** The action to take is determined by the constraint itself if
374** overrideError is OE_Default. Otherwise, overrideError determines
375** which action to use.
376**
377** The calling routine must an open read/write cursor for pTab with
378** cursor number "base". All indices of pTab must also have open
379** read/write cursors with cursor number base+i for the i-th cursor.
380** Except, if there is no possibility of a REPLACE action then
381** cursors do not need to be open for indices where aIdxUsed[i]==0.
382**
383** If the isUpdate flag is true, it means that the "base" cursor is
384** initially pointing to an entry that is being updated. The isUpdate
385** flag causes extra code to be generated so that the "base" cursor
386** is still pointing at the same entry after the routine returns.
387** Without the isUpdate flag, the "base" cursor might be moved.
388*/
389void sqliteGenerateConstraintChecks(
390 Parse *pParse, /* The parser context */
391 Table *pTab, /* the table into which we are inserting */
392 int base, /* Index of a read/write cursor pointing at pTab */
393 char *aIdxUsed, /* Which indices are used. NULL means all are used */
394 int overrideError, /* Override onError to this if not OE_Default */
395 int ignoreDest, /* Jump to this label on an OE_Ignore resolution */
396 int isUpdate /* True for UPDATE, False for INSERT */
397){
398 int i;
399 Vdbe *v;
400 int nCol;
401 int onError;
402 int addr;
403 int extra;
404 char *pToFree = 0;
405 int seenIgnore = 0;
406
407 v = sqliteGetVdbe(pParse);
408 assert( v!=0 );
409 nCol = pTab->nCol;
410
411 /* Test all NOT NULL constraints.
412 */
413 for(i=0; i<nCol; i++){
414 onError = pTab->aCol[i].notNull;
415 if( i==iPKey || onError==OE_None ) continue;
416 if( overrideError!=OE_Default ){
417 onError = overrideError;
418 }
419 if( onError==OE_Replace && pTab->aCol[i].zDflt==0 ){
420 onError = OE_Abort;
421 }
422 addr = sqliteVdbeAddOp(v, OP_Dup, nCol-i, 1);
423 sqliteVdbeAddOp(v, OP_NotNull, 0, addr+1+(onError!=OE_Abort));
424 switch( onError ){
425 case OE_Abort: {
426 sqliteVdbeAddOp(v, OP_Halt, SQLITE_CONSTRAINT, 0);
427 break;
428 }
429 case OE_Ignore: {
430 sqliteVdbeAddOp(v, OP_Pop, nCol+1, 0);
431 sqliteVdbeAddOp(v, OP_GoTo, 0, ignoreDest);
432 break;
433 }
434 case OE_Replace: {
435 sqliteVdbeAddOp(v, OP_String, 0, 0);
436 sqliteVdbeChangeP3(v, -1, pTab->aCol[i].zDflt, P3_STATIC);
437 sqliteVdbeAddOp(v, OP_Push, nCol-i, 0);
438 break;
439 }
440 default: {
441 CANT_HAPPEN;
442 }
443 }
444 }
445
446 /* Test all CHECK constraints
447 */
448
449 /* Test all UNIQUE constraints. Add index records as we go.
450 */
451 extra = 0;
452 for(extra=(-1), iCur=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, iCur++){
453 int jumpInst;
454 int contAddr;
455
456 if( aIdxUsed && aIdxUsed[iCur]==0 ) continue;
457 extra++;
458 sqliteVdbeAddOp(v, OP_Dup, nCol+extra, 1);
459 for(i=0; i<pIdx->nColumn; i++){
460 int idx = pIdx->aiColumn[i];
461 if( idx==pTab->iPKey ){
462 sqliteVdbeAddOp(v, OP_Dup, i+extra+nCol+1, 0);
463 }else{
464 sqliteVdbeAddOp(v, OP_Dup, i+extra+nCol-idx, 0);
465 }
466 }
467 sqliteVdbeAddOp(v, OP_MakeIdxKey, pIdx->nColumn, 0);
468 onError = pIdx->onError;
469 if( onError==OE_None ) continue;
470 if( overrideError!=OE_Default ){
471 onError = overrideError;
472 }
473 jumpInst = sqliteVdbeAddOp(v, OP_IsUnique, iCur, 0);
474 switch( onError ){
475 case OE_Abort: {
476 sqliteVdbeAddOp(v, OP_Halt, SQLITE_CONSTRAINT, 0);
477 break;
478 }
479 case OE_Ignore: {
480 sqliteVdbeAddOp(v, OP_Pop, nCol+extra+2, 0);
481 sqliteVdbeAddOp(v, OP_Goto, 0, ignoreDest);
482 seenIgnore = 1;
483 break;
484 }
485 case OE_Replace: {
486 assert( seenIgnore==0 );
487 sqliteGenerateRowDelete(v, pTab, base);
488 if( isUpdate ){
489 sqliteVdbeAddOp(v, OP_Dup, nCol+extra+2, 1);
490 sqliteVdbeAddOp(v, OP_Moveto, base, 0);
491 }
492 break;
493 }
494 default: CANT_HAPPEN;
495 }
496 contAddr = sqliteVdbeCurrentAddr(v);
497 sqliteVdbeChangeP2(v, jumpInst, contAddr);
498 }
499}
500#endif