001/*
002 * Copyright (c) 2009 The openGion Project.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
013 * either express or implied. See the License for the specific language
014 * governing permissions and limitations under the License.
015 */
016package org.opengion.plugin.query;
017
018import org.opengion.hayabusa.common.HybsSystem;
019import org.opengion.hayabusa.common.HybsSystemException;
020import org.opengion.hayabusa.db.AbstractQuery;
021import org.opengion.fukurou.util.ErrorMessage;
022import org.opengion.fukurou.util.StringUtil;
023import org.opengion.fukurou.util.Closer;
024
025import java.sql.CallableStatement;
026import java.sql.SQLException;
027import java.sql.Types;
028
029/**
030 * バッチ系標準のPL/SQL をコールする Query クラスです。
031 *
032 * java.sql.CallableStatement を用いて、データベース検索処理を行います。
033 * 引数は、従来のPL/SQLの実行が可能なように、第一引数はエラーコード、第二引数は、
034 * エラーメッセージを返してきます。第三引数以降は、自由に指定できます。
035 * 内部変数の受け渡しのデフォルト実装は、AbstractQuery クラスを継承している
036 * ため,ここでは、execute() メソッドを実装しています。
037 *
038 * @og.formSample
039 * 例:
040 *     第一引数、第二引数は、通常のPL/SQLと同じ、結果(STATUS)と
041 *     内容(ERR_CODE)を返します。
042 *     それ以降の引数については、入力(IN)のみですが、自由に設定できます。
043 *     引数に変数を使用する場合は、? 記号を当てはめます。
044 *     第一引数、第二引数は、予約済みですが、それ以降は、好きな位置に割り当てられます。
045 *     names 属性の順番に、値だけがセットされていきます。
046 *     下記の例は、変数の引数は、使用していません。
047 *
048 * <og:query
049 *     command="NEW"
050 *     queryType="JDBCCallable"
051 *     displayMsg="" >
052 *         { call GEP00002.GEP00002( ?,?,'{@GUI.KEY}','{@USER.ID}' ) }
053 * </og:query>
054 *
055 *    CREATE OR REPLACE PACKAGE GEP00002 AS
056 *        PROCEDURE GEP00002(
057 *            P_STATUS    OUT    NUMBER,
058 *            P_ERR_CODE  OUT    VARCHAR2,
059 *            P_MIDDB     IN     VARCHAR2,
060 *            P_USRUPD    IN     VARCHAR2  );
061 *    END;
062 *
063 * @og.group データ表示
064 * @og.group データ編集
065 *
066 * @version  4.0
067 * @author   Kazuhiko Hasegawa
068 * @since    JDK5.0,
069 */
070public class Query_JDBCCallable extends AbstractQuery {
071        //* このプログラムのVERSION文字列を設定します。   {@value} */
072        private static final String VERSION = "4.0.0.0 (2005/08/31)" ;
073
074        /**
075         * クエリーを実行します。
076         * セットされているステートメント文字列とそのタイプが合っていない場合は,
077         * エラーになります。
078         * 実行結果は、DBTableModel にセットされます。
079         *
080         */
081        @Override
082        public void execute() {
083                execute( null );
084        }
085
086        /**
087         * 引数配列付のクエリーを実行します。
088         * 処理自体は, #execute() と同様に、各サブクラスの実装に依存します。
089         * これは、PreparedQuery で使用する引数を配列でセットするものです。
090         * select * from emp where deptno = ? and job = ? などの PreparedQuery の
091         * ? 部分の引数を
092         * 順番にセットしていきます。
093         *
094         * @og.rev 3.1.1.0 (2003/03/28) 同期メソッド(synchronized付き)を非同期に変更する。
095         * @og.rev 3.3.3.1 (2003/07/18) DB登録時の後ろスペースを削除する。
096         * @og.rev 3.5.6.0 (2004/06/18) nullに対する無駄な比較を削除します。
097         * @og.rev 3.8.0.8 (2005/10/03) エラーメッセージの出力順をメッセージ+Queryに変更します。
098         *
099         * @param   args オブジェクトの引数配列
100         */
101        @Override
102        public void execute( final String[] args ) {
103                CallableStatement callStmt = null ;
104                try {
105                        callStmt  = getConnection().prepareCall( getStatement() );
106                        callStmt.setQueryTimeout( DB_MAX_QUERY_TIMEOUT );
107
108                        callStmt.registerOutParameter(1, Types.INTEGER);
109                        callStmt.registerOutParameter(2, Types.VARCHAR);
110                        if( args != null ) {
111                                for( int i=0; i<args.length; i++ ) {
112                                        callStmt.setObject( i+3,StringUtil.rTrim( args[i] ) );
113                                }
114                        }
115                        callStmt.execute();
116
117                        int rtnCode = callStmt.getInt(1);
118                        setErrorCode( rtnCode );
119
120                        if( rtnCode > ErrorMessage.OK ) {               // 正常以外の場合
121                                String ermsg = callStmt.getString(2);
122                                ErrorMessage errMessage = new ErrorMessage( "Query_JDBCCallable Error!!" );
123                                errMessage.addMessage( ermsg );
124                                setErrorMessage( errMessage );
125                        }
126                }
127                catch ( SQLException ex ) {
128                        setErrorCode( ErrorMessage.EXCEPTION );
129                        String errMsg = ex.getMessage() + ":" + ex.getSQLState() + HybsSystem.CR
130                                                + getStatement() + HybsSystem.CR;
131                        rollback();
132                        realClose();
133                        throw new HybsSystemException( errMsg,ex );             // 3.5.5.4 (2004/04/15) 引数の並び順変更
134                }
135                finally {
136                        Closer.stmtClose( callStmt );
137                }
138        }
139}