1*75897234Sdrh /* 2*75897234Sdrh ** Copyright (c) 1999, 2000 D. Richard Hipp 3*75897234Sdrh ** 4*75897234Sdrh ** This program is free software; you can redistribute it and/or 5*75897234Sdrh ** modify it under the terms of the GNU General Public 6*75897234Sdrh ** License as published by the Free Software Foundation; either 7*75897234Sdrh ** version 2 of the License, or (at your option) any later version. 8*75897234Sdrh ** 9*75897234Sdrh ** This program is distributed in the hope that it will be useful, 10*75897234Sdrh ** but WITHOUT ANY WARRANTY; without even the implied warranty of 11*75897234Sdrh ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12*75897234Sdrh ** General Public License for more details. 13*75897234Sdrh ** 14*75897234Sdrh ** You should have received a copy of the GNU General Public 15*75897234Sdrh ** License along with this library; if not, write to the 16*75897234Sdrh ** Free Software Foundation, Inc., 59 Temple Place - Suite 330, 17*75897234Sdrh ** Boston, MA 02111-1307, USA. 18*75897234Sdrh ** 19*75897234Sdrh ** Author contact information: 20*75897234Sdrh ** [email protected] 21*75897234Sdrh ** http://www.hwaci.com/drh/ 22*75897234Sdrh ** 23*75897234Sdrh ************************************************************************* 24*75897234Sdrh ** Internal interface definitions for SQLite. 25*75897234Sdrh ** 26*75897234Sdrh ** @(#) $Id: sqliteInt.h,v 1.1 2000/05/29 14:26:01 drh Exp $ 27*75897234Sdrh */ 28*75897234Sdrh #include "sqlite.h" 29*75897234Sdrh #include "dbbe.h" 30*75897234Sdrh #include "vdbe.h" 31*75897234Sdrh #include "parse.h" 32*75897234Sdrh #include <gdbm.h> 33*75897234Sdrh #include <stdio.h> 34*75897234Sdrh #include <stdlib.h> 35*75897234Sdrh #include <string.h> 36*75897234Sdrh #include <assert.h> 37*75897234Sdrh 38*75897234Sdrh /* 39*75897234Sdrh ** The number of entries in the in-memory hash table holding the 40*75897234Sdrh ** schema. 41*75897234Sdrh */ 42*75897234Sdrh #define N_HASH 51 43*75897234Sdrh 44*75897234Sdrh /* 45*75897234Sdrh ** Name of the master database table. The master database table 46*75897234Sdrh ** is a special table that holds the names and attributes of all 47*75897234Sdrh ** user tables and indices. 48*75897234Sdrh */ 49*75897234Sdrh #define MASTER_NAME "sqlite_master" 50*75897234Sdrh 51*75897234Sdrh /* 52*75897234Sdrh ** A convenience macro that returns the number of elements in 53*75897234Sdrh ** an array. 54*75897234Sdrh */ 55*75897234Sdrh #define ArraySize(X) (sizeof(X)/sizeof(X[0])) 56*75897234Sdrh 57*75897234Sdrh /* 58*75897234Sdrh ** Forward references to structures 59*75897234Sdrh */ 60*75897234Sdrh typedef struct Table Table; 61*75897234Sdrh typedef struct Index Index; 62*75897234Sdrh typedef struct Instruction Instruction; 63*75897234Sdrh typedef struct Expr Expr; 64*75897234Sdrh typedef struct ExprList ExprList; 65*75897234Sdrh typedef struct Parse Parse; 66*75897234Sdrh typedef struct Token Token; 67*75897234Sdrh typedef struct IdList IdList; 68*75897234Sdrh typedef struct WhereInfo WhereInfo; 69*75897234Sdrh 70*75897234Sdrh /* 71*75897234Sdrh ** Each database is an instance of the following structure 72*75897234Sdrh */ 73*75897234Sdrh struct sqlite { 74*75897234Sdrh Dbbe *pBe; /* The backend driver */ 75*75897234Sdrh int flags; /* Miscellanous flags */ 76*75897234Sdrh Table *apTblHash[N_HASH]; /* All tables of the database */ 77*75897234Sdrh Index *apIdxHash[N_HASH]; /* All indices of the database */ 78*75897234Sdrh }; 79*75897234Sdrh 80*75897234Sdrh /* 81*75897234Sdrh ** Possible values for the flags field of sqlite 82*75897234Sdrh */ 83*75897234Sdrh #define SQLITE_VdbeTrace 0x00000001 84*75897234Sdrh 85*75897234Sdrh /* 86*75897234Sdrh ** Each table is represented in memory by 87*75897234Sdrh ** an instance of the following structure 88*75897234Sdrh */ 89*75897234Sdrh struct Table { 90*75897234Sdrh char *zName; /* Name of the table */ 91*75897234Sdrh Table *pHash; /* Next table with same hash on zName */ 92*75897234Sdrh int nCol; /* Number of columns in this table */ 93*75897234Sdrh int readOnly; /* True if this table should not be written by the user */ 94*75897234Sdrh char **azCol; /* Name of each column */ 95*75897234Sdrh Index *pIndex; /* List of indices on this table. */ 96*75897234Sdrh }; 97*75897234Sdrh 98*75897234Sdrh /* 99*75897234Sdrh ** Each index is represented in memory by and 100*75897234Sdrh ** instance of the following structure. 101*75897234Sdrh */ 102*75897234Sdrh struct Index { 103*75897234Sdrh char *zName; /* Name of this index */ 104*75897234Sdrh Index *pHash; /* Next index with the same hash on zName */ 105*75897234Sdrh int nField; /* Number of fields in the table indexed by this index */ 106*75897234Sdrh int *aiField; /* Indices of fields used by this index. 1st is 0 */ 107*75897234Sdrh Table *pTable; /* The table being indexed */ 108*75897234Sdrh Index *pNext; /* The next index associated with the same table */ 109*75897234Sdrh }; 110*75897234Sdrh 111*75897234Sdrh /* 112*75897234Sdrh ** Each token coming out of the lexer is an instance of 113*75897234Sdrh ** this structure. 114*75897234Sdrh */ 115*75897234Sdrh struct Token { 116*75897234Sdrh char *z; /* Text of the token */ 117*75897234Sdrh int n; /* Number of characters in this token */ 118*75897234Sdrh }; 119*75897234Sdrh 120*75897234Sdrh /* 121*75897234Sdrh ** Each node of an expression in the parse tree is an instance 122*75897234Sdrh ** of this structure 123*75897234Sdrh */ 124*75897234Sdrh struct Expr { 125*75897234Sdrh int op; /* Operation performed by this node */ 126*75897234Sdrh Expr *pLeft, *pRight; /* Left and right subnodes */ 127*75897234Sdrh ExprList *pList; /* A list of expressions used as a function argument */ 128*75897234Sdrh Token token; /* An operand token */ 129*75897234Sdrh int iTable, iField; /* When op==TK_FIELD, then this node means the 130*75897234Sdrh ** iField-th field of the iTable-th table */ 131*75897234Sdrh }; 132*75897234Sdrh 133*75897234Sdrh /* 134*75897234Sdrh ** A list of expressions. Each expression may optionally have a 135*75897234Sdrh ** name. An expr/name combination can be used in several ways, such 136*75897234Sdrh ** as the list of "expr AS ID" fields following a "SELECT" or in the 137*75897234Sdrh ** list of "ID = expr" items in an UPDATE. A list of expressions can 138*75897234Sdrh ** also be used as the argument to a function, in which case the azName 139*75897234Sdrh ** field is not used. 140*75897234Sdrh */ 141*75897234Sdrh struct ExprList { 142*75897234Sdrh int nExpr; /* Number of expressions on the list */ 143*75897234Sdrh struct { 144*75897234Sdrh Expr *pExpr; /* The list of expressions */ 145*75897234Sdrh char *zName; /* Token associated with this expression */ 146*75897234Sdrh int idx; /* ... */ 147*75897234Sdrh } *a; /* One entry for each expression */ 148*75897234Sdrh }; 149*75897234Sdrh 150*75897234Sdrh /* 151*75897234Sdrh ** A list of identifiers. 152*75897234Sdrh */ 153*75897234Sdrh struct IdList { 154*75897234Sdrh int nId; /* Number of identifiers on the list */ 155*75897234Sdrh struct { 156*75897234Sdrh char *zName; /* Text of the identifier. */ 157*75897234Sdrh char *zAlias; /* The "B" part of a "A AS B" phrase. zName is the "A" */ 158*75897234Sdrh Table *pTab; /* Table corresponding to zName */ 159*75897234Sdrh int idx; /* Index of a field name in the table */ 160*75897234Sdrh } *a; /* One entry for each identifier on the list */ 161*75897234Sdrh }; 162*75897234Sdrh 163*75897234Sdrh /* 164*75897234Sdrh ** The WHERE clause processing routine has two halves. The 165*75897234Sdrh ** first part does the start of the WHERE loop and the second 166*75897234Sdrh ** half does the tail of the WHERE loop. An instance of 167*75897234Sdrh ** this structure is returned by the first half and passed 168*75897234Sdrh ** into the second half to give some continuity. 169*75897234Sdrh */ 170*75897234Sdrh struct WhereInfo { 171*75897234Sdrh Parse *pParse; 172*75897234Sdrh IdList *pTabList; 173*75897234Sdrh int iContinue; 174*75897234Sdrh int iBreak; 175*75897234Sdrh }; 176*75897234Sdrh 177*75897234Sdrh /* 178*75897234Sdrh ** An SQL parser context 179*75897234Sdrh */ 180*75897234Sdrh struct Parse { 181*75897234Sdrh sqlite *db; /* The main database structure */ 182*75897234Sdrh sqlite_callback xCallback; /* The callback function */ 183*75897234Sdrh void *pArg; /* First argument to the callback function */ 184*75897234Sdrh char *zErrMsg; /* An error message */ 185*75897234Sdrh Token sErrToken; /* The token at which the error occurred */ 186*75897234Sdrh Token sFirstToken; /* The first token parsed */ 187*75897234Sdrh Token sLastToken; /* The last token parsed */ 188*75897234Sdrh Table *pNewTable; /* A table being constructed by CREATE TABLE */ 189*75897234Sdrh Vdbe *pVdbe; /* An engine for executing database bytecode */ 190*75897234Sdrh int explain; /* True if the EXPLAIN flag is found on the query */ 191*75897234Sdrh int initFlag; /* True if reparsing CREATE TABLEs */ 192*75897234Sdrh int nErr; /* Number of errors seen */ 193*75897234Sdrh }; 194*75897234Sdrh 195*75897234Sdrh /* 196*75897234Sdrh ** Internal function prototypes 197*75897234Sdrh */ 198*75897234Sdrh int sqliteStrICmp(const char *, const char *); 199*75897234Sdrh int sqliteStrNICmp(const char *, const char *, int); 200*75897234Sdrh int sqliteHashNoCase(const char *, int); 201*75897234Sdrh int sqliteCompare(const char *, const char *); 202*75897234Sdrh int sqliteSortCompare(const char *, const char *); 203*75897234Sdrh void *sqliteMalloc(int); 204*75897234Sdrh void sqliteFree(void*); 205*75897234Sdrh void *sqliteRealloc(void*,int); 206*75897234Sdrh int sqliteGetToken(const char*, int *); 207*75897234Sdrh void sqliteSetString(char **, const char *, ...); 208*75897234Sdrh void sqliteSetNString(char **, ...); 209*75897234Sdrh int sqliteRunParser(Parse*, char*, char **); 210*75897234Sdrh void sqliteExec(Parse*); 211*75897234Sdrh Expr *sqliteExpr(int, Expr*, Expr*, Token*); 212*75897234Sdrh Expr *sqliteExprFunction(ExprList*, Token*); 213*75897234Sdrh void sqliteExprDelete(Expr*); 214*75897234Sdrh ExprList *sqliteExprListAppend(ExprList*,Expr*,Token*); 215*75897234Sdrh void sqliteExprListDelete(ExprList*); 216*75897234Sdrh void sqliteStartTable(Parse*,Token*,Token*); 217*75897234Sdrh void sqliteAddColumn(Parse*,Token*); 218*75897234Sdrh void sqliteEndTable(Parse*,Token*); 219*75897234Sdrh void sqliteDropTable(Parse*, Token*); 220*75897234Sdrh void sqliteDeleteTable(sqlite*, Table*); 221*75897234Sdrh void sqliteInsert(Parse*, Token*, ExprList*, IdList*); 222*75897234Sdrh IdList *sqliteIdListAppend(IdList*, Token*); 223*75897234Sdrh void sqliteIdListAddAlias(IdList*, Token*); 224*75897234Sdrh void sqliteIdListDelete(IdList*); 225*75897234Sdrh void sqliteCreateIndex(Parse*, Token*, Token*, IdList*, Token*, Token*); 226*75897234Sdrh void sqliteDropIndex(Parse*, Token*); 227*75897234Sdrh void sqliteSelect(Parse*, ExprList*, IdList*, Expr*, ExprList*); 228*75897234Sdrh void sqliteDeleteFrom(Parse*, Token*, Expr*); 229*75897234Sdrh void sqliteUpdate(Parse*, Token*, ExprList*, Expr*); 230*75897234Sdrh WhereInfo *sqliteWhereBegin(Parse*, IdList*, Expr*, int); 231*75897234Sdrh void sqliteWhereEnd(WhereInfo*); 232*75897234Sdrh void sqliteExprCode(Parse*, Expr*); 233*75897234Sdrh void sqliteExprIfTrue(Parse*, Expr*, int); 234*75897234Sdrh void sqliteExprIfFalse(Parse*, Expr*, int); 235*75897234Sdrh Table *sqliteFindTable(sqlite*,char*); 236