All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hfg.sql.WhereClauseGroup Maven / Gradle / Ivy

There is a newer version: 20240423
Show newest version
package com.hfg.sql;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.hfg.util.StringBuilderPlus;
import com.hfg.util.StringUtil;
import com.hfg.util.collection.CollectionUtil;

//------------------------------------------------------------------------------
/**
 Grouping of SQL where clauses - either AND or OR.
 
@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 abstract class WhereClauseGroup { public enum Type { AND(3), OR(4); private String mPadding; private Type(int inPadding) { mPadding = StringUtil.polyChar(' ', inPadding); } public String getPadding() { return mPadding; } } private Type mType; private List mWhereClauses; private static final Pattern CLAUSE_INTERIOR_REGEXP = Pattern.compile("([^\\)])\n\\s+"); //########################################################################### // CONSTRUCTORS //########################################################################### //--------------------------------------------------------------------------- protected WhereClauseGroup(Type inValue) { mType = inValue; } //########################################################################### // PUBLIC METHODS //########################################################################### //--------------------------------------------------------------------------- public Type getType() { return mType; } //--------------------------------------------------------------------------- public String toSQL() { StringBuilderPlus buffer = new StringBuilderPlus().setDelimiter("\n" + mType.getPadding() + mType + " "); if (CollectionUtil.hasValues(mWhereClauses)) { Type type = null; for (Object clause : mWhereClauses) { if (null == type) { if (clause instanceof WhereClauseGroup) { type = ((WhereClauseGroup)clause).getType(); } else { type = Type.AND; } } if (clause instanceof WhereClauseGroup && (! ((WhereClauseGroup)clause).getType().equals(type) || (mWhereClauses.size() > 1 && ! ((WhereClauseGroup)clause).getType().equals(getType())))) { // Remove any line returns we placed in the interior of a grouped clause int index = 0; StringBuilder clauseSql = new StringBuilder(((WhereClauseGroup) clause).toSQL()); Matcher m = CLAUSE_INTERIOR_REGEXP.matcher(clauseSql); while (m.find(index)) { clauseSql.replace(m.start() + 1, m.end(), " "); index = m.start() + 1; } buffer.delimitedAppend("(" + clauseSql + ")"); } else { buffer.delimitedAppend(clause); } } } return buffer.toString(); } //--------------------------------------------------------------------------- @Override public String toString() { return toSQL(); } //--------------------------------------------------------------------------- public WhereClauseGroup add(WhereClause inValue) { if (null == mWhereClauses) { mWhereClauses = new ArrayList<>(4); } mWhereClauses.add(inValue); return this; } //--------------------------------------------------------------------------- public WhereClauseGroup add(WhereClauseGroup inValue) { if (null == mWhereClauses) { mWhereClauses = new ArrayList<>(4); } mWhereClauses.add(inValue); return this; } //--------------------------------------------------------------------------- public WhereClauseGroup addAll(Collection inValues) { if (CollectionUtil.hasValues(inValues)) { if (null == mWhereClauses) { mWhereClauses = new ArrayList<>(inValues.size()); } mWhereClauses.addAll(inValues); } return this; } //--------------------------------------------------------------------------- public WhereClauseGroup addAllGroups(Collection inValues) { if (CollectionUtil.hasValues(inValues)) { if (null == mWhereClauses) { mWhereClauses = new ArrayList<>(inValues.size()); } mWhereClauses.addAll(inValues); } return this; } }