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 SubQueryClause {
 
     // ===================================================================================
     //                                                                           Attribute
     //                                                                           =========
     protected final SubQueryPath _subQueryPath;
     protected final String _selectClause// needed for union
     protected final SqlClause _subQuerySqlClause;
     protected final String _localAliasName;
 
     // ===================================================================================
     //                                                                         Constructor
     //                                                                         ===========
     

Parameters:
subQueryPath The property path of sub-query. (NotNull)
selectClause The select clause of sub-query. (NotNull)
subQuerySqlClause The SQL clause for sub-query. (NotNull)
localAliasName The alias name of sub-query local table. (NullAllowed: if plain)
 
     public SubQueryClause(SubQueryPath subQueryPathString selectClauseSqlClause subQuerySqlClauseString localAliasName) {
          = subQueryPath;
          = selectClause;
          = subQuerySqlClause;
          = localAliasName;
     }
 
     // ===================================================================================
     //                                                                               Plain
     //                                                                               =====
     public String buildPlainSubQueryFromWhereClause(String correlatedFixedCondition) {
         if (correlatedFixedCondition == null) { // basically here
             String clause = .getClauseFromWhereWithUnionTemplate();
             clause = resolveParameterLocationPath(clause);
             clause = replaceString(clausegetUnionSelectClauseMark(), );
             clause = replaceString(clausegetUnionWhereClauseMark(), "");
             clause = replaceString(clausegetUnionWhereFirstConditionMark(), "");
             return clause;
         }
         // e.g. biz-many-to-one
         final String correlationCondition = correlatedFixedCondition// only fixed condition
         final String firstConditionAfter = ln() + "   and ";
         clause = resolveParameterLocationPath(clause);
         clause = replaceString(clausegetWhereClauseMark(), ln() + " where " + correlationCondition);
         clause = replaceString(clausegetWhereFirstConditionMark(), correlationCondition + firstConditionAfter);
         clause = replaceString(clausegetUnionSelectClauseMark(), );
         clause = replaceString(clausegetUnionWhereClauseMark(), ln() + " where " + correlationCondition);
         clause = replaceString(clausegetUnionWhereFirstConditionMark(), correlationCondition + firstConditionAfter);
         return clause;
     }
 
     // ===================================================================================
     //                                                                         Correlation
     //                                                                         ===========
     
Build the clause of correlation sub-query from from-where clause.

Parameters:
correlatedColumnRealName The real name of correlated column that is main-query table's column. (NotNull)
relatedColumnSqlName The real name of related column that is sub-query table's column. (NotNull)
correlatedFixedCondition The fixed condition as correlated condition. (NullAllowed)
Returns:
The clause string of correlation sub-query. (NotNull)
 
     public String buildCorrelationSubQueryFromWhereClause(ColumnRealName correlatedColumnRealNameColumnSqlName relatedColumnSqlName,
             String correlatedFixedCondition) {
         final String clause = xprepareCorrelationSubQueryFromWhereClause();
         final String joinCondition =  + "." + relatedColumnSqlName + " = " + correlatedColumnRealName;
         return xreplaceCorrelationSubQueryFromWhereClause(clausejoinConditioncorrelatedFixedCondition);
     }

    
Build the clause of correlation sub-query from from-where clause.

Parameters:
correlatedColumnRealNames The real names of correlated column that is main-query table's column. (NotNull)
relatedColumnSqlNames The real names of related column that is sub-query table's column. (NotNull)
correlatedFixedCondition The fixed condition as correlated condition. (NullAllowed)
Returns:
The clause string of correlation sub-query. (NotNull)
    public String buildCorrelationSubQueryFromWhereClause(ColumnRealName[] correlatedColumnRealNames,
            ColumnSqlName[] relatedColumnSqlNamesString correlatedFixedCondition) {
        final String joinCondition;
        final StringBuilder sb = new StringBuilder();
        for (int i = 0; i < relatedColumnSqlNames.lengthi++) {
            if (sb.length() > 0) {
                sb.append(ln()).append("   and ");
            }
            sb.append().append(".").append(relatedColumnSqlNames[i]);
            sb.append(" = ").append(correlatedColumnRealNames[i]);
        }
        joinCondition = sb.toString();
        clause = xreplaceCorrelationSubQueryFromWhereClause(clausejoinConditioncorrelatedFixedCondition);
        return clause;
    }
        return resolveParameterLocationPath(clause);
    }
    protected String xreplaceCorrelationSubQueryFromWhereClause(String clauseString joinConditionString fixedCondition) {
        final String correlationCondition;
        if (fixedCondition != null && fixedCondition.trim().length() > 0) {
            correlationCondition = joinCondition + ln() + "   and " + fixedCondition;
        } else {
            correlationCondition = joinCondition;
        }
        final String firstConditionAfter = ln() + "   and ";
        clause = replaceString(clausegetWhereClauseMark(), ln() + " where " + correlationCondition);
        clause = replaceString(clausegetWhereFirstConditionMark(), correlationCondition + firstConditionAfter);
        clause = replaceString(clausegetUnionSelectClauseMark(), );
        clause = replaceString(clausegetUnionWhereClauseMark(), ln() + " where " + correlationCondition);
        clause = replaceString(clausegetUnionWhereFirstConditionMark(), correlationCondition + firstConditionAfter);
        return clause;
    }
    // ===================================================================================
    //                                                                       Assist Helper
    //                                                                       =============
    protected String resolveParameterLocationPath(String clauseSubQueryPath subQueryPath) {
        return subQueryPath.resolveParameterLocationPath(clause);
    }
    // ===================================================================================
    //                                                                          Alias Name
    //                                                                          ==========
    protected String getBasePointAliasName() {
        return .getBasePointAliasName();
    }
    // ===================================================================================
    //                                                                       Template Mark
    //                                                                       =============
    protected String getWhereClauseMark() {
        return .getWhereClauseMark();
    }
    protected String getWhereFirstConditionMark() {
    }
    protected String getUnionSelectClauseMark() {
    }
    protected String getUnionWhereClauseMark() {
    }
    }
    // ===================================================================================
    //                                                                      General Helper
    //                                                                      ==============
    protected final String replaceString(String textString fromTextString toText) {
        return Srl.replace(textfromTexttoText);
    }
    protected String initCap(String str) {
        return Srl.initCap(str);
    }
    protected String initUncap(String str) {
        return Srl.initUncap(str);
    }
    protected String ln() {
        return DBFluteSystem.ln();
    }
New to GrepCode? Check out our FAQ X