Search in sources :

Example 26 with DbAttribute

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

the class EJBQLPathTranslator method processTerminatingAttribute.

protected void processTerminatingAttribute(ObjAttribute attribute) {
    DbEntity table = null;
    Iterator<?> it = attribute.getDbPathIterator();
    while (it.hasNext()) {
        Object pathComponent = it.next();
        if (pathComponent instanceof DbAttribute) {
            table = (DbEntity) ((DbAttribute) pathComponent).getEntity();
        }
    }
    if (isUsingAliases()) {
        String alias = this.lastAlias != null ? lastAlias : context.getTableAlias(idPath, context.getQuotingStrategy().quotedFullyQualifiedName(table));
        context.append(' ').append(alias).append('.').append(context.getQuotingStrategy().quotedName(attribute.getDbAttribute()));
    } else {
        context.append(' ').append(context.getQuotingStrategy().quotedName(attribute.getDbAttribute()));
    }
}
Also used : DbEntity(org.apache.cayenne.map.DbEntity) DbAttribute(org.apache.cayenne.map.DbAttribute)

Example 27 with DbAttribute

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

the class EJBQLPathTranslator method processTerminatingRelationship.

protected void processTerminatingRelationship(ObjRelationship relationship) {
    if (relationship.isSourceIndependentFromTargetChange()) {
        // (andrus) use an outer join for to-many matches.. This is somewhat
        // different
        // from traditional Cayenne SelectQuery, as EJBQL spec does not
        // allow regular
        // path matches done against to-many relationships, and instead
        // provides
        // MEMBER OF and IS EMPTY operators. Outer join is needed for IS
        // EMPTY... I
        // guess MEMBER OF could've been done with an inner join though..
        this.innerJoin = false;
        resolveJoin();
        DbRelationship dbRelationship = chooseDbRelationship(relationship);
        DbEntity table = (DbEntity) dbRelationship.getTargetEntity();
        String alias = this.lastAlias != null ? lastAlias : context.getTableAlias(idPath, context.getQuotingStrategy().quotedFullyQualifiedName(table));
        Collection<DbAttribute> pks = table.getPrimaryKeys();
        if (pks.size() == 1) {
            DbAttribute pk = pks.iterator().next();
            context.append(' ');
            if (isUsingAliases()) {
                context.append(alias).append('.');
            }
            context.append(context.getQuotingStrategy().quotedName(pk));
        } else {
            throw new EJBQLException("Multi-column PK to-many matches are not yet supported.");
        }
    } else {
        // match FK against the target object
        DbRelationship dbRelationship = chooseDbRelationship(relationship);
        DbEntity table = (DbEntity) dbRelationship.getSourceEntity();
        String alias = this.lastAlias != null ? lastAlias : context.getTableAlias(idPath, context.getQuotingStrategy().quotedFullyQualifiedName(table));
        List<DbJoin> joins = dbRelationship.getJoins();
        if (joins.size() == 1) {
            DbJoin join = joins.get(0);
            context.append(' ');
            if (isUsingAliases()) {
                context.append(alias).append('.');
            }
            context.append(context.getQuotingStrategy().quotedName(join.getSource()));
        } else {
            Map<String, String> multiColumnMatch = new HashMap<>(joins.size() + 2);
            for (DbJoin join : joins) {
                String column = isUsingAliases() ? alias + "." + join.getSourceName() : join.getSourceName();
                multiColumnMatch.put(join.getTargetName(), column);
            }
            appendMultiColumnPath(EJBQLMultiColumnOperand.getPathOperand(context, multiColumnMatch));
        }
    }
}
Also used : DbEntity(org.apache.cayenne.map.DbEntity) HashMap(java.util.HashMap) DbRelationship(org.apache.cayenne.map.DbRelationship) DbAttribute(org.apache.cayenne.map.DbAttribute) EJBQLException(org.apache.cayenne.ejbql.EJBQLException) DbJoin(org.apache.cayenne.map.DbJoin)

Example 28 with DbAttribute

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

the class SetAllowNullToDbIT method test.

@Test
public void test() throws Exception {
    DbEntity dbEntity = map.getDbEntity("PAINTING");
    assertNotNull(dbEntity);
    // create and add new column to model and db
    DbAttribute column = new DbAttribute("NEWCOL2", Types.VARCHAR, dbEntity);
    try {
        column.setMandatory(true);
        column.setMaxLength(10);
        dbEntity.addAttribute(column);
        assertTokensAndExecute(2, 0);
        // check that is was merged
        assertTokensAndExecute(0, 0);
        // set null
        column.setMandatory(false);
        // merge to db
        assertTokensAndExecute(1, 0);
        // check that is was merged
        assertTokensAndExecute(0, 0);
    // clean up
    } finally {
        dbEntity.removeAttribute(column.getName());
        assertTokensAndExecute(1, 0);
        assertTokensAndExecute(0, 0);
    }
}
Also used : DbEntity(org.apache.cayenne.map.DbEntity) DbAttribute(org.apache.cayenne.map.DbAttribute) Test(org.junit.Test)

Example 29 with DbAttribute

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

the class SetGeneratedFlagToDbIT method dropGeneratedFlag.

@Test
public void dropGeneratedFlag() throws Exception {
    DbEntity dbEntity = createTestTable(true);
    assertNotNull(dbEntity);
    DbAttribute attribute = dbEntity.getAttribute("ID");
    assertNotNull(attribute);
    assertTrue(attribute.isGenerated());
    attribute.setGenerated(false);
    List<MergerToken> tokens = createMergeTokens();
    if (!dbAdapter.supportsGeneratedKeys()) {
        assertEquals(0, tokens.size());
        return;
    }
    assertEquals(1, tokens.size());
    MergerToken token = tokens.get(0);
    assertTrue(token instanceof SetGeneratedFlagToDb);
    try {
        execute(token);
        if (!dbAdapter.supportsGeneratedKeysDrop()) {
            fail("SetGeneratedFlagToDb should fail on current DB");
        }
    } catch (UnsupportedOperationException ignored) {
        return;
    }
    assertTokensAndExecute(0, 0);
}
Also used : DbEntity(org.apache.cayenne.map.DbEntity) DbAttribute(org.apache.cayenne.map.DbAttribute) MergerToken(org.apache.cayenne.dbsync.merge.token.MergerToken) Test(org.junit.Test)

Example 30 with DbAttribute

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

the class SetNotNullToDbIT method test.

@Test
public void test() throws Exception {
    DbEntity dbEntity = map.getDbEntity("PAINTING");
    assertNotNull(dbEntity);
    // create and add new column to model and db
    DbAttribute column = new DbAttribute("NEWCOL2", Types.VARCHAR, dbEntity);
    column.setMandatory(false);
    column.setMaxLength(10);
    dbEntity.addAttribute(column);
    assertTokensAndExecute(1, 0);
    // check that is was merged
    assertTokensAndExecute(0, 0);
    // set not null
    column.setMandatory(true);
    // merge to db
    assertTokensAndExecute(1, 0);
    // check that is was merged
    assertTokensAndExecute(0, 0);
    // clean up
    dbEntity.removeAttribute(column.getName());
    assertTokensAndExecute(1, 0);
    assertTokensAndExecute(0, 0);
}
Also used : DbEntity(org.apache.cayenne.map.DbEntity) DbAttribute(org.apache.cayenne.map.DbAttribute) Test(org.junit.Test)

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