@rickyChen
2018-02-01T17:06:21.000000Z
字数 1106
阅读 2218
ClickHouse
clickhouse-jdbc 0.1.36
executeQuery和execute的区别
execute其实还是调用的是executeQuery
方法,只是返回的是isSelect(sql),因此在执行INSERT方法时返回false
@Override
public boolean execute(String sql) throws SQLException {
// currentResult is stored here. InputString and currentResult will be closed on this.close()
executeQuery(sql);
return isSelect(sql);
}
private static boolean isSelect(String sql) {
String upper = sql.toUpperCase().trim();
return upper.startsWith("SELECT") || upper.startsWith("WITH") || upper.startsWith("SHOW") ||
upper.startsWith("DESC") || upper.startsWith("EXISTS");
}
clickhousifySql
在执行最终的SQL之前,会将用户传入的SQL clickhouse化,也是只针对SELECT、SHOW等方法
static String clickhousifySql(String sql) {
return addFormatIfAbsent(sql, "TabSeparatedWithNamesAndTypes");
}
private static String addFormatIfAbsent(String sql, String format) {
sql = sql.trim();
String woSemicolon = Patterns.SEMICOLON.matcher(sql).replaceAll("").trim();
if (isSelect(sql)
&& !woSemicolon.endsWith(" TabSeparatedWithNamesAndTypes")
&& !woSemicolon.endsWith(" TabSeparated")
&& !woSemicolon.endsWith(" JSONCompact")) {
if (sql.endsWith(";")) {
sql = sql.substring(0, sql.length() - 1);
}
sql += " FORMAT " + format + ';';
}
return sql;
}