Search in sources :

Example 1 with DatasourceCallQueryMechanism

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

the class DatasourceCall method buildQueryMechanism.

/**
 * Return the appropriate mechanism,
 * with the call added as necessary.
 */
@Override
public DatabaseQueryMechanism buildQueryMechanism(DatabaseQuery query, DatabaseQueryMechanism mechanism) {
    if (mechanism.isCallQueryMechanism() && (mechanism instanceof DatasourceCallQueryMechanism)) {
        // Must also add the call singleton...
        DatasourceCallQueryMechanism callMechanism = ((DatasourceCallQueryMechanism) mechanism);
        if (!callMechanism.hasMultipleCalls()) {
            callMechanism.addCall(callMechanism.getCall());
            callMechanism.setCall(null);
        }
        callMechanism.addCall(this);
        return mechanism;
    } else {
        return buildNewQueryMechanism(query);
    }
}
Also used : DatasourceCallQueryMechanism(org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism)

Example 2 with DatasourceCallQueryMechanism

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

the class ReadObjectQuery method executeObjectLevelReadQueryFromResultSet.

/**
 * INTERNAL:
 * Execute the query building the objects directly from the database result-set.
 * @exception  DatabaseException - an error has occurred on the database
 * @return object - the first object found or null if none.
 */
@Override
protected Object executeObjectLevelReadQueryFromResultSet() throws DatabaseException {
    AbstractSession session = this.session;
    DatabasePlatform platform = session.getPlatform();
    DatabaseCall call = ((DatasourceCallQueryMechanism) this.queryMechanism).selectResultSet();
    Statement statement = call.getStatement();
    ResultSet resultSet = call.getResult();
    DatabaseAccessor accessor = (DatabaseAccessor) ((List<Accessor>) this.accessors).get(0);
    boolean exceptionOccured = false;
    try {
        if (!resultSet.next()) {
            return null;
        }
        ResultSetMetaData metaData = resultSet.getMetaData();
        return this.descriptor.getObjectBuilder().buildObjectFromResultSet(this, null, resultSet, session, accessor, metaData, platform, call.getFields(), call.getFieldsArray());
    } catch (SQLException exception) {
        exceptionOccured = true;
        DatabaseException commException = accessor.processExceptionForCommError(session, exception, call);
        if (commException != null) {
            throw commException;
        }
        throw DatabaseException.sqlException(exception, call, accessor, session, false);
    } finally {
        try {
            if (resultSet != null) {
                resultSet.close();
            }
            if (statement != null) {
                accessor.releaseStatement(statement, call.getSQLString(), call, session);
            }
            if (accessor != null) {
                session.releaseReadConnection(accessor);
            }
        } catch (SQLException exception) {
            if (!exceptionOccured) {
                // in the case of an external connection pool the connection may be null after the statement release
                // if it is null we will be unable to check the connection for a comm error and
                // therefore must return as if it was not a comm error.
                DatabaseException commException = accessor.processExceptionForCommError(session, exception, call);
                if (commException != null) {
                    throw commException;
                }
                throw DatabaseException.sqlException(exception, call, accessor, session, false);
            }
        }
    }
}
Also used : DatabaseCall(org.eclipse.persistence.internal.databaseaccess.DatabaseCall) ResultSetMetaData(java.sql.ResultSetMetaData) SQLException(java.sql.SQLException) Statement(java.sql.Statement) DatasourceCallQueryMechanism(org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism) ResultSet(java.sql.ResultSet) DatabaseAccessor(org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor) DatabasePlatform(org.eclipse.persistence.internal.databaseaccess.DatabasePlatform) DatabaseAccessor(org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor) Accessor(org.eclipse.persistence.internal.databaseaccess.Accessor) DatabaseException(org.eclipse.persistence.exceptions.DatabaseException) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession)

Example 3 with DatasourceCallQueryMechanism

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

the class ReadAllQuery method executeObjectLevelReadQuery.

/**
 * INTERNAL:
 * Execute the query.
 * Get the rows and build the object from the rows.
 * @exception  DatabaseException - an error has occurred on the database
 * @return java.lang.Object collection of objects resulting from execution of query.
 */
@Override
protected Object executeObjectLevelReadQuery() throws DatabaseException {
    Object result = null;
    if (this.containerPolicy.overridesRead()) {
        this.executionTime = System.currentTimeMillis();
        return this.containerPolicy.execute();
    }
    if (this.descriptor.isDescriptorForInterface()) {
        Object returnValue = this.descriptor.getInterfacePolicy().selectAllObjectsUsingMultipleTableSubclassRead(this);
        this.executionTime = System.currentTimeMillis();
        return returnValue;
    }
    if (this.descriptor.hasTablePerClassPolicy() && this.descriptor.isAbstract()) {
        result = this.containerPolicy.containerInstance();
        if (this.shouldIncludeData) {
            ComplexQueryResult complexResult = new ComplexQueryResult();
            complexResult.setResult(result);
            complexResult.setData(new ArrayList());
            result = complexResult;
        }
    } else {
        Object sopObject = getTranslationRow().getSopObject();
        boolean useOptimization = false;
        if (sopObject == null) {
            useOptimization = usesResultSetAccessOptimization();
        }
        if (useOptimization) {
            DatabaseCall call = ((DatasourceCallQueryMechanism) this.queryMechanism).selectResultSet();
            this.executionTime = System.currentTimeMillis();
            Statement statement = call.getStatement();
            ResultSet resultSet = call.getResult();
            DatabaseAccessor dbAccessor = (DatabaseAccessor) getAccessor();
            boolean exceptionOccured = false;
            try {
                if (this.session.isUnitOfWork()) {
                    result = registerResultSetInUnitOfWork(resultSet, call.getFields(), call.getFieldsArray(), (UnitOfWorkImpl) this.session, this.translationRow);
                } else {
                    result = this.containerPolicy.containerInstance();
                    this.descriptor.getObjectBuilder().buildObjectsFromResultSetInto(this, resultSet, call.getFields(), call.getFieldsArray(), result);
                }
            } catch (SQLException exception) {
                exceptionOccured = true;
                DatabaseException commException = dbAccessor.processExceptionForCommError(this.session, exception, call);
                if (commException != null) {
                    throw commException;
                }
                throw DatabaseException.sqlException(exception, call, dbAccessor, this.session, false);
            } finally {
                try {
                    if (resultSet != null) {
                        resultSet.close();
                    }
                    if (dbAccessor != null) {
                        if (statement != null) {
                            dbAccessor.releaseStatement(statement, call.getSQLString(), call, this.session);
                        }
                    }
                    if (call.hasAllocatedConnection()) {
                        getExecutionSession().releaseConnectionAfterCall(this);
                    }
                } catch (RuntimeException cleanupException) {
                    if (!exceptionOccured) {
                        throw cleanupException;
                    }
                } catch (SQLException cleanupSQLException) {
                    if (!exceptionOccured) {
                        throw DatabaseException.sqlException(cleanupSQLException, call, dbAccessor, this.session, false);
                    }
                }
            }
        } else {
            List<AbstractRecord> rows;
            if (sopObject != null) {
                Object valuesIterator = this.containerPolicy.iteratorFor(getTranslationRow().getSopObject());
                int size = this.containerPolicy.sizeFor(sopObject);
                rows = new ArrayList<>(size);
                while (this.containerPolicy.hasNext(valuesIterator)) {
                    Object memberSopObject = this.containerPolicy.next(valuesIterator, this.session);
                    DatabaseRecord memberRow = new DatabaseRecord(0);
                    memberRow.setSopObject(memberSopObject);
                    rows.add(memberRow);
                }
                this.executionTime = System.currentTimeMillis();
            } else {
                rows = getQueryMechanism().selectAllRows();
                this.executionTime = System.currentTimeMillis();
                // If using 1-m joins, must set all rows.
                if (hasJoining() && this.joinedAttributeManager.isToManyJoin()) {
                    this.joinedAttributeManager.setDataResults(rows, this.session);
                }
                // Batch fetching in IN requires access to the rows to build the id array.
                if ((this.batchFetchPolicy != null) && this.batchFetchPolicy.isIN()) {
                    this.batchFetchPolicy.setDataResults(rows);
                }
            }
            if (this.session.isUnitOfWork()) {
                // 
                result = registerResultInUnitOfWork(rows, (UnitOfWorkImpl) this.session, this.translationRow, true);
            } else {
                if (rows instanceof ThreadCursoredList) {
                    result = this.containerPolicy.containerInstance();
                } else {
                    result = this.containerPolicy.containerInstance(rows.size());
                }
                this.descriptor.getObjectBuilder().buildObjectsInto(this, rows, result);
            }
            if (sopObject != null) {
                if (!this.descriptor.getObjectBuilder().isSimple()) {
                    // remove sopObject so it's not stuck in any value holder.
                    for (AbstractRecord row : rows) {
                        row.setSopObject(null);
                    }
                }
            } else {
                if (this.shouldIncludeData) {
                    ComplexQueryResult complexResult = new ComplexQueryResult();
                    complexResult.setResult(result);
                    complexResult.setData(rows);
                    result = complexResult;
                }
            }
        }
    }
    // Add the other (already registered) results and return them.
    if (this.descriptor.hasTablePerClassPolicy()) {
        result = this.containerPolicy.concatenateContainers(result, this.descriptor.getTablePerClassPolicy().selectAllObjectsUsingMultipleTableSubclassRead(this), this.session);
    }
    // If the results were empty, then ensure they get cached still.
    if (shouldCacheQueryResults() && this.containerPolicy.isEmpty(result)) {
        this.temporaryCachedQueryResults = InvalidObject.instance();
    }
    return result;
}
Also used : DatabaseRecord(org.eclipse.persistence.sessions.DatabaseRecord) SQLException(java.sql.SQLException) Statement(java.sql.Statement) ThreadCursoredList(org.eclipse.persistence.internal.helper.ThreadCursoredList) DatasourceCallQueryMechanism(org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism) ArrayList(java.util.ArrayList) AbstractRecord(org.eclipse.persistence.internal.sessions.AbstractRecord) UnitOfWorkImpl(org.eclipse.persistence.internal.sessions.UnitOfWorkImpl) DatabaseCall(org.eclipse.persistence.internal.databaseaccess.DatabaseCall) ResultSet(java.sql.ResultSet) InvalidObject(org.eclipse.persistence.internal.helper.InvalidObject) DatabaseAccessor(org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor) DatabaseException(org.eclipse.persistence.exceptions.DatabaseException)

Example 4 with DatasourceCallQueryMechanism

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

the class ReadAllQuery method executeObjectLevelReadQueryFromResultSet.

/**
 * INTERNAL:
 * Execute the query building the objects directly from the database result-set.
 * @exception  DatabaseException - an error has occurred on the database
 * @return an ArrayList of the resulting objects.
 */
@Override
protected Object executeObjectLevelReadQueryFromResultSet() throws DatabaseException {
    AbstractSession session = this.session;
    DatabasePlatform platform = session.getPlatform();
    DatabaseCall call = ((DatasourceCallQueryMechanism) this.queryMechanism).selectResultSet();
    Statement statement = call.getStatement();
    ResultSet resultSet = call.getResult();
    DatabaseAccessor accessor = (DatabaseAccessor) getAccessor();
    boolean exceptionOccured = false;
    try {
        ResultSetMetaData metaData = resultSet.getMetaData();
        List results = new ArrayList();
        ObjectBuilder builder = this.descriptor.getObjectBuilder();
        while (resultSet.next()) {
            results.add(builder.buildObjectFromResultSet(this, this.joinedAttributeManager, resultSet, session, accessor, metaData, platform, call.getFields(), call.getFieldsArray()));
        }
        return results;
    } catch (SQLException exception) {
        exceptionOccured = true;
        DatabaseException commException = accessor.processExceptionForCommError(session, exception, call);
        if (commException != null) {
            throw commException;
        }
        throw DatabaseException.sqlException(exception, call, accessor, session, false);
    } finally {
        try {
            if (resultSet != null) {
                resultSet.close();
            }
            if (statement != null) {
                accessor.releaseStatement(statement, call.getSQLString(), call, session);
            }
            if (accessor != null) {
                session.releaseReadConnection(accessor);
            }
        } catch (SQLException exception) {
            if (!exceptionOccured) {
                // in the case of an external connection pool the connection may be null after the statement release
                // if it is null we will be unable to check the connection for a comm error and
                // therefore must return as if it was not a comm error.
                DatabaseException commException = accessor.processExceptionForCommError(session, exception, call);
                if (commException != null) {
                    throw commException;
                }
                throw DatabaseException.sqlException(exception, call, accessor, session, false);
            }
        }
    }
}
Also used : SQLException(java.sql.SQLException) Statement(java.sql.Statement) DatasourceCallQueryMechanism(org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism) ArrayList(java.util.ArrayList) DatabasePlatform(org.eclipse.persistence.internal.databaseaccess.DatabasePlatform) ObjectBuilder(org.eclipse.persistence.internal.descriptors.ObjectBuilder) DatabaseCall(org.eclipse.persistence.internal.databaseaccess.DatabaseCall) ResultSetMetaData(java.sql.ResultSetMetaData) ResultSet(java.sql.ResultSet) DatabaseAccessor(org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor) ThreadCursoredList(org.eclipse.persistence.internal.helper.ThreadCursoredList) ArrayList(java.util.ArrayList) List(java.util.List) DatabaseException(org.eclipse.persistence.exceptions.DatabaseException) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession)

Example 5 with DatasourceCallQueryMechanism

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

the class ReadObjectQuery method executeObjectLevelReadQuery.

/**
 * INTERNAL:
 * Execute the query.
 * Do a cache lookup and build object from row if required.
 * @exception  DatabaseException - an error has occurred on the database
 * @return object - the first object found or null if none.
 */
@Override
protected Object executeObjectLevelReadQuery() throws DatabaseException {
    if (this.descriptor.isDescriptorForInterface() || this.descriptor.hasTablePerClassPolicy()) {
        Object returnValue = this.descriptor.getInterfacePolicy().selectOneObjectUsingMultipleTableSubclassRead(this);
        if (this.descriptor.hasTablePerClassPolicy() && (!this.descriptor.isAbstract()) && (returnValue == null)) {
        // let it fall through to query the root.
        } else {
            this.executionTime = System.currentTimeMillis();
            return returnValue;
        }
    }
    boolean shouldSetRowsForJoins = hasJoining() && this.joinedAttributeManager.isToManyJoin();
    AbstractSession session = getSession();
    Object result = null;
    AbstractRecord row = null;
    Object sopObject = getTranslationRow().getSopObject();
    boolean useOptimization = false;
    if (sopObject == null) {
        useOptimization = usesResultSetAccessOptimization();
    }
    if (useOptimization) {
        DatabaseCall call = ((DatasourceCallQueryMechanism) this.queryMechanism).selectResultSet();
        this.executionTime = System.currentTimeMillis();
        boolean exceptionOccured = false;
        ResultSet resultSet = call.getResult();
        DatabaseAccessor dbAccessor = (DatabaseAccessor) getAccessor();
        try {
            if (resultSet.next()) {
                ResultSetMetaData metaData = call.getResult().getMetaData();
                boolean useSimple = this.descriptor.getObjectBuilder().isSimple();
                DatabasePlatform platform = dbAccessor.getPlatform();
                boolean optimizeData = platform.shouldOptimizeDataConversion();
                if (useSimple) {
                    row = new SimpleResultSetRecord(call.getFields(), call.getFieldsArray(), resultSet, metaData, dbAccessor, getExecutionSession(), platform, optimizeData);
                    if (this.descriptor.isDescriptorTypeAggregate()) {
                        // Aggregate Collection may have an unmapped primary key referencing the owner, the corresponding field will not be used when the object is populated and therefore may not be cleared.
                        ((SimpleResultSetRecord) row).setShouldKeepValues(true);
                    }
                } else {
                    row = new ResultSetRecord(call.getFields(), call.getFieldsArray(), resultSet, metaData, dbAccessor, getExecutionSession(), platform, optimizeData);
                }
                if (session.isUnitOfWork()) {
                    result = registerResultInUnitOfWork(row, (UnitOfWorkImpl) session, this.translationRow, true);
                } else {
                    result = buildObject(row);
                }
                if (!useSimple && this.descriptor.getObjectBuilder().shouldKeepRow()) {
                    if (((ResultSetRecord) row).hasResultSet()) {
                        // ResultSet has not been fully triggered - that means the cached object was used.
                        // Yet the row still may be cached in a value holder (see loadBatchReadAttributes and loadJoinedAttributes methods).
                        // Remove ResultSet to avoid attempt to trigger it (already closed) when pk or fk values (already extracted) accessed when the value holder is instantiated.
                        ((ResultSetRecord) row).removeResultSet();
                    } else {
                        ((ResultSetRecord) row).removeNonIndirectionValues();
                    }
                }
            }
        } catch (SQLException exception) {
            exceptionOccured = true;
            DatabaseException commException = dbAccessor.processExceptionForCommError(session, exception, call);
            if (commException != null) {
                throw commException;
            }
            throw DatabaseException.sqlException(exception, call, getAccessor(), session, false);
        } finally {
            try {
                if (resultSet != null) {
                    resultSet.close();
                }
                if (dbAccessor != null) {
                    if (call.getStatement() != null) {
                        dbAccessor.releaseStatement(call.getStatement(), call.getSQLString(), call, session);
                    }
                }
                if (call.hasAllocatedConnection()) {
                    getExecutionSession().releaseConnectionAfterCall(this);
                }
            } catch (RuntimeException cleanupException) {
                if (!exceptionOccured) {
                    throw cleanupException;
                }
            } catch (SQLException cleanupSQLException) {
                if (!exceptionOccured) {
                    throw DatabaseException.sqlException(cleanupSQLException, call, dbAccessor, session, false);
                }
            }
        }
    } else {
        if (sopObject != null) {
            row = new DatabaseRecord(0);
            row.setSopObject(sopObject);
        } else {
            // If using 1-m joins, must select all rows.
            if (shouldSetRowsForJoins) {
                List rows = getQueryMechanism().selectAllRows();
                if (rows.size() > 0) {
                    row = (AbstractRecord) rows.get(0);
                }
                getJoinedAttributeManager().setDataResults(rows, session);
            } else {
                row = getQueryMechanism().selectOneRow();
            }
        }
        this.executionTime = System.currentTimeMillis();
        if (row != null) {
            if (session.isUnitOfWork()) {
                result = registerResultInUnitOfWork(row, (UnitOfWorkImpl) session, this.translationRow, true);
            } else {
                result = buildObject(row);
            }
            if (sopObject != null) {
                // remove sopObject so it's not stuck in a value holder.
                row.setSopObject(null);
            }
        }
    }
    if ((result == null) && shouldCacheQueryResults()) {
        cacheResult(null);
    }
    if ((result == null) && this.shouldRefreshIdentityMapResult) {
        // bug5955326, should invalidate the shared cached if refreshed object no longer exists.
        if (this.selectionId != null) {
            session.getParentIdentityMapSession(this.descriptor, true, true).getIdentityMapAccessor().invalidateObject(this.selectionId, this.referenceClass);
        } else if (this.selectionObject != null) {
            session.getParentIdentityMapSession(this.descriptor, true, true).getIdentityMapAccessor().invalidateObject(this.selectionObject);
        }
    }
    if (this.shouldIncludeData && (sopObject == null)) {
        ComplexQueryResult complexResult = new ComplexQueryResult();
        complexResult.setResult(result);
        complexResult.setData(row);
        return complexResult;
    }
    return result;
}
Also used : DatabaseRecord(org.eclipse.persistence.sessions.DatabaseRecord) SQLException(java.sql.SQLException) DatasourceCallQueryMechanism(org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism) AbstractRecord(org.eclipse.persistence.internal.sessions.AbstractRecord) UnitOfWorkImpl(org.eclipse.persistence.internal.sessions.UnitOfWorkImpl) SimpleResultSetRecord(org.eclipse.persistence.internal.sessions.SimpleResultSetRecord) ResultSetRecord(org.eclipse.persistence.internal.sessions.ResultSetRecord) DatabasePlatform(org.eclipse.persistence.internal.databaseaccess.DatabasePlatform) SimpleResultSetRecord(org.eclipse.persistence.internal.sessions.SimpleResultSetRecord) DatabaseCall(org.eclipse.persistence.internal.databaseaccess.DatabaseCall) ResultSetMetaData(java.sql.ResultSetMetaData) ResultSet(java.sql.ResultSet) InvalidObject(org.eclipse.persistence.internal.helper.InvalidObject) DatabaseAccessor(org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor) List(java.util.List) DatabaseException(org.eclipse.persistence.exceptions.DatabaseException) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession)

Aggregations

DatasourceCallQueryMechanism (org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism)5 ResultSet (java.sql.ResultSet)4 SQLException (java.sql.SQLException)4 DatabaseException (org.eclipse.persistence.exceptions.DatabaseException)4 DatabaseAccessor (org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor)4 DatabaseCall (org.eclipse.persistence.internal.databaseaccess.DatabaseCall)4 ResultSetMetaData (java.sql.ResultSetMetaData)3 Statement (java.sql.Statement)3 DatabasePlatform (org.eclipse.persistence.internal.databaseaccess.DatabasePlatform)3 AbstractSession (org.eclipse.persistence.internal.sessions.AbstractSession)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 InvalidObject (org.eclipse.persistence.internal.helper.InvalidObject)2 ThreadCursoredList (org.eclipse.persistence.internal.helper.ThreadCursoredList)2 AbstractRecord (org.eclipse.persistence.internal.sessions.AbstractRecord)2 UnitOfWorkImpl (org.eclipse.persistence.internal.sessions.UnitOfWorkImpl)2 DatabaseRecord (org.eclipse.persistence.sessions.DatabaseRecord)2 Accessor (org.eclipse.persistence.internal.databaseaccess.Accessor)1 ObjectBuilder (org.eclipse.persistence.internal.descriptors.ObjectBuilder)1 ResultSetRecord (org.eclipse.persistence.internal.sessions.ResultSetRecord)1