blob: a044bc144894148090df156a40fb8cd3d0d8af0c [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**
drh967e8b72000-06-21 13:59:10 +000027** $Id: insert.c,v 1.11 2000/06/21 13:59:12 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
drh1ccde152000-06-17 13:12:39 +000063 /* Locate the table into which we will be inserting new information.
64 */
drhcce7d172000-05-31 15:34:51 +000065 zTab = sqliteTableNameFromToken(pTableName);
66 pTab = sqliteFindTable(pParse->db, zTab);
67 sqliteFree(zTab);
68 if( pTab==0 ){
69 sqliteSetNString(&pParse->zErrMsg, "no such table: ", 0,
70 pTableName->z, pTableName->n, 0);
71 pParse->nErr++;
72 goto insert_cleanup;
73 }
74 if( pTab->readOnly ){
75 sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName,
76 " may not be modified", 0);
77 pParse->nErr++;
78 goto insert_cleanup;
79 }
drh1ccde152000-06-17 13:12:39 +000080
81 /* Allocate a VDBE
82 */
drhd8bc7082000-06-07 23:51:50 +000083 v = sqliteGetVdbe(pParse);
drh5974a302000-06-07 14:42:26 +000084 if( v==0 ) goto insert_cleanup;
drh1ccde152000-06-17 13:12:39 +000085
86 /* Figure out how many columns of data are supplied. If the data
87 ** is comming from a SELECT statement, then this step has to generate
88 ** all the code to implement the SELECT statement and leave the data
89 ** in a temporary table. If data is coming from an expression list,
90 ** then we just have to count the number of expressions.
91 */
drh5974a302000-06-07 14:42:26 +000092 if( pSelect ){
93 int rc;
94 srcTab = pParse->nTab++;
95 sqliteVdbeAddOp(v, OP_Open, srcTab, 1, 0, 0);
96 rc = sqliteSelect(pParse, pSelect, SRT_Table, srcTab);
97 if( rc ) goto insert_cleanup;
98 assert( pSelect->pEList );
drh967e8b72000-06-21 13:59:10 +000099 nColumn = pSelect->pEList->nExpr;
drh5974a302000-06-07 14:42:26 +0000100 }else{
101 srcTab = -1;
102 assert( pList );
drh967e8b72000-06-21 13:59:10 +0000103 nColumn = pList->nExpr;
drh5974a302000-06-07 14:42:26 +0000104 }
drh1ccde152000-06-17 13:12:39 +0000105
106 /* Make sure the number of columns in the source data matches the number
107 ** of columns to be inserted into the table.
108 */
drh967e8b72000-06-21 13:59:10 +0000109 if( pColumn==0 && nColumn!=pTab->nCol ){
drhcce7d172000-05-31 15:34:51 +0000110 char zNum1[30];
111 char zNum2[30];
drh967e8b72000-06-21 13:59:10 +0000112 sprintf(zNum1,"%d", nColumn);
drhcce7d172000-05-31 15:34:51 +0000113 sprintf(zNum2,"%d", pTab->nCol);
114 sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName,
115 " has ", zNum2, " columns but ",
116 zNum1, " values were supplied", 0);
117 pParse->nErr++;
118 goto insert_cleanup;
119 }
drh967e8b72000-06-21 13:59:10 +0000120 if( pColumn!=0 && nColumn!=pColumn->nId ){
drhcce7d172000-05-31 15:34:51 +0000121 char zNum1[30];
122 char zNum2[30];
drh967e8b72000-06-21 13:59:10 +0000123 sprintf(zNum1,"%d", nColumn);
124 sprintf(zNum2,"%d", pColumn->nId);
drhcce7d172000-05-31 15:34:51 +0000125 sqliteSetString(&pParse->zErrMsg, zNum1, " values for ",
126 zNum2, " columns", 0);
127 pParse->nErr++;
128 goto insert_cleanup;
129 }
drh1ccde152000-06-17 13:12:39 +0000130
131 /* If the INSERT statement included an IDLIST term, then make sure
132 ** all elements of the IDLIST really are columns of the table and
133 ** remember the column indices.
134 */
drh967e8b72000-06-21 13:59:10 +0000135 if( pColumn ){
136 for(i=0; i<pColumn->nId; i++){
137 pColumn->a[i].idx = -1;
drhcce7d172000-05-31 15:34:51 +0000138 }
drh967e8b72000-06-21 13:59:10 +0000139 for(i=0; i<pColumn->nId; i++){
drhcce7d172000-05-31 15:34:51 +0000140 for(j=0; j<pTab->nCol; j++){
drh967e8b72000-06-21 13:59:10 +0000141 if( sqliteStrICmp(pColumn->a[i].zName, pTab->aCol[j].zName)==0 ){
142 pColumn->a[i].idx = j;
drhcce7d172000-05-31 15:34:51 +0000143 break;
144 }
145 }
146 if( j>=pTab->nCol ){
147 sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName,
drh967e8b72000-06-21 13:59:10 +0000148 " has no column named ", pColumn->a[i].zName, 0);
drhcce7d172000-05-31 15:34:51 +0000149 pParse->nErr++;
150 goto insert_cleanup;
151 }
152 }
153 }
drh1ccde152000-06-17 13:12:39 +0000154
155 /* Open cursors into the table that is received the new data and
156 ** all indices of that table.
157 */
drh5974a302000-06-07 14:42:26 +0000158 base = pParse->nTab;
159 sqliteVdbeAddOp(v, OP_Open, base, 1, pTab->zName, 0);
160 for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
161 sqliteVdbeAddOp(v, OP_Open, idx+base, 1, pIdx->zName, 0);
drh19a775c2000-06-05 18:54:46 +0000162 }
drh1ccde152000-06-17 13:12:39 +0000163
164 /* If the data source is a SELECT statement, then we have to create
165 ** a loop because there might be multiple rows of data. If the data
166 ** source is an expression list, then exactly one row will be inserted
167 ** and the loop is not used.
168 */
drh5974a302000-06-07 14:42:26 +0000169 if( srcTab>=0 ){
170 sqliteVdbeAddOp(v, OP_Rewind, srcTab, 0, 0, 0);
171 iBreak = sqliteVdbeMakeLabel(v);
172 iCont = sqliteVdbeAddOp(v, OP_Next, srcTab, iBreak, 0, 0);
173 }
drh1ccde152000-06-17 13:12:39 +0000174
175 /* Create a new entry in the table and fill it with data.
176 */
drh5974a302000-06-07 14:42:26 +0000177 sqliteVdbeAddOp(v, OP_New, 0, 0, 0, 0);
178 if( pTab->pIndex ){
179 sqliteVdbeAddOp(v, OP_Dup, 0, 0, 0, 0);
180 }
181 for(i=0; i<pTab->nCol; i++){
drh967e8b72000-06-21 13:59:10 +0000182 if( pColumn==0 ){
drh5974a302000-06-07 14:42:26 +0000183 j = i;
184 }else{
drh967e8b72000-06-21 13:59:10 +0000185 for(j=0; j<pColumn->nId; j++){
186 if( pColumn->a[j].idx==i ) break;
drh5974a302000-06-07 14:42:26 +0000187 }
drhbed86902000-06-02 13:27:59 +0000188 }
drh967e8b72000-06-21 13:59:10 +0000189 if( pColumn && j>=pColumn->nId ){
drh5974a302000-06-07 14:42:26 +0000190 char *zDflt = pTab->aCol[i].zDflt;
191 if( zDflt==0 ){
192 sqliteVdbeAddOp(v, OP_Null, 0, 0, 0, 0);
193 }else{
194 sqliteVdbeAddOp(v, OP_String, 0, 0, zDflt, 0);
195 }
196 }else if( srcTab>=0 ){
197 sqliteVdbeAddOp(v, OP_Field, srcTab, i, 0, 0);
198 }else{
199 sqliteExprCode(pParse, pList->a[j].pExpr);
200 }
201 }
202 sqliteVdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0, 0, 0);
203 sqliteVdbeAddOp(v, OP_Put, base, 0, 0, 0);
drh1ccde152000-06-17 13:12:39 +0000204
205 /* Create appropriate entries for the new data row in all indices
206 ** of the table.
207 */
drh5974a302000-06-07 14:42:26 +0000208 for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
209 if( pIdx->pNext ){
drhcce7d172000-05-31 15:34:51 +0000210 sqliteVdbeAddOp(v, OP_Dup, 0, 0, 0, 0);
211 }
drh967e8b72000-06-21 13:59:10 +0000212 for(i=0; i<pIdx->nColumn; i++){
213 int idx = pIdx->aiColumn[i];
214 if( pColumn==0 ){
drh5974a302000-06-07 14:42:26 +0000215 j = idx;
drhcce7d172000-05-31 15:34:51 +0000216 }else{
drh967e8b72000-06-21 13:59:10 +0000217 for(j=0; j<pColumn->nId; j++){
218 if( pColumn->a[j].idx==idx ) break;
drhcce7d172000-05-31 15:34:51 +0000219 }
220 }
drh967e8b72000-06-21 13:59:10 +0000221 if( pColumn && j>=pColumn->nId ){
drh5974a302000-06-07 14:42:26 +0000222 char *zDflt = pTab->aCol[idx].zDflt;
drhc61053b2000-06-04 12:58:36 +0000223 if( zDflt==0 ){
224 sqliteVdbeAddOp(v, OP_Null, 0, 0, 0, 0);
225 }else{
226 sqliteVdbeAddOp(v, OP_String, 0, 0, zDflt, 0);
227 }
drh5974a302000-06-07 14:42:26 +0000228 }else if( srcTab>=0 ){
drhcc85b412000-06-07 15:11:27 +0000229 sqliteVdbeAddOp(v, OP_Field, srcTab, idx, 0, 0);
drhcce7d172000-05-31 15:34:51 +0000230 }else{
231 sqliteExprCode(pParse, pList->a[j].pExpr);
232 }
233 }
drh967e8b72000-06-21 13:59:10 +0000234 sqliteVdbeAddOp(v, OP_MakeKey, pIdx->nColumn, 0, 0, 0);
drh5974a302000-06-07 14:42:26 +0000235 sqliteVdbeAddOp(v, OP_PutIdx, idx+base, 0, 0, 0);
drh5974a302000-06-07 14:42:26 +0000236 }
drh1ccde152000-06-17 13:12:39 +0000237
238 /* The bottom of the loop, if the data source is a SELECT statement
239 */
drh5974a302000-06-07 14:42:26 +0000240 if( srcTab>=0 ){
241 sqliteVdbeAddOp(v, OP_Goto, 0, iCont, 0, 0);
242 sqliteVdbeAddOp(v, OP_Noop, 0, 0, 0, iBreak);
drhcce7d172000-05-31 15:34:51 +0000243 }
244
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}