예제 #1
0
  private final <T> void setValue(int index, Field<T> field, T value) {
    Value<T> val = getValue0(index);
    UniqueKey<?> key = getPrimaryKey();

    // Normal fields' changed flag is always set to true
    if (key == null || !key.getFields().contains(field)) {
      val.setValue(value);
    }

    // The primary key's changed flag might've been set previously
    else if (val.isChanged()) {
      val.setValue(value);
    }

    // [#2764] Users may override updatability of primary key values
    else if (updatablePrimaryKeys(settings(this))) {
      val.setValue(value);
    }

    // [#2698] If the primary key has not yet been set
    else if (val.getOriginal() == null) {
      val.setValue(value);
    }

    // [#979] If the primary key is being changed, all other fields' flags
    // need to be set to true for in case this record is stored again, an
    // INSERT statement will thus be issued
    else {
      val.setValue(value, true);

      if (val.isChanged()) {
        changed(true);
      }
    }
  }
예제 #2
0
 @Override
 public final Result<O> fetchParents(Collection<? extends R> records) {
   if (records == null || records.size() == 0) {
     return new ResultImpl<O>(new DefaultConfiguration(), key.getFields());
   } else {
     return fetch(records, key.getTable(), key.getFieldsArray(), getFieldsArray());
   }
 }
예제 #3
0
  @SuppressWarnings("unchecked")
  public static <T extends UpdatableRecord<?>> T findById(
      DSLContext context, Class<T> clz, Object id) {
    if (id == null) return null;

    Table<?> table = getTableFromRecordClass(clz);
    if (table == null) return null;

    UniqueKey<?> key = table.getPrimaryKey();
    if (key == null || key.getFieldsArray().length != 1) return null;

    TableField<?, Object> keyField = (TableField<?, Object>) key.getFieldsArray()[0];

    /* Convert object because we are abusing type safety here */
    Object converted = keyField.getDataType().convert(id);

    return (T) context.selectFrom(table).where(keyField.eq(converted)).fetchOne();
  }