drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1 | /* |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2 | ** 2001 September 15 |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 7 | ** May you do good and not evil. |
| 8 | ** May you find forgiveness for yourself and forgive others. |
| 9 | ** May you share freely, never taking more than you give. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 10 | ** |
| 11 | ************************************************************************* |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 12 | ** This file contains C code routines that are called by the SQLite parser |
| 13 | ** when syntax rules are reduced. The routines in this file handle the |
| 14 | ** following kinds of SQL syntax: |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 15 | ** |
drh | bed8690 | 2000-06-02 13:27:59 +0000 | [diff] [blame] | 16 | ** CREATE TABLE |
| 17 | ** DROP TABLE |
| 18 | ** CREATE INDEX |
| 19 | ** DROP INDEX |
| 20 | ** creating expressions and ID lists |
| 21 | ** COPY |
| 22 | ** VACUUM |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 23 | ** BEGIN TRANSACTION |
| 24 | ** COMMIT |
| 25 | ** ROLLBACK |
| 26 | ** PRAGMA |
drh | bed8690 | 2000-06-02 13:27:59 +0000 | [diff] [blame] | 27 | ** |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame^] | 28 | ** $Id: build.c,v 1.45 2001/10/08 13:22:32 drh Exp $ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 29 | */ |
| 30 | #include "sqliteInt.h" |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 31 | #include <ctype.h> |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 32 | |
| 33 | /* |
| 34 | ** This routine is called after a single SQL statement has been |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 35 | ** parsed and we want to execute the VDBE code to implement |
| 36 | ** that statement. Prior action routines should have already |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 37 | ** constructed VDBE code to do the work of the SQL statement. |
| 38 | ** This routine just has to execute the VDBE code. |
| 39 | ** |
| 40 | ** Note that if an error occurred, it might be the case that |
| 41 | ** no VDBE code was generated. |
| 42 | */ |
| 43 | void sqliteExec(Parse *pParse){ |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 44 | int rc = SQLITE_OK; |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 45 | sqlite *db = pParse->db; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 46 | if( sqlite_malloc_failed ) return; |
drh | 3fc190c | 2001-09-14 03:24:23 +0000 | [diff] [blame] | 47 | if( pParse->pVdbe && pParse->nErr==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 48 | if( pParse->explain ){ |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 49 | rc = sqliteVdbeList(pParse->pVdbe, pParse->xCallback, pParse->pArg, |
| 50 | &pParse->zErrMsg); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 51 | }else{ |
drh | 3fc190c | 2001-09-14 03:24:23 +0000 | [diff] [blame] | 52 | FILE *trace = (db->flags & SQLITE_VdbeTrace)!=0 ? stdout : 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 53 | sqliteVdbeTrace(pParse->pVdbe, trace); |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 54 | rc = sqliteVdbeExec(pParse->pVdbe, pParse->xCallback, pParse->pArg, |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 55 | &pParse->zErrMsg, db->pBusyArg, |
| 56 | db->xBusyCallback); |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 57 | if( rc ) pParse->nErr++; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 58 | } |
| 59 | sqliteVdbeDelete(pParse->pVdbe); |
| 60 | pParse->pVdbe = 0; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 61 | pParse->colNamesSet = 0; |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 62 | pParse->rc = rc; |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 63 | pParse->schemaVerified = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 64 | } |
| 65 | } |
| 66 | |
| 67 | /* |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame^] | 68 | ** Construct a new expression node and return a pointer to it. Memory |
| 69 | ** for this node is obtained from sqliteMalloc(). The calling function |
| 70 | ** is responsible for making sure the node eventually gets freed. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 71 | */ |
| 72 | Expr *sqliteExpr(int op, Expr *pLeft, Expr *pRight, Token *pToken){ |
| 73 | Expr *pNew; |
| 74 | pNew = sqliteMalloc( sizeof(Expr) ); |
| 75 | if( pNew==0 ) return 0; |
| 76 | pNew->op = op; |
| 77 | pNew->pLeft = pLeft; |
| 78 | pNew->pRight = pRight; |
| 79 | if( pToken ){ |
| 80 | pNew->token = *pToken; |
| 81 | }else{ |
| 82 | pNew->token.z = ""; |
| 83 | pNew->token.n = 0; |
| 84 | } |
drh | e1b6a5b | 2000-07-29 13:06:59 +0000 | [diff] [blame] | 85 | if( pLeft && pRight ){ |
| 86 | sqliteExprSpan(pNew, &pLeft->span, &pRight->span); |
| 87 | }else{ |
| 88 | pNew->span = pNew->token; |
| 89 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 90 | return pNew; |
| 91 | } |
| 92 | |
| 93 | /* |
drh | e1b6a5b | 2000-07-29 13:06:59 +0000 | [diff] [blame] | 94 | ** Set the Expr.token field of the given expression to span all |
| 95 | ** text between the two given tokens. |
| 96 | */ |
| 97 | void sqliteExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){ |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 98 | if( pExpr ){ |
| 99 | pExpr->span.z = pLeft->z; |
| 100 | pExpr->span.n = pRight->n + (int)pRight->z - (int)pLeft->z; |
| 101 | } |
drh | e1b6a5b | 2000-07-29 13:06:59 +0000 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 105 | ** Construct a new expression node for a function with multiple |
| 106 | ** arguments. |
| 107 | */ |
| 108 | Expr *sqliteExprFunction(ExprList *pList, Token *pToken){ |
| 109 | Expr *pNew; |
| 110 | pNew = sqliteMalloc( sizeof(Expr) ); |
| 111 | if( pNew==0 ) return 0; |
| 112 | pNew->op = TK_FUNCTION; |
| 113 | pNew->pList = pList; |
| 114 | if( pToken ){ |
| 115 | pNew->token = *pToken; |
| 116 | }else{ |
| 117 | pNew->token.z = ""; |
| 118 | pNew->token.n = 0; |
| 119 | } |
| 120 | return pNew; |
| 121 | } |
| 122 | |
| 123 | /* |
| 124 | ** Recursively delete an expression tree. |
| 125 | */ |
| 126 | void sqliteExprDelete(Expr *p){ |
| 127 | if( p==0 ) return; |
| 128 | if( p->pLeft ) sqliteExprDelete(p->pLeft); |
| 129 | if( p->pRight ) sqliteExprDelete(p->pRight); |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 130 | if( p->pList ) sqliteExprListDelete(p->pList); |
| 131 | if( p->pSelect ) sqliteSelectDelete(p->pSelect); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 132 | sqliteFree(p); |
| 133 | } |
| 134 | |
| 135 | /* |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame^] | 136 | ** Locate the in-memory structure that describes |
| 137 | ** a particular database table given the name |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 138 | ** of that table. Return NULL if not found. |
| 139 | */ |
| 140 | Table *sqliteFindTable(sqlite *db, char *zName){ |
drh | afa4a02 | 2001-09-24 03:12:39 +0000 | [diff] [blame] | 141 | Table *p = sqliteHashFind(&db->tblHash, zName, strlen(zName)+1); |
| 142 | return (p==0 || p->isDelete) ? 0 : p; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | /* |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame^] | 146 | ** Locate the in-memory structure that describes |
| 147 | ** a particular index given the name of that index. |
| 148 | ** Return NULL if not found. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 149 | */ |
| 150 | Index *sqliteFindIndex(sqlite *db, char *zName){ |
drh | afa4a02 | 2001-09-24 03:12:39 +0000 | [diff] [blame] | 151 | Index *p = sqliteHashFind(&db->idxHash, zName, strlen(zName)+1); |
| 152 | return (p==0 || p->isDelete) ? 0 : p; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | /* |
| 156 | ** Remove the given index from the index hash table, and free |
| 157 | ** its memory structures. |
| 158 | ** |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 159 | ** The index is removed from the database hash table if db!=NULL. |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame^] | 160 | ** But the index is not unlinked from the Table that it indexes. |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 161 | ** Unlinking from the Table must be done by the calling function. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 162 | */ |
| 163 | static void sqliteDeleteIndex(sqlite *db, Index *pIndex){ |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 164 | if( pIndex->zName && db ){ |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 165 | sqliteHashInsert(&db->idxHash, pIndex->zName, strlen(pIndex->zName)+1, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 166 | } |
| 167 | sqliteFree(pIndex); |
| 168 | } |
| 169 | |
| 170 | /* |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 171 | ** Unlink the given index from its table, then remove |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame^] | 172 | ** the index from the index hash table and free its memory |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 173 | ** structures. |
| 174 | */ |
| 175 | static void sqliteUnlinkAndDeleteIndex(sqlite *db, Index *pIndex){ |
| 176 | if( pIndex->pTable->pIndex==pIndex ){ |
| 177 | pIndex->pTable->pIndex = pIndex->pNext; |
| 178 | }else{ |
| 179 | Index *p; |
| 180 | for(p=pIndex->pTable->pIndex; p && p->pNext!=pIndex; p=p->pNext){} |
| 181 | if( p && p->pNext==pIndex ){ |
| 182 | p->pNext = pIndex->pNext; |
| 183 | } |
| 184 | } |
| 185 | sqliteDeleteIndex(db, pIndex); |
| 186 | } |
| 187 | |
| 188 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 189 | ** Remove the memory data structures associated with the given |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 190 | ** Table. No changes are made to disk by this routine. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 191 | ** |
| 192 | ** This routine just deletes the data structure. It does not unlink |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 193 | ** the table data structure from the hash table. But it does destroy |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 194 | ** memory structures of the indices associated with the table. |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 195 | ** |
| 196 | ** Indices associated with the table are unlinked from the "db" |
| 197 | ** data structure if db!=NULL. If db==NULL, indices attached to |
| 198 | ** the table are deleted, but it is assumed they have already been |
| 199 | ** unlinked. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 200 | */ |
| 201 | void sqliteDeleteTable(sqlite *db, Table *pTable){ |
| 202 | int i; |
| 203 | Index *pIndex, *pNext; |
| 204 | if( pTable==0 ) return; |
| 205 | for(i=0; i<pTable->nCol; i++){ |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 206 | sqliteFree(pTable->aCol[i].zName); |
| 207 | sqliteFree(pTable->aCol[i].zDflt); |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 208 | sqliteFree(pTable->aCol[i].zType); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 209 | } |
| 210 | for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){ |
| 211 | pNext = pIndex->pNext; |
| 212 | sqliteDeleteIndex(db, pIndex); |
| 213 | } |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 214 | sqliteFree(pTable->zName); |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 215 | sqliteFree(pTable->aCol); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 216 | sqliteFree(pTable); |
| 217 | } |
| 218 | |
| 219 | /* |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 220 | ** Unlink the given table from the hash tables and the delete the |
| 221 | ** table structure and all its indices. |
| 222 | */ |
| 223 | static void sqliteUnlinkAndDeleteTable(sqlite *db, Table *pTable){ |
| 224 | if( pTable->zName && db ){ |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 225 | sqliteHashInsert(&db->tblHash, pTable->zName, strlen(pTable->zName)+1, 0); |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 226 | } |
| 227 | sqliteDeleteTable(db, pTable); |
| 228 | } |
| 229 | |
| 230 | /* |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 231 | ** Check all Tables and Indexes in the internal hash table and commit |
| 232 | ** any additions or deletions to those hash tables. |
| 233 | ** |
| 234 | ** When executing CREATE TABLE and CREATE INDEX statements, the Table |
| 235 | ** and Index structures are created and added to the hash tables, but |
| 236 | ** the "isCommit" field is not set. This routine sets those fields. |
| 237 | ** When executing DROP TABLE and DROP INDEX, the "isDelete" fields of |
| 238 | ** Table and Index structures is set but the structures are not unlinked |
| 239 | ** from the hash tables nor deallocated. This routine handles that |
| 240 | ** deallocation. |
| 241 | ** |
| 242 | ** See also: sqliteRollbackInternalChanges() |
| 243 | */ |
| 244 | void sqliteCommitInternalChanges(sqlite *db){ |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 245 | Hash toDelete; |
| 246 | HashElem *pElem; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 247 | if( (db->flags & SQLITE_InternChanges)==0 ) return; |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 248 | sqliteHashInit(&toDelete, SQLITE_HASH_POINTER, 0); |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 249 | db->schema_cookie = db->next_cookie; |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 250 | for(pElem=sqliteHashFirst(&db->tblHash); pElem; pElem=sqliteHashNext(pElem)){ |
| 251 | Table *pTable = sqliteHashData(pElem); |
| 252 | if( pTable->isDelete ){ |
| 253 | sqliteHashInsert(&toDelete, pTable, 0, pTable); |
| 254 | }else{ |
| 255 | pTable->isCommit = 1; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 256 | } |
| 257 | } |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 258 | for(pElem=sqliteHashFirst(&toDelete); pElem; pElem=sqliteHashNext(pElem)){ |
| 259 | Table *pTable = sqliteHashData(pElem); |
| 260 | sqliteUnlinkAndDeleteTable(db, pTable); |
| 261 | } |
| 262 | sqliteHashClear(&toDelete); |
| 263 | for(pElem=sqliteHashFirst(&db->idxHash); pElem; pElem=sqliteHashNext(pElem)){ |
| 264 | Table *pIndex = sqliteHashData(pElem); |
| 265 | if( pIndex->isDelete ){ |
| 266 | sqliteHashInsert(&toDelete, pIndex, 0, pIndex); |
| 267 | }else{ |
| 268 | pIndex->isCommit = 1; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 269 | } |
| 270 | } |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 271 | for(pElem=sqliteHashFirst(&toDelete); pElem; pElem=sqliteHashNext(pElem)){ |
| 272 | Index *pIndex = sqliteHashData(pElem); |
| 273 | sqliteUnlinkAndDeleteIndex(db, pIndex); |
| 274 | } |
| 275 | sqliteHashClear(&toDelete); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 276 | db->flags &= ~SQLITE_InternChanges; |
| 277 | } |
| 278 | |
| 279 | /* |
| 280 | ** This routine runs when one or more CREATE TABLE, CREATE INDEX, |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame^] | 281 | ** DROP TABLE, or DROP INDEX statements gets rolled back. The |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 282 | ** additions or deletions of Table and Index structures in the |
| 283 | ** internal hash tables are undone. |
| 284 | ** |
| 285 | ** See also: sqliteCommitInternalChanges() |
| 286 | */ |
| 287 | void sqliteRollbackInternalChanges(sqlite *db){ |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 288 | Hash toDelete; |
| 289 | HashElem *pElem; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 290 | if( (db->flags & SQLITE_InternChanges)==0 ) return; |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 291 | sqliteHashInit(&toDelete, SQLITE_HASH_POINTER, 0); |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 292 | db->next_cookie = db->schema_cookie; |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 293 | for(pElem=sqliteHashFirst(&db->tblHash); pElem; pElem=sqliteHashNext(pElem)){ |
| 294 | Table *pTable = sqliteHashData(pElem); |
| 295 | if( !pTable->isCommit ){ |
| 296 | sqliteHashInsert(&toDelete, pTable, 0, pTable); |
| 297 | }else{ |
| 298 | pTable->isDelete = 0; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 299 | } |
| 300 | } |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 301 | for(pElem=sqliteHashFirst(&toDelete); pElem; pElem=sqliteHashNext(pElem)){ |
| 302 | Table *pTable = sqliteHashData(pElem); |
| 303 | sqliteUnlinkAndDeleteTable(db, pTable); |
| 304 | } |
| 305 | sqliteHashClear(&toDelete); |
| 306 | for(pElem=sqliteHashFirst(&db->idxHash); pElem; pElem=sqliteHashNext(pElem)){ |
| 307 | Table *pIndex = sqliteHashData(pElem); |
| 308 | if( !pIndex->isCommit ){ |
| 309 | sqliteHashInsert(&toDelete, pIndex, 0, pIndex); |
| 310 | }else{ |
| 311 | pIndex->isDelete = 0; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 312 | } |
| 313 | } |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 314 | for(pElem=sqliteHashFirst(&toDelete); pElem; pElem=sqliteHashNext(pElem)){ |
| 315 | Index *pIndex = sqliteHashData(pElem); |
| 316 | sqliteUnlinkAndDeleteIndex(db, pIndex); |
| 317 | } |
| 318 | sqliteHashClear(&toDelete); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 319 | db->flags &= ~SQLITE_InternChanges; |
| 320 | } |
| 321 | |
| 322 | /* |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 323 | ** Construct the name of a user table or index from a token. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 324 | ** |
| 325 | ** Space to hold the name is obtained from sqliteMalloc() and must |
| 326 | ** be freed by the calling function. |
| 327 | */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 328 | char *sqliteTableNameFromToken(Token *pName){ |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 329 | char *zName = sqliteStrNDup(pName->z, pName->n); |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 330 | sqliteDequote(zName); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 331 | return zName; |
| 332 | } |
| 333 | |
| 334 | /* |
| 335 | ** Begin constructing a new table representation in memory. This is |
| 336 | ** the first of several action routines that get called in response |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 337 | ** to a CREATE TABLE statement. In particular, this routine is called |
| 338 | ** after seeing tokens "CREATE" and "TABLE" and the table name. The |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame^] | 339 | ** pStart token is the CREATE and pName is the table name. The isTemp |
| 340 | ** flag is true if the "TEMP" or "TEMPORARY" keyword occurs in between |
| 341 | ** CREATE and TABLE. |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 342 | ** |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame^] | 343 | ** The new table record is initialized and put in pParse->pNewTable. |
| 344 | ** As more of the CREATE TABLE statement is parsed, additional action |
| 345 | ** routines will be called to add more information to this record. |
| 346 | ** At the end of the CREATE TABLE statement, the sqliteEndTable() routine |
| 347 | ** is called to complete the construction of the new table record. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 348 | */ |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame^] | 349 | void sqliteStartTable(Parse *pParse, Token *pStart, Token *pName, int isTemp){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 350 | Table *pTable; |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame^] | 351 | Index *pIdx; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 352 | char *zName; |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 353 | sqlite *db = pParse->db; |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 354 | Vdbe *v; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 355 | |
| 356 | pParse->sFirstToken = *pStart; |
| 357 | zName = sqliteTableNameFromToken(pName); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 358 | if( zName==0 ) return; |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame^] | 359 | |
| 360 | /* Before trying to create a temporary table, make sure the Btree for |
| 361 | ** holding temporary tables is open. |
| 362 | */ |
| 363 | if( isTemp && db->pBeTemp==0 ){ |
| 364 | int rc = sqliteBtreeOpen(0, 0, MAX_PAGES, &db->pBeTemp); |
| 365 | if( rc!=SQLITE_OK ){ |
| 366 | sqliteSetNString(&pParse->zErrMsg, "unable to open a temporary database " |
| 367 | "file for storing temporary tables", 0); |
| 368 | pParse->nErr++; |
| 369 | return; |
| 370 | } |
| 371 | if( db->flags & SQLITE_InTrans ){ |
| 372 | rc = sqliteBtreeBeginTrans(db->pBeTemp); |
| 373 | if( rc!=SQLITE_OK ){ |
| 374 | sqliteSetNString(&pParse->zErrMsg, "unable to get a write lock on " |
| 375 | "the temporary datbase file", 0); |
| 376 | pParse->nErr++; |
| 377 | return; |
| 378 | } |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | /* Make sure the new table name does not collide with an existing |
| 383 | ** index or table name. Issue an error message if it does. |
| 384 | ** |
| 385 | ** If we are re-reading the sqlite_master table because of a schema |
| 386 | ** change and a new permanent table is found whose name collides with |
| 387 | ** an existing temporary table, then ignore the new permanent table. |
| 388 | ** We will continue parsing, but the pParse->nameClash flag will be set |
| 389 | ** so we will know to discard the table record once parsing has finished. |
| 390 | */ |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 391 | pTable = sqliteFindTable(db, zName); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 392 | if( pTable!=0 ){ |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame^] | 393 | if( pTable->isTemp && pParse->initFlag ){ |
| 394 | pParse->nameClash = 1; |
| 395 | }else{ |
| 396 | sqliteSetNString(&pParse->zErrMsg, "table ", 0, pName->z, pName->n, |
| 397 | " already exists", 0, 0); |
| 398 | sqliteFree(zName); |
| 399 | pParse->nErr++; |
| 400 | return; |
| 401 | } |
| 402 | }else{ |
| 403 | pParse->nameClash = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 404 | } |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame^] | 405 | if( (pIdx = sqliteFindIndex(db, zName))!=0 && |
| 406 | (!pIdx->pTable->isTemp || !pParse->initFlag) ){ |
drh | 1d37e28 | 2000-05-30 03:12:21 +0000 | [diff] [blame] | 407 | sqliteSetString(&pParse->zErrMsg, "there is already an index named ", |
| 408 | zName, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 409 | sqliteFree(zName); |
| 410 | pParse->nErr++; |
| 411 | return; |
| 412 | } |
| 413 | pTable = sqliteMalloc( sizeof(Table) ); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 414 | if( pTable==0 ) return; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 415 | pTable->zName = zName; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 416 | pTable->nCol = 0; |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 417 | pTable->aCol = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 418 | pTable->pIndex = 0; |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame^] | 419 | pTable->isTemp = isTemp; |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 420 | if( pParse->pNewTable ) sqliteDeleteTable(db, pParse->pNewTable); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 421 | pParse->pNewTable = pTable; |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 422 | if( !pParse->initFlag && (v = sqliteGetVdbe(pParse))!=0 ){ |
| 423 | if( (db->flags & SQLITE_InTrans)==0 ){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 424 | sqliteVdbeAddOp(v, OP_Transaction, 0, 0, 0, 0); |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 425 | sqliteVdbeAddOp(v, OP_VerifyCookie, db->schema_cookie, 0, 0, 0); |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 426 | pParse->schemaVerified = 1; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 427 | } |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame^] | 428 | if( !isTemp ){ |
| 429 | sqliteVdbeAddOp(v, OP_OpenWrite, 0, 2, MASTER_NAME, 0); |
| 430 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 431 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | /* |
| 435 | ** Add a new column to the table currently being constructed. |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 436 | ** |
| 437 | ** The parser calls this routine once for each column declaration |
| 438 | ** in a CREATE TABLE statement. sqliteStartTable() gets called |
| 439 | ** first to get things going. Then this routine is called for each |
| 440 | ** column. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 441 | */ |
| 442 | void sqliteAddColumn(Parse *pParse, Token *pName){ |
| 443 | Table *p; |
| 444 | char **pz; |
| 445 | if( (p = pParse->pNewTable)==0 ) return; |
| 446 | if( (p->nCol & 0x7)==0 ){ |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 447 | p->aCol = sqliteRealloc( p->aCol, (p->nCol+8)*sizeof(p->aCol[0])); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 448 | if( p->aCol==0 ){ |
| 449 | p->nCol = 0; |
| 450 | return; |
| 451 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 452 | } |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 453 | memset(&p->aCol[p->nCol], 0, sizeof(p->aCol[0])); |
| 454 | pz = &p->aCol[p->nCol++].zName; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 455 | sqliteSetNString(pz, pName->z, pName->n, 0); |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 456 | sqliteDequote(*pz); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | /* |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 460 | ** This routine is called by the parser while in the middle of |
| 461 | ** parsing a CREATE TABLE statement. A "NOT NULL" constraint has |
| 462 | ** been seen on a column. This routine sets the notNull flag on |
| 463 | ** the column currently under construction. |
| 464 | */ |
| 465 | void sqliteAddNotNull(Parse *pParse){ |
| 466 | Table *p; |
| 467 | int i; |
| 468 | if( (p = pParse->pNewTable)==0 ) return; |
| 469 | i = p->nCol-1; |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame^] | 470 | if( i>=0 ) p->aCol[i].notNull = 1; |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | /* |
| 474 | ** This routine is called by the parser while in the middle of |
| 475 | ** parsing a CREATE TABLE statement. The pFirst token is the first |
| 476 | ** token in the sequence of tokens that describe the type of the |
| 477 | ** column currently under construction. pLast is the last token |
| 478 | ** in the sequence. Use this information to construct a string |
| 479 | ** that contains the typename of the column and store that string |
| 480 | ** in zType. |
| 481 | */ |
| 482 | void sqliteAddColumnType(Parse *pParse, Token *pFirst, Token *pLast){ |
| 483 | Table *p; |
| 484 | int i, j; |
| 485 | int n; |
| 486 | char *z, **pz; |
| 487 | if( (p = pParse->pNewTable)==0 ) return; |
| 488 | i = p->nCol-1; |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame^] | 489 | if( i<0 ) return; |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 490 | pz = &p->aCol[i].zType; |
| 491 | n = pLast->n + ((int)pLast->z) - (int)pFirst->z; |
| 492 | sqliteSetNString(pz, pFirst->z, n, 0); |
| 493 | z = *pz; |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame^] | 494 | if( z==0 ) return; |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 495 | for(i=j=0; z[i]; i++){ |
| 496 | int c = z[i]; |
| 497 | if( isspace(c) ) continue; |
| 498 | z[j++] = c; |
| 499 | } |
| 500 | z[j] = 0; |
| 501 | } |
| 502 | |
| 503 | /* |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 504 | ** The given token is the default value for the last column added to |
| 505 | ** the table currently under construction. If "minusFlag" is true, it |
| 506 | ** means the value token was preceded by a minus sign. |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 507 | ** |
| 508 | ** This routine is called by the parser while in the middle of |
| 509 | ** parsing a CREATE TABLE statement. |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 510 | */ |
| 511 | void sqliteAddDefaultValue(Parse *pParse, Token *pVal, int minusFlag){ |
| 512 | Table *p; |
| 513 | int i; |
| 514 | char **pz; |
| 515 | if( (p = pParse->pNewTable)==0 ) return; |
| 516 | i = p->nCol-1; |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame^] | 517 | if( i<0 ) return; |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 518 | pz = &p->aCol[i].zDflt; |
| 519 | if( minusFlag ){ |
| 520 | sqliteSetNString(pz, "-", 1, pVal->z, pVal->n, 0); |
| 521 | }else{ |
| 522 | sqliteSetNString(pz, pVal->z, pVal->n, 0); |
| 523 | } |
| 524 | sqliteDequote(*pz); |
| 525 | } |
| 526 | |
| 527 | /* |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 528 | ** Come up with a new random value for the schema cookie. Make sure |
| 529 | ** the new value is different from the old. |
| 530 | ** |
| 531 | ** The schema cookie is used to determine when the schema for the |
| 532 | ** database changes. After each schema change, the cookie value |
| 533 | ** changes. When a process first reads the schema it records the |
| 534 | ** cookie. Thereafter, whenever it goes to access the database, |
| 535 | ** it checks the cookie to make sure the schema has not changed |
| 536 | ** since it was last read. |
| 537 | ** |
| 538 | ** This plan is not completely bullet-proof. It is possible for |
| 539 | ** the schema to change multiple times and for the cookie to be |
| 540 | ** set back to prior value. But schema changes are infrequent |
| 541 | ** and the probability of hitting the same cookie value is only |
| 542 | ** 1 chance in 2^32. So we're safe enough. |
| 543 | */ |
| 544 | static void changeCookie(sqlite *db){ |
| 545 | if( db->next_cookie==db->schema_cookie ){ |
drh | 90bfcda | 2001-09-23 19:46:51 +0000 | [diff] [blame] | 546 | db->next_cookie = db->schema_cookie + sqliteRandomByte(db) + 1; |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 547 | db->flags |= SQLITE_InternChanges; |
| 548 | } |
| 549 | } |
| 550 | |
| 551 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 552 | ** This routine is called to report the final ")" that terminates |
| 553 | ** a CREATE TABLE statement. |
| 554 | ** |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame^] | 555 | ** The table structure that other action routines have been building |
| 556 | ** is added to the internal hash tables, assuming no errors have |
| 557 | ** occurred. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 558 | ** |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 559 | ** An entry for the table is made in the master table on disk, |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame^] | 560 | ** unless this is a temporary table or initFlag==1. When initFlag==1, |
| 561 | ** it means we are reading the sqlite_master table because we just |
| 562 | ** connected to the database or because the sqlite_master table has |
| 563 | ** recently changes, so the entry for this table already exists in |
| 564 | ** the sqlite_master table. We do not want to create it again. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 565 | */ |
| 566 | void sqliteEndTable(Parse *pParse, Token *pEnd){ |
| 567 | Table *p; |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 568 | sqlite *db = pParse->db; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 569 | |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 570 | if( pEnd==0 || pParse->nErr || sqlite_malloc_failed ) return; |
drh | 2803757 | 2000-08-02 13:47:41 +0000 | [diff] [blame] | 571 | p = pParse->pNewTable; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 572 | if( p==0 ) return; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 573 | |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame^] | 574 | /* Add the table to the in-memory representation of the database. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 575 | */ |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame^] | 576 | assert( pParse->nameClash==0 || pParse->initFlag==0 ); |
| 577 | if( pParse->explain==0 && pParse->nameClash==0 ){ |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 578 | sqliteHashInsert(&db->tblHash, p->zName, strlen(p->zName)+1, p); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 579 | pParse->pNewTable = 0; |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 580 | db->nTable++; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 581 | db->flags |= SQLITE_InternChanges; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 582 | } |
| 583 | |
drh | d78eeee | 2001-09-13 16:18:53 +0000 | [diff] [blame] | 584 | /* If the initFlag is 1 it means we are reading the SQL off the |
| 585 | ** "sqlite_master" table on the disk. So do not write to the disk |
drh | e3c4137 | 2001-09-17 20:25:58 +0000 | [diff] [blame] | 586 | ** again. Extract the root page number for the table from the |
| 587 | ** pParse->newTnum field. (The page number should have been put |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 588 | ** there by the sqliteOpenCb routine.) |
drh | d78eeee | 2001-09-13 16:18:53 +0000 | [diff] [blame] | 589 | */ |
| 590 | if( pParse->initFlag ){ |
| 591 | p->tnum = pParse->newTnum; |
| 592 | } |
| 593 | |
drh | e3c4137 | 2001-09-17 20:25:58 +0000 | [diff] [blame] | 594 | /* If not initializing, then create a record for the new table |
| 595 | ** in the SQLITE_MASTER table of the database. |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame^] | 596 | ** |
| 597 | ** If this is a TEMPORARY table, then just create the table. Do not |
| 598 | ** make an entry in SQLITE_MASTER. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 599 | */ |
| 600 | if( !pParse->initFlag ){ |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 601 | int n, addr; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 602 | Vdbe *v; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 603 | |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 604 | v = sqliteGetVdbe(pParse); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 605 | if( v==0 ) return; |
| 606 | n = (int)pEnd->z - (int)pParse->sFirstToken.z + 1; |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame^] | 607 | if( !p->isTemp ){ |
| 608 | sqliteVdbeAddOp(v, OP_NewRecno, 0, 0, 0, 0); |
| 609 | sqliteVdbeAddOp(v, OP_String, 0, 0, "table", 0); |
| 610 | sqliteVdbeAddOp(v, OP_String, 0, 0, p->zName, 0); |
| 611 | sqliteVdbeAddOp(v, OP_String, 0, 0, p->zName, 0); |
| 612 | } |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 613 | addr = sqliteVdbeAddOp(v, OP_CreateTable, 0, 0, 0, 0); |
| 614 | sqliteVdbeChangeP3(v, addr, (char *)&p->tnum, -1); |
| 615 | p->tnum = 0; |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame^] | 616 | if( !p->isTemp ){ |
| 617 | addr = sqliteVdbeAddOp(v, OP_String, 0, 0, 0, 0); |
| 618 | sqliteVdbeChangeP3(v, addr, pParse->sFirstToken.z, n); |
| 619 | sqliteVdbeAddOp(v, OP_MakeRecord, 5, 0, 0, 0); |
| 620 | sqliteVdbeAddOp(v, OP_Put, 0, 0, 0, 0); |
| 621 | changeCookie(db); |
| 622 | sqliteVdbeAddOp(v, OP_SetCookie, db->next_cookie, 0, 0, 0); |
| 623 | sqliteVdbeAddOp(v, OP_Close, 0, 0, 0, 0); |
| 624 | } |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 625 | if( (db->flags & SQLITE_InTrans)==0 ){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 626 | sqliteVdbeAddOp(v, OP_Commit, 0, 0, 0, 0); |
| 627 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 628 | } |
| 629 | } |
| 630 | |
| 631 | /* |
| 632 | ** Given a token, look up a table with that name. If not found, leave |
| 633 | ** an error for the parser to find and return NULL. |
| 634 | */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 635 | Table *sqliteTableFromToken(Parse *pParse, Token *pTok){ |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 636 | char *zName; |
| 637 | Table *pTab; |
| 638 | zName = sqliteTableNameFromToken(pTok); |
| 639 | if( zName==0 ) return 0; |
| 640 | pTab = sqliteFindTable(pParse->db, zName); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 641 | sqliteFree(zName); |
| 642 | if( pTab==0 ){ |
drh | b24fcbe | 2000-05-29 23:30:50 +0000 | [diff] [blame] | 643 | sqliteSetNString(&pParse->zErrMsg, "no such table: ", 0, |
| 644 | pTok->z, pTok->n, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 645 | pParse->nErr++; |
| 646 | } |
| 647 | return pTab; |
| 648 | } |
| 649 | |
| 650 | /* |
| 651 | ** This routine is called to do the work of a DROP TABLE statement. |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 652 | ** pName is the name of the table to be dropped. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 653 | */ |
| 654 | void sqliteDropTable(Parse *pParse, Token *pName){ |
| 655 | Table *pTable; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 656 | Vdbe *v; |
| 657 | int base; |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 658 | sqlite *db = pParse->db; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 659 | |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 660 | if( pParse->nErr || sqlite_malloc_failed ) return; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 661 | pTable = sqliteTableFromToken(pParse, pName); |
| 662 | if( pTable==0 ) return; |
| 663 | if( pTable->readOnly ){ |
drh | 1d37e28 | 2000-05-30 03:12:21 +0000 | [diff] [blame] | 664 | sqliteSetString(&pParse->zErrMsg, "table ", pTable->zName, |
| 665 | " may not be dropped", 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 666 | pParse->nErr++; |
| 667 | return; |
| 668 | } |
| 669 | |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 670 | /* Generate code to remove the table from the master table |
| 671 | ** on disk. |
| 672 | */ |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 673 | v = sqliteGetVdbe(pParse); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 674 | if( v ){ |
| 675 | static VdbeOp dropTable[] = { |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 676 | { OP_OpenWrite, 0, 2, MASTER_NAME}, |
drh | d78eeee | 2001-09-13 16:18:53 +0000 | [diff] [blame] | 677 | { OP_Rewind, 0, 0, 0}, |
| 678 | { OP_String, 0, 0, 0}, /* 2 */ |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 679 | { OP_Next, 0, ADDR(9), 0}, /* 3 */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 680 | { OP_Dup, 0, 0, 0}, |
drh | e3c4137 | 2001-09-17 20:25:58 +0000 | [diff] [blame] | 681 | { OP_Column, 0, 2, 0}, |
drh | d78eeee | 2001-09-13 16:18:53 +0000 | [diff] [blame] | 682 | { OP_Ne, 0, ADDR(3), 0}, |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 683 | { OP_Delete, 0, 0, 0}, |
drh | d78eeee | 2001-09-13 16:18:53 +0000 | [diff] [blame] | 684 | { OP_Goto, 0, ADDR(3), 0}, |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame^] | 685 | { OP_SetCookie, 0, 0, 0}, /* 9 */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 686 | { OP_Close, 0, 0, 0}, |
| 687 | }; |
| 688 | Index *pIdx; |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 689 | if( (db->flags & SQLITE_InTrans)==0 ){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 690 | sqliteVdbeAddOp(v, OP_Transaction, 0, 0, 0, 0); |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 691 | sqliteVdbeAddOp(v, OP_VerifyCookie, db->schema_cookie, 0, 0, 0); |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 692 | pParse->schemaVerified = 1; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 693 | } |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame^] | 694 | if( !pTable->isTemp ){ |
| 695 | base = sqliteVdbeAddOpList(v, ArraySize(dropTable), dropTable); |
| 696 | sqliteVdbeChangeP3(v, base+2, pTable->zName, 0); |
| 697 | changeCookie(db); |
| 698 | sqliteVdbeChangeP1(v, base+9, db->next_cookie); |
| 699 | } |
| 700 | sqliteVdbeAddOp(v, OP_Destroy, pTable->tnum, pTable->isTemp, 0, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 701 | for(pIdx=pTable->pIndex; pIdx; pIdx=pIdx->pNext){ |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame^] | 702 | sqliteVdbeAddOp(v, OP_Destroy, pIdx->tnum, pTable->isTemp, 0, 0); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 703 | } |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 704 | if( (db->flags & SQLITE_InTrans)==0 ){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 705 | sqliteVdbeAddOp(v, OP_Commit, 0, 0, 0, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 706 | } |
| 707 | } |
| 708 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 709 | /* Mark the in-memory Table structure as being deleted. The actually |
| 710 | ** deletion occurs inside of sqliteCommitInternalChanges(). |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 711 | ** |
| 712 | ** Exception: if the SQL statement began with the EXPLAIN keyword, |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 713 | ** then no changes should be made. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 714 | */ |
| 715 | if( !pParse->explain ){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 716 | pTable->isDelete = 1; |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 717 | db->flags |= SQLITE_InternChanges; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 718 | } |
| 719 | } |
| 720 | |
| 721 | /* |
| 722 | ** Create a new index for an SQL table. pIndex is the name of the index |
| 723 | ** and pTable is the name of the table that is to be indexed. Both will |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 724 | ** be NULL for a primary key or an index that is created to satisfy a |
| 725 | ** UNIQUE constraint. If pTable and pIndex are NULL, use pParse->pNewTable |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 726 | ** as the table to be indexed. pParse->pNewTable is a table that is |
| 727 | ** currently being constructed by a CREATE TABLE statement. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 728 | ** |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 729 | ** pList is a list of columns to be indexed. pList will be NULL if this |
| 730 | ** is a primary key or unique-constraint on the most recent column added |
| 731 | ** to the table currently under construction. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 732 | */ |
| 733 | void sqliteCreateIndex( |
| 734 | Parse *pParse, /* All information about this parse */ |
| 735 | Token *pName, /* Name of the index. May be NULL */ |
| 736 | Token *pTable, /* Name of the table to index. Use pParse->pNewTable if 0 */ |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 737 | IdList *pList, /* A list of columns to be indexed */ |
drh | 717e640 | 2001-09-27 03:22:32 +0000 | [diff] [blame] | 738 | int isUnique, /* True if all entries in this index must be unique */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 739 | Token *pStart, /* The CREATE token that begins a CREATE TABLE statement */ |
| 740 | Token *pEnd /* The ")" that closes the CREATE INDEX statement */ |
| 741 | ){ |
| 742 | Table *pTab; /* Table to be indexed */ |
| 743 | Index *pIndex; /* The index to be created */ |
| 744 | char *zName = 0; |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 745 | int i, j; |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame^] | 746 | Token nullId; /* Fake token for an empty ID list */ |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 747 | sqlite *db = pParse->db; |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame^] | 748 | int hideName = 0; /* Do not put table name in the hash table */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 749 | |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 750 | if( pParse->nErr || sqlite_malloc_failed ) goto exit_create_index; |
| 751 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 752 | /* |
| 753 | ** Find the table that is to be indexed. Return early if not found. |
| 754 | */ |
| 755 | if( pTable!=0 ){ |
drh | e3c4137 | 2001-09-17 20:25:58 +0000 | [diff] [blame] | 756 | assert( pName!=0 ); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 757 | pTab = sqliteTableFromToken(pParse, pTable); |
| 758 | }else{ |
drh | e3c4137 | 2001-09-17 20:25:58 +0000 | [diff] [blame] | 759 | assert( pName==0 ); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 760 | pTab = pParse->pNewTable; |
| 761 | } |
| 762 | if( pTab==0 || pParse->nErr ) goto exit_create_index; |
| 763 | if( pTab->readOnly ){ |
drh | b24fcbe | 2000-05-29 23:30:50 +0000 | [diff] [blame] | 764 | sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName, |
| 765 | " may not have new indices added", 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 766 | pParse->nErr++; |
| 767 | goto exit_create_index; |
| 768 | } |
| 769 | |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame^] | 770 | /* If this index is created while re-reading the schema from sqlite_master |
| 771 | ** but the table associated with this index is a temporary table, it can |
| 772 | ** only mean that the table this index is really associated with is one |
| 773 | ** whose name is hidden behind a temporary table with the same name. |
| 774 | ** Since its table has been suppressed, we need to also suppress the |
| 775 | ** index. |
| 776 | */ |
| 777 | if( pParse->initFlag && pTab->isTemp ){ |
| 778 | goto exit_create_index; |
| 779 | } |
| 780 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 781 | /* |
| 782 | ** Find the name of the index. Make sure there is not already another |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame^] | 783 | ** index or table with the same name. |
| 784 | ** |
| 785 | ** Exception: If we are reading the names of permanent indices from the |
| 786 | ** sqlite_master table (because some other process changed the schema) and |
| 787 | ** one of the index names collides with the name of a temporary table or |
| 788 | ** index, then we will continue to process this index, but we will not |
| 789 | ** store its name in the hash table. Set the hideName flag to accomplish |
| 790 | ** this. |
| 791 | ** |
| 792 | ** If pName==0 it means that we are |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 793 | ** dealing with a primary key or UNIQUE constraint. We have to invent our |
| 794 | ** own name. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 795 | */ |
| 796 | if( pName ){ |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame^] | 797 | Index *pISameName; /* Another index with the same name */ |
| 798 | Table *pTSameName; /* A table with same name as the index */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 799 | zName = sqliteTableNameFromToken(pName); |
drh | e3c4137 | 2001-09-17 20:25:58 +0000 | [diff] [blame] | 800 | if( zName==0 ) goto exit_create_index; |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame^] | 801 | if( (pISameName = sqliteFindIndex(db, zName))!=0 ){ |
| 802 | if( pISameName->pTable->isTemp && pParse->initFlag ){ |
| 803 | hideName = 1; |
| 804 | }else{ |
| 805 | sqliteSetString(&pParse->zErrMsg, "index ", zName, |
| 806 | " already exists", 0); |
| 807 | pParse->nErr++; |
| 808 | goto exit_create_index; |
| 809 | } |
drh | e3c4137 | 2001-09-17 20:25:58 +0000 | [diff] [blame] | 810 | } |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame^] | 811 | if( (pTSameName = sqliteFindTable(db, zName))!=0 ){ |
| 812 | if( pTSameName->isTemp && pParse->initFlag ){ |
| 813 | hideName = 1; |
| 814 | }else{ |
| 815 | sqliteSetString(&pParse->zErrMsg, "there is already a table named ", |
| 816 | zName, 0); |
| 817 | pParse->nErr++; |
| 818 | goto exit_create_index; |
| 819 | } |
drh | e3c4137 | 2001-09-17 20:25:58 +0000 | [diff] [blame] | 820 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 821 | }else{ |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 822 | char zBuf[30]; |
| 823 | int n; |
| 824 | Index *pLoop; |
| 825 | for(pLoop=pTab->pIndex, n=1; pLoop; pLoop=pLoop->pNext, n++){} |
| 826 | sprintf(zBuf,"%d)",n); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 827 | zName = 0; |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 828 | sqliteSetString(&zName, "(", pTab->zName, " autoindex ", zBuf, 0); |
drh | e3c4137 | 2001-09-17 20:25:58 +0000 | [diff] [blame] | 829 | if( zName==0 ) goto exit_create_index; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 830 | } |
| 831 | |
| 832 | /* If pList==0, it means this routine was called to make a primary |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 833 | ** key out of the last column added to the table under construction. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 834 | ** So create a fake list to simulate this. |
| 835 | */ |
| 836 | if( pList==0 ){ |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 837 | nullId.z = pTab->aCol[pTab->nCol-1].zName; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 838 | nullId.n = strlen(nullId.z); |
| 839 | pList = sqliteIdListAppend(0, &nullId); |
| 840 | if( pList==0 ) goto exit_create_index; |
| 841 | } |
| 842 | |
| 843 | /* |
| 844 | ** Allocate the index structure. |
| 845 | */ |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 846 | pIndex = sqliteMalloc( sizeof(Index) + strlen(zName) + 1 + |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 847 | sizeof(int)*pList->nId ); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 848 | if( pIndex==0 ) goto exit_create_index; |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 849 | pIndex->aiColumn = (int*)&pIndex[1]; |
| 850 | pIndex->zName = (char*)&pIndex->aiColumn[pList->nId]; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 851 | strcpy(pIndex->zName, zName); |
| 852 | pIndex->pTable = pTab; |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 853 | pIndex->nColumn = pList->nId; |
drh | 717e640 | 2001-09-27 03:22:32 +0000 | [diff] [blame] | 854 | pIndex->isUnique = isUnique; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 855 | |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 856 | /* Scan the names of the columns of the table to be indexed and |
| 857 | ** load the column indices into the Index structure. Report an error |
| 858 | ** if any column is not found. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 859 | */ |
| 860 | for(i=0; i<pList->nId; i++){ |
| 861 | for(j=0; j<pTab->nCol; j++){ |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 862 | if( sqliteStrICmp(pList->a[i].zName, pTab->aCol[j].zName)==0 ) break; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 863 | } |
| 864 | if( j>=pTab->nCol ){ |
drh | b24fcbe | 2000-05-29 23:30:50 +0000 | [diff] [blame] | 865 | sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName, |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 866 | " has no column named ", pList->a[i].zName, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 867 | pParse->nErr++; |
| 868 | sqliteFree(pIndex); |
| 869 | goto exit_create_index; |
| 870 | } |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 871 | pIndex->aiColumn[i] = j; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 872 | } |
| 873 | |
| 874 | /* Link the new Index structure to its table and to the other |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 875 | ** in-memory database structures. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 876 | */ |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 877 | pIndex->pNext = pTab->pIndex; |
| 878 | pTab->pIndex = pIndex; |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame^] | 879 | if( !pParse->explain && !hideName ){ |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 880 | sqliteHashInsert(&db->idxHash, pIndex->zName, strlen(zName)+1, pIndex); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 881 | db->flags |= SQLITE_InternChanges; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 882 | } |
| 883 | |
drh | d78eeee | 2001-09-13 16:18:53 +0000 | [diff] [blame] | 884 | /* If the initFlag is 1 it means we are reading the SQL off the |
| 885 | ** "sqlite_master" table on the disk. So do not write to the disk |
| 886 | ** again. Extract the table number from the pParse->newTnum field. |
| 887 | */ |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 888 | if( pParse->initFlag && pTable!=0 ){ |
drh | d78eeee | 2001-09-13 16:18:53 +0000 | [diff] [blame] | 889 | pIndex->tnum = pParse->newTnum; |
| 890 | } |
| 891 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 892 | /* If the initFlag is 0 then create the index on disk. This |
| 893 | ** involves writing the index into the master table and filling in the |
| 894 | ** index with the current table contents. |
| 895 | ** |
| 896 | ** The initFlag is 0 when the user first enters a CREATE INDEX |
| 897 | ** command. The initFlag is 1 when a database is opened and |
| 898 | ** CREATE INDEX statements are read out of the master table. In |
| 899 | ** the latter case the index already exists on disk, which is why |
| 900 | ** we don't want to recreate it. |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 901 | ** |
| 902 | ** If pTable==0 it means this index is generated as a primary key |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 903 | ** or UNIQUE constraint of a CREATE TABLE statement. Since the table |
| 904 | ** has just been created, it contains no data and the index initialization |
| 905 | ** step can be skipped. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 906 | */ |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 907 | else if( pParse->initFlag==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 908 | int n; |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 909 | Vdbe *v; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 910 | int lbl1, lbl2; |
| 911 | int i; |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 912 | int addr; |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame^] | 913 | int isTemp = pTab->isTemp; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 914 | |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 915 | v = sqliteGetVdbe(pParse); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 916 | if( v==0 ) goto exit_create_index; |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 917 | if( pTable!=0 ){ |
| 918 | if( (db->flags & SQLITE_InTrans)==0 ){ |
| 919 | sqliteVdbeAddOp(v, OP_Transaction, 0, 0, 0, 0); |
| 920 | sqliteVdbeAddOp(v, OP_VerifyCookie, db->schema_cookie, 0, 0, 0); |
| 921 | pParse->schemaVerified = 1; |
| 922 | } |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame^] | 923 | if( !isTemp ){ |
| 924 | sqliteVdbeAddOp(v, OP_OpenWrite, 0, 2, MASTER_NAME, 0); |
| 925 | } |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 926 | } |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame^] | 927 | if( !isTemp ){ |
| 928 | sqliteVdbeAddOp(v, OP_NewRecno, 0, 0, 0, 0); |
| 929 | sqliteVdbeAddOp(v, OP_String, 0, 0, "index", 0); |
| 930 | sqliteVdbeAddOp(v, OP_String, 0, 0, pIndex->zName, 0); |
| 931 | sqliteVdbeAddOp(v, OP_String, 0, 0, pTab->zName, 0); |
| 932 | } |
| 933 | addr = sqliteVdbeAddOp(v, OP_CreateIndex, 0, isTemp, 0, 0); |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 934 | sqliteVdbeChangeP3(v, addr, (char*)&pIndex->tnum, -1); |
| 935 | pIndex->tnum = 0; |
| 936 | if( pTable ){ |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame^] | 937 | if( isTemp ){ |
| 938 | sqliteVdbeAddOp(v, OP_OpenWrAux, 1, 0, 0, 0); |
| 939 | }else{ |
| 940 | sqliteVdbeAddOp(v, OP_Dup, 0, 0, 0, 0); |
| 941 | sqliteVdbeAddOp(v, OP_OpenWrite, 1, 0, 0, 0); |
| 942 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 943 | } |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame^] | 944 | if( !isTemp ){ |
| 945 | addr = sqliteVdbeAddOp(v, OP_String, 0, 0, 0, 0); |
| 946 | if( pStart && pEnd ){ |
| 947 | n = (int)pEnd->z - (int)pStart->z + 1; |
| 948 | sqliteVdbeChangeP3(v, addr, pStart->z, n); |
| 949 | } |
| 950 | sqliteVdbeAddOp(v, OP_MakeRecord, 5, 0, 0, 0); |
| 951 | sqliteVdbeAddOp(v, OP_Put, 0, 0, 0, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 952 | } |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 953 | if( pTable ){ |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame^] | 954 | sqliteVdbeAddOp(v, isTemp ? OP_OpenAux : OP_Open, |
| 955 | 2, pTab->tnum, pTab->zName, 0); |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 956 | lbl1 = sqliteVdbeMakeLabel(v); |
| 957 | lbl2 = sqliteVdbeMakeLabel(v); |
| 958 | sqliteVdbeAddOp(v, OP_Rewind, 2, 0, 0, 0); |
| 959 | sqliteVdbeAddOp(v, OP_Next, 2, lbl2, 0, lbl1); |
| 960 | sqliteVdbeAddOp(v, OP_Recno, 2, 0, 0, 0); |
| 961 | for(i=0; i<pIndex->nColumn; i++){ |
| 962 | sqliteVdbeAddOp(v, OP_Column, 2, pIndex->aiColumn[i], 0, 0); |
| 963 | } |
| 964 | sqliteVdbeAddOp(v, OP_MakeIdxKey, pIndex->nColumn, 0, 0, 0); |
| 965 | sqliteVdbeAddOp(v, OP_PutIdx, 1, pIndex->isUnique, 0, 0); |
| 966 | sqliteVdbeAddOp(v, OP_Goto, 0, lbl1, 0, 0); |
| 967 | sqliteVdbeAddOp(v, OP_Noop, 0, 0, 0, lbl2); |
| 968 | sqliteVdbeAddOp(v, OP_Close, 2, 0, 0, 0); |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame^] | 969 | sqliteVdbeAddOp(v, OP_Close, 1, 0, 0, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 970 | } |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 971 | if( pTable!=0 ){ |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame^] | 972 | if( !isTemp ){ |
| 973 | changeCookie(db); |
| 974 | sqliteVdbeAddOp(v, OP_SetCookie, db->next_cookie, 0, 0, 0); |
| 975 | sqliteVdbeAddOp(v, OP_Close, 0, 0, 0, 0); |
| 976 | } |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 977 | if( (db->flags & SQLITE_InTrans)==0 ){ |
| 978 | sqliteVdbeAddOp(v, OP_Commit, 0, 0, 0, 0); |
| 979 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 980 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 981 | } |
| 982 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 983 | /* Clean up before exiting */ |
| 984 | exit_create_index: |
| 985 | sqliteIdListDelete(pList); |
| 986 | sqliteFree(zName); |
| 987 | return; |
| 988 | } |
| 989 | |
| 990 | /* |
| 991 | ** This routine will drop an existing named index. |
| 992 | */ |
| 993 | void sqliteDropIndex(Parse *pParse, Token *pName){ |
| 994 | Index *pIndex; |
| 995 | char *zName; |
| 996 | Vdbe *v; |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 997 | sqlite *db = pParse->db; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 998 | |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 999 | if( pParse->nErr || sqlite_malloc_failed ) return; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1000 | zName = sqliteTableNameFromToken(pName); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1001 | if( zName==0 ) return; |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 1002 | pIndex = sqliteFindIndex(db, zName); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1003 | sqliteFree(zName); |
| 1004 | if( pIndex==0 ){ |
drh | 1d37e28 | 2000-05-30 03:12:21 +0000 | [diff] [blame] | 1005 | sqliteSetNString(&pParse->zErrMsg, "no such index: ", 0, |
| 1006 | pName->z, pName->n, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1007 | pParse->nErr++; |
| 1008 | return; |
| 1009 | } |
| 1010 | |
| 1011 | /* Generate code to remove the index and from the master table */ |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 1012 | v = sqliteGetVdbe(pParse); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1013 | if( v ){ |
| 1014 | static VdbeOp dropIndex[] = { |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 1015 | { OP_OpenWrite, 0, 2, MASTER_NAME}, |
drh | d78eeee | 2001-09-13 16:18:53 +0000 | [diff] [blame] | 1016 | { OP_Rewind, 0, 0, 0}, |
| 1017 | { OP_String, 0, 0, 0}, /* 2 */ |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 1018 | { OP_Next, 0, ADDR(8), 0}, /* 3 */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1019 | { OP_Dup, 0, 0, 0}, |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1020 | { OP_Column, 0, 1, 0}, |
drh | d78eeee | 2001-09-13 16:18:53 +0000 | [diff] [blame] | 1021 | { OP_Ne, 0, ADDR(3), 0}, |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1022 | { OP_Delete, 0, 0, 0}, |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 1023 | { OP_Destroy, 0, 0, 0}, /* 8 */ |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 1024 | { OP_SetCookie, 0, 0, 0}, /* 9 */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1025 | { OP_Close, 0, 0, 0}, |
| 1026 | }; |
| 1027 | int base; |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame^] | 1028 | Table *pTab = pIndex->pTable; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1029 | |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 1030 | if( (db->flags & SQLITE_InTrans)==0 ){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1031 | sqliteVdbeAddOp(v, OP_Transaction, 0, 0, 0, 0); |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 1032 | sqliteVdbeAddOp(v, OP_VerifyCookie, db->schema_cookie, 0, 0, 0); |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 1033 | pParse->schemaVerified = 1; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1034 | } |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame^] | 1035 | if( !pTab->isTemp ){ |
| 1036 | base = sqliteVdbeAddOpList(v, ArraySize(dropIndex), dropIndex); |
| 1037 | sqliteVdbeChangeP3(v, base+2, pIndex->zName, 0); |
| 1038 | changeCookie(db); |
| 1039 | sqliteVdbeChangeP1(v, base+9, db->next_cookie); |
| 1040 | } |
| 1041 | sqliteVdbeAddOp(v, OP_Destroy, pIndex->tnum, pTab->isTemp, 0, 0); |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 1042 | if( (db->flags & SQLITE_InTrans)==0 ){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1043 | sqliteVdbeAddOp(v, OP_Commit, 0, 0, 0, 0); |
| 1044 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1045 | } |
| 1046 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1047 | /* Mark the internal Index structure for deletion by the |
| 1048 | ** sqliteCommitInternalChanges routine. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1049 | */ |
| 1050 | if( !pParse->explain ){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1051 | pIndex->isDelete = 1; |
| 1052 | db->flags |= SQLITE_InternChanges; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1053 | } |
| 1054 | } |
| 1055 | |
| 1056 | /* |
| 1057 | ** Add a new element to the end of an expression list. If pList is |
| 1058 | ** initially NULL, then create a new expression list. |
| 1059 | */ |
| 1060 | ExprList *sqliteExprListAppend(ExprList *pList, Expr *pExpr, Token *pName){ |
| 1061 | int i; |
| 1062 | if( pList==0 ){ |
| 1063 | pList = sqliteMalloc( sizeof(ExprList) ); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1064 | if( pList==0 ) return 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1065 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1066 | if( (pList->nExpr & 7)==0 ){ |
| 1067 | int n = pList->nExpr + 8; |
| 1068 | pList->a = sqliteRealloc(pList->a, n*sizeof(pList->a[0])); |
| 1069 | if( pList->a==0 ){ |
| 1070 | pList->nExpr = 0; |
| 1071 | return pList; |
| 1072 | } |
| 1073 | } |
| 1074 | i = pList->nExpr++; |
| 1075 | pList->a[i].pExpr = pExpr; |
| 1076 | pList->a[i].zName = 0; |
| 1077 | if( pName ){ |
| 1078 | sqliteSetNString(&pList->a[i].zName, pName->z, pName->n, 0); |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 1079 | sqliteDequote(pList->a[i].zName); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1080 | } |
| 1081 | return pList; |
| 1082 | } |
| 1083 | |
| 1084 | /* |
| 1085 | ** Delete an entire expression list. |
| 1086 | */ |
| 1087 | void sqliteExprListDelete(ExprList *pList){ |
| 1088 | int i; |
| 1089 | if( pList==0 ) return; |
| 1090 | for(i=0; i<pList->nExpr; i++){ |
| 1091 | sqliteExprDelete(pList->a[i].pExpr); |
| 1092 | sqliteFree(pList->a[i].zName); |
| 1093 | } |
| 1094 | sqliteFree(pList->a); |
| 1095 | sqliteFree(pList); |
| 1096 | } |
| 1097 | |
| 1098 | /* |
| 1099 | ** Append a new element to the given IdList. Create a new IdList if |
| 1100 | ** need be. |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1101 | ** |
| 1102 | ** A new IdList is returned, or NULL if malloc() fails. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1103 | */ |
| 1104 | IdList *sqliteIdListAppend(IdList *pList, Token *pToken){ |
| 1105 | if( pList==0 ){ |
| 1106 | pList = sqliteMalloc( sizeof(IdList) ); |
| 1107 | if( pList==0 ) return 0; |
| 1108 | } |
| 1109 | if( (pList->nId & 7)==0 ){ |
| 1110 | pList->a = sqliteRealloc(pList->a, (pList->nId+8)*sizeof(pList->a[0]) ); |
| 1111 | if( pList->a==0 ){ |
| 1112 | pList->nId = 0; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1113 | sqliteIdListDelete(pList); |
| 1114 | return 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1115 | } |
| 1116 | } |
| 1117 | memset(&pList->a[pList->nId], 0, sizeof(pList->a[0])); |
| 1118 | if( pToken ){ |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1119 | char **pz = &pList->a[pList->nId].zName; |
| 1120 | sqliteSetNString(pz, pToken->z, pToken->n, 0); |
| 1121 | if( *pz==0 ){ |
| 1122 | sqliteIdListDelete(pList); |
| 1123 | return 0; |
| 1124 | }else{ |
| 1125 | sqliteDequote(*pz); |
| 1126 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1127 | } |
| 1128 | pList->nId++; |
| 1129 | return pList; |
| 1130 | } |
| 1131 | |
| 1132 | /* |
| 1133 | ** Add an alias to the last identifier on the given identifier list. |
| 1134 | */ |
| 1135 | void sqliteIdListAddAlias(IdList *pList, Token *pToken){ |
| 1136 | if( pList && pList->nId>0 ){ |
| 1137 | int i = pList->nId - 1; |
| 1138 | sqliteSetNString(&pList->a[i].zAlias, pToken->z, pToken->n, 0); |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 1139 | sqliteDequote(pList->a[i].zAlias); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1140 | } |
| 1141 | } |
| 1142 | |
| 1143 | /* |
| 1144 | ** Delete an entire IdList |
| 1145 | */ |
| 1146 | void sqliteIdListDelete(IdList *pList){ |
| 1147 | int i; |
| 1148 | if( pList==0 ) return; |
| 1149 | for(i=0; i<pList->nId; i++){ |
| 1150 | sqliteFree(pList->a[i].zName); |
| 1151 | sqliteFree(pList->a[i].zAlias); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1152 | if( pList->a[i].pSelect ){ |
| 1153 | sqliteFree(pList->a[i].zName); |
| 1154 | sqliteSelectDelete(pList->a[i].pSelect); |
| 1155 | sqliteDeleteTable(0, pList->a[i].pTab); |
| 1156 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1157 | } |
| 1158 | sqliteFree(pList->a); |
| 1159 | sqliteFree(pList); |
| 1160 | } |
| 1161 | |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 1162 | |
| 1163 | /* |
| 1164 | ** The COPY command is for compatibility with PostgreSQL and specificially |
| 1165 | ** for the ability to read the output of pg_dump. The format is as |
| 1166 | ** follows: |
| 1167 | ** |
| 1168 | ** COPY table FROM file [USING DELIMITERS string] |
| 1169 | ** |
| 1170 | ** "table" is an existing table name. We will read lines of code from |
| 1171 | ** file to fill this table with data. File might be "stdin". The optional |
| 1172 | ** delimiter string identifies the field separators. The default is a tab. |
| 1173 | */ |
| 1174 | void sqliteCopy( |
| 1175 | Parse *pParse, /* The parser context */ |
| 1176 | Token *pTableName, /* The name of the table into which we will insert */ |
| 1177 | Token *pFilename, /* The file from which to obtain information */ |
| 1178 | Token *pDelimiter /* Use this as the field delimiter */ |
| 1179 | ){ |
| 1180 | Table *pTab; |
| 1181 | char *zTab; |
| 1182 | int i, j; |
| 1183 | Vdbe *v; |
| 1184 | int addr, end; |
| 1185 | Index *pIdx; |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 1186 | sqlite *db = pParse->db; |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 1187 | |
| 1188 | zTab = sqliteTableNameFromToken(pTableName); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1189 | if( sqlite_malloc_failed || zTab==0 ) goto copy_cleanup; |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 1190 | pTab = sqliteFindTable(db, zTab); |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 1191 | sqliteFree(zTab); |
| 1192 | if( pTab==0 ){ |
| 1193 | sqliteSetNString(&pParse->zErrMsg, "no such table: ", 0, |
| 1194 | pTableName->z, pTableName->n, 0); |
| 1195 | pParse->nErr++; |
| 1196 | goto copy_cleanup; |
| 1197 | } |
| 1198 | if( pTab->readOnly ){ |
| 1199 | sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName, |
| 1200 | " may not be modified", 0); |
| 1201 | pParse->nErr++; |
| 1202 | goto copy_cleanup; |
| 1203 | } |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 1204 | v = sqliteGetVdbe(pParse); |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 1205 | if( v ){ |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame^] | 1206 | int openOp; |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 1207 | if( (db->flags & SQLITE_InTrans)==0 ){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1208 | sqliteVdbeAddOp(v, OP_Transaction, 0, 0, 0, 0); |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 1209 | sqliteVdbeAddOp(v, OP_VerifyCookie, db->schema_cookie, 0, 0, 0); |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 1210 | pParse->schemaVerified = 1; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1211 | } |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 1212 | addr = sqliteVdbeAddOp(v, OP_FileOpen, 0, 0, 0, 0); |
| 1213 | sqliteVdbeChangeP3(v, addr, pFilename->z, pFilename->n); |
drh | b766599 | 2000-05-30 17:30:35 +0000 | [diff] [blame] | 1214 | sqliteVdbeDequoteP3(v, addr); |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame^] | 1215 | openOp = pTab->isTemp ? OP_OpenWrAux : OP_OpenWrite; |
| 1216 | sqliteVdbeAddOp(v, openOp, 0, pTab->tnum, pTab->zName, 0); |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 1217 | for(i=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){ |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame^] | 1218 | sqliteVdbeAddOp(v, openOp, i, pIdx->tnum, pIdx->zName, 0); |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 1219 | } |
| 1220 | end = sqliteVdbeMakeLabel(v); |
| 1221 | addr = sqliteVdbeAddOp(v, OP_FileRead, pTab->nCol, end, 0, 0); |
| 1222 | if( pDelimiter ){ |
| 1223 | sqliteVdbeChangeP3(v, addr, pDelimiter->z, pDelimiter->n); |
| 1224 | sqliteVdbeDequoteP3(v, addr); |
| 1225 | }else{ |
| 1226 | sqliteVdbeChangeP3(v, addr, "\t", 1); |
| 1227 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1228 | sqliteVdbeAddOp(v, OP_NewRecno, 0, 0, 0, 0); |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 1229 | if( pTab->pIndex ){ |
| 1230 | sqliteVdbeAddOp(v, OP_Dup, 0, 0, 0, 0); |
| 1231 | } |
| 1232 | for(i=0; i<pTab->nCol; i++){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1233 | sqliteVdbeAddOp(v, OP_FileColumn, i, 0, 0, 0); |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 1234 | } |
| 1235 | sqliteVdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0, 0, 0); |
| 1236 | sqliteVdbeAddOp(v, OP_Put, 0, 0, 0, 0); |
| 1237 | for(i=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){ |
| 1238 | if( pIdx->pNext ){ |
| 1239 | sqliteVdbeAddOp(v, OP_Dup, 0, 0, 0, 0); |
| 1240 | } |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 1241 | for(j=0; j<pIdx->nColumn; j++){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1242 | sqliteVdbeAddOp(v, OP_FileColumn, pIdx->aiColumn[j], 0, 0, 0); |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 1243 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1244 | sqliteVdbeAddOp(v, OP_MakeIdxKey, pIdx->nColumn, 0, 0, 0); |
drh | 717e640 | 2001-09-27 03:22:32 +0000 | [diff] [blame] | 1245 | sqliteVdbeAddOp(v, OP_PutIdx, i, pIdx->isUnique, 0, 0); |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 1246 | } |
| 1247 | sqliteVdbeAddOp(v, OP_Goto, 0, addr, 0, 0); |
| 1248 | sqliteVdbeAddOp(v, OP_Noop, 0, 0, 0, end); |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 1249 | if( (db->flags & SQLITE_InTrans)==0 ){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1250 | sqliteVdbeAddOp(v, OP_Commit, 0, 0, 0, 0); |
| 1251 | } |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 1252 | } |
| 1253 | |
| 1254 | copy_cleanup: |
| 1255 | return; |
| 1256 | } |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 1257 | |
| 1258 | /* |
| 1259 | ** The non-standard VACUUM command is used to clean up the database, |
| 1260 | ** collapse free space, etc. It is modelled after the VACUUM command |
| 1261 | ** in PostgreSQL. |
| 1262 | */ |
| 1263 | void sqliteVacuum(Parse *pParse, Token *pTableName){ |
| 1264 | char *zName; |
| 1265 | Vdbe *v; |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 1266 | sqlite *db = pParse->db; |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 1267 | |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1268 | if( pParse->nErr || sqlite_malloc_failed ) return; |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 1269 | if( pTableName ){ |
| 1270 | zName = sqliteTableNameFromToken(pTableName); |
| 1271 | }else{ |
| 1272 | zName = 0; |
| 1273 | } |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 1274 | if( zName && sqliteFindIndex(db, zName)==0 |
| 1275 | && sqliteFindTable(db, zName)==0 ){ |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 1276 | sqliteSetString(&pParse->zErrMsg, "no such table or index: ", zName, 0); |
| 1277 | pParse->nErr++; |
| 1278 | goto vacuum_cleanup; |
| 1279 | } |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 1280 | v = sqliteGetVdbe(pParse); |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 1281 | if( v==0 ) goto vacuum_cleanup; |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 1282 | if( (db->flags & SQLITE_InTrans)==0 ){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1283 | sqliteVdbeAddOp(v, OP_Transaction, 0, 0, 0, 0); |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 1284 | sqliteVdbeAddOp(v, OP_VerifyCookie, db->schema_cookie, 0, 0, 0); |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 1285 | pParse->schemaVerified = 1; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1286 | } |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 1287 | if( zName ){ |
| 1288 | sqliteVdbeAddOp(v, OP_Reorganize, 0, 0, zName, 0); |
| 1289 | }else{ |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 1290 | Table *pTab; |
| 1291 | Index *pIdx; |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 1292 | HashElem *pE; |
| 1293 | for(pE=sqliteHashFirst(&db->tblHash); pE; pE=sqliteHashNext(pE)){ |
| 1294 | pTab = sqliteHashData(pE); |
| 1295 | sqliteVdbeAddOp(v, OP_Reorganize, 0, 0, pTab->zName, 0); |
| 1296 | for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
| 1297 | sqliteVdbeAddOp(v, OP_Reorganize, 0, 0, pIdx->zName, 0); |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 1298 | } |
| 1299 | } |
| 1300 | } |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 1301 | if( (db->flags & SQLITE_InTrans)==0 ){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1302 | sqliteVdbeAddOp(v, OP_Commit, 0, 0, 0, 0); |
| 1303 | } |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 1304 | |
| 1305 | vacuum_cleanup: |
| 1306 | sqliteFree(zName); |
| 1307 | return; |
| 1308 | } |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 1309 | |
| 1310 | /* |
| 1311 | ** Begin a transaction |
| 1312 | */ |
| 1313 | void sqliteBeginTransaction(Parse *pParse){ |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 1314 | sqlite *db; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1315 | Vdbe *v; |
| 1316 | |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 1317 | if( pParse==0 || (db=pParse->db)==0 || db->pBe==0 ) return; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1318 | if( pParse->nErr || sqlite_malloc_failed ) return; |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 1319 | if( db->flags & SQLITE_InTrans ) return; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1320 | v = sqliteGetVdbe(pParse); |
| 1321 | if( v ){ |
| 1322 | sqliteVdbeAddOp(v, OP_Transaction, 1, 0, 0, 0); |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 1323 | sqliteVdbeAddOp(v, OP_VerifyCookie, db->schema_cookie, 0, 0, 0); |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 1324 | pParse->schemaVerified = 1; |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 1325 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1326 | db->flags |= SQLITE_InTrans; |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 1327 | } |
| 1328 | |
| 1329 | /* |
| 1330 | ** Commit a transaction |
| 1331 | */ |
| 1332 | void sqliteCommitTransaction(Parse *pParse){ |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 1333 | sqlite *db; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1334 | Vdbe *v; |
| 1335 | |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 1336 | if( pParse==0 || (db=pParse->db)==0 || db->pBe==0 ) return; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1337 | if( pParse->nErr || sqlite_malloc_failed ) return; |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 1338 | if( (db->flags & SQLITE_InTrans)==0 ) return; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1339 | v = sqliteGetVdbe(pParse); |
| 1340 | if( v ){ |
| 1341 | sqliteVdbeAddOp(v, OP_Commit, 0, 0, 0, 0); |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 1342 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1343 | db->flags &= ~SQLITE_InTrans; |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 1344 | } |
| 1345 | |
| 1346 | /* |
| 1347 | ** Rollback a transaction |
| 1348 | */ |
| 1349 | void sqliteRollbackTransaction(Parse *pParse){ |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 1350 | sqlite *db; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1351 | Vdbe *v; |
| 1352 | |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 1353 | if( pParse==0 || (db=pParse->db)==0 || db->pBe==0 ) return; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1354 | if( pParse->nErr || sqlite_malloc_failed ) return; |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 1355 | if( (db->flags & SQLITE_InTrans)==0 ) return; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1356 | v = sqliteGetVdbe(pParse); |
| 1357 | if( v ){ |
| 1358 | sqliteVdbeAddOp(v, OP_Rollback, 0, 0, 0, 0); |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 1359 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1360 | db->flags &= ~SQLITE_InTrans; |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 1361 | } |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 1362 | |
| 1363 | /* |
| 1364 | ** Interpret the given string as a boolean value. |
| 1365 | */ |
| 1366 | static int getBoolean(char *z){ |
| 1367 | static char *azTrue[] = { "yes", "on", "true" }; |
| 1368 | int i; |
| 1369 | if( z[0]==0 ) return 0; |
| 1370 | if( isdigit(z[0]) || (z[0]=='-' && isdigit(z[1])) ){ |
| 1371 | return atoi(z); |
| 1372 | } |
| 1373 | for(i=0; i<sizeof(azTrue)/sizeof(azTrue[0]); i++){ |
| 1374 | if( sqliteStrICmp(z,azTrue[i])==0 ) return 1; |
| 1375 | } |
| 1376 | return 0; |
| 1377 | } |
| 1378 | |
| 1379 | /* |
| 1380 | ** Process a pragma statement. |
| 1381 | ** |
| 1382 | ** Pragmas are of this form: |
| 1383 | ** |
| 1384 | ** PRAGMA id = value |
| 1385 | ** |
| 1386 | ** The identifier might also be a string. The value is a string, and |
| 1387 | ** identifier, or a number. If minusFlag is true, then the value is |
| 1388 | ** a number that was preceded by a minus sign. |
| 1389 | */ |
| 1390 | void sqlitePragma(Parse *pParse, Token *pLeft, Token *pRight, int minusFlag){ |
| 1391 | char *zLeft = 0; |
| 1392 | char *zRight = 0; |
| 1393 | sqlite *db = pParse->db; |
| 1394 | |
| 1395 | zLeft = sqliteStrNDup(pLeft->z, pLeft->n); |
| 1396 | sqliteDequote(zLeft); |
| 1397 | if( minusFlag ){ |
| 1398 | zRight = 0; |
| 1399 | sqliteSetNString(&zRight, "-", 1, pRight->z, pRight->n, 0); |
| 1400 | }else{ |
| 1401 | zRight = sqliteStrNDup(pRight->z, pRight->n); |
| 1402 | sqliteDequote(zRight); |
| 1403 | } |
| 1404 | |
| 1405 | if( sqliteStrICmp(zLeft,"cache_size")==0 ){ |
| 1406 | int size = atoi(zRight); |
| 1407 | sqliteBtreeSetCacheSize(db->pBe, size); |
| 1408 | }else |
| 1409 | |
| 1410 | if( sqliteStrICmp(zLeft, "vdbe_trace")==0 ){ |
| 1411 | if( getBoolean(zRight) ){ |
| 1412 | db->flags |= SQLITE_VdbeTrace; |
| 1413 | }else{ |
| 1414 | db->flags &= ~SQLITE_VdbeTrace; |
| 1415 | } |
| 1416 | }else |
| 1417 | |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 1418 | if( sqliteStrICmp(zLeft, "full_column_names")==0 ){ |
| 1419 | if( getBoolean(zRight) ){ |
| 1420 | db->flags |= SQLITE_FullColNames; |
| 1421 | }else{ |
| 1422 | db->flags &= ~SQLITE_FullColNames; |
| 1423 | } |
| 1424 | }else |
| 1425 | |
| 1426 | if( sqliteStrICmp(zLeft, "table_info")==0 ){ |
| 1427 | Table *pTab; |
| 1428 | Vdbe *v; |
| 1429 | pTab = sqliteFindTable(db, zRight); |
| 1430 | if( pTab ) v = sqliteGetVdbe(pParse); |
| 1431 | if( pTab && v ){ |
| 1432 | static VdbeOp tableInfoPreface[] = { |
| 1433 | { OP_ColumnCount, 5, 0, 0}, |
| 1434 | { OP_ColumnName, 0, 0, "cid"}, |
| 1435 | { OP_ColumnName, 1, 0, "name"}, |
| 1436 | { OP_ColumnName, 2, 0, "type"}, |
| 1437 | { OP_ColumnName, 3, 0, "notnull"}, |
| 1438 | { OP_ColumnName, 4, 0, "dflt_value"}, |
| 1439 | }; |
| 1440 | int i; |
| 1441 | sqliteVdbeAddOpList(v, ArraySize(tableInfoPreface), tableInfoPreface); |
| 1442 | for(i=0; i<pTab->nCol; i++){ |
| 1443 | sqliteVdbeAddOp(v, OP_Integer, i, 0, 0, 0); |
| 1444 | sqliteVdbeAddOp(v, OP_String, 0, 0, pTab->aCol[i].zName, 0); |
| 1445 | sqliteVdbeAddOp(v, OP_String, 0, 0, |
| 1446 | pTab->aCol[i].zType ? pTab->aCol[i].zType : "text", 0); |
| 1447 | sqliteVdbeAddOp(v, OP_Integer, pTab->aCol[i].notNull, 0, 0, 0); |
| 1448 | sqliteVdbeAddOp(v, OP_String, 0, 0, pTab->aCol[i].zDflt, 0); |
| 1449 | sqliteVdbeAddOp(v, OP_Callback, 5, 0, 0, 0); |
| 1450 | } |
| 1451 | } |
| 1452 | }else |
| 1453 | |
| 1454 | if( sqliteStrICmp(zLeft, "index_info")==0 ){ |
| 1455 | Index *pIdx; |
| 1456 | Table *pTab; |
| 1457 | Vdbe *v; |
| 1458 | pIdx = sqliteFindIndex(db, zRight); |
| 1459 | if( pIdx ) v = sqliteGetVdbe(pParse); |
| 1460 | if( pIdx && v ){ |
| 1461 | static VdbeOp tableInfoPreface[] = { |
| 1462 | { OP_ColumnCount, 3, 0, 0}, |
| 1463 | { OP_ColumnName, 0, 0, "seqno"}, |
| 1464 | { OP_ColumnName, 1, 0, "cid"}, |
| 1465 | { OP_ColumnName, 2, 0, "name"}, |
| 1466 | }; |
| 1467 | int i; |
| 1468 | pTab = pIdx->pTable; |
| 1469 | sqliteVdbeAddOpList(v, ArraySize(tableInfoPreface), tableInfoPreface); |
| 1470 | for(i=0; i<pIdx->nColumn; i++){ |
| 1471 | sqliteVdbeAddOp(v, OP_Integer, i, 0, 0, 0); |
| 1472 | sqliteVdbeAddOp(v, OP_Integer, pIdx->aiColumn[i], 0, 0, 0); |
| 1473 | sqliteVdbeAddOp(v, OP_String, 0, 0, |
| 1474 | pTab->aCol[pIdx->aiColumn[i]].zName, 0); |
| 1475 | sqliteVdbeAddOp(v, OP_Callback, 3, 0, 0, 0); |
| 1476 | } |
| 1477 | } |
| 1478 | }else |
| 1479 | |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 1480 | #ifndef NDEBUG |
| 1481 | if( sqliteStrICmp(zLeft, "parser_trace")==0 ){ |
| 1482 | extern void sqliteParserTrace(FILE*, char *); |
| 1483 | if( getBoolean(zRight) ){ |
| 1484 | sqliteParserTrace(stdout, "parser: "); |
| 1485 | }else{ |
| 1486 | sqliteParserTrace(0, 0); |
| 1487 | } |
| 1488 | }else |
| 1489 | #endif |
| 1490 | |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame^] | 1491 | {} |
| 1492 | sqliteFree(zLeft); |
| 1493 | sqliteFree(zRight); |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 1494 | } |