Search in sources :

Example 41 with DbEntity

use of org.apache.cayenne.map.DbEntity 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() + "'");
        }
    };
}
Also used : DbAdapter(org.apache.cayenne.dba.DbAdapter) DbEntity(org.apache.cayenne.map.DbEntity) DropRelationshipToDb(org.apache.cayenne.dbsync.merge.token.db.DropRelationshipToDb) DbJoin(org.apache.cayenne.map.DbJoin)

Example 42 with DbEntity

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

the class SetPrimaryKeyToModel method execute.

@Override
public void execute(MergerContext mergerContext) {
    DbEntity e = getEntity();
    for (DbAttribute attr : e.getAttributes()) {
        boolean wasPrimaryKey = attr.isPrimaryKey();
        boolean willBePrimaryKey = primaryKeyNewAttributeNames.contains(attr.getName().toUpperCase());
        if (wasPrimaryKey != willBePrimaryKey) {
            attr.setPrimaryKey(willBePrimaryKey);
            e.dbAttributeChanged(new AttributeEvent(this, attr, e));
            mergerContext.getDelegate().dbAttributeModified(attr);
        }
    }
}
Also used : DbEntity(org.apache.cayenne.map.DbEntity) DbAttribute(org.apache.cayenne.map.DbAttribute) AttributeEvent(org.apache.cayenne.map.event.AttributeEvent)

Example 43 with DbEntity

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

the class AttributeLoader method processResultSetRow.

@Override
protected void processResultSetRow(CatalogFilter catalog, SchemaFilter schema, DbLoadDataStore map, ResultSet rs) throws SQLException {
    if (firstRow) {
        supportAutoIncrement = checkForAutoIncrement(rs);
        firstRow = false;
    }
    // for a reason not quiet apparent to me, Oracle sometimes
    // returns duplicate record sets for the same table, messing up
    // table names. E.g. for the system table "WK$_ATTR_MAPPING" columns
    // are returned twice - as "WK$_ATTR_MAPPING" and "WK$$_ATTR_MAPPING"...
    // Go figure
    String tableName = rs.getString("TABLE_NAME");
    DbEntity entity = map.getDbEntity(tableName);
    if (entity == null) {
        return;
    }
    // Filter out columns by name
    String columnName = rs.getString("COLUMN_NAME");
    PatternFilter columnFilter = schema.tables.getIncludeTableColumnFilter(tableName);
    if (columnFilter == null || !columnFilter.isIncluded(columnName)) {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("Skip column '" + tableName + "." + columnName + "' (Path: " + catalog.name + "/" + schema.name + "; Filter: " + columnFilter + ")");
        }
        return;
    }
    DbAttribute attribute = createDbAttribute(rs);
    addToDbEntity(entity, attribute);
}
Also used : PatternFilter(org.apache.cayenne.dbsync.reverse.filters.PatternFilter) DbEntity(org.apache.cayenne.map.DbEntity) DbAttribute(org.apache.cayenne.map.DbAttribute)

Example 44 with DbEntity

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

the class DbLoadDataStore method addDbEntitySafe.

DbEntity addDbEntitySafe(DbEntity entity) {
    if (!(entity instanceof DetectedDbEntity)) {
        throw new IllegalArgumentException("Only DetectedDbEntity can be inserted in this map");
    }
    DbEntity old = getDbEntity(entity.getName());
    if (old != null) {
        removeDbEntity(old.getName());
    }
    addDbEntity(entity);
    return old;
}
Also used : DetectedDbEntity(org.apache.cayenne.map.DetectedDbEntity) DbEntity(org.apache.cayenne.map.DbEntity) DetectedDbEntity(org.apache.cayenne.map.DetectedDbEntity)

Example 45 with DbEntity

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

the class EntityLoader method addDbEntityToMap.

private void addDbEntityToMap(DetectedDbEntity table, DbLoadDataStore map) {
    DbEntity oldEnt = map.addDbEntitySafe(table);
    if (oldEnt != null) {
        LOGGER.debug("Overwrite DbEntity: " + oldEnt.getName());
        delegate.dbEntityRemoved(oldEnt);
    }
    delegate.dbEntityAdded(table);
}
Also used : DetectedDbEntity(org.apache.cayenne.map.DetectedDbEntity) DbEntity(org.apache.cayenne.map.DbEntity)

Aggregations

DbEntity (org.apache.cayenne.map.DbEntity)273 DbAttribute (org.apache.cayenne.map.DbAttribute)106 Test (org.junit.Test)106 ObjEntity (org.apache.cayenne.map.ObjEntity)64 DbRelationship (org.apache.cayenne.map.DbRelationship)55 DataMap (org.apache.cayenne.map.DataMap)47 ObjAttribute (org.apache.cayenne.map.ObjAttribute)26 ArrayList (java.util.ArrayList)25 DbJoin (org.apache.cayenne.map.DbJoin)24 MergerToken (org.apache.cayenne.dbsync.merge.token.MergerToken)20 ObjRelationship (org.apache.cayenne.map.ObjRelationship)19 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)16 JdbcAdapter (org.apache.cayenne.dba.JdbcAdapter)16 Entity (org.apache.cayenne.map.Entity)16 List (java.util.List)15 DbAdapter (org.apache.cayenne.dba.DbAdapter)15 EntityEvent (org.apache.cayenne.map.event.EntityEvent)14 HashMap (java.util.HashMap)12 SelectQuery (org.apache.cayenne.query.SelectQuery)12 DataChannelDescriptor (org.apache.cayenne.configuration.DataChannelDescriptor)11