aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authordrh <drh@noemail.net>2006-01-04 15:54:36 +0000
committerdrh <drh@noemail.net>2006-01-04 15:54:36 +0000
commit4d91a701bd0daef78d60bf780e5e25b6f4e44605 (patch)
tree06e5d8f4ddad1f6d54effc962024829f6b0a3a68 /src
parentf93339decbd89d4582791886b020addd2b28e409 (diff)
downloadsqlite-4d91a701bd0daef78d60bf780e5e25b6f4e44605.tar.gz
sqlite-4d91a701bd0daef78d60bf780e5e25b6f4e44605.zip
Add support for CREATE INDEX IF NOT EXISTS and DROP INDEX IF EXISTS. (CVS 2855)
FossilOrigin-Name: 551cdd6c309e75687abaeac5381b794cd5e4c10a
Diffstat (limited to 'src')
-rw-r--r--src/build.c17
-rw-r--r--src/parse.y12
-rw-r--r--src/prepare.c4
-rw-r--r--src/sqliteInt.h6
4 files changed, 22 insertions, 17 deletions
diff --git a/src/build.c b/src/build.c
index 655fee543..48cebe427 100644
--- a/src/build.c
+++ b/src/build.c
@@ -22,7 +22,7 @@
** COMMIT
** ROLLBACK
**
-** $Id: build.c,v 1.364 2005/12/29 23:33:54 drh Exp $
+** $Id: build.c,v 1.365 2006/01/04 15:54:36 drh Exp $
*/
#include "sqliteInt.h"
#include <ctype.h>
@@ -1056,7 +1056,7 @@ void sqlite3AddPrimaryKey(
"INTEGER PRIMARY KEY");
#endif
}else{
- sqlite3CreateIndex(pParse, 0, 0, 0, pList, onError, 0, 0, sortOrder);
+ sqlite3CreateIndex(pParse, 0, 0, 0, pList, onError, 0, 0, sortOrder, 0);
pList = 0;
}
@@ -2105,7 +2105,8 @@ void sqlite3CreateIndex(
int onError, /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
Token *pStart, /* The CREATE token that begins a CREATE TABLE statement */
Token *pEnd, /* The ")" that closes the CREATE INDEX statement */
- int sortOrder /* Sort order of primary key when pList==NULL */
+ int sortOrder, /* Sort order of primary key when pList==NULL */
+ int ifNotExist /* Omit error if index already exists */
){
Table *pTab = 0; /* Table to be indexed */
Index *pIndex = 0; /* The index to be created */
@@ -2199,7 +2200,9 @@ void sqlite3CreateIndex(
if( !db->init.busy ){
if( SQLITE_OK!=sqlite3ReadSchema(pParse) ) goto exit_create_index;
if( sqlite3FindIndex(db, zName, pDb->zName)!=0 ){
- sqlite3ErrorMsg(pParse, "index %s already exists", zName);
+ if( !ifNotExist ){
+ sqlite3ErrorMsg(pParse, "index %s already exists", zName);
+ }
goto exit_create_index;
}
if( sqlite3FindTable(db, zName, 0)!=0 ){
@@ -2525,7 +2528,7 @@ void sqlite3DefaultRowEst(Index *pIdx){
** This routine will drop an existing named index. This routine
** implements the DROP INDEX statement.
*/
-void sqlite3DropIndex(Parse *pParse, SrcList *pName){
+void sqlite3DropIndex(Parse *pParse, SrcList *pName, int ifExists){
Index *pIndex;
Vdbe *v;
sqlite3 *db = pParse->db;
@@ -2539,7 +2542,9 @@ void sqlite3DropIndex(Parse *pParse, SrcList *pName){
}
pIndex = sqlite3FindIndex(db, pName->a[0].zName, pName->a[0].zDatabase);
if( pIndex==0 ){
- sqlite3ErrorMsg(pParse, "no such index: %S", pName, 0);
+ if( !ifExists ){
+ sqlite3ErrorMsg(pParse, "no such index: %S", pName, 0);
+ }
pParse->checkSchema = 1;
goto exit_drop_index;
}
diff --git a/src/parse.y b/src/parse.y
index 13005756b..e7d13e865 100644
--- a/src/parse.y
+++ b/src/parse.y
@@ -14,7 +14,7 @@
** the parser. Lemon will also generate a header file containing
** numeric codes for all of the tokens.
**
-** @(#) $Id: parse.y,v 1.190 2005/12/29 23:33:54 drh Exp $
+** @(#) $Id: parse.y,v 1.191 2006/01/04 15:54:36 drh Exp $
*/
// All token codes are small integers with #defines that begin with "TK_"
@@ -262,7 +262,7 @@ ccons ::= NULL onconf.
ccons ::= NOT NULL onconf(R). {sqlite3AddNotNull(pParse, R);}
ccons ::= PRIMARY KEY sortorder(Z) onconf(R) autoinc(I).
{sqlite3AddPrimaryKey(pParse,0,R,I,Z);}
-ccons ::= UNIQUE onconf(R). {sqlite3CreateIndex(pParse,0,0,0,0,R,0,0,0);}
+ccons ::= UNIQUE onconf(R). {sqlite3CreateIndex(pParse,0,0,0,0,R,0,0,0,0);}
ccons ::= CHECK LP expr(X) RP. {sqlite3AddCheckConstraint(pParse,X);}
ccons ::= REFERENCES nm(T) idxlist_opt(TA) refargs(R).
{sqlite3CreateForeignKey(pParse,0,&T,TA,R);}
@@ -312,7 +312,7 @@ tcons ::= CONSTRAINT nm.
tcons ::= PRIMARY KEY LP idxlist(X) autoinc(I) RP onconf(R).
{sqlite3AddPrimaryKey(pParse,X,R,I,0);}
tcons ::= UNIQUE LP idxlist(X) RP onconf(R).
- {sqlite3CreateIndex(pParse,0,0,0,X,R,0,0,0);}
+ {sqlite3CreateIndex(pParse,0,0,0,X,R,0,0,0,0);}
tcons ::= CHECK LP expr(E) RP onconf. {sqlite3AddCheckConstraint(pParse,E);}
tcons ::= FOREIGN KEY LP idxlist(FA) RP
REFERENCES nm(T) idxlist_opt(TA) refargs(R) defer_subclause_opt(D). {
@@ -836,12 +836,12 @@ expritem(A) ::= . {A = 0;}
///////////////////////////// The CREATE INDEX command ///////////////////////
//
-cmd ::= CREATE(S) uniqueflag(U) INDEX nm(X) dbnm(D)
+cmd ::= CREATE(S) uniqueflag(U) INDEX ifnotexists(NE) nm(X) dbnm(D)
ON nm(Y) LP idxlist(Z) RP(E) onconf(R). {
if( U!=OE_None ) U = R;
if( U==OE_Default) U = OE_Abort;
sqlite3CreateIndex(pParse, &X, &D, sqlite3SrcListAppend(0,&Y,0), Z, U,
- &S, &E, SQLITE_SO_ASC);
+ &S, &E, SQLITE_SO_ASC, NE);
}
%type uniqueflag {int}
@@ -879,7 +879,7 @@ idxitem(A) ::= nm(X). {A = X;}
///////////////////////////// The DROP INDEX command /////////////////////////
//
-cmd ::= DROP INDEX fullname(X). {sqlite3DropIndex(pParse, X);}
+cmd ::= DROP INDEX ifexists(E) fullname(X). {sqlite3DropIndex(pParse, X, E);}
///////////////////////////// The VACUUM command /////////////////////////////
//
diff --git a/src/prepare.c b/src/prepare.c
index 164c4149e..1510b344e 100644
--- a/src/prepare.c
+++ b/src/prepare.c
@@ -13,7 +13,7 @@
** interface, and routines that contribute to loading the database schema
** from disk.
**
-** $Id: prepare.c,v 1.11 2005/12/29 19:23:07 drh Exp $
+** $Id: prepare.c,v 1.12 2006/01/04 15:54:36 drh Exp $
*/
#include "sqliteInt.h"
#include "os.h"
@@ -433,7 +433,7 @@ int sqlite3_prepare(
sParse.rc = SQLITE_NOMEM;
}
if( sParse.rc==SQLITE_DONE ) sParse.rc = SQLITE_OK;
- if( sParse.rc!=SQLITE_OK && sParse.checkSchema && !schemaIsValid(db) ){
+ if( sParse.checkSchema && !schemaIsValid(db) ){
sParse.rc = SQLITE_SCHEMA;
}
if( sParse.rc==SQLITE_SCHEMA ){
diff --git a/src/sqliteInt.h b/src/sqliteInt.h
index f735aa408..7c30a5eec 100644
--- a/src/sqliteInt.h
+++ b/src/sqliteInt.h
@@ -11,7 +11,7 @@
*************************************************************************
** Internal interface definitions for SQLite.
**
-** @(#) $Id: sqliteInt.h,v 1.446 2005/12/30 16:28:02 danielk1977 Exp $
+** @(#) $Id: sqliteInt.h,v 1.447 2006/01/04 15:54:36 drh Exp $
*/
#ifndef _SQLITEINT_H_
#define _SQLITEINT_H_
@@ -1516,8 +1516,8 @@ void sqlite3SrcListAssignCursors(Parse*, SrcList*);
void sqlite3IdListDelete(IdList*);
void sqlite3SrcListDelete(SrcList*);
void sqlite3CreateIndex(Parse*,Token*,Token*,SrcList*,ExprList*,int,Token*,
- Token*, int);
-void sqlite3DropIndex(Parse*, SrcList*);
+ Token*, int, int);
+void sqlite3DropIndex(Parse*, SrcList*, int);
void sqlite3AddKeyType(Vdbe*, ExprList*);
void sqlite3AddIdxKeyType(Vdbe*, Index*);
int sqlite3Select(Parse*, Select*, int, int, Select*, int, int*, char *aff);