|
| 1 | +/* |
| 2 | + * Copyright (c) 2024 Oracle and/or its affiliates. All rights reserved. |
| 3 | + * |
| 4 | + * This program and the accompanying materials are made available under the |
| 5 | + * terms of the Eclipse Public License v. 2.0 which is available at |
| 6 | + * http://www.eclipse.org/legal/epl-2.0, |
| 7 | + * or the Eclipse Distribution License v. 1.0 which is available at |
| 8 | + * http://www.eclipse.org/org/documents/edl-v10.php. |
| 9 | + * |
| 10 | + * SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause |
| 11 | + */ |
| 12 | + |
| 13 | +// Contributors: |
| 14 | +// Oracle - initial API and implementation |
| 15 | +// |
| 16 | +package org.eclipse.persistence.internal.jpa.jpql; |
| 17 | + |
| 18 | +import org.eclipse.persistence.descriptors.ClassDescriptor; |
| 19 | +import org.eclipse.persistence.descriptors.VersionLockingPolicy; |
| 20 | +import org.eclipse.persistence.internal.helper.DatabaseField; |
| 21 | +import org.eclipse.persistence.jpa.jpql.parser.EclipseLinkAnonymousExpressionVisitor; |
| 22 | +import org.eclipse.persistence.jpa.jpql.parser.IdExpression; |
| 23 | +import org.eclipse.persistence.jpa.jpql.parser.IdentificationVariable; |
| 24 | +import org.eclipse.persistence.jpa.jpql.parser.StateFieldPathExpression; |
| 25 | +import org.eclipse.persistence.jpa.jpql.parser.VersionExpression; |
| 26 | +import org.eclipse.persistence.mappings.DatabaseMapping; |
| 27 | + |
| 28 | +import java.util.List; |
| 29 | + |
| 30 | +/** |
| 31 | + * JPQL exclusive ID(), VERSION() functions/expressions are transformed there to StateFieldPathExpression. |
| 32 | + * It should be used in the future for another JPQL functions/expressions which are not available at the DB level. |
| 33 | + * E.g. For Entity e with idAttr as a primary key: <code>SELECT ID(e) FROM Entity e -> SELECT e.idAttr FROM Entity e</code> |
| 34 | + * For Entity e with versionAttr as a version attribute: <code>SELECT VERSION(e) FROM Entity e -> SELECT e.versionAttr FROM Entity e</code> |
| 35 | + * |
| 36 | + * @author Radek Felcman |
| 37 | + * @since 5.0 |
| 38 | + */ |
| 39 | +public abstract class JPQLFunctionsAbstractBuilder extends EclipseLinkAnonymousExpressionVisitor { |
| 40 | + |
| 41 | + /** |
| 42 | + * The {@link JPQLQueryContext} is used to query information about the application metadata and |
| 43 | + * cached information. |
| 44 | + */ |
| 45 | + final JPQLQueryContext queryContext; |
| 46 | + |
| 47 | + protected JPQLFunctionsAbstractBuilder(JPQLQueryContext queryContext) { |
| 48 | + this.queryContext = queryContext; |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * For Entity e with idAttr as a primary key: <code>SELECT ID(e) FROM Entity e -> SELECT e.idAttr FROM Entity e</code> |
| 53 | + * |
| 54 | + * @param expression The {@link IdExpression} to visit |
| 55 | + */ |
| 56 | + @Override |
| 57 | + public void visit(IdExpression expression) { |
| 58 | + //Fetch identification variable info |
| 59 | + IdentificationVariable identificationVariable = (IdentificationVariable) expression.getExpression(); |
| 60 | + String variableText = identificationVariable.getText(); |
| 61 | + String variableName = identificationVariable.getVariableName(); |
| 62 | + |
| 63 | + //Get id attribute name |
| 64 | + ClassDescriptor descriptor = this.queryContext.getDeclaration(variableName).getDescriptor(); |
| 65 | + List<DatabaseField> primaryKeyFields = descriptor.getPrimaryKeyFields(); |
| 66 | + String idAttributeName = getIdAttributeNameByField(descriptor.getMappings(), primaryKeyFields.get(0)); |
| 67 | + StateFieldPathExpression stateFieldPathExpression = new StateFieldPathExpression(expression.getParent(), variableText + "." + idAttributeName); |
| 68 | + expression.setStateFieldPathExpression(stateFieldPathExpression); |
| 69 | + |
| 70 | + //Continue with created StateFieldPathExpression |
| 71 | + //It handle by ObjectBuilder booth @Id/primary key types (simple/composite) |
| 72 | + expression.getStateFieldPathExpression().accept(this); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * For Entity e with versionAttr as a version attribute: <code>SELECT VERSION(e) FROM Entity e -> SELECT e.versionAttr FROM Entity e</code> |
| 77 | + * |
| 78 | + * @param expression The {@link VersionExpression} to visit |
| 79 | + */ |
| 80 | + @Override |
| 81 | + public void visit(VersionExpression expression) { |
| 82 | + //Fetch identification variable info |
| 83 | + IdentificationVariable identificationVariable = (IdentificationVariable) expression.getExpression(); |
| 84 | + String variableText = identificationVariable.getText(); |
| 85 | + String variableName = identificationVariable.getVariableName(); |
| 86 | + |
| 87 | + //Get version attribute name |
| 88 | + ClassDescriptor descriptor = this.queryContext.getDeclaration(variableName).getDescriptor(); |
| 89 | + String versionAttributeName = ((VersionLockingPolicy) descriptor.getOptimisticLockingPolicy()).getVersionMapping().getAttributeName(); |
| 90 | + StateFieldPathExpression stateFieldPathExpression = new StateFieldPathExpression(expression.getParent(), variableText + "." + versionAttributeName); |
| 91 | + expression.setStateFieldPathExpression(stateFieldPathExpression); |
| 92 | + |
| 93 | + //Continue with created StateFieldPathExpression |
| 94 | + expression.getStateFieldPathExpression().accept(this); |
| 95 | + } |
| 96 | + |
| 97 | + private String getIdAttributeNameByField(List<DatabaseMapping> databaseMappings, DatabaseField field) { |
| 98 | + for (DatabaseMapping mapping : databaseMappings) { |
| 99 | + if (field.equals(mapping.getField()) || mapping.isPrimaryKeyMapping()) { |
| 100 | + return mapping.getAttributeName(); |
| 101 | + } |
| 102 | + } |
| 103 | + return null; |
| 104 | + } |
| 105 | +} |
0 commit comments