Class ClickHouseUtils


  • public final class ClickHouseUtils
    extends Object
    • Method Detail

      • newThreadPool

        public static ExecutorService newThreadPool​(Object owner,
                                                    int maxThreads,
                                                    int maxRequests)
      • newThreadPool

        public static ExecutorService newThreadPool​(Object owner,
                                                    int coreThreads,
                                                    int maxThreads,
                                                    int maxRequests,
                                                    long keepAliveTimeoutMs,
                                                    boolean allowCoreThreadTimeout)
      • isCloseBracket

        public static boolean isCloseBracket​(char ch)
      • isOpenBracket

        public static boolean isOpenBracket​(char ch)
      • isQuote

        public static boolean isQuote​(char ch)
      • isSeparator

        public static boolean isSeparator​(char ch)
      • escape

        public static String escape​(String str,
                                    char quote)
        Escape quotes in given string.
        Parameters:
        str - string
        quote - quote to escape
        Returns:
        escaped string
      • unescape

        public static String unescape​(String str)
        Unescape quoted string.
        Parameters:
        str - quoted string
        Returns:
        unescaped string
      • format

        public static String format​(String template,
                                    Object... args)
        Wrapper of String.format(Locale.ROOT, ...).
        Parameters:
        template - string to format
        args - arguments used in substitution
        Returns:
        formatted string
      • parseJson

        public static Object parseJson​(String json)
        Simple and un-protected JSON parser.
        Parameters:
        json - non-empty JSON string
        Returns:
        object array, Boolean, Number, null, String, or Map
        Throws:
        IllegalArgumentException - when JSON string is null or empty
      • getCloseBracket

        public static char getCloseBracket​(char openBracket)
      • getService

        public static <T> T getService​(Class<? extends T> serviceInterface)
      • getService

        public static <T> T getService​(Class<? extends T> serviceInterface,
                                       T defaultService)
        Load service according to given interface using ServiceLoader, fallback to given default service or supplier function if not found.
        Type Parameters:
        T - type of service
        Parameters:
        serviceInterface - non-null service interface
        defaultService - optionally default service
        Returns:
        non-null service
      • getService

        public static <T> T getService​(Class<? extends T> serviceInterface,
                                       Supplier<T> supplier)
      • getFileInputStream

        public static InputStream getFileInputStream​(String file)
                                              throws FileNotFoundException
        Search file in current directory, home directory, and then classpath, Get input stream to read the given file.
        Parameters:
        file - path to the file
        Returns:
        input stream
        Throws:
        FileNotFoundException - when the file does not exists
      • getFileOutputStream

        public static OutputStream getFileOutputStream​(String file)
                                                throws IOException
        Get output stream for writing a file. Directories and file will be created if they do not exist.
        Parameters:
        file - path to the file
        Returns:
        output stream
        Throws:
        IOException - when failed to create directories and/or file
      • getKeyValuePairs

        public static Map<String,​String> getKeyValuePairs​(String str)
        Extracts key value pairs from the given string.
        Parameters:
        str - string
        Returns:
        non-null map containing extracted key value pairs
      • getLeadingComment

        public static String getLeadingComment​(String sql)
      • skipBrackets

        public static int skipBrackets​(String args,
                                       int startIndex,
                                       int len,
                                       char bracket)
        Skip brackets and content inside with consideration of nested brackets, quoted string and comments.
        Parameters:
        args - non-null string to scan
        startIndex - start index, optionally index of the opening bracket
        len - end index, usually length of the given string
        bracket - opening bracket supported by isOpenBracket(char)
        Returns:
        index next to matched closing bracket
        Throws:
        IllegalArgumentException - when missing closing bracket(s)
      • skipQuotedString

        public static int skipQuotedString​(String args,
                                           int startIndex,
                                           int len,
                                           char quote)
        Skip quoted string.
        Parameters:
        args - non-null string to scan
        startIndex - start index, optionally start of the quoted string
        len - end index, usually length of the given string
        quote - quote supported by isQuote(char)
        Returns:
        index next to the other matched quote
        Throws:
        IllegalArgumentException - when missing quote
      • skipSingleLineComment

        public static int skipSingleLineComment​(String args,
                                                int startIndex,
                                                int len)
        Skip single line comment.
        Parameters:
        args - non-null string to scan
        startIndex - start index, optionally start of the single line comment
        len - end index, usually length of the given string
        Returns:
        index of start of next line, right after \n
      • skipMultiLineComment

        public static int skipMultiLineComment​(String args,
                                               int startIndex,
                                               int len)
        Skip nested multi-line comment.
        Parameters:
        args - non-null string to scan
        startIndex - start index, optionally start of the multi-line comment
        len - end index, usually length of the given string
        Returns:
        index next to end of the outter most multi-line comment
        Throws:
        IllegalArgumentException - when multi-line comment is unclosed
      • skipContentsUntil

        public static int skipContentsUntil​(String args,
                                            int startIndex,
                                            int len,
                                            char... endChars)
        Skip quoted string, comments, and brackets until seeing one of endChars or reaching end of the given string.
        Parameters:
        args - non-null string to scan
        startIndex - start index
        len - end index, usually length of the given string
        endChars - skip characters until seeing one of the specified characters or reaching end of the string; '\0' is used when it's null or empty
        Returns:
        index of endChar or len
      • skipContentsUntil

        public static int skipContentsUntil​(String args,
                                            int startIndex,
                                            int len,
                                            String keyword,
                                            boolean caseSensitive)
        Skip quoted string, comments, and brackets until seeing the keyword or reaching end of the given string.
        Parameters:
        args - non-null string to scan
        startIndex - start index
        len - end index, usually length of the given string
        keyword - keyword, null or empty string means any character
        caseSensitive - whether keyword is case sensitive or not
        Returns:
        index of endChar or len
      • skipContentsUntil

        public static int skipContentsUntil​(String args,
                                            int startIndex,
                                            int len,
                                            String[] keywords,
                                            boolean caseSensitive)
        Skip quoted string, comments, and brackets until seeing all the given keywords(with only whitespaces or comments in between) or reaching end of the given string.
        Parameters:
        args - non-null string to scan
        startIndex - start index
        len - end index, usually length of the given string
        keywords - keywords, null or empty one means any character
        caseSensitive - whether keyword is case sensitive or not
        Returns:
        index of endChar or len
      • readNameOrQuotedString

        public static int readNameOrQuotedString​(String args,
                                                 int startIndex,
                                                 int len,
                                                 StringBuilder builder)
      • readEnumValues

        public static int readEnumValues​(String args,
                                         int startIndex,
                                         int len,
                                         Map<String,​Integer> values)
      • readValueArray

        public static List<String> readValueArray​(String args,
                                                  int startIndex,
                                                  int len)
      • readValueArray

        public static int readValueArray​(String args,
                                         int startIndex,
                                         int len,
                                         Consumer<String> func)
      • readParameters

        public static int readParameters​(String args,
                                         int startIndex,
                                         int len,
                                         List<String> params)
      • toArray

        protected static <T> T[] toArray​(Class<T> clazz,
                                         Collection<T> list)