xref: /sqlite-3.40.0/src/insert.c (revision 9aa028da)
1cce7d176Sdrh /*
2b19a2bc6Sdrh ** 2001 September 15
3cce7d176Sdrh **
4b19a2bc6Sdrh ** The author disclaims copyright to this source code.  In place of
5b19a2bc6Sdrh ** a legal notice, here is a blessing:
6cce7d176Sdrh **
7b19a2bc6Sdrh **    May you do good and not evil.
8b19a2bc6Sdrh **    May you find forgiveness for yourself and forgive others.
9b19a2bc6Sdrh **    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
13b19a2bc6Sdrh ** to handle INSERT statements in SQLite.
14cce7d176Sdrh **
15*9aa028daSdrh ** $Id: insert.c,v 1.29 2001/12/22 21:48:30 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 */
50ecdc7530Sdrh   sqlite *db;           /* The main database structure */
51f57b3399Sdrh   int openOp;           /* Opcode used to open cursors */
524a32431cSdrh   int keyColumn = -1;   /* Column that is the INTEGER PRIMARY KEY */
53cce7d176Sdrh 
54daffd0e5Sdrh   if( pParse->nErr || sqlite_malloc_failed ) goto insert_cleanup;
55ecdc7530Sdrh   db = pParse->db;
56daffd0e5Sdrh 
571ccde15dSdrh   /* Locate the table into which we will be inserting new information.
581ccde15dSdrh   */
59cce7d176Sdrh   zTab = sqliteTableNameFromToken(pTableName);
60daffd0e5Sdrh   if( zTab==0 ) goto insert_cleanup;
61ecdc7530Sdrh   pTab = sqliteFindTable(db, zTab);
62cce7d176Sdrh   sqliteFree(zTab);
63cce7d176Sdrh   if( pTab==0 ){
64cce7d176Sdrh     sqliteSetNString(&pParse->zErrMsg, "no such table: ", 0,
65cce7d176Sdrh         pTableName->z, pTableName->n, 0);
66cce7d176Sdrh     pParse->nErr++;
67cce7d176Sdrh     goto insert_cleanup;
68cce7d176Sdrh   }
69cce7d176Sdrh   if( pTab->readOnly ){
70cce7d176Sdrh     sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName,
71cce7d176Sdrh         " may not be modified", 0);
72cce7d176Sdrh     pParse->nErr++;
73cce7d176Sdrh     goto insert_cleanup;
74cce7d176Sdrh   }
751ccde15dSdrh 
761ccde15dSdrh   /* Allocate a VDBE
771ccde15dSdrh   */
78d8bc7086Sdrh   v = sqliteGetVdbe(pParse);
795974a30fSdrh   if( v==0 ) goto insert_cleanup;
80ecdc7530Sdrh   if( (db->flags & SQLITE_InTrans)==0 ){
8199fcd718Sdrh     sqliteVdbeAddOp(v, OP_Transaction, 0, 0);
8299fcd718Sdrh     sqliteVdbeAddOp(v, OP_VerifyCookie, db->schema_cookie, 0);
83ecdc7530Sdrh     pParse->schemaVerified = 1;
845e00f6c7Sdrh   }
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++;
9599fcd718Sdrh     sqliteVdbeAddOp(v, OP_OpenTemp, srcTab, 0);
965974a30fSdrh     rc = sqliteSelect(pParse, pSelect, SRT_Table, srcTab);
97daffd0e5Sdrh     if( rc || pParse->nErr || sqlite_malloc_failed ) goto insert_cleanup;
985974a30fSdrh     assert( pSelect->pEList );
99967e8b73Sdrh     nColumn = pSelect->pEList->nExpr;
1005974a30fSdrh   }else{
101daffd0e5Sdrh     assert( pList!=0 );
1025974a30fSdrh     srcTab = -1;
1035974a30fSdrh     assert( pList );
104967e8b73Sdrh     nColumn = pList->nExpr;
1055974a30fSdrh   }
1061ccde15dSdrh 
1071ccde15dSdrh   /* Make sure the number of columns in the source data matches the number
1081ccde15dSdrh   ** of columns to be inserted into the table.
1091ccde15dSdrh   */
110967e8b73Sdrh   if( pColumn==0 && nColumn!=pTab->nCol ){
111cce7d176Sdrh     char zNum1[30];
112cce7d176Sdrh     char zNum2[30];
113967e8b73Sdrh     sprintf(zNum1,"%d", nColumn);
114cce7d176Sdrh     sprintf(zNum2,"%d", pTab->nCol);
115cce7d176Sdrh     sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName,
116cce7d176Sdrh        " has ", zNum2, " columns but ",
117cce7d176Sdrh        zNum1, " values were supplied", 0);
118cce7d176Sdrh     pParse->nErr++;
119cce7d176Sdrh     goto insert_cleanup;
120cce7d176Sdrh   }
121967e8b73Sdrh   if( pColumn!=0 && nColumn!=pColumn->nId ){
122cce7d176Sdrh     char zNum1[30];
123cce7d176Sdrh     char zNum2[30];
124967e8b73Sdrh     sprintf(zNum1,"%d", nColumn);
125967e8b73Sdrh     sprintf(zNum2,"%d", pColumn->nId);
126cce7d176Sdrh     sqliteSetString(&pParse->zErrMsg, zNum1, " values for ",
127cce7d176Sdrh        zNum2, " columns", 0);
128cce7d176Sdrh     pParse->nErr++;
129cce7d176Sdrh     goto insert_cleanup;
130cce7d176Sdrh   }
1311ccde15dSdrh 
1321ccde15dSdrh   /* If the INSERT statement included an IDLIST term, then make sure
1331ccde15dSdrh   ** all elements of the IDLIST really are columns of the table and
1341ccde15dSdrh   ** remember the column indices.
1351ccde15dSdrh   */
136967e8b73Sdrh   if( pColumn ){
137967e8b73Sdrh     for(i=0; i<pColumn->nId; i++){
138967e8b73Sdrh       pColumn->a[i].idx = -1;
139cce7d176Sdrh     }
140967e8b73Sdrh     for(i=0; i<pColumn->nId; i++){
141cce7d176Sdrh       for(j=0; j<pTab->nCol; j++){
142967e8b73Sdrh         if( sqliteStrICmp(pColumn->a[i].zName, pTab->aCol[j].zName)==0 ){
143967e8b73Sdrh           pColumn->a[i].idx = j;
1444a32431cSdrh           if( j==pTab->iPKey ){
145*9aa028daSdrh             keyColumn = i;
1464a32431cSdrh           }
147cce7d176Sdrh           break;
148cce7d176Sdrh         }
149cce7d176Sdrh       }
150cce7d176Sdrh       if( j>=pTab->nCol ){
151cce7d176Sdrh         sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName,
152967e8b73Sdrh            " has no column named ", pColumn->a[i].zName, 0);
153cce7d176Sdrh         pParse->nErr++;
154cce7d176Sdrh         goto insert_cleanup;
155cce7d176Sdrh       }
156cce7d176Sdrh     }
157cce7d176Sdrh   }
1581ccde15dSdrh 
1594a32431cSdrh   /* If there is not IDLIST term but the table has an integer primary
1604a32431cSdrh   ** key, the set the keyColumn variable to the primary key column.
1614a32431cSdrh   */
1624a32431cSdrh   if( pColumn==0 ){
1634a32431cSdrh     keyColumn = pTab->iPKey;
1644a32431cSdrh   }
1654a32431cSdrh 
1661ccde15dSdrh   /* Open cursors into the table that is received the new data and
1671ccde15dSdrh   ** all indices of that table.
1681ccde15dSdrh   */
1695974a30fSdrh   base = pParse->nTab;
170f57b3399Sdrh   openOp = pTab->isTemp ? OP_OpenWrAux : OP_OpenWrite;
17199fcd718Sdrh   sqliteVdbeAddOp(v, openOp, base, pTab->tnum);
17299fcd718Sdrh   sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC);
173bed8690fSdrh   for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
17499fcd718Sdrh     sqliteVdbeAddOp(v, openOp, idx+base, pIdx->tnum);
17599fcd718Sdrh     sqliteVdbeChangeP3(v, -1, pIdx->zName, P3_STATIC);
1765974a30fSdrh   }
1771ccde15dSdrh 
1781ccde15dSdrh   /* If the data source is a SELECT statement, then we have to create
1791ccde15dSdrh   ** a loop because there might be multiple rows of data.  If the data
1801ccde15dSdrh   ** source is an expression list, then exactly one row will be inserted
1811ccde15dSdrh   ** and the loop is not used.
1821ccde15dSdrh   */
1835974a30fSdrh   if( srcTab>=0 ){
1841bee3d7bSdrh     if( db->flags & SQLITE_CountRows ){
1851bee3d7bSdrh       sqliteVdbeAddOp(v, OP_Integer, 0, 0);  /* Initialize the row count */
1861bee3d7bSdrh     }
1875974a30fSdrh     iBreak = sqliteVdbeMakeLabel(v);
1886b56344dSdrh     sqliteVdbeAddOp(v, OP_Rewind, srcTab, iBreak);
1896b56344dSdrh     iCont = sqliteVdbeCurrentAddr(v);
190bed8690fSdrh   }
1911ccde15dSdrh 
1924a32431cSdrh   /* Push the record number for the new entry onto the stack.  The
1934a32431cSdrh   ** record number is a randomly generate integer created by NewRecno
1944a32431cSdrh   ** except when the table has an INTEGER PRIMARY KEY column, in which
1954a32431cSdrh   ** case the record number is the same as that column.
1961ccde15dSdrh   */
1974a32431cSdrh   if( keyColumn>=0 ){
1984a32431cSdrh     if( srcTab>=0 ){
1994a32431cSdrh       sqliteVdbeAddOp(v, OP_Column, srcTab, keyColumn);
2004a32431cSdrh     }else{
2014a32431cSdrh       sqliteExprCode(pParse, pList->a[keyColumn].pExpr);
2024a32431cSdrh     }
2038aff1015Sdrh     sqliteVdbeAddOp(v, OP_MustBeInt, 0, 0);
2044a32431cSdrh   }else{
20599fcd718Sdrh     sqliteVdbeAddOp(v, OP_NewRecno, base, 0);
2064a32431cSdrh   }
2074a32431cSdrh 
2084a32431cSdrh   /* If there are indices, we'll need this record number again, so make
2094a32431cSdrh   ** a copy.
2104a32431cSdrh   */
211cce7d176Sdrh   if( pTab->pIndex ){
21299fcd718Sdrh     sqliteVdbeAddOp(v, OP_Dup, 0, 0);
213cce7d176Sdrh   }
2144a32431cSdrh 
2154a32431cSdrh   /* Push onto the stack data for all columns of the new entry, beginning
2164a32431cSdrh   ** with the first column.
2174a32431cSdrh   */
218cce7d176Sdrh   for(i=0; i<pTab->nCol; i++){
2194a32431cSdrh     if( i==pTab->iPKey ){
2204a32431cSdrh       /* The value of the INTEGER PRIMARY KEY column is always a NULL.
2214a32431cSdrh       ** Whenever this column is used, the record number will be substituted
2224a32431cSdrh       ** in its place, so there is no point it it taking up space in
2234a32431cSdrh       ** the data record. */
2244a32431cSdrh       sqliteVdbeAddOp(v, OP_String, 0, 0);
2254a32431cSdrh       continue;
2264a32431cSdrh     }
227967e8b73Sdrh     if( pColumn==0 ){
228cce7d176Sdrh       j = i;
229cce7d176Sdrh     }else{
230967e8b73Sdrh       for(j=0; j<pColumn->nId; j++){
231967e8b73Sdrh         if( pColumn->a[j].idx==i ) break;
232cce7d176Sdrh       }
233cce7d176Sdrh     }
234967e8b73Sdrh     if( pColumn && j>=pColumn->nId ){
23599fcd718Sdrh       sqliteVdbeAddOp(v, OP_String, 0, 0);
23699fcd718Sdrh       sqliteVdbeChangeP3(v, -1, pTab->aCol[i].zDflt, P3_STATIC);
2375974a30fSdrh     }else if( srcTab>=0 ){
23899fcd718Sdrh       sqliteVdbeAddOp(v, OP_Column, srcTab, i);
239cce7d176Sdrh     }else{
240cce7d176Sdrh       sqliteExprCode(pParse, pList->a[j].pExpr);
241cce7d176Sdrh     }
242cce7d176Sdrh   }
2431ccde15dSdrh 
2444a32431cSdrh   /* Create the new record and put it into the database.
2454a32431cSdrh   */
2464a32431cSdrh   sqliteVdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0);
2474a32431cSdrh   sqliteVdbeAddOp(v, OP_Put, base, keyColumn>=0);
2481bee3d7bSdrh 
2491ccde15dSdrh   /* Create appropriate entries for the new data row in all indices
2501ccde15dSdrh   ** of the table.
2511ccde15dSdrh   */
252bed8690fSdrh   for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
253cce7d176Sdrh     if( pIdx->pNext ){
25499fcd718Sdrh       sqliteVdbeAddOp(v, OP_Dup, 0, 0);
255cce7d176Sdrh     }
256967e8b73Sdrh     for(i=0; i<pIdx->nColumn; i++){
257967e8b73Sdrh       int idx = pIdx->aiColumn[i];
2584a32431cSdrh       if( idx==pTab->iPKey ){
2594a32431cSdrh         /* Copy the record number in place of the INTEGER PRIMARY KEY column */
2604a32431cSdrh         sqliteVdbeAddOp(v, OP_Dup, i, 0);
2614a32431cSdrh         continue;
2624a32431cSdrh       }
263967e8b73Sdrh       if( pColumn==0 ){
264cce7d176Sdrh         j = idx;
265cce7d176Sdrh       }else{
266967e8b73Sdrh         for(j=0; j<pColumn->nId; j++){
267967e8b73Sdrh           if( pColumn->a[j].idx==idx ) break;
268cce7d176Sdrh         }
269cce7d176Sdrh       }
270967e8b73Sdrh       if( pColumn && j>=pColumn->nId ){
27199fcd718Sdrh         sqliteVdbeAddOp(v, OP_String, 0, 0);
27299fcd718Sdrh         sqliteVdbeChangeP3(v, -1, pTab->aCol[idx].zDflt, P3_STATIC);
2735974a30fSdrh       }else if( srcTab>=0 ){
27499fcd718Sdrh         sqliteVdbeAddOp(v, OP_Column, srcTab, idx);
275cce7d176Sdrh       }else{
276cce7d176Sdrh         sqliteExprCode(pParse, pList->a[j].pExpr);
277cce7d176Sdrh       }
278cce7d176Sdrh     }
27999fcd718Sdrh     sqliteVdbeAddOp(v, OP_MakeIdxKey, pIdx->nColumn, 0);
2808721ce4aSdrh     sqliteVdbeAddOp(v, OP_IdxPut, idx+base, pIdx->isUnique);
281cce7d176Sdrh   }
2821ccde15dSdrh 
2831bee3d7bSdrh 
2841bee3d7bSdrh   /* If inserting from a SELECT, keep a count of the number of
2851bee3d7bSdrh   ** rows inserted.
2861bee3d7bSdrh   */
2871bee3d7bSdrh   if( srcTab>=0 && (db->flags & SQLITE_CountRows)!=0 ){
2881bee3d7bSdrh     sqliteVdbeAddOp(v, OP_AddImm, 1, 0);
2891bee3d7bSdrh   }
2901bee3d7bSdrh 
2911ccde15dSdrh   /* The bottom of the loop, if the data source is a SELECT statement
2921ccde15dSdrh   */
2935974a30fSdrh   if( srcTab>=0 ){
2946b56344dSdrh     sqliteVdbeAddOp(v, OP_Next, srcTab, iCont);
29599fcd718Sdrh     sqliteVdbeResolveLabel(v, iBreak);
2966b56344dSdrh     sqliteVdbeAddOp(v, OP_Close, srcTab, 0);
2976b56344dSdrh   }
2986b56344dSdrh   sqliteVdbeAddOp(v, OP_Close, base, 0);
2996b56344dSdrh   for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
3006b56344dSdrh     sqliteVdbeAddOp(v, OP_Close, idx+base, 0);
301cce7d176Sdrh   }
302ecdc7530Sdrh   if( (db->flags & SQLITE_InTrans)==0 ){
30399fcd718Sdrh     sqliteVdbeAddOp(v, OP_Commit, 0, 0);
3045e00f6c7Sdrh   }
3055e00f6c7Sdrh 
3061bee3d7bSdrh   /*
3071bee3d7bSdrh   ** Return the number of rows inserted.
3081bee3d7bSdrh   */
3091bee3d7bSdrh   if( db->flags & SQLITE_CountRows ){
3101bee3d7bSdrh     sqliteVdbeAddOp(v, OP_ColumnCount, 1, 0);
3111bee3d7bSdrh     sqliteVdbeAddOp(v, OP_ColumnName, 0, 0);
3121bee3d7bSdrh     sqliteVdbeChangeP3(v, -1, "rows inserted", P3_STATIC);
3131bee3d7bSdrh     if( srcTab<0 ){
3141bee3d7bSdrh       sqliteVdbeAddOp(v, OP_Integer, 1, 0);
3151bee3d7bSdrh     }
3161bee3d7bSdrh     sqliteVdbeAddOp(v, OP_Callback, 1, 0);
3171bee3d7bSdrh   }
318cce7d176Sdrh 
319cce7d176Sdrh insert_cleanup:
3205974a30fSdrh   if( pList ) sqliteExprListDelete(pList);
3215974a30fSdrh   if( pSelect ) sqliteSelectDelete(pSelect);
322967e8b73Sdrh   sqliteIdListDelete(pColumn);
323cce7d176Sdrh }
324