001package com.hfg.sql.table.field; 002 003import com.hfg.sql.jdbc.JDBCException; 004import com.hfg.sql.table.DatabaseCol; 005import com.hfg.sql.table.DatabaseTable; 006import com.hfg.xml.XMLTag; 007 008import java.sql.PreparedStatement; 009import java.sql.ResultSet; 010import java.sql.SQLException; 011import java.sql.Types; 012 013//------------------------------------------------------------------------------ 014/** 015 Database field that manages a double value. 016 <div> 017 @author J. Alex Taylor, hairyfatguy.com 018 </div> 019 */ 020//------------------------------------------------------------------------------ 021// com.hfg XML/HTML Coding Library 022// 023// This library is free software; you can redistribute it and/or 024// modify it under the terms of the GNU Lesser General Public 025// License as published by the Free Software Foundation; either 026// version 2.1 of the License, or (at your option) any later version. 027// 028// This library is distributed in the hope that it will be useful, 029// but WITHOUT ANY WARRANTY; without even the implied warranty of 030// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 031// Lesser General Public License for more details. 032// 033// You should have received a copy of the GNU Lesser General Public 034// License along with this library; if not, write to the Free Software 035// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 036// 037// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com 038// jataylor@hairyfatguy.com 039//------------------------------------------------------------------------------ 040 041 042public class DatabaseDoubleField extends DatabaseField<Double> 043{ 044 //########################################################################### 045 // CONSTRUCTORS 046 //########################################################################### 047 048 //--------------------------------------------------------------------------- 049 public DatabaseDoubleField(DatabaseCol inCol) 050 { 051 this(inCol, (Object) null); 052 } 053 054 //--------------------------------------------------------------------------- 055 public DatabaseDoubleField(DatabaseCol inCol, Double inValue) 056 { 057 super(inCol, inValue); 058 } 059 060 //--------------------------------------------------------------------------- 061 public DatabaseDoubleField(DatabaseCol inCol, Object inValue) 062 { 063 super(inCol, convertToDouble(inValue)); 064 } 065 066 //--------------------------------------------------------------------------- 067 public DatabaseDoubleField(DatabaseCol inCol, ResultSet inResultSet) 068 { 069 super(inCol, inResultSet); 070 } 071 072 //--------------------------------------------------------------------------- 073 public DatabaseDoubleField(XMLTag inXMLTag, DatabaseTable inTable) 074 { 075 super(inXMLTag, inTable); 076 } 077 078 //########################################################################### 079 // PUBLIC METHODS 080 //########################################################################### 081 082 //--------------------------------------------------------------------------- 083 protected void setValueFromResultSet(ResultSet inResultSet) 084 { 085 // Retrieve the index for the ResultSet column with the matching name. 086 // If no column with the proper name is present in the ResultSet, don't do anything. 087 Integer index = getColIndex(inResultSet); 088 if (index != null) 089 { 090 try 091 { 092 double value = inResultSet.getDouble(index); 093 setInitialValue(0 == value && inResultSet.wasNull() ? null : value); 094 } 095 catch (SQLException e) 096 { 097 try 098 { 099 throw new JDBCException("Problem mapping " + getCol().name() + " value: " + inResultSet.getString(getCol().name()) + "!", e); 100 } 101 catch (SQLException e2) 102 { 103 throw new JDBCException("Problem mapping " + getCol().name() + " value!", e); 104 } 105 } 106 } 107 } 108 109 //--------------------------------------------------------------------------- 110 public void setValueInPreparedStatement(PreparedStatement inPreparedStatement, int inIndex) 111 { 112 try 113 { 114 if (isNull()) 115 { 116 inPreparedStatement.setNull(inIndex, Types.DOUBLE); 117 } 118 else 119 { 120 inPreparedStatement.setDouble(inIndex, getValue()); 121 } 122 } 123 catch (SQLException e) 124 { 125 throw new JDBCException("Problem setting column " + getCol().name() + " value into PreparedStatement!", e); 126 } 127 } 128 129 //--------------------------------------------------------------------------- 130 public void setValueFromString(String inValue) 131 { 132 setValue(convertToDouble(inValue)); 133 } 134 135 //########################################################################### 136 // PRIVATE METHODS 137 //########################################################################### 138 139 //--------------------------------------------------------------------------- 140 private static Double convertToDouble(Object inValue) 141 { 142 Double doubleValue = null; 143 if (inValue != null) 144 { 145 if (inValue instanceof Float) 146 { 147 doubleValue = (Double) inValue; 148 } 149 else 150 { 151 try 152 { 153 doubleValue = Double.parseDouble(inValue.toString()); 154 } 155 catch (Exception e) 156 { 157 // Ignore 158 } 159 } 160 } 161 162 return doubleValue; 163 } 164}