Skip to content

Commit

Permalink
chore: improves issuance process query on credentialDefinitions field (
Browse files Browse the repository at this point in the history
  • Loading branch information
wolf4ood authored Feb 12, 2025
1 parent fb6fc8e commit 0008d67
Show file tree
Hide file tree
Showing 6 changed files with 7 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public IssuanceProcessStore createSqlStore(ServiceExtensionContext context) {
}

private IssuanceProcessStoreStatements getStatementImpl() {
return statements != null ? statements : new PostgresDialectStatements(typemanager.getMapper());
return statements != null ? statements : new PostgresDialectStatements();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import org.eclipse.edc.issuerservice.store.sql.issuanceprocess.IssuanceProcessStoreStatements;
import org.eclipse.edc.sql.lease.StatefulEntityMapping;
import org.eclipse.edc.sql.translation.JsonArrayTranslator;
import org.eclipse.edc.sql.translation.JsonFieldTranslator;


Expand All @@ -34,6 +35,6 @@ public IssuanceProcessMapping(IssuanceProcessStoreStatements statements) {
add(FIELD_ID, statements.getIdColumn());
add(FIELD_PARTICIPANT_ID, statements.getParticipantIdColumn());
add(FIELD_CLAIMS, new JsonFieldTranslator(FIELD_CLAIMS));
add(FIELD_CREDENTIAL_DEFINITIONS, statements.getCredentialDefinitionsColumn());
add(FIELD_CREDENTIAL_DEFINITIONS, new JsonArrayTranslator(statements.getCredentialDefinitionsColumn()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,17 @@

package org.eclipse.edc.issuerservice.store.sql.issuanceprocess.schema.postgres;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.eclipse.edc.issuerservice.store.sql.issuanceprocess.BaseSqlDialectStatements;
import org.eclipse.edc.spi.persistence.EdcPersistenceException;
import org.eclipse.edc.spi.query.QuerySpec;
import org.eclipse.edc.sql.dialect.PostgresDialect;
import org.eclipse.edc.sql.translation.PostgresqlOperatorTranslator;
import org.eclipse.edc.sql.translation.SqlQueryStatement;

import static org.eclipse.edc.issuerservice.store.sql.issuanceprocess.schema.postgres.IssuanceProcessMapping.FIELD_CREDENTIAL_DEFINITIONS;

/**
* Postgres-specific specialization for creating queries based on Postgres JSON operators
*/
public class PostgresDialectStatements extends BaseSqlDialectStatements {

private final ObjectMapper mapper;

public PostgresDialectStatements(ObjectMapper mapper) {
public PostgresDialectStatements() {
super(new PostgresqlOperatorTranslator());
this.mapper = mapper;
}


Expand All @@ -43,33 +33,4 @@ public String getFormatAsJsonOperator() {
return PostgresDialect.getJsonCastOperator();
}

@Override
public SqlQueryStatement createQuery(QuerySpec querySpec) {

// if any criterion credentialDefinition array field, we need to slightly adapt the FROM clause
// by including a JSONB containment operator
if (querySpec.containsAnyLeftOperand(FIELD_CREDENTIAL_DEFINITIONS)) {
var select = "SELECT * FROM %s ".formatted(getIssuanceProcessTable());

var criteria = querySpec.getFilterExpression();
var filteredCriteria = criteria.stream()
.filter(c -> c.getOperandLeft().toString().startsWith(FIELD_CREDENTIAL_DEFINITIONS))
.toList();

criteria.removeAll(filteredCriteria);
var stmt = new SqlQueryStatement(select, querySpec, new IssuanceProcessMapping(this), operatorTranslator);
filteredCriteria.forEach(c -> {
var rightOperand = c.getOperandRight();
try {
var rightOperandJson = mapper.writeValueAsString(rightOperand);
stmt.addWhereClause("%s @> ?::jsonb".formatted(getCredentialDefinitionsColumn()), rightOperandJson);
} catch (JsonProcessingException e) {
throw new EdcPersistenceException(e);
}

});
return stmt;
}
return super.createQuery(querySpec);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class SqlIssuanceProcessStoreTest extends IssuanceProcessStoreTestBase {
void setup(PostgresqlStoreSetupExtension extension, QueryExecutor queryExecutor) {
var typeManager = new JacksonTypeManager();

statements = new PostgresDialectStatements(typeManager.getMapper());
statements = new PostgresDialectStatements();
store = new SqlIssuanceProcessStore(extension.getDataSourceRegistry(), extension.getDatasourceName(),
extension.getTransactionContext(), typeManager.getMapper(), queryExecutor, statements, RUNTIME_ID, Clock.systemUTC());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public CredentialDefinitionMapping(CredentialDefinitionStoreStatements statement
add(FIELD_JSON_SCHEMA_URL, statements.getJsonSchemaUrlColumn());
add(FIELD_VALIDITY, statements.getValidityColumn());
add(FIELD_DATAMODEL, statements.getDataModelColumn());
add(FIELD_ATTESTATIONS, new JsonArrayTranslator());
add(FIELD_ATTESTATIONS, new JsonArrayTranslator(statements.getAttestationsColumn()));
add(FIELD_RULES, new JsonFieldTranslator(RULES_ALIAS));
add(FIELD_MAPPINGS, new JsonFieldTranslator(MAPPING_ALIAS));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@ public ParticipantMapping(ParticipantStoreStatements statements) {
add(FIELD_LASTMODIFIED_TIMESTAMP, statements.getLastModifiedTimestampColumn());
add(FIELD_NAME, statements.getParticipantNameColumn());
add(FIELD_DID, statements.getDidColumn());
add(FIELD_ATTESTATIONS, new JsonArrayTranslator());
add(FIELD_ATTESTATIONS, new JsonArrayTranslator(statements.getAttestationsColumn()));
}
}

0 comments on commit 0008d67

Please sign in to comment.