Search in sources :

Example 26 with ColumnSqlName

use of org.dbflute.dbmeta.name.ColumnSqlName 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();
}
Also used : ColumnSqlName(org.dbflute.dbmeta.name.ColumnSqlName) TnPropertyType(org.dbflute.s2dao.metadata.TnPropertyType)

Example 27 with ColumnSqlName

use of org.dbflute.dbmeta.name.ColumnSqlName in project dbflute-core by dbflute.

the class TnAbstractEntityDynamicCommand method prepareWherePrimaryKey.

protected void prepareWherePrimaryKey(StringBuilder sb, Set<String> uniqueDrivenPropSet) {
    final String bindingSuffix = " = ?";
    final String connectorSuffix = " and ";
    if (uniqueDrivenPropSet != null && !uniqueDrivenPropSet.isEmpty()) {
        for (String uniqueProp : uniqueDrivenPropSet) {
            final ColumnSqlName sqlName = _targetDBMeta.findColumnInfo(uniqueProp).getColumnSqlName();
            sb.append(sqlName).append(bindingSuffix).append(connectorSuffix);
        }
    } else {
        // basically here
        for (int i = 0; i < _beanMetaData.getPrimaryKeySize(); i++) {
            // never zero loop
            ColumnSqlName sqlName = _beanMetaData.getPrimaryKeySqlName(i);
            sb.append(sqlName).append(bindingSuffix).append(connectorSuffix);
        }
    }
    // for deleting extra ' and '
    sb.setLength(sb.length() - connectorSuffix.length());
}
Also used : ColumnSqlName(org.dbflute.dbmeta.name.ColumnSqlName)

Example 28 with ColumnSqlName

use of org.dbflute.dbmeta.name.ColumnSqlName in project dbflute-core by dbflute.

the class TnInsertEntityDynamicCommand method createInsertSql.

// ===================================================================================
// Insert SQL
// ==========
protected String createInsertSql(TnBeanMetaData bmd, TnPropertyType[] propertyTypes, InsertOption<ConditionBean> option) {
    final String tableDbName = _targetDBMeta.getTableDbName();
    final StringBuilder columnSb = new StringBuilder(48);
    final StringBuilder valuesSb = new StringBuilder(48);
    for (int i = 0; i < propertyTypes.length; ++i) {
        final TnPropertyType pt = propertyTypes[i];
        final ColumnSqlName columnSqlName = pt.getColumnSqlName();
        if (i > 0) {
            columnSb.append(", ");
            valuesSb.append(", ");
        }
        columnSb.append(columnSqlName);
        final String columnDbName = pt.getColumnDbName();
        valuesSb.append(encryptIfNeeds(tableDbName, columnDbName, "?"));
    }
    final StringBuilder sb = new StringBuilder(128);
    sb.append("insert into ").append(_targetDBMeta.getTableSqlName());
    sb.append(" (").append(columnSb).append(")");
    sb.append(ln()).append(" values (").append(valuesSb).append(")");
    return sb.toString();
}
Also used : ColumnSqlName(org.dbflute.dbmeta.name.ColumnSqlName) TnPropertyType(org.dbflute.s2dao.metadata.TnPropertyType)

Aggregations

ColumnSqlName (org.dbflute.dbmeta.name.ColumnSqlName)28 ColumnInfo (org.dbflute.dbmeta.info.ColumnInfo)14 ColumnRealName (org.dbflute.dbmeta.name.ColumnRealName)8 DBMeta (org.dbflute.dbmeta.DBMeta)5 SpecifiedColumn (org.dbflute.cbean.dream.SpecifiedColumn)4 IllegalConditionBeanOperationException (org.dbflute.exception.IllegalConditionBeanOperationException)3 TnPropertyType (org.dbflute.s2dao.metadata.TnPropertyType)3 LinkedHashMap (java.util.LinkedHashMap)2 SqlClause (org.dbflute.cbean.sqlclause.SqlClause)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 ConditionBean (org.dbflute.cbean.ConditionBean)1 ColumnFunctionCipher (org.dbflute.cbean.cipher.ColumnFunctionCipher)1 SelectedRelationColumn (org.dbflute.cbean.sqlclause.select.SelectedRelationColumn)1 OnQueryStringConnector (org.dbflute.dbway.OnQueryStringConnector)1 StringKeyMap (org.dbflute.helper.StringKeyMap)1 ValueType (org.dbflute.jdbc.ValueType)1 TnPropertyTypeImpl (org.dbflute.s2dao.metadata.impl.TnPropertyTypeImpl)1