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

io.reactiverse.pgclient.impl.PgPreparedQueryImpl Maven / Gradle / Ivy

There is a newer version: 0.11.4
Show newest version
/*
 * Copyright (C) 2017 Julien Viet
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package io.reactiverse.pgclient.impl;

import io.reactiverse.pgclient.*;
import io.vertx.core.*;

import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Function;
import java.util.stream.Collector;

/**
 * @author Julien Viet
 */
class PgPreparedQueryImpl implements PgPreparedQuery {

  private final Connection conn;
  private final Context context;
  private final PreparedStatement ps;
  private final AtomicBoolean closed = new AtomicBoolean();

  PgPreparedQueryImpl(Connection conn, Context context, PreparedStatement ps) {
    this.conn = conn;
    this.context = context;
    this.ps = ps;
  }

  @Override
  public PgPreparedQuery execute(Tuple args, Handler> handler) {
    return execute(args, false, PgRowSetImpl.FACTORY, PgRowSetImpl.COLLECTOR, handler);
  }

  @Override
  public  PgPreparedQuery execute(Tuple args, Collector collector, Handler>> handler) {
    return execute(args, true, PgResultImpl::new, collector, handler);
  }

  private , R3 extends PgResult> PgPreparedQuery execute(
    Tuple args,
    boolean singleton,
    Function factory,
    Collector collector,
    Handler> handler) {
    PgResultBuilder b = new PgResultBuilder<>(factory, handler);
    return execute(args, 0, null, false, singleton, collector, b, b);
  }

   PgPreparedQuery execute(Tuple args,
                                 int fetch,
                                 String portal,
                                 boolean suspended,
                                 boolean singleton,
                                 Collector collector,
                                 QueryResultHandler resultHandler,
                                 Handler> handler) {
    if (context == Vertx.currentContext()) {
      String msg = ps.prepare((List) args);
      if (msg != null) {
        handler.handle(Future.failedFuture(msg));
      } else {
        conn.schedule(new ExtendedQueryCommand<>(
          ps,
          args,
          fetch,
          portal,
          suspended,
          singleton,
          collector,
          resultHandler,
          handler));
      }
    } else {
      context.runOnContext(v -> execute(args, fetch, portal, suspended, singleton, collector, resultHandler, handler));
    }
    return this;
  }

  @Override
  public PgCursor cursor(Tuple args) {
    String msg = ps.prepare((List) args);
    if (msg != null) {
      throw new IllegalArgumentException(msg);
    }
    return new PgCursorImpl(this, args);
  }

  @Override
  public void close() {
    close(ar -> {
    });
  }

  public PgPreparedQuery batch(List argsList, Handler> handler) {
    return batch(argsList, false, PgRowSetImpl.FACTORY, PgRowSetImpl.COLLECTOR, handler);
  }

  @Override
  public  PgPreparedQuery batch(List argsList, Collector collector, Handler>> handler) {
    return batch(argsList, true, PgResultImpl::new, collector, handler);
  }

  private , R3 extends PgResult> PgPreparedQuery batch(
    List argsList,
    boolean singleton,
    Function factory,
    Collector collector,
    Handler> handler) {
    for  (Tuple args : argsList) {
      String msg = ps.prepare((List) args);
      if (msg != null) {
        handler.handle(Future.failedFuture(msg));
        return this;
      }
    }
    PgResultBuilder b = new PgResultBuilder<>(factory, handler);
    conn.schedule(new ExtendedBatchQueryCommand<>(ps, argsList.iterator(), singleton, collector, b, b));
    return this;
  }

  @Override
  public PgStream createStream(int fetch, Tuple args) {
    return new PgStreamImpl(this, fetch, args);
  }

  @Override
  public void close(Handler> completionHandler) {
    if (closed.compareAndSet(false, true)) {
      conn.schedule(new CloseStatementCommand(completionHandler));
    } else {
      completionHandler.handle(Future.failedFuture("Already closed"));
    }
  }

  void closePortal(String portal, Handler> handler) {
    conn.schedule(new ClosePortalCommand(portal, handler));
  }

}