001package com.hfg.sql.table.field;
002
003import java.sql.Array;
004import java.sql.ResultSet;
005import java.sql.SQLException;
006import java.sql.SQLFeatureNotSupportedException;
007import java.sql.Types;
008import java.util.Arrays;
009import java.util.Map;
010
011import com.hfg.sql.SQLUtil;
012import com.hfg.util.StringBuilderPlus;
013
014//------------------------------------------------------------------------------
015/**
016 Simple implementation of the Array interface that handles int[] or String[].
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 SQLArray implements Array
043{
044   private int mBaseType;
045
046   private int[]    mIntArray;
047   private String[] mStringArray;
048
049   //###########################################################################
050   // CONSTRUCTORS
051   //###########################################################################
052
053   //---------------------------------------------------------------------------
054   public SQLArray(Array inArray)
055   {
056     initFromArray(inArray);
057   }
058
059   //---------------------------------------------------------------------------
060   public SQLArray(int[] inIntArray)
061   {
062      mBaseType = Types.INTEGER;
063      mIntArray = inIntArray;
064   }
065
066   //---------------------------------------------------------------------------
067   public SQLArray(String[] inStringArray)
068   {
069      mBaseType = Types.VARCHAR;
070      mStringArray = inStringArray;
071   }
072
073   //###########################################################################
074   // PUBLIC METHODS
075   //###########################################################################
076
077   //---------------------------------------------------------------------------
078   @Override
079   public String toString()
080   {
081      String stringValue;
082      try
083      {
084         stringValue = toSQLString();
085      }
086      catch (SQLException e)
087      {
088         throw new RuntimeException(e);
089      }
090
091      return stringValue;
092   }
093
094   //---------------------------------------------------------------------------
095   public String toSQLString()
096      throws SQLException
097   {
098      StringBuilderPlus buffer = new StringBuilderPlus().setDelimiter(",");
099      if (isNull())
100      {
101         buffer.append("NULL");
102      }
103      else
104      {
105         switch (mBaseType)
106         {
107            case Types.INTEGER:
108               for (int value : mIntArray)
109               {
110                  buffer.delimitedAppend(value);
111               }
112               break;
113            case Types.VARCHAR:
114               for (String value : mStringArray)
115               {
116                  buffer.delimitedAppend(SQLUtil.sqlStringWithDoubleQuotes(value));
117               }
118               break;
119            default:
120               throw new SQLException("Unsupported base type: " + mBaseType + "!");
121         }
122
123         buffer.insert(0, "{");
124         buffer.append("}");
125      }
126
127      return buffer.toString();
128   }
129
130
131   //---------------------------------------------------------------------------
132   public boolean isNull()
133         throws SQLException
134   {
135      boolean isNull;
136      switch (mBaseType)
137      {
138         case Types.INTEGER:
139            isNull = (null == mIntArray);
140            break;
141         case Types.VARCHAR:
142            isNull = (null == mStringArray);
143            break;
144         default:
145            throw new SQLException("Unsupported base type: " + mBaseType + "!");
146      }
147
148      return isNull;
149   }
150
151   //---------------------------------------------------------------------------
152   public int getBaseType()
153         throws SQLException
154   {
155      return mBaseType;
156   }
157
158   //---------------------------------------------------------------------------
159   public String getBaseTypeName()
160         throws SQLException
161   {
162      String name;
163      switch (mBaseType)
164      {
165         case Types.INTEGER:
166            name = "int4";
167            break;
168         case Types.VARCHAR:
169            name = "varchar";
170            break;
171         default:
172            throw new SQLException("Unsupported base type: " + mBaseType + "!");
173      }
174
175      return name;
176   }
177
178
179   //---------------------------------------------------------------------------
180   public Object getArray()
181         throws SQLException
182   {
183      Object arrayObj = null;
184
185      if (! isNull())
186      {
187         switch (mBaseType)
188         {
189            case Types.INTEGER:
190               arrayObj = Arrays.copyOf(mIntArray, mIntArray.length);
191               break;
192            case Types.VARCHAR:
193               arrayObj = Arrays.copyOf(mStringArray, mStringArray.length);
194               break;
195            default:
196               throw new SQLException("Unsupported base type: " + mBaseType + "!");
197         }
198      }
199
200      return arrayObj;
201   }
202
203   //---------------------------------------------------------------------------
204   public Object getArray(Map<String, Class<?>> inMap)
205         throws SQLException
206   {
207      return getArray();
208   }
209
210   //---------------------------------------------------------------------------
211   public Object getArray(long inIndex, int inCount)
212         throws SQLException
213   {
214      Object arrayObj = null;
215
216      if (! isNull())
217      {
218         switch (mBaseType)
219         {
220            case Types.INTEGER:
221               arrayObj = Arrays.copyOfRange(mIntArray, (int) inIndex, (int) inIndex + inCount);
222               break;
223            case Types.VARCHAR:
224               arrayObj = Arrays.copyOfRange(mStringArray, (int) inIndex, (int) inIndex + inCount);
225               break;
226            default:
227               throw new SQLException("Unsupported base type: " + mBaseType + "!");
228         }
229      }
230
231      return arrayObj;
232   }
233
234   //---------------------------------------------------------------------------
235   public Object getArray(long inIndex, int inCount, Map<String, Class<?>> inMap)
236         throws SQLException
237   {
238      return getArray(inIndex, inCount);
239   }
240
241   //---------------------------------------------------------------------------
242   public ResultSet getResultSet()
243         throws SQLException
244   {
245      throw new SQLFeatureNotSupportedException();
246   }
247
248   //---------------------------------------------------------------------------
249   public ResultSet getResultSet(Map<String, Class<?>> map)
250         throws SQLException
251   {
252      throw new SQLFeatureNotSupportedException();
253   }
254
255   //---------------------------------------------------------------------------
256   public ResultSet getResultSet(long index, int count)
257         throws SQLException
258   {
259      throw new SQLFeatureNotSupportedException();
260   }
261
262   //---------------------------------------------------------------------------
263   public ResultSet getResultSet(long index, int count, Map<String, Class<?>> map)
264         throws SQLException
265   {
266      throw new SQLFeatureNotSupportedException();
267   }
268
269   public void free()
270         throws SQLException
271   {
272      mIntArray = null;
273   }
274
275   //---------------------------------------------------------------------------
276   private void initFromArray(Array inArray)
277   {
278      try
279      {
280         mBaseType = inArray.getBaseType();
281
282         switch (inArray.getBaseType())
283         {
284            case Types.INTEGER:
285               // Unfortunately this comes back as an Integer[].
286               Integer[] objArray = (Integer[]) inArray.getArray();
287               mIntArray = new int[objArray.length];
288               int index = 0;
289               for (Integer value : objArray)
290               {
291                  mIntArray[index++] = value;
292               }
293               break;
294            case Types.VARCHAR:
295               mStringArray = (String[]) inArray.getArray();
296               break;
297            default:
298               throw new SQLException("Unsupported base type: " + mBaseType + "!");
299         }
300      }
301      catch (SQLException e)
302      {
303         throw new RuntimeException(e);
304      }
305   }
306}