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    }