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
@@ -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;