io.vertx.rxjava.sqlclient.Pool Maven / Gradle / Ivy
/*
* Copyright 2014 Red Hat, Inc.
*
* Red Hat licenses this file to you 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.rxjava.sqlclient;
import rx.Observable;
import rx.Single;
import io.vertx.rx.java.RxHelper;
import io.vertx.rx.java.WriteStreamSubscriber;
import io.vertx.rx.java.SingleOnSubscribeAdapter;
import java.util.Map;
import java.util.Set;
import java.util.List;
import java.util.Iterator;
import java.util.function.Function;
import java.util.stream.Collectors;
import io.vertx.core.Handler;
import io.vertx.core.AsyncResult;
import io.vertx.core.json.JsonObject;
import io.vertx.core.json.JsonArray;
import io.vertx.lang.rx.RxGen;
import io.vertx.lang.rx.TypeArg;
import io.vertx.lang.rx.MappingIterator;
/**
* A connection pool which reuses a number of SQL connections.
*
*
* NOTE: This class has been automatically generated from the {@link io.vertx.sqlclient.Pool original} non RX-ified interface using Vert.x codegen.
*/
@RxGen(io.vertx.sqlclient.Pool.class)
public class Pool extends io.vertx.rxjava.sqlclient.SqlClient {
@Override
public String toString() {
return delegate.toString();
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Pool that = (Pool) o;
return delegate.equals(that.delegate);
}
@Override
public int hashCode() {
return delegate.hashCode();
}
public static final TypeArg __TYPE_ARG = new TypeArg<>( obj -> new Pool((io.vertx.sqlclient.Pool) obj),
Pool::getDelegate
);
private final io.vertx.sqlclient.Pool delegate;
public Pool(io.vertx.sqlclient.Pool delegate) {
super(delegate);
this.delegate = delegate;
}
public Pool(Object delegate) {
super((io.vertx.sqlclient.Pool)delegate);
this.delegate = (io.vertx.sqlclient.Pool)delegate;
}
public io.vertx.sqlclient.Pool getDelegate() {
return delegate;
}
private static final TypeArg> TYPE_ARG_0 = new TypeArg>(o1 -> io.vertx.rxjava.sqlclient.RowSet.newInstance((io.vertx.sqlclient.RowSet)o1, new TypeArg(o2 -> io.vertx.rxjava.sqlclient.Row.newInstance((io.vertx.sqlclient.Row)o2), o2 -> o2.getDelegate())), o1 -> o1.getDelegate());
private static final TypeArg> TYPE_ARG_1 = new TypeArg>(o1 -> io.vertx.rxjava.sqlclient.RowSet.newInstance((io.vertx.sqlclient.RowSet)o1, new TypeArg(o2 -> io.vertx.rxjava.sqlclient.Row.newInstance((io.vertx.sqlclient.Row)o2), o2 -> o2.getDelegate())), o1 -> o1.getDelegate());
/**
* Create a connection pool to the database configured with the given connectOptions
and default {@link io.vertx.sqlclient.PoolOptions}
* @param connectOptions the options used to create the connection pool, such as database hostname
* @return the connection pool
*/
public static io.vertx.rxjava.sqlclient.Pool pool(io.vertx.sqlclient.SqlConnectOptions connectOptions) {
io.vertx.rxjava.sqlclient.Pool ret = io.vertx.rxjava.sqlclient.Pool.newInstance((io.vertx.sqlclient.Pool)io.vertx.sqlclient.Pool.pool(connectOptions));
return ret;
}
/**
* Create a connection pool to the database configured with the given connectOptions
and poolOptions
.
* @param connectOptions the options used to create the connection pool, such as database hostname
* @param poolOptions the options for creating the pool
* @return the connection pool
*/
public static io.vertx.rxjava.sqlclient.Pool pool(io.vertx.sqlclient.SqlConnectOptions connectOptions, io.vertx.sqlclient.PoolOptions poolOptions) {
io.vertx.rxjava.sqlclient.Pool ret = io.vertx.rxjava.sqlclient.Pool.newInstance((io.vertx.sqlclient.Pool)io.vertx.sqlclient.Pool.pool(connectOptions, poolOptions));
return ret;
}
/**
* Create a connection pool to the database configured with the given connectOptions
and poolOptions
.
* @param vertx the Vertx instance to be used with the connection pool
* @param connectOptions the options used to create the connection pool, such as database hostname
* @param poolOptions the options for creating the pool
* @return the connection pool
*/
public static io.vertx.rxjava.sqlclient.Pool pool(io.vertx.rxjava.core.Vertx vertx, io.vertx.sqlclient.SqlConnectOptions connectOptions, io.vertx.sqlclient.PoolOptions poolOptions) {
io.vertx.rxjava.sqlclient.Pool ret = io.vertx.rxjava.sqlclient.Pool.newInstance((io.vertx.sqlclient.Pool)io.vertx.sqlclient.Pool.pool(vertx.getDelegate(), connectOptions, poolOptions));
return ret;
}
/**
* Get a connection from the pool.
* @param handler the handler that will get the connection result
*/
public void getConnection(Handler> handler) {
delegate.getConnection(new Handler>() {
public void handle(AsyncResult ar) {
if (ar.succeeded()) {
handler.handle(io.vertx.core.Future.succeededFuture(io.vertx.rxjava.sqlclient.SqlConnection.newInstance((io.vertx.sqlclient.SqlConnection)ar.result())));
} else {
handler.handle(io.vertx.core.Future.failedFuture(ar.cause()));
}
}
});
}
/**
* Get a connection from the pool.
*/
public void getConnection() {
getConnection(ar -> { });
}
/**
* Get a connection from the pool.
* @return
*/
public Single rxGetConnection() {
return Single.create(new SingleOnSubscribeAdapter<>(fut -> {
getConnection(fut);
}));
}
/**
*
*
* A connection is borrowed from the connection pool when the query is executed and then immediately returned
* to the pool after it completes.
* @param sql
* @return
*/
public io.vertx.rxjava.sqlclient.Query> query(String sql) {
io.vertx.rxjava.sqlclient.Query> ret = io.vertx.rxjava.sqlclient.Query.newInstance((io.vertx.sqlclient.Query)delegate.query(sql), TYPE_ARG_0);
return ret;
}
/**
*
*
* A connection is borrowed from the connection pool when the query is executed and then immediately returned
* to the pool after it completes.
* @param sql
* @return
*/
public io.vertx.rxjava.sqlclient.PreparedQuery> preparedQuery(String sql) {
io.vertx.rxjava.sqlclient.PreparedQuery> ret = io.vertx.rxjava.sqlclient.PreparedQuery.newInstance((io.vertx.sqlclient.PreparedQuery)delegate.preparedQuery(sql), TYPE_ARG_1);
return ret;
}
/**
* Execute the given function
within a transaction.
*
* The function
is passed a client executing all operations within a transaction.
* When the future returned by the function
*
* - succeeds the transaction commits
* - fails the transaction rollbacks
*
*
* The handler
is given a success result when the function returns a succeeded futures and the transaction commits.
* Otherwise it is given a failure result.
* @param function the code to execute
* @param handler the result handler
*/
public void withTransaction(Function> function, Handler> handler) {
delegate.withTransaction(new Function>() {
public io.vertx.core.Future apply(io.vertx.sqlclient.SqlConnection arg) {
io.vertx.rxjava.core.Future ret = function.apply(io.vertx.rxjava.sqlclient.SqlConnection.newInstance((io.vertx.sqlclient.SqlConnection)arg));
return ret.getDelegate();
}
}, handler);
}
/**
* Execute the given function
within a transaction.
*
* The function
is passed a client executing all operations within a transaction.
* When the future returned by the function
*
* - succeeds the transaction commits
* - fails the transaction rollbacks
*
*
* The handler
is given a success result when the function returns a succeeded futures and the transaction commits.
* Otherwise it is given a failure result.
* @param function the code to execute
*/
public void withTransaction(Function> function) {
withTransaction(function, ar -> { });
}
/**
* Execute the given function
within a transaction.
*
* The function
is passed a client executing all operations within a transaction.
* When the future returned by the function
*
* - succeeds the transaction commits
* - fails the transaction rollbacks
*
*
* The handler
is given a success result when the function returns a succeeded futures and the transaction commits.
* Otherwise it is given a failure result.
* @param function the code to execute
* @return
*/
public Single rxWithTransaction(Function> function) {
return Single.create(new SingleOnSubscribeAdapter<>(fut -> {
withTransaction(function, fut);
}));
}
/**
* Get a connection from the pool and execute the given function
.
*
* When the future returned by the function
completes, the connection is returned to the pool.
*
*
The handler
is given a success result when the function returns a succeeded futures.
* Otherwise it is given a failure result.
* @param function the code to execute
* @param handler the result handler
*/
public void withConnection(Function> function, Handler> handler) {
delegate.withConnection(new Function>() {
public io.vertx.core.Future apply(io.vertx.sqlclient.SqlConnection arg) {
io.vertx.rxjava.core.Future ret = function.apply(io.vertx.rxjava.sqlclient.SqlConnection.newInstance((io.vertx.sqlclient.SqlConnection)arg));
return ret.getDelegate();
}
}, handler);
}
/**
* Get a connection from the pool and execute the given function
.
*
* When the future returned by the function
completes, the connection is returned to the pool.
*
*
The handler
is given a success result when the function returns a succeeded futures.
* Otherwise it is given a failure result.
* @param function the code to execute
*/
public void withConnection(Function> function) {
withConnection(function, ar -> { });
}
/**
* Get a connection from the pool and execute the given function
.
*
* When the future returned by the function
completes, the connection is returned to the pool.
*
*
The handler
is given a success result when the function returns a succeeded futures.
* Otherwise it is given a failure result.
* @param function the code to execute
* @return
*/
public Single rxWithConnection(Function> function) {
return Single.create(new SingleOnSubscribeAdapter<>(fut -> {
withConnection(function, fut);
}));
}
/**
* Close the pool and release the associated resources.
* @param handler the completion handler
*/
public void close(Handler> handler) {
delegate.close(handler);
}
/**
* Close the pool and release the associated resources.
*/
public void close() {
close(ar -> { });
}
/**
* Close the pool and release the associated resources.
* @return
*/
public Single rxClose() {
return Single.create(new SingleOnSubscribeAdapter<>(fut -> {
close(fut);
}));
}
public static Pool newInstance(io.vertx.sqlclient.Pool arg) {
return arg != null ? new Pool(arg) : null;
}
}