-
Notifications
You must be signed in to change notification settings - Fork 247
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
29 changed files
with
477 additions
and
142 deletions.
There are no files selected for viewing
43 changes: 43 additions & 0 deletions
43
...lib/sql-lib/src/main/java/org/eclipse/edc/sql/translation/EntityStateFieldTranslator.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,43 @@ | ||
/* | ||
* Copyright (c) 2025 Cofinity-X | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Apache License, Version 2.0 which is available at | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Contributors: | ||
* Cofinity-X - initial API and implementation | ||
* | ||
*/ | ||
|
||
package org.eclipse.edc.sql.translation; | ||
|
||
import org.eclipse.edc.spi.entity.StateResolver; | ||
import org.eclipse.edc.spi.query.Criterion; | ||
|
||
import java.util.Collection; | ||
|
||
import static java.util.stream.Collectors.toList; | ||
|
||
/** | ||
* Supports the string representation of a state, that will be converted into int by the {@link #stateResolver}. | ||
*/ | ||
public class EntityStateFieldTranslator extends PlainColumnFieldTranslator { | ||
|
||
private final StateResolver stateResolver; | ||
|
||
public EntityStateFieldTranslator(String columnName, StateResolver stateResolver) { | ||
super(columnName); | ||
this.stateResolver = stateResolver; | ||
} | ||
|
||
@Override | ||
public Collection<Object> toParameters(Criterion criterion) { | ||
return super.toParameters(criterion) | ||
.stream() | ||
.map(it -> it instanceof String stringState ? stateResolver.resolve(stringState) : it) | ||
.collect(toList()); | ||
} | ||
} |
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
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
85 changes: 85 additions & 0 deletions
85
...sql-lib/src/test/java/org/eclipse/edc/sql/translation/EntityStateFieldTranslatorTest.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,85 @@ | ||
/* | ||
* Copyright (c) 2025 Cofinity-X | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Apache License, Version 2.0 which is available at | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Contributors: | ||
* Cofinity-X - initial API and implementation | ||
* | ||
*/ | ||
|
||
package org.eclipse.edc.sql.translation; | ||
|
||
import org.eclipse.edc.spi.entity.StateResolver; | ||
import org.eclipse.edc.util.reflection.PathItem; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.List; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.eclipse.edc.spi.query.Criterion.criterion; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
class EntityStateFieldTranslatorTest { | ||
|
||
private final StateResolver stateResolver = mock(); | ||
private final EntityStateFieldTranslator translator = new EntityStateFieldTranslator("column_name", stateResolver); | ||
|
||
@Test | ||
void shouldReturnWhereClause_whenOperatorSpecified() { | ||
var path = PathItem.parse("any"); | ||
|
||
var result = translator.toWhereClause(path, criterion("field", "=", 100), createOperator()); | ||
|
||
assertThat(result.sql()).isEqualTo("column_name any ?"); | ||
assertThat(result.parameters()).containsExactly(100); | ||
} | ||
|
||
@Test | ||
void shouldMapMultipleParameters_whenRightOperandIsCollection() { | ||
var path = PathItem.parse("any"); | ||
|
||
var result = translator.toWhereClause(path, criterion("field", "in", List.of(100, 200)), createOperator()); | ||
|
||
assertThat(result.sql()).isEqualTo("column_name any (?,?)"); | ||
assertThat(result.parameters()).containsExactly(100, 200); | ||
} | ||
|
||
@Test | ||
void shouldTranslateStateToCode_whenInputTypeIsString() { | ||
when(stateResolver.resolve(any())).thenReturn(100); | ||
var path = PathItem.parse("any"); | ||
|
||
var result = translator.toWhereClause(path, criterion("field", "=", "STATE"), createOperator()); | ||
|
||
assertThat(result.sql()).isEqualTo("column_name any ?"); | ||
assertThat(result.parameters()).containsExactly(100); | ||
verify(stateResolver).resolve("STATE"); | ||
} | ||
|
||
@Test | ||
void shouldMapMultipleParametersToCode_whenRightOperandIsStringCollection() { | ||
when(stateResolver.resolve(any())).thenReturn(100).thenReturn(200); | ||
var path = PathItem.parse("any"); | ||
|
||
var result = translator.toWhereClause(path, criterion("field", "in", List.of("STATE", "ANOTHER_STATE")), createOperator()); | ||
|
||
assertThat(result.sql()).isEqualTo("column_name any (?,?)"); | ||
assertThat(result.parameters()).containsExactly(100, 200); | ||
verify(stateResolver).resolve("STATE"); | ||
verify(stateResolver).resolve("ANOTHER_STATE"); | ||
} | ||
|
||
@NotNull | ||
private static SqlOperator createOperator() { | ||
return new SqlOperator("any", Object.class); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
...mon/lib/store-lib/src/main/java/org/eclipse/edc/store/AndOperatorCriteriaToPredicate.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,44 @@ | ||
/* | ||
* Copyright (c) 2025 Cofinity-X | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Apache License, Version 2.0 which is available at | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Contributors: | ||
* Cofinity-X - initial API and implementation | ||
* | ||
*/ | ||
|
||
package org.eclipse.edc.store; | ||
|
||
import org.eclipse.edc.spi.query.CriteriaToPredicate; | ||
import org.eclipse.edc.spi.query.Criterion; | ||
import org.eclipse.edc.spi.query.CriterionOperatorRegistry; | ||
|
||
import java.util.List; | ||
import java.util.function.Predicate; | ||
|
||
/** | ||
* Converts criteria to a Predicate that converts all the criterion to predicate using the passed {@link #criterionOperatorRegistry} | ||
* and joins them with "and" operator. | ||
* | ||
* @param <T> the object type | ||
*/ | ||
public class AndOperatorCriteriaToPredicate<T> implements CriteriaToPredicate<T> { | ||
|
||
private final CriterionOperatorRegistry criterionOperatorRegistry; | ||
|
||
public AndOperatorCriteriaToPredicate(CriterionOperatorRegistry criterionOperatorRegistry) { | ||
this.criterionOperatorRegistry = criterionOperatorRegistry; | ||
} | ||
|
||
@Override | ||
public Predicate<T> convert(List<Criterion> criteria) { | ||
return criteria.stream() | ||
.map(criterionOperatorRegistry::<T>toPredicate) | ||
.reduce(x -> true, Predicate::and); | ||
} | ||
} |
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
55 changes: 55 additions & 0 deletions
55
.../lib/store-lib/src/main/java/org/eclipse/edc/store/StatefulEntityCriteriaToPredicate.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,55 @@ | ||
/* | ||
* Copyright (c) 2025 Cofinity-X | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Apache License, Version 2.0 which is available at | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Contributors: | ||
* Cofinity-X - initial API and implementation | ||
* | ||
*/ | ||
|
||
package org.eclipse.edc.store; | ||
|
||
import org.eclipse.edc.spi.entity.StateResolver; | ||
import org.eclipse.edc.spi.entity.StatefulEntity; | ||
import org.eclipse.edc.spi.query.CriteriaToPredicate; | ||
import org.eclipse.edc.spi.query.Criterion; | ||
import org.eclipse.edc.spi.query.CriterionOperatorRegistry; | ||
|
||
import java.util.List; | ||
import java.util.function.Predicate; | ||
|
||
import static org.eclipse.edc.spi.query.Criterion.criterion; | ||
|
||
/** | ||
* Convert criteria to predicate for stateful entities. | ||
* | ||
* @param <E> entity type | ||
*/ | ||
public class StatefulEntityCriteriaToPredicate<E extends StatefulEntity<E>> implements CriteriaToPredicate<E> { | ||
|
||
private final CriterionOperatorRegistry criterionOperatorRegistry; | ||
private final StateResolver stateResolver; | ||
|
||
public StatefulEntityCriteriaToPredicate(CriterionOperatorRegistry criterionOperatorRegistry, StateResolver stateResolver) { | ||
this.criterionOperatorRegistry = criterionOperatorRegistry; | ||
this.stateResolver = stateResolver; | ||
} | ||
|
||
@Override | ||
public Predicate<E> convert(List<Criterion> criteria) { | ||
return criteria.stream() | ||
.map(criterion -> { | ||
if (criterion.getOperandLeft().equals("state") && criterion.getOperandRight() instanceof String stateString) { | ||
return criterion(criterion.getOperandLeft(), criterion.getOperator(), stateResolver.resolve(stateString)); | ||
} | ||
return criterion; | ||
}) | ||
.map(criterionOperatorRegistry::<E>toPredicate) | ||
.reduce(x -> true, Predicate::and); | ||
} | ||
} |
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
Oops, something went wrong.