From c0c9c358e8703c1af917d7270adbb04160ad34b3 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Sun, 4 Aug 2013 10:24:43 +0200 Subject: wip --- .../java/io/trygvis/persistence/EntityMirror.java | 103 +++++++++++++-------- 1 file changed, 65 insertions(+), 38 deletions(-) (limited to 'container-compiler-plugin/src/main/java/io/trygvis/persistence/EntityMirror.java') 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 74c4829..e06855f 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,16 +3,18 @@ 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.Imports; +import io.trygvis.container.compiler.model.MethodRef; import io.trygvis.container.compiler.model.Parameters; import io.trygvis.container.compiler.model.TypeRef; import io.trygvis.persistence.sql.AbstractTypedQuery; -import io.trygvis.persistence.sql.SqlEntityDesc; import javax.persistence.TypedQuery; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; +import java.sql.Types; import java.util.ArrayList; import java.util.List; @@ -41,10 +43,13 @@ public class EntityMirror implements Comparable { this.daoType = new TypeRef(type.plainName + "Dao").args(type.args); } - public void add(FieldMirror field) { - fields.add(field); - if (field.id) { - idFields.add(field); + public void add(FieldMirror... fields) { + for (FieldMirror field : fields) { + this.fields.add(field); + if (field.id) { + this.idFields.add(field); + } + } } @@ -128,9 +133,11 @@ public class EntityMirror implements Comparable { return join(names, ", "); } - public void insertInto(SqlUnitModel unit, ClassG g) { - TypeRef conType = g.addImport(Connection.class); - TypeRef psType = g.addImport(PreparedStatement.class); + public MethodRef insertInto(SqlUnitModel unit, Imports imports) { + TypeRef sqlExceptionType = imports.add(SQLException.class); + TypeRef typesType = imports.add(Types.class); + TypeRef conType = imports.add(Connection.class); + TypeRef psType = imports.add(PreparedStatement.class); Parameters p = new Parameters(); ParameterRef con = p.addParameter(conType, "con"); ParameterRef o = p.addParameter(type, "o"); @@ -142,7 +149,20 @@ public class EntityMirror implements Comparable { FieldMirror field = fields.get(i); if(field instanceof PrimitiveFieldMirror) { TypeHandler typeHandler = generatorConfiguration.typeHandler(field.type); - body.add(" stmt." + typeHandler.setter(i + 1, o.name + "." + field.javaName) + ";"); + String access = o.name + "." + field.javaName; + String setter = " stmt." + typeHandler.setter(i + 1, access) + ";"; + + if(field.notNull) { + body.add(setter); + } + else { + body.add(" " + field.type + " " + field.javaName + " = " + access + ";"); + body.add(" if(" + field.javaName + " == null) {"); + body.add(" stmt.setNull(" + (i + 1) + ", " + typesType + "." + typeHandler.typeName() + ");"); + body.add(" } else {"); + body.add(" " + setter); + body.add(" }"); + } } else if (field instanceof ReferenceFieldMirror) { ReferenceFieldMirror ref = (ReferenceFieldMirror) field; EntityMirror referenced = unit.get(ref.type); @@ -153,14 +173,12 @@ public class EntityMirror implements Comparable { } body.add(" stmt.executeUpdate();"); body.add("}"); - - g.addStaticMethod(body, TypeRef.VOID, "insertInto", p). - exception(g.addImport(SQLException.class)); + return new MethodRef(PUBLIC | STATIC, TypeRef.VOID, "insertInto", p, body).exception(sqlExceptionType); } - public void delete(ClassG g) { - TypeRef conType = g.addImport(Connection.class); - TypeRef objectType = g.addImport(type); + public MethodRef delete(Imports imports) { + TypeRef conType = imports.add(Connection.class); + TypeRef objectType = imports.add(type); Parameters p = new Parameters(); ParameterRef con = p.addParameter(conType, "con"); ParameterRef o = p.addParameter(objectType, "o"); @@ -173,13 +191,13 @@ public class EntityMirror implements Comparable { List body = new ArrayList<>(); body.add("deleteById(" + join(arguments, ", ") + ");"); - g.addStaticMethod(body, TypeRef.VOID, "delete", p). - exception(g.addImport(SQLException.class)); + return new MethodRef(PUBLIC | STATIC, TypeRef.VOID, "delete", p, body). + exception(imports.add(SQLException.class)); } - public void deleteById(ClassG g) { - TypeRef conType = g.addImport(Connection.class); - TypeRef psType = g.addImport(PreparedStatement.class); + public MethodRef deleteById(Imports imports) { + TypeRef conType = imports.add(Connection.class); + TypeRef psType = imports.add(PreparedStatement.class); Parameters p = new Parameters(); ParameterRef con = p.addParameter(conType, "con"); @@ -195,30 +213,39 @@ public class EntityMirror implements Comparable { body.add(" stmt.executeUpdate();"); body.add("}"); - g.addStaticMethod(body, TypeRef.VOID, "deleteById", p). - exception(g.addImport(SQLException.class)); + return new MethodRef(PUBLIC | STATIC, TypeRef.VOID, "deleteById", p, body). + exception(imports.add(SQLException.class)); } - public void query(SqlUnitModel sqlUnit, ClassG g) { - TypeRef conType = g.addImport(Connection.class); - TypeRef abstractQueryType = g.addImport(AbstractTypedQuery.class).args(type); - TypeRef typedQueryType = g.addImport(TypedQuery.class).args(type); + public ClassG queryType(Imports imports) { + TypeRef abstractQueryType = imports.add(AbstractTypedQuery.class).args(type); + TypeRef conType = imports.add(Connection.class); + TypeRef entityTypedQuery = new TypeRef(type.className + "TypedQuery"); Parameters p = new Parameters(); ParameterRef c = p.addParameter(conType, "c"); - ClassG typedQuery = g.addInnerClass(PUBLIC | STATIC, new TypeRef(type.className + "TypedQuery")). + ClassG typedQuery = new ClassG(PUBLIC | STATIC, entityTypedQuery). extendsType(abstractQueryType); typedQuery.addConstructor(p, singletonList("super(" + c.name + ", " + daoType.className + ".desc);")); - fromResultSet(sqlUnit, typedQuery); + return typedQuery; + } + + public MethodRef query(SqlUnitModel sqlUnit, Imports imports) { + TypeRef conType = imports.add(Connection.class); + TypeRef typedQueryType = imports.add(TypedQuery.class).args(type); + TypeRef entityTypedQuery = new TypeRef(type.className + "TypedQuery"); + + Parameters p = new Parameters(); + ParameterRef c = p.addParameter(conType, "c"); List body = new ArrayList<>(); - body.add("return new " + typedQuery.type + "(" + c.name + ");"); - g.addStaticMethod(body, typedQueryType, "query", p); + body.add("return new " + entityTypedQuery + "(" + c.name + ");"); + return new MethodRef(PUBLIC | STATIC, typedQueryType, "query", p, body); } - public void fromResultSet(SqlUnitModel unit, ClassG g) { - TypeRef rsType = g.addImport(ResultSet.class); + public MethodRef fromResultSet(SqlUnitModel unit, Imports g) { + TypeRef rsType = g.add(ResultSet.class); Parameters p = new Parameters(); ParameterRef rs = p.addParameter(rsType, "rs"); @@ -230,10 +257,10 @@ public class EntityMirror implements Comparable { TypeHandler typeHandler = generatorConfiguration.typeHandler(field.type); body.add(field.type + " " + field.javaName + " = " + typeHandler.getter(rs.name, i + 1) + ";"); } else if (field instanceof ReferenceFieldMirror) { - ReferenceFieldMirror ref = (ReferenceFieldMirror) field; - EntityMirror referenced = unit.get(ref.type); - FieldMirror idField = referenced.getIdField(); - TypeHandler typeHandler = generatorConfiguration.typeHandler(idField.type); +// 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(field.type + " " + field.javaName + " = null;"); } @@ -242,8 +269,8 @@ public class EntityMirror implements Comparable { body.add("return new " + type + "(" + join(names, ", ") + ");"); - g.addMethod(body, type, "fromResultSet", p). - exception(g.addImport(SQLException.class)); + return new MethodRef(PUBLIC, type, "fromResultSet", p, body). + exception(g.add(SQLException.class)); } @Override -- cgit v1.2.3