Search in sources :

Example 86 with DataMap

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

the class CayenneIT method testScalarObjectForQuery.

@Test
public void testScalarObjectForQuery() throws Exception {
    createTwoArtists();
    String sql = "SELECT count(1) AS X FROM ARTIST";
    DataMap map = context.getEntityResolver().getDataMap("testmap");
    SQLTemplate query = new SQLTemplate(map, sql, false);
    query.setTemplate(FrontBaseAdapter.class.getName(), "SELECT COUNT(ARTIST_ID) AS X FROM ARTIST");
    query.setTemplate(OpenBaseAdapter.class.getName(), "SELECT COUNT(ARTIST_ID) AS X FROM ARTIST");
    query.setColumnNamesCapitalization(CapsStrategy.UPPER);
    SQLResult rsMap = new SQLResult();
    rsMap.addColumnResult("X");
    query.setResult(rsMap);
    Object object = Cayenne.objectForQuery(context, query);
    assertNotNull(object);
    assertTrue(object instanceof Number);
    assertEquals(2, ((Number) object).intValue());
}
Also used : SQLTemplate(org.apache.cayenne.query.SQLTemplate) SQLResult(org.apache.cayenne.map.SQLResult) FrontBaseAdapter(org.apache.cayenne.dba.frontbase.FrontBaseAdapter) OpenBaseAdapter(org.apache.cayenne.dba.openbase.OpenBaseAdapter) DataMap(org.apache.cayenne.map.DataMap) Test(org.junit.Test)

Example 87 with DataMap

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

the class OraclePkGenerator method dropAutoPk.

/**
 * Drops PK sequences for all specified DbEntities.
 */
@Override
public void dropAutoPk(DataNode node, List<DbEntity> dbEntities) throws Exception {
    List<String> sequences = getExistingSequences(node);
    // drop obsolete sequences
    for (DbEntity dbEntity : dbEntities) {
        String name;
        if (dbEntity.getDataMap().isQuotingSQLIdentifiers()) {
            DbEntity tempEnt = new DbEntity();
            DataMap dm = new DataMap();
            dm.setQuotingSQLIdentifiers(false);
            tempEnt.setDataMap(dm);
            tempEnt.setName(dbEntity.getName());
            name = stripSchemaName(sequenceName(tempEnt));
        } else {
            name = stripSchemaName(sequenceName(dbEntity));
        }
        if (sequences.contains(name)) {
            runUpdate(node, dropSequenceString(dbEntity));
        }
    }
}
Also used : DbEntity(org.apache.cayenne.map.DbEntity) DataMap(org.apache.cayenne.map.DataMap)

Example 88 with DataMap

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

the class ProjectUtil method setDbEntityName.

/**
 * Renames a DbEntity and changes the name of all references.
 */
public static void setDbEntityName(DbEntity entity, String newName) {
    String oldName = entity.getName();
    // If name hasn't changed, just return
    if (Util.nullSafeEquals(oldName, newName)) {
        return;
    }
    entity.setName(newName);
    DataMap map = entity.getDataMap();
    if (map != null) {
        map.removeDbEntity(oldName, false);
        map.addDbEntity(entity);
        // important - clear parent namespace:
        MappingNamespace ns = map.getNamespace();
        if (ns instanceof EntityResolver) {
            ((EntityResolver) ns).refreshMappingCache();
        }
    }
}
Also used : EntityResolver(org.apache.cayenne.map.EntityResolver) MappingNamespace(org.apache.cayenne.map.MappingNamespace) DataMap(org.apache.cayenne.map.DataMap)

Example 89 with DataMap

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

the class ProjectUtil method getRelationshipsUsingAttributeAsTarget.

/**
 * Returns a collection of DbRelationships that use this attribute as a source.
 */
public static Collection<DbRelationship> getRelationshipsUsingAttributeAsTarget(DbAttribute attribute) {
    Entity parent = attribute.getEntity();
    if (parent == null) {
        return Collections.EMPTY_LIST;
    }
    DataMap map = parent.getDataMap();
    if (map == null) {
        return Collections.EMPTY_LIST;
    }
    Collection<DbRelationship> relationships = new ArrayList<DbRelationship>();
    for (Entity entity : map.getDbEntities()) {
        if (entity == parent) {
            continue;
        }
        Collection<DbRelationship> entityRelationships = (Collection<DbRelationship>) entity.getRelationships();
        for (DbRelationship relationship : entityRelationships) {
            if (ProjectUtil.containsTargetAttribute(relationship, attribute)) {
                relationships.add(relationship);
            }
        }
    }
    return relationships;
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) Entity(org.apache.cayenne.map.Entity) DbEntity(org.apache.cayenne.map.DbEntity) DbRelationship(org.apache.cayenne.map.DbRelationship) ArrayList(java.util.ArrayList) Collection(java.util.Collection) DataMap(org.apache.cayenne.map.DataMap)

Example 90 with DataMap

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

the class ProjectUtil method findObjAttributesForDbRelationship.

public static Collection<ObjAttribute> findObjAttributesForDbRelationship(ProjectController mediator, DbRelationship relationship) {
    DataChannelDescriptor domain = (DataChannelDescriptor) mediator.getProject().getRootNode();
    List<ObjAttribute> attributes = new ArrayList<>();
    String[] dbAttrPathByDot;
    if (domain != null) {
        for (DataMap map : domain.getDataMaps()) {
            for (ObjEntity entity : map.getObjEntities()) {
                for (ObjAttribute objAttribute : entity.getAttributes()) {
                    if (objAttribute.getDbAttributePath() != null) {
                        dbAttrPathByDot = objAttribute.getDbAttributePath().split(Pattern.quote("."));
                        for (String partOfPath : dbAttrPathByDot) {
                            if (partOfPath.equals(relationship.getName())) {
                                attributes.add(objAttribute);
                            }
                        }
                    }
                }
            }
        }
    }
    return attributes;
}
Also used : DataChannelDescriptor(org.apache.cayenne.configuration.DataChannelDescriptor) ObjEntity(org.apache.cayenne.map.ObjEntity) ObjAttribute(org.apache.cayenne.map.ObjAttribute) ArrayList(java.util.ArrayList) DataMap(org.apache.cayenne.map.DataMap)

Aggregations

DataMap (org.apache.cayenne.map.DataMap)233 Test (org.junit.Test)88 DataChannelDescriptor (org.apache.cayenne.configuration.DataChannelDescriptor)48 DbEntity (org.apache.cayenne.map.DbEntity)48 ObjEntity (org.apache.cayenne.map.ObjEntity)45 DataNodeDescriptor (org.apache.cayenne.configuration.DataNodeDescriptor)22 URL (java.net.URL)21 URLResource (org.apache.cayenne.resource.URLResource)20 ArrayList (java.util.ArrayList)19 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)19 MergerToken (org.apache.cayenne.dbsync.merge.token.MergerToken)17 QueryDescriptor (org.apache.cayenne.map.QueryDescriptor)16 DataMapEvent (org.apache.cayenne.configuration.event.DataMapEvent)15 File (java.io.File)14 Procedure (org.apache.cayenne.map.Procedure)14 Embeddable (org.apache.cayenne.map.Embeddable)13 Injector (org.apache.cayenne.di.Injector)12 DbAttribute (org.apache.cayenne.map.DbAttribute)11 EntityResolver (org.apache.cayenne.map.EntityResolver)11 Entity (org.apache.cayenne.map.Entity)10