Skip to content

Commit

Permalink
#34 ReactiveClassicQueryExecutor work
Browse files Browse the repository at this point in the history
  • Loading branch information
Jens Klingsporn committed Mar 14, 2018
1 parent 3bcad33 commit 4534ee7
Show file tree
Hide file tree
Showing 22 changed files with 109 additions and 132 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,75 +6,63 @@
import com.julienviet.pgclient.Tuple;
import io.github.jklingsporn.vertx.jooq.shared.internal.QueryExecutor;
import io.vertx.core.Future;
import io.vertx.core.json.JsonObject;
import org.jooq.*;
import org.jooq.exception.TooManyRowsException;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.function.UnaryOperator;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

/**
* Created by jensklingsporn on 01.03.18.
*/
public class ReactiveClassicQueryExecutor<R extends UpdatableRecord<R>,P,T> implements QueryExecutor<R,T,Future<List<P>>,Future<P>,Future<Integer>,Future<T>>{

private final Function<JsonObject,P> pojoMapper;
private final Function<Row,P> pojoMapper;
private final PgClient delegate;

public ReactiveClassicQueryExecutor(PgClient delegate, Function<JsonObject, P> pojoMapper, Table<R> table) {
this.pojoMapper = convertFromSQL(table).andThen(pojoMapper);
public ReactiveClassicQueryExecutor(PgClient delegate, Function<Row, P> pojoMapper) {
this.pojoMapper = pojoMapper; //TODO respect jOOQ-converters
this.delegate = delegate;
}

@Override
public Future<List<P>> findMany(ResultQuery<R> query) {
Future<PgResult<Row>> rowFuture = Future.future();
delegate.preparedQuery(query.getSQL(),getBindValues(query),rowFuture);
rowFuture.map(res->{
for(Row row: res){

}
return Future.succeededFuture();
});
return null;
return rowFuture.map(res-> StreamSupport
.stream(res.spliterator(),false)
.map(pojoMapper::apply)
.collect(Collectors.toList()));
}

@Override
public Future<P> findOne(ResultQuery<R> query) {
return null;
Future<PgResult<Row>> rowFuture = Future.future();
delegate.preparedQuery(query.getSQL(),getBindValues(query),rowFuture);
return rowFuture.map(res-> {
switch (res.size()) {
case 0: return null;
case 1: return pojoMapper.apply(res.iterator().next());
default: throw new TooManyRowsException(String.format("Found more than one row: %d", res.size()));
}
});
}

@Override
public Future<Integer> execute(Query query) {
return null;
Future<PgResult<Row>> rowFuture = Future.future();
delegate.preparedQuery(query.getSQL(),getBindValues(query),rowFuture);
return rowFuture.map(PgResult::updatedCount);
}

@Override
public Future<T> insertReturning(InsertResultStep<R> query, Function<Object, T> keyMapper) {
return null;
}

protected UnaryOperator<JsonObject> convertFromSQL(Table<?> table){
Map<String, Converter<Object, Object>> pojoConverters = table
.fieldStream()
.filter(f -> f.getConverter() != null)
.collect(Collectors.toMap(Field::getName, v -> ((Converter<Object, Object>) v.getConverter())));
return json -> {
JsonObject theCopy = new JsonObject();
for (Map.Entry<String, Object> jsonMap : json.getMap().entrySet()) {
Converter<Object, Object> converter = pojoConverters.get(jsonMap.getKey());
if(converter!=null){
theCopy.put(jsonMap.getKey(), converter.from(jsonMap.getValue()));
}else{
theCopy.put(jsonMap.getKey(), jsonMap.getValue());
}
}
return theCopy;
};
}

protected Tuple getBindValues(Query query) {
ArrayList<Object> bindValues = new ArrayList<>();
Expand All @@ -85,8 +73,6 @@ protected Tuple getBindValues(Query query) {
return Tuple.of(bindValues.toArray());
}



protected <U> Object convertToDatabaseType(Param<U> param) {
return (param.getBinding().converter().to(param.getValue()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ public DIStep withPostgresReactiveDriver() {
base.addWriteExtraDataDelegate((schema, writerGen) -> {
ComponentBasedVertxGenerator.logger.info("Generate RowMappers ... ");
String packageName = (base.getStrategy().getTargetDirectory() + "/" + base.getStrategy().getJavaPackageName(schema) + ".tables.mappers").replaceAll("\\.", "/");
File moduleFile = new File(packageName, "DaoModule.java");
File moduleFile = new File(packageName, "RowMappers.java");
JavaWriter out = writerGen.apply(moduleFile);
out.println("package " + base.getStrategy().getJavaPackageName(schema) + ".tables.mappers;");
out.println();
Expand All @@ -300,6 +300,7 @@ public DIStep withPostgresReactiveDriver() {
supportedRowTypes.add(Integer.class.getName());
supportedRowTypes.add(Long.class.getName());
supportedRowTypes.add(Float.class.getName());
supportedRowTypes.add(Double.class.getName());
supportedRowTypes.add(BigDecimal.class.getName());
supportedRowTypes.add(String.class.getName());
supportedRowTypes.add(Character.class.getName());
Expand All @@ -321,7 +322,7 @@ public DIStep withPostgresReactiveDriver() {
continue;
}
final String pType = base.getStrategy().getFullJavaClassName(table, GeneratorStrategy.Mode.POJO);
out.tab(1).println("public static Function<Row,%s> get%sMapper() {",pType,pType);
out.tab(1).println("public static Function<Row,%s> get%sMapper() {",pType,base.getStrategy().getJavaClassName(table, GeneratorStrategy.Mode.POJO));
out.tab(2).println("return row -> {");
out.tab(3).println("%s pojo = new %s();",pType,pType);
for (TypedElementDefinition<?> column : table.getColumns()) {
Expand All @@ -343,7 +344,7 @@ public DIStep withPostgresReactiveDriver() {
}
}
out.tab(3).println("return pojo;");
out.tab(2).println("}");
out.tab(2).println("};");
out.tab(1).println("}");
out.println();
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 4534ee7

Please sign in to comment.