package io.trygvis.persistence; import io.trygvis.container.compiler.model.TypeRef; import javax.persistence.AccessType; import static io.trygvis.container.compiler.Utils.toGetterName; import static io.trygvis.container.compiler.model.Parameters.ParameterRef; import static javax.persistence.AccessType.FIELD; /** * TODO: a single field might have to be mapped to multiple sql columns. */ public class FieldMirror { public final FieldType fieldType; public final boolean constructorParam; public final AccessType setterType; public final AccessType 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 FieldMirror(FieldType fieldType, boolean constructorParam, AccessType setterType, AccessType getterType, TypeRef type, String javaName, String sqlName, boolean id, boolean notNull, boolean unique) { this.fieldType = fieldType; this.constructorParam = constructorParam; 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 == FIELD) { return o + "." + javaName; } return o + "." + toGetterName(javaName) + "()"; } @Override public String toString() { return "FieldMirror{" + "fieldType=" + fieldType + ", constructorParam=" + constructorParam + ", setterType=" + setterType + ", getterType=" + getterType + ", type=" + type + ", javaName='" + javaName + '\'' + ", sqlName='" + sqlName + '\'' + ", id=" + id + ", notNull=" + notNull + ", unique=" + unique + '}'; } }