blob: 28a13cdd01c59d488348cffadaa02e76f385a439 [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**
drh50e5dad2001-09-15 00:57:28 +000027** $Id: insert.c,v 1.17 2001/09/15 00:57:29 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;
drh5e00f6c2001-09-13 13:46:56 +000088 if( (pParse->db->flags & SQLITE_InTrans)==0 ){
89 sqliteVdbeAddOp(v, OP_Transaction, 0, 0, 0, 0);
drh50e5dad2001-09-15 00:57:28 +000090 sqliteVdbeAddOp(v, OP_VerifyCookie, pParse->db->schema_cookie, 0, 0, 0);
drh5e00f6c2001-09-13 13:46:56 +000091 }
drh1ccde152000-06-17 13:12:39 +000092
93 /* Figure out how many columns of data are supplied. If the data
94 ** is comming from a SELECT statement, then this step has to generate
95 ** all the code to implement the SELECT statement and leave the data
96 ** in a temporary table. If data is coming from an expression list,
97 ** then we just have to count the number of expressions.
98 */
drh5974a302000-06-07 14:42:26 +000099 if( pSelect ){
100 int rc;
101 srcTab = pParse->nTab++;
drhbe0072d2001-09-13 14:46:09 +0000102 sqliteVdbeAddOp(v, OP_OpenTemp, srcTab, 0, 0, 0);
drh5974a302000-06-07 14:42:26 +0000103 rc = sqliteSelect(pParse, pSelect, SRT_Table, srcTab);
drhdaffd0e2001-04-11 14:28:42 +0000104 if( rc || pParse->nErr || sqlite_malloc_failed ) goto insert_cleanup;
drh5974a302000-06-07 14:42:26 +0000105 assert( pSelect->pEList );
drh967e8b72000-06-21 13:59:10 +0000106 nColumn = pSelect->pEList->nExpr;
drh5974a302000-06-07 14:42:26 +0000107 }else{
drhdaffd0e2001-04-11 14:28:42 +0000108 assert( pList!=0 );
drh5974a302000-06-07 14:42:26 +0000109 srcTab = -1;
110 assert( pList );
drh967e8b72000-06-21 13:59:10 +0000111 nColumn = pList->nExpr;
drh5974a302000-06-07 14:42:26 +0000112 }
drh1ccde152000-06-17 13:12:39 +0000113
114 /* Make sure the number of columns in the source data matches the number
115 ** of columns to be inserted into the table.
116 */
drh967e8b72000-06-21 13:59:10 +0000117 if( pColumn==0 && nColumn!=pTab->nCol ){
drhcce7d172000-05-31 15:34:51 +0000118 char zNum1[30];
119 char zNum2[30];
drh967e8b72000-06-21 13:59:10 +0000120 sprintf(zNum1,"%d", nColumn);
drhcce7d172000-05-31 15:34:51 +0000121 sprintf(zNum2,"%d", pTab->nCol);
122 sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName,
123 " has ", zNum2, " columns but ",
124 zNum1, " values were supplied", 0);
125 pParse->nErr++;
126 goto insert_cleanup;
127 }
drh967e8b72000-06-21 13:59:10 +0000128 if( pColumn!=0 && nColumn!=pColumn->nId ){
drhcce7d172000-05-31 15:34:51 +0000129 char zNum1[30];
130 char zNum2[30];
drh967e8b72000-06-21 13:59:10 +0000131 sprintf(zNum1,"%d", nColumn);
132 sprintf(zNum2,"%d", pColumn->nId);
drhcce7d172000-05-31 15:34:51 +0000133 sqliteSetString(&pParse->zErrMsg, zNum1, " values for ",
134 zNum2, " columns", 0);
135 pParse->nErr++;
136 goto insert_cleanup;
137 }
drh1ccde152000-06-17 13:12:39 +0000138
139 /* If the INSERT statement included an IDLIST term, then make sure
140 ** all elements of the IDLIST really are columns of the table and
141 ** remember the column indices.
142 */
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;
drhcce7d172000-05-31 15:34:51 +0000151 break;
152 }
153 }
154 if( j>=pTab->nCol ){
155 sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName,
drh967e8b72000-06-21 13:59:10 +0000156 " has no column named ", pColumn->a[i].zName, 0);
drhcce7d172000-05-31 15:34:51 +0000157 pParse->nErr++;
158 goto insert_cleanup;
159 }
160 }
161 }
drh1ccde152000-06-17 13:12:39 +0000162
163 /* Open cursors into the table that is received the new data and
164 ** all indices of that table.
165 */
drh5974a302000-06-07 14:42:26 +0000166 base = pParse->nTab;
drhbe0072d2001-09-13 14:46:09 +0000167 sqliteVdbeAddOp(v, OP_Open, base, pTab->tnum, pTab->zName, 0);
drh5974a302000-06-07 14:42:26 +0000168 for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
drhbe0072d2001-09-13 14:46:09 +0000169 sqliteVdbeAddOp(v, OP_Open, idx+base, pIdx->tnum, pIdx->zName, 0);
drh19a775c2000-06-05 18:54:46 +0000170 }
drh1ccde152000-06-17 13:12:39 +0000171
172 /* If the data source is a SELECT statement, then we have to create
173 ** a loop because there might be multiple rows of data. If the data
174 ** source is an expression list, then exactly one row will be inserted
175 ** and the loop is not used.
176 */
drh5974a302000-06-07 14:42:26 +0000177 if( srcTab>=0 ){
178 sqliteVdbeAddOp(v, OP_Rewind, srcTab, 0, 0, 0);
179 iBreak = sqliteVdbeMakeLabel(v);
180 iCont = sqliteVdbeAddOp(v, OP_Next, srcTab, iBreak, 0, 0);
181 }
drh1ccde152000-06-17 13:12:39 +0000182
183 /* Create a new entry in the table and fill it with data.
184 */
drh3fc190c2001-09-14 03:24:23 +0000185 sqliteVdbeAddOp(v, OP_NewRecno, base, 0, 0, 0);
drh5974a302000-06-07 14:42:26 +0000186 if( pTab->pIndex ){
187 sqliteVdbeAddOp(v, OP_Dup, 0, 0, 0, 0);
188 }
189 for(i=0; i<pTab->nCol; i++){
drh967e8b72000-06-21 13:59:10 +0000190 if( pColumn==0 ){
drh5974a302000-06-07 14:42:26 +0000191 j = i;
192 }else{
drh967e8b72000-06-21 13:59:10 +0000193 for(j=0; j<pColumn->nId; j++){
194 if( pColumn->a[j].idx==i ) break;
drh5974a302000-06-07 14:42:26 +0000195 }
drhbed86902000-06-02 13:27:59 +0000196 }
drh967e8b72000-06-21 13:59:10 +0000197 if( pColumn && j>=pColumn->nId ){
drh5974a302000-06-07 14:42:26 +0000198 char *zDflt = pTab->aCol[i].zDflt;
199 if( zDflt==0 ){
200 sqliteVdbeAddOp(v, OP_Null, 0, 0, 0, 0);
201 }else{
202 sqliteVdbeAddOp(v, OP_String, 0, 0, zDflt, 0);
203 }
204 }else if( srcTab>=0 ){
drhbe0072d2001-09-13 14:46:09 +0000205 sqliteVdbeAddOp(v, OP_Column, srcTab, i, 0, 0);
drh5974a302000-06-07 14:42:26 +0000206 }else{
207 sqliteExprCode(pParse, pList->a[j].pExpr);
208 }
209 }
210 sqliteVdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0, 0, 0);
211 sqliteVdbeAddOp(v, OP_Put, base, 0, 0, 0);
drh1ccde152000-06-17 13:12:39 +0000212
213 /* Create appropriate entries for the new data row in all indices
214 ** of the table.
215 */
drh5974a302000-06-07 14:42:26 +0000216 for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
217 if( pIdx->pNext ){
drhcce7d172000-05-31 15:34:51 +0000218 sqliteVdbeAddOp(v, OP_Dup, 0, 0, 0, 0);
219 }
drh967e8b72000-06-21 13:59:10 +0000220 for(i=0; i<pIdx->nColumn; i++){
221 int idx = pIdx->aiColumn[i];
222 if( pColumn==0 ){
drh5974a302000-06-07 14:42:26 +0000223 j = idx;
drhcce7d172000-05-31 15:34:51 +0000224 }else{
drh967e8b72000-06-21 13:59:10 +0000225 for(j=0; j<pColumn->nId; j++){
226 if( pColumn->a[j].idx==idx ) break;
drhcce7d172000-05-31 15:34:51 +0000227 }
228 }
drh967e8b72000-06-21 13:59:10 +0000229 if( pColumn && j>=pColumn->nId ){
drh5974a302000-06-07 14:42:26 +0000230 char *zDflt = pTab->aCol[idx].zDflt;
drhc61053b2000-06-04 12:58:36 +0000231 if( zDflt==0 ){
232 sqliteVdbeAddOp(v, OP_Null, 0, 0, 0, 0);
233 }else{
234 sqliteVdbeAddOp(v, OP_String, 0, 0, zDflt, 0);
235 }
drh5974a302000-06-07 14:42:26 +0000236 }else if( srcTab>=0 ){
drhbe0072d2001-09-13 14:46:09 +0000237 sqliteVdbeAddOp(v, OP_Column, srcTab, idx, 0, 0);
drhcce7d172000-05-31 15:34:51 +0000238 }else{
239 sqliteExprCode(pParse, pList->a[j].pExpr);
240 }
241 }
drh5e00f6c2001-09-13 13:46:56 +0000242 sqliteVdbeAddOp(v, OP_MakeIdxKey, pIdx->nColumn, 0, 0, 0);
drh5974a302000-06-07 14:42:26 +0000243 sqliteVdbeAddOp(v, OP_PutIdx, idx+base, 0, 0, 0);
drh5974a302000-06-07 14:42:26 +0000244 }
drh1ccde152000-06-17 13:12:39 +0000245
246 /* The bottom of the loop, if the data source is a SELECT statement
247 */
drh5974a302000-06-07 14:42:26 +0000248 if( srcTab>=0 ){
249 sqliteVdbeAddOp(v, OP_Goto, 0, iCont, 0, 0);
250 sqliteVdbeAddOp(v, OP_Noop, 0, 0, 0, iBreak);
drhcce7d172000-05-31 15:34:51 +0000251 }
drh5e00f6c2001-09-13 13:46:56 +0000252 if( (pParse->db->flags & SQLITE_InTrans)==0 ){
253 sqliteVdbeAddOp(v, OP_Commit, 0, 0, 0, 0);
254 }
255
drhcce7d172000-05-31 15:34:51 +0000256
257insert_cleanup:
drh5974a302000-06-07 14:42:26 +0000258 if( pList ) sqliteExprListDelete(pList);
259 if( pSelect ) sqliteSelectDelete(pSelect);
drh967e8b72000-06-21 13:59:10 +0000260 sqliteIdListDelete(pColumn);
drhcce7d172000-05-31 15:34:51 +0000261}