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 2010 Sun Microsystems, Inc.
3  * All rights reserved. Use is subject to license terms.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; version 2 of the License.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 package com.mysql.clusterj.bindings;
20 
21 import java.util.HashMap;
22 import java.util.Map;
23 
24 import com.mysql.cluster.ndbj.NdbColumn;
25 import com.mysql.cluster.ndbj.NdbTable;
26 
27 import com.mysql.clusterj.ClusterJDatastoreException;
28 import com.mysql.clusterj.ClusterJFatalInternalException;
29 
30 import com.mysql.clusterj.core.store.Column;
31 
32 import com.mysql.clusterj.core.util.I18NHelper;
33 import com.mysql.clusterj.core.util.Logger;
34 import com.mysql.clusterj.core.util.LoggerFactoryService;
35 
39 class ColumnImpl implements Column {
40 
42  static final I18NHelper local = I18NHelper
43  .getInstance(ColumnImpl.class);
44 
46  static final Logger logger = LoggerFactoryService.getFactory()
47  .getInstance(ColumnImpl.class);
48 
52  static private Map<String, String> charsetMap = new HashMap<String, String>();
53  static {
54  charsetMap.put("latin1", "windows-1252");
55  charsetMap.put("utf8", "UTF-8");
56  charsetMap.put("sjis", "SJIS");
57  charsetMap.put("big5", "big5");
58  }
59 
61  private NdbColumn column;
62 
64  private String nativeCharsetName;
65 
67  private String charsetName;
68 
70  private Column.Type columnType;
71 
73  private NdbTable table;
74 
75  private int prefixLength;
76 
77  private String tableName;
78 
79  private String columnName;
80 
81  public ColumnImpl(NdbTable table, NdbColumn column) {
82  this.column = column;
83  this.columnName = column.getName();
84  this.table = table;
85  this.tableName = table.getName();
86  this.columnType = convertType(this.column.getType());
87  switch(column.getType()) {
88  case Char:
89  prefixLength = 0;
90  mapCharsetName();
91  break;
92  case Varchar:
93  prefixLength = 1;
94  mapCharsetName();
95  break;
96  case Longvarchar:
97  prefixLength = 2;
98  mapCharsetName();
99  break;
100  case Text:
101  prefixLength = 0;
102  mapCharsetName();
103  break;
104  }
105  }
106 
107  private void mapCharsetName() {
108  this.nativeCharsetName = column.getCharsetName();
109  this.charsetName = charsetMap.get(nativeCharsetName);
110  if (charsetName == null) {
111  throw new ClusterJDatastoreException(
112  local.message("ERR_Unknown_Charset_Name",
113  tableName, columnName, nativeCharsetName));
114  }
115  }
116 
117  public String getName() {
118  return columnName;
119  }
120 
121  public Column.Type getType() {
122  return columnType;
123  }
124 
125  private Type convertType(com.mysql.cluster.ndbj.NdbColumn.Type type) {
126  switch (type) {
127  case Bigint: return Column.Type.Bigint;
128  case Bigunsigned: return Column.Type.Bigunsigned;
129  case Binary: return Column.Type.Binary;
130  case Bit: return Column.Type.Bit;
131  case Blob: return Column.Type.Blob;
132  case Char: return Column.Type.Char;
133  case Date: return Column.Type.Date;
134  case Datetime: return Column.Type.Datetime;
135  case Decimal: return Column.Type.Decimal;
136  case Decimalunsigned: return Column.Type.Decimalunsigned;
137  case Double: return Column.Type.Double;
138  case Float: return Column.Type.Float;
139  case Int: return Column.Type.Int;
140  case Longvarbinary: return Column.Type.Longvarbinary;
141  case Longvarchar: return Column.Type.Longvarchar;
142  case Mediumint: return Column.Type.Mediumint;
143  case Mediumunsigned: return Column.Type.Mediumunsigned;
144  case Olddecimal: return Column.Type.Olddecimal;
145  case Olddecimalunsigned: return Column.Type.Olddecimalunsigned;
146  case Smallint: return Column.Type.Smallint;
147  case Smallunsigned: return Column.Type.Smallunsigned;
148  case Text: return Column.Type.Text;
149  case Time: return Column.Type.Time;
150  case Timestamp: return Column.Type.Timestamp;
151  case Tinyint: return Column.Type.Tinyint;
152  case Tinyunsigned: return Column.Type.Tinyunsigned;
153  case Undefined: return Column.Type.Undefined;
154  case Unsigned: return Column.Type.Unsigned;
155  case Varbinary: return Column.Type.Varbinary;
156  case Varchar: return Column.Type.Varchar;
157  case Year: return Column.Type.Year;
158  default: throw new ClusterJFatalInternalException(
159  local.message("ERR_Unknown_Column_Type",
160  table.getName(), column.getName(), type.toString()));
161  }
162  }
163 
164  public String getCharsetName() {
165  return charsetName;
166  }
167 
168  public int getPrefixLength() {
169  if (prefixLength != 0) {
170  return prefixLength;
171  } else {
172  throw new ClusterJFatalInternalException(local.message(
173  "ERR_Prefix_Length_Not_Defined", tableName, columnName));
174  }
175  }
176 
177  public int getColumnId() {
178  throw new ClusterJFatalInternalException("Not implemented");
179  }
180 
181  public int getColumnSpace() {
182  throw new ClusterJFatalInternalException("Not implemented");
183  }
184 
185  public int getPrecision() {
186  throw new ClusterJFatalInternalException("Not implemented");
187  }
188 
189  public int getScale() {
190  throw new ClusterJFatalInternalException("Not implemented");
191  }
192 
193 }