blob: c122643d4df535d8b69389daf0a5b35b6d84f4b3 [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**
drhd3d39e92004-05-20 22:16:29 +000015** $Id: insert.c,v 1.105 2004/05/20 22:16:29 drh 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
danielk1977a37cdde2004-05-16 11:15:36 +000021** string for index pIdx. A column affinity string has one character
danielk19773d1bfea2004-05-14 11:00:53 +000022** 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*/
danielk1977a37cdde2004-05-16 11:15:36 +000031void sqlite3IndexAffinityStr(Vdbe *v, Index *pIdx){
32 if( !pIdx->zColAff ){
danielk1977e014a832004-05-17 10:48:57 +000033 /* The first time a column affinity string for a particular index is
danielk1977a37cdde2004-05-16 11:15:36 +000034 ** required, it is allocated and populated here. It is then stored as
danielk1977e014a832004-05-17 10:48:57 +000035 ** a member of the Index structure for subsequent use.
danielk1977a37cdde2004-05-16 11:15:36 +000036 **
37 ** The column affinity string will eventually be deleted by
danielk1977e014a832004-05-17 10:48:57 +000038 ** sqliteDeleteIndex() when the Index structure itself is cleaned
danielk1977a37cdde2004-05-16 11:15:36 +000039 ** up.
40 */
41 int n;
42 Table *pTab = pIdx->pTable;
43 pIdx->zColAff = (char *)sqliteMalloc(pIdx->nColumn+1);
44 if( !pIdx->zColAff ){
45 return;
46 }
47 for(n=0; n<pIdx->nColumn; n++){
48 pIdx->zColAff[n] = pTab->aCol[pIdx->aiColumn[n]].affinity;
49 }
50 pIdx->zColAff[pIdx->nColumn] = '\0';
51 }
danielk19773d1bfea2004-05-14 11:00:53 +000052
danielk1977a37cdde2004-05-16 11:15:36 +000053 sqlite3VdbeChangeP3(v, -1, pIdx->zColAff, P3_STATIC);
54}
55
56/*
57** Set P3 of the most recently inserted opcode to a column affinity
58** string for table pTab. A column affinity string has one character
59** for each column indexed by the index, according to the affinity of the
60** column:
61**
62** Character Column affinity
63** ------------------------------
64** 'n' NUMERIC
65** 'i' INTEGER
66** 't' TEXT
67** 'o' NONE
68*/
69void sqlite3TableAffinityStr(Vdbe *v, Table *pTab){
danielk19773d1bfea2004-05-14 11:00:53 +000070 /* The first time a column affinity string for a particular table
71 ** is required, it is allocated and populated here. It is then
72 ** stored as a member of the Table structure for subsequent use.
73 **
74 ** The column affinity string will eventually be deleted by
75 ** sqlite3DeleteTable() when the Table structure itself is cleaned up.
76 */
77 if( !pTab->zColAff ){
78 char *zColAff;
79 int i;
80
danielk1977a37cdde2004-05-16 11:15:36 +000081 zColAff = (char *)sqliteMalloc(pTab->nCol+1);
danielk19773d1bfea2004-05-14 11:00:53 +000082 if( !zColAff ){
danielk1977a37cdde2004-05-16 11:15:36 +000083 return;
danielk19773d1bfea2004-05-14 11:00:53 +000084 }
85
86 for(i=0; i<pTab->nCol; i++){
danielk1977a37cdde2004-05-16 11:15:36 +000087 zColAff[i] = pTab->aCol[i].affinity;
danielk19773d1bfea2004-05-14 11:00:53 +000088 }
89 zColAff[pTab->nCol] = '\0';
90
91 pTab->zColAff = zColAff;
92 }
93
danielk19773d1bfea2004-05-14 11:00:53 +000094 sqlite3VdbeChangeP3(v, -1, pTab->zColAff, P3_STATIC);
danielk19773d1bfea2004-05-14 11:00:53 +000095}
96
97
98/*
drh1ccde152000-06-17 13:12:39 +000099** This routine is call to handle SQL of the following forms:
drhcce7d172000-05-31 15:34:51 +0000100**
101** insert into TABLE (IDLIST) values(EXPRLIST)
drh1ccde152000-06-17 13:12:39 +0000102** insert into TABLE (IDLIST) select
drhcce7d172000-05-31 15:34:51 +0000103**
drh1ccde152000-06-17 13:12:39 +0000104** The IDLIST following the table name is always optional. If omitted,
105** then a list of all columns for the table is substituted. The IDLIST
drh967e8b72000-06-21 13:59:10 +0000106** appears in the pColumn parameter. pColumn is NULL if IDLIST is omitted.
drh1ccde152000-06-17 13:12:39 +0000107**
108** The pList parameter holds EXPRLIST in the first form of the INSERT
109** statement above, and pSelect is NULL. For the second form, pList is
110** NULL and pSelect is a pointer to the select statement used to generate
111** data for the insert.
drh142e30d2002-08-28 03:00:58 +0000112**
113** The code generated follows one of three templates. For a simple
114** select with data coming from a VALUES clause, the code executes
115** once straight down through. The template looks like this:
116**
117** open write cursor to <table> and its indices
118** puts VALUES clause expressions onto the stack
119** write the resulting record into <table>
120** cleanup
121**
122** If the statement is of the form
123**
124** INSERT INTO <table> SELECT ...
125**
126** And the SELECT clause does not read from <table> at any time, then
127** the generated code follows this template:
128**
129** goto B
130** A: setup for the SELECT
131** loop over the tables in the SELECT
132** gosub C
133** end loop
134** cleanup after the SELECT
135** goto D
136** B: open write cursor to <table> and its indices
137** goto A
138** C: insert the select result into <table>
139** return
140** D: cleanup
141**
142** The third template is used if the insert statement takes its
143** values from a SELECT but the data is being inserted into a table
144** that is also read as part of the SELECT. In the third form,
145** we have to use a intermediate table to store the results of
146** the select. The template is like this:
147**
148** goto B
149** A: setup for the SELECT
150** loop over the tables in the SELECT
151** gosub C
152** end loop
153** cleanup after the SELECT
154** goto D
155** C: insert the select result into the intermediate table
156** return
157** B: open a cursor to an intermediate table
158** goto A
159** D: open write cursor to <table> and its indices
160** loop over the intermediate table
161** transfer values form intermediate table into <table>
162** end the loop
163** cleanup
drhcce7d172000-05-31 15:34:51 +0000164*/
danielk19774adee202004-05-08 08:23:19 +0000165void sqlite3Insert(
drhcce7d172000-05-31 15:34:51 +0000166 Parse *pParse, /* Parser context */
drh113088e2003-03-20 01:16:58 +0000167 SrcList *pTabList, /* Name of table into which we are inserting */
drhcce7d172000-05-31 15:34:51 +0000168 ExprList *pList, /* List of values to be inserted */
drh5974a302000-06-07 14:42:26 +0000169 Select *pSelect, /* A SELECT statement to use as the data source */
drh9cfcf5d2002-01-29 18:41:24 +0000170 IdList *pColumn, /* Column names corresponding to IDLIST. */
171 int onError /* How to handle constraint errors */
drhcce7d172000-05-31 15:34:51 +0000172){
drh5974a302000-06-07 14:42:26 +0000173 Table *pTab; /* The table to insert into */
drh113088e2003-03-20 01:16:58 +0000174 char *zTab; /* Name of the table into which we are inserting */
drhe22a3342003-04-22 20:30:37 +0000175 const char *zDb; /* Name of the database holding this table */
drh5974a302000-06-07 14:42:26 +0000176 int i, j, idx; /* Loop counters */
177 Vdbe *v; /* Generate code into this virtual machine */
178 Index *pIdx; /* For looping over indices of the table */
drh967e8b72000-06-21 13:59:10 +0000179 int nColumn; /* Number of columns in the data */
drh6a3ea0e2003-05-02 14:32:12 +0000180 int base; /* VDBE Cursor number for pTab */
drh5974a302000-06-07 14:42:26 +0000181 int iCont, iBreak; /* Beginning and end of the loop over srcTab */
drhecdc7532001-09-23 02:35:53 +0000182 sqlite *db; /* The main database structure */
drh4a324312001-12-21 14:30:42 +0000183 int keyColumn = -1; /* Column that is the INTEGER PRIMARY KEY */
drh0ca3e242002-01-29 23:07:02 +0000184 int endOfLoop; /* Label for the end of the insertion loop */
drh142e30d2002-08-28 03:00:58 +0000185 int useTempTable; /* Store SELECT results in intermediate table */
186 int srcTab; /* Data comes from this temporary cursor if >=0 */
187 int iSelectLoop; /* Address of code that implements the SELECT */
188 int iCleanup; /* Address of the cleanup code */
189 int iInsertBlock; /* Address of the subroutine used to insert data */
190 int iCntMem; /* Memory cell used for the row counter */
drh5cf590c2003-04-24 01:45:04 +0000191 int isView; /* True if attempting to insert into a view */
drhcce7d172000-05-31 15:34:51 +0000192
danielk1977c3f9bad2002-05-15 08:30:12 +0000193 int row_triggers_exist = 0; /* True if there are FOR EACH ROW triggers */
drh70ce3f02003-04-15 19:22:22 +0000194 int before_triggers; /* True if there are BEFORE triggers */
195 int after_triggers; /* True if there are AFTER triggers */
196 int newIdx = -1; /* Cursor for the NEW table */
danielk1977c3f9bad2002-05-15 08:30:12 +0000197
danielk197724b03fd2004-05-10 10:34:34 +0000198 if( pParse->nErr || sqlite3_malloc_failed ) goto insert_cleanup;
drhecdc7532001-09-23 02:35:53 +0000199 db = pParse->db;
drhdaffd0e2001-04-11 14:28:42 +0000200
drh1ccde152000-06-17 13:12:39 +0000201 /* Locate the table into which we will be inserting new information.
202 */
drh113088e2003-03-20 01:16:58 +0000203 assert( pTabList->nSrc==1 );
204 zTab = pTabList->a[0].zName;
drhdaffd0e2001-04-11 14:28:42 +0000205 if( zTab==0 ) goto insert_cleanup;
danielk19774adee202004-05-08 08:23:19 +0000206 pTab = sqlite3SrcListLookup(pParse, pTabList);
danielk1977c3f9bad2002-05-15 08:30:12 +0000207 if( pTab==0 ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000208 goto insert_cleanup;
209 }
drhe22a3342003-04-22 20:30:37 +0000210 assert( pTab->iDb<db->nDb );
211 zDb = db->aDb[pTab->iDb].zName;
danielk19774adee202004-05-08 08:23:19 +0000212 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, pTab->zName, 0, zDb) ){
drh1962bda2003-01-12 19:33:52 +0000213 goto insert_cleanup;
214 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000215
216 /* Ensure that:
217 * (a) the table is not read-only,
218 * (b) that if it is a view then ON INSERT triggers exist
219 */
danielk19774adee202004-05-08 08:23:19 +0000220 before_triggers = sqlite3TriggersExist(pParse, pTab->pTrigger, TK_INSERT,
drh70ce3f02003-04-15 19:22:22 +0000221 TK_BEFORE, TK_ROW, 0);
danielk19774adee202004-05-08 08:23:19 +0000222 after_triggers = sqlite3TriggersExist(pParse, pTab->pTrigger, TK_INSERT,
drh70ce3f02003-04-15 19:22:22 +0000223 TK_AFTER, TK_ROW, 0);
224 row_triggers_exist = before_triggers || after_triggers;
drh5cf590c2003-04-24 01:45:04 +0000225 isView = pTab->pSelect!=0;
danielk19774adee202004-05-08 08:23:19 +0000226 if( sqlite3IsReadOnly(pParse, pTab, before_triggers) ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000227 goto insert_cleanup;
228 }
drha76b5df2002-02-23 02:32:10 +0000229 if( pTab==0 ) goto insert_cleanup;
drh1ccde152000-06-17 13:12:39 +0000230
drhf573c992002-07-31 00:32:50 +0000231 /* If pTab is really a view, make sure it has been initialized.
232 */
danielk19774adee202004-05-08 08:23:19 +0000233 if( isView && sqlite3ViewGetColumnNames(pParse, pTab) ){
drh5cf590c2003-04-24 01:45:04 +0000234 goto insert_cleanup;
drhf573c992002-07-31 00:32:50 +0000235 }
236
drh1ccde152000-06-17 13:12:39 +0000237 /* Allocate a VDBE
238 */
danielk19774adee202004-05-08 08:23:19 +0000239 v = sqlite3GetVdbe(pParse);
drh5974a302000-06-07 14:42:26 +0000240 if( v==0 ) goto insert_cleanup;
danielk19774adee202004-05-08 08:23:19 +0000241 sqlite3BeginWriteOperation(pParse, pSelect || row_triggers_exist, pTab->iDb);
drh1ccde152000-06-17 13:12:39 +0000242
danielk1977c3f9bad2002-05-15 08:30:12 +0000243 /* if there are row triggers, allocate a temp table for new.* references. */
danielk1977f29ce552002-05-19 23:43:12 +0000244 if( row_triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000245 newIdx = pParse->nTab++;
danielk1977f29ce552002-05-19 23:43:12 +0000246 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000247
drh1ccde152000-06-17 13:12:39 +0000248 /* Figure out how many columns of data are supplied. If the data
drh142e30d2002-08-28 03:00:58 +0000249 ** is coming from a SELECT statement, then this step also generates
250 ** all the code to implement the SELECT statement and invoke a subroutine
251 ** to process each row of the result. (Template 2.) If the SELECT
252 ** statement uses the the table that is being inserted into, then the
253 ** subroutine is also coded here. That subroutine stores the SELECT
254 ** results in a temporary table. (Template 3.)
drh1ccde152000-06-17 13:12:39 +0000255 */
drh5974a302000-06-07 14:42:26 +0000256 if( pSelect ){
drh142e30d2002-08-28 03:00:58 +0000257 /* Data is coming from a SELECT. Generate code to implement that SELECT
258 */
259 int rc, iInitCode;
danielk19774adee202004-05-08 08:23:19 +0000260 iInitCode = sqlite3VdbeAddOp(v, OP_Goto, 0, 0);
261 iSelectLoop = sqlite3VdbeCurrentAddr(v);
262 iInsertBlock = sqlite3VdbeMakeLabel(v);
danielk197784ac9d02004-05-18 09:58:06 +0000263 rc = sqlite3Select(pParse, pSelect, SRT_Subroutine, iInsertBlock, 0,0,0,0);
danielk197724b03fd2004-05-10 10:34:34 +0000264 if( rc || pParse->nErr || sqlite3_malloc_failed ) goto insert_cleanup;
danielk19774adee202004-05-08 08:23:19 +0000265 iCleanup = sqlite3VdbeMakeLabel(v);
266 sqlite3VdbeAddOp(v, OP_Goto, 0, iCleanup);
drh5974a302000-06-07 14:42:26 +0000267 assert( pSelect->pEList );
drh967e8b72000-06-21 13:59:10 +0000268 nColumn = pSelect->pEList->nExpr;
drh142e30d2002-08-28 03:00:58 +0000269
270 /* Set useTempTable to TRUE if the result of the SELECT statement
271 ** should be written into a temporary table. Set to FALSE if each
272 ** row of the SELECT can be written directly into the result table.
drh048c5302003-04-03 01:50:44 +0000273 **
274 ** A temp table must be used if the table being updated is also one
275 ** of the tables being read by the SELECT statement. Also use a
276 ** temp table in the case of row triggers.
drh142e30d2002-08-28 03:00:58 +0000277 */
drh048c5302003-04-03 01:50:44 +0000278 if( row_triggers_exist ){
279 useTempTable = 1;
280 }else{
danielk197784ac9d02004-05-18 09:58:06 +0000281 int addr = sqlite3VdbeFindOp(v, 0, OP_OpenRead, pTab->tnum);
drh048c5302003-04-03 01:50:44 +0000282 useTempTable = 0;
283 if( addr>0 ){
danielk19774adee202004-05-08 08:23:19 +0000284 VdbeOp *pOp = sqlite3VdbeGetOp(v, addr-2);
drh048c5302003-04-03 01:50:44 +0000285 if( pOp->opcode==OP_Integer && pOp->p1==pTab->iDb ){
286 useTempTable = 1;
287 }
288 }
289 }
drh142e30d2002-08-28 03:00:58 +0000290
291 if( useTempTable ){
292 /* Generate the subroutine that SELECT calls to process each row of
293 ** the result. Store the result in a temporary table
294 */
295 srcTab = pParse->nTab++;
danielk19774adee202004-05-08 08:23:19 +0000296 sqlite3VdbeResolveLabel(v, iInsertBlock);
297 sqlite3VdbeAddOp(v, OP_MakeRecord, nColumn, 0);
danielk1977a37cdde2004-05-16 11:15:36 +0000298 sqlite3TableAffinityStr(v, pTab);
danielk19774adee202004-05-08 08:23:19 +0000299 sqlite3VdbeAddOp(v, OP_NewRecno, srcTab, 0);
300 sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
301 sqlite3VdbeAddOp(v, OP_PutIntKey, srcTab, 0);
302 sqlite3VdbeAddOp(v, OP_Return, 0, 0);
drh142e30d2002-08-28 03:00:58 +0000303
304 /* The following code runs first because the GOTO at the very top
305 ** of the program jumps to it. Create the temporary table, then jump
306 ** back up and execute the SELECT code above.
307 */
danielk19774adee202004-05-08 08:23:19 +0000308 sqlite3VdbeChangeP2(v, iInitCode, sqlite3VdbeCurrentAddr(v));
309 sqlite3VdbeAddOp(v, OP_OpenTemp, srcTab, 0);
drhb6f54522004-05-20 02:42:16 +0000310 sqlite3VdbeAddOp(v, OP_SetNumColumns, srcTab, nColumn);
danielk19774adee202004-05-08 08:23:19 +0000311 sqlite3VdbeAddOp(v, OP_Goto, 0, iSelectLoop);
312 sqlite3VdbeResolveLabel(v, iCleanup);
drh142e30d2002-08-28 03:00:58 +0000313 }else{
danielk19774adee202004-05-08 08:23:19 +0000314 sqlite3VdbeChangeP2(v, iInitCode, sqlite3VdbeCurrentAddr(v));
drh142e30d2002-08-28 03:00:58 +0000315 }
drh5974a302000-06-07 14:42:26 +0000316 }else{
drh142e30d2002-08-28 03:00:58 +0000317 /* This is the case if the data for the INSERT is coming from a VALUES
318 ** clause
319 */
drhad3cab52002-05-24 02:04:32 +0000320 SrcList dummy;
drhdaffd0e2001-04-11 14:28:42 +0000321 assert( pList!=0 );
drh5974a302000-06-07 14:42:26 +0000322 srcTab = -1;
drh142e30d2002-08-28 03:00:58 +0000323 useTempTable = 0;
drh5974a302000-06-07 14:42:26 +0000324 assert( pList );
drh967e8b72000-06-21 13:59:10 +0000325 nColumn = pList->nExpr;
drhad3cab52002-05-24 02:04:32 +0000326 dummy.nSrc = 0;
drhe64e7b22002-02-18 13:56:36 +0000327 for(i=0; i<nColumn; i++){
danielk19774adee202004-05-08 08:23:19 +0000328 if( sqlite3ExprResolveIds(pParse, &dummy, 0, pList->a[i].pExpr) ){
drhe64e7b22002-02-18 13:56:36 +0000329 goto insert_cleanup;
330 }
danielk19774adee202004-05-08 08:23:19 +0000331 if( sqlite3ExprCheck(pParse, pList->a[i].pExpr, 0, 0) ){
drhb04a5d82002-04-12 03:55:15 +0000332 goto insert_cleanup;
333 }
drhe64e7b22002-02-18 13:56:36 +0000334 }
drh5974a302000-06-07 14:42:26 +0000335 }
drh1ccde152000-06-17 13:12:39 +0000336
337 /* Make sure the number of columns in the source data matches the number
338 ** of columns to be inserted into the table.
339 */
drh967e8b72000-06-21 13:59:10 +0000340 if( pColumn==0 && nColumn!=pTab->nCol ){
danielk19774adee202004-05-08 08:23:19 +0000341 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +0000342 "table %S has %d columns but %d values were supplied",
343 pTabList, 0, pTab->nCol, nColumn);
drhcce7d172000-05-31 15:34:51 +0000344 goto insert_cleanup;
345 }
drh967e8b72000-06-21 13:59:10 +0000346 if( pColumn!=0 && nColumn!=pColumn->nId ){
danielk19774adee202004-05-08 08:23:19 +0000347 sqlite3ErrorMsg(pParse, "%d values for %d columns", nColumn, pColumn->nId);
drhcce7d172000-05-31 15:34:51 +0000348 goto insert_cleanup;
349 }
drh1ccde152000-06-17 13:12:39 +0000350
351 /* If the INSERT statement included an IDLIST term, then make sure
352 ** all elements of the IDLIST really are columns of the table and
353 ** remember the column indices.
drhc8392582001-12-31 02:48:51 +0000354 **
355 ** If the table has an INTEGER PRIMARY KEY column and that column
356 ** is named in the IDLIST, then record in the keyColumn variable
357 ** the index into IDLIST of the primary key column. keyColumn is
358 ** the index of the primary key as it appears in IDLIST, not as
359 ** is appears in the original table. (The index of the primary
360 ** key in the original table is pTab->iPKey.)
drh1ccde152000-06-17 13:12:39 +0000361 */
drh967e8b72000-06-21 13:59:10 +0000362 if( pColumn ){
363 for(i=0; i<pColumn->nId; i++){
364 pColumn->a[i].idx = -1;
drhcce7d172000-05-31 15:34:51 +0000365 }
drh967e8b72000-06-21 13:59:10 +0000366 for(i=0; i<pColumn->nId; i++){
drhcce7d172000-05-31 15:34:51 +0000367 for(j=0; j<pTab->nCol; j++){
danielk19774adee202004-05-08 08:23:19 +0000368 if( sqlite3StrICmp(pColumn->a[i].zName, pTab->aCol[j].zName)==0 ){
drh967e8b72000-06-21 13:59:10 +0000369 pColumn->a[i].idx = j;
drh4a324312001-12-21 14:30:42 +0000370 if( j==pTab->iPKey ){
drh9aa028d2001-12-22 21:48:29 +0000371 keyColumn = i;
drh4a324312001-12-21 14:30:42 +0000372 }
drhcce7d172000-05-31 15:34:51 +0000373 break;
374 }
375 }
376 if( j>=pTab->nCol ){
danielk19774adee202004-05-08 08:23:19 +0000377 if( sqlite3IsRowid(pColumn->a[i].zName) ){
drha0217ba2003-06-01 01:10:33 +0000378 keyColumn = i;
379 }else{
danielk19774adee202004-05-08 08:23:19 +0000380 sqlite3ErrorMsg(pParse, "table %S has no column named %s",
drha0217ba2003-06-01 01:10:33 +0000381 pTabList, 0, pColumn->a[i].zName);
382 pParse->nErr++;
383 goto insert_cleanup;
384 }
drhcce7d172000-05-31 15:34:51 +0000385 }
386 }
387 }
drh1ccde152000-06-17 13:12:39 +0000388
drhaacc5432002-01-06 17:07:40 +0000389 /* If there is no IDLIST term but the table has an integer primary
drhc8392582001-12-31 02:48:51 +0000390 ** key, the set the keyColumn variable to the primary key column index
391 ** in the original table definition.
drh4a324312001-12-21 14:30:42 +0000392 */
393 if( pColumn==0 ){
394 keyColumn = pTab->iPKey;
395 }
396
drh142e30d2002-08-28 03:00:58 +0000397 /* Open the temp table for FOR EACH ROW triggers
398 */
danielk1977f29ce552002-05-19 23:43:12 +0000399 if( row_triggers_exist ){
danielk19774adee202004-05-08 08:23:19 +0000400 sqlite3VdbeAddOp(v, OP_OpenPseudo, newIdx, 0);
danielk197784ac9d02004-05-18 09:58:06 +0000401 sqlite3VdbeAddOp(v, OP_SetNumColumns, newIdx, pTab->nCol);
danielk1977f29ce552002-05-19 23:43:12 +0000402 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000403
drhfeeb1392002-04-09 03:28:01 +0000404 /* Initialize the count of rows to be inserted
405 */
drh142e30d2002-08-28 03:00:58 +0000406 if( db->flags & SQLITE_CountRows ){
407 iCntMem = pParse->nMem++;
danielk19774adee202004-05-08 08:23:19 +0000408 sqlite3VdbeAddOp(v, OP_Integer, 0, 0);
409 sqlite3VdbeAddOp(v, OP_MemStore, iCntMem, 1);
drhfeeb1392002-04-09 03:28:01 +0000410 }
411
danielk1977c3f9bad2002-05-15 08:30:12 +0000412 /* Open tables and indices if there are no row triggers */
danielk1977f29ce552002-05-19 23:43:12 +0000413 if( !row_triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000414 base = pParse->nTab;
danielk19774adee202004-05-08 08:23:19 +0000415 idx = sqlite3OpenTableAndIndices(pParse, pTab, base);
danielk1977c3f9bad2002-05-15 08:30:12 +0000416 pParse->nTab += idx;
417 }
418
drh142e30d2002-08-28 03:00:58 +0000419 /* If the data source is a temporary table, then we have to create
drh1ccde152000-06-17 13:12:39 +0000420 ** a loop because there might be multiple rows of data. If the data
drh142e30d2002-08-28 03:00:58 +0000421 ** source is a subroutine call from the SELECT statement, then we need
422 ** to launch the SELECT statement processing.
drh1ccde152000-06-17 13:12:39 +0000423 */
drh142e30d2002-08-28 03:00:58 +0000424 if( useTempTable ){
danielk19774adee202004-05-08 08:23:19 +0000425 iBreak = sqlite3VdbeMakeLabel(v);
426 sqlite3VdbeAddOp(v, OP_Rewind, srcTab, iBreak);
427 iCont = sqlite3VdbeCurrentAddr(v);
drh142e30d2002-08-28 03:00:58 +0000428 }else if( pSelect ){
danielk19774adee202004-05-08 08:23:19 +0000429 sqlite3VdbeAddOp(v, OP_Goto, 0, iSelectLoop);
430 sqlite3VdbeResolveLabel(v, iInsertBlock);
drh5974a302000-06-07 14:42:26 +0000431 }
drh1ccde152000-06-17 13:12:39 +0000432
drh5cf590c2003-04-24 01:45:04 +0000433 /* Run the BEFORE and INSTEAD OF triggers, if there are any
drh70ce3f02003-04-15 19:22:22 +0000434 */
danielk19774adee202004-05-08 08:23:19 +0000435 endOfLoop = sqlite3VdbeMakeLabel(v);
drh70ce3f02003-04-15 19:22:22 +0000436 if( before_triggers ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000437
drh70ce3f02003-04-15 19:22:22 +0000438 /* build the NEW.* reference row. Note that if there is an INTEGER
439 ** PRIMARY KEY into which a NULL is being inserted, that NULL will be
440 ** translated into a unique ID for the row. But on a BEFORE trigger,
441 ** we do not know what the unique ID will be (because the insert has
442 ** not happened yet) so we substitute a rowid of -1
443 */
444 if( keyColumn<0 ){
danielk19774adee202004-05-08 08:23:19 +0000445 sqlite3VdbeAddOp(v, OP_Integer, -1, 0);
drh70ce3f02003-04-15 19:22:22 +0000446 }else if( useTempTable ){
danielk19774adee202004-05-08 08:23:19 +0000447 sqlite3VdbeAddOp(v, OP_Column, srcTab, keyColumn);
drh70ce3f02003-04-15 19:22:22 +0000448 }else if( pSelect ){
danielk19774adee202004-05-08 08:23:19 +0000449 sqlite3VdbeAddOp(v, OP_Dup, nColumn - keyColumn - 1, 1);
drh70ce3f02003-04-15 19:22:22 +0000450 }else{
danielk19774adee202004-05-08 08:23:19 +0000451 sqlite3ExprCode(pParse, pList->a[keyColumn].pExpr);
452 sqlite3VdbeAddOp(v, OP_NotNull, -1, sqlite3VdbeCurrentAddr(v)+3);
453 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
454 sqlite3VdbeAddOp(v, OP_Integer, -1, 0);
455 sqlite3VdbeAddOp(v, OP_MustBeInt, 0, 0);
drh70ce3f02003-04-15 19:22:22 +0000456 }
457
458 /* Create the new column data
459 */
danielk1977c3f9bad2002-05-15 08:30:12 +0000460 for(i=0; i<pTab->nCol; i++){
461 if( pColumn==0 ){
drh9adf9ac2002-05-15 11:44:13 +0000462 j = i;
danielk1977c3f9bad2002-05-15 08:30:12 +0000463 }else{
drh9adf9ac2002-05-15 11:44:13 +0000464 for(j=0; j<pColumn->nId; j++){
465 if( pColumn->a[j].idx==i ) break;
466 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000467 }
468 if( pColumn && j>=pColumn->nId ){
danielk19774adee202004-05-08 08:23:19 +0000469 sqlite3VdbeOp3(v, OP_String, 0, 0, pTab->aCol[i].zDflt, P3_STATIC);
drh142e30d2002-08-28 03:00:58 +0000470 }else if( useTempTable ){
danielk19774adee202004-05-08 08:23:19 +0000471 sqlite3VdbeAddOp(v, OP_Column, srcTab, j);
drh142e30d2002-08-28 03:00:58 +0000472 }else if( pSelect ){
danielk19774adee202004-05-08 08:23:19 +0000473 sqlite3VdbeAddOp(v, OP_Dup, nColumn-j-1, 1);
danielk1977c3f9bad2002-05-15 08:30:12 +0000474 }else{
danielk19774adee202004-05-08 08:23:19 +0000475 sqlite3ExprCode(pParse, pList->a[j].pExpr);
danielk1977c3f9bad2002-05-15 08:30:12 +0000476 }
477 }
danielk19774adee202004-05-08 08:23:19 +0000478 sqlite3VdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0);
danielk1977a37cdde2004-05-16 11:15:36 +0000479
480 /* If this is an INSERT on a view with an INSTEAD OF INSERT trigger,
481 ** do not attempt any conversions before assembling the record.
482 ** If this is a real table, attempt conversions as required by the
483 ** table column affinities.
484 */
485 if( !isView ){
486 sqlite3TableAffinityStr(v, pTab);
487 }
danielk19774adee202004-05-08 08:23:19 +0000488 sqlite3VdbeAddOp(v, OP_PutIntKey, newIdx, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000489
drh5cf590c2003-04-24 01:45:04 +0000490 /* Fire BEFORE or INSTEAD OF triggers */
danielk19774adee202004-05-08 08:23:19 +0000491 if( sqlite3CodeRowTrigger(pParse, TK_INSERT, 0, TK_BEFORE, pTab,
drhb2fe7d82003-04-20 17:29:23 +0000492 newIdx, -1, onError, endOfLoop) ){
danielk1977f29ce552002-05-19 23:43:12 +0000493 goto insert_cleanup;
494 }
drh70ce3f02003-04-15 19:22:22 +0000495 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000496
drh70ce3f02003-04-15 19:22:22 +0000497 /* If any triggers exists, the opening of tables and indices is deferred
498 ** until now.
499 */
drh5cf590c2003-04-24 01:45:04 +0000500 if( row_triggers_exist && !isView ){
501 base = pParse->nTab;
danielk19774adee202004-05-08 08:23:19 +0000502 idx = sqlite3OpenTableAndIndices(pParse, pTab, base);
drh5cf590c2003-04-24 01:45:04 +0000503 pParse->nTab += idx;
danielk1977c3f9bad2002-05-15 08:30:12 +0000504 }
505
drh4a324312001-12-21 14:30:42 +0000506 /* Push the record number for the new entry onto the stack. The
507 ** record number is a randomly generate integer created by NewRecno
508 ** except when the table has an INTEGER PRIMARY KEY column, in which
drhb419a922002-01-30 16:17:23 +0000509 ** case the record number is the same as that column.
drh1ccde152000-06-17 13:12:39 +0000510 */
drh5cf590c2003-04-24 01:45:04 +0000511 if( !isView ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000512 if( keyColumn>=0 ){
drh142e30d2002-08-28 03:00:58 +0000513 if( useTempTable ){
danielk19774adee202004-05-08 08:23:19 +0000514 sqlite3VdbeAddOp(v, OP_Column, srcTab, keyColumn);
drh142e30d2002-08-28 03:00:58 +0000515 }else if( pSelect ){
danielk19774adee202004-05-08 08:23:19 +0000516 sqlite3VdbeAddOp(v, OP_Dup, nColumn - keyColumn - 1, 1);
danielk1977c3f9bad2002-05-15 08:30:12 +0000517 }else{
danielk19774adee202004-05-08 08:23:19 +0000518 sqlite3ExprCode(pParse, pList->a[keyColumn].pExpr);
danielk1977c3f9bad2002-05-15 08:30:12 +0000519 }
drh27a32782002-06-19 20:32:43 +0000520 /* If the PRIMARY KEY expression is NULL, then use OP_NewRecno
521 ** to generate a unique primary key value.
522 */
danielk19774adee202004-05-08 08:23:19 +0000523 sqlite3VdbeAddOp(v, OP_NotNull, -1, sqlite3VdbeCurrentAddr(v)+3);
524 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
525 sqlite3VdbeAddOp(v, OP_NewRecno, base, 0);
526 sqlite3VdbeAddOp(v, OP_MustBeInt, 0, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000527 }else{
danielk19774adee202004-05-08 08:23:19 +0000528 sqlite3VdbeAddOp(v, OP_NewRecno, base, 0);
drh4a324312001-12-21 14:30:42 +0000529 }
drh4a324312001-12-21 14:30:42 +0000530
danielk1977c3f9bad2002-05-15 08:30:12 +0000531 /* Push onto the stack, data for all columns of the new entry, beginning
danielk1977f29ce552002-05-19 23:43:12 +0000532 ** with the first column.
533 */
danielk1977c3f9bad2002-05-15 08:30:12 +0000534 for(i=0; i<pTab->nCol; i++){
535 if( i==pTab->iPKey ){
drh9adf9ac2002-05-15 11:44:13 +0000536 /* The value of the INTEGER PRIMARY KEY column is always a NULL.
danielk1977f29ce552002-05-19 23:43:12 +0000537 ** Whenever this column is read, the record number will be substituted
538 ** in its place. So will fill this column with a NULL to avoid
539 ** taking up data space with information that will never be used. */
danielk19774adee202004-05-08 08:23:19 +0000540 sqlite3VdbeAddOp(v, OP_String, 0, 0);
drh9adf9ac2002-05-15 11:44:13 +0000541 continue;
danielk1977c3f9bad2002-05-15 08:30:12 +0000542 }
543 if( pColumn==0 ){
drh9adf9ac2002-05-15 11:44:13 +0000544 j = i;
danielk1977c3f9bad2002-05-15 08:30:12 +0000545 }else{
drh9adf9ac2002-05-15 11:44:13 +0000546 for(j=0; j<pColumn->nId; j++){
547 if( pColumn->a[j].idx==i ) break;
548 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000549 }
550 if( pColumn && j>=pColumn->nId ){
danielk19774adee202004-05-08 08:23:19 +0000551 sqlite3VdbeOp3(v, OP_String, 0, 0, pTab->aCol[i].zDflt, P3_STATIC);
drh142e30d2002-08-28 03:00:58 +0000552 }else if( useTempTable ){
danielk19774adee202004-05-08 08:23:19 +0000553 sqlite3VdbeAddOp(v, OP_Column, srcTab, j);
drh142e30d2002-08-28 03:00:58 +0000554 }else if( pSelect ){
danielk19774adee202004-05-08 08:23:19 +0000555 sqlite3VdbeAddOp(v, OP_Dup, i+nColumn-j, 1);
danielk1977c3f9bad2002-05-15 08:30:12 +0000556 }else{
danielk19774adee202004-05-08 08:23:19 +0000557 sqlite3ExprCode(pParse, pList->a[j].pExpr);
drh5974a302000-06-07 14:42:26 +0000558 }
drhbed86902000-06-02 13:27:59 +0000559 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000560
561 /* Generate code to check constraints and generate index keys and
danielk1977f29ce552002-05-19 23:43:12 +0000562 ** do the insertion.
563 */
danielk19774adee202004-05-08 08:23:19 +0000564 sqlite3GenerateConstraintChecks(pParse, pTab, base, 0, keyColumn>=0,
drha0217ba2003-06-01 01:10:33 +0000565 0, onError, endOfLoop);
danielk19774adee202004-05-08 08:23:19 +0000566 sqlite3CompleteInsertion(pParse, pTab, base, 0,0,0,
drh70ce3f02003-04-15 19:22:22 +0000567 after_triggers ? newIdx : -1);
drh5cf590c2003-04-24 01:45:04 +0000568 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000569
drh5cf590c2003-04-24 01:45:04 +0000570 /* Update the count of rows that are inserted
571 */
572 if( (db->flags & SQLITE_CountRows)!=0 ){
danielk19774adee202004-05-08 08:23:19 +0000573 sqlite3VdbeAddOp(v, OP_MemIncr, iCntMem, 0);
drh5974a302000-06-07 14:42:26 +0000574 }
drh1ccde152000-06-17 13:12:39 +0000575
danielk1977f29ce552002-05-19 23:43:12 +0000576 if( row_triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000577 /* Close all tables opened */
drh5cf590c2003-04-24 01:45:04 +0000578 if( !isView ){
danielk19774adee202004-05-08 08:23:19 +0000579 sqlite3VdbeAddOp(v, OP_Close, base, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000580 for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
danielk19774adee202004-05-08 08:23:19 +0000581 sqlite3VdbeAddOp(v, OP_Close, idx+base, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000582 }
583 }
drh1bee3d72001-10-15 00:44:35 +0000584
danielk1977c3f9bad2002-05-15 08:30:12 +0000585 /* Code AFTER triggers */
danielk19774adee202004-05-08 08:23:19 +0000586 if( sqlite3CodeRowTrigger(pParse, TK_INSERT, 0, TK_AFTER, pTab, newIdx, -1,
danielk19776f349032002-06-11 02:25:40 +0000587 onError, endOfLoop) ){
danielk1977f29ce552002-05-19 23:43:12 +0000588 goto insert_cleanup;
589 }
drh1bee3d72001-10-15 00:44:35 +0000590 }
591
drh1ccde152000-06-17 13:12:39 +0000592 /* The bottom of the loop, if the data source is a SELECT statement
danielk1977f29ce552002-05-19 23:43:12 +0000593 */
danielk19774adee202004-05-08 08:23:19 +0000594 sqlite3VdbeResolveLabel(v, endOfLoop);
drh142e30d2002-08-28 03:00:58 +0000595 if( useTempTable ){
danielk19774adee202004-05-08 08:23:19 +0000596 sqlite3VdbeAddOp(v, OP_Next, srcTab, iCont);
597 sqlite3VdbeResolveLabel(v, iBreak);
598 sqlite3VdbeAddOp(v, OP_Close, srcTab, 0);
drh142e30d2002-08-28 03:00:58 +0000599 }else if( pSelect ){
danielk19774adee202004-05-08 08:23:19 +0000600 sqlite3VdbeAddOp(v, OP_Pop, nColumn, 0);
601 sqlite3VdbeAddOp(v, OP_Return, 0, 0);
602 sqlite3VdbeResolveLabel(v, iCleanup);
drh6b563442001-11-07 16:48:26 +0000603 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000604
danielk1977f29ce552002-05-19 23:43:12 +0000605 if( !row_triggers_exist ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000606 /* Close all tables opened */
danielk19774adee202004-05-08 08:23:19 +0000607 sqlite3VdbeAddOp(v, OP_Close, base, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000608 for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
danielk19774adee202004-05-08 08:23:19 +0000609 sqlite3VdbeAddOp(v, OP_Close, idx+base, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000610 }
drhcce7d172000-05-31 15:34:51 +0000611 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000612
danielk19774adee202004-05-08 08:23:19 +0000613 sqlite3VdbeAddOp(v, OP_SetCounts, 0, 0);
614 sqlite3EndWriteOperation(pParse);
drh5e00f6c2001-09-13 13:46:56 +0000615
drh1bee3d72001-10-15 00:44:35 +0000616 /*
danielk1977f29ce552002-05-19 23:43:12 +0000617 ** Return the number of rows inserted.
drh1bee3d72001-10-15 00:44:35 +0000618 */
drh142e30d2002-08-28 03:00:58 +0000619 if( db->flags & SQLITE_CountRows ){
danielk19774adee202004-05-08 08:23:19 +0000620 sqlite3VdbeOp3(v, OP_ColumnName, 0, 1, "rows inserted", P3_STATIC);
621 sqlite3VdbeAddOp(v, OP_MemLoad, iCntMem, 0);
622 sqlite3VdbeAddOp(v, OP_Callback, 1, 0);
drh1bee3d72001-10-15 00:44:35 +0000623 }
drhcce7d172000-05-31 15:34:51 +0000624
625insert_cleanup:
danielk19774adee202004-05-08 08:23:19 +0000626 sqlite3SrcListDelete(pTabList);
627 if( pList ) sqlite3ExprListDelete(pList);
628 if( pSelect ) sqlite3SelectDelete(pSelect);
629 sqlite3IdListDelete(pColumn);
drhcce7d172000-05-31 15:34:51 +0000630}
drh9cfcf5d2002-01-29 18:41:24 +0000631
drh9cfcf5d2002-01-29 18:41:24 +0000632/*
633** Generate code to do a constraint check prior to an INSERT or an UPDATE.
634**
635** When this routine is called, the stack contains (from bottom to top)
drh0ca3e242002-01-29 23:07:02 +0000636** the following values:
637**
drhb2fe7d82003-04-20 17:29:23 +0000638** 1. The recno of the row to be updated before the update. This
drhb419a922002-01-30 16:17:23 +0000639** value is omitted unless we are doing an UPDATE that involves a
640** change to the record number.
drh0ca3e242002-01-29 23:07:02 +0000641**
drhb419a922002-01-30 16:17:23 +0000642** 2. The recno of the row after the update.
drh0ca3e242002-01-29 23:07:02 +0000643**
644** 3. The data in the first column of the entry after the update.
645**
646** i. Data from middle columns...
647**
648** N. The data in the last column of the entry after the update.
649**
drhb419a922002-01-30 16:17:23 +0000650** The old recno shown as entry (1) above is omitted unless both isUpdate
drh1c928532002-01-31 15:54:21 +0000651** and recnoChng are 1. isUpdate is true for UPDATEs and false for
652** INSERTs and recnoChng is true if the record number is being changed.
drh0ca3e242002-01-29 23:07:02 +0000653**
654** The code generated by this routine pushes additional entries onto
655** the stack which are the keys for new index entries for the new record.
656** The order of index keys is the same as the order of the indices on
657** the pTable->pIndex list. A key is only created for index i if
658** aIdxUsed!=0 and aIdxUsed[i]!=0.
drh9cfcf5d2002-01-29 18:41:24 +0000659**
660** This routine also generates code to check constraints. NOT NULL,
661** CHECK, and UNIQUE constraints are all checked. If a constraint fails,
drh1c928532002-01-31 15:54:21 +0000662** then the appropriate action is performed. There are five possible
663** actions: ROLLBACK, ABORT, FAIL, REPLACE, and IGNORE.
drh9cfcf5d2002-01-29 18:41:24 +0000664**
665** Constraint type Action What Happens
666** --------------- ---------- ----------------------------------------
drh1c928532002-01-31 15:54:21 +0000667** any ROLLBACK The current transaction is rolled back and
danielk197724b03fd2004-05-10 10:34:34 +0000668** sqlite3_exec() returns immediately with a
drh9cfcf5d2002-01-29 18:41:24 +0000669** return code of SQLITE_CONSTRAINT.
670**
drh1c928532002-01-31 15:54:21 +0000671** any ABORT Back out changes from the current command
672** only (do not do a complete rollback) then
danielk197724b03fd2004-05-10 10:34:34 +0000673** cause sqlite3_exec() to return immediately
drh1c928532002-01-31 15:54:21 +0000674** with SQLITE_CONSTRAINT.
675**
676** any FAIL Sqlite_exec() returns immediately with a
677** return code of SQLITE_CONSTRAINT. The
678** transaction is not rolled back and any
679** prior changes are retained.
680**
drh9cfcf5d2002-01-29 18:41:24 +0000681** any IGNORE The record number and data is popped from
682** the stack and there is an immediate jump
683** to label ignoreDest.
684**
685** NOT NULL REPLACE The NULL value is replace by the default
686** value for that column. If the default value
687** is NULL, the action is the same as ABORT.
688**
689** UNIQUE REPLACE The other row that conflicts with the row
690** being inserted is removed.
691**
692** CHECK REPLACE Illegal. The results in an exception.
693**
drh1c928532002-01-31 15:54:21 +0000694** Which action to take is determined by the overrideError parameter.
695** Or if overrideError==OE_Default, then the pParse->onError parameter
696** is used. Or if pParse->onError==OE_Default then the onError value
697** for the constraint is used.
drh9cfcf5d2002-01-29 18:41:24 +0000698**
drhaaab5722002-02-19 13:39:21 +0000699** The calling routine must open a read/write cursor for pTab with
drh9cfcf5d2002-01-29 18:41:24 +0000700** cursor number "base". All indices of pTab must also have open
701** read/write cursors with cursor number base+i for the i-th cursor.
702** Except, if there is no possibility of a REPLACE action then
703** cursors do not need to be open for indices where aIdxUsed[i]==0.
704**
705** If the isUpdate flag is true, it means that the "base" cursor is
706** initially pointing to an entry that is being updated. The isUpdate
707** flag causes extra code to be generated so that the "base" cursor
708** is still pointing at the same entry after the routine returns.
709** Without the isUpdate flag, the "base" cursor might be moved.
710*/
danielk19774adee202004-05-08 08:23:19 +0000711void sqlite3GenerateConstraintChecks(
drh9cfcf5d2002-01-29 18:41:24 +0000712 Parse *pParse, /* The parser context */
713 Table *pTab, /* the table into which we are inserting */
714 int base, /* Index of a read/write cursor pointing at pTab */
715 char *aIdxUsed, /* Which indices are used. NULL means all are used */
drh0ca3e242002-01-29 23:07:02 +0000716 int recnoChng, /* True if the record number will change */
drhb419a922002-01-30 16:17:23 +0000717 int isUpdate, /* True for UPDATE, False for INSERT */
drh9cfcf5d2002-01-29 18:41:24 +0000718 int overrideError, /* Override onError to this if not OE_Default */
drhb419a922002-01-30 16:17:23 +0000719 int ignoreDest /* Jump to this label on an OE_Ignore resolution */
drh9cfcf5d2002-01-29 18:41:24 +0000720){
721 int i;
722 Vdbe *v;
723 int nCol;
724 int onError;
725 int addr;
726 int extra;
drh0ca3e242002-01-29 23:07:02 +0000727 int iCur;
728 Index *pIdx;
729 int seenReplace = 0;
drhf5905aa2002-05-26 20:54:33 +0000730 int jumpInst1, jumpInst2;
drh0ca3e242002-01-29 23:07:02 +0000731 int contAddr;
drhb419a922002-01-30 16:17:23 +0000732 int hasTwoRecnos = (isUpdate && recnoChng);
drh9cfcf5d2002-01-29 18:41:24 +0000733
danielk19774adee202004-05-08 08:23:19 +0000734 v = sqlite3GetVdbe(pParse);
drh9cfcf5d2002-01-29 18:41:24 +0000735 assert( v!=0 );
drh417be792002-03-03 18:59:40 +0000736 assert( pTab->pSelect==0 ); /* This table is not a VIEW */
drh9cfcf5d2002-01-29 18:41:24 +0000737 nCol = pTab->nCol;
738
739 /* Test all NOT NULL constraints.
740 */
741 for(i=0; i<nCol; i++){
drh0ca3e242002-01-29 23:07:02 +0000742 if( i==pTab->iPKey ){
drh0ca3e242002-01-29 23:07:02 +0000743 continue;
744 }
drh9cfcf5d2002-01-29 18:41:24 +0000745 onError = pTab->aCol[i].notNull;
drh0ca3e242002-01-29 23:07:02 +0000746 if( onError==OE_None ) continue;
drh9cfcf5d2002-01-29 18:41:24 +0000747 if( overrideError!=OE_Default ){
748 onError = overrideError;
drha996e472003-05-16 02:30:27 +0000749 }else if( pParse->db->onError!=OE_Default ){
drh0d65dc02002-02-03 00:56:09 +0000750 onError = pParse->db->onError;
drha996e472003-05-16 02:30:27 +0000751 }else if( onError==OE_Default ){
752 onError = OE_Abort;
drh9cfcf5d2002-01-29 18:41:24 +0000753 }
754 if( onError==OE_Replace && pTab->aCol[i].zDflt==0 ){
755 onError = OE_Abort;
756 }
danielk19774adee202004-05-08 08:23:19 +0000757 sqlite3VdbeAddOp(v, OP_Dup, nCol-1-i, 1);
758 addr = sqlite3VdbeAddOp(v, OP_NotNull, 1, 0);
drh9cfcf5d2002-01-29 18:41:24 +0000759 switch( onError ){
drh1c928532002-01-31 15:54:21 +0000760 case OE_Rollback:
761 case OE_Abort:
762 case OE_Fail: {
drh483750b2003-01-29 18:46:51 +0000763 char *zMsg = 0;
danielk19774adee202004-05-08 08:23:19 +0000764 sqlite3VdbeAddOp(v, OP_Halt, SQLITE_CONSTRAINT, onError);
765 sqlite3SetString(&zMsg, pTab->zName, ".", pTab->aCol[i].zName,
drh41743982003-12-06 21:43:55 +0000766 " may not be NULL", (char*)0);
danielk19774adee202004-05-08 08:23:19 +0000767 sqlite3VdbeChangeP3(v, -1, zMsg, P3_DYNAMIC);
drh9cfcf5d2002-01-29 18:41:24 +0000768 break;
769 }
770 case OE_Ignore: {
danielk19774adee202004-05-08 08:23:19 +0000771 sqlite3VdbeAddOp(v, OP_Pop, nCol+1+hasTwoRecnos, 0);
772 sqlite3VdbeAddOp(v, OP_Goto, 0, ignoreDest);
drh9cfcf5d2002-01-29 18:41:24 +0000773 break;
774 }
775 case OE_Replace: {
danielk19774adee202004-05-08 08:23:19 +0000776 sqlite3VdbeOp3(v, OP_String, 0, 0, pTab->aCol[i].zDflt, P3_STATIC);
777 sqlite3VdbeAddOp(v, OP_Push, nCol-i, 0);
drh9cfcf5d2002-01-29 18:41:24 +0000778 break;
779 }
drh0ca3e242002-01-29 23:07:02 +0000780 default: assert(0);
drh9cfcf5d2002-01-29 18:41:24 +0000781 }
danielk19774adee202004-05-08 08:23:19 +0000782 sqlite3VdbeChangeP2(v, addr, sqlite3VdbeCurrentAddr(v));
drh9cfcf5d2002-01-29 18:41:24 +0000783 }
784
785 /* Test all CHECK constraints
786 */
drh0bd1f4e2002-06-06 18:54:39 +0000787 /**** TBD ****/
drh9cfcf5d2002-01-29 18:41:24 +0000788
drh0bd1f4e2002-06-06 18:54:39 +0000789 /* If we have an INTEGER PRIMARY KEY, make sure the primary key
790 ** of the new record does not previously exist. Except, if this
791 ** is an UPDATE and the primary key is not changing, that is OK.
drh9cfcf5d2002-01-29 18:41:24 +0000792 */
drh5383ae52003-06-04 12:23:30 +0000793 if( recnoChng ){
drh0ca3e242002-01-29 23:07:02 +0000794 onError = pTab->keyConf;
795 if( overrideError!=OE_Default ){
796 onError = overrideError;
drha996e472003-05-16 02:30:27 +0000797 }else if( pParse->db->onError!=OE_Default ){
drh0d65dc02002-02-03 00:56:09 +0000798 onError = pParse->db->onError;
drha996e472003-05-16 02:30:27 +0000799 }else if( onError==OE_Default ){
800 onError = OE_Abort;
drh0ca3e242002-01-29 23:07:02 +0000801 }
drha0217ba2003-06-01 01:10:33 +0000802
drh5383ae52003-06-04 12:23:30 +0000803 if( isUpdate ){
danielk19774adee202004-05-08 08:23:19 +0000804 sqlite3VdbeAddOp(v, OP_Dup, nCol+1, 1);
805 sqlite3VdbeAddOp(v, OP_Dup, nCol+1, 1);
806 jumpInst1 = sqlite3VdbeAddOp(v, OP_Eq, 0, 0);
drh5383ae52003-06-04 12:23:30 +0000807 }
danielk19774adee202004-05-08 08:23:19 +0000808 sqlite3VdbeAddOp(v, OP_Dup, nCol, 1);
809 jumpInst2 = sqlite3VdbeAddOp(v, OP_NotExists, base, 0);
drh5383ae52003-06-04 12:23:30 +0000810 switch( onError ){
811 default: {
812 onError = OE_Abort;
813 /* Fall thru into the next case */
drh79b0c952002-05-21 12:56:43 +0000814 }
drh5383ae52003-06-04 12:23:30 +0000815 case OE_Rollback:
816 case OE_Abort:
817 case OE_Fail: {
danielk19774adee202004-05-08 08:23:19 +0000818 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, onError,
drh701a0ae2004-02-22 20:05:00 +0000819 "PRIMARY KEY must be unique", P3_STATIC);
drh5383ae52003-06-04 12:23:30 +0000820 break;
drh0ca3e242002-01-29 23:07:02 +0000821 }
drh5383ae52003-06-04 12:23:30 +0000822 case OE_Replace: {
danielk19774adee202004-05-08 08:23:19 +0000823 sqlite3GenerateRowIndexDelete(pParse->db, v, pTab, base, 0);
drh5383ae52003-06-04 12:23:30 +0000824 if( isUpdate ){
danielk19774adee202004-05-08 08:23:19 +0000825 sqlite3VdbeAddOp(v, OP_Dup, nCol+hasTwoRecnos, 1);
drh7cf6e4d2004-05-19 14:56:55 +0000826 sqlite3VdbeAddOp(v, OP_MoveGe, base, 0);
drh5383ae52003-06-04 12:23:30 +0000827 }
828 seenReplace = 1;
829 break;
drh0ca3e242002-01-29 23:07:02 +0000830 }
drh5383ae52003-06-04 12:23:30 +0000831 case OE_Ignore: {
832 assert( seenReplace==0 );
danielk19774adee202004-05-08 08:23:19 +0000833 sqlite3VdbeAddOp(v, OP_Pop, nCol+1+hasTwoRecnos, 0);
834 sqlite3VdbeAddOp(v, OP_Goto, 0, ignoreDest);
drh5383ae52003-06-04 12:23:30 +0000835 break;
836 }
837 }
danielk19774adee202004-05-08 08:23:19 +0000838 contAddr = sqlite3VdbeCurrentAddr(v);
839 sqlite3VdbeChangeP2(v, jumpInst2, contAddr);
drh5383ae52003-06-04 12:23:30 +0000840 if( isUpdate ){
danielk19774adee202004-05-08 08:23:19 +0000841 sqlite3VdbeChangeP2(v, jumpInst1, contAddr);
842 sqlite3VdbeAddOp(v, OP_Dup, nCol+1, 1);
drh7cf6e4d2004-05-19 14:56:55 +0000843 sqlite3VdbeAddOp(v, OP_MoveGe, base, 0);
drh0ca3e242002-01-29 23:07:02 +0000844 }
845 }
drh0bd1f4e2002-06-06 18:54:39 +0000846
847 /* Test all UNIQUE constraints by creating entries for each UNIQUE
848 ** index and making sure that duplicate entries do not already exist.
849 ** Add the new records to the indices as we go.
850 */
drhb2fe7d82003-04-20 17:29:23 +0000851 extra = -1;
852 for(iCur=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, iCur++){
853 if( aIdxUsed && aIdxUsed[iCur]==0 ) continue; /* Skip unused indices */
854 extra++;
855
856 /* Create a key for accessing the index entry */
danielk19774adee202004-05-08 08:23:19 +0000857 sqlite3VdbeAddOp(v, OP_Dup, nCol+extra, 1);
drh9cfcf5d2002-01-29 18:41:24 +0000858 for(i=0; i<pIdx->nColumn; i++){
859 int idx = pIdx->aiColumn[i];
860 if( idx==pTab->iPKey ){
danielk19774adee202004-05-08 08:23:19 +0000861 sqlite3VdbeAddOp(v, OP_Dup, i+extra+nCol+1, 1);
drh9cfcf5d2002-01-29 18:41:24 +0000862 }else{
danielk19774adee202004-05-08 08:23:19 +0000863 sqlite3VdbeAddOp(v, OP_Dup, i+extra+nCol-idx, 1);
drh9cfcf5d2002-01-29 18:41:24 +0000864 }
865 }
danielk19774adee202004-05-08 08:23:19 +0000866 jumpInst1 = sqlite3VdbeAddOp(v, OP_MakeIdxKey, pIdx->nColumn, 0);
danielk1977a37cdde2004-05-16 11:15:36 +0000867 sqlite3IndexAffinityStr(v, pIdx);
drhb2fe7d82003-04-20 17:29:23 +0000868
869 /* Find out what action to take in case there is an indexing conflict */
drh9cfcf5d2002-01-29 18:41:24 +0000870 onError = pIdx->onError;
drhb2fe7d82003-04-20 17:29:23 +0000871 if( onError==OE_None ) continue; /* pIdx is not a UNIQUE index */
drh9cfcf5d2002-01-29 18:41:24 +0000872 if( overrideError!=OE_Default ){
873 onError = overrideError;
drha996e472003-05-16 02:30:27 +0000874 }else if( pParse->db->onError!=OE_Default ){
drh0d65dc02002-02-03 00:56:09 +0000875 onError = pParse->db->onError;
drha996e472003-05-16 02:30:27 +0000876 }else if( onError==OE_Default ){
877 onError = OE_Abort;
drh9cfcf5d2002-01-29 18:41:24 +0000878 }
drh5383ae52003-06-04 12:23:30 +0000879 if( seenReplace ){
880 if( onError==OE_Ignore ) onError = OE_Replace;
881 else if( onError==OE_Fail ) onError = OE_Abort;
882 }
883
drhb2fe7d82003-04-20 17:29:23 +0000884
885 /* Check to see if the new index entry will be unique */
danielk19774adee202004-05-08 08:23:19 +0000886 sqlite3VdbeAddOp(v, OP_Dup, extra+nCol+1+hasTwoRecnos, 1);
887 jumpInst2 = sqlite3VdbeAddOp(v, OP_IsUnique, base+iCur+1, 0);
drhb2fe7d82003-04-20 17:29:23 +0000888
889 /* Generate code that executes if the new index entry is not unique */
drh9cfcf5d2002-01-29 18:41:24 +0000890 switch( onError ){
drh1c928532002-01-31 15:54:21 +0000891 case OE_Rollback:
892 case OE_Abort:
893 case OE_Fail: {
drh37ed48e2003-08-05 13:13:38 +0000894 int j, n1, n2;
895 char zErrMsg[200];
896 strcpy(zErrMsg, pIdx->nColumn>1 ? "columns " : "column ");
897 n1 = strlen(zErrMsg);
898 for(j=0; j<pIdx->nColumn && n1<sizeof(zErrMsg)-30; j++){
899 char *zCol = pTab->aCol[pIdx->aiColumn[j]].zName;
900 n2 = strlen(zCol);
901 if( j>0 ){
902 strcpy(&zErrMsg[n1], ", ");
903 n1 += 2;
904 }
905 if( n1+n2>sizeof(zErrMsg)-30 ){
906 strcpy(&zErrMsg[n1], "...");
907 n1 += 3;
908 break;
909 }else{
910 strcpy(&zErrMsg[n1], zCol);
911 n1 += n2;
912 }
913 }
914 strcpy(&zErrMsg[n1],
915 pIdx->nColumn>1 ? " are not unique" : " is not unique");
danielk19774adee202004-05-08 08:23:19 +0000916 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, onError, zErrMsg, 0);
drh9cfcf5d2002-01-29 18:41:24 +0000917 break;
918 }
919 case OE_Ignore: {
drh0ca3e242002-01-29 23:07:02 +0000920 assert( seenReplace==0 );
danielk19774adee202004-05-08 08:23:19 +0000921 sqlite3VdbeAddOp(v, OP_Pop, nCol+extra+3+hasTwoRecnos, 0);
922 sqlite3VdbeAddOp(v, OP_Goto, 0, ignoreDest);
drh9cfcf5d2002-01-29 18:41:24 +0000923 break;
924 }
925 case OE_Replace: {
danielk19774adee202004-05-08 08:23:19 +0000926 sqlite3GenerateRowDelete(pParse->db, v, pTab, base, 0);
drh9cfcf5d2002-01-29 18:41:24 +0000927 if( isUpdate ){
danielk19774adee202004-05-08 08:23:19 +0000928 sqlite3VdbeAddOp(v, OP_Dup, nCol+extra+1+hasTwoRecnos, 1);
drh7cf6e4d2004-05-19 14:56:55 +0000929 sqlite3VdbeAddOp(v, OP_MoveGe, base, 0);
drh9cfcf5d2002-01-29 18:41:24 +0000930 }
drh0ca3e242002-01-29 23:07:02 +0000931 seenReplace = 1;
drh9cfcf5d2002-01-29 18:41:24 +0000932 break;
933 }
drh0ca3e242002-01-29 23:07:02 +0000934 default: assert(0);
drh9cfcf5d2002-01-29 18:41:24 +0000935 }
danielk19774adee202004-05-08 08:23:19 +0000936 contAddr = sqlite3VdbeCurrentAddr(v);
drh0bd1f4e2002-06-06 18:54:39 +0000937#if NULL_DISTINCT_FOR_UNIQUE
danielk19774adee202004-05-08 08:23:19 +0000938 sqlite3VdbeChangeP2(v, jumpInst1, contAddr);
drh0bd1f4e2002-06-06 18:54:39 +0000939#endif
danielk19774adee202004-05-08 08:23:19 +0000940 sqlite3VdbeChangeP2(v, jumpInst2, contAddr);
drh9cfcf5d2002-01-29 18:41:24 +0000941 }
942}
drh0ca3e242002-01-29 23:07:02 +0000943
944/*
945** This routine generates code to finish the INSERT or UPDATE operation
danielk19774adee202004-05-08 08:23:19 +0000946** that was started by a prior call to sqlite3GenerateConstraintChecks.
drh0ca3e242002-01-29 23:07:02 +0000947** The stack must contain keys for all active indices followed by data
948** and the recno for the new entry. This routine creates the new
949** entries in all indices and in the main table.
950**
drhb419a922002-01-30 16:17:23 +0000951** The arguments to this routine should be the same as the first six
danielk19774adee202004-05-08 08:23:19 +0000952** arguments to sqlite3GenerateConstraintChecks.
drh0ca3e242002-01-29 23:07:02 +0000953*/
danielk19774adee202004-05-08 08:23:19 +0000954void sqlite3CompleteInsertion(
drh0ca3e242002-01-29 23:07:02 +0000955 Parse *pParse, /* The parser context */
956 Table *pTab, /* the table into which we are inserting */
957 int base, /* Index of a read/write cursor pointing at pTab */
958 char *aIdxUsed, /* Which indices are used. NULL means all are used */
drhb419a922002-01-30 16:17:23 +0000959 int recnoChng, /* True if the record number will change */
drh70ce3f02003-04-15 19:22:22 +0000960 int isUpdate, /* True for UPDATE, False for INSERT */
961 int newIdx /* Index of NEW table for triggers. -1 if none */
drh0ca3e242002-01-29 23:07:02 +0000962){
963 int i;
964 Vdbe *v;
965 int nIdx;
966 Index *pIdx;
967
danielk19774adee202004-05-08 08:23:19 +0000968 v = sqlite3GetVdbe(pParse);
drh0ca3e242002-01-29 23:07:02 +0000969 assert( v!=0 );
drh417be792002-03-03 18:59:40 +0000970 assert( pTab->pSelect==0 ); /* This table is not a VIEW */
drh0ca3e242002-01-29 23:07:02 +0000971 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){}
972 for(i=nIdx-1; i>=0; i--){
973 if( aIdxUsed && aIdxUsed[i]==0 ) continue;
danielk19774adee202004-05-08 08:23:19 +0000974 sqlite3VdbeAddOp(v, OP_IdxPut, base+i+1, 0);
drh0ca3e242002-01-29 23:07:02 +0000975 }
danielk19774adee202004-05-08 08:23:19 +0000976 sqlite3VdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0);
danielk1977a37cdde2004-05-16 11:15:36 +0000977 sqlite3TableAffinityStr(v, pTab);
drh70ce3f02003-04-15 19:22:22 +0000978 if( newIdx>=0 ){
danielk19774adee202004-05-08 08:23:19 +0000979 sqlite3VdbeAddOp(v, OP_Dup, 1, 0);
980 sqlite3VdbeAddOp(v, OP_Dup, 1, 0);
981 sqlite3VdbeAddOp(v, OP_PutIntKey, newIdx, 0);
drh70ce3f02003-04-15 19:22:22 +0000982 }
danielk19774adee202004-05-08 08:23:19 +0000983 sqlite3VdbeAddOp(v, OP_PutIntKey, base,
rdcb0c374f2004-02-20 22:53:38 +0000984 (pParse->trigStack?0:OPFLAG_NCHANGE) |
985 (isUpdate?0:OPFLAG_LASTROWID) | OPFLAG_CSCHANGE);
drhb419a922002-01-30 16:17:23 +0000986 if( isUpdate && recnoChng ){
danielk19774adee202004-05-08 08:23:19 +0000987 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drh0ca3e242002-01-29 23:07:02 +0000988 }
989}
drhcd446902004-02-24 01:05:31 +0000990
991/*
992** Generate code that will open write cursors for a table and for all
993** indices of that table. The "base" parameter is the cursor number used
994** for the table. Indices are opened on subsequent cursors.
995**
996** Return the total number of cursors opened. This is always at least
997** 1 (for the main table) plus more for each cursor.
998*/
danielk19774adee202004-05-08 08:23:19 +0000999int sqlite3OpenTableAndIndices(Parse *pParse, Table *pTab, int base){
drhcd446902004-02-24 01:05:31 +00001000 int i;
1001 Index *pIdx;
danielk19774adee202004-05-08 08:23:19 +00001002 Vdbe *v = sqlite3GetVdbe(pParse);
drhcd446902004-02-24 01:05:31 +00001003 assert( v!=0 );
danielk19774adee202004-05-08 08:23:19 +00001004 sqlite3VdbeAddOp(v, OP_Integer, pTab->iDb, 0);
drhd3d39e92004-05-20 22:16:29 +00001005 sqlite3VdbeAddOp(v, OP_OpenWrite, base, pTab->tnum);
danielk1977b4964b72004-05-18 01:23:38 +00001006 sqlite3VdbeAddOp(v, OP_SetNumColumns, base, pTab->nCol);
drhcd446902004-02-24 01:05:31 +00001007 for(i=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
danielk19774adee202004-05-08 08:23:19 +00001008 sqlite3VdbeAddOp(v, OP_Integer, pIdx->iDb, 0);
drhd3d39e92004-05-20 22:16:29 +00001009 sqlite3VdbeOp3(v, OP_OpenWrite, i+base, pIdx->tnum,
1010 (char*)&pIdx->keyInfo, P3_KEYINFO);
drhcd446902004-02-24 01:05:31 +00001011 }
1012 return i;
1013}