net.sf.jasperreports.engine.query.JRSqlAbstractInClause Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jasperreports Show documentation
Show all versions of jasperreports Show documentation
Free Java Reporting Library
/*
* JasperReports - Free Java Reporting Library.
* Copyright (C) 2001 - 2019 TIBCO Software Inc. 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 java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import net.sf.jasperreports.engine.JRRuntimeException;
/**
* Base (NOT) IN clause function for SQL queries.
*
*
* The first token in the $X{...} syntax is the function ID token. Possible values for
* the (NOT) IN clause function ID token are:
*
* IN
* NOTIN
*
*
*
* @author Lucian Chirita ([email protected])
*/
public abstract class JRSqlAbstractInClause implements JRClauseFunction
{
public static final String EXCEPTION_MESSAGE_KEY_QUERY_IN_CLAUSE_DB_COLUMN_TOKEN_MISSING = "query.in.clause.db.column.token.missing";
public static final String EXCEPTION_MESSAGE_KEY_QUERY_IN_CLAUSE_INVALID_PARAMETER_TYPE = "query.in.clause.invalid.parameter.type";
public static final String EXCEPTION_MESSAGE_KEY_QUERY_IN_CLAUSE_PARAMETER_TOKEN_MISSING = "query.in.clause.parameter.token.missing";
protected static final int POSITION_DB_COLUMN = 1;
protected static final int POSITION_PARAMETER = 2;
protected static final String CLAUSE_TRUISM = "0 = 0";
protected JRSqlAbstractInClause()
{
}
/**
* Creates a (NOT) IN SQL clause.
*
*
* The function expects two clause tokens (after the ID token):
*
* - The first token is the SQL column to be used in the clause.
* - The second token is the name of the report parameter that contains the value list.
*
* The value of this parameter has to be an array, a java.util.Collection
* or null
.
*
*
*
*
*
* The function constructs one of the following clauses:
*
* - When the function ID token is IN:
*
* - If the parameter's value is a collection of not null values, the function constructs
* a
<column_name> IN (?, ?, .., ?)
clause
* - If the parameter's value is a collection containing both null and not null values, the
* function constructs a
(<column_name> IS NULL OR <column_name> IN (?, ?, .., ?))
clause
* - If the parameter's value is a collection containing only null values, the function
* constructs a
<column_name> IS NULL
clause
*
*
* - When the function ID token is NOTIN:
*
* - If the parameter's value is a collection of not null values, the function constructs
* a
<column_name> NOT IN (?, ?, .., ?)
clause
* - If the parameter's value is a collection containing both null and not null values, the
* function constructs a
(<column_name> IS NOT NULL AND <column_name> NOT IN (?, ?, .., ?))
clause
* - If the parameter's value is a collection containing only null values, the function
* constructs a
<column_name> IS NOT NULL
clause
*
*
* - If the values list is null or empty, both IN and NOTIN functions generate a SQL clause that
* will always evaluate to true (e.g.
0 = 0
).
*
*
*
* @param clauseTokens
* @param queryContext
*
*/
@Override
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(
EXCEPTION_MESSAGE_KEY_QUERY_IN_CLAUSE_DB_COLUMN_TOKEN_MISSING,
(Object[])null);
}
if (param == null)
{
throw
new JRRuntimeException(
EXCEPTION_MESSAGE_KEY_QUERY_IN_CLAUSE_PARAMETER_TOKEN_MISSING,
(Object[])null);
}
StringBuffer sbuffer = queryContext.queryBuffer();
Object paramValue = queryContext.getValueParameter(param).getValue();
if (paramValue == null)
{
handleNoValues(queryContext);
}
else
{
Collection paramCollection = convert(param, paramValue);
int count = paramCollection.size();
Iterator it = paramCollection.iterator();
if (count == 0)
{
handleNoValues(queryContext);
}
else
{
StringBuffer nullSbuffer = new StringBuffer();
StringBuffer notNullSbuffer = new StringBuffer();
boolean nullFound = false;
boolean notNullFound = false;
int idx = 0;
List