Search in sources :

Example 6 with DbRowOp

use of org.apache.cayenne.access.flush.operation.DbRowOp in project cayenne by apache.

the class ArcValuesCreationHandler method processRelationship.

protected void processRelationship(DbRelationship dbRelationship, ObjectId srcId, ObjectId targetId, boolean add) {
    for (DbJoin join : dbRelationship.getJoins()) {
        boolean srcPK = join.getSource().isPrimaryKey();
        boolean targetPK = join.getTarget().isPrimaryKey();
        Object valueToUse;
        DbRowOp rowOp;
        DbAttribute attribute;
        ObjectId id;
        boolean processDelete;
        // but get value from DbRow, not ObjID
        if (srcPK != targetPK) {
            // case 1
            processDelete = true;
            id = null;
            if (srcPK) {
                valueToUse = ObjectIdValueSupplier.getFor(srcId, join.getSourceName());
                rowOp = factory.getOrCreate(dbRelationship.getTargetEntity(), targetId, DbRowOpType.UPDATE);
                attribute = join.getTarget();
            } else {
                valueToUse = ObjectIdValueSupplier.getFor(targetId, join.getTargetName());
                rowOp = factory.getOrCreate(dbRelationship.getSourceEntity(), srcId, defaultType);
                attribute = join.getSource();
            }
        } else {
            // case 2 and 3
            processDelete = false;
            if (dbRelationship.isToDependentPK()) {
                valueToUse = ObjectIdValueSupplier.getFor(srcId, join.getSourceName());
                rowOp = factory.getOrCreate(dbRelationship.getTargetEntity(), targetId, DbRowOpType.UPDATE);
                attribute = join.getTarget();
                id = targetId;
                if (dbRelationship.isToMany()) {
                    // strange mapping toDepPK and toMany, but just skip it
                    rowOp = null;
                }
            } else {
                valueToUse = ObjectIdValueSupplier.getFor(targetId, join.getTargetName());
                rowOp = factory.getOrCreate(dbRelationship.getSourceEntity(), srcId, defaultType);
                attribute = join.getSource();
                id = srcId;
                if (dbRelationship.getReverseRelationship().isToMany()) {
                    // strange mapping toDepPK and toMany, but just skip it
                    rowOp = null;
                }
            }
        }
        // propagated master -> child PK
        if (id != null && attribute.isPrimaryKey()) {
            id.getReplacementIdMap().put(attribute.getName(), valueToUse);
        }
        if (rowOp != null) {
            rowOp.accept(new ValuePropagationVisitor(attribute, add, valueToUse, processDelete));
        }
    }
}
Also used : ObjectId(org.apache.cayenne.ObjectId) DbRowOp(org.apache.cayenne.access.flush.operation.DbRowOp) DeleteDbRowOp(org.apache.cayenne.access.flush.operation.DeleteDbRowOp) UpdateDbRowOp(org.apache.cayenne.access.flush.operation.UpdateDbRowOp) InsertDbRowOp(org.apache.cayenne.access.flush.operation.InsertDbRowOp) DbAttribute(org.apache.cayenne.map.DbAttribute) DbJoin(org.apache.cayenne.map.DbJoin)

Example 7 with DbRowOp

use of org.apache.cayenne.access.flush.operation.DbRowOp in project cayenne by apache.

the class DefaultDataDomainFlushAction method flush.

@Override
public GraphDiff flush(DataContext context, GraphDiff changes) {
    CompoundDiff afterCommitDiff = new CompoundDiff();
    if (changes == null) {
        return afterCommitDiff;
    }
    if (!(changes instanceof ObjectStoreGraphDiff)) {
        throw new CayenneRuntimeException("Instance of ObjectStoreGraphDiff expected, got %s", changes.getClass());
    }
    ObjectStore objectStore = context.getObjectStore();
    ObjectStoreGraphDiff objectStoreGraphDiff = (ObjectStoreGraphDiff) changes;
    List<DbRowOp> dbRowOps = createDbRowOps(objectStore, objectStoreGraphDiff);
    updateObjectIds(dbRowOps);
    List<DbRowOp> deduplicatedOps = mergeSameObjectIds(dbRowOps);
    List<DbRowOp> filteredOps = filterOps(deduplicatedOps);
    List<DbRowOp> sortedOps = sort(filteredOps);
    List<? extends Query> queries = createQueries(sortedOps);
    executeQueries(queries);
    createReplacementIds(objectStore, afterCommitDiff, sortedOps);
    postprocess(context, objectStoreGraphDiff, afterCommitDiff, sortedOps);
    return afterCommitDiff;
}
Also used : ObjectStoreGraphDiff(org.apache.cayenne.access.ObjectStoreGraphDiff) ObjectStore(org.apache.cayenne.access.ObjectStore) DbRowOp(org.apache.cayenne.access.flush.operation.DbRowOp) UpdateDbRowOp(org.apache.cayenne.access.flush.operation.UpdateDbRowOp) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) CompoundDiff(org.apache.cayenne.graph.CompoundDiff)

Example 8 with DbRowOp

use of org.apache.cayenne.access.flush.operation.DbRowOp in project cayenne by apache.

the class DefaultDbRowOpSorterTest method sortByOpType.

@Test
public void sortByOpType() {
    ObjectId id1 = ObjectId.of("test", "id", 1);
    ObjectId id2 = ObjectId.of("test", "id", 2);
    ObjectId id3 = ObjectId.of("test", "id", 3);
    ObjectId id4 = ObjectId.of("test", "id", 4);
    ObjectId id5 = ObjectId.of("test", "id", 5);
    ObjectId id6 = ObjectId.of("test", "id", 6);
    DbEntity test = mockEntity("test");
    DbRowOp op1 = new InsertDbRowOp(mockObject(id1), test, id1);
    DbRowOp op2 = new UpdateDbRowOp(mockObject(id2), test, id2);
    DbRowOp op3 = new DeleteDbRowOp(mockObject(id3), test, id3);
    DbRowOp op4 = new InsertDbRowOp(mockObject(id4), test, id4);
    DbRowOp op5 = new UpdateDbRowOp(mockObject(id5), test, id5);
    DbRowOp op6 = new DeleteDbRowOp(mockObject(id6), test, id6);
    List<DbRowOp> rows = Arrays.asList(op1, op2, op3, op4, op5, op6);
    List<DbRowOp> expected = Arrays.asList(op1, op4, op2, op5, op3, op6);
    List<DbRowOp> sorted = sorter.sort(rows);
    assertEquals(expected, sorted);
}
Also used : InsertDbRowOp(org.apache.cayenne.access.flush.operation.InsertDbRowOp) UpdateDbRowOp(org.apache.cayenne.access.flush.operation.UpdateDbRowOp) DbEntity(org.apache.cayenne.map.DbEntity) ObjectId(org.apache.cayenne.ObjectId) DbRowOp(org.apache.cayenne.access.flush.operation.DbRowOp) DeleteDbRowOp(org.apache.cayenne.access.flush.operation.DeleteDbRowOp) BaseDbRowOp(org.apache.cayenne.access.flush.operation.BaseDbRowOp) UpdateDbRowOp(org.apache.cayenne.access.flush.operation.UpdateDbRowOp) InsertDbRowOp(org.apache.cayenne.access.flush.operation.InsertDbRowOp) DeleteDbRowOp(org.apache.cayenne.access.flush.operation.DeleteDbRowOp) Test(org.junit.Test)

Example 9 with DbRowOp

use of org.apache.cayenne.access.flush.operation.DbRowOp in project cayenne by apache.

the class DefaultDbRowOpSorterTest method sortByIdDifferentEntities.

@Test
public void sortByIdDifferentEntities() {
    ObjectId id1 = ObjectId.of("test2", "id", 1);
    ObjectId id2 = ObjectId.of("test", "id", 2);
    ObjectId id3 = ObjectId.of("test", "id", 2);
    ObjectId id4 = ObjectId.of("test", "id", 3);
    ObjectId id5 = ObjectId.of("test2", "id", 4);
    ObjectId id6 = ObjectId.of("test", "id", 5);
    ObjectId id7 = ObjectId.of("test", "id", 8);
    ObjectId id8 = ObjectId.of("test2", "id", 7);
    ObjectId id9 = ObjectId.of("test2", "id", 4);
    ObjectId id10 = ObjectId.of("test", "id", 8);
    DbEntity test = mockEntity("test");
    DbEntity test2 = mockEntity("test2");
    BaseDbRowOp[] op = new BaseDbRowOp[10];
    op[0] = new InsertDbRowOp(mockObject(id1), test2, id1);
    op[1] = new InsertDbRowOp(mockObject(id2), test, id2);
    op[2] = new DeleteDbRowOp(mockObject(id3), test, id3);
    op[3] = new UpdateDbRowOp(mockObject(id4), test, id4);
    op[4] = new InsertDbRowOp(mockObject(id5), test2, id5);
    op[5] = new DeleteDbRowOp(mockObject(id6), test, id6);
    op[6] = new InsertDbRowOp(mockObject(id7), test, id7);
    op[7] = new UpdateDbRowOp(mockObject(id8), test2, id8);
    op[8] = new DeleteDbRowOp(mockObject(id9), test2, id9);
    op[9] = new DeleteDbRowOp(mockObject(id10), test, id10);
    List<DbRowOp> expected = Arrays.asList(op[1], op[6], op[0], op[4], op[3], op[7], op[8], op[2], op[5], op[9]);
    List<DbRowOp> sorted = sorter.sort(Arrays.asList(op));
    assertEquals(expected, sorted);
}
Also used : InsertDbRowOp(org.apache.cayenne.access.flush.operation.InsertDbRowOp) UpdateDbRowOp(org.apache.cayenne.access.flush.operation.UpdateDbRowOp) DbEntity(org.apache.cayenne.map.DbEntity) BaseDbRowOp(org.apache.cayenne.access.flush.operation.BaseDbRowOp) ObjectId(org.apache.cayenne.ObjectId) DbRowOp(org.apache.cayenne.access.flush.operation.DbRowOp) DeleteDbRowOp(org.apache.cayenne.access.flush.operation.DeleteDbRowOp) BaseDbRowOp(org.apache.cayenne.access.flush.operation.BaseDbRowOp) UpdateDbRowOp(org.apache.cayenne.access.flush.operation.UpdateDbRowOp) InsertDbRowOp(org.apache.cayenne.access.flush.operation.InsertDbRowOp) DeleteDbRowOp(org.apache.cayenne.access.flush.operation.DeleteDbRowOp) Test(org.junit.Test)

Example 10 with DbRowOp

use of org.apache.cayenne.access.flush.operation.DbRowOp in project cayenne by apache.

the class DefaultDataDomainFlushActionTest method createQueries.

@Test
public void createQueries() {
    ObjectId id1 = ObjectId.of("test", "id", 1);
    ObjectId id2 = ObjectId.of("test", "id", 2);
    ObjectId id3 = ObjectId.of("test2", "id", 3);
    ObjectId id4 = ObjectId.of("test2", "id", 4);
    ObjectId id5 = ObjectId.of("test", "id", 5);
    ObjectId id6 = ObjectId.of("test2", "id", 6);
    ObjectId id7 = ObjectId.of("test", "id", 7);
    DbEntity test = mockEntity("test");
    DbEntity test2 = mockEntity("test2");
    List<DbRowOp> ops = new ArrayList<>();
    ops.add(new InsertDbRowOp(mockObject(id1), test, id1));
    ops.add(new InsertDbRowOp(mockObject(id2), test, id2));
    ops.add(new InsertDbRowOp(mockObject(id3), test2, id5));
    ops.add(new InsertDbRowOp(mockObject(id4), test2, id7));
    ops.add(new UpdateDbRowOp(mockObject(id5), test, id3));
    ops.add(new DeleteDbRowOp(mockObject(id6), test2, id6));
    ops.add(new DeleteDbRowOp(mockObject(id7), test, id4));
    DefaultDataDomainFlushAction action = mock(DefaultDataDomainFlushAction.class);
    when(action.createQueries((List<DbRowOp>) any(List.class))).thenCallRealMethod();
    List<? extends Query> queries = action.createQueries(ops);
    assertEquals(4, queries.size());
    assertThat(queries.get(0), instanceOf(InsertBatchQuery.class));
    InsertBatchQuery insert1 = (InsertBatchQuery) queries.get(0);
    assertSame(test, insert1.getDbEntity());
    assertEquals(2, insert1.getRows().size());
    assertThat(queries.get(1), instanceOf(InsertBatchQuery.class));
    InsertBatchQuery insert2 = (InsertBatchQuery) queries.get(1);
    assertSame(test2, insert2.getDbEntity());
    assertEquals(2, insert2.getRows().size());
    assertThat(queries.get(2), instanceOf(DeleteBatchQuery.class));
    DeleteBatchQuery delete1 = (DeleteBatchQuery) queries.get(2);
    assertSame(test2, delete1.getDbEntity());
    assertEquals(1, delete1.getRows().size());
    assertThat(queries.get(3), instanceOf(DeleteBatchQuery.class));
    DeleteBatchQuery delete2 = (DeleteBatchQuery) queries.get(3);
    assertSame(test, delete2.getDbEntity());
    assertEquals(1, delete2.getRows().size());
}
Also used : InsertDbRowOp(org.apache.cayenne.access.flush.operation.InsertDbRowOp) UpdateDbRowOp(org.apache.cayenne.access.flush.operation.UpdateDbRowOp) DbEntity(org.apache.cayenne.map.DbEntity) ObjectId(org.apache.cayenne.ObjectId) InsertBatchQuery(org.apache.cayenne.query.InsertBatchQuery) DbRowOp(org.apache.cayenne.access.flush.operation.DbRowOp) DeleteDbRowOp(org.apache.cayenne.access.flush.operation.DeleteDbRowOp) BaseDbRowOp(org.apache.cayenne.access.flush.operation.BaseDbRowOp) UpdateDbRowOp(org.apache.cayenne.access.flush.operation.UpdateDbRowOp) InsertDbRowOp(org.apache.cayenne.access.flush.operation.InsertDbRowOp) DeleteBatchQuery(org.apache.cayenne.query.DeleteBatchQuery) ArrayList(java.util.ArrayList) DeleteDbRowOp(org.apache.cayenne.access.flush.operation.DeleteDbRowOp) DefaultDataDomainFlushAction(org.apache.cayenne.access.flush.DefaultDataDomainFlushAction) Test(org.junit.Test)

Aggregations

DbRowOp (org.apache.cayenne.access.flush.operation.DbRowOp)12 UpdateDbRowOp (org.apache.cayenne.access.flush.operation.UpdateDbRowOp)11 DeleteDbRowOp (org.apache.cayenne.access.flush.operation.DeleteDbRowOp)9 InsertDbRowOp (org.apache.cayenne.access.flush.operation.InsertDbRowOp)9 ObjectId (org.apache.cayenne.ObjectId)8 DbEntity (org.apache.cayenne.map.DbEntity)8 BaseDbRowOp (org.apache.cayenne.access.flush.operation.BaseDbRowOp)7 Test (org.junit.Test)7 ArrayList (java.util.ArrayList)4 List (java.util.List)2 DefaultDataDomainFlushAction (org.apache.cayenne.access.flush.DefaultDataDomainFlushAction)2 HashSet (java.util.HashSet)1 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)1 Persistent (org.apache.cayenne.Persistent)1 ObjectDiff (org.apache.cayenne.access.ObjectDiff)1 ObjectStore (org.apache.cayenne.access.ObjectStore)1 ObjectStoreGraphDiff (org.apache.cayenne.access.ObjectStoreGraphDiff)1 CompoundDiff (org.apache.cayenne.graph.CompoundDiff)1 DbAttribute (org.apache.cayenne.map.DbAttribute)1 DbJoin (org.apache.cayenne.map.DbJoin)1