Search in sources :

Example 1 with Settable

use of io.requery.proxy.Settable in project requery by requery.

the class EntityWriter method batchInsert.

GeneratedKeys<E> batchInsert(Iterable<E> entities, boolean returnKeys) {
    // true if using JDBC batching
    final boolean batchInStatement = canBatchInStatement();
    final int batchSize = context.getBatchUpdateSize();
    final EntityReader<E, S> reader = context.read(entityClass);
    final Iterator<E> iterator = entities.iterator();
    final boolean isImmtuable = type.isImmutable();
    final GeneratedKeys<E> keys = returnKeys && hasGeneratedKey ? new GeneratedKeys<E>() : null;
    int collectionSize = entities instanceof Collection ? ((Collection) entities).size() : -1;
    @SuppressWarnings("unchecked") final E[] elements = (E[]) new Object[Math.min(collectionSize, batchSize)];
    while (iterator.hasNext()) {
        int index = 0;
        Map<Class<? extends S>, List<S>> associations = new HashMap<>();
        while (iterator.hasNext() && index < batchSize) {
            E entity = iterator.next();
            EntityProxy<E> proxy = proxyProvider.apply(entity);
            elements[index] = entity;
            if (hasForeignKeys) {
                for (Attribute<E, ?> attribute : associativeAttributes) {
                    S referenced = foreignKeyReference(proxy, attribute);
                    if (referenced != null) {
                        EntityProxy<S> otherProxy = context.proxyOf(referenced, false);
                        if (otherProxy != null && !otherProxy.isLinked()) {
                            Class<? extends S> key = otherProxy.type().getClassType();
                            List<S> values = associations.get(key);
                            if (values == null) {
                                associations.put(key, values = new ArrayList<>());
                            }
                            values.add(referenced);
                        }
                    }
                }
            }
            incrementVersion(proxy);
            context.getStateListener().preInsert(entity, proxy);
            index++;
        }
        cascadeBatch(associations);
        final int count = index;
        GeneratedResultReader keyReader = null;
        if (hasGeneratedKey) {
            keyReader = new GeneratedResultReader() {

                @Override
                public void read(int index, ResultSet results) throws SQLException {
                    // check if reading batch keys, otherwise read 1
                    int readCount = batchInStatement ? count : 1;
                    for (int i = index; i < index + readCount; i++) {
                        if (!results.next()) {
                            throw new IllegalStateException();
                        }
                        EntityProxy<E> proxy = proxyProvider.apply(elements[i]);
                        Settable<E> keyProxy = keys == null ? proxy : keys.proxy(isImmtuable ? null : proxy);
                        readGeneratedKeys(keyProxy, results);
                    }
                }

                @Override
                public String[] generatedColumns() {
                    return generatedColumnNames;
                }
            };
        }
        BatchUpdateOperation<E> operation = new BatchUpdateOperation<>(context, elements, count, this, keyReader, batchInStatement);
        QueryElement<int[]> query = new QueryElement<>(QueryType.INSERT, model, operation);
        query.from(entityClass);
        for (Attribute attribute : bindableAttributes) {
            query.value((Expression) attribute, null);
        }
        int[] updates = query.get();
        for (int i = 0; i < updates.length; i++) {
            E entity = elements[i];
            EntityProxy<E> proxy = proxyProvider.apply(entity);
            checkRowsAffected(updates[i], entity, proxy);
            proxy.link(reader);
            updateAssociations(Cascade.AUTO, entity, proxy, null);
            context.getStateListener().postInsert(entity, proxy);
            // cache entity
            if (cacheable) {
                cache.put(entityClass, proxy.key(), entity);
            }
        }
    }
    return keys;
}
Also used : UPDATE(io.requery.query.element.QueryType.UPDATE) HashMap(java.util.HashMap) SQLException(java.sql.SQLException) QueryAttribute(io.requery.meta.QueryAttribute) Attribute(io.requery.meta.Attribute) ArrayList(java.util.ArrayList) QueryElement(io.requery.query.element.QueryElement) EntityProxy(io.requery.proxy.EntityProxy) Settable(io.requery.proxy.Settable) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) ObservableCollection(io.requery.util.ObservableCollection) Collection(java.util.Collection)

Aggregations

Attribute (io.requery.meta.Attribute)1 QueryAttribute (io.requery.meta.QueryAttribute)1 EntityProxy (io.requery.proxy.EntityProxy)1 Settable (io.requery.proxy.Settable)1 QueryElement (io.requery.query.element.QueryElement)1 UPDATE (io.requery.query.element.QueryType.UPDATE)1 ObservableCollection (io.requery.util.ObservableCollection)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1