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*ecdc7530Sdrh ** $Id: insert.c,v 1.19 2001/09/23 02:35:53 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 */ 50*ecdc7530Sdrh sqlite *db; /* The main database structure */ 51cce7d176Sdrh 52daffd0e5Sdrh if( pParse->nErr || sqlite_malloc_failed ) goto insert_cleanup; 53*ecdc7530Sdrh db = pParse->db; 54daffd0e5Sdrh 551ccde15dSdrh /* Locate the table into which we will be inserting new information. 561ccde15dSdrh */ 57cce7d176Sdrh zTab = sqliteTableNameFromToken(pTableName); 58daffd0e5Sdrh if( zTab==0 ) goto insert_cleanup; 59*ecdc7530Sdrh pTab = sqliteFindTable(db, zTab); 60cce7d176Sdrh sqliteFree(zTab); 61cce7d176Sdrh if( pTab==0 ){ 62cce7d176Sdrh sqliteSetNString(&pParse->zErrMsg, "no such table: ", 0, 63cce7d176Sdrh pTableName->z, pTableName->n, 0); 64cce7d176Sdrh pParse->nErr++; 65cce7d176Sdrh goto insert_cleanup; 66cce7d176Sdrh } 67cce7d176Sdrh if( pTab->readOnly ){ 68cce7d176Sdrh sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName, 69cce7d176Sdrh " may not be modified", 0); 70cce7d176Sdrh pParse->nErr++; 71cce7d176Sdrh goto insert_cleanup; 72cce7d176Sdrh } 731ccde15dSdrh 741ccde15dSdrh /* Allocate a VDBE 751ccde15dSdrh */ 76d8bc7086Sdrh v = sqliteGetVdbe(pParse); 775974a30fSdrh if( v==0 ) goto insert_cleanup; 78*ecdc7530Sdrh if( (db->flags & SQLITE_InTrans)==0 ){ 795e00f6c7Sdrh sqliteVdbeAddOp(v, OP_Transaction, 0, 0, 0, 0); 80*ecdc7530Sdrh sqliteVdbeAddOp(v, OP_VerifyCookie, db->schema_cookie, 0, 0, 0); 81*ecdc7530Sdrh pParse->schemaVerified = 1; 825e00f6c7Sdrh } 831ccde15dSdrh 841ccde15dSdrh /* Figure out how many columns of data are supplied. If the data 851ccde15dSdrh ** is comming from a SELECT statement, then this step has to generate 861ccde15dSdrh ** all the code to implement the SELECT statement and leave the data 871ccde15dSdrh ** in a temporary table. If data is coming from an expression list, 881ccde15dSdrh ** then we just have to count the number of expressions. 891ccde15dSdrh */ 905974a30fSdrh if( pSelect ){ 915974a30fSdrh int rc; 925974a30fSdrh srcTab = pParse->nTab++; 93be0072d2Sdrh sqliteVdbeAddOp(v, OP_OpenTemp, srcTab, 0, 0, 0); 945974a30fSdrh rc = sqliteSelect(pParse, pSelect, SRT_Table, srcTab); 95daffd0e5Sdrh if( rc || pParse->nErr || sqlite_malloc_failed ) goto insert_cleanup; 965974a30fSdrh assert( pSelect->pEList ); 97967e8b73Sdrh nColumn = pSelect->pEList->nExpr; 985974a30fSdrh }else{ 99daffd0e5Sdrh assert( pList!=0 ); 1005974a30fSdrh srcTab = -1; 1015974a30fSdrh assert( pList ); 102967e8b73Sdrh nColumn = pList->nExpr; 1035974a30fSdrh } 1041ccde15dSdrh 1051ccde15dSdrh /* Make sure the number of columns in the source data matches the number 1061ccde15dSdrh ** of columns to be inserted into the table. 1071ccde15dSdrh */ 108967e8b73Sdrh if( pColumn==0 && nColumn!=pTab->nCol ){ 109cce7d176Sdrh char zNum1[30]; 110cce7d176Sdrh char zNum2[30]; 111967e8b73Sdrh sprintf(zNum1,"%d", nColumn); 112cce7d176Sdrh sprintf(zNum2,"%d", pTab->nCol); 113cce7d176Sdrh sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName, 114cce7d176Sdrh " has ", zNum2, " columns but ", 115cce7d176Sdrh zNum1, " values were supplied", 0); 116cce7d176Sdrh pParse->nErr++; 117cce7d176Sdrh goto insert_cleanup; 118cce7d176Sdrh } 119967e8b73Sdrh if( pColumn!=0 && nColumn!=pColumn->nId ){ 120cce7d176Sdrh char zNum1[30]; 121cce7d176Sdrh char zNum2[30]; 122967e8b73Sdrh sprintf(zNum1,"%d", nColumn); 123967e8b73Sdrh sprintf(zNum2,"%d", pColumn->nId); 124cce7d176Sdrh sqliteSetString(&pParse->zErrMsg, zNum1, " values for ", 125cce7d176Sdrh zNum2, " columns", 0); 126cce7d176Sdrh pParse->nErr++; 127cce7d176Sdrh goto insert_cleanup; 128cce7d176Sdrh } 1291ccde15dSdrh 1301ccde15dSdrh /* If the INSERT statement included an IDLIST term, then make sure 1311ccde15dSdrh ** all elements of the IDLIST really are columns of the table and 1321ccde15dSdrh ** remember the column indices. 1331ccde15dSdrh */ 134967e8b73Sdrh if( pColumn ){ 135967e8b73Sdrh for(i=0; i<pColumn->nId; i++){ 136967e8b73Sdrh pColumn->a[i].idx = -1; 137cce7d176Sdrh } 138967e8b73Sdrh for(i=0; i<pColumn->nId; i++){ 139cce7d176Sdrh for(j=0; j<pTab->nCol; j++){ 140967e8b73Sdrh if( sqliteStrICmp(pColumn->a[i].zName, pTab->aCol[j].zName)==0 ){ 141967e8b73Sdrh pColumn->a[i].idx = j; 142cce7d176Sdrh break; 143cce7d176Sdrh } 144cce7d176Sdrh } 145cce7d176Sdrh if( j>=pTab->nCol ){ 146cce7d176Sdrh sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName, 147967e8b73Sdrh " has no column named ", pColumn->a[i].zName, 0); 148cce7d176Sdrh pParse->nErr++; 149cce7d176Sdrh goto insert_cleanup; 150cce7d176Sdrh } 151cce7d176Sdrh } 152cce7d176Sdrh } 1531ccde15dSdrh 1541ccde15dSdrh /* Open cursors into the table that is received the new data and 1551ccde15dSdrh ** all indices of that table. 1561ccde15dSdrh */ 1575974a30fSdrh base = pParse->nTab; 158*ecdc7530Sdrh sqliteVdbeAddOp(v, OP_OpenWrite, base, pTab->tnum, pTab->zName, 0); 159bed8690fSdrh for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){ 160*ecdc7530Sdrh sqliteVdbeAddOp(v, OP_OpenWrite, idx+base, pIdx->tnum, pIdx->zName, 0); 1615974a30fSdrh } 1621ccde15dSdrh 1631ccde15dSdrh /* If the data source is a SELECT statement, then we have to create 1641ccde15dSdrh ** a loop because there might be multiple rows of data. If the data 1651ccde15dSdrh ** source is an expression list, then exactly one row will be inserted 1661ccde15dSdrh ** and the loop is not used. 1671ccde15dSdrh */ 1685974a30fSdrh if( srcTab>=0 ){ 1695974a30fSdrh sqliteVdbeAddOp(v, OP_Rewind, srcTab, 0, 0, 0); 1705974a30fSdrh iBreak = sqliteVdbeMakeLabel(v); 1715974a30fSdrh iCont = sqliteVdbeAddOp(v, OP_Next, srcTab, iBreak, 0, 0); 172bed8690fSdrh } 1731ccde15dSdrh 1741ccde15dSdrh /* Create a new entry in the table and fill it with data. 1751ccde15dSdrh */ 1763fc190ccSdrh sqliteVdbeAddOp(v, OP_NewRecno, base, 0, 0, 0); 177cce7d176Sdrh if( pTab->pIndex ){ 178cce7d176Sdrh sqliteVdbeAddOp(v, OP_Dup, 0, 0, 0, 0); 179cce7d176Sdrh } 180cce7d176Sdrh for(i=0; i<pTab->nCol; i++){ 181967e8b73Sdrh if( pColumn==0 ){ 182cce7d176Sdrh j = i; 183cce7d176Sdrh }else{ 184967e8b73Sdrh for(j=0; j<pColumn->nId; j++){ 185967e8b73Sdrh if( pColumn->a[j].idx==i ) break; 186cce7d176Sdrh } 187cce7d176Sdrh } 188967e8b73Sdrh if( pColumn && j>=pColumn->nId ){ 1897020f651Sdrh char *zDflt = pTab->aCol[i].zDflt; 190c61053b7Sdrh if( zDflt==0 ){ 191c61053b7Sdrh sqliteVdbeAddOp(v, OP_Null, 0, 0, 0, 0); 192c61053b7Sdrh }else{ 1937020f651Sdrh sqliteVdbeAddOp(v, OP_String, 0, 0, zDflt, 0); 194c61053b7Sdrh } 1955974a30fSdrh }else if( srcTab>=0 ){ 196be0072d2Sdrh sqliteVdbeAddOp(v, OP_Column, srcTab, i, 0, 0); 197cce7d176Sdrh }else{ 198cce7d176Sdrh sqliteExprCode(pParse, pList->a[j].pExpr); 199cce7d176Sdrh } 200cce7d176Sdrh } 201cce7d176Sdrh sqliteVdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0, 0, 0); 2025974a30fSdrh sqliteVdbeAddOp(v, OP_Put, base, 0, 0, 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 ){ 209cce7d176Sdrh sqliteVdbeAddOp(v, OP_Dup, 0, 0, 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 ){ 2217020f651Sdrh char *zDflt = pTab->aCol[idx].zDflt; 222c61053b7Sdrh if( zDflt==0 ){ 223c61053b7Sdrh sqliteVdbeAddOp(v, OP_Null, 0, 0, 0, 0); 224c61053b7Sdrh }else{ 2257020f651Sdrh sqliteVdbeAddOp(v, OP_String, 0, 0, zDflt, 0); 226c61053b7Sdrh } 2275974a30fSdrh }else if( srcTab>=0 ){ 228be0072d2Sdrh sqliteVdbeAddOp(v, OP_Column, srcTab, idx, 0, 0); 229cce7d176Sdrh }else{ 230cce7d176Sdrh sqliteExprCode(pParse, pList->a[j].pExpr); 231cce7d176Sdrh } 232cce7d176Sdrh } 2335e00f6c7Sdrh sqliteVdbeAddOp(v, OP_MakeIdxKey, pIdx->nColumn, 0, 0, 0); 2345974a30fSdrh sqliteVdbeAddOp(v, OP_PutIdx, idx+base, 0, 0, 0); 235cce7d176Sdrh } 2361ccde15dSdrh 2371ccde15dSdrh /* The bottom of the loop, if the data source is a SELECT statement 2381ccde15dSdrh */ 2395974a30fSdrh if( srcTab>=0 ){ 2405974a30fSdrh sqliteVdbeAddOp(v, OP_Goto, 0, iCont, 0, 0); 2415974a30fSdrh sqliteVdbeAddOp(v, OP_Noop, 0, 0, 0, iBreak); 242cce7d176Sdrh } 243*ecdc7530Sdrh if( (db->flags & SQLITE_InTrans)==0 ){ 2445e00f6c7Sdrh sqliteVdbeAddOp(v, OP_Commit, 0, 0, 0, 0); 2455e00f6c7Sdrh } 2465e00f6c7Sdrh 247cce7d176Sdrh 248cce7d176Sdrh insert_cleanup: 2495974a30fSdrh if( pList ) sqliteExprListDelete(pList); 2505974a30fSdrh if( pSelect ) sqliteSelectDelete(pSelect); 251967e8b73Sdrh sqliteIdListDelete(pColumn); 252cce7d176Sdrh } 253