diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2013-08-04 17:17:39 +0200 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2013-08-04 17:17:39 +0200 |
commit | 381c368b8907640bdd7cd81db2294724e082a119 (patch) | |
tree | ee35a0ec90200e0b344aa36f2cabe13153331bed /container-compiler-plugin/src/main/java/io/trygvis/persistence | |
parent | 3d556bfcad3eb3face9db986aa570d6e2a059448 (diff) | |
download | container-playground-381c368b8907640bdd7cd81db2294724e082a119.tar.gz container-playground-381c368b8907640bdd7cd81db2294724e082a119.tar.bz2 container-playground-381c368b8907640bdd7cd81db2294724e082a119.tar.xz container-playground-381c368b8907640bdd7cd81db2294724e082a119.zip |
wip
Diffstat (limited to 'container-compiler-plugin/src/main/java/io/trygvis/persistence')
-rw-r--r-- | container-compiler-plugin/src/main/java/io/trygvis/persistence/EntityMirror.java | 60 | ||||
-rw-r--r-- | container-compiler-plugin/src/main/java/io/trygvis/persistence/FieldMirror.java | 62 |
2 files changed, 68 insertions, 54 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 48f8adc..f1970f7 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 @@ -18,9 +18,13 @@ import java.sql.Types; import java.util.ArrayList; import java.util.List; +import static io.trygvis.container.compiler.Utils.toGetterName; +import static io.trygvis.container.compiler.Utils.toSetterName; import static io.trygvis.container.compiler.model.Parameters.ParameterRef; -import static io.trygvis.persistence.FieldMirror.PrimitiveFieldMirror; -import static io.trygvis.persistence.FieldMirror.ReferenceFieldMirror; +import static io.trygvis.persistence.FieldMirror.AccessorType.FIELD; +import static io.trygvis.persistence.FieldMirror.AccessorType.METHOD; +import static io.trygvis.persistence.FieldMirror.FieldType.PRIMITIVE; +import static io.trygvis.persistence.FieldMirror.FieldType.REFERENCE; import static java.lang.String.format; import static java.lang.reflect.Modifier.PUBLIC; import static java.lang.reflect.Modifier.STATIC; @@ -61,7 +65,7 @@ public class EntityMirror implements Comparable<EntityMirror> { List<String> columns = new ArrayList<>(); for (FieldMirror field : fields) { String s; - if (field instanceof PrimitiveFieldMirror) { + if (field.fieldType == PRIMITIVE) { TypeHandler typeHandler = generatorConfiguration.typeHandler(field.type); s = " " + field.sqlName + " " + typeHandler.sqlType(field); if (field.id) { @@ -71,9 +75,8 @@ public class EntityMirror implements Comparable<EntityMirror> { } else if (field.unique) { s += " UNIQUE"; } - } else if (field instanceof ReferenceFieldMirror) { - ReferenceFieldMirror ref = (ReferenceFieldMirror) field; - EntityMirror referenced = unit.get(ref.type); + } else if (field.fieldType == REFERENCE) { + EntityMirror referenced = unit.get(field.type); if (referenced.idFields.size() == 1) { FieldMirror idField = referenced.idFields.get(0); TypeHandler typeHandler = generatorConfiguration.typeHandler(idField.type); @@ -157,7 +160,7 @@ public class EntityMirror implements Comparable<EntityMirror> { i++; - if (field instanceof PrimitiveFieldMirror) { + if (field.fieldType == PRIMITIVE) { TypeHandler typeHandler = generatorConfiguration.typeHandler(field.type); String access = o.name + "." + field.javaName; String setter = " stmt." + typeHandler.setter(i, access) + ";"; @@ -172,9 +175,8 @@ public class EntityMirror implements Comparable<EntityMirror> { body.add(" " + setter); body.add(" }"); } - } else if (field instanceof ReferenceFieldMirror) { - ReferenceFieldMirror ref = (ReferenceFieldMirror) field; - EntityMirror referenced = unit.get(ref.type); + } else if (field.fieldType == REFERENCE) { + EntityMirror referenced = unit.get(field.type); FieldMirror idField = referenced.getIdField(); TypeHandler typeHandler = generatorConfiguration.typeHandler(idField.type); body.add(" stmt." + typeHandler.setter(i, o.name + "." + field.javaName + "." + idField.javaName) + ";"); @@ -195,7 +197,11 @@ public class EntityMirror implements Comparable<EntityMirror> { List<String> arguments = new ArrayList<>(); arguments.add(con.name); for (FieldMirror field : idFields) { - arguments.add(o.name + "." + field.javaName); + if(field.accessorType == FIELD) { + arguments.add(o.name + "." + field.javaName); + } else { + arguments.add(o.name + "." + toGetterName(field.javaName) + "()"); + } } List<String> body = new ArrayList<>(); body.add("deleteById(" + join(arguments, ", ") + ");"); @@ -262,10 +268,13 @@ public class EntityMirror implements Comparable<EntityMirror> { List<String> names = new ArrayList<>(); for (int i = 0; i < fields.size(); i++) { FieldMirror field = fields.get(i); - if (field instanceof PrimitiveFieldMirror) { + if (field.accessorType != FIELD) { + continue; + } + if (field.fieldType == PRIMITIVE) { TypeHandler typeHandler = generatorConfiguration.typeHandler(field.type); body.add(field.type + " " + field.javaName + " = " + typeHandler.getter(rs.name, i + 1) + ";"); - } else if (field instanceof ReferenceFieldMirror) { + } else if (field.fieldType == REFERENCE) { // ReferenceFieldMirror ref = (ReferenceFieldMirror) field; // EntityMirror referenced = unit.get(ref.type); // FieldMirror idField = referenced.getIdField(); @@ -274,9 +283,32 @@ public class EntityMirror implements Comparable<EntityMirror> { body.add(field.type + " " + field.javaName + " = null;"); } names.add(field.javaName); + i++; + } + + body.add(type + " returnValue = new " + type + "(" + join(names, ", ") + ");"); + + for (int i = 0; i < fields.size(); i++) { + FieldMirror field = fields.get(i); + if (field.accessorType != METHOD) { + continue; + } + if (field.fieldType == PRIMITIVE) { + TypeHandler typeHandler = generatorConfiguration.typeHandler(field.type); + body.add("returnValue." + toSetterName(field.javaName) + "(" + typeHandler.getter(rs.name, i + 1) + ");"); + } else if (field.fieldType == REFERENCE) { +// ReferenceFieldMirror ref = (ReferenceFieldMirror) field; +// EntityMirror referenced = unit.get(ref.type); +// FieldMirror idField = referenced.getIdField(); +// TypeHandler typeHandler = generatorConfiguration.typeHandler(idField.type); +// body.add(field.type + " " + field.javaName + " = " + typeHandler.getter(rs.name, i + 1) + ";"); + body.add("returnValue." + toSetterName(field.javaName) + "(" + null + ");"); + } + names.add(field.javaName); + i++; } - body.add("return new " + type + "(" + join(names, ", ") + ");"); + body.add("return returnValue;"); return new MethodRef(PUBLIC, type, "fromResultSet", p, body). exception(g.add(SQLException.class)); diff --git a/container-compiler-plugin/src/main/java/io/trygvis/persistence/FieldMirror.java b/container-compiler-plugin/src/main/java/io/trygvis/persistence/FieldMirror.java index f92cc2c..07fa769 100644 --- a/container-compiler-plugin/src/main/java/io/trygvis/persistence/FieldMirror.java +++ b/container-compiler-plugin/src/main/java/io/trygvis/persistence/FieldMirror.java @@ -2,14 +2,12 @@ package io.trygvis.persistence; import io.trygvis.container.compiler.model.TypeRef; -import static io.trygvis.persistence.FieldMirror.FieldType.PRIMITIVE; -import static io.trygvis.persistence.FieldMirror.FieldType.REFERENCE; - /** * TODO: a single field might have to be mapped to multiple sql columns. */ -public abstract class FieldMirror { +public class FieldMirror { public final FieldType fieldType; + public final AccessorType accessorType; public final TypeRef type; public final String javaName; public final String sqlName; @@ -22,8 +20,15 @@ public abstract class FieldMirror { REFERENCE, } - protected FieldMirror(FieldType fieldType, TypeRef type, String javaName, String sqlName, boolean id, boolean notNull, boolean unique) { + public enum AccessorType { + FIELD, + METHOD, + } + + public FieldMirror(FieldType fieldType, AccessorType accessorType, TypeRef type, String javaName, String sqlName, + boolean id, boolean notNull, boolean unique) { this.fieldType = fieldType; + this.accessorType = accessorType; this.type = type; this.javaName = javaName; this.sqlName = sqlName; @@ -32,40 +37,17 @@ public abstract class FieldMirror { this.unique = unique; } - public static class PrimitiveFieldMirror extends FieldMirror { - public PrimitiveFieldMirror(TypeRef typeRef, String javaName, String sqlName, boolean id, boolean notNull, boolean unique) { - super(PRIMITIVE, typeRef, javaName, sqlName, id, notNull, unique); - } - - @Override - public String toString() { - return "PrimitiveFieldMirror{" + - "type='" + type + '\'' + - ", javaName='" + javaName + '\'' + - ", sqlName='" + sqlName + '\'' + - ", notNull=" + notNull + - ", unique=" + unique + - '}'; - } - } - - public static class ReferenceFieldMirror extends FieldMirror { - public ReferenceFieldMirror(TypeRef typeRef, String javaName, String sqlName, boolean notNull, boolean unique) { - super(REFERENCE, typeRef, javaName, sqlName, false, notNull, unique); - } - - @Override - public String toString() { - return "ReferenceFieldMirror{" + - "type='" + type + '\'' + - ", javaName='" + javaName + '\'' + - ", sqlName='" + sqlName + '\'' + - ", notNull=" + notNull + - ", unique=" + unique + - '}'; - } - } - @Override - public abstract String toString(); + public String toString() { + return "FieldMirror{" + + "fieldType=" + fieldType + + ", accessorType=" + accessorType + + ", type=" + type + + ", javaName='" + javaName + '\'' + + ", sqlName='" + sqlName + '\'' + + ", id=" + id + + ", notNull=" + notNull + + ", unique=" + unique + + '}'; + } } |