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:
@@ -0,0 +1,67 @@
|
||||
package com.alibaba.druid.bvt.bug;
|
||||
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLCreateViewStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor;
|
||||
import com.alibaba.druid.stat.TableStat;
|
||||
import com.alibaba.druid.util.JdbcConstants;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class Issue1935 extends TestCase {
|
||||
public void test_for_issue() throws Exception {
|
||||
String DBTYPE = JdbcConstants.MYSQL;
|
||||
//String sql = "select name, course ,scole from student inner join scole on student.id = scole.sd_id where course = '数学' limit 10;";
|
||||
//sql = "select name,course,sum(scole) as total from student where student.id in (select sd_id from scole where name='aaa') and scole in (1,2,3) group by name HAVING total <60 order by scole desc limit 10 ,2 ";
|
||||
String sql = "select name from student where id in (select sd_id from scole where scole < 60 order by scole asc) or id = 2 order by name desc";
|
||||
String format = SQLUtils.format(sql, DBTYPE);
|
||||
//System.out.println("formated sql : " + format);
|
||||
List<SQLStatement> list = SQLUtils.parseStatements(sql, DBTYPE);
|
||||
|
||||
for (int i = 0; i < list.size(); i++) {
|
||||
SQLStatement stmt = list.get(i);
|
||||
MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
|
||||
stmt.accept(visitor);
|
||||
|
||||
|
||||
//获取操作方法名称,依赖于表名称
|
||||
System.out.println("涉及到的所有表 : " + visitor.getTables());
|
||||
Map<TableStat.Name, TableStat> table_map = visitor.getTables();
|
||||
for(Map.Entry<TableStat.Name, TableStat> entry : table_map.entrySet()){
|
||||
TableStat.Name name = entry.getKey();
|
||||
name.getName();
|
||||
//存储表的调度次数,包括select ,update等
|
||||
TableStat ts = entry.getValue();
|
||||
|
||||
}
|
||||
//获取字段名称
|
||||
System.out.println( visitor.getParameters());
|
||||
//获取列名
|
||||
System.out.println("查询的列信息 : " + visitor.getColumns());
|
||||
Collection<TableStat.Column> cc = visitor.getColumns();
|
||||
//column 存储了表名,列名,以及列是出现的位置,where,select,groupby ,order
|
||||
for(TableStat.Column column : cc){
|
||||
|
||||
}
|
||||
System.out.println("conditions : " + visitor.getConditions() );
|
||||
List<TableStat.Condition> conditions = visitor.getConditions();
|
||||
System.out.println("----------------------------");
|
||||
for(TableStat.Condition cond : conditions){
|
||||
System.out.println( "column : " + cond.getColumn());
|
||||
System.out.println( "operator : " + cond.getOperator());
|
||||
System.out.println( "values : " + cond.getValues());
|
||||
|
||||
System.out.println("----------------------------");
|
||||
}
|
||||
System.out.println("group by : " + visitor.getGroupByColumns() );
|
||||
System.out.println("order by : " + visitor.getOrderByColumns() );
|
||||
System.out.println("relations ships : " + visitor.getRelationships() );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -10,13 +10,9 @@ public class SQLSortTest_2 extends TestCase {
|
||||
" FOR hr.locations;create table hr.locations(fid varchar(200));";
|
||||
|
||||
String sortedSql = SQLUtils.sort(sql, JdbcConstants.ORACLE);
|
||||
assertEquals("CREATE VIEW v0\n" +
|
||||
"AS\n" +
|
||||
"SELECT *\n" +
|
||||
"FROM t;\n" +
|
||||
"CREATE VIEW v1\n" +
|
||||
"AS\n" +
|
||||
"SELECT *\n" +
|
||||
"FROM v0;", sortedSql);
|
||||
assertEquals("CREATE TABLE hr.locations (\n" +
|
||||
"\tfid varchar(200)\n" +
|
||||
");\n" +
|
||||
"CREATE SYNONYM offices FOR hr.locations;", sortedSql);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,80 +0,0 @@
|
||||
/*
|
||||
* 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.oracle.select;
|
||||
|
||||
import com.alibaba.druid.sql.OracleTest;
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelectStatement;
|
||||
import com.alibaba.druid.sql.dialect.oracle.parser.OracleStatementParser;
|
||||
import com.alibaba.druid.sql.dialect.oracle.visitor.OracleSchemaStatVisitor;
|
||||
import org.junit.Assert;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class OracleSelectTest100 extends OracleTest {
|
||||
|
||||
public void test_0() throws Exception {
|
||||
String sql = //
|
||||
"SELECT EXTRACT(YEAR FROM SYSTIMESTAMP) AS \"YEAR\" FROM DUAL;\n" +
|
||||
"SELECT EXTRACT(MONTH FROM SYSTIMESTAMP) \"MONTH\" FROM DUAL;" +
|
||||
"SELECT EXTRACT(DAY FROM SYSTIMESTAMP) AS \"DAY\" FROM DUAL;" +
|
||||
"SELECT EXTRACT(HOUR FROM SYSTIMESTAMP) AS \"HOUR\" FROM DUAL;" +
|
||||
"SELECT EXTRACT(MINUTE FROM SYSTIMESTAMP) AS \"MINUTE\" FROM DUAL;" +
|
||||
"SELECT EXTRACT(SECOND FROM SYSTIMESTAMP) AS \"SECOND\" FROM DUAL;" +
|
||||
"SELECT EXTRACT(TIMEZONE_HOUR FROM SYSTIMESTAMP) AS \"TIMEZONE_HOUR\" FROM DUAL;" +
|
||||
"SELECT EXTRACT(TIMEZONE_MINUTE FROM SYSTIMESTAMP) AS \"TIMEZONE_MINUTE\" FROM DUAL;" +
|
||||
"SELECT EXTRACT(TIMEZONE_REGION FROM SYSTIMESTAMP) AS \"TIMEZONE_REGION\" FROM DUAL;" +
|
||||
"SELECT EXTRACT(TIMEZONE_ABBR FROM SYSTIMESTAMP) AS \"TIMEZONE_ABBR\" FROM DUAL;";
|
||||
System.out.println(sql);
|
||||
|
||||
OracleStatementParser parser = new OracleStatementParser(sql);
|
||||
List<SQLStatement> statementList = parser.parseStatementList();
|
||||
SQLSelectStatement stmt = (SQLSelectStatement) statementList.get(0);
|
||||
System.out.println(stmt.toString());
|
||||
|
||||
Assert.assertEquals(10, statementList.size());
|
||||
|
||||
// SQLMethodInvokeExpr expr = (SQLMethodInvokeExpr) stmt.getSelect().getQueryBlock().getSelectList().get(0).getExpr();
|
||||
// SQLMethodInvokeExpr param0 = (SQLMethodInvokeExpr) expr.getParameters().get(0);
|
||||
// assertTrue(param0.getParameters().get(0)
|
||||
// instanceof SQLAggregateExpr);
|
||||
|
||||
OracleSchemaStatVisitor visitor = new OracleSchemaStatVisitor();
|
||||
stmt.accept(visitor);
|
||||
|
||||
{
|
||||
String text = SQLUtils.toOracleString(stmt);
|
||||
|
||||
assertEquals("SELECT last_name, hire_date, salary, SUM(salary) OVER (ORDER BY hire_date RANGE NUMTOYMINTERVAL(1, 'year') PRECEDING) AS t_sal\n" +
|
||||
"FROM employees;", text);
|
||||
}
|
||||
|
||||
System.out.println("Tables : " + visitor.getTables());
|
||||
System.out.println("fields : " + visitor.getColumns());
|
||||
System.out.println("coditions : " + visitor.getConditions());
|
||||
System.out.println("relationships : " + visitor.getRelationships());
|
||||
System.out.println("orderBy : " + visitor.getOrderByColumns());
|
||||
|
||||
assertEquals(1, visitor.getTables().size());
|
||||
assertEquals(3, visitor.getColumns().size());
|
||||
assertEquals(0, visitor.getConditions().size());
|
||||
assertEquals(0, visitor.getRelationships().size());
|
||||
assertEquals(1, visitor.getOrderByColumns().size());
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,71 +0,0 @@
|
||||
/*
|
||||
* 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.postgresql;
|
||||
|
||||
import com.alibaba.druid.sql.PGTest;
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.parser.PGSQLStatementParser;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.visitor.PGSchemaStatVisitor;
|
||||
import com.alibaba.druid.stat.TableStat;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class PGCreateTableTest_12 extends PGTest {
|
||||
|
||||
public void test_0() throws Exception {
|
||||
String sql = "CREATE GLOBAL TEMPORARY TABLE ZEUS.\"RUPD$_ALI_ZEUS_CSREASON\" (\n" +
|
||||
"\tID DECIMAL(38),\n" +
|
||||
"\t\"DMLTYPE$$\" VARCHAR(1),\n" +
|
||||
"\tSNAPID BIGINT,\n" +
|
||||
"\t\"CHANGE_VECTOR$$\" BYTEA\n" +
|
||||
")\n" +
|
||||
"ON COMMIT PRESERVE ROWS";
|
||||
|
||||
PGSQLStatementParser parser = new PGSQLStatementParser(sql);
|
||||
List<SQLStatement> statementList = parser.parseStatementList();
|
||||
SQLStatement stmt = statementList.get(0);
|
||||
|
||||
assertEquals("CREATE TABLE KTV.KTV_FUNCTION_ROLE_20151211 (\n" +
|
||||
"\tURL VARCHAR(150),\n" +
|
||||
"\tFUNCTION VARCHAR(1002),\n" +
|
||||
"\tROLE VARCHAR(100),\n" +
|
||||
"\t\"COUNT\" VARCHAR(102)\n" +
|
||||
")", SQLUtils.toPGString(stmt));
|
||||
|
||||
assertEquals("create table KTV.KTV_FUNCTION_ROLE_20151211 (\n" +
|
||||
"\tURL VARCHAR(150),\n" +
|
||||
"\tFUNCTION VARCHAR(1002),\n" +
|
||||
"\tROLE VARCHAR(100),\n" +
|
||||
"\t\"COUNT\" VARCHAR(102)\n" +
|
||||
")", SQLUtils.toPGString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
|
||||
|
||||
assertEquals(1, statementList.size());
|
||||
|
||||
PGSchemaStatVisitor visitor = new PGSchemaStatVisitor();
|
||||
stmt.accept(visitor);
|
||||
|
||||
// System.out.println("Tables : " + visitor.getTables());
|
||||
// System.out.println("fields : " + visitor.getColumns());
|
||||
|
||||
assertTrue(visitor.getTables().containsKey(new TableStat.Name("KTV.KTV_FUNCTION_ROLE_20151211")));
|
||||
|
||||
assertTrue(visitor.getTables().get(new TableStat.Name("KTV.KTV_FUNCTION_ROLE_20151211")).getCreateCount() == 1);
|
||||
|
||||
assertEquals(4, visitor.getColumns().size() );
|
||||
}
|
||||
|
||||
}
|
||||
+1
-1
@@ -13,7 +13,7 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.alibaba.druid.bvt.sql.postgresql.select;
|
||||
package com.alibaba.druid.not_support.sql.postgresql;
|
||||
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
Reference in New Issue
Block a user