Skip to content

Commit

Permalink
Merge pull request #138 from zero88/feature/support-session
Browse files Browse the repository at this point in the history
[#137] Implement JooqxSession
  • Loading branch information
zero88 authored Aug 19, 2022
2 parents 2bc4825 + 22a018e commit 667a691
Show file tree
Hide file tree
Showing 43 changed files with 1,103 additions and 1,129 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();

}
33 changes: 24 additions & 9 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 @@ -133,20 +133,30 @@ static class JooqxConnImpl extends JooqxImpl<SqlConnection> implements JooqxTx {

@Override
@SuppressWarnings("unchecked")
public @NotNull JooqxTx transaction() {
return delegate().transaction();
public @NotNull JooqxTx transaction() { 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>> transactionFn) {
return delegate().sqlClient()
.getConnection()
.compose(conn -> beginTx(conn, transactionFn),
t -> Future.failedFuture(unableOpenConn(t)));
}

@Override
public <X> Future<@Nullable X> run(@NotNull Function<JooqxTx, Future<X>> function) {
public <R> Future<R> perform(@NotNull Function<JooqxSession, Future<R>> sessionFn) {
return delegate().sqlClient()
.getConnection()
.compose(conn -> beginTx(conn, function), t -> Future.failedFuture(
transientConnFailed("Unable to open SQL connection", t)));
.compose(conn -> sessionFn.apply(withSqlClient(conn)).onComplete(ar -> conn.close()),
t -> Future.failedFuture(unableOpenConn(t)));
}

@Override
protected JooqxTx withSqlClient(SqlConnection sqlClient) {
protected JooqxConnImpl withSqlClient(SqlConnection sqlClient) {
return new JooqxConnImpl(vertx(), dsl(), sqlClient, preparedQuery(), resultCollector(), errorConverter(),
typeMapperRegistry());
}
Expand Down Expand Up @@ -184,9 +194,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
33 changes: 33 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,33 @@
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.AsyncResult;
import io.vertx.core.Future;
import io.vertx.core.Handler;
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; }

default <R> void perform(@NotNull Function<JooqxSession, Future<R>> sessionFn,
@NotNull Handler<AsyncResult<R>> handler) {
perform(sessionFn).onComplete(handler);
}

@Override
<R> Future<R> perform(@NotNull Function<JooqxSession, Future<R>> sessionFn);

}
10 changes: 4 additions & 6 deletions core/src/main/java/io/github/zero88/jooqx/JooqxTx.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,14 @@ public interface JooqxTx
extends JooqxConn, SQLTxExecutor<SqlConnection, Tuple, JooqxPreparedQuery, JooqxResultCollector, JooqxTx> {

@Override
default @NotNull JooqxTx transaction() {
return this;
}
default @NotNull JooqxTx transaction() { return this; }

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

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

}
8 changes: 8 additions & 0 deletions core/src/main/java/io/github/zero88/jooqx/LegacyInternal.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,12 @@ public interface LegacyInternal<S extends SQLOperations>
@Override
@NotNull LegacySQLCollector resultCollector();

@Override
@SuppressWarnings("unchecked")
@NotNull LegacyJooqxTx transaction();

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

}
7 changes: 6 additions & 1 deletion core/src/main/java/io/github/zero88/jooqx/LegacyJooqx.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,12 @@ static LegacyJooqxBuilder builder() {
* @see LegacyJooqxTx
*/
@Override
@SuppressWarnings("unchecked")
@NotNull LegacyJooqxTx transaction();

/**
* @see LegacyJooqxSession
*/
@Override
@NotNull LegacyJooqxSession session();

}
38 changes: 38 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,38 @@
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.AsyncResult;
import io.vertx.core.Future;
import io.vertx.core.Handler;
import io.vertx.core.json.JsonArray;
import io.vertx.ext.sql.SQLConnection;

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

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

@Override
@NotNull LegacyJooqxTx transaction();

@Override
default <R> void perform(@NotNull Function<LegacyJooqxSession, Future<R>> sessionFn,
@NotNull Handler<AsyncResult<R>> handler) {
perform(sessionFn).onComplete(handler);
}

@Override
<R> Future<R> perform(@NotNull Function<LegacyJooqxSession, Future<R>> sessionFn);

}
64 changes: 5 additions & 59 deletions core/src/main/java/io/github/zero88/jooqx/LegacyJooqxTx.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,11 @@
import java.util.function.Function;

import org.jetbrains.annotations.NotNull;
import org.jooq.DSLContext;
import org.jooq.Query;

import io.github.zero88.jooqx.adapter.SQLResultAdapter;
import io.github.zero88.jooqx.datatype.DataTypeMapperRegistry;
import io.vertx.codegen.annotations.GenIgnore;
import io.vertx.codegen.annotations.Nullable;
import io.vertx.codegen.annotations.VertxGen;
import io.vertx.core.AsyncResult;
import io.vertx.core.Future;
import io.vertx.core.Handler;
import io.vertx.core.Vertx;
import io.vertx.core.json.JsonArray;
import io.vertx.ext.sql.SQLConnection;

Expand All @@ -30,65 +23,18 @@ public interface LegacyJooqxTx extends LegacyInternal<SQLConnection>,
LegacySQLCollector, LegacyJooqxTx> {

@Override
@NotNull Vertx vertx();
@NotNull LegacyJooqxSession session();

@Override
@GenIgnore(GenIgnore.PERMITTED_TYPE)
@NotNull DSLContext dsl();
default @NotNull LegacyJooqxTx transaction() { return this; }

@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,
default <X> void run(@NotNull Function<LegacyJooqxTx, Future<X>> transactionFn,
@NotNull Handler<AsyncResult<X>> handler) {
SQLTxExecutor.super.run(function, handler);
run(transactionFn).onComplete(handler);
}

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

}
Loading

0 comments on commit 667a691

Please sign in to comment.