com.hfg.sql.table.field.DatabaseCharField Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of com_hfg Show documentation
Show all versions of com_hfg Show documentation
com.hfg xml, html, svg, and bioinformatics utility library
package com.hfg.sql.table.field;
//------------------------------------------------------------------------------
import com.hfg.sql.SQLUtil;
import com.hfg.sql.jdbc.JDBCException;
import com.hfg.sql.table.DatabaseCol;
import com.hfg.sql.table.DatabaseTable;
import com.hfg.util.StringUtil;
import com.hfg.xml.XMLTag;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
/**
Database field that manages a string value.
@author J. Alex Taylor, hairyfatguy.com
*/
//------------------------------------------------------------------------------
// com.hfg XML/HTML Coding Library
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
// [email protected]
//------------------------------------------------------------------------------
public class DatabaseCharField extends DatabaseField
{
//###########################################################################
// CONSTRUCTORS
//###########################################################################
//---------------------------------------------------------------------------
public DatabaseCharField(DatabaseCol inCol, Character inValue)
{
super(inCol, inValue);
}
//---------------------------------------------------------------------------
public DatabaseCharField(DatabaseCol inCol, Object inValue)
{
super(inCol, convertToChar(inValue));
}
//---------------------------------------------------------------------------
public DatabaseCharField(XMLTag inXMLTag, DatabaseTable inTable)
{
super(inXMLTag, inTable);
}
//###########################################################################
// PUBLIC METHODS
//###########################################################################
//---------------------------------------------------------------------------
public void setValueFromResultSet(ResultSet inResultSet)
{
try
{
String value = inResultSet.getString(getCol().name());
if (value != null
&& value.length() > 1)
{
throw new JDBCException("Value is longer than 1 character!");
}
setValue(inResultSet.wasNull() ? null : value.charAt(0));
}
catch (SQLException e)
{
try
{
throw new JDBCException("Problem mapping " + getCol().name() + " value: " + inResultSet.getString(getCol().name()) + "!", e);
}
catch (SQLException e2)
{
throw new JDBCException("Problem mapping " + getCol().name() + " value!", e);
}
}
}
//---------------------------------------------------------------------------
public void setValueInPreparedStatement(PreparedStatement inPreparedStatement, int inIndex)
{
try
{
if (isNull())
{
inPreparedStatement.setNull(inIndex, Types.CHAR);
}
else
{
inPreparedStatement.setString(inIndex, getValue() + "");
}
}
catch (SQLException e)
{
throw new JDBCException("Problem setting column " + getCol().name() + " value into PreparedStatement!", e);
}
}
//---------------------------------------------------------------------------
public void setValueFromString(String inValue)
{
setValue(convertToChar(inValue));
}
//---------------------------------------------------------------------------
@Override
public String getSQLValue()
{
return SQLUtil.sqlString(getValue());
}
//###########################################################################
// PRIVATE METHODS
//###########################################################################
//---------------------------------------------------------------------------
private static Character convertToChar(Object inValue)
{
Character charValue = null;
if (inValue != null)
{
if (inValue instanceof Character)
{
charValue = (Character) inValue;
}
else
{
String stringValue = inValue.toString();
if (stringValue.length() > 1)
{
throw new JDBCException("Value " + StringUtil.singleQuote(stringValue) + " is longer than 1 character!");
}
charValue = stringValue.charAt(0);
}
}
return charValue;
}
}