org.hibernate.criterion.CountProjection Maven / Gradle / Ivy
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2009 by Red Hat Inc and/or its affiliates or by
* third-party contributors as indicated by either @author tags or express
* copyright attribution statements applied by the authors. All
* third-party contributions are distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program 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 distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.criterion;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.QueryException;
/**
* A count
* @author Gavin King
*/
public class CountProjection extends AggregateProjection {
private boolean distinct;
protected CountProjection(String prop) {
super("count", prop);
}
public String toString() {
if ( distinct ) {
return "distinct " + super.toString();
}
else {
return super.toString();
}
}
protected List buildFunctionParameterList(Criteria criteria, CriteriaQuery criteriaQuery) {
String cols[] = criteriaQuery.getColumns( propertyName, criteria );
return ( distinct ? buildCountDistinctParameterList( cols ) : Arrays.asList( cols ) );
}
private List buildCountDistinctParameterList(String[] cols) {
List params = new ArrayList( cols.length + 1 );
params.add( "distinct" );
params.addAll( Arrays.asList( cols ) );
return params;
}
public CountProjection setDistinct() {
distinct = true;
return this;
}
}