imporoved sql parser.

This commit is contained in:
wenshao
2017-10-15 15:52:41 +08:00
parent cf228c5b34
commit 7fe9f468ac
44 changed files with 381 additions and 36 deletions
@@ -19,6 +19,7 @@ import com.alibaba.druid.sql.ast.SQLExpr;
import com.alibaba.druid.sql.ast.SQLName;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.ast.statement.SQLAlterStatement;
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitor;
import com.alibaba.druid.util.JdbcConstants;
public class MySqlAlterEventStatement extends MySqlStatementImpl implements SQLAlterStatement {
@@ -37,6 +38,18 @@ public class MySqlAlterEventStatement extends MySqlStatementImpl implements SQLA
setDbType(JdbcConstants.MYSQL);
}
public void accept0(MySqlASTVisitor visitor) {
if (visitor.visit(this)) {
acceptChild(visitor, definer);
acceptChild(visitor, name);
acceptChild(visitor, schedule);
acceptChild(visitor, renameTo);
acceptChild(visitor, comment);
acceptChild(visitor, eventBody);
}
visitor.endVisit(this);
}
public SQLName getDefiner() {
return definer;
}
@@ -18,6 +18,7 @@ package com.alibaba.druid.sql.dialect.mysql.ast.statement;
import com.alibaba.druid.sql.ast.SQLExpr;
import com.alibaba.druid.sql.ast.SQLName;
import com.alibaba.druid.sql.ast.statement.SQLAlterStatement;
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitor;
public class MySqlAlterLogFileGroupStatement extends MySqlStatementImpl implements SQLAlterStatement {
private SQLName name;
@@ -26,6 +27,16 @@ public class MySqlAlterLogFileGroupStatement extends MySqlStatementImpl implemen
private boolean wait;
private SQLExpr engine;
public void accept0(MySqlASTVisitor visitor) {
if (visitor.visit(this)) {
acceptChild(visitor, name);
acceptChild(visitor, addUndoFile);
acceptChild(visitor, initialSize);
acceptChild(visitor, engine);
}
visitor.endVisit(this);
}
public SQLName getName() {
return name;
}
@@ -18,6 +18,7 @@ package com.alibaba.druid.sql.dialect.mysql.ast.statement;
import com.alibaba.druid.sql.ast.SQLExpr;
import com.alibaba.druid.sql.ast.SQLName;
import com.alibaba.druid.sql.ast.statement.SQLAlterStatement;
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitor;
public class MySqlAlterServerStatement extends MySqlStatementImpl implements SQLAlterStatement {
private SQLName name;
@@ -46,4 +47,12 @@ public class MySqlAlterServerStatement extends MySqlStatementImpl implements SQL
}
this.user = user;
}
public void accept0(MySqlASTVisitor visitor) {
if (visitor.visit(this)) {
acceptChild(visitor, name);
acceptChild(visitor, user);
}
visitor.endVisit(this);
}
}
@@ -18,6 +18,7 @@ package com.alibaba.druid.sql.dialect.mysql.ast.statement;
import com.alibaba.druid.sql.ast.SQLExpr;
import com.alibaba.druid.sql.ast.SQLName;
import com.alibaba.druid.sql.ast.statement.SQLAlterStatement;
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitor;
public class MySqlAlterTablespaceStatement extends MySqlStatementImpl implements SQLAlterStatement {
private SQLName name;
@@ -28,6 +29,16 @@ public class MySqlAlterTablespaceStatement extends MySqlStatementImpl implements
private boolean wait;
private SQLExpr engine;
public void accept0(MySqlASTVisitor visitor) {
if (visitor.visit(this)) {
acceptChild(visitor, name);
acceptChild(visitor, addDataFile);
acceptChild(visitor, initialSize);
acceptChild(visitor, engine);
}
visitor.endVisit(this);
}
public SQLName getName() {
return name;
}
@@ -479,4 +479,16 @@ public interface MySqlASTVisitor extends SQLASTVisitor {
boolean visit(MySqlCreateTableSpaceStatement x);
void endVisit(MySqlCreateTableSpaceStatement x);
boolean visit(MySqlAlterEventStatement x);
void endVisit(MySqlAlterEventStatement x);
boolean visit(MySqlAlterLogFileGroupStatement x);
void endVisit(MySqlAlterLogFileGroupStatement x);
boolean visit(MySqlAlterServerStatement x);
void endVisit(MySqlAlterServerStatement x);
boolean visit(MySqlAlterTablespaceStatement x);
void endVisit(MySqlAlterTablespaceStatement x);
} //
@@ -1170,7 +1170,7 @@ public class MySqlASTVisitorAdapter extends SQLASTVisitorAdapter implements MySq
@Override
public boolean visit(MySqlCreateTableSpaceStatement x) {
return false;
return true;
}
@Override
@@ -1178,4 +1178,44 @@ public class MySqlASTVisitorAdapter extends SQLASTVisitorAdapter implements MySq
}
@Override
public boolean visit(MySqlAlterEventStatement x) {
return true;
}
@Override
public void endVisit(MySqlAlterEventStatement x) {
}
@Override
public boolean visit(MySqlAlterLogFileGroupStatement x) {
return true;
}
@Override
public void endVisit(MySqlAlterLogFileGroupStatement x) {
}
@Override
public boolean visit(MySqlAlterServerStatement x) {
return true;
}
@Override
public void endVisit(MySqlAlterServerStatement x) {
}
@Override
public boolean visit(MySqlAlterTablespaceStatement x) {
return true;
}
@Override
public void endVisit(MySqlAlterTablespaceStatement x) {
}
} //
@@ -3918,6 +3918,149 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
}
@Override
public boolean visit(MySqlAlterEventStatement x) {
print0(ucase ? "ALTER " : "alter ");
SQLName definer = x.getDefiner();
if (definer != null) {
print0(ucase ? "DEFINER = " : "definer = ");
}
print0(ucase ? "EVENT " : "evnet ");
printExpr(x.getName());
MySqlEventSchedule schedule = x.getSchedule();
if (schedule != null) {
print0(ucase ? " ON SCHEDULE " : " on schedule ");
schedule.accept(this);
}
Boolean enable = x.getEnable();
if (enable != null) {
if (enable) {
print0(ucase ? " ENABLE" : " enable");
} else {
print0(ucase ? " DISABLE" : " disable");
if (x.isDisableOnSlave()) {
print0(ucase ? " ON SLAVE" : " on slave");
}
}
}
SQLExpr comment = x.getComment();
if (comment != null) {
print0(ucase ? "COMMENT " : "comment ");
comment.accept(this);
}
SQLStatement body = x.getEventBody();
if (body != null) {
println();
if (!(body instanceof SQLExprStatement)) {
print0(ucase ? "DO" : "do");
println();
}
body.accept(this);
}
return false;
}
@Override
public void endVisit(MySqlAlterEventStatement x) {
}
@Override
public boolean visit(MySqlAlterLogFileGroupStatement x) {
print0(ucase ? "ALTER LOGFILE GROUP " : "alter logfile group ");
x.getName().accept(this);
print(' ');
print0(ucase ? "ADD UNDOFILE " : "add undofile ");
printExpr(x.getAddUndoFile());
SQLExpr initialSize = x.getInitialSize();
if (initialSize != null) {
print0(ucase ? " INITIAL_SIZE " : " initial_size ");
printExpr(initialSize);
}
if (x.isWait()) {
print0(ucase ? " WAIT" : " wait");
}
SQLExpr engine = x.getEngine();
if (engine != null) {
print0(ucase ? " ENGINE " : " engine ");
printExpr(engine);
}
return false;
}
@Override
public void endVisit(MySqlAlterLogFileGroupStatement x) {
}
@Override
public boolean visit(MySqlAlterServerStatement x) {
print0(ucase ? "ATLER SERVER " : "alter server ");
x.getName().accept(this);
print(" OPTIONS(");
SQLExpr user = x.getUser();
if (user != null) {
print0(ucase ? "USER " : "user ");
printExpr(user);
}
print(')');
return false;
}
@Override
public void endVisit(MySqlAlterServerStatement x) {
}
@Override
public boolean visit(MySqlAlterTablespaceStatement x) {
print0(ucase ? "CREATE TABLESPACE " : "create tablespace ");
x.getName().accept(this);
SQLExpr addDataFile = x.getAddDataFile();
if (addDataFile != null) {
print0(ucase ? " ADD DATAFILE " : " add datafile ");
addDataFile.accept(this);
}
SQLExpr initialSize = x.getInitialSize();
if (initialSize != null) {
print0(ucase ? " INITIAL_SIZE = " : " initial_size = ");
initialSize.accept(this);
}
if (x.isWait()) {
print0(ucase ? " WAIT" : " wait");
}
SQLExpr engine = x.getEngine();
if (engine != null) {
print0(ucase ? " ENGINE " : " engine ");
printExpr(engine);
}
return false;
}
@Override
public void endVisit(MySqlAlterTablespaceStatement x) {
}
protected void printQuery(SQLSelectQuery x) {
Class<?> clazz = x.getClass();
if (clazz == MySqlSelectQueryBlock.class) {
@@ -1304,4 +1304,44 @@ public class MySqlSchemaStatVisitor extends SchemaStatVisitor implements MySqlAS
public void endVisit(MySqlCreateTableSpaceStatement x) {
}
@Override
public boolean visit(MySqlAlterEventStatement x) {
return false;
}
@Override
public void endVisit(MySqlAlterEventStatement x) {
}
@Override
public boolean visit(MySqlAlterLogFileGroupStatement x) {
return false;
}
@Override
public void endVisit(MySqlAlterLogFileGroupStatement x) {
}
@Override
public boolean visit(MySqlAlterServerStatement x) {
return false;
}
@Override
public void endVisit(MySqlAlterServerStatement x) {
}
@Override
public boolean visit(MySqlAlterTablespaceStatement x) {
return false;
}
@Override
public void endVisit(MySqlAlterTablespaceStatement x) {
}
}
@@ -1,22 +1,37 @@
package com.alibaba.druid.bvt.sql.mysql;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.visitor.SchemaStatVisitor;
import com.alibaba.druid.util.JdbcConstants;
import com.alibaba.druid.wall.WallProvider;
import com.alibaba.druid.wall.WallUtils;
import com.alibaba.druid.wall.spi.MySqlWallProvider;
import junit.framework.TestCase;
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.net.URL;
import java.util.List;
public class AntlrMySqlTest extends TestCase {
public void test_for_antlr_examples() throws Exception {
SchemaStatVisitor schemaStatVisitor = SQLUtils.createSchemaStatVisitor(JdbcConstants.MYSQL);
MySqlWallProvider provider = new MySqlWallProvider();
String path = "bvt/parser/antlr_grammers_v4_mysql/examples/";
URL resource = Thread.currentThread().getContextClassLoader().getResource(path);
File dir = new File(resource.getFile());
for (File file : dir.listFiles()) {
System.out.println(file);
String sql = FileUtils.readFileToString(file);
SQLUtils.parseStatements(sql, JdbcConstants.MYSQL);
List<SQLStatement> stmtList = SQLUtils.parseStatements(sql, JdbcConstants.MYSQL);
for (SQLStatement stmt : stmtList) {
stmt.toString();
stmt.accept(schemaStatVisitor);
provider.checkValid(sql);
}
}
}
}
@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.bvt.sql.sqlserver;
package com.alibaba.druid.bvt.sql.sqlserver.select;
import org.junit.Assert;
import junit.framework.TestCase;
@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.bvt.sql.sqlserver;
package com.alibaba.druid.bvt.sql.sqlserver.select;
import junit.framework.TestCase;
@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.bvt.sql.sqlserver;
package com.alibaba.druid.bvt.sql.sqlserver.select;
import junit.framework.TestCase;
@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.bvt.sql.sqlserver;
package com.alibaba.druid.bvt.sql.sqlserver.select;
import junit.framework.TestCase;
@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.bvt.sql.sqlserver;
package com.alibaba.druid.bvt.sql.sqlserver.select;
import junit.framework.TestCase;
@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.bvt.sql.sqlserver;
package com.alibaba.druid.bvt.sql.sqlserver.select;
import junit.framework.TestCase;
@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.bvt.sql.sqlserver;
package com.alibaba.druid.bvt.sql.sqlserver.select;
import junit.framework.TestCase;
@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.bvt.sql.sqlserver;
package com.alibaba.druid.bvt.sql.sqlserver.select;
import junit.framework.TestCase;
@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.bvt.sql.sqlserver;
package com.alibaba.druid.bvt.sql.sqlserver.select;
import junit.framework.TestCase;
@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.bvt.sql.sqlserver;
package com.alibaba.druid.bvt.sql.sqlserver.select;
import junit.framework.TestCase;
@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.bvt.sql.sqlserver;
package com.alibaba.druid.bvt.sql.sqlserver.select;
import junit.framework.TestCase;
@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.bvt.sql.sqlserver;
package com.alibaba.druid.bvt.sql.sqlserver.select;
import junit.framework.TestCase;
@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.bvt.sql.sqlserver;
package com.alibaba.druid.bvt.sql.sqlserver.select;
import org.junit.Assert;
import junit.framework.TestCase;
@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.bvt.sql.sqlserver;
package com.alibaba.druid.bvt.sql.sqlserver.select;
import junit.framework.TestCase;
@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.bvt.sql.sqlserver;
package com.alibaba.druid.bvt.sql.sqlserver.select;
import org.junit.Assert;
@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.bvt.sql.sqlserver;
package com.alibaba.druid.bvt.sql.sqlserver.select;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLStatement;
@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.bvt.sql.sqlserver;
package com.alibaba.druid.bvt.sql.sqlserver.select;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLStatement;
@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.bvt.sql.sqlserver;
package com.alibaba.druid.bvt.sql.sqlserver.select;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLStatement;
@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.bvt.sql.sqlserver;
package com.alibaba.druid.bvt.sql.sqlserver.select;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLStatement;
@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.bvt.sql.sqlserver;
package com.alibaba.druid.bvt.sql.sqlserver.select;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLStatement;
@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.bvt.sql.sqlserver;
package com.alibaba.druid.bvt.sql.sqlserver.select;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLStatement;
@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.bvt.sql.sqlserver;
package com.alibaba.druid.bvt.sql.sqlserver.select;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLStatement;
@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.bvt.sql.sqlserver;
package com.alibaba.druid.bvt.sql.sqlserver.select;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLStatement;
@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.bvt.sql.sqlserver;
package com.alibaba.druid.bvt.sql.sqlserver.select;
import org.junit.Assert;
import junit.framework.TestCase;
@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.bvt.sql.sqlserver;
package com.alibaba.druid.bvt.sql.sqlserver.select;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLStatement;
@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.bvt.sql.sqlserver;
package com.alibaba.druid.bvt.sql.sqlserver.select;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLStatement;
@@ -0,0 +1,51 @@
/*
* Copyright 1999-2017 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.bvt.sql.sqlserver.select;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.util.JdbcConstants;
import junit.framework.TestCase;
import java.util.List;
public class SQLServerSelectTest32_with_hint extends TestCase {
public void test_simple() throws Exception {
String sql = //
"SELECT * FROM dbo.Customers AS c \n" +
"WITH (SNAPSHOT) \n" +
"LEFT JOIN dbo.[Order History] AS oh \n" +
" ON c.customer_id=oh.customer_id;"; //
List<SQLStatement> stmtList = SQLUtils.parseStatements(sql, JdbcConstants.SQL_SERVER);
{
String text = SQLUtils.toSQLString(stmtList, JdbcConstants.SQL_SERVER);
assertEquals("SELECT *\n" +
"FROM dbo.Customers c WITH (SNAPSHOT)\n" +
"\tLEFT JOIN dbo.[Order History] oh ON c.customer_id = oh.customer_id;", text);
}
{
String text = SQLUtils.toSQLString(stmtList, JdbcConstants.SQL_SERVER, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION);
assertEquals("select *\n" +
"from dbo.Customers c with (SNAPSHOT)\n" +
"\tleft join dbo.[Order History] oh on c.customer_id = oh.customer_id;", text);
}
}
}
@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.bvt.sql.sqlserver;
package com.alibaba.druid.bvt.sql.sqlserver.select;
import org.junit.Assert;
import junit.framework.TestCase;
@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.bvt.sql.sqlserver;
package com.alibaba.druid.bvt.sql.sqlserver.select;
import org.junit.Assert;
import junit.framework.TestCase;
@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.bvt.sql.sqlserver;
package com.alibaba.druid.bvt.sql.sqlserver.select;
import junit.framework.TestCase;
@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.bvt.sql.sqlserver;
package com.alibaba.druid.bvt.sql.sqlserver.select;
import junit.framework.TestCase;
@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.bvt.sql.sqlserver;
package com.alibaba.druid.bvt.sql.sqlserver.select;
import junit.framework.TestCase;
@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.bvt.sql.sqlserver;
package com.alibaba.druid.bvt.sql.sqlserver.select;
import junit.framework.TestCase;
@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.bvt.sql.sqlserver;
package com.alibaba.druid.bvt.sql.sqlserver.select;
import java.util.List;
@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.bvt.sql.sqlserver;
package com.alibaba.druid.bvt.sql.sqlserver.select;
import java.util.List;