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 | bed8690 | 2000-06-02 13:27:59 +0000 | [diff] [blame] | 27 | ** $Id: insert.c,v 1.3 2000/06/02 13:27:59 drh Exp $ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 28 | */ |
| 29 | #include "sqliteInt.h" |
| 30 | |
| 31 | /* |
| 32 | ** This routine is call to handle SQL of the following form: |
| 33 | ** |
| 34 | ** insert into TABLE (IDLIST) values(EXPRLIST) |
| 35 | ** |
| 36 | ** The parameters are the table name and the expression list. |
| 37 | */ |
| 38 | void sqliteInsert( |
| 39 | Parse *pParse, /* Parser context */ |
| 40 | Token *pTableName, /* Name of table into which we are inserting */ |
| 41 | ExprList *pList, /* List of values to be inserted */ |
| 42 | IdList *pField /* Field name corresponding to pList. Might be NULL */ |
| 43 | ){ |
| 44 | Table *pTab; |
| 45 | char *zTab; |
drh | bed8690 | 2000-06-02 13:27:59 +0000 | [diff] [blame] | 46 | int i, j, idx; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 47 | Vdbe *v; |
| 48 | |
| 49 | zTab = sqliteTableNameFromToken(pTableName); |
| 50 | pTab = sqliteFindTable(pParse->db, zTab); |
| 51 | sqliteFree(zTab); |
| 52 | if( pTab==0 ){ |
| 53 | sqliteSetNString(&pParse->zErrMsg, "no such table: ", 0, |
| 54 | pTableName->z, pTableName->n, 0); |
| 55 | pParse->nErr++; |
| 56 | goto insert_cleanup; |
| 57 | } |
| 58 | if( pTab->readOnly ){ |
| 59 | sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName, |
| 60 | " may not be modified", 0); |
| 61 | pParse->nErr++; |
| 62 | goto insert_cleanup; |
| 63 | } |
| 64 | if( pField==0 && pList->nExpr!=pTab->nCol ){ |
| 65 | char zNum1[30]; |
| 66 | char zNum2[30]; |
| 67 | sprintf(zNum1,"%d", pList->nExpr); |
| 68 | sprintf(zNum2,"%d", pTab->nCol); |
| 69 | sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName, |
| 70 | " has ", zNum2, " columns but ", |
| 71 | zNum1, " values were supplied", 0); |
| 72 | pParse->nErr++; |
| 73 | goto insert_cleanup; |
| 74 | } |
| 75 | if( pField!=0 && pList->nExpr!=pField->nId ){ |
| 76 | char zNum1[30]; |
| 77 | char zNum2[30]; |
| 78 | sprintf(zNum1,"%d", pList->nExpr); |
| 79 | sprintf(zNum2,"%d", pField->nId); |
| 80 | sqliteSetString(&pParse->zErrMsg, zNum1, " values for ", |
| 81 | zNum2, " columns", 0); |
| 82 | pParse->nErr++; |
| 83 | goto insert_cleanup; |
| 84 | } |
| 85 | if( pField ){ |
| 86 | for(i=0; i<pField->nId; i++){ |
| 87 | pField->a[i].idx = -1; |
| 88 | } |
| 89 | for(i=0; i<pField->nId; i++){ |
| 90 | for(j=0; j<pTab->nCol; j++){ |
| 91 | if( sqliteStrICmp(pField->a[i].zName, pTab->azCol[j])==0 ){ |
| 92 | pField->a[i].idx = j; |
| 93 | break; |
| 94 | } |
| 95 | } |
| 96 | if( j>=pTab->nCol ){ |
| 97 | sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName, |
| 98 | " has no column named ", pField->a[i].zName, 0); |
| 99 | pParse->nErr++; |
| 100 | goto insert_cleanup; |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | v = pParse->pVdbe = sqliteVdbeCreate(pParse->db->pBe); |
| 105 | if( v ){ |
| 106 | Index *pIdx; |
drh | 58b9576 | 2000-06-02 01:17:37 +0000 | [diff] [blame] | 107 | sqliteVdbeAddOp(v, OP_Open, 0, 1, pTab->zName, 0); |
drh | bed8690 | 2000-06-02 13:27:59 +0000 | [diff] [blame] | 108 | for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){ |
| 109 | sqliteVdbeAddOp(v, OP_Open, idx, 1, pIdx->zName, 0); |
| 110 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 111 | sqliteVdbeAddOp(v, OP_New, 0, 0, 0, 0); |
| 112 | if( pTab->pIndex ){ |
| 113 | sqliteVdbeAddOp(v, OP_Dup, 0, 0, 0, 0); |
| 114 | } |
| 115 | for(i=0; i<pTab->nCol; i++){ |
| 116 | if( pField==0 ){ |
| 117 | j = i; |
| 118 | }else{ |
| 119 | for(j=0; j<pField->nId; j++){ |
| 120 | if( pField->a[j].idx==i ) break; |
| 121 | } |
| 122 | } |
| 123 | if( pField && j>=pField->nId ){ |
| 124 | sqliteVdbeAddOp(v, OP_String, 0, 0, "", 0); |
| 125 | }else{ |
| 126 | sqliteExprCode(pParse, pList->a[j].pExpr); |
| 127 | } |
| 128 | } |
| 129 | sqliteVdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0, 0, 0); |
| 130 | sqliteVdbeAddOp(v, OP_Put, 0, 0, 0, 0); |
| 131 | sqliteVdbeAddOp(v, OP_Close, 0, 0, 0, 0); |
drh | bed8690 | 2000-06-02 13:27:59 +0000 | [diff] [blame] | 132 | for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 133 | if( pIdx->pNext ){ |
| 134 | sqliteVdbeAddOp(v, OP_Dup, 0, 0, 0, 0); |
| 135 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 136 | for(i=0; i<pIdx->nField; i++){ |
| 137 | int idx = pIdx->aiField[i]; |
| 138 | if( pField==0 ){ |
| 139 | j = idx; |
| 140 | }else{ |
| 141 | for(j=0; j<pField->nId; j++){ |
| 142 | if( pField->a[j].idx==idx ) break; |
| 143 | } |
| 144 | } |
| 145 | if( pField && j>=pField->nId ){ |
| 146 | sqliteVdbeAddOp(v, OP_String, 0, 0, "", 0); |
| 147 | }else{ |
| 148 | sqliteExprCode(pParse, pList->a[j].pExpr); |
| 149 | } |
| 150 | } |
| 151 | sqliteVdbeAddOp(v, OP_MakeKey, pIdx->nField, 0, 0, 0); |
drh | bed8690 | 2000-06-02 13:27:59 +0000 | [diff] [blame] | 152 | sqliteVdbeAddOp(v, OP_PutIdx, idx, 0, 0, 0); |
| 153 | sqliteVdbeAddOp(v, OP_Close, idx, 0, 0, 0); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 154 | } |
| 155 | } |
| 156 | |
| 157 | insert_cleanup: |
| 158 | sqliteExprListDelete(pList); |
| 159 | sqliteIdListDelete(pField); |
| 160 | } |