18 package com.mysql.clusterj.tie;
20 import com.mysql.ndbjtie.mysql.CharsetMap;
21 import com.mysql.ndbjtie.ndbapi.NdbDictionary.ColumnConst;
23 import com.mysql.clusterj.ClusterJDatastoreException;
24 import com.mysql.clusterj.ClusterJFatalInternalException;
26 import com.mysql.clusterj.ColumnType;
28 import com.mysql.clusterj.core.store.Column;
30 import com.mysql.clusterj.core.util.I18NHelper;
31 import com.mysql.clusterj.core.util.Logger;
32 import com.mysql.clusterj.core.util.LoggerFactoryService;
37 class ColumnImpl
implements Column {
40 static final I18NHelper local = I18NHelper
41 .getInstance(ColumnImpl.class);
44 static final Logger logger = LoggerFactoryService.getFactory()
45 .getInstance(ColumnImpl.class);
48 static final CharsetMap charsetMap = Utility.getCharsetMap();
51 private String nativeCharsetName;
54 private String charsetName;
57 private int charsetNumber = 0;
60 private ColumnType columnType;
63 private int prefixLength = -1;
66 private int columnSpace = 0;
78 private boolean primaryKey;
81 private boolean partitionKey;
85 private int inlineSize;
87 private int precision;
93 private boolean nullable;
95 public ColumnImpl(
String tableName, ColumnConst ndbColumn) {
96 this.columnName = ndbColumn.getName();
97 this.columnId = ndbColumn.getColumnNo();
98 this.tableName = tableName;
99 int ndbType = ndbColumn.getType();
100 this.columnType = convertType(ndbType);
101 this.primaryKey = ndbColumn.getPrimaryKey();
102 this.partitionKey = ndbColumn.getPartitionKey();
103 this.nullable = ndbColumn.getNullable();
104 this.length = ndbColumn.getLength();
105 this.inlineSize = ndbColumn.getInlineSize();
106 this.precision = ndbColumn.getPrecision();
107 this.scale = ndbColumn.getScale();
108 this.
size = ndbColumn.getSize();
109 switch(ndbColumn.getType()) {
110 case ColumnConst.Type.Tinyint:
111 case ColumnConst.Type.Tinyunsigned:
112 this.prefixLength = 0;
113 this.columnSpace = 0;
115 case ColumnConst.Type.Smallint:
116 case ColumnConst.Type.Smallunsigned:
117 this.prefixLength = 0;
118 this.columnSpace = 0;
120 case ColumnConst.Type.Mediumint:
121 case ColumnConst.Type.Mediumunsigned:
122 this.prefixLength = 0;
123 this.columnSpace = 0;
125 case ColumnConst.Type.Int:
126 case ColumnConst.Type.Unsigned:
127 this.prefixLength = 0;
128 this.columnSpace = 0;
130 case ColumnConst.Type.Bigint:
131 case ColumnConst.Type.Bigunsigned:
132 this.prefixLength = 0;
133 this.columnSpace = 0;
135 case ColumnConst.Type.Float:
136 this.prefixLength = 0;
137 this.columnSpace = 0;
139 case ColumnConst.Type.Double:
140 this.prefixLength = 0;
141 this.columnSpace = 0;
143 case ColumnConst.Type.Olddecimal:
144 case ColumnConst.Type.Olddecimalunsigned:
145 case ColumnConst.Type.Decimal:
146 case ColumnConst.Type.Decimalunsigned:
147 this.prefixLength = 0;
148 this.columnSpace = alignTo4(Utility.getDecimalColumnSpace(precision, scale));
150 case ColumnConst.Type.Char:
151 this.prefixLength = 0;
152 this.columnSpace = length;
153 this.charsetNumber = ndbColumn.getCharsetNumber();
156 case ColumnConst.Type.Varchar:
158 this.columnSpace = alignTo4(length + prefixLength);
159 this.charsetNumber = ndbColumn.getCharsetNumber();
162 case ColumnConst.Type.Binary:
163 this.prefixLength = 0;
164 this.columnSpace = length;
166 case ColumnConst.Type.Varbinary:
167 this.prefixLength = 1;
168 this.columnSpace = alignTo4(length + prefixLength);
170 case ColumnConst.Type.Datetime:
171 this.prefixLength = 0;
172 this.columnSpace = 0;
174 case ColumnConst.Type.Date:
175 this.prefixLength = 0;
176 this.columnSpace = 0;
178 case ColumnConst.Type.Blob:
179 this.prefixLength = 0;
180 this.columnSpace = inlineSize;
182 case ColumnConst.Type.Text:
183 this.prefixLength = 0;
184 this.columnSpace = inlineSize;
185 this.charsetNumber = ndbColumn.getCharsetNumber();
188 case ColumnConst.Type.Bit:
189 this.prefixLength = 0;
190 this.columnSpace = 0;
192 case ColumnConst.Type.Longvarchar:
193 this.prefixLength = 2;
194 this.columnSpace = alignTo4(length + prefixLength);
195 this.charsetNumber = ndbColumn.getCharsetNumber();
198 case ColumnConst.Type.Longvarbinary:
199 this.prefixLength = 2;
200 this.columnSpace = alignTo4(length + prefixLength);
202 case ColumnConst.Type.Time:
203 this.prefixLength = 0;
204 this.columnSpace = 0;
206 case ColumnConst.Type.Year:
207 this.prefixLength = 0;
208 this.columnSpace = 4;
210 case ColumnConst.Type.Timestamp:
211 this.prefixLength = 0;
212 this.columnSpace = 4;
214 default:
throw new ClusterJFatalInternalException(
215 local.message(
"ERR_Unknown_Column_Type",
216 tableName, ndbColumn.getName(), ndbType));
218 if (logger.isDetailEnabled()) logger.detail(
"Column " + columnName
219 +
" columnSpace: " + columnSpace +
" prefixLength: " + prefixLength
220 +
" inlineSize: " + inlineSize +
" length: " + length +
" size: " +
size
221 +
" charsetNumber: " + charsetNumber +
" charsetName: " + charsetName
222 +
" nativeCharsetNumber: " + nativeCharsetName);
225 private int alignTo4(
int size) {
226 int extra = 4 - ((size % 4) % 4);
227 int result = size + extra;
231 private void mapCharsetName() {
232 this.nativeCharsetName = charsetMap.getName(charsetNumber);
233 this.charsetName = charsetMap.getMysqlName(charsetNumber);
234 if (charsetName == null) {
235 throw new ClusterJDatastoreException(
236 local.message(
"ERR_Unknown_Charset_Name",
237 tableName, columnName, nativeCharsetName));
241 public ColumnType getType() {
245 private ColumnType convertType(
int type) {
247 case ColumnConst.Type.Bigint:
return ColumnType.Bigint;
248 case ColumnConst.Type.Bigunsigned:
return ColumnType.Bigunsigned;
249 case ColumnConst.Type.Binary:
return ColumnType.Binary;
250 case ColumnConst.Type.Bit:
return ColumnType.Bit;
251 case ColumnConst.Type.Blob:
return ColumnType.Blob;
252 case ColumnConst.Type.Char:
return ColumnType.Char;
253 case ColumnConst.Type.Date:
return ColumnType.Date;
254 case ColumnConst.Type.Datetime:
return ColumnType.Datetime;
255 case ColumnConst.Type.Decimal:
return ColumnType.Decimal;
256 case ColumnConst.Type.Decimalunsigned:
return ColumnType.Decimalunsigned;
257 case ColumnConst.Type.Double:
return ColumnType.Double;
258 case ColumnConst.Type.Float:
return ColumnType.Float;
259 case ColumnConst.Type.Int:
return ColumnType.Int;
260 case ColumnConst.Type.Longvarbinary:
return ColumnType.Longvarbinary;
261 case ColumnConst.Type.Longvarchar:
return ColumnType.Longvarchar;
262 case ColumnConst.Type.Mediumint:
return ColumnType.Mediumint;
263 case ColumnConst.Type.Mediumunsigned:
return ColumnType.Mediumunsigned;
264 case ColumnConst.Type.Olddecimal:
return ColumnType.Olddecimal;
265 case ColumnConst.Type.Olddecimalunsigned:
return ColumnType.Olddecimalunsigned;
266 case ColumnConst.Type.Smallint:
return ColumnType.Smallint;
267 case ColumnConst.Type.Smallunsigned:
return ColumnType.Smallunsigned;
268 case ColumnConst.Type.Text:
return ColumnType.Text;
269 case ColumnConst.Type.Time:
return ColumnType.Time;
270 case ColumnConst.Type.Timestamp:
return ColumnType.Timestamp;
271 case ColumnConst.Type.Tinyint:
return ColumnType.Tinyint;
272 case ColumnConst.Type.Tinyunsigned:
return ColumnType.Tinyunsigned;
273 case ColumnConst.Type.Undefined:
return ColumnType.Undefined;
274 case ColumnConst.Type.Unsigned:
return ColumnType.Unsigned;
275 case ColumnConst.Type.Varbinary:
return ColumnType.Varbinary;
276 case ColumnConst.Type.Varchar:
return ColumnType.Varchar;
277 case ColumnConst.Type.Year:
return ColumnType.Year;
278 default:
throw new ClusterJFatalInternalException(
279 local.message(
"ERR_Unknown_Column_Type",
280 tableName, columnName, type));
284 public String getCharsetName() {
292 public boolean isPrimaryKey() {
296 public boolean isPartitionKey() {
300 public int getLength() {
304 public int getPrefixLength() {
305 if (prefixLength != -1) {
308 throw new ClusterJFatalInternalException(local.message(
309 "ERR_Prefix_Length_Not_Defined", tableName, columnName));
313 public int getColumnId() {
317 public int getColumnSpace() {
321 public int getPrecision() {
325 public int getScale() {
329 public int getCharsetNumber() {
330 return charsetNumber;
333 public String decode(byte[] array) {
334 return Utility.decode(array, charsetNumber);
337 public byte[] encode(
String string) {
338 return Utility.encode(
string, charsetNumber);
342 public String toString() {
346 public boolean getNullable() {