use of org.eclipse.persistence.sessions.DatabaseRecord in project eclipselink by eclipse-ee4j.
the class DirectCollectionMapping method initializeUpdateAtIndexQuery.
protected void initializeUpdateAtIndexQuery(AbstractSession session) {
if (!getUpdateAtIndexQuery().hasSessionName()) {
getUpdateAtIndexQuery().setSessionName(session.getName());
}
if (getUpdateAtIndexQuery().getPartitioningPolicy() == null) {
getUpdateAtIndexQuery().setPartitioningPolicy(getPartitioningPolicy());
}
if (hasCustomUpdateAtIndexQuery()) {
return;
}
SQLUpdateStatement statement = new SQLUpdateStatement();
ExpressionBuilder builder = new ExpressionBuilder();
Expression expression = createWhereClauseForDeleteQuery(builder);
expression = expression.and(builder.getField(this.listOrderField).equal(builder.getParameter(this.listOrderField)));
statement.setWhereClause(expression);
statement.setTable(getReferenceTable());
AbstractRecord modifyRow = new DatabaseRecord();
modifyRow.add(this.listOrderField, null);
statement.setModifyRow(modifyRow);
getUpdateAtIndexQuery().setSQLStatement(statement);
}
use of org.eclipse.persistence.sessions.DatabaseRecord 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);
}
use of org.eclipse.persistence.sessions.DatabaseRecord in project eclipselink by eclipse-ee4j.
the class AggregateCollectionMapping method getAggregateRow.
/**
* INTERNAL:
* return the aggregate Record with the primary keys from the source table and target table
*/
public AbstractRecord getAggregateRow(ObjectLevelModifyQuery query, Object object) {
Vector referenceObjectKeys = getReferenceObjectKeys(query);
AbstractRecord aggregateRow = new DatabaseRecord();
Vector<DatabaseField> keys = getTargetForeignKeyFields();
for (int keyIndex = 0; keyIndex < keys.size(); keyIndex++) {
aggregateRow.put(keys.elementAt(keyIndex), referenceObjectKeys.elementAt(keyIndex));
}
getReferenceDescriptor(object.getClass(), query.getSession()).getObjectBuilder().buildRow(aggregateRow, object, query.getSession(), WriteType.UNDEFINED);
return aggregateRow;
}
use of org.eclipse.persistence.sessions.DatabaseRecord in project eclipselink by eclipse-ee4j.
the class AggregateCollectionMapping method compareListsAndWrite_NonUpdatableListOrderField.
/**
* INTERNAL:
* Old and new lists are compared and only the changes are written to the database.
* Called only if listOrderField != null
*/
protected void compareListsAndWrite_NonUpdatableListOrderField(List previousList, List currentList, WriteObjectQuery query) throws DatabaseException, OptimisticLockException {
boolean shouldRepairOrder = false;
if (currentList instanceof IndirectList) {
shouldRepairOrder = ((IndirectList) currentList).isListOrderBrokenInDb();
}
HashMap<Object, Object[]> previousAndCurrentByKey = new HashMap<>();
int pkSize = getReferenceDescriptor().getPrimaryKeyFields().size();
// First index the current objects by their primary key.
for (int i = 0; i < currentList.size(); i++) {
Object currentObject = currentList.get(i);
try {
CacheId primaryKey = (CacheId) getReferenceDescriptor().getObjectBuilder().extractPrimaryKeyFromObject(currentObject, query.getSession());
primaryKey.add(i);
Object[] previousAndCurrent = new Object[] { null, currentObject };
previousAndCurrentByKey.put(primaryKey, previousAndCurrent);
} catch (NullPointerException e) {
// ideally the customer should check for these themselves.
if (currentObject != null) {
throw e;
}
}
}
if (shouldRepairOrder) {
DeleteAllQuery deleteAllQuery = (DeleteAllQuery) this.deleteAllQuery;
if (this.isCascadeOnDeleteSetOnDatabase) {
deleteAllQuery = (DeleteAllQuery) deleteAllQuery.clone();
deleteAllQuery.setIsInMemoryOnly(false);
}
deleteAllQuery.executeDeleteAll(query.getSession().getSessionForClass(getReferenceClass()), query.getTranslationRow(), new Vector(previousList));
} else {
// Next index the previous objects (read from db or from backup in uow)
for (int i = 0; i < previousList.size(); i++) {
Object previousObject = previousList.get(i);
CacheId primaryKey = (CacheId) getReferenceDescriptor().getObjectBuilder().extractPrimaryKeyFromObject(previousObject, query.getSession());
primaryKey.add(i);
Object[] previousAndCurrent = previousAndCurrentByKey.get(primaryKey);
if (previousAndCurrent == null) {
// there's no current object - that means that previous object should be deleted
DatabaseRecord extraData = new DatabaseRecord(1);
extraData.put(this.listOrderField, i);
objectRemovedDuringUpdate(query, previousObject, extraData);
} else {
previousAndCurrent[0] = previousObject;
}
}
}
Iterator<Map.Entry<Object, Object[]>> it = previousAndCurrentByKey.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<Object, Object[]> entry = it.next();
Object key = entry.getKey();
Object[] previousAndCurrent = entry.getValue();
// previousObject may be null, meaning currentObject has been added to the list
Object previousObject = previousAndCurrent[0];
// currentObject is not null
Object currentObject = previousAndCurrent[1];
if (previousObject == null) {
// there's no previous object - that means that current object should be added.
// index of currentObject in currentList
int iCurrent = (Integer) ((CacheId) key).getPrimaryKey()[pkSize];
DatabaseRecord extraData = new DatabaseRecord(1);
extraData.put(this.listOrderField, iCurrent);
objectAddedDuringUpdate(query, currentObject, null, extraData);
} else {
if (!this.isEntireObjectPK) {
objectUnchangedDuringUpdate(query, currentObject, previousObject);
}
}
}
if (shouldRepairOrder) {
((IndirectList) currentList).setIsListOrderBrokenInDb(false);
}
}
use of org.eclipse.persistence.sessions.DatabaseRecord in project eclipselink by eclipse-ee4j.
the class AggregateCollectionMapping method getAndPrepareModifyQueryForInsert.
/**
* INTERNAL:
* setup the modifyQuery for post insert/update and pre delete
*/
public InsertObjectQuery getAndPrepareModifyQueryForInsert(ObjectLevelModifyQuery originalQuery, Object object) {
AbstractSession session = originalQuery.getSession();
ClassDescriptor objReferenceDescriptor = getReferenceDescriptor(object.getClass(), session);
InsertObjectQuery insertQuery = getInsertObjectQuery(session, objReferenceDescriptor);
insertQuery.setObject(object);
insertQuery.setDescriptor(objReferenceDescriptor);
AbstractRecord targetForeignKeyRow = new DatabaseRecord();
Vector referenceObjectKeys = getReferenceObjectKeys(originalQuery);
for (int keyIndex = 0; keyIndex < getTargetForeignKeyFields().size(); keyIndex++) {
targetForeignKeyRow.put(getTargetForeignKeyFields().elementAt(keyIndex), referenceObjectKeys.elementAt(keyIndex));
}
insertQuery.setModifyRow(targetForeignKeyRow);
insertQuery.setTranslationRow(targetForeignKeyRow);
insertQuery.setSession(session);
insertQuery.setCascadePolicy(originalQuery.getCascadePolicy());
insertQuery.dontMaintainCache();
// way to get a backup directly from a clone.
if (session.isUnitOfWork()) {
Object backupAttributeValue = getReferenceDescriptor(object.getClass(), session).getObjectBuilder().buildNewInstance();
insertQuery.setBackupClone(backupAttributeValue);
}
return insertQuery;
}
Aggregations