OSDN Git Service

Ver8.5.2.0
[opengion/opengionV8.git] / uap / webapps / gf / src / org / opengion / plugin / query / Query_JDBCCallable.java
1 /*
2  * Copyright (c) 2009 The openGion Project.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific language
14  * governing permissions and limitations under the License.
15  */
16 package org.opengion.plugin.query;
17
18 import org.opengion.hayabusa.common.HybsSystemException;
19 import org.opengion.hayabusa.db.AbstractQuery;
20 import org.opengion.fukurou.util.ErrorMessage;
21 import org.opengion.fukurou.util.StringUtil;
22 import org.opengion.fukurou.system.Closer;
23
24 import java.sql.CallableStatement;
25 import java.sql.SQLException;
26 import java.sql.Types;
27
28 /**
29  * バッチ系標準のPL/SQL をコールする Query クラスです。
30  *
31  * java.sql.CallableStatement を用いて、データベース検索処理を行います。
32  * 引数は、従来のPL/SQLの実行が可能なように、第一引数はエラーコード、第二引数は、
33  * エラーメッセージを返してきます。第三引数以降は、自由に指定できます。
34  * 内部変数の受け渡しのデフォルト実装は、AbstractQuery クラスを継承している
35  * ため、ここでは、execute() メソッドを実装しています。
36  *
37  * @og.formSample
38  * 例:
39  *     第一引数、第二引数は、通常のPL/SQLと同じ、結果(STATUS)と
40  *     内容(ERR_CODE)を返します。
41  *     それ以降の引数については、入力(IN)のみですが、自由に設定できます。
42  *     引数に変数を使用する場合は、? 記号を当てはめます。
43  *     第一引数、第二引数は、予約済みですが、それ以降は、好きな位置に割り当てられます。
44  *     names 属性の順番に、値だけがセットされていきます。
45  *     下記の例は、変数の引数は、使用していません。
46  *
47  * <og:query
48  *     command="NEW"
49  *     queryType="JDBCCallable"
50  *     displayMsg="" >
51  *         { call GEP00002.GEP00002( ?,?,'{@GUI.KEY}','{@USER.ID}' ) }
52  * </og:query>
53  *
54  *    CREATE OR REPLACE PACKAGE GEP00002 AS
55  *        PROCEDURE GEP00002(
56  *            P_STATUS    OUT    NUMBER,
57  *            P_ERR_CODE  OUT    VARCHAR2,
58  *            P_MIDDB     IN     VARCHAR2,
59  *            P_USRUPD    IN     VARCHAR2  );
60  *    END;
61  *
62  * @og.group データ表示
63  * @og.group データ編集
64  *
65  * @version  4.0
66  * @author   Kazuhiko Hasegawa
67  * @since    JDK5.0,
68  */
69 public class Query_JDBCCallable extends AbstractQuery {
70         /** このプログラムのVERSION文字列を設定します。       {@value} */
71         private static final String VERSION = "6.9.3.0 (2018/03/26)" ;
72
73         /**
74          * デフォルトコンストラクター
75          *
76          * @og.rev 6.4.2.0 (2016/01/29) PMD refactoring. Each class should declare at least one constructor.
77          */
78         public Query_JDBCCallable() { super(); }                // これも、自動的に呼ばれるが、空のメソッドを作成すると警告されるので、明示的にしておきます。
79
80         /**
81          * 引数配列付のクエリーを実行します。
82          * 処理自体は、#execute() と同様に、各サブクラスの実装に依存します。
83          * これは、PreparedQuery で使用する引数を配列でセットするものです。
84          * select * from emp where deptno = ? and job = ? などの PreparedQuery の
85          * ? 部分の引数を
86          * 順番にセットしていきます。
87          *
88          * @og.rev 3.1.1.0 (2003/03/28) 同期メソッド(synchronized付き)を非同期に変更する。
89          * @og.rev 3.3.3.1 (2003/07/18) DB登録時の後ろスペースを削除する。
90          * @og.rev 3.5.6.0 (2004/06/18) nullに対する無駄な比較を削除します。
91          * @og.rev 3.8.0.8 (2005/10/03) エラーメッセージの出力順をメッセージ+Queryに変更します。
92          * @og.rev 6.3.6.1 (2015/08/28) close(),realClose() 廃止。Queryはキャッシュしません。
93          * @og.rev 6.9.3.0 (2018/03/26) DB_FETCH_SIZE追加。
94          *
95          * @param   args オブジェクトの引数配列(可変長引数)
96          */
97         @Override
98         public void execute( final String... args ) {                   // 6.1.1.0 (2015/01/17) refactoring
99                 CallableStatement callStmt = null ;
100                 try {
101                         callStmt  = getConnection().prepareCall( getStatement() );
102                         callStmt.setQueryTimeout( DB_MAX_QUERY_TIMEOUT );
103                         callStmt.setFetchSize( DB_FETCH_SIZE );                 // 6.9.3.0 (2018/03/26)
104
105                         callStmt.registerOutParameter(1, Types.INTEGER);
106                         callStmt.registerOutParameter(2, Types.VARCHAR);
107                         // 6.1.1.0 (2015/01/17) refactoring. 可変引数にしたため、null は来ない。
108                                 for( int i=0; i<args.length; i++ ) {
109                                         callStmt.setObject( i+3,StringUtil.rTrim( args[i] ) );
110                                 }
111                         callStmt.execute();
112
113                         final int rtnCode = callStmt.getInt(1);
114                         setErrorCode( rtnCode );
115
116                         if( rtnCode > ErrorMessage.OK ) {               // 正常以外の場合
117                                 final String ermsg = callStmt.getString(2);
118                                 final ErrorMessage errMessage = new ErrorMessage( "Query_JDBCCallable Error!!" );
119                                 errMessage.addMessage( ermsg );
120                                 setErrorMessage( errMessage );
121                         }
122                 }
123                 catch( final SQLException ex ) {
124                         setErrorCode( ErrorMessage.EXCEPTION );
125                         final String errMsg = ex.getMessage() + ":" + ex.getSQLState() + CR
126                                                 + getStatement() + CR;
127                         throw new HybsSystemException( errMsg,ex );             // 3.5.5.4 (2004/04/15) 引数の並び順変更
128                 }
129                 finally {
130                         Closer.stmtClose( callStmt );
131                 }
132         }
133 }