Exemplo n.º 1
0
    @Override
    public Object map(R record) {
      AbstractRecord copy = (AbstractRecord) DSL.using(configuration).newRecord(f);

      for (int i = 0; i < f.length; i++) if (f[i] != null) copy.setValue(i, record.getValue(i));

      return d.map(record);
    }
Exemplo n.º 2
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);
        }
      }
    }
  }
Exemplo n.º 3
0
    @Override
    public R operate(R target) throws MappingException {
      AbstractRecord source = AbstractRecord.this;

      try {

        // [#1522] [#2989] If possible the complete state of this record should be copied onto the
        // other record
        if (target instanceof AbstractRecord) {
          AbstractRecord t = (AbstractRecord) target;

          // Iterate over target fields, to avoid ambiguities when two source fields share the same
          // name.
          for (int targetIndex = 0; targetIndex < t.size(); targetIndex++) {
            Field<?> targetField = t.field(targetIndex);
            int sourceIndex = fields.indexOf(targetField);

            if (sourceIndex >= 0) {
              DataType<?> targetType = targetField.getDataType();
              Value<?> sourceValue = values[sourceIndex];

              t.values[targetIndex] =
                  new Value<Object>(
                      targetType.convert(sourceValue.getValue()),
                      targetType.convert(sourceValue.getOriginal()),
                      sourceValue.isChanged());
            }
          }
        } else {
          for (Field<?> targetField : target.fields()) {
            Field<?> sourceField = field(targetField);

            if (sourceField != null) {
              Utils.setValue(target, targetField, source, sourceField);
            }
          }
        }

        return target;
      }

      // All reflection exceptions are intercepted
      catch (Exception e) {
        throw new MappingException("An error ocurred when mapping record to " + target, e);
      }
    }
Exemplo n.º 4
0
  @Override
  public final Result<R> intern(int... fieldIndexes) {
    for (int fieldIndex : fieldIndexes) {
      if (fields.fields[fieldIndex].getType() == String.class) {
        for (Record record : this) {
          ((AbstractRecord) record).getValue0(fieldIndex).intern();
        }
      }
    }

    return this;
  }
Exemplo n.º 5
0
      @Override
      public AbstractRecord operate(AbstractRecord record) throws SQLException {
        ctx.record(record);
        listener.recordStart(ctx);

        for (int i = 0; i < fields.length; i++) {
          setValue(record, fields[i], i);

          if (intern[i]) {
            record.getValue0(i).intern();
          }
        }

        ctx.record(record);
        listener.recordEnd(ctx);

        return record;
      }
Exemplo n.º 6
0
 final void setValues(Field<?>[] fields, AbstractRecord record) {
   for (Field<?> field : fields) {
     setValue(field, record.getValue0(field));
   }
 }
Exemplo n.º 7
0
 /** Utility method to prevent unnecessary unchecked conversions */
 private final <T> void setValue(AbstractRecord record, Field<T> field, int index)
     throws SQLException {
   T value = Utils.getFromResultSet(ctx, field, index + 1);
   record.setValue(index, new Value<T>(value));
 }