    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.getColumnName(1) ;
    120                        String series = metaData.getColumnLabel(1).toUpperCase( Locale.JAPAN );
    121                        while (resultSet.next()) {
    122                                // first column contains the row key...
    123                                String category   = resultSet.getString(1);
    124                                Object objVal     = resultSet.getObject(columnCount);
    125                                int    columnType = metaData.getColumnType(columnCount);
    126
    127                                if( columnCount > 2 ) { series = resultSet.getString(2); }
    128
    129                                Number value = null;
    130                                switch (columnType) {
    131                                        case Types.TINYINT:
    132                                        case Types.SMALLINT:
    133                                        case Types.INTEGER:
    134                                        case Types.BIGINT:
    135                                        case Types.FLOAT:
    136                                        case Types.DOUBLE:
    137                                        case Types.DECIMAL:
    138                                        case Types.NUMERIC:
    139                                        case Types.REAL: {
    140                                                value = (Number)objVal;
    141                                                break;
    142                                        }
    143                                        case Types.DATE:
    144                                        case Types.TIME:
    145                                        case Types.TIMESTAMP: {
    146                                                Date date = (Date) objVal;
    147                                                value = Long.valueOf(date.getTime());
    148                                                break;
    149                                        }
    150                                        case Types.CHAR:
    151                                        case Types.VARCHAR:
    152                                        case Types.LONGVARCHAR: {
    153                                                String string = (String)objVal;
    154                                                try {
    155                                                        value = Double.valueOf(string);
    156                                                }
    157                                                catch (NumberFormatException ex) {
    158                                                        LogWriter.log( ex );
    159                                                        // suppress (value defaults to null)
    160                                                }
    161                                                break;
    162                                        }
    163                                        default:
    164                                                // not a value, can't use it (defaults to null)
    165                                                break;
    166                                }
    167                                setValue(value, series, category);
    168                        }
    169                }
    170                finally {
    171                        Closer.resultClose( resultSet ) ;
    172                        Closer.stmtClose( statement ) ;
    173                }
    174        }
    175
    176}