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