mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
improved sql parser.
This commit is contained in:
@@ -186,6 +186,10 @@ public class MySqlCreateTableParser extends SQLCreateTableParser {
|
||||
}
|
||||
|
||||
for (;;) {
|
||||
if (lexer.token() == Token.COMMA) {
|
||||
lexer.nextToken();
|
||||
}
|
||||
|
||||
if (identifierEquals("ENGINE")) {
|
||||
lexer.nextToken();
|
||||
if (lexer.token() == Token.EQ) {
|
||||
|
||||
@@ -279,6 +279,12 @@ public class SQLSelectParser extends SQLParser {
|
||||
accept(Token.BY);
|
||||
|
||||
SQLSelectGroupByClause groupBy = new SQLSelectGroupByClause();
|
||||
if (identifierEquals("ROLLUP")) {
|
||||
lexer.nextToken();
|
||||
accept(Token.LPAREN);
|
||||
groupBy.setWithRollUp(true);
|
||||
}
|
||||
|
||||
for (;;) {
|
||||
SQLExpr item = parseGroupByItem();
|
||||
|
||||
@@ -291,6 +297,9 @@ public class SQLSelectParser extends SQLParser {
|
||||
|
||||
lexer.nextToken();
|
||||
}
|
||||
if (groupBy.isWithRollUp()) {
|
||||
accept(Token.RPAREN);
|
||||
}
|
||||
|
||||
if (lexer.token() == (Token.HAVING)) {
|
||||
lexer.nextToken();
|
||||
|
||||
@@ -275,10 +275,27 @@ public class SchemaRepository {
|
||||
for (SQLStatement stmt : stmtList) {
|
||||
if (stmt instanceof MySqlShowColumnsStatement) {
|
||||
MySqlShowColumnsStatement showColumns = ((MySqlShowColumnsStatement) stmt);
|
||||
SQLName table = showColumns.getTable();
|
||||
SchemaObject schemaObject = findTable(table);
|
||||
MySqlCreateTableStatement createTableStmt = (MySqlCreateTableStatement) schemaObject.getStatement();
|
||||
createTableStmt.showCoumns(buf);
|
||||
SQLName db = showColumns.getDatabase();
|
||||
Schema schema;
|
||||
if (db == null) {
|
||||
schema = getDefaultSchema();
|
||||
} else {
|
||||
schema = findSchema(db.getSimpleName());
|
||||
}
|
||||
|
||||
SQLName table = null;
|
||||
SchemaObject schemaObject = null;
|
||||
if (schema != null) {
|
||||
table = showColumns.getTable();
|
||||
schemaObject = schema.findTable(table.getSimpleName());
|
||||
}
|
||||
|
||||
if (schemaObject == null) {
|
||||
buf.append("ERROR 1146 (42S02): Table '" + table + "' doesn't exist\n");
|
||||
} else {
|
||||
MySqlCreateTableStatement createTableStmt = (MySqlCreateTableStatement) schemaObject.getStatement();
|
||||
createTableStmt.showCoumns(buf);
|
||||
}
|
||||
} else if (stmt instanceof MySqlShowCreateTableStatement) {
|
||||
MySqlShowCreateTableStatement showCreateTableStmt = (MySqlShowCreateTableStatement) stmt;
|
||||
SQLName table = showCreateTableStmt.getName();
|
||||
|
||||
@@ -1307,9 +1307,16 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
|
||||
}
|
||||
|
||||
public boolean visit(SQLSelectGroupByClause x) {
|
||||
|
||||
boolean oracle = JdbcConstants.ORACLE.equals(dbType);
|
||||
boolean rollup = x.isWithRollUp();
|
||||
|
||||
int itemSize = x.getItems().size();
|
||||
if (itemSize > 0) {
|
||||
print0(ucase ? "GROUP BY " : "group by ");
|
||||
if (oracle && rollup) {
|
||||
print0(ucase ? "ROLLUP (" : "rollup (");
|
||||
}
|
||||
incrementIndent();
|
||||
for (int i = 0; i < itemSize; ++i) {
|
||||
if (i != 0) {
|
||||
@@ -1321,6 +1328,9 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
|
||||
}
|
||||
x.getItems().get(i).accept(this);
|
||||
}
|
||||
if (oracle && rollup) {
|
||||
print(')');
|
||||
}
|
||||
decrementIndent();
|
||||
}
|
||||
|
||||
@@ -1330,7 +1340,7 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
|
||||
x.getHaving().accept(this);
|
||||
}
|
||||
|
||||
if (x.isWithRollUp()) {
|
||||
if (x.isWithRollUp() && !oracle) {
|
||||
print0(ucase ? " WITH ROLLUP" : " with rollup");
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user