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