xref: /sqlite-3.40.0/src/insert.c (revision b19a2bc6)
1cce7d176Sdrh /*
2*b19a2bc6Sdrh ** 2001 September 15
3cce7d176Sdrh **
4*b19a2bc6Sdrh ** The author disclaims copyright to this source code.  In place of
5*b19a2bc6Sdrh ** a legal notice, here is a blessing:
6cce7d176Sdrh **
7*b19a2bc6Sdrh **    May you do good and not evil.
8*b19a2bc6Sdrh **    May you find forgiveness for yourself and forgive others.
9*b19a2bc6Sdrh **    May you share freely, never taking more than you give.
10cce7d176Sdrh **
11cce7d176Sdrh *************************************************************************
12cce7d176Sdrh ** This file contains C code routines that are called by the parser
13*b19a2bc6Sdrh ** to handle INSERT statements in SQLite.
14cce7d176Sdrh **
15*b19a2bc6Sdrh ** $Id: insert.c,v 1.18 2001/09/16 00:13:27 drh Exp $
16cce7d176Sdrh */
17cce7d176Sdrh #include "sqliteInt.h"
18cce7d176Sdrh 
19cce7d176Sdrh /*
201ccde15dSdrh ** This routine is call to handle SQL of the following forms:
21cce7d176Sdrh **
22cce7d176Sdrh **    insert into TABLE (IDLIST) values(EXPRLIST)
231ccde15dSdrh **    insert into TABLE (IDLIST) select
24cce7d176Sdrh **
251ccde15dSdrh ** The IDLIST following the table name is always optional.  If omitted,
261ccde15dSdrh ** then a list of all columns for the table is substituted.  The IDLIST
27967e8b73Sdrh ** appears in the pColumn parameter.  pColumn is NULL if IDLIST is omitted.
281ccde15dSdrh **
291ccde15dSdrh ** The pList parameter holds EXPRLIST in the first form of the INSERT
301ccde15dSdrh ** statement above, and pSelect is NULL.  For the second form, pList is
311ccde15dSdrh ** NULL and pSelect is a pointer to the select statement used to generate
321ccde15dSdrh ** data for the insert.
33cce7d176Sdrh */
34cce7d176Sdrh void sqliteInsert(
35cce7d176Sdrh   Parse *pParse,        /* Parser context */
36cce7d176Sdrh   Token *pTableName,    /* Name of table into which we are inserting */
37cce7d176Sdrh   ExprList *pList,      /* List of values to be inserted */
385974a30fSdrh   Select *pSelect,      /* A SELECT statement to use as the data source */
39967e8b73Sdrh   IdList *pColumn       /* Column names corresponding to IDLIST. */
40cce7d176Sdrh ){
415974a30fSdrh   Table *pTab;          /* The table to insert into */
425974a30fSdrh   char *zTab;           /* Name of the table into which we are inserting */
435974a30fSdrh   int i, j, idx;        /* Loop counters */
445974a30fSdrh   Vdbe *v;              /* Generate code into this virtual machine */
455974a30fSdrh   Index *pIdx;          /* For looping over indices of the table */
465974a30fSdrh   int srcTab;           /* Date comes from this temporary cursor if >=0 */
47967e8b73Sdrh   int nColumn;          /* Number of columns in the data */
485974a30fSdrh   int base;             /* First available cursor */
495974a30fSdrh   int iCont, iBreak;    /* Beginning and end of the loop over srcTab */
50cce7d176Sdrh 
51daffd0e5Sdrh   if( pParse->nErr || sqlite_malloc_failed ) goto insert_cleanup;
52daffd0e5Sdrh 
531ccde15dSdrh   /* Locate the table into which we will be inserting new information.
541ccde15dSdrh   */
55cce7d176Sdrh   zTab = sqliteTableNameFromToken(pTableName);
56daffd0e5Sdrh   if( zTab==0 ) goto insert_cleanup;
57cce7d176Sdrh   pTab = sqliteFindTable(pParse->db, zTab);
58cce7d176Sdrh   sqliteFree(zTab);
59cce7d176Sdrh   if( pTab==0 ){
60cce7d176Sdrh     sqliteSetNString(&pParse->zErrMsg, "no such table: ", 0,
61cce7d176Sdrh         pTableName->z, pTableName->n, 0);
62cce7d176Sdrh     pParse->nErr++;
63cce7d176Sdrh     goto insert_cleanup;
64cce7d176Sdrh   }
65cce7d176Sdrh   if( pTab->readOnly ){
66cce7d176Sdrh     sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName,
67cce7d176Sdrh         " may not be modified", 0);
68cce7d176Sdrh     pParse->nErr++;
69cce7d176Sdrh     goto insert_cleanup;
70cce7d176Sdrh   }
711ccde15dSdrh 
721ccde15dSdrh   /* Allocate a VDBE
731ccde15dSdrh   */
74d8bc7086Sdrh   v = sqliteGetVdbe(pParse);
755974a30fSdrh   if( v==0 ) goto insert_cleanup;
765e00f6c7Sdrh   if( (pParse->db->flags & SQLITE_InTrans)==0 ){
775e00f6c7Sdrh     sqliteVdbeAddOp(v, OP_Transaction, 0, 0, 0, 0);
7850e5dadfSdrh     sqliteVdbeAddOp(v, OP_VerifyCookie, pParse->db->schema_cookie, 0, 0, 0);
795e00f6c7Sdrh   }
801ccde15dSdrh 
811ccde15dSdrh   /* Figure out how many columns of data are supplied.  If the data
821ccde15dSdrh   ** is comming from a SELECT statement, then this step has to generate
831ccde15dSdrh   ** all the code to implement the SELECT statement and leave the data
841ccde15dSdrh   ** in a temporary table.  If data is coming from an expression list,
851ccde15dSdrh   ** then we just have to count the number of expressions.
861ccde15dSdrh   */
875974a30fSdrh   if( pSelect ){
885974a30fSdrh     int rc;
895974a30fSdrh     srcTab = pParse->nTab++;
90be0072d2Sdrh     sqliteVdbeAddOp(v, OP_OpenTemp, srcTab, 0, 0, 0);
915974a30fSdrh     rc = sqliteSelect(pParse, pSelect, SRT_Table, srcTab);
92daffd0e5Sdrh     if( rc || pParse->nErr || sqlite_malloc_failed ) goto insert_cleanup;
935974a30fSdrh     assert( pSelect->pEList );
94967e8b73Sdrh     nColumn = pSelect->pEList->nExpr;
955974a30fSdrh   }else{
96daffd0e5Sdrh     assert( pList!=0 );
975974a30fSdrh     srcTab = -1;
985974a30fSdrh     assert( pList );
99967e8b73Sdrh     nColumn = pList->nExpr;
1005974a30fSdrh   }
1011ccde15dSdrh 
1021ccde15dSdrh   /* Make sure the number of columns in the source data matches the number
1031ccde15dSdrh   ** of columns to be inserted into the table.
1041ccde15dSdrh   */
105967e8b73Sdrh   if( pColumn==0 && nColumn!=pTab->nCol ){
106cce7d176Sdrh     char zNum1[30];
107cce7d176Sdrh     char zNum2[30];
108967e8b73Sdrh     sprintf(zNum1,"%d", nColumn);
109cce7d176Sdrh     sprintf(zNum2,"%d", pTab->nCol);
110cce7d176Sdrh     sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName,
111cce7d176Sdrh        " has ", zNum2, " columns but ",
112cce7d176Sdrh        zNum1, " values were supplied", 0);
113cce7d176Sdrh     pParse->nErr++;
114cce7d176Sdrh     goto insert_cleanup;
115cce7d176Sdrh   }
116967e8b73Sdrh   if( pColumn!=0 && nColumn!=pColumn->nId ){
117cce7d176Sdrh     char zNum1[30];
118cce7d176Sdrh     char zNum2[30];
119967e8b73Sdrh     sprintf(zNum1,"%d", nColumn);
120967e8b73Sdrh     sprintf(zNum2,"%d", pColumn->nId);
121cce7d176Sdrh     sqliteSetString(&pParse->zErrMsg, zNum1, " values for ",
122cce7d176Sdrh        zNum2, " columns", 0);
123cce7d176Sdrh     pParse->nErr++;
124cce7d176Sdrh     goto insert_cleanup;
125cce7d176Sdrh   }
1261ccde15dSdrh 
1271ccde15dSdrh   /* If the INSERT statement included an IDLIST term, then make sure
1281ccde15dSdrh   ** all elements of the IDLIST really are columns of the table and
1291ccde15dSdrh   ** remember the column indices.
1301ccde15dSdrh   */
131967e8b73Sdrh   if( pColumn ){
132967e8b73Sdrh     for(i=0; i<pColumn->nId; i++){
133967e8b73Sdrh       pColumn->a[i].idx = -1;
134cce7d176Sdrh     }
135967e8b73Sdrh     for(i=0; i<pColumn->nId; i++){
136cce7d176Sdrh       for(j=0; j<pTab->nCol; j++){
137967e8b73Sdrh         if( sqliteStrICmp(pColumn->a[i].zName, pTab->aCol[j].zName)==0 ){
138967e8b73Sdrh           pColumn->a[i].idx = j;
139cce7d176Sdrh           break;
140cce7d176Sdrh         }
141cce7d176Sdrh       }
142cce7d176Sdrh       if( j>=pTab->nCol ){
143cce7d176Sdrh         sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName,
144967e8b73Sdrh            " has no column named ", pColumn->a[i].zName, 0);
145cce7d176Sdrh         pParse->nErr++;
146cce7d176Sdrh         goto insert_cleanup;
147cce7d176Sdrh       }
148cce7d176Sdrh     }
149cce7d176Sdrh   }
1501ccde15dSdrh 
1511ccde15dSdrh   /* Open cursors into the table that is received the new data and
1521ccde15dSdrh   ** all indices of that table.
1531ccde15dSdrh   */
1545974a30fSdrh   base = pParse->nTab;
155be0072d2Sdrh   sqliteVdbeAddOp(v, OP_Open, base, pTab->tnum, pTab->zName, 0);
156bed8690fSdrh   for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
157be0072d2Sdrh     sqliteVdbeAddOp(v, OP_Open, idx+base, pIdx->tnum, pIdx->zName, 0);
1585974a30fSdrh   }
1591ccde15dSdrh 
1601ccde15dSdrh   /* If the data source is a SELECT statement, then we have to create
1611ccde15dSdrh   ** a loop because there might be multiple rows of data.  If the data
1621ccde15dSdrh   ** source is an expression list, then exactly one row will be inserted
1631ccde15dSdrh   ** and the loop is not used.
1641ccde15dSdrh   */
1655974a30fSdrh   if( srcTab>=0 ){
1665974a30fSdrh     sqliteVdbeAddOp(v, OP_Rewind, srcTab, 0, 0, 0);
1675974a30fSdrh     iBreak = sqliteVdbeMakeLabel(v);
1685974a30fSdrh     iCont = sqliteVdbeAddOp(v, OP_Next, srcTab, iBreak, 0, 0);
169bed8690fSdrh   }
1701ccde15dSdrh 
1711ccde15dSdrh   /* Create a new entry in the table and fill it with data.
1721ccde15dSdrh   */
1733fc190ccSdrh   sqliteVdbeAddOp(v, OP_NewRecno, base, 0, 0, 0);
174cce7d176Sdrh   if( pTab->pIndex ){
175cce7d176Sdrh     sqliteVdbeAddOp(v, OP_Dup, 0, 0, 0, 0);
176cce7d176Sdrh   }
177cce7d176Sdrh   for(i=0; i<pTab->nCol; i++){
178967e8b73Sdrh     if( pColumn==0 ){
179cce7d176Sdrh       j = i;
180cce7d176Sdrh     }else{
181967e8b73Sdrh       for(j=0; j<pColumn->nId; j++){
182967e8b73Sdrh         if( pColumn->a[j].idx==i ) break;
183cce7d176Sdrh       }
184cce7d176Sdrh     }
185967e8b73Sdrh     if( pColumn && j>=pColumn->nId ){
1867020f651Sdrh       char *zDflt = pTab->aCol[i].zDflt;
187c61053b7Sdrh       if( zDflt==0 ){
188c61053b7Sdrh         sqliteVdbeAddOp(v, OP_Null, 0, 0, 0, 0);
189c61053b7Sdrh       }else{
1907020f651Sdrh         sqliteVdbeAddOp(v, OP_String, 0, 0, zDflt, 0);
191c61053b7Sdrh       }
1925974a30fSdrh     }else if( srcTab>=0 ){
193be0072d2Sdrh       sqliteVdbeAddOp(v, OP_Column, srcTab, i, 0, 0);
194cce7d176Sdrh     }else{
195cce7d176Sdrh       sqliteExprCode(pParse, pList->a[j].pExpr);
196cce7d176Sdrh     }
197cce7d176Sdrh   }
198cce7d176Sdrh   sqliteVdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0, 0, 0);
1995974a30fSdrh   sqliteVdbeAddOp(v, OP_Put, base, 0, 0, 0);
2001ccde15dSdrh 
2011ccde15dSdrh   /* Create appropriate entries for the new data row in all indices
2021ccde15dSdrh   ** of the table.
2031ccde15dSdrh   */
204bed8690fSdrh   for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
205cce7d176Sdrh     if( pIdx->pNext ){
206cce7d176Sdrh       sqliteVdbeAddOp(v, OP_Dup, 0, 0, 0, 0);
207cce7d176Sdrh     }
208967e8b73Sdrh     for(i=0; i<pIdx->nColumn; i++){
209967e8b73Sdrh       int idx = pIdx->aiColumn[i];
210967e8b73Sdrh       if( pColumn==0 ){
211cce7d176Sdrh         j = idx;
212cce7d176Sdrh       }else{
213967e8b73Sdrh         for(j=0; j<pColumn->nId; j++){
214967e8b73Sdrh           if( pColumn->a[j].idx==idx ) break;
215cce7d176Sdrh         }
216cce7d176Sdrh       }
217967e8b73Sdrh       if( pColumn && j>=pColumn->nId ){
2187020f651Sdrh         char *zDflt = pTab->aCol[idx].zDflt;
219c61053b7Sdrh         if( zDflt==0 ){
220c61053b7Sdrh           sqliteVdbeAddOp(v, OP_Null, 0, 0, 0, 0);
221c61053b7Sdrh         }else{
2227020f651Sdrh           sqliteVdbeAddOp(v, OP_String, 0, 0, zDflt, 0);
223c61053b7Sdrh         }
2245974a30fSdrh       }else if( srcTab>=0 ){
225be0072d2Sdrh         sqliteVdbeAddOp(v, OP_Column, srcTab, idx, 0, 0);
226cce7d176Sdrh       }else{
227cce7d176Sdrh         sqliteExprCode(pParse, pList->a[j].pExpr);
228cce7d176Sdrh       }
229cce7d176Sdrh     }
2305e00f6c7Sdrh     sqliteVdbeAddOp(v, OP_MakeIdxKey, pIdx->nColumn, 0, 0, 0);
2315974a30fSdrh     sqliteVdbeAddOp(v, OP_PutIdx, idx+base, 0, 0, 0);
232cce7d176Sdrh   }
2331ccde15dSdrh 
2341ccde15dSdrh   /* The bottom of the loop, if the data source is a SELECT statement
2351ccde15dSdrh   */
2365974a30fSdrh   if( srcTab>=0 ){
2375974a30fSdrh     sqliteVdbeAddOp(v, OP_Goto, 0, iCont, 0, 0);
2385974a30fSdrh     sqliteVdbeAddOp(v, OP_Noop, 0, 0, 0, iBreak);
239cce7d176Sdrh   }
2405e00f6c7Sdrh   if( (pParse->db->flags & SQLITE_InTrans)==0 ){
2415e00f6c7Sdrh     sqliteVdbeAddOp(v, OP_Commit, 0, 0, 0, 0);
2425e00f6c7Sdrh   }
2435e00f6c7Sdrh 
244cce7d176Sdrh 
245cce7d176Sdrh insert_cleanup:
2465974a30fSdrh   if( pList ) sqliteExprListDelete(pList);
2475974a30fSdrh   if( pSelect ) sqliteSelectDelete(pSelect);
248967e8b73Sdrh   sqliteIdListDelete(pColumn);
249cce7d176Sdrh }
250