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 long 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 DatabaseLongField extends DatabaseField<Long> 043{ 044 045 //########################################################################### 046 // CONSTRUCTORS 047 //########################################################################### 048 049 //--------------------------------------------------------------------------- 050 public DatabaseLongField(DatabaseCol inCol) 051 { 052 this(inCol, (Object) null); 053 } 054 055 //--------------------------------------------------------------------------- 056 public DatabaseLongField(DatabaseCol inCol, Long inValue) 057 { 058 super(inCol, inValue); 059 } 060 061 //--------------------------------------------------------------------------- 062 public DatabaseLongField(DatabaseCol inCol, Object inValue) 063 { 064 super(inCol, convertToLong(inValue)); 065 } 066 067 //--------------------------------------------------------------------------- 068 public DatabaseLongField(DatabaseCol inCol, ResultSet inResultSet) 069 { 070 super(inCol, inResultSet); 071 } 072 073 //--------------------------------------------------------------------------- 074 public DatabaseLongField(XMLTag inXMLTag, DatabaseTable inTable) 075 { 076 super(inXMLTag, inTable); 077 } 078 079 //########################################################################### 080 // PUBLIC METHODS 081 //########################################################################### 082 083 //--------------------------------------------------------------------------- 084 protected void setValueFromResultSet(ResultSet inResultSet) 085 { 086 // Retrieve the index for the ResultSet column with the matching name. 087 // If no column with the proper name is present in the ResultSet, don't do anything. 088 Integer index = getColIndex(inResultSet); 089 if (index != null) 090 { 091 try 092 { 093 long value = inResultSet.getLong(index); 094 setInitialValue(0 == value && inResultSet.wasNull() ? null : value); 095 } 096 catch (SQLException e) 097 { 098 try 099 { 100 throw new JDBCException("Problem mapping " + getCol().name() + " value: " + inResultSet.getString(getCol().name()) + "!", e); 101 } 102 catch (SQLException e2) 103 { 104 throw new JDBCException("Problem mapping " + getCol().name() + " value!", e); 105 } 106 } 107 } 108 } 109/* 110 //--------------------------------------------------------------------------- 111 protected void setValueFromResultSet(ResultSet inResultSet, int inColIndex) 112 { 113 try 114 { 115 Long value = inResultSet.getLong(inColIndex); 116 setInitialValue(inResultSet.wasNull() ? null : value); 117 } 118 catch (SQLException e) 119 { 120 try 121 { 122 throw new JDBCException("Problem mapping " + getCol().name() + " value: " + inResultSet.getString(getCol().name()) + "!", e); 123 } 124 catch (SQLException e2) 125 { 126 throw new JDBCException("Problem mapping " + getCol().name() + " value!", e); 127 } 128 } 129 } 130*/ 131 //--------------------------------------------------------------------------- 132 public void setValueInPreparedStatement(PreparedStatement inPreparedStatement, int inIndex) 133 { 134 try 135 { 136 if (isNull()) 137 { 138 inPreparedStatement.setNull(inIndex, Types.BIGINT); 139 } 140 else 141 { 142 inPreparedStatement.setLong(inIndex, getValue()); 143 } 144 } 145 catch (SQLException e) 146 { 147 throw new JDBCException("Problem setting column " + getCol().name() + " value into PreparedStatement!", e); 148 } 149 } 150 151 //--------------------------------------------------------------------------- 152 public void setValueFromString(String inValue) 153 { 154 setValue(convertToLong(inValue)); 155 } 156 157 //########################################################################### 158 // PRIVATE METHODS 159 //########################################################################### 160 161 //--------------------------------------------------------------------------- 162 private static Long convertToLong(Object inValue) 163 { 164 Long longValue = null; 165 if (inValue != null) 166 { 167 if (inValue instanceof Long) 168 { 169 longValue = (Long) inValue; 170 } 171 else 172 { 173 try 174 { 175 longValue = Long.parseLong(inValue.toString()); 176 } 177 catch (Exception e) 178 { 179 // Ignore 180 } 181 } 182 } 183 184 return longValue; 185 } 186}