mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
improved sql parser. bug fixed for issue #2446
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
package com.alibaba.druid.bvt.sql.clickhouse;
|
||||
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.dialect.db2.visitor.DB2SchemaStatVisitor;
|
||||
import com.alibaba.druid.util.JdbcConstants;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class ClickHouse_select_0 extends TestCase {
|
||||
public void test_0() throws Exception {
|
||||
String sql = "SELECT date, transactionChannel, tranactionType FROM preComp_3All_20180322 limit 1,10";
|
||||
|
||||
List<SQLStatement> statementList = SQLUtils.parseStatements(sql, JdbcConstants.CLICKHOUSE);
|
||||
SQLStatement stmt = statementList.get(0);
|
||||
|
||||
assertEquals(1, statementList.size());
|
||||
|
||||
DB2SchemaStatVisitor visitor = new DB2SchemaStatVisitor();
|
||||
stmt.accept(visitor);
|
||||
|
||||
// System.out.println("Tables : " + visitor.getTables());
|
||||
// System.out.println("fields : " + visitor.getColumns());
|
||||
// System.out.println("coditions : " + visitor.getConditions());
|
||||
// System.out.println("orderBy : " + visitor.getOrderByColumns());
|
||||
|
||||
assertEquals(1, visitor.getTables().size());
|
||||
assertEquals(3, visitor.getColumns().size());
|
||||
assertEquals(0, visitor.getConditions().size());
|
||||
|
||||
assertTrue(visitor.containsTable("preComp_3All_20180322"));
|
||||
|
||||
// assertTrue(visitor.getColumns().contains(new Column("mytable", "last_name")));
|
||||
// assertTrue(visitor.getColumns().contains(new Column("mytable", "first_name")));
|
||||
// assertTrue(visitor.getColumns().contains(new Column("mytable", "full_name")));
|
||||
|
||||
String output = SQLUtils.toSQLString(stmt, JdbcConstants.CLICKHOUSE);
|
||||
assertEquals("SELECT date, transactionChannel, tranactionType\n" +
|
||||
"FROM preComp_3All_20180322\n" +
|
||||
"LIMIT 1, 10", //
|
||||
output);
|
||||
}
|
||||
}
|
||||
@@ -39,7 +39,8 @@ public class DMLSelectParserTest extends TestCase {
|
||||
"UNION ALL\n" +
|
||||
"(SELECT id\n" +
|
||||
"FROM t3)\n" +
|
||||
"ORDER BY d DESC", output);
|
||||
"ORDER BY d DESC\n" +
|
||||
"LIMIT ?, 1", output);
|
||||
}
|
||||
|
||||
public void test_union_1() throws Exception {
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* 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.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 PGDeleteTest10 extends PGTest {
|
||||
|
||||
public void test_0() throws Exception {
|
||||
String sql = "delete from account a using (values (1), (7)) as tmp(id) WHERE a.id = tmp.id";
|
||||
|
||||
PGSQLStatementParser parser = new PGSQLStatementParser(sql);
|
||||
List<SQLStatement> statementList = parser.parseStatementList();
|
||||
SQLStatement stmt = statementList.get(0);
|
||||
|
||||
assertEquals("DELETE FROM account AS a\n" +
|
||||
"USING (VALUES (1), (7)) AS tmp (id)\n" +
|
||||
"WHERE a.id = tmp.id", stmt.toString());
|
||||
|
||||
assertEquals(1, statementList.size());
|
||||
|
||||
PGSchemaStatVisitor visitor = new PGSchemaStatVisitor();
|
||||
stmt.accept(visitor);
|
||||
|
||||
System.out.println("Tables : " + visitor.getTables());
|
||||
System.out.println("fields : " + visitor.getColumns());
|
||||
// System.out.println("coditions : " + visitor.getConditions());
|
||||
|
||||
assertTrue(visitor.getTables().containsKey(new TableStat.Name("account")));
|
||||
|
||||
assertEquals(2, visitor.getColumns().size() );
|
||||
|
||||
assertTrue(visitor.containsColumn("account", "id"));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -17,13 +17,11 @@ public class SchemaStatTest14 extends TestCase {
|
||||
public void test_schemaStat() throws Exception {
|
||||
String sql = "delete r from t_res r where id=1 ";
|
||||
|
||||
String dbType = JdbcConstants.MYSQL;
|
||||
SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, dbType);
|
||||
SQLStatement stmt = parser.parseStatementList().get(0);
|
||||
SQLStatement stmt = SQLUtils.parseStatements(sql, JdbcConstants.MYSQL).get(0);
|
||||
|
||||
System.out.println(stmt);
|
||||
|
||||
SchemaStatVisitor statVisitor = SQLUtils.createSchemaStatVisitor(dbType);
|
||||
SchemaStatVisitor statVisitor = SQLUtils.createSchemaStatVisitor(JdbcConstants.MYSQL);
|
||||
stmt.accept(statVisitor);
|
||||
|
||||
Set<TableStat.Relationship> relationships = statVisitor.getRelationships();
|
||||
|
||||
Reference in New Issue
Block a user