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