use of com.alibaba.druid.sql.dialect.mysql.visitor.MySqlOutputVisitor in project sharding-jdbc by dangdangdotcom.
the class MySQLSelectVisitor method visit.
@Override
public boolean visit(final SQLAggregateExpr x) {
if (!(x.getParent() instanceof SQLSelectItem)) {
return super.visit(x);
}
AggregationType aggregationType;
try {
aggregationType = AggregationType.valueOf(x.getMethodName().toUpperCase());
} catch (final IllegalArgumentException ex) {
return super.visit(x);
}
StringBuilder expression = new StringBuilder();
x.accept(new MySqlOutputVisitor(expression));
// TODO index获取不准,考虑使用别名替换
AggregationColumn column = new AggregationColumn(expression.toString(), aggregationType, Optional.fromNullable(((SQLSelectItem) x.getParent()).getAlias()), null == x.getOption() ? Optional.<String>absent() : Optional.of(x.getOption().toString()), getParseContext().getItemIndex());
getParseContext().getParsedResult().getMergeContext().getAggregationColumns().add(column);
if (AggregationType.AVG.equals(aggregationType)) {
getParseContext().addDerivedColumnsForAvgColumn(column);
// TODO 将AVG列替换成常数,避免数据库再计算无用的AVG函数
}
return super.visit(x);
}
use of com.alibaba.druid.sql.dialect.mysql.visitor.MySqlOutputVisitor in project druid by alibaba.
the class Bug_for_weizhi method test_for_issue.
public void test_for_issue() throws Exception {
String sql = "insert into aaa values(1,2,'这是个反斜杠\\\\');";
String expected = "INSERT INTO aaa\nVALUES (1, 2, '这是个反斜杠\\\\');";
StringBuilder out = new StringBuilder();
MySqlOutputVisitor visitor = new MySqlOutputVisitor(out);
MySqlStatementParser parser = new MySqlStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
for (SQLStatement statement : statementList) {
statement.accept(visitor);
visitor.print(";");
}
System.out.println(out.toString());
Assert.assertEquals(expected, out.toString());
}
use of com.alibaba.druid.sql.dialect.mysql.visitor.MySqlOutputVisitor in project druid by alibaba.
the class MySqlPerfTest method execMySql.
private String execMySql(String sql) {
StringBuilder out = new StringBuilder();
MySqlOutputVisitor visitor = new MySqlOutputVisitor(out);
MySqlStatementParser parser = new MySqlStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
// }
return out.toString();
}
use of com.alibaba.druid.sql.dialect.mysql.visitor.MySqlOutputVisitor in project druid by alibaba.
the class AssignmentOperatorsTest method output.
private String output(List<SQLStatement> stmtList) {
StringBuilder out = new StringBuilder();
for (SQLStatement stmt : stmtList) {
stmt.accept(new MySqlOutputVisitor(out));
out.append(";");
}
return out.toString();
}
use of com.alibaba.druid.sql.dialect.mysql.visitor.MySqlOutputVisitor in project druid by alibaba.
the class BooleanValuesTest method output.
private String output(List<SQLStatement> stmtList) {
StringBuilder out = new StringBuilder();
for (SQLStatement stmt : stmtList) {
stmt.accept(new MySqlOutputVisitor(out));
out.append(";");
}
return out.toString();
}
Aggregations