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,55 @@
|
||||
/*
|
||||
* Copyright 1999-2101 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.mysql;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.alibaba.druid.sql.MysqlTest;
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser;
|
||||
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor;
|
||||
|
||||
public class MySqlInsertTest_11 extends MysqlTest {
|
||||
|
||||
public void test_insert_rollback_on_fail() throws Exception {
|
||||
String sql = "insert ROLLBACK_ON_FAIL into t(a,b) values ('a1','b1')";
|
||||
|
||||
MySqlStatementParser parser = new MySqlStatementParser(sql);
|
||||
parser.setParseCompleteValues(false);
|
||||
parser.setParseValuesSize(3);
|
||||
List<SQLStatement> statementList = parser.parseStatementList();
|
||||
SQLStatement stmt = statementList.get(0);
|
||||
|
||||
MySqlInsertStatement insertStmt = (MySqlInsertStatement) stmt;
|
||||
|
||||
Assert.assertEquals(1, insertStmt.getValuesList().size());
|
||||
Assert.assertEquals(2, insertStmt.getValues().getValues().size());
|
||||
Assert.assertEquals(2, insertStmt.getColumns().size());
|
||||
Assert.assertEquals(1, statementList.size());
|
||||
|
||||
MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
|
||||
stmt.accept(visitor);
|
||||
|
||||
String formatSql = "INSERT ROLLBACK_ON_FAIL INTO t (a, b)"//
|
||||
+ "\nVALUES ('a1', 'b1')";
|
||||
Assert.assertEquals(formatSql, SQLUtils.toMySqlString(insertStmt));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
/*
|
||||
* Copyright 1999-2101 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.mysql;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.alibaba.druid.sql.MysqlTest;
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser;
|
||||
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor;
|
||||
import com.alibaba.druid.stat.TableStat;
|
||||
import com.alibaba.druid.stat.TableStat.Column;
|
||||
|
||||
public class MySqlUpdateTest_8 extends MysqlTest {
|
||||
|
||||
public void test_0() throws Exception {
|
||||
String sql = "update haskell_function set `arity` = arity-'1' where id = 1;";
|
||||
|
||||
MySqlStatementParser parser = new MySqlStatementParser(sql);
|
||||
List<SQLStatement> statementList = parser.parseStatementList();
|
||||
SQLStatement stmt = statementList.get(0);
|
||||
print(statementList);
|
||||
|
||||
Assert.assertEquals(1, statementList.size());
|
||||
|
||||
MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
|
||||
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());
|
||||
|
||||
Assert.assertEquals(1, visitor.getTables().size());
|
||||
Assert.assertEquals(2, visitor.getColumns().size());
|
||||
Assert.assertEquals(1, visitor.getConditions().size());
|
||||
|
||||
Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("haskell_function")));
|
||||
|
||||
Assert.assertTrue(visitor.getColumns().contains(new Column("haskell_function", "arity")));
|
||||
Assert.assertTrue(visitor.getColumns().contains(new Column("haskell_function", "id")));
|
||||
|
||||
{
|
||||
String output = SQLUtils.toMySqlString(stmt);
|
||||
Assert.assertEquals("UPDATE haskell_function"
|
||||
+ "\nSET `arity` = arity - '1'"
|
||||
+ "\nWHERE id = 1", //
|
||||
output);
|
||||
}
|
||||
{
|
||||
String output = SQLUtils.toMySqlString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION);
|
||||
Assert.assertEquals("update haskell_function"
|
||||
+ "\nset `arity` = arity - '1'"
|
||||
+ "\nwhere id = 1", //
|
||||
output);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.alibaba.druid.bvt.sql.schemaStat;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
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 junit.framework.TestCase;
|
||||
|
||||
public class SchemaStatTest4 extends TestCase {
|
||||
|
||||
public void test_schemaStat() throws Exception {
|
||||
String sql = "select name, age from t_user where id = 1";
|
||||
|
||||
String dbType = JdbcConstants.MYSQL;
|
||||
List<SQLStatement> stmtList = SQLUtils.parseStatements(sql, dbType);
|
||||
SQLStatement stmt = stmtList.get(0);
|
||||
|
||||
SchemaStatVisitor statVisitor = SQLUtils.createSchemaStatVisitor(dbType);
|
||||
stmt.accept(statVisitor);
|
||||
|
||||
System.out.println(statVisitor.getColumns());
|
||||
System.out.println(statVisitor.getTables());
|
||||
System.out.println(statVisitor.getConditions());
|
||||
|
||||
Assert.assertEquals(3, statVisitor.getColumns().size());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.alibaba.druid.bvt.sql.schemaStat;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
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 junit.framework.TestCase;
|
||||
|
||||
public class SchemaStatTest5 extends TestCase {
|
||||
|
||||
public void test_schemaStat() throws Exception {
|
||||
String sql = "create table t_org (fid int, name varchar(256))";
|
||||
|
||||
String dbType = JdbcConstants.MYSQL;
|
||||
List<SQLStatement> stmtList = SQLUtils.parseStatements(sql, dbType);
|
||||
SQLStatement stmt = stmtList.get(0);
|
||||
|
||||
SchemaStatVisitor statVisitor = SQLUtils.createSchemaStatVisitor(dbType);
|
||||
stmt.accept(statVisitor);
|
||||
|
||||
System.out.println(statVisitor.getColumns());
|
||||
System.out.println(statVisitor.getTables());
|
||||
System.out.println(statVisitor.getConditions());
|
||||
|
||||
Assert.assertEquals(2, statVisitor.getColumns().size());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.alibaba.druid.bvt.sql.schemaStat;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.parser.SQLParserUtils;
|
||||
import com.alibaba.druid.sql.parser.SQLStatementParser;
|
||||
import com.alibaba.druid.sql.visitor.SchemaStatVisitor;
|
||||
import com.alibaba.druid.util.JdbcConstants;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
public class SchemaStatTest6 extends TestCase {
|
||||
public void test_schemaStat() throws Exception {
|
||||
String sql = "select count(1), name from tg_rpc_user where id < 5 group by name order by id desc";
|
||||
|
||||
|
||||
String dbType = JdbcConstants.MYSQL;
|
||||
SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, dbType);
|
||||
SQLStatement stmt = parser.parseStatementList().get(0);
|
||||
|
||||
SchemaStatVisitor statVisitor = SQLUtils.createSchemaStatVisitor(dbType);
|
||||
stmt.accept(statVisitor);
|
||||
|
||||
System.out.println(statVisitor.getColumns());
|
||||
System.out.println(statVisitor.getGroupByColumns()); // group by
|
||||
|
||||
Assert.assertEquals(2, statVisitor.getColumns().size());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user