improved mysql parser

This commit is contained in:
高铁
2021-04-18 16:43:13 +08:00
parent 5b5cff8dec
commit b75ee07be6
8 changed files with 76 additions and 4 deletions
@@ -141,6 +141,14 @@ public class MySqlStatementParser extends SQLStatementParser {
public MySqlDeleteStatement parseDeleteStatement() {
MySqlDeleteStatement deleteStatement = new MySqlDeleteStatement();
if (lexer.isKeepComments() && lexer.hasComment()) {
List<String> comments = lexer.readAndResetComments();
if (comments != null) {
deleteStatement.addBeforeComment(comments);
}
}
if (lexer.token() == Token.DELETE) {
lexer.nextToken();
@@ -5123,6 +5131,11 @@ public class MySqlStatementParser extends SQLStatementParser {
public SQLStatement parseAlter() {
List<String> comments = null;
if (lexer.isKeepComments() && lexer.hasComment()) {
comments = lexer.readAndResetComments();
}
Lexer.SavePoint mark = lexer.mark();
accept(Token.ALTER);
@@ -5150,7 +5163,12 @@ public class MySqlStatementParser extends SQLStatementParser {
}
if (lexer.token() == Token.TABLE) {
return parseAlterTable(ignore, online, offline);
SQLStatement alterTable = parseAlterTable(ignore, online, offline);
if (comments != null) {
alterTable.addBeforeComment(comments);
}
return alterTable;
}
if (lexer.token() == Token.DATABASE
@@ -1016,6 +1016,10 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
}
}
if (this.isPrettyFormat() && x.hasBeforeComment()) {
this.printlnComments(x.getBeforeCommentsDirect());
}
print0(ucase ? "DELETE " : "delete ");
for (int i = 0, size = x.getHintsSize(); i < size; ++i) {
@@ -1101,6 +1105,10 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
}
}
if (this.isPrettyFormat() && x.hasBeforeComment()) {
this.printlnComments(x.getBeforeCommentsDirect());
}
SQLWithSubqueryClause with = x.getWith();
if (with != null) {
visit(with);
@@ -4825,7 +4825,7 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
@Override
public boolean visit(SQLAlterViewStatement x) {
print0(ucase ? "ALTER " : "atler ");
print0(ucase ? "ALTER " : "alter ");
this.indentCount++;
String algorithm = x.getAlgorithm();
@@ -4844,7 +4844,7 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
String sqlSecurity = x.getSqlSecurity();
if (sqlSecurity != null && sqlSecurity.length() > 0) {
print0(ucase ? "SQL SECURITY = " : "sql security = ");
print0(ucase ? "SQL SECURITY " : "sql security ");
print0(sqlSecurity);
println();
}
@@ -34,7 +34,7 @@ public class MySqlAlterViewTest_0 extends TestCase {
"AS\n" +
"SELECT count(*)\n" +
"FROM t3;", SQLUtils.toMySqlString(stmt));
assertEquals("atler definer = 'ivan'@'%'\n" +
assertEquals("alter definer = 'ivan'@'%'\n" +
"\tview my_view3\n" +
"as\n" +
"select count(*)\n" +
@@ -99,6 +99,10 @@ public class MySqlResourceTest extends TestCase {
exec_test("bvt/parser/mysql-26.txt");
}
public void test_27() throws Exception {
exec_test("bvt/parser/mysql-27.txt");
}
public void exec_test(String resource) throws Exception {
// System.out.println(resource);
InputStream is = null;
@@ -0,0 +1,21 @@
CREATE TABLE product_order (
no INT NOT NULL AUTO_INCREMENT,
product_category INT NOT NULL,
product_id INT NOT NULL,
customer_id INT NOT NULL,
total_price DECIMAL(5,2),
PRIMARY KEY(no),
INDEX (product_category, product_id),
UNIQUE INDEX idx_cust_id (customer_id)
) ENGINE=INNODB ;
---------------------------
CREATE TABLE product_order (
no INT NOT NULL AUTO_INCREMENT,
product_category INT NOT NULL,
product_id INT NOT NULL,
customer_id INT NOT NULL,
total_price DECIMAL(5, 2),
PRIMARY KEY (no),
INDEX(product_category, product_id),
UNIQUE INDEX idx_cust_id (customer_id)
) ENGINE = INNODB;
@@ -0,0 +1,9 @@
select unique a.optdeptno,b.systypeno,a.optid pid
from t_char2opt a ,t_character b
where a.cserno = b.cserno
and b.t_code IN('NQAMIS','NQSZIS')
---------------------------
SELECT UNIQUE a.optdeptno, b.systypeno, a.optid AS pid
FROM t_char2opt a, t_character b
WHERE a.cserno = b.cserno
AND b.t_code IN ('NQAMIS', 'NQSZIS')
@@ -0,0 +1,12 @@
ALTER
ALGORITHM=UNDEFINED
SQL SECURITY DEFINER
View `test2`.`v1` AS
select `test_table1`.`id` AS `id`,`test_table1`.`value` AS `value` from `test_table1`
---------------------------
ALTER ALGORITHM = UNDEFINED
SQL SECURITY DEFINER
VIEW `test2`.`v1`
AS
SELECT `test_table1`.`id` AS `id`, `test_table1`.`value` AS `value`
FROM `test_table1`