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 java.sql.Connection;
028    import java.sql.CallableStatement;
029    import java.sql.SQLException;
030    import java.sql.Types;
031    import java.sql.Array;                                                          // 5.7.2.2 (2014/01/24) Oracle11g(11.2.0.3のドライ? 対応?oracle.sql.ARRAY の置き換?
032    // import oracle.sql.ARRAY;                                                     // 5.7.2.2 (2014/01/24) Oracle11g(11.2.0.3のドライ? 対?
033    // import oracle.sql.ArrayDescriptor;                           // 5.7.2.2 (2014/01/24) Oracle11g(11.2.0.3のドライ? 対?
034    import oracle.jdbc.OracleConnection;                            // 5.7.2.2 (2014/01/24) Oracle11g(11.2.0.3のドライ? 対?
035    
036    // import oracle.jdbc.OracleTypes;                                      // 5.7.2.2 (2014/01/24) Oracle11g(11.2.0.3のドライ? 対応?oracle.sql.Types の置き換?
037    // import oracle.jdbc.OracleCallableStatement;          // 5.7.2.2 (2014/01/24) Oracle11g(11.2.0.3のドライ? 対?
038    
039    import java.util.Map;
040    
041    /**
042     * PL/SQL をコールする 登録系 Queryクラスです?
043     *
044     * java.sql.CallableStatement を用?、データベ?ス検索処?行います?
045     * 引数に、SYSARG_ARRAYと、ユーザーARG_ARRAY を?列指定で渡すことが?来??
046     * エラー時には、DBErrMsg オブジェクトにエラー??を?納して返すことが可能です?
047     * ?変数の受け渡し??ォルト実??、AbstractQuery クラスを継承して?
048     * ため,ここでは、execute() メソ?を実?て?す?
049     * こ?クラスでは、ス??トメント文?execute() する事により,??タベ?ス?
050     * 検索した結果?DBTableModel に割り当てます?
051     *
052     * @og.formSample
053     * 例:jsp/TYPE1B/result.jsp
054     *     names には、GEA08ARG で定義したカラ??します?
055     *     呼び出?PL/SQL では、登録系PL/SQL です?
056     *
057     * <og:plsqlUpdate
058     *     command    = "{@command}"
059     *     names      = "SYSTEM_ID,LANG,CLM,NAME_JA,LABEL_NAME,KBSAKU,FGJ,USRSET"
060     *     dbType     = "GEA08ARG"
061     *     queryType  = "JDBCPLSQL" >
062     *            { call TYPE1B01.TYPE1B01( ?,?,?,?,? ) }
063     * </og:plsqlUpdate>
064     *
065     *      PROCEDURE TYPE1B01 (
066     *         P_KEKKA     OUT   NUMBER,          -- エラー結果(0:正常 1:警?2:異常)
067     *         P_ERRMSGS   OUT   ERR_MSG_ARRAY,   -- エラーのあるとき?エラーメ?ージ配?
068     *         P_NAMES     IN    VARCHAR2,
069     *         P_SYSARGS   IN    SYSARG_ARRAY,    -- 引数 SYSTEM??タ
070     *         P_GE08ARGS  IN    GEA08ARG_ARRAY   -- 引数 USER??タ
071     *  );
072     *
073     * @og.group ??タ表示
074     * @og.group ??タ編?
075     *
076     * @version  4.0
077     * @author   Kazuhiko Hasegawa
078     * @since    JDK5.0,
079     */
080    public class Query_JDBCPLSQL extends AbstractQuery {
081            //* こ?プログラ??VERSION??を設定します?       {@value} */
082            private static final String VERSION = "5.7.2.2 (2014/01/24)" ;
083    
084            /**
085             * 引数配?付?クエリーを実行します?
086             * 処??体?, #execute() と同様に、各サブクラスの実?依存します?
087             * これは、PreparedQuery で使用する引数を?列でセ?するも?です?
088             * select * from emp where deptno = ? and job = ? などの PreparedQuery の
089             * ? 部??引数?
090             * ?にセ?して?ます?
091             *
092             * @og.rev 3.1.1.0 (2003/03/28) 同期メソ?(synchronized付き)を非同期に変更する?
093             * @og.rev 3.5.2.0 (2003/10/20) ?オブジェクトタイプ名?シス?パラメータ で定義します?
094             * @og.rev 3.5.6.0 (2004/06/18) nullに対する無?比?削除します?
095             * @og.rev 3.8.0.8 (2005/10/03) エラーメ?ージの出力?をメ?ージ?Queryに変更します?
096             * @og.rev 4.0.0.0 (2005/01/31) 引数をすべて受け取って実行するメソ?を標準メソ?として追?
097             * @og.rev 5.7.2.2 (2014/01/24) Oracle11g(11.2.0.3のドライ?対?
098             *
099             * @param       names           カラ?(CSV形?
100             * @param       dbArrayType     アレイタイプ名称
101             * @param       sysArg          DBSysArg配?
102             * @param       userArg         DBUserArg配?
103             */
104            @Override
105            public void execute( final String names,final String dbArrayType,
106                                                            final DBSysArg[] sysArg,final DBUserArg[] userArg ) {
107                    CallableStatement callStmt = null ;
108                    try {
109                            Connection conn = getConnection();
110                            callStmt  = getConnection().prepareCall( getStatement() );
111                            callStmt.setQueryTimeout( DB_MAX_QUERY_TIMEOUT );
112                            Map<String,Class<?>> map = conn.getTypeMap();
113                            map.put( ERR_MSG,DBErrMsg.class );      // 4.0.0 (2005/01/31)
114    
115                            // 5.7.2.2 (2014/01/24) Oracle11g(11.2.0.3のドライ?対?http://docs.oracle.com/cd/E28389_01/web.1111/b60995/thirdparty.htm
116    //                      ArrayDescriptor sd2 = ArrayDescriptor.createDescriptor( SYSARG_ARRAY, conn );
117    //                      ARRAY newArray2 = new ARRAY( sd2,conn,sysArg );
118                            Array newArray2 = ((OracleConnection)conn).createOracleArray( SYSARG_ARRAY, sysArg );                   // 5.7.2.2 (2014/01/24) Oracle11g(11.2.0.3のドライ?対?
119    
120                            // 5.7.2.2 (2014/01/24) Oracle11g(11.2.0.3のドライ?対?http://docs.oracle.com/cd/E28389_01/web.1111/b60995/thirdparty.htm
121    //                      ArrayDescriptor sd = ArrayDescriptor.createDescriptor( dbArrayType, conn );
122    //                      ARRAY newArray = new ARRAY( sd,conn,userArg );
123                            Array newArray = ((OracleConnection)conn).createOracleArray( dbArrayType, userArg );                    // 5.7.2.2 (2014/01/24) Oracle11g(11.2.0.3のドライ?対?
124    
125                            callStmt.registerOutParameter(1, Types.INTEGER);
126    //                      callStmt.registerOutParameter(2, OracleTypes.ARRAY,ERR_MSG_ARRAY);      // 5.7.2.2 (2014/01/24) Oracle11g(11.2.0.3のドライ?対?
127                            callStmt.registerOutParameter(2, Types.ARRAY,ERR_MSG_ARRAY);            // 5.7.2.2 (2014/01/24) Oracle11g(11.2.0.3のドライ?対?
128                            callStmt.setString( 3,names );
129    //                      ((OracleCallableStatement)callStmt).setARRAY( 4,newArray2 );            // 5.7.2.2 (2014/01/24) Oracle11g(11.2.0.3のドライ?対?
130    //                      ((OracleCallableStatement)callStmt).setARRAY( 5,newArray );                     // 5.7.2.2 (2014/01/24) Oracle11g(11.2.0.3のドライ?対?
131                            callStmt.setArray( 4,newArray2 );                                                                       // 5.7.2.2 (2014/01/24) Oracle11g(11.2.0.3のドライ?対?
132                            callStmt.setArray( 5,newArray );                                                                        // 5.7.2.2 (2014/01/24) Oracle11g(11.2.0.3のドライ?対?
133    
134                            callStmt.execute();
135    
136                            int rtnCode = callStmt.getInt(1);
137                            setErrorCode( rtnCode );
138                            if( rtnCode > ErrorMessage.OK ) {            // 正常以外?場?
139    //                              ARRAY rtn3 = ((OracleCallableStatement)callStmt).getARRAY(2);   // 5.7.2.2 (2014/01/24) Oracle11g(11.2.0.3のドライ?対?
140                                    Array rtn3 = callStmt.getArray(2);                                                              // 5.7.2.2 (2014/01/24) Oracle11g(11.2.0.3のドライ?対?
141                                    Object[] rtnval3 = (Object[])rtn3.getArray();
142                                    ErrorMessage errMessage = new ErrorMessage( "Query_JDBCPLSQL Error!!" );
143                                    for( int i=0; i<rtnval3.length; i++ ) {
144                                            DBErrMsg er = (DBErrMsg)rtnval3[i];
145                                            if( er == null ) { break; }
146                                            errMessage.addMessage( er.getErrMsg() );
147                                    }
148                                    setErrorMessage( errMessage );
149                            }
150                    }
151                    catch (SQLException ex) {
152                            setErrorCode( ErrorMessage.EXCEPTION );
153                            String errMsg = ex.getMessage() + ":" + ex.getSQLState() + HybsSystem.CR
154                                                    + getStatement() + HybsSystem.CR;
155                            rollback();
156                            realClose();
157                            throw new HybsSystemException( errMsg,ex );             // 3.5.5.4 (2004/04/15) 引数の並び?更
158                    }
159                    finally {
160                            Closer.stmtClose( callStmt );
161                    }
162            }
163    }