blob: 97a21046fc42c2b3cad6047814ab4fc7ddd3b264 [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**
drh5e00f6c2001-09-13 13:46:56 +000027** $Id: insert.c,v 1.14 2001/09/13 13:46:56 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);
90 }
drh1ccde152000-06-17 13:12:39 +000091
92 /* Figure out how many columns of data are supplied. If the data
93 ** is comming from a SELECT statement, then this step has to generate
94 ** all the code to implement the SELECT statement and leave the data
95 ** in a temporary table. If data is coming from an expression list,
96 ** then we just have to count the number of expressions.
97 */
drh5974a302000-06-07 14:42:26 +000098 if( pSelect ){
99 int rc;
100 srcTab = pParse->nTab++;
drh345fda32001-01-15 22:51:08 +0000101 sqliteVdbeAddOp(v, OP_OpenTbl, srcTab, 1, 0, 0);
drh5974a302000-06-07 14:42:26 +0000102 rc = sqliteSelect(pParse, pSelect, SRT_Table, srcTab);
drhdaffd0e2001-04-11 14:28:42 +0000103 if( rc || pParse->nErr || sqlite_malloc_failed ) goto insert_cleanup;
drh5974a302000-06-07 14:42:26 +0000104 assert( pSelect->pEList );
drh967e8b72000-06-21 13:59:10 +0000105 nColumn = pSelect->pEList->nExpr;
drh5974a302000-06-07 14:42:26 +0000106 }else{
drhdaffd0e2001-04-11 14:28:42 +0000107 assert( pList!=0 );
drh5974a302000-06-07 14:42:26 +0000108 srcTab = -1;
109 assert( pList );
drh967e8b72000-06-21 13:59:10 +0000110 nColumn = pList->nExpr;
drh5974a302000-06-07 14:42:26 +0000111 }
drh1ccde152000-06-17 13:12:39 +0000112
113 /* Make sure the number of columns in the source data matches the number
114 ** of columns to be inserted into the table.
115 */
drh967e8b72000-06-21 13:59:10 +0000116 if( pColumn==0 && nColumn!=pTab->nCol ){
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);
drhcce7d172000-05-31 15:34:51 +0000120 sprintf(zNum2,"%d", pTab->nCol);
121 sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName,
122 " has ", zNum2, " columns but ",
123 zNum1, " values were supplied", 0);
124 pParse->nErr++;
125 goto insert_cleanup;
126 }
drh967e8b72000-06-21 13:59:10 +0000127 if( pColumn!=0 && nColumn!=pColumn->nId ){
drhcce7d172000-05-31 15:34:51 +0000128 char zNum1[30];
129 char zNum2[30];
drh967e8b72000-06-21 13:59:10 +0000130 sprintf(zNum1,"%d", nColumn);
131 sprintf(zNum2,"%d", pColumn->nId);
drhcce7d172000-05-31 15:34:51 +0000132 sqliteSetString(&pParse->zErrMsg, zNum1, " values for ",
133 zNum2, " columns", 0);
134 pParse->nErr++;
135 goto insert_cleanup;
136 }
drh1ccde152000-06-17 13:12:39 +0000137
138 /* If the INSERT statement included an IDLIST term, then make sure
139 ** all elements of the IDLIST really are columns of the table and
140 ** remember the column indices.
141 */
drh967e8b72000-06-21 13:59:10 +0000142 if( pColumn ){
143 for(i=0; i<pColumn->nId; i++){
144 pColumn->a[i].idx = -1;
drhcce7d172000-05-31 15:34:51 +0000145 }
drh967e8b72000-06-21 13:59:10 +0000146 for(i=0; i<pColumn->nId; i++){
drhcce7d172000-05-31 15:34:51 +0000147 for(j=0; j<pTab->nCol; j++){
drh967e8b72000-06-21 13:59:10 +0000148 if( sqliteStrICmp(pColumn->a[i].zName, pTab->aCol[j].zName)==0 ){
149 pColumn->a[i].idx = j;
drhcce7d172000-05-31 15:34:51 +0000150 break;
151 }
152 }
153 if( j>=pTab->nCol ){
154 sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName,
drh967e8b72000-06-21 13:59:10 +0000155 " has no column named ", pColumn->a[i].zName, 0);
drhcce7d172000-05-31 15:34:51 +0000156 pParse->nErr++;
157 goto insert_cleanup;
158 }
159 }
160 }
drh1ccde152000-06-17 13:12:39 +0000161
162 /* Open cursors into the table that is received the new data and
163 ** all indices of that table.
164 */
drh5974a302000-06-07 14:42:26 +0000165 base = pParse->nTab;
drh345fda32001-01-15 22:51:08 +0000166 sqliteVdbeAddOp(v, OP_OpenTbl, base, 1, pTab->zName, 0);
drh5974a302000-06-07 14:42:26 +0000167 for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
drh345fda32001-01-15 22:51:08 +0000168 sqliteVdbeAddOp(v, OP_OpenIdx, idx+base, 1, pIdx->zName, 0);
drh19a775c2000-06-05 18:54:46 +0000169 }
drh1ccde152000-06-17 13:12:39 +0000170
171 /* If the data source is a SELECT statement, then we have to create
172 ** a loop because there might be multiple rows of data. If the data
173 ** source is an expression list, then exactly one row will be inserted
174 ** and the loop is not used.
175 */
drh5974a302000-06-07 14:42:26 +0000176 if( srcTab>=0 ){
177 sqliteVdbeAddOp(v, OP_Rewind, srcTab, 0, 0, 0);
178 iBreak = sqliteVdbeMakeLabel(v);
179 iCont = sqliteVdbeAddOp(v, OP_Next, srcTab, iBreak, 0, 0);
180 }
drh1ccde152000-06-17 13:12:39 +0000181
182 /* Create a new entry in the table and fill it with data.
183 */
drh5974a302000-06-07 14:42:26 +0000184 sqliteVdbeAddOp(v, OP_New, 0, 0, 0, 0);
185 if( pTab->pIndex ){
186 sqliteVdbeAddOp(v, OP_Dup, 0, 0, 0, 0);
187 }
188 for(i=0; i<pTab->nCol; i++){
drh967e8b72000-06-21 13:59:10 +0000189 if( pColumn==0 ){
drh5974a302000-06-07 14:42:26 +0000190 j = i;
191 }else{
drh967e8b72000-06-21 13:59:10 +0000192 for(j=0; j<pColumn->nId; j++){
193 if( pColumn->a[j].idx==i ) break;
drh5974a302000-06-07 14:42:26 +0000194 }
drhbed86902000-06-02 13:27:59 +0000195 }
drh967e8b72000-06-21 13:59:10 +0000196 if( pColumn && j>=pColumn->nId ){
drh5974a302000-06-07 14:42:26 +0000197 char *zDflt = pTab->aCol[i].zDflt;
198 if( zDflt==0 ){
199 sqliteVdbeAddOp(v, OP_Null, 0, 0, 0, 0);
200 }else{
201 sqliteVdbeAddOp(v, OP_String, 0, 0, zDflt, 0);
202 }
203 }else if( srcTab>=0 ){
204 sqliteVdbeAddOp(v, OP_Field, srcTab, i, 0, 0);
205 }else{
206 sqliteExprCode(pParse, pList->a[j].pExpr);
207 }
208 }
209 sqliteVdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0, 0, 0);
210 sqliteVdbeAddOp(v, OP_Put, base, 0, 0, 0);
drh1ccde152000-06-17 13:12:39 +0000211
212 /* Create appropriate entries for the new data row in all indices
213 ** of the table.
214 */
drh5974a302000-06-07 14:42:26 +0000215 for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
216 if( pIdx->pNext ){
drhcce7d172000-05-31 15:34:51 +0000217 sqliteVdbeAddOp(v, OP_Dup, 0, 0, 0, 0);
218 }
drh967e8b72000-06-21 13:59:10 +0000219 for(i=0; i<pIdx->nColumn; i++){
220 int idx = pIdx->aiColumn[i];
221 if( pColumn==0 ){
drh5974a302000-06-07 14:42:26 +0000222 j = idx;
drhcce7d172000-05-31 15:34:51 +0000223 }else{
drh967e8b72000-06-21 13:59:10 +0000224 for(j=0; j<pColumn->nId; j++){
225 if( pColumn->a[j].idx==idx ) break;
drhcce7d172000-05-31 15:34:51 +0000226 }
227 }
drh967e8b72000-06-21 13:59:10 +0000228 if( pColumn && j>=pColumn->nId ){
drh5974a302000-06-07 14:42:26 +0000229 char *zDflt = pTab->aCol[idx].zDflt;
drhc61053b2000-06-04 12:58:36 +0000230 if( zDflt==0 ){
231 sqliteVdbeAddOp(v, OP_Null, 0, 0, 0, 0);
232 }else{
233 sqliteVdbeAddOp(v, OP_String, 0, 0, zDflt, 0);
234 }
drh5974a302000-06-07 14:42:26 +0000235 }else if( srcTab>=0 ){
drhcc85b412000-06-07 15:11:27 +0000236 sqliteVdbeAddOp(v, OP_Field, srcTab, idx, 0, 0);
drhcce7d172000-05-31 15:34:51 +0000237 }else{
238 sqliteExprCode(pParse, pList->a[j].pExpr);
239 }
240 }
drh5e00f6c2001-09-13 13:46:56 +0000241 sqliteVdbeAddOp(v, OP_MakeIdxKey, pIdx->nColumn, 0, 0, 0);
drh5974a302000-06-07 14:42:26 +0000242 sqliteVdbeAddOp(v, OP_PutIdx, idx+base, 0, 0, 0);
drh5974a302000-06-07 14:42:26 +0000243 }
drh1ccde152000-06-17 13:12:39 +0000244
245 /* The bottom of the loop, if the data source is a SELECT statement
246 */
drh5974a302000-06-07 14:42:26 +0000247 if( srcTab>=0 ){
248 sqliteVdbeAddOp(v, OP_Goto, 0, iCont, 0, 0);
249 sqliteVdbeAddOp(v, OP_Noop, 0, 0, 0, iBreak);
drhcce7d172000-05-31 15:34:51 +0000250 }
drh5e00f6c2001-09-13 13:46:56 +0000251 if( (pParse->db->flags & SQLITE_InTrans)==0 ){
252 sqliteVdbeAddOp(v, OP_Commit, 0, 0, 0, 0);
253 }
254
drhcce7d172000-05-31 15:34:51 +0000255
256insert_cleanup:
drh5974a302000-06-07 14:42:26 +0000257 if( pList ) sqliteExprListDelete(pList);
258 if( pSelect ) sqliteSelectDelete(pSelect);
drh967e8b72000-06-21 13:59:10 +0000259 sqliteIdListDelete(pColumn);
drhcce7d172000-05-31 15:34:51 +0000260}