blob: 45625a532e3d96fe5ebd32cf86434ebc73ab90d5 [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**
drhc61053b2000-06-04 12:58:36 +000026** @(#) $Id: sqliteInt.h,v 1.12 2000/06/04 12:58:38 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
drhc61053b2000-06-04 12:58:36 +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__)
drhc3c2fc92000-05-31 22:58:39 +000043 void sqliteStrRealloc(char**);
44#else
45# define sqliteStrRealloc(X)
drhdcc581c2000-05-30 13:44:19 +000046#endif
47
drh75897232000-05-29 14:26:00 +000048/*
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/*
drhcce7d172000-05-31 15:34:51 +000068** 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/*
drh75897232000-05-29 14:26:00 +000078** Forward references to structures
79*/
drh7020f652000-06-03 18:06:52 +000080typedef struct Column Column;
drh75897232000-05-29 14:26:00 +000081typedef struct Table Table;
82typedef struct Index Index;
83typedef struct Instruction Instruction;
84typedef struct Expr Expr;
85typedef struct ExprList ExprList;
86typedef struct Parse Parse;
87typedef struct Token Token;
88typedef struct IdList IdList;
89typedef struct WhereInfo WhereInfo;
90
91/*
92** Each database is an instance of the following structure
93*/
94struct sqlite {
95 Dbbe *pBe; /* The backend driver */
96 int flags; /* Miscellanous flags */
97 Table *apTblHash[N_HASH]; /* All tables of the database */
98 Index *apIdxHash[N_HASH]; /* All indices of the database */
99};
100
101/*
102** Possible values for the flags field of sqlite
103*/
104#define SQLITE_VdbeTrace 0x00000001
drh58b95762000-06-02 01:17:37 +0000105#define SQLITE_Initialized 0x00000002
106
107/*
drh7020f652000-06-03 18:06:52 +0000108** information about each column of a table is held in an instance
109** of this structure.
110*/
111struct Column {
112 char *zName; /* Name of this column */
113 char *zDflt; /* Default value of this column */
114 int notNull; /* True if there is a NOT NULL constraing */
115};
116
117/*
drh75897232000-05-29 14:26:00 +0000118** Each table is represented in memory by
119** an instance of the following structure
120*/
121struct Table {
122 char *zName; /* Name of the table */
123 Table *pHash; /* Next table with same hash on zName */
124 int nCol; /* Number of columns in this table */
drh7020f652000-06-03 18:06:52 +0000125 Column *aCol; /* Information about each column */
drh75897232000-05-29 14:26:00 +0000126 int readOnly; /* True if this table should not be written by the user */
drh75897232000-05-29 14:26:00 +0000127 Index *pIndex; /* List of indices on this table. */
128};
129
130/*
131** Each index is represented in memory by and
132** instance of the following structure.
133*/
134struct Index {
135 char *zName; /* Name of this index */
136 Index *pHash; /* Next index with the same hash on zName */
137 int nField; /* Number of fields in the table indexed by this index */
138 int *aiField; /* Indices of fields used by this index. 1st is 0 */
139 Table *pTable; /* The table being indexed */
drh7020f652000-06-03 18:06:52 +0000140 int isUnique; /* True if keys must all be unique */
drh75897232000-05-29 14:26:00 +0000141 Index *pNext; /* The next index associated with the same table */
142};
143
144/*
145** Each token coming out of the lexer is an instance of
146** this structure.
147*/
148struct Token {
149 char *z; /* Text of the token */
150 int n; /* Number of characters in this token */
151};
152
153/*
154** Each node of an expression in the parse tree is an instance
155** of this structure
156*/
157struct Expr {
158 int op; /* Operation performed by this node */
159 Expr *pLeft, *pRight; /* Left and right subnodes */
160 ExprList *pList; /* A list of expressions used as a function argument */
161 Token token; /* An operand token */
162 int iTable, iField; /* When op==TK_FIELD, then this node means the
163 ** iField-th field of the iTable-th table */
164};
165
166/*
167** A list of expressions. Each expression may optionally have a
168** name. An expr/name combination can be used in several ways, such
169** as the list of "expr AS ID" fields following a "SELECT" or in the
170** list of "ID = expr" items in an UPDATE. A list of expressions can
171** also be used as the argument to a function, in which case the azName
172** field is not used.
173*/
174struct ExprList {
175 int nExpr; /* Number of expressions on the list */
176 struct {
177 Expr *pExpr; /* The list of expressions */
178 char *zName; /* Token associated with this expression */
179 int idx; /* ... */
drhcce7d172000-05-31 15:34:51 +0000180 int isAgg; /* True if this is an aggregate like count(*) */
drh75897232000-05-29 14:26:00 +0000181 } *a; /* One entry for each expression */
182};
183
184/*
185** A list of identifiers.
186*/
187struct IdList {
188 int nId; /* Number of identifiers on the list */
189 struct {
190 char *zName; /* Text of the identifier. */
191 char *zAlias; /* The "B" part of a "A AS B" phrase. zName is the "A" */
192 Table *pTab; /* Table corresponding to zName */
193 int idx; /* Index of a field name in the table */
194 } *a; /* One entry for each identifier on the list */
195};
196
197/*
198** The WHERE clause processing routine has two halves. The
199** first part does the start of the WHERE loop and the second
200** half does the tail of the WHERE loop. An instance of
201** this structure is returned by the first half and passed
202** into the second half to give some continuity.
203*/
204struct WhereInfo {
205 Parse *pParse;
206 IdList *pTabList;
207 int iContinue;
208 int iBreak;
209};
210
211/*
212** An SQL parser context
213*/
214struct Parse {
215 sqlite *db; /* The main database structure */
216 sqlite_callback xCallback; /* The callback function */
217 void *pArg; /* First argument to the callback function */
218 char *zErrMsg; /* An error message */
219 Token sErrToken; /* The token at which the error occurred */
220 Token sFirstToken; /* The first token parsed */
221 Token sLastToken; /* The last token parsed */
222 Table *pNewTable; /* A table being constructed by CREATE TABLE */
223 Vdbe *pVdbe; /* An engine for executing database bytecode */
224 int explain; /* True if the EXPLAIN flag is found on the query */
225 int initFlag; /* True if reparsing CREATE TABLEs */
226 int nErr; /* Number of errors seen */
227};
228
229/*
230** Internal function prototypes
231*/
232int sqliteStrICmp(const char *, const char *);
233int sqliteStrNICmp(const char *, const char *, int);
234int sqliteHashNoCase(const char *, int);
235int sqliteCompare(const char *, const char *);
236int sqliteSortCompare(const char *, const char *);
drhdcc581c2000-05-30 13:44:19 +0000237#ifdef MEMORY_DEBUG
238 void *sqliteMalloc_(int,char*,int);
239 void sqliteFree_(void*,char*,int);
240 void *sqliteRealloc_(void*,int,char*,int);
241#else
242 void *sqliteMalloc(int);
243 void sqliteFree(void*);
244 void *sqliteRealloc(void*,int);
245#endif
drh75897232000-05-29 14:26:00 +0000246int sqliteGetToken(const char*, int *);
247void sqliteSetString(char **, const char *, ...);
248void sqliteSetNString(char **, ...);
drh982cef72000-05-30 16:27:03 +0000249void sqliteDequote(char*);
drh75897232000-05-29 14:26:00 +0000250int sqliteRunParser(Parse*, char*, char **);
251void sqliteExec(Parse*);
252Expr *sqliteExpr(int, Expr*, Expr*, Token*);
253Expr *sqliteExprFunction(ExprList*, Token*);
254void sqliteExprDelete(Expr*);
255ExprList *sqliteExprListAppend(ExprList*,Expr*,Token*);
256void sqliteExprListDelete(ExprList*);
257void sqliteStartTable(Parse*,Token*,Token*);
258void sqliteAddColumn(Parse*,Token*);
drh7020f652000-06-03 18:06:52 +0000259void sqliteAddDefaultValue(Parse*,Token*,int);
drh75897232000-05-29 14:26:00 +0000260void sqliteEndTable(Parse*,Token*);
261void sqliteDropTable(Parse*, Token*);
262void sqliteDeleteTable(sqlite*, Table*);
263void sqliteInsert(Parse*, Token*, ExprList*, IdList*);
264IdList *sqliteIdListAppend(IdList*, Token*);
265void sqliteIdListAddAlias(IdList*, Token*);
266void sqliteIdListDelete(IdList*);
267void sqliteCreateIndex(Parse*, Token*, Token*, IdList*, Token*, Token*);
268void sqliteDropIndex(Parse*, Token*);
drhefb72512000-05-31 20:00:52 +0000269void sqliteSelect(Parse*, ExprList*, IdList*, Expr*, ExprList*, int);
drh75897232000-05-29 14:26:00 +0000270void sqliteDeleteFrom(Parse*, Token*, Expr*);
271void sqliteUpdate(Parse*, Token*, ExprList*, Expr*);
272WhereInfo *sqliteWhereBegin(Parse*, IdList*, Expr*, int);
273void sqliteWhereEnd(WhereInfo*);
274void sqliteExprCode(Parse*, Expr*);
275void sqliteExprIfTrue(Parse*, Expr*, int);
276void sqliteExprIfFalse(Parse*, Expr*, int);
277Table *sqliteFindTable(sqlite*,char*);
drh982cef72000-05-30 16:27:03 +0000278void sqliteCopy(Parse*, Token*, Token*, Token*);
drhdce2cbe2000-05-31 02:27:49 +0000279void sqliteVacuum(Parse*, Token*);
280int sqliteGlobCompare(const char*,const char*);
281int sqliteLikeCompare(const unsigned char*,const unsigned char*);
drhcce7d172000-05-31 15:34:51 +0000282char *sqliteTableNameFromToken(Token*);
283int sqliteExprCheck(Parse*, Expr*, int, int*);
284int sqliteFuncId(Token*);
drhbed86902000-06-02 13:27:59 +0000285int sqliteExprResolveIds(Parse*, IdList*, Expr*);