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.fukurou.util.ErrorMessage;
019import org.opengion.hayabusa.db.AbstractQuery;
020import org.opengion.hayabusa.db.DBSysArg;
021import org.opengion.hayabusa.db.DBUserArg;
022import org.opengion.hayabusa.db.DBErrMsg;
023import org.opengion.hayabusa.common.HybsSystemException;
024
025import java.sql.Connection;
026import java.sql.CallableStatement;
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 の置き換え
030import oracle.jdbc.OracleConnection;                            // 6.0.0.0 (2014/04/11) Oracle11g(11.2.0.3のドライバ) 対応
031
032import java.util.Map;
033
034/**
035 * PL/SQL をコールする 登録系 Queryクラスです。
036 *
037 * java.sql.CallableStatement を用いて、データベース検索処理を行います。
038 * 引数に、SYSARG_ARRAYと、ユーザーARG_ARRAY を配列指定で渡すことが出来、
039 * エラー時には、DBErrMsg オブジェクトにエラー情報を格納して返すことが可能です。
040 * 内部変数の受け渡しのデフォルト実装は、AbstractQuery クラスを継承している
041 * ため、ここでは、execute() メソッドを実装しています。
042 * このクラスでは、ステートメント文を execute() する事により、データベースを
043 * 検索した結果を DBTableModel に割り当てます。
044 *
045 * @og.formSample
046 * 例:jsp/TYPE1B/result.jsp
047 *     names には、GEA08ARG で定義したカラムを指定します。
048 *     呼び出す PL/SQL では、登録系PL/SQL です。
049 *
050 * <og:plsqlUpdate
051 *     command    = "{@command}"
052 *     names      = "SYSTEM_ID,LANG,CLM,NAME_JA,LABEL_NAME,KBSAKU,FGJ,USRSET"
053 *     dbType     = "GEA08ARG"
054 *     queryType  = "JDBCPLSQL" >
055 *            { call TYPE1B01.TYPE1B01( ?,?,?,?,? ) }
056 * </og:plsqlUpdate>
057 *
058 *      PROCEDURE TYPE1B01 (
059 *         P_KEKKA     OUT   NUMBER,          -- エラー結果(0:正常 1:警告 2:異常)
060 *         P_ERRMSGS   OUT   ERR_MSG_ARRAY,   -- エラーのあるときのエラーメッセージ配列
061 *         P_NAMES     IN    VARCHAR2,
062 *         P_SYSARGS   IN    SYSARG_ARRAY,    -- 引数 SYSTEMデータ
063 *         P_GE08ARGS  IN    GEA08ARG_ARRAY   -- 引数 USERデータ
064 *  );
065 *
066 * @og.group データ表示
067 * @og.group データ編集
068 *
069 * @version  4.0
070 * @author   Kazuhiko Hasegawa
071 * @since    JDK5.0,
072 */
073public class Query_JDBCPLSQL extends AbstractQuery {
074        /** このプログラムのVERSION文字列を設定します。   {@value} */
075        private static final String VERSION = "6.9.3.0 (2018/03/26)" ;
076
077        /**
078         * デフォルトコンストラクター
079         *
080         * @og.rev 6.4.2.0 (2016/01/29) PMD refactoring. Each class should declare at least one constructor.
081         */
082        public Query_JDBCPLSQL() { super(); }           // これも、自動的に呼ばれるが、空のメソッドを作成すると警告されるので、明示的にしておきます。
083
084        /**
085         * 引数配列付のクエリーを実行します。
086         * 処理自体は、#execute() と同様に、各サブクラスの実装に依存します。
087         * これは、PreparedQuery で使用する引数を配列でセットするものです。
088         * select * from emp where deptno = ? and job = ? などの PreparedQuery の
089         * ? 部分の引数を
090         * 順番にセットしていきます。
091         *
092         * @og.rev 3.1.1.0 (2003/03/28) 同期メソッド(synchronized付き)を非同期に変更する。
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 4.0.0.0 (2005/01/31) 引数をすべて受け取って実行するメソッドを標準メソッドとして追加
097         * @og.rev 6.0.0.0 (2014/04/11) Oracle11g(11.2.0.3のドライバ)対応
098         * @og.rev 6.3.6.1 (2015/08/28) close(),realClose() 廃止。Queryはキャッシュしません。
099         * @og.rev 6.4.2.1 (2016/02/05) try-with-resources 文で記述。
100         * @og.rev 6.9.3.0 (2018/03/26) DB_FETCH_SIZE追加。
101         *
102         * @param       names           カラム名(CSV形式)
103         * @param       dbArrayType     アレイタイプ名称
104         * @param       sysArg          DBSysArg配列
105         * @param       userArg         DBUserArg配列
106         */
107        @Override
108        public void execute( final String names,final String dbArrayType,
109                                                        final DBSysArg[] sysArg,final DBUserArg[] userArg ) {
110                        final Connection conn = getConnection();
111                // 6.4.2.1 (2016/02/05) try-with-resources 文
112                try( CallableStatement callStmt = conn.prepareCall( getStatement() ) ) {
113                        callStmt.setQueryTimeout( DB_MAX_QUERY_TIMEOUT );
114                        callStmt.setFetchSize( DB_FETCH_SIZE );                 // 6.9.3.0 (2018/03/26)
115
116                        final Map<String,Class<?>> map = conn.getTypeMap();
117                        map.put( ERR_MSG,DBErrMsg.class );      // 4.0.0 (2005/01/31)
118
119                        // 6.0.0.0 (2014/04/11) Oracle11g(11.2.0.3のドライバ)対応 http://docs.oracle.com/cd/E28389_01/web.1111/b60995/thirdparty.htm
120                        final Array newArray2 = ((OracleConnection)conn).createOracleArray( SYSARG_ARRAY, sysArg );                     // 6.0.0.0 (2014/04/11) Oracle11g(11.2.0.3のドライバ)対応
121
122                        // 6.0.0.0 (2014/04/11) Oracle11g(11.2.0.3のドライバ)対応 http://docs.oracle.com/cd/E28389_01/web.1111/b60995/thirdparty.htm
123                        final Array newArray = ((OracleConnection)conn).createOracleArray( dbArrayType, userArg );                      // 6.0.0.0 (2014/04/11) Oracle11g(11.2.0.3のドライバ)対応
124
125                        callStmt.registerOutParameter(1, Types.INTEGER);
126                        callStmt.registerOutParameter(2, Types.ARRAY,ERR_MSG_ARRAY);            // 6.0.0.0 (2014/04/11) Oracle11g(11.2.0.3のドライバ)対応
127                        callStmt.setString( 3,names );
128                        callStmt.setArray( 4,newArray2 );                                                                       // 6.0.0.0 (2014/04/11) Oracle11g(11.2.0.3のドライバ)対応
129                        callStmt.setArray( 5,newArray );                                                                        // 6.0.0.0 (2014/04/11) Oracle11g(11.2.0.3のドライバ)対応
130
131                        callStmt.execute();
132
133                        final int rtnCode = callStmt.getInt(1);
134                        setErrorCode( rtnCode );
135                        if( rtnCode > ErrorMessage.OK ) {               // 正常以外の場合
136                                final Array rtn3 = callStmt.getArray(2);                                                                // 6.0.0.0 (2014/04/11) Oracle11g(11.2.0.3のドライバ)対応
137                                final Object[] rtnval3 = (Object[])rtn3.getArray();
138                                final ErrorMessage errMessage = new ErrorMessage( "Query_JDBCPLSQL Error!!" );
139                                for( int i=0; i<rtnval3.length; i++ ) {
140                                        final DBErrMsg er = (DBErrMsg)rtnval3[i];
141                                        if( er == null ) { break; }
142                                        errMessage.addMessage( er.getErrMsg() );
143                                }
144                                setErrorMessage( errMessage );
145                        }
146                }
147                catch( final SQLException ex) {         // catch は、close() されてから呼ばれます。
148                        setErrorCode( ErrorMessage.EXCEPTION );
149                        final String errMsg = ex.getMessage() + ":" + ex.getSQLState() + CR
150                                                + getStatement() + CR;
151                        throw new HybsSystemException( errMsg,ex );             // 3.5.5.4 (2004/04/15) 引数の並び順変更
152                }
153        }
154}