All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ru.yandex.clickhouse.ClickHouseUtil Maven / Gradle / Ivy

There is a newer version: 0.3.2
Show newest version
package ru.yandex.clickhouse;


import com.google.common.escape.Escaper;
import com.google.common.escape.Escapers;

public class ClickHouseUtil {

    private final static Escaper CLICKHOUSE_ESCAPER = Escapers.builder()
        .addEscape('\\', "\\\\")
        .addEscape('\n', "\\n")
        .addEscape('\t', "\\t")
        .addEscape('\b', "\\b")
        .addEscape('\f', "\\f")
        .addEscape('\r', "\\r")
        .addEscape('\0', "\\0")
        .addEscape('\'', "\\'")
        .addEscape('`', "\\`")
        .build();

    public static String escape(String s) {
        if (s == null) {
            return "\\N";
        }
        return CLICKHOUSE_ESCAPER.escape(s);
    }

    public static String quoteIdentifier(String s) {
        if (s == null) {
            throw new IllegalArgumentException("Can't quote null as identifier");
        }
        StringBuilder sb = new StringBuilder(s.length() + 2);
        sb.append('`');
        sb.append(CLICKHOUSE_ESCAPER.escape(s));
        sb.append('`');
        return sb.toString();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy