001package com.hfg.sql.table.field;
002
003import com.hfg.sql.SQLUtil;
004import com.hfg.sql.jdbc.JDBCException;
005import com.hfg.sql.table.DatabaseCol;
006import com.hfg.sql.table.DatabaseTable;
007import com.hfg.xml.XMLTag;
008
009import java.sql.PreparedStatement;
010import java.sql.ResultSet;
011import java.sql.SQLException;
012import java.sql.Types;
013
014//------------------------------------------------------------------------------
015/**
016 Database field that manages a string 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 DatabaseStringField extends DatabaseField<String>
043{
044   //###########################################################################
045   // CONSTRUCTORS
046   //###########################################################################
047
048   //---------------------------------------------------------------------------
049   public DatabaseStringField(DatabaseCol inCol)
050   {
051      this(inCol, (Object) null);
052   }
053
054   //---------------------------------------------------------------------------
055   public DatabaseStringField(DatabaseCol inCol, String inValue)
056   {
057      super(inCol, inValue);
058   }
059
060   //---------------------------------------------------------------------------
061   public DatabaseStringField(DatabaseCol inCol, Object inValue)
062   {
063      super(inCol, convertToString(inValue));
064   }
065
066   //---------------------------------------------------------------------------
067   public DatabaseStringField(DatabaseCol inCol, ResultSet inResultSet)
068   {
069      super(inCol, inResultSet);
070   }
071
072   //---------------------------------------------------------------------------
073   public DatabaseStringField(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            setInitialValue(inResultSet.getString(index));
093         }
094         catch (SQLException e)
095         {
096            try
097            {
098               throw new JDBCException("Problem mapping " + getCol().name() + " value: " + inResultSet.getString(getCol().name()) + "!", e);
099            }
100            catch (SQLException e2)
101            {
102               throw new JDBCException("Problem mapping " + getCol().name() + " value!", e);
103            }
104         }
105      }
106   }
107
108   //---------------------------------------------------------------------------
109   public void setValueInPreparedStatement(PreparedStatement inPreparedStatement, int inIndex)
110   {
111      try
112      {
113         if (isNull())
114         {
115            inPreparedStatement.setNull(inIndex, Types.VARCHAR);
116         }
117         else
118         {
119            inPreparedStatement.setString(inIndex, getValue());
120         }
121      }
122      catch (SQLException e)
123      {
124         throw new JDBCException("Problem setting column " + getCol().name() + " value into PreparedStatement!", e);
125      }
126   }
127
128   //---------------------------------------------------------------------------
129   public void setValueFromString(String inValue)
130   {
131      setValue(inValue);
132   }
133
134   //---------------------------------------------------------------------------
135   @Override
136   public String getSQLValue()
137   {
138      return SQLUtil.sqlString(getValue());
139   }
140
141   //###########################################################################
142   // PRIVATE METHODS
143   //###########################################################################
144
145   //---------------------------------------------------------------------------
146   private static String convertToString(Object inValue)
147   {
148      String stringValue = null;
149      if (inValue != null)
150      {
151         if (inValue instanceof String)
152         {
153            stringValue = (String) inValue;
154         }
155         else
156         {
157            stringValue = inValue.toString();
158         }
159      }
160
161      return stringValue;
162   }
163}