Search in sources :

Example 6 with BatchQueryRow

use of org.apache.cayenne.query.BatchQueryRow in project cayenne by apache.

the class DefaultBatchTranslatorIT method testAppendDbAttribute2.

@Test
public void testAppendDbAttribute2() throws Exception {
    DbAdapter adapter = objectFactory.newInstance(DbAdapter.class, JdbcAdapter.class.getName());
    DefaultBatchTranslator builder = new DefaultBatchTranslator(mock(BatchQuery.class), adapter, null) {

        @Override
        protected String createSql() {
            return null;
        }

        @Override
        protected DbAttributeBinding[] createBindings() {
            return new DbAttributeBinding[0];
        }

        @Override
        protected DbAttributeBinding[] doUpdateBindings(BatchQueryRow row) {
            return new DbAttributeBinding[0];
        }
    };
    StringBuilder buf = new StringBuilder();
    DbEntity entity = new DbEntity("Test");
    DbAttribute attr = new DbAttribute("testAttr", Types.CHAR, null);
    attr.setEntity(entity);
    builder.appendDbAttribute(buf, attr);
    assertEquals("testAttr", buf.toString());
    buf = new StringBuilder();
    attr = new DbAttribute("testAttr", Types.VARCHAR, null);
    attr.setEntity(entity);
    builder.appendDbAttribute(buf, attr);
    assertEquals("testAttr", buf.toString());
}
Also used : BatchQueryRow(org.apache.cayenne.query.BatchQueryRow) JdbcAdapter(org.apache.cayenne.dba.JdbcAdapter) DbAdapter(org.apache.cayenne.dba.DbAdapter) DbEntity(org.apache.cayenne.map.DbEntity) DbAttribute(org.apache.cayenne.map.DbAttribute) DbAttributeBinding(org.apache.cayenne.access.translator.DbAttributeBinding) BatchQuery(org.apache.cayenne.query.BatchQuery) Test(org.junit.Test)

Example 7 with BatchQueryRow

use of org.apache.cayenne.query.BatchQueryRow in project cayenne by apache.

the class DataDomainFlattenedBucket method appendInserts.

/**
 * responsible for adding the flattened Insert Queries. Its possible an insert query for the same DbEntity/ObjectId
 * already has been added from the insert bucket queries if that Object also has an attribute. So we want to merge
 * the data for each insert into a single insert.
 */
void appendInserts(Collection<Query> queries) {
    for (Map.Entry<DbEntity, List<FlattenedArcKey>> entry : insertArcKeys.entrySet()) {
        DbEntity dbEntity = entry.getKey();
        List<FlattenedArcKey> flattenedArcKeys = entry.getValue();
        DataNode node = parent.getDomain().lookupDataNode(dbEntity.getDataMap());
        // TODO: O(N) lookup
        InsertBatchQuery existingQuery = findInsertBatchQuery(queries, dbEntity);
        InsertBatchQuery newQuery = new InsertBatchQuery(dbEntity, 50);
        // merge the snapshots of the FAKs by ObjectId for all ToOne relationships in case we have multiple Arcs per Object
        Map<ObjectId, Map<String, Object>> toOneSnapshots = new HashMap<>();
        // gather the list of the ToMany snapshots (these will actually be their own insert rows)
        List<Map<String, Object>> toManySnapshots = new ArrayList<>();
        for (FlattenedArcKey flattenedArcKey : flattenedArcKeys) {
            Map<String, Object> joinSnapshot = flattenedArcKey.buildJoinSnapshotForInsert(node);
            if (flattenedArcKey.relationship.isToMany()) {
                toManySnapshots.add(joinSnapshot);
            } else {
                ObjectId objectId = flattenedArcKey.id1.getSourceId();
                Map<String, Object> snapshot = toOneSnapshots.get(objectId);
                if (snapshot == null) {
                    toOneSnapshots.put(objectId, joinSnapshot);
                } else {
                    // merge joinSnapshot data with existing snapshot
                    for (Map.Entry<String, Object> dbValue : joinSnapshot.entrySet()) {
                        snapshot.put(dbValue.getKey(), dbValue.getValue());
                    }
                }
            }
        }
        // apply the merged ToOne snapshots information and possibly merge it with an existing BatchQueryRow
        for (Map.Entry<ObjectId, Map<String, Object>> flattenedSnapshot : toOneSnapshots.entrySet()) {
            ObjectId objectId = flattenedSnapshot.getKey();
            Map<String, Object> snapshot = flattenedSnapshot.getValue();
            if (existingQuery != null) {
                // TODO: O(N) lookup
                BatchQueryRow existingRow = findRowForObjectId(existingQuery.getRows(), objectId);
                if (existingRow != null) {
                    List<DbAttribute> existingQueryDbAttributes = existingQuery.getDbAttributes();
                    for (int i = 0; i < existingQueryDbAttributes.size(); i++) {
                        Object value = existingRow.getValue(i);
                        if (value != null) {
                            snapshot.put(existingQueryDbAttributes.get(i).getName(), value);
                        }
                    }
                }
            }
            newQuery.add(snapshot, objectId);
        }
        // apply the ToMany snapshots as new BatchQueryRows
        for (Map<String, Object> toManySnapshot : toManySnapshots) {
            newQuery.add(toManySnapshot);
        }
        if (existingQuery != null) {
            queries.remove(existingQuery);
        }
        queries.add(newQuery);
    }
}
Also used : InsertBatchQuery(org.apache.cayenne.query.InsertBatchQuery) ObjectId(org.apache.cayenne.ObjectId) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) DbAttribute(org.apache.cayenne.map.DbAttribute) BatchQueryRow(org.apache.cayenne.query.BatchQueryRow) DbEntity(org.apache.cayenne.map.DbEntity) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Example 8 with BatchQueryRow

use of org.apache.cayenne.query.BatchQueryRow in project cayenne by apache.

the class BatchAction method runAsIndividualQueries.

/**
 * Executes batch as individual queries over the same prepared statement.
 */
protected void runAsIndividualQueries(Connection connection, BatchTranslator translator, OperationObserver delegate, boolean generatesKeys) throws SQLException, Exception {
    JdbcEventLogger logger = dataNode.getJdbcEventLogger();
    boolean useOptimisticLock = query.isUsingOptimisticLocking();
    String queryStr = translator.getSql();
    // log batch SQL execution
    logger.log(queryStr);
    // run batch queries one by one
    DbAdapter adapter = dataNode.getAdapter();
    try (PreparedStatement statement = prepareStatement(connection, queryStr, adapter, generatesKeys)) {
        for (BatchQueryRow row : query.getRows()) {
            DbAttributeBinding[] bindings = translator.updateBindings(row);
            logger.logQueryParameters("bind", bindings);
            bind(adapter, statement, bindings);
            int updated = statement.executeUpdate();
            if (useOptimisticLock && updated != 1) {
                throw new OptimisticLockException(row.getObjectId(), query.getDbEntity(), queryStr, row.getQualifier());
            }
            delegate.nextCount(query, updated);
            if (generatesKeys) {
                processGeneratedKeys(statement, delegate, row);
            }
            logger.logUpdateCount(updated);
        }
    }
}
Also used : BatchQueryRow(org.apache.cayenne.query.BatchQueryRow) DbAdapter(org.apache.cayenne.dba.DbAdapter) JdbcEventLogger(org.apache.cayenne.log.JdbcEventLogger) OptimisticLockException(org.apache.cayenne.access.OptimisticLockException) DbAttributeBinding(org.apache.cayenne.access.translator.DbAttributeBinding)

Aggregations

BatchQueryRow (org.apache.cayenne.query.BatchQueryRow)8 DbAttributeBinding (org.apache.cayenne.access.translator.DbAttributeBinding)7 DbAdapter (org.apache.cayenne.dba.DbAdapter)5 DbAttribute (org.apache.cayenne.map.DbAttribute)4 JdbcAdapter (org.apache.cayenne.dba.JdbcAdapter)3 DbEntity (org.apache.cayenne.map.DbEntity)3 BatchQuery (org.apache.cayenne.query.BatchQuery)3 Test (org.junit.Test)3 JdbcEventLogger (org.apache.cayenne.log.JdbcEventLogger)2 InsertBatchQuery (org.apache.cayenne.query.InsertBatchQuery)2 PreparedStatement (java.sql.PreparedStatement)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 CayenneException (org.apache.cayenne.CayenneException)1 ObjectId (org.apache.cayenne.ObjectId)1 OptimisticLockException (org.apache.cayenne.access.OptimisticLockException)1 BatchTranslator (org.apache.cayenne.access.translator.batch.BatchTranslator)1 BindingsTransformer (org.apache.cayenne.crypto.transformer.BindingsTransformer)1