Search in sources :

Example 46 with DbAttribute

use of org.apache.cayenne.map.DbAttribute 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 47 with DbAttribute

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

the class PrimaryKeyLoader method processResultSet.

@Override
void processResultSet(DbEntity dbEntity, DbLoadDataStore map, ResultSet rs) throws SQLException {
    String columnName = rs.getString("COLUMN_NAME");
    DbAttribute attribute = dbEntity.getAttribute(columnName);
    if (attribute == null) {
        // why an attribute might be null is not quiet clear
        // but there is a bug report 731406 indicating that it is
        // possible so just print the warning, and ignore
        LOGGER.warn("Can't locate attribute for primary key: " + columnName);
        return;
    }
    attribute.setPrimaryKey(true);
    ((DetectedDbEntity) dbEntity).setPrimaryKeyName(rs.getString("PK_NAME"));
}
Also used : DbAttribute(org.apache.cayenne.map.DbAttribute) DetectedDbEntity(org.apache.cayenne.map.DetectedDbEntity)

Example 48 with DbAttribute

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

the class RelationshipLoader method createAndAppendJoins.

private void createAndAppendJoins(Set<ExportedKey> exportedKeys, DbEntity pkEntity, DbEntity fkEntity, DbRelationship forwardRelationship, DbRelationship reverseRelationship) {
    for (ExportedKey exportedKey : exportedKeys) {
        // Create and append joins
        String pkName = exportedKey.getPk().getColumn();
        String fkName = exportedKey.getFk().getColumn();
        // skip invalid joins...
        DbAttribute pkAtt = pkEntity.getAttribute(pkName);
        if (pkAtt == null) {
            LOGGER.info("no attribute for declared primary key: " + pkName);
            continue;
        }
        DbAttribute fkAtt = fkEntity.getAttribute(fkName);
        if (fkAtt == null) {
            LOGGER.info("no attribute for declared foreign key: " + fkName);
            continue;
        }
        addJoin(forwardRelationship, pkName, fkName);
        addJoin(reverseRelationship, fkName, pkName);
    }
}
Also used : DbAttribute(org.apache.cayenne.map.DbAttribute)

Example 49 with DbAttribute

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

the class MergerFactoryIT method testAddTableToDb.

@Test
public void testAddTableToDb() throws Exception {
    dropTableIfPresent("NEW_TABLE");
    assertTokensAndExecute(0, 0);
    DbEntity dbEntity = new DbEntity("NEW_TABLE");
    DbAttribute column1 = new DbAttribute("ID", Types.INTEGER, dbEntity);
    column1.setMandatory(true);
    column1.setPrimaryKey(true);
    dbEntity.addAttribute(column1);
    DbAttribute column2 = new DbAttribute("NAME", Types.VARCHAR, dbEntity);
    column2.setMaxLength(10);
    column2.setMandatory(false);
    dbEntity.addAttribute(column2);
    map.addDbEntity(dbEntity);
    assertTokensAndExecute(1, 0);
    assertTokensAndExecute(0, 0);
    ObjEntity objEntity = new ObjEntity("NewTable");
    objEntity.setDbEntity(dbEntity);
    ObjAttribute oatr1 = new ObjAttribute("name");
    oatr1.setDbAttributePath(column2.getName());
    oatr1.setType("java.lang.String");
    objEntity.addAttribute(oatr1);
    map.addObjEntity(objEntity);
    for (int i = 0; i < 5; i++) {
        CayenneDataObject dao = (CayenneDataObject) context.newObject(objEntity.getName());
        dao.writeProperty(oatr1.getName(), "test " + i);
    }
    context.commitChanges();
    // 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);
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) CayenneDataObject(org.apache.cayenne.CayenneDataObject) DbEntity(org.apache.cayenne.map.DbEntity) ObjAttribute(org.apache.cayenne.map.ObjAttribute) DbAttribute(org.apache.cayenne.map.DbAttribute) Test(org.junit.Test)

Example 50 with DbAttribute

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

the class MergerFactoryIT method testMultipleTokensToDb.

@Test
public void testMultipleTokensToDb() throws Exception {
    DbEntity dbEntity = map.getDbEntity("PAINTING");
    assertNotNull(dbEntity);
    DbAttribute column1 = new DbAttribute("NEWCOL3", Types.VARCHAR, dbEntity);
    column1.setMandatory(false);
    column1.setMaxLength(10);
    dbEntity.addAttribute(column1);
    DbAttribute column2 = new DbAttribute("NEWCOL4", Types.VARCHAR, dbEntity);
    column2.setMandatory(false);
    column2.setMaxLength(10);
    dbEntity.addAttribute(column2);
    assertTokensAndExecute(2, 0);
    // check that is was merged
    assertTokensAndExecute(0, 0);
    // change size
    column1.setMaxLength(20);
    column2.setMaxLength(30);
    // merge to db
    assertTokensAndExecute(2, 0);
    // check that is was merged
    assertTokensAndExecute(0, 0);
    // clean up
    dbEntity.removeAttribute(column1.getName());
    dbEntity.removeAttribute(column2.getName());
    assertTokensAndExecute(2, 0);
    assertTokensAndExecute(0, 0);
}
Also used : DbEntity(org.apache.cayenne.map.DbEntity) DbAttribute(org.apache.cayenne.map.DbAttribute) Test(org.junit.Test)

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