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; /** * TODO: a single field might have to be mapped to multiple sql columns. */ public class FieldMirror { public final FieldType fieldType; public final SetterType setterType; public final GetterType getterType; 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 SetterType { CONSTRUCTOR, FIELD, METHOD, } public enum GetterType { FIELD, METHOD, } public FieldMirror(FieldType fieldType, SetterType setterType, GetterType getterType, TypeRef type, String javaName, String sqlName, boolean id, boolean notNull, boolean unique) { this.fieldType = fieldType; this.setterType = setterType; this.getterType = getterType; 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 (getterType == GetterType.FIELD) { return o + "." + javaName; } return o + "." + toGetterName(javaName) + "()"; } @Override public String toString() { return "FieldMirror{" + "fieldType=" + fieldType + ", setterType=" + setterType + ", getterType=" + getterType + ", type=" + type + ", javaName='" + javaName + '\'' + ", sqlName='" + sqlName + '\'' + ", id=" + id + ", notNull=" + notNull + ", unique=" + unique + '}'; } }