Search in sources :

Example 1 with SQLInsertStatement

use of org.eclipse.persistence.internal.expressions.SQLInsertStatement in project eclipselink by eclipse-ee4j.

the class HistoryPolicy method mappingLogicalInsert.

/**
 * INTERNAL:
 * Performs a logical insert into the historical schema.  Direct
 * collections and many to many mappings are maintained through the session
 * events.
 */
public void mappingLogicalInsert(DataModifyQuery originalQuery, AbstractRecord arguments, AbstractSession session) {
    DataModifyQuery historyQuery = new DataModifyQuery();
    SQLInsertStatement historyStatement = new SQLInsertStatement();
    DatabaseTable histTable = getHistoricalTables().get(0);
    historyStatement.setTable(histTable);
    AbstractRecord modifyRow = originalQuery.getModifyRow().clone();
    AbstractRecord translationRow = arguments.clone();
    // Start could be the version field in timestamp locking.
    if (!modifyRow.containsKey(getStart())) {
        Object time = getCurrentTime(session);
        modifyRow.add(getStart(), time);
        translationRow.add(getStart(), time);
    }
    historyQuery.setSQLStatement(historyStatement);
    historyQuery.setModifyRow(modifyRow);
    historyStatement.setModifyRow(modifyRow);
    session.executeQuery(historyQuery, translationRow);
}
Also used : AbstractRecord(org.eclipse.persistence.internal.sessions.AbstractRecord) SQLInsertStatement(org.eclipse.persistence.internal.expressions.SQLInsertStatement) DatabaseTable(org.eclipse.persistence.internal.helper.DatabaseTable) HistoricalDatabaseTable(org.eclipse.persistence.internal.history.HistoricalDatabaseTable) DataModifyQuery(org.eclipse.persistence.queries.DataModifyQuery)

Example 2 with SQLInsertStatement

use of org.eclipse.persistence.internal.expressions.SQLInsertStatement in project eclipselink by eclipse-ee4j.

the class RelationTableMechanism method initializeInsertQuery.

/**
 * INTERNAL:
 * Initialize insert query. This query is used to insert the collection of objects into the
 * relation table.
 */
protected void initializeInsertQuery(AbstractSession session, ForeignReferenceMapping mapping) {
    if (!getInsertQuery().hasSessionName()) {
        getInsertQuery().setSessionName(session.getName());
    }
    if (getInsertQuery().getPartitioningPolicy() == null) {
        getInsertQuery().setPartitioningPolicy(mapping.getPartitioningPolicy());
    }
    getInsertQuery().setName(mapping.getAttributeName());
    if (hasCustomInsertQuery()) {
        return;
    }
    SQLInsertStatement statement = new SQLInsertStatement();
    statement.setTable(getRelationTable());
    AbstractRecord joinRow = new DatabaseRecord();
    for (DatabaseField field : getTargetRelationKeyFields()) {
        joinRow.put(field, null);
    }
    for (DatabaseField field : getSourceRelationKeyFields()) {
        joinRow.put(field, null);
    }
    if (mapping.isCollectionMapping()) {
        CollectionMapping collectionMapping = (CollectionMapping) mapping;
        if (collectionMapping.getListOrderField() != null) {
            joinRow.put(collectionMapping.getListOrderField(), null);
        }
        collectionMapping.getContainerPolicy().addFieldsForMapKey(joinRow);
    }
    statement.setModifyRow(joinRow);
    getInsertQuery().setSQLStatement(statement);
    getInsertQuery().setModifyRow(joinRow);
}
Also used : DatabaseRecord(org.eclipse.persistence.sessions.DatabaseRecord) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) AbstractRecord(org.eclipse.persistence.internal.sessions.AbstractRecord) SQLInsertStatement(org.eclipse.persistence.internal.expressions.SQLInsertStatement)

Example 3 with SQLInsertStatement

use of org.eclipse.persistence.internal.expressions.SQLInsertStatement in project eclipselink by eclipse-ee4j.

the class DirectCollectionMapping method initializeInsertQuery.

/**
 * Initialize insert query. This query is used to insert the collection of objects into the
 * reference table.
 */
protected void initializeInsertQuery(AbstractSession session) {
    if (!getInsertQuery().hasSessionName()) {
        getInsertQuery().setSessionName(session.getName());
    }
    if (getInsertQuery().getPartitioningPolicy() == null) {
        getInsertQuery().setPartitioningPolicy(getPartitioningPolicy());
    }
    if (hasCustomInsertQuery()) {
        return;
    }
    SQLInsertStatement statement = new SQLInsertStatement();
    statement.setTable(getReferenceTable());
    AbstractRecord directRow = new DatabaseRecord();
    for (Enumeration<DatabaseField> referenceEnum = getReferenceKeyFields().elements(); referenceEnum.hasMoreElements(); ) {
        directRow.put(referenceEnum.nextElement(), null);
    }
    directRow.put(getDirectField(), null);
    if (listOrderField != null) {
        directRow.put(listOrderField, null);
    }
    statement.setModifyRow(directRow);
    getInsertQuery().setSQLStatement(statement);
    getInsertQuery().setModifyRow(directRow);
}
Also used : DatabaseRecord(org.eclipse.persistence.sessions.DatabaseRecord) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) AbstractRecord(org.eclipse.persistence.internal.sessions.AbstractRecord) SQLInsertStatement(org.eclipse.persistence.internal.expressions.SQLInsertStatement)

Example 4 with SQLInsertStatement

use of org.eclipse.persistence.internal.expressions.SQLInsertStatement in project eclipselink by eclipse-ee4j.

the class ExpressionQueryMechanism method buildInsertStatement.

/**
 * Return the appropriate insert statement
 */
protected SQLInsertStatement buildInsertStatement(DatabaseTable table) {
    SQLInsertStatement insertStatement = new SQLInsertStatement();
    insertStatement.setTable(table);
    insertStatement.setModifyRow(getModifyRow());
    if (getDescriptor().hasReturningPolicies() && getDescriptor().getReturnFieldsToGenerateInsert() != null) {
        // In case of RelationalDescriptor only return fields for current table must be used.
        Vector<DatabaseField> returnFieldsForTable = new NonSynchronizedVector<>();
        for (DatabaseField item : getDescriptor().getReturnFieldsToGenerateInsert()) {
            if (table.equals(item.getTable())) {
                returnFieldsForTable.add(item);
            }
        }
        if (!returnFieldsForTable.isEmpty()) {
            insertStatement.setReturnFields(getDescriptor().getReturnFieldsToGenerateInsert());
        }
    }
    insertStatement.setHintString(getQuery().getHintString());
    return insertStatement;
}
Also used : DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) SQLInsertStatement(org.eclipse.persistence.internal.expressions.SQLInsertStatement) NonSynchronizedVector(org.eclipse.persistence.internal.helper.NonSynchronizedVector)

Example 5 with SQLInsertStatement

use of org.eclipse.persistence.internal.expressions.SQLInsertStatement in project eclipselink by eclipse-ee4j.

the class ExpressionQueryMechanism method prepareInsertObject.

/**
 * Pre-build the SQL statement from the expression.
 */
@Override
public void prepareInsertObject() {
    // Require modify row to prepare.
    if (getModifyRow() == null) {
        return;
    }
    // Add and prepare to a call a update statement for each table.
    // In the case of multiple tables, build the sql statements in insert order.
    ClassDescriptor descriptor = getDescriptor();
    if (descriptor.getTables().size() == 1) {
        setSQLStatement(buildInsertStatement(descriptor.getTables().get(0)));
    } else {
        for (DatabaseTable table : descriptor.getMultipleTableInsertOrder()) {
            SQLInsertStatement insertStatement = buildInsertStatement(table);
            getSQLStatements().addElement(insertStatement);
        }
    }
    super.prepareInsertObject();
}
Also used : ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) DatabaseTable(org.eclipse.persistence.internal.helper.DatabaseTable) SQLInsertStatement(org.eclipse.persistence.internal.expressions.SQLInsertStatement)

Aggregations

SQLInsertStatement (org.eclipse.persistence.internal.expressions.SQLInsertStatement)6 AbstractRecord (org.eclipse.persistence.internal.sessions.AbstractRecord)4 DatabaseField (org.eclipse.persistence.internal.helper.DatabaseField)3 DatabaseTable (org.eclipse.persistence.internal.helper.DatabaseTable)3 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)2 HistoricalDatabaseTable (org.eclipse.persistence.internal.history.HistoricalDatabaseTable)2 DatabaseRecord (org.eclipse.persistence.sessions.DatabaseRecord)2 NonSynchronizedVector (org.eclipse.persistence.internal.helper.NonSynchronizedVector)1 StatementQueryMechanism (org.eclipse.persistence.internal.queries.StatementQueryMechanism)1 DataModifyQuery (org.eclipse.persistence.queries.DataModifyQuery)1