175897234Sdrh /* 2b19a2bc6Sdrh ** 2001 September 15 375897234Sdrh ** 4b19a2bc6Sdrh ** The author disclaims copyright to this source code. In place of 5b19a2bc6Sdrh ** a legal notice, here is a blessing: 675897234Sdrh ** 7b19a2bc6Sdrh ** May you do good and not evil. 8b19a2bc6Sdrh ** May you find forgiveness for yourself and forgive others. 9b19a2bc6Sdrh ** May you share freely, never taking more than you give. 1075897234Sdrh ** 1175897234Sdrh ************************************************************************* 1275897234Sdrh ** Internal interface definitions for SQLite. 1375897234Sdrh ** 14*c22bd47dSdrh ** @(#) $Id: sqliteInt.h,v 1.107 2002/05/10 13:14:07 drh Exp $ 1575897234Sdrh */ 1675897234Sdrh #include "sqlite.h" 17beae3194Sdrh #include "hash.h" 1875897234Sdrh #include "vdbe.h" 1975897234Sdrh #include "parse.h" 20be0072d2Sdrh #include "btree.h" 2175897234Sdrh #include <stdio.h> 2275897234Sdrh #include <stdlib.h> 2375897234Sdrh #include <string.h> 2475897234Sdrh #include <assert.h> 2575897234Sdrh 26967e8b73Sdrh /* 27a1b351afSdrh ** The maximum number of in-memory pages to use for the main database 28a1b351afSdrh ** table and for temporary tables. 29a1b351afSdrh */ 30603240cfSdrh #define MAX_PAGES 2000 31603240cfSdrh #define TEMP_PAGES 500 32a1b351afSdrh 33a1b351afSdrh /* 345a2c2c20Sdrh ** Integers of known sizes. These typedefs might change for architectures 355a2c2c20Sdrh ** where the sizes very. Preprocessor macros are available so that the 365a2c2c20Sdrh ** types can be conveniently redefined at compile-type. Like this: 375a2c2c20Sdrh ** 385a2c2c20Sdrh ** cc '-DUINTPTR_TYPE=long long int' ... 3941a2b48bSdrh */ 405a2c2c20Sdrh #ifndef UINT32_TYPE 415a2c2c20Sdrh # define UINT32_TYPE unsigned int 425a2c2c20Sdrh #endif 435a2c2c20Sdrh #ifndef UINT16_TYPE 445a2c2c20Sdrh # define UINT16_TYPE unsigned short int 455a2c2c20Sdrh #endif 465a2c2c20Sdrh #ifndef UINT8_TYPE 475a2c2c20Sdrh # define UINT8_TYPE unsigned char 485a2c2c20Sdrh #endif 495a2c2c20Sdrh #ifndef INTPTR_TYPE 505a2c2c20Sdrh # define INTPTR_TYPE int 515a2c2c20Sdrh #endif 525a2c2c20Sdrh typedef UINT32_TYPE u32; /* 4-byte unsigned integer */ 535a2c2c20Sdrh typedef UINT16_TYPE u16; /* 2-byte unsigned integer */ 545a2c2c20Sdrh typedef UINT8_TYPE u8; /* 1-byte unsigned integer */ 555a2c2c20Sdrh typedef INTPTR_TYPE ptr; /* Big enough to hold a pointer */ 565a2c2c20Sdrh typedef unsigned INTPTR_TYPE uptr; /* Big enough to hold a pointer */ 575a2c2c20Sdrh 585a2c2c20Sdrh /* 595a2c2c20Sdrh ** This macro casts a pointer to an integer. Useful for doing 605a2c2c20Sdrh ** pointer arithmetic. 615a2c2c20Sdrh */ 625a2c2c20Sdrh #define Addr(X) ((uptr)X) 6341a2b48bSdrh 6441a2b48bSdrh /* 65872ff86fSdrh ** The maximum number of bytes of data that can be put into a single 6680ff32f5Sdrh ** row of a single table. The upper bound on this limit is 16777215 6780ff32f5Sdrh ** bytes (or 16MB-1). We have arbitrarily set the limit to just 1MB 6880ff32f5Sdrh ** here because the overflow page chain is inefficient for really big 6980ff32f5Sdrh ** records and we want to discourage people from thinking that 7080ff32f5Sdrh ** multi-megabyte records are OK. If your needs are different, you can 7180ff32f5Sdrh ** change this define and recompile to increase or decrease the record 7280ff32f5Sdrh ** size. 73872ff86fSdrh */ 7480ff32f5Sdrh #define MAX_BYTES_PER_ROW 1048576 75872ff86fSdrh 76872ff86fSdrh /* 77967e8b73Sdrh ** If memory allocation problems are found, recompile with 78967e8b73Sdrh ** 79967e8b73Sdrh ** -DMEMORY_DEBUG=1 80967e8b73Sdrh ** 81967e8b73Sdrh ** to enable some sanity checking on malloc() and free(). To 82967e8b73Sdrh ** check for memory leaks, recompile with 83967e8b73Sdrh ** 84967e8b73Sdrh ** -DMEMORY_DEBUG=2 85967e8b73Sdrh ** 86967e8b73Sdrh ** and a line of text will be written to standard error for 87967e8b73Sdrh ** each malloc() and free(). This output can be analyzed 88967e8b73Sdrh ** by an AWK script to determine if there are any leaks. 89967e8b73Sdrh */ 90dcc581ccSdrh #ifdef MEMORY_DEBUG 91dcc581ccSdrh # define sqliteMalloc(X) sqliteMalloc_(X,__FILE__,__LINE__) 92dcc581ccSdrh # define sqliteFree(X) sqliteFree_(X,__FILE__,__LINE__) 93dcc581ccSdrh # define sqliteRealloc(X,Y) sqliteRealloc_(X,Y,__FILE__,__LINE__) 946e142f54Sdrh # define sqliteStrDup(X) sqliteStrDup_(X,__FILE__,__LINE__) 956e142f54Sdrh # define sqliteStrNDup(X,Y) sqliteStrNDup_(X,Y,__FILE__,__LINE__) 96c3c2fc9aSdrh void sqliteStrRealloc(char**); 97c3c2fc9aSdrh #else 98c3c2fc9aSdrh # define sqliteStrRealloc(X) 99dcc581ccSdrh #endif 100dcc581ccSdrh 10175897234Sdrh /* 102daffd0e5Sdrh ** This variable gets set if malloc() ever fails. After it gets set, 103daffd0e5Sdrh ** the SQLite library shuts down permanently. 104daffd0e5Sdrh */ 105daffd0e5Sdrh extern int sqlite_malloc_failed; 106daffd0e5Sdrh 107daffd0e5Sdrh /* 1086e142f54Sdrh ** The following global variables are used for testing and debugging 1098c82b350Sdrh ** only. They only work if MEMORY_DEBUG is defined. 1106e142f54Sdrh */ 1116e142f54Sdrh #ifdef MEMORY_DEBUG 1128c82b350Sdrh extern int sqlite_nMalloc; /* Number of sqliteMalloc() calls */ 1138c82b350Sdrh extern int sqlite_nFree; /* Number of sqliteFree() calls */ 1148c82b350Sdrh extern int sqlite_iMallocFail; /* Fail sqliteMalloc() after this many calls */ 1156e142f54Sdrh #endif 1166e142f54Sdrh 1176e142f54Sdrh /* 11875897234Sdrh ** Name of the master database table. The master database table 11975897234Sdrh ** is a special table that holds the names and attributes of all 12075897234Sdrh ** user tables and indices. 12175897234Sdrh */ 12275897234Sdrh #define MASTER_NAME "sqlite_master" 12375897234Sdrh 12475897234Sdrh /* 12575897234Sdrh ** A convenience macro that returns the number of elements in 12675897234Sdrh ** an array. 12775897234Sdrh */ 12875897234Sdrh #define ArraySize(X) (sizeof(X)/sizeof(X[0])) 12975897234Sdrh 13075897234Sdrh /* 13175897234Sdrh ** Forward references to structures 13275897234Sdrh */ 1337020f651Sdrh typedef struct Column Column; 13475897234Sdrh typedef struct Table Table; 13575897234Sdrh typedef struct Index Index; 13675897234Sdrh typedef struct Instruction Instruction; 13775897234Sdrh typedef struct Expr Expr; 13875897234Sdrh typedef struct ExprList ExprList; 13975897234Sdrh typedef struct Parse Parse; 14075897234Sdrh typedef struct Token Token; 14175897234Sdrh typedef struct IdList IdList; 14275897234Sdrh typedef struct WhereInfo WhereInfo; 1436b56344dSdrh typedef struct WhereLevel WhereLevel; 1449bb61fe7Sdrh typedef struct Select Select; 1452282792aSdrh typedef struct AggExpr AggExpr; 1460bce8354Sdrh typedef struct FuncDef FuncDef; 14775897234Sdrh 14875897234Sdrh /* 14975897234Sdrh ** Each database is an instance of the following structure 15075897234Sdrh */ 15175897234Sdrh struct sqlite { 1525e00f6c7Sdrh Btree *pBe; /* The B*Tree backend */ 153f57b3399Sdrh Btree *pBeTemp; /* Backend for session temporary tables */ 1548c82b350Sdrh int flags; /* Miscellanous flags. See below */ 1552803757aSdrh int file_format; /* What file format version is this database? */ 15650e5dadfSdrh int schema_cookie; /* Magic number that changes with the schema */ 15750e5dadfSdrh int next_cookie; /* Value of schema_cookie after commit */ 158cd61c281Sdrh int cache_size; /* Number of pages to use in the cache */ 1592803757aSdrh int nTable; /* Number of tables in the database */ 1602dfbbcafSdrh void *pBusyArg; /* 1st Argument to the busy callback */ 161353f57e0Sdrh int (*xBusyCallback)(void *,const char*,int); /* The busy callback */ 162beae3194Sdrh Hash tblHash; /* All tables indexed by name */ 163beae3194Sdrh Hash idxHash; /* All (named) indices indexed by name */ 16474e24cd0Sdrh Hash tblDrop; /* Uncommitted DROP TABLEs */ 16574e24cd0Sdrh Hash idxDrop; /* Uncommitted DROP INDEXs */ 1660bce8354Sdrh Hash aFunc; /* All functions that can be in SQL exprs */ 167af9ff33aSdrh int lastRowid; /* ROWID of most recent insert */ 1685cf8e8c7Sdrh int priorNewRowid; /* Last randomly generated ROWID */ 1691c92853dSdrh int onError; /* Default conflict algorithm */ 170247be43dSdrh int magic; /* Magic number for detect library misuse */ 171c8d30ac1Sdrh int nChange; /* Number of rows changed */ 172c8d30ac1Sdrh int recursionDepth; /* Number of nested calls to sqlite_exec() */ 17375897234Sdrh }; 17475897234Sdrh 17575897234Sdrh /* 176967e8b73Sdrh ** Possible values for the sqlite.flags. 17775897234Sdrh */ 1784c504391Sdrh #define SQLITE_VdbeTrace 0x00000001 /* True to trace VDBE execution */ 1794c504391Sdrh #define SQLITE_Initialized 0x00000002 /* True after initialization */ 1804c504391Sdrh #define SQLITE_Interrupt 0x00000004 /* Cancel current operation */ 181c4a3c779Sdrh #define SQLITE_InTrans 0x00000008 /* True if in a transaction */ 1825e00f6c7Sdrh #define SQLITE_InternChanges 0x00000010 /* Uncommitted Hash table changes */ 183382c0247Sdrh #define SQLITE_FullColNames 0x00000020 /* Show full column names on SELECT */ 1841bee3d7bSdrh #define SQLITE_CountRows 0x00000040 /* Count rows changed by INSERT, */ 1851bee3d7bSdrh /* DELETE, or UPDATE and return */ 1861bee3d7bSdrh /* the count using a callback. */ 1876a535340Sdrh #define SQLITE_NullCallback 0x00000080 /* Invoke the callback once if the */ 1886a535340Sdrh /* result set is empty */ 189c3a64ba0Sdrh #define SQLITE_ResultDetails 0x00000100 /* Details added to result set */ 190417be79cSdrh #define SQLITE_UnresetViews 0x00000200 /* True if one or more views have */ 191417be79cSdrh /* defined column names */ 19258b9576bSdrh 19358b9576bSdrh /* 194247be43dSdrh ** Possible values for the sqlite.magic field. 195247be43dSdrh ** The numbers are obtained at random and have no special meaning, other 196247be43dSdrh ** than being distinct from one another. 197247be43dSdrh */ 198247be43dSdrh #define SQLITE_MAGIC_OPEN 0xa029a697 /* Database is open */ 199247be43dSdrh #define SQLITE_MAGIC_CLOSED 0x9f3c2d33 /* Database is closed */ 200247be43dSdrh #define SQLITE_MAGIC_BUSY 0xf03b7906 /* Database currently in use */ 201247be43dSdrh #define SQLITE_MAGIC_ERROR 0xb5357930 /* An SQLITE_MISUSE error occurred */ 202247be43dSdrh 203247be43dSdrh /* 2040bce8354Sdrh ** Each SQL function is defined by an instance of the following 2050bce8354Sdrh ** structure. A pointer to this structure is stored in the sqlite.aFunc 2068e0a2f90Sdrh ** hash table. When multiple functions have the same name, the hash table 2078e0a2f90Sdrh ** points to a linked list of these structures. 2082803757aSdrh */ 2090bce8354Sdrh struct FuncDef { 2101350b030Sdrh void (*xFunc)(sqlite_func*,int,const char**); /* Regular function */ 21156c0e926Sdrh void (*xStep)(sqlite_func*,int,const char**); /* Aggregate function step */ 2121350b030Sdrh void (*xFinalize)(sqlite_func*); /* Aggregate function finializer */ 2138e0a2f90Sdrh int nArg; /* Number of arguments */ 2141350b030Sdrh void *pUserData; /* User data parameter */ 2150bce8354Sdrh FuncDef *pNext; /* Next function with same name */ 2168e0a2f90Sdrh }; 2172803757aSdrh 2182803757aSdrh /* 219967e8b73Sdrh ** information about each column of an SQL table is held in an instance 2207020f651Sdrh ** of this structure. 2217020f651Sdrh */ 2227020f651Sdrh struct Column { 2237020f651Sdrh char *zName; /* Name of this column */ 2247020f651Sdrh char *zDflt; /* Default value of this column */ 225382c0247Sdrh char *zType; /* Data type for this column */ 2264a32431cSdrh u8 notNull; /* True if there is a NOT NULL constraint */ 2274a32431cSdrh u8 isPrimKey; /* True if this column is an INTEGER PRIMARY KEY */ 2287020f651Sdrh }; 2297020f651Sdrh 2307020f651Sdrh /* 23122f70c32Sdrh ** Each SQL table is represented in memory by an instance of the 23222f70c32Sdrh ** following structure. 23322f70c32Sdrh ** 23422f70c32Sdrh ** Expr.zName is the name of the table. The case of the original 23522f70c32Sdrh ** CREATE TABLE statement is stored, but case is not significant for 23622f70c32Sdrh ** comparisons. 23722f70c32Sdrh ** 23822f70c32Sdrh ** Expr.nCol is the number of columns in this table. Expr.aCol is a 23922f70c32Sdrh ** pointer to an array of Column structures, one for each column. 24022f70c32Sdrh ** 24122f70c32Sdrh ** If the table has an INTEGER PRIMARY KEY, then Expr.iPKey is the index of 24222f70c32Sdrh ** the column that is that key. Otherwise Expr.iPKey is negative. Note 24322f70c32Sdrh ** that the datatype of the PRIMARY KEY must be INTEGER for this field to 24422f70c32Sdrh ** be set. An INTEGER PRIMARY KEY is used as the rowid for each row of 24522f70c32Sdrh ** the table. If a table has no INTEGER PRIMARY KEY, then a random rowid 24622f70c32Sdrh ** is generated for each row of the table. Expr.hasPrimKey is true if 24722f70c32Sdrh ** the table has any PRIMARY KEY, INTEGER or otherwise. 24822f70c32Sdrh ** 24922f70c32Sdrh ** Expr.tnum is the page number for the root BTree page of the table in the 25022f70c32Sdrh ** database file. If Expr.isTemp is true, then this page occurs in the 25122f70c32Sdrh ** auxiliary database file, not the main database file. If Expr.isTransient 25222f70c32Sdrh ** is true, then the table is stored in a file that is automatically deleted 25322f70c32Sdrh ** when the VDBE cursor to the table is closed. In this case Expr.tnum 25422f70c32Sdrh ** refers VDBE cursor number that holds the table open, not to the root 25522f70c32Sdrh ** page number. Transient tables are used to hold the results of a 25622f70c32Sdrh ** sub-query that appears instead of a real table name in the FROM clause 25722f70c32Sdrh ** of a SELECT statement. 25875897234Sdrh */ 25975897234Sdrh struct Table { 26075897234Sdrh char *zName; /* Name of the table */ 26175897234Sdrh int nCol; /* Number of columns in this table */ 2627020f651Sdrh Column *aCol; /* Information about each column */ 263c8392586Sdrh int iPKey; /* If not less then 0, use aCol[iPKey] as the primary key */ 264967e8b73Sdrh Index *pIndex; /* List of SQL indexes on this table. */ 26522f70c32Sdrh int tnum; /* Root BTree node for this table (see note above) */ 266a76b5dfcSdrh Select *pSelect; /* NULL for tables. Points to definition if a view. */ 267717e6402Sdrh u8 readOnly; /* True if this table should not be written by the user */ 268717e6402Sdrh u8 isCommit; /* True if creation of this table has been committed */ 269f57b3399Sdrh u8 isTemp; /* True if stored in db->pBeTemp instead of db->pBe */ 27022f70c32Sdrh u8 isTransient; /* True if automatically deleted when VDBE finishes */ 2714a32431cSdrh u8 hasPrimKey; /* True if there exists a primary key */ 2729cfcf5d4Sdrh u8 keyConf; /* What to do in case of uniqueness conflict on iPKey */ 27375897234Sdrh }; 27475897234Sdrh 27575897234Sdrh /* 27622f70c32Sdrh ** SQLite supports 5 different ways to resolve a contraint 27722f70c32Sdrh ** error. ROLLBACK processing means that a constraint violation 2781c92853dSdrh ** causes the operation in proces to fail and for the current transaction 2791c92853dSdrh ** to be rolled back. ABORT processing means the operation in process 2801c92853dSdrh ** fails and any prior changes from that one operation are backed out, 2811c92853dSdrh ** but the transaction is not rolled back. FAIL processing means that 2821c92853dSdrh ** the operation in progress stops and returns an error code. But prior 2831c92853dSdrh ** changes due to the same operation are not backed out and no rollback 2841c92853dSdrh ** occurs. IGNORE means that the particular row that caused the constraint 2851c92853dSdrh ** error is not inserted or updated. Processing continues and no error 2861c92853dSdrh ** is returned. REPLACE means that preexisting database rows that caused 2871c92853dSdrh ** a UNIQUE constraint violation are removed so that the new insert or 2881c92853dSdrh ** update can proceed. Processing continues and no error is reported. 2891c92853dSdrh ** 2901c92853dSdrh ** The following there symbolic values are used to record which type 2911c92853dSdrh ** of action to take. 2929cfcf5d4Sdrh */ 2939cfcf5d4Sdrh #define OE_None 0 /* There is no constraint to check */ 2941c92853dSdrh #define OE_Rollback 1 /* Fail the operation and rollback the transaction */ 2951c92853dSdrh #define OE_Abort 2 /* Back out changes but do no rollback transaction */ 2961c92853dSdrh #define OE_Fail 3 /* Stop the operation but leave all prior changes */ 2971c92853dSdrh #define OE_Ignore 4 /* Ignore the error. Do not do the INSERT or UPDATE */ 2981c92853dSdrh #define OE_Replace 5 /* Delete existing record, then do INSERT or UPDATE */ 2999cfcf5d4Sdrh #define OE_Default 9 /* Do whatever the default action is */ 3009cfcf5d4Sdrh 3019cfcf5d4Sdrh /* 30266b89c8fSdrh ** Each SQL index is represented in memory by an 30375897234Sdrh ** instance of the following structure. 304967e8b73Sdrh ** 305967e8b73Sdrh ** The columns of the table that are to be indexed are described 306967e8b73Sdrh ** by the aiColumn[] field of this structure. For example, suppose 307967e8b73Sdrh ** we have the following table and index: 308967e8b73Sdrh ** 309967e8b73Sdrh ** CREATE TABLE Ex1(c1 int, c2 int, c3 text); 310967e8b73Sdrh ** CREATE INDEX Ex2 ON Ex1(c3,c1); 311967e8b73Sdrh ** 312967e8b73Sdrh ** In the Table structure describing Ex1, nCol==3 because there are 313967e8b73Sdrh ** three columns in the table. In the Index structure describing 314967e8b73Sdrh ** Ex2, nColumn==2 since 2 of the 3 columns of Ex1 are indexed. 315967e8b73Sdrh ** The value of aiColumn is {2, 0}. aiColumn[0]==2 because the 316967e8b73Sdrh ** first column to be indexed (c3) has an index of 2 in Ex1.aCol[]. 317967e8b73Sdrh ** The second column to be indexed (c1) has an index of 0 in 318967e8b73Sdrh ** Ex1.aCol[], hence Ex2.aiColumn[1]==0. 31975897234Sdrh */ 32075897234Sdrh struct Index { 32175897234Sdrh char *zName; /* Name of this index */ 322967e8b73Sdrh int nColumn; /* Number of columns in the table used by this index */ 323967e8b73Sdrh int *aiColumn; /* Which columns are used by this index. 1st is 0 */ 324967e8b73Sdrh Table *pTable; /* The SQL table being indexed */ 325be0072d2Sdrh int tnum; /* Page containing root of this index in database file */ 3269cfcf5d4Sdrh u8 isUnique; /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */ 327717e6402Sdrh u8 isCommit; /* True if creation of this index has been committed */ 32874e24cd0Sdrh u8 isDropped; /* True if a DROP INDEX has executed on this index */ 3299cfcf5d4Sdrh u8 onError; /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */ 33075897234Sdrh Index *pNext; /* The next index associated with the same table */ 33175897234Sdrh }; 33275897234Sdrh 33375897234Sdrh /* 33475897234Sdrh ** Each token coming out of the lexer is an instance of 33575897234Sdrh ** this structure. 33675897234Sdrh */ 33775897234Sdrh struct Token { 33880ff32f5Sdrh const char *z; /* Text of the token. Not NULL-terminated! */ 33975897234Sdrh int n; /* Number of characters in this token */ 34075897234Sdrh }; 34175897234Sdrh 34275897234Sdrh /* 34375897234Sdrh ** Each node of an expression in the parse tree is an instance 34422f70c32Sdrh ** of this structure. 34522f70c32Sdrh ** 34622f70c32Sdrh ** Expr.op is the opcode. The integer parser token codes are reused 34722f70c32Sdrh ** as opcodes here. For example, the parser defines TK_GE to be an integer 34822f70c32Sdrh ** code representing the ">=" operator. This same integer code is reused 34922f70c32Sdrh ** to represent the greater-than-or-equal-to operator in the expression 35022f70c32Sdrh ** tree. 35122f70c32Sdrh ** 35222f70c32Sdrh ** Expr.pRight and Expr.pLeft are subexpressions. Expr.pList is a list 35322f70c32Sdrh ** of argument if the expression is a function. 35422f70c32Sdrh ** 35522f70c32Sdrh ** Expr.token is the operator token for this node. Expr.span is the complete 35622f70c32Sdrh ** subexpression represented by this node and all its decendents. These 35722f70c32Sdrh ** fields are used for error reporting and for reconstructing the text of 35822f70c32Sdrh ** an expression to use as the column name in a SELECT statement. 35922f70c32Sdrh ** 36022f70c32Sdrh ** An expression of the form ID or ID.ID refers to a column in a table. 36122f70c32Sdrh ** For such expressions, Expr.op is set to TK_COLUMN and Expr.iTable is 36222f70c32Sdrh ** the integer cursor number of a VDBE cursor pointing to that table and 36322f70c32Sdrh ** Expr.iColumn is the column number for the specific column. If the 36422f70c32Sdrh ** expression is used as a result in an aggregate SELECT, then the 36522f70c32Sdrh ** value is also stored in the Expr.iAgg column in the aggregate so that 36622f70c32Sdrh ** it can be accessed after all aggregates are computed. 36722f70c32Sdrh ** 36822f70c32Sdrh ** If the expression is a function, the Expr.iTable is an integer code 36922f70c32Sdrh ** representing which function. 37022f70c32Sdrh ** 37122f70c32Sdrh ** The Expr.pSelect field points to a SELECT statement. The SELECT might 37222f70c32Sdrh ** be the right operand of an IN operator. Or, if a scalar SELECT appears 37322f70c32Sdrh ** in an expression the opcode is TK_SELECT and Expr.pSelect is the only 37422f70c32Sdrh ** operand. 37575897234Sdrh */ 37675897234Sdrh struct Expr { 37775897234Sdrh int op; /* Operation performed by this node */ 37875897234Sdrh Expr *pLeft, *pRight; /* Left and right subnodes */ 37975897234Sdrh ExprList *pList; /* A list of expressions used as a function argument */ 38075897234Sdrh Token token; /* An operand token */ 381e1b6a5b8Sdrh Token span; /* Complete text of the expression */ 382967e8b73Sdrh int iTable, iColumn; /* When op==TK_COLUMN, then this expr node means the 383967e8b73Sdrh ** iColumn-th field of the iTable-th table. When 384967e8b73Sdrh ** op==TK_FUNCTION, iColumn holds the function id */ 385967e8b73Sdrh int iAgg; /* When op==TK_COLUMN and pParse->useAgg==TRUE, pull 386967e8b73Sdrh ** result from the iAgg-th element of the aggregator */ 38719a775c2Sdrh Select *pSelect; /* When the expression is a sub-select */ 38875897234Sdrh }; 38975897234Sdrh 39075897234Sdrh /* 39175897234Sdrh ** A list of expressions. Each expression may optionally have a 39275897234Sdrh ** name. An expr/name combination can be used in several ways, such 39375897234Sdrh ** as the list of "expr AS ID" fields following a "SELECT" or in the 39475897234Sdrh ** list of "ID = expr" items in an UPDATE. A list of expressions can 39575897234Sdrh ** also be used as the argument to a function, in which case the azName 39675897234Sdrh ** field is not used. 39775897234Sdrh */ 39875897234Sdrh struct ExprList { 39975897234Sdrh int nExpr; /* Number of expressions on the list */ 4006d4abfbeSdrh struct ExprList_item { 40175897234Sdrh Expr *pExpr; /* The list of expressions */ 40275897234Sdrh char *zName; /* Token associated with this expression */ 403d8bc7086Sdrh char sortOrder; /* 1 for DESC or 0 for ASC */ 404d8bc7086Sdrh char isAgg; /* True if this is an aggregate like count(*) */ 405d8bc7086Sdrh char done; /* A flag to indicate when processing is finished */ 40675897234Sdrh } *a; /* One entry for each expression */ 40775897234Sdrh }; 40875897234Sdrh 40975897234Sdrh /* 41075897234Sdrh ** A list of identifiers. 41175897234Sdrh */ 41275897234Sdrh struct IdList { 41375897234Sdrh int nId; /* Number of identifiers on the list */ 4146d4abfbeSdrh struct IdList_item { 41575897234Sdrh char *zName; /* Text of the identifier. */ 41675897234Sdrh char *zAlias; /* The "B" part of a "A AS B" phrase. zName is the "A" */ 417967e8b73Sdrh int idx; /* Index in some Table.aCol[] of a column named zName */ 418daffd0e5Sdrh Table *pTab; /* An SQL table corresponding to zName */ 419daffd0e5Sdrh Select *pSelect; /* A SELECT statement used in place of a table name */ 42075897234Sdrh } *a; /* One entry for each identifier on the list */ 42175897234Sdrh }; 42275897234Sdrh 42375897234Sdrh /* 4246b56344dSdrh ** For each nested loop in a WHERE clause implementation, the WhereInfo 4256b56344dSdrh ** structure contains a single instance of this structure. This structure 4266b56344dSdrh ** is intended to be private the the where.c module and should not be 4276b56344dSdrh ** access or modified by other modules. 4286b56344dSdrh */ 4296b56344dSdrh struct WhereLevel { 4306b56344dSdrh int iMem; /* Memory cell used by this level */ 4316b56344dSdrh Index *pIdx; /* Index used */ 4326b56344dSdrh int iCur; /* Cursor number used for this index */ 433487ab3caSdrh int score; /* How well this indexed scored */ 4346b56344dSdrh int brk; /* Jump here to break out of the loop */ 4356b56344dSdrh int cont; /* Jump here to continue with the next loop cycle */ 4366b56344dSdrh int op, p1, p2; /* Opcode used to terminate the loop */ 4376b56344dSdrh }; 4386b56344dSdrh 4396b56344dSdrh /* 44075897234Sdrh ** The WHERE clause processing routine has two halves. The 44175897234Sdrh ** first part does the start of the WHERE loop and the second 44275897234Sdrh ** half does the tail of the WHERE loop. An instance of 44375897234Sdrh ** this structure is returned by the first half and passed 44475897234Sdrh ** into the second half to give some continuity. 44575897234Sdrh */ 44675897234Sdrh struct WhereInfo { 44775897234Sdrh Parse *pParse; 44819a775c2Sdrh IdList *pTabList; /* List of tables in the join */ 44919a775c2Sdrh int iContinue; /* Jump here to continue with next record */ 45019a775c2Sdrh int iBreak; /* Jump here to break out of the loop */ 45119a775c2Sdrh int base; /* Index of first Open opcode */ 4526b56344dSdrh int nLevel; /* Number of nested loop */ 453832508b7Sdrh int savedNTab; /* Value of pParse->nTab before WhereBegin() */ 454832508b7Sdrh int peakNTab; /* Value of pParse->nTab after WhereBegin() */ 4556b56344dSdrh WhereLevel a[1]; /* Information about each nest loop in the WHERE */ 45675897234Sdrh }; 45775897234Sdrh 45875897234Sdrh /* 4599bb61fe7Sdrh ** An instance of the following structure contains all information 4609bb61fe7Sdrh ** needed to generate code for a single SELECT statement. 461a76b5dfcSdrh ** 462a76b5dfcSdrh ** The zSelect field is used when the Select structure must be persistent. 463a76b5dfcSdrh ** Normally, the expression tree points to tokens in the original input 464a76b5dfcSdrh ** string that encodes the select. But if the Select structure must live 465a76b5dfcSdrh ** longer than its input string (for example when it is used to describe 466a76b5dfcSdrh ** a VIEW) we have to make a copy of the input string so that the nodes 467a76b5dfcSdrh ** of the expression tree will have something to point to. zSelect is used 468a76b5dfcSdrh ** to hold that copy. 4699bb61fe7Sdrh */ 4709bb61fe7Sdrh struct Select { 4719bb61fe7Sdrh int isDistinct; /* True if the DISTINCT keyword is present */ 4729bb61fe7Sdrh ExprList *pEList; /* The fields of the result */ 4739bb61fe7Sdrh IdList *pSrc; /* The FROM clause */ 4749bb61fe7Sdrh Expr *pWhere; /* The WHERE clause */ 4759bb61fe7Sdrh ExprList *pGroupBy; /* The GROUP BY clause */ 4769bb61fe7Sdrh Expr *pHaving; /* The HAVING clause */ 4779bb61fe7Sdrh ExprList *pOrderBy; /* The ORDER BY clause */ 47882c3d636Sdrh int op; /* One of: TK_UNION TK_ALL TK_INTERSECT TK_EXCEPT */ 479967e8b73Sdrh Select *pPrior; /* Prior select in a compound select statement */ 4809bbca4c1Sdrh int nLimit, nOffset; /* LIMIT and OFFSET values. -1 means not used */ 481a76b5dfcSdrh char *zSelect; /* Complete text of the SELECT command */ 482832508b7Sdrh int base; /* Index of VDBE cursor for left-most FROM table */ 4839bb61fe7Sdrh }; 4849bb61fe7Sdrh 4859bb61fe7Sdrh /* 486fef5208cSdrh ** The results of a select can be distributed in several ways. 487fef5208cSdrh */ 488fef5208cSdrh #define SRT_Callback 1 /* Invoke a callback with each row of result */ 489fef5208cSdrh #define SRT_Mem 2 /* Store result in a memory cell */ 49082c3d636Sdrh #define SRT_Set 3 /* Store result as unique keys in a table */ 49182c3d636Sdrh #define SRT_Union 5 /* Store result as keys in a table */ 49282c3d636Sdrh #define SRT_Except 6 /* Remove result from a UNION table */ 4935974a30fSdrh #define SRT_Table 7 /* Store result as data with a unique key */ 4942d0794e3Sdrh #define SRT_TempTable 8 /* Store result in a trasient table */ 495fef5208cSdrh 496fef5208cSdrh /* 4972282792aSdrh ** When a SELECT uses aggregate functions (like "count(*)" or "avg(f1)") 4982282792aSdrh ** we have to do some additional analysis of expressions. An instance 4992282792aSdrh ** of the following structure holds information about a single subexpression 5002282792aSdrh ** somewhere in the SELECT statement. An array of these structures holds 5012282792aSdrh ** all the information we need to generate code for aggregate 5022282792aSdrh ** expressions. 5032282792aSdrh ** 5042282792aSdrh ** Note that when analyzing a SELECT containing aggregates, both 5052282792aSdrh ** non-aggregate field variables and aggregate functions are stored 5062282792aSdrh ** in the AggExpr array of the Parser structure. 5072282792aSdrh ** 5082282792aSdrh ** The pExpr field points to an expression that is part of either the 5092282792aSdrh ** field list, the GROUP BY clause, the HAVING clause or the ORDER BY 5102282792aSdrh ** clause. The expression will be freed when those clauses are cleaned 5112282792aSdrh ** up. Do not try to delete the expression attached to AggExpr.pExpr. 5122282792aSdrh ** 5132282792aSdrh ** If AggExpr.pExpr==0, that means the expression is "count(*)". 5142282792aSdrh */ 5152282792aSdrh struct AggExpr { 5162282792aSdrh int isAgg; /* if TRUE contains an aggregate function */ 5172282792aSdrh Expr *pExpr; /* The expression */ 5180bce8354Sdrh FuncDef *pFunc; /* Information about the aggregate function */ 5192282792aSdrh }; 5202282792aSdrh 5212282792aSdrh /* 522f57b3399Sdrh ** An SQL parser context. A copy of this structure is passed through 523f57b3399Sdrh ** the parser and down into all the parser action routine in order to 524f57b3399Sdrh ** carry around information that is global to the entire parse. 52575897234Sdrh */ 52675897234Sdrh struct Parse { 52775897234Sdrh sqlite *db; /* The main database structure */ 5285e00f6c7Sdrh Btree *pBe; /* The database backend */ 5294c504391Sdrh int rc; /* Return code from execution */ 53075897234Sdrh sqlite_callback xCallback; /* The callback function */ 53175897234Sdrh void *pArg; /* First argument to the callback function */ 53275897234Sdrh char *zErrMsg; /* An error message */ 53375897234Sdrh Token sErrToken; /* The token at which the error occurred */ 53475897234Sdrh Token sFirstToken; /* The first token parsed */ 53575897234Sdrh Token sLastToken; /* The last token parsed */ 53675897234Sdrh Table *pNewTable; /* A table being constructed by CREATE TABLE */ 53775897234Sdrh Vdbe *pVdbe; /* An engine for executing database bytecode */ 538d8bc7086Sdrh int colNamesSet; /* TRUE after OP_ColumnCount has been issued to pVdbe */ 53975897234Sdrh int explain; /* True if the EXPLAIN flag is found on the query */ 54075897234Sdrh int initFlag; /* True if reparsing CREATE TABLEs */ 541f57b3399Sdrh int nameClash; /* A permanent table name clashes with temp table name */ 542d78eeee1Sdrh int newTnum; /* Table number to use when reparsing CREATE TABLEs */ 54375897234Sdrh int nErr; /* Number of errors seen */ 544832508b7Sdrh int nTab; /* Number of previously allocated VDBE cursors */ 54519a775c2Sdrh int nMem; /* Number of memory cells used so far */ 546fef5208cSdrh int nSet; /* Number of sets used so far */ 5472282792aSdrh int nAgg; /* Number of aggregate expressions */ 5482282792aSdrh AggExpr *aAgg; /* An array of aggregate expressions */ 5492282792aSdrh int useAgg; /* If true, extract field values from the aggregator 5502282792aSdrh ** while generating expressions. Normally false */ 55150e5dadfSdrh int schemaVerified; /* True if an OP_VerifySchema has been coded someplace 55250e5dadfSdrh ** other than after an OP_Transaction */ 55375897234Sdrh }; 55475897234Sdrh 55575897234Sdrh /* 55675897234Sdrh ** Internal function prototypes 55775897234Sdrh */ 55875897234Sdrh int sqliteStrICmp(const char *, const char *); 55975897234Sdrh int sqliteStrNICmp(const char *, const char *, int); 56075897234Sdrh int sqliteHashNoCase(const char *, int); 56175897234Sdrh int sqliteCompare(const char *, const char *); 56275897234Sdrh int sqliteSortCompare(const char *, const char *); 5639bbca4c1Sdrh void sqliteRealToSortable(double r, char *); 564dcc581ccSdrh #ifdef MEMORY_DEBUG 565dcc581ccSdrh void *sqliteMalloc_(int,char*,int); 566dcc581ccSdrh void sqliteFree_(void*,char*,int); 567dcc581ccSdrh void *sqliteRealloc_(void*,int,char*,int); 5686e142f54Sdrh char *sqliteStrDup_(const char*,char*,int); 5696e142f54Sdrh char *sqliteStrNDup_(const char*, int,char*,int); 570dcc581ccSdrh #else 57175897234Sdrh void *sqliteMalloc(int); 57275897234Sdrh void sqliteFree(void*); 57375897234Sdrh void *sqliteRealloc(void*,int); 5746e142f54Sdrh char *sqliteStrDup(const char*); 5756e142f54Sdrh char *sqliteStrNDup(const char*, int); 576dcc581ccSdrh #endif 57775897234Sdrh void sqliteSetString(char **, const char *, ...); 57875897234Sdrh void sqliteSetNString(char **, ...); 579982cef7eSdrh void sqliteDequote(char*); 58017f71934Sdrh int sqliteKeywordCode(const char*, int); 58180ff32f5Sdrh int sqliteRunParser(Parse*, const char*, char **); 58275897234Sdrh void sqliteExec(Parse*); 58375897234Sdrh Expr *sqliteExpr(int, Expr*, Expr*, Token*); 584e1b6a5b8Sdrh void sqliteExprSpan(Expr*,Token*,Token*); 58575897234Sdrh Expr *sqliteExprFunction(ExprList*, Token*); 58675897234Sdrh void sqliteExprDelete(Expr*); 58775897234Sdrh ExprList *sqliteExprListAppend(ExprList*,Expr*,Token*); 58875897234Sdrh void sqliteExprListDelete(ExprList*); 589f57b14a6Sdrh void sqlitePragma(Parse*,Token*,Token*,int); 5905e00f6c7Sdrh void sqliteCommitInternalChanges(sqlite*); 5915e00f6c7Sdrh void sqliteRollbackInternalChanges(sqlite*); 592969fa7c1Sdrh Table *sqliteResultSetOfSelect(Parse*,char*,Select*); 593f57b3399Sdrh void sqliteStartTable(Parse*,Token*,Token*,int); 59475897234Sdrh void sqliteAddColumn(Parse*,Token*); 5959cfcf5d4Sdrh void sqliteAddNotNull(Parse*, int); 5969cfcf5d4Sdrh void sqliteAddPrimaryKey(Parse*, IdList*, int); 597382c0247Sdrh void sqliteAddColumnType(Parse*,Token*,Token*); 5987020f651Sdrh void sqliteAddDefaultValue(Parse*,Token*,int); 599969fa7c1Sdrh void sqliteEndTable(Parse*,Token*,Select*); 600a76b5dfcSdrh void sqliteCreateView(Parse*,Token*,Token*,Select*); 601417be79cSdrh int sqliteViewGetColumnNames(Parse*,Table*); 602417be79cSdrh void sqliteViewResetAll(sqlite*); 6034ff6dfa7Sdrh void sqliteDropTable(Parse*, Token*, int); 60475897234Sdrh void sqliteDeleteTable(sqlite*, Table*); 6059cfcf5d4Sdrh void sqliteInsert(Parse*, Token*, ExprList*, Select*, IdList*, int); 60675897234Sdrh IdList *sqliteIdListAppend(IdList*, Token*); 60775897234Sdrh void sqliteIdListAddAlias(IdList*, Token*); 60875897234Sdrh void sqliteIdListDelete(IdList*); 609717e6402Sdrh void sqliteCreateIndex(Parse*, Token*, Token*, IdList*, int, Token*, Token*); 61075897234Sdrh void sqliteDropIndex(Parse*, Token*); 6111b2e0329Sdrh int sqliteSelect(Parse*, Select*, int, int, Select*, int, int*); 6129bbca4c1Sdrh Select *sqliteSelectNew(ExprList*,IdList*,Expr*,ExprList*,Expr*,ExprList*, 6139bbca4c1Sdrh int,int,int); 6149bb61fe7Sdrh void sqliteSelectDelete(Select*); 615ff78bd2fSdrh void sqliteSelectUnbind(Select*); 616a76b5dfcSdrh Table *sqliteTableNameToTable(Parse*, const char*); 617a76b5dfcSdrh IdList *sqliteTableTokenToIdList(Parse*, Token*); 61875897234Sdrh void sqliteDeleteFrom(Parse*, Token*, Expr*); 6199cfcf5d4Sdrh void sqliteUpdate(Parse*, Token*, ExprList*, Expr*, int); 620832508b7Sdrh WhereInfo *sqliteWhereBegin(Parse*, int, IdList*, Expr*, int); 62175897234Sdrh void sqliteWhereEnd(WhereInfo*); 62275897234Sdrh void sqliteExprCode(Parse*, Expr*); 62375897234Sdrh void sqliteExprIfTrue(Parse*, Expr*, int); 62475897234Sdrh void sqliteExprIfFalse(Parse*, Expr*, int); 625a76b5dfcSdrh Table *sqliteFindTable(sqlite*,const char*); 626a76b5dfcSdrh Index *sqliteFindIndex(sqlite*,const char*); 6276d4abfbeSdrh void sqliteUnlinkAndDeleteIndex(sqlite*,Index*); 628b419a926Sdrh void sqliteCopy(Parse*, Token*, Token*, Token*, int); 629dce2cbe6Sdrh void sqliteVacuum(Parse*, Token*); 630e17a7e33Sdrh int sqliteGlobCompare(const unsigned char*,const unsigned char*); 631dce2cbe6Sdrh int sqliteLikeCompare(const unsigned char*,const unsigned char*); 632cce7d176Sdrh char *sqliteTableNameFromToken(Token*); 633cce7d176Sdrh int sqliteExprCheck(Parse*, Expr*, int, int*); 634d8bc7086Sdrh int sqliteExprCompare(Expr*, Expr*); 635cce7d176Sdrh int sqliteFuncId(Token*); 636832508b7Sdrh int sqliteExprResolveIds(Parse*, int, IdList*, ExprList*, Expr*); 6372282792aSdrh int sqliteExprAnalyzeAggregates(Parse*, Expr*); 638d8bc7086Sdrh Vdbe *sqliteGetVdbe(Parse*); 639b8ca307eSdrh int sqliteRandomByte(void); 640b8ca307eSdrh int sqliteRandomInteger(void); 6411c92853dSdrh void sqliteBeginTransaction(Parse*, int); 642c4a3c779Sdrh void sqliteCommitTransaction(Parse*); 643c4a3c779Sdrh void sqliteRollbackTransaction(Parse*); 644d1bf3512Sdrh char *sqlite_mprintf(const char *, ...); 6459208643dSdrh int sqliteExprIsConstant(Expr*); 646c8d30ac1Sdrh void sqliteGenerateRowDelete(Vdbe*, Table*, int, int); 6470ca3e24bSdrh void sqliteGenerateRowIndexDelete(Vdbe*, Table*, int, char*); 6480ca3e24bSdrh void sqliteGenerateConstraintChecks(Parse*,Table*,int,char*,int,int,int,int); 649b419a926Sdrh void sqliteCompleteInsertion(Parse*, Table*, int, char*, int, int); 6501c92853dSdrh void sqliteBeginWriteOperation(Parse*); 651663fc63aSdrh void sqliteBeginMultiWriteOperation(Parse*); 6521c92853dSdrh void sqliteEndWriteOperation(Parse*); 653a76b5dfcSdrh void sqliteExprMoveStrings(Expr*, int); 654a76b5dfcSdrh void sqliteExprListMoveStrings(ExprList*, int); 655a76b5dfcSdrh void sqliteSelectMoveStrings(Select*, int); 656ff78bd2fSdrh Expr *sqliteExprDup(Expr*); 657ff78bd2fSdrh ExprList *sqliteExprListDup(ExprList*); 658ff78bd2fSdrh IdList *sqliteIdListDup(IdList*); 659ff78bd2fSdrh Select *sqliteSelectDup(Select*); 6600bce8354Sdrh FuncDef *sqliteFindFunction(sqlite*,const char*,int,int,int); 661dc04c583Sdrh void sqliteRegisterBuildinFunctions(sqlite*); 662247be43dSdrh int sqliteSafetyOn(sqlite*); 663247be43dSdrh int sqliteSafetyOff(sqlite*); 664*c22bd47dSdrh int sqliteSafetyCheck(sqlite*); 665