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.HybsSystemException;
019import org.opengion.hayabusa.db.AbstractQuery;
020import org.opengion.hayabusa.db.DBErrMsg;
021import org.opengion.fukurou.util.ErrorMessage;
022import org.opengion.fukurou.util.StringUtil;
023
024import java.util.Map;
025import java.sql.CallableStatement;
026import java.sql.Connection;
027import java.sql.SQLException;
028import java.sql.Types;
029import java.sql.Array;                                                          // 6.0.0.0 (2014/04/11) Oracle11g(11.2.0.3のドライバ) 対応。oracle.sql.ARRAY の置き換え
030
031/**
032 * Callableのエラー配列対応版です。バッチ系標準のPL/SQL をコールする Query クラスです。
033 *
034 * java.sql.CallableStatement を用いて、データベース検索処理を行います。
035 * 引数は、従来のPL/SQLの実行が可能なように、第一引数はエラーコード、第二引数は、
036 * エラーメッセージを返してきます。第三引数以降は、自由に指定できます。
037 * 内部変数の受け渡しのデフォルト実装は、AbstractQuery クラスを継承している
038 * ため,ここでは、execute() メソッドを実装しています。
039 *
040 * @og.formSample
041 * 例:
042 *     第一引数、第二引数は、通常のPL/SQLと同じ、結果(STATUS)と
043 *     内容(ERR_CODE)を返します。
044 *     それ以降の引数については、入力(IN)のみですが、自由に設定できます。
045 *     引数に変数を使用する場合は、? 記号を当てはめます。
046 *     第一引数、第二引数は、予約済みですが、それ以降は、好きな位置に割り当てられます。
047 *     names 属性の順番に、値だけがセットされていきます。
048 *     下記の例は、変数の引数は、使用していません。
049 *
050 * <og:query
051 *     command="NEW"
052 *     queryType="JDBCArrayCallable"
053 *     displayMsg="" >
054 *         { call GEP00002.GEP00002( ?,?,'{@GUI.KEY}','{@USER.ID}' ) }
055 * </og:query>
056 *
057 *    CREATE OR REPLACE PACKAGE GEP00002 AS
058 *        PROCEDURE GEP00002(
059 *            P_STATUS    OUT    NUMBER,
060 *            P_ERR_CODE  OUT    ERR_MSG_ARRAY,
061 *            P_MIDDB     IN     VARCHAR2,
062 *            P_USRUPD    IN     VARCHAR2  );
063 *    END;
064 *
065 * @og.group データ表示
066 * @og.group データ編集
067 *
068 * @version  4.0
069 * @author   高橋正和
070 * @since    JDK5.0,
071 */
072public class Query_JDBCArrayCallable extends AbstractQuery {
073        /** このプログラムのVERSION文字列を設定します。   {@value} */
074        private static final String VERSION = "6.9.3.0 (2018/03/26)" ;
075
076        /**
077         * デフォルトコンストラクター
078         *
079         * @og.rev 6.4.2.0 (2016/01/29) PMD refactoring. Each class should declare at least one constructor.
080         */
081        public Query_JDBCArrayCallable() { super(); }           // これも、自動的に呼ばれるが、空のメソッドを作成すると警告されるので、明示的にしておきます。
082
083        /**
084         * 引数配列付のクエリーを実行します。
085         * 処理自体は, #execute() と同様に、各サブクラスの実装に依存します。
086         * これは、PreparedQuery で使用する引数を配列でセットするものです。
087         * select * from emp where deptno = ? and job = ? などの PreparedQuery の
088         * ? 部分の引数を
089         * 順番にセットしていきます。
090         *
091         * @og.rev 6.0.0.0 (2014/04/11) Oracle11g(11.2.0.3のドライバ)対応
092         * @og.rev 6.3.6.1 (2015/08/28) close(),realClose() 廃止。Queryはキャッシュしません。
093         * @og.rev 6.4.2.1 (2016/02/05) try-with-resources 文で記述。
094         * @og.rev 6.9.3.0 (2018/03/26) DB_FETCH_SIZE追加。
095         *
096         * @param   args オブジェクトの引数配列(可変長引数)
097         */
098        @Override
099        public void execute( final String... args ) {                   // 6.1.1.0 (2015/01/17) refactoring
100                        final Connection conn = getConnection();
101                // 6.4.2.1 (2016/02/05) try-with-resources 文
102                try( final CallableStatement callStmt = conn.prepareCall( getStatement() ) ) {
103                        callStmt.setQueryTimeout( DB_MAX_QUERY_TIMEOUT );
104                        callStmt.setFetchSize( DB_FETCH_SIZE );                 // 6.9.3.0 (2018/03/26)
105
106                        final Map<String,Class<?>> map = conn.getTypeMap();
107                        map.put( ERR_MSG,DBErrMsg.class );
108
109                        // 6.0.0.0 (2014/04/11) Oracle11g(11.2.0.3のドライバ)対応
110                        callStmt.registerOutParameter(1, Types.INTEGER);
111                        callStmt.registerOutParameter(2, Types.ARRAY,ERR_MSG_ARRAY);
112                        // 6.1.1.0 (2015/01/17) refactoring. 可変引数にしたため、null は来ない。
113                                for( int i=0; i<args.length; i++ ) {
114                                        callStmt.setObject( i+3,StringUtil.rTrim( args[i] ) );
115                                }
116                        callStmt.execute();
117
118                        final int rtnCode = callStmt.getInt(1);
119                        setErrorCode( rtnCode );
120
121                        if( rtnCode > ErrorMessage.OK ) {               // 正常以外の場合
122                                // 6.0.0.0 (2014/04/11) Oracle11g(11.2.0.3のドライバ) 対応
123                                final Array rtn3 = callStmt.getArray(2);
124                                final Object[] rtnval3 = (Object[])rtn3.getArray();
125                                final ErrorMessage errMessage = new ErrorMessage( "Query_JDBCArrayCallable Error!!" );
126                                for( int i=0; i<rtnval3.length; i++ ) {
127                                        final DBErrMsg er = (DBErrMsg)rtnval3[i];
128                                        if( er == null ) { break; }
129                                        errMessage.addMessage( er.getErrMsg() );
130                                }
131                                setErrorMessage( errMessage );
132                        }
133                }
134                catch( final SQLException ex ) {                // catch は、close() されてから呼ばれます。
135                        setErrorCode( ErrorMessage.EXCEPTION );
136                        final String errMsg = ex.getMessage() + ":" + ex.getSQLState() + CR
137                                                + getStatement() + CR;
138                        throw new HybsSystemException( errMsg,ex );
139                }
140        }
141}