Skip to content

Commit

Permalink
[#115] Refactor BatchResult & BatchReturningResult
Browse files Browse the repository at this point in the history
  • Loading branch information
zero88 committed Aug 5, 2022
1 parent c2dbb7f commit ff5a9fc
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 81 deletions.
4 changes: 4 additions & 0 deletions core/src/main/java/io/github/zero88/jooqx/BatchResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
*/
public interface BatchResult {

static BatchResult create(int total, int successes) {
return new BatchResultImpl(total, successes);
}

int getTotal();

int getSuccesses();
Expand Down
19 changes: 19 additions & 0 deletions core/src/main/java/io/github/zero88/jooqx/BatchResultImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package io.github.zero88.jooqx;

class BatchResultImpl implements BatchResult {

private final int total;
private final int successes;

BatchResultImpl(int total, int successes) {
this.total = total;
this.successes = successes;
}

@Override
public int getTotal() { return total; }

@Override
public int getSuccesses() { return successes; }

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package io.github.zero88.jooqx;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import org.jetbrains.annotations.NotNull;

Expand All @@ -12,6 +14,10 @@
*/
public interface BatchReturningResult<R> extends BatchResult {

static <R> BatchReturningResult<R> create(int total, List<R> results) {
return new BatchReturningResultImpl<>(total, Optional.ofNullable(results).orElseGet(ArrayList::new));
}

@NotNull
List<R> getRecords();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.github.zero88.jooqx;

import java.util.List;

import org.jetbrains.annotations.NotNull;

final class BatchReturningResultImpl<R> extends BatchResultImpl implements BatchReturningResult<R> {

@NotNull
private final List<R> records;

BatchReturningResultImpl(int total, @NotNull List<R> rs) {
super(total, rs.size());
this.records = rs;
}

@NotNull
@Override
public List<R> getRecords() { return records; }

}
5 changes: 2 additions & 3 deletions core/src/main/java/io/github/zero88/jooqx/JooqxSQLImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import org.jooq.conf.Settings;
import org.jooq.impl.DSL;

import io.github.zero88.jooqx.MiscImpl.BatchResultImpl;
import io.github.zero88.jooqx.SQLImpl.SQLEI;
import io.github.zero88.jooqx.SQLImpl.SQLExecutorBuilderImpl;
import io.github.zero88.jooqx.SQLImpl.SQLPQ;
Expand Down Expand Up @@ -275,7 +274,7 @@ public Future<BatchResult> batch(@NotNull Query query, @NotNull BindBatchValues
return sqlClient().preparedQuery(preparedQuery().sql(dsl().configuration(), query))
.executeBatch(preparedQuery().bindValues(query, bindBatchValues, typeMapperRegistry()))
.map(r -> new ReactiveSQLBC().batchResultSize(r))
.map(s -> BatchResultImpl.create(bindBatchValues.size(), s))
.map(s -> BatchResult.create(bindBatchValues.size(), s))
.otherwise(errorConverter()::reThrowError);
}

Expand All @@ -286,7 +285,7 @@ public <T, R> Future<BatchReturningResult<R>> batchResult(@NotNull Query query,
return sqlClient().preparedQuery(preparedQuery().sql(dsl().configuration(), query))
.executeBatch(preparedQuery().bindValues(query, bindBatchValues, typeMapperRegistry()))
.map(rs -> new ReactiveSQLBC().collect(rs, adapter, dsl(), typeMapperRegistry()))
.map(rs -> BatchResultImpl.create(bindBatchValues.size(), rs))
.map(rs -> BatchReturningResult.create(bindBatchValues.size(), rs))
.otherwise(errorConverter()::reThrowError);
}

Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/io/github/zero88/jooqx/JsonRecord.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public interface JsonRecord<R extends TableRecord<R>> extends TableRecord<R> {
JsonObject toJson();

static <R extends TableRecord<R>> JsonRecord<R> create(@NotNull TableLike<R> table) {
return new MiscImpl.JsonRecordImpl<>(table.asTable());
return new JsonRecordImpl<>(table.asTable());
}

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

import java.util.Arrays;

import org.jooq.Table;
import org.jooq.TableRecord;
import org.jooq.impl.CustomRecord;

import io.vertx.core.json.JsonObject;

final class JsonRecordImpl<R extends TableRecord<R>> extends CustomRecord<R> implements JsonRecord<R> {

JsonRecordImpl(Table<R> table) {
super(table);
}

public JsonObject toJson() {
final JsonObject json = new JsonObject();
Arrays.stream(this.fields()).forEach(f -> json.put(f.getName(), f.get(this)));
return json;
}

}
3 changes: 1 addition & 2 deletions core/src/main/java/io/github/zero88/jooqx/LegacySQLImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import org.jooq.Routine;
import org.jooq.impl.DSL;

import io.github.zero88.jooqx.MiscImpl.BatchResultImpl;
import io.github.zero88.jooqx.SQLImpl.SQLEI;
import io.github.zero88.jooqx.SQLImpl.SQLExecutorBuilderImpl;
import io.github.zero88.jooqx.SQLImpl.SQLPQ;
Expand Down Expand Up @@ -148,7 +147,7 @@ public final Future<BatchResult> batch(@NotNull Query query, @NotNull BindBatchV
typeMapperRegistry()), promise));
return promise.future()
.map(r -> resultCollector().batchResultSize(r))
.map(s -> BatchResultImpl.create(bindBatchValues.size(), s))
.map(s -> BatchResult.create(bindBatchValues.size(), s))
.otherwise(errorConverter()::reThrowError);
}

Expand Down
75 changes: 0 additions & 75 deletions core/src/main/java/io/github/zero88/jooqx/MiscImpl.java

This file was deleted.

0 comments on commit ff5a9fc

Please sign in to comment.