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);
}
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);
}
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());
}
}
}
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());
}
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;
}
Aggregations