drh | 7589723 | 2000-05-29 14:26:00 +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 |
drh | bed8690 | 2000-06-02 13:27:59 +0000 | [diff] [blame] | 25 | ** when syntax rules are reduced. The routines in this file handle |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 26 | ** the following kinds of syntax: |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 27 | ** |
drh | bed8690 | 2000-06-02 13:27:59 +0000 | [diff] [blame] | 28 | ** CREATE TABLE |
| 29 | ** DROP TABLE |
| 30 | ** CREATE INDEX |
| 31 | ** DROP INDEX |
| 32 | ** creating expressions and ID lists |
| 33 | ** COPY |
| 34 | ** VACUUM |
| 35 | ** |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame^] | 36 | ** $Id: build.c,v 1.32 2001/09/13 21:53:10 drh Exp $ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 37 | */ |
| 38 | #include "sqliteInt.h" |
| 39 | |
| 40 | /* |
| 41 | ** This routine is called after a single SQL statement has been |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 42 | ** parsed and we want to execute the VDBE code to implement |
| 43 | ** that statement. Prior action routines should have already |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 44 | ** constructed VDBE code to do the work of the SQL statement. |
| 45 | ** This routine just has to execute the VDBE code. |
| 46 | ** |
| 47 | ** Note that if an error occurred, it might be the case that |
| 48 | ** no VDBE code was generated. |
| 49 | */ |
| 50 | void sqliteExec(Parse *pParse){ |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 51 | int rc = SQLITE_OK; |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 52 | sqlite *db = pParse->db; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 53 | if( sqlite_malloc_failed ) return; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 54 | if( pParse->pVdbe ){ |
| 55 | if( pParse->explain ){ |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 56 | rc = sqliteVdbeList(pParse->pVdbe, pParse->xCallback, pParse->pArg, |
| 57 | &pParse->zErrMsg); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 58 | }else{ |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 59 | FILE *trace = (db->flags & SQLITE_VdbeTrace)!=0 ? stderr : 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 60 | sqliteVdbeTrace(pParse->pVdbe, trace); |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 61 | rc = sqliteVdbeExec(pParse->pVdbe, pParse->xCallback, pParse->pArg, |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 62 | &pParse->zErrMsg, db->pBusyArg, |
| 63 | db->xBusyCallback); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 64 | } |
| 65 | sqliteVdbeDelete(pParse->pVdbe); |
| 66 | pParse->pVdbe = 0; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 67 | pParse->colNamesSet = 0; |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 68 | pParse->rc = rc; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 69 | } |
| 70 | } |
| 71 | |
| 72 | /* |
| 73 | ** Construct a new expression node and return a pointer to it. |
| 74 | */ |
| 75 | Expr *sqliteExpr(int op, Expr *pLeft, Expr *pRight, Token *pToken){ |
| 76 | Expr *pNew; |
| 77 | pNew = sqliteMalloc( sizeof(Expr) ); |
| 78 | if( pNew==0 ) return 0; |
| 79 | pNew->op = op; |
| 80 | pNew->pLeft = pLeft; |
| 81 | pNew->pRight = pRight; |
| 82 | if( pToken ){ |
| 83 | pNew->token = *pToken; |
| 84 | }else{ |
| 85 | pNew->token.z = ""; |
| 86 | pNew->token.n = 0; |
| 87 | } |
drh | e1b6a5b | 2000-07-29 13:06:59 +0000 | [diff] [blame] | 88 | if( pLeft && pRight ){ |
| 89 | sqliteExprSpan(pNew, &pLeft->span, &pRight->span); |
| 90 | }else{ |
| 91 | pNew->span = pNew->token; |
| 92 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 93 | return pNew; |
| 94 | } |
| 95 | |
| 96 | /* |
drh | e1b6a5b | 2000-07-29 13:06:59 +0000 | [diff] [blame] | 97 | ** Set the Expr.token field of the given expression to span all |
| 98 | ** text between the two given tokens. |
| 99 | */ |
| 100 | void sqliteExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){ |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 101 | if( pExpr ){ |
| 102 | pExpr->span.z = pLeft->z; |
| 103 | pExpr->span.n = pRight->n + (int)pRight->z - (int)pLeft->z; |
| 104 | } |
drh | e1b6a5b | 2000-07-29 13:06:59 +0000 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 108 | ** Construct a new expression node for a function with multiple |
| 109 | ** arguments. |
| 110 | */ |
| 111 | Expr *sqliteExprFunction(ExprList *pList, Token *pToken){ |
| 112 | Expr *pNew; |
| 113 | pNew = sqliteMalloc( sizeof(Expr) ); |
| 114 | if( pNew==0 ) return 0; |
| 115 | pNew->op = TK_FUNCTION; |
| 116 | pNew->pList = pList; |
| 117 | if( pToken ){ |
| 118 | pNew->token = *pToken; |
| 119 | }else{ |
| 120 | pNew->token.z = ""; |
| 121 | pNew->token.n = 0; |
| 122 | } |
| 123 | return pNew; |
| 124 | } |
| 125 | |
| 126 | /* |
| 127 | ** Recursively delete an expression tree. |
| 128 | */ |
| 129 | void sqliteExprDelete(Expr *p){ |
| 130 | if( p==0 ) return; |
| 131 | if( p->pLeft ) sqliteExprDelete(p->pLeft); |
| 132 | if( p->pRight ) sqliteExprDelete(p->pRight); |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 133 | if( p->pList ) sqliteExprListDelete(p->pList); |
| 134 | if( p->pSelect ) sqliteSelectDelete(p->pSelect); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 135 | sqliteFree(p); |
| 136 | } |
| 137 | |
| 138 | /* |
| 139 | ** Locate the in-memory structure that describes the |
| 140 | ** format of a particular database table given the name |
| 141 | ** of that table. Return NULL if not found. |
| 142 | */ |
| 143 | Table *sqliteFindTable(sqlite *db, char *zName){ |
| 144 | Table *pTable; |
| 145 | int h; |
| 146 | |
| 147 | h = sqliteHashNoCase(zName, 0) % N_HASH; |
| 148 | for(pTable=db->apTblHash[h]; pTable; pTable=pTable->pHash){ |
| 149 | if( sqliteStrICmp(pTable->zName, zName)==0 ) return pTable; |
| 150 | } |
| 151 | return 0; |
| 152 | } |
| 153 | |
| 154 | /* |
| 155 | ** Locate the in-memory structure that describes the |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 156 | ** format of a particular index given the name |
| 157 | ** of that index. Return NULL if not found. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 158 | */ |
| 159 | Index *sqliteFindIndex(sqlite *db, char *zName){ |
| 160 | Index *p; |
| 161 | int h; |
| 162 | |
| 163 | h = sqliteHashNoCase(zName, 0) % N_HASH; |
| 164 | for(p=db->apIdxHash[h]; p; p=p->pHash){ |
| 165 | if( sqliteStrICmp(p->zName, zName)==0 ) return p; |
| 166 | } |
| 167 | return 0; |
| 168 | } |
| 169 | |
| 170 | /* |
| 171 | ** Remove the given index from the index hash table, and free |
| 172 | ** its memory structures. |
| 173 | ** |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 174 | ** The index is removed from the database hash table if db!=NULL. |
| 175 | ** But it is not unlinked from the Table that is being indexed. |
| 176 | ** Unlinking from the Table must be done by the calling function. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 177 | */ |
| 178 | static void sqliteDeleteIndex(sqlite *db, Index *pIndex){ |
| 179 | int h; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 180 | if( pIndex->zName && db ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 181 | h = sqliteHashNoCase(pIndex->zName, 0) % N_HASH; |
| 182 | if( db->apIdxHash[h]==pIndex ){ |
| 183 | db->apIdxHash[h] = pIndex->pHash; |
| 184 | }else{ |
| 185 | Index *p; |
| 186 | for(p=db->apIdxHash[h]; p && p->pHash!=pIndex; p=p->pHash){} |
| 187 | if( p && p->pHash==pIndex ){ |
| 188 | p->pHash = pIndex->pHash; |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | sqliteFree(pIndex); |
| 193 | } |
| 194 | |
| 195 | /* |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 196 | ** Unlink the given index from its table, then remove |
| 197 | ** the index from the index hash table, and free its memory |
| 198 | ** structures. |
| 199 | */ |
| 200 | static void sqliteUnlinkAndDeleteIndex(sqlite *db, Index *pIndex){ |
| 201 | if( pIndex->pTable->pIndex==pIndex ){ |
| 202 | pIndex->pTable->pIndex = pIndex->pNext; |
| 203 | }else{ |
| 204 | Index *p; |
| 205 | for(p=pIndex->pTable->pIndex; p && p->pNext!=pIndex; p=p->pNext){} |
| 206 | if( p && p->pNext==pIndex ){ |
| 207 | p->pNext = pIndex->pNext; |
| 208 | } |
| 209 | } |
| 210 | sqliteDeleteIndex(db, pIndex); |
| 211 | } |
| 212 | |
| 213 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 214 | ** Remove the memory data structures associated with the given |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 215 | ** Table. No changes are made to disk by this routine. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 216 | ** |
| 217 | ** This routine just deletes the data structure. It does not unlink |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 218 | ** the table data structure from the hash table. But it does destroy |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 219 | ** memory structures of the indices associated with the table. |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 220 | ** |
| 221 | ** Indices associated with the table are unlinked from the "db" |
| 222 | ** data structure if db!=NULL. If db==NULL, indices attached to |
| 223 | ** the table are deleted, but it is assumed they have already been |
| 224 | ** unlinked. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 225 | */ |
| 226 | void sqliteDeleteTable(sqlite *db, Table *pTable){ |
| 227 | int i; |
| 228 | Index *pIndex, *pNext; |
| 229 | if( pTable==0 ) return; |
| 230 | for(i=0; i<pTable->nCol; i++){ |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 231 | sqliteFree(pTable->aCol[i].zName); |
| 232 | sqliteFree(pTable->aCol[i].zDflt); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 233 | } |
| 234 | for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){ |
| 235 | pNext = pIndex->pNext; |
| 236 | sqliteDeleteIndex(db, pIndex); |
| 237 | } |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 238 | sqliteFree(pTable->zName); |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 239 | sqliteFree(pTable->aCol); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 240 | sqliteFree(pTable); |
| 241 | } |
| 242 | |
| 243 | /* |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame^] | 244 | ** Unlink the given table from the hash tables and the delete the |
| 245 | ** table structure and all its indices. |
| 246 | */ |
| 247 | static void sqliteUnlinkAndDeleteTable(sqlite *db, Table *pTable){ |
| 248 | if( pTable->zName && db ){ |
| 249 | int h = sqliteHashNoCase(pTable->zName, 0) % N_HASH; |
| 250 | if( db->apTblHash[h]==pTable ){ |
| 251 | db->apTblHash[h] = pTable->pHash; |
| 252 | }else{ |
| 253 | Table *p; |
| 254 | for(p=db->apTblHash[h]; p && p->pHash!=pTable; p=p->pHash){} |
| 255 | if( p && p->pHash==pTable ){ |
| 256 | p->pHash = pTable->pHash; |
| 257 | } |
| 258 | } |
| 259 | } |
| 260 | sqliteDeleteTable(db, pTable); |
| 261 | } |
| 262 | |
| 263 | /* |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 264 | ** Check all Tables and Indexes in the internal hash table and commit |
| 265 | ** any additions or deletions to those hash tables. |
| 266 | ** |
| 267 | ** When executing CREATE TABLE and CREATE INDEX statements, the Table |
| 268 | ** and Index structures are created and added to the hash tables, but |
| 269 | ** the "isCommit" field is not set. This routine sets those fields. |
| 270 | ** When executing DROP TABLE and DROP INDEX, the "isDelete" fields of |
| 271 | ** Table and Index structures is set but the structures are not unlinked |
| 272 | ** from the hash tables nor deallocated. This routine handles that |
| 273 | ** deallocation. |
| 274 | ** |
| 275 | ** See also: sqliteRollbackInternalChanges() |
| 276 | */ |
| 277 | void sqliteCommitInternalChanges(sqlite *db){ |
| 278 | int i; |
| 279 | if( (db->flags & SQLITE_InternChanges)==0 ) return; |
| 280 | for(i=0; i<N_HASH; i++){ |
| 281 | Table *pTable, *pNext; |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 282 | for(pTable = db->apTblHash[i]; pTable; pTable=pNext){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 283 | pNext = pTable->pHash; |
| 284 | if( pTable->isDelete ){ |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame^] | 285 | sqliteUnlinkAndDeleteTable(db, pTable); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 286 | }else if( pTable->isCommit==0 ){ |
| 287 | pTable->isCommit = 1; |
| 288 | } |
| 289 | } |
| 290 | } |
| 291 | for(i=0; i<N_HASH; i++){ |
| 292 | Index *pIndex, *pNext; |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 293 | for(pIndex = db->apIdxHash[i]; pIndex; pIndex=pNext){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 294 | pNext = pIndex->pHash; |
| 295 | if( pIndex->isDelete ){ |
| 296 | sqliteUnlinkAndDeleteIndex(db, pIndex); |
| 297 | }else if( pIndex->isCommit==0 ){ |
| 298 | pIndex->isCommit = 1; |
| 299 | } |
| 300 | } |
| 301 | } |
| 302 | db->flags &= ~SQLITE_InternChanges; |
| 303 | } |
| 304 | |
| 305 | /* |
| 306 | ** This routine runs when one or more CREATE TABLE, CREATE INDEX, |
| 307 | ** DROP TABLE, or DROP INDEX statements get rolled back. The |
| 308 | ** additions or deletions of Table and Index structures in the |
| 309 | ** internal hash tables are undone. |
| 310 | ** |
| 311 | ** See also: sqliteCommitInternalChanges() |
| 312 | */ |
| 313 | void sqliteRollbackInternalChanges(sqlite *db){ |
| 314 | int i; |
| 315 | if( (db->flags & SQLITE_InternChanges)==0 ) return; |
| 316 | for(i=0; i<N_HASH; i++){ |
| 317 | Table *pTable, *pNext; |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 318 | for(pTable = db->apTblHash[i]; pTable; pTable=pNext){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 319 | pNext = pTable->pHash; |
| 320 | if( !pTable->isCommit ){ |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame^] | 321 | sqliteUnlinkAndDeleteTable(db, pTable); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 322 | }else if( pTable->isDelete ){ |
| 323 | pTable->isDelete = 0; |
| 324 | } |
| 325 | } |
| 326 | } |
| 327 | for(i=0; i<N_HASH; i++){ |
| 328 | Index *pIndex, *pNext; |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 329 | for(pIndex = db->apIdxHash[i]; pIndex; pIndex=pNext){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 330 | pNext = pIndex->pHash; |
| 331 | if( !pIndex->isCommit ){ |
| 332 | sqliteUnlinkAndDeleteIndex(db, pIndex); |
| 333 | }else if( pIndex->isDelete ){ |
| 334 | pIndex->isDelete = 0; |
| 335 | } |
| 336 | } |
| 337 | } |
| 338 | db->flags &= ~SQLITE_InternChanges; |
| 339 | } |
| 340 | |
| 341 | /* |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 342 | ** Construct the name of a user table or index from a token. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 343 | ** |
| 344 | ** Space to hold the name is obtained from sqliteMalloc() and must |
| 345 | ** be freed by the calling function. |
| 346 | */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 347 | char *sqliteTableNameFromToken(Token *pName){ |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 348 | char *zName = sqliteStrNDup(pName->z, pName->n); |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 349 | sqliteDequote(zName); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 350 | return zName; |
| 351 | } |
| 352 | |
| 353 | /* |
| 354 | ** Begin constructing a new table representation in memory. This is |
| 355 | ** the first of several action routines that get called in response |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 356 | ** to a CREATE TABLE statement. In particular, this routine is called |
| 357 | ** after seeing tokens "CREATE" and "TABLE" and the table name. The |
| 358 | ** pStart token is the CREATE and pName is the table name. |
| 359 | ** |
| 360 | ** The new table is constructed in files of the pParse structure. As |
| 361 | ** more of the CREATE TABLE statement is parsed, additional action |
| 362 | ** routines are called to build up more of the table. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 363 | */ |
| 364 | void sqliteStartTable(Parse *pParse, Token *pStart, Token *pName){ |
| 365 | Table *pTable; |
| 366 | char *zName; |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 367 | sqlite *db = pParse->db; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 368 | |
| 369 | pParse->sFirstToken = *pStart; |
| 370 | zName = sqliteTableNameFromToken(pName); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 371 | if( zName==0 ) return; |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 372 | pTable = sqliteFindTable(db, zName); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 373 | if( pTable!=0 ){ |
drh | 1d37e28 | 2000-05-30 03:12:21 +0000 | [diff] [blame] | 374 | sqliteSetNString(&pParse->zErrMsg, "table ", 0, pName->z, pName->n, |
| 375 | " already exists", 0, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 376 | sqliteFree(zName); |
| 377 | pParse->nErr++; |
| 378 | return; |
| 379 | } |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 380 | if( sqliteFindIndex(db, zName) ){ |
drh | 1d37e28 | 2000-05-30 03:12:21 +0000 | [diff] [blame] | 381 | sqliteSetString(&pParse->zErrMsg, "there is already an index named ", |
| 382 | zName, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 383 | sqliteFree(zName); |
| 384 | pParse->nErr++; |
| 385 | return; |
| 386 | } |
| 387 | pTable = sqliteMalloc( sizeof(Table) ); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 388 | if( pTable==0 ) return; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 389 | pTable->zName = zName; |
| 390 | pTable->pHash = 0; |
| 391 | pTable->nCol = 0; |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 392 | pTable->aCol = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 393 | pTable->pIndex = 0; |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 394 | if( pParse->pNewTable ) sqliteDeleteTable(db, pParse->pNewTable); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 395 | pParse->pNewTable = pTable; |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 396 | if( !pParse->initFlag && (db->flags & SQLITE_InTrans)==0 ){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 397 | Vdbe *v = sqliteGetVdbe(pParse); |
| 398 | if( v ){ |
| 399 | sqliteVdbeAddOp(v, OP_Transaction, 0, 0, 0, 0); |
| 400 | } |
| 401 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | /* |
| 405 | ** Add a new column to the table currently being constructed. |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 406 | ** |
| 407 | ** The parser calls this routine once for each column declaration |
| 408 | ** in a CREATE TABLE statement. sqliteStartTable() gets called |
| 409 | ** first to get things going. Then this routine is called for each |
| 410 | ** column. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 411 | */ |
| 412 | void sqliteAddColumn(Parse *pParse, Token *pName){ |
| 413 | Table *p; |
| 414 | char **pz; |
| 415 | if( (p = pParse->pNewTable)==0 ) return; |
| 416 | if( (p->nCol & 0x7)==0 ){ |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 417 | p->aCol = sqliteRealloc( p->aCol, (p->nCol+8)*sizeof(p->aCol[0])); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 418 | if( p->aCol==0 ){ |
| 419 | p->nCol = 0; |
| 420 | return; |
| 421 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 422 | } |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 423 | memset(&p->aCol[p->nCol], 0, sizeof(p->aCol[0])); |
| 424 | pz = &p->aCol[p->nCol++].zName; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 425 | sqliteSetNString(pz, pName->z, pName->n, 0); |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 426 | sqliteDequote(*pz); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | /* |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 430 | ** The given token is the default value for the last column added to |
| 431 | ** the table currently under construction. If "minusFlag" is true, it |
| 432 | ** means the value token was preceded by a minus sign. |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 433 | ** |
| 434 | ** This routine is called by the parser while in the middle of |
| 435 | ** parsing a CREATE TABLE statement. |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 436 | */ |
| 437 | void sqliteAddDefaultValue(Parse *pParse, Token *pVal, int minusFlag){ |
| 438 | Table *p; |
| 439 | int i; |
| 440 | char **pz; |
| 441 | if( (p = pParse->pNewTable)==0 ) return; |
| 442 | i = p->nCol-1; |
| 443 | pz = &p->aCol[i].zDflt; |
| 444 | if( minusFlag ){ |
| 445 | sqliteSetNString(pz, "-", 1, pVal->z, pVal->n, 0); |
| 446 | }else{ |
| 447 | sqliteSetNString(pz, pVal->z, pVal->n, 0); |
| 448 | } |
| 449 | sqliteDequote(*pz); |
| 450 | } |
| 451 | |
| 452 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 453 | ** This routine is called to report the final ")" that terminates |
| 454 | ** a CREATE TABLE statement. |
| 455 | ** |
| 456 | ** The table structure is added to the internal hash tables. |
| 457 | ** |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 458 | ** An entry for the table is made in the master table on disk, |
| 459 | ** unless initFlag==1. When initFlag==1, it means we are reading |
| 460 | ** the master table because we just connected to the database, so |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 461 | ** the entry for this table already exists in the master table. |
| 462 | ** We do not want to create it again. |
| 463 | */ |
| 464 | void sqliteEndTable(Parse *pParse, Token *pEnd){ |
| 465 | Table *p; |
| 466 | int h; |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 467 | sqlite *db = pParse->db; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 468 | |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 469 | if( pEnd==0 || pParse->nErr || sqlite_malloc_failed ) return; |
drh | 2803757 | 2000-08-02 13:47:41 +0000 | [diff] [blame] | 470 | p = pParse->pNewTable; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 471 | if( p==0 ) return; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 472 | |
| 473 | /* Add the table to the in-memory representation of the database |
| 474 | */ |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 475 | if( pParse->explain==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 476 | h = sqliteHashNoCase(p->zName, 0) % N_HASH; |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 477 | p->pHash = db->apTblHash[h]; |
| 478 | db->apTblHash[h] = p; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 479 | pParse->pNewTable = 0; |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 480 | db->nTable++; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 481 | db->flags |= SQLITE_InternChanges; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 482 | } |
| 483 | |
drh | d78eeee | 2001-09-13 16:18:53 +0000 | [diff] [blame] | 484 | /* If the initFlag is 1 it means we are reading the SQL off the |
| 485 | ** "sqlite_master" table on the disk. So do not write to the disk |
| 486 | ** again. Extract the table number from the pParse->newTnum field. |
| 487 | */ |
| 488 | if( pParse->initFlag ){ |
| 489 | p->tnum = pParse->newTnum; |
| 490 | } |
| 491 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 492 | /* If not initializing, then create the table on disk. |
| 493 | */ |
| 494 | if( !pParse->initFlag ){ |
| 495 | static VdbeOp addTable[] = { |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame^] | 496 | { OP_Open, 0, 2, MASTER_NAME}, |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 497 | { OP_NewRecno, 0, 0, 0}, |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 498 | { OP_String, 0, 0, "table" }, |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 499 | { OP_String, 0, 0, 0}, /* 3 */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 500 | { OP_CreateTable, 0, 0, 0}, |
drh | 305cea6 | 2000-05-29 17:44:25 +0000 | [diff] [blame] | 501 | { OP_String, 0, 0, 0}, /* 5 */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 502 | { OP_String, 0, 0, 0}, /* 6 */ |
drh | d78eeee | 2001-09-13 16:18:53 +0000 | [diff] [blame] | 503 | { OP_MakeRecord, 5, 0, 0}, |
drh | 2803757 | 2000-08-02 13:47:41 +0000 | [diff] [blame] | 504 | { OP_Put, 0, 0, 0}, |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 505 | }; |
| 506 | int n, base; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 507 | Vdbe *v; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 508 | |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 509 | v = sqliteGetVdbe(pParse); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 510 | if( v==0 ) return; |
| 511 | n = (int)pEnd->z - (int)pParse->sFirstToken.z + 1; |
| 512 | base = sqliteVdbeAddOpList(v, ArraySize(addTable), addTable); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 513 | sqliteVdbeChangeP3(v, base+3, p->zName, 0); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 514 | sqliteVdbeTableRootAddr(v, &p->tnum); |
| 515 | sqliteVdbeChangeP3(v, base+5, p->zName, 0); |
| 516 | sqliteVdbeChangeP3(v, base+6, pParse->sFirstToken.z, n); |
drh | 2803757 | 2000-08-02 13:47:41 +0000 | [diff] [blame] | 517 | sqliteVdbeAddOp(v, OP_Close, 0, 0, 0, 0); |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame^] | 518 | if( p->pIndex ){ |
| 519 | /* If the table has a primary key, create an index in the database |
| 520 | ** for that key. */ |
| 521 | Index *pIndex = p->pIndex; |
| 522 | assert( pIndex->pNext==0 ); |
| 523 | assert( pIndex->tnum==0 ); |
| 524 | sqliteVdbeAddOp(v, OP_CreateIndex, 0, 0, 0, 0), |
| 525 | sqliteVdbeIndexRootAddr(v, &pIndex->tnum); |
| 526 | } |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 527 | if( (db->flags & SQLITE_InTrans)==0 ){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 528 | sqliteVdbeAddOp(v, OP_Commit, 0, 0, 0, 0); |
| 529 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 530 | } |
| 531 | } |
| 532 | |
| 533 | /* |
| 534 | ** Given a token, look up a table with that name. If not found, leave |
| 535 | ** an error for the parser to find and return NULL. |
| 536 | */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 537 | Table *sqliteTableFromToken(Parse *pParse, Token *pTok){ |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 538 | char *zName; |
| 539 | Table *pTab; |
| 540 | zName = sqliteTableNameFromToken(pTok); |
| 541 | if( zName==0 ) return 0; |
| 542 | pTab = sqliteFindTable(pParse->db, zName); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 543 | sqliteFree(zName); |
| 544 | if( pTab==0 ){ |
drh | b24fcbe | 2000-05-29 23:30:50 +0000 | [diff] [blame] | 545 | sqliteSetNString(&pParse->zErrMsg, "no such table: ", 0, |
| 546 | pTok->z, pTok->n, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 547 | pParse->nErr++; |
| 548 | } |
| 549 | return pTab; |
| 550 | } |
| 551 | |
| 552 | /* |
| 553 | ** This routine is called to do the work of a DROP TABLE statement. |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 554 | ** pName is the name of the table to be dropped. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 555 | */ |
| 556 | void sqliteDropTable(Parse *pParse, Token *pName){ |
| 557 | Table *pTable; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 558 | Vdbe *v; |
| 559 | int base; |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame^] | 560 | sqlite *db = pParse->db; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 561 | |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 562 | if( pParse->nErr || sqlite_malloc_failed ) return; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 563 | pTable = sqliteTableFromToken(pParse, pName); |
| 564 | if( pTable==0 ) return; |
| 565 | if( pTable->readOnly ){ |
drh | 1d37e28 | 2000-05-30 03:12:21 +0000 | [diff] [blame] | 566 | sqliteSetString(&pParse->zErrMsg, "table ", pTable->zName, |
| 567 | " may not be dropped", 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 568 | pParse->nErr++; |
| 569 | return; |
| 570 | } |
| 571 | |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 572 | /* Generate code to remove the table from the master table |
| 573 | ** on disk. |
| 574 | */ |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 575 | v = sqliteGetVdbe(pParse); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 576 | if( v ){ |
| 577 | static VdbeOp dropTable[] = { |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame^] | 578 | { OP_Open, 0, 2, MASTER_NAME}, |
drh | d78eeee | 2001-09-13 16:18:53 +0000 | [diff] [blame] | 579 | { OP_Rewind, 0, 0, 0}, |
| 580 | { OP_String, 0, 0, 0}, /* 2 */ |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame^] | 581 | { OP_Next, 0, ADDR(9), 0}, /* 3 */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 582 | { OP_Dup, 0, 0, 0}, |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 583 | { OP_Column, 0, 3, 0}, |
drh | d78eeee | 2001-09-13 16:18:53 +0000 | [diff] [blame] | 584 | { OP_Ne, 0, ADDR(3), 0}, |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 585 | { OP_Delete, 0, 0, 0}, |
drh | d78eeee | 2001-09-13 16:18:53 +0000 | [diff] [blame] | 586 | { OP_Goto, 0, ADDR(3), 0}, |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame^] | 587 | { OP_Destroy, 0, 0, 0}, /* 9 */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 588 | { OP_Close, 0, 0, 0}, |
| 589 | }; |
| 590 | Index *pIdx; |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame^] | 591 | if( (db->flags & SQLITE_InTrans)==0 ){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 592 | sqliteVdbeAddOp(v, OP_Transaction, 0, 0, 0, 0); |
| 593 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 594 | base = sqliteVdbeAddOpList(v, ArraySize(dropTable), dropTable); |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame^] | 595 | sqliteVdbeChangeP3(v, base+2, pTable->zName, 0); |
| 596 | sqliteVdbeChangeP1(v, base+9, pTable->tnum); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 597 | for(pIdx=pTable->pIndex; pIdx; pIdx=pIdx->pNext){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 598 | sqliteVdbeAddOp(v, OP_Destroy, pIdx->tnum, 0, 0, 0); |
| 599 | } |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame^] | 600 | if( (db->flags & SQLITE_InTrans)==0 ){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 601 | sqliteVdbeAddOp(v, OP_Commit, 0, 0, 0, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 602 | } |
| 603 | } |
| 604 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 605 | /* Mark the in-memory Table structure as being deleted. The actually |
| 606 | ** deletion occurs inside of sqliteCommitInternalChanges(). |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 607 | ** |
| 608 | ** Exception: if the SQL statement began with the EXPLAIN keyword, |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 609 | ** then no changes should be made. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 610 | */ |
| 611 | if( !pParse->explain ){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 612 | pTable->isDelete = 1; |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame^] | 613 | db->flags |= SQLITE_InternChanges; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 614 | } |
| 615 | } |
| 616 | |
| 617 | /* |
| 618 | ** Create a new index for an SQL table. pIndex is the name of the index |
| 619 | ** and pTable is the name of the table that is to be indexed. Both will |
| 620 | ** be NULL for a primary key. In that case, use pParse->pNewTable as the |
| 621 | ** table to be indexed. |
| 622 | ** |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 623 | ** pList is a list of columns to be indexed. pList will be NULL if the |
| 624 | ** most recently added column of the table is labeled as the primary key. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 625 | */ |
| 626 | void sqliteCreateIndex( |
| 627 | Parse *pParse, /* All information about this parse */ |
| 628 | Token *pName, /* Name of the index. May be NULL */ |
| 629 | Token *pTable, /* Name of the table to index. Use pParse->pNewTable if 0 */ |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 630 | IdList *pList, /* A list of columns to be indexed */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 631 | Token *pStart, /* The CREATE token that begins a CREATE TABLE statement */ |
| 632 | Token *pEnd /* The ")" that closes the CREATE INDEX statement */ |
| 633 | ){ |
| 634 | Table *pTab; /* Table to be indexed */ |
| 635 | Index *pIndex; /* The index to be created */ |
| 636 | char *zName = 0; |
| 637 | int i, j, h; |
| 638 | Token nullId; /* Fake token for an empty ID list */ |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 639 | sqlite *db = pParse->db; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 640 | |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 641 | if( pParse->nErr || sqlite_malloc_failed ) goto exit_create_index; |
| 642 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 643 | /* |
| 644 | ** Find the table that is to be indexed. Return early if not found. |
| 645 | */ |
| 646 | if( pTable!=0 ){ |
| 647 | pTab = sqliteTableFromToken(pParse, pTable); |
| 648 | }else{ |
| 649 | pTab = pParse->pNewTable; |
| 650 | } |
| 651 | if( pTab==0 || pParse->nErr ) goto exit_create_index; |
| 652 | if( pTab->readOnly ){ |
drh | b24fcbe | 2000-05-29 23:30:50 +0000 | [diff] [blame] | 653 | sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName, |
| 654 | " may not have new indices added", 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 655 | pParse->nErr++; |
| 656 | goto exit_create_index; |
| 657 | } |
| 658 | |
| 659 | /* |
| 660 | ** Find the name of the index. Make sure there is not already another |
| 661 | ** index or table with the same name. |
| 662 | */ |
| 663 | if( pName ){ |
| 664 | zName = sqliteTableNameFromToken(pName); |
| 665 | }else{ |
| 666 | zName = 0; |
| 667 | sqliteSetString(&zName, pTab->zName, "__primary_key", 0); |
| 668 | } |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 669 | if( zName==0 ) goto exit_create_index; |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 670 | if( sqliteFindIndex(db, zName) ){ |
drh | 1d37e28 | 2000-05-30 03:12:21 +0000 | [diff] [blame] | 671 | sqliteSetString(&pParse->zErrMsg, "index ", zName, |
| 672 | " already exists", 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 673 | pParse->nErr++; |
| 674 | goto exit_create_index; |
| 675 | } |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 676 | if( sqliteFindTable(db, zName) ){ |
drh | 1d37e28 | 2000-05-30 03:12:21 +0000 | [diff] [blame] | 677 | sqliteSetString(&pParse->zErrMsg, "there is already a table named ", |
| 678 | zName, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 679 | pParse->nErr++; |
| 680 | goto exit_create_index; |
| 681 | } |
| 682 | |
| 683 | /* If pList==0, it means this routine was called to make a primary |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 684 | ** key out of the last column added to the table under construction. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 685 | ** So create a fake list to simulate this. |
| 686 | */ |
| 687 | if( pList==0 ){ |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 688 | nullId.z = pTab->aCol[pTab->nCol-1].zName; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 689 | nullId.n = strlen(nullId.z); |
| 690 | pList = sqliteIdListAppend(0, &nullId); |
| 691 | if( pList==0 ) goto exit_create_index; |
| 692 | } |
| 693 | |
| 694 | /* |
| 695 | ** Allocate the index structure. |
| 696 | */ |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 697 | pIndex = sqliteMalloc( sizeof(Index) + strlen(zName) + 1 + |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 698 | sizeof(int)*pList->nId ); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 699 | if( pIndex==0 ) goto exit_create_index; |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 700 | pIndex->aiColumn = (int*)&pIndex[1]; |
| 701 | pIndex->zName = (char*)&pIndex->aiColumn[pList->nId]; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 702 | strcpy(pIndex->zName, zName); |
| 703 | pIndex->pTable = pTab; |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 704 | pIndex->nColumn = pList->nId; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 705 | |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 706 | /* Scan the names of the columns of the table to be indexed and |
| 707 | ** load the column indices into the Index structure. Report an error |
| 708 | ** if any column is not found. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 709 | */ |
| 710 | for(i=0; i<pList->nId; i++){ |
| 711 | for(j=0; j<pTab->nCol; j++){ |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 712 | if( sqliteStrICmp(pList->a[i].zName, pTab->aCol[j].zName)==0 ) break; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 713 | } |
| 714 | if( j>=pTab->nCol ){ |
drh | b24fcbe | 2000-05-29 23:30:50 +0000 | [diff] [blame] | 715 | sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName, |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 716 | " has no column named ", pList->a[i].zName, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 717 | pParse->nErr++; |
| 718 | sqliteFree(pIndex); |
| 719 | goto exit_create_index; |
| 720 | } |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 721 | pIndex->aiColumn[i] = j; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 722 | } |
| 723 | |
| 724 | /* Link the new Index structure to its table and to the other |
| 725 | ** in-memory database structures. |
| 726 | */ |
| 727 | if( pParse->explain==0 ){ |
| 728 | h = sqliteHashNoCase(pIndex->zName, 0) % N_HASH; |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 729 | pIndex->pHash = db->apIdxHash[h]; |
| 730 | db->apIdxHash[h] = pIndex; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 731 | pIndex->pNext = pTab->pIndex; |
| 732 | pTab->pIndex = pIndex; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 733 | db->flags |= SQLITE_InternChanges; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 734 | } |
| 735 | |
drh | d78eeee | 2001-09-13 16:18:53 +0000 | [diff] [blame] | 736 | /* If the initFlag is 1 it means we are reading the SQL off the |
| 737 | ** "sqlite_master" table on the disk. So do not write to the disk |
| 738 | ** again. Extract the table number from the pParse->newTnum field. |
| 739 | */ |
| 740 | if( pParse->initFlag ){ |
| 741 | pIndex->tnum = pParse->newTnum; |
| 742 | } |
| 743 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 744 | /* If the initFlag is 0 then create the index on disk. This |
| 745 | ** involves writing the index into the master table and filling in the |
| 746 | ** index with the current table contents. |
| 747 | ** |
| 748 | ** The initFlag is 0 when the user first enters a CREATE INDEX |
| 749 | ** command. The initFlag is 1 when a database is opened and |
| 750 | ** CREATE INDEX statements are read out of the master table. In |
| 751 | ** the latter case the index already exists on disk, which is why |
| 752 | ** we don't want to recreate it. |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame^] | 753 | ** |
| 754 | ** If pTable==0 it means this index is generated as a primary key |
| 755 | ** and those does not have a CREATE INDEX statement to add to the |
| 756 | ** master table. Also, since primary keys are created at the same |
| 757 | ** time as tables, the table will be empty so there is no need to |
| 758 | ** initialize the index. Hence, skip all the code generation if |
| 759 | ** pTable==0. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 760 | */ |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame^] | 761 | else if( pParse->initFlag==0 && pTable!=0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 762 | static VdbeOp addTable[] = { |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame^] | 763 | { OP_Open, 2, 2, MASTER_NAME}, |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 764 | { OP_NewRecno, 2, 0, 0}, |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 765 | { OP_String, 0, 0, "index"}, |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 766 | { OP_String, 0, 0, 0}, /* 3 */ |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame^] | 767 | { OP_CreateIndex, 1, 0, 0}, |
| 768 | { OP_Dup, 0, 0, 0}, |
| 769 | { OP_Open, 1, 0, 0}, /* 6 */ |
| 770 | { OP_String, 0, 0, 0}, /* 7 */ |
| 771 | { OP_String, 0, 0, 0}, /* 8 */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 772 | { OP_MakeRecord, 5, 0, 0}, |
drh | bed8690 | 2000-06-02 13:27:59 +0000 | [diff] [blame] | 773 | { OP_Put, 2, 0, 0}, |
| 774 | { OP_Close, 2, 0, 0}, |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 775 | }; |
| 776 | int n; |
| 777 | Vdbe *v = pParse->pVdbe; |
| 778 | int lbl1, lbl2; |
| 779 | int i; |
| 780 | |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 781 | v = sqliteGetVdbe(pParse); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 782 | if( v==0 ) goto exit_create_index; |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 783 | if( pTable!=0 && (db->flags & SQLITE_InTrans)==0 ){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 784 | sqliteVdbeAddOp(v, OP_Transaction, 0, 0, 0, 0); |
| 785 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 786 | if( pStart && pEnd ){ |
| 787 | int base; |
| 788 | n = (int)pEnd->z - (int)pStart->z + 1; |
| 789 | base = sqliteVdbeAddOpList(v, ArraySize(addTable), addTable); |
drh | b24fcbe | 2000-05-29 23:30:50 +0000 | [diff] [blame] | 790 | sqliteVdbeChangeP3(v, base+3, pIndex->zName, 0); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 791 | sqliteVdbeIndexRootAddr(v, &pIndex->tnum); |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame^] | 792 | sqliteVdbeChangeP3(v, base+6, pIndex->zName, 0); |
| 793 | sqliteVdbeChangeP3(v, base+7, pTab->zName, 0); |
| 794 | sqliteVdbeChangeP3(v, base+8, pStart->z, n); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 795 | } |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame^] | 796 | sqliteVdbeAddOp(v, OP_Open, 0, pTab->tnum, pTab->zName, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 797 | lbl1 = sqliteVdbeMakeLabel(v); |
| 798 | lbl2 = sqliteVdbeMakeLabel(v); |
drh | d78eeee | 2001-09-13 16:18:53 +0000 | [diff] [blame] | 799 | sqliteVdbeAddOp(v, OP_Rewind, 0, 0, 0, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 800 | sqliteVdbeAddOp(v, OP_Next, 0, lbl2, 0, lbl1); |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 801 | sqliteVdbeAddOp(v, OP_Recno, 0, 0, 0, 0); |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 802 | for(i=0; i<pIndex->nColumn; i++){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 803 | sqliteVdbeAddOp(v, OP_Column, 0, pIndex->aiColumn[i], 0, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 804 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 805 | sqliteVdbeAddOp(v, OP_MakeIdxKey, pIndex->nColumn, 0, 0, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 806 | sqliteVdbeAddOp(v, OP_PutIdx, 1, 0, 0, 0); |
| 807 | sqliteVdbeAddOp(v, OP_Goto, 0, lbl1, 0, 0); |
| 808 | sqliteVdbeAddOp(v, OP_Noop, 0, 0, 0, lbl2); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 809 | sqliteVdbeAddOp(v, OP_Close, 1, 0, 0, 0); |
drh | bed8690 | 2000-06-02 13:27:59 +0000 | [diff] [blame] | 810 | sqliteVdbeAddOp(v, OP_Close, 0, 0, 0, 0); |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 811 | if( pTable!=0 && (db->flags & SQLITE_InTrans)==0 ){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 812 | sqliteVdbeAddOp(v, OP_Commit, 0, 0, 0, 0); |
| 813 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 814 | } |
| 815 | |
| 816 | /* Reclaim memory on an EXPLAIN call. |
| 817 | */ |
| 818 | if( pParse->explain ){ |
| 819 | sqliteFree(pIndex); |
| 820 | } |
| 821 | |
| 822 | /* Clean up before exiting */ |
| 823 | exit_create_index: |
| 824 | sqliteIdListDelete(pList); |
| 825 | sqliteFree(zName); |
| 826 | return; |
| 827 | } |
| 828 | |
| 829 | /* |
| 830 | ** This routine will drop an existing named index. |
| 831 | */ |
| 832 | void sqliteDropIndex(Parse *pParse, Token *pName){ |
| 833 | Index *pIndex; |
| 834 | char *zName; |
| 835 | Vdbe *v; |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 836 | sqlite *db = pParse->db; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 837 | |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 838 | if( pParse->nErr || sqlite_malloc_failed ) return; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 839 | zName = sqliteTableNameFromToken(pName); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 840 | if( zName==0 ) return; |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 841 | pIndex = sqliteFindIndex(db, zName); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 842 | sqliteFree(zName); |
| 843 | if( pIndex==0 ){ |
drh | 1d37e28 | 2000-05-30 03:12:21 +0000 | [diff] [blame] | 844 | sqliteSetNString(&pParse->zErrMsg, "no such index: ", 0, |
| 845 | pName->z, pName->n, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 846 | pParse->nErr++; |
| 847 | return; |
| 848 | } |
| 849 | |
| 850 | /* Generate code to remove the index and from the master table */ |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 851 | v = sqliteGetVdbe(pParse); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 852 | if( v ){ |
| 853 | static VdbeOp dropIndex[] = { |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame^] | 854 | { OP_Open, 0, 2, MASTER_NAME}, |
drh | d78eeee | 2001-09-13 16:18:53 +0000 | [diff] [blame] | 855 | { OP_Rewind, 0, 0, 0}, |
| 856 | { OP_String, 0, 0, 0}, /* 2 */ |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame^] | 857 | { OP_Next, 0, ADDR(8), 0}, /* 3 */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 858 | { OP_Dup, 0, 0, 0}, |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 859 | { OP_Column, 0, 1, 0}, |
drh | d78eeee | 2001-09-13 16:18:53 +0000 | [diff] [blame] | 860 | { OP_Ne, 0, ADDR(3), 0}, |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 861 | { OP_Delete, 0, 0, 0}, |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame^] | 862 | { OP_Destroy, 0, 0, 0}, /* 8 */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 863 | { OP_Close, 0, 0, 0}, |
| 864 | }; |
| 865 | int base; |
| 866 | |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 867 | if( (db->flags & SQLITE_InTrans)==0 ){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 868 | sqliteVdbeAddOp(v, OP_Transaction, 0, 0, 0, 0); |
| 869 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 870 | base = sqliteVdbeAddOpList(v, ArraySize(dropIndex), dropIndex); |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame^] | 871 | sqliteVdbeChangeP1(v, base+8, pIndex->tnum); |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 872 | if( (db->flags & SQLITE_InTrans)==0 ){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 873 | sqliteVdbeAddOp(v, OP_Commit, 0, 0, 0, 0); |
| 874 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 875 | } |
| 876 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 877 | /* Mark the internal Index structure for deletion by the |
| 878 | ** sqliteCommitInternalChanges routine. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 879 | */ |
| 880 | if( !pParse->explain ){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 881 | pIndex->isDelete = 1; |
| 882 | db->flags |= SQLITE_InternChanges; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 883 | } |
| 884 | } |
| 885 | |
| 886 | /* |
| 887 | ** Add a new element to the end of an expression list. If pList is |
| 888 | ** initially NULL, then create a new expression list. |
| 889 | */ |
| 890 | ExprList *sqliteExprListAppend(ExprList *pList, Expr *pExpr, Token *pName){ |
| 891 | int i; |
| 892 | if( pList==0 ){ |
| 893 | pList = sqliteMalloc( sizeof(ExprList) ); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 894 | if( pList==0 ) return 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 895 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 896 | if( (pList->nExpr & 7)==0 ){ |
| 897 | int n = pList->nExpr + 8; |
| 898 | pList->a = sqliteRealloc(pList->a, n*sizeof(pList->a[0])); |
| 899 | if( pList->a==0 ){ |
| 900 | pList->nExpr = 0; |
| 901 | return pList; |
| 902 | } |
| 903 | } |
| 904 | i = pList->nExpr++; |
| 905 | pList->a[i].pExpr = pExpr; |
| 906 | pList->a[i].zName = 0; |
| 907 | if( pName ){ |
| 908 | sqliteSetNString(&pList->a[i].zName, pName->z, pName->n, 0); |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 909 | sqliteDequote(pList->a[i].zName); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 910 | } |
| 911 | return pList; |
| 912 | } |
| 913 | |
| 914 | /* |
| 915 | ** Delete an entire expression list. |
| 916 | */ |
| 917 | void sqliteExprListDelete(ExprList *pList){ |
| 918 | int i; |
| 919 | if( pList==0 ) return; |
| 920 | for(i=0; i<pList->nExpr; i++){ |
| 921 | sqliteExprDelete(pList->a[i].pExpr); |
| 922 | sqliteFree(pList->a[i].zName); |
| 923 | } |
| 924 | sqliteFree(pList->a); |
| 925 | sqliteFree(pList); |
| 926 | } |
| 927 | |
| 928 | /* |
| 929 | ** Append a new element to the given IdList. Create a new IdList if |
| 930 | ** need be. |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 931 | ** |
| 932 | ** A new IdList is returned, or NULL if malloc() fails. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 933 | */ |
| 934 | IdList *sqliteIdListAppend(IdList *pList, Token *pToken){ |
| 935 | if( pList==0 ){ |
| 936 | pList = sqliteMalloc( sizeof(IdList) ); |
| 937 | if( pList==0 ) return 0; |
| 938 | } |
| 939 | if( (pList->nId & 7)==0 ){ |
| 940 | pList->a = sqliteRealloc(pList->a, (pList->nId+8)*sizeof(pList->a[0]) ); |
| 941 | if( pList->a==0 ){ |
| 942 | pList->nId = 0; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 943 | sqliteIdListDelete(pList); |
| 944 | return 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 945 | } |
| 946 | } |
| 947 | memset(&pList->a[pList->nId], 0, sizeof(pList->a[0])); |
| 948 | if( pToken ){ |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 949 | char **pz = &pList->a[pList->nId].zName; |
| 950 | sqliteSetNString(pz, pToken->z, pToken->n, 0); |
| 951 | if( *pz==0 ){ |
| 952 | sqliteIdListDelete(pList); |
| 953 | return 0; |
| 954 | }else{ |
| 955 | sqliteDequote(*pz); |
| 956 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 957 | } |
| 958 | pList->nId++; |
| 959 | return pList; |
| 960 | } |
| 961 | |
| 962 | /* |
| 963 | ** Add an alias to the last identifier on the given identifier list. |
| 964 | */ |
| 965 | void sqliteIdListAddAlias(IdList *pList, Token *pToken){ |
| 966 | if( pList && pList->nId>0 ){ |
| 967 | int i = pList->nId - 1; |
| 968 | sqliteSetNString(&pList->a[i].zAlias, pToken->z, pToken->n, 0); |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 969 | sqliteDequote(pList->a[i].zAlias); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 970 | } |
| 971 | } |
| 972 | |
| 973 | /* |
| 974 | ** Delete an entire IdList |
| 975 | */ |
| 976 | void sqliteIdListDelete(IdList *pList){ |
| 977 | int i; |
| 978 | if( pList==0 ) return; |
| 979 | for(i=0; i<pList->nId; i++){ |
| 980 | sqliteFree(pList->a[i].zName); |
| 981 | sqliteFree(pList->a[i].zAlias); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 982 | if( pList->a[i].pSelect ){ |
| 983 | sqliteFree(pList->a[i].zName); |
| 984 | sqliteSelectDelete(pList->a[i].pSelect); |
| 985 | sqliteDeleteTable(0, pList->a[i].pTab); |
| 986 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 987 | } |
| 988 | sqliteFree(pList->a); |
| 989 | sqliteFree(pList); |
| 990 | } |
| 991 | |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 992 | |
| 993 | /* |
| 994 | ** The COPY command is for compatibility with PostgreSQL and specificially |
| 995 | ** for the ability to read the output of pg_dump. The format is as |
| 996 | ** follows: |
| 997 | ** |
| 998 | ** COPY table FROM file [USING DELIMITERS string] |
| 999 | ** |
| 1000 | ** "table" is an existing table name. We will read lines of code from |
| 1001 | ** file to fill this table with data. File might be "stdin". The optional |
| 1002 | ** delimiter string identifies the field separators. The default is a tab. |
| 1003 | */ |
| 1004 | void sqliteCopy( |
| 1005 | Parse *pParse, /* The parser context */ |
| 1006 | Token *pTableName, /* The name of the table into which we will insert */ |
| 1007 | Token *pFilename, /* The file from which to obtain information */ |
| 1008 | Token *pDelimiter /* Use this as the field delimiter */ |
| 1009 | ){ |
| 1010 | Table *pTab; |
| 1011 | char *zTab; |
| 1012 | int i, j; |
| 1013 | Vdbe *v; |
| 1014 | int addr, end; |
| 1015 | Index *pIdx; |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 1016 | sqlite *db = pParse->db; |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 1017 | |
| 1018 | zTab = sqliteTableNameFromToken(pTableName); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1019 | if( sqlite_malloc_failed || zTab==0 ) goto copy_cleanup; |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 1020 | pTab = sqliteFindTable(db, zTab); |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 1021 | sqliteFree(zTab); |
| 1022 | if( pTab==0 ){ |
| 1023 | sqliteSetNString(&pParse->zErrMsg, "no such table: ", 0, |
| 1024 | pTableName->z, pTableName->n, 0); |
| 1025 | pParse->nErr++; |
| 1026 | goto copy_cleanup; |
| 1027 | } |
| 1028 | if( pTab->readOnly ){ |
| 1029 | sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName, |
| 1030 | " may not be modified", 0); |
| 1031 | pParse->nErr++; |
| 1032 | goto copy_cleanup; |
| 1033 | } |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 1034 | v = sqliteGetVdbe(pParse); |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 1035 | if( v ){ |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 1036 | if( (db->flags & SQLITE_InTrans)==0 ){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1037 | sqliteVdbeAddOp(v, OP_Transaction, 0, 0, 0, 0); |
| 1038 | } |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 1039 | addr = sqliteVdbeAddOp(v, OP_FileOpen, 0, 0, 0, 0); |
| 1040 | sqliteVdbeChangeP3(v, addr, pFilename->z, pFilename->n); |
drh | b766599 | 2000-05-30 17:30:35 +0000 | [diff] [blame] | 1041 | sqliteVdbeDequoteP3(v, addr); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1042 | sqliteVdbeAddOp(v, OP_Open, 0, pTab->tnum, pTab->zName, 0); |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 1043 | for(i=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1044 | sqliteVdbeAddOp(v, OP_Open, i, pIdx->tnum, pIdx->zName, 0); |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 1045 | } |
| 1046 | end = sqliteVdbeMakeLabel(v); |
| 1047 | addr = sqliteVdbeAddOp(v, OP_FileRead, pTab->nCol, end, 0, 0); |
| 1048 | if( pDelimiter ){ |
| 1049 | sqliteVdbeChangeP3(v, addr, pDelimiter->z, pDelimiter->n); |
| 1050 | sqliteVdbeDequoteP3(v, addr); |
| 1051 | }else{ |
| 1052 | sqliteVdbeChangeP3(v, addr, "\t", 1); |
| 1053 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1054 | sqliteVdbeAddOp(v, OP_NewRecno, 0, 0, 0, 0); |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 1055 | if( pTab->pIndex ){ |
| 1056 | sqliteVdbeAddOp(v, OP_Dup, 0, 0, 0, 0); |
| 1057 | } |
| 1058 | for(i=0; i<pTab->nCol; i++){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1059 | sqliteVdbeAddOp(v, OP_FileColumn, i, 0, 0, 0); |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 1060 | } |
| 1061 | sqliteVdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0, 0, 0); |
| 1062 | sqliteVdbeAddOp(v, OP_Put, 0, 0, 0, 0); |
| 1063 | for(i=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){ |
| 1064 | if( pIdx->pNext ){ |
| 1065 | sqliteVdbeAddOp(v, OP_Dup, 0, 0, 0, 0); |
| 1066 | } |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 1067 | for(j=0; j<pIdx->nColumn; j++){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1068 | sqliteVdbeAddOp(v, OP_FileColumn, pIdx->aiColumn[j], 0, 0, 0); |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 1069 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1070 | sqliteVdbeAddOp(v, OP_MakeIdxKey, pIdx->nColumn, 0, 0, 0); |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 1071 | sqliteVdbeAddOp(v, OP_PutIdx, i, 0, 0, 0); |
| 1072 | } |
| 1073 | sqliteVdbeAddOp(v, OP_Goto, 0, addr, 0, 0); |
| 1074 | sqliteVdbeAddOp(v, OP_Noop, 0, 0, 0, end); |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 1075 | if( (db->flags & SQLITE_InTrans)==0 ){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1076 | sqliteVdbeAddOp(v, OP_Commit, 0, 0, 0, 0); |
| 1077 | } |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 1078 | } |
| 1079 | |
| 1080 | copy_cleanup: |
| 1081 | return; |
| 1082 | } |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 1083 | |
| 1084 | /* |
| 1085 | ** The non-standard VACUUM command is used to clean up the database, |
| 1086 | ** collapse free space, etc. It is modelled after the VACUUM command |
| 1087 | ** in PostgreSQL. |
| 1088 | */ |
| 1089 | void sqliteVacuum(Parse *pParse, Token *pTableName){ |
| 1090 | char *zName; |
| 1091 | Vdbe *v; |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 1092 | sqlite *db = pParse->db; |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 1093 | |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1094 | if( pParse->nErr || sqlite_malloc_failed ) return; |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 1095 | if( pTableName ){ |
| 1096 | zName = sqliteTableNameFromToken(pTableName); |
| 1097 | }else{ |
| 1098 | zName = 0; |
| 1099 | } |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 1100 | if( zName && sqliteFindIndex(db, zName)==0 |
| 1101 | && sqliteFindTable(db, zName)==0 ){ |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 1102 | sqliteSetString(&pParse->zErrMsg, "no such table or index: ", zName, 0); |
| 1103 | pParse->nErr++; |
| 1104 | goto vacuum_cleanup; |
| 1105 | } |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 1106 | v = sqliteGetVdbe(pParse); |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 1107 | if( v==0 ) goto vacuum_cleanup; |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 1108 | if( (db->flags & SQLITE_InTrans)==0 ){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1109 | sqliteVdbeAddOp(v, OP_Transaction, 0, 0, 0, 0); |
| 1110 | } |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 1111 | if( zName ){ |
| 1112 | sqliteVdbeAddOp(v, OP_Reorganize, 0, 0, zName, 0); |
| 1113 | }else{ |
| 1114 | int h; |
| 1115 | Table *pTab; |
| 1116 | Index *pIdx; |
| 1117 | for(h=0; h<N_HASH; h++){ |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 1118 | for(pTab=db->apTblHash[h]; pTab; pTab=pTab->pHash){ |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 1119 | sqliteVdbeAddOp(v, OP_Reorganize, 0, 0, pTab->zName, 0); |
| 1120 | for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
| 1121 | sqliteVdbeAddOp(v, OP_Reorganize, 0, 0, pIdx->zName, 0); |
| 1122 | } |
| 1123 | } |
| 1124 | } |
| 1125 | } |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 1126 | if( (db->flags & SQLITE_InTrans)==0 ){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1127 | sqliteVdbeAddOp(v, OP_Commit, 0, 0, 0, 0); |
| 1128 | } |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 1129 | |
| 1130 | vacuum_cleanup: |
| 1131 | sqliteFree(zName); |
| 1132 | return; |
| 1133 | } |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 1134 | |
| 1135 | /* |
| 1136 | ** Begin a transaction |
| 1137 | */ |
| 1138 | void sqliteBeginTransaction(Parse *pParse){ |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 1139 | sqlite *db; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1140 | Vdbe *v; |
| 1141 | |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 1142 | if( pParse==0 || (db=pParse->db)==0 || db->pBe==0 ) return; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1143 | if( pParse->nErr || sqlite_malloc_failed ) return; |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 1144 | if( db->flags & SQLITE_InTrans ) return; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1145 | v = sqliteGetVdbe(pParse); |
| 1146 | if( v ){ |
| 1147 | sqliteVdbeAddOp(v, OP_Transaction, 1, 0, 0, 0); |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 1148 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1149 | db->flags |= SQLITE_InTrans; |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 1150 | } |
| 1151 | |
| 1152 | /* |
| 1153 | ** Commit a transaction |
| 1154 | */ |
| 1155 | void sqliteCommitTransaction(Parse *pParse){ |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 1156 | sqlite *db; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1157 | Vdbe *v; |
| 1158 | |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 1159 | if( pParse==0 || (db=pParse->db)==0 || db->pBe==0 ) return; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1160 | if( pParse->nErr || sqlite_malloc_failed ) return; |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 1161 | if( (db->flags & SQLITE_InTrans)==0 ) return; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1162 | v = sqliteGetVdbe(pParse); |
| 1163 | if( v ){ |
| 1164 | sqliteVdbeAddOp(v, OP_Commit, 0, 0, 0, 0); |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 1165 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1166 | db->flags &= ~SQLITE_InTrans; |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 1167 | } |
| 1168 | |
| 1169 | /* |
| 1170 | ** Rollback a transaction |
| 1171 | */ |
| 1172 | void sqliteRollbackTransaction(Parse *pParse){ |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 1173 | sqlite *db; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1174 | Vdbe *v; |
| 1175 | |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 1176 | if( pParse==0 || (db=pParse->db)==0 || db->pBe==0 ) return; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1177 | if( pParse->nErr || sqlite_malloc_failed ) return; |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 1178 | if( (db->flags & SQLITE_InTrans)==0 ) return; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1179 | v = sqliteGetVdbe(pParse); |
| 1180 | if( v ){ |
| 1181 | sqliteVdbeAddOp(v, OP_Rollback, 0, 0, 0, 0); |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 1182 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1183 | db->flags &= ~SQLITE_InTrans; |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 1184 | } |