/** * Turn a {@link Record} into a {@link Map} */ static final Map<Field<?>, Object> map(Record record) { Map<Field<?>, Object> result = new LinkedHashMap<Field<?>, Object>(); int size = record.size(); for (int i = 0; i < size; i++) { result.put(record.field(i), record.getValue(i)); } return result; }
@SuppressWarnings("unchecked") protected Function<Object, T> keyConverter() { return record->{ Objects.requireNonNull(record, () -> "Failed inserting record or no key"); Record key1 = ((R)record).key(); if(key1.size() == 1){ return ((Record1<T>)key1).value1(); } return (T) key1; }; }
@SuppressWarnings("unchecked") protected Function<Object, T> keyConverter() { return record->{ Objects.requireNonNull(record, () -> "Failed inserting record or no key"); Record key1 = ((R)record).key(); if(key1.size() == 1){ return ((Record1<T>)key1).value1(); } return (T) key1; }; }
/** * Turn a {@link Record} into a {@link Map} */ static final Map<Field<?>, Object> mapOfChangedValues(Record record) { Map<Field<?>, Object> result = new LinkedHashMap<Field<?>, Object>(); int size = record.size(); for (int i = 0; i < size; i++) if (record.changed(i)) result.put(record.field(i), record.get(i)); return result; }
/** * Create a PostgreSQL string representation of a record. */ public static String toPGString(Record r) { StringBuilder sb = new StringBuilder(); sb.append("("); String separator = ""; for (int i = 0; i < r.size(); i++) { @SuppressWarnings({ "unchecked", "rawtypes" }) Object a = ((Converter) r.field(i).getConverter()).to(r.get(i)); sb.append(separator); // [#753] null must not be set as a literal if (a != null) { if (a instanceof byte[]) sb.append(toPGString((byte[]) a)); else sb.append("\"") .append(StringUtils.replace(StringUtils.replace(toPGString(a), "\\", "\\\\"), "\"", "\\\"")) .append("\""); } separator = ","; } sb.append(")"); return sb.toString(); }
throw new NullPointerException(); if (size() != that.size()) { throw new ClassCastException(String.format("Trying to compare incomparable records (wrong degree):\n%s\n%s", this, that));
throw new NullPointerException(); if (size() != that.size()) { throw new ClassCastException(String.format("Trying to compare incomparable records (wrong degree):\n%s\n%s", this, that));
@SuppressWarnings({ "unchecked", "rawtypes" }) @Override public final void setRecord(R record) { for (int i = 0; i < record.size(); i++) if (record.changed(i)) addValue((Field) record.field(i), record.get(i)); }
@SuppressWarnings({ "unchecked", "rawtypes" }) @Override public final void setRecord(R record) { for (int i = 0; i < record.size(); i++) { if (record.changed(i)) { addValue((Field) record.field(i), record.getValue(i)); } } }
/** * [#2700] [#3582] If a POJO attribute is NULL, but the column is NOT NULL * then we should let the database apply DEFAULT values */ static final void resetChangedOnNotNull(Record record) { int size = record.size(); for (int i = 0; i < size; i++) if (record.get(i) == null) if (!record.field(i).getDataType().nullable()) record.changed(i, false); }
/** * [#2700] [#3582] If a POJO attribute is NULL, but the column is NOT NULL * then we should let the database apply DEFAULT values */ private static final void resetChangedOnNotNull(Record record) { int size = record.size(); for (int i = 0; i < size; i++) if (record.get(i) == null) if (!record.field(i).getDataType().nullable()) record.changed(i, false); } }
@SuppressWarnings({ "unchecked", "rawtypes" }) @Override public void accept(Context<?> ctx) { ConditionProviderImpl condition = new ConditionProviderImpl(); int size = record.size(); for (int i = 0; i < size; i++) { Object value = record.get(i); if (value != null) { Field f1 = record.field(i); Field f2 = DSL.val(value, f1.getDataType()); condition.addConditions(f1.eq(f2)); } } ctx.visit(condition); } }
/** * Defaults fields that have a default value and are nullable. * @param record the record * @return the record */ private Record setDefault(Record record) { int size = record.size(); for (int i = 0; i < size; i++) if (record.get(i) == null) { @SuppressWarnings("unchecked") Field<Object> field = (Field<Object>) record.field(i); if (!field.getDataType().nullable() && !field.getDataType().identity()) record.set(field, DSL.defaultValue()); } return record; }
/** * Defaults fields that have a default value and are nullable. * @param record the record * @return the record */ private Record setDefault(Record record) { int size = record.size(); for (int i = 0; i < size; i++) if (record.get(i) == null) { @SuppressWarnings("unchecked") Field<Object> field = (Field<Object>) record.field(i); if (!field.getDataType().nullable() && !field.getDataType().identity()) record.set(field, DSL.defaultValue()); } return record; }