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

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

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

import java.lang.reflect.Constructor;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.hfg.exception.ProgrammingException;
import com.hfg.sql.jdbc.JDBCException;
import com.hfg.sql.jdbc.JDBCResultSet;
import com.hfg.sql.table.DatabaseCol;
import com.hfg.sql.table.DatabaseTable;
import com.hfg.sql.table.DatabaseXML;
import com.hfg.util.StringBuilderPlus;
import com.hfg.xml.XMLCDATA;
import com.hfg.xml.XMLTag;
import com.hfg.xml.XMLizable;

//------------------------------------------------------------------------------
/**
 Base class for a database field.
 
@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 abstract class DatabaseField implements Cloneable { private DatabaseCol mCol; private T mValue; private boolean mIsDirty; /** Indicates that the XML content should be enclosed in a CDATA tag */ private boolean mUseCDATA; //########################################################################### // CONSTRUCTORS //########################################################################### //--------------------------------------------------------------------------- public DatabaseField(DatabaseCol inCol, T inValue) { mCol = inCol; mValue = inValue; } //--------------------------------------------------------------------------- public DatabaseField(DatabaseCol inCol, ResultSet inResultSet) { mCol = inCol; if (inResultSet != null) { setValueFromResultSet(inResultSet); } } //--------------------------------------------------------------------------- public DatabaseField(XMLTag inXMLTag, DatabaseTable inTable) { inXMLTag.verifyTagName(DatabaseXML.FIELD); mCol = inTable.getCol(inXMLTag.getAttributeValue(DatabaseXML.NAME_ATT)); if (! inXMLTag.hasAttribute(DatabaseXML.IS_NULL_ATT)) { if (inXMLTag.hasCDATA()) { StringBuilderPlus content = new StringBuilderPlus(); for (XMLizable subtag : inXMLTag.getSubtags()) { if (subtag instanceof XMLCDATA) { content.append(((XMLCDATA) subtag).getContent()); } } setValueFromString(content.toString()); } else { setValueFromString(inXMLTag.getUnescapedContent()); } mIsDirty = false; } } //########################################################################### // PUBLIC METHODS //########################################################################### //--------------------------------------------------------------------------- public static DatabaseField instantiate(XMLTag inXMLTag, DatabaseTable inTable) { DatabaseField field; try { Class clazz = Class.forName(inXMLTag.getAttributeValue(DatabaseXML.CLASS_ATT)); Constructor constructor = clazz.getConstructor(XMLTag.class, DatabaseTable.class); field = (DatabaseField) constructor.newInstance(inXMLTag, inTable); } catch (Exception e) { throw new JDBCException(e); } return field; } //--------------------------------------------------------------------------- @Override public String toString() { return isNull() ? "null" : getValue().toString(); } //--------------------------------------------------------------------------- public XMLTag toXMLTag() { XMLTag tag = new XMLTag(DatabaseXML.FIELD); tag.setAttribute(DatabaseXML.CLASS_ATT, getClass().getName()); tag.setAttribute(DatabaseXML.NAME_ATT, getCol().name()); if (isNull()) { tag.setAttribute(DatabaseXML.IS_NULL_ATT, true); } else if (mUseCDATA) { tag.addSubtag(new XMLCDATA(getStringValue())); } else { tag.setContent(getStringValue()); } return tag; } //--------------------------------------------------------------------------- protected String getStringValue() { return (getValue() != null ? getValue().toString() : null); } //--------------------------------------------------------------------------- @Override public DatabaseField clone() { DatabaseField cloneObj; try { cloneObj = (DatabaseField) super.clone(); } catch (CloneNotSupportedException e) { throw new ProgrammingException(e); } return cloneObj; } //--------------------------------------------------------------------------- public DatabaseCol getCol() { return mCol; } //--------------------------------------------------------------------------- public DatabaseField useCDATA(boolean inValue) { mUseCDATA = inValue; return this; } //--------------------------------------------------------------------------- public abstract void setValueInPreparedStatement(PreparedStatement inPreparedStatement, int inIndex); //--------------------------------------------------------------------------- public abstract void setValueFromString(String inValue); //--------------------------------------------------------------------------- public void setValue(T inValue) { if ((null == inValue && mValue != null) || (inValue != null && ! inValue.equals(mValue))) { mIsDirty = true; } mValue = inValue; } //--------------------------------------------------------------------------- public T getValue() { return mValue; } //--------------------------------------------------------------------------- public String getSQLValue() { return mValue != null ? mValue.toString() : "null"; } //--------------------------------------------------------------------------- public boolean isNull() { return null == mValue; } //--------------------------------------------------------------------------- public boolean isDirty() { return mIsDirty; } //--------------------------------------------------------------------------- public DatabaseField setIsDirty(boolean inValue) { mIsDirty = inValue; return this; } //########################################################################### // PROTECTED METHODS //########################################################################### //--------------------------------------------------------------------------- protected abstract void setValueFromResultSet(ResultSet inResultSet); //--------------------------------------------------------------------------- protected void setInitialValue(T inValue) { mValue = inValue; } //--------------------------------------------------------------------------- protected Integer getColIndex(ResultSet inResultSet) { Integer index = null; try { // If the ResultSet has been wrapped with a JDBCResultSet, // use the more performant version of findColumn(). if (inResultSet instanceof JDBCResultSet) { index = ((JDBCResultSet) inResultSet).findColumn(getCol()); } else { index = inResultSet.findColumn(getCol().name()); } } catch (SQLException e) { // Ignore } return index; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy