use of org.apache.cayenne.map.ObjAttribute in project cayenne by apache.
the class EntityMergeSupport method getRidOfAttributesThatAreNowSrcAttributesForRelationships.
private boolean getRidOfAttributesThatAreNowSrcAttributesForRelationships(ObjEntity entity) {
boolean changed = false;
for (DbAttribute da : getMeaningfulFKs(entity)) {
ObjAttribute oa = entity.getAttributeForDbAttribute(da);
while (oa != null) {
String attrName = oa.getName();
entity.removeAttribute(attrName);
changed = true;
oa = entity.getAttributeForDbAttribute(da);
}
}
return changed;
}
use of org.apache.cayenne.map.ObjAttribute in project cayenne by apache.
the class ValueForNullIT method test.
@Test
public void test() throws Exception {
DbEntity dbEntity = map.getDbEntity("PAINTING");
assertNotNull(dbEntity);
ObjEntity objEntity = map.getObjEntity("Painting");
assertNotNull(objEntity);
// insert some rows before adding "not null" column
final int nrows = 10;
for (int i = 0; i < nrows; i++) {
DataObject o = (DataObject) context.newObject("Painting");
o.writeProperty("paintingTitle", "ptitle" + i);
}
context.commitChanges();
// create and add new column to model and db
DbAttribute column = new DbAttribute("NEWCOL2", Types.VARCHAR, dbEntity);
column.setMandatory(false);
column.setMaxLength(10);
dbEntity.addAttribute(column);
assertTrue(dbEntity.getAttributes().contains(column));
assertEquals(column, dbEntity.getAttribute(column.getName()));
assertTokensAndExecute(1, 0);
// need obj attr to be able to query
ObjAttribute objAttr = new ObjAttribute("newcol2");
objAttr.setDbAttributePath(column.getName());
objEntity.addAttribute(objAttr);
// check that is was merged
assertTokensAndExecute(0, 0);
// set not null
column.setMandatory(true);
// merge to db
assertTokensAndExecute(2, 0);
// check that is was merged
assertTokensAndExecute(0, 0);
// check values for null
Expression qual = ExpressionFactory.matchExp(objAttr.getName(), DEFAULT_VALUE_STRING);
SelectQuery query = new SelectQuery("Painting", qual);
@SuppressWarnings("unchecked") List<Persistent> rows = context.performQuery(query);
assertEquals(nrows, rows.size());
// clean up
dbEntity.removeAttribute(column.getName());
assertTokensAndExecute(1, 0);
assertTokensAndExecute(0, 0);
}
use of org.apache.cayenne.map.ObjAttribute in project cayenne by apache.
the class SQLTemplateAction method configureRowDescriptorBuilder.
/**
* @since 3.0
*/
protected RowDescriptorBuilder configureRowDescriptorBuilder(SQLStatement compiled, ResultSet resultSet) throws SQLException {
RowDescriptorBuilder builder = new RowDescriptorBuilder().setResultSet(resultSet).validateDuplicateColumnNames();
// SQLTemplate #result columns take precedence over other ways to determine the type
if (compiled.getResultColumns().length > 0) {
builder.setColumns(compiled.getResultColumns());
}
ObjEntity entity = queryMetadata.getObjEntity();
if (entity != null) {
// TODO: andrus 2008/03/28 support flattened attributes with aliases...
for (ObjAttribute attribute : entity.getAttributes()) {
String column = attribute.getDbAttributePath();
if (column == null || column.indexOf('.') > 0) {
continue;
}
builder.overrideColumnType(column, attribute.getType());
}
}
// (BigDecimal vs Long vs. Integer)
if (dbEntity != null) {
for (DbAttribute attribute : dbEntity.getAttributes()) {
if (!builder.isOverriden(attribute.getName()) && TypesMapping.isNumeric(attribute.getType())) {
builder.overrideColumnType(attribute.getName(), TypesMapping.getJavaBySqlType(attribute.getType()));
}
}
}
switch(query.getColumnNamesCapitalization()) {
case LOWER:
builder.useLowercaseColumnNames();
break;
case UPPER:
builder.useUppercaseColumnNames();
break;
}
return builder;
}
use of org.apache.cayenne.map.ObjAttribute in project cayenne by apache.
the class EJBQLSelectColumnsTranslator method visitPath.
@Override
public boolean visitPath(EJBQLExpression expression, int finishedChildIndex) {
EJBQLPathTranslator pathTranslator = new EJBQLPathTranslator(context) {
@Override
protected void appendMultiColumnPath(EJBQLMultiColumnOperand operand) {
throw new EJBQLException("Can't use multi-column paths in column clause");
}
@Override
protected void processTerminatingRelationship(ObjRelationship relationship) {
Map<String, String> xfields = null;
if (context.isAppendingResultColumns()) {
xfields = context.nextEntityResult().getFields();
}
final Map<String, String> fields = xfields;
Collection<DbAttribute> dbAttr = ((ObjEntity) relationship.getTargetEntity()).getDbEntity().getAttributes();
DbRelationship dbRelationship = relationship.getDbRelationships().get(0);
DbEntity table = (DbEntity) dbRelationship.getTargetEntity();
Iterator<DbAttribute> it = dbAttr.iterator();
if (dbAttr.size() > 0) {
resolveJoin();
}
String alias = this.lastAlias != null ? lastAlias : context.getTableAlias(idPath, context.getQuotingStrategy().quotedFullyQualifiedName(table));
boolean first = true;
while (it.hasNext()) {
context.append(!first ? ", " : " ");
DbAttribute dbAttribute = it.next();
appendColumn(TypesMapping.getJavaBySqlType(dbAttribute.getType()), alias, dbAttribute, fields != null ? fields.get(dbAttribute.getName()) : "");
first = false;
}
}
@Override
protected void processTerminatingAttribute(ObjAttribute attribute) {
DbEntity table = currentEntity.getDbEntity();
String alias = this.lastAlias != null ? lastAlias : context.getTableAlias(idPath, context.getQuotingStrategy().quotedFullyQualifiedName(table));
if (attribute.isFlattened()) {
Iterator<?> dbPathIterator = attribute.getDbPathIterator();
EJBQLTableId lhsId = new EJBQLTableId(idPath);
while (dbPathIterator.hasNext()) {
Object pathPart = dbPathIterator.next();
// later when appending table
if (pathPart == null) {
throw new CayenneRuntimeException("ObjAttribute has no component: %s", attribute.getName());
} else if (pathPart instanceof DbAttribute) {
DbAttribute dbAttribute = (DbAttribute) pathPart;
appendColumn(attribute.getType(), context.getTableAlias(lhsId.getEntityId(), context.getQuotingStrategy().quotedFullyQualifiedName((DbEntity) dbAttribute.getEntity())), dbAttribute, context.isAppendingResultColumns() ? context.nextColumnAlias() : "");
}
}
} else {
DbAttribute dbAttribute = attribute.getDbAttribute();
appendColumn(attribute.getType(), alias, dbAttribute, context.isAppendingResultColumns() ? context.nextColumnAlias() : "");
}
}
};
expression.visit(pathTranslator);
return false;
}
use of org.apache.cayenne.map.ObjAttribute in project cayenne by apache.
the class LockingUpdateController method updateAction.
public void updateAction() {
int defaultLockType = dataMap.getDefaultLockType();
boolean on = defaultLockType == ObjEntity.LOCK_TYPE_OPTIMISTIC;
boolean updateEntities = view.getEntities().isSelected();
boolean updateAttributes = view.getAttributes().isSelected();
boolean updateRelationships = view.getRelationships().isSelected();
ProjectController parent = (ProjectController) getParent();
for (ObjEntity entity : dataMap.getObjEntities()) {
if (updateEntities && defaultLockType != entity.getDeclaredLockType()) {
entity.setDeclaredLockType(defaultLockType);
parent.fireObjEntityEvent(new EntityEvent(this, entity));
}
if (updateAttributes) {
for (ObjAttribute a : entity.getAttributes()) {
if (a.isUsedForLocking() != on) {
a.setUsedForLocking(on);
parent.fireObjAttributeEvent(new AttributeEvent(this, a, entity));
}
}
}
if (updateRelationships) {
for (ObjRelationship r : entity.getRelationships()) {
if (r.isUsedForLocking() != on) {
r.setUsedForLocking(on);
parent.fireObjRelationshipEvent(new RelationshipEvent(this, r, entity));
}
}
}
}
if (view != null) {
view.dispose();
}
}
Aggregations