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

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

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

import com.hfg.sql.jdbc.JDBCException;
import com.hfg.util.StringBuilderPlus;
import com.hfg.util.collection.CollectionUtil;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

//------------------------------------------------------------------------------
/**
 Base class for SQL commands.
 
@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 SQLCmd { private SQLAndGroup mWhere; private List mOrderByList; //########################################################################### // PUBLIC METHODS //########################################################################### //--------------------------------------------------------------------------- public SQLCmd addClauses(Collection inClauses) { if (CollectionUtil.hasValues(inClauses)) { for (SQLClause clause : inClauses) { addClause(clause); } } return this; } //--------------------------------------------------------------------------- public SQLCmd addClause(SQLClause inValue) { switch (inValue.getType()) { case WHERE: if (null == mWhere) { mWhere = new SQLAndGroup(); } mWhere.add((WhereClause)inValue); break; case ORDER_BY: if (null == mOrderByList) { mOrderByList = new ArrayList<>(5); } mOrderByList.add(inValue); break; default: throw new JDBCException("Unrecognized SQL clause type!"); } return this; } //--------------------------------------------------------------------------- public SQLCmd addWhereClauseGroup(WhereClauseGroup inValue) { if (null == mWhere) { mWhere = new SQLAndGroup(); } mWhere.add(inValue); return this; } //--------------------------------------------------------------------------- protected String generateWhereClause() { StringBuilderPlus sql = new StringBuilderPlus().setDelimiter("\n AND "); if (mWhere != null) { sql.append(" WHERE "); sql.append(mWhere.toSQL()); sql.appendln(); } return sql.toString(); } //--------------------------------------------------------------------------- protected String generateOrderByClause() { StringBuilderPlus sql = new StringBuilderPlus().setDelimiter(", "); if (mOrderByList != null) { for (SQLClause clause : mOrderByList) { sql.delimitedAppend(clause.toSQL()); } sql.insert(0, "ORDER BY "); sql.appendln(); } return sql.toString(); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy