MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ColumnImpl.java
1 /*
2  * Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16  */
17 
18 package com.mysql.clusterj.tie;
19 
20 import com.mysql.ndbjtie.mysql.CharsetMap;
21 import com.mysql.ndbjtie.ndbapi.NdbDictionary.ColumnConst;
22 
23 import com.mysql.clusterj.ClusterJDatastoreException;
24 import com.mysql.clusterj.ClusterJFatalInternalException;
25 
26 import com.mysql.clusterj.ColumnType;
27 
28 import com.mysql.clusterj.core.store.Column;
29 
30 import com.mysql.clusterj.core.util.I18NHelper;
31 import com.mysql.clusterj.core.util.Logger;
32 import com.mysql.clusterj.core.util.LoggerFactoryService;
33 
37 class ColumnImpl implements Column {
38 
40  static final I18NHelper local = I18NHelper
41  .getInstance(ColumnImpl.class);
42 
44  static final Logger logger = LoggerFactoryService.getFactory()
45  .getInstance(ColumnImpl.class);
46 
48  static final CharsetMap charsetMap = Utility.getCharsetMap();
49 
51  private String nativeCharsetName;
52 
54  private String charsetName;
55 
57  private int charsetNumber = 0;
58 
60  private ColumnType columnType;
61 
63  private int prefixLength = -1;
64 
66  private int columnSpace = 0;
67 
69  private String columnName;
70 
72  private String tableName;
73 
75  private int columnId;
76 
78  private boolean primaryKey;
79 
81  private boolean partitionKey;
82 
83  private int length;
84 
85  private int inlineSize;
86 
87  private int precision;
88 
89  private int scale;
90 
91  private int size;
92 
93  private boolean nullable;
94 
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;
114  break;
115  case ColumnConst.Type.Smallint:
116  case ColumnConst.Type.Smallunsigned:
117  this.prefixLength = 0;
118  this.columnSpace = 0;
119  break;
120  case ColumnConst.Type.Mediumint:
121  case ColumnConst.Type.Mediumunsigned:
122  this.prefixLength = 0;
123  this.columnSpace = 0;
124  break;
125  case ColumnConst.Type.Int:
126  case ColumnConst.Type.Unsigned:
127  this.prefixLength = 0;
128  this.columnSpace = 0;
129  break;
130  case ColumnConst.Type.Bigint:
131  case ColumnConst.Type.Bigunsigned:
132  this.prefixLength = 0;
133  this.columnSpace = 0;
134  break;
135  case ColumnConst.Type.Float:
136  this.prefixLength = 0;
137  this.columnSpace = 0;
138  break;
139  case ColumnConst.Type.Double:
140  this.prefixLength = 0;
141  this.columnSpace = 0;
142  break;
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));
149  break;
150  case ColumnConst.Type.Char:
151  this.prefixLength = 0;
152  this.columnSpace = length;
153  this.charsetNumber = ndbColumn.getCharsetNumber();
154  mapCharsetName();
155  break;
156  case ColumnConst.Type.Varchar:
157  prefixLength = 1;
158  this.columnSpace = alignTo4(length + prefixLength);
159  this.charsetNumber = ndbColumn.getCharsetNumber();
160  mapCharsetName();
161  break;
162  case ColumnConst.Type.Binary:
163  this.prefixLength = 0;
164  this.columnSpace = length;
165  break;
166  case ColumnConst.Type.Varbinary:
167  this.prefixLength = 1;
168  this.columnSpace = alignTo4(length + prefixLength);
169  break;
170  case ColumnConst.Type.Datetime:
171  this.prefixLength = 0;
172  this.columnSpace = 0;
173  break;
174  case ColumnConst.Type.Date:
175  this.prefixLength = 0;
176  this.columnSpace = 0;
177  break;
178  case ColumnConst.Type.Blob:
179  this.prefixLength = 0;
180  this.columnSpace = inlineSize;
181  break;
182  case ColumnConst.Type.Text:
183  this.prefixLength = 0;
184  this.columnSpace = inlineSize;
185  this.charsetNumber = ndbColumn.getCharsetNumber();
186  mapCharsetName();
187  break;
188  case ColumnConst.Type.Bit:
189  this.prefixLength = 0;
190  this.columnSpace = 0;
191  break;
192  case ColumnConst.Type.Longvarchar:
193  this.prefixLength = 2;
194  this.columnSpace = alignTo4(length + prefixLength);
195  this.charsetNumber = ndbColumn.getCharsetNumber();
196  mapCharsetName();
197  break;
198  case ColumnConst.Type.Longvarbinary:
199  this.prefixLength = 2;
200  this.columnSpace = alignTo4(length + prefixLength);
201  break;
202  case ColumnConst.Type.Time:
203  this.prefixLength = 0;
204  this.columnSpace = 0;
205  break;
206  case ColumnConst.Type.Year:
207  this.prefixLength = 0;
208  this.columnSpace = 4;
209  break;
210  case ColumnConst.Type.Timestamp:
211  this.prefixLength = 0;
212  this.columnSpace = 4;
213  break;
214  default: throw new ClusterJFatalInternalException(
215  local.message("ERR_Unknown_Column_Type",
216  tableName, ndbColumn.getName(), ndbType));
217  }
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);
223  }
224 
225  private int alignTo4(int size) {
226  int extra = 4 - ((size % 4) % 4);
227  int result = size + extra;
228  return result;
229  }
230 
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));
238  }
239  }
240 
241  public ColumnType getType() {
242  return columnType;
243  }
244 
245  private ColumnType convertType(int type) {
246  switch (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));
281  }
282  }
283 
284  public String getCharsetName() {
285  return charsetName;
286  }
287 
288  public String getName() {
289  return columnName;
290  }
291 
292  public boolean isPrimaryKey() {
293  return primaryKey;
294  }
295 
296  public boolean isPartitionKey() {
297  return partitionKey;
298  }
299 
300  public int getLength() {
301  return length;
302  }
303 
304  public int getPrefixLength() {
305  if (prefixLength != -1) {
306  return prefixLength;
307  } else {
308  throw new ClusterJFatalInternalException(local.message(
309  "ERR_Prefix_Length_Not_Defined", tableName, columnName));
310  }
311  }
312 
313  public int getColumnId() {
314  return columnId;
315  }
316 
317  public int getColumnSpace() {
318  return columnSpace;
319  }
320 
321  public int getPrecision() {
322  return precision;
323  }
324 
325  public int getScale() {
326  return scale;
327  }
328 
329  public int getCharsetNumber() {
330  return charsetNumber;
331  }
332 
333  public String decode(byte[] array) {
334  return Utility.decode(array, charsetNumber);
335  }
336 
337  public byte[] encode(String string) {
338  return Utility.encode(string, charsetNumber);
339  }
340 
341  @Override
342  public String toString() {
343  return columnName;
344  }
345 
346  public boolean getNullable() {
347  return nullable;
348  }
349 
350 }