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