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