queryimpl.java

来自「RESIN 3.2 最新源码」· Java 代码 · 共 1,262 行 · 第 1/3 页

JAVA
1,262
字号
    ArrayList<FieldResultConfig> fieldResults      = entityResult.getFieldResults();    Entity entity = null;    int consumed = 0;    try {      // jpa/0y14      entity = (Entity) _aConn.load(className, rs.getObject(oldIndex));      ArrayList<String> columnNameList = new ArrayList<String>();      entityType.generateNativeColumnNames(columnNameList);      String []columnNames = new String[columnNameList.size()];      columnNameList.toArray(columnNames);      entity.__caucho_load_native(rs, columnNames);      // jpa/0y10      //consumed = entity.__caucho_load(_aConn, rs, oldIndex + keyLength);    } catch (Exception e) {      // jpa/0y1a: invalid query.      throw new IllegalStateException(L.l("Unable to load an entity of class '{0}' using a native query. When mapped to @EntityResult, a native query should select all fields for the corresponding entity in '{1}'", className, _nativeSql));    }    // item.setNumberOfLoadingColumns(consumed);    _currIndex++;    // jpa/0y12, jpa/0y15    _currIndex += consumed;    return entity;  }  /**   * Returns the object using the correct   * result set getter based on SQL type.   */  private Object getInternalObject(ResultSet rs,                                   int columnType)    throws Exception  {    // jpa/110-, jpa/11a4, and jpa/11z1    int oldIndex = _currIndex;    _currIndex++;    Object object = rs.getObject(oldIndex);    if (object instanceof Entity) {      // _currIndex += ((ResultSetImpl) rs).getNumberOfLoadingColumns();      ArrayList<AmberExpr> resultList        = ((SelectQuery) _query).getResultList();      AmberExpr expr = resultList.get(oldIndex-1);      // jpa/1160      if (expr instanceof LoadEntityExpr) {        LoadEntityExpr entityExpr = (LoadEntityExpr) expr;        joinFetch((ResultSetImpl) rs, entityExpr, (Entity) object);      }      return object;    }    if (object == null)      return null;    switch (columnType) {    case Types.BIT:    case Types.BOOLEAN:      //      try {      //        object = rs.getInt(oldIndex);      //      } catch (Exception ex) {      if (! (object instanceof Boolean))        object = rs.getBoolean(oldIndex);      //      }      break;    case Types.TINYINT:      if (! (object instanceof Number))        object = rs.getByte(oldIndex);      break;    case Types.SMALLINT:      if (! (object instanceof Number))        object = rs.getShort(oldIndex);      break;    case Types.INTEGER:      if (! (object instanceof Number))        object = rs.getLong(oldIndex);      break;    case Types.DECIMAL:    case Types.DOUBLE:    case Types.NUMERIC:    case Types.REAL:      if (! (object instanceof Number))        object = rs.getDouble(oldIndex);      break;    case Types.FLOAT:      if (! (object instanceof Number))        object = rs.getFloat(oldIndex);      break;    // It was fetched with getObject (see top).    // default:    //  object = rs.getObject(oldIndex);    }    return object;  }  private ArgExpr checkParameterIndex(int index)  {    // jpa/141h    ArgExpr args[] = _userQuery.getQuery().getArgList();    int len = args.length;    for (int i = 0; i < len; i++) {      if (args[i].getIndex() == index)        return args[i];    }    throw new IllegalArgumentException(L.l("Parameter index '{0}' is invalid for query {1}", index, _userQuery.getQuery()));  }  private void joinFetch(ResultSetImpl rs,                         LoadEntityExpr entityExpr,                         Entity entity)  {    String property = rs.getJoinFetchMap().get(entityExpr.getExpr());    EntityType entityType = entity.__caucho_getEntityType();    Iterator eagerFieldsIterator = null;    HashSet<String> eagerFieldNames = entityType.getEagerFieldNames();    if (eagerFieldNames != null)      eagerFieldsIterator = eagerFieldNames.iterator();    // XXX: needs to handle field-based access    if (! entityType.isFieldAccess()) {      if (property == null)        if ((eagerFieldsIterator != null) && eagerFieldsIterator.hasNext())          property = (String) eagerFieldsIterator.next();    }    if (property != null) {      try {        Class cl = entityType.getInstanceClass();        do {          String methodName = "get" +            Character.toUpperCase(property.charAt(0)) +            property.substring(1);          Method method = cl.getDeclaredMethod(methodName, null);          Object field = method.invoke(entity, null);          // XXX: for now, invoke the toString() method on          // the collection to fetch all the objects (join fetch).          if (field == null) {            try {              methodName = "__caucho_item_" + methodName;              method = cl.getDeclaredMethod(methodName, new Class[] {AmberConnection.class});              field = method.invoke(entity, _aConn);            } catch (Exception ex) {            }          }          if (field != null) {            Class fieldClass = field.getClass();            method = fieldClass.getMethod("toString", null);            method.invoke(field, null);          }          property = null;          if ((eagerFieldsIterator != null) && (eagerFieldsIterator.hasNext()))            property = (String) eagerFieldsIterator.next();        }        while (property != null);      } catch (NoSuchMethodException e) {        log.log(Level.FINER, e.toString(), e);      } catch (IllegalAccessException e) {        log.log(Level.FINER, e.toString(), e);      } catch (java.lang.reflect.InvocationTargetException e) {        log.log(Level.FINER, e.toString(), e);      }    }  }  /**   * Sets an indexed parameter.   */  private Query setInternalParameter(ArgExpr arg,                                     int index,                                     Object value)  {    try {      if (value == null) {        _userQuery.setNull(index, java.sql.Types.JAVA_OBJECT);        return this;      }      // jpa/141i      boolean valueIsNumber = value instanceof Number;      if (valueIsNumber) {        // jpa/0w20: type is null.        if (arg.getType() != null) {          boolean typeIsNumber = arg.getType().isNumeric();          if (! typeIsNumber)            throw new IllegalArgumentException(L.l("Type mismatch for parameter index '{0}'. Value '{1}' for type '{2}' is not valid in query '{3}'", index, value, arg.getType().getClass().getName(), _userQuery.getQuery().getSQL()));        }      }      setInternalParameter(null, _userQuery, index, value);      return this;    } catch (IndexOutOfBoundsException e) {      log.log(Level.FINE, e.toString(), e);      throw new IllegalArgumentException(L.l("Parameter index '{0}' is not valid for setParameter()", index));    }  }  /**   * Sets an indexed parameter.   */  private static void setInternalParameter(PreparedStatement pstmt,                                           UserQuery userQuery,                                           int index,                                           Object value)  {    try {      if (value instanceof Byte) {        byte arg = ((Byte) value).byteValue();        if (pstmt == null)          userQuery.setByte(index, arg);        else          pstmt.setByte(index, arg);      }      else if (value instanceof Short) {        short arg = ((Short) value).shortValue();        if (pstmt == null)          userQuery.setShort(index, arg);        else          pstmt.setShort(index, arg);      }      else if (value instanceof Integer) {        int arg = ((Integer) value).intValue();        if (pstmt == null)          userQuery.setInt(index, arg);        else          pstmt.setInt(index, arg);      }      else if (value instanceof Long) {        long arg = ((Long) value).longValue();        if (pstmt == null)          userQuery.setLong(index, arg);        else          pstmt.setLong(index, arg);      }      else if (value instanceof Float) {        float arg = ((Float) value).floatValue();        if (pstmt == null)          userQuery.setFloat(index, arg);        else          pstmt.setFloat(index, arg);      }      else if (value instanceof Double) { // jpa/141a        double arg = ((Double) value).doubleValue();        if (pstmt == null)          userQuery.setDouble(index, arg);        else          pstmt.setDouble(index, arg);      }      else if (value instanceof Character) {        if (pstmt == null)          userQuery.setString(index, value.toString());        else          pstmt.setString(index, value.toString());      }      else if (value instanceof Entity) {        // XXX: needs to handle Compound PK        Object pk = ((Entity) value).__caucho_getPrimaryKey();        if (pstmt == null)          userQuery.setObject(index, pk);        else          pstmt.setObject(index, pk);      }      else {        if (pstmt == null)          userQuery.setObject(index, value);        else          pstmt.setObject(index, value);      }    } catch (Exception e) {      log.log(Level.FINE, e.toString(), e);      throw new IllegalArgumentException(L.l("Parameter index '{0}' is not valid for setParameter()", index));    }  }  /**   * The maximum number of results to retrieve.   *   * @Since JPA 2.0   */  public int getMaxResults()  {    throw new UnsupportedOperationException(getClass().getName());  }  /**   * The first to retrieve.   *   * @Since JPA 2.0   */  public int getFirstResult()  {    throw new UnsupportedOperationException(getClass().getName());  }  /**   * Returns the implementation-specific hints   *   * @Since JPA 2.0   */  public Map getHints()  {    throw new UnsupportedOperationException(getClass().getName());  }  /**   * Returns the supported hints   *   * @Since JPA 2.0   */  public Set<String> getSupportedHints()  {    throw new UnsupportedOperationException(getClass().getName());  }  /**   * Returns the named parameters as a map   *   * @since JPA 2.0   */  public Map getNamedParameters()  {    throw new UnsupportedOperationException(getClass().getName());  }  /**   * Returns the positional parameters as a list   *   * @since JPA 2.0   */  public List getPositionalParameters()  {    throw new UnsupportedOperationException(getClass().getName());  }  /**   * Gets the flush type.   *   * @since JPA 2.0   */  public FlushModeType getFlushMode()  {    throw new UnsupportedOperationException(getClass().getName());  }  /**   * Sets the lock type.   *   * @since JPA 2.0   */  public Query setLockMode(LockModeType lockMode)  {    throw new UnsupportedOperationException(getClass().getName());  }  /**   * Gets the lock type.   *   * @since JPA 2.0   */  public LockModeType getLockMode()  {    throw new UnsupportedOperationException(getClass().getName());  }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?