Search in sources :

Example 96 with DbAttribute

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;
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) DbEntity(org.apache.cayenne.map.DbEntity) Entity(org.apache.cayenne.map.Entity) CayenneMapEntry(org.apache.cayenne.util.CayenneMapEntry) DbRelationship(org.apache.cayenne.map.DbRelationship) DbAttribute(org.apache.cayenne.map.DbAttribute)

Example 97 with DbAttribute

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());
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) ObjAttribute(org.apache.cayenne.map.ObjAttribute) DbAttribute(org.apache.cayenne.map.DbAttribute) URL(java.net.URL) DataMap(org.apache.cayenne.map.DataMap) Test(org.junit.Test)

Example 98 with DbAttribute

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());
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) ObjRelationship(org.apache.cayenne.map.ObjRelationship) XMLEncoder(org.apache.cayenne.util.XMLEncoder) EmptyConfigurationNodeVisitor(org.apache.cayenne.configuration.EmptyConfigurationNodeVisitor) DbEntity(org.apache.cayenne.map.DbEntity) ObjAttribute(org.apache.cayenne.map.ObjAttribute) DbRelationship(org.apache.cayenne.map.DbRelationship) DbAttribute(org.apache.cayenne.map.DbAttribute) PrintWriter(java.io.PrintWriter)

Example 99 with DbAttribute

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();
}
Also used : DbAttribute(org.apache.cayenne.map.DbAttribute) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) QuotingStrategy(org.apache.cayenne.dba.QuotingStrategy)

Example 100 with DbAttribute

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(')');
    }
}
Also used : DbAttribute(org.apache.cayenne.map.DbAttribute) ArrayList(java.util.ArrayList)

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