com.hfg.sql.JoinClause 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.util.ArrayList;
import java.util.List;
import com.hfg.sql.table.DatabaseTable;
import com.hfg.util.StringBuilderPlus;
import com.hfg.util.StringUtil;
import com.hfg.util.collection.CollectionUtil;
//------------------------------------------------------------------------------
/**
Container for a SQL JOIN clause.
@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 JoinClause extends SQLClause
{
private SQLJoinType mJoinType = SQLJoinType.JOIN;
private List mOnClauses;
//###########################################################################
// CONSTRUCTORS
//###########################################################################
//---------------------------------------------------------------------------
public JoinClause(String inValue)
{
super(SQLClauseType.JOIN, inValue);
}
//---------------------------------------------------------------------------
public JoinClause(DatabaseTable inTable)
{
super(SQLClauseType.JOIN, inTable.getQualifiedName() + (StringUtil.isSet(inTable.getAlias()) ? " " + inTable.getAlias() : ""));
}
//---------------------------------------------------------------------------
public JoinClause(SQLJoinType inType, String inValue)
{
super(SQLClauseType.JOIN, inValue);
mJoinType = inType;
}
//---------------------------------------------------------------------------
public JoinClause(SQLJoinType inType, DatabaseTable inTable)
{
super(SQLClauseType.JOIN, inTable.getQualifiedName() + (StringUtil.isSet(inTable.getAlias()) ? " " + inTable.getAlias() : ""));
mJoinType = inType;
}
//---------------------------------------------------------------------------
public JoinClause(SQLJoinType inType, DatabaseTable inTable, String inAlias)
{
super(SQLClauseType.JOIN, inTable.getQualifiedName() + (StringUtil.isSet(inAlias) ? " " + inAlias : ""));
mJoinType = inType;
}
//---------------------------------------------------------------------------
public JoinClause addOnClause(String inValue)
{
if (null == mOnClauses)
{
mOnClauses = new ArrayList<>(2);
}
mOnClauses.add(inValue);
return this;
}
//---------------------------------------------------------------------------
public List getOnClauses()
{
return mOnClauses;
}
//---------------------------------------------------------------------------
public SQLJoinType getJoinType()
{
return mJoinType;
}
//---------------------------------------------------------------------------
@Override
public String toSQL()
{
StringBuilderPlus sql = null;
if (CollectionUtil.hasValues(getOnClauses()))
{
sql = new StringBuilderPlus().appendln();
for (String onClause : getOnClauses())
{
sql.appendln((sql.length() > 1 ? " AND " : " ON ") + onClause);
}
}
return super.toSQL() + (sql != null ? sql : "");
}
}