digikam
sqliteInt.h
Go to the documentation of this file.00001 /* 00002 ** 2001 September 15 00003 ** 00004 ** The author disclaims copyright to this source code. In place of 00005 ** a legal notice, here is a blessing: 00006 ** 00007 ** May you do good and not evil. 00008 ** May you find forgiveness for yourself and forgive others. 00009 ** May you share freely, never taking more than you give. 00010 ** 00011 ************************************************************************* 00012 ** Internal interface definitions for SQLite. 00013 ** 00014 ** @(#) $Id: sqliteInt.h 875674 2008-10-25 07:30:45Z cgilles $ 00015 */ 00016 00017 #ifdef HAVE_CONFIG_H 00018 #include "config.h" 00019 #endif 00020 00021 #include "sqlite.h" 00022 #include "hash.h" 00023 #include "parse.h" 00024 #include "btree.h" 00025 #include <stdio.h> 00026 #include <stdlib.h> 00027 #include <string.h> 00028 #include <assert.h> 00029 00030 /* 00031 ** The maximum number of in-memory pages to use for the main database 00032 ** table and for temporary tables. 00033 */ 00034 #define MAX_PAGES 2000 00035 #define TEMP_PAGES 500 00036 00037 /* 00038 ** If the following macro is set to 1, then NULL values are considered 00039 ** distinct for the SELECT DISTINCT statement and for UNION or EXCEPT 00040 ** compound queries. No other SQL database engine (among those tested) 00041 ** works this way except for OCELOT. But the SQL92 spec implies that 00042 ** this is how things should work. 00043 ** 00044 ** If the following macro is set to 0, then NULLs are indistinct for 00045 ** SELECT DISTINCT and for UNION. 00046 */ 00047 #define NULL_ALWAYS_DISTINCT 0 00048 00049 /* 00050 ** If the following macro is set to 1, then NULL values are considered 00051 ** distinct when determining whether or not two entries are the same 00052 ** in a UNIQUE index. This is the way PostgreSQL, Oracle, DB2, MySQL, 00053 ** OCELOT, and Firebird all work. The SQL92 spec explicitly says this 00054 ** is the way things are suppose to work. 00055 ** 00056 ** If the following macro is set to 0, the NULLs are indistinct for 00057 ** a UNIQUE index. In this mode, you can only have a single NULL entry 00058 ** for a column declared UNIQUE. This is the way Informix and SQL Server 00059 ** work. 00060 */ 00061 #define NULL_DISTINCT_FOR_UNIQUE 1 00062 00063 /* 00064 ** The maximum number of attached databases. This must be at least 2 00065 ** in order to support the main database file (0) and the file used to 00066 ** hold temporary tables (1). And it must be less than 256 because 00067 ** an unsigned character is used to stored the database index. 00068 */ 00069 #define MAX_ATTACHED 10 00070 00071 /* 00072 ** The next macro is used to determine where TEMP tables and indices 00073 ** are stored. Possible values: 00074 ** 00075 ** 0 Always use a temporary files 00076 ** 1 Use a file unless overridden by "PRAGMA temp_store" 00077 ** 2 Use memory unless overridden by "PRAGMA temp_store" 00078 ** 3 Always use memory 00079 */ 00080 #ifndef TEMP_STORE 00081 # define TEMP_STORE 1 00082 #endif 00083 00084 /* 00085 ** When building SQLite for embedded systems where memory is scarce, 00086 ** you can define one or more of the following macros to omit extra 00087 ** features of the library and thus keep the size of the library to 00088 ** a minimum. 00089 */ 00090 /* #define SQLITE_OMIT_AUTHORIZATION 1 */ 00091 /* #define SQLITE_OMIT_INMEMORYDB 1 */ 00092 /* #define SQLITE_OMIT_VACUUM 1 */ 00093 /* #define SQLITE_OMIT_DATETIME_FUNCS 1 */ 00094 /* #define SQLITE_OMIT_PROGRESS_CALLBACK 1 */ 00095 00096 /* 00097 ** Integers of known sizes. These typedefs might change for architectures 00098 ** where the sizes very. Preprocessor macros are available so that the 00099 ** types can be conveniently redefined at compile-type. Like this: 00100 ** 00101 ** cc '-DUINTPTR_TYPE=long long int' ... 00102 */ 00103 #ifndef UINT32_TYPE 00104 # define UINT32_TYPE unsigned int 00105 #endif 00106 #ifndef UINT16_TYPE 00107 # define UINT16_TYPE unsigned short int 00108 #endif 00109 #ifndef INT16_TYPE 00110 # define INT16_TYPE short int 00111 #endif 00112 #ifndef UINT8_TYPE 00113 # define UINT8_TYPE unsigned char 00114 #endif 00115 #ifndef INT8_TYPE 00116 # define INT8_TYPE signed char 00117 #endif 00118 #ifndef INTPTR_TYPE 00119 # if SQLITE_PTR_SZ==4 00120 # define INTPTR_TYPE int 00121 # else 00122 # define INTPTR_TYPE long long 00123 # endif 00124 #endif 00125 typedef UINT32_TYPE u32; /* 4-byte unsigned integer */ 00126 typedef UINT16_TYPE u16; /* 2-byte unsigned integer */ 00127 typedef INT16_TYPE i16; /* 2-byte signed integer */ 00128 typedef UINT8_TYPE u8; /* 1-byte unsigned integer */ 00129 typedef UINT8_TYPE i8; /* 1-byte signed integer */ 00130 typedef INTPTR_TYPE ptr; /* Big enough to hold a pointer */ 00131 typedef unsigned INTPTR_TYPE uptr; /* Big enough to hold a pointer */ 00132 00133 /* 00134 ** Defer sourcing vdbe.h until after the "u8" typedef is defined. 00135 */ 00136 #include "vdbe.h" 00137 00138 /* 00139 ** Most C compilers these days recognize "long double", don't they? 00140 ** Just in case we encounter one that does not, we will create a macro 00141 ** for long double so that it can be easily changed to just "double". 00142 */ 00143 #ifndef LONGDOUBLE_TYPE 00144 # define LONGDOUBLE_TYPE long double 00145 #endif 00146 00147 /* 00148 ** This macro casts a pointer to an integer. Useful for doing 00149 ** pointer arithmetic. 00150 */ 00151 #define Addr(X) ((uptr)X) 00152 00153 /* 00154 ** The maximum number of bytes of data that can be put into a single 00155 ** row of a single table. The upper bound on this limit is 16777215 00156 ** bytes (or 16MB-1). We have arbitrarily set the limit to just 1MB 00157 ** here because the overflow page chain is inefficient for really big 00158 ** records and we want to discourage people from thinking that 00159 ** multi-megabyte records are OK. If your needs are different, you can 00160 ** change this define and recompile to increase or decrease the record 00161 ** size. 00162 ** 00163 ** The 16777198 is computed as follows: 238 bytes of payload on the 00164 ** original pages plus 16448 overflow pages each holding 1020 bytes of 00165 ** data. 00166 */ 00167 #define MAX_BYTES_PER_ROW 1048576 00168 /* #define MAX_BYTES_PER_ROW 16777198 */ 00169 00170 /* 00171 ** If memory allocation problems are found, recompile with 00172 ** 00173 ** -DMEMORY_DEBUG=1 00174 ** 00175 ** to enable some sanity checking on malloc() and free(). To 00176 ** check for memory leaks, recompile with 00177 ** 00178 ** -DMEMORY_DEBUG=2 00179 ** 00180 ** and a line of text will be written to standard error for 00181 ** each malloc() and free(). This output can be analyzed 00182 ** by an AWK script to determine if there are any leaks. 00183 */ 00184 #ifdef MEMORY_DEBUG 00185 # define sqliteMalloc(X) sqliteMalloc_(X,1,__FILE__,__LINE__) 00186 # define sqliteMallocRaw(X) sqliteMalloc_(X,0,__FILE__,__LINE__) 00187 # define sqliteFree(X) sqliteFree_(X,__FILE__,__LINE__) 00188 # define sqliteRealloc(X,Y) sqliteRealloc_(X,Y,__FILE__,__LINE__) 00189 # define sqliteStrDup(X) sqliteStrDup_(X,__FILE__,__LINE__) 00190 # define sqliteStrNDup(X,Y) sqliteStrNDup_(X,Y,__FILE__,__LINE__) 00191 void sqliteStrRealloc(char**); 00192 #else 00193 # define sqliteRealloc_(X,Y) sqliteRealloc(X,Y) 00194 # define sqliteStrRealloc(X) 00195 #endif 00196 00197 /* 00198 ** This variable gets set if malloc() ever fails. After it gets set, 00199 ** the SQLite library shuts down permanently. 00200 */ 00201 extern int sqlite_malloc_failed; 00202 00203 /* 00204 ** The following global variables are used for testing and debugging 00205 ** only. They only work if MEMORY_DEBUG is defined. 00206 */ 00207 #ifdef MEMORY_DEBUG 00208 extern int sqlite_nMalloc; /* Number of sqliteMalloc() calls */ 00209 extern int sqlite_nFree; /* Number of sqliteFree() calls */ 00210 extern int sqlite_iMallocFail; /* Fail sqliteMalloc() after this many calls */ 00211 #endif 00212 00213 /* 00214 ** Name of the master database table. The master database table 00215 ** is a special table that holds the names and attributes of all 00216 ** user tables and indices. 00217 */ 00218 #define MASTER_NAME "sqlite_master" 00219 #define TEMP_MASTER_NAME "sqlite_temp_master" 00220 00221 /* 00222 ** The name of the schema table. 00223 */ 00224 #define SCHEMA_TABLE(x) (x?TEMP_MASTER_NAME:MASTER_NAME) 00225 00226 /* 00227 ** A convenience macro that returns the number of elements in 00228 ** an array. 00229 */ 00230 #define ArraySize(X) (sizeof(X)/sizeof(X[0])) 00231 00232 /* 00233 ** Forward references to structures 00234 */ 00235 typedef struct Column Column; 00236 typedef struct Table Table; 00237 typedef struct Index Index; 00238 typedef struct Instruction Instruction; 00239 typedef struct Expr Expr; 00240 typedef struct ExprList ExprList; 00241 typedef struct Parse Parse; 00242 typedef struct Token Token; 00243 typedef struct IdList IdList; 00244 typedef struct SrcList SrcList; 00245 typedef struct WhereInfo WhereInfo; 00246 typedef struct WhereLevel WhereLevel; 00247 typedef struct Select Select; 00248 typedef struct AggExpr AggExpr; 00249 typedef struct FuncDef FuncDef; 00250 typedef struct Trigger Trigger; 00251 typedef struct TriggerStep TriggerStep; 00252 typedef struct TriggerStack TriggerStack; 00253 typedef struct FKey FKey; 00254 typedef struct Db Db; 00255 typedef struct AuthContext AuthContext; 00256 00257 /* 00258 ** Each database file to be accessed by the system is an instance 00259 ** of the following structure. There are normally two of these structures 00260 ** in the sqlite.aDb[] array. aDb[0] is the main database file and 00261 ** aDb[1] is the database file used to hold temporary tables. Additional 00262 ** databases may be attached. 00263 */ 00264 struct Db { 00265 char *zName; /* Name of this database */ 00266 Btree *pBt; /* The B*Tree structure for this database file */ 00267 int schema_cookie; /* Database schema version number for this file */ 00268 Hash tblHash; /* All tables indexed by name */ 00269 Hash idxHash; /* All (named) indices indexed by name */ 00270 Hash trigHash; /* All triggers indexed by name */ 00271 Hash aFKey; /* Foreign keys indexed by to-table */ 00272 u8 inTrans; /* 0: not writable. 1: Transaction. 2: Checkpoint */ 00273 u16 flags; /* Flags associated with this database */ 00274 void *pAux; /* Auxiliary data. Usually NULL */ 00275 void (*xFreeAux)(void*); /* Routine to free pAux */ 00276 }; 00277 00278 /* 00279 ** These macros can be used to test, set, or clear bits in the 00280 ** Db.flags field. 00281 */ 00282 #define DbHasProperty(D,I,P) (((D)->aDb[I].flags&(P))==(P)) 00283 #define DbHasAnyProperty(D,I,P) (((D)->aDb[I].flags&(P))!=0) 00284 #define DbSetProperty(D,I,P) (D)->aDb[I].flags|=(P) 00285 #define DbClearProperty(D,I,P) (D)->aDb[I].flags&=~(P) 00286 00287 /* 00288 ** Allowed values for the DB.flags field. 00289 ** 00290 ** The DB_Locked flag is set when the first OP_Transaction or OP_Checkpoint 00291 ** opcode is emitted for a database. This prevents multiple occurances 00292 ** of those opcodes for the same database in the same program. Similarly, 00293 ** the DB_Cookie flag is set when the OP_VerifyCookie opcode is emitted, 00294 ** and prevents duplicate OP_VerifyCookies from taking up space and slowing 00295 ** down execution. 00296 ** 00297 ** The DB_SchemaLoaded flag is set after the database schema has been 00298 ** read into internal hash tables. 00299 ** 00300 ** DB_UnresetViews means that one or more views have column names that 00301 ** have been filled out. If the schema changes, these column names might 00302 ** changes and so the view will need to be reset. 00303 */ 00304 #define DB_Locked 0x0001 /* OP_Transaction opcode has been emitted */ 00305 #define DB_Cookie 0x0002 /* OP_VerifyCookie opcode has been emiited */ 00306 #define DB_SchemaLoaded 0x0004 /* The schema has been loaded */ 00307 #define DB_UnresetViews 0x0008 /* Some views have defined column names */ 00308 00309 00310 /* 00311 ** Each database is an instance of the following structure. 00312 ** 00313 ** The sqlite.file_format is initialized by the database file 00314 ** and helps determines how the data in the database file is 00315 ** represented. This field allows newer versions of the library 00316 ** to read and write older databases. The various file formats 00317 ** are as follows: 00318 ** 00319 ** file_format==1 Version 2.1.0. 00320 ** file_format==2 Version 2.2.0. Add support for INTEGER PRIMARY KEY. 00321 ** file_format==3 Version 2.6.0. Fix empty-string index bug. 00322 ** file_format==4 Version 2.7.0. Add support for separate numeric and 00323 ** text datatypes. 00324 ** 00325 ** The sqlite.temp_store determines where temporary database files 00326 ** are stored. If 1, then a file is created to hold those tables. If 00327 ** 2, then they are held in memory. 0 means use the default value in 00328 ** the TEMP_STORE macro. 00329 ** 00330 ** The sqlite.lastRowid records the last insert rowid generated by an 00331 ** insert statement. Inserts on views do not affect its value. Each 00332 ** trigger has its own context, so that lastRowid can be updated inside 00333 ** triggers as usual. The previous value will be restored once the trigger 00334 ** exits. Upon entering a before or instead of trigger, lastRowid is no 00335 ** longer (since after version 2.8.12) reset to -1. 00336 ** 00337 ** The sqlite.nChange does not count changes within triggers and keeps no 00338 ** context. It is reset at start of sqlite_exec. 00339 ** The sqlite.lsChange represents the number of changes made by the last 00340 ** insert, update, or delete statement. It remains constant throughout the 00341 ** length of a statement and is then updated by OP_SetCounts. It keeps a 00342 ** context stack just like lastRowid so that the count of changes 00343 ** within a trigger is not seen outside the trigger. Changes to views do not 00344 ** affect the value of lsChange. 00345 ** The sqlite.csChange keeps track of the number of current changes (since 00346 ** the last statement) and is used to update sqlite_lsChange. 00347 */ 00348 struct sqlite { 00349 int nDb; /* Number of backends currently in use */ 00350 Db *aDb; /* All backends */ 00351 Db aDbStatic[2]; /* Static space for the 2 default backends */ 00352 int flags; /* Miscellanous flags. See below */ 00353 u8 file_format; /* What file format version is this database? */ 00354 u8 safety_level; /* How aggressive at synching data to disk */ 00355 u8 want_to_close; /* Close after all VDBEs are deallocated */ 00356 u8 temp_store; /* 1=file, 2=memory, 0=compile-time default */ 00357 u8 onError; /* Default conflict algorithm */ 00358 int next_cookie; /* Next value of aDb[0].schema_cookie */ 00359 int cache_size; /* Number of pages to use in the cache */ 00360 int nTable; /* Number of tables in the database */ 00361 void *pBusyArg; /* 1st Argument to the busy callback */ 00362 int (*xBusyCallback)(void *,const char*,int); /* The busy callback */ 00363 void *pCommitArg; /* Argument to xCommitCallback() */ 00364 int (*xCommitCallback)(void*);/* Invoked at every commit. */ 00365 Hash aFunc; /* All functions that can be in SQL exprs */ 00366 int lastRowid; /* ROWID of most recent insert (see above) */ 00367 int priorNewRowid; /* Last randomly generated ROWID */ 00368 int magic; /* Magic number for detect library misuse */ 00369 int nChange; /* Number of rows changed (see above) */ 00370 int lsChange; /* Last statement change count (see above) */ 00371 int csChange; /* Current statement change count (see above) */ 00372 struct sqliteInitInfo { /* Information used during initialization */ 00373 int iDb; /* When back is being initialized */ 00374 int newTnum; /* Rootpage of table being initialized */ 00375 u8 busy; /* TRUE if currently initializing */ 00376 } init; 00377 struct Vdbe *pVdbe; /* List of active virtual machines */ 00378 void (*xTrace)(void*,const char*); /* Trace function */ 00379 void *pTraceArg; /* Argument to the trace function */ 00380 #ifndef SQLITE_OMIT_AUTHORIZATION 00381 int (*xAuth)(void*,int,const char*,const char*,const char*,const char*); 00382 /* Access authorization function */ 00383 void *pAuthArg; /* 1st argument to the access auth function */ 00384 #endif 00385 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK 00386 int (*xProgress)(void *); /* The progress callback */ 00387 void *pProgressArg; /* Argument to the progress callback */ 00388 int nProgressOps; /* Number of opcodes for progress callback */ 00389 #endif 00390 }; 00391 00392 /* 00393 ** Possible values for the sqlite.flags and or Db.flags fields. 00394 ** 00395 ** On sqlite.flags, the SQLITE_InTrans value means that we have 00396 ** executed a BEGIN. On Db.flags, SQLITE_InTrans means a statement 00397 ** transaction is active on that particular database file. 00398 */ 00399 #define SQLITE_VdbeTrace 0x00000001 /* True to trace VDBE execution */ 00400 #define SQLITE_Initialized 0x00000002 /* True after initialization */ 00401 #define SQLITE_Interrupt 0x00000004 /* Cancel current operation */ 00402 #define SQLITE_InTrans 0x00000008 /* True if in a transaction */ 00403 #define SQLITE_InternChanges 0x00000010 /* Uncommitted Hash table changes */ 00404 #define SQLITE_FullColNames 0x00000020 /* Show full column names on SELECT */ 00405 #define SQLITE_ShortColNames 0x00000040 /* Show short columns names */ 00406 #define SQLITE_CountRows 0x00000080 /* Count rows changed by INSERT, */ 00407 /* DELETE, or UPDATE and return */ 00408 /* the count using a callback. */ 00409 #define SQLITE_NullCallback 0x00000100 /* Invoke the callback once if the */ 00410 /* result set is empty */ 00411 #define SQLITE_ReportTypes 0x00000200 /* Include information on datatypes */ 00412 /* in 4th argument of callback */ 00413 00414 /* 00415 ** Possible values for the sqlite.magic field. 00416 ** The numbers are obtained at random and have no special meaning, other 00417 ** than being distinct from one another. 00418 */ 00419 #define SQLITE_MAGIC_OPEN 0xa029a697 /* Database is open */ 00420 #define SQLITE_MAGIC_CLOSED 0x9f3c2d33 /* Database is closed */ 00421 #define SQLITE_MAGIC_BUSY 0xf03b7906 /* Database currently in use */ 00422 #define SQLITE_MAGIC_ERROR 0xb5357930 /* An SQLITE_MISUSE error occurred */ 00423 00424 /* 00425 ** Each SQL function is defined by an instance of the following 00426 ** structure. A pointer to this structure is stored in the sqlite.aFunc 00427 ** hash table. When multiple functions have the same name, the hash table 00428 ** points to a linked list of these structures. 00429 */ 00430 struct FuncDef { 00431 void (*xFunc)(sqlite_func*,int,const char**); /* Regular function */ 00432 void (*xStep)(sqlite_func*,int,const char**); /* Aggregate function step */ 00433 void (*xFinalize)(sqlite_func*); /* Aggregate function finializer */ 00434 signed char nArg; /* Number of arguments. -1 means unlimited */ 00435 signed char dataType; /* Arg that determines datatype. -1=NUMERIC, */ 00436 /* -2=TEXT. -3=SQLITE_ARGS */ 00437 u8 includeTypes; /* Add datatypes to args of xFunc and xStep */ 00438 void *pUserData; /* User data parameter */ 00439 FuncDef *pNext; /* Next function with same name */ 00440 }; 00441 00442 /* 00443 ** information about each column of an SQL table is held in an instance 00444 ** of this structure. 00445 */ 00446 struct Column { 00447 char *zName; /* Name of this column */ 00448 char *zDflt; /* Default value of this column */ 00449 char *zType; /* Data type for this column */ 00450 u8 notNull; /* True if there is a NOT NULL constraint */ 00451 u8 isPrimKey; /* True if this column is part of the PRIMARY KEY */ 00452 u8 sortOrder; /* Some combination of SQLITE_SO_... values */ 00453 u8 dottedName; /* True if zName contains a "." character */ 00454 }; 00455 00456 /* 00457 ** The allowed sort orders. 00458 ** 00459 ** The TEXT and NUM values use bits that do not overlap with DESC and ASC. 00460 ** That way the two can be combined into a single number. 00461 */ 00462 #define SQLITE_SO_UNK 0 /* Use the default collating type. (SCT_NUM) */ 00463 #define SQLITE_SO_TEXT 2 /* Sort using memcmp() */ 00464 #define SQLITE_SO_NUM 4 /* Sort using sqliteCompare() */ 00465 #define SQLITE_SO_TYPEMASK 6 /* Mask to extract the collating sequence */ 00466 #define SQLITE_SO_ASC 0 /* Sort in ascending order */ 00467 #define SQLITE_SO_DESC 1 /* Sort in descending order */ 00468 #define SQLITE_SO_DIRMASK 1 /* Mask to extract the sort direction */ 00469 00470 /* 00471 ** Each SQL table is represented in memory by an instance of the 00472 ** following structure. 00473 ** 00474 ** Table.zName is the name of the table. The case of the original 00475 ** CREATE TABLE statement is stored, but case is not significant for 00476 ** comparisons. 00477 ** 00478 ** Table.nCol is the number of columns in this table. Table.aCol is a 00479 ** pointer to an array of Column structures, one for each column. 00480 ** 00481 ** If the table has an INTEGER PRIMARY KEY, then Table.iPKey is the index of 00482 ** the column that is that key. Otherwise Table.iPKey is negative. Note 00483 ** that the datatype of the PRIMARY KEY must be INTEGER for this field to 00484 ** be set. An INTEGER PRIMARY KEY is used as the rowid for each row of 00485 ** the table. If a table has no INTEGER PRIMARY KEY, then a random rowid 00486 ** is generated for each row of the table. Table.hasPrimKey is true if 00487 ** the table has any PRIMARY KEY, INTEGER or otherwise. 00488 ** 00489 ** Table.tnum is the page number for the root BTree page of the table in the 00490 ** database file. If Table.iDb is the index of the database table backend 00491 ** in sqlite.aDb[]. 0 is for the main database and 1 is for the file that 00492 ** holds temporary tables and indices. If Table.isTransient 00493 ** is true, then the table is stored in a file that is automatically deleted 00494 ** when the VDBE cursor to the table is closed. In this case Table.tnum 00495 ** refers VDBE cursor number that holds the table open, not to the root 00496 ** page number. Transient tables are used to hold the results of a 00497 ** sub-query that appears instead of a real table name in the FROM clause 00498 ** of a SELECT statement. 00499 */ 00500 struct Table { 00501 char *zName; /* Name of the table */ 00502 int nCol; /* Number of columns in this table */ 00503 Column *aCol; /* Information about each column */ 00504 int iPKey; /* If not less then 0, use aCol[iPKey] as the primary key */ 00505 Index *pIndex; /* List of SQL indexes on this table. */ 00506 int tnum; /* Root BTree node for this table (see note above) */ 00507 Select *pSelect; /* NULL for tables. Points to definition if a view. */ 00508 u8 readOnly; /* True if this table should not be written by the user */ 00509 u8 iDb; /* Index into sqlite.aDb[] of the backend for this table */ 00510 u8 isTransient; /* True if automatically deleted when VDBE finishes */ 00511 u8 hasPrimKey; /* True if there exists a primary key */ 00512 u8 keyConf; /* What to do in case of uniqueness conflict on iPKey */ 00513 Trigger *pTrigger; /* List of SQL triggers on this table */ 00514 FKey *pFKey; /* Linked list of all foreign keys in this table */ 00515 }; 00516 00517 /* 00518 ** Each foreign key constraint is an instance of the following structure. 00519 ** 00520 ** A foreign key is associated with two tables. The "from" table is 00521 ** the table that contains the REFERENCES clause that creates the foreign 00522 ** key. The "to" table is the table that is named in the REFERENCES clause. 00523 ** Consider this example: 00524 ** 00525 ** CREATE TABLE ex1( 00526 ** a INTEGER PRIMARY KEY, 00527 ** b INTEGER CONSTRAINT fk1 REFERENCES ex2(x) 00528 ** ); 00529 ** 00530 ** For foreign key "fk1", the from-table is "ex1" and the to-table is "ex2". 00531 ** 00532 ** Each REFERENCES clause generates an instance of the following structure 00533 ** which is attached to the from-table. The to-table need not exist when 00534 ** the from-table is created. The existance of the to-table is not checked 00535 ** until an attempt is made to insert data into the from-table. 00536 ** 00537 ** The sqlite.aFKey hash table stores pointers to this structure 00538 ** given the name of a to-table. For each to-table, all foreign keys 00539 ** associated with that table are on a linked list using the FKey.pNextTo 00540 ** field. 00541 */ 00542 struct FKey { 00543 Table *pFrom; /* The table that constains the REFERENCES clause */ 00544 FKey *pNextFrom; /* Next foreign key in pFrom */ 00545 char *zTo; /* Name of table that the key points to */ 00546 FKey *pNextTo; /* Next foreign key that points to zTo */ 00547 int nCol; /* Number of columns in this key */ 00548 struct sColMap { /* Mapping of columns in pFrom to columns in zTo */ 00549 int iFrom; /* Index of column in pFrom */ 00550 char *zCol; /* Name of column in zTo. If 0 use PRIMARY KEY */ 00551 } *aCol; /* One entry for each of nCol column s */ 00552 u8 isDeferred; /* True if constraint checking is deferred till COMMIT */ 00553 u8 updateConf; /* How to resolve conflicts that occur on UPDATE */ 00554 u8 deleteConf; /* How to resolve conflicts that occur on DELETE */ 00555 u8 insertConf; /* How to resolve conflicts that occur on INSERT */ 00556 }; 00557 00558 /* 00559 ** SQLite supports many different ways to resolve a contraint 00560 ** error. ROLLBACK processing means that a constraint violation 00561 ** causes the operation in process to fail and for the current transaction 00562 ** to be rolled back. ABORT processing means the operation in process 00563 ** fails and any prior changes from that one operation are backed out, 00564 ** but the transaction is not rolled back. FAIL processing means that 00565 ** the operation in progress stops and returns an error code. But prior 00566 ** changes due to the same operation are not backed out and no rollback 00567 ** occurs. IGNORE means that the particular row that caused the constraint 00568 ** error is not inserted or updated. Processing continues and no error 00569 ** is returned. REPLACE means that preexisting database rows that caused 00570 ** a UNIQUE constraint violation are removed so that the new insert or 00571 ** update can proceed. Processing continues and no error is reported. 00572 ** 00573 ** RESTRICT, SETNULL, and CASCADE actions apply only to foreign keys. 00574 ** RESTRICT is the same as ABORT for IMMEDIATE foreign keys and the 00575 ** same as ROLLBACK for DEFERRED keys. SETNULL means that the foreign 00576 ** key is set to NULL. CASCADE means that a DELETE or UPDATE of the 00577 ** referenced table row is propagated into the row that holds the 00578 ** foreign key. 00579 ** 00580 ** The following symbolic values are used to record which type 00581 ** of action to take. 00582 */ 00583 #define OE_None 0 /* There is no constraint to check */ 00584 #define OE_Rollback 1 /* Fail the operation and rollback the transaction */ 00585 #define OE_Abort 2 /* Back out changes but do no rollback transaction */ 00586 #define OE_Fail 3 /* Stop the operation but leave all prior changes */ 00587 #define OE_Ignore 4 /* Ignore the error. Do not do the INSERT or UPDATE */ 00588 #define OE_Replace 5 /* Delete existing record, then do INSERT or UPDATE */ 00589 00590 #define OE_Restrict 6 /* OE_Abort for IMMEDIATE, OE_Rollback for DEFERRED */ 00591 #define OE_SetNull 7 /* Set the foreign key value to NULL */ 00592 #define OE_SetDflt 8 /* Set the foreign key value to its default */ 00593 #define OE_Cascade 9 /* Cascade the changes */ 00594 00595 #define OE_Default 99 /* Do whatever the default action is */ 00596 00597 /* 00598 ** Each SQL index is represented in memory by an 00599 ** instance of the following structure. 00600 ** 00601 ** The columns of the table that are to be indexed are described 00602 ** by the aiColumn[] field of this structure. For example, suppose 00603 ** we have the following table and index: 00604 ** 00605 ** CREATE TABLE Ex1(c1 int, c2 int, c3 text); 00606 ** CREATE INDEX Ex2 ON Ex1(c3,c1); 00607 ** 00608 ** In the Table structure describing Ex1, nCol==3 because there are 00609 ** three columns in the table. In the Index structure describing 00610 ** Ex2, nColumn==2 since 2 of the 3 columns of Ex1 are indexed. 00611 ** The value of aiColumn is {2, 0}. aiColumn[0]==2 because the 00612 ** first column to be indexed (c3) has an index of 2 in Ex1.aCol[]. 00613 ** The second column to be indexed (c1) has an index of 0 in 00614 ** Ex1.aCol[], hence Ex2.aiColumn[1]==0. 00615 ** 00616 ** The Index.onError field determines whether or not the indexed columns 00617 ** must be unique and what to do if they are not. When Index.onError=OE_None, 00618 ** it means this is not a unique index. Otherwise it is a unique index 00619 ** and the value of Index.onError indicate the which conflict resolution 00620 ** algorithm to employ whenever an attempt is made to insert a non-unique 00621 ** element. 00622 */ 00623 struct Index { 00624 char *zName; /* Name of this index */ 00625 int nColumn; /* Number of columns in the table used by this index */ 00626 int *aiColumn; /* Which columns are used by this index. 1st is 0 */ 00627 Table *pTable; /* The SQL table being indexed */ 00628 int tnum; /* Page containing root of this index in database file */ 00629 u8 onError; /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */ 00630 u8 autoIndex; /* True if is automatically created (ex: by UNIQUE) */ 00631 u8 iDb; /* Index in sqlite.aDb[] of where this index is stored */ 00632 Index *pNext; /* The next index associated with the same table */ 00633 }; 00634 00635 /* 00636 ** Each token coming out of the lexer is an instance of 00637 ** this structure. Tokens are also used as part of an expression. 00638 ** 00639 ** Note if Token.z==0 then Token.dyn and Token.n are undefined and 00640 ** may contain random values. Do not make any assuptions about Token.dyn 00641 ** and Token.n when Token.z==0. 00642 */ 00643 struct Token { 00644 const char *z; /* Text of the token. Not NULL-terminated! */ 00645 unsigned dyn : 1; /* True for malloced memory, false for static */ 00646 unsigned n : 31; /* Number of characters in this token */ 00647 }; 00648 00649 /* 00650 ** Each node of an expression in the parse tree is an instance 00651 ** of this structure. 00652 ** 00653 ** Expr.op is the opcode. The integer parser token codes are reused 00654 ** as opcodes here. For example, the parser defines TK_GE to be an integer 00655 ** code representing the ">=" operator. This same integer code is reused 00656 ** to represent the greater-than-or-equal-to operator in the expression 00657 ** tree. 00658 ** 00659 ** Expr.pRight and Expr.pLeft are subexpressions. Expr.pList is a list 00660 ** of argument if the expression is a function. 00661 ** 00662 ** Expr.token is the operator token for this node. For some expressions 00663 ** that have subexpressions, Expr.token can be the complete text that gave 00664 ** rise to the Expr. In the latter case, the token is marked as being 00665 ** a compound token. 00666 ** 00667 ** An expression of the form ID or ID.ID refers to a column in a table. 00668 ** For such expressions, Expr.op is set to TK_COLUMN and Expr.iTable is 00669 ** the integer cursor number of a VDBE cursor pointing to that table and 00670 ** Expr.iColumn is the column number for the specific column. If the 00671 ** expression is used as a result in an aggregate SELECT, then the 00672 ** value is also stored in the Expr.iAgg column in the aggregate so that 00673 ** it can be accessed after all aggregates are computed. 00674 ** 00675 ** If the expression is a function, the Expr.iTable is an integer code 00676 ** representing which function. If the expression is an unbound variable 00677 ** marker (a question mark character '?' in the original SQL) then the 00678 ** Expr.iTable holds the index number for that variable. 00679 ** 00680 ** The Expr.pSelect field points to a SELECT statement. The SELECT might 00681 ** be the right operand of an IN operator. Or, if a scalar SELECT appears 00682 ** in an expression the opcode is TK_SELECT and Expr.pSelect is the only 00683 ** operand. 00684 */ 00685 struct Expr { 00686 u8 op; /* Operation performed by this node */ 00687 u8 dataType; /* Either SQLITE_SO_TEXT or SQLITE_SO_NUM */ 00688 u8 iDb; /* Database referenced by this expression */ 00689 u8 flags; /* Various flags. See below */ 00690 Expr *pLeft, *pRight; /* Left and right subnodes */ 00691 ExprList *pList; /* A list of expressions used as function arguments 00692 ** or in "<expr> IN (<expr-list)" */ 00693 Token token; /* An operand token */ 00694 Token span; /* Complete text of the expression */ 00695 int iTable, iColumn; /* When op==TK_COLUMN, then this expr node means the 00696 ** iColumn-th field of the iTable-th table. */ 00697 int iAgg; /* When op==TK_COLUMN and pParse->useAgg==TRUE, pull 00698 ** result from the iAgg-th element of the aggregator */ 00699 Select *pSelect; /* When the expression is a sub-select. Also the 00700 ** right side of "<expr> IN (<select>)" */ 00701 }; 00702 00703 /* 00704 ** The following are the meanings of bits in the Expr.flags field. 00705 */ 00706 #define EP_FromJoin 0x0001 /* Originated in ON or USING clause of a join */ 00707 00708 /* 00709 ** These macros can be used to test, set, or clear bits in the 00710 ** Expr.flags field. 00711 */ 00712 #define ExprHasProperty(E,P) (((E)->flags&(P))==(P)) 00713 #define ExprHasAnyProperty(E,P) (((E)->flags&(P))!=0) 00714 #define ExprSetProperty(E,P) (E)->flags|=(P) 00715 #define ExprClearProperty(E,P) (E)->flags&=~(P) 00716 00717 /* 00718 ** A list of expressions. Each expression may optionally have a 00719 ** name. An expr/name combination can be used in several ways, such 00720 ** as the list of "expr AS ID" fields following a "SELECT" or in the 00721 ** list of "ID = expr" items in an UPDATE. A list of expressions can 00722 ** also be used as the argument to a function, in which case the a.zName 00723 ** field is not used. 00724 */ 00725 struct ExprList { 00726 int nExpr; /* Number of expressions on the list */ 00727 int nAlloc; /* Number of entries allocated below */ 00728 struct ExprList_item { 00729 Expr *pExpr; /* The list of expressions */ 00730 char *zName; /* Token associated with this expression */ 00731 u8 sortOrder; /* 1 for DESC or 0 for ASC */ 00732 u8 isAgg; /* True if this is an aggregate like count(*) */ 00733 u8 done; /* A flag to indicate when processing is finished */ 00734 } *a; /* One entry for each expression */ 00735 }; 00736 00737 /* 00738 ** An instance of this structure can hold a simple list of identifiers, 00739 ** such as the list "a,b,c" in the following statements: 00740 ** 00741 ** INSERT INTO t(a,b,c) VALUES ...; 00742 ** CREATE INDEX idx ON t(a,b,c); 00743 ** CREATE TRIGGER trig BEFORE UPDATE ON t(a,b,c) ...; 00744 ** 00745 ** The IdList.a.idx field is used when the IdList represents the list of 00746 ** column names after a table name in an INSERT statement. In the statement 00747 ** 00748 ** INSERT INTO t(a,b,c) ... 00749 ** 00750 ** If "a" is the k-th column of table "t", then IdList.a[0].idx==k. 00751 */ 00752 struct IdList { 00753 int nId; /* Number of identifiers on the list */ 00754 int nAlloc; /* Number of entries allocated for a[] below */ 00755 struct IdList_item { 00756 char *zName; /* Name of the identifier */ 00757 int idx; /* Index in some Table.aCol[] of a column named zName */ 00758 } *a; 00759 }; 00760 00761 /* 00762 ** The following structure describes the FROM clause of a SELECT statement. 00763 ** Each table or subquery in the FROM clause is a separate element of 00764 ** the SrcList.a[] array. 00765 ** 00766 ** With the addition of multiple database support, the following structure 00767 ** can also be used to describe a particular table such as the table that 00768 ** is modified by an INSERT, DELETE, or UPDATE statement. In standard SQL, 00769 ** such a table must be a simple name: ID. But in SQLite, the table can 00770 ** now be identified by a database name, a dot, then the table name: ID.ID. 00771 */ 00772 struct SrcList { 00773 i16 nSrc; /* Number of tables or subqueries in the FROM clause */ 00774 i16 nAlloc; /* Number of entries allocated in a[] below */ 00775 struct SrcList_item { 00776 char *zDatabase; /* Name of database holding this table */ 00777 char *zName; /* Name of the table */ 00778 char *zAlias; /* The "B" part of a "A AS B" phrase. zName is the "A" */ 00779 Table *pTab; /* An SQL table corresponding to zName */ 00780 Select *pSelect; /* A SELECT statement used in place of a table name */ 00781 int jointype; /* Type of join between this table and the next */ 00782 int iCursor; /* The VDBE cursor number used to access this table */ 00783 Expr *pOn; /* The ON clause of a join */ 00784 IdList *pUsing; /* The USING clause of a join */ 00785 } a[1]; /* One entry for each identifier on the list */ 00786 }; 00787 00788 /* 00789 ** Permitted values of the SrcList.a.jointype field 00790 */ 00791 #define JT_INNER 0x0001 /* Any kind of inner or cross join */ 00792 #define JT_NATURAL 0x0002 /* True for a "natural" join */ 00793 #define JT_LEFT 0x0004 /* Left outer join */ 00794 #define JT_RIGHT 0x0008 /* Right outer join */ 00795 #define JT_OUTER 0x0010 /* The "OUTER" keyword is present */ 00796 #define JT_ERROR 0x0020 /* unknown or unsupported join type */ 00797 00798 /* 00799 ** For each nested loop in a WHERE clause implementation, the WhereInfo 00800 ** structure contains a single instance of this structure. This structure 00801 ** is intended to be private the the where.c module and should not be 00802 ** access or modified by other modules. 00803 */ 00804 struct WhereLevel { 00805 int iMem; /* Memory cell used by this level */ 00806 Index *pIdx; /* Index used */ 00807 int iCur; /* Cursor number used for this index */ 00808 int score; /* How well this indexed scored */ 00809 int brk; /* Jump here to break out of the loop */ 00810 int cont; /* Jump here to continue with the next loop cycle */ 00811 int op, p1, p2; /* Opcode used to terminate the loop */ 00812 int iLeftJoin; /* Memory cell used to implement LEFT OUTER JOIN */ 00813 int top; /* First instruction of interior of the loop */ 00814 int inOp, inP1, inP2;/* Opcode used to implement an IN operator */ 00815 int bRev; /* Do the scan in the reverse direction */ 00816 }; 00817 00818 /* 00819 ** The WHERE clause processing routine has two halves. The 00820 ** first part does the start of the WHERE loop and the second 00821 ** half does the tail of the WHERE loop. An instance of 00822 ** this structure is returned by the first half and passed 00823 ** into the second half to give some continuity. 00824 */ 00825 struct WhereInfo { 00826 Parse *pParse; 00827 SrcList *pTabList; /* List of tables in the join */ 00828 int iContinue; /* Jump here to continue with next record */ 00829 int iBreak; /* Jump here to break out of the loop */ 00830 int nLevel; /* Number of nested loop */ 00831 int savedNTab; /* Value of pParse->nTab before WhereBegin() */ 00832 int peakNTab; /* Value of pParse->nTab after WhereBegin() */ 00833 WhereLevel a[1]; /* Information about each nest loop in the WHERE */ 00834 }; 00835 00836 /* 00837 ** An instance of the following structure contains all information 00838 ** needed to generate code for a single SELECT statement. 00839 ** 00840 ** The zSelect field is used when the Select structure must be persistent. 00841 ** Normally, the expression tree points to tokens in the original input 00842 ** string that encodes the select. But if the Select structure must live 00843 ** longer than its input string (for example when it is used to describe 00844 ** a VIEW) we have to make a copy of the input string so that the nodes 00845 ** of the expression tree will have something to point to. zSelect is used 00846 ** to hold that copy. 00847 ** 00848 ** nLimit is set to -1 if there is no LIMIT clause. nOffset is set to 0. 00849 ** If there is a LIMIT clause, the parser sets nLimit to the value of the 00850 ** limit and nOffset to the value of the offset (or 0 if there is not 00851 ** offset). But later on, nLimit and nOffset become the memory locations 00852 ** in the VDBE that record the limit and offset counters. 00853 */ 00854 struct Select { 00855 ExprList *pEList; /* The fields of the result */ 00856 u8 op; /* One of: TK_UNION TK_ALL TK_INTERSECT TK_EXCEPT */ 00857 u8 isDistinct; /* True if the DISTINCT keyword is present */ 00858 SrcList *pSrc; /* The FROM clause */ 00859 Expr *pWhere; /* The WHERE clause */ 00860 ExprList *pGroupBy; /* The GROUP BY clause */ 00861 Expr *pHaving; /* The HAVING clause */ 00862 ExprList *pOrderBy; /* The ORDER BY clause */ 00863 Select *pPrior; /* Prior select in a compound select statement */ 00864 int nLimit, nOffset; /* LIMIT and OFFSET values. -1 means not used */ 00865 int iLimit, iOffset; /* Memory registers holding LIMIT & OFFSET counters */ 00866 char *zSelect; /* Complete text of the SELECT command */ 00867 }; 00868 00869 /* 00870 ** The results of a select can be distributed in several ways. 00871 */ 00872 #define SRT_Callback 1 /* Invoke a callback with each row of result */ 00873 #define SRT_Mem 2 /* Store result in a memory cell */ 00874 #define SRT_Set 3 /* Store result as unique keys in a table */ 00875 #define SRT_Union 5 /* Store result as keys in a table */ 00876 #define SRT_Except 6 /* Remove result from a UNION table */ 00877 #define SRT_Table 7 /* Store result as data with a unique key */ 00878 #define SRT_TempTable 8 /* Store result in a trasient table */ 00879 #define SRT_Discard 9 /* Do not save the results anywhere */ 00880 #define SRT_Sorter 10 /* Store results in the sorter */ 00881 #define SRT_Subroutine 11 /* Call a subroutine to handle results */ 00882 00883 /* 00884 ** When a SELECT uses aggregate functions (like "count(*)" or "avg(f1)") 00885 ** we have to do some additional analysis of expressions. An instance 00886 ** of the following structure holds information about a single subexpression 00887 ** somewhere in the SELECT statement. An array of these structures holds 00888 ** all the information we need to generate code for aggregate 00889 ** expressions. 00890 ** 00891 ** Note that when analyzing a SELECT containing aggregates, both 00892 ** non-aggregate field variables and aggregate functions are stored 00893 ** in the AggExpr array of the Parser structure. 00894 ** 00895 ** The pExpr field points to an expression that is part of either the 00896 ** field list, the GROUP BY clause, the HAVING clause or the ORDER BY 00897 ** clause. The expression will be freed when those clauses are cleaned 00898 ** up. Do not try to delete the expression attached to AggExpr.pExpr. 00899 ** 00900 ** If AggExpr.pExpr==0, that means the expression is "count(*)". 00901 */ 00902 struct AggExpr { 00903 int isAgg; /* if TRUE contains an aggregate function */ 00904 Expr *pExpr; /* The expression */ 00905 FuncDef *pFunc; /* Information about the aggregate function */ 00906 }; 00907 00908 /* 00909 ** An SQL parser context. A copy of this structure is passed through 00910 ** the parser and down into all the parser action routine in order to 00911 ** carry around information that is global to the entire parse. 00912 */ 00913 struct Parse { 00914 sqlite *db; /* The main database structure */ 00915 int rc; /* Return code from execution */ 00916 char *zErrMsg; /* An error message */ 00917 Token sErrToken; /* The token at which the error occurred */ 00918 Token sFirstToken; /* The first token parsed */ 00919 Token sLastToken; /* The last token parsed */ 00920 const char *zTail; /* All SQL text past the last semicolon parsed */ 00921 Table *pNewTable; /* A table being constructed by CREATE TABLE */ 00922 Vdbe *pVdbe; /* An engine for executing database bytecode */ 00923 u8 colNamesSet; /* TRUE after OP_ColumnName has been issued to pVdbe */ 00924 u8 explain; /* True if the EXPLAIN flag is found on the query */ 00925 u8 nameClash; /* A permanent table name clashes with temp table name */ 00926 u8 useAgg; /* If true, extract field values from the aggregator 00927 ** while generating expressions. Normally false */ 00928 int nErr; /* Number of errors seen */ 00929 int nTab; /* Number of previously allocated VDBE cursors */ 00930 int nMem; /* Number of memory cells used so far */ 00931 int nSet; /* Number of sets used so far */ 00932 int nAgg; /* Number of aggregate expressions */ 00933 int nVar; /* Number of '?' variables seen in the SQL so far */ 00934 AggExpr *aAgg; /* An array of aggregate expressions */ 00935 const char *zAuthContext; /* The 6th parameter to db->xAuth callbacks */ 00936 Trigger *pNewTrigger; /* Trigger under construct by a CREATE TRIGGER */ 00937 TriggerStack *trigStack; /* Trigger actions being coded */ 00938 }; 00939 00940 /* 00941 ** An instance of the following structure can be declared on a stack and used 00942 ** to save the Parse.zAuthContext value so that it can be restored later. 00943 */ 00944 struct AuthContext { 00945 const char *zAuthContext; /* Put saved Parse.zAuthContext here */ 00946 Parse *pParse; /* The Parse structure */ 00947 }; 00948 00949 /* 00950 ** Bitfield flags for P2 value in OP_PutIntKey and OP_Delete 00951 */ 00952 #define OPFLAG_NCHANGE 1 /* Set to update db->nChange */ 00953 #define OPFLAG_LASTROWID 2 /* Set to update db->lastRowid */ 00954 #define OPFLAG_CSCHANGE 4 /* Set to update db->csChange */ 00955 00956 /* 00957 * Each trigger present in the database schema is stored as an instance of 00958 * struct Trigger. 00959 * 00960 * Pointers to instances of struct Trigger are stored in two ways. 00961 * 1. In the "trigHash" hash table (part of the sqlite* that represents the 00962 * database). This allows Trigger structures to be retrieved by name. 00963 * 2. All triggers associated with a single table form a linked list, using the 00964 * pNext member of struct Trigger. A pointer to the first element of the 00965 * linked list is stored as the "pTrigger" member of the associated 00966 * struct Table. 00967 * 00968 * The "step_list" member points to the first element of a linked list 00969 * containing the SQL statements specified as the trigger program. 00970 */ 00971 struct Trigger { 00972 char *name; /* The name of the trigger */ 00973 char *table; /* The table or view to which the trigger applies */ 00974 u8 iDb; /* Database containing this trigger */ 00975 u8 iTabDb; /* Database containing Trigger.table */ 00976 u8 op; /* One of TK_DELETE, TK_UPDATE, TK_INSERT */ 00977 u8 tr_tm; /* One of TK_BEFORE, TK_AFTER */ 00978 Expr *pWhen; /* The WHEN clause of the expresion (may be NULL) */ 00979 IdList *pColumns; /* If this is an UPDATE OF <column-list> trigger, 00980 the <column-list> is stored here */ 00981 int foreach; /* One of TK_ROW or TK_STATEMENT */ 00982 Token nameToken; /* Token containing zName. Use during parsing only */ 00983 00984 TriggerStep *step_list; /* Link list of trigger program steps */ 00985 Trigger *pNext; /* Next trigger associated with the table */ 00986 }; 00987 00988 /* 00989 * An instance of struct TriggerStep is used to store a single SQL statement 00990 * that is a part of a trigger-program. 00991 * 00992 * Instances of struct TriggerStep are stored in a singly linked list (linked 00993 * using the "pNext" member) referenced by the "step_list" member of the 00994 * associated struct Trigger instance. The first element of the linked list is 00995 * the first step of the trigger-program. 00996 * 00997 * The "op" member indicates whether this is a "DELETE", "INSERT", "UPDATE" or 00998 * "SELECT" statement. The meanings of the other members is determined by the 00999 * value of "op" as follows: 01000 * 01001 * (op == TK_INSERT) 01002 * orconf -> stores the ON CONFLICT algorithm 01003 * pSelect -> If this is an INSERT INTO ... SELECT ... statement, then 01004 * this stores a pointer to the SELECT statement. Otherwise NULL. 01005 * target -> A token holding the name of the table to insert into. 01006 * pExprList -> If this is an INSERT INTO ... VALUES ... statement, then 01007 * this stores values to be inserted. Otherwise NULL. 01008 * pIdList -> If this is an INSERT INTO ... (<column-names>) VALUES ... 01009 * statement, then this stores the column-names to be 01010 * inserted into. 01011 * 01012 * (op == TK_DELETE) 01013 * target -> A token holding the name of the table to delete from. 01014 * pWhere -> The WHERE clause of the DELETE statement if one is specified. 01015 * Otherwise NULL. 01016 * 01017 * (op == TK_UPDATE) 01018 * target -> A token holding the name of the table to update rows of. 01019 * pWhere -> The WHERE clause of the UPDATE statement if one is specified. 01020 * Otherwise NULL. 01021 * pExprList -> A list of the columns to update and the expressions to update 01022 * them to. See sqliteUpdate() documentation of "pChanges" 01023 * argument. 01024 * 01025 */ 01026 struct TriggerStep { 01027 int op; /* One of TK_DELETE, TK_UPDATE, TK_INSERT, TK_SELECT */ 01028 int orconf; /* OE_Rollback etc. */ 01029 Trigger *pTrig; /* The trigger that this step is a part of */ 01030 01031 Select *pSelect; /* Valid for SELECT and sometimes 01032 INSERT steps (when pExprList == 0) */ 01033 Token target; /* Valid for DELETE, UPDATE, INSERT steps */ 01034 Expr *pWhere; /* Valid for DELETE, UPDATE steps */ 01035 ExprList *pExprList; /* Valid for UPDATE statements and sometimes 01036 INSERT steps (when pSelect == 0) */ 01037 IdList *pIdList; /* Valid for INSERT statements only */ 01038 01039 TriggerStep * pNext; /* Next in the link-list */ 01040 }; 01041 01042 /* 01043 * An instance of struct TriggerStack stores information required during code 01044 * generation of a single trigger program. While the trigger program is being 01045 * coded, its associated TriggerStack instance is pointed to by the 01046 * "pTriggerStack" member of the Parse structure. 01047 * 01048 * The pTab member points to the table that triggers are being coded on. The 01049 * newIdx member contains the index of the vdbe cursor that points at the temp 01050 * table that stores the new.* references. If new.* references are not valid 01051 * for the trigger being coded (for example an ON DELETE trigger), then newIdx 01052 * is set to -1. The oldIdx member is analogous to newIdx, for old.* references. 01053 * 01054 * The ON CONFLICT policy to be used for the trigger program steps is stored 01055 * as the orconf member. If this is OE_Default, then the ON CONFLICT clause 01056 * specified for individual triggers steps is used. 01057 * 01058 * struct TriggerStack has a "pNext" member, to allow linked lists to be 01059 * constructed. When coding nested triggers (triggers fired by other triggers) 01060 * each nested trigger stores its parent trigger's TriggerStack as the "pNext" 01061 * pointer. Once the nested trigger has been coded, the pNext value is restored 01062 * to the pTriggerStack member of the Parse stucture and coding of the parent 01063 * trigger continues. 01064 * 01065 * Before a nested trigger is coded, the linked list pointed to by the 01066 * pTriggerStack is scanned to ensure that the trigger is not about to be coded 01067 * recursively. If this condition is detected, the nested trigger is not coded. 01068 */ 01069 struct TriggerStack { 01070 Table *pTab; /* Table that triggers are currently being coded on */ 01071 int newIdx; /* Index of vdbe cursor to "new" temp table */ 01072 int oldIdx; /* Index of vdbe cursor to "old" temp table */ 01073 int orconf; /* Current orconf policy */ 01074 int ignoreJump; /* where to jump to for a RAISE(IGNORE) */ 01075 Trigger *pTrigger; /* The trigger currently being coded */ 01076 TriggerStack *pNext; /* Next trigger down on the trigger stack */ 01077 }; 01078 01079 /* 01080 ** The following structure contains information used by the sqliteFix... 01081 ** routines as they walk the parse tree to make database references 01082 ** explicit. 01083 */ 01084 typedef struct DbFixer DbFixer; 01085 struct DbFixer { 01086 Parse *pParse; /* The parsing context. Error messages written here */ 01087 const char *zDb; /* Make sure all objects are contained in this database */ 01088 const char *zType; /* Type of the container - used for error messages */ 01089 const Token *pName; /* Name of the container - used for error messages */ 01090 }; 01091 01092 /* 01093 * This global flag is set for performance testing of triggers. When it is set 01094 * SQLite will perform the overhead of building new and old trigger references 01095 * even when no triggers exist 01096 */ 01097 extern int always_code_trigger_setup; 01098 01099 /* 01100 ** Internal function prototypes 01101 */ 01102 int sqliteStrICmp(const char *, const char *); 01103 int sqliteStrNICmp(const char *, const char *, int); 01104 int sqliteHashNoCase(const char *, int); 01105 int sqliteIsNumber(const char*); 01106 int sqliteCompare(const char *, const char *); 01107 int sqliteSortCompare(const char *, const char *); 01108 void sqliteRealToSortable(double r, char *); 01109 #ifdef MEMORY_DEBUG 01110 void *sqliteMalloc_(int,int,char*,int); 01111 void sqliteFree_(void*,char*,int); 01112 void *sqliteRealloc_(void*,int,char*,int); 01113 char *sqliteStrDup_(const char*,char*,int); 01114 char *sqliteStrNDup_(const char*, int,char*,int); 01115 void sqliteCheckMemory(void*,int); 01116 #else 01117 void *sqliteMalloc(int); 01118 void *sqliteMallocRaw(int); 01119 void sqliteFree(void*); 01120 void *sqliteRealloc(void*,int); 01121 char *sqliteStrDup(const char*); 01122 char *sqliteStrNDup(const char*, int); 01123 # define sqliteCheckMemory(a,b) 01124 #endif 01125 char *sqliteMPrintf(const char*, ...); 01126 char *sqliteVMPrintf(const char*, va_list); 01127 void sqliteSetString(char **, ...); 01128 void sqliteSetNString(char **, ...); 01129 void sqliteErrorMsg(Parse*, const char*, ...); 01130 void sqliteDequote(char*); 01131 int sqliteKeywordCode(const char*, int); 01132 int sqliteRunParser(Parse*, const char*, char **); 01133 void sqliteExec(Parse*); 01134 Expr *sqliteExpr(int, Expr*, Expr*, Token*); 01135 void sqliteExprSpan(Expr*,Token*,Token*); 01136 Expr *sqliteExprFunction(ExprList*, Token*); 01137 void sqliteExprDelete(Expr*); 01138 ExprList *sqliteExprListAppend(ExprList*,Expr*,Token*); 01139 void sqliteExprListDelete(ExprList*); 01140 int sqliteInit(sqlite*, char**); 01141 void sqlitePragma(Parse*,Token*,Token*,int); 01142 void sqliteResetInternalSchema(sqlite*, int); 01143 void sqliteBeginParse(Parse*,int); 01144 void sqliteRollbackInternalChanges(sqlite*); 01145 void sqliteCommitInternalChanges(sqlite*); 01146 Table *sqliteResultSetOfSelect(Parse*,char*,Select*); 01147 void sqliteOpenMasterTable(Vdbe *v, int); 01148 void sqliteStartTable(Parse*,Token*,Token*,int,int); 01149 void sqliteAddColumn(Parse*,Token*); 01150 void sqliteAddNotNull(Parse*, int); 01151 void sqliteAddPrimaryKey(Parse*, IdList*, int); 01152 void sqliteAddColumnType(Parse*,Token*,Token*); 01153 void sqliteAddDefaultValue(Parse*,Token*,int); 01154 int sqliteCollateType(const char*, int); 01155 void sqliteAddCollateType(Parse*, int); 01156 void sqliteEndTable(Parse*,Token*,Select*); 01157 void sqliteCreateView(Parse*,Token*,Token*,Select*,int); 01158 int sqliteViewGetColumnNames(Parse*,Table*); 01159 void sqliteDropTable(Parse*, Token*, int); 01160 void sqliteDeleteTable(sqlite*, Table*); 01161 void sqliteInsert(Parse*, SrcList*, ExprList*, Select*, IdList*, int); 01162 IdList *sqliteIdListAppend(IdList*, Token*); 01163 int sqliteIdListIndex(IdList*,const char*); 01164 SrcList *sqliteSrcListAppend(SrcList*, Token*, Token*); 01165 void sqliteSrcListAddAlias(SrcList*, Token*); 01166 void sqliteSrcListAssignCursors(Parse*, SrcList*); 01167 void sqliteIdListDelete(IdList*); 01168 void sqliteSrcListDelete(SrcList*); 01169 void sqliteCreateIndex(Parse*,Token*,SrcList*,IdList*,int,Token*,Token*); 01170 void sqliteDropIndex(Parse*, SrcList*); 01171 void sqliteAddKeyType(Vdbe*, ExprList*); 01172 void sqliteAddIdxKeyType(Vdbe*, Index*); 01173 int sqliteSelect(Parse*, Select*, int, int, Select*, int, int*); 01174 Select *sqliteSelectNew(ExprList*,SrcList*,Expr*,ExprList*,Expr*,ExprList*, 01175 int,int,int); 01176 void sqliteSelectDelete(Select*); 01177 void sqliteSelectUnbind(Select*); 01178 Table *sqliteSrcListLookup(Parse*, SrcList*); 01179 int sqliteIsReadOnly(Parse*, Table*, int); 01180 void sqliteDeleteFrom(Parse*, SrcList*, Expr*); 01181 void sqliteUpdate(Parse*, SrcList*, ExprList*, Expr*, int); 01182 WhereInfo *sqliteWhereBegin(Parse*, SrcList*, Expr*, int, ExprList**); 01183 void sqliteWhereEnd(WhereInfo*); 01184 void sqliteExprCode(Parse*, Expr*); 01185 int sqliteExprCodeExprList(Parse*, ExprList*, int); 01186 void sqliteExprIfTrue(Parse*, Expr*, int, int); 01187 void sqliteExprIfFalse(Parse*, Expr*, int, int); 01188 Table *sqliteFindTable(sqlite*,const char*, const char*); 01189 Table *sqliteLocateTable(Parse*,const char*, const char*); 01190 Index *sqliteFindIndex(sqlite*,const char*, const char*); 01191 void sqliteUnlinkAndDeleteIndex(sqlite*,Index*); 01192 void sqliteCopy(Parse*, SrcList*, Token*, Token*, int); 01193 void sqliteVacuum(Parse*, Token*); 01194 int sqliteRunVacuum(char**, sqlite*); 01195 int sqliteGlobCompare(const unsigned char*,const unsigned char*); 01196 int sqliteLikeCompare(const unsigned char*,const unsigned char*); 01197 char *sqliteTableNameFromToken(Token*); 01198 int sqliteExprCheck(Parse*, Expr*, int, int*); 01199 int sqliteExprType(Expr*); 01200 int sqliteExprCompare(Expr*, Expr*); 01201 int sqliteFuncId(Token*); 01202 int sqliteExprResolveIds(Parse*, SrcList*, ExprList*, Expr*); 01203 int sqliteExprAnalyzeAggregates(Parse*, Expr*); 01204 Vdbe *sqliteGetVdbe(Parse*); 01205 void sqliteRandomness(int, void*); 01206 void sqliteRollbackAll(sqlite*); 01207 void sqliteCodeVerifySchema(Parse*, int); 01208 void sqliteBeginTransaction(Parse*, int); 01209 void sqliteCommitTransaction(Parse*); 01210 void sqliteRollbackTransaction(Parse*); 01211 int sqliteExprIsConstant(Expr*); 01212 int sqliteExprIsInteger(Expr*, int*); 01213 int sqliteIsRowid(const char*); 01214 void sqliteGenerateRowDelete(sqlite*, Vdbe*, Table*, int, int); 01215 void sqliteGenerateRowIndexDelete(sqlite*, Vdbe*, Table*, int, char*); 01216 void sqliteGenerateConstraintChecks(Parse*,Table*,int,char*,int,int,int,int); 01217 void sqliteCompleteInsertion(Parse*, Table*, int, char*, int, int, int); 01218 int sqliteOpenTableAndIndices(Parse*, Table*, int); 01219 void sqliteBeginWriteOperation(Parse*, int, int); 01220 void sqliteEndWriteOperation(Parse*); 01221 Expr *sqliteExprDup(Expr*); 01222 void sqliteTokenCopy(Token*, Token*); 01223 ExprList *sqliteExprListDup(ExprList*); 01224 SrcList *sqliteSrcListDup(SrcList*); 01225 IdList *sqliteIdListDup(IdList*); 01226 Select *sqliteSelectDup(Select*); 01227 FuncDef *sqliteFindFunction(sqlite*,const char*,int,int,int); 01228 void sqliteRegisterBuiltinFunctions(sqlite*); 01229 void sqliteRegisterDateTimeFunctions(sqlite*); 01230 int sqliteSafetyOn(sqlite*); 01231 int sqliteSafetyOff(sqlite*); 01232 int sqliteSafetyCheck(sqlite*); 01233 void sqliteChangeCookie(sqlite*, Vdbe*); 01234 void sqliteBeginTrigger(Parse*, Token*,int,int,IdList*,SrcList*,int,Expr*,int); 01235 void sqliteFinishTrigger(Parse*, TriggerStep*, Token*); 01236 void sqliteDropTrigger(Parse*, SrcList*); 01237 void sqliteDropTriggerPtr(Parse*, Trigger*, int); 01238 int sqliteTriggersExist(Parse* , Trigger* , int , int , int, ExprList*); 01239 int sqliteCodeRowTrigger(Parse*, int, ExprList*, int, Table *, int, int, 01240 int, int); 01241 void sqliteViewTriggers(Parse*, Table*, Expr*, int, ExprList*); 01242 void sqliteDeleteTriggerStep(TriggerStep*); 01243 TriggerStep *sqliteTriggerSelectStep(Select*); 01244 TriggerStep *sqliteTriggerInsertStep(Token*, IdList*, ExprList*, Select*, int); 01245 TriggerStep *sqliteTriggerUpdateStep(Token*, ExprList*, Expr*, int); 01246 TriggerStep *sqliteTriggerDeleteStep(Token*, Expr*); 01247 void sqliteDeleteTrigger(Trigger*); 01248 int sqliteJoinType(Parse*, Token*, Token*, Token*); 01249 void sqliteCreateForeignKey(Parse*, IdList*, Token*, IdList*, int); 01250 void sqliteDeferForeignKey(Parse*, int); 01251 #ifndef SQLITE_OMIT_AUTHORIZATION 01252 void sqliteAuthRead(Parse*,Expr*,SrcList*); 01253 int sqliteAuthCheck(Parse*,int, const char*, const char*, const char*); 01254 void sqliteAuthContextPush(Parse*, AuthContext*, const char*); 01255 void sqliteAuthContextPop(AuthContext*); 01256 #else 01257 # define sqliteAuthRead(a,b,c) 01258 # define sqliteAuthCheck(a,b,c,d,e) SQLITE_OK 01259 # define sqliteAuthContextPush(a,b,c) 01260 # define sqliteAuthContextPop(a) ((void)(a)) 01261 #endif 01262 void sqliteAttach(Parse*, Token*, Token*, Token*); 01263 void sqliteDetach(Parse*, Token*); 01264 int sqliteBtreeFactory(const sqlite *db, const char *zFilename, 01265 int mode, int nPg, Btree **ppBtree); 01266 int sqliteFixInit(DbFixer*, Parse*, int, const char*, const Token*); 01267 int sqliteFixSrcList(DbFixer*, SrcList*); 01268 int sqliteFixSelect(DbFixer*, Select*); 01269 int sqliteFixExpr(DbFixer*, Expr*); 01270 int sqliteFixExprList(DbFixer*, ExprList*); 01271 int sqliteFixTriggerStep(DbFixer*, TriggerStep*); 01272 double sqliteAtoF(const char *z, const char **); 01273 char *sqlite_snprintf(int,char*,const char*,...); 01274 int sqliteFitsIn32Bits(const char *);
KDE 4.5 API Reference