net.sf.jasperreports.engine.query.JRSqlAbstractEqualClause Maven / Gradle / Ivy
/*
* JasperReports - Free Java Reporting Library.
* Copyright (C) 2001 - 2013 Jaspersoft Corporation. All rights reserved.
* http://www.jaspersoft.com
*
* Unless you have purchased a commercial license agreement from Jaspersoft,
* the following license terms apply:
*
* This program is part of JasperReports.
*
* JasperReports 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 3 of the License, or
* (at your option) any later version.
*
* JasperReports 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 JasperReports. If not, see .
*/
package net.sf.jasperreports.engine.query;
import net.sf.jasperreports.engine.JRRuntimeException;
/**
* Base (NOT) EQUAL clause function for SQL queries.
*
* The first token in the $X{...} syntax is the function ID token. Possible values for
* the (NOT) EQUAL clause function ID token are:
*
* - EQUAL
* - NOTEQUAL
*
*
*
* @author sanda zaharia ([email protected])
* @version $Id: JRSqlAbstractEqualClause.java 5878 2013-01-07 20:23:13Z teodord $
*/
public abstract class JRSqlAbstractEqualClause implements JRClauseFunction
{
public static final int POSITION_DB_COLUMN = 1;
public static final int POSITION_PARAMETER = 2;
protected JRSqlAbstractEqualClause()
{
}
/**
* Creates a (NOT) EQUAL SQL clause.
*
*
* The method expects two clause tokens (after the ID token):
*
* - The first token is the SQL column (or column combination) to be used in the clause.
* - The second token is the name of the report parameter that contains the value to compare to.
*
*
*
* The EQUAL function constructs either a column = ?
or an
* column IS NULL
clause, depending on the parameter's value.
*
*
* The NOTEQUAL function constructs either a column <> ?
or an
* column IS NOT NULL
clause, depending on the parameter's value.
*
*/
public void apply(JRClauseTokens clauseTokens, JRQueryClauseContext queryContext)
{
String col = clauseTokens.getToken(POSITION_DB_COLUMN);
String param = clauseTokens.getToken(POSITION_PARAMETER);
if (col == null)
{
throw new JRRuntimeException("SQL EQUAL clause missing DB column token");
}
if (param == null)
{
throw new JRRuntimeException("SQL EQUAL clause missing parameter token");
}
StringBuffer sbuffer = queryContext.queryBuffer();
sbuffer.append(col);
sbuffer.append(' ');
handleEqualOperator(sbuffer, param, queryContext);
}
/**
* Finalizes the query string
*
* @param sbuffer
* @param param
* @param queryContext
*/
protected void finalizeClause(StringBuffer sbuffer, String param, JRQueryClauseContext queryContext)
{
sbuffer.append(' ');
sbuffer.append('?');
queryContext.addQueryParameter(param);
}
protected abstract void handleEqualOperator(StringBuffer sBuffer, String param, JRQueryClauseContext queryContext);
}