[关闭]
@rickyChen 2018-02-01T17:06:21.000000Z 字数 1106 阅读 2218

Clickhouse-jdbc源码阅读笔记

ClickHouse


clickhouse-jdbc 0.1.36

  1. executeQuery和execute的区别
    execute其实还是调用的是executeQuery方法,只是返回的是isSelect(sql),因此在执行INSERT方法时返回false

    1. @Override
    2. public boolean execute(String sql) throws SQLException {
    3. // currentResult is stored here. InputString and currentResult will be closed on this.close()
    4. executeQuery(sql);
    5. return isSelect(sql);
    6. }
    7. private static boolean isSelect(String sql) {
    8. String upper = sql.toUpperCase().trim();
    9. return upper.startsWith("SELECT") || upper.startsWith("WITH") || upper.startsWith("SHOW") ||
    10. upper.startsWith("DESC") || upper.startsWith("EXISTS");
    11. }
  2. clickhousifySql
    在执行最终的SQL之前,会将用户传入的SQL clickhouse化,也是只针对SELECT、SHOW等方法

    1. static String clickhousifySql(String sql) {
    2. return addFormatIfAbsent(sql, "TabSeparatedWithNamesAndTypes");
    3. }
    4. private static String addFormatIfAbsent(String sql, String format) {
    5. sql = sql.trim();
    6. String woSemicolon = Patterns.SEMICOLON.matcher(sql).replaceAll("").trim();
    7. if (isSelect(sql)
    8. && !woSemicolon.endsWith(" TabSeparatedWithNamesAndTypes")
    9. && !woSemicolon.endsWith(" TabSeparated")
    10. && !woSemicolon.endsWith(" JSONCompact")) {
    11. if (sql.endsWith(";")) {
    12. sql = sql.substring(0, sql.length() - 1);
    13. }
    14. sql += " FORMAT " + format + ';';
    15. }
    16. return sql;
    17. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注