com.hfg.sql.table.field.SQLArray 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.sql.Array;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.sql.Types;
import java.util.Arrays;
import java.util.Map;
import com.hfg.sql.SQLUtil;
import com.hfg.util.StringBuilderPlus;
//------------------------------------------------------------------------------
/**
Simple implementation of the Array interface that handles int[] or String[].
@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 SQLArray implements Array
{
private int mBaseType;
private int[] mIntArray;
private String[] mStringArray;
//###########################################################################
// CONSTRUCTORS
//###########################################################################
//---------------------------------------------------------------------------
public SQLArray(Array inArray)
{
initFromArray(inArray);
}
//---------------------------------------------------------------------------
public SQLArray(int[] inIntArray)
{
mBaseType = Types.INTEGER;
mIntArray = inIntArray;
}
//---------------------------------------------------------------------------
public SQLArray(String[] inStringArray)
{
mBaseType = Types.VARCHAR;
mStringArray = inStringArray;
}
//###########################################################################
// PUBLIC METHODS
//###########################################################################
//---------------------------------------------------------------------------
@Override
public String toString()
{
String stringValue;
try
{
stringValue = toSQLString();
}
catch (SQLException e)
{
throw new RuntimeException(e);
}
return stringValue;
}
//---------------------------------------------------------------------------
public String toSQLString()
throws SQLException
{
StringBuilderPlus buffer = new StringBuilderPlus().setDelimiter(",");
if (isNull())
{
buffer.append("NULL");
}
else
{
switch (mBaseType)
{
case Types.INTEGER:
for (int value : mIntArray)
{
buffer.delimitedAppend(value);
}
break;
case Types.VARCHAR:
for (String value : mStringArray)
{
buffer.delimitedAppend(SQLUtil.sqlStringWithDoubleQuotes(value));
}
break;
default:
throw new SQLException("Unsupported base type: " + mBaseType + "!");
}
buffer.insert(0, "{");
buffer.append("}");
}
return buffer.toString();
}
//---------------------------------------------------------------------------
public boolean isNull()
throws SQLException
{
boolean isNull;
switch (mBaseType)
{
case Types.INTEGER:
isNull = (null == mIntArray);
break;
case Types.VARCHAR:
isNull = (null == mStringArray);
break;
default:
throw new SQLException("Unsupported base type: " + mBaseType + "!");
}
return isNull;
}
//---------------------------------------------------------------------------
public int getBaseType()
throws SQLException
{
return mBaseType;
}
//---------------------------------------------------------------------------
public String getBaseTypeName()
throws SQLException
{
String name;
switch (mBaseType)
{
case Types.INTEGER:
name = "int4";
break;
case Types.VARCHAR:
name = "varchar";
break;
default:
throw new SQLException("Unsupported base type: " + mBaseType + "!");
}
return name;
}
//---------------------------------------------------------------------------
public Object getArray()
throws SQLException
{
Object arrayObj = null;
if (! isNull())
{
switch (mBaseType)
{
case Types.INTEGER:
arrayObj = Arrays.copyOf(mIntArray, mIntArray.length);
break;
case Types.VARCHAR:
arrayObj = Arrays.copyOf(mStringArray, mStringArray.length);
break;
default:
throw new SQLException("Unsupported base type: " + mBaseType + "!");
}
}
return arrayObj;
}
//---------------------------------------------------------------------------
public Object getArray(Map> inMap)
throws SQLException
{
return getArray();
}
//---------------------------------------------------------------------------
public Object getArray(long inIndex, int inCount)
throws SQLException
{
Object arrayObj = null;
if (! isNull())
{
switch (mBaseType)
{
case Types.INTEGER:
arrayObj = Arrays.copyOfRange(mIntArray, (int) inIndex, (int) inIndex + inCount);
break;
case Types.VARCHAR:
arrayObj = Arrays.copyOfRange(mStringArray, (int) inIndex, (int) inIndex + inCount);
break;
default:
throw new SQLException("Unsupported base type: " + mBaseType + "!");
}
}
return arrayObj;
}
//---------------------------------------------------------------------------
public Object getArray(long inIndex, int inCount, Map> inMap)
throws SQLException
{
return getArray(inIndex, inCount);
}
//---------------------------------------------------------------------------
public ResultSet getResultSet()
throws SQLException
{
throw new SQLFeatureNotSupportedException();
}
//---------------------------------------------------------------------------
public ResultSet getResultSet(Map> map)
throws SQLException
{
throw new SQLFeatureNotSupportedException();
}
//---------------------------------------------------------------------------
public ResultSet getResultSet(long index, int count)
throws SQLException
{
throw new SQLFeatureNotSupportedException();
}
//---------------------------------------------------------------------------
public ResultSet getResultSet(long index, int count, Map> map)
throws SQLException
{
throw new SQLFeatureNotSupportedException();
}
public void free()
throws SQLException
{
mIntArray = null;
}
//---------------------------------------------------------------------------
private void initFromArray(Array inArray)
{
try
{
mBaseType = inArray.getBaseType();
switch (inArray.getBaseType())
{
case Types.INTEGER:
// Unfortunately this comes back as an Integer[].
Integer[] objArray = (Integer[]) inArray.getArray();
mIntArray = new int[objArray.length];
int index = 0;
for (Integer value : objArray)
{
mIntArray[index++] = value;
}
break;
case Types.VARCHAR:
mStringArray = (String[]) inArray.getArray();
break;
default:
throw new SQLException("Unsupported base type: " + mBaseType + "!");
}
}
catch (SQLException e)
{
throw new RuntimeException(e);
}
}
}