org.hotrod.metadata.ColumnMetadata Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hotrod-generator Show documentation
Show all versions of hotrod-generator Show documentation
HotRod is an ORM for Java, Spring and SpringBoot.
The newest version!
package org.hotrod.metadata;
import java.io.Serializable;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.hotrod.config.ColumnTag;
import org.hotrod.config.ConverterTag;
import org.hotrod.config.NameSolverNameTag.Scope;
import org.hotrod.config.NameSolverTag;
import org.hotrod.config.TypeSolverTag;
import org.hotrod.database.DatabaseAdapter;
import org.hotrod.database.PropertyType;
import org.hotrod.database.PropertyType.ValueRange;
import org.hotrod.exceptions.CouldNotResolveNameException;
import org.hotrod.exceptions.InvalidIdentifierException;
import org.hotrod.exceptions.UnresolvableDataTypeException;
import org.hotrod.identifiers.Id;
import org.hotrod.identifiers.ObjectId;
import org.hotrod.utils.JdbcTypes;
import org.hotrod.utils.JdbcTypes.JDBCType;
import org.nocrala.tools.database.tartarus.core.JdbcColumn;
import org.nocrala.tools.database.tartarus.core.JdbcColumn.AutogenerationType;
public class ColumnMetadata implements Serializable {
private static final long serialVersionUID = 1L;
private static final Logger log = LogManager.getLogger(ColumnMetadata.class);
private DataSetMetadata dataSet;
private JdbcColumn c;
private String columnName;
private String tableName;
private Id id;
private boolean belongsToPK;
private AutogenerationType autogenerationType;
private int dataType;
private JDBCType resultSetType;
private String typeName;
private Integer columnSize;
private Integer decimalDigits;
private String columnDefault;
private EnumDataSetMetadata enumMetadata;
private transient DatabaseAdapter adapter;
private ColumnTag tag;
private PropertyType type;
private TypeSolverTag typeSolverTag;
private boolean isVersionControlColumn;
private boolean reusesMemberFromSuperClass;
// ToString
public String toString() {
return "columnName=" + columnName + " tableName=" + this.tableName + ", dataType=" + this.dataType + ", typeName="
+ this.typeName + " columnSize=" + this.columnSize + " decimalDigits=" + this.decimalDigits + " --- TYPE: "
+ this.type;
}
// From a , , or tag
public ColumnMetadata(final DataSetMetadata dataSet, final JdbcColumn c, final DatabaseAdapter adapter,
final ColumnTag columnTag, final boolean isVersionControlColumn, final boolean belongsToPK,
final TypeSolverTag typeSolverTag, final NameSolverTag nameSolverTag)
throws UnresolvableDataTypeException, InvalidIdentifierException {
log.debug("init c=" + c);
this.dataSet = dataSet;
this.c = c;
this.columnName = c.getName();
log.debug("this.columnName=" + this.columnName);
this.tableName = c.getTable().getName();
this.tag = columnTag;
if (this.tag != null && this.tag.getJavaName() != null) {
this.id = Id.fromCanonicalSQLAndJavaMember(c.getName(), adapter, this.tag.getJavaName());
} else {
String replacedName = null;
try {
replacedName = nameSolverTag.resolveName(c.getName(), Scope.COLUMN);
log.debug("%%% " + this.tableName + "." + c.getName() + " -- replacedName=" + replacedName);
} catch (CouldNotResolveNameException e) {
throw new InvalidIdentifierException(
"Could not resolve property name for column " + this.tableName + "." + c.getName() + ": " + e.getMessage());
}
if (replacedName != null) {
String javaClassName = Id.fromCanonicalSQL(replacedName, adapter).getJavaClassName();
this.id = Id.fromCanonicalSQLAndJavaClass(c.getName(), adapter, javaClassName);
} else {
this.id = Id.fromCanonicalSQL(c.getName(), adapter);
}
}
log.debug(
" > CanonicalSQLName=" + this.id.getCanonicalSQLName() + " RenderedSQLName=" + this.id.getRenderedSQLName());
this.belongsToPK = belongsToPK;
this.autogenerationType = c.getAutogenerationType();
this.dataType = c.getDataType();
this.typeName = c.getTypeName();
this.columnSize = c.getColumnSize();
this.decimalDigits = c.getDecimalDigits();
this.columnDefault = c.getColumnDef();
this.enumMetadata = null;
this.adapter = adapter;
this.type = ColumnMetadata.resolveJavaType(this, this.tag, this.c, null, typeSolverTag, this.adapter);
this.typeSolverTag = typeSolverTag;
this.isVersionControlColumn = isVersionControlColumn;
this.reusesMemberFromSuperClass = false;
}
public static PropertyType resolveJavaType(final ColumnMetadata cm, final ColumnTag columnTag, final JdbcColumn c,
final JDBCType resultSetType, final TypeSolverTag typeSolverTag, final DatabaseAdapter adapter)
throws UnresolvableDataTypeException {
log.debug("columnTag=" + columnTag);
if (columnTag != null) {
log.debug("columnTag.getJdbcColumn()=" + columnTag.getJdbcColumn());
}
PropertyType typeSolverType = typeSolverTag.resolveType(cm, c, resultSetType);
if (columnTag != null && (columnTag.getJavaType() != null || columnTag.getConverterTag() != null)) {
// Use the type specified in the tag
log.debug("User-specified column type. Use it.");
JDBCType jdbcType;
if (columnTag.getJdbcType() != null) {
// User specified the JDBC type. Use the user's.
jdbcType = JdbcTypes.nameToType(columnTag.getJdbcType());
if (jdbcType == null) {
throw new UnresolvableDataTypeException(cm);
}
} else {
// User did not specify the JDBC type. Get it from the live database.
jdbcType = JdbcTypes.codeToType(cm.getDataType());
if (jdbcType == null) {
throw new UnresolvableDataTypeException(cm);
}
}
ValueRange range = columnTag.getValueRange();
if (range == null) {
range = PropertyType.getDefaultValueRange(columnTag.getJavaType());
}
String javaType = columnTag.getJavaType() != null ? columnTag.getJavaType()
: columnTag.getConverterTag().getJavaType();
return new PropertyType(javaType, jdbcType, columnTag.isLOB(), range);
} else {
// Try the rules
if (typeSolverType != null) {
return typeSolverType;
}
// Otherwise, use the default type from the database adapter
return adapter.getAdapterDefaultType(cm);
}
}
public void setEnumMetadata(final EnumDataSetMetadata enumMetadata) {
log.debug("[mark enum column] name=" + this.columnName + " enum=" + enumMetadata.getJdbcName());
this.enumMetadata = enumMetadata;
}
// From another ColumnMetadata object
protected ColumnMetadata(final ColumnMetadata cm) {
this.dataSet = cm.dataSet;
this.c = cm.c;
this.columnName = cm.columnName;
this.tableName = cm.tableName;
this.id = cm.id;
this.belongsToPK = cm.belongsToPK;
this.autogenerationType = cm.autogenerationType;
this.dataType = cm.dataType;
this.typeName = cm.typeName;
this.columnSize = cm.columnSize;
this.decimalDigits = cm.decimalDigits;
this.columnDefault = cm.columnDefault;
this.enumMetadata = cm.enumMetadata;
this.adapter = cm.adapter;
this.tag = cm.tag;
this.type = cm.type;
this.typeSolverTag = cm.typeSolverTag;
this.isVersionControlColumn = cm.isVersionControlColumn;
this.reusesMemberFromSuperClass = false;
}
// From a