use of org.apache.cayenne.map.DbAttribute in project cayenne by apache.
the class DbAttributePathComboBoxEditor method getFirstEntity.
private Entity getFirstEntity(ObjAttribute attribute) {
Iterator<CayenneMapEntry> it = attribute.getDbPathIterator();
Entity firstEnt = attribute.getDbAttribute().getEntity();
boolean setEnt = false;
while (it.hasNext()) {
Object ob = it.next();
if (ob instanceof DbRelationship) {
if (!setEnt) {
firstEnt = ((DbRelationship) ob).getSourceEntity();
setEnt = true;
}
} else if (ob instanceof DbAttribute) {
if (!setEnt) {
firstEnt = ((DbAttribute) ob).getEntity();
}
}
}
return firstEnt;
}
use of org.apache.cayenne.map.DbAttribute in project cayenne by apache.
the class EOModelProcessorTest method testLoadModelWithDependencies.
@Test
public void testLoadModelWithDependencies() throws Exception {
URL url = getClass().getClassLoader().getResource("wotests/cross-model-relationships.eomodeld/");
assertNotNull(url);
DataMap map = processor.loadEOModel(url);
ObjEntity entity = map.getObjEntity("CrossModelRelTest");
assertNotNull(entity);
ObjAttribute a1 = entity.getAttribute("testAttribute");
assertNotNull(a1);
DbAttribute da1 = a1.getDbAttribute();
assertNotNull(da1);
assertSame(da1, entity.getDbEntity().getAttribute("TEST_ATTRIBUTE"));
// for now cross model relationships are simply ignored
// eventually we must handle those...
assertEquals(0, entity.getRelationships().size());
assertEquals(0, entity.getDbEntity().getRelationships().size());
}
use of org.apache.cayenne.map.DbAttribute in project cayenne by apache.
the class EOModelProcessorTest method assertLoaded.
protected void assertLoaded(String mapName, DataMap map) throws Exception {
assertNotNull(map);
assertEquals(mapName, map.getName());
// check obj entities
ObjEntity artistE = map.getObjEntity("Artist");
assertNotNull(artistE);
assertEquals("Artist", artistE.getName());
// check Db entities
DbEntity artistDE = map.getDbEntity("ARTIST");
DbEntity artistDE1 = artistE.getDbEntity();
assertSame(artistDE, artistDE1);
// check attributes
ObjAttribute a1 = artistE.getAttribute("artistName");
assertNotNull(a1);
DbAttribute da1 = a1.getDbAttribute();
assertNotNull(da1);
assertSame(da1, artistDE.getAttribute("ARTIST_NAME"));
// check ObjRelationships
ObjRelationship rel = artistE.getRelationship("artistExhibitArray");
assertNotNull(rel);
assertEquals(1, rel.getDbRelationships().size());
// check DbRelationships
DbRelationship drel = artistDE.getRelationship("artistExhibitArray");
assertNotNull(drel);
assertSame(drel, rel.getDbRelationships().get(0));
// flattened relationships
ObjRelationship frel = artistE.getRelationship("exhibitArray");
assertNotNull(frel);
assertEquals(2, frel.getDbRelationships().size());
// storing data map may uncover some inconsistencies
PrintWriter mockupWriter = new NullPrintWriter();
map.encodeAsXML(new XMLEncoder(mockupWriter), new EmptyConfigurationNodeVisitor());
}
use of org.apache.cayenne.map.DbAttribute in project cayenne by apache.
the class FrontBaseAdapter method createTable.
/**
* Customizes table creating procedure for FrontBase.
*/
@Override
public String createTable(DbEntity ent) {
QuotingStrategy context = getQuotingStrategy();
StringBuilder buf = new StringBuilder();
buf.append("CREATE TABLE ");
buf.append(context.quotedFullyQualifiedName(ent));
buf.append(" (");
// columns
Iterator<DbAttribute> it = ent.getAttributes().iterator();
boolean first = true;
while (it.hasNext()) {
if (first) {
first = false;
} else {
buf.append(", ");
}
DbAttribute at = it.next();
// attribute may not be fully valid, do a simple check
if (at.getType() == TypesMapping.NOT_DEFINED) {
throw new CayenneRuntimeException("Undefined type for attribute '%s.%s'.", ent.getFullyQualifiedName(), at.getName());
}
String[] types = externalTypesForJdbcType(at.getType());
if (types == null || types.length == 0) {
throw new CayenneRuntimeException("Undefined type for attribute '%s.%s': %s", ent.getFullyQualifiedName(), at.getName(), at.getType());
}
String type = types[0];
buf.append(context.quotedName(at)).append(' ').append(type);
// to be the limit for FB)
if (at.getType() == Types.LONGVARCHAR) {
int len = at.getMaxLength() > 0 ? at.getMaxLength() : 1073741824;
buf.append("(").append(len).append(")");
} else if (at.getType() == Types.VARBINARY || at.getType() == Types.BINARY) {
// use a BIT column with size * 8
int len = at.getMaxLength() > 0 ? at.getMaxLength() : 1073741824;
len *= 8;
buf.append("(").append(len).append(")");
} else if (typeSupportsLength(at.getType())) {
int len = at.getMaxLength();
int scale = TypesMapping.isDecimal(at.getType()) ? at.getScale() : -1;
// sanity check
if (scale > len) {
scale = -1;
}
if (len > 0) {
buf.append('(').append(len);
if (scale >= 0) {
buf.append(", ").append(scale);
}
buf.append(')');
}
}
if (at.isMandatory()) {
buf.append(" NOT NULL");
}
// else: don't appen NULL for FrontBase:
}
// primary key clause
Iterator<DbAttribute> pkit = ent.getPrimaryKeys().iterator();
if (pkit.hasNext()) {
if (first) {
first = false;
} else {
buf.append(", ");
}
buf.append("PRIMARY KEY (");
boolean firstPk = true;
while (pkit.hasNext()) {
if (firstPk) {
firstPk = false;
} else {
buf.append(", ");
}
DbAttribute at = pkit.next();
buf.append(quotingStrategy.quotedName(at));
}
buf.append(')');
}
buf.append(')');
return buf.toString();
}
use of org.apache.cayenne.map.DbAttribute in project cayenne by apache.
the class MySQLAdapter method createTableAppendPKClause.
/**
* Customizes PK clause semantics to ensure that generated columns are in
* the beginning of the PK definition, as this seems to be a requirement for
* InnoDB tables.
*
* @since 1.2
*/
// See CAY-358 for details of the InnoDB problem
@Override
protected void createTableAppendPKClause(StringBuffer sqlBuffer, DbEntity entity) {
// must move generated to the front...
List<DbAttribute> pkList = new ArrayList<>(entity.getPrimaryKeys());
Collections.sort(pkList, new PKComparator());
Iterator<DbAttribute> pkit = pkList.iterator();
if (pkit.hasNext()) {
sqlBuffer.append(", PRIMARY KEY (");
boolean firstPk = true;
while (pkit.hasNext()) {
if (firstPk) {
firstPk = false;
} else {
sqlBuffer.append(", ");
}
DbAttribute at = pkit.next();
sqlBuffer.append(quotingStrategy.quotedName(at));
}
sqlBuffer.append(')');
}
}
Aggregations