Coverage Report - liquibase.change.ColumnConfig
 
Classes in this File Line Coverage Branch Coverage Complexity
ColumnConfig
54%
73/133
46%
30/64
2.048
 
 1  
 package liquibase.change;
 2  
 
 3  
 import liquibase.database.structure.Column;
 4  
 import liquibase.statement.DatabaseFunction;
 5  
 import liquibase.util.ISODateFormat;
 6  
 
 7  
 import java.text.NumberFormat;
 8  
 import java.text.ParseException;
 9  
 import java.util.Date;
 10  
 import java.util.Locale;
 11  
 
 12  
 /**
 13  
  * This class is the representation of the column tag in the XMl file It has a reference to the Constraints object for
 14  
  * getting information about the columns constraints.
 15  
  */
 16  
 public class ColumnConfig {
 17  
     private String name;
 18  
     private String type;
 19  
     private String value;
 20  
     private Number valueNumeric;
 21  
     private Date valueDate;
 22  
     private Boolean valueBoolean;
 23  
     private DatabaseFunction valueComputed;
 24  
 
 25  
     private String defaultValue;
 26  
     private Number defaultValueNumeric;
 27  
     private Date defaultValueDate;
 28  
     private Boolean defaultValueBoolean;
 29  
     private DatabaseFunction defaultValueComputed;
 30  
 
 31  
     private ConstraintsConfig constraints;
 32  
     private Boolean autoIncrement;
 33  
     private String remarks;
 34  
 
 35  0
     public ColumnConfig(Column columnStructure) {
 36  0
         setName(columnStructure.getName());
 37  0
         setType(columnStructure.getTypeName());
 38  0
         if (columnStructure.getDefaultValue() != null) {
 39  0
             setDefaultValue(columnStructure.getDefaultValue().toString());
 40  
         }
 41  0
         setAutoIncrement(columnStructure.isAutoIncrement());
 42  0
         ConstraintsConfig constraints = new ConstraintsConfig();
 43  0
         constraints.setNullable(columnStructure.isNullable());
 44  0
         constraints.setPrimaryKey(columnStructure.isPrimaryKey());
 45  0
         constraints.setUnique(columnStructure.isUnique());
 46  0
         setConstraints(constraints);
 47  0
     }
 48  
 
 49  0
     public ColumnConfig(ColumnConfig column) {
 50  0
         setName(column.getName());
 51  0
         setType(column.getType());
 52  0
         setDefaultValue(column.getDefaultValue());
 53  0
         setAutoIncrement(column.isAutoIncrement());
 54  0
         if (column.getConstraints() != null) {
 55  0
             ConstraintsConfig constraints = new ConstraintsConfig();
 56  0
             constraints.setNullable(column.getConstraints().isNullable());
 57  0
             constraints.setPrimaryKey(column.getConstraints().isPrimaryKey());
 58  0
             constraints.setPrimaryKeyTablespace(column.getConstraints().getPrimaryKeyTablespace());
 59  0
             constraints.setUnique(column.getConstraints().isUnique());
 60  
         }
 61  0
         setConstraints(constraints);
 62  0
     }
 63  
 
 64  157
     public ColumnConfig() {
 65  
         // do nothing
 66  157
     }
 67  
 
 68  
     public String getName() {
 69  279
         return name;
 70  
     }
 71  
 
 72  
     public ColumnConfig setName(String name) {
 73  129
         this.name = name;
 74  129
         return this;
 75  
     }
 76  
 
 77  
     public String getType() {
 78  255
         return type;
 79  
     }
 80  
 
 81  
     public ColumnConfig setType(String type) {
 82  178
         this.type = type;
 83  178
         return this;
 84  
     }
 85  
 
 86  
     public String getValue() {
 87  193
         return value;
 88  
     }
 89  
 
 90  
     public void setValue(String value) {
 91  
         // Since we have two rules for the value it can either be specifed as an attribute
 92  
         // or as the tag body in case of long values then the check is necessary so that it
 93  
         // should not override the value specifed as an attribute.
 94  
         // if (StringUtils.trimToNull(value) != null) {
 95  
         // this.value = value;
 96  
         // }
 97  
         // TODO find where this is being called with the tag body
 98  
         // and fix the code there. this logic does not belong here
 99  
         // because it prevents a column from being the empty string
 100  13
         this.value = value;
 101  13
     }
 102  
 
 103  
     public Number getValueNumeric() {
 104  131
         return valueNumeric;
 105  
     }
 106  
 
 107  
     public ColumnConfig setValueNumeric(String valueNumeric) {
 108  23
         if (valueNumeric == null || valueNumeric.equalsIgnoreCase("null")) {
 109  0
             this.valueNumeric = null;
 110  
         } else {
 111  23
             valueNumeric = valueNumeric.replaceFirst("^\\(", "");
 112  23
             valueNumeric = valueNumeric.replaceFirst("\\)$", "");
 113  
 
 114  23
             if (valueNumeric.matches("\\d+\\.?\\d*")) {
 115  
                 try {
 116  23
                     this.valueNumeric = NumberFormat.getInstance(Locale.US).parse(valueNumeric);
 117  0
                 } catch (ParseException e) {
 118  0
                     throw new RuntimeException(e);
 119  23
                 }
 120  
             } else {
 121  0
                 this.valueComputed = new DatabaseFunction(valueNumeric);
 122  
             }
 123  
         }
 124  
 
 125  23
         return this;
 126  
     }
 127  
 
 128  
     public ColumnConfig setValueNumeric(Number valueNumeric) {
 129  1
         this.valueNumeric = valueNumeric;
 130  
 
 131  1
         return this;
 132  
     }
 133  
 
 134  
     public Boolean getValueBoolean() {
 135  127
         return valueBoolean;
 136  
     }
 137  
 
 138  
     public ColumnConfig setValueBoolean(Boolean valueBoolean) {
 139  2
         this.valueBoolean = valueBoolean;
 140  
 
 141  2
         return this;
 142  
     }
 143  
 
 144  
     public DatabaseFunction getValueComputed() {
 145  118
         return valueComputed;
 146  
     }
 147  
 
 148  
     public ColumnConfig setValueComputed(DatabaseFunction valueComputed) {
 149  0
         this.valueComputed = valueComputed;
 150  
 
 151  0
         return this;
 152  
     }
 153  
 
 154  
     public Date getValueDate() {
 155  118
         return valueDate;
 156  
     }
 157  
 
 158  
     public ColumnConfig setValueDate(Date valueDate) {
 159  0
         this.valueDate = valueDate;
 160  
 
 161  0
         return this;
 162  
     }
 163  
 
 164  
     public ColumnConfig setValueDate(String valueDate) {
 165  0
         if (valueDate == null || valueDate.equalsIgnoreCase("null")) {
 166  0
             this.valueDate = null;
 167  
         } else {
 168  
             try {
 169  0
                 this.valueDate = new ISODateFormat().parse(valueDate);
 170  0
             } catch (ParseException e) {
 171  
                 // probably a function
 172  0
                 this.valueComputed = new DatabaseFunction(valueDate);
 173  0
             }
 174  
         }
 175  
 
 176  0
         return this;
 177  
     }
 178  
 
 179  
     public Object getValueObject() {
 180  143
         if (getValue() != null) {
 181  31
             return getValue();
 182  112
         } else if (getValueBoolean() != null) {
 183  2
             return getValueBoolean();
 184  110
         } else if (getValueNumeric() != null) {
 185  5
             return getValueNumeric();
 186  105
         } else if (getValueDate() != null) {
 187  0
             return getValueDate();
 188  105
         } else if (getValueComputed() != null) {
 189  0
             return getValueComputed();
 190  
         }
 191  105
         return null;
 192  
     }
 193  
 
 194  
     public String getDefaultValue() {
 195  201
         return defaultValue;
 196  
     }
 197  
 
 198  
     public ColumnConfig setDefaultValue(String defaultValue) {
 199  5
         this.defaultValue = defaultValue;
 200  
 
 201  5
         return this;
 202  
     }
 203  
 
 204  
     public Number getDefaultValueNumeric() {
 205  193
         return defaultValueNumeric;
 206  
     }
 207  
 
 208  
     public ColumnConfig setDefaultValueNumeric(Number defaultValueNumeric) {
 209  0
         this.defaultValueNumeric = defaultValueNumeric;
 210  
 
 211  0
         return this;
 212  
     }
 213  
 
 214  
     public ColumnConfig setDefaultValueNumeric(String defaultValueNumeric) throws ParseException {
 215  1
         if (defaultValueNumeric == null || defaultValueNumeric.equalsIgnoreCase("null")) {
 216  0
             this.defaultValueNumeric = null;
 217  
         } else {
 218  1
             if ("GENERATED_BY_DEFAULT".equals(defaultValueNumeric)) {
 219  0
                 setAutoIncrement(true);
 220  
             } else {
 221  1
                 defaultValueNumeric = defaultValueNumeric.replaceFirst("^\\(", "");
 222  1
                 defaultValueNumeric = defaultValueNumeric.replaceFirst("\\)$", "");
 223  
                 try {
 224  1
                     this.defaultValueNumeric = NumberFormat.getInstance(Locale.US).parse(defaultValueNumeric);
 225  0
                 } catch (ParseException e) {
 226  0
                     this.defaultValueComputed = new DatabaseFunction(defaultValueNumeric);
 227  1
                 }
 228  
             }
 229  
         }
 230  
 
 231  1
         return this;
 232  
     }
 233  
 
 234  
     public Date getDefaultValueDate() {
 235  193
         return defaultValueDate;
 236  
     }
 237  
 
 238  
     public ColumnConfig setDefaultValueDate(String defaultValueDate) {
 239  1
         if (defaultValueDate == null || defaultValueDate.equalsIgnoreCase("null")) {
 240  0
             this.defaultValueDate = null;
 241  
         } else {
 242  
             try {
 243  1
                 this.defaultValueDate = new ISODateFormat().parse(defaultValueDate);
 244  0
             } catch (ParseException e) {
 245  
                 // probably a computed date
 246  0
                 this.defaultValueComputed = new DatabaseFunction(defaultValueDate);
 247  1
             }
 248  
         }
 249  
 
 250  1
         return this;
 251  
     }
 252  
 
 253  
     public ColumnConfig setDefaultValueDate(Date defaultValueDate) {
 254  0
         this.defaultValueDate = defaultValueDate;
 255  
 
 256  0
         return this;
 257  
     }
 258  
 
 259  
     public Boolean getDefaultValueBoolean() {
 260  194
         return defaultValueBoolean;
 261  
     }
 262  
 
 263  
     public ColumnConfig setDefaultValueBoolean(Boolean defaultValueBoolean) {
 264  1
         this.defaultValueBoolean = defaultValueBoolean;
 265  
 
 266  1
         return this;
 267  
     }
 268  
 
 269  
     public DatabaseFunction getDefaultValueComputed() {
 270  190
         return defaultValueComputed;
 271  
     }
 272  
 
 273  
     public ColumnConfig setDefaultValueComputed(DatabaseFunction defaultValueComputed) {
 274  0
         this.defaultValueComputed = defaultValueComputed;
 275  
 
 276  0
         return this;
 277  
     }
 278  
 
 279  
     public Object getDefaultValueObject() {
 280  138
         if (getDefaultValue() != null) {
 281  3
             return getDefaultValue();
 282  135
         } else if (getDefaultValueBoolean() != null) {
 283  1
             return getDefaultValueBoolean();
 284  134
         } else if (getDefaultValueNumeric() != null) {
 285  1
             return getDefaultValueNumeric();
 286  133
         } else if (getDefaultValueDate() != null) {
 287  1
             return getDefaultValueDate();
 288  132
         } else if (getDefaultValueComputed() != null) {
 289  0
             return getDefaultValueComputed();
 290  
         }
 291  132
         return null;
 292  
     }
 293  
 
 294  
     public ConstraintsConfig getConstraints() {
 295  885
         return constraints;
 296  
     }
 297  
 
 298  
     public ColumnConfig setConstraints(ConstraintsConfig constraints) {
 299  49
         this.constraints = constraints;
 300  
 
 301  49
         return this;
 302  
     }
 303  
 
 304  
     public Boolean isAutoIncrement() {
 305  284
         return autoIncrement;
 306  
     }
 307  
 
 308  
     public ColumnConfig setAutoIncrement(Boolean autoIncrement) {
 309  5
         this.autoIncrement = autoIncrement;
 310  
 
 311  5
         return this;
 312  
     }
 313  
 
 314  
     public boolean isPrimaryKey() {
 315  0
         return getConstraints() != null && getConstraints().isPrimaryKey() != null && getConstraints().isPrimaryKey();
 316  
     }
 317  
 
 318  
     public boolean isNullable() {
 319  0
         return getConstraints() == null || getConstraints().isNullable();
 320  
     }
 321  
 
 322  
     public boolean hasDefaultValue() {
 323  45
         return this.getDefaultValue() != null || this.getDefaultValueBoolean() != null
 324  
                 || this.getDefaultValueDate() != null || this.getDefaultValueNumeric() != null
 325  
                 || this.getDefaultValueComputed() != null;
 326  
     }
 327  
 
 328  
     public String getRemarks() {
 329  30
         return remarks;
 330  
     }
 331  
 
 332  
     public ColumnConfig setRemarks(String remarks) {
 333  0
         this.remarks = remarks;
 334  0
         return this;
 335  
     }
 336  
 }