All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.vertx.up.uca.jooq.ActionUpdate Maven / Gradle / Ivy

The newest version!
package io.vertx.up.uca.jooq;

import io.horizon.eon.VValue;
import io.vertx.core.Future;
import io.vertx.core.json.JsonObject;
import org.jooq.Query;
import org.jooq.UpdateConditionStep;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * @author Lang
 */
@SuppressWarnings("all")
class ActionUpdate extends AbstractAction {

    private transient ActionFetch fetch;

    ActionUpdate(final JqAnalyzer analyzer) {
        super(analyzer);
        // Qr
        this.fetch = new ActionFetch(analyzer);
    }

    // ============ UPDATE Operation =============

    /* Future */
     Future updateAsync(final T entity) {
        Objects.requireNonNull(entity);
        return ((Future) this.dao().update(entity)).compose(rows -> {
            this.logging("[ Jq ] updateAsync(T) executed rows: {0}", String.valueOf(rows));
            return Future.succeededFuture(entity);
        });
    }

    /* T */
     T update(final T entity) {
        Objects.requireNonNull(entity);
        final UpdateConditionStep updateStep = this.editRecord(entity);
        final int rows = updateStep.execute();
        this.logging("[ Jq ] update(T) executed rows: {0}", String.valueOf(rows));
        return entity;
    }

    /* Future> */
     Future> updateAsync(final List list) {
        return this.dsl.executeBlocking(h -> h.complete(this.update(list)));
    }

    /* T */
     List update(final List list) {
        Objects.requireNonNull(list);
        if (list.isEmpty()) {
            this.logging("[ Jq ] update(List) executed empty: 0");
            return list;
        }
        final List batchOps = new ArrayList<>();
        list.stream().map(this::editRecord).forEach(batchOps::add);
        final int rows[] = this.context().batch(batchOps).execute();
        final long updated = Arrays.stream(rows).filter(value -> VValue.ONE == value).count();
        this.logging("[ Jq ] update(List) executed rows: {0}/{1}",
            String.valueOf(updated), String.valueOf(rows.length));
        return list;
    }

    /* Future */
     Future updateAsync(final ID id, final T updated) {
        return this.fetch.fetchByIdAsync(id).compose(previous -> {
            final T combine = this.analyzer.copyEntity((T) previous, updated);
            return this.updateAsync(combine);
        });
    }

    /* T */
     T update(final ID id, final T updated) {
        final T previous = this.fetch.fetchById(id);
        final T combine = this.analyzer.copyEntity((T) previous, updated);
        return this.update(combine);
    }

    /* Future */
     Future updateAsync(final JsonObject criteria, final T updated) {
        return this.fetch.fetchOneAsync(criteria).compose(previous -> {
            final T combine = this.analyzer.copyEntity((T) previous, updated);
            return this.updateAsync(combine);
        });
    }

    /* T */
     T update(final JsonObject criteria, final T updated) {
        final T previous = this.fetch.fetchOne(criteria);
        final T combine = this.analyzer.copyEntity((T) previous, updated);
        return this.update(combine);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy