Search in sources :

Example 1 with TableField

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;
}
Also used : Field(org.jooq.Field) TableField(org.jooq.TableField) TableField(org.jooq.TableField)

Example 2 with TableField

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;
}
Also used : Field(org.jooq.Field) TableField(org.jooq.TableField) BigInteger(java.math.BigInteger) Timestamp(java.sql.Timestamp)

Example 3 with TableField

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);
    }
}
Also used : Schema(org.jooq.Schema) TableRecord(org.jooq.TableRecord) UpdatableRecord(org.jooq.UpdatableRecord) Record(org.jooq.Record) IOException(org.jooq.exception.IOException) TableField(org.jooq.TableField)

Example 4 with TableField

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 "";
}
Also used : Configuration(org.jooq.Configuration) Schema(org.jooq.Schema) TableField(org.jooq.TableField) AttachableInternal(org.jooq.AttachableInternal)

Example 5 with TableField

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;
}
Also used : UpdatableRecord(org.jooq.UpdatableRecord) Record(org.jooq.Record) TableField(org.jooq.TableField)

Aggregations

TableField (org.jooq.TableField)10 Record (org.jooq.Record)7 Schema (org.jooq.Schema)6 UpdatableRecord (org.jooq.UpdatableRecord)5 TableRecord (org.jooq.TableRecord)4 ArrayList (java.util.ArrayList)2 Field (org.jooq.Field)2 IOException (org.jooq.exception.IOException)2 BigInteger (java.math.BigInteger)1 Connection (java.sql.Connection)1 SQLException (java.sql.SQLException)1 Timestamp (java.sql.Timestamp)1 Collections.unmodifiableList (java.util.Collections.unmodifiableList)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 AttachableInternal (org.jooq.AttachableInternal)1