blob: 112ba3386cc3383af5d7b29d3df95423541b9c91 [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**
drh19a775c2000-06-05 18:54:46 +000027** $Id: insert.c,v 1.6 2000/06/05 18:54:46 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 */
42 IdList *pField /* Field name corresponding to pList. Might be NULL */
43){
44 Table *pTab;
45 char *zTab;
drhbed86902000-06-02 13:27:59 +000046 int i, j, idx;
drhcce7d172000-05-31 15:34:51 +000047 Vdbe *v;
48
49 zTab = sqliteTableNameFromToken(pTableName);
50 pTab = sqliteFindTable(pParse->db, zTab);
51 sqliteFree(zTab);
52 if( pTab==0 ){
53 sqliteSetNString(&pParse->zErrMsg, "no such table: ", 0,
54 pTableName->z, pTableName->n, 0);
55 pParse->nErr++;
56 goto insert_cleanup;
57 }
58 if( pTab->readOnly ){
59 sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName,
60 " may not be modified", 0);
61 pParse->nErr++;
62 goto insert_cleanup;
63 }
64 if( pField==0 && pList->nExpr!=pTab->nCol ){
65 char zNum1[30];
66 char zNum2[30];
67 sprintf(zNum1,"%d", pList->nExpr);
68 sprintf(zNum2,"%d", pTab->nCol);
69 sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName,
70 " has ", zNum2, " columns but ",
71 zNum1, " values were supplied", 0);
72 pParse->nErr++;
73 goto insert_cleanup;
74 }
75 if( pField!=0 && pList->nExpr!=pField->nId ){
76 char zNum1[30];
77 char zNum2[30];
78 sprintf(zNum1,"%d", pList->nExpr);
79 sprintf(zNum2,"%d", pField->nId);
80 sqliteSetString(&pParse->zErrMsg, zNum1, " values for ",
81 zNum2, " columns", 0);
82 pParse->nErr++;
83 goto insert_cleanup;
84 }
85 if( pField ){
86 for(i=0; i<pField->nId; i++){
87 pField->a[i].idx = -1;
88 }
89 for(i=0; i<pField->nId; i++){
90 for(j=0; j<pTab->nCol; j++){
drh7020f652000-06-03 18:06:52 +000091 if( sqliteStrICmp(pField->a[i].zName, pTab->aCol[j].zName)==0 ){
drhcce7d172000-05-31 15:34:51 +000092 pField->a[i].idx = j;
93 break;
94 }
95 }
96 if( j>=pTab->nCol ){
97 sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName,
98 " has no column named ", pField->a[i].zName, 0);
99 pParse->nErr++;
100 goto insert_cleanup;
101 }
102 }
103 }
drh19a775c2000-06-05 18:54:46 +0000104 v = pParse->pVdbe;
105 if( v==0 ){
106 v = pParse->pVdbe = sqliteVdbeCreate(pParse->db->pBe);
107 }
drhcce7d172000-05-31 15:34:51 +0000108 if( v ){
109 Index *pIdx;
drh58b95762000-06-02 01:17:37 +0000110 sqliteVdbeAddOp(v, OP_Open, 0, 1, pTab->zName, 0);
drhbed86902000-06-02 13:27:59 +0000111 for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
112 sqliteVdbeAddOp(v, OP_Open, idx, 1, pIdx->zName, 0);
113 }
drhcce7d172000-05-31 15:34:51 +0000114 sqliteVdbeAddOp(v, OP_New, 0, 0, 0, 0);
115 if( pTab->pIndex ){
116 sqliteVdbeAddOp(v, OP_Dup, 0, 0, 0, 0);
117 }
118 for(i=0; i<pTab->nCol; i++){
119 if( pField==0 ){
120 j = i;
121 }else{
122 for(j=0; j<pField->nId; j++){
123 if( pField->a[j].idx==i ) break;
124 }
125 }
126 if( pField && j>=pField->nId ){
drh7020f652000-06-03 18:06:52 +0000127 char *zDflt = pTab->aCol[i].zDflt;
drhc61053b2000-06-04 12:58:36 +0000128 if( zDflt==0 ){
129 sqliteVdbeAddOp(v, OP_Null, 0, 0, 0, 0);
130 }else{
131 sqliteVdbeAddOp(v, OP_String, 0, 0, zDflt, 0);
132 }
drhcce7d172000-05-31 15:34:51 +0000133 }else{
134 sqliteExprCode(pParse, pList->a[j].pExpr);
135 }
136 }
137 sqliteVdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0, 0, 0);
138 sqliteVdbeAddOp(v, OP_Put, 0, 0, 0, 0);
139 sqliteVdbeAddOp(v, OP_Close, 0, 0, 0, 0);
drhbed86902000-06-02 13:27:59 +0000140 for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
drhcce7d172000-05-31 15:34:51 +0000141 if( pIdx->pNext ){
142 sqliteVdbeAddOp(v, OP_Dup, 0, 0, 0, 0);
143 }
drhcce7d172000-05-31 15:34:51 +0000144 for(i=0; i<pIdx->nField; i++){
145 int idx = pIdx->aiField[i];
146 if( pField==0 ){
147 j = idx;
148 }else{
149 for(j=0; j<pField->nId; j++){
150 if( pField->a[j].idx==idx ) break;
151 }
152 }
153 if( pField && j>=pField->nId ){
drh7020f652000-06-03 18:06:52 +0000154 char *zDflt = pTab->aCol[idx].zDflt;
drhc61053b2000-06-04 12:58:36 +0000155 if( zDflt==0 ){
156 sqliteVdbeAddOp(v, OP_Null, 0, 0, 0, 0);
157 }else{
158 sqliteVdbeAddOp(v, OP_String, 0, 0, zDflt, 0);
159 }
drhcce7d172000-05-31 15:34:51 +0000160 }else{
161 sqliteExprCode(pParse, pList->a[j].pExpr);
162 }
163 }
164 sqliteVdbeAddOp(v, OP_MakeKey, pIdx->nField, 0, 0, 0);
drhbed86902000-06-02 13:27:59 +0000165 sqliteVdbeAddOp(v, OP_PutIdx, idx, 0, 0, 0);
166 sqliteVdbeAddOp(v, OP_Close, idx, 0, 0, 0);
drhcce7d172000-05-31 15:34:51 +0000167 }
168 }
169
170insert_cleanup:
171 sqliteExprListDelete(pList);
172 sqliteIdListDelete(pField);
173}