001: package net.sourceforge.squirrel_sql.plugins.refactoring;
002:
003: import java.util.ArrayList;
004: import java.util.List;
005:
006: import net.sourceforge.squirrel_sql.fw.dialects.HibernateDialect;
007: import net.sourceforge.squirrel_sql.fw.sql.TableColumnInfo;
008:
009: import org.hibernate.HibernateException;
010:
011: public class DBUtil {
012:
013: /**
014: *
015: * @param info
016: * @param dialect
017: * @return
018: * @throws UnsupportedOperationException
019: * if the specified dialect doesn't support
020: * @throws HibernateException
021: * if the type in the specified info isn't supported by this
022: * dialect.
023: */
024: public static String[] getAlterSQLForColumnAddition(
025: TableColumnInfo info, HibernateDialect dialect)
026: throws HibernateException, UnsupportedOperationException {
027: ArrayList<String> result = new ArrayList<String>();
028:
029: String[] addSQLs = dialect.getColumnAddSQL(info);
030:
031: for (int i = 0; i < addSQLs.length; i++) {
032: String addSQL = addSQLs[i];
033: result.add(addSQL);
034: }
035:
036: return result.toArray(new String[result.size()]);
037: }
038:
039: public static String[] getAlterSQLForColumnChange(
040: TableColumnInfo from, TableColumnInfo to,
041: HibernateDialect dialect) {
042: ArrayList<String> result = new ArrayList<String>();
043: // It is important to process the name change first - so that we can use
044: // the new name instead of the old in subsequent alterations
045: String nameSQL = getColumnNameAlterSQL(from, to, dialect);
046: if (nameSQL != null) {
047: result.add(nameSQL);
048: }
049: String nullSQL = getNullAlterSQL(from, to, dialect);
050: if (nullSQL != null) {
051: result.add(nullSQL);
052: }
053: String commentSQL = getCommentAlterSQL(from, to, dialect);
054: if (commentSQL != null) {
055: result.add(commentSQL);
056: }
057: List<String> typeSQL = getTypeAlterSQL(from, to, dialect);
058: if (typeSQL != null) {
059: result.addAll(typeSQL);
060: }
061: String defaultSQL = getAlterSQLForColumnDefault(from, to,
062: dialect);
063: if (defaultSQL != null) {
064: result.add(defaultSQL);
065: }
066: return result.toArray(new String[result.size()]);
067: }
068:
069: public static List<String> getTypeAlterSQL(TableColumnInfo from,
070: TableColumnInfo to, HibernateDialect dialect) {
071: if (from.getDataType() == to.getDataType()
072: && from.getColumnSize() == to.getColumnSize()) {
073: return null;
074: }
075: return dialect.getColumnTypeAlterSQL(from, to);
076: }
077:
078: public static String getColumnNameAlterSQL(TableColumnInfo from,
079: TableColumnInfo to, HibernateDialect dialect) {
080: if (from.getColumnName().equals(to.getColumnName())) {
081: return null;
082: }
083: return dialect.getColumnNameAlterSQL(from, to);
084: }
085:
086: public static String getNullAlterSQL(TableColumnInfo from,
087: TableColumnInfo to, HibernateDialect dialect) {
088: if (from.isNullable().equalsIgnoreCase(to.isNullable())) {
089: return null;
090: }
091: return dialect.getColumnNullableAlterSQL(to);
092: }
093:
094: public static String getCommentAlterSQL(TableColumnInfo from,
095: TableColumnInfo to, HibernateDialect dialect) {
096: String oldComment = from.getRemarks();
097: String newComment = to.getRemarks();
098: if (!dialect.supportsColumnComment()) {
099: return null;
100: }
101: if (oldComment == null && newComment == null) {
102: return null;
103: }
104: if (oldComment == null || !oldComment.equals(newComment)) {
105: return dialect.getColumnCommentAlterSQL(to);
106: }
107: return null;
108: }
109:
110: public static String getAlterSQLForColumnDefault(
111: TableColumnInfo from, TableColumnInfo to,
112: HibernateDialect dialect) {
113: String oldDefault = from.getDefaultValue();
114: String newDefault = to.getDefaultValue();
115: // empty string ('') seems to be represented as null in some drivers.
116: // Not sure if this is the best thing to do here, but it fixes an issue
117: // where SQL returns is set default to '', when it is already null.
118: if (oldDefault == null) {
119: oldDefault = "";
120: }
121: if (newDefault == null) {
122: newDefault = "";
123: }
124: if (!oldDefault.equals(newDefault)) {
125: if (!dialect.supportsAlterColumnDefault()) {
126: throw new UnsupportedOperationException(
127: dialect.getDisplayName()
128: + " doesn't support column default value alterations");
129: }
130: return dialect.getColumnDefaultAlterSQL(to);
131: }
132: return null;
133: }
134: }
|