From f0a9ad191465bba0e7ddaea700d9667be699fec3 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Sun, 4 Aug 2013 19:14:14 +0200 Subject: wip o Adding support for int and long types. --- .../trygvis/container/compiler/EntityHandler.java | 7 ++- .../persistence/GeneratorConfiguration.java | 7 ++- .../java/io/trygvis/persistence/TypeHandler.java | 63 ++++++---------------- .../trygvis/container/compiler/ProcessorTest.java | 5 +- .../io/trygvis/persistence/test/ChildEntity.java | 5 +- 5 files changed, 34 insertions(+), 53 deletions(-) diff --git a/container-compiler-plugin/src/main/java/io/trygvis/container/compiler/EntityHandler.java b/container-compiler-plugin/src/main/java/io/trygvis/container/compiler/EntityHandler.java index 53811d9..ae3443c 100644 --- a/container-compiler-plugin/src/main/java/io/trygvis/container/compiler/EntityHandler.java +++ b/container-compiler-plugin/src/main/java/io/trygvis/container/compiler/EntityHandler.java @@ -297,6 +297,12 @@ public class EntityHandler extends AbstractHandler { FieldMirror field; if (primitive) { + System.out.println("type = " + type); + TypeHandler typeHandler = generatorConfiguration.typeHandler(type); + System.out.println("typeHandler = " + typeHandler); + System.out.println("typeHandler.typeName() = " + typeHandler.typeName()); + // TODO: check for configuration conflict + notNull = !typeHandler.nullable; field = new FieldMirror(PRIMITIVE, accessorType, type, javaName, sqlName, id, notNull, unique); } else if (generatorConfiguration.hasTypeHandler(type)) { throw new CompilerException(var, "Missing type handler for type: " + type.fqName); @@ -311,7 +317,6 @@ public class EntityHandler extends AbstractHandler { } public void phase3(boolean errorRaised) throws Exception { - System.out.println("errorRaised = " + errorRaised); try { for (EntityMirror entity : sqlUnit.getEntities().values()) { writeFile(phase3(entity), sqlUnit.element(entity)); diff --git a/container-compiler-plugin/src/main/java/io/trygvis/persistence/GeneratorConfiguration.java b/container-compiler-plugin/src/main/java/io/trygvis/persistence/GeneratorConfiguration.java index 9df6c35..48072de 100644 --- a/container-compiler-plugin/src/main/java/io/trygvis/persistence/GeneratorConfiguration.java +++ b/container-compiler-plugin/src/main/java/io/trygvis/persistence/GeneratorConfiguration.java @@ -3,6 +3,7 @@ package io.trygvis.persistence; import io.trygvis.container.compiler.CompilerException; import io.trygvis.container.compiler.model.TypeRef; +import java.sql.Types; import java.util.Date; import java.util.Map; import java.util.TreeMap; @@ -13,8 +14,10 @@ public class GeneratorConfiguration { private final Map typeHandlers = new TreeMap<>(); { - typeHandlers.put(new TypeRef(Integer.class), new TypeHandler.IntTypeHandler()); - typeHandlers.put(new TypeRef(Long.class), new TypeHandler.LongTypeHandler()); + typeHandlers.put(new TypeRef(int.class), new TypeHandler.PrimitiveTypeHandler("INTEGER", Types.INTEGER, "Int", false)); + typeHandlers.put(new TypeRef(Integer.class), new TypeHandler.PrimitiveTypeHandler("INTEGER", Types.INTEGER, "Int", true)); + typeHandlers.put(new TypeRef(long.class), new TypeHandler.PrimitiveTypeHandler("BIGINT", Types.BIGINT, "Long", false)); + typeHandlers.put(new TypeRef(Long.class), new TypeHandler.PrimitiveTypeHandler("BIGINT", Types.BIGINT, "Long", true)); typeHandlers.put(new TypeRef(String.class), new TypeHandler.StringTypeHandler()); typeHandlers.put(new TypeRef(Date.class), new TypeHandler.DateTypeHandler()); typeHandlers.put(new TypeRef("org.joda.time.DateTime"), new TypeHandler.JodaDateTimeTypeHandler()); 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 31eba4b..dfb2be3 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 @@ -10,10 +10,12 @@ import static java.sql.Types.*; public abstract class TypeHandler { private final String sqlType; private final int type; + public final boolean nullable; - protected TypeHandler(String sqlType, int type) { + protected TypeHandler(String sqlType, int type, boolean nullable) { this.sqlType = sqlType; this.type = type; + this.nullable = nullable; } abstract String setter(int i, String expr); @@ -48,51 +50,28 @@ public abstract class TypeHandler { // // ----------------------------------------------------------------------- - public static class IntTypeHandler extends TypeHandler { - public IntTypeHandler() { - super("INTEGER", INTEGER); - } + public static class PrimitiveTypeHandler extends TypeHandler { + private final String accessor; - @Override - public String setter(int i, String expr) { - return "setInt(" + i + ", " + expr + ")"; - } - - @Override - public String getter(String rs, int i) { - return rs + ".getInt(" + i + ")"; - } - } - - public static class LongTypeHandler extends TypeHandler { - protected LongTypeHandler() { - super("BIGINT", BIGINT); + public PrimitiveTypeHandler(String sqlType, int type, String accessor, boolean nullable) { + super(sqlType, type, nullable); + this.accessor = accessor; } @Override public String setter(int i, String expr) { - return "setLong(" + i + ", " + expr + ")"; + return "set" + accessor + "(" + i + ", " + expr + ")"; } @Override public String getter(String rs, int i) { - return rs + ".getLong(" + i + ")"; + return rs + ".get" + accessor + "(" + i + ")"; } } - public static class StringTypeHandler extends TypeHandler { + public static class StringTypeHandler extends PrimitiveTypeHandler { protected StringTypeHandler() { - super(null, VARCHAR); - } - - @Override - public String setter(int i, String expr) { - return "setString(" + i + ", " + expr + ")"; - } - - @Override - public String getter(String rs, int i) { - return rs + ".getString(" + i + ")"; + super(null, VARCHAR, "String", true); } @Override @@ -103,7 +82,7 @@ public abstract class TypeHandler { public static class DateTypeHandler extends TypeHandler { protected DateTypeHandler() { - super("TIMESTAMP", TIMESTAMP); + super("TIMESTAMP", TIMESTAMP, true); } @Override @@ -119,7 +98,7 @@ public abstract class TypeHandler { public static class JodaDateTimeTypeHandler extends TypeHandler { protected JodaDateTimeTypeHandler() { - super("TIMESTAMP", TIMESTAMP); + super("TIMESTAMP", TIMESTAMP, true); } @Override @@ -133,19 +112,9 @@ public abstract class TypeHandler { } } - public static class UuidTypeHandler extends TypeHandler { + public static class UuidTypeHandler extends PrimitiveTypeHandler { protected UuidTypeHandler() { - super("CHAR(36)", CHAR); - } - - @Override - public String setter(int i, String expr) { - return "setString(" + i + ", " + expr + ".toString())"; - } - - @Override - public String getter(String rs, int i) { - return "java.util.UUID.fromString(" + rs + ".getString(" + i + "))"; + super("CHAR(36)", CHAR, "String", true); } } } diff --git a/container-compiler-plugin/src/test/java/io/trygvis/container/compiler/ProcessorTest.java b/container-compiler-plugin/src/test/java/io/trygvis/container/compiler/ProcessorTest.java index f30b731..4266a01 100644 --- a/container-compiler-plugin/src/test/java/io/trygvis/container/compiler/ProcessorTest.java +++ b/container-compiler-plugin/src/test/java/io/trygvis/container/compiler/ProcessorTest.java @@ -31,11 +31,12 @@ public class ProcessorTest { InMemoryJavaFileManager fileManager = new InMemoryJavaFileManager(standardFileManager); - List sources = asList( + List sources = asList((JavaSourceFromString[])new JavaSourceFromString[]{ loadJava("io.trygvis.persistence.test.package-info"), loadJava("io.trygvis.persistence.test.Person"), loadJava("io.trygvis.persistence.test.ParentEntity"), - loadJava("io.trygvis.persistence.test.ChildEntity")); + loadJava("io.trygvis.persistence.test.ChildEntity"), + }); JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, collector, null, null, sources); diff --git a/container-compiler-plugin/src/test/resources/io/trygvis/persistence/test/ChildEntity.java b/container-compiler-plugin/src/test/resources/io/trygvis/persistence/test/ChildEntity.java index a341fd2..ff91680 100644 --- a/container-compiler-plugin/src/test/resources/io/trygvis/persistence/test/ChildEntity.java +++ b/container-compiler-plugin/src/test/resources/io/trygvis/persistence/test/ChildEntity.java @@ -6,9 +6,12 @@ import javax.persistence.Entity; public class ChildEntity extends ParentEntity { public final String name; - public ChildEntity(Long id, String name) { + public final int favoriteNumber; + + public ChildEntity(Long id, String name, int favoriteNumber) { super(id); this.name = name; + this.favoriteNumber = favoriteNumber; } // ----------------------------------------------------------------------- -- cgit v1.2.3