    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.hayabusa.io;
    017
    018import java.sql.Connection;
    019import java.sql.Date;
    020import java.sql.ResultSet;
    021import java.sql.ResultSetMetaData;
    022import java.sql.SQLException;
    023import java.sql.Statement;
    024import java.sql.Types;
    025import java.util.Locale;
    026
    027import org.opengion.fukurou.util.Closer;
    028import org.opengion.fukurou.util.LogWriter;
    029
    030import org.jfree.data.jdbc.JDBCCategoryDataset;
    031
    032/**
    033 * HybsJDBCCategoryDataset は、org.jfree.data.jdbc.JDBCCategoryDataset を継承したサブクラスで、
    034 * executeQuery(Connection , String )  をオーバーライドしています。
    035 * これは、元のソースが、series と category の扱いが異なる為で、QUERY には、
    036 * select series,values from ･･･ または、select series,category,values from ･･･
    037 * の形式で検索することを想定した作りに修正しています。
    038 * series の縦持ち 対応です。
    039 * 参考:JFreeChart : a free chart library for the Java(tm) platform(jfreechart-1.0.6)
    040 *
    041 * @og.rev 3.8.9.2 (2007/07/28) 新規作成
    042 *
    043 * @version  0.9.0  2001/05/05
    044 * @author   Kazuhiko Hasegawa
    045 * @since    JDK1.1,
    046 */
    047public class HybsJDBCCategoryDataset extends JDBCCategoryDataset {
    048        private static final long serialVersionUID = 518020100701L ;
    049
    050        /**
    051         * Creates a new dataset with the given database connection, and executes
    052         * the supplied query to populate the dataset.
    053         *
    054         * @param connection  the connection.
    055         * @param query  the query.
    056         *
    057         * @throws SQLException if there is a problem executing the query.
    058         */
    059        public HybsJDBCCategoryDataset( final Connection connection, final String query ) throws SQLException {
    060                super( connection );
    061                innerQuery( connection,query );
    062        }
    063
    064        /**
    065         * Populates the dataset by executing the supplied query against the
    066         * existing database connection.  If no connection exists then no action
    067         * is taken.
    068         *
    069         * The results from the query are extracted and cached locally, thus
    070         * applying an upper limit on how many rows can be retrieved successfully.
    071         *
    072         * @param con  the connection.
    073         * @param query  the query.
    074         *
    075         * @throws SQLException if there is a problem executing the query.
    076         * @see org.jfree.data.jdbc.JDBCCategoryDataset#executeQuery(Connection , String )
    077         */
    078        @Override
    079        public void executeQuery( final Connection con, final String query ) throws SQLException {
    080                innerQuery( con,query );
    081        }
    082
    083        /**
    084         * Populates the dataset by executing the supplied query against the
    085         * existing database connection.  If no connection exists then no action
    086         * is taken.
    087         *
    088         * The results from the query are extracted and cached locally, thus
    089         * applying an upper limit on how many rows can be retrieved successfully.
    090         *
    091         * @og.rev 4.0.0.0 (2007/11/30) public な executeQuery メソッドを private 化します。
    092         * @og.rev 5.1.8.0 (2010/07/01) column名は大文字化し、項目名の取得は#getColumnLabel()で行う。(PotgreSQL対応&バグ修正)
    093         *
    094         * @param con  the connection.
    095         * @param query  the query.
    096         *
    097         * @throws SQLException if there is a problem executing the query.
    098         * @see org.jfree.data.jdbc.JDBCCategoryDataset#executeQuery(Connection , String )
    099         */
    100        private void innerQuery( final Connection con, final String query ) throws SQLException {
    101
    102                Statement statement = null;
    103                ResultSet resultSet = null;
    104                try {
    105                        statement = con.createStatement();
    106                        resultSet = statement.executeQuery(query);
    107                        ResultSetMetaData metaData = resultSet.getMetaData();
    108
    109                        int columnCount = metaData.getColumnCount();
    110
    111                        if(columnCount < 2) {
    112                                String errMsg = "JDBCCategoryDataset.executeQuery() : insufficient columns "
    113                                                        + "returned from the database. \n"
    114                                                        + " SQL=" + query ;
    115                                throw new SQLException( errMsg );
    116                        }
    117
    118                        // 5.1.8.0 (2010/07/01) column名は大文字化し、項目名の取得は#getColumnLabel()で行う。(PotgreSQL対応&バグ修正)
    119                        String series = metaData.getColumnLabel(1).toUpperCase( Locale.JAPAN );
    120                        while (resultSet.next()) {
    121                                // first column contains the row key...
    122                                String category   = resultSet.getString(1);
    123                                Object objVal     = resultSet.getObject(columnCount);
    124                                int    columnType = metaData.getColumnType(columnCount);
    125
    126                                if( columnCount > 2 ) { series = resultSet.getString(2); }
    127
    128                                Number value = null;
    129                                switch (columnType) {
    130                                        case Types.TINYINT:
    131                                        case Types.SMALLINT:
    132                                        case Types.INTEGER:
    133                                        case Types.BIGINT:
    134                                        case Types.FLOAT:
    135                                        case Types.DOUBLE:
    136                                        case Types.DECIMAL:
    137                                        case Types.NUMERIC:
    138                                        case Types.REAL: {
    139                                                value = (Number)objVal;
    140                                                break;
    141                                        }
    142                                        case Types.DATE:
    143                                        case Types.TIME:
    144                                        case Types.TIMESTAMP: {
    145                                                Date date = (Date) objVal;
    146                                                value = Long.valueOf(date.getTime());
    147                                                break;
    148                                        }
    149                                        case Types.CHAR:
    150                                        case Types.VARCHAR:
    151                                        case Types.LONGVARCHAR: {
    152                                                String string = (String)objVal;
    153                                                try {
    154                                                        value = Double.valueOf(string);
    155                                                }
    156                                                catch (NumberFormatException ex) {
    157                                                        LogWriter.log( ex );
    158                                                        // suppress (value defaults to null)
    159                                                }
    160                                                break;
    161                                        }
    162                                        default:
    163                                                // not a value, can't use it (defaults to null)
    164                                                break;
    165                                }
    166                                setValue(value, series, category);
    167                        }
    168                }
    169                finally {
    170                        Closer.resultClose( resultSet ) ;
    171                        Closer.stmtClose( statement ) ;
    172                }
    173        }
    174
    175}