Search in sources :

Example 1 with AttachableInternal

use of org.jooq.AttachableInternal in project jOOQ by jOOQ.

the class DefaultRecordMapper method attach.

private static <E> E attach(E attachable, Record record) {
    // Settings.attachRecords flag is set
    if (attachable instanceof Attachable && record instanceof AttachableInternal) {
        Attachable a = (Attachable) attachable;
        AttachableInternal r = (AttachableInternal) record;
        if (Tools.attachRecords(r.configuration())) {
            a.attach(r.configuration());
        }
    }
    return attachable;
}
Also used : Attachable(org.jooq.Attachable) AttachableInternal(org.jooq.AttachableInternal)

Example 2 with AttachableInternal

use of org.jooq.AttachableInternal in project jOOQ by jOOQ.

the class BatchCRUD method executeStatic.

private final int[] executeStatic() {
    List<Query> queries = new ArrayList<Query>();
    QueryCollector collector = new QueryCollector();
    Configuration local = configuration.derive(Tools.combine(configuration.executeListenerProviders(), new DefaultExecuteListenerProvider(collector)));
    for (int i = 0; i < records.length; i++) {
        Configuration previous = ((AttachableInternal) records[i]).configuration();
        try {
            records[i].attach(local);
            executeAction(i);
        } catch (QueryCollectorSignal e) {
            Query query = e.getQuery();
            if (query.isExecutable()) {
                queries.add(query);
            }
        } finally {
            records[i].attach(previous);
        }
    }
    // Resulting statements can be batch executed in their requested order
    int[] result = create.batch(queries).execute();
    updateChangedFlag();
    return result;
}
Also used : Query(org.jooq.Query) Configuration(org.jooq.Configuration) ArrayList(java.util.ArrayList) AttachableInternal(org.jooq.AttachableInternal)

Example 3 with AttachableInternal

use of org.jooq.AttachableInternal in project jOOQ by jOOQ.

the class BatchCRUD method executePrepared.

private final int[] executePrepared() {
    Map<String, List<Query>> queries = new LinkedHashMap<String, List<Query>>();
    QueryCollector collector = new QueryCollector();
    // Add the QueryCollector to intercept query execution after rendering
    Configuration local = configuration.derive(Tools.combine(configuration.executeListenerProviders(), new DefaultExecuteListenerProvider(collector)));
    // [#1537] Communicate with UpdatableRecordImpl
    local.data(DATA_OMIT_RETURNING_CLAUSE, true);
    // [#1529] Avoid DEBUG logging of single INSERT / UPDATE statements
    local.settings().setExecuteLogging(false);
    for (int i = 0; i < records.length; i++) {
        Configuration previous = ((AttachableInternal) records[i]).configuration();
        try {
            records[i].attach(local);
            executeAction(i);
        } catch (QueryCollectorSignal e) {
            Query query = e.getQuery();
            String sql = e.getSQL();
            // Aggregate executable queries by identical SQL
            if (query.isExecutable()) {
                List<Query> list = queries.get(sql);
                if (list == null) {
                    list = new ArrayList<Query>();
                    queries.put(sql, list);
                }
                list.add(query);
            }
        } finally {
            records[i].attach(previous);
        }
    }
    // Execute one batch statement for each identical SQL statement. Every
    // SQL statement may have several queries with different bind values.
    // The order is preserved as much as possible
    List<Integer> result = new ArrayList<Integer>();
    for (Entry<String, List<Query>> entry : queries.entrySet()) {
        BatchBindStep batch = create.batch(entry.getValue().get(0));
        for (Query query : entry.getValue()) {
            batch.bind(query.getBindValues().toArray());
        }
        int[] array = batch.execute();
        for (int i : array) {
            result.add(i);
        }
    }
    int[] array = new int[result.size()];
    for (int i = 0; i < result.size(); i++) {
        array[i] = result.get(i);
    }
    updateChangedFlag();
    return array;
}
Also used : Query(org.jooq.Query) Configuration(org.jooq.Configuration) BatchBindStep(org.jooq.BatchBindStep) ArrayList(java.util.ArrayList) AttachableInternal(org.jooq.AttachableInternal) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) List(java.util.List)

Example 4 with AttachableInternal

use of org.jooq.AttachableInternal 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 AttachableInternal

use of org.jooq.AttachableInternal in project jOOQ by jOOQ.

the class Mock method result.

/**
     * Wrap a record in a result.
     */
static final Result<?> result(Record data) {
    Configuration configuration = data instanceof AttachableInternal ? ((AttachableInternal) data).configuration() : new DefaultConfiguration();
    Result<Record> result = using(configuration).newResult(data.fields());
    result.add(data);
    return result;
}
Also used : Configuration(org.jooq.Configuration) DefaultConfiguration(org.jooq.impl.DefaultConfiguration) DefaultConfiguration(org.jooq.impl.DefaultConfiguration) Record(org.jooq.Record) AttachableInternal(org.jooq.AttachableInternal)

Aggregations

AttachableInternal (org.jooq.AttachableInternal)5 Configuration (org.jooq.Configuration)4 ArrayList (java.util.ArrayList)2 Query (org.jooq.Query)2 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Attachable (org.jooq.Attachable)1 BatchBindStep (org.jooq.BatchBindStep)1 Record (org.jooq.Record)1 Schema (org.jooq.Schema)1 TableField (org.jooq.TableField)1 DefaultConfiguration (org.jooq.impl.DefaultConfiguration)1