Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2014-2015 the original author or authors.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
  * either express or implied. See the License for the specific language
  * governing permissions and limitations under the License.
  */
 package org.dbflute.cbean.sqlclause.subquery;
 

Author(s):
jflute
Since:
0.9.7.2 (2010/06/20 Sunday)
 
 public class ScalarCondition extends AbstractSubQuery {
 
     // ===================================================================================
     //                                                                           Attribute
     //                                                                           =========
     protected final String _mainSubQueryIdentity// NotNull
     protected final String _operand// NotNull
     protected final PartitionByProvider _partitionByProvider// NotNull
 
     // ===================================================================================
     //                                                                         Constructor
     //                                                                         ===========
     public ScalarCondition(SubQueryPath subQueryPathColumnRealNameProvider localRealNameProvider,
             ColumnSqlNameProvider subQuerySqlNameProviderint subQueryLevelSqlClause subQuerySqlClauseString subQueryIdentity,
             DBMeta subQueryDBMetaGearedCipherManager cipherManagerString mainSubQueryIdentityString operand,
             PartitionByProvider partitionByProvider) {
         super(subQueryPathlocalRealNameProvidersubQuerySqlNameProvidersubQueryLevelsubQuerySqlClausesubQueryIdentity,
                 subQueryDBMetacipherManager);
          = mainSubQueryIdentity;
          = operand;
          = partitionByProvider;
     }
 
     public static interface PartitionByProvider {
         SqlClause provideSqlClause();
     }
 
     // ===================================================================================
     //                                                                        Build Clause
     //                                                                        ============
     public String buildScalarCondition(String function) {
         // Get the specified column before it disappears at sub-query making.
         final ColumnRealName columnRealName;
         {
             final String columnDbName = .getSpecifiedColumnDbNameAsOne();
             if (columnDbName == null || columnDbName.trim().length() == 0) {
                 throwScalarConditionInvalidColumnSpecificationException(function);
             }
             columnRealName = .provide(columnDbName);
         }
 
         final String subQueryClause = getSubQueryClause(function);
         final String beginMark = resolveSubQueryBeginMark() + ln();
         final String endMark = resolveSubQueryEndMark();
         final String endIndent = "       ";
         final ColumnInfo columnInfo = .getSpecifiedColumnInfoAsOne();
         final String specifiedExp = decrypt(columnInfocolumnRealName.toString());
         return specifiedExp + " " +  // left and operand
                 + " (" + beginMark + subQueryClause + ln() + endIndent + ") " + endMark// right
     }
 
     protected String getSubQueryClause(String function) {
         // 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 derivedColumnDbName = .getSpecifiedColumnDbNameAsOne();
         if (derivedColumnDbName == null) {
         }
         final ColumnSqlName derivedColumnSqlName = getDerivedColumnSqlName();
         final ColumnRealName derivedColumnRealName = getDerivedColumnRealName();
         final String tableAliasName = derivedColumnRealName.getTableAliasName();
         assertScalarConditionColumnType(functionderivedColumnDbName);
         ColumnRealName partitionByCorrelatedColumnRealName = null;
         ColumnSqlName partitionByRelatedColumnSqlName = null;
        final SqlClause partitionBySqlClause = .provideSqlClause();
        if (partitionBySqlClause != null) {
            final String partitionByColumnDbName = partitionBySqlClause.getSpecifiedColumnDbNameAsOne();
            if (partitionByColumnDbName == null) { // means empty specify or duplicate specify
            }
            partitionByCorrelatedColumnRealName = .provide(partitionByColumnDbName);
            partitionByRelatedColumnSqlName = .provide(partitionByColumnDbName);
        }
        final String subQueryClause;
        if (.hasUnionQuery()) {
            subQueryClause =
                    getUnionSubQuerySql(functiontableAliasNamederivedColumnSqlNamederivedColumnRealName,
                            partitionByCorrelatedColumnRealNamepartitionByRelatedColumnSqlName);
        } else {
            final ColumnInfo columnInfo = .getSpecifiedColumnInfoAsOne();
            final String specifiedExp = decrypt(columnInfoderivedColumnRealName.toString());
            final String selectClause = "select " + function + "(" + specifiedExp + ")";
            final String fromWhereClause =
                    buildFromWhereClause(selectClausetableAliasNamepartitionByCorrelatedColumnRealNamepartitionByRelatedColumnSqlName);
            subQueryClause = selectClause + " " + fromWhereClause;
        }
        return resolveSubQueryLevelVariable(subQueryClause);
    }
    protected ColumnSqlName getDerivedColumnSqlName() {
    }
        return .getSpecifiedResolvedColumnRealNameAsOne(); // resolved calculation
    }
    protected String getUnionSubQuerySql(String functionString tableAliasName // basic
            , ColumnSqlName derivedColumnSqlNameColumnRealName derivedColumnRealName // derived
            , ColumnRealName partitionByCorrelatedColumnRealNameColumnSqlName partitionByRelatedColumnSqlName) { // partition-by
        final String beginMark = resolveSubQueryBeginMark() + ln();
        final String endMark = resolveSubQueryEndMark();
        final String mainSql;
        {
            final ColumnSqlName pkSqlName = .getPrimaryInfo().getFirstColumn().getColumnSqlName();
            final ColumnRealName pkRealName = ColumnRealName.create(tableAliasNamepkSqlName);
            final String selectClause = "select " + pkRealName + ", " + derivedColumnRealName;
            final String fromWhereClause =
                    buildFromWhereClause(selectClausetableAliasNamepartitionByCorrelatedColumnRealNamepartitionByRelatedColumnSqlName);
            mainSql = selectClause + " " + fromWhereClause;
        }
        final String mainAlias = buildSubQueryMainAliasName();
        final ColumnRealName mainDerivedColumnRealName = ColumnRealName.create(mainAliasderivedColumnSqlName);
        final ColumnInfo columnInfo = .getSpecifiedColumnInfoAsOne();
        final String specifiedExp = decrypt(columnInfomainDerivedColumnRealName.toString());
        return "select " + function + "(" + specifiedExp + ")" + ln() // select
                + "  from (" + beginMark + mainSql + ln() + "       ) " + mainAlias + endMark// from
    }
    protected String buildFromWhereClause(String selectClauseString tableAliasNameColumnRealName partitionByCorrelatedColumnRealName,
            ColumnSqlName partitionByRelatedColumnSqlName) {
        final String fromWhereClause;
        if (partitionByCorrelatedColumnRealName != null) {
            fromWhereClause =
                    buildCorrelationFromWhereClause(selectClausetableAliasNamepartitionByCorrelatedColumnRealName,
                            partitionByRelatedColumnSqlNamenull);
        } else {
            fromWhereClause = buildPlainFromWhereClause(selectClausetableAliasNamenull);
        }
        return fromWhereClause;
    }
    }
    }
    protected void assertScalarConditionColumnType(String functionString derivedColumnDbName) {
        if ("sum".equalsIgnoreCase(function) || "avg".equalsIgnoreCase(function)) {
            final ColumnInfo columnInfo = .findColumnInfo(derivedColumnDbName);
            final Class<?> deriveColumnType = columnInfo.getObjectNativeType();
            if (!columnInfo.isObjectNativeTypeNumber()) {
                throwScalarConditionUnmatchedColumnTypeException(functionderivedColumnDbNamederiveColumnType);
            }
        }
    }
    protected void throwScalarConditionUnmatchedColumnTypeException(String functionString derivedColumnDbNameClass<?> derivedColumnType) {
        createCBExThrower().throwScalarConditionUnmatchedColumnTypeException(functionderivedColumnDbNamederivedColumnType);
    }
New to GrepCode? Check out our FAQ X