Search in sources :

Example 76 with DatabaseRecord

use of org.eclipse.persistence.sessions.DatabaseRecord in project eclipselink by eclipse-ee4j.

the class DatabaseAccessor method fetchRow.

/**
 * Return a new DatabaseRow.<p>
 * Populate the row from the data in cursor. The fields representing the results
 * and the order of the results are stored in fields.
 * <p><b>NOTE</b>:
 * Make sure that the field name is set.  An empty field name placeholder is
 * used in the sortFields() method when the number of fields defined does not
 * match the number of column names available on the database.
 * PERF: This method must be highly optimized.
 */
protected AbstractRecord fetchRow(Vector<DatabaseField> fields, ResultSet resultSet, ResultSetMetaData metaData, AbstractSession session) throws DatabaseException {
    int size = fields.size();
    Vector<Object> values = NonSynchronizedVector.newInstance(size);
    // PERF: Pass platform and optimize data flag.
    DatabasePlatform platform = getPlatform();
    boolean optimizeData = platform.shouldOptimizeDataConversion();
    for (int index = 0; index < size; index++) {
        DatabaseField field = fields.elementAt(index);
        // Field can be null for fetch groups.
        if (field != null) {
            values.add(getObject(resultSet, field, metaData, index + 1, platform, optimizeData, session));
        } else {
            values.add(null);
        }
    }
    // Row creation is optimized through sharing the same fields for the entire result set.
    return new DatabaseRecord(fields, values);
}
Also used : DatabaseRecord(org.eclipse.persistence.sessions.DatabaseRecord) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField)

Example 77 with DatabaseRecord

use of org.eclipse.persistence.sessions.DatabaseRecord in project eclipselink by eclipse-ee4j.

the class MongoPlatform method buildCallFromStatement.

/**
 * INTERNAL:
 * Override this method to throw an exception by default.
 * Platforms that support dynamic querying can override this to generate an EISInteraction.
 */
@Override
public DatasourceCall buildCallFromStatement(SQLStatement statement, DatabaseQuery query, AbstractSession session) {
    if (query.isObjectLevelReadQuery()) {
        ObjectLevelReadQuery readQuery = (ObjectLevelReadQuery) query;
        MappedInteraction interaction = new MappedInteraction();
        interaction.setProperty(OPERATION, MongoOperation.FIND);
        interaction.setProperty(COLLECTION, ((EISDescriptor) query.getDescriptor()).getDataTypeName());
        if (readQuery.getFirstResult() > 0) {
            interaction.setProperty(SKIP, readQuery.getFirstResult());
        }
        if (readQuery.getMaxRows() > 0) {
            interaction.setProperty(LIMIT, readQuery.getMaxRows());
        }
        if (readQuery.getFetchSize() > 0) {
            interaction.setProperty(BATCH_SIZE, readQuery.getMaxRows());
        }
        DatabaseRecord row = new DatabaseRecord();
        if (statement.getWhereClause() != null) {
            appendExpressionToQueryRow(statement.getWhereClause(), row, query);
        }
        if (readQuery.hasOrderByExpressions()) {
            DatabaseRecord sort = new DatabaseRecord();
            for (Expression orderBy : readQuery.getOrderByExpressions()) {
                appendExpressionToSortRow(orderBy, sort, query);
            }
            row.put(MongoRecord.SORT, sort);
        }
        if (readQuery.isReportQuery()) {
            DatabaseRecord select = new DatabaseRecord();
            for (Object field : ((SQLSelectStatement) statement).getFields()) {
                if (field instanceof DatabaseField) {
                    select.put((DatabaseField) field, 1);
                } else if (field instanceof Expression) {
                    Object value = extractValueFromExpression((Expression) field, readQuery);
                    if (!(value instanceof DatabaseField)) {
                        throw new EISException("Query too complex for Mongo translation, only field selects are supported in query: " + query);
                    }
                    select.put((DatabaseField) value, 1);
                }
            }
            row.put("$select", select);
        }
        interaction.setInputRow(row);
        return interaction;
    }
    throw new EISException("Query too complex for Mongo translation, only select queries are supported in query: " + query);
}
Also used : ObjectLevelReadQuery(org.eclipse.persistence.queries.ObjectLevelReadQuery) MappedInteraction(org.eclipse.persistence.eis.interactions.MappedInteraction) DatabaseRecord(org.eclipse.persistence.sessions.DatabaseRecord) RelationExpression(org.eclipse.persistence.internal.expressions.RelationExpression) FieldExpression(org.eclipse.persistence.internal.expressions.FieldExpression) QueryKeyExpression(org.eclipse.persistence.internal.expressions.QueryKeyExpression) ParameterExpression(org.eclipse.persistence.internal.expressions.ParameterExpression) LogicalExpression(org.eclipse.persistence.internal.expressions.LogicalExpression) FunctionExpression(org.eclipse.persistence.internal.expressions.FunctionExpression) ConstantExpression(org.eclipse.persistence.internal.expressions.ConstantExpression) Expression(org.eclipse.persistence.expressions.Expression) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) SQLSelectStatement(org.eclipse.persistence.internal.expressions.SQLSelectStatement) EISException(org.eclipse.persistence.eis.EISException)

Example 78 with DatabaseRecord

use of org.eclipse.persistence.sessions.DatabaseRecord in project eclipselink by eclipse-ee4j.

the class MongoPlatform method appendExpressionToQueryRow.

/**
 * Append the expression and recursively to the query row.
 */
protected void appendExpressionToQueryRow(Expression expression, AbstractRecord row, DatabaseQuery query) {
    if (expression.isRelationExpression()) {
        RelationExpression relation = (RelationExpression) expression;
        Object left = extractValueFromExpression(relation.getFirstChild(), query);
        Object right = extractValueFromExpression(relation.getSecondChild(), query);
        if (relation.getOperator().getSelector() == ExpressionOperator.Equal) {
            row.put(left, right);
        } else {
            DatabaseRecord nested = new DatabaseRecord();
            if (relation.getOperator().getSelector() == ExpressionOperator.GreaterThan) {
                nested.put("$gt", right);
            } else if (relation.getOperator().getSelector() == ExpressionOperator.LessThan) {
                nested.put("$lt", right);
            } else if (relation.getOperator().getSelector() == ExpressionOperator.LessThanEqual) {
                nested.put("$lte", right);
            } else if (relation.getOperator().getSelector() == ExpressionOperator.GreaterThanEqual) {
                nested.put("$gte", right);
            } else if (relation.getOperator().getSelector() == ExpressionOperator.NotEqual) {
                nested.put("$ne", right);
            } else if (relation.getOperator().getSelector() == ExpressionOperator.In) {
                nested.put("$in", right);
                row.put(left, nested);
            } else if (relation.getOperator().getSelector() == ExpressionOperator.NotIn) {
                nested.put("$nin", right);
                row.put(left, nested);
            } else {
                throw new EISException("Query too complex for Mongo translation, relation [" + expression + "] not supported in query: " + query);
            }
            row.put(left, nested);
        }
    } else if (expression.isLogicalExpression()) {
        LogicalExpression logic = (LogicalExpression) expression;
        DatabaseRecord first = new DatabaseRecord();
        DatabaseRecord second = new DatabaseRecord();
        appendExpressionToQueryRow(logic.getFirstChild(), first, query);
        appendExpressionToQueryRow(logic.getSecondChild(), second, query);
        List<DatabaseRecord> nested = new Vector<>();
        nested.add(first);
        nested.add(second);
        if (logic.getOperator().getSelector() == ExpressionOperator.And) {
            row.put("$and", nested);
        } else if (logic.getOperator().getSelector() == ExpressionOperator.Or) {
            row.put("$or", nested);
        } else {
            throw new EISException("Query too complex for Mongo translation, logic [" + expression + "] not supported in query: " + query);
        }
    } else if (expression.isFunctionExpression()) {
        FunctionExpression function = (FunctionExpression) expression;
        if (function.getOperator().getSelector() == ExpressionOperator.Like) {
            Object left = extractValueFromExpression(function.getChildren().get(0), query);
            Object right = extractValueFromExpression(function.getChildren().get(1), query);
            if (!(right instanceof String)) {
                throw new EISException("Query too complex for Mongo translation, like with [" + right + "] not supported in query: " + query);
            }
            String pattern = (String) right;
            DatabaseRecord nested = new DatabaseRecord();
            if (!this.isLikeRegex) {
                pattern = Helper.convertLikeToRegex(pattern);
            }
            nested.put("$regex", pattern);
            row.put(left, nested);
        } else if (function.getOperator().getSelector() == ExpressionOperator.Not) {
            // Detect situation 'not(a = b)' and change it to '(a != b)'
            Expression expr = function.getChildren().get(0);
            if (expr.isRelationExpression()) {
                RelationExpression relation = (RelationExpression) expr;
                Object left = extractValueFromExpression(relation.getFirstChild(), query);
                Object right = extractValueFromExpression(relation.getSecondChild(), query);
                DatabaseRecord nested = new DatabaseRecord();
                if (expr.getOperator().getSelector() == ExpressionOperator.Equal) {
                    nested.put("$ne", right);
                } else {
                    nested.put("not", right);
                }
                row.put(left, nested);
            } else {
                throw new EISException("Query too complex for Mongo translation, function [" + expression + "] not supported in query: " + query);
            }
        } else {
            throw new EISException("Query too complex for Mongo translation, function [" + expression + "] not supported in query: " + query);
        }
    } else {
        throw new EISException("Query too complex for Mongo translation, expression [" + expression + "] not supported in query: " + query);
    }
}
Also used : LogicalExpression(org.eclipse.persistence.internal.expressions.LogicalExpression) FunctionExpression(org.eclipse.persistence.internal.expressions.FunctionExpression) DatabaseRecord(org.eclipse.persistence.sessions.DatabaseRecord) RelationExpression(org.eclipse.persistence.internal.expressions.RelationExpression) RelationExpression(org.eclipse.persistence.internal.expressions.RelationExpression) FieldExpression(org.eclipse.persistence.internal.expressions.FieldExpression) QueryKeyExpression(org.eclipse.persistence.internal.expressions.QueryKeyExpression) ParameterExpression(org.eclipse.persistence.internal.expressions.ParameterExpression) LogicalExpression(org.eclipse.persistence.internal.expressions.LogicalExpression) FunctionExpression(org.eclipse.persistence.internal.expressions.FunctionExpression) ConstantExpression(org.eclipse.persistence.internal.expressions.ConstantExpression) Expression(org.eclipse.persistence.expressions.Expression) EISException(org.eclipse.persistence.eis.EISException) List(java.util.List)

Example 79 with DatabaseRecord

use of org.eclipse.persistence.sessions.DatabaseRecord in project eclipselink by eclipse-ee4j.

the class ObjectBuilder method cacheForeignKeyValues.

/**
 * INTERNAL:
 * This method is used to store the FK values used for this mapping in the cachekey.
 * This is used when the mapping is protected but we have retrieved the fk values and will cache
 * them for use when the entity is cloned.
 */
public void cacheForeignKeyValues(AbstractRecord databaseRecord, CacheKey cacheKey, AbstractSession session) {
    Set<DatabaseField> foreignKeys = this.descriptor.getForeignKeyValuesForCaching();
    if (foreignKeys.isEmpty()) {
        return;
    }
    DatabaseRecord cacheRecord = new DatabaseRecord(foreignKeys.size());
    for (DatabaseField field : foreignKeys) {
        cacheRecord.put(field, databaseRecord.get(field));
    }
    cacheKey.setProtectedForeignKeys(cacheRecord);
}
Also used : DatabaseRecord(org.eclipse.persistence.sessions.DatabaseRecord) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField)

Example 80 with DatabaseRecord

use of org.eclipse.persistence.sessions.DatabaseRecord in project eclipselink by eclipse-ee4j.

the class AggregateCollectionMapping method preDelete.

/**
 * INTERNAL:
 * Delete privately owned parts
 */
@Override
public void preDelete(DeleteObjectQuery query) throws DatabaseException, OptimisticLockException {
    if (isReadOnly()) {
        return;
    }
    AbstractSession session = query.getSession();
    // If privately owned parts have their privately own parts, delete those one by one
    // else delete everything in one shot.
    int index = 0;
    if (mustDeleteReferenceObjectsOneByOne()) {
        Object objects = getRealCollectionAttributeValueFromObject(query.getObject(), query.getSession());
        ContainerPolicy cp = getContainerPolicy();
        if (this.isCascadeOnDeleteSetOnDatabase && session.isUnitOfWork()) {
            for (Object iterator = cp.iteratorFor(objects); cp.hasNext(iterator); ) {
                Object wrappedObject = cp.nextEntry(iterator, session);
                Object object = cp.unwrapIteratorResult(wrappedObject);
                ((UnitOfWorkImpl) session).getCascadeDeleteObjects().add(object);
            }
        }
        for (Object iter = cp.iteratorFor(objects); cp.hasNext(iter); ) {
            Object wrappedObject = cp.nextEntry(iter, session);
            DeleteObjectQuery deleteQuery = new DeleteObjectQuery();
            deleteQuery.setIsExecutionClone(true);
            Map extraData = null;
            if (this.listOrderField != null) {
                extraData = new DatabaseRecord(1);
                extraData.put(this.listOrderField, index++);
            }
            prepareModifyQueryForDelete(query, deleteQuery, wrappedObject, extraData);
            session.executeQuery(deleteQuery, deleteQuery.getTranslationRow());
            cp.propogatePreDelete(query, wrappedObject);
        }
        if (!session.isUnitOfWork()) {
            // This deletes any objects on the database, as the collection in memory may has been changed.
            // This is not required for unit of work, as the update would have already deleted these objects,
            // and the backup copy will include the same objects causing double deletes.
            verifyDeleteForUpdate(query);
        }
    } else {
        deleteAll(query, session);
    }
}
Also used : ContainerPolicy(org.eclipse.persistence.internal.queries.ContainerPolicy) DatabaseRecord(org.eclipse.persistence.sessions.DatabaseRecord) DeleteObjectQuery(org.eclipse.persistence.queries.DeleteObjectQuery) Map(java.util.Map) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession)

Aggregations

DatabaseRecord (org.eclipse.persistence.sessions.DatabaseRecord)110 DatabaseField (org.eclipse.persistence.internal.helper.DatabaseField)44 AbstractRecord (org.eclipse.persistence.internal.sessions.AbstractRecord)43 Vector (java.util.Vector)31 ArrayList (java.util.ArrayList)17 NonSynchronizedVector (org.eclipse.persistence.internal.helper.NonSynchronizedVector)17 BigDecimal (java.math.BigDecimal)14 List (java.util.List)14 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)14 DatabaseSession (org.eclipse.persistence.sessions.DatabaseSession)14 Test (org.junit.Test)14 Expression (org.eclipse.persistence.expressions.Expression)12 AbstractSession (org.eclipse.persistence.internal.sessions.AbstractSession)12 HashMap (java.util.HashMap)11 Map (java.util.Map)10 EclipseLinkException (org.eclipse.persistence.exceptions.EclipseLinkException)7 ExpressionBuilder (org.eclipse.persistence.expressions.ExpressionBuilder)7 RelationalDescriptor (org.eclipse.persistence.descriptors.RelationalDescriptor)6 DescriptorException (org.eclipse.persistence.exceptions.DescriptorException)6 SQLUpdateStatement (org.eclipse.persistence.internal.expressions.SQLUpdateStatement)6