Package com.clickhouse.client
Class ClickHouseUtils
- java.lang.Object
-
- com.clickhouse.client.ClickHouseUtils
-
public final class ClickHouseUtils extends Object
-
-
Field Summary
Fields Modifier and Type Field Description static String
VARIABLE_PREFIX
static String
VARIABLE_SUFFIX
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static String
applyVariables(String template, UnaryOperator<String> applyFunc)
static String
applyVariables(String template, Map<String,String> variables)
static String
escape(String str, char quote)
Escape quotes in given string.static String
format(String template, Object... args)
Wrapper ofString.format(Locale.ROOT, ...)
.static char
getCloseBracket(char openBracket)
static InputStream
getFileInputStream(String file)
Search file in current directory, home directory, and then classpath, Get input stream to read the given file.static OutputStream
getFileOutputStream(String file)
Get output stream for writing a file.static Map<String,String>
getKeyValuePairs(String str)
Extracts key value pairs from the given string.static String
getLeadingComment(String sql)
static String
getProperty(String key, String defaultValue, Properties... props)
static String
getProperty(String key, Properties... props)
static <T> T
getService(Class<? extends T> serviceInterface)
static <T> T
getService(Class<? extends T> serviceInterface, Supplier<T> supplier)
static <T> T
getService(Class<? extends T> serviceInterface, T defaultService)
Load service according to given interface usingServiceLoader
, fallback to given default service or supplier function if not found.static boolean
isCloseBracket(char ch)
static boolean
isOpenBracket(char ch)
static boolean
isQuote(char ch)
static boolean
isSeparator(char ch)
static ExecutorService
newThreadPool(Object owner, int maxThreads, int maxRequests)
static ExecutorService
newThreadPool(Object owner, int coreThreads, int maxThreads, int maxRequests, long keepAliveTimeoutMs, boolean allowCoreThreadTimeout)
static Object
parseJson(String json)
Simple and un-protected JSON parser.static int
readEnumValues(String args, int startIndex, int len, Map<String,Integer> values)
static int
readNameOrQuotedString(String args, int startIndex, int len, StringBuilder builder)
static int
readParameters(String args, int startIndex, int len, List<String> params)
static List<String>
readValueArray(String args, int startIndex, int len)
static int
readValueArray(String args, int startIndex, int len, Consumer<String> func)
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.static int
skipContentsUntil(String args, int startIndex, int len, char... endChars)
Skip quoted string, comments, and brackets until seeing one ofendChars
or reaching end of the given string.static int
skipContentsUntil(String args, int startIndex, int len, String[] keywords, boolean caseSensitive)
Skip quoted string, comments, and brackets until seeing all the givenkeywords
(with only whitespaces or comments in between) or reaching end of the given string.static int
skipContentsUntil(String args, int startIndex, int len, String keyword, boolean caseSensitive)
Skip quoted string, comments, and brackets until seeing thekeyword
or reaching end of the given string.static int
skipMultiLineComment(String args, int startIndex, int len)
Skip nested multi-line comment.static int
skipQuotedString(String args, int startIndex, int len, char quote)
Skip quoted string.static int
skipSingleLineComment(String args, int startIndex, int len)
Skip single line comment.protected static <T> T[]
toArray(Class<T> clazz, Collection<T> list)
static String
unescape(String str)
Unescape quoted string.
-
-
-
Field Detail
-
VARIABLE_PREFIX
public static final String VARIABLE_PREFIX
- See Also:
- Constant Field Values
-
VARIABLE_SUFFIX
public static final String VARIABLE_SUFFIX
- See Also:
- Constant Field Values
-
-
Method Detail
-
applyVariables
public static String applyVariables(String template, UnaryOperator<String> applyFunc)
-
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
- stringquote
- 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 ofString.format(Locale.ROOT, ...)
.- Parameters:
template
- string to formatargs
- 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 usingServiceLoader
, fallback to given default service or supplier function if not found.- Type Parameters:
T
- type of service- Parameters:
serviceInterface
- non-null service interfacedefaultService
- 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
-
getProperty
public static String getProperty(String key, Properties... props)
-
getProperty
public static String getProperty(String key, String defaultValue, Properties... props)
-
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 scanstartIndex
- start index, optionally index of the opening bracketlen
- end index, usually length of the given stringbracket
- opening bracket supported byisOpenBracket(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 scanstartIndex
- start index, optionally start of the quoted stringlen
- end index, usually length of the given stringquote
- quote supported byisQuote(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 scanstartIndex
- start index, optionally start of the single line commentlen
- 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 scanstartIndex
- start index, optionally start of the multi-line commentlen
- 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 ofendChars
or reaching end of the given string.- Parameters:
args
- non-null string to scanstartIndex
- start indexlen
- end index, usually length of the given stringendChars
- 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
orlen
-
skipContentsUntil
public static int skipContentsUntil(String args, int startIndex, int len, String keyword, boolean caseSensitive)
Skip quoted string, comments, and brackets until seeing thekeyword
or reaching end of the given string.- Parameters:
args
- non-null string to scanstartIndex
- start indexlen
- end index, usually length of the given stringkeyword
- keyword, null or empty string means any charactercaseSensitive
- whether keyword is case sensitive or not- Returns:
- index of
endChar
orlen
-
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 givenkeywords
(with only whitespaces or comments in between) or reaching end of the given string.- Parameters:
args
- non-null string to scanstartIndex
- start indexlen
- end index, usually length of the given stringkeywords
- keywords, null or empty one means any charactercaseSensitive
- whether keyword is case sensitive or not- Returns:
- index of
endChar
orlen
-
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 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)
-
-