blob: 071e642c7db14ea299fb3568fa05442c6972d1e6 [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**
27** $Id: insert.c,v 1.1 2000/05/31 15:34:53 drh Exp $
28*/
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;
46 int i, j;
47 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++){
91 if( sqliteStrICmp(pField->a[i].zName, pTab->azCol[j])==0 ){
92 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;
107 sqliteVdbeAddOp(v, OP_Open, 0, 0, pTab->zName, 0);
108 sqliteVdbeAddOp(v, OP_New, 0, 0, 0, 0);
109 if( pTab->pIndex ){
110 sqliteVdbeAddOp(v, OP_Dup, 0, 0, 0, 0);
111 }
112 for(i=0; i<pTab->nCol; i++){
113 if( pField==0 ){
114 j = i;
115 }else{
116 for(j=0; j<pField->nId; j++){
117 if( pField->a[j].idx==i ) break;
118 }
119 }
120 if( pField && j>=pField->nId ){
121 sqliteVdbeAddOp(v, OP_String, 0, 0, "", 0);
122 }else{
123 sqliteExprCode(pParse, pList->a[j].pExpr);
124 }
125 }
126 sqliteVdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0, 0, 0);
127 sqliteVdbeAddOp(v, OP_Put, 0, 0, 0, 0);
128 sqliteVdbeAddOp(v, OP_Close, 0, 0, 0, 0);
129 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
130 if( pIdx->pNext ){
131 sqliteVdbeAddOp(v, OP_Dup, 0, 0, 0, 0);
132 }
133 sqliteVdbeAddOp(v, OP_Open, 0, 0, pIdx->zName, 0);
134 for(i=0; i<pIdx->nField; i++){
135 int idx = pIdx->aiField[i];
136 if( pField==0 ){
137 j = idx;
138 }else{
139 for(j=0; j<pField->nId; j++){
140 if( pField->a[j].idx==idx ) break;
141 }
142 }
143 if( pField && j>=pField->nId ){
144 sqliteVdbeAddOp(v, OP_String, 0, 0, "", 0);
145 }else{
146 sqliteExprCode(pParse, pList->a[j].pExpr);
147 }
148 }
149 sqliteVdbeAddOp(v, OP_MakeKey, pIdx->nField, 0, 0, 0);
150 sqliteVdbeAddOp(v, OP_PutIdx, 0, 0, 0, 0);
151 sqliteVdbeAddOp(v, OP_Close, 0, 0, 0, 0);
152 }
153 }
154
155insert_cleanup:
156 sqliteExprListDelete(pList);
157 sqliteIdListDelete(pField);
158}