blob: 86c2fcc2ed9d7712b08f63d3b4270d7f079f9de1 [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**
danielk19773d1bfea2004-05-14 11:00:53 +000015** $Id: insert.c,v 1.98 2004/05/14 11:00:53 danielk1977 Exp $
drhcce7d172000-05-31 15:34:51 +000016*/
17#include "sqliteInt.h"
18
19/*
danielk19773d1bfea2004-05-14 11:00:53 +000020** Set P3 of the most recently inserted opcode to a column affinity
21** string for table pTab. A column affinity string has one character
22** for each column in the table, according to the affinity of the column:
23**
24** Character Column affinity
25** ------------------------------
26** 'n' NUMERIC
27** 'i' INTEGER
28** 't' TEXT
29** 'o' NONE
30*/
31int sqlite3AddRecordType(Vdbe *v, Table *pTab){
32 assert( pTab );
33
34 /* The first time a column affinity string for a particular table
35 ** is required, it is allocated and populated here. It is then
36 ** stored as a member of the Table structure for subsequent use.
37 **
38 ** The column affinity string will eventually be deleted by
39 ** sqlite3DeleteTable() when the Table structure itself is cleaned up.
40 */
41 if( !pTab->zColAff ){
42 char *zColAff;
43 int i;
44
45 zColAff = sqliteMalloc(pTab->nCol+1);
46 if( !zColAff ){
47 return SQLITE_NOMEM;
48 }
49
50 for(i=0; i<pTab->nCol; i++){
51 if( pTab->aCol[i].sortOrder&SQLITE_SO_TEXT ){
52 zColAff[i] = 't';
53 }else{
54 zColAff[i] = 'n';
55 }
56 }
57 zColAff[pTab->nCol] = '\0';
58
59 pTab->zColAff = zColAff;
60 }
61
62 /* Set the memory management at the vdbe to P3_STATIC, as the column
63 ** affinity string is managed as part of the Table structure.
64 */
65 sqlite3VdbeChangeP3(v, -1, pTab->zColAff, P3_STATIC);
66 return SQLITE_OK;
67}
68
69
70/*
drh1ccde152000-06-17 13:12:39 +000071** This routine is call to handle SQL of the following forms:
drhcce7d172000-05-31 15:34:51 +000072**
73** insert into TABLE (IDLIST) values(EXPRLIST)
drh1ccde152000-06-17 13:12:39 +000074** insert into TABLE (IDLIST) select
drhcce7d172000-05-31 15:34:51 +000075**
drh1ccde152000-06-17 13:12:39 +000076** The IDLIST following the table name is always optional. If omitted,
77** then a list of all columns for the table is substituted. The IDLIST
drh967e8b72000-06-21 13:59:10 +000078** appears in the pColumn parameter. pColumn is NULL if IDLIST is omitted.
drh1ccde152000-06-17 13:12:39 +000079**
80** The pList parameter holds EXPRLIST in the first form of the INSERT
81** statement above, and pSelect is NULL. For the second form, pList is
82** NULL and pSelect is a pointer to the select statement used to generate
83** data for the insert.
drh142e30d2002-08-28 03:00:58 +000084**
85** The code generated follows one of three templates. For a simple
86** select with data coming from a VALUES clause, the code executes
87** once straight down through. The template looks like this:
88**
89** open write cursor to <table> and its indices
90** puts VALUES clause expressions onto the stack
91** write the resulting record into <table>
92** cleanup
93**
94** If the statement is of the form
95**
96** INSERT INTO <table> SELECT ...
97**
98** And the SELECT clause does not read from <table> at any time, then
99** the generated code follows this template:
100**
101** goto B
102** A: setup for the SELECT
103** loop over the tables in the SELECT
104** gosub C
105** end loop
106** cleanup after the SELECT
107** goto D
108** B: open write cursor to <table> and its indices
109** goto A
110** C: insert the select result into <table>
111** return
112** D: cleanup
113**
114** The third template is used if the insert statement takes its
115** values from a SELECT but the data is being inserted into a table
116** that is also read as part of the SELECT. In the third form,
117** we have to use a intermediate table to store the results of
118** the select. The template is like this:
119**
120** goto B
121** A: setup for the SELECT
122** loop over the tables in the SELECT
123** gosub C
124** end loop
125** cleanup after the SELECT
126** goto D
127** C: insert the select result into the intermediate table
128** return
129** B: open a cursor to an intermediate table
130** goto A
131** D: open write cursor to <table> and its indices
132** loop over the intermediate table
133** transfer values form intermediate table into <table>
134** end the loop
135** cleanup
drhcce7d172000-05-31 15:34:51 +0000136*/
danielk19774adee202004-05-08 08:23:19 +0000137void sqlite3Insert(
drhcce7d172000-05-31 15:34:51 +0000138 Parse *pParse, /* Parser context */
drh113088e2003-03-20 01:16:58 +0000139 SrcList *pTabList, /* Name of table into which we are inserting */
drhcce7d172000-05-31 15:34:51 +0000140 ExprList *pList, /* List of values to be inserted */
drh5974a302000-06-07 14:42:26 +0000141 Select *pSelect, /* A SELECT statement to use as the data source */
drh9cfcf5d2002-01-29 18:41:24 +0000142 IdList *pColumn, /* Column names corresponding to IDLIST. */
143 int onError /* How to handle constraint errors */
drhcce7d172000-05-31 15:34:51 +0000144){
drh5974a302000-06-07 14:42:26 +0000145 Table *pTab; /* The table to insert into */
drh113088e2003-03-20 01:16:58 +0000146 char *zTab; /* Name of the table into which we are inserting */
drhe22a3342003-04-22 20:30:37 +0000147 const char *zDb; /* Name of the database holding this table */
drh5974a302000-06-07 14:42:26 +0000148 int i, j, idx; /* Loop counters */
149 Vdbe *v; /* Generate code into this virtual machine */
150 Index *pIdx; /* For looping over indices of the table */
drh967e8b72000-06-21 13:59:10 +0000151 int nColumn; /* Number of columns in the data */
drh6a3ea0e2003-05-02 14:32:12 +0000152 int base; /* VDBE Cursor number for pTab */
drh5974a302000-06-07 14:42:26 +0000153 int iCont, iBreak; /* Beginning and end of the loop over srcTab */
drhecdc7532001-09-23 02:35:53 +0000154 sqlite *db; /* The main database structure */
drh4a324312001-12-21 14:30:42 +0000155 int keyColumn = -1; /* Column that is the INTEGER PRIMARY KEY */
drh0ca3e242002-01-29 23:07:02 +0000156 int endOfLoop; /* Label for the end of the insertion loop */
drh142e30d2002-08-28 03:00:58 +0000157 int useTempTable; /* Store SELECT results in intermediate table */
158 int srcTab; /* Data comes from this temporary cursor if >=0 */
159 int iSelectLoop; /* Address of code that implements the SELECT */
160 int iCleanup; /* Address of the cleanup code */
161 int iInsertBlock; /* Address of the subroutine used to insert data */
162 int iCntMem; /* Memory cell used for the row counter */
drh5cf590c2003-04-24 01:45:04 +0000163 int isView; /* True if attempting to insert into a view */
drhcce7d172000-05-31 15:34:51 +0000164
danielk1977c3f9bad2002-05-15 08:30:12 +0000165 int row_triggers_exist = 0; /* True if there are FOR EACH ROW triggers */
drh70ce3f02003-04-15 19:22:22 +0000166 int before_triggers; /* True if there are BEFORE triggers */
167 int after_triggers; /* True if there are AFTER triggers */
168 int newIdx = -1; /* Cursor for the NEW table */
danielk1977c3f9bad2002-05-15 08:30:12 +0000169
danielk197724b03fd2004-05-10 10:34:34 +0000170 if( pParse->nErr || sqlite3_malloc_failed ) goto insert_cleanup;
drhecdc7532001-09-23 02:35:53 +0000171 db = pParse->db;
drhdaffd0e2001-04-11 14:28:42 +0000172
drh1ccde152000-06-17 13:12:39 +0000173 /* Locate the table into which we will be inserting new information.
174 */
drh113088e2003-03-20 01:16:58 +0000175 assert( pTabList->nSrc==1 );
176 zTab = pTabList->a[0].zName;
drhdaffd0e2001-04-11 14:28:42 +0000177 if( zTab==0 ) goto insert_cleanup;
danielk19774adee202004-05-08 08:23:19 +0000178 pTab = sqlite3SrcListLookup(pParse, pTabList);
danielk1977c3f9bad2002-05-15 08:30:12 +0000179 if( pTab==0 ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000180 goto insert_cleanup;
181 }
drhe22a3342003-04-22 20:30:37 +0000182 assert( pTab->iDb<db->nDb );
183 zDb = db->aDb[pTab->iDb].zName;
danielk19774adee202004-05-08 08:23:19 +0000184 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, pTab->zName, 0, zDb) ){
drh1962bda2003-01-12 19:33:52 +0000185 goto insert_cleanup;
186 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000187
188 /* Ensure that:
189 * (a) the table is not read-only,
190 * (b) that if it is a view then ON INSERT triggers exist
191 */
danielk19774adee202004-05-08 08:23:19 +0000192 before_triggers = sqlite3TriggersExist(pParse, pTab->pTrigger, TK_INSERT,
drh70ce3f02003-04-15 19:22:22 +0000193 TK_BEFORE, TK_ROW, 0);
danielk19774adee202004-05-08 08:23:19 +0000194 after_triggers = sqlite3TriggersExist(pParse, pTab->pTrigger, TK_INSERT,
drh70ce3f02003-04-15 19:22:22 +0000195 TK_AFTER, TK_ROW, 0);
196 row_triggers_exist = before_triggers || after_triggers;
drh5cf590c2003-04-24 01:45:04 +0000197 isView = pTab->pSelect!=0;
danielk19774adee202004-05-08 08:23:19 +0000198 if( sqlite3IsReadOnly(pParse, pTab, before_triggers) ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000199 goto insert_cleanup;
200 }
drha76b5df2002-02-23 02:32:10 +0000201 if( pTab==0 ) goto insert_cleanup;
drh1ccde152000-06-17 13:12:39 +0000202
drhf573c992002-07-31 00:32:50 +0000203 /* If pTab is really a view, make sure it has been initialized.
204 */
danielk19774adee202004-05-08 08:23:19 +0000205 if( isView && sqlite3ViewGetColumnNames(pParse, pTab) ){
drh5cf590c2003-04-24 01:45:04 +0000206 goto insert_cleanup;
drhf573c992002-07-31 00:32:50 +0000207 }
208
drh1ccde152000-06-17 13:12:39 +0000209 /* Allocate a VDBE
210 */
danielk19774adee202004-05-08 08:23:19 +0000211 v = sqlite3GetVdbe(pParse);
drh5974a302000-06-07 14:42:26 +0000212 if( v==0 ) goto insert_cleanup;
danielk19774adee202004-05-08 08:23:19 +0000213 sqlite3BeginWriteOperation(pParse, pSelect || row_triggers_exist, pTab->iDb);
drh1ccde152000-06-17 13:12:39 +0000214
danielk1977c3f9bad2002-05-15 08:30:12 +0000215 /* if there are row triggers, allocate a temp table for new.* references. */
danielk1977f29ce552002-05-19 23:43:12 +0000216 if( row_triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000217 newIdx = pParse->nTab++;
danielk1977f29ce552002-05-19 23:43:12 +0000218 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000219
drh1ccde152000-06-17 13:12:39 +0000220 /* Figure out how many columns of data are supplied. If the data
drh142e30d2002-08-28 03:00:58 +0000221 ** is coming from a SELECT statement, then this step also generates
222 ** all the code to implement the SELECT statement and invoke a subroutine
223 ** to process each row of the result. (Template 2.) If the SELECT
224 ** statement uses the the table that is being inserted into, then the
225 ** subroutine is also coded here. That subroutine stores the SELECT
226 ** results in a temporary table. (Template 3.)
drh1ccde152000-06-17 13:12:39 +0000227 */
drh5974a302000-06-07 14:42:26 +0000228 if( pSelect ){
drh142e30d2002-08-28 03:00:58 +0000229 /* Data is coming from a SELECT. Generate code to implement that SELECT
230 */
231 int rc, iInitCode;
danielk19774adee202004-05-08 08:23:19 +0000232 iInitCode = sqlite3VdbeAddOp(v, OP_Goto, 0, 0);
233 iSelectLoop = sqlite3VdbeCurrentAddr(v);
234 iInsertBlock = sqlite3VdbeMakeLabel(v);
235 rc = sqlite3Select(pParse, pSelect, SRT_Subroutine, iInsertBlock, 0,0,0);
danielk197724b03fd2004-05-10 10:34:34 +0000236 if( rc || pParse->nErr || sqlite3_malloc_failed ) goto insert_cleanup;
danielk19774adee202004-05-08 08:23:19 +0000237 iCleanup = sqlite3VdbeMakeLabel(v);
238 sqlite3VdbeAddOp(v, OP_Goto, 0, iCleanup);
drh5974a302000-06-07 14:42:26 +0000239 assert( pSelect->pEList );
drh967e8b72000-06-21 13:59:10 +0000240 nColumn = pSelect->pEList->nExpr;
drh142e30d2002-08-28 03:00:58 +0000241
242 /* Set useTempTable to TRUE if the result of the SELECT statement
243 ** should be written into a temporary table. Set to FALSE if each
244 ** row of the SELECT can be written directly into the result table.
drh048c5302003-04-03 01:50:44 +0000245 **
246 ** A temp table must be used if the table being updated is also one
247 ** of the tables being read by the SELECT statement. Also use a
248 ** temp table in the case of row triggers.
drh142e30d2002-08-28 03:00:58 +0000249 */
drh048c5302003-04-03 01:50:44 +0000250 if( row_triggers_exist ){
251 useTempTable = 1;
252 }else{
danielk19774adee202004-05-08 08:23:19 +0000253 int addr = sqlite3VdbeFindOp(v, OP_OpenRead, pTab->tnum);
drh048c5302003-04-03 01:50:44 +0000254 useTempTable = 0;
255 if( addr>0 ){
danielk19774adee202004-05-08 08:23:19 +0000256 VdbeOp *pOp = sqlite3VdbeGetOp(v, addr-2);
drh048c5302003-04-03 01:50:44 +0000257 if( pOp->opcode==OP_Integer && pOp->p1==pTab->iDb ){
258 useTempTable = 1;
259 }
260 }
261 }
drh142e30d2002-08-28 03:00:58 +0000262
263 if( useTempTable ){
264 /* Generate the subroutine that SELECT calls to process each row of
265 ** the result. Store the result in a temporary table
266 */
267 srcTab = pParse->nTab++;
danielk19774adee202004-05-08 08:23:19 +0000268 sqlite3VdbeResolveLabel(v, iInsertBlock);
269 sqlite3VdbeAddOp(v, OP_MakeRecord, nColumn, 0);
danielk19773d1bfea2004-05-14 11:00:53 +0000270 sqlite3AddRecordType(v, pTab);
danielk19774adee202004-05-08 08:23:19 +0000271 sqlite3VdbeAddOp(v, OP_NewRecno, srcTab, 0);
272 sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
273 sqlite3VdbeAddOp(v, OP_PutIntKey, srcTab, 0);
274 sqlite3VdbeAddOp(v, OP_Return, 0, 0);
drh142e30d2002-08-28 03:00:58 +0000275
276 /* The following code runs first because the GOTO at the very top
277 ** of the program jumps to it. Create the temporary table, then jump
278 ** back up and execute the SELECT code above.
279 */
danielk19774adee202004-05-08 08:23:19 +0000280 sqlite3VdbeChangeP2(v, iInitCode, sqlite3VdbeCurrentAddr(v));
281 sqlite3VdbeAddOp(v, OP_OpenTemp, srcTab, 0);
282 sqlite3VdbeAddOp(v, OP_Goto, 0, iSelectLoop);
283 sqlite3VdbeResolveLabel(v, iCleanup);
drh142e30d2002-08-28 03:00:58 +0000284 }else{
danielk19774adee202004-05-08 08:23:19 +0000285 sqlite3VdbeChangeP2(v, iInitCode, sqlite3VdbeCurrentAddr(v));
drh142e30d2002-08-28 03:00:58 +0000286 }
drh5974a302000-06-07 14:42:26 +0000287 }else{
drh142e30d2002-08-28 03:00:58 +0000288 /* This is the case if the data for the INSERT is coming from a VALUES
289 ** clause
290 */
drhad3cab52002-05-24 02:04:32 +0000291 SrcList dummy;
drhdaffd0e2001-04-11 14:28:42 +0000292 assert( pList!=0 );
drh5974a302000-06-07 14:42:26 +0000293 srcTab = -1;
drh142e30d2002-08-28 03:00:58 +0000294 useTempTable = 0;
drh5974a302000-06-07 14:42:26 +0000295 assert( pList );
drh967e8b72000-06-21 13:59:10 +0000296 nColumn = pList->nExpr;
drhad3cab52002-05-24 02:04:32 +0000297 dummy.nSrc = 0;
drhe64e7b22002-02-18 13:56:36 +0000298 for(i=0; i<nColumn; i++){
danielk19774adee202004-05-08 08:23:19 +0000299 if( sqlite3ExprResolveIds(pParse, &dummy, 0, pList->a[i].pExpr) ){
drhe64e7b22002-02-18 13:56:36 +0000300 goto insert_cleanup;
301 }
danielk19774adee202004-05-08 08:23:19 +0000302 if( sqlite3ExprCheck(pParse, pList->a[i].pExpr, 0, 0) ){
drhb04a5d82002-04-12 03:55:15 +0000303 goto insert_cleanup;
304 }
drhe64e7b22002-02-18 13:56:36 +0000305 }
drh5974a302000-06-07 14:42:26 +0000306 }
drh1ccde152000-06-17 13:12:39 +0000307
308 /* Make sure the number of columns in the source data matches the number
309 ** of columns to be inserted into the table.
310 */
drh967e8b72000-06-21 13:59:10 +0000311 if( pColumn==0 && nColumn!=pTab->nCol ){
danielk19774adee202004-05-08 08:23:19 +0000312 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +0000313 "table %S has %d columns but %d values were supplied",
314 pTabList, 0, pTab->nCol, nColumn);
drhcce7d172000-05-31 15:34:51 +0000315 goto insert_cleanup;
316 }
drh967e8b72000-06-21 13:59:10 +0000317 if( pColumn!=0 && nColumn!=pColumn->nId ){
danielk19774adee202004-05-08 08:23:19 +0000318 sqlite3ErrorMsg(pParse, "%d values for %d columns", nColumn, pColumn->nId);
drhcce7d172000-05-31 15:34:51 +0000319 goto insert_cleanup;
320 }
drh1ccde152000-06-17 13:12:39 +0000321
322 /* If the INSERT statement included an IDLIST term, then make sure
323 ** all elements of the IDLIST really are columns of the table and
324 ** remember the column indices.
drhc8392582001-12-31 02:48:51 +0000325 **
326 ** If the table has an INTEGER PRIMARY KEY column and that column
327 ** is named in the IDLIST, then record in the keyColumn variable
328 ** the index into IDLIST of the primary key column. keyColumn is
329 ** the index of the primary key as it appears in IDLIST, not as
330 ** is appears in the original table. (The index of the primary
331 ** key in the original table is pTab->iPKey.)
drh1ccde152000-06-17 13:12:39 +0000332 */
drh967e8b72000-06-21 13:59:10 +0000333 if( pColumn ){
334 for(i=0; i<pColumn->nId; i++){
335 pColumn->a[i].idx = -1;
drhcce7d172000-05-31 15:34:51 +0000336 }
drh967e8b72000-06-21 13:59:10 +0000337 for(i=0; i<pColumn->nId; i++){
drhcce7d172000-05-31 15:34:51 +0000338 for(j=0; j<pTab->nCol; j++){
danielk19774adee202004-05-08 08:23:19 +0000339 if( sqlite3StrICmp(pColumn->a[i].zName, pTab->aCol[j].zName)==0 ){
drh967e8b72000-06-21 13:59:10 +0000340 pColumn->a[i].idx = j;
drh4a324312001-12-21 14:30:42 +0000341 if( j==pTab->iPKey ){
drh9aa028d2001-12-22 21:48:29 +0000342 keyColumn = i;
drh4a324312001-12-21 14:30:42 +0000343 }
drhcce7d172000-05-31 15:34:51 +0000344 break;
345 }
346 }
347 if( j>=pTab->nCol ){
danielk19774adee202004-05-08 08:23:19 +0000348 if( sqlite3IsRowid(pColumn->a[i].zName) ){
drha0217ba2003-06-01 01:10:33 +0000349 keyColumn = i;
350 }else{
danielk19774adee202004-05-08 08:23:19 +0000351 sqlite3ErrorMsg(pParse, "table %S has no column named %s",
drha0217ba2003-06-01 01:10:33 +0000352 pTabList, 0, pColumn->a[i].zName);
353 pParse->nErr++;
354 goto insert_cleanup;
355 }
drhcce7d172000-05-31 15:34:51 +0000356 }
357 }
358 }
drh1ccde152000-06-17 13:12:39 +0000359
drhaacc5432002-01-06 17:07:40 +0000360 /* If there is no IDLIST term but the table has an integer primary
drhc8392582001-12-31 02:48:51 +0000361 ** key, the set the keyColumn variable to the primary key column index
362 ** in the original table definition.
drh4a324312001-12-21 14:30:42 +0000363 */
364 if( pColumn==0 ){
365 keyColumn = pTab->iPKey;
366 }
367
drh142e30d2002-08-28 03:00:58 +0000368 /* Open the temp table for FOR EACH ROW triggers
369 */
danielk1977f29ce552002-05-19 23:43:12 +0000370 if( row_triggers_exist ){
danielk19774adee202004-05-08 08:23:19 +0000371 sqlite3VdbeAddOp(v, OP_OpenPseudo, newIdx, 0);
danielk1977f29ce552002-05-19 23:43:12 +0000372 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000373
drhfeeb1392002-04-09 03:28:01 +0000374 /* Initialize the count of rows to be inserted
375 */
drh142e30d2002-08-28 03:00:58 +0000376 if( db->flags & SQLITE_CountRows ){
377 iCntMem = pParse->nMem++;
danielk19774adee202004-05-08 08:23:19 +0000378 sqlite3VdbeAddOp(v, OP_Integer, 0, 0);
379 sqlite3VdbeAddOp(v, OP_MemStore, iCntMem, 1);
drhfeeb1392002-04-09 03:28:01 +0000380 }
381
danielk1977c3f9bad2002-05-15 08:30:12 +0000382 /* Open tables and indices if there are no row triggers */
danielk1977f29ce552002-05-19 23:43:12 +0000383 if( !row_triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000384 base = pParse->nTab;
danielk19774adee202004-05-08 08:23:19 +0000385 idx = sqlite3OpenTableAndIndices(pParse, pTab, base);
danielk1977c3f9bad2002-05-15 08:30:12 +0000386 pParse->nTab += idx;
387 }
388
drh142e30d2002-08-28 03:00:58 +0000389 /* If the data source is a temporary table, then we have to create
drh1ccde152000-06-17 13:12:39 +0000390 ** a loop because there might be multiple rows of data. If the data
drh142e30d2002-08-28 03:00:58 +0000391 ** source is a subroutine call from the SELECT statement, then we need
392 ** to launch the SELECT statement processing.
drh1ccde152000-06-17 13:12:39 +0000393 */
drh142e30d2002-08-28 03:00:58 +0000394 if( useTempTable ){
danielk19774adee202004-05-08 08:23:19 +0000395 iBreak = sqlite3VdbeMakeLabel(v);
396 sqlite3VdbeAddOp(v, OP_Rewind, srcTab, iBreak);
397 iCont = sqlite3VdbeCurrentAddr(v);
drh142e30d2002-08-28 03:00:58 +0000398 }else if( pSelect ){
danielk19774adee202004-05-08 08:23:19 +0000399 sqlite3VdbeAddOp(v, OP_Goto, 0, iSelectLoop);
400 sqlite3VdbeResolveLabel(v, iInsertBlock);
drh5974a302000-06-07 14:42:26 +0000401 }
drh1ccde152000-06-17 13:12:39 +0000402
drh5cf590c2003-04-24 01:45:04 +0000403 /* Run the BEFORE and INSTEAD OF triggers, if there are any
drh70ce3f02003-04-15 19:22:22 +0000404 */
danielk19774adee202004-05-08 08:23:19 +0000405 endOfLoop = sqlite3VdbeMakeLabel(v);
drh70ce3f02003-04-15 19:22:22 +0000406 if( before_triggers ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000407
drh70ce3f02003-04-15 19:22:22 +0000408 /* build the NEW.* reference row. Note that if there is an INTEGER
409 ** PRIMARY KEY into which a NULL is being inserted, that NULL will be
410 ** translated into a unique ID for the row. But on a BEFORE trigger,
411 ** we do not know what the unique ID will be (because the insert has
412 ** not happened yet) so we substitute a rowid of -1
413 */
414 if( keyColumn<0 ){
danielk19774adee202004-05-08 08:23:19 +0000415 sqlite3VdbeAddOp(v, OP_Integer, -1, 0);
drh70ce3f02003-04-15 19:22:22 +0000416 }else if( useTempTable ){
danielk19774adee202004-05-08 08:23:19 +0000417 sqlite3VdbeAddOp(v, OP_Column, srcTab, keyColumn);
drh70ce3f02003-04-15 19:22:22 +0000418 }else if( pSelect ){
danielk19774adee202004-05-08 08:23:19 +0000419 sqlite3VdbeAddOp(v, OP_Dup, nColumn - keyColumn - 1, 1);
drh70ce3f02003-04-15 19:22:22 +0000420 }else{
danielk19774adee202004-05-08 08:23:19 +0000421 sqlite3ExprCode(pParse, pList->a[keyColumn].pExpr);
422 sqlite3VdbeAddOp(v, OP_NotNull, -1, sqlite3VdbeCurrentAddr(v)+3);
423 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
424 sqlite3VdbeAddOp(v, OP_Integer, -1, 0);
425 sqlite3VdbeAddOp(v, OP_MustBeInt, 0, 0);
drh70ce3f02003-04-15 19:22:22 +0000426 }
427
428 /* Create the new column data
429 */
danielk1977c3f9bad2002-05-15 08:30:12 +0000430 for(i=0; i<pTab->nCol; i++){
431 if( pColumn==0 ){
drh9adf9ac2002-05-15 11:44:13 +0000432 j = i;
danielk1977c3f9bad2002-05-15 08:30:12 +0000433 }else{
drh9adf9ac2002-05-15 11:44:13 +0000434 for(j=0; j<pColumn->nId; j++){
435 if( pColumn->a[j].idx==i ) break;
436 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000437 }
438 if( pColumn && j>=pColumn->nId ){
danielk19774adee202004-05-08 08:23:19 +0000439 sqlite3VdbeOp3(v, OP_String, 0, 0, pTab->aCol[i].zDflt, P3_STATIC);
drh142e30d2002-08-28 03:00:58 +0000440 }else if( useTempTable ){
danielk19774adee202004-05-08 08:23:19 +0000441 sqlite3VdbeAddOp(v, OP_Column, srcTab, j);
drh142e30d2002-08-28 03:00:58 +0000442 }else if( pSelect ){
danielk19774adee202004-05-08 08:23:19 +0000443 sqlite3VdbeAddOp(v, OP_Dup, nColumn-j-1, 1);
danielk1977c3f9bad2002-05-15 08:30:12 +0000444 }else{
danielk19774adee202004-05-08 08:23:19 +0000445 sqlite3ExprCode(pParse, pList->a[j].pExpr);
danielk1977c3f9bad2002-05-15 08:30:12 +0000446 }
447 }
danielk19774adee202004-05-08 08:23:19 +0000448 sqlite3VdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0);
danielk19773d1bfea2004-05-14 11:00:53 +0000449 sqlite3AddRecordType(v, pTab);
danielk19774adee202004-05-08 08:23:19 +0000450 sqlite3VdbeAddOp(v, OP_PutIntKey, newIdx, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000451
drh5cf590c2003-04-24 01:45:04 +0000452 /* Fire BEFORE or INSTEAD OF triggers */
danielk19774adee202004-05-08 08:23:19 +0000453 if( sqlite3CodeRowTrigger(pParse, TK_INSERT, 0, TK_BEFORE, pTab,
drhb2fe7d82003-04-20 17:29:23 +0000454 newIdx, -1, onError, endOfLoop) ){
danielk1977f29ce552002-05-19 23:43:12 +0000455 goto insert_cleanup;
456 }
drh70ce3f02003-04-15 19:22:22 +0000457 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000458
drh70ce3f02003-04-15 19:22:22 +0000459 /* If any triggers exists, the opening of tables and indices is deferred
460 ** until now.
461 */
drh5cf590c2003-04-24 01:45:04 +0000462 if( row_triggers_exist && !isView ){
463 base = pParse->nTab;
danielk19774adee202004-05-08 08:23:19 +0000464 idx = sqlite3OpenTableAndIndices(pParse, pTab, base);
drh5cf590c2003-04-24 01:45:04 +0000465 pParse->nTab += idx;
danielk1977c3f9bad2002-05-15 08:30:12 +0000466 }
467
drh4a324312001-12-21 14:30:42 +0000468 /* Push the record number for the new entry onto the stack. The
469 ** record number is a randomly generate integer created by NewRecno
470 ** except when the table has an INTEGER PRIMARY KEY column, in which
drhb419a922002-01-30 16:17:23 +0000471 ** case the record number is the same as that column.
drh1ccde152000-06-17 13:12:39 +0000472 */
drh5cf590c2003-04-24 01:45:04 +0000473 if( !isView ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000474 if( keyColumn>=0 ){
drh142e30d2002-08-28 03:00:58 +0000475 if( useTempTable ){
danielk19774adee202004-05-08 08:23:19 +0000476 sqlite3VdbeAddOp(v, OP_Column, srcTab, keyColumn);
drh142e30d2002-08-28 03:00:58 +0000477 }else if( pSelect ){
danielk19774adee202004-05-08 08:23:19 +0000478 sqlite3VdbeAddOp(v, OP_Dup, nColumn - keyColumn - 1, 1);
danielk1977c3f9bad2002-05-15 08:30:12 +0000479 }else{
danielk19774adee202004-05-08 08:23:19 +0000480 sqlite3ExprCode(pParse, pList->a[keyColumn].pExpr);
danielk1977c3f9bad2002-05-15 08:30:12 +0000481 }
drh27a32782002-06-19 20:32:43 +0000482 /* If the PRIMARY KEY expression is NULL, then use OP_NewRecno
483 ** to generate a unique primary key value.
484 */
danielk19774adee202004-05-08 08:23:19 +0000485 sqlite3VdbeAddOp(v, OP_NotNull, -1, sqlite3VdbeCurrentAddr(v)+3);
486 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
487 sqlite3VdbeAddOp(v, OP_NewRecno, base, 0);
488 sqlite3VdbeAddOp(v, OP_MustBeInt, 0, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000489 }else{
danielk19774adee202004-05-08 08:23:19 +0000490 sqlite3VdbeAddOp(v, OP_NewRecno, base, 0);
drh4a324312001-12-21 14:30:42 +0000491 }
drh4a324312001-12-21 14:30:42 +0000492
danielk1977c3f9bad2002-05-15 08:30:12 +0000493 /* Push onto the stack, data for all columns of the new entry, beginning
danielk1977f29ce552002-05-19 23:43:12 +0000494 ** with the first column.
495 */
danielk1977c3f9bad2002-05-15 08:30:12 +0000496 for(i=0; i<pTab->nCol; i++){
497 if( i==pTab->iPKey ){
drh9adf9ac2002-05-15 11:44:13 +0000498 /* The value of the INTEGER PRIMARY KEY column is always a NULL.
danielk1977f29ce552002-05-19 23:43:12 +0000499 ** Whenever this column is read, the record number will be substituted
500 ** in its place. So will fill this column with a NULL to avoid
501 ** taking up data space with information that will never be used. */
danielk19774adee202004-05-08 08:23:19 +0000502 sqlite3VdbeAddOp(v, OP_String, 0, 0);
drh9adf9ac2002-05-15 11:44:13 +0000503 continue;
danielk1977c3f9bad2002-05-15 08:30:12 +0000504 }
505 if( pColumn==0 ){
drh9adf9ac2002-05-15 11:44:13 +0000506 j = i;
danielk1977c3f9bad2002-05-15 08:30:12 +0000507 }else{
drh9adf9ac2002-05-15 11:44:13 +0000508 for(j=0; j<pColumn->nId; j++){
509 if( pColumn->a[j].idx==i ) break;
510 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000511 }
512 if( pColumn && j>=pColumn->nId ){
danielk19774adee202004-05-08 08:23:19 +0000513 sqlite3VdbeOp3(v, OP_String, 0, 0, pTab->aCol[i].zDflt, P3_STATIC);
drh142e30d2002-08-28 03:00:58 +0000514 }else if( useTempTable ){
danielk19774adee202004-05-08 08:23:19 +0000515 sqlite3VdbeAddOp(v, OP_Column, srcTab, j);
drh142e30d2002-08-28 03:00:58 +0000516 }else if( pSelect ){
danielk19774adee202004-05-08 08:23:19 +0000517 sqlite3VdbeAddOp(v, OP_Dup, i+nColumn-j, 1);
danielk1977c3f9bad2002-05-15 08:30:12 +0000518 }else{
danielk19774adee202004-05-08 08:23:19 +0000519 sqlite3ExprCode(pParse, pList->a[j].pExpr);
drh5974a302000-06-07 14:42:26 +0000520 }
drhbed86902000-06-02 13:27:59 +0000521 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000522
523 /* Generate code to check constraints and generate index keys and
danielk1977f29ce552002-05-19 23:43:12 +0000524 ** do the insertion.
525 */
danielk19774adee202004-05-08 08:23:19 +0000526 sqlite3GenerateConstraintChecks(pParse, pTab, base, 0, keyColumn>=0,
drha0217ba2003-06-01 01:10:33 +0000527 0, onError, endOfLoop);
danielk19774adee202004-05-08 08:23:19 +0000528 sqlite3CompleteInsertion(pParse, pTab, base, 0,0,0,
drh70ce3f02003-04-15 19:22:22 +0000529 after_triggers ? newIdx : -1);
drh5cf590c2003-04-24 01:45:04 +0000530 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000531
drh5cf590c2003-04-24 01:45:04 +0000532 /* Update the count of rows that are inserted
533 */
534 if( (db->flags & SQLITE_CountRows)!=0 ){
danielk19774adee202004-05-08 08:23:19 +0000535 sqlite3VdbeAddOp(v, OP_MemIncr, iCntMem, 0);
drh5974a302000-06-07 14:42:26 +0000536 }
drh1ccde152000-06-17 13:12:39 +0000537
danielk1977f29ce552002-05-19 23:43:12 +0000538 if( row_triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000539 /* Close all tables opened */
drh5cf590c2003-04-24 01:45:04 +0000540 if( !isView ){
danielk19774adee202004-05-08 08:23:19 +0000541 sqlite3VdbeAddOp(v, OP_Close, base, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000542 for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
danielk19774adee202004-05-08 08:23:19 +0000543 sqlite3VdbeAddOp(v, OP_Close, idx+base, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000544 }
545 }
drh1bee3d72001-10-15 00:44:35 +0000546
danielk1977c3f9bad2002-05-15 08:30:12 +0000547 /* Code AFTER triggers */
danielk19774adee202004-05-08 08:23:19 +0000548 if( sqlite3CodeRowTrigger(pParse, TK_INSERT, 0, TK_AFTER, pTab, newIdx, -1,
danielk19776f349032002-06-11 02:25:40 +0000549 onError, endOfLoop) ){
danielk1977f29ce552002-05-19 23:43:12 +0000550 goto insert_cleanup;
551 }
drh1bee3d72001-10-15 00:44:35 +0000552 }
553
drh1ccde152000-06-17 13:12:39 +0000554 /* The bottom of the loop, if the data source is a SELECT statement
danielk1977f29ce552002-05-19 23:43:12 +0000555 */
danielk19774adee202004-05-08 08:23:19 +0000556 sqlite3VdbeResolveLabel(v, endOfLoop);
drh142e30d2002-08-28 03:00:58 +0000557 if( useTempTable ){
danielk19774adee202004-05-08 08:23:19 +0000558 sqlite3VdbeAddOp(v, OP_Next, srcTab, iCont);
559 sqlite3VdbeResolveLabel(v, iBreak);
560 sqlite3VdbeAddOp(v, OP_Close, srcTab, 0);
drh142e30d2002-08-28 03:00:58 +0000561 }else if( pSelect ){
danielk19774adee202004-05-08 08:23:19 +0000562 sqlite3VdbeAddOp(v, OP_Pop, nColumn, 0);
563 sqlite3VdbeAddOp(v, OP_Return, 0, 0);
564 sqlite3VdbeResolveLabel(v, iCleanup);
drh6b563442001-11-07 16:48:26 +0000565 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000566
danielk1977f29ce552002-05-19 23:43:12 +0000567 if( !row_triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000568 /* Close all tables opened */
danielk19774adee202004-05-08 08:23:19 +0000569 sqlite3VdbeAddOp(v, OP_Close, base, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000570 for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
danielk19774adee202004-05-08 08:23:19 +0000571 sqlite3VdbeAddOp(v, OP_Close, idx+base, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000572 }
drhcce7d172000-05-31 15:34:51 +0000573 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000574
danielk19774adee202004-05-08 08:23:19 +0000575 sqlite3VdbeAddOp(v, OP_SetCounts, 0, 0);
576 sqlite3EndWriteOperation(pParse);
drh5e00f6c2001-09-13 13:46:56 +0000577
drh1bee3d72001-10-15 00:44:35 +0000578 /*
danielk1977f29ce552002-05-19 23:43:12 +0000579 ** Return the number of rows inserted.
drh1bee3d72001-10-15 00:44:35 +0000580 */
drh142e30d2002-08-28 03:00:58 +0000581 if( db->flags & SQLITE_CountRows ){
danielk19774adee202004-05-08 08:23:19 +0000582 sqlite3VdbeOp3(v, OP_ColumnName, 0, 1, "rows inserted", P3_STATIC);
583 sqlite3VdbeAddOp(v, OP_MemLoad, iCntMem, 0);
584 sqlite3VdbeAddOp(v, OP_Callback, 1, 0);
drh1bee3d72001-10-15 00:44:35 +0000585 }
drhcce7d172000-05-31 15:34:51 +0000586
587insert_cleanup:
danielk19774adee202004-05-08 08:23:19 +0000588 sqlite3SrcListDelete(pTabList);
589 if( pList ) sqlite3ExprListDelete(pList);
590 if( pSelect ) sqlite3SelectDelete(pSelect);
591 sqlite3IdListDelete(pColumn);
drhcce7d172000-05-31 15:34:51 +0000592}
drh9cfcf5d2002-01-29 18:41:24 +0000593
drh9cfcf5d2002-01-29 18:41:24 +0000594/*
595** Generate code to do a constraint check prior to an INSERT or an UPDATE.
596**
597** When this routine is called, the stack contains (from bottom to top)
drh0ca3e242002-01-29 23:07:02 +0000598** the following values:
599**
drhb2fe7d82003-04-20 17:29:23 +0000600** 1. The recno of the row to be updated before the update. This
drhb419a922002-01-30 16:17:23 +0000601** value is omitted unless we are doing an UPDATE that involves a
602** change to the record number.
drh0ca3e242002-01-29 23:07:02 +0000603**
drhb419a922002-01-30 16:17:23 +0000604** 2. The recno of the row after the update.
drh0ca3e242002-01-29 23:07:02 +0000605**
606** 3. The data in the first column of the entry after the update.
607**
608** i. Data from middle columns...
609**
610** N. The data in the last column of the entry after the update.
611**
drhb419a922002-01-30 16:17:23 +0000612** The old recno shown as entry (1) above is omitted unless both isUpdate
drh1c928532002-01-31 15:54:21 +0000613** and recnoChng are 1. isUpdate is true for UPDATEs and false for
614** INSERTs and recnoChng is true if the record number is being changed.
drh0ca3e242002-01-29 23:07:02 +0000615**
616** The code generated by this routine pushes additional entries onto
617** the stack which are the keys for new index entries for the new record.
618** The order of index keys is the same as the order of the indices on
619** the pTable->pIndex list. A key is only created for index i if
620** aIdxUsed!=0 and aIdxUsed[i]!=0.
drh9cfcf5d2002-01-29 18:41:24 +0000621**
622** This routine also generates code to check constraints. NOT NULL,
623** CHECK, and UNIQUE constraints are all checked. If a constraint fails,
drh1c928532002-01-31 15:54:21 +0000624** then the appropriate action is performed. There are five possible
625** actions: ROLLBACK, ABORT, FAIL, REPLACE, and IGNORE.
drh9cfcf5d2002-01-29 18:41:24 +0000626**
627** Constraint type Action What Happens
628** --------------- ---------- ----------------------------------------
drh1c928532002-01-31 15:54:21 +0000629** any ROLLBACK The current transaction is rolled back and
danielk197724b03fd2004-05-10 10:34:34 +0000630** sqlite3_exec() returns immediately with a
drh9cfcf5d2002-01-29 18:41:24 +0000631** return code of SQLITE_CONSTRAINT.
632**
drh1c928532002-01-31 15:54:21 +0000633** any ABORT Back out changes from the current command
634** only (do not do a complete rollback) then
danielk197724b03fd2004-05-10 10:34:34 +0000635** cause sqlite3_exec() to return immediately
drh1c928532002-01-31 15:54:21 +0000636** with SQLITE_CONSTRAINT.
637**
638** any FAIL Sqlite_exec() returns immediately with a
639** return code of SQLITE_CONSTRAINT. The
640** transaction is not rolled back and any
641** prior changes are retained.
642**
drh9cfcf5d2002-01-29 18:41:24 +0000643** any IGNORE The record number and data is popped from
644** the stack and there is an immediate jump
645** to label ignoreDest.
646**
647** NOT NULL REPLACE The NULL value is replace by the default
648** value for that column. If the default value
649** is NULL, the action is the same as ABORT.
650**
651** UNIQUE REPLACE The other row that conflicts with the row
652** being inserted is removed.
653**
654** CHECK REPLACE Illegal. The results in an exception.
655**
drh1c928532002-01-31 15:54:21 +0000656** Which action to take is determined by the overrideError parameter.
657** Or if overrideError==OE_Default, then the pParse->onError parameter
658** is used. Or if pParse->onError==OE_Default then the onError value
659** for the constraint is used.
drh9cfcf5d2002-01-29 18:41:24 +0000660**
drhaaab5722002-02-19 13:39:21 +0000661** The calling routine must open a read/write cursor for pTab with
drh9cfcf5d2002-01-29 18:41:24 +0000662** cursor number "base". All indices of pTab must also have open
663** read/write cursors with cursor number base+i for the i-th cursor.
664** Except, if there is no possibility of a REPLACE action then
665** cursors do not need to be open for indices where aIdxUsed[i]==0.
666**
667** If the isUpdate flag is true, it means that the "base" cursor is
668** initially pointing to an entry that is being updated. The isUpdate
669** flag causes extra code to be generated so that the "base" cursor
670** is still pointing at the same entry after the routine returns.
671** Without the isUpdate flag, the "base" cursor might be moved.
672*/
danielk19774adee202004-05-08 08:23:19 +0000673void sqlite3GenerateConstraintChecks(
drh9cfcf5d2002-01-29 18:41:24 +0000674 Parse *pParse, /* The parser context */
675 Table *pTab, /* the table into which we are inserting */
676 int base, /* Index of a read/write cursor pointing at pTab */
677 char *aIdxUsed, /* Which indices are used. NULL means all are used */
drh0ca3e242002-01-29 23:07:02 +0000678 int recnoChng, /* True if the record number will change */
drhb419a922002-01-30 16:17:23 +0000679 int isUpdate, /* True for UPDATE, False for INSERT */
drh9cfcf5d2002-01-29 18:41:24 +0000680 int overrideError, /* Override onError to this if not OE_Default */
drhb419a922002-01-30 16:17:23 +0000681 int ignoreDest /* Jump to this label on an OE_Ignore resolution */
drh9cfcf5d2002-01-29 18:41:24 +0000682){
683 int i;
684 Vdbe *v;
685 int nCol;
686 int onError;
687 int addr;
688 int extra;
drh0ca3e242002-01-29 23:07:02 +0000689 int iCur;
690 Index *pIdx;
691 int seenReplace = 0;
drhf5905aa2002-05-26 20:54:33 +0000692 int jumpInst1, jumpInst2;
drh0ca3e242002-01-29 23:07:02 +0000693 int contAddr;
drhb419a922002-01-30 16:17:23 +0000694 int hasTwoRecnos = (isUpdate && recnoChng);
drh9cfcf5d2002-01-29 18:41:24 +0000695
danielk19774adee202004-05-08 08:23:19 +0000696 v = sqlite3GetVdbe(pParse);
drh9cfcf5d2002-01-29 18:41:24 +0000697 assert( v!=0 );
drh417be792002-03-03 18:59:40 +0000698 assert( pTab->pSelect==0 ); /* This table is not a VIEW */
drh9cfcf5d2002-01-29 18:41:24 +0000699 nCol = pTab->nCol;
700
701 /* Test all NOT NULL constraints.
702 */
703 for(i=0; i<nCol; i++){
drh0ca3e242002-01-29 23:07:02 +0000704 if( i==pTab->iPKey ){
drh0ca3e242002-01-29 23:07:02 +0000705 continue;
706 }
drh9cfcf5d2002-01-29 18:41:24 +0000707 onError = pTab->aCol[i].notNull;
drh0ca3e242002-01-29 23:07:02 +0000708 if( onError==OE_None ) continue;
drh9cfcf5d2002-01-29 18:41:24 +0000709 if( overrideError!=OE_Default ){
710 onError = overrideError;
drha996e472003-05-16 02:30:27 +0000711 }else if( pParse->db->onError!=OE_Default ){
drh0d65dc02002-02-03 00:56:09 +0000712 onError = pParse->db->onError;
drha996e472003-05-16 02:30:27 +0000713 }else if( onError==OE_Default ){
714 onError = OE_Abort;
drh9cfcf5d2002-01-29 18:41:24 +0000715 }
716 if( onError==OE_Replace && pTab->aCol[i].zDflt==0 ){
717 onError = OE_Abort;
718 }
danielk19774adee202004-05-08 08:23:19 +0000719 sqlite3VdbeAddOp(v, OP_Dup, nCol-1-i, 1);
720 addr = sqlite3VdbeAddOp(v, OP_NotNull, 1, 0);
drh9cfcf5d2002-01-29 18:41:24 +0000721 switch( onError ){
drh1c928532002-01-31 15:54:21 +0000722 case OE_Rollback:
723 case OE_Abort:
724 case OE_Fail: {
drh483750b2003-01-29 18:46:51 +0000725 char *zMsg = 0;
danielk19774adee202004-05-08 08:23:19 +0000726 sqlite3VdbeAddOp(v, OP_Halt, SQLITE_CONSTRAINT, onError);
727 sqlite3SetString(&zMsg, pTab->zName, ".", pTab->aCol[i].zName,
drh41743982003-12-06 21:43:55 +0000728 " may not be NULL", (char*)0);
danielk19774adee202004-05-08 08:23:19 +0000729 sqlite3VdbeChangeP3(v, -1, zMsg, P3_DYNAMIC);
drh9cfcf5d2002-01-29 18:41:24 +0000730 break;
731 }
732 case OE_Ignore: {
danielk19774adee202004-05-08 08:23:19 +0000733 sqlite3VdbeAddOp(v, OP_Pop, nCol+1+hasTwoRecnos, 0);
734 sqlite3VdbeAddOp(v, OP_Goto, 0, ignoreDest);
drh9cfcf5d2002-01-29 18:41:24 +0000735 break;
736 }
737 case OE_Replace: {
danielk19774adee202004-05-08 08:23:19 +0000738 sqlite3VdbeOp3(v, OP_String, 0, 0, pTab->aCol[i].zDflt, P3_STATIC);
739 sqlite3VdbeAddOp(v, OP_Push, nCol-i, 0);
drh9cfcf5d2002-01-29 18:41:24 +0000740 break;
741 }
drh0ca3e242002-01-29 23:07:02 +0000742 default: assert(0);
drh9cfcf5d2002-01-29 18:41:24 +0000743 }
danielk19774adee202004-05-08 08:23:19 +0000744 sqlite3VdbeChangeP2(v, addr, sqlite3VdbeCurrentAddr(v));
drh9cfcf5d2002-01-29 18:41:24 +0000745 }
746
747 /* Test all CHECK constraints
748 */
drh0bd1f4e2002-06-06 18:54:39 +0000749 /**** TBD ****/
drh9cfcf5d2002-01-29 18:41:24 +0000750
drh0bd1f4e2002-06-06 18:54:39 +0000751 /* If we have an INTEGER PRIMARY KEY, make sure the primary key
752 ** of the new record does not previously exist. Except, if this
753 ** is an UPDATE and the primary key is not changing, that is OK.
drh9cfcf5d2002-01-29 18:41:24 +0000754 */
drh5383ae52003-06-04 12:23:30 +0000755 if( recnoChng ){
drh0ca3e242002-01-29 23:07:02 +0000756 onError = pTab->keyConf;
757 if( overrideError!=OE_Default ){
758 onError = overrideError;
drha996e472003-05-16 02:30:27 +0000759 }else if( pParse->db->onError!=OE_Default ){
drh0d65dc02002-02-03 00:56:09 +0000760 onError = pParse->db->onError;
drha996e472003-05-16 02:30:27 +0000761 }else if( onError==OE_Default ){
762 onError = OE_Abort;
drh0ca3e242002-01-29 23:07:02 +0000763 }
drha0217ba2003-06-01 01:10:33 +0000764
drh5383ae52003-06-04 12:23:30 +0000765 if( isUpdate ){
danielk19774adee202004-05-08 08:23:19 +0000766 sqlite3VdbeAddOp(v, OP_Dup, nCol+1, 1);
767 sqlite3VdbeAddOp(v, OP_Dup, nCol+1, 1);
768 jumpInst1 = sqlite3VdbeAddOp(v, OP_Eq, 0, 0);
drh5383ae52003-06-04 12:23:30 +0000769 }
danielk19774adee202004-05-08 08:23:19 +0000770 sqlite3VdbeAddOp(v, OP_Dup, nCol, 1);
771 jumpInst2 = sqlite3VdbeAddOp(v, OP_NotExists, base, 0);
drh5383ae52003-06-04 12:23:30 +0000772 switch( onError ){
773 default: {
774 onError = OE_Abort;
775 /* Fall thru into the next case */
drh79b0c952002-05-21 12:56:43 +0000776 }
drh5383ae52003-06-04 12:23:30 +0000777 case OE_Rollback:
778 case OE_Abort:
779 case OE_Fail: {
danielk19774adee202004-05-08 08:23:19 +0000780 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, onError,
drh701a0ae2004-02-22 20:05:00 +0000781 "PRIMARY KEY must be unique", P3_STATIC);
drh5383ae52003-06-04 12:23:30 +0000782 break;
drh0ca3e242002-01-29 23:07:02 +0000783 }
drh5383ae52003-06-04 12:23:30 +0000784 case OE_Replace: {
danielk19774adee202004-05-08 08:23:19 +0000785 sqlite3GenerateRowIndexDelete(pParse->db, v, pTab, base, 0);
drh5383ae52003-06-04 12:23:30 +0000786 if( isUpdate ){
danielk19774adee202004-05-08 08:23:19 +0000787 sqlite3VdbeAddOp(v, OP_Dup, nCol+hasTwoRecnos, 1);
788 sqlite3VdbeAddOp(v, OP_MoveTo, base, 0);
drh5383ae52003-06-04 12:23:30 +0000789 }
790 seenReplace = 1;
791 break;
drh0ca3e242002-01-29 23:07:02 +0000792 }
drh5383ae52003-06-04 12:23:30 +0000793 case OE_Ignore: {
794 assert( seenReplace==0 );
danielk19774adee202004-05-08 08:23:19 +0000795 sqlite3VdbeAddOp(v, OP_Pop, nCol+1+hasTwoRecnos, 0);
796 sqlite3VdbeAddOp(v, OP_Goto, 0, ignoreDest);
drh5383ae52003-06-04 12:23:30 +0000797 break;
798 }
799 }
danielk19774adee202004-05-08 08:23:19 +0000800 contAddr = sqlite3VdbeCurrentAddr(v);
801 sqlite3VdbeChangeP2(v, jumpInst2, contAddr);
drh5383ae52003-06-04 12:23:30 +0000802 if( isUpdate ){
danielk19774adee202004-05-08 08:23:19 +0000803 sqlite3VdbeChangeP2(v, jumpInst1, contAddr);
804 sqlite3VdbeAddOp(v, OP_Dup, nCol+1, 1);
805 sqlite3VdbeAddOp(v, OP_MoveTo, base, 0);
drh0ca3e242002-01-29 23:07:02 +0000806 }
807 }
drh0bd1f4e2002-06-06 18:54:39 +0000808
809 /* Test all UNIQUE constraints by creating entries for each UNIQUE
810 ** index and making sure that duplicate entries do not already exist.
811 ** Add the new records to the indices as we go.
812 */
drhb2fe7d82003-04-20 17:29:23 +0000813 extra = -1;
814 for(iCur=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, iCur++){
815 if( aIdxUsed && aIdxUsed[iCur]==0 ) continue; /* Skip unused indices */
816 extra++;
817
818 /* Create a key for accessing the index entry */
danielk19774adee202004-05-08 08:23:19 +0000819 sqlite3VdbeAddOp(v, OP_Dup, nCol+extra, 1);
drh9cfcf5d2002-01-29 18:41:24 +0000820 for(i=0; i<pIdx->nColumn; i++){
821 int idx = pIdx->aiColumn[i];
822 if( idx==pTab->iPKey ){
danielk19774adee202004-05-08 08:23:19 +0000823 sqlite3VdbeAddOp(v, OP_Dup, i+extra+nCol+1, 1);
drh9cfcf5d2002-01-29 18:41:24 +0000824 }else{
danielk19774adee202004-05-08 08:23:19 +0000825 sqlite3VdbeAddOp(v, OP_Dup, i+extra+nCol-idx, 1);
drh9cfcf5d2002-01-29 18:41:24 +0000826 }
827 }
danielk19774adee202004-05-08 08:23:19 +0000828 jumpInst1 = sqlite3VdbeAddOp(v, OP_MakeIdxKey, pIdx->nColumn, 0);
danielk19773d68f032004-05-11 07:11:51 +0000829 sqlite3AddIdxKeyType(v, pIdx);
drhb2fe7d82003-04-20 17:29:23 +0000830
831 /* Find out what action to take in case there is an indexing conflict */
drh9cfcf5d2002-01-29 18:41:24 +0000832 onError = pIdx->onError;
drhb2fe7d82003-04-20 17:29:23 +0000833 if( onError==OE_None ) continue; /* pIdx is not a UNIQUE index */
drh9cfcf5d2002-01-29 18:41:24 +0000834 if( overrideError!=OE_Default ){
835 onError = overrideError;
drha996e472003-05-16 02:30:27 +0000836 }else if( pParse->db->onError!=OE_Default ){
drh0d65dc02002-02-03 00:56:09 +0000837 onError = pParse->db->onError;
drha996e472003-05-16 02:30:27 +0000838 }else if( onError==OE_Default ){
839 onError = OE_Abort;
drh9cfcf5d2002-01-29 18:41:24 +0000840 }
drh5383ae52003-06-04 12:23:30 +0000841 if( seenReplace ){
842 if( onError==OE_Ignore ) onError = OE_Replace;
843 else if( onError==OE_Fail ) onError = OE_Abort;
844 }
845
drhb2fe7d82003-04-20 17:29:23 +0000846
847 /* Check to see if the new index entry will be unique */
danielk19774adee202004-05-08 08:23:19 +0000848 sqlite3VdbeAddOp(v, OP_Dup, extra+nCol+1+hasTwoRecnos, 1);
849 jumpInst2 = sqlite3VdbeAddOp(v, OP_IsUnique, base+iCur+1, 0);
drhb2fe7d82003-04-20 17:29:23 +0000850
851 /* Generate code that executes if the new index entry is not unique */
drh9cfcf5d2002-01-29 18:41:24 +0000852 switch( onError ){
drh1c928532002-01-31 15:54:21 +0000853 case OE_Rollback:
854 case OE_Abort:
855 case OE_Fail: {
drh37ed48e2003-08-05 13:13:38 +0000856 int j, n1, n2;
857 char zErrMsg[200];
858 strcpy(zErrMsg, pIdx->nColumn>1 ? "columns " : "column ");
859 n1 = strlen(zErrMsg);
860 for(j=0; j<pIdx->nColumn && n1<sizeof(zErrMsg)-30; j++){
861 char *zCol = pTab->aCol[pIdx->aiColumn[j]].zName;
862 n2 = strlen(zCol);
863 if( j>0 ){
864 strcpy(&zErrMsg[n1], ", ");
865 n1 += 2;
866 }
867 if( n1+n2>sizeof(zErrMsg)-30 ){
868 strcpy(&zErrMsg[n1], "...");
869 n1 += 3;
870 break;
871 }else{
872 strcpy(&zErrMsg[n1], zCol);
873 n1 += n2;
874 }
875 }
876 strcpy(&zErrMsg[n1],
877 pIdx->nColumn>1 ? " are not unique" : " is not unique");
danielk19774adee202004-05-08 08:23:19 +0000878 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, onError, zErrMsg, 0);
drh9cfcf5d2002-01-29 18:41:24 +0000879 break;
880 }
881 case OE_Ignore: {
drh0ca3e242002-01-29 23:07:02 +0000882 assert( seenReplace==0 );
danielk19774adee202004-05-08 08:23:19 +0000883 sqlite3VdbeAddOp(v, OP_Pop, nCol+extra+3+hasTwoRecnos, 0);
884 sqlite3VdbeAddOp(v, OP_Goto, 0, ignoreDest);
drh9cfcf5d2002-01-29 18:41:24 +0000885 break;
886 }
887 case OE_Replace: {
danielk19774adee202004-05-08 08:23:19 +0000888 sqlite3GenerateRowDelete(pParse->db, v, pTab, base, 0);
drh9cfcf5d2002-01-29 18:41:24 +0000889 if( isUpdate ){
danielk19774adee202004-05-08 08:23:19 +0000890 sqlite3VdbeAddOp(v, OP_Dup, nCol+extra+1+hasTwoRecnos, 1);
891 sqlite3VdbeAddOp(v, OP_MoveTo, base, 0);
drh9cfcf5d2002-01-29 18:41:24 +0000892 }
drh0ca3e242002-01-29 23:07:02 +0000893 seenReplace = 1;
drh9cfcf5d2002-01-29 18:41:24 +0000894 break;
895 }
drh0ca3e242002-01-29 23:07:02 +0000896 default: assert(0);
drh9cfcf5d2002-01-29 18:41:24 +0000897 }
danielk19774adee202004-05-08 08:23:19 +0000898 contAddr = sqlite3VdbeCurrentAddr(v);
drh0bd1f4e2002-06-06 18:54:39 +0000899#if NULL_DISTINCT_FOR_UNIQUE
danielk19774adee202004-05-08 08:23:19 +0000900 sqlite3VdbeChangeP2(v, jumpInst1, contAddr);
drh0bd1f4e2002-06-06 18:54:39 +0000901#endif
danielk19774adee202004-05-08 08:23:19 +0000902 sqlite3VdbeChangeP2(v, jumpInst2, contAddr);
drh9cfcf5d2002-01-29 18:41:24 +0000903 }
904}
drh0ca3e242002-01-29 23:07:02 +0000905
906/*
907** This routine generates code to finish the INSERT or UPDATE operation
danielk19774adee202004-05-08 08:23:19 +0000908** that was started by a prior call to sqlite3GenerateConstraintChecks.
drh0ca3e242002-01-29 23:07:02 +0000909** The stack must contain keys for all active indices followed by data
910** and the recno for the new entry. This routine creates the new
911** entries in all indices and in the main table.
912**
drhb419a922002-01-30 16:17:23 +0000913** The arguments to this routine should be the same as the first six
danielk19774adee202004-05-08 08:23:19 +0000914** arguments to sqlite3GenerateConstraintChecks.
drh0ca3e242002-01-29 23:07:02 +0000915*/
danielk19774adee202004-05-08 08:23:19 +0000916void sqlite3CompleteInsertion(
drh0ca3e242002-01-29 23:07:02 +0000917 Parse *pParse, /* The parser context */
918 Table *pTab, /* the table into which we are inserting */
919 int base, /* Index of a read/write cursor pointing at pTab */
920 char *aIdxUsed, /* Which indices are used. NULL means all are used */
drhb419a922002-01-30 16:17:23 +0000921 int recnoChng, /* True if the record number will change */
drh70ce3f02003-04-15 19:22:22 +0000922 int isUpdate, /* True for UPDATE, False for INSERT */
923 int newIdx /* Index of NEW table for triggers. -1 if none */
drh0ca3e242002-01-29 23:07:02 +0000924){
925 int i;
926 Vdbe *v;
927 int nIdx;
928 Index *pIdx;
929
danielk19774adee202004-05-08 08:23:19 +0000930 v = sqlite3GetVdbe(pParse);
drh0ca3e242002-01-29 23:07:02 +0000931 assert( v!=0 );
drh417be792002-03-03 18:59:40 +0000932 assert( pTab->pSelect==0 ); /* This table is not a VIEW */
drh0ca3e242002-01-29 23:07:02 +0000933 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){}
934 for(i=nIdx-1; i>=0; i--){
935 if( aIdxUsed && aIdxUsed[i]==0 ) continue;
danielk19774adee202004-05-08 08:23:19 +0000936 sqlite3VdbeAddOp(v, OP_IdxPut, base+i+1, 0);
drh0ca3e242002-01-29 23:07:02 +0000937 }
danielk19774adee202004-05-08 08:23:19 +0000938 sqlite3VdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0);
danielk19773d1bfea2004-05-14 11:00:53 +0000939 sqlite3AddRecordType(v, pTab);
drh70ce3f02003-04-15 19:22:22 +0000940 if( newIdx>=0 ){
danielk19774adee202004-05-08 08:23:19 +0000941 sqlite3VdbeAddOp(v, OP_Dup, 1, 0);
942 sqlite3VdbeAddOp(v, OP_Dup, 1, 0);
943 sqlite3VdbeAddOp(v, OP_PutIntKey, newIdx, 0);
drh70ce3f02003-04-15 19:22:22 +0000944 }
danielk19774adee202004-05-08 08:23:19 +0000945 sqlite3VdbeAddOp(v, OP_PutIntKey, base,
rdcb0c374f2004-02-20 22:53:38 +0000946 (pParse->trigStack?0:OPFLAG_NCHANGE) |
947 (isUpdate?0:OPFLAG_LASTROWID) | OPFLAG_CSCHANGE);
drhb419a922002-01-30 16:17:23 +0000948 if( isUpdate && recnoChng ){
danielk19774adee202004-05-08 08:23:19 +0000949 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drh0ca3e242002-01-29 23:07:02 +0000950 }
951}
drhcd446902004-02-24 01:05:31 +0000952
953/*
954** Generate code that will open write cursors for a table and for all
955** indices of that table. The "base" parameter is the cursor number used
956** for the table. Indices are opened on subsequent cursors.
957**
958** Return the total number of cursors opened. This is always at least
959** 1 (for the main table) plus more for each cursor.
960*/
danielk19774adee202004-05-08 08:23:19 +0000961int sqlite3OpenTableAndIndices(Parse *pParse, Table *pTab, int base){
drhcd446902004-02-24 01:05:31 +0000962 int i;
963 Index *pIdx;
danielk19774adee202004-05-08 08:23:19 +0000964 Vdbe *v = sqlite3GetVdbe(pParse);
drhcd446902004-02-24 01:05:31 +0000965 assert( v!=0 );
danielk19774adee202004-05-08 08:23:19 +0000966 sqlite3VdbeAddOp(v, OP_Integer, pTab->iDb, 0);
967 sqlite3VdbeOp3(v, OP_OpenWrite, base, pTab->tnum, pTab->zName, P3_STATIC);
drhcd446902004-02-24 01:05:31 +0000968 for(i=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
danielk19774adee202004-05-08 08:23:19 +0000969 sqlite3VdbeAddOp(v, OP_Integer, pIdx->iDb, 0);
970 sqlite3VdbeOp3(v, OP_OpenWrite, i+base, pIdx->tnum, pIdx->zName, P3_STATIC);
drhcd446902004-02-24 01:05:31 +0000971 }
972 return i;
973}
danielk19774adee202004-05-08 08:23:19 +0000974
975
976