blob: 0dd8ef82518a9cc0f1bf2df2d734de7010d1b399 [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**
drhefb72512000-05-31 20:00:52 +000026** @(#) $Id: sqliteInt.h,v 1.6 2000/05/31 20:00:52 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
drhdcc581c2000-05-30 13:44:19 +000038/* #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
drh75897232000-05-29 14:26:00 +000045/*
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/*
drhcce7d172000-05-31 15:34:51 +000065** 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/*
drh75897232000-05-29 14:26:00 +000075** Forward references to structures
76*/
77typedef struct Table Table;
78typedef struct Index Index;
79typedef struct Instruction Instruction;
80typedef struct Expr Expr;
81typedef struct ExprList ExprList;
82typedef struct Parse Parse;
83typedef struct Token Token;
84typedef struct IdList IdList;
85typedef struct WhereInfo WhereInfo;
86
87/*
88** Each database is an instance of the following structure
89*/
90struct 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*/
106struct 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*/
119struct 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*/
132struct 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*/
141struct 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*/
158struct 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; /* ... */
drhcce7d172000-05-31 15:34:51 +0000164 int isAgg; /* True if this is an aggregate like count(*) */
drh75897232000-05-29 14:26:00 +0000165 } *a; /* One entry for each expression */
166};
167
168/*
169** A list of identifiers.
170*/
171struct 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*/
188struct WhereInfo {
189 Parse *pParse;
190 IdList *pTabList;
191 int iContinue;
192 int iBreak;
193};
194
195/*
196** An SQL parser context
197*/
198struct 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*/
216int sqliteStrICmp(const char *, const char *);
217int sqliteStrNICmp(const char *, const char *, int);
218int sqliteHashNoCase(const char *, int);
219int sqliteCompare(const char *, const char *);
220int sqliteSortCompare(const char *, const char *);
drhdcc581c2000-05-30 13:44:19 +0000221#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
drh75897232000-05-29 14:26:00 +0000230int sqliteGetToken(const char*, int *);
231void sqliteSetString(char **, const char *, ...);
232void sqliteSetNString(char **, ...);
drh982cef72000-05-30 16:27:03 +0000233void sqliteDequote(char*);
drh75897232000-05-29 14:26:00 +0000234int sqliteRunParser(Parse*, char*, char **);
235void sqliteExec(Parse*);
236Expr *sqliteExpr(int, Expr*, Expr*, Token*);
237Expr *sqliteExprFunction(ExprList*, Token*);
238void sqliteExprDelete(Expr*);
239ExprList *sqliteExprListAppend(ExprList*,Expr*,Token*);
240void sqliteExprListDelete(ExprList*);
241void sqliteStartTable(Parse*,Token*,Token*);
242void sqliteAddColumn(Parse*,Token*);
243void sqliteEndTable(Parse*,Token*);
244void sqliteDropTable(Parse*, Token*);
245void sqliteDeleteTable(sqlite*, Table*);
246void sqliteInsert(Parse*, Token*, ExprList*, IdList*);
247IdList *sqliteIdListAppend(IdList*, Token*);
248void sqliteIdListAddAlias(IdList*, Token*);
249void sqliteIdListDelete(IdList*);
250void sqliteCreateIndex(Parse*, Token*, Token*, IdList*, Token*, Token*);
251void sqliteDropIndex(Parse*, Token*);
drhefb72512000-05-31 20:00:52 +0000252void sqliteSelect(Parse*, ExprList*, IdList*, Expr*, ExprList*, int);
drh75897232000-05-29 14:26:00 +0000253void sqliteDeleteFrom(Parse*, Token*, Expr*);
254void sqliteUpdate(Parse*, Token*, ExprList*, Expr*);
255WhereInfo *sqliteWhereBegin(Parse*, IdList*, Expr*, int);
256void sqliteWhereEnd(WhereInfo*);
257void sqliteExprCode(Parse*, Expr*);
258void sqliteExprIfTrue(Parse*, Expr*, int);
259void sqliteExprIfFalse(Parse*, Expr*, int);
260Table *sqliteFindTable(sqlite*,char*);
drh982cef72000-05-30 16:27:03 +0000261void sqliteCopy(Parse*, Token*, Token*, Token*);
drhdce2cbe2000-05-31 02:27:49 +0000262void sqliteVacuum(Parse*, Token*);
263int sqliteGlobCompare(const char*,const char*);
264int sqliteLikeCompare(const unsigned char*,const unsigned char*);
drhcce7d172000-05-31 15:34:51 +0000265char *sqliteTableNameFromToken(Token*);
266int sqliteExprCheck(Parse*, Expr*, int, int*);
267int sqliteFuncId(Token*);