blob: 8f93d5cebde8d5718696a0c9c689220f1b06e0b7 [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**
drh1bee3d72001-10-15 00:44:35 +000015** $Id: insert.c,v 1.24 2001/10/15 00:44:36 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 */
drhcce7d172000-05-31 15:34:51 +000052
drhdaffd0e2001-04-11 14:28:42 +000053 if( pParse->nErr || sqlite_malloc_failed ) goto insert_cleanup;
drhecdc7532001-09-23 02:35:53 +000054 db = pParse->db;
drhdaffd0e2001-04-11 14:28:42 +000055
drh1ccde152000-06-17 13:12:39 +000056 /* Locate the table into which we will be inserting new information.
57 */
drhcce7d172000-05-31 15:34:51 +000058 zTab = sqliteTableNameFromToken(pTableName);
drhdaffd0e2001-04-11 14:28:42 +000059 if( zTab==0 ) goto insert_cleanup;
drhecdc7532001-09-23 02:35:53 +000060 pTab = sqliteFindTable(db, zTab);
drhcce7d172000-05-31 15:34:51 +000061 sqliteFree(zTab);
62 if( pTab==0 ){
63 sqliteSetNString(&pParse->zErrMsg, "no such table: ", 0,
64 pTableName->z, pTableName->n, 0);
65 pParse->nErr++;
66 goto insert_cleanup;
67 }
68 if( pTab->readOnly ){
69 sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName,
70 " may not be modified", 0);
71 pParse->nErr++;
72 goto insert_cleanup;
73 }
drh1ccde152000-06-17 13:12:39 +000074
75 /* Allocate a VDBE
76 */
drhd8bc7082000-06-07 23:51:50 +000077 v = sqliteGetVdbe(pParse);
drh5974a302000-06-07 14:42:26 +000078 if( v==0 ) goto insert_cleanup;
drhecdc7532001-09-23 02:35:53 +000079 if( (db->flags & SQLITE_InTrans)==0 ){
drh99fcd712001-10-13 01:06:47 +000080 sqliteVdbeAddOp(v, OP_Transaction, 0, 0);
81 sqliteVdbeAddOp(v, OP_VerifyCookie, db->schema_cookie, 0);
drhecdc7532001-09-23 02:35:53 +000082 pParse->schemaVerified = 1;
drh5e00f6c2001-09-13 13:46:56 +000083 }
drh1ccde152000-06-17 13:12:39 +000084
85 /* Figure out how many columns of data are supplied. If the data
86 ** is comming from a SELECT statement, then this step has to generate
87 ** all the code to implement the SELECT statement and leave the data
88 ** in a temporary table. If data is coming from an expression list,
89 ** then we just have to count the number of expressions.
90 */
drh5974a302000-06-07 14:42:26 +000091 if( pSelect ){
92 int rc;
93 srcTab = pParse->nTab++;
drh99fcd712001-10-13 01:06:47 +000094 sqliteVdbeAddOp(v, OP_OpenTemp, srcTab, 0);
drh5974a302000-06-07 14:42:26 +000095 rc = sqliteSelect(pParse, pSelect, SRT_Table, srcTab);
drhdaffd0e2001-04-11 14:28:42 +000096 if( rc || pParse->nErr || sqlite_malloc_failed ) goto insert_cleanup;
drh5974a302000-06-07 14:42:26 +000097 assert( pSelect->pEList );
drh967e8b72000-06-21 13:59:10 +000098 nColumn = pSelect->pEList->nExpr;
drh5974a302000-06-07 14:42:26 +000099 }else{
drhdaffd0e2001-04-11 14:28:42 +0000100 assert( pList!=0 );
drh5974a302000-06-07 14:42:26 +0000101 srcTab = -1;
102 assert( pList );
drh967e8b72000-06-21 13:59:10 +0000103 nColumn = pList->nExpr;
drh5974a302000-06-07 14:42:26 +0000104 }
drh1ccde152000-06-17 13:12:39 +0000105
106 /* Make sure the number of columns in the source data matches the number
107 ** of columns to be inserted into the table.
108 */
drh967e8b72000-06-21 13:59:10 +0000109 if( pColumn==0 && nColumn!=pTab->nCol ){
drhcce7d172000-05-31 15:34:51 +0000110 char zNum1[30];
111 char zNum2[30];
drh967e8b72000-06-21 13:59:10 +0000112 sprintf(zNum1,"%d", nColumn);
drhcce7d172000-05-31 15:34:51 +0000113 sprintf(zNum2,"%d", pTab->nCol);
114 sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName,
115 " has ", zNum2, " columns but ",
116 zNum1, " values were supplied", 0);
117 pParse->nErr++;
118 goto insert_cleanup;
119 }
drh967e8b72000-06-21 13:59:10 +0000120 if( pColumn!=0 && nColumn!=pColumn->nId ){
drhcce7d172000-05-31 15:34:51 +0000121 char zNum1[30];
122 char zNum2[30];
drh967e8b72000-06-21 13:59:10 +0000123 sprintf(zNum1,"%d", nColumn);
124 sprintf(zNum2,"%d", pColumn->nId);
drhcce7d172000-05-31 15:34:51 +0000125 sqliteSetString(&pParse->zErrMsg, zNum1, " values for ",
126 zNum2, " columns", 0);
127 pParse->nErr++;
128 goto insert_cleanup;
129 }
drh1ccde152000-06-17 13:12:39 +0000130
131 /* If the INSERT statement included an IDLIST term, then make sure
132 ** all elements of the IDLIST really are columns of the table and
133 ** remember the column indices.
134 */
drh967e8b72000-06-21 13:59:10 +0000135 if( pColumn ){
136 for(i=0; i<pColumn->nId; i++){
137 pColumn->a[i].idx = -1;
drhcce7d172000-05-31 15:34:51 +0000138 }
drh967e8b72000-06-21 13:59:10 +0000139 for(i=0; i<pColumn->nId; i++){
drhcce7d172000-05-31 15:34:51 +0000140 for(j=0; j<pTab->nCol; j++){
drh967e8b72000-06-21 13:59:10 +0000141 if( sqliteStrICmp(pColumn->a[i].zName, pTab->aCol[j].zName)==0 ){
142 pColumn->a[i].idx = j;
drhcce7d172000-05-31 15:34:51 +0000143 break;
144 }
145 }
146 if( j>=pTab->nCol ){
147 sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName,
drh967e8b72000-06-21 13:59:10 +0000148 " has no column named ", pColumn->a[i].zName, 0);
drhcce7d172000-05-31 15:34:51 +0000149 pParse->nErr++;
150 goto insert_cleanup;
151 }
152 }
153 }
drh1ccde152000-06-17 13:12:39 +0000154
155 /* Open cursors into the table that is received the new data and
156 ** all indices of that table.
157 */
drh5974a302000-06-07 14:42:26 +0000158 base = pParse->nTab;
drhf57b3392001-10-08 13:22:32 +0000159 openOp = pTab->isTemp ? OP_OpenWrAux : OP_OpenWrite;
drh99fcd712001-10-13 01:06:47 +0000160 sqliteVdbeAddOp(v, openOp, base, pTab->tnum);
161 sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC);
drh5974a302000-06-07 14:42:26 +0000162 for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
drh99fcd712001-10-13 01:06:47 +0000163 sqliteVdbeAddOp(v, openOp, idx+base, pIdx->tnum);
164 sqliteVdbeChangeP3(v, -1, pIdx->zName, P3_STATIC);
drh19a775c2000-06-05 18:54:46 +0000165 }
drh1ccde152000-06-17 13:12:39 +0000166
167 /* If the data source is a SELECT statement, then we have to create
168 ** a loop because there might be multiple rows of data. If the data
169 ** source is an expression list, then exactly one row will be inserted
170 ** and the loop is not used.
171 */
drh5974a302000-06-07 14:42:26 +0000172 if( srcTab>=0 ){
drh1bee3d72001-10-15 00:44:35 +0000173 if( db->flags & SQLITE_CountRows ){
174 sqliteVdbeAddOp(v, OP_Integer, 0, 0); /* Initialize the row count */
175 }
drh99fcd712001-10-13 01:06:47 +0000176 sqliteVdbeAddOp(v, OP_Rewind, srcTab, 0);
drh5974a302000-06-07 14:42:26 +0000177 iBreak = sqliteVdbeMakeLabel(v);
drh99fcd712001-10-13 01:06:47 +0000178 iCont = sqliteVdbeAddOp(v, OP_Next, srcTab, iBreak);
drh5974a302000-06-07 14:42:26 +0000179 }
drh1ccde152000-06-17 13:12:39 +0000180
181 /* Create a new entry in the table and fill it with data.
182 */
drh99fcd712001-10-13 01:06:47 +0000183 sqliteVdbeAddOp(v, OP_NewRecno, base, 0);
drh5974a302000-06-07 14:42:26 +0000184 if( pTab->pIndex ){
drh99fcd712001-10-13 01:06:47 +0000185 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
drh5974a302000-06-07 14:42:26 +0000186 }
187 for(i=0; i<pTab->nCol; i++){
drh967e8b72000-06-21 13:59:10 +0000188 if( pColumn==0 ){
drh5974a302000-06-07 14:42:26 +0000189 j = i;
190 }else{
drh967e8b72000-06-21 13:59:10 +0000191 for(j=0; j<pColumn->nId; j++){
192 if( pColumn->a[j].idx==i ) break;
drh5974a302000-06-07 14:42:26 +0000193 }
drhbed86902000-06-02 13:27:59 +0000194 }
drh967e8b72000-06-21 13:59:10 +0000195 if( pColumn && j>=pColumn->nId ){
drh99fcd712001-10-13 01:06:47 +0000196 sqliteVdbeAddOp(v, OP_String, 0, 0);
197 sqliteVdbeChangeP3(v, -1, pTab->aCol[i].zDflt, P3_STATIC);
drh5974a302000-06-07 14:42:26 +0000198 }else if( srcTab>=0 ){
drh99fcd712001-10-13 01:06:47 +0000199 sqliteVdbeAddOp(v, OP_Column, srcTab, i);
drh5974a302000-06-07 14:42:26 +0000200 }else{
201 sqliteExprCode(pParse, pList->a[j].pExpr);
202 }
203 }
drh99fcd712001-10-13 01:06:47 +0000204 sqliteVdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0);
205 sqliteVdbeAddOp(v, OP_Put, base, 0);
drh1bee3d72001-10-15 00:44:35 +0000206
drh1ccde152000-06-17 13:12:39 +0000207
208 /* Create appropriate entries for the new data row in all indices
209 ** of the table.
210 */
drh5974a302000-06-07 14:42:26 +0000211 for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
212 if( pIdx->pNext ){
drh99fcd712001-10-13 01:06:47 +0000213 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
drhcce7d172000-05-31 15:34:51 +0000214 }
drh967e8b72000-06-21 13:59:10 +0000215 for(i=0; i<pIdx->nColumn; i++){
216 int idx = pIdx->aiColumn[i];
217 if( pColumn==0 ){
drh5974a302000-06-07 14:42:26 +0000218 j = idx;
drhcce7d172000-05-31 15:34:51 +0000219 }else{
drh967e8b72000-06-21 13:59:10 +0000220 for(j=0; j<pColumn->nId; j++){
221 if( pColumn->a[j].idx==idx ) break;
drhcce7d172000-05-31 15:34:51 +0000222 }
223 }
drh967e8b72000-06-21 13:59:10 +0000224 if( pColumn && j>=pColumn->nId ){
drh99fcd712001-10-13 01:06:47 +0000225 sqliteVdbeAddOp(v, OP_String, 0, 0);
226 sqliteVdbeChangeP3(v, -1, pTab->aCol[idx].zDflt, P3_STATIC);
drh5974a302000-06-07 14:42:26 +0000227 }else if( srcTab>=0 ){
drh99fcd712001-10-13 01:06:47 +0000228 sqliteVdbeAddOp(v, OP_Column, srcTab, idx);
drhcce7d172000-05-31 15:34:51 +0000229 }else{
230 sqliteExprCode(pParse, pList->a[j].pExpr);
231 }
232 }
drh99fcd712001-10-13 01:06:47 +0000233 sqliteVdbeAddOp(v, OP_MakeIdxKey, pIdx->nColumn, 0);
234 sqliteVdbeAddOp(v, OP_PutIdx, idx+base, pIdx->isUnique);
drh5974a302000-06-07 14:42:26 +0000235 }
drh1ccde152000-06-17 13:12:39 +0000236
drh1bee3d72001-10-15 00:44:35 +0000237
238 /* If inserting from a SELECT, keep a count of the number of
239 ** rows inserted.
240 */
241 if( srcTab>=0 && (db->flags & SQLITE_CountRows)!=0 ){
242 sqliteVdbeAddOp(v, OP_AddImm, 1, 0);
243 }
244
drh1ccde152000-06-17 13:12:39 +0000245 /* The bottom of the loop, if the data source is a SELECT statement
246 */
drh5974a302000-06-07 14:42:26 +0000247 if( srcTab>=0 ){
drh99fcd712001-10-13 01:06:47 +0000248 sqliteVdbeAddOp(v, OP_Goto, 0, iCont);
249 sqliteVdbeResolveLabel(v, iBreak);
250 sqliteVdbeAddOp(v, OP_Noop, 0, 0);
drhcce7d172000-05-31 15:34:51 +0000251 }
drhecdc7532001-09-23 02:35:53 +0000252 if( (db->flags & SQLITE_InTrans)==0 ){
drh99fcd712001-10-13 01:06:47 +0000253 sqliteVdbeAddOp(v, OP_Commit, 0, 0);
drh5e00f6c2001-09-13 13:46:56 +0000254 }
255
drh1bee3d72001-10-15 00:44:35 +0000256 /*
257 ** Return the number of rows inserted.
258 */
259 if( db->flags & SQLITE_CountRows ){
260 sqliteVdbeAddOp(v, OP_ColumnCount, 1, 0);
261 sqliteVdbeAddOp(v, OP_ColumnName, 0, 0);
262 sqliteVdbeChangeP3(v, -1, "rows inserted", P3_STATIC);
263 if( srcTab<0 ){
264 sqliteVdbeAddOp(v, OP_Integer, 1, 0);
265 }
266 sqliteVdbeAddOp(v, OP_Callback, 1, 0);
267 }
drhcce7d172000-05-31 15:34:51 +0000268
269insert_cleanup:
drh5974a302000-06-07 14:42:26 +0000270 if( pList ) sqliteExprListDelete(pList);
271 if( pSelect ) sqliteSelectDelete(pSelect);
drh967e8b72000-06-21 13:59:10 +0000272 sqliteIdListDelete(pColumn);
drhcce7d172000-05-31 15:34:51 +0000273}