1 /* 2 ** Copyright (c) 1999, 2000 D. Richard Hipp 3 ** 4 ** This program is free software; you can redistribute it and/or 5 ** modify it under the terms of the GNU General Public 6 ** License as published by the Free Software Foundation; either 7 ** version 2 of the License, or (at your option) any later version. 8 ** 9 ** This program is distributed in the hope that it will be useful, 10 ** but WITHOUT ANY WARRANTY; without even the implied warranty of 11 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 ** General Public License for more details. 13 ** 14 ** You should have received a copy of the GNU General Public 15 ** License along with this library; if not, write to the 16 ** Free Software Foundation, Inc., 59 Temple Place - Suite 330, 17 ** Boston, MA 02111-1307, USA. 18 ** 19 ** Author contact information: 20 ** [email protected] 21 ** http://www.hwaci.com/drh/ 22 ** 23 ************************************************************************* 24 ** This file contains C code routines that are called by the parser 25 ** to handle INSERT statements. 26 ** 27 ** $Id: insert.c,v 1.11 2000/06/21 13:59:12 drh Exp $ 28 */ 29 #include "sqliteInt.h" 30 31 /* 32 ** This routine is call to handle SQL of the following forms: 33 ** 34 ** insert into TABLE (IDLIST) values(EXPRLIST) 35 ** insert into TABLE (IDLIST) select 36 ** 37 ** The IDLIST following the table name is always optional. If omitted, 38 ** then a list of all columns for the table is substituted. The IDLIST 39 ** appears in the pColumn parameter. pColumn is NULL if IDLIST is omitted. 40 ** 41 ** The pList parameter holds EXPRLIST in the first form of the INSERT 42 ** statement above, and pSelect is NULL. For the second form, pList is 43 ** NULL and pSelect is a pointer to the select statement used to generate 44 ** data for the insert. 45 */ 46 void sqliteInsert( 47 Parse *pParse, /* Parser context */ 48 Token *pTableName, /* Name of table into which we are inserting */ 49 ExprList *pList, /* List of values to be inserted */ 50 Select *pSelect, /* A SELECT statement to use as the data source */ 51 IdList *pColumn /* Column names corresponding to IDLIST. */ 52 ){ 53 Table *pTab; /* The table to insert into */ 54 char *zTab; /* Name of the table into which we are inserting */ 55 int i, j, idx; /* Loop counters */ 56 Vdbe *v; /* Generate code into this virtual machine */ 57 Index *pIdx; /* For looping over indices of the table */ 58 int srcTab; /* Date comes from this temporary cursor if >=0 */ 59 int nColumn; /* Number of columns in the data */ 60 int base; /* First available cursor */ 61 int iCont, iBreak; /* Beginning and end of the loop over srcTab */ 62 63 /* Locate the table into which we will be inserting new information. 64 */ 65 zTab = sqliteTableNameFromToken(pTableName); 66 pTab = sqliteFindTable(pParse->db, zTab); 67 sqliteFree(zTab); 68 if( pTab==0 ){ 69 sqliteSetNString(&pParse->zErrMsg, "no such table: ", 0, 70 pTableName->z, pTableName->n, 0); 71 pParse->nErr++; 72 goto insert_cleanup; 73 } 74 if( pTab->readOnly ){ 75 sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName, 76 " may not be modified", 0); 77 pParse->nErr++; 78 goto insert_cleanup; 79 } 80 81 /* Allocate a VDBE 82 */ 83 v = sqliteGetVdbe(pParse); 84 if( v==0 ) goto insert_cleanup; 85 86 /* Figure out how many columns of data are supplied. If the data 87 ** is comming from a SELECT statement, then this step has to generate 88 ** all the code to implement the SELECT statement and leave the data 89 ** in a temporary table. If data is coming from an expression list, 90 ** then we just have to count the number of expressions. 91 */ 92 if( pSelect ){ 93 int rc; 94 srcTab = pParse->nTab++; 95 sqliteVdbeAddOp(v, OP_Open, srcTab, 1, 0, 0); 96 rc = sqliteSelect(pParse, pSelect, SRT_Table, srcTab); 97 if( rc ) goto insert_cleanup; 98 assert( pSelect->pEList ); 99 nColumn = pSelect->pEList->nExpr; 100 }else{ 101 srcTab = -1; 102 assert( pList ); 103 nColumn = pList->nExpr; 104 } 105 106 /* Make sure the number of columns in the source data matches the number 107 ** of columns to be inserted into the table. 108 */ 109 if( pColumn==0 && nColumn!=pTab->nCol ){ 110 char zNum1[30]; 111 char zNum2[30]; 112 sprintf(zNum1,"%d", nColumn); 113 sprintf(zNum2,"%d", pTab->nCol); 114 sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName, 115 " has ", zNum2, " columns but ", 116 zNum1, " values were supplied", 0); 117 pParse->nErr++; 118 goto insert_cleanup; 119 } 120 if( pColumn!=0 && nColumn!=pColumn->nId ){ 121 char zNum1[30]; 122 char zNum2[30]; 123 sprintf(zNum1,"%d", nColumn); 124 sprintf(zNum2,"%d", pColumn->nId); 125 sqliteSetString(&pParse->zErrMsg, zNum1, " values for ", 126 zNum2, " columns", 0); 127 pParse->nErr++; 128 goto insert_cleanup; 129 } 130 131 /* If the INSERT statement included an IDLIST term, then make sure 132 ** all elements of the IDLIST really are columns of the table and 133 ** remember the column indices. 134 */ 135 if( pColumn ){ 136 for(i=0; i<pColumn->nId; i++){ 137 pColumn->a[i].idx = -1; 138 } 139 for(i=0; i<pColumn->nId; i++){ 140 for(j=0; j<pTab->nCol; j++){ 141 if( sqliteStrICmp(pColumn->a[i].zName, pTab->aCol[j].zName)==0 ){ 142 pColumn->a[i].idx = j; 143 break; 144 } 145 } 146 if( j>=pTab->nCol ){ 147 sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName, 148 " has no column named ", pColumn->a[i].zName, 0); 149 pParse->nErr++; 150 goto insert_cleanup; 151 } 152 } 153 } 154 155 /* Open cursors into the table that is received the new data and 156 ** all indices of that table. 157 */ 158 base = pParse->nTab; 159 sqliteVdbeAddOp(v, OP_Open, base, 1, pTab->zName, 0); 160 for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){ 161 sqliteVdbeAddOp(v, OP_Open, idx+base, 1, pIdx->zName, 0); 162 } 163 164 /* If the data source is a SELECT statement, then we have to create 165 ** a loop because there might be multiple rows of data. If the data 166 ** source is an expression list, then exactly one row will be inserted 167 ** and the loop is not used. 168 */ 169 if( srcTab>=0 ){ 170 sqliteVdbeAddOp(v, OP_Rewind, srcTab, 0, 0, 0); 171 iBreak = sqliteVdbeMakeLabel(v); 172 iCont = sqliteVdbeAddOp(v, OP_Next, srcTab, iBreak, 0, 0); 173 } 174 175 /* Create a new entry in the table and fill it with data. 176 */ 177 sqliteVdbeAddOp(v, OP_New, 0, 0, 0, 0); 178 if( pTab->pIndex ){ 179 sqliteVdbeAddOp(v, OP_Dup, 0, 0, 0, 0); 180 } 181 for(i=0; i<pTab->nCol; i++){ 182 if( pColumn==0 ){ 183 j = i; 184 }else{ 185 for(j=0; j<pColumn->nId; j++){ 186 if( pColumn->a[j].idx==i ) break; 187 } 188 } 189 if( pColumn && j>=pColumn->nId ){ 190 char *zDflt = pTab->aCol[i].zDflt; 191 if( zDflt==0 ){ 192 sqliteVdbeAddOp(v, OP_Null, 0, 0, 0, 0); 193 }else{ 194 sqliteVdbeAddOp(v, OP_String, 0, 0, zDflt, 0); 195 } 196 }else if( srcTab>=0 ){ 197 sqliteVdbeAddOp(v, OP_Field, srcTab, i, 0, 0); 198 }else{ 199 sqliteExprCode(pParse, pList->a[j].pExpr); 200 } 201 } 202 sqliteVdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0, 0, 0); 203 sqliteVdbeAddOp(v, OP_Put, base, 0, 0, 0); 204 205 /* Create appropriate entries for the new data row in all indices 206 ** of the table. 207 */ 208 for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){ 209 if( pIdx->pNext ){ 210 sqliteVdbeAddOp(v, OP_Dup, 0, 0, 0, 0); 211 } 212 for(i=0; i<pIdx->nColumn; i++){ 213 int idx = pIdx->aiColumn[i]; 214 if( pColumn==0 ){ 215 j = idx; 216 }else{ 217 for(j=0; j<pColumn->nId; j++){ 218 if( pColumn->a[j].idx==idx ) break; 219 } 220 } 221 if( pColumn && j>=pColumn->nId ){ 222 char *zDflt = pTab->aCol[idx].zDflt; 223 if( zDflt==0 ){ 224 sqliteVdbeAddOp(v, OP_Null, 0, 0, 0, 0); 225 }else{ 226 sqliteVdbeAddOp(v, OP_String, 0, 0, zDflt, 0); 227 } 228 }else if( srcTab>=0 ){ 229 sqliteVdbeAddOp(v, OP_Field, srcTab, idx, 0, 0); 230 }else{ 231 sqliteExprCode(pParse, pList->a[j].pExpr); 232 } 233 } 234 sqliteVdbeAddOp(v, OP_MakeKey, pIdx->nColumn, 0, 0, 0); 235 sqliteVdbeAddOp(v, OP_PutIdx, idx+base, 0, 0, 0); 236 } 237 238 /* The bottom of the loop, if the data source is a SELECT statement 239 */ 240 if( srcTab>=0 ){ 241 sqliteVdbeAddOp(v, OP_Goto, 0, iCont, 0, 0); 242 sqliteVdbeAddOp(v, OP_Noop, 0, 0, 0, iBreak); 243 } 244 245 insert_cleanup: 246 if( pList ) sqliteExprListDelete(pList); 247 if( pSelect ) sqliteSelectDelete(pSelect); 248 sqliteIdListDelete(pColumn); 249 } 250