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 */
016package org.opengion.plugin.query;
017
018import java.sql.ResultSet;
019import java.sql.SQLException;
020import java.sql.Statement;
021
022import org.opengion.fukurou.util.Closer;
023import org.opengion.fukurou.util.ErrorMessage;
024import org.opengion.hayabusa.common.HybsSystem;
025import org.opengion.hayabusa.common.HybsSystemException;
026import org.opengion.hayabusa.db.AbstractQuery;
027
028/**
029 * 指定のSQL文を実行して、検索する Query クラスです。
030 *
031 * java.sql.Statement を用いて、データベース検索処理を行います。
032 * 引数は無しです。(与えられたSQL文を実行します。)
033 * 内部変数の受け渡しのデフォルト実装は、AbstractQuery クラスを継承している
034 * ため,ここでは、execute() メソッドを実装しています。
035 * このクラスでは、ステートメント文を execute() する事により,データベースを
036 * 検索した結果を DBTableModel に割り当てます。
037 *
038 * @og.formSample
039 * <og:query command="{@command}" debug="false">
040 *     <!-- 先頭のカラム名が、"WRITABLE" の場合、'true' or '1' で、書き込み許可が与えら,'2' でチェック済みになります。-->
041 *         select KBSAKU AS WRITABLE,
042 *                 CLM,NAME_JA,LABEL_NAME,KBSAKU,SYSTEM_ID,LANG,FGJ
043 *         from GE41
044 *     <og:where>
045 *         <og:and value = "FGJ         in  ('0','1')"         />
046 *         <og:and value = "SYSTEM_ID   like '{@SYSTEM_ID}%'"  />
047 *         <og:and value = "LANG        like '{@LANG}%'"       />
048 *         <og:and value = "CLM         like '{@CLM}%'"        />
049 *         <og:and value = "KBSAKU      =    '{@KBSAKU}'"      />
050 *     </og:where>
051 *     <og:appear startKey = "order by" value = "{@ORDER_BY}"
052 *                 defaultVal = "SYSTEM_ID,CLM,LANG" />
053 * </og:query>
054 *
055 * @og.group データ表示
056 * @og.group データ編集
057 *
058 * @version  4.0
059 * @author   Kazuhiko Hasegawa
060 * @since    JDK5.0,
061 */
062public class Query_JDBC extends AbstractQuery {
063        //* このプログラムのVERSION文字列を設定します。   {@value} */
064        private static final String VERSION = "4.0.0.0 (2005/08/31)" ;
065
066        /**
067         * クエリーを実行します。
068         * セットされているステートメント文字列とそのタイプが合っていない場合は,
069         * エラーになります。
070         * 実行結果は、DBTableModel にセットされます。
071         *
072         * @og.rev 3.8.0.8 (2005/10/03) エラーメッセージの出力順をメッセージ+Queryに変更します。
073         *
074         */
075        @Override
076        public void execute() {
077                Statement stmt = null ;
078                ResultSet resultSet = null ;
079                try {
080                        stmt = getConnection().createStatement();
081                        stmt.setQueryTimeout( DB_MAX_QUERY_TIMEOUT );
082
083                        boolean status = stmt.execute( getStatement() );
084
085                        if( status ) {
086                                resultSet = stmt.getResultSet();
087                                createTableModel( resultSet );
088                                setUpdateFlag( false );
089                        }
090                        else {
091                                setExecuteCount( stmt.getUpdateCount() );
092                        }
093                        setErrorCode( ErrorMessage.OK );
094                }
095                catch ( SQLException ex ) {
096                        setErrorCode( ErrorMessage.EXCEPTION );
097                        String errMsg = ex.getMessage() + ":" + ex.getSQLState() + HybsSystem.CR
098                                                + getStatement() + HybsSystem.CR;
099                        rollback();
100                        realClose();
101                        throw new HybsSystemException( errMsg,ex );             // 3.5.5.4 (2004/04/15) 引数の並び順変更
102                }
103                finally {
104                        Closer.resultClose( resultSet );
105                        Closer.stmtClose( stmt );
106                }
107        }
108}