Skip to content

Commit

Permalink
Deduplicated some code
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim203 committed Sep 20, 2024
1 parent a49701d commit aa7a7a3
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ EntityInfo processEntity(TypeMirror typeMirror) {

if (!keys.isEmpty()) {
indexes.add(new IndexInfo("", keys.toArray(new CharSequence[0]), true));
} else {
// todo just make every column a key
throw new IllegalStateException("Expected entity to have at least one field marked as key");
}

var entityInfo = new EntityInfo(tableName, type, columns, indexes, keys);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import static org.geysermc.databaseutils.processor.util.StringUtils.capitalize;

import com.squareup.javapoet.CodeBlock;
import java.util.HashMap;
import java.util.Map;
import javax.lang.model.element.Name;
Expand All @@ -31,15 +32,20 @@ public static String jdbcReadFor(Name typeName, String format) {
.formatted(format.formatted("read" + jdbcTypeFor(typeName) + "()"));
}

public static String jdbcGetFor(Name typeName, String format) {
return format.formatted("get" + jdbcTypeFor(typeName) + "("
+ CONVERT_FORMAT.getOrDefault(String.valueOf(typeName), "%s").formatted("$S") + ")");
public static String jdbcGetFor(Name typeName, String format, CharSequence parameterName) {
// returns e.g.:
// __result.getBytes("d")
// (char) __result.getInt("a")
var formatted = format.formatted(CodeBlock.of("get$L($S)", jdbcTypeFor(typeName), parameterName));
return CONVERT_FORMAT.getOrDefault(typeName.toString(), "%s").formatted(formatted);
}

public static String jdbcSetFor(Name typeName, String format) {
return CONVERT_FORMAT
.getOrDefault(String.valueOf(typeName), "%s")
.formatted(format.formatted("set" + jdbcTypeFor(typeName)));
public static String jdbcSetFor(Name typeName, String format, int index, CharSequence variableName) {
// returns e.g.:
// __result.setBytes(3, __d.encode(data))
// __result.setInt(0, (char) data)
var converted = CONVERT_FORMAT.getOrDefault(typeName.toString(), "%s").formatted(variableName);
return format.formatted(CodeBlock.of("set$L($L, $L)", jdbcTypeFor(typeName), index, converted));
}

private static void addMapping(Class<?> type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.Locale;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.function.Function;
import java.util.stream.Collectors;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.TypeElement;
Expand Down Expand Up @@ -257,119 +258,90 @@ public void addDelete(QueryContext context, MethodSpec.Builder spec) {

private void executeAndReturn(DialectMethod spec, QueryContext context, QueryBuilder builder) {
addExecuteQueryData(spec, context, builder, () -> {
if (context.returnInfo().isCollection()) {
spec.addStatement(
"$T __responses = new $T<>()",
context.returnType(),
context.typeUtils().collectionImplementationFor(context.returnType()));
spec.beginControlFlow("while (__result.next())");
} else {
spec.beginControlFlow("if (!__result.next())");
spec.addStatement("return null");
spec.endControlFlow();
}

if (context.hasProjectionColumnName()) {
var column = context.projectionColumnInfo();

var block = CodeBlock.builder();

if (context.returnInfo().isCollection()) {
block.add("__responses.add(");
} else {
block.add("return ");
}

var getFormat = jdbcGetFor(column.typeName(), "__result.%s");
if (TypeUtils.needsTypeCodec(column.typeName())) {
getFormat = CodeBlock.of("this.__$L.decode($L)", column.name(), getFormat)
.toString();
}

block.add("%s".formatted(getFormat), column.name());

if (context.returnInfo().isCollection()) {
block.add(")");
spec.addStatement(block.build());
spec.endControlFlow();
spec.addStatement("return __responses");
} else {
spec.addStatement(block.build());
}
return;
}

var arguments = new ArrayList<String>();
for (ColumnInfo column : context.columns()) {
var getFormat = jdbcGetFor(column.typeName(), "__result.%s");
if (TypeUtils.needsTypeCodec(column.typeName())) {
getFormat = CodeBlock.of("this.__$L.decode($L)", column.name(), getFormat)
.toString();
}

spec.addStatement("$T _$L = %s".formatted(getFormat), column.asType(), column.name(), column.name());
arguments.add("_" + column.name());
}

if (context.returnInfo().isCollection()) {
spec.addStatement(
"__responses.add(new $T($L))",
ClassName.get(context.entityType()),
String.join(", ", arguments));
spec.endControlFlow();
spec.addStatement("return __responses");
} else {
spec.addStatement(
"return new $T($L)", ClassName.get(context.entityType()), String.join(", ", arguments));
}
readResultBase(
spec,
context,
() -> {
if (context.returnInfo().isCollection()) {
spec.beginControlFlow("while (__result.next())");
} else {
spec.beginControlFlow("if (!__result.next())");
spec.addStatement("return null");
spec.endControlFlow();
}
},
(column) -> jdbcGetFor(column.typeName(), "__result.%s", column.name()));
});
}

private void readStructResult(DialectMethod spec, QueryContext context) {
// todo see if we can remove the common bits from this and executeAndReturn and make it separate methods
readResultBase(
spec,
context,
() -> {
if (context.returnInfo().isCollection()) {
spec.beginControlFlow("for (var __item : __result)");
spec.addStatement(
"var __data = new $T((($T) __item).getAttributes())",
FlexibleSqlInput.class,
Struct.class);
} else {
spec.beginControlFlow("if (__result == null)");
spec.addStatement("return null");
spec.endControlFlow();
spec.addStatement(
"var __data = new $T((($T) __result).getAttributes())",
FlexibleSqlInput.class,
Struct.class);
}
},
column -> jdbcReadFor(column.typeName(), "__data.%s"));
}

private void readResultBase(
DialectMethod spec, QueryContext context, Runnable initCode, Function<ColumnInfo, String> jdbcBaseFormat) {
if (context.returnInfo().isCollection()) {
spec.addStatement(
"$T __responses = new $T<>()",
context.returnType(),
context.typeUtils().collectionImplementationFor(context.returnType()));
spec.beginControlFlow("for (var __item : __result)");
spec.addStatement(
"var __data = new $T((($T) __item).getAttributes())", FlexibleSqlInput.class, Struct.class);
} else {
spec.beginControlFlow("if (__result == null)");
spec.addStatement("return null");
spec.endControlFlow();
spec.addStatement(
"var __data = new $T((($T) __result).getAttributes())", FlexibleSqlInput.class, Struct.class);
}

initCode.run();

if (context.hasProjectionColumnName()) {
var column = context.projectionColumnInfo();

var block = CodeBlock.builder();
block.add("__responses.add(");

var format = jdbcReadFor(column.typeName(), "__data.%s");
if (TypeUtils.needsTypeCodec(column.typeName())) {
format = CodeBlock.of("this.__$L.decode($L)", column.name(), format)
.toString();
if (context.returnInfo().isCollection()) {
block.add("__responses.add(");
} else {
block.add("return ");
}

block.add("%s".formatted(format), column.name());
var format = jdbcBaseFormat.apply(column);
if (TypeUtils.needsTypeCodec(column.typeName())) {
format = "this.__%s.decode(%s)".formatted(column.name(), format);
}
block.add(format);

block.add(")");
spec.addStatement(block.build());
spec.endControlFlow();
spec.addStatement("return __responses");
if (context.returnInfo().isCollection()) {
block.add(")");
spec.addStatement(block.build());
spec.endControlFlow();
spec.addStatement("return __responses");
} else {
spec.addStatement(block.build());
}
return;
}

var arguments = new ArrayList<String>();
for (ColumnInfo column : context.columns()) {
var format = jdbcReadFor(column.typeName(), "__data.%s");
var format = jdbcBaseFormat.apply(column);
if (TypeUtils.needsTypeCodec(column.typeName())) {
format = CodeBlock.of("this.__$L.decode($L)", column.name(), format)
.toString();
format = "this.__%s.decode(%s)".formatted(column.name(), format);
}

spec.addStatement("$T _$L = $L", column.asType(), column.name(), format);
Expand Down Expand Up @@ -473,7 +445,7 @@ private void addBySectionData(DialectMethod spec, QueryContext context, QueryBui
.toString();
}
// jdbc index starts at 1
spec.addStatement(jdbcSetFor(columnInfo.typeName(), "__statement.%s($L, $L)"), ++variableIndex, input);
spec.addStatement(jdbcSetFor(columnInfo.typeName(), "__statement.%s", ++variableIndex, input));
}

if (context.parametersInfo().isSelfCollection()) {
Expand Down

0 comments on commit aa7a7a3

Please sign in to comment.