Search in sources :

Example 16 with UpdateBatchQuery

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

the class QuotedIdentifiersIT method testPrefetchQuote.

@Test
public void testPrefetchQuote() {
    DbEntity entity = context.getEntityResolver().getObjEntity(QuoteAdress.class).getDbEntity();
    List<DbAttribute> idAttributes = Collections.singletonList(entity.getAttribute("City"));
    List<DbAttribute> updatedAttributes = Collections.singletonList(entity.getAttribute("City"));
    UpdateBatchQuery updateQuery = new UpdateBatchQuery(entity, idAttributes, updatedAttributes, Collections.emptySet(), 1);
    List objects3 = context.performQuery(updateQuery);
    assertEquals(0, objects3.size());
    List<Quote_Person> objects4 = ObjectSelect.query(Quote_Person.class).select(context);
    assertEquals(2, objects4.size());
    List<Quote_Person> objects5 = ObjectSelect.query(Quote_Person.class, Quote_Person.SALARY.eq(100)).select(context);
    assertEquals(1, objects5.size());
    List<Quote_Person> objects6 = ObjectSelect.query(Quote_Person.class, Quote_Person.GROUP.eq("107324")).select(context);
    assertEquals(1, objects6.size());
    List<QuoteAdress> objects7 = ObjectSelect.query(QuoteAdress.class, QuoteAdress.GROUP.eq("324")).select(context);
    assertEquals(1, objects7.size());
    ObjectIdQuery queryObjectId = new ObjectIdQuery(ObjectId.of("QuoteAdress", QuoteAdress.GROUP.getName(), "324"));
    List objects8 = context.performQuery(queryObjectId);
    assertEquals(1, objects8.size());
    ObjectIdQuery queryObjectId2 = new ObjectIdQuery(ObjectId.of("Quote_Person", "GROUP", "1111"));
    List objects9 = context.performQuery(queryObjectId2);
    assertEquals(1, objects9.size());
    Quote_Person quote_Person2 = ObjectSelect.query(Quote_Person.class, Quote_Person.NAME.eq("Name")).selectOne(context);
    RelationshipQuery relationshipQuery = new RelationshipQuery(quote_Person2.getObjectId(), "address_Rel");
    List objects10 = context.performQuery(relationshipQuery);
    assertEquals(1, objects10.size());
}
Also used : DbEntity(org.apache.cayenne.map.DbEntity) RelationshipQuery(org.apache.cayenne.query.RelationshipQuery) UpdateBatchQuery(org.apache.cayenne.query.UpdateBatchQuery) DbAttribute(org.apache.cayenne.map.DbAttribute) QuoteAdress(org.apache.cayenne.testdo.quotemap.QuoteAdress) List(java.util.List) Quote_Person(org.apache.cayenne.testdo.quotemap.Quote_Person) ObjectIdQuery(org.apache.cayenne.query.ObjectIdQuery) Test(org.junit.Test)

Example 17 with UpdateBatchQuery

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

the class UpdateBatchTranslator method createBindings.

@Override
protected DbAttributeBinding[] createBindings() {
    UpdateBatchQuery updateBatch = (UpdateBatchQuery) query;
    List<DbAttribute> updatedDbAttributes = updateBatch.getUpdatedAttributes();
    List<DbAttribute> qualifierAttributes = updateBatch.getQualifierAttributes();
    int ul = updatedDbAttributes.size();
    int ql = qualifierAttributes.size();
    DbAttributeBinding[] bindings = new DbAttributeBinding[ul + ql];
    for (int i = 0; i < ul; i++) {
        bindings[i] = new DbAttributeBinding(updatedDbAttributes.get(i));
    }
    for (int i = 0; i < ql; i++) {
        bindings[ul + i] = new DbAttributeBinding(qualifierAttributes.get(i));
    }
    return bindings;
}
Also used : UpdateBatchQuery(org.apache.cayenne.query.UpdateBatchQuery) DbAttribute(org.apache.cayenne.map.DbAttribute) DbAttributeBinding(org.apache.cayenne.access.translator.DbAttributeBinding)

Example 18 with UpdateBatchQuery

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

the class UpdateBatchTranslator method createSql.

@Override
protected String createSql() {
    UpdateBatchQuery updateBatch = (UpdateBatchQuery) query;
    QuotingStrategy strategy = adapter.getQuotingStrategy();
    List<DbAttribute> qualifierAttributes = updateBatch.getQualifierAttributes();
    List<DbAttribute> updatedDbAttributes = updateBatch.getUpdatedAttributes();
    StringBuilder buffer = new StringBuilder("UPDATE ");
    buffer.append(strategy.quotedFullyQualifiedName(query.getDbEntity()));
    buffer.append(" SET ");
    int len = updatedDbAttributes.size();
    for (int i = 0; i < len; i++) {
        if (i > 0) {
            buffer.append(", ");
        }
        DbAttribute attribute = updatedDbAttributes.get(i);
        buffer.append(strategy.quotedName(attribute));
        buffer.append(" = ?");
    }
    buffer.append(" WHERE ");
    Iterator<DbAttribute> i = qualifierAttributes.iterator();
    while (i.hasNext()) {
        DbAttribute attribute = i.next();
        appendDbAttribute(buffer, attribute);
        buffer.append(updateBatch.isNull(attribute) ? " IS NULL" : " = ?");
        if (i.hasNext()) {
            buffer.append(" AND ");
        }
    }
    return buffer.toString();
}
Also used : UpdateBatchQuery(org.apache.cayenne.query.UpdateBatchQuery) DbAttribute(org.apache.cayenne.map.DbAttribute) QuotingStrategy(org.apache.cayenne.dba.QuotingStrategy)

Example 19 with UpdateBatchQuery

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

the class UpdateBatchTranslator method updateBindings.

@Override
public DbAttributeBinding[] updateBindings(BatchQueryRow row) {
    UpdateBatchQuery updateBatch = context.getQuery();
    int i = 0;
    int j = 0;
    for (; i < updateBatch.getUpdatedAttributes().size(); i++) {
        Object value = row.getValue(i);
        ExtendedType<?> extendedType = value == null ? context.getAdapter().getExtendedTypes().getDefaultType() : context.getAdapter().getExtendedTypes().getRegisteredType(value.getClass());
        bindings[j].include(++j, value, extendedType);
    }
    for (DbAttribute attribute : updateBatch.getQualifierAttributes()) {
        if (updateBatch.isNull(attribute)) {
            i++;
            continue;
        }
        Object value = row.getValue(i);
        ExtendedType<?> extendedType = context.getAdapter().getExtendedTypes().getRegisteredType(value.getClass());
        bindings[j].include(++j, value, extendedType);
        i++;
    }
    return bindings;
}
Also used : UpdateBatchQuery(org.apache.cayenne.query.UpdateBatchQuery) DbAttribute(org.apache.cayenne.map.DbAttribute)

Example 20 with UpdateBatchQuery

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

the class Oracle8LOBUpdateBatchTranslator method createSql.

@Override
public String createSql(BatchQueryRow row) {
    UpdateBatchQuery updateBatch = (UpdateBatchQuery) query;
    List<DbAttribute> idDbAttributes = updateBatch.getQualifierAttributes();
    List<DbAttribute> updatedDbAttributes = updateBatch.getUpdatedAttributes();
    QuotingStrategy strategy = adapter.getQuotingStrategy();
    StringBuilder buffer = new StringBuilder("UPDATE ");
    buffer.append(strategy.quotedFullyQualifiedName(query.getDbEntity()));
    buffer.append(" SET ");
    int len = updatedDbAttributes.size();
    for (int i = 0; i < len; i++) {
        if (i > 0) {
            buffer.append(", ");
        }
        DbAttribute attribute = updatedDbAttributes.get(i);
        buffer.append(strategy.quotedName(attribute));
        buffer.append(" = ");
        appendUpdatedParameter(buffer, attribute, row.getValue(i));
    }
    buffer.append(" WHERE ");
    Iterator<DbAttribute> i = idDbAttributes.iterator();
    while (i.hasNext()) {
        DbAttribute attribute = i.next();
        appendDbAttribute(buffer, attribute);
        buffer.append(" = ?");
        if (i.hasNext()) {
            buffer.append(" AND ");
        }
    }
    return buffer.toString();
}
Also used : UpdateBatchQuery(org.apache.cayenne.query.UpdateBatchQuery) DbAttribute(org.apache.cayenne.map.DbAttribute) QuotingStrategy(org.apache.cayenne.dba.QuotingStrategy)

Aggregations

UpdateBatchQuery (org.apache.cayenne.query.UpdateBatchQuery)24 DbAttribute (org.apache.cayenne.map.DbAttribute)18 DbEntity (org.apache.cayenne.map.DbEntity)10 Test (org.junit.Test)10 JdbcAdapter (org.apache.cayenne.dba.JdbcAdapter)9 SimpleLockingTestEntity (org.apache.cayenne.testdo.locking.SimpleLockingTestEntity)8 List (java.util.List)5 DbAdapter (org.apache.cayenne.dba.DbAdapter)5 UnitDbAdapter (org.apache.cayenne.unit.UnitDbAdapter)5 DbAttributeBinding (org.apache.cayenne.access.translator.DbAttributeBinding)3 QuotingStrategy (org.apache.cayenne.dba.QuotingStrategy)3 Collection (java.util.Collection)2 ExtendedType (org.apache.cayenne.access.types.ExtendedType)2 InsertBatchQuery (org.apache.cayenne.query.InsertBatchQuery)2 PreparedStatement (java.sql.PreparedStatement)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 Set (java.util.Set)1