Skip to content

Commit 8f2f6d1

Browse files
authored
feat: SQL store for cel expression (#67)
1 parent 4dc368f commit 8f2f6d1

File tree

14 files changed

+586
-6
lines changed

14 files changed

+586
-6
lines changed

extensions/cel/cel-spi/src/testFixtures/java/org/eclipse/edc/virtualized/policy/cel/CelExpressionStoreTestBase.java

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
package org.eclipse.edc.virtualized.policy.cel;
1616

17-
import org.assertj.core.api.Assertions;
1817
import org.eclipse.edc.spi.query.Criterion;
1918
import org.eclipse.edc.spi.query.QuerySpec;
2019
import org.eclipse.edc.virtualized.policy.cel.model.CelExpression;
@@ -36,7 +35,7 @@ void create() {
3635
var result = getStore().create(expr);
3736
assertThat(result).isSucceeded();
3837
var expressions = getStore().query(QuerySpec.max());
39-
Assertions.assertThat(expressions).usingRecursiveFieldByFieldElementComparator().containsExactly(expr);
38+
assertThat(expressions).usingRecursiveFieldByFieldElementComparator().containsExactly(expr);
4039
}
4140

4241
@Test
@@ -58,11 +57,26 @@ void query_noQuerySpec() {
5857
resources.forEach(getStore()::create);
5958

6059
var res = getStore().query(QuerySpec.none());
61-
Assertions.assertThat(res)
60+
assertThat(res)
6261
.usingRecursiveFieldByFieldElementComparator()
6362
.containsExactlyInAnyOrder(resources.toArray(new CelExpression[0]));
6463
}
6564

65+
@Test
66+
void query_leftOperand() {
67+
var resources = range(0, 5)
68+
.mapToObj(i -> celExpression("id" + i, "leftOperand" + i))
69+
.toList();
70+
71+
resources.forEach(getStore()::create);
72+
73+
var res = getStore().query(QuerySpec.Builder.newInstance().filter(Criterion.criterion("leftOperand", "=", "leftOperand3")).build());
74+
assertThat(res).hasSize(1)
75+
.first().satisfies(celExpression -> {
76+
assertThat(celExpression).usingRecursiveComparison().isEqualTo(resources.get(3));
77+
});
78+
}
79+
6680
@Test
6781
void query_whenNotFound() {
6882
var resources = range(0, 5)
@@ -74,7 +88,7 @@ void query_whenNotFound() {
7488
var query = QuerySpec.Builder.newInstance().filter(Criterion.criterion("id", "=", "non-existing-id"))
7589
.build();
7690
var res = getStore().query(query);
77-
Assertions.assertThat(res).isEmpty();
91+
assertThat(res).isEmpty();
7892
}
7993

8094
@Test
@@ -90,7 +104,7 @@ void query_byInvalidField_shouldReturnEmptyList() {
90104
.filter(new Criterion("invalidField", "=", "test-value"))
91105
.build();
92106
var res = getStore().query(query);
93-
Assertions.assertThat(res).isNotNull().isEmpty();
107+
assertThat(res).isNotNull().isEmpty();
94108
}
95109

96110
@Test
@@ -132,6 +146,10 @@ void delete_whenNotExists() {
132146
}
133147

134148
private CelExpression celExpression(String id) {
135-
return new CelExpression(id, "leftOperand", "expression", "description");
149+
return celExpression(id, "leftOperand");
150+
}
151+
152+
private CelExpression celExpression(String id, String leftOperand) {
153+
return new CelExpression(id, leftOperand, "expression", "description");
136154
}
137155
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright (c) 2025 Metaform Systems, Inc.
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Apache License, Version 2.0 which is available at
6+
* https://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* SPDX-License-Identifier: Apache-2.0
9+
*
10+
* Contributors:
11+
* Metaform Systems, Inc. - initial API and implementation
12+
*
13+
*/
14+
15+
plugins {
16+
`java-library`
17+
}
18+
19+
dependencies {
20+
api(project(":extensions:cel:cel-spi"))
21+
implementation(libs.edc.spi.transaction.datasource)
22+
implementation(libs.edc.lib.sql)
23+
implementation(libs.edc.core.sql.bootstrapper)
24+
25+
testImplementation(libs.edc.junit)
26+
testImplementation(testFixtures(libs.edc.fixtures.sql))
27+
testImplementation(testFixtures(project(":extensions:cel:cel-spi")))
28+
29+
testImplementation(libs.postgres)
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright (c) 2025 Metaform Systems, Inc.
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Apache License, Version 2.0 which is available at
6+
* https://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* SPDX-License-Identifier: Apache-2.0
9+
*
10+
* Contributors:
11+
* Metaform Systems, Inc. - initial API and implementation
12+
*
13+
*/
14+
15+
package org.eclipse.edc.virtual.connector.store.sql.cel;
16+
17+
import org.eclipse.edc.spi.query.QuerySpec;
18+
import org.eclipse.edc.sql.translation.PostgresqlOperatorTranslator;
19+
import org.eclipse.edc.sql.translation.SqlQueryStatement;
20+
import org.eclipse.edc.virtual.connector.store.sql.cel.schema.postgres.CelExpressionMapping;
21+
22+
import static java.lang.String.format;
23+
24+
public class BaseSqlDialectStatementsConfig implements CelExpressionStoreStatements {
25+
26+
@Override
27+
public String getInsertTemplate() {
28+
return executeStatement()
29+
.column(getIdColumn())
30+
.column(getLeftOperandColumn())
31+
.column(getExpressionColumn())
32+
.column(getDescriptionColumn())
33+
.jsonColumn(getScopesColumn())
34+
.column(getCreateTimestampColumn())
35+
.column(getLastModifiedTimestampColumn())
36+
.insertInto(getCelExpressionTable());
37+
}
38+
39+
@Override
40+
public String getUpdateTemplate() {
41+
return executeStatement()
42+
.column(getLeftOperandColumn())
43+
.column(getExpressionColumn())
44+
.column(getDescriptionColumn())
45+
.jsonColumn(getScopesColumn())
46+
.column(getCreateTimestampColumn())
47+
.column(getLastModifiedTimestampColumn())
48+
.update(getCelExpressionTable(), getIdColumn());
49+
}
50+
51+
@Override
52+
public String getFindByIdTemplate() {
53+
return format("SELECT * FROM %s WHERE %s = ?", getCelExpressionTable(), getIdColumn());
54+
55+
}
56+
57+
@Override
58+
public String getDeleteByIdTemplate() {
59+
return executeStatement().delete(getCelExpressionTable(), getIdColumn());
60+
}
61+
62+
@Override
63+
public SqlQueryStatement createQuery(QuerySpec querySpec) {
64+
var select = getSelectStatement();
65+
return new SqlQueryStatement(select, querySpec, new CelExpressionMapping(this), new PostgresqlOperatorTranslator());
66+
}
67+
68+
@Override
69+
public String getSelectStatement() {
70+
return format("SELECT * FROM %s", getCelExpressionTable());
71+
}
72+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright (c) 2025 Metaform Systems, Inc.
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Apache License, Version 2.0 which is available at
6+
* https://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* SPDX-License-Identifier: Apache-2.0
9+
*
10+
* Contributors:
11+
* Metaform Systems, Inc. - initial API and implementation
12+
*
13+
*/
14+
15+
package org.eclipse.edc.virtual.connector.store.sql.cel;
16+
17+
import org.eclipse.edc.spi.query.QuerySpec;
18+
import org.eclipse.edc.sql.statement.SqlStatements;
19+
import org.eclipse.edc.sql.translation.SqlQueryStatement;
20+
import org.eclipse.edc.virtualized.policy.cel.model.CelExpression;
21+
22+
/**
23+
* Defines SQL-statements and column names for use with a SQL-based {@link CelExpression}
24+
*/
25+
public interface CelExpressionStoreStatements extends SqlStatements {
26+
27+
default String getCelExpressionTable() {
28+
return "edc_cel_expression";
29+
}
30+
31+
default String getIdColumn() {
32+
return "id";
33+
}
34+
35+
default String getCreateTimestampColumn() {
36+
return "created_date";
37+
}
38+
39+
default String getLastModifiedTimestampColumn() {
40+
return "last_modified_date";
41+
}
42+
43+
default String getScopesColumn() {
44+
return "scopes";
45+
}
46+
47+
default String getLeftOperandColumn() {
48+
return "left_operand";
49+
}
50+
51+
default String getExpressionColumn() {
52+
return "expression";
53+
}
54+
55+
default String getDescriptionColumn() {
56+
return "description";
57+
}
58+
59+
String getInsertTemplate();
60+
61+
String getUpdateTemplate();
62+
63+
String getDeleteByIdTemplate();
64+
65+
String getFindByIdTemplate();
66+
67+
SqlQueryStatement createQuery(QuerySpec query);
68+
69+
String getSelectStatement();
70+
}

0 commit comments

Comments
 (0)