diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2013-08-11 22:35:02 +0200 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2013-08-11 22:35:02 +0200 |
commit | e78c0a1e4a4ebc71502dceccc9ae640862b7ce9e (patch) | |
tree | 38517a79011ee280d08da06ce20dba1ac7aebd62 /container-compiler-plugin/src/main/java/io/trygvis/persistence | |
parent | 41704c6a57d53b90b4e764ea4fb9c8ff81ced500 (diff) | |
download | container-playground-e78c0a1e4a4ebc71502dceccc9ae640862b7ce9e.tar.gz container-playground-e78c0a1e4a4ebc71502dceccc9ae640862b7ce9e.tar.bz2 container-playground-e78c0a1e4a4ebc71502dceccc9ae640862b7ce9e.tar.xz container-playground-e78c0a1e4a4ebc71502dceccc9ae640862b7ce9e.zip |
o Overhauling inheritance, adding a decent start of support for @MappedSuperclass.
Diffstat (limited to 'container-compiler-plugin/src/main/java/io/trygvis/persistence')
5 files changed, 86 insertions, 58 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 85523dc..3f6816d 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 @@ -1,39 +1,50 @@ package io.trygvis.persistence; +import io.trygvis.container.compiler.CompilerException; import io.trygvis.container.compiler.model.TypeRef; import java.util.ArrayList; import java.util.List; +import static java.util.Collections.addAll; + public class EntityMirror implements Comparable<EntityMirror> { public final GeneratorConfiguration generatorConfiguration; - public final List<FieldMirror> fields = new ArrayList<>(); - public final List<FieldMirror> idFields = new ArrayList<>(); + private final List<FieldMirror> fields = new ArrayList<>(); public final TypeRef type; + public final boolean concrete; public final String tableName; public final TypeRef rowType; public final TypeRef daoType; public final TypeRef utilsType; public TypeRef idType; + /** + * The nearest @MappedSuperclass + */ + public EntityMirror superEntity; - public EntityMirror(GeneratorConfiguration generatorConfiguration, TypeRef type, String tableName) { + public EntityMirror(GeneratorConfiguration generatorConfiguration, TypeRef type, boolean concrete, + EntityMirror superEntity, String tableName) { this.generatorConfiguration = generatorConfiguration; this.type = type; - this.tableName = tableName; + this.concrete = concrete; + this.superEntity = superEntity; - this.rowType = new TypeRef(type.plainName + "Row"); - this.daoType = new TypeRef(type.plainName + "Dao").args(type.args); - this.utilsType = new TypeRef(type.plainName + "Dao.Utils").args(type.args); + if (concrete) { + this.tableName = tableName; + this.rowType = new TypeRef(type.plainName + "Row"); + this.daoType = new TypeRef(type.plainName + "Dao").args(type.args); + this.utilsType = new TypeRef(type.plainName + "Dao.Utils").args(type.args); + } else { + this.tableName = null; + this.rowType = null; + this.daoType = null; + this.utilsType = null; + } } public void add(FieldMirror... fields) { - for (FieldMirror field : fields) { - this.fields.add(field); - if (field.id) { - this.idFields.add(field); - } - - } + addAll(this.fields, fields); } public void setIdType(TypeRef idType) { @@ -41,9 +52,25 @@ public class EntityMirror implements Comparable<EntityMirror> { } public FieldMirror getIdField() { - return idFields.get(0); + for (FieldMirror f : getFields()) { + if (f.id) { + return f; + } + } + + throw new CompilerException("Internal error: no @Id field defined on " + type); } + public List<FieldMirror> getFields() { + List<FieldMirror> fields = superEntity != null ? superEntity.getFields() : new ArrayList<FieldMirror>(); + fields.addAll(this.fields); + return fields; + } + + // ----------------------------------------------------------------------- + // + // ----------------------------------------------------------------------- + @Override public boolean equals(Object o) { if (this == o) return true; diff --git a/container-compiler-plugin/src/main/java/io/trygvis/persistence/generators/DaoGenerator.java b/container-compiler-plugin/src/main/java/io/trygvis/persistence/generators/DaoGenerator.java index c260a48..7cd3f59 100644 --- a/container-compiler-plugin/src/main/java/io/trygvis/persistence/generators/DaoGenerator.java +++ b/container-compiler-plugin/src/main/java/io/trygvis/persistence/generators/DaoGenerator.java @@ -1,6 +1,5 @@ package io.trygvis.persistence.generators; -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; @@ -123,7 +122,7 @@ public class DaoGenerator { Parameters parameters = new Parameters(); List<String> body = new ArrayList<>(); - for (FieldMirror field : entity.fields) { + for (FieldMirror field : entity.getFields()) { TypeRef type; if (field.fieldType == PRIMITIVE) { type = field.type; @@ -142,7 +141,7 @@ public class DaoGenerator { public String createTableSql() { List<String> columns = new ArrayList<>(); - for (FieldMirror field : entity.fields) { + for (FieldMirror field : entity.getFields()) { String s; if (field.fieldType == PRIMITIVE) { TypeHandler typeHandler = generatorConfiguration.typeHandler(field.type); @@ -156,18 +155,14 @@ public class DaoGenerator { } } 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); - s = " " + field.sqlName + " " + typeHandler.sqlType(field); - s += " REFERENCES " + referenced.tableName + "(" + idField.sqlName + ")"; - if (field.notNull) { - s += " NOT NULL"; - } else if (field.unique) { - s += " UNIQUE"; - } - } else { - throw new NotImplementedException(); + FieldMirror idField = referenced.getIdField(); + TypeHandler typeHandler = generatorConfiguration.typeHandler(idField.type); + s = " " + field.sqlName + " " + typeHandler.sqlType(field); + s += " REFERENCES " + referenced.tableName + "(" + idField.sqlName + ")"; + if (field.notNull) { + s += " NOT NULL"; + } else if (field.unique) { + s += " UNIQUE"; } } else { throw new RuntimeException("Unknown field type: " + field.getClass()); @@ -187,7 +182,7 @@ public class DaoGenerator { public String insertIntoSql() { List<String> columns = new ArrayList<>(); List<String> values = new ArrayList<>(); - for (FieldMirror field : entity.fields) { + for (FieldMirror field : entity.getFields()) { columns.add(field.sqlName); if (field.id) { values.add("nextval('" + unit.getDefaultSequence().sequenceName + "')"); @@ -202,9 +197,7 @@ public class DaoGenerator { public String deleteFromSql() { List<String> ss = new ArrayList<>(); - for (FieldMirror field : entity.idFields) { - ss.add(field.sqlName + "=?"); - } + ss.add(entity.getIdField().sqlName + "=?"); return "DELETE FROM " + entity.tableName + " WHERE " + join(ss, " AND ") + ";"; } @@ -221,12 +214,8 @@ public class DaoGenerator { body.add(entityListType + " list = runQuery(new Preparator() {"); body.add(" @Override"); body.add(" public void prepare(" + preparedStatementType.plainName + " stmt) throws " + sqlExceptionType + " {"); - List<FieldMirror> idFields = entity.idFields; - for (int i = 0; i < idFields.size(); i++) { - FieldMirror field = idFields.get(i); - TypeHandler typeHandler = generatorConfiguration.typeHandler(field.type); - body.add(" stmt." + typeHandler.setter(i + 1, idP.name) + ";"); - } + TypeHandler typeHandler = generatorConfiguration.typeHandler(entity.getIdField().type); + body.add(" stmt." + typeHandler.setter(1, idP.name) + ";"); body.add(" }"); body.add("}, sql);"); body.add("if (list.isEmpty()) {"); @@ -257,8 +246,8 @@ public class DaoGenerator { List<String> body = new ArrayList<>(); List<String> fields = new ArrayList<>(); - for (int i = 0; i < entity.fields.size(); i++) { - FieldMirror field = entity.fields.get(i); + for (int i = 0; i < entity.getFields().size(); i++) { + FieldMirror field = entity.getFields().get(i); String getter; TypeRef type; @@ -290,8 +279,8 @@ public class DaoGenerator { List<String> body = new ArrayList<>(); List<String> cArgs = new ArrayList<>(); List<String> method = new ArrayList<>(); - for (int i = 0; i < this.entity.fields.size(); i++) { - FieldMirror field = this.entity.fields.get(i); + for (int i = 0; i < entity.getFields().size(); i++) { + FieldMirror field = this.entity.getFields().get(i); String value; if (field.fieldType == PRIMITIVE) { @@ -330,7 +319,7 @@ public class DaoGenerator { public String defaultFields() { List<String> names = new ArrayList<>(); - for (FieldMirror field : entity.fields) { + for (FieldMirror field : entity.getFields()) { names.add(field.sqlName); } diff --git a/container-compiler-plugin/src/main/java/io/trygvis/persistence/generators/DaoUtilsGenerator.java b/container-compiler-plugin/src/main/java/io/trygvis/persistence/generators/DaoUtilsGenerator.java index e4f69b5..08a7f45 100644 --- a/container-compiler-plugin/src/main/java/io/trygvis/persistence/generators/DaoUtilsGenerator.java +++ b/container-compiler-plugin/src/main/java/io/trygvis/persistence/generators/DaoUtilsGenerator.java @@ -68,7 +68,7 @@ public class DaoUtilsGenerator { body.add("try (" + psType + " stmt = " + con.name + ".prepareStatement(insertIntoSql)) {"); int i = 0; - for (FieldMirror field : entity.fields) { + for (FieldMirror field : entity.getFields()) { // Assume all ID fields are generated for now. if (field.id) { continue; @@ -133,12 +133,11 @@ public class DaoUtilsGenerator { List<String> arguments = new ArrayList<>(); arguments.add(con.name); - for (FieldMirror field : entity.idFields) { - if (field.getterType == GetterType.FIELD) { - arguments.add(o.name + "." + field.javaName); - } else { - arguments.add(o.name + "." + toGetterName(field.javaName) + "()"); - } + FieldMirror field = entity.getIdField(); + if (field.getterType == GetterType.FIELD) { + arguments.add(o.name + "." + field.javaName); + } else { + arguments.add(o.name + "." + toGetterName(field.javaName) + "()"); } return new MethodRef(PUBLIC | STATIC, TypeRef.VOID, "delete" + entity.type.className, p, @@ -154,12 +153,10 @@ public class DaoUtilsGenerator { List<String> body = new ArrayList<>(); body.add("try (" + psType + " stmt = " + con.name + ".prepareStatement(deleteFromSql)) {"); - for (int i = 0; i < entity.idFields.size(); i++) { - FieldMirror field = entity.idFields.get(i); - p.addParameter(field.type, field.javaName); - TypeHandler typeHandler = generatorConfiguration.typeHandler(field.type); - body.add(" stmt." + typeHandler.setter(i + 1, field.javaName) + ";"); - } + FieldMirror field = entity.getIdField(); + p.addParameter(field.type, field.javaName); + TypeHandler typeHandler = generatorConfiguration.typeHandler(field.type); + body.add(" stmt." + typeHandler.setter(1, field.javaName) + ";"); body.add(" stmt.executeUpdate();"); body.add("}"); diff --git a/container-compiler-plugin/src/main/java/io/trygvis/persistence/generators/SqlSessionFactoryGenerator.java b/container-compiler-plugin/src/main/java/io/trygvis/persistence/generators/SqlSessionFactoryGenerator.java index 3f0da2b..7b6f667 100644 --- a/container-compiler-plugin/src/main/java/io/trygvis/persistence/generators/SqlSessionFactoryGenerator.java +++ b/container-compiler-plugin/src/main/java/io/trygvis/persistence/generators/SqlSessionFactoryGenerator.java @@ -34,6 +34,10 @@ public class SqlSessionFactoryGenerator { List<String> s = new ArrayList<>(); for (EntityMirror entity : unit.getEntities().values()) { + if (!entity.concrete) { + continue; + } + s.add(entity.daoType.plainName + ".desc"); } TypeRef sqlEntityMetaArrayType = new TypeRef(SqlEntityMeta[].class); diff --git a/container-compiler-plugin/src/main/java/io/trygvis/persistence/generators/SqlSessionGenerator.java b/container-compiler-plugin/src/main/java/io/trygvis/persistence/generators/SqlSessionGenerator.java index ddb4d99..8e32cfe 100644 --- a/container-compiler-plugin/src/main/java/io/trygvis/persistence/generators/SqlSessionGenerator.java +++ b/container-compiler-plugin/src/main/java/io/trygvis/persistence/generators/SqlSessionGenerator.java @@ -33,6 +33,10 @@ public class SqlSessionGenerator { Map<EntityMirror, FieldRef> daoFields = new HashMap<>(); for (EntityMirror entity : unit.getEntities().values()) { + if (!entity.concrete) { + continue; + } + FieldRef f = g.addPublicFinalField(entity.daoType, toFieldName(entity.type.className)); daoFields.put(entity, f); } @@ -50,6 +54,10 @@ public class SqlSessionGenerator { body.add("super(" + c.name + ");"); for (EntityMirror entity : unit.getEntities().values()) { + if (!entity.concrete) { + continue; + } + FieldRef f = daoFields.get(entity); body.add("this." + f.name + " = new " + entity.daoType.plainName + "(this);"); } @@ -63,6 +71,9 @@ public class SqlSessionGenerator { p.addParameter(new TypeRef(Class.class).args("T"), "klass"); List<String> body = new ArrayList<>(); for (EntityMirror entity : unit.getEntities().values()) { + if(!entity.concrete) { + continue; + } String sqlDao = "SqlDao<Id, T, " + unit.sessionType.className + ", " + entity.rowType.className + ">"; body.add("if (klass == " + entity.type.plainName + ".class) {"); body.add(" return (" + sqlDao + ") " + daoFields.get(entity).name + ";"); |