blob: b86cf34675e57f3ff9dbbcabc86a2e6e16021775 [file] [log] [blame]
drh75897232000-05-29 14:26:00 +00001/*
drhb19a2bc2001-09-16 00:13:26 +00002** 2001 September 15
drh75897232000-05-29 14:26:00 +00003**
drhb19a2bc2001-09-16 00:13:26 +00004** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
drh75897232000-05-29 14:26:00 +00006**
drhb19a2bc2001-09-16 00:13:26 +00007** May you do good and not evil.
8** May you find forgiveness for yourself and forgive others.
9** May you share freely, never taking more than you give.
drh75897232000-05-29 14:26:00 +000010**
11*************************************************************************
12** Internal interface definitions for SQLite.
13**
drhdc04c582002-02-24 01:55:15 +000014** @(#) $Id: sqliteInt.h,v 1.92 2002/02/24 01:55:17 drh Exp $
drh75897232000-05-29 14:26:00 +000015*/
16#include "sqlite.h"
drhbeae3192001-09-22 18:12:08 +000017#include "hash.h"
drh75897232000-05-29 14:26:00 +000018#include "vdbe.h"
19#include "parse.h"
drhbe0072d2001-09-13 14:46:09 +000020#include "btree.h"
drh75897232000-05-29 14:26:00 +000021#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24#include <assert.h>
25
drh967e8b72000-06-21 13:59:10 +000026/*
drha1b351a2001-09-14 16:42:12 +000027** The maximum number of in-memory pages to use for the main database
28** table and for temporary tables.
29*/
drhf57b14a2001-09-14 18:54:08 +000030#define MAX_PAGES 100
31#define TEMP_PAGES 25
drha1b351a2001-09-14 16:42:12 +000032
33/*
drh5a2c2c22001-11-21 02:21:11 +000034** Integers of known sizes. These typedefs might change for architectures
35** where the sizes very. Preprocessor macros are available so that the
36** types can be conveniently redefined at compile-type. Like this:
37**
38** cc '-DUINTPTR_TYPE=long long int' ...
drh41a2b482001-01-20 19:52:49 +000039*/
drh5a2c2c22001-11-21 02:21:11 +000040#ifndef UINT32_TYPE
41# define UINT32_TYPE unsigned int
42#endif
43#ifndef UINT16_TYPE
44# define UINT16_TYPE unsigned short int
45#endif
46#ifndef UINT8_TYPE
47# define UINT8_TYPE unsigned char
48#endif
49#ifndef INTPTR_TYPE
50# define INTPTR_TYPE int
51#endif
52typedef UINT32_TYPE u32; /* 4-byte unsigned integer */
53typedef UINT16_TYPE u16; /* 2-byte unsigned integer */
54typedef UINT8_TYPE u8; /* 1-byte unsigned integer */
55typedef INTPTR_TYPE ptr; /* Big enough to hold a pointer */
56typedef unsigned INTPTR_TYPE uptr; /* Big enough to hold a pointer */
57
58/*
59** This macro casts a pointer to an integer. Useful for doing
60** pointer arithmetic.
61*/
62#define Addr(X) ((uptr)X)
drh41a2b482001-01-20 19:52:49 +000063
64/*
drh872ff862001-09-15 14:43:39 +000065** The maximum number of bytes of data that can be put into a single
drh80ff32f2001-11-04 18:32:46 +000066** row of a single table. The upper bound on this limit is 16777215
67** bytes (or 16MB-1). We have arbitrarily set the limit to just 1MB
68** here because the overflow page chain is inefficient for really big
69** records and we want to discourage people from thinking that
70** multi-megabyte records are OK. If your needs are different, you can
71** change this define and recompile to increase or decrease the record
72** size.
drh872ff862001-09-15 14:43:39 +000073*/
drh80ff32f2001-11-04 18:32:46 +000074#define MAX_BYTES_PER_ROW 1048576
drh872ff862001-09-15 14:43:39 +000075
76/*
drh967e8b72000-06-21 13:59:10 +000077** If memory allocation problems are found, recompile with
78**
79** -DMEMORY_DEBUG=1
80**
81** to enable some sanity checking on malloc() and free(). To
82** check for memory leaks, recompile with
83**
84** -DMEMORY_DEBUG=2
85**
86** and a line of text will be written to standard error for
87** each malloc() and free(). This output can be analyzed
88** by an AWK script to determine if there are any leaks.
89*/
drhdcc581c2000-05-30 13:44:19 +000090#ifdef MEMORY_DEBUG
91# define sqliteMalloc(X) sqliteMalloc_(X,__FILE__,__LINE__)
92# define sqliteFree(X) sqliteFree_(X,__FILE__,__LINE__)
93# define sqliteRealloc(X,Y) sqliteRealloc_(X,Y,__FILE__,__LINE__)
drh6e142f52000-06-08 13:36:40 +000094# define sqliteStrDup(X) sqliteStrDup_(X,__FILE__,__LINE__)
95# define sqliteStrNDup(X,Y) sqliteStrNDup_(X,Y,__FILE__,__LINE__)
drhc3c2fc92000-05-31 22:58:39 +000096 void sqliteStrRealloc(char**);
97#else
98# define sqliteStrRealloc(X)
drhdcc581c2000-05-30 13:44:19 +000099#endif
100
drh75897232000-05-29 14:26:00 +0000101/*
drhdaffd0e2001-04-11 14:28:42 +0000102** This variable gets set if malloc() ever fails. After it gets set,
103** the SQLite library shuts down permanently.
104*/
105extern int sqlite_malloc_failed;
106
107/*
drh6e142f52000-06-08 13:36:40 +0000108** The following global variables are used for testing and debugging
drh8c82b352000-12-10 18:23:50 +0000109** only. They only work if MEMORY_DEBUG is defined.
drh6e142f52000-06-08 13:36:40 +0000110*/
111#ifdef MEMORY_DEBUG
drh8c82b352000-12-10 18:23:50 +0000112extern int sqlite_nMalloc; /* Number of sqliteMalloc() calls */
113extern int sqlite_nFree; /* Number of sqliteFree() calls */
114extern int sqlite_iMallocFail; /* Fail sqliteMalloc() after this many calls */
drh6e142f52000-06-08 13:36:40 +0000115#endif
116
117/*
drh75897232000-05-29 14:26:00 +0000118** Name of the master database table. The master database table
119** is a special table that holds the names and attributes of all
120** user tables and indices.
121*/
122#define MASTER_NAME "sqlite_master"
123
124/*
125** A convenience macro that returns the number of elements in
126** an array.
127*/
128#define ArraySize(X) (sizeof(X)/sizeof(X[0]))
129
130/*
drh967e8b72000-06-21 13:59:10 +0000131** Integer identifiers for built-in SQL functions.
drhcce7d172000-05-31 15:34:51 +0000132*/
drh8e0a2f92002-02-23 23:45:45 +0000133#define FN_Unknown 0 /* Not a built-in. Might be user defined */
drhcce7d172000-05-31 15:34:51 +0000134#define FN_Count 1
135#define FN_Min 2
136#define FN_Max 3
137#define FN_Sum 4
138#define FN_Avg 5
drh0bdaf622000-06-11 23:50:13 +0000139#define FN_Fcnt 6
drh6ec27332000-08-28 15:51:43 +0000140#define FN_Length 7
141#define FN_Substr 8
drh81a20f22001-10-12 17:30:04 +0000142#define FN_Abs 9
drhbf4133c2001-10-13 02:59:08 +0000143#define FN_Round 10
drhcce7d172000-05-31 15:34:51 +0000144
145/*
drh75897232000-05-29 14:26:00 +0000146** Forward references to structures
147*/
drh7020f652000-06-03 18:06:52 +0000148typedef struct Column Column;
drh75897232000-05-29 14:26:00 +0000149typedef struct Table Table;
150typedef struct Index Index;
151typedef struct Instruction Instruction;
152typedef struct Expr Expr;
153typedef struct ExprList ExprList;
154typedef struct Parse Parse;
155typedef struct Token Token;
156typedef struct IdList IdList;
157typedef struct WhereInfo WhereInfo;
drh6b563442001-11-07 16:48:26 +0000158typedef struct WhereLevel WhereLevel;
drh9bb61fe2000-06-05 16:01:39 +0000159typedef struct Select Select;
drh22827922000-06-06 17:27:05 +0000160typedef struct AggExpr AggExpr;
drh8e0a2f92002-02-23 23:45:45 +0000161typedef struct UserFunc UserFunc;
drh75897232000-05-29 14:26:00 +0000162
163/*
164** Each database is an instance of the following structure
165*/
166struct sqlite {
drh5e00f6c2001-09-13 13:46:56 +0000167 Btree *pBe; /* The B*Tree backend */
drhf57b3392001-10-08 13:22:32 +0000168 Btree *pBeTemp; /* Backend for session temporary tables */
drh5e00f6c2001-09-13 13:46:56 +0000169 int flags; /* Miscellanous flags. See below */
170 int file_format; /* What file format version is this database? */
drh50e5dad2001-09-15 00:57:28 +0000171 int schema_cookie; /* Magic number that changes with the schema */
172 int next_cookie; /* Value of schema_cookie after commit */
drh5e00f6c2001-09-13 13:46:56 +0000173 int nTable; /* Number of tables in the database */
174 void *pBusyArg; /* 1st Argument to the busy callback */
drh353f57e2000-08-02 12:26:28 +0000175 int (*xBusyCallback)(void *,const char*,int); /* The busy callback */
drhbeae3192001-09-22 18:12:08 +0000176 Hash tblHash; /* All tables indexed by name */
177 Hash idxHash; /* All (named) indices indexed by name */
drh74e24cd2002-01-09 03:19:59 +0000178 Hash tblDrop; /* Uncommitted DROP TABLEs */
179 Hash idxDrop; /* Uncommitted DROP INDEXs */
drh8e0a2f92002-02-23 23:45:45 +0000180 Hash userFunc; /* User defined functions */
drhaf9ff332002-01-16 21:00:27 +0000181 int lastRowid; /* ROWID of most recent insert */
drh5cf8e8c2002-02-19 22:42:05 +0000182 int priorNewRowid; /* Last randomly generated ROWID */
drh1c928532002-01-31 15:54:21 +0000183 int onError; /* Default conflict algorithm */
drh75897232000-05-29 14:26:00 +0000184};
185
186/*
drh967e8b72000-06-21 13:59:10 +0000187** Possible values for the sqlite.flags.
drh75897232000-05-29 14:26:00 +0000188*/
drh5e00f6c2001-09-13 13:46:56 +0000189#define SQLITE_VdbeTrace 0x00000001 /* True to trace VDBE execution */
190#define SQLITE_Initialized 0x00000002 /* True after initialization */
191#define SQLITE_Interrupt 0x00000004 /* Cancel current operation */
192#define SQLITE_InTrans 0x00000008 /* True if in a transaction */
193#define SQLITE_InternChanges 0x00000010 /* Uncommitted Hash table changes */
drh382c0242001-10-06 16:33:02 +0000194#define SQLITE_FullColNames 0x00000020 /* Show full column names on SELECT */
drh1bee3d72001-10-15 00:44:35 +0000195#define SQLITE_CountRows 0x00000040 /* Count rows changed by INSERT, */
196 /* DELETE, or UPDATE and return */
197 /* the count using a callback. */
drh6a535342001-10-19 16:44:56 +0000198#define SQLITE_NullCallback 0x00000080 /* Invoke the callback once if the */
199 /* result set is empty */
drhc3a64ba2001-11-22 00:01:27 +0000200#define SQLITE_ResultDetails 0x00000100 /* Details added to result set */
drh58b95762000-06-02 01:17:37 +0000201
202/*
drh8e0a2f92002-02-23 23:45:45 +0000203** Each user-defined function is defined by an instance of the following
204** structure. A pointer to this structure is stored in the sqlite.userFunc
205** hash table. When multiple functions have the same name, the hash table
206** points to a linked list of these structures.
drh28037572000-08-02 13:47:41 +0000207*/
drh8e0a2f92002-02-23 23:45:45 +0000208struct UserFunc {
209 void (*xFunc)(void*,int,const char**); /* Regular function */
210 void *(*xStep)(void*,int,const char**); /* Aggregate function step */
211 void (*xFinalize)(void*,void*); /* Aggregate function finializer */
212 int nArg; /* Number of arguments */
213 UserFunc *pNext; /* Next function with same name */
214};
drh28037572000-08-02 13:47:41 +0000215
216/*
drh967e8b72000-06-21 13:59:10 +0000217** information about each column of an SQL table is held in an instance
drh7020f652000-06-03 18:06:52 +0000218** of this structure.
219*/
220struct Column {
drh967e8b72000-06-21 13:59:10 +0000221 char *zName; /* Name of this column */
222 char *zDflt; /* Default value of this column */
drh382c0242001-10-06 16:33:02 +0000223 char *zType; /* Data type for this column */
drh4a324312001-12-21 14:30:42 +0000224 u8 notNull; /* True if there is a NOT NULL constraint */
225 u8 isPrimKey; /* True if this column is an INTEGER PRIMARY KEY */
drh7020f652000-06-03 18:06:52 +0000226};
227
228/*
drh22f70c32002-02-18 01:17:00 +0000229** Each SQL table is represented in memory by an instance of the
230** following structure.
231**
232** Expr.zName is the name of the table. The case of the original
233** CREATE TABLE statement is stored, but case is not significant for
234** comparisons.
235**
236** Expr.nCol is the number of columns in this table. Expr.aCol is a
237** pointer to an array of Column structures, one for each column.
238**
239** If the table has an INTEGER PRIMARY KEY, then Expr.iPKey is the index of
240** the column that is that key. Otherwise Expr.iPKey is negative. Note
241** that the datatype of the PRIMARY KEY must be INTEGER for this field to
242** be set. An INTEGER PRIMARY KEY is used as the rowid for each row of
243** the table. If a table has no INTEGER PRIMARY KEY, then a random rowid
244** is generated for each row of the table. Expr.hasPrimKey is true if
245** the table has any PRIMARY KEY, INTEGER or otherwise.
246**
247** Expr.tnum is the page number for the root BTree page of the table in the
248** database file. If Expr.isTemp is true, then this page occurs in the
249** auxiliary database file, not the main database file. If Expr.isTransient
250** is true, then the table is stored in a file that is automatically deleted
251** when the VDBE cursor to the table is closed. In this case Expr.tnum
252** refers VDBE cursor number that holds the table open, not to the root
253** page number. Transient tables are used to hold the results of a
254** sub-query that appears instead of a real table name in the FROM clause
255** of a SELECT statement.
drh75897232000-05-29 14:26:00 +0000256*/
257struct Table {
drh967e8b72000-06-21 13:59:10 +0000258 char *zName; /* Name of the table */
drh967e8b72000-06-21 13:59:10 +0000259 int nCol; /* Number of columns in this table */
260 Column *aCol; /* Information about each column */
drhc8392582001-12-31 02:48:51 +0000261 int iPKey; /* If not less then 0, use aCol[iPKey] as the primary key */
drh967e8b72000-06-21 13:59:10 +0000262 Index *pIndex; /* List of SQL indexes on this table. */
drh22f70c32002-02-18 01:17:00 +0000263 int tnum; /* Root BTree node for this table (see note above) */
drha76b5df2002-02-23 02:32:10 +0000264 Select *pSelect; /* NULL for tables. Points to definition if a view. */
drh717e6402001-09-27 03:22:32 +0000265 u8 readOnly; /* True if this table should not be written by the user */
266 u8 isCommit; /* True if creation of this table has been committed */
drhf57b3392001-10-08 13:22:32 +0000267 u8 isTemp; /* True if stored in db->pBeTemp instead of db->pBe */
drh22f70c32002-02-18 01:17:00 +0000268 u8 isTransient; /* True if automatically deleted when VDBE finishes */
drh4a324312001-12-21 14:30:42 +0000269 u8 hasPrimKey; /* True if there exists a primary key */
drh9cfcf5d2002-01-29 18:41:24 +0000270 u8 keyConf; /* What to do in case of uniqueness conflict on iPKey */
drh75897232000-05-29 14:26:00 +0000271};
272
273/*
drh22f70c32002-02-18 01:17:00 +0000274** SQLite supports 5 different ways to resolve a contraint
275** error. ROLLBACK processing means that a constraint violation
drh1c928532002-01-31 15:54:21 +0000276** causes the operation in proces to fail and for the current transaction
277** to be rolled back. ABORT processing means the operation in process
278** fails and any prior changes from that one operation are backed out,
279** but the transaction is not rolled back. FAIL processing means that
280** the operation in progress stops and returns an error code. But prior
281** changes due to the same operation are not backed out and no rollback
282** occurs. IGNORE means that the particular row that caused the constraint
283** error is not inserted or updated. Processing continues and no error
284** is returned. REPLACE means that preexisting database rows that caused
285** a UNIQUE constraint violation are removed so that the new insert or
286** update can proceed. Processing continues and no error is reported.
287**
288** The following there symbolic values are used to record which type
289** of action to take.
drh9cfcf5d2002-01-29 18:41:24 +0000290*/
drh1c928532002-01-31 15:54:21 +0000291#define OE_None 0 /* There is no constraint to check */
292#define OE_Rollback 1 /* Fail the operation and rollback the transaction */
293#define OE_Abort 2 /* Back out changes but do no rollback transaction */
294#define OE_Fail 3 /* Stop the operation but leave all prior changes */
295#define OE_Ignore 4 /* Ignore the error. Do not do the INSERT or UPDATE */
296#define OE_Replace 5 /* Delete existing record, then do INSERT or UPDATE */
297#define OE_Default 9 /* Do whatever the default action is */
drh9cfcf5d2002-01-29 18:41:24 +0000298
299/*
drh66b89c82000-11-28 20:47:17 +0000300** Each SQL index is represented in memory by an
drh75897232000-05-29 14:26:00 +0000301** instance of the following structure.
drh967e8b72000-06-21 13:59:10 +0000302**
303** The columns of the table that are to be indexed are described
304** by the aiColumn[] field of this structure. For example, suppose
305** we have the following table and index:
306**
307** CREATE TABLE Ex1(c1 int, c2 int, c3 text);
308** CREATE INDEX Ex2 ON Ex1(c3,c1);
309**
310** In the Table structure describing Ex1, nCol==3 because there are
311** three columns in the table. In the Index structure describing
312** Ex2, nColumn==2 since 2 of the 3 columns of Ex1 are indexed.
313** The value of aiColumn is {2, 0}. aiColumn[0]==2 because the
314** first column to be indexed (c3) has an index of 2 in Ex1.aCol[].
315** The second column to be indexed (c1) has an index of 0 in
316** Ex1.aCol[], hence Ex2.aiColumn[1]==0.
drh75897232000-05-29 14:26:00 +0000317*/
318struct Index {
drh967e8b72000-06-21 13:59:10 +0000319 char *zName; /* Name of this index */
drh967e8b72000-06-21 13:59:10 +0000320 int nColumn; /* Number of columns in the table used by this index */
321 int *aiColumn; /* Which columns are used by this index. 1st is 0 */
322 Table *pTable; /* The SQL table being indexed */
drhbe0072d2001-09-13 14:46:09 +0000323 int tnum; /* Page containing root of this index in database file */
drh9cfcf5d2002-01-29 18:41:24 +0000324 u8 isUnique; /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
drh717e6402001-09-27 03:22:32 +0000325 u8 isCommit; /* True if creation of this index has been committed */
drh74e24cd2002-01-09 03:19:59 +0000326 u8 isDropped; /* True if a DROP INDEX has executed on this index */
drh9cfcf5d2002-01-29 18:41:24 +0000327 u8 onError; /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
drh967e8b72000-06-21 13:59:10 +0000328 Index *pNext; /* The next index associated with the same table */
drh75897232000-05-29 14:26:00 +0000329};
330
331/*
332** Each token coming out of the lexer is an instance of
333** this structure.
334*/
335struct Token {
drh80ff32f2001-11-04 18:32:46 +0000336 const char *z; /* Text of the token. Not NULL-terminated! */
337 int n; /* Number of characters in this token */
drh75897232000-05-29 14:26:00 +0000338};
339
340/*
341** Each node of an expression in the parse tree is an instance
drh22f70c32002-02-18 01:17:00 +0000342** of this structure.
343**
344** Expr.op is the opcode. The integer parser token codes are reused
345** as opcodes here. For example, the parser defines TK_GE to be an integer
346** code representing the ">=" operator. This same integer code is reused
347** to represent the greater-than-or-equal-to operator in the expression
348** tree.
349**
350** Expr.pRight and Expr.pLeft are subexpressions. Expr.pList is a list
351** of argument if the expression is a function.
352**
353** Expr.token is the operator token for this node. Expr.span is the complete
354** subexpression represented by this node and all its decendents. These
355** fields are used for error reporting and for reconstructing the text of
356** an expression to use as the column name in a SELECT statement.
357**
358** An expression of the form ID or ID.ID refers to a column in a table.
359** For such expressions, Expr.op is set to TK_COLUMN and Expr.iTable is
360** the integer cursor number of a VDBE cursor pointing to that table and
361** Expr.iColumn is the column number for the specific column. If the
362** expression is used as a result in an aggregate SELECT, then the
363** value is also stored in the Expr.iAgg column in the aggregate so that
364** it can be accessed after all aggregates are computed.
365**
366** If the expression is a function, the Expr.iTable is an integer code
367** representing which function.
368**
369** The Expr.pSelect field points to a SELECT statement. The SELECT might
370** be the right operand of an IN operator. Or, if a scalar SELECT appears
371** in an expression the opcode is TK_SELECT and Expr.pSelect is the only
372** operand.
drh75897232000-05-29 14:26:00 +0000373*/
374struct Expr {
375 int op; /* Operation performed by this node */
376 Expr *pLeft, *pRight; /* Left and right subnodes */
377 ExprList *pList; /* A list of expressions used as a function argument */
378 Token token; /* An operand token */
drhe1b6a5b2000-07-29 13:06:59 +0000379 Token span; /* Complete text of the expression */
drh967e8b72000-06-21 13:59:10 +0000380 int iTable, iColumn; /* When op==TK_COLUMN, then this expr node means the
381 ** iColumn-th field of the iTable-th table. When
382 ** op==TK_FUNCTION, iColumn holds the function id */
383 int iAgg; /* When op==TK_COLUMN and pParse->useAgg==TRUE, pull
384 ** result from the iAgg-th element of the aggregator */
drh19a775c2000-06-05 18:54:46 +0000385 Select *pSelect; /* When the expression is a sub-select */
drh75897232000-05-29 14:26:00 +0000386};
387
388/*
389** A list of expressions. Each expression may optionally have a
390** name. An expr/name combination can be used in several ways, such
391** as the list of "expr AS ID" fields following a "SELECT" or in the
392** list of "ID = expr" items in an UPDATE. A list of expressions can
393** also be used as the argument to a function, in which case the azName
394** field is not used.
395*/
396struct ExprList {
397 int nExpr; /* Number of expressions on the list */
drh6d4abfb2001-10-22 02:58:08 +0000398 struct ExprList_item {
drh75897232000-05-29 14:26:00 +0000399 Expr *pExpr; /* The list of expressions */
400 char *zName; /* Token associated with this expression */
drhd8bc7082000-06-07 23:51:50 +0000401 char sortOrder; /* 1 for DESC or 0 for ASC */
402 char isAgg; /* True if this is an aggregate like count(*) */
403 char done; /* A flag to indicate when processing is finished */
drh75897232000-05-29 14:26:00 +0000404 } *a; /* One entry for each expression */
405};
406
407/*
408** A list of identifiers.
409*/
410struct IdList {
411 int nId; /* Number of identifiers on the list */
drh6d4abfb2001-10-22 02:58:08 +0000412 struct IdList_item {
drh75897232000-05-29 14:26:00 +0000413 char *zName; /* Text of the identifier. */
414 char *zAlias; /* The "B" part of a "A AS B" phrase. zName is the "A" */
drh967e8b72000-06-21 13:59:10 +0000415 int idx; /* Index in some Table.aCol[] of a column named zName */
drhdaffd0e2001-04-11 14:28:42 +0000416 Table *pTab; /* An SQL table corresponding to zName */
417 Select *pSelect; /* A SELECT statement used in place of a table name */
drh75897232000-05-29 14:26:00 +0000418 } *a; /* One entry for each identifier on the list */
419};
420
421/*
drh6b563442001-11-07 16:48:26 +0000422** For each nested loop in a WHERE clause implementation, the WhereInfo
423** structure contains a single instance of this structure. This structure
424** is intended to be private the the where.c module and should not be
425** access or modified by other modules.
426*/
427struct WhereLevel {
428 int iMem; /* Memory cell used by this level */
429 Index *pIdx; /* Index used */
430 int iCur; /* Cursor number used for this index */
drh487ab3c2001-11-08 00:45:21 +0000431 int score; /* How well this indexed scored */
drh6b563442001-11-07 16:48:26 +0000432 int brk; /* Jump here to break out of the loop */
433 int cont; /* Jump here to continue with the next loop cycle */
434 int op, p1, p2; /* Opcode used to terminate the loop */
435};
436
437/*
drh75897232000-05-29 14:26:00 +0000438** The WHERE clause processing routine has two halves. The
439** first part does the start of the WHERE loop and the second
440** half does the tail of the WHERE loop. An instance of
441** this structure is returned by the first half and passed
442** into the second half to give some continuity.
443*/
444struct WhereInfo {
445 Parse *pParse;
drh19a775c2000-06-05 18:54:46 +0000446 IdList *pTabList; /* List of tables in the join */
447 int iContinue; /* Jump here to continue with next record */
448 int iBreak; /* Jump here to break out of the loop */
449 int base; /* Index of first Open opcode */
drh6b563442001-11-07 16:48:26 +0000450 int nLevel; /* Number of nested loop */
451 WhereLevel a[1]; /* Information about each nest loop in the WHERE */
drh75897232000-05-29 14:26:00 +0000452};
453
454/*
drh9bb61fe2000-06-05 16:01:39 +0000455** An instance of the following structure contains all information
456** needed to generate code for a single SELECT statement.
drha76b5df2002-02-23 02:32:10 +0000457**
458** The zSelect field is used when the Select structure must be persistent.
459** Normally, the expression tree points to tokens in the original input
460** string that encodes the select. But if the Select structure must live
461** longer than its input string (for example when it is used to describe
462** a VIEW) we have to make a copy of the input string so that the nodes
463** of the expression tree will have something to point to. zSelect is used
464** to hold that copy.
drh9bb61fe2000-06-05 16:01:39 +0000465*/
466struct Select {
467 int isDistinct; /* True if the DISTINCT keyword is present */
468 ExprList *pEList; /* The fields of the result */
469 IdList *pSrc; /* The FROM clause */
470 Expr *pWhere; /* The WHERE clause */
471 ExprList *pGroupBy; /* The GROUP BY clause */
472 Expr *pHaving; /* The HAVING clause */
473 ExprList *pOrderBy; /* The ORDER BY clause */
drh82c3d632000-06-06 21:56:07 +0000474 int op; /* One of: TK_UNION TK_ALL TK_INTERSECT TK_EXCEPT */
drh967e8b72000-06-21 13:59:10 +0000475 Select *pPrior; /* Prior select in a compound select statement */
drh9bbca4c2001-11-06 04:00:18 +0000476 int nLimit, nOffset; /* LIMIT and OFFSET values. -1 means not used */
drha76b5df2002-02-23 02:32:10 +0000477 char *zSelect; /* Complete text of the SELECT command */
drh9bb61fe2000-06-05 16:01:39 +0000478};
479
480/*
drhfef52082000-06-06 01:50:43 +0000481** The results of a select can be distributed in several ways.
482*/
483#define SRT_Callback 1 /* Invoke a callback with each row of result */
484#define SRT_Mem 2 /* Store result in a memory cell */
drh82c3d632000-06-06 21:56:07 +0000485#define SRT_Set 3 /* Store result as unique keys in a table */
486#define SRT_Union 5 /* Store result as keys in a table */
487#define SRT_Except 6 /* Remove result from a UNION table */
drh5974a302000-06-07 14:42:26 +0000488#define SRT_Table 7 /* Store result as data with a unique key */
drhfef52082000-06-06 01:50:43 +0000489
490/*
drh22827922000-06-06 17:27:05 +0000491** When a SELECT uses aggregate functions (like "count(*)" or "avg(f1)")
492** we have to do some additional analysis of expressions. An instance
493** of the following structure holds information about a single subexpression
494** somewhere in the SELECT statement. An array of these structures holds
495** all the information we need to generate code for aggregate
496** expressions.
497**
498** Note that when analyzing a SELECT containing aggregates, both
499** non-aggregate field variables and aggregate functions are stored
500** in the AggExpr array of the Parser structure.
501**
502** The pExpr field points to an expression that is part of either the
503** field list, the GROUP BY clause, the HAVING clause or the ORDER BY
504** clause. The expression will be freed when those clauses are cleaned
505** up. Do not try to delete the expression attached to AggExpr.pExpr.
506**
507** If AggExpr.pExpr==0, that means the expression is "count(*)".
508*/
509struct AggExpr {
510 int isAgg; /* if TRUE contains an aggregate function */
511 Expr *pExpr; /* The expression */
512};
513
514/*
drhf57b3392001-10-08 13:22:32 +0000515** An SQL parser context. A copy of this structure is passed through
516** the parser and down into all the parser action routine in order to
517** carry around information that is global to the entire parse.
drh75897232000-05-29 14:26:00 +0000518*/
519struct Parse {
520 sqlite *db; /* The main database structure */
drh5e00f6c2001-09-13 13:46:56 +0000521 Btree *pBe; /* The database backend */
drh4c504392000-10-16 22:06:40 +0000522 int rc; /* Return code from execution */
drh75897232000-05-29 14:26:00 +0000523 sqlite_callback xCallback; /* The callback function */
524 void *pArg; /* First argument to the callback function */
525 char *zErrMsg; /* An error message */
526 Token sErrToken; /* The token at which the error occurred */
527 Token sFirstToken; /* The first token parsed */
528 Token sLastToken; /* The last token parsed */
529 Table *pNewTable; /* A table being constructed by CREATE TABLE */
530 Vdbe *pVdbe; /* An engine for executing database bytecode */
drhd8bc7082000-06-07 23:51:50 +0000531 int colNamesSet; /* TRUE after OP_ColumnCount has been issued to pVdbe */
drh75897232000-05-29 14:26:00 +0000532 int explain; /* True if the EXPLAIN flag is found on the query */
533 int initFlag; /* True if reparsing CREATE TABLEs */
drhf57b3392001-10-08 13:22:32 +0000534 int nameClash; /* A permanent table name clashes with temp table name */
drhd78eeee2001-09-13 16:18:53 +0000535 int newTnum; /* Table number to use when reparsing CREATE TABLEs */
drh75897232000-05-29 14:26:00 +0000536 int nErr; /* Number of errors seen */
drh19a775c2000-06-05 18:54:46 +0000537 int nTab; /* Number of previously allocated cursors */
538 int nMem; /* Number of memory cells used so far */
drhfef52082000-06-06 01:50:43 +0000539 int nSet; /* Number of sets used so far */
drh22827922000-06-06 17:27:05 +0000540 int nAgg; /* Number of aggregate expressions */
541 AggExpr *aAgg; /* An array of aggregate expressions */
542 int iAggCount; /* Index of the count(*) aggregate in aAgg[] */
543 int useAgg; /* If true, extract field values from the aggregator
544 ** while generating expressions. Normally false */
drh50e5dad2001-09-15 00:57:28 +0000545 int schemaVerified; /* True if an OP_VerifySchema has been coded someplace
546 ** other than after an OP_Transaction */
drh75897232000-05-29 14:26:00 +0000547};
548
549/*
550** Internal function prototypes
551*/
552int sqliteStrICmp(const char *, const char *);
553int sqliteStrNICmp(const char *, const char *, int);
554int sqliteHashNoCase(const char *, int);
555int sqliteCompare(const char *, const char *);
556int sqliteSortCompare(const char *, const char *);
drh9bbca4c2001-11-06 04:00:18 +0000557void sqliteRealToSortable(double r, char *);
drhdcc581c2000-05-30 13:44:19 +0000558#ifdef MEMORY_DEBUG
559 void *sqliteMalloc_(int,char*,int);
560 void sqliteFree_(void*,char*,int);
561 void *sqliteRealloc_(void*,int,char*,int);
drh6e142f52000-06-08 13:36:40 +0000562 char *sqliteStrDup_(const char*,char*,int);
563 char *sqliteStrNDup_(const char*, int,char*,int);
drhdcc581c2000-05-30 13:44:19 +0000564#else
565 void *sqliteMalloc(int);
566 void sqliteFree(void*);
567 void *sqliteRealloc(void*,int);
drh6e142f52000-06-08 13:36:40 +0000568 char *sqliteStrDup(const char*);
569 char *sqliteStrNDup(const char*, int);
drhdcc581c2000-05-30 13:44:19 +0000570#endif
drh75897232000-05-29 14:26:00 +0000571void sqliteSetString(char **, const char *, ...);
572void sqliteSetNString(char **, ...);
drh982cef72000-05-30 16:27:03 +0000573void sqliteDequote(char*);
drh17f71932002-02-21 12:01:27 +0000574int sqliteKeywordCode(const char*, int);
drh80ff32f2001-11-04 18:32:46 +0000575int sqliteRunParser(Parse*, const char*, char **);
drh75897232000-05-29 14:26:00 +0000576void sqliteExec(Parse*);
577Expr *sqliteExpr(int, Expr*, Expr*, Token*);
drhe1b6a5b2000-07-29 13:06:59 +0000578void sqliteExprSpan(Expr*,Token*,Token*);
drh75897232000-05-29 14:26:00 +0000579Expr *sqliteExprFunction(ExprList*, Token*);
580void sqliteExprDelete(Expr*);
581ExprList *sqliteExprListAppend(ExprList*,Expr*,Token*);
582void sqliteExprListDelete(ExprList*);
drhf57b14a2001-09-14 18:54:08 +0000583void sqlitePragma(Parse*,Token*,Token*,int);
drh5e00f6c2001-09-13 13:46:56 +0000584void sqliteCommitInternalChanges(sqlite*);
585void sqliteRollbackInternalChanges(sqlite*);
drh969fa7c2002-02-18 18:30:32 +0000586Table *sqliteResultSetOfSelect(Parse*,char*,Select*);
drhf57b3392001-10-08 13:22:32 +0000587void sqliteStartTable(Parse*,Token*,Token*,int);
drh75897232000-05-29 14:26:00 +0000588void sqliteAddColumn(Parse*,Token*);
drh9cfcf5d2002-01-29 18:41:24 +0000589void sqliteAddNotNull(Parse*, int);
590void sqliteAddPrimaryKey(Parse*, IdList*, int);
drh382c0242001-10-06 16:33:02 +0000591void sqliteAddColumnType(Parse*,Token*,Token*);
drh7020f652000-06-03 18:06:52 +0000592void sqliteAddDefaultValue(Parse*,Token*,int);
drh969fa7c2002-02-18 18:30:32 +0000593void sqliteEndTable(Parse*,Token*,Select*);
drha76b5df2002-02-23 02:32:10 +0000594void sqliteCreateView(Parse*,Token*,Token*,Select*);
drh75897232000-05-29 14:26:00 +0000595void sqliteDropTable(Parse*, Token*);
596void sqliteDeleteTable(sqlite*, Table*);
drh9cfcf5d2002-01-29 18:41:24 +0000597void sqliteInsert(Parse*, Token*, ExprList*, Select*, IdList*, int);
drh75897232000-05-29 14:26:00 +0000598IdList *sqliteIdListAppend(IdList*, Token*);
599void sqliteIdListAddAlias(IdList*, Token*);
600void sqliteIdListDelete(IdList*);
drh717e6402001-09-27 03:22:32 +0000601void sqliteCreateIndex(Parse*, Token*, Token*, IdList*, int, Token*, Token*);
drh75897232000-05-29 14:26:00 +0000602void sqliteDropIndex(Parse*, Token*);
drh19a775c2000-06-05 18:54:46 +0000603int sqliteSelect(Parse*, Select*, int, int);
drh9bbca4c2001-11-06 04:00:18 +0000604Select *sqliteSelectNew(ExprList*,IdList*,Expr*,ExprList*,Expr*,ExprList*,
605 int,int,int);
drh9bb61fe2000-06-05 16:01:39 +0000606void sqliteSelectDelete(Select*);
drha76b5df2002-02-23 02:32:10 +0000607Table *sqliteTableNameToTable(Parse*, const char*);
608IdList *sqliteTableTokenToIdList(Parse*, Token*);
drh75897232000-05-29 14:26:00 +0000609void sqliteDeleteFrom(Parse*, Token*, Expr*);
drh9cfcf5d2002-01-29 18:41:24 +0000610void sqliteUpdate(Parse*, Token*, ExprList*, Expr*, int);
drh75897232000-05-29 14:26:00 +0000611WhereInfo *sqliteWhereBegin(Parse*, IdList*, Expr*, int);
612void sqliteWhereEnd(WhereInfo*);
613void sqliteExprCode(Parse*, Expr*);
614void sqliteExprIfTrue(Parse*, Expr*, int);
615void sqliteExprIfFalse(Parse*, Expr*, int);
drha76b5df2002-02-23 02:32:10 +0000616Table *sqliteFindTable(sqlite*,const char*);
617Index *sqliteFindIndex(sqlite*,const char*);
drh6d4abfb2001-10-22 02:58:08 +0000618void sqliteUnlinkAndDeleteIndex(sqlite*,Index*);
drhb419a922002-01-30 16:17:23 +0000619void sqliteCopy(Parse*, Token*, Token*, Token*, int);
drhdce2cbe2000-05-31 02:27:49 +0000620void sqliteVacuum(Parse*, Token*);
drhe17a7e32001-04-04 21:10:18 +0000621int sqliteGlobCompare(const unsigned char*,const unsigned char*);
drhdce2cbe2000-05-31 02:27:49 +0000622int sqliteLikeCompare(const unsigned char*,const unsigned char*);
drhcce7d172000-05-31 15:34:51 +0000623char *sqliteTableNameFromToken(Token*);
624int sqliteExprCheck(Parse*, Expr*, int, int*);
drhd8bc7082000-06-07 23:51:50 +0000625int sqliteExprCompare(Expr*, Expr*);
drhcce7d172000-05-31 15:34:51 +0000626int sqliteFuncId(Token*);
drha2e00042002-01-22 03:13:42 +0000627int sqliteExprResolveIds(Parse*, IdList*, ExprList*, Expr*);
drh4794b982000-06-06 13:54:14 +0000628void sqliteExprResolveInSelect(Parse*, Expr*);
drh22827922000-06-06 17:27:05 +0000629int sqliteExprAnalyzeAggregates(Parse*, Expr*);
drhd8bc7082000-06-07 23:51:50 +0000630Vdbe *sqliteGetVdbe(Parse*);
drhb8ca3072001-12-05 00:21:20 +0000631int sqliteRandomByte(void);
632int sqliteRandomInteger(void);
drh1c928532002-01-31 15:54:21 +0000633void sqliteBeginTransaction(Parse*, int);
drhc4a3c772001-04-04 11:48:57 +0000634void sqliteCommitTransaction(Parse*);
635void sqliteRollbackTransaction(Parse*);
drhd1bf3512001-04-07 15:24:33 +0000636char *sqlite_mprintf(const char *, ...);
drh92086432002-01-22 14:11:29 +0000637int sqliteExprIsConstant(Expr*);
drh0ca3e242002-01-29 23:07:02 +0000638void sqliteGenerateRowDelete(Vdbe*, Table*, int);
639void sqliteGenerateRowIndexDelete(Vdbe*, Table*, int, char*);
640void sqliteGenerateConstraintChecks(Parse*,Table*,int,char*,int,int,int,int);
drhb419a922002-01-30 16:17:23 +0000641void sqliteCompleteInsertion(Parse*, Table*, int, char*, int, int);
drh1c928532002-01-31 15:54:21 +0000642void sqliteBeginWriteOperation(Parse*);
drh663fc632002-02-02 18:49:19 +0000643void sqliteBeginMultiWriteOperation(Parse*);
drh1c928532002-01-31 15:54:21 +0000644void sqliteEndWriteOperation(Parse*);
drha76b5df2002-02-23 02:32:10 +0000645void sqliteExprMoveStrings(Expr*, int);
646void sqliteExprListMoveStrings(ExprList*, int);
647void sqliteSelectMoveStrings(Select*, int);
drh8e0a2f92002-02-23 23:45:45 +0000648UserFunc *sqliteFindUserFunction(sqlite*,const char*,int,int,int);
drhdc04c582002-02-24 01:55:15 +0000649void sqliteRegisterBuildinFunctions(sqlite*);