summaryrefslogtreecommitdiff
path: root/container-compiler-plugin/src/main/java/io/trygvis/persistence/EntityMirror.java
diff options
context:
space:
mode:
Diffstat (limited to 'container-compiler-plugin/src/main/java/io/trygvis/persistence/EntityMirror.java')
-rw-r--r--container-compiler-plugin/src/main/java/io/trygvis/persistence/EntityMirror.java75
1 files changed, 57 insertions, 18 deletions
diff --git a/container-compiler-plugin/src/main/java/io/trygvis/persistence/EntityMirror.java b/container-compiler-plugin/src/main/java/io/trygvis/persistence/EntityMirror.java
index 5d6defb..140a910 100644
--- a/container-compiler-plugin/src/main/java/io/trygvis/persistence/EntityMirror.java
+++ b/container-compiler-plugin/src/main/java/io/trygvis/persistence/EntityMirror.java
@@ -3,6 +3,7 @@ package io.trygvis.persistence;
import io.trygvis.container.compiler.NotImplementedException;
import io.trygvis.container.compiler.SqlUnitModel;
import io.trygvis.container.compiler.model.ClassG;
+import io.trygvis.container.compiler.model.Constructor;
import io.trygvis.container.compiler.model.Imports;
import io.trygvis.container.compiler.model.MethodRef;
import io.trygvis.container.compiler.model.Parameters;
@@ -38,6 +39,8 @@ public class EntityMirror implements Comparable<EntityMirror> {
public final TypeRef type;
public final String tableName;
public final TypeRef daoType;
+ public final TypeRef utilsType;
+ public TypeRef idType;
public EntityMirror(GeneratorConfiguration generatorConfiguration, TypeRef type, String tableName) {
this.generatorConfiguration = generatorConfiguration;
@@ -45,6 +48,7 @@ public class EntityMirror implements Comparable<EntityMirror> {
this.tableName = tableName;
this.daoType = new TypeRef(type.plainName + "Dao").args(type.args);
+ this.utilsType = new TypeRef(type.plainName + ".Utils").args(type.args);
}
public void add(FieldMirror... fields) {
@@ -57,6 +61,10 @@ public class EntityMirror implements Comparable<EntityMirror> {
}
}
+ public void setIdType(TypeRef idType) {
+ this.idType = idType;
+ }
+
public FieldMirror getIdField() {
return idFields.get(0);
}
@@ -180,7 +188,7 @@ public class EntityMirror implements Comparable<EntityMirror> {
body.add(setter);
} else {
body.add(" " + field.type + " " + field.javaName + " = " + accessor + ";");
- body.add(" if(" + field.javaName + " == null) {");
+ body.add(" if (" + field.javaName + " == null) {");
body.add(" stmt.setNull(" + i + ", " + typesType + "." + typeHandler.typeName() + ");");
body.add(" } else {");
body.add(" " + setter);
@@ -189,7 +197,23 @@ public class EntityMirror implements Comparable<EntityMirror> {
}
body.add(" stmt.executeUpdate();");
body.add("}");
- return new MethodRef(PUBLIC | STATIC, TypeRef.VOID, "insertInto", p, body).exception(sqlExceptionType);
+ return new MethodRef(PUBLIC | STATIC, TypeRef.VOID, "insert" + type.className, p, body).exception(sqlExceptionType);
+ }
+
+ public MethodRef selectById(Imports imports) {
+ Parameters p = new Parameters();
+ p.addParameter(imports.add(Connection.class), "c");
+ p.addParameter(idType, "id");
+ return new MethodRef(PUBLIC | STATIC, type, "select" + type.className + "ById", p,
+ "throw new UnsupportedOperationException(\"Not implemented\");");
+ }
+
+ public MethodRef update(Imports imports) {
+ Parameters p = new Parameters();
+ p.addParameter(imports.add(Connection.class), "c");
+ p.addParameter(type, "entity");
+ return new MethodRef(PUBLIC | STATIC, type, "update" + type.className, p,
+ "throw new UnsupportedOperationException(\"Not implemented\");");
}
public MethodRef delete(Imports imports) {
@@ -208,11 +232,9 @@ public class EntityMirror implements Comparable<EntityMirror> {
arguments.add(o.name + "." + toGetterName(field.javaName) + "()");
}
}
- List<String> body = new ArrayList<>();
- body.add("deleteById(" + join(arguments, ", ") + ");");
- return new MethodRef(PUBLIC | STATIC, TypeRef.VOID, "delete", p, body).
- exception(imports.add(SQLException.class));
+ return new MethodRef(PUBLIC | STATIC, TypeRef.VOID, "delete" + type.className, p,
+ "delete" + type.className + "ById(" + join(arguments, ", ") + ");").exception(imports.add(SQLException.class));
}
public MethodRef deleteById(Imports imports) {
@@ -233,22 +255,29 @@ public class EntityMirror implements Comparable<EntityMirror> {
body.add(" stmt.executeUpdate();");
body.add("}");
- return new MethodRef(PUBLIC | STATIC, TypeRef.VOID, "deleteById", p, body).
+ return new MethodRef(PUBLIC | STATIC, TypeRef.VOID, "delete" + type.className + "ById", p, body).
exception(imports.add(SQLException.class));
}
public ClassG queryType(Imports imports) {
- TypeRef abstractQueryType = imports.add(AbstractTypedQuery.class).args(type);
+ TypeRef sqlQueryType = imports.add(AbstractTypedQuery.class).args(type);
TypeRef conType = imports.add(Connection.class);
TypeRef entityTypedQuery = new TypeRef(type.className + "TypedQuery");
+ TypeRef sqlExceptionType = new TypeRef(SQLException.class);
Parameters p = new Parameters();
ParameterRef c = p.addParameter(conType, "c");
-
- ClassG typedQuery = new ClassG(PUBLIC | STATIC, entityTypedQuery).
- extendsType(abstractQueryType);
- typedQuery.addConstructor(p, singletonList("super(" + c.name + ", " + daoType.className + ".desc);"));
- return typedQuery;
+ Constructor constructor = new Constructor(p, singletonList("super(" + c.name + ", " + daoType.className + ".desc);"));
+ ClassG g = new ClassG(PUBLIC | STATIC, entityTypedQuery).
+ extendsType(sqlQueryType).
+ add(constructor);
+ p = new Parameters();
+ ParameterRef rs = p.addParameter(new TypeRef(ResultSet.class), "rs");
+ MethodRef fromResultSet = new MethodRef(PUBLIC, type, "fromResultSet", p,
+ "return " + utilsType.className + ".fromResultSet" + type.className + "(" + rs.name + ");").
+ exception(sqlExceptionType);
+ g.add(fromResultSet);
+ return g;
}
public MethodRef query(Imports imports) {
@@ -258,10 +287,8 @@ public class EntityMirror implements Comparable<EntityMirror> {
Parameters p = new Parameters();
ParameterRef c = p.addParameter(conType, "c");
-
- List<String> body = new ArrayList<>();
- body.add("return new " + entityTypedQuery + "(" + c.name + ");");
- return new MethodRef(PUBLIC | STATIC, typedQueryType, "query", p, body);
+ return new MethodRef(PUBLIC | STATIC, typedQueryType, "query" + type.className, p,
+ "return new " + entityTypedQuery + "(" + c.name + ");");
}
public MethodRef fromResultSet(Imports g) {
@@ -313,7 +340,7 @@ public class EntityMirror implements Comparable<EntityMirror> {
body.add("return returnValue;");
- return new MethodRef(PUBLIC, type, "fromResultSet", p, body).
+ return new MethodRef(PUBLIC | STATIC, type, "fromResultSet" + type.className, p, body).
exception(g.add(SQLException.class));
}
@@ -336,4 +363,16 @@ public class EntityMirror implements Comparable<EntityMirror> {
public int compareTo(@SuppressWarnings("NullableProblems") EntityMirror o) {
return type.compareTo(o.type);
}
+
+ public ClassG utils(SqlUnitModel unit) {
+ ClassG g = new ClassG(PUBLIC | STATIC, utilsType);
+ g.add(insertInto(unit, g.imports));
+ g.add(selectById(g.imports));
+ g.add(update(g.imports));
+ g.add(delete(g.imports));
+ g.add(deleteById(g.imports));
+ g.add(query(g.imports));
+ g.add(fromResultSet(g.imports));
+ return g;
+ }
}