| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
package com.healthmarketscience.jackcess; |
| 29 | |
|
| 30 | |
import java.sql.SQLException; |
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
public class ColumnBuilder { |
| 38 | |
|
| 39 | |
|
| 40 | |
private String _name; |
| 41 | |
|
| 42 | |
private DataType _type; |
| 43 | |
|
| 44 | |
private Integer _length; |
| 45 | |
|
| 46 | |
private Integer _precision; |
| 47 | |
|
| 48 | |
private Integer _scale; |
| 49 | |
|
| 50 | |
private boolean _autoNumber; |
| 51 | |
|
| 52 | |
private Boolean _compressedUnicode; |
| 53 | |
|
| 54 | |
public ColumnBuilder(String name) { |
| 55 | 3 | this(name, null); |
| 56 | 3 | } |
| 57 | |
|
| 58 | 297 | public ColumnBuilder(String name, DataType type) { |
| 59 | 297 | _name = name; |
| 60 | 297 | _type = type; |
| 61 | 297 | } |
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
public ColumnBuilder setType(DataType type) { |
| 67 | 3 | _type = type; |
| 68 | 3 | return this; |
| 69 | |
} |
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
public ColumnBuilder setSQLType(int type) throws SQLException { |
| 75 | 3 | return setSQLType(type, 0); |
| 76 | |
} |
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
public ColumnBuilder setSQLType(int type, int lengthInUnits) |
| 83 | |
throws SQLException |
| 84 | |
{ |
| 85 | 3 | return setType(DataType.fromSQLType(type, lengthInUnits)); |
| 86 | |
} |
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
public ColumnBuilder setPrecision(int newPrecision) { |
| 92 | 2 | _precision = newPrecision; |
| 93 | 2 | return this; |
| 94 | |
} |
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
public ColumnBuilder setScale(int newScale) { |
| 100 | 2 | _scale = newScale; |
| 101 | 2 | return this; |
| 102 | |
} |
| 103 | |
|
| 104 | |
|
| 105 | |
|
| 106 | |
|
| 107 | |
public ColumnBuilder setLength(int length) { |
| 108 | 14 | _length = length; |
| 109 | 14 | return this; |
| 110 | |
} |
| 111 | |
|
| 112 | |
|
| 113 | |
|
| 114 | |
|
| 115 | |
public ColumnBuilder setLengthInUnits(int unitLength) { |
| 116 | 1 | return setLength(_type.getUnitSize() * unitLength); |
| 117 | |
} |
| 118 | |
|
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | |
public ColumnBuilder setMaxLength() { |
| 123 | 0 | return setLength(_type.getMaxSize()); |
| 124 | |
} |
| 125 | |
|
| 126 | |
|
| 127 | |
|
| 128 | |
|
| 129 | |
public ColumnBuilder setAutoNumber(boolean autoNumber) { |
| 130 | 1 | _autoNumber = autoNumber; |
| 131 | 1 | return this; |
| 132 | |
} |
| 133 | |
|
| 134 | |
|
| 135 | |
|
| 136 | |
|
| 137 | |
public ColumnBuilder setCompressedUnicode(boolean compressedUnicode) { |
| 138 | 0 | _compressedUnicode = compressedUnicode; |
| 139 | 0 | return this; |
| 140 | |
} |
| 141 | |
|
| 142 | |
|
| 143 | |
|
| 144 | |
|
| 145 | |
public ColumnBuilder setFromColumn(Column template) { |
| 146 | 0 | DataType type = template.getType(); |
| 147 | 0 | setType(type); |
| 148 | 0 | setLength(template.getLength()); |
| 149 | 0 | setAutoNumber(template.isAutoNumber()); |
| 150 | 0 | if(type.getHasScalePrecision()) { |
| 151 | 0 | setScale(template.getScale()); |
| 152 | 0 | setPrecision(template.getPrecision()); |
| 153 | |
} |
| 154 | |
|
| 155 | 0 | return this; |
| 156 | |
} |
| 157 | |
|
| 158 | |
|
| 159 | |
|
| 160 | |
|
| 161 | |
public Column toColumn() { |
| 162 | 297 | Column col = new Column(); |
| 163 | 297 | col.setName(_name); |
| 164 | 297 | col.setType(_type); |
| 165 | 297 | if(_length != null) { |
| 166 | 14 | col.setLength(_length.shortValue()); |
| 167 | |
} |
| 168 | 297 | if(_precision != null) { |
| 169 | 2 | col.setPrecision(_precision.byteValue()); |
| 170 | |
} |
| 171 | 297 | if(_scale != null) { |
| 172 | 2 | col.setScale(_scale.byteValue()); |
| 173 | |
} |
| 174 | 297 | if(_autoNumber) { |
| 175 | 1 | col.setAutoNumber(true); |
| 176 | |
} |
| 177 | 297 | if(_compressedUnicode != null) { |
| 178 | 0 | col.setCompressedUnicode(_compressedUnicode); |
| 179 | |
} |
| 180 | 297 | return col; |
| 181 | |
} |
| 182 | |
|
| 183 | |
} |