Search in sources :

Example 1 with ConditionBean

use of org.dbflute.cbean.ConditionBean in project dbflute-core by dbflute.

the class HpAbstractSpecification method reflectDreamCruiseWhereUsedToJoin.

protected void reflectDreamCruiseWhereUsedToJoin(String relationPath, String tableAliasName) {
    if (!_baseCB.xisDreamCruiseShip()) {
        return;
    }
    // to suppress CountLeastJoin of the relation
    // the DreamCruise might be used in where clause (not correctly but safety logic)
    final ConditionBean portCB = _baseCB.xgetDreamCruiseDeparturePort();
    final QueryUsedAliasInfo usedAliasInfo = new QueryUsedAliasInfo(tableAliasName, new InnerJoinNoWaySpeaker() {

        public boolean isNoWayInner() {
            // non fact of inner-join, because judge is so difficult when DreamCruise
            return true;
        }
    });
    portCB.getSqlClause().reflectWhereUsedToJoin(usedAliasInfo);
}
Also used : QueryUsedAliasInfo(org.dbflute.cbean.sqlclause.query.QueryUsedAliasInfo) ConditionBean(org.dbflute.cbean.ConditionBean) InnerJoinNoWaySpeaker(org.dbflute.cbean.sqlclause.join.InnerJoinNoWaySpeaker)

Example 2 with ConditionBean

use of org.dbflute.cbean.ConditionBean in project dbflute-core by dbflute.

the class AbstractSqlClause method filterSpecifyColumnCalculation.

protected ColumnRealName filterSpecifyColumnCalculation(ColumnRealName specifiedRealName, SpecifiedColumn hpCol) {
    if (hasSpecifyCalculation(hpCol)) {
        hpCol.xinitSpecifyCalculation();
        final HpCalcSpecification<ConditionBean> calcSpecification = hpCol.getSpecifyCalculation();
        final String statement = calcSpecification.buildStatementToSpecifidName(specifiedRealName.toString());
        return ColumnRealName.create(null, new ColumnSqlName(statement));
    }
    return specifiedRealName;
}
Also used : ColumnSqlName(org.dbflute.dbmeta.name.ColumnSqlName) ConditionBean(org.dbflute.cbean.ConditionBean)

Example 3 with ConditionBean

use of org.dbflute.cbean.ConditionBean in project dbflute-core by dbflute.

the class OrderByElement method setupManualOrderClause.

protected void setupManualOrderClause(StringBuilder sb, String columnAlias, Map<String, String> selectClauseRealColumnAliasMap) {
    final String realAlias;
    if (_manualOrderOption.hasOrderByCalculation()) {
        final HpCalcSpecification<ConditionBean> calculationOrder = _manualOrderOption.getOrderByCalculation();
        realAlias = calculationOrder.buildStatementToSpecifidName(columnAlias, selectClauseRealColumnAliasMap);
    } else {
        if (selectClauseRealColumnAliasMap != null) {
            // means union
            realAlias = columnAlias;
        } else {
            realAlias = decryptIfNeeds(_columnInfo, columnAlias);
        }
    }
    final List<HpMobCaseWhenElement> caseWhenList = _manualOrderOption.getCaseWhenBoundList();
    if (!caseWhenList.isEmpty()) {
        sb.append(ln()).append("   case").append(ln());
        int index = 0;
        for (HpMobCaseWhenElement element : caseWhenList) {
            sb.append("     when ");
            doSetupManualOrderClause(sb, realAlias, element);
            final List<HpMobCaseWhenElement> connectedElementList = element.getConnectedElementList();
            for (HpMobCaseWhenElement connectedElement : connectedElementList) {
                doSetupManualOrderClause(sb, realAlias, connectedElement);
            }
            final Object thenValue = element.getThenValue();
            final String thenExp;
            if (thenValue != null) {
                thenExp = thenValue.toString();
            } else {
                thenExp = String.valueOf(index);
            }
            sb.append(" then ").append(thenExp).append(ln());
            ++index;
        }
        final Object elseValue = _manualOrderOption.getElseValue();
        final String elseExp;
        if (elseValue != null) {
            elseExp = elseValue.toString();
        } else {
            elseExp = String.valueOf(index);
        }
        sb.append("     else ").append(elseExp).append(ln());
        sb.append("   end");
    } else {
        sb.append(realAlias);
    }
    sb.append(" ").append(_ascDesc);
}
Also used : HpMobCaseWhenElement(org.dbflute.cbean.chelper.HpMobCaseWhenElement) ConditionBean(org.dbflute.cbean.ConditionBean)

Example 4 with ConditionBean

use of org.dbflute.cbean.ConditionBean in project dbflute-core by dbflute.

the class TnQueryInsertDynamicCommand method execute.

// ===================================================================================
// Execute
// =======
public Object execute(Object[] args) {
    // analyze arguments
    final Entity entity = extractEntityWithCheck(args);
    final ConditionBean intoCB = extractIntoConditionBeanWithCheck(args);
    final ConditionBean resourceCB = extractResourceConditionBeanWithCheck(args);
    final InsertOption<ConditionBean> option = extractInsertOptionWithCheck(args);
    prepareStatementConfigOnThreadIfExists(option);
    // arguments for execution (not contains an option)
    final String[] argNames = new String[] { "entity", "pmb" };
    final Class<?>[] argTypes = new Class<?>[] { entity.getClass(), resourceCB.getClass() };
    final Object[] realArgs = new Object[] { entity, resourceCB };
    // prepare context
    final List<TnPropertyType> boundPropTypeList = new ArrayList<TnPropertyType>();
    final CommandContext context;
    {
        final String twoWaySql = buildQueryInsertTwoWaySql(entity, intoCB, resourceCB, option, boundPropTypeList);
        context = createCommandContext(twoWaySql, argNames, argTypes, realArgs);
    }
    // execute
    final TnCommandContextHandler handler = createCommandContextHandler(context);
    handler.setExceptionMessageSqlArgs(context.getBindVariables());
    final boolean identityDisabled = option != null && option.isPrimaryKeyIdentityDisabled();
    if (identityDisabled) {
        disableIdentityGeneration(entity);
    }
    final int rows;
    RuntimeException sqlEx = null;
    try {
        rows = handler.execute(realArgs);
    } catch (RuntimeException e) {
        sqlEx = e;
        throw e;
    } finally {
        if (identityDisabled) {
            try {
                enableIdentityGeneration(entity);
            } catch (RuntimeException e) {
                if (sqlEx == null) {
                    throw e;
                }
            // ignore the exception when main SQL fails
            // not to close the main exception
            }
        }
    }
    return Integer.valueOf(rows);
}
Also used : Entity(org.dbflute.Entity) CommandContext(org.dbflute.twowaysql.context.CommandContext) ArrayList(java.util.ArrayList) TnCommandContextHandler(org.dbflute.s2dao.sqlhandler.TnCommandContextHandler) ConditionBean(org.dbflute.cbean.ConditionBean) TnPropertyType(org.dbflute.s2dao.metadata.TnPropertyType)

Example 5 with ConditionBean

use of org.dbflute.cbean.ConditionBean in project dbflute-core by dbflute.

the class TnBeanListResultSetHandler method mappingBean.

protected void mappingBean(ResultSet rs, BeanRowHandler handler) throws SQLException {
    // lazy initialization because if the result is zero, the resources are unused
    Map<String, String> selectColumnMap = null;
    Map<String, TnPropertyMapping> propertyCache = null;
    // key is relationNoSuffix, columnName
    Map<String, Map<String, TnPropertyMapping>> relPropCache = null;
    TnRelationRowCache relRowCache = null;
    TnRelationSelector relSelector = null;
    final TnBeanMetaData basePointBmd = getBeanMetaData();
    // condition-bean info (variable for minimum thread local access)
    final boolean hasCB = hasConditionBean();
    final ConditionBean cb = hasCB ? getConditionBean() : null;
    // outsideSql info (also variable for minimum thread local access)
    final boolean hasOql = hasOutsideSqlContext();
    final OutsideSqlContext oqlCtx = OutsideSqlContext.getOutsideSqlContextOnThread();
    final boolean checkNonSp = checkNonSpecifiedColumnAccess(hasCB, cb, hasOql, oqlCtx);
    final boolean colNullObj = isUseColumnNullObjectHandling(hasCB, cb);
    final boolean skipRelationLoop;
    {
        final boolean emptyRelationCB = hasCB && isSelectedRelationEmpty(cb);
        final boolean specifiedOutsideSql = hasOql && isSpecifiedOutsideSql(oqlCtx);
        // if it has condition-bean that has no relation to get
        // or it has outside SQL context that is specified outside-SQL,
        // they are unnecessary to do relation loop
        skipRelationLoop = emptyRelationCB || specifiedOutsideSql;
    }
    // null allowed
    final Map<String, Map<String, Integer>> selectIndexMap = ResourceContext.getSelectIndexMap();
    while (rs.next()) {
        if (selectColumnMap == null) {
            selectColumnMap = createSelectColumnMap(rs);
        }
        if (propertyCache == null) {
            propertyCache = createPropertyCache(selectColumnMap, selectIndexMap);
        }
        // create row instance of base table by row property cache
        final Object row = createRow(rs, selectIndexMap, propertyCache, cb);
        if (skipRelationLoop) {
            adjustCreatedRow(row, checkNonSp, colNullObj, basePointBmd, cb);
            final boolean continueToNext = handler.handle(row);
            if (!continueToNext) {
                // skip rear records (basically for cursor select)
                break;
            }
            continue;
        }
        if (relSelector == null) {
            relSelector = createRelationSelector(hasCB, cb);
        }
        if (relPropCache == null) {
            relPropCache = createRelationPropertyCache(selectColumnMap, selectIndexMap, relSelector);
        }
        if (relRowCache == null) {
            relRowCache = createRelationRowCache(hasCB, cb);
        }
        final List<TnRelationPropertyType> rptList = basePointBmd.getRelationPropertyTypeList();
        for (TnRelationPropertyType rpt : rptList) {
            if (relSelector.isNonSelectedRelation(rpt.getRelationNoSuffixPart())) {
                continue;
            }
            mappingFirstRelation(rs, row, rpt, selectColumnMap, selectIndexMap, relPropCache, relRowCache, relSelector);
        }
        adjustCreatedRow(row, checkNonSp, colNullObj, basePointBmd, cb);
        final boolean continueToNext = handler.handle(row);
        if (!continueToNext) {
            // skip rear records (basically for cursor select)
            break;
        }
    }
}
Also used : TnRelationPropertyType(org.dbflute.s2dao.metadata.TnRelationPropertyType) ConditionBean(org.dbflute.cbean.ConditionBean) TnPropertyMapping(org.dbflute.s2dao.metadata.TnPropertyMapping) TnRelationRowCache(org.dbflute.s2dao.rowcreator.TnRelationRowCache) OutsideSqlContext(org.dbflute.outsidesql.OutsideSqlContext) TnBeanMetaData(org.dbflute.s2dao.metadata.TnBeanMetaData) Map(java.util.Map) TnRelationSelector(org.dbflute.s2dao.rowcreator.TnRelationSelector)

Aggregations

ConditionBean (org.dbflute.cbean.ConditionBean)45 DeleteOption (org.dbflute.bhv.writable.DeleteOption)7 UpdateOption (org.dbflute.bhv.writable.UpdateOption)7 ArrayList (java.util.ArrayList)5 InsertOption (org.dbflute.bhv.writable.InsertOption)5 Entity (org.dbflute.Entity)4 ExceptionMessageBuilder (org.dbflute.helper.message.ExceptionMessageBuilder)4 List (java.util.List)3 AbstractBatchUpdateCommand (org.dbflute.bhv.core.command.AbstractBatchUpdateCommand)3 BatchDeleteCommand (org.dbflute.bhv.core.command.BatchDeleteCommand)3 BatchDeleteNonstrictCommand (org.dbflute.bhv.core.command.BatchDeleteNonstrictCommand)3 BatchInsertCommand (org.dbflute.bhv.core.command.BatchInsertCommand)3 BatchUpdateCommand (org.dbflute.bhv.core.command.BatchUpdateCommand)3 BatchUpdateNonstrictCommand (org.dbflute.bhv.core.command.BatchUpdateNonstrictCommand)3 DeleteEntityCommand (org.dbflute.bhv.core.command.DeleteEntityCommand)3 DeleteNonstrictEntityCommand (org.dbflute.bhv.core.command.DeleteNonstrictEntityCommand)3 InsertEntityCommand (org.dbflute.bhv.core.command.InsertEntityCommand)3 QueryDeleteCBCommand (org.dbflute.bhv.core.command.QueryDeleteCBCommand)3 QueryInsertCBCommand (org.dbflute.bhv.core.command.QueryInsertCBCommand)3 QueryUpdateCBCommand (org.dbflute.bhv.core.command.QueryUpdateCBCommand)3