#include "sqliteInt.h"
#include <ctype.h>
void
sqliteBeginParse(Parse *pParse,
int
explainFlag){
sqlite *db = pParse->db;
int
i;
pParse->explain = explainFlag;
if
((db->flags & SQLITE_Initialized)==0 && db->init.busy==0 ){
int
rc = sqliteInit(db, &pParse->zErrMsg);
if
( rc!=SQLITE_OK ){
pParse->rc = rc;
pParse->nErr++;
}
}
for
(i=0; i<db->nDb; i++){
DbClearProperty(db, i, DB_Locked);
if
( !db->aDb[i].inTrans ){
DbClearProperty(db, i, DB_Cookie);
}
}
pParse->nVar = 0;
}
void
sqliteExec(Parse *pParse){
sqlite *db = pParse->db;
Vdbe *v = pParse->pVdbe;
if
( v==0 && (v = sqliteGetVdbe(pParse))!=0 ){
sqliteVdbeAddOp(v, OP_Halt, 0, 0);
}
if
( sqlite_malloc_failed )
return
;
if
( v && pParse->nErr==0 ){
FILE
*trace = (db->flags & SQLITE_VdbeTrace)!=0 ? stdout : 0;
sqliteVdbeTrace(v, trace);
sqliteVdbeMakeReady(v, pParse->nVar, pParse->explain);
pParse->rc = pParse->nErr ? SQLITE_ERROR : SQLITE_DONE;
pParse->colNamesSet = 0;
}
else
if
( pParse->rc==SQLITE_OK ){
pParse->rc = SQLITE_ERROR;
}
pParse->nTab = 0;
pParse->nMem = 0;
pParse->nSet = 0;
pParse->nAgg = 0;
pParse->nVar = 0;
}
Table *sqliteFindTable(sqlite *db,
const
char
*zName,
const
char
*zDatabase){
Table *p = 0;
int
i;
for
(i=0; i<db->nDb; i++){
int
j = (i<2) ? i^1 : i;
if
( zDatabase!=0 && sqliteStrICmp(zDatabase, db->aDb[j].zName) )
continue
;
p = sqliteHashFind(&db->aDb[j].tblHash, zName,
strlen
(zName)+1);
if
( p )
break
;
}
return
p;
}
Table *sqliteLocateTable(Parse *pParse,
const
char
*zName,
const
char
*zDbase){
Table *p;
p = sqliteFindTable(pParse->db, zName, zDbase);
if
( p==0 ){
if
( zDbase ){
sqliteErrorMsg(pParse,
"no such table: %s.%s"
, zDbase, zName);
}
else
if
( sqliteFindTable(pParse->db, zName, 0)!=0 ){
sqliteErrorMsg(pParse,
"table \"%s\" is not in database \"%s\""
,
zName, zDbase);
}
else
{
sqliteErrorMsg(pParse,
"no such table: %s"
, zName);
}
}
return
p;
}
Index *sqliteFindIndex(sqlite *db,
const
char
*zName,
const
char
*zDb){
Index *p = 0;
int
i;
for
(i=0; i<db->nDb; i++){
int
j = (i<2) ? i^1 : i;
if
( zDb && sqliteStrICmp(zDb, db->aDb[j].zName) )
continue
;
p = sqliteHashFind(&db->aDb[j].idxHash, zName,
strlen
(zName)+1);
if
( p )
break
;
}
return
p;
}
static
void
sqliteDeleteIndex(sqlite *db, Index *p){
Index *pOld;
assert
( db!=0 && p->zName!=0 );
pOld = sqliteHashInsert(&db->aDb[p->iDb].idxHash, p->zName,
strlen
(p->zName)+1, 0);
if
( pOld!=0 && pOld!=p ){
sqliteHashInsert(&db->aDb[p->iDb].idxHash, pOld->zName,
strlen
(pOld->zName)+1, pOld);
}
sqliteFree(p);
}
void
sqliteUnlinkAndDeleteIndex(sqlite *db, Index *pIndex){
if
( pIndex->pTable->pIndex==pIndex ){
pIndex->pTable->pIndex = pIndex->pNext;
}
else
{
Index *p;
for
(p=pIndex->pTable->pIndex; p && p->pNext!=pIndex; p=p->pNext){}
if
( p && p->pNext==pIndex ){
p->pNext = pIndex->pNext;
}
}
sqliteDeleteIndex(db, pIndex);
}
void
sqliteResetInternalSchema(sqlite *db,
int
iDb){
HashElem *pElem;
Hash temp1;
Hash temp2;
int
i, j;
assert
( iDb>=0 && iDb<db->nDb );
db->flags &= ~SQLITE_Initialized;
for
(i=iDb; i<db->nDb; i++){
Db *pDb = &db->aDb[i];
temp1 = pDb->tblHash;
temp2 = pDb->trigHash;
sqliteHashInit(&pDb->trigHash, SQLITE_HASH_STRING, 0);
sqliteHashClear(&pDb->aFKey);
sqliteHashClear(&pDb->idxHash);
for
(pElem=sqliteHashFirst(&temp2); pElem; pElem=sqliteHashNext(pElem)){
Trigger *pTrigger = sqliteHashData(pElem);
sqliteDeleteTrigger(pTrigger);
}
sqliteHashClear(&temp2);
sqliteHashInit(&pDb->tblHash, SQLITE_HASH_STRING, 0);
for
(pElem=sqliteHashFirst(&temp1); pElem; pElem=sqliteHashNext(pElem)){
Table *pTab = sqliteHashData(pElem);
sqliteDeleteTable(db, pTab);
}
sqliteHashClear(&temp1);
DbClearProperty(db, i, DB_SchemaLoaded);
if
( iDb>0 )
return
;
}
assert
( iDb==0 );
db->flags &= ~SQLITE_InternChanges;
for
(i=0; i<db->nDb; i++){
struct
Db *pDb = &db->aDb[i];
if
( pDb->pBt==0 ){
if
( pDb->pAux && pDb->xFreeAux ) pDb->xFreeAux(pDb->pAux);
pDb->pAux = 0;
}
}
for
(i=j=2; i<db->nDb; i++){
struct
Db *pDb = &db->aDb[i];
if
( pDb->pBt==0 ){
sqliteFree(pDb->zName);
pDb->zName = 0;
continue
;
}
if
( j<i ){
db->aDb[j] = db->aDb[i];
}
j++;
}
memset
(&db->aDb[j], 0, (db->nDb-j)*
sizeof
(db->aDb[j]));
db->nDb = j;
if
( db->nDb<=2 && db->aDb!=db->aDbStatic ){
memcpy
(db->aDbStatic, db->aDb, 2*
sizeof
(db->aDb[0]));
sqliteFree(db->aDb);
db->aDb = db->aDbStatic;
}
}
void
sqliteRollbackInternalChanges(sqlite *db){
if
( db->flags & SQLITE_InternChanges ){
sqliteResetInternalSchema(db, 0);
}
}
void
sqliteCommitInternalChanges(sqlite *db){
db->aDb[0].schema_cookie = db->next_cookie;
db->flags &= ~SQLITE_InternChanges;
}
void
sqliteDeleteTable(sqlite *db, Table *pTable){
int
i;
Index *pIndex, *pNext;
FKey *pFKey, *pNextFKey;
if
( pTable==0 )
return
;
for
(pIndex = pTable->pIndex; pIndex; pIndex=pNext){
pNext = pIndex->pNext;
assert
( pIndex->iDb==pTable->iDb || (pTable->iDb==0 && pIndex->iDb==1) );
sqliteDeleteIndex(db, pIndex);
}
for
(pFKey=pTable->pFKey; pFKey; pFKey=pNextFKey){
pNextFKey = pFKey->pNextFrom;
assert
( pTable->iDb<db->nDb );
assert
( sqliteHashFind(&db->aDb[pTable->iDb].aFKey,
pFKey->zTo,
strlen
(pFKey->zTo)+1)!=pFKey );
sqliteFree(pFKey);
}
for
(i=0; i<pTable->nCol; i++){
sqliteFree(pTable->aCol[i].zName);
sqliteFree(pTable->aCol[i].zDflt);
sqliteFree(pTable->aCol[i].zType);
}
sqliteFree(pTable->zName);
sqliteFree(pTable->aCol);
sqliteSelectDelete(pTable->pSelect);
sqliteFree(pTable);
}
static
void
sqliteUnlinkAndDeleteTable(sqlite *db, Table *p){
Table *pOld;
FKey *pF1, *pF2;
int
i = p->iDb;
assert
( db!=0 );
pOld = sqliteHashInsert(&db->aDb[i].tblHash, p->zName,
strlen
(p->zName)+1, 0);
assert
( pOld==0 || pOld==p );
for
(pF1=p->pFKey; pF1; pF1=pF1->pNextFrom){
int
nTo =
strlen
(pF1->zTo) + 1;
pF2 = sqliteHashFind(&db->aDb[i].aFKey, pF1->zTo, nTo);
if
( pF2==pF1 ){
sqliteHashInsert(&db->aDb[i].aFKey, pF1->zTo, nTo, pF1->pNextTo);
}
else
{
while
( pF2 && pF2->pNextTo!=pF1 ){ pF2=pF2->pNextTo; }
if
( pF2 ){
pF2->pNextTo = pF1->pNextTo;
}
}
}
sqliteDeleteTable(db, p);
}
char
*sqliteTableNameFromToken(Token *pName){
char
*zName = sqliteStrNDup(pName->z, pName->n);
sqliteDequote(zName);
return
zName;
}
void
sqliteOpenMasterTable(Vdbe *v,
int
isTemp){
sqliteVdbeAddOp(v, OP_Integer, isTemp, 0);
sqliteVdbeAddOp(v, OP_OpenWrite, 0, 2);
}
void
sqliteStartTable(
Parse *pParse,
Token *pStart,
Token *pName,
int
isTemp,
int
isView
){
Table *pTable;
Index *pIdx;
char
*zName;
sqlite *db = pParse->db;
Vdbe *v;
int
iDb;
pParse->sFirstToken = *pStart;
zName = sqliteTableNameFromToken(pName);
if
( zName==0 )
return
;
if
( db->init.iDb==1 ) isTemp = 1;
#ifndef SQLITE_OMIT_AUTHORIZATION
assert
( (isTemp & 1)==isTemp );
{
int
code;
char
*zDb = isTemp ?
"temp"
:
"main"
;
if
( sqliteAuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(isTemp), 0, zDb) ){
sqliteFree(zName);
return
;
}
if
( isView ){
if
( isTemp ){
code = SQLITE_CREATE_TEMP_VIEW;
}
else
{
code = SQLITE_CREATE_VIEW;
}
}
else
{
if
( isTemp ){
code = SQLITE_CREATE_TEMP_TABLE;
}
else
{
code = SQLITE_CREATE_TABLE;
}
}
if
( sqliteAuthCheck(pParse, code, zName, 0, zDb) ){
sqliteFree(zName);
return
;
}
}
#endif
if
( isTemp && db->aDb[1].pBt==0 && !pParse->explain ){
int
rc = sqliteBtreeFactory(db, 0, 0, MAX_PAGES, &db->aDb[1].pBt);
if
( rc!=SQLITE_OK ){
sqliteErrorMsg(pParse,
"unable to open a temporary database "
"file for storing temporary tables"
);
pParse->nErr++;
return
;
}
if
( db->flags & SQLITE_InTrans ){
rc = sqliteBtreeBeginTrans(db->aDb[1].pBt);
if
( rc!=SQLITE_OK ){
sqliteErrorMsg(pParse,
"unable to get a write lock on "
"the temporary database file"
);
return
;
}
}
}
pTable = sqliteFindTable(db, zName, 0);
iDb = isTemp ? 1 : db->init.iDb;
if
( pTable!=0 && (pTable->iDb==iDb || !db->init.busy) ){
sqliteErrorMsg(pParse,
"table %T already exists"
, pName);
sqliteFree(zName);
return
;
}
if
( (pIdx = sqliteFindIndex(db, zName, 0))!=0 &&
(pIdx->iDb==0 || !db->init.busy) ){
sqliteErrorMsg(pParse,
"there is already an index named %s"
, zName);
sqliteFree(zName);
return
;
}
pTable = sqliteMalloc(
sizeof
(Table) );
if
( pTable==0 ){
sqliteFree(zName);
return
;
}
pTable->zName = zName;
pTable->nCol = 0;
pTable->aCol = 0;
pTable->iPKey = -1;
pTable->pIndex = 0;
pTable->iDb = iDb;
if
( pParse->pNewTable ) sqliteDeleteTable(db, pParse->pNewTable);
pParse->pNewTable = pTable;
if
( !db->init.busy && (v = sqliteGetVdbe(pParse))!=0 ){
sqliteBeginWriteOperation(pParse, 0, isTemp);
if
( !isTemp ){
sqliteVdbeAddOp(v, OP_Integer, db->file_format, 0);
sqliteVdbeAddOp(v, OP_SetCookie, 0, 1);
}
sqliteOpenMasterTable(v, isTemp);
sqliteVdbeAddOp(v, OP_NewRecno, 0, 0);
sqliteVdbeAddOp(v, OP_Dup, 0, 0);
sqliteVdbeAddOp(v, OP_String, 0, 0);
sqliteVdbeAddOp(v, OP_PutIntKey, 0, 0);
}
}
void
sqliteAddColumn(Parse *pParse, Token *pName){
Table *p;
int
i;
char
*z = 0;
Column *pCol;
if
( (p = pParse->pNewTable)==0 )
return
;
sqliteSetNString(&z, pName->z, pName->n, 0);
if
( z==0 )
return
;
sqliteDequote(z);
for
(i=0; i<p->nCol; i++){
if
( sqliteStrICmp(z, p->aCol[i].zName)==0 ){
sqliteErrorMsg(pParse,
"duplicate column name: %s"
, z);
sqliteFree(z);
return
;
}
}
if
( (p->nCol & 0x7)==0 ){
Column *aNew;
aNew = sqliteRealloc( p->aCol, (p->nCol+8)*
sizeof
(p->aCol[0]));
if
( aNew==0 )
return
;
p->aCol = aNew;
}
pCol = &p->aCol[p->nCol];
memset
(pCol, 0,
sizeof
(p->aCol[0]));
pCol->zName = z;
pCol->sortOrder = SQLITE_SO_NUM;
p->nCol++;
}
void
sqliteAddNotNull(Parse *pParse,
int
onError){
Table *p;
int
i;
if
( (p = pParse->pNewTable)==0 )
return
;
i = p->nCol-1;
if
( i>=0 ) p->aCol[i].notNull = onError;
}
void
sqliteAddColumnType(Parse *pParse, Token *pFirst, Token *pLast){
Table *p;
int
i, j;
int
n;
char
*z, **pz;
Column *pCol;
if
( (p = pParse->pNewTable)==0 )
return
;
i = p->nCol-1;
if
( i<0 )
return
;
pCol = &p->aCol[i];
pz = &pCol->zType;
n = pLast->n + Addr(pLast->z) - Addr(pFirst->z);
sqliteSetNString(pz, pFirst->z, n, 0);
z = *pz;
if
( z==0 )
return
;
for
(i=j=0; z[i]; i++){
int
c = z[i];
if
(
isspace
(c) )
continue
;
z[j++] = c;
}
z[j] = 0;
if
( pParse->db->file_format>=4 ){
pCol->sortOrder = sqliteCollateType(z, n);
}
else
{
pCol->sortOrder = SQLITE_SO_NUM;
}
}
void
sqliteAddDefaultValue(Parse *pParse, Token *pVal,
int
minusFlag){
Table *p;
int
i;
char
**pz;
if
( (p = pParse->pNewTable)==0 )
return
;
i = p->nCol-1;
if
( i<0 )
return
;
pz = &p->aCol[i].zDflt;
if
( minusFlag ){
sqliteSetNString(pz,
"-"
, 1, pVal->z, pVal->n, 0);
}
else
{
sqliteSetNString(pz, pVal->z, pVal->n, 0);
}
sqliteDequote(*pz);
}
void
sqliteAddPrimaryKey(Parse *pParse, IdList *pList,
int
onError){
Table *pTab = pParse->pNewTable;
char
*zType = 0;
int
iCol = -1, i;
if
( pTab==0 )
goto
primary_key_exit;
if
( pTab->hasPrimKey ){
sqliteErrorMsg(pParse,
"table \"%s\" has more than one primary key"
, pTab->zName);
goto
primary_key_exit;
}
pTab->hasPrimKey = 1;
if
( pList==0 ){
iCol = pTab->nCol - 1;
pTab->aCol[iCol].isPrimKey = 1;
}
else
{
for
(i=0; i<pList->nId; i++){
for
(iCol=0; iCol<pTab->nCol; iCol++){
if
( sqliteStrICmp(pList->a[i].zName, pTab->aCol[iCol].zName)==0 )
break
;
}
if
( iCol<pTab->nCol ) pTab->aCol[iCol].isPrimKey = 1;
}
if
( pList->nId>1 ) iCol = -1;
}
if
( iCol>=0 && iCol<pTab->nCol ){
zType = pTab->aCol[iCol].zType;
}
if
( pParse->db->file_format>=1 &&
zType && sqliteStrICmp(zType,
"INTEGER"
)==0 ){
pTab->iPKey = iCol;
pTab->keyConf = onError;
}
else
{
sqliteCreateIndex(pParse, 0, 0, pList, onError, 0, 0);
pList = 0;
}
primary_key_exit:
sqliteIdListDelete(pList);
return
;
}
int
sqliteCollateType(
const
char
*zType,
int
nType){
int
i;
for
(i=0; i<nType-3; i++){
int
c = *(zType++) | 0x60;
if
( (c==
'b'
|| c==
'c'
) && sqliteStrNICmp(zType,
"lob"
, 3)==0 ){
return
SQLITE_SO_TEXT;
}
if
( c==
'c'
&& sqliteStrNICmp(zType,
"har"
, 3)==0 ){
return
SQLITE_SO_TEXT;
}
if
( c==
't'
&& sqliteStrNICmp(zType,
"ext"
, 3)==0 ){
return
SQLITE_SO_TEXT;
}
}
return
SQLITE_SO_NUM;
}
void
sqliteAddCollateType(Parse *pParse,
int
collType){
Table *p;
int
i;
if
( (p = pParse->pNewTable)==0 )
return
;
i = p->nCol-1;
if
( i>=0 ) p->aCol[i].sortOrder = collType;
}
void
sqliteChangeCookie(sqlite *db, Vdbe *v){
if
( db->next_cookie==db->aDb[0].schema_cookie ){
unsigned
char
r;
sqliteRandomness(1, &r);
db->next_cookie = db->aDb[0].schema_cookie + r + 1;
db->flags |= SQLITE_InternChanges;
sqliteVdbeAddOp(v, OP_Integer, db->next_cookie, 0);
sqliteVdbeAddOp(v, OP_SetCookie, 0, 0);
}
}
static
int
identLength(
const
char
*z){
int
n;
int
needQuote = 0;
for
(n=0; *z; n++, z++){
if
( *z==
'\''
){ n++; needQuote=1; }
}
return
n + needQuote*2;
}
static
void
identPut(
char
*z,
int
*pIdx,
char
*zIdent){
int
i, j, needQuote;
i = *pIdx;
for
(j=0; zIdent[j]; j++){
if
( !
isalnum
(zIdent[j]) && zIdent[j]!=
'_'
)
break
;
}
needQuote = zIdent[j]!=0 ||
isdigit
(zIdent[0])
|| sqliteKeywordCode(zIdent, j)!=TK_ID;
if
( needQuote ) z[i++] =
'\''
;
for
(j=0; zIdent[j]; j++){
z[i++] = zIdent[j];
if
( zIdent[j]==
'\''
) z[i++] =
'\''
;
}
if
( needQuote ) z[i++] =
'\''
;
z[i] = 0;
*pIdx = i;
}
static
char
*createTableStmt(Table *p){
int
i, k, n;
char
*zStmt;
char
*zSep, *zSep2, *zEnd;
n = 0;
for
(i=0; i<p->nCol; i++){
n += identLength(p->aCol[i].zName);
}
n += identLength(p->zName);
if
( n<40 ){
zSep =
""
;
zSep2 =
","
;
zEnd =
")"
;
}
else
{
zSep =
"\n "
;
zSep2 =
",\n "
;
zEnd =
"\n)"
;
}
n += 35 + 6*p->nCol;
zStmt = sqliteMallocRaw( n );
if
( zStmt==0 )
return
0;
strcpy
(zStmt, p->iDb==1 ?
"CREATE TEMP TABLE "
:
"CREATE TABLE "
);
k =
strlen
(zStmt);
identPut(zStmt, &k, p->zName);
zStmt[k++] =
'('
;
for
(i=0; i<p->nCol; i++){
strcpy
(&zStmt[k], zSep);
k +=
strlen
(&zStmt[k]);
zSep = zSep2;
identPut(zStmt, &k, p->aCol[i].zName);
}
strcpy
(&zStmt[k], zEnd);
return
zStmt;
}
void
sqliteEndTable(Parse *pParse, Token *pEnd, Select *pSelect){
Table *p;
sqlite *db = pParse->db;
if
( (pEnd==0 && pSelect==0) || pParse->nErr || sqlite_malloc_failed )
return
;
p = pParse->pNewTable;
if
( p==0 )
return
;
if
( pSelect ){
Table *pSelTab = sqliteResultSetOfSelect(pParse, 0, pSelect);
if
( pSelTab==0 )
return
;
assert
( p->aCol==0 );
p->nCol = pSelTab->nCol;
p->aCol = pSelTab->aCol;
pSelTab->nCol = 0;
pSelTab->aCol = 0;
sqliteDeleteTable(0, pSelTab);
}
if
( db->init.busy ){
p->tnum = db->init.newTnum;
}
if
( !db->init.busy ){
int
n;
Vdbe *v;
v = sqliteGetVdbe(pParse);
if
( v==0 )
return
;
if
( p->pSelect==0 ){
sqliteVdbeOp3(v, OP_CreateTable, 0, p->iDb, (
char
*)&p->tnum, P3_POINTER);
}
else
{
sqliteVdbeAddOp(v, OP_Integer, 0, 0);
}
p->tnum = 0;
sqliteVdbeAddOp(v, OP_Pull, 1, 0);
sqliteVdbeOp3(v, OP_String, 0, 0, p->pSelect==0?
"table"
:
"view"
, P3_STATIC);
sqliteVdbeOp3(v, OP_String, 0, 0, p->zName, 0);
sqliteVdbeOp3(v, OP_String, 0, 0, p->zName, 0);
sqliteVdbeAddOp(v, OP_Dup, 4, 0);
sqliteVdbeAddOp(v, OP_String, 0, 0);
if
( pSelect ){
char
*z = createTableStmt(p);
n = z ?
strlen
(z) : 0;
sqliteVdbeChangeP3(v, -1, z, n);
sqliteFree(z);
}
else
{
assert
( pEnd!=0 );
n = Addr(pEnd->z) - Addr(pParse->sFirstToken.z) + 1;
sqliteVdbeChangeP3(v, -1, pParse->sFirstToken.z, n);
}
sqliteVdbeAddOp(v, OP_MakeRecord, 5, 0);
sqliteVdbeAddOp(v, OP_PutIntKey, 0, 0);
if
( !p->iDb ){
sqliteChangeCookie(db, v);
}
sqliteVdbeAddOp(v, OP_Close, 0, 0);
if
( pSelect ){
sqliteVdbeAddOp(v, OP_Integer, p->iDb, 0);
sqliteVdbeAddOp(v, OP_OpenWrite, 1, 0);
pParse->nTab = 2;
sqliteSelect(pParse, pSelect, SRT_Table, 1, 0, 0, 0);
}
sqliteEndWriteOperation(pParse);
}
if
( pParse->explain==0 && pParse->nErr==0 ){
Table *pOld;
FKey *pFKey;
pOld = sqliteHashInsert(&db->aDb[p->iDb].tblHash,
p->zName,
strlen
(p->zName)+1, p);
if
( pOld ){
assert
( p==pOld );
return
;
}
for
(pFKey=p->pFKey; pFKey; pFKey=pFKey->pNextFrom){
int
nTo =
strlen
(pFKey->zTo) + 1;
pFKey->pNextTo = sqliteHashFind(&db->aDb[p->iDb].aFKey, pFKey->zTo, nTo);
sqliteHashInsert(&db->aDb[p->iDb].aFKey, pFKey->zTo, nTo, pFKey);
}
pParse->pNewTable = 0;
db->nTable++;
db->flags |= SQLITE_InternChanges;
}
}
void
sqliteCreateView(
Parse *pParse,
Token *pBegin,
Token *pName,
Select *pSelect,
int
isTemp
){
Table *p;
int
n;
const
char
*z;
Token sEnd;
DbFixer sFix;
sqliteStartTable(pParse, pBegin, pName, isTemp, 1);
p = pParse->pNewTable;
if
( p==0 || pParse->nErr ){
sqliteSelectDelete(pSelect);
return
;
}
if
( sqliteFixInit(&sFix, pParse, p->iDb,
"view"
, pName)
&& sqliteFixSelect(&sFix, pSelect)
){
sqliteSelectDelete(pSelect);
return
;
}
p->pSelect = sqliteSelectDup(pSelect);
sqliteSelectDelete(pSelect);
if
( !pParse->db->init.busy ){
sqliteViewGetColumnNames(pParse, p);
}
sEnd = pParse->sLastToken;
if
( sEnd.z[0]!=0 && sEnd.z[0]!=
';'
){
sEnd.z += sEnd.n;
}
sEnd.n = 0;
n = sEnd.z - pBegin->z;
z = pBegin->z;
while
( n>0 && (z[n-1]==
';'
||
isspace
(z[n-1])) ){ n--; }
sEnd.z = &z[n-1];
sEnd.n = 1;
sqliteEndTable(pParse, &sEnd, 0);
return
;
}
int
sqliteViewGetColumnNames(Parse *pParse, Table *pTable){
ExprList *pEList;
Select *pSel;
Table *pSelTab;
int
nErr = 0;
assert
( pTable );
if
( pTable->nCol>0 )
return
0;
if
( pTable->nCol<0 ){
sqliteErrorMsg(pParse,
"view %s is circularly defined"
, pTable->zName);
return
1;
}
assert
( pTable->pSelect );
pSel = pTable->pSelect;
pEList = pSel->pEList;
pSel->pEList = sqliteExprListDup(pEList);
if
( pSel->pEList==0 ){
pSel->pEList = pEList;
return
1;
}
pTable->nCol = -1;
pSelTab = sqliteResultSetOfSelect(pParse, 0, pSel);
if
( pSelTab ){
assert
( pTable->aCol==0 );
pTable->nCol = pSelTab->nCol;
pTable->aCol = pSelTab->aCol;
pSelTab->nCol = 0;
pSelTab->aCol = 0;
sqliteDeleteTable(0, pSelTab);
DbSetProperty(pParse->db, pTable->iDb, DB_UnresetViews);
}
else
{
pTable->nCol = 0;
nErr++;
}
sqliteSelectUnbind(pSel);
sqliteExprListDelete(pSel->pEList);
pSel->pEList = pEList;
return
nErr;
}
static
void
sqliteViewResetColumnNames(Table *pTable){
int
i;
Column *pCol;
assert
( pTable!=0 && pTable->pSelect!=0 );
for
(i=0, pCol=pTable->aCol; i<pTable->nCol; i++, pCol++){
sqliteFree(pCol->zName);
sqliteFree(pCol->zDflt);
sqliteFree(pCol->zType);
}
sqliteFree(pTable->aCol);
pTable->aCol = 0;
pTable->nCol = 0;
}
static
void
sqliteViewResetAll(sqlite *db,
int
idx){
HashElem *i;
if
( !DbHasProperty(db, idx, DB_UnresetViews) )
return
;
for
(i=sqliteHashFirst(&db->aDb[idx].tblHash); i; i=sqliteHashNext(i)){
Table *pTab = sqliteHashData(i);
if
( pTab->pSelect ){
sqliteViewResetColumnNames(pTab);
}
}
DbClearProperty(db, idx, DB_UnresetViews);
}
Table *sqliteTableFromToken(Parse *pParse, Token *pTok){
char
*zName;
Table *pTab;
zName = sqliteTableNameFromToken(pTok);
if
( zName==0 )
return
0;
pTab = sqliteFindTable(pParse->db, zName, 0);
sqliteFree(zName);
if
( pTab==0 ){
sqliteErrorMsg(pParse,
"no such table: %T"
, pTok);
}
return
pTab;
}
void
sqliteDropTable(Parse *pParse, Token *pName,
int
isView){
Table *pTable;
Vdbe *v;
int
base;
sqlite *db = pParse->db;
int
iDb;
if
( pParse->nErr || sqlite_malloc_failed )
return
;
pTable = sqliteTableFromToken(pParse, pName);
if
( pTable==0 )
return
;
iDb = pTable->iDb;
assert
( iDb>=0 && iDb<db->nDb );
#ifndef SQLITE_OMIT_AUTHORIZATION
{
int
code;
const
char
*zTab = SCHEMA_TABLE(pTable->iDb);
const
char
*zDb = db->aDb[pTable->iDb].zName;
if
( sqliteAuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb)){
return
;
}
if
( isView ){
if
( iDb==1 ){
code = SQLITE_DROP_TEMP_VIEW;
}
else
{
code = SQLITE_DROP_VIEW;
}
}
else
{
if
( iDb==1 ){
code = SQLITE_DROP_TEMP_TABLE;
}
else
{
code = SQLITE_DROP_TABLE;
}
}
if
( sqliteAuthCheck(pParse, code, pTable->zName, 0, zDb) ){
return
;
}
if
( sqliteAuthCheck(pParse, SQLITE_DELETE, pTable->zName, 0, zDb) ){
return
;
}
}
#endif
if
( pTable->readOnly ){
sqliteErrorMsg(pParse,
"table %s may not be dropped"
, pTable->zName);
pParse->nErr++;
return
;
}
if
( isView && pTable->pSelect==0 ){
sqliteErrorMsg(pParse,
"use DROP TABLE to delete table %s"
, pTable->zName);
return
;
}
if
( !isView && pTable->pSelect ){
sqliteErrorMsg(pParse,
"use DROP VIEW to delete view %s"
, pTable->zName);
return
;
}
v = sqliteGetVdbe(pParse);
if
( v ){
static
VdbeOpList dropTable[] = {
{ OP_Rewind, 0, ADDR(8), 0},
{ OP_String, 0, 0, 0},
{ OP_MemStore, 1, 1, 0},
{ OP_MemLoad, 1, 0, 0},
{ OP_Column, 0, 2, 0},
{ OP_Ne, 0, ADDR(7), 0},
{ OP_Delete, 0, 0, 0},
{ OP_Next, 0, ADDR(3), 0},
};
Index *pIdx;
Trigger *pTrigger;
sqliteBeginWriteOperation(pParse, 0, pTable->iDb);
pTrigger = pTable->pTrigger;
while
( pTrigger ){
assert
( pTrigger->iDb==pTable->iDb || pTrigger->iDb==1 );
sqliteDropTriggerPtr(pParse, pTrigger, 1);
if
( pParse->explain ){
pTrigger = pTrigger->pNext;
}
else
{
pTrigger = pTable->pTrigger;
}
}
sqliteOpenMasterTable(v, pTable->iDb);
base = sqliteVdbeAddOpList(v, ArraySize(dropTable), dropTable);
sqliteVdbeChangeP3(v, base+1, pTable->zName, 0);
if
( pTable->iDb!=1 ){
sqliteOpenMasterTable(v, 1);
base = sqliteVdbeAddOpList(v, ArraySize(dropTable), dropTable);
sqliteVdbeChangeP3(v, base+1, pTable->zName, 0);
}
if
( pTable->iDb==0 ){
sqliteChangeCookie(db, v);
}
sqliteVdbeAddOp(v, OP_Close, 0, 0);
if
( !isView ){
sqliteVdbeAddOp(v, OP_Destroy, pTable->tnum, pTable->iDb);
for
(pIdx=pTable->pIndex; pIdx; pIdx=pIdx->pNext){
sqliteVdbeAddOp(v, OP_Destroy, pIdx->tnum, pIdx->iDb);
}
}
sqliteEndWriteOperation(pParse);
}
if
( !pParse->explain ){
sqliteUnlinkAndDeleteTable(db, pTable);
db->flags |= SQLITE_InternChanges;
}
sqliteViewResetAll(db, iDb);
}
void
sqliteAddIdxKeyType(Vdbe *v, Index *pIdx){
char
*zType;
Table *pTab;
int
i, n;
assert
( pIdx!=0 && pIdx->pTable!=0 );
pTab = pIdx->pTable;
n = pIdx->nColumn;
zType = sqliteMallocRaw( n+1 );
if
( zType==0 )
return
;
for
(i=0; i<n; i++){
int
iCol = pIdx->aiColumn[i];
assert
( iCol>=0 && iCol<pTab->nCol );
if
( (pTab->aCol[iCol].sortOrder & SQLITE_SO_TYPEMASK)==SQLITE_SO_TEXT ){
zType[i] =
't'
;
}
else
{
zType[i] =
'n'
;
}
}
zType[n] = 0;
sqliteVdbeChangeP3(v, -1, zType, n);
sqliteFree(zType);
}
void
sqliteCreateForeignKey(
Parse *pParse,
IdList *pFromCol,
Token *pTo,
IdList *pToCol,
int
flags
){
Table *p = pParse->pNewTable;
int
nByte;
int
i;
int
nCol;
char
*z;
FKey *pFKey = 0;
assert
( pTo!=0 );
if
( p==0 || pParse->nErr )
goto
fk_end;
if
( pFromCol==0 ){
int
iCol = p->nCol-1;
if
( iCol<0 )
goto
fk_end;
if
( pToCol && pToCol->nId!=1 ){
sqliteErrorMsg(pParse,
"foreign key on %s"
" should reference only one column of table %T"
,
p->aCol[iCol].zName, pTo);
goto
fk_end;
}
nCol = 1;
}
else
if
( pToCol && pToCol->nId!=pFromCol->nId ){
sqliteErrorMsg(pParse,
"number of columns in foreign key does not match the number of "
"columns in the referenced table"
);
goto
fk_end;
}
else
{
nCol = pFromCol->nId;
}
nByte =
sizeof
(*pFKey) + nCol*
sizeof
(pFKey->aCol[0]) + pTo->n + 1;
if
( pToCol ){
for
(i=0; i<pToCol->nId; i++){
nByte +=
strlen
(pToCol->a[i].zName) + 1;
}
}
pFKey = sqliteMalloc( nByte );
if
( pFKey==0 )
goto
fk_end;
pFKey->pFrom = p;
pFKey->pNextFrom = p->pFKey;
z = (
char
*)&pFKey[1];
pFKey->aCol = (
struct
sColMap*)z;
z +=
sizeof
(
struct
sColMap)*nCol;
pFKey->zTo = z;
memcpy
(z, pTo->z, pTo->n);
z[pTo->n] = 0;
z += pTo->n+1;
pFKey->pNextTo = 0;
pFKey->nCol = nCol;
if
( pFromCol==0 ){
pFKey->aCol[0].iFrom = p->nCol-1;
}
else
{
for
(i=0; i<nCol; i++){
int
j;
for
(j=0; j<p->nCol; j++){
if
( sqliteStrICmp(p->aCol[j].zName, pFromCol->a[i].zName)==0 ){
pFKey->aCol[i].iFrom = j;
break
;
}
}
if
( j>=p->nCol ){
sqliteErrorMsg(pParse,
"unknown column \"%s\" in foreign key definition"
,
pFromCol->a[i].zName);
goto
fk_end;
}
}
}
if
( pToCol ){
for
(i=0; i<nCol; i++){
int
n =
strlen
(pToCol->a[i].zName);
pFKey->aCol[i].zCol = z;
memcpy
(z, pToCol->a[i].zName, n);
z[n] = 0;
z += n+1;
}
}
pFKey->isDeferred = 0;
pFKey->deleteConf = flags & 0xff;
pFKey->updateConf = (flags >> 8 ) & 0xff;
pFKey->insertConf = (flags >> 16 ) & 0xff;
p->pFKey = pFKey;
pFKey = 0;
fk_end:
sqliteFree(pFKey);
sqliteIdListDelete(pFromCol);
sqliteIdListDelete(pToCol);
}
void
sqliteDeferForeignKey(Parse *pParse,
int
isDeferred){
Table *pTab;
FKey *pFKey;
if
( (pTab = pParse->pNewTable)==0 || (pFKey = pTab->pFKey)==0 )
return
;
pFKey->isDeferred = isDeferred;
}
void
sqliteCreateIndex(
Parse *pParse,
Token *pName,
SrcList *pTable,
IdList *pList,
int
onError,
Token *pStart,
Token *pEnd
){
Table *pTab;
Index *pIndex;
char
*zName = 0;
int
i, j;
Token nullId;
DbFixer sFix;
int
isTemp;
sqlite *db = pParse->db;
if
( pParse->nErr || sqlite_malloc_failed )
goto
exit_create_index;
if
( db->init.busy
&& sqliteFixInit(&sFix, pParse, db->init.iDb,
"index"
, pName)
&& sqliteFixSrcList(&sFix, pTable)
){
goto
exit_create_index;
}
if
( pTable!=0 ){
assert
( pName!=0 );
assert
( pTable->nSrc==1 );
pTab = sqliteSrcListLookup(pParse, pTable);
}
else
{
assert
( pName==0 );
pTab = pParse->pNewTable;
}
if
( pTab==0 || pParse->nErr )
goto
exit_create_index;
if
( pTab->readOnly ){
sqliteErrorMsg(pParse,
"table %s may not be indexed"
, pTab->zName);
goto
exit_create_index;
}
if
( pTab->iDb>=2 && db->init.busy==0 ){
sqliteErrorMsg(pParse,
"table %s may not have indices added"
, pTab->zName);
goto
exit_create_index;
}
if
( pTab->pSelect ){
sqliteErrorMsg(pParse,
"views may not be indexed"
);
goto
exit_create_index;
}
isTemp = pTab->iDb==1;
if
( pName && !db->init.busy ){
Index *pISameName;
Table *pTSameName;
zName = sqliteTableNameFromToken(pName);
if
( zName==0 )
goto
exit_create_index;
if
( (pISameName = sqliteFindIndex(db, zName, 0))!=0 ){
sqliteErrorMsg(pParse,
"index %s already exists"
, zName);
goto
exit_create_index;
}
if
( (pTSameName = sqliteFindTable(db, zName, 0))!=0 ){
sqliteErrorMsg(pParse,
"there is already a table named %s"
, zName);
goto
exit_create_index;
}
}
else
if
( pName==0 ){
char
zBuf[30];
int
n;
Index *pLoop;
for
(pLoop=pTab->pIndex, n=1; pLoop; pLoop=pLoop->pNext, n++){}
sprintf
(zBuf,
"%d)"
,n);
zName = 0;
sqliteSetString(&zName,
"("
, pTab->zName,
" autoindex "
, zBuf, (
char
*)0);
if
( zName==0 )
goto
exit_create_index;
}
else
{
zName = sqliteStrNDup(pName->z, pName->n);
}
#ifndef SQLITE_OMIT_AUTHORIZATION
{
const
char
*zDb = db->aDb[pTab->iDb].zName;
assert
( pTab->iDb==db->init.iDb || isTemp );
if
( sqliteAuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(isTemp), 0, zDb) ){
goto
exit_create_index;
}
i = SQLITE_CREATE_INDEX;
if
( isTemp ) i = SQLITE_CREATE_TEMP_INDEX;
if
( sqliteAuthCheck(pParse, i, zName, pTab->zName, zDb) ){
goto
exit_create_index;
}
}
#endif
if
( pList==0 ){
nullId.z = pTab->aCol[pTab->nCol-1].zName;
nullId.n =
strlen
(nullId.z);
pList = sqliteIdListAppend(0, &nullId);
if
( pList==0 )
goto
exit_create_index;
}
pIndex = sqliteMalloc(
sizeof
(Index) +
strlen
(zName) + 1 +
sizeof
(
int
)*pList->nId );
if
( pIndex==0 )
goto
exit_create_index;
pIndex->aiColumn = (
int
*)&pIndex[1];
pIndex->zName = (
char
*)&pIndex->aiColumn[pList->nId];
strcpy
(pIndex->zName, zName);
pIndex->pTable = pTab;
pIndex->nColumn = pList->nId;
pIndex->onError = onError;
pIndex->autoIndex = pName==0;
pIndex->iDb = isTemp ? 1 : db->init.iDb;
for
(i=0; i<pList->nId; i++){
for
(j=0; j<pTab->nCol; j++){
if
( sqliteStrICmp(pList->a[i].zName, pTab->aCol[j].zName)==0 )
break
;
}
if
( j>=pTab->nCol ){
sqliteErrorMsg(pParse,
"table %s has no column named %s"
,
pTab->zName, pList->a[i].zName);
sqliteFree(pIndex);
goto
exit_create_index;
}
pIndex->aiColumn[i] = j;
}
if
( !pParse->explain ){
Index *p;
p = sqliteHashInsert(&db->aDb[pIndex->iDb].idxHash,
pIndex->zName,
strlen
(pIndex->zName)+1, pIndex);
if
( p ){
assert
( p==pIndex );
sqliteFree(pIndex);
goto
exit_create_index;
}
db->flags |= SQLITE_InternChanges;
}
if
( onError!=OE_Replace || pTab->pIndex==0
|| pTab->pIndex->onError==OE_Replace){
pIndex->pNext = pTab->pIndex;
pTab->pIndex = pIndex;
}
else
{
Index *pOther = pTab->pIndex;
while
( pOther->pNext && pOther->pNext->onError!=OE_Replace ){
pOther = pOther->pNext;
}
pIndex->pNext = pOther->pNext;
pOther->pNext = pIndex;
}
if
( db->init.busy && pTable!=0 ){
pIndex->tnum = db->init.newTnum;
}
else
if
( db->init.busy==0 ){
int
n;
Vdbe *v;
int
lbl1, lbl2;
int
i;
int
addr;
v = sqliteGetVdbe(pParse);
if
( v==0 )
goto
exit_create_index;
if
( pTable!=0 ){
sqliteBeginWriteOperation(pParse, 0, isTemp);
sqliteOpenMasterTable(v, isTemp);
}
sqliteVdbeAddOp(v, OP_NewRecno, 0, 0);
sqliteVdbeOp3(v, OP_String, 0, 0,
"index"
, P3_STATIC);
sqliteVdbeOp3(v, OP_String, 0, 0, pIndex->zName, 0);
sqliteVdbeOp3(v, OP_String, 0, 0, pTab->zName, 0);
sqliteVdbeOp3(v, OP_CreateIndex, 0, isTemp,(
char
*)&pIndex->tnum,P3_POINTER);
pIndex->tnum = 0;
if
( pTable ){
sqliteVdbeCode(v,
OP_Dup, 0, 0,
OP_Integer, isTemp, 0,
OP_OpenWrite, 1, 0,
0);
}
addr = sqliteVdbeAddOp(v, OP_String, 0, 0);
if
( pStart && pEnd ){
n = Addr(pEnd->z) - Addr(pStart->z) + 1;
sqliteVdbeChangeP3(v, addr, pStart->z, n);
}
sqliteVdbeAddOp(v, OP_MakeRecord, 5, 0);
sqliteVdbeAddOp(v, OP_PutIntKey, 0, 0);
if
( pTable ){
sqliteVdbeAddOp(v, OP_Integer, pTab->iDb, 0);
sqliteVdbeOp3(v, OP_OpenRead, 2, pTab->tnum, pTab->zName, 0);
lbl2 = sqliteVdbeMakeLabel(v);
sqliteVdbeAddOp(v, OP_Rewind, 2, lbl2);
lbl1 = sqliteVdbeAddOp(v, OP_Recno, 2, 0);
for
(i=0; i<pIndex->nColumn; i++){
int
iCol = pIndex->aiColumn[i];
if
( pTab->iPKey==iCol ){
sqliteVdbeAddOp(v, OP_Dup, i, 0);
}
else
{
sqliteVdbeAddOp(v, OP_Column, 2, iCol);
}
}
sqliteVdbeAddOp(v, OP_MakeIdxKey, pIndex->nColumn, 0);
if
( db->file_format>=4 ) sqliteAddIdxKeyType(v, pIndex);
sqliteVdbeOp3(v, OP_IdxPut, 1, pIndex->onError!=OE_None,
"indexed columns are not unique"
, P3_STATIC);
sqliteVdbeAddOp(v, OP_Next, 2, lbl1);
sqliteVdbeResolveLabel(v, lbl2);
sqliteVdbeAddOp(v, OP_Close, 2, 0);
sqliteVdbeAddOp(v, OP_Close, 1, 0);
}
if
( pTable!=0 ){
if
( !isTemp ){
sqliteChangeCookie(db, v);
}
sqliteVdbeAddOp(v, OP_Close, 0, 0);
sqliteEndWriteOperation(pParse);
}
}
exit_create_index:
sqliteIdListDelete(pList);
sqliteSrcListDelete(pTable);
sqliteFree(zName);
return
;
}
void
sqliteDropIndex(Parse *pParse, SrcList *pName){
Index *pIndex;
Vdbe *v;
sqlite *db = pParse->db;
if
( pParse->nErr || sqlite_malloc_failed )
return
;
assert
( pName->nSrc==1 );
pIndex = sqliteFindIndex(db, pName->a[0].zName, pName->a[0].zDatabase);
if
( pIndex==0 ){
sqliteErrorMsg(pParse,
"no such index: %S"
, pName, 0);
goto
exit_drop_index;
}
if
( pIndex->autoIndex ){
sqliteErrorMsg(pParse,
"index associated with UNIQUE "
"or PRIMARY KEY constraint cannot be dropped"
, 0);
goto
exit_drop_index;
}
if
( pIndex->iDb>1 ){
sqliteErrorMsg(pParse,
"cannot alter schema of attached "
"databases"
, 0);
goto
exit_drop_index;
}
#ifndef SQLITE_OMIT_AUTHORIZATION
{
int
code = SQLITE_DROP_INDEX;
Table *pTab = pIndex->pTable;
const
char
*zDb = db->aDb[pIndex->iDb].zName;
const
char
*zTab = SCHEMA_TABLE(pIndex->iDb);
if
( sqliteAuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){
goto
exit_drop_index;
}
if
( pIndex->iDb ) code = SQLITE_DROP_TEMP_INDEX;
if
( sqliteAuthCheck(pParse, code, pIndex->zName, pTab->zName, zDb) ){
goto
exit_drop_index;
}
}
#endif
v = sqliteGetVdbe(pParse);
if
( v ){
static
VdbeOpList dropIndex[] = {
{ OP_Rewind, 0, ADDR(9), 0},
{ OP_String, 0, 0, 0},
{ OP_MemStore, 1, 1, 0},
{ OP_MemLoad, 1, 0, 0},
{ OP_Column, 0, 1, 0},
{ OP_Eq, 0, ADDR(8), 0},
{ OP_Next, 0, ADDR(3), 0},
{ OP_Goto, 0, ADDR(9), 0},
{ OP_Delete, 0, 0, 0},
};
int
base;
sqliteBeginWriteOperation(pParse, 0, pIndex->iDb);
sqliteOpenMasterTable(v, pIndex->iDb);
base = sqliteVdbeAddOpList(v, ArraySize(dropIndex), dropIndex);
sqliteVdbeChangeP3(v, base+1, pIndex->zName, 0);
if
( pIndex->iDb==0 ){
sqliteChangeCookie(db, v);
}
sqliteVdbeAddOp(v, OP_Close, 0, 0);
sqliteVdbeAddOp(v, OP_Destroy, pIndex->tnum, pIndex->iDb);
sqliteEndWriteOperation(pParse);
}
if
( !pParse->explain ){
sqliteUnlinkAndDeleteIndex(db, pIndex);
db->flags |= SQLITE_InternChanges;
}
exit_drop_index:
sqliteSrcListDelete(pName);
}
IdList *sqliteIdListAppend(IdList *pList, Token *pToken){
if
( pList==0 ){
pList = sqliteMalloc(
sizeof
(IdList) );
if
( pList==0 )
return
0;
pList->nAlloc = 0;
}
if
( pList->nId>=pList->nAlloc ){
struct
IdList_item *a;
pList->nAlloc = pList->nAlloc*2 + 5;
a = sqliteRealloc(pList->a, pList->nAlloc*
sizeof
(pList->a[0]) );
if
( a==0 ){
sqliteIdListDelete(pList);
return
0;
}
pList->a = a;
}
memset
(&pList->a[pList->nId], 0,
sizeof
(pList->a[0]));
if
( pToken ){
char
**pz = &pList->a[pList->nId].zName;
sqliteSetNString(pz, pToken->z, pToken->n, 0);
if
( *pz==0 ){
sqliteIdListDelete(pList);
return
0;
}
else
{
sqliteDequote(*pz);
}
}
pList->nId++;
return
pList;
}
SrcList *sqliteSrcListAppend(SrcList *pList, Token *pTable, Token *pDatabase){
if
( pList==0 ){
pList = sqliteMalloc(
sizeof
(SrcList) );
if
( pList==0 )
return
0;
pList->nAlloc = 1;
}
if
( pList->nSrc>=pList->nAlloc ){
SrcList *pNew;
pList->nAlloc *= 2;
pNew = sqliteRealloc(pList,
sizeof
(*pList) + (pList->nAlloc-1)*
sizeof
(pList->a[0]) );
if
( pNew==0 ){
sqliteSrcListDelete(pList);
return
0;
}
pList = pNew;
}
memset
(&pList->a[pList->nSrc], 0,
sizeof
(pList->a[0]));
if
( pDatabase && pDatabase->z==0 ){
pDatabase = 0;
}
if
( pDatabase && pTable ){
Token *pTemp = pDatabase;
pDatabase = pTable;
pTable = pTemp;
}
if
( pTable ){
char
**pz = &pList->a[pList->nSrc].zName;
sqliteSetNString(pz, pTable->z, pTable->n, 0);
if
( *pz==0 ){
sqliteSrcListDelete(pList);
return
0;
}
else
{
sqliteDequote(*pz);
}
}
if
( pDatabase ){
char
**pz = &pList->a[pList->nSrc].zDatabase;
sqliteSetNString(pz, pDatabase->z, pDatabase->n, 0);
if
( *pz==0 ){
sqliteSrcListDelete(pList);
return
0;
}
else
{
sqliteDequote(*pz);
}
}
pList->a[pList->nSrc].iCursor = -1;
pList->nSrc++;
return
pList;
}
void
sqliteSrcListAssignCursors(Parse *pParse, SrcList *pList){
int
i;
for
(i=0; i<pList->nSrc; i++){
if
( pList->a[i].iCursor<0 ){
pList->a[i].iCursor = pParse->nTab++;
}
}
}
void
sqliteSrcListAddAlias(SrcList *pList, Token *pToken){
if
( pList && pList->nSrc>0 ){
int
i = pList->nSrc - 1;
sqliteSetNString(&pList->a[i].zAlias, pToken->z, pToken->n, 0);
sqliteDequote(pList->a[i].zAlias);
}
}
void
sqliteIdListDelete(IdList *pList){
int
i;
if
( pList==0 )
return
;
for
(i=0; i<pList->nId; i++){
sqliteFree(pList->a[i].zName);
}
sqliteFree(pList->a);
sqliteFree(pList);
}
int
sqliteIdListIndex(IdList *pList,
const
char
*zName){
int
i;
if
( pList==0 )
return
-1;
for
(i=0; i<pList->nId; i++){
if
( sqliteStrICmp(pList->a[i].zName, zName)==0 )
return
i;
}
return
-1;
}
void
sqliteSrcListDelete(SrcList *pList){
int
i;
if
( pList==0 )
return
;
for
(i=0; i<pList->nSrc; i++){
sqliteFree(pList->a[i].zDatabase);
sqliteFree(pList->a[i].zName);
sqliteFree(pList->a[i].zAlias);
if
( pList->a[i].pTab && pList->a[i].pTab->isTransient ){
sqliteDeleteTable(0, pList->a[i].pTab);
}
sqliteSelectDelete(pList->a[i].pSelect);
sqliteExprDelete(pList->a[i].pOn);
sqliteIdListDelete(pList->a[i].pUsing);
}
sqliteFree(pList);
}
void
sqliteBeginTransaction(Parse *pParse,
int
onError){
sqlite *db;
if
( pParse==0 || (db=pParse->db)==0 || db->aDb[0].pBt==0 )
return
;
if
( pParse->nErr || sqlite_malloc_failed )
return
;
if
( sqliteAuthCheck(pParse, SQLITE_TRANSACTION,
"BEGIN"
, 0, 0) )
return
;
if
( db->flags & SQLITE_InTrans ){
sqliteErrorMsg(pParse,
"cannot start a transaction within a transaction"
);
return
;
}
sqliteBeginWriteOperation(pParse, 0, 0);
if
( !pParse->explain ){
db->flags |= SQLITE_InTrans;
db->onError = onError;
}
}
void
sqliteCommitTransaction(Parse *pParse){
sqlite *db;
if
( pParse==0 || (db=pParse->db)==0 || db->aDb[0].pBt==0 )
return
;
if
( pParse->nErr || sqlite_malloc_failed )
return
;
if
( sqliteAuthCheck(pParse, SQLITE_TRANSACTION,
"COMMIT"
, 0, 0) )
return
;
if
( (db->flags & SQLITE_InTrans)==0 ){
sqliteErrorMsg(pParse,
"cannot commit - no transaction is active"
);
return
;
}
if
( !pParse->explain ){
db->flags &= ~SQLITE_InTrans;
}
sqliteEndWriteOperation(pParse);
if
( !pParse->explain ){
db->onError = OE_Default;
}
}
void
sqliteRollbackTransaction(Parse *pParse){
sqlite *db;
Vdbe *v;
if
( pParse==0 || (db=pParse->db)==0 || db->aDb[0].pBt==0 )
return
;
if
( pParse->nErr || sqlite_malloc_failed )
return
;
if
( sqliteAuthCheck(pParse, SQLITE_TRANSACTION,
"ROLLBACK"
, 0, 0) )
return
;
if
( (db->flags & SQLITE_InTrans)==0 ){
sqliteErrorMsg(pParse,
"cannot rollback - no transaction is active"
);
return
;
}
v = sqliteGetVdbe(pParse);
if
( v ){
sqliteVdbeAddOp(v, OP_Rollback, 0, 0);
}
if
( !pParse->explain ){
db->flags &= ~SQLITE_InTrans;
db->onError = OE_Default;
}
}
void
sqliteCodeVerifySchema(Parse *pParse,
int
iDb){
sqlite *db = pParse->db;
Vdbe *v = sqliteGetVdbe(pParse);
assert
( iDb>=0 && iDb<db->nDb );
assert
( db->aDb[iDb].pBt!=0 );
if
( iDb!=1 && !DbHasProperty(db, iDb, DB_Cookie) ){
sqliteVdbeAddOp(v, OP_VerifyCookie, iDb, db->aDb[iDb].schema_cookie);
DbSetProperty(db, iDb, DB_Cookie);
}
}
void
sqliteBeginWriteOperation(Parse *pParse,
int
setCheckpoint,
int
iDb){
Vdbe *v;
sqlite *db = pParse->db;
if
( DbHasProperty(db, iDb, DB_Locked) )
return
;
v = sqliteGetVdbe(pParse);
if
( v==0 )
return
;
if
( !db->aDb[iDb].inTrans ){
sqliteVdbeAddOp(v, OP_Transaction, iDb, 0);
DbSetProperty(db, iDb, DB_Locked);
sqliteCodeVerifySchema(pParse, iDb);
if
( iDb!=1 ){
sqliteBeginWriteOperation(pParse, setCheckpoint, 1);
}
}
else
if
( setCheckpoint ){
sqliteVdbeAddOp(v, OP_Checkpoint, iDb, 0);
DbSetProperty(db, iDb, DB_Locked);
}
}
void
sqliteEndWriteOperation(Parse *pParse){
Vdbe *v;
sqlite *db = pParse->db;
if
( pParse->trigStack )
return
;
v = sqliteGetVdbe(pParse);
if
( v==0 )
return
;
if
( db->flags & SQLITE_InTrans ){
}
else
{
sqliteVdbeAddOp(v, OP_Commit, 0, 0);
}
}