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