/*
 * blancoDb
 * Copyright (C) 2005 Tosiki Iga
 * 
 * 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;

import java.sql.SQLException;

import blanco.db.conf.BlancoDbSetting;
import blanco.db.properties.GenerationProperties;
import blanco.db.util.BlancoDbObjectStorage;
import blanco.ig.expander.Value;
import blanco.ig.expander.implementor.Implementor;
import blanco.ig.expander.implementor.Receiver;
import blanco.ig.expander.method.MethodExpander;

/**
 * @author tosiki iga
 */
public class PrepareStatementCursorMethod extends MethodExpander {
    private BlancoDbSetting _setting = null;

    private Value queryArgument = new Value(String.class, "query");

    private Value resultSetTypeArgument = new Value(int.class, "resultSetType");

    private Value resultSetConcurrencyArgument = new Value(int.class,
            "resultSetConcurrency");

    private GenerationProperties _properties = null;

    public PrepareStatementCursorMethod() {
        super("prepareStatement");

        BlancoDbObjectStorage storage = BlancoDbObjectStorage.getInstance();
        _setting = storage.getSetting();
        _properties = storage.getGenerationProperties();
    }

    public void setupSignature() {
        // 引数を追加してみる。
        addArgument(queryArgument);
        addArgument(resultSetTypeArgument);
        addArgument(resultSetConcurrencyArgument);
        addException(SQLException.class);

        if (_properties.isGenerateJavaDoc()) {
            getJavaDoc().addLine("クエリのプリコンパイルを実施します。<br>");
            getJavaDoc().addLine("内部的には Connection.prepareStatement を呼び出します。");
            getJavaDoc().addParameter("query", "利用したいクエリ(SQL文)");
            getJavaDoc()
                    .addParameter(
                            "resultSetType",
                            "結果セットの型。ResultSet.TYPE_FORWARD_ONLY、ResultSet.TYPE_SCROLL_INSENSITIVE、ResultSet.TYPE_SCROLL_SENSITIVEのいずれかを指定。");
            getJavaDoc()
                    .addParameter("resultSetConcurrency",
                            "並行処理の種類。ResultSet.CONCUR_READ_ONLY、ResultSet.CONCUR_UPDATABLEのいずれかを指定。");
        }
    }

    public void implement() {
        Receiver r = null;
        Implementor i = new Implementor(getData());
        i.addStatement("close()");
        i
                .addStatement("_statement = _connection.prepareStatement(getQuery(), resultSetType, resultSetConcurrency)");
    }

}