Search in sources :

Example 11 with DatabasePlatform

use of org.eclipse.persistence.internal.databaseaccess.DatabasePlatform in project eclipselink by eclipse-ee4j.

the class ReadAllQuery method registerResultSetInUnitOfWork.

/**
 * INTERNAL:
 * Version of the previous method for ResultSet optimization.
 *
 * @return the final (conformed, refreshed, wrapped) UnitOfWork query result
 */
public Object registerResultSetInUnitOfWork(ResultSet resultSet, Vector fields, DatabaseField[] fieldsArray, UnitOfWorkImpl unitOfWork, AbstractRecord arguments) throws SQLException {
    // TODO: add support for Conforming results in UOW - currently conforming in uow is not compatible with ResultSet optimization.
    ContainerPolicy cp = this.containerPolicy;
    Object clones = cp.containerInstance();
    ResultSetMetaData metaData = resultSet.getMetaData();
    boolean hasNext = resultSet.next();
    if (hasNext) {
        // TODO: possibly add support for SortedListContainerPolicy (cp.shouldAddAll() == true) - this policy currently is not compatible with ResultSet optimization
        boolean quickAdd = (clones instanceof Collection) && !this.descriptor.getObjectBuilder().hasWrapperPolicy();
        DatabaseAccessor dbAccessor = (DatabaseAccessor) getAccessor();
        boolean useSimple = this.descriptor.getObjectBuilder().isSimple();
        AbstractSession executionSession = getExecutionSession();
        DatabasePlatform platform = dbAccessor.getPlatform();
        boolean optimizeData = platform.shouldOptimizeDataConversion();
        if (useSimple) {
            // None of the fields are relational - the row could be reused, just clear all the values.
            SimpleResultSetRecord row = new SimpleResultSetRecord(fields, fieldsArray, resultSet, metaData, dbAccessor, executionSession, 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.
                row.setShouldKeepValues(true);
            }
            while (hasNext) {
                Object clone = buildObject(row);
                if (quickAdd) {
                    ((Collection) clones).add(clone);
                } else {
                    // TODO: investigate is it possible to support MappedKeyMapPolicy - this policy currently is not compatible with ResultSet optimization
                    cp.addInto(clone, clones, unitOfWork);
                }
                row.reset();
                hasNext = resultSet.next();
            }
        } else {
            boolean shouldKeepRow = this.descriptor.getObjectBuilder().shouldKeepRow();
            while (hasNext) {
                ResultSetRecord row = new ResultSetRecord(fields, fieldsArray, resultSet, metaData, dbAccessor, executionSession, platform, optimizeData);
                Object clone = buildObject(row);
                if (quickAdd) {
                    ((Collection) clones).add(clone);
                } else {
                    // TODO: investigate is it possible to support MappedKeyMapPolicy - this policy currently is not compatible with ResultSet optimization
                    cp.addInto(clone, clones, unitOfWork);
                }
                if (shouldKeepRow) {
                    if (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.
                        row.removeResultSet();
                    } else {
                        row.removeNonIndirectionValues();
                    }
                }
                hasNext = resultSet.next();
            }
        }
    }
    return clones;
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) ContainerPolicy(org.eclipse.persistence.internal.queries.ContainerPolicy) Collection(java.util.Collection) InvalidObject(org.eclipse.persistence.internal.helper.InvalidObject) DatabaseAccessor(org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor) 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) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession)

Example 12 with DatabasePlatform

use of org.eclipse.persistence.internal.databaseaccess.DatabasePlatform 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 13 with DatabasePlatform

use of org.eclipse.persistence.internal.databaseaccess.DatabasePlatform in project eclipselink by eclipse-ee4j.

the class StoredProcedureDefinition method printArgument.

/**
 * Print the argument and its type.
 * @param argument Stored procedure argument.
 * @param writer   Target writer where to write argument string.
 * @param session  Current session context.
 * @throws IOException When any IO problem occurs.
 */
protected void printArgument(final FieldDefinition argument, final Writer writer, final AbstractSession session) throws IOException {
    final DatabasePlatform platform = session.getPlatform();
    final FieldTypeDefinition fieldType = getFieldTypeDefinition(session, argument.type, argument.typeName);
    writer.write(platform.getProcedureArgumentString());
    if (platform.shouldPrintInputTokenAtStart()) {
        writer.write(" ");
        writer.write(platform.getInputProcedureToken());
        writer.write(" ");
    }
    writer.write(argument.name);
    writer.write(" ");
    writer.write(fieldType.getName());
    if (fieldType.isSizeAllowed() && platform.allowsSizeInProcedureArguments() && ((argument.size != 0) || (fieldType.isSizeRequired()))) {
        writer.write("(");
        if (argument.size == 0) {
            writer.write(Integer.toString(fieldType.getDefaultSize()));
        } else {
            writer.write(Integer.toString(argument.size));
        }
        if (argument.subSize != 0) {
            writer.write(",");
            writer.write(Integer.toString(argument.subSize));
        } else if (fieldType.getDefaultSubSize() != 0) {
            writer.write(",");
            writer.write(Integer.toString(fieldType.getDefaultSubSize()));
        }
        writer.write(")");
    }
}
Also used : FieldTypeDefinition(org.eclipse.persistence.internal.databaseaccess.FieldTypeDefinition) DatabasePlatform(org.eclipse.persistence.internal.databaseaccess.DatabasePlatform)

Example 14 with DatabasePlatform

use of org.eclipse.persistence.internal.databaseaccess.DatabasePlatform in project eclipselink by eclipse-ee4j.

the class StoredProcedureDefinition method printInOutputArgument.

/**
 * Print the argument and its type.
 * @param argument Stored procedure argument.
 * @param writer   Target writer where to write argument string.
 * @param session  Current session context.
 * @throws ValidationException When invalid or inconsistent data were found.
 */
protected void printInOutputArgument(final FieldDefinition argument, final Writer writer, final AbstractSession session) throws ValidationException {
    try {
        final DatabasePlatform platform = session.getPlatform();
        final FieldTypeDefinition fieldType = getFieldTypeDefinition(session, argument.type, argument.typeName);
        writer.write(platform.getProcedureArgumentString());
        if (platform.shouldPrintOutputTokenAtStart()) {
            writer.write(" ");
            writer.write(platform.getCreationInOutputProcedureToken());
            writer.write(" ");
        }
        writer.write(argument.name);
        if ((!platform.shouldPrintOutputTokenAtStart()) && platform.shouldPrintOutputTokenBeforeType()) {
            writer.write(" ");
            writer.write(platform.getCreationInOutputProcedureToken());
        }
        writer.write(" ");
        writer.write(fieldType.getName());
        if (fieldType.isSizeAllowed() && platform.allowsSizeInProcedureArguments() && ((argument.size != 0) || (fieldType.isSizeRequired()))) {
            writer.write("(");
            if (argument.size == 0) {
                writer.write(Integer.toString(fieldType.getDefaultSize()));
            } else {
                writer.write(Integer.toString(argument.size));
            }
            if (argument.subSize != 0) {
                writer.write(",");
                writer.write(Integer.toString(argument.subSize));
            } else if (fieldType.getDefaultSubSize() != 0) {
                writer.write(",");
                writer.write(Integer.toString(fieldType.getDefaultSubSize()));
            }
            writer.write(")");
        }
        if ((!platform.shouldPrintOutputTokenAtStart()) && (!platform.shouldPrintOutputTokenBeforeType())) {
            writer.write(" ");
            writer.write(platform.getCreationInOutputProcedureToken());
        }
    } catch (IOException ioException) {
        throw ValidationException.fileError(ioException);
    }
}
Also used : FieldTypeDefinition(org.eclipse.persistence.internal.databaseaccess.FieldTypeDefinition) DatabasePlatform(org.eclipse.persistence.internal.databaseaccess.DatabasePlatform) IOException(java.io.IOException)

Example 15 with DatabasePlatform

use of org.eclipse.persistence.internal.databaseaccess.DatabasePlatform in project eclipselink by eclipse-ee4j.

the class StoredProcedureDefinition method printOutputArgument.

/**
 * Print the argument and its type.
 * @param argument Stored procedure argument.
 * @param writer   Target writer where to write argument string.
 * @param session  Current session context.
 * @throws ValidationException When invalid or inconsistent data were found.
 */
protected void printOutputArgument(final FieldDefinition argument, final Writer writer, final AbstractSession session) throws ValidationException {
    try {
        final DatabasePlatform platform = session.getPlatform();
        final FieldTypeDefinition fieldType = getFieldTypeDefinition(session, argument.type, argument.typeName);
        writer.write(platform.getProcedureArgumentString());
        if (platform.shouldPrintOutputTokenAtStart()) {
            writer.write(" ");
            writer.write(platform.getCreationOutputProcedureToken());
            writer.write(" ");
        }
        writer.write(argument.name);
        if ((!platform.shouldPrintOutputTokenAtStart()) && platform.shouldPrintOutputTokenBeforeType()) {
            writer.write(" ");
            writer.write(platform.getCreationOutputProcedureToken());
        }
        writer.write(" ");
        writer.write(fieldType.getName());
        if (fieldType.isSizeAllowed() && platform.allowsSizeInProcedureArguments() && ((argument.size != 0) || (fieldType.isSizeRequired()))) {
            writer.write("(");
            if (argument.size == 0) {
                writer.write(Integer.toString(fieldType.getDefaultSize()));
            } else {
                writer.write(Integer.toString(argument.size));
            }
            if (argument.subSize != 0) {
                writer.write(",");
                writer.write(Integer.toString(argument.subSize));
            } else if (fieldType.getDefaultSubSize() != 0) {
                writer.write(",");
                writer.write(Integer.toString(fieldType.getDefaultSubSize()));
            }
            writer.write(")");
        }
        if ((!platform.shouldPrintOutputTokenAtStart()) && !platform.shouldPrintOutputTokenBeforeType()) {
            writer.write(" ");
            writer.write(platform.getCreationOutputProcedureToken());
        }
    } catch (IOException ioException) {
        throw ValidationException.fileError(ioException);
    }
}
Also used : FieldTypeDefinition(org.eclipse.persistence.internal.databaseaccess.FieldTypeDefinition) DatabasePlatform(org.eclipse.persistence.internal.databaseaccess.DatabasePlatform) IOException(java.io.IOException)

Aggregations

DatabasePlatform (org.eclipse.persistence.internal.databaseaccess.DatabasePlatform)52 EntityManager (jakarta.persistence.EntityManager)12 DatabaseCall (org.eclipse.persistence.internal.databaseaccess.DatabaseCall)11 DatabaseException (org.eclipse.persistence.exceptions.DatabaseException)9 EntityManagerFactoryImpl (org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl)9 Test (org.junit.Test)9 GenericEntity (org.eclipse.persistence.jpa.test.property.model.GenericEntity)8 PersistenceException (jakarta.persistence.PersistenceException)6 Platform (org.eclipse.persistence.internal.databaseaccess.Platform)6 AbstractSession (org.eclipse.persistence.internal.sessions.AbstractSession)6 ResultSetMetaData (java.sql.ResultSetMetaData)5 DatabaseAccessor (org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor)5 FieldTypeDefinition (org.eclipse.persistence.internal.databaseaccess.FieldTypeDefinition)5 UnitOfWork (org.eclipse.persistence.sessions.UnitOfWork)5 IOException (java.io.IOException)4 ResultSet (java.sql.ResultSet)4 SQLException (java.sql.SQLException)4 ArrayList (java.util.ArrayList)4 List (java.util.List)4 Vector (java.util.Vector)4