Friday, July 15, 2011

Named Query Parameters

The Rdbhost protocol now uses named parameters in addition to positional parameters.

Where before you could submit a query like:

<input name="q"
value="SELECT * FROM personnel WHERE firstName = %s AND lastName = %s" />
<input name="arg000" value="John" />
<input name="arg001" value="Smith" />

Now you can make it more readable, like:

<input name="q" value="SELECT * FROM personnel WHERE firstName = %(first) AND lastName = %(last)" />
<input name="arg:first" value="John" />
<input name="arg:last" value="Smith" />

This addition has a couple of benefits. The first is the greater readability and greater ease in assuring the correct parameter values are matched to the correct query tokens.

The other is that each parameter can be used in the query multiple times. For example, in the following snippet, we want to ensure that the value is at least 0.

CASE WHEN (%(val))<=0 THEN trunc(%(val),6) ELSE 0.00 END


The query snippet could be written with positional parameters, but there would then be no way to ensure the same value is passed as both values, and a negative value could be returned. Using a named parameter ensures that parameter value is used both times.

No comments:

Post a Comment