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

io.vertx.db2client.impl.DB2ConnectionImpl Maven / Gradle / Ivy

/*
 * Copyright (C) 2019,2020 IBM Corporation
 *
 * 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.db2client.impl;

import io.vertx.core.AsyncResult;
import io.vertx.core.Future;
import io.vertx.core.Handler;
import io.vertx.core.Promise;
import io.vertx.core.Vertx;
import io.vertx.core.impl.ContextInternal;
import io.vertx.db2client.DB2ConnectOptions;
import io.vertx.db2client.DB2Connection;
import io.vertx.db2client.impl.command.PingCommand;
import io.vertx.sqlclient.impl.Connection;
import io.vertx.sqlclient.impl.SqlConnectionImpl;
import io.vertx.sqlclient.impl.tracing.QueryTracer;

public class DB2ConnectionImpl extends SqlConnectionImpl implements DB2Connection {

  public static Future connect(Vertx vertx, DB2ConnectOptions options) {
    ContextInternal ctx = (ContextInternal) vertx.getOrCreateContext();
    DB2ConnectionFactory client;
    try {
      client = new DB2ConnectionFactory(ctx, options);
    } catch (Exception e) {
      return ctx.failedFuture(e);
    }
    QueryTracer tracer = ctx.tracer() == null ? null : new QueryTracer(ctx.tracer(), options);
    Promise promise = ctx.promise();
    ctx.dispatch(null, v -> connect(client, ctx, tracer, promise));
    return promise.future();
  }

  private static void connect(DB2ConnectionFactory client, ContextInternal ctx, QueryTracer tracer, Promise promise) {
    client.connect().map(conn -> {
      DB2ConnectionImpl db2Connection = new DB2ConnectionImpl(client, ctx, conn, tracer);
      conn.init(db2Connection);
      return (DB2Connection) db2Connection;
    }).onComplete(promise);
  }

  public DB2ConnectionImpl(DB2ConnectionFactory factory, ContextInternal context, Connection conn, QueryTracer tracer) {
    super(context, conn, tracer);
  }

  @Override
  public DB2Connection ping(Handler> handler) {
    Future fut = ping();
    if (handler != null) {
      fut.onComplete(handler);
    }
    return this;
  }

  @Override
  public Future ping() {
    Promise promise = promise();
    schedule(new PingCommand(), promise);
    return promise.future();
  }

  @Override
  public DB2Connection debug(Handler> handler) {
    throw new UnsupportedOperationException("Debug command not implemented");
  }

  @Override
  public Future debug() {
    throw new UnsupportedOperationException("Debug command not implemented");
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy