blob: 87fba207f75b5a7e2020eefdc8528d384995399a [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**
drh66b89c82000-11-28 20:47:17 +000026** @(#) $Id: sqliteInt.h,v 1.32 2000/11/28 20:47:23 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
drh967e8b72000-06-21 13:59:10 +000038/*
39** If memory allocation problems are found, recompile with
40**
41** -DMEMORY_DEBUG=1
42**
43** to enable some sanity checking on malloc() and free(). To
44** check for memory leaks, recompile with
45**
46** -DMEMORY_DEBUG=2
47**
48** and a line of text will be written to standard error for
49** each malloc() and free(). This output can be analyzed
50** by an AWK script to determine if there are any leaks.
51*/
drhdcc581c2000-05-30 13:44:19 +000052#ifdef MEMORY_DEBUG
53# define sqliteMalloc(X) sqliteMalloc_(X,__FILE__,__LINE__)
54# define sqliteFree(X) sqliteFree_(X,__FILE__,__LINE__)
55# define sqliteRealloc(X,Y) sqliteRealloc_(X,Y,__FILE__,__LINE__)
drh6e142f52000-06-08 13:36:40 +000056# define sqliteStrDup(X) sqliteStrDup_(X,__FILE__,__LINE__)
57# define sqliteStrNDup(X,Y) sqliteStrNDup_(X,Y,__FILE__,__LINE__)
drhc3c2fc92000-05-31 22:58:39 +000058 void sqliteStrRealloc(char**);
59#else
60# define sqliteStrRealloc(X)
drhdcc581c2000-05-30 13:44:19 +000061#endif
62
drh75897232000-05-29 14:26:00 +000063/*
drh6e142f52000-06-08 13:36:40 +000064** The following global variables are used for testing and debugging
65** only. Thy only work if MEMORY_DEBUG is defined.
66*/
67#ifdef MEMORY_DEBUG
68int sqlite_nMalloc; /* Number of sqliteMalloc() calls */
69int sqlite_nFree; /* Number of sqliteFree() calls */
70int sqlite_iMallocFail; /* Fail sqliteMalloc() after this many calls */
71#endif
72
73/*
drh967e8b72000-06-21 13:59:10 +000074** The number of entries in the in-memory hash array holding the
75** database schema.
drh75897232000-05-29 14:26:00 +000076*/
77#define N_HASH 51
78
79/*
80** Name of the master database table. The master database table
81** is a special table that holds the names and attributes of all
82** user tables and indices.
83*/
84#define MASTER_NAME "sqlite_master"
85
86/*
87** A convenience macro that returns the number of elements in
88** an array.
89*/
90#define ArraySize(X) (sizeof(X)/sizeof(X[0]))
91
92/*
drh967e8b72000-06-21 13:59:10 +000093** Integer identifiers for built-in SQL functions.
drhcce7d172000-05-31 15:34:51 +000094*/
95#define FN_Unknown 0
96#define FN_Count 1
97#define FN_Min 2
98#define FN_Max 3
99#define FN_Sum 4
100#define FN_Avg 5
drh0bdaf622000-06-11 23:50:13 +0000101#define FN_Fcnt 6
drh6ec27332000-08-28 15:51:43 +0000102#define FN_Length 7
103#define FN_Substr 8
drhcce7d172000-05-31 15:34:51 +0000104
105/*
drh75897232000-05-29 14:26:00 +0000106** Forward references to structures
107*/
drh7020f652000-06-03 18:06:52 +0000108typedef struct Column Column;
drh75897232000-05-29 14:26:00 +0000109typedef struct Table Table;
110typedef struct Index Index;
111typedef struct Instruction Instruction;
112typedef struct Expr Expr;
113typedef struct ExprList ExprList;
114typedef struct Parse Parse;
115typedef struct Token Token;
116typedef struct IdList IdList;
117typedef struct WhereInfo WhereInfo;
drh9bb61fe2000-06-05 16:01:39 +0000118typedef struct Select Select;
drh22827922000-06-06 17:27:05 +0000119typedef struct AggExpr AggExpr;
drh75897232000-05-29 14:26:00 +0000120
121/*
122** Each database is an instance of the following structure
123*/
124struct sqlite {
125 Dbbe *pBe; /* The backend driver */
126 int flags; /* Miscellanous flags */
drh28037572000-08-02 13:47:41 +0000127 int file_format; /* What file format version is this database? */
128 int nTable; /* Number of tables in the database */
drh2dfbbca2000-07-28 14:32:48 +0000129 void *pBusyArg; /* 1st Argument to the busy callback */
drh353f57e2000-08-02 12:26:28 +0000130 int (*xBusyCallback)(void *,const char*,int); /* The busy callback */
drh75897232000-05-29 14:26:00 +0000131 Table *apTblHash[N_HASH]; /* All tables of the database */
132 Index *apIdxHash[N_HASH]; /* All indices of the database */
133};
134
135/*
drh967e8b72000-06-21 13:59:10 +0000136** Possible values for the sqlite.flags.
drh75897232000-05-29 14:26:00 +0000137*/
drh4c504392000-10-16 22:06:40 +0000138#define SQLITE_VdbeTrace 0x00000001 /* True to trace VDBE execution */
139#define SQLITE_Initialized 0x00000002 /* True after initialization */
140#define SQLITE_Interrupt 0x00000004 /* Cancel current operation */
drh58b95762000-06-02 01:17:37 +0000141
142/*
drh28037572000-08-02 13:47:41 +0000143** Current file format version
144*/
145#define SQLITE_FileFormat 2
146
147/*
drh967e8b72000-06-21 13:59:10 +0000148** information about each column of an SQL table is held in an instance
drh7020f652000-06-03 18:06:52 +0000149** of this structure.
150*/
151struct Column {
drh967e8b72000-06-21 13:59:10 +0000152 char *zName; /* Name of this column */
153 char *zDflt; /* Default value of this column */
154 int notNull; /* True if there is a NOT NULL constraint */
drh7020f652000-06-03 18:06:52 +0000155};
156
157/*
drh967e8b72000-06-21 13:59:10 +0000158** Each SQL table is represented in memory by
159** an instance of the following structure.
drh75897232000-05-29 14:26:00 +0000160*/
161struct Table {
drh967e8b72000-06-21 13:59:10 +0000162 char *zName; /* Name of the table */
163 Table *pHash; /* Next table with same hash on zName */
164 int nCol; /* Number of columns in this table */
165 Column *aCol; /* Information about each column */
166 int readOnly; /* True if this table should not be written by the user */
167 Index *pIndex; /* List of SQL indexes on this table. */
drh75897232000-05-29 14:26:00 +0000168};
169
170/*
drh66b89c82000-11-28 20:47:17 +0000171** Each SQL index is represented in memory by an
drh75897232000-05-29 14:26:00 +0000172** instance of the following structure.
drh967e8b72000-06-21 13:59:10 +0000173**
174** The columns of the table that are to be indexed are described
175** by the aiColumn[] field of this structure. For example, suppose
176** we have the following table and index:
177**
178** CREATE TABLE Ex1(c1 int, c2 int, c3 text);
179** CREATE INDEX Ex2 ON Ex1(c3,c1);
180**
181** In the Table structure describing Ex1, nCol==3 because there are
182** three columns in the table. In the Index structure describing
183** Ex2, nColumn==2 since 2 of the 3 columns of Ex1 are indexed.
184** The value of aiColumn is {2, 0}. aiColumn[0]==2 because the
185** first column to be indexed (c3) has an index of 2 in Ex1.aCol[].
186** The second column to be indexed (c1) has an index of 0 in
187** Ex1.aCol[], hence Ex2.aiColumn[1]==0.
drh75897232000-05-29 14:26:00 +0000188*/
189struct Index {
drh967e8b72000-06-21 13:59:10 +0000190 char *zName; /* Name of this index */
191 Index *pHash; /* Next index with the same hash on zName */
192 int nColumn; /* Number of columns in the table used by this index */
193 int *aiColumn; /* Which columns are used by this index. 1st is 0 */
194 Table *pTable; /* The SQL table being indexed */
195 int isUnique; /* True if keys must all be unique */
196 Index *pNext; /* The next index associated with the same table */
drh75897232000-05-29 14:26:00 +0000197};
198
199/*
200** Each token coming out of the lexer is an instance of
201** this structure.
202*/
203struct Token {
drh66b89c82000-11-28 20:47:17 +0000204 char *z; /* Text of the token. Not NULL-terminated! */
drh75897232000-05-29 14:26:00 +0000205 int n; /* Number of characters in this token */
206};
207
208/*
209** Each node of an expression in the parse tree is an instance
210** of this structure
211*/
212struct Expr {
213 int op; /* Operation performed by this node */
214 Expr *pLeft, *pRight; /* Left and right subnodes */
215 ExprList *pList; /* A list of expressions used as a function argument */
216 Token token; /* An operand token */
drhe1b6a5b2000-07-29 13:06:59 +0000217 Token span; /* Complete text of the expression */
drh967e8b72000-06-21 13:59:10 +0000218 int iTable, iColumn; /* When op==TK_COLUMN, then this expr node means the
219 ** iColumn-th field of the iTable-th table. When
220 ** op==TK_FUNCTION, iColumn holds the function id */
221 int iAgg; /* When op==TK_COLUMN and pParse->useAgg==TRUE, pull
222 ** result from the iAgg-th element of the aggregator */
drh19a775c2000-06-05 18:54:46 +0000223 Select *pSelect; /* When the expression is a sub-select */
drh75897232000-05-29 14:26:00 +0000224};
225
226/*
227** A list of expressions. Each expression may optionally have a
228** name. An expr/name combination can be used in several ways, such
229** as the list of "expr AS ID" fields following a "SELECT" or in the
230** list of "ID = expr" items in an UPDATE. A list of expressions can
231** also be used as the argument to a function, in which case the azName
232** field is not used.
233*/
234struct ExprList {
235 int nExpr; /* Number of expressions on the list */
236 struct {
237 Expr *pExpr; /* The list of expressions */
238 char *zName; /* Token associated with this expression */
drhd8bc7082000-06-07 23:51:50 +0000239 char sortOrder; /* 1 for DESC or 0 for ASC */
240 char isAgg; /* True if this is an aggregate like count(*) */
241 char done; /* A flag to indicate when processing is finished */
drh75897232000-05-29 14:26:00 +0000242 } *a; /* One entry for each expression */
243};
244
245/*
246** A list of identifiers.
247*/
248struct IdList {
249 int nId; /* Number of identifiers on the list */
250 struct {
251 char *zName; /* Text of the identifier. */
252 char *zAlias; /* The "B" part of a "A AS B" phrase. zName is the "A" */
drh967e8b72000-06-21 13:59:10 +0000253 Table *pTab; /* An SQL table corresponding to zName */
254 int idx; /* Index in some Table.aCol[] of a column named zName */
drh75897232000-05-29 14:26:00 +0000255 } *a; /* One entry for each identifier on the list */
256};
257
258/*
259** The WHERE clause processing routine has two halves. The
260** first part does the start of the WHERE loop and the second
261** half does the tail of the WHERE loop. An instance of
262** this structure is returned by the first half and passed
263** into the second half to give some continuity.
264*/
265struct WhereInfo {
266 Parse *pParse;
drh19a775c2000-06-05 18:54:46 +0000267 IdList *pTabList; /* List of tables in the join */
268 int iContinue; /* Jump here to continue with next record */
269 int iBreak; /* Jump here to break out of the loop */
270 int base; /* Index of first Open opcode */
271 Index *aIdx[32]; /* Indices used for each table */
drh75897232000-05-29 14:26:00 +0000272};
273
274/*
drh9bb61fe2000-06-05 16:01:39 +0000275** An instance of the following structure contains all information
276** needed to generate code for a single SELECT statement.
277*/
278struct Select {
279 int isDistinct; /* True if the DISTINCT keyword is present */
280 ExprList *pEList; /* The fields of the result */
281 IdList *pSrc; /* The FROM clause */
282 Expr *pWhere; /* The WHERE clause */
283 ExprList *pGroupBy; /* The GROUP BY clause */
284 Expr *pHaving; /* The HAVING clause */
285 ExprList *pOrderBy; /* The ORDER BY clause */
drh82c3d632000-06-06 21:56:07 +0000286 int op; /* One of: TK_UNION TK_ALL TK_INTERSECT TK_EXCEPT */
drh967e8b72000-06-21 13:59:10 +0000287 Select *pPrior; /* Prior select in a compound select statement */
drh9bb61fe2000-06-05 16:01:39 +0000288};
289
290/*
drhfef52082000-06-06 01:50:43 +0000291** The results of a select can be distributed in several ways.
292*/
293#define SRT_Callback 1 /* Invoke a callback with each row of result */
294#define SRT_Mem 2 /* Store result in a memory cell */
drh82c3d632000-06-06 21:56:07 +0000295#define SRT_Set 3 /* Store result as unique keys in a table */
296#define SRT_Union 5 /* Store result as keys in a table */
297#define SRT_Except 6 /* Remove result from a UNION table */
drh5974a302000-06-07 14:42:26 +0000298#define SRT_Table 7 /* Store result as data with a unique key */
drhfef52082000-06-06 01:50:43 +0000299
300/*
drh22827922000-06-06 17:27:05 +0000301** When a SELECT uses aggregate functions (like "count(*)" or "avg(f1)")
302** we have to do some additional analysis of expressions. An instance
303** of the following structure holds information about a single subexpression
304** somewhere in the SELECT statement. An array of these structures holds
305** all the information we need to generate code for aggregate
306** expressions.
307**
308** Note that when analyzing a SELECT containing aggregates, both
309** non-aggregate field variables and aggregate functions are stored
310** in the AggExpr array of the Parser structure.
311**
312** The pExpr field points to an expression that is part of either the
313** field list, the GROUP BY clause, the HAVING clause or the ORDER BY
314** clause. The expression will be freed when those clauses are cleaned
315** up. Do not try to delete the expression attached to AggExpr.pExpr.
316**
317** If AggExpr.pExpr==0, that means the expression is "count(*)".
318*/
319struct AggExpr {
320 int isAgg; /* if TRUE contains an aggregate function */
321 Expr *pExpr; /* The expression */
322};
323
324/*
drh75897232000-05-29 14:26:00 +0000325** An SQL parser context
326*/
327struct Parse {
328 sqlite *db; /* The main database structure */
drh4c504392000-10-16 22:06:40 +0000329 int rc; /* Return code from execution */
drh75897232000-05-29 14:26:00 +0000330 sqlite_callback xCallback; /* The callback function */
331 void *pArg; /* First argument to the callback function */
332 char *zErrMsg; /* An error message */
333 Token sErrToken; /* The token at which the error occurred */
334 Token sFirstToken; /* The first token parsed */
335 Token sLastToken; /* The last token parsed */
336 Table *pNewTable; /* A table being constructed by CREATE TABLE */
337 Vdbe *pVdbe; /* An engine for executing database bytecode */
drhd8bc7082000-06-07 23:51:50 +0000338 int colNamesSet; /* TRUE after OP_ColumnCount has been issued to pVdbe */
drh75897232000-05-29 14:26:00 +0000339 int explain; /* True if the EXPLAIN flag is found on the query */
340 int initFlag; /* True if reparsing CREATE TABLEs */
341 int nErr; /* Number of errors seen */
drh19a775c2000-06-05 18:54:46 +0000342 int nTab; /* Number of previously allocated cursors */
343 int nMem; /* Number of memory cells used so far */
drhfef52082000-06-06 01:50:43 +0000344 int nSet; /* Number of sets used so far */
drh22827922000-06-06 17:27:05 +0000345 int nAgg; /* Number of aggregate expressions */
346 AggExpr *aAgg; /* An array of aggregate expressions */
347 int iAggCount; /* Index of the count(*) aggregate in aAgg[] */
348 int useAgg; /* If true, extract field values from the aggregator
349 ** while generating expressions. Normally false */
drh75897232000-05-29 14:26:00 +0000350};
351
352/*
353** Internal function prototypes
354*/
355int sqliteStrICmp(const char *, const char *);
356int sqliteStrNICmp(const char *, const char *, int);
357int sqliteHashNoCase(const char *, int);
358int sqliteCompare(const char *, const char *);
359int sqliteSortCompare(const char *, const char *);
drhdcc581c2000-05-30 13:44:19 +0000360#ifdef MEMORY_DEBUG
361 void *sqliteMalloc_(int,char*,int);
362 void sqliteFree_(void*,char*,int);
363 void *sqliteRealloc_(void*,int,char*,int);
drh6e142f52000-06-08 13:36:40 +0000364 char *sqliteStrDup_(const char*,char*,int);
365 char *sqliteStrNDup_(const char*, int,char*,int);
drhdcc581c2000-05-30 13:44:19 +0000366#else
367 void *sqliteMalloc(int);
368 void sqliteFree(void*);
369 void *sqliteRealloc(void*,int);
drh6e142f52000-06-08 13:36:40 +0000370 char *sqliteStrDup(const char*);
371 char *sqliteStrNDup(const char*, int);
drhdcc581c2000-05-30 13:44:19 +0000372#endif
drh75897232000-05-29 14:26:00 +0000373int sqliteGetToken(const char*, int *);
374void sqliteSetString(char **, const char *, ...);
375void sqliteSetNString(char **, ...);
drh982cef72000-05-30 16:27:03 +0000376void sqliteDequote(char*);
drh75897232000-05-29 14:26:00 +0000377int sqliteRunParser(Parse*, char*, char **);
378void sqliteExec(Parse*);
379Expr *sqliteExpr(int, Expr*, Expr*, Token*);
drhe1b6a5b2000-07-29 13:06:59 +0000380void sqliteExprSpan(Expr*,Token*,Token*);
drh75897232000-05-29 14:26:00 +0000381Expr *sqliteExprFunction(ExprList*, Token*);
382void sqliteExprDelete(Expr*);
383ExprList *sqliteExprListAppend(ExprList*,Expr*,Token*);
384void sqliteExprListDelete(ExprList*);
385void sqliteStartTable(Parse*,Token*,Token*);
386void sqliteAddColumn(Parse*,Token*);
drh7020f652000-06-03 18:06:52 +0000387void sqliteAddDefaultValue(Parse*,Token*,int);
drh75897232000-05-29 14:26:00 +0000388void sqliteEndTable(Parse*,Token*);
389void sqliteDropTable(Parse*, Token*);
390void sqliteDeleteTable(sqlite*, Table*);
drh5974a302000-06-07 14:42:26 +0000391void sqliteInsert(Parse*, Token*, ExprList*, Select*, IdList*);
drh75897232000-05-29 14:26:00 +0000392IdList *sqliteIdListAppend(IdList*, Token*);
393void sqliteIdListAddAlias(IdList*, Token*);
394void sqliteIdListDelete(IdList*);
395void sqliteCreateIndex(Parse*, Token*, Token*, IdList*, Token*, Token*);
396void sqliteDropIndex(Parse*, Token*);
drh19a775c2000-06-05 18:54:46 +0000397int sqliteSelect(Parse*, Select*, int, int);
drh9bb61fe2000-06-05 16:01:39 +0000398Select *sqliteSelectNew(ExprList*,IdList*,Expr*,ExprList*,Expr*,ExprList*,int);
399void sqliteSelectDelete(Select*);
drh75897232000-05-29 14:26:00 +0000400void sqliteDeleteFrom(Parse*, Token*, Expr*);
401void sqliteUpdate(Parse*, Token*, ExprList*, Expr*);
402WhereInfo *sqliteWhereBegin(Parse*, IdList*, Expr*, int);
403void sqliteWhereEnd(WhereInfo*);
404void sqliteExprCode(Parse*, Expr*);
405void sqliteExprIfTrue(Parse*, Expr*, int);
406void sqliteExprIfFalse(Parse*, Expr*, int);
407Table *sqliteFindTable(sqlite*,char*);
drh982cef72000-05-30 16:27:03 +0000408void sqliteCopy(Parse*, Token*, Token*, Token*);
drhdce2cbe2000-05-31 02:27:49 +0000409void sqliteVacuum(Parse*, Token*);
410int sqliteGlobCompare(const char*,const char*);
411int sqliteLikeCompare(const unsigned char*,const unsigned char*);
drhcce7d172000-05-31 15:34:51 +0000412char *sqliteTableNameFromToken(Token*);
413int sqliteExprCheck(Parse*, Expr*, int, int*);
drhd8bc7082000-06-07 23:51:50 +0000414int sqliteExprCompare(Expr*, Expr*);
drhcce7d172000-05-31 15:34:51 +0000415int sqliteFuncId(Token*);
drhbed86902000-06-02 13:27:59 +0000416int sqliteExprResolveIds(Parse*, IdList*, Expr*);
drh4794b982000-06-06 13:54:14 +0000417void sqliteExprResolveInSelect(Parse*, Expr*);
drh22827922000-06-06 17:27:05 +0000418int sqliteExprAnalyzeAggregates(Parse*, Expr*);
drhd8bc7082000-06-07 23:51:50 +0000419void sqliteParseInfoReset(Parse*);
420Vdbe *sqliteGetVdbe(Parse*);