/*
 * blanco Framework
 * Copyright (C) 2004-2006 IGA Tosiki
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 */
package blanco.db.expander.query.iterator;

import blanco.cg.BlancoCgObjectFactory;
import blanco.cg.valueobject.BlancoCgSourceFile;
import blanco.commons.util.BlancoNameAdjuster;
import blanco.db.common.expander.BlancoDbAbstractClass;
import blanco.db.common.valueobject.BlancoDbSetting;
import blanco.db.common.valueobject.BlancoDbSqlInfoStructure;
import blanco.db.expander.query.GetQueryMethodPhp;
import blanco.db.expander.query.GetStatementMethodPhp;
import blanco.db.expander.query.PrepareStatementMethod2Php;
import blanco.db.expander.query.PrepareStatementMethodPhp;
import blanco.db.expander.query.QueryConstructorPhp;
import blanco.db.expander.query.field.ConnectionFieldPhp;
import blanco.db.expander.query.field.CurrentRowFieldPhp;
import blanco.db.expander.query.field.StatementFieldPhp;

/**
 * 個別のクラスを展開するためのクラス。
 */
public class QueryIteratorClassPhp extends BlancoDbAbstractClass {

    public QueryIteratorClassPhp(final BlancoDbSetting argDbSetting,
            final BlancoDbSqlInfoStructure argSqlInfo,
            final BlancoCgObjectFactory argCgFactory) {
        super(argDbSetting, argSqlInfo, argCgFactory);
    }

    public BlancoCgSourceFile expand() {
        final String className = BlancoNameAdjuster.toClassName(fSqlInfo
                .getName())
                + "Iterator";

        fCgSourceFile = fCgFactory.createSourceFile(fDbSetting.getBasePackage()
                + ".query", "This code is generated by blanco Framework.");
        fCgClass = fCgFactory.createClass(className, className + "クラス");
        fCgSourceFile.getClassList().add(fCgClass);

        fCgSourceFile.getImportList().add("pdo.PDO");

        fCgClass.getLangDoc().getDescriptionList().add(
                "QueryIteratorに属するクラスです。");
        fCgClass.getLangDoc().getDescriptionList().add(
                "検索型SQL文をラッピングして各種アクセサを提供します。");
        if (fSqlInfo.getSingle()) {
            fCgClass.getLangDoc().getDescriptionList().add(
                    "シングル属性: 有効 (期待する処理件数は1件)");
        }

        new ConnectionFieldPhp(fDbSetting, fSqlInfo, fCgFactory, fCgSourceFile,
                fCgClass).expand();
        new StatementFieldPhp(fDbSetting, fSqlInfo, fCgFactory, fCgSourceFile,
                fCgClass).expand();
        new CurrentRowFieldPhp(fDbSetting, fSqlInfo, fCgFactory, fCgSourceFile,
                fCgClass).expand();

        new QueryConstructorPhp(fDbSetting, fSqlInfo, fCgFactory,
                fCgSourceFile, fCgClass).expand();

        new GetQueryMethodPhp(fDbSetting, fSqlInfo, fCgFactory, fCgSourceFile,
                fCgClass).expand();

        // Closeは先に存在しておく必要があります。
        new CloseMethodPhp(fDbSetting, fSqlInfo, fCgFactory, fCgSourceFile,
                fCgClass).expand();

        new PrepareStatementMethod2Php(fDbSetting, fSqlInfo, fCgFactory,
                fCgSourceFile, fCgClass).expand();
        new PrepareStatementMethodPhp(fDbSetting, fSqlInfo, fCgFactory,
                fCgSourceFile, fCgClass).expand();

        // パラメータがある場合にのみ bindメソッドを生成します。
        if (fSqlInfo.getInParameterList().size() > 0) {
            new SetInputParameterMethodPhp(fDbSetting, fSqlInfo, fCgFactory,
                    fCgSourceFile, fCgClass).expand();
        }

        new ExecuteQueryMethodPhp(fDbSetting, fSqlInfo, fCgFactory,
                fCgSourceFile, fCgClass).expand();

        new NextMethodPhp(fDbSetting, fSqlInfo, fCgFactory, fCgSourceFile,
                fCgClass).expand();

        new GetRowMethodPhp(fDbSetting, fSqlInfo, fCgFactory, fCgSourceFile,
                fCgClass).expand();

        if (fSqlInfo.getSingle() == false) {
            new GetListMethodPhp(fDbSetting, fSqlInfo, fCgFactory,
                    fCgSourceFile, fCgClass).expand();
        }

        if (fSqlInfo.getSingle()) {
            new GetSingleRowMethodPhp(fDbSetting, fSqlInfo, fCgFactory,
                    fCgSourceFile, fCgClass).expand();
        }

        new GetStatementMethodPhp(fDbSetting, fSqlInfo, fCgFactory,
                fCgSourceFile, fCgClass).expand();

        return fCgSourceFile;
    }
}