/*
 * blancoDb
 * Copyright (C) 2004-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.iterator;

import java.sql.SQLException;

import blanco.db.definition.QueryIterator;
import blanco.db.properties.GenerationProperties;
import blanco.db.util.BlancoDbObjectStorage;
import blanco.ig.expander.Type;
import blanco.ig.expander.Value;
import blanco.ig.expander.implementor.Implementor;
import blanco.ig.expander.method.MethodExpander;

/**
 * カーソル属性がtrueの場合に、このメソッドは作成されます。
 * 
 * @author Tosiki Iga
 */
public class RelativeMethod extends MethodExpander {
    private GenerationProperties _properties = null;

    private QueryIterator _iterator = null;

    public RelativeMethod(QueryIterator iterator) {
        super("relative");

        _iterator = iterator;
        _properties = BlancoDbObjectStorage.getInstance()
                .getGenerationProperties();
    }

    public void setupSignature() {
        setReturnType(new Type(boolean.class));
        addException(SQLException.class);

        addArgument(new Value(int.class, "rows"));

        if (_properties.isGenerateJavaDoc()) {
            getJavaDoc().addLine("カーソルを結果セットの相対行数だけ移動します。");
            getJavaDoc().addLine("relative(1)はnext()を呼び出すのと同じです。");
            getJavaDoc().addLine("relative(-1)はprevious()を呼び出すのと同じです。");
            getJavaDoc().addReturn("新しい現在の行が有効な場合はtrue、それ以上の行がない場合はfalse");
        }
    }

    public void implement() {
        Implementor i = new Implementor(getData());

        // resultSetが未確保であるばあい、強制的にexecuteQueryを呼び出します。
        i.addLine("if (_resultSet == null) {");
        i.addStatement("executeQuery()");
        i.addLine("}");

        i.addStatement("return _resultSet.relative(rows)");
    }
}