summaryrefslogtreecommitdiff
path: root/container-compiler-plugin/src/main/java/io
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2013-08-10 19:12:49 +0200
committerTrygve Laugstøl <trygvis@inamo.no>2013-08-10 19:20:24 +0200
commit01557b4626d184f58c492c3d4c1fc74cb9a29a1e (patch)
treedb8b1ab51c7a861f239642ffe49378bb79b132f9 /container-compiler-plugin/src/main/java/io
parent46cb0cef9303940ade51ffe16831f55ee9cd8e1f (diff)
downloadcontainer-playground-01557b4626d184f58c492c3d4c1fc74cb9a29a1e.tar.gz
container-playground-01557b4626d184f58c492c3d4c1fc74cb9a29a1e.tar.bz2
container-playground-01557b4626d184f58c492c3d4c1fc74cb9a29a1e.tar.xz
container-playground-01557b4626d184f58c492c3d4c1fc74cb9a29a1e.zip
o Getting selectById() to work with references.
Diffstat (limited to 'container-compiler-plugin/src/main/java/io')
-rw-r--r--container-compiler-plugin/src/main/java/io/trygvis/persistence/generators/DaoGenerator.java43
-rw-r--r--container-compiler-plugin/src/main/java/io/trygvis/persistence/generators/DaoUtilsGenerator.java6
2 files changed, 35 insertions, 14 deletions
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<String> 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<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) + ";");
+ }
+ 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<String> 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<String> 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);