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    }