mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
sql parser improved mysql 8 support, support json_table, fix #3914
This commit is contained in:
@@ -22,13 +22,13 @@ import com.alibaba.druid.sql.visitor.SQLASTVisitor;
|
||||
public abstract class MySqlObjectImpl extends SQLObjectImpl implements MySqlObject {
|
||||
|
||||
@Override
|
||||
protected void accept0(SQLASTVisitor visitor) {
|
||||
if (visitor instanceof MySqlASTVisitor) {
|
||||
accept0((MySqlASTVisitor) visitor);
|
||||
protected void accept0(SQLASTVisitor v) {
|
||||
if (v instanceof MySqlASTVisitor) {
|
||||
accept0((MySqlASTVisitor) v);
|
||||
} else {
|
||||
throw new IllegalArgumentException("not support visitor type : " + visitor.getClass().getName());
|
||||
throw new IllegalArgumentException("not support visitor type : " + v.getClass().getName());
|
||||
}
|
||||
}
|
||||
|
||||
public abstract void accept0(MySqlASTVisitor visitor);
|
||||
public abstract void accept0(MySqlASTVisitor v);
|
||||
}
|
||||
|
||||
@@ -24,10 +24,7 @@ import com.alibaba.druid.sql.ast.statement.SQLForeignKeyImpl.Option;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.MySqlPrimaryKey;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.MySqlUnique;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.MysqlForeignKey;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlCharExpr;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlOrderingExpr;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlOutFileExpr;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlUserName;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.expr.*;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateTableStatement;
|
||||
import com.alibaba.druid.sql.parser.*;
|
||||
import com.alibaba.druid.util.FnvHash;
|
||||
@@ -208,6 +205,34 @@ public class MySqlExprParser extends SQLExprParser {
|
||||
currentTimeExpr = new SQLCurrentTimeExpr(SQLCurrentTimeExpr.Type.LOCALTIME);
|
||||
} else if(hash_lower == FnvHash.Constants.LOCALTIMESTAMP && !quoteStart) {
|
||||
currentTimeExpr = new SQLCurrentTimeExpr(SQLCurrentTimeExpr.Type.LOCALTIMESTAMP);
|
||||
} else if (hash_lower == FnvHash.Constants.JSON_TABLE) {
|
||||
if (lexer.identifierEquals("JSON_TABLE")) {
|
||||
lexer.nextToken();
|
||||
accept(Token.LPAREN);
|
||||
|
||||
MySqlJSONTableExpr jsonTable = new MySqlJSONTableExpr();
|
||||
jsonTable.setExpr(
|
||||
this.expr());
|
||||
accept(Token.COMMA);
|
||||
jsonTable.setPath(
|
||||
this.expr());
|
||||
acceptIdentifier("COLUMNS");
|
||||
accept(Token.LPAREN);
|
||||
for (;lexer.token() != Token.RPAREN;) {
|
||||
jsonTable.addColumn(
|
||||
parseJsonTableColumn());
|
||||
|
||||
if (lexer.token() == Token.COMMA) {
|
||||
lexer.nextToken();
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
}
|
||||
accept(Token.RPAREN);
|
||||
|
||||
accept(Token.RPAREN);
|
||||
return jsonTable;
|
||||
}
|
||||
} else if((hash_lower == FnvHash.Constants._LATIN1) && !quoteStart) {
|
||||
lexer.nextToken();
|
||||
|
||||
@@ -505,6 +530,77 @@ public class MySqlExprParser extends SQLExprParser {
|
||||
|
||||
}
|
||||
|
||||
protected MySqlJSONTableExpr.Column parseJsonTableColumn() {
|
||||
MySqlJSONTableExpr.Column column = new MySqlJSONTableExpr.Column();
|
||||
|
||||
SQLName name = this.name();
|
||||
column.setName(
|
||||
name);
|
||||
|
||||
if (lexer.token() == Token.FOR) {
|
||||
lexer.nextToken();
|
||||
acceptIdentifier("ORDINALITY");
|
||||
} else {
|
||||
boolean nested = name instanceof SQLIdentifierExpr
|
||||
&& name.nameHashCode64() == FnvHash.Constants.NESTED;
|
||||
|
||||
if (!nested) {
|
||||
column.setDataType(
|
||||
this.parseDataType());
|
||||
}
|
||||
|
||||
if (lexer.token() == Token.EXISTS) {
|
||||
lexer.nextToken();
|
||||
column.setExists(true);
|
||||
}
|
||||
|
||||
if (lexer.identifierEquals(FnvHash.Constants.PATH)) {
|
||||
lexer.nextToken();
|
||||
column.setPath(
|
||||
this.primary());
|
||||
}
|
||||
|
||||
if (name instanceof SQLIdentifierExpr
|
||||
&& name.nameHashCode64() == FnvHash.Constants.NESTED) {
|
||||
acceptIdentifier("COLUMNS");
|
||||
accept(Token.LPAREN);
|
||||
for (;lexer.token() != Token.RPAREN;) {
|
||||
MySqlJSONTableExpr.Column nestedColumn = parseJsonTableColumn();
|
||||
column.addNestedColumn(nestedColumn);
|
||||
|
||||
if (lexer.token() == Token.COMMA) {
|
||||
lexer.nextToken();
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
}
|
||||
accept(Token.RPAREN);
|
||||
}
|
||||
|
||||
for (int i = 0; i < 2; ++i) {
|
||||
if (lexer.identifierEquals("ERROR")
|
||||
|| lexer.token() == Token.DEFAULT
|
||||
|| lexer.token() == Token.NULL) {
|
||||
if (lexer.token() == Token.DEFAULT) {
|
||||
lexer.nextToken();
|
||||
}
|
||||
|
||||
SQLExpr expr = this.expr();
|
||||
accept(Token.ON);
|
||||
if (lexer.identifierEquals("ERROR")) {
|
||||
lexer.nextToken();
|
||||
column.setOnError(expr);
|
||||
} else {
|
||||
acceptIdentifier("EMPTY");
|
||||
column.setOnEmpty(expr);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return column;
|
||||
}
|
||||
|
||||
public final SQLExpr primaryRest(SQLExpr expr) {
|
||||
if (expr == null) {
|
||||
throw new IllegalArgumentException("expr");
|
||||
|
||||
@@ -22,10 +22,7 @@ import com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.*;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.clause.*;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.clause.MySqlCaseStatement.MySqlWhenStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlCharExpr;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlOrderingExpr;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlOutFileExpr;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlUserName;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.expr.*;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.*;
|
||||
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
|
||||
|
||||
@@ -1430,5 +1427,20 @@ public interface MySqlASTVisitor extends SQLASTVisitor {
|
||||
|
||||
}
|
||||
|
||||
default boolean visit(MySqlJSONTableExpr x) {
|
||||
return true;
|
||||
}
|
||||
|
||||
default void endVisit(MySqlJSONTableExpr x) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
default boolean visit(MySqlJSONTableExpr.Column x) {
|
||||
return true;
|
||||
}
|
||||
|
||||
default void endVisit(MySqlJSONTableExpr.Column x) {
|
||||
|
||||
}
|
||||
} //
|
||||
|
||||
@@ -23,10 +23,7 @@ import com.alibaba.druid.sql.dialect.mysql.ast.*;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.clause.*;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.clause.ConditionValue.ConditionType;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.clause.MySqlCaseStatement.MySqlWhenStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlCharExpr;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlOrderingExpr;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlOutFileExpr;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlUserName;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.expr.*;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.*;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateTableStatement.TableSpaceOption;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateUserStatement.UserSpecification;
|
||||
@@ -5394,6 +5391,79 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
|
||||
return hiveVisit(x);
|
||||
}
|
||||
|
||||
public boolean visit(MySqlJSONTableExpr x) {
|
||||
print0(ucase ? "JSON_TABLE(" : "json_table(");
|
||||
x.getExpr().accept(this);
|
||||
print(' ');
|
||||
x.getPath().accept(this);
|
||||
incrementIndent();
|
||||
println();
|
||||
print0(ucase ? "COLUMNS (" : "columns (");
|
||||
incrementIndent();
|
||||
println();
|
||||
printlnAndAccept(x.getColumns(), ", ");
|
||||
decrementIndent();
|
||||
println();
|
||||
print(')');
|
||||
decrementIndent();
|
||||
println();
|
||||
print(')');
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean visit(MySqlJSONTableExpr.Column x) {
|
||||
x.getName().accept(this);
|
||||
|
||||
if (x.isOrdinality()) {
|
||||
print0(ucase ? " FOR ORDINALITY" : " for ordinality");
|
||||
}
|
||||
|
||||
SQLDataType dataType = x.getDataType();
|
||||
if (dataType != null) {
|
||||
print(' ');
|
||||
dataType.accept(this);
|
||||
}
|
||||
|
||||
if (x.isExists()) {
|
||||
print0(ucase ? " EXISTS" : " exists");
|
||||
}
|
||||
|
||||
SQLExpr path = x.getPath();
|
||||
if (path != null) {
|
||||
print0(ucase ? " PATH " : " path ");
|
||||
path.accept(this);
|
||||
}
|
||||
|
||||
List<MySqlJSONTableExpr.Column> nestedColumns = x.getNestedColumns();
|
||||
if (nestedColumns.size() > 0) {
|
||||
print0(ucase ? " COLUMNS (" : " columns (");
|
||||
printAndAccept(nestedColumns, ", ");
|
||||
print(')');
|
||||
}
|
||||
|
||||
SQLExpr onEmpty = x.getOnEmpty();
|
||||
if (onEmpty != null) {
|
||||
print(' ');
|
||||
if (!(onEmpty instanceof SQLNullExpr || onEmpty instanceof SQLIdentifierExpr)) {
|
||||
print0(ucase ? "DEFAULT " : "default ");
|
||||
}
|
||||
onEmpty.accept(this);
|
||||
print0(ucase ? " ON EMPTY" : " on empty");
|
||||
}
|
||||
|
||||
SQLExpr onError = x.getOnError();
|
||||
if (onError != null) {
|
||||
print(' ');
|
||||
if (!(onEmpty instanceof SQLNullExpr || onEmpty instanceof SQLIdentifierExpr)) {
|
||||
print0(ucase ? "DEFAULT " : "default ");
|
||||
}
|
||||
onError.accept(this);
|
||||
print0(ucase ? " ON ERROR" : " on error");
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// public boolean visit(SQLSelectItem x) {
|
||||
// SQLExpr expr = x.getExpr();
|
||||
//
|
||||
|
||||
Reference in New Issue
Block a user