Search in sources :

Example 56 with DatabaseQuery

use of org.eclipse.persistence.queries.DatabaseQuery in project eclipselink by eclipse-ee4j.

the class ObjectPersistenceRuntimeXMLProject method buildDatabaseQueryDescriptor.

protected ClassDescriptor buildDatabaseQueryDescriptor() {
    XMLDescriptor descriptor = new XMLDescriptor();
    descriptor.setJavaClass(DatabaseQuery.class);
    descriptor.setDefaultRootElement("query");
    descriptor.getInheritancePolicy().setClassIndicatorField(new XMLField("@xsi:type"));
    descriptor.getInheritancePolicy().addClassIndicator(ReadAllQuery.class, getPrimaryNamespaceXPath() + "read-all-query");
    descriptor.getInheritancePolicy().addClassIndicator(ReadObjectQuery.class, getPrimaryNamespaceXPath() + "read-object-query");
    descriptor.getInheritancePolicy().addClassIndicator(DataReadQuery.class, getPrimaryNamespaceXPath() + "data-read-query");
    descriptor.getInheritancePolicy().addClassIndicator(DataModifyQuery.class, getPrimaryNamespaceXPath() + "data-modify-query");
    descriptor.getInheritancePolicy().addClassIndicator(DirectReadQuery.class, getPrimaryNamespaceXPath() + "direct-read-query");
    descriptor.getInheritancePolicy().addClassIndicator(ValueReadQuery.class, getPrimaryNamespaceXPath() + "value-read-query");
    descriptor.getInheritancePolicy().addClassIndicator(DeleteObjectQuery.class, getPrimaryNamespaceXPath() + "delete-object-query");
    descriptor.getInheritancePolicy().addClassIndicator(DeleteAllQuery.class, getPrimaryNamespaceXPath() + "delete-all-query");
    descriptor.getInheritancePolicy().addClassIndicator(InsertObjectQuery.class, getPrimaryNamespaceXPath() + "insert-object-query");
    descriptor.getInheritancePolicy().addClassIndicator(UpdateObjectQuery.class, getPrimaryNamespaceXPath() + "update-object-query");
    descriptor.getInheritancePolicy().addClassIndicator(DoesExistQuery.class, getPrimaryNamespaceXPath() + "does-exist-query");
    descriptor.getInheritancePolicy().addClassIndicator(ReportQuery.class, getPrimaryNamespaceXPath() + "report-query");
    XMLDirectMapping nameMapping = new XMLDirectMapping();
    nameMapping.setAttributeName("name");
    nameMapping.setGetMethodName("getName");
    nameMapping.setSetMethodName("setName");
    nameMapping.setXPath("@name");
    descriptor.addMapping(nameMapping);
    XMLCompositeObjectMapping expressionMapping = new XMLCompositeObjectMapping();
    expressionMapping.setAttributeName("selectionCriteria");
    expressionMapping.setGetMethodName("getSelectionCriteria");
    expressionMapping.setSetMethodName("setSelectionCriteria");
    expressionMapping.setAttributeAccessor(new AttributeAccessor() {

        @Override
        public Object getAttributeValueFromObject(Object object) {
            return ((DatabaseQuery) object).getSelectionCriteria();
        }

        @Override
        public void setAttributeValueInObject(Object object, Object value) {
            if (!(object instanceof ObjectLevelReadQuery)) {
                return;
            }
            ObjectLevelReadQuery query = (ObjectLevelReadQuery) object;
            Expression expression = (Expression) value;
            if (expression != null) {
                expression = expression.rebuildOn(query.getExpressionBuilder());
            }
            query.setSelectionCriteria(expression);
        }
    });
    expressionMapping.setReferenceClass(Expression.class);
    expressionMapping.setXPath(getSecondaryNamespaceXPath() + "criteria");
    descriptor.addMapping(expressionMapping);
    XMLCompositeCollectionMapping argumentsMapping = new XMLCompositeCollectionMapping();
    // Handle translation of argument lists to query-arguments.
    argumentsMapping.setAttributeAccessor(new AttributeAccessor() {

        @Override
        public Object getAttributeValueFromObject(Object object) {
            DatabaseQuery query = (DatabaseQuery) object;
            List<String> arguments = query.getArguments();
            List<String> types = query.getArgumentTypeNames();
            List<Object> values = query.getArgumentValues();
            Vector<QueryArgument> queryArguments = new Vector<>(arguments.size());
            for (int index = 0; index < arguments.size(); index++) {
                QueryArgument queryArgument = new QueryArgument();
                queryArgument.setKey(arguments.get(index));
                if (!types.isEmpty()) {
                    queryArgument.setTypeName(types.get(index));
                }
                if (!values.isEmpty()) {
                    queryArgument.setValue(values.get(index));
                }
                if (query.hasNullableArguments() && query.getNullableArguments().contains(new DatabaseField((String) queryArgument.getKey()))) {
                    queryArgument.setNullable(true);
                }
                queryArguments.add(queryArgument);
            }
            return queryArguments;
        }

        @Override
        public void setAttributeValueInObject(Object object, Object value) {
            DatabaseQuery query = (DatabaseQuery) object;
            @SuppressWarnings({ "unchecked" }) List<QueryArgument> queryArguments = (List<QueryArgument>) value;
            List<String> arguments = new ArrayList<>(queryArguments.size());
            List<Class<?>> types = new ArrayList<>(queryArguments.size());
            List<Object> values = new ArrayList<>(queryArguments.size());
            for (int index = 0; index < queryArguments.size(); index++) {
                QueryArgument queryArgument = queryArguments.get(index);
                arguments.add((String) queryArgument.getKey());
                if (queryArgument.getValue() != null) {
                    values.add(queryArgument.getValue());
                }
                if (queryArgument.getType() != null) {
                    types.add(queryArgument.getType());
                }
                if (queryArgument.isNullable()) {
                    query.getNullableArguments().add(new DatabaseField((String) queryArgument.getKey()));
                }
            }
            query.setArguments(arguments);
            if (!types.isEmpty()) {
                query.setArgumentTypes(types);
            }
            if (!values.isEmpty()) {
                query.setArgumentValues(values);
            }
        }
    });
    argumentsMapping.setAttributeName("argumentsMapping");
    argumentsMapping.setXPath(getSecondaryNamespaceXPath() + "arguments/" + getSecondaryNamespaceXPath() + "argument");
    argumentsMapping.setReferenceClass(QueryArgument.class);
    descriptor.addMapping(argumentsMapping);
    XMLDirectMapping shouldMaintainCacheMapping = new XMLDirectMapping();
    shouldMaintainCacheMapping.setAttributeName("shouldMaintainCache");
    shouldMaintainCacheMapping.setGetMethodName("shouldMaintainCache");
    shouldMaintainCacheMapping.setSetMethodName("setShouldMaintainCache");
    shouldMaintainCacheMapping.setXPath(getPrimaryNamespaceXPath() + "maintain-cache/text()");
    shouldMaintainCacheMapping.setNullValue(Boolean.TRUE);
    descriptor.addMapping(shouldMaintainCacheMapping);
    XMLDirectMapping shouldBindAllParametersMapping = new XMLDirectMapping();
    shouldBindAllParametersMapping.setAttributeName("shouldBindAllParameters");
    shouldBindAllParametersMapping.setXPath(getPrimaryNamespaceXPath() + "bind-all-parameters/text()");
    descriptor.addMapping(shouldBindAllParametersMapping);
    XMLDirectMapping shouldCacheStatementMapping = new XMLDirectMapping();
    shouldCacheStatementMapping.setAttributeName("shouldCacheStatement");
    shouldCacheStatementMapping.setXPath(getPrimaryNamespaceXPath() + "cache-statement/text()");
    descriptor.addMapping(shouldCacheStatementMapping);
    XMLDirectMapping queryTimeoutMapping = new XMLDirectMapping();
    queryTimeoutMapping.setAttributeName("queryTimeout");
    queryTimeoutMapping.setGetMethodName("getQueryTimeout");
    queryTimeoutMapping.setSetMethodName("setQueryTimeout");
    queryTimeoutMapping.setXPath(getPrimaryNamespaceXPath() + "timeout/text()");
    queryTimeoutMapping.setNullValue(DescriptorQueryManager.DefaultTimeout);
    descriptor.addMapping(queryTimeoutMapping);
    // feaure 2297
    XMLDirectMapping shouldPrepareMapping = new XMLDirectMapping();
    shouldPrepareMapping.setAttributeName("shouldPrepare");
    shouldPrepareMapping.setGetMethodName("shouldPrepare");
    shouldPrepareMapping.setSetMethodName("setShouldPrepare");
    shouldPrepareMapping.setXPath(getPrimaryNamespaceXPath() + "prepare/text()");
    shouldPrepareMapping.setNullValue(Boolean.TRUE);
    descriptor.addMapping(shouldPrepareMapping);
    XMLCompositeObjectMapping callMapping = new XMLCompositeObjectMapping();
    callMapping.setAttributeName("call");
    callMapping.setGetMethodName("getDatasourceCall");
    callMapping.setSetMethodName("setDatasourceCall");
    callMapping.setReferenceClass(Call.class);
    callMapping.setXPath(getPrimaryNamespaceXPath() + "call");
    descriptor.addMapping(callMapping);
    XMLCompositeObjectMapping redirectorMapping = new XMLCompositeObjectMapping();
    redirectorMapping.setAttributeName("redirector");
    redirectorMapping.setGetMethodName("getRedirector");
    redirectorMapping.setSetMethodName("setRedirector");
    redirectorMapping.setReferenceClass(MethodBaseQueryRedirector.class);
    redirectorMapping.setXPath(getPrimaryNamespaceXPath() + "query-redirector");
    descriptor.addMapping(redirectorMapping);
    return descriptor;
}
Also used : XMLField(org.eclipse.persistence.oxm.XMLField) QueryArgument(org.eclipse.persistence.internal.descriptors.QueryArgument) DatabaseQuery(org.eclipse.persistence.queries.DatabaseQuery) ObjectLevelReadQuery(org.eclipse.persistence.queries.ObjectLevelReadQuery) XMLDescriptor(org.eclipse.persistence.oxm.XMLDescriptor) XMLDirectMapping(org.eclipse.persistence.oxm.mappings.XMLDirectMapping) RelationExpression(org.eclipse.persistence.internal.expressions.RelationExpression) FieldExpression(org.eclipse.persistence.internal.expressions.FieldExpression) FunctionExpression(org.eclipse.persistence.internal.expressions.FunctionExpression) QueryKeyExpression(org.eclipse.persistence.internal.expressions.QueryKeyExpression) Expression(org.eclipse.persistence.expressions.Expression) ConstantExpression(org.eclipse.persistence.internal.expressions.ConstantExpression) ParameterExpression(org.eclipse.persistence.internal.expressions.ParameterExpression) LogicalExpression(org.eclipse.persistence.internal.expressions.LogicalExpression) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) XMLCompositeCollectionMapping(org.eclipse.persistence.oxm.mappings.XMLCompositeCollectionMapping) ArrayList(java.util.ArrayList) List(java.util.List) AttributeAccessor(org.eclipse.persistence.mappings.AttributeAccessor) Vector(java.util.Vector) NonSynchronizedVector(org.eclipse.persistence.internal.helper.NonSynchronizedVector) XMLCompositeObjectMapping(org.eclipse.persistence.oxm.mappings.XMLCompositeObjectMapping)

Example 57 with DatabaseQuery

use of org.eclipse.persistence.queries.DatabaseQuery in project eclipselink by eclipse-ee4j.

the class TestTimestampVersionLocking method testLocalTimestampPropertyCustomizer.

/**
 * Check that setting the value SERVER on the OptimisticLockingPolicy in a
 * DescriptorCustomizer will override the persistence property value.
 */
@Test
public void testLocalTimestampPropertyCustomizer() throws Exception {
    // Have policy customized to use Server time
    ClassDescriptorCustomizer.USETIME = TimestampLockingPolicy.SERVER_TIME;
    EntityManager em = emfCustomized.createEntityManager();
    try {
        em.getTransaction().begin();
        // Get the session for this transaction
        Session session = ((EntityManagerImpl) em).getActiveSession();
        // Get the TimestampQuery that would be used by the platform to get the current time
        DatabaseQuery query = session.getDatasourcePlatform().getTimestampQuery();
        // Add a Listener to the session
        QueryListener listener = new QueryListener(query);
        session.getEventManager().addListener(listener);
        // Persist an Entity that will use Timestamp version locking and will trigger QueryListener
        TimestampDog dog = new TimestampDog();
        em.persist(dog);
        em.getTransaction().commit();
        // Make sure the query was executed
        Assert.assertTrue("Query (" + listener.getQuery().getSQLString() + ") was not executed as expected.", listener.wasQueryExecuted());
    } finally {
        if (em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }
    }
}
Also used : EntityManager(jakarta.persistence.EntityManager) DatabaseQuery(org.eclipse.persistence.queries.DatabaseQuery) EntityManagerImpl(org.eclipse.persistence.internal.jpa.EntityManagerImpl) TimestampDog(org.eclipse.persistence.jpa.test.locking.model.TimestampDog) Session(org.eclipse.persistence.sessions.Session) Test(org.junit.Test)

Example 58 with DatabaseQuery

use of org.eclipse.persistence.queries.DatabaseQuery in project eclipselink by eclipse-ee4j.

the class TestTimestampVersionLocking method testLocalTimestampPropertyFalseCustomizer.

/**
 * Check that setting the value LOCAL on the OptimisticLockingPolicy in a
 * DescriptorCustomizer will override the persistence property value.
 */
@Test
public void testLocalTimestampPropertyFalseCustomizer() throws Exception {
    // Have policy customized to use local time
    ClassDescriptorCustomizer.USETIME = TimestampLockingPolicy.LOCAL_TIME;
    EntityManager em = emfFalseCustomized.createEntityManager();
    try {
        em.getTransaction().begin();
        // Get the session for this transaction
        Session session = ((EntityManagerImpl) em).getActiveSession();
        // Get the TimestampQuery that would be used by the platform to get the current time
        DatabaseQuery query = session.getDatasourcePlatform().getTimestampQuery();
        // Add a Listener to the session
        QueryListener listener = new QueryListener(query);
        session.getEventManager().addListener(listener);
        // Persist an Entity that will use Timestamp version locking and will trigger QueryListener
        TimestampDog dog = new TimestampDog();
        em.persist(dog);
        em.getTransaction().commit();
        // Make sure the query was not executed
        Assert.assertTrue("Query (" + listener.getQuery().getSQLString() + ") was executed unexpectedly.", !listener.wasQueryExecuted());
    } finally {
        if (em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }
    }
}
Also used : EntityManager(jakarta.persistence.EntityManager) DatabaseQuery(org.eclipse.persistence.queries.DatabaseQuery) EntityManagerImpl(org.eclipse.persistence.internal.jpa.EntityManagerImpl) TimestampDog(org.eclipse.persistence.jpa.test.locking.model.TimestampDog) Session(org.eclipse.persistence.sessions.Session) Test(org.junit.Test)

Example 59 with DatabaseQuery

use of org.eclipse.persistence.queries.DatabaseQuery in project eclipselink by eclipse-ee4j.

the class TestTimestampVersionLocking method testLocalTimestampPropertyFalse.

/**
 * Check that setting the property "false" will get the server system time
 * just like the default behavior.
 */
@Test
public void testLocalTimestampPropertyFalse() throws Exception {
    EntityManager em = emfFalse.createEntityManager();
    try {
        em.getTransaction().begin();
        // Get the session for this transaction
        Session session = ((EntityManagerImpl) em).getActiveSession();
        // Get the TimestampQuery that would be used by the platform to get the current time
        DatabaseQuery query = session.getDatasourcePlatform().getTimestampQuery();
        // Add a Listener to the session
        QueryListener listener = new QueryListener(query);
        session.getEventManager().addListener(listener);
        // Persist an Entity that will use Timestamp version locking and will trigger QueryListener
        TimestampDog dog = new TimestampDog();
        em.persist(dog);
        em.getTransaction().commit();
        // Make sure the query was not executed
        Assert.assertTrue("Query (" + listener.getQuery().getSQLString() + ") was executed unexpectedly.", listener.wasQueryExecuted());
    } finally {
        if (em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }
    }
}
Also used : EntityManager(jakarta.persistence.EntityManager) DatabaseQuery(org.eclipse.persistence.queries.DatabaseQuery) EntityManagerImpl(org.eclipse.persistence.internal.jpa.EntityManagerImpl) TimestampDog(org.eclipse.persistence.jpa.test.locking.model.TimestampDog) Session(org.eclipse.persistence.sessions.Session) Test(org.junit.Test)

Example 60 with DatabaseQuery

use of org.eclipse.persistence.queries.DatabaseQuery in project eclipselink by eclipse-ee4j.

the class LOBValueWriter method buildAndExecuteCall.

protected void buildAndExecuteCall(DatabaseCall dbCall, AbstractSession session) {
    DatabaseQuery query = dbCall.getQuery();
    if (!query.isWriteObjectQuery()) {
        // if not writequery, should not go through the locator writing..
        return;
    }
    WriteObjectQuery writeQuery = (WriteObjectQuery) query;
    writeQuery.setAccessor(accessor);
    // build a select statement form the query
    SQLSelectStatement selectStatement = buildSelectStatementForLocator(writeQuery, dbCall, session);
    // then build a call from the statement
    DatabaseCall call = buildCallFromSelectStatementForLocator(selectStatement, writeQuery, dbCall, session);
    accessor.executeCall(call, call.getQuery().getTranslationRow(), session);
}
Also used : DatabaseCall(org.eclipse.persistence.internal.databaseaccess.DatabaseCall) WriteObjectQuery(org.eclipse.persistence.queries.WriteObjectQuery) DatabaseQuery(org.eclipse.persistence.queries.DatabaseQuery) SQLSelectStatement(org.eclipse.persistence.internal.expressions.SQLSelectStatement)

Aggregations

DatabaseQuery (org.eclipse.persistence.queries.DatabaseQuery)86 ArrayList (java.util.ArrayList)18 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)15 List (java.util.List)14 Vector (java.util.Vector)12 AbstractSession (org.eclipse.persistence.internal.sessions.AbstractSession)12 ObjectLevelReadQuery (org.eclipse.persistence.queries.ObjectLevelReadQuery)12 DatabaseField (org.eclipse.persistence.internal.helper.DatabaseField)8 EntityManager (jakarta.persistence.EntityManager)6 HashMap (java.util.HashMap)6 QName (javax.xml.namespace.QName)6 EJBQueryImpl (org.eclipse.persistence.internal.jpa.EJBQueryImpl)6 PersistenceContext (org.eclipse.persistence.jpa.rs.PersistenceContext)6 DataReadQuery (org.eclipse.persistence.queries.DataReadQuery)6 Session (org.eclipse.persistence.sessions.Session)6 Test (org.junit.Test)6 NonSynchronizedVector (org.eclipse.persistence.internal.helper.NonSynchronizedVector)5 ReadQuery (org.eclipse.persistence.queries.ReadQuery)5 ReportQuery (org.eclipse.persistence.queries.ReportQuery)5 PersistenceException (jakarta.persistence.PersistenceException)4