Search in sources :

Example 26 with QuotingStrategy

use of org.apache.cayenne.dba.QuotingStrategy in project cayenne by apache.

the class IngresMergerTokenFactory method createDropColumnToDb.

@Override
public MergerToken createDropColumnToDb(DbEntity entity, DbAttribute column) {
    return new DropColumnToDb(entity, column) {

        @Override
        public List<String> createSql(DbAdapter adapter) {
            StringBuilder buf = new StringBuilder();
            QuotingStrategy context = adapter.getQuotingStrategy();
            buf.append("ALTER TABLE ");
            buf.append(context.quotedFullyQualifiedName(getEntity()));
            buf.append(" DROP COLUMN ");
            buf.append(context.quotedName(getColumn()));
            buf.append(" RESTRICT ");
            return Collections.singletonList(buf.toString());
        }
    };
}
Also used : DropColumnToDb(org.apache.cayenne.dbsync.merge.token.db.DropColumnToDb) DbAdapter(org.apache.cayenne.dba.DbAdapter) QuotingStrategy(org.apache.cayenne.dba.QuotingStrategy)

Example 27 with QuotingStrategy

use of org.apache.cayenne.dba.QuotingStrategy 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 28 with QuotingStrategy

use of org.apache.cayenne.dba.QuotingStrategy in project cayenne by apache.

the class IngresMergerTokenFactory method createSetAllowNullToDb.

@Override
public MergerToken createSetAllowNullToDb(DbEntity entity, DbAttribute column) {
    return new SetAllowNullToDb(entity, column) {

        @Override
        public List<String> createSql(DbAdapter adapter) {
            StringBuilder sqlBuffer = new StringBuilder();
            QuotingStrategy context = adapter.getQuotingStrategy();
            sqlBuffer.append("ALTER TABLE ");
            sqlBuffer.append(context.quotedFullyQualifiedName(getEntity()));
            sqlBuffer.append(" ALTER COLUMN ");
            sqlBuffer.append(context.quotedName(getColumn()));
            sqlBuffer.append(" ");
            sqlBuffer.append(adapter.externalTypesForJdbcType(getColumn().getType())[0]);
            if (adapter.typeSupportsLength(getColumn().getType()) && getColumn().getMaxLength() > 0) {
                sqlBuffer.append("(");
                sqlBuffer.append(getColumn().getMaxLength());
                sqlBuffer.append(")");
            }
            sqlBuffer.append(" WITH NULL");
            return Collections.singletonList(sqlBuffer.toString());
        }
    };
}
Also used : DbAdapter(org.apache.cayenne.dba.DbAdapter) SetAllowNullToDb(org.apache.cayenne.dbsync.merge.token.db.SetAllowNullToDb) QuotingStrategy(org.apache.cayenne.dba.QuotingStrategy)

Example 29 with QuotingStrategy

use of org.apache.cayenne.dba.QuotingStrategy in project cayenne by apache.

the class MySQLMergerTokenFactory method createSetAllowNullToDb.

@Override
public MergerToken createSetAllowNullToDb(final DbEntity entity, final DbAttribute column) {
    return new SetAllowNullToDb(entity, column) {

        @Override
        public List<String> createSql(DbAdapter adapter) {
            StringBuffer sqlBuffer = new StringBuffer();
            QuotingStrategy context = adapter.getQuotingStrategy();
            sqlBuffer.append("ALTER TABLE ");
            sqlBuffer.append(context.quotedFullyQualifiedName(getEntity()));
            sqlBuffer.append(" CHANGE ");
            sqlBuffer.append(context.quotedName(getColumn()));
            sqlBuffer.append(" ");
            adapter.createTableAppendColumn(sqlBuffer, column);
            return Collections.singletonList(sqlBuffer.toString());
        }
    };
}
Also used : DbAdapter(org.apache.cayenne.dba.DbAdapter) SetAllowNullToDb(org.apache.cayenne.dbsync.merge.token.db.SetAllowNullToDb) QuotingStrategy(org.apache.cayenne.dba.QuotingStrategy)

Example 30 with QuotingStrategy

use of org.apache.cayenne.dba.QuotingStrategy in project cayenne by apache.

the class MySQLMergerTokenFactory method createDropRelationshipToDb.

@Override
public MergerToken createDropRelationshipToDb(final DbEntity entity, DbRelationship rel) {
    return new DropRelationshipToDb(entity, rel) {

        @Override
        public List<String> createSql(DbAdapter adapter) {
            String fkName = getFkName();
            if (fkName == null) {
                return Collections.emptyList();
            }
            QuotingStrategy context = adapter.getQuotingStrategy();
            // http://dev.mysql.com/tech-resources/articles/mysql-cluster-50.html
            return Collections.singletonList("ALTER TABLE " + context.quotedFullyQualifiedName(entity) + " DROP FOREIGN KEY " + fkName);
        }
    };
}
Also used : DbAdapter(org.apache.cayenne.dba.DbAdapter) DropRelationshipToDb(org.apache.cayenne.dbsync.merge.token.db.DropRelationshipToDb) QuotingStrategy(org.apache.cayenne.dba.QuotingStrategy)

Aggregations

QuotingStrategy (org.apache.cayenne.dba.QuotingStrategy)42 DbAdapter (org.apache.cayenne.dba.DbAdapter)11 DbAttribute (org.apache.cayenne.map.DbAttribute)11 DbEntity (org.apache.cayenne.map.DbEntity)5 DbJoin (org.apache.cayenne.map.DbJoin)5 DbRelationship (org.apache.cayenne.map.DbRelationship)5 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)4 SetAllowNullToDb (org.apache.cayenne.dbsync.merge.token.db.SetAllowNullToDb)3 SetNotNullToDb (org.apache.cayenne.dbsync.merge.token.db.SetNotNullToDb)3 EJBQLException (org.apache.cayenne.ejbql.EJBQLException)3 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 HashMap (java.util.HashMap)2 List (java.util.List)2 DropColumnToDb (org.apache.cayenne.dbsync.merge.token.db.DropColumnToDb)2 EJBQLPath (org.apache.cayenne.ejbql.parser.EJBQLPath)2 DataMap (org.apache.cayenne.map.DataMap)2 ObjRelationship (org.apache.cayenne.map.ObjRelationship)2 UpdateBatchQuery (org.apache.cayenne.query.UpdateBatchQuery)2 ClassDescriptor (org.apache.cayenne.reflect.ClassDescriptor)2