All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.datanucleus.store.rdbms.query.ResultClassROF Maven / Gradle / Ivy

There is a newer version: 6.0.8
Show newest version
/**********************************************************************
Copyright (c) 2005 Andy Jefferson and others. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Contributors:
    ...
**********************************************************************/
package org.datanucleus.store.rdbms.query;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import org.datanucleus.ExecutionContext;
import org.datanucleus.exceptions.NucleusException;
import org.datanucleus.exceptions.NucleusUserException;
import org.datanucleus.metadata.AbstractClassMetaData;
import org.datanucleus.query.QueryUtils;
import org.datanucleus.store.rdbms.RDBMSStoreManager;
import org.datanucleus.store.rdbms.mapping.StatementClassMapping;
import org.datanucleus.store.rdbms.mapping.StatementMappingIndex;
import org.datanucleus.util.ClassUtils;
import org.datanucleus.util.Localiser;
import org.datanucleus.util.NucleusLogger;
import org.datanucleus.util.StringUtils;

/**
 * Take a ResultSet, and for each row retrieves an object of a specified type.
 * Follows the rules in JDO2 spec [14.6.12] regarding the result class.
 * 
 * 

* The resultClass will be used to create objects of that type when calling * getObject(). The resultClass can be one of the following *

    *
  • Simple type - String, Long, Integer, Float, Boolean, Byte, Character, Double, * Short, BigDecimal, BigInteger, java.util.Date, java.sql.Date, java.sql.Time, java.sql.Timestamp
  • *
  • java.util.Map - the JDO impl will choose the concrete impl of java.util.Map to use
  • *
  • Object[]
  • *
  • User defined type with either a constructor taking the result set fields, * or a default constructor and setting the fields using a put(Object,Object) method, setXXX methods, or public fields
  • *
* *

* Objects of this class are created in 2 distinct situations. The first is where a * candidate class is available, and consequently field position mappings are * available. The second is where no candidate class is available and so * only the field names are available, and the results are taken in ResultSet order. * These 2 modes have their own constructor. */ public class ResultClassROF implements ResultObjectFactory { private final RDBMSStoreManager storeMgr; /** The result class that we should create for each row of results. */ private final Class resultClass; /** The index of fields position to mapping type. */ private final StatementMappingIndex[] stmtMappings; /** Definition of results when the query has a result clause. */ private StatementResultMapping resultDefinition; /** Names of the result field columns (in the ResultSet). */ private final String[] resultFieldNames; private final Class[] resultFieldTypes; /** Map of the ResultClass Fields, keyed by the field names (only for user-defined result classes). */ private final Map resultClassFieldsByName = new HashMap(); /** * Constructor for a resultClass object factory where we have a result clause specified. * @param storeMgr RDBMS StoreManager * @param cls The result class to use (if any) * @param resultDefinition The mapping information for the result expressions */ public ResultClassROF(RDBMSStoreManager storeMgr, Class cls, StatementResultMapping resultDefinition) { this.storeMgr = storeMgr; // Set the result class that we convert each row into Class tmpClass = null; if (cls != null && cls.getName().equals("java.util.Map")) { // Spec 14.6.12 If user specifies java.util.Map, then impl chooses its own implementation Map class tmpClass = HashMap.class; } else if (cls == null) { // No result class specified so return Object/Object[] depending on number of expressions if (resultDefinition.getNumberOfResultExpressions() == 1) { tmpClass = Object.class; } else { tmpClass = Object[].class; } } else { tmpClass = cls; } this.resultClass = tmpClass; this.resultDefinition = resultDefinition; this.stmtMappings = null; if (resultDefinition != null) { this.resultFieldNames = new String[resultDefinition.getNumberOfResultExpressions()]; this.resultFieldTypes = new Class[resultDefinition.getNumberOfResultExpressions()]; for (int i=0;i 0) { str.append(","); } Class javaType = stmtMappings[i].getMapping().getJavaType(); str.append(javaType.getName()); } NucleusLogger.QUERY.debug(Localiser.msg("021206", resultClass.getName(), str.toString())); } } // B. No argumented constructor exists so create an object and update fields using fields/put method/set method obj = QueryUtils.createResultObjectUsingDefaultConstructorAndSetters(resultClass, resultFieldNames, resultClassFieldsByName, fieldValues); return obj; } // Impossible to satisfy the resultClass requirements so throw exception String msg = Localiser.msg("021203",resultClass.getName()); NucleusLogger.QUERY.error(msg); throw new NucleusUserException(msg); } /** * Convenience method to return the value of a NewObject mapping for the current row of the provided * query results. * @param newMap new object mapping * @param ec ExecutionContext * @param rs Query results * @return The value of the new object */ protected Object getValueForNewObject(StatementNewObjectMapping newMap, ExecutionContext ec, ResultSet rs) { Object value = null; if (newMap.getNumberOfConstructorArgMappings() == 0) { try { value = newMap.getObjectClass().newInstance(); } catch (Exception e) { throw new NucleusException("Attempt to create object for query result row of type " + newMap.getObjectClass().getName() + " threw an exception", e); } } else { int numArgs = newMap.getNumberOfConstructorArgMappings(); Class[] ctrArgTypes = new Class[numArgs]; Object[] ctrArgValues = new Object[numArgs]; for (int i=0;i ResultSetGetters by result classes */ private static Map resultSetGetters = new HashMap(20); static { // any type specific getter from ResultSet that we can guess from the desired result class resultSetGetters.put(Boolean.class, new ResultSetGetter() { public Object getValue(ResultSet rs, int i) throws SQLException { return Boolean.valueOf(rs.getBoolean(i)); } }); resultSetGetters.put(Byte.class, new ResultSetGetter() { public Object getValue(ResultSet rs, int i) throws SQLException { return Byte.valueOf(rs.getByte(i)); } }); resultSetGetters.put(Short.class, new ResultSetGetter() { public Object getValue(ResultSet rs, int i) throws SQLException { return Short.valueOf(rs.getShort(i)); } }); resultSetGetters.put(Integer.class, new ResultSetGetter() { public Object getValue(ResultSet rs, int i) throws SQLException { return Integer.valueOf(rs.getInt(i)); } }); resultSetGetters.put(Long.class, new ResultSetGetter() { public Object getValue(ResultSet rs, int i) throws SQLException { return Long.valueOf(rs.getLong(i)); } }); resultSetGetters.put(Float.class, new ResultSetGetter() { public Object getValue(ResultSet rs, int i) throws SQLException { return Float.valueOf(rs.getFloat(i)); } }); resultSetGetters.put(Double.class, new ResultSetGetter() { public Object getValue(ResultSet rs, int i) throws SQLException { return Double.valueOf(rs.getDouble(i)); } }); resultSetGetters.put(BigDecimal.class, new ResultSetGetter() { public Object getValue(ResultSet rs, int i) throws SQLException { return rs.getBigDecimal(i); } }); resultSetGetters.put(byte[].class, new ResultSetGetter() { public Object getValue(ResultSet rs, int i) throws SQLException { return rs.getBytes(i); } }); ResultSetGetter timestampGetter = new ResultSetGetter() { public Object getValue(ResultSet rs, int i) throws SQLException { return rs.getTimestamp(i); } }; resultSetGetters.put(java.sql.Timestamp.class, timestampGetter); // also use Timestamp getter for Date, so it also has time of the day e.g. with Oracle resultSetGetters.put(java.util.Date.class, timestampGetter); resultSetGetters.put(java.sql.Date.class, new ResultSetGetter() { public Object getValue(ResultSet rs, int i) throws SQLException { return rs.getDate(i); } }); resultSetGetters.put(String.class, new ResultSetGetter() { public Object getValue(ResultSet rs, int i) throws SQLException { return rs.getString(i); } }); resultSetGetters.put(java.io.Reader.class, new ResultSetGetter() { public Object getValue(ResultSet rs, int i) throws SQLException { return rs.getCharacterStream(i); } }); resultSetGetters.put(java.sql.Array.class, new ResultSetGetter() { public Object getValue(ResultSet rs, int i) throws SQLException { return rs.getArray(i); } }); } /** * Convenience method to read the value of a column out of the ResultSet. * @param rs ResultSet * @param columnNumber Number of the column (starting at 1) * @return Value for the column for this row. * @throws SQLException Thrown if an error occurs on reading */ private Object getResultObject(final ResultSet rs, int columnNumber) throws SQLException { // use getter on ResultSet specific to our desired resultClass ResultSetGetter getter = resultSetGetters.get(resultClass); if (getter != null) { // User has specified a result type for this column so use the specific getter return getter.getValue(rs, columnNumber); } // User has specified Object/Object[] so just retrieve generically return rs.getObject(columnNumber); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy