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.CallableStatement;
026    import java.sql.SQLException;
027    import java.sql.Types;
028    
029    /**
030     * バッチ系標準?PL/SQL をコールする Query クラスです?
031     *
032     * java.sql.CallableStatement を用?、データベ?ス検索処?行います?
033     * 引数は、従来のPL/SQLの実行が可能なように、第?数はエラーコード?第二引数は?
034     * エラーメ?ージを返してきます?第三引数以降?、?由に?できます?
035     * ?変数の受け渡し??ォルト実??、AbstractQuery クラスを継承して?
036     * ため,ここでは、execute() メソ?を実?て?す?
037     *
038     * @og.formSample
039     * 例?
040     *     第?数、第二引数は??常のPL/SQLと同じ、結果(STATUS)と
041     *     ?(ERR_CODE)を返します?
042     *     それ以降?引数につ?は、??IN)のみですが、?由に設定できます?
043     *     引数に変数を使用する場合?? 記号を当てはめます?
044     *     第?数、第二引数は、予?みですが、それ以降?、好きな位置に割り当てられます?
045     *     names 属?の?に、??がセ?されて?ます?
046     *     下記?例?、変数の引数は、使用して?せん?
047     *
048     * <og:query
049     *     command="NEW"
050     *     queryType="JDBCCallable"
051     *     displayMsg="" >
052     *         { call GEP00002.GEP00002( ?,?,'{@GUI.KEY}','{@USER.ID}' ) }
053     * </og:query>
054     *
055     *    CREATE OR REPLACE PACKAGE GEP00002 AS
056     *        PROCEDURE GEP00002(
057     *            P_STATUS    OUT    NUMBER,
058     *            P_ERR_CODE  OUT    VARCHAR2,
059     *            P_MIDDB     IN     VARCHAR2,
060     *            P_USRUPD    IN     VARCHAR2  );
061     *    END;
062     *
063     * @og.group ??タ表示
064     * @og.group ??タ編?
065     *
066     * @version  4.0
067     * @author   Kazuhiko Hasegawa
068     * @since    JDK5.0,
069     */
070    public class Query_JDBCCallable extends AbstractQuery {
071            //* こ?プログラ??VERSION??を設定します?       {@value} */
072            private static final String VERSION = "4.0.0.0 (2005/08/31)" ;
073    
074            /**
075             * クエリーを実行します?
076             * セ?されて?ス??トメント文字?とそ?タイプが合って???合?,
077             * エラーになります?
078             * 実行結果は、DBTableModel にセ?されます?
079             *
080             */
081            @Override
082            public void execute() {
083                    execute( null );
084            }
085    
086            /**
087             * 引数配?付?クエリーを実行します?
088             * 処??体?, #execute() と同様に、各サブクラスの実?依存します?
089             * これは、PreparedQuery で使用する引数を?列でセ?するも?です?
090             * select * from emp where deptno = ? and job = ? などの PreparedQuery の
091             * ? 部??引数?
092             * ?にセ?して?ます?
093             *
094             * @og.rev 3.1.1.0 (2003/03/28) 同期メソ?(synchronized付き)を非同期に変更する?
095             * @og.rev 3.3.3.1 (2003/07/18) ??登録時?後ろスペ?スを削除する?
096             * @og.rev 3.5.6.0 (2004/06/18) nullに対する無?比?削除します?
097             * @og.rev 3.8.0.8 (2005/10/03) エラーメ?ージの出力?をメ?ージ?Queryに変更します?
098             *
099             * @param   args オブジェクト?引数配?
100             */
101            @Override
102            public void execute( final String[] args ) {
103                    CallableStatement callStmt = null ;
104                    try {
105                            callStmt  = getConnection().prepareCall( getStatement() );
106                            callStmt.setQueryTimeout( DB_MAX_QUERY_TIMEOUT );
107    
108                            callStmt.registerOutParameter(1, Types.INTEGER);
109                            callStmt.registerOutParameter(2, Types.VARCHAR);
110                            if( args != null ) {
111                                    for( int i=0; i<args.length; i++ ) {
112                                            callStmt.setObject( i+3,StringUtil.rTrim( args[i] ) );
113                                    }
114                            }
115                            callStmt.execute();
116    
117                            int rtnCode = callStmt.getInt(1);
118                            setErrorCode( rtnCode );
119    
120                            if( rtnCode > ErrorMessage.OK ) {            // 正常以外?場?
121                                    String ermsg = callStmt.getString(2);
122                                    ErrorMessage errMessage = new ErrorMessage( "Query_JDBCCallable Error!!" );
123                                    errMessage.addMessage( ermsg );
124                                    setErrorMessage( errMessage );
125                            }
126                    }
127                    catch ( SQLException ex ) {
128                            setErrorCode( ErrorMessage.EXCEPTION );
129                            String errMsg = ex.getMessage() + ":" + ex.getSQLState() + HybsSystem.CR
130                                                    + getStatement() + HybsSystem.CR;
131                            rollback();
132                            realClose();
133                            throw new HybsSystemException( errMsg,ex );             // 3.5.5.4 (2004/04/15) 引数の並び?更
134                    }
135                    finally {
136                            Closer.stmtClose( callStmt );
137                    }
138            }
139    }