@SuppressWarnings("unchecked") protected Condition equalKeys(Collection<T> ids){ UniqueKey<?> uk = getTable().getPrimaryKey(); Objects.requireNonNull(uk,()->"No primary key"); /** * Copied from jOOQs DAOImpl#equal-method */ TableField<? extends Record, ?>[] pk = uk.getFieldsArray(); Condition condition; if (pk.length == 1) { if (ids.size() == 1) { condition = equalKey(ids.iterator().next()); }else { condition = pk[0].in(pk[0].getDataType().convert(ids)); } }else { condition = row(pk).in(ids.toArray(new Record[ids.size()])); } return condition; }
@SuppressWarnings("unchecked") protected Condition equalKeys(Collection<T> ids){ UniqueKey<?> uk = getTable().getPrimaryKey(); Objects.requireNonNull(uk,()->"No primary key"); /** * Copied from jOOQs DAOImpl#equal-method */ TableField<? extends Record, ?>[] pk = uk.getFieldsArray(); Condition condition; if (pk.length == 1) { if (ids.size() == 1) { condition = equalKey(ids.iterator().next()); }else { condition = pk[0].in(pk[0].getDataType().convert(ids)); } }else { condition = row(pk).in(ids.toArray(new Record[ids.size()])); } return condition; }
@SuppressWarnings("unchecked") public static <R extends UpdatableRecord<R>,T,F> F applyConditionally(T id, Table<R> table, Function<Condition, F> function){ UniqueKey<?> uk = table.getPrimaryKey(); Objects.requireNonNull(uk,()->"No primary key"); /** * Copied from jOOQs DAOImpl#equal-method */ TableField<? extends Record, ?>[] pk = uk.getFieldsArray(); Condition condition; if (pk.length == 1) { condition = ((Field<Object>) pk[0]).equal(pk[0].getDataType().convert(id)); } else { condition = row(pk).equal((Record) id); } return function.apply(condition); }
/** * Set a generated version and timestamp value onto this record after * successfully storing the record. */ final void setRecordVersionAndTimestamp(BigInteger version, Timestamp timestamp) { if (version != null) { TableField<R, ?> field = getTable().getRecordVersion(); int fieldIndex = indexOrFail(fieldsRow(), field); Object value = field.getDataType().convert(version); values[fieldIndex] = value; originals[fieldIndex] = value; changed.clear(fieldIndex); } if (timestamp != null) { TableField<R, ?> field = getTable().getRecordTimestamp(); int fieldIndex = indexOrFail(fieldsRow(), field); Object value = field.getDataType().convert(timestamp); values[fieldIndex] = value; originals[fieldIndex] = value; changed.clear(fieldIndex); } }
/** * Set a generated version and timestamp value onto this record after * successfully storing the record. */ private final void setRecordVersionAndTimestamp(BigInteger version, Timestamp timestamp) { if (version != null) { TableField<R, ?> field = getTable().getRecordVersion(); setValue(field, new Value<Object>(field.getDataType().convert(version))); } if (timestamp != null) { TableField<R, ?> field = getTable().getRecordTimestamp(); setValue(field, new Value<Object>(field.getDataType().convert(timestamp))); } } }
@SuppressWarnings("unchecked") protected /* non-final */ T compositeKeyRecord(Object... values) { UniqueKey<R> key = table.getPrimaryKey(); if (key == null) return null; TableField<R, Object>[] fields = (TableField<R, Object>[]) key.getFieldsArray(); Record result = DSL.using(configuration) .newRecord(fields); for (int i = 0; i < values.length; i++) result.set(fields[i], fields[i].getDataType().convert(values[i])); return (T) result; }
@SuppressWarnings("unchecked") protected Condition equalKey(T id){ UniqueKey<?> uk = getTable().getPrimaryKey(); Objects.requireNonNull(uk,()->"No primary key"); /** * Copied from jOOQs DAOImpl#equal-method */ TableField<? extends Record, ?>[] pk = uk.getFieldsArray(); Condition condition; if (pk.length == 1) { condition = ((Field<Object>) pk[0]).equal(pk[0].getDataType().convert(id)); } else { condition = row(pk).equal((Record) id); } return condition; }
@SuppressWarnings("unchecked") protected Condition equalKey(T id){ UniqueKey<?> uk = getTable().getPrimaryKey(); Objects.requireNonNull(uk,()->"No primary key"); /** * Copied from jOOQs DAOImpl#equal-method */ TableField<? extends Record, ?>[] pk = uk.getFieldsArray(); Condition condition; if (pk.length == 1) { condition = ((Field<Object>) pk[0]).equal(pk[0].getDataType().convert(id)); } else { condition = row(pk).equal((Record) id); } return condition; }
@SuppressWarnings("unchecked") protected /* non-final */ T compositeKeyRecord(Object... values) { UniqueKey<R> key = table.getPrimaryKey(); if (key == null) return null; TableField<R, Object>[] fields = (TableField<R, Object>[]) key.getFieldsArray(); Record result = DSL.using(queryExecutor.configuration()) .newRecord(fields); for (int i = 0; i < values.length; i++) result.set(fields[i], fields[i].getDataType().convert(values[i])); return (T) result; }
@SuppressWarnings("unchecked") protected /* non-final */ T compositeKeyRecord(Object... values) { UniqueKey<R> key = table.getPrimaryKey(); if (key == null) return null; TableField<R, Object>[] fields = (TableField<R, Object>[]) key.getFieldsArray(); Record result = DSL.using(queryExecutor.configuration()) .newRecord(fields); for (int i = 0; i < values.length; i++) result.set(fields[i], fields[i].getDataType().convert(values[i])); return (T) result; }
protected void register(ForeignKey<?, ?> foreignKey) { TableField<?, ?>[] fields = foreignKey.getFieldsArray(); if (fields.length == 0 || fields.length > 1) { return; } TableField<?, ?> field = fields[0]; if (!field.getDataType().isNumeric()) { return; } String propertyName = getNameFromField(field.getTable().getRecordType(), field.getName()); String referenceName = propertyName; if (field.getName().endsWith(ID_FIELD)) { referenceName = referenceName.substring(0, referenceName.length() - 2); } Class<?> localType = foreignKey.getTable().getRecordType(); Class<?> foreignType = foreignKey.getKey().getTable().getRecordType(); Schema localSchema = schemaFactory.getSchema(localType); String childName = localSchema.getPluralName(); String childNameOverride = ArchaiusUtil.getString( String.format("object.link.name.%s.%s.override", localType.getSimpleName(), propertyName).toLowerCase()).get(); if (childNameOverride != null) { childName = childNameOverride; } register(localType, new ForeignKeyRelationship(REFERENCE, referenceName, propertyName, foreignType, foreignKey)); register(foreignType, new ForeignKeyRelationship(CHILD, childName, propertyName, localType, foreignKey)); }
@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(); }
private final int store0(Field<?>[] storeFields) { TableField<R, ?>[] keys = getPrimaryKey().getFieldsArray(); boolean executeUpdate = false; // [#2764] If primary key values are allowed to be changed, // inserting is only possible without prior loading of pk values if (updatablePrimaryKeys(settings(this))) { executeUpdate = fetched; } else { for (TableField<R, ?> field : keys) { // If any primary key value is null or changed if (changed(field) || // [#3237] or if a NOT NULL primary key value is null, then execute an INSERT (field.getDataType().nullable() == false && get(field) == null)) { executeUpdate = false; break; } // Otherwise, updates are possible executeUpdate = true; } } int result = 0; if (executeUpdate) { result = storeUpdate(storeFields, keys); } else { result = storeInsert(storeFields); } return result; }
field("({0})::information_schema.sql_identifier", col.COLUMN_NAME.getDataType(), a.ATTNAME).as(col.COLUMN_NAME), field("({0})::information_schema.cardinal_number", col.ORDINAL_POSITION.getDataType(), a.ATTNUM).as(col.ORDINAL_POSITION), field("({0})::information_schema.character_data", col.DATA_TYPE.getDataType(), when(t.TYPTYPE.eq(inline("d")), when(bt.TYPELEM.ne(inline(0L)).and(bt.TYPLEN.eq(inline((short) -1))), inline("ARRAY")) .when(nt.NSPNAME.eq(inline("pg_catalog")), field("format_type({0}, NULL::integer)", String.class, a.ATTTYPID)) .otherwise(inline("USER-DEFINED")))).as(col.DATA_TYPE), field("(information_schema._pg_char_max_length(information_schema._pg_truetypid(a.*, t.*), information_schema._pg_truetypmod(a.*, t.*)))::information_schema.cardinal_number", col.CHARACTER_MAXIMUM_LENGTH.getDataType()).as(col.CHARACTER_MAXIMUM_LENGTH), field("(information_schema._pg_numeric_precision(information_schema._pg_truetypid(a.*, t.*), information_schema._pg_truetypmod(a.*, t.*)))::information_schema.cardinal_number", col.NUMERIC_PRECISION.getDataType()).as(col.NUMERIC_PRECISION), field("(information_schema._pg_numeric_scale(information_schema._pg_truetypid(a.*, t.*), information_schema._pg_truetypmod(a.*, t.*)))::information_schema.cardinal_number", col.NUMERIC_SCALE.getDataType()).as(col.NUMERIC_SCALE), field("({0})::information_schema.yes_or_no", col.IS_NULLABLE.getDataType(), when(condition(a.ATTNOTNULL).or(t.TYPTYPE.eq(inline("d")).and(t.TYPNOTNULL)), inline("NO")) .otherwise(inline("YES"))).as(col.IS_NULLABLE), field("(pg_get_expr({0}, {1}))::information_schema.character_data", col.COLUMN_DEFAULT.getDataType(), ad.ADBIN, ad.ADRELID).as(col.COLUMN_DEFAULT), field("({0})::information_schema.sql_identifier", col.UDT_SCHEMA.getDataType(), nvl(nbt.NSPNAME, nt.NSPNAME)).as(col.UDT_SCHEMA), field("({0})::information_schema.sql_identifier", col.UDT_NAME.getDataType(), nvl(bt.TYPNAME, t.TYPNAME)).as(col.UDT_NAME), PG_DESCRIPTION.DESCRIPTION)