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.hayabusa.io; 017 018 import java.sql.Connection; 019 import java.sql.Date; 020 import java.sql.ResultSet; 021 import java.sql.ResultSetMetaData; 022 import java.sql.SQLException; 023 import java.sql.Statement; 024 import java.sql.Types; 025 import java.util.Locale; 026 027 import org.opengion.fukurou.util.Closer; 028 import org.opengion.fukurou.util.LogWriter; 029 030 import 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 */ 047 public 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 }