-
Notifications
You must be signed in to change notification settings - Fork 351
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DATAJDBC-1953 Introduced DialectCriteriaCondition
Signed-off-by: mipo256 <[email protected]>
- Loading branch information
Showing
7 changed files
with
225 additions
and
35 deletions.
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.