1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
|
/*
** 2001 September 15
**
** The author disclaims copyright to this source code. In place of
** a legal notice, here is a blessing:
**
** May you do good and not evil.
** May you find forgiveness for yourself and forgive others.
** May you share freely, never taking more than you give.
**
*************************************************************************
** This file contains C code routines that are called by the parser
** to handle INSERT statements in SQLite.
**
** $Id: insert.c,v 1.18 2001/09/16 00:13:27 drh Exp $
*/
#include "sqliteInt.h"
/*
** This routine is call to handle SQL of the following forms:
**
** insert into TABLE (IDLIST) values(EXPRLIST)
** insert into TABLE (IDLIST) select
**
** The IDLIST following the table name is always optional. If omitted,
** then a list of all columns for the table is substituted. The IDLIST
** appears in the pColumn parameter. pColumn is NULL if IDLIST is omitted.
**
** The pList parameter holds EXPRLIST in the first form of the INSERT
** statement above, and pSelect is NULL. For the second form, pList is
** NULL and pSelect is a pointer to the select statement used to generate
** data for the insert.
*/
void sqliteInsert(
Parse *pParse, /* Parser context */
Token *pTableName, /* Name of table into which we are inserting */
ExprList *pList, /* List of values to be inserted */
Select *pSelect, /* A SELECT statement to use as the data source */
IdList *pColumn /* Column names corresponding to IDLIST. */
){
Table *pTab; /* The table to insert into */
char *zTab; /* Name of the table into which we are inserting */
int i, j, idx; /* Loop counters */
Vdbe *v; /* Generate code into this virtual machine */
Index *pIdx; /* For looping over indices of the table */
int srcTab; /* Date comes from this temporary cursor if >=0 */
int nColumn; /* Number of columns in the data */
int base; /* First available cursor */
int iCont, iBreak; /* Beginning and end of the loop over srcTab */
if( pParse->nErr || sqlite_malloc_failed ) goto insert_cleanup;
/* Locate the table into which we will be inserting new information.
*/
zTab = sqliteTableNameFromToken(pTableName);
if( zTab==0 ) goto insert_cleanup;
pTab = sqliteFindTable(pParse->db, zTab);
sqliteFree(zTab);
if( pTab==0 ){
sqliteSetNString(&pParse->zErrMsg, "no such table: ", 0,
pTableName->z, pTableName->n, 0);
pParse->nErr++;
goto insert_cleanup;
}
if( pTab->readOnly ){
sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName,
" may not be modified", 0);
pParse->nErr++;
goto insert_cleanup;
}
/* Allocate a VDBE
*/
v = sqliteGetVdbe(pParse);
if( v==0 ) goto insert_cleanup;
if( (pParse->db->flags & SQLITE_InTrans)==0 ){
sqliteVdbeAddOp(v, OP_Transaction, 0, 0, 0, 0);
sqliteVdbeAddOp(v, OP_VerifyCookie, pParse->db->schema_cookie, 0, 0, 0);
}
/* Figure out how many columns of data are supplied. If the data
** is comming from a SELECT statement, then this step has to generate
** all the code to implement the SELECT statement and leave the data
** in a temporary table. If data is coming from an expression list,
** then we just have to count the number of expressions.
*/
if( pSelect ){
int rc;
srcTab = pParse->nTab++;
sqliteVdbeAddOp(v, OP_OpenTemp, srcTab, 0, 0, 0);
rc = sqliteSelect(pParse, pSelect, SRT_Table, srcTab);
if( rc || pParse->nErr || sqlite_malloc_failed ) goto insert_cleanup;
assert( pSelect->pEList );
nColumn = pSelect->pEList->nExpr;
}else{
assert( pList!=0 );
srcTab = -1;
assert( pList );
nColumn = pList->nExpr;
}
/* Make sure the number of columns in the source data matches the number
** of columns to be inserted into the table.
*/
if( pColumn==0 && nColumn!=pTab->nCol ){
char zNum1[30];
char zNum2[30];
sprintf(zNum1,"%d", nColumn);
sprintf(zNum2,"%d", pTab->nCol);
sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName,
" has ", zNum2, " columns but ",
zNum1, " values were supplied", 0);
pParse->nErr++;
goto insert_cleanup;
}
if( pColumn!=0 && nColumn!=pColumn->nId ){
char zNum1[30];
char zNum2[30];
sprintf(zNum1,"%d", nColumn);
sprintf(zNum2,"%d", pColumn->nId);
sqliteSetString(&pParse->zErrMsg, zNum1, " values for ",
zNum2, " columns", 0);
pParse->nErr++;
goto insert_cleanup;
}
/* If the INSERT statement included an IDLIST term, then make sure
** all elements of the IDLIST really are columns of the table and
** remember the column indices.
*/
if( pColumn ){
for(i=0; i<pColumn->nId; i++){
pColumn->a[i].idx = -1;
}
for(i=0; i<pColumn->nId; i++){
for(j=0; j<pTab->nCol; j++){
if( sqliteStrICmp(pColumn->a[i].zName, pTab->aCol[j].zName)==0 ){
pColumn->a[i].idx = j;
break;
}
}
if( j>=pTab->nCol ){
sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName,
" has no column named ", pColumn->a[i].zName, 0);
pParse->nErr++;
goto insert_cleanup;
}
}
}
/* Open cursors into the table that is received the new data and
** all indices of that table.
*/
base = pParse->nTab;
sqliteVdbeAddOp(v, OP_Open, base, pTab->tnum, pTab->zName, 0);
for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
sqliteVdbeAddOp(v, OP_Open, idx+base, pIdx->tnum, pIdx->zName, 0);
}
/* If the data source is a SELECT statement, then we have to create
** a loop because there might be multiple rows of data. If the data
** source is an expression list, then exactly one row will be inserted
** and the loop is not used.
*/
if( srcTab>=0 ){
sqliteVdbeAddOp(v, OP_Rewind, srcTab, 0, 0, 0);
iBreak = sqliteVdbeMakeLabel(v);
iCont = sqliteVdbeAddOp(v, OP_Next, srcTab, iBreak, 0, 0);
}
/* Create a new entry in the table and fill it with data.
*/
sqliteVdbeAddOp(v, OP_NewRecno, base, 0, 0, 0);
if( pTab->pIndex ){
sqliteVdbeAddOp(v, OP_Dup, 0, 0, 0, 0);
}
for(i=0; i<pTab->nCol; i++){
if( pColumn==0 ){
j = i;
}else{
for(j=0; j<pColumn->nId; j++){
if( pColumn->a[j].idx==i ) break;
}
}
if( pColumn && j>=pColumn->nId ){
char *zDflt = pTab->aCol[i].zDflt;
if( zDflt==0 ){
sqliteVdbeAddOp(v, OP_Null, 0, 0, 0, 0);
}else{
sqliteVdbeAddOp(v, OP_String, 0, 0, zDflt, 0);
}
}else if( srcTab>=0 ){
sqliteVdbeAddOp(v, OP_Column, srcTab, i, 0, 0);
}else{
sqliteExprCode(pParse, pList->a[j].pExpr);
}
}
sqliteVdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0, 0, 0);
sqliteVdbeAddOp(v, OP_Put, base, 0, 0, 0);
/* Create appropriate entries for the new data row in all indices
** of the table.
*/
for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
if( pIdx->pNext ){
sqliteVdbeAddOp(v, OP_Dup, 0, 0, 0, 0);
}
for(i=0; i<pIdx->nColumn; i++){
int idx = pIdx->aiColumn[i];
if( pColumn==0 ){
j = idx;
}else{
for(j=0; j<pColumn->nId; j++){
if( pColumn->a[j].idx==idx ) break;
}
}
if( pColumn && j>=pColumn->nId ){
char *zDflt = pTab->aCol[idx].zDflt;
if( zDflt==0 ){
sqliteVdbeAddOp(v, OP_Null, 0, 0, 0, 0);
}else{
sqliteVdbeAddOp(v, OP_String, 0, 0, zDflt, 0);
}
}else if( srcTab>=0 ){
sqliteVdbeAddOp(v, OP_Column, srcTab, idx, 0, 0);
}else{
sqliteExprCode(pParse, pList->a[j].pExpr);
}
}
sqliteVdbeAddOp(v, OP_MakeIdxKey, pIdx->nColumn, 0, 0, 0);
sqliteVdbeAddOp(v, OP_PutIdx, idx+base, 0, 0, 0);
}
/* The bottom of the loop, if the data source is a SELECT statement
*/
if( srcTab>=0 ){
sqliteVdbeAddOp(v, OP_Goto, 0, iCont, 0, 0);
sqliteVdbeAddOp(v, OP_Noop, 0, 0, 0, iBreak);
}
if( (pParse->db->flags & SQLITE_InTrans)==0 ){
sqliteVdbeAddOp(v, OP_Commit, 0, 0, 0, 0);
}
insert_cleanup:
if( pList ) sqliteExprListDelete(pList);
if( pSelect ) sqliteSelectDelete(pSelect);
sqliteIdListDelete(pColumn);
}
|