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 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;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
public class SQLUpdate extends SQLCmd
{
private DatabaseTable mTable;
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 SQLUpdate setFields(Collection inValue)
{
if (CollectionUtil.hasValues(inValue))
{
for (DatabaseField field : inValue)
{
addSetField(field);
}
}
return this;
}
//---------------------------------------------------------------------------
@Override
public SQLUpdate addClause(SQLClause inValue)
{
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 boolean execute(Connection inConn)
throws SQLException
{
boolean result = SQLUtil.execute(inConn, toSQL());
if (result
&& 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)
{
throw new JDBCException("No fields has been specified for update!");
}
StringBuilderPlus sql = new StringBuilderPlus("UPDATE ");
sql.appendln(mTable.getQualifiedName() + (StringUtil.isSet(mTable.getAlias()) ? " " + mTable.getAlias() : ""));
StringBuilderPlus setClauses = new StringBuilderPlus().setDelimiter(", ");
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();
}
}