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

io.vertx.ext.jdbc.impl.JDBCConnectionImpl Maven / Gradle / Ivy

/*
 * Copyright (c) 2011-2014 The original author or authors
 * ------------------------------------------------------
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * and Apache License v2.0 which accompanies this distribution.
 *
 *     The Eclipse Public License is available at
 *     http://www.eclipse.org/legal/epl-v10.html
 *
 *     The Apache License v2.0 is available at
 *     http://www.opensource.org/licenses/apache2.0.php
 *
 * You may elect to redistribute this code under either of these licenses.
 */

package io.vertx.ext.jdbc.impl;

import io.vertx.core.*;
import io.vertx.core.impl.ContextInternal;
import io.vertx.core.impl.TaskQueue;
import io.vertx.core.impl.logging.Logger;
import io.vertx.core.impl.logging.LoggerFactory;
import io.vertx.core.json.JsonArray;
import io.vertx.core.spi.metrics.PoolMetrics;
import io.vertx.ext.jdbc.impl.actions.*;
import io.vertx.ext.sql.*;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

/**
 * @author Nick Scavelli
 * @author Paulo Lopes
 */
class JDBCConnectionImpl implements SQLConnection {

  static final Logger log = LoggerFactory.getLogger(JDBCConnectionImpl.class);

  private final Vertx vertx;
  final Connection conn;
  private final ContextInternal ctx;
  final PoolMetrics metrics;
  final Object metric;
  private final TaskQueue statementsQueue = new TaskQueue();

  private final JDBCStatementHelper helper;

  private SQLOptions options;

  public JDBCConnectionImpl(Context context, JDBCStatementHelper helper, Connection conn, PoolMetrics metrics, Object metric) {
    this.vertx = context.owner();
    this.helper = helper;
    this.conn = conn;
    this.metrics = metrics;
    this.metric = metric;
    this.ctx = (ContextInternal) context;
  }

  @Override
  public SQLConnection setOptions(SQLOptions options) {
    this.options = options;
    return this;
  }

  @Override
  public SQLConnection setAutoCommit(boolean autoCommit, Handler> resultHandler) {
    new JDBCAutoCommit(vertx, options, ctx, autoCommit).execute(conn, statementsQueue, resultHandler);
    return this;
  }

  @Override
  public SQLConnection execute(String sql, Handler> resultHandler) {
    new JDBCExecute(vertx, options, ctx, sql).execute(conn, statementsQueue, resultHandler);
    return this;
  }

  @Override
  public SQLConnection query(String sql, Handler> resultHandler) {
    new JDBCQuery(vertx, helper, options, ctx, sql, null).execute(conn, statementsQueue, resultHandler);
    return this;
  }

  @Override
  public SQLConnection queryStream(String sql, Handler> handler) {
    new StreamQuery(vertx, helper, options, ctx, statementsQueue, sql, null).execute(conn, statementsQueue,  handler);
    return this;
  }

  @Override
  public SQLConnection queryStreamWithParams(String sql, JsonArray params, Handler> handler) {
    new StreamQuery(vertx, helper, options, ctx, statementsQueue, sql, params).execute(conn, statementsQueue,  handler);
    return this;
  }

  @Override
  public SQLConnection queryWithParams(String sql, JsonArray params, Handler> resultHandler) {
    new JDBCQuery(vertx, helper, options, ctx, sql, params).execute(conn, statementsQueue, resultHandler);
    return this;
  }

  @Override
  public SQLConnection update(String sql, Handler> resultHandler) {
    new JDBCUpdate(vertx, helper, options, ctx, sql, null).execute(conn, statementsQueue, resultHandler);
    return this;
  }

  @Override
  public SQLConnection updateWithParams(String sql, JsonArray params, Handler> resultHandler) {
    new JDBCUpdate(vertx, helper, options, ctx, sql, params).execute(conn, statementsQueue, resultHandler);
    return this;
  }

  @Override
  public SQLConnection call(String sql, Handler> resultHandler) {
    new JDBCCallable(vertx, helper, options, ctx, sql, null, null).execute(conn, statementsQueue, resultHandler);
    return this;
  }

  @Override
  public SQLConnection callWithParams(String sql, JsonArray params, JsonArray outputs, Handler> resultHandler) {
    new JDBCCallable(vertx, helper, options, ctx, sql, params, outputs).execute(conn, statementsQueue, resultHandler);
    return this;
  }

  @Override
  public void close(Handler> handler) {
    new JDBCClose(vertx, options, ctx, metrics, metric).execute(conn, statementsQueue, handler);
  }

  @Override
  public void close() {
    close(ar -> {
      if (ar.failed()) {
        log.error("Failure in closing connection", ar.cause());
      }
    });
  }

  @Override
  public SQLConnection commit(Handler> handler) {
    new JDBCCommit(vertx, options, ctx).execute(conn, statementsQueue,  handler);
    return this;
  }

  @Override
  public SQLConnection rollback(Handler> handler) {
    new JDBCRollback(vertx, options, ctx).execute(conn, statementsQueue,  handler);
    return this;
  }

  @Override
  public SQLConnection getTransactionIsolation(Handler> handler) {
    ctx.executeBlocking((Promise f) -> {
      try {
        TransactionIsolation txIsolation = TransactionIsolation.from(conn.getTransactionIsolation());

        if (txIsolation != null) {
          f.complete(txIsolation);
        } else {
          f.fail("Unknown isolation level");
        }
      } catch (SQLException e) {
        f.fail(e);
      }
    }, statementsQueue, handler);

    return this;
  }

  @Override
  public SQLConnection batch(List sqlStatements, Handler>> handler) {
    new JDBCBatch(vertx, helper, options, ctx, sqlStatements).execute(conn, statementsQueue,  handler);
    return this;
  }

  @Override
  public SQLConnection batchWithParams(String statement, List args, Handler>> handler) {
    new JDBCBatch(vertx, helper, options, ctx, statement, args).execute(conn, statementsQueue,  handler);
    return this;
  }

  @Override
  public SQLConnection batchCallableWithParams(String statement, List inArgs, List outArgs, Handler>> handler) {
    new JDBCBatch(vertx, helper, options, ctx, statement, inArgs, outArgs).execute(conn, statementsQueue,  handler);
    return this;
  }

  @Override
  public SQLConnection setTransactionIsolation(TransactionIsolation isolation, Handler> handler) {
    ctx.executeBlocking((Promise f) -> {
      try {
        conn.setTransactionIsolation(isolation.getType());
        f.complete(null);
      } catch (SQLException e) {
        f.fail(e);
      }
    }, statementsQueue, handler);

    return this;
  }

  @Override
  @SuppressWarnings("unchecked")
  public  C unwrap() {
    return (C) conn;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy