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: no and name. Moreover, type of the last parameter is String.

See Also:
  • Field Details

  • Constructor Details

    • ClickHouseParameterizedQuery

      protected ClickHouseParameterizedQuery(ClickHouseConfig config, String query)
      Default constructor.
      Parameters:
      config - non-null config
      query - non-blank query
  • Method Details

    • apply

      public static String apply(String sql, Map<String,String> params)
      Substitute named parameters in given SQL.
      Parameters:
      sql - SQL containing named parameters
      params - mapping between parameter name and correspoding SQL expression(NOT raw value)
      Returns:
      substituted SQL, or the given sql if one of sql and params is 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 builder
      sql - SQL containing named parameters
      params - 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 config
      query - 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 parameter
      paramIndex - zero-based index of the parameter
      paramType - 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 parameter
      paramIndex - zero-based index of the parameter
      paramName - name of the parameter, null means String.valueOf(paramIndex)
      paramType - type of the parameter, could be null
    • 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 parameter
      value - 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 builder
      params - stringified parameters
    • apply

      public void apply(StringBuilder builder, Collection<String> params)
      Applies stringified parameters to the given string builder.
      Parameters:
      builder - non-null string builder
      params - 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 builder
      param - raw parameter
      more - 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 builder
      values - 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 builder
      param - stringified parameter
      more - 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 builder
      values - 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 from getOriginalQuery(), 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
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object