001package com.hfg.sql; 002 003import com.hfg.sql.jdbc.JDBCException; 004import com.hfg.util.StringBuilderPlus; 005import com.hfg.util.collection.CollectionUtil; 006 007import java.util.ArrayList; 008import java.util.Collection; 009import java.util.List; 010 011//------------------------------------------------------------------------------ 012/** 013 Base class for SQL commands. 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 039 040public abstract class SQLCmd 041{ 042 private SQLAndGroup mWhere; 043 private List<SQLClause> mOrderByList; 044 045 046 //########################################################################### 047 // PUBLIC METHODS 048 //########################################################################### 049 050 //--------------------------------------------------------------------------- 051 public SQLCmd addClauses(Collection<SQLClause> inClauses) 052 { 053 if (CollectionUtil.hasValues(inClauses)) 054 { 055 for (SQLClause clause : inClauses) 056 { 057 addClause(clause); 058 } 059 } 060 061 return this; 062 } 063 064 //--------------------------------------------------------------------------- 065 public SQLCmd addClause(SQLClause inValue) 066 { 067 switch (inValue.getType()) 068 { 069 case WHERE: 070 if (null == mWhere) 071 { 072 mWhere = new SQLAndGroup(); 073 } 074 mWhere.add((WhereClause)inValue); 075 break; 076 077 case ORDER_BY: 078 if (null == mOrderByList) 079 { 080 mOrderByList = new ArrayList<>(5); 081 } 082 mOrderByList.add(inValue); 083 break; 084 085 default: 086 throw new JDBCException("Unrecognized SQL clause type!"); 087 } 088 089 return this; 090 } 091 092 //--------------------------------------------------------------------------- 093 public SQLCmd addWhereClauseGroup(WhereClauseGroup inValue) 094 { 095 if (null == mWhere) 096 { 097 mWhere = new SQLAndGroup(); 098 } 099 100 mWhere.add(inValue); 101 102 return this; 103 } 104 105 //--------------------------------------------------------------------------- 106 protected String generateWhereClause() 107 { 108 StringBuilderPlus sql = new StringBuilderPlus().setDelimiter("\n AND "); 109 110 if (mWhere != null) 111 { 112 sql.append(" WHERE "); 113 sql.append(mWhere.toSQL()); 114 sql.appendln(); 115 } 116 117 return sql.toString(); 118 } 119 120 //--------------------------------------------------------------------------- 121 protected String generateOrderByClause() 122 { 123 StringBuilderPlus sql = new StringBuilderPlus().setDelimiter(", "); 124 if (mOrderByList != null) 125 { 126 for (SQLClause clause : mOrderByList) 127 { 128 sql.delimitedAppend(clause.toSQL()); 129 } 130 131 sql.insert(0, "ORDER BY "); 132 sql.appendln(); 133 } 134 135 return sql.toString(); 136 } 137}