Skip to content

Commit

Permalink
refactored so Dao type can be injected
Browse files Browse the repository at this point in the history
  • Loading branch information
edward3h committed Aug 24, 2024
1 parent 8d52515 commit 7f0cb2f
Show file tree
Hide file tree
Showing 29 changed files with 124 additions and 390 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.ethelred.kiwiproc.processor;

import io.soabase.recordbuilder.core.RecordBuilderFull;
import java.util.List;
import javax.lang.model.element.TypeElement;

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,4 @@ public interface DAOGenerator {
void generateProvider(DAOClassInfo classInfo);

void generateImpl(DAOClassInfo classInfo);

void generateTransactionManager(DAODataSourceInfo dataSourceInfo);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.ethelred.kiwiproc.processor;

import io.soabase.recordbuilder.core.RecordBuilderFull;
import java.util.List;
import javax.lang.model.element.ExecutableElement;
import org.ethelred.kiwiproc.meta.ParsedQuery;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,5 @@ private void processInterface(TypeElement interfaceElement) throws SQLException
var classInfo = builderStage.build();
poet.generateImpl(classInfo);
poet.generateProvider(classInfo);
if (generatedTransactionManagers.add(dataSourceName)) {
poet.generateTransactionManager(new DAODataSourceInfo(dataSourceName, packageName));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
package org.ethelred.kiwiproc.processor;

import io.soabase.recordbuilder.core.RecordBuilder;

import java.lang.annotation.*;

@RecordBuilder.Template(options = @RecordBuilder.Options(
builderMode = RecordBuilder.BuilderMode.STAGED,
enableWither = true,
addFunctionalMethodsToWith = true,
addSingleItemCollectionBuilders = true,
useImmutableCollections = true,
onceOnlyAssignment = true
))
@RecordBuilder.Template(
options =
@RecordBuilder.Options(
builderMode = RecordBuilder.BuilderMode.STAGED,
enableWither = true,
addFunctionalMethodsToWith = true,
addSingleItemCollectionBuilders = true,
useImmutableCollections = true,
onceOnlyAssignment = true))
@Retention(RetentionPolicy.SOURCE)
@Target(ElementType.TYPE)
@Inherited
public @interface KiwiRecordBuilder {
}
public @interface KiwiRecordBuilder {}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
/**
* Simplified view of type for parameters and return.
*/
public sealed interface KiwiType permits ContainerType, RecordType, SimpleType, SqlArrayType, UnsupportedType, VoidType {
public sealed interface KiwiType
permits ContainerType, RecordType, SimpleType, SqlArrayType, UnsupportedType, VoidType {
static KiwiType unsupported() {
return new UnsupportedType();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.ethelred.kiwiproc.processor;

import io.soabase.recordbuilder.core.RecordBuilderFull;
import java.util.*;
import java.util.stream.Collectors;
import javax.lang.model.element.VariableElement;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
package org.ethelred.kiwiproc.processor;

import io.soabase.recordbuilder.core.RecordBuilderFull;
import java.util.ArrayList;
import java.util.List;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.VariableElement;

@KiwiRecordBuilder
public record Signature(KiwiType returnType, String methodName, List<DAOResultColumn> params) {
public record Signature(KiwiType returnType, String methodName, List<String> paramNames) {
static Signature fromMethod(TypeUtils typeUtils, ExecutableElement element) {
List<DAOResultColumn> params = new ArrayList<>();
for (var parameterElement : element.getParameters()) {
// params.add(
// new DAOResultColumn(
// parameterElement.getSimpleName().toString(),
// SqlTypeMapping.get(JDBCType.INTEGER)) // TODO
// );
}
List<String> params = element.getParameters().stream()
.map(VariableElement::getSimpleName)
.map(Object::toString)
.toList();
return new Signature(
typeUtils.kiwiType(element.getReturnType()),
element.getSimpleName().toString(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.ethelred.kiwiproc.processor;

import io.soabase.recordbuilder.core.RecordBuilderFull;
import java.math.BigDecimal;
import java.sql.JDBCType;
import java.time.*;
Expand Down Expand Up @@ -73,10 +72,10 @@ public static SqlTypeMapping get(ColumnMetaData columnMetaData) {
}

public KiwiType kiwiType() {
if (jdbcType == JDBCType.ARRAY) {
assert componentType != null;
return new SqlArrayType(componentType.kiwiType());
}
if (jdbcType == JDBCType.ARRAY) {
assert componentType != null;
return new SqlArrayType(componentType.kiwiType());
}
var resolvedType = baseType;
if (isNullable) {
var maybeBoxed = CoreTypes.primitiveToBoxed.getByA(baseType);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
package org.ethelred.kiwiproc.processor;

public record TypeMapping(KiwiType source, KiwiType target) {
}
public record TypeMapping(KiwiType source, KiwiType target) {}
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
import javax.lang.model.element.Modifier;
import org.ethelred.kiwiproc.processor.*;

public class ImplGenerator {
public class InstanceGenerator {

private final Logger logger;
private final KiwiTypeConverter kiwiTypeConverter;
private final CoreTypes coreTypes;

public ImplGenerator(Logger logger, KiwiTypeConverter kiwiTypeConverter, CoreTypes coreTypes) {
public InstanceGenerator(Logger logger, KiwiTypeConverter kiwiTypeConverter, CoreTypes coreTypes) {
this.logger = logger;

this.kiwiTypeConverter = kiwiTypeConverter;
Expand All @@ -40,7 +40,6 @@ public JavaFile generate(DAOClassInfo classInfo) {
.addSuperinterface(daoName)
.addMethod(constructorSpec);
for (var methodThing : classInfo.methods()) {

typeSpecBuilder.addMethod(buildMethod(methodThing));
}
return JavaFile.builder(classInfo.packageName(), typeSpecBuilder.build())
Expand Down Expand Up @@ -84,7 +83,8 @@ private CodeBlock queryMethodBody(DAOMethodInfo methodInfo) {
var conversion = lookupConversion(parameterInfo::element, parameterInfo.mapper());
builder.addStatement(
"var $L = $L", name, conversion.conversionFormat().formatted(parameterInfo.javaAccessor()));
var nullableSource = parameterInfo.mapper().source() instanceof SimpleType simpleType && simpleType.isNullable();
var nullableSource =
parameterInfo.mapper().source() instanceof SimpleType simpleType && simpleType.isNullable();
if (nullableSource) {
builder.beginControlFlow("if ($L == null)", name)
.addStatement("statement.setNull($L, $L)", parameterInfo.index(), parameterInfo.sqlType())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,18 @@ public class PoetDAOGenerator implements DAOGenerator {
private final Filer filer;
private final DependencyInjectionStyle dependencyInjectionStyle;
private final KiwiTypeConverter kiwiTypeConverter;
private final ImplGenerator implGenerator;
private final InstanceGenerator instanceGenerator;
private final ProviderGenerator providerGenerator;
private final CoreTypes coreTypes;
private final TransactionManagerGenerator transactionManagerGenerator;

public PoetDAOGenerator(Logger logger, Filer filer, DependencyInjectionStyle dependencyInjectionStyle) {
this.logger = logger;
this.filer = filer;
this.dependencyInjectionStyle = dependencyInjectionStyle;
this.kiwiTypeConverter = new KiwiTypeConverter();
this.coreTypes = new CoreTypes();
this.implGenerator = new ImplGenerator(logger, kiwiTypeConverter, coreTypes);
this.instanceGenerator = new InstanceGenerator(logger, kiwiTypeConverter, coreTypes);
this.providerGenerator = new ProviderGenerator(dependencyInjectionStyle, kiwiTypeConverter);
this.transactionManagerGenerator = new TransactionManagerGenerator(dependencyInjectionStyle, kiwiTypeConverter);
}

@Override
Expand All @@ -36,16 +34,10 @@ public void generateProvider(DAOClassInfo classInfo) {

@Override
public void generateImpl(DAOClassInfo classInfo) {
var javaFile = implGenerator.generate(classInfo);
var javaFile = instanceGenerator.generate(classInfo);
writeJavaFile(classInfo::element, javaFile);
}

@Override
public void generateTransactionManager(DAODataSourceInfo dataSourceInfo) {
var javaFile = transactionManagerGenerator.generate(dataSourceInfo);
writeJavaFile(() -> null, javaFile);
}

private void writeJavaFile(ElementSupplier elementSupplier, JavaFile javaFile) {
javaFile = javaFile.toBuilder()
.skipJavaLangImports(true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import javax.lang.model.element.Modifier;
import javax.sql.DataSource;
import org.ethelred.kiwiproc.processor.DAOClassInfo;
import org.ethelred.kiwiproc.processor.DAOMethodInfo;
import org.ethelred.kiwiproc.processor.VoidType;
import org.ethelred.kiwiproc.processorconfig.DependencyInjectionStyle;

public class ProviderGenerator {
Expand Down Expand Up @@ -43,6 +45,7 @@ public JavaFile generate(DAOClassInfo classInfo) {
.singleton())
.addModifiers(Modifier.PUBLIC, Modifier.FINAL)
.superclass(superClass)
.addSuperinterface(daoName)
.addMethod(constructorSpec);
var withContextMethod = MethodSpec.methodBuilder("withContext")
.addModifiers(Modifier.PROTECTED)
Expand All @@ -52,7 +55,22 @@ public JavaFile generate(DAOClassInfo classInfo) {
.addStatement("return new $L(daoContext)", classInfo.className("Impl"))
.build();
typeSpecBuilder.addMethod(withContextMethod);
for (var methodThing : classInfo.methods()) {
typeSpecBuilder.addMethod(buildMethod(methodThing));
}
return JavaFile.builder(classInfo.packageName(), typeSpecBuilder.build())
.build();
}

private MethodSpec buildMethod(DAOMethodInfo methodInfo) {
var builder = MethodSpec.overriding(methodInfo.methodElement());
var signature = methodInfo.signature();
var params = String.join(", ", signature.paramNames());
if (signature.returnType() instanceof VoidType) {
builder.addStatement("run(dao -> dao.$L($L))", signature.methodName(), params);
} else {
builder.addStatement("return call(dao -> dao.$L($L))", signature.methodName(), params);
}
return builder.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
import com.palantir.javapoet.ClassName;

public final class RuntimeTypes {
public static final ClassName ABSTRACT_DAO = ClassName.get("org.ethelred.kiwiproc.impl", "AbstractDAO");
public static final ClassName ABSTRACT_DAO = ClassName.get("org.ethelred.kiwiproc.impl", "AbstractDAOInstance");
public static final ClassName ABSTRACT_PROVIDER =
ClassName.get("org.ethelred.kiwiproc.impl", "AbstractDAOProvider");
public static final ClassName ABSTRACT_TRANSACTION_MANAGER =
ClassName.get("org.ethelred.kiwiproc.impl", "AbstractTransactionManager");
ClassName.get("org.ethelred.kiwiproc.impl", "AbstractTransactionalDAO");
public static final ClassName DAO_CONTEXT = ClassName.get("org.ethelred.kiwiproc.api", "DAOContext");
public static final ClassName UNCHECKED_SQL_EXCEPTION =
ClassName.get("org.ethelred.kiwiproc.exception", "UncheckedSQLException");
Expand Down

This file was deleted.

12 changes: 0 additions & 12 deletions runtime/src/main/java/org/ethelred/kiwiproc/api/Batch.java

This file was deleted.

This file was deleted.

10 changes: 0 additions & 10 deletions runtime/src/main/java/org/ethelred/kiwiproc/api/DAOProvider.java

This file was deleted.

This file was deleted.

Loading

0 comments on commit 7f0cb2f

Please sign in to comment.