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

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

/*
 * 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.*;
import io.vertx.core.impl.ContextInternal;
import io.vertx.core.impl.future.PromiseInternal;
import io.vertx.core.spi.metrics.ClientMetrics;
import io.vertx.core.spi.tracing.VertxTracer;
import io.vertx.sqlclient.PrepareOptions;
import io.vertx.sqlclient.PreparedStatement;
import io.vertx.sqlclient.SqlConnection;
import io.vertx.sqlclient.Transaction;
import io.vertx.sqlclient.impl.command.CommandBase;
import io.vertx.sqlclient.impl.command.PrepareStatementCommand;
import io.vertx.sqlclient.impl.command.QueryCommandBase;
import io.vertx.sqlclient.impl.pool.SqlConnectionPool;
import io.vertx.sqlclient.impl.tracing.QueryReporter;
import io.vertx.sqlclient.spi.ConnectionFactory;
import io.vertx.sqlclient.spi.DatabaseMetadata;
import io.vertx.sqlclient.spi.Driver;

/**
 * @author Julien Viet
 */
public class SqlConnectionBase> extends SqlClientBase implements SqlConnectionInternal, Closeable {

  private volatile Handler exceptionHandler;
  private volatile Handler closeHandler;
  private volatile boolean closeFactoryAfterUsage;
  protected TransactionImpl tx;
  protected final ContextInternal context;
  protected final ConnectionFactory factory;
  protected final Connection conn;

  public SqlConnectionBase(ContextInternal context, ConnectionFactory factory, Connection conn, Driver driver) {
    super(driver);
    this.context = context;
    this.factory = factory;
    this.conn = conn;
  }

  public ConnectionFactory factory() {
    return factory;
  }

  public Connection unwrap() {
    return conn;
  }

  public C prepare(String sql, PrepareOptions options, Handler> handler) {
    Future fut = prepare(sql, options);
    if (handler != null) {
      fut.onComplete(handler);
    }
    return (C)this;
  }

  public Future prepare(String sql, PrepareOptions options) {
    return schedule(context, new PrepareStatementCommand(sql, options, true))
      .compose(
      cr -> Future.succeededFuture(PreparedStatementImpl.create(conn, context, cr, autoCommit())),
      err -> {
        if (conn.isIndeterminatePreparedStatementError(err)) {
          return Future.succeededFuture(PreparedStatementImpl.create(conn, context, options, sql, autoCommit()));
        } else {
          return Future.failedFuture(err);
        }
      });
  }

  public C prepare(String sql, Handler> handler) {
    return prepare(sql, null, handler);
  }

  public Future prepare(String sql) {
    return prepare(sql, (PrepareOptions) null);
  }

  @Override
  protected ContextInternal context() {
    return context;
  }

  @Override
  protected  PromiseInternal promise() {
    return context.promise();
  }

  @Override
  protected  PromiseInternal promise(Handler> handler) {
    return context.promise(handler);
  }

  @Override
  public void handleClosed() {
    Handler handler = closeHandler;
    if (handler != null) {
      context.emit(handler);
    }
  }

  @Override
  public  Future schedule(ContextInternal context, CommandBase cmd) {
    if (tx != null) {
      // TODO
      Promise promise = context.promise();
      tx.schedule(cmd, promise);
      return promise.future();
    } else {
      QueryReporter queryReporter;
      VertxTracer tracer = context.owner().tracer();
      ClientMetrics metrics = conn.metrics();
      if (!(conn instanceof SqlConnectionPool.PooledConnection) && cmd instanceof QueryCommandBase && (tracer != null || metrics != null)) {
        queryReporter = new QueryReporter(tracer, metrics, context, (QueryCommandBase) cmd, conn);
        queryReporter.before();
        return conn
          .schedule(context, cmd)
          .andThen(ar -> {
            queryReporter.after(ar);
          });
      } else {
        return conn.schedule(context, cmd);
      }
    }
  }

  @Override
  public void handleException(Throwable err) {
    Handler handler = exceptionHandler;
    if (handler != null) {
      context.emit(err, handler);
    } else {
      err.printStackTrace();
    }
  }

  @Override
  public boolean isSSL() {
    return conn.isSsl();
  }

  @Override
  public DatabaseMetadata databaseMetadata() {
    return conn.getDatabaseMetaData();
  }

  @Override
  public C closeHandler(Handler handler) {
    closeHandler = handler;
    return (C) this;
  }

  @Override
  public C exceptionHandler(Handler handler) {
    exceptionHandler = handler;
    return (C) this;
  }

  @Override
  public Future begin() {
    if (tx != null) {
      throw new IllegalStateException();
    }
    tx = new TransactionImpl(context, v -> tx = null, conn);
    return tx.begin();
  }

  @Override
  public Transaction transaction() {
    return tx;
  }

  @Override
  boolean autoCommit() {
    return tx == null;
  }

  @Override
  public void begin(Handler> handler) {
    Future fut = begin();
    fut.onComplete(handler);
  }

  public void handleEvent(Object event) {
  }

  @Override
  public Future close() {
    Promise promise = promise();
    close(promise);
    return promise.future();
  }

  @Override
  public void close(Handler> handler) {
    close(promise(handler));
  }

  @Override
  public void close(Promise completion) {
    doClose(completion);
    if (closeFactoryAfterUsage) {
      completion.future().onComplete(v -> factory.close(Promise.promise()));
      context.removeCloseHook(this);
    }
  }

  private void doClose(Promise promise) {
    context.execute(promise, p -> {
      if (tx != null) {
        tx.rollback(ar -> conn.close(this, p));
        tx = null;
      } else {
        conn.close(this, p);
      }
    });
  }

  protected static Future prepareForClose(ContextInternal ctx, Future future) {
    return future.andThen(ar -> {
      if (ar.succeeded()) {
        SqlConnectionBase base = (SqlConnectionBase) ar.result();
        base.closeFactoryAfterUsage = true;
        ctx.addCloseHook(base);
      }
    });
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy