Beispiel #1
0
  /**
   * Get the returning record in those dialects that do not support fetching arbitrary fields from
   * JDBC's {@link Statement#getGeneratedKeys()} method.
   */
  @SuppressWarnings("unchecked")
  private final void selectReturning(Configuration configuration, Object... values) {
    if (values != null && values.length > 0) {

      // This shouldn't be null, as relevant dialects should
      // return empty generated keys ResultSet
      if (into.getIdentity() != null) {
        Field<Number> field = (Field<Number>) into.getIdentity().getField();
        Number[] ids = new Number[values.length];
        for (int i = 0; i < values.length; i++) {
          ids[i] = field.getDataType().convert(values[i]);
        }

        // Only the IDENTITY value was requested. No need for an
        // additional query
        if (returning.size() == 1 && new Fields(returning).field(field) != null) {
          for (Number id : ids) {
            R typed = Utils.newRecord(into, configuration);
            ((AbstractRecord) typed).setValue(field, new Value<Number>(id));
            getReturnedRecords().add(typed);
          }
        }

        // Other values are requested, too. Run another query
        else {
          returned =
              create(configuration)
                  .select(returning)
                  .from(into)
                  .where(field.in(ids))
                  .fetchInto(into);
        }
      }
    }
  }