package io.trygvis.persistence; import io.trygvis.container.compiler.model.TypeRef; import static io.trygvis.container.compiler.Utils.toGetterName; import static io.trygvis.container.compiler.model.Parameters.ParameterRef; import static io.trygvis.persistence.FieldMirror.AccessorType.FIELD; /** * TODO: a single field might have to be mapped to multiple sql columns. */ public class FieldMirror { public final FieldType fieldType; public final AccessorType accessorType; public final TypeRef type; public final String javaName; public final String sqlName; public final boolean id; public final boolean notNull; public final boolean unique; public enum FieldType { PRIMITIVE, REFERENCE, } public enum AccessorType { FIELD, METHOD, } public FieldMirror(FieldType fieldType, AccessorType accessorType, TypeRef type, String javaName, String sqlName, boolean id, boolean notNull, boolean unique) { this.fieldType = fieldType; this.accessorType = accessorType; this.type = type; this.javaName = javaName; this.sqlName = sqlName; this.id = id; this.notNull = notNull; this.unique = unique; } public String fieldAccessor(ParameterRef o) { return fieldAccessor(o.name); } public String referenceAccessor(ParameterRef o, FieldMirror f) { return f.fieldAccessor(o.name + "." + javaName); } private String fieldAccessor(String o) { if (accessorType == FIELD) { return o + "." + javaName; } return o + "." + toGetterName(javaName) + "()"; } @Override public String toString() { return "FieldMirror{" + "fieldType=" + fieldType + ", accessorType=" + accessorType + ", type=" + type + ", javaName='" + javaName + '\'' + ", sqlName='" + sqlName + '\'' + ", id=" + id + ", notNull=" + notNull + ", unique=" + unique + '}'; } }