1 /*
2  *  Copyright (c) 2010, 2021, Oracle and/or its affiliates.
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, version 2.0,
7  *  as published by the Free Software Foundation.
8  *
9  *  This program is also distributed with certain software (including
10  *  but not limited to OpenSSL) that is licensed under separate terms,
11  *  as designated in a particular file or component or in included license
12  *  documentation.  The authors of MySQL hereby grant you an additional
13  *  permission to link the program and your derivative works with the
14  *  separately licensed software that they have included with MySQL.
15  *
16  *  This program is distributed in the hope that it will be useful,
17  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  *  GNU General Public License, version 2.0, for more details.
20  *
21  *  You should have received a copy of the GNU General Public License
22  *  along with this program; if not, write to the Free Software
23  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
24  */
25 
26 package com.mysql.clusterj.bindings;
27 
28 import com.mysql.cluster.ndbj.NdbApiException;
29 import com.mysql.cluster.ndbj.NdbResultSet;
30 import com.mysql.clusterj.ClusterJDatastoreException;
31 import com.mysql.clusterj.core.store.Blob;
32 import com.mysql.clusterj.core.store.Column;
33 import com.mysql.clusterj.core.store.ResultData;
34 import com.mysql.clusterj.core.util.I18NHelper;
35 import com.mysql.clusterj.core.util.Logger;
36 import com.mysql.clusterj.core.util.LoggerFactoryService;
37 import java.math.BigDecimal;
38 import java.sql.Date;
39 import java.sql.SQLException;
40 import java.sql.Time;
41 import java.sql.Timestamp;
42 
43 /**
44  *
45  */
46 class ResultDataImpl implements ResultData {
47 
48     /** My message translator */
49     static final I18NHelper local = I18NHelper
50             .getInstance(ClusterTransactionImpl.class);
51 
52     /** My logger */
53     static final Logger logger = LoggerFactoryService.getFactory()
54             .getInstance(ClusterTransactionImpl.class);
55 
56     private NdbResultSet resultData;
57 
ResultDataImpl(NdbResultSet resultData)58     public ResultDataImpl(NdbResultSet resultData) {
59         this.resultData = resultData;
60     }
61 
getBlob(Column storeColumn)62     public Blob getBlob(Column storeColumn) {
63         try {
64             return new BlobImpl(resultData.getBlob(storeColumn.getName()));
65         } catch (NdbApiException ndbApiException) {
66             throw new ClusterJDatastoreException(local.message("ERR_Datastore"),
67                     ndbApiException);
68         }
69     }
70 
getDate(Column storeColumn)71     public Date getDate(Column storeColumn) {
72         try {
73             Date result = resultData.getDate(storeColumn.getName());
74             if ((result != null) && wasNull(storeColumn.getName())) {
75         	logger.info("Column was null but non-null was returned.");
76         	return null;
77             }
78             return result;
79         } catch (NdbApiException ndbApiException) {
80             throw new ClusterJDatastoreException(local.message("ERR_Datastore"),
81                     ndbApiException);
82         } catch (SQLException sqlException) {
83             throw new ClusterJDatastoreException(local.message("ERR_Datastore"),
84                     sqlException);
85         }
86     }
87 
getDecimal(Column storeColumn)88     public BigDecimal getDecimal(Column storeColumn) {
89         try {
90             BigDecimal result = resultData.getDecimal(storeColumn.getName());
91             if ((result != null) && wasNull(storeColumn.getName())) {
92         	logger.info("Column was null but non-null was returned.");
93         	return null;
94             }
95             return result;
96         } catch (NdbApiException ndbApiException) {
97             throw new ClusterJDatastoreException(local.message("ERR_Datastore"),
98                     ndbApiException);
99         }
100     }
101 
getBoolean(Column storeColumn)102     public boolean getBoolean(Column storeColumn) {
103         throw new UnsupportedOperationException(local.message("ERR_NotImplemented"));
104     }
105 
getBooleans(Column storeColumn)106     public boolean[] getBooleans(Column storeColumn) {
107         throw new UnsupportedOperationException(local.message("ERR_NotImplemented"));
108     }
109 
getByte(Column storeColumn)110     public byte getByte(Column storeColumn) {
111         // In the ndb-bindings there is no getByte API, so get the result as an int
112         try {
113             return (byte)resultData.getInt(storeColumn.getName());
114         } catch (NdbApiException ndbApiException) {
115             throw new ClusterJDatastoreException(local.message("ERR_Datastore"),
116                     ndbApiException);
117         }
118     }
119 
getDouble(Column storeColumn)120     public double getDouble(Column storeColumn) {
121         try {
122             return resultData.getDouble(storeColumn.getName());
123         } catch (NdbApiException ndbApiException) {
124             throw new ClusterJDatastoreException(local.message("ERR_Datastore"),
125                     ndbApiException);
126         }
127     }
128 
getFloat(Column storeColumn)129     public float getFloat(Column storeColumn) {
130         try {
131             return resultData.getFloat(storeColumn.getName());
132         } catch (NdbApiException ndbApiException) {
133             throw new ClusterJDatastoreException(local.message("ERR_Datastore"),
134                     ndbApiException);
135         }
136     }
137 
getInt(Column storeColumn)138     public int getInt(Column storeColumn) {
139         try {
140             return resultData.getInt(storeColumn.getName());
141         } catch (NdbApiException ndbApiException) {
142             throw new ClusterJDatastoreException(local.message("ERR_Datastore"),
143                     ndbApiException);
144         }
145     }
146 
getLong(Column storeColumn)147     public long getLong(Column storeColumn) {
148         try {
149             return resultData.getLong(storeColumn.getName());
150         } catch (NdbApiException ndbApiException) {
151             throw new ClusterJDatastoreException(local.message("ERR_Datastore"),
152                     ndbApiException);
153         }
154     }
155 
getShort(Column storeColumn)156     public short getShort(Column storeColumn) {
157         try {
158             return resultData.getShort(storeColumn.getName());
159         } catch (NdbApiException ndbApiException) {
160             throw new ClusterJDatastoreException(local.message("ERR_Datastore"),
161                     ndbApiException);
162         }
163     }
164 
getString(Column storeColumn)165     public String getString(Column storeColumn) {
166         try {
167             String result = resultData.getString(storeColumn.getName());
168             if (wasNull(storeColumn.getName())) {
169                 if (result != null) {
170                     logger.info("Column " + storeColumn.getName() + " was null but non-null was returned.");
171                 }
172                 return null;
173             }
174             return result;
175         } catch (NdbApiException ndbApiException) {
176             throw new ClusterJDatastoreException(local.message("ERR_Datastore"),
177                     ndbApiException);
178         }
179     }
180 
getTime(Column storeColumn)181     public Time getTime(Column storeColumn) {
182         try {
183             Time result = resultData.getTime(storeColumn.getName());
184             if ((result != null) && wasNull(storeColumn.getName())) {
185         	logger.info("Column was null but non-null was returned.");
186         	return null;
187             }
188             return result;
189         } catch (NdbApiException ndbApiException) {
190             throw new ClusterJDatastoreException(local.message("ERR_Datastore"),
191                     ndbApiException);
192         } catch (SQLException sqlException) {
193             throw new ClusterJDatastoreException(local.message("ERR_Datastore"),
194                     sqlException);
195         }
196     }
197 
getTimestamp(Column storeColumn)198     public Timestamp getTimestamp(Column storeColumn) {
199         try {
200             Timestamp result = resultData.getTimestamp(storeColumn.getName());
201             if ((result != null) && wasNull(storeColumn.getName())) {
202         	logger.info("Column was null but non-null was returned.");
203         	return null;
204             }
205             return result;
206         } catch (NdbApiException ndbApiException) {
207             throw new ClusterJDatastoreException(local.message("ERR_Datastore"),
208                     ndbApiException);
209         }
210     }
211 
getDatetime(Column storeColumn)212     public Timestamp getDatetime(Column storeColumn) {
213         return getTimestamp(storeColumn);
214     }
215 
next()216     public boolean next() {
217         try {
218             return resultData.next();
219         } catch (NdbApiException ndbApiException) {
220             throw new ClusterJDatastoreException(local.message("ERR_Datastore"),
221                     ndbApiException);
222         }
223     }
224 
getBytes(Column storeColumn)225     public byte[] getBytes(Column storeColumn) {
226         if (logger.isDetailEnabled()) logger.detail("Column name: " + storeColumn.getName());
227         try {
228             byte[] result = resultData.getBytes(storeColumn.getName());
229             if ((result != null) && wasNull(storeColumn.getName())) {
230                 logger.info("Column was null but non-null was returned.");
231                 return null;
232             }
233             return result;
234         } catch (NdbApiException ndbApiException) {
235             throw new ClusterJDatastoreException(local.message("ERR_Datastore"),
236                     ndbApiException);
237         }
238     }
239 
getStringBytes(Column storeColumn)240     public byte[] getStringBytes(Column storeColumn) {
241         if (logger.isDetailEnabled()) logger.detail("Column name: " + storeColumn.getName());
242         try {
243             byte[] result = resultData.getStringBytes(storeColumn.getName());
244             if ((result != null) && wasNull(storeColumn.getName())) {
245                 logger.info("Column was null but non-null was returned.");
246                 return null;
247             }
248             return result;
249         } catch (NdbApiException ndbApiException) {
250             throw new ClusterJDatastoreException(local.message("ERR_Datastore"),
251                     ndbApiException);
252         }
253     }
254 
255 
getObject(Column storeColumn)256     public Object getObject(Column storeColumn) {
257         try {
258             return resultData.getObject(storeColumn.getName());
259         } catch (NdbApiException ndbApiException) {
260             throw new ClusterJDatastoreException(local.message("ERR_Datastore"),
261                     ndbApiException);
262         } catch (SQLException sqlException) {
263             throw new ClusterJDatastoreException(local.message("ERR_Datastore"),
264                     sqlException);
265         }
266     }
267 
wasNull(String columnName)268     public boolean wasNull(String columnName) {
269 	try {
270 	    return resultData.wasNull();
271 	} catch (NdbApiException ndbApiException) {
272             throw new ClusterJDatastoreException(local.message("ERR_Datastore"),
273                     ndbApiException);
274 	}
275     }
276 
getObjectBoolean(Column storeColumn)277     public Boolean getObjectBoolean(Column storeColumn) {
278         throw new UnsupportedOperationException(local.message("ERR_NotImplemented"));
279     }
280 
getObjectByte(Column storeColumn)281     public Byte getObjectByte(Column storeColumn) {
282         Byte result = getByte(storeColumn);
283         return (wasNull(storeColumn.getName())?null:result);
284     }
285 
getObjectDouble(Column storeColumn)286     public Double getObjectDouble(Column storeColumn) {
287         Double result = getDouble(storeColumn);
288         return (wasNull(storeColumn.getName())?null:result);
289     }
290 
getObjectFloat(Column storeColumn)291     public Float getObjectFloat(Column storeColumn) {
292         Float result = getFloat(storeColumn);
293         return (wasNull(storeColumn.getName())?null:result);
294     }
295 
getObjectInteger(Column storeColumn)296     public Integer getObjectInteger(Column storeColumn) {
297         Integer result = getInt(storeColumn);
298         return (wasNull(storeColumn.getName())?null:result);
299     }
300 
getObjectLong(Column storeColumn)301     public Long getObjectLong(Column storeColumn) {
302         Long result = getLong(storeColumn);
303         return (wasNull(storeColumn.getName())?null:result);
304     }
305 
getObjectShort(Column storeColumn)306     public Short getObjectShort(Column storeColumn) {
307         Short result = getShort(storeColumn);
308         return (wasNull(storeColumn.getName())?null:result);
309     }
310 
311 }
312