A QueryTranslator that uses an Antlr-based parser.
| Method from org.hibernate.hql.ast.QueryTranslatorImpl Detail: |
public List collectSqlStrings() {
ArrayList list = new ArrayList();
if ( isManipulationStatement() ) {
String[] sqlStatements = statementExecutor.getSqlStatements();
for ( int i = 0; i < sqlStatements.length; i++ ) {
list.add( sqlStatements[i] );
}
}
else {
list.add( sql );
}
return list;
}
|
public void compile(Map replacements,
boolean shallow) throws MappingException, QueryException {
doCompile( replacements, shallow, null );
}
Compile a "normal" query. This method may be called multiple
times. Subsequent invocations are no-ops. |
public void compile(String collectionRole,
Map replacements,
boolean shallow) throws MappingException, QueryException {
doCompile( replacements, shallow, collectionRole );
}
Compile a filter. This method may be called multiple
times. Subsequent invocations are no-ops. |
public boolean containsCollectionFetches() {
errorIfDML();
List collectionFetches = ( ( QueryNode ) sqlAst ).getFromClause().getCollectionFetches();
return collectionFetches != null && collectionFetches.size() > 0;
}
|
public int executeUpdate(QueryParameters queryParameters,
SessionImplementor session) throws HibernateException {
errorIfSelect();
return statementExecutor.execute( queryParameters, session );
}
|
public String[][] getColumnNames() {
errorIfDML();
return getWalker().getSelectClause().getColumnNames();
}
|
public Map getEnabledFilters() {
return enabledFilters;
}
|
public int[] getNamedParameterLocs(String name) {
return getWalker().getNamedParameterLocations( name );
}
|
public ParameterTranslations getParameterTranslations() {
if ( paramTranslations == null ) {
paramTranslations = new ParameterTranslationsImpl( getWalker().getParameters() );
}
return paramTranslations;
}
|
public String getQueryIdentifier() {
return queryIdentifier;
}
|
public Set getQuerySpaces() {
return getWalker().getQuerySpaces();
}
|
public String getQueryString() {
return hql;
}
|
public String[] getReturnAliases() {
errorIfDML();
return getWalker().getReturnAliases();
}
|
public Type[] getReturnTypes() {
errorIfDML();
return getWalker().getReturnTypes();
}
Types of the return values of an iterate() style query. |
public String getSQLString() {
return sql;
}
The SQL query string to be called; implemented by all subclasses |
public Statement getSqlAST() {
return sqlAst;
}
|
public boolean isManipulationStatement() {
return sqlAst.needsExecutor();
}
|
public boolean isShallowQuery() {
return shallowQuery;
}
|
public Iterator iterate(QueryParameters queryParameters,
EventSource session) throws HibernateException {
// Delegate to the QueryLoader...
errorIfDML();
return queryLoader.iterate( queryParameters, session );
}
Return the query results as an iterator |
public List list(SessionImplementor session,
QueryParameters queryParameters) throws HibernateException {
// Delegate to the QueryLoader...
errorIfDML();
QueryNode query = ( QueryNode ) sqlAst;
boolean hasLimit = queryParameters.getRowSelection() != null && queryParameters.getRowSelection().definesLimits();
boolean needsDistincting = ( query.getSelectClause().isDistinct() || hasLimit ) && containsCollectionFetches();
QueryParameters queryParametersToUse;
if ( hasLimit && containsCollectionFetches() ) {
log.warn( "firstResult/maxResults specified with collection fetch; applying in memory!" );
RowSelection selection = new RowSelection();
selection.setFetchSize( queryParameters.getRowSelection().getFetchSize() );
selection.setTimeout( queryParameters.getRowSelection().getTimeout() );
queryParametersToUse = queryParameters.createCopyUsing( selection );
}
else {
queryParametersToUse = queryParameters;
}
List results = queryLoader.list( session, queryParametersToUse );
if ( needsDistincting ) {
int includedCount = -1;
// NOTE : firstRow is zero-based
int first = !hasLimit || queryParameters.getRowSelection().getFirstRow() == null
? 0
: queryParameters.getRowSelection().getFirstRow().intValue();
int max = !hasLimit || queryParameters.getRowSelection().getMaxRows() == null
? -1
: queryParameters.getRowSelection().getMaxRows().intValue();
int size = results.size();
List tmp = new ArrayList();
IdentitySet distinction = new IdentitySet();
for ( int i = 0; i < size; i++ ) {
final Object result = results.get( i );
if ( !distinction.add( result ) ) {
continue;
}
includedCount++;
if ( includedCount < first ) {
continue;
}
tmp.add( result );
// NOTE : ( max - 1 ) because first is zero-based while max is not...
if ( max >= 0 && ( includedCount - first ) >= ( max - 1 ) ) {
break;
}
}
results = tmp;
}
return results;
}
|
public ScrollableResults scroll(QueryParameters queryParameters,
SessionImplementor session) throws HibernateException {
// Delegate to the QueryLoader...
errorIfDML();
return queryLoader.scroll( queryParameters, session );
}
Return the query results, as an instance of ScrollableResults |
void showHqlAst(AST hqlAst) {
if ( AST_LOG.isDebugEnabled() ) {
ASTPrinter printer = new ASTPrinter( HqlTokenTypes.class );
printer.setShowClassNames( false ); // The class names aren't interesting in the first tree.
AST_LOG.debug( printer.showAsString( hqlAst, "--- HQL AST ---" ) );
}
}
|
public void validateScrollability() throws HibernateException {
// Impl Note: allows multiple collection fetches as long as the
// entire fecthed graph still "points back" to a single
// root entity for return
errorIfDML();
QueryNode query = ( QueryNode ) sqlAst;
// If there are no collection fetches, then no further checks are needed
List collectionFetches = query.getFromClause().getCollectionFetches();
if ( collectionFetches.isEmpty() ) {
return;
}
// A shallow query is ok (although technically there should be no fetching here...)
if ( isShallowQuery() ) {
return;
}
// Otherwise, we have a non-scalar select with defined collection fetch(es).
// Make sure that there is only a single root entity in the return (no tuples)
if ( getReturnTypes().length > 1 ) {
throw new HibernateException( "cannot scroll with collection fetches and returned tuples" );
}
FromElement owner = null;
Iterator itr = query.getSelectClause().getFromElementsForLoad().iterator();
while ( itr.hasNext() ) {
// should be the first, but just to be safe...
final FromElement fromElement = ( FromElement ) itr.next();
if ( fromElement.getOrigin() == null ) {
owner = fromElement;
break;
}
}
if ( owner == null ) {
throw new HibernateException( "unable to locate collection fetch(es) owner for scrollability checks" );
}
// This is not strictly true. We actually just need to make sure that
// it is ordered by root-entity PK and that that order-by comes before
// any non-root-entity ordering...
AST primaryOrdering = query.getOrderByClause().getFirstChild();
if ( primaryOrdering != null ) {
// TODO : this is a bit dodgy, come up with a better way to check this (plus see above comment)
String [] idColNames = owner.getQueryable().getIdentifierColumnNames();
String expectedPrimaryOrderSeq = StringHelper.join(
", ",
StringHelper.qualify( owner.getTableAlias(), idColNames )
);
if ( !primaryOrdering.getText().startsWith( expectedPrimaryOrderSeq ) ) {
throw new HibernateException( "cannot scroll results with collection fetches which are not ordered primarily by the root entity's PK" );
}
}
}
|