Package com.clickhouse.client
Class ClickHouseParameterizedQuery
- java.lang.Object
-
- com.clickhouse.client.ClickHouseParameterizedQuery
-
- All Implemented Interfaces:
Serializable
public class ClickHouseParameterizedQuery extends Object implements Serializable
A parameterized query is a parsed query with parameters being extracted for substitution.Here parameter is define in the format of
:<name>[(<type>)]. It starts with colon, immediately followed by name, and then optionally type within brackets. For example: in query "select :no as no, :name(String) as name", we have two parameters:noandname. Moreover, type of the last parameter isString.- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static classClickHouseParameterizedQuery.QueryPartA part of query.
-
Field Summary
Fields Modifier and Type Field Description protected ClickHouseConfigconfigprotected StringoriginalQuery
-
Constructor Summary
Constructors Modifier Constructor Description protectedClickHouseParameterizedQuery(ClickHouseConfig config, String query)Default constructor.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voidaddPart(String part, int paramIndex, String paramType)Adds part of query and the following parameter.protected voidaddPart(String part, int paramIndex, String paramName, String paramType)Adds part of query and the following parameter.protected StringBuilderappendLastPartIfExists(StringBuilder builder)Appends last part of the query if it exists.voidapply(StringBuilder builder, Object[] values)Applies raw parameters to the given string builder.voidapply(StringBuilder builder, Object param, Object... more)Applies raw parameters to the given string builder.voidapply(StringBuilder builder, String[] values)Applies stringified parameters to the given string builder.voidapply(StringBuilder builder, String param, String... more)Applies stringified parameters to the given string builder.static voidapply(StringBuilder builder, String sql, Map<String,String> params)Substitute named parameters in given SQL.voidapply(StringBuilder builder, Collection<String> params)Applies stringified parameters to the given string builder.voidapply(StringBuilder builder, Map<String,String> params)Applies stringified parameters to the given string builder.static Stringapply(String sql, Map<String,String> params)Substitute named parameters in given SQL.booleanequals(Object obj)StringgetOriginalQuery()Gets original query.List<String>getParameters()Gets named parameters.ClickHouseValue[]getParameterTemplates()Gets parameter templates for converting value to SQL expression.protected List<ClickHouseParameterizedQuery.QueryPart>getParts()Gets immutable list of query parts.List<String[]>getQueryParts()Gets query parts.inthashCode()booleanhasParameter()Checks if the query has at least one parameter or not.static ClickHouseParameterizedQueryof(ClickHouseConfig config, String query)Creates an instance by parsing the given query.protected Stringparse()Parses the query given in constructor.protected StringtoSqlExpression(String paramName, Object value)Converts given raw value to SQL expression.
-
-
-
Field Detail
-
config
protected final ClickHouseConfig config
-
originalQuery
protected final String originalQuery
-
-
Constructor Detail
-
ClickHouseParameterizedQuery
protected ClickHouseParameterizedQuery(ClickHouseConfig config, String query)
Default constructor.- Parameters:
config- non-null configquery- non-blank query
-
-
Method Detail
-
apply
public static String apply(String sql, Map<String,String> params)
Substitute named parameters in given SQL.- Parameters:
sql- SQL containing named parametersparams- mapping between parameter name and correspoding SQL expression(NOT raw value)- Returns:
- substituted SQL, or the given sql if one of
sqlandparamsis null or empty
-
apply
public static void apply(StringBuilder builder, String sql, Map<String,String> params)
Substitute named parameters in given SQL.- Parameters:
builder- non-null string buildersql- SQL containing named parametersparams- mapping between parameter name and correspoding SQL expression(NOT raw value)
-
of
public static ClickHouseParameterizedQuery of(ClickHouseConfig config, String query)
Creates an instance by parsing the given query.- Parameters:
config- non-null configquery- non-empty SQL query- Returns:
- parameterized query
-
addPart
protected void addPart(String part, int paramIndex, String paramType)
Adds part of query and the following parameter.- Parameters:
part- part of the query, between previous and current parameterparamIndex- zero-based index of the parameterparamType- type of the parameter, could be null
-
addPart
protected void addPart(String part, int paramIndex, String paramName, String paramType)
Adds part of query and the following parameter.- Parameters:
part- part of the query, between previous and current parameterparamIndex- zero-based index of the parameterparamName- name of the parameter, null meansString.valueOf(paramIndex)paramType- type of the parameter, could be null
-
getParts
protected List<ClickHouseParameterizedQuery.QueryPart> getParts()
Gets immutable list of query parts.- Returns:
- immutable list of query parts
-
parse
protected String parse()
Parses the query given in constructor.- Returns:
- remaining part(right after the last parameter) after parsing, could be null
-
appendLastPartIfExists
protected StringBuilder appendLastPartIfExists(StringBuilder builder)
Appends last part of the query if it exists.- Parameters:
builder- non-null string builder- Returns:
- the builder
-
toSqlExpression
protected String toSqlExpression(String paramName, Object value)
Converts given raw value to SQL expression.- Parameters:
paramName- name of the parametervalue- raw value, could be null- Returns:
- non-null SQL expression
-
apply
public void apply(StringBuilder builder, Map<String,String> params)
Applies stringified parameters to the given string builder.- Parameters:
builder- non-null string builderparams- stringified parameters
-
apply
public void apply(StringBuilder builder, Collection<String> params)
Applies stringified parameters to the given string builder.- Parameters:
builder- non-null string builderparams- stringified parameters
-
apply
public void apply(StringBuilder builder, Object param, Object... more)
Applies raw parameters to the given string builder.ClickHouseValues.convertToSqlExpression(Object)will be used to stringify the parameters.- Parameters:
builder- non-null string builderparam- raw parametermore- more raw parameters if any
-
apply
public void apply(StringBuilder builder, Object[] values)
Applies raw parameters to the given string builder.ClickHouseValues.convertToSqlExpression(Object)will be used to stringify the parameters.- Parameters:
builder- non-null string buildervalues- raw parameters
-
apply
public void apply(StringBuilder builder, String param, String... more)
Applies stringified parameters to the given string builder.- Parameters:
builder- non-null string builderparam- stringified parametermore- more stringified parameters if any
-
apply
public void apply(StringBuilder builder, String[] values)
Applies stringified parameters to the given string builder.- Parameters:
builder- non-null string buildervalues- stringified parameters
-
getParameters
public List<String> getParameters()
Gets named parameters.- Returns:
- list of named parameters
-
getOriginalQuery
public String getOriginalQuery()
Gets original query.- Returns:
- original query
-
getQueryParts
public List<String[]> getQueryParts()
Gets query parts. Each part is composed of a snippet taken fromgetOriginalQuery(), followed by a parameter name, which might be null.- Returns:
- query parts
-
getParameterTemplates
public ClickHouseValue[] getParameterTemplates()
Gets parameter templates for converting value to SQL expression.- Returns:
- parameter templates
-
hasParameter
public boolean hasParameter()
Checks if the query has at least one parameter or not.- Returns:
- true if there's at least one parameter; false otherwise
-
-