sqllogictest
Check-in [4dc66b8b89]
Not logged in

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Update SQLite to version 3.6.19.
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 4dc66b8b89650799470b4fc7e998617964314a8a
User & Date: drh 2009-10-14 11:16:56
Context
2009-11-02
17:53
Update the SQLite implementation to the 3.6.20 release candidate. check-in: 2c7d3524b6 user: drh tags: trunk
2009-10-14
11:16
Update SQLite to version 3.6.19. check-in: 4dc66b8b89 user: drh tags: trunk
2009-09-11
14:48
Add the logo image. check-in: 4997b20c29 user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/sqlite3.c.

     1      1   /******************************************************************************
     2      2   ** This file is an amalgamation of many separate C source files from SQLite
     3         -** version 3.6.18.  By combining all the individual C code files into this 
            3  +** version 3.6.19.  By combining all the individual C code files into this 
     4      4   ** single large file, the entire code can be compiled as a one translation
     5      5   ** unit.  This allows many compilers to do optimizations that would not be
     6      6   ** possible if the files were compiled separately.  Performance improvements
     7      7   ** of 5% are more are commonly seen when SQLite is compiled as a single
     8      8   ** translation unit.
     9      9   **
    10     10   ** This file is all you need to compile SQLite.  To use SQLite in other
................................................................................
    13     13   ** the "sqlite3.h" header file at hand, you will find a copy embedded within
    14     14   ** the text of this file.  Search for "Begin file sqlite3.h" to find the start
    15     15   ** of the embedded sqlite3.h header file.) Additional code files may be needed
    16     16   ** if you want a wrapper to interface SQLite with your choice of programming
    17     17   ** language. The code for the "sqlite3" command-line shell is also in a
    18     18   ** separate file. This file contains only code for the core SQLite library.
    19     19   **
    20         -** This amalgamation was generated on 2009-09-11 14:03:22 UTC.
           20  +** This amalgamation was generated on 2009-10-14 03:07:49 UTC.
    21     21   */
    22     22   #define SQLITE_CORE 1
    23     23   #define SQLITE_AMALGAMATION 1
    24     24   #ifndef SQLITE_PRIVATE
    25     25   # define SQLITE_PRIVATE static
    26     26   #endif
    27     27   #ifndef SQLITE_API
................................................................................
   271    271   */
   272    272   #ifndef SQLITE_MAX_LIKE_PATTERN_LENGTH
   273    273   # define SQLITE_MAX_LIKE_PATTERN_LENGTH 50000
   274    274   #endif
   275    275   
   276    276   /*
   277    277   ** Maximum depth of recursion for triggers.
          278  +**
          279  +** A value of 1 means that a trigger program will not be able to itself
          280  +** fire any triggers. A value of 0 means that no trigger programs at all 
          281  +** may be executed.
   278    282   */
   279    283   #ifndef SQLITE_MAX_TRIGGER_DEPTH
   280    284   #if defined(SQLITE_SMALL_STACK)
   281    285   # define SQLITE_MAX_TRIGGER_DEPTH 10
   282    286   #else
   283    287   # define SQLITE_MAX_TRIGGER_DEPTH 1000
   284    288   #endif
................................................................................
   643    647   **
   644    648   ** See also: [sqlite3_libversion()],
   645    649   ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
   646    650   ** [sqlite_version()] and [sqlite_source_id()].
   647    651   **
   648    652   ** Requirements: [H10011] [H10014]
   649    653   */
   650         -#define SQLITE_VERSION        "3.6.18"
   651         -#define SQLITE_VERSION_NUMBER 3006018
   652         -#define SQLITE_SOURCE_ID      "2009-09-10 22:30:54 3ea10434434d27a1300ba2b58e2c47c54909f4ff"
          654  +#define SQLITE_VERSION        "3.6.19"
          655  +#define SQLITE_VERSION_NUMBER 3006019
          656  +#define SQLITE_SOURCE_ID      "2009-10-13 22:47:14 ac19dbc6a208be2313e47cbcc0b2a0d8d04bae4c"
   653    657   
   654    658   /*
   655    659   ** CAPI3REF: Run-Time Library Version Numbers {H10020} <S60100>
   656    660   ** KEYWORDS: sqlite3_version
   657    661   **
   658    662   ** These interfaces provide the same information as the [SQLITE_VERSION],
   659    663   ** [SQLITE_VERSION_NUMBER], and [SQLITE_SOURCE_ID] #defines in the header,
................................................................................
  1840   1844   ** CAPI3REF: Count The Number Of Rows Modified {H12240} <S10600>
  1841   1845   **
  1842   1846   ** This function returns the number of database rows that were changed
  1843   1847   ** or inserted or deleted by the most recently completed SQL statement
  1844   1848   ** on the [database connection] specified by the first parameter.
  1845   1849   ** Only changes that are directly specified by the [INSERT], [UPDATE],
  1846   1850   ** or [DELETE] statement are counted.  Auxiliary changes caused by
  1847         -** triggers are not counted. Use the [sqlite3_total_changes()] function
  1848         -** to find the total number of changes including changes caused by triggers.
         1851  +** triggers or [foreign key actions] are not counted. Use the
         1852  +** [sqlite3_total_changes()] function to find the total number of changes
         1853  +** including changes caused by triggers and foreign key actions.
  1849   1854   **
  1850   1855   ** Changes to a view that are simulated by an [INSTEAD OF trigger]
  1851   1856   ** are not counted.  Only real table changes are counted.
  1852   1857   **
  1853   1858   ** A "row change" is a change to a single row of a single table
  1854   1859   ** caused by an INSERT, DELETE, or UPDATE statement.  Rows that
  1855   1860   ** are changed as side effects of [REPLACE] constraint resolution,
................................................................................
  1893   1898   SQLITE_API int sqlite3_changes(sqlite3*);
  1894   1899   
  1895   1900   /*
  1896   1901   ** CAPI3REF: Total Number Of Rows Modified {H12260} <S10600>
  1897   1902   **
  1898   1903   ** This function returns the number of row changes caused by [INSERT],
  1899   1904   ** [UPDATE] or [DELETE] statements since the [database connection] was opened.
  1900         -** The count includes all changes from all 
  1901         -** [CREATE TRIGGER | trigger] contexts.  However,
         1905  +** The count includes all changes from all [CREATE TRIGGER | trigger] 
         1906  +** contexts and changes made by [foreign key actions]. However,
  1902   1907   ** the count does not include changes used to implement [REPLACE] constraints,
  1903   1908   ** do rollbacks or ABORT processing, or [DROP TABLE] processing.  The
  1904   1909   ** count does not include rows of views that fire an [INSTEAD OF trigger],
  1905   1910   ** though if the INSTEAD OF trigger makes changes of its own, those changes 
  1906   1911   ** are counted.
  1907   1912   ** The changes are counted as soon as the statement that makes them is
  1908   1913   ** completed (when the statement handle is passed to [sqlite3_reset()] or
................................................................................
  5035   5040   **
  5036   5041   ** <pre>
  5037   5042   **     SELECT zColumn FROM zDb.zTable WHERE [rowid] = iRow;
  5038   5043   ** </pre> {END}
  5039   5044   **
  5040   5045   ** If the flags parameter is non-zero, then the BLOB is opened for read
  5041   5046   ** and write access. If it is zero, the BLOB is opened for read access.
         5047  +** It is not possible to open a column that is part of an index or primary 
         5048  +** key for writing. ^If [foreign key constraints] are enabled, it is 
         5049  +** not possible to open a column that is part of a [child key] for writing.
  5042   5050   **
  5043   5051   ** Note that the database name is not the filename that contains
  5044   5052   ** the database but rather the symbolic name of the database that
  5045   5053   ** is assigned when the database is connected using [ATTACH].
  5046   5054   ** For the main database file, the database name is "main".
  5047   5055   ** For TEMP tables, the database name is "temp".
  5048   5056   **
................................................................................
  6417   6425   #define TK_LP                             22
  6418   6426   #define TK_RP                             23
  6419   6427   #define TK_AS                             24
  6420   6428   #define TK_COMMA                          25
  6421   6429   #define TK_ID                             26
  6422   6430   #define TK_INDEXED                        27
  6423   6431   #define TK_ABORT                          28
  6424         -#define TK_AFTER                          29
  6425         -#define TK_ANALYZE                        30
  6426         -#define TK_ASC                            31
  6427         -#define TK_ATTACH                         32
  6428         -#define TK_BEFORE                         33
  6429         -#define TK_BY                             34
  6430         -#define TK_CASCADE                        35
  6431         -#define TK_CAST                           36
  6432         -#define TK_COLUMNKW                       37
  6433         -#define TK_CONFLICT                       38
  6434         -#define TK_DATABASE                       39
  6435         -#define TK_DESC                           40
  6436         -#define TK_DETACH                         41
  6437         -#define TK_EACH                           42
  6438         -#define TK_FAIL                           43
  6439         -#define TK_FOR                            44
  6440         -#define TK_IGNORE                         45
  6441         -#define TK_INITIALLY                      46
  6442         -#define TK_INSTEAD                        47
  6443         -#define TK_LIKE_KW                        48
  6444         -#define TK_MATCH                          49
  6445         -#define TK_KEY                            50
  6446         -#define TK_OF                             51
  6447         -#define TK_OFFSET                         52
  6448         -#define TK_PRAGMA                         53
  6449         -#define TK_RAISE                          54
  6450         -#define TK_REPLACE                        55
  6451         -#define TK_RESTRICT                       56
  6452         -#define TK_ROW                            57
  6453         -#define TK_TRIGGER                        58
  6454         -#define TK_VACUUM                         59
  6455         -#define TK_VIEW                           60
  6456         -#define TK_VIRTUAL                        61
  6457         -#define TK_REINDEX                        62
  6458         -#define TK_RENAME                         63
  6459         -#define TK_CTIME_KW                       64
  6460         -#define TK_ANY                            65
  6461         -#define TK_OR                             66
  6462         -#define TK_AND                            67
  6463         -#define TK_IS                             68
  6464         -#define TK_BETWEEN                        69
  6465         -#define TK_IN                             70
  6466         -#define TK_ISNULL                         71
  6467         -#define TK_NOTNULL                        72
  6468         -#define TK_NE                             73
  6469         -#define TK_EQ                             74
  6470         -#define TK_GT                             75
  6471         -#define TK_LE                             76
  6472         -#define TK_LT                             77
  6473         -#define TK_GE                             78
  6474         -#define TK_ESCAPE                         79
  6475         -#define TK_BITAND                         80
  6476         -#define TK_BITOR                          81
  6477         -#define TK_LSHIFT                         82
  6478         -#define TK_RSHIFT                         83
  6479         -#define TK_PLUS                           84
  6480         -#define TK_MINUS                          85
  6481         -#define TK_STAR                           86
  6482         -#define TK_SLASH                          87
  6483         -#define TK_REM                            88
  6484         -#define TK_CONCAT                         89
  6485         -#define TK_COLLATE                        90
  6486         -#define TK_UMINUS                         91
  6487         -#define TK_UPLUS                          92
         6432  +#define TK_ACTION                         29
         6433  +#define TK_AFTER                          30
         6434  +#define TK_ANALYZE                        31
         6435  +#define TK_ASC                            32
         6436  +#define TK_ATTACH                         33
         6437  +#define TK_BEFORE                         34
         6438  +#define TK_BY                             35
         6439  +#define TK_CASCADE                        36
         6440  +#define TK_CAST                           37
         6441  +#define TK_COLUMNKW                       38
         6442  +#define TK_CONFLICT                       39
         6443  +#define TK_DATABASE                       40
         6444  +#define TK_DESC                           41
         6445  +#define TK_DETACH                         42
         6446  +#define TK_EACH                           43
         6447  +#define TK_FAIL                           44
         6448  +#define TK_FOR                            45
         6449  +#define TK_IGNORE                         46
         6450  +#define TK_INITIALLY                      47
         6451  +#define TK_INSTEAD                        48
         6452  +#define TK_LIKE_KW                        49
         6453  +#define TK_MATCH                          50
         6454  +#define TK_NO                             51
         6455  +#define TK_KEY                            52
         6456  +#define TK_OF                             53
         6457  +#define TK_OFFSET                         54
         6458  +#define TK_PRAGMA                         55
         6459  +#define TK_RAISE                          56
         6460  +#define TK_REPLACE                        57
         6461  +#define TK_RESTRICT                       58
         6462  +#define TK_ROW                            59
         6463  +#define TK_TRIGGER                        60
         6464  +#define TK_VACUUM                         61
         6465  +#define TK_VIEW                           62
         6466  +#define TK_VIRTUAL                        63
         6467  +#define TK_REINDEX                        64
         6468  +#define TK_RENAME                         65
         6469  +#define TK_CTIME_KW                       66
         6470  +#define TK_ANY                            67
         6471  +#define TK_OR                             68
         6472  +#define TK_AND                            69
         6473  +#define TK_IS                             70
         6474  +#define TK_BETWEEN                        71
         6475  +#define TK_IN                             72
         6476  +#define TK_ISNULL                         73
         6477  +#define TK_NOTNULL                        74
         6478  +#define TK_NE                             75
         6479  +#define TK_EQ                             76
         6480  +#define TK_GT                             77
         6481  +#define TK_LE                             78
         6482  +#define TK_LT                             79
         6483  +#define TK_GE                             80
         6484  +#define TK_ESCAPE                         81
         6485  +#define TK_BITAND                         82
         6486  +#define TK_BITOR                          83
         6487  +#define TK_LSHIFT                         84
         6488  +#define TK_RSHIFT                         85
         6489  +#define TK_PLUS                           86
         6490  +#define TK_MINUS                          87
         6491  +#define TK_STAR                           88
         6492  +#define TK_SLASH                          89
         6493  +#define TK_REM                            90
         6494  +#define TK_CONCAT                         91
         6495  +#define TK_COLLATE                        92
  6488   6496   #define TK_BITNOT                         93
  6489   6497   #define TK_STRING                         94
  6490   6498   #define TK_JOIN_KW                        95
  6491   6499   #define TK_CONSTRAINT                     96
  6492   6500   #define TK_DEFAULT                        97
  6493   6501   #define TK_NULL                           98
  6494   6502   #define TK_PRIMARY                        99
................................................................................
  6495   6503   #define TK_UNIQUE                         100
  6496   6504   #define TK_CHECK                          101
  6497   6505   #define TK_REFERENCES                     102
  6498   6506   #define TK_AUTOINCR                       103
  6499   6507   #define TK_ON                             104
  6500   6508   #define TK_DELETE                         105
  6501   6509   #define TK_UPDATE                         106
  6502         -#define TK_INSERT                         107
  6503         -#define TK_SET                            108
  6504         -#define TK_DEFERRABLE                     109
  6505         -#define TK_FOREIGN                        110
  6506         -#define TK_DROP                           111
  6507         -#define TK_UNION                          112
  6508         -#define TK_ALL                            113
  6509         -#define TK_EXCEPT                         114
  6510         -#define TK_INTERSECT                      115
  6511         -#define TK_SELECT                         116
  6512         -#define TK_DISTINCT                       117
  6513         -#define TK_DOT                            118
  6514         -#define TK_FROM                           119
  6515         -#define TK_JOIN                           120
  6516         -#define TK_USING                          121
  6517         -#define TK_ORDER                          122
  6518         -#define TK_GROUP                          123
  6519         -#define TK_HAVING                         124
  6520         -#define TK_LIMIT                          125
  6521         -#define TK_WHERE                          126
  6522         -#define TK_INTO                           127
  6523         -#define TK_VALUES                         128
         6510  +#define TK_SET                            107
         6511  +#define TK_DEFERRABLE                     108
         6512  +#define TK_FOREIGN                        109
         6513  +#define TK_DROP                           110
         6514  +#define TK_UNION                          111
         6515  +#define TK_ALL                            112
         6516  +#define TK_EXCEPT                         113
         6517  +#define TK_INTERSECT                      114
         6518  +#define TK_SELECT                         115
         6519  +#define TK_DISTINCT                       116
         6520  +#define TK_DOT                            117
         6521  +#define TK_FROM                           118
         6522  +#define TK_JOIN                           119
         6523  +#define TK_USING                          120
         6524  +#define TK_ORDER                          121
         6525  +#define TK_GROUP                          122
         6526  +#define TK_HAVING                         123
         6527  +#define TK_LIMIT                          124
         6528  +#define TK_WHERE                          125
         6529  +#define TK_INTO                           126
         6530  +#define TK_VALUES                         127
         6531  +#define TK_INSERT                         128
  6524   6532   #define TK_INTEGER                        129
  6525   6533   #define TK_FLOAT                          130
  6526   6534   #define TK_BLOB                           131
  6527   6535   #define TK_REGISTER                       132
  6528   6536   #define TK_VARIABLE                       133
  6529   6537   #define TK_CASE                           134
  6530   6538   #define TK_WHEN                           135
................................................................................
  6532   6540   #define TK_ELSE                           137
  6533   6541   #define TK_INDEX                          138
  6534   6542   #define TK_TO_TEXT                        139
  6535   6543   #define TK_TO_BLOB                        140
  6536   6544   #define TK_TO_NUMERIC                     141
  6537   6545   #define TK_TO_INT                         142
  6538   6546   #define TK_TO_REAL                        143
  6539         -#define TK_END_OF_FILE                    144
  6540         -#define TK_ILLEGAL                        145
  6541         -#define TK_SPACE                          146
  6542         -#define TK_UNCLOSED_STRING                147
  6543         -#define TK_FUNCTION                       148
  6544         -#define TK_COLUMN                         149
  6545         -#define TK_AGG_FUNCTION                   150
  6546         -#define TK_AGG_COLUMN                     151
  6547         -#define TK_CONST_FUNC                     152
         6547  +#define TK_ISNOT                          144
         6548  +#define TK_END_OF_FILE                    145
         6549  +#define TK_ILLEGAL                        146
         6550  +#define TK_SPACE                          147
         6551  +#define TK_UNCLOSED_STRING                148
         6552  +#define TK_FUNCTION                       149
         6553  +#define TK_COLUMN                         150
         6554  +#define TK_AGG_FUNCTION                   151
         6555  +#define TK_AGG_COLUMN                     152
         6556  +#define TK_CONST_FUNC                     153
         6557  +#define TK_UMINUS                         154
         6558  +#define TK_UPLUS                          155
  6548   6559   
  6549   6560   /************** End of parse.h ***********************************************/
  6550   6561   /************** Continuing where we left off in sqliteInt.h ******************/
  6551   6562   #include <stdio.h>
  6552   6563   #include <stdlib.h>
  6553   6564   #include <string.h>
  6554   6565   #include <assert.h>
................................................................................
  7304   7315   #define OP_Affinity                             2
  7305   7316   #define OP_Column                               3
  7306   7317   #define OP_SetCookie                            4
  7307   7318   #define OP_Seek                                 5
  7308   7319   #define OP_Real                               130   /* same as TK_FLOAT    */
  7309   7320   #define OP_Sequence                             6
  7310   7321   #define OP_Savepoint                            7
  7311         -#define OP_Ge                                  78   /* same as TK_GE       */
         7322  +#define OP_Ge                                  80   /* same as TK_GE       */
  7312   7323   #define OP_RowKey                               8
  7313   7324   #define OP_SCopy                                9
  7314         -#define OP_Eq                                  74   /* same as TK_EQ       */
         7325  +#define OP_Eq                                  76   /* same as TK_EQ       */
  7315   7326   #define OP_OpenWrite                           10
  7316         -#define OP_NotNull                             72   /* same as TK_NOTNULL  */
         7327  +#define OP_NotNull                             74   /* same as TK_NOTNULL  */
  7317   7328   #define OP_If                                  11
  7318   7329   #define OP_ToInt                              142   /* same as TK_TO_INT   */
  7319   7330   #define OP_String8                             94   /* same as TK_STRING   */
  7320   7331   #define OP_CollSeq                             12
  7321   7332   #define OP_OpenRead                            13
  7322   7333   #define OP_Expire                              14
  7323   7334   #define OP_AutoCommit                          15
  7324         -#define OP_Gt                                  75   /* same as TK_GT       */
         7335  +#define OP_Gt                                  77   /* same as TK_GT       */
  7325   7336   #define OP_Pagecount                           16
  7326   7337   #define OP_IntegrityCk                         17
  7327   7338   #define OP_Sort                                18
  7328   7339   #define OP_Copy                                20
  7329   7340   #define OP_Trace                               21
  7330   7341   #define OP_Function                            22
  7331   7342   #define OP_IfNeg                               23
  7332         -#define OP_And                                 67   /* same as TK_AND      */
  7333         -#define OP_Subtract                            85   /* same as TK_MINUS    */
         7343  +#define OP_And                                 69   /* same as TK_AND      */
         7344  +#define OP_Subtract                            87   /* same as TK_MINUS    */
  7334   7345   #define OP_Noop                                24
  7335   7346   #define OP_Program                             25
  7336   7347   #define OP_Return                              26
  7337         -#define OP_Remainder                           88   /* same as TK_REM      */
         7348  +#define OP_Remainder                           90   /* same as TK_REM      */
  7338   7349   #define OP_NewRowid                            27
  7339         -#define OP_Multiply                            86   /* same as TK_STAR     */
  7340         -#define OP_Variable                            28
  7341         -#define OP_String                              29
  7342         -#define OP_RealAffinity                        30
  7343         -#define OP_VRename                             31
  7344         -#define OP_ParseSchema                         32
  7345         -#define OP_VOpen                               33
  7346         -#define OP_Close                               34
  7347         -#define OP_CreateIndex                         35
  7348         -#define OP_IsUnique                            36
  7349         -#define OP_NotFound                            37
  7350         -#define OP_Int64                               38
  7351         -#define OP_MustBeInt                           39
  7352         -#define OP_Halt                                40
  7353         -#define OP_Rowid                               41
  7354         -#define OP_IdxLT                               42
  7355         -#define OP_AddImm                              43
  7356         -#define OP_RowData                             44
  7357         -#define OP_MemMax                              45
  7358         -#define OP_Or                                  66   /* same as TK_OR       */
  7359         -#define OP_NotExists                           46
  7360         -#define OP_Gosub                               47
  7361         -#define OP_Divide                              87   /* same as TK_SLASH    */
  7362         -#define OP_Integer                             48
         7350  +#define OP_Multiply                            88   /* same as TK_STAR     */
         7351  +#define OP_FkCounter                           28
         7352  +#define OP_Variable                            29
         7353  +#define OP_String                              30
         7354  +#define OP_RealAffinity                        31
         7355  +#define OP_VRename                             32
         7356  +#define OP_ParseSchema                         33
         7357  +#define OP_VOpen                               34
         7358  +#define OP_Close                               35
         7359  +#define OP_CreateIndex                         36
         7360  +#define OP_IsUnique                            37
         7361  +#define OP_NotFound                            38
         7362  +#define OP_Int64                               39
         7363  +#define OP_MustBeInt                           40
         7364  +#define OP_Halt                                41
         7365  +#define OP_Rowid                               42
         7366  +#define OP_IdxLT                               43
         7367  +#define OP_AddImm                              44
         7368  +#define OP_RowData                             45
         7369  +#define OP_MemMax                              46
         7370  +#define OP_Or                                  68   /* same as TK_OR       */
         7371  +#define OP_NotExists                           47
         7372  +#define OP_Gosub                               48
         7373  +#define OP_Divide                              89   /* same as TK_SLASH    */
         7374  +#define OP_Integer                             49
  7363   7375   #define OP_ToNumeric                          141   /* same as TK_TO_NUMERIC*/
  7364         -#define OP_Prev                                49
  7365         -#define OP_RowSetRead                          50
  7366         -#define OP_Concat                              89   /* same as TK_CONCAT   */
  7367         -#define OP_RowSetAdd                           51
  7368         -#define OP_BitAnd                              80   /* same as TK_BITAND   */
  7369         -#define OP_VColumn                             52
  7370         -#define OP_CreateTable                         53
  7371         -#define OP_Last                                54
  7372         -#define OP_SeekLe                              55
  7373         -#define OP_IsNull                              71   /* same as TK_ISNULL   */
  7374         -#define OP_IncrVacuum                          56
  7375         -#define OP_IdxRowid                            57
  7376         -#define OP_ShiftRight                          83   /* same as TK_RSHIFT   */
  7377         -#define OP_ResetCount                          58
  7378         -#define OP_Yield                               59
  7379         -#define OP_DropTrigger                         60
  7380         -#define OP_DropIndex                           61
  7381         -#define OP_Param                               62
  7382         -#define OP_IdxGE                               63
  7383         -#define OP_IdxDelete                           64
  7384         -#define OP_Vacuum                              65
  7385         -#define OP_IfNot                               68
  7386         -#define OP_DropTable                           69
  7387         -#define OP_SeekLt                              70
  7388         -#define OP_MakeRecord                          79
         7376  +#define OP_Prev                                50
         7377  +#define OP_RowSetRead                          51
         7378  +#define OP_Concat                              91   /* same as TK_CONCAT   */
         7379  +#define OP_RowSetAdd                           52
         7380  +#define OP_BitAnd                              82   /* same as TK_BITAND   */
         7381  +#define OP_VColumn                             53
         7382  +#define OP_CreateTable                         54
         7383  +#define OP_Last                                55
         7384  +#define OP_SeekLe                              56
         7385  +#define OP_IsNull                              73   /* same as TK_ISNULL   */
         7386  +#define OP_IncrVacuum                          57
         7387  +#define OP_IdxRowid                            58
         7388  +#define OP_ShiftRight                          85   /* same as TK_RSHIFT   */
         7389  +#define OP_ResetCount                          59
         7390  +#define OP_Yield                               60
         7391  +#define OP_DropTrigger                         61
         7392  +#define OP_DropIndex                           62
         7393  +#define OP_Param                               63
         7394  +#define OP_IdxGE                               64
         7395  +#define OP_IdxDelete                           65
         7396  +#define OP_Vacuum                              66
         7397  +#define OP_IfNot                               67
         7398  +#define OP_DropTable                           70
         7399  +#define OP_SeekLt                              71
         7400  +#define OP_MakeRecord                          72
  7389   7401   #define OP_ToBlob                             140   /* same as TK_TO_BLOB  */
  7390         -#define OP_ResultRow                           90
  7391         -#define OP_Delete                              91
  7392         -#define OP_AggFinal                            92
  7393         -#define OP_Compare                             95
  7394         -#define OP_ShiftLeft                           82   /* same as TK_LSHIFT   */
  7395         -#define OP_Goto                                96
  7396         -#define OP_TableLock                           97
  7397         -#define OP_Clear                               98
  7398         -#define OP_Le                                  76   /* same as TK_LE       */
  7399         -#define OP_VerifyCookie                        99
  7400         -#define OP_AggStep                            100
         7402  +#define OP_ResultRow                           81
         7403  +#define OP_Delete                              92
         7404  +#define OP_AggFinal                            95
         7405  +#define OP_Compare                             96
         7406  +#define OP_ShiftLeft                           84   /* same as TK_LSHIFT   */
         7407  +#define OP_Goto                                97
         7408  +#define OP_TableLock                           98
         7409  +#define OP_Clear                               99
         7410  +#define OP_Le                                  78   /* same as TK_LE       */
         7411  +#define OP_VerifyCookie                       100
         7412  +#define OP_AggStep                            101
  7401   7413   #define OP_ToText                             139   /* same as TK_TO_TEXT  */
  7402   7414   #define OP_Not                                 19   /* same as TK_NOT      */
  7403   7415   #define OP_ToReal                             143   /* same as TK_TO_REAL  */
  7404         -#define OP_Transaction                        101
  7405         -#define OP_VFilter                            102
  7406         -#define OP_Ne                                  73   /* same as TK_NE       */
  7407         -#define OP_VDestroy                           103
  7408         -#define OP_BitOr                               81   /* same as TK_BITOR    */
  7409         -#define OP_Next                               104
  7410         -#define OP_Count                              105
  7411         -#define OP_IdxInsert                          106
  7412         -#define OP_Lt                                  77   /* same as TK_LT       */
  7413         -#define OP_SeekGe                             107
  7414         -#define OP_Insert                             108
  7415         -#define OP_Destroy                            109
  7416         -#define OP_ReadCookie                         110
  7417         -#define OP_RowSetTest                         111
  7418         -#define OP_LoadAnalysis                       112
  7419         -#define OP_Explain                            113
  7420         -#define OP_HaltIfNull                         114
  7421         -#define OP_OpenPseudo                         115
  7422         -#define OP_OpenEphemeral                      116
  7423         -#define OP_Null                               117
  7424         -#define OP_Move                               118
  7425         -#define OP_Blob                               119
  7426         -#define OP_Add                                 84   /* same as TK_PLUS     */
  7427         -#define OP_Rewind                             120
  7428         -#define OP_SeekGt                             121
  7429         -#define OP_VBegin                             122
  7430         -#define OP_VUpdate                            123
  7431         -#define OP_IfZero                             124
         7416  +#define OP_Transaction                        102
         7417  +#define OP_VFilter                            103
         7418  +#define OP_Ne                                  75   /* same as TK_NE       */
         7419  +#define OP_VDestroy                           104
         7420  +#define OP_BitOr                               83   /* same as TK_BITOR    */
         7421  +#define OP_Next                               105
         7422  +#define OP_Count                              106
         7423  +#define OP_IdxInsert                          107
         7424  +#define OP_Lt                                  79   /* same as TK_LT       */
         7425  +#define OP_FkIfZero                           108
         7426  +#define OP_SeekGe                             109
         7427  +#define OP_Insert                             110
         7428  +#define OP_Destroy                            111
         7429  +#define OP_ReadCookie                         112
         7430  +#define OP_RowSetTest                         113
         7431  +#define OP_LoadAnalysis                       114
         7432  +#define OP_Explain                            115
         7433  +#define OP_HaltIfNull                         116
         7434  +#define OP_OpenPseudo                         117
         7435  +#define OP_OpenEphemeral                      118
         7436  +#define OP_Null                               119
         7437  +#define OP_Move                               120
         7438  +#define OP_Blob                               121
         7439  +#define OP_Add                                 86   /* same as TK_PLUS     */
         7440  +#define OP_Rewind                             122
         7441  +#define OP_SeekGt                             123
         7442  +#define OP_VBegin                             124
         7443  +#define OP_VUpdate                            125
         7444  +#define OP_IfZero                             126
  7432   7445   #define OP_BitNot                              93   /* same as TK_BITNOT   */
  7433         -#define OP_VCreate                            125
  7434         -#define OP_Found                              126
  7435         -#define OP_IfPos                              127
  7436         -#define OP_NullRow                            128
  7437         -#define OP_Jump                               129
  7438         -#define OP_Permutation                        131
         7446  +#define OP_VCreate                            127
         7447  +#define OP_Found                              128
         7448  +#define OP_IfPos                              129
         7449  +#define OP_NullRow                            131
         7450  +#define OP_Jump                               132
         7451  +#define OP_Permutation                        133
  7439   7452   
  7440   7453   /* The following opcode values are never used */
  7441         -#define OP_NotUsed_132                        132
  7442         -#define OP_NotUsed_133                        133
  7443   7454   #define OP_NotUsed_134                        134
  7444   7455   #define OP_NotUsed_135                        135
  7445   7456   #define OP_NotUsed_136                        136
  7446   7457   #define OP_NotUsed_137                        137
  7447   7458   #define OP_NotUsed_138                        138
  7448   7459   
  7449   7460   
................................................................................
  7457   7468   #define OPFLG_IN2             0x0008  /* in2:   P2 is an input */
  7458   7469   #define OPFLG_IN3             0x0010  /* in3:   P3 is an input */
  7459   7470   #define OPFLG_OUT3            0x0020  /* out3:  P3 is an output */
  7460   7471   #define OPFLG_INITIALIZER {\
  7461   7472   /*   0 */ 0x00, 0x01, 0x00, 0x00, 0x10, 0x08, 0x02, 0x00,\
  7462   7473   /*   8 */ 0x00, 0x04, 0x00, 0x05, 0x00, 0x00, 0x00, 0x00,\
  7463   7474   /*  16 */ 0x02, 0x00, 0x01, 0x04, 0x04, 0x00, 0x00, 0x05,\
  7464         -/*  24 */ 0x00, 0x01, 0x04, 0x02, 0x00, 0x02, 0x04, 0x00,\
  7465         -/*  32 */ 0x00, 0x00, 0x00, 0x02, 0x11, 0x11, 0x02, 0x05,\
  7466         -/*  40 */ 0x00, 0x02, 0x11, 0x04, 0x00, 0x08, 0x11, 0x01,\
  7467         -/*  48 */ 0x02, 0x01, 0x21, 0x08, 0x00, 0x02, 0x01, 0x11,\
  7468         -/*  56 */ 0x01, 0x02, 0x00, 0x04, 0x00, 0x00, 0x02, 0x11,\
  7469         -/*  64 */ 0x00, 0x00, 0x2c, 0x2c, 0x05, 0x00, 0x11, 0x05,\
  7470         -/*  72 */ 0x05, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x00,\
  7471         -/*  80 */ 0x2c, 0x2c, 0x2c, 0x2c, 0x2c, 0x2c, 0x2c, 0x2c,\
  7472         -/*  88 */ 0x2c, 0x2c, 0x00, 0x00, 0x00, 0x04, 0x02, 0x00,\
  7473         -/*  96 */ 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00,\
  7474         -/* 104 */ 0x01, 0x02, 0x08, 0x11, 0x00, 0x02, 0x02, 0x15,\
  7475         -/* 112 */ 0x00, 0x00, 0x10, 0x00, 0x00, 0x02, 0x00, 0x02,\
  7476         -/* 120 */ 0x01, 0x11, 0x00, 0x00, 0x05, 0x00, 0x11, 0x05,\
  7477         -/* 128 */ 0x00, 0x01, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,\
         7475  +/*  24 */ 0x00, 0x01, 0x04, 0x02, 0x00, 0x00, 0x02, 0x04,\
         7476  +/*  32 */ 0x00, 0x00, 0x00, 0x00, 0x02, 0x11, 0x11, 0x02,\
         7477  +/*  40 */ 0x05, 0x00, 0x02, 0x11, 0x04, 0x00, 0x08, 0x11,\
         7478  +/*  48 */ 0x01, 0x02, 0x01, 0x21, 0x08, 0x00, 0x02, 0x01,\
         7479  +/*  56 */ 0x11, 0x01, 0x02, 0x00, 0x04, 0x00, 0x00, 0x02,\
         7480  +/*  64 */ 0x11, 0x00, 0x00, 0x05, 0x2c, 0x2c, 0x00, 0x11,\
         7481  +/*  72 */ 0x00, 0x05, 0x05, 0x15, 0x15, 0x15, 0x15, 0x15,\
         7482  +/*  80 */ 0x15, 0x00, 0x2c, 0x2c, 0x2c, 0x2c, 0x2c, 0x2c,\
         7483  +/*  88 */ 0x2c, 0x2c, 0x2c, 0x2c, 0x00, 0x04, 0x02, 0x00,\
         7484  +/*  96 */ 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,\
         7485  +/* 104 */ 0x00, 0x01, 0x02, 0x08, 0x01, 0x11, 0x00, 0x02,\
         7486  +/* 112 */ 0x02, 0x15, 0x00, 0x00, 0x10, 0x00, 0x00, 0x02,\
         7487  +/* 120 */ 0x00, 0x02, 0x01, 0x11, 0x00, 0x00, 0x05, 0x00,\
         7488  +/* 128 */ 0x11, 0x05, 0x02, 0x00, 0x01, 0x00, 0x00, 0x00,\
  7478   7489   /* 136 */ 0x00, 0x00, 0x00, 0x04, 0x04, 0x04, 0x04, 0x04,\
  7479   7490   }
  7480   7491   
  7481   7492   /************** End of opcodes.h *********************************************/
  7482   7493   /************** Continuing where we left off in vdbe.h ***********************/
  7483   7494   
  7484   7495   /*
................................................................................
  8227   8238   #define sqlite3_mutex_try(X)      SQLITE_OK
  8228   8239   #define sqlite3_mutex_leave(X)
  8229   8240   #define sqlite3_mutex_held(X)     1
  8230   8241   #define sqlite3_mutex_notheld(X)  1
  8231   8242   #define sqlite3MutexAlloc(X)      ((sqlite3_mutex*)8)
  8232   8243   #define sqlite3MutexInit()        SQLITE_OK
  8233   8244   #define sqlite3MutexEnd()
  8234         -#endif /* defined(SQLITE_OMIT_MUTEX) */
         8245  +#endif /* defined(SQLITE_MUTEX_OMIT) */
  8235   8246   
  8236   8247   /************** End of mutex.h ***********************************************/
  8237   8248   /************** Continuing where we left off in sqliteInt.h ******************/
  8238   8249   
  8239   8250   
  8240   8251   /*
  8241   8252   ** Each database file to be accessed by the system is an instance
................................................................................
  8264   8275   ** statements.
  8265   8276   */
  8266   8277   struct Schema {
  8267   8278     int schema_cookie;   /* Database schema version number for this file */
  8268   8279     Hash tblHash;        /* All tables indexed by name */
  8269   8280     Hash idxHash;        /* All (named) indices indexed by name */
  8270   8281     Hash trigHash;       /* All triggers indexed by name */
         8282  +  Hash fkeyHash;       /* All foreign keys by referenced table name */
  8271   8283     Table *pSeqTab;      /* The sqlite_sequence table used by AUTOINCREMENT */
  8272   8284     u8 file_format;      /* Schema format version for this file */
  8273   8285     u8 enc;              /* Text encoding used by this database */
  8274   8286     u16 flags;           /* Flags associated with this schema */
  8275   8287     int cache_size;      /* Number of pages to use in the cache */
  8276   8288   #ifndef SQLITE_OMIT_VIRTUALTABLE
  8277   8289     sqlite3 *db;         /* "Owner" connection. See comment above */
................................................................................
  8454   8466     BusyHandler busyHandler;      /* Busy callback */
  8455   8467     int busyTimeout;              /* Busy handler timeout, in msec */
  8456   8468     Db aDbStatic[2];              /* Static space for the 2 default backends */
  8457   8469     Savepoint *pSavepoint;        /* List of active savepoints */
  8458   8470     int nSavepoint;               /* Number of non-transaction savepoints */
  8459   8471     int nStatement;               /* Number of nested statement-transactions  */
  8460   8472     u8 isTransactionSavepoint;    /* True if the outermost savepoint is a TS */
         8473  +  i64 nDeferredCons;            /* Net deferred constraints this transaction. */
  8461   8474   
  8462   8475   #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
  8463   8476     /* The following variables are all protected by the STATIC_MASTER 
  8464   8477     ** mutex, not by sqlite3.mutex. They are used by code in notify.c. 
  8465   8478     **
  8466   8479     ** When X.pUnlockConnection==Y, that means that X is waiting for Y to
  8467   8480     ** unlock so that it can proceed.
................................................................................
  8510   8523   #define SQLITE_LegacyFileFmt  0x00008000  /* Create new databases in format 1 */
  8511   8524   #define SQLITE_FullFSync      0x00010000  /* Use full fsync on the backend */
  8512   8525   #define SQLITE_LoadExtension  0x00020000  /* Enable load_extension */
  8513   8526   
  8514   8527   #define SQLITE_RecoveryMode   0x00040000  /* Ignore schema errors */
  8515   8528   #define SQLITE_ReverseOrder   0x00100000  /* Reverse unordered SELECTs */
  8516   8529   #define SQLITE_RecTriggers    0x00200000  /* Enable recursive triggers */
         8530  +#define SQLITE_ForeignKeys    0x00400000  /* Enforce foreign key constraints  */
  8517   8531   
  8518   8532   /*
  8519   8533   ** Possible values for the sqlite.magic field.
  8520   8534   ** The numbers are obtained at random and have no special meaning, other
  8521   8535   ** than being distinct from one another.
  8522   8536   */
  8523   8537   #define SQLITE_MAGIC_OPEN     0xa029a697  /* Database is open */
................................................................................
  8596   8610   ** All current savepoints are stored in a linked list starting at
  8597   8611   ** sqlite3.pSavepoint. The first element in the list is the most recently
  8598   8612   ** opened savepoint. Savepoints are added to the list by the vdbe
  8599   8613   ** OP_Savepoint instruction.
  8600   8614   */
  8601   8615   struct Savepoint {
  8602   8616     char *zName;                        /* Savepoint name (nul-terminated) */
         8617  +  i64 nDeferredCons;                  /* Number of deferred fk violations */
  8603   8618     Savepoint *pNext;                   /* Parent savepoint (if any) */
  8604   8619   };
  8605   8620   
  8606   8621   /*
  8607   8622   ** The following are used as the second parameter to sqlite3Savepoint(),
  8608   8623   ** and as the P1 argument to the OP_Savepoint instruction.
  8609   8624   */
................................................................................
  8716   8731   
  8717   8732   /*
  8718   8733   ** Additional bit values that can be ORed with an affinity without
  8719   8734   ** changing the affinity.
  8720   8735   */
  8721   8736   #define SQLITE_JUMPIFNULL   0x08  /* jumps if either operand is NULL */
  8722   8737   #define SQLITE_STOREP2      0x10  /* Store result in reg[P2] rather than jump */
         8738  +#define SQLITE_NULLEQ       0x80  /* NULL=NULL */
  8723   8739   
  8724   8740   /*
  8725   8741   ** An object of this type is created for each virtual table present in
  8726   8742   ** the database schema. 
  8727   8743   **
  8728   8744   ** If the database schema is shared, then there is one instance of this
  8729   8745   ** structure for each database connection (sqlite3*) that uses the shared
................................................................................
  8872   8888   ** For foreign key "fk1", the from-table is "ex1" and the to-table is "ex2".
  8873   8889   **
  8874   8890   ** Each REFERENCES clause generates an instance of the following structure
  8875   8891   ** which is attached to the from-table.  The to-table need not exist when
  8876   8892   ** the from-table is created.  The existence of the to-table is not checked.
  8877   8893   */
  8878   8894   struct FKey {
  8879         -  Table *pFrom;     /* The table that contains the REFERENCES clause */
         8895  +  Table *pFrom;     /* Table containing the REFERENCES clause (aka: Child) */
  8880   8896     FKey *pNextFrom;  /* Next foreign key in pFrom */
  8881         -  char *zTo;        /* Name of table that the key points to */
         8897  +  char *zTo;        /* Name of table that the key points to (aka: Parent) */
         8898  +  FKey *pNextTo;    /* Next foreign key on table named zTo */
         8899  +  FKey *pPrevTo;    /* Previous foreign key on table named zTo */
  8882   8900     int nCol;         /* Number of columns in this key */
         8901  +  /* EV: R-30323-21917 */
  8883   8902     u8 isDeferred;    /* True if constraint checking is deferred till COMMIT */
  8884         -  u8 updateConf;    /* How to resolve conflicts that occur on UPDATE */
  8885         -  u8 deleteConf;    /* How to resolve conflicts that occur on DELETE */
  8886         -  u8 insertConf;    /* How to resolve conflicts that occur on INSERT */
         8903  +  u8 aAction[2];          /* ON DELETE and ON UPDATE actions, respectively */
         8904  +  Trigger *apTrigger[2];  /* Triggers for aAction[] actions */
  8887   8905     struct sColMap {  /* Mapping of columns in pFrom to columns in zTo */
  8888   8906       int iFrom;         /* Index of column in pFrom */
  8889   8907       char *zCol;        /* Name of column in zTo.  If 0 use PRIMARY KEY */
  8890   8908     } aCol[1];        /* One entry for each of nCol column s */
  8891   8909   };
  8892   8910   
  8893   8911   /*
................................................................................
  9710   9728   
  9711   9729     /* Information used while coding trigger programs. */
  9712   9730     Parse *pToplevel;    /* Parse structure for main program (or NULL) */
  9713   9731     Table *pTriggerTab;  /* Table triggers are being coded for */
  9714   9732     u32 oldmask;         /* Mask of old.* columns referenced */
  9715   9733     u8 eTriggerOp;       /* TK_UPDATE, TK_INSERT or TK_DELETE */
  9716   9734     u8 eOrconf;          /* Default ON CONFLICT policy for trigger steps */
         9735  +  u8 disableTriggers;  /* True to disable triggers */
  9717   9736   
  9718   9737     /* Above is constant between recursions.  Below is reset before and after
  9719   9738     ** each recursion */
  9720   9739   
  9721   9740     int nVar;            /* Number of '?' variables seen in the SQL so far */
  9722   9741     int nVarExpr;        /* Number of used slots in apVarExpr[] */
  9723   9742     int nVarExprAlloc;   /* Number of allocated slots in apVarExpr[] */
................................................................................
 10180  10199                                         Token*, Select*, Expr*, IdList*);
 10181  10200   SQLITE_PRIVATE void sqlite3SrcListIndexedBy(Parse *, SrcList *, Token *);
 10182  10201   SQLITE_PRIVATE int sqlite3IndexedByLookup(Parse *, struct SrcList_item *);
 10183  10202   SQLITE_PRIVATE void sqlite3SrcListShiftJoinType(SrcList*);
 10184  10203   SQLITE_PRIVATE void sqlite3SrcListAssignCursors(Parse*, SrcList*);
 10185  10204   SQLITE_PRIVATE void sqlite3IdListDelete(sqlite3*, IdList*);
 10186  10205   SQLITE_PRIVATE void sqlite3SrcListDelete(sqlite3*, SrcList*);
 10187         -SQLITE_PRIVATE void sqlite3CreateIndex(Parse*,Token*,Token*,SrcList*,ExprList*,int,Token*,
        10206  +SQLITE_PRIVATE Index *sqlite3CreateIndex(Parse*,Token*,Token*,SrcList*,ExprList*,int,Token*,
 10188  10207                           Token*, int, int);
 10189  10208   SQLITE_PRIVATE void sqlite3DropIndex(Parse*, SrcList*, int);
 10190  10209   SQLITE_PRIVATE int sqlite3Select(Parse*, Select*, SelectDest*);
 10191  10210   SQLITE_PRIVATE Select *sqlite3SelectNew(Parse*,ExprList*,SrcList*,Expr*,ExprList*,
 10192  10211                            Expr*,ExprList*,int,Expr*,Expr*);
 10193  10212   SQLITE_PRIVATE void sqlite3SelectDelete(sqlite3*, Select*);
 10194  10213   SQLITE_PRIVATE Table *sqlite3SrcListLookup(Parse*, SrcList*);
................................................................................
 10251  10270   SQLITE_PRIVATE void sqlite3GenerateRowIndexDelete(Parse*, Table*, int, int*);
 10252  10271   SQLITE_PRIVATE int sqlite3GenerateIndexKey(Parse*, Index*, int, int, int);
 10253  10272   SQLITE_PRIVATE void sqlite3GenerateConstraintChecks(Parse*,Table*,int,int,
 10254  10273                                        int*,int,int,int,int,int*);
 10255  10274   SQLITE_PRIVATE void sqlite3CompleteInsertion(Parse*, Table*, int, int, int*, int, int, int);
 10256  10275   SQLITE_PRIVATE int sqlite3OpenTableAndIndices(Parse*, Table*, int, int);
 10257  10276   SQLITE_PRIVATE void sqlite3BeginWriteOperation(Parse*, int, int);
 10258         -SQLITE_PRIVATE void sqlite3MayAbort(Parse *);
        10277  +SQLITE_PRIVATE void sqlite3MultiWrite(Parse*);
        10278  +SQLITE_PRIVATE void sqlite3MayAbort(Parse*);
 10259  10279   SQLITE_PRIVATE void sqlite3HaltConstraint(Parse*, int, char*, int);
 10260  10280   SQLITE_PRIVATE Expr *sqlite3ExprDup(sqlite3*,Expr*,int);
 10261  10281   SQLITE_PRIVATE ExprList *sqlite3ExprListDup(sqlite3*,ExprList*,int);
 10262  10282   SQLITE_PRIVATE SrcList *sqlite3SrcListDup(sqlite3*,SrcList*,int);
 10263  10283   SQLITE_PRIVATE IdList *sqlite3IdListDup(sqlite3*,IdList*);
 10264  10284   SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3*,Select*,int);
 10265  10285   SQLITE_PRIVATE void sqlite3FuncDefInsert(FuncDefHash*, FuncDef*);
................................................................................
 10287  10307                              Expr*,int, int);
 10288  10308   SQLITE_PRIVATE   void sqlite3FinishTrigger(Parse*, TriggerStep*, Token*);
 10289  10309   SQLITE_PRIVATE   void sqlite3DropTrigger(Parse*, SrcList*, int);
 10290  10310   SQLITE_PRIVATE   void sqlite3DropTriggerPtr(Parse*, Trigger*);
 10291  10311   SQLITE_PRIVATE   Trigger *sqlite3TriggersExist(Parse *, Table*, int, ExprList*, int *pMask);
 10292  10312   SQLITE_PRIVATE   Trigger *sqlite3TriggerList(Parse *, Table *);
 10293  10313   SQLITE_PRIVATE   void sqlite3CodeRowTrigger(Parse*, Trigger *, int, ExprList*, int, Table *,
 10294         -                            int, int, int, int);
        10314  +                            int, int, int);
        10315  +SQLITE_PRIVATE   void sqlite3CodeRowTriggerDirect(Parse *, Trigger *, Table *, int, int, int);
 10295  10316     void sqliteViewTriggers(Parse*, Table*, Expr*, int, ExprList*);
 10296  10317   SQLITE_PRIVATE   void sqlite3DeleteTriggerStep(sqlite3*, TriggerStep*);
 10297  10318   SQLITE_PRIVATE   TriggerStep *sqlite3TriggerSelectStep(sqlite3*,Select*);
 10298  10319   SQLITE_PRIVATE   TriggerStep *sqlite3TriggerInsertStep(sqlite3*,Token*, IdList*,
 10299  10320                                           ExprList*,Select*,u8);
 10300  10321   SQLITE_PRIVATE   TriggerStep *sqlite3TriggerUpdateStep(sqlite3*,Token*,ExprList*, Expr*, u8);
 10301  10322   SQLITE_PRIVATE   TriggerStep *sqlite3TriggerDeleteStep(sqlite3*,Token*, Expr*);
 10302  10323   SQLITE_PRIVATE   void sqlite3DeleteTrigger(sqlite3*, Trigger*);
 10303  10324   SQLITE_PRIVATE   void sqlite3UnlinkAndDeleteTrigger(sqlite3*,int,const char*);
 10304         -SQLITE_PRIVATE   u32 sqlite3TriggerOldmask(Parse*,Trigger*,int,ExprList*,Table*,int);
        10325  +SQLITE_PRIVATE   u32 sqlite3TriggerOldmask(Parse*,Trigger*,ExprList*,Table*,int);
 10305  10326   # define sqlite3ParseToplevel(p) ((p)->pToplevel ? (p)->pToplevel : (p))
 10306  10327   #else
 10307  10328   # define sqlite3TriggersExist(B,C,D,E,F) 0
 10308  10329   # define sqlite3DeleteTrigger(A,B)
 10309  10330   # define sqlite3DropTriggerPtr(A,B)
 10310  10331   # define sqlite3UnlinkAndDeleteTrigger(A,B,C)
 10311         -# define sqlite3CodeRowTrigger(A,B,C,D,E,F,G,H,I,J)
        10332  +# define sqlite3CodeRowTrigger(A,B,C,D,E,F,G,H,I)
        10333  +# define sqlite3CodeRowTriggerDirect(A,B,C,D,E,F)
 10312  10334   # define sqlite3TriggerList(X, Y) 0
 10313  10335   # define sqlite3ParseToplevel(p) p
 10314         -# define sqlite3TriggerOldmask(A,B,C,D,E,F) 0
        10336  +# define sqlite3TriggerOldmask(A,B,C,D,E) 0
 10315  10337   #endif
 10316  10338   
 10317  10339   SQLITE_PRIVATE int sqlite3JoinType(Parse*, Token*, Token*, Token*);
 10318  10340   SQLITE_PRIVATE void sqlite3CreateForeignKey(Parse*, ExprList*, Token*, ExprList*, int);
 10319  10341   SQLITE_PRIVATE void sqlite3DeferForeignKey(Parse*, int);
 10320  10342   #ifndef SQLITE_OMIT_AUTHORIZATION
 10321  10343   SQLITE_PRIVATE   void sqlite3AuthRead(Parse*,Expr*,Schema*,SrcList*);
 10322  10344   SQLITE_PRIVATE   int sqlite3AuthCheck(Parse*,int, const char*, const char*, const char*);
 10323  10345   SQLITE_PRIVATE   void sqlite3AuthContextPush(Parse*, AuthContext*, const char*);
 10324  10346   SQLITE_PRIVATE   void sqlite3AuthContextPop(AuthContext*);
        10347  +SQLITE_PRIVATE   int sqlite3AuthReadCol(Parse*, const char *, const char *, int);
 10325  10348   #else
 10326  10349   # define sqlite3AuthRead(a,b,c,d)
 10327  10350   # define sqlite3AuthCheck(a,b,c,d,e)    SQLITE_OK
 10328  10351   # define sqlite3AuthContextPush(a,b,c)
 10329  10352   # define sqlite3AuthContextPop(a)  ((void)(a))
 10330  10353   #endif
 10331  10354   SQLITE_PRIVATE void sqlite3Attach(Parse*, Expr*, Expr*, Expr*);
................................................................................
 10525  10548   SQLITE_PRIVATE int sqlite3TransferBindings(sqlite3_stmt *, sqlite3_stmt *);
 10526  10549   SQLITE_PRIVATE int sqlite3Reprepare(Vdbe*);
 10527  10550   SQLITE_PRIVATE void sqlite3ExprListCheckLength(Parse*, ExprList*, const char*);
 10528  10551   SQLITE_PRIVATE CollSeq *sqlite3BinaryCompareCollSeq(Parse *, Expr *, Expr *);
 10529  10552   SQLITE_PRIVATE int sqlite3TempInMemory(const sqlite3*);
 10530  10553   SQLITE_PRIVATE VTable *sqlite3GetVTable(sqlite3*, Table*);
 10531  10554   
        10555  +/* Declarations for functions in fkey.c. All of these are replaced by
        10556  +** no-op macros if OMIT_FOREIGN_KEY is defined. In this case no foreign
        10557  +** key functionality is available. If OMIT_TRIGGER is defined but
        10558  +** OMIT_FOREIGN_KEY is not, only some of the functions are no-oped. In
        10559  +** this case foreign keys are parsed, but no other functionality is 
        10560  +** provided (enforcement of FK constraints requires the triggers sub-system).
        10561  +*/
        10562  +#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
        10563  +SQLITE_PRIVATE   void sqlite3FkCheck(Parse*, Table*, int, int);
        10564  +SQLITE_PRIVATE   void sqlite3FkDropTable(Parse*, SrcList *, Table*);
        10565  +SQLITE_PRIVATE   void sqlite3FkActions(Parse*, Table*, ExprList*, int);
        10566  +SQLITE_PRIVATE   int sqlite3FkRequired(Parse*, Table*, int*, int);
        10567  +SQLITE_PRIVATE   u32 sqlite3FkOldmask(Parse*, Table*);
        10568  +SQLITE_PRIVATE   FKey *sqlite3FkReferences(Table *);
        10569  +#else
        10570  +  #define sqlite3FkActions(a,b,c,d)
        10571  +  #define sqlite3FkCheck(a,b,c,d)
        10572  +  #define sqlite3FkDropTable(a,b,c)
        10573  +  #define sqlite3FkOldmask(a,b)      0
        10574  +  #define sqlite3FkRequired(a,b,c,d) 0
        10575  +#endif
        10576  +#ifndef SQLITE_OMIT_FOREIGN_KEY
        10577  +SQLITE_PRIVATE   void sqlite3FkDelete(Table*);
        10578  +#else
        10579  +  #define sqlite3FkDelete(a)
        10580  +#endif
 10532  10581   
 10533  10582   
 10534  10583   /*
 10535  10584   ** Available fault injectors.  Should be numbered beginning with 0.
 10536  10585   */
 10537  10586   #define SQLITE_FAULTINJECTOR_MALLOC     0
 10538  10587   #define SQLITE_FAULTINJECTOR_COUNT      1
................................................................................
 11383  11432       y.Y = sLocal.tm_year + 1900;
 11384  11433       y.M = sLocal.tm_mon + 1;
 11385  11434       y.D = sLocal.tm_mday;
 11386  11435       y.h = sLocal.tm_hour;
 11387  11436       y.m = sLocal.tm_min;
 11388  11437       y.s = sLocal.tm_sec;
 11389  11438     }
 11390         -#elif defined(HAVE_LOCALTIME_S)
        11439  +#elif defined(HAVE_LOCALTIME_S) && HAVE_LOCALTIME_S
 11391  11440     {
 11392  11441       struct tm sLocal;
 11393  11442       localtime_s(&sLocal, &t);
 11394  11443       y.Y = sLocal.tm_year + 1900;
 11395  11444       y.M = sLocal.tm_mon + 1;
 11396  11445       y.D = sLocal.tm_mday;
 11397  11446       y.h = sLocal.tm_hour;
................................................................................
 13659  13708   }
 13660  13709   
 13661  13710   /*
 13662  13711   ** Deinitialize this module.
 13663  13712   */
 13664  13713   static void memsys3Shutdown(void *NotUsed){
 13665  13714     UNUSED_PARAMETER(NotUsed);
        13715  +  mem3.mutex = 0;
 13666  13716     return;
 13667  13717   }
 13668  13718   
 13669  13719   
 13670  13720   
 13671  13721   /*
 13672  13722   ** Open the file indicated and write a log of all unfreed memory 
................................................................................
 14499  14549     return p==0 || sqlite3GlobalConfig.mutex.xMutexHeld(p);
 14500  14550   }
 14501  14551   SQLITE_API int sqlite3_mutex_notheld(sqlite3_mutex *p){
 14502  14552     return p==0 || sqlite3GlobalConfig.mutex.xMutexNotheld(p);
 14503  14553   }
 14504  14554   #endif
 14505  14555   
 14506         -#endif /* SQLITE_OMIT_MUTEX */
        14556  +#endif /* SQLITE_MUTEX_OMIT */
 14507  14557   
 14508  14558   /************** End of mutex.c ***********************************************/
 14509  14559   /************** Begin file mutex_noop.c **************************************/
 14510  14560   /*
 14511  14561   ** 2008 October 07
 14512  14562   **
 14513  14563   ** The author disclaims copyright to this source code.  In place of
................................................................................
 17764  17814     int nChange;            /* Number of db changes made since last reset */
 17765  17815     int btreeMask;          /* Bitmask of db->aDb[] entries referenced */
 17766  17816     i64 startTime;          /* Time when query started - used for profiling */
 17767  17817     BtreeMutexArray aMutex; /* An array of Btree used here and needing locks */
 17768  17818     int aCounter[2];        /* Counters used by sqlite3_stmt_status() */
 17769  17819     char *zSql;             /* Text of the SQL statement that generated this */
 17770  17820     void *pFree;            /* Free this when deleting the vdbe */
        17821  +  i64 nFkConstraint;      /* Number of imm. FK constraints this VM */
        17822  +  i64 nStmtDefCons;       /* Number of def. constraints when stmt started */
 17771  17823     int iStatement;         /* Statement number (or 0 if has not opened stmt) */
 17772  17824   #ifdef SQLITE_DEBUG
 17773  17825     FILE *trace;            /* Write an execution trace here, if not NULL */
 17774  17826   #endif
 17775  17827     VdbeFrame *pFrame;      /* Parent frame */
 17776  17828     int nFrame;             /* Number of frames in pFrame list */
 17777  17829   };
................................................................................
 17835  17887   SQLITE_PRIVATE int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve);
 17836  17888   SQLITE_PRIVATE int sqlite3VdbeCloseStatement(Vdbe *, int);
 17837  17889   SQLITE_PRIVATE void sqlite3VdbeFrameDelete(VdbeFrame*);
 17838  17890   SQLITE_PRIVATE int sqlite3VdbeFrameRestore(VdbeFrame *);
 17839  17891   #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
 17840  17892   SQLITE_PRIVATE int sqlite3VdbeReleaseBuffers(Vdbe *p);
 17841  17893   #endif
        17894  +
        17895  +#ifndef SQLITE_OMIT_FOREIGN_KEY
        17896  +SQLITE_PRIVATE int sqlite3VdbeCheckFk(Vdbe *, int);
        17897  +#else
        17898  +# define sqlite3VdbeCheckFk(p,i) 0
        17899  +#endif
 17842  17900   
 17843  17901   #ifndef SQLITE_OMIT_SHARED_CACHE
 17844  17902   SQLITE_PRIVATE void sqlite3VdbeMutexArrayEnter(Vdbe *p);
 17845  17903   #else
 17846  17904   # define sqlite3VdbeMutexArrayEnter(p)
 17847  17905   #endif
 17848  17906   
................................................................................
 19797  19855        /*  21 */ "Trace",
 19798  19856        /*  22 */ "Function",
 19799  19857        /*  23 */ "IfNeg",
 19800  19858        /*  24 */ "Noop",
 19801  19859        /*  25 */ "Program",
 19802  19860        /*  26 */ "Return",
 19803  19861        /*  27 */ "NewRowid",
 19804         -     /*  28 */ "Variable",
 19805         -     /*  29 */ "String",
 19806         -     /*  30 */ "RealAffinity",
 19807         -     /*  31 */ "VRename",
 19808         -     /*  32 */ "ParseSchema",
 19809         -     /*  33 */ "VOpen",
 19810         -     /*  34 */ "Close",
 19811         -     /*  35 */ "CreateIndex",
 19812         -     /*  36 */ "IsUnique",
 19813         -     /*  37 */ "NotFound",
 19814         -     /*  38 */ "Int64",
 19815         -     /*  39 */ "MustBeInt",
 19816         -     /*  40 */ "Halt",
 19817         -     /*  41 */ "Rowid",
 19818         -     /*  42 */ "IdxLT",
 19819         -     /*  43 */ "AddImm",
 19820         -     /*  44 */ "RowData",
 19821         -     /*  45 */ "MemMax",
 19822         -     /*  46 */ "NotExists",
 19823         -     /*  47 */ "Gosub",
 19824         -     /*  48 */ "Integer",
 19825         -     /*  49 */ "Prev",
 19826         -     /*  50 */ "RowSetRead",
 19827         -     /*  51 */ "RowSetAdd",
 19828         -     /*  52 */ "VColumn",
 19829         -     /*  53 */ "CreateTable",
 19830         -     /*  54 */ "Last",
 19831         -     /*  55 */ "SeekLe",
 19832         -     /*  56 */ "IncrVacuum",
 19833         -     /*  57 */ "IdxRowid",
 19834         -     /*  58 */ "ResetCount",
 19835         -     /*  59 */ "Yield",
 19836         -     /*  60 */ "DropTrigger",
 19837         -     /*  61 */ "DropIndex",
 19838         -     /*  62 */ "Param",
 19839         -     /*  63 */ "IdxGE",
 19840         -     /*  64 */ "IdxDelete",
 19841         -     /*  65 */ "Vacuum",
 19842         -     /*  66 */ "Or",
 19843         -     /*  67 */ "And",
 19844         -     /*  68 */ "IfNot",
 19845         -     /*  69 */ "DropTable",
 19846         -     /*  70 */ "SeekLt",
 19847         -     /*  71 */ "IsNull",
 19848         -     /*  72 */ "NotNull",
 19849         -     /*  73 */ "Ne",
 19850         -     /*  74 */ "Eq",
 19851         -     /*  75 */ "Gt",
 19852         -     /*  76 */ "Le",
 19853         -     /*  77 */ "Lt",
 19854         -     /*  78 */ "Ge",
 19855         -     /*  79 */ "MakeRecord",
 19856         -     /*  80 */ "BitAnd",
 19857         -     /*  81 */ "BitOr",
 19858         -     /*  82 */ "ShiftLeft",
 19859         -     /*  83 */ "ShiftRight",
 19860         -     /*  84 */ "Add",
 19861         -     /*  85 */ "Subtract",
 19862         -     /*  86 */ "Multiply",
 19863         -     /*  87 */ "Divide",
 19864         -     /*  88 */ "Remainder",
 19865         -     /*  89 */ "Concat",
 19866         -     /*  90 */ "ResultRow",
 19867         -     /*  91 */ "Delete",
 19868         -     /*  92 */ "AggFinal",
        19862  +     /*  28 */ "FkCounter",
        19863  +     /*  29 */ "Variable",
        19864  +     /*  30 */ "String",
        19865  +     /*  31 */ "RealAffinity",
        19866  +     /*  32 */ "VRename",
        19867  +     /*  33 */ "ParseSchema",
        19868  +     /*  34 */ "VOpen",
        19869  +     /*  35 */ "Close",
        19870  +     /*  36 */ "CreateIndex",
        19871  +     /*  37 */ "IsUnique",
        19872  +     /*  38 */ "NotFound",
        19873  +     /*  39 */ "Int64",
        19874  +     /*  40 */ "MustBeInt",
        19875  +     /*  41 */ "Halt",
        19876  +     /*  42 */ "Rowid",
        19877  +     /*  43 */ "IdxLT",
        19878  +     /*  44 */ "AddImm",
        19879  +     /*  45 */ "RowData",
        19880  +     /*  46 */ "MemMax",
        19881  +     /*  47 */ "NotExists",
        19882  +     /*  48 */ "Gosub",
        19883  +     /*  49 */ "Integer",
        19884  +     /*  50 */ "Prev",
        19885  +     /*  51 */ "RowSetRead",
        19886  +     /*  52 */ "RowSetAdd",
        19887  +     /*  53 */ "VColumn",
        19888  +     /*  54 */ "CreateTable",
        19889  +     /*  55 */ "Last",
        19890  +     /*  56 */ "SeekLe",
        19891  +     /*  57 */ "IncrVacuum",
        19892  +     /*  58 */ "IdxRowid",
        19893  +     /*  59 */ "ResetCount",
        19894  +     /*  60 */ "Yield",
        19895  +     /*  61 */ "DropTrigger",
        19896  +     /*  62 */ "DropIndex",
        19897  +     /*  63 */ "Param",
        19898  +     /*  64 */ "IdxGE",
        19899  +     /*  65 */ "IdxDelete",
        19900  +     /*  66 */ "Vacuum",
        19901  +     /*  67 */ "IfNot",
        19902  +     /*  68 */ "Or",
        19903  +     /*  69 */ "And",
        19904  +     /*  70 */ "DropTable",
        19905  +     /*  71 */ "SeekLt",
        19906  +     /*  72 */ "MakeRecord",
        19907  +     /*  73 */ "IsNull",
        19908  +     /*  74 */ "NotNull",
        19909  +     /*  75 */ "Ne",
        19910  +     /*  76 */ "Eq",
        19911  +     /*  77 */ "Gt",
        19912  +     /*  78 */ "Le",
        19913  +     /*  79 */ "Lt",
        19914  +     /*  80 */ "Ge",
        19915  +     /*  81 */ "ResultRow",
        19916  +     /*  82 */ "BitAnd",
        19917  +     /*  83 */ "BitOr",
        19918  +     /*  84 */ "ShiftLeft",
        19919  +     /*  85 */ "ShiftRight",
        19920  +     /*  86 */ "Add",
        19921  +     /*  87 */ "Subtract",
        19922  +     /*  88 */ "Multiply",
        19923  +     /*  89 */ "Divide",
        19924  +     /*  90 */ "Remainder",
        19925  +     /*  91 */ "Concat",
        19926  +     /*  92 */ "Delete",
 19869  19927        /*  93 */ "BitNot",
 19870  19928        /*  94 */ "String8",
 19871         -     /*  95 */ "Compare",
 19872         -     /*  96 */ "Goto",
 19873         -     /*  97 */ "TableLock",
 19874         -     /*  98 */ "Clear",
 19875         -     /*  99 */ "VerifyCookie",
 19876         -     /* 100 */ "AggStep",
 19877         -     /* 101 */ "Transaction",
 19878         -     /* 102 */ "VFilter",
 19879         -     /* 103 */ "VDestroy",
 19880         -     /* 104 */ "Next",
 19881         -     /* 105 */ "Count",
 19882         -     /* 106 */ "IdxInsert",
 19883         -     /* 107 */ "SeekGe",
 19884         -     /* 108 */ "Insert",
 19885         -     /* 109 */ "Destroy",
 19886         -     /* 110 */ "ReadCookie",
 19887         -     /* 111 */ "RowSetTest",
 19888         -     /* 112 */ "LoadAnalysis",
 19889         -     /* 113 */ "Explain",
 19890         -     /* 114 */ "HaltIfNull",
 19891         -     /* 115 */ "OpenPseudo",
 19892         -     /* 116 */ "OpenEphemeral",
 19893         -     /* 117 */ "Null",
 19894         -     /* 118 */ "Move",
 19895         -     /* 119 */ "Blob",
 19896         -     /* 120 */ "Rewind",
 19897         -     /* 121 */ "SeekGt",
 19898         -     /* 122 */ "VBegin",
 19899         -     /* 123 */ "VUpdate",
 19900         -     /* 124 */ "IfZero",
 19901         -     /* 125 */ "VCreate",
 19902         -     /* 126 */ "Found",
 19903         -     /* 127 */ "IfPos",
 19904         -     /* 128 */ "NullRow",
 19905         -     /* 129 */ "Jump",
        19929  +     /*  95 */ "AggFinal",
        19930  +     /*  96 */ "Compare",
        19931  +     /*  97 */ "Goto",
        19932  +     /*  98 */ "TableLock",
        19933  +     /*  99 */ "Clear",
        19934  +     /* 100 */ "VerifyCookie",
        19935  +     /* 101 */ "AggStep",
        19936  +     /* 102 */ "Transaction",
        19937  +     /* 103 */ "VFilter",
        19938  +     /* 104 */ "VDestroy",
        19939  +     /* 105 */ "Next",
        19940  +     /* 106 */ "Count",
        19941  +     /* 107 */ "IdxInsert",
        19942  +     /* 108 */ "FkIfZero",
        19943  +     /* 109 */ "SeekGe",
        19944  +     /* 110 */ "Insert",
        19945  +     /* 111 */ "Destroy",
        19946  +     /* 112 */ "ReadCookie",
        19947  +     /* 113 */ "RowSetTest",
        19948  +     /* 114 */ "LoadAnalysis",
        19949  +     /* 115 */ "Explain",
        19950  +     /* 116 */ "HaltIfNull",
        19951  +     /* 117 */ "OpenPseudo",
        19952  +     /* 118 */ "OpenEphemeral",
        19953  +     /* 119 */ "Null",
        19954  +     /* 120 */ "Move",
        19955  +     /* 121 */ "Blob",
        19956  +     /* 122 */ "Rewind",
        19957  +     /* 123 */ "SeekGt",
        19958  +     /* 124 */ "VBegin",
        19959  +     /* 125 */ "VUpdate",
        19960  +     /* 126 */ "IfZero",
        19961  +     /* 127 */ "VCreate",
        19962  +     /* 128 */ "Found",
        19963  +     /* 129 */ "IfPos",
 19906  19964        /* 130 */ "Real",
 19907         -     /* 131 */ "Permutation",
 19908         -     /* 132 */ "NotUsed_132",
 19909         -     /* 133 */ "NotUsed_133",
        19965  +     /* 131 */ "NullRow",
        19966  +     /* 132 */ "Jump",
        19967  +     /* 133 */ "Permutation",
 19910  19968        /* 134 */ "NotUsed_134",
 19911  19969        /* 135 */ "NotUsed_135",
 19912  19970        /* 136 */ "NotUsed_136",
 19913  19971        /* 137 */ "NotUsed_137",
 19914  19972        /* 138 */ "NotUsed_138",
 19915  19973        /* 139 */ "ToText",
 19916  19974        /* 140 */ "ToBlob",
................................................................................
 22522  22580       }
 22523  22581       if( pLock==0 ){
 22524  22582         pLock = sqlite3_malloc( sizeof(*pLock) );
 22525  22583         if( pLock==0 ){
 22526  22584           rc = SQLITE_NOMEM;
 22527  22585           goto exit_findlockinfo;
 22528  22586         }
 22529         -      pLock->lockKey = lockKey;
        22587  +      memcpy(&pLock->lockKey,&lockKey,sizeof(lockKey));
 22530  22588         pLock->nRef = 1;
 22531  22589         pLock->cnt = 0;
 22532  22590         pLock->locktype = 0;
 22533  22591         pLock->pNext = lockList;
 22534  22592         pLock->pPrev = 0;
 22535  22593         if( lockList ) lockList->pPrev = pLock;
 22536  22594         lockList = pLock;
................................................................................
 47328  47386     assert( p->magic==VDBE_MAGIC_INIT );
 47329  47387     assert( j>=0 && j<p->nLabel );
 47330  47388     if( p->aLabel ){
 47331  47389       p->aLabel[j] = p->nOp;
 47332  47390     }
 47333  47391   }
 47334  47392   
 47335         -#ifdef SQLITE_DEBUG
        47393  +#ifdef SQLITE_DEBUG /* sqlite3AssertMayAbort() logic */
 47336  47394   
 47337  47395   /*
 47338  47396   ** The following type and function are used to iterate through all opcodes
 47339  47397   ** in a Vdbe main program and each of the sub-programs (triggers) it may 
 47340  47398   ** invoke directly or indirectly. It should be used as follows:
 47341  47399   **
 47342  47400   **   Op *pOp;
................................................................................
 47400  47458     }
 47401  47459   
 47402  47460     return pRet;
 47403  47461   }
 47404  47462   
 47405  47463   /*
 47406  47464   ** Check if the program stored in the VM associated with pParse may
 47407         -** throw an ABORT exception (causing the statement, but not transaction
        47465  +** throw an ABORT exception (causing the statement, but not entire transaction
 47408  47466   ** to be rolled back). This condition is true if the main program or any
 47409  47467   ** sub-programs contains any of the following:
 47410  47468   **
 47411  47469   **   *  OP_Halt with P1=SQLITE_CONSTRAINT and P2=OE_Abort.
 47412  47470   **   *  OP_HaltIfNull with P1=SQLITE_CONSTRAINT and P2=OE_Abort.
 47413  47471   **   *  OP_Destroy
 47414  47472   **   *  OP_VUpdate
 47415  47473   **   *  OP_VRename
        47474  +**   *  OP_FkCounter with P2==0 (immediate foreign key constraint)
 47416  47475   **
 47417  47476   ** Then check that the value of Parse.mayAbort is true if an
 47418  47477   ** ABORT may be thrown, or false otherwise. Return true if it does
 47419  47478   ** match, or false otherwise. This function is intended to be used as
 47420  47479   ** part of an assert statement in the compiler. Similar to:
 47421  47480   **
 47422  47481   **   assert( sqlite3VdbeAssertMayAbort(pParse->pVdbe, pParse->mayAbort) );
................................................................................
 47427  47486     VdbeOpIter sIter;
 47428  47487     memset(&sIter, 0, sizeof(sIter));
 47429  47488     sIter.v = v;
 47430  47489   
 47431  47490     while( (pOp = opIterNext(&sIter))!=0 ){
 47432  47491       int opcode = pOp->opcode;
 47433  47492       if( opcode==OP_Destroy || opcode==OP_VUpdate || opcode==OP_VRename 
        47493  +#ifndef SQLITE_OMIT_FOREIGN_KEY
        47494  +     || (opcode==OP_FkCounter && pOp->p1==0 && pOp->p2==1) 
        47495  +#endif
 47434  47496        || ((opcode==OP_Halt || opcode==OP_HaltIfNull) 
 47435  47497         && (pOp->p1==SQLITE_CONSTRAINT && pOp->p2==OE_Abort))
 47436  47498       ){
 47437  47499         hasAbort = 1;
 47438  47500         break;
 47439  47501       }
 47440  47502     }
................................................................................
 47443  47505     /* Return true if hasAbort==mayAbort. Or if a malloc failure occured.
 47444  47506     ** If malloc failed, then the while() loop above may not have iterated
 47445  47507     ** through all opcodes and hasAbort may be set incorrectly. Return
 47446  47508     ** true for this case to prevent the assert() in the callers frame
 47447  47509     ** from failing.  */
 47448  47510     return ( v->db->mallocFailed || hasAbort==mayAbort );
 47449  47511   }
 47450         -#endif
        47512  +#endif /* SQLITE_DEBUG - the sqlite3AssertMayAbort() function */
 47451  47513   
 47452  47514   /*
 47453  47515   ** Loop through the program looking for P2 values that are negative
 47454  47516   ** on jump instructions.  Each such value is a label.  Resolve the
 47455  47517   ** label by setting the P2 value to its correct non-zero value.
 47456  47518   **
 47457  47519   ** This routine is called once after all opcodes have been inserted.
................................................................................
 48983  49045           if( rc==SQLITE_OK ){
 48984  49046             rc = rc2;
 48985  49047           }
 48986  49048         }
 48987  49049       }
 48988  49050       db->nStatement--;
 48989  49051       p->iStatement = 0;
        49052  +
        49053  +    /* If the statement transaction is being rolled back, also restore the 
        49054  +    ** database handles deferred constraint counter to the value it had when 
        49055  +    ** the statement transaction was opened.  */
        49056  +    if( eOp==SAVEPOINT_ROLLBACK ){
        49057  +      db->nDeferredCons = p->nStmtDefCons;
        49058  +    }
 48990  49059     }
 48991  49060     return rc;
 48992  49061   }
 48993  49062   
 48994  49063   /*
 48995  49064   ** If SQLite is compiled to support shared-cache mode and to be threadsafe,
 48996  49065   ** this routine obtains the mutex associated with each BtShared structure
................................................................................
 49014  49083     sqlite3BtreeMutexArrayEnter(&p->aMutex);
 49015  49084   #else
 49016  49085     sqlite3BtreeEnterAll(p->db);
 49017  49086   #endif
 49018  49087   }
 49019  49088   #endif
 49020  49089   
        49090  +/*
        49091  +** This function is called when a transaction opened by the database 
        49092  +** handle associated with the VM passed as an argument is about to be 
        49093  +** committed. If there are outstanding deferred foreign key constraint
        49094  +** violations, return SQLITE_ERROR. Otherwise, SQLITE_OK.
        49095  +**
        49096  +** If there are outstanding FK violations and this function returns 
        49097  +** SQLITE_ERROR, set the result of the VM to SQLITE_CONSTRAINT and write
        49098  +** an error message to it. Then return SQLITE_ERROR.
        49099  +*/
        49100  +#ifndef SQLITE_OMIT_FOREIGN_KEY
        49101  +SQLITE_PRIVATE int sqlite3VdbeCheckFk(Vdbe *p, int deferred){
        49102  +  sqlite3 *db = p->db;
        49103  +  if( (deferred && db->nDeferredCons>0) || (!deferred && p->nFkConstraint>0) ){
        49104  +    p->rc = SQLITE_CONSTRAINT;
        49105  +    p->errorAction = OE_Abort;
        49106  +    sqlite3SetString(&p->zErrMsg, db, "foreign key constraint failed");
        49107  +    return SQLITE_ERROR;
        49108  +  }
        49109  +  return SQLITE_OK;
        49110  +}
        49111  +#endif
        49112  +
 49021  49113   /*
 49022  49114   ** This routine is called the when a VDBE tries to halt.  If the VDBE
 49023  49115   ** has made changes and is in autocommit mode, then commit those
 49024  49116   ** changes.  If a rollback is needed, then do the rollback.
 49025  49117   **
 49026  49118   ** This routine is the only way to move the state of a VM from
 49027  49119   ** SQLITE_MAGIC_RUN to SQLITE_MAGIC_HALT.  It is harmless to
................................................................................
 49088  49180             invalidateCursorsOnModifiedBtrees(db);
 49089  49181             sqlite3RollbackAll(db);
 49090  49182             sqlite3CloseSavepoints(db);
 49091  49183             db->autoCommit = 1;
 49092  49184           }
 49093  49185         }
 49094  49186       }
        49187  +
        49188  +    /* Check for immediate foreign key violations. */
        49189  +    if( p->rc==SQLITE_OK ){
        49190  +      sqlite3VdbeCheckFk(p, 0);
        49191  +    }
 49095  49192     
 49096  49193       /* If the auto-commit flag is set and this is the only active writer 
 49097  49194       ** VM, then we do either a commit or rollback of the current transaction. 
 49098  49195       **
 49099  49196       ** Note: This block also runs if one of the special errors handled 
 49100  49197       ** above has occurred. 
 49101  49198       */
 49102  49199       if( !sqlite3VtabInSync(db) 
 49103  49200        && db->autoCommit 
 49104  49201        && db->writeVdbeCnt==(p->readOnly==0) 
 49105  49202       ){
 49106  49203         if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){
 49107         -        /* The auto-commit flag is true, and the vdbe program was 
 49108         -        ** successful or hit an 'OR FAIL' constraint. This means a commit 
 49109         -        ** is required.
 49110         -        */
        49204  +        if( sqlite3VdbeCheckFk(p, 1) ){
        49205  +          sqlite3BtreeMutexArrayLeave(&p->aMutex);
        49206  +          return SQLITE_ERROR;
        49207  +        }
        49208  +        /* The auto-commit flag is true, the vdbe program was successful 
        49209  +        ** or hit an 'OR FAIL' constraint and there are no deferred foreign
        49210  +        ** key constraints to hold up the transaction. This means a commit 
        49211  +        ** is required.  */
 49111  49212           rc = vdbeCommit(db, p);
 49112  49213           if( rc==SQLITE_BUSY ){
 49113  49214             sqlite3BtreeMutexArrayLeave(&p->aMutex);
 49114  49215             return SQLITE_BUSY;
 49115  49216           }else if( rc!=SQLITE_OK ){
 49116  49217             p->rc = rc;
 49117  49218             sqlite3RollbackAll(db);
 49118  49219           }else{
        49220  +          db->nDeferredCons = 0;
 49119  49221             sqlite3CommitInternalChanges(db);
 49120  49222           }
 49121  49223         }else{
 49122  49224           sqlite3RollbackAll(db);
 49123  49225         }
 49124  49226         db->nStatement = 0;
 49125  49227       }else if( eStatementOp==0 ){
................................................................................
 50392  50494       ** reset the interrupt flag.  This prevents a call to sqlite3_interrupt
 50393  50495       ** from interrupting a statement that has not yet started.
 50394  50496       */
 50395  50497       if( db->activeVdbeCnt==0 ){
 50396  50498         db->u1.isInterrupted = 0;
 50397  50499       }
 50398  50500   
        50501  +    assert( db->writeVdbeCnt>0 || db->autoCommit==0 || db->nDeferredCons==0 );
        50502  +
 50399  50503   #ifndef SQLITE_OMIT_TRACE
 50400  50504       if( db->xProfile && !db->init.busy ){
 50401  50505         double rNow;
 50402  50506         sqlite3OsCurrentTime(db->pVfs, &rNow);
 50403  50507         p->startTime = (u64)((rNow - (int)rNow)*3600.0*24.0*1000000000.0);
 50404  50508       }
 50405  50509   #endif
................................................................................
 51373  51477   static void updateMaxBlobsize(Mem *p){
 51374  51478     if( (p->flags & (MEM_Str|MEM_Blob))!=0 && p->n>sqlite3_max_blobsize ){
 51375  51479       sqlite3_max_blobsize = p->n;
 51376  51480     }
 51377  51481   }
 51378  51482   #endif
 51379  51483   
        51484  +/*
        51485  +** The next global variable is incremented each type the OP_Found opcode
        51486  +** is executed. This is used to test whether or not the foreign key
        51487  +** operation implemented using OP_FkIsZero is working. This variable
        51488  +** has no function other than to help verify the correct operation of the
        51489  +** library.
        51490  +*/
        51491  +#ifdef SQLITE_TEST
        51492  +SQLITE_API int sqlite3_found_count = 0;
        51493  +#endif
        51494  +
 51380  51495   /*
 51381  51496   ** Test a register to see if it exceeds the current maximum blob size.
 51382  51497   ** If it does, record the new maximum blob size.
 51383  51498   */
 51384  51499   #if defined(SQLITE_TEST) && !defined(SQLITE_OMIT_BUILTIN_TEST)
 51385  51500   # define UPDATE_MAX_BLOBSIZE(P)  updateMaxBlobsize(P)
 51386  51501   #else
................................................................................
 51982  52097         int n;
 51983  52098       } ag;
 51984  52099       struct OP_ShiftRight_stack_vars {
 51985  52100         i64 a;
 51986  52101         i64 b;
 51987  52102       } ah;
 51988  52103       struct OP_Ge_stack_vars {
 51989         -      int flags;
 51990         -      int res;
 51991         -      char affinity;
        52104  +      int res;            /* Result of the comparison of pIn1 against pIn3 */
        52105  +      char affinity;      /* Affinity to use for comparison */
 51992  52106       } ai;
 51993  52107       struct OP_Compare_stack_vars {
 51994  52108         int n;
 51995  52109         int i;
 51996  52110         int p1;
 51997  52111         int p2;
 51998  52112         const KeyInfo *pKeyInfo;
................................................................................
 52645  52759     p->rc = pOp->p1;
 52646  52760     p->errorAction = (u8)pOp->p2;
 52647  52761     p->pc = pc;
 52648  52762     if( pOp->p4.z ){
 52649  52763       sqlite3SetString(&p->zErrMsg, db, "%s", pOp->p4.z);
 52650  52764     }
 52651  52765     rc = sqlite3VdbeHalt(p);
 52652         -  assert( rc==SQLITE_BUSY || rc==SQLITE_OK );
        52766  +  assert( rc==SQLITE_BUSY || rc==SQLITE_OK || rc==SQLITE_ERROR );
 52653  52767     if( rc==SQLITE_BUSY ){
 52654  52768       p->rc = rc = SQLITE_BUSY;
 52655  52769     }else{
        52770  +    assert( rc==SQLITE_OK || p->rc==SQLITE_CONSTRAINT );
        52771  +    assert( rc==SQLITE_OK || db->nDeferredCons>0 );
 52656  52772       rc = p->rc ? SQLITE_ERROR : SQLITE_DONE;
 52657  52773     }
 52658  52774     goto vdbe_return;
 52659  52775   }
 52660  52776   
 52661  52777   /* Opcode: Integer P1 P2 * * *
 52662  52778   **
................................................................................
 52893  53009   #if 0  /* local variables moved into u.ad */
 52894  53010     Mem *pMem;
 52895  53011     int i;
 52896  53012   #endif /* local variables moved into u.ad */
 52897  53013     assert( p->nResColumn==pOp->p2 );
 52898  53014     assert( pOp->p1>0 );
 52899  53015     assert( pOp->p1+pOp->p2<=p->nMem+1 );
        53016  +
        53017  +  /* If this statement has violated immediate foreign key constraints, do
        53018  +  ** not return the number of rows modified. And do not RELEASE the statement
        53019  +  ** transaction. It needs to be rolled back.  */
        53020  +  if( SQLITE_OK!=(rc = sqlite3VdbeCheckFk(p, 0)) ){
        53021  +    assert( db->flags&SQLITE_CountRows );
        53022  +    assert( p->usesStmtJournal );
        53023  +    break;
        53024  +  }
 52900  53025   
 52901  53026     /* If the SQLITE_CountRows flag is set in sqlite3.flags mask, then
 52902  53027     ** DML statements invoke this opcode to return the number of rows
 52903  53028     ** modified to the user. This is the only way that a VM that
 52904  53029     ** opens a statement transaction may invoke this opcode.
 52905  53030     **
 52906  53031     ** In case this is such a statement, close any statement transaction
................................................................................
 53465  53590   ** store a boolean result (either 0, or 1, or NULL) in register P2.
 53466  53591   */
 53467  53592   /* Opcode: Ne P1 P2 P3 P4 P5
 53468  53593   **
 53469  53594   ** This works just like the Lt opcode except that the jump is taken if
 53470  53595   ** the operands in registers P1 and P3 are not equal.  See the Lt opcode for
 53471  53596   ** additional information.
        53597  +**
        53598  +** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either
        53599  +** true or false and is never NULL.  If both operands are NULL then the result
        53600  +** of comparison is false.  If either operand is NULL then the result is true.
        53601  +** If neither operand is NULL the the result is the same as it would be if
        53602  +** the SQLITE_NULLEQ flag were omitted from P5.
 53472  53603   */
 53473  53604   /* Opcode: Eq P1 P2 P3 P4 P5
 53474  53605   **
 53475  53606   ** This works just like the Lt opcode except that the jump is taken if
 53476  53607   ** the operands in registers P1 and P3 are equal.
 53477  53608   ** See the Lt opcode for additional information.
        53609  +**
        53610  +** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either
        53611  +** true or false and is never NULL.  If both operands are NULL then the result
        53612  +** of comparison is true.  If either operand is NULL then the result is false.
        53613  +** If neither operand is NULL the the result is the same as it would be if
        53614  +** the SQLITE_NULLEQ flag were omitted from P5.
 53478  53615   */
 53479  53616   /* Opcode: Le P1 P2 P3 P4 P5
 53480  53617   **
 53481  53618   ** This works just like the Lt opcode except that the jump is taken if
 53482  53619   ** the content of register P3 is less than or equal to the content of
 53483  53620   ** register P1.  See the Lt opcode for additional information.
 53484  53621   */
................................................................................
 53497  53634   case OP_Eq:               /* same as TK_EQ, jump, in1, in3 */
 53498  53635   case OP_Ne:               /* same as TK_NE, jump, in1, in3 */
 53499  53636   case OP_Lt:               /* same as TK_LT, jump, in1, in3 */
 53500  53637   case OP_Le:               /* same as TK_LE, jump, in1, in3 */
 53501  53638   case OP_Gt:               /* same as TK_GT, jump, in1, in3 */
 53502  53639   case OP_Ge: {             /* same as TK_GE, jump, in1, in3 */
 53503  53640   #if 0  /* local variables moved into u.ai */
 53504         -  int flags;
 53505         -  int res;
 53506         -  char affinity;
        53641  +  int res;            /* Result of the comparison of pIn1 against pIn3 */
        53642  +  char affinity;      /* Affinity to use for comparison */
 53507  53643   #endif /* local variables moved into u.ai */
 53508  53644   
 53509         -  u.ai.flags = pIn1->flags|pIn3->flags;
 53510         -
 53511         -  if( u.ai.flags&MEM_Null ){
 53512         -    /* If either operand is NULL then the result is always NULL.
 53513         -    ** The jump is taken if the SQLITE_JUMPIFNULL bit is set.
 53514         -    */
 53515         -    if( pOp->p5 & SQLITE_STOREP2 ){
 53516         -      pOut = &p->aMem[pOp->p2];
 53517         -      MemSetTypeFlag(pOut, MEM_Null);
 53518         -      REGISTER_TRACE(pOp->p2, pOut);
 53519         -    }else if( pOp->p5 & SQLITE_JUMPIFNULL ){
 53520         -      pc = pOp->p2-1;
 53521         -    }
 53522         -    break;
 53523         -  }
 53524         -
 53525         -  u.ai.affinity = pOp->p5 & SQLITE_AFF_MASK;
 53526         -  if( u.ai.affinity ){
 53527         -    applyAffinity(pIn1, u.ai.affinity, encoding);
 53528         -    applyAffinity(pIn3, u.ai.affinity, encoding);
 53529         -    if( db->mallocFailed ) goto no_mem;
 53530         -  }
 53531         -
 53532         -  assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 );
 53533         -  ExpandBlob(pIn1);
 53534         -  ExpandBlob(pIn3);
 53535         -  u.ai.res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl);
        53645  +  if( (pIn1->flags | pIn3->flags)&MEM_Null ){
        53646  +    /* One or both operands are NULL */
        53647  +    if( pOp->p5 & SQLITE_NULLEQ ){
        53648  +      /* If SQLITE_NULLEQ is set (which will only happen if the operator is
        53649  +      ** OP_Eq or OP_Ne) then take the jump or not depending on whether
        53650  +      ** or not both operands are null.
        53651  +      */
        53652  +      assert( pOp->opcode==OP_Eq || pOp->opcode==OP_Ne );
        53653  +      u.ai.res = (pIn1->flags & pIn3->flags & MEM_Null)==0;
        53654  +    }else{
        53655  +      /* SQLITE_NULLEQ is clear and at least one operand is NULL,
        53656  +      ** then the result is always NULL.
        53657  +      ** The jump is taken if the SQLITE_JUMPIFNULL bit is set.
        53658  +      */
        53659  +      if( pOp->p5 & SQLITE_STOREP2 ){
        53660  +        pOut = &p->aMem[pOp->p2];
        53661  +        MemSetTypeFlag(pOut, MEM_Null);
        53662  +        REGISTER_TRACE(pOp->p2, pOut);
        53663  +      }else if( pOp->p5 & SQLITE_JUMPIFNULL ){
        53664  +        pc = pOp->p2-1;
        53665  +      }
        53666  +      break;
        53667  +    }
        53668  +  }else{
        53669  +    /* Neither operand is NULL.  Do a comparison. */
        53670  +    u.ai.affinity = pOp->p5 & SQLITE_AFF_MASK;
        53671  +    if( u.ai.affinity ){
        53672  +      applyAffinity(pIn1, u.ai.affinity, encoding);
        53673  +      applyAffinity(pIn3, u.ai.affinity, encoding);
        53674  +      if( db->mallocFailed ) goto no_mem;
        53675  +    }
        53676  +
        53677  +    assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 );
        53678  +    ExpandBlob(pIn1);
        53679  +    ExpandBlob(pIn3);
        53680  +    u.ai.res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl);
        53681  +  }
 53536  53682     switch( pOp->opcode ){
 53537  53683       case OP_Eq:    u.ai.res = u.ai.res==0;     break;
 53538  53684       case OP_Ne:    u.ai.res = u.ai.res!=0;     break;
 53539  53685       case OP_Lt:    u.ai.res = u.ai.res<0;      break;
 53540  53686       case OP_Le:    u.ai.res = u.ai.res<=0;     break;
 53541  53687       case OP_Gt:    u.ai.res = u.ai.res>0;      break;
 53542  53688       default:       u.ai.res = u.ai.res>=0;     break;
................................................................................
 53596  53742   #endif /* local variables moved into u.aj */
 53597  53743   
 53598  53744     u.aj.n = pOp->p3;
 53599  53745     u.aj.pKeyInfo = pOp->p4.pKeyInfo;
 53600  53746     assert( u.aj.n>0 );
 53601  53747     assert( u.aj.pKeyInfo!=0 );
 53602  53748     u.aj.p1 = pOp->p1;
 53603         -  assert( u.aj.p1>0 && u.aj.p1+u.aj.n<=p->nMem+1 );
 53604  53749     u.aj.p2 = pOp->p2;
 53605         -  assert( u.aj.p2>0 && u.aj.p2+u.aj.n<=p->nMem+1 );
        53750  +#if SQLITE_DEBUG
        53751  +  if( aPermute ){
        53752  +    int k, mx = 0;
        53753  +    for(k=0; k<u.aj.n; k++) if( aPermute[k]>mx ) mx = aPermute[k];
        53754  +    assert( u.aj.p1>0 && u.aj.p1+mx<=p->nMem+1 );
        53755  +    assert( u.aj.p2>0 && u.aj.p2+mx<=p->nMem+1 );
        53756  +  }else{
        53757  +    assert( u.aj.p1>0 && u.aj.p1+u.aj.n<=p->nMem+1 );
        53758  +    assert( u.aj.p2>0 && u.aj.p2+u.aj.n<=p->nMem+1 );
        53759  +  }
        53760  +#endif /* SQLITE_DEBUG */
 53606  53761     for(u.aj.i=0; u.aj.i<u.aj.n; u.aj.i++){
 53607  53762       u.aj.idx = aPermute ? aPermute[u.aj.i] : u.aj.i;
 53608  53763       REGISTER_TRACE(u.aj.p1+u.aj.idx, &p->aMem[u.aj.p1+u.aj.idx]);
 53609  53764       REGISTER_TRACE(u.aj.p2+u.aj.idx, &p->aMem[u.aj.p2+u.aj.idx]);
 53610  53765       assert( u.aj.i<u.aj.pKeyInfo->nField );
 53611  53766       u.aj.pColl = u.aj.pKeyInfo->aColl[u.aj.i];
 53612  53767       u.aj.bRev = u.aj.pKeyInfo->aSortOrder[u.aj.i];
................................................................................
 54313  54468           }else{
 54314  54469             db->nSavepoint++;
 54315  54470           }
 54316  54471   
 54317  54472           /* Link the new savepoint into the database handle's list. */
 54318  54473           u.aq.pNew->pNext = db->pSavepoint;
 54319  54474           db->pSavepoint = u.aq.pNew;
        54475  +        u.aq.pNew->nDeferredCons = db->nDeferredCons;
 54320  54476         }
 54321  54477       }
 54322  54478     }else{
 54323  54479       u.aq.iSavepoint = 0;
 54324  54480   
 54325  54481       /* Find the named savepoint. If there is no such savepoint, then an
 54326  54482       ** an error is returned to the user.  */
................................................................................
 54350  54506   
 54351  54507         /* Determine whether or not this is a transaction savepoint. If so,
 54352  54508         ** and this is a RELEASE command, then the current transaction
 54353  54509         ** is committed.
 54354  54510         */
 54355  54511         int isTransaction = u.aq.pSavepoint->pNext==0 && db->isTransactionSavepoint;
 54356  54512         if( isTransaction && u.aq.p1==SAVEPOINT_RELEASE ){
        54513  +        if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
        54514  +          goto vdbe_return;
        54515  +        }
 54357  54516           db->autoCommit = 1;
 54358  54517           if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
 54359  54518             p->pc = pc;
 54360  54519             db->autoCommit = 0;
 54361  54520             p->rc = rc = SQLITE_BUSY;
 54362  54521             goto vdbe_return;
 54363  54522           }
................................................................................
 54382  54541         while( db->pSavepoint!=u.aq.pSavepoint ){
 54383  54542           u.aq.pTmp = db->pSavepoint;
 54384  54543           db->pSavepoint = u.aq.pTmp->pNext;
 54385  54544           sqlite3DbFree(db, u.aq.pTmp);
 54386  54545           db->nSavepoint--;
 54387  54546         }
 54388  54547   
 54389         -      /* If it is a RELEASE, then destroy the savepoint being operated on too */
        54548  +      /* If it is a RELEASE, then destroy the savepoint being operated on
        54549  +      ** too. If it is a ROLLBACK TO, then set the number of deferred
        54550  +      ** constraint violations present in the database to the value stored
        54551  +      ** when the savepoint was created.  */
 54390  54552         if( u.aq.p1==SAVEPOINT_RELEASE ){
 54391  54553           assert( u.aq.pSavepoint==db->pSavepoint );
 54392  54554           db->pSavepoint = u.aq.pSavepoint->pNext;
 54393  54555           sqlite3DbFree(db, u.aq.pSavepoint);
 54394  54556           if( !isTransaction ){
 54395  54557             db->nSavepoint--;
 54396  54558           }
        54559  +      }else{
        54560  +        db->nDeferredCons = u.aq.pSavepoint->nDeferredCons;
 54397  54561         }
 54398  54562       }
 54399  54563     }
 54400  54564   
 54401  54565     break;
 54402  54566   }
 54403  54567   
................................................................................
 54440  54604           "SQL statements in progress");
 54441  54605       rc = SQLITE_BUSY;
 54442  54606     }else if( u.ar.desiredAutoCommit!=db->autoCommit ){
 54443  54607       if( u.ar.iRollback ){
 54444  54608         assert( u.ar.desiredAutoCommit==1 );
 54445  54609         sqlite3RollbackAll(db);
 54446  54610         db->autoCommit = 1;
        54611  +    }else if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
        54612  +      goto vdbe_return;
 54447  54613       }else{
 54448  54614         db->autoCommit = (u8)u.ar.desiredAutoCommit;
 54449  54615         if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
 54450  54616           p->pc = pc;
 54451  54617           db->autoCommit = (u8)(1-u.ar.desiredAutoCommit);
 54452  54618           p->rc = rc = SQLITE_BUSY;
 54453  54619           goto vdbe_return;
................................................................................
 54515  54681     if( u.as.pBt ){
 54516  54682       rc = sqlite3BtreeBeginTrans(u.as.pBt, pOp->p2);
 54517  54683       if( rc==SQLITE_BUSY ){
 54518  54684         p->pc = pc;
 54519  54685         p->rc = rc = SQLITE_BUSY;
 54520  54686         goto vdbe_return;
 54521  54687       }
 54522         -    if( rc!=SQLITE_OK && rc!=SQLITE_READONLY /* && rc!=SQLITE_BUSY */ ){
        54688  +    if( rc!=SQLITE_OK ){
 54523  54689         goto abort_due_to_error;
 54524  54690       }
 54525  54691   
 54526  54692       if( pOp->p2 && p->usesStmtJournal
 54527  54693        && (db->autoCommit==0 || db->activeVdbeCnt>1)
 54528  54694       ){
 54529  54695         assert( sqlite3BtreeIsInTrans(u.as.pBt) );
 54530  54696         if( p->iStatement==0 ){
 54531  54697           assert( db->nStatement>=0 && db->nSavepoint>=0 );
 54532  54698           db->nStatement++;
 54533  54699           p->iStatement = db->nSavepoint + db->nStatement;
 54534  54700         }
 54535  54701         rc = sqlite3BtreeBeginStmt(u.as.pBt, p->iStatement);
        54702  +
        54703  +      /* Store the current value of the database handles deferred constraint
        54704  +      ** counter. If the statement transaction needs to be rolled back,
        54705  +      ** the value of this counter needs to be restored too.  */
        54706  +      p->nStmtDefCons = db->nDeferredCons;
 54536  54707       }
 54537  54708     }
 54538  54709     break;
 54539  54710   }
 54540  54711   
 54541  54712   /* Opcode: ReadCookie P1 P2 P3 * *
 54542  54713   **
................................................................................
 55148  55319   #if 0  /* local variables moved into u.bb */
 55149  55320     int alreadyExists;
 55150  55321     VdbeCursor *pC;
 55151  55322     int res;
 55152  55323     UnpackedRecord *pIdxKey;
 55153  55324     char aTempRec[ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*3 + 7];
 55154  55325   #endif /* local variables moved into u.bb */
        55326  +
        55327  +#ifdef SQLITE_TEST
        55328  +  sqlite3_found_count++;
        55329  +#endif
 55155  55330   
 55156  55331     u.bb.alreadyExists = 0;
 55157  55332     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 55158  55333     u.bb.pC = p->apCsr[pOp->p1];
 55159  55334     assert( u.bb.pC!=0 );
 55160  55335     if( ALWAYS(u.bb.pC->pCursor!=0) ){
 55161  55336   
................................................................................
 56608  56783     void *t;                /* Token identifying trigger */
 56609  56784   #endif /* local variables moved into u.bz */
 56610  56785   
 56611  56786     u.bz.pProgram = pOp->p4.pProgram;
 56612  56787     u.bz.pRt = &p->aMem[pOp->p3];
 56613  56788     assert( u.bz.pProgram->nOp>0 );
 56614  56789   
 56615         -  /* If the SQLITE_RecTriggers flag is clear, then recursive invocation of
 56616         -  ** triggers is disabled for backwards compatibility (flag set/cleared by
 56617         -  ** the "PRAGMA recursive_triggers" command).
        56790  +  /* If the p5 flag is clear, then recursive invocation of triggers is
        56791  +  ** disabled for backwards compatibility (p5 is set if this sub-program
        56792  +  ** is really a trigger, not a foreign key action, and the flag set
        56793  +  ** and cleared by the "PRAGMA recursive_triggers" command is clear).
 56618  56794     **
 56619  56795     ** It is recursive invocation of triggers, at the SQL level, that is
 56620  56796     ** disabled. In some cases a single trigger may generate more than one
 56621  56797     ** SubProgram (if the trigger may be executed with more than one different
 56622  56798     ** ON CONFLICT algorithm). SubProgram structures associated with a
 56623  56799     ** single trigger all have the same value for the SubProgram.token
 56624         -  ** variable.
 56625         -  */
 56626         -  if( 0==(db->flags&SQLITE_RecTriggers) ){
        56800  +  ** variable.  */
        56801  +  if( pOp->p5 ){
 56627  56802       u.bz.t = u.bz.pProgram->token;
 56628  56803       for(u.bz.pFrame=p->pFrame; u.bz.pFrame && u.bz.pFrame->token!=u.bz.t; u.bz.pFrame=u.bz.pFrame->pParent);
 56629  56804       if( u.bz.pFrame ) break;
 56630  56805     }
 56631  56806   
 56632         -  if( p->nFrame>db->aLimit[SQLITE_LIMIT_TRIGGER_DEPTH] ){
        56807  +  if( p->nFrame>=db->aLimit[SQLITE_LIMIT_TRIGGER_DEPTH] ){
 56633  56808       rc = SQLITE_ERROR;
 56634  56809       sqlite3SetString(&p->zErrMsg, db, "too many levels of trigger recursion");
 56635  56810       break;
 56636  56811     }
 56637  56812   
 56638  56813     /* Register u.bz.pRt is used to store the memory required to save the state
 56639  56814     ** of the current program, and the memory required at runtime to execute
................................................................................
 56719  56894     u.ca.pIn = &u.ca.pFrame->aMem[pOp->p1 + u.ca.pFrame->aOp[u.ca.pFrame->pc].p1];
 56720  56895     sqlite3VdbeMemShallowCopy(pOut, u.ca.pIn, MEM_Ephem);
 56721  56896     break;
 56722  56897   }
 56723  56898   
 56724  56899   #endif /* #ifndef SQLITE_OMIT_TRIGGER */
 56725  56900   
        56901  +#ifndef SQLITE_OMIT_FOREIGN_KEY
        56902  +/* Opcode: FkCounter P1 P2 * * *
        56903  +**
        56904  +** Increment a "constraint counter" by P2 (P2 may be negative or positive).
        56905  +** If P1 is non-zero, the database constraint counter is incremented 
        56906  +** (deferred foreign key constraints). Otherwise, if P1 is zero, the 
        56907  +** statement counter is incremented (immediate foreign key constraints).
        56908  +*/
        56909  +case OP_FkCounter: {
        56910  +  if( pOp->p1 ){
        56911  +    db->nDeferredCons += pOp->p2;
        56912  +  }else{
        56913  +    p->nFkConstraint += pOp->p2;
        56914  +  }
        56915  +  break;
        56916  +}
        56917  +
        56918  +/* Opcode: FkIfZero P1 P2 * * *
        56919  +**
        56920  +** This opcode tests if a foreign key constraint-counter is currently zero.
        56921  +** If so, jump to instruction P2. Otherwise, fall through to the next 
        56922  +** instruction.
        56923  +**
        56924  +** If P1 is non-zero, then the jump is taken if the database constraint-counter
        56925  +** is zero (the one that counts deferred constraint violations). If P1 is
        56926  +** zero, the jump is taken if the statement constraint-counter is zero
        56927  +** (immediate foreign key constraint violations).
        56928  +*/
        56929  +case OP_FkIfZero: {         /* jump */
        56930  +  if( pOp->p1 ){
        56931  +    if( db->nDeferredCons==0 ) pc = pOp->p2-1;
        56932  +  }else{
        56933  +    if( p->nFkConstraint==0 ) pc = pOp->p2-1;
        56934  +  }
        56935  +  break;
        56936  +}
        56937  +#endif /* #ifndef SQLITE_OMIT_FOREIGN_KEY */
        56938  +
 56726  56939   #ifndef SQLITE_OMIT_AUTOINCREMENT
 56727  56940   /* Opcode: MemMax P1 P2 * * *
 56728  56941   **
 56729  56942   ** P1 is a register in the root frame of this VM (the root frame is
 56730  56943   ** different from the current frame if this instruction is being executed
 56731  56944   ** within a sub-program). Set the value of register P1 to the maximum of 
 56732  56945   ** its current value and the value in register P2.
................................................................................
 57669  57882         rc = SQLITE_ERROR;
 57670  57883         (void)sqlite3SafetyOff(db);
 57671  57884         sqlite3BtreeLeaveAll(db);
 57672  57885         goto blob_open_out;
 57673  57886       }
 57674  57887   
 57675  57888       /* If the value is being opened for writing, check that the
 57676         -    ** column is not indexed. It is against the rules to open an
 57677         -    ** indexed column for writing.
 57678         -    */
        57889  +    ** column is not indexed, and that it is not part of a foreign key. 
        57890  +    ** It is against the rules to open a column to which either of these
        57891  +    ** descriptions applies for writing.  */
 57679  57892       if( flags ){
        57893  +      const char *zFault = 0;
 57680  57894         Index *pIdx;
        57895  +#ifndef SQLITE_OMIT_FOREIGN_KEY
        57896  +      if( db->flags&SQLITE_ForeignKeys ){
        57897  +        /* Check that the column is not part of an FK child key definition. It
        57898  +        ** is not necessary to check if it is part of a parent key, as parent
        57899  +        ** key columns must be indexed. The check below will pick up this 
        57900  +        ** case.  */
        57901  +        FKey *pFKey;
        57902  +        for(pFKey=pTab->pFKey; pFKey; pFKey=pFKey->pNextFrom){
        57903  +          int j;
        57904  +          for(j=0; j<pFKey->nCol; j++){
        57905  +            if( pFKey->aCol[j].iFrom==iCol ){
        57906  +              zFault = "foreign key";
        57907  +            }
        57908  +          }
        57909  +        }
        57910  +      }
        57911  +#endif
 57681  57912         for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
 57682  57913           int j;
 57683  57914           for(j=0; j<pIdx->nColumn; j++){
 57684  57915             if( pIdx->aiColumn[j]==iCol ){
 57685         -            sqlite3DbFree(db, zErr);
 57686         -            zErr = sqlite3MPrintf(db,
 57687         -                             "cannot open indexed column for writing");
 57688         -            rc = SQLITE_ERROR;
 57689         -            (void)sqlite3SafetyOff(db);
 57690         -            sqlite3BtreeLeaveAll(db);
 57691         -            goto blob_open_out;
        57916  +            zFault = "indexed";
 57692  57917             }
 57693  57918           }
 57694  57919         }
        57920  +      if( zFault ){
        57921  +        sqlite3DbFree(db, zErr);
        57922  +        zErr = sqlite3MPrintf(db, "cannot open %s column for writing", zFault);
        57923  +        rc = SQLITE_ERROR;
        57924  +        (void)sqlite3SafetyOff(db);
        57925  +        sqlite3BtreeLeaveAll(db);
        57926  +        goto blob_open_out;
        57927  +      }
 57695  57928       }
 57696  57929   
 57697  57930       v = sqlite3VdbeCreate(db);
 57698  57931       if( v ){
 57699  57932         int iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
 57700  57933         sqlite3VdbeAddOpList(v, sizeof(openBlob)/sizeof(VdbeOpList), openBlob);
 57701  57934         flags = !!flags;                 /* flags = (flags ? 1 : 0); */
................................................................................
 59862  60095   ** pExpr is a comparison operator.  Return the type affinity that should
 59863  60096   ** be applied to both operands prior to doing the comparison.
 59864  60097   */
 59865  60098   static char comparisonAffinity(Expr *pExpr){
 59866  60099     char aff;
 59867  60100     assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT ||
 59868  60101             pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE ||
 59869         -          pExpr->op==TK_NE );
        60102  +          pExpr->op==TK_NE || pExpr->op==TK_IS || pExpr->op==TK_ISNOT );
 59870  60103     assert( pExpr->pLeft );
 59871  60104     aff = sqlite3ExprAffinity(pExpr->pLeft);
 59872  60105     if( pExpr->pRight ){
 59873  60106       aff = sqlite3CompareAffinity(pExpr->pRight, aff);
 59874  60107     }else if( ExprHasProperty(pExpr, EP_xIsSelect) ){
 59875  60108       aff = sqlite3CompareAffinity(pExpr->x.pSelect->pEList->a[0].pExpr, aff);
 59876  60109     }else if( !aff ){
................................................................................
 61953  62186         codeCompareOperands(pParse, pExpr->pLeft, &r1, &regFree1,
 61954  62187                                     pExpr->pRight, &r2, &regFree2);
 61955  62188         codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
 61956  62189                     r1, r2, inReg, SQLITE_STOREP2);
 61957  62190         testcase( regFree1==0 );
 61958  62191         testcase( regFree2==0 );
 61959  62192         break;
        62193  +    }
        62194  +    case TK_IS:
        62195  +    case TK_ISNOT: {
        62196  +      testcase( op==TK_IS );
        62197  +      testcase( op==TK_ISNOT );
        62198  +      codeCompareOperands(pParse, pExpr->pLeft, &r1, &regFree1,
        62199  +                                  pExpr->pRight, &r2, &regFree2);
        62200  +      op = (op==TK_IS) ? TK_EQ : TK_NE;
        62201  +      codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
        62202  +                  r1, r2, inReg, SQLITE_STOREP2 | SQLITE_NULLEQ);
        62203  +      testcase( regFree1==0 );
        62204  +      testcase( regFree2==0 );
        62205  +      break;
 61960  62206       }
 61961  62207       case TK_AND:
 61962  62208       case TK_OR:
 61963  62209       case TK_PLUS:
 61964  62210       case TK_STAR:
 61965  62211       case TK_MINUS:
 61966  62212       case TK_REM:
................................................................................
 62727  62973         codeCompareOperands(pParse, pExpr->pLeft, &r1, &regFree1,
 62728  62974                                     pExpr->pRight, &r2, &regFree2);
 62729  62975         codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
 62730  62976                     r1, r2, dest, jumpIfNull);
 62731  62977         testcase( regFree1==0 );
 62732  62978         testcase( regFree2==0 );
 62733  62979         break;
        62980  +    }
        62981  +    case TK_IS:
        62982  +    case TK_ISNOT: {
        62983  +      testcase( op==TK_IS );
        62984  +      testcase( op==TK_ISNOT );
        62985  +      codeCompareOperands(pParse, pExpr->pLeft, &r1, &regFree1,
        62986  +                                  pExpr->pRight, &r2, &regFree2);
        62987  +      op = (op==TK_IS) ? TK_EQ : TK_NE;
        62988  +      codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
        62989  +                  r1, r2, dest, SQLITE_NULLEQ);
        62990  +      testcase( regFree1==0 );
        62991  +      testcase( regFree2==0 );
        62992  +      break;
 62734  62993       }
 62735  62994       case TK_ISNULL:
 62736  62995       case TK_NOTNULL: {
 62737  62996         assert( TK_ISNULL==OP_IsNull );
 62738  62997         assert( TK_NOTNULL==OP_NotNull );
 62739  62998         testcase( op==TK_ISNULL );
 62740  62999         testcase( op==TK_NOTNULL );
................................................................................
 62876  63135         codeCompareOperands(pParse, pExpr->pLeft, &r1, &regFree1,
 62877  63136                                     pExpr->pRight, &r2, &regFree2);
 62878  63137         codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
 62879  63138                     r1, r2, dest, jumpIfNull);
 62880  63139         testcase( regFree1==0 );
 62881  63140         testcase( regFree2==0 );
 62882  63141         break;
        63142  +    }
        63143  +    case TK_IS:
        63144  +    case TK_ISNOT: {
        63145  +      testcase( pExpr->op==TK_IS );
        63146  +      testcase( pExpr->op==TK_ISNOT );
        63147  +      codeCompareOperands(pParse, pExpr->pLeft, &r1, &regFree1,
        63148  +                                  pExpr->pRight, &r2, &regFree2);
        63149  +      op = (pExpr->op==TK_IS) ? TK_NE : TK_EQ;
        63150  +      codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
        63151  +                  r1, r2, dest, SQLITE_NULLEQ);
        63152  +      testcase( regFree1==0 );
        63153  +      testcase( regFree2==0 );
        63154  +      break;
 62883  63155       }
 62884  63156       case TK_ISNULL:
 62885  63157       case TK_NOTNULL: {
 62886  63158         testcase( op==TK_ISNULL );
 62887  63159         testcase( op==TK_NOTNULL );
 62888  63160         r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
 62889  63161         sqlite3VdbeAddOp2(v, op, r1, dest);
................................................................................
 63338  63610   
 63339  63611       zRet = sqlite3MPrintf(db, "%.*s\"%w\"%s", ((u8*)tname.z) - zSql, zSql, 
 63340  63612          zTableName, tname.z+tname.n);
 63341  63613       sqlite3_result_text(context, zRet, -1, SQLITE_DYNAMIC);
 63342  63614     }
 63343  63615   }
 63344  63616   
        63617  +/*
        63618  +** This C function implements an SQL user function that is used by SQL code
        63619  +** generated by the ALTER TABLE ... RENAME command to modify the definition
        63620  +** of any foreign key constraints that use the table being renamed as the 
        63621  +** parent table. It is passed three arguments:
        63622  +**
        63623  +**   1) The complete text of the CREATE TABLE statement being modified,
        63624  +**   2) The old name of the table being renamed, and
        63625  +**   3) The new name of the table being renamed.
        63626  +**
        63627  +** It returns the new CREATE TABLE statement. For example:
        63628  +**
        63629  +**   sqlite_rename_parent('CREATE TABLE t1(a REFERENCES t2)', 't2', 't3')
        63630  +**       -> 'CREATE TABLE t1(a REFERENCES t3)'
        63631  +*/
        63632  +#ifndef SQLITE_OMIT_FOREIGN_KEY
        63633  +static void renameParentFunc(
        63634  +  sqlite3_context *context,
        63635  +  int NotUsed,
        63636  +  sqlite3_value **argv
        63637  +){
        63638  +  sqlite3 *db = sqlite3_context_db_handle(context);
        63639  +  char *zOutput = 0;
        63640  +  char *zResult;
        63641  +  unsigned char const *zInput = sqlite3_value_text(argv[0]);
        63642  +  unsigned char const *zOld = sqlite3_value_text(argv[1]);
        63643  +  unsigned char const *zNew = sqlite3_value_text(argv[2]);
        63644  +
        63645  +  unsigned const char *z;         /* Pointer to token */
        63646  +  int n;                          /* Length of token z */
        63647  +  int token;                      /* Type of token */
        63648  +
        63649  +  UNUSED_PARAMETER(NotUsed);
        63650  +  for(z=zInput; *z; z=z+n){
        63651  +    n = sqlite3GetToken(z, &token);
        63652  +    if( token==TK_REFERENCES ){
        63653  +      char *zParent;
        63654  +      do {
        63655  +        z += n;
        63656  +        n = sqlite3GetToken(z, &token);
        63657  +      }while( token==TK_SPACE );
        63658  +
        63659  +      zParent = sqlite3DbStrNDup(db, (const char *)z, n);
        63660  +      if( zParent==0 ) break;
        63661  +      sqlite3Dequote(zParent);
        63662  +      if( 0==sqlite3StrICmp((const char *)zOld, zParent) ){
        63663  +        char *zOut = sqlite3MPrintf(db, "%s%.*s\"%w\"", 
        63664  +            (zOutput?zOutput:""), z-zInput, zInput, (const char *)zNew
        63665  +        );
        63666  +        sqlite3DbFree(db, zOutput);
        63667  +        zOutput = zOut;
        63668  +        zInput = &z[n];
        63669  +      }
        63670  +      sqlite3DbFree(db, zParent);
        63671  +    }
        63672  +  }
        63673  +
        63674  +  zResult = sqlite3MPrintf(db, "%s%s", (zOutput?zOutput:""), zInput), 
        63675  +  sqlite3_result_text(context, zResult, -1, SQLITE_DYNAMIC);
        63676  +  sqlite3DbFree(db, zOutput);
        63677  +}
        63678  +#endif
        63679  +
 63345  63680   #ifndef SQLITE_OMIT_TRIGGER
 63346  63681   /* This function is used by SQL generated to implement the
 63347  63682   ** ALTER TABLE command. The first argument is the text of a CREATE TRIGGER 
 63348  63683   ** statement. The second is a table name. The table name in the CREATE 
 63349  63684   ** TRIGGER statement is replaced with the third argument and the result 
 63350  63685   ** returned. This is analagous to renameTableFunc() above, except for CREATE
 63351  63686   ** TRIGGER, not CREATE INDEX and CREATE TABLE.
................................................................................
 63425  63760   SQLITE_PRIVATE void sqlite3AlterFunctions(sqlite3 *db){
 63426  63761     sqlite3CreateFunc(db, "sqlite_rename_table", 2, SQLITE_UTF8, 0,
 63427  63762                            renameTableFunc, 0, 0);
 63428  63763   #ifndef SQLITE_OMIT_TRIGGER
 63429  63764     sqlite3CreateFunc(db, "sqlite_rename_trigger", 2, SQLITE_UTF8, 0,
 63430  63765                            renameTriggerFunc, 0, 0);
 63431  63766   #endif
        63767  +#ifndef SQLITE_OMIT_FOREIGN_KEY
        63768  +  sqlite3CreateFunc(db, "sqlite_rename_parent", 3, SQLITE_UTF8, 0,
        63769  +                         renameParentFunc, 0, 0);
        63770  +#endif
 63432  63771   }
 63433  63772   
        63773  +/*
        63774  +** This function is used to create the text of expressions of the form:
        63775  +**
        63776  +**   name=<constant1> OR name=<constant2> OR ...
        63777  +**
        63778  +** If argument zWhere is NULL, then a pointer string containing the text 
        63779  +** "name=<constant>" is returned, where <constant> is the quoted version
        63780  +** of the string passed as argument zConstant. The returned buffer is
        63781  +** allocated using sqlite3DbMalloc(). It is the responsibility of the
        63782  +** caller to ensure that it is eventually freed.
        63783  +**
        63784  +** If argument zWhere is not NULL, then the string returned is 
        63785  +** "<where> OR name=<constant>", where <where> is the contents of zWhere.
        63786  +** In this case zWhere is passed to sqlite3DbFree() before returning.
        63787  +** 
        63788  +*/
        63789  +static char *whereOrName(sqlite3 *db, char *zWhere, char *zConstant){
        63790  +  char *zNew;
        63791  +  if( !zWhere ){
        63792  +    zNew = sqlite3MPrintf(db, "name=%Q", zConstant);
        63793  +  }else{
        63794  +    zNew = sqlite3MPrintf(db, "%s OR name=%Q", zWhere, zConstant);
        63795  +    sqlite3DbFree(db, zWhere);
        63796  +  }
        63797  +  return zNew;
        63798  +}
        63799  +
        63800  +#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
        63801  +/*
        63802  +** Generate the text of a WHERE expression which can be used to select all
        63803  +** tables that have foreign key constraints that refer to table pTab (i.e.
        63804  +** constraints for which pTab is the parent table) from the sqlite_master
        63805  +** table.
        63806  +*/
        63807  +static char *whereForeignKeys(Parse *pParse, Table *pTab){
        63808  +  FKey *p;
        63809  +  char *zWhere = 0;
        63810  +  for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
        63811  +    zWhere = whereOrName(pParse->db, zWhere, p->pFrom->zName);
        63812  +  }
        63813  +  return zWhere;
        63814  +}
        63815  +#endif
        63816  +
 63434  63817   /*
 63435  63818   ** Generate the text of a WHERE expression which can be used to select all
 63436  63819   ** temporary triggers on table pTab from the sqlite_temp_master table. If
 63437  63820   ** table pTab has no temporary triggers, or is itself stored in the 
 63438  63821   ** temporary database, NULL is returned.
 63439  63822   */
 63440  63823   static char *whereTempTriggers(Parse *pParse, Table *pTab){
 63441  63824     Trigger *pTrig;
 63442  63825     char *zWhere = 0;
 63443         -  char *tmp = 0;
 63444  63826     const Schema *pTempSchema = pParse->db->aDb[1].pSchema; /* Temp db schema */
 63445  63827   
 63446  63828     /* If the table is not located in the temp-db (in which case NULL is 
 63447  63829     ** returned, loop through the tables list of triggers. For each trigger
 63448  63830     ** that is not part of the temp-db schema, add a clause to the WHERE 
 63449  63831     ** expression being built up in zWhere.
 63450  63832     */
 63451  63833     if( pTab->pSchema!=pTempSchema ){
 63452  63834       sqlite3 *db = pParse->db;
 63453  63835       for(pTrig=sqlite3TriggerList(pParse, pTab); pTrig; pTrig=pTrig->pNext){
 63454  63836         if( pTrig->pSchema==pTempSchema ){
 63455         -        if( !zWhere ){
 63456         -          zWhere = sqlite3MPrintf(db, "name=%Q", pTrig->zName);
 63457         -        }else{
 63458         -          tmp = zWhere;
 63459         -          zWhere = sqlite3MPrintf(db, "%s OR name=%Q", zWhere, pTrig->zName);
 63460         -          sqlite3DbFree(db, tmp);
 63461         -        }
        63837  +        zWhere = whereOrName(db, zWhere, pTrig->zName);
 63462  63838         }
 63463  63839       }
 63464  63840     }
 63465  63841     return zWhere;
 63466  63842   }
 63467  63843   
 63468  63844   /*
................................................................................
 63492  63868     for(pTrig=sqlite3TriggerList(pParse, pTab); pTrig; pTrig=pTrig->pNext){
 63493  63869       int iTrigDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema);
 63494  63870       assert( iTrigDb==iDb || iTrigDb==1 );
 63495  63871       sqlite3VdbeAddOp4(v, OP_DropTrigger, iTrigDb, 0, 0, pTrig->zName, 0);
 63496  63872     }
 63497  63873   #endif
 63498  63874   
 63499         -  /* Drop the table and index from the internal schema */
        63875  +  /* Drop the table and index from the internal schema.  */
 63500  63876     sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0);
 63501  63877   
 63502  63878     /* Reload the table, index and permanent trigger schemas. */
 63503  63879     zWhere = sqlite3MPrintf(pParse->db, "tbl_name=%Q", zName);
 63504  63880     if( !zWhere ) return;
 63505  63881     sqlite3VdbeAddOp4(v, OP_ParseSchema, iDb, 0, 0, zWhere, P4_DYNAMIC);
 63506  63882   
................................................................................
 63622  63998       sqlite3MayAbort(pParse);
 63623  63999     }
 63624  64000   #endif
 63625  64001   
 63626  64002     /* figure out how many UTF-8 characters are in zName */
 63627  64003     zTabName = pTab->zName;
 63628  64004     nTabName = sqlite3Utf8CharLen(zTabName, -1);
        64005  +
        64006  +#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
        64007  +  if( db->flags&SQLITE_ForeignKeys ){
        64008  +    /* If foreign-key support is enabled, rewrite the CREATE TABLE 
        64009  +    ** statements corresponding to all child tables of foreign key constraints
        64010  +    ** for which the renamed table is the parent table.  */
        64011  +    if( (zWhere=whereForeignKeys(pParse, pTab))!=0 ){
        64012  +      sqlite3NestedParse(pParse, 
        64013  +          "UPDATE sqlite_master SET "
        64014  +              "sql = sqlite_rename_parent(sql, %Q, %Q) "
        64015  +              "WHERE %s;", zTabName, zName, zWhere);
        64016  +      sqlite3DbFree(db, zWhere);
        64017  +    }
        64018  +  }
        64019  +#endif
 63629  64020   
 63630  64021     /* Modify the sqlite_master table to use the new table name. */
 63631  64022     sqlite3NestedParse(pParse,
 63632  64023         "UPDATE %Q.%s SET "
 63633  64024   #ifdef SQLITE_OMIT_TRIGGER
 63634  64025             "sql = sqlite_rename_table(sql, %Q), "
 63635  64026   #else
................................................................................
 63671  64062     if( (zWhere=whereTempTriggers(pParse, pTab))!=0 ){
 63672  64063       sqlite3NestedParse(pParse, 
 63673  64064           "UPDATE sqlite_temp_master SET "
 63674  64065               "sql = sqlite_rename_trigger(sql, %Q), "
 63675  64066               "tbl_name = %Q "
 63676  64067               "WHERE %s;", zName, zName, zWhere);
 63677  64068       sqlite3DbFree(db, zWhere);
        64069  +  }
        64070  +#endif
        64071  +
        64072  +#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
        64073  +  if( db->flags&SQLITE_ForeignKeys ){
        64074  +    FKey *p;
        64075  +    for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
        64076  +      Table *pFrom = p->pFrom;
        64077  +      if( pFrom!=pTab ){
        64078  +        reloadTableSchema(pParse, p->pFrom, pFrom->zName);
        64079  +      }
        64080  +    }
 63678  64081     }
 63679  64082   #endif
 63680  64083   
 63681  64084     /* Drop and reload the internal table schema. */
 63682  64085     reloadTableSchema(pParse, pTab, zName);
 63683  64086   
 63684  64087   exit_rename_table:
................................................................................
 63767  64170     if( pCol->isPrimKey ){
 63768  64171       sqlite3ErrorMsg(pParse, "Cannot add a PRIMARY KEY column");
 63769  64172       return;
 63770  64173     }
 63771  64174     if( pNew->pIndex ){
 63772  64175       sqlite3ErrorMsg(pParse, "Cannot add a UNIQUE column");
 63773  64176       return;
        64177  +  }
        64178  +  if( (db->flags&SQLITE_ForeignKeys) && pNew->pFKey && pDflt ){
        64179  +    sqlite3ErrorMsg(pParse, 
        64180  +        "Cannot add a REFERENCES column with non-NULL default value");
        64181  +    return;
 63774  64182     }
 63775  64183     if( pCol->notNull && !pDflt ){
 63776  64184       sqlite3ErrorMsg(pParse, 
 63777  64185           "Cannot add a NOT NULL column with default value NULL");
 63778  64186       return;
 63779  64187     }
 63780  64188   
................................................................................
 65198  65606   ** Write an error message into pParse->zErrMsg that explains that the
 65199  65607   ** user-supplied authorization function returned an illegal value.
 65200  65608   */
 65201  65609   static void sqliteAuthBadReturnCode(Parse *pParse){
 65202  65610     sqlite3ErrorMsg(pParse, "authorizer malfunction");
 65203  65611     pParse->rc = SQLITE_ERROR;
 65204  65612   }
        65613  +
        65614  +/*
        65615  +** Invoke the authorization callback for permission to read column zCol from
        65616  +** table zTab in database zDb. This function assumes that an authorization
        65617  +** callback has been registered (i.e. that sqlite3.xAuth is not NULL).
        65618  +**
        65619  +** If SQLITE_IGNORE is returned and pExpr is not NULL, then pExpr is changed
        65620  +** to an SQL NULL expression. Otherwise, if pExpr is NULL, then SQLITE_IGNORE
        65621  +** is treated as SQLITE_DENY. In this case an error is left in pParse.
        65622  +*/
        65623  +SQLITE_PRIVATE int sqlite3AuthReadCol(
        65624  +  Parse *pParse,                  /* The parser context */
        65625  +  const char *zTab,               /* Table name */
        65626  +  const char *zCol,               /* Column name */
        65627  +  int iDb                         /* Index of containing database. */
        65628  +){
        65629  +  sqlite3 *db = pParse->db;       /* Database handle */
        65630  +  char *zDb = db->aDb[iDb].zName; /* Name of attached database */
        65631  +  int rc;                         /* Auth callback return code */
        65632  +
        65633  +  rc = db->xAuth(db->pAuthArg, SQLITE_READ, zTab,zCol,zDb,pParse->zAuthContext);
        65634  +  if( rc==SQLITE_DENY ){
        65635  +    if( db->nDb>2 || iDb!=0 ){
        65636  +      sqlite3ErrorMsg(pParse, "access to %s.%s.%s is prohibited",zDb,zTab,zCol);
        65637  +    }else{
        65638  +      sqlite3ErrorMsg(pParse, "access to %s.%s is prohibited", zTab, zCol);
        65639  +    }
        65640  +    pParse->rc = SQLITE_AUTH;
        65641  +  }else if( rc!=SQLITE_IGNORE && rc!=SQLITE_OK ){
        65642  +    sqliteAuthBadReturnCode(pParse);
        65643  +  }
        65644  +  return rc;
        65645  +}
 65205  65646   
 65206  65647   /*
 65207  65648   ** The pExpr should be a TK_COLUMN expression.  The table referred to
 65208  65649   ** is in pTabList or else it is the NEW or OLD table of a trigger.  
 65209  65650   ** Check to see if it is OK to read this particular column.
 65210  65651   **
 65211  65652   ** If the auth function returns SQLITE_IGNORE, change the TK_COLUMN 
................................................................................
 65215  65656   SQLITE_PRIVATE void sqlite3AuthRead(
 65216  65657     Parse *pParse,        /* The parser context */
 65217  65658     Expr *pExpr,          /* The expression to check authorization on */
 65218  65659     Schema *pSchema,      /* The schema of the expression */
 65219  65660     SrcList *pTabList     /* All table that pExpr might refer to */
 65220  65661   ){
 65221  65662     sqlite3 *db = pParse->db;
 65222         -  int rc;
 65223  65663     Table *pTab = 0;      /* The table being read */
 65224  65664     const char *zCol;     /* Name of the column of the table */
 65225  65665     int iSrc;             /* Index in pTabList->a[] of table being read */
 65226         -  const char *zDBase;   /* Name of database being accessed */
 65227  65666     int iDb;              /* The index of the database the expression refers to */
 65228  65667     int iCol;             /* Index of column in table */
 65229  65668   
 65230  65669     if( db->xAuth==0 ) return;
 65231  65670     iDb = sqlite3SchemaToIndex(pParse->db, pSchema);
 65232  65671     if( iDb<0 ){
 65233  65672       /* An attempt to read a column out of a subquery or other
................................................................................
 65256  65695     }else if( pTab->iPKey>=0 ){
 65257  65696       assert( pTab->iPKey<pTab->nCol );
 65258  65697       zCol = pTab->aCol[pTab->iPKey].zName;
 65259  65698     }else{
 65260  65699       zCol = "ROWID";
 65261  65700     }
 65262  65701     assert( iDb>=0 && iDb<db->nDb );
 65263         -  zDBase = db->aDb[iDb].zName;
 65264         -  rc = db->xAuth(db->pAuthArg, SQLITE_READ, pTab->zName, zCol, zDBase, 
 65265         -                 pParse->zAuthContext);
 65266         -  if( rc==SQLITE_IGNORE ){
        65702  +  if( SQLITE_IGNORE==sqlite3AuthReadCol(pParse, pTab->zName, zCol, iDb) ){
 65267  65703       pExpr->op = TK_NULL;
 65268         -  }else if( rc==SQLITE_DENY ){
 65269         -    if( db->nDb>2 || iDb!=0 ){
 65270         -      sqlite3ErrorMsg(pParse, "access to %s.%s.%s is prohibited", 
 65271         -         zDBase, pTab->zName, zCol);
 65272         -    }else{
 65273         -      sqlite3ErrorMsg(pParse, "access to %s.%s is prohibited",pTab->zName,zCol);
 65274         -    }
 65275         -    pParse->rc = SQLITE_AUTH;
 65276         -  }else if( rc!=SQLITE_OK ){
 65277         -    sqliteAuthBadReturnCode(pParse);
 65278  65704     }
 65279  65705   }
 65280  65706   
 65281  65707   /*
 65282  65708   ** Do an authorization check using the code and arguments given.  Return
 65283  65709   ** either SQLITE_OK (zero) or SQLITE_IGNORE or SQLITE_DENY.  If SQLITE_DENY
 65284  65710   ** is returned, then the error count and error message in pParse are
................................................................................
 65539  65965     */
 65540  65966     if( v && ALWAYS(pParse->nErr==0) && !db->mallocFailed ){
 65541  65967   #ifdef SQLITE_DEBUG
 65542  65968       FILE *trace = (db->flags & SQLITE_VdbeTrace)!=0 ? stdout : 0;
 65543  65969       sqlite3VdbeTrace(v, trace);
 65544  65970   #endif
 65545  65971       assert( pParse->iCacheLevel==0 );  /* Disables and re-enables match */
        65972  +    /* A minimum of one cursor is required if autoincrement is used
        65973  +    *  See ticket [a696379c1f08866] */
        65974  +    if( pParse->pAinc!=0 && pParse->nTab==0 ) pParse->nTab = 1;
 65546  65975       sqlite3VdbeMakeReady(v, pParse->nVar, pParse->nMem,
 65547  65976                            pParse->nTab, pParse->nMaxArg, pParse->explain,
 65548  65977                            pParse->isMultiWrite && pParse->mayAbort);
 65549  65978       pParse->rc = SQLITE_DONE;
 65550  65979       pParse->colNamesSet = 0;
 65551  65980     }else if( pParse->rc==SQLITE_OK ){
 65552  65981       pParse->rc = SQLITE_ERROR;
................................................................................
 65844  66273   ** This routine just deletes the data structure.  It does not unlink
 65845  66274   ** the table data structure from the hash table.  But it does destroy
 65846  66275   ** memory structures of the indices and foreign keys associated with 
 65847  66276   ** the table.
 65848  66277   */
 65849  66278   SQLITE_PRIVATE void sqlite3DeleteTable(Table *pTable){
 65850  66279     Index *pIndex, *pNext;
 65851         -  FKey *pFKey, *pNextFKey;
 65852  66280     sqlite3 *db;
 65853  66281   
 65854  66282     if( pTable==0 ) return;
 65855  66283     db = pTable->dbMem;
 65856  66284     testcase( db==0 );
 65857  66285   
 65858  66286     /* Do not delete the table until the reference count reaches zero. */
................................................................................
 65866  66294     */
 65867  66295     for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){
 65868  66296       pNext = pIndex->pNext;
 65869  66297       assert( pIndex->pSchema==pTable->pSchema );
 65870  66298       sqlite3DeleteIndex(pIndex);
 65871  66299     }
 65872  66300   
 65873         -#ifndef SQLITE_OMIT_FOREIGN_KEY
 65874         -  /* Delete all foreign keys associated with this table. */
 65875         -  for(pFKey=pTable->pFKey; pFKey; pFKey=pNextFKey){
 65876         -    pNextFKey = pFKey->pNextFrom;
 65877         -    sqlite3DbFree(db, pFKey);
 65878         -  }
 65879         -#endif
        66301  +  /* Delete any foreign keys attached to this table. */
        66302  +  sqlite3FkDelete(pTable);
 65880  66303   
 65881  66304     /* Delete the Table structure itself.
 65882  66305     */
 65883  66306     sqliteResetColumnNames(pTable);
 65884  66307     sqlite3DbFree(db, pTable->zName);
 65885  66308     sqlite3DbFree(db, pTable->zColAff);
 65886  66309     sqlite3SelectDelete(db, pTable->pSelect);
................................................................................
 66514  66937       pTab->tabFlags |= autoInc*TF_Autoincrement;
 66515  66938     }else if( autoInc ){
 66516  66939   #ifndef SQLITE_OMIT_AUTOINCREMENT
 66517  66940       sqlite3ErrorMsg(pParse, "AUTOINCREMENT is only allowed on an "
 66518  66941          "INTEGER PRIMARY KEY");
 66519  66942   #endif
 66520  66943     }else{
 66521         -    sqlite3CreateIndex(pParse, 0, 0, 0, pList, onError, 0, 0, sortOrder, 0);
        66944  +    Index *p;
        66945  +    p = sqlite3CreateIndex(pParse, 0, 0, 0, pList, onError, 0, 0, sortOrder, 0);
        66946  +    if( p ){
        66947  +      p->autoIndex = 2;
        66948  +    }
 66522  66949       pList = 0;
 66523  66950     }
 66524  66951   
 66525  66952   primary_key_exit:
 66526  66953     sqlite3ExprListDelete(pParse->db, pList);
 66527  66954     return;
 66528  66955   }
................................................................................
 67403  67830       sqlite3BeginWriteOperation(pParse, 1, iDb);
 67404  67831   
 67405  67832   #ifndef SQLITE_OMIT_VIRTUALTABLE
 67406  67833       if( IsVirtual(pTab) ){
 67407  67834         sqlite3VdbeAddOp0(v, OP_VBegin);
 67408  67835       }
 67409  67836   #endif
        67837  +    sqlite3FkDropTable(pParse, pName, pTab);
 67410  67838   
 67411  67839       /* Drop all triggers associated with the table being dropped. Code
 67412  67840       ** is generated to remove entries from sqlite_master and/or
 67413  67841       ** sqlite_temp_master if required.
 67414  67842       */
 67415  67843       pTrigger = sqlite3TriggerList(pParse, pTab);
 67416  67844       while( pTrigger ){
................................................................................
 67493  67921     Token *pTo,          /* Name of the other table */
 67494  67922     ExprList *pToCol,    /* Columns in the other table */
 67495  67923     int flags            /* Conflict resolution algorithms. */
 67496  67924   ){
 67497  67925     sqlite3 *db = pParse->db;
 67498  67926   #ifndef SQLITE_OMIT_FOREIGN_KEY
 67499  67927     FKey *pFKey = 0;
        67928  +  FKey *pNextTo;
 67500  67929     Table *p = pParse->pNewTable;
 67501  67930     int nByte;
 67502  67931     int i;
 67503  67932     int nCol;
 67504  67933     char *z;
 67505  67934   
 67506  67935     assert( pTo!=0 );
................................................................................
 67567  67996         pFKey->aCol[i].zCol = z;
 67568  67997         memcpy(z, pToCol->a[i].zName, n);
 67569  67998         z[n] = 0;
 67570  67999         z += n+1;
 67571  68000       }
 67572  68001     }
 67573  68002     pFKey->isDeferred = 0;
 67574         -  pFKey->deleteConf = (u8)(flags & 0xff);
 67575         -  pFKey->updateConf = (u8)((flags >> 8 ) & 0xff);
 67576         -  pFKey->insertConf = (u8)((flags >> 16 ) & 0xff);
        68003  +  pFKey->aAction[0] = (u8)(flags & 0xff);            /* ON DELETE action */
        68004  +  pFKey->aAction[1] = (u8)((flags >> 8 ) & 0xff);    /* ON UPDATE action */
        68005  +
        68006  +  pNextTo = (FKey *)sqlite3HashInsert(&p->pSchema->fkeyHash, 
        68007  +      pFKey->zTo, sqlite3Strlen30(pFKey->zTo), (void *)pFKey
        68008  +  );
        68009  +  if( pNextTo==pFKey ){
        68010  +    db->mallocFailed = 1;
        68011  +    goto fk_end;
        68012  +  }
        68013  +  if( pNextTo ){
        68014  +    assert( pNextTo->pPrevTo==0 );
        68015  +    pFKey->pNextTo = pNextTo;
        68016  +    pNextTo->pPrevTo = pFKey;
        68017  +  }
 67577  68018   
 67578  68019     /* Link the foreign key to the table as the last step.
 67579  68020     */
 67580  68021     p->pFKey = pFKey;
 67581  68022     pFKey = 0;
 67582  68023   
 67583  68024   fk_end:
................................................................................
 67595  68036   ** accordingly.
 67596  68037   */
 67597  68038   SQLITE_PRIVATE void sqlite3DeferForeignKey(Parse *pParse, int isDeferred){
 67598  68039   #ifndef SQLITE_OMIT_FOREIGN_KEY
 67599  68040     Table *pTab;
 67600  68041     FKey *pFKey;
 67601  68042     if( (pTab = pParse->pNewTable)==0 || (pFKey = pTab->pFKey)==0 ) return;
 67602         -  assert( isDeferred==0 || isDeferred==1 );
        68043  +  assert( isDeferred==0 || isDeferred==1 ); /* EV: R-30323-21917 */
 67603  68044     pFKey->isDeferred = (u8)isDeferred;
 67604  68045   #endif
 67605  68046   }
 67606  68047   
 67607  68048   /*
 67608  68049   ** Generate code that will erase and refill index *pIdx.  This is
 67609  68050   ** used to initialize a newly created index or to recompute the
................................................................................
 67690  68131   ** UNIQUE constraint.  If pTable and pIndex are NULL, use pParse->pNewTable
 67691  68132   ** as the table to be indexed.  pParse->pNewTable is a table that is
 67692  68133   ** currently being constructed by a CREATE TABLE statement.
 67693  68134   **
 67694  68135   ** pList is a list of columns to be indexed.  pList will be NULL if this
 67695  68136   ** is a primary key or unique-constraint on the most recent column added
 67696  68137   ** to the table currently under construction.  
        68138  +**
        68139  +** If the index is created successfully, return a pointer to the new Index
        68140  +** structure. This is used by sqlite3AddPrimaryKey() to mark the index
        68141  +** as the tables primary key (Index.autoIndex==2).
 67697  68142   */
 67698         -SQLITE_PRIVATE void sqlite3CreateIndex(
        68143  +SQLITE_PRIVATE Index *sqlite3CreateIndex(
 67699  68144     Parse *pParse,     /* All information about this parse */
 67700  68145     Token *pName1,     /* First part of index name. May be NULL */
 67701  68146     Token *pName2,     /* Second part of index name. May be NULL */
 67702  68147     SrcList *pTblName, /* Table to index. Use pParse->pNewTable if 0 */
 67703  68148     ExprList *pList,   /* A list of columns to be indexed */
 67704  68149     int onError,       /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
 67705  68150     Token *pStart,     /* The CREATE token that begins this statement */
 67706  68151     Token *pEnd,       /* The ")" that closes the CREATE INDEX statement */
 67707  68152     int sortOrder,     /* Sort order of primary key when pList==NULL */
 67708  68153     int ifNotExist     /* Omit error if index already exists */
 67709  68154   ){
        68155  +  Index *pRet = 0;     /* Pointer to return */
 67710  68156     Table *pTab = 0;     /* Table to be indexed */
 67711  68157     Index *pIndex = 0;   /* The index to be created */
 67712  68158     char *zName = 0;     /* Name of the index */
 67713  68159     int nName;           /* Number of characters in zName */
 67714  68160     int i, j;
 67715  68161     Token nullId;        /* Fake token for an empty ID list */
 67716  68162     DbFixer sFix;        /* For assigning database names to pTable */
................................................................................
 68138  68584         Index *pOther = pTab->pIndex;
 68139  68585         while( pOther->pNext && pOther->pNext->onError!=OE_Replace ){
 68140  68586           pOther = pOther->pNext;
 68141  68587         }
 68142  68588         pIndex->pNext = pOther->pNext;
 68143  68589         pOther->pNext = pIndex;
 68144  68590       }
        68591  +    pRet = pIndex;
 68145  68592       pIndex = 0;
 68146  68593     }
 68147  68594   
 68148  68595     /* Clean up before exiting */
 68149  68596   exit_create_index:
 68150  68597     if( pIndex ){
 68151  68598       sqlite3_free(pIndex->zColAff);
 68152  68599       sqlite3DbFree(db, pIndex);
 68153  68600     }
 68154  68601     sqlite3ExprListDelete(db, pList);
 68155  68602     sqlite3SrcListDelete(db, pTblName);
 68156  68603     sqlite3DbFree(db, zName);
 68157         -  return;
        68604  +  return pRet;
 68158  68605   }
 68159  68606   
 68160  68607   /*
 68161  68608   ** Fill the Index.aiRowEst[] array with default information - information
 68162  68609   ** to be used when we have not run the ANALYZE command.
 68163  68610   **
 68164  68611   ** aiRowEst[0] is suppose to contain the number of elements in the index.
................................................................................
 68815  69262   */
 68816  69263   SQLITE_PRIVATE void sqlite3BeginWriteOperation(Parse *pParse, int setStatement, int iDb){
 68817  69264     Parse *pToplevel = sqlite3ParseToplevel(pParse);
 68818  69265     sqlite3CodeVerifySchema(pParse, iDb);
 68819  69266     pToplevel->writeMask |= 1<<iDb;
 68820  69267     pToplevel->isMultiWrite |= setStatement;
 68821  69268   }
        69269  +
        69270  +/*
        69271  +** Indicate that the statement currently under construction might write
        69272  +** more than one entry (example: deleting one row then inserting another,
        69273  +** inserting multiple rows in a table, or inserting a row and index entries.)
        69274  +** If an abort occurs after some of these writes have completed, then it will
        69275  +** be necessary to undo the completed writes.
        69276  +*/
        69277  +SQLITE_PRIVATE void sqlite3MultiWrite(Parse *pParse){
        69278  +  Parse *pToplevel = sqlite3ParseToplevel(pParse);
        69279  +  pToplevel->isMultiWrite = 1;
        69280  +}
 68822  69281   
 68823  69282   /* 
 68824         -** Set the "may throw abort exception" flag for the statement currently 
 68825         -** being coded.
        69283  +** The code generator calls this routine if is discovers that it is
        69284  +** possible to abort a statement prior to completion.  In order to 
        69285  +** perform this abort without corrupting the database, we need to make
        69286  +** sure that the statement is protected by a statement transaction.
        69287  +**
        69288  +** Technically, we only need to set the mayAbort flag if the
        69289  +** isMultiWrite flag was previously set.  There is a time dependency
        69290  +** such that the abort must occur after the multiwrite.  This makes
        69291  +** some statements involving the REPLACE conflict resolution algorithm
        69292  +** go a little faster.  But taking advantage of this time dependency
        69293  +** makes it more difficult to prove that the code is correct (in 
        69294  +** particular, it prevents us from writing an effective
        69295  +** implementation of sqlite3AssertMayAbort()) and so we have chosen
        69296  +** to take the safe route and skip the optimization.
 68826  69297   */
 68827  69298   SQLITE_PRIVATE void sqlite3MayAbort(Parse *pParse){
 68828  69299     Parse *pToplevel = sqlite3ParseToplevel(pParse);
 68829  69300     pToplevel->mayAbort = 1;
 68830  69301   }
 68831  69302   
 68832  69303   /*
................................................................................
 69429  69900     sqlite3HashInit(&pSchema->tblHash);
 69430  69901     for(pElem=sqliteHashFirst(&temp1); pElem; pElem=sqliteHashNext(pElem)){
 69431  69902       Table *pTab = sqliteHashData(pElem);
 69432  69903       assert( pTab->dbMem==0 );
 69433  69904       sqlite3DeleteTable(pTab);
 69434  69905     }
 69435  69906     sqlite3HashClear(&temp1);
        69907  +  sqlite3HashClear(&pSchema->fkeyHash);
 69436  69908     pSchema->pSeqTab = 0;
 69437  69909     pSchema->flags &= ~DB_SchemaLoaded;
 69438  69910   }
 69439  69911   
 69440  69912   /*
 69441  69913   ** Find and return the schema associated with a BTree.  Create
 69442  69914   ** a new one if necessary.
................................................................................
 69450  69922     }
 69451  69923     if( !p ){
 69452  69924       db->mallocFailed = 1;
 69453  69925     }else if ( 0==p->file_format ){
 69454  69926       sqlite3HashInit(&p->tblHash);
 69455  69927       sqlite3HashInit(&p->idxHash);
 69456  69928       sqlite3HashInit(&p->trigHash);
        69929  +    sqlite3HashInit(&p->fkeyHash);
 69457  69930       p->enc = SQLITE_UTF8;
 69458  69931     }
 69459  69932     return p;
 69460  69933   }
 69461  69934   
 69462  69935   /************** End of callback.c ********************************************/
 69463  69936   /************** Begin file delete.c ******************************************/
................................................................................
 69766  70239     /* Begin generating code.
 69767  70240     */
 69768  70241     v = sqlite3GetVdbe(pParse);
 69769  70242     if( v==0 ){
 69770  70243       goto delete_from_cleanup;
 69771  70244     }
 69772  70245     if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
 69773         -  sqlite3BeginWriteOperation(pParse, (pTrigger?1:0), iDb);
        70246  +  sqlite3BeginWriteOperation(pParse, 1, iDb);
 69774  70247   
 69775  70248     /* If we are trying to delete from a view, realize that view into
 69776  70249     ** a ephemeral table.
 69777  70250     */
 69778  70251   #if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
 69779  70252     if( isView ){
 69780  70253       sqlite3MaterializeView(pParse, pTab, pWhere, iCur);
................................................................................
 69799  70272     }
 69800  70273   
 69801  70274   #ifndef SQLITE_OMIT_TRUNCATE_OPTIMIZATION
 69802  70275     /* Special case: A DELETE without a WHERE clause deletes everything.
 69803  70276     ** It is easier just to erase the whole table. Prior to version 3.6.5,
 69804  70277     ** this optimization caused the row change count (the value returned by 
 69805  70278     ** API function sqlite3_count_changes) to be set incorrectly.  */
 69806         -  if( rcauth==SQLITE_OK && pWhere==0 && !pTrigger && !IsVirtual(pTab) ){
        70279  +  if( rcauth==SQLITE_OK && pWhere==0 && !pTrigger && !IsVirtual(pTab) 
        70280  +   && 0==sqlite3FkRequired(pParse, pTab, 0, 0)
        70281  +  ){
 69807  70282       assert( !isView );
 69808  70283       sqlite3VdbeAddOp4(v, OP_Clear, pTab->tnum, iDb, memCnt,
 69809  70284                         pTab->zName, P4_STATIC);
 69810  70285       for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
 69811  70286         assert( pIdx->pSchema==pTab->pSchema );
 69812  70287         sqlite3VdbeAddOp2(v, OP_Clear, pIdx->tnum, iDb);
 69813  70288       }
................................................................................
 69895  70370   
 69896  70371   delete_from_cleanup:
 69897  70372     sqlite3AuthContextPop(&sContext);
 69898  70373     sqlite3SrcListDelete(db, pTabList);
 69899  70374     sqlite3ExprDelete(db, pWhere);
 69900  70375     return;
 69901  70376   }
        70377  +/* Make sure "isView" and other macros defined above are undefined. Otherwise
        70378  +** thely may interfere with compilation of other functions in this file
        70379  +** (or in another file, if this file becomes part of the amalgamation).  */
        70380  +#ifdef isView
        70381  + #undef isView
        70382  +#endif
        70383  +#ifdef pTrigger
        70384  + #undef pTrigger
        70385  +#endif
 69902  70386   
 69903  70387   /*
 69904  70388   ** This routine generates VDBE code that causes a single row of a
 69905  70389   ** single table to be deleted.
 69906  70390   **
 69907  70391   ** The VDBE must be in a particular state when this routine is called.
 69908  70392   ** These are the requirements:
 69909  70393   **
 69910  70394   **   1.  A read/write cursor pointing to pTab, the table containing the row
 69911         -**       to be deleted, must be opened as cursor number "base".
        70395  +**       to be deleted, must be opened as cursor number $iCur.
 69912  70396   **
 69913  70397   **   2.  Read/write cursors for all indices of pTab must be open as
 69914  70398   **       cursor number base+i for the i-th index.
 69915  70399   **
 69916  70400   **   3.  The record number of the row to be deleted must be stored in
 69917  70401   **       memory cell iRowid.
 69918  70402   **
................................................................................
 69939  70423     ** (this can happen if a trigger program has already deleted it), do
 69940  70424     ** not attempt to delete it or fire any DELETE triggers.  */
 69941  70425     iLabel = sqlite3VdbeMakeLabel(v);
 69942  70426     sqlite3VdbeAddOp3(v, OP_NotExists, iCur, iLabel, iRowid);
 69943  70427    
 69944  70428     /* If there are any triggers to fire, allocate a range of registers to
 69945  70429     ** use for the old.* references in the triggers.  */
 69946         -  if( pTrigger ){
        70430  +  if( sqlite3FkRequired(pParse, pTab, 0, 0) || pTrigger ){
 69947  70431       u32 mask;                     /* Mask of OLD.* columns in use */
 69948  70432       int iCol;                     /* Iterator used while populating OLD.* */
 69949  70433   
 69950  70434       /* TODO: Could use temporary registers here. Also could attempt to
 69951  70435       ** avoid copying the contents of the rowid register.  */
 69952         -    mask = sqlite3TriggerOldmask(pParse, pTrigger, TK_DELETE, 0, pTab, onconf);
        70436  +    mask = sqlite3TriggerOldmask(pParse, pTrigger, 0, pTab, onconf);
        70437  +    mask |= sqlite3FkOldmask(pParse, pTab);
 69953  70438       iOld = pParse->nMem+1;
 69954  70439       pParse->nMem += (1 + pTab->nCol);
 69955  70440   
 69956  70441       /* Populate the OLD.* pseudo-table register array. These values will be 
 69957  70442       ** used by any BEFORE and AFTER triggers that exist.  */
 69958  70443       sqlite3VdbeAddOp2(v, OP_Copy, iRowid, iOld);
 69959  70444       for(iCol=0; iCol<pTab->nCol; iCol++){
................................................................................
 69960  70445         if( mask==0xffffffff || mask&(1<<iCol) ){
 69961  70446           int iTarget = iOld + iCol + 1;
 69962  70447           sqlite3VdbeAddOp3(v, OP_Column, iCur, iCol, iTarget);
 69963  70448           sqlite3ColumnDefault(v, pTab, iCol, iTarget);
 69964  70449         }
 69965  70450       }
 69966  70451   
 69967         -    /* Invoke any BEFORE trigger programs */
        70452  +    /* Invoke BEFORE DELETE trigger programs. */
 69968  70453       sqlite3CodeRowTrigger(pParse, pTrigger, 
 69969         -        TK_DELETE, 0, TRIGGER_BEFORE, pTab, -1, iOld, onconf, iLabel
        70454  +        TK_DELETE, 0, TRIGGER_BEFORE, pTab, iOld, onconf, iLabel
 69970  70455       );
 69971  70456   
 69972  70457       /* Seek the cursor to the row to be deleted again. It may be that
 69973  70458       ** the BEFORE triggers coded above have already removed the row
 69974  70459       ** being deleted. Do not attempt to delete the row a second time, and 
 69975  70460       ** do not fire AFTER triggers.  */
 69976  70461       sqlite3VdbeAddOp3(v, OP_NotExists, iCur, iLabel, iRowid);
        70462  +
        70463  +    /* Do FK processing. This call checks that any FK constraints that
        70464  +    ** refer to this table (i.e. constraints attached to other tables) 
        70465  +    ** are not violated by deleting this row.  */
        70466  +    sqlite3FkCheck(pParse, pTab, iOld, 0);
 69977  70467     }
 69978  70468   
 69979  70469     /* Delete the index and table entries. Skip this step if pTab is really
 69980  70470     ** a view (in which case the only effect of the DELETE statement is to
 69981  70471     ** fire the INSTEAD OF triggers).  */ 
 69982  70472     if( pTab->pSelect==0 ){
 69983  70473       sqlite3GenerateRowIndexDelete(pParse, pTab, iCur, 0);
 69984  70474       sqlite3VdbeAddOp2(v, OP_Delete, iCur, (count?OPFLAG_NCHANGE:0));
 69985  70475       if( count ){
 69986  70476         sqlite3VdbeChangeP4(v, -1, pTab->zName, P4_STATIC);
 69987  70477       }
 69988  70478     }
 69989  70479   
 69990         -  /* Invoke AFTER triggers. */
 69991         -  if( pTrigger ){
 69992         -    sqlite3CodeRowTrigger(pParse, pTrigger, 
 69993         -        TK_DELETE, 0, TRIGGER_AFTER, pTab, -1, iOld, onconf, iLabel
 69994         -    );
 69995         -  }
        70480  +  /* Do any ON CASCADE, SET NULL or SET DEFAULT operations required to
        70481  +  ** handle rows (possibly in other tables) that refer via a foreign key
        70482  +  ** to the row just deleted. */ 
        70483  +  sqlite3FkActions(pParse, pTab, 0, iOld);
        70484  +
        70485  +  /* Invoke AFTER DELETE trigger programs. */
        70486  +  sqlite3CodeRowTrigger(pParse, pTrigger, 
        70487  +      TK_DELETE, 0, TRIGGER_AFTER, pTab, iOld, onconf, iLabel
        70488  +  );
 69996  70489   
 69997  70490     /* Jump here if the row had already been deleted before any BEFORE
 69998  70491     ** trigger programs were invoked. Or if a trigger program throws a 
 69999  70492     ** RAISE(IGNORE) exception.  */
 70000  70493     sqlite3VdbeResolveLabel(v, iLabel);
 70001  70494   }
 70002  70495   
................................................................................
 70074  70567       sqlite3VdbeChangeP4(v, -1, sqlite3IndexAffinityStr(v, pIdx), 0);
 70075  70568       sqlite3ExprCacheAffinityChange(pParse, regBase, nCol+1);
 70076  70569     }
 70077  70570     sqlite3ReleaseTempRange(pParse, regBase, nCol+1);
 70078  70571     return regBase;
 70079  70572   }
 70080  70573   
 70081         -/* Make sure "isView" gets undefined in case this file becomes part of
 70082         -** the amalgamation - so that subsequent files do not see isView as a
 70083         -** macro. */
 70084         -#undef isView
 70085  70574   
 70086  70575   /************** End of delete.c **********************************************/
 70087  70576   /************** Begin file func.c ********************************************/
 70088  70577   /*
 70089  70578   ** 2002 February 23
 70090  70579   **
 70091  70580   ** The author disclaims copyright to this source code.  In place of
................................................................................
 71568  72057     for(i=0; i<ArraySize(aBuiltinFunc); i++){
 71569  72058       sqlite3FuncDefInsert(pHash, &aFunc[i]);
 71570  72059     }
 71571  72060     sqlite3RegisterDateTimeFunctions();
 71572  72061   }
 71573  72062   
 71574  72063   /************** End of func.c ************************************************/
        72064  +/************** Begin file fkey.c ********************************************/
        72065  +/*
        72066  +**
        72067  +** The author disclaims copyright to this source code.  In place of
        72068  +** a legal notice, here is a blessing:
        72069  +**
        72070  +**    May you do good and not evil.
        72071  +**    May you find forgiveness for yourself and forgive others.
        72072  +**    May you share freely, never taking more than you give.
        72073  +**
        72074  +*************************************************************************
        72075  +** This file contains code used by the compiler to add foreign key
        72076  +** support to compiled SQL statements.
        72077  +*/
        72078  +
        72079  +#ifndef SQLITE_OMIT_FOREIGN_KEY
        72080  +#ifndef SQLITE_OMIT_TRIGGER
        72081  +
        72082  +/*
        72083  +** Deferred and Immediate FKs
        72084  +** --------------------------
        72085  +**
        72086  +** Foreign keys in SQLite come in two flavours: deferred and immediate.
        72087  +** If an immediate foreign key constraint is violated, SQLITE_CONSTRAINT
        72088  +** is returned and the current statement transaction rolled back. If a 
        72089  +** deferred foreign key constraint is violated, no action is taken 
        72090  +** immediately. However if the application attempts to commit the 
        72091  +** transaction before fixing the constraint violation, the attempt fails.
        72092  +**
        72093  +** Deferred constraints are implemented using a simple counter associated
        72094  +** with the database handle. The counter is set to zero each time a 
        72095  +** database transaction is opened. Each time a statement is executed 
        72096  +** that causes a foreign key violation, the counter is incremented. Each
        72097  +** time a statement is executed that removes an existing violation from
        72098  +** the database, the counter is decremented. When the transaction is
        72099  +** committed, the commit fails if the current value of the counter is
        72100  +** greater than zero. This scheme has two big drawbacks:
        72101  +**
        72102  +**   * When a commit fails due to a deferred foreign key constraint, 
        72103  +**     there is no way to tell which foreign constraint is not satisfied,
        72104  +**     or which row it is not satisfied for.
        72105  +**
        72106  +**   * If the database contains foreign key violations when the 
        72107  +**     transaction is opened, this may cause the mechanism to malfunction.
        72108  +**
        72109  +** Despite these problems, this approach is adopted as it seems simpler
        72110  +** than the alternatives.
        72111  +**
        72112  +** INSERT operations:
        72113  +**
        72114  +**   I.1) For each FK for which the table is the child table, search
        72115  +**        the parent table for a match. If none is found increment the
        72116  +**        constraint counter.
        72117  +**
        72118  +**   I.2) For each FK for which the table is the parent table, 
        72119  +**        search the child table for rows that correspond to the new
        72120  +**        row in the parent table. Decrement the counter for each row
        72121  +**        found (as the constraint is now satisfied).
        72122  +**
        72123  +** DELETE operations:
        72124  +**
        72125  +**   D.1) For each FK for which the table is the child table, 
        72126  +**        search the parent table for a row that corresponds to the 
        72127  +**        deleted row in the child table. If such a row is not found, 
        72128  +**        decrement the counter.
        72129  +**
        72130  +**   D.2) For each FK for which the table is the parent table, search 
        72131  +**        the child table for rows that correspond to the deleted row 
        72132  +**        in the parent table. For each found increment the counter.
        72133  +**
        72134  +** UPDATE operations:
        72135  +**
        72136  +**   An UPDATE command requires that all 4 steps above are taken, but only
        72137  +**   for FK constraints for which the affected columns are actually 
        72138  +**   modified (values must be compared at runtime).
        72139  +**
        72140  +** Note that I.1 and D.1 are very similar operations, as are I.2 and D.2.
        72141  +** This simplifies the implementation a bit.
        72142  +**
        72143  +** For the purposes of immediate FK constraints, the OR REPLACE conflict
        72144  +** resolution is considered to delete rows before the new row is inserted.
        72145  +** If a delete caused by OR REPLACE violates an FK constraint, an exception
        72146  +** is thrown, even if the FK constraint would be satisfied after the new 
        72147  +** row is inserted.
        72148  +**
        72149  +** Immediate constraints are usually handled similarly. The only difference 
        72150  +** is that the counter used is stored as part of each individual statement
        72151  +** object (struct Vdbe). If, after the statement has run, its immediate
        72152  +** constraint counter is greater than zero, it returns SQLITE_CONSTRAINT
        72153  +** and the statement transaction is rolled back. An exception is an INSERT
        72154  +** statement that inserts a single row only (no triggers). In this case,
        72155  +** instead of using a counter, an exception is thrown immediately if the
        72156  +** INSERT violates a foreign key constraint. This is necessary as such
        72157  +** an INSERT does not open a statement transaction.
        72158  +**
        72159  +** TODO: How should dropping a table be handled? How should renaming a 
        72160  +** table be handled?
        72161  +**
        72162  +**
        72163  +** Query API Notes
        72164  +** ---------------
        72165  +**
        72166  +** Before coding an UPDATE or DELETE row operation, the code-generator
        72167  +** for those two operations needs to know whether or not the operation
        72168  +** requires any FK processing and, if so, which columns of the original
        72169  +** row are required by the FK processing VDBE code (i.e. if FKs were
        72170  +** implemented using triggers, which of the old.* columns would be 
        72171  +** accessed). No information is required by the code-generator before
        72172  +** coding an INSERT operation. The functions used by the UPDATE/DELETE
        72173  +** generation code to query for this information are:
        72174  +**
        72175  +**   sqlite3FkRequired() - Test to see if FK processing is required.
        72176  +**   sqlite3FkOldmask()  - Query for the set of required old.* columns.
        72177  +**
        72178  +**
        72179  +** Externally accessible module functions
        72180  +** --------------------------------------
        72181  +**
        72182  +**   sqlite3FkCheck()    - Check for foreign key violations.
        72183  +**   sqlite3FkActions()  - Code triggers for ON UPDATE/ON DELETE actions.
        72184  +**   sqlite3FkDelete()   - Delete an FKey structure.
        72185  +*/
        72186  +
        72187  +/*
        72188  +** VDBE Calling Convention
        72189  +** -----------------------
        72190  +**
        72191  +** Example:
        72192  +**
        72193  +**   For the following INSERT statement:
        72194  +**
        72195  +**     CREATE TABLE t1(a, b INTEGER PRIMARY KEY, c);
        72196  +**     INSERT INTO t1 VALUES(1, 2, 3.1);
        72197  +**
        72198  +**   Register (x):        2    (type integer)
        72199  +**   Register (x+1):      1    (type integer)
        72200  +**   Register (x+2):      NULL (type NULL)
        72201  +**   Register (x+3):      3.1  (type real)
        72202  +*/
        72203  +
        72204  +/*
        72205  +** A foreign key constraint requires that the key columns in the parent
        72206  +** table are collectively subject to a UNIQUE or PRIMARY KEY constraint.
        72207  +** Given that pParent is the parent table for foreign key constraint pFKey, 
        72208  +** search the schema a unique index on the parent key columns. 
        72209  +**
        72210  +** If successful, zero is returned. If the parent key is an INTEGER PRIMARY 
        72211  +** KEY column, then output variable *ppIdx is set to NULL. Otherwise, *ppIdx 
        72212  +** is set to point to the unique index. 
        72213  +** 
        72214  +** If the parent key consists of a single column (the foreign key constraint
        72215  +** is not a composite foreign key), output variable *paiCol is set to NULL.
        72216  +** Otherwise, it is set to point to an allocated array of size N, where
        72217  +** N is the number of columns in the parent key. The first element of the
        72218  +** array is the index of the child table column that is mapped by the FK
        72219  +** constraint to the parent table column stored in the left-most column
        72220  +** of index *ppIdx. The second element of the array is the index of the
        72221  +** child table column that corresponds to the second left-most column of
        72222  +** *ppIdx, and so on.
        72223  +**
        72224  +** If the required index cannot be found, either because:
        72225  +**
        72226  +**   1) The named parent key columns do not exist, or
        72227  +**
        72228  +**   2) The named parent key columns do exist, but are not subject to a
        72229  +**      UNIQUE or PRIMARY KEY constraint, or
        72230  +**
        72231  +**   3) No parent key columns were provided explicitly as part of the
        72232  +**      foreign key definition, and the parent table does not have a
        72233  +**      PRIMARY KEY, or
        72234  +**
        72235  +**   4) No parent key columns were provided explicitly as part of the
        72236  +**      foreign key definition, and the PRIMARY KEY of the parent table 
        72237  +**      consists of a a different number of columns to the child key in 
        72238  +**      the child table.
        72239  +**
        72240  +** then non-zero is returned, and a "foreign key mismatch" error loaded
        72241  +** into pParse. If an OOM error occurs, non-zero is returned and the
        72242  +** pParse->db->mallocFailed flag is set.
        72243  +*/
        72244  +static int locateFkeyIndex(
        72245  +  Parse *pParse,                  /* Parse context to store any error in */
        72246  +  Table *pParent,                 /* Parent table of FK constraint pFKey */
        72247  +  FKey *pFKey,                    /* Foreign key to find index for */
        72248  +  Index **ppIdx,                  /* OUT: Unique index on parent table */
        72249  +  int **paiCol                    /* OUT: Map of index columns in pFKey */
        72250  +){
        72251  +  Index *pIdx = 0;                    /* Value to return via *ppIdx */
        72252  +  int *aiCol = 0;                     /* Value to return via *paiCol */
        72253  +  int nCol = pFKey->nCol;             /* Number of columns in parent key */
        72254  +  char *zKey = pFKey->aCol[0].zCol;   /* Name of left-most parent key column */
        72255  +
        72256  +  /* The caller is responsible for zeroing output parameters. */
        72257  +  assert( ppIdx && *ppIdx==0 );
        72258  +  assert( !paiCol || *paiCol==0 );
        72259  +  assert( pParse );
        72260  +
        72261  +  /* If this is a non-composite (single column) foreign key, check if it 
        72262  +  ** maps to the INTEGER PRIMARY KEY of table pParent. If so, leave *ppIdx 
        72263  +  ** and *paiCol set to zero and return early. 
        72264  +  **
        72265  +  ** Otherwise, for a composite foreign key (more than one column), allocate
        72266  +  ** space for the aiCol array (returned via output parameter *paiCol).
        72267  +  ** Non-composite foreign keys do not require the aiCol array.
        72268  +  */
        72269  +  if( nCol==1 ){
        72270  +    /* The FK maps to the IPK if any of the following are true:
        72271  +    **
        72272  +    **   1) There is an INTEGER PRIMARY KEY column and the FK is implicitly 
        72273  +    **      mapped to the primary key of table pParent, or
        72274  +    **   2) The FK is explicitly mapped to a column declared as INTEGER
        72275  +    **      PRIMARY KEY.
        72276  +    */
        72277  +    if( pParent->iPKey>=0 ){
        72278  +      if( !zKey ) return 0;
        72279  +      if( !sqlite3StrICmp(pParent->aCol[pParent->iPKey].zName, zKey) ) return 0;
        72280  +    }
        72281  +  }else if( paiCol ){
        72282  +    assert( nCol>1 );
        72283  +    aiCol = (int *)sqlite3DbMallocRaw(pParse->db, nCol*sizeof(int));
        72284  +    if( !aiCol ) return 1;
        72285  +    *paiCol = aiCol;
        72286  +  }
        72287  +
        72288  +  for(pIdx=pParent->pIndex; pIdx; pIdx=pIdx->pNext){
        72289  +    if( pIdx->nColumn==nCol && pIdx->onError!=OE_None ){ 
        72290  +      /* pIdx is a UNIQUE index (or a PRIMARY KEY) and has the right number
        72291  +      ** of columns. If each indexed column corresponds to a foreign key
        72292  +      ** column of pFKey, then this index is a winner.  */
        72293  +
        72294  +      if( zKey==0 ){
        72295  +        /* If zKey is NULL, then this foreign key is implicitly mapped to 
        72296  +        ** the PRIMARY KEY of table pParent. The PRIMARY KEY index may be 
        72297  +        ** identified by the test (Index.autoIndex==2).  */
        72298  +        if( pIdx->autoIndex==2 ){
        72299  +          if( aiCol ){
        72300  +            int i;
        72301  +            for(i=0; i<nCol; i++) aiCol[i] = pFKey->aCol[i].iFrom;
        72302  +          }
        72303  +          break;
        72304  +        }
        72305  +      }else{
        72306  +        /* If zKey is non-NULL, then this foreign key was declared to
        72307  +        ** map to an explicit list of columns in table pParent. Check if this
        72308  +        ** index matches those columns. Also, check that the index uses
        72309  +        ** the default collation sequences for each column. */
        72310  +        int i, j;
        72311  +        for(i=0; i<nCol; i++){
        72312  +          int iCol = pIdx->aiColumn[i];     /* Index of column in parent tbl */
        72313  +          char *zDfltColl;                  /* Def. collation for column */
        72314  +          char *zIdxCol;                    /* Name of indexed column */
        72315  +
        72316  +          /* If the index uses a collation sequence that is different from
        72317  +          ** the default collation sequence for the column, this index is
        72318  +          ** unusable. Bail out early in this case.  */
        72319  +          zDfltColl = pParent->aCol[iCol].zColl;
        72320  +          if( !zDfltColl ){
        72321  +            zDfltColl = "BINARY";
        72322  +          }
        72323  +          if( sqlite3StrICmp(pIdx->azColl[i], zDfltColl) ) break;
        72324  +
        72325  +          zIdxCol = pParent->aCol[iCol].zName;
        72326  +          for(j=0; j<nCol; j++){
        72327  +            if( sqlite3StrICmp(pFKey->aCol[j].zCol, zIdxCol)==0 ){
        72328  +              if( aiCol ) aiCol[i] = pFKey->aCol[j].iFrom;
        72329  +              break;
        72330  +            }
        72331  +          }
        72332  +          if( j==nCol ) break;
        72333  +        }
        72334  +        if( i==nCol ) break;      /* pIdx is usable */
        72335  +      }
        72336  +    }
        72337  +  }
        72338  +
        72339  +  if( !pIdx ){
        72340  +    if( !pParse->disableTriggers ){
        72341  +      sqlite3ErrorMsg(pParse, "foreign key mismatch");
        72342  +    }
        72343  +    sqlite3DbFree(pParse->db, aiCol);
        72344  +    return 1;
        72345  +  }
        72346  +
        72347  +  *ppIdx = pIdx;
        72348  +  return 0;
        72349  +}
        72350  +
        72351  +/*
        72352  +** This function is called when a row is inserted into or deleted from the 
        72353  +** child table of foreign key constraint pFKey. If an SQL UPDATE is executed 
        72354  +** on the child table of pFKey, this function is invoked twice for each row
        72355  +** affected - once to "delete" the old row, and then again to "insert" the
        72356  +** new row.
        72357  +**
        72358  +** Each time it is called, this function generates VDBE code to locate the
        72359  +** row in the parent table that corresponds to the row being inserted into 
        72360  +** or deleted from the child table. If the parent row can be found, no 
        72361  +** special action is taken. Otherwise, if the parent row can *not* be
        72362  +** found in the parent table:
        72363  +**
        72364  +**   Operation | FK type   | Action taken
        72365  +**   --------------------------------------------------------------------------
        72366  +**   INSERT      immediate   Increment the "immediate constraint counter".
        72367  +**
        72368  +**   DELETE      immediate   Decrement the "immediate constraint counter".
        72369  +**
        72370  +**   INSERT      deferred    Increment the "deferred constraint counter".
        72371  +**
        72372  +**   DELETE      deferred    Decrement the "deferred constraint counter".
        72373  +**
        72374  +** These operations are identified in the comment at the top of this file 
        72375  +** (fkey.c) as "I.1" and "D.1".
        72376  +*/
        72377  +static void fkLookupParent(
        72378  +  Parse *pParse,        /* Parse context */
        72379  +  int iDb,              /* Index of database housing pTab */
        72380  +  Table *pTab,          /* Parent table of FK pFKey */
        72381  +  Index *pIdx,          /* Unique index on parent key columns in pTab */
        72382  +  FKey *pFKey,          /* Foreign key constraint */
        72383  +  int *aiCol,           /* Map from parent key columns to child table columns */
        72384  +  int regData,          /* Address of array containing child table row */
        72385  +  int nIncr,            /* Increment constraint counter by this */
        72386  +  int isIgnore          /* If true, pretend pTab contains all NULL values */
        72387  +){
        72388  +  int i;                                    /* Iterator variable */
        72389  +  Vdbe *v = sqlite3GetVdbe(pParse);         /* Vdbe to add code to */
        72390  +  int iCur = pParse->nTab - 1;              /* Cursor number to use */
        72391  +  int iOk = sqlite3VdbeMakeLabel(v);        /* jump here if parent key found */
        72392  +
        72393  +  /* If nIncr is less than zero, then check at runtime if there are any
        72394  +  ** outstanding constraints to resolve. If there are not, there is no need
        72395  +  ** to check if deleting this row resolves any outstanding violations.
        72396  +  **
        72397  +  ** Check if any of the key columns in the child table row are NULL. If 
        72398  +  ** any are, then the constraint is considered satisfied. No need to 
        72399  +  ** search for a matching row in the parent table.  */
        72400  +  if( nIncr<0 ){
        72401  +    sqlite3VdbeAddOp2(v, OP_FkIfZero, pFKey->isDeferred, iOk);
        72402  +  }
        72403  +  for(i=0; i<pFKey->nCol; i++){
        72404  +    int iReg = aiCol[i] + regData + 1;
        72405  +    sqlite3VdbeAddOp2(v, OP_IsNull, iReg, iOk);
        72406  +  }
        72407  +
        72408  +  if( isIgnore==0 ){
        72409  +    if( pIdx==0 ){
        72410  +      /* If pIdx is NULL, then the parent key is the INTEGER PRIMARY KEY
        72411  +      ** column of the parent table (table pTab).  */
        72412  +      int iMustBeInt;               /* Address of MustBeInt instruction */
        72413  +      int regTemp = sqlite3GetTempReg(pParse);
        72414  +  
        72415  +      /* Invoke MustBeInt to coerce the child key value to an integer (i.e. 
        72416  +      ** apply the affinity of the parent key). If this fails, then there
        72417  +      ** is no matching parent key. Before using MustBeInt, make a copy of
        72418  +      ** the value. Otherwise, the value inserted into the child key column
        72419  +      ** will have INTEGER affinity applied to it, which may not be correct.  */
        72420  +      sqlite3VdbeAddOp2(v, OP_SCopy, aiCol[0]+1+regData, regTemp);
        72421  +      iMustBeInt = sqlite3VdbeAddOp2(v, OP_MustBeInt, regTemp, 0);
        72422  +  
        72423  +      /* If the parent table is the same as the child table, and we are about
        72424  +      ** to increment the constraint-counter (i.e. this is an INSERT operation),
        72425  +      ** then check if the row being inserted matches itself. If so, do not
        72426  +      ** increment the constraint-counter.  */
        72427  +      if( pTab==pFKey->pFrom && nIncr==1 ){
        72428  +        sqlite3VdbeAddOp3(v, OP_Eq, regData, iOk, regTemp);
        72429  +      }
        72430  +  
        72431  +      sqlite3OpenTable(pParse, iCur, iDb, pTab, OP_OpenRead);
        72432  +      sqlite3VdbeAddOp3(v, OP_NotExists, iCur, 0, regTemp);
        72433  +      sqlite3VdbeAddOp2(v, OP_Goto, 0, iOk);
        72434  +      sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
        72435  +      sqlite3VdbeJumpHere(v, iMustBeInt);
        72436  +      sqlite3ReleaseTempReg(pParse, regTemp);
        72437  +    }else{
        72438  +      int nCol = pFKey->nCol;
        72439  +      int regTemp = sqlite3GetTempRange(pParse, nCol);
        72440  +      int regRec = sqlite3GetTempReg(pParse);
        72441  +      KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIdx);
        72442  +  
        72443  +      sqlite3VdbeAddOp3(v, OP_OpenRead, iCur, pIdx->tnum, iDb);
        72444  +      sqlite3VdbeChangeP4(v, -1, (char*)pKey, P4_KEYINFO_HANDOFF);
        72445  +      for(i=0; i<nCol; i++){
        72446  +        sqlite3VdbeAddOp2(v, OP_SCopy, aiCol[i]+1+regData, regTemp+i);
        72447  +      }
        72448  +  
        72449  +      /* If the parent table is the same as the child table, and we are about
        72450  +      ** to increment the constraint-counter (i.e. this is an INSERT operation),
        72451  +      ** then check if the row being inserted matches itself. If so, do not
        72452  +      ** increment the constraint-counter.  */
        72453  +      if( pTab==pFKey->pFrom && nIncr==1 ){
        72454  +        int iJump = sqlite3VdbeCurrentAddr(v) + nCol + 1;
        72455  +        for(i=0; i<nCol; i++){
        72456  +          int iChild = aiCol[i]+1+regData;
        72457  +          int iParent = pIdx->aiColumn[i]+1+regData;
        72458  +          sqlite3VdbeAddOp3(v, OP_Ne, iChild, iJump, iParent);
        72459  +        }
        72460  +        sqlite3VdbeAddOp2(v, OP_Goto, 0, iOk);
        72461  +      }
        72462  +  
        72463  +      sqlite3VdbeAddOp3(v, OP_MakeRecord, regTemp, nCol, regRec);
        72464  +      sqlite3VdbeChangeP4(v, -1, sqlite3IndexAffinityStr(v, pIdx), 0);
        72465  +      sqlite3VdbeAddOp3(v, OP_Found, iCur, iOk, regRec);
        72466  +  
        72467  +      sqlite3ReleaseTempReg(pParse, regRec);
        72468  +      sqlite3ReleaseTempRange(pParse, regTemp, nCol);
        72469  +    }
        72470  +  }
        72471  +
        72472  +  if( !pFKey->isDeferred && !pParse->pToplevel && !pParse->isMultiWrite ){
        72473  +    /* Special case: If this is an INSERT statement that will insert exactly
        72474  +    ** one row into the table, raise a constraint immediately instead of
        72475  +    ** incrementing a counter. This is necessary as the VM code is being
        72476  +    ** generated for will not open a statement transaction.  */
        72477  +    assert( nIncr==1 );
        72478  +    sqlite3HaltConstraint(
        72479  +        pParse, OE_Abort, "foreign key constraint failed", P4_STATIC
        72480  +    );
        72481  +  }else{
        72482  +    if( nIncr>0 && pFKey->isDeferred==0 ){
        72483  +      sqlite3ParseToplevel(pParse)->mayAbort = 1;
        72484  +    }
        72485  +    sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, nIncr);
        72486  +  }
        72487  +
        72488  +  sqlite3VdbeResolveLabel(v, iOk);
        72489  +  sqlite3VdbeAddOp1(v, OP_Close, iCur);
        72490  +}
        72491  +
        72492  +/*
        72493  +** This function is called to generate code executed when a row is deleted
        72494  +** from the parent table of foreign key constraint pFKey and, if pFKey is 
        72495  +** deferred, when a row is inserted into the same table. When generating
        72496  +** code for an SQL UPDATE operation, this function may be called twice -
        72497  +** once to "delete" the old row and once to "insert" the new row.
        72498  +**
        72499  +** The code generated by this function scans through the rows in the child
        72500  +** table that correspond to the parent table row being deleted or inserted.
        72501  +** For each child row found, one of the following actions is taken:
        72502  +**
        72503  +**   Operation | FK type   | Action taken
        72504  +**   --------------------------------------------------------------------------
        72505  +**   DELETE      immediate   Increment the "immediate constraint counter".
        72506  +**                           Or, if the ON (UPDATE|DELETE) action is RESTRICT,
        72507  +**                           throw a "foreign key constraint failed" exception.
        72508  +**
        72509  +**   INSERT      immediate   Decrement the "immediate constraint counter".
        72510  +**
        72511  +**   DELETE      deferred    Increment the "deferred constraint counter".
        72512  +**                           Or, if the ON (UPDATE|DELETE) action is RESTRICT,
        72513  +**                           throw a "foreign key constraint failed" exception.
        72514  +**
        72515  +**   INSERT      deferred    Decrement the "deferred constraint counter".
        72516  +**
        72517  +** These operations are identified in the comment at the top of this file 
        72518  +** (fkey.c) as "I.2" and "D.2".
        72519  +*/
        72520  +static void fkScanChildren(
        72521  +  Parse *pParse,                  /* Parse context */
        72522  +  SrcList *pSrc,                  /* SrcList containing the table to scan */
        72523  +  Table *pTab,
        72524  +  Index *pIdx,                    /* Foreign key index */
        72525  +  FKey *pFKey,                    /* Foreign key relationship */
        72526  +  int *aiCol,                     /* Map from pIdx cols to child table cols */
        72527  +  int regData,                    /* Referenced table data starts here */
        72528  +  int nIncr                       /* Amount to increment deferred counter by */
        72529  +){
        72530  +  sqlite3 *db = pParse->db;       /* Database handle */
        72531  +  int i;                          /* Iterator variable */
        72532  +  Expr *pWhere = 0;               /* WHERE clause to scan with */
        72533  +  NameContext sNameContext;       /* Context used to resolve WHERE clause */
        72534  +  WhereInfo *pWInfo;              /* Context used by sqlite3WhereXXX() */
        72535  +  int iFkIfZero = 0;              /* Address of OP_FkIfZero */
        72536  +  Vdbe *v = sqlite3GetVdbe(pParse);
        72537  +
        72538  +  assert( !pIdx || pIdx->pTable==pTab );
        72539  +
        72540  +  if( nIncr<0 ){
        72541  +    iFkIfZero = sqlite3VdbeAddOp2(v, OP_FkIfZero, pFKey->isDeferred, 0);
        72542  +  }
        72543  +
        72544  +  /* Create an Expr object representing an SQL expression like:
        72545  +  **
        72546  +  **   <parent-key1> = <child-key1> AND <parent-key2> = <child-key2> ...
        72547  +  **
        72548  +  ** The collation sequence used for the comparison should be that of
        72549  +  ** the parent key columns. The affinity of the parent key column should
        72550  +  ** be applied to each child key value before the comparison takes place.
        72551  +  */
        72552  +  for(i=0; i<pFKey->nCol; i++){
        72553  +    Expr *pLeft;                  /* Value from parent table row */
        72554  +    Expr *pRight;                 /* Column ref to child table */
        72555  +    Expr *pEq;                    /* Expression (pLeft = pRight) */
        72556  +    int iCol;                     /* Index of column in child table */ 
        72557  +    const char *zCol;             /* Name of column in child table */
        72558  +
        72559  +    pLeft = sqlite3Expr(db, TK_REGISTER, 0);
        72560  +    if( pLeft ){
        72561  +      /* Set the collation sequence and affinity of the LHS of each TK_EQ
        72562  +      ** expression to the parent key column defaults.  */
        72563  +      if( pIdx ){
        72564  +        Column *pCol;
        72565  +        iCol = pIdx->aiColumn[i];
        72566  +        pCol = &pIdx->pTable->aCol[iCol];
        72567  +        pLeft->iTable = regData+iCol+1;
        72568  +        pLeft->affinity = pCol->affinity;
        72569  +        pLeft->pColl = sqlite3LocateCollSeq(pParse, pCol->zColl);
        72570  +      }else{
        72571  +        pLeft->iTable = regData;
        72572  +        pLeft->affinity = SQLITE_AFF_INTEGER;
        72573  +      }
        72574  +    }
        72575  +    iCol = aiCol ? aiCol[i] : pFKey->aCol[0].iFrom;
        72576  +    assert( iCol>=0 );
        72577  +    zCol = pFKey->pFrom->aCol[iCol].zName;
        72578  +    pRight = sqlite3Expr(db, TK_ID, zCol);
        72579  +    pEq = sqlite3PExpr(pParse, TK_EQ, pLeft, pRight, 0);
        72580  +    pWhere = sqlite3ExprAnd(db, pWhere, pEq);
        72581  +  }
        72582  +
        72583  +  /* If the child table is the same as the parent table, and this scan
        72584  +  ** is taking place as part of a DELETE operation (operation D.2), omit the
        72585  +  ** row being deleted from the scan by adding ($rowid != rowid) to the WHERE 
        72586  +  ** clause, where $rowid is the rowid of the row being deleted.  */
        72587  +  if( pTab==pFKey->pFrom && nIncr>0 ){
        72588  +    Expr *pEq;                    /* Expression (pLeft = pRight) */
        72589  +    Expr *pLeft;                  /* Value from parent table row */
        72590  +    Expr *pRight;                 /* Column ref to child table */
        72591  +    pLeft = sqlite3Expr(db, TK_REGISTER, 0);
        72592  +    pRight = sqlite3Expr(db, TK_COLUMN, 0);
        72593  +    if( pLeft && pRight ){
        72594  +      pLeft->iTable = regData;
        72595  +      pLeft->affinity = SQLITE_AFF_INTEGER;
        72596  +      pRight->iTable = pSrc->a[0].iCursor;
        72597  +      pRight->iColumn = -1;
        72598  +    }
        72599  +    pEq = sqlite3PExpr(pParse, TK_NE, pLeft, pRight, 0);
        72600  +    pWhere = sqlite3ExprAnd(db, pWhere, pEq);
        72601  +  }
        72602  +
        72603  +  /* Resolve the references in the WHERE clause. */
        72604  +  memset(&sNameContext, 0, sizeof(NameContext));
        72605  +  sNameContext.pSrcList = pSrc;
        72606  +  sNameContext.pParse = pParse;
        72607  +  sqlite3ResolveExprNames(&sNameContext, pWhere);
        72608  +
        72609  +  /* Create VDBE to loop through the entries in pSrc that match the WHERE
        72610  +  ** clause. If the constraint is not deferred, throw an exception for
        72611  +  ** each row found. Otherwise, for deferred constraints, increment the
        72612  +  ** deferred constraint counter by nIncr for each row selected.  */
        72613  +  pWInfo = sqlite3WhereBegin(pParse, pSrc, pWhere, 0, 0);
        72614  +  if( nIncr>0 && pFKey->isDeferred==0 ){
        72615  +    sqlite3ParseToplevel(pParse)->mayAbort = 1;
        72616  +  }
        72617  +  sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, nIncr);
        72618  +  if( pWInfo ){
        72619  +    sqlite3WhereEnd(pWInfo);
        72620  +  }
        72621  +
        72622  +  /* Clean up the WHERE clause constructed above. */
        72623  +  sqlite3ExprDelete(db, pWhere);
        72624  +  if( iFkIfZero ){
        72625  +    sqlite3VdbeJumpHere(v, iFkIfZero);
        72626  +  }
        72627  +}
        72628  +
        72629  +/*
        72630  +** This function returns a pointer to the head of a linked list of FK
        72631  +** constraints for which table pTab is the parent table. For example,
        72632  +** given the following schema:
        72633  +**
        72634  +**   CREATE TABLE t1(a PRIMARY KEY);
        72635  +**   CREATE TABLE t2(b REFERENCES t1(a);
        72636  +**
        72637  +** Calling this function with table "t1" as an argument returns a pointer
        72638  +** to the FKey structure representing the foreign key constraint on table
        72639  +** "t2". Calling this function with "t2" as the argument would return a
        72640  +** NULL pointer (as there are no FK constraints for which t2 is the parent
        72641  +** table).
        72642  +*/
        72643  +SQLITE_PRIVATE FKey *sqlite3FkReferences(Table *pTab){
        72644  +  int nName = sqlite3Strlen30(pTab->zName);
        72645  +  return (FKey *)sqlite3HashFind(&pTab->pSchema->fkeyHash, pTab->zName, nName);
        72646  +}
        72647  +
        72648  +/*
        72649  +** The second argument is a Trigger structure allocated by the 
        72650  +** fkActionTrigger() routine. This function deletes the Trigger structure
        72651  +** and all of its sub-components.
        72652  +**
        72653  +** The Trigger structure or any of its sub-components may be allocated from
        72654  +** the lookaside buffer belonging to database handle dbMem.
        72655  +*/
        72656  +static void fkTriggerDelete(sqlite3 *dbMem, Trigger *p){
        72657  +  if( p ){
        72658  +    TriggerStep *pStep = p->step_list;
        72659  +    sqlite3ExprDelete(dbMem, pStep->pWhere);
        72660  +    sqlite3ExprListDelete(dbMem, pStep->pExprList);
        72661  +    sqlite3SelectDelete(dbMem, pStep->pSelect);
        72662  +    sqlite3ExprDelete(dbMem, p->pWhen);
        72663  +    sqlite3DbFree(dbMem, p);
        72664  +  }
        72665  +}
        72666  +
        72667  +/*
        72668  +** This function is called to generate code that runs when table pTab is
        72669  +** being dropped from the database. The SrcList passed as the second argument
        72670  +** to this function contains a single entry guaranteed to resolve to
        72671  +** table pTab.
        72672  +**
        72673  +** Normally, no code is required. However, if either
        72674  +**
        72675  +**   (a) The table is the parent table of a FK constraint, or
        72676  +**   (b) The table is the child table of a deferred FK constraint and it is
        72677  +**       determined at runtime that there are outstanding deferred FK 
        72678  +**       constraint violations in the database,
        72679  +**
        72680  +** then the equivalent of "DELETE FROM <tbl>" is executed before dropping
        72681  +** the table from the database. Triggers are disabled while running this
        72682  +** DELETE, but foreign key actions are not.
        72683  +*/
        72684  +SQLITE_PRIVATE void sqlite3FkDropTable(Parse *pParse, SrcList *pName, Table *pTab){
        72685  +  sqlite3 *db = pParse->db;
        72686  +  if( (db->flags&SQLITE_ForeignKeys) && !IsVirtual(pTab) && !pTab->pSelect ){
        72687  +    int iSkip = 0;
        72688  +    Vdbe *v = sqlite3GetVdbe(pParse);
        72689  +
        72690  +    assert( v );                  /* VDBE has already been allocated */
        72691  +    if( sqlite3FkReferences(pTab)==0 ){
        72692  +      /* Search for a deferred foreign key constraint for which this table
        72693  +      ** is the child table. If one cannot be found, return without 
        72694  +      ** generating any VDBE code. If one can be found, then jump over
        72695  +      ** the entire DELETE if there are no outstanding deferred constraints
        72696  +      ** when this statement is run.  */
        72697  +      FKey *p;
        72698  +      for(p=pTab->pFKey; p; p=p->pNextFrom){
        72699  +        if( p->isDeferred ) break;
        72700  +      }
        72701  +      if( !p ) return;
        72702  +      iSkip = sqlite3VdbeMakeLabel(v);
        72703  +      sqlite3VdbeAddOp2(v, OP_FkIfZero, 1, iSkip);
        72704  +    }
        72705  +
        72706  +    pParse->disableTriggers = 1;
        72707  +    sqlite3DeleteFrom(pParse, sqlite3SrcListDup(db, pName, 0), 0);
        72708  +    pParse->disableTriggers = 0;
        72709  +
        72710  +    /* If the DELETE has generated immediate foreign key constraint 
        72711  +    ** violations, halt the VDBE and return an error at this point, before
        72712  +    ** any modifications to the schema are made. This is because statement
        72713  +    ** transactions are not able to rollback schema changes.  */
        72714  +    sqlite3VdbeAddOp2(v, OP_FkIfZero, 0, sqlite3VdbeCurrentAddr(v)+2);
        72715  +    sqlite3HaltConstraint(
        72716  +        pParse, OE_Abort, "foreign key constraint failed", P4_STATIC
        72717  +    );
        72718  +
        72719  +    if( iSkip ){
        72720  +      sqlite3VdbeResolveLabel(v, iSkip);
        72721  +    }
        72722  +  }
        72723  +}
        72724  +
        72725  +/*
        72726  +** This function is called when inserting, deleting or updating a row of
        72727  +** table pTab to generate VDBE code to perform foreign key constraint 
        72728  +** processing for the operation.
        72729  +**
        72730  +** For a DELETE operation, parameter regOld is passed the index of the
        72731  +** first register in an array of (pTab->nCol+1) registers containing the
        72732  +** rowid of the row being deleted, followed by each of the column values
        72733  +** of the row being deleted, from left to right. Parameter regNew is passed
        72734  +** zero in this case.
        72735  +**
        72736  +** For an INSERT operation, regOld is passed zero and regNew is passed the
        72737  +** first register of an array of (pTab->nCol+1) registers containing the new
        72738  +** row data.
        72739  +**
        72740  +** For an UPDATE operation, this function is called twice. Once before
        72741  +** the original record is deleted from the table using the calling convention
        72742  +** described for DELETE. Then again after the original record is deleted
        72743  +** but before the new record is inserted using the INSERT convention. 
        72744  +*/
        72745  +SQLITE_PRIVATE void sqlite3FkCheck(
        72746  +  Parse *pParse,                  /* Parse context */
        72747  +  Table *pTab,                    /* Row is being deleted from this table */ 
        72748  +  int regOld,                     /* Previous row data is stored here */
        72749  +  int regNew                      /* New row data is stored here */
        72750  +){
        72751  +  sqlite3 *db = pParse->db;       /* Database handle */
        72752  +  Vdbe *v;                        /* VM to write code to */
        72753  +  FKey *pFKey;                    /* Used to iterate through FKs */
        72754  +  int iDb;                        /* Index of database containing pTab */
        72755  +  const char *zDb;                /* Name of database containing pTab */
        72756  +  int isIgnoreErrors = pParse->disableTriggers;
        72757  +
        72758  +  /* Exactly one of regOld and regNew should be non-zero. */
        72759  +  assert( (regOld==0)!=(regNew==0) );
        72760  +
        72761  +  /* If foreign-keys are disabled, this function is a no-op. */
        72762  +  if( (db->flags&SQLITE_ForeignKeys)==0 ) return;
        72763  +
        72764  +  v = sqlite3GetVdbe(pParse);
        72765  +  iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
        72766  +  zDb = db->aDb[iDb].zName;
        72767  +
        72768  +  /* Loop through all the foreign key constraints for which pTab is the
        72769  +  ** child table (the table that the foreign key definition is part of).  */
        72770  +  for(pFKey=pTab->pFKey; pFKey; pFKey=pFKey->pNextFrom){
        72771  +    Table *pTo;                   /* Parent table of foreign key pFKey */
        72772  +    Index *pIdx = 0;              /* Index on key columns in pTo */
        72773  +    int *aiFree = 0;
        72774  +    int *aiCol;
        72775  +    int iCol;
        72776  +    int i;
        72777  +    int isIgnore = 0;
        72778  +
        72779  +    /* Find the parent table of this foreign key. Also find a unique index 
        72780  +    ** on the parent key columns in the parent table. If either of these 
        72781  +    ** schema items cannot be located, set an error in pParse and return 
        72782  +    ** early.  */
        72783  +    if( pParse->disableTriggers ){
        72784  +      pTo = sqlite3FindTable(db, pFKey->zTo, zDb);
        72785  +    }else{
        72786  +      pTo = sqlite3LocateTable(pParse, 0, pFKey->zTo, zDb);
        72787  +    }
        72788  +    if( !pTo || locateFkeyIndex(pParse, pTo, pFKey, &pIdx, &aiFree) ){
        72789  +      if( !isIgnoreErrors || db->mallocFailed ) return;
        72790  +      continue;
        72791  +    }
        72792  +    assert( pFKey->nCol==1 || (aiFree && pIdx) );
        72793  +
        72794  +    if( aiFree ){
        72795  +      aiCol = aiFree;
        72796  +    }else{
        72797  +      iCol = pFKey->aCol[0].iFrom;
        72798  +      aiCol = &iCol;
        72799  +    }
        72800  +    for(i=0; i<pFKey->nCol; i++){
        72801  +      if( aiCol[i]==pTab->iPKey ){
        72802  +        aiCol[i] = -1;
        72803  +      }
        72804  +#ifndef SQLITE_OMIT_AUTHORIZATION
        72805  +      /* Request permission to read the parent key columns. If the 
        72806  +      ** authorization callback returns SQLITE_IGNORE, behave as if any
        72807  +      ** values read from the parent table are NULL. */
        72808  +      if( db->xAuth ){
        72809  +        int rcauth;
        72810  +        char *zCol = pTo->aCol[pIdx ? pIdx->aiColumn[i] : pTo->iPKey].zName;
        72811  +        rcauth = sqlite3AuthReadCol(pParse, pTo->zName, zCol, iDb);
        72812  +        isIgnore = (rcauth==SQLITE_IGNORE);
        72813  +      }
        72814  +#endif
        72815  +    }
        72816  +
        72817  +    /* Take a shared-cache advisory read-lock on the parent table. Allocate 
        72818  +    ** a cursor to use to search the unique index on the parent key columns 
        72819  +    ** in the parent table.  */
        72820  +    sqlite3TableLock(pParse, iDb, pTo->tnum, 0, pTo->zName);
        72821  +    pParse->nTab++;
        72822  +
        72823  +    if( regOld!=0 ){
        72824  +      /* A row is being removed from the child table. Search for the parent.
        72825  +      ** If the parent does not exist, removing the child row resolves an 
        72826  +      ** outstanding foreign key constraint violation. */
        72827  +      fkLookupParent(pParse, iDb, pTo, pIdx, pFKey, aiCol, regOld, -1,isIgnore);
        72828  +    }
        72829  +    if( regNew!=0 ){
        72830  +      /* A row is being added to the child table. If a parent row cannot
        72831  +      ** be found, adding the child row has violated the FK constraint. */ 
        72832  +      fkLookupParent(pParse, iDb, pTo, pIdx, pFKey, aiCol, regNew, +1,isIgnore);
        72833  +    }
        72834  +
        72835  +    sqlite3DbFree(db, aiFree);
        72836  +  }
        72837  +
        72838  +  /* Loop through all the foreign key constraints that refer to this table */
        72839  +  for(pFKey = sqlite3FkReferences(pTab); pFKey; pFKey=pFKey->pNextTo){
        72840  +    Index *pIdx = 0;              /* Foreign key index for pFKey */
        72841  +    SrcList *pSrc;
        72842  +    int *aiCol = 0;
        72843  +
        72844  +    if( !pFKey->isDeferred && !pParse->pToplevel && !pParse->isMultiWrite ){
        72845  +      assert( regOld==0 && regNew!=0 );
        72846  +      /* Inserting a single row into a parent table cannot cause an immediate
        72847  +      ** foreign key violation. So do nothing in this case.  */
        72848  +      continue;
        72849  +    }
        72850  +
        72851  +    if( locateFkeyIndex(pParse, pTab, pFKey, &pIdx, &aiCol) ){
        72852  +      if( !isIgnoreErrors || db->mallocFailed ) return;
        72853  +      continue;
        72854  +    }
        72855  +    assert( aiCol || pFKey->nCol==1 );
        72856  +
        72857  +    /* Create a SrcList structure containing a single table (the table 
        72858  +    ** the foreign key that refers to this table is attached to). This
        72859  +    ** is required for the sqlite3WhereXXX() interface.  */
        72860  +    pSrc = sqlite3SrcListAppend(db, 0, 0, 0);
        72861  +    if( pSrc ){
        72862  +      struct SrcList_item *pItem = pSrc->a;
        72863  +      pItem->pTab = pFKey->pFrom;
        72864  +      pItem->zName = pFKey->pFrom->zName;
        72865  +      pItem->pTab->nRef++;
        72866  +      pItem->iCursor = pParse->nTab++;
        72867  +  
        72868  +      if( regNew!=0 ){
        72869  +        fkScanChildren(pParse, pSrc, pTab, pIdx, pFKey, aiCol, regNew, -1);
        72870  +      }
        72871  +      if( regOld!=0 ){
        72872  +        /* If there is a RESTRICT action configured for the current operation
        72873  +        ** on the parent table of this FK, then throw an exception 
        72874  +        ** immediately if the FK constraint is violated, even if this is a
        72875  +        ** deferred trigger. That's what RESTRICT means. To defer checking
        72876  +        ** the constraint, the FK should specify NO ACTION (represented
        72877  +        ** using OE_None). NO ACTION is the default.  */
        72878  +        fkScanChildren(pParse, pSrc, pTab, pIdx, pFKey, aiCol, regOld, 1);
        72879  +      }
        72880  +      pItem->zName = 0;
        72881  +      sqlite3SrcListDelete(db, pSrc);
        72882  +    }
        72883  +    sqlite3DbFree(db, aiCol);
        72884  +  }
        72885  +}
        72886  +
        72887  +#define COLUMN_MASK(x) (((x)>31) ? 0xffffffff : ((u32)1<<(x)))
        72888  +
        72889  +/*
        72890  +** This function is called before generating code to update or delete a 
        72891  +** row contained in table pTab.
        72892  +*/
        72893  +SQLITE_PRIVATE u32 sqlite3FkOldmask(
        72894  +  Parse *pParse,                  /* Parse context */
        72895  +  Table *pTab                     /* Table being modified */
        72896  +){
        72897  +  u32 mask = 0;
        72898  +  if( pParse->db->flags&SQLITE_ForeignKeys ){
        72899  +    FKey *p;
        72900  +    int i;
        72901  +    for(p=pTab->pFKey; p; p=p->pNextFrom){
        72902  +      for(i=0; i<p->nCol; i++) mask |= COLUMN_MASK(p->aCol[i].iFrom);
        72903  +    }
        72904  +    for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
        72905  +      Index *pIdx = 0;
        72906  +      locateFkeyIndex(pParse, pTab, p, &pIdx, 0);
        72907  +      if( pIdx ){
        72908  +        for(i=0; i<pIdx->nColumn; i++) mask |= COLUMN_MASK(pIdx->aiColumn[i]);
        72909  +      }
        72910  +    }
        72911  +  }
        72912  +  return mask;
        72913  +}
        72914  +
        72915  +/*
        72916  +** This function is called before generating code to update or delete a 
        72917  +** row contained in table pTab. If the operation is a DELETE, then
        72918  +** parameter aChange is passed a NULL value. For an UPDATE, aChange points
        72919  +** to an array of size N, where N is the number of columns in table pTab.
        72920  +** If the i'th column is not modified by the UPDATE, then the corresponding 
        72921  +** entry in the aChange[] array is set to -1. If the column is modified,
        72922  +** the value is 0 or greater. Parameter chngRowid is set to true if the
        72923  +** UPDATE statement modifies the rowid fields of the table.
        72924  +**
        72925  +** If any foreign key processing will be required, this function returns
        72926  +** true. If there is no foreign key related processing, this function 
        72927  +** returns false.
        72928  +*/
        72929  +SQLITE_PRIVATE int sqlite3FkRequired(
        72930  +  Parse *pParse,                  /* Parse context */
        72931  +  Table *pTab,                    /* Table being modified */
        72932  +  int *aChange,                   /* Non-NULL for UPDATE operations */
        72933  +  int chngRowid                   /* True for UPDATE that affects rowid */
        72934  +){
        72935  +  if( pParse->db->flags&SQLITE_ForeignKeys ){
        72936  +    if( !aChange ){
        72937  +      /* A DELETE operation. Foreign key processing is required if the 
        72938  +      ** table in question is either the child or parent table for any 
        72939  +      ** foreign key constraint.  */
        72940  +      return (sqlite3FkReferences(pTab) || pTab->pFKey);
        72941  +    }else{
        72942  +      /* This is an UPDATE. Foreign key processing is only required if the
        72943  +      ** operation modifies one or more child or parent key columns. */
        72944  +      int i;
        72945  +      FKey *p;
        72946  +
        72947  +      /* Check if any child key columns are being modified. */
        72948  +      for(p=pTab->pFKey; p; p=p->pNextFrom){
        72949  +        for(i=0; i<p->nCol; i++){
        72950  +          int iChildKey = p->aCol[i].iFrom;
        72951  +          if( aChange[iChildKey]>=0 ) return 1;
        72952  +          if( iChildKey==pTab->iPKey && chngRowid ) return 1;
        72953  +        }
        72954  +      }
        72955  +
        72956  +      /* Check if any parent key columns are being modified. */
        72957  +      for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
        72958  +        for(i=0; i<p->nCol; i++){
        72959  +          char *zKey = p->aCol[i].zCol;
        72960  +          int iKey;
        72961  +          for(iKey=0; iKey<pTab->nCol; iKey++){
        72962  +            Column *pCol = &pTab->aCol[iKey];
        72963  +            if( (zKey ? !sqlite3StrICmp(pCol->zName, zKey) : pCol->isPrimKey) ){
        72964  +              if( aChange[iKey]>=0 ) return 1;
        72965  +              if( iKey==pTab->iPKey && chngRowid ) return 1;
        72966  +            }
        72967  +          }
        72968  +        }
        72969  +      }
        72970  +    }
        72971  +  }
        72972  +  return 0;
        72973  +}
        72974  +
        72975  +/*
        72976  +** This function is called when an UPDATE or DELETE operation is being 
        72977  +** compiled on table pTab, which is the parent table of foreign-key pFKey.
        72978  +** If the current operation is an UPDATE, then the pChanges parameter is
        72979  +** passed a pointer to the list of columns being modified. If it is a
        72980  +** DELETE, pChanges is passed a NULL pointer.
        72981  +**
        72982  +** It returns a pointer to a Trigger structure containing a trigger
        72983  +** equivalent to the ON UPDATE or ON DELETE action specified by pFKey.
        72984  +** If the action is "NO ACTION" or "RESTRICT", then a NULL pointer is
        72985  +** returned (these actions require no special handling by the triggers
        72986  +** sub-system, code for them is created by fkScanChildren()).
        72987  +**
        72988  +** For example, if pFKey is the foreign key and pTab is table "p" in 
        72989  +** the following schema:
        72990  +**
        72991  +**   CREATE TABLE p(pk PRIMARY KEY);
        72992  +**   CREATE TABLE c(ck REFERENCES p ON DELETE CASCADE);
        72993  +**
        72994  +** then the returned trigger structure is equivalent to:
        72995  +**
        72996  +**   CREATE TRIGGER ... DELETE ON p BEGIN
        72997  +**     DELETE FROM c WHERE ck = old.pk;
        72998  +**   END;
        72999  +**
        73000  +** The returned pointer is cached as part of the foreign key object. It
        73001  +** is eventually freed along with the rest of the foreign key object by 
        73002  +** sqlite3FkDelete().
        73003  +*/
        73004  +static Trigger *fkActionTrigger(
        73005  +  Parse *pParse,                  /* Parse context */
        73006  +  Table *pTab,                    /* Table being updated or deleted from */
        73007  +  FKey *pFKey,                    /* Foreign key to get action for */
        73008  +  ExprList *pChanges              /* Change-list for UPDATE, NULL for DELETE */
        73009  +){
        73010  +  sqlite3 *db = pParse->db;       /* Database handle */
        73011  +  int action;                     /* One of OE_None, OE_Cascade etc. */
        73012  +  Trigger *pTrigger;              /* Trigger definition to return */
        73013  +  int iAction = (pChanges!=0);    /* 1 for UPDATE, 0 for DELETE */
        73014  +
        73015  +  action = pFKey->aAction[iAction];
        73016  +  pTrigger = pFKey->apTrigger[iAction];
        73017  +
        73018  +  if( action!=OE_None && !pTrigger ){
        73019  +    u8 enableLookaside;           /* Copy of db->lookaside.bEnabled */
        73020  +    char const *zFrom;            /* Name of child table */
        73021  +    int nFrom;                    /* Length in bytes of zFrom */
        73022  +    Index *pIdx = 0;              /* Parent key index for this FK */
        73023  +    int *aiCol = 0;               /* child table cols -> parent key cols */
        73024  +    TriggerStep *pStep = 0;        /* First (only) step of trigger program */
        73025  +    Expr *pWhere = 0;             /* WHERE clause of trigger step */
        73026  +    ExprList *pList = 0;          /* Changes list if ON UPDATE CASCADE */
        73027  +    Select *pSelect = 0;          /* If RESTRICT, "SELECT RAISE(...)" */
        73028  +    int i;                        /* Iterator variable */
        73029  +    Expr *pWhen = 0;              /* WHEN clause for the trigger */
        73030  +
        73031  +    if( locateFkeyIndex(pParse, pTab, pFKey, &pIdx, &aiCol) ) return 0;
        73032  +    assert( aiCol || pFKey->nCol==1 );
        73033  +
        73034  +    for(i=0; i<pFKey->nCol; i++){
        73035  +      Token tOld = { "old", 3 };  /* Literal "old" token */
        73036  +      Token tNew = { "new", 3 };  /* Literal "new" token */
        73037  +      Token tFromCol;             /* Name of column in child table */
        73038  +      Token tToCol;               /* Name of column in parent table */
        73039  +      int iFromCol;               /* Idx of column in child table */
        73040  +      Expr *pEq;                  /* tFromCol = OLD.tToCol */
        73041  +
        73042  +      iFromCol = aiCol ? aiCol[i] : pFKey->aCol[0].iFrom;
        73043  +      assert( iFromCol>=0 );
        73044  +      tToCol.z = pIdx ? pTab->aCol[pIdx->aiColumn[i]].zName : "oid";
        73045  +      tFromCol.z = pFKey->pFrom->aCol[iFromCol].zName;
        73046  +
        73047  +      tToCol.n = sqlite3Strlen30(tToCol.z);
        73048  +      tFromCol.n = sqlite3Strlen30(tFromCol.z);
        73049  +
        73050  +      /* Create the expression "OLD.zToCol = zFromCol". It is important
        73051  +      ** that the "OLD.zToCol" term is on the LHS of the = operator, so
        73052  +      ** that the affinity and collation sequence associated with the
        73053  +      ** parent table are used for the comparison. */
        73054  +      pEq = sqlite3PExpr(pParse, TK_EQ,
        73055  +          sqlite3PExpr(pParse, TK_DOT, 
        73056  +            sqlite3PExpr(pParse, TK_ID, 0, 0, &tOld),
        73057  +            sqlite3PExpr(pParse, TK_ID, 0, 0, &tToCol)
        73058  +          , 0),
        73059  +          sqlite3PExpr(pParse, TK_ID, 0, 0, &tFromCol)
        73060  +      , 0);
        73061  +      pWhere = sqlite3ExprAnd(db, pWhere, pEq);
        73062  +
        73063  +      /* For ON UPDATE, construct the next term of the WHEN clause.
        73064  +      ** The final WHEN clause will be like this:
        73065  +      **
        73066  +      **    WHEN NOT(old.col1 IS new.col1 AND ... AND old.colN IS new.colN)
        73067  +      */
        73068  +      if( pChanges ){
        73069  +        pEq = sqlite3PExpr(pParse, TK_IS,
        73070  +            sqlite3PExpr(pParse, TK_DOT, 
        73071  +              sqlite3PExpr(pParse, TK_ID, 0, 0, &tOld),
        73072  +              sqlite3PExpr(pParse, TK_ID, 0, 0, &tToCol),
        73073  +              0),
        73074  +            sqlite3PExpr(pParse, TK_DOT, 
        73075  +              sqlite3PExpr(pParse, TK_ID, 0, 0, &tNew),
        73076  +              sqlite3PExpr(pParse, TK_ID, 0, 0, &tToCol),
        73077  +              0),
        73078  +            0);
        73079  +        pWhen = sqlite3ExprAnd(db, pWhen, pEq);
        73080  +      }
        73081  +  
        73082  +      if( action!=OE_Restrict && (action!=OE_Cascade || pChanges) ){
        73083  +        Expr *pNew;
        73084  +        if( action==OE_Cascade ){
        73085  +          pNew = sqlite3PExpr(pParse, TK_DOT, 
        73086  +            sqlite3PExpr(pParse, TK_ID, 0, 0, &tNew),
        73087  +            sqlite3PExpr(pParse, TK_ID, 0, 0, &tToCol)
        73088  +          , 0);
        73089  +        }else if( action==OE_SetDflt ){
        73090  +          Expr *pDflt = pFKey->pFrom->aCol[iFromCol].pDflt;
        73091  +          if( pDflt ){
        73092  +            pNew = sqlite3ExprDup(db, pDflt, 0);
        73093  +          }else{
        73094  +            pNew = sqlite3PExpr(pParse, TK_NULL, 0, 0, 0);
        73095  +          }
        73096  +        }else{
        73097  +          pNew = sqlite3PExpr(pParse, TK_NULL, 0, 0, 0);
        73098  +        }
        73099  +        pList = sqlite3ExprListAppend(pParse, pList, pNew);
        73100  +        sqlite3ExprListSetName(pParse, pList, &tFromCol, 0);
        73101  +      }
        73102  +    }
        73103  +    sqlite3DbFree(db, aiCol);
        73104  +
        73105  +    zFrom = pFKey->pFrom->zName;
        73106  +    nFrom = sqlite3Strlen30(zFrom);
        73107  +
        73108  +    if( action==OE_Restrict ){
        73109  +      Token tFrom;
        73110  +      Expr *pRaise; 
        73111  +
        73112  +      tFrom.z = zFrom;
        73113  +      tFrom.n = nFrom;
        73114  +      pRaise = sqlite3Expr(db, TK_RAISE, "foreign key constraint failed");
        73115  +      if( pRaise ){
        73116  +        pRaise->affinity = OE_Abort;
        73117  +      }
        73118  +      pSelect = sqlite3SelectNew(pParse, 
        73119  +          sqlite3ExprListAppend(pParse, 0, pRaise),
        73120  +          sqlite3SrcListAppend(db, 0, &tFrom, 0),
        73121  +          pWhere,
        73122  +          0, 0, 0, 0, 0, 0
        73123  +      );
        73124  +      pWhere = 0;
        73125  +    }
        73126  +
        73127  +    /* In the current implementation, pTab->dbMem==0 for all tables except
        73128  +    ** for temporary tables used to describe subqueries.  And temporary
        73129  +    ** tables do not have foreign key constraints.  Hence, pTab->dbMem
        73130  +    ** should always be 0 there.
        73131  +    */
        73132  +    enableLookaside = db->lookaside.bEnabled;
        73133  +    db->lookaside.bEnabled = 0;
        73134  +
        73135  +    pTrigger = (Trigger *)sqlite3DbMallocZero(db, 
        73136  +        sizeof(Trigger) +         /* struct Trigger */
        73137  +        sizeof(TriggerStep) +     /* Single step in trigger program */
        73138  +        nFrom + 1                 /* Space for pStep->target.z */
        73139  +    );
        73140  +    if( pTrigger ){
        73141  +      pStep = pTrigger->step_list = (TriggerStep *)&pTrigger[1];
        73142  +      pStep->target.z = (char *)&pStep[1];
        73143  +      pStep->target.n = nFrom;
        73144  +      memcpy((char *)pStep->target.z, zFrom, nFrom);
        73145  +  
        73146  +      pStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
        73147  +      pStep->pExprList = sqlite3ExprListDup(db, pList, EXPRDUP_REDUCE);
        73148  +      pStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
        73149  +      if( pWhen ){
        73150  +        pWhen = sqlite3PExpr(pParse, TK_NOT, pWhen, 0, 0);
        73151  +        pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE);
        73152  +      }
        73153  +    }
        73154  +
        73155  +    /* Re-enable the lookaside buffer, if it was disabled earlier. */
        73156  +    db->lookaside.bEnabled = enableLookaside;
        73157  +
        73158  +    sqlite3ExprDelete(db, pWhere);
        73159  +    sqlite3ExprDelete(db, pWhen);
        73160  +    sqlite3ExprListDelete(db, pList);
        73161  +    sqlite3SelectDelete(db, pSelect);
        73162  +    if( db->mallocFailed==1 ){
        73163  +      fkTriggerDelete(db, pTrigger);
        73164  +      return 0;
        73165  +    }
        73166  +
        73167  +    switch( action ){
        73168  +      case OE_Restrict:
        73169  +        pStep->op = TK_SELECT; 
        73170  +        break;
        73171  +      case OE_Cascade: 
        73172  +        if( !pChanges ){ 
        73173  +          pStep->op = TK_DELETE; 
        73174  +          break; 
        73175  +        }
        73176  +      default:
        73177  +        pStep->op = TK_UPDATE;
        73178  +    }
        73179  +    pStep->pTrig = pTrigger;
        73180  +    pTrigger->pSchema = pTab->pSchema;
        73181  +    pTrigger->pTabSchema = pTab->pSchema;
        73182  +    pFKey->apTrigger[iAction] = pTrigger;
        73183  +    pTrigger->op = (pChanges ? TK_UPDATE : TK_DELETE);
        73184  +  }
        73185  +
        73186  +  return pTrigger;
        73187  +}
        73188  +
        73189  +/*
        73190  +** This function is called when deleting or updating a row to implement
        73191  +** any required CASCADE, SET NULL or SET DEFAULT actions.
        73192  +*/
        73193  +SQLITE_PRIVATE void sqlite3FkActions(
        73194  +  Parse *pParse,                  /* Parse context */
        73195  +  Table *pTab,                    /* Table being updated or deleted from */
        73196  +  ExprList *pChanges,             /* Change-list for UPDATE, NULL for DELETE */
        73197  +  int regOld                      /* Address of array containing old row */
        73198  +){
        73199  +  /* If foreign-key support is enabled, iterate through all FKs that 
        73200  +  ** refer to table pTab. If there is an action associated with the FK 
        73201  +  ** for this operation (either update or delete), invoke the associated 
        73202  +  ** trigger sub-program.  */
        73203  +  if( pParse->db->flags&SQLITE_ForeignKeys ){
        73204  +    FKey *pFKey;                  /* Iterator variable */
        73205  +    for(pFKey = sqlite3FkReferences(pTab); pFKey; pFKey=pFKey->pNextTo){
        73206  +      Trigger *pAction = fkActionTrigger(pParse, pTab, pFKey, pChanges);
        73207  +      if( pAction ){
        73208  +        sqlite3CodeRowTriggerDirect(pParse, pAction, pTab, regOld, OE_Abort, 0);
        73209  +      }
        73210  +    }
        73211  +  }
        73212  +}
        73213  +
        73214  +#endif /* ifndef SQLITE_OMIT_TRIGGER */
        73215  +
        73216  +/*
        73217  +** Free all memory associated with foreign key definitions attached to
        73218  +** table pTab. Remove the deleted foreign keys from the Schema.fkeyHash
        73219  +** hash table.
        73220  +*/
        73221  +SQLITE_PRIVATE void sqlite3FkDelete(Table *pTab){
        73222  +  FKey *pFKey;                    /* Iterator variable */
        73223  +  FKey *pNext;                    /* Copy of pFKey->pNextFrom */
        73224  +
        73225  +  for(pFKey=pTab->pFKey; pFKey; pFKey=pNext){
        73226  +
        73227  +    /* Remove the FK from the fkeyHash hash table. */
        73228  +    if( pFKey->pPrevTo ){
        73229  +      pFKey->pPrevTo->pNextTo = pFKey->pNextTo;
        73230  +    }else{
        73231  +      void *data = (void *)pFKey->pNextTo;
        73232  +      const char *z = (data ? pFKey->pNextTo->zTo : pFKey->zTo);
        73233  +      sqlite3HashInsert(&pTab->pSchema->fkeyHash, z, sqlite3Strlen30(z), data);
        73234  +    }
        73235  +    if( pFKey->pNextTo ){
        73236  +      pFKey->pNextTo->pPrevTo = pFKey->pPrevTo;
        73237  +    }
        73238  +
        73239  +    /* Delete any triggers created to implement actions for this FK. */
        73240  +#ifndef SQLITE_OMIT_TRIGGER
        73241  +    fkTriggerDelete(pTab->dbMem, pFKey->apTrigger[0]);
        73242  +    fkTriggerDelete(pTab->dbMem, pFKey->apTrigger[1]);
        73243  +#endif
        73244  +
        73245  +    /* EV: R-30323-21917 Each foreign key constraint in SQLite is
        73246  +    ** classified as either immediate or deferred.
        73247  +    */
        73248  +    assert( pFKey->isDeferred==0 || pFKey->isDeferred==1 );
        73249  +
        73250  +    pNext = pFKey->pNextFrom;
        73251  +    sqlite3DbFree(pTab->dbMem, pFKey);
        73252  +  }
        73253  +}
        73254  +#endif /* ifndef SQLITE_OMIT_FOREIGN_KEY */
        73255  +
        73256  +/************** End of fkey.c ************************************************/
 71575  73257   /************** Begin file insert.c ******************************************/
 71576  73258   /*
 71577  73259   ** 2001 September 15
 71578  73260   **
 71579  73261   ** The author disclaims copyright to this source code.  In place of
 71580  73262   ** a legal notice, here is a blessing:
 71581  73263   **
................................................................................
 72440  74122       if( !isView ){
 72441  74123         sqlite3VdbeAddOp2(v, OP_Affinity, regCols+1, pTab->nCol);
 72442  74124         sqlite3TableAffinityStr(v, pTab);
 72443  74125       }
 72444  74126   
 72445  74127       /* Fire BEFORE or INSTEAD OF triggers */
 72446  74128       sqlite3CodeRowTrigger(pParse, pTrigger, TK_INSERT, 0, TRIGGER_BEFORE, 
 72447         -        pTab, -1, regCols-pTab->nCol-1, onError, endOfLoop);
        74129  +        pTab, regCols-pTab->nCol-1, onError, endOfLoop);
 72448  74130   
 72449  74131       sqlite3ReleaseTempRange(pParse, regCols, pTab->nCol+1);
 72450  74132     }
 72451  74133   
 72452  74134     /* Push the record number for the new entry onto the stack.  The
 72453  74135     ** record number is a randomly generate integer created by NewRowid
 72454  74136     ** except when the table has an INTEGER PRIMARY KEY column, in which
................................................................................
 72549  74231       }else
 72550  74232   #endif
 72551  74233       {
 72552  74234         int isReplace;    /* Set to true if constraints may cause a replace */
 72553  74235         sqlite3GenerateConstraintChecks(pParse, pTab, baseCur, regIns, aRegIdx,
 72554  74236             keyColumn>=0, 0, onError, endOfLoop, &isReplace
 72555  74237         );
        74238  +      sqlite3FkCheck(pParse, pTab, 0, regIns);
 72556  74239         sqlite3CompleteInsertion(
 72557  74240             pParse, pTab, baseCur, regIns, aRegIdx, 0, appendFlag, isReplace==0
 72558  74241         );
 72559  74242       }
 72560  74243     }
 72561  74244   
 72562  74245     /* Update the count of rows that are inserted
................................................................................
 72564  74247     if( (db->flags & SQLITE_CountRows)!=0 ){
 72565  74248       sqlite3VdbeAddOp2(v, OP_AddImm, regRowCount, 1);
 72566  74249     }
 72567  74250   
 72568  74251     if( pTrigger ){
 72569  74252       /* Code AFTER triggers */
 72570  74253       sqlite3CodeRowTrigger(pParse, pTrigger, TK_INSERT, 0, TRIGGER_AFTER, 
 72571         -        pTab, -1, regData-2-pTab->nCol, onError, endOfLoop);
        74254  +        pTab, regData-2-pTab->nCol, onError, endOfLoop);
 72572  74255     }
 72573  74256   
 72574  74257     /* The bottom of the main insertion loop, if the data source
 72575  74258     ** is a SELECT statement.
 72576  74259     */
 72577  74260     sqlite3VdbeResolveLabel(v, endOfLoop);
 72578  74261     if( useTempTable ){
................................................................................
 72615  74298   insert_cleanup:
 72616  74299     sqlite3SrcListDelete(db, pTabList);
 72617  74300     sqlite3ExprListDelete(db, pList);
 72618  74301     sqlite3SelectDelete(db, pSelect);
 72619  74302     sqlite3IdListDelete(db, pColumn);
 72620  74303     sqlite3DbFree(db, aRegIdx);
 72621  74304   }
        74305  +
        74306  +/* Make sure "isView" and other macros defined above are undefined. Otherwise
        74307  +** thely may interfere with compilation of other functions in this file
        74308  +** (or in another file, if this file becomes part of the amalgamation).  */
        74309  +#ifdef isView
        74310  + #undef isView
        74311  +#endif
        74312  +#ifdef pTrigger
        74313  + #undef pTrigger
        74314  +#endif
        74315  +#ifdef tmask
        74316  + #undef tmask
        74317  +#endif
        74318  +
 72622  74319   
 72623  74320   /*
 72624  74321   ** Generate code to do constraint checks prior to an INSERT or an UPDATE.
 72625  74322   **
 72626  74323   ** The input is a range of consecutive registers as follows:
 72627  74324   **
 72628  74325   **    1.  The rowid of the row after the update.
................................................................................
 72795  74492       onError = pTab->keyConf;
 72796  74493       if( overrideError!=OE_Default ){
 72797  74494         onError = overrideError;
 72798  74495       }else if( onError==OE_Default ){
 72799  74496         onError = OE_Abort;
 72800  74497       }
 72801  74498       
 72802         -    if( onError!=OE_Replace || pTab->pIndex ){
 72803         -      if( isUpdate ){
 72804         -        j2 = sqlite3VdbeAddOp3(v, OP_Eq, regRowid, 0, rowidChng);
 72805         -      }
 72806         -      j3 = sqlite3VdbeAddOp3(v, OP_NotExists, baseCur, 0, regRowid);
 72807         -      switch( onError ){
 72808         -        default: {
 72809         -          onError = OE_Abort;
 72810         -          /* Fall thru into the next case */
 72811         -        }
 72812         -        case OE_Rollback:
 72813         -        case OE_Abort:
 72814         -        case OE_Fail: {
 72815         -          sqlite3HaltConstraint(
 72816         -            pParse, onError, "PRIMARY KEY must be unique", P4_STATIC);
 72817         -          break;
 72818         -        }
 72819         -        case OE_Replace: {
 72820         -          /* If there are DELETE triggers on this table and the
 72821         -          ** recursive-triggers flag is set, call GenerateRowDelete() to
 72822         -          ** remove the conflicting row from the the table. This will fire
 72823         -          ** the triggers and remove both the table and index b-tree entries.
 72824         -          **
 72825         -          ** Otherwise, if there are no triggers or the recursive-triggers
 72826         -          ** flag is not set, call GenerateRowIndexDelete(). This removes
 72827         -          ** the index b-tree entries only. The table b-tree entry will be 
 72828         -          ** replaced by the new entry when it is inserted.  */
 72829         -          Trigger *pTrigger = 0;
 72830         -          if( pParse->db->flags&SQLITE_RecTriggers ){
 72831         -            pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0);
 72832         -          }
 72833         -          if( pTrigger ){
 72834         -            sqlite3GenerateRowDelete(
 72835         -                pParse, pTab, baseCur, regRowid, 0, pTrigger, OE_Replace
 72836         -            );
 72837         -          }else{
 72838         -            sqlite3GenerateRowIndexDelete(pParse, pTab, baseCur, 0);
 72839         -          }
 72840         -          seenReplace = 1;
 72841         -          break;
 72842         -        }
 72843         -        case OE_Ignore: {
 72844         -          assert( seenReplace==0 );
 72845         -          sqlite3VdbeAddOp2(v, OP_Goto, 0, ignoreDest);
 72846         -          break;
 72847         -        }
 72848         -      }
 72849         -      sqlite3VdbeJumpHere(v, j3);
 72850         -      if( isUpdate ){
 72851         -        sqlite3VdbeJumpHere(v, j2);
 72852         -      }
        74499  +    if( isUpdate ){
        74500  +      j2 = sqlite3VdbeAddOp3(v, OP_Eq, regRowid, 0, rowidChng);
        74501  +    }
        74502  +    j3 = sqlite3VdbeAddOp3(v, OP_NotExists, baseCur, 0, regRowid);
        74503  +    switch( onError ){
        74504  +      default: {
        74505  +        onError = OE_Abort;
        74506  +        /* Fall thru into the next case */
        74507  +      }
        74508  +      case OE_Rollback:
        74509  +      case OE_Abort:
        74510  +      case OE_Fail: {
        74511  +        sqlite3HaltConstraint(
        74512  +          pParse, onError, "PRIMARY KEY must be unique", P4_STATIC);
        74513  +        break;
        74514  +      }
        74515  +      case OE_Replace: {
        74516  +        /* If there are DELETE triggers on this table and the
        74517  +        ** recursive-triggers flag is set, call GenerateRowDelete() to
        74518  +        ** remove the conflicting row from the the table. This will fire
        74519  +        ** the triggers and remove both the table and index b-tree entries.
        74520  +        **
        74521  +        ** Otherwise, if there are no triggers or the recursive-triggers
        74522  +        ** flag is not set, call GenerateRowIndexDelete(). This removes
        74523  +        ** the index b-tree entries only. The table b-tree entry will be 
        74524  +        ** replaced by the new entry when it is inserted.  */
        74525  +        Trigger *pTrigger = 0;
        74526  +        if( pParse->db->flags&SQLITE_RecTriggers ){
        74527  +          pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0);
        74528  +        }
        74529  +        sqlite3MultiWrite(pParse);
        74530  +        if( pTrigger || sqlite3FkRequired(pParse, pTab, 0, 0) ){
        74531  +          sqlite3GenerateRowDelete(
        74532  +              pParse, pTab, baseCur, regRowid, 0, pTrigger, OE_Replace
        74533  +          );
        74534  +        }else{
        74535  +          sqlite3GenerateRowIndexDelete(pParse, pTab, baseCur, 0);
        74536  +        }
        74537  +        seenReplace = 1;
        74538  +        break;
        74539  +      }
        74540  +      case OE_Ignore: {
        74541  +        assert( seenReplace==0 );
        74542  +        sqlite3VdbeAddOp2(v, OP_Goto, 0, ignoreDest);
        74543  +        break;
        74544  +      }
        74545  +    }
        74546  +    sqlite3VdbeJumpHere(v, j3);
        74547  +    if( isUpdate ){
        74548  +      sqlite3VdbeJumpHere(v, j2);
 72853  74549       }
 72854  74550     }
 72855  74551   
 72856  74552     /* Test all UNIQUE constraints by creating entries for each UNIQUE
 72857  74553     ** index and making sure that duplicate entries do not already exist.
 72858  74554     ** Add the new records to the indices as we go.
 72859  74555     */
................................................................................
 72934  74630           assert( seenReplace==0 );
 72935  74631           sqlite3VdbeAddOp2(v, OP_Goto, 0, ignoreDest);
 72936  74632           break;
 72937  74633         }
 72938  74634         default: {
 72939  74635           Trigger *pTrigger = 0;
 72940  74636           assert( onError==OE_Replace );
        74637  +        sqlite3MultiWrite(pParse);
 72941  74638           if( pParse->db->flags&SQLITE_RecTriggers ){
 72942  74639             pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0);
 72943  74640           }
 72944  74641           sqlite3GenerateRowDelete(
 72945  74642               pParse, pTab, baseCur, regR, 0, pTrigger, OE_Replace
 72946  74643           );
 72947  74644           seenReplace = 1;
 72948  74645           break;
 72949  74646         }
 72950  74647       }
 72951  74648       sqlite3VdbeJumpHere(v, j3);
 72952  74649       sqlite3ReleaseTempReg(pParse, regR);
 72953  74650     }
 72954         -
        74651  +  
 72955  74652     if( pbMayReplace ){
 72956  74653       *pbMayReplace = seenReplace;
 72957  74654     }
 72958  74655   }
 72959  74656   
 72960  74657   /*
 72961  74658   ** This routine generates code to finish the INSERT or UPDATE operation
................................................................................
 73371  75068       return 0;
 73372  75069     }else{
 73373  75070       return 1;
 73374  75071     }
 73375  75072   }
 73376  75073   #endif /* SQLITE_OMIT_XFER_OPT */
 73377  75074   
 73378         -/* Make sure "isView" gets undefined in case this file becomes part of
 73379         -** the amalgamation - so that subsequent files do not see isView as a
 73380         -** macro. */
 73381         -#undef isView
 73382         -
 73383  75075   /************** End of insert.c **********************************************/
 73384  75076   /************** Begin file legacy.c ******************************************/
 73385  75077   /*
 73386  75078   ** 2001 September 15
 73387  75079   **
 73388  75080   ** The author disclaims copyright to this source code.  In place of
 73389  75081   ** a legal notice, here is a blessing:
................................................................................
 74713  76405       { "writable_schema",          SQLITE_WriteSchema|SQLITE_RecoveryMode },
 74714  76406       { "omit_readlock",            SQLITE_NoReadlock    },
 74715  76407   
 74716  76408       /* TODO: Maybe it shouldn't be possible to change the ReadUncommitted
 74717  76409       ** flag if there are any active statements. */
 74718  76410       { "read_uncommitted",         SQLITE_ReadUncommitted },
 74719  76411       { "recursive_triggers",       SQLITE_RecTriggers },
        76412  +
        76413  +    /* This flag may only be set if both foreign-key and trigger support
        76414  +    ** are present in the build.  */
        76415  +#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
        76416  +    { "foreign_keys",             SQLITE_ForeignKeys },
        76417  +#endif
 74720  76418     };
 74721  76419     int i;
 74722  76420     const struct sPragmaType *p;
 74723  76421     for(i=0, p=aPragma; i<ArraySize(aPragma); i++, p++){
 74724  76422       if( sqlite3StrICmp(zLeft, p->zName)==0 ){
 74725  76423         sqlite3 *db = pParse->db;
 74726  76424         Vdbe *v;
 74727  76425         v = sqlite3GetVdbe(pParse);
 74728  76426         assert( v!=0 );  /* Already allocated by sqlite3Pragma() */
 74729  76427         if( ALWAYS(v) ){
 74730  76428           if( zRight==0 ){
 74731  76429             returnSingleInt(pParse, p->zName, (db->flags & p->mask)!=0 );
 74732  76430           }else{
        76431  +          int mask = p->mask;          /* Mask of bits to set or clear. */
        76432  +          if( db->autoCommit==0 ){
        76433  +            /* Foreign key support may not be enabled or disabled while not
        76434  +            ** in auto-commit mode.  */
        76435  +            mask &= ~(SQLITE_ForeignKeys);
        76436  +          }
        76437  +
 74733  76438             if( getBoolean(zRight) ){
 74734         -            db->flags |= p->mask;
        76439  +            db->flags |= mask;
 74735  76440             }else{
 74736         -            db->flags &= ~p->mask;
        76441  +            db->flags &= ~mask;
 74737  76442             }
 74738  76443   
 74739  76444             /* Many of the flag-pragmas modify the code generated by the SQL 
 74740  76445             ** compiler (eg. count_changes). So add an opcode to expire all
 74741  76446             ** compiled SQL statements after modifying a pragma value.
 74742  76447             */
 74743  76448             sqlite3VdbeAddOp2(v, OP_Expire, 0, 0);
................................................................................
 74750  76455     return 0;
 74751  76456   }
 74752  76457   #endif /* SQLITE_OMIT_FLAG_PRAGMAS */
 74753  76458   
 74754  76459   /*
 74755  76460   ** Return a human-readable name for a constraint resolution action.
 74756  76461   */
        76462  +#ifndef SQLITE_OMIT_FOREIGN_KEY
 74757  76463   static const char *actionName(u8 action){
 74758  76464     const char *zName;
 74759  76465     switch( action ){
 74760         -    case OE_SetNull:  zName = "SET NULL";            break;
 74761         -    case OE_SetDflt:  zName = "SET DEFAULT";         break;
 74762         -    case OE_Cascade:  zName = "CASCADE";             break;
 74763         -    default:          zName = "RESTRICT";  
 74764         -                      assert( action==OE_Restrict ); break;
        76466  +    case OE_SetNull:  zName = "SET NULL";        break;
        76467  +    case OE_SetDflt:  zName = "SET DEFAULT";     break;
        76468  +    case OE_Cascade:  zName = "CASCADE";         break;
        76469  +    case OE_Restrict: zName = "RESTRICT";        break;
        76470  +    default:          zName = "NO ACTION";  
        76471  +                      assert( action==OE_None ); break;
 74765  76472     }
 74766  76473     return zName;
 74767  76474   }
        76475  +#endif
 74768  76476   
 74769  76477   /*
 74770  76478   ** Process a pragma statement.  
 74771  76479   **
 74772  76480   ** Pragmas are of this form:
 74773  76481   **
 74774  76482   **      PRAGMA [database.]id [= value]
................................................................................
 75487  77195           sqlite3VdbeSetColName(v, 5, COLNAME_NAME, "on_update", SQLITE_STATIC);
 75488  77196           sqlite3VdbeSetColName(v, 6, COLNAME_NAME, "on_delete", SQLITE_STATIC);
 75489  77197           sqlite3VdbeSetColName(v, 7, COLNAME_NAME, "match", SQLITE_STATIC);
 75490  77198           while(pFK){
 75491  77199             int j;
 75492  77200             for(j=0; j<pFK->nCol; j++){
 75493  77201               char *zCol = pFK->aCol[j].zCol;
 75494         -            char *zOnUpdate = (char *)actionName(pFK->updateConf);
 75495         -            char *zOnDelete = (char *)actionName(pFK->deleteConf);
        77202  +            char *zOnDelete = (char *)actionName(pFK->aAction[0]);
        77203  +            char *zOnUpdate = (char *)actionName(pFK->aAction[1]);
 75496  77204               sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
 75497  77205               sqlite3VdbeAddOp2(v, OP_Integer, j, 2);
 75498  77206               sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, pFK->zTo, 0);
 75499  77207               sqlite3VdbeAddOp4(v, OP_String8, 0, 4, 0,
 75500  77208                                 pTab->aCol[pFK->aCol[j].iFrom].zName, 0);
 75501  77209               sqlite3VdbeAddOp4(v, zCol ? OP_String8 : OP_Null, 0, 5, 0, zCol, 0);
 75502  77210               sqlite3VdbeAddOp4(v, OP_String8, 0, 6, 0, zOnUpdate, 0);
................................................................................
 81308  83016   ** To state it another way:  This routine returns a list of all triggers
 81309  83017   ** that fire off of pTab.  The list will include any TEMP triggers on
 81310  83018   ** pTab as well as the triggers lised in pTab->pTrigger.
 81311  83019   */
 81312  83020   SQLITE_PRIVATE Trigger *sqlite3TriggerList(Parse *pParse, Table *pTab){
 81313  83021     Schema * const pTmpSchema = pParse->db->aDb[1].pSchema;
 81314  83022     Trigger *pList = 0;                  /* List of triggers to return */
        83023  +
        83024  +  if( pParse->disableTriggers ){
        83025  +    return 0;
        83026  +  }
 81315  83027   
 81316  83028     if( pTmpSchema!=pTab->pSchema ){
 81317  83029       HashElem *p;
 81318  83030       for(p=sqliteHashFirst(&pTmpSchema->trigHash); p; p=sqliteHashNext(p)){
 81319  83031         Trigger *pTrig = (Trigger *)sqliteHashData(p);
 81320  83032         if( pTrig->pTabSchema==pTab->pSchema
 81321  83033          && 0==sqlite3StrICmp(pTrig->table, pTab->zName) 
................................................................................
 82054  83766     Expr *pWhen = 0;            /* Duplicate of trigger WHEN expression */
 82055  83767     Vdbe *v;                    /* Temporary VM */
 82056  83768     NameContext sNC;            /* Name context for sub-vdbe */
 82057  83769     SubProgram *pProgram = 0;   /* Sub-vdbe for trigger program */
 82058  83770     Parse *pSubParse;           /* Parse context for sub-vdbe */
 82059  83771     int iEndTrigger = 0;        /* Label to jump to if WHEN is false */
 82060  83772   
 82061         -  assert( pTab==tableOfTrigger(pTrigger) );
        83773  +  assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) );
 82062  83774   
 82063  83775     /* Allocate the TriggerPrg and SubProgram objects. To ensure that they
 82064  83776     ** are freed if an error occurs, link them into the Parse.pTriggerPrg 
 82065  83777     ** list of the top-level Parse object sooner rather than later.  */
 82066  83778     pPrg = sqlite3DbMallocZero(db, sizeof(TriggerPrg));
 82067  83779     if( !pPrg ) return 0;
 82068  83780     pPrg->pNext = pTop->pTriggerPrg;
 82069  83781     pTop->pTriggerPrg = pPrg;
 82070  83782     pPrg->pProgram = pProgram = sqlite3DbMallocZero(db, sizeof(SubProgram));
 82071  83783     if( !pProgram ) return 0;
 82072  83784     pProgram->nRef = 1;
 82073  83785     pPrg->pTrigger = pTrigger;
 82074  83786     pPrg->orconf = orconf;
        83787  +  pPrg->oldmask = 0xffffffff;
 82075  83788   
 82076  83789     /* Allocate and populate a new Parse context to use for coding the 
 82077  83790     ** trigger sub-program.  */
 82078  83791     pSubParse = sqlite3StackAllocZero(db, sizeof(Parse));
 82079  83792     if( !pSubParse ) return 0;
 82080  83793     memset(&sNC, 0, sizeof(sNC));
 82081  83794     sNC.pParse = pSubParse;
................................................................................
 82154  83867     Trigger *pTrigger,   /* Trigger to code */
 82155  83868     Table *pTab,         /* The table trigger pTrigger is attached to */
 82156  83869     int orconf           /* ON CONFLICT algorithm. */
 82157  83870   ){
 82158  83871     Parse *pRoot = sqlite3ParseToplevel(pParse);
 82159  83872     TriggerPrg *pPrg;
 82160  83873   
 82161         -  assert( pTab==tableOfTrigger(pTrigger) );
        83874  +  assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) );
 82162  83875   
 82163  83876     /* It may be that this trigger has already been coded (or is in the
 82164  83877     ** process of being coded). If this is the case, then an entry with
 82165  83878     ** a matching TriggerPrg.pTrigger field will be present somewhere
 82166  83879     ** in the Parse.pTriggerPrg list. Search for such an entry.  */
 82167  83880     for(pPrg=pRoot->pTriggerPrg; 
 82168  83881         pPrg && (pPrg->pTrigger!=pTrigger || pPrg->orconf!=orconf); 
................................................................................
 82174  83887       pPrg = codeRowTrigger(pParse, pTrigger, pTab, orconf);
 82175  83888     }
 82176  83889   
 82177  83890     return pPrg;
 82178  83891   }
 82179  83892   
 82180  83893   /*
 82181         -** This is called to code FOR EACH ROW triggers.
 82182         -**
 82183         -** When the code that this function generates is executed, the following 
 82184         -** must be true:
 82185         -**
 82186         -** 1. No cursors may be open in the main database.  (But newIdx and oldIdx
 82187         -**    can be indices of cursors in temporary tables.  See below.)
 82188         -**
 82189         -** 2. If the triggers being coded are ON INSERT or ON UPDATE triggers, then
 82190         -**    a temporary vdbe cursor (index newIdx) must be open and pointing at
 82191         -**    a row containing values to be substituted for new.* expressions in the
 82192         -**    trigger program(s).
 82193         -**
 82194         -** 3. If the triggers being coded are ON DELETE or ON UPDATE triggers, then
 82195         -**    a temporary vdbe cursor (index oldIdx) must be open and pointing at
 82196         -**    a row containing values to be substituted for old.* expressions in the
 82197         -**    trigger program(s).
 82198         -**
 82199         -** If they are not NULL, the piOldColMask and piNewColMask output variables
 82200         -** are set to values that describe the columns used by the trigger program
 82201         -** in the OLD.* and NEW.* tables respectively. If column N of the 
 82202         -** pseudo-table is read at least once, the corresponding bit of the output
 82203         -** mask is set. If a column with an index greater than 32 is read, the
 82204         -** output mask is set to the special value 0xffffffff.
 82205         -**
        83894  +** Generate code for the trigger program associated with trigger p on 
        83895  +** table pTab. The reg, orconf and ignoreJump parameters passed to this
        83896  +** function are the same as those described in the header function for
        83897  +** sqlite3CodeRowTrigger()
        83898  +*/
        83899  +SQLITE_PRIVATE void sqlite3CodeRowTriggerDirect(
        83900  +  Parse *pParse,       /* Parse context */
        83901  +  Trigger *p,          /* Trigger to code */
        83902  +  Table *pTab,         /* The table to code triggers from */
        83903  +  int reg,             /* Reg array containing OLD.* and NEW.* values */
        83904  +  int orconf,          /* ON CONFLICT policy */
        83905  +  int ignoreJump       /* Instruction to jump to for RAISE(IGNORE) */
        83906  +){
        83907  +  Vdbe *v = sqlite3GetVdbe(pParse); /* Main VM */
        83908  +  TriggerPrg *pPrg;
        83909  +  pPrg = getRowTrigger(pParse, p, pTab, orconf);
        83910  +  assert( pPrg || pParse->nErr || pParse->db->mallocFailed );
        83911  +
        83912  +  /* Code the OP_Program opcode in the parent VDBE. P4 of the OP_Program 
        83913  +  ** is a pointer to the sub-vdbe containing the trigger program.  */
        83914  +  if( pPrg ){
        83915  +    sqlite3VdbeAddOp3(v, OP_Program, reg, ignoreJump, ++pParse->nMem);
        83916  +    pPrg->pProgram->nRef++;
        83917  +    sqlite3VdbeChangeP4(v, -1, (const char *)pPrg->pProgram, P4_SUBPROGRAM);
        83918  +    VdbeComment(
        83919  +        (v, "Call: %s.%s", (p->zName?p->zName:"fkey"), onErrorText(orconf)));
        83920  +
        83921  +    /* Set the P5 operand of the OP_Program instruction to non-zero if
        83922  +    ** recursive invocation of this trigger program is disallowed. Recursive
        83923  +    ** invocation is disallowed if (a) the sub-program is really a trigger,
        83924  +    ** not a foreign key action, and (b) the flag to enable recursive triggers
        83925  +    ** is clear.  */
        83926  +    sqlite3VdbeChangeP5(v, (u8)(p->zName && !(pParse->db->flags&SQLITE_RecTriggers)));
        83927  +  }
        83928  +}
        83929  +
        83930  +/*
        83931  +** This is called to code the required FOR EACH ROW triggers for an operation
        83932  +** on table pTab. The operation to code triggers for (INSERT, UPDATE or DELETE)
        83933  +** is given by the op paramater. The tr_tm parameter determines whether the
        83934  +** BEFORE or AFTER triggers are coded. If the operation is an UPDATE, then
        83935  +** parameter pChanges is passed the list of columns being modified.
        83936  +**
        83937  +** If there are no triggers that fire at the specified time for the specified
        83938  +** operation on pTab, this function is a no-op.
        83939  +**
        83940  +** The reg argument is the address of the first in an array of registers 
        83941  +** that contain the values substituted for the new.* and old.* references
        83942  +** in the trigger program. If N is the number of columns in table pTab
        83943  +** (a copy of pTab->nCol), then registers are populated as follows:
        83944  +**
        83945  +**   Register       Contains
        83946  +**   ------------------------------------------------------
        83947  +**   reg+0          OLD.rowid
        83948  +**   reg+1          OLD.* value of left-most column of pTab
        83949  +**   ...            ...
        83950  +**   reg+N          OLD.* value of right-most column of pTab
        83951  +**   reg+N+1        NEW.rowid
        83952  +**   reg+N+2        OLD.* value of left-most column of pTab
        83953  +**   ...            ...
        83954  +**   reg+N+N+1      NEW.* value of right-most column of pTab
        83955  +**
        83956  +** For ON DELETE triggers, the registers containing the NEW.* values will
        83957  +** never be accessed by the trigger program, so they are not allocated or 
        83958  +** populated by the caller (there is no data to populate them with anyway). 
        83959  +** Similarly, for ON INSERT triggers the values stored in the OLD.* registers
        83960  +** are never accessed, and so are not allocated by the caller. So, for an
        83961  +** ON INSERT trigger, the value passed to this function as parameter reg
        83962  +** is not a readable register, although registers (reg+N) through 
        83963  +** (reg+N+N+1) are.
        83964  +**
        83965  +** Parameter orconf is the default conflict resolution algorithm for the
        83966  +** trigger program to use (REPLACE, IGNORE etc.). Parameter ignoreJump
        83967  +** is the instruction that control should jump to if a trigger program
        83968  +** raises an IGNORE exception.
 82206  83969   */
 82207  83970   SQLITE_PRIVATE void sqlite3CodeRowTrigger(
 82208  83971     Parse *pParse,       /* Parse context */
 82209  83972     Trigger *pTrigger,   /* List of triggers on table pTab */
 82210  83973     int op,              /* One of TK_UPDATE, TK_INSERT, TK_DELETE */
 82211  83974     ExprList *pChanges,  /* Changes list for any UPDATE OF triggers */
 82212  83975     int tr_tm,           /* One of TRIGGER_BEFORE, TRIGGER_AFTER */
 82213  83976     Table *pTab,         /* The table to code triggers from */
 82214         -  int newIdx,          /* The indice of the "new" row to access */
 82215         -  int oldIdx,          /* The indice of the "old" row to access */
        83977  +  int reg,             /* The first in an array of registers (see above) */
 82216  83978     int orconf,          /* ON CONFLICT policy */
 82217  83979     int ignoreJump       /* Instruction to jump to for RAISE(IGNORE) */
 82218  83980   ){
 82219         -  Trigger *p;
        83981  +  Trigger *p;          /* Used to iterate through pTrigger list */
 82220  83982   
 82221         -  UNUSED_PARAMETER(newIdx);
 82222         -
 82223         -  assert(op == TK_UPDATE || op == TK_INSERT || op == TK_DELETE);
 82224         -  assert(tr_tm == TRIGGER_BEFORE || tr_tm == TRIGGER_AFTER );
        83983  +  assert( op==TK_UPDATE || op==TK_INSERT || op==TK_DELETE );
        83984  +  assert( tr_tm==TRIGGER_BEFORE || tr_tm==TRIGGER_AFTER );
        83985  +  assert( (op==TK_UPDATE)==(pChanges!=0) );
 82225  83986   
 82226  83987     for(p=pTrigger; p; p=p->pNext){
 82227  83988   
 82228  83989       /* Sanity checking:  The schema for the trigger and for the table are
 82229  83990       ** always defined.  The trigger must be in the same schema as the table
 82230  83991       ** or else it must be a TEMP trigger. */
 82231  83992       assert( p->pSchema!=0 );
................................................................................
 82232  83993       assert( p->pTabSchema!=0 );
 82233  83994       assert( p->pSchema==p->pTabSchema 
 82234  83995            || p->pSchema==pParse->db->aDb[1].pSchema );
 82235  83996   
 82236  83997       /* Determine whether we should code this trigger */
 82237  83998       if( p->op==op 
 82238  83999        && p->tr_tm==tr_tm 
 82239         -     && checkColumnOverlap(p->pColumns,pChanges)
        84000  +     && checkColumnOverlap(p->pColumns, pChanges)
 82240  84001       ){
 82241         -      Vdbe *v = sqlite3GetVdbe(pParse); /* Main VM */
 82242         -      TriggerPrg *pPrg;
 82243         -      pPrg = getRowTrigger(pParse, p, pTab, orconf);
 82244         -      assert( pPrg || pParse->nErr || pParse->db->mallocFailed );
 82245         -
 82246         -      /* Code the OP_Program opcode in the parent VDBE. P4 of the OP_Program 
 82247         -      ** is a pointer to the sub-vdbe containing the trigger program.  */
 82248         -      if( pPrg ){
 82249         -        sqlite3VdbeAddOp3(v, OP_Program, oldIdx, ignoreJump, ++pParse->nMem);
 82250         -        pPrg->pProgram->nRef++;
 82251         -        sqlite3VdbeChangeP4(v, -1, (const char *)pPrg->pProgram, P4_SUBPROGRAM);
 82252         -        VdbeComment((v, "Call: %s.%s", p->zName, onErrorText(orconf)));
 82253         -      }
        84002  +      sqlite3CodeRowTriggerDirect(pParse, p, pTab, reg, orconf, ignoreJump);
 82254  84003       }
 82255  84004     }
 82256  84005   }
 82257  84006   
 82258  84007   /*
 82259  84008   ** Triggers fired by UPDATE or DELETE statements may access values stored
 82260  84009   ** in the old.* pseudo-table. This function returns a 32-bit bitmask
................................................................................
 82273  84022   ** by triggers. The caller must always assume that it is.
 82274  84023   **
 82275  84024   ** There is no equivalent function for new.* references.
 82276  84025   */
 82277  84026   SQLITE_PRIVATE u32 sqlite3TriggerOldmask(
 82278  84027     Parse *pParse,       /* Parse context */
 82279  84028     Trigger *pTrigger,   /* List of triggers on table pTab */
 82280         -  int op,              /* Either TK_UPDATE or TK_DELETE */
 82281  84029     ExprList *pChanges,  /* Changes list for any UPDATE OF triggers */
 82282  84030     Table *pTab,         /* The table to code triggers from */
 82283  84031     int orconf           /* Default ON CONFLICT policy for trigger steps */
 82284  84032   ){
        84033  +  const int op = pChanges ? TK_UPDATE : TK_DELETE;
 82285  84034     u32 mask = 0;
 82286  84035     Trigger *p;
 82287  84036   
 82288         -  assert(op==TK_UPDATE || op==TK_DELETE);
 82289  84037     for(p=pTrigger; p; p=p->pNext){
 82290  84038       if( p->op==op && checkColumnOverlap(p->pColumns,pChanges) ){
 82291  84039         TriggerPrg *pPrg;
 82292  84040         pPrg = getRowTrigger(pParse, p, pTab, orconf);
 82293  84041         if( pPrg ){
 82294  84042           mask |= pPrg->oldmask;
 82295  84043         }
................................................................................
 82415  84163     Expr *pRowidExpr = 0;  /* Expression defining the new record number */
 82416  84164     int openAll = 0;       /* True if all indices need to be opened */
 82417  84165     AuthContext sContext;  /* The authorization context */
 82418  84166     NameContext sNC;       /* The name-context to resolve expressions in */
 82419  84167     int iDb;               /* Database containing the table being updated */
 82420  84168     int j1;                /* Addresses of jump instructions */
 82421  84169     int okOnePass;         /* True for one-pass algorithm without the FIFO */
        84170  +  int hasFK;             /* True if foreign key processing is required */
 82422  84171   
 82423  84172   #ifndef SQLITE_OMIT_TRIGGER
 82424  84173     int isView;                  /* Trying to update a view */
 82425  84174     Trigger *pTrigger;           /* List of triggers on pTab, if required */
 82426  84175   #endif
 82427         -  u32 oldmask = 0;        /* Mask of OLD.* columns in use */
 82428  84176   
 82429  84177     /* Register Allocations */
 82430  84178     int regRowCount = 0;   /* A count of rows changed */
 82431  84179     int regOldRowid;       /* The old rowid */
 82432  84180     int regNewRowid;       /* The new rowid */
 82433  84181     int regNew;
 82434  84182     int regOld = 0;
................................................................................
 82527  84275           goto update_cleanup;
 82528  84276         }else if( rc==SQLITE_IGNORE ){
 82529  84277           aXRef[j] = -1;
 82530  84278         }
 82531  84279       }
 82532  84280   #endif
 82533  84281     }
        84282  +
        84283  +  hasFK = sqlite3FkRequired(pParse, pTab, aXRef, chngRowid);
 82534  84284   
 82535  84285     /* Allocate memory for the array aRegIdx[].  There is one entry in the
 82536  84286     ** array for each index associated with table being updated.  Fill in
 82537  84287     ** the value with a register number for indices that are to be used
 82538  84288     ** and with zero for unused indices.
 82539  84289     */
 82540  84290     for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){}
................................................................................
 82573  84323       pTabList = 0;
 82574  84324       goto update_cleanup;
 82575  84325     }
 82576  84326   #endif
 82577  84327   
 82578  84328     /* Allocate required registers. */
 82579  84329     regOldRowid = regNewRowid = ++pParse->nMem;
 82580         -  if( pTrigger ){
        84330  +  if( pTrigger || hasFK ){
 82581  84331       regOld = pParse->nMem + 1;
 82582  84332       pParse->nMem += pTab->nCol;
 82583  84333     }
 82584         -  if( chngRowid || pTrigger ){
        84334  +  if( chngRowid || pTrigger || hasFK ){
 82585  84335       regNewRowid = ++pParse->nMem;
 82586  84336     }
 82587  84337     regNew = pParse->nMem + 1;
 82588  84338     pParse->nMem += pTab->nCol;
 82589  84339     regRec = ++pParse->nMem;
 82590  84340   
 82591  84341     /* Start the view context. */
 82592  84342     if( isView ){
 82593  84343       sqlite3AuthContextPush(pParse, &sContext, pTab->zName);
 82594  84344     }
 82595  84345   
 82596         -  /* If there are any triggers, set oldmask and new_col_mask. */
 82597         -  oldmask = sqlite3TriggerOldmask(
 82598         -      pParse, pTrigger, TK_UPDATE, pChanges, pTab, onError);
 82599         -
 82600  84346     /* If we are trying to update a view, realize that view into
 82601  84347     ** a ephemeral table.
 82602  84348     */
 82603  84349   #if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
 82604  84350     if( isView ){
 82605  84351       sqlite3MaterializeView(pParse, pTab, pWhere, iCur);
 82606  84352     }
................................................................................
 82677  84423       addr = sqlite3VdbeAddOp3(v, OP_RowSetRead, regRowSet, 0, regOldRowid);
 82678  84424     }
 82679  84425   
 82680  84426     /* Make cursor iCur point to the record that is being updated. If
 82681  84427     ** this record does not exist for some reason (deleted by a trigger,
 82682  84428     ** for example, then jump to the next iteration of the RowSet loop.  */
 82683  84429     sqlite3VdbeAddOp3(v, OP_NotExists, iCur, addr, regOldRowid);
        84430  +
        84431  +  /* If the record number will change, set register regNewRowid to
        84432  +  ** contain the new value. If the record number is not being modified,
        84433  +  ** then regNewRowid is the same register as regOldRowid, which is
        84434  +  ** already populated.  */
        84435  +  assert( chngRowid || pTrigger || hasFK || regOldRowid==regNewRowid );
        84436  +  if( chngRowid ){
        84437  +    sqlite3ExprCode(pParse, pRowidExpr, regNewRowid);
        84438  +    sqlite3VdbeAddOp1(v, OP_MustBeInt, regNewRowid);
        84439  +  }
 82684  84440   
 82685  84441     /* If there are triggers on this table, populate an array of registers 
 82686  84442     ** with the required old.* column data.  */
 82687         -  if( pTrigger ){
        84443  +  if( hasFK || pTrigger ){
        84444  +    u32 oldmask = (hasFK ? sqlite3FkOldmask(pParse, pTab) : 0);
        84445  +    oldmask |= sqlite3TriggerOldmask(pParse, pTrigger, pChanges, pTab, onError);
 82688  84446       for(i=0; i<pTab->nCol; i++){
 82689  84447         if( aXRef[i]<0 || oldmask==0xffffffff || (oldmask & (1<<i)) ){
 82690  84448           sqlite3VdbeAddOp3(v, OP_Column, iCur, i, regOld+i);
 82691  84449           sqlite3ColumnDefault(v, pTab, i, regOld+i);
 82692  84450         }else{
 82693  84451           sqlite3VdbeAddOp2(v, OP_Null, 0, regOld+i);
 82694  84452         }
 82695  84453       }
 82696         -  }
 82697         -
 82698         -  /* If the record number will change, set register regNewRowid to
 82699         -  ** contain the new value. If the record number is not being modified,
 82700         -  ** then regNewRowid is the same register as regOldRowid, which is
 82701         -  ** already populated.  */
 82702         -  assert( chngRowid || pTrigger || regOldRowid==regNewRowid );
 82703         -  if( chngRowid ){
 82704         -    sqlite3ExprCode(pParse, pRowidExpr, regNewRowid);
 82705         -    sqlite3VdbeAddOp1(v, OP_MustBeInt, regNewRowid);
 82706         -  }else if( pTrigger ){
 82707         -    sqlite3VdbeAddOp2(v, OP_Copy, regOldRowid, regNewRowid);
        84454  +    if( chngRowid==0 ){
        84455  +      sqlite3VdbeAddOp2(v, OP_Copy, regOldRowid, regNewRowid);
        84456  +    }
 82708  84457     }
 82709  84458   
 82710  84459     /* Populate the array of registers beginning at regNew with the new
 82711  84460     ** row data. This array is used to check constaints, create the new
 82712  84461     ** table and index records, and as the values for any new.* references
 82713  84462     ** made by triggers.  */
 82714  84463     for(i=0; i<pTab->nCol; i++){
................................................................................
 82727  84476   
 82728  84477     /* Fire any BEFORE UPDATE triggers. This happens before constraints are
 82729  84478     ** verified. One could argue that this is wrong.  */
 82730  84479     if( pTrigger ){
 82731  84480       sqlite3VdbeAddOp2(v, OP_Affinity, regNew, pTab->nCol);
 82732  84481       sqlite3TableAffinityStr(v, pTab);
 82733  84482       sqlite3CodeRowTrigger(pParse, pTrigger, TK_UPDATE, pChanges, 
 82734         -        TRIGGER_BEFORE, pTab, -1, regOldRowid, onError, addr);
        84483  +        TRIGGER_BEFORE, pTab, regOldRowid, onError, addr);
 82735  84484   
 82736  84485       /* The row-trigger may have deleted the row being updated. In this
 82737  84486       ** case, jump to the next row. No updates or AFTER triggers are 
 82738  84487       ** required. This behaviour - what happens when the row being updated
 82739  84488       ** is deleted or renamed by a BEFORE trigger - is left undefined in the
 82740  84489       ** documentation.  */
 82741  84490       sqlite3VdbeAddOp3(v, OP_NotExists, iCur, addr, regOldRowid);
................................................................................
 82742  84491     }
 82743  84492   
 82744  84493     if( !isView ){
 82745  84494   
 82746  84495       /* Do constraint checks. */
 82747  84496       sqlite3GenerateConstraintChecks(pParse, pTab, iCur, regNewRowid,
 82748  84497           aRegIdx, (chngRowid?regOldRowid:0), 1, onError, addr, 0);
        84498  +
        84499  +    /* Do FK constraint checks. */
        84500  +    if( hasFK ){
        84501  +      sqlite3FkCheck(pParse, pTab, regOldRowid, 0);
        84502  +    }
 82749  84503   
 82750  84504       /* Delete the index entries associated with the current record.  */
 82751  84505       j1 = sqlite3VdbeAddOp3(v, OP_NotExists, iCur, 0, regOldRowid);
 82752  84506       sqlite3GenerateRowIndexDelete(pParse, pTab, iCur, aRegIdx);
 82753  84507     
 82754  84508       /* If changing the record number, delete the old record.  */
 82755         -    if( chngRowid ){
        84509  +    if( hasFK || chngRowid ){
 82756  84510         sqlite3VdbeAddOp2(v, OP_Delete, iCur, 0);
 82757  84511       }
 82758  84512       sqlite3VdbeJumpHere(v, j1);
        84513  +
        84514  +    if( hasFK ){
        84515  +      sqlite3FkCheck(pParse, pTab, 0, regNewRowid);
        84516  +    }
 82759  84517     
 82760  84518       /* Insert the new index entries and the new record. */
 82761  84519       sqlite3CompleteInsertion(pParse, pTab, iCur, regNewRowid, aRegIdx, 1, 0, 0);
        84520  +
        84521  +    /* Do any ON CASCADE, SET NULL or SET DEFAULT operations required to
        84522  +    ** handle rows (possibly in other tables) that refer via a foreign key
        84523  +    ** to the row just updated. */ 
        84524  +    if( hasFK ){
        84525  +      sqlite3FkActions(pParse, pTab, pChanges, regOldRowid);
        84526  +    }
 82762  84527     }
 82763  84528   
 82764  84529     /* Increment the row counter 
 82765  84530     */
 82766  84531     if( (db->flags & SQLITE_CountRows) && !pParse->pTriggerTab){
 82767  84532       sqlite3VdbeAddOp2(v, OP_AddImm, regRowCount, 1);
 82768  84533     }
 82769  84534   
 82770  84535     sqlite3CodeRowTrigger(pParse, pTrigger, TK_UPDATE, pChanges, 
 82771         -      TRIGGER_AFTER, pTab, -1, regOldRowid, onError, addr);
        84536  +      TRIGGER_AFTER, pTab, regOldRowid, onError, addr);
 82772  84537   
 82773  84538     /* Repeat the above with the next record to be updated, until
 82774  84539     ** all record selected by the WHERE clause have been updated.
 82775  84540     */
 82776  84541     sqlite3VdbeAddOp2(v, OP_Goto, 0, addr);
 82777  84542     sqlite3VdbeJumpHere(v, addr);
 82778  84543   
................................................................................
 82808  84573     sqlite3DbFree(db, aRegIdx);
 82809  84574     sqlite3DbFree(db, aXRef);
 82810  84575     sqlite3SrcListDelete(db, pTabList);
 82811  84576     sqlite3ExprListDelete(db, pChanges);
 82812  84577     sqlite3ExprDelete(db, pWhere);
 82813  84578     return;
 82814  84579   }
        84580  +/* Make sure "isView" and other macros defined above are undefined. Otherwise
        84581  +** thely may interfere with compilation of other functions in this file
        84582  +** (or in another file, if this file becomes part of the amalgamation).  */
        84583  +#ifdef isView
        84584  + #undef isView
        84585  +#endif
        84586  +#ifdef pTrigger
        84587  + #undef pTrigger
        84588  +#endif
 82815  84589   
 82816  84590   #ifndef SQLITE_OMIT_VIRTUALTABLE
 82817  84591   /*
 82818  84592   ** Generate code for an UPDATE of a virtual table.
 82819  84593   **
 82820  84594   ** The strategy is that we create an ephemerial table that contains
 82821  84595   ** for each row to be changed:
................................................................................
 82903  84677     sqlite3VdbeAddOp2(v, OP_Close, ephemTab, 0);
 82904  84678   
 82905  84679     /* Cleanup */
 82906  84680     sqlite3SelectDelete(db, pSelect);  
 82907  84681   }
 82908  84682   #endif /* SQLITE_OMIT_VIRTUALTABLE */
 82909  84683   
 82910         -/* Make sure "isView" gets undefined in case this file becomes part of
 82911         -** the amalgamation - so that subsequent files do not see isView as a
 82912         -** macro. */
 82913         -#undef isView
 82914         -
 82915  84684   /************** End of update.c **********************************************/
 82916  84685   /************** Begin file vacuum.c ******************************************/
 82917  84686   /*
 82918  84687   ** 2003 April 6
 82919  84688   **
 82920  84689   ** The author disclaims copyright to this source code.  In place of
 82921  84690   ** a legal notice, here is a blessing:
................................................................................
 83007  84776     int nRes;
 83008  84777   
 83009  84778     if( !db->autoCommit ){
 83010  84779       sqlite3SetString(pzErrMsg, db, "cannot VACUUM from within a transaction");
 83011  84780       return SQLITE_ERROR;
 83012  84781     }
 83013  84782   
 83014         -  /* Save the current value of the write-schema flag before setting it. */
        84783  +  /* Save the current value of the database flags so that it can be 
        84784  +  ** restored before returning. Then set the writable-schema flag, and
        84785  +  ** disable CHECK and foreign key constraints.  */
 83015  84786     saved_flags = db->flags;
 83016  84787     saved_nChange = db->nChange;
 83017  84788     saved_nTotalChange = db->nTotalChange;
 83018  84789     db->flags |= SQLITE_WriteSchema | SQLITE_IgnoreChecks;
        84790  +  db->flags &= ~SQLITE_ForeignKeys;
 83019  84791   
 83020  84792     pMain = db->aDb[0].pBt;
 83021  84793     isMemDb = sqlite3PagerIsMemdb(sqlite3BtreePager(pMain));
 83022  84794   
 83023  84795     /* Attach the temporary database as 'vacuum_db'. The synchronous pragma
 83024  84796     ** can be set to 'off' for this file, as it is not recovered if a crash
 83025  84797     ** occurs anyway. The integrity of the database is maintained by a
................................................................................
 86131  87903         n = sqlite3ValueBytes(pVal, pColl->enc);
 86132  87904   
 86133  87905         for(i=0; i<SQLITE_INDEX_SAMPLES; i++){
 86134  87906           int r;
 86135  87907           int eSampletype = aSample[i].eType;
 86136  87908           if( eSampletype==SQLITE_NULL || eSampletype<eType ) continue;
 86137  87909           if( (eSampletype!=eType) ) break;
 86138         -        if( pColl->enc==SQLITE_UTF8 ){
 86139         -          r = pColl->xCmp(pColl->pUser, aSample[i].nByte, aSample[i].u.z, n, z);
 86140         -        }else{
        87910  +#ifndef SQLITE_OMIT_UTF16
        87911  +        if( pColl->enc!=SQLITE_UTF8 ){
 86141  87912             int nSample;
 86142  87913             char *zSample = sqlite3Utf8to16(
 86143  87914                 db, pColl->enc, aSample[i].u.z, aSample[i].nByte, &nSample
 86144  87915             );
 86145  87916             if( !zSample ){
 86146  87917               assert( db->mallocFailed );
 86147  87918               return SQLITE_NOMEM;
 86148  87919             }
 86149  87920             r = pColl->xCmp(pColl->pUser, nSample, zSample, n, z);
 86150  87921             sqlite3DbFree(db, zSample);
        87922  +        }else
        87923  +#endif
        87924  +        {
        87925  +          r = pColl->xCmp(pColl->pUser, aSample[i].nByte, aSample[i].u.z, n, z);
 86151  87926           }
 86152  87927           if( r>0 ) break;
 86153  87928         }
 86154  87929       }
 86155  87930   
 86156  87931       assert( i>=0 && i<=SQLITE_INDEX_SAMPLES );
 86157  87932       *piRegion = i;
................................................................................
 88296  90071   **    YYNRULE            the number of rules in the grammar
 88297  90072   **    YYERRORSYMBOL      is the code number of the error symbol.  If not
 88298  90073   **                       defined, then do no error processing.
 88299  90074   */
 88300  90075   #define YYCODETYPE unsigned char
 88301  90076   #define YYNOCODE 241
 88302  90077   #define YYACTIONTYPE unsigned short int
 88303         -#define YYWILDCARD 65
        90078  +#define YYWILDCARD 67
 88304  90079   #define sqlite3ParserTOKENTYPE Token
 88305  90080   typedef union {
 88306  90081     int yyinit;
 88307  90082     sqlite3ParserTOKENTYPE yy0;
 88308  90083     Expr* yy2;
 88309  90084     u8 yy18;
 88310  90085     ExprSpan yy22;
................................................................................
 88394  90169   **  yy_shift_ofst[]    For each state, the offset into yy_action for
 88395  90170   **                     shifting terminals.
 88396  90171   **  yy_reduce_ofst[]   For each state, the offset into yy_action for
 88397  90172   **                     shifting non-terminals after a reduce.
 88398  90173   **  yy_default[]       Default action for each state.
 88399  90174   */
 88400  90175   static const YYACTIONTYPE yy_action[] = {
 88401         - /*     0 */   277,  849,  157,  558,    2,  141,  194,  403,   21,   21,
 88402         - /*    10 */    21,   21,  146,   23,   23,   23,   23,   24,   24,   25,
 88403         - /*    20 */    25,   25,   26,  196,  377,  378,  192,  377,  378,  410,
 88404         - /*    30 */   416,   28,   23,   23,   23,   23,   24,   24,   25,   25,
 88405         - /*    40 */    25,   26,  196,   27,  447,   29,  125,   20,   19,  281,
 88406         - /*    50 */   420,  421,  417,  417,   22,   22,   21,   21,   21,   21,
 88407         - /*    60 */   400,   23,   23,   23,   23,   24,   24,   25,   25,   25,
 88408         - /*    70 */    26,  196,  277,  338,  284,  403,  432,  198,   42,   23,
 88409         - /*    80 */    23,   23,   23,   24,   24,   25,   25,   25,   26,  196,
 88410         - /*    90 */   490,  171,  380,  381,  147,  380,  381,  330,  333,  334,
 88411         - /*   100 */   284,  410,  416,  145,   27,   18,   29,  125,  335,   24,
 88412         - /*   110 */    24,   25,   25,   25,   26,  196,  141,  267,  403,   20,
 88413         - /*   120 */    19,  281,  420,  421,  417,  417,   22,   22,   21,   21,
 88414         - /*   130 */    21,   21,  145,   23,   23,   23,   23,   24,   24,   25,
 88415         - /*   140 */    25,   25,   26,  196,  277,  208,  434,  307,  180,  118,
 88416         - /*   150 */   459,  136,  123,  238,  328,  243,  329,  144,  279,  270,
 88417         - /*   160 */   301,  268,  147,  247,  247,  330,  333,  334,   55,  536,
 88418         - /*   170 */   173,  208,  403,  410,  416,  298,  335,  476,  123,  238,
 88419         - /*   180 */   328,  243,  329,  144,  399,   68,  403,  279,  436,  202,
 88420         - /*   190 */   247,   20,   19,  281,  420,  421,  417,  417,   22,   22,
 88421         - /*   200 */    21,   21,   21,   21,  400,   23,   23,   23,   23,   24,
 88422         - /*   210 */    24,   25,   25,   25,   26,  196,  277,  280,  477,  361,
 88423         - /*   220 */   264,  263,  405,  301,  403,  368,  315,  196,  294,  374,
 88424         - /*   230 */   194,  314,  394,  509,  193,  301,  116,  301,  298,  269,
 88425         - /*   240 */   403,  506,  191,  395,  194,  410,  416,  399,   68,  479,
 88426         - /*   250 */   298,  506,  298,  407,  407,  407,  367,  459,  396,  399,
 88427         - /*   260 */    69,  399,   75,   20,   19,  281,  420,  421,  417,  417,
 88428         - /*   270 */    22,   22,   21,   21,   21,   21,  400,   23,   23,   23,
 88429         - /*   280 */    23,   24,   24,   25,   25,   25,   26,  196,  277,  315,
 88430         - /*   290 */   537,  348,  403,    1,  325,  351,  468,  377,  378,  398,
 88431         - /*   300 */   366,  317,  201,  397,  301,  274,  301,  478,  434,  510,
 88432         - /*   310 */   180,  229,  463,   27,  172,   29,  125,  410,  416,  298,
 88433         - /*   320 */   194,  298,   25,   25,   25,   26,  196,  379,  399,   62,
 88434         - /*   330 */   399,   75,  360,   37,  536,   20,   19,  281,  420,  421,
 88435         - /*   340 */   417,  417,   22,   22,   21,   21,   21,   21,  400,   23,
 88436         - /*   350 */    23,   23,   23,   24,   24,   25,   25,   25,   26,  196,
 88437         - /*   360 */   277,  511,  246,  239,  472,  380,  381,  301,  519,  377,
 88438         - /*   370 */   378,  200,  393,   54,  240,  283,  379,  379,  301,  282,
 88439         - /*   380 */   427,  428,  298,   57,  361,  264,  263,  377,  378,  410,
 88440         - /*   390 */   416,  399,   68,  298,  512,  194,  170,  520,  531,  287,
 88441         - /*   400 */   372,  369,  399,   83,   14,   40,  164,   20,   19,  281,
 88442         - /*   410 */   420,  421,  417,  417,   22,   22,   21,   21,   21,   21,
 88443         - /*   420 */   223,   23,   23,   23,   23,   24,   24,   25,   25,   25,
 88444         - /*   430 */    26,  196,  277,  319,  511,  301,  475,  380,  381,  323,
 88445         - /*   440 */   454,  197,  222,  377,  378,  521,   54,  322,  292,  379,
 88446         - /*   450 */   298,  471,  312,  377,  378,  380,  381,  371,    2,  399,
 88447         - /*   460 */    83,  410,  416,  455,  147,   26,  196,  330,  333,  334,
 88448         - /*   470 */   306,  427,  428,  559,  372,  369,  441,  533,  335,   20,
 88449         - /*   480 */    19,  281,  420,  421,  417,  417,   22,   22,   21,   21,
 88450         - /*   490 */    21,   21,  442,   23,   23,   23,   23,   24,   24,   25,
 88451         - /*   500 */    25,   25,   26,  196,  277,  293,  453,  443,  492,  483,
 88452         - /*   510 */   126,  380,  381,  470,  324,  804,  518,  493,  160,  484,
 88453         - /*   520 */   454,  380,  381,  517,    4,  424,  463,  411,  412,  298,
 88454         - /*   530 */   189,  463,  197,  410,  416,  454,  355,  456,  399,    7,
 88455         - /*   540 */   304,  379,  423,  423,  354,  176,  379,  216,  414,  415,
 88456         - /*   550 */   190,   20,   19,  281,  420,  421,  417,  417,   22,   22,
 88457         - /*   560 */    21,   21,   21,   21,  400,   23,   23,   23,   23,   24,
 88458         - /*   570 */    24,   25,   25,   25,   26,  196,  277,  413,  301,  400,
 88459         - /*   580 */   250,  246,  433,  301,  400,  353,  301,  495,  359,  346,
 88460         - /*   590 */   126,  177,  128,  298,  291,  437,  379,  301,  298,  159,
 88461         - /*   600 */   379,  298,  399,   63,   53,  410,  416,  399,   58,  362,
 88462         - /*   610 */   399,   66,  298,  500,  304,  454,  423,  423,  209,  481,
 88463         - /*   620 */   482,  399,   85,   20,   19,  281,  420,  421,  417,  417,
 88464         - /*   630 */    22,   22,   21,   21,   21,   21,  301,   23,   23,   23,
 88465         - /*   640 */    23,   24,   24,   25,   25,   25,   26,  196,  277,  301,
 88466         - /*   650 */   449,  298,  246,  382,  383,  384,  301,  246,  498,  450,
 88467         - /*   660 */   399,   84,  534,  482,  298,  181,  128,  379,  301,  303,
 88468         - /*   670 */   353,  298,  379,  399,   89,  210,  188,  410,  416,  499,
 88469         - /*   680 */   399,   87,  342,  298,  304,  379,  423,  423,  304,  288,
 88470         - /*   690 */   423,  423,  399,   92,  289,   20,   19,  281,  420,  421,
 88471         - /*   700 */   417,  417,   22,   22,   21,   21,   21,   21,  301,   23,
 88472         - /*   710 */    23,   23,   23,   24,   24,   25,   25,   25,   26,  196,
 88473         - /*   720 */   277,  301,  258,  298,  441,  459,  246,  301,  195,  301,
 88474         - /*   730 */   156,  286,  399,   93,  501,   15,  298,  379,  430,  430,
 88475         - /*   740 */   442,  379,  298,  353,  298,  399,  117,  358,  418,  410,
 88476         - /*   750 */   416,  399,  119,  399,   52,  443,  391,  311,  379,  252,
 88477         - /*   760 */   265,  166,  379,  290,  234,  259,  224,   20,   30,  281,
 88478         - /*   770 */   420,  421,  417,  417,   22,   22,   21,   21,   21,   21,
 88479         - /*   780 */   301,   23,   23,   23,   23,   24,   24,   25,   25,   25,
 88480         - /*   790 */    26,  196,  277,  301,  404,  298,  258,  230,  529,  326,
 88481         - /*   800 */   301,  256,  528,  254,  399,   90,  309,  304,  298,  423,
 88482         - /*   810 */   423,  379,  379,  301,  211,  298,  347,  399,   67,  358,
 88483         - /*   820 */   127,  410,  416,  429,  399,   88,  398,   36,  298,   38,
 88484         - /*   830 */   397,   44,  459,  212,  379,  379,  174,  399,   59,  548,
 88485         - /*   840 */    19,  281,  420,  421,  417,  417,   22,   22,   21,   21,
 88486         - /*   850 */    21,   21,  301,   23,   23,   23,   23,   24,   24,   25,
 88487         - /*   860 */    25,   25,   26,  196,  277,  301,  320,  298,  258,  258,
 88488         - /*   870 */   400,  236,  301,  527,  345,  527,  399,   86,  452,  493,
 88489         - /*   880 */   298,  379,  448,  379,  379,  301,  215,  298,  431,  399,
 88490         - /*   890 */   120,  451,  231,  410,  416,  232,  399,  121,  497,  496,
 88491         - /*   900 */   298,  182,  183,  184,  189,  487,  488,  379,  241,  399,
 88492         - /*   910 */   122,  550,  551,  281,  420,  421,  417,  417,   22,   22,
 88493         - /*   920 */    21,   21,   21,   21,  503,   23,   23,   23,   23,   24,
 88494         - /*   930 */    24,   25,   25,   25,   26,  196,   32,  308,  400,    3,
 88495         - /*   940 */   301,  258,  301,  297,  378,  185,  302,  425,  186,   56,
 88496         - /*   950 */   504,   32,  308,  305,    3,  298,  379,  298,  297,  378,
 88497         - /*   960 */   258,  379,  359,  258,  399,   60,  399,   70,  305,  507,
 88498         - /*   970 */   301,  310,  167,  301,  505,  379,  332,  258,  379,  400,
 88499         - /*   980 */   356,  436,  400,  352,  552,  298,  310,  266,  298,  301,
 88500         - /*   990 */   376,  461,  379,  142,  399,   71,  436,  399,   61,  542,
 88501         - /*  1000 */   554,   35,   34,  213,  298,  379,  214,  301,  388,  389,
 88502         - /*  1010 */    33,  299,  300,  399,   72,  405,   35,   34,  469,  502,
 88503         - /*  1020 */   273,  146,  298,  379,  379,   33,  299,  300,  271,  390,
 88504         - /*  1030 */   405,  399,   73,  194,  365,   32,  308,  375,    3,  124,
 88505         - /*  1040 */   387,  406,  297,  378,  379,  313,  407,  407,  407,  408,
 88506         - /*  1050 */   409,    8,  305,   16,  379,  132,  379,  377,  378,  385,
 88507         - /*  1060 */   379,  407,  407,  407,  408,  409,    8,  178,  301,  445,
 88508         - /*  1070 */   310,  458,  217,  218,  219,  134,  221,  386,  295,  547,
 88509         - /*  1080 */   436,  301,  462,  298,  379,  133,  379,  464,  301,  244,
 88510         - /*  1090 */     6,  135,  399,   74,  296,  436,  298,  379,  401,   44,
 88511         - /*  1100 */    35,   34,  379,  298,  379,  399,   76,  301,  494,   33,
 88512         - /*  1110 */   299,  300,  399,   77,  405,  168,  169,  536,  318,  301,
 88513         - /*  1120 */   245,   43,  298,  379,  513,  477,  251,  253,  249,  405,
 88514         - /*  1130 */   146,  399,   78,  226,  298,  379,  316,  255,  257,  379,
 88515         - /*  1140 */   225,  379,  379,  399,   64,  407,  407,  407,  408,  409,
 88516         - /*  1150 */     8,  301,  379,  379,  227,  262,  301,  350,  228,  301,
 88517         - /*  1160 */   407,  407,  407,  337,  402,  146,  298,  364,  272,  263,
 88518         - /*  1170 */   379,  298,  379,  278,  298,  399,   65,  532,  194,  146,
 88519         - /*  1180 */   399,   79,  301,  399,   80,  301,   17,  539,  540,  543,
 88520         - /*  1190 */   357,  142,  544,  555,   51,   51,  140,  298,   39,  199,
 88521         - /*  1200 */   298,  446,  379,  379,   45,  379,  399,   81,  158,  399,
 88522         - /*  1210 */    82,  321,  233,  327,  460,  466,  490,  480,  235,  485,
 88523         - /*  1220 */   237,  285,  467,  486,  242,   95,  491,  489,  206,  205,
 88524         - /*  1230 */   207,  275,  514,  515,  516,  339,  149,  508,  522,  150,
 88525         - /*  1240 */   151,  276,  341,  187,  524,  343,   48,  152,  131,  349,
 88526         - /*  1250 */   260,  106,  162,  525,  535,  103,  363,   91,  204,  108,
 88527         - /*  1260 */   109,  110,  111,  545,  113,  161,  373,  220,  392,   99,
 88528         - /*  1270 */   553,  179,  419,  600,  601,  602,  137,  138,  422,   31,
 88529         - /*  1280 */   438,  426,  435,  163,  175,  139,  439,  440,    5,  444,
 88530         - /*  1290 */    10,  457,   41,    9,   94,  129,  473,  474,  465,   46,
 88531         - /*  1300 */   203,  331,   96,   97,  143,  248,   47,   98,  148,  240,
 88532         - /*  1310 */   340,  165,  523,  130,  100,  142,  261,  153,  102,   13,
 88533         - /*  1320 */   344,  538,  154,  155,  541,  105,  336,  104,   11,   49,
 88534         - /*  1330 */   107,  546,  549,  112,  101,  114,  526,  530,   12,  115,
 88535         - /*  1340 */   370,  850,  557,  556,  850,  850,  850,  850,  850,  850,
 88536         - /*  1350 */   850,  850,  850,  850,  850,  850,  850,  850,  850,  850,
 88537         - /*  1360 */   850,  850,   50,
        90176  + /*     0 */   280,  849,  161,  558,    2,  145,  197,  405,   21,   21,
        90177  + /*    10 */    21,   21,  199,   23,   23,   23,   23,   24,   24,   25,
        90178  + /*    20 */    25,   25,   26,  199,  379,  380,   27,  447,   30,  129,
        90179  + /*    30 */   412,  418,   28,   23,   23,   23,   23,   24,   24,   25,
        90180  + /*    40 */    25,   25,   26,  199,   25,   25,   25,   26,  199,   20,
        90181  + /*    50 */    19,   29,  420,  421,  419,  419,   22,   22,   21,   21,
        90182  + /*    60 */    21,   21,  459,   23,   23,   23,   23,   24,   24,   25,
        90183  + /*    70 */    25,   25,   26,  199,  280,  405,   27,  285,   30,  129,
        90184  + /*    80 */    44,   23,   23,   23,   23,   24,   24,   25,   25,   25,
        90185  + /*    90 */    26,  199,  382,  383,  151,  374,  371,  332,  335,  336,
        90186  + /*   100 */   476,  468,  285,  519,  412,  418,  478,  337,  479,   18,
        90187  + /*   110 */    24,   24,   25,   25,   25,   26,  199,  398,   27,  512,
        90188  + /*   120 */    30,  129,  226,   20,   19,   29,  420,  421,  419,  419,
        90189  + /*   130 */    22,   22,   21,   21,   21,   21,  313,   23,   23,   23,
        90190  + /*   140 */    23,   24,   24,   25,   25,   25,   26,  199,  280,  303,
        90191  + /*   150 */   211,  432,  201,  122,  140,  127,  241,  329,  246,  330,
        90192  + /*   160 */   148,  397,  296,  376,  300,   14,  250,  168,  196,  506,
        90193  + /*   170 */   120,  303,  498,  401,   71,  211,  194,  233,  412,  418,
        90194  + /*   180 */   127,  241,  329,  246,  330,  148,  300,  331,  282,  379,
        90195  + /*   190 */   380,  250,  381,  453,  499,  401,   70,   20,   19,   29,
        90196  + /*   200 */   420,  421,  419,  419,   22,   22,   21,   21,   21,   21,
        90197  + /*   210 */   402,   23,   23,   23,   23,   24,   24,   25,   25,   25,
        90198  + /*   220 */    26,  199,  280,  303,  537,  232,    1,  495,  518,  493,
        90199  + /*   230 */   379,  380,  501,  176,  368,  517,  303,  316,  300,  175,
        90200  + /*   240 */   303,  151,  326,  288,  332,  335,  336,  401,   64,  405,
        90201  + /*   250 */   177,  300,  412,  418,  337,  300,  198,  382,  383,  219,
        90202  + /*   260 */   401,   77,  350,   15,  401,   70,  353,  402,   26,  199,
        90203  + /*   270 */   536,   20,   19,   29,  420,  421,  419,  419,   22,   22,
        90204  + /*   280 */    21,   21,   21,   21,  402,   23,   23,   23,   23,   24,
        90205  + /*   290 */    24,   25,   25,   25,   26,  199,  280,  511,  382,  383,
        90206  + /*   300 */   318,  204,  434,  308,  186,  303,  316,  449,  395,   56,
        90207  + /*   310 */   151,  315,  381,  332,  335,  336,  450,  405,  363,  267,
        90208  + /*   320 */   300,  379,  380,  337,  379,  380,  412,  418,  197,  401,
        90209  + /*   330 */    70,   59,  174,  520,    4,  379,  380,  187,  132,   38,
        90210  + /*   340 */   145,  266,  405,  249,  436,   20,   19,   29,  420,  421,
        90211  + /*   350 */   419,  419,   22,   22,   21,   21,   21,   21,  381,   23,
        90212  + /*   360 */    23,   23,   23,   24,   24,   25,   25,   25,   26,  199,
        90213  + /*   370 */   280,  320,  477,  511,  472,  164,  407,  454,  400,  131,
        90214  + /*   380 */   289,  225,  399,  433,  521,   56,  191,  303,  381,  382,
        90215  + /*   390 */   383,  303,  382,  383,  381,  379,  380,  536,  192,  344,
        90216  + /*   400 */   412,  418,  300,  382,  383,  270,  300,  409,  409,  409,
        90217  + /*   410 */   405,  401,   77,   41,  455,  401,   85,  456,  393,   20,
        90218  + /*   420 */    19,   29,  420,  421,  419,  419,   22,   22,   21,   21,
        90219  + /*   430 */    21,   21,  402,   23,   23,   23,   23,   24,   24,   25,
        90220  + /*   440 */    25,   25,   26,  199,  280,  363,  267,  130,  475,  273,
        90221  + /*   450 */   340,  271,  203,  459,  303,  197,  361,  470,  325,  481,
        90222  + /*   460 */   482,  294,  277,  382,  383,  434,  510,  186,  266,  300,
        90223  + /*   470 */   471,  454,   55,  300,  412,  418,  321,  364,  401,   85,
        90224  + /*   480 */   531,  149,  401,    7,  559,  374,  371,  170,  283,  427,
        90225  + /*   490 */   428,  381,  205,   20,   19,   29,  420,  421,  419,  419,
        90226  + /*   500 */    22,   22,   21,   21,   21,   21,  303,   23,   23,   23,
        90227  + /*   510 */    23,   24,   24,   25,   25,   25,   26,  199,  280,  303,
        90228  + /*   520 */   441,  300,  492,  424,  295,  459,  305,  272,  423,  423,
        90229  + /*   530 */   401,   65,  534,  482,  300,  287,  442,  324,  303,  250,
        90230  + /*   540 */   307,  427,  428,  401,   60,  323,  396,  509,  412,  418,
        90231  + /*   550 */   200,  261,  443,  300,  483,  506,  373,    2,  305,  533,
        90232  + /*   560 */   423,  423,  401,   68,  237,  484,  381,   20,   19,   29,
        90233  + /*   570 */   420,  421,  419,  419,   22,   22,   21,   21,   21,   21,
        90234  + /*   580 */   303,   23,   23,   23,   23,   24,   24,   25,   25,   25,
        90235  + /*   590 */    26,  199,  280,  303,  262,  300,  253,  804,  437,  459,
        90236  + /*   600 */   227,  303,  200,   57,  401,   87,  303,  405,  300,  378,
        90237  + /*   610 */   370,  305,  303,  423,  423,  163,  300,  401,   86,  242,
        90238  + /*   620 */   355,  300,  412,  418,  381,  401,   93,  300,  197,  243,
        90239  + /*   630 */   401,   89,  430,  430,  180,  381,  401,   96,  239,  406,
        90240  + /*   640 */   369,   20,   19,   29,  420,  421,  419,  419,   22,   22,
        90241  + /*   650 */    21,   21,   21,   21,  303,   23,   23,   23,   23,   24,
        90242  + /*   660 */    24,   25,   25,   25,   26,  199,  280,  303,  402,  300,
        90243  + /*   670 */   303,  384,  385,  386,  452,  405,  327,  150,  401,   97,
        90244  + /*   680 */   181,  132,  300,  303,  463,  300,  305,  303,  423,  423,
        90245  + /*   690 */   429,  401,  121,  454,  401,  123,  412,  418,  300,  381,
        90246  + /*   700 */   400,  197,  300,  305,  399,  423,  423,  401,   54,  431,
        90247  + /*   710 */   213,  401,   94,  362,  195,   20,   19,   29,  420,  421,
        90248  + /*   720 */   419,  419,   22,   22,   21,   21,   21,   21,  303,   23,
        90249  + /*   730 */    23,   23,   23,   24,   24,   25,   25,   25,   26,  199,
        90250  + /*   740 */   280,  303,  310,  300,  303,  448,  441,  284,  402,  193,
        90251  + /*   750 */   527,  347,  401,   90,  451,  490,  300,  303,  235,  300,
        90252  + /*   760 */   463,  303,  442,  130,  355,  401,   69,  171,  401,   91,
        90253  + /*   770 */   412,  418,  300,  527,  149,  381,  300,  244,  443,  381,
        90254  + /*   780 */   312,  401,   61,  402,  493,  401,   88,  454,  504,   20,
        90255  + /*   790 */    31,   29,  420,  421,  419,  419,   22,   22,   21,   21,
        90256  + /*   800 */    21,   21,  303,   23,   23,   23,   23,   24,   24,   25,
        90257  + /*   810 */    25,   25,   26,  199,  280,  249,  503,  300,  303,  487,
        90258  + /*   820 */   488,  357,  234,  348,  304,  505,  401,  124,  287,  356,
        90259  + /*   830 */   381,  303,  360,  300,  303,  463,  303,  381,  355,  381,
        90260  + /*   840 */   413,  414,  401,  125,  412,  418,  300,  381,  334,  300,
        90261  + /*   850 */   381,  300,  212,  381,  268,  401,  126,  507,  401,   62,
        90262  + /*   860 */   401,   72,  416,  417,   19,   29,  420,  421,  419,  419,
        90263  + /*   870 */    22,   22,   21,   21,   21,   21,  303,   23,   23,   23,
        90264  + /*   880 */    23,   24,   24,   25,   25,   25,   26,  199,  280,  415,
        90265  + /*   890 */   303,  300,  303,  529,  425,  178,   58,  528,  293,  214,
        90266  + /*   900 */   401,   73,   37,  542,   39,  300,  303,  300,  303,  390,
        90267  + /*   910 */   303,  349,  182,  183,  401,   63,  401,   74,  412,  418,
        90268  + /*   920 */   358,  300,  391,  300,  381,  300,  274,  255,  215,  402,
        90269  + /*   930 */   401,   75,  401,   92,  401,   76,  502,  381,  150,   29,
        90270  + /*   940 */   420,  421,  419,  419,   22,   22,   21,   21,   21,   21,
        90271  + /*   950 */   188,   23,   23,   23,   23,   24,   24,   25,   25,   25,
        90272  + /*   960 */    26,  199,   33,  309,  249,    3,  303,  377,  192,  299,
        90273  + /*   970 */   380,  259,  392,  257,  269,   33,  309,  367,    3,  381,
        90274  + /*   980 */   306,  300,  299,  380,  402,  554,  461,  381,  146,  303,
        90275  + /*   990 */   401,   78,  303,  306,  189,  303,  261,  497,  496,  311,
        90276  + /*  1000 */   303,  290,  402,  360,  300,  136,  387,  300,  303,  436,
        90277  + /*  1010 */   300,  381,  311,  401,   79,  300,  401,   80,  381,  401,
        90278  + /*  1020 */    66,  388,  436,  300,  401,   67,  361,  297,  402,   36,
        90279  + /*  1030 */    35,  303,  401,   81,  137,  389,   34,  301,  302,  548,
        90280  + /*  1040 */   139,  407,   36,   35,  469,  298,  300,  354,    6,   34,
        90281  + /*  1050 */   301,  302,   46,  403,  407,  401,   82,  319,  197,  160,
        90282  + /*  1060 */   229,   33,  309,  249,    3,  228,  249,  303,  299,  380,
        90283  + /*  1070 */   218,  230,  409,  409,  409,  410,  411,    8,  381,  306,
        90284  + /*  1080 */   303,  381,  300,   45,  261,  409,  409,  409,  410,  411,
        90285  + /*  1090 */     8,  401,   83,  317,  231,  300,  261,  128,  311,  381,
        90286  + /*  1100 */   291,  261,  261,  292,  401,   84,  408,  261,  436,  261,
        90287  + /*  1110 */   314,  381,  381,   17,   40,  445,  381,  381,  458,  462,
        90288  + /*  1120 */   202,  381,  381,  404,  381,  381,  144,  550,   36,   35,
        90289  + /*  1130 */   381,  464,  247,  381,  381,   34,  301,  302,  494,  551,
        90290  + /*  1140 */   407,  252,  248,  150,  552,  216,  381,  381,   43,  309,
        90291  + /*  1150 */   217,    3,  276,  381,   47,  299,  380,  381,  339,   46,
        90292  + /*  1160 */   150,  446,  513,  254,  256,  258,  306,  260,  265,  352,
        90293  + /*  1170 */   328,  409,  409,  409,  410,  411,    8,  381,  381,  381,
        90294  + /*  1180 */   381,  184,  381,  381,  381,  311,  220,  221,  222,  138,
        90295  + /*  1190 */   224,  539,  540,  547,  162,  436,  359,  532,  543,  150,
        90296  + /*  1200 */   146,  544,  322,   53,   16,  236,  381,  381,  379,  380,
        90297  + /*  1210 */   555,  381,   53,  460,  466,   36,   35,  467,  238,  240,
        90298  + /*  1220 */   480,  485,   34,  301,  302,  245,  286,  407,  490,  489,
        90299  + /*  1230 */   491,  486,   99,  536,  278,  209,  508,  514,  515,  516,
        90300  + /*  1240 */   208,  210,  341,  153,  279,  522,  154,  155,  436,  343,
        90301  + /*  1250 */   190,  524,  345,  263,   50,  156,  135,  351,  409,  409,
        90302  + /*  1260 */   409,  410,  411,    8,  525,  535,  107,  110,  172,  173,
        90303  + /*  1270 */   545,  365,  166,  112,  113,  114,  477,  115,  553,  165,
        90304  + /*  1280 */   407,  366,  275,   95,  117,  207,  281,  103,  375,  223,
        90305  + /*  1290 */   394,  197,  185,   32,  600,  426,  435,  601,  602,  167,
        90306  + /*  1300 */   141,  142,  422,  438,  266,  179,  143,  439,  440,  444,
        90307  + /*  1310 */     5,  409,  409,  409,   42,    9,  457,   10,   98,  133,
        90308  + /*  1320 */   465,  473,  474,  147,   48,  100,  500,  251,  206,  101,
        90309  + /*  1330 */   134,  333,   49,  102,  152,  243,  342,  169,  523,  104,
        90310  + /*  1340 */   146,  264,  157,  346,  106,   13,  338,  538,  158,  159,
        90311  + /*  1350 */   541,  108,  109,   11,  526,  111,   51,  546,  105,  530,
        90312  + /*  1360 */   116,  549,  118,  556,   12,  119,  372,  850,  557,  850,
        90313  + /*  1370 */   850,  850,  850,  850,  850,  850,  850,   52,
 88538  90314   };
 88539  90315   static const YYCODETYPE yy_lookahead[] = {
 88540         - /*     0 */    19,  140,  141,  142,  143,   24,  116,   26,   75,   76,
 88541         - /*    10 */    77,   78,   25,   80,   81,   82,   83,   84,   85,   86,
 88542         - /*    20 */    87,   88,   89,   90,   26,   27,  158,   26,   27,   48,
 88543         - /*    30 */    49,   79,   80,   81,   82,   83,   84,   85,   86,   87,
 88544         - /*    40 */    88,   89,   90,  220,  221,  222,  223,   66,   67,   68,
        90316  + /*     0 */    19,  140,  141,  142,  143,   24,  115,   26,   77,   78,
        90317  + /*    10 */    79,   80,   92,   82,   83,   84,   85,   86,   87,   88,
        90318  + /*    20 */    89,   90,   91,   92,   26,   27,  220,  221,  222,  223,
        90319  + /*    30 */    49,   50,   81,   82,   83,   84,   85,   86,   87,   88,
        90320  + /*    40 */    89,   90,   91,   92,   88,   89,   90,   91,   92,   68,
 88545  90321    /*    50 */    69,   70,   71,   72,   73,   74,   75,   76,   77,   78,
 88546         - /*    60 */   192,   80,   81,   82,   83,   84,   85,   86,   87,   88,
 88547         - /*    70 */    89,   90,   19,   19,   19,   94,   84,   85,   25,   80,
 88548         - /*    80 */    81,   82,   83,   84,   85,   86,   87,   88,   89,   90,
 88549         - /*    90 */   103,   25,   94,   95,   96,   94,   95,   99,  100,  101,
 88550         - /*   100 */    19,   48,   49,   49,  220,   52,  222,  223,  110,   84,
 88551         - /*   110 */    85,   86,   87,   88,   89,   90,   24,   16,   26,   66,
 88552         - /*   120 */    67,   68,   69,   70,   71,   72,   73,   74,   75,   76,
 88553         - /*   130 */    77,   78,   49,   80,   81,   82,   83,   84,   85,   86,
 88554         - /*   140 */    87,   88,   89,   90,   19,   90,  164,  165,  166,   24,
 88555         - /*   150 */   164,   96,   97,   98,   99,  100,  101,  102,  104,   58,
 88556         - /*   160 */   148,   60,   96,  109,  109,   99,  100,  101,   22,   55,
 88557         - /*   170 */   158,   90,   26,   48,   49,  163,  110,  172,   97,   98,
 88558         - /*   180 */    99,  100,  101,  102,  172,  173,   94,  104,   64,  203,
 88559         - /*   190 */   109,   66,   67,   68,   69,   70,   71,   72,   73,   74,
 88560         - /*   200 */    75,   76,   77,   78,  192,   80,   81,   82,   83,   84,
 88561         - /*   210 */    85,   86,   87,   88,   89,   90,   19,  153,   94,  105,
 88562         - /*   220 */   106,  107,   98,  148,   26,   97,  214,   90,  144,  145,
 88563         - /*   230 */   116,  219,  170,  171,  150,  148,  152,  148,  163,  138,
 88564         - /*   240 */    94,  179,  158,  171,  116,   48,   49,  172,  173,  163,
 88565         - /*   250 */   163,  179,  163,  129,  130,  131,  128,  164,  172,  172,
 88566         - /*   260 */   173,  172,  173,   66,   67,   68,   69,   70,   71,   72,
 88567         - /*   270 */    73,   74,   75,   76,   77,   78,  192,   80,   81,   82,
 88568         - /*   280 */    83,   84,   85,   86,   87,   88,   89,   90,   19,  214,
 88569         - /*   290 */    11,  227,   94,   22,  219,  231,  203,   26,   27,  113,
 88570         - /*   300 */   216,  212,  213,  117,  148,  161,  148,  172,  164,  165,
 88571         - /*   310 */   166,  224,  148,  220,  158,  222,  223,   48,   49,  163,
 88572         - /*   320 */   116,  163,   86,   87,   88,   89,   90,  163,  172,  173,
 88573         - /*   330 */   172,  173,  128,  136,   55,   66,   67,   68,   69,   70,
 88574         - /*   340 */    71,   72,   73,   74,   75,   76,   77,   78,  192,   80,
 88575         - /*   350 */    81,   82,   83,   84,   85,   86,   87,   88,   89,   90,
 88576         - /*   360 */    19,  148,  148,   98,   23,   94,   95,  148,  184,   26,
 88577         - /*   370 */    27,  213,  159,  160,  109,  211,  163,  163,  148,  167,
 88578         - /*   380 */   168,  169,  163,   22,  105,  106,  107,   26,   27,   48,
 88579         - /*   390 */    49,  172,  173,  163,  184,  116,  183,  184,   21,  185,
 88580         - /*   400 */     1,    2,  172,  173,   22,  136,   24,   66,   67,   68,
 88581         - /*   410 */    69,   70,   71,   72,   73,   74,   75,   76,   77,   78,
 88582         - /*   420 */   193,   80,   81,   82,   83,   84,   85,   86,   87,   88,
 88583         - /*   430 */    89,   90,   19,  214,  148,  148,   23,   94,   95,   19,
 88584         - /*   440 */    25,  229,  156,   26,   27,  159,  160,   27,  218,  163,
 88585         - /*   450 */   163,   23,  189,   26,   27,   94,   95,  142,  143,  172,
 88586         - /*   460 */   173,   48,   49,  120,   96,   89,   90,   99,  100,  101,
 88587         - /*   470 */   167,  168,  169,    0,    1,    2,   12,  100,  110,   66,
 88588         - /*   480 */    67,   68,   69,   70,   71,   72,   73,   74,   75,   76,
 88589         - /*   490 */    77,   78,   28,   80,   81,   82,   83,   84,   85,   86,
 88590         - /*   500 */    87,   88,   89,   90,   19,  218,  206,   43,   23,   45,
 88591         - /*   510 */    95,   94,   95,   86,  148,  138,  179,  180,   23,   55,
 88592         - /*   520 */    25,   94,   95,  186,  194,   23,  148,   48,   49,  163,
 88593         - /*   530 */   158,  148,  229,   48,   49,  120,   19,  120,  172,  173,
 88594         - /*   540 */   112,  163,  114,  115,   27,  158,  163,  146,   69,   70,
 88595         - /*   550 */   158,   66,   67,   68,   69,   70,   71,   72,   73,   74,
 88596         - /*   560 */    75,   76,   77,   78,  192,   80,   81,   82,   83,   84,
 88597         - /*   570 */    85,   86,   87,   88,   89,   90,   19,   98,  148,  192,
 88598         - /*   580 */    23,  148,  164,  148,  192,  148,  148,  181,  216,  211,
 88599         - /*   590 */    95,  204,  205,  163,  211,   23,  163,  148,  163,   23,
 88600         - /*   600 */   163,  163,  172,  173,  232,   48,   49,  172,  173,  237,
 88601         - /*   610 */   172,  173,  163,  181,  112,  120,  114,  115,  185,  188,
 88602         - /*   620 */   189,  172,  173,   66,   67,   68,   69,   70,   71,   72,
 88603         - /*   630 */    73,   74,   75,   76,   77,   78,  148,   80,   81,   82,
 88604         - /*   640 */    83,   84,   85,   86,   87,   88,   89,   90,   19,  148,
 88605         - /*   650 */    31,  163,  148,    7,    8,    9,  148,  148,   35,   40,
 88606         - /*   660 */   172,  173,  188,  189,  163,  204,  205,  163,  148,   19,
 88607         - /*   670 */   148,  163,  163,  172,  173,  238,  233,   48,   49,   56,
 88608         - /*   680 */   172,  173,  234,  163,  112,  163,  114,  115,  112,  185,
 88609         - /*   690 */   114,  115,  172,  173,  185,   66,   67,   68,   69,   70,
 88610         - /*   700 */    71,   72,   73,   74,   75,   76,   77,   78,  148,   80,
 88611         - /*   710 */    81,   82,   83,   84,   85,   86,   87,   88,   89,   90,
 88612         - /*   720 */    19,  148,  148,  163,   12,  164,  148,  148,  195,  148,
 88613         - /*   730 */    25,  108,  172,  173,  181,  202,  163,  163,  129,  130,
 88614         - /*   740 */    28,  163,  163,  148,  163,  172,  173,  148,   98,   48,
 88615         - /*   750 */    49,  172,  173,  172,  173,   43,  151,   45,  163,   16,
 88616         - /*   760 */   238,  194,  163,  185,  203,  191,  196,   66,   67,   68,
 88617         - /*   770 */    69,   70,   71,   72,   73,   74,   75,   76,   77,   78,
 88618         - /*   780 */   148,   80,   81,   82,   83,   84,   85,   86,   87,   88,
 88619         - /*   790 */    89,   90,   19,  148,  164,  163,  148,  148,   29,   86,
 88620         - /*   800 */   148,   58,   33,   60,  172,  173,  226,  112,  163,  114,
 88621         - /*   810 */   115,  163,  163,  148,  215,  163,   47,  172,  173,  148,
 88622         - /*   820 */   148,   48,   49,  230,  172,  173,  113,  135,  163,  137,
 88623         - /*   830 */   117,  126,  164,  238,  163,  163,  158,  172,  173,  191,
 88624         - /*   840 */    67,   68,   69,   70,   71,   72,   73,   74,   75,   76,
 88625         - /*   850 */    77,   78,  148,   80,   81,   82,   83,   84,   85,   86,
 88626         - /*   860 */    87,   88,   89,   90,   19,  148,  148,  163,  148,  148,
 88627         - /*   870 */   192,  203,  148,  105,  106,  107,  172,  173,   25,  180,
 88628         - /*   880 */   163,  163,  175,  163,  163,  148,  215,  163,  230,  172,
 88629         - /*   890 */   173,  175,  148,   48,   49,  207,  172,  173,   97,   98,
 88630         - /*   900 */   163,  105,  106,  107,  158,    7,    8,  163,  175,  172,
 88631         - /*   910 */   173,  191,  191,   68,   69,   70,   71,   72,   73,   74,
 88632         - /*   920 */    75,   76,   77,   78,  175,   80,   81,   82,   83,   84,
 88633         - /*   930 */    85,   86,   87,   88,   89,   90,   19,   20,  192,   22,
 88634         - /*   940 */   148,  148,  148,   26,   27,  158,  148,   23,  158,   25,
 88635         - /*   950 */   164,   19,   20,   36,   22,  163,  163,  163,   26,   27,
 88636         - /*   960 */   148,  163,  216,  148,  172,  173,  172,  173,   36,  164,
 88637         - /*   970 */   148,   54,  119,  148,  175,  163,  176,  148,  163,  192,
 88638         - /*   980 */   239,   64,  192,  237,  191,  163,   54,  239,  163,  148,
 88639         - /*   990 */   148,   23,  163,   25,  172,  173,   64,  172,  173,  197,
 88640         - /*  1000 */   197,   84,   85,  191,  163,  163,  191,  148,  148,  148,
 88641         - /*  1010 */    93,   94,   95,  172,  173,   98,   84,   85,   86,   23,
 88642         - /*  1020 */   191,   25,  163,  163,  163,   93,   94,   95,  207,  148,
 88643         - /*  1030 */    98,  172,  173,  116,  207,   19,   20,  147,   22,  148,
 88644         - /*  1040 */    13,  148,   26,   27,  163,  148,  129,  130,  131,  132,
 88645         - /*  1050 */   133,  134,   36,   22,  163,    6,  163,   26,   27,  147,
 88646         - /*  1060 */   163,  129,  130,  131,  132,  133,  134,    5,  148,  148,
 88647         - /*  1070 */    54,  148,   10,   11,   12,   13,   14,  147,  147,   17,
 88648         - /*  1080 */    64,  148,  148,  163,  163,  149,  163,  148,  148,  148,
 88649         - /*  1090 */    25,  149,  172,  173,  157,   64,  163,  163,  192,  126,
 88650         - /*  1100 */    84,   85,  163,  163,  163,  172,  173,  148,  148,   93,
 88651         - /*  1110 */    94,   95,  172,  173,   98,   84,   85,   55,  123,  148,
 88652         - /*  1120 */   148,  124,  163,  163,  148,   94,  148,  148,   23,   98,
 88653         - /*  1130 */    25,  172,  173,  198,  163,  163,  122,  148,  148,  163,
 88654         - /*  1140 */   197,  163,  163,  172,  173,  129,  130,  131,  132,  133,
 88655         - /*  1150 */   134,  148,  163,  163,  199,  148,  148,  148,  200,  148,
 88656         - /*  1160 */   129,  130,  131,   23,  201,   25,  163,  105,  106,  107,
 88657         - /*  1170 */   163,  163,  163,  111,  163,  172,  173,   23,  116,   25,
 88658         - /*  1180 */   172,  173,  148,  172,  173,  148,  125,  148,  148,   23,
 88659         - /*  1190 */   148,   25,   23,   23,   25,   25,  118,  163,  135,  225,
 88660         - /*  1200 */   163,  155,  163,  163,  104,  163,  172,  173,  155,  172,
 88661         - /*  1210 */   173,  121,  208,  104,  209,  209,  103,  174,  208,  174,
 88662         - /*  1220 */   208,   46,  209,  182,  174,   22,  174,  176,   90,  228,
 88663         - /*  1230 */   228,  177,  174,  174,  174,   18,  154,  182,  155,  154,
 88664         - /*  1240 */   154,  177,  155,  155,  155,   44,  135,  154,   66,  155,
 88665         - /*  1250 */   235,   22,  217,  236,  187,  187,   18,  162,  178,  190,
 88666         - /*  1260 */   190,  190,  190,  197,  187,  217,    1,   15,   23,  178,
 88667         - /*  1270 */   197,   22,   98,  118,  118,  118,  118,  118,  113,   22,
 88668         - /*  1280 */    11,   23,   23,   22,   22,   25,   23,   23,   34,   23,
 88669         - /*  1290 */    34,  120,   25,   25,   22,  118,   23,   23,   27,   22,
 88670         - /*  1300 */    50,   50,   22,   22,   34,   23,   22,   22,  102,  109,
 88671         - /*  1310 */    19,   24,   20,   38,  104,   25,  138,  104,   22,    5,
 88672         - /*  1320 */    42,    1,  118,   34,   27,  127,   50,  108,   22,   74,
 88673         - /*  1330 */   119,    1,   20,  119,   51,  108,   57,   51,   22,  127,
 88674         - /*  1340 */     3,  240,    4,  128,  240,  240,  240,  240,  240,  240,
 88675         - /*  1350 */   240,  240,  240,  240,  240,  240,  240,  240,  240,  240,
 88676         - /*  1360 */   240,  240,   74,
 88677         -};
 88678         -#define YY_SHIFT_USE_DFLT (-111)
 88679         -#define YY_SHIFT_MAX 370
        90322  + /*    60 */    79,   80,  164,   82,   83,   84,   85,   86,   87,   88,
        90323  + /*    70 */    89,   90,   91,   92,   19,   94,  220,   19,  222,  223,
        90324  + /*    80 */    25,   82,   83,   84,   85,   86,   87,   88,   89,   90,
        90325  + /*    90 */    91,   92,   94,   95,   96,    1,    2,   99,  100,  101,
        90326  + /*   100 */   172,  203,   19,  184,   49,   50,  172,  109,  163,   54,
        90327  + /*   110 */    86,   87,   88,   89,   90,   91,   92,  172,  220,  184,
        90328  + /*   120 */   222,  223,  193,   68,   69,   70,   71,   72,   73,   74,
        90329  + /*   130 */    75,   76,   77,   78,   79,   80,  189,   82,   83,   84,
        90330  + /*   140 */    85,   86,   87,   88,   89,   90,   91,   92,   19,  148,
        90331  + /*   150 */    92,   86,   87,   24,   96,   97,   98,   99,  100,  101,
        90332  + /*   160 */   102,  171,  144,  145,  163,   22,  108,   24,  150,  179,
        90333  + /*   170 */   152,  148,   36,  172,  173,   92,  158,  148,   49,   50,
        90334  + /*   180 */    97,   98,   99,  100,  101,  102,  163,   51,  153,   26,
        90335  + /*   190 */    27,  108,  163,  206,   58,  172,  173,   68,   69,   70,
        90336  + /*   200 */    71,   72,   73,   74,   75,   76,   77,   78,   79,   80,
        90337  + /*   210 */   192,   82,   83,   84,   85,   86,   87,   88,   89,   90,
        90338  + /*   220 */    91,   92,   19,  148,   11,  224,   22,  181,  179,  180,
        90339  + /*   230 */    26,   27,  181,  158,  216,  186,  148,  214,  163,   25,
        90340  + /*   240 */   148,   96,  219,  107,   99,  100,  101,  172,  173,   26,
        90341  + /*   250 */   158,  163,   49,   50,  109,  163,  195,   94,   95,  146,
        90342  + /*   260 */   172,  173,  227,  202,  172,  173,  231,  192,   91,   92,
        90343  + /*   270 */    57,   68,   69,   70,   71,   72,   73,   74,   75,   76,
        90344  + /*   280 */    77,   78,   79,   80,  192,   82,   83,   84,   85,   86,
        90345  + /*   290 */    87,   88,   89,   90,   91,   92,   19,  148,   94,   95,
        90346  + /*   300 */   212,  213,  164,  165,  166,  148,  214,   32,  159,  160,
        90347  + /*   310 */    96,  219,  163,   99,  100,  101,   41,   94,  105,  106,
        90348  + /*   320 */   163,   26,   27,  109,   26,   27,   49,   50,  115,  172,
        90349  + /*   330 */   173,   22,  183,  184,  194,   26,   27,  204,  205,  136,
        90350  + /*   340 */    24,  128,   26,  148,   66,   68,   69,   70,   71,   72,
        90351  + /*   350 */    73,   74,   75,   76,   77,   78,   79,   80,  163,   82,
        90352  + /*   360 */    83,   84,   85,   86,   87,   88,   89,   90,   91,   92,
        90353  + /*   370 */    19,  214,   94,  148,   23,   23,   98,   25,  112,  148,
        90354  + /*   380 */   185,  156,  116,  164,  159,  160,  233,  148,  163,   94,
        90355  + /*   390 */    95,  148,   94,   95,  163,   26,   27,   57,  158,  234,
        90356  + /*   400 */    49,   50,  163,   94,   95,   16,  163,  129,  130,  131,
        90357  + /*   410 */    94,  172,  173,  136,  119,  172,  173,  119,  151,   68,
        90358  + /*   420 */    69,   70,   71,   72,   73,   74,   75,   76,   77,   78,
        90359  + /*   430 */    79,   80,  192,   82,   83,   84,   85,   86,   87,   88,
        90360  + /*   440 */    89,   90,   91,   92,   19,  105,  106,   95,   23,   60,
        90361  + /*   450 */    19,   62,  213,  164,  148,  115,  216,   88,  148,  188,
        90362  + /*   460 */   189,  218,  161,   94,   95,  164,  165,  166,  128,  163,
        90363  + /*   470 */    23,  119,  232,  163,   49,   50,  148,  237,  172,  173,
        90364  + /*   480 */    21,   50,  172,  173,    0,    1,    2,  194,  167,  168,
        90365  + /*   490 */   169,  163,  203,   68,   69,   70,   71,   72,   73,   74,
        90366  + /*   500 */    75,   76,   77,   78,   79,   80,  148,   82,   83,   84,
        90367  + /*   510 */    85,   86,   87,   88,   89,   90,   91,   92,   19,  148,
        90368  + /*   520 */    12,  163,   23,   23,  218,  164,  111,  138,  113,  114,
        90369  + /*   530 */   172,  173,  188,  189,  163,  104,   28,   19,  148,  108,
        90370  + /*   540 */   167,  168,  169,  172,  173,   27,  170,  171,   49,   50,
        90371  + /*   550 */   229,  148,   44,  163,   46,  179,  142,  143,  111,  100,
        90372  + /*   560 */   113,  114,  172,  173,  203,   57,  163,   68,   69,   70,
        90373  + /*   570 */    71,   72,   73,   74,   75,   76,   77,   78,   79,   80,
        90374  + /*   580 */   148,   82,   83,   84,   85,   86,   87,   88,   89,   90,
        90375  + /*   590 */    91,   92,   19,  148,  191,  163,   23,  138,   23,  164,
        90376  + /*   600 */   196,  148,  229,   22,  172,  173,  148,   26,  163,  148,
        90377  + /*   610 */    97,  111,  148,  113,  114,   23,  163,  172,  173,   98,
        90378  + /*   620 */   148,  163,   49,   50,  163,  172,  173,  163,  115,  108,
        90379  + /*   630 */   172,  173,  129,  130,  158,  163,  172,  173,  203,  164,
        90380  + /*   640 */   127,   68,   69,   70,   71,   72,   73,   74,   75,   76,
        90381  + /*   650 */    77,   78,   79,   80,  148,   82,   83,   84,   85,   86,
        90382  + /*   660 */    87,   88,   89,   90,   91,   92,   19,  148,  192,  163,
        90383  + /*   670 */   148,    7,    8,    9,   25,   94,   88,   25,  172,  173,
        90384  + /*   680 */   204,  205,  163,  148,  148,  163,  111,  148,  113,  114,
        90385  + /*   690 */   230,  172,  173,   25,  172,  173,   49,   50,  163,  163,
        90386  + /*   700 */   112,  115,  163,  111,  116,  113,  114,  172,  173,  230,
        90387  + /*   710 */   238,  172,  173,  127,  158,   68,   69,   70,   71,   72,
        90388  + /*   720 */    73,   74,   75,   76,   77,   78,   79,   80,  148,   82,
        90389  + /*   730 */    83,   84,   85,   86,   87,   88,   89,   90,   91,   92,
        90390  + /*   740 */    19,  148,  226,  163,  148,  175,   12,  211,  192,  158,
        90391  + /*   750 */   105,  106,  172,  173,  175,  103,  163,  148,  207,  163,
        90392  + /*   760 */   148,  148,   28,   95,  148,  172,  173,  118,  172,  173,
        90393  + /*   770 */    49,   50,  163,  128,   50,  163,  163,  175,   44,  163,
        90394  + /*   780 */    46,  172,  173,  192,  180,  172,  173,  119,  164,   68,
        90395  + /*   790 */    69,   70,   71,   72,   73,   74,   75,   76,   77,   78,
        90396  + /*   800 */    79,   80,  148,   82,   83,   84,   85,   86,   87,   88,
        90397  + /*   810 */    89,   90,   91,   92,   19,  148,  175,  163,  148,    7,
        90398  + /*   820 */     8,   19,  148,  211,  148,  175,  172,  173,  104,   27,
        90399  + /*   830 */   163,  148,  148,  163,  148,  148,  148,  163,  148,  163,
        90400  + /*   840 */    49,   50,  172,  173,   49,   50,  163,  163,  176,  163,
        90401  + /*   850 */   163,  163,  185,  163,  238,  172,  173,  164,  172,  173,
        90402  + /*   860 */   172,  173,   71,   72,   69,   70,   71,   72,   73,   74,
        90403  + /*   870 */    75,   76,   77,   78,   79,   80,  148,   82,   83,   84,
        90404  + /*   880 */    85,   86,   87,   88,   89,   90,   91,   92,   19,   98,
        90405  + /*   890 */   148,  163,  148,   30,   23,  158,   25,   34,  211,  215,
        90406  + /*   900 */   172,  173,  135,  197,  137,  163,  148,  163,  148,  148,
        90407  + /*   910 */   148,   48,  105,  106,  172,  173,  172,  173,   49,   50,
        90408  + /*   920 */   239,  163,  148,  163,  163,  163,  207,   16,  238,  192,
        90409  + /*   930 */   172,  173,  172,  173,  172,  173,   23,  163,   25,   70,
        90410  + /*   940 */    71,   72,   73,   74,   75,   76,   77,   78,   79,   80,
        90411  + /*   950 */   158,   82,   83,   84,   85,   86,   87,   88,   89,   90,
        90412  + /*   960 */    91,   92,   19,   20,  148,   22,  148,  147,  158,   26,
        90413  + /*   970 */    27,   60,  148,   62,  239,   19,   20,  207,   22,  163,
        90414  + /*   980 */    37,  163,   26,   27,  192,  197,   23,  163,   25,  148,
        90415  + /*   990 */   172,  173,  148,   37,  158,  148,  148,   97,   98,   56,
        90416  + /*  1000 */   148,  185,  192,  148,  163,    6,  147,  163,  148,   66,
        90417  + /*  1010 */   163,  163,   56,  172,  173,  163,  172,  173,  163,  172,
        90418  + /*  1020 */   173,  147,   66,  163,  172,  173,  216,  147,  192,   86,
        90419  + /*  1030 */    87,  148,  172,  173,  149,   13,   93,   94,   95,  191,
        90420  + /*  1040 */   149,   98,   86,   87,   88,  157,  163,  237,   25,   93,
        90421  + /*  1050 */    94,   95,  125,  192,   98,  172,  173,  122,  115,   25,
        90422  + /*  1060 */   198,   19,   20,  148,   22,  197,  148,  148,   26,   27,
        90423  + /*  1070 */   215,  199,  129,  130,  131,  132,  133,  134,  163,   37,
        90424  + /*  1080 */   148,  163,  163,  123,  148,  129,  130,  131,  132,  133,
        90425  + /*  1090 */   134,  172,  173,  121,  200,  163,  148,  148,   56,  163,
        90426  + /*  1100 */   185,  148,  148,  185,  172,  173,  148,  148,   66,  148,
        90427  + /*  1110 */   148,  163,  163,  124,  135,  148,  163,  163,  148,  148,
        90428  + /*  1120 */   225,  163,  163,  201,  163,  163,  117,  191,   86,   87,
        90429  + /*  1130 */   163,  148,  148,  163,  163,   93,   94,   95,  148,  191,
        90430  + /*  1140 */    98,   23,  148,   25,  191,  191,  163,  163,   19,   20,
        90431  + /*  1150 */   191,   22,  191,  163,  104,   26,   27,  163,   23,  125,
        90432  + /*  1160 */    25,  155,  148,  148,  148,  148,   37,  148,  148,  148,
        90433  + /*  1170 */   104,  129,  130,  131,  132,  133,  134,  163,  163,  163,
        90434  + /*  1180 */   163,    5,  163,  163,  163,   56,   10,   11,   12,   13,
        90435  + /*  1190 */    14,  148,  148,   17,  155,   66,  148,   23,   23,   25,
        90436  + /*  1200 */    25,   23,  120,   25,   22,  208,  163,  163,   26,   27,
        90437  + /*  1210 */    23,  163,   25,  209,  209,   86,   87,  209,  208,  208,
        90438  + /*  1220 */   174,  174,   93,   94,   95,  174,   47,   98,  103,  176,
        90439  + /*  1230 */   174,  182,   22,   57,  177,   92,  182,  174,  174,  174,
        90440  + /*  1240 */   228,  228,   18,  154,  177,  155,  154,  154,   66,  155,
        90441  + /*  1250 */   155,  155,   45,  235,  135,  154,   68,  155,  129,  130,
        90442  + /*  1260 */   131,  132,  133,  134,  236,  187,  187,   22,   86,   87,
        90443  + /*  1270 */   197,   18,  217,  190,  190,  190,   94,  190,  197,  217,
        90444  + /*  1280 */    98,  105,  106,  162,  187,  178,  110,  178,    1,   15,
        90445  + /*  1290 */    23,  115,   22,   22,  117,   23,   23,  117,  117,   22,
        90446  + /*  1300 */   117,  117,  112,   11,  128,   22,   25,   23,   23,   23,
        90447  + /*  1310 */    35,  129,  130,  131,   25,   25,  119,   35,   22,  117,
        90448  + /*  1320 */    27,   23,   23,   35,   22,   22,   29,   23,   52,   22,
        90449  + /*  1330 */    39,   52,   22,   22,  102,  108,   19,   24,   20,  104,
        90450  + /*  1340 */    25,  138,  104,   43,   22,    5,   52,    1,  117,   35,
        90451  + /*  1350 */    27,  107,  126,   22,   59,  118,   76,    1,   53,   53,
        90452  + /*  1360 */   118,   20,  107,  127,   22,  126,    3,  240,    4,  240,
        90453  + /*  1370 */   240,  240,  240,  240,  240,  240,  240,   76,
        90454  +};
        90455  +#define YY_SHIFT_USE_DFLT (-110)
        90456  +#define YY_SHIFT_MAX 372
 88680  90457   static const short yy_shift_ofst[] = {
 88681         - /*     0 */   399,  917, 1062,  917, 1016, 1016,   -2,  -19, 1016, 1016,
 88682         - /*    10 */  1016, 1016, 1016,  114,    1,  932, 1016, 1016, 1016, 1016,
 88683         - /*    20 */  1016, 1016, 1016, 1016, 1016, 1016, 1016, 1016, 1016, 1016,
 88684         - /*    30 */  1016, 1016, 1016, 1016, 1016, 1016, 1016, 1016, 1016, 1016,
 88685         - /*    40 */  1016, 1016, 1016, 1016, 1016, 1016, 1016, 1016, 1016, 1016,
 88686         - /*    50 */  1016, 1016,  -48,  279,  198,   -8,   -8, -110,   53,  125,
 88687         - /*    60 */   197,  269,  341,  413,  485,  557,  629,  701,  629,  629,
 88688         - /*    70 */   629,  629,  629,  629,  629,  629,  629,  629,  629,  629,
 88689         - /*    80 */   629,  629,  629,  629,  773,  845,  845,  -67,  -67,   -1,
 88690         - /*    90 */    -1,   55,   25,  236,    1,    1,    1,    1,    1,   54,
 88691         - /*   100 */     1,    1,    1,    1,    1,    1,    1,    1,    1,    1,
 88692         - /*   110 */     1,    1,    1,    1,    1,    1,  377,  376,  198,  137,
 88693         - /*   120 */  -111, -111, -111, 1031,   81,  271,  343,  417,  361,  427,
 88694         - /*   130 */   464,  464,    1,    1,    1,    1,    1,    1,    1,    1,
 88695         - /*   140 */     1,    1,    1,    1,    1,    1,    1,    1,    1,    1,
 88696         - /*   150 */     1,    1,    1,    1,    1,    1,    1,  473,   92,   92,
 88697         - /*   160 */    92,  128,  204, -110, -110, -110, -111, -111,  124,  124,
 88698         - /*   170 */    66,  368,  428,  502,  572,  712,  576,  495,  646,  713,
 88699         - /*   180 */   146,  415,  623,  623,  623,  695,  695,  769,  768,  695,
 88700         - /*   190 */   695,  695,  695,  382,  186,  853,  198,  609,  609,  692,
 88701         - /*   200 */   619,  619,  420,  619,   83,  619,  198,  619,  198,  -13,
 88702         - /*   210 */   517,  705,  517,  420,  420,  705, 1049, 1049, 1049, 1049,
 88703         - /*   220 */  1027, 1027, 1065, -110,  973,  995,  997, 1014, 1061, 1063,
 88704         - /*   230 */  1078, 1078, 1100, 1090, 1100, 1090, 1100, 1090, 1109, 1109,
 88705         - /*   240 */  1175, 1109, 1113, 1109, 1203, 1138, 1138, 1175, 1109, 1109,
 88706         - /*   250 */  1109, 1203, 1217, 1078, 1217, 1078, 1217, 1078, 1078, 1201,
 88707         - /*   260 */  1111, 1217, 1078, 1182, 1182, 1229,  973, 1238, 1238, 1238,
 88708         - /*   270 */  1238,  973, 1182, 1229, -111, -111, -111,  479,  101,  796,
 88709         - /*   280 */   743,  650,  924,  968,  265,  898,  801,  996, 1105, 1140,
 88710         - /*   290 */  1154, 1166, 1169, 1170, 1265, 1252, 1245, 1249, 1155, 1156,
 88711         - /*   300 */  1157, 1158, 1159, 1174, 1165, 1257, 1258, 1259, 1261, 1269,
 88712         - /*   310 */  1262, 1263, 1260, 1264, 1266, 1267, 1254, 1268, 1256, 1267,
 88713         - /*   320 */  1171, 1272, 1270, 1271, 1177, 1273, 1274, 1275, 1250, 1277,
 88714         - /*   330 */  1251, 1280, 1282, 1281, 1284, 1276, 1285, 1206, 1200, 1291,
 88715         - /*   340 */  1292, 1287, 1210, 1278, 1279, 1283, 1290, 1286, 1178, 1213,
 88716         - /*   350 */  1296, 1314, 1320, 1204, 1289, 1297, 1219, 1255, 1288, 1198,
 88717         - /*   360 */  1306, 1211, 1330, 1312, 1214, 1227, 1212, 1316, 1215, 1337,
 88718         - /*   370 */  1338,
 88719         -};
 88720         -#define YY_REDUCE_USE_DFLT (-178)
 88721         -#define YY_REDUCE_MAX 276
        90458  + /*     0 */    94,  943, 1176,  943, 1042, 1042,   -2,  -19, 1042, 1042,
        90459  + /*    10 */  1042, 1042, 1042,  340,  163,  956, 1042, 1042, 1042, 1042,
        90460  + /*    20 */  1042, 1042, 1042, 1042, 1042, 1042, 1042, 1042, 1042, 1129,
        90461  + /*    30 */  1042, 1042, 1042, 1042, 1042, 1042, 1042, 1042, 1042, 1042,
        90462  + /*    40 */  1042, 1042, 1042, 1042, 1042, 1042, 1042, 1042, 1042, 1042,
        90463  + /*    50 */  1042, 1042, 1042, 1042,  -49,  213,  223,   65,   65, -109,
        90464  + /*    60 */    55,  129,  203,  277,  351,  425,  499,  573,  647,  721,
        90465  + /*    70 */   647,  647,  647,  647,  647,  647,  647,  647,  647,  647,
        90466  + /*    80 */   647,  647,  647,  647,  647,  647,  795,  869,  869,  -69,
        90467  + /*    90 */   -69,  -69,  -69,   -1,   -1,   58,   24,  -44,  163,  163,
        90468  + /*   100 */   163,  163,  163,  431,  163,  163,  163,  163,  163,  163,
        90469  + /*   110 */   163,  163,  163,  163,  163,  163,  163,  163,  163,  163,
        90470  + /*   120 */   459,  177,  223,  -80, -110, -110, -110, 1182,   83,  204,
        90471  + /*   130 */   295,  298,  309,  369,  508,  508,  163,  163,  163,  163,
        90472  + /*   140 */   163,  163,  163,  163,  163,  163,  163,  163,  163,  163,
        90473  + /*   150 */   163,  163,  163,  163,  163,  163,  163,  163,  163,  163,
        90474  + /*   160 */   163,  484,  316,  316,  316,  513,  586, -109, -109, -109,
        90475  + /*   170 */  -110, -110,  278,  278,  214,  145,  447,  500,  575,  734,
        90476  + /*   180 */   592,  352,  136,  136,  664,  588,  581,  668,  415,  415,
        90477  + /*   190 */   863,  645,  415,  415,  415,  415,  143,  266,  649,  223,
        90478  + /*   200 */   503,  503,  767,  275,  275,  518,  275,  724,  275,  223,
        90479  + /*   210 */   275,  223,  652,  802, 1034,  802,  518,  518, 1034,  999,
        90480  + /*   220 */   999,  999,  999, 1022, 1022, 1023, -109,  927,  935,  960,
        90481  + /*   230 */   972,  989,  979, 1009, 1009, 1050, 1082, 1050, 1082, 1050,
        90482  + /*   240 */  1082, 1066, 1066, 1179, 1066, 1125, 1066, 1210, 1143, 1143,
        90483  + /*   250 */  1179, 1066, 1066, 1066, 1210, 1224, 1009, 1224, 1009, 1224,
        90484  + /*   260 */  1009, 1009, 1207, 1119, 1224, 1009, 1188, 1188, 1245,  927,
        90485  + /*   270 */  1253, 1253, 1253, 1253,  927, 1188, 1245, -110, -110, -110,
        90486  + /*   280 */   791,  389,  911,  871,  963,  521,  812,  807,  900,  913,
        90487  + /*   290 */  1118, 1135, 1174, 1175, 1178, 1187, 1287, 1274, 1267, 1270,
        90488  + /*   300 */  1177, 1180, 1181, 1183, 1184, 1190, 1271, 1272, 1273, 1277,
        90489  + /*   310 */  1292, 1283, 1284, 1281, 1285, 1286, 1289, 1275, 1290, 1282,
        90490  + /*   320 */  1289, 1197, 1296, 1288, 1293, 1202, 1298, 1299, 1291, 1276,
        90491  + /*   330 */  1302, 1297, 1279, 1303, 1304, 1307, 1310, 1294, 1311, 1232,
        90492  + /*   340 */  1227, 1317, 1318, 1313, 1235, 1300, 1295, 1305, 1315, 1306,
        90493  + /*   350 */  1203, 1238, 1322, 1340, 1346, 1231, 1314, 1323, 1244, 1280,
        90494  + /*   360 */  1301, 1226, 1331, 1237, 1356, 1341, 1242, 1255, 1239, 1342,
        90495  + /*   370 */  1236, 1363, 1364,
        90496  +};
        90497  +#define YY_REDUCE_USE_DFLT (-195)
        90498  +#define YY_REDUCE_MAX 279
 88722  90499   static const short yy_reduce_ofst[] = {
 88723         - /*     0 */  -139,   12,   84,  156,   75,   89,  213,   93,   87,  158,
 88724         - /*    10 */   219,  230,  287,  372,  286,  366,  430,  435,  438,  449,
 88725         - /*    20 */   488,  501,  508,  520,  560,  573,  579,  581,  632,  645,
 88726         - /*    30 */   652,  665,  704,  717,  724,  737,  792,  794,  822,  825,
 88727         - /*    40 */   841,  859,  920,  933,  940,  959,  971, 1003, 1008, 1011,
 88728         - /*    50 */  1034, 1037, -177,  746,  144,  212,  303,  387, -116, -116,
 88729         - /*    60 */  -116, -116, -116, -116, -116, -116, -116, -116, -116, -116,
 88730         - /*    70 */  -116, -116, -116, -116, -116, -116, -116, -116, -116, -116,
 88731         - /*    80 */  -116, -116, -116, -116, -116, -116, -116, -116, -116, -116,
 88732         - /*    90 */  -116,   62, -116, -116,  164,  214,  433,  504,  509,  337,
 88733         - /*   100 */   574,  378,  578,  437,  599,  522,  383,  595,  648,  720,
 88734         - /*   110 */   721,  793,  812,  815,  671,  829,   64, -116,  -18, -116,
 88735         - /*   120 */  -116, -116, -116,   86,   72,  649,  672,  718,  744,  798,
 88736         - /*   130 */   431,  474,  842,  860,  861,  881,  891,  798,  893,  897,
 88737         - /*   140 */   921,  923,  934,  939,  941,  960,  972,  976,  978,  979,
 88738         - /*   150 */   989,  990, 1007, 1009, 1039, 1040, 1042,  315,  -14,  561,
 88739         - /*   160 */   668, -132,  392,  678,  787,  790,  533,  461,    5,  135,
 88740         - /*   170 */   184,  210,  227,  227,  227,  263,  227,  300,  401,  330,
 88741         - /*   180 */   418,  300,  406,  432,  553,  227,  227,  443,  448,  227,
 88742         - /*   190 */   227,  227,  227,  605,  567,  570,  630,  593,  658,  580,
 88743         - /*   200 */   707,  716,  688,  733,  699,  749,  786,  799,  805,  800,
 88744         - /*   210 */   741,  802,  748,  821,  827,  803,  890,  912,  930,  931,
 88745         - /*   220 */   936,  942,  937,  906,  943,  935,  955,  958,  963,  974,
 88746         - /*   230 */  1046, 1053, 1004, 1005, 1010, 1006, 1012, 1013, 1043, 1045,
 88747         - /*   240 */  1041, 1050, 1051, 1052, 1054, 1001, 1002, 1055, 1058, 1059,
 88748         - /*   250 */  1060, 1064, 1082, 1083, 1085, 1087, 1086, 1088, 1089, 1015,
 88749         - /*   260 */  1017, 1093, 1094, 1067, 1068, 1035, 1066, 1069, 1070, 1071,
 88750         - /*   270 */  1072, 1073, 1077, 1048, 1095, 1080, 1091,
        90500  + /*     0 */  -139,   92,   18,   75,   23,   88,  149, -102,    1,  239,
        90501  + /*    10 */   157,  243,  306,  240,  225,  310,  358,  371,  390,  432,
        90502  + /*    20 */   445,  453,  458,  464,  506,  519,  522,  535,  539,  580,
        90503  + /*    30 */   593,  596,  609,  613,  654,  670,  683,  686,  688,  728,
        90504  + /*    40 */   742,  744,  758,  760,  762,  818,  841,  844,  847,  852,
        90505  + /*    50 */   860,  883,  919,  932, -194,  810,  301,  321,  373,  476,
        90506  + /*    60 */  -144, -144, -144, -144, -144, -144, -144, -144, -144, -144,
        90507  + /*    70 */  -144, -144, -144, -144, -144, -144, -144, -144, -144, -144,
        90508  + /*    80 */  -144, -144, -144, -144, -144, -144, -144, -144, -144, -144,
        90509  + /*    90 */  -144, -144, -144, -144, -144,  376, -144, -144,  536,  195,
        90510  + /*   100 */   667,  816,  915,   49,  403,  612,  918,  472,  684,  616,
        90511  + /*   110 */   687,  690,  848,  936,  948,  953,  954,  959,  855,  961,
        90512  + /*   120 */    35, -144,  138, -144, -144, -144, -144,  -55,  -10,   29,
        90513  + /*   130 */   231,  328,  674,  676,  271,  344,  461,  761,  774,  824,
        90514  + /*   140 */   949,  676,  958,  962,  967,  970,  971,  983,  984,  990,
        90515  + /*   150 */   994, 1014, 1015, 1016, 1017, 1019, 1020, 1021, 1043, 1044,
        90516  + /*   160 */  1048,  414,  289,  361,  435,  556,  591,  737,  792,  836,
        90517  + /*   170 */    61,  133,  -72,  -66,  -81,  -65,  -71,  -71,  -71,  -53,
        90518  + /*   180 */   -71,  -13,   46,   51,  113,  140,  219,  -13,  -71,  -71,
        90519  + /*   190 */   153,  165,  -71,  -71,  -71,  -71,  267,  293,  404,  475,
        90520  + /*   200 */   460,  479,  516,  570,  579,  551,  602,  604,  641,  624,
        90521  + /*   210 */   650,  693,  672,  681,  706,  735,  719,  770,  788,  820,
        90522  + /*   220 */   859,  874,  880,  885,  891,  888,  861,  868,  862,  872,
        90523  + /*   230 */   894,  922,  895, 1006, 1039,  997, 1004, 1010, 1005, 1011,
        90524  + /*   240 */  1008, 1046, 1047, 1049, 1051, 1053, 1056, 1057, 1012, 1013,
        90525  + /*   250 */  1054, 1063, 1064, 1065, 1067, 1089, 1090, 1092, 1094, 1093,
        90526  + /*   260 */  1095, 1096, 1018, 1028, 1101, 1102, 1078, 1079, 1055, 1073,
        90527  + /*   270 */  1083, 1084, 1085, 1087, 1081, 1097, 1062, 1121, 1107, 1109,
 88751  90528   };
 88752  90529   static const YYACTIONTYPE yy_default[] = {
 88753  90530    /*     0 */   564,  799,  848,  848,  799,  848,  848,  687,  797,  848,
 88754  90531    /*    10 */   848,  848,  848,  848,  848,  848,  848,  848,  848,  848,
 88755  90532    /*    20 */   848,  848,  848,  848,  848,  848,  848,  848,  848,  848,
 88756  90533    /*    30 */   848,  848,  848,  848,  848,  848,  848,  848,  848,  848,
 88757  90534    /*    40 */   848,  848,  848,  848,  848,  848,  848,  848,  848,  848,
 88758         - /*    50 */   848,  848,  771,  848,  603,  816,  816,  691,  722,  848,
 88759         - /*    60 */   848,  848,  848,  848,  848,  848,  723,  848,  801,  796,
 88760         - /*    70 */   792,  794,  793,  800,  724,  713,  720,  727,  702,  829,
 88761         - /*    80 */   729,  730,  736,  737,  759,  758,  777,  761,  783,  760,
 88762         - /*    90 */   770,  595,  762,  763,  848,  848,  848,  848,  848,  656,
 88763         - /*   100 */   848,  848,  848,  848,  848,  848,  848,  848,  848,  848,
 88764         - /*   110 */   848,  848,  848,  848,  848,  848,  590,  764,  848,  765,
 88765         - /*   120 */   778,  779,  780,  848,  848,  848,  848,  848,  848,  848,
 88766         - /*   130 */   848,  848,  570,  848,  848,  848,  848,  848,  848,  848,
        90535  + /*    50 */   848,  848,  848,  848,  771,  848,  603,  816,  816,  691,
        90536  + /*    60 */   722,  848,  848,  848,  848,  848,  848,  848,  723,  848,
        90537  + /*    70 */   801,  796,  792,  794,  793,  800,  724,  713,  720,  727,
        90538  + /*    80 */   702,  829,  729,  730,  736,  737,  759,  758,  777,  761,
        90539  + /*    90 */   775,  783,  776,  760,  770,  595,  762,  763,  848,  848,
        90540  + /*   100 */   848,  848,  848,  656,  848,  848,  848,  848,  848,  848,
        90541  + /*   110 */   848,  848,  848,  848,  848,  848,  848,  848,  848,  848,
        90542  + /*   120 */   590,  764,  848,  765,  778,  779,  780,  848,  848,  848,
        90543  + /*   130 */   848,  848,  848,  848,  848,  848,  570,  848,  848,  848,
 88767  90544    /*   140 */   848,  848,  848,  848,  848,  848,  848,  848,  848,  848,
 88768         - /*   150 */   848,  848,  848,  848,  848,  848,  848,  564,  687,  687,
 88769         - /*   160 */   687,  848,  848,  848,  848,  848,  681,  691,  848,  848,
 88770         - /*   170 */   647,  848,  848,  848,  848,  848,  848,  848,  572,  679,
 88771         - /*   180 */   605,  689,  848,  848,  848,  592,  668,  822,  848,  841,
 88772         - /*   190 */   839,  670,  732,  848,  679,  688,  848,  848,  848,  795,
 88773         - /*   200 */   716,  716,  704,  716,  626,  716,  848,  716,  848,  629,
 88774         - /*   210 */   834,  726,  834,  704,  704,  726,  569,  569,  569,  569,
 88775         - /*   220 */   580,  580,  646,  848,  726,  717,  719,  709,  721,  848,
 88776         - /*   230 */   695,  695,  703,  708,  703,  708,  703,  708,  658,  658,
 88777         - /*   240 */   643,  658,  629,  658,  805,  809,  809,  643,  658,  658,
 88778         - /*   250 */   658,  805,  587,  695,  587,  695,  587,  695,  695,  826,
 88779         - /*   260 */   828,  587,  695,  660,  660,  738,  726,  667,  667,  667,
 88780         - /*   270 */   667,  726,  660,  738,  613,  631,  631,  848,  848,  848,
        90545  + /*   150 */   848,  848,  848,  848,  848,  848,  848,  848,  848,  848,
        90546  + /*   160 */   848,  564,  687,  687,  687,  848,  848,  848,  848,  848,
        90547  + /*   170 */   681,  691,  848,  848,  647,  848,  848,  848,  848,  848,
        90548  + /*   180 */   848,  848,  848,  848,  572,  679,  605,  689,  592,  668,
        90549  + /*   190 */   822,  848,  841,  839,  670,  732,  848,  679,  688,  848,
        90550  + /*   200 */   848,  848,  795,  716,  716,  704,  716,  626,  716,  848,
        90551  + /*   210 */   716,  848,  629,  834,  726,  834,  704,  704,  726,  569,
        90552  + /*   220 */   569,  569,  569,  580,  580,  646,  848,  726,  717,  719,
        90553  + /*   230 */   709,  721,  848,  695,  695,  703,  708,  703,  708,  703,
        90554  + /*   240 */   708,  658,  658,  643,  658,  629,  658,  805,  809,  809,
        90555  + /*   250 */   643,  658,  658,  658,  805,  587,  695,  587,  695,  587,
        90556  + /*   260 */   695,  695,  826,  828,  587,  695,  660,  660,  738,  726,
        90557  + /*   270 */   667,  667,  667,  667,  726,  660,  738,  613,  631,  631,
 88781  90558    /*   280 */   848,  848,  848,  848,  848,  848,  848,  848,  848,  848,
 88782         - /*   290 */   848,  848,  848,  848,  848,  578,  848,  597,  745,  750,
 88783         - /*   300 */   746,  848,  747,  848,  673,  848,  848,  848,  848,  848,
 88784         - /*   310 */   848,  848,  848,  848,  848,  798,  848,  710,  848,  718,
 88785         - /*   320 */   848,  848,  848,  848,  848,  848,  848,  848,  848,  848,
        90559  + /*   290 */   848,  848,  848,  848,  848,  848,  848,  578,  848,  597,
        90560  + /*   300 */   745,  750,  746,  848,  747,  673,  848,  848,  848,  848,
        90561  + /*   310 */   848,  848,  848,  848,  848,  848,  798,  848,  710,  848,
        90562  + /*   320 */   718,  848,  848,  848,  848,  848,  848,  848,  848,  848,
 88786  90563    /*   330 */   848,  848,  848,  848,  848,  848,  848,  848,  848,  848,
 88787         - /*   340 */   848,  848,  848,  848,  848,  824,  825,  848,  848,  848,
 88788         - /*   350 */   848,  848,  848,  832,  848,  848,  848,  848,  848,  848,
 88789         - /*   360 */   848,  848,  848,  848,  848,  848,  848,  848,  848,  565,
 88790         - /*   370 */   848,  560,  562,  563,  567,  568,  571,  597,  598,  600,
 88791         - /*   380 */   601,  602,  573,  574,  575,  576,  577,  579,  583,  581,
 88792         - /*   390 */   582,  584,  591,  593,  612,  614,  616,  677,  678,  742,
 88793         - /*   400 */   671,  672,  676,  599,  753,  744,  748,  749,  751,  752,
 88794         - /*   410 */   766,  767,  769,  775,  782,  785,  768,  773,  774,  776,
        90564  + /*   340 */   848,  848,  848,  848,  848,  848,  848,  824,  825,  848,
        90565  + /*   350 */   848,  848,  848,  848,  848,  832,  848,  848,  848,  848,
        90566  + /*   360 */   848,  848,  848,  848,  848,  848,  848,  848,  848,  848,
        90567  + /*   370 */   848,  565,  848,  560,  562,  563,  567,  568,  571,  597,
        90568  + /*   380 */   598,  600,  601,  602,  573,  574,  575,  576,  577,  579,
        90569  + /*   390 */   583,  581,  582,  584,  591,  593,  612,  614,  616,  677,
        90570  + /*   400 */   678,  742,  671,  672,  676,  599,  753,  744,  748,  749,
        90571  + /*   410 */   751,  752,  766,  767,  769,  774,  782,  785,  768,  773,
 88795  90572    /*   420 */   781,  784,  674,  675,  788,  606,  607,  610,  611,  812,
 88796  90573    /*   430 */   814,  813,  815,  609,  608,  754,  757,  790,  791,  842,
 88797  90574    /*   440 */   843,  844,  845,  846,  786,  696,  789,  772,  711,  714,
 88798  90575    /*   450 */   715,  712,  680,  690,  698,  699,  700,  701,  685,  686,
 88799  90576    /*   460 */   692,  707,  740,  741,  705,  706,  693,  694,  682,  683,
 88800  90577    /*   470 */   684,  787,  743,  755,  756,  617,  618,  750,  619,  620,
 88801  90578    /*   480 */   621,  659,  662,  663,  664,  622,  641,  644,  645,  623,
 88802         - /*   490 */   630,  624,  625,  632,  633,  634,  637,  638,  639,  640,
 88803         - /*   500 */   635,  636,  806,  807,  810,  808,  627,  628,  642,  615,
        90579  + /*   490 */   630,  624,  625,  632,  633,  634,  636,  637,  638,  639,
        90580  + /*   500 */   640,  635,  806,  807,  810,  808,  627,  628,  642,  615,
 88804  90581    /*   510 */   604,  596,  648,  651,  652,  653,  654,  655,  657,  649,
 88805  90582    /*   520 */   650,  594,  585,  588,  697,  818,  827,  823,  819,  820,
 88806  90583    /*   530 */   821,  589,  802,  803,  661,  734,  735,  817,  830,  833,
 88807  90584    /*   540 */   835,  836,  837,  739,  838,  840,  831,  586,  665,  666,
 88808  90585    /*   550 */   669,  811,  847,  725,  728,  731,  733,  566,  561,
 88809  90586   };
 88810  90587   #define YY_SZ_ACTTAB (int)(sizeof(yy_action)/sizeof(yy_action[0]))
................................................................................
 88846  90623       0,  /*         LP => nothing */
 88847  90624       0,  /*         RP => nothing */
 88848  90625       0,  /*         AS => nothing */
 88849  90626       0,  /*      COMMA => nothing */
 88850  90627       0,  /*         ID => nothing */
 88851  90628       0,  /*    INDEXED => nothing */
 88852  90629      26,  /*      ABORT => ID */
        90630  +   26,  /*     ACTION => ID */
 88853  90631      26,  /*      AFTER => ID */
 88854  90632      26,  /*    ANALYZE => ID */
 88855  90633      26,  /*        ASC => ID */
 88856  90634      26,  /*     ATTACH => ID */
 88857  90635      26,  /*     BEFORE => ID */
 88858  90636      26,  /*         BY => ID */
 88859  90637      26,  /*    CASCADE => ID */
................................................................................
 88867  90645      26,  /*       FAIL => ID */
 88868  90646      26,  /*        FOR => ID */
 88869  90647      26,  /*     IGNORE => ID */
 88870  90648      26,  /*  INITIALLY => ID */
 88871  90649      26,  /*    INSTEAD => ID */
 88872  90650      26,  /*    LIKE_KW => ID */
 88873  90651      26,  /*      MATCH => ID */
        90652  +   26,  /*         NO => ID */
 88874  90653      26,  /*        KEY => ID */
 88875  90654      26,  /*         OF => ID */
 88876  90655      26,  /*     OFFSET => ID */
 88877  90656      26,  /*     PRAGMA => ID */
 88878  90657      26,  /*      RAISE => ID */
 88879  90658      26,  /*    REPLACE => ID */
 88880  90659      26,  /*   RESTRICT => ID */
................................................................................
 88966  90745     "$",             "SEMI",          "EXPLAIN",       "QUERY",       
 88967  90746     "PLAN",          "BEGIN",         "TRANSACTION",   "DEFERRED",    
 88968  90747     "IMMEDIATE",     "EXCLUSIVE",     "COMMIT",        "END",         
 88969  90748     "ROLLBACK",      "SAVEPOINT",     "RELEASE",       "TO",          
 88970  90749     "TABLE",         "CREATE",        "IF",            "NOT",         
 88971  90750     "EXISTS",        "TEMP",          "LP",            "RP",          
 88972  90751     "AS",            "COMMA",         "ID",            "INDEXED",     
 88973         -  "ABORT",         "AFTER",         "ANALYZE",       "ASC",         
 88974         -  "ATTACH",        "BEFORE",        "BY",            "CASCADE",     
 88975         -  "CAST",          "COLUMNKW",      "CONFLICT",      "DATABASE",    
 88976         -  "DESC",          "DETACH",        "EACH",          "FAIL",        
 88977         -  "FOR",           "IGNORE",        "INITIALLY",     "INSTEAD",     
 88978         -  "LIKE_KW",       "MATCH",         "KEY",           "OF",          
 88979         -  "OFFSET",        "PRAGMA",        "RAISE",         "REPLACE",     
 88980         -  "RESTRICT",      "ROW",           "TRIGGER",       "VACUUM",      
 88981         -  "VIEW",          "VIRTUAL",       "REINDEX",       "RENAME",      
 88982         -  "CTIME_KW",      "ANY",           "OR",            "AND",         
 88983         -  "IS",            "BETWEEN",       "IN",            "ISNULL",      
 88984         -  "NOTNULL",       "NE",            "EQ",            "GT",          
 88985         -  "LE",            "LT",            "GE",            "ESCAPE",      
 88986         -  "BITAND",        "BITOR",         "LSHIFT",        "RSHIFT",      
 88987         -  "PLUS",          "MINUS",         "STAR",          "SLASH",       
 88988         -  "REM",           "CONCAT",        "COLLATE",       "UMINUS",      
 88989         -  "UPLUS",         "BITNOT",        "STRING",        "JOIN_KW",     
        90752  +  "ABORT",         "ACTION",        "AFTER",         "ANALYZE",     
        90753  +  "ASC",           "ATTACH",        "BEFORE",        "BY",          
        90754  +  "CASCADE",       "CAST",          "COLUMNKW",      "CONFLICT",    
        90755  +  "DATABASE",      "DESC",          "DETACH",        "EACH",        
        90756  +  "FAIL",          "FOR",           "IGNORE",        "INITIALLY",   
        90757  +  "INSTEAD",       "LIKE_KW",       "MATCH",         "NO",          
        90758  +  "KEY",           "OF",            "OFFSET",        "PRAGMA",      
        90759  +  "RAISE",         "REPLACE",       "RESTRICT",      "ROW",         
        90760  +  "TRIGGER",       "VACUUM",        "VIEW",          "VIRTUAL",     
        90761  +  "REINDEX",       "RENAME",        "CTIME_KW",      "ANY",         
        90762  +  "OR",            "AND",           "IS",            "BETWEEN",     
        90763  +  "IN",            "ISNULL",        "NOTNULL",       "NE",          
        90764  +  "EQ",            "GT",            "LE",            "LT",          
        90765  +  "GE",            "ESCAPE",        "BITAND",        "BITOR",       
        90766  +  "LSHIFT",        "RSHIFT",        "PLUS",          "MINUS",       
        90767  +  "STAR",          "SLASH",         "REM",           "CONCAT",      
        90768  +  "COLLATE",       "BITNOT",        "STRING",        "JOIN_KW",     
 88990  90769     "CONSTRAINT",    "DEFAULT",       "NULL",          "PRIMARY",     
 88991  90770     "UNIQUE",        "CHECK",         "REFERENCES",    "AUTOINCR",    
 88992         -  "ON",            "DELETE",        "UPDATE",        "INSERT",      
 88993         -  "SET",           "DEFERRABLE",    "FOREIGN",       "DROP",        
 88994         -  "UNION",         "ALL",           "EXCEPT",        "INTERSECT",   
 88995         -  "SELECT",        "DISTINCT",      "DOT",           "FROM",        
 88996         -  "JOIN",          "USING",         "ORDER",         "GROUP",       
 88997         -  "HAVING",        "LIMIT",         "WHERE",         "INTO",        
 88998         -  "VALUES",        "INTEGER",       "FLOAT",         "BLOB",        
        90771  +  "ON",            "DELETE",        "UPDATE",        "SET",         
        90772  +  "DEFERRABLE",    "FOREIGN",       "DROP",          "UNION",       
        90773  +  "ALL",           "EXCEPT",        "INTERSECT",     "SELECT",      
        90774  +  "DISTINCT",      "DOT",           "FROM",          "JOIN",        
        90775  +  "USING",         "ORDER",         "GROUP",         "HAVING",      
        90776  +  "LIMIT",         "WHERE",         "INTO",          "VALUES",      
        90777  +  "INSERT",        "INTEGER",       "FLOAT",         "BLOB",        
 88999  90778     "REGISTER",      "VARIABLE",      "CASE",          "WHEN",        
 89000  90779     "THEN",          "ELSE",          "INDEX",         "error",       
 89001  90780     "input",         "cmdlist",       "ecmd",          "explain",     
 89002  90781     "cmdx",          "cmd",           "transtype",     "trans_opt",   
 89003  90782     "nm",            "savepoint_opt",  "create_table",  "create_table_args",
 89004  90783     "createkw",      "temp",          "ifnotexists",   "dbnm",        
 89005  90784     "columnlist",    "conslist_opt",  "select",        "column",      
................................................................................
 89103  90882    /*  70 */ "autoinc ::=",
 89104  90883    /*  71 */ "autoinc ::= AUTOINCR",
 89105  90884    /*  72 */ "refargs ::=",
 89106  90885    /*  73 */ "refargs ::= refargs refarg",
 89107  90886    /*  74 */ "refarg ::= MATCH nm",
 89108  90887    /*  75 */ "refarg ::= ON DELETE refact",
 89109  90888    /*  76 */ "refarg ::= ON UPDATE refact",
 89110         - /*  77 */ "refarg ::= ON INSERT refact",
 89111         - /*  78 */ "refact ::= SET NULL",
 89112         - /*  79 */ "refact ::= SET DEFAULT",
 89113         - /*  80 */ "refact ::= CASCADE",
 89114         - /*  81 */ "refact ::= RESTRICT",
        90889  + /*  77 */ "refact ::= SET NULL",
        90890  + /*  78 */ "refact ::= SET DEFAULT",
        90891  + /*  79 */ "refact ::= CASCADE",
        90892  + /*  80 */ "refact ::= RESTRICT",
        90893  + /*  81 */ "refact ::= NO ACTION",
 89115  90894    /*  82 */ "defer_subclause ::= NOT DEFERRABLE init_deferred_pred_opt",
 89116  90895    /*  83 */ "defer_subclause ::= DEFERRABLE init_deferred_pred_opt",
 89117  90896    /*  84 */ "init_deferred_pred_opt ::=",
 89118  90897    /*  85 */ "init_deferred_pred_opt ::= INITIALLY DEFERRED",
 89119  90898    /*  86 */ "init_deferred_pred_opt ::= INITIALLY IMMEDIATE",
 89120  90899    /*  87 */ "conslist_opt ::=",
 89121  90900    /*  88 */ "conslist_opt ::= COMMA conslist",
................................................................................
 89241  91020    /* 208 */ "likeop ::= NOT LIKE_KW",
 89242  91021    /* 209 */ "likeop ::= MATCH",
 89243  91022    /* 210 */ "likeop ::= NOT MATCH",
 89244  91023    /* 211 */ "escape ::= ESCAPE expr",
 89245  91024    /* 212 */ "escape ::=",
 89246  91025    /* 213 */ "expr ::= expr likeop expr escape",
 89247  91026    /* 214 */ "expr ::= expr ISNULL|NOTNULL",
 89248         - /* 215 */ "expr ::= expr IS NULL",
 89249         - /* 216 */ "expr ::= expr NOT NULL",
 89250         - /* 217 */ "expr ::= expr IS NOT NULL",
        91027  + /* 215 */ "expr ::= expr NOT NULL",
        91028  + /* 216 */ "expr ::= expr IS expr",
        91029  + /* 217 */ "expr ::= expr IS NOT expr",
 89251  91030    /* 218 */ "expr ::= NOT expr",
 89252  91031    /* 219 */ "expr ::= BITNOT expr",
 89253  91032    /* 220 */ "expr ::= MINUS expr",
 89254  91033    /* 221 */ "expr ::= PLUS expr",
 89255  91034    /* 222 */ "between_op ::= BETWEEN",
 89256  91035    /* 223 */ "between_op ::= NOT BETWEEN",
 89257  91036    /* 224 */ "expr ::= expr between_op expr AND expr",
................................................................................
 89772  91551     { 176, 0 },
 89773  91552     { 176, 1 },
 89774  91553     { 178, 0 },
 89775  91554     { 178, 2 },
 89776  91555     { 180, 2 },
 89777  91556     { 180, 3 },
 89778  91557     { 180, 3 },
 89779         -  { 180, 3 },
 89780  91558     { 181, 2 },
 89781  91559     { 181, 2 },
 89782  91560     { 181, 1 },
 89783  91561     { 181, 1 },
        91562  +  { 181, 2 },
 89784  91563     { 179, 3 },
 89785  91564     { 179, 2 },
 89786  91565     { 182, 0 },
 89787  91566     { 182, 2 },
 89788  91567     { 182, 2 },
 89789  91568     { 157, 0 },
 89790  91569     { 157, 2 },
................................................................................
 90099  91878     pParse->db->lookaside.bEnabled = 0;
 90100  91879     yygotominor.yy0 = yymsp[0].minor.yy0;
 90101  91880   }
 90102  91881           break;
 90103  91882         case 28: /* ifnotexists ::= */
 90104  91883         case 31: /* temp ::= */ yytestcase(yyruleno==31);
 90105  91884         case 70: /* autoinc ::= */ yytestcase(yyruleno==70);
        91885  +      case 82: /* defer_subclause ::= NOT DEFERRABLE init_deferred_pred_opt */ yytestcase(yyruleno==82);
 90106  91886         case 84: /* init_deferred_pred_opt ::= */ yytestcase(yyruleno==84);
 90107  91887         case 86: /* init_deferred_pred_opt ::= INITIALLY IMMEDIATE */ yytestcase(yyruleno==86);
 90108  91888         case 97: /* defer_subclause_opt ::= */ yytestcase(yyruleno==97);
 90109  91889         case 108: /* ifexists ::= */ yytestcase(yyruleno==108);
 90110  91890         case 119: /* distinct ::= ALL */ yytestcase(yyruleno==119);
 90111  91891         case 120: /* distinct ::= */ yytestcase(yyruleno==120);
 90112  91892         case 222: /* between_op ::= BETWEEN */ yytestcase(yyruleno==222);
................................................................................
 90224  92004         case 68: /* ccons ::= defer_subclause */
 90225  92005   {sqlite3DeferForeignKey(pParse,yymsp[0].minor.yy412);}
 90226  92006           break;
 90227  92007         case 69: /* ccons ::= COLLATE ids */
 90228  92008   {sqlite3AddCollateType(pParse, &yymsp[0].minor.yy0);}
 90229  92009           break;
 90230  92010         case 72: /* refargs ::= */
 90231         -{ yygotominor.yy412 = OE_Restrict * 0x010101; }
        92011  +{ yygotominor.yy412 = OE_None * 0x000101; }
 90232  92012           break;
 90233  92013         case 73: /* refargs ::= refargs refarg */
 90234  92014   { yygotominor.yy412 = (yymsp[-1].minor.yy412 & ~yymsp[0].minor.yy47.mask) | yymsp[0].minor.yy47.value; }
 90235  92015           break;
 90236  92016         case 74: /* refarg ::= MATCH nm */
 90237  92017   { yygotominor.yy47.value = 0;     yygotominor.yy47.mask = 0x000000; }
 90238  92018           break;
 90239  92019         case 75: /* refarg ::= ON DELETE refact */
 90240  92020   { yygotominor.yy47.value = yymsp[0].minor.yy412;     yygotominor.yy47.mask = 0x0000ff; }
 90241  92021           break;
 90242  92022         case 76: /* refarg ::= ON UPDATE refact */
 90243  92023   { yygotominor.yy47.value = yymsp[0].minor.yy412<<8;  yygotominor.yy47.mask = 0x00ff00; }
 90244  92024           break;
 90245         -      case 77: /* refarg ::= ON INSERT refact */
 90246         -{ yygotominor.yy47.value = yymsp[0].minor.yy412<<16; yygotominor.yy47.mask = 0xff0000; }
 90247         -        break;
 90248         -      case 78: /* refact ::= SET NULL */
        92025  +      case 77: /* refact ::= SET NULL */
 90249  92026   { yygotominor.yy412 = OE_SetNull; }
 90250  92027           break;
 90251         -      case 79: /* refact ::= SET DEFAULT */
        92028  +      case 78: /* refact ::= SET DEFAULT */
 90252  92029   { yygotominor.yy412 = OE_SetDflt; }
 90253  92030           break;
 90254         -      case 80: /* refact ::= CASCADE */
        92031  +      case 79: /* refact ::= CASCADE */
 90255  92032   { yygotominor.yy412 = OE_Cascade; }
 90256  92033           break;
 90257         -      case 81: /* refact ::= RESTRICT */
        92034  +      case 80: /* refact ::= RESTRICT */
 90258  92035   { yygotominor.yy412 = OE_Restrict; }
 90259  92036           break;
 90260         -      case 82: /* defer_subclause ::= NOT DEFERRABLE init_deferred_pred_opt */
 90261         -      case 83: /* defer_subclause ::= DEFERRABLE init_deferred_pred_opt */ yytestcase(yyruleno==83);
        92037  +      case 81: /* refact ::= NO ACTION */
        92038  +{ yygotominor.yy412 = OE_None; }
        92039  +        break;
        92040  +      case 83: /* defer_subclause ::= DEFERRABLE init_deferred_pred_opt */
 90262  92041         case 98: /* defer_subclause_opt ::= defer_subclause */ yytestcase(yyruleno==98);
 90263  92042         case 100: /* onconf ::= ON CONFLICT resolvetype */ yytestcase(yyruleno==100);
 90264  92043         case 103: /* resolvetype ::= raisetype */ yytestcase(yyruleno==103);
 90265  92044   {yygotominor.yy412 = yymsp[0].minor.yy412;}
 90266  92045           break;
 90267  92046         case 87: /* conslist_opt ::= */
 90268  92047   {yygotominor.yy0.n = 0; yygotominor.yy0.z = 0;}
................................................................................
 90688  92467     yygotominor.yy22.zEnd = yymsp[-1].minor.yy22.zEnd;
 90689  92468     if( yygotominor.yy22.pExpr ) yygotominor.yy22.pExpr->flags |= EP_InfixFunc;
 90690  92469   }
 90691  92470           break;
 90692  92471         case 214: /* expr ::= expr ISNULL|NOTNULL */
 90693  92472   {spanUnaryPostfix(&yygotominor.yy22,pParse,yymsp[0].major,&yymsp[-1].minor.yy22,&yymsp[0].minor.yy0);}
 90694  92473           break;
 90695         -      case 215: /* expr ::= expr IS NULL */
 90696         -{spanUnaryPostfix(&yygotominor.yy22,pParse,TK_ISNULL,&yymsp[-2].minor.yy22,&yymsp[0].minor.yy0);}
 90697         -        break;
 90698         -      case 216: /* expr ::= expr NOT NULL */
        92474  +      case 215: /* expr ::= expr NOT NULL */
 90699  92475   {spanUnaryPostfix(&yygotominor.yy22,pParse,TK_NOTNULL,&yymsp[-2].minor.yy22,&yymsp[0].minor.yy0);}
 90700  92476           break;
 90701         -      case 217: /* expr ::= expr IS NOT NULL */
 90702         -{spanUnaryPostfix(&yygotominor.yy22,pParse,TK_NOTNULL,&yymsp[-3].minor.yy22,&yymsp[0].minor.yy0);}
        92477  +      case 216: /* expr ::= expr IS expr */
        92478  +{
        92479  +  spanBinaryExpr(&yygotominor.yy22,pParse,TK_IS,&yymsp[-2].minor.yy22,&yymsp[0].minor.yy22);
        92480  +  if( pParse->db->mallocFailed==0  && yymsp[0].minor.yy22.pExpr->op==TK_NULL ){
        92481  +    yygotominor.yy22.pExpr->op = TK_ISNULL;
        92482  +  }
        92483  +}
        92484  +        break;
        92485  +      case 217: /* expr ::= expr IS NOT expr */
        92486  +{
        92487  +  spanBinaryExpr(&yygotominor.yy22,pParse,TK_ISNOT,&yymsp[-3].minor.yy22,&yymsp[0].minor.yy22);
        92488  +  if( pParse->db->mallocFailed==0  && yymsp[0].minor.yy22.pExpr->op==TK_NULL ){
        92489  +    yygotominor.yy22.pExpr->op = TK_NOTNULL;
        92490  +  }
        92491  +}
 90703  92492           break;
 90704  92493         case 218: /* expr ::= NOT expr */
 90705  92494         case 219: /* expr ::= BITNOT expr */ yytestcase(yyruleno==219);
 90706  92495   {spanUnaryPrefix(&yygotominor.yy22,pParse,yymsp[-1].major,&yymsp[0].minor.yy22,&yymsp[-1].minor.yy0);}
 90707  92496           break;
 90708  92497         case 220: /* expr ::= MINUS expr */
 90709  92498   {spanUnaryPrefix(&yygotominor.yy22,pParse,TK_UMINUS,&yymsp[0].minor.yy22,&yymsp[-1].minor.yy0);}
................................................................................
 91353  93142   ** The code in this file implements a function that determines whether
 91354  93143   ** or not a given identifier is really an SQL keyword.  The same thing
 91355  93144   ** might be implemented more directly using a hand-written hash table.
 91356  93145   ** But by using this automatically generated code, the size of the code
 91357  93146   ** is substantially reduced.  This is important for embedded applications
 91358  93147   ** on platforms with limited memory.
 91359  93148   */
 91360         -/* Hash score: 135 */
        93149  +/* Hash score: 139 */
 91361  93150   static int keywordCode(const char *z, int n){
 91362         -  /* zText[] encodes 685 bytes of keywords in 452 bytes */
        93151  +  /* zText[] encodes 695 bytes of keywords in 452 bytes */
 91363  93152     /*   BEFOREIGNOREGEXPLAINDEXEDESCAPEACHECKEYCONSTRAINTERSECTABLEFT      */
 91364  93153     /*   HENDEFERRABLELSELECTRANSACTIONATURALIKEXCEPTRIGGERAISEXCLUSIVE     */
 91365  93154     /*   XISTSAVEPOINTOFFSETEMPORARYUNIQUERYBEGINNEREFERENCESBETWEEN        */
 91366  93155     /*   OTNULLIMITCASCADELETECASECOLLATECREATECURRENT_DATEGROUPDATE        */
 91367  93156     /*   IMMEDIATEJOINSERTMATCHAVINGLOBYPLANDEFAULTRELEASEVALUESWHEN        */
 91368  93157     /*   WHERESTRICTABORTAFTERIGHTCASTCOMMITCROSSCURRENT_TIMESTAMP          */
 91369  93158     /*   RIMARYDEFERREDISTINCTDROPFAILFROMFULLIFINSTEADISNULLORDER          */
................................................................................
 91393  93182       'D','R','O','P','F','A','I','L','F','R','O','M','F','U','L','L','I','F',
 91394  93183       'I','N','S','T','E','A','D','I','S','N','U','L','L','O','R','D','E','R',
 91395  93184       'O','L','L','B','A','C','K','O','U','T','E','R','O','W','U','N','I','O',
 91396  93185       'N','U','S','I','N','G','V','I','E','W','I','N','I','T','I','A','L','L',
 91397  93186       'Y',
 91398  93187     };
 91399  93188     static const unsigned char aHash[127] = {
 91400         -      63,  88,  97,  61,   0,  39,   0,   0,  73,   0,  64,   0,   0,
 91401         -      37,   4,  68,  94,   0,  79,  74,  65,  93,   0,  23,   0,   0,
 91402         -      33,   0, 100,  76,   0,  19,  35,   0,   1,   0,   0,  56,  57,
 91403         -       0,  55,  11,   0,  43,  69,  85,   0,  99,  84,   0,   0,  40,
 91404         -       0,  86,  21,   0,   0,   0, 102,   0,  20,   0,  10,  66,  22,
 91405         -       0,   0,   0, 104,  89,  47, 103,  44,  12,  59,   0,  70,   0,
 91406         -      83,  28,   0,  82,   0,   0,   0,  80,   2,  81,  78,  92,   6,
 91407         -      14,  91,   0,  77,   0,  72,  71,  67,  30,   0, 101,   0,  95,
 91408         -      49,  41,  51,   0,   0,  16,  34,   0,  98,   0,  15,   0,   0,
 91409         -      25,   0,  75,  45,  50,   0,  17,  48,   0,  60,
        93189  +      65,  90,  99,  63,   0,  40,   0,   0,  75,   0,  66,   0,   0,
        93190  +      38,   4,  70,  96,   0,  81,  76,  67,  95,   0,  23,   0,   0,
        93191  +      34,   0, 102,  78,   0,  19,  36,   0,   1,   0,   0,  58,  59,
        93192  +       0,  57,  11,   0,  44,  71,  87,   0, 101,  86,   0,   0,  41,
        93193  +       0,  88,  21,   0,   0,   0, 104,   0,  20,   0,  10,  68,  22,
        93194  +       0,   0,   0, 106,  91,  48, 105,  45,  25,  61,   0,  72,   0,
        93195  +      85,  29,   0,  84,   0,   0,   0,  82,   2,  83,  80,  94,   6,
        93196  +      14,  93,   0,  79,   0,  74,  73,  69,  31,   0, 103,   0,  97,
        93197  +      51,  42,  53,   0,   0,  16,  35,   0, 100,   0,  15,   0,   0,
        93198  +      26,   0,  77,  50,  52,   0,  17,  49,   0,  62,
 91410  93199     };
 91411         -  static const unsigned char aNext[104] = {
        93200  +  static const unsigned char aNext[106] = {
 91412  93201          0,   0,   0,   0,   0,   0,   0,   0,   0,   9,   0,   0,   0,
 91413         -       0,   0,   0,   0,   0,   5,   0,   0,   0,   0,   0,   0,   0,
        93202  +       0,   0,   0,   0,   0,   5,   0,   0,   0,   0,   0,  12,   0,
 91414  93203          0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
 91415         -      31,  18,   0,   0,   0,   0,  27,   0,  26,   0,   0,   0,   0,
 91416         -       0,   0,   0,  52,   0,   0,   8,  42,   0,  36,   0,   0,  38,
 91417         -       0,   0,   0,   0,  32,   0,   0,   0,   0,   0,   0,   0,   0,
 91418         -      24,   0,   0,   0,   3,  58,   0,   0,  13,   0,  87,   0,   0,
 91419         -      90,   0,   7,  62,   0,  29,  96,  53,  46,   0,  54,   0,   0,
        93204  +       0,  32,  18,   0,   0,   0,   0,  28,   0,  27,  46,   0,   0,
        93205  +       0,   0,   0,   0,   0,  54,   0,   0,   8,  43,   0,  37,   0,
        93206  +       0,  39,   0,   0,   0,   0,  33,   0,   0,   0,   0,   0,   0,
        93207  +       0,   0,  24,   0,   0,   0,   3,  60,   0,   0,  13,   0,  89,
        93208  +       0,   0,  92,   0,   7,  64,   0,  30,  98,  55,  47,   0,  56,
        93209  +       0,   0,
 91420  93210     };
 91421         -  static const unsigned char aLen[104] = {
        93211  +  static const unsigned char aLen[106] = {
 91422  93212          6,   7,   3,   6,   6,   7,   7,   5,   4,   6,   4,   5,   3,
 91423         -      10,   9,   2,   5,   4,   4,   3,  10,   4,   6,  11,   2,   7,
 91424         -       4,   6,   7,   5,   9,   6,   9,   4,   2,   6,   2,   3,   4,
 91425         -       9,   2,   6,   5,   5,   5,  10,   7,   7,   3,   4,   5,   7,
 91426         -       3,   6,   4,   7,   6,  12,   5,   6,   9,   4,   6,   5,   6,
 91427         -       4,   2,   4,   3,   7,   7,   2,   6,   4,   5,   8,   5,   5,
 91428         -       5,   4,   6,   5,  17,  12,   7,   8,   8,   2,   4,   4,   4,
 91429         -       4,   2,   7,   6,   5,   8,   5,   3,   5,   5,   4,   9,   3,
        93213  +      10,   9,   2,   5,   4,   4,   3,  10,   4,   6,  11,   6,   2,
        93214  +       7,   4,   6,   7,   5,   9,   6,   9,   4,   2,   6,   2,   3,
        93215  +       4,   9,   2,   6,   5,   5,   5,  10,   7,   7,   2,   3,   4,
        93216  +       5,   7,   3,   6,   4,   7,   6,  12,   5,   6,   9,   4,   6,
        93217  +       5,   6,   4,   2,   4,   3,   7,   7,   2,   6,   4,   5,   8,
        93218  +       5,   5,   5,   4,   6,   5,  17,  12,   7,   8,   8,   2,   4,
        93219  +       4,   4,   4,   2,   7,   6,   5,   8,   5,   3,   5,   5,   4,
        93220  +       9,   3,
 91430  93221     };
 91431         -  static const unsigned short int aOffset[104] = {
        93222  +  static const unsigned short int aOffset[106] = {
 91432  93223          0,   2,   2,   6,  10,  13,  18,  18,  24,  25,  30,  32,  36,
 91433         -      39,  46,  46,  54,  57,  60,  62,  64,  73,  75,  80,  89,  90,
 91434         -      96,  99, 104, 110, 114, 122, 127, 133, 135, 136, 136, 139, 141,
 91435         -     141, 145, 150, 153, 158, 161, 165, 175, 181, 181, 184, 187, 192,
 91436         -     193, 197, 203, 207, 214, 220, 232, 235, 241, 250, 252, 258, 262,
 91437         -     267, 270, 272, 274, 276, 283, 287, 290, 296, 300, 303, 311, 316,
 91438         -     320, 325, 329, 335, 340, 340, 356, 363, 370, 371, 378, 382, 386,
 91439         -     390, 394, 396, 403, 409, 413, 421, 425, 428, 433, 438, 442, 447,
        93224  +      39,  46,  46,  54,  57,  60,  62,  64,  73,  75,  80,  85,  89,
        93225  +      90,  96,  99, 104, 110, 114, 122, 127, 133, 135, 136, 136, 139,
        93226  +     141, 141, 145, 150, 153, 158, 161, 165, 175, 181, 181, 181, 184,
        93227  +     187, 192, 193, 197, 203, 207, 214, 220, 232, 235, 241, 250, 252,
        93228  +     258, 262, 267, 270, 272, 274, 276, 283, 287, 290, 296, 300, 303,
        93229  +     311, 316, 320, 325, 329, 335, 340, 340, 356, 363, 370, 371, 378,
        93230  +     382, 386, 390, 394, 396, 403, 409, 413, 421, 425, 428, 433, 438,
        93231  +     442, 447,
 91440  93232     };
 91441         -  static const unsigned char aCode[104] = {
        93233  +  static const unsigned char aCode[106] = {
 91442  93234       TK_BEFORE,     TK_FOREIGN,    TK_FOR,        TK_IGNORE,     TK_LIKE_KW,    
 91443  93235       TK_EXPLAIN,    TK_INDEXED,    TK_INDEX,      TK_DESC,       TK_ESCAPE,     
 91444  93236       TK_EACH,       TK_CHECK,      TK_KEY,        TK_CONSTRAINT, TK_INTERSECT,  
 91445  93237       TK_IN,         TK_TABLE,      TK_JOIN_KW,    TK_THEN,       TK_END,        
 91446         -    TK_DEFERRABLE, TK_ELSE,       TK_SELECT,     TK_TRANSACTION,TK_ON,         
 91447         -    TK_JOIN_KW,    TK_LIKE_KW,    TK_EXCEPT,     TK_TRIGGER,    TK_RAISE,      
 91448         -    TK_EXCLUSIVE,  TK_EXISTS,     TK_SAVEPOINT,  TK_INTO,       TK_TO,         
 91449         -    TK_OFFSET,     TK_OF,         TK_SET,        TK_TEMP,       TK_TEMP,       
 91450         -    TK_OR,         TK_UNIQUE,     TK_QUERY,      TK_BEGIN,      TK_JOIN_KW,    
 91451         -    TK_REFERENCES, TK_BETWEEN,    TK_NOTNULL,    TK_NOT,        TK_NULL,       
 91452         -    TK_LIMIT,      TK_CASCADE,    TK_ASC,        TK_DELETE,     TK_CASE,       
 91453         -    TK_COLLATE,    TK_CREATE,     TK_CTIME_KW,   TK_GROUP,      TK_UPDATE,     
 91454         -    TK_IMMEDIATE,  TK_JOIN,       TK_INSERT,     TK_MATCH,      TK_HAVING,     
 91455         -    TK_LIKE_KW,    TK_BY,         TK_PLAN,       TK_AND,        TK_DEFAULT,    
 91456         -    TK_RELEASE,    TK_AS,         TK_VALUES,     TK_WHEN,       TK_WHERE,      
 91457         -    TK_RESTRICT,   TK_ABORT,      TK_AFTER,      TK_JOIN_KW,    TK_CAST,       
 91458         -    TK_COMMIT,     TK_JOIN_KW,    TK_CTIME_KW,   TK_CTIME_KW,   TK_PRIMARY,    
 91459         -    TK_DEFERRED,   TK_DISTINCT,   TK_IS,         TK_DROP,       TK_FAIL,       
 91460         -    TK_FROM,       TK_JOIN_KW,    TK_IF,         TK_INSTEAD,    TK_ISNULL,     
 91461         -    TK_ORDER,      TK_ROLLBACK,   TK_JOIN_KW,    TK_ROW,        TK_UNION,      
 91462         -    TK_USING,      TK_VIEW,       TK_INITIALLY,  TK_ALL,        
        93238  +    TK_DEFERRABLE, TK_ELSE,       TK_SELECT,     TK_TRANSACTION,TK_ACTION,     
        93239  +    TK_ON,         TK_JOIN_KW,    TK_LIKE_KW,    TK_EXCEPT,     TK_TRIGGER,    
        93240  +    TK_RAISE,      TK_EXCLUSIVE,  TK_EXISTS,     TK_SAVEPOINT,  TK_INTO,       
        93241  +    TK_TO,         TK_OFFSET,     TK_OF,         TK_SET,        TK_TEMP,       
        93242  +    TK_TEMP,       TK_OR,         TK_UNIQUE,     TK_QUERY,      TK_BEGIN,      
        93243  +    TK_JOIN_KW,    TK_REFERENCES, TK_BETWEEN,    TK_NOTNULL,    TK_NO,         
        93244  +    TK_NOT,        TK_NULL,       TK_LIMIT,      TK_CASCADE,    TK_ASC,        
        93245  +    TK_DELETE,     TK_CASE,       TK_COLLATE,    TK_CREATE,     TK_CTIME_KW,   
        93246  +    TK_GROUP,      TK_UPDATE,     TK_IMMEDIATE,  TK_JOIN,       TK_INSERT,     
        93247  +    TK_MATCH,      TK_HAVING,     TK_LIKE_KW,    TK_BY,         TK_PLAN,       
        93248  +    TK_AND,        TK_DEFAULT,    TK_RELEASE,    TK_AS,         TK_VALUES,     
        93249  +    TK_WHEN,       TK_WHERE,      TK_RESTRICT,   TK_ABORT,      TK_AFTER,      
        93250  +    TK_JOIN_KW,    TK_CAST,       TK_COMMIT,     TK_JOIN_KW,    TK_CTIME_KW,   
        93251  +    TK_CTIME_KW,   TK_PRIMARY,    TK_DEFERRED,   TK_DISTINCT,   TK_IS,         
        93252  +    TK_DROP,       TK_FAIL,       TK_FROM,       TK_JOIN_KW,    TK_IF,         
        93253  +    TK_INSTEAD,    TK_ISNULL,     TK_ORDER,      TK_ROLLBACK,   TK_JOIN_KW,    
        93254  +    TK_ROW,        TK_UNION,      TK_USING,      TK_VIEW,       TK_INITIALLY,  
        93255  +    TK_ALL,        
 91463  93256     };
 91464  93257     int h, i;
 91465  93258     if( n<2 ) return TK_ID;
 91466  93259     h = ((charMap(z[0])*4) ^
 91467  93260         (charMap(z[n-1])*3) ^
 91468  93261         n) % 127;
 91469  93262     for(i=((int)aHash[h])-1; i>=0; i=((int)aNext[i])-1){
................................................................................
 91488  93281         testcase( i==17 ); /* LEFT */
 91489  93282         testcase( i==18 ); /* THEN */
 91490  93283         testcase( i==19 ); /* END */
 91491  93284         testcase( i==20 ); /* DEFERRABLE */
 91492  93285         testcase( i==21 ); /* ELSE */
 91493  93286         testcase( i==22 ); /* SELECT */
 91494  93287         testcase( i==23 ); /* TRANSACTION */
 91495         -      testcase( i==24 ); /* ON */
 91496         -      testcase( i==25 ); /* NATURAL */
 91497         -      testcase( i==26 ); /* LIKE */
 91498         -      testcase( i==27 ); /* EXCEPT */
 91499         -      testcase( i==28 ); /* TRIGGER */
 91500         -      testcase( i==29 ); /* RAISE */
 91501         -      testcase( i==30 ); /* EXCLUSIVE */
 91502         -      testcase( i==31 ); /* EXISTS */
 91503         -      testcase( i==32 ); /* SAVEPOINT */
 91504         -      testcase( i==33 ); /* INTO */
 91505         -      testcase( i==34 ); /* TO */
 91506         -      testcase( i==35 ); /* OFFSET */
 91507         -      testcase( i==36 ); /* OF */
 91508         -      testcase( i==37 ); /* SET */
 91509         -      testcase( i==38 ); /* TEMP */
 91510         -      testcase( i==39 ); /* TEMPORARY */
 91511         -      testcase( i==40 ); /* OR */
 91512         -      testcase( i==41 ); /* UNIQUE */
 91513         -      testcase( i==42 ); /* QUERY */
 91514         -      testcase( i==43 ); /* BEGIN */
 91515         -      testcase( i==44 ); /* INNER */
 91516         -      testcase( i==45 ); /* REFERENCES */
 91517         -      testcase( i==46 ); /* BETWEEN */
 91518         -      testcase( i==47 ); /* NOTNULL */
 91519         -      testcase( i==48 ); /* NOT */
 91520         -      testcase( i==49 ); /* NULL */
 91521         -      testcase( i==50 ); /* LIMIT */
 91522         -      testcase( i==51 ); /* CASCADE */
 91523         -      testcase( i==52 ); /* ASC */
 91524         -      testcase( i==53 ); /* DELETE */
 91525         -      testcase( i==54 ); /* CASE */
 91526         -      testcase( i==55 ); /* COLLATE */
 91527         -      testcase( i==56 ); /* CREATE */
 91528         -      testcase( i==57 ); /* CURRENT_DATE */
 91529         -      testcase( i==58 ); /* GROUP */
 91530         -      testcase( i==59 ); /* UPDATE */
 91531         -      testcase( i==60 ); /* IMMEDIATE */
 91532         -      testcase( i==61 ); /* JOIN */
 91533         -      testcase( i==62 ); /* INSERT */
 91534         -      testcase( i==63 ); /* MATCH */
 91535         -      testcase( i==64 ); /* HAVING */
 91536         -      testcase( i==65 ); /* GLOB */
 91537         -      testcase( i==66 ); /* BY */
 91538         -      testcase( i==67 ); /* PLAN */
 91539         -      testcase( i==68 ); /* AND */
 91540         -      testcase( i==69 ); /* DEFAULT */
 91541         -      testcase( i==70 ); /* RELEASE */
 91542         -      testcase( i==71 ); /* AS */
 91543         -      testcase( i==72 ); /* VALUES */
 91544         -      testcase( i==73 ); /* WHEN */
 91545         -      testcase( i==74 ); /* WHERE */
 91546         -      testcase( i==75 ); /* RESTRICT */
 91547         -      testcase( i==76 ); /* ABORT */
 91548         -      testcase( i==77 ); /* AFTER */
 91549         -      testcase( i==78 ); /* RIGHT */
 91550         -      testcase( i==79 ); /* CAST */
 91551         -      testcase( i==80 ); /* COMMIT */
 91552         -      testcase( i==81 ); /* CROSS */
 91553         -      testcase( i==82 ); /* CURRENT_TIMESTAMP */
 91554         -      testcase( i==83 ); /* CURRENT_TIME */
 91555         -      testcase( i==84 ); /* PRIMARY */
 91556         -      testcase( i==85 ); /* DEFERRED */
 91557         -      testcase( i==86 ); /* DISTINCT */
 91558         -      testcase( i==87 ); /* IS */
 91559         -      testcase( i==88 ); /* DROP */
 91560         -      testcase( i==89 ); /* FAIL */
 91561         -      testcase( i==90 ); /* FROM */
 91562         -      testcase( i==91 ); /* FULL */
 91563         -      testcase( i==92 ); /* IF */
 91564         -      testcase( i==93 ); /* INSTEAD */
 91565         -      testcase( i==94 ); /* ISNULL */
 91566         -      testcase( i==95 ); /* ORDER */
 91567         -      testcase( i==96 ); /* ROLLBACK */
 91568         -      testcase( i==97 ); /* OUTER */
 91569         -      testcase( i==98 ); /* ROW */
 91570         -      testcase( i==99 ); /* UNION */
 91571         -      testcase( i==100 ); /* USING */
 91572         -      testcase( i==101 ); /* VIEW */
 91573         -      testcase( i==102 ); /* INITIALLY */
 91574         -      testcase( i==103 ); /* ALL */
        93288  +      testcase( i==24 ); /* ACTION */
        93289  +      testcase( i==25 ); /* ON */
        93290  +      testcase( i==26 ); /* NATURAL */
        93291  +      testcase( i==27 ); /* LIKE */
        93292  +      testcase( i==28 ); /* EXCEPT */
        93293  +      testcase( i==29 ); /* TRIGGER */
        93294  +      testcase( i==30 ); /* RAISE */
        93295  +      testcase( i==31 ); /* EXCLUSIVE */
        93296  +      testcase( i==32 ); /* EXISTS */
        93297  +      testcase( i==33 ); /* SAVEPOINT */
        93298  +      testcase( i==34 ); /* INTO */
        93299  +      testcase( i==35 ); /* TO */
        93300  +      testcase( i==36 ); /* OFFSET */
        93301  +      testcase( i==37 ); /* OF */
        93302  +      testcase( i==38 ); /* SET */
        93303  +      testcase( i==39 ); /* TEMP */
        93304  +      testcase( i==40 ); /* TEMPORARY */
        93305  +      testcase( i==41 ); /* OR */
        93306  +      testcase( i==42 ); /* UNIQUE */
        93307  +      testcase( i==43 ); /* QUERY */
        93308  +      testcase( i==44 ); /* BEGIN */
        93309  +      testcase( i==45 ); /* INNER */
        93310  +      testcase( i==46 ); /* REFERENCES */
        93311  +      testcase( i==47 ); /* BETWEEN */
        93312  +      testcase( i==48 ); /* NOTNULL */
        93313  +      testcase( i==49 ); /* NO */
        93314  +      testcase( i==50 ); /* NOT */
        93315  +      testcase( i==51 ); /* NULL */
        93316  +      testcase( i==52 ); /* LIMIT */
        93317  +      testcase( i==53 ); /* CASCADE */
        93318  +      testcase( i==54 ); /* ASC */
        93319  +      testcase( i==55 ); /* DELETE */
        93320  +      testcase( i==56 ); /* CASE */
        93321  +      testcase( i==57 ); /* COLLATE */
        93322  +      testcase( i==58 ); /* CREATE */
        93323  +      testcase( i==59 ); /* CURRENT_DATE */
        93324  +      testcase( i==60 ); /* GROUP */
        93325  +      testcase( i==61 ); /* UPDATE */
        93326  +      testcase( i==62 ); /* IMMEDIATE */
        93327  +      testcase( i==63 ); /* JOIN */
        93328  +      testcase( i==64 ); /* INSERT */
        93329  +      testcase( i==65 ); /* MATCH */
        93330  +      testcase( i==66 ); /* HAVING */
        93331  +      testcase( i==67 ); /* GLOB */
        93332  +      testcase( i==68 ); /* BY */
        93333  +      testcase( i==69 ); /* PLAN */
        93334  +      testcase( i==70 ); /* AND */
        93335  +      testcase( i==71 ); /* DEFAULT */
        93336  +      testcase( i==72 ); /* RELEASE */
        93337  +      testcase( i==73 ); /* AS */
        93338  +      testcase( i==74 ); /* VALUES */
        93339  +      testcase( i==75 ); /* WHEN */
        93340  +      testcase( i==76 ); /* WHERE */
        93341  +      testcase( i==77 ); /* RESTRICT */
        93342  +      testcase( i==78 ); /* ABORT */
        93343  +      testcase( i==79 ); /* AFTER */
        93344  +      testcase( i==80 ); /* RIGHT */
        93345  +      testcase( i==81 ); /* CAST */
        93346  +      testcase( i==82 ); /* COMMIT */
        93347  +      testcase( i==83 ); /* CROSS */
        93348  +      testcase( i==84 ); /* CURRENT_TIMESTAMP */
        93349  +      testcase( i==85 ); /* CURRENT_TIME */
        93350  +      testcase( i==86 ); /* PRIMARY */
        93351  +      testcase( i==87 ); /* DEFERRED */
        93352  +      testcase( i==88 ); /* DISTINCT */
        93353  +      testcase( i==89 ); /* IS */
        93354  +      testcase( i==90 ); /* DROP */
        93355  +      testcase( i==91 ); /* FAIL */
        93356  +      testcase( i==92 ); /* FROM */
        93357  +      testcase( i==93 ); /* FULL */
        93358  +      testcase( i==94 ); /* IF */
        93359  +      testcase( i==95 ); /* INSTEAD */
        93360  +      testcase( i==96 ); /* ISNULL */
        93361  +      testcase( i==97 ); /* ORDER */
        93362  +      testcase( i==98 ); /* ROLLBACK */
        93363  +      testcase( i==99 ); /* OUTER */
        93364  +      testcase( i==100 ); /* ROW */
        93365  +      testcase( i==101 ); /* UNION */
        93366  +      testcase( i==102 ); /* USING */
        93367  +      testcase( i==103 ); /* VIEW */
        93368  +      testcase( i==104 ); /* INITIALLY */
        93369  +      testcase( i==105 ); /* ALL */
 91575  93370         return aCode[i];
 91576  93371       }
 91577  93372     }
 91578  93373     return TK_ID;
 91579  93374   }
 91580  93375   SQLITE_PRIVATE int sqlite3KeywordCode(const unsigned char *z, int n){
 91581  93376     return keywordCode((char*)z, n);
................................................................................
 93148  94943     sqlite3VtabRollback(db);
 93149  94944     sqlite3EndBenignMalloc();
 93150  94945   
 93151  94946     if( db->flags&SQLITE_InternChanges ){
 93152  94947       sqlite3ExpirePreparedStatements(db);
 93153  94948       sqlite3ResetInternalSchema(db, 0);
 93154  94949     }
        94950  +
        94951  +  /* Any deferred constraint violations have now been resolved. */
        94952  +  db->nDeferredCons = 0;
 93155  94953   
 93156  94954     /* If one has been configured, invoke the rollback-hook callback */
 93157  94955     if( db->xRollbackCallback && (inTrans || !db->autoCommit) ){
 93158  94956       db->xRollbackCallback(db->pRollbackArg);
 93159  94957     }
 93160  94958   }
 93161  94959   
................................................................................
106410 108208   
106411 108209     /* Pick two "seed" cells from the array of cells. The algorithm used
106412 108210     ** here is the LinearPickSeeds algorithm from Gutman[1984]. The 
106413 108211     ** indices of the two seed cells in the array are stored in local
106414 108212     ** variables iLeftSeek and iRightSeed.
106415 108213     */
106416 108214     for(i=0; i<pRtree->nDim; i++){
106417         -    float x1 = aCell[0].aCoord[i*2];
106418         -    float x2 = aCell[0].aCoord[i*2+1];
       108215  +    float x1 = DCOORD(aCell[0].aCoord[i*2]);
       108216  +    float x2 = DCOORD(aCell[0].aCoord[i*2+1]);
106419 108217       float x3 = x1;
106420 108218       float x4 = x2;
106421 108219       int jj;
106422 108220   
106423 108221       int iCellLeft = 0;
106424 108222       int iCellRight = 0;
106425 108223   
106426 108224       for(jj=1; jj<nCell; jj++){
106427         -      float left = aCell[jj].aCoord[i*2];
106428         -      float right = aCell[jj].aCoord[i*2+1];
       108225  +      float left = DCOORD(aCell[jj].aCoord[i*2]);
       108226  +      float right = DCOORD(aCell[jj].aCoord[i*2+1]);
106429 108227   
106430 108228         if( left<x1 ) x1 = left;
106431 108229         if( right>x4 ) x4 = right;
106432 108230         if( left>x3 ){
106433 108231           x3 = left;
106434 108232           iCellRight = jj;
106435 108233         }
................................................................................
106779 108577   ){
106780 108578     int iLeftSeed = 0;
106781 108579     int iRightSeed = 1;
106782 108580     int *aiUsed;
106783 108581     int i;
106784 108582   
106785 108583     aiUsed = sqlite3_malloc(sizeof(int)*nCell);
       108584  +  if( !aiUsed ){
       108585  +    return SQLITE_NOMEM;
       108586  +  }
106786 108587     memset(aiUsed, 0, sizeof(int)*nCell);
106787 108588   
106788 108589     PickSeeds(pRtree, aCell, nCell, &iLeftSeed, &iRightSeed);
106789 108590   
106790 108591     memcpy(pBboxLeft, &aCell[iLeftSeed], sizeof(RtreeCell));
106791 108592     memcpy(pBboxRight, &aCell[iRightSeed], sizeof(RtreeCell));
106792 108593     nodeInsertCell(pRtree, pLeft, &aCell[iLeftSeed]);

Changes to src/sqlite3.h.

   115    115   **
   116    116   ** See also: [sqlite3_libversion()],
   117    117   ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
   118    118   ** [sqlite_version()] and [sqlite_source_id()].
   119    119   **
   120    120   ** Requirements: [H10011] [H10014]
   121    121   */
   122         -#define SQLITE_VERSION        "3.6.18"
   123         -#define SQLITE_VERSION_NUMBER 3006018
   124         -#define SQLITE_SOURCE_ID      "2009-09-10 22:30:54 3ea10434434d27a1300ba2b58e2c47c54909f4ff"
          122  +#define SQLITE_VERSION        "3.6.19"
          123  +#define SQLITE_VERSION_NUMBER 3006019
          124  +#define SQLITE_SOURCE_ID      "2009-10-13 22:47:14 ac19dbc6a208be2313e47cbcc0b2a0d8d04bae4c"
   125    125   
   126    126   /*
   127    127   ** CAPI3REF: Run-Time Library Version Numbers {H10020} <S60100>
   128    128   ** KEYWORDS: sqlite3_version
   129    129   **
   130    130   ** These interfaces provide the same information as the [SQLITE_VERSION],
   131    131   ** [SQLITE_VERSION_NUMBER], and [SQLITE_SOURCE_ID] #defines in the header,
................................................................................
  1312   1312   ** CAPI3REF: Count The Number Of Rows Modified {H12240} <S10600>
  1313   1313   **
  1314   1314   ** This function returns the number of database rows that were changed
  1315   1315   ** or inserted or deleted by the most recently completed SQL statement
  1316   1316   ** on the [database connection] specified by the first parameter.
  1317   1317   ** Only changes that are directly specified by the [INSERT], [UPDATE],
  1318   1318   ** or [DELETE] statement are counted.  Auxiliary changes caused by
  1319         -** triggers are not counted. Use the [sqlite3_total_changes()] function
  1320         -** to find the total number of changes including changes caused by triggers.
         1319  +** triggers or [foreign key actions] are not counted. Use the
         1320  +** [sqlite3_total_changes()] function to find the total number of changes
         1321  +** including changes caused by triggers and foreign key actions.
  1321   1322   **
  1322   1323   ** Changes to a view that are simulated by an [INSTEAD OF trigger]
  1323   1324   ** are not counted.  Only real table changes are counted.
  1324   1325   **
  1325   1326   ** A "row change" is a change to a single row of a single table
  1326   1327   ** caused by an INSERT, DELETE, or UPDATE statement.  Rows that
  1327   1328   ** are changed as side effects of [REPLACE] constraint resolution,
................................................................................
  1365   1366   SQLITE_API int sqlite3_changes(sqlite3*);
  1366   1367   
  1367   1368   /*
  1368   1369   ** CAPI3REF: Total Number Of Rows Modified {H12260} <S10600>
  1369   1370   **
  1370   1371   ** This function returns the number of row changes caused by [INSERT],
  1371   1372   ** [UPDATE] or [DELETE] statements since the [database connection] was opened.
  1372         -** The count includes all changes from all 
  1373         -** [CREATE TRIGGER | trigger] contexts.  However,
         1373  +** The count includes all changes from all [CREATE TRIGGER | trigger] 
         1374  +** contexts and changes made by [foreign key actions]. However,
  1374   1375   ** the count does not include changes used to implement [REPLACE] constraints,
  1375   1376   ** do rollbacks or ABORT processing, or [DROP TABLE] processing.  The
  1376   1377   ** count does not include rows of views that fire an [INSTEAD OF trigger],
  1377   1378   ** though if the INSTEAD OF trigger makes changes of its own, those changes 
  1378   1379   ** are counted.
  1379   1380   ** The changes are counted as soon as the statement that makes them is
  1380   1381   ** completed (when the statement handle is passed to [sqlite3_reset()] or
................................................................................
  4507   4508   **
  4508   4509   ** <pre>
  4509   4510   **     SELECT zColumn FROM zDb.zTable WHERE [rowid] = iRow;
  4510   4511   ** </pre> {END}
  4511   4512   **
  4512   4513   ** If the flags parameter is non-zero, then the BLOB is opened for read
  4513   4514   ** and write access. If it is zero, the BLOB is opened for read access.
         4515  +** It is not possible to open a column that is part of an index or primary 
         4516  +** key for writing. ^If [foreign key constraints] are enabled, it is 
         4517  +** not possible to open a column that is part of a [child key] for writing.
  4514   4518   **
  4515   4519   ** Note that the database name is not the filename that contains
  4516   4520   ** the database but rather the symbolic name of the database that
  4517   4521   ** is assigned when the database is connected using [ATTACH].
  4518   4522   ** For the main database file, the database name is "main".
  4519   4523   ** For TEMP tables, the database name is "temp".
  4520   4524   **