Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Implement INSERT and UPDATE parsing#10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
gpittarelli wants to merge3 commits intoJavaScriptor:master
base:master
Choose a base branch
Loading
fromgpittarelli:master
Open
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148 changes: 139 additions & 9 deletionssrc/sqlParser.jison
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -10,11 +10,13 @@
[-][-]\s.*\n /* skip sql comments */
[#]\s.*\n /* skip sql comments */
\s+ /* skip whitespace */

[`][a-zA-Z_\u4e00-\u9fa5][a-zA-Z0-9_\u4e00-\u9fa5]*[`] return 'IDENTIFIER'
[\w]+[\u4e00-\u9fa5]+[0-9a-zA-Z_\u4e00-\u9fa5]* return 'IDENTIFIER'
[\u4e00-\u9fa5][0-9a-zA-Z_\u4e00-\u9fa5]* return 'IDENTIFIER'
SELECT return 'SELECT'
INSERT return 'INSERT'
DEFAULT return 'DEFAULT'
ALL return 'ALL'
ANY return 'ANY'
DISTINCT return 'DISTINCT'
Expand DownExpand Up@@ -66,6 +68,9 @@ JOIN return 'JOIN'
ORDER\s+BY return 'ORDER_BY'
GROUP\s+BY return 'GROUP_BY'
IGNORE return 'IGNORE'
LOW_PRIORITY return 'LOW_PRIORITY'
DELAYED return 'DELAYED'
HIGH_PRIORITY return 'HIGH_PRIORITY'
FORCE return 'FORCE'
INNER return 'INNER'
CROSS return 'CROSS'
Expand All@@ -82,13 +87,20 @@ WITH return 'WITH'
ROLLUP return 'ROLLUP'
HAVING return 'HAVING'
OFFSET return 'OFFSET'
SET return 'SET'
PROCEDURE return 'PROCEDURE'
UPDATE return 'UPDATE'
LOCK return 'LOCK'
SHARE return 'SHARE'
MODE return 'MODE'
OJ return 'OJ'
LIMIT return 'LIMIT'
INTO return 'INTO'
VALUE return 'VALUE'
VALUES return 'VALUES'
DUPLICATE return 'DUPLICATE'
KEY return 'KEY'
UPDATE return 'UPDATE'

"," return ','
"=" return '='
Expand DownExpand Up@@ -116,7 +128,7 @@ LIMIT return 'LIMIT'
"{" return '{'
"}" return '}'
";" return ';'

['](\\.|[^'])*['] return 'STRING'
["](\\.|[^"])*["] return 'STRING'
[0][x][0-9a-fA-F]+ return 'HEX_NUMERIC'
Expand All@@ -126,7 +138,7 @@ LIMIT return 'LIMIT'
[a-zA-Z_\u4e00-\u9fa5][a-zA-Z0-9_\u4e00-\u9fa5]* return 'IDENTIFIER'
\. return 'DOT'
['"][a-zA-Z_\u4e00-\u9fa5][a-zA-Z0-9_\u4e00-\u9fa5]*["'] return 'QUOTED_IDENTIFIER'

<<EOF>> return 'EOF'
. return 'INVALID'

Expand DownExpand Up@@ -159,12 +171,126 @@ LIMIT return 'LIMIT'
%% /* language grammar */

main
: selectClause EOF { return {nodeType: 'Main', value: $1}; }
| selectClause ';' EOF { return {nodeType: 'Main', value: $1, hasSemicolon: true}; }
: query EOF { return {nodeType: 'Main', value: $1}; }
| query ';' EOF { return {nodeType: 'Main', value: $1, hasSemicolon: true}; }
;

query
: selectClause
| updateClause
| insertClause
;

insertClause
: INSERT priority_opt ignore_opt
into_opt simple_table_factor
partitionOpt
insert_cols
insert_source
on_dup_assigns
{
$$ = {
type: 'Insert',
priority: $2,
ignore: $3,
into: $4,
table: $5,
partitions: $6,
cols: $7,
src: $8,
duplicateAssignments: $9
}
}
;

insert_source
: insert_value value_list_list { $$ = { type: 'Values', keyword: $1, values: $2 } }
| selectClause
;

on_dup_assigns
: { $$ = null}
| ON DUPLICATE KEY UPDATE assignment_list { $$ = $5 }
;

insert_cols
: { $$ = null}
| '(' identifier_list ')' { $$ = $2 }
;

value
: expr | DEFAULT
;

value_list
: value_list ',' value { $1.value.push($3); }
| value { $$ = { type: 'ValueList', value: [ $1 ] } }
;

value_list_list
: value_list_list ',' '(' value_list ')' { $1.value.push($4); }
| '(' value_list ')' { $$ = { type: 'InsertList', value: [ $2 ] } }
;

insert_value
: VALUE | VALUES
;

into_opt
: { $$ = null }
| INTO { $$ = $1 }
;

updateClause
: UPDATE low_priority_opt ignore_opt
table_refrences
SET
assignment_list
where_opt
order_by_opt
limit_opt
{
$$ = {
type: 'Update',
lowPriority: $2,
ignore: $3,
tables: $4,
assignments: $6,
where: $7,
orderBy: $8,
limit: $9
}
}
;

low_priority_opt
: { $$ = null }
| LOW_PRIORITY { $$ = $1 }
;

priority_opt
: { $$ = null }
| LOW_PRIORITY { $$ = $1 }
| HIGH_PRIORITY { $$ = $1 }
| DELAYED { $$ = $1 }
;

ignore_opt
: { $$ = null }
| IGNORE { $$ = $1 }
;

assignment
: identifier '=' expr { $$ = { type: 'Assignment', left: $1, right: $3 } }
;

assignment_list
: assignment_list ',' assignment { $1.value.push($3); }
| assignment { $$ = { type: 'AssignmentList', value: [ $1 ] } }
;

selectClause
: SELECT
: SELECT
distinctOpt
highPriorityOpt
maxStateMentTimeOpt
Expand DownExpand Up@@ -203,7 +329,7 @@ selectClause
;

distinctOpt
: ALL { $$ = $1 }
: ALL { $$ = $1 }
| DISTINCT { $$ = $1 }
| DISTINCTROW { $$ = $1 }
| { $$ = null }
Expand DownExpand Up@@ -280,6 +406,7 @@ literal
;
function_call
: IDENTIFIER '(' function_call_param_list ')' { $$ = {type: 'FunctionCall', name: $1, params: $3} }
| VALUES '(' function_call_param_list ')' { $$ = {type: 'FunctionCall', name: $1, params: $3} }
;
function_call_param_list
: function_call_param_list ',' function_call_param { $1.push($3); $$ = $1; }
Expand DownExpand Up@@ -336,7 +463,7 @@ simple_expr
;
bit_expr
: simple_expr { $$ = $1 }
| bit_expr '|' bit_expr { $$ = { type: 'BitExpression', operator: '|', left: $1, right: $3 } }
| bit_expr '|' bit_expr { $$ = { type: 'BitExpression', operator: '|', left: $1, right: $3 } }
| bit_expr '&' bit_expr { $$ = { type: 'BitExpression', operator: '&', left: $1, right: $3 } }
| bit_expr '<<' bit_expr { $$ = { type: 'BitExpression', operator: '<<', left: $1, right: $3 } }
| bit_expr '>>' bit_expr { $$ = { type: 'BitExpression', operator: '>>', left: $1, right: $3 } }
Expand DownExpand Up@@ -555,8 +682,11 @@ index_hint
| IGNORE index_or_key for_opt '(' identifier_list ')' { $$ = { type: 'IgnoreIndexHint', value: $5, forOpt: $3, indexOrKey: $2 } }
| FORCE index_or_key for_opt '(' identifier_list ')' { $$ = { type: 'ForceIndexHint', value: $5, forOpt: $3, indexOrKey: $2 } }
;
table_factor
simple_table_factor
: identifier partitionOpt aliasOpt index_hint_list_opt { $$ = { type: 'TableFactor', value: $1, partition: $2, alias: $3.alias, hasAs: $3.hasAs, indexHintOpt: $4 } }
;
table_factor
: simple_table_factor
| '(' selectClause ')' aliasOpt { $$ = { type: 'SubQuery', value: $2, alias: $4.alias, hasAs: $4.hasAs } }
| '(' table_refrences ')' { $$ = $2; $$.hasParentheses = true }
;
106 changes: 103 additions & 3 deletionssrc/stringify.js
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -55,7 +55,15 @@ Sql.prototype.append = function(word, noPrefix, noSuffix) {
}
}
Sql.prototype.travelMain = function(ast) {
this.travelSelect(ast.value);
if (ast.value.type === 'Select') {
this.travelSelect(ast.value);
} else if (ast.value.type === 'Update') {
this.travelUpdate(ast.value);
} else if (ast.value.type === 'Insert') {
this.travelInsert(ast.value);
} else {
throw new Error('Unknown query value type');
}
if (ast.hasSemicolon) {
this.append(';', true);
}
Expand DownExpand Up@@ -123,6 +131,98 @@ Sql.prototype.travelSelect = function(ast) {
this.appendKeyword(ast.updateLockMode);
}
}
Sql.prototype.travelInsert = function(ast) {
this.appendKeyword('insert', true);

if (ast.lowPriority) {
this.appendKeyword('low_priority');
}
if (ast.ignore) {
this.appendKeyword('ignore');
}
if (ast.into) {
this.appendKeyword('into');
}
this.travelTableRefrence(ast.table);
if (ast.partitions) {
this.travelPartitions(ast.partitions);
}
if (ast.cols) {
this.travel('(');
this.travelIdentifierList(ast.cols);
this.travel(')');
}
this.travel(ast.value);
if (ast.src.type === 'Select') {
this.travelSelect(ast.src);
} else if (ast.src.type === 'Values') {
this.travel(ast.src.keyword);
this.travelInsertRows(ast.src.values);
}
if (ast.duplicateAssignments) {
this.appendKeyword('ON');
this.appendKeyword('DUPLICATE');
this.appendKeyword('KEY');
this.appendKeyword('UPDATE');
this.travelAssignments(ast.duplicateAssignments);
}
}
Sql.prototype.travelInsertRows = function(ast) {
for (var i = 0; i < ast.value.length; i++) {
var x = ast.value[i];
this.travel('(');
this.travelValueList(x.value);
this.travel(')');

if (i !== ast.value.length - 1) {
this.append(',', true);
}
}
}
Sql.prototype.travelValueList = function(ast) {
for (var i = 0; i < ast.length; i++) {
var x = ast[i];
this.travel(x);

if (i !== ast.length - 1) {
this.append(',', true);
}
}
}
Sql.prototype.travelUpdate = function(ast) {
this.appendKeyword('update', true);
if (ast.lowPriority) {
this.appendKeyword('low_priority');
}
if (ast.ignore) {
this.appendKeyword('ignore');
}
this.travelTableRefrences(ast.tables);
this.appendKeyword('set');
this.travelAssignments(ast.assignments);
if (ast.where) {
this.appendKeyword('where');
this.travel(ast.where);
}
if (ast.orderBy) {
this.travel(ast.orderBy);
}
if (ast.limit) {
this.travel(ast.limit);
}
}
Sql.prototype.travelAssignments = function(ast) {
for (var i = 0; i < ast.value.length; i++) {
var x = ast.value[i];
this.travel(x.left);
this.travel('=');
this.travel(x.right);

if (i !== ast.value.length - 1) {
this.append(',', true);
}
}
}
Sql.prototype.travelSelectExpr = function (ast) {
var exprList = ast.value;
for (var i = 0; i < exprList.length; i++) {
Expand DownExpand Up@@ -161,8 +261,8 @@ Sql.prototype.travelXORExpression = function (ast) {
this.appendKeyword(ast.operator);
this.travel(ast.right);
}
Sql.prototype.travelNull =
Sql.prototype.travelBoolean =
Sql.prototype.travelNull =
Sql.prototype.travelBoolean =
Sql.prototype.travelBooleanExtra = function (ast) {
this.appendKeyword(ast.value);
}
Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp