Record Class QueryTemplateWithParameters

java.lang.Object
java.lang.Record
de.calamanari.adl.sql.QueryTemplateWithParameters
Record Components:
qmTemplate - template with positional parameters
orderedParameters - parameters in order of appearance in the template
qmPositions - parameter question mark positions in order of appearance in the template
All Implemented Interfaces:
Serializable

public record QueryTemplateWithParameters(String qmTemplate, List<QueryParameter> orderedParameters, List<Integer> qmPositions) extends Record implements Serializable
An QueryTemplateWithParameters combines an SQL-script with positional parameters (question marks) with the parameters to be set once the related PreparedStatement has been created.
Author:
Karl Eilebrecht
See Also:
  • Constructor Details

    • QueryTemplateWithParameters

      public QueryTemplateWithParameters(String qmTemplate, List<QueryParameter> orderedParameters, List<Integer> qmPositions)
      In general you should use of(String, List) instead of directly calling this constructor
      Parameters:
      qmTemplate - template with positional parameters
      orderedParameters - parameters in order of appearance in the template
      qmPositions - parameter question mark positions in order of appearance in the template
  • Method Details

    • of

      public static QueryTemplateWithParameters of(String queryTemplate, List<QueryParameter> parameters)
      Takes a template with named parameters (${id} references) and a list of parameters to produce a query template with positional parameters to be executed later as a PreparedStatement after calling apply(PreparedStatement) to set the parameters safely.
      Parameters:
      queryTemplate - template with named parameters (name=id)
      parameters - available parameters, can be a superset of the parameters required by the template
      Returns:
      template with related parameters
    • createPreparedStatement

      public PreparedStatement createPreparedStatement(Connection conn) throws SQLException
      Convenience method to create a PreparedStatement and apply all parameters
      Parameters:
      conn -
      Returns:
      PreparedStatement with all parameters from this template applied, ready to be executed
      Throws:
      SQLException
    • apply

      public void apply(PreparedStatement stmt) throws SQLException
      Applies the parameters to the PreparedStatement created from the qmTemplate().
      Parameters:
      stmt -
      Throws:
      SQLException
    • applyUnsafe

      public void applyUnsafe(StringBuilder sb)
      This method returns the SQL-string with the positional parameters replaced with string arguments. It relies on the formatters configured for the AdlSqlTypes.

      Warning! This method is in general NOT SUITABLE FOR EXECUTING SQLs because (even with escaping!) we cannot harden this against SQL-injection. It is impossible to predict the behavior of all possible driver/db combinations. Only PreparedStatements can guarantee sufficient safety.

      Throws:
      de.calamanari.adl.cnv.tps.AdlFormattingException - in case of errors
      See Also:
    • toDebugString

      public String toDebugString()
      This method is fail-safe and falls back to the regular toString()-method if anything goes wrong.
      In this case the returned String starts with an error indicator.
      Returns:
      returns a (usually functional) version of the statement, for debugging/logging purposes
    • toString

      public final String toString()
      Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. All components in this record class are compared with Objects::equals(Object,Object).
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • qmTemplate

      public String qmTemplate()
      Returns the value of the qmTemplate record component.
      Returns:
      the value of the qmTemplate record component
    • orderedParameters

      public List<QueryParameter> orderedParameters()
      Returns the value of the orderedParameters record component.
      Returns:
      the value of the orderedParameters record component
    • qmPositions

      public List<Integer> qmPositions()
      Returns the value of the qmPositions record component.
      Returns:
      the value of the qmPositions record component