use of com.alibaba.druid.sql.visitor.SchemaStatVisitor in project druid by alibaba.
the class SchemaStatTest6 method test_schemaStat.
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());
// group by
System.out.println(statVisitor.getGroupByColumns());
Assert.assertEquals(2, statVisitor.getColumns().size());
}
use of com.alibaba.druid.sql.visitor.SchemaStatVisitor in project druid by alibaba.
the class SchemaStatTest_odps method test_schemaStat.
public void test_schemaStat() throws Exception {
File file = new File("/Users/wenshao/Downloads/odps_sql_1.txt");
String sql = FileUtils.readFileToString(file);
String dbType = JdbcConstants.ODPS;
SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, dbType);
List<SQLStatement> stmtList = parser.parseStatementList();
System.out.println("stmtList size : " + stmtList.size());
SchemaStatVisitor statVisitor = SQLUtils.createSchemaStatVisitor(dbType);
for (SQLStatement stmt : stmtList) {
stmt.accept(statVisitor);
}
Set<TableStat.Relationship> relationships = statVisitor.getRelationships();
for (TableStat.Relationship relationship : relationships) {
// table1.id = table2.id
System.out.println(relationship);
}
// System.out.println(statVisitor.getColumns());
// System.out.println(statVisitor.getGroupByColumns()); // group by
// group by
System.out.println("relationships : " + statVisitor.getRelationships());
System.out.println(statVisitor.getConditions());
// assertEquals(3, relationships.size());
//
// Assert.assertEquals(21, statVisitor.getColumns().size());
// Assert.assertEquals(20, statVisitor.getConditions().size());
// assertEquals(1, statVisitor.getFunctions().size());
}
use of com.alibaba.druid.sql.visitor.SchemaStatVisitor in project druid by alibaba.
the class ExportConditions method evaluate.
public String evaluate(String sql, String dbType, Boolean compactValues) {
try {
List<SQLStatement> statementList = SQLUtils.parseStatements(sql, dbType);
SchemaStatVisitor visitor = SQLUtils.createSchemaStatVisitor(dbType);
for (SQLStatement stmt : statementList) {
stmt.accept(visitor);
}
List<List<Object>> rows = new ArrayList<List<Object>>();
List<Condition> conditions = visitor.getConditions();
for (int i = 0; i < conditions.size(); ++i) {
TableStat.Condition condition = conditions.get(i);
Column column = condition.getColumn();
String operator = condition.getOperator();
List<Object> values = condition.getValues();
List<Object> row = new ArrayList<Object>();
row.add(column.getTable());
row.add(column.getName());
row.add(operator);
if (values.size() == 0) {
row.add(null);
} else if (values.size() == 1) {
if (compactValues != null && compactValues.booleanValue()) {
row.add(values);
} else {
row.add(values.get(0));
}
} else {
row.add(values);
}
rows.add(row);
}
return JSONUtils.toJSONString(rows);
} catch (Throwable ex) {
System.err.println("error sql : " + sql);
ex.printStackTrace();
return null;
}
}
use of com.alibaba.druid.sql.visitor.SchemaStatVisitor in project druid by alibaba.
the class ExportSelectListColumns method evaluate.
public String evaluate(String sql, String dbType) {
try {
List<SQLStatement> statementList = SQLUtils.parseStatements(sql, dbType);
SchemaStatVisitor visitor = SQLUtils.createSchemaStatVisitor(dbType);
for (SQLStatement stmt : statementList) {
stmt.accept(visitor);
}
StringBuffer buf = new StringBuffer();
for (TableStat.Column column : visitor.getColumns()) {
if (!column.isSelect()) {
continue;
}
if (buf.length() != 0) {
buf.append(',');
}
buf.append(column.toString());
}
return buf.toString();
} catch (Throwable ex) {
System.err.println("error sql : " + sql);
ex.printStackTrace();
return null;
}
}
use of com.alibaba.druid.sql.visitor.SchemaStatVisitor in project druid by alibaba.
the class MySqlAlterTableDropIndex_0 method test_alter_first.
public void test_alter_first() throws Exception {
String sql = "ALTER TABLE `test`.`tb1` DROP INDEX `ix` ;";
MySqlStatementParser parser = new MySqlStatementParser(sql);
SQLStatement stmt = parser.parseStatementList().get(0);
parser.match(Token.EOF);
Assert.assertEquals(//
"ALTER TABLE `test`.`tb1`" + "\n\tDROP INDEX `ix`", SQLUtils.toMySqlString(stmt));
Assert.assertEquals(//
"alter table `test`.`tb1`" + "\n\tdrop index `ix`", SQLUtils.toMySqlString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
SchemaStatVisitor visitor = new SQLUtils().createSchemaStatVisitor(JdbcConstants.MYSQL);
stmt.accept(visitor);
TableStat tableStat = visitor.getTableStat("test.tb1");
assertNotNull(tableStat);
assertEquals(1, tableStat.getAlterCount());
assertEquals(1, tableStat.getDropIndexCount());
}
Aggregations