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    }