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.PreparedStatement;
026import java.sql.ResultSet;
027import java.sql.SQLException;
028
029/**
030 * 引数引き当て(PreparedStatement) を利用した登録系Queryです。
031 *
032 * java.sql.PreparedStatement を用いて、データベース検索処理を行います。
033 * 引数に、指定した値を配列で渡します。
034 * 内部変数の受け渡しのデフォルト実装は、AbstractQuery クラスを継承している
035 * ため,ここでは、execute() メソッドを実装しています。
036 * このクラスでは、ステートメント文を execute() する事により,データベースを
037 * 検索した結果を DBTableModel に割り当てます。
038 *
039 * このクラスは、Query で使用されるより、内部の DBTableModelUtilから、
040 * 利用されるケースが主です。
041 * Query で使用する場合は、JDBCPrepared ではなく、JDBCTableUpdate を
042 * 使用することを、ご検討ください。
043 *
044 * @og.formSample
045 * 例:
046 *     可変引数付きのSQL文を実行します。
047 *     これは、INSERT,UPDATE,DELETE など、どのようなSQL文でも実行できます。
048 *     names 属性で指定するのは、DBTableModelのカラム名で、その値が順番に、
049 *     引数(?記号)の個所に設定されます。
050 *     選択されたデータ(行)の数だけ、繰り返し実行されます。
051 *
052 * jsp/TYPE1A/copy.jsp
053 * <og:value scope="session"
054 *            key="names"
055 *            value="CLM,NAME_JA,LABEL_NAME,KBSAKU,SYSTEM_ID,LANG" />
056 * <og:value scope="session" key="SQL" >
057 *     INSERT INTO GEA08
058 *         (CLM,NAME_JA,LABEL_NAME,KBSAKU,SYSTEM_ID,LANG,
059 *          FGJ,DYSET,DYUPD,USRSET,USRUPD,PGUPD)
060 *     VALUES
061 *         (?,?,?,?,?,?,
062 *          '1','{@USER.YMDH}','{@USER.YMDH}','{@USER.ID}','{@USER.ID}','{@GUI.KEY}')
063 * </og:value>
064 *
065 * jsp/TYPE1A/entry.jsp
066 * <h:tableUpdate
067 *     command   = "{@command}"
068 *     queryType = "JDBCPrepared"
069 *     names     = "{@names}"  >
070 * {@SQL}
071 * </og:tableUpdate>
072 *
073 * <!-- 前画面で指定のSQL文を削除します。(scope="session"なので削除が必要。) -->
074 * <og:value scope="session" key="names" command="REMOVE" />
075 * <og:value scope="session" key="SQL"   command="REMOVE" />
076 *
077 * @og.group データ表示
078 * @og.group データ編集
079 *
080 * @version  4.0
081 * @author   Kazuhiko Hasegawa
082 * @since    JDK5.0,
083 */
084public class Query_JDBCPrepared extends AbstractQuery {
085        //* このプログラムのVERSION文字列を設定します。   {@value} */
086        private static final String VERSION = "5.3.8.0 (2011/08/01)" ;
087
088        private PreparedStatement pstmt = null ;
089
090        /**
091         * 引数配列付のクエリーを実行します。
092         * 処理自体は, #execute() と同様に、各サブクラスの実装に依存します。
093         * これは、PreparedQuery で使用する引数を配列でセットするものです。
094         * select * from emp where deptno = ? and job = ? などの PreparedQuery の
095         * ? 部分の引数を
096         * 順番にセットしていきます。
097         *
098         * @og.rev 2.1.2.3 (2002/12/02) データベース更新時に、更新フラグをセットするように変更
099         * @og.rev 2.3.1.3 (2003/01/28) Open Cursor が、大量に残る件の対応。ResultSet を close()
100         * @og.rev 3.1.1.0 (2003/03/28) 同期メソッド(synchronized付き)を非同期に変更する。
101         * @og.rev 3.3.3.1 (2003/07/18) DB登録時の後ろスペースを削除する。
102         * @og.rev 3.5.6.0 (2004/06/18) PreparedStatement をexecute 間で使いまわします。
103         * @og.rev 3.8.0.8 (2005/10/03) エラーメッセージの出力順をメッセージ+Queryに変更します。
104         * @og.rev 5.3.8.0 (2011/08/01) pstmt.setObject で、useParamMetaData の判定を避けるため、pstmt.setString で代用(PostgreSQL対応)
105         *
106         * @param   args オブジェクトの引数配列
107         */
108        @Override
109        public void execute( final String[] args ) {
110                ResultSet resultSet = null ;
111                try {
112                        if( pstmt== null ) {
113                                pstmt = getConnection().prepareStatement( getStatement() );
114                                pstmt.setQueryTimeout( DB_MAX_QUERY_TIMEOUT );
115                        }
116
117                        if( args != null ) {
118                                for( int i=0; i<args.length; i++ ) {
119                                        pstmt.setString( i+1,StringUtil.rTrim( args[i] ) );     // 5.3.8.0 (2011/08/01) 処理の簡素化
120                                }
121                        }
122
123                        boolean status = pstmt.execute();
124                        if( status ) {
125                                resultSet = pstmt.getResultSet();
126                                createTableModel( resultSet );
127                                setUpdateFlag( false );
128                        }
129                        else {
130                                setExecuteCount( pstmt.getUpdateCount() );
131                        }
132
133                        setErrorCode( ErrorMessage.OK );
134                }
135                catch ( SQLException ex ) {
136                        setErrorCode( ErrorMessage.EXCEPTION );
137                        Closer.stmtClose( pstmt );
138
139                        String errMsg = ex.getMessage() + ":" + ex.getSQLState() + HybsSystem.CR
140                                                + getStatement() + HybsSystem.CR;
141                        rollback();
142                        realClose();
143                        throw new HybsSystemException( errMsg,ex );             // 3.5.5.4 (2004/04/15) 引数の並び順変更
144                }
145                finally {
146                        Closer.resultClose( resultSet );
147                }
148        }
149
150        /**
151         * PreparedStatement をクローズします。
152         */
153        @Override
154        public void close() {
155                Closer.stmtClose( pstmt );
156                super.close();
157        }
158}