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;
026
027/**
028 * 一般的な PL/SQLをコールする、Query クラスです。
029 *
030 * java.sql.CallableStatement を用いて、データベース登録処理を行います。
031 * 引数は、そのまま配列に格納して処理を行います。エラー時の処理や、検索結果の
032 * 取り出しは出来ません。
033 * 内部変数の受け渡しのデフォルト実装は、AbstractQuery クラスを継承している
034 * ため,ここでは、execute() メソッドを実装しています。
035 * このクラスでは、ステートメント文を execute() する事により,データベースを
036 * 検索した結果を DBTableModel に割り当てます。
037 *
038 * 例:
039 *     Hybs独自のステータスやエラーメッセージなどの引数を持たない、
040 *     一般的なPL/SQLをCALLします。
041 *     names 属性で指定するのは、DBTableModelのカラム名で、その値が順番に、
042 *     引数(?記号)の個所に設定されます。
043 *     引数が、? でない個所には、直接値を設定したり、{@カラム名}で、
044 *     リクエスト変数をセットする事も可能です。
045 *     選択されたデータ(行)の数だけ、繰り返し実行されます。
046 *     下記の例は、テーブルのアナライザを実行しています。
047 *
048 *   jsp/ORA08/result.jsp
049 *   <og:query
050 *     displayMsg = ""
051 *     command    = "{@command}"
052 *     names      = "TABLE_OWNER,TABLE_NAME"
053 *     queryType  = "JDBCUpdate" >
054 *          { call DBMS_STATS.GATHER_TABLE_STATS( ?,? ) }
055 *   </og:query>
056 *
057 * @og.group データ編集
058 *
059 * @version  4.0
060 * @author   Kazuhiko Hasegawa
061 * @since    JDK5.0,
062 */
063public class Query_JDBCUpdate extends AbstractQuery {
064        /** このプログラムのVERSION文字列を設定します。   {@value} */
065        private static final String VERSION = "6.4.2.0 (2016/01/29)" ;
066
067        /**
068         * デフォルトコンストラクター
069         *
070         * @og.rev 6.4.2.0 (2016/01/29) PMD refactoring. Each class should declare at least one constructor.
071         */
072        public Query_JDBCUpdate() { super(); }          // これも、自動的に呼ばれるが、空のメソッドを作成すると警告されるので、明示的にしておきます。
073
074        /**
075         * 引数配列付のクエリーを実行します。
076         * 処理自体は, #execute() と同様に、各サブクラスの実装に依存します。
077         * これは、PreparedQuery で使用する引数を配列でセットするものです。
078         * select * from emp where deptno = ? and job = ? などの PreparedQuery の
079         * ? 部分の引数を
080         * 順番にセットしていきます。
081         *
082         * @og.rev 3.1.1.0 (2003/03/28) 同期メソッド(synchronized付き)を非同期に変更する。
083         * @og.rev 3.3.3.1 (2003/07/18) DB登録時の後ろスペースを削除する。
084         * @og.rev 3.5.6.0 (2004/06/18) nullに対する無駄な比較を削除します。
085         * @og.rev 3.8.0.8 (2005/10/03) エラーメッセージの出力順をメッセージ+Queryに変更します。
086         * @og.rev 6.3.6.1 (2015/08/28) close(),realClose() 廃止。Queryはキャッシュしません。
087         *
088         * @param   args オブジェクトの引数配列(可変長引数)
089         */
090        @Override
091        public void execute( final String... args ) {                   // 6.1.1.0 (2015/01/17) refactoring
092                CallableStatement callStmt = null ;
093                try {
094                        callStmt  = getConnection().prepareCall( getStatement() );              // 更新系なので、setFetchSize は不要。
095                        callStmt.setQueryTimeout( DB_MAX_QUERY_TIMEOUT );
096
097                        // 6.1.1.0 (2015/01/17) refactoring. 可変引数にしたため、null は来ない。
098                        for( int i=0; i<args.length; i++ ) {
099                                callStmt.setObject( i+1,StringUtil.rTrim( args[i] ) );
100                        }
101                        callStmt.execute();
102                        setErrorCode( ErrorMessage.OK );
103                }
104                catch( final SQLException ex ) {
105                        setErrorCode( ErrorMessage.EXCEPTION );
106                        final String errMsg = ex.getMessage() + ":" + ex.getSQLState() + CR
107                                                + getStatement() + CR;
108                        throw new HybsSystemException( errMsg,ex );             // 3.5.5.4 (2004/04/15) 引数の並び順変更
109                }
110                finally {
111                        Closer.stmtClose( callStmt );
112                }
113        }
114}