blob: d4069eac8623adb1a0fb4ee80a7cff860312c5b9 [file] [log] [blame]
drh75897232000-05-29 14:26:00 +00001/*
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**
drh0bdaf622000-06-11 23:50:13 +000026** @(#) $Id: sqliteInt.h,v 1.24 2000/06/11 23:50:13 drh Exp $
drh75897232000-05-29 14:26:00 +000027*/
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
drh56c58162000-06-07 15:24:40 +000038/* #define MEMORY_DEBUG 1 */
drhdcc581c2000-05-30 13:44:19 +000039#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__)
drh6e142f52000-06-08 13:36:40 +000043# define sqliteStrDup(X) sqliteStrDup_(X,__FILE__,__LINE__)
44# define sqliteStrNDup(X,Y) sqliteStrNDup_(X,Y,__FILE__,__LINE__)
drhc3c2fc92000-05-31 22:58:39 +000045 void sqliteStrRealloc(char**);
46#else
47# define sqliteStrRealloc(X)
drhdcc581c2000-05-30 13:44:19 +000048#endif
49
drh75897232000-05-29 14:26:00 +000050/*
drh6e142f52000-06-08 13:36:40 +000051** The following global variables are used for testing and debugging
52** only. Thy only work if MEMORY_DEBUG is defined.
53*/
54#ifdef MEMORY_DEBUG
55int sqlite_nMalloc; /* Number of sqliteMalloc() calls */
56int sqlite_nFree; /* Number of sqliteFree() calls */
57int sqlite_iMallocFail; /* Fail sqliteMalloc() after this many calls */
58#endif
59
60/*
drh75897232000-05-29 14:26:00 +000061** The number of entries in the in-memory hash table holding the
62** schema.
63*/
64#define N_HASH 51
65
66/*
67** Name of the master database table. The master database table
68** is a special table that holds the names and attributes of all
69** user tables and indices.
70*/
71#define MASTER_NAME "sqlite_master"
72
73/*
74** A convenience macro that returns the number of elements in
75** an array.
76*/
77#define ArraySize(X) (sizeof(X)/sizeof(X[0]))
78
79/*
drhcce7d172000-05-31 15:34:51 +000080** Integer identifiers for functions.
81*/
82#define FN_Unknown 0
83#define FN_Count 1
84#define FN_Min 2
85#define FN_Max 3
86#define FN_Sum 4
87#define FN_Avg 5
drh0bdaf622000-06-11 23:50:13 +000088#define FN_Fcnt 6
drhcce7d172000-05-31 15:34:51 +000089
90/*
drh75897232000-05-29 14:26:00 +000091** Forward references to structures
92*/
drh7020f652000-06-03 18:06:52 +000093typedef struct Column Column;
drh75897232000-05-29 14:26:00 +000094typedef struct Table Table;
95typedef struct Index Index;
96typedef struct Instruction Instruction;
97typedef struct Expr Expr;
98typedef struct ExprList ExprList;
99typedef struct Parse Parse;
100typedef struct Token Token;
101typedef struct IdList IdList;
102typedef struct WhereInfo WhereInfo;
drh9bb61fe2000-06-05 16:01:39 +0000103typedef struct Select Select;
drh22827922000-06-06 17:27:05 +0000104typedef struct AggExpr AggExpr;
drh75897232000-05-29 14:26:00 +0000105
106/*
107** Each database is an instance of the following structure
108*/
109struct sqlite {
110 Dbbe *pBe; /* The backend driver */
111 int flags; /* Miscellanous flags */
112 Table *apTblHash[N_HASH]; /* All tables of the database */
113 Index *apIdxHash[N_HASH]; /* All indices of the database */
114};
115
116/*
117** Possible values for the flags field of sqlite
118*/
119#define SQLITE_VdbeTrace 0x00000001
drh58b95762000-06-02 01:17:37 +0000120#define SQLITE_Initialized 0x00000002
121
122/*
drh7020f652000-06-03 18:06:52 +0000123** information about each column of a table is held in an instance
124** of this structure.
125*/
126struct Column {
127 char *zName; /* Name of this column */
128 char *zDflt; /* Default value of this column */
129 int notNull; /* True if there is a NOT NULL constraing */
130};
131
132/*
drh75897232000-05-29 14:26:00 +0000133** Each table is represented in memory by
134** an instance of the following structure
135*/
136struct Table {
137 char *zName; /* Name of the table */
138 Table *pHash; /* Next table with same hash on zName */
139 int nCol; /* Number of columns in this table */
drh7020f652000-06-03 18:06:52 +0000140 Column *aCol; /* Information about each column */
drh75897232000-05-29 14:26:00 +0000141 int readOnly; /* True if this table should not be written by the user */
drh75897232000-05-29 14:26:00 +0000142 Index *pIndex; /* List of indices on this table. */
143};
144
145/*
146** Each index is represented in memory by and
147** instance of the following structure.
148*/
149struct Index {
150 char *zName; /* Name of this index */
151 Index *pHash; /* Next index with the same hash on zName */
152 int nField; /* Number of fields in the table indexed by this index */
153 int *aiField; /* Indices of fields used by this index. 1st is 0 */
154 Table *pTable; /* The table being indexed */
drh7020f652000-06-03 18:06:52 +0000155 int isUnique; /* True if keys must all be unique */
drh75897232000-05-29 14:26:00 +0000156 Index *pNext; /* The next index associated with the same table */
157};
158
159/*
160** Each token coming out of the lexer is an instance of
161** this structure.
162*/
163struct Token {
164 char *z; /* Text of the token */
165 int n; /* Number of characters in this token */
166};
167
168/*
169** Each node of an expression in the parse tree is an instance
170** of this structure
171*/
172struct Expr {
173 int op; /* Operation performed by this node */
174 Expr *pLeft, *pRight; /* Left and right subnodes */
175 ExprList *pList; /* A list of expressions used as a function argument */
176 Token token; /* An operand token */
177 int iTable, iField; /* When op==TK_FIELD, then this node means the
drh22827922000-06-06 17:27:05 +0000178 ** iField-th field of the iTable-th table. When
179 ** op==TK_FUNCTION, iField holds the function id */
180 int iAgg; /* When op==TK_FIELD and pParse->useAgg==TRUE, pull
181 ** value from these element of the aggregator */
drh19a775c2000-06-05 18:54:46 +0000182 Select *pSelect; /* When the expression is a sub-select */
drh75897232000-05-29 14:26:00 +0000183};
184
185/*
186** A list of expressions. Each expression may optionally have a
187** name. An expr/name combination can be used in several ways, such
188** as the list of "expr AS ID" fields following a "SELECT" or in the
189** list of "ID = expr" items in an UPDATE. A list of expressions can
190** also be used as the argument to a function, in which case the azName
191** field is not used.
192*/
193struct ExprList {
194 int nExpr; /* Number of expressions on the list */
195 struct {
196 Expr *pExpr; /* The list of expressions */
197 char *zName; /* Token associated with this expression */
drhd8bc7082000-06-07 23:51:50 +0000198 char sortOrder; /* 1 for DESC or 0 for ASC */
199 char isAgg; /* True if this is an aggregate like count(*) */
200 char done; /* A flag to indicate when processing is finished */
drh75897232000-05-29 14:26:00 +0000201 } *a; /* One entry for each expression */
202};
203
204/*
205** A list of identifiers.
206*/
207struct IdList {
208 int nId; /* Number of identifiers on the list */
209 struct {
210 char *zName; /* Text of the identifier. */
211 char *zAlias; /* The "B" part of a "A AS B" phrase. zName is the "A" */
212 Table *pTab; /* Table corresponding to zName */
drh19a775c2000-06-05 18:54:46 +0000213 int idx; /* Index of a field named zName in a table */
drh75897232000-05-29 14:26:00 +0000214 } *a; /* One entry for each identifier on the list */
215};
216
217/*
218** The WHERE clause processing routine has two halves. The
219** first part does the start of the WHERE loop and the second
220** half does the tail of the WHERE loop. An instance of
221** this structure is returned by the first half and passed
222** into the second half to give some continuity.
223*/
224struct WhereInfo {
225 Parse *pParse;
drh19a775c2000-06-05 18:54:46 +0000226 IdList *pTabList; /* List of tables in the join */
227 int iContinue; /* Jump here to continue with next record */
228 int iBreak; /* Jump here to break out of the loop */
229 int base; /* Index of first Open opcode */
230 Index *aIdx[32]; /* Indices used for each table */
drh75897232000-05-29 14:26:00 +0000231};
232
233/*
drh9bb61fe2000-06-05 16:01:39 +0000234** An instance of the following structure contains all information
235** needed to generate code for a single SELECT statement.
236*/
237struct Select {
238 int isDistinct; /* True if the DISTINCT keyword is present */
239 ExprList *pEList; /* The fields of the result */
240 IdList *pSrc; /* The FROM clause */
241 Expr *pWhere; /* The WHERE clause */
242 ExprList *pGroupBy; /* The GROUP BY clause */
243 Expr *pHaving; /* The HAVING clause */
244 ExprList *pOrderBy; /* The ORDER BY clause */
drh82c3d632000-06-06 21:56:07 +0000245 int op; /* One of: TK_UNION TK_ALL TK_INTERSECT TK_EXCEPT */
246 Select *pPrior; /* Prior select to which this one joins */
drh9bb61fe2000-06-05 16:01:39 +0000247};
248
249/*
drhfef52082000-06-06 01:50:43 +0000250** The results of a select can be distributed in several ways.
251*/
252#define SRT_Callback 1 /* Invoke a callback with each row of result */
253#define SRT_Mem 2 /* Store result in a memory cell */
drh82c3d632000-06-06 21:56:07 +0000254#define SRT_Set 3 /* Store result as unique keys in a table */
255#define SRT_Union 5 /* Store result as keys in a table */
256#define SRT_Except 6 /* Remove result from a UNION table */
drh5974a302000-06-07 14:42:26 +0000257#define SRT_Table 7 /* Store result as data with a unique key */
drhfef52082000-06-06 01:50:43 +0000258
259/*
drh22827922000-06-06 17:27:05 +0000260** When a SELECT uses aggregate functions (like "count(*)" or "avg(f1)")
261** we have to do some additional analysis of expressions. An instance
262** of the following structure holds information about a single subexpression
263** somewhere in the SELECT statement. An array of these structures holds
264** all the information we need to generate code for aggregate
265** expressions.
266**
267** Note that when analyzing a SELECT containing aggregates, both
268** non-aggregate field variables and aggregate functions are stored
269** in the AggExpr array of the Parser structure.
270**
271** The pExpr field points to an expression that is part of either the
272** field list, the GROUP BY clause, the HAVING clause or the ORDER BY
273** clause. The expression will be freed when those clauses are cleaned
274** up. Do not try to delete the expression attached to AggExpr.pExpr.
275**
276** If AggExpr.pExpr==0, that means the expression is "count(*)".
277*/
278struct AggExpr {
279 int isAgg; /* if TRUE contains an aggregate function */
280 Expr *pExpr; /* The expression */
281};
282
283/*
drh75897232000-05-29 14:26:00 +0000284** An SQL parser context
285*/
286struct Parse {
287 sqlite *db; /* The main database structure */
288 sqlite_callback xCallback; /* The callback function */
289 void *pArg; /* First argument to the callback function */
290 char *zErrMsg; /* An error message */
291 Token sErrToken; /* The token at which the error occurred */
292 Token sFirstToken; /* The first token parsed */
293 Token sLastToken; /* The last token parsed */
294 Table *pNewTable; /* A table being constructed by CREATE TABLE */
295 Vdbe *pVdbe; /* An engine for executing database bytecode */
drhd8bc7082000-06-07 23:51:50 +0000296 int colNamesSet; /* TRUE after OP_ColumnCount has been issued to pVdbe */
drh75897232000-05-29 14:26:00 +0000297 int explain; /* True if the EXPLAIN flag is found on the query */
298 int initFlag; /* True if reparsing CREATE TABLEs */
299 int nErr; /* Number of errors seen */
drh19a775c2000-06-05 18:54:46 +0000300 int nTab; /* Number of previously allocated cursors */
301 int nMem; /* Number of memory cells used so far */
drhfef52082000-06-06 01:50:43 +0000302 int nSet; /* Number of sets used so far */
drh22827922000-06-06 17:27:05 +0000303 int nAgg; /* Number of aggregate expressions */
304 AggExpr *aAgg; /* An array of aggregate expressions */
305 int iAggCount; /* Index of the count(*) aggregate in aAgg[] */
306 int useAgg; /* If true, extract field values from the aggregator
307 ** while generating expressions. Normally false */
drh75897232000-05-29 14:26:00 +0000308};
309
310/*
311** Internal function prototypes
312*/
313int sqliteStrICmp(const char *, const char *);
314int sqliteStrNICmp(const char *, const char *, int);
315int sqliteHashNoCase(const char *, int);
316int sqliteCompare(const char *, const char *);
317int sqliteSortCompare(const char *, const char *);
drhdcc581c2000-05-30 13:44:19 +0000318#ifdef MEMORY_DEBUG
319 void *sqliteMalloc_(int,char*,int);
320 void sqliteFree_(void*,char*,int);
321 void *sqliteRealloc_(void*,int,char*,int);
drh6e142f52000-06-08 13:36:40 +0000322 char *sqliteStrDup_(const char*,char*,int);
323 char *sqliteStrNDup_(const char*, int,char*,int);
drhdcc581c2000-05-30 13:44:19 +0000324#else
325 void *sqliteMalloc(int);
326 void sqliteFree(void*);
327 void *sqliteRealloc(void*,int);
drh6e142f52000-06-08 13:36:40 +0000328 char *sqliteStrDup(const char*);
329 char *sqliteStrNDup(const char*, int);
drhdcc581c2000-05-30 13:44:19 +0000330#endif
drh75897232000-05-29 14:26:00 +0000331int sqliteGetToken(const char*, int *);
332void sqliteSetString(char **, const char *, ...);
333void sqliteSetNString(char **, ...);
drh982cef72000-05-30 16:27:03 +0000334void sqliteDequote(char*);
drh75897232000-05-29 14:26:00 +0000335int sqliteRunParser(Parse*, char*, char **);
336void sqliteExec(Parse*);
337Expr *sqliteExpr(int, Expr*, Expr*, Token*);
338Expr *sqliteExprFunction(ExprList*, Token*);
339void sqliteExprDelete(Expr*);
340ExprList *sqliteExprListAppend(ExprList*,Expr*,Token*);
341void sqliteExprListDelete(ExprList*);
342void sqliteStartTable(Parse*,Token*,Token*);
343void sqliteAddColumn(Parse*,Token*);
drh7020f652000-06-03 18:06:52 +0000344void sqliteAddDefaultValue(Parse*,Token*,int);
drh75897232000-05-29 14:26:00 +0000345void sqliteEndTable(Parse*,Token*);
346void sqliteDropTable(Parse*, Token*);
347void sqliteDeleteTable(sqlite*, Table*);
drh5974a302000-06-07 14:42:26 +0000348void sqliteInsert(Parse*, Token*, ExprList*, Select*, IdList*);
drh75897232000-05-29 14:26:00 +0000349IdList *sqliteIdListAppend(IdList*, Token*);
350void sqliteIdListAddAlias(IdList*, Token*);
351void sqliteIdListDelete(IdList*);
352void sqliteCreateIndex(Parse*, Token*, Token*, IdList*, Token*, Token*);
353void sqliteDropIndex(Parse*, Token*);
drh19a775c2000-06-05 18:54:46 +0000354int sqliteSelect(Parse*, Select*, int, int);
drh9bb61fe2000-06-05 16:01:39 +0000355Select *sqliteSelectNew(ExprList*,IdList*,Expr*,ExprList*,Expr*,ExprList*,int);
356void sqliteSelectDelete(Select*);
drh75897232000-05-29 14:26:00 +0000357void sqliteDeleteFrom(Parse*, Token*, Expr*);
358void sqliteUpdate(Parse*, Token*, ExprList*, Expr*);
359WhereInfo *sqliteWhereBegin(Parse*, IdList*, Expr*, int);
360void sqliteWhereEnd(WhereInfo*);
361void sqliteExprCode(Parse*, Expr*);
362void sqliteExprIfTrue(Parse*, Expr*, int);
363void sqliteExprIfFalse(Parse*, Expr*, int);
364Table *sqliteFindTable(sqlite*,char*);
drh982cef72000-05-30 16:27:03 +0000365void sqliteCopy(Parse*, Token*, Token*, Token*);
drhdce2cbe2000-05-31 02:27:49 +0000366void sqliteVacuum(Parse*, Token*);
367int sqliteGlobCompare(const char*,const char*);
368int sqliteLikeCompare(const unsigned char*,const unsigned char*);
drhcce7d172000-05-31 15:34:51 +0000369char *sqliteTableNameFromToken(Token*);
370int sqliteExprCheck(Parse*, Expr*, int, int*);
drhd8bc7082000-06-07 23:51:50 +0000371int sqliteExprCompare(Expr*, Expr*);
drhcce7d172000-05-31 15:34:51 +0000372int sqliteFuncId(Token*);
drhbed86902000-06-02 13:27:59 +0000373int sqliteExprResolveIds(Parse*, IdList*, Expr*);
drh4794b982000-06-06 13:54:14 +0000374void sqliteExprResolveInSelect(Parse*, Expr*);
drh22827922000-06-06 17:27:05 +0000375int sqliteExprAnalyzeAggregates(Parse*, Expr*);
drhd8bc7082000-06-07 23:51:50 +0000376void sqliteParseInfoReset(Parse*);
377Vdbe *sqliteGetVdbe(Parse*);