build.c revision c5c4113dfcabb1eed3d4bdf7609de5170027a794
#pragma ident "%Z%%M% %I% %E% SMI"
/*
** 2001 September 15
**
** The author disclaims copyright to this source code. In place of
** a legal notice, here is a blessing:
**
** May you do good and not evil.
** May you find forgiveness for yourself and forgive others.
** May you share freely, never taking more than you give.
**
*************************************************************************
** This file contains C code routines that are called by the SQLite parser
** when syntax rules are reduced. The routines in this file handle the
** following kinds of SQL syntax:
**
** CREATE TABLE
** DROP TABLE
** CREATE INDEX
** DROP INDEX
** creating ID lists
** BEGIN TRANSACTION
** COMMIT
** ROLLBACK
** PRAGMA
**
** $Id: build.c,v 1.176.2.2 2004/07/20 00:50:30 drh Exp $
*/
#include "sqliteInt.h"
#include <ctype.h>
/*
** This routine is called when a new SQL statement is beginning to
** be parsed. Check to see if the schema for the database needs
** to be read from the SQLITE_MASTER and SQLITE_TEMP_MASTER tables.
** If it does, then read it.
*/
int i;
}
}
}
}
}
/*
** This routine is called after a single SQL statement has been
** parsed and we want to execute the VDBE code to implement
** that statement. Prior action routines should have already
** constructed VDBE code to do the work of the SQL statement.
** This routine just has to execute the VDBE code.
**
** Note that if an error occurred, it might be the case that
** no VDBE code was generated.
*/
if( v==0 && (v = sqliteGetVdbe(pParse))!=0 ){
sqliteVdbeAddOp(v, OP_Halt, 0, 0);
}
if( sqlite_malloc_failed ) return;
sqliteVdbeTrace(v, trace);
pParse->colNamesSet = 0;
}
}
/*
** Locate the in-memory structure that describes
** a particular database table given the name
** of that table and (optionally) the name of the database
** containing the table. Return NULL if not found.
**
** If zDatabase is 0, all databases are searched for the
** table and the first matching table is returned. (No checking
** for duplicate table names is done.) The search order is
** TEMP first, then MAIN, then any auxiliary databases added
** using the ATTACH command.
**
** See also sqliteLocateTable().
*/
Table *p = 0;
int i;
int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */
if( p ) break;
}
return p;
}
/*
** Locate the in-memory structure that describes
** a particular database table given the name
** of that table and (optionally) the name of the database
** containing the table. Return NULL if not found.
** Also leave an error message in pParse->zErrMsg.
**
** The difference between this routine and sqliteFindTable()
** is that this routine leaves an error message in pParse->zErrMsg
** where sqliteFindTable() does not.
*/
Table *p;
if( p==0 ){
if( zDbase ){
}else{
}
}
return p;
}
/*
** Locate the in-memory structure that describes
** a particular index given the name of that index
** and the name of the database that contains the index.
** Return NULL if not found.
**
** If zDatabase is 0, all databases are searched for the
** table and the first matching index is returned. (No checking
** for duplicate index names is done.) The search order is
** TEMP first, then MAIN, then any auxiliary databases added
** using the ATTACH command.
*/
Index *p = 0;
int i;
int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */
if( p ) break;
}
return p;
}
/*
** Remove the given index from the index hash table, and free
** its memory structures.
**
** The index is removed from the database hash tables but
** it is not unlinked from the Table that it indexes.
** Unlinking from the Table must be done by the calling function.
*/
}
sqliteFree(p);
}
/*
** Unlink the given index from its table, then remove
** the index from the index hash table and free its memory
** structures.
*/
}else{
Index *p;
}
}
}
/*
** Erase all schema information from the in-memory hash tables of
** database connection. This routine is called to reclaim memory
** before the connection closes. It is also called during a rollback
** if there were schema changes during the transaction.
**
** If iDb<=0 then reset the internal schema tables for all database
** files. If iDb>=2 then reset the internal schema for only the
** single file indicated.
*/
int i, j;
}
}
if( iDb>0 ) return;
}
/* If one or more of the auxiliary database files has been closed,
** then remove then from the auxiliary database list. We take the
** opportunity to do this here since we have just deleted all of the
** schema hash tables and therefore do not have to make any changes
** to any of those tables.
*/
}
}
continue;
}
if( j<i ){
}
j++;
}
}
}
/*
** This routine is called whenever a rollback occurs. If there were
** schema changes during the transaction, then we have to reset the
** internal hash tables and reload them from disk.
*/
}
}
/*
** This routine is called when a commit occurs.
*/
}
/*
** Remove the memory data structures associated with the given
** Table. No changes are made to disk by this routine.
**
** This routine just deletes the data structure. It does not unlink
** the table data structure from the hash table. Nor does it remove
** foreign keys from the sqlite.aFKey hash table. But it does destroy
** memory structures of the indices and foreign keys associated with
** the table.
**
** Indices associated with the table are unlinked from the "db"
** data structure if db!=NULL. If db==NULL, indices attached to
** the table are deleted, but it is assumed they have already been
** unlinked.
*/
int i;
if( pTable==0 ) return;
/* Delete all indices associated with this table
*/
}
/* Delete all foreign keys associated with this table. The keys
** should have already been unlinked from the db->aFKey hash table
*/
}
/* Delete the Table structure itself.
*/
}
}
/*
** Unlink the given table from the hash tables and the delete the
** table structure with all its indices and foreign keys.
*/
int i = p->iDb;
}else{
if( pF2 ){
}
}
}
sqliteDeleteTable(db, p);
}
/*
** Construct the name of a user table or index from a token.
**
** Space to hold the name is obtained from sqliteMalloc() and must
** be freed by the calling function.
*/
return zName;
}
/*
** Generate code to open the appropriate master table. The table
** opened will be SQLITE_MASTER for persistent tables and
** SQLITE_TEMP_MASTER for temporary tables. The table is opened
** on cursor 0.
*/
}
/*
** Begin constructing a new table representation in memory. This is
** the first of several action routines that get called in response
** to a CREATE TABLE statement. In particular, this routine is called
** after seeing tokens "CREATE" and "TABLE" and the table name. The
** pStart token is the CREATE and pName is the table name. The isTemp
** flag is true if the table should be stored in the auxiliary database
** file instead of in the main database file. This is normally the case
** when the "TEMP" or "TEMPORARY" keyword occurs in between
** CREATE and TABLE.
**
** The new table record is initialized and put in pParse->pNewTable.
** As more of the CREATE TABLE statement is parsed, additional action
** routines will be called to add more information to this record.
** At the end of the CREATE TABLE statement, the sqliteEndTable() routine
** is called to complete the construction of the new table record.
*/
void sqliteStartTable(
int isTemp, /* True if this is a TEMP table */
int isView /* True if this is a VIEW */
){
char *zName;
Vdbe *v;
int iDb;
if( zName==0 ) return;
#ifndef SQLITE_OMIT_AUTHORIZATION
{
int code;
return;
}
if( isView ){
if( isTemp ){
}else{
}
}else{
if( isTemp ){
}else{
}
}
return;
}
}
#endif
/* Before trying to create a temporary table, make sure the Btree for
** holding temporary tables is open.
*/
"file for storing temporary tables");
return;
}
"the temporary database file");
return;
}
}
}
/* Make sure the new table name does not collide with an existing
** index or table name. Issue an error message if it does.
**
** If we are re-reading the sqlite_master table because of a schema
** change and a new permanent table is found whose name collides with
** an existing temporary table, that is not an error.
*/
return;
}
return;
}
if( pTable==0 ){
return;
}
/* Begin generating the code that will insert the table record into
** the SQLITE_MASTER table. Note in particular that we must go ahead
** and allocate the record number for the table entry now. Before any
** PRIMARY KEY or UNIQUE keywords are parsed. Those keywords will cause
** indices to be created and the table record must come before the
** indices. Hence, the record number for the table must be allocated
** now.
*/
if( !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);
}
}
/*
** Add a new column to the table currently being constructed.
**
** The parser calls this routine once for each column declaration
** in a CREATE TABLE statement. sqliteStartTable() gets called
** first to get things going. Then this routine is called for each
** column.
*/
Table *p;
int i;
char *z = 0;
if( z==0 ) return;
sqliteDequote(z);
for(i=0; i<p->nCol; i++){
sqliteFree(z);
return;
}
}
if( (p->nCol & 0x7)==0 ){
if( aNew==0 ) return;
}
p->nCol++;
}
/*
** This routine is called by the parser while in the middle of
** parsing a CREATE TABLE statement. A "NOT NULL" constraint has
** been seen on a column. This routine sets the notNull flag on
** the column currently under construction.
*/
Table *p;
int i;
i = p->nCol-1;
}
/*
** This routine is called by the parser while in the middle of
** parsing a CREATE TABLE statement. The pFirst token is the first
** token in the sequence of tokens that describe the type of the
** column currently under construction. pLast is the last token
** in the sequence. Use this information to construct a string
** that contains the typename of the column and store that string
** in zType.
*/
Table *p;
int i, j;
int n;
char *z, **pz;
i = p->nCol-1;
if( i<0 ) return;
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;
}else{
}
}
/*
** The given token is the default value for the last column added to
** the table currently under construction. If "minusFlag" is true, it
** means the value token was preceded by a minus sign.
**
** This routine is called by the parser while in the middle of
** parsing a CREATE TABLE statement.
*/
Table *p;
int i;
char **pz;
i = p->nCol-1;
if( i<0 ) return;
if( minusFlag ){
}else{
}
sqliteDequote(*pz);
}
/*
** Designate the PRIMARY KEY for the table. pList is a list of names
** of columns that form the primary key. If pList is NULL, then the
** most recently added column of the table is the primary key.
**
** A table can have at most one primary key. If the table already has
** a primary key (and this is the second primary key) then create an
** error.
**
** If the PRIMARY KEY is on a single column whose datatype is INTEGER,
** then we will try to use that column as the row id. (Exception:
** For backwards compatibility with older databases, do not do this
** if the file format version number is less than 1.) Set the Table.iPKey
** field of the table under construction to be the index of the
** INTEGER PRIMARY KEY column. Table.iPKey is set to -1 if there is
** no INTEGER PRIMARY KEY.
**
** If the key is not an INTEGER PRIMARY KEY, then create a unique
** index for the key. No index is created for INTEGER PRIMARY KEYs.
*/
char *zType = 0;
int iCol = -1, i;
if( pTab==0 ) goto primary_key_exit;
if( pTab->hasPrimKey ){
goto primary_key_exit;
}
if( pList==0 ){
}else{
}
}
}
}
}else{
pList = 0;
}
return;
}
/*
** Return the appropriate collating type given a type name.
**
** The collation type is text (SQLITE_SO_TEXT) if the type
** name contains the character stream "text" or "blob" or
** "clob". Any other type name is collated as numeric
** (SQLITE_SO_NUM).
*/
int i;
for(i=0; i<nType-3; i++){
int c = *(zType++) | 0x60;
return SQLITE_SO_TEXT;
}
return SQLITE_SO_TEXT;
}
return SQLITE_SO_TEXT;
}
}
return SQLITE_SO_NUM;
}
/*
** This routine is called by the parser while in the middle of
** parsing a CREATE TABLE statement. A "COLLATE" clause has
** been seen on a column. This routine sets the Column.sortOrder on
** the column currently under construction.
*/
Table *p;
int i;
i = p->nCol-1;
}
/*
** Come up with a new random value for the schema cookie. Make sure
** the new value is different from the old.
**
** The schema cookie is used to determine when the schema for the
** database changes. After each schema change, the cookie value
** changes. When a process first reads the schema it records the
** cookie. Thereafter, whenever it goes to access the database,
** it checks the cookie to make sure the schema has not changed
** since it was last read.
**
** This plan is not completely bullet-proof. It is possible for
** the schema to change multiple times and for the cookie to be
** set back to prior value. But schema changes are infrequent
** and the probability of hitting the same cookie value is only
** 1 chance in 2^32. So we're safe enough.
*/
unsigned char r;
sqliteRandomness(1, &r);
sqliteVdbeAddOp(v, OP_SetCookie, 0, 0);
}
}
/*
** Measure the number of characters needed to output the given
** identifier. The number returned includes any quotes used
** but does not include the null terminator.
*/
static int identLength(const char *z){
int n;
int needQuote = 0;
for(n=0; *z; n++, z++){
}
return n + needQuote*2;
}
/*
** Write an identifier onto the end of the given string. Add
** quote characters as needed.
*/
int i, j, needQuote;
i = *pIdx;
for(j=0; zIdent[j]; j++){
}
if( needQuote ) z[i++] = '\'';
for(j=0; zIdent[j]; j++){
z[i++] = zIdent[j];
}
if( needQuote ) z[i++] = '\'';
z[i] = 0;
*pIdx = i;
}
/*
** Generate a CREATE TABLE statement appropriate for the given
** table. Memory to hold the text of the statement is obtained
** from sqliteMalloc() and must be freed by the calling function.
*/
static char *createTableStmt(Table *p){
int i, k, n;
char *zStmt;
n = 0;
for(i=0; i<p->nCol; i++){
}
n += identLength(p->zName);
if( n<40 ){
zSep = "";
zSep2 = ",";
zEnd = ")";
}else{
zSep = "\n ";
zSep2 = ",\n ";
zEnd = "\n)";
}
zStmt = sqliteMallocRaw( n );
if( zStmt==0 ) return 0;
zStmt[k++] = '(';
for(i=0; i<p->nCol; i++){
}
return zStmt;
}
/*
** This routine is called to report the final ")" that terminates
** a CREATE TABLE statement.
**
** The table structure that other action routines have been building
** is added to the internal hash tables, assuming no errors have
** occurred.
**
** An entry for the table is made in the master table on disk, unless
** this is a temporary table or db->init.busy==1. When db->init.busy==1
** it means we are reading the sqlite_master table because we just
** connected to the database or because the sqlite_master table has
** recently changes, so the entry for this table already exists in
** the sqlite_master table. We do not want to create it again.
**
** If the pSelect argument is not NULL, it means that this routine
** was called to create a table generated from a
** "CREATE TABLE ... AS SELECT ..." statement. The column names of
** the new table will match the result set of the SELECT.
*/
Table *p;
if( p==0 ) return;
/* If the table is generated from a SELECT, then construct the
** list of columns and the text of the table.
*/
if( pSelect ){
if( pSelTab==0 ) return;
sqliteDeleteTable(0, pSelTab);
}
/* If the db->init.busy is 1 it means we are reading the SQL off the
** "sqlite_master" or "sqlite_temp_master" table on the disk.
** So do not write to the disk again. Extract the root page number
** for the table from the db->init.newTnum field. (The page number
** should have been put there by the sqliteOpenCb routine.)
*/
}
/* If not initializing, then create a record for the new table
** in the SQLITE_MASTER table of the database. The record number
** for the new table entry should already be on the stack.
**
** If this is a TEMPORARY table, write the entry into the auxiliary
** file instead of into the main database file.
*/
int n;
Vdbe *v;
v = sqliteGetVdbe(pParse);
if( v==0 ) return;
if( p->pSelect==0 ){
/* A regular table */
}else{
/* A view */
sqliteVdbeAddOp(v, OP_Integer, 0, 0);
}
p->tnum = 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{
}
sqliteVdbeAddOp(v, OP_PutIntKey, 0, 0);
if( !p->iDb ){
sqliteChangeCookie(db, v);
}
sqliteVdbeAddOp(v, OP_Close, 0, 0);
if( pSelect ){
}
}
/* Add the table to the in-memory representation of the database.
*/
if( pOld ){
return;
}
}
}
}
/*
** The parser calls this routine in order to create a new VIEW
*/
void sqliteCreateView(
int isTemp /* TRUE for a TEMPORARY view */
){
Table *p;
int n;
const char *z;
return;
}
){
return;
}
/* Make a copy of the entire SELECT statement that defines the view.
** This will force all the Expr.token.z values to be dynamically
** allocated rather than point to the input string - which means that
** they will persist after the current sqlite_exec() call returns.
*/
}
/* Locate the end of the CREATE VIEW statement. Make sEnd point to
** the end.
*/
}
sEnd.n = 0;
z = pBegin->z;
sEnd.z = &z[n-1];
sEnd.n = 1;
/* Use sqliteEndTable() to add the view to the SQLITE_MASTER table */
return;
}
/*
** The Table structure pTable is really a VIEW. Fill in the names of
** the columns of the view in the pTable structure. Return the number
** of errors. If an error is seen leave an error message in pParse->zErrMsg.
*/
int nErr = 0;
/* A positive nCol means the columns names for this view are
** already known.
*/
/* A negative nCol is a special marker meaning that we are currently
** trying to compute the column names. If we enter this routine with
** a negative nCol, it means two or more views form a loop, like this:
**
** CREATE VIEW one AS SELECT * FROM two;
** CREATE VIEW two AS SELECT * FROM one;
**
** Actually, this error is caught previously and so the following test
** should always fail. But we will leave it in place just to be safe.
*/
return 1;
}
/* If we get this far, it means we need to compute the table names.
*/
/* Note that the call to sqliteResultSetOfSelect() will expand any
** "*" elements in this list. But we will need to restore the list
** back to its original configuration afterwards, so we save a copy of
** the original in pEList.
*/
return 1; /* Malloc failed */
}
if( pSelTab ){
sqliteDeleteTable(0, pSelTab);
}else{
nErr++;
}
return nErr;
}
/*
** Clear the column names from the VIEW pTable.
**
** This routine is called whenever any other table or view is modified.
** The view passed into this routine might depend directly or indirectly
** on the modified or deleted table so we need to clear the old column
** names so that they will be recomputed.
*/
int i;
}
}
/*
** Clear the column names from every VIEW in database idx.
*/
HashElem *i;
}
}
}
/*
** Given a token, look up a table with that name. If not found, leave
** an error for the parser to find and return NULL.
*/
char *zName;
if( zName==0 ) return 0;
if( pTab==0 ){
}
return pTab;
}
/*
** This routine is called to do the work of a DROP TABLE statement.
** pName is the name of the table to be dropped.
*/
Vdbe *v;
int base;
int iDb;
if( pTable==0 ) return;
#ifndef SQLITE_OMIT_AUTHORIZATION
{
int code;
return;
}
if( isView ){
if( iDb==1 ){
}else{
}
}else{
if( iDb==1 ){
}else{
}
}
return;
}
return;
}
}
#endif
return;
}
return;
}
return;
}
/* Generate code to remove the table from the master table
** on disk.
*/
v = sqliteGetVdbe(pParse);
if( v ){
static VdbeOpList dropTable[] = {
{ OP_String, 0, 0, 0}, /* 1 */
{ OP_Column, 0, 2, 0},
{ OP_Delete, 0, 0, 0},
};
/* Drop all triggers associated with the table being dropped */
while( pTrigger ){
}else{
}
}
/* Drop all SQLITE_MASTER entries that refer to the table */
/* Drop all SQLITE_TEMP_MASTER entries that refer to the table */
sqliteOpenMasterTable(v, 1);
}
sqliteChangeCookie(db, v);
}
sqliteVdbeAddOp(v, OP_Close, 0, 0);
if( !isView ){
}
}
}
/* Delete the in-memory description of the table.
**
** Exception: if the SQL statement began with the EXPLAIN keyword,
** then no changes should be made.
*/
}
}
/*
** This routine constructs a P3 string suitable for an OP_MakeIdxKey
** opcode and adds that P3 string to the most recently inserted instruction
** in the virtual machine. The P3 string consists of a single character
** for each column in the index pIdx of table pTab. If the column uses
** a numeric sort order, then the P3 string character corresponding to
** that column is 'n'. If the column uses a text sort order, then the
** P3 string is 't'. See the OP_MakeIdxKey opcode documentation for
** additional information. See also the sqliteAddKeyType() routine.
*/
char *zType;
int i, n;
if( zType==0 ) return;
for(i=0; i<n; i++){
zType[i] = 't';
}else{
zType[i] = 'n';
}
}
zType[n] = 0;
}
/*
** This routine is called to create a new foreign key on the table
** currently under construction. pFromCol determines which columns
** in the current table point to the foreign key. If pFromCol==0 then
** connect the key to the last column inserted. pTo is the name of
** the table referred to. pToCol is a list of tables in the other
** pTo table that the foreign key points to. flags contains all
** information about the conflict resolution algorithms specified
** in the ON DELETE, ON UPDATE and ON INSERT clauses.
**
** An FKey structure is created and added to the table currently
** under construction in the pParse->pNewTable field. The new FKey
** is not linked into db->aFKey at this point - that does not happen
** until sqliteEndTable().
**
** The foreign key is set for IMMEDIATE processing. A subsequent call
** to sqliteDeferForeignKey() might change this to DEFERRED.
*/
void sqliteCreateForeignKey(
int flags /* Conflict resolution algorithms. */
){
int nByte;
int i;
int nCol;
char *z;
if( pFromCol==0 ){
" should reference only one column of table %T",
goto fk_end;
}
nCol = 1;
"number of columns in foreign key does not match the number of "
"columns in the referenced table");
goto fk_end;
}else{
}
if( pToCol ){
}
}
z = (char*)&pFKey[1];
z[pTo->n] = 0;
z += pTo->n+1;
if( pFromCol==0 ){
}else{
for(i=0; i<nCol; i++){
int j;
for(j=0; j<p->nCol; j++){
break;
}
}
if( j>=p->nCol ){
"unknown column \"%s\" in foreign key definition",
goto fk_end;
}
}
}
if( pToCol ){
for(i=0; i<nCol; i++){
z[n] = 0;
z += n+1;
}
}
pFKey->isDeferred = 0;
/* Link the foreign key to the table as the last step.
*/
pFKey = 0;
}
/*
** This routine is called when an INITIALLY IMMEDIATE or INITIALLY DEFERRED
** clause is seen as part of a foreign key definition. The isDeferred
** parameter is 1 for INITIALLY DEFERRED and 0 for INITIALLY IMMEDIATE.
** The behavior of the most recently created foreign key is adjusted
** accordingly.
*/
}
/*
** Create a new index for an SQL table. pIndex is the name of the index
** and pTable is the name of the table that is to be indexed. Both will
** be NULL for a primary key or an index that is created to satisfy a
** UNIQUE constraint. If pTable and pIndex are NULL, use pParse->pNewTable
** as the table to be indexed. pParse->pNewTable is a table that is
** currently being constructed by a CREATE TABLE statement.
**
** pList is a list of columns to be indexed. pList will be NULL if this
** is a primary key or unique-constraint on the most recent column added
** to the table currently under construction.
*/
void sqliteCreateIndex(
int onError, /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
){
char *zName = 0;
int i, j;
int isTemp; /* True for a temporary index */
){
goto exit_create_index;
}
/*
** Find the table that is to be indexed. Return early if not found.
*/
if( pTable!=0 ){
}else{
}
goto exit_create_index;
}
goto exit_create_index;
}
goto exit_create_index;
}
/*
** Find the name of the index. Make sure there is not already another
** index or table with the same name.
**
** Exception: If we are reading the names of permanent indices from the
** sqlite_master table (because some other process changed the schema) and
** one of the index names collides with the name of a temporary table or
** index, then we will continue to process this index.
**
** If pName==0 it means that we are
** dealing with a primary key or UNIQUE constraint. We have to invent our
** own name.
*/
if( zName==0 ) goto exit_create_index;
goto exit_create_index;
}
goto exit_create_index;
}
}else if( pName==0 ){
char zBuf[30];
int n;
zName = 0;
if( zName==0 ) goto exit_create_index;
}else{
}
/* Check for authorization to create an index.
*/
#ifndef SQLITE_OMIT_AUTHORIZATION
{
goto exit_create_index;
}
i = SQLITE_CREATE_INDEX;
if( isTemp ) i = SQLITE_CREATE_TEMP_INDEX;
goto exit_create_index;
}
}
#endif
/* If pList==0, it means this routine was called to make a primary
** key out of the last column added to the table under construction.
** So create a fake list to simulate this.
*/
if( pList==0 ){
if( pList==0 ) goto exit_create_index;
}
/*
** Allocate the index structure.
*/
if( pIndex==0 ) goto exit_create_index;
/* Scan the names of the columns of the table to be indexed and
** load the column indices into the Index structure. Report an error
** if any column is not found.
*/
}
goto exit_create_index;
}
}
/* Link the new Index structure to its table and to the other
** in-memory database structures.
*/
Index *p;
if( p ){
goto exit_create_index;
}
}
/* When adding an index to the list of indices for a table, make
** sure all indices labeled OE_Replace come after all those labeled
** OE_Ignore. This is necessary for the correct operation of UPDATE
** and INSERT.
*/
}else{
}
}
/* If the db->init.busy is 1 it means we are reading the SQL off the
** "sqlite_master" table on the disk. So do not write to the disk
** again. Extract the table number from the db->init.newTnum field.
*/
}
/* If the db->init.busy is 0 then create the index on disk. This
** involves writing the index into the master table and filling in the
** index with the current table contents.
**
** The db->init.busy is 0 when the user first enters a CREATE INDEX
** command. db->init.busy is 1 when a database is opened and
** CREATE INDEX statements are read out of the master table. In
** the latter case the index already exists on disk, which is why
** we don't want to recreate it.
**
** If pTable==0 it means this index is generated as a primary key
** or UNIQUE constraint of a CREATE TABLE statement. Since the table
** has just been created, it contains no data and the index initialization
** step can be skipped.
*/
int n;
Vdbe *v;
int i;
int addr;
v = sqliteGetVdbe(pParse);
if( v==0 ) goto exit_create_index;
if( pTable!=0 ){
}
sqliteVdbeAddOp(v, OP_NewRecno, 0, 0);
if( pTable ){
OP_Dup, 0, 0,
OP_Integer, isTemp, 0,
OP_OpenWrite, 1, 0,
0);
}
}
sqliteVdbeAddOp(v, OP_PutIntKey, 0, 0);
if( pTable ){
lbl2 = sqliteVdbeMakeLabel(v);
sqliteVdbeAddOp(v, OP_Dup, i, 0);
}else{
}
}
"indexed columns are not unique", P3_STATIC);
}
if( pTable!=0 ){
if( !isTemp ){
sqliteChangeCookie(db, v);
}
sqliteVdbeAddOp(v, OP_Close, 0, 0);
}
}
/* Clean up before exiting */
return;
}
/*
** This routine will drop an existing named index. This routine
** implements the DROP INDEX statement.
*/
Vdbe *v;
if( pIndex==0 ){
goto exit_drop_index;
}
"or PRIMARY KEY constraint cannot be dropped", 0);
goto exit_drop_index;
}
"databases", 0);
goto exit_drop_index;
}
#ifndef SQLITE_OMIT_AUTHORIZATION
{
int code = SQLITE_DROP_INDEX;
goto exit_drop_index;
}
goto exit_drop_index;
}
}
#endif
/* Generate code to remove the index and from the master table */
v = sqliteGetVdbe(pParse);
if( v ){
static VdbeOpList dropIndex[] = {
{ OP_String, 0, 0, 0}, /* 1 */
{ OP_Column, 0, 1, 0},
{ OP_Delete, 0, 0, 0}, /* 8 */
};
int base;
sqliteChangeCookie(db, v);
}
sqliteVdbeAddOp(v, OP_Close, 0, 0);
}
/* Delete the in-memory description of this index.
*/
}
}
/*
** Append a new element to the given IdList. Create a new IdList if
** need be.
**
** A new IdList is returned, or NULL if malloc() fails.
*/
if( pList==0 ){
if( pList==0 ) return 0;
}
struct IdList_item *a;
if( a==0 ){
return 0;
}
pList->a = a;
}
if( pToken ){
if( *pz==0 ){
return 0;
}else{
sqliteDequote(*pz);
}
}
return pList;
}
/*
** Append a new table name to the given SrcList. Create a new SrcList if
** need be. A new entry is created in the SrcList even if pToken is NULL.
**
** A new SrcList is returned, or NULL if malloc() fails.
**
** If pDatabase is not null, it means that the table has an optional
** database name prefix. Like this: "database.table". The pDatabase
** points to the table name and the pTable points to the database name.
** The SrcList.a[].zName field is filled with the table name which might
** come from pTable (if pDatabase is NULL) or from pDatabase.
** SrcList.a[].zDatabase is filled with the database name from pTable,
** or with NULL if no database is specified.
**
** In other words, if call like this:
**
** sqliteSrcListAppend(A,B,0);
**
** Then B is a table name and the database name is unspecified. If called
** like this:
**
** sqliteSrcListAppend(A,B,C);
**
** Then C is the table name and B is the database name.
*/
if( pList==0 ){
if( pList==0 ) return 0;
}
if( pNew==0 ){
return 0;
}
}
pDatabase = 0;
}
}
if( pTable ){
if( *pz==0 ){
return 0;
}else{
sqliteDequote(*pz);
}
}
if( pDatabase ){
if( *pz==0 ){
return 0;
}else{
sqliteDequote(*pz);
}
}
return pList;
}
/*
** Assign cursors to all tables in a SrcList
*/
int i;
}
}
}
/*
** Add an alias to the last identifier on the given identifier list.
*/
}
}
/*
** Delete an IdList.
*/
int i;
if( pList==0 ) return;
}
sqliteFree(pList->a);
}
/*
** Return the index in pList of the identifier named zId. Return -1
** if not found.
*/
int i;
if( pList==0 ) return -1;
}
return -1;
}
/*
** Delete an entire SrcList including all its substructure.
*/
int i;
if( pList==0 ) return;
}
}
}
/*
** Begin a transaction
*/
return;
}
sqliteBeginWriteOperation(pParse, 0, 0);
}
}
/*
** Commit a transaction
*/
return;
}
}
}
}
/*
** Rollback a transaction
*/
Vdbe *v;
return;
}
v = sqliteGetVdbe(pParse);
if( v ){
sqliteVdbeAddOp(v, OP_Rollback, 0, 0);
}
}
}
/*
** Generate VDBE code that will verify the schema cookie for all
** named database files.
*/
}
}
/*
** Generate VDBE code that prepares for doing an operation that
** might change the database.
**
** This routine starts a new transaction if we are not already within
** a transaction. If we are already within a transaction, then a checkpoint
** is set if the setCheckpoint parameter is true. A checkpoint should
** be set for operations that might fail (due to a constraint) part of
** the way through and which will need to undo some writes without having to
** rollback the whole transaction. For operations where all constraints
** can be checked before any changes are made to the database, it is never
** necessary to undo a write and the checkpoint should not be set.
**
** Only database iDb and the temp database are made writable by this call.
** If iDb==0, then the main and temp databases are made writable. If
** iDb==1 then only the temp database is made writable. If iDb>1 then the
** specified auxiliary database and the temp database are made writable.
*/
Vdbe *v;
v = sqliteGetVdbe(pParse);
if( v==0 ) return;
if( iDb!=1 ){
}
}else if( setCheckpoint ){
}
}
/*
** Generate code that concludes an operation that may have changed
** the database. If a statement transaction was started, then emit
** an OP_Commit that will cause the changes to be committed to disk.
**
** Note that checkpoints are automatically committed at the end of
** a statement. Note also that there can be multiple calls to
** sqliteBeginWriteOperation() but there should only be a single
** call to sqliteEndWriteOperation() at the conclusion of the statement.
*/
Vdbe *v;
v = sqliteGetVdbe(pParse);
if( v==0 ) return;
/* A BEGIN has executed. Do not commit until we see an explicit
** COMMIT statement. */
}else{
sqliteVdbeAddOp(v, OP_Commit, 0, 0);
}
}