Interface QueryParameter
- All Superinterfaces:
Serializable
- All Known Implementing Classes:
DefaultQueryParameter
QueryParameter encapsulates the action to correctly set a parameter on a prepared statement.
The contained value is already pre-validated, so this class timely decouples the moment when we align types and convert values from the moment where we actually set the parameters on a statement.
Each QueryParameter has a unique id(). This was introduced to keep the parameters of the PreparedStatement independent from the
parameter order. The issue with relying on the order is that it is difficult to guarantee strict left-to-right production of parameters while building
a statement.
And even worse: if you later put together larger text blocks to form the final statement, it may be impossible to keep the connection between the position of
the question marks and the actual parameter to set. Thus, in an intermediate step we include parameter id-references ${id} in the
expression's SQL-text.
Eventually (once all parameters are known), these temporary placeholders will be replaced with question marks to set the parameters safely on the the
resulting PreparedStatement.
- Author:
- Karl Eilebrecht
-
Method Summary
Modifier and TypeMethodDescriptiondefault voidapply(PreparedStatement stmt, int parameterIndex) Sets the value of this parameter on the statement at the given index.default voidapplyUnsafe(StringBuilder sb, int parameterIndex) Appends the value of the given parameter to an SQL script.default Stringid()de.calamanari.adl.irl.MatchOperatoroperator()Returns the operator currently being translated.value()
-
Method Details
-
id
String id()- Returns:
- technical identifier for this parameter, must not be blank or contain any whitespace or curly braces or dollar signs (see
Character.isLetterOrDigit(char)), no whitespace
-
createReference
- Returns:
- temporary placeholder to be used in an SQL-expression template:
${id}, seeQueryParameter
-
adlSqlType
AdlSqlType adlSqlType()- Returns:
- a description of the target column's type
-
value
Serializable value()- Returns:
- the value to be set on a prepared statement, concrete type depends on
QueryParameterCreatorand requires a matchingQueryParameterApplicator
-
operator
de.calamanari.adl.irl.MatchOperator operator()Returns the operator currently being translated. This contextual information may influence the formatter in case ofapplyUnsafe(StringBuilder, int).- Returns:
- the operator this parameter is used with, by default
MatchOperator.EQUALS, not null
-
apply
Sets the value of this parameter on the statement at the given index.This is the preferred (safe) methods to process the parameters of an SQL-statement without the risk of SQL-injection.
- Parameters:
stmt-parameterIndex-- Throws:
SQLException- if the set operation on the prepared statement failed
-
applyUnsafe
Appends the value of the given parameter to an SQL script.Warning! As the name of this method states, using this method to compose plain SQL-queries is inherently unsafe and highly discouraged.
Please also read:
QueryParameterApplicator.applyUnsafe(StringBuilder, QueryParameter, int)- Parameters:
sb- to append the parameter valueparameterIndex-- Throws:
de.calamanari.adl.cnv.tps.AdlFormattingException- if the formatting failed
-