blob: 8de423f678f6634be30a48d383fab7dbb39a600b [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**
drhecdc7532001-09-23 02:35:53 +000015** $Id: insert.c,v 1.19 2001/09/23 02:35:53 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 */
drhcce7d172000-05-31 15:34:51 +000051
drhdaffd0e2001-04-11 14:28:42 +000052 if( pParse->nErr || sqlite_malloc_failed ) goto insert_cleanup;
drhecdc7532001-09-23 02:35:53 +000053 db = pParse->db;
drhdaffd0e2001-04-11 14:28:42 +000054
drh1ccde152000-06-17 13:12:39 +000055 /* Locate the table into which we will be inserting new information.
56 */
drhcce7d172000-05-31 15:34:51 +000057 zTab = sqliteTableNameFromToken(pTableName);
drhdaffd0e2001-04-11 14:28:42 +000058 if( zTab==0 ) goto insert_cleanup;
drhecdc7532001-09-23 02:35:53 +000059 pTab = sqliteFindTable(db, zTab);
drhcce7d172000-05-31 15:34:51 +000060 sqliteFree(zTab);
61 if( pTab==0 ){
62 sqliteSetNString(&pParse->zErrMsg, "no such table: ", 0,
63 pTableName->z, pTableName->n, 0);
64 pParse->nErr++;
65 goto insert_cleanup;
66 }
67 if( pTab->readOnly ){
68 sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName,
69 " may not be modified", 0);
70 pParse->nErr++;
71 goto insert_cleanup;
72 }
drh1ccde152000-06-17 13:12:39 +000073
74 /* Allocate a VDBE
75 */
drhd8bc7082000-06-07 23:51:50 +000076 v = sqliteGetVdbe(pParse);
drh5974a302000-06-07 14:42:26 +000077 if( v==0 ) goto insert_cleanup;
drhecdc7532001-09-23 02:35:53 +000078 if( (db->flags & SQLITE_InTrans)==0 ){
drh5e00f6c2001-09-13 13:46:56 +000079 sqliteVdbeAddOp(v, OP_Transaction, 0, 0, 0, 0);
drhecdc7532001-09-23 02:35:53 +000080 sqliteVdbeAddOp(v, OP_VerifyCookie, db->schema_cookie, 0, 0, 0);
81 pParse->schemaVerified = 1;
drh5e00f6c2001-09-13 13:46:56 +000082 }
drh1ccde152000-06-17 13:12:39 +000083
84 /* Figure out how many columns of data are supplied. If the data
85 ** is comming from a SELECT statement, then this step has to generate
86 ** all the code to implement the SELECT statement and leave the data
87 ** in a temporary table. If data is coming from an expression list,
88 ** then we just have to count the number of expressions.
89 */
drh5974a302000-06-07 14:42:26 +000090 if( pSelect ){
91 int rc;
92 srcTab = pParse->nTab++;
drhbe0072d2001-09-13 14:46:09 +000093 sqliteVdbeAddOp(v, OP_OpenTemp, srcTab, 0, 0, 0);
drh5974a302000-06-07 14:42:26 +000094 rc = sqliteSelect(pParse, pSelect, SRT_Table, srcTab);
drhdaffd0e2001-04-11 14:28:42 +000095 if( rc || pParse->nErr || sqlite_malloc_failed ) goto insert_cleanup;
drh5974a302000-06-07 14:42:26 +000096 assert( pSelect->pEList );
drh967e8b72000-06-21 13:59:10 +000097 nColumn = pSelect->pEList->nExpr;
drh5974a302000-06-07 14:42:26 +000098 }else{
drhdaffd0e2001-04-11 14:28:42 +000099 assert( pList!=0 );
drh5974a302000-06-07 14:42:26 +0000100 srcTab = -1;
101 assert( pList );
drh967e8b72000-06-21 13:59:10 +0000102 nColumn = pList->nExpr;
drh5974a302000-06-07 14:42:26 +0000103 }
drh1ccde152000-06-17 13:12:39 +0000104
105 /* Make sure the number of columns in the source data matches the number
106 ** of columns to be inserted into the table.
107 */
drh967e8b72000-06-21 13:59:10 +0000108 if( pColumn==0 && nColumn!=pTab->nCol ){
drhcce7d172000-05-31 15:34:51 +0000109 char zNum1[30];
110 char zNum2[30];
drh967e8b72000-06-21 13:59:10 +0000111 sprintf(zNum1,"%d", nColumn);
drhcce7d172000-05-31 15:34:51 +0000112 sprintf(zNum2,"%d", pTab->nCol);
113 sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName,
114 " has ", zNum2, " columns but ",
115 zNum1, " values were supplied", 0);
116 pParse->nErr++;
117 goto insert_cleanup;
118 }
drh967e8b72000-06-21 13:59:10 +0000119 if( pColumn!=0 && nColumn!=pColumn->nId ){
drhcce7d172000-05-31 15:34:51 +0000120 char zNum1[30];
121 char zNum2[30];
drh967e8b72000-06-21 13:59:10 +0000122 sprintf(zNum1,"%d", nColumn);
123 sprintf(zNum2,"%d", pColumn->nId);
drhcce7d172000-05-31 15:34:51 +0000124 sqliteSetString(&pParse->zErrMsg, zNum1, " values for ",
125 zNum2, " columns", 0);
126 pParse->nErr++;
127 goto insert_cleanup;
128 }
drh1ccde152000-06-17 13:12:39 +0000129
130 /* If the INSERT statement included an IDLIST term, then make sure
131 ** all elements of the IDLIST really are columns of the table and
132 ** remember the column indices.
133 */
drh967e8b72000-06-21 13:59:10 +0000134 if( pColumn ){
135 for(i=0; i<pColumn->nId; i++){
136 pColumn->a[i].idx = -1;
drhcce7d172000-05-31 15:34:51 +0000137 }
drh967e8b72000-06-21 13:59:10 +0000138 for(i=0; i<pColumn->nId; i++){
drhcce7d172000-05-31 15:34:51 +0000139 for(j=0; j<pTab->nCol; j++){
drh967e8b72000-06-21 13:59:10 +0000140 if( sqliteStrICmp(pColumn->a[i].zName, pTab->aCol[j].zName)==0 ){
141 pColumn->a[i].idx = j;
drhcce7d172000-05-31 15:34:51 +0000142 break;
143 }
144 }
145 if( j>=pTab->nCol ){
146 sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName,
drh967e8b72000-06-21 13:59:10 +0000147 " has no column named ", pColumn->a[i].zName, 0);
drhcce7d172000-05-31 15:34:51 +0000148 pParse->nErr++;
149 goto insert_cleanup;
150 }
151 }
152 }
drh1ccde152000-06-17 13:12:39 +0000153
154 /* Open cursors into the table that is received the new data and
155 ** all indices of that table.
156 */
drh5974a302000-06-07 14:42:26 +0000157 base = pParse->nTab;
drhecdc7532001-09-23 02:35:53 +0000158 sqliteVdbeAddOp(v, OP_OpenWrite, base, pTab->tnum, pTab->zName, 0);
drh5974a302000-06-07 14:42:26 +0000159 for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
drhecdc7532001-09-23 02:35:53 +0000160 sqliteVdbeAddOp(v, OP_OpenWrite, idx+base, pIdx->tnum, pIdx->zName, 0);
drh19a775c2000-06-05 18:54:46 +0000161 }
drh1ccde152000-06-17 13:12:39 +0000162
163 /* If the data source is a SELECT statement, then we have to create
164 ** a loop because there might be multiple rows of data. If the data
165 ** source is an expression list, then exactly one row will be inserted
166 ** and the loop is not used.
167 */
drh5974a302000-06-07 14:42:26 +0000168 if( srcTab>=0 ){
169 sqliteVdbeAddOp(v, OP_Rewind, srcTab, 0, 0, 0);
170 iBreak = sqliteVdbeMakeLabel(v);
171 iCont = sqliteVdbeAddOp(v, OP_Next, srcTab, iBreak, 0, 0);
172 }
drh1ccde152000-06-17 13:12:39 +0000173
174 /* Create a new entry in the table and fill it with data.
175 */
drh3fc190c2001-09-14 03:24:23 +0000176 sqliteVdbeAddOp(v, OP_NewRecno, base, 0, 0, 0);
drh5974a302000-06-07 14:42:26 +0000177 if( pTab->pIndex ){
178 sqliteVdbeAddOp(v, OP_Dup, 0, 0, 0, 0);
179 }
180 for(i=0; i<pTab->nCol; i++){
drh967e8b72000-06-21 13:59:10 +0000181 if( pColumn==0 ){
drh5974a302000-06-07 14:42:26 +0000182 j = i;
183 }else{
drh967e8b72000-06-21 13:59:10 +0000184 for(j=0; j<pColumn->nId; j++){
185 if( pColumn->a[j].idx==i ) break;
drh5974a302000-06-07 14:42:26 +0000186 }
drhbed86902000-06-02 13:27:59 +0000187 }
drh967e8b72000-06-21 13:59:10 +0000188 if( pColumn && j>=pColumn->nId ){
drh5974a302000-06-07 14:42:26 +0000189 char *zDflt = pTab->aCol[i].zDflt;
190 if( zDflt==0 ){
191 sqliteVdbeAddOp(v, OP_Null, 0, 0, 0, 0);
192 }else{
193 sqliteVdbeAddOp(v, OP_String, 0, 0, zDflt, 0);
194 }
195 }else if( srcTab>=0 ){
drhbe0072d2001-09-13 14:46:09 +0000196 sqliteVdbeAddOp(v, OP_Column, srcTab, i, 0, 0);
drh5974a302000-06-07 14:42:26 +0000197 }else{
198 sqliteExprCode(pParse, pList->a[j].pExpr);
199 }
200 }
201 sqliteVdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0, 0, 0);
202 sqliteVdbeAddOp(v, OP_Put, base, 0, 0, 0);
drh1ccde152000-06-17 13:12:39 +0000203
204 /* Create appropriate entries for the new data row in all indices
205 ** of the table.
206 */
drh5974a302000-06-07 14:42:26 +0000207 for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
208 if( pIdx->pNext ){
drhcce7d172000-05-31 15:34:51 +0000209 sqliteVdbeAddOp(v, OP_Dup, 0, 0, 0, 0);
210 }
drh967e8b72000-06-21 13:59:10 +0000211 for(i=0; i<pIdx->nColumn; i++){
212 int idx = pIdx->aiColumn[i];
213 if( pColumn==0 ){
drh5974a302000-06-07 14:42:26 +0000214 j = idx;
drhcce7d172000-05-31 15:34:51 +0000215 }else{
drh967e8b72000-06-21 13:59:10 +0000216 for(j=0; j<pColumn->nId; j++){
217 if( pColumn->a[j].idx==idx ) break;
drhcce7d172000-05-31 15:34:51 +0000218 }
219 }
drh967e8b72000-06-21 13:59:10 +0000220 if( pColumn && j>=pColumn->nId ){
drh5974a302000-06-07 14:42:26 +0000221 char *zDflt = pTab->aCol[idx].zDflt;
drhc61053b2000-06-04 12:58:36 +0000222 if( zDflt==0 ){
223 sqliteVdbeAddOp(v, OP_Null, 0, 0, 0, 0);
224 }else{
225 sqliteVdbeAddOp(v, OP_String, 0, 0, zDflt, 0);
226 }
drh5974a302000-06-07 14:42:26 +0000227 }else if( srcTab>=0 ){
drhbe0072d2001-09-13 14:46:09 +0000228 sqliteVdbeAddOp(v, OP_Column, srcTab, idx, 0, 0);
drhcce7d172000-05-31 15:34:51 +0000229 }else{
230 sqliteExprCode(pParse, pList->a[j].pExpr);
231 }
232 }
drh5e00f6c2001-09-13 13:46:56 +0000233 sqliteVdbeAddOp(v, OP_MakeIdxKey, pIdx->nColumn, 0, 0, 0);
drh5974a302000-06-07 14:42:26 +0000234 sqliteVdbeAddOp(v, OP_PutIdx, idx+base, 0, 0, 0);
drh5974a302000-06-07 14:42:26 +0000235 }
drh1ccde152000-06-17 13:12:39 +0000236
237 /* The bottom of the loop, if the data source is a SELECT statement
238 */
drh5974a302000-06-07 14:42:26 +0000239 if( srcTab>=0 ){
240 sqliteVdbeAddOp(v, OP_Goto, 0, iCont, 0, 0);
241 sqliteVdbeAddOp(v, OP_Noop, 0, 0, 0, iBreak);
drhcce7d172000-05-31 15:34:51 +0000242 }
drhecdc7532001-09-23 02:35:53 +0000243 if( (db->flags & SQLITE_InTrans)==0 ){
drh5e00f6c2001-09-13 13:46:56 +0000244 sqliteVdbeAddOp(v, OP_Commit, 0, 0, 0, 0);
245 }
246
drhcce7d172000-05-31 15:34:51 +0000247
248insert_cleanup:
drh5974a302000-06-07 14:42:26 +0000249 if( pList ) sqliteExprListDelete(pList);
250 if( pSelect ) sqliteSelectDelete(pSelect);
drh967e8b72000-06-21 13:59:10 +0000251 sqliteIdListDelete(pColumn);
drhcce7d172000-05-31 15:34:51 +0000252}