xref: /sqlite-3.40.0/src/insert.c (revision 345fda3e)
1cce7d176Sdrh /*
2cce7d176Sdrh ** Copyright (c) 1999, 2000 D. Richard Hipp
3cce7d176Sdrh **
4cce7d176Sdrh ** This program is free software; you can redistribute it and/or
5cce7d176Sdrh ** modify it under the terms of the GNU General Public
6cce7d176Sdrh ** License as published by the Free Software Foundation; either
7cce7d176Sdrh ** version 2 of the License, or (at your option) any later version.
8cce7d176Sdrh **
9cce7d176Sdrh ** This program is distributed in the hope that it will be useful,
10cce7d176Sdrh ** but WITHOUT ANY WARRANTY; without even the implied warranty of
11cce7d176Sdrh ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12cce7d176Sdrh ** General Public License for more details.
13cce7d176Sdrh **
14cce7d176Sdrh ** You should have received a copy of the GNU General Public
15cce7d176Sdrh ** License along with this library; if not, write to the
16cce7d176Sdrh ** Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17cce7d176Sdrh ** Boston, MA  02111-1307, USA.
18cce7d176Sdrh **
19cce7d176Sdrh ** Author contact information:
20cce7d176Sdrh **   [email protected]
21cce7d176Sdrh **   http://www.hwaci.com/drh/
22cce7d176Sdrh **
23cce7d176Sdrh *************************************************************************
24cce7d176Sdrh ** This file contains C code routines that are called by the parser
25cce7d176Sdrh ** to handle INSERT statements.
26cce7d176Sdrh **
27*345fda3eSdrh ** $Id: insert.c,v 1.12 2001/01/15 22:51:11 drh Exp $
28cce7d176Sdrh */
29cce7d176Sdrh #include "sqliteInt.h"
30cce7d176Sdrh 
31cce7d176Sdrh /*
321ccde15dSdrh ** This routine is call to handle SQL of the following forms:
33cce7d176Sdrh **
34cce7d176Sdrh **    insert into TABLE (IDLIST) values(EXPRLIST)
351ccde15dSdrh **    insert into TABLE (IDLIST) select
36cce7d176Sdrh **
371ccde15dSdrh ** The IDLIST following the table name is always optional.  If omitted,
381ccde15dSdrh ** then a list of all columns for the table is substituted.  The IDLIST
39967e8b73Sdrh ** appears in the pColumn parameter.  pColumn is NULL if IDLIST is omitted.
401ccde15dSdrh **
411ccde15dSdrh ** The pList parameter holds EXPRLIST in the first form of the INSERT
421ccde15dSdrh ** statement above, and pSelect is NULL.  For the second form, pList is
431ccde15dSdrh ** NULL and pSelect is a pointer to the select statement used to generate
441ccde15dSdrh ** data for the insert.
45cce7d176Sdrh */
46cce7d176Sdrh void sqliteInsert(
47cce7d176Sdrh   Parse *pParse,        /* Parser context */
48cce7d176Sdrh   Token *pTableName,    /* Name of table into which we are inserting */
49cce7d176Sdrh   ExprList *pList,      /* List of values to be inserted */
505974a30fSdrh   Select *pSelect,      /* A SELECT statement to use as the data source */
51967e8b73Sdrh   IdList *pColumn       /* Column names corresponding to IDLIST. */
52cce7d176Sdrh ){
535974a30fSdrh   Table *pTab;          /* The table to insert into */
545974a30fSdrh   char *zTab;           /* Name of the table into which we are inserting */
555974a30fSdrh   int i, j, idx;        /* Loop counters */
565974a30fSdrh   Vdbe *v;              /* Generate code into this virtual machine */
575974a30fSdrh   Index *pIdx;          /* For looping over indices of the table */
585974a30fSdrh   int srcTab;           /* Date comes from this temporary cursor if >=0 */
59967e8b73Sdrh   int nColumn;          /* Number of columns in the data */
605974a30fSdrh   int base;             /* First available cursor */
615974a30fSdrh   int iCont, iBreak;    /* Beginning and end of the loop over srcTab */
62cce7d176Sdrh 
631ccde15dSdrh   /* Locate the table into which we will be inserting new information.
641ccde15dSdrh   */
65cce7d176Sdrh   zTab = sqliteTableNameFromToken(pTableName);
66cce7d176Sdrh   pTab = sqliteFindTable(pParse->db, zTab);
67cce7d176Sdrh   sqliteFree(zTab);
68cce7d176Sdrh   if( pTab==0 ){
69cce7d176Sdrh     sqliteSetNString(&pParse->zErrMsg, "no such table: ", 0,
70cce7d176Sdrh         pTableName->z, pTableName->n, 0);
71cce7d176Sdrh     pParse->nErr++;
72cce7d176Sdrh     goto insert_cleanup;
73cce7d176Sdrh   }
74cce7d176Sdrh   if( pTab->readOnly ){
75cce7d176Sdrh     sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName,
76cce7d176Sdrh         " may not be modified", 0);
77cce7d176Sdrh     pParse->nErr++;
78cce7d176Sdrh     goto insert_cleanup;
79cce7d176Sdrh   }
801ccde15dSdrh 
811ccde15dSdrh   /* Allocate a VDBE
821ccde15dSdrh   */
83d8bc7086Sdrh   v = sqliteGetVdbe(pParse);
845974a30fSdrh   if( v==0 ) goto insert_cleanup;
851ccde15dSdrh 
861ccde15dSdrh   /* Figure out how many columns of data are supplied.  If the data
871ccde15dSdrh   ** is comming from a SELECT statement, then this step has to generate
881ccde15dSdrh   ** all the code to implement the SELECT statement and leave the data
891ccde15dSdrh   ** in a temporary table.  If data is coming from an expression list,
901ccde15dSdrh   ** then we just have to count the number of expressions.
911ccde15dSdrh   */
925974a30fSdrh   if( pSelect ){
935974a30fSdrh     int rc;
945974a30fSdrh     srcTab = pParse->nTab++;
95*345fda3eSdrh     sqliteVdbeAddOp(v, OP_OpenTbl, srcTab, 1, 0, 0);
965974a30fSdrh     rc = sqliteSelect(pParse, pSelect, SRT_Table, srcTab);
975974a30fSdrh     if( rc ) goto insert_cleanup;
985974a30fSdrh     assert( pSelect->pEList );
99967e8b73Sdrh     nColumn = pSelect->pEList->nExpr;
1005974a30fSdrh   }else{
1015974a30fSdrh     srcTab = -1;
1025974a30fSdrh     assert( pList );
103967e8b73Sdrh     nColumn = pList->nExpr;
1045974a30fSdrh   }
1051ccde15dSdrh 
1061ccde15dSdrh   /* Make sure the number of columns in the source data matches the number
1071ccde15dSdrh   ** of columns to be inserted into the table.
1081ccde15dSdrh   */
109967e8b73Sdrh   if( pColumn==0 && nColumn!=pTab->nCol ){
110cce7d176Sdrh     char zNum1[30];
111cce7d176Sdrh     char zNum2[30];
112967e8b73Sdrh     sprintf(zNum1,"%d", nColumn);
113cce7d176Sdrh     sprintf(zNum2,"%d", pTab->nCol);
114cce7d176Sdrh     sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName,
115cce7d176Sdrh        " has ", zNum2, " columns but ",
116cce7d176Sdrh        zNum1, " values were supplied", 0);
117cce7d176Sdrh     pParse->nErr++;
118cce7d176Sdrh     goto insert_cleanup;
119cce7d176Sdrh   }
120967e8b73Sdrh   if( pColumn!=0 && nColumn!=pColumn->nId ){
121cce7d176Sdrh     char zNum1[30];
122cce7d176Sdrh     char zNum2[30];
123967e8b73Sdrh     sprintf(zNum1,"%d", nColumn);
124967e8b73Sdrh     sprintf(zNum2,"%d", pColumn->nId);
125cce7d176Sdrh     sqliteSetString(&pParse->zErrMsg, zNum1, " values for ",
126cce7d176Sdrh        zNum2, " columns", 0);
127cce7d176Sdrh     pParse->nErr++;
128cce7d176Sdrh     goto insert_cleanup;
129cce7d176Sdrh   }
1301ccde15dSdrh 
1311ccde15dSdrh   /* If the INSERT statement included an IDLIST term, then make sure
1321ccde15dSdrh   ** all elements of the IDLIST really are columns of the table and
1331ccde15dSdrh   ** remember the column indices.
1341ccde15dSdrh   */
135967e8b73Sdrh   if( pColumn ){
136967e8b73Sdrh     for(i=0; i<pColumn->nId; i++){
137967e8b73Sdrh       pColumn->a[i].idx = -1;
138cce7d176Sdrh     }
139967e8b73Sdrh     for(i=0; i<pColumn->nId; i++){
140cce7d176Sdrh       for(j=0; j<pTab->nCol; j++){
141967e8b73Sdrh         if( sqliteStrICmp(pColumn->a[i].zName, pTab->aCol[j].zName)==0 ){
142967e8b73Sdrh           pColumn->a[i].idx = j;
143cce7d176Sdrh           break;
144cce7d176Sdrh         }
145cce7d176Sdrh       }
146cce7d176Sdrh       if( j>=pTab->nCol ){
147cce7d176Sdrh         sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName,
148967e8b73Sdrh            " has no column named ", pColumn->a[i].zName, 0);
149cce7d176Sdrh         pParse->nErr++;
150cce7d176Sdrh         goto insert_cleanup;
151cce7d176Sdrh       }
152cce7d176Sdrh     }
153cce7d176Sdrh   }
1541ccde15dSdrh 
1551ccde15dSdrh   /* Open cursors into the table that is received the new data and
1561ccde15dSdrh   ** all indices of that table.
1571ccde15dSdrh   */
1585974a30fSdrh   base = pParse->nTab;
159*345fda3eSdrh   sqliteVdbeAddOp(v, OP_OpenTbl, base, 1, pTab->zName, 0);
160bed8690fSdrh   for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
161*345fda3eSdrh     sqliteVdbeAddOp(v, OP_OpenIdx, idx+base, 1, pIdx->zName, 0);
1625974a30fSdrh   }
1631ccde15dSdrh 
1641ccde15dSdrh   /* If the data source is a SELECT statement, then we have to create
1651ccde15dSdrh   ** a loop because there might be multiple rows of data.  If the data
1661ccde15dSdrh   ** source is an expression list, then exactly one row will be inserted
1671ccde15dSdrh   ** and the loop is not used.
1681ccde15dSdrh   */
1695974a30fSdrh   if( srcTab>=0 ){
1705974a30fSdrh     sqliteVdbeAddOp(v, OP_Rewind, srcTab, 0, 0, 0);
1715974a30fSdrh     iBreak = sqliteVdbeMakeLabel(v);
1725974a30fSdrh     iCont = sqliteVdbeAddOp(v, OP_Next, srcTab, iBreak, 0, 0);
173bed8690fSdrh   }
1741ccde15dSdrh 
1751ccde15dSdrh   /* Create a new entry in the table and fill it with data.
1761ccde15dSdrh   */
177cce7d176Sdrh   sqliteVdbeAddOp(v, OP_New, 0, 0, 0, 0);
178cce7d176Sdrh   if( pTab->pIndex ){
179cce7d176Sdrh     sqliteVdbeAddOp(v, OP_Dup, 0, 0, 0, 0);
180cce7d176Sdrh   }
181cce7d176Sdrh   for(i=0; i<pTab->nCol; i++){
182967e8b73Sdrh     if( pColumn==0 ){
183cce7d176Sdrh       j = i;
184cce7d176Sdrh     }else{
185967e8b73Sdrh       for(j=0; j<pColumn->nId; j++){
186967e8b73Sdrh         if( pColumn->a[j].idx==i ) break;
187cce7d176Sdrh       }
188cce7d176Sdrh     }
189967e8b73Sdrh     if( pColumn && j>=pColumn->nId ){
1907020f651Sdrh       char *zDflt = pTab->aCol[i].zDflt;
191c61053b7Sdrh       if( zDflt==0 ){
192c61053b7Sdrh         sqliteVdbeAddOp(v, OP_Null, 0, 0, 0, 0);
193c61053b7Sdrh       }else{
1947020f651Sdrh         sqliteVdbeAddOp(v, OP_String, 0, 0, zDflt, 0);
195c61053b7Sdrh       }
1965974a30fSdrh     }else if( srcTab>=0 ){
1975974a30fSdrh       sqliteVdbeAddOp(v, OP_Field, srcTab, i, 0, 0);
198cce7d176Sdrh     }else{
199cce7d176Sdrh       sqliteExprCode(pParse, pList->a[j].pExpr);
200cce7d176Sdrh     }
201cce7d176Sdrh   }
202cce7d176Sdrh   sqliteVdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0, 0, 0);
2035974a30fSdrh   sqliteVdbeAddOp(v, OP_Put, base, 0, 0, 0);
2041ccde15dSdrh 
2051ccde15dSdrh   /* Create appropriate entries for the new data row in all indices
2061ccde15dSdrh   ** of the table.
2071ccde15dSdrh   */
208bed8690fSdrh   for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
209cce7d176Sdrh     if( pIdx->pNext ){
210cce7d176Sdrh       sqliteVdbeAddOp(v, OP_Dup, 0, 0, 0, 0);
211cce7d176Sdrh     }
212967e8b73Sdrh     for(i=0; i<pIdx->nColumn; i++){
213967e8b73Sdrh       int idx = pIdx->aiColumn[i];
214967e8b73Sdrh       if( pColumn==0 ){
215cce7d176Sdrh         j = idx;
216cce7d176Sdrh       }else{
217967e8b73Sdrh         for(j=0; j<pColumn->nId; j++){
218967e8b73Sdrh           if( pColumn->a[j].idx==idx ) break;
219cce7d176Sdrh         }
220cce7d176Sdrh       }
221967e8b73Sdrh       if( pColumn && j>=pColumn->nId ){
2227020f651Sdrh         char *zDflt = pTab->aCol[idx].zDflt;
223c61053b7Sdrh         if( zDflt==0 ){
224c61053b7Sdrh           sqliteVdbeAddOp(v, OP_Null, 0, 0, 0, 0);
225c61053b7Sdrh         }else{
2267020f651Sdrh           sqliteVdbeAddOp(v, OP_String, 0, 0, zDflt, 0);
227c61053b7Sdrh         }
2285974a30fSdrh       }else if( srcTab>=0 ){
229cc85b411Sdrh         sqliteVdbeAddOp(v, OP_Field, srcTab, idx, 0, 0);
230cce7d176Sdrh       }else{
231cce7d176Sdrh         sqliteExprCode(pParse, pList->a[j].pExpr);
232cce7d176Sdrh       }
233cce7d176Sdrh     }
234967e8b73Sdrh     sqliteVdbeAddOp(v, OP_MakeKey, pIdx->nColumn, 0, 0, 0);
2355974a30fSdrh     sqliteVdbeAddOp(v, OP_PutIdx, idx+base, 0, 0, 0);
236cce7d176Sdrh   }
2371ccde15dSdrh 
2381ccde15dSdrh   /* The bottom of the loop, if the data source is a SELECT statement
2391ccde15dSdrh   */
2405974a30fSdrh   if( srcTab>=0 ){
2415974a30fSdrh     sqliteVdbeAddOp(v, OP_Goto, 0, iCont, 0, 0);
2425974a30fSdrh     sqliteVdbeAddOp(v, OP_Noop, 0, 0, 0, iBreak);
243cce7d176Sdrh   }
244cce7d176Sdrh 
245cce7d176Sdrh insert_cleanup:
2465974a30fSdrh   if( pList ) sqliteExprListDelete(pList);
2475974a30fSdrh   if( pSelect ) sqliteSelectDelete(pSelect);
248967e8b73Sdrh   sqliteIdListDelete(pColumn);
249cce7d176Sdrh }
250