-
Notifications
You must be signed in to change notification settings - Fork 359
Implemented Postgres-specific array Criteria operations #1981
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
base: main
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,7 @@ | |
* | ||
* @author Mark Paluch | ||
* @author Jens Schauder | ||
* @author Mikhail Polivakha | ||
* @since 2.0 | ||
*/ | ||
public interface CriteriaDefinition { | ||
|
@@ -140,7 +141,7 @@ enum Combinator { | |
enum Comparator { | ||
INITIAL(""), EQ("="), NEQ("!="), BETWEEN("BETWEEN"), NOT_BETWEEN("NOT BETWEEN"), LT("<"), LTE("<="), GT(">"), GTE( | ||
">="), IS_NULL("IS NULL"), IS_NOT_NULL("IS NOT NULL"), LIKE( | ||
"LIKE"), NOT_LIKE("NOT LIKE"), NOT_IN("NOT IN"), IN("IN"), IS_TRUE("IS TRUE"), IS_FALSE("IS FALSE"); | ||
"LIKE"), NOT_LIKE("NOT LIKE"), NOT_IN("NOT IN"), IN("IN"), IS_TRUE("IS TRUE"), IS_FALSE("IS FALSE"), ARRAY_CONTAINS("@>"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's keep the comparator enum as-is. It's closed for extension. Any comparators should come really from inside of the extension. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I have thought about it too. I'll thinkg about the way to solve it, I'll brb. |
||
|
||
private final String comparator; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* Copyright 2020-2025 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.data.relational.core.query; | ||
|
||
/** | ||
* An Ongoing Criteria builder for {@link java.sql.Types#ARRAY SQL Array} related operations. | ||
* Used by intermediate builder objects returned from the {@link Criteria}. | ||
* | ||
* @author Mikhail Polivakha | ||
*/ | ||
public interface OngoingArrayCriteria { | ||
|
||
/** | ||
* Builds a {@link Criteria} where the pre-defined array must contain given values. | ||
* | ||
* @param values values to be present in the array | ||
* @return built {@link Criteria} | ||
*/ | ||
Criteria contains(Object... values); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/* | ||
* Copyright 2020-2025 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.data.relational.core.query; | ||
|
||
import static org.springframework.data.relational.core.query.Criteria.*; | ||
|
||
import java.sql.JDBCType; | ||
import java.util.StringJoiner; | ||
|
||
import org.jetbrains.annotations.NotNull; | ||
import org.springframework.core.ResolvableType; | ||
import org.springframework.data.relational.core.sql.SqlIdentifier; | ||
import org.springframework.data.util.TypeInformation; | ||
import org.springframework.util.Assert; | ||
|
||
/** | ||
* PostgreSQL-specific {@link Criteria} conditions. | ||
* | ||
* @author Mikhail Polivakha | ||
*/ | ||
public class Postgres { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the way to go, with fluent API and something that guides towards the final operator. We have a similar setup in MongoDB's aggregation operators. |
||
|
||
/** | ||
* Custom {@link Criteria} condition builder to check if the column of an {@link java.sql.Types#ARRAY ARRAY} sql type | ||
* matches specific conditions. Samples of usage is: | ||
* <p> | ||
* <pre class="code"> | ||
* // Code below produces the SQL: "my_column" @> ARRAY['A', 'B'] | ||
* Postgres.whereArray("my_column").contains("A", "B") | ||
* </pre> | ||
* Code above produces the SQL: | ||
* <pre class="code"> | ||
* "my_column" @> ARRAY['A', 'B'] | ||
* </pre> | ||
* | ||
* @param arrayName the name of an ARRAY column to match against | ||
* @return the {@link OngoingArrayCriteria} to chain future condition | ||
*/ | ||
public static OngoingArrayCriteria array(String arrayName) { | ||
return new PostgresCriteriaArray(arrayName); | ||
} | ||
|
||
public static class PostgresCriteriaArray implements OngoingArrayCriteria { | ||
|
||
private final String arrayColumnName; | ||
|
||
public PostgresCriteriaArray(String arrayColumnName) { | ||
this.arrayColumnName = arrayColumnName; | ||
} | ||
|
||
@NotNull | ||
@Override | ||
public Criteria contains(Object... values) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have not a good feeling yet for the return type. I need to explore this aspect in much more depth. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay, but as was stated here:
Maybe we can just try this solution and do not introduce another abstractions? I absolutely share your concern, @mp911de, because the pattern:
is not always the case. So, I do not know... Maybe we can indeed leave it for now. |
||
Assert.notNull(values, "values array cannot be null"); | ||
|
||
return new Criteria(SqlIdentifier.quoted(arrayColumnName), CriteriaDefinition.Comparator.ARRAY_CONTAINS, new CriteriaLiteral() { | ||
|
||
@Override | ||
public String getLiteral() { | ||
boolean quoted = true; | ||
|
||
if (values.length > 0) { | ||
quoted = !Number.class.isAssignableFrom(values[0].getClass()); | ||
} | ||
|
||
return toArrayLiteral(quoted, values); | ||
} | ||
}); | ||
} | ||
|
||
@SafeVarargs | ||
public final <T> String toArrayLiteral(boolean quoted, T... values) { | ||
StringJoiner accumulator = new StringJoiner(",", "ARRAY[", "]"); | ||
|
||
for (T value : values) { | ||
if (value != null) { | ||
if (quoted) { | ||
accumulator.add("'" + value + "'"); | ||
} else { | ||
accumulator.add(value.toString()); | ||
} | ||
} else { | ||
accumulator.add(JDBCType.NULL.name()); | ||
} | ||
} | ||
return accumulator.toString(); | ||
} | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.