${database.allClassCopyright}package ${glPackageBaseCB};
#set ($myClassName = "${myBaseConditionBeanClassName}")

import org.seasar.dbflute.cbean.AbstractConditionBean;
import org.seasar.dbflute.cbean.AndQuery;
import org.seasar.dbflute.cbean.ConditionBean;
import org.seasar.dbflute.cbean.ConditionQuery;
import org.seasar.dbflute.cbean.OrQuery;
import org.seasar.dbflute.cbean.SpecifyQuery;
import org.seasar.dbflute.cbean.SubQuery;
import org.seasar.dbflute.cbean.UnionQuery;
import org.seasar.dbflute.cbean.chelper.*;
import org.seasar.dbflute.cbean.coption.*;
import org.seasar.dbflute.cbean.sqlclause.SqlClause;
import org.seasar.dbflute.cbean.sqlclause.SqlClauseCreator;
import org.seasar.dbflute.dbmeta.DBMetaProvider;
import org.seasar.dbflute.twowaysql.factory.SqlAnalyzerFactory;
#if ($table.hasPrimaryKeyForcedClassificationSetting())
import ${glPackageBaseCommon}.${glCDef};
#end
import ${glPackageBaseCommon}.${glDBFluteConfig};
import ${glPackageBaseCommon}.${glDBMetaInstanceHandler};
import ${glPackageBaseCommon}.${glImplementedInvokerAssistant};
import ${glPackageBaseCommon}.${glImplementedSqlClauseCreator};
import ${glPackageCB}.*;
import ${glPackageCQ}.*;
#if ($table.hasForeignKeyOrReferrerAsOne())
import ${glPackageNss}.*;
#end

/**
 * The base condition-bean of ${table.name}.
 * @author ${database.classAuthor}
 */
public class ${myClassName} extends AbstractConditionBean {

    // ===================================================================================
    //                                                                           Attribute
    //                                                                           =========
    protected ${myConditionQueryClassName} _conditionQuery;

    // ===================================================================================
    //                                                                           SqlClause
    //                                                                           =========
    @Override
    protected SqlClause createSqlClause() {
        SqlClauseCreator creator = ${glDBFluteConfig}.getInstance().getSqlClauseCreator();
        if (creator != null) {
            return creator.createSqlClause(this);
        }
        return new ${glImplementedSqlClauseCreator}().createSqlClause(this); // as default
    }

    // ===================================================================================
    //                                                                     DBMeta Provider
    //                                                                     ===============
    @Override
    protected DBMetaProvider getDBMetaProvider() {
        return ${glDBMetaInstanceHandler}.getProvider(); // as default
    }

    // ===================================================================================
    //                                                                          Table Name
    //                                                                          ==========
    public String getTableDbName() {
        return "${table.name}";
    }

    // ===================================================================================
    //                                                                 PrimaryKey Handling
    //                                                                 ===================
#if ($table.hasPrimaryKey())
    public void acceptPrimaryKey(${table.primaryKeyArgsString}) {
        ${table.primaryKeyArgsAssertString}
        ${myClassName} cb = this;
        ${table.primaryKeyArgsConditionSetupString}
    }

#end
    public ConditionBean addOrderBy_PK_Asc() {
#if ($table.hasPrimaryKey())
#foreach ($col in $table.primaryKey)
        query().addOrderBy_${col.JavaName}_Asc();
#end
        return this;
#else
        String msg = "The table has no primary-keys: " + getTableDbName();
        throw new UnsupportedOperationException(msg);
#end
    }

    public ConditionBean addOrderBy_PK_Desc() {
#if ($table.hasPrimaryKey())
#foreach ($col in $table.primaryKey)
        query().addOrderBy_${col.JavaName}_Desc();
#end
        return this;
#else
        String msg = "The table has no primary-keys: " + getTableDbName();
        throw new UnsupportedOperationException(msg);
#end
    }

    // ===================================================================================
    //                                                                               Query
    //                                                                               =====
    /**
     * Prepare for various queries. <br />
     * Examples of main functions are following:
     * <pre>
     * <span style="color: #3F7E5E">// Basic Queries</span>
     * cb.query().setMemberId_Equal(value);        <span style="color: #3F7E5E">// =</span>
     * cb.query().setMemberId_NotEqual(value);     <span style="color: #3F7E5E">// !=</span>
     * cb.query().setMemberId_GreaterThan(value);  <span style="color: #3F7E5E">// &gt;</span>
     * cb.query().setMemberId_LessThan(value);     <span style="color: #3F7E5E">// &lt;</span>
     * cb.query().setMemberId_GreaterEqual(value); <span style="color: #3F7E5E">// &gt;=</span>
     * cb.query().setMemberId_LessEqual(value);    <span style="color: #3F7E5E">// &lt;=</span>
     * cb.query().setMemberName_InScope(valueList);    <span style="color: #3F7E5E">// in ('a', 'b')</span>
     * cb.query().setMemberName_NotInScope(valueList); <span style="color: #3F7E5E">// not in ('a', 'b')</span>
     * cb.query().setMemberName_PrefixSearch(value);   <span style="color: #3F7E5E">// like 'a%' escape '|'</span>
     * <span style="color: #3F7E5E">// LikeSearch with various options: (versatile)</span>
     * <span style="color: #3F7E5E">// {like ... [options]}</span>
     * cb.query().setMemberName_LikeSearch(value, option);
     * cb.query().setMemberName_NotLikeSearch(value, option); <span style="color: #3F7E5E">// not like ...</span>
     * <span style="color: #3F7E5E">// FromTo with various options: (versatile)</span>
     * <span style="color: #3F7E5E">// {(default) fromDatetime &lt;= BIRTHDATE &lt;= toDatetime}</span>
     * cb.query().setBirthdate_FromTo(fromDatetime, toDatetime, option);
     * <span style="color: #3F7E5E">// DateFromTo: (Date means yyyy/MM/dd)</span>
     * <span style="color: #3F7E5E">// {fromDate &lt;= BIRTHDATE &lt; toDate + 1 day}</span>
     * cb.query().setBirthdate_DateFromTo(fromDate, toDate);
     * cb.query().setBirthdate_IsNull();    <span style="color: #3F7E5E">// is null</span>
     * cb.query().setBirthdate_IsNotNull(); <span style="color: #3F7E5E">// is not null</span>
     * 
     * <span style="color: #3F7E5E">// ExistsReferrer: (co-related sub-query)</span>
     * <span style="color: #3F7E5E">// {where exists (select PURCHASE_ID from PURCHASE where ...)}</span>
     * cb.query().existsPurchaseList(new SubQuery&lt;PurchaseCB&gt;() {
     *     public void query(PurchaseCB subCB) {
     *         subCB.query().setXxx... <span style="color: #3F7E5E">// referrer sub-query condition</span>
     *     }
     * });
     * cb.query().notExistsPurchaseList...
     * 
     * <span style="color: #3F7E5E">// InScopeRelation: (sub-query)</span>
     * <span style="color: #3F7E5E">// {where MEMBER_STATUS_CODE in (select MEMBER_STATUS_CODE from MEMBER_STATUS where ...)}</span>
     * cb.query().inScopeMemberStatus(new SubQuery&lt;MemberStatusCB&gt;() {
     *     public void query(MemberStatusCB subCB) {
     *         subCB.query().setXxx... <span style="color: #3F7E5E">// relation sub-query condition</span>
     *     }
     * });
     * cb.query().notInScopeMemberStatus...
     * 
     * <span style="color: #3F7E5E">// (Query)DerivedReferrer: (co-related sub-query)</span>
     * cb.query().derivedPurchaseList().max(new SubQuery&lt;PurchaseCB&gt;() {
     *     public void query(PurchaseCB subCB) {
     *         subCB.specify().columnPurchasePrice(); <span style="color: #3F7E5E">// derived column for function</span>
     *         subCB.query().setXxx... <span style="color: #3F7E5E">// referrer sub-query condition</span>
     *     }
     * }).greaterEqual(value);
     * 
     * <span style="color: #3F7E5E">// ScalarCondition: (self-table sub-query)</span>
     * cb.query().scalar_Equal().max(new SubQuery&lt;MemberCB&gt;() {
     *     public void query(MemberCB subCB) {
     *         subCB.specify().columnBirthdate(); <span style="color: #3F7E5E">// derived column for function</span>
     *         subCB.query().setXxx... <span style="color: #3F7E5E">// scalar sub-query condition</span>
     *     }
     * });
     * 
     * <span style="color: #3F7E5E">// OrderBy</span>
     * cb.query().addOrderBy_MemberName_Asc();
     * cb.query().addOrderBy_MemberName_Desc().withManualOrder(valueList);
     * cb.query().addOrderBy_MemberName_Desc().withNullsFirst();
     * cb.query().addOrderBy_MemberName_Desc().withNullsLast();
     * cb.query().addSpecifiedDerivedOrderBy_Desc(aliasName);
     * 
     * <span style="color: #3F7E5E">// Query(Relation)</span>
     * cb.query().queryMemberStatus()...;
     * cb.query().queryMemberAddressAsValid(targetDate)...;
     * </pre>
     * @return The instance of condition-query for base-point table to set up query. (NotNull)
     */
    public ${myConditionQueryClassName} query() {
        assertQueryPurpose(); // assert only when user-public query 
        return getConditionQuery();
    }

    public ${myConditionQueryClassName} getConditionQuery() { // public for parameter comment and internal
        if (_conditionQuery == null) {
            _conditionQuery = createLocalCQ();
        }
        return _conditionQuery;
    }

    protected ${myConditionQueryClassName} createLocalCQ() {
        return xcreateCQ(null, getSqlClause(), getSqlClause().getBasePointAliasName(), 0);
    }

    protected ${myConditionQueryClassName} xcreateCQ(ConditionQuery childQuery, SqlClause sqlClause, String aliasName, int nestLevel) {
        return new ${myConditionQueryClassName}(childQuery, sqlClause, aliasName, nestLevel);
    }

    public ConditionQuery localCQ() {
        return getConditionQuery();
    }

    // ===================================================================================
    //                                                                               Union
    //                                                                               =====
    /**
     * Set up 'union' for base-point table. <br />
     * You don't need to call SetupSelect in union-query,
     * because it inherits calls before. (Don't call SetupSelect after here)
     * <pre>
     * cb.query().<span style="color: #FD4747">union</span>(new UnionQuery&lt;${myConditionBeanClassName}&gt;() {
     *     public void query(${myConditionBeanClassName} unionCB) {
     *         unionCB.query().setXxx...
     *     }
     * });
     * </pre>
     * @param unionQuery The query of 'union'. (NotNull)
     */
    public void union(UnionQuery<${myConditionBeanClassName}> unionQuery) {
        final ${myConditionBeanClassName} cb = new ${myConditionBeanClassName}();
        cb.xsetupForUnion(this); xsyncUQ(cb); unionQuery.query(cb);
        final ${myConditionQueryClassName} cq = cb.query(); query().xsetUnionQuery(cq);
    }

    /**
     * Set up 'union all' for base-point table. <br />
     * You don't need to call SetupSelect in union-query,
     * because it inherits calls before. (Don't call SetupSelect after here)
     * <pre>
     * cb.query().<span style="color: #FD4747">unionAll</span>(new UnionQuery&lt;${myConditionBeanClassName}&gt;() {
     *     public void query(${myConditionBeanClassName} unionCB) {
     *         unionCB.query().setXxx...
     *     }
     * });
     * </pre>
     * @param unionQuery The query of 'union all'. (NotNull)
     */
    public void unionAll(UnionQuery<${myConditionBeanClassName}> unionQuery) {
        final ${myConditionBeanClassName} cb = new ${myConditionBeanClassName}();
        cb.xsetupForUnion(this); xsyncUQ(cb); unionQuery.query(cb);
        final ${myConditionQueryClassName} cq = cb.query(); query().xsetUnionAllQuery(cq);
    }
#if ($database.isAvailableDatabaseDependency())
#if ($database.isDatabasePostgreSQL())

    // ===================================================================================
    //                                                                           Lock Wait
    //                                                                           =========
    public ConditionBean lockForUpdateNoWait()
    { if (xhelpIsSqlClausePostgreSql()) { xhelpGettingSqlClausePostgreSql().lockForUpdateNoWait(); } return this; }

    protected boolean xhelpIsSqlClausePostgreSql() {
        return getSqlClause() instanceof org.seasar.dbflute.cbean.sqlclause.SqlClausePostgreSql;
    }

    protected org.seasar.dbflute.cbean.sqlclause.SqlClausePostgreSql xhelpGettingSqlClausePostgreSql() {
        return (org.seasar.dbflute.cbean.sqlclause.SqlClausePostgreSql)getSqlClause();
    }
#end
#if ($database.isDatabaseOracle())

    // ===================================================================================
    //                                                                           Lock Wait
    //                                                                           =========
    public ConditionBean lockForUpdateNoWait()
    { if (xhelpIsSqlClauseOracle()) { xhelpGettingSqlClauseOracle().lockForUpdateNoWait(); } return this; }
    public ConditionBean lockForUpdateWait(int waitSec)
    { if (xhelpIsSqlClauseOracle()) { xhelpGettingSqlClauseOracle().lockForUpdateWait(waitSec); } return this; }

    protected boolean xhelpIsSqlClauseOracle() {
        return getSqlClause() instanceof org.seasar.dbflute.cbean.sqlclause.SqlClauseOracle;
    }

    protected org.seasar.dbflute.cbean.sqlclause.SqlClauseOracle xhelpGettingSqlClauseOracle() {
        return (org.seasar.dbflute.cbean.sqlclause.SqlClauseOracle)getSqlClause();
    }
#end
#if ($database.isDatabaseDB2())

    // [DBFlute-0.7.9]
    // ===================================================================================
    //                                                                     Isolation Level
    //                                                                     ===============
    // *Database Dependency
    public void lockWithRR() { if (xhelpIsSqlClauseDb2()) { xhelpGettingSqlClauseDb2().lockWithRR(); } }
    public void lockWithRS() { if (xhelpIsSqlClauseDb2()) { xhelpGettingSqlClauseDb2().lockWithRS(); } }
    public void lockWithCS() { if (xhelpIsSqlClauseDb2()) { xhelpGettingSqlClauseDb2().lockWithCS(); } }
    public void lockWithUR() { if (xhelpIsSqlClauseDb2()) { xhelpGettingSqlClauseDb2().lockWithUR(); } }

    protected boolean xhelpIsSqlClauseDb2() {
        return getSqlClause() instanceof org.seasar.dbflute.cbean.sqlclause.SqlClauseDb2;
    }

    protected org.seasar.dbflute.cbean.sqlclause.SqlClauseDb2 xhelpGettingSqlClauseDb2() {
        return (org.seasar.dbflute.cbean.sqlclause.SqlClauseDb2)getSqlClause();
    }
#end
#end

    // ===================================================================================
    //                                                                         SetupSelect
    //                                                                         ===========
#foreach ($fk in $table.foreignKeys)
#set ($tmpPropertyName = "${fk.foreignPropertyName}")
#set ($tmpPropertyNameInitCap = "${fk.foreignPropertyNameInitCap}")
#set ($nestSelectSetuppperClassName = "${fk.foreignTableNestSelectSetupperClassName}")
#set ($dynamicFixedConditionArgs = "")
#set ($dynamicFixedConditionVariables = "")
#if ($fk.hasDynamicFixedCondition())
#set ($dynamicFixedConditionArgs = "${fk.dynamicFixedConditionFinalArgs}")
#set ($dynamicFixedConditionVariables = "${fk.dynamicFixedConditionVariables}")
#end
    protected ${nestSelectSetuppperClassName} _nss${tmpPropertyNameInitCap};
    public ${nestSelectSetuppperClassName} getNss${tmpPropertyNameInitCap}() {
        if (_nss${tmpPropertyNameInitCap} == null) { _nss${tmpPropertyNameInitCap} = new ${nestSelectSetuppperClassName}(null); }
        return _nss${tmpPropertyNameInitCap};
    }
    /**
     * Set up relation columns to select clause. <br />
     * ${fk.foreignDispForJavaDoc}
     * <pre>
     * ${myConditionBeanClassName} cb = new ${myConditionBeanClassName}();
     * cb.<span style="color: #FD4747">setupSelect_${tmpPropertyNameInitCap}(${dynamicFixedConditionVariables})</span>; <span style="color: #3F7E5E">// ...().with[nested-relation]()</span>
     * cb.query().setFoo...(value);
     * ${myExtendedObjectClassName} ${myEntityVariableName} = ${myEntityVariableName}Bhv.selectEntityWithDeletedCheck(cb);
     * ... = ${myEntityVariableName}.<span style="color: #FD4747">get${tmpPropertyNameInitCap}()</span>; <span style="color: #3F7E5E">// you can get by using SetupSelect</span>
     * </pre>
#if ($fk.hasDynamicFixedCondition())
     * ${fk.dynamicFixedConditionArgsJavaDocString}
#end
     * @return The set-upper of nested relation. {setupSelect...().with[nested-relation]} (NotNull)
     */
    public ${nestSelectSetuppperClassName} setupSelect_${tmpPropertyNameInitCap}(${dynamicFixedConditionArgs}) {
#if ($fk.hasLocalColumnExceptPrimaryKey())
        if (hasSpecifiedColumn()) { // if reverse call
#foreach ($col in $fk.localColumnList)
#if (!$col.isPrimaryKey())
            specify().column${col.javaName}();
#end
#end
        }
#end
        doSetupSelect(new SsCall() { public ConditionQuery qf() { return query().query${tmpPropertyNameInitCap}(${dynamicFixedConditionVariables}); } });
        if (_nss${tmpPropertyNameInitCap} == null || !_nss${tmpPropertyNameInitCap}.hasConditionQuery())
        { _nss${tmpPropertyNameInitCap} = new ${nestSelectSetuppperClassName}(query().query${tmpPropertyNameInitCap}(${dynamicFixedConditionVariables})); }
        return _nss${tmpPropertyNameInitCap};
    }
#end
#foreach ($referrer in $table.referrerAsOneList)
#set ($tmpPropertyName = "${referrer.referrerPropertyNameAsOne}")
#set ($tmpPropertyNameInitCap = "${referrer.referrerPropertyNameInitCapAsOne}")
#set ($nestSelectSetuppperClassName = "${referrer.referrerTableNestSelectSetupperClassName}")

    protected ${nestSelectSetuppperClassName} _nss${tmpPropertyNameInitCap};
    public ${nestSelectSetuppperClassName} getNss${tmpPropertyNameInitCap}() {
        if (_nss${tmpPropertyNameInitCap} == null) { _nss${tmpPropertyNameInitCap} = new ${nestSelectSetuppperClassName}(null); }
        return _nss${tmpPropertyNameInitCap};
    }
    /**
     * Set up relation columns to select clause. <br />
     * ${referrer.referrerDispAsOneForJavaDoc}
     * <pre>
     * ${myConditionBeanClassName} cb = new ${myConditionBeanClassName}();
     * cb.<span style="color: #FD4747">setupSelect_${tmpPropertyNameInitCap}()</span>; <span style="color: #3F7E5E">// ...().with[nested-relation]()</span>
     * cb.query().setFoo...(value);
     * ${myExtendedObjectClassName} ${myEntityVariableName} = ${myEntityVariableName}Bhv.selectEntityWithDeletedCheck(cb);
     * ... = ${myEntityVariableName}.<span style="color: #FD4747">get${tmpPropertyNameInitCap}()</span>; <span style="color: #3F7E5E">// you can get by using SetupSelect</span>
     * </pre>
     * @return The set-upper of nested relation. {setupSelect...().with[nested-relation]} (NotNull)
     */
    public ${nestSelectSetuppperClassName} setupSelect_${tmpPropertyNameInitCap}() {
        doSetupSelect(new SsCall() { public ConditionQuery qf() { return query().query${tmpPropertyNameInitCap}(); } });
        if (_nss${tmpPropertyNameInitCap} == null || !_nss${tmpPropertyNameInitCap}.hasConditionQuery()) { _nss${tmpPropertyNameInitCap} = new ${nestSelectSetuppperClassName}(query().query${tmpPropertyNameInitCap}()); }
        return _nss${tmpPropertyNameInitCap};
    }
#end

    // [DBFlute-0.7.4]
    // ===================================================================================
    //                                                                             Specify
    //                                                                             =======
    protected HpSpecification _specification;

    /**
     * Prepare for SpecifyColumn, (Specify)DerivedReferrer. <br />
     * This method should be called after SetupSelect.
     * <pre>
     * cb.setupSelect_MemberStatus(); <span style="color: #3F7E5E">// should be called before specify()</span>
     * cb.specify().columnMemberName();
     * cb.specify().specifyMemberStatus().columnMemberStatusName();
     * cb.specify().derivedPurchaseList().max(new SubQuery&lt;PurchaseCB&gt;() {
     *     public void query(PurchaseCB subCB) {
     *         subCB.specify().columnPurchaseDatetime();
     *         subCB.query().set...
     *     }
     * }, aliasName);
     * </pre>
     * @return The instance of specification. (NotNull)
     */
    public HpSpecification specify() {
        assertSpecifyPurpose();
        if (_specification == null) { _specification = new HpSpecification(this
            , new HpSpQyCall<${myConditionQueryClassName}>() {
                public boolean has() { return true; }
                public ${myConditionQueryClassName} qy() { return getConditionQuery(); }
            }
            , _purpose, getDBMetaProvider()); }
        return _specification;
    }

    protected boolean hasSpecifiedColumn() {
        return _specification != null && _specification.isAlreadySpecifiedRequiredColumn();
    }

    protected HpAbstractSpecification<? extends ConditionQuery> localSp() {
        return specify();
    }

    public static class HpSpecification extends HpAbstractSpecification<${myConditionQueryClassName}> {
#foreach ($fk in $table.foreignKeys)
#set ($tmpPropertyName = "${fk.foreignPropertyName}")
#set ($tmpCBClassName = "${fk.foreignTableExtendedConditionBeanClassName}")
        protected ${tmpCBClassName}.HpSpecification _${tmpPropertyName};
#end
#foreach ($referrer in $table.referrerAsOneList)
#set ($tmpPropertyName = "${referrer.referrerPropertyNameAsOne}")
#set ($tmpCBClassName = "${referrer.referrerTableExtendedConditionBeanClassName}")
        protected ${tmpCBClassName}.HpSpecification _${tmpPropertyName};
#end
        public HpSpecification(ConditionBean baseCB, HpSpQyCall<${myConditionQueryClassName}> qyCall
                             , HpCBPurpose purpose, DBMetaProvider dbmetaProvider)
        { super(baseCB, qyCall, purpose, dbmetaProvider); }
#foreach ($col in $table.columns)
        /** ${col.aliasExpression}${col.name}: {${col.columnDefinitionLineDisp}} */
        public void column${col.javaName}() { doColumn("${col.name}"); }
#end
        @Override
        protected void doSpecifyRequiredColumn() {
#foreach ($col in $table.primaryKey)
            column${col.javaName}(); // PK
#end
#foreach ($fk in $table.foreignKeys)
#set ($tmpPropertyName = "${fk.foreignPropertyName}")
#set ($tmpPropertyNameInitCap = "${fk.foreignPropertyNameInitCap}")
#if ($fk.hasLocalColumnExceptPrimaryKey())
            if (qyCall().qy().hasConditionQuery${tmpPropertyNameInitCap}()
                    || qyCall().qy().xgetReferrerQuery() instanceof ${fk.foreignTableExtendedConditionQueryClassName}) {
#foreach ($col in $fk.localColumnList)
#if (!$col.isPrimaryKey())
                column${col.javaName}(); // FK or one-to-one referrer
#end
#end
            }
#end
#end
        }
        @Override
        protected String getTableDbName() { return "$table.name"; }
#foreach ($fk in $table.foreignKeys)
#set ($tmpPropertyName = "${fk.foreignPropertyName}")
#set ($tmpPropertyNameInitCap = "${fk.foreignPropertyNameInitCap}")
#set ($tmpCBClassName = "${fk.foreignTableExtendedConditionBeanClassName}")
#set ($tmpCQClassName = "${fk.foreignTableExtendedConditionQueryClassName}")
        /**
         * Prepare to specify functions about relation table. <br />
         * ${fk.foreignDispForJavaDocNest}
         * @return The instance for specification for relation table to specify. (NotNull)
         */
        public ${tmpCBClassName}.HpSpecification specify${tmpPropertyNameInitCap}() {
            assertRelation("${tmpPropertyName}");
            if (_${tmpPropertyName} == null) {
                _${tmpPropertyName} = new ${tmpCBClassName}.HpSpecification(_baseCB, new HpSpQyCall<${tmpCQClassName}>() {
                    public boolean has() { return _qyCall.has() && _qyCall.qy().hasConditionQuery${tmpPropertyNameInitCap}(); }
#if ($fk.hasDynamicFixedCondition())
                    public ${tmpCQClassName} qy() { return _qyCall.qy().getConditionQuery${tmpPropertyNameInitCap}(); } }
#else
                    public ${tmpCQClassName} qy() { return _qyCall.qy().query${tmpPropertyNameInitCap}(); } }
#end
                    , _purpose, _dbmetaProvider);
                if (xhasSyncQyCall()) { // inherits it
                    _${tmpPropertyName}.xsetSyncQyCall(new HpSpQyCall<${tmpCQClassName}>() {
                        public boolean has() { return xsyncQyCall().has() && xsyncQyCall().qy().hasConditionQuery${tmpPropertyNameInitCap}(); }
#if ($fk.hasDynamicFixedCondition())
                        public ${tmpCQClassName} qy() { return xsyncQyCall().qy().getConditionQuery${tmpPropertyNameInitCap}(); }
#else
                        public ${tmpCQClassName} qy() { return xsyncQyCall().qy().query${tmpPropertyNameInitCap}(); }
#end
                    });
                }
            }
            return _${tmpPropertyName};
        }
#end
#foreach ($referrer in $table.referrerAsOneList)
#set ($tmpPropertyName = "${referrer.referrerPropertyNameAsOne}")
#set ($tmpPropertyNameInitCap = "${referrer.referrerPropertyNameInitCapAsOne}")
#set ($tmpCBClassName = "${referrer.referrerTableExtendedConditionBeanClassName}")
#set ($tmpCQClassName = "${referrer.referrerTableExtendedConditionQueryClassName}")
        /**
         * Prepare to specify functions about relation table. <br />
         * ${referrer.referrerDispAsOneForJavaDocNest}
         * @return The instance for specification for relation table to specify. (NotNull)
         */
        public ${tmpCBClassName}.HpSpecification specify${tmpPropertyNameInitCap}() {
            assertRelation("${tmpPropertyName}");
            if (_${tmpPropertyName} == null) {
                _${tmpPropertyName} = new ${tmpCBClassName}.HpSpecification(_baseCB, new HpSpQyCall<${tmpCQClassName}>() {
                    public boolean has() { return _qyCall.has() && _qyCall.qy().hasConditionQuery${tmpPropertyNameInitCap}(); }
                    public ${tmpCQClassName} qy() { return _qyCall.qy().query${tmpPropertyNameInitCap}(); } }
                    , _purpose, _dbmetaProvider);
                if (xhasSyncQyCall()) { // inherits it
                    _${tmpPropertyName}.xsetSyncQyCall(new HpSpQyCall<${tmpCQClassName}>() {
                        public boolean has() { return xsyncQyCall().has() && xsyncQyCall().qy().hasConditionQuery${tmpPropertyNameInitCap}(); }
                        public ${tmpCQClassName} qy() { return xsyncQyCall().qy().query${tmpPropertyNameInitCap}(); }
                    });
                }
            }
            return _${tmpPropertyName};
        }
#end
#foreach ($referrer in $table.singleKeyStringOrIntegerReferrers)
#if (!${referrer.isOneToOne()})
#set ($tmpPropertyName = "${referrer.referrerPropertyName}")
#set ($tmpPropertyNameInitCap = "${referrer.referrerPropertyNameInitCap}")
#set ($tmpCBClassName = "${referrer.referrerTableExtendedConditionBeanClassName}")
#set ($tmpLocalCQClassName = "${referrer.foreignTableExtendedConditionQueryClassName}")
        /**
         * Prepare for '(Specify)DerivedReferrer'. <br />
         * ${referrer.referrerDispForJavaDocNest}
         * <pre>
         * cb.specify().<span style="color: #FD4747">derived${tmpPropertyNameInitCap}()</span>.max(new SubQuery&lt;${tmpCBClassName}&gt;() {
         *     public void query(${tmpCBClassName} subCB) {
         *         subCB.specify().<span style="color: #FD4747">columnFoo...</span> <span style="color: #3F7E5E">// derived column for function</span>
         *         subCB.query().setBar... <span style="color: #3F7E5E">// referrer condition</span>
         *     }
         * }, aliasName);
         * </pre>
         * @return The object to set up a function for referrer table. (NotNull)
         */
        public HpSDRFunction<${tmpCBClassName}, ${tmpLocalCQClassName}> derived${tmpPropertyNameInitCap}() {
            assertDerived("${tmpPropertyName}"); if (xhasSyncQyCall()) { xsyncQyCall().qy(); } // for sync (for example, this in ColumnQuery)
            return new HpSDRFunction<${tmpCBClassName}, ${tmpLocalCQClassName}>(_baseCB, _qyCall.qy(), new HpSDRSetupper<${tmpCBClassName}, ${tmpLocalCQClassName}>() {
                public void setup(String function, SubQuery<${tmpCBClassName}> subQuery, ${tmpLocalCQClassName} cq, String aliasName, DerivedReferrerOption option) {
                    cq.xsderive${referrer.referrerPropertyNameInitCap}(function, subQuery, aliasName, option); } }, _dbmetaProvider);
        }
#end
#end
    }

    // [DBFlute-0.9.5.3]
    // ===================================================================================
    //                                                                         ColumnQuery
    //                                                                         ===========
    /**
     * Set up column-query. {column1 = column2}
     * <pre>
     * <span style="color: #3F7E5E">// where FOO &lt; BAR</span>
     * cb.<span style="color: #FD4747">columnQuery</span>(new SpecifyQuery&lt;${myConditionBeanClassName}&gt;() {
     *     public void query(${myConditionBeanClassName} cb) {
     *         cb.specify().<span style="color: #FD4747">columnFoo()</span>; <span style="color: #3F7E5E">// left column</span>
     *     }
     * }).lessThan(new SpecifyQuery&lt;${myConditionBeanClassName}&gt;() {
     *     public void query(${myConditionBeanClassName} cb) {
     *         cb.specify().<span style="color: #FD4747">columnBar()</span>; <span style="color: #3F7E5E">// right column</span>
     *     }
     * }); <span style="color: #3F7E5E">// you can calculate for right column like '}).plus(3);'</span>
     * </pre>
     * @param leftSpecifyQuery The specify-query for left column. (NotNull)
     * @return The object for setting up operand and right column. (NotNull)
     */
    public HpColQyOperand<${myConditionBeanClassName}> columnQuery(final SpecifyQuery<${myConditionBeanClassName}> leftSpecifyQuery) {
        return new HpColQyOperand<${myConditionBeanClassName}>(new HpColQyHandler<${myConditionBeanClassName}>() {
            public HpCalculator handle(SpecifyQuery<${myConditionBeanClassName}> rightSp, String operand) {
                return xcolqy(xcreateColumnQueryCB(), xcreateColumnQueryCB(), leftSpecifyQuery, rightSp, operand);
            }
        });
    }

    protected ${myConditionBeanClassName} xcreateColumnQueryCB() {
        ${myConditionBeanClassName} cb = new ${myConditionBeanClassName}();
        cb.xsetupForColumnQuery((${myConditionBeanClassName})this);
        return cb;
    }

    // [DBFlute-0.9.6.3]
    // ===================================================================================
    //                                                                        OrScopeQuery
    //                                                                        ============
    /**
     * Set up the query for or-scope. <br />
     * (Same-column-and-same-condition-key conditions are allowed in or-scope)
     * <pre>
     * <span style="color: #3F7E5E">// where (FOO = '...' or BAR = '...')</span>
     * cb.<span style="color: #FD4747">orScopeQuery</span>(new OrQuery&lt;${myConditionBeanClassName}&gt;() {
     *     public void query(${myConditionBeanClassName} orCB) {
     *         orCB.query().setFOO_Equal...
     *         orCB.query().setBAR_Equal...
     *     }
     * });
     * </pre>
     * @param orQuery The query for or-condition. (NotNull)
     */
    public void orScopeQuery(OrQuery<${myConditionBeanClassName}> orQuery) {
        xorSQ((${myConditionBeanClassName})this, orQuery);
    }

    /**
     * Set up the and-part of or-scope. <br />
     * (However nested or-scope query and as-or-split of like-search in and-part are unsupported)
     * <pre>
     * <span style="color: #3F7E5E">// where (FOO = '...' or (BAR = '...' and QUX = '...'))</span>
     * cb.<span style="color: #FD4747">orScopeQuery</span>(new OrQuery&lt;${myConditionBeanClassName}&gt;() {
     *     public void query(${myConditionBeanClassName} orCB) {
     *         orCB.query().setFOO_Equal...
     *         orCB.<span style="color: #FD4747">orScopeQueryAndPart</span>(new AndQuery&lt;${myConditionBeanClassName}&gt;() {
     *             public void query(${myConditionBeanClassName} andCB) {
     *                 andCB.query().setBar_...
     *                 andCB.query().setQux_...
     *             }
     *         });
     *     }
     * });
     * </pre>
     * @param andQuery The query for and-condition. (NotNull)
     */
    public void orScopeQueryAndPart(AndQuery<${myConditionBeanClassName}> andQuery) {
        xorSQAP((${myConditionBeanClassName})this, andQuery);
    }

    // ===================================================================================
    //                                                                          DisplaySQL
    //                                                                          ==========
    @Override
    protected SqlAnalyzerFactory getSqlAnalyzerFactory()
    { return new ${glImplementedInvokerAssistant}().assistSqlAnalyzerFactory(); }
    @Override
    protected String getLogDateFormat() { return ${glDBFluteConfig}.getInstance().getLogDateFormat(); }
    @Override
    protected String getLogTimestampFormat() { return ${glDBFluteConfig}.getInstance().getLogTimestampFormat(); }

    // ===================================================================================
    //                                                          Basic Status Determination
    //                                                          ==========================
    public boolean hasUnionQueryOrUnionAllQuery() {
        return query().hasUnionQueryOrUnionAllQuery();
    }

    // [DBFlute-0.7.4]
    // ===================================================================================
    //                                                                        Purpose Type
    //                                                                        ============
    public void xsetupForColumnQuery(final ${myConditionBeanClassName} mainCB) {
        xinheritSubQueryInfo(mainCB.localCQ());
        xchangePurposeSqlClause(HpCBPurpose.COLUMN_QUERY);

        // inherits a parent query to synchronize real name
        // (and also for suppressing query check) 
        specify().xsetSyncQyCall(new HpSpQyCall<${myConditionQueryClassName}>() {
            public boolean has() { return true; }
            public ${myConditionQueryClassName} qy() { return mainCB.query(); }
        });
    }

    public void xsetupForVaryingUpdate() {
        xchangePurposeSqlClause(HpCBPurpose.VARYING_UPDATE);

        // for suppressing query check
        final ${myConditionBeanClassName} nonCheckCB = new ${myConditionBeanClassName}();
        specify().xsetSyncQyCall(new HpSpQyCall<${myConditionQueryClassName}>() {
            public boolean has() { return true; }
            public ${myConditionQueryClassName} qy() { return nonCheckCB.query(); }
        });
    }

    // ===================================================================================
    //                                                                            Internal
    //                                                                            ========
    // very internal (for suppressing warn about 'Not Use Import')
    protected String getConditionBeanClassNameInternally() { return ${myConditionBeanClassName}.class.getName(); }
    protected String getConditionQueryClassNameInternally() { return ${myConditionQueryClassName}.class.getName(); }
    protected String getSubQueryClassNameInternally() { return SubQuery.class.getName(); }
    protected String getConditionOptionClassNameInternally() { return ConditionOption.class.getName(); }
}
