com.hfg.sql.SQLUpdate 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;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import com.hfg.sql.jdbc.JDBCException;
import com.hfg.sql.table.DatabaseRow;
import com.hfg.sql.table.DatabaseTable;
import com.hfg.sql.table.field.DatabaseField;
import com.hfg.util.StringBuilderPlus;
import com.hfg.util.StringUtil;
import com.hfg.util.collection.CollectionUtil;
//------------------------------------------------------------------------------
/**
Object for doing SQL updates.
@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 SQLUpdate extends SQLCmd
{
private DatabaseTable mTable;
// Set clauses expressed in SQL
private List mSetClauses;
// Set clauses expressed as fields
private Collection mFieldList;
//###########################################################################
// CONSTRUCTORS
//###########################################################################
//---------------------------------------------------------------------------
public SQLUpdate()
{
}
//---------------------------------------------------------------------------
public SQLUpdate(DatabaseRow inRow)
{
setTable(inRow.getDatabaseTable());
// We only want to update dirty fields
for (DatabaseField field : inRow.getFields())
{
if (field.getCol().isId())
{
addClause(new WhereClause(field.getCol().name() + " = " + field.getSQLValue()));
}
else if (field.isDirty())
{
addSetField(field);
}
}
}
//###########################################################################
// PUBLIC METHODS
//###########################################################################
//---------------------------------------------------------------------------
public SQLUpdate setTable(DatabaseTable inValue)
{
mTable = inValue;
return this;
}
//---------------------------------------------------------------------------
public SQLUpdate addSetField(DatabaseField inValue)
{
if (null == mFieldList)
{
mFieldList = new ArrayList<>(50);
}
mFieldList.add(inValue.setIsDirty(true));
return this;
}
//---------------------------------------------------------------------------
public boolean hasSetFields()
{
return CollectionUtil.hasValues(mFieldList);
}
//---------------------------------------------------------------------------
public Collection getSetFields()
{
return mFieldList;
}
//---------------------------------------------------------------------------
public SQLUpdate setFields(Collection inValue)
{
if (CollectionUtil.hasValues(inValue))
{
for (DatabaseField field : inValue)
{
addSetField(field);
}
}
return this;
}
//---------------------------------------------------------------------------
public SQLUpdate addSetClause(CharSequence inClauseText)
{
return addClause(new SQLClause(SQLClauseType.SET, inClauseText));
}
//---------------------------------------------------------------------------
@Override
public SQLUpdate addClause(SQLClause inValue)
{
if (inValue.getType().equals(SQLClauseType.SET))
{
if (null == mSetClauses)
{
mSetClauses = new ArrayList<>(2);
}
mSetClauses.add(inValue);
}
else
{
super.addClause(inValue);
}
return this;
}
//---------------------------------------------------------------------------
public SQLUpdate addWhereClause(String inValue)
{
super.addClause(new WhereClause(inValue));
return this;
}
//---------------------------------------------------------------------------
public SQLUpdate addWhereClause(DatabaseField inValue)
{
super.addClause(new WhereClause(inValue));
return this;
}
//---------------------------------------------------------------------------
public SQLUpdate addWhereClauseGroup(WhereClauseGroup inValue)
{
return (SQLUpdate) super.addWhereClauseGroup(inValue);
}
//---------------------------------------------------------------------------
public int execute(Connection inConn)
throws SQLException
{
int result = SQLUtil.executeUpdate(inConn, toSQL());
if (result > 0
&& mFieldList != null)
{
// Since the fields have been updated, clear the dirty flags.
for (DatabaseField field : mFieldList)
{
field.setIsDirty(false);
}
}
return result;
}
//---------------------------------------------------------------------------
public String toSQL()
{
if (null == mTable)
{
throw new JDBCException("No table has been specified for update!");
}
else if (null == mFieldList
&& ! CollectionUtil.hasValues(mSetClauses))
{
throw new JDBCException("No fields have been specified for update!");
}
StringBuilderPlus sql = new StringBuilderPlus("UPDATE ");
sql.appendln(mTable.getQualifiedName() + (StringUtil.isSet(mTable.getAlias()) ? " " + mTable.getAlias() : ""));
StringBuilderPlus setClauses = new StringBuilderPlus().setDelimiter(", ");
if (CollectionUtil.hasValues(mSetClauses))
{
for (SQLClause clause : mSetClauses)
{
setClauses.delimitedAppend(clause.toSQL());
}
}
if (CollectionUtil.hasValues(mFieldList))
{
for (DatabaseField field : mFieldList)
{
if (field.isDirty())
{
setClauses.delimitedAppend(field.getCol().name() + " = " + field.getSQLValue());
}
}
}
sql.appendln("SET " + setClauses.toString());
sql.appendln(generateWhereClause());
return sql.toString();
}
//---------------------------------------------------------------------------
@Override
public String toString()
{
return toSQL();
}
}