use of org.jooq.TableField in project jOOQ by jOOQ.
the class JoinTable method onKey.
@SuppressWarnings({ "unchecked", "rawtypes" })
private final JoinTable onKey(ForeignKey<?, ?> key, Table<?> fk, Table<?> pk) {
JoinTable result = this;
TableField<?, ?>[] references = key.getFieldsArray();
TableField<?, ?>[] referenced = key.getKey().getFieldsArray();
for (int i = 0; i < references.length; i++) {
Field f1 = fk.field(references[i]);
Field f2 = pk.field(referenced[i]);
// [#2870] TODO: If lhs or rhs are aliased tables, extract the appropriate fields from them
result.and(f1.equal(f2));
}
return result;
}
use of org.jooq.TableField in project jOOQ by jOOQ.
the class UpdatableRecordImpl method storeUpdate0.
private final int storeUpdate0(Field<?>[] storeFields, TableField<R, ?>[] keys) {
UpdateQuery<R> update = create().updateQuery(getTable());
addChangedValues(storeFields, update);
Tools.addConditions(update, this, keys);
// Don't store records if no value was set by client code
if (!update.isExecutable()) {
if (log.isDebugEnabled())
log.debug("Query is not executable", update);
return 0;
}
// [#1596] Set timestamp and/or version columns to appropriate values
BigInteger version = addRecordVersion(update);
Timestamp timestamp = addRecordTimestamp(update);
if (isExecuteWithOptimisticLocking()) {
// [#1596] Add additional conditions for version and/or timestamp columns
if (isTimestampOrVersionAvailable()) {
addConditionForVersionAndTimestamp(update);
} else // [#5384] Do this only if the exclusion flag for unversioned records is off
if (isExecuteWithOptimisticLockingIncludeUnversioned()) {
checkIfChanged(keys);
}
}
// [#1596] Check if the record was really changed in the database
// [#1859] Specify the returning clause if needed
Collection<Field<?>> key = setReturningIfNeeded(update);
int result = update.execute();
checkIfChanged(result, version, timestamp);
if (result > 0) {
for (Field<?> storeField : storeFields) changed(storeField, false);
// [#1859] If an update was successful try fetching the generated
getReturningIfNeeded(update, key);
}
return result;
}
use of org.jooq.TableField in project jOOQ by jOOQ.
the class ResultImpl method formatJSON.
@Override
public final void formatJSON(Writer writer, JSONFormat format) {
try {
String separator;
int recordLevel = format.header() ? 2 : 1;
if (format.header()) {
if (format.format())
writer.append('{').append(format.newline()).append(format.indentString(1)).append("\"fields\": [");
else
writer.append("{\"fields\":[");
separator = "";
for (Field<?> field : fields.fields) {
writer.append(separator);
if (format.format())
writer.append(format.newline()).append(format.indentString(2));
writer.append('{');
if (format.format())
writer.append(format.newline()).append(format.indentString(3));
if (field instanceof TableField) {
Table<?> table = ((TableField<?, ?>) field).getTable();
if (table != null) {
Schema schema = table.getSchema();
if (schema != null) {
writer.append("\"schema\":");
if (format.format())
writer.append(' ');
JSONValue.writeJSONString(schema.getName(), writer);
writer.append(',');
if (format.format())
writer.append(format.newline()).append(format.indentString(3));
}
writer.append("\"table\":");
if (format.format())
writer.append(' ');
JSONValue.writeJSONString(table.getName(), writer);
writer.append(',');
if (format.format())
writer.append(format.newline()).append(format.indentString(3));
}
}
writer.append("\"name\":");
if (format.format())
writer.append(' ');
JSONValue.writeJSONString(field.getName(), writer);
writer.append(',');
if (format.format())
writer.append(format.newline()).append(format.indentString(3));
writer.append("\"type\":");
if (format.format())
writer.append(' ');
JSONValue.writeJSONString(field.getDataType().getTypeName().toUpperCase(), writer);
if (format.format())
writer.append(format.newline()).append(format.indentString(2));
writer.append('}');
separator = ",";
}
if (format.format())
writer.append(format.newline()).append(format.indentString(1)).append("],").append(format.newline()).append(format.indentString(1)).append("\"records\": ");
else
writer.append("],\"records\":");
}
writer.append('[');
separator = "";
switch(format.recordFormat()) {
case ARRAY:
for (Record record : this) {
writer.append(separator);
if (format.format())
writer.append(format.newline());
formatJSONArray0(record, fields, format, recordLevel, writer);
separator = ",";
}
break;
case OBJECT:
for (Record record : this) {
writer.append(separator);
if (format.format())
writer.append(format.newline());
formatJSONMap0(record, fields, format, recordLevel, writer);
separator = ",";
}
break;
default:
throw new IllegalArgumentException("Format not supported: " + format);
}
if (format.format()) {
writer.append(format.newline());
if (format.header())
writer.append(format.indentString(1));
}
writer.append(']');
if (format.header())
writer.append(format.newline()).append('}');
writer.flush();
} catch (java.io.IOException e) {
throw new IOException("Exception while writing JSON", e);
}
}
use of org.jooq.TableField in project jOOQ by jOOQ.
the class MockResultSetMetaData method getSchemaName.
@Override
public String getSchemaName(int column) throws SQLException {
rs.checkNotClosed();
Field<?> field = rs.result.field(column - 1);
if (field instanceof TableField) {
Table<?> table = ((TableField<?, ?>) field).getTable();
if (table != null) {
Schema schema = table.getSchema();
if (schema != null) {
Configuration configuration = ((AttachableInternal) rs.result).configuration();
Schema mapped = null;
if (configuration != null) {
mapped = DSL.using(configuration).map(schema);
}
if (mapped != null) {
return mapped.getName();
} else {
return schema.getName();
}
}
}
}
// By default, no schema is available
return "";
}
use of org.jooq.TableField in project jOOQ by jOOQ.
the class DAOImpl method compositeKeyRecord.
@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;
}
Aggregations