org.dbflute.dbmeta.info.ColumnInfo Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of dbflute-runtime Show documentation
Show all versions of dbflute-runtime Show documentation
The runtime library of DBFlute
/*
* Copyright 2014-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
package org.dbflute.dbmeta.info;
import java.io.Serializable;
import java.lang.reflect.Method;
import java.sql.Time;
import java.sql.Timestamp;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import org.dbflute.Entity;
import org.dbflute.dbmeta.DBMeta;
import org.dbflute.dbmeta.DBMeta.OptimisticLockType;
import org.dbflute.dbmeta.name.ColumnSqlName;
import org.dbflute.dbmeta.property.PropertyGateway;
import org.dbflute.dbmeta.property.PropertyMethodFinder;
import org.dbflute.jdbc.Classification;
import org.dbflute.jdbc.ClassificationMeta;
import org.dbflute.util.DfReflectionUtil;
import org.dbflute.util.DfTypeUtil;
import org.dbflute.util.Srl;
/**
* The information of column.
* @author jflute
*/
public class ColumnInfo {
// ===================================================================================
// Definition
// ==========
/** The empty read-only list for empty property. */
protected static final List EMPTY_LIST = Collections.unmodifiableList(new ArrayList());
/** The type name of Java8Time's just date. */
protected static final String JAVA8_JUST_DATE_TYPE = LocalDate.class.getName();
/** The type name of Java8Time's just time-stamp. */
protected static final String JAVA8_JUST_TIMESTAMP_TYPE = LocalDateTime.class.getName();
/** The type name of Java8Time's just time. */
protected static final String JAVA8_JUST_TIME_TYPE = LocalTime.class.getName();
/** The set of type name for Java8Time judgment. */
protected static final Set JAVA8_DATE_TYPE_SET;
static {
final Set tmpSet = new HashSet();
tmpSet.add(JAVA8_JUST_DATE_TYPE);
tmpSet.add(JAVA8_JUST_TIMESTAMP_TYPE);
tmpSet.add(JAVA8_JUST_TIME_TYPE);
JAVA8_DATE_TYPE_SET = Collections.unmodifiableSet(tmpSet);
}
// ===================================================================================
// Attribute
// =========
protected final DBMeta _dbmeta;
protected final String _columnDbName;
protected final ColumnSqlName _columnSqlName;
protected final String _columnSynonym;
protected final String _columnAlias;
protected final String _propertyName;
protected final Class> _objectNativeType;
protected final Class> _propertyAccessType;
protected final boolean _primary;
protected final boolean _autoIncrement;
protected final boolean _notNull;
protected final String _columnDbType;
protected final Integer _columnSize;
protected final Integer _decimalDigits;
protected final Integer _datetimePrecision;
protected final String _defaultValue;
protected final boolean _commonColumn;
protected final OptimisticLockType _optimisticLockType;
protected final String _columnComment;
protected final List _foreignPropList;
protected final List _referrerPropList;
protected final boolean _foreignKey;
protected final ClassificationMeta _classificationMeta;
protected final boolean _canBeNullObject;
protected final PropertyGateway _propertyGateway;
protected final PropertyMethodFinder _propertyMethodFinder;
protected final Method _readMethod;
protected final Method _writeMethod;
// ===================================================================================
// Constructor
// ===========
public ColumnInfo(DBMeta dbmeta // DB meta
, String columnDbName, String columnSqlName, String columnSynonym, String columnAlias // column name
, Class> objectNativeType, String propertyName, Class> propertyAccessType // property info
, boolean primary, boolean autoIncrement, boolean notNull // column basic check
, String columnDbType, Integer columnSize, Integer decimalDigits, Integer datetimePrecision, String defaultValue // column type
, boolean commonColumn, OptimisticLockType optimisticLockType, String columnComment // column others
, List foreignPropList, List referrerPropList // relation property
, ClassificationMeta classificationMeta, boolean canBeNullObject, PropertyMethodFinder propertyMethodFinder // various info
) { // big constructor
assertObjectNotNull("dbmeta", dbmeta);
assertObjectNotNull("columnDbName", columnDbName);
assertObjectNotNull("columnSqlName", columnSqlName);
assertObjectNotNull("objectNativeType", objectNativeType);
assertObjectNotNull("propertyName", propertyName);
assertObjectNotNull("propertyAccessType", propertyAccessType);
assertObjectNotNull("propertyMethodFinder", propertyMethodFinder);
_dbmeta = dbmeta;
_columnDbName = columnDbName;
_columnSqlName = new ColumnSqlName(columnSqlName);
_columnSynonym = columnSynonym;
_columnAlias = columnAlias;
_objectNativeType = objectNativeType;
_propertyName = propertyName;
_propertyAccessType = propertyAccessType;
_primary = primary;
_autoIncrement = autoIncrement;
_notNull = notNull;
_columnSize = columnSize;
_columnDbType = columnDbType;
_decimalDigits = decimalDigits;
_datetimePrecision = datetimePrecision;
_defaultValue = defaultValue;
_commonColumn = commonColumn;
_optimisticLockType = optimisticLockType != null ? optimisticLockType : OptimisticLockType.NONE;
_columnComment = columnComment;
_foreignPropList = foreignPropList != null ? foreignPropList : EMPTY_LIST;
_referrerPropList = referrerPropList != null ? referrerPropList : EMPTY_LIST;
_foreignKey = foreignPropList != null && !foreignPropList.isEmpty();
_classificationMeta = classificationMeta;
_canBeNullObject = canBeNullObject;
_propertyGateway = findPropertyGateway();
_propertyMethodFinder = propertyMethodFinder; // before finding
_readMethod = findReadMethod();
_writeMethod = findWriteMethod();
}
// ===================================================================================
// Reflection
// ==========
// -----------------------------------------------------
// Gateway
// -------
/**
* Get the property gateway for the column.
* @return The property gateway for the column, cached in this instance. (NotNull)
*/
public PropertyGateway getPropertyGateway() {
return _propertyGateway;
}
// -----------------------------------------------------
// Read
// ----
/**
* Read the value from the entity by its gateway (means no reflection).
* It returns plain value in entity as property access type.
* @param The type of the property.
* @param entity The target entity of this column to read. (NotNull)
* @return The read value. (NullAllowed)
*/
@SuppressWarnings("unchecked")
public PROPERTY read(Entity entity) {
return (PROPERTY) _propertyGateway.read(entity);
}
/**
* Get the read method for entity reflection.
* @return The read method, cached in this instance. (NotNull)
*/
public Method getReadMethod() { // basically unused in DBFlute, use gateway instead
return _readMethod;
}
// -----------------------------------------------------
// Write
// -----
/**
* Write the value to the entity by its gateway (means no reflection).
* It contains the basic conversion, but no converting to optional so check the property access type.
* @param entity The target entity of this column to write. (NotNull)
* @param value The written value. (NullAllowed: if null, null value is written)
*/
public void write(Entity entity, Object value) {
_propertyGateway.write(entity, value);
}
/**
* Get the write method for entity reflection.
* @return The writer method, cached in this instance. (NotNull)
*/
public Method getWriteMethod() { // basically unused in DBFlute, use gateway instead
return _writeMethod;
}
// -----------------------------------------------------
// Generic
// -------
/**
* Get the (first) generic type of property type for list property.
* @return The type instance. (NullAllowed: e.g. when not list type)
*/
public Class> getGenericType() {
return DfReflectionUtil.getGenericFirstClass(getReadMethod().getGenericReturnType());
}
// -----------------------------------------------------
// Finder
// ------
protected PropertyGateway findPropertyGateway() {
final PropertyGateway gateway = _dbmeta.findPropertyGateway(_propertyName);
if (gateway == null) { // no way
String msg = "Not found the property gateway by the name: " + _propertyName;
throw new IllegalStateException(msg);
}
return gateway;
}
protected Method findReadMethod() {
final Class extends Entity> entityType = _dbmeta.getEntityType();
return _propertyMethodFinder.findReadMethod(entityType, _propertyName, _propertyAccessType);
}
protected Method findWriteMethod() {
final Class extends Entity> entityType = _dbmeta.getEntityType();
return _propertyMethodFinder.findWriteMethod(entityType, _propertyName, _propertyAccessType);
}
// ===================================================================================
// Convert Type
// ============
/**
* Convert the value to object native type.
* @param The type of column value.
* @param value The conversion target value. (NullAllowed: if null, returns null)
* @return The converted value as object native type. (NullAllowed: when the value is null)
*/
@SuppressWarnings("unchecked")
public VALUE convertToObjectNativeType(Object value) {
final VALUE result;
if (value instanceof Collection>) {
final Collection> valueList = (Collection>) value;
final List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy