From 01557b4626d184f58c492c3d4c1fc74cb9a29a1e Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Sat, 10 Aug 2013 19:12:49 +0200 Subject: o Getting selectById() to work with references. --- .../persistence/generators/DaoGenerator.java | 43 ++++++++++++++++------ .../persistence/generators/DaoUtilsGenerator.java | 6 +-- 2 files changed, 35 insertions(+), 14 deletions(-) (limited to 'container-compiler-plugin/src/main/java/io') 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 c4367fd..c260a48 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 @@ -17,6 +17,7 @@ import io.trygvis.persistence.sql.SqlDao; import io.trygvis.persistence.sql.SqlEntityMeta; import java.io.IOException; +import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; @@ -87,14 +88,7 @@ public class DaoGenerator { exception(sqlException)); } - { - p = new Parameters(); - Parameters.ParameterRef id = p.addParameter(entity.idType, "id"); - g.add(new MethodRef(PUBLIC, entity.type, "selectById", p, - "return Utils.select" + entity.type.className + "ById(super.session.c, " + id.name + ");"). - exception(sqlException)); - } - + g.add(selectById(g.imports)); g.add(selectWhere(g.imports)); { @@ -215,6 +209,34 @@ public class DaoGenerator { return "DELETE FROM " + entity.tableName + " WHERE " + join(ss, " AND ") + ";"; } + public MethodRef selectById(Imports imports) { + TypeRef preparedStatementType = imports.add(PreparedStatement.class); + TypeRef sqlExceptionType = imports.add(SQLException.class); + Parameters p = new Parameters(); + Parameters.ParameterRef idP = p.addParameter(entity.idType, "id"); + TypeRef entityListType = imports.add(List.class).args(entity.type); + List body = new ArrayList<>(); + FieldMirror id = entity.getIdField(); + body.add("String sql = \"SELECT \" + desc.defaultFields + \" FROM " + entity.tableName + " WHERE " + id.sqlName + "=?\";"); + body.add(entityListType + " list = runQuery(new Preparator() {"); + body.add(" @Override"); + body.add(" public void prepare(" + preparedStatementType.plainName + " stmt) throws " + sqlExceptionType + " {"); + List 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) + ";"); + } + body.add(" }"); + body.add("}, sql);"); + body.add("if (list.isEmpty()) {"); + body.add(" return null;"); + body.add("}"); + body.add("return list.get(0);"); + return new MethodRef(PUBLIC, entity.type, "selectById", p, body). + exception(imports.add(SQLException.class)); + } + public MethodRef selectWhere(Imports imports) { TypeRef sqlExceptionType = imports.add(SQLException.class); Parameters p = new Parameters(); @@ -272,10 +294,9 @@ public class DaoGenerator { FieldMirror field = this.entity.fields.get(i); String value; - if(field.fieldType == PRIMITIVE) { + if (field.fieldType == PRIMITIVE) { value = row.name + "." + field.javaName; - } - else { + } else { EntityMirror referencedEntity = unit.getEntities().get(field.type); // String x = field.type.plainName + " " + field.javaName + " = " + value = "session." + toFieldName(referencedEntity.type) + ".selectById(" + row.name + "." + field.javaName + ")"; 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 2848c8e..51b3879 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 @@ -43,7 +43,7 @@ public class DaoUtilsGenerator { public ClassG utils() { ClassG g = new ClassG(PUBLIC | STATIC, entity.utilsType); g.add(insertInto(g.imports)); - g.add(selectById(g.imports)); +// g.add(selectById(g.imports)); // g.add(selectWhere(g.imports)); g.add(update(g.imports)); g.add(delete(g.imports)); @@ -66,7 +66,7 @@ public class DaoUtilsGenerator { List body = new ArrayList<>(); - body.add("try(" + psType + " stmt = " + con.name + ".prepareStatement(insertIntoSql)) {"); + body.add("try (" + psType + " stmt = " + con.name + ".prepareStatement(insertIntoSql)) {"); int i = 0; for (FieldMirror field : entity.fields) { // Assume all ID fields are generated for now. @@ -153,7 +153,7 @@ public class DaoUtilsGenerator { List body = new ArrayList<>(); - body.add("try(" + psType + " stmt = " + con.name + ".prepareStatement(deleteFromSql)) {"); + 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); -- cgit v1.2.3