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 |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 20 | ** creating ID lists |
drh | bed8690 | 2000-06-02 13:27:59 +0000 | [diff] [blame] | 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 | 113088e | 2003-03-20 01:16:58 +0000 | [diff] [blame] | 28 | ** $Id: build.c,v 1.133 2003/03/20 01:16:58 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 | /* |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 34 | ** This routine is called when a new SQL statement is beginning to |
| 35 | ** be parsed. Check to see if the schema for the database needs |
| 36 | ** to be read from the SQLITE_MASTER and SQLITE_TEMP_MASTER tables. |
| 37 | ** If it does, then read it. |
| 38 | */ |
| 39 | void sqliteBeginParse(Parse *pParse, int explainFlag){ |
| 40 | sqlite *db = pParse->db; |
| 41 | pParse->explain = explainFlag; |
| 42 | if((db->flags & SQLITE_Initialized)==0 && pParse->initFlag==0 ){ |
| 43 | int rc = sqliteInit(db, &pParse->zErrMsg); |
| 44 | if( rc!=SQLITE_OK ){ |
| 45 | pParse->rc = rc; |
| 46 | pParse->nErr++; |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | /* |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 52 | ** This is a fake callback procedure used when sqlite_exec() is |
| 53 | ** invoked with a NULL callback pointer. If we pass a NULL callback |
| 54 | ** pointer into sqliteVdbeExec() it will return at every OP_Callback, |
| 55 | ** which we do not want it to do. So we substitute a pointer to this |
| 56 | ** procedure in place of the NULL. |
| 57 | */ |
| 58 | static int fakeCallback(void *NotUsed, int n, char **az1, char **az2){ |
| 59 | return 0; |
| 60 | } |
| 61 | |
| 62 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 63 | ** This routine is called after a single SQL statement has been |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 64 | ** parsed and we want to execute the VDBE code to implement |
| 65 | ** that statement. Prior action routines should have already |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 66 | ** constructed VDBE code to do the work of the SQL statement. |
| 67 | ** This routine just has to execute the VDBE code. |
| 68 | ** |
| 69 | ** Note that if an error occurred, it might be the case that |
| 70 | ** no VDBE code was generated. |
| 71 | */ |
| 72 | void sqliteExec(Parse *pParse){ |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 73 | int rc = SQLITE_OK; |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 74 | sqlite *db = pParse->db; |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 75 | Vdbe *v = pParse->pVdbe; |
| 76 | int (*xCallback)(void*,int,char**,char**); |
| 77 | |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 78 | if( sqlite_malloc_failed ) return; |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 79 | xCallback = pParse->xCallback; |
| 80 | if( xCallback==0 && pParse->useCallback ) xCallback = fakeCallback; |
| 81 | if( v && pParse->nErr==0 ){ |
| 82 | FILE *trace = (db->flags & SQLITE_VdbeTrace)!=0 ? stdout : 0; |
| 83 | sqliteVdbeTrace(v, trace); |
| 84 | sqliteVdbeMakeReady(v, xCallback, pParse->pArg, pParse->explain); |
| 85 | if( pParse->useCallback ){ |
| 86 | if( pParse->explain ){ |
| 87 | rc = sqliteVdbeList(v); |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 88 | db->next_cookie = db->aDb[0].schema_cookie; |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 89 | }else{ |
| 90 | sqliteVdbeExec(v); |
| 91 | } |
| 92 | rc = sqliteVdbeFinalize(v, &pParse->zErrMsg); |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 93 | if( rc ) pParse->nErr++; |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 94 | pParse->pVdbe = 0; |
| 95 | pParse->rc = rc; |
| 96 | if( rc ) pParse->nErr++; |
| 97 | }else{ |
| 98 | pParse->rc = pParse->nErr ? SQLITE_ERROR : SQLITE_DONE; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 99 | } |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 100 | pParse->colNamesSet = 0; |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 101 | pParse->schemaVerified = 0; |
drh | 483750b | 2003-01-29 18:46:51 +0000 | [diff] [blame] | 102 | }else if( pParse->useCallback==0 ){ |
| 103 | pParse->rc = SQLITE_ERROR; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 104 | } |
drh | a226d05 | 2002-09-25 19:04:07 +0000 | [diff] [blame] | 105 | pParse->nTab = 0; |
| 106 | pParse->nMem = 0; |
| 107 | pParse->nSet = 0; |
| 108 | pParse->nAgg = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | /* |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 112 | ** Locate the in-memory structure that describes |
| 113 | ** a particular database table given the name |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 114 | ** of that table. Return NULL if not found. |
| 115 | */ |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 116 | Table *sqliteFindTable(sqlite *db, const char *zName){ |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 117 | Table *p; |
| 118 | p = sqliteHashFind(&db->tblHash, zName, strlen(zName)+1); |
drh | 74e24cd | 2002-01-09 03:19:59 +0000 | [diff] [blame] | 119 | return p; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | /* |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 123 | ** Locate the in-memory structure that describes |
| 124 | ** a particular index given the name of that index. |
| 125 | ** Return NULL if not found. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 126 | */ |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 127 | Index *sqliteFindIndex(sqlite *db, const char *zName){ |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 128 | Index *p; |
| 129 | p = sqliteHashFind(&db->idxHash, zName, strlen(zName)+1); |
drh | 74e24cd | 2002-01-09 03:19:59 +0000 | [diff] [blame] | 130 | return p; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | /* |
| 134 | ** Remove the given index from the index hash table, and free |
| 135 | ** its memory structures. |
| 136 | ** |
drh | d229ca9 | 2002-01-09 13:30:41 +0000 | [diff] [blame] | 137 | ** The index is removed from the database hash tables but |
| 138 | ** it is not unlinked from the Table that it indexes. |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 139 | ** Unlinking from the Table must be done by the calling function. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 140 | */ |
drh | 74e24cd | 2002-01-09 03:19:59 +0000 | [diff] [blame] | 141 | static void sqliteDeleteIndex(sqlite *db, Index *p){ |
drh | d229ca9 | 2002-01-09 13:30:41 +0000 | [diff] [blame] | 142 | Index *pOld; |
| 143 | assert( db!=0 && p->zName!=0 ); |
| 144 | pOld = sqliteHashInsert(&db->idxHash, p->zName, strlen(p->zName)+1, 0); |
| 145 | if( pOld!=0 && pOld!=p ){ |
| 146 | sqliteHashInsert(&db->idxHash, pOld->zName, strlen(pOld->zName)+1, pOld); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 147 | } |
drh | 74e24cd | 2002-01-09 03:19:59 +0000 | [diff] [blame] | 148 | sqliteFree(p); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | /* |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 152 | ** Unlink the given index from its table, then remove |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 153 | ** the index from the index hash table and free its memory |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 154 | ** structures. |
| 155 | */ |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 156 | void sqliteUnlinkAndDeleteIndex(sqlite *db, Index *pIndex){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 157 | if( pIndex->pTable->pIndex==pIndex ){ |
| 158 | pIndex->pTable->pIndex = pIndex->pNext; |
| 159 | }else{ |
| 160 | Index *p; |
| 161 | for(p=pIndex->pTable->pIndex; p && p->pNext!=pIndex; p=p->pNext){} |
| 162 | if( p && p->pNext==pIndex ){ |
| 163 | p->pNext = pIndex->pNext; |
| 164 | } |
| 165 | } |
| 166 | sqliteDeleteIndex(db, pIndex); |
| 167 | } |
| 168 | |
| 169 | /* |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 170 | ** Erase all schema information from the in-memory hash tables of |
| 171 | ** database connection. This routine is called to reclaim memory |
| 172 | ** before the connection closes. It is also called during a rollback |
| 173 | ** if there were schema changes during the transaction. |
drh | 74e24cd | 2002-01-09 03:19:59 +0000 | [diff] [blame] | 174 | */ |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 175 | void sqliteResetInternalSchema(sqlite *db){ |
| 176 | HashElem *pElem; |
| 177 | Hash temp1; |
| 178 | Hash temp2; |
| 179 | |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 180 | sqliteHashClear(&db->aFKey); |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 181 | temp1 = db->tblHash; |
| 182 | temp2 = db->trigHash; |
| 183 | sqliteHashInit(&db->trigHash, SQLITE_HASH_STRING, 0); |
| 184 | sqliteHashClear(&db->idxHash); |
| 185 | for(pElem=sqliteHashFirst(&temp2); pElem; pElem=sqliteHashNext(pElem)){ |
| 186 | Trigger *pTrigger = sqliteHashData(pElem); |
| 187 | sqliteDeleteTrigger(pTrigger); |
drh | 74e24cd | 2002-01-09 03:19:59 +0000 | [diff] [blame] | 188 | } |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 189 | sqliteHashClear(&temp2); |
| 190 | sqliteHashInit(&db->tblHash, SQLITE_HASH_STRING, 0); |
| 191 | for(pElem=sqliteHashFirst(&temp1); pElem; pElem=sqliteHashNext(pElem)){ |
| 192 | Table *pTab = sqliteHashData(pElem); |
| 193 | sqliteDeleteTable(db, pTab); |
| 194 | } |
| 195 | sqliteHashClear(&temp1); |
| 196 | db->flags &= ~(SQLITE_Initialized|SQLITE_InternChanges); |
| 197 | } |
| 198 | |
| 199 | /* |
| 200 | ** This routine is called whenever a rollback occurs. If there were |
| 201 | ** schema changes during the transaction, then we have to reset the |
| 202 | ** internal hash tables and reload them from disk. |
| 203 | */ |
| 204 | void sqliteRollbackInternalChanges(sqlite *db){ |
| 205 | if( db->flags & SQLITE_InternChanges ){ |
| 206 | sqliteResetInternalSchema(db); |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | /* |
| 211 | ** This routine is called when a commit occurs. |
| 212 | */ |
| 213 | void sqliteCommitInternalChanges(sqlite *db){ |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 214 | db->aDb[0].schema_cookie = db->next_cookie; |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 215 | db->flags &= ~SQLITE_InternChanges; |
drh | 74e24cd | 2002-01-09 03:19:59 +0000 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 219 | ** Remove the memory data structures associated with the given |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 220 | ** Table. No changes are made to disk by this routine. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 221 | ** |
| 222 | ** This routine just deletes the data structure. It does not unlink |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 223 | ** the table data structure from the hash table. Nor does it remove |
| 224 | ** foreign keys from the sqlite.aFKey hash table. But it does destroy |
| 225 | ** memory structures of the indices and foreign keys associated with |
| 226 | ** the table. |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 227 | ** |
| 228 | ** Indices associated with the table are unlinked from the "db" |
| 229 | ** data structure if db!=NULL. If db==NULL, indices attached to |
| 230 | ** the table are deleted, but it is assumed they have already been |
| 231 | ** unlinked. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 232 | */ |
| 233 | void sqliteDeleteTable(sqlite *db, Table *pTable){ |
| 234 | int i; |
| 235 | Index *pIndex, *pNext; |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 236 | FKey *pFKey, *pNextFKey; |
| 237 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 238 | if( pTable==0 ) return; |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 239 | |
| 240 | /* Delete all indices associated with this table |
| 241 | */ |
| 242 | for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){ |
| 243 | pNext = pIndex->pNext; |
| 244 | sqliteDeleteIndex(db, pIndex); |
| 245 | } |
| 246 | |
| 247 | /* Delete all foreign keys associated with this table. The keys |
| 248 | ** should have already been unlinked from the db->aFKey hash table |
| 249 | */ |
| 250 | for(pFKey=pTable->pFKey; pFKey; pFKey=pNextFKey){ |
| 251 | pNextFKey = pFKey->pNextFrom; |
| 252 | assert( sqliteHashFind(&db->aFKey,pFKey->zTo,strlen(pFKey->zTo)+1)!=pFKey ); |
| 253 | sqliteFree(pFKey); |
| 254 | } |
| 255 | |
| 256 | /* Delete the Table structure itself. |
| 257 | */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 258 | for(i=0; i<pTable->nCol; i++){ |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 259 | sqliteFree(pTable->aCol[i].zName); |
| 260 | sqliteFree(pTable->aCol[i].zDflt); |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 261 | sqliteFree(pTable->aCol[i].zType); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 262 | } |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 263 | sqliteFree(pTable->zName); |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 264 | sqliteFree(pTable->aCol); |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 265 | sqliteSelectDelete(pTable->pSelect); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 266 | sqliteFree(pTable); |
| 267 | } |
| 268 | |
| 269 | /* |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 270 | ** Unlink the given table from the hash tables and the delete the |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 271 | ** table structure with all its indices and foreign keys. |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 272 | */ |
drh | 74e24cd | 2002-01-09 03:19:59 +0000 | [diff] [blame] | 273 | static void sqliteUnlinkAndDeleteTable(sqlite *db, Table *p){ |
drh | d229ca9 | 2002-01-09 13:30:41 +0000 | [diff] [blame] | 274 | Table *pOld; |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 275 | FKey *pF1, *pF2; |
drh | d229ca9 | 2002-01-09 13:30:41 +0000 | [diff] [blame] | 276 | assert( db!=0 ); |
| 277 | pOld = sqliteHashInsert(&db->tblHash, p->zName, strlen(p->zName)+1, 0); |
| 278 | assert( pOld==0 || pOld==p ); |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 279 | for(pF1=p->pFKey; pF1; pF1=pF1->pNextFrom){ |
| 280 | int nTo = strlen(pF1->zTo) + 1; |
| 281 | pF2 = sqliteHashFind(&db->aFKey, pF1->zTo, nTo); |
| 282 | if( pF2==pF1 ){ |
| 283 | sqliteHashInsert(&db->aFKey, pF1->zTo, nTo, pF1->pNextTo); |
| 284 | }else{ |
| 285 | while( pF2 && pF2->pNextTo!=pF1 ){ pF2=pF2->pNextTo; } |
| 286 | if( pF2 ){ |
| 287 | pF2->pNextTo = pF1->pNextTo; |
| 288 | } |
| 289 | } |
| 290 | } |
drh | 74e24cd | 2002-01-09 03:19:59 +0000 | [diff] [blame] | 291 | sqliteDeleteTable(db, p); |
| 292 | } |
| 293 | |
| 294 | /* |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 295 | ** Construct the name of a user table or index from a token. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 296 | ** |
| 297 | ** Space to hold the name is obtained from sqliteMalloc() and must |
| 298 | ** be freed by the calling function. |
| 299 | */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 300 | char *sqliteTableNameFromToken(Token *pName){ |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 301 | char *zName = sqliteStrNDup(pName->z, pName->n); |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 302 | sqliteDequote(zName); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 303 | return zName; |
| 304 | } |
| 305 | |
| 306 | /* |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 307 | ** Generate code to open the appropriate master table. The table |
| 308 | ** opened will be SQLITE_MASTER for persistent tables and |
| 309 | ** SQLITE_TEMP_MASTER for temporary tables. The table is opened |
| 310 | ** on cursor 0. |
| 311 | */ |
| 312 | void sqliteOpenMasterTable(Vdbe *v, int isTemp){ |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 313 | sqliteVdbeAddOp(v, OP_Integer, isTemp, 0); |
| 314 | sqliteVdbeAddOp(v, OP_OpenWrite, 0, 2); |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 318 | ** Begin constructing a new table representation in memory. This is |
| 319 | ** the first of several action routines that get called in response |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 320 | ** to a CREATE TABLE statement. In particular, this routine is called |
| 321 | ** after seeing tokens "CREATE" and "TABLE" and the table name. The |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 322 | ** pStart token is the CREATE and pName is the table name. The isTemp |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 323 | ** flag is true if the table should be stored in the auxiliary database |
| 324 | ** file instead of in the main database file. This is normally the case |
| 325 | ** when the "TEMP" or "TEMPORARY" keyword occurs in between |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 326 | ** CREATE and TABLE. |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 327 | ** |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 328 | ** The new table record is initialized and put in pParse->pNewTable. |
| 329 | ** As more of the CREATE TABLE statement is parsed, additional action |
| 330 | ** routines will be called to add more information to this record. |
| 331 | ** At the end of the CREATE TABLE statement, the sqliteEndTable() routine |
| 332 | ** is called to complete the construction of the new table record. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 333 | */ |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 334 | void sqliteStartTable( |
| 335 | Parse *pParse, /* Parser context */ |
| 336 | Token *pStart, /* The "CREATE" token */ |
| 337 | Token *pName, /* Name of table or view to create */ |
| 338 | int isTemp, /* True if this is a TEMP table */ |
| 339 | int isView /* True if this is a VIEW */ |
| 340 | ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 341 | Table *pTable; |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 342 | Index *pIdx; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 343 | char *zName; |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 344 | sqlite *db = pParse->db; |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 345 | Vdbe *v; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 346 | |
| 347 | pParse->sFirstToken = *pStart; |
| 348 | zName = sqliteTableNameFromToken(pName); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 349 | if( zName==0 ) return; |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 350 | #ifndef SQLITE_OMIT_AUTHORIZATION |
| 351 | if( sqliteAuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(isTemp), 0) ){ |
drh | 77ad4e4 | 2003-01-14 02:49:27 +0000 | [diff] [blame] | 352 | sqliteFree(zName); |
drh | ed6c867 | 2003-01-12 18:02:16 +0000 | [diff] [blame] | 353 | return; |
| 354 | } |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 355 | { |
| 356 | int code; |
| 357 | if( isView ){ |
| 358 | if( isTemp ){ |
| 359 | code = SQLITE_CREATE_TEMP_VIEW; |
| 360 | }else{ |
| 361 | code = SQLITE_CREATE_VIEW; |
| 362 | } |
| 363 | }else{ |
| 364 | if( isTemp ){ |
| 365 | code = SQLITE_CREATE_TEMP_TABLE; |
| 366 | }else{ |
| 367 | code = SQLITE_CREATE_TABLE; |
| 368 | } |
| 369 | } |
| 370 | if( sqliteAuthCheck(pParse, code, zName, 0) ){ |
drh | 77ad4e4 | 2003-01-14 02:49:27 +0000 | [diff] [blame] | 371 | sqliteFree(zName); |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 372 | return; |
| 373 | } |
| 374 | } |
| 375 | #endif |
| 376 | |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 377 | |
| 378 | /* Before trying to create a temporary table, make sure the Btree for |
| 379 | ** holding temporary tables is open. |
| 380 | */ |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 381 | if( isTemp && db->aDb[1].pBt==0 ){ |
| 382 | int rc = sqliteBtreeOpen(0, 0, MAX_PAGES, &db->aDb[1].pBt); |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 383 | if( rc!=SQLITE_OK ){ |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 384 | sqliteSetString(&pParse->zErrMsg, "unable to open a temporary database " |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 385 | "file for storing temporary tables", 0); |
| 386 | pParse->nErr++; |
| 387 | return; |
| 388 | } |
| 389 | if( db->flags & SQLITE_InTrans ){ |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 390 | rc = sqliteBtreeBeginTrans(db->aDb[1].pBt); |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 391 | if( rc!=SQLITE_OK ){ |
| 392 | sqliteSetNString(&pParse->zErrMsg, "unable to get a write lock on " |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 393 | "the temporary database file", 0); |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 394 | pParse->nErr++; |
| 395 | return; |
| 396 | } |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | /* Make sure the new table name does not collide with an existing |
| 401 | ** index or table name. Issue an error message if it does. |
| 402 | ** |
| 403 | ** If we are re-reading the sqlite_master table because of a schema |
| 404 | ** change and a new permanent table is found whose name collides with |
| 405 | ** an existing temporary table, then ignore the new permanent table. |
| 406 | ** We will continue parsing, but the pParse->nameClash flag will be set |
| 407 | ** so we will know to discard the table record once parsing has finished. |
| 408 | */ |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 409 | pTable = sqliteFindTable(db, zName); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 410 | if( pTable!=0 ){ |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 411 | if( pTable->isTemp && pParse->initFlag ){ |
| 412 | pParse->nameClash = 1; |
| 413 | }else{ |
| 414 | sqliteSetNString(&pParse->zErrMsg, "table ", 0, pName->z, pName->n, |
| 415 | " already exists", 0, 0); |
| 416 | sqliteFree(zName); |
| 417 | pParse->nErr++; |
| 418 | return; |
| 419 | } |
| 420 | }else{ |
| 421 | pParse->nameClash = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 422 | } |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 423 | if( (pIdx = sqliteFindIndex(db, zName))!=0 && |
| 424 | (!pIdx->pTable->isTemp || !pParse->initFlag) ){ |
drh | 1d37e28 | 2000-05-30 03:12:21 +0000 | [diff] [blame] | 425 | sqliteSetString(&pParse->zErrMsg, "there is already an index named ", |
| 426 | zName, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 427 | sqliteFree(zName); |
| 428 | pParse->nErr++; |
| 429 | return; |
| 430 | } |
| 431 | pTable = sqliteMalloc( sizeof(Table) ); |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 432 | if( pTable==0 ){ |
| 433 | sqliteFree(zName); |
| 434 | return; |
| 435 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 436 | pTable->zName = zName; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 437 | pTable->nCol = 0; |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 438 | pTable->aCol = 0; |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 439 | pTable->iPKey = -1; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 440 | pTable->pIndex = 0; |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 441 | pTable->isTemp = isTemp; |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 442 | if( pParse->pNewTable ) sqliteDeleteTable(db, pParse->pNewTable); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 443 | pParse->pNewTable = pTable; |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 444 | |
| 445 | /* Begin generating the code that will insert the table record into |
| 446 | ** the SQLITE_MASTER table. Note in particular that we must go ahead |
| 447 | ** and allocate the record number for the table entry now. Before any |
| 448 | ** PRIMARY KEY or UNIQUE keywords are parsed. Those keywords will cause |
| 449 | ** indices to be created and the table record must come before the |
| 450 | ** indices. Hence, the record number for the table must be allocated |
| 451 | ** now. |
| 452 | */ |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 453 | if( !pParse->initFlag && (v = sqliteGetVdbe(pParse))!=0 ){ |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 454 | sqliteBeginWriteOperation(pParse, 0, isTemp); |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 455 | if( !isTemp ){ |
drh | 603240c | 2002-03-05 01:11:12 +0000 | [diff] [blame] | 456 | sqliteVdbeAddOp(v, OP_Integer, db->file_format, 0); |
| 457 | sqliteVdbeAddOp(v, OP_SetCookie, 0, 1); |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 458 | } |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 459 | sqliteOpenMasterTable(v, isTemp); |
| 460 | sqliteVdbeAddOp(v, OP_NewRecno, 0, 0); |
| 461 | sqliteVdbeAddOp(v, OP_Dup, 0, 0); |
| 462 | sqliteVdbeAddOp(v, OP_String, 0, 0); |
| 463 | sqliteVdbeAddOp(v, OP_PutIntKey, 0, 0); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 464 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | /* |
| 468 | ** Add a new column to the table currently being constructed. |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 469 | ** |
| 470 | ** The parser calls this routine once for each column declaration |
| 471 | ** in a CREATE TABLE statement. sqliteStartTable() gets called |
| 472 | ** first to get things going. Then this routine is called for each |
| 473 | ** column. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 474 | */ |
| 475 | void sqliteAddColumn(Parse *pParse, Token *pName){ |
| 476 | Table *p; |
drh | 97fc3d0 | 2002-05-22 21:27:03 +0000 | [diff] [blame] | 477 | int i; |
| 478 | char *z = 0; |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 479 | Column *pCol; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 480 | if( (p = pParse->pNewTable)==0 ) return; |
drh | 97fc3d0 | 2002-05-22 21:27:03 +0000 | [diff] [blame] | 481 | sqliteSetNString(&z, pName->z, pName->n, 0); |
| 482 | if( z==0 ) return; |
| 483 | sqliteDequote(z); |
| 484 | for(i=0; i<p->nCol; i++){ |
| 485 | if( sqliteStrICmp(z, p->aCol[i].zName)==0 ){ |
| 486 | sqliteSetString(&pParse->zErrMsg, "duplicate column name: ", z, 0); |
| 487 | pParse->nErr++; |
| 488 | sqliteFree(z); |
| 489 | return; |
| 490 | } |
| 491 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 492 | if( (p->nCol & 0x7)==0 ){ |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 493 | Column *aNew; |
| 494 | aNew = sqliteRealloc( p->aCol, (p->nCol+8)*sizeof(p->aCol[0])); |
| 495 | if( aNew==0 ) return; |
| 496 | p->aCol = aNew; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 497 | } |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 498 | pCol = &p->aCol[p->nCol]; |
| 499 | memset(pCol, 0, sizeof(p->aCol[0])); |
| 500 | pCol->zName = z; |
| 501 | pCol->sortOrder = SQLITE_SO_NUM; |
| 502 | p->nCol++; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | /* |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 506 | ** This routine is called by the parser while in the middle of |
| 507 | ** parsing a CREATE TABLE statement. A "NOT NULL" constraint has |
| 508 | ** been seen on a column. This routine sets the notNull flag on |
| 509 | ** the column currently under construction. |
| 510 | */ |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 511 | void sqliteAddNotNull(Parse *pParse, int onError){ |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 512 | Table *p; |
| 513 | int i; |
| 514 | if( (p = pParse->pNewTable)==0 ) return; |
| 515 | i = p->nCol-1; |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 516 | if( i>=0 ) p->aCol[i].notNull = onError; |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | /* |
| 520 | ** This routine is called by the parser while in the middle of |
| 521 | ** parsing a CREATE TABLE statement. The pFirst token is the first |
| 522 | ** token in the sequence of tokens that describe the type of the |
| 523 | ** column currently under construction. pLast is the last token |
| 524 | ** in the sequence. Use this information to construct a string |
| 525 | ** that contains the typename of the column and store that string |
| 526 | ** in zType. |
| 527 | */ |
| 528 | void sqliteAddColumnType(Parse *pParse, Token *pFirst, Token *pLast){ |
| 529 | Table *p; |
| 530 | int i, j; |
| 531 | int n; |
| 532 | char *z, **pz; |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 533 | Column *pCol; |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 534 | if( (p = pParse->pNewTable)==0 ) return; |
| 535 | i = p->nCol-1; |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 536 | if( i<0 ) return; |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 537 | pCol = &p->aCol[i]; |
| 538 | pz = &pCol->zType; |
drh | 5a2c2c2 | 2001-11-21 02:21:11 +0000 | [diff] [blame] | 539 | n = pLast->n + Addr(pLast->z) - Addr(pFirst->z); |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 540 | sqliteSetNString(pz, pFirst->z, n, 0); |
| 541 | z = *pz; |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 542 | if( z==0 ) return; |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 543 | for(i=j=0; z[i]; i++){ |
| 544 | int c = z[i]; |
| 545 | if( isspace(c) ) continue; |
| 546 | z[j++] = c; |
| 547 | } |
| 548 | z[j] = 0; |
drh | 3d037a9 | 2002-08-15 01:26:09 +0000 | [diff] [blame] | 549 | if( pParse->db->file_format>=4 ){ |
drh | fcb78a4 | 2003-01-18 20:11:05 +0000 | [diff] [blame] | 550 | pCol->sortOrder = sqliteCollateType(z, n); |
| 551 | }else{ |
| 552 | pCol->sortOrder = SQLITE_SO_NUM; |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 553 | } |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | /* |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 557 | ** The given token is the default value for the last column added to |
| 558 | ** the table currently under construction. If "minusFlag" is true, it |
| 559 | ** means the value token was preceded by a minus sign. |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 560 | ** |
| 561 | ** This routine is called by the parser while in the middle of |
| 562 | ** parsing a CREATE TABLE statement. |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 563 | */ |
| 564 | void sqliteAddDefaultValue(Parse *pParse, Token *pVal, int minusFlag){ |
| 565 | Table *p; |
| 566 | int i; |
| 567 | char **pz; |
| 568 | if( (p = pParse->pNewTable)==0 ) return; |
| 569 | i = p->nCol-1; |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 570 | if( i<0 ) return; |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 571 | pz = &p->aCol[i].zDflt; |
| 572 | if( minusFlag ){ |
| 573 | sqliteSetNString(pz, "-", 1, pVal->z, pVal->n, 0); |
| 574 | }else{ |
| 575 | sqliteSetNString(pz, pVal->z, pVal->n, 0); |
| 576 | } |
| 577 | sqliteDequote(*pz); |
| 578 | } |
| 579 | |
| 580 | /* |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 581 | ** Designate the PRIMARY KEY for the table. pList is a list of names |
| 582 | ** of columns that form the primary key. If pList is NULL, then the |
| 583 | ** most recently added column of the table is the primary key. |
| 584 | ** |
| 585 | ** A table can have at most one primary key. If the table already has |
| 586 | ** a primary key (and this is the second primary key) then create an |
| 587 | ** error. |
| 588 | ** |
| 589 | ** If the PRIMARY KEY is on a single column whose datatype is INTEGER, |
| 590 | ** then we will try to use that column as the row id. (Exception: |
| 591 | ** For backwards compatibility with older databases, do not do this |
| 592 | ** if the file format version number is less than 1.) Set the Table.iPKey |
| 593 | ** field of the table under construction to be the index of the |
| 594 | ** INTEGER PRIMARY KEY column. Table.iPKey is set to -1 if there is |
| 595 | ** no INTEGER PRIMARY KEY. |
| 596 | ** |
| 597 | ** If the key is not an INTEGER PRIMARY KEY, then create a unique |
| 598 | ** index for the key. No index is created for INTEGER PRIMARY KEYs. |
| 599 | */ |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 600 | void sqliteAddPrimaryKey(Parse *pParse, IdList *pList, int onError){ |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 601 | Table *pTab = pParse->pNewTable; |
| 602 | char *zType = 0; |
| 603 | int iCol = -1; |
drh | e0194f2 | 2003-02-26 13:52:51 +0000 | [diff] [blame] | 604 | if( pTab==0 ) goto primary_key_exit; |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 605 | if( pTab->hasPrimKey ){ |
| 606 | sqliteSetString(&pParse->zErrMsg, "table \"", pTab->zName, |
| 607 | "\" has more than one primary key", 0); |
| 608 | pParse->nErr++; |
drh | e0194f2 | 2003-02-26 13:52:51 +0000 | [diff] [blame] | 609 | goto primary_key_exit; |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 610 | } |
| 611 | pTab->hasPrimKey = 1; |
| 612 | if( pList==0 ){ |
| 613 | iCol = pTab->nCol - 1; |
| 614 | }else if( pList->nId==1 ){ |
| 615 | for(iCol=0; iCol<pTab->nCol; iCol++){ |
| 616 | if( sqliteStrICmp(pList->a[0].zName, pTab->aCol[iCol].zName)==0 ) break; |
| 617 | } |
| 618 | } |
| 619 | if( iCol>=0 && iCol<pTab->nCol ){ |
| 620 | zType = pTab->aCol[iCol].zType; |
| 621 | } |
| 622 | if( pParse->db->file_format>=1 && |
| 623 | zType && sqliteStrICmp(zType, "INTEGER")==0 ){ |
| 624 | pTab->iPKey = iCol; |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 625 | pTab->keyConf = onError; |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 626 | }else{ |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 627 | sqliteCreateIndex(pParse, 0, 0, pList, onError, 0, 0); |
drh | e0194f2 | 2003-02-26 13:52:51 +0000 | [diff] [blame] | 628 | pList = 0; |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 629 | } |
drh | e0194f2 | 2003-02-26 13:52:51 +0000 | [diff] [blame] | 630 | |
| 631 | primary_key_exit: |
| 632 | sqliteIdListDelete(pList); |
| 633 | return; |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 634 | } |
| 635 | |
| 636 | /* |
drh | fcb78a4 | 2003-01-18 20:11:05 +0000 | [diff] [blame] | 637 | ** Return the appropriate collating type given a type name. |
| 638 | ** |
| 639 | ** The collation type is text (SQLITE_SO_TEXT) if the type |
| 640 | ** name contains the character stream "text" or "blob" or |
| 641 | ** "clob". Any other type name is collated as numeric |
| 642 | ** (SQLITE_SO_NUM). |
drh | 8e2ca02 | 2002-06-17 17:07:19 +0000 | [diff] [blame] | 643 | */ |
drh | fcb78a4 | 2003-01-18 20:11:05 +0000 | [diff] [blame] | 644 | int sqliteCollateType(const char *zType, int nType){ |
| 645 | int i; |
drh | fcb78a4 | 2003-01-18 20:11:05 +0000 | [diff] [blame] | 646 | for(i=0; i<nType-1; i++){ |
| 647 | switch( zType[i] ){ |
| 648 | case 'b': |
| 649 | case 'B': { |
| 650 | if( i<nType-3 && sqliteStrNICmp(&zType[i],"blob",4)==0 ){ |
| 651 | return SQLITE_SO_TEXT; |
| 652 | } |
| 653 | break; |
| 654 | } |
| 655 | case 'c': |
| 656 | case 'C': { |
| 657 | if( i<nType-3 && (sqliteStrNICmp(&zType[i],"char",4)==0 || |
| 658 | sqliteStrNICmp(&zType[i],"clob",4)==0) |
| 659 | ){ |
| 660 | return SQLITE_SO_TEXT; |
| 661 | } |
| 662 | break; |
| 663 | } |
| 664 | case 'x': |
| 665 | case 'X': { |
| 666 | if( i>=2 && sqliteStrNICmp(&zType[i-2],"text",4)==0 ){ |
| 667 | return SQLITE_SO_TEXT; |
| 668 | } |
| 669 | break; |
| 670 | } |
| 671 | default: { |
| 672 | break; |
| 673 | } |
| 674 | } |
drh | 8e2ca02 | 2002-06-17 17:07:19 +0000 | [diff] [blame] | 675 | } |
drh | fcb78a4 | 2003-01-18 20:11:05 +0000 | [diff] [blame] | 676 | return SQLITE_SO_NUM; |
drh | 8e2ca02 | 2002-06-17 17:07:19 +0000 | [diff] [blame] | 677 | } |
| 678 | |
| 679 | /* |
| 680 | ** This routine is called by the parser while in the middle of |
| 681 | ** parsing a CREATE TABLE statement. A "COLLATE" clause has |
| 682 | ** been seen on a column. This routine sets the Column.sortOrder on |
| 683 | ** the column currently under construction. |
| 684 | */ |
| 685 | void sqliteAddCollateType(Parse *pParse, int collType){ |
| 686 | Table *p; |
| 687 | int i; |
| 688 | if( (p = pParse->pNewTable)==0 ) return; |
| 689 | i = p->nCol-1; |
| 690 | if( i>=0 ) p->aCol[i].sortOrder = collType; |
| 691 | } |
| 692 | |
| 693 | /* |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 694 | ** Come up with a new random value for the schema cookie. Make sure |
| 695 | ** the new value is different from the old. |
| 696 | ** |
| 697 | ** The schema cookie is used to determine when the schema for the |
| 698 | ** database changes. After each schema change, the cookie value |
| 699 | ** changes. When a process first reads the schema it records the |
| 700 | ** cookie. Thereafter, whenever it goes to access the database, |
| 701 | ** it checks the cookie to make sure the schema has not changed |
| 702 | ** since it was last read. |
| 703 | ** |
| 704 | ** This plan is not completely bullet-proof. It is possible for |
| 705 | ** the schema to change multiple times and for the cookie to be |
| 706 | ** set back to prior value. But schema changes are infrequent |
| 707 | ** and the probability of hitting the same cookie value is only |
| 708 | ** 1 chance in 2^32. So we're safe enough. |
| 709 | */ |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 710 | void sqliteChangeCookie(sqlite *db, Vdbe *v){ |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 711 | if( db->next_cookie==db->aDb[0].schema_cookie ){ |
| 712 | db->next_cookie = db->aDb[0].schema_cookie + sqliteRandomByte() + 1; |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 713 | db->flags |= SQLITE_InternChanges; |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 714 | sqliteVdbeAddOp(v, OP_Integer, db->next_cookie, 0); |
| 715 | sqliteVdbeAddOp(v, OP_SetCookie, 0, 0); |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 716 | } |
| 717 | } |
| 718 | |
| 719 | /* |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 720 | ** Measure the number of characters needed to output the given |
| 721 | ** identifier. The number returned includes any quotes used |
| 722 | ** but does not include the null terminator. |
| 723 | */ |
| 724 | static int identLength(const char *z){ |
| 725 | int n; |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 726 | int needQuote = 0; |
| 727 | for(n=0; *z; n++, z++){ |
| 728 | if( *z=='\'' ){ n++; needQuote=1; } |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 729 | } |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 730 | return n + needQuote*2; |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 731 | } |
| 732 | |
| 733 | /* |
| 734 | ** Write an identifier onto the end of the given string. Add |
| 735 | ** quote characters as needed. |
| 736 | */ |
| 737 | static void identPut(char *z, int *pIdx, char *zIdent){ |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 738 | int i, j, needQuote; |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 739 | i = *pIdx; |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 740 | for(j=0; zIdent[j]; j++){ |
| 741 | if( !isalnum(zIdent[j]) && zIdent[j]!='_' ) break; |
| 742 | } |
| 743 | needQuote = zIdent[j]!=0 || isdigit(zIdent[0]) |
| 744 | || sqliteKeywordCode(zIdent, j)!=TK_ID; |
| 745 | if( needQuote ) z[i++] = '\''; |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 746 | for(j=0; zIdent[j]; j++){ |
| 747 | z[i++] = zIdent[j]; |
| 748 | if( zIdent[j]=='\'' ) z[i++] = '\''; |
| 749 | } |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 750 | if( needQuote ) z[i++] = '\''; |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 751 | z[i] = 0; |
| 752 | *pIdx = i; |
| 753 | } |
| 754 | |
| 755 | /* |
| 756 | ** Generate a CREATE TABLE statement appropriate for the given |
| 757 | ** table. Memory to hold the text of the statement is obtained |
| 758 | ** from sqliteMalloc() and must be freed by the calling function. |
| 759 | */ |
| 760 | static char *createTableStmt(Table *p){ |
| 761 | int i, k, n; |
| 762 | char *zStmt; |
| 763 | char *zSep, *zSep2, *zEnd; |
| 764 | n = 0; |
| 765 | for(i=0; i<p->nCol; i++){ |
| 766 | n += identLength(p->aCol[i].zName); |
| 767 | } |
| 768 | n += identLength(p->zName); |
| 769 | if( n<40 ){ |
| 770 | zSep = ""; |
| 771 | zSep2 = ","; |
| 772 | zEnd = ")"; |
| 773 | }else{ |
| 774 | zSep = "\n "; |
| 775 | zSep2 = ",\n "; |
| 776 | zEnd = "\n)"; |
| 777 | } |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 778 | n += 35 + 6*p->nCol; |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 779 | zStmt = sqliteMallocRaw( n ); |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 780 | if( zStmt==0 ) return 0; |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 781 | strcpy(zStmt, p->isTemp ? "CREATE TEMP TABLE " : "CREATE TABLE "); |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 782 | k = strlen(zStmt); |
| 783 | identPut(zStmt, &k, p->zName); |
| 784 | zStmt[k++] = '('; |
| 785 | for(i=0; i<p->nCol; i++){ |
| 786 | strcpy(&zStmt[k], zSep); |
| 787 | k += strlen(&zStmt[k]); |
| 788 | zSep = zSep2; |
| 789 | identPut(zStmt, &k, p->aCol[i].zName); |
| 790 | } |
| 791 | strcpy(&zStmt[k], zEnd); |
| 792 | return zStmt; |
| 793 | } |
| 794 | |
| 795 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 796 | ** This routine is called to report the final ")" that terminates |
| 797 | ** a CREATE TABLE statement. |
| 798 | ** |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 799 | ** The table structure that other action routines have been building |
| 800 | ** is added to the internal hash tables, assuming no errors have |
| 801 | ** occurred. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 802 | ** |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 803 | ** An entry for the table is made in the master table on disk, |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 804 | ** unless this is a temporary table or initFlag==1. When initFlag==1, |
| 805 | ** it means we are reading the sqlite_master table because we just |
| 806 | ** connected to the database or because the sqlite_master table has |
| 807 | ** recently changes, so the entry for this table already exists in |
| 808 | ** the sqlite_master table. We do not want to create it again. |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 809 | ** |
| 810 | ** If the pSelect argument is not NULL, it means that this routine |
| 811 | ** was called to create a table generated from a |
| 812 | ** "CREATE TABLE ... AS SELECT ..." statement. The column names of |
| 813 | ** the new table will match the result set of the SELECT. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 814 | */ |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 815 | void sqliteEndTable(Parse *pParse, Token *pEnd, Select *pSelect){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 816 | Table *p; |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 817 | sqlite *db = pParse->db; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 818 | |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 819 | if( (pEnd==0 && pSelect==0) || pParse->nErr || sqlite_malloc_failed ) return; |
drh | 2803757 | 2000-08-02 13:47:41 +0000 | [diff] [blame] | 820 | p = pParse->pNewTable; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 821 | if( p==0 ) return; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 822 | |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 823 | /* If the table is generated from a SELECT, then construct the |
| 824 | ** list of columns and the text of the table. |
| 825 | */ |
| 826 | if( pSelect ){ |
| 827 | Table *pSelTab = sqliteResultSetOfSelect(pParse, 0, pSelect); |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 828 | if( pSelTab==0 ) return; |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 829 | assert( p->aCol==0 ); |
| 830 | p->nCol = pSelTab->nCol; |
| 831 | p->aCol = pSelTab->aCol; |
| 832 | pSelTab->nCol = 0; |
| 833 | pSelTab->aCol = 0; |
| 834 | sqliteDeleteTable(0, pSelTab); |
| 835 | } |
| 836 | |
drh | d78eeee | 2001-09-13 16:18:53 +0000 | [diff] [blame] | 837 | /* If the initFlag is 1 it means we are reading the SQL off the |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 838 | ** "sqlite_master" or "sqlite_temp_master" table on the disk. |
| 839 | ** So do not write to the disk again. Extract the root page number |
| 840 | ** for the table from the pParse->newTnum field. (The page number |
| 841 | ** should have been put there by the sqliteOpenCb routine.) |
drh | d78eeee | 2001-09-13 16:18:53 +0000 | [diff] [blame] | 842 | */ |
| 843 | if( pParse->initFlag ){ |
| 844 | p->tnum = pParse->newTnum; |
| 845 | } |
| 846 | |
drh | e3c4137 | 2001-09-17 20:25:58 +0000 | [diff] [blame] | 847 | /* If not initializing, then create a record for the new table |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 848 | ** in the SQLITE_MASTER table of the database. The record number |
| 849 | ** for the new table entry should already be on the stack. |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 850 | ** |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 851 | ** If this is a TEMPORARY table, write the entry into the auxiliary |
| 852 | ** file instead of into the main database file. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 853 | */ |
| 854 | if( !pParse->initFlag ){ |
drh | 4ff6dfa | 2002-03-03 23:06:00 +0000 | [diff] [blame] | 855 | int n; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 856 | Vdbe *v; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 857 | |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 858 | v = sqliteGetVdbe(pParse); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 859 | if( v==0 ) return; |
drh | 4ff6dfa | 2002-03-03 23:06:00 +0000 | [diff] [blame] | 860 | if( p->pSelect==0 ){ |
| 861 | /* A regular table */ |
| 862 | sqliteVdbeAddOp(v, OP_CreateTable, 0, p->isTemp); |
| 863 | sqliteVdbeChangeP3(v, -1, (char *)&p->tnum, P3_POINTER); |
| 864 | }else{ |
| 865 | /* A view */ |
| 866 | sqliteVdbeAddOp(v, OP_Integer, 0, 0); |
| 867 | } |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 868 | p->tnum = 0; |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 869 | sqliteVdbeAddOp(v, OP_Pull, 1, 0); |
| 870 | sqliteVdbeAddOp(v, OP_String, 0, 0); |
| 871 | if( p->pSelect==0 ){ |
| 872 | sqliteVdbeChangeP3(v, -1, "table", P3_STATIC); |
| 873 | }else{ |
| 874 | sqliteVdbeChangeP3(v, -1, "view", P3_STATIC); |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 875 | } |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 876 | sqliteVdbeAddOp(v, OP_String, 0, 0); |
| 877 | sqliteVdbeChangeP3(v, -1, p->zName, P3_STATIC); |
| 878 | sqliteVdbeAddOp(v, OP_String, 0, 0); |
| 879 | sqliteVdbeChangeP3(v, -1, p->zName, P3_STATIC); |
| 880 | sqliteVdbeAddOp(v, OP_Dup, 4, 0); |
| 881 | sqliteVdbeAddOp(v, OP_String, 0, 0); |
| 882 | if( pSelect ){ |
| 883 | char *z = createTableStmt(p); |
| 884 | n = z ? strlen(z) : 0; |
| 885 | sqliteVdbeChangeP3(v, -1, z, n); |
| 886 | sqliteFree(z); |
| 887 | }else{ |
| 888 | assert( pEnd!=0 ); |
| 889 | n = Addr(pEnd->z) - Addr(pParse->sFirstToken.z) + 1; |
| 890 | sqliteVdbeChangeP3(v, -1, pParse->sFirstToken.z, n); |
| 891 | } |
| 892 | sqliteVdbeAddOp(v, OP_MakeRecord, 5, 0); |
| 893 | sqliteVdbeAddOp(v, OP_PutIntKey, 0, 0); |
| 894 | if( !p->isTemp ){ |
| 895 | sqliteChangeCookie(db, v); |
| 896 | } |
| 897 | sqliteVdbeAddOp(v, OP_Close, 0, 0); |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 898 | if( pSelect ){ |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 899 | sqliteVdbeAddOp(v, OP_Integer, p->isTemp, 0); |
| 900 | sqliteVdbeAddOp(v, OP_OpenWrite, 1, 0); |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 901 | pParse->nTab = 2; |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 902 | sqliteSelect(pParse, pSelect, SRT_Table, 1, 0, 0, 0); |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 903 | } |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 904 | sqliteEndWriteOperation(pParse); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 905 | } |
drh | 17e9e29 | 2003-02-01 13:53:28 +0000 | [diff] [blame] | 906 | |
| 907 | /* Add the table to the in-memory representation of the database. |
| 908 | */ |
| 909 | assert( pParse->nameClash==0 || pParse->initFlag==1 ); |
| 910 | if( pParse->explain==0 && pParse->nameClash==0 && pParse->nErr==0 ){ |
| 911 | Table *pOld; |
| 912 | FKey *pFKey; |
| 913 | pOld = sqliteHashInsert(&db->tblHash, p->zName, strlen(p->zName)+1, p); |
| 914 | if( pOld ){ |
| 915 | assert( p==pOld ); /* Malloc must have failed inside HashInsert() */ |
| 916 | return; |
| 917 | } |
| 918 | for(pFKey=p->pFKey; pFKey; pFKey=pFKey->pNextFrom){ |
| 919 | int nTo = strlen(pFKey->zTo) + 1; |
| 920 | pFKey->pNextTo = sqliteHashFind(&db->aFKey, pFKey->zTo, nTo); |
| 921 | sqliteHashInsert(&db->aFKey, pFKey->zTo, nTo, pFKey); |
| 922 | } |
| 923 | pParse->pNewTable = 0; |
| 924 | db->nTable++; |
| 925 | db->flags |= SQLITE_InternChanges; |
| 926 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 927 | } |
| 928 | |
| 929 | /* |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 930 | ** The parser calls this routine in order to create a new VIEW |
| 931 | */ |
| 932 | void sqliteCreateView( |
| 933 | Parse *pParse, /* The parsing context */ |
| 934 | Token *pBegin, /* The CREATE token that begins the statement */ |
| 935 | Token *pName, /* The token that holds the name of the view */ |
drh | 6276c1c | 2002-07-08 22:03:32 +0000 | [diff] [blame] | 936 | Select *pSelect, /* A SELECT statement that will become the new view */ |
| 937 | int isTemp /* TRUE for a TEMPORARY view */ |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 938 | ){ |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 939 | Table *p; |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 940 | int n; |
drh | 4ff6dfa | 2002-03-03 23:06:00 +0000 | [diff] [blame] | 941 | const char *z; |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 942 | Token sEnd; |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 943 | |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 944 | sqliteStartTable(pParse, pBegin, pName, isTemp, 1); |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 945 | p = pParse->pNewTable; |
drh | ed6c867 | 2003-01-12 18:02:16 +0000 | [diff] [blame] | 946 | if( p==0 || pParse->nErr ){ |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 947 | sqliteSelectDelete(pSelect); |
| 948 | return; |
| 949 | } |
drh | 174b619 | 2002-12-03 02:22:52 +0000 | [diff] [blame] | 950 | |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 951 | /* Make a copy of the entire SELECT statement that defines the view. |
| 952 | ** This will force all the Expr.token.z values to be dynamically |
| 953 | ** allocated rather than point to the input string - which means that |
| 954 | ** they will persist after the current sqlite_exec() call returns. |
| 955 | */ |
| 956 | p->pSelect = sqliteSelectDup(pSelect); |
| 957 | sqliteSelectDelete(pSelect); |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 958 | if( !pParse->initFlag ){ |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 959 | sqliteViewGetColumnNames(pParse, p); |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 960 | } |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 961 | |
| 962 | /* Locate the end of the CREATE VIEW statement. Make sEnd point to |
| 963 | ** the end. |
| 964 | */ |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 965 | sEnd = pParse->sLastToken; |
| 966 | if( sEnd.z[0]!=0 && sEnd.z[0]!=';' ){ |
| 967 | sEnd.z += sEnd.n; |
| 968 | } |
| 969 | sEnd.n = 0; |
| 970 | n = ((int)sEnd.z) - (int)pBegin->z; |
drh | 4ff6dfa | 2002-03-03 23:06:00 +0000 | [diff] [blame] | 971 | z = pBegin->z; |
| 972 | while( n>0 && (z[n-1]==';' || isspace(z[n-1])) ){ n--; } |
| 973 | sEnd.z = &z[n-1]; |
| 974 | sEnd.n = 1; |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 975 | |
| 976 | /* Use sqliteEndTable() to add the view to the SQLITE_MASTER table */ |
| 977 | sqliteEndTable(pParse, &sEnd, 0); |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 978 | return; |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 979 | } |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 980 | |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 981 | /* |
| 982 | ** The Table structure pTable is really a VIEW. Fill in the names of |
| 983 | ** the columns of the view in the pTable structure. Return the number |
| 984 | ** of errors. If an error is seen leave an error message in pPare->zErrMsg. |
| 985 | */ |
| 986 | int sqliteViewGetColumnNames(Parse *pParse, Table *pTable){ |
| 987 | ExprList *pEList; |
| 988 | Select *pSel; |
| 989 | Table *pSelTab; |
| 990 | int nErr = 0; |
| 991 | |
| 992 | assert( pTable ); |
| 993 | |
| 994 | /* A positive nCol means the columns names for this view are |
| 995 | ** already known. |
| 996 | */ |
| 997 | if( pTable->nCol>0 ) return 0; |
| 998 | |
| 999 | /* A negative nCol is a special marker meaning that we are currently |
| 1000 | ** trying to compute the column names. If we enter this routine with |
| 1001 | ** a negative nCol, it means two or more views form a loop, like this: |
| 1002 | ** |
| 1003 | ** CREATE VIEW one AS SELECT * FROM two; |
| 1004 | ** CREATE VIEW two AS SELECT * FROM one; |
drh | 3b167c7 | 2002-06-28 12:18:47 +0000 | [diff] [blame] | 1005 | ** |
| 1006 | ** Actually, this error is caught previously and so the following test |
| 1007 | ** should always fail. But we will leave it in place just to be safe. |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 1008 | */ |
| 1009 | if( pTable->nCol<0 ){ |
| 1010 | sqliteSetString(&pParse->zErrMsg, "view ", pTable->zName, |
| 1011 | " is circularly defined", 0); |
| 1012 | pParse->nErr++; |
| 1013 | return 1; |
| 1014 | } |
| 1015 | |
| 1016 | /* If we get this far, it means we need to compute the table names. |
| 1017 | */ |
| 1018 | assert( pTable->pSelect ); /* If nCol==0, then pTable must be a VIEW */ |
| 1019 | pSel = pTable->pSelect; |
| 1020 | |
| 1021 | /* Note that the call to sqliteResultSetOfSelect() will expand any |
| 1022 | ** "*" elements in this list. But we will need to restore the list |
| 1023 | ** back to its original configuration afterwards, so we save a copy of |
| 1024 | ** the original in pEList. |
| 1025 | */ |
| 1026 | pEList = pSel->pEList; |
| 1027 | pSel->pEList = sqliteExprListDup(pEList); |
| 1028 | if( pSel->pEList==0 ){ |
| 1029 | pSel->pEList = pEList; |
| 1030 | return 1; /* Malloc failed */ |
| 1031 | } |
| 1032 | pTable->nCol = -1; |
| 1033 | pSelTab = sqliteResultSetOfSelect(pParse, 0, pSel); |
| 1034 | if( pSelTab ){ |
| 1035 | assert( pTable->aCol==0 ); |
| 1036 | pTable->nCol = pSelTab->nCol; |
| 1037 | pTable->aCol = pSelTab->aCol; |
| 1038 | pSelTab->nCol = 0; |
| 1039 | pSelTab->aCol = 0; |
| 1040 | sqliteDeleteTable(0, pSelTab); |
| 1041 | pParse->db->flags |= SQLITE_UnresetViews; |
| 1042 | }else{ |
| 1043 | pTable->nCol = 0; |
| 1044 | nErr++; |
| 1045 | } |
| 1046 | sqliteSelectUnbind(pSel); |
| 1047 | sqliteExprListDelete(pSel->pEList); |
| 1048 | pSel->pEList = pEList; |
| 1049 | return nErr; |
| 1050 | } |
| 1051 | |
| 1052 | /* |
| 1053 | ** Clear the column names from the VIEW pTable. |
| 1054 | ** |
| 1055 | ** This routine is called whenever any other table or view is modified. |
| 1056 | ** The view passed into this routine might depend directly or indirectly |
| 1057 | ** on the modified or deleted table so we need to clear the old column |
| 1058 | ** names so that they will be recomputed. |
| 1059 | */ |
| 1060 | static void sqliteViewResetColumnNames(Table *pTable){ |
| 1061 | int i; |
| 1062 | if( pTable==0 || pTable->pSelect==0 ) return; |
| 1063 | if( pTable->nCol==0 ) return; |
| 1064 | for(i=0; i<pTable->nCol; i++){ |
| 1065 | sqliteFree(pTable->aCol[i].zName); |
| 1066 | sqliteFree(pTable->aCol[i].zDflt); |
| 1067 | sqliteFree(pTable->aCol[i].zType); |
| 1068 | } |
| 1069 | sqliteFree(pTable->aCol); |
| 1070 | pTable->aCol = 0; |
| 1071 | pTable->nCol = 0; |
| 1072 | } |
| 1073 | |
| 1074 | /* |
| 1075 | ** Clear the column names from every VIEW. |
| 1076 | */ |
| 1077 | void sqliteViewResetAll(sqlite *db){ |
| 1078 | HashElem *i; |
| 1079 | if( (db->flags & SQLITE_UnresetViews)==0 ) return; |
| 1080 | for(i=sqliteHashFirst(&db->tblHash); i; i=sqliteHashNext(i)){ |
| 1081 | Table *pTab = sqliteHashData(i); |
| 1082 | if( pTab->pSelect ){ |
| 1083 | sqliteViewResetColumnNames(pTab); |
| 1084 | } |
| 1085 | } |
| 1086 | db->flags &= ~SQLITE_UnresetViews; |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 1087 | } |
| 1088 | |
| 1089 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1090 | ** Given a token, look up a table with that name. If not found, leave |
| 1091 | ** an error for the parser to find and return NULL. |
| 1092 | */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1093 | Table *sqliteTableFromToken(Parse *pParse, Token *pTok){ |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1094 | char *zName; |
| 1095 | Table *pTab; |
| 1096 | zName = sqliteTableNameFromToken(pTok); |
| 1097 | if( zName==0 ) return 0; |
| 1098 | pTab = sqliteFindTable(pParse->db, zName); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1099 | sqliteFree(zName); |
| 1100 | if( pTab==0 ){ |
drh | b24fcbe | 2000-05-29 23:30:50 +0000 | [diff] [blame] | 1101 | sqliteSetNString(&pParse->zErrMsg, "no such table: ", 0, |
| 1102 | pTok->z, pTok->n, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1103 | pParse->nErr++; |
| 1104 | } |
| 1105 | return pTab; |
| 1106 | } |
| 1107 | |
| 1108 | /* |
| 1109 | ** This routine is called to do the work of a DROP TABLE statement. |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1110 | ** pName is the name of the table to be dropped. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1111 | */ |
drh | 4ff6dfa | 2002-03-03 23:06:00 +0000 | [diff] [blame] | 1112 | void sqliteDropTable(Parse *pParse, Token *pName, int isView){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1113 | Table *pTable; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1114 | Vdbe *v; |
| 1115 | int base; |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 1116 | sqlite *db = pParse->db; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1117 | |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1118 | if( pParse->nErr || sqlite_malloc_failed ) return; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1119 | pTable = sqliteTableFromToken(pParse, pName); |
| 1120 | if( pTable==0 ) return; |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 1121 | #ifndef SQLITE_OMIT_AUTHORIZATION |
| 1122 | if( sqliteAuthCheck(pParse, SQLITE_DELETE, SCHEMA_TABLE(pTable->isTemp),0)){ |
drh | ed6c867 | 2003-01-12 18:02:16 +0000 | [diff] [blame] | 1123 | return; |
| 1124 | } |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 1125 | { |
| 1126 | int code; |
| 1127 | if( isView ){ |
| 1128 | if( pTable->isTemp ){ |
| 1129 | code = SQLITE_DROP_TEMP_VIEW; |
| 1130 | }else{ |
| 1131 | code = SQLITE_DROP_VIEW; |
| 1132 | } |
| 1133 | }else{ |
| 1134 | if( pTable->isTemp ){ |
| 1135 | code = SQLITE_DROP_TEMP_TABLE; |
| 1136 | }else{ |
| 1137 | code = SQLITE_DROP_TABLE; |
| 1138 | } |
| 1139 | } |
| 1140 | if( sqliteAuthCheck(pParse, code, pTable->zName, 0) ){ |
| 1141 | return; |
| 1142 | } |
drh | 77ad4e4 | 2003-01-14 02:49:27 +0000 | [diff] [blame] | 1143 | if( sqliteAuthCheck(pParse, SQLITE_DELETE, pTable->zName, 0) ){ |
| 1144 | return; |
| 1145 | } |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 1146 | } |
| 1147 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1148 | if( pTable->readOnly ){ |
drh | 1d37e28 | 2000-05-30 03:12:21 +0000 | [diff] [blame] | 1149 | sqliteSetString(&pParse->zErrMsg, "table ", pTable->zName, |
| 1150 | " may not be dropped", 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1151 | pParse->nErr++; |
| 1152 | return; |
| 1153 | } |
drh | 4ff6dfa | 2002-03-03 23:06:00 +0000 | [diff] [blame] | 1154 | if( isView && pTable->pSelect==0 ){ |
| 1155 | sqliteSetString(&pParse->zErrMsg, "use DROP TABLE to delete table ", |
| 1156 | pTable->zName, 0); |
| 1157 | pParse->nErr++; |
| 1158 | return; |
| 1159 | } |
| 1160 | if( !isView && pTable->pSelect ){ |
| 1161 | sqliteSetString(&pParse->zErrMsg, "use DROP VIEW to delete view ", |
| 1162 | pTable->zName, 0); |
| 1163 | pParse->nErr++; |
| 1164 | return; |
| 1165 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1166 | |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 1167 | /* Generate code to remove the table from the master table |
| 1168 | ** on disk. |
| 1169 | */ |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 1170 | v = sqliteGetVdbe(pParse); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1171 | if( v ){ |
| 1172 | static VdbeOp dropTable[] = { |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1173 | { OP_Rewind, 0, ADDR(8), 0}, |
| 1174 | { OP_String, 0, 0, 0}, /* 1 */ |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 1175 | { OP_MemStore, 1, 1, 0}, |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1176 | { OP_MemLoad, 1, 0, 0}, /* 3 */ |
drh | e3c4137 | 2001-09-17 20:25:58 +0000 | [diff] [blame] | 1177 | { OP_Column, 0, 2, 0}, |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1178 | { OP_Ne, 0, ADDR(7), 0}, |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1179 | { OP_Delete, 0, 0, 0}, |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1180 | { OP_Next, 0, ADDR(3), 0}, /* 7 */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1181 | }; |
| 1182 | Index *pIdx; |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1183 | Trigger *pTrigger; |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 1184 | sqliteBeginWriteOperation(pParse, 0, pTable->isTemp); |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1185 | sqliteOpenMasterTable(v, pTable->isTemp); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 1186 | /* Drop all triggers associated with the table being dropped */ |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1187 | pTrigger = pTable->pTrigger; |
| 1188 | while( pTrigger ){ |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 1189 | Token tt; |
| 1190 | tt.z = pTable->pTrigger->name; |
| 1191 | tt.n = strlen(pTable->pTrigger->name); |
| 1192 | sqliteDropTrigger(pParse, &tt, 1); |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1193 | if( pParse->explain ){ |
| 1194 | pTrigger = pTrigger->pNext; |
| 1195 | }else{ |
| 1196 | pTrigger = pTable->pTrigger; |
| 1197 | } |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 1198 | } |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1199 | base = sqliteVdbeAddOpList(v, ArraySize(dropTable), dropTable); |
| 1200 | sqliteVdbeChangeP3(v, base+1, pTable->zName, 0); |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 1201 | if( !pTable->isTemp ){ |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1202 | sqliteChangeCookie(db, v); |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 1203 | } |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1204 | sqliteVdbeAddOp(v, OP_Close, 0, 0); |
drh | 4ff6dfa | 2002-03-03 23:06:00 +0000 | [diff] [blame] | 1205 | if( !isView ){ |
| 1206 | sqliteVdbeAddOp(v, OP_Destroy, pTable->tnum, pTable->isTemp); |
| 1207 | for(pIdx=pTable->pIndex; pIdx; pIdx=pIdx->pNext){ |
| 1208 | sqliteVdbeAddOp(v, OP_Destroy, pIdx->tnum, pTable->isTemp); |
| 1209 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1210 | } |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 1211 | sqliteEndWriteOperation(pParse); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1212 | } |
| 1213 | |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1214 | /* Delete the in-memory description of the table. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1215 | ** |
| 1216 | ** Exception: if the SQL statement began with the EXPLAIN keyword, |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1217 | ** then no changes should be made. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1218 | */ |
| 1219 | if( !pParse->explain ){ |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1220 | sqliteUnlinkAndDeleteTable(db, pTable); |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 1221 | db->flags |= SQLITE_InternChanges; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1222 | } |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 1223 | sqliteViewResetAll(db); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1224 | } |
| 1225 | |
| 1226 | /* |
drh | 38640e1 | 2002-07-05 21:42:36 +0000 | [diff] [blame] | 1227 | ** This routine constructs a P3 string suitable for an OP_MakeIdxKey |
| 1228 | ** opcode and adds that P3 string to the most recently inserted instruction |
| 1229 | ** in the virtual machine. The P3 string consists of a single character |
| 1230 | ** for each column in the index pIdx of table pTab. If the column uses |
| 1231 | ** a numeric sort order, then the P3 string character corresponding to |
| 1232 | ** that column is 'n'. If the column uses a text sort order, then the |
| 1233 | ** P3 string is 't'. See the OP_MakeIdxKey opcode documentation for |
| 1234 | ** additional information. See also the sqliteAddKeyType() routine. |
| 1235 | */ |
| 1236 | void sqliteAddIdxKeyType(Vdbe *v, Index *pIdx){ |
| 1237 | char *zType; |
| 1238 | Table *pTab; |
| 1239 | int i, n; |
| 1240 | assert( pIdx!=0 && pIdx->pTable!=0 ); |
| 1241 | pTab = pIdx->pTable; |
| 1242 | n = pIdx->nColumn; |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 1243 | zType = sqliteMallocRaw( n+1 ); |
drh | 38640e1 | 2002-07-05 21:42:36 +0000 | [diff] [blame] | 1244 | if( zType==0 ) return; |
| 1245 | for(i=0; i<n; i++){ |
| 1246 | int iCol = pIdx->aiColumn[i]; |
| 1247 | assert( iCol>=0 && iCol<pTab->nCol ); |
| 1248 | if( (pTab->aCol[iCol].sortOrder & SQLITE_SO_TYPEMASK)==SQLITE_SO_TEXT ){ |
| 1249 | zType[i] = 't'; |
| 1250 | }else{ |
| 1251 | zType[i] = 'n'; |
| 1252 | } |
| 1253 | } |
| 1254 | zType[n] = 0; |
| 1255 | sqliteVdbeChangeP3(v, -1, zType, n); |
| 1256 | sqliteFree(zType); |
| 1257 | } |
| 1258 | |
| 1259 | /* |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 1260 | ** This routine is called to create a new foreign key on the table |
| 1261 | ** currently under construction. pFromCol determines which columns |
| 1262 | ** in the current table point to the foreign key. If pFromCol==0 then |
| 1263 | ** connect the key to the last column inserted. pTo is the name of |
| 1264 | ** the table referred to. pToCol is a list of tables in the other |
| 1265 | ** pTo table that the foreign key points to. flags contains all |
| 1266 | ** information about the conflict resolution algorithms specified |
| 1267 | ** in the ON DELETE, ON UPDATE and ON INSERT clauses. |
| 1268 | ** |
| 1269 | ** An FKey structure is created and added to the table currently |
| 1270 | ** under construction in the pParse->pNewTable field. The new FKey |
| 1271 | ** is not linked into db->aFKey at this point - that does not happen |
| 1272 | ** until sqliteEndTable(). |
| 1273 | ** |
| 1274 | ** The foreign key is set for IMMEDIATE processing. A subsequent call |
| 1275 | ** to sqliteDeferForeignKey() might change this to DEFERRED. |
| 1276 | */ |
| 1277 | void sqliteCreateForeignKey( |
| 1278 | Parse *pParse, /* Parsing context */ |
| 1279 | IdList *pFromCol, /* Columns in this table that point to other table */ |
| 1280 | Token *pTo, /* Name of the other table */ |
| 1281 | IdList *pToCol, /* Columns in the other table */ |
| 1282 | int flags /* Conflict resolution algorithms. */ |
| 1283 | ){ |
| 1284 | Table *p = pParse->pNewTable; |
| 1285 | int nByte; |
| 1286 | int i; |
| 1287 | int nCol; |
| 1288 | char *z; |
| 1289 | FKey *pFKey = 0; |
| 1290 | |
| 1291 | assert( pTo!=0 ); |
| 1292 | if( p==0 || pParse->nErr ) goto fk_end; |
| 1293 | if( pFromCol==0 ){ |
| 1294 | int iCol = p->nCol-1; |
| 1295 | if( iCol<0 ) goto fk_end; |
| 1296 | if( pToCol && pToCol->nId!=1 ){ |
| 1297 | sqliteSetNString(&pParse->zErrMsg, "foreign key on ", -1, |
| 1298 | p->aCol[iCol].zName, -1, |
| 1299 | " should reference only one column of table ", -1, |
| 1300 | pTo->z, pTo->n, 0); |
| 1301 | pParse->nErr++; |
| 1302 | goto fk_end; |
| 1303 | } |
| 1304 | nCol = 1; |
| 1305 | }else if( pToCol && pToCol->nId!=pFromCol->nId ){ |
| 1306 | sqliteSetString(&pParse->zErrMsg, |
| 1307 | "number of columns in foreign key does not match the number of " |
| 1308 | "columns in the referenced table", 0); |
| 1309 | pParse->nErr++; |
| 1310 | goto fk_end; |
| 1311 | }else{ |
| 1312 | nCol = pFromCol->nId; |
| 1313 | } |
| 1314 | nByte = sizeof(*pFKey) + nCol*sizeof(pFKey->aCol[0]) + pTo->n + 1; |
| 1315 | if( pToCol ){ |
| 1316 | for(i=0; i<pToCol->nId; i++){ |
| 1317 | nByte += strlen(pToCol->a[i].zName) + 1; |
| 1318 | } |
| 1319 | } |
| 1320 | pFKey = sqliteMalloc( nByte ); |
| 1321 | if( pFKey==0 ) goto fk_end; |
| 1322 | pFKey->pFrom = p; |
| 1323 | pFKey->pNextFrom = p->pFKey; |
drh | df68f6b | 2002-09-21 15:57:57 +0000 | [diff] [blame] | 1324 | z = (char*)&pFKey[1]; |
| 1325 | pFKey->aCol = (struct sColMap*)z; |
| 1326 | z += sizeof(struct sColMap)*nCol; |
| 1327 | pFKey->zTo = z; |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 1328 | memcpy(z, pTo->z, pTo->n); |
| 1329 | z[pTo->n] = 0; |
| 1330 | z += pTo->n+1; |
| 1331 | pFKey->pNextTo = 0; |
| 1332 | pFKey->nCol = nCol; |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 1333 | if( pFromCol==0 ){ |
| 1334 | pFKey->aCol[0].iFrom = p->nCol-1; |
| 1335 | }else{ |
| 1336 | for(i=0; i<nCol; i++){ |
| 1337 | int j; |
| 1338 | for(j=0; j<p->nCol; j++){ |
| 1339 | if( sqliteStrICmp(p->aCol[j].zName, pFromCol->a[i].zName)==0 ){ |
| 1340 | pFKey->aCol[i].iFrom = j; |
| 1341 | break; |
| 1342 | } |
| 1343 | } |
| 1344 | if( j>=p->nCol ){ |
| 1345 | sqliteSetString(&pParse->zErrMsg, "unknown column \"", |
| 1346 | pFromCol->a[i].zName, "\" in foreign key definition", 0); |
| 1347 | pParse->nErr++; |
| 1348 | goto fk_end; |
| 1349 | } |
| 1350 | } |
| 1351 | } |
| 1352 | if( pToCol ){ |
| 1353 | for(i=0; i<nCol; i++){ |
| 1354 | int n = strlen(pToCol->a[i].zName); |
| 1355 | pFKey->aCol[i].zCol = z; |
| 1356 | memcpy(z, pToCol->a[i].zName, n); |
| 1357 | z[n] = 0; |
| 1358 | z += n+1; |
| 1359 | } |
| 1360 | } |
| 1361 | pFKey->isDeferred = 0; |
| 1362 | pFKey->deleteConf = flags & 0xff; |
| 1363 | pFKey->updateConf = (flags >> 8 ) & 0xff; |
| 1364 | pFKey->insertConf = (flags >> 16 ) & 0xff; |
| 1365 | |
| 1366 | /* Link the foreign key to the table as the last step. |
| 1367 | */ |
| 1368 | p->pFKey = pFKey; |
| 1369 | pFKey = 0; |
| 1370 | |
| 1371 | fk_end: |
| 1372 | sqliteFree(pFKey); |
| 1373 | sqliteIdListDelete(pFromCol); |
| 1374 | sqliteIdListDelete(pToCol); |
| 1375 | } |
| 1376 | |
| 1377 | /* |
| 1378 | ** This routine is called when an INITIALLY IMMEDIATE or INITIALLY DEFERRED |
| 1379 | ** clause is seen as part of a foreign key definition. The isDeferred |
| 1380 | ** parameter is 1 for INITIALLY DEFERRED and 0 for INITIALLY IMMEDIATE. |
| 1381 | ** The behavior of the most recently created foreign key is adjusted |
| 1382 | ** accordingly. |
| 1383 | */ |
| 1384 | void sqliteDeferForeignKey(Parse *pParse, int isDeferred){ |
| 1385 | Table *pTab; |
| 1386 | FKey *pFKey; |
| 1387 | if( (pTab = pParse->pNewTable)==0 || (pFKey = pTab->pFKey)==0 ) return; |
| 1388 | pFKey->isDeferred = isDeferred; |
| 1389 | } |
| 1390 | |
| 1391 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1392 | ** Create a new index for an SQL table. pIndex is the name of the index |
| 1393 | ** 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] | 1394 | ** be NULL for a primary key or an index that is created to satisfy a |
| 1395 | ** UNIQUE constraint. If pTable and pIndex are NULL, use pParse->pNewTable |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 1396 | ** as the table to be indexed. pParse->pNewTable is a table that is |
| 1397 | ** currently being constructed by a CREATE TABLE statement. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1398 | ** |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 1399 | ** pList is a list of columns to be indexed. pList will be NULL if this |
| 1400 | ** is a primary key or unique-constraint on the most recent column added |
| 1401 | ** to the table currently under construction. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1402 | */ |
| 1403 | void sqliteCreateIndex( |
| 1404 | Parse *pParse, /* All information about this parse */ |
| 1405 | Token *pName, /* Name of the index. May be NULL */ |
| 1406 | Token *pTable, /* Name of the table to index. Use pParse->pNewTable if 0 */ |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 1407 | IdList *pList, /* A list of columns to be indexed */ |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 1408 | int onError, /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1409 | Token *pStart, /* The CREATE token that begins a CREATE TABLE statement */ |
| 1410 | Token *pEnd /* The ")" that closes the CREATE INDEX statement */ |
| 1411 | ){ |
| 1412 | Table *pTab; /* Table to be indexed */ |
| 1413 | Index *pIndex; /* The index to be created */ |
| 1414 | char *zName = 0; |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 1415 | int i, j; |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 1416 | Token nullId; /* Fake token for an empty ID list */ |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 1417 | sqlite *db = pParse->db; |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 1418 | int hideName = 0; /* Do not put table name in the hash table */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1419 | |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1420 | if( pParse->nErr || sqlite_malloc_failed ) goto exit_create_index; |
| 1421 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1422 | /* |
| 1423 | ** Find the table that is to be indexed. Return early if not found. |
| 1424 | */ |
| 1425 | if( pTable!=0 ){ |
drh | e3c4137 | 2001-09-17 20:25:58 +0000 | [diff] [blame] | 1426 | assert( pName!=0 ); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1427 | pTab = sqliteTableFromToken(pParse, pTable); |
| 1428 | }else{ |
drh | e3c4137 | 2001-09-17 20:25:58 +0000 | [diff] [blame] | 1429 | assert( pName==0 ); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1430 | pTab = pParse->pNewTable; |
| 1431 | } |
| 1432 | if( pTab==0 || pParse->nErr ) goto exit_create_index; |
| 1433 | if( pTab->readOnly ){ |
drh | b24fcbe | 2000-05-29 23:30:50 +0000 | [diff] [blame] | 1434 | sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName, |
| 1435 | " may not have new indices added", 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1436 | pParse->nErr++; |
| 1437 | goto exit_create_index; |
| 1438 | } |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 1439 | if( pTab->pSelect ){ |
| 1440 | sqliteSetString(&pParse->zErrMsg, "views may not be indexed", 0); |
| 1441 | pParse->nErr++; |
| 1442 | goto exit_create_index; |
| 1443 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1444 | |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 1445 | /* If this index is created while re-reading the schema from sqlite_master |
| 1446 | ** but the table associated with this index is a temporary table, it can |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 1447 | ** only mean that the table that this index is really associated with is |
| 1448 | ** one whose name is hidden behind a temporary table with the same name. |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 1449 | ** Since its table has been suppressed, we need to also suppress the |
| 1450 | ** index. |
| 1451 | */ |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1452 | if( pParse->initFlag && !pParse->isTemp && pTab->isTemp ){ |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 1453 | goto exit_create_index; |
| 1454 | } |
| 1455 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1456 | /* |
| 1457 | ** Find the name of the index. Make sure there is not already another |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 1458 | ** index or table with the same name. |
| 1459 | ** |
| 1460 | ** Exception: If we are reading the names of permanent indices from the |
| 1461 | ** sqlite_master table (because some other process changed the schema) and |
| 1462 | ** one of the index names collides with the name of a temporary table or |
| 1463 | ** index, then we will continue to process this index, but we will not |
| 1464 | ** store its name in the hash table. Set the hideName flag to accomplish |
| 1465 | ** this. |
| 1466 | ** |
| 1467 | ** If pName==0 it means that we are |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 1468 | ** dealing with a primary key or UNIQUE constraint. We have to invent our |
| 1469 | ** own name. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1470 | */ |
| 1471 | if( pName ){ |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 1472 | Index *pISameName; /* Another index with the same name */ |
| 1473 | Table *pTSameName; /* A table with same name as the index */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1474 | zName = sqliteTableNameFromToken(pName); |
drh | e3c4137 | 2001-09-17 20:25:58 +0000 | [diff] [blame] | 1475 | if( zName==0 ) goto exit_create_index; |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 1476 | if( (pISameName = sqliteFindIndex(db, zName))!=0 ){ |
| 1477 | if( pISameName->pTable->isTemp && pParse->initFlag ){ |
| 1478 | hideName = 1; |
| 1479 | }else{ |
| 1480 | sqliteSetString(&pParse->zErrMsg, "index ", zName, |
| 1481 | " already exists", 0); |
| 1482 | pParse->nErr++; |
| 1483 | goto exit_create_index; |
| 1484 | } |
drh | e3c4137 | 2001-09-17 20:25:58 +0000 | [diff] [blame] | 1485 | } |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 1486 | if( (pTSameName = sqliteFindTable(db, zName))!=0 ){ |
| 1487 | if( pTSameName->isTemp && pParse->initFlag ){ |
| 1488 | hideName = 1; |
| 1489 | }else{ |
| 1490 | sqliteSetString(&pParse->zErrMsg, "there is already a table named ", |
| 1491 | zName, 0); |
| 1492 | pParse->nErr++; |
| 1493 | goto exit_create_index; |
| 1494 | } |
drh | e3c4137 | 2001-09-17 20:25:58 +0000 | [diff] [blame] | 1495 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1496 | }else{ |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 1497 | char zBuf[30]; |
| 1498 | int n; |
| 1499 | Index *pLoop; |
| 1500 | for(pLoop=pTab->pIndex, n=1; pLoop; pLoop=pLoop->pNext, n++){} |
| 1501 | sprintf(zBuf,"%d)",n); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1502 | zName = 0; |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 1503 | sqliteSetString(&zName, "(", pTab->zName, " autoindex ", zBuf, 0); |
drh | e3c4137 | 2001-09-17 20:25:58 +0000 | [diff] [blame] | 1504 | if( zName==0 ) goto exit_create_index; |
drh | da9e034 | 2002-01-10 14:31:48 +0000 | [diff] [blame] | 1505 | hideName = sqliteFindIndex(db, zName)!=0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1506 | } |
| 1507 | |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 1508 | /* Check for authorization to create an index. |
| 1509 | */ |
| 1510 | #ifndef SQLITE_OMIT_AUTHORIZATION |
| 1511 | if( sqliteAuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(pTab->isTemp), 0) ){ |
| 1512 | goto exit_create_index; |
| 1513 | } |
| 1514 | i = SQLITE_CREATE_INDEX; |
| 1515 | if( pTab->isTemp ) i = SQLITE_CREATE_TEMP_INDEX; |
| 1516 | if( sqliteAuthCheck(pParse, i, zName, pTab->zName) ){ |
| 1517 | goto exit_create_index; |
| 1518 | } |
| 1519 | #endif |
| 1520 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1521 | /* If pList==0, it means this routine was called to make a primary |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 1522 | ** key out of the last column added to the table under construction. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1523 | ** So create a fake list to simulate this. |
| 1524 | */ |
| 1525 | if( pList==0 ){ |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 1526 | nullId.z = pTab->aCol[pTab->nCol-1].zName; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1527 | nullId.n = strlen(nullId.z); |
| 1528 | pList = sqliteIdListAppend(0, &nullId); |
| 1529 | if( pList==0 ) goto exit_create_index; |
| 1530 | } |
| 1531 | |
| 1532 | /* |
| 1533 | ** Allocate the index structure. |
| 1534 | */ |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 1535 | pIndex = sqliteMalloc( sizeof(Index) + strlen(zName) + 1 + |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1536 | sizeof(int)*pList->nId ); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1537 | if( pIndex==0 ) goto exit_create_index; |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 1538 | pIndex->aiColumn = (int*)&pIndex[1]; |
| 1539 | pIndex->zName = (char*)&pIndex->aiColumn[pList->nId]; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1540 | strcpy(pIndex->zName, zName); |
| 1541 | pIndex->pTable = pTab; |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 1542 | pIndex->nColumn = pList->nId; |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 1543 | pIndex->onError = pIndex->isUnique = onError; |
drh | 485b39b | 2002-07-13 03:11:52 +0000 | [diff] [blame] | 1544 | pIndex->autoIndex = pName==0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1545 | |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 1546 | /* Scan the names of the columns of the table to be indexed and |
| 1547 | ** load the column indices into the Index structure. Report an error |
| 1548 | ** if any column is not found. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1549 | */ |
| 1550 | for(i=0; i<pList->nId; i++){ |
| 1551 | for(j=0; j<pTab->nCol; j++){ |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 1552 | if( sqliteStrICmp(pList->a[i].zName, pTab->aCol[j].zName)==0 ) break; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1553 | } |
| 1554 | if( j>=pTab->nCol ){ |
drh | b24fcbe | 2000-05-29 23:30:50 +0000 | [diff] [blame] | 1555 | sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName, |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 1556 | " has no column named ", pList->a[i].zName, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1557 | pParse->nErr++; |
| 1558 | sqliteFree(pIndex); |
| 1559 | goto exit_create_index; |
| 1560 | } |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 1561 | pIndex->aiColumn[i] = j; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1562 | } |
| 1563 | |
| 1564 | /* Link the new Index structure to its table and to the other |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 1565 | ** in-memory database structures. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1566 | */ |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 1567 | if( !pParse->explain && !hideName ){ |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 1568 | Index *p; |
| 1569 | p = sqliteHashInsert(&db->idxHash, pIndex->zName, strlen(zName)+1, pIndex); |
| 1570 | if( p ){ |
| 1571 | assert( p==pIndex ); /* Malloc must have failed */ |
| 1572 | sqliteFree(pIndex); |
| 1573 | goto exit_create_index; |
| 1574 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1575 | db->flags |= SQLITE_InternChanges; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1576 | } |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 1577 | |
| 1578 | /* When adding an index to the list of indices for a table, make |
| 1579 | ** sure all indices labeled OE_Replace come after all those labeled |
| 1580 | ** OE_Ignore. This is necessary for the correct operation of UPDATE |
| 1581 | ** and INSERT. |
| 1582 | */ |
| 1583 | if( onError!=OE_Replace || pTab->pIndex==0 |
| 1584 | || pTab->pIndex->onError==OE_Replace){ |
| 1585 | pIndex->pNext = pTab->pIndex; |
| 1586 | pTab->pIndex = pIndex; |
| 1587 | }else{ |
| 1588 | Index *pOther = pTab->pIndex; |
| 1589 | while( pOther->pNext && pOther->pNext->onError!=OE_Replace ){ |
| 1590 | pOther = pOther->pNext; |
| 1591 | } |
| 1592 | pIndex->pNext = pOther->pNext; |
| 1593 | pOther->pNext = pIndex; |
| 1594 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1595 | |
drh | d78eeee | 2001-09-13 16:18:53 +0000 | [diff] [blame] | 1596 | /* If the initFlag is 1 it means we are reading the SQL off the |
| 1597 | ** "sqlite_master" table on the disk. So do not write to the disk |
| 1598 | ** again. Extract the table number from the pParse->newTnum field. |
| 1599 | */ |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 1600 | if( pParse->initFlag && pTable!=0 ){ |
drh | d78eeee | 2001-09-13 16:18:53 +0000 | [diff] [blame] | 1601 | pIndex->tnum = pParse->newTnum; |
| 1602 | } |
| 1603 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1604 | /* If the initFlag is 0 then create the index on disk. This |
| 1605 | ** involves writing the index into the master table and filling in the |
| 1606 | ** index with the current table contents. |
| 1607 | ** |
| 1608 | ** The initFlag is 0 when the user first enters a CREATE INDEX |
| 1609 | ** command. The initFlag is 1 when a database is opened and |
| 1610 | ** CREATE INDEX statements are read out of the master table. In |
| 1611 | ** the latter case the index already exists on disk, which is why |
| 1612 | ** we don't want to recreate it. |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 1613 | ** |
| 1614 | ** If pTable==0 it means this index is generated as a primary key |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 1615 | ** or UNIQUE constraint of a CREATE TABLE statement. Since the table |
| 1616 | ** has just been created, it contains no data and the index initialization |
| 1617 | ** step can be skipped. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1618 | */ |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 1619 | else if( pParse->initFlag==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1620 | int n; |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 1621 | Vdbe *v; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1622 | int lbl1, lbl2; |
| 1623 | int i; |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 1624 | int addr; |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 1625 | int isTemp = pTab->isTemp; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1626 | |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 1627 | v = sqliteGetVdbe(pParse); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1628 | if( v==0 ) goto exit_create_index; |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 1629 | if( pTable!=0 ){ |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 1630 | sqliteBeginWriteOperation(pParse, 0, isTemp); |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1631 | sqliteOpenMasterTable(v, isTemp); |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 1632 | } |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1633 | sqliteVdbeAddOp(v, OP_NewRecno, 0, 0); |
| 1634 | sqliteVdbeAddOp(v, OP_String, 0, 0); |
| 1635 | sqliteVdbeChangeP3(v, -1, "index", P3_STATIC); |
| 1636 | sqliteVdbeAddOp(v, OP_String, 0, 0); |
| 1637 | sqliteVdbeChangeP3(v, -1, pIndex->zName, P3_STATIC); |
| 1638 | sqliteVdbeAddOp(v, OP_String, 0, 0); |
| 1639 | sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1640 | addr = sqliteVdbeAddOp(v, OP_CreateIndex, 0, isTemp); |
| 1641 | sqliteVdbeChangeP3(v, addr, (char*)&pIndex->tnum, P3_POINTER); |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 1642 | pIndex->tnum = 0; |
| 1643 | if( pTable ){ |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1644 | sqliteVdbeAddOp(v, OP_Dup, 0, 0); |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 1645 | sqliteVdbeAddOp(v, OP_Integer, isTemp, 0); |
| 1646 | sqliteVdbeAddOp(v, OP_OpenWrite, 1, 0); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1647 | } |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1648 | addr = sqliteVdbeAddOp(v, OP_String, 0, 0); |
| 1649 | if( pStart && pEnd ){ |
| 1650 | n = Addr(pEnd->z) - Addr(pStart->z) + 1; |
| 1651 | sqliteVdbeChangeP3(v, addr, pStart->z, n); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1652 | } |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1653 | sqliteVdbeAddOp(v, OP_MakeRecord, 5, 0); |
| 1654 | sqliteVdbeAddOp(v, OP_PutIntKey, 0, 0); |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 1655 | if( pTable ){ |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 1656 | sqliteVdbeAddOp(v, OP_Integer, isTemp, 0); |
| 1657 | sqliteVdbeAddOp(v, OP_OpenRead, 2, pTab->tnum); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1658 | sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC); |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 1659 | lbl2 = sqliteVdbeMakeLabel(v); |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 1660 | sqliteVdbeAddOp(v, OP_Rewind, 2, lbl2); |
| 1661 | lbl1 = sqliteVdbeAddOp(v, OP_Recno, 2, 0); |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 1662 | for(i=0; i<pIndex->nColumn; i++){ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1663 | sqliteVdbeAddOp(v, OP_Column, 2, pIndex->aiColumn[i]); |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 1664 | } |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1665 | sqliteVdbeAddOp(v, OP_MakeIdxKey, pIndex->nColumn, 0); |
drh | 491791a | 2002-07-18 00:34:09 +0000 | [diff] [blame] | 1666 | if( db->file_format>=4 ) sqliteAddIdxKeyType(v, pIndex); |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 1667 | sqliteVdbeAddOp(v, OP_IdxPut, 1, pIndex->onError!=OE_None); |
drh | 483750b | 2003-01-29 18:46:51 +0000 | [diff] [blame] | 1668 | sqliteVdbeChangeP3(v, -1, "indexed columns are not unique", P3_STATIC); |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 1669 | sqliteVdbeAddOp(v, OP_Next, 2, lbl1); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1670 | sqliteVdbeResolveLabel(v, lbl2); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1671 | sqliteVdbeAddOp(v, OP_Close, 2, 0); |
| 1672 | sqliteVdbeAddOp(v, OP_Close, 1, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1673 | } |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 1674 | if( pTable!=0 ){ |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 1675 | if( !isTemp ){ |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1676 | sqliteChangeCookie(db, v); |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 1677 | } |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1678 | sqliteVdbeAddOp(v, OP_Close, 0, 0); |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 1679 | sqliteEndWriteOperation(pParse); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1680 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1681 | } |
| 1682 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1683 | /* Clean up before exiting */ |
| 1684 | exit_create_index: |
| 1685 | sqliteIdListDelete(pList); |
| 1686 | sqliteFree(zName); |
| 1687 | return; |
| 1688 | } |
| 1689 | |
| 1690 | /* |
drh | 74e24cd | 2002-01-09 03:19:59 +0000 | [diff] [blame] | 1691 | ** This routine will drop an existing named index. This routine |
| 1692 | ** implements the DROP INDEX statement. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1693 | */ |
| 1694 | void sqliteDropIndex(Parse *pParse, Token *pName){ |
| 1695 | Index *pIndex; |
| 1696 | char *zName; |
| 1697 | Vdbe *v; |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 1698 | sqlite *db = pParse->db; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1699 | |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1700 | if( pParse->nErr || sqlite_malloc_failed ) return; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1701 | zName = sqliteTableNameFromToken(pName); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1702 | if( zName==0 ) return; |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 1703 | pIndex = sqliteFindIndex(db, zName); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1704 | sqliteFree(zName); |
| 1705 | if( pIndex==0 ){ |
drh | 1d37e28 | 2000-05-30 03:12:21 +0000 | [diff] [blame] | 1706 | sqliteSetNString(&pParse->zErrMsg, "no such index: ", 0, |
| 1707 | pName->z, pName->n, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1708 | pParse->nErr++; |
| 1709 | return; |
| 1710 | } |
drh | 485b39b | 2002-07-13 03:11:52 +0000 | [diff] [blame] | 1711 | if( pIndex->autoIndex ){ |
| 1712 | sqliteSetString(&pParse->zErrMsg, "index associated with UNIQUE " |
| 1713 | "or PRIMARY KEY constraint cannot be dropped", 0); |
| 1714 | pParse->nErr++; |
| 1715 | return; |
| 1716 | } |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 1717 | #ifndef SQLITE_OMIT_AUTHORIZATION |
| 1718 | { |
| 1719 | int code = SQLITE_DROP_INDEX; |
| 1720 | Table *pTab = pIndex->pTable; |
| 1721 | if( sqliteAuthCheck(pParse, SQLITE_DELETE, SCHEMA_TABLE(pTab->isTemp), 0) ){ |
| 1722 | return; |
| 1723 | } |
| 1724 | if( pTab->isTemp ) code = SQLITE_DROP_TEMP_INDEX; |
drh | 77ad4e4 | 2003-01-14 02:49:27 +0000 | [diff] [blame] | 1725 | if( sqliteAuthCheck(pParse, code, pIndex->zName, pTab->zName) ){ |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 1726 | return; |
| 1727 | } |
drh | ed6c867 | 2003-01-12 18:02:16 +0000 | [diff] [blame] | 1728 | } |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 1729 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1730 | |
| 1731 | /* Generate code to remove the index and from the master table */ |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 1732 | v = sqliteGetVdbe(pParse); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1733 | if( v ){ |
| 1734 | static VdbeOp dropIndex[] = { |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1735 | { OP_Rewind, 0, ADDR(9), 0}, |
| 1736 | { OP_String, 0, 0, 0}, /* 1 */ |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 1737 | { OP_MemStore, 1, 1, 0}, |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1738 | { OP_MemLoad, 1, 0, 0}, /* 3 */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1739 | { OP_Column, 0, 1, 0}, |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1740 | { OP_Eq, 0, ADDR(8), 0}, |
| 1741 | { OP_Next, 0, ADDR(3), 0}, |
| 1742 | { OP_Goto, 0, ADDR(9), 0}, |
| 1743 | { OP_Delete, 0, 0, 0}, /* 8 */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1744 | }; |
| 1745 | int base; |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 1746 | Table *pTab = pIndex->pTable; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1747 | |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 1748 | sqliteBeginWriteOperation(pParse, 0, pTab->isTemp); |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1749 | sqliteOpenMasterTable(v, pTab->isTemp); |
| 1750 | base = sqliteVdbeAddOpList(v, ArraySize(dropIndex), dropIndex); |
| 1751 | sqliteVdbeChangeP3(v, base+1, pIndex->zName, 0); |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 1752 | if( !pTab->isTemp ){ |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1753 | sqliteChangeCookie(db, v); |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 1754 | } |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1755 | sqliteVdbeAddOp(v, OP_Close, 0, 0); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1756 | sqliteVdbeAddOp(v, OP_Destroy, pIndex->tnum, pTab->isTemp); |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 1757 | sqliteEndWriteOperation(pParse); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1758 | } |
| 1759 | |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1760 | /* Delete the in-memory description of this index. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1761 | */ |
| 1762 | if( !pParse->explain ){ |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1763 | sqliteUnlinkAndDeleteIndex(db, pIndex); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1764 | db->flags |= SQLITE_InternChanges; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1765 | } |
| 1766 | } |
| 1767 | |
| 1768 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1769 | ** Append a new element to the given IdList. Create a new IdList if |
| 1770 | ** need be. |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1771 | ** |
| 1772 | ** A new IdList is returned, or NULL if malloc() fails. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1773 | */ |
| 1774 | IdList *sqliteIdListAppend(IdList *pList, Token *pToken){ |
| 1775 | if( pList==0 ){ |
| 1776 | pList = sqliteMalloc( sizeof(IdList) ); |
| 1777 | if( pList==0 ) return 0; |
| 1778 | } |
| 1779 | if( (pList->nId & 7)==0 ){ |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 1780 | struct IdList_item *a; |
| 1781 | a = sqliteRealloc(pList->a, (pList->nId+8)*sizeof(pList->a[0]) ); |
| 1782 | if( a==0 ){ |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1783 | sqliteIdListDelete(pList); |
| 1784 | return 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1785 | } |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 1786 | pList->a = a; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1787 | } |
| 1788 | memset(&pList->a[pList->nId], 0, sizeof(pList->a[0])); |
| 1789 | if( pToken ){ |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1790 | char **pz = &pList->a[pList->nId].zName; |
| 1791 | sqliteSetNString(pz, pToken->z, pToken->n, 0); |
| 1792 | if( *pz==0 ){ |
| 1793 | sqliteIdListDelete(pList); |
| 1794 | return 0; |
| 1795 | }else{ |
| 1796 | sqliteDequote(*pz); |
| 1797 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1798 | } |
| 1799 | pList->nId++; |
| 1800 | return pList; |
| 1801 | } |
| 1802 | |
| 1803 | /* |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 1804 | ** Append a new table name to the given SrcList. Create a new SrcList if |
| 1805 | ** need be. A new entry is created in the SrcList even if pToken is NULL. |
| 1806 | ** |
| 1807 | ** A new SrcList is returned, or NULL if malloc() fails. |
drh | 113088e | 2003-03-20 01:16:58 +0000 | [diff] [blame] | 1808 | ** |
| 1809 | ** If pDatabase is not null, it means that the table has an optional |
| 1810 | ** database name prefix. Like this: "database.table". The pDatabase |
| 1811 | ** points to the table name and the pTable points to the database name. |
| 1812 | ** The SrcList.a[].zName field is filled with the table name which might |
| 1813 | ** come from pTable (if pDatabase is NULL) or from pDatabase. |
| 1814 | ** SrcList.a[].zDatabase is filled with the database name from pTable, |
| 1815 | ** or with NULL if no database is specified. |
| 1816 | ** |
| 1817 | ** In other words, if call like this: |
| 1818 | ** |
| 1819 | ** sqliteSrcListAppend(A,B,0); |
| 1820 | ** |
| 1821 | ** Then B is a table name and the database name is unspecified. If called |
| 1822 | ** like this: |
| 1823 | ** |
| 1824 | ** sqliteSrcListAppend(A,B,C); |
| 1825 | ** |
| 1826 | ** Then C is the table name and B is the database name. |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 1827 | */ |
drh | 113088e | 2003-03-20 01:16:58 +0000 | [diff] [blame] | 1828 | SrcList *sqliteSrcListAppend(SrcList *pList, Token *pTable, Token *pDatabase){ |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 1829 | if( pList==0 ){ |
drh | 113088e | 2003-03-20 01:16:58 +0000 | [diff] [blame] | 1830 | pList = sqliteMalloc( sizeof(SrcList) ); |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 1831 | if( pList==0 ) return 0; |
| 1832 | } |
drh | 113088e | 2003-03-20 01:16:58 +0000 | [diff] [blame] | 1833 | if( (pList->nSrc & 7)==1 ){ |
| 1834 | SrcList *pNew; |
| 1835 | pNew = sqliteRealloc(pList, |
| 1836 | sizeof(*pList) + (pList->nSrc+8)*sizeof(pList->a[0]) ); |
| 1837 | if( pNew==0 ){ |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 1838 | sqliteSrcListDelete(pList); |
| 1839 | return 0; |
| 1840 | } |
drh | 113088e | 2003-03-20 01:16:58 +0000 | [diff] [blame] | 1841 | pList = pNew; |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 1842 | } |
| 1843 | memset(&pList->a[pList->nSrc], 0, sizeof(pList->a[0])); |
drh | 113088e | 2003-03-20 01:16:58 +0000 | [diff] [blame] | 1844 | if( pDatabase && pDatabase->z==0 ){ |
| 1845 | pDatabase = 0; |
| 1846 | } |
| 1847 | if( pDatabase && pTable ){ |
| 1848 | Token *pTemp = pDatabase; |
| 1849 | pDatabase = pTable; |
| 1850 | pTable = pTemp; |
| 1851 | } |
| 1852 | if( pTable ){ |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 1853 | char **pz = &pList->a[pList->nSrc].zName; |
drh | 113088e | 2003-03-20 01:16:58 +0000 | [diff] [blame] | 1854 | sqliteSetNString(pz, pTable->z, pTable->n, 0); |
| 1855 | if( *pz==0 ){ |
| 1856 | sqliteSrcListDelete(pList); |
| 1857 | return 0; |
| 1858 | }else{ |
| 1859 | sqliteDequote(*pz); |
| 1860 | } |
| 1861 | } |
| 1862 | if( pDatabase ){ |
| 1863 | char **pz = &pList->a[pList->nSrc].zDatabase; |
| 1864 | sqliteSetNString(pz, pDatabase->z, pDatabase->n, 0); |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 1865 | if( *pz==0 ){ |
| 1866 | sqliteSrcListDelete(pList); |
| 1867 | return 0; |
| 1868 | }else{ |
| 1869 | sqliteDequote(*pz); |
| 1870 | } |
| 1871 | } |
| 1872 | pList->nSrc++; |
| 1873 | return pList; |
| 1874 | } |
| 1875 | |
| 1876 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1877 | ** Add an alias to the last identifier on the given identifier list. |
| 1878 | */ |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 1879 | void sqliteSrcListAddAlias(SrcList *pList, Token *pToken){ |
| 1880 | if( pList && pList->nSrc>0 ){ |
| 1881 | int i = pList->nSrc - 1; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1882 | sqliteSetNString(&pList->a[i].zAlias, pToken->z, pToken->n, 0); |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 1883 | sqliteDequote(pList->a[i].zAlias); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1884 | } |
| 1885 | } |
| 1886 | |
| 1887 | /* |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 1888 | ** Delete an IdList. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1889 | */ |
| 1890 | void sqliteIdListDelete(IdList *pList){ |
| 1891 | int i; |
| 1892 | if( pList==0 ) return; |
| 1893 | for(i=0; i<pList->nId; i++){ |
| 1894 | sqliteFree(pList->a[i].zName); |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 1895 | } |
| 1896 | sqliteFree(pList->a); |
| 1897 | sqliteFree(pList); |
| 1898 | } |
| 1899 | |
| 1900 | /* |
drh | ad2d830 | 2002-05-24 20:31:36 +0000 | [diff] [blame] | 1901 | ** Return the index in pList of the identifier named zId. Return -1 |
| 1902 | ** if not found. |
| 1903 | */ |
| 1904 | int sqliteIdListIndex(IdList *pList, const char *zName){ |
| 1905 | int i; |
| 1906 | if( pList==0 ) return -1; |
| 1907 | for(i=0; i<pList->nId; i++){ |
| 1908 | if( sqliteStrICmp(pList->a[i].zName, zName)==0 ) return i; |
| 1909 | } |
| 1910 | return -1; |
| 1911 | } |
| 1912 | |
| 1913 | /* |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 1914 | ** Delete an entire SrcList including all its substructure. |
| 1915 | */ |
| 1916 | void sqliteSrcListDelete(SrcList *pList){ |
| 1917 | int i; |
| 1918 | if( pList==0 ) return; |
| 1919 | for(i=0; i<pList->nSrc; i++){ |
drh | 113088e | 2003-03-20 01:16:58 +0000 | [diff] [blame] | 1920 | sqliteFree(pList->a[i].zDatabase); |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 1921 | sqliteFree(pList->a[i].zName); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1922 | sqliteFree(pList->a[i].zAlias); |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 1923 | if( pList->a[i].pTab && pList->a[i].pTab->isTransient ){ |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1924 | sqliteDeleteTable(0, pList->a[i].pTab); |
| 1925 | } |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 1926 | sqliteSelectDelete(pList->a[i].pSelect); |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 1927 | sqliteExprDelete(pList->a[i].pOn); |
| 1928 | sqliteIdListDelete(pList->a[i].pUsing); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1929 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1930 | sqliteFree(pList); |
| 1931 | } |
| 1932 | |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 1933 | /* |
| 1934 | ** The COPY command is for compatibility with PostgreSQL and specificially |
| 1935 | ** for the ability to read the output of pg_dump. The format is as |
| 1936 | ** follows: |
| 1937 | ** |
| 1938 | ** COPY table FROM file [USING DELIMITERS string] |
| 1939 | ** |
| 1940 | ** "table" is an existing table name. We will read lines of code from |
| 1941 | ** file to fill this table with data. File might be "stdin". The optional |
| 1942 | ** delimiter string identifies the field separators. The default is a tab. |
| 1943 | */ |
| 1944 | void sqliteCopy( |
| 1945 | Parse *pParse, /* The parser context */ |
| 1946 | Token *pTableName, /* The name of the table into which we will insert */ |
| 1947 | Token *pFilename, /* The file from which to obtain information */ |
drh | b419a92 | 2002-01-30 16:17:23 +0000 | [diff] [blame] | 1948 | Token *pDelimiter, /* Use this as the field delimiter */ |
| 1949 | int onError /* What to do if a constraint fails */ |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 1950 | ){ |
| 1951 | Table *pTab; |
| 1952 | char *zTab; |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 1953 | int i; |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 1954 | Vdbe *v; |
| 1955 | int addr, end; |
| 1956 | Index *pIdx; |
drh | 77ad4e4 | 2003-01-14 02:49:27 +0000 | [diff] [blame] | 1957 | char *zFile = 0; |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 1958 | sqlite *db = pParse->db; |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 1959 | |
drh | 77ad4e4 | 2003-01-14 02:49:27 +0000 | [diff] [blame] | 1960 | |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 1961 | zTab = sqliteTableNameFromToken(pTableName); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1962 | if( sqlite_malloc_failed || zTab==0 ) goto copy_cleanup; |
drh | ef2daf5 | 2002-03-04 02:26:15 +0000 | [diff] [blame] | 1963 | pTab = sqliteTableNameToTable(pParse, zTab); |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 1964 | sqliteFree(zTab); |
drh | ef2daf5 | 2002-03-04 02:26:15 +0000 | [diff] [blame] | 1965 | if( pTab==0 ) goto copy_cleanup; |
drh | 77ad4e4 | 2003-01-14 02:49:27 +0000 | [diff] [blame] | 1966 | zFile = sqliteStrNDup(pFilename->z, pFilename->n); |
| 1967 | sqliteDequote(zFile); |
| 1968 | if( sqliteAuthCheck(pParse, SQLITE_INSERT, pTab->zName, zFile) |
| 1969 | || sqliteAuthCheck(pParse, SQLITE_COPY, pTab->zName, zFile) ){ |
drh | ed6c867 | 2003-01-12 18:02:16 +0000 | [diff] [blame] | 1970 | goto copy_cleanup; |
| 1971 | } |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 1972 | v = sqliteGetVdbe(pParse); |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 1973 | if( v ){ |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 1974 | sqliteBeginWriteOperation(pParse, 1, pTab->isTemp); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1975 | addr = sqliteVdbeAddOp(v, OP_FileOpen, 0, 0); |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 1976 | sqliteVdbeChangeP3(v, addr, pFilename->z, pFilename->n); |
drh | b766599 | 2000-05-30 17:30:35 +0000 | [diff] [blame] | 1977 | sqliteVdbeDequoteP3(v, addr); |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 1978 | sqliteVdbeAddOp(v, OP_Integer, pTab->isTemp, 0); |
| 1979 | sqliteVdbeAddOp(v, OP_OpenWrite, 0, pTab->tnum); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1980 | sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC); |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 1981 | for(i=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){ |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 1982 | sqliteVdbeAddOp(v, OP_Integer, pTab->isTemp, 0); |
| 1983 | sqliteVdbeAddOp(v, OP_OpenWrite, i, pIdx->tnum); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1984 | sqliteVdbeChangeP3(v, -1, pIdx->zName, P3_STATIC); |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 1985 | } |
drh | b419a92 | 2002-01-30 16:17:23 +0000 | [diff] [blame] | 1986 | if( db->flags & SQLITE_CountRows ){ |
| 1987 | sqliteVdbeAddOp(v, OP_Integer, 0, 0); /* Initialize the row count */ |
| 1988 | } |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 1989 | end = sqliteVdbeMakeLabel(v); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1990 | addr = sqliteVdbeAddOp(v, OP_FileRead, pTab->nCol, end); |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 1991 | if( pDelimiter ){ |
| 1992 | sqliteVdbeChangeP3(v, addr, pDelimiter->z, pDelimiter->n); |
| 1993 | sqliteVdbeDequoteP3(v, addr); |
| 1994 | }else{ |
| 1995 | sqliteVdbeChangeP3(v, addr, "\t", 1); |
| 1996 | } |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1997 | if( pTab->iPKey>=0 ){ |
| 1998 | sqliteVdbeAddOp(v, OP_FileColumn, pTab->iPKey, 0); |
| 1999 | sqliteVdbeAddOp(v, OP_MustBeInt, 0, 0); |
| 2000 | }else{ |
| 2001 | sqliteVdbeAddOp(v, OP_NewRecno, 0, 0); |
| 2002 | } |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 2003 | for(i=0; i<pTab->nCol; i++){ |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 2004 | if( i==pTab->iPKey ){ |
| 2005 | /* The integer primary key column is filled with NULL since its |
| 2006 | ** value is always pulled from the record number */ |
| 2007 | sqliteVdbeAddOp(v, OP_String, 0, 0); |
| 2008 | }else{ |
| 2009 | sqliteVdbeAddOp(v, OP_FileColumn, i, 0); |
| 2010 | } |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 2011 | } |
drh | b419a92 | 2002-01-30 16:17:23 +0000 | [diff] [blame] | 2012 | sqliteGenerateConstraintChecks(pParse, pTab, 0, 0, 0, 0, onError, addr); |
| 2013 | sqliteCompleteInsertion(pParse, pTab, 0, 0, 0, 0); |
| 2014 | if( (db->flags & SQLITE_CountRows)!=0 ){ |
| 2015 | sqliteVdbeAddOp(v, OP_AddImm, 1, 0); /* Increment row count */ |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 2016 | } |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 2017 | sqliteVdbeAddOp(v, OP_Goto, 0, addr); |
| 2018 | sqliteVdbeResolveLabel(v, end); |
| 2019 | sqliteVdbeAddOp(v, OP_Noop, 0, 0); |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 2020 | sqliteEndWriteOperation(pParse); |
drh | b419a92 | 2002-01-30 16:17:23 +0000 | [diff] [blame] | 2021 | if( db->flags & SQLITE_CountRows ){ |
drh | b419a92 | 2002-01-30 16:17:23 +0000 | [diff] [blame] | 2022 | sqliteVdbeAddOp(v, OP_ColumnName, 0, 0); |
| 2023 | sqliteVdbeChangeP3(v, -1, "rows inserted", P3_STATIC); |
| 2024 | sqliteVdbeAddOp(v, OP_Callback, 1, 0); |
| 2025 | } |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 2026 | } |
| 2027 | |
| 2028 | copy_cleanup: |
drh | 77ad4e4 | 2003-01-14 02:49:27 +0000 | [diff] [blame] | 2029 | sqliteFree(zFile); |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 2030 | return; |
| 2031 | } |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 2032 | |
| 2033 | /* |
| 2034 | ** The non-standard VACUUM command is used to clean up the database, |
| 2035 | ** collapse free space, etc. It is modelled after the VACUUM command |
| 2036 | ** in PostgreSQL. |
drh | 1dd397f | 2002-02-03 03:34:07 +0000 | [diff] [blame] | 2037 | ** |
drh | 1bffb9c | 2002-02-03 17:37:36 +0000 | [diff] [blame] | 2038 | ** In version 1.0.x of SQLite, the VACUUM command would call |
| 2039 | ** gdbm_reorganize() on all the database tables. But beginning |
| 2040 | ** with 2.0.0, SQLite no longer uses GDBM so this command has |
| 2041 | ** become a no-op. |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 2042 | */ |
| 2043 | void sqliteVacuum(Parse *pParse, Token *pTableName){ |
drh | 1bffb9c | 2002-02-03 17:37:36 +0000 | [diff] [blame] | 2044 | /* Do nothing */ |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 2045 | } |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 2046 | |
| 2047 | /* |
| 2048 | ** Begin a transaction |
| 2049 | */ |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 2050 | void sqliteBeginTransaction(Parse *pParse, int onError){ |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 2051 | sqlite *db; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2052 | |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2053 | if( pParse==0 || (db=pParse->db)==0 || db->aDb[0].pBt==0 ) return; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 2054 | if( pParse->nErr || sqlite_malloc_failed ) return; |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 2055 | if( sqliteAuthCheck(pParse, SQLITE_TRANSACTION, "BEGIN", 0) ) return; |
drh | 6b8b874 | 2002-08-18 20:28:06 +0000 | [diff] [blame] | 2056 | if( db->flags & SQLITE_InTrans ){ |
| 2057 | pParse->nErr++; |
| 2058 | sqliteSetString(&pParse->zErrMsg, "cannot start a transaction " |
| 2059 | "within a transaction", 0); |
| 2060 | return; |
| 2061 | } |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 2062 | sqliteBeginWriteOperation(pParse, 0, 0); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2063 | db->flags |= SQLITE_InTrans; |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 2064 | db->onError = onError; |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 2065 | } |
| 2066 | |
| 2067 | /* |
| 2068 | ** Commit a transaction |
| 2069 | */ |
| 2070 | void sqliteCommitTransaction(Parse *pParse){ |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 2071 | sqlite *db; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2072 | |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2073 | if( pParse==0 || (db=pParse->db)==0 || db->aDb[0].pBt==0 ) return; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 2074 | if( pParse->nErr || sqlite_malloc_failed ) return; |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 2075 | if( sqliteAuthCheck(pParse, SQLITE_TRANSACTION, "COMMIT", 0) ) return; |
drh | 6b8b874 | 2002-08-18 20:28:06 +0000 | [diff] [blame] | 2076 | if( (db->flags & SQLITE_InTrans)==0 ){ |
| 2077 | pParse->nErr++; |
| 2078 | sqliteSetString(&pParse->zErrMsg, |
| 2079 | "cannot commit - no transaction is active", 0); |
| 2080 | return; |
| 2081 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2082 | db->flags &= ~SQLITE_InTrans; |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 2083 | sqliteEndWriteOperation(pParse); |
| 2084 | db->onError = OE_Default; |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 2085 | } |
| 2086 | |
| 2087 | /* |
| 2088 | ** Rollback a transaction |
| 2089 | */ |
| 2090 | void sqliteRollbackTransaction(Parse *pParse){ |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 2091 | sqlite *db; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2092 | Vdbe *v; |
| 2093 | |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2094 | if( pParse==0 || (db=pParse->db)==0 || db->aDb[0].pBt==0 ) return; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 2095 | if( pParse->nErr || sqlite_malloc_failed ) return; |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 2096 | if( sqliteAuthCheck(pParse, SQLITE_TRANSACTION, "ROLLBACK", 0) ) return; |
drh | 6b8b874 | 2002-08-18 20:28:06 +0000 | [diff] [blame] | 2097 | if( (db->flags & SQLITE_InTrans)==0 ){ |
| 2098 | pParse->nErr++; |
| 2099 | sqliteSetString(&pParse->zErrMsg, |
| 2100 | "cannot rollback - no transaction is active", 0); |
| 2101 | return; |
| 2102 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2103 | v = sqliteGetVdbe(pParse); |
| 2104 | if( v ){ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 2105 | sqliteVdbeAddOp(v, OP_Rollback, 0, 0); |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 2106 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2107 | db->flags &= ~SQLITE_InTrans; |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 2108 | db->onError = OE_Default; |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 2109 | } |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 2110 | |
| 2111 | /* |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2112 | ** Generate VDBE code that will verify the schema cookie for all |
| 2113 | ** named database files. |
| 2114 | */ |
| 2115 | void sqliteCodeVerifySchema(Parse *pParse){ |
| 2116 | int i; |
| 2117 | sqlite *db = pParse->db; |
| 2118 | Vdbe *v = sqliteGetVdbe(pParse); |
| 2119 | for(i=0; i<db->nDb; i++){ |
drh | 113088e | 2003-03-20 01:16:58 +0000 | [diff] [blame] | 2120 | if( i==1 || db->aDb[i].pBt==0 ) continue; |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2121 | sqliteVdbeAddOp(v, OP_VerifyCookie, 0, db->aDb[i].schema_cookie); |
| 2122 | } |
| 2123 | pParse->schemaVerified = 1; |
| 2124 | } |
| 2125 | |
| 2126 | /* |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 2127 | ** Generate VDBE code that prepares for doing an operation that |
drh | c977f7f | 2002-05-21 11:38:11 +0000 | [diff] [blame] | 2128 | ** might change the database. |
| 2129 | ** |
| 2130 | ** This routine starts a new transaction if we are not already within |
| 2131 | ** a transaction. If we are already within a transaction, then a checkpoint |
| 2132 | ** is set if the setCheckpoint parameter is true. A checkpoint should |
| 2133 | ** be set for operations that might fail (due to a constraint) part of |
| 2134 | ** the way through and which will need to undo some writes without having to |
| 2135 | ** rollback the whole transaction. For operations where all constraints |
| 2136 | ** can be checked before any changes are made to the database, it is never |
| 2137 | ** necessary to undo a write and the checkpoint should not be set. |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 2138 | ** |
| 2139 | ** The tempOnly flag indicates that only temporary tables will be changed |
| 2140 | ** during this write operation. The primary database table is not |
| 2141 | ** write-locked. Only the temporary database file gets a write lock. |
| 2142 | ** Other processes can continue to read or write the primary database file. |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 2143 | */ |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 2144 | void sqliteBeginWriteOperation(Parse *pParse, int setCheckpoint, int tempOnly){ |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2145 | Vdbe *v; |
| 2146 | v = sqliteGetVdbe(pParse); |
| 2147 | if( v==0 ) return; |
drh | dc37945 | 2002-05-15 12:45:43 +0000 | [diff] [blame] | 2148 | if( pParse->trigStack ) return; /* if this is in a trigger */ |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2149 | if( (pParse->db->flags & SQLITE_InTrans)==0 ){ |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2150 | sqliteVdbeAddOp(v, OP_Transaction, 1, 0); |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 2151 | if( !tempOnly ){ |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2152 | sqliteVdbeAddOp(v, OP_Transaction, 0, 0); |
| 2153 | sqliteCodeVerifySchema(pParse); |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 2154 | } |
drh | c977f7f | 2002-05-21 11:38:11 +0000 | [diff] [blame] | 2155 | }else if( setCheckpoint ){ |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2156 | sqliteVdbeAddOp(v, OP_Checkpoint, 0, 0); |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2157 | sqliteVdbeAddOp(v, OP_Checkpoint, 1, 0); |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2158 | } |
| 2159 | } |
| 2160 | |
| 2161 | /* |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 2162 | ** Generate code that concludes an operation that may have changed |
| 2163 | ** the database. This is a companion function to BeginWriteOperation(). |
| 2164 | ** If a transaction was started, then commit it. If a checkpoint was |
| 2165 | ** started then commit that. |
| 2166 | */ |
| 2167 | void sqliteEndWriteOperation(Parse *pParse){ |
| 2168 | Vdbe *v; |
danielk1977 | f29ce55 | 2002-05-19 23:43:12 +0000 | [diff] [blame] | 2169 | if( pParse->trigStack ) return; /* if this is in a trigger */ |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 2170 | v = sqliteGetVdbe(pParse); |
| 2171 | if( v==0 ) return; |
| 2172 | if( pParse->db->flags & SQLITE_InTrans ){ |
| 2173 | /* Do Nothing */ |
| 2174 | }else{ |
| 2175 | sqliteVdbeAddOp(v, OP_Commit, 0, 0); |
| 2176 | } |
| 2177 | } |
| 2178 | |
| 2179 | |
| 2180 | /* |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 2181 | ** Interpret the given string as a boolean value. |
| 2182 | */ |
| 2183 | static int getBoolean(char *z){ |
| 2184 | static char *azTrue[] = { "yes", "on", "true" }; |
| 2185 | int i; |
| 2186 | if( z[0]==0 ) return 0; |
| 2187 | if( isdigit(z[0]) || (z[0]=='-' && isdigit(z[1])) ){ |
| 2188 | return atoi(z); |
| 2189 | } |
| 2190 | for(i=0; i<sizeof(azTrue)/sizeof(azTrue[0]); i++){ |
| 2191 | if( sqliteStrICmp(z,azTrue[i])==0 ) return 1; |
| 2192 | } |
| 2193 | return 0; |
| 2194 | } |
| 2195 | |
| 2196 | /* |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 2197 | ** Interpret the given string as a safety level. Return 0 for OFF, |
| 2198 | ** 1 for ON or NORMAL and 2 for FULL. |
| 2199 | ** |
| 2200 | ** Note that the values returned are one less that the values that |
| 2201 | ** should be passed into sqliteBtreeSetSafetyLevel(). The is done |
| 2202 | ** to support legacy SQL code. The safety level used to be boolean |
| 2203 | ** and older scripts may have used numbers 0 for OFF and 1 for ON. |
| 2204 | */ |
| 2205 | static int getSafetyLevel(char *z){ |
| 2206 | static const struct { |
| 2207 | const char *zWord; |
| 2208 | int val; |
| 2209 | } aKey[] = { |
| 2210 | { "no", 0 }, |
| 2211 | { "off", 0 }, |
| 2212 | { "false", 0 }, |
| 2213 | { "yes", 1 }, |
| 2214 | { "on", 1 }, |
| 2215 | { "true", 1 }, |
| 2216 | { "full", 2 }, |
| 2217 | }; |
| 2218 | int i; |
| 2219 | if( z[0]==0 ) return 1; |
| 2220 | if( isdigit(z[0]) || (z[0]=='-' && isdigit(z[1])) ){ |
| 2221 | return atoi(z); |
| 2222 | } |
| 2223 | for(i=0; i<sizeof(aKey)/sizeof(aKey[0]); i++){ |
| 2224 | if( sqliteStrICmp(z,aKey[i].zWord)==0 ) return aKey[i].val; |
| 2225 | } |
| 2226 | return 1; |
| 2227 | } |
| 2228 | |
| 2229 | /* |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 2230 | ** Process a pragma statement. |
| 2231 | ** |
| 2232 | ** Pragmas are of this form: |
| 2233 | ** |
| 2234 | ** PRAGMA id = value |
| 2235 | ** |
| 2236 | ** The identifier might also be a string. The value is a string, and |
| 2237 | ** identifier, or a number. If minusFlag is true, then the value is |
| 2238 | ** a number that was preceded by a minus sign. |
| 2239 | */ |
| 2240 | void sqlitePragma(Parse *pParse, Token *pLeft, Token *pRight, int minusFlag){ |
| 2241 | char *zLeft = 0; |
| 2242 | char *zRight = 0; |
| 2243 | sqlite *db = pParse->db; |
drh | dde85d9 | 2003-03-01 19:45:34 +0000 | [diff] [blame] | 2244 | Vdbe *v = sqliteGetVdbe(pParse); |
| 2245 | if( v==0 ) return; |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 2246 | |
| 2247 | zLeft = sqliteStrNDup(pLeft->z, pLeft->n); |
| 2248 | sqliteDequote(zLeft); |
| 2249 | if( minusFlag ){ |
| 2250 | zRight = 0; |
| 2251 | sqliteSetNString(&zRight, "-", 1, pRight->z, pRight->n, 0); |
| 2252 | }else{ |
| 2253 | zRight = sqliteStrNDup(pRight->z, pRight->n); |
| 2254 | sqliteDequote(zRight); |
| 2255 | } |
drh | bf0c78a | 2003-01-14 02:54:08 +0000 | [diff] [blame] | 2256 | if( sqliteAuthCheck(pParse, SQLITE_PRAGMA, zLeft, zRight) ){ |
| 2257 | sqliteFree(zLeft); |
| 2258 | sqliteFree(zRight); |
| 2259 | return; |
| 2260 | } |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 2261 | |
drh | cd61c28 | 2002-03-06 22:01:34 +0000 | [diff] [blame] | 2262 | /* |
| 2263 | ** PRAGMA default_cache_size |
| 2264 | ** PRAGMA default_cache_size=N |
| 2265 | ** |
| 2266 | ** The first form reports the current persistent setting for the |
| 2267 | ** page cache size. The value returned is the maximum number of |
| 2268 | ** pages in the page cache. The second form sets both the current |
| 2269 | ** page cache size value and the persistent page cache size value |
| 2270 | ** stored in the database file. |
| 2271 | ** |
| 2272 | ** The default cache size is stored in meta-value 2 of page 1 of the |
| 2273 | ** database file. The cache size is actually the absolute value of |
| 2274 | ** this memory location. The sign of meta-value 2 determines the |
| 2275 | ** synchronous setting. A negative value means synchronous is off |
| 2276 | ** and a positive value means synchronous is on. |
| 2277 | */ |
| 2278 | if( sqliteStrICmp(zLeft,"default_cache_size")==0 ){ |
drh | 603240c | 2002-03-05 01:11:12 +0000 | [diff] [blame] | 2279 | static VdbeOp getCacheSize[] = { |
| 2280 | { OP_ReadCookie, 0, 2, 0}, |
| 2281 | { OP_AbsValue, 0, 0, 0}, |
drh | cd61c28 | 2002-03-06 22:01:34 +0000 | [diff] [blame] | 2282 | { OP_Dup, 0, 0, 0}, |
| 2283 | { OP_Integer, 0, 0, 0}, |
| 2284 | { OP_Ne, 0, 6, 0}, |
| 2285 | { OP_Integer, MAX_PAGES,0, 0}, |
drh | 603240c | 2002-03-05 01:11:12 +0000 | [diff] [blame] | 2286 | { OP_ColumnName, 0, 0, "cache_size"}, |
| 2287 | { OP_Callback, 1, 0, 0}, |
| 2288 | }; |
drh | 603240c | 2002-03-05 01:11:12 +0000 | [diff] [blame] | 2289 | if( pRight->z==pLeft->z ){ |
| 2290 | sqliteVdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize); |
| 2291 | }else{ |
| 2292 | int addr; |
| 2293 | int size = atoi(zRight); |
| 2294 | if( size<0 ) size = -size; |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 2295 | sqliteBeginWriteOperation(pParse, 0, 0); |
drh | 603240c | 2002-03-05 01:11:12 +0000 | [diff] [blame] | 2296 | sqliteVdbeAddOp(v, OP_Integer, size, 0); |
| 2297 | sqliteVdbeAddOp(v, OP_ReadCookie, 0, 2); |
| 2298 | addr = sqliteVdbeAddOp(v, OP_Integer, 0, 0); |
| 2299 | sqliteVdbeAddOp(v, OP_Ge, 0, addr+3); |
| 2300 | sqliteVdbeAddOp(v, OP_Negative, 0, 0); |
| 2301 | sqliteVdbeAddOp(v, OP_SetCookie, 0, 2); |
| 2302 | sqliteEndWriteOperation(pParse); |
drh | cd61c28 | 2002-03-06 22:01:34 +0000 | [diff] [blame] | 2303 | db->cache_size = db->cache_size<0 ? -size : size; |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2304 | sqliteBtreeSetCacheSize(db->aDb[0].pBt, db->cache_size); |
drh | 603240c | 2002-03-05 01:11:12 +0000 | [diff] [blame] | 2305 | } |
| 2306 | }else |
| 2307 | |
drh | cd61c28 | 2002-03-06 22:01:34 +0000 | [diff] [blame] | 2308 | /* |
| 2309 | ** PRAGMA cache_size |
| 2310 | ** PRAGMA cache_size=N |
| 2311 | ** |
| 2312 | ** The first form reports the current local setting for the |
| 2313 | ** page cache size. The local setting can be different from |
| 2314 | ** the persistent cache size value that is stored in the database |
| 2315 | ** file itself. The value returned is the maximum number of |
| 2316 | ** pages in the page cache. The second form sets the local |
| 2317 | ** page cache size value. It does not change the persistent |
| 2318 | ** cache size stored on the disk so the cache size will revert |
| 2319 | ** to its default value when the database is closed and reopened. |
| 2320 | ** N should be a positive integer. |
| 2321 | */ |
| 2322 | if( sqliteStrICmp(zLeft,"cache_size")==0 ){ |
| 2323 | static VdbeOp getCacheSize[] = { |
drh | cd61c28 | 2002-03-06 22:01:34 +0000 | [diff] [blame] | 2324 | { OP_ColumnName, 0, 0, "cache_size"}, |
| 2325 | { OP_Callback, 1, 0, 0}, |
| 2326 | }; |
drh | cd61c28 | 2002-03-06 22:01:34 +0000 | [diff] [blame] | 2327 | if( pRight->z==pLeft->z ){ |
| 2328 | int size = db->cache_size;; |
| 2329 | if( size<0 ) size = -size; |
| 2330 | sqliteVdbeAddOp(v, OP_Integer, size, 0); |
| 2331 | sqliteVdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize); |
| 2332 | }else{ |
| 2333 | int size = atoi(zRight); |
| 2334 | if( size<0 ) size = -size; |
| 2335 | if( db->cache_size<0 ) size = -size; |
| 2336 | db->cache_size = size; |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2337 | sqliteBtreeSetCacheSize(db->aDb[0].pBt, db->cache_size); |
drh | cd61c28 | 2002-03-06 22:01:34 +0000 | [diff] [blame] | 2338 | } |
| 2339 | }else |
| 2340 | |
| 2341 | /* |
| 2342 | ** PRAGMA default_synchronous |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 2343 | ** PRAGMA default_synchronous=ON|OFF|NORMAL|FULL |
drh | cd61c28 | 2002-03-06 22:01:34 +0000 | [diff] [blame] | 2344 | ** |
| 2345 | ** The first form returns the persistent value of the "synchronous" setting |
| 2346 | ** that is stored in the database. This is the synchronous setting that |
| 2347 | ** is used whenever the database is opened unless overridden by a separate |
| 2348 | ** "synchronous" pragma. The second form changes the persistent and the |
| 2349 | ** local synchronous setting to the value given. |
| 2350 | ** |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 2351 | ** If synchronous is OFF, SQLite does not attempt any fsync() systems calls |
| 2352 | ** to make sure data is committed to disk. Write operations are very fast, |
| 2353 | ** but a power failure can leave the database in an inconsistent state. |
| 2354 | ** If synchronous is ON or NORMAL, SQLite will do an fsync() system call to |
| 2355 | ** make sure data is being written to disk. The risk of corruption due to |
| 2356 | ** a power loss in this mode is negligible but non-zero. If synchronous |
| 2357 | ** is FULL, extra fsync()s occur to reduce the risk of corruption to near |
| 2358 | ** zero, but with a write performance penalty. The default mode is NORMAL. |
drh | cd61c28 | 2002-03-06 22:01:34 +0000 | [diff] [blame] | 2359 | */ |
| 2360 | if( sqliteStrICmp(zLeft,"default_synchronous")==0 ){ |
drh | 603240c | 2002-03-05 01:11:12 +0000 | [diff] [blame] | 2361 | static VdbeOp getSync[] = { |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 2362 | { OP_ColumnName, 0, 0, "synchronous"}, |
| 2363 | { OP_ReadCookie, 0, 3, 0}, |
| 2364 | { OP_Dup, 0, 0, 0}, |
| 2365 | { OP_If, 0, 0, 0}, /* 3 */ |
drh | 603240c | 2002-03-05 01:11:12 +0000 | [diff] [blame] | 2366 | { OP_ReadCookie, 0, 2, 0}, |
| 2367 | { OP_Integer, 0, 0, 0}, |
| 2368 | { OP_Lt, 0, 5, 0}, |
| 2369 | { OP_AddImm, 1, 0, 0}, |
drh | 603240c | 2002-03-05 01:11:12 +0000 | [diff] [blame] | 2370 | { OP_Callback, 1, 0, 0}, |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 2371 | { OP_Halt, 0, 0, 0}, |
| 2372 | { OP_AddImm, -1, 0, 0}, /* 10 */ |
| 2373 | { OP_Callback, 1, 0, 0} |
drh | 603240c | 2002-03-05 01:11:12 +0000 | [diff] [blame] | 2374 | }; |
drh | 603240c | 2002-03-05 01:11:12 +0000 | [diff] [blame] | 2375 | if( pRight->z==pLeft->z ){ |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 2376 | int addr = sqliteVdbeAddOpList(v, ArraySize(getSync), getSync); |
| 2377 | sqliteVdbeChangeP2(v, addr+3, addr+10); |
drh | 603240c | 2002-03-05 01:11:12 +0000 | [diff] [blame] | 2378 | }else{ |
| 2379 | int addr; |
drh | cd61c28 | 2002-03-06 22:01:34 +0000 | [diff] [blame] | 2380 | int size = db->cache_size; |
| 2381 | if( size<0 ) size = -size; |
drh | cabb081 | 2002-09-14 13:47:32 +0000 | [diff] [blame] | 2382 | sqliteBeginWriteOperation(pParse, 0, 0); |
drh | 603240c | 2002-03-05 01:11:12 +0000 | [diff] [blame] | 2383 | sqliteVdbeAddOp(v, OP_ReadCookie, 0, 2); |
drh | cd61c28 | 2002-03-06 22:01:34 +0000 | [diff] [blame] | 2384 | sqliteVdbeAddOp(v, OP_Dup, 0, 0); |
| 2385 | addr = sqliteVdbeAddOp(v, OP_Integer, 0, 0); |
| 2386 | sqliteVdbeAddOp(v, OP_Ne, 0, addr+3); |
| 2387 | sqliteVdbeAddOp(v, OP_AddImm, MAX_PAGES, 0); |
drh | 603240c | 2002-03-05 01:11:12 +0000 | [diff] [blame] | 2388 | sqliteVdbeAddOp(v, OP_AbsValue, 0, 0); |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 2389 | db->safety_level = getSafetyLevel(zRight)+1; |
| 2390 | if( db->safety_level==1 ){ |
drh | 603240c | 2002-03-05 01:11:12 +0000 | [diff] [blame] | 2391 | sqliteVdbeAddOp(v, OP_Negative, 0, 0); |
drh | cd61c28 | 2002-03-06 22:01:34 +0000 | [diff] [blame] | 2392 | size = -size; |
drh | 603240c | 2002-03-05 01:11:12 +0000 | [diff] [blame] | 2393 | } |
| 2394 | sqliteVdbeAddOp(v, OP_SetCookie, 0, 2); |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 2395 | sqliteVdbeAddOp(v, OP_Integer, db->safety_level, 0); |
| 2396 | sqliteVdbeAddOp(v, OP_SetCookie, 0, 3); |
drh | 603240c | 2002-03-05 01:11:12 +0000 | [diff] [blame] | 2397 | sqliteEndWriteOperation(pParse); |
drh | cd61c28 | 2002-03-06 22:01:34 +0000 | [diff] [blame] | 2398 | db->cache_size = size; |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2399 | sqliteBtreeSetCacheSize(db->aDb[0].pBt, db->cache_size); |
| 2400 | sqliteBtreeSetSafetyLevel(db->aDb[0].pBt, db->safety_level); |
drh | cd61c28 | 2002-03-06 22:01:34 +0000 | [diff] [blame] | 2401 | } |
| 2402 | }else |
| 2403 | |
| 2404 | /* |
| 2405 | ** PRAGMA synchronous |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 2406 | ** PRAGMA synchronous=OFF|ON|NORMAL|FULL |
drh | cd61c28 | 2002-03-06 22:01:34 +0000 | [diff] [blame] | 2407 | ** |
| 2408 | ** Return or set the local value of the synchronous flag. Changing |
| 2409 | ** the local value does not make changes to the disk file and the |
| 2410 | ** default value will be restored the next time the database is |
| 2411 | ** opened. |
| 2412 | */ |
| 2413 | if( sqliteStrICmp(zLeft,"synchronous")==0 ){ |
| 2414 | static VdbeOp getSync[] = { |
drh | cd61c28 | 2002-03-06 22:01:34 +0000 | [diff] [blame] | 2415 | { OP_ColumnName, 0, 0, "synchronous"}, |
| 2416 | { OP_Callback, 1, 0, 0}, |
| 2417 | }; |
drh | cd61c28 | 2002-03-06 22:01:34 +0000 | [diff] [blame] | 2418 | if( pRight->z==pLeft->z ){ |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 2419 | sqliteVdbeAddOp(v, OP_Integer, db->safety_level-1, 0); |
drh | cd61c28 | 2002-03-06 22:01:34 +0000 | [diff] [blame] | 2420 | sqliteVdbeAddOpList(v, ArraySize(getSync), getSync); |
| 2421 | }else{ |
| 2422 | int size = db->cache_size; |
| 2423 | if( size<0 ) size = -size; |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 2424 | db->safety_level = getSafetyLevel(zRight)+1; |
| 2425 | if( db->safety_level==1 ) size = -size; |
drh | cd61c28 | 2002-03-06 22:01:34 +0000 | [diff] [blame] | 2426 | db->cache_size = size; |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2427 | sqliteBtreeSetCacheSize(db->aDb[0].pBt, db->cache_size); |
| 2428 | sqliteBtreeSetSafetyLevel(db->aDb[0].pBt, db->safety_level); |
drh | 603240c | 2002-03-05 01:11:12 +0000 | [diff] [blame] | 2429 | } |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 2430 | }else |
| 2431 | |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 2432 | if( sqliteStrICmp(zLeft, "trigger_overhead_test")==0 ){ |
| 2433 | if( getBoolean(zRight) ){ |
| 2434 | always_code_trigger_setup = 1; |
| 2435 | }else{ |
| 2436 | always_code_trigger_setup = 0; |
| 2437 | } |
| 2438 | }else |
| 2439 | |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 2440 | if( sqliteStrICmp(zLeft, "vdbe_trace")==0 ){ |
| 2441 | if( getBoolean(zRight) ){ |
| 2442 | db->flags |= SQLITE_VdbeTrace; |
| 2443 | }else{ |
| 2444 | db->flags &= ~SQLITE_VdbeTrace; |
| 2445 | } |
| 2446 | }else |
| 2447 | |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 2448 | if( sqliteStrICmp(zLeft, "full_column_names")==0 ){ |
| 2449 | if( getBoolean(zRight) ){ |
| 2450 | db->flags |= SQLITE_FullColNames; |
| 2451 | }else{ |
| 2452 | db->flags &= ~SQLITE_FullColNames; |
| 2453 | } |
| 2454 | }else |
| 2455 | |
drh | 5080aaa | 2002-07-11 12:18:16 +0000 | [diff] [blame] | 2456 | if( sqliteStrICmp(zLeft, "show_datatypes")==0 ){ |
| 2457 | if( getBoolean(zRight) ){ |
| 2458 | db->flags |= SQLITE_ReportTypes; |
| 2459 | }else{ |
| 2460 | db->flags &= ~SQLITE_ReportTypes; |
| 2461 | } |
| 2462 | }else |
| 2463 | |
drh | c3a64ba | 2001-11-22 00:01:27 +0000 | [diff] [blame] | 2464 | if( sqliteStrICmp(zLeft, "result_set_details")==0 ){ |
| 2465 | if( getBoolean(zRight) ){ |
| 2466 | db->flags |= SQLITE_ResultDetails; |
| 2467 | }else{ |
| 2468 | db->flags &= ~SQLITE_ResultDetails; |
| 2469 | } |
| 2470 | }else |
| 2471 | |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 2472 | if( sqliteStrICmp(zLeft, "count_changes")==0 ){ |
| 2473 | if( getBoolean(zRight) ){ |
| 2474 | db->flags |= SQLITE_CountRows; |
| 2475 | }else{ |
| 2476 | db->flags &= ~SQLITE_CountRows; |
| 2477 | } |
| 2478 | }else |
| 2479 | |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 2480 | if( sqliteStrICmp(zLeft, "empty_result_callbacks")==0 ){ |
| 2481 | if( getBoolean(zRight) ){ |
| 2482 | db->flags |= SQLITE_NullCallback; |
| 2483 | }else{ |
| 2484 | db->flags &= ~SQLITE_NullCallback; |
| 2485 | } |
| 2486 | }else |
| 2487 | |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 2488 | if( sqliteStrICmp(zLeft, "table_info")==0 ){ |
| 2489 | Table *pTab; |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 2490 | pTab = sqliteFindTable(db, zRight); |
drh | dde85d9 | 2003-03-01 19:45:34 +0000 | [diff] [blame] | 2491 | if( pTab ){ |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 2492 | static VdbeOp tableInfoPreface[] = { |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 2493 | { OP_ColumnName, 0, 0, "cid"}, |
| 2494 | { OP_ColumnName, 1, 0, "name"}, |
| 2495 | { OP_ColumnName, 2, 0, "type"}, |
| 2496 | { OP_ColumnName, 3, 0, "notnull"}, |
| 2497 | { OP_ColumnName, 4, 0, "dflt_value"}, |
| 2498 | }; |
| 2499 | int i; |
| 2500 | sqliteVdbeAddOpList(v, ArraySize(tableInfoPreface), tableInfoPreface); |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 2501 | sqliteViewGetColumnNames(pParse, pTab); |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 2502 | for(i=0; i<pTab->nCol; i++){ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 2503 | sqliteVdbeAddOp(v, OP_Integer, i, 0); |
| 2504 | sqliteVdbeAddOp(v, OP_String, 0, 0); |
| 2505 | sqliteVdbeChangeP3(v, -1, pTab->aCol[i].zName, P3_STATIC); |
| 2506 | sqliteVdbeAddOp(v, OP_String, 0, 0); |
| 2507 | sqliteVdbeChangeP3(v, -1, |
drh | 3c2007a | 2002-10-20 16:00:27 +0000 | [diff] [blame] | 2508 | pTab->aCol[i].zType ? pTab->aCol[i].zType : "numeric", P3_STATIC); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 2509 | sqliteVdbeAddOp(v, OP_Integer, pTab->aCol[i].notNull, 0); |
| 2510 | sqliteVdbeAddOp(v, OP_String, 0, 0); |
| 2511 | sqliteVdbeChangeP3(v, -1, pTab->aCol[i].zDflt, P3_STATIC); |
| 2512 | sqliteVdbeAddOp(v, OP_Callback, 5, 0); |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 2513 | } |
| 2514 | } |
| 2515 | }else |
| 2516 | |
| 2517 | if( sqliteStrICmp(zLeft, "index_info")==0 ){ |
| 2518 | Index *pIdx; |
| 2519 | Table *pTab; |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 2520 | pIdx = sqliteFindIndex(db, zRight); |
drh | dde85d9 | 2003-03-01 19:45:34 +0000 | [diff] [blame] | 2521 | if( pIdx ){ |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 2522 | static VdbeOp tableInfoPreface[] = { |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 2523 | { OP_ColumnName, 0, 0, "seqno"}, |
| 2524 | { OP_ColumnName, 1, 0, "cid"}, |
| 2525 | { OP_ColumnName, 2, 0, "name"}, |
| 2526 | }; |
| 2527 | int i; |
| 2528 | pTab = pIdx->pTable; |
| 2529 | sqliteVdbeAddOpList(v, ArraySize(tableInfoPreface), tableInfoPreface); |
| 2530 | for(i=0; i<pIdx->nColumn; i++){ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 2531 | int cnum = pIdx->aiColumn[i]; |
| 2532 | sqliteVdbeAddOp(v, OP_Integer, i, 0); |
| 2533 | sqliteVdbeAddOp(v, OP_Integer, cnum, 0); |
| 2534 | sqliteVdbeAddOp(v, OP_String, 0, 0); |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 2535 | assert( pTab->nCol>cnum ); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 2536 | sqliteVdbeChangeP3(v, -1, pTab->aCol[cnum].zName, P3_STATIC); |
| 2537 | sqliteVdbeAddOp(v, OP_Callback, 3, 0); |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 2538 | } |
| 2539 | } |
| 2540 | }else |
| 2541 | |
drh | 81a20f2 | 2001-10-12 17:30:04 +0000 | [diff] [blame] | 2542 | if( sqliteStrICmp(zLeft, "index_list")==0 ){ |
| 2543 | Index *pIdx; |
| 2544 | Table *pTab; |
drh | 81a20f2 | 2001-10-12 17:30:04 +0000 | [diff] [blame] | 2545 | pTab = sqliteFindTable(db, zRight); |
| 2546 | if( pTab ){ |
| 2547 | v = sqliteGetVdbe(pParse); |
| 2548 | pIdx = pTab->pIndex; |
| 2549 | } |
drh | dde85d9 | 2003-03-01 19:45:34 +0000 | [diff] [blame] | 2550 | if( pTab && pIdx ){ |
drh | 81a20f2 | 2001-10-12 17:30:04 +0000 | [diff] [blame] | 2551 | int i = 0; |
| 2552 | static VdbeOp indexListPreface[] = { |
drh | 81a20f2 | 2001-10-12 17:30:04 +0000 | [diff] [blame] | 2553 | { OP_ColumnName, 0, 0, "seq"}, |
| 2554 | { OP_ColumnName, 1, 0, "name"}, |
| 2555 | { OP_ColumnName, 2, 0, "unique"}, |
| 2556 | }; |
| 2557 | |
| 2558 | sqliteVdbeAddOpList(v, ArraySize(indexListPreface), indexListPreface); |
| 2559 | while(pIdx){ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 2560 | sqliteVdbeAddOp(v, OP_Integer, i, 0); |
| 2561 | sqliteVdbeAddOp(v, OP_String, 0, 0); |
| 2562 | sqliteVdbeChangeP3(v, -1, pIdx->zName, P3_STATIC); |
drh | 9cfcf5d | 2002-01-29 18:41:24 +0000 | [diff] [blame] | 2563 | sqliteVdbeAddOp(v, OP_Integer, pIdx->onError!=OE_None, 0); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 2564 | sqliteVdbeAddOp(v, OP_Callback, 3, 0); |
drh | 9adf9ac | 2002-05-15 11:44:13 +0000 | [diff] [blame] | 2565 | ++i; |
| 2566 | pIdx = pIdx->pNext; |
drh | 81a20f2 | 2001-10-12 17:30:04 +0000 | [diff] [blame] | 2567 | } |
| 2568 | } |
| 2569 | }else |
| 2570 | |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 2571 | #ifndef NDEBUG |
| 2572 | if( sqliteStrICmp(zLeft, "parser_trace")==0 ){ |
| 2573 | extern void sqliteParserTrace(FILE*, char *); |
| 2574 | if( getBoolean(zRight) ){ |
| 2575 | sqliteParserTrace(stdout, "parser: "); |
| 2576 | }else{ |
| 2577 | sqliteParserTrace(0, 0); |
| 2578 | } |
| 2579 | }else |
| 2580 | #endif |
| 2581 | |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 2582 | if( sqliteStrICmp(zLeft, "integrity_check")==0 ){ |
drh | 1bffb9c | 2002-02-03 17:37:36 +0000 | [diff] [blame] | 2583 | static VdbeOp checkDb[] = { |
| 2584 | { OP_SetInsert, 0, 0, "2"}, |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2585 | { OP_Integer, 0, 0, 0}, |
| 2586 | { OP_OpenRead, 0, 2, 0}, |
| 2587 | { OP_Rewind, 0, 7, 0}, |
| 2588 | { OP_Column, 0, 3, 0}, /* 4 */ |
drh | 1bffb9c | 2002-02-03 17:37:36 +0000 | [diff] [blame] | 2589 | { OP_SetInsert, 0, 0, 0}, |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2590 | { OP_Next, 0, 4, 0}, |
| 2591 | { OP_IntegrityCk, 0, 0, 0}, /* 7 */ |
drh | 4ff6dfa | 2002-03-03 23:06:00 +0000 | [diff] [blame] | 2592 | { OP_ColumnName, 0, 0, "integrity_check"}, |
drh | 1bffb9c | 2002-02-03 17:37:36 +0000 | [diff] [blame] | 2593 | { OP_Callback, 1, 0, 0}, |
drh | 2150432 | 2002-06-25 13:16:02 +0000 | [diff] [blame] | 2594 | { OP_SetInsert, 1, 0, "2"}, |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2595 | { OP_Integer, 1, 0, 0}, |
| 2596 | { OP_OpenRead, 1, 2, 0}, |
| 2597 | { OP_Rewind, 1, 17, 0}, |
| 2598 | { OP_Column, 1, 3, 0}, /* 14 */ |
drh | 2150432 | 2002-06-25 13:16:02 +0000 | [diff] [blame] | 2599 | { OP_SetInsert, 1, 0, 0}, |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 2600 | { OP_Next, 1, 14, 0}, |
| 2601 | { OP_IntegrityCk, 1, 1, 0}, /* 17 */ |
drh | 2150432 | 2002-06-25 13:16:02 +0000 | [diff] [blame] | 2602 | { OP_Callback, 1, 0, 0}, |
drh | 1bffb9c | 2002-02-03 17:37:36 +0000 | [diff] [blame] | 2603 | }; |
drh | 1bffb9c | 2002-02-03 17:37:36 +0000 | [diff] [blame] | 2604 | sqliteVdbeAddOpList(v, ArraySize(checkDb), checkDb); |
| 2605 | }else |
drh | 1bffb9c | 2002-02-03 17:37:36 +0000 | [diff] [blame] | 2606 | |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 2607 | {} |
| 2608 | sqliteFree(zLeft); |
| 2609 | sqliteFree(zRight); |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 2610 | } |