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