MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ResultDataImpl.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 com.mysql.cluster.ndbj.NdbApiException;
22 import com.mysql.cluster.ndbj.NdbResultSet;
23 import com.mysql.clusterj.ClusterJDatastoreException;
24 import com.mysql.clusterj.core.store.Blob;
25 import com.mysql.clusterj.core.store.Column;
26 import com.mysql.clusterj.core.store.ResultData;
27 import com.mysql.clusterj.core.util.I18NHelper;
28 import com.mysql.clusterj.core.util.Logger;
29 import com.mysql.clusterj.core.util.LoggerFactoryService;
30 import java.math.BigDecimal;
31 import java.sql.Date;
32 import java.sql.SQLException;
33 import java.sql.Time;
34 import java.sql.Timestamp;
35 
39 class ResultDataImpl implements ResultData {
40 
42  static final I18NHelper local = I18NHelper
43  .getInstance(ClusterTransactionImpl.class);
44 
46  static final Logger logger = LoggerFactoryService.getFactory()
47  .getInstance(ClusterTransactionImpl.class);
48 
49  private NdbResultSet resultData;
50 
51  public ResultDataImpl(NdbResultSet resultData) {
52  this.resultData = resultData;
53  }
54 
55  public Blob getBlob(Column storeColumn) {
56  try {
57  return new BlobImpl(resultData.getBlob(storeColumn.getName()));
58  } catch (NdbApiException ndbApiException) {
59  throw new ClusterJDatastoreException(local.message("ERR_Datastore"),
60  ndbApiException);
61  }
62  }
63 
64  public Date getDate(Column storeColumn) {
65  try {
66  Date result = resultData.getDate(storeColumn.getName());
67  if ((result != null) && wasNull(storeColumn.getName())) {
68  logger.info("Column was null but non-null was returned.");
69  return null;
70  }
71  return result;
72  } catch (NdbApiException ndbApiException) {
73  throw new ClusterJDatastoreException(local.message("ERR_Datastore"),
74  ndbApiException);
75  } catch (SQLException sqlException) {
76  throw new ClusterJDatastoreException(local.message("ERR_Datastore"),
77  sqlException);
78  }
79  }
80 
81  public BigDecimal getDecimal(Column storeColumn) {
82  try {
83  BigDecimal result = resultData.getDecimal(storeColumn.getName());
84  if ((result != null) && wasNull(storeColumn.getName())) {
85  logger.info("Column was null but non-null was returned.");
86  return null;
87  }
88  return result;
89  } catch (NdbApiException ndbApiException) {
90  throw new ClusterJDatastoreException(local.message("ERR_Datastore"),
91  ndbApiException);
92  }
93  }
94 
95  public boolean getBoolean(Column storeColumn) {
96  throw new UnsupportedOperationException(local.message("ERR_NotImplemented"));
97  }
98 
99  public boolean[] getBooleans(Column storeColumn) {
100  throw new UnsupportedOperationException(local.message("ERR_NotImplemented"));
101  }
102 
103  public byte getByte(Column storeColumn) {
104  // In the ndb-bindings there is no getByte API, so get the result as an int
105  try {
106  return (byte)resultData.getInt(storeColumn.getName());
107  } catch (NdbApiException ndbApiException) {
108  throw new ClusterJDatastoreException(local.message("ERR_Datastore"),
109  ndbApiException);
110  }
111  }
112 
113  public double getDouble(Column storeColumn) {
114  try {
115  return resultData.getDouble(storeColumn.getName());
116  } catch (NdbApiException ndbApiException) {
117  throw new ClusterJDatastoreException(local.message("ERR_Datastore"),
118  ndbApiException);
119  }
120  }
121 
122  public float getFloat(Column storeColumn) {
123  try {
124  return resultData.getFloat(storeColumn.getName());
125  } catch (NdbApiException ndbApiException) {
126  throw new ClusterJDatastoreException(local.message("ERR_Datastore"),
127  ndbApiException);
128  }
129  }
130 
131  public int getInt(Column storeColumn) {
132  try {
133  return resultData.getInt(storeColumn.getName());
134  } catch (NdbApiException ndbApiException) {
135  throw new ClusterJDatastoreException(local.message("ERR_Datastore"),
136  ndbApiException);
137  }
138  }
139 
140  public long getLong(Column storeColumn) {
141  try {
142  return resultData.getLong(storeColumn.getName());
143  } catch (NdbApiException ndbApiException) {
144  throw new ClusterJDatastoreException(local.message("ERR_Datastore"),
145  ndbApiException);
146  }
147  }
148 
149  public short getShort(Column storeColumn) {
150  try {
151  return resultData.getShort(storeColumn.getName());
152  } catch (NdbApiException ndbApiException) {
153  throw new ClusterJDatastoreException(local.message("ERR_Datastore"),
154  ndbApiException);
155  }
156  }
157 
158  public String getString(Column storeColumn) {
159  try {
160  String result = resultData.getString(storeColumn.getName());
161  if (wasNull(storeColumn.getName())) {
162  if (result != null) {
163  logger.info("Column " + storeColumn.getName() + " was null but non-null was returned.");
164  }
165  return null;
166  }
167  return result;
168  } catch (NdbApiException ndbApiException) {
169  throw new ClusterJDatastoreException(local.message("ERR_Datastore"),
170  ndbApiException);
171  }
172  }
173 
174  public Time getTime(Column storeColumn) {
175  try {
176  Time result = resultData.getTime(storeColumn.getName());
177  if ((result != null) && wasNull(storeColumn.getName())) {
178  logger.info("Column was null but non-null was returned.");
179  return null;
180  }
181  return result;
182  } catch (NdbApiException ndbApiException) {
183  throw new ClusterJDatastoreException(local.message("ERR_Datastore"),
184  ndbApiException);
185  } catch (SQLException sqlException) {
186  throw new ClusterJDatastoreException(local.message("ERR_Datastore"),
187  sqlException);
188  }
189  }
190 
191  public Timestamp getTimestamp(Column storeColumn) {
192  try {
193  Timestamp result = resultData.getTimestamp(storeColumn.getName());
194  if ((result != null) && wasNull(storeColumn.getName())) {
195  logger.info("Column was null but non-null was returned.");
196  return null;
197  }
198  return result;
199  } catch (NdbApiException ndbApiException) {
200  throw new ClusterJDatastoreException(local.message("ERR_Datastore"),
201  ndbApiException);
202  }
203  }
204 
205  public Timestamp getDatetime(Column storeColumn) {
206  return getTimestamp(storeColumn);
207  }
208 
209  public boolean next() {
210  try {
211  return resultData.next();
212  } catch (NdbApiException ndbApiException) {
213  throw new ClusterJDatastoreException(local.message("ERR_Datastore"),
214  ndbApiException);
215  }
216  }
217 
218  public byte[] getBytes(Column storeColumn) {
219  if (logger.isDetailEnabled()) logger.detail("Column name: " + storeColumn.getName());
220  try {
221  byte[] result = resultData.getBytes(storeColumn.getName());
222  if ((result != null) && wasNull(storeColumn.getName())) {
223  logger.info("Column was null but non-null was returned.");
224  return null;
225  }
226  return result;
227  } catch (NdbApiException ndbApiException) {
228  throw new ClusterJDatastoreException(local.message("ERR_Datastore"),
229  ndbApiException);
230  }
231  }
232 
233  public byte[] getStringBytes(Column storeColumn) {
234  if (logger.isDetailEnabled()) logger.detail("Column name: " + storeColumn.getName());
235  try {
236  byte[] result = resultData.getStringBytes(storeColumn.getName());
237  if ((result != null) && wasNull(storeColumn.getName())) {
238  logger.info("Column was null but non-null was returned.");
239  return null;
240  }
241  return result;
242  } catch (NdbApiException ndbApiException) {
243  throw new ClusterJDatastoreException(local.message("ERR_Datastore"),
244  ndbApiException);
245  }
246  }
247 
248 
249  public Object getObject(Column storeColumn) {
250  try {
251  return resultData.getObject(storeColumn.getName());
252  } catch (NdbApiException ndbApiException) {
253  throw new ClusterJDatastoreException(local.message("ERR_Datastore"),
254  ndbApiException);
255  } catch (SQLException sqlException) {
256  throw new ClusterJDatastoreException(local.message("ERR_Datastore"),
257  sqlException);
258  }
259  }
260 
261  public boolean wasNull(String columnName) {
262  try {
263  return resultData.wasNull();
264  } catch (NdbApiException ndbApiException) {
265  throw new ClusterJDatastoreException(local.message("ERR_Datastore"),
266  ndbApiException);
267  }
268  }
269 
270  public Boolean getObjectBoolean(Column storeColumn) {
271  throw new UnsupportedOperationException(local.message("ERR_NotImplemented"));
272  }
273 
274  public Byte getObjectByte(Column storeColumn) {
275  Byte result = getByte(storeColumn);
276  return (wasNull(storeColumn.getName())?null:result);
277  }
278 
279  public Double getObjectDouble(Column storeColumn) {
280  Double result = getDouble(storeColumn);
281  return (wasNull(storeColumn.getName())?null:result);
282  }
283 
284  public Float getObjectFloat(Column storeColumn) {
285  Float result = getFloat(storeColumn);
286  return (wasNull(storeColumn.getName())?null:result);
287  }
288 
289  public Integer getObjectInteger(Column storeColumn) {
290  Integer result = getInt(storeColumn);
291  return (wasNull(storeColumn.getName())?null:result);
292  }
293 
294  public Long getObjectLong(Column storeColumn) {
295  Long result = getLong(storeColumn);
296  return (wasNull(storeColumn.getName())?null:result);
297  }
298 
299  public Short getObjectShort(Column storeColumn) {
300  Short result = getShort(storeColumn);
301  return (wasNull(storeColumn.getName())?null:result);
302  }
303 
304 }