Search in sources :

Example 16 with DbAttribute

use of org.apache.cayenne.map.DbAttribute in project cayenne by apache.

the class DeleteBatchTranslator method applyQualifier.

/**
 * Appends WHERE clause to SQL string
 */
protected void applyQualifier(StringBuilder buffer) {
    buffer.append(" WHERE ");
    DeleteBatchQuery deleteBatch = (DeleteBatchQuery) query;
    Iterator<DbAttribute> i = deleteBatch.getDbAttributes().iterator();
    while (i.hasNext()) {
        DbAttribute attribute = i.next();
        appendDbAttribute(buffer, attribute);
        buffer.append(deleteBatch.isNull(attribute) ? " IS NULL" : " = ?");
        if (i.hasNext()) {
            buffer.append(" AND ");
        }
    }
}
Also used : DeleteBatchQuery(org.apache.cayenne.query.DeleteBatchQuery) DbAttribute(org.apache.cayenne.map.DbAttribute)

Example 17 with DbAttribute

use of org.apache.cayenne.map.DbAttribute in project cayenne by apache.

the class SoftDeleteBatchTranslator method createBindings.

@Override
protected DbAttributeBinding[] createBindings() {
    DbAttributeBinding[] superBindings = super.createBindings();
    int slen = superBindings.length;
    DbAttributeBinding[] bindings = new DbAttributeBinding[slen + 1];
    DbAttribute deleteAttribute = query.getDbEntity().getAttribute(deletedFieldName);
    String typeName = TypesMapping.getJavaBySqlType(deleteAttribute.getType());
    ExtendedType extendedType = adapter.getExtendedTypes().getRegisteredType(typeName);
    bindings[0] = new DbAttributeBinding(deleteAttribute);
    bindings[0].include(1, true, extendedType);
    System.arraycopy(superBindings, 0, bindings, 1, slen);
    return bindings;
}
Also used : DbAttribute(org.apache.cayenne.map.DbAttribute) DbAttributeBinding(org.apache.cayenne.access.translator.DbAttributeBinding) ExtendedType(org.apache.cayenne.access.types.ExtendedType)

Example 18 with DbAttribute

use of org.apache.cayenne.map.DbAttribute 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 DbAttribute

use of org.apache.cayenne.map.DbAttribute in project cayenne by apache.

the class UpdateBatchTranslator method doUpdateBindings.

@Override
protected DbAttributeBinding[] doUpdateBindings(BatchQueryRow row) {
    UpdateBatchQuery updateBatch = (UpdateBatchQuery) query;
    List<DbAttribute> updatedDbAttributes = updateBatch.getUpdatedAttributes();
    List<DbAttribute> qualifierAttributes = updateBatch.getQualifierAttributes();
    int ul = updatedDbAttributes.size();
    int ql = qualifierAttributes.size();
    int j = 1;
    for (int i = 0; i < ul; i++) {
        Object value = row.getValue(i);
        ExtendedType extendedType = value != null ? adapter.getExtendedTypes().getRegisteredType(value.getClass()) : adapter.getExtendedTypes().getDefaultType();
        bindings[i].include(j++, value, extendedType);
    }
    for (int i = 0; i < ql; i++) {
        DbAttribute a = qualifierAttributes.get(i);
        // skip null attributes... they are translated as "IS NULL"
        if (updateBatch.isNull(a)) {
            continue;
        }
        Object value = row.getValue(ul + i);
        ExtendedType extendedType = value != null ? adapter.getExtendedTypes().getRegisteredType(value.getClass()) : adapter.getExtendedTypes().getDefaultType();
        bindings[ul + i].include(j++, value, extendedType);
    }
    return bindings;
}
Also used : UpdateBatchQuery(org.apache.cayenne.query.UpdateBatchQuery) DbAttribute(org.apache.cayenne.map.DbAttribute) ExtendedType(org.apache.cayenne.access.types.ExtendedType)

Example 20 with DbAttribute

use of org.apache.cayenne.map.DbAttribute 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)

Aggregations

DbAttribute (org.apache.cayenne.map.DbAttribute)194 DbEntity (org.apache.cayenne.map.DbEntity)109 Test (org.junit.Test)67 ObjEntity (org.apache.cayenne.map.ObjEntity)36 DbRelationship (org.apache.cayenne.map.DbRelationship)35 ObjAttribute (org.apache.cayenne.map.ObjAttribute)32 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)21 DbJoin (org.apache.cayenne.map.DbJoin)18 HashMap (java.util.HashMap)16 ObjRelationship (org.apache.cayenne.map.ObjRelationship)16 ArrayList (java.util.ArrayList)14 DbAttributeBinding (org.apache.cayenne.access.translator.DbAttributeBinding)12 DataMap (org.apache.cayenne.map.DataMap)11 JdbcAdapter (org.apache.cayenne.dba.JdbcAdapter)10 QuotingStrategy (org.apache.cayenne.dba.QuotingStrategy)10 MergerToken (org.apache.cayenne.dbsync.merge.token.MergerToken)10 DeleteBatchQuery (org.apache.cayenne.query.DeleteBatchQuery)10 ClassDescriptor (org.apache.cayenne.reflect.ClassDescriptor)10 ObjectId (org.apache.cayenne.ObjectId)9 Expression (org.apache.cayenne.exp.Expression)8