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 +++++++++++++-------- .../java/io/trygvis/persistence/TypeHandler.java | 84 ++++++++++++----- 2 files changed, 128 insertions(+), 59 deletions(-) (limited to 'container-compiler-plugin/src/main/java/io/trygvis/persistence') 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 diff --git a/container-compiler-plugin/src/main/java/io/trygvis/persistence/TypeHandler.java b/container-compiler-plugin/src/main/java/io/trygvis/persistence/TypeHandler.java index 0454a96..72941f2 100644 --- a/container-compiler-plugin/src/main/java/io/trygvis/persistence/TypeHandler.java +++ b/container-compiler-plugin/src/main/java/io/trygvis/persistence/TypeHandler.java @@ -1,13 +1,58 @@ package io.trygvis.persistence; -public interface TypeHandler { - String setter(int i, String expr); +import io.trygvis.container.compiler.InternalErrorException; - String getter(String rs, int i); +import java.lang.reflect.Field; +import java.sql.Types; - String sqlType(FieldMirror field); +import static java.sql.Types.*; + +public abstract class TypeHandler { + private final String sqlType; + private final int type; + + protected TypeHandler(String sqlType, int type) { + this.sqlType = sqlType; + this.type = type; + } + + abstract String setter(int i, String expr); + + abstract String getter(String rs, int i); + + public String sqlType(FieldMirror field) { + return sqlType; + } + + public int type() { + return type; + } + + public final String typeName() { + try { + int type = type(); + for (Field field : Types.class.getFields()) { + Object o = field.get(null); + if (o != null && o instanceof Integer && o.equals(type)) { + return field.getName(); + } + } + } catch (IllegalAccessException e) { + throw new InternalErrorException(e); + } + + throw new InternalErrorException("Unknown java.sql.Types: " + type); + } + + // ----------------------------------------------------------------------- + // + // ----------------------------------------------------------------------- + + public static class IntTypeHandler extends TypeHandler { + public IntTypeHandler() { + super("INTEGER", INTEGER); + } - public static class IntTypeHandler implements TypeHandler { @Override public String setter(int i, String expr) { return "setInt(" + i + ", " + expr + ")"; @@ -17,14 +62,13 @@ public interface TypeHandler { public String getter(String rs, int i) { return rs + ".getInt(" + i + ")"; } + } - @Override - public String sqlType(FieldMirror field) { - return "INTEGER"; + public static class LongTypeHandler extends TypeHandler { + protected LongTypeHandler() { + super("BIGINT", BIGINT); } - } - public static class LongTypeHandler implements TypeHandler { @Override public String setter(int i, String expr) { return "setLong(" + i + ", " + expr + ")"; @@ -34,14 +78,13 @@ public interface TypeHandler { public String getter(String rs, int i) { return rs + ".getLong(" + i + ")"; } + } - @Override - public String sqlType(FieldMirror field) { - return "BIGINT"; + public static class StringTypeHandler extends TypeHandler { + protected StringTypeHandler() { + super(null, VARCHAR); } - } - public static class StringTypeHandler implements TypeHandler { @Override public String setter(int i, String expr) { return "setString(" + i + ", " + expr + ")"; @@ -58,7 +101,11 @@ public interface TypeHandler { } } - public static class DateTypeHandler implements TypeHandler { + public static class DateTypeHandler extends TypeHandler { + protected DateTypeHandler() { + super("TIMESTAMP", TIMESTAMP); + } + @Override public String setter(int i, String expr) { return "setTimestamp(" + i + ", new java.sql.Timestamp(" + expr + ".getTime()))"; @@ -68,10 +115,5 @@ public interface TypeHandler { public String getter(String rs, int i) { return "new java.util.Date(" + rs + ".getTimestamp(" + i + ").getTime())"; } - - @Override - public String sqlType(FieldMirror field) { - return "TIMESTAMP"; - } } } -- cgit v1.2.3