mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
bug fixed for SchemaStatVisitor. for issue #2407
This commit is contained in:
@@ -18,6 +18,7 @@ package com.alibaba.druid.stat;
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLPropertyExpr;
|
||||
import com.alibaba.druid.support.json.JSONUtils;
|
||||
import com.alibaba.druid.util.FnvHash;
|
||||
import com.alibaba.druid.util.JdbcConstants;
|
||||
|
||||
@@ -366,26 +367,32 @@ public class TableStat {
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
stringBuilder.append(this.column.toString());
|
||||
stringBuilder.append(' ');
|
||||
stringBuilder.append(this.operator);
|
||||
StringBuilder buf = new StringBuilder();
|
||||
buf.append(this.column.toString());
|
||||
buf.append(' ');
|
||||
buf.append(this.operator);
|
||||
|
||||
if (values.size() == 1) {
|
||||
stringBuilder.append(' ');
|
||||
stringBuilder.append(String.valueOf(this.values.get(0)));
|
||||
buf.append(' ');
|
||||
buf.append(String.valueOf(this.values.get(0)));
|
||||
} else if (values.size() > 0) {
|
||||
stringBuilder.append(" (");
|
||||
buf.append(" (");
|
||||
for (int i = 0; i < values.size(); ++i) {
|
||||
if (i != 0) {
|
||||
stringBuilder.append(", ");
|
||||
buf.append(", ");
|
||||
}
|
||||
Object val = values.get(i);
|
||||
if (val instanceof String) {
|
||||
String jsonStr = JSONUtils.toJSONString(val);
|
||||
buf.append(jsonStr);
|
||||
} else {
|
||||
buf.append(String.valueOf(val));
|
||||
}
|
||||
stringBuilder.append(String.valueOf(values.get(i)));
|
||||
}
|
||||
stringBuilder.append(")");
|
||||
buf.append(")");
|
||||
}
|
||||
|
||||
return stringBuilder.toString();
|
||||
return buf.toString();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+53
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
* 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.mysql.visitor;
|
||||
|
||||
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 junit.framework.TestCase;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class MySqlSchemaStatVisitorTest9_in extends TestCase {
|
||||
|
||||
public void test_0() throws Exception {
|
||||
String sql = "SELECT name FROM employee WHERE no in ('1', '2');";
|
||||
|
||||
// sql = "select columnName from table1 where id in (select id from table3 where name = ?)";
|
||||
MySqlStatementParser parser = new MySqlStatementParser(sql);
|
||||
List<SQLStatement> statementList = parser.parseStatementList();
|
||||
SQLStatement stmt = statementList.get(0);
|
||||
|
||||
assertEquals(1, statementList.size());
|
||||
|
||||
MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
|
||||
stmt.accept(visitor);
|
||||
|
||||
System.out.println(stmt.toString());
|
||||
|
||||
// System.out.println(sql);
|
||||
System.out.println("Tables : " + visitor.getTables());
|
||||
System.out.println("fields : " + visitor.getColumns());
|
||||
System.out.println(visitor.getConditions());
|
||||
|
||||
assertEquals(1, visitor.getTables().size());
|
||||
assertEquals("[employee.no IN (\"1\", \"2\")]", visitor.getConditions().toString());
|
||||
// Column("users", "name")));
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user