diff --git a/extensions/store/sql/identity-hub-credentials-store-sql/src/main/java/org/eclipse/edc/identityhub/store/sql/credentials/schema/postgres/CredentialJsonMapping.java b/extensions/store/sql/identity-hub-credentials-store-sql/src/main/java/org/eclipse/edc/identityhub/store/sql/credentials/schema/postgres/CredentialJsonMapping.java index db83e73e3..0b1c18163 100644 --- a/extensions/store/sql/identity-hub-credentials-store-sql/src/main/java/org/eclipse/edc/identityhub/store/sql/credentials/schema/postgres/CredentialJsonMapping.java +++ b/extensions/store/sql/identity-hub-credentials-store-sql/src/main/java/org/eclipse/edc/identityhub/store/sql/credentials/schema/postgres/CredentialJsonMapping.java @@ -15,45 +15,50 @@ package org.eclipse.edc.identityhub.store.sql.credentials.schema.postgres; import org.eclipse.edc.spi.types.PathItem; -import org.eclipse.edc.sql.translation.JsonFieldMapping; +import org.eclipse.edc.sql.translation.JsonFieldTranslator; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; /** * Maps the canonical format of a {@link org.eclipse.edc.identitytrust.model.VerifiableCredential} onto its JSON representation * and generates query statements for Postgres. */ -public class CredentialJsonMapping extends JsonFieldMapping { +public class CredentialJsonMapping extends JsonFieldTranslator { + + private final Map replacements = new HashMap<>(); public CredentialJsonMapping(String columnName) { super(columnName); - add("credentialSubject", PostgresDialectStatements.CREDENTIAL_SUBJECT_ALIAS); + replacements.put("credentialSubject", PostgresDialectStatements.CREDENTIAL_SUBJECT_ALIAS); } @Override - public String getStatement(String canonicalPropertyName, Class type) { - return super.getStatement(canonicalPropertyName, type); - } + public String getLeftOperand(List path, Class type) { + var stmt = super.getLeftOperand(path, type); - @Override - public String getStatement(List path, Class type) { - var replacement = fieldMap.get(path.get(0).toString()); + if (path.stream().allMatch(p -> p.toString().equals("types"))) { + return "(%s)::jsonb".formatted(stmt).replace("->>", "->"); + } + + if (path.size() == 1) { + return stmt; + } // the WHERE clause can't handle set-returning functions such as "json_array_elements". thus, we must use an alias // in the FROM clause, and re-use the same alias in the WHERE clause, for example: - // SELECT * FROM credential_resource r, json_array_elements(r.verifiable_credential -> 'credentialSubject') subj WHERE subj ->> 'test-key' = 'test-val2'; + // SELECT * FROM credential_resource r, json_array_elements(r.verifiable_credential -> 'credentialSubject') subj WHERE subj ->> 'test-key' = 'test-val2'; + var firstPathItem = path.get(0).toString(); + var replacement = replacements.get(firstPathItem); if (replacement != null) { - List newPath = new ArrayList<>(); + var newPath = new ArrayList(); path.stream().skip(1).forEach(newPath::add); - var newMapping = new JsonFieldMapping(replacement.toString()); - return newMapping.getStatement(newPath, type); + var newMapping = new JsonFieldTranslator(replacement); + return newMapping.getLeftOperand(newPath, type); } - var stmt = super.getStatement(path, type); - if (path.stream().allMatch(p -> p.toString().equals("types"))) { - stmt = "(%s)::jsonb".formatted(stmt).replace("->>", "->"); - } return stmt; } } diff --git a/extensions/store/sql/identity-hub-did-store-sql/src/main/java/org/eclipse/edc/identityhub/did/store/sql/schema/postgres/DidDocumentMapping.java b/extensions/store/sql/identity-hub-did-store-sql/src/main/java/org/eclipse/edc/identityhub/did/store/sql/schema/postgres/DidDocumentMapping.java index cbe845c83..b15e8841b 100644 --- a/extensions/store/sql/identity-hub-did-store-sql/src/main/java/org/eclipse/edc/identityhub/did/store/sql/schema/postgres/DidDocumentMapping.java +++ b/extensions/store/sql/identity-hub-did-store-sql/src/main/java/org/eclipse/edc/identityhub/did/store/sql/schema/postgres/DidDocumentMapping.java @@ -15,7 +15,7 @@ package org.eclipse.edc.identityhub.did.store.sql.schema.postgres; import org.eclipse.edc.identityhub.did.store.sql.DidResourceStatements; -import org.eclipse.edc.sql.translation.JsonFieldMapping; +import org.eclipse.edc.sql.translation.JsonFieldTranslator; import org.eclipse.edc.sql.translation.TranslationMapping; /** @@ -30,8 +30,8 @@ public class DidDocumentMapping extends TranslationMapping { public DidDocumentMapping(DidResourceStatements statements) { add(FIELD_ID, statements.getIdColumn()); - add(FIELD_SERVICE, new JsonFieldMapping(FIELD_SERVICE)); - add(FIELD_VERIFICATION_METHOD, new JsonFieldMapping(FIELD_VERIFICATION_METHOD)); + add(FIELD_SERVICE, new JsonFieldTranslator(FIELD_SERVICE)); + add(FIELD_VERIFICATION_METHOD, new JsonFieldTranslator(FIELD_VERIFICATION_METHOD)); add(FIELD_AUTHENTICATION, FIELD_AUTHENTICATION); } }