use of org.apache.cayenne.map.DbJoin in project cayenne by apache.
the class OpenBaseMergerTokenFactory method createDropRelationshipToDb.
@Override
public MergerToken createDropRelationshipToDb(final DbEntity entity, final DbRelationship rel) {
return new DropRelationshipToDb(entity, rel) {
@Override
public List<String> createSql(DbAdapter adapter) {
// FK_NAME form jdbc metadata seem to be wrong. It contain a column name
// and not the 'relationshipName'
// TODO: tell openbase developer mail list
DbEntity source = getEntity();
DbEntity dest = rel.getTargetEntity();
// only use the first. See adapter
// TODO: can we be sure this is the first and same as used by the adapter?
DbJoin join = rel.getJoins().get(0);
return Collections.singletonList("delete from _SYS_RELATIONSHIP where " + " source_table = '" + dest.getFullyQualifiedName() + "'" + " and source_column = '" + join.getTargetName() + "'" + " and dest_table = '" + source.getFullyQualifiedName() + "'" + " and dest_column = '" + join.getSourceName() + "'");
}
};
}
use of org.apache.cayenne.map.DbJoin in project cayenne by apache.
the class DropColumnToModel method execute.
@Override
public void execute(MergerContext mergerContext) {
// remove relationships mapped to column. duplicate List to prevent
// ConcurrentModificationException
List<DbRelationship> dbRelationships = new ArrayList<>(getEntity().getRelationships());
for (DbRelationship dbRelationship : dbRelationships) {
for (DbJoin join : dbRelationship.getJoins()) {
if (join.getSource() == getColumn() || join.getTarget() == getColumn()) {
remove(mergerContext.getDelegate(), dbRelationship, true);
}
}
}
// remove ObjAttribute mapped to same column
for (ObjEntity objEntity : getEntity().mappedObjEntities()) {
ObjAttribute objAttribute = objEntity.getAttributeForDbAttribute(getColumn());
if (objAttribute != null) {
objEntity.removeAttribute(objAttribute.getName());
mergerContext.getDelegate().objAttributeRemoved(objAttribute);
}
}
// remove DbAttribute
getEntity().removeAttribute(getColumn().getName());
mergerContext.getDelegate().dbAttributeRemoved(getColumn());
}
use of org.apache.cayenne.map.DbJoin in project cayenne by apache.
the class DefaultObjectNameGenerator method toOneRelationshipName.
protected String toOneRelationshipName(DbRelationship... relationshipChain) {
DbRelationship first = relationshipChain[0];
DbRelationship last = relationshipChain[relationshipChain.length - 1];
List<DbJoin> joins = first.getJoins();
if (joins.isEmpty()) {
// and just return targetName
return stemmed(last.getTargetEntityName());
}
DbJoin join1 = joins.get(0);
// TODO: multi-join relationships
// return the name of the FK column sans ID
String fkColName = join1.getSourceName();
if (fkColName == null) {
return stemmed(last.getTargetEntityName());
} else if (fkColName.toUpperCase().endsWith("_ID") && fkColName.length() > 3) {
return fkColName.substring(0, fkColName.length() - 3);
} else if (fkColName.toUpperCase().endsWith("ID") && fkColName.length() > 2) {
return fkColName.substring(0, fkColName.length() - 2);
} else {
return stemmed(last.getTargetEntityName());
}
}
use of org.apache.cayenne.map.DbJoin in project cayenne by apache.
the class MergerFactoryIT method testAddForeignKeyWithTable.
@Test
public void testAddForeignKeyWithTable() throws Exception {
dropTableIfPresent("NEW_TABLE");
assertTokensAndExecute(0, 0);
DbEntity dbEntity = new DbEntity("NEW_TABLE");
attr(dbEntity, "ID", Types.INTEGER, true, true);
attr(dbEntity, "NAME", Types.VARCHAR, false, false).setMaxLength(10);
attr(dbEntity, "ARTIST_ID", Types.BIGINT, false, false);
map.addDbEntity(dbEntity);
DbEntity artistDbEntity = map.getDbEntity("ARTIST");
assertNotNull(artistDbEntity);
// relation from new_table to artist
DbRelationship r1 = new DbRelationship("toArtistR1");
r1.setSourceEntity(dbEntity);
r1.setTargetEntityName(artistDbEntity);
r1.setToMany(false);
r1.addJoin(new DbJoin(r1, "ARTIST_ID", "ARTIST_ID"));
dbEntity.addRelationship(r1);
// relation from artist to new_table
DbRelationship r2 = new DbRelationship("toNewTableR2");
r2.setSourceEntity(artistDbEntity);
r2.setTargetEntityName(dbEntity);
r2.setToMany(true);
r2.addJoin(new DbJoin(r2, "ARTIST_ID", "ARTIST_ID"));
artistDbEntity.addRelationship(r2);
assertTokensAndExecute(2, 0);
assertTokensAndExecute(0, 0);
// remove relationships
dbEntity.removeRelationship(r1.getName());
artistDbEntity.removeRelationship(r2.getName());
resolver.refreshMappingCache();
/*
* Db -Rel 'toArtistR1' - NEW_TABLE 1 -> 1 ARTIST"
r2 = * Db -Rel 'toNewTableR2' - ARTIST 1 -> * NEW_TABLE" -- Not generated any more
* */
assertTokensAndExecute(1, 0);
assertTokensAndExecute(0, 0);
// clear up
// map.removeObjEntity(objEntity.getName(), true);
map.removeDbEntity(dbEntity.getName(), true);
resolver.refreshMappingCache();
// assertNull(map.getObjEntity(objEntity.getName()));
assertNull(map.getDbEntity(dbEntity.getName()));
assertFalse(map.getDbEntities().contains(dbEntity));
assertTokensAndExecute(1, 0);
assertTokensAndExecute(0, 0);
}
use of org.apache.cayenne.map.DbJoin in project cayenne by apache.
the class InferRelationshipsControllerBase method createReversRelationship.
public void createReversRelationship(DbEntity eSourse, DbEntity eTarget) {
InferredRelationship myir = new InferredRelationship();
for (DbRelationship relationship : eSourse.getRelationships()) {
for (DbJoin join : relationship.getJoins()) {
if (((DbEntity) join.getSource().getEntity()).equals(eSourse) && ((DbEntity) join.getTarget().getEntity()).equals(eTarget)) {
return;
}
}
}
myir.setSource(eSourse);
myir.setTarget(eTarget);
inferredRelationships.add(myir);
}
Aggregations