All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hfg.sql.table.field.DatabaseArrayField Maven / Gradle / Ivy

There is a newer version: 20240423
Show newest version
package com.hfg.sql.table.field;

import java.sql.Array;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;

import com.hfg.sql.jdbc.JDBCException;
import com.hfg.sql.table.DatabaseCol;
import com.hfg.sql.table.DatabaseTable;
import com.hfg.xml.XMLTag;

//------------------------------------------------------------------------------
/**
 Database field that manages an array value.
 
@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 DatabaseArrayField extends DatabaseField { //########################################################################### // CONSTRUCTORS //########################################################################### //--------------------------------------------------------------------------- public DatabaseArrayField(DatabaseCol inCol, Array inValue) { super(inCol, inValue); } //--------------------------------------------------------------------------- public DatabaseArrayField(DatabaseCol inCol, Object inValue) { super(inCol, convertToArray(inValue)); } //--------------------------------------------------------------------------- public DatabaseArrayField(XMLTag inXMLTag, DatabaseTable inTable) { super(inXMLTag, inTable); } //########################################################################### // PUBLIC METHODS //########################################################################### //--------------------------------------------------------------------------- public void setValueFromResultSet(ResultSet inResultSet) { try { Array arrayValue = inResultSet.getArray(getCol().name()); // Convert the Array returned from to database into a SQLArray before setting the value setValue(inResultSet.wasNull() ? null : new SQLArray(arrayValue)); } catch (SQLException e) { try { throw new JDBCException("Problem mapping " + getCol().name() + " value: " + inResultSet.getString(getCol().name()) + "!", e); } catch (SQLException e2) { throw new JDBCException("Problem mapping " + getCol().name() + " value!", e); } } } //--------------------------------------------------------------------------- public void setValueInPreparedStatement(PreparedStatement inPreparedStatement, int inIndex) { try { if (isNull()) { inPreparedStatement.setNull(inIndex, Types.ARRAY); } else { inPreparedStatement.setArray(inIndex, getValue()); } } catch (SQLException e) { throw new JDBCException("Problem setting column " + getCol().name() + " value into PreparedStatement!", e); } } //--------------------------------------------------------------------------- public void setValueFromString(String inValue) { setValue(convertToArray(inValue)); } //--------------------------------------------------------------------------- public void setValue(int[] inValue) { super.setValue(new SQLArray(inValue)); } //--------------------------------------------------------------------------- public void setValue(String[] inValue) { super.setValue(new SQLArray(inValue)); } //--------------------------------------------------------------------------- @Override public String getSQLValue() { return getValue() != null ? "'" + toString() + "'" : "NULL"; } //########################################################################### // PRIVATE METHODS //########################################################################### //--------------------------------------------------------------------------- private static Array convertToArray(Object inValue) { Array arrayValue = null; if (inValue != null) { if (inValue instanceof int[]) { arrayValue = new SQLArray((int[]) inValue); } else if (inValue instanceof String[]) { arrayValue = new SQLArray((String[]) inValue); } else { throw new RuntimeException(inValue.getClass().getSimpleName() + " is not a currently supported array type!"); } } return arrayValue; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy