blob: c24d019e535212685d403034fca7c27fcd7b8b62 [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**
drh6b125452002-01-28 15:53:03 +000015** $Id: insert.c,v 1.33 2002/01/28 15:53:05 drh Exp $
drhcce7d172000-05-31 15:34:51 +000016*/
17#include "sqliteInt.h"
18
19/*
drh1ccde152000-06-17 13:12:39 +000020** This routine is call to handle SQL of the following forms:
drhcce7d172000-05-31 15:34:51 +000021**
22** insert into TABLE (IDLIST) values(EXPRLIST)
drh1ccde152000-06-17 13:12:39 +000023** insert into TABLE (IDLIST) select
drhcce7d172000-05-31 15:34:51 +000024**
drh1ccde152000-06-17 13:12:39 +000025** The IDLIST following the table name is always optional. If omitted,
26** then a list of all columns for the table is substituted. The IDLIST
drh967e8b72000-06-21 13:59:10 +000027** appears in the pColumn parameter. pColumn is NULL if IDLIST is omitted.
drh1ccde152000-06-17 13:12:39 +000028**
29** The pList parameter holds EXPRLIST in the first form of the INSERT
30** statement above, and pSelect is NULL. For the second form, pList is
31** NULL and pSelect is a pointer to the select statement used to generate
32** data for the insert.
drhcce7d172000-05-31 15:34:51 +000033*/
34void sqliteInsert(
35 Parse *pParse, /* Parser context */
36 Token *pTableName, /* Name of table into which we are inserting */
37 ExprList *pList, /* List of values to be inserted */
drh5974a302000-06-07 14:42:26 +000038 Select *pSelect, /* A SELECT statement to use as the data source */
drh967e8b72000-06-21 13:59:10 +000039 IdList *pColumn /* Column names corresponding to IDLIST. */
drhcce7d172000-05-31 15:34:51 +000040){
drh5974a302000-06-07 14:42:26 +000041 Table *pTab; /* The table to insert into */
42 char *zTab; /* Name of the table into which we are inserting */
43 int i, j, idx; /* Loop counters */
44 Vdbe *v; /* Generate code into this virtual machine */
45 Index *pIdx; /* For looping over indices of the table */
46 int srcTab; /* Date comes from this temporary cursor if >=0 */
drh967e8b72000-06-21 13:59:10 +000047 int nColumn; /* Number of columns in the data */
drh5974a302000-06-07 14:42:26 +000048 int base; /* First available cursor */
49 int iCont, iBreak; /* Beginning and end of the loop over srcTab */
drhecdc7532001-09-23 02:35:53 +000050 sqlite *db; /* The main database structure */
drhf57b3392001-10-08 13:22:32 +000051 int openOp; /* Opcode used to open cursors */
drh4a324312001-12-21 14:30:42 +000052 int keyColumn = -1; /* Column that is the INTEGER PRIMARY KEY */
drhcce7d172000-05-31 15:34:51 +000053
drhdaffd0e2001-04-11 14:28:42 +000054 if( pParse->nErr || sqlite_malloc_failed ) goto insert_cleanup;
drhecdc7532001-09-23 02:35:53 +000055 db = pParse->db;
drhdaffd0e2001-04-11 14:28:42 +000056
drh1ccde152000-06-17 13:12:39 +000057 /* Locate the table into which we will be inserting new information.
58 */
drhcce7d172000-05-31 15:34:51 +000059 zTab = sqliteTableNameFromToken(pTableName);
drhdaffd0e2001-04-11 14:28:42 +000060 if( zTab==0 ) goto insert_cleanup;
drhecdc7532001-09-23 02:35:53 +000061 pTab = sqliteFindTable(db, zTab);
drhcce7d172000-05-31 15:34:51 +000062 sqliteFree(zTab);
63 if( pTab==0 ){
64 sqliteSetNString(&pParse->zErrMsg, "no such table: ", 0,
65 pTableName->z, pTableName->n, 0);
66 pParse->nErr++;
67 goto insert_cleanup;
68 }
69 if( pTab->readOnly ){
70 sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName,
71 " may not be modified", 0);
72 pParse->nErr++;
73 goto insert_cleanup;
74 }
drh1ccde152000-06-17 13:12:39 +000075
76 /* Allocate a VDBE
77 */
drhd8bc7082000-06-07 23:51:50 +000078 v = sqliteGetVdbe(pParse);
drh5974a302000-06-07 14:42:26 +000079 if( v==0 ) goto insert_cleanup;
drhecdc7532001-09-23 02:35:53 +000080 if( (db->flags & SQLITE_InTrans)==0 ){
drh99fcd712001-10-13 01:06:47 +000081 sqliteVdbeAddOp(v, OP_Transaction, 0, 0);
82 sqliteVdbeAddOp(v, OP_VerifyCookie, db->schema_cookie, 0);
drhecdc7532001-09-23 02:35:53 +000083 pParse->schemaVerified = 1;
drh5e00f6c2001-09-13 13:46:56 +000084 }
drh1ccde152000-06-17 13:12:39 +000085
86 /* Figure out how many columns of data are supplied. If the data
drhc6b52df2002-01-04 03:09:29 +000087 ** is coming from a SELECT statement, then this step has to generate
drh1ccde152000-06-17 13:12:39 +000088 ** all the code to implement the SELECT statement and leave the data
89 ** in a temporary table. If data is coming from an expression list,
90 ** then we just have to count the number of expressions.
91 */
drh5974a302000-06-07 14:42:26 +000092 if( pSelect ){
93 int rc;
94 srcTab = pParse->nTab++;
drh99fcd712001-10-13 01:06:47 +000095 sqliteVdbeAddOp(v, OP_OpenTemp, srcTab, 0);
drh5974a302000-06-07 14:42:26 +000096 rc = sqliteSelect(pParse, pSelect, SRT_Table, srcTab);
drhdaffd0e2001-04-11 14:28:42 +000097 if( rc || pParse->nErr || sqlite_malloc_failed ) goto insert_cleanup;
drh5974a302000-06-07 14:42:26 +000098 assert( pSelect->pEList );
drh967e8b72000-06-21 13:59:10 +000099 nColumn = pSelect->pEList->nExpr;
drh5974a302000-06-07 14:42:26 +0000100 }else{
drhdaffd0e2001-04-11 14:28:42 +0000101 assert( pList!=0 );
drh5974a302000-06-07 14:42:26 +0000102 srcTab = -1;
103 assert( pList );
drh967e8b72000-06-21 13:59:10 +0000104 nColumn = pList->nExpr;
drh5974a302000-06-07 14:42:26 +0000105 }
drh1ccde152000-06-17 13:12:39 +0000106
107 /* Make sure the number of columns in the source data matches the number
108 ** of columns to be inserted into the table.
109 */
drh967e8b72000-06-21 13:59:10 +0000110 if( pColumn==0 && nColumn!=pTab->nCol ){
drhcce7d172000-05-31 15:34:51 +0000111 char zNum1[30];
112 char zNum2[30];
drh967e8b72000-06-21 13:59:10 +0000113 sprintf(zNum1,"%d", nColumn);
drhcce7d172000-05-31 15:34:51 +0000114 sprintf(zNum2,"%d", pTab->nCol);
115 sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName,
116 " has ", zNum2, " columns but ",
117 zNum1, " values were supplied", 0);
118 pParse->nErr++;
119 goto insert_cleanup;
120 }
drh967e8b72000-06-21 13:59:10 +0000121 if( pColumn!=0 && nColumn!=pColumn->nId ){
drhcce7d172000-05-31 15:34:51 +0000122 char zNum1[30];
123 char zNum2[30];
drh967e8b72000-06-21 13:59:10 +0000124 sprintf(zNum1,"%d", nColumn);
125 sprintf(zNum2,"%d", pColumn->nId);
drhcce7d172000-05-31 15:34:51 +0000126 sqliteSetString(&pParse->zErrMsg, zNum1, " values for ",
127 zNum2, " columns", 0);
128 pParse->nErr++;
129 goto insert_cleanup;
130 }
drh1ccde152000-06-17 13:12:39 +0000131
132 /* If the INSERT statement included an IDLIST term, then make sure
133 ** all elements of the IDLIST really are columns of the table and
134 ** remember the column indices.
drhc8392582001-12-31 02:48:51 +0000135 **
136 ** If the table has an INTEGER PRIMARY KEY column and that column
137 ** is named in the IDLIST, then record in the keyColumn variable
138 ** the index into IDLIST of the primary key column. keyColumn is
139 ** the index of the primary key as it appears in IDLIST, not as
140 ** is appears in the original table. (The index of the primary
141 ** key in the original table is pTab->iPKey.)
drh1ccde152000-06-17 13:12:39 +0000142 */
drh967e8b72000-06-21 13:59:10 +0000143 if( pColumn ){
144 for(i=0; i<pColumn->nId; i++){
145 pColumn->a[i].idx = -1;
drhcce7d172000-05-31 15:34:51 +0000146 }
drh967e8b72000-06-21 13:59:10 +0000147 for(i=0; i<pColumn->nId; i++){
drhcce7d172000-05-31 15:34:51 +0000148 for(j=0; j<pTab->nCol; j++){
drh967e8b72000-06-21 13:59:10 +0000149 if( sqliteStrICmp(pColumn->a[i].zName, pTab->aCol[j].zName)==0 ){
150 pColumn->a[i].idx = j;
drh4a324312001-12-21 14:30:42 +0000151 if( j==pTab->iPKey ){
drh9aa028d2001-12-22 21:48:29 +0000152 keyColumn = i;
drh4a324312001-12-21 14:30:42 +0000153 }
drhcce7d172000-05-31 15:34:51 +0000154 break;
155 }
156 }
157 if( j>=pTab->nCol ){
158 sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName,
drh967e8b72000-06-21 13:59:10 +0000159 " has no column named ", pColumn->a[i].zName, 0);
drhcce7d172000-05-31 15:34:51 +0000160 pParse->nErr++;
161 goto insert_cleanup;
162 }
163 }
164 }
drh1ccde152000-06-17 13:12:39 +0000165
drhaacc5432002-01-06 17:07:40 +0000166 /* If there is no IDLIST term but the table has an integer primary
drhc8392582001-12-31 02:48:51 +0000167 ** key, the set the keyColumn variable to the primary key column index
168 ** in the original table definition.
drh4a324312001-12-21 14:30:42 +0000169 */
170 if( pColumn==0 ){
171 keyColumn = pTab->iPKey;
172 }
173
drh1ccde152000-06-17 13:12:39 +0000174 /* Open cursors into the table that is received the new data and
175 ** all indices of that table.
176 */
drh5974a302000-06-07 14:42:26 +0000177 base = pParse->nTab;
drhf57b3392001-10-08 13:22:32 +0000178 openOp = pTab->isTemp ? OP_OpenWrAux : OP_OpenWrite;
drh99fcd712001-10-13 01:06:47 +0000179 sqliteVdbeAddOp(v, openOp, base, pTab->tnum);
180 sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC);
drh5974a302000-06-07 14:42:26 +0000181 for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
drh99fcd712001-10-13 01:06:47 +0000182 sqliteVdbeAddOp(v, openOp, idx+base, pIdx->tnum);
183 sqliteVdbeChangeP3(v, -1, pIdx->zName, P3_STATIC);
drh19a775c2000-06-05 18:54:46 +0000184 }
drh1ccde152000-06-17 13:12:39 +0000185
186 /* If the data source is a SELECT statement, then we have to create
187 ** a loop because there might be multiple rows of data. If the data
188 ** source is an expression list, then exactly one row will be inserted
189 ** and the loop is not used.
190 */
drh5974a302000-06-07 14:42:26 +0000191 if( srcTab>=0 ){
drh1bee3d72001-10-15 00:44:35 +0000192 if( db->flags & SQLITE_CountRows ){
193 sqliteVdbeAddOp(v, OP_Integer, 0, 0); /* Initialize the row count */
194 }
drh5974a302000-06-07 14:42:26 +0000195 iBreak = sqliteVdbeMakeLabel(v);
drh6b563442001-11-07 16:48:26 +0000196 sqliteVdbeAddOp(v, OP_Rewind, srcTab, iBreak);
197 iCont = sqliteVdbeCurrentAddr(v);
drh5974a302000-06-07 14:42:26 +0000198 }
drh1ccde152000-06-17 13:12:39 +0000199
drh4a324312001-12-21 14:30:42 +0000200 /* Push the record number for the new entry onto the stack. The
201 ** record number is a randomly generate integer created by NewRecno
202 ** except when the table has an INTEGER PRIMARY KEY column, in which
203 ** case the record number is the same as that column.
drh1ccde152000-06-17 13:12:39 +0000204 */
drh4a324312001-12-21 14:30:42 +0000205 if( keyColumn>=0 ){
206 if( srcTab>=0 ){
207 sqliteVdbeAddOp(v, OP_Column, srcTab, keyColumn);
208 }else{
209 sqliteExprCode(pParse, pList->a[keyColumn].pExpr);
210 }
drh8aff1012001-12-22 14:49:24 +0000211 sqliteVdbeAddOp(v, OP_MustBeInt, 0, 0);
drh4a324312001-12-21 14:30:42 +0000212 }else{
213 sqliteVdbeAddOp(v, OP_NewRecno, base, 0);
214 }
215
drhc8392582001-12-31 02:48:51 +0000216 /* If there are indices, we'll need the new record number again, so make
drh4a324312001-12-21 14:30:42 +0000217 ** a copy.
218 */
drh5974a302000-06-07 14:42:26 +0000219 if( pTab->pIndex ){
drh99fcd712001-10-13 01:06:47 +0000220 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
drh5974a302000-06-07 14:42:26 +0000221 }
drh4a324312001-12-21 14:30:42 +0000222
drhaacc5432002-01-06 17:07:40 +0000223 /* Push onto the stack, data for all columns of the new entry, beginning
drh4a324312001-12-21 14:30:42 +0000224 ** with the first column.
225 */
drh5974a302000-06-07 14:42:26 +0000226 for(i=0; i<pTab->nCol; i++){
drh4a324312001-12-21 14:30:42 +0000227 if( i==pTab->iPKey ){
228 /* The value of the INTEGER PRIMARY KEY column is always a NULL.
drhaacc5432002-01-06 17:07:40 +0000229 ** Whenever this column is read, the record number will be substituted
230 ** in its place. So will fill this column with a NULL to avoid
231 ** taking up data space with information that will never be used. */
drh4a324312001-12-21 14:30:42 +0000232 sqliteVdbeAddOp(v, OP_String, 0, 0);
233 continue;
234 }
drh967e8b72000-06-21 13:59:10 +0000235 if( pColumn==0 ){
drh5974a302000-06-07 14:42:26 +0000236 j = i;
237 }else{
drh967e8b72000-06-21 13:59:10 +0000238 for(j=0; j<pColumn->nId; j++){
239 if( pColumn->a[j].idx==i ) break;
drh5974a302000-06-07 14:42:26 +0000240 }
drhbed86902000-06-02 13:27:59 +0000241 }
drh967e8b72000-06-21 13:59:10 +0000242 if( pColumn && j>=pColumn->nId ){
drh99fcd712001-10-13 01:06:47 +0000243 sqliteVdbeAddOp(v, OP_String, 0, 0);
244 sqliteVdbeChangeP3(v, -1, pTab->aCol[i].zDflt, P3_STATIC);
drh5974a302000-06-07 14:42:26 +0000245 }else if( srcTab>=0 ){
drh99fcd712001-10-13 01:06:47 +0000246 sqliteVdbeAddOp(v, OP_Column, srcTab, i);
drh5974a302000-06-07 14:42:26 +0000247 }else{
248 sqliteExprCode(pParse, pList->a[j].pExpr);
249 }
250 }
drh1ccde152000-06-17 13:12:39 +0000251
drh4a324312001-12-21 14:30:42 +0000252 /* Create the new record and put it into the database.
253 */
254 sqliteVdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0);
drh6b125452002-01-28 15:53:03 +0000255 sqliteVdbeAddOp(v, OP_PutIntKey, base, keyColumn>=0);
drh4a324312001-12-21 14:30:42 +0000256
drh1ccde152000-06-17 13:12:39 +0000257 /* Create appropriate entries for the new data row in all indices
258 ** of the table.
259 */
drh5974a302000-06-07 14:42:26 +0000260 for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
261 if( pIdx->pNext ){
drh99fcd712001-10-13 01:06:47 +0000262 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
drhcce7d172000-05-31 15:34:51 +0000263 }
drh967e8b72000-06-21 13:59:10 +0000264 for(i=0; i<pIdx->nColumn; i++){
265 int idx = pIdx->aiColumn[i];
drh4a324312001-12-21 14:30:42 +0000266 if( idx==pTab->iPKey ){
267 /* Copy the record number in place of the INTEGER PRIMARY KEY column */
268 sqliteVdbeAddOp(v, OP_Dup, i, 0);
269 continue;
270 }
drh967e8b72000-06-21 13:59:10 +0000271 if( pColumn==0 ){
drh5974a302000-06-07 14:42:26 +0000272 j = idx;
drhcce7d172000-05-31 15:34:51 +0000273 }else{
drh967e8b72000-06-21 13:59:10 +0000274 for(j=0; j<pColumn->nId; j++){
275 if( pColumn->a[j].idx==idx ) break;
drhcce7d172000-05-31 15:34:51 +0000276 }
277 }
drh967e8b72000-06-21 13:59:10 +0000278 if( pColumn && j>=pColumn->nId ){
drh99fcd712001-10-13 01:06:47 +0000279 sqliteVdbeAddOp(v, OP_String, 0, 0);
280 sqliteVdbeChangeP3(v, -1, pTab->aCol[idx].zDflt, P3_STATIC);
drh5974a302000-06-07 14:42:26 +0000281 }else if( srcTab>=0 ){
drh99fcd712001-10-13 01:06:47 +0000282 sqliteVdbeAddOp(v, OP_Column, srcTab, idx);
drhcce7d172000-05-31 15:34:51 +0000283 }else{
284 sqliteExprCode(pParse, pList->a[j].pExpr);
285 }
286 }
drh99fcd712001-10-13 01:06:47 +0000287 sqliteVdbeAddOp(v, OP_MakeIdxKey, pIdx->nColumn, 0);
drh8721ce42001-11-07 14:22:00 +0000288 sqliteVdbeAddOp(v, OP_IdxPut, idx+base, pIdx->isUnique);
drh5974a302000-06-07 14:42:26 +0000289 }
drh1ccde152000-06-17 13:12:39 +0000290
drh1bee3d72001-10-15 00:44:35 +0000291
292 /* If inserting from a SELECT, keep a count of the number of
293 ** rows inserted.
294 */
295 if( srcTab>=0 && (db->flags & SQLITE_CountRows)!=0 ){
296 sqliteVdbeAddOp(v, OP_AddImm, 1, 0);
297 }
298
drh1ccde152000-06-17 13:12:39 +0000299 /* The bottom of the loop, if the data source is a SELECT statement
300 */
drh5974a302000-06-07 14:42:26 +0000301 if( srcTab>=0 ){
drh6b563442001-11-07 16:48:26 +0000302 sqliteVdbeAddOp(v, OP_Next, srcTab, iCont);
drh99fcd712001-10-13 01:06:47 +0000303 sqliteVdbeResolveLabel(v, iBreak);
drh6b563442001-11-07 16:48:26 +0000304 sqliteVdbeAddOp(v, OP_Close, srcTab, 0);
305 }
306 sqliteVdbeAddOp(v, OP_Close, base, 0);
307 for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
308 sqliteVdbeAddOp(v, OP_Close, idx+base, 0);
drhcce7d172000-05-31 15:34:51 +0000309 }
drhecdc7532001-09-23 02:35:53 +0000310 if( (db->flags & SQLITE_InTrans)==0 ){
drh99fcd712001-10-13 01:06:47 +0000311 sqliteVdbeAddOp(v, OP_Commit, 0, 0);
drh5e00f6c2001-09-13 13:46:56 +0000312 }
313
drh1bee3d72001-10-15 00:44:35 +0000314 /*
315 ** Return the number of rows inserted.
316 */
317 if( db->flags & SQLITE_CountRows ){
318 sqliteVdbeAddOp(v, OP_ColumnCount, 1, 0);
319 sqliteVdbeAddOp(v, OP_ColumnName, 0, 0);
320 sqliteVdbeChangeP3(v, -1, "rows inserted", P3_STATIC);
321 if( srcTab<0 ){
322 sqliteVdbeAddOp(v, OP_Integer, 1, 0);
323 }
324 sqliteVdbeAddOp(v, OP_Callback, 1, 0);
325 }
drhcce7d172000-05-31 15:34:51 +0000326
327insert_cleanup:
drh5974a302000-06-07 14:42:26 +0000328 if( pList ) sqliteExprListDelete(pList);
329 if( pSelect ) sqliteSelectDelete(pSelect);
drh967e8b72000-06-21 13:59:10 +0000330 sqliteIdListDelete(pColumn);
drhcce7d172000-05-31 15:34:51 +0000331}