use of org.dbflute.dbmeta.DBMeta in project dbflute-core by dbflute.
the class UpdateOption method xcheckSpecifiedUpdateColumnPrimaryKey.
// -----------------------------------------------------
// Update Process
// --------------
public void xcheckSpecifiedUpdateColumnPrimaryKey() {
// checked later by process if it needs
if (_updateColumnSpecification == null) {
return;
}
assertUpdateColumnSpecifiedCB();
final CB cb = _updateColumnSpecifiedCB;
final String basePointAliasName = cb.getSqlClause().getBasePointAliasName();
final DBMeta dbmeta = cb.asDBMeta();
if (dbmeta.hasPrimaryKey()) {
final PrimaryInfo pkInfo = dbmeta.getPrimaryInfo();
final List<ColumnInfo> pkList = pkInfo.getPrimaryColumnList();
for (ColumnInfo pk : pkList) {
final String columnDbName = pk.getColumnDbName();
if (cb.getSqlClause().hasSpecifiedSelectColumn(basePointAliasName, columnDbName)) {
String msg = "PK columns should not be allowed to specify as update columns: " + columnDbName;
throw new SpecifyUpdateColumnInvalidException(msg);
}
}
}
}
use of org.dbflute.dbmeta.DBMeta in project dbflute-core by dbflute.
the class UpdateOption method xacceptUpdateColumnModifiedPropertiesIfNeeds.
// -----------------------------------------------------
// Modified Properties
// -------------------
// for BatchUpdate
public void xacceptUpdateColumnModifiedPropertiesIfNeeds(List<? extends Entity> entityList) {
// internal
if (entityList == null) {
throw new IllegalArgumentException("The argument 'entityList' should not be null.");
}
if (_updateColumnSpecification != null) {
// already specified
return;
}
if (entityList.isEmpty()) {
// do nothing
return;
}
if (xisCompatibleBatchUpdateDefaultEveryColumn()) {
// every column for compatible
return;
}
final Entity firstEntity = entityList.get(0);
final Set<String> targetProps = xgatherUpdateColumnModifiedProperties(entityList, firstEntity);
final DBMeta dbmeta = firstEntity.asDBMeta();
specify(new SpecifyQuery<CB>() {
public void specify(CB cb) {
// you don't need to specify primary key because primary key has special handling
for (String prop : targetProps) {
final ColumnInfo info = dbmeta.findColumnInfo(prop);
if (!info.isPrimary()) {
// except PK
cb.localSp().xspecifyColumn(info.getColumnDbName());
}
}
}
});
}
use of org.dbflute.dbmeta.DBMeta in project dbflute-core by dbflute.
the class TnQueryUpdateDynamicCommand method buildQueryUpdateTwoWaySql.
// ===================================================================================
// Build SQL
// =========
/**
* @param entity The entity for update. (NotNull)
* @param cb The condition-beanĀ for query. (NotNull)
* @param option The option of update. (NullAllowed)
* @param boundPropTypeList The type list of bound property. (NotNull, AlwaysEmpty)
* @return The two-way SQL of query update. (NullAllowed: if non-modification, return null)
*/
protected String buildQueryUpdateTwoWaySql(Entity entity, ConditionBean cb, final UpdateOption<ConditionBean> option, List<TnPropertyType> boundPropTypeList) {
final Map<String, Object> columnParameterMap = new LinkedHashMap<String, Object>();
final DBMeta dbmeta = entity.asDBMeta();
final Set<String> modifiedSet = entity.mymodifiedProperties();
final List<ColumnInfo> columnInfoList = dbmeta.getColumnInfoList();
for (final ColumnInfo columnInfo : columnInfoList) {
if (columnInfo.isOptimisticLock()) {
// exclusive control columns are processed after here
continue;
}
final String columnDbName = columnInfo.getColumnDbName();
if (option != null && option.hasStatement(columnDbName)) {
// prior to specified
columnParameterMap.put(columnDbName, new SqlClause.QueryUpdateSetCalculationHandler() {
public String buildStatement(String aliasName) {
return option.buildStatement(columnDbName, aliasName);
}
});
continue;
}
if (isSpecifiedProperty(option, modifiedSet, columnInfo)) {
final String propertyName = columnInfo.getPropertyName();
final Object value = columnInfo.read(entity);
if (value != null) {
columnParameterMap.put(columnDbName, "/*entity." + propertyName + "*/null");
// add bound property type
final TnPropertyType propertyType = _beanMetaData.getPropertyType(propertyName);
boundPropTypeList.add(propertyType);
} else {
// it uses null literal on query
// because the SQL analyzer blocks null parameters
// (the analyzer should do it for condition-bean)
columnParameterMap.put(columnDbName, "null");
}
continue;
}
}
if (columnParameterMap.isEmpty()) {
return null;
}
if (dbmeta.hasVersionNo()) {
final ColumnInfo columnInfo = dbmeta.getVersionNoColumnInfo();
final String columnDbName = columnInfo.getColumnDbName();
columnParameterMap.put(columnDbName, new SqlClause.QueryUpdateSetCalculationHandler() {
public String buildStatement(String aliasName) {
// cipher for versionNo is unsupported
final ColumnSqlName columnSqlName = columnInfo.getColumnSqlName();
return (aliasName != null ? aliasName : "") + columnSqlName + " + 1";
}
});
}
if (dbmeta.hasUpdateDate()) {
ColumnInfo columnInfo = dbmeta.getUpdateDateColumnInfo();
columnInfo.write(entity, ResourceContext.getAccessTimestamp());
final String columnDbName = columnInfo.getColumnDbName();
final String propertyName = columnInfo.getPropertyName();
columnParameterMap.put(columnDbName, "/*entity." + propertyName + "*/null");
// add bound property type
boundPropTypeList.add(_beanMetaData.getPropertyType(propertyName));
}
if (option != null && option.isQueryUpdateForcedDirectAllowed()) {
cb.getSqlClause().enableQueryUpdateForcedDirect();
}
return cb.getSqlClause().getClauseQueryUpdate(columnParameterMap);
}
use of org.dbflute.dbmeta.DBMeta in project dbflute-core by dbflute.
the class TnQueryInsertDynamicCommand method buildQueryInsertTwoWaySql.
// ===================================================================================
// Build SQL
// =========
/**
* @param entity The entity for fixed values. (NotNull)
* @param intoCB The condition-bean for insert into. (NotNull)
* @param resourceCB The condition-bean for resource. (NotNull)
* @param option The option of insert. (NullAllowed)
* @param boundPropTypeList The type list of bound property. (NotNull, Empty)
* @return The two-way SQL of query-insert. (NotNull)
*/
protected String buildQueryInsertTwoWaySql(Entity entity, ConditionBean intoCB, ConditionBean resourceCB, InsertOption<ConditionBean> option, List<TnPropertyType> boundPropTypeList) {
final StringKeyMap<String> fixedValueQueryExpMap = StringKeyMap.createAsFlexibleOrdered();
final Set<String> modifiedProperties = entity.mymodifiedProperties();
final DBMeta dbmeta = entity.asDBMeta();
final List<ColumnInfo> columnInfoList = dbmeta.getColumnInfoList();
for (ColumnInfo columnInfo : columnInfoList) {
final String propertyName = columnInfo.getPropertyName();
if (!modifiedProperties.contains(propertyName)) {
continue;
}
final Object value = columnInfo.read(entity);
final String fixedValueQueryExp = (value != null ? "/*entity." + propertyName + "*/null" : null);
fixedValueQueryExpMap.put(columnInfo.getColumnDbName(), fixedValueQueryExp);
}
return intoCB.getSqlClause().getClauseQueryInsert(fixedValueQueryExpMap, resourceCB.getSqlClause());
}
Aggregations