Search in sources :

Example 1 with SqlClause

use of org.dbflute.cbean.sqlclause.SqlClause in project dbflute-core by dbflute.

the class HpSpecifyColumnRequiredChecker method checkSpecifyColumnRequiredIfNeeds.

public void checkSpecifyColumnRequiredIfNeeds(ConditionBean cb, Consumer<Set<String>> thrower) {
    // cannot embed this to SQL clause because of too complex
    // so simple implementation like this:
    final SqlClause sqlClause = cb.getSqlClause();
    final String basePointAliasName = sqlClause.getBasePointAliasName();
    final Set<String> nonSpecifiedAliasSet = new LinkedHashSet<>();
    if (!sqlClause.hasSpecifiedSelectColumn(basePointAliasName)) {
        // local table without SpecifyColumn
        nonSpecifiedAliasSet.add(cb.asDBMeta().getTableDispName() + " (" + basePointAliasName + ")");
    }
    for (Entry<String, Map<String, SelectedRelationColumn>> entry : sqlClause.getSelectedRelationColumnMap().entrySet()) {
        final String tableAliasName = entry.getKey();
        if (!sqlClause.hasSpecifiedSelectColumn(tableAliasName)) {
            // relation table without SpecifyColumn
            final Collection<SelectedRelationColumn> values = entry.getValue().values();
            final String dispName;
            if (!values.isEmpty()) {
                final SelectedRelationColumn firstColumn = values.iterator().next();
                dispName = sqlClause.translateSelectedRelationPathToPropName(firstColumn.getRelationNoSuffix());
            } else {
                // no way, just in case
                dispName = "*no name";
            }
            nonSpecifiedAliasSet.add(dispName + " (" + tableAliasName + ")");
        }
    }
    if (!nonSpecifiedAliasSet.isEmpty()) {
        thrower.accept(nonSpecifiedAliasSet);
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) SqlClause(org.dbflute.cbean.sqlclause.SqlClause) SelectedRelationColumn(org.dbflute.cbean.sqlclause.select.SelectedRelationColumn) Map(java.util.Map)

Example 2 with SqlClause

use of org.dbflute.cbean.sqlclause.SqlClause in project dbflute-core by dbflute.

the class ScalarCondition method buildSubQueryClause.

protected String buildSubQueryClause(String function, ScalarConditionOption option) {
    // release ScalarCondition for compound PK
    // (compound PK restricted until 1.0.5G without special reason)
    // if (!_subQueryDBMeta.hasPrimaryKey() || _subQueryDBMeta.hasCompoundPrimaryKey()) {
    // String msg = "The scalar-condition is unsupported when no primary key or compound primary key:";
    // msg = msg + " table=" + _subQueryDBMeta.getTableDbName();
    // throw new IllegalConditionBeanOperationException(msg);
    // }
    final String tableAliasName = getSubQueryLocalAliasName();
    final String derivedColumnDbName = _subQuerySqlClause.getSpecifiedColumnDbNameAsOne();
    if (derivedColumnDbName == null) {
        throwScalarConditionInvalidColumnSpecificationException(function);
    }
    final ColumnSqlName derivedColumnSqlName = getDerivedColumnSqlName();
    final ColumnRealName derivedColumnRealName = getDerivedColumnRealName();
    assertScalarConditionColumnType(function, derivedColumnDbName);
    ColumnRealName partitionByCorrelatedColumnRealName = null;
    ColumnSqlName partitionByRelatedColumnSqlName = null;
    final SqlClause partitionBySqlClause = _partitionByProvider.provideSqlClause();
    if (partitionBySqlClause != null) {
        final String partitionByColumnDbName = partitionBySqlClause.getSpecifiedColumnDbNameAsOne();
        if (partitionByColumnDbName == null) {
            // means empty specify or duplicate specify
            throwScalarConditionPartitionByInvalidColumnSpecificationException(function);
        }
        partitionByCorrelatedColumnRealName = _localRealNameProvider.provide(partitionByColumnDbName);
        partitionByRelatedColumnSqlName = _subQuerySqlNameProvider.provide(partitionByColumnDbName);
    }
    final String subQueryClause;
    if (_subQuerySqlClause.hasUnionQuery()) {
        subQueryClause = buildUnionSubQuerySql(function, tableAliasName, derivedColumnSqlName, derivedColumnRealName, partitionByCorrelatedColumnRealName, partitionByRelatedColumnSqlName, option);
    } else {
        final String selectClause = "select " + buildFunctionPart(function, derivedColumnRealName, option, false);
        final String fromWhereClause = buildFromWhereClause(selectClause, tableAliasName, partitionByCorrelatedColumnRealName, partitionByRelatedColumnSqlName);
        subQueryClause = selectClause + " " + fromWhereClause;
    }
    return resolveSubQueryLevelVariable(subQueryClause);
}
Also used : ColumnSqlName(org.dbflute.dbmeta.name.ColumnSqlName) SqlClause(org.dbflute.cbean.sqlclause.SqlClause) ColumnRealName(org.dbflute.dbmeta.name.ColumnRealName)

Example 3 with SqlClause

use of org.dbflute.cbean.sqlclause.SqlClause in project dbflute-core by dbflute.

the class TnRowCreatorExtension method adjustCreatedRow.

// ===================================================================================
// Fix Row
// =======
/**
 * Adjust created row. (clearing modified info, ...)
 * @param row The row of result list. (NotNull)
 * @param checkNonSp Does is use the check of access to non-specified column?
 * @param colNullObj Does is use the handling of column null object?
 * @param basePointBmd The bean meta data of the row for base-point table. (NotNull)
 * @param cb The condition-bean for the select. (NullAllowed: when not condition-bean select)
 */
public static void adjustCreatedRow(final Object row, boolean checkNonSp, boolean colNullObj, TnBeanMetaData basePointBmd, ConditionBean cb) {
    // _/_/_/_/_/_/_/_/_/_/
    if (row instanceof Entity) {
        final Entity entity = (Entity) row;
        // _/_/_/_/_/_/_/_/_/_/
        if (checkNonSp) {
            // contains enabled by CB and using SpecifyColumn
            entity.modifiedToSpecified();
            // _/_/_/_/_/_/_/_/_/_/
            if (colNullObj && cb != null) {
                // check 'cb' just in case
                final SqlClause sqlClause = cb.getSqlClause();
                for (ColumnInfo columnInfo : sqlClause.getLocalSpecifiedNullObjectColumnSet()) {
                    entity.myspecifyProperty(columnInfo.getPropertyName());
                }
            }
        }
        // only table that has null object target column is object-able (generated as it)
        if (colNullObj && entity instanceof ColumnNullObjectable) {
            ((ColumnNullObjectable) entity).enableColumnNullObject();
        }
        // clear modified properties for update process using selected entity
        entity.clearModifiedInfo();
        // mark as select to determine the entity is selected or user-created
        // basically for e.g. determine columns of batch insert
        entity.markAsSelect();
    } else {
        // not DBFlute entity
        // actually any bean meta data can be accepted
        // because only it gets modified properties
        basePointBmd.getModifiedPropertyNames(row).clear();
    }
}
Also used : Entity(org.dbflute.Entity) SqlClause(org.dbflute.cbean.sqlclause.SqlClause) ColumnInfo(org.dbflute.dbmeta.info.ColumnInfo) ColumnNullObjectable(org.dbflute.dbmeta.accessory.ColumnNullObjectable)

Example 4 with SqlClause

use of org.dbflute.cbean.sqlclause.SqlClause in project dbflute-core by dbflute.

the class TnRowCreatorExtension method processDerivedMap.

protected void processDerivedMap(ResultSet rs, Map<String, Map<String, Integer>> selectIndexMap, Map<String, TnPropertyMapping> propertyCache, Object row) throws SQLException {
    final ConditionBean cb = ConditionBeanContext.getConditionBeanOnThread();
    final SqlClause sqlClause = cb.getSqlClause();
    if (!sqlClause.hasSpecifiedDerivingSubQuery()) {
        return;
    }
    final DerivedMappable mappable = (DerivedMappable) row;
    final List<String> derivingAliasList = sqlClause.getSpecifiedDerivingAliasList();
    DerivedTypeHandler typeHandler = null;
    for (String derivingAlias : derivingAliasList) {
        // propertyCache has alias name when derived-referrer as case-insensitive
        if (propertyCache.containsKey(derivingAlias)) {
            // already handled
            continue;
        }
        if (!derivingAlias.startsWith(DERIVED_MAPPABLE_ALIAS_PREFIX)) {
            // might be exception but no need to be strict here
            continue;
        }
        if (typeHandler == null) {
            // basically fixed instance returned
            typeHandler = cb.xgetDerivedTypeHandler();
            if (typeHandler == null) {
                // no way, just in case
                String msg = "Not found the type handler from condition-bean: " + cb.asTableDbName();
                throw new IllegalStateException(msg);
            }
        }
        final HpDerivingSubQueryInfo derivingInfo = sqlClause.getSpecifiedDerivingInfo(derivingAlias);
        final ValueType valueType = TnValueTypes.getValueType(typeHandler.findMappingType(derivingInfo));
        final String onQueryAlias = Srl.substringFirstRear(derivingAlias, DERIVED_MAPPABLE_ALIAS_PREFIX);
        Object selectedValue = getValue(rs, onQueryAlias, valueType, selectIndexMap);
        selectedValue = typeHandler.convertToMapValue(derivingInfo, selectedValue);
        mappable.registerDerivedValue(derivingAlias, selectedValue);
    }
}
Also used : DerivedMappable(org.dbflute.dbmeta.accessory.DerivedMappable) ValueType(org.dbflute.jdbc.ValueType) DerivedTypeHandler(org.dbflute.dbmeta.accessory.DerivedTypeHandler) SqlClause(org.dbflute.cbean.sqlclause.SqlClause) HpDerivingSubQueryInfo(org.dbflute.cbean.chelper.HpDerivingSubQueryInfo) ConditionBean(org.dbflute.cbean.ConditionBean)

Example 5 with SqlClause

use of org.dbflute.cbean.sqlclause.SqlClause in project dbflute-core by dbflute.

the class TnBeanListResultSetHandler method createRelationSelector.

/**
 * Create the selector of relation.
 * @param hasCB Does the select use condition-bean?
 * @param cb The condition-bean for the select. (NullAllowed: not condition-bean select)
 * @return The created selector instance. (NotNull)
 */
protected TnRelationSelector createRelationSelector(final boolean hasCB, ConditionBean cb) {
    final boolean undefClsSel = isUndefinedClassificationSelectAllowed(hasCB, cb);
    final boolean colNullObj = isUseColumnNullObjectHandling(hasCB, cb);
    return new TnRelationSelector() {

        public boolean isNonLimitMapping() {
            return hasCB;
        }

        public boolean isNonSelectedRelation(String relationNoSuffix) {
            return hasCB && !cb.getSqlClause().hasSelectedRelation(relationNoSuffix);
        }

        public boolean isNonSelectedNextConnectingRelation(String relationNoSuffix) {
            return hasCB && !cb.getSqlClause().isSelectedNextConnectingRelation(relationNoSuffix);
        }

        public boolean canUseRelationCache(String relationNoSuffix) {
            return hasCB && cb.getSqlClause().canUseRelationCache(relationNoSuffix);
        }

        public boolean isNonSpecifiedColumnAccessAllowed(String relationNoSuffix) {
            return hasCB && cb.isNonSpecifiedColumnAccessAllowed();
        }

        public boolean isUsingSpecifyColumnInRelation(String relationNoSuffix) {
            if (!hasCB) {
                return false;
            }
            final SqlClause sqlClause = cb.getSqlClause();
            final String tableAlias = sqlClause.translateSelectedRelationPathToTableAlias(relationNoSuffix);
            if (tableAlias == null) {
                // no way but just in case
                return false;
            }
            return sqlClause.hasSpecifiedSelectColumn(tableAlias);
        }

        public Set<ColumnInfo> getRelationSpecifiedNullObjectColumnSet(String relationNoSuffix) {
            if (!hasCB) {
                return DfCollectionUtil.emptySet();
            }
            return cb.getSqlClause().getRelationSpecifiedNullObjectColumnSet(relationNoSuffix);
        }

        public boolean isUndefinedClassificationSelectAllowed(String relationNoSuffix) {
            return undefClsSel;
        }

        public boolean isColumnNullObjectEnabled(String relationNoSuffix) {
            return colNullObj;
        }
    };
}
Also used : SqlClause(org.dbflute.cbean.sqlclause.SqlClause) ColumnInfo(org.dbflute.dbmeta.info.ColumnInfo) TnRelationSelector(org.dbflute.s2dao.rowcreator.TnRelationSelector)

Aggregations

SqlClause (org.dbflute.cbean.sqlclause.SqlClause)20 ColumnInfo (org.dbflute.dbmeta.info.ColumnInfo)11 SpecifiedColumn (org.dbflute.cbean.dream.SpecifiedColumn)8 Map (java.util.Map)6 HpDerivingSubQueryInfo (org.dbflute.cbean.chelper.HpDerivingSubQueryInfo)6 Method (java.lang.reflect.Method)5 Timestamp (java.sql.Timestamp)5 LocalDate (java.time.LocalDate)5 LocalDateTime (java.time.LocalDateTime)5 ArrayList (java.util.ArrayList)5 Collection (java.util.Collection)5 Date (java.util.Date)5 HashMap (java.util.HashMap)5 Iterator (java.util.Iterator)5 LinkedHashMap (java.util.LinkedHashMap)5 List (java.util.List)5 Entry (java.util.Map.Entry)5 TimeZone (java.util.TimeZone)5 FunCustodial (org.dbflute.FunCustodial)5 HpFixedConditionQueryResolver (org.dbflute.cbean.chelper.HpFixedConditionQueryResolver)5