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