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

io.vertx.sqlclient.impl.QueryExecutor Maven / Gradle / Ivy

There is a newer version: 5.0.0.CR1
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.vertx.sqlclient.impl;

import io.vertx.core.impl.ContextInternal;
import io.vertx.core.impl.future.PromiseInternal;
import io.vertx.sqlclient.PrepareOptions;
import io.vertx.sqlclient.Row;
import io.vertx.sqlclient.SqlResult;
import io.vertx.sqlclient.Tuple;
import io.vertx.sqlclient.impl.command.CommandScheduler;
import io.vertx.sqlclient.impl.command.ExtendedQueryCommand;
import io.vertx.sqlclient.impl.command.SimpleQueryCommand;

import java.util.List;
import java.util.function.Function;
import java.util.stream.Collector;

/**
 * Executes query.
 */
class QueryExecutor, L extends SqlResult> {

  private final Function factory;
  private final Collector collector;

  public QueryExecutor(Function factory,
                       Collector collector) {
    this.factory = factory;
    this.collector = collector;
  }

  private QueryResultBuilder createHandler(PromiseInternal promise) {
    return new QueryResultBuilder<>(factory, promise);
  }

  void executeSimpleQuery(CommandScheduler scheduler,
                          String sql,
                          boolean autoCommit,
                          boolean singleton,
                          PromiseInternal promise) {
    ContextInternal context = promise.context();
    QueryResultBuilder handler = createHandler(promise);
    scheduler.schedule(context, new SimpleQueryCommand<>(sql, singleton, autoCommit, collector, handler)).onComplete(handler);
  }

  QueryResultBuilder executeExtendedQuery(CommandScheduler scheduler,
                                                   PreparedStatement preparedStatement,
                                                   PrepareOptions options,
                                                   boolean autoCommit,
                                                   Tuple arguments,
                                                   int fetch,
                                                   String cursorId,
                                                   boolean suspended,
                                                   PromiseInternal promise) {
    ContextInternal context = promise.context();
    QueryResultBuilder handler = createHandler(promise);
    String msg = preparedStatement.prepare((TupleInternal) arguments);
    if (msg != null) {
      handler.fail(msg);
      return null;
    }
    ExtendedQueryCommand cmd = ExtendedQueryCommand.createQuery(
      preparedStatement.sql(),
      options,
      preparedStatement,
      arguments,
      fetch,
      cursorId,
      suspended,
      autoCommit,
      collector,
      handler);
    scheduler.schedule(context, cmd).onComplete(handler);
    return handler;
  }

  void executeExtendedQuery(CommandScheduler scheduler, String sql, PrepareOptions options, boolean autoCommit, Tuple arguments, PromiseInternal promise) {
    ContextInternal context = (ContextInternal) promise.context();
    QueryResultBuilder handler = this.createHandler(promise);
    ExtendedQueryCommand cmd = createExtendedQueryCommand(sql, options, autoCommit, arguments, handler);
    scheduler.schedule(context, cmd).onComplete(handler);
  }

  private ExtendedQueryCommand createExtendedQueryCommand(String sql,
                                                             PrepareOptions options,
                                                             boolean autoCommit,
                                                             Tuple tuple,
                                                             QueryResultBuilder handler) {
    return ExtendedQueryCommand.createQuery(
      sql,
      options,
      null,
      tuple,
      autoCommit,
      collector,
      handler);
  }

  void executeBatchQuery(CommandScheduler scheduler,
                         PrepareOptions options,
                         PreparedStatement preparedStatement,
                         boolean autoCommit,
                         List batch,
                         PromiseInternal promise) {
    ContextInternal context = promise.context();
    QueryResultBuilder handler = createHandler(promise);
    for  (Tuple args : batch) {
      String msg = preparedStatement.prepare((TupleInternal)args);
      if (msg != null) {
        handler.fail(msg);
        return;
      }
    }
    ExtendedQueryCommand cmd = ExtendedQueryCommand.createBatch(preparedStatement.sql(), options, preparedStatement, batch, autoCommit, collector, handler);
    scheduler.schedule(context, cmd).onComplete(handler);
  }

  void executeBatchQuery(CommandScheduler scheduler, String sql, PrepareOptions options, boolean autoCommit, List batch, PromiseInternal promise) {
    ContextInternal context = promise.context();
    QueryResultBuilder handler = createHandler(promise);
    ExtendedQueryCommand cmd = createBatchQueryCommand(sql, options, autoCommit, batch, handler);
    scheduler.schedule(context, cmd).onComplete(handler);
  }

  private ExtendedQueryCommand createBatchQueryCommand(String sql,
                                                          PrepareOptions options,
                                                          boolean autoCommit,
                                                          List argsList,
                                                          QueryResultBuilder handler) {
    return ExtendedQueryCommand.createBatch(sql, options, null, argsList, autoCommit, collector, handler);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy