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 | 56c5816 | 2000-06-07 15:24:40 +0000 | [diff] [blame] | 26 | ** @(#) $Id: sqliteInt.h,v 1.21 2000/06/07 15:24:40 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 | 56c5816 | 2000-06-07 15:24:40 +0000 | [diff] [blame] | 38 | /* #define MEMORY_DEBUG 1 */ |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 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__) |
drh | c3c2fc9 | 2000-05-31 22:58:39 +0000 | [diff] [blame] | 43 | void sqliteStrRealloc(char**); |
| 44 | #else |
| 45 | # define sqliteStrRealloc(X) |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 46 | #endif |
| 47 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 48 | /* |
| 49 | ** The number of entries in the in-memory hash table holding the |
| 50 | ** schema. |
| 51 | */ |
| 52 | #define N_HASH 51 |
| 53 | |
| 54 | /* |
| 55 | ** Name of the master database table. The master database table |
| 56 | ** is a special table that holds the names and attributes of all |
| 57 | ** user tables and indices. |
| 58 | */ |
| 59 | #define MASTER_NAME "sqlite_master" |
| 60 | |
| 61 | /* |
| 62 | ** A convenience macro that returns the number of elements in |
| 63 | ** an array. |
| 64 | */ |
| 65 | #define ArraySize(X) (sizeof(X)/sizeof(X[0])) |
| 66 | |
| 67 | /* |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 68 | ** Integer identifiers for functions. |
| 69 | */ |
| 70 | #define FN_Unknown 0 |
| 71 | #define FN_Count 1 |
| 72 | #define FN_Min 2 |
| 73 | #define FN_Max 3 |
| 74 | #define FN_Sum 4 |
| 75 | #define FN_Avg 5 |
| 76 | |
| 77 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 78 | ** Forward references to structures |
| 79 | */ |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 80 | typedef struct Column Column; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 81 | typedef struct Table Table; |
| 82 | typedef struct Index Index; |
| 83 | typedef struct Instruction Instruction; |
| 84 | typedef struct Expr Expr; |
| 85 | typedef struct ExprList ExprList; |
| 86 | typedef struct Parse Parse; |
| 87 | typedef struct Token Token; |
| 88 | typedef struct IdList IdList; |
| 89 | typedef struct WhereInfo WhereInfo; |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 90 | typedef struct Select Select; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 91 | typedef struct AggExpr AggExpr; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 92 | |
| 93 | /* |
| 94 | ** Each database is an instance of the following structure |
| 95 | */ |
| 96 | struct sqlite { |
| 97 | Dbbe *pBe; /* The backend driver */ |
| 98 | int flags; /* Miscellanous flags */ |
| 99 | Table *apTblHash[N_HASH]; /* All tables of the database */ |
| 100 | Index *apIdxHash[N_HASH]; /* All indices of the database */ |
| 101 | }; |
| 102 | |
| 103 | /* |
| 104 | ** Possible values for the flags field of sqlite |
| 105 | */ |
| 106 | #define SQLITE_VdbeTrace 0x00000001 |
drh | 58b9576 | 2000-06-02 01:17:37 +0000 | [diff] [blame] | 107 | #define SQLITE_Initialized 0x00000002 |
| 108 | |
| 109 | /* |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 110 | ** information about each column of a table is held in an instance |
| 111 | ** of this structure. |
| 112 | */ |
| 113 | struct Column { |
| 114 | char *zName; /* Name of this column */ |
| 115 | char *zDflt; /* Default value of this column */ |
| 116 | int notNull; /* True if there is a NOT NULL constraing */ |
| 117 | }; |
| 118 | |
| 119 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 120 | ** Each table is represented in memory by |
| 121 | ** an instance of the following structure |
| 122 | */ |
| 123 | struct Table { |
| 124 | char *zName; /* Name of the table */ |
| 125 | Table *pHash; /* Next table with same hash on zName */ |
| 126 | int nCol; /* Number of columns in this table */ |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 127 | Column *aCol; /* Information about each column */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 128 | int readOnly; /* True if this table should not be written by the user */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 129 | Index *pIndex; /* List of indices on this table. */ |
| 130 | }; |
| 131 | |
| 132 | /* |
| 133 | ** Each index is represented in memory by and |
| 134 | ** instance of the following structure. |
| 135 | */ |
| 136 | struct Index { |
| 137 | char *zName; /* Name of this index */ |
| 138 | Index *pHash; /* Next index with the same hash on zName */ |
| 139 | int nField; /* Number of fields in the table indexed by this index */ |
| 140 | int *aiField; /* Indices of fields used by this index. 1st is 0 */ |
| 141 | Table *pTable; /* The table being indexed */ |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 142 | int isUnique; /* True if keys must all be unique */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 143 | Index *pNext; /* The next index associated with the same table */ |
| 144 | }; |
| 145 | |
| 146 | /* |
| 147 | ** Each token coming out of the lexer is an instance of |
| 148 | ** this structure. |
| 149 | */ |
| 150 | struct Token { |
| 151 | char *z; /* Text of the token */ |
| 152 | int n; /* Number of characters in this token */ |
| 153 | }; |
| 154 | |
| 155 | /* |
| 156 | ** Each node of an expression in the parse tree is an instance |
| 157 | ** of this structure |
| 158 | */ |
| 159 | struct Expr { |
| 160 | int op; /* Operation performed by this node */ |
| 161 | Expr *pLeft, *pRight; /* Left and right subnodes */ |
| 162 | ExprList *pList; /* A list of expressions used as a function argument */ |
| 163 | Token token; /* An operand token */ |
| 164 | int iTable, iField; /* When op==TK_FIELD, then this node means the |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 165 | ** iField-th field of the iTable-th table. When |
| 166 | ** op==TK_FUNCTION, iField holds the function id */ |
| 167 | int iAgg; /* When op==TK_FIELD and pParse->useAgg==TRUE, pull |
| 168 | ** value from these element of the aggregator */ |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 169 | Select *pSelect; /* When the expression is a sub-select */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 170 | }; |
| 171 | |
| 172 | /* |
| 173 | ** A list of expressions. Each expression may optionally have a |
| 174 | ** name. An expr/name combination can be used in several ways, such |
| 175 | ** as the list of "expr AS ID" fields following a "SELECT" or in the |
| 176 | ** list of "ID = expr" items in an UPDATE. A list of expressions can |
| 177 | ** also be used as the argument to a function, in which case the azName |
| 178 | ** field is not used. |
| 179 | */ |
| 180 | struct ExprList { |
| 181 | int nExpr; /* Number of expressions on the list */ |
| 182 | struct { |
| 183 | Expr *pExpr; /* The list of expressions */ |
| 184 | char *zName; /* Token associated with this expression */ |
| 185 | int idx; /* ... */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 186 | int isAgg; /* True if this is an aggregate like count(*) */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 187 | } *a; /* One entry for each expression */ |
| 188 | }; |
| 189 | |
| 190 | /* |
| 191 | ** A list of identifiers. |
| 192 | */ |
| 193 | struct IdList { |
| 194 | int nId; /* Number of identifiers on the list */ |
| 195 | struct { |
| 196 | char *zName; /* Text of the identifier. */ |
| 197 | char *zAlias; /* The "B" part of a "A AS B" phrase. zName is the "A" */ |
| 198 | Table *pTab; /* Table corresponding to zName */ |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 199 | int idx; /* Index of a field named zName in a table */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 200 | } *a; /* One entry for each identifier on the list */ |
| 201 | }; |
| 202 | |
| 203 | /* |
| 204 | ** The WHERE clause processing routine has two halves. The |
| 205 | ** first part does the start of the WHERE loop and the second |
| 206 | ** half does the tail of the WHERE loop. An instance of |
| 207 | ** this structure is returned by the first half and passed |
| 208 | ** into the second half to give some continuity. |
| 209 | */ |
| 210 | struct WhereInfo { |
| 211 | Parse *pParse; |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 212 | IdList *pTabList; /* List of tables in the join */ |
| 213 | int iContinue; /* Jump here to continue with next record */ |
| 214 | int iBreak; /* Jump here to break out of the loop */ |
| 215 | int base; /* Index of first Open opcode */ |
| 216 | Index *aIdx[32]; /* Indices used for each table */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 217 | }; |
| 218 | |
| 219 | /* |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 220 | ** An instance of the following structure contains all information |
| 221 | ** needed to generate code for a single SELECT statement. |
| 222 | */ |
| 223 | struct Select { |
| 224 | int isDistinct; /* True if the DISTINCT keyword is present */ |
| 225 | ExprList *pEList; /* The fields of the result */ |
| 226 | IdList *pSrc; /* The FROM clause */ |
| 227 | Expr *pWhere; /* The WHERE clause */ |
| 228 | ExprList *pGroupBy; /* The GROUP BY clause */ |
| 229 | Expr *pHaving; /* The HAVING clause */ |
| 230 | ExprList *pOrderBy; /* The ORDER BY clause */ |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 231 | int op; /* One of: TK_UNION TK_ALL TK_INTERSECT TK_EXCEPT */ |
| 232 | Select *pPrior; /* Prior select to which this one joins */ |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 233 | }; |
| 234 | |
| 235 | /* |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 236 | ** The results of a select can be distributed in several ways. |
| 237 | */ |
| 238 | #define SRT_Callback 1 /* Invoke a callback with each row of result */ |
| 239 | #define SRT_Mem 2 /* Store result in a memory cell */ |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 240 | #define SRT_Set 3 /* Store result as unique keys in a table */ |
| 241 | #define SRT_Union 5 /* Store result as keys in a table */ |
| 242 | #define SRT_Except 6 /* Remove result from a UNION table */ |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 243 | #define SRT_Table 7 /* Store result as data with a unique key */ |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 244 | |
| 245 | /* |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 246 | ** When a SELECT uses aggregate functions (like "count(*)" or "avg(f1)") |
| 247 | ** we have to do some additional analysis of expressions. An instance |
| 248 | ** of the following structure holds information about a single subexpression |
| 249 | ** somewhere in the SELECT statement. An array of these structures holds |
| 250 | ** all the information we need to generate code for aggregate |
| 251 | ** expressions. |
| 252 | ** |
| 253 | ** Note that when analyzing a SELECT containing aggregates, both |
| 254 | ** non-aggregate field variables and aggregate functions are stored |
| 255 | ** in the AggExpr array of the Parser structure. |
| 256 | ** |
| 257 | ** The pExpr field points to an expression that is part of either the |
| 258 | ** field list, the GROUP BY clause, the HAVING clause or the ORDER BY |
| 259 | ** clause. The expression will be freed when those clauses are cleaned |
| 260 | ** up. Do not try to delete the expression attached to AggExpr.pExpr. |
| 261 | ** |
| 262 | ** If AggExpr.pExpr==0, that means the expression is "count(*)". |
| 263 | */ |
| 264 | struct AggExpr { |
| 265 | int isAgg; /* if TRUE contains an aggregate function */ |
| 266 | Expr *pExpr; /* The expression */ |
| 267 | }; |
| 268 | |
| 269 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 270 | ** An SQL parser context |
| 271 | */ |
| 272 | struct Parse { |
| 273 | sqlite *db; /* The main database structure */ |
| 274 | sqlite_callback xCallback; /* The callback function */ |
| 275 | void *pArg; /* First argument to the callback function */ |
| 276 | char *zErrMsg; /* An error message */ |
| 277 | Token sErrToken; /* The token at which the error occurred */ |
| 278 | Token sFirstToken; /* The first token parsed */ |
| 279 | Token sLastToken; /* The last token parsed */ |
| 280 | Table *pNewTable; /* A table being constructed by CREATE TABLE */ |
| 281 | Vdbe *pVdbe; /* An engine for executing database bytecode */ |
| 282 | int explain; /* True if the EXPLAIN flag is found on the query */ |
| 283 | int initFlag; /* True if reparsing CREATE TABLEs */ |
| 284 | int nErr; /* Number of errors seen */ |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 285 | int nTab; /* Number of previously allocated cursors */ |
| 286 | int nMem; /* Number of memory cells used so far */ |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 287 | int nSet; /* Number of sets used so far */ |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 288 | int nAgg; /* Number of aggregate expressions */ |
| 289 | AggExpr *aAgg; /* An array of aggregate expressions */ |
| 290 | int iAggCount; /* Index of the count(*) aggregate in aAgg[] */ |
| 291 | int useAgg; /* If true, extract field values from the aggregator |
| 292 | ** while generating expressions. Normally false */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 293 | }; |
| 294 | |
| 295 | /* |
| 296 | ** Internal function prototypes |
| 297 | */ |
| 298 | int sqliteStrICmp(const char *, const char *); |
| 299 | int sqliteStrNICmp(const char *, const char *, int); |
| 300 | int sqliteHashNoCase(const char *, int); |
| 301 | int sqliteCompare(const char *, const char *); |
| 302 | int sqliteSortCompare(const char *, const char *); |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 303 | #ifdef MEMORY_DEBUG |
| 304 | void *sqliteMalloc_(int,char*,int); |
| 305 | void sqliteFree_(void*,char*,int); |
| 306 | void *sqliteRealloc_(void*,int,char*,int); |
| 307 | #else |
| 308 | void *sqliteMalloc(int); |
| 309 | void sqliteFree(void*); |
| 310 | void *sqliteRealloc(void*,int); |
| 311 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 312 | int sqliteGetToken(const char*, int *); |
| 313 | void sqliteSetString(char **, const char *, ...); |
| 314 | void sqliteSetNString(char **, ...); |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 315 | void sqliteDequote(char*); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 316 | int sqliteRunParser(Parse*, char*, char **); |
| 317 | void sqliteExec(Parse*); |
| 318 | Expr *sqliteExpr(int, Expr*, Expr*, Token*); |
| 319 | Expr *sqliteExprFunction(ExprList*, Token*); |
| 320 | void sqliteExprDelete(Expr*); |
| 321 | ExprList *sqliteExprListAppend(ExprList*,Expr*,Token*); |
| 322 | void sqliteExprListDelete(ExprList*); |
| 323 | void sqliteStartTable(Parse*,Token*,Token*); |
| 324 | void sqliteAddColumn(Parse*,Token*); |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 325 | void sqliteAddDefaultValue(Parse*,Token*,int); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 326 | void sqliteEndTable(Parse*,Token*); |
| 327 | void sqliteDropTable(Parse*, Token*); |
| 328 | void sqliteDeleteTable(sqlite*, Table*); |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 329 | void sqliteInsert(Parse*, Token*, ExprList*, Select*, IdList*); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 330 | IdList *sqliteIdListAppend(IdList*, Token*); |
| 331 | void sqliteIdListAddAlias(IdList*, Token*); |
| 332 | void sqliteIdListDelete(IdList*); |
| 333 | void sqliteCreateIndex(Parse*, Token*, Token*, IdList*, Token*, Token*); |
| 334 | void sqliteDropIndex(Parse*, Token*); |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 335 | int sqliteSelect(Parse*, Select*, int, int); |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 336 | Select *sqliteSelectNew(ExprList*,IdList*,Expr*,ExprList*,Expr*,ExprList*,int); |
| 337 | void sqliteSelectDelete(Select*); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 338 | void sqliteDeleteFrom(Parse*, Token*, Expr*); |
| 339 | void sqliteUpdate(Parse*, Token*, ExprList*, Expr*); |
| 340 | WhereInfo *sqliteWhereBegin(Parse*, IdList*, Expr*, int); |
| 341 | void sqliteWhereEnd(WhereInfo*); |
| 342 | void sqliteExprCode(Parse*, Expr*); |
| 343 | void sqliteExprIfTrue(Parse*, Expr*, int); |
| 344 | void sqliteExprIfFalse(Parse*, Expr*, int); |
| 345 | Table *sqliteFindTable(sqlite*,char*); |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 346 | void sqliteCopy(Parse*, Token*, Token*, Token*); |
drh | dce2cbe | 2000-05-31 02:27:49 +0000 | [diff] [blame] | 347 | void sqliteVacuum(Parse*, Token*); |
| 348 | int sqliteGlobCompare(const char*,const char*); |
| 349 | int sqliteLikeCompare(const unsigned char*,const unsigned char*); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 350 | char *sqliteTableNameFromToken(Token*); |
| 351 | int sqliteExprCheck(Parse*, Expr*, int, int*); |
| 352 | int sqliteFuncId(Token*); |
drh | bed8690 | 2000-06-02 13:27:59 +0000 | [diff] [blame] | 353 | int sqliteExprResolveIds(Parse*, IdList*, Expr*); |
drh | 4794b98 | 2000-06-06 13:54:14 +0000 | [diff] [blame] | 354 | void sqliteExprResolveInSelect(Parse*, Expr*); |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 355 | int sqliteExprAnalyzeAggregates(Parse*, Expr*); |
| 356 | void sqlitePArseInfoReset(Parse*); |