diff options
Diffstat (limited to 'container-compiler-plugin/src/main/java/io/trygvis/container')
3 files changed, 17 insertions, 99 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 1dbc534..a2bb5a6 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 @@ -1,10 +1,5 @@ package io.trygvis.container.compiler; -import io.trygvis.container.compiler.model.ClassG; -import io.trygvis.container.compiler.model.Constructor; -import io.trygvis.container.compiler.model.FieldRef; -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.EntityMirror; import io.trygvis.persistence.FieldMirror; @@ -13,8 +8,7 @@ import io.trygvis.persistence.SequenceMirror; import io.trygvis.persistence.SqlEntity; import io.trygvis.persistence.SqlEntitySet; import io.trygvis.persistence.TypeHandler; -import io.trygvis.persistence.sql.SqlDao; -import io.trygvis.persistence.sql.SqlEntityMeta; +import io.trygvis.persistence.generators.DaoGenerator; import javax.annotation.processing.ProcessingEnvironment; import javax.lang.model.element.AnnotationMirror; @@ -30,10 +24,6 @@ import javax.lang.model.type.TypeKind; import javax.lang.model.type.TypeMirror; import javax.persistence.Id; import javax.persistence.SequenceGenerator; -import java.io.IOException; -import java.sql.Connection; -import java.sql.ResultSet; -import java.sql.SQLException; import java.util.ArrayList; import java.util.Iterator; import java.util.List; @@ -41,9 +31,8 @@ import java.util.Map; import java.util.Set; import java.util.TreeMap; -import static io.trygvis.container.compiler.Utils.*; -import static io.trygvis.container.compiler.model.Parameters.ParameterRef; -import static io.trygvis.container.compiler.model.TypeRef.VOID; +import static io.trygvis.container.compiler.Utils.toFieldName; +import static io.trygvis.container.compiler.Utils.writeFile; import static io.trygvis.persistence.FieldMirror.AccessorType.FIELD; import static io.trygvis.persistence.FieldMirror.AccessorType.METHOD; import static io.trygvis.persistence.FieldMirror.FieldType.PRIMITIVE; @@ -51,7 +40,6 @@ import static io.trygvis.persistence.FieldMirror.FieldType.REFERENCE; import static io.trygvis.persistence.TypeHandler.StringEnumTypeHandler; import static java.lang.Character.isUpperCase; import static java.lang.String.format; -import static java.lang.reflect.Modifier.PUBLIC; import static javax.lang.model.util.ElementFilter.fieldsIn; import static javax.lang.model.util.ElementFilter.methodsIn; import static org.apache.commons.lang.StringUtils.join; @@ -237,88 +225,6 @@ public class EntityHandler extends AbstractHandler { } } - public ClassG phase3(EntityMirror entityMirror) throws IOException { - ClassG g = new ClassG(PUBLIC, entityMirror.daoType). - extendsType(new TypeRef(SqlDao.class).args(entityMirror.idType, entityMirror.type)); - Parameters p = new Parameters(); - ParameterRef c = p.addParameter(new TypeRef(Connection.class), "c"); - g.add(new Constructor(p, "super(" + c.name + ");")); - - TypeRef stringType = g.imports.add(String.class); - TypeRef sqlEntityDescType = g.imports.add(SqlEntityMeta.class); - TypeRef sqlException = g.imports.add(SQLException.class); - - FieldRef createTableSql = g.addPublicStaticFinalField(stringType, "createTableSql"). - value(toJavaString(entityMirror.createTableSql(sqlUnit))); - g.add(new MethodRef(PUBLIC, stringType, "createTableSql", "return createTableSql;")); - FieldRef dropTableSql = g.addPublicStaticFinalField(stringType, "dropTableSql"). - value(toJavaString(entityMirror.dropTableSql())); - g.add(new MethodRef(PUBLIC, stringType, "dropTableSql", "return dropTableSql;")); - g.addPublicStaticFinalField(stringType, "insertIntoSql"). - value(toJavaString(entityMirror.insertIntoSql(sqlUnit))); - g.addPublicStaticFinalField(stringType, "deleteFromSql"). - value(toJavaString(entityMirror.deleteFromSql())); - String desc = "new " + sqlEntityDescType + "(" + - toJavaString(entityMirror.tableName) + ", " + - toJavaString(entityMirror.defaultFields()) + ", " + - createTableSql.name + ", " + - dropTableSql.name + - ")"; - g.addPublicStaticFinalField(sqlEntityDescType, "desc").value(desc); - ClassG.InnerClassG typedQuery = g.addInnerClass(entityMirror.queryType(g.imports)); - g.addInnerClass(entityMirror.utils(sqlUnit)); - - { - p = new Parameters(); - ParameterRef rs = p.addParameter(new TypeRef(ResultSet.class), "rs"); - g.add(new MethodRef(PUBLIC, entityMirror.type, "fromResultSet", p, - "return Utils.fromResultSet" + entityMirror.type.className + "(" + rs.name + ");"). - exception(sqlException)); - } - - { - p = new Parameters(); - ParameterRef entity = p.addParameter(entityMirror.type, "entity"); - g.add(new MethodRef(PUBLIC, VOID, "insert", p, - "Utils.insert" + entityMirror.type.className + "(super.c, " + entity.name + ");"). - exception(sqlException)); - } - - { - p = new Parameters(); - ParameterRef id = p.addParameter(entityMirror.idType, "id"); - g.add(new MethodRef(PUBLIC, entityMirror.type, "selectById", p, - "return Utils.select" + entityMirror.type.className + "ById(super.c, " + id.name + ");"). - exception(sqlException)); - } - - { - p = new Parameters(); - ParameterRef entity = p.addParameter(entityMirror.type, "entity"); - g.add(new MethodRef(PUBLIC, VOID, "delete", p, - "Utils.delete" + entityMirror.type.className + "(super.c, " + entity.name + ");"). - exception(sqlException)); - } - - { - p = new Parameters(); - ParameterRef id = p.addParameter(entityMirror.idType, "id"); - g.add(new MethodRef(PUBLIC, VOID, "deleteById", p, - "Utils.delete" + entityMirror.type.className + "ById(super.c, " + id.name + ");"). - exception(sqlException)); - } - - { - p = new Parameters(); - ParameterRef entity = p.addParameter(entityMirror.type, "entity"); - g.add(new MethodRef(PUBLIC, VOID, "update", p, - "Utils.update" + entityMirror.type.className + "(super.c, " + entity.name + ");"). - exception(sqlException)); - } - - return g; - } - public FieldMirror fromElement(GeneratorConfiguration generatorConfiguration, VariableElement var, ExecutableElement getter, ExecutableElement setter) { // TODO: check the setter for annotations too @@ -401,7 +307,8 @@ public class EntityHandler extends AbstractHandler { public void phase3(boolean errorRaised) throws Exception { try { for (EntityMirror entity : sqlUnit.getEntities().values()) { - writeFile(processingEnv, phase3(entity), sqlUnit.element(entity)); + DaoGenerator daoGenerator = new DaoGenerator(generatorConfiguration, sqlUnit, entity); + writeFile(processingEnv, daoGenerator.generate(), sqlUnit.element(entity)); } } catch (CompilerException | InternalErrorException e) { // Ignore any exceptions if we had an error from before diff --git a/container-compiler-plugin/src/main/java/io/trygvis/container/compiler/MyProcessor.java b/container-compiler-plugin/src/main/java/io/trygvis/container/compiler/MyProcessor.java index 64ac678..1272759 100644 --- a/container-compiler-plugin/src/main/java/io/trygvis/container/compiler/MyProcessor.java +++ b/container-compiler-plugin/src/main/java/io/trygvis/container/compiler/MyProcessor.java @@ -1,8 +1,10 @@ package io.trygvis.container.compiler; import io.trygvis.container.log.Log; +import io.trygvis.persistence.EntityMirror; import io.trygvis.persistence.SqlEntity; import io.trygvis.persistence.SqlEntitySet; +import io.trygvis.persistence.generators.DaoGenerator; import org.springframework.transaction.annotation.Transactional; import javax.annotation.processing.Completion; @@ -26,9 +28,11 @@ import java.util.HashSet; import java.util.Set; import static io.trygvis.container.compiler.Utils.writeFile; -import static io.trygvis.persistence.generators.EntityManagerGenerator.generateEntityManager; import static io.trygvis.persistence.generators.EntityManagerFactoryGenerator.generateEntityManagerFactory; +import static io.trygvis.persistence.generators.EntityManagerGenerator.generateEntityManager; import static io.trygvis.persistence.generators.SequencesGenerator.generateSequences; +import static io.trygvis.persistence.generators.SqlSessionGenerator.generateSqlSession; +import static io.trygvis.persistence.generators.SqlSessionFactoryGenerator.generateSqlSessionFactory; import static java.util.Arrays.asList; import static java.util.Collections.emptyList; import static java.util.Collections.emptySet; @@ -144,6 +148,8 @@ public class MyProcessor implements Processor { writeFile(processingEnv, generateSequences(unit), null); writeFile(processingEnv, generateEntityManagerFactory(unit), null); writeFile(processingEnv, generateEntityManager(unit), null); + writeFile(processingEnv, generateSqlSession(unit), null); + writeFile(processingEnv, generateSqlSessionFactory(unit), null); return true; } diff --git a/container-compiler-plugin/src/main/java/io/trygvis/container/compiler/model/ClassG.java b/container-compiler-plugin/src/main/java/io/trygvis/container/compiler/model/ClassG.java index fe595a0..e9b5a47 100644 --- a/container-compiler-plugin/src/main/java/io/trygvis/container/compiler/model/ClassG.java +++ b/container-compiler-plugin/src/main/java/io/trygvis/container/compiler/model/ClassG.java @@ -93,6 +93,11 @@ public class ClassG { return addField(PUBLIC | STATIC | FINAL, type, name); } + public ClassG add(FieldRef fieldRef) { + this.fields.add(fieldRef); + return this; + } + public ClassG add(Constructor constructor) { constructors.add(constructor); return this; |