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.fukurou.util.Closer;
019    import org.opengion.fukurou.util.ErrorMessage;
020    import org.opengion.hayabusa.db.AbstractQuery;
021    import org.opengion.hayabusa.db.DBSysArg;
022    import org.opengion.hayabusa.db.DBUserArg;
023    import org.opengion.hayabusa.db.DBErrMsg;
024    import org.opengion.hayabusa.common.HybsSystem;
025    import org.opengion.hayabusa.common.HybsSystemException;
026    
027    import oracle.jdbc.OracleTypes;
028    import oracle.jdbc.OracleCallableStatement;
029    
030    import oracle.sql.ARRAY;
031    import oracle.sql.ArrayDescriptor;
032    
033    import java.sql.Types;
034    import java.sql.Connection;
035    import java.sql.CallableStatement;
036    import java.sql.SQLException;
037    
038    import java.util.Map;
039    
040    /**
041     * PL/SQL をコールする 登録系 Queryクラスですã?
042     *
043     * java.sql.CallableStatement を用ã�?�¦、データベã?ス検索処ç�?‚’行いますã?
044     * 引数に、SYSARG_ARRAYと、ユーザーARG_ARRAY をé?列指定で渡すことがå?来??
045     * エラー時には、DBErrMsg オブジェクトにエラーæƒ??をæ?納して返すことが可能ですã?
046     * å†?ƒ¨変数の受け渡しã?ãƒ?ƒ•ォルト実è£??、AbstractQuery クラスを継承してã�?‚‹
047     * ため,ここでは、execute() メソãƒ?ƒ‰を実è£?�—てã�?�¾すã?
048     * こã?クラスでは、スãƒ??トメント文ã‚?execute() する事により,ãƒ??タベã?スã‚?
049     * 検索した結果ã‚?DBTableModel に割り当てますã?
050     *
051     * @og.formSample
052     * 例:jsp/TYPE1B/result.jsp
053     *     names には、GEA08ARG で定義したカラãƒ?‚’æŒ?®šしますã?
054     *     呼び出ã�?PL/SQL では、登録系PL/SQL ですã?
055     *
056     * <og:plsqlUpdate
057     *     command    = "{@command}"
058     *     names      = "SYSTEM_ID,LANG,CLM,NAME_JA,LABEL_NAME,KBSAKU,FGJ,USRSET"
059     *     dbType     = "GEA08ARG"
060     *     queryType  = "JDBCPLSQL" >
061     *            { call TYPE1B01.TYPE1B01( ?,?,?,?,? ) }
062     * </og:plsqlUpdate>
063     *
064     *      PROCEDURE TYPE1B01 (
065     *         P_KEKKA     OUT   NUMBER,          -- エラー結果(0:正常 1:警å‘?2:異常)
066     *         P_ERRMSGS   OUT   ERR_MSG_ARRAY,   -- エラーのあるときã?エラーメãƒ?‚»ージ配å?
067     *         P_NAMES     IN    VARCHAR2,
068     *         P_SYSARGS   IN    SYSARG_ARRAY,    -- 引数 SYSTEMãƒ??タ
069     *         P_GE08ARGS  IN    GEA08ARG_ARRAY   -- 引数 USERãƒ??タ
070     *  );
071     *
072     * @og.group ãƒ??タ表示
073     * @og.group ãƒ??タ編é›?
074     *
075     * @version  4.0
076     * @author   Kazuhiko Hasegawa
077     * @since    JDK5.0,
078     */
079    public class Query_JDBCPLSQL extends AbstractQuery {
080            //* こã?プログラãƒ??VERSIONæ–?­—å?を設定しますã?       {@value} */
081            private static final String VERSION = "4.0.0.0 (2005/08/31)" ;
082    
083            /**
084             * 引数配å?付ã?クエリーを実行しますã?
085             * 処ç�??体ã?, #execute() と同様に、各サブクラスの実è£?�«依存しますã?
086             * これは、PreparedQuery で使用する引数をé?列でセãƒ?ƒˆするもã?ですã?
087             * select * from emp where deptno = ? and job = ? などの PreparedQuery の
088             * ? 部åˆ??引数ã‚?
089             * é ?•ªにセãƒ?ƒˆしてã�?��ますã?
090             *
091             * @og.rev 3.1.1.0 (2003/03/28) 同期メソãƒ?ƒ‰(synchronized付き)を非同期に変更するã€?
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             * @og.rev 4.0.0.0 (2005/01/31) 引数をすべて受け取って実行するメソãƒ?ƒ‰を標準メソãƒ?ƒ‰として追åŠ?
096             *
097             * @param       names           カラãƒ?��(CSV形å¼?
098             * @param       dbArrayType     アレイタイプ名称
099             * @param       sysArg          DBSysArg配å?
100             * @param       userArg         DBUserArg配å?
101             */
102            @Override
103            public void execute( final String names,final String dbArrayType,
104                                                            final DBSysArg[] sysArg,final DBUserArg[] userArg ) {
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 );      // 4.0.0 (2005/01/31)
112    
113                            ArrayDescriptor sd2 = ArrayDescriptor.createDescriptor( SYSARG_ARRAY, conn );
114                            ARRAY newArray2 = new ARRAY( sd2,conn,sysArg );
115    
116                            ArrayDescriptor sd = ArrayDescriptor.createDescriptor( dbArrayType, conn );
117                            ARRAY newArray = new ARRAY( sd,conn,userArg );
118    
119                            callStmt.registerOutParameter(1, Types.INTEGER);
120                            callStmt.registerOutParameter(2, OracleTypes.ARRAY,ERR_MSG_ARRAY);
121                            callStmt.setString( 3,names );
122                            ((OracleCallableStatement)callStmt).setARRAY( 4,newArray2 );
123                            ((OracleCallableStatement)callStmt).setARRAY( 5,newArray );
124    
125                            callStmt.execute();
126    
127                            int rtnCode = callStmt.getInt(1);
128                            setErrorCode( rtnCode );
129                            if( rtnCode > ErrorMessage.OK ) {            // 正常以外ã?場å�?
130                                    ARRAY rtn3 = ((OracleCallableStatement)callStmt).getARRAY(2);
131                                    Object[] rtnval3 = (Object[])rtn3.getArray();
132                                    ErrorMessage errMessage = new ErrorMessage( "Query_JDBCPLSQL Error!!" );
133                                    for( int i=0; i<rtnval3.length; i++ ) {
134                                            DBErrMsg er = (DBErrMsg)rtnval3[i];
135                                            if( er == null ) { break; }
136                                            errMessage.addMessage( er.getErrMsg() );
137                                    }
138                                    setErrorMessage( errMessage );
139                            }
140                    }
141                    catch (SQLException ex) {
142                            setErrorCode( ErrorMessage.EXCEPTION );
143                            String errMsg = ex.getMessage() + ":" + ex.getSQLState() + HybsSystem.CR
144                                                    + getStatement() + HybsSystem.CR;
145                            rollback();
146                            realClose();
147                            throw new HybsSystemException( errMsg,ex );             // 3.5.5.4 (2004/04/15) 引数の並びé ?¤‰更
148                    }
149                    finally {
150                            Closer.stmtClose( callStmt );
151                    }
152            }
153    }