-
Notifications
You must be signed in to change notification settings - Fork 355
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
DATAJDBC-1953 Introduced DialectCriteriaCondition interface #1981
Open
mipo256
wants to merge
1
commit into
spring-projects:main
Choose a base branch
from
mipo256:DATAJDBC-1953
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
.../org/springframework/data/relational/core/dialect/condition/DialectCriteriaCondition.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package org.springframework.data.relational.core.dialect.condition; | ||
|
||
import org.springframework.data.relational.core.query.Criteria; | ||
|
||
/** | ||
* This interface represents dialect specific conditions used in WHERE causes built by {@link Criteria}. | ||
* | ||
* @author Mikhail Polivakha | ||
*/ | ||
public interface DialectCriteriaCondition { | ||
|
||
/** | ||
* Render a vendor-specific part of the SQL condition. | ||
* | ||
* @return the rendered part of the SQL statement | ||
*/ | ||
String render(); | ||
} |
67 changes: 67 additions & 0 deletions
67
...al/src/main/java/org/springframework/data/relational/core/dialect/condition/Postgres.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package org.springframework.data.relational.core.dialect.condition; | ||
|
||
import org.springframework.data.relational.core.dialect.PostgresDialect; | ||
|
||
/** | ||
* {@link DialectCriteriaCondition DialectCriteriaConditions} that are specific to {@link PostgresDialect PostgreSQL Dialect} | ||
* | ||
* @author Mikhail Polivakha | ||
*/ | ||
public class Postgres { | ||
|
||
/** | ||
* Creates a condition that checks that the assumed column of an {@link java.sql.Array} type | ||
* contains an array of any values | ||
* | ||
* @param values array that assumed column should contain | ||
* @return crafted {@link DialectCriteriaCondition} | ||
*/ | ||
public static DialectCriteriaCondition arrayContains(Object... values) { | ||
return () -> "@> ARRAY[%s]".formatted(toLiterals(false, values)); | ||
} | ||
|
||
/** | ||
* Creates a condition that checks that the assumed column of an {@link java.sql.Array} type | ||
* contains an array of {@link String} values. | ||
* | ||
* @param values array of {@link String String} that assumed column should contain | ||
* @return crafted {@link DialectCriteriaCondition} | ||
*/ | ||
public static DialectCriteriaCondition arrayContains(String... values) { | ||
return () -> "@> ARRAY[%s]::text[]".formatted(toLiterals(true, values)); | ||
} | ||
|
||
/** | ||
* Creates a condition that checks that the assumed column of an {@link java.sql.Array} type | ||
* contains an array of a single {@link String} value. | ||
* | ||
* @param value array of {@link String String} that assumed value should contain | ||
* @return crafted {@link DialectCriteriaCondition} | ||
*/ | ||
public static DialectCriteriaCondition arrayContains(String value) { | ||
return arrayContains(new String[]{value}); | ||
} | ||
|
||
@SafeVarargs | ||
private static <T> String toLiterals(boolean quoted, T... values) { | ||
StringBuilder result = new StringBuilder(); | ||
for (int i = 0; i < values.length; i++) { | ||
T value = values[i]; | ||
|
||
if (value != null) { | ||
if (quoted) { | ||
result.append('\'').append(value).append('\''); | ||
} else { | ||
result.append(value); | ||
} | ||
} else { | ||
result.append("NULL"); | ||
} | ||
|
||
if (i != values.length - 1) { | ||
result.append(","); | ||
} | ||
} | ||
return result.toString(); | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a bit too little, especially given the later variant of concatenating values into SQL as it opens loopholes for SQL injection.
An operator should have a method accepting a
MappingContext
and likely aPersistentProperty
that applies to it and an abstraction like R2DBC'sBindMarkers
. In return, we need a tuple of bind markers associated with their value and the SQL fragment. Maybe even a slimmer variant ofBindTarget
(without the binding identifier) would suit fit.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mp911de Am I correct that the desired interface signature should look like this:
If so, then I have a couple of questions here:
MappingContext
,PersistentProperty
an other abstraction? They arepublic
, but it does not seem to be desirable to expose them to users.PersistentProperty
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Combinator
andComparator
enums are closed types that do not allow for extensions. In return, Spring Data Relational controls what enum values are available to ensure we only specify safe operators. At some point, we have to translate criteria elements into a String. Specifically referring to the linked method, rendering of criteria elements inside ofCriteria
is limited totoString()
usage. The actual mapping ofCriteriaDefinition
toCondition
(the SQL DSL) happens inQueryMapper
.MappingContext
towards users. Ideally, we have an API that allows building comparators/query elements without leading users into Spring Data's Mapping infrastructure. Something likePostgres.arrayContains("first", "second")
where the resulting value is used inCriteria
so that users don't have to interact with methods exposed by the result object ofPostgres.arrayContains(…)
. We have a similar arrangement in MongoDB withAggregationOperation
.AggregationOperationContext
handles some abstraction for mapping and I think we might end up doing something similar.This also crossed my mind. There could be variants where two functions compare against each other or the property is specified on the right side. So far, we have to consider this in some way. Let's however start from a simpler side first so that we can implement a first proof of concept before digging into the more complex things. It is typically easier to build something (starting from the side how things should be expressed in code) and then we can iterate on it towards a variant that handles an increasing number of usecases.