io.vertx.sqlclient.impl.ClientBuilderBase 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.Future;
import io.vertx.core.Handler;
import io.vertx.core.Vertx;
import io.vertx.sqlclient.*;
import io.vertx.sqlclient.spi.Driver;
import java.util.List;
import java.util.function.Supplier;
public abstract class ClientBuilderBase implements ClientBuilder {
protected final Driver driver;
private PoolOptions poolOptions;
private Supplier> database;
private Handler connectionHandler;
private Vertx vertx;
public ClientBuilderBase(Driver driver) {
this.driver = driver;
}
@Override
public ClientBuilder with(PoolOptions options) {
this.poolOptions = options;
return this;
}
@Override
public ClientBuilder connectingTo(SqlConnectOptions database) {
return connectingTo(SingletonSupplier.wrap(database));
}
@Override
public ClientBuilder connectingTo(String database) {
return connectingTo(driver.parseConnectionUri(database));
}
@Override
public ClientBuilder connectingTo(Supplier> supplier) {
this.database = supplier;
return this;
}
@Override
public ClientBuilder connectingTo(List databases) {
return connectingTo(Utils.roundRobinSupplier(databases));
}
@Override
public ClientBuilder withConnectHandler(Handler handler) {
this.connectionHandler = handler;
return this;
}
@Override
public ClientBuilder using(Vertx vertx) {
this.vertx = vertx;
return this;
}
@Override
public final C build() {
PoolOptions poolOptions = this.poolOptions;
if (poolOptions == null) {
poolOptions = new PoolOptions();
}
C c = create(vertx, database, poolOptions);
if (c instanceof Pool) {
((Pool)c).connectHandler(connectionHandler);
}
return c;
}
protected abstract C create(Vertx vertx, Supplier> databases, PoolOptions poolOptions);
}