com.hfg.sql.table.field.DatabaseBinaryField 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 java.io.ByteArrayInputStream;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Base64;
import com.hfg.sql.jdbc.JDBCException;
import com.hfg.sql.table.DatabaseCol;
import com.hfg.sql.table.DatabaseTable;
import com.hfg.xml.XMLTag;
//------------------------------------------------------------------------------
/**
Database field that manages a binary 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 DatabaseBinaryField extends DatabaseField
{
//###########################################################################
// CONSTRUCTORS
//###########################################################################
//---------------------------------------------------------------------------
public DatabaseBinaryField(DatabaseCol inCol, byte[] inValue)
{
super(inCol, inValue);
}
//---------------------------------------------------------------------------
public DatabaseBinaryField(DatabaseCol inCol, Object inValue)
{
super(inCol, convertToByteArray(inValue));
}
//---------------------------------------------------------------------------
public DatabaseBinaryField(XMLTag inXMLTag, DatabaseTable inTable)
{
super(inXMLTag, inTable);
}
//###########################################################################
// PUBLIC METHODS
//###########################################################################
//---------------------------------------------------------------------------
public void setValueFromResultSet(ResultSet inResultSet)
{
try
{
setValue(inResultSet.wasNull() ? null : inResultSet.getBytes(getCol().name()));
}
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.BINARY);
}
else
{
ByteArrayInputStream stream = new ByteArrayInputStream(getValue());
inPreparedStatement.setBinaryStream(inIndex, stream, getValue().length);
}
}
catch (Exception e)
{
throw new JDBCException("Problem setting column " + getCol().name() + " value into PreparedStatement!", e);
}
}
//---------------------------------------------------------------------------
public void setValueFromString(String inValue)
{
setValue(convertToByteArray(inValue));
}
//---------------------------------------------------------------------------
@Override
public String getSQLValue()
{
return getValue() != null ? "'" + getValue() : "null";
}
//---------------------------------------------------------------------------
@Override
protected String getStringValue ()
{
// Return a base64-encoded version of the value
return (getValue() != null ? Base64.getEncoder().encodeToString(getValue()) : null);
}
//###########################################################################
// PRIVATE METHODS
//###########################################################################
//---------------------------------------------------------------------------
private static byte[] convertToByteArray(Object inValue)
{
byte[] value = null;
if (inValue != null)
{
if (inValue instanceof byte[])
{
value = (byte[]) inValue;
}
else if (inValue instanceof String)
{
// Assume strings are base64-encoded
value = Base64.getDecoder().decode((String)inValue);
}
else
{
value = inValue.toString().getBytes();
}
}
return value;
}
}