Search in sources :

Example 11 with DbAttribute

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

the class OptimisticLockException method getFreshSnapshot.

/**
 * Retrieves fresh snapshot for the failed row. Null row indicates that it was
 * deleted.
 *
 * @since 3.0
 */
public Map<?, ?> getFreshSnapshot(ObjectContext context) {
    Expression qualifier = null;
    for (DbAttribute attribute : rootEntity.getPrimaryKeys()) {
        Expression attributeQualifier = ExpressionFactory.matchDbExp(attribute.getName(), qualifierSnapshot.get(attribute.getName()));
        qualifier = (qualifier != null) ? qualifier.andExp(attributeQualifier) : attributeQualifier;
    }
    SelectQuery<DataRow> query = new SelectQuery<DataRow>(rootEntity, qualifier);
    query.setFetchingDataRows(true);
    return (Map<?, ?>) Cayenne.objectForQuery(context, query);
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) Expression(org.apache.cayenne.exp.Expression) DbAttribute(org.apache.cayenne.map.DbAttribute) DataRow(org.apache.cayenne.DataRow) Map(java.util.Map)

Example 12 with DbAttribute

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

the class PrefetchProcessorJointNode method buildRowMapping.

/**
 * Configures row columns mapping for this node entity.
 */
private void buildRowMapping() {
    final Map<String, ColumnDescriptor> targetSource = new TreeMap<>();
    // build a DB path .. find parent node that terminates the joint group...
    PrefetchTreeNode jointRoot = this;
    while (jointRoot.getParent() != null && !jointRoot.isDisjointPrefetch() && !jointRoot.isDisjointByIdPrefetch()) {
        jointRoot = jointRoot.getParent();
    }
    final String prefix;
    if (jointRoot != this) {
        Expression objectPath = ExpressionFactory.exp(getPath(jointRoot));
        ASTPath translated = (ASTPath) ((PrefetchProcessorNode) jointRoot).getResolver().getEntity().translateToDbPath(objectPath);
        // make sure we do not include "db:" prefix
        prefix = translated.getOperand(0) + ".";
    } else {
        prefix = "";
    }
    if (getParent() != null && !getParent().isPhantom() && getIncoming() != null && !getIncoming().getRelationship().isFlattened()) {
        DbRelationship r = getIncoming().getRelationship().getDbRelationships().get(0);
        for (final DbJoin join : r.getJoins()) {
            appendColumn(targetSource, join.getTargetName(), prefix + join.getTargetName());
        }
    }
    ClassDescriptor descriptor = resolver.getDescriptor();
    descriptor.visitAllProperties(new PropertyVisitor() {

        public boolean visitAttribute(AttributeProperty property) {
            String target = property.getAttribute().getDbAttributePath();
            appendColumn(targetSource, target, prefix + target);
            return true;
        }

        public boolean visitToMany(ToManyProperty property) {
            return visitRelationship(property);
        }

        public boolean visitToOne(ToOneProperty property) {
            return visitRelationship(property);
        }

        private boolean visitRelationship(ArcProperty arc) {
            DbRelationship dbRel = arc.getRelationship().getDbRelationships().get(0);
            for (DbAttribute attribute : dbRel.getSourceAttributes()) {
                String target = attribute.getName();
                appendColumn(targetSource, target, prefix + target);
            }
            return true;
        }
    });
    // append id columns ... (some may have been appended already via relationships)
    for (String pkName : descriptor.getEntity().getPrimaryKeyNames()) {
        appendColumn(targetSource, pkName, prefix + pkName);
    }
    // append inheritance discriminator columns...
    for (ObjAttribute column : descriptor.getDiscriminatorColumns()) {
        String target = column.getDbAttributePath();
        appendColumn(targetSource, target, prefix + target);
    }
    int size = targetSource.size();
    this.rowCapacity = (int) Math.ceil(size / 0.75);
    this.columns = new ColumnDescriptor[size];
    targetSource.values().toArray(columns);
}
Also used : ArcProperty(org.apache.cayenne.reflect.ArcProperty) ClassDescriptor(org.apache.cayenne.reflect.ClassDescriptor) ObjAttribute(org.apache.cayenne.map.ObjAttribute) ColumnDescriptor(org.apache.cayenne.access.jdbc.ColumnDescriptor) DbAttribute(org.apache.cayenne.map.DbAttribute) TreeMap(java.util.TreeMap) AttributeProperty(org.apache.cayenne.reflect.AttributeProperty) ToOneProperty(org.apache.cayenne.reflect.ToOneProperty) ToManyProperty(org.apache.cayenne.reflect.ToManyProperty) Expression(org.apache.cayenne.exp.Expression) PrefetchTreeNode(org.apache.cayenne.query.PrefetchTreeNode) DbRelationship(org.apache.cayenne.map.DbRelationship) DbJoin(org.apache.cayenne.map.DbJoin) ASTPath(org.apache.cayenne.exp.parser.ASTPath) PropertyVisitor(org.apache.cayenne.reflect.PropertyVisitor)

Example 13 with DbAttribute

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

the class PrefetchProcessorJointNode method buildPKIndex.

/**
 * Creates an internal index of PK columns in the result.
 */
private void buildPKIndex() {
    // index PK
    Collection<DbAttribute> pks = getResolver().getEntity().getDbEntity().getPrimaryKeys();
    this.idIndices = new int[pks.size()];
    // this is needed for checking that a valid index is made
    Arrays.fill(idIndices, -1);
    Iterator<DbAttribute> it = pks.iterator();
    for (int i = 0; i < idIndices.length; i++) {
        DbAttribute pk = it.next();
        for (int j = 0; j < columns.length; j++) {
            if (pk.getName().equals(columns[j].getName())) {
                idIndices[i] = j;
                break;
            }
        }
        // sanity check
        if (idIndices[i] == -1) {
            throw new CayenneRuntimeException("PK column is not part of result row: %s", pk.getName());
        }
    }
}
Also used : DbAttribute(org.apache.cayenne.map.DbAttribute) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException)

Example 14 with DbAttribute

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

the class BatchAction method processGeneratedKeys.

/**
 * Implements generated keys extraction supported in JDBC 3.0 specification.
 *
 * @since 4.0
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
protected void processGeneratedKeys(Statement statement, OperationObserver observer, BatchQueryRow row) throws SQLException, CayenneException {
    ResultSet keysRS = statement.getGeneratedKeys();
    // TODO: andrus, 7/4/2007 - (1) get the type of meaningful PK's from
    // their
    // ObjAttributes; (2) use a different form of Statement.execute -
    // "execute(String,String[])" to be able to map generated column names
    // (this way
    // we can support multiple columns.. although need to check how well
    // this works
    // with most common drivers)
    RowDescriptorBuilder builder = new RowDescriptorBuilder();
    if (this.keyRowDescriptor == null) {
        // attempt to figure out the right descriptor from the mapping...
        Collection<DbAttribute> generated = query.getDbEntity().getGeneratedAttributes();
        if (generated.size() == 1) {
            DbAttribute key = generated.iterator().next();
            ColumnDescriptor[] columns = new ColumnDescriptor[1];
            // use column name from result set, but type and Java class from
            // DB
            // attribute
            columns[0] = new ColumnDescriptor(keysRS.getMetaData(), 1);
            columns[0].setJdbcType(key.getType());
            columns[0].setJavaClass(TypesMapping.getJavaBySqlType(key.getType()));
            builder.setColumns(columns);
        } else {
            builder.setResultSet(keysRS);
        }
        this.keyRowDescriptor = builder.getDescriptor(dataNode.getAdapter().getExtendedTypes());
    }
    RowReader<?> rowReader = dataNode.rowReader(keyRowDescriptor, query.getMetaData(dataNode.getEntityResolver()), Collections.<ObjAttribute, ColumnDescriptor>emptyMap());
    ResultIterator iterator = new JDBCResultIterator(null, keysRS, rowReader);
    observer.nextGeneratedRows(query, iterator, row.getObjectId());
}
Also used : DbAttribute(org.apache.cayenne.map.DbAttribute) ResultIterator(org.apache.cayenne.ResultIterator)

Example 15 with DbAttribute

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

the class DeleteBatchTranslator method createBindings.

@Override
protected DbAttributeBinding[] createBindings() {
    DeleteBatchQuery deleteBatch = (DeleteBatchQuery) query;
    List<DbAttribute> attributes = deleteBatch.getDbAttributes();
    int len = attributes.size();
    DbAttributeBinding[] bindings = new DbAttributeBinding[len];
    for (int i = 0; i < len; i++) {
        bindings[i] = new DbAttributeBinding(attributes.get(i));
    }
    return bindings;
}
Also used : DeleteBatchQuery(org.apache.cayenne.query.DeleteBatchQuery) 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