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 this(name, null);
56 }
57
58 public ColumnBuilder(String name, DataType type) {
59 _name = name;
60 _type = type;
61 }
62
63
64
65
66 public ColumnBuilder setType(DataType type) {
67 _type = type;
68 return this;
69 }
70
71
72
73
74 public ColumnBuilder setSQLType(int type) throws SQLException {
75 return setSQLType(type, 0);
76 }
77
78
79
80
81
82 public ColumnBuilder setSQLType(int type, int lengthInUnits)
83 throws SQLException
84 {
85 return setType(DataType.fromSQLType(type, lengthInUnits));
86 }
87
88
89
90
91 public ColumnBuilder setPrecision(int newPrecision) {
92 _precision = newPrecision;
93 return this;
94 }
95
96
97
98
99 public ColumnBuilder setScale(int newScale) {
100 _scale = newScale;
101 return this;
102 }
103
104
105
106
107 public ColumnBuilder setLength(int length) {
108 _length = length;
109 return this;
110 }
111
112
113
114
115 public ColumnBuilder setLengthInUnits(int unitLength) {
116 return setLength(_type.getUnitSize() * unitLength);
117 }
118
119
120
121
122 public ColumnBuilder setMaxLength() {
123 return setLength(_type.getMaxSize());
124 }
125
126
127
128
129 public ColumnBuilder setAutoNumber(boolean autoNumber) {
130 _autoNumber = autoNumber;
131 return this;
132 }
133
134
135
136
137 public ColumnBuilder setCompressedUnicode(boolean compressedUnicode) {
138 _compressedUnicode = compressedUnicode;
139 return this;
140 }
141
142
143
144
145 public ColumnBuilder setFromColumn(Column template) {
146 DataType type = template.getType();
147 setType(type);
148 setLength(template.getLength());
149 setAutoNumber(template.isAutoNumber());
150 if(type.getHasScalePrecision()) {
151 setScale(template.getScale());
152 setPrecision(template.getPrecision());
153 }
154
155 return this;
156 }
157
158
159
160
161 public Column toColumn() {
162 Column col = new Column();
163 col.setName(_name);
164 col.setType(_type);
165 if(_length != null) {
166 col.setLength(_length.shortValue());
167 }
168 if(_precision != null) {
169 col.setPrecision(_precision.byteValue());
170 }
171 if(_scale != null) {
172 col.setScale(_scale.byteValue());
173 }
174 if(_autoNumber) {
175 col.setAutoNumber(true);
176 }
177 if(_compressedUnicode != null) {
178 col.setCompressedUnicode(_compressedUnicode);
179 }
180 return col;
181 }
182
183 }