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

org.postgresql.adba.operations.PgOutOperation Maven / Gradle / Ivy

package org.postgresql.adba.operations;

import jdk.incubator.sql2.OutOperation;
import jdk.incubator.sql2.Result;
import jdk.incubator.sql2.SqlType;
import jdk.incubator.sql2.Submission;
import org.postgresql.adba.PgSession;
import org.postgresql.adba.PgSubmission;
import org.postgresql.adba.operations.helpers.FutureQueryParameter;
import org.postgresql.adba.operations.helpers.ParameterHolder;
import org.postgresql.adba.operations.helpers.ValueQueryParameter;
import org.postgresql.adba.submissions.GroupSubmission;
import org.postgresql.adba.submissions.OutSubmission;

import java.time.Duration;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletionStage;
import java.util.function.Consumer;
import java.util.function.Function;

public class PgOutOperation implements OutOperation {
  private final PgSession connection;
  private final String sql;
  private ParameterHolder holder;
  private Consumer errorHandler;
  private Function processor;
  private Map outParameterTypes;
  private GroupSubmission groupSubmission;

  /**
   * Creates a OutOperation, an operation that returns the out parameters from the query.
   * @param connection connection that the query should be part of
   * @param sql the query
   * @param groupSubmission the group that this execution should be part of
   */
  public PgOutOperation(PgSession connection, String sql, GroupSubmission groupSubmission) {
    this.connection = connection;
    this.sql = sql;
    this.holder = new ParameterHolder();
    this.outParameterTypes = new HashMap<>();
    this.groupSubmission = groupSubmission;
  }

  @Override
  public OutOperation outParameter(String id, SqlType type) {
    outParameterTypes.put(id, type);
    holder.add(id, new ValueQueryParameter(null, type));
    return this;
  }

  @Override
  public OutOperation apply(Function processor) {
    this.processor = processor;
    return this;
  }

  @Override
  public OutOperation onError(Consumer errorHandler) {
    if (this.errorHandler != null) {
      throw new IllegalStateException("you are not allowed to call onError multiple times");
    }

    this.errorHandler = errorHandler;
    return this;
  }

  @Override
  public OutOperation set(String id, Object value) {
    holder.add(id, new ValueQueryParameter(value));
    return this;
  }

  @Override
  public OutOperation set(String id, Object value, SqlType type) {
    holder.add(id, new ValueQueryParameter(value, type));
    return this;
  }

  @Override
  public OutOperation set(String id, CompletionStage source) {
    holder.add(id, new FutureQueryParameter(source));
    return this;
  }

  @Override
  public OutOperation set(String id, CompletionStage source, SqlType type) {
    holder.add(id, new FutureQueryParameter(source, type));
    return this;
  }

  @Override
  public OutOperation timeout(Duration minTime) {
    return this;
  }

  @Override
  public Submission submit() {
    PgSubmission submission = new OutSubmission<>(this::cancel, errorHandler, sql, outParameterTypes, processor,
        groupSubmission, holder);
    connection.submit(submission);
    return submission;
  }

  private boolean cancel() {
    // todo set life cycle to canceled
    return true;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy