You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Title: Enhance Helidon DB Client's Handling of Named Parameters to Align with Map Interface Contract
Description:
The Helidon DB Client's treatment of named parameters needs enhancement to align more closely with the Map interface contract regarding null handling. While immutable maps (e.g., created with Map.of()) cannot store null values or keys, they, like all map implementations, return null for non-existent keys. The current handling within the DB Client does not fully leverage this aspect of map behavior, especially concerning the flexibility offered by mutable map implementations such as HashMap.
Issue Detail:
The current implementation for preparing statements with named parameters does not distinguish between the absence of a key (which should result in a null value being used) and the map's capability to store null values. This oversight restricts the use of optional parameters in SQL queries, especially when utilizing maps that support null values.
Example Illustration:
A custom LocationMapper demonstrates the need for improved handling:
publicclassLocationMapperimplementsDbMapper<Location> {
...
@OverridepublicMap<String, ?> toNamedParameters(Locationvalue) {
Map<String, Object> params = newHashMap<>();
// Expected to leverage HashMap's ability to handle null values for missing keysreturnparams;
}
}
publicList<Location> getLocations(Locationlocation) {
Stringquery = """ SELECT * FROM location l WHERE (coalesce(:name, l.name) = l.name) """;
returnthis.dbClient.execute()
.createQuery(query)
.namedParam(location)
.execute()
.map(dbRow -> dbRow.as(Location.class))
.toList();
}
Current Implementation Concern:
privatePreparedStatementprepareNamedStatement(StringstmtName, Stringstmt, Map<String, Object> parameters) {
...
for (Stringname : namesOrder) {
if (!parameters.containsKey(name)) {
// This behavior is restrictive and does not fully utilize the Map interface's contract.thrownewDbClientException(namedStatementErrorMessage(namesOrder, parameters));
}
...
}
...
}
Proposed Enhancement:
Revise the DB Client to utilize null for absent keys in parameter maps, respecting the Map interface's contract. This change should apply universally, acknowledging that null is a valid return value for absent keys, while also respecting the immutability constraints of certain map implementations:
For all maps: Treat absent keys as null, leveraging SQL's handling of null for optional parameters.
For mutable maps (e.g., HashMap): Continue to allow the explicit setting of null values.
For immutable maps (e.g., Map.of()): No change needed as these maps inherently comply by not storing null but still returning null for non-existent keys.
Conclusion:
By refining the named parameter handling to universally treat absent keys as null, the Helidon DB Client will better align with the Java Map interface contract, enhancing its flexibility and usability across a wider range of use cases. This adjustment ensures a more consistent and predictable developer experience when constructing dynamic SQL queries with optional parameters.
The text was updated successfully, but these errors were encountered:
Environment Details
Title: Enhance Helidon DB Client's Handling of Named Parameters to Align with Map Interface Contract
Description:
The Helidon DB Client's treatment of named parameters needs enhancement to align more closely with the
Map
interface contract regarding null handling. While immutable maps (e.g., created withMap.of()
) cannot storenull
values or keys, they, like all map implementations, returnnull
for non-existent keys. The current handling within the DB Client does not fully leverage this aspect of map behavior, especially concerning the flexibility offered by mutable map implementations such asHashMap
.Issue Detail:
The current implementation for preparing statements with named parameters does not distinguish between the absence of a key (which should result in a
null
value being used) and the map's capability to storenull
values. This oversight restricts the use of optional parameters in SQL queries, especially when utilizing maps that supportnull
values.Example Illustration:
A custom
LocationMapper
demonstrates the need for improved handling:Current Implementation Concern:
Proposed Enhancement:
Revise the DB Client to utilize
null
for absent keys in parameter maps, respecting theMap
interface's contract. This change should apply universally, acknowledging thatnull
is a valid return value for absent keys, while also respecting the immutability constraints of certain map implementations:null
, leveraging SQL's handling ofnull
for optional parameters.HashMap
): Continue to allow the explicit setting ofnull
values.Map.of()
): No change needed as these maps inherently comply by not storingnull
but still returningnull
for non-existent keys.Conclusion:
By refining the named parameter handling to universally treat absent keys as
null
, the Helidon DB Client will better align with the JavaMap
interface contract, enhancing its flexibility and usability across a wider range of use cases. This adjustment ensures a more consistent and predictable developer experience when constructing dynamic SQL queries with optional parameters.The text was updated successfully, but these errors were encountered: