Named parameters instead of numbered parameters
Arthur McGibbon
arthur.mcgibbon at gmail.com
Thu Aug 8 12:03:05 UTC 2019
Does ABDA allow named parameters instead of numbered parameters in its
equivalent of JDBC's PreparedStatement? Looking on the GitHub repository
example it looks like it handles parameters the same way as JDBC does and I
was hoping this could be changed.
The idea would be that instead of calling preparedStatement.setInt(1, 42)
the call would be preparedStatement.setInt("paramName", 42)
Then the SQL would be something like...
"select * from tableName where colValue = :paramName"
rather than
"select * from tableName where colValue = ?"
The reason is that some queries simply cannot be prepared using unnamed
parameters because it's not possible to repeat the use of a parameter.
A contrived example of this (in SQL Server TSQL but it applies to other
DBs)...
"select case when a.someData > ? then 0 else a.someData end,
sum(a.someOtherData)
from (select 1 as someData, 2 as someOtherData) a
group by case when a.someData > ? then 0 else a.someData end"
This cannot be prepared because the group by section is not the same as the
select section even though I'm wanting to always use the same values for
both parameters.
The following version using named parameters could be prepared as there is
only 1 parameter which is then used twice...
"select case when a.someData > :param1 then 0 else a.someData end,
sum(a.someOtherData)
from (select 1 as someData, 2 as someOtherData) a
group by case when a.someData > :param1 then 0 else a.someData end"
I realise that the above SQL could be re-written in various ways to cope
with this but I feel that ideally the SQL wouldn't have to be changed
because of the database driver design.
More information about the jdbc-spec-discuss
mailing list