dvander@1203: /* dvander@1203: ** 2001 September 15 dvander@1203: ** dvander@1203: ** The author disclaims copyright to this source code. In place of dvander@1203: ** a legal notice, here is a blessing: dvander@1203: ** dvander@1203: ** May you do good and not evil. dvander@1203: ** May you find forgiveness for yourself and forgive others. dvander@1203: ** May you share freely, never taking more than you give. dvander@1203: ** dvander@1203: ************************************************************************* dvander@1203: ** Internal interface definitions for SQLite. dvander@1203: ** dvander@1203: ** @(#) $Id$ dvander@1203: */ dvander@1203: #ifndef _SQLITEINT_H_ dvander@1203: #define _SQLITEINT_H_ dvander@1203: #include "sqliteLimit.h" dvander@1203: dvander@1203: dvander@1203: #if defined(SQLITE_TCL) || defined(TCLSH) dvander@1203: # include dvander@1203: #endif dvander@1203: dvander@1203: /* dvander@1203: ** Many people are failing to set -DNDEBUG=1 when compiling SQLite. dvander@1203: ** Setting NDEBUG makes the code smaller and run faster. So the following dvander@1203: ** lines are added to automatically set NDEBUG unless the -DSQLITE_DEBUG=1 dvander@1203: ** option is set. Thus NDEBUG becomes an opt-in rather than an opt-out dvander@1203: ** feature. dvander@1203: */ dvander@1203: #if !defined(NDEBUG) && !defined(SQLITE_DEBUG) dvander@1203: # define NDEBUG 1 dvander@1203: #endif dvander@1203: dvander@1203: /* dvander@1203: ** These #defines should enable >2GB file support on Posix if the dvander@1203: ** underlying operating system supports it. If the OS lacks dvander@1203: ** large file support, or if the OS is windows, these should be no-ops. dvander@1203: ** dvander@1203: ** Large file support can be disabled using the -DSQLITE_DISABLE_LFS switch dvander@1203: ** on the compiler command line. This is necessary if you are compiling dvander@1203: ** on a recent machine (ex: RedHat 7.2) but you want your code to work dvander@1203: ** on an older machine (ex: RedHat 6.0). If you compile on RedHat 7.2 dvander@1203: ** without this option, LFS is enable. But LFS does not exist in the kernel dvander@1203: ** in RedHat 6.0, so the code won't work. Hence, for maximum binary dvander@1203: ** portability you should omit LFS. dvander@1203: ** dvander@1203: ** Similar is true for MacOS. LFS is only supported on MacOS 9 and later. dvander@1203: */ dvander@1203: #ifndef SQLITE_DISABLE_LFS dvander@1203: # define _LARGE_FILE 1 dvander@1203: # ifndef _FILE_OFFSET_BITS dvander@1203: # define _FILE_OFFSET_BITS 64 dvander@1203: # endif dvander@1203: # define _LARGEFILE_SOURCE 1 dvander@1203: #endif dvander@1203: dvander@1203: #include "sqlite3.h" dvander@1203: #include "hash.h" dvander@1203: #include "parse.h" dvander@1203: #include dvander@1203: #include dvander@1203: #include dvander@1203: #include dvander@1203: #include dvander@1203: dvander@1203: #define sqlite3_isnan(X) ((X)!=(X)) dvander@1203: dvander@1203: /* dvander@1203: ** If compiling for a processor that lacks floating point support, dvander@1203: ** substitute integer for floating-point dvander@1203: */ dvander@1203: #ifdef SQLITE_OMIT_FLOATING_POINT dvander@1203: # define double sqlite_int64 dvander@1203: # define LONGDOUBLE_TYPE sqlite_int64 dvander@1203: # ifndef SQLITE_BIG_DBL dvander@1203: # define SQLITE_BIG_DBL (0x7fffffffffffffff) dvander@1203: # endif dvander@1203: # define SQLITE_OMIT_DATETIME_FUNCS 1 dvander@1203: # define SQLITE_OMIT_TRACE 1 dvander@1203: # undef SQLITE_MIXED_ENDIAN_64BIT_FLOAT dvander@1203: #endif dvander@1203: #ifndef SQLITE_BIG_DBL dvander@1203: # define SQLITE_BIG_DBL (1e99) dvander@1203: #endif dvander@1203: dvander@1203: /* dvander@1203: ** OMIT_TEMPDB is set to 1 if SQLITE_OMIT_TEMPDB is defined, or 0 dvander@1203: ** afterward. Having this macro allows us to cause the C compiler dvander@1203: ** to omit code used by TEMP tables without messy #ifndef statements. dvander@1203: */ dvander@1203: #ifdef SQLITE_OMIT_TEMPDB dvander@1203: #define OMIT_TEMPDB 1 dvander@1203: #else dvander@1203: #define OMIT_TEMPDB 0 dvander@1203: #endif dvander@1203: dvander@1203: /* dvander@1203: ** If the following macro is set to 1, then NULL values are considered dvander@1203: ** distinct when determining whether or not two entries are the same dvander@1203: ** in a UNIQUE index. This is the way PostgreSQL, Oracle, DB2, MySQL, dvander@1203: ** OCELOT, and Firebird all work. The SQL92 spec explicitly says this dvander@1203: ** is the way things are suppose to work. dvander@1203: ** dvander@1203: ** If the following macro is set to 0, the NULLs are indistinct for dvander@1203: ** a UNIQUE index. In this mode, you can only have a single NULL entry dvander@1203: ** for a column declared UNIQUE. This is the way Informix and SQL Server dvander@1203: ** work. dvander@1203: */ dvander@1203: #define NULL_DISTINCT_FOR_UNIQUE 1 dvander@1203: dvander@1203: /* dvander@1203: ** The "file format" number is an integer that is incremented whenever dvander@1203: ** the VDBE-level file format changes. The following macros define the dvander@1203: ** the default file format for new databases and the maximum file format dvander@1203: ** that the library can read. dvander@1203: */ dvander@1203: #define SQLITE_MAX_FILE_FORMAT 4 dvander@1203: #ifndef SQLITE_DEFAULT_FILE_FORMAT dvander@1203: # define SQLITE_DEFAULT_FILE_FORMAT 1 dvander@1203: #endif dvander@1203: dvander@1203: /* dvander@1203: ** Provide a default value for TEMP_STORE in case it is not specified dvander@1203: ** on the command-line dvander@1203: */ dvander@1203: #ifndef TEMP_STORE dvander@1203: # define TEMP_STORE 1 dvander@1203: #endif dvander@1203: dvander@1203: /* dvander@1203: ** GCC does not define the offsetof() macro so we'll have to do it dvander@1203: ** ourselves. dvander@1203: */ dvander@1203: #ifndef offsetof dvander@1203: #define offsetof(STRUCTURE,FIELD) ((int)((char*)&((STRUCTURE*)0)->FIELD)) dvander@1203: #endif dvander@1203: dvander@1203: /* dvander@1203: ** Check to see if this machine uses EBCDIC. (Yes, believe it or dvander@1203: ** not, there are still machines out there that use EBCDIC.) dvander@1203: */ dvander@1203: #if 'A' == '\301' dvander@1203: # define SQLITE_EBCDIC 1 dvander@1203: #else dvander@1203: # define SQLITE_ASCII 1 dvander@1203: #endif dvander@1203: dvander@1203: /* dvander@1203: ** Integers of known sizes. These typedefs might change for architectures dvander@1203: ** where the sizes very. Preprocessor macros are available so that the dvander@1203: ** types can be conveniently redefined at compile-type. Like this: dvander@1203: ** dvander@1203: ** cc '-DUINTPTR_TYPE=long long int' ... dvander@1203: */ dvander@1203: #ifndef UINT32_TYPE dvander@1203: # define UINT32_TYPE unsigned int dvander@1203: #endif dvander@1203: #ifndef UINT16_TYPE dvander@1203: # define UINT16_TYPE unsigned short int dvander@1203: #endif dvander@1203: #ifndef INT16_TYPE dvander@1203: # define INT16_TYPE short int dvander@1203: #endif dvander@1203: #ifndef UINT8_TYPE dvander@1203: # define UINT8_TYPE unsigned char dvander@1203: #endif dvander@1203: #ifndef INT8_TYPE dvander@1203: # define INT8_TYPE signed char dvander@1203: #endif dvander@1203: #ifndef LONGDOUBLE_TYPE dvander@1203: # define LONGDOUBLE_TYPE long double dvander@1203: #endif dvander@1203: typedef sqlite_int64 i64; /* 8-byte signed integer */ dvander@1203: typedef sqlite_uint64 u64; /* 8-byte unsigned integer */ dvander@1203: typedef UINT32_TYPE u32; /* 4-byte unsigned integer */ dvander@1203: typedef UINT16_TYPE u16; /* 2-byte unsigned integer */ dvander@1203: typedef INT16_TYPE i16; /* 2-byte signed integer */ dvander@1203: typedef UINT8_TYPE u8; /* 1-byte unsigned integer */ dvander@1203: typedef UINT8_TYPE i8; /* 1-byte signed integer */ dvander@1203: dvander@1203: /* dvander@1203: ** Macros to determine whether the machine is big or little endian, dvander@1203: ** evaluated at runtime. dvander@1203: */ dvander@1203: extern const int sqlite3one; dvander@1203: #if defined(i386) || defined(__i386__) || defined(_M_IX86) dvander@1203: # define SQLITE_BIGENDIAN 0 dvander@1203: # define SQLITE_LITTLEENDIAN 1 dvander@1203: # define SQLITE_UTF16NATIVE SQLITE_UTF16LE dvander@1203: #else dvander@1203: # define SQLITE_BIGENDIAN (*(char *)(&sqlite3one)==0) dvander@1203: # define SQLITE_LITTLEENDIAN (*(char *)(&sqlite3one)==1) dvander@1203: # define SQLITE_UTF16NATIVE (SQLITE_BIGENDIAN?SQLITE_UTF16BE:SQLITE_UTF16LE) dvander@1203: #endif dvander@1203: dvander@1203: /* dvander@1203: ** An instance of the following structure is used to store the busy-handler dvander@1203: ** callback for a given sqlite handle. dvander@1203: ** dvander@1203: ** The sqlite.busyHandler member of the sqlite struct contains the busy dvander@1203: ** callback for the database handle. Each pager opened via the sqlite dvander@1203: ** handle is passed a pointer to sqlite.busyHandler. The busy-handler dvander@1203: ** callback is currently invoked only from within pager.c. dvander@1203: */ dvander@1203: typedef struct BusyHandler BusyHandler; dvander@1203: struct BusyHandler { dvander@1203: int (*xFunc)(void *,int); /* The busy callback */ dvander@1203: void *pArg; /* First arg to busy callback */ dvander@1203: int nBusy; /* Incremented with each busy call */ dvander@1203: }; dvander@1203: dvander@1203: /* dvander@1203: ** Defer sourcing vdbe.h and btree.h until after the "u8" and dvander@1203: ** "BusyHandler typedefs. dvander@1203: */ dvander@1203: #include "vdbe.h" dvander@1203: #include "btree.h" dvander@1203: #include "pager.h" dvander@1203: dvander@1203: #ifdef SQLITE_MEMDEBUG dvander@1203: /* dvander@1203: ** The following global variables are used for testing and debugging dvander@1203: ** only. They only work if SQLITE_MEMDEBUG is defined. dvander@1203: */ dvander@1203: extern int sqlite3_nMalloc; /* Number of sqliteMalloc() calls */ dvander@1203: extern int sqlite3_nFree; /* Number of sqliteFree() calls */ dvander@1203: extern int sqlite3_iMallocFail; /* Fail sqliteMalloc() after this many calls */ dvander@1203: extern int sqlite3_iMallocReset; /* Set iMallocFail to this when it reaches 0 */ dvander@1203: dvander@1203: extern void *sqlite3_pFirst; /* Pointer to linked list of allocations */ dvander@1203: extern int sqlite3_nMaxAlloc; /* High water mark of ThreadData.nAlloc */ dvander@1203: extern int sqlite3_mallocDisallowed; /* assert() in sqlite3Malloc() if set */ dvander@1203: extern int sqlite3_isFail; /* True if all malloc calls should fail */ dvander@1203: extern const char *sqlite3_zFile; /* Filename to associate debug info with */ dvander@1203: extern int sqlite3_iLine; /* Line number for debug info */ dvander@1203: dvander@1203: #define ENTER_MALLOC (sqlite3_zFile = __FILE__, sqlite3_iLine = __LINE__) dvander@1203: #define sqliteMalloc(x) (ENTER_MALLOC, sqlite3Malloc(x,1)) dvander@1203: #define sqliteMallocRaw(x) (ENTER_MALLOC, sqlite3MallocRaw(x,1)) dvander@1203: #define sqliteRealloc(x,y) (ENTER_MALLOC, sqlite3Realloc(x,y)) dvander@1203: #define sqliteStrDup(x) (ENTER_MALLOC, sqlite3StrDup(x)) dvander@1203: #define sqliteStrNDup(x,y) (ENTER_MALLOC, sqlite3StrNDup(x,y)) dvander@1203: #define sqliteReallocOrFree(x,y) (ENTER_MALLOC, sqlite3ReallocOrFree(x,y)) dvander@1203: dvander@1203: #else dvander@1203: dvander@1203: #define ENTER_MALLOC 0 dvander@1203: #define sqliteMalloc(x) sqlite3Malloc(x,1) dvander@1203: #define sqliteMallocRaw(x) sqlite3MallocRaw(x,1) dvander@1203: #define sqliteRealloc(x,y) sqlite3Realloc(x,y) dvander@1203: #define sqliteStrDup(x) sqlite3StrDup(x) dvander@1203: #define sqliteStrNDup(x,y) sqlite3StrNDup(x,y) dvander@1203: #define sqliteReallocOrFree(x,y) sqlite3ReallocOrFree(x,y) dvander@1203: dvander@1203: #endif dvander@1203: dvander@1203: /* Variable sqlite3_mallocHasFailed is set to true after a malloc() dvander@1203: ** failure occurs. dvander@1203: ** dvander@1203: ** The sqlite3MallocFailed() macro returns true if a malloc has failed dvander@1203: ** in this thread since the last call to sqlite3ApiExit(), or false dvander@1203: ** otherwise. dvander@1203: */ dvander@1203: extern int sqlite3_mallocHasFailed; dvander@1203: #define sqlite3MallocFailed() (sqlite3_mallocHasFailed && sqlite3OsInMutex(1)) dvander@1203: dvander@1203: #define sqliteFree(x) sqlite3FreeX(x) dvander@1203: #define sqliteAllocSize(x) sqlite3AllocSize(x) dvander@1203: dvander@1203: /* dvander@1203: ** An instance of this structure might be allocated to store information dvander@1203: ** specific to a single thread. dvander@1203: */ dvander@1203: struct ThreadData { dvander@1203: int dummy; /* So that this structure is never empty */ dvander@1203: dvander@1203: #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT dvander@1203: int nSoftHeapLimit; /* Suggested max mem allocation. No limit if <0 */ dvander@1203: int nAlloc; /* Number of bytes currently allocated */ dvander@1203: Pager *pPager; /* Linked list of all pagers in this thread */ dvander@1203: #endif dvander@1203: dvander@1203: #ifndef SQLITE_OMIT_SHARED_CACHE dvander@1203: u8 useSharedData; /* True if shared pagers and schemas are enabled */ dvander@1203: BtShared *pBtree; /* Linked list of all currently open BTrees */ dvander@1203: #endif dvander@1203: }; dvander@1203: dvander@1203: /* dvander@1203: ** Name of the master database table. The master database table dvander@1203: ** is a special table that holds the names and attributes of all dvander@1203: ** user tables and indices. dvander@1203: */ dvander@1203: #define MASTER_NAME "sqlite_master" dvander@1203: #define TEMP_MASTER_NAME "sqlite_temp_master" dvander@1203: dvander@1203: /* dvander@1203: ** The root-page of the master database table. dvander@1203: */ dvander@1203: #define MASTER_ROOT 1 dvander@1203: dvander@1203: /* dvander@1203: ** The name of the schema table. dvander@1203: */ dvander@1203: #define SCHEMA_TABLE(x) ((!OMIT_TEMPDB)&&(x==1)?TEMP_MASTER_NAME:MASTER_NAME) dvander@1203: dvander@1203: /* dvander@1203: ** A convenience macro that returns the number of elements in dvander@1203: ** an array. dvander@1203: */ dvander@1203: #define ArraySize(X) (sizeof(X)/sizeof(X[0])) dvander@1203: dvander@1203: /* dvander@1203: ** Forward references to structures dvander@1203: */ dvander@1203: typedef struct AggInfo AggInfo; dvander@1203: typedef struct AuthContext AuthContext; dvander@1203: typedef struct CollSeq CollSeq; dvander@1203: typedef struct Column Column; dvander@1203: typedef struct Db Db; dvander@1203: typedef struct Schema Schema; dvander@1203: typedef struct Expr Expr; dvander@1203: typedef struct ExprList ExprList; dvander@1203: typedef struct FKey FKey; dvander@1203: typedef struct FuncDef FuncDef; dvander@1203: typedef struct IdList IdList; dvander@1203: typedef struct Index Index; dvander@1203: typedef struct KeyClass KeyClass; dvander@1203: typedef struct KeyInfo KeyInfo; dvander@1203: typedef struct Module Module; dvander@1203: typedef struct NameContext NameContext; dvander@1203: typedef struct Parse Parse; dvander@1203: typedef struct Select Select; dvander@1203: typedef struct SrcList SrcList; dvander@1203: typedef struct ThreadData ThreadData; dvander@1203: typedef struct Table Table; dvander@1203: typedef struct TableLock TableLock; dvander@1203: typedef struct Token Token; dvander@1203: typedef struct TriggerStack TriggerStack; dvander@1203: typedef struct TriggerStep TriggerStep; dvander@1203: typedef struct Trigger Trigger; dvander@1203: typedef struct WhereInfo WhereInfo; dvander@1203: typedef struct WhereLevel WhereLevel; dvander@1203: dvander@1203: #include "os.h" dvander@1203: dvander@1203: /* dvander@1203: ** Each database file to be accessed by the system is an instance dvander@1203: ** of the following structure. There are normally two of these structures dvander@1203: ** in the sqlite.aDb[] array. aDb[0] is the main database file and dvander@1203: ** aDb[1] is the database file used to hold temporary tables. Additional dvander@1203: ** databases may be attached. dvander@1203: */ dvander@1203: struct Db { dvander@1203: char *zName; /* Name of this database */ dvander@1203: Btree *pBt; /* The B*Tree structure for this database file */ dvander@1203: u8 inTrans; /* 0: not writable. 1: Transaction. 2: Checkpoint */ dvander@1203: u8 safety_level; /* How aggressive at synching data to disk */ dvander@1203: void *pAux; /* Auxiliary data. Usually NULL */ dvander@1203: void (*xFreeAux)(void*); /* Routine to free pAux */ dvander@1203: Schema *pSchema; /* Pointer to database schema (possibly shared) */ dvander@1203: }; dvander@1203: dvander@1203: /* dvander@1203: ** An instance of the following structure stores a database schema. dvander@1203: ** dvander@1203: ** If there are no virtual tables configured in this schema, the dvander@1203: ** Schema.db variable is set to NULL. After the first virtual table dvander@1203: ** has been added, it is set to point to the database connection dvander@1203: ** used to create the connection. Once a virtual table has been dvander@1203: ** added to the Schema structure and the Schema.db variable populated, dvander@1203: ** only that database connection may use the Schema to prepare dvander@1203: ** statements. dvander@1203: */ dvander@1203: struct Schema { dvander@1203: int schema_cookie; /* Database schema version number for this file */ dvander@1203: Hash tblHash; /* All tables indexed by name */ dvander@1203: Hash idxHash; /* All (named) indices indexed by name */ dvander@1203: Hash trigHash; /* All triggers indexed by name */ dvander@1203: Hash aFKey; /* Foreign keys indexed by to-table */ dvander@1203: Table *pSeqTab; /* The sqlite_sequence table used by AUTOINCREMENT */ dvander@1203: u8 file_format; /* Schema format version for this file */ dvander@1203: u8 enc; /* Text encoding used by this database */ dvander@1203: u16 flags; /* Flags associated with this schema */ dvander@1203: int cache_size; /* Number of pages to use in the cache */ dvander@1203: #ifndef SQLITE_OMIT_VIRTUALTABLE dvander@1203: sqlite3 *db; /* "Owner" connection. See comment above */ dvander@1203: #endif dvander@1203: }; dvander@1203: dvander@1203: /* dvander@1203: ** These macros can be used to test, set, or clear bits in the dvander@1203: ** Db.flags field. dvander@1203: */ dvander@1203: #define DbHasProperty(D,I,P) (((D)->aDb[I].pSchema->flags&(P))==(P)) dvander@1203: #define DbHasAnyProperty(D,I,P) (((D)->aDb[I].pSchema->flags&(P))!=0) dvander@1203: #define DbSetProperty(D,I,P) (D)->aDb[I].pSchema->flags|=(P) dvander@1203: #define DbClearProperty(D,I,P) (D)->aDb[I].pSchema->flags&=~(P) dvander@1203: dvander@1203: /* dvander@1203: ** Allowed values for the DB.flags field. dvander@1203: ** dvander@1203: ** The DB_SchemaLoaded flag is set after the database schema has been dvander@1203: ** read into internal hash tables. dvander@1203: ** dvander@1203: ** DB_UnresetViews means that one or more views have column names that dvander@1203: ** have been filled out. If the schema changes, these column names might dvander@1203: ** changes and so the view will need to be reset. dvander@1203: */ dvander@1203: #define DB_SchemaLoaded 0x0001 /* The schema has been loaded */ dvander@1203: #define DB_UnresetViews 0x0002 /* Some views have defined column names */ dvander@1203: #define DB_Empty 0x0004 /* The file is empty (length 0 bytes) */ dvander@1203: dvander@1203: dvander@1203: /* dvander@1203: ** Each database is an instance of the following structure. dvander@1203: ** dvander@1203: ** The sqlite.lastRowid records the last insert rowid generated by an dvander@1203: ** insert statement. Inserts on views do not affect its value. Each dvander@1203: ** trigger has its own context, so that lastRowid can be updated inside dvander@1203: ** triggers as usual. The previous value will be restored once the trigger dvander@1203: ** exits. Upon entering a before or instead of trigger, lastRowid is no dvander@1203: ** longer (since after version 2.8.12) reset to -1. dvander@1203: ** dvander@1203: ** The sqlite.nChange does not count changes within triggers and keeps no dvander@1203: ** context. It is reset at start of sqlite3_exec. dvander@1203: ** The sqlite.lsChange represents the number of changes made by the last dvander@1203: ** insert, update, or delete statement. It remains constant throughout the dvander@1203: ** length of a statement and is then updated by OP_SetCounts. It keeps a dvander@1203: ** context stack just like lastRowid so that the count of changes dvander@1203: ** within a trigger is not seen outside the trigger. Changes to views do not dvander@1203: ** affect the value of lsChange. dvander@1203: ** The sqlite.csChange keeps track of the number of current changes (since dvander@1203: ** the last statement) and is used to update sqlite_lsChange. dvander@1203: ** dvander@1203: ** The member variables sqlite.errCode, sqlite.zErrMsg and sqlite.zErrMsg16 dvander@1203: ** store the most recent error code and, if applicable, string. The dvander@1203: ** internal function sqlite3Error() is used to set these variables dvander@1203: ** consistently. dvander@1203: */ dvander@1203: struct sqlite3 { dvander@1203: int nDb; /* Number of backends currently in use */ dvander@1203: Db *aDb; /* All backends */ dvander@1203: int flags; /* Miscellanous flags. See below */ dvander@1203: int errCode; /* Most recent error code (SQLITE_*) */ dvander@1203: int errMask; /* & result codes with this before returning */ dvander@1203: u8 autoCommit; /* The auto-commit flag. */ dvander@1203: u8 temp_store; /* 1: file 2: memory 0: default */ dvander@1203: int nTable; /* Number of tables in the database */ dvander@1203: CollSeq *pDfltColl; /* The default collating sequence (BINARY) */ dvander@1203: i64 lastRowid; /* ROWID of most recent insert (see above) */ dvander@1203: i64 priorNewRowid; /* Last randomly generated ROWID */ dvander@1203: int magic; /* Magic number for detect library misuse */ dvander@1203: int nChange; /* Value returned by sqlite3_changes() */ dvander@1203: int nTotalChange; /* Value returned by sqlite3_total_changes() */ dvander@1203: struct sqlite3InitInfo { /* Information used during initialization */ dvander@1203: int iDb; /* When back is being initialized */ dvander@1203: int newTnum; /* Rootpage of table being initialized */ dvander@1203: u8 busy; /* TRUE if currently initializing */ dvander@1203: } init; dvander@1203: int nExtension; /* Number of loaded extensions */ dvander@1203: void **aExtension; /* Array of shared libraray handles */ dvander@1203: struct Vdbe *pVdbe; /* List of active virtual machines */ dvander@1203: int activeVdbeCnt; /* Number of vdbes currently executing */ dvander@1203: void (*xTrace)(void*,const char*); /* Trace function */ dvander@1203: void *pTraceArg; /* Argument to the trace function */ dvander@1203: void (*xProfile)(void*,const char*,u64); /* Profiling function */ dvander@1203: void *pProfileArg; /* Argument to profile function */ dvander@1203: void *pCommitArg; /* Argument to xCommitCallback() */ dvander@1203: int (*xCommitCallback)(void*); /* Invoked at every commit. */ dvander@1203: void *pRollbackArg; /* Argument to xRollbackCallback() */ dvander@1203: void (*xRollbackCallback)(void*); /* Invoked at every commit. */ dvander@1203: void *pUpdateArg; dvander@1203: void (*xUpdateCallback)(void*,int, const char*,const char*,sqlite_int64); dvander@1203: void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*); dvander@1203: void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*); dvander@1203: void *pCollNeededArg; dvander@1203: sqlite3_value *pErr; /* Most recent error message */ dvander@1203: char *zErrMsg; /* Most recent error message (UTF-8 encoded) */ dvander@1203: char *zErrMsg16; /* Most recent error message (UTF-16 encoded) */ dvander@1203: union { dvander@1203: int isInterrupted; /* True if sqlite3_interrupt has been called */ dvander@1203: double notUsed1; /* Spacer */ dvander@1203: } u1; dvander@1203: #ifndef SQLITE_OMIT_AUTHORIZATION dvander@1203: int (*xAuth)(void*,int,const char*,const char*,const char*,const char*); dvander@1203: /* Access authorization function */ dvander@1203: void *pAuthArg; /* 1st argument to the access auth function */ dvander@1203: #endif dvander@1203: #ifndef SQLITE_OMIT_PROGRESS_CALLBACK dvander@1203: int (*xProgress)(void *); /* The progress callback */ dvander@1203: void *pProgressArg; /* Argument to the progress callback */ dvander@1203: int nProgressOps; /* Number of opcodes for progress callback */ dvander@1203: #endif dvander@1203: #ifndef SQLITE_OMIT_VIRTUALTABLE dvander@1203: Hash aModule; /* populated by sqlite3_create_module() */ dvander@1203: Table *pVTab; /* vtab with active Connect/Create method */ dvander@1203: sqlite3_vtab **aVTrans; /* Virtual tables with open transactions */ dvander@1203: int nVTrans; /* Allocated size of aVTrans */ dvander@1203: #endif dvander@1203: Hash aFunc; /* All functions that can be in SQL exprs */ dvander@1203: Hash aCollSeq; /* All collating sequences */ dvander@1203: BusyHandler busyHandler; /* Busy callback */ dvander@1203: int busyTimeout; /* Busy handler timeout, in msec */ dvander@1203: Db aDbStatic[2]; /* Static space for the 2 default backends */ dvander@1203: #ifdef SQLITE_SSE dvander@1203: sqlite3_stmt *pFetch; /* Used by SSE to fetch stored statements */ dvander@1203: #endif dvander@1203: u8 dfltLockMode; /* Default locking-mode for attached dbs */ dvander@1203: }; dvander@1203: dvander@1203: /* dvander@1203: ** A macro to discover the encoding of a database. dvander@1203: */ dvander@1203: #define ENC(db) ((db)->aDb[0].pSchema->enc) dvander@1203: dvander@1203: /* dvander@1203: ** Possible values for the sqlite.flags and or Db.flags fields. dvander@1203: ** dvander@1203: ** On sqlite.flags, the SQLITE_InTrans value means that we have dvander@1203: ** executed a BEGIN. On Db.flags, SQLITE_InTrans means a statement dvander@1203: ** transaction is active on that particular database file. dvander@1203: */ dvander@1203: #define SQLITE_VdbeTrace 0x00000001 /* True to trace VDBE execution */ dvander@1203: #define SQLITE_InTrans 0x00000008 /* True if in a transaction */ dvander@1203: #define SQLITE_InternChanges 0x00000010 /* Uncommitted Hash table changes */ dvander@1203: #define SQLITE_FullColNames 0x00000020 /* Show full column names on SELECT */ dvander@1203: #define SQLITE_ShortColNames 0x00000040 /* Show short columns names */ dvander@1203: #define SQLITE_CountRows 0x00000080 /* Count rows changed by INSERT, */ dvander@1203: /* DELETE, or UPDATE and return */ dvander@1203: /* the count using a callback. */ dvander@1203: #define SQLITE_NullCallback 0x00000100 /* Invoke the callback once if the */ dvander@1203: /* result set is empty */ dvander@1203: #define SQLITE_SqlTrace 0x00000200 /* Debug print SQL as it executes */ dvander@1203: #define SQLITE_VdbeListing 0x00000400 /* Debug listings of VDBE programs */ dvander@1203: #define SQLITE_WriteSchema 0x00000800 /* OK to update SQLITE_MASTER */ dvander@1203: #define SQLITE_NoReadlock 0x00001000 /* Readlocks are omitted when dvander@1203: ** accessing read-only databases */ dvander@1203: #define SQLITE_IgnoreChecks 0x00002000 /* Do not enforce check constraints */ dvander@1203: #define SQLITE_ReadUncommitted 0x00004000 /* For shared-cache mode */ dvander@1203: #define SQLITE_LegacyFileFmt 0x00008000 /* Create new databases in format 1 */ dvander@1203: #define SQLITE_FullFSync 0x00010000 /* Use full fsync on the backend */ dvander@1203: #define SQLITE_LoadExtension 0x00020000 /* Enable load_extension */ dvander@1203: dvander@1203: #define SQLITE_RecoveryMode 0x00040000 /* Ignore schema errors */ dvander@1203: dvander@1203: /* dvander@1203: ** Possible values for the sqlite.magic field. dvander@1203: ** The numbers are obtained at random and have no special meaning, other dvander@1203: ** than being distinct from one another. dvander@1203: */ dvander@1203: #define SQLITE_MAGIC_OPEN 0xa029a697 /* Database is open */ dvander@1203: #define SQLITE_MAGIC_CLOSED 0x9f3c2d33 /* Database is closed */ dvander@1203: #define SQLITE_MAGIC_BUSY 0xf03b7906 /* Database currently in use */ dvander@1203: #define SQLITE_MAGIC_ERROR 0xb5357930 /* An SQLITE_MISUSE error occurred */ dvander@1203: dvander@1203: /* dvander@1203: ** Each SQL function is defined by an instance of the following dvander@1203: ** structure. A pointer to this structure is stored in the sqlite.aFunc dvander@1203: ** hash table. When multiple functions have the same name, the hash table dvander@1203: ** points to a linked list of these structures. dvander@1203: */ dvander@1203: struct FuncDef { dvander@1203: i16 nArg; /* Number of arguments. -1 means unlimited */ dvander@1203: u8 iPrefEnc; /* Preferred text encoding (SQLITE_UTF8, 16LE, 16BE) */ dvander@1203: u8 needCollSeq; /* True if sqlite3GetFuncCollSeq() might be called */ dvander@1203: u8 flags; /* Some combination of SQLITE_FUNC_* */ dvander@1203: void *pUserData; /* User data parameter */ dvander@1203: FuncDef *pNext; /* Next function with same name */ dvander@1203: void (*xFunc)(sqlite3_context*,int,sqlite3_value**); /* Regular function */ dvander@1203: void (*xStep)(sqlite3_context*,int,sqlite3_value**); /* Aggregate step */ dvander@1203: void (*xFinalize)(sqlite3_context*); /* Aggregate finializer */ dvander@1203: char zName[1]; /* SQL name of the function. MUST BE LAST */ dvander@1203: }; dvander@1203: dvander@1203: /* dvander@1203: ** Each SQLite module (virtual table definition) is defined by an dvander@1203: ** instance of the following structure, stored in the sqlite3.aModule dvander@1203: ** hash table. dvander@1203: */ dvander@1203: struct Module { dvander@1203: const sqlite3_module *pModule; /* Callback pointers */ dvander@1203: const char *zName; /* Name passed to create_module() */ dvander@1203: void *pAux; /* pAux passed to create_module() */ dvander@1203: void (*xDestroy)(void *); /* Module destructor function */ dvander@1203: }; dvander@1203: dvander@1203: /* dvander@1203: ** Possible values for FuncDef.flags dvander@1203: */ dvander@1203: #define SQLITE_FUNC_LIKE 0x01 /* Candidate for the LIKE optimization */ dvander@1203: #define SQLITE_FUNC_CASE 0x02 /* Case-sensitive LIKE-type function */ dvander@1203: #define SQLITE_FUNC_EPHEM 0x04 /* Ephermeral. Delete with VDBE */ dvander@1203: dvander@1203: /* dvander@1203: ** information about each column of an SQL table is held in an instance dvander@1203: ** of this structure. dvander@1203: */ dvander@1203: struct Column { dvander@1203: char *zName; /* Name of this column */ dvander@1203: Expr *pDflt; /* Default value of this column */ dvander@1203: char *zType; /* Data type for this column */ dvander@1203: char *zColl; /* Collating sequence. If NULL, use the default */ dvander@1203: u8 notNull; /* True if there is a NOT NULL constraint */ dvander@1203: u8 isPrimKey; /* True if this column is part of the PRIMARY KEY */ dvander@1203: char affinity; /* One of the SQLITE_AFF_... values */ dvander@1203: #ifndef SQLITE_OMIT_VIRTUALTABLE dvander@1203: u8 isHidden; /* True if this column is 'hidden' */ dvander@1203: #endif dvander@1203: }; dvander@1203: dvander@1203: /* dvander@1203: ** A "Collating Sequence" is defined by an instance of the following dvander@1203: ** structure. Conceptually, a collating sequence consists of a name and dvander@1203: ** a comparison routine that defines the order of that sequence. dvander@1203: ** dvander@1203: ** There may two seperate implementations of the collation function, one dvander@1203: ** that processes text in UTF-8 encoding (CollSeq.xCmp) and another that dvander@1203: ** processes text encoded in UTF-16 (CollSeq.xCmp16), using the machine dvander@1203: ** native byte order. When a collation sequence is invoked, SQLite selects dvander@1203: ** the version that will require the least expensive encoding dvander@1203: ** translations, if any. dvander@1203: ** dvander@1203: ** The CollSeq.pUser member variable is an extra parameter that passed in dvander@1203: ** as the first argument to the UTF-8 comparison function, xCmp. dvander@1203: ** CollSeq.pUser16 is the equivalent for the UTF-16 comparison function, dvander@1203: ** xCmp16. dvander@1203: ** dvander@1203: ** If both CollSeq.xCmp and CollSeq.xCmp16 are NULL, it means that the dvander@1203: ** collating sequence is undefined. Indices built on an undefined dvander@1203: ** collating sequence may not be read or written. dvander@1203: */ dvander@1203: struct CollSeq { dvander@1203: char *zName; /* Name of the collating sequence, UTF-8 encoded */ dvander@1203: u8 enc; /* Text encoding handled by xCmp() */ dvander@1203: u8 type; /* One of the SQLITE_COLL_... values below */ dvander@1203: void *pUser; /* First argument to xCmp() */ dvander@1203: int (*xCmp)(void*,int, const void*, int, const void*); dvander@1203: void (*xDel)(void*); /* Destructor for pUser */ dvander@1203: }; dvander@1203: dvander@1203: /* dvander@1203: ** Allowed values of CollSeq flags: dvander@1203: */ dvander@1203: #define SQLITE_COLL_BINARY 1 /* The default memcmp() collating sequence */ dvander@1203: #define SQLITE_COLL_NOCASE 2 /* The built-in NOCASE collating sequence */ dvander@1203: #define SQLITE_COLL_REVERSE 3 /* The built-in REVERSE collating sequence */ dvander@1203: #define SQLITE_COLL_USER 0 /* Any other user-defined collating sequence */ dvander@1203: dvander@1203: /* dvander@1203: ** A sort order can be either ASC or DESC. dvander@1203: */ dvander@1203: #define SQLITE_SO_ASC 0 /* Sort in ascending order */ dvander@1203: #define SQLITE_SO_DESC 1 /* Sort in ascending order */ dvander@1203: dvander@1203: /* dvander@1203: ** Column affinity types. dvander@1203: ** dvander@1203: ** These used to have mnemonic name like 'i' for SQLITE_AFF_INTEGER and dvander@1203: ** 't' for SQLITE_AFF_TEXT. But we can save a little space and improve dvander@1203: ** the speed a little by number the values consecutively. dvander@1203: ** dvander@1203: ** But rather than start with 0 or 1, we begin with 'a'. That way, dvander@1203: ** when multiple affinity types are concatenated into a string and dvander@1203: ** used as the P3 operand, they will be more readable. dvander@1203: ** dvander@1203: ** Note also that the numeric types are grouped together so that testing dvander@1203: ** for a numeric type is a single comparison. dvander@1203: */ dvander@1203: #define SQLITE_AFF_TEXT 'a' dvander@1203: #define SQLITE_AFF_NONE 'b' dvander@1203: #define SQLITE_AFF_NUMERIC 'c' dvander@1203: #define SQLITE_AFF_INTEGER 'd' dvander@1203: #define SQLITE_AFF_REAL 'e' dvander@1203: dvander@1203: #define sqlite3IsNumericAffinity(X) ((X)>=SQLITE_AFF_NUMERIC) dvander@1203: dvander@1203: /* dvander@1203: ** Each SQL table is represented in memory by an instance of the dvander@1203: ** following structure. dvander@1203: ** dvander@1203: ** Table.zName is the name of the table. The case of the original dvander@1203: ** CREATE TABLE statement is stored, but case is not significant for dvander@1203: ** comparisons. dvander@1203: ** dvander@1203: ** Table.nCol is the number of columns in this table. Table.aCol is a dvander@1203: ** pointer to an array of Column structures, one for each column. dvander@1203: ** dvander@1203: ** If the table has an INTEGER PRIMARY KEY, then Table.iPKey is the index of dvander@1203: ** the column that is that key. Otherwise Table.iPKey is negative. Note dvander@1203: ** that the datatype of the PRIMARY KEY must be INTEGER for this field to dvander@1203: ** be set. An INTEGER PRIMARY KEY is used as the rowid for each row of dvander@1203: ** the table. If a table has no INTEGER PRIMARY KEY, then a random rowid dvander@1203: ** is generated for each row of the table. Table.hasPrimKey is true if dvander@1203: ** the table has any PRIMARY KEY, INTEGER or otherwise. dvander@1203: ** dvander@1203: ** Table.tnum is the page number for the root BTree page of the table in the dvander@1203: ** database file. If Table.iDb is the index of the database table backend dvander@1203: ** in sqlite.aDb[]. 0 is for the main database and 1 is for the file that dvander@1203: ** holds temporary tables and indices. If Table.isEphem dvander@1203: ** is true, then the table is stored in a file that is automatically deleted dvander@1203: ** when the VDBE cursor to the table is closed. In this case Table.tnum dvander@1203: ** refers VDBE cursor number that holds the table open, not to the root dvander@1203: ** page number. Transient tables are used to hold the results of a dvander@1203: ** sub-query that appears instead of a real table name in the FROM clause dvander@1203: ** of a SELECT statement. dvander@1203: */ dvander@1203: struct Table { dvander@1203: char *zName; /* Name of the table */ dvander@1203: int nCol; /* Number of columns in this table */ dvander@1203: Column *aCol; /* Information about each column */ dvander@1203: int iPKey; /* If not less then 0, use aCol[iPKey] as the primary key */ dvander@1203: Index *pIndex; /* List of SQL indexes on this table. */ dvander@1203: int tnum; /* Root BTree node for this table (see note above) */ dvander@1203: Select *pSelect; /* NULL for tables. Points to definition if a view. */ dvander@1203: int nRef; /* Number of pointers to this Table */ dvander@1203: Trigger *pTrigger; /* List of SQL triggers on this table */ dvander@1203: FKey *pFKey; /* Linked list of all foreign keys in this table */ dvander@1203: char *zColAff; /* String defining the affinity of each column */ dvander@1203: #ifndef SQLITE_OMIT_CHECK dvander@1203: Expr *pCheck; /* The AND of all CHECK constraints */ dvander@1203: #endif dvander@1203: #ifndef SQLITE_OMIT_ALTERTABLE dvander@1203: int addColOffset; /* Offset in CREATE TABLE statement to add a new column */ dvander@1203: #endif dvander@1203: u8 readOnly; /* True if this table should not be written by the user */ dvander@1203: u8 isEphem; /* True if created using OP_OpenEphermeral */ dvander@1203: u8 hasPrimKey; /* True if there exists a primary key */ dvander@1203: u8 keyConf; /* What to do in case of uniqueness conflict on iPKey */ dvander@1203: u8 autoInc; /* True if the integer primary key is autoincrement */ dvander@1203: #ifndef SQLITE_OMIT_VIRTUALTABLE dvander@1203: u8 isVirtual; /* True if this is a virtual table */ dvander@1203: u8 isCommit; /* True once the CREATE TABLE has been committed */ dvander@1203: Module *pMod; /* Pointer to the implementation of the module */ dvander@1203: sqlite3_vtab *pVtab; /* Pointer to the module instance */ dvander@1203: int nModuleArg; /* Number of arguments to the module */ dvander@1203: char **azModuleArg; /* Text of all module args. [0] is module name */ dvander@1203: #endif dvander@1203: Schema *pSchema; dvander@1203: }; dvander@1203: dvander@1203: /* dvander@1203: ** Test to see whether or not a table is a virtual table. This is dvander@1203: ** done as a macro so that it will be optimized out when virtual dvander@1203: ** table support is omitted from the build. dvander@1203: */ dvander@1203: #ifndef SQLITE_OMIT_VIRTUALTABLE dvander@1203: # define IsVirtual(X) ((X)->isVirtual) dvander@1203: # define IsHiddenColumn(X) ((X)->isHidden) dvander@1203: #else dvander@1203: # define IsVirtual(X) 0 dvander@1203: # define IsHiddenColumn(X) 0 dvander@1203: #endif dvander@1203: dvander@1203: /* dvander@1203: ** Each foreign key constraint is an instance of the following structure. dvander@1203: ** dvander@1203: ** A foreign key is associated with two tables. The "from" table is dvander@1203: ** the table that contains the REFERENCES clause that creates the foreign dvander@1203: ** key. The "to" table is the table that is named in the REFERENCES clause. dvander@1203: ** Consider this example: dvander@1203: ** dvander@1203: ** CREATE TABLE ex1( dvander@1203: ** a INTEGER PRIMARY KEY, dvander@1203: ** b INTEGER CONSTRAINT fk1 REFERENCES ex2(x) dvander@1203: ** ); dvander@1203: ** dvander@1203: ** For foreign key "fk1", the from-table is "ex1" and the to-table is "ex2". dvander@1203: ** dvander@1203: ** Each REFERENCES clause generates an instance of the following structure dvander@1203: ** which is attached to the from-table. The to-table need not exist when dvander@1203: ** the from-table is created. The existance of the to-table is not checked dvander@1203: ** until an attempt is made to insert data into the from-table. dvander@1203: ** dvander@1203: ** The sqlite.aFKey hash table stores pointers to this structure dvander@1203: ** given the name of a to-table. For each to-table, all foreign keys dvander@1203: ** associated with that table are on a linked list using the FKey.pNextTo dvander@1203: ** field. dvander@1203: */ dvander@1203: struct FKey { dvander@1203: Table *pFrom; /* The table that constains the REFERENCES clause */ dvander@1203: FKey *pNextFrom; /* Next foreign key in pFrom */ dvander@1203: char *zTo; /* Name of table that the key points to */ dvander@1203: FKey *pNextTo; /* Next foreign key that points to zTo */ dvander@1203: int nCol; /* Number of columns in this key */ dvander@1203: struct sColMap { /* Mapping of columns in pFrom to columns in zTo */ dvander@1203: int iFrom; /* Index of column in pFrom */ dvander@1203: char *zCol; /* Name of column in zTo. If 0 use PRIMARY KEY */ dvander@1203: } *aCol; /* One entry for each of nCol column s */ dvander@1203: u8 isDeferred; /* True if constraint checking is deferred till COMMIT */ dvander@1203: u8 updateConf; /* How to resolve conflicts that occur on UPDATE */ dvander@1203: u8 deleteConf; /* How to resolve conflicts that occur on DELETE */ dvander@1203: u8 insertConf; /* How to resolve conflicts that occur on INSERT */ dvander@1203: }; dvander@1203: dvander@1203: /* dvander@1203: ** SQLite supports many different ways to resolve a contraint dvander@1203: ** error. ROLLBACK processing means that a constraint violation dvander@1203: ** causes the operation in process to fail and for the current transaction dvander@1203: ** to be rolled back. ABORT processing means the operation in process dvander@1203: ** fails and any prior changes from that one operation are backed out, dvander@1203: ** but the transaction is not rolled back. FAIL processing means that dvander@1203: ** the operation in progress stops and returns an error code. But prior dvander@1203: ** changes due to the same operation are not backed out and no rollback dvander@1203: ** occurs. IGNORE means that the particular row that caused the constraint dvander@1203: ** error is not inserted or updated. Processing continues and no error dvander@1203: ** is returned. REPLACE means that preexisting database rows that caused dvander@1203: ** a UNIQUE constraint violation are removed so that the new insert or dvander@1203: ** update can proceed. Processing continues and no error is reported. dvander@1203: ** dvander@1203: ** RESTRICT, SETNULL, and CASCADE actions apply only to foreign keys. dvander@1203: ** RESTRICT is the same as ABORT for IMMEDIATE foreign keys and the dvander@1203: ** same as ROLLBACK for DEFERRED keys. SETNULL means that the foreign dvander@1203: ** key is set to NULL. CASCADE means that a DELETE or UPDATE of the dvander@1203: ** referenced table row is propagated into the row that holds the dvander@1203: ** foreign key. dvander@1203: ** dvander@1203: ** The following symbolic values are used to record which type dvander@1203: ** of action to take. dvander@1203: */ dvander@1203: #define OE_None 0 /* There is no constraint to check */ dvander@1203: #define OE_Rollback 1 /* Fail the operation and rollback the transaction */ dvander@1203: #define OE_Abort 2 /* Back out changes but do no rollback transaction */ dvander@1203: #define OE_Fail 3 /* Stop the operation but leave all prior changes */ dvander@1203: #define OE_Ignore 4 /* Ignore the error. Do not do the INSERT or UPDATE */ dvander@1203: #define OE_Replace 5 /* Delete existing record, then do INSERT or UPDATE */ dvander@1203: dvander@1203: #define OE_Restrict 6 /* OE_Abort for IMMEDIATE, OE_Rollback for DEFERRED */ dvander@1203: #define OE_SetNull 7 /* Set the foreign key value to NULL */ dvander@1203: #define OE_SetDflt 8 /* Set the foreign key value to its default */ dvander@1203: #define OE_Cascade 9 /* Cascade the changes */ dvander@1203: dvander@1203: #define OE_Default 99 /* Do whatever the default action is */ dvander@1203: dvander@1203: dvander@1203: /* dvander@1203: ** An instance of the following structure is passed as the first dvander@1203: ** argument to sqlite3VdbeKeyCompare and is used to control the dvander@1203: ** comparison of the two index keys. dvander@1203: ** dvander@1203: ** If the KeyInfo.incrKey value is true and the comparison would dvander@1203: ** otherwise be equal, then return a result as if the second key dvander@1203: ** were larger. dvander@1203: */ dvander@1203: struct KeyInfo { dvander@1203: u8 enc; /* Text encoding - one of the TEXT_Utf* values */ dvander@1203: u8 incrKey; /* Increase 2nd key by epsilon before comparison */ dvander@1203: int nField; /* Number of entries in aColl[] */ dvander@1203: u8 *aSortOrder; /* If defined an aSortOrder[i] is true, sort DESC */ dvander@1203: CollSeq *aColl[1]; /* Collating sequence for each term of the key */ dvander@1203: }; dvander@1203: dvander@1203: /* dvander@1203: ** Each SQL index is represented in memory by an dvander@1203: ** instance of the following structure. dvander@1203: ** dvander@1203: ** The columns of the table that are to be indexed are described dvander@1203: ** by the aiColumn[] field of this structure. For example, suppose dvander@1203: ** we have the following table and index: dvander@1203: ** dvander@1203: ** CREATE TABLE Ex1(c1 int, c2 int, c3 text); dvander@1203: ** CREATE INDEX Ex2 ON Ex1(c3,c1); dvander@1203: ** dvander@1203: ** In the Table structure describing Ex1, nCol==3 because there are dvander@1203: ** three columns in the table. In the Index structure describing dvander@1203: ** Ex2, nColumn==2 since 2 of the 3 columns of Ex1 are indexed. dvander@1203: ** The value of aiColumn is {2, 0}. aiColumn[0]==2 because the dvander@1203: ** first column to be indexed (c3) has an index of 2 in Ex1.aCol[]. dvander@1203: ** The second column to be indexed (c1) has an index of 0 in dvander@1203: ** Ex1.aCol[], hence Ex2.aiColumn[1]==0. dvander@1203: ** dvander@1203: ** The Index.onError field determines whether or not the indexed columns dvander@1203: ** must be unique and what to do if they are not. When Index.onError=OE_None, dvander@1203: ** it means this is not a unique index. Otherwise it is a unique index dvander@1203: ** and the value of Index.onError indicate the which conflict resolution dvander@1203: ** algorithm to employ whenever an attempt is made to insert a non-unique dvander@1203: ** element. dvander@1203: */ dvander@1203: struct Index { dvander@1203: char *zName; /* Name of this index */ dvander@1203: int nColumn; /* Number of columns in the table used by this index */ dvander@1203: int *aiColumn; /* Which columns are used by this index. 1st is 0 */ dvander@1203: unsigned *aiRowEst; /* Result of ANALYZE: Est. rows selected by each column */ dvander@1203: Table *pTable; /* The SQL table being indexed */ dvander@1203: int tnum; /* Page containing root of this index in database file */ dvander@1203: u8 onError; /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */ dvander@1203: u8 autoIndex; /* True if is automatically created (ex: by UNIQUE) */ dvander@1203: char *zColAff; /* String defining the affinity of each column */ dvander@1203: Index *pNext; /* The next index associated with the same table */ dvander@1203: Schema *pSchema; /* Schema containing this index */ dvander@1203: u8 *aSortOrder; /* Array of size Index.nColumn. True==DESC, False==ASC */ dvander@1203: char **azColl; /* Array of collation sequence names for index */ dvander@1203: }; dvander@1203: dvander@1203: /* dvander@1203: ** Each token coming out of the lexer is an instance of dvander@1203: ** this structure. Tokens are also used as part of an expression. dvander@1203: ** dvander@1203: ** Note if Token.z==0 then Token.dyn and Token.n are undefined and dvander@1203: ** may contain random values. Do not make any assuptions about Token.dyn dvander@1203: ** and Token.n when Token.z==0. dvander@1203: */ dvander@1203: struct Token { dvander@1203: const unsigned char *z; /* Text of the token. Not NULL-terminated! */ dvander@1203: unsigned dyn : 1; /* True for malloced memory, false for static */ dvander@1203: unsigned n : 31; /* Number of characters in this token */ dvander@1203: }; dvander@1203: dvander@1203: /* dvander@1203: ** An instance of this structure contains information needed to generate dvander@1203: ** code for a SELECT that contains aggregate functions. dvander@1203: ** dvander@1203: ** If Expr.op==TK_AGG_COLUMN or TK_AGG_FUNCTION then Expr.pAggInfo is a dvander@1203: ** pointer to this structure. The Expr.iColumn field is the index in dvander@1203: ** AggInfo.aCol[] or AggInfo.aFunc[] of information needed to generate dvander@1203: ** code for that node. dvander@1203: ** dvander@1203: ** AggInfo.pGroupBy and AggInfo.aFunc.pExpr point to fields within the dvander@1203: ** original Select structure that describes the SELECT statement. These dvander@1203: ** fields do not need to be freed when deallocating the AggInfo structure. dvander@1203: */ dvander@1203: struct AggInfo { dvander@1203: u8 directMode; /* Direct rendering mode means take data directly dvander@1203: ** from source tables rather than from accumulators */ dvander@1203: u8 useSortingIdx; /* In direct mode, reference the sorting index rather dvander@1203: ** than the source table */ dvander@1203: int sortingIdx; /* Cursor number of the sorting index */ dvander@1203: ExprList *pGroupBy; /* The group by clause */ dvander@1203: int nSortingColumn; /* Number of columns in the sorting index */ dvander@1203: struct AggInfo_col { /* For each column used in source tables */ dvander@1203: Table *pTab; /* Source table */ dvander@1203: int iTable; /* Cursor number of the source table */ dvander@1203: int iColumn; /* Column number within the source table */ dvander@1203: int iSorterColumn; /* Column number in the sorting index */ dvander@1203: int iMem; /* Memory location that acts as accumulator */ dvander@1203: Expr *pExpr; /* The original expression */ dvander@1203: } *aCol; dvander@1203: int nColumn; /* Number of used entries in aCol[] */ dvander@1203: int nColumnAlloc; /* Number of slots allocated for aCol[] */ dvander@1203: int nAccumulator; /* Number of columns that show through to the output. dvander@1203: ** Additional columns are used only as parameters to dvander@1203: ** aggregate functions */ dvander@1203: struct AggInfo_func { /* For each aggregate function */ dvander@1203: Expr *pExpr; /* Expression encoding the function */ dvander@1203: FuncDef *pFunc; /* The aggregate function implementation */ dvander@1203: int iMem; /* Memory location that acts as accumulator */ dvander@1203: int iDistinct; /* Ephermeral table used to enforce DISTINCT */ dvander@1203: } *aFunc; dvander@1203: int nFunc; /* Number of entries in aFunc[] */ dvander@1203: int nFuncAlloc; /* Number of slots allocated for aFunc[] */ dvander@1203: }; dvander@1203: dvander@1203: /* dvander@1203: ** Each node of an expression in the parse tree is an instance dvander@1203: ** of this structure. dvander@1203: ** dvander@1203: ** Expr.op is the opcode. The integer parser token codes are reused dvander@1203: ** as opcodes here. For example, the parser defines TK_GE to be an integer dvander@1203: ** code representing the ">=" operator. This same integer code is reused dvander@1203: ** to represent the greater-than-or-equal-to operator in the expression dvander@1203: ** tree. dvander@1203: ** dvander@1203: ** Expr.pRight and Expr.pLeft are subexpressions. Expr.pList is a list dvander@1203: ** of argument if the expression is a function. dvander@1203: ** dvander@1203: ** Expr.token is the operator token for this node. For some expressions dvander@1203: ** that have subexpressions, Expr.token can be the complete text that gave dvander@1203: ** rise to the Expr. In the latter case, the token is marked as being dvander@1203: ** a compound token. dvander@1203: ** dvander@1203: ** An expression of the form ID or ID.ID refers to a column in a table. dvander@1203: ** For such expressions, Expr.op is set to TK_COLUMN and Expr.iTable is dvander@1203: ** the integer cursor number of a VDBE cursor pointing to that table and dvander@1203: ** Expr.iColumn is the column number for the specific column. If the dvander@1203: ** expression is used as a result in an aggregate SELECT, then the dvander@1203: ** value is also stored in the Expr.iAgg column in the aggregate so that dvander@1203: ** it can be accessed after all aggregates are computed. dvander@1203: ** dvander@1203: ** If the expression is a function, the Expr.iTable is an integer code dvander@1203: ** representing which function. If the expression is an unbound variable dvander@1203: ** marker (a question mark character '?' in the original SQL) then the dvander@1203: ** Expr.iTable holds the index number for that variable. dvander@1203: ** dvander@1203: ** If the expression is a subquery then Expr.iColumn holds an integer dvander@1203: ** register number containing the result of the subquery. If the dvander@1203: ** subquery gives a constant result, then iTable is -1. If the subquery dvander@1203: ** gives a different answer at different times during statement processing dvander@1203: ** then iTable is the address of a subroutine that computes the subquery. dvander@1203: ** dvander@1203: ** The Expr.pSelect field points to a SELECT statement. The SELECT might dvander@1203: ** be the right operand of an IN operator. Or, if a scalar SELECT appears dvander@1203: ** in an expression the opcode is TK_SELECT and Expr.pSelect is the only dvander@1203: ** operand. dvander@1203: ** dvander@1203: ** If the Expr is of type OP_Column, and the table it is selecting from dvander@1203: ** is a disk table or the "old.*" pseudo-table, then pTab points to the dvander@1203: ** corresponding table definition. dvander@1203: */ dvander@1203: struct Expr { dvander@1203: u8 op; /* Operation performed by this node */ dvander@1203: char affinity; /* The affinity of the column or 0 if not a column */ dvander@1203: u16 flags; /* Various flags. See below */ dvander@1203: CollSeq *pColl; /* The collation type of the column or 0 */ dvander@1203: Expr *pLeft, *pRight; /* Left and right subnodes */ dvander@1203: ExprList *pList; /* A list of expressions used as function arguments dvander@1203: ** or in " IN (aCol[] or ->aFunc[] */ dvander@1203: int iRightJoinTable; /* If EP_FromJoin, the right table of the join */ dvander@1203: Select *pSelect; /* When the expression is a sub-select. Also the dvander@1203: ** right side of " IN (