2 Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
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.
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.
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
18 package com.mysql.clusterj.openjpa;
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;
28 import java.sql.SQLException;
29 import java.sql.Types;
30 import java.util.BitSet;
31 import java.util.HashMap;
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;
44 * Result for use with the Ndb runtime.
47 public class NdbOpenJPAResult extends AbstractResult implements Result {
49 /** My message translator */
50 static final I18NHelper local = I18NHelper.getInstance(NdbOpenJPAResult.class);
53 static final Logger logger = LoggerFactoryService.getFactory().getInstance(NdbOpenJPAResult.class);
55 /** The ResultData as returned from the operation. */
56 protected ResultData resultData;
58 /** The DomainTypeHandler for the root object of the operation. */
59 protected DomainTypeHandler<?> domainTypeHandler;
61 /** The BitSet of fields requested from the operation. */
62 protected BitSet fields;
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>();
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>();
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.
77 public NdbOpenJPAResult(ResultData resultData, DomainTypeHandler<?> domainTypeHandler,
80 this.resultData = resultData;
81 this.domainTypeHandler = domainTypeHandler;
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);
90 protected boolean nextInternal() throws SQLException {
91 boolean result = resultData.next();
92 if (logger.isDetailEnabled()) {
93 logger.detail("returning: " + result);
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;
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);
112 case JavaTypes.INT_OBJ:
113 // int value = resultData.getInt(columnName);
114 // result = resultData.wasNull(columnName)?null:value;
116 result = resultData.getInt(columnName);
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);
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);
130 case JavaTypes.STRING:
131 result = resultData.getString(columnName);
133 case JavaTypes.LONG_OBJ:
134 // long value = resultData.getLong(columnName);
135 // result = resultData.wasNull(columnName)?null:value;
137 result = resultData.getLong(columnName);
139 case JavaTypes.BIGDECIMAL:
140 result = resultData.getDecimal(columnName);
142 case JavaTypes.BIGINTEGER:
143 result = resultData.getBigInteger(columnName);
146 result = new java.util.Date(resultData.getLong(columnName));
148 case JavaSQLTypes.TIME:
149 result = new java.sql.Time(resultData.getLong(columnName));
151 case JavaSQLTypes.SQL_DATE:
152 result = new java.sql.Date(resultData.getLong(columnName));
154 case JavaSQLTypes.TIMESTAMP:
155 result = new java.sql.Timestamp(resultData.getLong(columnName));
157 case JavaSQLTypes.BYTES:
158 result = resultData.getBytes(columnName);
160 case JavaTypes.BOOLEAN:
161 case JavaTypes.BOOLEAN_OBJ:
162 result = resultData.getObjectBoolean(columnName);
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);
172 result = resultData.getInt(columnName);
174 if (logger.isDetailEnabled()) {
175 logger.detail("obj: " + obj + " arg: " + arg + " joins: " + joins + " metaType: " + metaType + " result: " + result);
181 protected Object getStreamInternal(JDBCStore store, Object obj, int metaType, Object arg, Joins joins) throws SQLException {
182 throw new UnsupportedOperationException("Not supported yet.");
185 public int size() throws SQLException {
186 throw new UnsupportedOperationException("Not supported yet.");
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
193 * @return the long value
194 * @throws java.sql.SQLException
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();
204 protected com.mysql.clusterj.core.store.Column resolve(Object obj) {
206 com.mysql.clusterj.core.store.Column result = null;
207 if (logger.isDetailEnabled()) {
208 logger.detail("resolving object of type: " + obj.getClass().getName());
210 if (obj instanceof String) {
212 result = columnMap.get(key);
213 } else if (obj instanceof Column) {
214 key = ((Column)obj).getName();
215 result = columnMap.get(key);
217 throw new UnsupportedOperationException(
218 local.message("ERR_Unsupported_Object_Type_For_Resolve", obj.getClass().getName()));
220 if (logger.isDetailEnabled()) logger.detail("key: " + key + " column: " + ((result==null)?"<null>":result.getName()));
224 public Set<String> getColumnNames() {
225 return columnMap.keySet();