translates a Hibernate
query string to SQL.
| Method from org.hibernate.hql.classic.QueryTranslatorImpl Detail: |
void addCollection(String name,
String role) {
collections.put( name, role );
}
|
void addEntityToFetch(String name,
String oneToOneOwnerName,
AssociationType ownerAssociationType) {
addEntityToFetch( name );
if ( oneToOneOwnerName != null ) oneToOneOwnerNames.put( name, oneToOneOwnerName );
if ( ownerAssociationType != null ) uniqueKeyOwnerReferences.put( name, ownerAssociationType );
}
|
void addFrom(String name,
JoinSequence joinSequence) throws QueryException {
fromTypes.add( name );
addJoin( name, joinSequence );
}
|
void addFrom(String name,
String type,
JoinSequence joinSequence) throws QueryException {
addType( name, type );
addFrom( name, joinSequence );
}
|
void addFromClass(String name,
Queryable classPersister) throws QueryException {
JoinSequence joinSequence = new JoinSequence( getFactory() )
.setRoot( classPersister, name );
//crossJoins.add(name);
addFrom( name, classPersister.getEntityName(), joinSequence );
}
|
void addFromCollection(String name,
String collectionRole,
JoinSequence joinSequence) throws QueryException {
//register collection role
addCollection( name, collectionRole );
addJoin( name, joinSequence );
}
|
void addFromJoinOnly(String name,
JoinSequence joinSequence) throws QueryException {
addJoin( name, joinSequence.getFromPart() );
}
|
void addJoin(String name,
JoinSequence joinSequence) throws QueryException {
if ( !joins.containsKey( name ) ) joins.put( name, joinSequence );
}
|
void addNamedParameter(String name) {
if ( superQuery != null ) superQuery.addNamedParameter( name );
Integer loc = new Integer( parameterCount++ );
Object o = namedParameters.get( name );
if ( o == null ) {
namedParameters.put( name, loc );
}
else if ( o instanceof Integer ) {
ArrayList list = new ArrayList( 4 );
list.add( o );
list.add( loc );
namedParameters.put( name, list );
}
else {
( ( ArrayList ) o ).add( loc );
}
}
|
void addPathAliasAndJoin(String path,
String alias,
JoinSequence joinSequence) {
pathAliases.put( path, alias );
pathJoins.put( path, joinSequence );
}
|
void addQuerySpaces(Serializable[] spaces) {
for ( int i = 0; i < spaces.length; i++ ) {
querySpaces.add( spaces[i] );
}
if ( superQuery != null ) superQuery.addQuerySpaces( spaces );
}
|
void addSelectClass(String name) {
returnedTypes.add( name );
}
|
void addSelectScalar(Type type) {
scalarTypes.add( type );
}
|
void addType(String name,
String type) {
typeMap.put( name, type );
}
|
void appendGroupByToken(String token) {
groupByTokens.add( token );
}
|
void appendHavingToken(String token) {
havingTokens.add( token );
}
|
void appendOrderByToken(String token) {
orderByTokens.add( token );
}
|
void appendScalarSelectToken(String token) {
scalarSelectTokens.add( token );
}
|
void appendScalarSelectTokens(String[] tokens) {
scalarSelectTokens.add( tokens );
}
|
void appendWhereToken(String token) {
whereTokens.add( token );
}
|
protected String applyLocks(String sql,
Map lockModes,
Dialect dialect) throws QueryException {
// can't cache this stuff either (per-invocation)
final String result;
if ( lockModes == null || lockModes.size() == 0 ) {
result = sql;
}
else {
Map aliasedLockModes = new HashMap();
Iterator iter = lockModes.entrySet().iterator();
while ( iter.hasNext() ) {
Map.Entry me = ( Map.Entry ) iter.next();
aliasedLockModes.put( getAliasName( ( String ) me.getKey() ), me.getValue() );
}
Map keyColumnNames = null;
if ( dialect.forUpdateOfColumns() ) {
keyColumnNames = new HashMap();
for ( int i = 0; i < names.length; i++ ) {
keyColumnNames.put( names[i], persisters[i].getIdentifierColumnNames() );
}
}
result = dialect.applyLocksToSql( sql, aliasedLockModes, keyColumnNames );
}
logQuery( queryString, result );
return result;
}
|
public List collectSqlStrings() {
return ArrayHelper.toList( new String[] { sqlString } );
}
|
void compile(QueryTranslatorImpl superquery) throws QueryException, MappingException {
this.tokenReplacements = superquery.tokenReplacements;
this.superQuery = superquery;
this.shallowQuery = true;
this.enabledFilters = superquery.getEnabledFilters();
compile();
}
|
public synchronized void compile(Map replacements,
boolean scalar) throws QueryException, MappingException {
if ( !compiled ) {
this.tokenReplacements = replacements;
this.shallowQuery = scalar;
compile();
}
}
Compile a "normal" query. This method may be called multiple
times. Subsequent invocations are no-ops. |
public synchronized void compile(String collectionRole,
Map replacements,
boolean scalar) throws QueryException, MappingException {
if ( !isCompiled() ) {
addFromAssociation( "this", collectionRole );
compile( replacements, scalar );
}
}
Compile a filter. This method may be called multiple
times. Subsequent invocations are no-ops. |
public boolean containsCollectionFetches() {
return false;
}
|
String createNameFor(String type) {
return StringHelper.generateAlias( type, nextCount() );
}
|
String createNameForCollection(String role) {
return StringHelper.generateAlias( role, nextCount() );
}
|
void decoratePropertyMapping(String name,
PropertyMapping mapping) {
decoratedPropertyMappings.put( name, mapping );
}
|
public int executeUpdate(QueryParameters queryParameters,
SessionImplementor session) throws HibernateException {
throw new UnsupportedOperationException( "Not supported! Use the AST translator...");
}
|
public String getAliasName(String alias) {
String name = ( String ) aliasNames.get( alias );
if ( name == null ) {
if ( superQuery != null ) {
name = superQuery.getAliasName( alias );
}
else {
name = alias;
}
}
return name;
}
|
protected String[] getAliases() {
return names;
}
|
protected int[] getCollectionOwners() {
return new int[] { collectionOwnerColumn };
}
|
QueryableCollection getCollectionPersister(String role) throws QueryException {
try {
return ( QueryableCollection ) getFactory().getCollectionPersister( role );
}
catch ( ClassCastException cce ) {
throw new QueryException( "collection role is not queryable: " + role );
}
catch ( Exception e ) {
throw new QueryException( "collection role not found: " + role );
}
}
|
public CollectionPersister[] getCollectionPersisters() {
return collectionPersister == null ? null : new CollectionPersister[] { collectionPersister };
}
Overrides method from Loader |
protected String[] getCollectionSuffixes() {
return collectionPersister == null ? null : new String[] { "__" };
}
|
public String[][] getColumnNames() {
return scalarColumnNames;
}
|
public Map getEnabledFilters() {
return enabledFilters;
}
|
Queryable getEntityPersister(String entityName) throws QueryException {
try {
return ( Queryable ) getFactory().getEntityPersister( entityName );
}
catch ( Exception e ) {
throw new QueryException( "persistent class not found: " + entityName );
}
}
|
Queryable getEntityPersisterUsingImports(String className) {
final String importedClassName = getFactory().getImportedClassName( className );
if ( importedClassName == null ) {
return null;
}
try {
return ( Queryable ) getFactory().getEntityPersister( importedClassName );
}
catch ( MappingException me ) {
return null;
}
}
|
protected Loadable[] getEntityPersisters() {
return persisters;
}
Persisters for the return values of a find() style query. |
public Class getHolderClass() {
return holderClass;
}
|
protected LockMode[] getLockModes(Map lockModes) {
// unfortunately this stuff can't be cached because
// it is per-invocation, not constant for the
// QueryTranslator instance
HashMap nameLockModes = new HashMap();
if ( lockModes != null ) {
Iterator iter = lockModes.entrySet().iterator();
while ( iter.hasNext() ) {
Map.Entry me = ( Map.Entry ) iter.next();
nameLockModes.put( getAliasName( ( String ) me.getKey() ),
me.getValue() );
}
}
LockMode[] lockModeArray = new LockMode[names.length];
for ( int i = 0; i < names.length; i++ ) {
LockMode lm = ( LockMode ) nameLockModes.get( names[i] );
if ( lm == null ) lm = LockMode.NONE;
lockModeArray[i] = lm;
}
return lockModeArray;
}
|
public int[] getNamedParameterLocs(String name) throws QueryException {
Object o = namedParameters.get( name );
if ( o == null ) {
QueryException qe = new QueryException( ERROR_NAMED_PARAMETER_DOES_NOT_APPEAR + name );
qe.setQueryString( queryString );
throw qe;
}
if ( o instanceof Integer ) {
return new int[]{ ( ( Integer ) o ).intValue() };
}
else {
return ArrayHelper.toIntArray( ( ArrayList ) o );
}
}
|
protected EntityType[] getOwnerAssociationTypes() {
return ownerAssociationTypes;
}
|
protected int[] getOwners() {
return owners;
}
|
public ParameterTranslations getParameterTranslations() {
return new ParameterTranslations() {
public boolean supportsOrdinalParameterMetadata() {
// classic translator does not support collection of ordinal
// param metadata
return false;
}
public int getOrdinalParameterCount() {
return 0; // not known!
}
public int getOrdinalParameterSqlLocation(int ordinalPosition) {
return 0; // not known!
}
public Type getOrdinalParameterExpectedType(int ordinalPosition) {
return null; // not known!
}
public Set getNamedParameterNames() {
return namedParameters.keySet();
}
public int[] getNamedParameterSqlLocations(String name) {
return getNamedParameterLocs( name );
}
public Type getNamedParameterExpectedType(String name) {
return null; // not known!
}
};
}
|
String getPathAlias(String path) {
return ( String ) pathAliases.get( path );
}
|
JoinSequence getPathJoin(String path) {
return ( JoinSequence ) pathJoins.get( path );
}
|
PropertyMapping getPropertyMapping(String name) throws QueryException {
PropertyMapping decorator = getDecoratedPropertyMapping( name );
if ( decorator != null ) return decorator;
String type = getType( name );
if ( type == null ) {
String role = getRole( name );
if ( role == null ) {
throw new QueryException( "alias not found: " + name );
}
return getCollectionPersister( role ); //.getElementPropertyMapping();
}
else {
Queryable persister = getEntityPersister( type );
if ( persister == null ) throw new QueryException( "persistent class not found: " + type );
return persister;
}
}
|
public String getQueryIdentifier() {
return queryIdentifier;
}
|
public final Set getQuerySpaces() {
return querySpaces;
}
|
public String getQueryString() {
return queryString;
}
|
protected Object getResultColumnOrRow(Object[] row,
ResultTransformer transformer,
ResultSet rs,
SessionImplementor session) throws SQLException, HibernateException {
row = toResultRow( row );
if ( hasScalars ) {
String[][] scalarColumns = getColumnNames();
int queryCols = returnTypes.length;
if ( holderClass == null && queryCols == 1 ) {
return returnTypes[0].nullSafeGet( rs, scalarColumns[0], session, null );
}
else {
row = new Object[queryCols];
for ( int i = 0; i < queryCols; i++ )
row[i] = returnTypes[i].nullSafeGet( rs, scalarColumns[i], session, null );
return row;
}
}
else if ( holderClass == null ) {
return row.length == 1 ? row[0] : row;
}
else {
return row;
}
}
|
protected List getResultList(List results,
ResultTransformer resultTransformer) throws QueryException {
if ( holderClass != null ) {
for ( int i = 0; i < results.size(); i++ ) {
Object[] row = ( Object[] ) results.get( i );
try {
results.set( i, holderConstructor.newInstance( row ) );
}
catch ( Exception e ) {
throw new QueryException( "could not instantiate: " + holderClass, e );
}
}
}
return results;
}
|
public String[] getReturnAliases() {
// return aliases not supported in classic translator!
return NO_RETURN_ALIASES;
}
|
public Type[] getReturnTypes() {
return actualReturnTypes;
}
Types of the return values of an iterate() style query. |
public String getSQLString() {
return sqlString;
}
|
protected String[] getSuffixes() {
return suffixes;
}
|
protected boolean isCompiled() {
return compiled;
}
|
public boolean isManipulationStatement() {
// classic parser does not support bulk manipulation statements
return false;
}
|
boolean isName(String name) {
return aliasNames.containsKey( name ) ||
typeMap.containsKey( name ) ||
collections.containsKey( name ) || (
superQuery != null && superQuery.isName( name )
);
}
|
boolean isShallowQuery() {
return shallowQuery;
}
Is this query called by scroll() or iterate()? |
boolean isSubquery() {
return superQuery != null;
}
|
protected boolean isSubselectLoadingEnabled() {
return hasSubselectLoadableCollections();
}
|
public Iterator iterate(QueryParameters queryParameters,
EventSource session) throws HibernateException {
boolean stats = session.getFactory().getStatistics().isStatisticsEnabled();
long startTime = 0;
if ( stats ) startTime = System.currentTimeMillis();
try {
PreparedStatement st = prepareQueryStatement( queryParameters, false, session );
ResultSet rs = getResultSet( st, queryParameters.hasAutoDiscoverScalarTypes(), false, queryParameters.getRowSelection(), session );
HolderInstantiator hi = HolderInstantiator.createClassicHolderInstantiator(holderConstructor, queryParameters.getResultTransformer());
Iterator result = new IteratorImpl( rs, st, session, returnTypes, getColumnNames(), hi );
if ( stats ) {
session.getFactory().getStatisticsImplementor().queryExecuted(
"HQL: " + queryString,
0,
System.currentTimeMillis() - startTime
);
}
return result;
}
catch ( SQLException sqle ) {
throw JDBCExceptionHelper.convert(
getFactory().getSQLExceptionConverter(),
sqle,
"could not execute query using iterate",
getSQLString()
);
}
}
Return the query results as an iterator |
public List list(SessionImplementor session,
QueryParameters queryParameters) throws HibernateException {
return list( session, queryParameters, getQuerySpaces(), actualReturnTypes );
}
|
public ScrollableResults scroll(QueryParameters queryParameters,
SessionImplementor session) throws HibernateException {
HolderInstantiator hi = HolderInstantiator.createClassicHolderInstantiator(holderConstructor, queryParameters.getResultTransformer());
return scroll( queryParameters, returnTypes, hi, session );
}
|
void setAliasName(String alias,
String name) {
aliasNames.put( alias, name );
}
|
void setCollectionToFetch(String role,
String name,
String ownerName,
String entityName) throws QueryException {
fetchName = name;
collectionPersister = getCollectionPersister( role );
collectionOwnerName = ownerName;
if ( collectionPersister.getElementType().isEntityType() ) {
addEntityToFetch( entityName );
}
}
|
void setDistinct(boolean distinct) {
this.distinct = distinct;
}
|
void setHolderClass(Class clazz) {
holderClass = clazz;
}
|
public String toString() {
return queryString;
}
|
String unalias(String path) {
String alias = StringHelper.root( path );
String name = getAliasName( alias );
if ( name != null ) {
return name + path.substring( alias.length() );
}
else {
return path;
}
}
|
protected boolean upgradeLocks() {
return true;
}
|
public void validateScrollability() throws HibernateException {
// This is the legacy behaviour for HQL queries...
if ( getCollectionPersisters() != null ) {
throw new HibernateException( "Cannot scroll queries which initialize collections" );
}
}
|