blob: 24fcff6705b5cc632e09e7988b16e7a33669a7f6 [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**
drhc61053b2000-06-04 12:58:36 +000027** $Id: insert.c,v 1.5 2000/06/04 12:58:38 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 }
104 v = pParse->pVdbe = sqliteVdbeCreate(pParse->db->pBe);
105 if( v ){
106 Index *pIdx;
drh58b95762000-06-02 01:17:37 +0000107 sqliteVdbeAddOp(v, OP_Open, 0, 1, pTab->zName, 0);
drhbed86902000-06-02 13:27:59 +0000108 for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
109 sqliteVdbeAddOp(v, OP_Open, idx, 1, pIdx->zName, 0);
110 }
drhcce7d172000-05-31 15:34:51 +0000111 sqliteVdbeAddOp(v, OP_New, 0, 0, 0, 0);
112 if( pTab->pIndex ){
113 sqliteVdbeAddOp(v, OP_Dup, 0, 0, 0, 0);
114 }
115 for(i=0; i<pTab->nCol; i++){
116 if( pField==0 ){
117 j = i;
118 }else{
119 for(j=0; j<pField->nId; j++){
120 if( pField->a[j].idx==i ) break;
121 }
122 }
123 if( pField && j>=pField->nId ){
drh7020f652000-06-03 18:06:52 +0000124 char *zDflt = pTab->aCol[i].zDflt;
drhc61053b2000-06-04 12:58:36 +0000125 if( zDflt==0 ){
126 sqliteVdbeAddOp(v, OP_Null, 0, 0, 0, 0);
127 }else{
128 sqliteVdbeAddOp(v, OP_String, 0, 0, zDflt, 0);
129 }
drhcce7d172000-05-31 15:34:51 +0000130 }else{
131 sqliteExprCode(pParse, pList->a[j].pExpr);
132 }
133 }
134 sqliteVdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0, 0, 0);
135 sqliteVdbeAddOp(v, OP_Put, 0, 0, 0, 0);
136 sqliteVdbeAddOp(v, OP_Close, 0, 0, 0, 0);
drhbed86902000-06-02 13:27:59 +0000137 for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
drhcce7d172000-05-31 15:34:51 +0000138 if( pIdx->pNext ){
139 sqliteVdbeAddOp(v, OP_Dup, 0, 0, 0, 0);
140 }
drhcce7d172000-05-31 15:34:51 +0000141 for(i=0; i<pIdx->nField; i++){
142 int idx = pIdx->aiField[i];
143 if( pField==0 ){
144 j = idx;
145 }else{
146 for(j=0; j<pField->nId; j++){
147 if( pField->a[j].idx==idx ) break;
148 }
149 }
150 if( pField && j>=pField->nId ){
drh7020f652000-06-03 18:06:52 +0000151 char *zDflt = pTab->aCol[idx].zDflt;
drhc61053b2000-06-04 12:58:36 +0000152 if( zDflt==0 ){
153 sqliteVdbeAddOp(v, OP_Null, 0, 0, 0, 0);
154 }else{
155 sqliteVdbeAddOp(v, OP_String, 0, 0, zDflt, 0);
156 }
drhcce7d172000-05-31 15:34:51 +0000157 }else{
158 sqliteExprCode(pParse, pList->a[j].pExpr);
159 }
160 }
161 sqliteVdbeAddOp(v, OP_MakeKey, pIdx->nField, 0, 0, 0);
drhbed86902000-06-02 13:27:59 +0000162 sqliteVdbeAddOp(v, OP_PutIdx, idx, 0, 0, 0);
163 sqliteVdbeAddOp(v, OP_Close, idx, 0, 0, 0);
drhcce7d172000-05-31 15:34:51 +0000164 }
165 }
166
167insert_cleanup:
168 sqliteExprListDelete(pList);
169 sqliteIdListDelete(pField);
170}