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.db.AbstractQuery;
019    import org.opengion.hayabusa.db.DBErrMsg;
020    import org.opengion.hayabusa.common.HybsSystem;
021    import org.opengion.hayabusa.common.HybsSystemException;
022    import org.opengion.fukurou.util.ErrorMessage;
023    import org.opengion.fukurou.util.StringUtil;
024    import org.opengion.fukurou.util.Closer;
025    
026    import oracle.jdbc.OracleTypes;
027    import oracle.jdbc.OracleCallableStatement;
028    
029    import oracle.sql.ARRAY;
030    import oracle.sql.ArrayDescriptor;
031    
032    import java.sql.Types;
033    import java.sql.Connection;
034    import java.sql.CallableStatement;
035    import java.sql.SQLException;
036    
037    import java.util.Map;
038    
039    /**
040     * PL/SQL にエントリ??を?列渡しする?Query 実行クラスです?
041     *
042     * java.sql.CallableStatement を用?、データベ?ス登録処?行います?
043     * 引数に、キーと値をセ?で配??で渡すことが?来?エラー時には?
044     * DBErrMsg オブジェクトにエラー??を?納して返すことが可能です?
045     * ?変数の受け渡し??ォルト実??、AbstractQuery クラスを継承して?
046     * ため,ここでは、execute() メソ?を実?て?す?
047     * こ?クラスでは、ス??トメント文?execute() する事により,??タベ?ス?
048     * 検索した結果?DBTableModel に割り当てます?
049     *
050     * @og.formSample
051     * 例?
052     *     第?数、第二引数は、結果(KEKKA)とエラーメ?ージ配?を返します?
053     *     キーエントリでは、エントリ(リクエスト情報)のキーと値をセ?で
054     *     引数の配?に設定します?
055     *     キーを?に、?を利用する場合に使用します?
056     *     下記?例?、動?ラ?実現して?PL/SQLの例です?
057     *
058     * <og:entryQuery
059     *     command    = "NEW"
060     *     queryType  = "JDBCKeyEntry" >
061     *         { call DYNAMIC_PKG.DYNAMIC_TEST( ?,?,?,? ) }
062     * </og:entryQuery>
063     *
064     *    CREATE OR REPLACE PACKAGE DYNAMIC_PKG AS
065     *        PROCEDURE DYNAMIC_TEST(
066     *            P_KEKKA       OUT    NUMBER,
067     *            P_ERRMSGS     OUT    ERR_MSG_ARRAY,
068     *            P_KEYS        IN     ARG_ARRAY,
069     *            P_ARGS        IN     ARG_ARRAY );
070     *    END;
071     *
072     * @og.group ??タ編?
073     *
074     * @version  4.0
075     * @author   Kazuhiko Hasegawa
076     * @since    JDK5.0,
077     */
078    public class Query_JDBCKeyEntry extends AbstractQuery {
079            //* こ?プログラ??VERSION??を設定します?       {@value} */
080            private static final String VERSION = "4.0.0.0 (2005/08/31)" ;
081    
082            /**
083             * 引数配?付?クエリーを実行します?
084             * 処??体?, #execute() と同様に、各サブクラスの実?依存します?
085             * これは、PreparedQuery で使用する引数を?列でセ?するも?です?
086             * select * from emp where deptno = ? and job = ? などの PreparedQuery の
087             * ? 部??引数?
088             * ?にセ?して?ます?
089             *
090             * @og.rev 3.1.1.0 (2003/03/28) 同期メソ?(synchronized付き)を非同期に変更する?
091             * @og.rev 3.3.3.1 (2003/07/18) ??登録時?後ろスペ?スを削除する?
092             * @og.rev 3.5.2.0 (2003/10/20) ?オブジェクトタイプ名?シス?パラメータ で定義します?
093             * @og.rev 3.5.6.0 (2004/06/18) nullに対する無?比?削除します?
094             * @og.rev 3.8.0.8 (2005/10/03) エラーメ?ージの出力?をメ?ージ?Queryに変更します?
095             *
096             * @param   keys オブジェクト?キー配?
097             * @param   args オブジェクト?引数配?
098             */
099            @Override
100            public void execute( final String[] keys, final String[] args ) {
101                    CallableStatement callStmt = null ;
102                    try {
103                            Connection conn = getConnection();
104                            callStmt  = getConnection().prepareCall( getStatement() );
105                            callStmt.setQueryTimeout( DB_MAX_QUERY_TIMEOUT );
106                            Map<String,Class<?>> map = conn.getTypeMap();
107                            map.put( ERR_MSG,DBErrMsg.class );      // 4.0.0 (2005/01/31)
108    
109                            ArrayDescriptor sd_key = ArrayDescriptor.createDescriptor( ARG_ARRAY, conn );
110                            ARRAY newArray_key = new ARRAY( sd_key,conn,keys );
111    
112                            ArrayDescriptor sd_val = ArrayDescriptor.createDescriptor( ARG_ARRAY, conn );
113                            ARRAY newArray_val = new ARRAY( sd_val,conn,StringUtil.rTrims( args ) );
114    
115                            callStmt.registerOutParameter(1, Types.INTEGER);
116                            callStmt.registerOutParameter(2, OracleTypes.ARRAY,ERR_MSG_ARRAY);
117                            ((OracleCallableStatement)callStmt).setARRAY( 3,newArray_key );
118                            ((OracleCallableStatement)callStmt).setARRAY( 4,newArray_val );
119    
120                            callStmt.execute();
121    
122                            int rtnCode = callStmt.getInt(1);
123                            setErrorCode( rtnCode );
124                            if( rtnCode > ErrorMessage.OK ) {            // 正常以外?場?
125                                    ARRAY rtn3 = ((OracleCallableStatement)callStmt).getARRAY(2);
126                                    Object[] rtnval3 = (Object[])rtn3.getArray();
127                                    ErrorMessage errMessage = new ErrorMessage( "Query_JDBCKeyEntry Error!!" );
128                                    for( int i=0; i<rtnval3.length; i++ ) {
129                                            DBErrMsg er = (DBErrMsg)rtnval3[i];
130                                            if( er == null ) { break; }
131                                            errMessage.addMessage( er.getErrMsg() );
132                                    }
133                                    setErrorMessage( errMessage );
134                            }
135                    }
136                    catch (SQLException ex) {
137                            setErrorCode( ErrorMessage.EXCEPTION );
138                            String errMsg = ex.getMessage() + ":" + ex.getSQLState() + HybsSystem.CR
139                                                    + getStatement() + HybsSystem.CR ;
140                            rollback();
141                            realClose();
142                            throw new HybsSystemException( errMsg,ex );             // 3.5.5.4 (2004/04/15) 引数の並び?更
143                    }
144                    finally {
145                            Closer.stmtClose( callStmt );
146                    }
147            }
148    }