All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.hibernate.sql.ast.spi.SqlAliasBaseManager Maven / Gradle / Ivy

There is a newer version: 7.0.0.Beta1
Show newest version
/*
 * Hibernate, Relational Persistence for Idiomatic Java
 *
 * License: GNU Lesser General Public License (LGPL), version 2.1 or later
 * See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
 */
package org.hibernate.sql.ast.spi;

import java.util.HashMap;
import java.util.Map;

import org.hibernate.sql.ast.SqlTreeCreationLogger;

/**
 * Helper used in creating unique SQL table aliases for a SQL AST
 *
 * @author Steve Ebersole
 */
public class SqlAliasBaseManager implements SqlAliasBaseGenerator {
	// work dictionary used to map an acronym to the number of times it has been used.
	private Map acronymCountMap = new HashMap<>();

	@Override
	public SqlAliasBase createSqlAliasBase(String stem) {
		Integer acronymCount = acronymCountMap.get( stem );
		if ( acronymCount == null ) {
			acronymCount = 0;
		}
		acronymCount++;
		acronymCountMap.put( stem, acronymCount );

		return new SqlAliasBaseImpl( stem + acronymCount );
	}

	private static class SqlAliasBaseImpl implements SqlAliasBase {
		private final String stem;
		private int aliasCount;

		SqlAliasBaseImpl(String stem) {
			this.stem = stem;
		}

		@Override
		public String getAliasStem() {
			return stem;
		}

		@Override
		public String generateNewAlias() {
			synchronized ( this ) {
				final String alias = stem + "_" + ( aliasCount++ );
				if ( SqlTreeCreationLogger.DEBUG_ENABLED ) {
					SqlTreeCreationLogger.LOGGER.debugf( "Created new SQL alias : %s", alias );
				}
				return alias;
			}
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy