use of org.dbflute.s2dao.metadata.TnPropertyType in project dbflute-core by dbflute.
the class TnQueryUpdateDynamicCommand method execute.
// ===================================================================================
// Execute
// =======
public Object execute(Object[] args) {
// analyze arguments
final Entity entity = extractEntityWithCheck(args);
final ConditionBean cb = extractConditionBeanWithCheck(args);
final UpdateOption<ConditionBean> option = extractUpdateOptionWithCheck(args);
prepareStatementConfigOnThreadIfExists(option);
// arguments for execution (not contains an option)
final String[] argNames = new String[] { "entity", "pmb" };
final Class<?>[] argTypes = new Class<?>[] { entity.getClass(), cb.getClass() };
final Object[] realArgs = new Object[] { entity, cb };
// prepare context
final List<TnPropertyType> boundPropTypeList = new ArrayList<TnPropertyType>();
final CommandContext context;
{
final String twoWaySql = buildQueryUpdateTwoWaySql(entity, cb, option, boundPropTypeList);
if (twoWaySql == null) {
// non execute
return 0;
}
context = createCommandContext(twoWaySql, argNames, argTypes, realArgs);
}
// execute
final TnCommandContextHandler handler = createCommandContextHandler(context);
handler.setExceptionMessageSqlArgs(context.getBindVariables());
handler.setFirstBoundPropTypeList(boundPropTypeList);
final int rows = handler.execute(realArgs);
return Integer.valueOf(rows);
}
use of org.dbflute.s2dao.metadata.TnPropertyType 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.s2dao.metadata.TnPropertyType in project dbflute-core by dbflute.
the class TnUpdateEntityDynamicCommand method createUpdateSql.
// ===================================================================================
// Update SQL
// ==========
/**
* Create update SQL. The update is by the primary keys or unique keys.
* @param bean The bean of the entity to update. (NotNull)
* @param propertyTypes The types of property for update. (NotNull)
* @param option An option of update. (NullAllowed)
* @return The update SQL. (NotNull)
*/
protected String createUpdateSql(Object bean, TnPropertyType[] propertyTypes, UpdateOption<ConditionBean> option) {
checkPrimaryKey();
final String tableDbName = _targetDBMeta.getTableDbName();
final Set<String> uniqueDrivenPropSet = extractUniqueDrivenPropSet(bean);
final StringBuilder sb = new StringBuilder(96);
sb.append("update ").append(_targetDBMeta.getTableSqlName()).append(" set ");
final String versionNoPropertyName = _beanMetaData.getVersionNoPropertyName();
int columnCount = 0;
for (TnPropertyType pt : propertyTypes) {
final String columnDbName = pt.getColumnDbName();
final ColumnSqlName columnSqlName = pt.getColumnSqlName();
final String propertyName = pt.getPropertyName();
if (uniqueDrivenPropSet != null && uniqueDrivenPropSet.contains(propertyName)) {
if (option != null && option.hasStatement(columnDbName)) {
throwUniqueDrivenColumnUpdateStatementException(tableDbName, columnDbName, uniqueDrivenPropSet);
}
continue;
}
if (columnCount > 0) {
sb.append(", ");
}
++columnCount;
if (propertyName.equalsIgnoreCase(versionNoPropertyName)) {
if (!isVersionNoAutoIncrementOnMemory()) {
setupVersionNoAutoIncrementOnQuery(sb, columnSqlName);
continue;
}
}
sb.append(columnSqlName).append(" = ");
final String valueExp;
if (option != null && option.hasStatement(columnDbName)) {
// prior to specified
final String statement = option.buildStatement(columnDbName);
valueExp = encryptIfNeeds(tableDbName, columnDbName, statement);
} else {
valueExp = encryptIfNeeds(tableDbName, columnDbName, "?");
}
sb.append(valueExp);
}
sb.append(ln());
setupUpdateWhere(sb, uniqueDrivenPropSet, _optimisticLockHandling);
return sb.toString();
}
use of org.dbflute.s2dao.metadata.TnPropertyType in project dbflute-core by dbflute.
the class TnAbstractEntityHandler method doSetupUpdateWhereBindVariables.
protected void doSetupUpdateWhereBindVariables(List<Object> varList, List<ValueType> varValueTypeList, Object bean, Set<String> uniqueDrivenPropSet) {
final TnBeanMetaData bmd = getBeanMetaData();
if (uniqueDrivenPropSet != null && !uniqueDrivenPropSet.isEmpty()) {
for (String uniqueProp : uniqueDrivenPropSet) {
final TnPropertyType pt = bmd.getPropertyType(uniqueProp);
doRegisterUpdateWhereBindVariable(varList, varValueTypeList, bean, pt);
}
} else {
for (int i = 0; i < bmd.getPrimaryKeySize(); ++i) {
final TnPropertyType pt = bmd.getPropertyTypeByColumnName(bmd.getPrimaryKeyDbName(i));
doRegisterUpdateWhereBindVariable(varList, varValueTypeList, bean, pt);
}
}
if (_optimisticLockHandling && bmd.hasVersionNoPropertyType()) {
final TnPropertyType pt = bmd.getVersionNoPropertyType();
doRegisterUpdateWhereBindVariable(varList, varValueTypeList, bean, pt);
}
if (_optimisticLockHandling && bmd.hasTimestampPropertyType()) {
final TnPropertyType pt = bmd.getTimestampPropertyType();
doRegisterUpdateWhereBindVariable(varList, varValueTypeList, bean, pt);
}
}
use of org.dbflute.s2dao.metadata.TnPropertyType in project dbflute-core by dbflute.
the class TnCommandContextHandler method bindFirstScope.
// ===================================================================================
// Bind Scope
// ==========
protected int bindFirstScope(Connection conn, PreparedStatement ps, Object[] bindVariables, Class<?>[] bindVariableTypes) {
final List<Object> firstVariableList = new ArrayList<Object>();
final List<ValueType> firstValueTypeList = new ArrayList<ValueType>();
int index = 0;
for (TnPropertyType propertyType : _firstBoundPropTypeList) {
firstVariableList.add(bindVariables[index]);
firstValueTypeList.add(propertyType.getValueType());
++index;
}
bindArgs(conn, ps, firstVariableList.toArray(), firstValueTypeList.toArray(new ValueType[0]));
return index;
}
Aggregations