blob: 3fcb61e70815c725cd716fa783a8d021d71f48d1 [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**
drhb19a2bc2001-09-16 00:13:26 +000015** $Id: insert.c,v 1.18 2001/09/16 00:13:27 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 */
drhcce7d172000-05-31 15:34:51 +000050
drhdaffd0e2001-04-11 14:28:42 +000051 if( pParse->nErr || sqlite_malloc_failed ) goto insert_cleanup;
52
drh1ccde152000-06-17 13:12:39 +000053 /* Locate the table into which we will be inserting new information.
54 */
drhcce7d172000-05-31 15:34:51 +000055 zTab = sqliteTableNameFromToken(pTableName);
drhdaffd0e2001-04-11 14:28:42 +000056 if( zTab==0 ) goto insert_cleanup;
drhcce7d172000-05-31 15:34:51 +000057 pTab = sqliteFindTable(pParse->db, zTab);
58 sqliteFree(zTab);
59 if( pTab==0 ){
60 sqliteSetNString(&pParse->zErrMsg, "no such table: ", 0,
61 pTableName->z, pTableName->n, 0);
62 pParse->nErr++;
63 goto insert_cleanup;
64 }
65 if( pTab->readOnly ){
66 sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName,
67 " may not be modified", 0);
68 pParse->nErr++;
69 goto insert_cleanup;
70 }
drh1ccde152000-06-17 13:12:39 +000071
72 /* Allocate a VDBE
73 */
drhd8bc7082000-06-07 23:51:50 +000074 v = sqliteGetVdbe(pParse);
drh5974a302000-06-07 14:42:26 +000075 if( v==0 ) goto insert_cleanup;
drh5e00f6c2001-09-13 13:46:56 +000076 if( (pParse->db->flags & SQLITE_InTrans)==0 ){
77 sqliteVdbeAddOp(v, OP_Transaction, 0, 0, 0, 0);
drh50e5dad2001-09-15 00:57:28 +000078 sqliteVdbeAddOp(v, OP_VerifyCookie, pParse->db->schema_cookie, 0, 0, 0);
drh5e00f6c2001-09-13 13:46:56 +000079 }
drh1ccde152000-06-17 13:12:39 +000080
81 /* Figure out how many columns of data are supplied. If the data
82 ** is comming from a SELECT statement, then this step has to generate
83 ** all the code to implement the SELECT statement and leave the data
84 ** in a temporary table. If data is coming from an expression list,
85 ** then we just have to count the number of expressions.
86 */
drh5974a302000-06-07 14:42:26 +000087 if( pSelect ){
88 int rc;
89 srcTab = pParse->nTab++;
drhbe0072d2001-09-13 14:46:09 +000090 sqliteVdbeAddOp(v, OP_OpenTemp, srcTab, 0, 0, 0);
drh5974a302000-06-07 14:42:26 +000091 rc = sqliteSelect(pParse, pSelect, SRT_Table, srcTab);
drhdaffd0e2001-04-11 14:28:42 +000092 if( rc || pParse->nErr || sqlite_malloc_failed ) goto insert_cleanup;
drh5974a302000-06-07 14:42:26 +000093 assert( pSelect->pEList );
drh967e8b72000-06-21 13:59:10 +000094 nColumn = pSelect->pEList->nExpr;
drh5974a302000-06-07 14:42:26 +000095 }else{
drhdaffd0e2001-04-11 14:28:42 +000096 assert( pList!=0 );
drh5974a302000-06-07 14:42:26 +000097 srcTab = -1;
98 assert( pList );
drh967e8b72000-06-21 13:59:10 +000099 nColumn = pList->nExpr;
drh5974a302000-06-07 14:42:26 +0000100 }
drh1ccde152000-06-17 13:12:39 +0000101
102 /* Make sure the number of columns in the source data matches the number
103 ** of columns to be inserted into the table.
104 */
drh967e8b72000-06-21 13:59:10 +0000105 if( pColumn==0 && nColumn!=pTab->nCol ){
drhcce7d172000-05-31 15:34:51 +0000106 char zNum1[30];
107 char zNum2[30];
drh967e8b72000-06-21 13:59:10 +0000108 sprintf(zNum1,"%d", nColumn);
drhcce7d172000-05-31 15:34:51 +0000109 sprintf(zNum2,"%d", pTab->nCol);
110 sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName,
111 " has ", zNum2, " columns but ",
112 zNum1, " values were supplied", 0);
113 pParse->nErr++;
114 goto insert_cleanup;
115 }
drh967e8b72000-06-21 13:59:10 +0000116 if( pColumn!=0 && nColumn!=pColumn->nId ){
drhcce7d172000-05-31 15:34:51 +0000117 char zNum1[30];
118 char zNum2[30];
drh967e8b72000-06-21 13:59:10 +0000119 sprintf(zNum1,"%d", nColumn);
120 sprintf(zNum2,"%d", pColumn->nId);
drhcce7d172000-05-31 15:34:51 +0000121 sqliteSetString(&pParse->zErrMsg, zNum1, " values for ",
122 zNum2, " columns", 0);
123 pParse->nErr++;
124 goto insert_cleanup;
125 }
drh1ccde152000-06-17 13:12:39 +0000126
127 /* If the INSERT statement included an IDLIST term, then make sure
128 ** all elements of the IDLIST really are columns of the table and
129 ** remember the column indices.
130 */
drh967e8b72000-06-21 13:59:10 +0000131 if( pColumn ){
132 for(i=0; i<pColumn->nId; i++){
133 pColumn->a[i].idx = -1;
drhcce7d172000-05-31 15:34:51 +0000134 }
drh967e8b72000-06-21 13:59:10 +0000135 for(i=0; i<pColumn->nId; i++){
drhcce7d172000-05-31 15:34:51 +0000136 for(j=0; j<pTab->nCol; j++){
drh967e8b72000-06-21 13:59:10 +0000137 if( sqliteStrICmp(pColumn->a[i].zName, pTab->aCol[j].zName)==0 ){
138 pColumn->a[i].idx = j;
drhcce7d172000-05-31 15:34:51 +0000139 break;
140 }
141 }
142 if( j>=pTab->nCol ){
143 sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName,
drh967e8b72000-06-21 13:59:10 +0000144 " has no column named ", pColumn->a[i].zName, 0);
drhcce7d172000-05-31 15:34:51 +0000145 pParse->nErr++;
146 goto insert_cleanup;
147 }
148 }
149 }
drh1ccde152000-06-17 13:12:39 +0000150
151 /* Open cursors into the table that is received the new data and
152 ** all indices of that table.
153 */
drh5974a302000-06-07 14:42:26 +0000154 base = pParse->nTab;
drhbe0072d2001-09-13 14:46:09 +0000155 sqliteVdbeAddOp(v, OP_Open, base, pTab->tnum, pTab->zName, 0);
drh5974a302000-06-07 14:42:26 +0000156 for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
drhbe0072d2001-09-13 14:46:09 +0000157 sqliteVdbeAddOp(v, OP_Open, idx+base, pIdx->tnum, pIdx->zName, 0);
drh19a775c2000-06-05 18:54:46 +0000158 }
drh1ccde152000-06-17 13:12:39 +0000159
160 /* If the data source is a SELECT statement, then we have to create
161 ** a loop because there might be multiple rows of data. If the data
162 ** source is an expression list, then exactly one row will be inserted
163 ** and the loop is not used.
164 */
drh5974a302000-06-07 14:42:26 +0000165 if( srcTab>=0 ){
166 sqliteVdbeAddOp(v, OP_Rewind, srcTab, 0, 0, 0);
167 iBreak = sqliteVdbeMakeLabel(v);
168 iCont = sqliteVdbeAddOp(v, OP_Next, srcTab, iBreak, 0, 0);
169 }
drh1ccde152000-06-17 13:12:39 +0000170
171 /* Create a new entry in the table and fill it with data.
172 */
drh3fc190c2001-09-14 03:24:23 +0000173 sqliteVdbeAddOp(v, OP_NewRecno, base, 0, 0, 0);
drh5974a302000-06-07 14:42:26 +0000174 if( pTab->pIndex ){
175 sqliteVdbeAddOp(v, OP_Dup, 0, 0, 0, 0);
176 }
177 for(i=0; i<pTab->nCol; i++){
drh967e8b72000-06-21 13:59:10 +0000178 if( pColumn==0 ){
drh5974a302000-06-07 14:42:26 +0000179 j = i;
180 }else{
drh967e8b72000-06-21 13:59:10 +0000181 for(j=0; j<pColumn->nId; j++){
182 if( pColumn->a[j].idx==i ) break;
drh5974a302000-06-07 14:42:26 +0000183 }
drhbed86902000-06-02 13:27:59 +0000184 }
drh967e8b72000-06-21 13:59:10 +0000185 if( pColumn && j>=pColumn->nId ){
drh5974a302000-06-07 14:42:26 +0000186 char *zDflt = pTab->aCol[i].zDflt;
187 if( zDflt==0 ){
188 sqliteVdbeAddOp(v, OP_Null, 0, 0, 0, 0);
189 }else{
190 sqliteVdbeAddOp(v, OP_String, 0, 0, zDflt, 0);
191 }
192 }else if( srcTab>=0 ){
drhbe0072d2001-09-13 14:46:09 +0000193 sqliteVdbeAddOp(v, OP_Column, srcTab, i, 0, 0);
drh5974a302000-06-07 14:42:26 +0000194 }else{
195 sqliteExprCode(pParse, pList->a[j].pExpr);
196 }
197 }
198 sqliteVdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0, 0, 0);
199 sqliteVdbeAddOp(v, OP_Put, base, 0, 0, 0);
drh1ccde152000-06-17 13:12:39 +0000200
201 /* Create appropriate entries for the new data row in all indices
202 ** of the table.
203 */
drh5974a302000-06-07 14:42:26 +0000204 for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
205 if( pIdx->pNext ){
drhcce7d172000-05-31 15:34:51 +0000206 sqliteVdbeAddOp(v, OP_Dup, 0, 0, 0, 0);
207 }
drh967e8b72000-06-21 13:59:10 +0000208 for(i=0; i<pIdx->nColumn; i++){
209 int idx = pIdx->aiColumn[i];
210 if( pColumn==0 ){
drh5974a302000-06-07 14:42:26 +0000211 j = idx;
drhcce7d172000-05-31 15:34:51 +0000212 }else{
drh967e8b72000-06-21 13:59:10 +0000213 for(j=0; j<pColumn->nId; j++){
214 if( pColumn->a[j].idx==idx ) break;
drhcce7d172000-05-31 15:34:51 +0000215 }
216 }
drh967e8b72000-06-21 13:59:10 +0000217 if( pColumn && j>=pColumn->nId ){
drh5974a302000-06-07 14:42:26 +0000218 char *zDflt = pTab->aCol[idx].zDflt;
drhc61053b2000-06-04 12:58:36 +0000219 if( zDflt==0 ){
220 sqliteVdbeAddOp(v, OP_Null, 0, 0, 0, 0);
221 }else{
222 sqliteVdbeAddOp(v, OP_String, 0, 0, zDflt, 0);
223 }
drh5974a302000-06-07 14:42:26 +0000224 }else if( srcTab>=0 ){
drhbe0072d2001-09-13 14:46:09 +0000225 sqliteVdbeAddOp(v, OP_Column, srcTab, idx, 0, 0);
drhcce7d172000-05-31 15:34:51 +0000226 }else{
227 sqliteExprCode(pParse, pList->a[j].pExpr);
228 }
229 }
drh5e00f6c2001-09-13 13:46:56 +0000230 sqliteVdbeAddOp(v, OP_MakeIdxKey, pIdx->nColumn, 0, 0, 0);
drh5974a302000-06-07 14:42:26 +0000231 sqliteVdbeAddOp(v, OP_PutIdx, idx+base, 0, 0, 0);
drh5974a302000-06-07 14:42:26 +0000232 }
drh1ccde152000-06-17 13:12:39 +0000233
234 /* The bottom of the loop, if the data source is a SELECT statement
235 */
drh5974a302000-06-07 14:42:26 +0000236 if( srcTab>=0 ){
237 sqliteVdbeAddOp(v, OP_Goto, 0, iCont, 0, 0);
238 sqliteVdbeAddOp(v, OP_Noop, 0, 0, 0, iBreak);
drhcce7d172000-05-31 15:34:51 +0000239 }
drh5e00f6c2001-09-13 13:46:56 +0000240 if( (pParse->db->flags & SQLITE_InTrans)==0 ){
241 sqliteVdbeAddOp(v, OP_Commit, 0, 0, 0, 0);
242 }
243
drhcce7d172000-05-31 15:34:51 +0000244
245insert_cleanup:
drh5974a302000-06-07 14:42:26 +0000246 if( pList ) sqliteExprListDelete(pList);
247 if( pSelect ) sqliteSelectDelete(pSelect);
drh967e8b72000-06-21 13:59:10 +0000248 sqliteIdListDelete(pColumn);
drhcce7d172000-05-31 15:34:51 +0000249}