未验证 提交 2a14ec65 编写于 作者: J JingShang Lu 提交者: GitHub

Improve the definition of select (#7938)

* Improve the definition of select

* fix
上级 2c29075d
...@@ -648,3 +648,7 @@ cursorName ...@@ -648,3 +648,7 @@ cursorName
conditionName conditionName
: identifier : identifier
; ;
unionOption
: ALL | DISTINCT
;
...@@ -381,7 +381,7 @@ explainType ...@@ -381,7 +381,7 @@ explainType
; ;
explainableStatement explainableStatement
: select | tableStatement | delete | insert | replace | update : select | delete | insert | replace | update
; ;
formatName formatName
......
...@@ -105,15 +105,46 @@ singleTableClause ...@@ -105,15 +105,46 @@ singleTableClause
; ;
multipleTablesClause multipleTablesClause
: multipleTableNames FROM tableReferences | FROM multipleTableNames USING tableReferences : tableAliasRefList FROM tableReferences | FROM tableAliasRefList USING tableReferences
; ;
multipleTableNames multipleTableNames
: tableName DOT_ASTERISK_? (COMMA_ tableName DOT_ASTERISK_?)* : tableName DOT_ASTERISK_? (COMMA_ tableName DOT_ASTERISK_?)*
; ;
select select
: withClause? unionClause : queryExpression lockClauseList?
| queryExpressionParens
| selectWithInto
;
selectWithInto
: LP_ selectWithInto RP_
| queryExpression selectIntoExpression lockClauseList?
| queryExpression lockClauseList selectIntoExpression
;
queryExpression
: withClause? (queryExpressionBody | queryExpressionParens) orderByClause? limitClause?
;
queryExpressionBody
: queryPrimary
| queryExpressionParens UNION unionOption? (queryPrimary | queryExpressionParens)
;
queryExpressionParens
: LP_ (queryExpressionParens | queryExpression lockClauseList?) RP_
;
queryPrimary
: querySpecification
| tableValueConstructor
| explicitTable
;
querySpecification
: SELECT selectSpecification* projections selectIntoExpression? fromClause? whereClause? groupByClause? havingClause? windowClause?
; ;
call call
...@@ -177,12 +208,12 @@ loadXmlStatement ...@@ -177,12 +208,12 @@ loadXmlStatement
(setAssignmentsClause)? (setAssignmentsClause)?
; ;
tableStatement explicitTable
: TABLE tableName (ORDER BY columnName)? (LIMIT NUMBER_ (OFFSET NUMBER_)?)? : TABLE tableName
; ;
valuesStatement tableValueConstructor
: VALUES rowConstructorList (ORDER BY columnDesignator)? (LIMIT BY NUMBER_)? : VALUES rowConstructorList
; ;
columnDesignator columnDesignator
...@@ -201,14 +232,6 @@ cteClause ...@@ -201,14 +232,6 @@ cteClause
: ignoredIdentifier columnNames? AS subquery : ignoredIdentifier columnNames? AS subquery
; ;
unionClause
: selectClause (UNION (ALL | DISTINCT)? selectClause)*
;
selectClause
: LP_? SELECT selectSpecification* projections selectIntoExpression? fromClause? whereClause? groupByClause? havingClause? windowClause? orderByClause? limitClause? selectIntoExpression? lockClause? RP_?
;
selectSpecification selectSpecification
: duplicateSpecification | HIGH_PRIORITY | STRAIGHT_JOIN | SQL_SMALL_RESULT | SQL_BIG_RESULT | SQL_BUFFER_RESULT | (SQL_CACHE | SQL_NO_CACHE) | SQL_CALC_FOUND_ROWS : duplicateSpecification | HIGH_PRIORITY | STRAIGHT_JOIN | SQL_SMALL_RESULT | SQL_BIG_RESULT | SQL_BUFFER_RESULT | (SQL_CACHE | SQL_NO_CACHE) | SQL_CALC_FOUND_ROWS
; ;
...@@ -308,7 +331,7 @@ windowItem ...@@ -308,7 +331,7 @@ windowItem
; ;
subquery subquery
: LP_ unionClause RP_ : queryExpressionParens
; ;
selectLinesInto selectLinesInto
...@@ -325,5 +348,31 @@ selectIntoExpression ...@@ -325,5 +348,31 @@ selectIntoExpression
; ;
lockClause lockClause
: FOR UPDATE | LOCK IN SHARE MODE : FOR lockStrength lockedRowAction?
| FOR lockStrength
| LOCK IN SHARE MODE
;
lockClauseList
: lockClause+
;
lockStrength
: UPDATE | SHARE
;
lockedRowAction
: SKIP_SYMBOL LOCKED | NOWAIT
;
tableLockingList
: OF tableAliasRefList
;
tableIdentOptWild
: tableName DOT_ASTERISK_?
;
tableAliasRefList
: tableIdentOptWild+
; ;
...@@ -2259,9 +2259,9 @@ SIMPLE ...@@ -2259,9 +2259,9 @@ SIMPLE
: S I M P L E : S I M P L E
; ;
//SKIP SKIP_SYMBOL
// : S K I P : S K I P
// ; ;
SLAVE SLAVE
: S L A V E : S L A V E
......
...@@ -498,9 +498,72 @@ public abstract class MySQLStatementSQLVisitor extends MySQLStatementBaseVisitor ...@@ -498,9 +498,72 @@ public abstract class MySQLStatementSQLVisitor extends MySQLStatementBaseVisitor
@Override @Override
public ASTNode visitSubquery(final SubqueryContext ctx) { public ASTNode visitSubquery(final SubqueryContext ctx) {
return visit(ctx.unionClause()); return visit(ctx.queryExpressionParens());
} }
@Override
public ASTNode visitQueryExpressionParens(final MySQLStatementParser.QueryExpressionParensContext ctx) {
if (null != ctx.queryExpressionParens()) {
return visit(ctx.queryExpressionParens());
}
MySQLSelectStatement result = (MySQLSelectStatement) visit(ctx.queryExpression());
if (null != ctx.lockClauseList()) {
result.setLock((LockSegment) visit(ctx.lockClauseList()));
}
result.setParameterCount(getCurrentParameterIndex());
return result;
}
@Override
public ASTNode visitLockClauseList(final MySQLStatementParser.LockClauseListContext ctx) {
return new LockSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex());
}
@Override
public ASTNode visitQueryExpression(final MySQLStatementParser.QueryExpressionContext ctx) {
MySQLSelectStatement result;
if (null != ctx.queryExpressionBody()) {
result = (MySQLSelectStatement) visit(ctx.queryExpressionBody());
} else {
result = (MySQLSelectStatement) visit(ctx.queryExpressionParens());
}
if (null != ctx.orderByClause()) {
result.setOrderBy((OrderBySegment) visit(ctx.orderByClause()));
}
if (null != ctx.limitClause()) {
result.setLimit((LimitSegment) visit(ctx.limitClause()));
}
return result;
}
@Override
public ASTNode visitQueryExpressionBody(final MySQLStatementParser.QueryExpressionBodyContext ctx) {
if (1 == ctx.getChildCount() && ctx.getChild(0) instanceof MySQLStatementParser.QueryPrimaryContext) {
return visit(ctx.queryPrimary());
}
throw new IllegalStateException("union select is not supported yet.");
}
@Override
public ASTNode visitQuerySpecification(final MySQLStatementParser.QuerySpecificationContext ctx) {
MySQLSelectStatement result = new MySQLSelectStatement();
result.setProjections((ProjectionsSegment) visit(ctx.projections()));
if (null != ctx.selectSpecification()) {
result.getProjections().setDistinctRow(isDistinct(ctx));
}
if (null != ctx.fromClause()) {
TableSegment tableSource = (TableSegment) visit(ctx.fromClause().tableReferences());
result.setFrom(tableSource);
}
if (null != ctx.whereClause()) {
result.setWhere((WhereSegment) visit(ctx.whereClause()));
}
if (null != ctx.groupByClause()) {
result.setGroupBy((GroupBySegment) visit(ctx.groupByClause()));
}
return result;
}
@Override @Override
public final ASTNode visitIntervalExpression(final IntervalExpressionContext ctx) { public final ASTNode visitIntervalExpression(final IntervalExpressionContext ctx) {
calculateParameterCount(Collections.singleton(ctx.intervalValue().expr())); calculateParameterCount(Collections.singleton(ctx.intervalValue().expr()));
...@@ -950,14 +1013,14 @@ public abstract class MySQLStatementSQLVisitor extends MySQLStatementBaseVisitor ...@@ -950,14 +1013,14 @@ public abstract class MySQLStatementSQLVisitor extends MySQLStatementBaseVisitor
DeleteMultiTableSegment result = new DeleteMultiTableSegment(); DeleteMultiTableSegment result = new DeleteMultiTableSegment();
TableSegment relateTableSource = (TableSegment) visit(ctx.tableReferences()); TableSegment relateTableSource = (TableSegment) visit(ctx.tableReferences());
result.setRelationTable(relateTableSource); result.setRelationTable(relateTableSource);
result.setActualDeleteTables(generateTablesFromTableMultipleTableNames(ctx.multipleTableNames())); result.setActualDeleteTables(generateTablesFromTableMultipleTableNames(ctx.tableAliasRefList()));
return result; return result;
} }
private List<SimpleTableSegment> generateTablesFromTableMultipleTableNames(final MySQLStatementParser.MultipleTableNamesContext ctx) { private List<SimpleTableSegment> generateTablesFromTableMultipleTableNames(final MySQLStatementParser.TableAliasRefListContext ctx) {
List<SimpleTableSegment> result = new LinkedList<>(); List<SimpleTableSegment> result = new LinkedList<>();
for (TableNameContext each : ctx.tableName()) { for (MySQLStatementParser.TableIdentOptWildContext each : ctx.tableIdentOptWild()) {
result.add((SimpleTableSegment) visit(each)); result.add((SimpleTableSegment) visit(each.tableName()));
} }
return result; return result;
} }
...@@ -965,47 +1028,20 @@ public abstract class MySQLStatementSQLVisitor extends MySQLStatementBaseVisitor ...@@ -965,47 +1028,20 @@ public abstract class MySQLStatementSQLVisitor extends MySQLStatementBaseVisitor
@Override @Override
public ASTNode visitSelect(final MySQLStatementParser.SelectContext ctx) { public ASTNode visitSelect(final MySQLStatementParser.SelectContext ctx) {
// TODO :Unsupported for withClause. // TODO :Unsupported for withClause.
MySQLSelectStatement result = (MySQLSelectStatement) visit(ctx.unionClause()); MySQLSelectStatement result;
result.setParameterCount(getCurrentParameterIndex()); if (null != ctx.queryExpression()) {
return result; result = (MySQLSelectStatement) visit(ctx.queryExpression());
} if (null != ctx.lockClauseList()) {
result.setLock((LockSegment) visit(ctx.lockClauseList()));
@Override }
public ASTNode visitUnionClause(final MySQLStatementParser.UnionClauseContext ctx) { } else {
// TODO :Unsupported for union SQL. result = (MySQLSelectStatement) visit(ctx.getChild(0));
return visit(ctx.selectClause(0));
}
@Override
public ASTNode visitSelectClause(final MySQLStatementParser.SelectClauseContext ctx) {
MySQLSelectStatement result = new MySQLSelectStatement();
result.setProjections((ProjectionsSegment) visit(ctx.projections()));
if (null != ctx.selectSpecification()) {
result.getProjections().setDistinctRow(isDistinct(ctx));
}
if (null != ctx.fromClause()) {
TableSegment tableSource = (TableSegment) visit(ctx.fromClause().tableReferences());
result.setFrom(tableSource);
}
if (null != ctx.whereClause()) {
result.setWhere((WhereSegment) visit(ctx.whereClause()));
}
if (null != ctx.groupByClause()) {
result.setGroupBy((GroupBySegment) visit(ctx.groupByClause()));
}
if (null != ctx.orderByClause()) {
result.setOrderBy((OrderBySegment) visit(ctx.orderByClause()));
}
if (null != ctx.limitClause()) {
result.setLimit((LimitSegment) visit(ctx.limitClause()));
}
if (null != ctx.lockClause()) {
result.setLock((LockSegment) visit(ctx.lockClause()));
} }
result.setParameterCount(getCurrentParameterIndex());
return result; return result;
} }
private boolean isDistinct(final MySQLStatementParser.SelectClauseContext ctx) { private boolean isDistinct(final MySQLStatementParser.QuerySpecificationContext ctx) {
for (MySQLStatementParser.SelectSpecificationContext each : ctx.selectSpecification()) { for (MySQLStatementParser.SelectSpecificationContext each : ctx.selectSpecification()) {
if (((BooleanLiteralValue) visit(each)).getValue()) { if (((BooleanLiteralValue) visit(each)).getValue()) {
return true; return true;
...@@ -1258,9 +1294,4 @@ public abstract class MySQLStatementSQLVisitor extends MySQLStatementBaseVisitor ...@@ -1258,9 +1294,4 @@ public abstract class MySQLStatementSQLVisitor extends MySQLStatementBaseVisitor
} }
return new ParameterMarkerLimitValueSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), ((ParameterMarkerValue) visit(ctx.parameterMarker())).getValue()); return new ParameterMarkerLimitValueSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), ((ParameterMarkerValue) visit(ctx.parameterMarker())).getValue());
} }
@Override
public ASTNode visitLockClause(final MySQLStatementParser.LockClauseContext ctx) {
return new LockSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex());
}
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册