OLD | NEW |
1 /* | 1 /* |
2 ** 2001 September 15 | 2 ** 2001 September 15 |
3 ** | 3 ** |
4 ** The author disclaims copyright to this source code. In place of | 4 ** The author disclaims copyright to this source code. In place of |
5 ** a legal notice, here is a blessing: | 5 ** a legal notice, here is a blessing: |
6 ** | 6 ** |
7 ** May you do good and not evil. | 7 ** May you do good and not evil. |
8 ** May you find forgiveness for yourself and forgive others. | 8 ** May you find forgiveness for yourself and forgive others. |
9 ** May you share freely, never taking more than you give. | 9 ** May you share freely, never taking more than you give. |
10 ** | 10 ** |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
135 ** | 135 ** |
136 ** Note that if an error occurred, it might be the case that | 136 ** Note that if an error occurred, it might be the case that |
137 ** no VDBE code was generated. | 137 ** no VDBE code was generated. |
138 */ | 138 */ |
139 void sqlite3FinishCoding(Parse *pParse){ | 139 void sqlite3FinishCoding(Parse *pParse){ |
140 sqlite3 *db; | 140 sqlite3 *db; |
141 Vdbe *v; | 141 Vdbe *v; |
142 | 142 |
143 assert( pParse->pToplevel==0 ); | 143 assert( pParse->pToplevel==0 ); |
144 db = pParse->db; | 144 db = pParse->db; |
145 if( db->mallocFailed ) return; | |
146 if( pParse->nested ) return; | 145 if( pParse->nested ) return; |
147 if( pParse->nErr ) return; | 146 if( db->mallocFailed || pParse->nErr ){ |
| 147 if( pParse->rc==SQLITE_OK ) pParse->rc = SQLITE_ERROR; |
| 148 return; |
| 149 } |
148 | 150 |
149 /* Begin by generating some termination code at the end of the | 151 /* Begin by generating some termination code at the end of the |
150 ** vdbe program | 152 ** vdbe program |
151 */ | 153 */ |
152 v = sqlite3GetVdbe(pParse); | 154 v = sqlite3GetVdbe(pParse); |
153 assert( !pParse->isMultiWrite | 155 assert( !pParse->isMultiWrite |
154 || sqlite3VdbeAssertMayAbort(v, pParse->mayAbort)); | 156 || sqlite3VdbeAssertMayAbort(v, pParse->mayAbort)); |
155 if( v ){ | 157 if( v ){ |
156 while( sqlite3VdbeDeletePriorOpcode(v, OP_Close) ){} | 158 while( sqlite3VdbeDeletePriorOpcode(v, OP_Close) ){} |
157 sqlite3VdbeAddOp0(v, OP_Halt); | 159 sqlite3VdbeAddOp0(v, OP_Halt); |
(...skipping 25 matching lines...) Expand all Loading... |
183 if( DbMaskTest(pParse->cookieMask, iDb)==0 ) continue; | 185 if( DbMaskTest(pParse->cookieMask, iDb)==0 ) continue; |
184 sqlite3VdbeUsesBtree(v, iDb); | 186 sqlite3VdbeUsesBtree(v, iDb); |
185 sqlite3VdbeAddOp4Int(v, | 187 sqlite3VdbeAddOp4Int(v, |
186 OP_Transaction, /* Opcode */ | 188 OP_Transaction, /* Opcode */ |
187 iDb, /* P1 */ | 189 iDb, /* P1 */ |
188 DbMaskTest(pParse->writeMask,iDb), /* P2 */ | 190 DbMaskTest(pParse->writeMask,iDb), /* P2 */ |
189 pParse->cookieValue[iDb], /* P3 */ | 191 pParse->cookieValue[iDb], /* P3 */ |
190 db->aDb[iDb].pSchema->iGeneration /* P4 */ | 192 db->aDb[iDb].pSchema->iGeneration /* P4 */ |
191 ); | 193 ); |
192 if( db->init.busy==0 ) sqlite3VdbeChangeP5(v, 1); | 194 if( db->init.busy==0 ) sqlite3VdbeChangeP5(v, 1); |
| 195 VdbeComment((v, |
| 196 "usesStmtJournal=%d", pParse->mayAbort && pParse->isMultiWrite)); |
193 } | 197 } |
194 #ifndef SQLITE_OMIT_VIRTUALTABLE | 198 #ifndef SQLITE_OMIT_VIRTUALTABLE |
195 for(i=0; i<pParse->nVtabLock; i++){ | 199 for(i=0; i<pParse->nVtabLock; i++){ |
196 char *vtab = (char *)sqlite3GetVTable(db, pParse->apVtabLock[i]); | 200 char *vtab = (char *)sqlite3GetVTable(db, pParse->apVtabLock[i]); |
197 sqlite3VdbeAddOp4(v, OP_VBegin, 0, 0, 0, vtab, P4_VTAB); | 201 sqlite3VdbeAddOp4(v, OP_VBegin, 0, 0, 0, vtab, P4_VTAB); |
198 } | 202 } |
199 pParse->nVtabLock = 0; | 203 pParse->nVtabLock = 0; |
200 #endif | 204 #endif |
201 | 205 |
202 /* Once all the cookies have been verified and transactions opened, | 206 /* Once all the cookies have been verified and transactions opened, |
203 ** obtain the required table-locks. This is a no-op unless the | 207 ** obtain the required table-locks. This is a no-op unless the |
204 ** shared-cache feature is enabled. | 208 ** shared-cache feature is enabled. |
205 */ | 209 */ |
206 codeTableLocks(pParse); | 210 codeTableLocks(pParse); |
207 | 211 |
208 /* Initialize any AUTOINCREMENT data structures required. | 212 /* Initialize any AUTOINCREMENT data structures required. |
209 */ | 213 */ |
210 sqlite3AutoincrementBegin(pParse); | 214 sqlite3AutoincrementBegin(pParse); |
211 | 215 |
212 /* Code constant expressions that where factored out of inner loops */ | 216 /* Code constant expressions that where factored out of inner loops */ |
213 if( pParse->pConstExpr ){ | 217 if( pParse->pConstExpr ){ |
214 ExprList *pEL = pParse->pConstExpr; | 218 ExprList *pEL = pParse->pConstExpr; |
215 pParse->okConstFactor = 0; | 219 pParse->okConstFactor = 0; |
216 for(i=0; i<pEL->nExpr; i++){ | 220 for(i=0; i<pEL->nExpr; i++){ |
217 sqlite3ExprCode(pParse, pEL->a[i].pExpr, pEL->a[i].u.iConstExprReg); | 221 sqlite3ExprCode(pParse, pEL->a[i].pExpr, pEL->a[i].u.iConstExprReg); |
218 } | 222 } |
219 } | 223 } |
220 | 224 |
221 /* Finally, jump back to the beginning of the executable code. */ | 225 /* Finally, jump back to the beginning of the executable code. */ |
222 sqlite3VdbeAddOp2(v, OP_Goto, 0, 1); | 226 sqlite3VdbeGoto(v, 1); |
223 } | 227 } |
224 } | 228 } |
225 | 229 |
226 | 230 |
227 /* Get the VDBE program ready for execution | 231 /* Get the VDBE program ready for execution |
228 */ | 232 */ |
229 if( v && ALWAYS(pParse->nErr==0) && !db->mallocFailed ){ | 233 if( v && pParse->nErr==0 && !db->mallocFailed ){ |
230 assert( pParse->iCacheLevel==0 ); /* Disables and re-enables match */ | 234 assert( pParse->iCacheLevel==0 ); /* Disables and re-enables match */ |
231 /* A minimum of one cursor is required if autoincrement is used | 235 /* A minimum of one cursor is required if autoincrement is used |
232 * See ticket [a696379c1f08866] */ | 236 * See ticket [a696379c1f08866] */ |
233 if( pParse->pAinc!=0 && pParse->nTab==0 ) pParse->nTab = 1; | 237 if( pParse->pAinc!=0 && pParse->nTab==0 ) pParse->nTab = 1; |
234 sqlite3VdbeMakeReady(v, pParse); | 238 sqlite3VdbeMakeReady(v, pParse); |
235 pParse->rc = SQLITE_DONE; | 239 pParse->rc = SQLITE_DONE; |
236 pParse->colNamesSet = 0; | 240 pParse->colNamesSet = 0; |
237 }else{ | 241 }else{ |
238 pParse->rc = SQLITE_ERROR; | 242 pParse->rc = SQLITE_ERROR; |
239 } | 243 } |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
300 ** If zDatabase is 0, all databases are searched for the table and the | 304 ** If zDatabase is 0, all databases are searched for the table and the |
301 ** first matching table is returned. (No checking for duplicate table | 305 ** first matching table is returned. (No checking for duplicate table |
302 ** names is done.) The search order is TEMP first, then MAIN, then any | 306 ** names is done.) The search order is TEMP first, then MAIN, then any |
303 ** auxiliary databases added using the ATTACH command. | 307 ** auxiliary databases added using the ATTACH command. |
304 ** | 308 ** |
305 ** See also sqlite3LocateTable(). | 309 ** See also sqlite3LocateTable(). |
306 */ | 310 */ |
307 Table *sqlite3FindTable(sqlite3 *db, const char *zName, const char *zDatabase){ | 311 Table *sqlite3FindTable(sqlite3 *db, const char *zName, const char *zDatabase){ |
308 Table *p = 0; | 312 Table *p = 0; |
309 int i; | 313 int i; |
310 assert( zName!=0 ); | 314 |
311 /* All mutexes are required for schema access. Make sure we hold them. */ | 315 /* All mutexes are required for schema access. Make sure we hold them. */ |
312 assert( zDatabase!=0 || sqlite3BtreeHoldsAllMutexes(db) ); | 316 assert( zDatabase!=0 || sqlite3BtreeHoldsAllMutexes(db) ); |
313 #if SQLITE_USER_AUTHENTICATION | 317 #if SQLITE_USER_AUTHENTICATION |
314 /* Only the admin user is allowed to know that the sqlite_user table | 318 /* Only the admin user is allowed to know that the sqlite_user table |
315 ** exists */ | 319 ** exists */ |
316 if( db->auth.authLevel<UAUTH_Admin && sqlite3UserAuthTable(zName)!=0 ){ | 320 if( db->auth.authLevel<UAUTH_Admin && sqlite3UserAuthTable(zName)!=0 ){ |
317 return 0; | 321 return 0; |
318 } | 322 } |
319 #endif | 323 #endif |
320 for(i=OMIT_TEMPDB; i<db->nDb; i++){ | 324 for(i=OMIT_TEMPDB; i<db->nDb; i++){ |
(...skipping 26 matching lines...) Expand all Loading... |
347 | 351 |
348 /* Read the database schema. If an error occurs, leave an error message | 352 /* Read the database schema. If an error occurs, leave an error message |
349 ** and code in pParse and return NULL. */ | 353 ** and code in pParse and return NULL. */ |
350 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){ | 354 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){ |
351 return 0; | 355 return 0; |
352 } | 356 } |
353 | 357 |
354 p = sqlite3FindTable(pParse->db, zName, zDbase); | 358 p = sqlite3FindTable(pParse->db, zName, zDbase); |
355 if( p==0 ){ | 359 if( p==0 ){ |
356 const char *zMsg = isView ? "no such view" : "no such table"; | 360 const char *zMsg = isView ? "no such view" : "no such table"; |
| 361 #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 362 if( sqlite3FindDbName(pParse->db, zDbase)<1 ){ |
| 363 /* If zName is the not the name of a table in the schema created using |
| 364 ** CREATE, then check to see if it is the name of an virtual table that |
| 365 ** can be an eponymous virtual table. */ |
| 366 Module *pMod = (Module*)sqlite3HashFind(&pParse->db->aModule, zName); |
| 367 if( pMod && sqlite3VtabEponymousTableInit(pParse, pMod) ){ |
| 368 return pMod->pEpoTab; |
| 369 } |
| 370 } |
| 371 #endif |
357 if( zDbase ){ | 372 if( zDbase ){ |
358 sqlite3ErrorMsg(pParse, "%s: %s.%s", zMsg, zDbase, zName); | 373 sqlite3ErrorMsg(pParse, "%s: %s.%s", zMsg, zDbase, zName); |
359 }else{ | 374 }else{ |
360 sqlite3ErrorMsg(pParse, "%s: %s", zMsg, zName); | 375 sqlite3ErrorMsg(pParse, "%s: %s", zMsg, zName); |
361 } | 376 } |
362 pParse->checkSchema = 1; | 377 pParse->checkSchema = 1; |
363 } | 378 } |
364 #if SQLITE_USER_AUTHENICATION | 379 |
365 else if( pParse->db->auth.authLevel<UAUTH_User ){ | |
366 sqlite3ErrorMsg(pParse, "user not authenticated"); | |
367 p = 0; | |
368 } | |
369 #endif | |
370 return p; | 380 return p; |
371 } | 381 } |
372 | 382 |
373 /* | 383 /* |
374 ** Locate the table identified by *p. | 384 ** Locate the table identified by *p. |
375 ** | 385 ** |
376 ** This is a wrapper around sqlite3LocateTable(). The difference between | 386 ** This is a wrapper around sqlite3LocateTable(). The difference between |
377 ** sqlite3LocateTable() and this function is that this function restricts | 387 ** sqlite3LocateTable() and this function is that this function restricts |
378 ** the search to schema (p->pSchema) if it is not NULL. p->pSchema may be | 388 ** the search to schema (p->pSchema) if it is not NULL. p->pSchema may be |
379 ** non-NULL if it is part of a view or trigger program definition. See | 389 ** non-NULL if it is part of a view or trigger program definition. See |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
424 return p; | 434 return p; |
425 } | 435 } |
426 | 436 |
427 /* | 437 /* |
428 ** Reclaim the memory used by an index | 438 ** Reclaim the memory used by an index |
429 */ | 439 */ |
430 static void freeIndex(sqlite3 *db, Index *p){ | 440 static void freeIndex(sqlite3 *db, Index *p){ |
431 #ifndef SQLITE_OMIT_ANALYZE | 441 #ifndef SQLITE_OMIT_ANALYZE |
432 sqlite3DeleteIndexSamples(db, p); | 442 sqlite3DeleteIndexSamples(db, p); |
433 #endif | 443 #endif |
434 if( db==0 || db->pnBytesFreed==0 ) sqlite3KeyInfoUnref(p->pKeyInfo); | |
435 sqlite3ExprDelete(db, p->pPartIdxWhere); | 444 sqlite3ExprDelete(db, p->pPartIdxWhere); |
| 445 sqlite3ExprListDelete(db, p->aColExpr); |
436 sqlite3DbFree(db, p->zColAff); | 446 sqlite3DbFree(db, p->zColAff); |
437 if( p->isResized ) sqlite3DbFree(db, p->azColl); | 447 if( p->isResized ) sqlite3DbFree(db, (void *)p->azColl); |
438 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 | 448 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
439 sqlite3_free(p->aiRowEst); | 449 sqlite3_free(p->aiRowEst); |
440 #endif | 450 #endif |
441 sqlite3DbFree(db, p); | 451 sqlite3DbFree(db, p); |
442 } | 452 } |
443 | 453 |
444 /* | 454 /* |
445 ** For the index called zIdxName which is found in the database iDb, | 455 ** For the index called zIdxName which is found in the database iDb, |
446 ** unlike that index from its Table then remove the index from | 456 ** unlike that index from its Table then remove the index from |
447 ** the index hash table and free all memory structures associated | 457 ** the index hash table and free all memory structures associated |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
552 ** This routine is called when a commit occurs. | 562 ** This routine is called when a commit occurs. |
553 */ | 563 */ |
554 void sqlite3CommitInternalChanges(sqlite3 *db){ | 564 void sqlite3CommitInternalChanges(sqlite3 *db){ |
555 db->flags &= ~SQLITE_InternChanges; | 565 db->flags &= ~SQLITE_InternChanges; |
556 } | 566 } |
557 | 567 |
558 /* | 568 /* |
559 ** Delete memory allocated for the column names of a table or view (the | 569 ** Delete memory allocated for the column names of a table or view (the |
560 ** Table.aCol[] array). | 570 ** Table.aCol[] array). |
561 */ | 571 */ |
562 static void sqliteDeleteColumnNames(sqlite3 *db, Table *pTable){ | 572 void sqlite3DeleteColumnNames(sqlite3 *db, Table *pTable){ |
563 int i; | 573 int i; |
564 Column *pCol; | 574 Column *pCol; |
565 assert( pTable!=0 ); | 575 assert( pTable!=0 ); |
566 if( (pCol = pTable->aCol)!=0 ){ | 576 if( (pCol = pTable->aCol)!=0 ){ |
567 for(i=0; i<pTable->nCol; i++, pCol++){ | 577 for(i=0; i<pTable->nCol; i++, pCol++){ |
568 sqlite3DbFree(db, pCol->zName); | 578 sqlite3DbFree(db, pCol->zName); |
569 sqlite3ExprDelete(db, pCol->pDflt); | 579 sqlite3ExprDelete(db, pCol->pDflt); |
570 sqlite3DbFree(db, pCol->zDflt); | 580 sqlite3DbFree(db, pCol->zDflt); |
571 sqlite3DbFree(db, pCol->zType); | 581 sqlite3DbFree(db, pCol->zType); |
572 sqlite3DbFree(db, pCol->zColl); | 582 sqlite3DbFree(db, pCol->zColl); |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
619 assert( pOld==pIndex || pOld==0 ); | 629 assert( pOld==pIndex || pOld==0 ); |
620 } | 630 } |
621 freeIndex(db, pIndex); | 631 freeIndex(db, pIndex); |
622 } | 632 } |
623 | 633 |
624 /* Delete any foreign keys attached to this table. */ | 634 /* Delete any foreign keys attached to this table. */ |
625 sqlite3FkDelete(db, pTable); | 635 sqlite3FkDelete(db, pTable); |
626 | 636 |
627 /* Delete the Table structure itself. | 637 /* Delete the Table structure itself. |
628 */ | 638 */ |
629 sqliteDeleteColumnNames(db, pTable); | 639 sqlite3DeleteColumnNames(db, pTable); |
630 sqlite3DbFree(db, pTable->zName); | 640 sqlite3DbFree(db, pTable->zName); |
631 sqlite3DbFree(db, pTable->zColAff); | 641 sqlite3DbFree(db, pTable->zColAff); |
632 sqlite3SelectDelete(db, pTable->pSelect); | 642 sqlite3SelectDelete(db, pTable->pSelect); |
633 #ifndef SQLITE_OMIT_CHECK | |
634 sqlite3ExprListDelete(db, pTable->pCheck); | 643 sqlite3ExprListDelete(db, pTable->pCheck); |
635 #endif | |
636 #ifndef SQLITE_OMIT_VIRTUALTABLE | 644 #ifndef SQLITE_OMIT_VIRTUALTABLE |
637 sqlite3VtabClear(db, pTable); | 645 sqlite3VtabClear(db, pTable); |
638 #endif | 646 #endif |
639 sqlite3DbFree(db, pTable); | 647 sqlite3DbFree(db, pTable); |
640 | 648 |
641 /* Verify that no lookaside memory was used by schema tables */ | 649 /* Verify that no lookaside memory was used by schema tables */ |
642 assert( nLookaside==0 || nLookaside==db->lookaside.nOut ); | 650 assert( nLookaside==0 || nLookaside==db->lookaside.nOut ); |
643 } | 651 } |
644 | 652 |
645 /* | 653 /* |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
755 Token *pName1, /* The "xxx" in the name "xxx.yyy" or "xxx" */ | 763 Token *pName1, /* The "xxx" in the name "xxx.yyy" or "xxx" */ |
756 Token *pName2, /* The "yyy" in the name "xxx.yyy" */ | 764 Token *pName2, /* The "yyy" in the name "xxx.yyy" */ |
757 Token **pUnqual /* Write the unqualified object name here */ | 765 Token **pUnqual /* Write the unqualified object name here */ |
758 ){ | 766 ){ |
759 int iDb; /* Database holding the object */ | 767 int iDb; /* Database holding the object */ |
760 sqlite3 *db = pParse->db; | 768 sqlite3 *db = pParse->db; |
761 | 769 |
762 if( ALWAYS(pName2!=0) && pName2->n>0 ){ | 770 if( ALWAYS(pName2!=0) && pName2->n>0 ){ |
763 if( db->init.busy ) { | 771 if( db->init.busy ) { |
764 sqlite3ErrorMsg(pParse, "corrupt database"); | 772 sqlite3ErrorMsg(pParse, "corrupt database"); |
765 pParse->nErr++; | |
766 return -1; | 773 return -1; |
767 } | 774 } |
768 *pUnqual = pName2; | 775 *pUnqual = pName2; |
769 iDb = sqlite3FindDb(db, pName1); | 776 iDb = sqlite3FindDb(db, pName1); |
770 if( iDb<0 ){ | 777 if( iDb<0 ){ |
771 sqlite3ErrorMsg(pParse, "unknown database %T", pName1); | 778 sqlite3ErrorMsg(pParse, "unknown database %T", pName1); |
772 pParse->nErr++; | |
773 return -1; | 779 return -1; |
774 } | 780 } |
775 }else{ | 781 }else{ |
776 assert( db->init.iDb==0 || db->init.busy ); | 782 assert( db->init.iDb==0 || db->init.busy ); |
777 iDb = db->init.iDb; | 783 iDb = db->init.iDb; |
778 *pUnqual = pName1; | 784 *pUnqual = pName1; |
779 } | 785 } |
780 return iDb; | 786 return iDb; |
781 } | 787 } |
782 | 788 |
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
921 if( !IN_DECLARE_VTAB ){ | 927 if( !IN_DECLARE_VTAB ){ |
922 char *zDb = db->aDb[iDb].zName; | 928 char *zDb = db->aDb[iDb].zName; |
923 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){ | 929 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){ |
924 goto begin_table_error; | 930 goto begin_table_error; |
925 } | 931 } |
926 pTable = sqlite3FindTable(db, zName, zDb); | 932 pTable = sqlite3FindTable(db, zName, zDb); |
927 if( pTable ){ | 933 if( pTable ){ |
928 if( !noErr ){ | 934 if( !noErr ){ |
929 sqlite3ErrorMsg(pParse, "table %T already exists", pName); | 935 sqlite3ErrorMsg(pParse, "table %T already exists", pName); |
930 }else{ | 936 }else{ |
931 assert( !db->init.busy ); | 937 assert( !db->init.busy || CORRUPT_DB ); |
932 sqlite3CodeVerifySchema(pParse, iDb); | 938 sqlite3CodeVerifySchema(pParse, iDb); |
933 } | 939 } |
934 goto begin_table_error; | 940 goto begin_table_error; |
935 } | 941 } |
936 if( sqlite3FindIndex(db, zName, zDb)!=0 ){ | 942 if( sqlite3FindIndex(db, zName, zDb)!=0 ){ |
937 sqlite3ErrorMsg(pParse, "there is already an index named %s", zName); | 943 sqlite3ErrorMsg(pParse, "there is already an index named %s", zName); |
938 goto begin_table_error; | 944 goto begin_table_error; |
939 } | 945 } |
940 } | 946 } |
941 | 947 |
(...skipping 25 matching lines...) Expand all Loading... |
967 | 973 |
968 /* Begin generating the code that will insert the table record into | 974 /* Begin generating the code that will insert the table record into |
969 ** the SQLITE_MASTER table. Note in particular that we must go ahead | 975 ** the SQLITE_MASTER table. Note in particular that we must go ahead |
970 ** and allocate the record number for the table entry now. Before any | 976 ** and allocate the record number for the table entry now. Before any |
971 ** PRIMARY KEY or UNIQUE keywords are parsed. Those keywords will cause | 977 ** PRIMARY KEY or UNIQUE keywords are parsed. Those keywords will cause |
972 ** indices to be created and the table record must come before the | 978 ** indices to be created and the table record must come before the |
973 ** indices. Hence, the record number for the table must be allocated | 979 ** indices. Hence, the record number for the table must be allocated |
974 ** now. | 980 ** now. |
975 */ | 981 */ |
976 if( !db->init.busy && (v = sqlite3GetVdbe(pParse))!=0 ){ | 982 if( !db->init.busy && (v = sqlite3GetVdbe(pParse))!=0 ){ |
977 int j1; | 983 int addr1; |
978 int fileFormat; | 984 int fileFormat; |
979 int reg1, reg2, reg3; | 985 int reg1, reg2, reg3; |
980 sqlite3BeginWriteOperation(pParse, 0, iDb); | 986 /* nullRow[] is an OP_Record encoding of a row containing 5 NULLs */ |
| 987 static const char nullRow[] = { 6, 0, 0, 0, 0, 0 }; |
| 988 sqlite3BeginWriteOperation(pParse, 1, iDb); |
981 | 989 |
982 #ifndef SQLITE_OMIT_VIRTUALTABLE | 990 #ifndef SQLITE_OMIT_VIRTUALTABLE |
983 if( isVirtual ){ | 991 if( isVirtual ){ |
984 sqlite3VdbeAddOp0(v, OP_VBegin); | 992 sqlite3VdbeAddOp0(v, OP_VBegin); |
985 } | 993 } |
986 #endif | 994 #endif |
987 | 995 |
988 /* If the file format and encoding in the database have not been set, | 996 /* If the file format and encoding in the database have not been set, |
989 ** set them now. | 997 ** set them now. |
990 */ | 998 */ |
991 reg1 = pParse->regRowid = ++pParse->nMem; | 999 reg1 = pParse->regRowid = ++pParse->nMem; |
992 reg2 = pParse->regRoot = ++pParse->nMem; | 1000 reg2 = pParse->regRoot = ++pParse->nMem; |
993 reg3 = ++pParse->nMem; | 1001 reg3 = ++pParse->nMem; |
994 sqlite3VdbeAddOp3(v, OP_ReadCookie, iDb, reg3, BTREE_FILE_FORMAT); | 1002 sqlite3VdbeAddOp3(v, OP_ReadCookie, iDb, reg3, BTREE_FILE_FORMAT); |
995 sqlite3VdbeUsesBtree(v, iDb); | 1003 sqlite3VdbeUsesBtree(v, iDb); |
996 j1 = sqlite3VdbeAddOp1(v, OP_If, reg3); VdbeCoverage(v); | 1004 addr1 = sqlite3VdbeAddOp1(v, OP_If, reg3); VdbeCoverage(v); |
997 fileFormat = (db->flags & SQLITE_LegacyFileFmt)!=0 ? | 1005 fileFormat = (db->flags & SQLITE_LegacyFileFmt)!=0 ? |
998 1 : SQLITE_MAX_FILE_FORMAT; | 1006 1 : SQLITE_MAX_FILE_FORMAT; |
999 sqlite3VdbeAddOp2(v, OP_Integer, fileFormat, reg3); | 1007 sqlite3VdbeAddOp2(v, OP_Integer, fileFormat, reg3); |
1000 sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_FILE_FORMAT, reg3); | 1008 sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_FILE_FORMAT, reg3); |
1001 sqlite3VdbeAddOp2(v, OP_Integer, ENC(db), reg3); | 1009 sqlite3VdbeAddOp2(v, OP_Integer, ENC(db), reg3); |
1002 sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_TEXT_ENCODING, reg3); | 1010 sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_TEXT_ENCODING, reg3); |
1003 sqlite3VdbeJumpHere(v, j1); | 1011 sqlite3VdbeJumpHere(v, addr1); |
1004 | 1012 |
1005 /* This just creates a place-holder record in the sqlite_master table. | 1013 /* This just creates a place-holder record in the sqlite_master table. |
1006 ** The record created does not contain anything yet. It will be replaced | 1014 ** The record created does not contain anything yet. It will be replaced |
1007 ** by the real entry in code generated at sqlite3EndTable(). | 1015 ** by the real entry in code generated at sqlite3EndTable(). |
1008 ** | 1016 ** |
1009 ** The rowid for the new entry is left in register pParse->regRowid. | 1017 ** The rowid for the new entry is left in register pParse->regRowid. |
1010 ** The root page number of the new table is left in reg pParse->regRoot. | 1018 ** The root page number of the new table is left in reg pParse->regRoot. |
1011 ** The rowid and root page number values are needed by the code that | 1019 ** The rowid and root page number values are needed by the code that |
1012 ** sqlite3EndTable will generate. | 1020 ** sqlite3EndTable will generate. |
1013 */ | 1021 */ |
1014 #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) | 1022 #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) |
1015 if( isView || isVirtual ){ | 1023 if( isView || isVirtual ){ |
1016 sqlite3VdbeAddOp2(v, OP_Integer, 0, reg2); | 1024 sqlite3VdbeAddOp2(v, OP_Integer, 0, reg2); |
1017 }else | 1025 }else |
1018 #endif | 1026 #endif |
1019 { | 1027 { |
1020 pParse->addrCrTab = sqlite3VdbeAddOp2(v, OP_CreateTable, iDb, reg2); | 1028 pParse->addrCrTab = sqlite3VdbeAddOp2(v, OP_CreateTable, iDb, reg2); |
1021 } | 1029 } |
1022 sqlite3OpenMasterTable(pParse, iDb); | 1030 sqlite3OpenMasterTable(pParse, iDb); |
1023 sqlite3VdbeAddOp2(v, OP_NewRowid, 0, reg1); | 1031 sqlite3VdbeAddOp2(v, OP_NewRowid, 0, reg1); |
1024 sqlite3VdbeAddOp2(v, OP_Null, 0, reg3); | 1032 sqlite3VdbeAddOp4(v, OP_Blob, 6, reg3, 0, nullRow, P4_STATIC); |
1025 sqlite3VdbeAddOp3(v, OP_Insert, 0, reg3, reg1); | 1033 sqlite3VdbeAddOp3(v, OP_Insert, 0, reg3, reg1); |
1026 sqlite3VdbeChangeP5(v, OPFLAG_APPEND); | 1034 sqlite3VdbeChangeP5(v, OPFLAG_APPEND); |
1027 sqlite3VdbeAddOp0(v, OP_Close); | 1035 sqlite3VdbeAddOp0(v, OP_Close); |
1028 } | 1036 } |
1029 | 1037 |
1030 /* Normal (non-error) return. */ | 1038 /* Normal (non-error) return. */ |
1031 return; | 1039 return; |
1032 | 1040 |
1033 /* If an error occurs, we jump here */ | 1041 /* If an error occurs, we jump here */ |
1034 begin_table_error: | 1042 begin_table_error: |
1035 sqlite3DbFree(db, zName); | 1043 sqlite3DbFree(db, zName); |
1036 return; | 1044 return; |
1037 } | 1045 } |
1038 | 1046 |
1039 /* | 1047 /* Set properties of a table column based on the (magical) |
1040 ** This macro is used to compare two strings in a case-insensitive manner. | 1048 ** name of the column. |
1041 ** It is slightly faster than calling sqlite3StrICmp() directly, but | |
1042 ** produces larger code. | |
1043 ** | |
1044 ** WARNING: This macro is not compatible with the strcmp() family. It | |
1045 ** returns true if the two strings are equal, otherwise false. | |
1046 */ | 1049 */ |
1047 #define STRICMP(x, y) (\ | 1050 #if SQLITE_ENABLE_HIDDEN_COLUMNS |
1048 sqlite3UpperToLower[*(unsigned char *)(x)]== \ | 1051 void sqlite3ColumnPropertiesFromName(Table *pTab, Column *pCol){ |
1049 sqlite3UpperToLower[*(unsigned char *)(y)] \ | 1052 if( sqlite3_strnicmp(pCol->zName, "__hidden__", 10)==0 ){ |
1050 && sqlite3StrICmp((x)+1,(y)+1)==0 ) | 1053 pCol->colFlags |= COLFLAG_HIDDEN; |
| 1054 }else if( pTab && pCol!=pTab->aCol && (pCol[-1].colFlags & COLFLAG_HIDDEN) ){ |
| 1055 pTab->tabFlags |= TF_OOOHidden; |
| 1056 } |
| 1057 } |
| 1058 #endif |
| 1059 |
1051 | 1060 |
1052 /* | 1061 /* |
1053 ** Add a new column to the table currently being constructed. | 1062 ** Add a new column to the table currently being constructed. |
1054 ** | 1063 ** |
1055 ** The parser calls this routine once for each column declaration | 1064 ** The parser calls this routine once for each column declaration |
1056 ** in a CREATE TABLE statement. sqlite3StartTable() gets called | 1065 ** in a CREATE TABLE statement. sqlite3StartTable() gets called |
1057 ** first to get things going. Then this routine is called for each | 1066 ** first to get things going. Then this routine is called for each |
1058 ** column. | 1067 ** column. |
1059 */ | 1068 */ |
1060 void sqlite3AddColumn(Parse *pParse, Token *pName){ | 1069 void sqlite3AddColumn(Parse *pParse, Token *pName){ |
1061 Table *p; | 1070 Table *p; |
1062 int i; | 1071 int i; |
1063 char *z; | 1072 char *z; |
1064 Column *pCol; | 1073 Column *pCol; |
1065 sqlite3 *db = pParse->db; | 1074 sqlite3 *db = pParse->db; |
1066 if( (p = pParse->pNewTable)==0 ) return; | 1075 if( (p = pParse->pNewTable)==0 ) return; |
1067 #if SQLITE_MAX_COLUMN | 1076 #if SQLITE_MAX_COLUMN |
1068 if( p->nCol+1>db->aLimit[SQLITE_LIMIT_COLUMN] ){ | 1077 if( p->nCol+1>db->aLimit[SQLITE_LIMIT_COLUMN] ){ |
1069 sqlite3ErrorMsg(pParse, "too many columns on %s", p->zName); | 1078 sqlite3ErrorMsg(pParse, "too many columns on %s", p->zName); |
1070 return; | 1079 return; |
1071 } | 1080 } |
1072 #endif | 1081 #endif |
1073 z = sqlite3NameFromToken(db, pName); | 1082 z = sqlite3NameFromToken(db, pName); |
1074 if( z==0 ) return; | 1083 if( z==0 ) return; |
1075 for(i=0; i<p->nCol; i++){ | 1084 for(i=0; i<p->nCol; i++){ |
1076 if( STRICMP(z, p->aCol[i].zName) ){ | 1085 if( sqlite3_stricmp(z, p->aCol[i].zName)==0 ){ |
1077 sqlite3ErrorMsg(pParse, "duplicate column name: %s", z); | 1086 sqlite3ErrorMsg(pParse, "duplicate column name: %s", z); |
1078 sqlite3DbFree(db, z); | 1087 sqlite3DbFree(db, z); |
1079 return; | 1088 return; |
1080 } | 1089 } |
1081 } | 1090 } |
1082 if( (p->nCol & 0x7)==0 ){ | 1091 if( (p->nCol & 0x7)==0 ){ |
1083 Column *aNew; | 1092 Column *aNew; |
1084 aNew = sqlite3DbRealloc(db,p->aCol,(p->nCol+8)*sizeof(p->aCol[0])); | 1093 aNew = sqlite3DbRealloc(db,p->aCol,(p->nCol+8)*sizeof(p->aCol[0])); |
1085 if( aNew==0 ){ | 1094 if( aNew==0 ){ |
1086 sqlite3DbFree(db, z); | 1095 sqlite3DbFree(db, z); |
1087 return; | 1096 return; |
1088 } | 1097 } |
1089 p->aCol = aNew; | 1098 p->aCol = aNew; |
1090 } | 1099 } |
1091 pCol = &p->aCol[p->nCol]; | 1100 pCol = &p->aCol[p->nCol]; |
1092 memset(pCol, 0, sizeof(p->aCol[0])); | 1101 memset(pCol, 0, sizeof(p->aCol[0])); |
1093 pCol->zName = z; | 1102 pCol->zName = z; |
| 1103 sqlite3ColumnPropertiesFromName(p, pCol); |
1094 | 1104 |
1095 /* If there is no type specified, columns have the default affinity | 1105 /* If there is no type specified, columns have the default affinity |
1096 ** 'NONE'. If there is a type specified, then sqlite3AddColumnType() will | 1106 ** 'BLOB'. If there is a type specified, then sqlite3AddColumnType() will |
1097 ** be called next to set pCol->affinity correctly. | 1107 ** be called next to set pCol->affinity correctly. |
1098 */ | 1108 */ |
1099 pCol->affinity = SQLITE_AFF_NONE; | 1109 pCol->affinity = SQLITE_AFF_BLOB; |
1100 pCol->szEst = 1; | 1110 pCol->szEst = 1; |
1101 p->nCol++; | 1111 p->nCol++; |
1102 } | 1112 } |
1103 | 1113 |
1104 /* | 1114 /* |
1105 ** This routine is called by the parser while in the middle of | 1115 ** This routine is called by the parser while in the middle of |
1106 ** parsing a CREATE TABLE statement. A "NOT NULL" constraint has | 1116 ** parsing a CREATE TABLE statement. A "NOT NULL" constraint has |
1107 ** been seen on a column. This routine sets the notNull flag on | 1117 ** been seen on a column. This routine sets the notNull flag on |
1108 ** the column currently under construction. | 1118 ** the column currently under construction. |
1109 */ | 1119 */ |
(...skipping 14 matching lines...) Expand all Loading... |
1124 ** more than one of the substrings, entries toward the top of | 1134 ** more than one of the substrings, entries toward the top of |
1125 ** the table take priority. For example, if zType is 'BLOBINT', | 1135 ** the table take priority. For example, if zType is 'BLOBINT', |
1126 ** SQLITE_AFF_INTEGER is returned. | 1136 ** SQLITE_AFF_INTEGER is returned. |
1127 ** | 1137 ** |
1128 ** Substring | Affinity | 1138 ** Substring | Affinity |
1129 ** -------------------------------- | 1139 ** -------------------------------- |
1130 ** 'INT' | SQLITE_AFF_INTEGER | 1140 ** 'INT' | SQLITE_AFF_INTEGER |
1131 ** 'CHAR' | SQLITE_AFF_TEXT | 1141 ** 'CHAR' | SQLITE_AFF_TEXT |
1132 ** 'CLOB' | SQLITE_AFF_TEXT | 1142 ** 'CLOB' | SQLITE_AFF_TEXT |
1133 ** 'TEXT' | SQLITE_AFF_TEXT | 1143 ** 'TEXT' | SQLITE_AFF_TEXT |
1134 ** 'BLOB' | SQLITE_AFF_NONE | 1144 ** 'BLOB' | SQLITE_AFF_BLOB |
1135 ** 'REAL' | SQLITE_AFF_REAL | 1145 ** 'REAL' | SQLITE_AFF_REAL |
1136 ** 'FLOA' | SQLITE_AFF_REAL | 1146 ** 'FLOA' | SQLITE_AFF_REAL |
1137 ** 'DOUB' | SQLITE_AFF_REAL | 1147 ** 'DOUB' | SQLITE_AFF_REAL |
1138 ** | 1148 ** |
1139 ** If none of the substrings in the above table are found, | 1149 ** If none of the substrings in the above table are found, |
1140 ** SQLITE_AFF_NUMERIC is returned. | 1150 ** SQLITE_AFF_NUMERIC is returned. |
1141 */ | 1151 */ |
1142 char sqlite3AffinityType(const char *zIn, u8 *pszEst){ | 1152 char sqlite3AffinityType(const char *zIn, u8 *pszEst){ |
1143 u32 h = 0; | 1153 u32 h = 0; |
1144 char aff = SQLITE_AFF_NUMERIC; | 1154 char aff = SQLITE_AFF_NUMERIC; |
1145 const char *zChar = 0; | 1155 const char *zChar = 0; |
1146 | 1156 |
1147 if( zIn==0 ) return aff; | 1157 if( zIn==0 ) return aff; |
1148 while( zIn[0] ){ | 1158 while( zIn[0] ){ |
1149 h = (h<<8) + sqlite3UpperToLower[(*zIn)&0xff]; | 1159 h = (h<<8) + sqlite3UpperToLower[(*zIn)&0xff]; |
1150 zIn++; | 1160 zIn++; |
1151 if( h==(('c'<<24)+('h'<<16)+('a'<<8)+'r') ){ /* CHAR */ | 1161 if( h==(('c'<<24)+('h'<<16)+('a'<<8)+'r') ){ /* CHAR */ |
1152 aff = SQLITE_AFF_TEXT; | 1162 aff = SQLITE_AFF_TEXT; |
1153 zChar = zIn; | 1163 zChar = zIn; |
1154 }else if( h==(('c'<<24)+('l'<<16)+('o'<<8)+'b') ){ /* CLOB */ | 1164 }else if( h==(('c'<<24)+('l'<<16)+('o'<<8)+'b') ){ /* CLOB */ |
1155 aff = SQLITE_AFF_TEXT; | 1165 aff = SQLITE_AFF_TEXT; |
1156 }else if( h==(('t'<<24)+('e'<<16)+('x'<<8)+'t') ){ /* TEXT */ | 1166 }else if( h==(('t'<<24)+('e'<<16)+('x'<<8)+'t') ){ /* TEXT */ |
1157 aff = SQLITE_AFF_TEXT; | 1167 aff = SQLITE_AFF_TEXT; |
1158 }else if( h==(('b'<<24)+('l'<<16)+('o'<<8)+'b') /* BLOB */ | 1168 }else if( h==(('b'<<24)+('l'<<16)+('o'<<8)+'b') /* BLOB */ |
1159 && (aff==SQLITE_AFF_NUMERIC || aff==SQLITE_AFF_REAL) ){ | 1169 && (aff==SQLITE_AFF_NUMERIC || aff==SQLITE_AFF_REAL) ){ |
1160 aff = SQLITE_AFF_NONE; | 1170 aff = SQLITE_AFF_BLOB; |
1161 if( zIn[0]=='(' ) zChar = zIn; | 1171 if( zIn[0]=='(' ) zChar = zIn; |
1162 #ifndef SQLITE_OMIT_FLOATING_POINT | 1172 #ifndef SQLITE_OMIT_FLOATING_POINT |
1163 }else if( h==(('r'<<24)+('e'<<16)+('a'<<8)+'l') /* REAL */ | 1173 }else if( h==(('r'<<24)+('e'<<16)+('a'<<8)+'l') /* REAL */ |
1164 && aff==SQLITE_AFF_NUMERIC ){ | 1174 && aff==SQLITE_AFF_NUMERIC ){ |
1165 aff = SQLITE_AFF_REAL; | 1175 aff = SQLITE_AFF_REAL; |
1166 }else if( h==(('f'<<24)+('l'<<16)+('o'<<8)+'a') /* FLOA */ | 1176 }else if( h==(('f'<<24)+('l'<<16)+('o'<<8)+'a') /* FLOA */ |
1167 && aff==SQLITE_AFF_NUMERIC ){ | 1177 && aff==SQLITE_AFF_NUMERIC ){ |
1168 aff = SQLITE_AFF_REAL; | 1178 aff = SQLITE_AFF_REAL; |
1169 }else if( h==(('d'<<24)+('o'<<16)+('u'<<8)+'b') /* DOUB */ | 1179 }else if( h==(('d'<<24)+('o'<<16)+('u'<<8)+'b') /* DOUB */ |
1170 && aff==SQLITE_AFF_NUMERIC ){ | 1180 && aff==SQLITE_AFF_NUMERIC ){ |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1210 ** that contains the typename of the column and store that string | 1220 ** that contains the typename of the column and store that string |
1211 ** in zType. | 1221 ** in zType. |
1212 */ | 1222 */ |
1213 void sqlite3AddColumnType(Parse *pParse, Token *pType){ | 1223 void sqlite3AddColumnType(Parse *pParse, Token *pType){ |
1214 Table *p; | 1224 Table *p; |
1215 Column *pCol; | 1225 Column *pCol; |
1216 | 1226 |
1217 p = pParse->pNewTable; | 1227 p = pParse->pNewTable; |
1218 if( p==0 || NEVER(p->nCol<1) ) return; | 1228 if( p==0 || NEVER(p->nCol<1) ) return; |
1219 pCol = &p->aCol[p->nCol-1]; | 1229 pCol = &p->aCol[p->nCol-1]; |
1220 assert( pCol->zType==0 ); | 1230 assert( pCol->zType==0 || CORRUPT_DB ); |
| 1231 sqlite3DbFree(pParse->db, pCol->zType); |
1221 pCol->zType = sqlite3NameFromToken(pParse->db, pType); | 1232 pCol->zType = sqlite3NameFromToken(pParse->db, pType); |
1222 pCol->affinity = sqlite3AffinityType(pCol->zType, &pCol->szEst); | 1233 pCol->affinity = sqlite3AffinityType(pCol->zType, &pCol->szEst); |
1223 } | 1234 } |
1224 | 1235 |
1225 /* | 1236 /* |
1226 ** The expression is the default value for the most recently added column | 1237 ** The expression is the default value for the most recently added column |
1227 ** of the table currently under construction. | 1238 ** of the table currently under construction. |
1228 ** | 1239 ** |
1229 ** Default value expressions must be constant. Raise an exception if this | 1240 ** Default value expressions must be constant. Raise an exception if this |
1230 ** is not the case. | 1241 ** is not the case. |
(...skipping 20 matching lines...) Expand all Loading... |
1251 pCol->pDflt = sqlite3ExprDup(db, pSpan->pExpr, EXPRDUP_REDUCE); | 1262 pCol->pDflt = sqlite3ExprDup(db, pSpan->pExpr, EXPRDUP_REDUCE); |
1252 sqlite3DbFree(db, pCol->zDflt); | 1263 sqlite3DbFree(db, pCol->zDflt); |
1253 pCol->zDflt = sqlite3DbStrNDup(db, (char*)pSpan->zStart, | 1264 pCol->zDflt = sqlite3DbStrNDup(db, (char*)pSpan->zStart, |
1254 (int)(pSpan->zEnd - pSpan->zStart)); | 1265 (int)(pSpan->zEnd - pSpan->zStart)); |
1255 } | 1266 } |
1256 } | 1267 } |
1257 sqlite3ExprDelete(db, pSpan->pExpr); | 1268 sqlite3ExprDelete(db, pSpan->pExpr); |
1258 } | 1269 } |
1259 | 1270 |
1260 /* | 1271 /* |
| 1272 ** Backwards Compatibility Hack: |
| 1273 ** |
| 1274 ** Historical versions of SQLite accepted strings as column names in |
| 1275 ** indexes and PRIMARY KEY constraints and in UNIQUE constraints. Example: |
| 1276 ** |
| 1277 ** CREATE TABLE xyz(a,b,c,d,e,PRIMARY KEY('a'),UNIQUE('b','c' COLLATE trim) |
| 1278 ** CREATE INDEX abc ON xyz('c','d' DESC,'e' COLLATE nocase DESC); |
| 1279 ** |
| 1280 ** This is goofy. But to preserve backwards compatibility we continue to |
| 1281 ** accept it. This routine does the necessary conversion. It converts |
| 1282 ** the expression given in its argument from a TK_STRING into a TK_ID |
| 1283 ** if the expression is just a TK_STRING with an optional COLLATE clause. |
| 1284 ** If the epxression is anything other than TK_STRING, the expression is |
| 1285 ** unchanged. |
| 1286 */ |
| 1287 static void sqlite3StringToId(Expr *p){ |
| 1288 if( p->op==TK_STRING ){ |
| 1289 p->op = TK_ID; |
| 1290 }else if( p->op==TK_COLLATE && p->pLeft->op==TK_STRING ){ |
| 1291 p->pLeft->op = TK_ID; |
| 1292 } |
| 1293 } |
| 1294 |
| 1295 /* |
1261 ** Designate the PRIMARY KEY for the table. pList is a list of names | 1296 ** Designate the PRIMARY KEY for the table. pList is a list of names |
1262 ** of columns that form the primary key. If pList is NULL, then the | 1297 ** of columns that form the primary key. If pList is NULL, then the |
1263 ** most recently added column of the table is the primary key. | 1298 ** most recently added column of the table is the primary key. |
1264 ** | 1299 ** |
1265 ** A table can have at most one primary key. If the table already has | 1300 ** A table can have at most one primary key. If the table already has |
1266 ** a primary key (and this is the second primary key) then create an | 1301 ** a primary key (and this is the second primary key) then create an |
1267 ** error. | 1302 ** error. |
1268 ** | 1303 ** |
1269 ** If the PRIMARY KEY is on a single column whose datatype is INTEGER, | 1304 ** If the PRIMARY KEY is on a single column whose datatype is INTEGER, |
1270 ** then we will try to use that column as the rowid. Set the Table.iPKey | 1305 ** then we will try to use that column as the rowid. Set the Table.iPKey |
(...skipping 23 matching lines...) Expand all Loading... |
1294 } | 1329 } |
1295 pTab->tabFlags |= TF_HasPrimaryKey; | 1330 pTab->tabFlags |= TF_HasPrimaryKey; |
1296 if( pList==0 ){ | 1331 if( pList==0 ){ |
1297 iCol = pTab->nCol - 1; | 1332 iCol = pTab->nCol - 1; |
1298 pTab->aCol[iCol].colFlags |= COLFLAG_PRIMKEY; | 1333 pTab->aCol[iCol].colFlags |= COLFLAG_PRIMKEY; |
1299 zType = pTab->aCol[iCol].zType; | 1334 zType = pTab->aCol[iCol].zType; |
1300 nTerm = 1; | 1335 nTerm = 1; |
1301 }else{ | 1336 }else{ |
1302 nTerm = pList->nExpr; | 1337 nTerm = pList->nExpr; |
1303 for(i=0; i<nTerm; i++){ | 1338 for(i=0; i<nTerm; i++){ |
1304 for(iCol=0; iCol<pTab->nCol; iCol++){ | 1339 Expr *pCExpr = sqlite3ExprSkipCollate(pList->a[i].pExpr); |
1305 if( sqlite3StrICmp(pList->a[i].zName, pTab->aCol[iCol].zName)==0 ){ | 1340 assert( pCExpr!=0 ); |
1306 pTab->aCol[iCol].colFlags |= COLFLAG_PRIMKEY; | 1341 sqlite3StringToId(pCExpr); |
1307 zType = pTab->aCol[iCol].zType; | 1342 if( pCExpr->op==TK_ID ){ |
1308 break; | 1343 const char *zCName = pCExpr->u.zToken; |
| 1344 for(iCol=0; iCol<pTab->nCol; iCol++){ |
| 1345 if( sqlite3StrICmp(zCName, pTab->aCol[iCol].zName)==0 ){ |
| 1346 pTab->aCol[iCol].colFlags |= COLFLAG_PRIMKEY; |
| 1347 zType = pTab->aCol[iCol].zType; |
| 1348 break; |
| 1349 } |
1309 } | 1350 } |
1310 } | 1351 } |
1311 } | 1352 } |
1312 } | 1353 } |
1313 if( nTerm==1 | 1354 if( nTerm==1 |
1314 && zType && sqlite3StrICmp(zType, "INTEGER")==0 | 1355 && zType && sqlite3StrICmp(zType, "INTEGER")==0 |
1315 && sortOrder==SQLITE_SO_ASC | 1356 && sortOrder!=SQLITE_SO_DESC |
1316 ){ | 1357 ){ |
1317 pTab->iPKey = iCol; | 1358 pTab->iPKey = iCol; |
1318 pTab->keyConf = (u8)onError; | 1359 pTab->keyConf = (u8)onError; |
1319 assert( autoInc==0 || autoInc==1 ); | 1360 assert( autoInc==0 || autoInc==1 ); |
1320 pTab->tabFlags |= autoInc*TF_Autoincrement; | 1361 pTab->tabFlags |= autoInc*TF_Autoincrement; |
1321 if( pList ) pParse->iPkSortOrder = pList->a[0].sortOrder; | 1362 if( pList ) pParse->iPkSortOrder = pList->a[0].sortOrder; |
1322 }else if( autoInc ){ | 1363 }else if( autoInc ){ |
1323 #ifndef SQLITE_OMIT_AUTOINCREMENT | 1364 #ifndef SQLITE_OMIT_AUTOINCREMENT |
1324 sqlite3ErrorMsg(pParse, "AUTOINCREMENT is only allowed on an " | 1365 sqlite3ErrorMsg(pParse, "AUTOINCREMENT is only allowed on an " |
1325 "INTEGER PRIMARY KEY"); | 1366 "INTEGER PRIMARY KEY"); |
1326 #endif | 1367 #endif |
1327 }else{ | 1368 }else{ |
1328 Vdbe *v = pParse->pVdbe; | |
1329 Index *p; | 1369 Index *p; |
1330 if( v ) pParse->addrSkipPK = sqlite3VdbeAddOp0(v, OP_Noop); | |
1331 p = sqlite3CreateIndex(pParse, 0, 0, 0, pList, onError, 0, | 1370 p = sqlite3CreateIndex(pParse, 0, 0, 0, pList, onError, 0, |
1332 0, sortOrder, 0); | 1371 0, sortOrder, 0); |
1333 if( p ){ | 1372 if( p ){ |
1334 p->idxType = SQLITE_IDXTYPE_PRIMARYKEY; | 1373 p->idxType = SQLITE_IDXTYPE_PRIMARYKEY; |
1335 if( v ) sqlite3VdbeJumpHere(v, pParse->addrSkipPK); | |
1336 } | 1374 } |
1337 pList = 0; | 1375 pList = 0; |
1338 } | 1376 } |
1339 | 1377 |
1340 primary_key_exit: | 1378 primary_key_exit: |
1341 sqlite3ExprListDelete(pParse->db, pList); | 1379 sqlite3ExprListDelete(pParse->db, pList); |
1342 return; | 1380 return; |
1343 } | 1381 } |
1344 | 1382 |
1345 /* | 1383 /* |
(...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1544 if( zStmt==0 ){ | 1582 if( zStmt==0 ){ |
1545 db->mallocFailed = 1; | 1583 db->mallocFailed = 1; |
1546 return 0; | 1584 return 0; |
1547 } | 1585 } |
1548 sqlite3_snprintf(n, zStmt, "CREATE TABLE "); | 1586 sqlite3_snprintf(n, zStmt, "CREATE TABLE "); |
1549 k = sqlite3Strlen30(zStmt); | 1587 k = sqlite3Strlen30(zStmt); |
1550 identPut(zStmt, &k, p->zName); | 1588 identPut(zStmt, &k, p->zName); |
1551 zStmt[k++] = '('; | 1589 zStmt[k++] = '('; |
1552 for(pCol=p->aCol, i=0; i<p->nCol; i++, pCol++){ | 1590 for(pCol=p->aCol, i=0; i<p->nCol; i++, pCol++){ |
1553 static const char * const azType[] = { | 1591 static const char * const azType[] = { |
1554 /* SQLITE_AFF_NONE */ "", | 1592 /* SQLITE_AFF_BLOB */ "", |
1555 /* SQLITE_AFF_TEXT */ " TEXT", | 1593 /* SQLITE_AFF_TEXT */ " TEXT", |
1556 /* SQLITE_AFF_NUMERIC */ " NUM", | 1594 /* SQLITE_AFF_NUMERIC */ " NUM", |
1557 /* SQLITE_AFF_INTEGER */ " INT", | 1595 /* SQLITE_AFF_INTEGER */ " INT", |
1558 /* SQLITE_AFF_REAL */ " REAL" | 1596 /* SQLITE_AFF_REAL */ " REAL" |
1559 }; | 1597 }; |
1560 int len; | 1598 int len; |
1561 const char *zType; | 1599 const char *zType; |
1562 | 1600 |
1563 sqlite3_snprintf(n-k, &zStmt[k], zSep); | 1601 sqlite3_snprintf(n-k, &zStmt[k], zSep); |
1564 k += sqlite3Strlen30(&zStmt[k]); | 1602 k += sqlite3Strlen30(&zStmt[k]); |
1565 zSep = zSep2; | 1603 zSep = zSep2; |
1566 identPut(zStmt, &k, pCol->zName); | 1604 identPut(zStmt, &k, pCol->zName); |
1567 assert( pCol->affinity-SQLITE_AFF_NONE >= 0 ); | 1605 assert( pCol->affinity-SQLITE_AFF_BLOB >= 0 ); |
1568 assert( pCol->affinity-SQLITE_AFF_NONE < ArraySize(azType) ); | 1606 assert( pCol->affinity-SQLITE_AFF_BLOB < ArraySize(azType) ); |
1569 testcase( pCol->affinity==SQLITE_AFF_NONE ); | 1607 testcase( pCol->affinity==SQLITE_AFF_BLOB ); |
1570 testcase( pCol->affinity==SQLITE_AFF_TEXT ); | 1608 testcase( pCol->affinity==SQLITE_AFF_TEXT ); |
1571 testcase( pCol->affinity==SQLITE_AFF_NUMERIC ); | 1609 testcase( pCol->affinity==SQLITE_AFF_NUMERIC ); |
1572 testcase( pCol->affinity==SQLITE_AFF_INTEGER ); | 1610 testcase( pCol->affinity==SQLITE_AFF_INTEGER ); |
1573 testcase( pCol->affinity==SQLITE_AFF_REAL ); | 1611 testcase( pCol->affinity==SQLITE_AFF_REAL ); |
1574 | 1612 |
1575 zType = azType[pCol->affinity - SQLITE_AFF_NONE]; | 1613 zType = azType[pCol->affinity - SQLITE_AFF_BLOB]; |
1576 len = sqlite3Strlen30(zType); | 1614 len = sqlite3Strlen30(zType); |
1577 assert( pCol->affinity==SQLITE_AFF_NONE | 1615 assert( pCol->affinity==SQLITE_AFF_BLOB |
1578 || pCol->affinity==sqlite3AffinityType(zType, 0) ); | 1616 || pCol->affinity==sqlite3AffinityType(zType, 0) ); |
1579 memcpy(&zStmt[k], zType, len); | 1617 memcpy(&zStmt[k], zType, len); |
1580 k += len; | 1618 k += len; |
1581 assert( k<=n ); | 1619 assert( k<=n ); |
1582 } | 1620 } |
1583 sqlite3_snprintf(n-k, &zStmt[k], "%s", zEnd); | 1621 sqlite3_snprintf(n-k, &zStmt[k], "%s", zEnd); |
1584 return zStmt; | 1622 return zStmt; |
1585 } | 1623 } |
1586 | 1624 |
1587 /* | 1625 /* |
1588 ** Resize an Index object to hold N columns total. Return SQLITE_OK | 1626 ** Resize an Index object to hold N columns total. Return SQLITE_OK |
1589 ** on success and SQLITE_NOMEM on an OOM error. | 1627 ** on success and SQLITE_NOMEM on an OOM error. |
1590 */ | 1628 */ |
1591 static int resizeIndexObject(sqlite3 *db, Index *pIdx, int N){ | 1629 static int resizeIndexObject(sqlite3 *db, Index *pIdx, int N){ |
1592 char *zExtra; | 1630 char *zExtra; |
1593 int nByte; | 1631 int nByte; |
1594 if( pIdx->nColumn>=N ) return SQLITE_OK; | 1632 if( pIdx->nColumn>=N ) return SQLITE_OK; |
1595 assert( pIdx->isResized==0 ); | 1633 assert( pIdx->isResized==0 ); |
1596 nByte = (sizeof(char*) + sizeof(i16) + 1)*N; | 1634 nByte = (sizeof(char*) + sizeof(i16) + 1)*N; |
1597 zExtra = sqlite3DbMallocZero(db, nByte); | 1635 zExtra = sqlite3DbMallocZero(db, nByte); |
1598 if( zExtra==0 ) return SQLITE_NOMEM; | 1636 if( zExtra==0 ) return SQLITE_NOMEM; |
1599 memcpy(zExtra, pIdx->azColl, sizeof(char*)*pIdx->nColumn); | 1637 memcpy(zExtra, pIdx->azColl, sizeof(char*)*pIdx->nColumn); |
1600 pIdx->azColl = (char**)zExtra; | 1638 pIdx->azColl = (const char**)zExtra; |
1601 zExtra += sizeof(char*)*N; | 1639 zExtra += sizeof(char*)*N; |
1602 memcpy(zExtra, pIdx->aiColumn, sizeof(i16)*pIdx->nColumn); | 1640 memcpy(zExtra, pIdx->aiColumn, sizeof(i16)*pIdx->nColumn); |
1603 pIdx->aiColumn = (i16*)zExtra; | 1641 pIdx->aiColumn = (i16*)zExtra; |
1604 zExtra += sizeof(i16)*N; | 1642 zExtra += sizeof(i16)*N; |
1605 memcpy(zExtra, pIdx->aSortOrder, pIdx->nColumn); | 1643 memcpy(zExtra, pIdx->aSortOrder, pIdx->nColumn); |
1606 pIdx->aSortOrder = (u8*)zExtra; | 1644 pIdx->aSortOrder = (u8*)zExtra; |
1607 pIdx->nColumn = N; | 1645 pIdx->nColumn = N; |
1608 pIdx->isResized = 1; | 1646 pIdx->isResized = 1; |
1609 return SQLITE_OK; | 1647 return SQLITE_OK; |
1610 } | 1648 } |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1675 int i, j; | 1713 int i, j; |
1676 sqlite3 *db = pParse->db; | 1714 sqlite3 *db = pParse->db; |
1677 Vdbe *v = pParse->pVdbe; | 1715 Vdbe *v = pParse->pVdbe; |
1678 | 1716 |
1679 /* Convert the OP_CreateTable opcode that would normally create the | 1717 /* Convert the OP_CreateTable opcode that would normally create the |
1680 ** root-page for the table into an OP_CreateIndex opcode. The index | 1718 ** root-page for the table into an OP_CreateIndex opcode. The index |
1681 ** created will become the PRIMARY KEY index. | 1719 ** created will become the PRIMARY KEY index. |
1682 */ | 1720 */ |
1683 if( pParse->addrCrTab ){ | 1721 if( pParse->addrCrTab ){ |
1684 assert( v ); | 1722 assert( v ); |
1685 sqlite3VdbeGetOp(v, pParse->addrCrTab)->opcode = OP_CreateIndex; | 1723 sqlite3VdbeChangeOpcode(v, pParse->addrCrTab, OP_CreateIndex); |
1686 } | |
1687 | |
1688 /* Bypass the creation of the PRIMARY KEY btree and the sqlite_master | |
1689 ** table entry. | |
1690 */ | |
1691 if( pParse->addrSkipPK ){ | |
1692 assert( v ); | |
1693 sqlite3VdbeGetOp(v, pParse->addrSkipPK)->opcode = OP_Goto; | |
1694 } | 1724 } |
1695 | 1725 |
1696 /* Locate the PRIMARY KEY index. Or, if this table was originally | 1726 /* Locate the PRIMARY KEY index. Or, if this table was originally |
1697 ** an INTEGER PRIMARY KEY table, create a new PRIMARY KEY index. | 1727 ** an INTEGER PRIMARY KEY table, create a new PRIMARY KEY index. |
1698 */ | 1728 */ |
1699 if( pTab->iPKey>=0 ){ | 1729 if( pTab->iPKey>=0 ){ |
1700 ExprList *pList; | 1730 ExprList *pList; |
1701 pList = sqlite3ExprListAppend(pParse, 0, 0); | 1731 Token ipkToken; |
| 1732 ipkToken.z = pTab->aCol[pTab->iPKey].zName; |
| 1733 ipkToken.n = sqlite3Strlen30(ipkToken.z); |
| 1734 pList = sqlite3ExprListAppend(pParse, 0, |
| 1735 sqlite3ExprAlloc(db, TK_ID, &ipkToken, 0)); |
1702 if( pList==0 ) return; | 1736 if( pList==0 ) return; |
1703 pList->a[0].zName = sqlite3DbStrDup(pParse->db, | |
1704 pTab->aCol[pTab->iPKey].zName); | |
1705 pList->a[0].sortOrder = pParse->iPkSortOrder; | 1737 pList->a[0].sortOrder = pParse->iPkSortOrder; |
1706 assert( pParse->pNewTable==pTab ); | 1738 assert( pParse->pNewTable==pTab ); |
1707 pPk = sqlite3CreateIndex(pParse, 0, 0, 0, pList, pTab->keyConf, 0, 0, 0, 0); | 1739 pPk = sqlite3CreateIndex(pParse, 0, 0, 0, pList, pTab->keyConf, 0, 0, 0, 0); |
1708 if( pPk==0 ) return; | 1740 if( pPk==0 ) return; |
1709 pPk->idxType = SQLITE_IDXTYPE_PRIMARYKEY; | 1741 pPk->idxType = SQLITE_IDXTYPE_PRIMARYKEY; |
1710 pTab->iPKey = -1; | 1742 pTab->iPKey = -1; |
1711 }else{ | 1743 }else{ |
1712 pPk = sqlite3PrimaryKeyIndex(pTab); | 1744 pPk = sqlite3PrimaryKeyIndex(pTab); |
| 1745 |
| 1746 /* Bypass the creation of the PRIMARY KEY btree and the sqlite_master |
| 1747 ** table entry. This is only required if currently generating VDBE |
| 1748 ** code for a CREATE TABLE (not when parsing one as part of reading |
| 1749 ** a database schema). */ |
| 1750 if( v ){ |
| 1751 assert( db->init.busy==0 ); |
| 1752 sqlite3VdbeChangeOpcode(v, pPk->tnum, OP_Goto); |
| 1753 } |
| 1754 |
| 1755 /* |
| 1756 ** Remove all redundant columns from the PRIMARY KEY. For example, change |
| 1757 ** "PRIMARY KEY(a,b,a,b,c,b,c,d)" into just "PRIMARY KEY(a,b,c,d)". Later |
| 1758 ** code assumes the PRIMARY KEY contains no repeated columns. |
| 1759 */ |
| 1760 for(i=j=1; i<pPk->nKeyCol; i++){ |
| 1761 if( hasColumn(pPk->aiColumn, j, pPk->aiColumn[i]) ){ |
| 1762 pPk->nColumn--; |
| 1763 }else{ |
| 1764 pPk->aiColumn[j++] = pPk->aiColumn[i]; |
| 1765 } |
| 1766 } |
| 1767 pPk->nKeyCol = j; |
1713 } | 1768 } |
1714 pPk->isCovering = 1; | 1769 pPk->isCovering = 1; |
1715 assert( pPk!=0 ); | 1770 assert( pPk!=0 ); |
1716 nPk = pPk->nKeyCol; | 1771 nPk = pPk->nKeyCol; |
1717 | 1772 |
1718 /* Make sure every column of the PRIMARY KEY is NOT NULL */ | 1773 /* Make sure every column of the PRIMARY KEY is NOT NULL. (Except, |
1719 for(i=0; i<nPk; i++){ | 1774 ** do not enforce this for imposter tables.) */ |
1720 pTab->aCol[pPk->aiColumn[i]].notNull = 1; | 1775 if( !db->init.imposterTable ){ |
| 1776 for(i=0; i<nPk; i++){ |
| 1777 pTab->aCol[pPk->aiColumn[i]].notNull = OE_Abort; |
| 1778 } |
| 1779 pPk->uniqNotNull = 1; |
1721 } | 1780 } |
1722 pPk->uniqNotNull = 1; | |
1723 | 1781 |
1724 /* The root page of the PRIMARY KEY is the table root page */ | 1782 /* The root page of the PRIMARY KEY is the table root page */ |
1725 pPk->tnum = pTab->tnum; | 1783 pPk->tnum = pTab->tnum; |
1726 | 1784 |
1727 /* Update the in-memory representation of all UNIQUE indices by converting | 1785 /* Update the in-memory representation of all UNIQUE indices by converting |
1728 ** the final rowid column into one or more columns of the PRIMARY KEY. | 1786 ** the final rowid column into one or more columns of the PRIMARY KEY. |
1729 */ | 1787 */ |
1730 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ | 1788 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
1731 int n; | 1789 int n; |
1732 if( IsPrimaryKeyIndex(pIdx) ) continue; | 1790 if( IsPrimaryKeyIndex(pIdx) ) continue; |
(...skipping 18 matching lines...) Expand all Loading... |
1751 } | 1809 } |
1752 | 1810 |
1753 /* Add all table columns to the PRIMARY KEY index | 1811 /* Add all table columns to the PRIMARY KEY index |
1754 */ | 1812 */ |
1755 if( nPk<pTab->nCol ){ | 1813 if( nPk<pTab->nCol ){ |
1756 if( resizeIndexObject(db, pPk, pTab->nCol) ) return; | 1814 if( resizeIndexObject(db, pPk, pTab->nCol) ) return; |
1757 for(i=0, j=nPk; i<pTab->nCol; i++){ | 1815 for(i=0, j=nPk; i<pTab->nCol; i++){ |
1758 if( !hasColumn(pPk->aiColumn, j, i) ){ | 1816 if( !hasColumn(pPk->aiColumn, j, i) ){ |
1759 assert( j<pPk->nColumn ); | 1817 assert( j<pPk->nColumn ); |
1760 pPk->aiColumn[j] = i; | 1818 pPk->aiColumn[j] = i; |
1761 pPk->azColl[j] = "BINARY"; | 1819 pPk->azColl[j] = sqlite3StrBINARY; |
1762 j++; | 1820 j++; |
1763 } | 1821 } |
1764 } | 1822 } |
1765 assert( pPk->nColumn==j ); | 1823 assert( pPk->nColumn==j ); |
1766 assert( pTab->nCol==j ); | 1824 assert( pTab->nCol==j ); |
1767 }else{ | 1825 }else{ |
1768 pPk->nColumn = pTab->nCol; | 1826 pPk->nColumn = pTab->nCol; |
1769 } | 1827 } |
1770 } | 1828 } |
1771 | 1829 |
(...skipping 22 matching lines...) Expand all Loading... |
1794 Token *pCons, /* The ',' token after the last column defn. */ | 1852 Token *pCons, /* The ',' token after the last column defn. */ |
1795 Token *pEnd, /* The ')' before options in the CREATE TABLE */ | 1853 Token *pEnd, /* The ')' before options in the CREATE TABLE */ |
1796 u8 tabOpts, /* Extra table options. Usually 0. */ | 1854 u8 tabOpts, /* Extra table options. Usually 0. */ |
1797 Select *pSelect /* Select from a "CREATE ... AS SELECT" */ | 1855 Select *pSelect /* Select from a "CREATE ... AS SELECT" */ |
1798 ){ | 1856 ){ |
1799 Table *p; /* The new table */ | 1857 Table *p; /* The new table */ |
1800 sqlite3 *db = pParse->db; /* The database connection */ | 1858 sqlite3 *db = pParse->db; /* The database connection */ |
1801 int iDb; /* Database in which the table lives */ | 1859 int iDb; /* Database in which the table lives */ |
1802 Index *pIdx; /* An implied index of the table */ | 1860 Index *pIdx; /* An implied index of the table */ |
1803 | 1861 |
1804 if( (pEnd==0 && pSelect==0) || db->mallocFailed ){ | 1862 if( pEnd==0 && pSelect==0 ){ |
1805 return; | 1863 return; |
1806 } | 1864 } |
| 1865 assert( !db->mallocFailed ); |
1807 p = pParse->pNewTable; | 1866 p = pParse->pNewTable; |
1808 if( p==0 ) return; | 1867 if( p==0 ) return; |
1809 | 1868 |
1810 assert( !db->init.busy || !pSelect ); | 1869 assert( !db->init.busy || !pSelect ); |
1811 | 1870 |
1812 /* If the db->init.busy is 1 it means we are reading the SQL off the | 1871 /* If the db->init.busy is 1 it means we are reading the SQL off the |
1813 ** "sqlite_master" or "sqlite_temp_master" table on the disk. | 1872 ** "sqlite_master" or "sqlite_temp_master" table on the disk. |
1814 ** So do not write to the disk again. Extract the root page number | 1873 ** So do not write to the disk again. Extract the root page number |
1815 ** for the table from the db->init.newTnum field. (The page number | 1874 ** for the table from the db->init.newTnum field. (The page number |
1816 ** should have been put there by the sqliteOpenCb routine.) | 1875 ** should have been put there by the sqliteOpenCb routine.) |
1817 */ | 1876 */ |
1818 if( db->init.busy ){ | 1877 if( db->init.busy ){ |
1819 p->tnum = db->init.newTnum; | 1878 p->tnum = db->init.newTnum; |
1820 } | 1879 } |
1821 | 1880 |
1822 /* Special processing for WITHOUT ROWID Tables */ | 1881 /* Special processing for WITHOUT ROWID Tables */ |
1823 if( tabOpts & TF_WithoutRowid ){ | 1882 if( tabOpts & TF_WithoutRowid ){ |
1824 if( (p->tabFlags & TF_Autoincrement) ){ | 1883 if( (p->tabFlags & TF_Autoincrement) ){ |
1825 sqlite3ErrorMsg(pParse, | 1884 sqlite3ErrorMsg(pParse, |
1826 "AUTOINCREMENT not allowed on WITHOUT ROWID tables"); | 1885 "AUTOINCREMENT not allowed on WITHOUT ROWID tables"); |
1827 return; | 1886 return; |
1828 } | 1887 } |
1829 if( (p->tabFlags & TF_HasPrimaryKey)==0 ){ | 1888 if( (p->tabFlags & TF_HasPrimaryKey)==0 ){ |
1830 sqlite3ErrorMsg(pParse, "PRIMARY KEY missing on table %s", p->zName); | 1889 sqlite3ErrorMsg(pParse, "PRIMARY KEY missing on table %s", p->zName); |
1831 }else{ | 1890 }else{ |
1832 p->tabFlags |= TF_WithoutRowid; | 1891 p->tabFlags |= TF_WithoutRowid | TF_NoVisibleRowid; |
1833 convertToWithoutRowidTable(pParse, p); | 1892 convertToWithoutRowidTable(pParse, p); |
1834 } | 1893 } |
1835 } | 1894 } |
1836 | 1895 |
1837 iDb = sqlite3SchemaToIndex(db, p->pSchema); | 1896 iDb = sqlite3SchemaToIndex(db, p->pSchema); |
1838 | 1897 |
1839 #ifndef SQLITE_OMIT_CHECK | 1898 #ifndef SQLITE_OMIT_CHECK |
1840 /* Resolve names in all CHECK constraint expressions. | 1899 /* Resolve names in all CHECK constraint expressions. |
1841 */ | 1900 */ |
1842 if( p->pCheck ){ | 1901 if( p->pCheck ){ |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1890 ** Once the SELECT has been coded by sqlite3Select(), it is in a | 1949 ** Once the SELECT has been coded by sqlite3Select(), it is in a |
1891 ** suitable state to query for the column names and types to be used | 1950 ** suitable state to query for the column names and types to be used |
1892 ** by the new table. | 1951 ** by the new table. |
1893 ** | 1952 ** |
1894 ** A shared-cache write-lock is not required to write to the new table, | 1953 ** A shared-cache write-lock is not required to write to the new table, |
1895 ** as a schema-lock must have already been obtained to create it. Since | 1954 ** as a schema-lock must have already been obtained to create it. Since |
1896 ** a schema-lock excludes all other database users, the write-lock would | 1955 ** a schema-lock excludes all other database users, the write-lock would |
1897 ** be redundant. | 1956 ** be redundant. |
1898 */ | 1957 */ |
1899 if( pSelect ){ | 1958 if( pSelect ){ |
1900 SelectDest dest; | 1959 SelectDest dest; /* Where the SELECT should store results */ |
1901 Table *pSelTab; | 1960 int regYield; /* Register holding co-routine entry-point */ |
| 1961 int addrTop; /* Top of the co-routine */ |
| 1962 int regRec; /* A record to be insert into the new table */ |
| 1963 int regRowid; /* Rowid of the next row to insert */ |
| 1964 int addrInsLoop; /* Top of the loop for inserting rows */ |
| 1965 Table *pSelTab; /* A table that describes the SELECT results */ |
1902 | 1966 |
| 1967 regYield = ++pParse->nMem; |
| 1968 regRec = ++pParse->nMem; |
| 1969 regRowid = ++pParse->nMem; |
1903 assert(pParse->nTab==1); | 1970 assert(pParse->nTab==1); |
| 1971 sqlite3MayAbort(pParse); |
1904 sqlite3VdbeAddOp3(v, OP_OpenWrite, 1, pParse->regRoot, iDb); | 1972 sqlite3VdbeAddOp3(v, OP_OpenWrite, 1, pParse->regRoot, iDb); |
1905 sqlite3VdbeChangeP5(v, OPFLAG_P2ISREG); | 1973 sqlite3VdbeChangeP5(v, OPFLAG_P2ISREG); |
1906 pParse->nTab = 2; | 1974 pParse->nTab = 2; |
1907 sqlite3SelectDestInit(&dest, SRT_Table, 1); | 1975 addrTop = sqlite3VdbeCurrentAddr(v) + 1; |
| 1976 sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, addrTop); |
| 1977 sqlite3SelectDestInit(&dest, SRT_Coroutine, regYield); |
1908 sqlite3Select(pParse, pSelect, &dest); | 1978 sqlite3Select(pParse, pSelect, &dest); |
| 1979 sqlite3VdbeAddOp1(v, OP_EndCoroutine, regYield); |
| 1980 sqlite3VdbeJumpHere(v, addrTop - 1); |
| 1981 if( pParse->nErr ) return; |
| 1982 pSelTab = sqlite3ResultSetOfSelect(pParse, pSelect); |
| 1983 if( pSelTab==0 ) return; |
| 1984 assert( p->aCol==0 ); |
| 1985 p->nCol = pSelTab->nCol; |
| 1986 p->aCol = pSelTab->aCol; |
| 1987 pSelTab->nCol = 0; |
| 1988 pSelTab->aCol = 0; |
| 1989 sqlite3DeleteTable(db, pSelTab); |
| 1990 addrInsLoop = sqlite3VdbeAddOp1(v, OP_Yield, dest.iSDParm); |
| 1991 VdbeCoverage(v); |
| 1992 sqlite3VdbeAddOp3(v, OP_MakeRecord, dest.iSdst, dest.nSdst, regRec); |
| 1993 sqlite3TableAffinity(v, p, 0); |
| 1994 sqlite3VdbeAddOp2(v, OP_NewRowid, 1, regRowid); |
| 1995 sqlite3VdbeAddOp3(v, OP_Insert, 1, regRec, regRowid); |
| 1996 sqlite3VdbeGoto(v, addrInsLoop); |
| 1997 sqlite3VdbeJumpHere(v, addrInsLoop); |
1909 sqlite3VdbeAddOp1(v, OP_Close, 1); | 1998 sqlite3VdbeAddOp1(v, OP_Close, 1); |
1910 if( pParse->nErr==0 ){ | |
1911 pSelTab = sqlite3ResultSetOfSelect(pParse, pSelect); | |
1912 if( pSelTab==0 ) return; | |
1913 assert( p->aCol==0 ); | |
1914 p->nCol = pSelTab->nCol; | |
1915 p->aCol = pSelTab->aCol; | |
1916 pSelTab->nCol = 0; | |
1917 pSelTab->aCol = 0; | |
1918 sqlite3DeleteTable(db, pSelTab); | |
1919 } | |
1920 } | 1999 } |
1921 | 2000 |
1922 /* Compute the complete text of the CREATE statement */ | 2001 /* Compute the complete text of the CREATE statement */ |
1923 if( pSelect ){ | 2002 if( pSelect ){ |
1924 zStmt = createTableStmt(db, p); | 2003 zStmt = createTableStmt(db, p); |
1925 }else{ | 2004 }else{ |
1926 Token *pEnd2 = tabOpts ? &pParse->sLastToken : pEnd; | 2005 Token *pEnd2 = tabOpts ? &pParse->sLastToken : pEnd; |
1927 n = (int)(pEnd2->z - pParse->sNameToken.z); | 2006 n = (int)(pEnd2->z - pParse->sNameToken.z); |
1928 if( pEnd2->z[0]!=';' ) n += pEnd2->n; | 2007 if( pEnd2->z[0]!=';' ) n += pEnd2->n; |
1929 zStmt = sqlite3MPrintf(db, | 2008 zStmt = sqlite3MPrintf(db, |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2004 | 2083 |
2005 #ifndef SQLITE_OMIT_VIEW | 2084 #ifndef SQLITE_OMIT_VIEW |
2006 /* | 2085 /* |
2007 ** The parser calls this routine in order to create a new VIEW | 2086 ** The parser calls this routine in order to create a new VIEW |
2008 */ | 2087 */ |
2009 void sqlite3CreateView( | 2088 void sqlite3CreateView( |
2010 Parse *pParse, /* The parsing context */ | 2089 Parse *pParse, /* The parsing context */ |
2011 Token *pBegin, /* The CREATE token that begins the statement */ | 2090 Token *pBegin, /* The CREATE token that begins the statement */ |
2012 Token *pName1, /* The token that holds the name of the view */ | 2091 Token *pName1, /* The token that holds the name of the view */ |
2013 Token *pName2, /* The token that holds the name of the view */ | 2092 Token *pName2, /* The token that holds the name of the view */ |
| 2093 ExprList *pCNames, /* Optional list of view column names */ |
2014 Select *pSelect, /* A SELECT statement that will become the new view */ | 2094 Select *pSelect, /* A SELECT statement that will become the new view */ |
2015 int isTemp, /* TRUE for a TEMPORARY view */ | 2095 int isTemp, /* TRUE for a TEMPORARY view */ |
2016 int noErr /* Suppress error messages if VIEW already exists */ | 2096 int noErr /* Suppress error messages if VIEW already exists */ |
2017 ){ | 2097 ){ |
2018 Table *p; | 2098 Table *p; |
2019 int n; | 2099 int n; |
2020 const char *z; | 2100 const char *z; |
2021 Token sEnd; | 2101 Token sEnd; |
2022 DbFixer sFix; | 2102 DbFixer sFix; |
2023 Token *pName = 0; | 2103 Token *pName = 0; |
2024 int iDb; | 2104 int iDb; |
2025 sqlite3 *db = pParse->db; | 2105 sqlite3 *db = pParse->db; |
2026 | 2106 |
2027 if( pParse->nVar>0 ){ | 2107 if( pParse->nVar>0 ){ |
2028 sqlite3ErrorMsg(pParse, "parameters are not allowed in views"); | 2108 sqlite3ErrorMsg(pParse, "parameters are not allowed in views"); |
2029 sqlite3SelectDelete(db, pSelect); | 2109 goto create_view_fail; |
2030 return; | |
2031 } | 2110 } |
2032 sqlite3StartTable(pParse, pName1, pName2, isTemp, 1, 0, noErr); | 2111 sqlite3StartTable(pParse, pName1, pName2, isTemp, 1, 0, noErr); |
2033 p = pParse->pNewTable; | 2112 p = pParse->pNewTable; |
2034 if( p==0 || pParse->nErr ){ | 2113 if( p==0 || pParse->nErr ) goto create_view_fail; |
2035 sqlite3SelectDelete(db, pSelect); | |
2036 return; | |
2037 } | |
2038 sqlite3TwoPartName(pParse, pName1, pName2, &pName); | 2114 sqlite3TwoPartName(pParse, pName1, pName2, &pName); |
2039 iDb = sqlite3SchemaToIndex(db, p->pSchema); | 2115 iDb = sqlite3SchemaToIndex(db, p->pSchema); |
2040 sqlite3FixInit(&sFix, pParse, iDb, "view", pName); | 2116 sqlite3FixInit(&sFix, pParse, iDb, "view", pName); |
2041 if( sqlite3FixSelect(&sFix, pSelect) ){ | 2117 if( sqlite3FixSelect(&sFix, pSelect) ) goto create_view_fail; |
2042 sqlite3SelectDelete(db, pSelect); | |
2043 return; | |
2044 } | |
2045 | 2118 |
2046 /* Make a copy of the entire SELECT statement that defines the view. | 2119 /* Make a copy of the entire SELECT statement that defines the view. |
2047 ** This will force all the Expr.token.z values to be dynamically | 2120 ** This will force all the Expr.token.z values to be dynamically |
2048 ** allocated rather than point to the input string - which means that | 2121 ** allocated rather than point to the input string - which means that |
2049 ** they will persist after the current sqlite3_exec() call returns. | 2122 ** they will persist after the current sqlite3_exec() call returns. |
2050 */ | 2123 */ |
2051 p->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE); | 2124 p->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE); |
2052 sqlite3SelectDelete(db, pSelect); | 2125 p->pCheck = sqlite3ExprListDup(db, pCNames, EXPRDUP_REDUCE); |
2053 if( db->mallocFailed ){ | 2126 if( db->mallocFailed ) goto create_view_fail; |
2054 return; | |
2055 } | |
2056 if( !db->init.busy ){ | |
2057 sqlite3ViewGetColumnNames(pParse, p); | |
2058 } | |
2059 | 2127 |
2060 /* Locate the end of the CREATE VIEW statement. Make sEnd point to | 2128 /* Locate the end of the CREATE VIEW statement. Make sEnd point to |
2061 ** the end. | 2129 ** the end. |
2062 */ | 2130 */ |
2063 sEnd = pParse->sLastToken; | 2131 sEnd = pParse->sLastToken; |
2064 if( ALWAYS(sEnd.z[0]!=0) && sEnd.z[0]!=';' ){ | 2132 assert( sEnd.z[0]!=0 ); |
| 2133 if( sEnd.z[0]!=';' ){ |
2065 sEnd.z += sEnd.n; | 2134 sEnd.z += sEnd.n; |
2066 } | 2135 } |
2067 sEnd.n = 0; | 2136 sEnd.n = 0; |
2068 n = (int)(sEnd.z - pBegin->z); | 2137 n = (int)(sEnd.z - pBegin->z); |
| 2138 assert( n>0 ); |
2069 z = pBegin->z; | 2139 z = pBegin->z; |
2070 while( ALWAYS(n>0) && sqlite3Isspace(z[n-1]) ){ n--; } | 2140 while( sqlite3Isspace(z[n-1]) ){ n--; } |
2071 sEnd.z = &z[n-1]; | 2141 sEnd.z = &z[n-1]; |
2072 sEnd.n = 1; | 2142 sEnd.n = 1; |
2073 | 2143 |
2074 /* Use sqlite3EndTable() to add the view to the SQLITE_MASTER table */ | 2144 /* Use sqlite3EndTable() to add the view to the SQLITE_MASTER table */ |
2075 sqlite3EndTable(pParse, 0, &sEnd, 0, 0); | 2145 sqlite3EndTable(pParse, 0, &sEnd, 0, 0); |
| 2146 |
| 2147 create_view_fail: |
| 2148 sqlite3SelectDelete(db, pSelect); |
| 2149 sqlite3ExprListDelete(db, pCNames); |
2076 return; | 2150 return; |
2077 } | 2151 } |
2078 #endif /* SQLITE_OMIT_VIEW */ | 2152 #endif /* SQLITE_OMIT_VIEW */ |
2079 | 2153 |
2080 #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) | 2154 #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) |
2081 /* | 2155 /* |
2082 ** The Table structure pTable is really a VIEW. Fill in the names of | 2156 ** The Table structure pTable is really a VIEW. Fill in the names of |
2083 ** the columns of the view in the pTable structure. Return the number | 2157 ** the columns of the view in the pTable structure. Return the number |
2084 ** of errors. If an error is seen leave an error message in pParse->zErrMsg. | 2158 ** of errors. If an error is seen leave an error message in pParse->zErrMsg. |
2085 */ | 2159 */ |
2086 int sqlite3ViewGetColumnNames(Parse *pParse, Table *pTable){ | 2160 int sqlite3ViewGetColumnNames(Parse *pParse, Table *pTable){ |
2087 Table *pSelTab; /* A fake table from which we get the result set */ | 2161 Table *pSelTab; /* A fake table from which we get the result set */ |
2088 Select *pSel; /* Copy of the SELECT that implements the view */ | 2162 Select *pSel; /* Copy of the SELECT that implements the view */ |
2089 int nErr = 0; /* Number of errors encountered */ | 2163 int nErr = 0; /* Number of errors encountered */ |
2090 int n; /* Temporarily holds the number of cursors assigned */ | 2164 int n; /* Temporarily holds the number of cursors assigned */ |
2091 sqlite3 *db = pParse->db; /* Database connection for malloc errors */ | 2165 sqlite3 *db = pParse->db; /* Database connection for malloc errors */ |
2092 sqlite3_xauth xAuth; /* Saved xAuth pointer */ | 2166 sqlite3_xauth xAuth; /* Saved xAuth pointer */ |
| 2167 u8 bEnabledLA; /* Saved db->lookaside.bEnabled state */ |
2093 | 2168 |
2094 assert( pTable ); | 2169 assert( pTable ); |
2095 | 2170 |
2096 #ifndef SQLITE_OMIT_VIRTUALTABLE | 2171 #ifndef SQLITE_OMIT_VIRTUALTABLE |
2097 if( sqlite3VtabCallConnect(pParse, pTable) ){ | 2172 if( sqlite3VtabCallConnect(pParse, pTable) ){ |
2098 return SQLITE_ERROR; | 2173 return SQLITE_ERROR; |
2099 } | 2174 } |
2100 if( IsVirtual(pTable) ) return 0; | 2175 if( IsVirtual(pTable) ) return 0; |
2101 #endif | 2176 #endif |
2102 | 2177 |
(...skipping 25 matching lines...) Expand all Loading... |
2128 assert( pTable->nCol>=0 ); | 2203 assert( pTable->nCol>=0 ); |
2129 | 2204 |
2130 /* If we get this far, it means we need to compute the table names. | 2205 /* If we get this far, it means we need to compute the table names. |
2131 ** Note that the call to sqlite3ResultSetOfSelect() will expand any | 2206 ** Note that the call to sqlite3ResultSetOfSelect() will expand any |
2132 ** "*" elements in the results set of the view and will assign cursors | 2207 ** "*" elements in the results set of the view and will assign cursors |
2133 ** to the elements of the FROM clause. But we do not want these changes | 2208 ** to the elements of the FROM clause. But we do not want these changes |
2134 ** to be permanent. So the computation is done on a copy of the SELECT | 2209 ** to be permanent. So the computation is done on a copy of the SELECT |
2135 ** statement that defines the view. | 2210 ** statement that defines the view. |
2136 */ | 2211 */ |
2137 assert( pTable->pSelect ); | 2212 assert( pTable->pSelect ); |
2138 pSel = sqlite3SelectDup(db, pTable->pSelect, 0); | 2213 bEnabledLA = db->lookaside.bEnabled; |
2139 if( pSel ){ | 2214 if( pTable->pCheck ){ |
2140 u8 enableLookaside = db->lookaside.bEnabled; | |
2141 n = pParse->nTab; | |
2142 sqlite3SrcListAssignCursors(pParse, pSel->pSrc); | |
2143 pTable->nCol = -1; | |
2144 db->lookaside.bEnabled = 0; | 2215 db->lookaside.bEnabled = 0; |
| 2216 sqlite3ColumnsFromExprList(pParse, pTable->pCheck, |
| 2217 &pTable->nCol, &pTable->aCol); |
| 2218 }else{ |
| 2219 pSel = sqlite3SelectDup(db, pTable->pSelect, 0); |
| 2220 if( pSel ){ |
| 2221 n = pParse->nTab; |
| 2222 sqlite3SrcListAssignCursors(pParse, pSel->pSrc); |
| 2223 pTable->nCol = -1; |
| 2224 db->lookaside.bEnabled = 0; |
2145 #ifndef SQLITE_OMIT_AUTHORIZATION | 2225 #ifndef SQLITE_OMIT_AUTHORIZATION |
2146 xAuth = db->xAuth; | 2226 xAuth = db->xAuth; |
2147 db->xAuth = 0; | 2227 db->xAuth = 0; |
2148 pSelTab = sqlite3ResultSetOfSelect(pParse, pSel); | 2228 pSelTab = sqlite3ResultSetOfSelect(pParse, pSel); |
2149 db->xAuth = xAuth; | 2229 db->xAuth = xAuth; |
2150 #else | 2230 #else |
2151 pSelTab = sqlite3ResultSetOfSelect(pParse, pSel); | 2231 pSelTab = sqlite3ResultSetOfSelect(pParse, pSel); |
2152 #endif | 2232 #endif |
2153 db->lookaside.bEnabled = enableLookaside; | 2233 pParse->nTab = n; |
2154 pParse->nTab = n; | 2234 if( pSelTab ){ |
2155 if( pSelTab ){ | 2235 assert( pTable->aCol==0 ); |
2156 assert( pTable->aCol==0 ); | 2236 pTable->nCol = pSelTab->nCol; |
2157 pTable->nCol = pSelTab->nCol; | 2237 pTable->aCol = pSelTab->aCol; |
2158 pTable->aCol = pSelTab->aCol; | 2238 pSelTab->nCol = 0; |
2159 pSelTab->nCol = 0; | 2239 pSelTab->aCol = 0; |
2160 pSelTab->aCol = 0; | 2240 sqlite3DeleteTable(db, pSelTab); |
2161 sqlite3DeleteTable(db, pSelTab); | 2241 assert( sqlite3SchemaMutexHeld(db, 0, pTable->pSchema) ); |
2162 assert( sqlite3SchemaMutexHeld(db, 0, pTable->pSchema) ); | 2242 }else{ |
2163 pTable->pSchema->schemaFlags |= DB_UnresetViews; | 2243 pTable->nCol = 0; |
2164 }else{ | 2244 nErr++; |
2165 pTable->nCol = 0; | 2245 } |
| 2246 sqlite3SelectDelete(db, pSel); |
| 2247 } else { |
2166 nErr++; | 2248 nErr++; |
2167 } | 2249 } |
2168 sqlite3SelectDelete(db, pSel); | |
2169 } else { | |
2170 nErr++; | |
2171 } | 2250 } |
| 2251 db->lookaside.bEnabled = bEnabledLA; |
| 2252 pTable->pSchema->schemaFlags |= DB_UnresetViews; |
2172 #endif /* SQLITE_OMIT_VIEW */ | 2253 #endif /* SQLITE_OMIT_VIEW */ |
2173 return nErr; | 2254 return nErr; |
2174 } | 2255 } |
2175 #endif /* !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) */ | 2256 #endif /* !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) */ |
2176 | 2257 |
2177 #ifndef SQLITE_OMIT_VIEW | 2258 #ifndef SQLITE_OMIT_VIEW |
2178 /* | 2259 /* |
2179 ** Clear the column names from every VIEW in database idx. | 2260 ** Clear the column names from every VIEW in database idx. |
2180 */ | 2261 */ |
2181 static void sqliteViewResetAll(sqlite3 *db, int idx){ | 2262 static void sqliteViewResetAll(sqlite3 *db, int idx){ |
2182 HashElem *i; | 2263 HashElem *i; |
2183 assert( sqlite3SchemaMutexHeld(db, idx, 0) ); | 2264 assert( sqlite3SchemaMutexHeld(db, idx, 0) ); |
2184 if( !DbHasProperty(db, idx, DB_UnresetViews) ) return; | 2265 if( !DbHasProperty(db, idx, DB_UnresetViews) ) return; |
2185 for(i=sqliteHashFirst(&db->aDb[idx].pSchema->tblHash); i;i=sqliteHashNext(i)){ | 2266 for(i=sqliteHashFirst(&db->aDb[idx].pSchema->tblHash); i;i=sqliteHashNext(i)){ |
2186 Table *pTab = sqliteHashData(i); | 2267 Table *pTab = sqliteHashData(i); |
2187 if( pTab->pSelect ){ | 2268 if( pTab->pSelect ){ |
2188 sqliteDeleteColumnNames(db, pTab); | 2269 sqlite3DeleteColumnNames(db, pTab); |
2189 pTab->aCol = 0; | 2270 pTab->aCol = 0; |
2190 pTab->nCol = 0; | 2271 pTab->nCol = 0; |
2191 } | 2272 } |
2192 } | 2273 } |
2193 DbClearProperty(db, idx, DB_UnresetViews); | 2274 DbClearProperty(db, idx, DB_UnresetViews); |
2194 } | 2275 } |
2195 #else | 2276 #else |
2196 # define sqliteViewResetAll(A,B) | 2277 # define sqliteViewResetAll(A,B) |
2197 #endif /* SQLITE_OMIT_VIEW */ | 2278 #endif /* SQLITE_OMIT_VIEW */ |
2198 | 2279 |
(...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2428 Table *pTab; | 2509 Table *pTab; |
2429 Vdbe *v; | 2510 Vdbe *v; |
2430 sqlite3 *db = pParse->db; | 2511 sqlite3 *db = pParse->db; |
2431 int iDb; | 2512 int iDb; |
2432 | 2513 |
2433 if( db->mallocFailed ){ | 2514 if( db->mallocFailed ){ |
2434 goto exit_drop_table; | 2515 goto exit_drop_table; |
2435 } | 2516 } |
2436 assert( pParse->nErr==0 ); | 2517 assert( pParse->nErr==0 ); |
2437 assert( pName->nSrc==1 ); | 2518 assert( pName->nSrc==1 ); |
| 2519 if( sqlite3ReadSchema(pParse) ) goto exit_drop_table; |
2438 if( noErr ) db->suppressErr++; | 2520 if( noErr ) db->suppressErr++; |
2439 pTab = sqlite3LocateTableItem(pParse, isView, &pName->a[0]); | 2521 pTab = sqlite3LocateTableItem(pParse, isView, &pName->a[0]); |
2440 if( noErr ) db->suppressErr--; | 2522 if( noErr ) db->suppressErr--; |
2441 | 2523 |
2442 if( pTab==0 ){ | 2524 if( pTab==0 ){ |
2443 if( noErr ) sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].zDatabase); | 2525 if( noErr ) sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].zDatabase); |
2444 goto exit_drop_table; | 2526 goto exit_drop_table; |
2445 } | 2527 } |
2446 iDb = sqlite3SchemaToIndex(db, pTab->pSchema); | 2528 iDb = sqlite3SchemaToIndex(db, pTab->pSchema); |
2447 assert( iDb>=0 && iDb<db->nDb ); | 2529 assert( iDb>=0 && iDb<db->nDb ); |
(...skipping 284 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2732 sqlite3VdbeJumpHere(v, addr1); | 2814 sqlite3VdbeJumpHere(v, addr1); |
2733 if( memRootPage<0 ) sqlite3VdbeAddOp2(v, OP_Clear, tnum, iDb); | 2815 if( memRootPage<0 ) sqlite3VdbeAddOp2(v, OP_Clear, tnum, iDb); |
2734 sqlite3VdbeAddOp4(v, OP_OpenWrite, iIdx, tnum, iDb, | 2816 sqlite3VdbeAddOp4(v, OP_OpenWrite, iIdx, tnum, iDb, |
2735 (char *)pKey, P4_KEYINFO); | 2817 (char *)pKey, P4_KEYINFO); |
2736 sqlite3VdbeChangeP5(v, OPFLAG_BULKCSR|((memRootPage>=0)?OPFLAG_P2ISREG:0)); | 2818 sqlite3VdbeChangeP5(v, OPFLAG_BULKCSR|((memRootPage>=0)?OPFLAG_P2ISREG:0)); |
2737 | 2819 |
2738 addr1 = sqlite3VdbeAddOp2(v, OP_SorterSort, iSorter, 0); VdbeCoverage(v); | 2820 addr1 = sqlite3VdbeAddOp2(v, OP_SorterSort, iSorter, 0); VdbeCoverage(v); |
2739 assert( pKey!=0 || db->mallocFailed || pParse->nErr ); | 2821 assert( pKey!=0 || db->mallocFailed || pParse->nErr ); |
2740 if( IsUniqueIndex(pIndex) && pKey!=0 ){ | 2822 if( IsUniqueIndex(pIndex) && pKey!=0 ){ |
2741 int j2 = sqlite3VdbeCurrentAddr(v) + 3; | 2823 int j2 = sqlite3VdbeCurrentAddr(v) + 3; |
2742 sqlite3VdbeAddOp2(v, OP_Goto, 0, j2); | 2824 sqlite3VdbeGoto(v, j2); |
2743 addr2 = sqlite3VdbeCurrentAddr(v); | 2825 addr2 = sqlite3VdbeCurrentAddr(v); |
2744 sqlite3VdbeAddOp4Int(v, OP_SorterCompare, iSorter, j2, regRecord, | 2826 sqlite3VdbeAddOp4Int(v, OP_SorterCompare, iSorter, j2, regRecord, |
2745 pIndex->nKeyCol); VdbeCoverage(v); | 2827 pIndex->nKeyCol); VdbeCoverage(v); |
2746 sqlite3UniqueConstraint(pParse, OE_Abort, pIndex); | 2828 sqlite3UniqueConstraint(pParse, OE_Abort, pIndex); |
2747 }else{ | 2829 }else{ |
2748 addr2 = sqlite3VdbeCurrentAddr(v); | 2830 addr2 = sqlite3VdbeCurrentAddr(v); |
2749 } | 2831 } |
2750 sqlite3VdbeAddOp3(v, OP_SorterData, iSorter, regRecord, iIdx); | 2832 sqlite3VdbeAddOp3(v, OP_SorterData, iSorter, regRecord, iIdx); |
2751 sqlite3VdbeAddOp3(v, OP_IdxInsert, iIdx, regRecord, 1); | 2833 sqlite3VdbeAddOp3(v, OP_Last, iIdx, 0, -1); |
| 2834 sqlite3VdbeAddOp3(v, OP_IdxInsert, iIdx, regRecord, 0); |
2752 sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT); | 2835 sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT); |
2753 sqlite3ReleaseTempReg(pParse, regRecord); | 2836 sqlite3ReleaseTempReg(pParse, regRecord); |
2754 sqlite3VdbeAddOp2(v, OP_SorterNext, iSorter, addr2); VdbeCoverage(v); | 2837 sqlite3VdbeAddOp2(v, OP_SorterNext, iSorter, addr2); VdbeCoverage(v); |
2755 sqlite3VdbeJumpHere(v, addr1); | 2838 sqlite3VdbeJumpHere(v, addr1); |
2756 | 2839 |
2757 sqlite3VdbeAddOp1(v, OP_Close, iTab); | 2840 sqlite3VdbeAddOp1(v, OP_Close, iTab); |
2758 sqlite3VdbeAddOp1(v, OP_Close, iIdx); | 2841 sqlite3VdbeAddOp1(v, OP_Close, iIdx); |
2759 sqlite3VdbeAddOp1(v, OP_Close, iSorter); | 2842 sqlite3VdbeAddOp1(v, OP_Close, iSorter); |
2760 } | 2843 } |
2761 | 2844 |
(...skipping 14 matching lines...) Expand all Loading... |
2776 int nByte; /* Bytes of space for Index object + arrays */ | 2859 int nByte; /* Bytes of space for Index object + arrays */ |
2777 | 2860 |
2778 nByte = ROUND8(sizeof(Index)) + /* Index structure */ | 2861 nByte = ROUND8(sizeof(Index)) + /* Index structure */ |
2779 ROUND8(sizeof(char*)*nCol) + /* Index.azColl */ | 2862 ROUND8(sizeof(char*)*nCol) + /* Index.azColl */ |
2780 ROUND8(sizeof(LogEst)*(nCol+1) + /* Index.aiRowLogEst */ | 2863 ROUND8(sizeof(LogEst)*(nCol+1) + /* Index.aiRowLogEst */ |
2781 sizeof(i16)*nCol + /* Index.aiColumn */ | 2864 sizeof(i16)*nCol + /* Index.aiColumn */ |
2782 sizeof(u8)*nCol); /* Index.aSortOrder */ | 2865 sizeof(u8)*nCol); /* Index.aSortOrder */ |
2783 p = sqlite3DbMallocZero(db, nByte + nExtra); | 2866 p = sqlite3DbMallocZero(db, nByte + nExtra); |
2784 if( p ){ | 2867 if( p ){ |
2785 char *pExtra = ((char*)p)+ROUND8(sizeof(Index)); | 2868 char *pExtra = ((char*)p)+ROUND8(sizeof(Index)); |
2786 p->azColl = (char**)pExtra; pExtra += ROUND8(sizeof(char*)*nCol); | 2869 p->azColl = (const char**)pExtra; pExtra += ROUND8(sizeof(char*)*nCol); |
2787 p->aiRowLogEst = (LogEst*)pExtra; pExtra += sizeof(LogEst)*(nCol+1); | 2870 p->aiRowLogEst = (LogEst*)pExtra; pExtra += sizeof(LogEst)*(nCol+1); |
2788 p->aiColumn = (i16*)pExtra; pExtra += sizeof(i16)*nCol; | 2871 p->aiColumn = (i16*)pExtra; pExtra += sizeof(i16)*nCol; |
2789 p->aSortOrder = (u8*)pExtra; | 2872 p->aSortOrder = (u8*)pExtra; |
2790 p->nColumn = nCol; | 2873 p->nColumn = nCol; |
2791 p->nKeyCol = nCol - 1; | 2874 p->nKeyCol = nCol - 1; |
2792 *ppExtra = ((char*)p) + nByte; | 2875 *ppExtra = ((char*)p) + nByte; |
2793 } | 2876 } |
2794 return p; | 2877 return p; |
2795 } | 2878 } |
2796 | 2879 |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2828 char *zName = 0; /* Name of the index */ | 2911 char *zName = 0; /* Name of the index */ |
2829 int nName; /* Number of characters in zName */ | 2912 int nName; /* Number of characters in zName */ |
2830 int i, j; | 2913 int i, j; |
2831 DbFixer sFix; /* For assigning database names to pTable */ | 2914 DbFixer sFix; /* For assigning database names to pTable */ |
2832 int sortOrderMask; /* 1 to honor DESC in index. 0 to ignore. */ | 2915 int sortOrderMask; /* 1 to honor DESC in index. 0 to ignore. */ |
2833 sqlite3 *db = pParse->db; | 2916 sqlite3 *db = pParse->db; |
2834 Db *pDb; /* The specific table containing the indexed database */ | 2917 Db *pDb; /* The specific table containing the indexed database */ |
2835 int iDb; /* Index of the database that is being written */ | 2918 int iDb; /* Index of the database that is being written */ |
2836 Token *pName = 0; /* Unqualified name of the index to create */ | 2919 Token *pName = 0; /* Unqualified name of the index to create */ |
2837 struct ExprList_item *pListItem; /* For looping over pList */ | 2920 struct ExprList_item *pListItem; /* For looping over pList */ |
2838 const Column *pTabCol; /* A column in the table */ | |
2839 int nExtra = 0; /* Space allocated for zExtra[] */ | 2921 int nExtra = 0; /* Space allocated for zExtra[] */ |
2840 int nExtraCol; /* Number of extra columns needed */ | 2922 int nExtraCol; /* Number of extra columns needed */ |
2841 char *zExtra = 0; /* Extra space after the Index object */ | 2923 char *zExtra = 0; /* Extra space after the Index object */ |
2842 Index *pPk = 0; /* PRIMARY KEY index for WITHOUT ROWID tables */ | 2924 Index *pPk = 0; /* PRIMARY KEY index for WITHOUT ROWID tables */ |
2843 | 2925 |
2844 assert( pParse->nErr==0 ); /* Never called with prior errors */ | 2926 if( db->mallocFailed || IN_DECLARE_VTAB || pParse->nErr>0 ){ |
2845 if( db->mallocFailed || IN_DECLARE_VTAB ){ | |
2846 goto exit_create_index; | 2927 goto exit_create_index; |
2847 } | 2928 } |
2848 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){ | 2929 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){ |
2849 goto exit_create_index; | 2930 goto exit_create_index; |
2850 } | 2931 } |
2851 | 2932 |
2852 /* | 2933 /* |
2853 ** Find the table that is to be indexed. Return early if not found. | 2934 ** Find the table that is to be indexed. Return early if not found. |
2854 */ | 2935 */ |
2855 if( pTblName!=0 ){ | 2936 if( pTblName!=0 ){ |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2984 goto exit_create_index; | 3065 goto exit_create_index; |
2985 } | 3066 } |
2986 } | 3067 } |
2987 #endif | 3068 #endif |
2988 | 3069 |
2989 /* If pList==0, it means this routine was called to make a primary | 3070 /* If pList==0, it means this routine was called to make a primary |
2990 ** key out of the last column added to the table under construction. | 3071 ** key out of the last column added to the table under construction. |
2991 ** So create a fake list to simulate this. | 3072 ** So create a fake list to simulate this. |
2992 */ | 3073 */ |
2993 if( pList==0 ){ | 3074 if( pList==0 ){ |
2994 pList = sqlite3ExprListAppend(pParse, 0, 0); | 3075 Token prevCol; |
| 3076 prevCol.z = pTab->aCol[pTab->nCol-1].zName; |
| 3077 prevCol.n = sqlite3Strlen30(prevCol.z); |
| 3078 pList = sqlite3ExprListAppend(pParse, 0, |
| 3079 sqlite3ExprAlloc(db, TK_ID, &prevCol, 0)); |
2995 if( pList==0 ) goto exit_create_index; | 3080 if( pList==0 ) goto exit_create_index; |
2996 pList->a[0].zName = sqlite3DbStrDup(pParse->db, | 3081 assert( pList->nExpr==1 ); |
2997 pTab->aCol[pTab->nCol-1].zName); | 3082 sqlite3ExprListSetSortOrder(pList, sortOrder); |
2998 pList->a[0].sortOrder = (u8)sortOrder; | 3083 }else{ |
| 3084 sqlite3ExprListCheckLength(pParse, pList, "index"); |
2999 } | 3085 } |
3000 | 3086 |
3001 /* Figure out how many bytes of space are required to store explicitly | 3087 /* Figure out how many bytes of space are required to store explicitly |
3002 ** specified collation sequence names. | 3088 ** specified collation sequence names. |
3003 */ | 3089 */ |
3004 for(i=0; i<pList->nExpr; i++){ | 3090 for(i=0; i<pList->nExpr; i++){ |
3005 Expr *pExpr = pList->a[i].pExpr; | 3091 Expr *pExpr = pList->a[i].pExpr; |
3006 if( pExpr ){ | 3092 assert( pExpr!=0 ); |
3007 assert( pExpr->op==TK_COLLATE ); | 3093 if( pExpr->op==TK_COLLATE ){ |
3008 nExtra += (1 + sqlite3Strlen30(pExpr->u.zToken)); | 3094 nExtra += (1 + sqlite3Strlen30(pExpr->u.zToken)); |
3009 } | 3095 } |
3010 } | 3096 } |
3011 | 3097 |
3012 /* | 3098 /* |
3013 ** Allocate the index structure. | 3099 ** Allocate the index structure. |
3014 */ | 3100 */ |
3015 nName = sqlite3Strlen30(zName); | 3101 nName = sqlite3Strlen30(zName); |
3016 nExtraCol = pPk ? pPk->nKeyCol : 1; | 3102 nExtraCol = pPk ? pPk->nKeyCol : 1; |
3017 pIndex = sqlite3AllocateIndexObject(db, pList->nExpr + nExtraCol, | 3103 pIndex = sqlite3AllocateIndexObject(db, pList->nExpr + nExtraCol, |
(...skipping 20 matching lines...) Expand all Loading... |
3038 assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); | 3124 assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); |
3039 | 3125 |
3040 /* Check to see if we should honor DESC requests on index columns | 3126 /* Check to see if we should honor DESC requests on index columns |
3041 */ | 3127 */ |
3042 if( pDb->pSchema->file_format>=4 ){ | 3128 if( pDb->pSchema->file_format>=4 ){ |
3043 sortOrderMask = -1; /* Honor DESC */ | 3129 sortOrderMask = -1; /* Honor DESC */ |
3044 }else{ | 3130 }else{ |
3045 sortOrderMask = 0; /* Ignore DESC */ | 3131 sortOrderMask = 0; /* Ignore DESC */ |
3046 } | 3132 } |
3047 | 3133 |
3048 /* Scan the names of the columns of the table to be indexed and | 3134 /* Analyze the list of expressions that form the terms of the index and |
3049 ** load the column indices into the Index structure. Report an error | 3135 ** report any errors. In the common case where the expression is exactly |
3050 ** if any column is not found. | 3136 ** a table column, store that column in aiColumn[]. For general expressions, |
| 3137 ** populate pIndex->aColExpr and store XN_EXPR (-2) in aiColumn[]. |
3051 ** | 3138 ** |
3052 ** TODO: Add a test to make sure that the same column is not named | 3139 ** TODO: Issue a warning if two or more columns of the index are identical. |
3053 ** more than once within the same index. Only the first instance of | 3140 ** TODO: Issue a warning if the table primary key is used as part of the |
3054 ** the column will ever be used by the optimizer. Note that using the | 3141 ** index key. |
3055 ** same column more than once cannot be an error because that would | |
3056 ** break backwards compatibility - it needs to be a warning. | |
3057 */ | 3142 */ |
3058 for(i=0, pListItem=pList->a; i<pList->nExpr; i++, pListItem++){ | 3143 for(i=0, pListItem=pList->a; i<pList->nExpr; i++, pListItem++){ |
3059 const char *zColName = pListItem->zName; | 3144 Expr *pCExpr; /* The i-th index expression */ |
3060 int requestedSortOrder; | 3145 int requestedSortOrder; /* ASC or DESC on the i-th expression */ |
3061 char *zColl; /* Collation sequence name */ | 3146 const char *zColl; /* Collation sequence name */ |
3062 | 3147 |
3063 for(j=0, pTabCol=pTab->aCol; j<pTab->nCol; j++, pTabCol++){ | 3148 sqlite3StringToId(pListItem->pExpr); |
3064 if( sqlite3StrICmp(zColName, pTabCol->zName)==0 ) break; | 3149 sqlite3ResolveSelfReference(pParse, pTab, NC_IdxExpr, pListItem->pExpr, 0); |
| 3150 if( pParse->nErr ) goto exit_create_index; |
| 3151 pCExpr = sqlite3ExprSkipCollate(pListItem->pExpr); |
| 3152 if( pCExpr->op!=TK_COLUMN ){ |
| 3153 if( pTab==pParse->pNewTable ){ |
| 3154 sqlite3ErrorMsg(pParse, "expressions prohibited in PRIMARY KEY and " |
| 3155 "UNIQUE constraints"); |
| 3156 goto exit_create_index; |
| 3157 } |
| 3158 if( pIndex->aColExpr==0 ){ |
| 3159 ExprList *pCopy = sqlite3ExprListDup(db, pList, 0); |
| 3160 pIndex->aColExpr = pCopy; |
| 3161 if( !db->mallocFailed ){ |
| 3162 assert( pCopy!=0 ); |
| 3163 pListItem = &pCopy->a[i]; |
| 3164 } |
| 3165 } |
| 3166 j = XN_EXPR; |
| 3167 pIndex->aiColumn[i] = XN_EXPR; |
| 3168 pIndex->uniqNotNull = 0; |
| 3169 }else{ |
| 3170 j = pCExpr->iColumn; |
| 3171 assert( j<=0x7fff ); |
| 3172 if( j<0 ){ |
| 3173 j = pTab->iPKey; |
| 3174 }else if( pTab->aCol[j].notNull==0 ){ |
| 3175 pIndex->uniqNotNull = 0; |
| 3176 } |
| 3177 pIndex->aiColumn[i] = (i16)j; |
3065 } | 3178 } |
3066 if( j>=pTab->nCol ){ | 3179 zColl = 0; |
3067 sqlite3ErrorMsg(pParse, "table %s has no column named %s", | 3180 if( pListItem->pExpr->op==TK_COLLATE ){ |
3068 pTab->zName, zColName); | |
3069 pParse->checkSchema = 1; | |
3070 goto exit_create_index; | |
3071 } | |
3072 assert( j<=0x7fff ); | |
3073 pIndex->aiColumn[i] = (i16)j; | |
3074 if( pListItem->pExpr ){ | |
3075 int nColl; | 3181 int nColl; |
3076 assert( pListItem->pExpr->op==TK_COLLATE ); | |
3077 zColl = pListItem->pExpr->u.zToken; | 3182 zColl = pListItem->pExpr->u.zToken; |
3078 nColl = sqlite3Strlen30(zColl) + 1; | 3183 nColl = sqlite3Strlen30(zColl) + 1; |
3079 assert( nExtra>=nColl ); | 3184 assert( nExtra>=nColl ); |
3080 memcpy(zExtra, zColl, nColl); | 3185 memcpy(zExtra, zColl, nColl); |
3081 zColl = zExtra; | 3186 zColl = zExtra; |
3082 zExtra += nColl; | 3187 zExtra += nColl; |
3083 nExtra -= nColl; | 3188 nExtra -= nColl; |
3084 }else{ | 3189 }else if( j>=0 ){ |
3085 zColl = pTab->aCol[j].zColl; | 3190 zColl = pTab->aCol[j].zColl; |
3086 if( !zColl ) zColl = "BINARY"; | |
3087 } | 3191 } |
| 3192 if( !zColl ) zColl = sqlite3StrBINARY; |
3088 if( !db->init.busy && !sqlite3LocateCollSeq(pParse, zColl) ){ | 3193 if( !db->init.busy && !sqlite3LocateCollSeq(pParse, zColl) ){ |
3089 goto exit_create_index; | 3194 goto exit_create_index; |
3090 } | 3195 } |
3091 pIndex->azColl[i] = zColl; | 3196 pIndex->azColl[i] = zColl; |
3092 requestedSortOrder = pListItem->sortOrder & sortOrderMask; | 3197 requestedSortOrder = pListItem->sortOrder & sortOrderMask; |
3093 pIndex->aSortOrder[i] = (u8)requestedSortOrder; | 3198 pIndex->aSortOrder[i] = (u8)requestedSortOrder; |
3094 if( pTab->aCol[j].notNull==0 ) pIndex->uniqNotNull = 0; | |
3095 } | 3199 } |
| 3200 |
| 3201 /* Append the table key to the end of the index. For WITHOUT ROWID |
| 3202 ** tables (when pPk!=0) this will be the declared PRIMARY KEY. For |
| 3203 ** normal tables (when pPk==0) this will be the rowid. |
| 3204 */ |
3096 if( pPk ){ | 3205 if( pPk ){ |
3097 for(j=0; j<pPk->nKeyCol; j++){ | 3206 for(j=0; j<pPk->nKeyCol; j++){ |
3098 int x = pPk->aiColumn[j]; | 3207 int x = pPk->aiColumn[j]; |
| 3208 assert( x>=0 ); |
3099 if( hasColumn(pIndex->aiColumn, pIndex->nKeyCol, x) ){ | 3209 if( hasColumn(pIndex->aiColumn, pIndex->nKeyCol, x) ){ |
3100 pIndex->nColumn--; | 3210 pIndex->nColumn--; |
3101 }else{ | 3211 }else{ |
3102 pIndex->aiColumn[i] = x; | 3212 pIndex->aiColumn[i] = x; |
3103 pIndex->azColl[i] = pPk->azColl[j]; | 3213 pIndex->azColl[i] = pPk->azColl[j]; |
3104 pIndex->aSortOrder[i] = pPk->aSortOrder[j]; | 3214 pIndex->aSortOrder[i] = pPk->aSortOrder[j]; |
3105 i++; | 3215 i++; |
3106 } | 3216 } |
3107 } | 3217 } |
3108 assert( i==pIndex->nColumn ); | 3218 assert( i==pIndex->nColumn ); |
3109 }else{ | 3219 }else{ |
3110 pIndex->aiColumn[i] = -1; | 3220 pIndex->aiColumn[i] = XN_ROWID; |
3111 pIndex->azColl[i] = "BINARY"; | 3221 pIndex->azColl[i] = sqlite3StrBINARY; |
3112 } | 3222 } |
3113 sqlite3DefaultRowEst(pIndex); | 3223 sqlite3DefaultRowEst(pIndex); |
3114 if( pParse->pNewTable==0 ) estimateIndexWidth(pIndex); | 3224 if( pParse->pNewTable==0 ) estimateIndexWidth(pIndex); |
3115 | 3225 |
3116 if( pTab==pParse->pNewTable ){ | 3226 if( pTab==pParse->pNewTable ){ |
3117 /* This routine has been called to create an automatic index as a | 3227 /* This routine has been called to create an automatic index as a |
3118 ** result of a PRIMARY KEY or UNIQUE clause on a column definition, or | 3228 ** result of a PRIMARY KEY or UNIQUE clause on a column definition, or |
3119 ** a PRIMARY KEY or UNIQUE clause following the column definitions. | 3229 ** a PRIMARY KEY or UNIQUE clause following the column definitions. |
3120 ** i.e. one of: | 3230 ** i.e. one of: |
3121 ** | 3231 ** |
(...skipping 17 matching lines...) Expand all Loading... |
3139 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ | 3249 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
3140 int k; | 3250 int k; |
3141 assert( IsUniqueIndex(pIdx) ); | 3251 assert( IsUniqueIndex(pIdx) ); |
3142 assert( pIdx->idxType!=SQLITE_IDXTYPE_APPDEF ); | 3252 assert( pIdx->idxType!=SQLITE_IDXTYPE_APPDEF ); |
3143 assert( IsUniqueIndex(pIndex) ); | 3253 assert( IsUniqueIndex(pIndex) ); |
3144 | 3254 |
3145 if( pIdx->nKeyCol!=pIndex->nKeyCol ) continue; | 3255 if( pIdx->nKeyCol!=pIndex->nKeyCol ) continue; |
3146 for(k=0; k<pIdx->nKeyCol; k++){ | 3256 for(k=0; k<pIdx->nKeyCol; k++){ |
3147 const char *z1; | 3257 const char *z1; |
3148 const char *z2; | 3258 const char *z2; |
| 3259 assert( pIdx->aiColumn[k]>=0 ); |
3149 if( pIdx->aiColumn[k]!=pIndex->aiColumn[k] ) break; | 3260 if( pIdx->aiColumn[k]!=pIndex->aiColumn[k] ) break; |
3150 z1 = pIdx->azColl[k]; | 3261 z1 = pIdx->azColl[k]; |
3151 z2 = pIndex->azColl[k]; | 3262 z2 = pIndex->azColl[k]; |
3152 if( z1!=z2 && sqlite3StrICmp(z1, z2) ) break; | 3263 if( z1!=z2 && sqlite3StrICmp(z1, z2) ) break; |
3153 } | 3264 } |
3154 if( k==pIdx->nKeyCol ){ | 3265 if( k==pIdx->nKeyCol ){ |
3155 if( pIdx->onError!=pIndex->onError ){ | 3266 if( pIdx->onError!=pIndex->onError ){ |
3156 /* This constraint creates the same index as a previous | 3267 /* This constraint creates the same index as a previous |
3157 ** constraint specified somewhere in the CREATE TABLE statement. | 3268 ** constraint specified somewhere in the CREATE TABLE statement. |
3158 ** However the ON CONFLICT clauses are different. If both this | 3269 ** However the ON CONFLICT clauses are different. If both this |
3159 ** constraint and the previous equivalent constraint have explicit | 3270 ** constraint and the previous equivalent constraint have explicit |
3160 ** ON CONFLICT clauses this is an error. Otherwise, use the | 3271 ** ON CONFLICT clauses this is an error. Otherwise, use the |
3161 ** explicitly specified behavior for the index. | 3272 ** explicitly specified behavior for the index. |
3162 */ | 3273 */ |
3163 if( !(pIdx->onError==OE_Default || pIndex->onError==OE_Default) ){ | 3274 if( !(pIdx->onError==OE_Default || pIndex->onError==OE_Default) ){ |
3164 sqlite3ErrorMsg(pParse, | 3275 sqlite3ErrorMsg(pParse, |
3165 "conflicting ON CONFLICT clauses specified", 0); | 3276 "conflicting ON CONFLICT clauses specified", 0); |
3166 } | 3277 } |
3167 if( pIdx->onError==OE_Default ){ | 3278 if( pIdx->onError==OE_Default ){ |
3168 pIdx->onError = pIndex->onError; | 3279 pIdx->onError = pIndex->onError; |
3169 } | 3280 } |
3170 } | 3281 } |
| 3282 pRet = pIdx; |
3171 goto exit_create_index; | 3283 goto exit_create_index; |
3172 } | 3284 } |
3173 } | 3285 } |
3174 } | 3286 } |
3175 | 3287 |
3176 /* Link the new Index structure to its table and to the other | 3288 /* Link the new Index structure to its table and to the other |
3177 ** in-memory database structures. | 3289 ** in-memory database structures. |
3178 */ | 3290 */ |
| 3291 assert( pParse->nErr==0 ); |
3179 if( db->init.busy ){ | 3292 if( db->init.busy ){ |
3180 Index *p; | 3293 Index *p; |
3181 assert( sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) ); | 3294 assert( sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) ); |
3182 p = sqlite3HashInsert(&pIndex->pSchema->idxHash, | 3295 p = sqlite3HashInsert(&pIndex->pSchema->idxHash, |
3183 pIndex->zName, pIndex); | 3296 pIndex->zName, pIndex); |
3184 if( p ){ | 3297 if( p ){ |
3185 assert( p==pIndex ); /* Malloc must have failed */ | 3298 assert( p==pIndex ); /* Malloc must have failed */ |
3186 db->mallocFailed = 1; | 3299 db->mallocFailed = 1; |
3187 goto exit_create_index; | 3300 goto exit_create_index; |
3188 } | 3301 } |
3189 db->flags |= SQLITE_InternChanges; | 3302 db->flags |= SQLITE_InternChanges; |
3190 if( pTblName!=0 ){ | 3303 if( pTblName!=0 ){ |
3191 pIndex->tnum = db->init.newTnum; | 3304 pIndex->tnum = db->init.newTnum; |
3192 } | 3305 } |
3193 } | 3306 } |
3194 | 3307 |
3195 /* If this is the initial CREATE INDEX statement (or CREATE TABLE if the | 3308 /* If this is the initial CREATE INDEX statement (or CREATE TABLE if the |
3196 ** index is an implied index for a UNIQUE or PRIMARY KEY constraint) then | 3309 ** index is an implied index for a UNIQUE or PRIMARY KEY constraint) then |
3197 ** emit code to allocate the index rootpage on disk and make an entry for | 3310 ** emit code to allocate the index rootpage on disk and make an entry for |
3198 ** the index in the sqlite_master table and populate the index with | 3311 ** the index in the sqlite_master table and populate the index with |
3199 ** content. But, do not do this if we are simply reading the sqlite_master | 3312 ** content. But, do not do this if we are simply reading the sqlite_master |
3200 ** table to parse the schema, or if this index is the PRIMARY KEY index | 3313 ** table to parse the schema, or if this index is the PRIMARY KEY index |
3201 ** of a WITHOUT ROWID table. | 3314 ** of a WITHOUT ROWID table. |
3202 ** | 3315 ** |
3203 ** If pTblName==0 it means this index is generated as an implied PRIMARY KEY | 3316 ** If pTblName==0 it means this index is generated as an implied PRIMARY KEY |
3204 ** or UNIQUE index in a CREATE TABLE statement. Since the table | 3317 ** or UNIQUE index in a CREATE TABLE statement. Since the table |
3205 ** has just been created, it contains no data and the index initialization | 3318 ** has just been created, it contains no data and the index initialization |
3206 ** step can be skipped. | 3319 ** step can be skipped. |
3207 */ | 3320 */ |
3208 else if( pParse->nErr==0 && (HasRowid(pTab) || pTblName!=0) ){ | 3321 else if( HasRowid(pTab) || pTblName!=0 ){ |
3209 Vdbe *v; | 3322 Vdbe *v; |
3210 char *zStmt; | 3323 char *zStmt; |
3211 int iMem = ++pParse->nMem; | 3324 int iMem = ++pParse->nMem; |
3212 | 3325 |
3213 v = sqlite3GetVdbe(pParse); | 3326 v = sqlite3GetVdbe(pParse); |
3214 if( v==0 ) goto exit_create_index; | 3327 if( v==0 ) goto exit_create_index; |
3215 | 3328 |
| 3329 sqlite3BeginWriteOperation(pParse, 1, iDb); |
3216 | 3330 |
3217 /* Create the rootpage for the index | 3331 /* Create the rootpage for the index using CreateIndex. But before |
3218 */ | 3332 ** doing so, code a Noop instruction and store its address in |
3219 sqlite3BeginWriteOperation(pParse, 1, iDb); | 3333 ** Index.tnum. This is required in case this index is actually a |
| 3334 ** PRIMARY KEY and the table is actually a WITHOUT ROWID table. In |
| 3335 ** that case the convertToWithoutRowidTable() routine will replace |
| 3336 ** the Noop with a Goto to jump over the VDBE code generated below. */ |
| 3337 pIndex->tnum = sqlite3VdbeAddOp0(v, OP_Noop); |
3220 sqlite3VdbeAddOp2(v, OP_CreateIndex, iDb, iMem); | 3338 sqlite3VdbeAddOp2(v, OP_CreateIndex, iDb, iMem); |
3221 | 3339 |
3222 /* Gather the complete text of the CREATE INDEX statement into | 3340 /* Gather the complete text of the CREATE INDEX statement into |
3223 ** the zStmt variable | 3341 ** the zStmt variable |
3224 */ | 3342 */ |
3225 if( pStart ){ | 3343 if( pStart ){ |
3226 int n = (int)(pParse->sLastToken.z - pName->z) + pParse->sLastToken.n; | 3344 int n = (int)(pParse->sLastToken.z - pName->z) + pParse->sLastToken.n; |
3227 if( pName->z[n-1]==';' ) n--; | 3345 if( pName->z[n-1]==';' ) n--; |
3228 /* A named index with an explicit CREATE INDEX statement */ | 3346 /* A named index with an explicit CREATE INDEX statement */ |
3229 zStmt = sqlite3MPrintf(db, "CREATE%s INDEX %.*s", | 3347 zStmt = sqlite3MPrintf(db, "CREATE%s INDEX %.*s", |
(...skipping 19 matching lines...) Expand all Loading... |
3249 /* Fill the index with data and reparse the schema. Code an OP_Expire | 3367 /* Fill the index with data and reparse the schema. Code an OP_Expire |
3250 ** to invalidate all pre-compiled statements. | 3368 ** to invalidate all pre-compiled statements. |
3251 */ | 3369 */ |
3252 if( pTblName ){ | 3370 if( pTblName ){ |
3253 sqlite3RefillIndex(pParse, pIndex, iMem); | 3371 sqlite3RefillIndex(pParse, pIndex, iMem); |
3254 sqlite3ChangeCookie(pParse, iDb); | 3372 sqlite3ChangeCookie(pParse, iDb); |
3255 sqlite3VdbeAddParseSchemaOp(v, iDb, | 3373 sqlite3VdbeAddParseSchemaOp(v, iDb, |
3256 sqlite3MPrintf(db, "name='%q' AND type='index'", pIndex->zName)); | 3374 sqlite3MPrintf(db, "name='%q' AND type='index'", pIndex->zName)); |
3257 sqlite3VdbeAddOp1(v, OP_Expire, 0); | 3375 sqlite3VdbeAddOp1(v, OP_Expire, 0); |
3258 } | 3376 } |
| 3377 |
| 3378 sqlite3VdbeJumpHere(v, pIndex->tnum); |
3259 } | 3379 } |
3260 | 3380 |
3261 /* When adding an index to the list of indices for a table, make | 3381 /* When adding an index to the list of indices for a table, make |
3262 ** sure all indices labeled OE_Replace come after all those labeled | 3382 ** sure all indices labeled OE_Replace come after all those labeled |
3263 ** OE_Ignore. This is necessary for the correct constraint check | 3383 ** OE_Ignore. This is necessary for the correct constraint check |
3264 ** processing (in sqlite3GenerateConstraintChecks()) as part of | 3384 ** processing (in sqlite3GenerateConstraintChecks()) as part of |
3265 ** UPDATE and INSERT statements. | 3385 ** UPDATE and INSERT statements. |
3266 */ | 3386 */ |
3267 if( db->init.busy || pTblName==0 ){ | 3387 if( db->init.busy || pTblName==0 ){ |
3268 if( onError!=OE_Replace || pTab->pIndex==0 | 3388 if( onError!=OE_Replace || pTab->pIndex==0 |
(...skipping 382 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3651 ** Delete an entire SrcList including all its substructure. | 3771 ** Delete an entire SrcList including all its substructure. |
3652 */ | 3772 */ |
3653 void sqlite3SrcListDelete(sqlite3 *db, SrcList *pList){ | 3773 void sqlite3SrcListDelete(sqlite3 *db, SrcList *pList){ |
3654 int i; | 3774 int i; |
3655 struct SrcList_item *pItem; | 3775 struct SrcList_item *pItem; |
3656 if( pList==0 ) return; | 3776 if( pList==0 ) return; |
3657 for(pItem=pList->a, i=0; i<pList->nSrc; i++, pItem++){ | 3777 for(pItem=pList->a, i=0; i<pList->nSrc; i++, pItem++){ |
3658 sqlite3DbFree(db, pItem->zDatabase); | 3778 sqlite3DbFree(db, pItem->zDatabase); |
3659 sqlite3DbFree(db, pItem->zName); | 3779 sqlite3DbFree(db, pItem->zName); |
3660 sqlite3DbFree(db, pItem->zAlias); | 3780 sqlite3DbFree(db, pItem->zAlias); |
3661 sqlite3DbFree(db, pItem->zIndex); | 3781 if( pItem->fg.isIndexedBy ) sqlite3DbFree(db, pItem->u1.zIndexedBy); |
| 3782 if( pItem->fg.isTabFunc ) sqlite3ExprListDelete(db, pItem->u1.pFuncArg); |
3662 sqlite3DeleteTable(db, pItem->pTab); | 3783 sqlite3DeleteTable(db, pItem->pTab); |
3663 sqlite3SelectDelete(db, pItem->pSelect); | 3784 sqlite3SelectDelete(db, pItem->pSelect); |
3664 sqlite3ExprDelete(db, pItem->pOn); | 3785 sqlite3ExprDelete(db, pItem->pOn); |
3665 sqlite3IdListDelete(db, pItem->pUsing); | 3786 sqlite3IdListDelete(db, pItem->pUsing); |
3666 } | 3787 } |
3667 sqlite3DbFree(db, pList); | 3788 sqlite3DbFree(db, pList); |
3668 } | 3789 } |
3669 | 3790 |
3670 /* | 3791 /* |
3671 ** This routine is called by the parser to add a new term to the | 3792 ** This routine is called by the parser to add a new term to the |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3724 } | 3845 } |
3725 | 3846 |
3726 /* | 3847 /* |
3727 ** Add an INDEXED BY or NOT INDEXED clause to the most recently added | 3848 ** Add an INDEXED BY or NOT INDEXED clause to the most recently added |
3728 ** element of the source-list passed as the second argument. | 3849 ** element of the source-list passed as the second argument. |
3729 */ | 3850 */ |
3730 void sqlite3SrcListIndexedBy(Parse *pParse, SrcList *p, Token *pIndexedBy){ | 3851 void sqlite3SrcListIndexedBy(Parse *pParse, SrcList *p, Token *pIndexedBy){ |
3731 assert( pIndexedBy!=0 ); | 3852 assert( pIndexedBy!=0 ); |
3732 if( p && ALWAYS(p->nSrc>0) ){ | 3853 if( p && ALWAYS(p->nSrc>0) ){ |
3733 struct SrcList_item *pItem = &p->a[p->nSrc-1]; | 3854 struct SrcList_item *pItem = &p->a[p->nSrc-1]; |
3734 assert( pItem->notIndexed==0 && pItem->zIndex==0 ); | 3855 assert( pItem->fg.notIndexed==0 ); |
| 3856 assert( pItem->fg.isIndexedBy==0 ); |
| 3857 assert( pItem->fg.isTabFunc==0 ); |
3735 if( pIndexedBy->n==1 && !pIndexedBy->z ){ | 3858 if( pIndexedBy->n==1 && !pIndexedBy->z ){ |
3736 /* A "NOT INDEXED" clause was supplied. See parse.y | 3859 /* A "NOT INDEXED" clause was supplied. See parse.y |
3737 ** construct "indexed_opt" for details. */ | 3860 ** construct "indexed_opt" for details. */ |
3738 pItem->notIndexed = 1; | 3861 pItem->fg.notIndexed = 1; |
3739 }else{ | 3862 }else{ |
3740 pItem->zIndex = sqlite3NameFromToken(pParse->db, pIndexedBy); | 3863 pItem->u1.zIndexedBy = sqlite3NameFromToken(pParse->db, pIndexedBy); |
| 3864 pItem->fg.isIndexedBy = (pItem->u1.zIndexedBy!=0); |
3741 } | 3865 } |
3742 } | 3866 } |
3743 } | 3867 } |
3744 | 3868 |
3745 /* | 3869 /* |
| 3870 ** Add the list of function arguments to the SrcList entry for a |
| 3871 ** table-valued-function. |
| 3872 */ |
| 3873 void sqlite3SrcListFuncArgs(Parse *pParse, SrcList *p, ExprList *pList){ |
| 3874 if( p ){ |
| 3875 struct SrcList_item *pItem = &p->a[p->nSrc-1]; |
| 3876 assert( pItem->fg.notIndexed==0 ); |
| 3877 assert( pItem->fg.isIndexedBy==0 ); |
| 3878 assert( pItem->fg.isTabFunc==0 ); |
| 3879 pItem->u1.pFuncArg = pList; |
| 3880 pItem->fg.isTabFunc = 1; |
| 3881 }else{ |
| 3882 sqlite3ExprListDelete(pParse->db, pList); |
| 3883 } |
| 3884 } |
| 3885 |
| 3886 /* |
3746 ** When building up a FROM clause in the parser, the join operator | 3887 ** When building up a FROM clause in the parser, the join operator |
3747 ** is initially attached to the left operand. But the code generator | 3888 ** is initially attached to the left operand. But the code generator |
3748 ** expects the join operator to be on the right operand. This routine | 3889 ** expects the join operator to be on the right operand. This routine |
3749 ** Shifts all join operators from left to right for an entire FROM | 3890 ** Shifts all join operators from left to right for an entire FROM |
3750 ** clause. | 3891 ** clause. |
3751 ** | 3892 ** |
3752 ** Example: Suppose the join is like this: | 3893 ** Example: Suppose the join is like this: |
3753 ** | 3894 ** |
3754 ** A natural cross join B | 3895 ** A natural cross join B |
3755 ** | 3896 ** |
3756 ** The operator is "natural cross join". The A and B operands are stored | 3897 ** The operator is "natural cross join". The A and B operands are stored |
3757 ** in p->a[0] and p->a[1], respectively. The parser initially stores the | 3898 ** in p->a[0] and p->a[1], respectively. The parser initially stores the |
3758 ** operator with A. This routine shifts that operator over to B. | 3899 ** operator with A. This routine shifts that operator over to B. |
3759 */ | 3900 */ |
3760 void sqlite3SrcListShiftJoinType(SrcList *p){ | 3901 void sqlite3SrcListShiftJoinType(SrcList *p){ |
3761 if( p ){ | 3902 if( p ){ |
3762 int i; | 3903 int i; |
3763 assert( p->a || p->nSrc==0 ); | |
3764 for(i=p->nSrc-1; i>0; i--){ | 3904 for(i=p->nSrc-1; i>0; i--){ |
3765 p->a[i].jointype = p->a[i-1].jointype; | 3905 p->a[i].fg.jointype = p->a[i-1].fg.jointype; |
3766 } | 3906 } |
3767 p->a[0].jointype = 0; | 3907 p->a[0].fg.jointype = 0; |
3768 } | 3908 } |
3769 } | 3909 } |
3770 | 3910 |
3771 /* | 3911 /* |
3772 ** Begin a transaction | 3912 ** Begin a transaction |
3773 */ | 3913 */ |
3774 void sqlite3BeginTransaction(Parse *pParse, int type){ | 3914 void sqlite3BeginTransaction(Parse *pParse, int type){ |
3775 sqlite3 *db; | 3915 sqlite3 *db; |
3776 Vdbe *v; | 3916 Vdbe *v; |
3777 int i; | 3917 int i; |
(...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4000 void sqlite3UniqueConstraint( | 4140 void sqlite3UniqueConstraint( |
4001 Parse *pParse, /* Parsing context */ | 4141 Parse *pParse, /* Parsing context */ |
4002 int onError, /* Constraint type */ | 4142 int onError, /* Constraint type */ |
4003 Index *pIdx /* The index that triggers the constraint */ | 4143 Index *pIdx /* The index that triggers the constraint */ |
4004 ){ | 4144 ){ |
4005 char *zErr; | 4145 char *zErr; |
4006 int j; | 4146 int j; |
4007 StrAccum errMsg; | 4147 StrAccum errMsg; |
4008 Table *pTab = pIdx->pTable; | 4148 Table *pTab = pIdx->pTable; |
4009 | 4149 |
4010 sqlite3StrAccumInit(&errMsg, 0, 0, 200); | 4150 sqlite3StrAccumInit(&errMsg, pParse->db, 0, 0, 200); |
4011 errMsg.db = pParse->db; | 4151 if( pIdx->aColExpr ){ |
4012 for(j=0; j<pIdx->nKeyCol; j++){ | 4152 sqlite3XPrintf(&errMsg, 0, "index '%q'", pIdx->zName); |
4013 char *zCol = pTab->aCol[pIdx->aiColumn[j]].zName; | 4153 }else{ |
4014 if( j ) sqlite3StrAccumAppend(&errMsg, ", ", 2); | 4154 for(j=0; j<pIdx->nKeyCol; j++){ |
4015 sqlite3StrAccumAppendAll(&errMsg, pTab->zName); | 4155 char *zCol; |
4016 sqlite3StrAccumAppend(&errMsg, ".", 1); | 4156 assert( pIdx->aiColumn[j]>=0 ); |
4017 sqlite3StrAccumAppendAll(&errMsg, zCol); | 4157 zCol = pTab->aCol[pIdx->aiColumn[j]].zName; |
| 4158 if( j ) sqlite3StrAccumAppend(&errMsg, ", ", 2); |
| 4159 sqlite3XPrintf(&errMsg, 0, "%s.%s", pTab->zName, zCol); |
| 4160 } |
4018 } | 4161 } |
4019 zErr = sqlite3StrAccumFinish(&errMsg); | 4162 zErr = sqlite3StrAccumFinish(&errMsg); |
4020 sqlite3HaltConstraint(pParse, | 4163 sqlite3HaltConstraint(pParse, |
4021 IsPrimaryKeyIndex(pIdx) ? SQLITE_CONSTRAINT_PRIMARYKEY | 4164 IsPrimaryKeyIndex(pIdx) ? SQLITE_CONSTRAINT_PRIMARYKEY |
4022 : SQLITE_CONSTRAINT_UNIQUE, | 4165 : SQLITE_CONSTRAINT_UNIQUE, |
4023 onError, zErr, P4_DYNAMIC, P5_ConstraintUnique); | 4166 onError, zErr, P4_DYNAMIC, P5_ConstraintUnique); |
4024 } | 4167 } |
4025 | 4168 |
4026 | 4169 |
4027 /* | 4170 /* |
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4179 ** Return a KeyInfo structure that is appropriate for the given Index. | 4322 ** Return a KeyInfo structure that is appropriate for the given Index. |
4180 ** | 4323 ** |
4181 ** The KeyInfo structure for an index is cached in the Index object. | 4324 ** The KeyInfo structure for an index is cached in the Index object. |
4182 ** So there might be multiple references to the returned pointer. The | 4325 ** So there might be multiple references to the returned pointer. The |
4183 ** caller should not try to modify the KeyInfo object. | 4326 ** caller should not try to modify the KeyInfo object. |
4184 ** | 4327 ** |
4185 ** The caller should invoke sqlite3KeyInfoUnref() on the returned object | 4328 ** The caller should invoke sqlite3KeyInfoUnref() on the returned object |
4186 ** when it has finished using it. | 4329 ** when it has finished using it. |
4187 */ | 4330 */ |
4188 KeyInfo *sqlite3KeyInfoOfIndex(Parse *pParse, Index *pIdx){ | 4331 KeyInfo *sqlite3KeyInfoOfIndex(Parse *pParse, Index *pIdx){ |
| 4332 int i; |
| 4333 int nCol = pIdx->nColumn; |
| 4334 int nKey = pIdx->nKeyCol; |
| 4335 KeyInfo *pKey; |
4189 if( pParse->nErr ) return 0; | 4336 if( pParse->nErr ) return 0; |
4190 #ifndef SQLITE_OMIT_SHARED_CACHE | 4337 if( pIdx->uniqNotNull ){ |
4191 if( pIdx->pKeyInfo && pIdx->pKeyInfo->db!=pParse->db ){ | 4338 pKey = sqlite3KeyInfoAlloc(pParse->db, nKey, nCol-nKey); |
4192 sqlite3KeyInfoUnref(pIdx->pKeyInfo); | 4339 }else{ |
4193 pIdx->pKeyInfo = 0; | 4340 pKey = sqlite3KeyInfoAlloc(pParse->db, nCol, 0); |
4194 } | 4341 } |
4195 #endif | 4342 if( pKey ){ |
4196 if( pIdx->pKeyInfo==0 ){ | 4343 assert( sqlite3KeyInfoIsWriteable(pKey) ); |
4197 int i; | 4344 for(i=0; i<nCol; i++){ |
4198 int nCol = pIdx->nColumn; | 4345 const char *zColl = pIdx->azColl[i]; |
4199 int nKey = pIdx->nKeyCol; | 4346 pKey->aColl[i] = zColl==sqlite3StrBINARY ? 0 : |
4200 KeyInfo *pKey; | 4347 sqlite3LocateCollSeq(pParse, zColl); |
4201 if( pIdx->uniqNotNull ){ | 4348 pKey->aSortOrder[i] = pIdx->aSortOrder[i]; |
4202 pKey = sqlite3KeyInfoAlloc(pParse->db, nKey, nCol-nKey); | |
4203 }else{ | |
4204 pKey = sqlite3KeyInfoAlloc(pParse->db, nCol, 0); | |
4205 } | 4349 } |
4206 if( pKey ){ | 4350 if( pParse->nErr ){ |
4207 assert( sqlite3KeyInfoIsWriteable(pKey) ); | 4351 sqlite3KeyInfoUnref(pKey); |
4208 for(i=0; i<nCol; i++){ | 4352 pKey = 0; |
4209 char *zColl = pIdx->azColl[i]; | |
4210 assert( zColl!=0 ); | |
4211 pKey->aColl[i] = strcmp(zColl,"BINARY")==0 ? 0 : | |
4212 sqlite3LocateCollSeq(pParse, zColl); | |
4213 pKey->aSortOrder[i] = pIdx->aSortOrder[i]; | |
4214 } | |
4215 if( pParse->nErr ){ | |
4216 sqlite3KeyInfoUnref(pKey); | |
4217 }else{ | |
4218 pIdx->pKeyInfo = pKey; | |
4219 } | |
4220 } | 4353 } |
4221 } | 4354 } |
4222 return sqlite3KeyInfoRef(pIdx->pKeyInfo); | 4355 return pKey; |
4223 } | 4356 } |
4224 | 4357 |
4225 #ifndef SQLITE_OMIT_CTE | 4358 #ifndef SQLITE_OMIT_CTE |
4226 /* | 4359 /* |
4227 ** This routine is invoked once per CTE by the parser while parsing a | 4360 ** This routine is invoked once per CTE by the parser while parsing a |
4228 ** WITH clause. | 4361 ** WITH clause. |
4229 */ | 4362 */ |
4230 With *sqlite3WithAdd( | 4363 With *sqlite3WithAdd( |
4231 Parse *pParse, /* Parsing context */ | 4364 Parse *pParse, /* Parsing context */ |
4232 With *pWith, /* Existing WITH clause, or NULL */ | 4365 With *pWith, /* Existing WITH clause, or NULL */ |
(...skipping 28 matching lines...) Expand all Loading... |
4261 | 4394 |
4262 if( pNew==0 ){ | 4395 if( pNew==0 ){ |
4263 sqlite3ExprListDelete(db, pArglist); | 4396 sqlite3ExprListDelete(db, pArglist); |
4264 sqlite3SelectDelete(db, pQuery); | 4397 sqlite3SelectDelete(db, pQuery); |
4265 sqlite3DbFree(db, zName); | 4398 sqlite3DbFree(db, zName); |
4266 pNew = pWith; | 4399 pNew = pWith; |
4267 }else{ | 4400 }else{ |
4268 pNew->a[pNew->nCte].pSelect = pQuery; | 4401 pNew->a[pNew->nCte].pSelect = pQuery; |
4269 pNew->a[pNew->nCte].pCols = pArglist; | 4402 pNew->a[pNew->nCte].pCols = pArglist; |
4270 pNew->a[pNew->nCte].zName = zName; | 4403 pNew->a[pNew->nCte].zName = zName; |
4271 pNew->a[pNew->nCte].zErr = 0; | 4404 pNew->a[pNew->nCte].zCteErr = 0; |
4272 pNew->nCte++; | 4405 pNew->nCte++; |
4273 } | 4406 } |
4274 | 4407 |
4275 return pNew; | 4408 return pNew; |
4276 } | 4409 } |
4277 | 4410 |
4278 /* | 4411 /* |
4279 ** Free the contents of the With object passed as the second argument. | 4412 ** Free the contents of the With object passed as the second argument. |
4280 */ | 4413 */ |
4281 void sqlite3WithDelete(sqlite3 *db, With *pWith){ | 4414 void sqlite3WithDelete(sqlite3 *db, With *pWith){ |
4282 if( pWith ){ | 4415 if( pWith ){ |
4283 int i; | 4416 int i; |
4284 for(i=0; i<pWith->nCte; i++){ | 4417 for(i=0; i<pWith->nCte; i++){ |
4285 struct Cte *pCte = &pWith->a[i]; | 4418 struct Cte *pCte = &pWith->a[i]; |
4286 sqlite3ExprListDelete(db, pCte->pCols); | 4419 sqlite3ExprListDelete(db, pCte->pCols); |
4287 sqlite3SelectDelete(db, pCte->pSelect); | 4420 sqlite3SelectDelete(db, pCte->pSelect); |
4288 sqlite3DbFree(db, pCte->zName); | 4421 sqlite3DbFree(db, pCte->zName); |
4289 } | 4422 } |
4290 sqlite3DbFree(db, pWith); | 4423 sqlite3DbFree(db, pWith); |
4291 } | 4424 } |
4292 } | 4425 } |
4293 #endif /* !defined(SQLITE_OMIT_CTE) */ | 4426 #endif /* !defined(SQLITE_OMIT_CTE) */ |
OLD | NEW |