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