diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2013-08-03 20:21:19 +0200 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2013-08-03 20:21:19 +0200 |
commit | df92538ab3d83da9839f08b28fc8a67317565463 (patch) | |
tree | a958d3cce1d3a9045c79371b182adb1ea6cf5a73 /container-compiler-plugin/src/main/java/io/trygvis/persistence | |
parent | e1cf8889628d2d31cf7067b8c002f229fc22007d (diff) | |
download | container-playground-df92538ab3d83da9839f08b28fc8a67317565463.tar.gz container-playground-df92538ab3d83da9839f08b28fc8a67317565463.tar.bz2 container-playground-df92538ab3d83da9839f08b28fc8a67317565463.tar.xz container-playground-df92538ab3d83da9839f08b28fc8a67317565463.zip |
wip
Diffstat (limited to 'container-compiler-plugin/src/main/java/io/trygvis/persistence')
-rw-r--r-- | container-compiler-plugin/src/main/java/io/trygvis/persistence/EntityMirror.java | 131 | ||||
-rw-r--r-- | container-compiler-plugin/src/main/java/io/trygvis/persistence/FieldMirror.java | 16 | ||||
-rw-r--r-- | container-compiler-plugin/src/main/java/io/trygvis/persistence/GeneratorConfiguration.java (renamed from container-compiler-plugin/src/main/java/io/trygvis/persistence/GeneratorSupport.java) | 8 | ||||
-rw-r--r-- | container-compiler-plugin/src/main/java/io/trygvis/persistence/TypeHandler.java | 18 |
4 files changed, 142 insertions, 31 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 2a4952f..8e1602c 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,5 +1,7 @@ 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.ParameterRef; import io.trygvis.container.compiler.model.TypeRef; @@ -10,19 +12,21 @@ import java.sql.SQLException; import java.util.ArrayList; import java.util.List; +import static io.trygvis.persistence.FieldMirror.PrimitiveFieldMirror; +import static io.trygvis.persistence.FieldMirror.ReferenceFieldMirror; import static java.lang.String.format; import static org.apache.commons.lang.StringUtils.join; -public class EntityMirror { - public final GeneratorSupport generatorSupport; +public class EntityMirror implements Comparable<EntityMirror> { + public final GeneratorConfiguration generatorConfiguration; public final List<FieldMirror> fields = new ArrayList<>(); public final List<FieldMirror> idFields = new ArrayList<>(); public final TypeRef type; public final String tableName; public final TypeRef daoType; - public EntityMirror(GeneratorSupport generatorSupport, TypeRef type, String tableName) { - this.generatorSupport = generatorSupport; + public EntityMirror(GeneratorConfiguration generatorConfiguration, TypeRef type, String tableName) { + this.generatorConfiguration = generatorConfiguration; this.type = type; this.tableName = tableName; @@ -31,18 +35,48 @@ public class EntityMirror { public void add(FieldMirror field) { fields.add(field); - if(field.id) { + if (field.id) { idFields.add(field); } } - public String createTableSql() { + public FieldMirror getIdField() { + return idFields.get(0); + } + + public String createTableSql(SqlUnitModel unit) { List<String> columns = new ArrayList<>(); for (FieldMirror field : fields) { - TypeHandler typeHandler = generatorSupport.typeHandler(field.type); - String s = " " + field.sqlName + " " + typeHandler.sqlType(field); - if(field.id) { - s += " PRIMARY KEY"; + String s; + if (field instanceof PrimitiveFieldMirror) { + TypeHandler typeHandler = generatorConfiguration.typeHandler(field.type); + s = " " + field.sqlName + " " + typeHandler.sqlType(field); + if (field.id) { + s += " PRIMARY KEY"; + } else if (field.notNull) { + s += " NOT NULL"; + } else if (field.unique) { + s += " UNIQUE"; + } + } else if (field instanceof ReferenceFieldMirror) { + ReferenceFieldMirror ref = (ReferenceFieldMirror) field; + EntityMirror referenced = unit.get(ref.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(); + } + } else { + throw new RuntimeException("Unknown field type: " + field.getClass()); } columns.add(s); } @@ -77,7 +111,7 @@ public class EntityMirror { return "DELETE FROM " + tableName + " WHERE " + join(ss, " AND ") + ";"; } - public void insertInto(ClassG g) { + public void insertInto(SqlUnitModel unit, ClassG g) { TypeRef conType = g.addImport(Connection.class); TypeRef psType = g.addImport(PreparedStatement.class); TypeRef objectType = g.addImport(type); @@ -89,8 +123,16 @@ public class EntityMirror { body.add("try(" + psType.name + " stmt = " + con.name + ".prepareStatement(insertIntoSql)) {"); for (int i = 0; i < fields.size(); i++) { FieldMirror field = fields.get(i); - TypeHandler typeHandler = generatorSupport.typeHandler(field.type); - body.add(" stmt." + typeHandler.resultSetSetter(i + 1, "o", field) + ";"); + if(field instanceof PrimitiveFieldMirror) { + TypeHandler typeHandler = generatorConfiguration.typeHandler(field.type); + body.add(" stmt." + typeHandler.setter(i + 1, "o." + field.javaName) + ";"); + } 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); + body.add(" stmt." + typeHandler.setter(i + 1, "o." + field.javaName + "." + idField.javaName) + ";"); + } } body.add(" stmt.executeUpdate();"); body.add("}"); @@ -98,4 +140,67 @@ public class EntityMirror { g.addStaticMethod(body, TypeRef.VOID, "insertInto", con, object). exception(g.addImport(SQLException.class)); } + + public void delete(ClassG g) { + TypeRef conType = g.addImport(Connection.class); + TypeRef objectType = g.addImport(type); + ParameterRef con = new ParameterRef(conType, "con"); + ParameterRef o = new ParameterRef(objectType, "o"); + + List<String> arguments = new ArrayList<>(); + arguments.add(con.name); + for (FieldMirror field : idFields) { + arguments.add(o.name + "." + field.javaName); + } + List<String> body = new ArrayList<>(); + body.add("deleteById(" + join(arguments, ", ") + ");"); + + g.addStaticMethod(body, TypeRef.VOID, "delete", con, o). + exception(g.addImport(SQLException.class)); + } + + public void deleteById(ClassG g) { + TypeRef conType = g.addImport(Connection.class); + TypeRef psType = g.addImport(PreparedStatement.class); + ParameterRef con = new ParameterRef(conType, "con"); + + ParameterRef[] parameters = new ParameterRef[idFields.size() + 1]; + parameters[0] = con; + + List<String> body = new ArrayList<>(); + + body.add("try(" + psType.name + " stmt = " + con.name + ".prepareStatement(deleteFromSql)) {"); + for (int i = 0; i < idFields.size(); i++) { + FieldMirror field = idFields.get(i); + TypeHandler typeHandler = generatorConfiguration.typeHandler(field.type); + ParameterRef p = new ParameterRef(field.type, field.javaName); + parameters[i + 1] = p; + body.add(" stmt." + typeHandler.setter(i + 1, field.javaName) + ";"); + } + body.add(" stmt.executeUpdate();"); + body.add("}"); + + g.addStaticMethod(body, TypeRef.VOID, "deleteById", parameters). + exception(g.addImport(SQLException.class)); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof EntityMirror)) return false; + + EntityMirror that = (EntityMirror) o; + + return type.equals(that.type); + } + + @Override + public int hashCode() { + return type.hashCode(); + } + + @Override + public int compareTo(EntityMirror o) { + return type.compareTo(o.type); + } } diff --git a/container-compiler-plugin/src/main/java/io/trygvis/persistence/FieldMirror.java b/container-compiler-plugin/src/main/java/io/trygvis/persistence/FieldMirror.java index bf0a0c3..f92cc2c 100644 --- a/container-compiler-plugin/src/main/java/io/trygvis/persistence/FieldMirror.java +++ b/container-compiler-plugin/src/main/java/io/trygvis/persistence/FieldMirror.java @@ -15,24 +15,26 @@ public abstract class FieldMirror { public final String sqlName; public final boolean id; public final boolean notNull; + public final boolean unique; public enum FieldType { PRIMITIVE, REFERENCE, } - protected FieldMirror(FieldType fieldType, TypeRef type, String javaName, String sqlName, boolean id, boolean notNull) { + protected FieldMirror(FieldType fieldType, TypeRef type, String javaName, String sqlName, boolean id, boolean notNull, boolean unique) { this.fieldType = fieldType; this.type = type; this.javaName = javaName; this.sqlName = sqlName; this.id = id; this.notNull = notNull; + this.unique = unique; } public static class PrimitiveFieldMirror extends FieldMirror { - public PrimitiveFieldMirror(TypeRef typeRef, String javaName, String sqlName, boolean id, boolean notNull) { - super(PRIMITIVE, typeRef, javaName, sqlName, id, notNull); + public PrimitiveFieldMirror(TypeRef typeRef, String javaName, String sqlName, boolean id, boolean notNull, boolean unique) { + super(PRIMITIVE, typeRef, javaName, sqlName, id, notNull, unique); } @Override @@ -42,13 +44,14 @@ public abstract class FieldMirror { ", javaName='" + javaName + '\'' + ", sqlName='" + sqlName + '\'' + ", notNull=" + notNull + + ", unique=" + unique + '}'; } } - public static class ReferenceFieldMirror extends FieldMirror { - public ReferenceFieldMirror(TypeRef typeRef, String javaName, String sqlName, boolean notNull) { - super(REFERENCE, typeRef, javaName, sqlName, false, notNull); + public static class ReferenceFieldMirror extends FieldMirror { + public ReferenceFieldMirror(TypeRef typeRef, String javaName, String sqlName, boolean notNull, boolean unique) { + super(REFERENCE, typeRef, javaName, sqlName, false, notNull, unique); } @Override @@ -58,6 +61,7 @@ public abstract class FieldMirror { ", javaName='" + javaName + '\'' + ", sqlName='" + sqlName + '\'' + ", notNull=" + notNull + + ", unique=" + unique + '}'; } } diff --git a/container-compiler-plugin/src/main/java/io/trygvis/persistence/GeneratorSupport.java b/container-compiler-plugin/src/main/java/io/trygvis/persistence/GeneratorConfiguration.java index 0563691..37d8146 100644 --- a/container-compiler-plugin/src/main/java/io/trygvis/persistence/GeneratorSupport.java +++ b/container-compiler-plugin/src/main/java/io/trygvis/persistence/GeneratorConfiguration.java @@ -1,12 +1,13 @@ package io.trygvis.persistence; +import io.trygvis.container.compiler.CompilerException; import io.trygvis.container.compiler.model.TypeRef; import java.util.Date; import java.util.Map; import java.util.TreeMap; -public class GeneratorSupport { +public class GeneratorConfiguration { private final Map<TypeRef, TypeHandler> primitiveTypeHandlers = new TreeMap<>(); private final Map<TypeRef, TypeHandler> typeHandlers = new TreeMap<>(); @@ -26,8 +27,9 @@ public class GeneratorSupport { public TypeHandler typeHandler(TypeRef type) { TypeHandler typeHandler = typeHandlers.get(type); - if (typeHandler == null) - throw new RuntimeException("Unsupported field type: " + type); + if (typeHandler == null) { + throw new CompilerException("Unsupported field type: " + type.fqName); + } return typeHandler; } 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 ff2ad0a..3aacd7a 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,14 +1,14 @@ package io.trygvis.persistence; public interface TypeHandler { - String resultSetSetter(int i, String o, FieldMirror field); + String setter(int i, String ref); String sqlType(FieldMirror field); public static class IntTypeHandler implements TypeHandler { @Override - public String resultSetSetter(int i, String o, FieldMirror field) { - return "setInt(" + i + ", " + o + "." + field.javaName + ")"; + public String setter(int i, String ref) { + return "setInt(" + i + ", " + ref + ")"; } @Override @@ -19,8 +19,8 @@ public interface TypeHandler { public static class LongTypeHandler implements TypeHandler { @Override - public String resultSetSetter(int i, String o, FieldMirror field) { - return "setLong(" + i + ", " + o + "." + field.javaName + ")"; + public String setter(int i, String ref) { + return "setLong(" + i + ", " + ref + ")"; } @Override @@ -31,8 +31,8 @@ public interface TypeHandler { public static class StringTypeHandler implements TypeHandler { @Override - public String resultSetSetter(int i, String o, FieldMirror field) { - return "setString(" + i + ", " + o + "." + field.javaName + ")"; + public String setter(int i, String ref) { + return "setString(" + i + ", " + ref + ")"; } @Override @@ -43,8 +43,8 @@ public interface TypeHandler { public static class DateTypeHandler implements TypeHandler { @Override - public String resultSetSetter(int i, String o, FieldMirror field) { - return "setTimestamp(" + i + ", new java.sql.Timestamp(" + o + "." + field.javaName + ".getTime()))"; + public String setter(int i, String ref) { + return "setTimestamp(" + i + ", new java.sql.Timestamp(" + ref + ".getTime()))"; } @Override |