00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include "sqliteInt.h"
00029 #include <ctype.h>
00030
00031
00032
00033
00034
00035
00036
00037 void sqliteBeginParse(Parse *pParse, int explainFlag){
00038 sqlite *db = pParse->db;
00039 int i;
00040 pParse->explain = explainFlag;
00041 if((db->flags & SQLITE_Initialized)==0 && db->init.busy==0 ){
00042 int rc = sqliteInit(db, &pParse->zErrMsg);
00043 if( rc!=SQLITE_OK ){
00044 pParse->rc = rc;
00045 pParse->nErr++;
00046 }
00047 }
00048 for(i=0; i<db->nDb; i++){
00049 DbClearProperty(db, i, DB_Locked);
00050 if( !db->aDb[i].inTrans ){
00051 DbClearProperty(db, i, DB_Cookie);
00052 }
00053 }
00054 pParse->nVar = 0;
00055 }
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067 void sqliteExec(Parse *pParse){
00068 sqlite *db = pParse->db;
00069 Vdbe *v = pParse->pVdbe;
00070
00071 if( v==0 && (v = sqliteGetVdbe(pParse))!=0 ){
00072 sqliteVdbeAddOp(v, OP_Halt, 0, 0);
00073 }
00074 if( sqlite_malloc_failed ) return;
00075 if( v && pParse->nErr==0 ){
00076 FILE *trace = (db->flags & SQLITE_VdbeTrace)!=0 ? stdout : 0;
00077 sqliteVdbeTrace(v, trace);
00078 sqliteVdbeMakeReady(v, pParse->nVar, pParse->explain);
00079 pParse->rc = pParse->nErr ? SQLITE_ERROR : SQLITE_DONE;
00080 pParse->colNamesSet = 0;
00081 }else if( pParse->rc==SQLITE_OK ){
00082 pParse->rc = SQLITE_ERROR;
00083 }
00084 pParse->nTab = 0;
00085 pParse->nMem = 0;
00086 pParse->nSet = 0;
00087 pParse->nAgg = 0;
00088 pParse->nVar = 0;
00089 }
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105 Table *sqliteFindTable(sqlite *db, const char *zName, const char *zDatabase){
00106 Table *p = 0;
00107 int i;
00108 for(i=0; i<db->nDb; i++){
00109 int j = (i<2) ? i^1 : i;
00110 if( zDatabase!=0 && sqliteStrICmp(zDatabase, db->aDb[j].zName) ) continue;
00111 p = sqliteHashFind(&db->aDb[j].tblHash, zName, strlen(zName)+1);
00112 if( p ) break;
00113 }
00114 return p;
00115 }
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128 Table *sqliteLocateTable(Parse *pParse, const char *zName, const char *zDbase){
00129 Table *p;
00130
00131 p = sqliteFindTable(pParse->db, zName, zDbase);
00132 if( p==0 ){
00133 if( zDbase ){
00134 sqliteErrorMsg(pParse, "no such table: %s.%s", zDbase, zName);
00135 }else if( sqliteFindTable(pParse->db, zName, 0)!=0 ){
00136 sqliteErrorMsg(pParse, "table \"%s\" is not in database \"%s\"",
00137 zName, zDbase);
00138 }else{
00139 sqliteErrorMsg(pParse, "no such table: %s", zName);
00140 }
00141 }
00142 return p;
00143 }
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157 Index *sqliteFindIndex(sqlite *db, const char *zName, const char *zDb){
00158 Index *p = 0;
00159 int i;
00160 for(i=0; i<db->nDb; i++){
00161 int j = (i<2) ? i^1 : i;
00162 if( zDb && sqliteStrICmp(zDb, db->aDb[j].zName) ) continue;
00163 p = sqliteHashFind(&db->aDb[j].idxHash, zName, strlen(zName)+1);
00164 if( p ) break;
00165 }
00166 return p;
00167 }
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177 static void sqliteDeleteIndex(sqlite *db, Index *p){
00178 Index *pOld;
00179
00180 assert( db!=0 && p->zName!=0 );
00181 pOld = sqliteHashInsert(&db->aDb[p->iDb].idxHash, p->zName,
00182 strlen(p->zName)+1, 0);
00183 if( pOld!=0 && pOld!=p ){
00184 sqliteHashInsert(&db->aDb[p->iDb].idxHash, pOld->zName,
00185 strlen(pOld->zName)+1, pOld);
00186 }
00187 sqliteFree(p);
00188 }
00189
00190
00191
00192
00193
00194
00195 void sqliteUnlinkAndDeleteIndex(sqlite *db, Index *pIndex){
00196 if( pIndex->pTable->pIndex==pIndex ){
00197 pIndex->pTable->pIndex = pIndex->pNext;
00198 }else{
00199 Index *p;
00200 for(p=pIndex->pTable->pIndex; p && p->pNext!=pIndex; p=p->pNext){}
00201 if( p && p->pNext==pIndex ){
00202 p->pNext = pIndex->pNext;
00203 }
00204 }
00205 sqliteDeleteIndex(db, pIndex);
00206 }
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218 void sqliteResetInternalSchema(sqlite *db, int iDb){
00219 HashElem *pElem;
00220 Hash temp1;
00221 Hash temp2;
00222 int i, j;
00223
00224 assert( iDb>=0 && iDb<db->nDb );
00225 db->flags &= ~SQLITE_Initialized;
00226 for(i=iDb; i<db->nDb; i++){
00227 Db *pDb = &db->aDb[i];
00228 temp1 = pDb->tblHash;
00229 temp2 = pDb->trigHash;
00230 sqliteHashInit(&pDb->trigHash, SQLITE_HASH_STRING, 0);
00231 sqliteHashClear(&pDb->aFKey);
00232 sqliteHashClear(&pDb->idxHash);
00233 for(pElem=sqliteHashFirst(&temp2); pElem; pElem=sqliteHashNext(pElem)){
00234 Trigger *pTrigger = sqliteHashData(pElem);
00235 sqliteDeleteTrigger(pTrigger);
00236 }
00237 sqliteHashClear(&temp2);
00238 sqliteHashInit(&pDb->tblHash, SQLITE_HASH_STRING, 0);
00239 for(pElem=sqliteHashFirst(&temp1); pElem; pElem=sqliteHashNext(pElem)){
00240 Table *pTab = sqliteHashData(pElem);
00241 sqliteDeleteTable(db, pTab);
00242 }
00243 sqliteHashClear(&temp1);
00244 DbClearProperty(db, i, DB_SchemaLoaded);
00245 if( iDb>0 ) return;
00246 }
00247 assert( iDb==0 );
00248 db->flags &= ~SQLITE_InternChanges;
00249
00250
00251
00252
00253
00254
00255
00256 for(i=0; i<db->nDb; i++){
00257 struct Db *pDb = &db->aDb[i];
00258 if( pDb->pBt==0 ){
00259 if( pDb->pAux && pDb->xFreeAux ) pDb->xFreeAux(pDb->pAux);
00260 pDb->pAux = 0;
00261 }
00262 }
00263 for(i=j=2; i<db->nDb; i++){
00264 struct Db *pDb = &db->aDb[i];
00265 if( pDb->pBt==0 ){
00266 sqliteFree(pDb->zName);
00267 pDb->zName = 0;
00268 continue;
00269 }
00270 if( j<i ){
00271 db->aDb[j] = db->aDb[i];
00272 }
00273 j++;
00274 }
00275 memset(&db->aDb[j], 0, (db->nDb-j)*sizeof(db->aDb[j]));
00276 db->nDb = j;
00277 if( db->nDb<=2 && db->aDb!=db->aDbStatic ){
00278 memcpy(db->aDbStatic, db->aDb, 2*sizeof(db->aDb[0]));
00279 sqliteFree(db->aDb);
00280 db->aDb = db->aDbStatic;
00281 }
00282 }
00283
00284
00285
00286
00287
00288
00289 void sqliteRollbackInternalChanges(sqlite *db){
00290 if( db->flags & SQLITE_InternChanges ){
00291 sqliteResetInternalSchema(db, 0);
00292 }
00293 }
00294
00295
00296
00297
00298 void sqliteCommitInternalChanges(sqlite *db){
00299 db->aDb[0].schema_cookie = db->next_cookie;
00300 db->flags &= ~SQLITE_InternChanges;
00301 }
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318 void sqliteDeleteTable(sqlite *db, Table *pTable){
00319 int i;
00320 Index *pIndex, *pNext;
00321 FKey *pFKey, *pNextFKey;
00322
00323 if( pTable==0 ) return;
00324
00325
00326
00327 for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){
00328 pNext = pIndex->pNext;
00329 assert( pIndex->iDb==pTable->iDb || (pTable->iDb==0 && pIndex->iDb==1) );
00330 sqliteDeleteIndex(db, pIndex);
00331 }
00332
00333
00334
00335
00336 for(pFKey=pTable->pFKey; pFKey; pFKey=pNextFKey){
00337 pNextFKey = pFKey->pNextFrom;
00338 assert( pTable->iDb<db->nDb );
00339 assert( sqliteHashFind(&db->aDb[pTable->iDb].aFKey,
00340 pFKey->zTo, strlen(pFKey->zTo)+1)!=pFKey );
00341 sqliteFree(pFKey);
00342 }
00343
00344
00345
00346 for(i=0; i<pTable->nCol; i++){
00347 sqliteFree(pTable->aCol[i].zName);
00348 sqliteFree(pTable->aCol[i].zDflt);
00349 sqliteFree(pTable->aCol[i].zType);
00350 }
00351 sqliteFree(pTable->zName);
00352 sqliteFree(pTable->aCol);
00353 sqliteSelectDelete(pTable->pSelect);
00354 sqliteFree(pTable);
00355 }
00356
00357
00358
00359
00360
00361 static void sqliteUnlinkAndDeleteTable(sqlite *db, Table *p){
00362 Table *pOld;
00363 FKey *pF1, *pF2;
00364 int i = p->iDb;
00365 assert( db!=0 );
00366 pOld = sqliteHashInsert(&db->aDb[i].tblHash, p->zName, strlen(p->zName)+1, 0);
00367 assert( pOld==0 || pOld==p );
00368 for(pF1=p->pFKey; pF1; pF1=pF1->pNextFrom){
00369 int nTo = strlen(pF1->zTo) + 1;
00370 pF2 = sqliteHashFind(&db->aDb[i].aFKey, pF1->zTo, nTo);
00371 if( pF2==pF1 ){
00372 sqliteHashInsert(&db->aDb[i].aFKey, pF1->zTo, nTo, pF1->pNextTo);
00373 }else{
00374 while( pF2 && pF2->pNextTo!=pF1 ){ pF2=pF2->pNextTo; }
00375 if( pF2 ){
00376 pF2->pNextTo = pF1->pNextTo;
00377 }
00378 }
00379 }
00380 sqliteDeleteTable(db, p);
00381 }
00382
00383
00384
00385
00386
00387
00388
00389 char *sqliteTableNameFromToken(Token *pName){
00390 char *zName = sqliteStrNDup(pName->z, pName->n);
00391 sqliteDequote(zName);
00392 return zName;
00393 }
00394
00395
00396
00397
00398
00399
00400
00401 void sqliteOpenMasterTable(Vdbe *v, int isTemp){
00402 sqliteVdbeAddOp(v, OP_Integer, isTemp, 0);
00403 sqliteVdbeAddOp(v, OP_OpenWrite, 0, 2);
00404 }
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423 void sqliteStartTable(
00424 Parse *pParse,
00425 Token *pStart,
00426 Token *pName,
00427 int isTemp,
00428 int isView
00429 ){
00430 Table *pTable;
00431 Index *pIdx;
00432 char *zName;
00433 sqlite *db = pParse->db;
00434 Vdbe *v;
00435 int iDb;
00436
00437 pParse->sFirstToken = *pStart;
00438 zName = sqliteTableNameFromToken(pName);
00439 if( zName==0 ) return;
00440 if( db->init.iDb==1 ) isTemp = 1;
00441 #ifndef SQLITE_OMIT_AUTHORIZATION
00442 assert( (isTemp & 1)==isTemp );
00443 {
00444 int code;
00445 char *zDb = isTemp ? "temp" : "main";
00446 if( sqliteAuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(isTemp), 0, zDb) ){
00447 sqliteFree(zName);
00448 return;
00449 }
00450 if( isView ){
00451 if( isTemp ){
00452 code = SQLITE_CREATE_TEMP_VIEW;
00453 }else{
00454 code = SQLITE_CREATE_VIEW;
00455 }
00456 }else{
00457 if( isTemp ){
00458 code = SQLITE_CREATE_TEMP_TABLE;
00459 }else{
00460 code = SQLITE_CREATE_TABLE;
00461 }
00462 }
00463 if( sqliteAuthCheck(pParse, code, zName, 0, zDb) ){
00464 sqliteFree(zName);
00465 return;
00466 }
00467 }
00468 #endif
00469
00470
00471
00472
00473
00474 if( isTemp && db->aDb[1].pBt==0 && !pParse->explain ){
00475 int rc = sqliteBtreeFactory(db, 0, 0, MAX_PAGES, &db->aDb[1].pBt);
00476 if( rc!=SQLITE_OK ){
00477 sqliteErrorMsg(pParse, "unable to open a temporary database "
00478 "file for storing temporary tables");
00479 pParse->nErr++;
00480 return;
00481 }
00482 if( db->flags & SQLITE_InTrans ){
00483 rc = sqliteBtreeBeginTrans(db->aDb[1].pBt);
00484 if( rc!=SQLITE_OK ){
00485 sqliteErrorMsg(pParse, "unable to get a write lock on "
00486 "the temporary database file");
00487 return;
00488 }
00489 }
00490 }
00491
00492
00493
00494
00495
00496
00497
00498
00499 pTable = sqliteFindTable(db, zName, 0);
00500 iDb = isTemp ? 1 : db->init.iDb;
00501 if( pTable!=0 && (pTable->iDb==iDb || !db->init.busy) ){
00502 sqliteErrorMsg(pParse, "table %T already exists", pName);
00503 sqliteFree(zName);
00504 return;
00505 }
00506 if( (pIdx = sqliteFindIndex(db, zName, 0))!=0 &&
00507 (pIdx->iDb==0 || !db->init.busy) ){
00508 sqliteErrorMsg(pParse, "there is already an index named %s", zName);
00509 sqliteFree(zName);
00510 return;
00511 }
00512 pTable = sqliteMalloc( sizeof(Table) );
00513 if( pTable==0 ){
00514 sqliteFree(zName);
00515 return;
00516 }
00517 pTable->zName = zName;
00518 pTable->nCol = 0;
00519 pTable->aCol = 0;
00520 pTable->iPKey = -1;
00521 pTable->pIndex = 0;
00522 pTable->iDb = iDb;
00523 if( pParse->pNewTable ) sqliteDeleteTable(db, pParse->pNewTable);
00524 pParse->pNewTable = pTable;
00525
00526
00527
00528
00529
00530
00531
00532
00533
00534 if( !db->init.busy && (v = sqliteGetVdbe(pParse))!=0 ){
00535 sqliteBeginWriteOperation(pParse, 0, isTemp);
00536 if( !isTemp ){
00537 sqliteVdbeAddOp(v, OP_Integer, db->file_format, 0);
00538 sqliteVdbeAddOp(v, OP_SetCookie, 0, 1);
00539 }
00540 sqliteOpenMasterTable(v, isTemp);
00541 sqliteVdbeAddOp(v, OP_NewRecno, 0, 0);
00542 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
00543 sqliteVdbeAddOp(v, OP_String, 0, 0);
00544 sqliteVdbeAddOp(v, OP_PutIntKey, 0, 0);
00545 }
00546 }
00547
00548
00549
00550
00551
00552
00553
00554
00555
00556 void sqliteAddColumn(Parse *pParse, Token *pName){
00557 Table *p;
00558 int i;
00559 char *z = 0;
00560 Column *pCol;
00561 if( (p = pParse->pNewTable)==0 ) return;
00562 sqliteSetNString(&z, pName->z, pName->n, 0);
00563 if( z==0 ) return;
00564 sqliteDequote(z);
00565 for(i=0; i<p->nCol; i++){
00566 if( sqliteStrICmp(z, p->aCol[i].zName)==0 ){
00567 sqliteErrorMsg(pParse, "duplicate column name: %s", z);
00568 sqliteFree(z);
00569 return;
00570 }
00571 }
00572 if( (p->nCol & 0x7)==0 ){
00573 Column *aNew;
00574 aNew = sqliteRealloc( p->aCol, (p->nCol+8)*sizeof(p->aCol[0]));
00575 if( aNew==0 ) return;
00576 p->aCol = aNew;
00577 }
00578 pCol = &p->aCol[p->nCol];
00579 memset(pCol, 0, sizeof(p->aCol[0]));
00580 pCol->zName = z;
00581 pCol->sortOrder = SQLITE_SO_NUM;
00582 p->nCol++;
00583 }
00584
00585
00586
00587
00588
00589
00590
00591 void sqliteAddNotNull(Parse *pParse, int onError){
00592 Table *p;
00593 int i;
00594 if( (p = pParse->pNewTable)==0 ) return;
00595 i = p->nCol-1;
00596 if( i>=0 ) p->aCol[i].notNull = onError;
00597 }
00598
00599
00600
00601
00602
00603
00604
00605
00606
00607
00608 void sqliteAddColumnType(Parse *pParse, Token *pFirst, Token *pLast){
00609 Table *p;
00610 int i, j;
00611 int n;
00612 char *z, **pz;
00613 Column *pCol;
00614 if( (p = pParse->pNewTable)==0 ) return;
00615 i = p->nCol-1;
00616 if( i<0 ) return;
00617 pCol = &p->aCol[i];
00618 pz = &pCol->zType;
00619 n = pLast->n + Addr(pLast->z) - Addr(pFirst->z);
00620 sqliteSetNString(pz, pFirst->z, n, 0);
00621 z = *pz;
00622 if( z==0 ) return;
00623 for(i=j=0; z[i]; i++){
00624 int c = z[i];
00625 if( isspace(c) ) continue;
00626 z[j++] = c;
00627 }
00628 z[j] = 0;
00629 if( pParse->db->file_format>=4 ){
00630 pCol->sortOrder = sqliteCollateType(z, n);
00631 }else{
00632 pCol->sortOrder = SQLITE_SO_NUM;
00633 }
00634 }
00635
00636
00637
00638
00639
00640
00641
00642
00643
00644 void sqliteAddDefaultValue(Parse *pParse, Token *pVal, int minusFlag){
00645 Table *p;
00646 int i;
00647 char **pz;
00648 if( (p = pParse->pNewTable)==0 ) return;
00649 i = p->nCol-1;
00650 if( i<0 ) return;
00651 pz = &p->aCol[i].zDflt;
00652 if( minusFlag ){
00653 sqliteSetNString(pz, "-", 1, pVal->z, pVal->n, 0);
00654 }else{
00655 sqliteSetNString(pz, pVal->z, pVal->n, 0);
00656 }
00657 sqliteDequote(*pz);
00658 }
00659
00660
00661
00662
00663
00664
00665
00666
00667
00668
00669
00670
00671
00672
00673
00674
00675
00676
00677
00678
00679
00680 void sqliteAddPrimaryKey(Parse *pParse, IdList *pList, int onError){
00681 Table *pTab = pParse->pNewTable;
00682 char *zType = 0;
00683 int iCol = -1, i;
00684 if( pTab==0 ) goto primary_key_exit;
00685 if( pTab->hasPrimKey ){
00686 sqliteErrorMsg(pParse,
00687 "table \"%s\" has more than one primary key", pTab->zName);
00688 goto primary_key_exit;
00689 }
00690 pTab->hasPrimKey = 1;
00691 if( pList==0 ){
00692 iCol = pTab->nCol - 1;
00693 pTab->aCol[iCol].isPrimKey = 1;
00694 }else{
00695 for(i=0; i<pList->nId; i++){
00696 for(iCol=0; iCol<pTab->nCol; iCol++){
00697 if( sqliteStrICmp(pList->a[i].zName, pTab->aCol[iCol].zName)==0 ) break;
00698 }
00699 if( iCol<pTab->nCol ) pTab->aCol[iCol].isPrimKey = 1;
00700 }
00701 if( pList->nId>1 ) iCol = -1;
00702 }
00703 if( iCol>=0 && iCol<pTab->nCol ){
00704 zType = pTab->aCol[iCol].zType;
00705 }
00706 if( pParse->db->file_format>=1 &&
00707 zType && sqliteStrICmp(zType, "INTEGER")==0 ){
00708 pTab->iPKey = iCol;
00709 pTab->keyConf = onError;
00710 }else{
00711 sqliteCreateIndex(pParse, 0, 0, pList, onError, 0, 0);
00712 pList = 0;
00713 }
00714
00715 primary_key_exit:
00716 sqliteIdListDelete(pList);
00717 return;
00718 }
00719
00720
00721
00722
00723
00724
00725
00726
00727
00728 int sqliteCollateType(const char *zType, int nType){
00729 int i;
00730 for(i=0; i<nType-3; i++){
00731 int c = *(zType++) | 0x60;
00732 if( (c=='b' || c=='c') && sqliteStrNICmp(zType, "lob", 3)==0 ){
00733 return SQLITE_SO_TEXT;
00734 }
00735 if( c=='c' && sqliteStrNICmp(zType, "har", 3)==0 ){
00736 return SQLITE_SO_TEXT;
00737 }
00738 if( c=='t' && sqliteStrNICmp(zType, "ext", 3)==0 ){
00739 return SQLITE_SO_TEXT;
00740 }
00741 }
00742 return SQLITE_SO_NUM;
00743 }
00744
00745
00746
00747
00748
00749
00750
00751 void sqliteAddCollateType(Parse *pParse, int collType){
00752 Table *p;
00753 int i;
00754 if( (p = pParse->pNewTable)==0 ) return;
00755 i = p->nCol-1;
00756 if( i>=0 ) p->aCol[i].sortOrder = collType;
00757 }
00758
00759
00760
00761
00762
00763
00764
00765
00766
00767
00768
00769
00770
00771
00772
00773
00774
00775
00776 void sqliteChangeCookie(sqlite *db, Vdbe *v){
00777 if( db->next_cookie==db->aDb[0].schema_cookie ){
00778 unsigned char r;
00779 sqliteRandomness(1, &r);
00780 db->next_cookie = db->aDb[0].schema_cookie + r + 1;
00781 db->flags |= SQLITE_InternChanges;
00782 sqliteVdbeAddOp(v, OP_Integer, db->next_cookie, 0);
00783 sqliteVdbeAddOp(v, OP_SetCookie, 0, 0);
00784 }
00785 }
00786
00787
00788
00789
00790
00791
00792 static int identLength(const char *z){
00793 int n;
00794 int needQuote = 0;
00795 for(n=0; *z; n++, z++){
00796 if( *z=='\'' ){ n++; needQuote=1; }
00797 }
00798 return n + needQuote*2;
00799 }
00800
00801
00802
00803
00804
00805 static void identPut(char *z, int *pIdx, char *zIdent){
00806 int i, j, needQuote;
00807 i = *pIdx;
00808 for(j=0; zIdent[j]; j++){
00809 if( !isalnum(zIdent[j]) && zIdent[j]!='_' ) break;
00810 }
00811 needQuote = zIdent[j]!=0 || isdigit(zIdent[0])
00812 || sqliteKeywordCode(zIdent, j)!=TK_ID;
00813 if( needQuote ) z[i++] = '\'';
00814 for(j=0; zIdent[j]; j++){
00815 z[i++] = zIdent[j];
00816 if( zIdent[j]=='\'' ) z[i++] = '\'';
00817 }
00818 if( needQuote ) z[i++] = '\'';
00819 z[i] = 0;
00820 *pIdx = i;
00821 }
00822
00823
00824
00825
00826
00827
00828 static char *createTableStmt(Table *p){
00829 int i, k, n;
00830 char *zStmt;
00831 char *zSep, *zSep2, *zEnd;
00832 n = 0;
00833 for(i=0; i<p->nCol; i++){
00834 n += identLength(p->aCol[i].zName);
00835 }
00836 n += identLength(p->zName);
00837 if( n<40 ){
00838 zSep = "";
00839 zSep2 = ",";
00840 zEnd = ")";
00841 }else{
00842 zSep = "\n ";
00843 zSep2 = ",\n ";
00844 zEnd = "\n)";
00845 }
00846 n += 35 + 6*p->nCol;
00847 zStmt = sqliteMallocRaw( n );
00848 if( zStmt==0 ) return 0;
00849 strcpy(zStmt, p->iDb==1 ? "CREATE TEMP TABLE " : "CREATE TABLE ");
00850 k = strlen(zStmt);
00851 identPut(zStmt, &k, p->zName);
00852 zStmt[k++] = '(';
00853 for(i=0; i<p->nCol; i++){
00854 strcpy(&zStmt[k], zSep);
00855 k += strlen(&zStmt[k]);
00856 zSep = zSep2;
00857 identPut(zStmt, &k, p->aCol[i].zName);
00858 }
00859 strcpy(&zStmt[k], zEnd);
00860 return zStmt;
00861 }
00862
00863
00864
00865
00866
00867
00868
00869
00870
00871
00872
00873
00874
00875
00876
00877
00878
00879
00880
00881
00882
00883 void sqliteEndTable(Parse *pParse, Token *pEnd, Select *pSelect){
00884 Table *p;
00885 sqlite *db = pParse->db;
00886
00887 if( (pEnd==0 && pSelect==0) || pParse->nErr || sqlite_malloc_failed ) return;
00888 p = pParse->pNewTable;
00889 if( p==0 ) return;
00890
00891
00892
00893
00894 if( pSelect ){
00895 Table *pSelTab = sqliteResultSetOfSelect(pParse, 0, pSelect);
00896 if( pSelTab==0 ) return;
00897 assert( p->aCol==0 );
00898 p->nCol = pSelTab->nCol;
00899 p->aCol = pSelTab->aCol;
00900 pSelTab->nCol = 0;
00901 pSelTab->aCol = 0;
00902 sqliteDeleteTable(0, pSelTab);
00903 }
00904
00905
00906
00907
00908
00909
00910
00911 if( db->init.busy ){
00912 p->tnum = db->init.newTnum;
00913 }
00914
00915
00916
00917
00918
00919
00920
00921
00922 if( !db->init.busy ){
00923 int n;
00924 Vdbe *v;
00925
00926 v = sqliteGetVdbe(pParse);
00927 if( v==0 ) return;
00928 if( p->pSelect==0 ){
00929
00930 sqliteVdbeOp3(v, OP_CreateTable, 0, p->iDb, (char*)&p->tnum, P3_POINTER);
00931 }else{
00932
00933 sqliteVdbeAddOp(v, OP_Integer, 0, 0);
00934 }
00935 p->tnum = 0;
00936 sqliteVdbeAddOp(v, OP_Pull, 1, 0);
00937 sqliteVdbeOp3(v, OP_String, 0, 0, p->pSelect==0?"table":"view", P3_STATIC);
00938 sqliteVdbeOp3(v, OP_String, 0, 0, p->zName, 0);
00939 sqliteVdbeOp3(v, OP_String, 0, 0, p->zName, 0);
00940 sqliteVdbeAddOp(v, OP_Dup, 4, 0);
00941 sqliteVdbeAddOp(v, OP_String, 0, 0);
00942 if( pSelect ){
00943 char *z = createTableStmt(p);
00944 n = z ? strlen(z) : 0;
00945 sqliteVdbeChangeP3(v, -1, z, n);
00946 sqliteFree(z);
00947 }else{
00948 assert( pEnd!=0 );
00949 n = Addr(pEnd->z) - Addr(pParse->sFirstToken.z) + 1;
00950 sqliteVdbeChangeP3(v, -1, pParse->sFirstToken.z, n);
00951 }
00952 sqliteVdbeAddOp(v, OP_MakeRecord, 5, 0);
00953 sqliteVdbeAddOp(v, OP_PutIntKey, 0, 0);
00954 if( !p->iDb ){
00955 sqliteChangeCookie(db, v);
00956 }
00957 sqliteVdbeAddOp(v, OP_Close, 0, 0);
00958 if( pSelect ){
00959 sqliteVdbeAddOp(v, OP_Integer, p->iDb, 0);
00960 sqliteVdbeAddOp(v, OP_OpenWrite, 1, 0);
00961 pParse->nTab = 2;
00962 sqliteSelect(pParse, pSelect, SRT_Table, 1, 0, 0, 0);
00963 }
00964 sqliteEndWriteOperation(pParse);
00965 }
00966
00967
00968
00969 if( pParse->explain==0 && pParse->nErr==0 ){
00970 Table *pOld;
00971 FKey *pFKey;
00972 pOld = sqliteHashInsert(&db->aDb[p->iDb].tblHash,
00973 p->zName, strlen(p->zName)+1, p);
00974 if( pOld ){
00975 assert( p==pOld );
00976 return;
00977 }
00978 for(pFKey=p->pFKey; pFKey; pFKey=pFKey->pNextFrom){
00979 int nTo = strlen(pFKey->zTo) + 1;
00980 pFKey->pNextTo = sqliteHashFind(&db->aDb[p->iDb].aFKey, pFKey->zTo, nTo);
00981 sqliteHashInsert(&db->aDb[p->iDb].aFKey, pFKey->zTo, nTo, pFKey);
00982 }
00983 pParse->pNewTable = 0;
00984 db->nTable++;
00985 db->flags |= SQLITE_InternChanges;
00986 }
00987 }
00988
00989
00990
00991
00992 void sqliteCreateView(
00993 Parse *pParse,
00994 Token *pBegin,
00995 Token *pName,
00996 Select *pSelect,
00997 int isTemp
00998 ){
00999 Table *p;
01000 int n;
01001 const char *z;
01002 Token sEnd;
01003 DbFixer sFix;
01004
01005 sqliteStartTable(pParse, pBegin, pName, isTemp, 1);
01006 p = pParse->pNewTable;
01007 if( p==0 || pParse->nErr ){
01008 sqliteSelectDelete(pSelect);
01009 return;
01010 }
01011 if( sqliteFixInit(&sFix, pParse, p->iDb, "view", pName)
01012 && sqliteFixSelect(&sFix, pSelect)
01013 ){
01014 sqliteSelectDelete(pSelect);
01015 return;
01016 }
01017
01018
01019
01020
01021
01022
01023 p->pSelect = sqliteSelectDup(pSelect);
01024 sqliteSelectDelete(pSelect);
01025 if( !pParse->db->init.busy ){
01026 sqliteViewGetColumnNames(pParse, p);
01027 }
01028
01029
01030
01031
01032 sEnd = pParse->sLastToken;
01033 if( sEnd.z[0]!=0 && sEnd.z[0]!=';' ){
01034 sEnd.z += sEnd.n;
01035 }
01036 sEnd.n = 0;
01037 n = sEnd.z - pBegin->z;
01038 z = pBegin->z;
01039 while( n>0 && (z[n-1]==';' || isspace(z[n-1])) ){ n--; }
01040 sEnd.z = &z[n-1];
01041 sEnd.n = 1;
01042
01043
01044 sqliteEndTable(pParse, &sEnd, 0);
01045 return;
01046 }
01047
01048
01049
01050
01051
01052
01053 int sqliteViewGetColumnNames(Parse *pParse, Table *pTable){
01054 ExprList *pEList;
01055 Select *pSel;
01056 Table *pSelTab;
01057 int nErr = 0;
01058
01059 assert( pTable );
01060
01061
01062
01063
01064 if( pTable->nCol>0 ) return 0;
01065
01066
01067
01068
01069
01070
01071
01072
01073
01074
01075
01076 if( pTable->nCol<0 ){
01077 sqliteErrorMsg(pParse, "view %s is circularly defined", pTable->zName);
01078 return 1;
01079 }
01080
01081
01082
01083 assert( pTable->pSelect );
01084 pSel = pTable->pSelect;
01085
01086
01087
01088
01089
01090
01091 pEList = pSel->pEList;
01092 pSel->pEList = sqliteExprListDup(pEList);
01093 if( pSel->pEList==0 ){
01094 pSel->pEList = pEList;
01095 return 1;
01096 }
01097 pTable->nCol = -1;
01098 pSelTab = sqliteResultSetOfSelect(pParse, 0, pSel);
01099 if( pSelTab ){
01100 assert( pTable->aCol==0 );
01101 pTable->nCol = pSelTab->nCol;
01102 pTable->aCol = pSelTab->aCol;
01103 pSelTab->nCol = 0;
01104 pSelTab->aCol = 0;
01105 sqliteDeleteTable(0, pSelTab);
01106 DbSetProperty(pParse->db, pTable->iDb, DB_UnresetViews);
01107 }else{
01108 pTable->nCol = 0;
01109 nErr++;
01110 }
01111 sqliteSelectUnbind(pSel);
01112 sqliteExprListDelete(pSel->pEList);
01113 pSel->pEList = pEList;
01114 return nErr;
01115 }
01116
01117
01118
01119
01120
01121
01122
01123
01124
01125 static void sqliteViewResetColumnNames(Table *pTable){
01126 int i;
01127 Column *pCol;
01128 assert( pTable!=0 && pTable->pSelect!=0 );
01129 for(i=0, pCol=pTable->aCol; i<pTable->nCol; i++, pCol++){
01130 sqliteFree(pCol->zName);
01131 sqliteFree(pCol->zDflt);
01132 sqliteFree(pCol->zType);
01133 }
01134 sqliteFree(pTable->aCol);
01135 pTable->aCol = 0;
01136 pTable->nCol = 0;
01137 }
01138
01139
01140
01141
01142 static void sqliteViewResetAll(sqlite *db, int idx){
01143 HashElem *i;
01144 if( !DbHasProperty(db, idx, DB_UnresetViews) ) return;
01145 for(i=sqliteHashFirst(&db->aDb[idx].tblHash); i; i=sqliteHashNext(i)){
01146 Table *pTab = sqliteHashData(i);
01147 if( pTab->pSelect ){
01148 sqliteViewResetColumnNames(pTab);
01149 }
01150 }
01151 DbClearProperty(db, idx, DB_UnresetViews);
01152 }
01153
01154
01155
01156
01157
01158 Table *sqliteTableFromToken(Parse *pParse, Token *pTok){
01159 char *zName;
01160 Table *pTab;
01161 zName = sqliteTableNameFromToken(pTok);
01162 if( zName==0 ) return 0;
01163 pTab = sqliteFindTable(pParse->db, zName, 0);
01164 sqliteFree(zName);
01165 if( pTab==0 ){
01166 sqliteErrorMsg(pParse, "no such table: %T", pTok);
01167 }
01168 return pTab;
01169 }
01170
01171
01172
01173
01174
01175 void sqliteDropTable(Parse *pParse, Token *pName, int isView){
01176 Table *pTable;
01177 Vdbe *v;
01178 int base;
01179 sqlite *db = pParse->db;
01180 int iDb;
01181
01182 if( pParse->nErr || sqlite_malloc_failed ) return;
01183 pTable = sqliteTableFromToken(pParse, pName);
01184 if( pTable==0 ) return;
01185 iDb = pTable->iDb;
01186 assert( iDb>=0 && iDb<db->nDb );
01187 #ifndef SQLITE_OMIT_AUTHORIZATION
01188 {
01189 int code;
01190 const char *zTab = SCHEMA_TABLE(pTable->iDb);
01191 const char *zDb = db->aDb[pTable->iDb].zName;
01192 if( sqliteAuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb)){
01193 return;
01194 }
01195 if( isView ){
01196 if( iDb==1 ){
01197 code = SQLITE_DROP_TEMP_VIEW;
01198 }else{
01199 code = SQLITE_DROP_VIEW;
01200 }
01201 }else{
01202 if( iDb==1 ){
01203 code = SQLITE_DROP_TEMP_TABLE;
01204 }else{
01205 code = SQLITE_DROP_TABLE;
01206 }
01207 }
01208 if( sqliteAuthCheck(pParse, code, pTable->zName, 0, zDb) ){
01209 return;
01210 }
01211 if( sqliteAuthCheck(pParse, SQLITE_DELETE, pTable->zName, 0, zDb) ){
01212 return;
01213 }
01214 }
01215 #endif
01216 if( pTable->readOnly ){
01217 sqliteErrorMsg(pParse, "table %s may not be dropped", pTable->zName);
01218 pParse->nErr++;
01219 return;
01220 }
01221 if( isView && pTable->pSelect==0 ){
01222 sqliteErrorMsg(pParse, "use DROP TABLE to delete table %s", pTable->zName);
01223 return;
01224 }
01225 if( !isView && pTable->pSelect ){
01226 sqliteErrorMsg(pParse, "use DROP VIEW to delete view %s", pTable->zName);
01227 return;
01228 }
01229
01230
01231
01232
01233 v = sqliteGetVdbe(pParse);
01234 if( v ){
01235 static VdbeOpList dropTable[] = {
01236 { OP_Rewind, 0, ADDR(8), 0},
01237 { OP_String, 0, 0, 0},
01238 { OP_MemStore, 1, 1, 0},
01239 { OP_MemLoad, 1, 0, 0},
01240 { OP_Column, 0, 2, 0},
01241 { OP_Ne, 0, ADDR(7), 0},
01242 { OP_Delete, 0, 0, 0},
01243 { OP_Next, 0, ADDR(3), 0},
01244 };
01245 Index *pIdx;
01246 Trigger *pTrigger;
01247 sqliteBeginWriteOperation(pParse, 0, pTable->iDb);
01248
01249
01250 pTrigger = pTable->pTrigger;
01251 while( pTrigger ){
01252 assert( pTrigger->iDb==pTable->iDb || pTrigger->iDb==1 );
01253 sqliteDropTriggerPtr(pParse, pTrigger, 1);
01254 if( pParse->explain ){
01255 pTrigger = pTrigger->pNext;
01256 }else{
01257 pTrigger = pTable->pTrigger;
01258 }
01259 }
01260
01261
01262 sqliteOpenMasterTable(v, pTable->iDb);
01263 base = sqliteVdbeAddOpList(v, ArraySize(dropTable), dropTable);
01264 sqliteVdbeChangeP3(v, base+1, pTable->zName, 0);
01265
01266
01267 if( pTable->iDb!=1 ){
01268 sqliteOpenMasterTable(v, 1);
01269 base = sqliteVdbeAddOpList(v, ArraySize(dropTable), dropTable);
01270 sqliteVdbeChangeP3(v, base+1, pTable->zName, 0);
01271 }
01272
01273 if( pTable->iDb==0 ){
01274 sqliteChangeCookie(db, v);
01275 }
01276 sqliteVdbeAddOp(v, OP_Close, 0, 0);
01277 if( !isView ){
01278 sqliteVdbeAddOp(v, OP_Destroy, pTable->tnum, pTable->iDb);
01279 for(pIdx=pTable->pIndex; pIdx; pIdx=pIdx->pNext){
01280 sqliteVdbeAddOp(v, OP_Destroy, pIdx->tnum, pIdx->iDb);
01281 }
01282 }
01283 sqliteEndWriteOperation(pParse);
01284 }
01285
01286
01287
01288
01289
01290
01291 if( !pParse->explain ){
01292 sqliteUnlinkAndDeleteTable(db, pTable);
01293 db->flags |= SQLITE_InternChanges;
01294 }
01295 sqliteViewResetAll(db, iDb);
01296 }
01297
01298
01299
01300
01301
01302
01303
01304
01305
01306
01307
01308 void sqliteAddIdxKeyType(Vdbe *v, Index *pIdx){
01309 char *zType;
01310 Table *pTab;
01311 int i, n;
01312 assert( pIdx!=0 && pIdx->pTable!=0 );
01313 pTab = pIdx->pTable;
01314 n = pIdx->nColumn;
01315 zType = sqliteMallocRaw( n+1 );
01316 if( zType==0 ) return;
01317 for(i=0; i<n; i++){
01318 int iCol = pIdx->aiColumn[i];
01319 assert( iCol>=0 && iCol<pTab->nCol );
01320 if( (pTab->aCol[iCol].sortOrder & SQLITE_SO_TYPEMASK)==SQLITE_SO_TEXT ){
01321 zType[i] = 't';
01322 }else{
01323 zType[i] = 'n';
01324 }
01325 }
01326 zType[n] = 0;
01327 sqliteVdbeChangeP3(v, -1, zType, n);
01328 sqliteFree(zType);
01329 }
01330
01331
01332
01333
01334
01335
01336
01337
01338
01339
01340
01341
01342
01343
01344
01345
01346
01347
01348
01349 void sqliteCreateForeignKey(
01350 Parse *pParse,
01351 IdList *pFromCol,
01352 Token *pTo,
01353 IdList *pToCol,
01354 int flags
01355 ){
01356 Table *p = pParse->pNewTable;
01357 int nByte;
01358 int i;
01359 int nCol;
01360 char *z;
01361 FKey *pFKey = 0;
01362
01363 assert( pTo!=0 );
01364 if( p==0 || pParse->nErr ) goto fk_end;
01365 if( pFromCol==0 ){
01366 int iCol = p->nCol-1;
01367 if( iCol<0 ) goto fk_end;
01368 if( pToCol && pToCol->nId!=1 ){
01369 sqliteErrorMsg(pParse, "foreign key on %s"
01370 " should reference only one column of table %T",
01371 p->aCol[iCol].zName, pTo);
01372 goto fk_end;
01373 }
01374 nCol = 1;
01375 }else if( pToCol && pToCol->nId!=pFromCol->nId ){
01376 sqliteErrorMsg(pParse,
01377 "number of columns in foreign key does not match the number of "
01378 "columns in the referenced table");
01379 goto fk_end;
01380 }else{
01381 nCol = pFromCol->nId;
01382 }
01383 nByte = sizeof(*pFKey) + nCol*sizeof(pFKey->aCol[0]) + pTo->n + 1;
01384 if( pToCol ){
01385 for(i=0; i<pToCol->nId; i++){
01386 nByte += strlen(pToCol->a[i].zName) + 1;
01387 }
01388 }
01389 pFKey = sqliteMalloc( nByte );
01390 if( pFKey==0 ) goto fk_end;
01391 pFKey->pFrom = p;
01392 pFKey->pNextFrom = p->pFKey;
01393 z = (char*)&pFKey[1];
01394 pFKey->aCol = (struct sColMap*)z;
01395 z += sizeof(struct sColMap)*nCol;
01396 pFKey->zTo = z;
01397 memcpy(z, pTo->z, pTo->n);
01398 z[pTo->n] = 0;
01399 z += pTo->n+1;
01400 pFKey->pNextTo = 0;
01401 pFKey->nCol = nCol;
01402 if( pFromCol==0 ){
01403 pFKey->aCol[0].iFrom = p->nCol-1;
01404 }else{
01405 for(i=0; i<nCol; i++){
01406 int j;
01407 for(j=0; j<p->nCol; j++){
01408 if( sqliteStrICmp(p->aCol[j].zName, pFromCol->a[i].zName)==0 ){
01409 pFKey->aCol[i].iFrom = j;
01410 break;
01411 }
01412 }
01413 if( j>=p->nCol ){
01414 sqliteErrorMsg(pParse,
01415 "unknown column \"%s\" in foreign key definition",
01416 pFromCol->a[i].zName);
01417 goto fk_end;
01418 }
01419 }
01420 }
01421 if( pToCol ){
01422 for(i=0; i<nCol; i++){
01423 int n = strlen(pToCol->a[i].zName);
01424 pFKey->aCol[i].zCol = z;
01425 memcpy(z, pToCol->a[i].zName, n);
01426 z[n] = 0;
01427 z += n+1;
01428 }
01429 }
01430 pFKey->isDeferred = 0;
01431 pFKey->deleteConf = flags & 0xff;
01432 pFKey->updateConf = (flags >> 8 ) & 0xff;
01433 pFKey->insertConf = (flags >> 16 ) & 0xff;
01434
01435
01436
01437 p->pFKey = pFKey;
01438 pFKey = 0;
01439
01440 fk_end:
01441 sqliteFree(pFKey);
01442 sqliteIdListDelete(pFromCol);
01443 sqliteIdListDelete(pToCol);
01444 }
01445
01446
01447
01448
01449
01450
01451
01452
01453 void sqliteDeferForeignKey(Parse *pParse, int isDeferred){
01454 Table *pTab;
01455 FKey *pFKey;
01456 if( (pTab = pParse->pNewTable)==0 || (pFKey = pTab->pFKey)==0 ) return;
01457 pFKey->isDeferred = isDeferred;
01458 }
01459
01460
01461
01462
01463
01464
01465
01466
01467
01468
01469
01470
01471
01472 void sqliteCreateIndex(
01473 Parse *pParse,
01474 Token *pName,
01475 SrcList *pTable,
01476 IdList *pList,
01477 int onError,
01478 Token *pStart,
01479 Token *pEnd
01480 ){
01481 Table *pTab;
01482 Index *pIndex;
01483 char *zName = 0;
01484 int i, j;
01485 Token nullId;
01486 DbFixer sFix;
01487 int isTemp;
01488 sqlite *db = pParse->db;
01489
01490 if( pParse->nErr || sqlite_malloc_failed ) goto exit_create_index;
01491 if( db->init.busy
01492 && sqliteFixInit(&sFix, pParse, db->init.iDb, "index", pName)
01493 && sqliteFixSrcList(&sFix, pTable)
01494 ){
01495 goto exit_create_index;
01496 }
01497
01498
01499
01500
01501 if( pTable!=0 ){
01502 assert( pName!=0 );
01503 assert( pTable->nSrc==1 );
01504 pTab = sqliteSrcListLookup(pParse, pTable);
01505 }else{
01506 assert( pName==0 );
01507 pTab = pParse->pNewTable;
01508 }
01509 if( pTab==0 || pParse->nErr ) goto exit_create_index;
01510 if( pTab->readOnly ){
01511 sqliteErrorMsg(pParse, "table %s may not be indexed", pTab->zName);
01512 goto exit_create_index;
01513 }
01514 if( pTab->iDb>=2 && db->init.busy==0 ){
01515 sqliteErrorMsg(pParse, "table %s may not have indices added", pTab->zName);
01516 goto exit_create_index;
01517 }
01518 if( pTab->pSelect ){
01519 sqliteErrorMsg(pParse, "views may not be indexed");
01520 goto exit_create_index;
01521 }
01522 isTemp = pTab->iDb==1;
01523
01524
01525
01526
01527
01528
01529
01530
01531
01532
01533
01534
01535
01536
01537 if( pName && !db->init.busy ){
01538 Index *pISameName;
01539 Table *pTSameName;
01540 zName = sqliteTableNameFromToken(pName);
01541 if( zName==0 ) goto exit_create_index;
01542 if( (pISameName = sqliteFindIndex(db, zName, 0))!=0 ){
01543 sqliteErrorMsg(pParse, "index %s already exists", zName);
01544 goto exit_create_index;
01545 }
01546 if( (pTSameName = sqliteFindTable(db, zName, 0))!=0 ){
01547 sqliteErrorMsg(pParse, "there is already a table named %s", zName);
01548 goto exit_create_index;
01549 }
01550 }else if( pName==0 ){
01551 char zBuf[30];
01552 int n;
01553 Index *pLoop;
01554 for(pLoop=pTab->pIndex, n=1; pLoop; pLoop=pLoop->pNext, n++){}
01555 sprintf(zBuf,"%d)",n);
01556 zName = 0;
01557 sqliteSetString(&zName, "(", pTab->zName, " autoindex ", zBuf, (char*)0);
01558 if( zName==0 ) goto exit_create_index;
01559 }else{
01560 zName = sqliteTableNameFromToken(pName);
01561 }
01562
01563
01564
01565 #ifndef SQLITE_OMIT_AUTHORIZATION
01566 {
01567 const char *zDb = db->aDb[pTab->iDb].zName;
01568
01569 assert( pTab->iDb==db->init.iDb || isTemp );
01570 if( sqliteAuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(isTemp), 0, zDb) ){
01571 goto exit_create_index;
01572 }
01573 i = SQLITE_CREATE_INDEX;
01574 if( isTemp ) i = SQLITE_CREATE_TEMP_INDEX;
01575 if( sqliteAuthCheck(pParse, i, zName, pTab->zName, zDb) ){
01576 goto exit_create_index;
01577 }
01578 }
01579 #endif
01580
01581
01582
01583
01584
01585 if( pList==0 ){
01586 nullId.z = pTab->aCol[pTab->nCol-1].zName;
01587 nullId.n = strlen(nullId.z);
01588 pList = sqliteIdListAppend(0, &nullId);
01589 if( pList==0 ) goto exit_create_index;
01590 }
01591
01592
01593
01594
01595 pIndex = sqliteMalloc( sizeof(Index) + strlen(zName) + 1 +
01596 sizeof(int)*pList->nId );
01597 if( pIndex==0 ) goto exit_create_index;
01598 pIndex->aiColumn = (int*)&pIndex[1];
01599 pIndex->zName = (char*)&pIndex->aiColumn[pList->nId];
01600 strcpy(pIndex->zName, zName);
01601 pIndex->pTable = pTab;
01602 pIndex->nColumn = pList->nId;
01603 pIndex->onError = onError;
01604 pIndex->autoIndex = pName==0;
01605 pIndex->iDb = isTemp ? 1 : db->init.iDb;
01606
01607
01608
01609
01610
01611 for(i=0; i<pList->nId; i++){
01612 for(j=0; j<pTab->nCol; j++){
01613 if( sqliteStrICmp(pList->a[i].zName, pTab->aCol[j].zName)==0 ) break;
01614 }
01615 if( j>=pTab->nCol ){
01616 sqliteErrorMsg(pParse, "table %s has no column named %s",
01617 pTab->zName, pList->a[i].zName);
01618 sqliteFree(pIndex);
01619 goto exit_create_index;
01620 }
01621 pIndex->aiColumn[i] = j;
01622 }
01623
01624
01625
01626
01627 if( !pParse->explain ){
01628 Index *p;
01629 p = sqliteHashInsert(&db->aDb[pIndex->iDb].idxHash,
01630 pIndex->zName, strlen(pIndex->zName)+1, pIndex);
01631 if( p ){
01632 assert( p==pIndex );
01633 sqliteFree(pIndex);
01634 goto exit_create_index;
01635 }
01636 db->flags |= SQLITE_InternChanges;
01637 }
01638
01639
01640
01641
01642
01643
01644 if( onError!=OE_Replace || pTab->pIndex==0
01645 || pTab->pIndex->onError==OE_Replace){
01646 pIndex->pNext = pTab->pIndex;
01647 pTab->pIndex = pIndex;
01648 }else{
01649 Index *pOther = pTab->pIndex;
01650 while( pOther->pNext && pOther->pNext->onError!=OE_Replace ){
01651 pOther = pOther->pNext;
01652 }
01653 pIndex->pNext = pOther->pNext;
01654 pOther->pNext = pIndex;
01655 }
01656
01657
01658
01659
01660
01661 if( db->init.busy && pTable!=0 ){
01662 pIndex->tnum = db->init.newTnum;
01663 }
01664
01665
01666
01667
01668
01669
01670
01671
01672
01673
01674
01675
01676
01677
01678
01679
01680 else if( db->init.busy==0 ){
01681 int n;
01682 Vdbe *v;
01683 int lbl1, lbl2;
01684 int i;
01685 int addr;
01686
01687 v = sqliteGetVdbe(pParse);
01688 if( v==0 ) goto exit_create_index;
01689 if( pTable!=0 ){
01690 sqliteBeginWriteOperation(pParse, 0, isTemp);
01691 sqliteOpenMasterTable(v, isTemp);
01692 }
01693 sqliteVdbeAddOp(v, OP_NewRecno, 0, 0);
01694 sqliteVdbeOp3(v, OP_String, 0, 0, "index", P3_STATIC);
01695 sqliteVdbeOp3(v, OP_String, 0, 0, pIndex->zName, 0);
01696 sqliteVdbeOp3(v, OP_String, 0, 0, pTab->zName, 0);
01697 sqliteVdbeOp3(v, OP_CreateIndex, 0, isTemp,(char*)&pIndex->tnum,P3_POINTER);
01698 pIndex->tnum = 0;
01699 if( pTable ){
01700 sqliteVdbeCode(v,
01701 OP_Dup, 0, 0,
01702 OP_Integer, isTemp, 0,
01703 OP_OpenWrite, 1, 0,
01704 0);
01705 }
01706 addr = sqliteVdbeAddOp(v, OP_String, 0, 0);
01707 if( pStart && pEnd ){
01708 n = Addr(pEnd->z) - Addr(pStart->z) + 1;
01709 sqliteVdbeChangeP3(v, addr, pStart->z, n);
01710 }
01711 sqliteVdbeAddOp(v, OP_MakeRecord, 5, 0);
01712 sqliteVdbeAddOp(v, OP_PutIntKey, 0, 0);
01713 if( pTable ){
01714 sqliteVdbeAddOp(v, OP_Integer, pTab->iDb, 0);
01715 sqliteVdbeOp3(v, OP_OpenRead, 2, pTab->tnum, pTab->zName, 0);
01716 lbl2 = sqliteVdbeMakeLabel(v);
01717 sqliteVdbeAddOp(v, OP_Rewind, 2, lbl2);
01718 lbl1 = sqliteVdbeAddOp(v, OP_Recno, 2, 0);
01719 for(i=0; i<pIndex->nColumn; i++){
01720 int iCol = pIndex->aiColumn[i];
01721 if( pTab->iPKey==iCol ){
01722 sqliteVdbeAddOp(v, OP_Dup, i, 0);
01723 }else{
01724 sqliteVdbeAddOp(v, OP_Column, 2, iCol);
01725 }
01726 }
01727 sqliteVdbeAddOp(v, OP_MakeIdxKey, pIndex->nColumn, 0);
01728 if( db->file_format>=4 ) sqliteAddIdxKeyType(v, pIndex);
01729 sqliteVdbeOp3(v, OP_IdxPut, 1, pIndex->onError!=OE_None,
01730 "indexed columns are not unique", P3_STATIC);
01731 sqliteVdbeAddOp(v, OP_Next, 2, lbl1);
01732 sqliteVdbeResolveLabel(v, lbl2);
01733 sqliteVdbeAddOp(v, OP_Close, 2, 0);
01734 sqliteVdbeAddOp(v, OP_Close, 1, 0);
01735 }
01736 if( pTable!=0 ){
01737 if( !isTemp ){
01738 sqliteChangeCookie(db, v);
01739 }
01740 sqliteVdbeAddOp(v, OP_Close, 0, 0);
01741 sqliteEndWriteOperation(pParse);
01742 }
01743 }
01744
01745
01746 exit_create_index:
01747 sqliteIdListDelete(pList);
01748 sqliteSrcListDelete(pTable);
01749 sqliteFree(zName);
01750 return;
01751 }
01752
01753
01754
01755
01756
01757 void sqliteDropIndex(Parse *pParse, SrcList *pName){
01758 Index *pIndex;
01759 Vdbe *v;
01760 sqlite *db = pParse->db;
01761
01762 if( pParse->nErr || sqlite_malloc_failed ) return;
01763 assert( pName->nSrc==1 );
01764 pIndex = sqliteFindIndex(db, pName->a[0].zName, pName->a[0].zDatabase);
01765 if( pIndex==0 ){
01766 sqliteErrorMsg(pParse, "no such index: %S", pName, 0);
01767 goto exit_drop_index;
01768 }
01769 if( pIndex->autoIndex ){
01770 sqliteErrorMsg(pParse, "index associated with UNIQUE "
01771 "or PRIMARY KEY constraint cannot be dropped", 0);
01772 goto exit_drop_index;
01773 }
01774 if( pIndex->iDb>1 ){
01775 sqliteErrorMsg(pParse, "cannot alter schema of attached "
01776 "databases", 0);
01777 goto exit_drop_index;
01778 }
01779 #ifndef SQLITE_OMIT_AUTHORIZATION
01780 {
01781 int code = SQLITE_DROP_INDEX;
01782 Table *pTab = pIndex->pTable;
01783 const char *zDb = db->aDb[pIndex->iDb].zName;
01784 const char *zTab = SCHEMA_TABLE(pIndex->iDb);
01785 if( sqliteAuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){
01786 goto exit_drop_index;
01787 }
01788 if( pIndex->iDb ) code = SQLITE_DROP_TEMP_INDEX;
01789 if( sqliteAuthCheck(pParse, code, pIndex->zName, pTab->zName, zDb) ){
01790 goto exit_drop_index;
01791 }
01792 }
01793 #endif
01794
01795
01796 v = sqliteGetVdbe(pParse);
01797 if( v ){
01798 static VdbeOpList dropIndex[] = {
01799 { OP_Rewind, 0, ADDR(9), 0},
01800 { OP_String, 0, 0, 0},
01801 { OP_MemStore, 1, 1, 0},
01802 { OP_MemLoad, 1, 0, 0},
01803 { OP_Column, 0, 1, 0},
01804 { OP_Eq, 0, ADDR(8), 0},
01805 { OP_Next, 0, ADDR(3), 0},
01806 { OP_Goto, 0, ADDR(9), 0},
01807 { OP_Delete, 0, 0, 0},
01808 };
01809 int base;
01810
01811 sqliteBeginWriteOperation(pParse, 0, pIndex->iDb);
01812 sqliteOpenMasterTable(v, pIndex->iDb);
01813 base = sqliteVdbeAddOpList(v, ArraySize(dropIndex), dropIndex);
01814 sqliteVdbeChangeP3(v, base+1, pIndex->zName, 0);
01815 if( pIndex->iDb==0 ){
01816 sqliteChangeCookie(db, v);
01817 }
01818 sqliteVdbeAddOp(v, OP_Close, 0, 0);
01819 sqliteVdbeAddOp(v, OP_Destroy, pIndex->tnum, pIndex->iDb);
01820 sqliteEndWriteOperation(pParse);
01821 }
01822
01823
01824
01825 if( !pParse->explain ){
01826 sqliteUnlinkAndDeleteIndex(db, pIndex);
01827 db->flags |= SQLITE_InternChanges;
01828 }
01829
01830 exit_drop_index:
01831 sqliteSrcListDelete(pName);
01832 }
01833
01834
01835
01836
01837
01838
01839
01840 IdList *sqliteIdListAppend(IdList *pList, Token *pToken){
01841 if( pList==0 ){
01842 pList = sqliteMalloc( sizeof(IdList) );
01843 if( pList==0 ) return 0;
01844 pList->nAlloc = 0;
01845 }
01846 if( pList->nId>=pList->nAlloc ){
01847 struct IdList_item *a;
01848 pList->nAlloc = pList->nAlloc*2 + 5;
01849 a = sqliteRealloc(pList->a, pList->nAlloc*sizeof(pList->a[0]) );
01850 if( a==0 ){
01851 sqliteIdListDelete(pList);
01852 return 0;
01853 }
01854 pList->a = a;
01855 }
01856 memset(&pList->a[pList->nId], 0, sizeof(pList->a[0]));
01857 if( pToken ){
01858 char **pz = &pList->a[pList->nId].zName;
01859 sqliteSetNString(pz, pToken->z, pToken->n, 0);
01860 if( *pz==0 ){
01861 sqliteIdListDelete(pList);
01862 return 0;
01863 }else{
01864 sqliteDequote(*pz);
01865 }
01866 }
01867 pList->nId++;
01868 return pList;
01869 }
01870
01871
01872
01873
01874
01875
01876
01877
01878
01879
01880
01881
01882
01883
01884
01885
01886
01887
01888
01889
01890
01891
01892
01893
01894
01895
01896 SrcList *sqliteSrcListAppend(SrcList *pList, Token *pTable, Token *pDatabase){
01897 if( pList==0 ){
01898 pList = sqliteMalloc( sizeof(SrcList) );
01899 if( pList==0 ) return 0;
01900 pList->nAlloc = 1;
01901 }
01902 if( pList->nSrc>=pList->nAlloc ){
01903 SrcList *pNew;
01904 pList->nAlloc *= 2;
01905 pNew = sqliteRealloc(pList,
01906 sizeof(*pList) + (pList->nAlloc-1)*sizeof(pList->a[0]) );
01907 if( pNew==0 ){
01908 sqliteSrcListDelete(pList);
01909 return 0;
01910 }
01911 pList = pNew;
01912 }
01913 memset(&pList->a[pList->nSrc], 0, sizeof(pList->a[0]));
01914 if( pDatabase && pDatabase->z==0 ){
01915 pDatabase = 0;
01916 }
01917 if( pDatabase && pTable ){
01918 Token *pTemp = pDatabase;
01919 pDatabase = pTable;
01920 pTable = pTemp;
01921 }
01922 if( pTable ){
01923 char **pz = &pList->a[pList->nSrc].zName;
01924 sqliteSetNString(pz, pTable->z, pTable->n, 0);
01925 if( *pz==0 ){
01926 sqliteSrcListDelete(pList);
01927 return 0;
01928 }else{
01929 sqliteDequote(*pz);
01930 }
01931 }
01932 if( pDatabase ){
01933 char **pz = &pList->a[pList->nSrc].zDatabase;
01934 sqliteSetNString(pz, pDatabase->z, pDatabase->n, 0);
01935 if( *pz==0 ){
01936 sqliteSrcListDelete(pList);
01937 return 0;
01938 }else{
01939 sqliteDequote(*pz);
01940 }
01941 }
01942 pList->a[pList->nSrc].iCursor = -1;
01943 pList->nSrc++;
01944 return pList;
01945 }
01946
01947
01948
01949
01950 void sqliteSrcListAssignCursors(Parse *pParse, SrcList *pList){
01951 int i;
01952 for(i=0; i<pList->nSrc; i++){
01953 if( pList->a[i].iCursor<0 ){
01954 pList->a[i].iCursor = pParse->nTab++;
01955 }
01956 }
01957 }
01958
01959
01960
01961
01962 void sqliteSrcListAddAlias(SrcList *pList, Token *pToken){
01963 if( pList && pList->nSrc>0 ){
01964 int i = pList->nSrc - 1;
01965 sqliteSetNString(&pList->a[i].zAlias, pToken->z, pToken->n, 0);
01966 sqliteDequote(pList->a[i].zAlias);
01967 }
01968 }
01969
01970
01971
01972
01973 void sqliteIdListDelete(IdList *pList){
01974 int i;
01975 if( pList==0 ) return;
01976 for(i=0; i<pList->nId; i++){
01977 sqliteFree(pList->a[i].zName);
01978 }
01979 sqliteFree(pList->a);
01980 sqliteFree(pList);
01981 }
01982
01983
01984
01985
01986
01987 int sqliteIdListIndex(IdList *pList, const char *zName){
01988 int i;
01989 if( pList==0 ) return -1;
01990 for(i=0; i<pList->nId; i++){
01991 if( sqliteStrICmp(pList->a[i].zName, zName)==0 ) return i;
01992 }
01993 return -1;
01994 }
01995
01996
01997
01998
01999 void sqliteSrcListDelete(SrcList *pList){
02000 int i;
02001 if( pList==0 ) return;
02002 for(i=0; i<pList->nSrc; i++){
02003 sqliteFree(pList->a[i].zDatabase);
02004 sqliteFree(pList->a[i].zName);
02005 sqliteFree(pList->a[i].zAlias);
02006 if( pList->a[i].pTab && pList->a[i].pTab->isTransient ){
02007 sqliteDeleteTable(0, pList->a[i].pTab);
02008 }
02009 sqliteSelectDelete(pList->a[i].pSelect);
02010 sqliteExprDelete(pList->a[i].pOn);
02011 sqliteIdListDelete(pList->a[i].pUsing);
02012 }
02013 sqliteFree(pList);
02014 }
02015
02016
02017
02018
02019 void sqliteBeginTransaction(Parse *pParse, int onError){
02020 sqlite *db;
02021
02022 if( pParse==0 || (db=pParse->db)==0 || db->aDb[0].pBt==0 ) return;
02023 if( pParse->nErr || sqlite_malloc_failed ) return;
02024 if( sqliteAuthCheck(pParse, SQLITE_TRANSACTION, "BEGIN", 0, 0) ) return;
02025 if( db->flags & SQLITE_InTrans ){
02026 sqliteErrorMsg(pParse, "cannot start a transaction within a transaction");
02027 return;
02028 }
02029 sqliteBeginWriteOperation(pParse, 0, 0);
02030 if( !pParse->explain ){
02031 db->flags |= SQLITE_InTrans;
02032 db->onError = onError;
02033 }
02034 }
02035
02036
02037
02038
02039 void sqliteCommitTransaction(Parse *pParse){
02040 sqlite *db;
02041
02042 if( pParse==0 || (db=pParse->db)==0 || db->aDb[0].pBt==0 ) return;
02043 if( pParse->nErr || sqlite_malloc_failed ) return;
02044 if( sqliteAuthCheck(pParse, SQLITE_TRANSACTION, "COMMIT", 0, 0) ) return;
02045 if( (db->flags & SQLITE_InTrans)==0 ){
02046 sqliteErrorMsg(pParse, "cannot commit - no transaction is active");
02047 return;
02048 }
02049 if( !pParse->explain ){
02050 db->flags &= ~SQLITE_InTrans;
02051 }
02052 sqliteEndWriteOperation(pParse);
02053 if( !pParse->explain ){
02054 db->onError = OE_Default;
02055 }
02056 }
02057
02058
02059
02060
02061 void sqliteRollbackTransaction(Parse *pParse){
02062 sqlite *db;
02063 Vdbe *v;
02064
02065 if( pParse==0 || (db=pParse->db)==0 || db->aDb[0].pBt==0 ) return;
02066 if( pParse->nErr || sqlite_malloc_failed ) return;
02067 if( sqliteAuthCheck(pParse, SQLITE_TRANSACTION, "ROLLBACK", 0, 0) ) return;
02068 if( (db->flags & SQLITE_InTrans)==0 ){
02069 sqliteErrorMsg(pParse, "cannot rollback - no transaction is active");
02070 return;
02071 }
02072 v = sqliteGetVdbe(pParse);
02073 if( v ){
02074 sqliteVdbeAddOp(v, OP_Rollback, 0, 0);
02075 }
02076 if( !pParse->explain ){
02077 db->flags &= ~SQLITE_InTrans;
02078 db->onError = OE_Default;
02079 }
02080 }
02081
02082
02083
02084
02085
02086 void sqliteCodeVerifySchema(Parse *pParse, int iDb){
02087 sqlite *db = pParse->db;
02088 Vdbe *v = sqliteGetVdbe(pParse);
02089 assert( iDb>=0 && iDb<db->nDb );
02090 assert( db->aDb[iDb].pBt!=0 );
02091 if( iDb!=1 && !DbHasProperty(db, iDb, DB_Cookie) ){
02092 sqliteVdbeAddOp(v, OP_VerifyCookie, iDb, db->aDb[iDb].schema_cookie);
02093 DbSetProperty(db, iDb, DB_Cookie);
02094 }
02095 }
02096
02097
02098
02099
02100
02101
02102
02103
02104
02105
02106
02107
02108
02109
02110
02111
02112
02113
02114
02115 void sqliteBeginWriteOperation(Parse *pParse, int setCheckpoint, int iDb){
02116 Vdbe *v;
02117 sqlite *db = pParse->db;
02118 if( DbHasProperty(db, iDb, DB_Locked) ) return;
02119 v = sqliteGetVdbe(pParse);
02120 if( v==0 ) return;
02121 if( !db->aDb[iDb].inTrans ){
02122 sqliteVdbeAddOp(v, OP_Transaction, iDb, 0);
02123 DbSetProperty(db, iDb, DB_Locked);
02124 sqliteCodeVerifySchema(pParse, iDb);
02125 if( iDb!=1 ){
02126 sqliteBeginWriteOperation(pParse, setCheckpoint, 1);
02127 }
02128 }else if( setCheckpoint ){
02129 sqliteVdbeAddOp(v, OP_Checkpoint, iDb, 0);
02130 DbSetProperty(db, iDb, DB_Locked);
02131 }
02132 }
02133
02134
02135
02136
02137
02138
02139
02140
02141
02142
02143
02144 void sqliteEndWriteOperation(Parse *pParse){
02145 Vdbe *v;
02146 sqlite *db = pParse->db;
02147 if( pParse->trigStack ) return;
02148 v = sqliteGetVdbe(pParse);
02149 if( v==0 ) return;
02150 if( db->flags & SQLITE_InTrans ){
02151
02152
02153 }else{
02154 sqliteVdbeAddOp(v, OP_Commit, 0, 0);
02155 }
02156 }