com.github.longdt.vertxorm.util.SQLHelper Maven / Gradle / Ivy
package com.github.longdt.vertxorm.util;
import io.vertx.core.AsyncResult;
import io.vertx.core.Future;
import io.vertx.core.Handler;
import io.vertx.core.Promise;
import io.vertx.sqlclient.*;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collector;
public class SQLHelper {
public static void executeInConnection(Pool pool, QuadConsumer>> consumer, T arg1, V arg2, Handler> resultHandler) {
pool.getConnection(getConn -> {
if (getConn.failed()) {
resultHandler.handle(Future.failedFuture(getConn.cause()));
} else {
final SqlConnection conn = getConn.result();
consumer.accept(conn, arg1, arg2, res -> {
conn.close();
resultHandler.handle(res);
});
}
});
}
public static void executeInConnection(Pool pool, TriConsumer>> consumer, T arg, Handler> resultHandler) {
pool.getConnection(getConn -> {
if (getConn.failed()) {
resultHandler.handle(Future.failedFuture(getConn.cause()));
} else {
final SqlConnection conn = getConn.result();
consumer.accept(conn, arg, res -> {
conn.close();
resultHandler.handle(res);
});
}
});
}
public static void executeInConnection(Pool pool, BiConsumer>> consumer, Handler> resultHandler) {
pool.getConnection(getConn -> {
if (getConn.failed()) {
resultHandler.handle(Future.failedFuture(getConn.cause()));
} else {
final SqlConnection conn = getConn.result();
consumer.accept(conn, res -> {
conn.close();
resultHandler.handle(res);
});
}
});
}
public static void executeInConnection(Pool pool, Function> action, Handler> resultHandler) {
pool.getConnection(getConn -> {
if (getConn.failed()) {
resultHandler.handle(Future.failedFuture(getConn.cause()));
} else {
final SqlConnection conn = getConn.result();
action.apply(conn).onComplete(res -> {
conn.close();
resultHandler.handle(res);
});
}
});
}
public static void inTransactionSingle(Pool pool, Function> action, Handler> resultHandler) {
executeInConnection(pool, conn -> {
var txn = conn.begin();
return action.apply(conn).compose(res -> Futures.toFuture((Consumer>>) txn::commit).map(res));
}, resultHandler);
}
public static Future> query(Pool pool, String query) {
var promise = Promise.>promise();
pool.query(query).execute(promise);
return promise.future();
}
public static Future> query(SqlConnection conn, String query) {
var promise = Promise.>promise();
conn.query(query).execute(promise);
return promise.future();
}
public static Future> query(SqlConnection conn, String query, Collector collector) {
var promise = Promise.>promise();
conn.query(query)
.collecting(collector)
.execute(promise);
return promise.future();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy