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