001package com.hfg.sql;
002
003import java.util.ArrayList;
004import java.util.List;
005
006import com.hfg.sql.table.DatabaseTable;
007import com.hfg.util.StringBuilderPlus;
008import com.hfg.util.StringUtil;
009import com.hfg.util.collection.CollectionUtil;
010
011//------------------------------------------------------------------------------
012/**
013 Container for a SQL JOIN clause.
014 <div>
015 @author J. Alex Taylor, hairyfatguy.com
016 </div>
017 */
018//------------------------------------------------------------------------------
019// com.hfg XML/HTML Coding Library
020//
021// This library is free software; you can redistribute it and/or
022// modify it under the terms of the GNU Lesser General Public
023// License as published by the Free Software Foundation; either
024// version 2.1 of the License, or (at your option) any later version.
025//
026// This library is distributed in the hope that it will be useful,
027// but WITHOUT ANY WARRANTY; without even the implied warranty of
028// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
029// Lesser General Public License for more details.
030//
031// You should have received a copy of the GNU Lesser General Public
032// License along with this library; if not, write to the Free Software
033// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
034//
035// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
036// jataylor@hairyfatguy.com
037//------------------------------------------------------------------------------
038
039public class JoinClause extends SQLClause
040{
041   private SQLJoinType  mJoinType = SQLJoinType.JOIN;
042   private List<String> mOnClauses;
043
044   //###########################################################################
045   // CONSTRUCTORS
046   //###########################################################################
047
048   //---------------------------------------------------------------------------
049   public JoinClause(String inValue)
050   {
051      super(SQLClauseType.JOIN, inValue);
052   }
053
054   //---------------------------------------------------------------------------
055   public JoinClause(DatabaseTable inTable)
056   {
057      super(SQLClauseType.JOIN, inTable.getQualifiedName() + (StringUtil.isSet(inTable.getAlias()) ? " " + inTable.getAlias() : ""));
058   }
059
060   //---------------------------------------------------------------------------
061   public JoinClause(SQLJoinType inType, String inValue)
062   {
063      super(SQLClauseType.JOIN, inValue);
064      mJoinType = inType;
065   }
066
067   //---------------------------------------------------------------------------
068   public JoinClause(SQLJoinType inType, DatabaseTable inTable)
069   {
070      super(SQLClauseType.JOIN, inTable.getQualifiedName() + (StringUtil.isSet(inTable.getAlias()) ? " " + inTable.getAlias() : ""));
071      mJoinType = inType;
072   }
073
074   //---------------------------------------------------------------------------
075   public JoinClause(SQLJoinType inType, DatabaseTable inTable, String inAlias)
076   {
077      super(SQLClauseType.JOIN, inTable.getQualifiedName() + (StringUtil.isSet(inAlias) ? " " + inAlias : ""));
078      mJoinType = inType;
079   }
080
081   //---------------------------------------------------------------------------
082   public JoinClause addOnClause(String inValue)
083   {
084      if (null == mOnClauses)
085      {
086         mOnClauses = new ArrayList<>(2);
087      }
088
089      mOnClauses.add(inValue);
090      return this;
091   }
092
093   //---------------------------------------------------------------------------
094   public List<String> getOnClauses()
095   {
096      return mOnClauses;
097   }
098
099   //---------------------------------------------------------------------------
100   public SQLJoinType getJoinType()
101   {
102      return mJoinType;
103   }
104
105   //---------------------------------------------------------------------------
106   @Override
107   public String toSQL()
108   {
109      StringBuilderPlus sql = null;
110      if (CollectionUtil.hasValues(getOnClauses()))
111      {
112         sql = new StringBuilderPlus().appendln();
113         for (String onClause : getOnClauses())
114         {
115            sql.appendln((sql.length() > 1 ? "   AND " : "    ON ") + onClause);
116         }
117      }
118
119      return super.toSQL() + (sql != null ? sql : "");
120   }
121}