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