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 | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame^] | 27 | ** $Id: insert.c,v 1.17 2001/09/15 00:57:29 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 |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 39 | ** appears in the pColumn parameter. pColumn is NULL if IDLIST is omitted. |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 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 | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 51 | IdList *pColumn /* Column 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 */ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 59 | int nColumn; /* Number of columns in the data */ |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 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 | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 63 | if( pParse->nErr || sqlite_malloc_failed ) goto insert_cleanup; |
| 64 | |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 65 | /* Locate the table into which we will be inserting new information. |
| 66 | */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 67 | zTab = sqliteTableNameFromToken(pTableName); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 68 | if( zTab==0 ) goto insert_cleanup; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 69 | pTab = sqliteFindTable(pParse->db, zTab); |
| 70 | sqliteFree(zTab); |
| 71 | if( pTab==0 ){ |
| 72 | sqliteSetNString(&pParse->zErrMsg, "no such table: ", 0, |
| 73 | pTableName->z, pTableName->n, 0); |
| 74 | pParse->nErr++; |
| 75 | goto insert_cleanup; |
| 76 | } |
| 77 | if( pTab->readOnly ){ |
| 78 | sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName, |
| 79 | " may not be modified", 0); |
| 80 | pParse->nErr++; |
| 81 | goto insert_cleanup; |
| 82 | } |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 83 | |
| 84 | /* Allocate a VDBE |
| 85 | */ |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 86 | v = sqliteGetVdbe(pParse); |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 87 | if( v==0 ) goto insert_cleanup; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 88 | if( (pParse->db->flags & SQLITE_InTrans)==0 ){ |
| 89 | sqliteVdbeAddOp(v, OP_Transaction, 0, 0, 0, 0); |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame^] | 90 | sqliteVdbeAddOp(v, OP_VerifyCookie, pParse->db->schema_cookie, 0, 0, 0); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 91 | } |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 92 | |
| 93 | /* Figure out how many columns of data are supplied. If the data |
| 94 | ** is comming from a SELECT statement, then this step has to generate |
| 95 | ** all the code to implement the SELECT statement and leave the data |
| 96 | ** in a temporary table. If data is coming from an expression list, |
| 97 | ** then we just have to count the number of expressions. |
| 98 | */ |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 99 | if( pSelect ){ |
| 100 | int rc; |
| 101 | srcTab = pParse->nTab++; |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 102 | sqliteVdbeAddOp(v, OP_OpenTemp, srcTab, 0, 0, 0); |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 103 | rc = sqliteSelect(pParse, pSelect, SRT_Table, srcTab); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 104 | if( rc || pParse->nErr || sqlite_malloc_failed ) goto insert_cleanup; |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 105 | assert( pSelect->pEList ); |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 106 | nColumn = pSelect->pEList->nExpr; |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 107 | }else{ |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 108 | assert( pList!=0 ); |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 109 | srcTab = -1; |
| 110 | assert( pList ); |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 111 | nColumn = pList->nExpr; |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 112 | } |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 113 | |
| 114 | /* Make sure the number of columns in the source data matches the number |
| 115 | ** of columns to be inserted into the table. |
| 116 | */ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 117 | if( pColumn==0 && nColumn!=pTab->nCol ){ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 118 | char zNum1[30]; |
| 119 | char zNum2[30]; |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 120 | sprintf(zNum1,"%d", nColumn); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 121 | sprintf(zNum2,"%d", pTab->nCol); |
| 122 | sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName, |
| 123 | " has ", zNum2, " columns but ", |
| 124 | zNum1, " values were supplied", 0); |
| 125 | pParse->nErr++; |
| 126 | goto insert_cleanup; |
| 127 | } |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 128 | if( pColumn!=0 && nColumn!=pColumn->nId ){ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 129 | char zNum1[30]; |
| 130 | char zNum2[30]; |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 131 | sprintf(zNum1,"%d", nColumn); |
| 132 | sprintf(zNum2,"%d", pColumn->nId); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 133 | sqliteSetString(&pParse->zErrMsg, zNum1, " values for ", |
| 134 | zNum2, " columns", 0); |
| 135 | pParse->nErr++; |
| 136 | goto insert_cleanup; |
| 137 | } |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 138 | |
| 139 | /* If the INSERT statement included an IDLIST term, then make sure |
| 140 | ** all elements of the IDLIST really are columns of the table and |
| 141 | ** remember the column indices. |
| 142 | */ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 143 | if( pColumn ){ |
| 144 | for(i=0; i<pColumn->nId; i++){ |
| 145 | pColumn->a[i].idx = -1; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 146 | } |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 147 | for(i=0; i<pColumn->nId; i++){ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 148 | for(j=0; j<pTab->nCol; j++){ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 149 | if( sqliteStrICmp(pColumn->a[i].zName, pTab->aCol[j].zName)==0 ){ |
| 150 | pColumn->a[i].idx = j; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 151 | break; |
| 152 | } |
| 153 | } |
| 154 | if( j>=pTab->nCol ){ |
| 155 | sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName, |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 156 | " has no column named ", pColumn->a[i].zName, 0); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 157 | pParse->nErr++; |
| 158 | goto insert_cleanup; |
| 159 | } |
| 160 | } |
| 161 | } |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 162 | |
| 163 | /* Open cursors into the table that is received the new data and |
| 164 | ** all indices of that table. |
| 165 | */ |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 166 | base = pParse->nTab; |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 167 | sqliteVdbeAddOp(v, OP_Open, base, pTab->tnum, pTab->zName, 0); |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 168 | for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){ |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 169 | sqliteVdbeAddOp(v, OP_Open, idx+base, pIdx->tnum, pIdx->zName, 0); |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 170 | } |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 171 | |
| 172 | /* If the data source is a SELECT statement, then we have to create |
| 173 | ** a loop because there might be multiple rows of data. If the data |
| 174 | ** source is an expression list, then exactly one row will be inserted |
| 175 | ** and the loop is not used. |
| 176 | */ |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 177 | if( srcTab>=0 ){ |
| 178 | sqliteVdbeAddOp(v, OP_Rewind, srcTab, 0, 0, 0); |
| 179 | iBreak = sqliteVdbeMakeLabel(v); |
| 180 | iCont = sqliteVdbeAddOp(v, OP_Next, srcTab, iBreak, 0, 0); |
| 181 | } |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 182 | |
| 183 | /* Create a new entry in the table and fill it with data. |
| 184 | */ |
drh | 3fc190c | 2001-09-14 03:24:23 +0000 | [diff] [blame] | 185 | sqliteVdbeAddOp(v, OP_NewRecno, base, 0, 0, 0); |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 186 | if( pTab->pIndex ){ |
| 187 | sqliteVdbeAddOp(v, OP_Dup, 0, 0, 0, 0); |
| 188 | } |
| 189 | for(i=0; i<pTab->nCol; i++){ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 190 | if( pColumn==0 ){ |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 191 | j = i; |
| 192 | }else{ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 193 | for(j=0; j<pColumn->nId; j++){ |
| 194 | if( pColumn->a[j].idx==i ) break; |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 195 | } |
drh | bed8690 | 2000-06-02 13:27:59 +0000 | [diff] [blame] | 196 | } |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 197 | if( pColumn && j>=pColumn->nId ){ |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 198 | char *zDflt = pTab->aCol[i].zDflt; |
| 199 | if( zDflt==0 ){ |
| 200 | sqliteVdbeAddOp(v, OP_Null, 0, 0, 0, 0); |
| 201 | }else{ |
| 202 | sqliteVdbeAddOp(v, OP_String, 0, 0, zDflt, 0); |
| 203 | } |
| 204 | }else if( srcTab>=0 ){ |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 205 | sqliteVdbeAddOp(v, OP_Column, srcTab, i, 0, 0); |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 206 | }else{ |
| 207 | sqliteExprCode(pParse, pList->a[j].pExpr); |
| 208 | } |
| 209 | } |
| 210 | sqliteVdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0, 0, 0); |
| 211 | sqliteVdbeAddOp(v, OP_Put, base, 0, 0, 0); |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 212 | |
| 213 | /* Create appropriate entries for the new data row in all indices |
| 214 | ** of the table. |
| 215 | */ |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 216 | for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){ |
| 217 | if( pIdx->pNext ){ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 218 | sqliteVdbeAddOp(v, OP_Dup, 0, 0, 0, 0); |
| 219 | } |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 220 | for(i=0; i<pIdx->nColumn; i++){ |
| 221 | int idx = pIdx->aiColumn[i]; |
| 222 | if( pColumn==0 ){ |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 223 | j = idx; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 224 | }else{ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 225 | for(j=0; j<pColumn->nId; j++){ |
| 226 | if( pColumn->a[j].idx==idx ) break; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 227 | } |
| 228 | } |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 229 | if( pColumn && j>=pColumn->nId ){ |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 230 | char *zDflt = pTab->aCol[idx].zDflt; |
drh | c61053b | 2000-06-04 12:58:36 +0000 | [diff] [blame] | 231 | if( zDflt==0 ){ |
| 232 | sqliteVdbeAddOp(v, OP_Null, 0, 0, 0, 0); |
| 233 | }else{ |
| 234 | sqliteVdbeAddOp(v, OP_String, 0, 0, zDflt, 0); |
| 235 | } |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 236 | }else if( srcTab>=0 ){ |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 237 | sqliteVdbeAddOp(v, OP_Column, srcTab, idx, 0, 0); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 238 | }else{ |
| 239 | sqliteExprCode(pParse, pList->a[j].pExpr); |
| 240 | } |
| 241 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 242 | sqliteVdbeAddOp(v, OP_MakeIdxKey, pIdx->nColumn, 0, 0, 0); |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 243 | sqliteVdbeAddOp(v, OP_PutIdx, idx+base, 0, 0, 0); |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 244 | } |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 245 | |
| 246 | /* The bottom of the loop, if the data source is a SELECT statement |
| 247 | */ |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 248 | if( srcTab>=0 ){ |
| 249 | sqliteVdbeAddOp(v, OP_Goto, 0, iCont, 0, 0); |
| 250 | sqliteVdbeAddOp(v, OP_Noop, 0, 0, 0, iBreak); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 251 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 252 | if( (pParse->db->flags & SQLITE_InTrans)==0 ){ |
| 253 | sqliteVdbeAddOp(v, OP_Commit, 0, 0, 0, 0); |
| 254 | } |
| 255 | |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 256 | |
| 257 | insert_cleanup: |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 258 | if( pList ) sqliteExprListDelete(pList); |
| 259 | if( pSelect ) sqliteSelectDelete(pSelect); |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 260 | sqliteIdListDelete(pColumn); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 261 | } |