drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 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 | ** drh@hwaci.com |
| 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 | ** |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 27 | ** $Id: insert.c,v 1.10 2000/06/17 13:12:40 drh Exp $ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 28 | */ |
| 29 | #include "sqliteInt.h" |
| 30 | |
| 31 | /* |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 32 | ** This routine is call to handle SQL of the following forms: |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 33 | ** |
| 34 | ** insert into TABLE (IDLIST) values(EXPRLIST) |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 35 | ** insert into TABLE (IDLIST) select |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 36 | ** |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 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 pField parameter. pField 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. |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 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 */ |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 50 | Select *pSelect, /* A SELECT statement to use as the data source */ |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 51 | IdList *pField /* Field names corresponding to IDLIST. */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 52 | ){ |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 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 nField; /* Number of columns in the data */ |
| 60 | int base; /* First available cursor */ |
| 61 | int iCont, iBreak; /* Beginning and end of the loop over srcTab */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 62 | |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 63 | /* Locate the table into which we will be inserting new information. |
| 64 | */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 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 | } |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 80 | |
| 81 | /* Allocate a VDBE |
| 82 | */ |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 83 | v = sqliteGetVdbe(pParse); |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 84 | if( v==0 ) goto insert_cleanup; |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 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 | */ |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 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 | nField = pSelect->pEList->nExpr; |
| 100 | }else{ |
| 101 | srcTab = -1; |
| 102 | assert( pList ); |
| 103 | nField = pList->nExpr; |
| 104 | } |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 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 | */ |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 109 | if( pField==0 && nField!=pTab->nCol ){ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 110 | char zNum1[30]; |
| 111 | char zNum2[30]; |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 112 | sprintf(zNum1,"%d", nField); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 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 | } |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 120 | if( pField!=0 && nField!=pField->nId ){ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 121 | char zNum1[30]; |
| 122 | char zNum2[30]; |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 123 | sprintf(zNum1,"%d", nField); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 124 | sprintf(zNum2,"%d", pField->nId); |
| 125 | sqliteSetString(&pParse->zErrMsg, zNum1, " values for ", |
| 126 | zNum2, " columns", 0); |
| 127 | pParse->nErr++; |
| 128 | goto insert_cleanup; |
| 129 | } |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 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 | */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 135 | if( pField ){ |
| 136 | for(i=0; i<pField->nId; i++){ |
| 137 | pField->a[i].idx = -1; |
| 138 | } |
| 139 | for(i=0; i<pField->nId; i++){ |
| 140 | for(j=0; j<pTab->nCol; j++){ |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 141 | if( sqliteStrICmp(pField->a[i].zName, pTab->aCol[j].zName)==0 ){ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 142 | pField->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 ", pField->a[i].zName, 0); |
| 149 | pParse->nErr++; |
| 150 | goto insert_cleanup; |
| 151 | } |
| 152 | } |
| 153 | } |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 154 | |
| 155 | /* Open cursors into the table that is received the new data and |
| 156 | ** all indices of that table. |
| 157 | */ |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 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); |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 162 | } |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 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 | */ |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 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 | } |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 174 | |
| 175 | /* Create a new entry in the table and fill it with data. |
| 176 | */ |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 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( pField==0 ){ |
| 183 | j = i; |
| 184 | }else{ |
| 185 | for(j=0; j<pField->nId; j++){ |
| 186 | if( pField->a[j].idx==i ) break; |
| 187 | } |
drh | bed8690 | 2000-06-02 13:27:59 +0000 | [diff] [blame] | 188 | } |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 189 | if( pField && j>=pField->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); |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 204 | |
| 205 | /* Create appropriate entries for the new data row in all indices |
| 206 | ** of the table. |
| 207 | */ |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 208 | for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){ |
| 209 | if( pIdx->pNext ){ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 210 | sqliteVdbeAddOp(v, OP_Dup, 0, 0, 0, 0); |
| 211 | } |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 212 | for(i=0; i<pIdx->nField; i++){ |
| 213 | int idx = pIdx->aiField[i]; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 214 | if( pField==0 ){ |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 215 | j = idx; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 216 | }else{ |
| 217 | for(j=0; j<pField->nId; j++){ |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 218 | if( pField->a[j].idx==idx ) break; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 219 | } |
| 220 | } |
| 221 | if( pField && j>=pField->nId ){ |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 222 | char *zDflt = pTab->aCol[idx].zDflt; |
drh | c61053b | 2000-06-04 12:58:36 +0000 | [diff] [blame] | 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 | } |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 228 | }else if( srcTab>=0 ){ |
drh | cc85b41 | 2000-06-07 15:11:27 +0000 | [diff] [blame] | 229 | sqliteVdbeAddOp(v, OP_Field, srcTab, idx, 0, 0); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 230 | }else{ |
| 231 | sqliteExprCode(pParse, pList->a[j].pExpr); |
| 232 | } |
| 233 | } |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 234 | sqliteVdbeAddOp(v, OP_MakeKey, pIdx->nField, 0, 0, 0); |
| 235 | sqliteVdbeAddOp(v, OP_PutIdx, idx+base, 0, 0, 0); |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 236 | } |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 237 | |
| 238 | /* The bottom of the loop, if the data source is a SELECT statement |
| 239 | */ |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 240 | if( srcTab>=0 ){ |
| 241 | sqliteVdbeAddOp(v, OP_Goto, 0, iCont, 0, 0); |
| 242 | sqliteVdbeAddOp(v, OP_Noop, 0, 0, 0, iBreak); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | insert_cleanup: |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 246 | if( pList ) sqliteExprListDelete(pList); |
| 247 | if( pSelect ) sqliteSelectDelete(pSelect); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 248 | sqliteIdListDelete(pField); |
| 249 | } |