org.hibernate.impl.SQLQueryImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of smartweb Show documentation
Show all versions of smartweb Show documentation
SmartWeb is a web application development meta framework based on Jakarta Struts, Hibernate and other open source frameworks and libraries.
//$Id: SQLQueryImpl.java 10861 2006-11-22 00:11:25Z [email protected] $
package org.hibernate.impl;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.io.Serializable;
import java.math.BigInteger;
import org.hibernate.FlushMode;
import org.hibernate.HibernateException;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.QueryException;
import org.hibernate.SQLQuery;
import org.hibernate.ScrollMode;
import org.hibernate.ScrollableResults;
import org.hibernate.MappingException;
import org.hibernate.Session;
import org.hibernate.engine.query.sql.NativeSQLQuerySpecification;
import org.hibernate.engine.ResultSetMappingDefinition;
import org.hibernate.engine.NamedSQLQueryDefinition;
import org.hibernate.engine.QueryParameters;
import org.hibernate.engine.SessionImplementor;
import org.hibernate.engine.TypedValue;
import org.hibernate.engine.query.ParameterMetadata;
import org.hibernate.engine.query.sql.NativeSQLQueryJoinReturn;
import org.hibernate.engine.query.sql.NativeSQLQueryScalarReturn;
import org.hibernate.engine.query.sql.NativeSQLQueryRootReturn;
import org.hibernate.engine.query.sql.NativeSQLQueryReturn;
import org.hibernate.type.Type;
import org.hibernate.util.CollectionHelper;
import org.hibernate.util.StringHelper;
/**
* Implements SQL query passthrough.
*
*
*
*
* SELECT {person}.NAME AS {person.name}, {person}.AGE AS {person.age}, {person}.SEX AS {person.sex}
* FROM PERSON {person} WHERE {person}.NAME LIKE 'Hiber%'
*
*
*
* @author gperrone
*/
public class SQLQueryImpl extends AbstractQueryImpl implements SQLQuery {
private final List queryReturns;
private Collection querySpaces;
private final boolean callable;
private boolean autodiscovertypes;
/**
* Constructs a SQLQueryImpl given a sql query defined in the mappings.
*
* @param queryDef The representation of the defined .
* @param session The session to which this SQLQueryImpl belongs.
* @param parameterMetadata Metadata about parameters found in the query.
*/
SQLQueryImpl(NamedSQLQueryDefinition queryDef, SessionImplementor session, ParameterMetadata parameterMetadata) {
super( queryDef.getQueryString(), queryDef.getFlushMode(), session, parameterMetadata );
if ( queryDef.getResultSetRef() != null ) {
ResultSetMappingDefinition definition = session.getFactory()
.getResultSetMapping( queryDef.getResultSetRef() );
if (definition == null) {
throw new MappingException(
"Unable to find resultset-ref definition: " +
queryDef.getResultSetRef()
);
}
this.queryReturns = Arrays.asList( definition.getQueryReturns() );
}
else {
this.queryReturns = Arrays.asList( queryDef.getQueryReturns() );
}
this.querySpaces = queryDef.getQuerySpaces();
this.callable = queryDef.isCallable();
}
SQLQueryImpl(
final String sql,
final List queryReturns,
final Collection querySpaces,
final FlushMode flushMode,
boolean callable,
final SessionImplementor session,
ParameterMetadata parameterMetadata) {
// TODO : absolutely no usages of this constructor form; can it go away?
super( sql, flushMode, session, parameterMetadata );
this.queryReturns = queryReturns;
this.querySpaces = querySpaces;
this.callable = callable;
}
SQLQueryImpl(
final String sql,
final String returnAliases[],
final Class returnClasses[],
final LockMode[] lockModes,
final SessionImplementor session,
final Collection querySpaces,
final FlushMode flushMode,
ParameterMetadata parameterMetadata) {
// TODO : this constructor form is *only* used from constructor directly below us; can it go away?
super( sql, flushMode, session, parameterMetadata );
queryReturns = new ArrayList(returnAliases.length);
for ( int i=0; i= 0) {
sqlQueryBuffer.append(this.getQueryString().substring(start, stop));
} else {
sqlQueryBuffer.append(this.getQueryString().substring(start));
}
Query countQuery = ((Session)this.session).createSQLQuery(sqlQueryBuffer.toString());
//SET NAMED PARAMS
Map parameters = this.getNamedParams();
Set parameterNames = parameters.keySet();
Iterator iter = parameterNames.iterator();
while (iter.hasNext()) {
String name = (String)iter.next();
TypedValue typedValue = (TypedValue)parameters.get(name);
countQuery.setParameter(name, typedValue.getValue(), typedValue.getType());
}
//SET POSITIONAL PARAMS
List paramTypes = this.getTypes();
List paramValues = this.getValues();
if (paramTypes != null && paramTypes.size() > 0) {
for (int i = 0; i < paramTypes.size(); i++) {
countQuery.setParameter(i, paramValues.get(i), (Type)paramTypes.get(i));
}
}
int count = ((BigInteger)countQuery.uniqueResult()).intValue();
return count;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy