diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/build.c | 27 | ||||
-rw-r--r-- | src/dbbe.c | 59 | ||||
-rw-r--r-- | src/expr.c | 7 | ||||
-rw-r--r-- | src/insert.c | 14 | ||||
-rw-r--r-- | src/main.c | 21 | ||||
-rw-r--r-- | src/shell.c | 5 | ||||
-rw-r--r-- | src/sqliteInt.h | 3 | ||||
-rw-r--r-- | src/util.c | 4 |
8 files changed, 98 insertions, 42 deletions
diff --git a/src/build.c b/src/build.c index d5e98435c..0c7904d34 100644 --- a/src/build.c +++ b/src/build.c @@ -22,9 +22,18 @@ ** ************************************************************************* ** This file contains C code routines that are called by the parser -** when syntax rules are reduced. +** when syntax rules are reduced. The routines in this file handle +** the following kinds of rules: ** -** $Id: build.c,v 1.12 2000/06/02 01:17:37 drh Exp $ +** CREATE TABLE +** DROP TABLE +** CREATE INDEX +** DROP INDEX +** creating expressions and ID lists +** COPY +** VACUUM +** +** $Id: build.c,v 1.13 2000/06/02 13:27:59 drh Exp $ */ #include "sqliteInt.h" @@ -529,15 +538,15 @@ void sqliteCreateIndex( */ if( pParse->initFlag==0 ){ static VdbeOp addTable[] = { - { OP_Open, 0, 1, MASTER_NAME}, - { OP_New, 0, 0, 0}, + { OP_Open, 2, 1, MASTER_NAME}, + { OP_New, 2, 0, 0}, { OP_String, 0, 0, "index"}, { OP_String, 0, 0, 0}, /* 3 */ { OP_String, 0, 0, 0}, /* 4 */ { OP_String, 0, 0, 0}, /* 5 */ { OP_MakeRecord, 4, 0, 0}, - { OP_Put, 0, 0, 0}, - { OP_Close, 0, 0, 0}, + { OP_Put, 2, 0, 0}, + { OP_Close, 2, 0, 0}, }; int n; Vdbe *v = pParse->pVdbe; @@ -548,6 +557,8 @@ void sqliteCreateIndex( v = pParse->pVdbe = sqliteVdbeCreate(pParse->db->pBe); } if( v==0 ) goto exit_create_index; + sqliteVdbeAddOp(v, OP_Open, 0, 0, pTab->zName, 0); + sqliteVdbeAddOp(v, OP_Open, 1, 1, pIndex->zName, 0); if( pStart && pEnd ){ int base; n = (int)pEnd->z - (int)pStart->z + 1; @@ -556,8 +567,6 @@ void sqliteCreateIndex( sqliteVdbeChangeP3(v, base+4, pTab->zName, 0); sqliteVdbeChangeP3(v, base+5, pStart->z, n); } - sqliteVdbeAddOp(v, OP_Open, 0, 0, pTab->zName, 0); - sqliteVdbeAddOp(v, OP_Open, 1, 1, pIndex->zName, 0); lbl1 = sqliteVdbeMakeLabel(v); lbl2 = sqliteVdbeMakeLabel(v); sqliteVdbeAddOp(v, OP_Next, 0, lbl2, 0, lbl1); @@ -569,8 +578,8 @@ void sqliteCreateIndex( sqliteVdbeAddOp(v, OP_PutIdx, 1, 0, 0, 0); sqliteVdbeAddOp(v, OP_Goto, 0, lbl1, 0, 0); sqliteVdbeAddOp(v, OP_Noop, 0, 0, 0, lbl2); - sqliteVdbeAddOp(v, OP_Close, 0, 0, 0, 0); sqliteVdbeAddOp(v, OP_Close, 1, 0, 0, 0); + sqliteVdbeAddOp(v, OP_Close, 0, 0, 0, 0); } /* Reclaim memory on an EXPLAIN call. diff --git a/src/dbbe.c b/src/dbbe.c index ec3cb33a4..ece4f4ff6 100644 --- a/src/dbbe.c +++ b/src/dbbe.c @@ -30,7 +30,7 @@ ** relatively simple to convert to a different database such ** as NDBM, SDBM, or BerkeleyDB. ** -** $Id: dbbe.c,v 1.10 2000/06/02 02:09:23 drh Exp $ +** $Id: dbbe.c,v 1.11 2000/06/02 13:27:59 drh Exp $ */ #include "sqliteInt.h" #include <gdbm.h> @@ -124,9 +124,13 @@ static int rc4byte(struct rc4 *p){ } /* -** This routine opens a new database. For the current driver scheme, -** the database name is the name of the directory +** This routine opens a new database. For the GDBM driver +** implemented here, the database name is the name of the directory ** containing all the files of the database. +** +** If successful, a pointer to the Dbbe structure is returned. +** If there are errors, an appropriate error message is left +** in *pzErrMsg and NULL is returned. */ Dbbe *sqliteDbbeOpen( const char *zName, /* The name of the database */ @@ -142,7 +146,8 @@ Dbbe *sqliteDbbeOpen( if( stat(zName, &statbuf)!=0 ){ if( createFlag ) mkdir(zName, 0750); if( stat(zName, &statbuf)!=0 ){ - sqliteSetString(pzErrMsg, "can't find or make directory \"", + sqliteSetString(pzErrMsg, createFlag ? + "can't find or create directory \"" : "can't find directory \"", zName, "\"", 0); return 0; } @@ -229,6 +234,9 @@ static char *sqliteFileOfTable(Dbbe *pBe, const char *zTable){ /* ** Generate a random filename with the given prefix. +** +** Very random names are chosen so that the chance of a +** collision with an existing filename is very very small. */ static void randomName(struct rc4 *pRc4, char *zBuf, char *zPrefix){ int i, j; @@ -242,9 +250,28 @@ static void randomName(struct rc4 *pRc4, char *zBuf, char *zPrefix){ zBuf[j] = 0; } - /* -** Open a new table cursor +** Open a new table cursor. Write a pointer to the corresponding +** DbbeTable structure into *ppTable. Return an integer success +** code: +** +** SQLITE_OK It worked! +** +** SQLITE_NOMEM sqliteMalloc() failed +** +** SQLITE_PERM Attempt to access a file for which file +** access permission is denied +** +** SQLITE_BUSY Another thread or process is already using +** the corresponding file and has that file locked. +** +** SQLITE_READONLY The current thread already has this file open +** readonly but you are trying to open for writing. +** (This can happen if a SELECT callback tries to +** do an UPDATE or DELETE.) +** +** If zTable is 0 or "", then a temporary table is created and opened. +** This table will be deleted from the disk when it is closed. */ int sqliteDbbeOpenTable( Dbbe *pBe, /* The database the table belongs to */ @@ -335,7 +362,8 @@ int sqliteDbbeOpenTable( } /* -** Drop a table from the database. +** Drop a table from the database. The file on the disk that corresponds +** to this table is deleted. */ void sqliteDbbeDropTable(Dbbe *pBe, const char *zTable){ char *zFile; /* Name of the table file */ @@ -349,7 +377,6 @@ void sqliteDbbeDropTable(Dbbe *pBe, const char *zTable){ ** Reorganize a table to reduce search times and disk usage. */ void sqliteDbbeReorganizeTable(Dbbe *pBe, const char *zTable){ - char *zFile; /* Name of the table file */ DbbeTable *pTab; if( sqliteDbbeOpenTable(pBe, zTable, 1, &pTab)!=SQLITE_OK ){ @@ -603,14 +630,18 @@ int sqliteDbbeDelete(DbbeTable *pTable, int nKey, char *pKey){ } /* -** Open a temporary file. +** Open a temporary file. The file should be deleted when closed. +** +** Note that we can't use the old Unix trick of opening the file +** and then immediately unlinking the file. That works great +** under Unix, but fails when we try to port to Windows. */ int sqliteDbbeOpenTempFile(Dbbe *pBe, FILE **ppFile){ - char *zFile; - char zBuf[50]; - int i, j; - int limit; - int rc = SQLITE_OK; + char *zFile; /* Full name of the temporary file */ + char zBuf[50]; /* Base name of the temporary file */ + int i; /* Loop counter */ + int limit; /* Prevent an infinite loop */ + int rc = SQLITE_OK; /* Value returned by this function */ for(i=0; i<pBe->nTemp; i++){ if( pBe->apTemp[i]==0 ) break; diff --git a/src/expr.c b/src/expr.c index 01ed63d1c..73478feb6 100644 --- a/src/expr.c +++ b/src/expr.c @@ -23,7 +23,7 @@ ************************************************************************* ** This file contains C code routines used for processing expressions ** -** $Id: expr.c,v 1.1 2000/05/31 15:34:53 drh Exp $ +** $Id: expr.c,v 1.2 2000/06/02 13:27:59 drh Exp $ */ #include "sqliteInt.h" @@ -77,10 +77,9 @@ int sqliteExprResolveIds(Parse *pParse, IdList *pTabList, Expr *pExpr){ /* A table name and field name: ID.ID */ case TK_DOT: { - int cnt = 0; /* Number of matches */ - int i; /* Loop counter */ + int cnt = 0; /* Number of matches */ + int i; /* Loop counter */ Expr *pLeft, *pRight; /* Left and right subbranches of the expr */ - int n; /* Length of an identifier */ char *zLeft, *zRight; /* Text of an identifier */ pLeft = pExpr->pLeft; diff --git a/src/insert.c b/src/insert.c index 85f9af86d..3edd78b7c 100644 --- a/src/insert.c +++ b/src/insert.c @@ -24,7 +24,7 @@ ** This file contains C code routines that are called by the parser ** to handle INSERT statements. ** -** $Id: insert.c,v 1.2 2000/06/02 01:17:37 drh Exp $ +** $Id: insert.c,v 1.3 2000/06/02 13:27:59 drh Exp $ */ #include "sqliteInt.h" @@ -43,7 +43,7 @@ void sqliteInsert( ){ Table *pTab; char *zTab; - int i, j; + int i, j, idx; Vdbe *v; zTab = sqliteTableNameFromToken(pTableName); @@ -105,6 +105,9 @@ void sqliteInsert( if( v ){ Index *pIdx; sqliteVdbeAddOp(v, OP_Open, 0, 1, pTab->zName, 0); + for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){ + sqliteVdbeAddOp(v, OP_Open, idx, 1, pIdx->zName, 0); + } sqliteVdbeAddOp(v, OP_New, 0, 0, 0, 0); if( pTab->pIndex ){ sqliteVdbeAddOp(v, OP_Dup, 0, 0, 0, 0); @@ -126,11 +129,10 @@ void sqliteInsert( sqliteVdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0, 0, 0); sqliteVdbeAddOp(v, OP_Put, 0, 0, 0, 0); sqliteVdbeAddOp(v, OP_Close, 0, 0, 0, 0); - for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ + for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){ if( pIdx->pNext ){ sqliteVdbeAddOp(v, OP_Dup, 0, 0, 0, 0); } - sqliteVdbeAddOp(v, OP_Open, 0, 1, pIdx->zName, 0); for(i=0; i<pIdx->nField; i++){ int idx = pIdx->aiField[i]; if( pField==0 ){ @@ -147,8 +149,8 @@ void sqliteInsert( } } sqliteVdbeAddOp(v, OP_MakeKey, pIdx->nField, 0, 0, 0); - sqliteVdbeAddOp(v, OP_PutIdx, 0, 0, 0, 0); - sqliteVdbeAddOp(v, OP_Close, 0, 0, 0, 0); + sqliteVdbeAddOp(v, OP_PutIdx, idx, 0, 0, 0); + sqliteVdbeAddOp(v, OP_Close, idx, 0, 0, 0); } } diff --git a/src/main.c b/src/main.c index c035302b2..6b348a86c 100644 --- a/src/main.c +++ b/src/main.c @@ -26,7 +26,7 @@ ** other files are for internal use by SQLite and should not be ** accessed by users of the library. ** -** $Id: main.c,v 1.5 2000/06/02 01:51:20 drh Exp $ +** $Id: main.c,v 1.6 2000/06/02 13:27:59 drh Exp $ */ #include "sqliteInt.h" @@ -54,6 +54,13 @@ static int sqliteOpenCb(void *pDb, int argc, char **argv, char **azColName){ ** Attempt to read the database schema and initialize internal ** data structures. Return one of the SQLITE_ error codes to ** indicate success or failure. +** +** After the database is initialized, the SQLITE_Initialized +** bit is set in the flags field of the sqlite structure. An +** attempt is made to initialize the database as soon as it +** is opened. If that fails (perhaps because another process +** has the sqlite_master table locked) than another attempt +** is made the first time the database is accessed. */ static int sqliteInit(sqlite *db, char **pzErrMsg){ Vdbe *vdbe; @@ -177,6 +184,9 @@ sqlite *sqlite_open(const char *zFilename, int mode, char **pzErrMsg){ if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){ sqlite_close(db); return 0; + }else{ + sqliteFree(pzErrMsg); + *pzErrMsg = 0; } return db; } @@ -230,7 +240,14 @@ int sqlite_complete(const char *zSql){ } /* -** Execute SQL code +** Execute SQL code. Return one of the SQLITE_ success/failure +** codes. Also write an error message into memory obtained from +** malloc() and make *pzErrMsg point to that message. +** +** If the SQL is a query, then for each row in the query result +** the xCallback() function is called. pArg becomes the first +** argument to xCallback(). If xCallback=NULL then no callback +** is invoked, even for queries. */ int sqlite_exec( sqlite *db, /* The database on which the SQL executes */ diff --git a/src/shell.c b/src/shell.c index 86202d1d2..252140121 100644 --- a/src/shell.c +++ b/src/shell.c @@ -24,7 +24,7 @@ ** This file contains code to implement the "sqlite" command line ** utility for accessing SQLite databases. ** -** $Id: shell.c,v 1.5 2000/05/31 23:33:17 drh Exp $ +** $Id: shell.c,v 1.6 2000/06/02 13:27:59 drh Exp $ */ #include <stdlib.h> #include <string.h> @@ -53,7 +53,6 @@ static char *getline(char *zPrompt){ char *zLine; int nLine; - char *z; int n; int eol; @@ -433,7 +432,7 @@ int main(int argc, char **argv){ argc--; argv++; }else if( argc>=3 && strcmp(argv[0],"-separator")==0 ){ - sprintf(data.separator,"%.*s",sizeof(data.separator)-1,argv[2]); + sprintf(data.separator,"%.*s",(int)sizeof(data.separator)-1,argv[2]); argc -= 2; argv += 2; }else if( strcmp(argv[1],"-header")==0 ){ diff --git a/src/sqliteInt.h b/src/sqliteInt.h index 4bd1f46ca..bfabcc379 100644 --- a/src/sqliteInt.h +++ b/src/sqliteInt.h @@ -23,7 +23,7 @@ ************************************************************************* ** Internal interface definitions for SQLite. ** -** @(#) $Id: sqliteInt.h,v 1.9 2000/06/02 01:51:20 drh Exp $ +** @(#) $Id: sqliteInt.h,v 1.10 2000/06/02 13:28:00 drh Exp $ */ #include "sqlite.h" #include "dbbe.h" @@ -269,3 +269,4 @@ int sqliteLikeCompare(const unsigned char*,const unsigned char*); char *sqliteTableNameFromToken(Token*); int sqliteExprCheck(Parse*, Expr*, int, int*); int sqliteFuncId(Token*); +int sqliteExprResolveIds(Parse*, IdList*, Expr*); diff --git a/src/util.c b/src/util.c index 23789fdc4..d8a68f2a3 100644 --- a/src/util.c +++ b/src/util.c @@ -26,7 +26,7 @@ ** This file contains functions for allocating memory, comparing ** strings, and stuff like that. ** -** $Id: util.c,v 1.8 2000/05/31 22:58:39 drh Exp $ +** $Id: util.c,v 1.9 2000/06/02 13:28:00 drh Exp $ */ #include "sqliteInt.h" #include <stdarg.h> @@ -683,8 +683,6 @@ int sqliteGlobCompare(const char *zPattern, const char *zString){ int sqliteLikeCompare(const unsigned char *zPattern, const unsigned char *zString){ register char c; - int invert; - int seen; char c2; while( (c = UpperToLower[*zPattern])!=0 ){ |