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.fukurou.util.ErrorMessage; 021import org.opengion.fukurou.util.StringUtil; 022import org.opengion.fukurou.system.Closer; 023 024import java.sql.CallableStatement; 025import java.sql.SQLException; 026import java.sql.Types; 027 028/** 029 * バッチ系標準のPL/SQL をコールする Query クラスです。 030 * 031 * java.sql.CallableStatement を用いて、データベース検索処理を行います。 032 * 引数は、従来のPL/SQLの実行が可能なように、第一引数はエラーコード、第二引数は、 033 * エラーメッセージを返してきます。第三引数以降は、自由に指定できます。 034 * 内部変数の受け渡しのデフォルト実装は、AbstractQuery クラスを継承している 035 * ため、ここでは、execute() メソッドを実装しています。 036 * 037 * @og.formSample 038 * 例: 039 * 第一引数、第二引数は、通常のPL/SQLと同じ、結果(STATUS)と 040 * 内容(ERR_CODE)を返します。 041 * それ以降の引数については、入力(IN)のみですが、自由に設定できます。 042 * 引数に変数を使用する場合は、? 記号を当てはめます。 043 * 第一引数、第二引数は、予約済みですが、それ以降は、好きな位置に割り当てられます。 044 * names 属性の順番に、値だけがセットされていきます。 045 * 下記の例は、変数の引数は、使用していません。 046 * 047 * <og:query 048 * command="NEW" 049 * queryType="JDBCCallable" 050 * displayMsg="" > 051 * { call GEP00002.GEP00002( ?,?,'{@GUI.KEY}','{@USER.ID}' ) } 052 * </og:query> 053 * 054 * CREATE OR REPLACE PACKAGE GEP00002 AS 055 * PROCEDURE GEP00002( 056 * P_STATUS OUT NUMBER, 057 * P_ERR_CODE OUT VARCHAR2, 058 * P_MIDDB IN VARCHAR2, 059 * P_USRUPD IN VARCHAR2 ); 060 * END; 061 * 062 * @og.group データ表示 063 * @og.group データ編集 064 * 065 * @version 4.0 066 * @author Kazuhiko Hasegawa 067 * @since JDK5.0, 068 */ 069public class Query_JDBCCallable extends AbstractQuery { 070 /** このプログラムのVERSION文字列を設定します。 {@value} */ 071 private static final String VERSION = "6.9.3.0 (2018/03/26)" ; 072 073 /** 074 * デフォルトコンストラクター 075 * 076 * @og.rev 6.4.2.0 (2016/01/29) PMD refactoring. Each class should declare at least one constructor. 077 */ 078 public Query_JDBCCallable() { super(); } // これも、自動的に呼ばれるが、空のメソッドを作成すると警告されるので、明示的にしておきます。 079 080 /** 081 * 引数配列付のクエリーを実行します。 082 * 処理自体は、#execute() と同様に、各サブクラスの実装に依存します。 083 * これは、PreparedQuery で使用する引数を配列でセットするものです。 084 * select * from emp where deptno = ? and job = ? などの PreparedQuery の 085 * ? 部分の引数を 086 * 順番にセットしていきます。 087 * 088 * @og.rev 3.1.1.0 (2003/03/28) 同期メソッド(synchronized付き)を非同期に変更する。 089 * @og.rev 3.3.3.1 (2003/07/18) DB登録時の後ろスペースを削除する。 090 * @og.rev 3.5.6.0 (2004/06/18) nullに対する無駄な比較を削除します。 091 * @og.rev 3.8.0.8 (2005/10/03) エラーメッセージの出力順をメッセージ+Queryに変更します。 092 * @og.rev 6.3.6.1 (2015/08/28) close(),realClose() 廃止。Queryはキャッシュしません。 093 * @og.rev 6.9.3.0 (2018/03/26) DB_FETCH_SIZE追加。 094 * 095 * @param args オブジェクトの引数配列(可変長引数) 096 */ 097 @Override 098 public void execute( final String... args ) { // 6.1.1.0 (2015/01/17) refactoring 099 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}