Skip to content

Commit

Permalink
[#137] First step
Browse files Browse the repository at this point in the history
  • Loading branch information
zero88 committed Aug 16, 2022
1 parent 2bc4825 commit 4d3cc32
Show file tree
Hide file tree
Showing 12 changed files with 151 additions and 69 deletions.
6 changes: 6 additions & 0 deletions core/src/main/java/io/github/zero88/jooqx/Jooqx.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,10 @@ public interface Jooqx extends JooqxBase<Pool> {
@Override
@NotNull JooqxTx transaction();

/**
* @see JooqxSession
*/
@SuppressWarnings("unchecked")
@Override
@NotNull JooqxSession session();
}
4 changes: 4 additions & 0 deletions core/src/main/java/io/github/zero88/jooqx/JooqxConn.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,8 @@ public interface JooqxConn extends JooqxBase<SqlConnection> {
@Override
@NotNull JooqxTx transaction();

@Override
@SuppressWarnings("unchecked")
@NotNull JooqxSession session();

}
36 changes: 30 additions & 6 deletions core/src/main/java/io/github/zero88/jooqx/JooqxSQLImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ private static <ROW, RESULT> RESULT collect(@NotNull SQLResultAdapter<ROW, RESUL
}


static class JooqxConnImpl extends JooqxImpl<SqlConnection> implements JooqxTx {
static class JooqxConnImpl extends JooqxImpl<SqlConnection> implements JooqxTx, JooqxSession {

private final Jooqx delegate;

Expand All @@ -137,16 +137,21 @@ static class JooqxConnImpl extends JooqxImpl<SqlConnection> implements JooqxTx {
return delegate().transaction();
}

@Override
@SuppressWarnings("unchecked")
public @NotNull JooqxSession session() {
return delegate().session();
}

@Override
public <X> Future<@Nullable X> run(@NotNull Function<JooqxTx, Future<X>> function) {
return delegate().sqlClient()
.getConnection()
.compose(conn -> beginTx(conn, function), t -> Future.failedFuture(
transientConnFailed("Unable to open SQL connection", t)));
.compose(conn -> beginTx(conn, function), this::unableToOpenSqlConnection);
}

@Override
protected JooqxTx withSqlClient(SqlConnection sqlClient) {
protected JooqxConnImpl withSqlClient(SqlConnection sqlClient) {
return new JooqxConnImpl(vertx(), dsl(), sqlClient, preparedQuery(), resultCollector(), errorConverter(),
typeMapperRegistry());
}
Expand All @@ -172,6 +177,20 @@ private Jooqx delegate() {
return delegate;
}

@Override
public <R> Future<R> session(@NotNull Function<JooqxSession, Future<R>> session) {
return delegate().sqlClient()
.getConnection()
.compose(conn -> conn.begin()
.flatMap(tx -> session.apply(withSqlClient(conn)))
.onComplete(ar -> conn.close()), this::unableToOpenSqlConnection);
}

@NotNull
private <R> Future<R> unableToOpenSqlConnection(Throwable t) {
return Future.failedFuture(transientConnFailed("Unable to open SQL connection", t));
}

}


Expand All @@ -184,9 +203,14 @@ static class JooqxPoolImpl extends JooqxImpl<Pool> implements Jooqx {
}

@Override
@NotNull
@SuppressWarnings("unchecked")
public JooqxTx transaction() {
public @NotNull JooqxTx transaction() {
return new JooqxConnImpl(this);
}

@Override
@SuppressWarnings("unchecked")
public @NotNull JooqxSession session() {
return new JooqxConnImpl(this);
}

Expand Down
26 changes: 26 additions & 0 deletions core/src/main/java/io/github/zero88/jooqx/JooqxSession.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package io.github.zero88.jooqx;

import java.util.function.Function;

import org.jetbrains.annotations.NotNull;

import io.vertx.codegen.annotations.VertxGen;
import io.vertx.core.Future;
import io.vertx.sqlclient.SqlConnection;
import io.vertx.sqlclient.Tuple;

/**
* @since 2.0.0
*/
@VertxGen
public interface JooqxSession extends JooqxConn,
SQLSessionExecutor<SqlConnection, Tuple, JooqxPreparedQuery,
JooqxResultCollector, JooqxSession> {

@Override
default @NotNull JooqxSession session() { return this; }

@Override
<R> Future<R> session(@NotNull Function<JooqxSession, Future<R>> function);

}
7 changes: 0 additions & 7 deletions core/src/main/java/io/github/zero88/jooqx/JooqxTx.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
import org.jetbrains.annotations.NotNull;

import io.vertx.codegen.annotations.VertxGen;
import io.vertx.core.AsyncResult;
import io.vertx.core.Future;
import io.vertx.core.Handler;
import io.vertx.sqlclient.SqlConnection;
import io.vertx.sqlclient.Tuple;

Expand All @@ -25,11 +23,6 @@ public interface JooqxTx
return this;
}

@Override
default <X> void run(@NotNull Function<JooqxTx, Future<X>> function, @NotNull Handler<AsyncResult<X>> handler) {
SQLTxExecutor.super.run(function, handler);
}

@Override
<X> Future<X> run(@NotNull Function<JooqxTx, Future<X>> function);

Expand Down
7 changes: 7 additions & 0 deletions core/src/main/java/io/github/zero88/jooqx/LegacyJooqx.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,11 @@ static LegacyJooqxBuilder builder() {
@SuppressWarnings("unchecked")
@NotNull LegacyJooqxTx transaction();

/**
* @see LegacyJooqxSession
*/
@Override
@SuppressWarnings("unchecked")
@NotNull LegacyJooqxSession session();

}
27 changes: 27 additions & 0 deletions core/src/main/java/io/github/zero88/jooqx/LegacyJooqxSession.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package io.github.zero88.jooqx;

import java.util.function.Function;

import org.jetbrains.annotations.NotNull;

import io.vertx.codegen.annotations.VertxGen;
import io.vertx.core.Future;
import io.vertx.core.json.JsonArray;
import io.vertx.ext.sql.SQLConnection;

/**
* @since 2.0.0
*/
@Deprecated
@VertxGen
public interface LegacyJooqxSession extends LegacyInternal<SQLConnection>,
SQLSessionExecutor<SQLConnection, JsonArray, LegacySQLPreparedQuery,
LegacySQLCollector, LegacyJooqxSession> {

@Override
default @NotNull LegacyJooqxSession session() { return this; }

@Override
<R> Future<R> session(@NotNull Function<LegacyJooqxSession, Future<R>> function);

}
53 changes: 0 additions & 53 deletions core/src/main/java/io/github/zero88/jooqx/LegacyJooqxTx.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,65 +29,12 @@ public interface LegacyJooqxTx extends LegacyInternal<SQLConnection>,
SQLTxExecutor<SQLConnection, JsonArray, LegacySQLPreparedQuery,
LegacySQLCollector, LegacyJooqxTx> {

@Override
@NotNull Vertx vertx();

@Override
@GenIgnore(GenIgnore.PERMITTED_TYPE)
@NotNull DSLContext dsl();

@Override
@NotNull SQLConnection sqlClient();

@Override
@GenIgnore(GenIgnore.PERMITTED_TYPE)
@NotNull LegacySQLPreparedQuery preparedQuery();

@Override
@NotNull LegacySQLCollector resultCollector();

@Override
@GenIgnore(GenIgnore.PERMITTED_TYPE)
@NotNull SQLErrorConverter errorConverter();

@Override
@GenIgnore(GenIgnore.PERMITTED_TYPE)
@NotNull DataTypeMapperRegistry typeMapperRegistry();

@Override
@SuppressWarnings("unchecked")
default @NotNull LegacyJooqxTx transaction() {
return this;
}

@Override
@GenIgnore(GenIgnore.PERMITTED_TYPE)
<T, R> Future<@Nullable R> execute(@NotNull Query query, @NotNull SQLResultAdapter<T, R> adapter);

@Override
@GenIgnore(GenIgnore.PERMITTED_TYPE)
default <T, R> void execute(@NotNull Query query, @NotNull SQLResultAdapter<T, R> resultAdapter,
@NotNull Handler<AsyncResult<@Nullable R>> handler) {
LegacyInternal.super.execute(query, resultAdapter, handler);
}

@Override
@GenIgnore(GenIgnore.PERMITTED_TYPE)
default void batch(@NotNull Query query, @NotNull BindBatchValues bindBatchValues,
@NotNull Handler<AsyncResult<BatchResult>> handler) {
LegacyInternal.super.batch(query, bindBatchValues, handler);
}

@Override
@GenIgnore(GenIgnore.PERMITTED_TYPE)
Future<BatchResult> batch(@NotNull Query query, @NotNull BindBatchValues bindBatchValues);

@Override
default <X> void run(@NotNull Function<LegacyJooqxTx, Future<X>> function,
@NotNull Handler<AsyncResult<X>> handler) {
SQLTxExecutor.super.run(function, handler);
}

@Override
<X> Future<X> run(@NotNull Function<LegacyJooqxTx, Future<X>> function);

Expand Down
10 changes: 8 additions & 2 deletions core/src/main/java/io/github/zero88/jooqx/LegacySQLImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,8 @@ protected Future<SQLConnection> openConn() {


@Deprecated
static final class LegacyJooqTxImpl extends LegacySQLEI<SQLConnection> implements LegacyJooqxTx {
static final class LegacyJooqTxImpl extends LegacySQLEI<SQLConnection>
implements LegacyJooqxTx, LegacyJooqxSession {

private final LegacySQLEI<SQLClient> delegate;

Expand Down Expand Up @@ -260,7 +261,7 @@ public <X> Future<X> run(@NotNull Function<LegacyJooqxTx, Future<X>> block) {
protected Future<SQLConnection> openConn() { return Future.succeededFuture(sqlClient()); }

@Override
protected LegacyJooqxTx withSqlClient(@NotNull SQLConnection sqlConn) {
protected LegacyJooqTxImpl withSqlClient(@NotNull SQLConnection sqlConn) {
return new LegacyJooqTxImpl(vertx(), dsl(), sqlConn, preparedQuery(), resultCollector(), errorConverter(),
typeMapperRegistry());
}
Expand Down Expand Up @@ -290,6 +291,11 @@ private <X> void failed(@NotNull SQLConnection conn, @NotNull Promise<X> promise
conn.close();
}

@Override
public @NotNull LegacyJooqxSession session() {
return null;
}

}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
@Unstable
@Experimental
@VertxGen(concrete = false)

public interface SQLBlockExecutor extends JooqDSLProvider {

/**
Expand Down
9 changes: 9 additions & 0 deletions core/src/main/java/io/github/zero88/jooqx/SQLExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,15 @@ public interface SQLExecutor<S, B, PQ extends SQLPreparedQuery<B>, RC extends SQ
*/
@NotNull <E extends SQLExecutor<S, B, PQ, RC>> SQLTxExecutor<S, B, PQ, RC, E> transaction();

/**
* Open session executor
*
* @param <E> Type of VertxJooqExecutor
* @return transaction executor
* @see SQLSessionExecutor
*/
@NotNull <E extends SQLExecutor<S, B, PQ, RC>> SQLSessionExecutor<S, B, PQ, RC, E> session();

@Override
default <T> Future<T> routine(@NotNull Routine<T> routine) {
return RoutineExecutorDelegate.init(this).routine(routine);
Expand Down
34 changes: 34 additions & 0 deletions core/src/main/java/io/github/zero88/jooqx/SQLSessionExecutor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package io.github.zero88.jooqx;

import java.util.function.Function;

import org.jetbrains.annotations.NotNull;

import io.vertx.core.AsyncResult;
import io.vertx.core.Future;
import io.vertx.core.Handler;

public interface SQLSessionExecutor<S, B, P extends SQLPreparedQuery<B>, C extends SQLResultCollector,
E extends SQLExecutor<S, B, P, C>> {

/**
* Run the session code
*
* @param function session function
* @param handler handler
* @param <R> Type of result
*/
default <R> void session(@NotNull Function<E, Future<R>> function, @NotNull Handler<AsyncResult<R>> handler) {
session(function).onComplete(handler);
}

/**
* Like {@link #session(Function, Handler)} but returns a {@code Future} of the asynchronous result
*
* @param function session function
* @param <R> Type of result
* @return a {@code Future} of the asynchronous result
*/
<R> Future<R> session(@NotNull Function<E, Future<R>> function);

}

0 comments on commit 4d3cc32

Please sign in to comment.