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