drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1 | /* |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2 | ** 2001 September 15 |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 7 | ** May you do good and not evil. |
| 8 | ** May you find forgiveness for yourself and forgive others. |
| 9 | ** May you share freely, never taking more than you give. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 10 | ** |
| 11 | ************************************************************************* |
| 12 | ** An tokenizer for SQL |
| 13 | ** |
| 14 | ** This file contains C code that splits an SQL input string up into |
| 15 | ** individual tokens and sends those tokens one-by-one over to the |
| 16 | ** parser for analysis. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 17 | */ |
| 18 | #include "sqliteInt.h" |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 19 | #include <stdlib.h> |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 20 | |
drh | 34dcee6 | 2016-02-08 19:15:48 +0000 | [diff] [blame] | 21 | /* Character classes for tokenizing |
| 22 | ** |
| 23 | ** In the sqlite3GetToken() function, a switch() on aiClass[c] is implemented |
| 24 | ** using a lookup table, whereas a switch() directly on c uses a binary search. |
| 25 | ** The lookup table is much faster. To maximize speed, and to ensure that |
| 26 | ** a lookup table is used, all of the classes need to be small integers and |
| 27 | ** all of them need to be used within the switch. |
| 28 | */ |
drh | e96f361 | 2016-02-08 19:36:46 +0000 | [diff] [blame] | 29 | #define CC_X 0 /* The letter 'x', or start of BLOB literal */ |
drh | d1032f9 | 2020-11-27 20:56:16 +0000 | [diff] [blame] | 30 | #define CC_KYWD0 1 /* First letter of a keyword */ |
| 31 | #define CC_KYWD 2 /* Alphabetics or '_'. Usable in a keyword */ |
drh | 8974331 | 2016-02-08 02:30:50 +0000 | [diff] [blame] | 32 | #define CC_DIGIT 3 /* Digits */ |
| 33 | #define CC_DOLLAR 4 /* '$' */ |
| 34 | #define CC_VARALPHA 5 /* '@', '#', ':'. Alphabetic SQL variables */ |
| 35 | #define CC_VARNUM 6 /* '?'. Numeric SQL variables */ |
| 36 | #define CC_SPACE 7 /* Space characters */ |
| 37 | #define CC_QUOTE 8 /* '"', '\'', or '`'. String literals, quoted ids */ |
| 38 | #define CC_QUOTE2 9 /* '['. [...] style quoted ids */ |
| 39 | #define CC_PIPE 10 /* '|'. Bitwise OR or concatenate */ |
| 40 | #define CC_MINUS 11 /* '-'. Minus or SQL-style comment */ |
| 41 | #define CC_LT 12 /* '<'. Part of < or <= or <> */ |
| 42 | #define CC_GT 13 /* '>'. Part of > or >= */ |
| 43 | #define CC_EQ 14 /* '='. Part of = or == */ |
| 44 | #define CC_BANG 15 /* '!'. Part of != */ |
| 45 | #define CC_SLASH 16 /* '/'. / or c-style comment */ |
| 46 | #define CC_LP 17 /* '(' */ |
| 47 | #define CC_RP 18 /* ')' */ |
| 48 | #define CC_SEMI 19 /* ';' */ |
| 49 | #define CC_PLUS 20 /* '+' */ |
| 50 | #define CC_STAR 21 /* '*' */ |
| 51 | #define CC_PERCENT 22 /* '%' */ |
| 52 | #define CC_COMMA 23 /* ',' */ |
| 53 | #define CC_AND 24 /* '&' */ |
| 54 | #define CC_TILDA 25 /* '~' */ |
| 55 | #define CC_DOT 26 /* '.' */ |
drh | d1032f9 | 2020-11-27 20:56:16 +0000 | [diff] [blame] | 56 | #define CC_ID 27 /* unicode characters usable in IDs */ |
| 57 | #define CC_ILLEGAL 28 /* Illegal character */ |
| 58 | #define CC_NUL 29 /* 0x00 */ |
drh | ba9ebc2 | 2021-04-24 12:24:08 +0000 | [diff] [blame] | 59 | #define CC_BOM 30 /* First byte of UTF8 BOM: 0xEF 0xBB 0xBF */ |
drh | 8974331 | 2016-02-08 02:30:50 +0000 | [diff] [blame] | 60 | |
| 61 | static const unsigned char aiClass[] = { |
drh | 34dcee6 | 2016-02-08 19:15:48 +0000 | [diff] [blame] | 62 | #ifdef SQLITE_ASCII |
drh | 8974331 | 2016-02-08 02:30:50 +0000 | [diff] [blame] | 63 | /* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xa xb xc xd xe xf */ |
drh | d1032f9 | 2020-11-27 20:56:16 +0000 | [diff] [blame] | 64 | /* 0x */ 29, 28, 28, 28, 28, 28, 28, 28, 28, 7, 7, 28, 7, 7, 28, 28, |
| 65 | /* 1x */ 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, |
drh | 8974331 | 2016-02-08 02:30:50 +0000 | [diff] [blame] | 66 | /* 2x */ 7, 15, 8, 5, 4, 22, 24, 8, 17, 18, 21, 20, 23, 11, 26, 16, |
| 67 | /* 3x */ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 5, 19, 12, 14, 13, 6, |
| 68 | /* 4x */ 5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
drh | d1032f9 | 2020-11-27 20:56:16 +0000 | [diff] [blame] | 69 | /* 5x */ 1, 1, 1, 1, 1, 1, 1, 1, 0, 2, 2, 9, 28, 28, 28, 2, |
drh | 8974331 | 2016-02-08 02:30:50 +0000 | [diff] [blame] | 70 | /* 6x */ 8, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
drh | d1032f9 | 2020-11-27 20:56:16 +0000 | [diff] [blame] | 71 | /* 7x */ 1, 1, 1, 1, 1, 1, 1, 1, 0, 2, 2, 28, 10, 28, 25, 28, |
drh | ba9ebc2 | 2021-04-24 12:24:08 +0000 | [diff] [blame] | 72 | /* 8x */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, |
| 73 | /* 9x */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, |
| 74 | /* Ax */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, |
| 75 | /* Bx */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, |
| 76 | /* Cx */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, |
| 77 | /* Dx */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, |
| 78 | /* Ex */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 30, |
| 79 | /* Fx */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27 |
drh | 34dcee6 | 2016-02-08 19:15:48 +0000 | [diff] [blame] | 80 | #endif |
| 81 | #ifdef SQLITE_EBCDIC |
| 82 | /* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xa xb xc xd xe xf */ |
drh | d1032f9 | 2020-11-27 20:56:16 +0000 | [diff] [blame] | 83 | /* 0x */ 29, 28, 28, 28, 28, 7, 28, 28, 28, 28, 28, 28, 7, 7, 28, 28, |
larrybr | 4cf34a5 | 2021-03-19 15:02:59 +0000 | [diff] [blame] | 84 | /* 1x */ 28, 28, 28, 28, 28, 7, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, |
drh | d1032f9 | 2020-11-27 20:56:16 +0000 | [diff] [blame] | 85 | /* 2x */ 28, 28, 28, 28, 28, 7, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, |
| 86 | /* 3x */ 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, |
| 87 | /* 4x */ 7, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 26, 12, 17, 20, 10, |
| 88 | /* 5x */ 24, 28, 28, 28, 28, 28, 28, 28, 28, 28, 15, 4, 21, 18, 19, 28, |
| 89 | /* 6x */ 11, 16, 28, 28, 28, 28, 28, 28, 28, 28, 28, 23, 22, 2, 13, 6, |
| 90 | /* 7x */ 28, 28, 28, 28, 28, 28, 28, 28, 28, 8, 5, 5, 5, 8, 14, 8, |
| 91 | /* 8x */ 28, 1, 1, 1, 1, 1, 1, 1, 1, 1, 28, 28, 28, 28, 28, 28, |
| 92 | /* 9x */ 28, 1, 1, 1, 1, 1, 1, 1, 1, 1, 28, 28, 28, 28, 28, 28, |
| 93 | /* Ax */ 28, 25, 1, 1, 1, 1, 1, 0, 2, 2, 28, 28, 28, 28, 28, 28, |
| 94 | /* Bx */ 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 9, 28, 28, 28, 28, 28, |
| 95 | /* Cx */ 28, 1, 1, 1, 1, 1, 1, 1, 1, 1, 28, 28, 28, 28, 28, 28, |
| 96 | /* Dx */ 28, 1, 1, 1, 1, 1, 1, 1, 1, 1, 28, 28, 28, 28, 28, 28, |
| 97 | /* Ex */ 28, 28, 1, 1, 1, 1, 1, 0, 2, 2, 28, 28, 28, 28, 28, 28, |
| 98 | /* Fx */ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 28, 28, 28, 28, 28, 28, |
drh | 34dcee6 | 2016-02-08 19:15:48 +0000 | [diff] [blame] | 99 | #endif |
drh | 8974331 | 2016-02-08 02:30:50 +0000 | [diff] [blame] | 100 | }; |
| 101 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 102 | /* |
drh | 34dcee6 | 2016-02-08 19:15:48 +0000 | [diff] [blame] | 103 | ** The charMap() macro maps alphabetic characters (only) into their |
drh | 9b8f447 | 2006-04-04 01:54:55 +0000 | [diff] [blame] | 104 | ** lower-case ASCII equivalent. On ASCII machines, this is just |
| 105 | ** an upper-to-lower case map. On EBCDIC machines we also need |
drh | 34dcee6 | 2016-02-08 19:15:48 +0000 | [diff] [blame] | 106 | ** to adjust the encoding. The mapping is only valid for alphabetics |
| 107 | ** which are the only characters for which this feature is used. |
| 108 | ** |
| 109 | ** Used by keywordhash.h |
drh | 9b8f447 | 2006-04-04 01:54:55 +0000 | [diff] [blame] | 110 | */ |
| 111 | #ifdef SQLITE_ASCII |
| 112 | # define charMap(X) sqlite3UpperToLower[(unsigned char)X] |
| 113 | #endif |
| 114 | #ifdef SQLITE_EBCDIC |
| 115 | # define charMap(X) ebcdicToAscii[(unsigned char)X] |
| 116 | const unsigned char ebcdicToAscii[] = { |
| 117 | /* 0 1 2 3 4 5 6 7 8 9 A B C D E F */ |
| 118 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x */ |
| 119 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 1x */ |
| 120 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 2x */ |
| 121 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 3x */ |
| 122 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 4x */ |
| 123 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 5x */ |
| 124 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 95, 0, 0, /* 6x */ |
| 125 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 7x */ |
| 126 | 0, 97, 98, 99,100,101,102,103,104,105, 0, 0, 0, 0, 0, 0, /* 8x */ |
| 127 | 0,106,107,108,109,110,111,112,113,114, 0, 0, 0, 0, 0, 0, /* 9x */ |
| 128 | 0, 0,115,116,117,118,119,120,121,122, 0, 0, 0, 0, 0, 0, /* Ax */ |
| 129 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* Bx */ |
| 130 | 0, 97, 98, 99,100,101,102,103,104,105, 0, 0, 0, 0, 0, 0, /* Cx */ |
| 131 | 0,106,107,108,109,110,111,112,113,114, 0, 0, 0, 0, 0, 0, /* Dx */ |
| 132 | 0, 0,115,116,117,118,119,120,121,122, 0, 0, 0, 0, 0, 0, /* Ex */ |
| 133 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* Fx */ |
| 134 | }; |
| 135 | #endif |
| 136 | |
| 137 | /* |
drh | 52fb6d7 | 2004-11-03 03:59:57 +0000 | [diff] [blame] | 138 | ** The sqlite3KeywordCode function looks up an identifier to determine if |
| 139 | ** it is a keyword. If it is a keyword, the token code of that keyword is |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 140 | ** returned. If the input is not a keyword, TK_ID is returned. |
drh | 2090a0e | 2004-10-07 19:03:01 +0000 | [diff] [blame] | 141 | ** |
| 142 | ** The implementation of this routine was generated by a program, |
drh | e96f361 | 2016-02-08 19:36:46 +0000 | [diff] [blame] | 143 | ** mkkeywordhash.c, located in the tool subdirectory of the distribution. |
drh | 52fb6d7 | 2004-11-03 03:59:57 +0000 | [diff] [blame] | 144 | ** The output of the mkkeywordhash.c program is written into a file |
drh | 73b211a | 2005-01-18 04:00:42 +0000 | [diff] [blame] | 145 | ** named keywordhash.h and then included into this source file by |
drh | 52fb6d7 | 2004-11-03 03:59:57 +0000 | [diff] [blame] | 146 | ** the #include below. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 147 | */ |
drh | 73b211a | 2005-01-18 04:00:42 +0000 | [diff] [blame] | 148 | #include "keywordhash.h" |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 149 | |
drh | 40f20f7 | 2004-10-23 05:10:18 +0000 | [diff] [blame] | 150 | |
drh | 98808ba | 2001-10-18 12:34:46 +0000 | [diff] [blame] | 151 | /* |
drh | 9b8f447 | 2006-04-04 01:54:55 +0000 | [diff] [blame] | 152 | ** If X is a character that can be used in an identifier then |
| 153 | ** IdChar(X) will be true. Otherwise it is false. |
drh | 98808ba | 2001-10-18 12:34:46 +0000 | [diff] [blame] | 154 | ** |
drh | 9b8f447 | 2006-04-04 01:54:55 +0000 | [diff] [blame] | 155 | ** For ASCII, any character with the high-order bit set is |
| 156 | ** allowed in an identifier. For 7-bit characters, |
| 157 | ** sqlite3IsIdChar[X] must be 1. |
| 158 | ** |
| 159 | ** For EBCDIC, the rules are more complex but have the same |
| 160 | ** end result. |
drh | a0d1f66 | 2005-01-11 17:59:47 +0000 | [diff] [blame] | 161 | ** |
| 162 | ** Ticket #1066. the SQL standard does not allow '$' in the |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 163 | ** middle of identifiers. But many SQL implementations do. |
drh | a0d1f66 | 2005-01-11 17:59:47 +0000 | [diff] [blame] | 164 | ** SQLite will allow '$' in identifiers for compatibility. |
| 165 | ** But the feature is undocumented. |
drh | 98808ba | 2001-10-18 12:34:46 +0000 | [diff] [blame] | 166 | */ |
drh | 9b8f447 | 2006-04-04 01:54:55 +0000 | [diff] [blame] | 167 | #ifdef SQLITE_ASCII |
drh | af2b572 | 2009-11-16 15:11:51 +0000 | [diff] [blame] | 168 | #define IdChar(C) ((sqlite3CtypeMap[(unsigned char)C]&0x46)!=0) |
drh | 9b8f447 | 2006-04-04 01:54:55 +0000 | [diff] [blame] | 169 | #endif |
| 170 | #ifdef SQLITE_EBCDIC |
drh | 46c99e0 | 2007-08-27 23:26:59 +0000 | [diff] [blame] | 171 | const char sqlite3IsEbcdicIdChar[] = { |
drh | 9b8f447 | 2006-04-04 01:54:55 +0000 | [diff] [blame] | 172 | /* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */ |
| 173 | 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 4x */ |
| 174 | 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, /* 5x */ |
| 175 | 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, /* 6x */ |
| 176 | 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, /* 7x */ |
| 177 | 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, /* 8x */ |
| 178 | 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, /* 9x */ |
| 179 | 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, /* Ax */ |
| 180 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* Bx */ |
| 181 | 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* Cx */ |
| 182 | 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* Dx */ |
| 183 | 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* Ex */ |
| 184 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, /* Fx */ |
| 185 | }; |
drh | 46c99e0 | 2007-08-27 23:26:59 +0000 | [diff] [blame] | 186 | #define IdChar(C) (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40])) |
drh | 9b8f447 | 2006-04-04 01:54:55 +0000 | [diff] [blame] | 187 | #endif |
drh | f5ed7ad | 2015-06-15 14:43:25 +0000 | [diff] [blame] | 188 | |
drh | 38d9964 | 2018-08-18 18:27:18 +0000 | [diff] [blame] | 189 | /* Make the IdChar function accessible from ctime.c and alter.c */ |
drh | 97348b3 | 2014-09-25 02:44:29 +0000 | [diff] [blame] | 190 | int sqlite3IsIdChar(u8 c){ return IdChar(c); } |
drh | 9b8f447 | 2006-04-04 01:54:55 +0000 | [diff] [blame] | 191 | |
dan | 34a7d79 | 2018-06-29 20:43:33 +0000 | [diff] [blame] | 192 | #ifndef SQLITE_OMIT_WINDOWFUNC |
dan | 59ff425 | 2018-06-29 17:44:52 +0000 | [diff] [blame] | 193 | /* |
| 194 | ** Return the id of the next token in string (*pz). Before returning, set |
| 195 | ** (*pz) to point to the byte following the parsed token. |
dan | 59ff425 | 2018-06-29 17:44:52 +0000 | [diff] [blame] | 196 | */ |
dan | 6e2210e | 2018-06-30 18:54:56 +0000 | [diff] [blame] | 197 | static int getToken(const unsigned char **pz){ |
dan | 59ff425 | 2018-06-29 17:44:52 +0000 | [diff] [blame] | 198 | const unsigned char *z = *pz; |
dan | 6e2210e | 2018-06-30 18:54:56 +0000 | [diff] [blame] | 199 | int t; /* Token type to return */ |
| 200 | do { |
| 201 | z += sqlite3GetToken(z, &t); |
| 202 | }while( t==TK_SPACE ); |
| 203 | if( t==TK_ID |
| 204 | || t==TK_STRING |
| 205 | || t==TK_JOIN_KW |
| 206 | || t==TK_WINDOW |
| 207 | || t==TK_OVER |
| 208 | || sqlite3ParserFallback(t)==TK_ID |
| 209 | ){ |
| 210 | t = TK_ID; |
dan | 59ff425 | 2018-06-29 17:44:52 +0000 | [diff] [blame] | 211 | } |
| 212 | *pz = z; |
dan | 6e2210e | 2018-06-30 18:54:56 +0000 | [diff] [blame] | 213 | return t; |
dan | 59ff425 | 2018-06-29 17:44:52 +0000 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | /* |
dan | 6e2210e | 2018-06-30 18:54:56 +0000 | [diff] [blame] | 217 | ** The following three functions are called immediately after the tokenizer |
| 218 | ** reads the keywords WINDOW, OVER and FILTER, respectively, to determine |
| 219 | ** whether the token should be treated as a keyword or an SQL identifier. |
| 220 | ** This cannot be handled by the usual lemon %fallback method, due to |
| 221 | ** the ambiguity in some constructions. e.g. |
dan | 59ff425 | 2018-06-29 17:44:52 +0000 | [diff] [blame] | 222 | ** |
dan | 6e2210e | 2018-06-30 18:54:56 +0000 | [diff] [blame] | 223 | ** SELECT sum(x) OVER ... |
dan | 59ff425 | 2018-06-29 17:44:52 +0000 | [diff] [blame] | 224 | ** |
dan | 6e2210e | 2018-06-30 18:54:56 +0000 | [diff] [blame] | 225 | ** In the above, "OVER" might be a keyword, or it might be an alias for the |
| 226 | ** sum(x) expression. If a "%fallback ID OVER" directive were added to |
| 227 | ** grammar, then SQLite would always treat "OVER" as an alias, making it |
| 228 | ** impossible to call a window-function without a FILTER clause. |
| 229 | ** |
| 230 | ** WINDOW is treated as a keyword if: |
| 231 | ** |
| 232 | ** * the following token is an identifier, or a keyword that can fallback |
| 233 | ** to being an identifier, and |
| 234 | ** * the token after than one is TK_AS. |
| 235 | ** |
| 236 | ** OVER is a keyword if: |
| 237 | ** |
| 238 | ** * the previous token was TK_RP, and |
| 239 | ** * the next token is either TK_LP or an identifier. |
| 240 | ** |
| 241 | ** FILTER is a keyword if: |
| 242 | ** |
| 243 | ** * the previous token was TK_RP, and |
| 244 | ** * the next token is TK_LP. |
dan | 59ff425 | 2018-06-29 17:44:52 +0000 | [diff] [blame] | 245 | */ |
dan | 769309f | 2018-06-29 19:54:51 +0000 | [diff] [blame] | 246 | static int analyzeWindowKeyword(const unsigned char *z){ |
dan | 59ff425 | 2018-06-29 17:44:52 +0000 | [diff] [blame] | 247 | int t; |
dan | 6e2210e | 2018-06-30 18:54:56 +0000 | [diff] [blame] | 248 | t = getToken(&z); |
| 249 | if( t!=TK_ID ) return TK_ID; |
| 250 | t = getToken(&z); |
| 251 | if( t!=TK_AS ) return TK_ID; |
| 252 | return TK_WINDOW; |
| 253 | } |
| 254 | static int analyzeOverKeyword(const unsigned char *z, int lastToken){ |
| 255 | if( lastToken==TK_RP ){ |
| 256 | int t = getToken(&z); |
| 257 | if( t==TK_LP || t==TK_ID ) return TK_OVER; |
dan | 59ff425 | 2018-06-29 17:44:52 +0000 | [diff] [blame] | 258 | } |
dan | 6e2210e | 2018-06-30 18:54:56 +0000 | [diff] [blame] | 259 | return TK_ID; |
| 260 | } |
| 261 | static int analyzeFilterKeyword(const unsigned char *z, int lastToken){ |
| 262 | if( lastToken==TK_RP && getToken(&z)==TK_LP ){ |
| 263 | return TK_FILTER; |
| 264 | } |
| 265 | return TK_ID; |
dan | 59ff425 | 2018-06-29 17:44:52 +0000 | [diff] [blame] | 266 | } |
drh | c7bf571 | 2018-07-09 22:49:01 +0000 | [diff] [blame] | 267 | #endif /* SQLITE_OMIT_WINDOWFUNC */ |
drh | 98808ba | 2001-10-18 12:34:46 +0000 | [diff] [blame] | 268 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 269 | /* |
drh | e96f361 | 2016-02-08 19:36:46 +0000 | [diff] [blame] | 270 | ** Return the length (in bytes) of the token that begins at z[0]. |
drh | 61b487d | 2003-09-12 02:08:14 +0000 | [diff] [blame] | 271 | ** Store the token type in *tokenType before returning. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 272 | */ |
drh | 35b5a33 | 2008-04-05 18:41:42 +0000 | [diff] [blame] | 273 | int sqlite3GetToken(const unsigned char *z, int *tokenType){ |
drh | aa756b0 | 2004-09-25 15:25:26 +0000 | [diff] [blame] | 274 | int i, c; |
drh | e96f361 | 2016-02-08 19:36:46 +0000 | [diff] [blame] | 275 | switch( aiClass[*z] ){ /* Switch on the character-class of the first byte |
| 276 | ** of the token. See the comment on the CC_ defines |
| 277 | ** above. */ |
drh | 8974331 | 2016-02-08 02:30:50 +0000 | [diff] [blame] | 278 | case CC_SPACE: { |
drh | 19f81f6 | 2009-06-09 18:01:37 +0000 | [diff] [blame] | 279 | testcase( z[0]==' ' ); |
| 280 | testcase( z[0]=='\t' ); |
| 281 | testcase( z[0]=='\n' ); |
| 282 | testcase( z[0]=='\f' ); |
| 283 | testcase( z[0]=='\r' ); |
danielk1977 | 78ca0e7 | 2009-01-20 16:53:39 +0000 | [diff] [blame] | 284 | for(i=1; sqlite3Isspace(z[i]); i++){} |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 285 | *tokenType = TK_SPACE; |
| 286 | return i; |
| 287 | } |
drh | 8974331 | 2016-02-08 02:30:50 +0000 | [diff] [blame] | 288 | case CC_MINUS: { |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 289 | if( z[1]=='-' ){ |
drh | aa756b0 | 2004-09-25 15:25:26 +0000 | [diff] [blame] | 290 | for(i=2; (c=z[i])!=0 && c!='\n'; i++){} |
drh | d3479b9 | 2009-12-14 17:42:12 +0000 | [diff] [blame] | 291 | *tokenType = TK_SPACE; /* IMP: R-22934-25134 */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 292 | return i; |
drh | d5326c3 | 2022-01-07 14:58:47 +0000 | [diff] [blame] | 293 | }else if( z[1]=='>' ){ |
| 294 | *tokenType = TK_PTR; |
| 295 | return 2 + (z[2]=='>'); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 296 | } |
| 297 | *tokenType = TK_MINUS; |
| 298 | return 1; |
| 299 | } |
drh | 8974331 | 2016-02-08 02:30:50 +0000 | [diff] [blame] | 300 | case CC_LP: { |
drh | dab3518 | 2003-09-27 13:39:38 +0000 | [diff] [blame] | 301 | *tokenType = TK_LP; |
| 302 | return 1; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 303 | } |
drh | 8974331 | 2016-02-08 02:30:50 +0000 | [diff] [blame] | 304 | case CC_RP: { |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 305 | *tokenType = TK_RP; |
| 306 | return 1; |
| 307 | } |
drh | 8974331 | 2016-02-08 02:30:50 +0000 | [diff] [blame] | 308 | case CC_SEMI: { |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 309 | *tokenType = TK_SEMI; |
| 310 | return 1; |
| 311 | } |
drh | 8974331 | 2016-02-08 02:30:50 +0000 | [diff] [blame] | 312 | case CC_PLUS: { |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 313 | *tokenType = TK_PLUS; |
| 314 | return 1; |
| 315 | } |
drh | 8974331 | 2016-02-08 02:30:50 +0000 | [diff] [blame] | 316 | case CC_STAR: { |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 317 | *tokenType = TK_STAR; |
| 318 | return 1; |
| 319 | } |
drh | 8974331 | 2016-02-08 02:30:50 +0000 | [diff] [blame] | 320 | case CC_SLASH: { |
drh | 66105a8 | 2002-08-27 14:28:29 +0000 | [diff] [blame] | 321 | if( z[1]!='*' || z[2]==0 ){ |
| 322 | *tokenType = TK_SLASH; |
| 323 | return 1; |
| 324 | } |
drh | aa756b0 | 2004-09-25 15:25:26 +0000 | [diff] [blame] | 325 | for(i=3, c=z[2]; (c!='*' || z[i]!='/') && (c=z[i])!=0; i++){} |
| 326 | if( c ) i++; |
drh | d3479b9 | 2009-12-14 17:42:12 +0000 | [diff] [blame] | 327 | *tokenType = TK_SPACE; /* IMP: R-22934-25134 */ |
drh | 66105a8 | 2002-08-27 14:28:29 +0000 | [diff] [blame] | 328 | return i; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 329 | } |
drh | 8974331 | 2016-02-08 02:30:50 +0000 | [diff] [blame] | 330 | case CC_PERCENT: { |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 331 | *tokenType = TK_REM; |
| 332 | return 1; |
| 333 | } |
drh | 8974331 | 2016-02-08 02:30:50 +0000 | [diff] [blame] | 334 | case CC_EQ: { |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 335 | *tokenType = TK_EQ; |
| 336 | return 1 + (z[1]=='='); |
| 337 | } |
drh | 8974331 | 2016-02-08 02:30:50 +0000 | [diff] [blame] | 338 | case CC_LT: { |
drh | aa756b0 | 2004-09-25 15:25:26 +0000 | [diff] [blame] | 339 | if( (c=z[1])=='=' ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 340 | *tokenType = TK_LE; |
| 341 | return 2; |
drh | aa756b0 | 2004-09-25 15:25:26 +0000 | [diff] [blame] | 342 | }else if( c=='>' ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 343 | *tokenType = TK_NE; |
| 344 | return 2; |
drh | aa756b0 | 2004-09-25 15:25:26 +0000 | [diff] [blame] | 345 | }else if( c=='<' ){ |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 346 | *tokenType = TK_LSHIFT; |
| 347 | return 2; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 348 | }else{ |
| 349 | *tokenType = TK_LT; |
| 350 | return 1; |
| 351 | } |
| 352 | } |
drh | 8974331 | 2016-02-08 02:30:50 +0000 | [diff] [blame] | 353 | case CC_GT: { |
drh | aa756b0 | 2004-09-25 15:25:26 +0000 | [diff] [blame] | 354 | if( (c=z[1])=='=' ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 355 | *tokenType = TK_GE; |
| 356 | return 2; |
drh | aa756b0 | 2004-09-25 15:25:26 +0000 | [diff] [blame] | 357 | }else if( c=='>' ){ |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 358 | *tokenType = TK_RSHIFT; |
| 359 | return 2; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 360 | }else{ |
| 361 | *tokenType = TK_GT; |
| 362 | return 1; |
| 363 | } |
| 364 | } |
drh | 8974331 | 2016-02-08 02:30:50 +0000 | [diff] [blame] | 365 | case CC_BANG: { |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 366 | if( z[1]!='=' ){ |
| 367 | *tokenType = TK_ILLEGAL; |
drh | b2bddbb | 2016-02-18 14:49:28 +0000 | [diff] [blame] | 368 | return 1; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 369 | }else{ |
| 370 | *tokenType = TK_NE; |
| 371 | return 2; |
| 372 | } |
| 373 | } |
drh | 8974331 | 2016-02-08 02:30:50 +0000 | [diff] [blame] | 374 | case CC_PIPE: { |
drh | 0040077 | 2000-06-16 20:51:26 +0000 | [diff] [blame] | 375 | if( z[1]!='|' ){ |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 376 | *tokenType = TK_BITOR; |
drh | 0040077 | 2000-06-16 20:51:26 +0000 | [diff] [blame] | 377 | return 1; |
| 378 | }else{ |
| 379 | *tokenType = TK_CONCAT; |
| 380 | return 2; |
| 381 | } |
| 382 | } |
drh | 8974331 | 2016-02-08 02:30:50 +0000 | [diff] [blame] | 383 | case CC_COMMA: { |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 384 | *tokenType = TK_COMMA; |
| 385 | return 1; |
| 386 | } |
drh | 8974331 | 2016-02-08 02:30:50 +0000 | [diff] [blame] | 387 | case CC_AND: { |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 388 | *tokenType = TK_BITAND; |
| 389 | return 1; |
| 390 | } |
drh | 8974331 | 2016-02-08 02:30:50 +0000 | [diff] [blame] | 391 | case CC_TILDA: { |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 392 | *tokenType = TK_BITNOT; |
| 393 | return 1; |
| 394 | } |
drh | 8974331 | 2016-02-08 02:30:50 +0000 | [diff] [blame] | 395 | case CC_QUOTE: { |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 396 | int delim = z[0]; |
drh | 19f81f6 | 2009-06-09 18:01:37 +0000 | [diff] [blame] | 397 | testcase( delim=='`' ); |
| 398 | testcase( delim=='\'' ); |
| 399 | testcase( delim=='"' ); |
drh | aa756b0 | 2004-09-25 15:25:26 +0000 | [diff] [blame] | 400 | for(i=1; (c=z[i])!=0; i++){ |
| 401 | if( c==delim ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 402 | if( z[i+1]==delim ){ |
| 403 | i++; |
| 404 | }else{ |
| 405 | break; |
| 406 | } |
| 407 | } |
| 408 | } |
drh | a33cb5f | 2008-08-07 13:05:34 +0000 | [diff] [blame] | 409 | if( c=='\'' ){ |
drh | eef8b55 | 2005-10-23 11:29:40 +0000 | [diff] [blame] | 410 | *tokenType = TK_STRING; |
| 411 | return i+1; |
drh | a33cb5f | 2008-08-07 13:05:34 +0000 | [diff] [blame] | 412 | }else if( c!=0 ){ |
| 413 | *tokenType = TK_ID; |
| 414 | return i+1; |
drh | eef8b55 | 2005-10-23 11:29:40 +0000 | [diff] [blame] | 415 | }else{ |
| 416 | *tokenType = TK_ILLEGAL; |
| 417 | return i; |
| 418 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 419 | } |
drh | 8974331 | 2016-02-08 02:30:50 +0000 | [diff] [blame] | 420 | case CC_DOT: { |
drh | 7681618 | 2005-08-23 11:31:26 +0000 | [diff] [blame] | 421 | #ifndef SQLITE_OMIT_FLOATING_POINT |
danielk1977 | 78ca0e7 | 2009-01-20 16:53:39 +0000 | [diff] [blame] | 422 | if( !sqlite3Isdigit(z[1]) ) |
drh | 7681618 | 2005-08-23 11:31:26 +0000 | [diff] [blame] | 423 | #endif |
| 424 | { |
| 425 | *tokenType = TK_DOT; |
| 426 | return 1; |
| 427 | } |
| 428 | /* If the next character is a digit, this is a floating point |
| 429 | ** number that begins with ".". Fall thru into the next case */ |
drh | 08b9208 | 2020-08-10 14:18:00 +0000 | [diff] [blame] | 430 | /* no break */ deliberate_fall_through |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 431 | } |
drh | 8974331 | 2016-02-08 02:30:50 +0000 | [diff] [blame] | 432 | case CC_DIGIT: { |
drh | 19f81f6 | 2009-06-09 18:01:37 +0000 | [diff] [blame] | 433 | testcase( z[0]=='0' ); testcase( z[0]=='1' ); testcase( z[0]=='2' ); |
| 434 | testcase( z[0]=='3' ); testcase( z[0]=='4' ); testcase( z[0]=='5' ); |
| 435 | testcase( z[0]=='6' ); testcase( z[0]=='7' ); testcase( z[0]=='8' ); |
| 436 | testcase( z[0]=='9' ); |
drh | c837e70 | 2000-06-08 16:26:24 +0000 | [diff] [blame] | 437 | *tokenType = TK_INTEGER; |
drh | 28e048c | 2014-07-23 01:26:51 +0000 | [diff] [blame] | 438 | #ifndef SQLITE_OMIT_HEX_INTEGER |
| 439 | if( z[0]=='0' && (z[1]=='x' || z[1]=='X') && sqlite3Isxdigit(z[2]) ){ |
| 440 | for(i=3; sqlite3Isxdigit(z[i]); i++){} |
| 441 | return i; |
| 442 | } |
| 443 | #endif |
danielk1977 | 78ca0e7 | 2009-01-20 16:53:39 +0000 | [diff] [blame] | 444 | for(i=0; sqlite3Isdigit(z[i]); i++){} |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 445 | #ifndef SQLITE_OMIT_FLOATING_POINT |
drh | 7681618 | 2005-08-23 11:31:26 +0000 | [diff] [blame] | 446 | if( z[i]=='.' ){ |
| 447 | i++; |
danielk1977 | 78ca0e7 | 2009-01-20 16:53:39 +0000 | [diff] [blame] | 448 | while( sqlite3Isdigit(z[i]) ){ i++; } |
drh | c837e70 | 2000-06-08 16:26:24 +0000 | [diff] [blame] | 449 | *tokenType = TK_FLOAT; |
| 450 | } |
| 451 | if( (z[i]=='e' || z[i]=='E') && |
danielk1977 | 78ca0e7 | 2009-01-20 16:53:39 +0000 | [diff] [blame] | 452 | ( sqlite3Isdigit(z[i+1]) |
| 453 | || ((z[i+1]=='+' || z[i+1]=='-') && sqlite3Isdigit(z[i+2])) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 454 | ) |
drh | c837e70 | 2000-06-08 16:26:24 +0000 | [diff] [blame] | 455 | ){ |
| 456 | i += 2; |
danielk1977 | 78ca0e7 | 2009-01-20 16:53:39 +0000 | [diff] [blame] | 457 | while( sqlite3Isdigit(z[i]) ){ i++; } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 458 | *tokenType = TK_FLOAT; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 459 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 460 | #endif |
drh | 67dd901 | 2006-08-12 12:33:14 +0000 | [diff] [blame] | 461 | while( IdChar(z[i]) ){ |
| 462 | *tokenType = TK_ILLEGAL; |
| 463 | i++; |
| 464 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 465 | return i; |
| 466 | } |
drh | 8974331 | 2016-02-08 02:30:50 +0000 | [diff] [blame] | 467 | case CC_QUOTE2: { |
drh | aa756b0 | 2004-09-25 15:25:26 +0000 | [diff] [blame] | 468 | for(i=1, c=z[0]; c!=']' && (c=z[i])!=0; i++){} |
drh | 4b2f936 | 2008-01-22 23:37:09 +0000 | [diff] [blame] | 469 | *tokenType = c==']' ? TK_ID : TK_ILLEGAL; |
drh | 2f4392f | 2002-02-14 21:42:51 +0000 | [diff] [blame] | 470 | return i; |
| 471 | } |
drh | 8974331 | 2016-02-08 02:30:50 +0000 | [diff] [blame] | 472 | case CC_VARNUM: { |
drh | 5045789 | 2003-09-06 01:10:47 +0000 | [diff] [blame] | 473 | *tokenType = TK_VARIABLE; |
danielk1977 | 78ca0e7 | 2009-01-20 16:53:39 +0000 | [diff] [blame] | 474 | for(i=1; sqlite3Isdigit(z[i]); i++){} |
drh | fa6bc00 | 2004-09-07 16:19:52 +0000 | [diff] [blame] | 475 | return i; |
drh | 5045789 | 2003-09-06 01:10:47 +0000 | [diff] [blame] | 476 | } |
drh | 8974331 | 2016-02-08 02:30:50 +0000 | [diff] [blame] | 477 | case CC_DOLLAR: |
| 478 | case CC_VARALPHA: { |
drh | 288d37f | 2005-06-22 08:48:06 +0000 | [diff] [blame] | 479 | int n = 0; |
drh | f59b12f | 2014-01-11 03:54:05 +0000 | [diff] [blame] | 480 | testcase( z[0]=='$' ); testcase( z[0]=='@' ); |
| 481 | testcase( z[0]==':' ); testcase( z[0]=='#' ); |
drh | 9d74b4c | 2004-08-24 15:23:34 +0000 | [diff] [blame] | 482 | *tokenType = TK_VARIABLE; |
drh | 288d37f | 2005-06-22 08:48:06 +0000 | [diff] [blame] | 483 | for(i=1; (c=z[i])!=0; i++){ |
| 484 | if( IdChar(c) ){ |
| 485 | n++; |
| 486 | #ifndef SQLITE_OMIT_TCL_VARIABLE |
| 487 | }else if( c=='(' && n>0 ){ |
| 488 | do{ |
| 489 | i++; |
danielk1977 | 78ca0e7 | 2009-01-20 16:53:39 +0000 | [diff] [blame] | 490 | }while( (c=z[i])!=0 && !sqlite3Isspace(c) && c!=')' ); |
drh | 288d37f | 2005-06-22 08:48:06 +0000 | [diff] [blame] | 491 | if( c==')' ){ |
drh | 895d747 | 2004-08-20 16:02:39 +0000 | [diff] [blame] | 492 | i++; |
| 493 | }else{ |
drh | 288d37f | 2005-06-22 08:48:06 +0000 | [diff] [blame] | 494 | *tokenType = TK_ILLEGAL; |
drh | 895d747 | 2004-08-20 16:02:39 +0000 | [diff] [blame] | 495 | } |
drh | 288d37f | 2005-06-22 08:48:06 +0000 | [diff] [blame] | 496 | break; |
| 497 | }else if( c==':' && z[i+1]==':' ){ |
| 498 | i++; |
| 499 | #endif |
| 500 | }else{ |
| 501 | break; |
drh | 895d747 | 2004-08-20 16:02:39 +0000 | [diff] [blame] | 502 | } |
| 503 | } |
drh | 288d37f | 2005-06-22 08:48:06 +0000 | [diff] [blame] | 504 | if( n==0 ) *tokenType = TK_ILLEGAL; |
drh | 895d747 | 2004-08-20 16:02:39 +0000 | [diff] [blame] | 505 | return i; |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 506 | } |
drh | d1032f9 | 2020-11-27 20:56:16 +0000 | [diff] [blame] | 507 | case CC_KYWD0: { |
drh | e96f361 | 2016-02-08 19:36:46 +0000 | [diff] [blame] | 508 | for(i=1; aiClass[z[i]]<=CC_KYWD; i++){} |
| 509 | if( IdChar(z[i]) ){ |
| 510 | /* This token started out using characters that can appear in keywords, |
| 511 | ** but z[i] is a character not allowed within keywords, so this must |
| 512 | ** be an identifier instead */ |
| 513 | i++; |
| 514 | break; |
| 515 | } |
| 516 | *tokenType = TK_ID; |
dan | 769309f | 2018-06-29 19:54:51 +0000 | [diff] [blame] | 517 | return keywordCode((char*)z, i, tokenType); |
drh | e96f361 | 2016-02-08 19:36:46 +0000 | [diff] [blame] | 518 | } |
drh | 8974331 | 2016-02-08 02:30:50 +0000 | [diff] [blame] | 519 | case CC_X: { |
dan | a73086d | 2016-02-24 16:14:07 +0000 | [diff] [blame] | 520 | #ifndef SQLITE_OMIT_BLOB_LITERAL |
drh | 19f81f6 | 2009-06-09 18:01:37 +0000 | [diff] [blame] | 521 | testcase( z[0]=='x' ); testcase( z[0]=='X' ); |
drh | 4b2f936 | 2008-01-22 23:37:09 +0000 | [diff] [blame] | 522 | if( z[1]=='\'' ){ |
danielk1977 | 3fd0a73 | 2004-05-27 13:35:19 +0000 | [diff] [blame] | 523 | *tokenType = TK_BLOB; |
drh | 3a61a5a | 2011-06-03 21:34:45 +0000 | [diff] [blame] | 524 | for(i=2; sqlite3Isxdigit(z[i]); i++){} |
| 525 | if( z[i]!='\'' || i%2 ){ |
| 526 | *tokenType = TK_ILLEGAL; |
| 527 | while( z[i] && z[i]!='\'' ){ i++; } |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 528 | } |
drh | 3a61a5a | 2011-06-03 21:34:45 +0000 | [diff] [blame] | 529 | if( z[i] ) i++; |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 530 | return i; |
| 531 | } |
dan | a73086d | 2016-02-24 16:14:07 +0000 | [diff] [blame] | 532 | #endif |
drh | e96f361 | 2016-02-08 19:36:46 +0000 | [diff] [blame] | 533 | /* If it is not a BLOB literal, then it must be an ID, since no |
| 534 | ** SQL keywords start with the letter 'x'. Fall through */ |
drh | 08b9208 | 2020-08-10 14:18:00 +0000 | [diff] [blame] | 535 | /* no break */ deliberate_fall_through |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 536 | } |
drh | d1032f9 | 2020-11-27 20:56:16 +0000 | [diff] [blame] | 537 | case CC_KYWD: |
drh | 8974331 | 2016-02-08 02:30:50 +0000 | [diff] [blame] | 538 | case CC_ID: { |
| 539 | i = 1; |
| 540 | break; |
| 541 | } |
drh | ba9ebc2 | 2021-04-24 12:24:08 +0000 | [diff] [blame] | 542 | case CC_BOM: { |
| 543 | if( z[1]==0xbb && z[2]==0xbf ){ |
| 544 | *tokenType = TK_SPACE; |
| 545 | return 3; |
| 546 | } |
| 547 | i = 1; |
| 548 | break; |
| 549 | } |
dan | 2b5f152 | 2018-06-30 20:00:35 +0000 | [diff] [blame] | 550 | case CC_NUL: { |
| 551 | *tokenType = TK_ILLEGAL; |
| 552 | return 0; |
| 553 | } |
drh | 98808ba | 2001-10-18 12:34:46 +0000 | [diff] [blame] | 554 | default: { |
drh | 8974331 | 2016-02-08 02:30:50 +0000 | [diff] [blame] | 555 | *tokenType = TK_ILLEGAL; |
| 556 | return 1; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 557 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 558 | } |
drh | 34dcee6 | 2016-02-08 19:15:48 +0000 | [diff] [blame] | 559 | while( IdChar(z[i]) ){ i++; } |
drh | 8974331 | 2016-02-08 02:30:50 +0000 | [diff] [blame] | 560 | *tokenType = TK_ID; |
| 561 | return i; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | /* |
drh | 54bc638 | 2021-12-31 19:20:42 +0000 | [diff] [blame] | 565 | ** Run the parser on the given SQL string. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 566 | */ |
drh | 54bc638 | 2021-12-31 19:20:42 +0000 | [diff] [blame] | 567 | int sqlite3RunParser(Parse *pParse, const char *zSql){ |
drh | d9da78a | 2009-03-24 15:08:09 +0000 | [diff] [blame] | 568 | int nErr = 0; /* Number of errors encountered */ |
drh | d9da78a | 2009-03-24 15:08:09 +0000 | [diff] [blame] | 569 | void *pEngine; /* The LEMON-generated LALR(1) parser */ |
drh | 9b38d66 | 2017-03-07 14:38:52 +0000 | [diff] [blame] | 570 | int n = 0; /* Length of the next token token */ |
drh | d9da78a | 2009-03-24 15:08:09 +0000 | [diff] [blame] | 571 | int tokenType; /* type of the next token */ |
| 572 | int lastTokenParsed = -1; /* type of the previous token */ |
drh | d9da78a | 2009-03-24 15:08:09 +0000 | [diff] [blame] | 573 | sqlite3 *db = pParse->db; /* The database connection */ |
| 574 | int mxSqlLen; /* Max length of an SQL string */ |
dan | 488b558 | 2021-11-16 13:36:50 +0000 | [diff] [blame] | 575 | Parse *pParentParse = 0; /* Outer parse context, if any */ |
drh | d26cc54 | 2017-01-28 20:46:37 +0000 | [diff] [blame] | 576 | #ifdef sqlite3Parser_ENGINEALWAYSONSTACK |
drh | 53b2459 | 2017-03-30 17:13:37 +0000 | [diff] [blame] | 577 | yyParser sEngine; /* Space to hold the Lemon-generated Parser object */ |
drh | d26cc54 | 2017-01-28 20:46:37 +0000 | [diff] [blame] | 578 | #endif |
drh | 2b454e0 | 2019-02-26 16:11:46 +0000 | [diff] [blame] | 579 | VVA_ONLY( u8 startedWithOom = db->mallocFailed ); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 580 | |
drh | 96c707a | 2015-02-13 16:36:14 +0000 | [diff] [blame] | 581 | assert( zSql!=0 ); |
drh | d9da78a | 2009-03-24 15:08:09 +0000 | [diff] [blame] | 582 | mxSqlLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH]; |
drh | 4f7d3a5 | 2013-06-27 23:54:02 +0000 | [diff] [blame] | 583 | if( db->nVdbeActive==0 ){ |
dan | 892edb6 | 2020-03-30 13:35:05 +0000 | [diff] [blame] | 584 | AtomicStore(&db->u1.isInterrupted, 0); |
drh | 15ca1df | 2006-07-26 13:43:30 +0000 | [diff] [blame] | 585 | } |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 586 | pParse->rc = SQLITE_OK; |
drh | e7f3f3e | 2009-07-03 22:54:36 +0000 | [diff] [blame] | 587 | pParse->zTail = zSql; |
drh | 9b74706 | 2019-01-31 01:39:01 +0000 | [diff] [blame] | 588 | #ifdef SQLITE_DEBUG |
| 589 | if( db->flags & SQLITE_ParserTrace ){ |
| 590 | printf("parser: [[[%s]]]\n", zSql); |
| 591 | sqlite3ParserTrace(stdout, "parser: "); |
| 592 | }else{ |
| 593 | sqlite3ParserTrace(0, 0); |
| 594 | } |
| 595 | #endif |
drh | d26cc54 | 2017-01-28 20:46:37 +0000 | [diff] [blame] | 596 | #ifdef sqlite3Parser_ENGINEALWAYSONSTACK |
drh | 53b2459 | 2017-03-30 17:13:37 +0000 | [diff] [blame] | 597 | pEngine = &sEngine; |
drh | fb32c44 | 2018-04-21 13:51:42 +0000 | [diff] [blame] | 598 | sqlite3ParserInit(pEngine, pParse); |
drh | d26cc54 | 2017-01-28 20:46:37 +0000 | [diff] [blame] | 599 | #else |
drh | fb32c44 | 2018-04-21 13:51:42 +0000 | [diff] [blame] | 600 | pEngine = sqlite3ParserAlloc(sqlite3Malloc, pParse); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 601 | if( pEngine==0 ){ |
drh | 4a642b6 | 2016-02-05 01:55:27 +0000 | [diff] [blame] | 602 | sqlite3OomFault(db); |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 603 | return SQLITE_NOMEM_BKPT; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 604 | } |
drh | d26cc54 | 2017-01-28 20:46:37 +0000 | [diff] [blame] | 605 | #endif |
drh | fa6bc00 | 2004-09-07 16:19:52 +0000 | [diff] [blame] | 606 | assert( pParse->pNewTable==0 ); |
| 607 | assert( pParse->pNewTrigger==0 ); |
| 608 | assert( pParse->nVar==0 ); |
drh | 9bf755c | 2016-12-23 03:59:31 +0000 | [diff] [blame] | 609 | assert( pParse->pVList==0 ); |
dan | 488b558 | 2021-11-16 13:36:50 +0000 | [diff] [blame] | 610 | pParentParse = db->pParse; |
drh | 1cf1975 | 2019-02-08 14:55:30 +0000 | [diff] [blame] | 611 | db->pParse = pParse; |
drh | 167fbbe | 2016-08-10 14:40:00 +0000 | [diff] [blame] | 612 | while( 1 ){ |
dan | d437ac0 | 2018-06-29 20:21:24 +0000 | [diff] [blame] | 613 | n = sqlite3GetToken((u8*)zSql, &tokenType); |
| 614 | mxSqlLen -= n; |
| 615 | if( mxSqlLen<0 ){ |
| 616 | pParse->rc = SQLITE_TOOBIG; |
drh | cd9e863 | 2022-07-11 14:36:03 +0000 | [diff] [blame^] | 617 | pParse->nErr++; |
dan | d437ac0 | 2018-06-29 20:21:24 +0000 | [diff] [blame] | 618 | break; |
drh | e5c941b | 2007-05-08 13:58:26 +0000 | [diff] [blame] | 619 | } |
dan | 34a7d79 | 2018-06-29 20:43:33 +0000 | [diff] [blame] | 620 | #ifndef SQLITE_OMIT_WINDOWFUNC |
| 621 | if( tokenType>=TK_WINDOW ){ |
dan | 6e2210e | 2018-06-30 18:54:56 +0000 | [diff] [blame] | 622 | assert( tokenType==TK_SPACE || tokenType==TK_OVER || tokenType==TK_FILTER |
dan | 34a7d79 | 2018-06-29 20:43:33 +0000 | [diff] [blame] | 623 | || tokenType==TK_ILLEGAL || tokenType==TK_WINDOW |
| 624 | ); |
| 625 | #else |
drh | 0739e45 | 2015-11-09 02:08:09 +0000 | [diff] [blame] | 626 | if( tokenType>=TK_SPACE ){ |
| 627 | assert( tokenType==TK_SPACE || tokenType==TK_ILLEGAL ); |
drh | c7bf571 | 2018-07-09 22:49:01 +0000 | [diff] [blame] | 628 | #endif /* SQLITE_OMIT_WINDOWFUNC */ |
dan | 892edb6 | 2020-03-30 13:35:05 +0000 | [diff] [blame] | 629 | if( AtomicLoad(&db->u1.isInterrupted) ){ |
drh | 0739e45 | 2015-11-09 02:08:09 +0000 | [diff] [blame] | 630 | pParse->rc = SQLITE_INTERRUPT; |
drh | 54bc638 | 2021-12-31 19:20:42 +0000 | [diff] [blame] | 631 | pParse->nErr++; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 632 | break; |
| 633 | } |
dan | d437ac0 | 2018-06-29 20:21:24 +0000 | [diff] [blame] | 634 | if( tokenType==TK_SPACE ){ |
| 635 | zSql += n; |
| 636 | continue; |
| 637 | } |
| 638 | if( zSql[0]==0 ){ |
| 639 | /* Upon reaching the end of input, call the parser two more times |
| 640 | ** with tokens TK_SEMI and 0, in that order. */ |
| 641 | if( lastTokenParsed==TK_SEMI ){ |
| 642 | tokenType = 0; |
| 643 | }else if( lastTokenParsed==0 ){ |
| 644 | break; |
| 645 | }else{ |
| 646 | tokenType = TK_SEMI; |
| 647 | } |
| 648 | n = 0; |
dan | 34a7d79 | 2018-06-29 20:43:33 +0000 | [diff] [blame] | 649 | #ifndef SQLITE_OMIT_WINDOWFUNC |
| 650 | }else if( tokenType==TK_WINDOW ){ |
dan | 6e2210e | 2018-06-30 18:54:56 +0000 | [diff] [blame] | 651 | assert( n==6 ); |
dan | 34a7d79 | 2018-06-29 20:43:33 +0000 | [diff] [blame] | 652 | tokenType = analyzeWindowKeyword((const u8*)&zSql[6]); |
dan | 6e2210e | 2018-06-30 18:54:56 +0000 | [diff] [blame] | 653 | }else if( tokenType==TK_OVER ){ |
| 654 | assert( n==4 ); |
| 655 | tokenType = analyzeOverKeyword((const u8*)&zSql[4], lastTokenParsed); |
| 656 | }else if( tokenType==TK_FILTER ){ |
| 657 | assert( n==6 ); |
| 658 | tokenType = analyzeFilterKeyword((const u8*)&zSql[6], lastTokenParsed); |
drh | c7bf571 | 2018-07-09 22:49:01 +0000 | [diff] [blame] | 659 | #endif /* SQLITE_OMIT_WINDOWFUNC */ |
dan | d437ac0 | 2018-06-29 20:21:24 +0000 | [diff] [blame] | 660 | }else{ |
drh | 4e53295 | 2022-02-08 13:41:23 +0000 | [diff] [blame] | 661 | Token x; |
| 662 | x.z = zSql; |
| 663 | x.n = n; |
| 664 | sqlite3ErrorMsg(pParse, "unrecognized token: \"%T\"", &x); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 665 | break; |
drh | 32eb7b4 | 2003-01-07 01:44:37 +0000 | [diff] [blame] | 666 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 667 | } |
dan | d437ac0 | 2018-06-29 20:21:24 +0000 | [diff] [blame] | 668 | pParse->sLastToken.z = zSql; |
| 669 | pParse->sLastToken.n = n; |
| 670 | sqlite3Parser(pEngine, tokenType, pParse->sLastToken); |
| 671 | lastTokenParsed = tokenType; |
| 672 | zSql += n; |
drh | 1cf1975 | 2019-02-08 14:55:30 +0000 | [diff] [blame] | 673 | assert( db->mallocFailed==0 || pParse->rc!=SQLITE_OK || startedWithOom ); |
| 674 | if( pParse->rc!=SQLITE_OK ) break; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 675 | } |
drh | 0be0cf6 | 2015-04-15 08:37:42 +0000 | [diff] [blame] | 676 | assert( nErr==0 ); |
drh | ec424a5 | 2008-07-25 15:39:03 +0000 | [diff] [blame] | 677 | #ifdef YYTRACKMAXSTACKDEPTH |
drh | af89fe6 | 2015-03-23 17:25:18 +0000 | [diff] [blame] | 678 | sqlite3_mutex_enter(sqlite3MallocMutex()); |
drh | b02392e | 2015-10-15 15:28:56 +0000 | [diff] [blame] | 679 | sqlite3StatusHighwater(SQLITE_STATUS_PARSER_STACK, |
drh | ec424a5 | 2008-07-25 15:39:03 +0000 | [diff] [blame] | 680 | sqlite3ParserStackPeak(pEngine) |
| 681 | ); |
drh | af89fe6 | 2015-03-23 17:25:18 +0000 | [diff] [blame] | 682 | sqlite3_mutex_leave(sqlite3MallocMutex()); |
drh | ec424a5 | 2008-07-25 15:39:03 +0000 | [diff] [blame] | 683 | #endif /* YYDEBUG */ |
drh | d26cc54 | 2017-01-28 20:46:37 +0000 | [diff] [blame] | 684 | #ifdef sqlite3Parser_ENGINEALWAYSONSTACK |
| 685 | sqlite3ParserFinalize(pEngine); |
| 686 | #else |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 687 | sqlite3ParserFree(pEngine, sqlite3_free); |
drh | d26cc54 | 2017-01-28 20:46:37 +0000 | [diff] [blame] | 688 | #endif |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 689 | if( db->mallocFailed ){ |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 690 | pParse->rc = SQLITE_NOMEM_BKPT; |
drh | 71c697e | 2004-08-08 23:39:19 +0000 | [diff] [blame] | 691 | } |
drh | 1611826 | 2021-12-31 17:54:48 +0000 | [diff] [blame] | 692 | if( pParse->zErrMsg || (pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE) ){ |
| 693 | if( pParse->zErrMsg==0 ){ |
| 694 | pParse->zErrMsg = sqlite3MPrintf(db, "%s", sqlite3ErrStr(pParse->rc)); |
| 695 | } |
drh | 54bc638 | 2021-12-31 19:20:42 +0000 | [diff] [blame] | 696 | sqlite3_log(pParse->rc, "%s in \"%s\"", pParse->zErrMsg, pParse->zTail); |
drh | 4b2f936 | 2008-01-22 23:37:09 +0000 | [diff] [blame] | 697 | nErr++; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 698 | } |
drh | 875ad8c | 2018-06-21 23:53:54 +0000 | [diff] [blame] | 699 | pParse->zTail = zSql; |
drh | 4f3dd15 | 2008-04-28 18:46:43 +0000 | [diff] [blame] | 700 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | b975598 | 2010-07-24 16:34:37 +0000 | [diff] [blame] | 701 | sqlite3_free(pParse->apVtabLock); |
drh | 4f3dd15 | 2008-04-28 18:46:43 +0000 | [diff] [blame] | 702 | #endif |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 +0000 | [diff] [blame] | 703 | |
drh | 1611826 | 2021-12-31 17:54:48 +0000 | [diff] [blame] | 704 | if( pParse->pNewTable && !IN_SPECIAL_PARSE ){ |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 +0000 | [diff] [blame] | 705 | /* If the pParse->declareVtab flag is set, do not delete any table |
| 706 | ** structure built up in pParse->pNewTable. The calling code (see vtab.c) |
| 707 | ** will take responsibility for freeing the Table structure. |
| 708 | */ |
dan | 1feeaed | 2010-07-23 15:41:47 +0000 | [diff] [blame] | 709 | sqlite3DeleteTable(db, pParse->pNewTable); |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 +0000 | [diff] [blame] | 710 | } |
drh | 1611826 | 2021-12-31 17:54:48 +0000 | [diff] [blame] | 711 | if( pParse->pNewTrigger && !IN_RENAME_OBJECT ){ |
dan | 5496d6a | 2018-08-13 17:14:26 +0000 | [diff] [blame] | 712 | sqlite3DeleteTrigger(db, pParse->pNewTrigger); |
| 713 | } |
drh | da3ec15 | 2022-03-28 14:18:03 +0000 | [diff] [blame] | 714 | if( pParse->pVList ) sqlite3DbFreeNN(db, pParse->pVList); |
dan | 488b558 | 2021-11-16 13:36:50 +0000 | [diff] [blame] | 715 | db->pParse = pParentParse; |
drh | f3151f0 | 2015-04-16 20:27:09 +0000 | [diff] [blame] | 716 | assert( nErr==0 || pParse->rc!=SQLITE_OK ); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 717 | return nErr; |
| 718 | } |
drh | 643d855 | 2018-12-10 16:00:57 +0000 | [diff] [blame] | 719 | |
| 720 | |
| 721 | #ifdef SQLITE_ENABLE_NORMALIZE |
| 722 | /* |
| 723 | ** Insert a single space character into pStr if the current string |
| 724 | ** ends with an identifier |
| 725 | */ |
| 726 | static void addSpaceSeparator(sqlite3_str *pStr){ |
| 727 | if( pStr->nChar && sqlite3IsIdChar(pStr->zText[pStr->nChar-1]) ){ |
| 728 | sqlite3_str_append(pStr, " ", 1); |
| 729 | } |
| 730 | } |
| 731 | |
| 732 | /* |
| 733 | ** Compute a normalization of the SQL given by zSql[0..nSql-1]. Return |
| 734 | ** the normalization in space obtained from sqlite3DbMalloc(). Or return |
| 735 | ** NULL if anything goes wrong or if zSql is NULL. |
| 736 | */ |
| 737 | char *sqlite3Normalize( |
| 738 | Vdbe *pVdbe, /* VM being reprepared */ |
drh | 1a6c2b1 | 2018-12-10 20:01:40 +0000 | [diff] [blame] | 739 | const char *zSql /* The original SQL string */ |
drh | 643d855 | 2018-12-10 16:00:57 +0000 | [diff] [blame] | 740 | ){ |
| 741 | sqlite3 *db; /* The database connection */ |
| 742 | int i; /* Next unread byte of zSql[] */ |
| 743 | int n; /* length of current token */ |
| 744 | int tokenType; /* type of current token */ |
mistachkin | 844b900 | 2019-02-02 01:27:45 +0000 | [diff] [blame] | 745 | int prevType = 0; /* Previous non-whitespace token */ |
drh | 643d855 | 2018-12-10 16:00:57 +0000 | [diff] [blame] | 746 | int nParen; /* Number of nested levels of parentheses */ |
| 747 | int iStartIN; /* Start of RHS of IN operator in z[] */ |
| 748 | int nParenAtIN; /* Value of nParent at start of RHS of IN operator */ |
mistachkin | 16fd04c | 2019-10-16 17:46:22 +0000 | [diff] [blame] | 749 | u32 j; /* Bytes of normalized SQL generated so far */ |
drh | 643d855 | 2018-12-10 16:00:57 +0000 | [diff] [blame] | 750 | sqlite3_str *pStr; /* The normalized SQL string under construction */ |
| 751 | |
drh | 643d855 | 2018-12-10 16:00:57 +0000 | [diff] [blame] | 752 | db = sqlite3VdbeDb(pVdbe); |
| 753 | tokenType = -1; |
| 754 | nParen = iStartIN = nParenAtIN = 0; |
| 755 | pStr = sqlite3_str_new(db); |
drh | 1a6c2b1 | 2018-12-10 20:01:40 +0000 | [diff] [blame] | 756 | assert( pStr!=0 ); /* sqlite3_str_new() never returns NULL */ |
| 757 | for(i=0; zSql[i] && pStr->accError==0; i+=n){ |
drh | 643d855 | 2018-12-10 16:00:57 +0000 | [diff] [blame] | 758 | if( tokenType!=TK_SPACE ){ |
| 759 | prevType = tokenType; |
| 760 | } |
| 761 | n = sqlite3GetToken((unsigned char*)zSql+i, &tokenType); |
| 762 | if( NEVER(n<=0) ) break; |
| 763 | switch( tokenType ){ |
| 764 | case TK_SPACE: { |
| 765 | break; |
| 766 | } |
| 767 | case TK_NULL: { |
| 768 | if( prevType==TK_IS || prevType==TK_NOT ){ |
| 769 | sqlite3_str_append(pStr, " NULL", 5); |
| 770 | break; |
| 771 | } |
| 772 | /* Fall through */ |
| 773 | } |
| 774 | case TK_STRING: |
| 775 | case TK_INTEGER: |
| 776 | case TK_FLOAT: |
| 777 | case TK_VARIABLE: |
| 778 | case TK_BLOB: { |
| 779 | sqlite3_str_append(pStr, "?", 1); |
| 780 | break; |
| 781 | } |
| 782 | case TK_LP: { |
| 783 | nParen++; |
| 784 | if( prevType==TK_IN ){ |
| 785 | iStartIN = pStr->nChar; |
| 786 | nParenAtIN = nParen; |
| 787 | } |
| 788 | sqlite3_str_append(pStr, "(", 1); |
| 789 | break; |
| 790 | } |
| 791 | case TK_RP: { |
| 792 | if( iStartIN>0 && nParen==nParenAtIN ){ |
mistachkin | 16fd04c | 2019-10-16 17:46:22 +0000 | [diff] [blame] | 793 | assert( pStr->nChar>=(u32)iStartIN ); |
drh | 643d855 | 2018-12-10 16:00:57 +0000 | [diff] [blame] | 794 | pStr->nChar = iStartIN+1; |
| 795 | sqlite3_str_append(pStr, "?,?,?", 5); |
| 796 | iStartIN = 0; |
| 797 | } |
| 798 | nParen--; |
| 799 | sqlite3_str_append(pStr, ")", 1); |
| 800 | break; |
| 801 | } |
| 802 | case TK_ID: { |
drh | 9042ff2 | 2018-12-10 16:49:33 +0000 | [diff] [blame] | 803 | iStartIN = 0; |
drh | 643d855 | 2018-12-10 16:00:57 +0000 | [diff] [blame] | 804 | j = pStr->nChar; |
| 805 | if( sqlite3Isquote(zSql[i]) ){ |
| 806 | char *zId = sqlite3DbStrNDup(db, zSql+i, n); |
| 807 | int nId; |
| 808 | int eType = 0; |
| 809 | if( zId==0 ) break; |
| 810 | sqlite3Dequote(zId); |
| 811 | if( zSql[i]=='"' && sqlite3VdbeUsesDoubleQuotedString(pVdbe, zId) ){ |
| 812 | sqlite3_str_append(pStr, "?", 1); |
| 813 | sqlite3DbFree(db, zId); |
| 814 | break; |
| 815 | } |
| 816 | nId = sqlite3Strlen30(zId); |
| 817 | if( sqlite3GetToken((u8*)zId, &eType)==nId && eType==TK_ID ){ |
| 818 | addSpaceSeparator(pStr); |
| 819 | sqlite3_str_append(pStr, zId, nId); |
| 820 | }else{ |
| 821 | sqlite3_str_appendf(pStr, "\"%w\"", zId); |
| 822 | } |
| 823 | sqlite3DbFree(db, zId); |
| 824 | }else{ |
| 825 | addSpaceSeparator(pStr); |
| 826 | sqlite3_str_append(pStr, zSql+i, n); |
| 827 | } |
| 828 | while( j<pStr->nChar ){ |
| 829 | pStr->zText[j] = sqlite3Tolower(pStr->zText[j]); |
| 830 | j++; |
| 831 | } |
| 832 | break; |
| 833 | } |
drh | 9042ff2 | 2018-12-10 16:49:33 +0000 | [diff] [blame] | 834 | case TK_SELECT: { |
| 835 | iStartIN = 0; |
| 836 | /* fall through */ |
| 837 | } |
drh | 643d855 | 2018-12-10 16:00:57 +0000 | [diff] [blame] | 838 | default: { |
| 839 | if( sqlite3IsIdChar(zSql[i]) ) addSpaceSeparator(pStr); |
| 840 | j = pStr->nChar; |
| 841 | sqlite3_str_append(pStr, zSql+i, n); |
| 842 | while( j<pStr->nChar ){ |
| 843 | pStr->zText[j] = sqlite3Toupper(pStr->zText[j]); |
| 844 | j++; |
| 845 | } |
| 846 | break; |
| 847 | } |
| 848 | } |
| 849 | } |
drh | 9042ff2 | 2018-12-10 16:49:33 +0000 | [diff] [blame] | 850 | if( tokenType!=TK_SEMI ) sqlite3_str_append(pStr, ";", 1); |
drh | 643d855 | 2018-12-10 16:00:57 +0000 | [diff] [blame] | 851 | return sqlite3_str_finish(pStr); |
| 852 | } |
| 853 | #endif /* SQLITE_ENABLE_NORMALIZE */ |