@Override
    public final E map(R record) {
      try {
        for (int i = 0; i < fields.length; i++) {
          if (propertyIndexes[i] != null) {
            parameterValues[propertyIndexes[i]] = record.getValue(i);
          } else {
            for (java.lang.reflect.Field member : members[i]) {
              int index = propertyNames.indexOf(member.getName());

              if (index >= 0) {
                parameterValues[index] = record.getValue(i);
              }
            }

            if (methods[i] != null) {
              String name = getPropertyName(methods[i].getName());
              int index = propertyNames.indexOf(name);

              if (index >= 0) {
                parameterValues[index] = record.getValue(i);
              }
            }
          }
        }

        Object[] converted = Convert.convert(parameterValues, parameterTypes);
        return accessible(constructor).newInstance(converted);
      } catch (Exception e) {
        throw new MappingException("An error ocurred when mapping record to " + type, e);
      }
    }
    @SuppressWarnings("rawtypes")
    @Override
    public final E map(R record) {
      try {
        E result = instance != null ? instance : constructor.newInstance();

        for (int i = 0; i < fields.length; i++) {
          for (java.lang.reflect.Field member : members[i]) {

            // [#935] Avoid setting final fields
            if ((member.getModifiers() & Modifier.FINAL) == 0) {
              map(record, result, member, i);
            }
          }

          for (java.lang.reflect.Method method : methods[i]) {
            Class<?> mType = method.getParameterTypes()[0];
            Object value = record.getValue(i, mType);

            // [#3082] Map nested collection types
            if (value instanceof Collection && List.class.isAssignableFrom(mType)) {
              Class componentType =
                  (Class)
                      ((ParameterizedType) method.getGenericParameterTypes()[0])
                          .getActualTypeArguments()[0];
              method.invoke(result, Convert.convert((Collection) value, componentType));
            }

            // Default reference types (including arrays)
            else {
              method.invoke(result, record.getValue(i, mType));
            }
          }
        }

        for (Entry<String, List<RecordMapper<R, Object>>> entry : nested.entrySet()) {
          String prefix = entry.getKey();

          for (RecordMapper<R, Object> mapper : entry.getValue()) {
            Object value = mapper.map(record);

            for (java.lang.reflect.Field member : getMatchingMembers(configuration, type, prefix)) {

              // [#935] Avoid setting final fields
              if ((member.getModifiers() & Modifier.FINAL) == 0) {
                map(value, result, member);
              }
            }

            for (Method method : getMatchingSetters(configuration, type, prefix)) {
              method.invoke(result, value);
            }
          }
        }

        return result;
      } catch (Exception e) {
        throw new MappingException("An error ocurred when mapping record to " + type, e);
      }
    }
Beispiel #3
0
  private static Object[] convertArray(Array array, Class<? extends Object[]> type)
      throws SQLException {
    if (array != null) {
      return Convert.convert(array.getArray(), type);
    }

    return null;
  }
 @Override
 public final E map(R record) {
   try {
     Object[] converted = Convert.convert(record.intoArray(), parameterTypes);
     return constructor.newInstance(converted);
   } catch (Exception e) {
     throw new MappingException("An error ocurred when mapping record to " + type, e);
   }
 }
Beispiel #5
0
  private static Object[] convertArray(Object array, Class<? extends Object[]> type)
      throws SQLException {
    if (array instanceof Object[]) {
      return Convert.convert(array, type);
    } else if (array instanceof Array) {
      return convertArray((Array) array, type);
    }

    return null;
  }
Beispiel #6
0
  @SuppressWarnings("unchecked")
  private static <T, U> void testConversion(U expected, T from, Class<U> toClass) {
    if (from != null) {
      assertEquals(from, Convert.convert(from, Object.class));
      assertEquals(from, Convert.convert(from, from.getClass()));
    }

    U conv1 = Convert.convert(from, toClass);
    assertEquals(expected, conv1);

    if (toClass.isPrimitive()) {
      assertTrue(wrapper(toClass).isInstance(conv1));
      return;
    } else if (expected == null) {
      assertNull(conv1);
    } else {
      assertTrue(toClass.isInstance(conv1));
    }

    Class<?> toArrayClass = Array.newInstance(toClass, 0).getClass();

    Object[] conv2 = Convert.convert(new Object[] {from, from}, new Class[] {toClass, toClass});
    U[] conv3 = (U[]) Convert.convert(new Object[] {from, from}, toClass);
    U[] conv4 = (U[]) Convert.convertArray(new Object[] {from, from}, toClass);
    U[] conv5 = (U[]) Convert.convertArray(new Object[] {from, from}, toArrayClass);

    assertEquals(2, conv2.length);
    assertEquals(2, conv3.length);
    assertEquals(2, conv4.length);
    assertEquals(2, conv5.length);
    assertEquals(expected, conv2[0]);
    assertEquals(expected, conv2[1]);
    assertEquals(expected, conv3[0]);
    assertEquals(expected, conv3[1]);
    assertEquals(expected, conv4[0]);
    assertEquals(expected, conv4[1]);
    assertEquals(expected, conv5[0]);
    assertEquals(expected, conv5[1]);
    assertTrue(Object[].class.isInstance(conv2));
    assertTrue(toArrayClass.isInstance(conv3));
    assertTrue(toArrayClass.isInstance(conv4));
    assertTrue(toArrayClass.isInstance(conv5));
  }
    @Override
    public final E map(R record) {
      int size = record.size();
      Class<?> componentType = type.getComponentType();
      Object[] result =
          (Object[]) (instance != null ? instance : Array.newInstance(componentType, size));

      // Just as in Collection.toArray(Object[]), return a new array in case
      // sizes don't match
      if (size > result.length) {
        result = (Object[]) Array.newInstance(componentType, size);
      }

      for (int i = 0; i < size; i++) {
        result[i] = Convert.convert(record.getValue(i), componentType);
      }

      return (E) result;
    }
    @SuppressWarnings("rawtypes")
    private final void map(Record record, Object result, java.lang.reflect.Field member, int index)
        throws IllegalAccessException {
      Class<?> mType = member.getType();

      if (mType.isPrimitive()) {
        if (mType == byte.class) {
          map(record.getValue(index, byte.class), result, member);
        } else if (mType == short.class) {
          map(record.getValue(index, short.class), result, member);
        } else if (mType == int.class) {
          map(record.getValue(index, int.class), result, member);
        } else if (mType == long.class) {
          map(record.getValue(index, long.class), result, member);
        } else if (mType == float.class) {
          map(record.getValue(index, float.class), result, member);
        } else if (mType == double.class) {
          map(record.getValue(index, double.class), result, member);
        } else if (mType == boolean.class) {
          map(record.getValue(index, boolean.class), result, member);
        } else if (mType == char.class) {
          map(record.getValue(index, char.class), result, member);
        }
      } else {
        Object value = record.getValue(index, mType);

        // [#3082] Map nested collection types
        if (value instanceof Collection && List.class.isAssignableFrom(mType)) {
          Class componentType =
              (Class) ((ParameterizedType) member.getGenericType()).getActualTypeArguments()[0];
          member.set(result, Convert.convert((Collection) value, componentType));
        }

        // Default reference types (including arrays)
        else {
          map(value, result, member);
        }
      }
    }
Beispiel #9
0
 @Override
 public final <T> T fetchOne(String fieldName, Class<? extends T> type) {
   return Convert.convert(fetchOne(fieldName), type);
 }
Beispiel #10
0
 @Override
 public final <U> U fetchOne(String fieldName, Converter<?, U> converter) {
   return Convert.convert(fetchOne(fieldName), converter);
 }
Beispiel #11
0
 @Override
 public final <T> T fetchOne(int fieldIndex, Class<? extends T> type) {
   return Convert.convert(fetchOne(fieldIndex), type);
 }
Beispiel #12
0
 @Override
 public final <U> U fetchOne(int fieldIndex, Converter<?, U> converter) {
   return Convert.convert(fetchOne(fieldIndex), converter);
 }
Beispiel #13
0
 @Override
 public final <T> T fetchOne(Field<?> field, Class<? extends T> type) {
   return Convert.convert(fetchOne(field), type);
 }
Beispiel #14
0
 @Override
 public final <T, U> U fetchOne(Field<T> field, Converter<? super T, U> converter) {
   return Convert.convert(fetchOne(field), converter);
 }
Beispiel #15
0
 @Override
 public final <U> List<U> getValues(int fieldIndex, Converter<?, U> converter) {
   return Convert.convert(getValues(fieldIndex), converter);
 }
Beispiel #16
0
 @Override
 public final <T> List<T> getValues(int fieldIndex, Class<? extends T> type) {
   return Convert.convert(getValues(fieldIndex), type);
 }
Beispiel #17
0
 @Override
 public final <T> T getValue(Field<?> field, Class<? extends T> type) {
   return Convert.convert(getValue(field), type);
 }
Beispiel #18
0
  @SuppressWarnings("unchecked")
  private static <T> T getFromResultSet(ExecuteContext ctx, Class<? extends T> type, int index)
      throws SQLException {

    ResultSet rs = ctx.resultSet();

    if (type == Blob.class) {
      return (T) rs.getBlob(index);
    } else if (type == Boolean.class) {
      return (T) checkWasNull(rs, Boolean.valueOf(rs.getBoolean(index)));
    } else if (type == BigInteger.class) {
      // The SQLite JDBC driver doesn't support BigDecimals
      if (ctx.getDialect() == SQLDialect.SQLITE) {
        return Convert.convert(rs.getString(index), (Class<? extends T>) BigInteger.class);
      } else {
        BigDecimal result = rs.getBigDecimal(index);
        return (T) (result == null ? null : result.toBigInteger());
      }
    } else if (type == BigDecimal.class) {
      // The SQLite JDBC driver doesn't support BigDecimals
      if (ctx.getDialect() == SQLDialect.SQLITE) {
        return Convert.convert(rs.getString(index), (Class<? extends T>) BigDecimal.class);
      } else {
        return (T) rs.getBigDecimal(index);
      }
    } else if (type == Byte.class) {
      return (T) checkWasNull(rs, Byte.valueOf(rs.getByte(index)));
    } else if (type == byte[].class) {
      return (T) rs.getBytes(index);
    } else if (type == Clob.class) {
      return (T) rs.getClob(index);
    } else if (type == Date.class) {
      return (T) getDate(ctx.getDialect(), rs, index);
    } else if (type == Double.class) {
      return (T) checkWasNull(rs, Double.valueOf(rs.getDouble(index)));
    } else if (type == Float.class) {
      return (T) checkWasNull(rs, Float.valueOf(rs.getFloat(index)));
    } else if (type == Integer.class) {
      return (T) checkWasNull(rs, Integer.valueOf(rs.getInt(index)));
    } else if (type == Long.class) {
      return (T) checkWasNull(rs, Long.valueOf(rs.getLong(index)));
    } else if (type == Short.class) {
      return (T) checkWasNull(rs, Short.valueOf(rs.getShort(index)));
    } else if (type == String.class) {
      return (T) rs.getString(index);
    } else if (type == Time.class) {
      return (T) getTime(ctx.getDialect(), rs, index);
    } else if (type == Timestamp.class) {
      return (T) getTimestamp(ctx.getDialect(), rs, index);
    } else if (type == YearToMonth.class) {
      if (ctx.getDialect() == POSTGRES) {
        Object object = rs.getObject(index);
        return (T) (object == null ? null : PostgresUtils.toYearToMonth(object));
      } else {
        String string = rs.getString(index);
        return (T) (string == null ? null : YearToMonth.valueOf(string));
      }
    } else if (type == DayToSecond.class) {
      if (ctx.getDialect() == POSTGRES) {
        Object object = rs.getObject(index);
        return (T) (object == null ? null : PostgresUtils.toDayToSecond(object));
      } else {
        String string = rs.getString(index);
        return (T) (string == null ? null : DayToSecond.valueOf(string));
      }
    } else if (type == UByte.class) {
      String string = rs.getString(index);
      return (T) (string == null ? null : UByte.valueOf(string));
    } else if (type == UShort.class) {
      String string = rs.getString(index);
      return (T) (string == null ? null : UShort.valueOf(string));
    } else if (type == UInteger.class) {
      String string = rs.getString(index);
      return (T) (string == null ? null : UInteger.valueOf(string));
    } else if (type == ULong.class) {
      String string = rs.getString(index);
      return (T) (string == null ? null : ULong.valueOf(string));
    }

    // The type byte[] is handled earlier. byte[][] can be handled here
    else if (type.isArray()) {
      switch (ctx.getDialect()) {
        case POSTGRES:
          {
            return pgGetArray(ctx, type, index);
          }

        default:
          // Note: due to a HSQLDB bug, it is not recommended to call rs.getObject() here:
          // See https://sourceforge.net/tracker/?func=detail&aid=3181365&group_id=23316&atid=378131
          return (T) convertArray(rs.getArray(index), (Class<? extends Object[]>) type);
      }
    } else if (ArrayRecord.class.isAssignableFrom(type)) {
      return (T) getArrayRecord(ctx, rs.getArray(index), (Class<? extends ArrayRecord<?>>) type);
    } else if (EnumType.class.isAssignableFrom(type)) {
      return getEnumType(type, rs.getString(index));
    } else if (MasterDataType.class.isAssignableFrom(type)) {
      return (T) getMasterDataType(type, rs.getObject(index));
    } else if (UDTRecord.class.isAssignableFrom(type)) {
      switch (ctx.getDialect()) {
        case POSTGRES:
          return (T) pgNewUDTRecord(type, rs.getObject(index));
      }

      return (T) rs.getObject(index, DataTypes.udtRecords());
    } else if (Result.class.isAssignableFrom(type)) {
      ResultSet nested = (ResultSet) rs.getObject(index);
      return (T) getNewFactory(ctx).fetch(nested);
    } else {
      return (T) rs.getObject(index);
    }
  }
Beispiel #19
0
 @Override
 public final <T> T getValue(int index, Class<? extends T> type) {
   return Convert.convert(getValue(index), type);
 }
Beispiel #20
0
 @Override
 public final <T, U> List<U> getValues(Field<T> field, Converter<? super T, U> converter) {
   return Convert.convert(getValues(field), converter);
 }
Beispiel #21
0
 @Override
 public final <U> U getValue(int index, Converter<?, U> converter) {
   return Convert.convert(getValue(index), converter);
 }
Beispiel #22
0
 @Override
 public final <T> T getValue(String fieldName, Class<? extends T> type) {
   return Convert.convert(getValue(fieldName), type);
 }
Beispiel #23
0
 @Override
 public final <U> U getValue(String fieldName, Converter<?, U> converter) {
   return Convert.convert(getValue(fieldName), converter);
 }