| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent c4b1c55 commit 84f4402
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -15,6 +15,8 @@ | |||
| 15 | 15 | [\w]+[\u4e00-\u9fa5]+[0-9a-zA-Z_\u4e00-\u9fa5]* return 'IDENTIFIER' | |
| 16 | 16 | [\u4e00-\u9fa5][0-9a-zA-Z_\u4e00-\u9fa5]* return 'IDENTIFIER' | |
| 17 | 17 | SELECT return 'SELECT' | |
| 18 | + INSERT return 'INSERT' | ||
| 19 | + DEFAULT return 'DEFAULT' | ||
| 18 | 20 | ALL return 'ALL' | |
| 19 | 21 | ANY return 'ANY' | |
| 20 | 22 | DISTINCT return 'DISTINCT' | |
@@ -67,6 +69,8 @@ ORDER\s+BY return 'ORDER_ | |||
| 67 | 69 | GROUP\s+BY return 'GROUP_BY' | |
| 68 | 70 | IGNORE return 'IGNORE' | |
| 69 | 71 | LOW_PRIORITY return 'LOW_PRIORITY' | |
| 72 | + DELAYED return 'DELAYED' | ||
| 73 | + HIGH_PRIORITY return 'HIGH_PRIORITY' | ||
| 70 | 74 | FORCE return 'FORCE' | |
| 71 | 75 | INNER return 'INNER' | |
| 72 | 76 | CROSS return 'CROSS' | |
@@ -91,6 +95,12 @@ SHARE return 'SHARE' | |||
| 91 | 95 | MODE return 'MODE' | |
| 92 | 96 | OJ return 'OJ' | |
| 93 | 97 | LIMIT return 'LIMIT' | |
| 98 | + INTO return 'INTO' | ||
| 99 | + VALUE return 'VALUE' | ||
| 100 | + VALUES return 'VALUES' | ||
| 101 | + DUPLICATE return 'DUPLICATE' | ||
| 102 | + KEY return 'KEY' | ||
| 103 | + UPDATE return 'UPDATE' | ||
| 94 | 104 | ||
| 95 | 105 | "," return ',' | |
| 96 | 106 | "=" return '=' | |
@@ -168,13 +178,74 @@ main | |||
| 168 | 178 | query | |
| 169 | 179 | : selectClause | |
| 170 | 180 | | updateClause | |
| 181 | + | insertClause | ||
| 182 | + ; | ||
| 183 | + | ||
| 184 | + insertClause | ||
| 185 | + : INSERT priority_opt ignore_opt | ||
| 186 | + into_opt simple_table_factor | ||
| 187 | + partitionOpt | ||
| 188 | + insert_cols | ||
| 189 | + insert_source | ||
| 190 | + on_dup_assigns | ||
| 191 | + { | ||
| 192 | + $$ = { | ||
| 193 | + type: 'Insert', | ||
| 194 | + priority: $2, | ||
| 195 | + ignore: $3, | ||
| 196 | + into: $4, | ||
| 197 | + table: $5, | ||
| 198 | + partitions: $6, | ||
| 199 | + cols: $7, | ||
| 200 | + src: $8, | ||
| 201 | + duplicateAssignments: $9 | ||
| 202 | + } | ||
| 203 | + } | ||
| 204 | + ; | ||
| 205 | + | ||
| 206 | + insert_source | ||
| 207 | + : insert_value value_list_list { $$ = { type: 'Values', keyword: $1, values: $2 } } | ||
| 208 | + | selectClause | ||
| 209 | + ; | ||
| 210 | + | ||
| 211 | + on_dup_assigns | ||
| 212 | + : { $$ = null} | ||
| 213 | + | ON DUPLICATE KEY UPDATE assignment_list { $$ = $5 } | ||
| 214 | + ; | ||
| 215 | + | ||
| 216 | + insert_cols | ||
| 217 | + : { $$ = null} | ||
| 218 | + | '(' identifier_list ')' { $$ = $2 } | ||
| 219 | + ; | ||
| 220 | + | ||
| 221 | + value | ||
| 222 | + : expr | DEFAULT | ||
| 223 | + ; | ||
| 224 | + | ||
| 225 | + value_list | ||
| 226 | + : value_list ',' value { $1.value.push($3); } | ||
| 227 | + | value { $$ = { type: 'ValueList', value: [ $1 ] } } | ||
| 228 | + ; | ||
| 229 | + | ||
| 230 | + value_list_list | ||
| 231 | + : value_list_list ',' '(' value_list ')' { $1.value.push($4); } | ||
| 232 | + | '(' value_list ')' { $$ = { type: 'InsertList', value: [ $2 ] } } | ||
| 233 | + ; | ||
| 234 | + | ||
| 235 | + insert_value | ||
| 236 | + : VALUE | VALUES | ||
| 237 | + ; | ||
| 238 | + | ||
| 239 | + into_opt | ||
| 240 | + : { $$ = null } | ||
| 241 | + | INTO { $$ = $1 } | ||
| 171 | 242 | ; | |
| 172 | 243 | ||
| 173 | 244 | updateClause | |
| 174 | 245 | : UPDATE low_priority_opt ignore_opt | |
| 175 | 246 | table_refrences | |
| 176 | 247 | SET | |
| 177 | - assignmentList | ||
| 248 | + assignment_list | ||
| 178 | 249 | where_opt | |
| 179 | 250 | order_by_opt | |
| 180 | 251 | limit_opt | |
@@ -197,6 +268,13 @@ low_priority_opt | |||
| 197 | 268 | | LOW_PRIORITY { $$ = $1 } | |
| 198 | 269 | ; | |
| 199 | 270 | ||
| 271 | + priority_opt | ||
| 272 | + : { $$ = null } | ||
| 273 | + | LOW_PRIORITY { $$ = $1 } | ||
| 274 | + | HIGH_PRIORITY { $$ = $1 } | ||
| 275 | + | DELAYED { $$ = $1 } | ||
| 276 | + ; | ||
| 277 | + | ||
| 200 | 278 | ignore_opt | |
| 201 | 279 | : { $$ = null } | |
| 202 | 280 | | IGNORE { $$ = $1 } | |
@@ -206,8 +284,8 @@ assignment | |||
| 206 | 284 | : identifier '=' expr { $$ = { type: 'Assignment', left: $1, right: $3 } } | |
| 207 | 285 | ; | |
| 208 | 286 | ||
| 209 | - assignmentList | ||
| 210 | - : assignmentList ',' assignment { $1.value.push($3); } | ||
| 287 | + assignment_list | ||
| 288 | + : assignment_list ',' assignment { $1.value.push($3); } | ||
| 211 | 289 | | assignment { $$ = { type: 'AssignmentList', value: [ $1 ] } } | |
| 212 | 290 | ; | |
| 213 | 291 | ||
@@ -603,8 +681,11 @@ index_hint | |||
| 603 | 681 | | IGNORE index_or_key for_opt '(' identifier_list ')' { $$ = { type: 'IgnoreIndexHint', value: $5, forOpt: $3, indexOrKey: $2 } } | |
| 604 | 682 | | FORCE index_or_key for_opt '(' identifier_list ')' { $$ = { type: 'ForceIndexHint', value: $5, forOpt: $3, indexOrKey: $2 } } | |
| 605 | 683 | ; | |
| 606 | - table_factor | ||
| 684 | + simple_table_factor | ||
| 607 | 685 | : identifier partitionOpt aliasOpt index_hint_list_opt { $$ = { type: 'TableFactor', value: $1, partition: $2, alias: $3.alias, hasAs: $3.hasAs, indexHintOpt: $4 } } | |
| 686 | + ; | ||
| 687 | + table_factor | ||
| 688 | + : simple_table_factor | ||
| 608 | 689 | | '(' selectClause ')' aliasOpt { $$ = { type: 'SubQuery', value: $2, alias: $4.alias, hasAs: $4.hasAs } } | |
| 609 | 690 | | '(' table_refrences ')' { $$ = $2; $$.hasParentheses = true } | |
| 610 | 691 | ; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -59,6 +59,8 @@ Sql.prototype.travelMain = function(ast) { | |||
| 59 | 59 | this.travelSelect(ast.value); | |
| 60 | 60 | } else if (ast.value.type === 'Update') { | |
| 61 | 61 | this.travelUpdate(ast.value); | |
| 62 | + } else if (ast.value.type === 'Insert') { | ||
| 63 | + this.travelInsert(ast.value); | ||
| 62 | 64 | } else { | |
| 63 | 65 | throw new Error('Unknown query value type'); | |
| 64 | 66 | } | |
@@ -129,6 +131,64 @@ Sql.prototype.travelSelect = function(ast) { | |||
| 129 | 131 | this.appendKeyword(ast.updateLockMode); | |
| 130 | 132 | } | |
| 131 | 133 | } | |
| 134 | + Sql.prototype.travelInsert = function(ast) { | ||
| 135 | + this.appendKeyword('insert', true); | ||
| 136 | + | ||
| 137 | + if (ast.lowPriority) { | ||
| 138 | + this.appendKeyword('low_priority'); | ||
| 139 | + } | ||
| 140 | + if (ast.ignore) { | ||
| 141 | + this.appendKeyword('ignore'); | ||
| 142 | + } | ||
| 143 | + if (ast.into) { | ||
| 144 | + this.appendKeyword('into'); | ||
| 145 | + } | ||
| 146 | + this.travelTableRefrence(ast.table); | ||
| 147 | + if (ast.partitions) { | ||
| 148 | + this.travelPartitions(ast.partitions); | ||
| 149 | + } | ||
| 150 | + if (ast.cols) { | ||
| 151 | + this.travel('('); | ||
| 152 | + this.travelIdentifierList(ast.cols); | ||
| 153 | + this.travel(')'); | ||
| 154 | + } | ||
| 155 | + this.travel(ast.value); | ||
| 156 | + if (ast.src.type === 'Select') { | ||
| 157 | + this.travelSelect(ast.src); | ||
| 158 | + } else if (ast.src.type === 'Values') { | ||
| 159 | + this.travel(ast.src.keyword); | ||
| 160 | + this.travelInsertRows(ast.src.values); | ||
| 161 | + } | ||
| 162 | + if (ast.duplicateAssignments) { | ||
| 163 | + this.appendKeyword('ON'); | ||
| 164 | + this.appendKeyword('DUPLICATE'); | ||
| 165 | + this.appendKeyword('KEY'); | ||
| 166 | + this.appendKeyword('UPDATE'); | ||
| 167 | + this.travelAssignments(ast.duplicateAssignments); | ||
| 168 | + } | ||
| 169 | + } | ||
| 170 | + Sql.prototype.travelInsertRows = function(ast) { | ||
| 171 | + for (var i = 0; i < ast.value.length; i++) { | ||
| 172 | + var x = ast.value[i]; | ||
| 173 | + this.travel('('); | ||
| 174 | + this.travelValueList(x.value); | ||
| 175 | + this.travel(')'); | ||
| 176 | + | ||
| 177 | + if (i !== ast.value.length - 1) { | ||
| 178 | + this.append(',', true); | ||
| 179 | + } | ||
| 180 | + } | ||
| 181 | + } | ||
| 182 | + Sql.prototype.travelValueList = function(ast) { | ||
| 183 | + for (var i = 0; i < ast.length; i++) { | ||
| 184 | + var x = ast[i]; | ||
| 185 | + this.travel(x); | ||
| 186 | + | ||
| 187 | + if (i !== ast.length - 1) { | ||
| 188 | + this.append(',', true); | ||
| 189 | + } | ||
| 190 | + } | ||
| 191 | + } | ||
| 132 | 192 | Sql.prototype.travelUpdate = function(ast) { | |
| 133 | 193 | this.appendKeyword('update', true); | |
| 134 | 194 | if (ast.lowPriority) { | |
@@ -154,9 +214,9 @@ Sql.prototype.travelUpdate = function(ast) { | |||
| 154 | 214 | Sql.prototype.travelAssignments = function(ast) { | |
| 155 | 215 | for (var i = 0; i < ast.value.length; i++) { | |
| 156 | 216 | var x = ast.value[i]; | |
| 157 | - this.travelIdentifier(x.left); | ||
| 217 | + this.travel(x.left); | ||
| 158 | 218 | this.travel('='); | |
| 159 | - this.travelIdentifier(x.right); | ||
| 219 | + this.travel(x.right); | ||
| 160 | 220 | ||
| 161 | 221 | if (i !== ast.value.length - 1) { | |
| 162 | 222 | this.append(',', true); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -137,6 +137,10 @@ AND (rd.rd_numberofrooms <= (select sum(rn.reservation_numberofrooms) as count_r | |||
| 137 | 137 | testParser('select a from b limit 2;'); | |
| 138 | 138 | }); | |
| 139 | 139 | ||
| 140 | + it ('subquery.', function () { | ||
| 141 | + testParser('select a from (select 1 as x) b limit 2;'); | ||
| 142 | + }); | ||
| 143 | + | ||
| 140 | 144 | it('update0', function () { | |
| 141 | 145 | testParser('update A SET b=2 WHERE c=3;'); | |
| 142 | 146 | }); | |
@@ -148,4 +152,43 @@ AND (rd.rd_numberofrooms <= (select sum(rn.reservation_numberofrooms) as count_r | |||
| 148 | 152 | it('update2', function () { | |
| 149 | 153 | testParser('update LOW_PRIORITY ignore A, B, C SET b=2, `C`.c=d WHERE e=4 ORDER BY d DESC limit 1;'); | |
| 150 | 154 | }); | |
| 155 | + | ||
| 156 | + it('insert0', function () { | ||
| 157 | + testParser('INSERT INTO A VALUES (a, `b`), (c, 3)'); | ||
| 158 | + }); | ||
| 159 | + | ||
| 160 | + it('insert1', function () { | ||
| 161 | + testParser('INSERT DELAYED IGNORE INTO A VALUE (a, DEFAULT, 3+2)'); | ||
| 162 | + }); | ||
| 163 | + | ||
| 164 | + it('insert2', function () { | ||
| 165 | + testParser('INSERT DELAYED IGNORE INTO A VALUE (a, `b`)'); | ||
| 166 | + }); | ||
| 167 | + | ||
| 168 | + it('insert3', function () { | ||
| 169 | + testParser('INSERT DELAYED IGNORE INTO A (`a`, b, c) VALUE (1, 2+3)'); | ||
| 170 | + }); | ||
| 171 | + | ||
| 172 | + it('insert4', function () { | ||
| 173 | + testParser('INSERT DELAYED IGNORE INTO `s0`.`A` PARTITION (p0, p1) (`a`, b, c) VALUE (1, 2+3)'); | ||
| 174 | + }); | ||
| 175 | + | ||
| 176 | + it('insert5', function () { | ||
| 177 | + testParser(` | ||
| 178 | + INSERT DELAYED IGNORE INTO \`s0\`.\`A\` | ||
| 179 | + PARTITION (p0, p1) (\`a\`, b, c) | ||
| 180 | + VALUE (1, 2+3), (5, '6') | ||
| 181 | + ON DUPLICATE KEY UPDATE q=q+1, c=b | ||
| 182 | + `); | ||
| 183 | + }); | ||
| 184 | + | ||
| 185 | + it('insert6', function () { | ||
| 186 | + testParser(` | ||
| 187 | + INSERT HIGH_PRIORITY \`massdrop\`.\`A\` | ||
| 188 | + PARTITION (p0, p1) (\`a\`, b, c) | ||
| 189 | + SELECT 1+1 as b, d | ||
| 190 | + FROM B | ||
| 191 | + ON DUPLICATE KEY UPDATE q=q+1, c=b | ||
| 192 | + `); | ||
| 193 | + }); | ||
| 151 | 194 | }); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments