blob: 7756b10b94d8d5dbd57fe6db1f7219cd08398895 [file] [log] [blame]
drhcce7d172000-05-31 15:34:51 +00001/*
2** Copyright (c) 1999, 2000 D. Richard Hipp
3**
4** This program is free software; you can redistribute it and/or
5** modify it under the terms of the GNU General Public
6** License as published by the Free Software Foundation; either
7** version 2 of the License, or (at your option) any later version.
8**
9** This program is distributed in the hope that it will be useful,
10** but WITHOUT ANY WARRANTY; without even the implied warranty of
11** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12** General Public License for more details.
13**
14** You should have received a copy of the GNU General Public
15** License along with this library; if not, write to the
16** Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17** Boston, MA 02111-1307, USA.
18**
19** Author contact information:
20** drh@hwaci.com
21** http://www.hwaci.com/drh/
22**
23*************************************************************************
24** This file contains C code routines that are called by the parser
25** to handle INSERT statements.
26**
drhdaffd0e2001-04-11 14:28:42 +000027** $Id: insert.c,v 1.13 2001/04/11 14:28:42 drh Exp $
drhcce7d172000-05-31 15:34:51 +000028*/
29#include "sqliteInt.h"
30
31/*
drh1ccde152000-06-17 13:12:39 +000032** This routine is call to handle SQL of the following forms:
drhcce7d172000-05-31 15:34:51 +000033**
34** insert into TABLE (IDLIST) values(EXPRLIST)
drh1ccde152000-06-17 13:12:39 +000035** insert into TABLE (IDLIST) select
drhcce7d172000-05-31 15:34:51 +000036**
drh1ccde152000-06-17 13:12:39 +000037** The IDLIST following the table name is always optional. If omitted,
38** then a list of all columns for the table is substituted. The IDLIST
drh967e8b72000-06-21 13:59:10 +000039** appears in the pColumn parameter. pColumn is NULL if IDLIST is omitted.
drh1ccde152000-06-17 13:12:39 +000040**
41** The pList parameter holds EXPRLIST in the first form of the INSERT
42** statement above, and pSelect is NULL. For the second form, pList is
43** NULL and pSelect is a pointer to the select statement used to generate
44** data for the insert.
drhcce7d172000-05-31 15:34:51 +000045*/
46void sqliteInsert(
47 Parse *pParse, /* Parser context */
48 Token *pTableName, /* Name of table into which we are inserting */
49 ExprList *pList, /* List of values to be inserted */
drh5974a302000-06-07 14:42:26 +000050 Select *pSelect, /* A SELECT statement to use as the data source */
drh967e8b72000-06-21 13:59:10 +000051 IdList *pColumn /* Column names corresponding to IDLIST. */
drhcce7d172000-05-31 15:34:51 +000052){
drh5974a302000-06-07 14:42:26 +000053 Table *pTab; /* The table to insert into */
54 char *zTab; /* Name of the table into which we are inserting */
55 int i, j, idx; /* Loop counters */
56 Vdbe *v; /* Generate code into this virtual machine */
57 Index *pIdx; /* For looping over indices of the table */
58 int srcTab; /* Date comes from this temporary cursor if >=0 */
drh967e8b72000-06-21 13:59:10 +000059 int nColumn; /* Number of columns in the data */
drh5974a302000-06-07 14:42:26 +000060 int base; /* First available cursor */
61 int iCont, iBreak; /* Beginning and end of the loop over srcTab */
drhcce7d172000-05-31 15:34:51 +000062
drhdaffd0e2001-04-11 14:28:42 +000063 if( pParse->nErr || sqlite_malloc_failed ) goto insert_cleanup;
64
drh1ccde152000-06-17 13:12:39 +000065 /* Locate the table into which we will be inserting new information.
66 */
drhcce7d172000-05-31 15:34:51 +000067 zTab = sqliteTableNameFromToken(pTableName);
drhdaffd0e2001-04-11 14:28:42 +000068 if( zTab==0 ) goto insert_cleanup;
drhcce7d172000-05-31 15:34:51 +000069 pTab = sqliteFindTable(pParse->db, zTab);
70 sqliteFree(zTab);
71 if( pTab==0 ){
72 sqliteSetNString(&pParse->zErrMsg, "no such table: ", 0,
73 pTableName->z, pTableName->n, 0);
74 pParse->nErr++;
75 goto insert_cleanup;
76 }
77 if( pTab->readOnly ){
78 sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName,
79 " may not be modified", 0);
80 pParse->nErr++;
81 goto insert_cleanup;
82 }
drh1ccde152000-06-17 13:12:39 +000083
84 /* Allocate a VDBE
85 */
drhd8bc7082000-06-07 23:51:50 +000086 v = sqliteGetVdbe(pParse);
drh5974a302000-06-07 14:42:26 +000087 if( v==0 ) goto insert_cleanup;
drh1ccde152000-06-17 13:12:39 +000088
89 /* Figure out how many columns of data are supplied. If the data
90 ** is comming from a SELECT statement, then this step has to generate
91 ** all the code to implement the SELECT statement and leave the data
92 ** in a temporary table. If data is coming from an expression list,
93 ** then we just have to count the number of expressions.
94 */
drh5974a302000-06-07 14:42:26 +000095 if( pSelect ){
96 int rc;
97 srcTab = pParse->nTab++;
drh345fda32001-01-15 22:51:08 +000098 sqliteVdbeAddOp(v, OP_OpenTbl, srcTab, 1, 0, 0);
drh5974a302000-06-07 14:42:26 +000099 rc = sqliteSelect(pParse, pSelect, SRT_Table, srcTab);
drhdaffd0e2001-04-11 14:28:42 +0000100 if( rc || pParse->nErr || sqlite_malloc_failed ) goto insert_cleanup;
drh5974a302000-06-07 14:42:26 +0000101 assert( pSelect->pEList );
drh967e8b72000-06-21 13:59:10 +0000102 nColumn = pSelect->pEList->nExpr;
drh5974a302000-06-07 14:42:26 +0000103 }else{
drhdaffd0e2001-04-11 14:28:42 +0000104 assert( pList!=0 );
drh5974a302000-06-07 14:42:26 +0000105 srcTab = -1;
106 assert( pList );
drh967e8b72000-06-21 13:59:10 +0000107 nColumn = pList->nExpr;
drh5974a302000-06-07 14:42:26 +0000108 }
drh1ccde152000-06-17 13:12:39 +0000109
110 /* Make sure the number of columns in the source data matches the number
111 ** of columns to be inserted into the table.
112 */
drh967e8b72000-06-21 13:59:10 +0000113 if( pColumn==0 && nColumn!=pTab->nCol ){
drhcce7d172000-05-31 15:34:51 +0000114 char zNum1[30];
115 char zNum2[30];
drh967e8b72000-06-21 13:59:10 +0000116 sprintf(zNum1,"%d", nColumn);
drhcce7d172000-05-31 15:34:51 +0000117 sprintf(zNum2,"%d", pTab->nCol);
118 sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName,
119 " has ", zNum2, " columns but ",
120 zNum1, " values were supplied", 0);
121 pParse->nErr++;
122 goto insert_cleanup;
123 }
drh967e8b72000-06-21 13:59:10 +0000124 if( pColumn!=0 && nColumn!=pColumn->nId ){
drhcce7d172000-05-31 15:34:51 +0000125 char zNum1[30];
126 char zNum2[30];
drh967e8b72000-06-21 13:59:10 +0000127 sprintf(zNum1,"%d", nColumn);
128 sprintf(zNum2,"%d", pColumn->nId);
drhcce7d172000-05-31 15:34:51 +0000129 sqliteSetString(&pParse->zErrMsg, zNum1, " values for ",
130 zNum2, " columns", 0);
131 pParse->nErr++;
132 goto insert_cleanup;
133 }
drh1ccde152000-06-17 13:12:39 +0000134
135 /* If the INSERT statement included an IDLIST term, then make sure
136 ** all elements of the IDLIST really are columns of the table and
137 ** remember the column indices.
138 */
drh967e8b72000-06-21 13:59:10 +0000139 if( pColumn ){
140 for(i=0; i<pColumn->nId; i++){
141 pColumn->a[i].idx = -1;
drhcce7d172000-05-31 15:34:51 +0000142 }
drh967e8b72000-06-21 13:59:10 +0000143 for(i=0; i<pColumn->nId; i++){
drhcce7d172000-05-31 15:34:51 +0000144 for(j=0; j<pTab->nCol; j++){
drh967e8b72000-06-21 13:59:10 +0000145 if( sqliteStrICmp(pColumn->a[i].zName, pTab->aCol[j].zName)==0 ){
146 pColumn->a[i].idx = j;
drhcce7d172000-05-31 15:34:51 +0000147 break;
148 }
149 }
150 if( j>=pTab->nCol ){
151 sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName,
drh967e8b72000-06-21 13:59:10 +0000152 " has no column named ", pColumn->a[i].zName, 0);
drhcce7d172000-05-31 15:34:51 +0000153 pParse->nErr++;
154 goto insert_cleanup;
155 }
156 }
157 }
drh1ccde152000-06-17 13:12:39 +0000158
159 /* Open cursors into the table that is received the new data and
160 ** all indices of that table.
161 */
drh5974a302000-06-07 14:42:26 +0000162 base = pParse->nTab;
drh345fda32001-01-15 22:51:08 +0000163 sqliteVdbeAddOp(v, OP_OpenTbl, base, 1, pTab->zName, 0);
drh5974a302000-06-07 14:42:26 +0000164 for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
drh345fda32001-01-15 22:51:08 +0000165 sqliteVdbeAddOp(v, OP_OpenIdx, idx+base, 1, pIdx->zName, 0);
drh19a775c2000-06-05 18:54:46 +0000166 }
drh1ccde152000-06-17 13:12:39 +0000167
168 /* If the data source is a SELECT statement, then we have to create
169 ** a loop because there might be multiple rows of data. If the data
170 ** source is an expression list, then exactly one row will be inserted
171 ** and the loop is not used.
172 */
drh5974a302000-06-07 14:42:26 +0000173 if( srcTab>=0 ){
174 sqliteVdbeAddOp(v, OP_Rewind, srcTab, 0, 0, 0);
175 iBreak = sqliteVdbeMakeLabel(v);
176 iCont = sqliteVdbeAddOp(v, OP_Next, srcTab, iBreak, 0, 0);
177 }
drh1ccde152000-06-17 13:12:39 +0000178
179 /* Create a new entry in the table and fill it with data.
180 */
drh5974a302000-06-07 14:42:26 +0000181 sqliteVdbeAddOp(v, OP_New, 0, 0, 0, 0);
182 if( pTab->pIndex ){
183 sqliteVdbeAddOp(v, OP_Dup, 0, 0, 0, 0);
184 }
185 for(i=0; i<pTab->nCol; i++){
drh967e8b72000-06-21 13:59:10 +0000186 if( pColumn==0 ){
drh5974a302000-06-07 14:42:26 +0000187 j = i;
188 }else{
drh967e8b72000-06-21 13:59:10 +0000189 for(j=0; j<pColumn->nId; j++){
190 if( pColumn->a[j].idx==i ) break;
drh5974a302000-06-07 14:42:26 +0000191 }
drhbed86902000-06-02 13:27:59 +0000192 }
drh967e8b72000-06-21 13:59:10 +0000193 if( pColumn && j>=pColumn->nId ){
drh5974a302000-06-07 14:42:26 +0000194 char *zDflt = pTab->aCol[i].zDflt;
195 if( zDflt==0 ){
196 sqliteVdbeAddOp(v, OP_Null, 0, 0, 0, 0);
197 }else{
198 sqliteVdbeAddOp(v, OP_String, 0, 0, zDflt, 0);
199 }
200 }else if( srcTab>=0 ){
201 sqliteVdbeAddOp(v, OP_Field, srcTab, i, 0, 0);
202 }else{
203 sqliteExprCode(pParse, pList->a[j].pExpr);
204 }
205 }
206 sqliteVdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0, 0, 0);
207 sqliteVdbeAddOp(v, OP_Put, base, 0, 0, 0);
drh1ccde152000-06-17 13:12:39 +0000208
209 /* Create appropriate entries for the new data row in all indices
210 ** of the table.
211 */
drh5974a302000-06-07 14:42:26 +0000212 for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
213 if( pIdx->pNext ){
drhcce7d172000-05-31 15:34:51 +0000214 sqliteVdbeAddOp(v, OP_Dup, 0, 0, 0, 0);
215 }
drh967e8b72000-06-21 13:59:10 +0000216 for(i=0; i<pIdx->nColumn; i++){
217 int idx = pIdx->aiColumn[i];
218 if( pColumn==0 ){
drh5974a302000-06-07 14:42:26 +0000219 j = idx;
drhcce7d172000-05-31 15:34:51 +0000220 }else{
drh967e8b72000-06-21 13:59:10 +0000221 for(j=0; j<pColumn->nId; j++){
222 if( pColumn->a[j].idx==idx ) break;
drhcce7d172000-05-31 15:34:51 +0000223 }
224 }
drh967e8b72000-06-21 13:59:10 +0000225 if( pColumn && j>=pColumn->nId ){
drh5974a302000-06-07 14:42:26 +0000226 char *zDflt = pTab->aCol[idx].zDflt;
drhc61053b2000-06-04 12:58:36 +0000227 if( zDflt==0 ){
228 sqliteVdbeAddOp(v, OP_Null, 0, 0, 0, 0);
229 }else{
230 sqliteVdbeAddOp(v, OP_String, 0, 0, zDflt, 0);
231 }
drh5974a302000-06-07 14:42:26 +0000232 }else if( srcTab>=0 ){
drhcc85b412000-06-07 15:11:27 +0000233 sqliteVdbeAddOp(v, OP_Field, srcTab, idx, 0, 0);
drhcce7d172000-05-31 15:34:51 +0000234 }else{
235 sqliteExprCode(pParse, pList->a[j].pExpr);
236 }
237 }
drh967e8b72000-06-21 13:59:10 +0000238 sqliteVdbeAddOp(v, OP_MakeKey, pIdx->nColumn, 0, 0, 0);
drh5974a302000-06-07 14:42:26 +0000239 sqliteVdbeAddOp(v, OP_PutIdx, idx+base, 0, 0, 0);
drh5974a302000-06-07 14:42:26 +0000240 }
drh1ccde152000-06-17 13:12:39 +0000241
242 /* The bottom of the loop, if the data source is a SELECT statement
243 */
drh5974a302000-06-07 14:42:26 +0000244 if( srcTab>=0 ){
245 sqliteVdbeAddOp(v, OP_Goto, 0, iCont, 0, 0);
246 sqliteVdbeAddOp(v, OP_Noop, 0, 0, 0, iBreak);
drhcce7d172000-05-31 15:34:51 +0000247 }
248
249insert_cleanup:
drh5974a302000-06-07 14:42:26 +0000250 if( pList ) sqliteExprListDelete(pList);
251 if( pSelect ) sqliteSelectDelete(pSelect);
drh967e8b72000-06-21 13:59:10 +0000252 sqliteIdListDelete(pColumn);
drhcce7d172000-05-31 15:34:51 +0000253}