com.hfg.sql.SQLInsert 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.table.DatabaseTable;
import com.hfg.sql.table.field.DatabaseField;
import com.hfg.util.StringBuilderPlus;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
//------------------------------------------------------------------------------
/**
Container for building a SQL insert statement.
@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 SQLInsert extends SQLCmd
{
private DatabaseTable mTable;
private Collection mFieldList;
//###########################################################################
// PUBLIC METHODS
//###########################################################################
//---------------------------------------------------------------------------
public boolean execute(Connection inConn)
throws SQLException
{
return SQLUtil.execute(inConn, toSQL());
}
//---------------------------------------------------------------------------
public String toSQL()
{
StringBuilderPlus sql = new StringBuilderPlus("INSERT INTO ");
sql.appendln(mTable.getQualifiedName());
sql.append(" (");
StringBuilderPlus colNames = new StringBuilderPlus().setDelimiter(", ");
for (DatabaseField field : mFieldList)
{
if (! field.isNull()
|| (field.getCol().isId()
&& field.getCol().getSequence() != null))
{
colNames.delimitedAppend(field.getCol().name());
}
}
sql.append(colNames.toString());
sql.appendln(")");
sql.append(" VALUES (");
StringBuilderPlus values = new StringBuilderPlus().setDelimiter(", ");
for (DatabaseField field : mFieldList)
{
if (field.getCol().isId())
{
String value = null;
if (field.getCol().getSequence() != null)
{
value = field.getCol().getSequence().nextvalSQL();
}
else if (! field.isNull())
{
value = field.getSQLValue();
}
if (value != null)
{
values.delimitedAppend(value);
}
}
else if (! field.isNull())
{
values.delimitedAppend(field.getSQLValue());
}
}
sql.append(values.toString());
sql.append(")");
return sql.toString();
}
//---------------------------------------------------------------------------
@Override
public String toString()
{
return toSQL();
}
//---------------------------------------------------------------------------
public SQLInsert setTable(DatabaseTable inValue)
{
mTable = inValue;
return this;
}
//---------------------------------------------------------------------------
public SQLInsert setFields(Collection inValue)
{
mFieldList = inValue;
return this;
}
}