]> review.fuel-infra Code Review - packages/trusty/mysql-wsrep-5.6.git/blob
3e375021f658f1d981db4ae0c0ce84f5b6602708
[packages/trusty/mysql-wsrep-5.6.git] /
1 /*
2    Copyright (c) 2010, 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.openjpa;
19
20 import com.mysql.clusterj.core.spi.DomainTypeHandler;
21 import com.mysql.clusterj.core.store.ResultData;
22 import com.mysql.clusterj.core.util.I18NHelper;
23 import com.mysql.clusterj.core.util.Logger;
24 import com.mysql.clusterj.core.util.LoggerFactoryService;
25
26
27
28 import java.sql.SQLException;
29 import java.sql.Types;
30 import java.util.BitSet;
31 import java.util.HashMap;
32 import java.util.Map;
33 import java.util.Set;
34 import org.apache.openjpa.jdbc.kernel.JDBCStore;
35 import org.apache.openjpa.jdbc.meta.JavaSQLTypes;
36 import org.apache.openjpa.jdbc.schema.Column;
37 import org.apache.openjpa.jdbc.sql.AbstractResult;
38 import org.apache.openjpa.jdbc.sql.Joins;
39 import org.apache.openjpa.jdbc.sql.Result;
40 import org.apache.openjpa.meta.JavaTypes;
41
42
43 /**
44  * Result for use with the Ndb runtime.
45  *
46  */
47 public class NdbOpenJPAResult extends AbstractResult implements Result {
48
49     /** My message translator */
50     static final I18NHelper local = I18NHelper.getInstance(NdbOpenJPAResult.class);
51
52     /** My logger */
53     static final Logger logger = LoggerFactoryService.getFactory().getInstance(NdbOpenJPAResult.class);
54
55     /** The ResultData as returned from the operation. */
56     protected ResultData resultData;
57
58     /** The DomainTypeHandler for the root object of the operation. */
59     protected DomainTypeHandler<?> domainTypeHandler;
60
61     /** The BitSet of fields requested from the operation. */
62     protected BitSet fields;
63
64     /** The Set of columns in this result. */
65     // TODO: needed? might need if joining tables with the same column names
66 //    protected Set<Column> columns = new HashSet<Column>();
67
68     /** The Map of column names to columns in this result. */
69     private Map<String, com.mysql.clusterj.core.store.Column> columnMap =
70         new HashMap<String, com.mysql.clusterj.core.store.Column>();
71
72     /**
73      * Construct the Result with the result of an NDB query.
74      * This encapsulates both the result from the database and the
75      * metadata that describes the result.
76      */
77     public NdbOpenJPAResult(ResultData resultData, DomainTypeHandler<?> domainTypeHandler,
78             BitSet fields) {
79         super();
80         this.resultData = resultData;
81         this.domainTypeHandler = domainTypeHandler;
82         this.fields = fields;
83         Set<com.mysql.clusterj.core.store.Column> storeColumns = domainTypeHandler.getStoreColumns(fields);
84         for (com.mysql.clusterj.core.store.Column storeColumn: storeColumns) {
85             columnMap.put(storeColumn.getName(), storeColumn);
86         }
87     }
88
89     @Override
90     protected boolean nextInternal() throws SQLException {
91         boolean result = resultData.next();
92         if (logger.isDetailEnabled()) {
93             logger.detail("returning: " + result);
94         }
95         return result;
96     }
97
98     @Override
99     protected boolean containsInternal(Object obj, Joins joins) throws SQLException {
100         // TODO: support join specifications
101         com.mysql.clusterj.core.store.Column storeColumn = resolve(obj);
102         return storeColumn != null;
103     }
104
105     @Override
106     protected Object getObjectInternal(Object obj, int metaType, Object arg, Joins joins) throws SQLException {
107         // TODO: support other object types
108         // TODO: support null values for int, double, float, long, and short
109         Object result = null;
110         com.mysql.clusterj.core.store.Column columnName = resolve(obj);
111         switch (metaType) {
112             case JavaTypes.INT_OBJ:
113 //                int value = resultData.getInt(columnName);
114 //                result = resultData.wasNull(columnName)?null:value;
115             case JavaTypes.INT:
116                 result = resultData.getInt(columnName);
117                 break;
118             case JavaTypes.DOUBLE_OBJ:
119 //              double value = resultData.getDouble(columnName);
120 //              result = resultData.wasNull(columnName)?null:value;
121             case JavaTypes.DOUBLE:
122                 result = resultData.getDouble(columnName);
123                 break;
124             case JavaTypes.FLOAT_OBJ:
125 //              float value = resultData.getFloat(columnName);
126 //              result = resultData.wasNull(columnName)?null:value;
127             case JavaTypes.FLOAT:
128                 result = resultData.getFloat(columnName);
129                 break;
130             case JavaTypes.STRING:
131                 result = resultData.getString(columnName);
132                 break;
133             case JavaTypes.LONG_OBJ:
134 //              long value = resultData.getLong(columnName);
135 //              result = resultData.wasNull(columnName)?null:value;
136             case JavaTypes.LONG:
137                 result = resultData.getLong(columnName);
138                 break;
139             case JavaTypes.BIGDECIMAL:
140                 result = resultData.getDecimal(columnName);
141                 break;
142             case JavaTypes.BIGINTEGER:
143                 result = resultData.getBigInteger(columnName);
144                 break;
145             case JavaTypes.DATE:
146                 result = new java.util.Date(resultData.getLong(columnName));
147                 break;
148             case JavaSQLTypes.TIME:
149                 result = new java.sql.Time(resultData.getLong(columnName));
150                 break;
151             case JavaSQLTypes.SQL_DATE:
152                 result = new java.sql.Date(resultData.getLong(columnName));
153                 break;
154             case JavaSQLTypes.TIMESTAMP:
155                 result = new java.sql.Timestamp(resultData.getLong(columnName));
156                 break;
157             case JavaSQLTypes.BYTES:
158                 result = resultData.getBytes(columnName);
159                 break;
160             case JavaTypes.BOOLEAN:
161             case JavaTypes.BOOLEAN_OBJ:
162                 result = resultData.getObjectBoolean(columnName);
163                 break;
164             default:
165                 if (obj instanceof Column) {
166                     Column col = (Column) obj;
167                     if (col.getType() == Types.BLOB
168                         || col.getType() == Types.VARBINARY) {
169                         result = resultData.getBytes(columnName);
170                     }
171                 }
172                 result = resultData.getInt(columnName);
173         }
174         if (logger.isDetailEnabled()) {
175             logger.detail("obj: " + obj + " arg: " + arg + " joins: " + joins + " metaType: " + metaType + " result: " + result);
176         }
177         return result;
178     }
179
180     @Override
181     protected Object getStreamInternal(JDBCStore store, Object obj, int metaType, Object arg, Joins joins) throws SQLException {
182         throw new UnsupportedOperationException("Not supported yet.");
183     }
184
185     public int size() throws SQLException {
186         throw new UnsupportedOperationException("Not supported yet.");
187     }
188
189     /** This method exists solely to bypass a known bug in OpenJPA.
190      * http://issues.apache.org/jira/browse/OPENJPA-1158
191      * @param obj the column (or column name) to fetch
192      * @param joins joins
193      * @return the long value
194      * @throws java.sql.SQLException 
195      */
196     @Override
197     protected long getLongInternal(Object obj, Joins joins)
198         throws SQLException {
199         Number val = (Number) checkNull(getObjectInternal(obj,
200             JavaTypes.LONG, null, joins));
201         return (val == null) ? 0 : val.longValue();
202     }
203
204     protected com.mysql.clusterj.core.store.Column resolve(Object obj) {
205         String key = null;
206         com.mysql.clusterj.core.store.Column result = null;
207         if (logger.isDetailEnabled()) {
208             logger.detail("resolving object of type: " + obj.getClass().getName());
209         }
210         if (obj instanceof String) {
211             key = (String)obj;
212             result = columnMap.get(key);
213         } else if (obj instanceof Column) {
214             key = ((Column)obj).getName();
215             result = columnMap.get(key);
216         } else {
217             throw new UnsupportedOperationException(
218                     local.message("ERR_Unsupported_Object_Type_For_Resolve", obj.getClass().getName()));
219         }
220         if (logger.isDetailEnabled()) logger.detail("key: " + key + " column: " + ((result==null)?"<null>":result.getName()));
221         return result;
222     }
223
224     public Set<String> getColumnNames() {
225         return columnMap.keySet();
226     }
227 }