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*99fcd718Sdrh ** $Id: insert.c,v 1.23 2001/10/13 01:06:48 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 */ 52cce7d176Sdrh 53daffd0e5Sdrh if( pParse->nErr || sqlite_malloc_failed ) goto insert_cleanup; 54ecdc7530Sdrh db = pParse->db; 55daffd0e5Sdrh 561ccde15dSdrh /* Locate the table into which we will be inserting new information. 571ccde15dSdrh */ 58cce7d176Sdrh zTab = sqliteTableNameFromToken(pTableName); 59daffd0e5Sdrh if( zTab==0 ) goto insert_cleanup; 60ecdc7530Sdrh pTab = sqliteFindTable(db, zTab); 61cce7d176Sdrh sqliteFree(zTab); 62cce7d176Sdrh if( pTab==0 ){ 63cce7d176Sdrh sqliteSetNString(&pParse->zErrMsg, "no such table: ", 0, 64cce7d176Sdrh pTableName->z, pTableName->n, 0); 65cce7d176Sdrh pParse->nErr++; 66cce7d176Sdrh goto insert_cleanup; 67cce7d176Sdrh } 68cce7d176Sdrh if( pTab->readOnly ){ 69cce7d176Sdrh sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName, 70cce7d176Sdrh " may not be modified", 0); 71cce7d176Sdrh pParse->nErr++; 72cce7d176Sdrh goto insert_cleanup; 73cce7d176Sdrh } 741ccde15dSdrh 751ccde15dSdrh /* Allocate a VDBE 761ccde15dSdrh */ 77d8bc7086Sdrh v = sqliteGetVdbe(pParse); 785974a30fSdrh if( v==0 ) goto insert_cleanup; 79ecdc7530Sdrh if( (db->flags & SQLITE_InTrans)==0 ){ 80*99fcd718Sdrh sqliteVdbeAddOp(v, OP_Transaction, 0, 0); 81*99fcd718Sdrh sqliteVdbeAddOp(v, OP_VerifyCookie, db->schema_cookie, 0); 82ecdc7530Sdrh pParse->schemaVerified = 1; 835e00f6c7Sdrh } 841ccde15dSdrh 851ccde15dSdrh /* Figure out how many columns of data are supplied. If the data 861ccde15dSdrh ** is comming from a SELECT statement, then this step has to generate 871ccde15dSdrh ** all the code to implement the SELECT statement and leave the data 881ccde15dSdrh ** in a temporary table. If data is coming from an expression list, 891ccde15dSdrh ** then we just have to count the number of expressions. 901ccde15dSdrh */ 915974a30fSdrh if( pSelect ){ 925974a30fSdrh int rc; 935974a30fSdrh srcTab = pParse->nTab++; 94*99fcd718Sdrh sqliteVdbeAddOp(v, OP_OpenTemp, srcTab, 0); 955974a30fSdrh rc = sqliteSelect(pParse, pSelect, SRT_Table, srcTab); 96daffd0e5Sdrh if( rc || pParse->nErr || sqlite_malloc_failed ) goto insert_cleanup; 975974a30fSdrh assert( pSelect->pEList ); 98967e8b73Sdrh nColumn = pSelect->pEList->nExpr; 995974a30fSdrh }else{ 100daffd0e5Sdrh assert( pList!=0 ); 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; 159f57b3399Sdrh openOp = pTab->isTemp ? OP_OpenWrAux : OP_OpenWrite; 160*99fcd718Sdrh sqliteVdbeAddOp(v, openOp, base, pTab->tnum); 161*99fcd718Sdrh sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC); 162bed8690fSdrh for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){ 163*99fcd718Sdrh sqliteVdbeAddOp(v, openOp, idx+base, pIdx->tnum); 164*99fcd718Sdrh sqliteVdbeChangeP3(v, -1, pIdx->zName, P3_STATIC); 1655974a30fSdrh } 1661ccde15dSdrh 1671ccde15dSdrh /* If the data source is a SELECT statement, then we have to create 1681ccde15dSdrh ** a loop because there might be multiple rows of data. If the data 1691ccde15dSdrh ** source is an expression list, then exactly one row will be inserted 1701ccde15dSdrh ** and the loop is not used. 1711ccde15dSdrh */ 1725974a30fSdrh if( srcTab>=0 ){ 173*99fcd718Sdrh sqliteVdbeAddOp(v, OP_Rewind, srcTab, 0); 1745974a30fSdrh iBreak = sqliteVdbeMakeLabel(v); 175*99fcd718Sdrh iCont = sqliteVdbeAddOp(v, OP_Next, srcTab, iBreak); 176bed8690fSdrh } 1771ccde15dSdrh 1781ccde15dSdrh /* Create a new entry in the table and fill it with data. 1791ccde15dSdrh */ 180*99fcd718Sdrh sqliteVdbeAddOp(v, OP_NewRecno, base, 0); 181cce7d176Sdrh if( pTab->pIndex ){ 182*99fcd718Sdrh sqliteVdbeAddOp(v, OP_Dup, 0, 0); 183cce7d176Sdrh } 184cce7d176Sdrh for(i=0; i<pTab->nCol; i++){ 185967e8b73Sdrh if( pColumn==0 ){ 186cce7d176Sdrh j = i; 187cce7d176Sdrh }else{ 188967e8b73Sdrh for(j=0; j<pColumn->nId; j++){ 189967e8b73Sdrh if( pColumn->a[j].idx==i ) break; 190cce7d176Sdrh } 191cce7d176Sdrh } 192967e8b73Sdrh if( pColumn && j>=pColumn->nId ){ 193*99fcd718Sdrh sqliteVdbeAddOp(v, OP_String, 0, 0); 194*99fcd718Sdrh sqliteVdbeChangeP3(v, -1, pTab->aCol[i].zDflt, P3_STATIC); 1955974a30fSdrh }else if( srcTab>=0 ){ 196*99fcd718Sdrh sqliteVdbeAddOp(v, OP_Column, srcTab, i); 197cce7d176Sdrh }else{ 198cce7d176Sdrh sqliteExprCode(pParse, pList->a[j].pExpr); 199cce7d176Sdrh } 200cce7d176Sdrh } 201*99fcd718Sdrh sqliteVdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0); 202*99fcd718Sdrh sqliteVdbeAddOp(v, OP_Put, base, 0); 2031ccde15dSdrh 2041ccde15dSdrh /* Create appropriate entries for the new data row in all indices 2051ccde15dSdrh ** of the table. 2061ccde15dSdrh */ 207bed8690fSdrh for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){ 208cce7d176Sdrh if( pIdx->pNext ){ 209*99fcd718Sdrh sqliteVdbeAddOp(v, OP_Dup, 0, 0); 210cce7d176Sdrh } 211967e8b73Sdrh for(i=0; i<pIdx->nColumn; i++){ 212967e8b73Sdrh int idx = pIdx->aiColumn[i]; 213967e8b73Sdrh if( pColumn==0 ){ 214cce7d176Sdrh j = idx; 215cce7d176Sdrh }else{ 216967e8b73Sdrh for(j=0; j<pColumn->nId; j++){ 217967e8b73Sdrh if( pColumn->a[j].idx==idx ) break; 218cce7d176Sdrh } 219cce7d176Sdrh } 220967e8b73Sdrh if( pColumn && j>=pColumn->nId ){ 221*99fcd718Sdrh sqliteVdbeAddOp(v, OP_String, 0, 0); 222*99fcd718Sdrh sqliteVdbeChangeP3(v, -1, pTab->aCol[idx].zDflt, P3_STATIC); 2235974a30fSdrh }else if( srcTab>=0 ){ 224*99fcd718Sdrh sqliteVdbeAddOp(v, OP_Column, srcTab, idx); 225cce7d176Sdrh }else{ 226cce7d176Sdrh sqliteExprCode(pParse, pList->a[j].pExpr); 227cce7d176Sdrh } 228cce7d176Sdrh } 229*99fcd718Sdrh sqliteVdbeAddOp(v, OP_MakeIdxKey, pIdx->nColumn, 0); 230*99fcd718Sdrh sqliteVdbeAddOp(v, OP_PutIdx, idx+base, pIdx->isUnique); 231cce7d176Sdrh } 2321ccde15dSdrh 2331ccde15dSdrh /* The bottom of the loop, if the data source is a SELECT statement 2341ccde15dSdrh */ 2355974a30fSdrh if( srcTab>=0 ){ 236*99fcd718Sdrh sqliteVdbeAddOp(v, OP_Goto, 0, iCont); 237*99fcd718Sdrh sqliteVdbeResolveLabel(v, iBreak); 238*99fcd718Sdrh sqliteVdbeAddOp(v, OP_Noop, 0, 0); 239cce7d176Sdrh } 240ecdc7530Sdrh if( (db->flags & SQLITE_InTrans)==0 ){ 241*99fcd718Sdrh sqliteVdbeAddOp(v, OP_Commit, 0, 0); 2425e00f6c7Sdrh } 2435e00f6c7Sdrh 244cce7d176Sdrh 245cce7d176Sdrh insert_cleanup: 2465974a30fSdrh if( pList ) sqliteExprListDelete(pList); 2475974a30fSdrh if( pSelect ) sqliteSelectDelete(pSelect); 248967e8b73Sdrh sqliteIdListDelete(pColumn); 249cce7d176Sdrh } 250