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     */
016    package org.opengion.plugin.query;
017    
018    import org.opengion.hayabusa.common.HybsSystem;
019    import org.opengion.hayabusa.common.HybsSystemException;
020    import org.opengion.hayabusa.db.AbstractQuery;
021    import org.opengion.fukurou.util.ErrorMessage;
022    import org.opengion.fukurou.util.StringUtil;
023    import org.opengion.fukurou.util.Closer;
024    
025    import java.sql.PreparedStatement;
026    import java.sql.ResultSet;
027    import 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     *     可変引数付きの????実行します?
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) ??登録時?後ろスペ?スを削除する?
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    }