Search in sources :

Example 1 with AddRelationshipToDb

use of org.apache.cayenne.dbsync.merge.token.db.AddRelationshipToDb in project cayenne by apache.

the class IngresMergerTokenFactory method createAddRelationshipToDb.

@Override
public MergerToken createAddRelationshipToDb(DbEntity entity, final DbRelationship rel) {
    return new AddRelationshipToDb(entity, rel) {

        @Override
        public List<String> createSql(DbAdapter adapter) {
            if (!rel.isToMany() && rel.isToPK() && !rel.isToDependentPK()) {
                DbEntity source = (DbEntity) rel.getSourceEntity();
                QuotingStrategy context = adapter.getQuotingStrategy();
                StringBuilder buf = new StringBuilder();
                StringBuilder refBuf = new StringBuilder();
                buf.append("ALTER TABLE ");
                buf.append(context.quotedFullyQualifiedName(source));
                // requires the ADD CONSTRAINT statement
                buf.append(" ADD CONSTRAINT ");
                String name = "U_" + rel.getSourceEntity().getName() + "_" + (long) (System.currentTimeMillis() / (Math.random() * 100000));
                buf.append(context.quotedIdentifier(rel.getSourceEntity(), name));
                buf.append(" FOREIGN KEY (");
                boolean first = true;
                for (DbJoin join : rel.getJoins()) {
                    if (!first) {
                        buf.append(", ");
                        refBuf.append(", ");
                    } else {
                        first = false;
                    }
                    buf.append(context.quotedSourceName(join));
                    refBuf.append(context.quotedTargetName(join));
                }
                buf.append(") REFERENCES ");
                buf.append(context.quotedFullyQualifiedName((DbEntity) rel.getTargetEntity()));
                buf.append(" (");
                buf.append(refBuf.toString());
                buf.append(')');
                // also make sure we delete dependent FKs
                buf.append(" ON DELETE CASCADE");
                String fksql = buf.toString();
                if (fksql != null) {
                    return Collections.singletonList(fksql);
                }
            }
            return Collections.emptyList();
        }
    };
}
Also used : DbAdapter(org.apache.cayenne.dba.DbAdapter) DbEntity(org.apache.cayenne.map.DbEntity) AddRelationshipToDb(org.apache.cayenne.dbsync.merge.token.db.AddRelationshipToDb) DbJoin(org.apache.cayenne.map.DbJoin) QuotingStrategy(org.apache.cayenne.dba.QuotingStrategy)

Example 2 with AddRelationshipToDb

use of org.apache.cayenne.dbsync.merge.token.db.AddRelationshipToDb in project cayenne by apache.

the class TokenSortTest method testToDbTokensCompare.

@Test
public void testToDbTokensCompare() throws Exception {
    List<MergerToken> tokens = Arrays.<MergerToken>asList(new DropColumnToDb(null, null), new DropRelationshipToDb(null, null), new DropTableToDb(null), new AddColumnToModel(null, null), new AddRelationshipToDb(null, null), new AddColumnToDb(null, null), new CreateTableToDb(null));
    Collections.sort(tokens);
    List<String> actual = toClassesNames(tokens);
    List<String> expected = Arrays.asList("DropRelationshipToDb", "DropColumnToDb", "DropTableToDb", "CreateTableToDb", "AddColumnToDb", "AddColumnToModel", "AddRelationshipToDb");
    assertEquals(expected, actual);
}
Also used : AddColumnToDb(org.apache.cayenne.dbsync.merge.token.db.AddColumnToDb) CreateTableToDb(org.apache.cayenne.dbsync.merge.token.db.CreateTableToDb) DropColumnToDb(org.apache.cayenne.dbsync.merge.token.db.DropColumnToDb) AddRelationshipToDb(org.apache.cayenne.dbsync.merge.token.db.AddRelationshipToDb) DropTableToDb(org.apache.cayenne.dbsync.merge.token.db.DropTableToDb) DropRelationshipToDb(org.apache.cayenne.dbsync.merge.token.db.DropRelationshipToDb) AddColumnToModel(org.apache.cayenne.dbsync.merge.token.model.AddColumnToModel) Test(org.junit.Test)

Aggregations

AddRelationshipToDb (org.apache.cayenne.dbsync.merge.token.db.AddRelationshipToDb)2 DbAdapter (org.apache.cayenne.dba.DbAdapter)1 QuotingStrategy (org.apache.cayenne.dba.QuotingStrategy)1 AddColumnToDb (org.apache.cayenne.dbsync.merge.token.db.AddColumnToDb)1 CreateTableToDb (org.apache.cayenne.dbsync.merge.token.db.CreateTableToDb)1 DropColumnToDb (org.apache.cayenne.dbsync.merge.token.db.DropColumnToDb)1 DropRelationshipToDb (org.apache.cayenne.dbsync.merge.token.db.DropRelationshipToDb)1 DropTableToDb (org.apache.cayenne.dbsync.merge.token.db.DropTableToDb)1 AddColumnToModel (org.apache.cayenne.dbsync.merge.token.model.AddColumnToModel)1 DbEntity (org.apache.cayenne.map.DbEntity)1 DbJoin (org.apache.cayenne.map.DbJoin)1 Test (org.junit.Test)1