blob: 2d838d48f5a5b57d36d8b96b1fae021d4817de8f [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**
drhd8bc7082000-06-07 23:51:50 +000027** $Id: insert.c,v 1.9 2000/06/07 23:51:50 drh Exp $
drhcce7d172000-05-31 15:34:51 +000028*/
29#include "sqliteInt.h"
30
31/*
32** This routine is call to handle SQL of the following form:
33**
34** insert into TABLE (IDLIST) values(EXPRLIST)
35**
36** The parameters are the table name and the expression list.
37*/
38void sqliteInsert(
39 Parse *pParse, /* Parser context */
40 Token *pTableName, /* Name of table into which we are inserting */
41 ExprList *pList, /* List of values to be inserted */
drh5974a302000-06-07 14:42:26 +000042 Select *pSelect, /* A SELECT statement to use as the data source */
drhcce7d172000-05-31 15:34:51 +000043 IdList *pField /* Field name corresponding to pList. Might be NULL */
44){
drh5974a302000-06-07 14:42:26 +000045 Table *pTab; /* The table to insert into */
46 char *zTab; /* Name of the table into which we are inserting */
47 int i, j, idx; /* Loop counters */
48 Vdbe *v; /* Generate code into this virtual machine */
49 Index *pIdx; /* For looping over indices of the table */
50 int srcTab; /* Date comes from this temporary cursor if >=0 */
51 int nField; /* Number of columns in the data */
52 int base; /* First available cursor */
53 int iCont, iBreak; /* Beginning and end of the loop over srcTab */
drhcce7d172000-05-31 15:34:51 +000054
55 zTab = sqliteTableNameFromToken(pTableName);
56 pTab = sqliteFindTable(pParse->db, zTab);
57 sqliteFree(zTab);
58 if( pTab==0 ){
59 sqliteSetNString(&pParse->zErrMsg, "no such table: ", 0,
60 pTableName->z, pTableName->n, 0);
61 pParse->nErr++;
62 goto insert_cleanup;
63 }
64 if( pTab->readOnly ){
65 sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName,
66 " may not be modified", 0);
67 pParse->nErr++;
68 goto insert_cleanup;
69 }
drhd8bc7082000-06-07 23:51:50 +000070 v = sqliteGetVdbe(pParse);
drh5974a302000-06-07 14:42:26 +000071 if( v==0 ) goto insert_cleanup;
72 if( pSelect ){
73 int rc;
74 srcTab = pParse->nTab++;
75 sqliteVdbeAddOp(v, OP_Open, srcTab, 1, 0, 0);
76 rc = sqliteSelect(pParse, pSelect, SRT_Table, srcTab);
77 if( rc ) goto insert_cleanup;
78 assert( pSelect->pEList );
79 nField = pSelect->pEList->nExpr;
80 }else{
81 srcTab = -1;
82 assert( pList );
83 nField = pList->nExpr;
84 }
85 if( pField==0 && nField!=pTab->nCol ){
drhcce7d172000-05-31 15:34:51 +000086 char zNum1[30];
87 char zNum2[30];
drh5974a302000-06-07 14:42:26 +000088 sprintf(zNum1,"%d", nField);
drhcce7d172000-05-31 15:34:51 +000089 sprintf(zNum2,"%d", pTab->nCol);
90 sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName,
91 " has ", zNum2, " columns but ",
92 zNum1, " values were supplied", 0);
93 pParse->nErr++;
94 goto insert_cleanup;
95 }
drh5974a302000-06-07 14:42:26 +000096 if( pField!=0 && nField!=pField->nId ){
drhcce7d172000-05-31 15:34:51 +000097 char zNum1[30];
98 char zNum2[30];
drh5974a302000-06-07 14:42:26 +000099 sprintf(zNum1,"%d", nField);
drhcce7d172000-05-31 15:34:51 +0000100 sprintf(zNum2,"%d", pField->nId);
101 sqliteSetString(&pParse->zErrMsg, zNum1, " values for ",
102 zNum2, " columns", 0);
103 pParse->nErr++;
104 goto insert_cleanup;
105 }
106 if( pField ){
107 for(i=0; i<pField->nId; i++){
108 pField->a[i].idx = -1;
109 }
110 for(i=0; i<pField->nId; i++){
111 for(j=0; j<pTab->nCol; j++){
drh7020f652000-06-03 18:06:52 +0000112 if( sqliteStrICmp(pField->a[i].zName, pTab->aCol[j].zName)==0 ){
drhcce7d172000-05-31 15:34:51 +0000113 pField->a[i].idx = j;
114 break;
115 }
116 }
117 if( j>=pTab->nCol ){
118 sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName,
119 " has no column named ", pField->a[i].zName, 0);
120 pParse->nErr++;
121 goto insert_cleanup;
122 }
123 }
124 }
drh5974a302000-06-07 14:42:26 +0000125 base = pParse->nTab;
126 sqliteVdbeAddOp(v, OP_Open, base, 1, pTab->zName, 0);
127 for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
128 sqliteVdbeAddOp(v, OP_Open, idx+base, 1, pIdx->zName, 0);
drh19a775c2000-06-05 18:54:46 +0000129 }
drh5974a302000-06-07 14:42:26 +0000130 if( srcTab>=0 ){
131 sqliteVdbeAddOp(v, OP_Rewind, srcTab, 0, 0, 0);
132 iBreak = sqliteVdbeMakeLabel(v);
133 iCont = sqliteVdbeAddOp(v, OP_Next, srcTab, iBreak, 0, 0);
134 }
135 sqliteVdbeAddOp(v, OP_New, 0, 0, 0, 0);
136 if( pTab->pIndex ){
137 sqliteVdbeAddOp(v, OP_Dup, 0, 0, 0, 0);
138 }
139 for(i=0; i<pTab->nCol; i++){
140 if( pField==0 ){
141 j = i;
142 }else{
143 for(j=0; j<pField->nId; j++){
144 if( pField->a[j].idx==i ) break;
145 }
drhbed86902000-06-02 13:27:59 +0000146 }
drh5974a302000-06-07 14:42:26 +0000147 if( pField && j>=pField->nId ){
148 char *zDflt = pTab->aCol[i].zDflt;
149 if( zDflt==0 ){
150 sqliteVdbeAddOp(v, OP_Null, 0, 0, 0, 0);
151 }else{
152 sqliteVdbeAddOp(v, OP_String, 0, 0, zDflt, 0);
153 }
154 }else if( srcTab>=0 ){
155 sqliteVdbeAddOp(v, OP_Field, srcTab, i, 0, 0);
156 }else{
157 sqliteExprCode(pParse, pList->a[j].pExpr);
158 }
159 }
160 sqliteVdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0, 0, 0);
161 sqliteVdbeAddOp(v, OP_Put, base, 0, 0, 0);
162 /* sqliteVdbeAddOp(v, OP_Close, 0, 0, 0, 0); */
163 for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
164 if( pIdx->pNext ){
drhcce7d172000-05-31 15:34:51 +0000165 sqliteVdbeAddOp(v, OP_Dup, 0, 0, 0, 0);
166 }
drh5974a302000-06-07 14:42:26 +0000167 for(i=0; i<pIdx->nField; i++){
168 int idx = pIdx->aiField[i];
drhcce7d172000-05-31 15:34:51 +0000169 if( pField==0 ){
drh5974a302000-06-07 14:42:26 +0000170 j = idx;
drhcce7d172000-05-31 15:34:51 +0000171 }else{
172 for(j=0; j<pField->nId; j++){
drh5974a302000-06-07 14:42:26 +0000173 if( pField->a[j].idx==idx ) break;
drhcce7d172000-05-31 15:34:51 +0000174 }
175 }
176 if( pField && j>=pField->nId ){
drh5974a302000-06-07 14:42:26 +0000177 char *zDflt = pTab->aCol[idx].zDflt;
drhc61053b2000-06-04 12:58:36 +0000178 if( zDflt==0 ){
179 sqliteVdbeAddOp(v, OP_Null, 0, 0, 0, 0);
180 }else{
181 sqliteVdbeAddOp(v, OP_String, 0, 0, zDflt, 0);
182 }
drh5974a302000-06-07 14:42:26 +0000183 }else if( srcTab>=0 ){
drhcc85b412000-06-07 15:11:27 +0000184 sqliteVdbeAddOp(v, OP_Field, srcTab, idx, 0, 0);
drhcce7d172000-05-31 15:34:51 +0000185 }else{
186 sqliteExprCode(pParse, pList->a[j].pExpr);
187 }
188 }
drh5974a302000-06-07 14:42:26 +0000189 sqliteVdbeAddOp(v, OP_MakeKey, pIdx->nField, 0, 0, 0);
190 sqliteVdbeAddOp(v, OP_PutIdx, idx+base, 0, 0, 0);
191 /* sqliteVdbeAddOp(v, OP_Close, idx, 0, 0, 0); */
192 }
193 if( srcTab>=0 ){
194 sqliteVdbeAddOp(v, OP_Goto, 0, iCont, 0, 0);
195 sqliteVdbeAddOp(v, OP_Noop, 0, 0, 0, iBreak);
drhcce7d172000-05-31 15:34:51 +0000196 }
197
198insert_cleanup:
drh5974a302000-06-07 14:42:26 +0000199 if( pList ) sqliteExprListDelete(pList);
200 if( pSelect ) sqliteSelectDelete(pSelect);
drhcce7d172000-05-31 15:34:51 +0000201 sqliteIdListDelete(pField);
202}