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.hayabusa.db.DBErrMsg;
022    import org.opengion.fukurou.util.ErrorMessage;
023    import org.opengion.fukurou.util.StringUtil;
024    import org.opengion.fukurou.util.Closer;
025    
026    import java.sql.CallableStatement;
027    import java.sql.Connection;
028    import java.sql.SQLException;
029    import java.sql.Types;
030    import java.util.Map;
031    
032    import oracle.jdbc.OracleCallableStatement;
033    import oracle.jdbc.OracleTypes;
034    import oracle.sql.ARRAY;
035    
036    /**
037     * Callableのエラー配å?対応版ですã?バッチ系標準ã?PL/SQL をコールする Query クラスですã?
038     *
039     * java.sql.CallableStatement を用ã�?�¦、データベã?ス検索処ç�?‚’行いますã?
040     * 引数は、従来のPL/SQLの実行が可能なように、第ä¸?¼•数はエラーコードã?第二引数はã€?
041     * エラーメãƒ?‚»ージを返してきますã?第三引数以降ã?、è?由にæŒ?®šできますã?
042     * å†?ƒ¨変数の受け渡しã?ãƒ?ƒ•ォルト実è£??、AbstractQuery クラスを継承してã�?‚‹
043     * ため,ここでは、execute() メソãƒ?ƒ‰を実è£?�—てã�?�¾すã?
044     *
045     * @og.formSample
046     * 例ï¼?
047     *     第ä¸?¼•数、第二引数はã€??常のPL/SQLと同じ、結果(STATUS)と
048     *     å†?®¹(ERR_CODE)を返しますã?
049     *     それ以降ã?引数につã�?�¦は、å?åŠ?IN)のみですが、è?由に設定できますã?
050     *     引数に変数を使用する場合ã?ã€? 記号を当てはめますã?
051     *     第ä¸?¼•数、第二引数は、予ç´?¸ˆみですが、それ以降ã?、好きな位置に割り当てられますã?
052     *     names 属æ?のé ?•ªに、å?ã�?�‘がセãƒ?ƒˆされてã�?��ますã?
053     *     下記ã?例ã?、変数の引数は、使用してã�?�¾せんã€?
054     *
055     * <og:query
056     *     command="NEW"
057     *     queryType="JDBCArrayCallable"
058     *     displayMsg="" >
059     *         { call GEP00002.GEP00002( ?,?,'{@GUI.KEY}','{@USER.ID}' ) }
060     * </og:query>
061     *
062     *    CREATE OR REPLACE PACKAGE GEP00002 AS
063     *        PROCEDURE GEP00002(
064     *            P_STATUS    OUT    NUMBER,
065     *            P_ERR_CODE  OUT    ERR_MSG_ARRAY,
066     *            P_MIDDB     IN     VARCHAR2,
067     *            P_USRUPD    IN     VARCHAR2  );
068     *    END;
069     *
070     * @og.group ãƒ??タ表示
071     * @og.group ãƒ??タ編é›?
072     *
073     * @version  4.0
074     * @author   高橋正å’?
075     * @since    JDK5.0,
076     */
077    public class Query_JDBCArrayCallable extends AbstractQuery {
078            //* こã?プログラãƒ??VERSIONæ–?­—å?を設定しますã?       {@value} */
079            private static final String VERSION = "4.0.0.0 (2005/08/31)" ;
080    
081            /**
082             * クエリーを実行しますã?
083             * セãƒ?ƒˆされてã�?‚‹スãƒ??トメント文字å?とそã?タイプが合ってã�?�ªã�??合ã?,
084             * エラーになりますã?
085             * 実行結果は、DBTableModel にセãƒ?ƒˆされますã?
086             *
087             */
088            @Override
089            public void execute() {
090                    execute( null );
091            }
092    
093            /**
094             * 引数配å?付ã?クエリーを実行しますã?
095             * 処ç�??体ã?, #execute() と同様に、各サブクラスの実è£?�«依存しますã?
096             * これは、PreparedQuery で使用する引数をé?列でセãƒ?ƒˆするもã?ですã?
097             * select * from emp where deptno = ? and job = ? などの PreparedQuery の
098             * ? 部åˆ??引数ã‚?
099             * é ?•ªにセãƒ?ƒˆしてã�?��ますã?
100             *
101             * @param   args オブジェクトã?引数配å?
102             */
103            @Override
104            public void execute( final String[] args ) {
105                    CallableStatement callStmt = null ;
106                    try {
107                            Connection conn = getConnection();
108                            callStmt  = getConnection().prepareCall( getStatement() );
109                            callStmt.setQueryTimeout( DB_MAX_QUERY_TIMEOUT );
110                            Map<String,Class<?>> map = conn.getTypeMap();
111                            map.put( ERR_MSG,DBErrMsg.class );
112    
113    //                      ArrayDescriptor sd = ArrayDescriptor.createDescriptor( ARG_ARRAY, conn );
114                            callStmt.registerOutParameter(1, Types.INTEGER);
115                            callStmt.registerOutParameter(2, OracleTypes.ARRAY,ERR_MSG_ARRAY);
116                            if( args != null ) {
117                                    for( int i=0; i<args.length; i++ ) {
118                                            callStmt.setObject( i+3,StringUtil.rTrim( args[i] ) );
119                                    }
120                            }
121                            callStmt.execute();
122    
123                            int rtnCode = callStmt.getInt(1);
124                            setErrorCode( rtnCode );
125    
126                            if( rtnCode > ErrorMessage.OK ) {            // 正常以外ã?場å�?
127                                    ARRAY rtn3 = ((OracleCallableStatement)callStmt).getARRAY(2);
128                                    Object[] rtnval3 = (Object[])rtn3.getArray();
129                                    ErrorMessage errMessage = new ErrorMessage( "Query_JDBCArrayCallable Error!!" );
130                                    for( int i=0; i<rtnval3.length; i++ ) {
131                                            DBErrMsg er = (DBErrMsg)rtnval3[i];
132                                            if( er == null ) { break; }
133                                            errMessage.addMessage( er.getErrMsg() );
134                                    }
135                                    setErrorMessage( errMessage );
136                            }
137                    }
138                    catch ( SQLException ex ) {
139                            setErrorCode( ErrorMessage.EXCEPTION );
140                            String errMsg = ex.getMessage() + ":" + ex.getSQLState() + HybsSystem.CR
141                                                    + getStatement() + HybsSystem.CR;
142                            rollback();
143                            realClose();
144                            throw new HybsSystemException( errMsg,ex );
145                    }
146                    finally {
147                            Closer.stmtClose( callStmt );
148                    }
149            }
150    }