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.db.AbstractQuery;
019import org.opengion.hayabusa.db.DBErrMsg;
020import org.opengion.hayabusa.common.HybsSystemException;
021import org.opengion.fukurou.util.ErrorMessage;
022import org.opengion.fukurou.util.StringUtil;
023
024import java.sql.Types;
025import java.sql.Connection;
026import java.sql.CallableStatement;
027import java.sql.SQLException;
028import java.sql.Array;                                                          // 6.0.0.0 (2014/04/11) Oracle11g(11.2.0.3のドライバ) 対応。oracle.sql.ARRAY の置き換え
029import oracle.jdbc.OracleConnection;                            // 6.0.0.0 (2014/04/11) Oracle11g(11.2.0.3のドライバ) 対応
030
031import java.util.Map;
032
033/**
034 * PL/SQL にエントリ情報を配列渡しする、Query 実行クラスです。
035 *
036 * java.sql.CallableStatement を用いて、データベース登録処理を行います。
037 * 引数に、キーと値をセットで配列指定で渡すことが出来,エラー時には、
038 * DBErrMsg オブジェクトにエラー情報を格納して返すことが可能です。
039 * 内部変数の受け渡しのデフォルト実装は、AbstractQuery クラスを継承している
040 * ため,ここでは、execute() メソッドを実装しています。
041 * このクラスでは、ステートメント文を execute() する事により,データベースを
042 * 検索した結果を DBTableModel に割り当てます。
043 *
044 * @og.formSample
045 * 例:
046 *     第一引数、第二引数は、結果(KEKKA)とエラーメッセージ配列を返します。
047 *     キーエントリでは、エントリ(リクエスト情報)のキーと値をセットで
048 *     引数の配列に設定します。
049 *     キーを元に、値を利用する場合に使用します。
050 *     下記の例は、動的カラムを実現しているPL/SQLの例です。
051 *
052 * <og:entryQuery
053 *     command    = "NEW"
054 *     queryType  = "JDBCKeyEntry" >
055 *         { call DYNAMIC_PKG.DYNAMIC_TEST( ?,?,?,? ) }
056 * </og:entryQuery>
057 *
058 *    CREATE OR REPLACE PACKAGE DYNAMIC_PKG AS
059 *        PROCEDURE DYNAMIC_TEST(
060 *            P_KEKKA       OUT    NUMBER,
061 *            P_ERRMSGS     OUT    ERR_MSG_ARRAY,
062 *            P_KEYS        IN     ARG_ARRAY,
063 *            P_ARGS        IN     ARG_ARRAY );
064 *    END;
065 *
066 * @og.group データ編集
067 *
068 * @version  4.0
069 * @author   Kazuhiko Hasegawa
070 * @since    JDK5.0,
071 */
072public class Query_JDBCKeyEntry extends AbstractQuery {
073        /** このプログラムのVERSION文字列を設定します。   {@value} */
074        private static final String VERSION = "6.4.2.1 (2016/02/05)" ;
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_JDBCKeyEntry() { super(); }                // これも、自動的に呼ばれるが、空のメソッドを作成すると警告されるので、明示的にしておきます。
082
083        /**
084         * 引数配列付のクエリーを実行します。
085         * 処理自体は, #execute() と同様に、各サブクラスの実装に依存します。
086         * これは、PreparedQuery で使用する引数を配列でセットするものです。
087         * select * from emp where deptno = ? and job = ? などの PreparedQuery の
088         * ? 部分の引数を
089         * 順番にセットしていきます。
090         *
091         * @og.rev 3.1.1.0 (2003/03/28) 同期メソッド(synchronized付き)を非同期に変更する。
092         * @og.rev 3.3.3.1 (2003/07/18) DB登録時の後ろスペースを削除する。
093         * @og.rev 3.5.2.0 (2003/10/20) 内部オブジェクトタイプ名を システムパラメータ で定義します。
094         * @og.rev 3.5.6.0 (2004/06/18) nullに対する無駄な比較を削除します。
095         * @og.rev 3.8.0.8 (2005/10/03) エラーメッセージの出力順をメッセージ+Queryに変更します。
096         * @og.rev 6.0.0.0 (2014/04/11) Oracle11g(11.2.0.3のドライバ)対応
097         * @og.rev 6.3.6.1 (2015/08/28) close(),realClose() 廃止。Queryはキャッシュしません。
098         * @og.rev 6.4.2.1 (2016/02/05) try-with-resources 文で記述。
099         * @og.rev 6.9.3.0 (2018/03/26) DB_FETCH_SIZE追加。
100         *
101         * @param   keys オブジェクトのキー配列
102         * @param   args オブジェクトの引数配列(可変長引数)
103         */
104        @Override
105        public void execute( final String[] keys, final String... args ) {                      // 6.1.1.0 (2015/01/17) refactoring
106                        final Connection conn = getConnection();
107                // 6.4.2.1 (2016/02/05) try-with-resources 文
108                try( final CallableStatement callStmt = conn.prepareCall( getStatement() ) ) {
109                        callStmt.setQueryTimeout( DB_MAX_QUERY_TIMEOUT );
110                        callStmt.setFetchSize( DB_FETCH_SIZE );                 // 6.9.3.0 (2018/03/26)
111
112                        final Map<String,Class<?>> map = conn.getTypeMap();
113                        map.put( ERR_MSG,DBErrMsg.class );      // 4.0.0 (2005/01/31)
114
115                        // 6.0.0.0 (2014/04/11) Oracle11g(11.2.0.3のドライバ)対応 http://docs.oracle.com/cd/E28389_01/web.1111/b60995/thirdparty.htm
116                        final Array newArray_key = ((OracleConnection)conn).createOracleArray( ARG_ARRAY, keys );               // 6.0.0.0 (2014/04/11) Oracle11g(11.2.0.3のドライバ)対応
117
118                        // 6.0.0.0 (2014/04/11) Oracle11g(11.2.0.3のドライバ)対応 http://docs.oracle.com/cd/E28389_01/web.1111/b60995/thirdparty.htm
119                        final Array newArray_val = ((OracleConnection)conn).createOracleArray( ARG_ARRAY, StringUtil.rTrims( args ));           // 6.0.0.0 (2014/04/11) Oracle11g(11.2.0.3のドライバ)対応
120
121                        callStmt.registerOutParameter(1, Types.INTEGER);
122                        callStmt.registerOutParameter(2, Types.ARRAY,ERR_MSG_ARRAY);                            // 6.0.0.0 (2014/04/11) Oracle11g(11.2.0.3のドライバ)対応
123                        callStmt.setArray( 3,newArray_key );                                                                            // 6.0.0.0 (2014/04/11) Oracle11g(11.2.0.3のドライバ)対応
124                        callStmt.setArray( 4,newArray_val );                                                                            // 6.0.0.0 (2014/04/11) Oracle11g(11.2.0.3のドライバ)対応
125
126                        callStmt.execute();
127
128                        final int rtnCode = callStmt.getInt(1);
129                        setErrorCode( rtnCode );
130                        if( rtnCode > ErrorMessage.OK ) {               // 正常以外の場合
131                                final Array rtn3 = callStmt.getArray(2);                                                                                // 6.0.0.0 (2014/04/11) Oracle11g(11.2.0.3のドライバ)対応
132                                final Object[] rtnval3 = (Object[])rtn3.getArray();
133                                final ErrorMessage errMessage = new ErrorMessage( "Query_JDBCKeyEntry Error!!" );
134                                for( int i=0; i<rtnval3.length; i++ ) {
135                                        final DBErrMsg er = (DBErrMsg)rtnval3[i];
136                                        if( er == null ) { break; }
137                                        errMessage.addMessage( er.getErrMsg() );
138                                }
139                                setErrorMessage( errMessage );
140                        }
141                }
142                catch( final SQLException ex) {         // catch は、close() されてから呼ばれます。
143                        setErrorCode( ErrorMessage.EXCEPTION );
144                        final String errMsg = ex.getMessage() + ":" + ex.getSQLState() + CR
145                                                + getStatement() + CR ;
146                        throw new HybsSystemException( errMsg,ex );             // 3.5.5.4 (2004/04/15) 引数の並び順変更
147                }
148        }
149}