Author: steve.ebersole(a)jboss.com
Date: 2009-02-02 15:24:25 -0500 (Mon, 02 Feb 2009)
New Revision: 15857
Added:
core/branches/SQL_GEN_REDESIGN/src/main/java/org/hibernate/sql/ast/alias/
core/branches/SQL_GEN_REDESIGN/src/main/java/org/hibernate/sql/ast/alias/AbstractTableAliasGeneratorTemplate.java
core/branches/SQL_GEN_REDESIGN/src/main/java/org/hibernate/sql/ast/alias/DefaultTableAliasGenerator.java
core/branches/SQL_GEN_REDESIGN/src/main/java/org/hibernate/sql/ast/alias/ImplicitAliasGenerator.java
core/branches/SQL_GEN_REDESIGN/src/main/java/org/hibernate/sql/ast/alias/TableAliasGenerator.java
Removed:
core/branches/SQL_GEN_REDESIGN/src/main/java/org/hibernate/sql/ast/phase/hql/normalize/AliasBuilder.java
Log:
HHH-2407 : HQL translation rework ->
HHH-3687 : parse (phase1)
HHH-3688 : normalize (phase2)
Added:
core/branches/SQL_GEN_REDESIGN/src/main/java/org/hibernate/sql/ast/alias/AbstractTableAliasGeneratorTemplate.java
===================================================================
---
core/branches/SQL_GEN_REDESIGN/src/main/java/org/hibernate/sql/ast/alias/AbstractTableAliasGeneratorTemplate.java
(rev 0)
+++
core/branches/SQL_GEN_REDESIGN/src/main/java/org/hibernate/sql/ast/alias/AbstractTableAliasGeneratorTemplate.java 2009-02-02
20:24:25 UTC (rev 15857)
@@ -0,0 +1,146 @@
+/*
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * Copyright (c) 2009, Red Hat Middleware LLC or third-party contributors as
+ * indicated by the @author tags or express copyright attribution
+ * statements applied by the authors. All third-party contributions are
+ * distributed under license by Red Hat Middleware LLC.
+ *
+ * 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.sql.ast.alias;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import org.hibernate.persister.entity.Queryable;
+import org.hibernate.persister.collection.QueryableCollection;
+import org.hibernate.dialect.Dialect;
+import org.hibernate.util.StringHelper;
+
+/**
+ * Defines a termplated implementation of the {@link TableAliasGenerator} contract.
+ * <p/>
+ * The variance is in the subclass implementation of the {@link #truncateAliasBase}
method.
+ *
+ * @author Steve Ebersole
+ */
+public abstract class AbstractTableAliasGeneratorTemplate implements TableAliasGenerator
{
+ private static final Logger log = LoggerFactory.getLogger( TableAliasGenerator.class );
+
+ private final Dialect dialect;
+ private int uniqueingValue = 0;
+
+ protected AbstractTableAliasGeneratorTemplate(Dialect dialect) {
+ this.dialect = dialect;
+ }
+
+ /**
+ * Truncate down the base of the sql alias root to the
'totalAllowableSizeOfAliasBase'.
+ * <p/>
+ * This abstract method provides the variance in the templating routine; different
implementations will
+ * define this extact behavior differently.
+ *
+ * @param base The base for the alias root.
+ * @param totalAllowableSizeOfAliasBase The total allowable size of the base after
truncating.
+ *
+ * @return The appropriately truncated base.
+ */
+ protected abstract String truncateAliasBase(String base, int
totalAllowableSizeOfAliasBase);
+
+ /**
+ * {@inheritDoc}
+ */
+ public final TableAliasRoot generateSqlAliasRoot(Queryable persister, String
sourceAlias) {
+ log.trace( "Generating SQL alias root (entity) : " + sourceAlias );
+ String base = sourceAlias;
+ if ( ImplicitAliasGenerator.isImplicitAlias( sourceAlias ) ) {
+ base = persister.getMappedTableMetadata().getSqlAliasRootBase();
+ }
+ return generateSqlAliasRoot( base, determineMappedTableCount( persister ) );
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public final TableAliasRoot generateSqlAliasRoot(QueryableCollection persister, String
sourceAlias) {
+ log.trace( "Generating SQL alias root (collection) : " + sourceAlias );
+ String base = sourceAlias;
+ if ( ImplicitAliasGenerator.isImplicitAlias( sourceAlias ) ) {
+ base = StringHelper.unqualify( persister.getName() ).toLowerCase();
+ }
+ return generateSqlAliasRoot( base, determineMappedTableCount( persister ) );
+ }
+
+ private int determineMappedTableCount(Queryable persister) {
+ return persister.getMappedTableMetadata().getJoinedTables().length + 1;
+ }
+
+ private int determineMappedTableCount(QueryableCollection persister) {
+ if ( !persister.getElementType().isAssociationType() ) {
+ return 1;
+ }
+ else {
+ return determineMappedTableCount( ( Queryable ) persister.getElementPersister() );
+ }
+ }
+
+ protected final TableAliasRoot generateSqlAliasRoot(String base, int tableCount) {
+ base = cleanBase( base );
+ base = ensureAliasCapacity( base, tableCount );
+ return new TableAliasRoot( base );
+ }
+
+ private String cleanBase(String base) {
+ base = base.toLowerCase()
+ .replace( '/', '_' )
+ .replace( '$', '_' );
+
+ char[] chars = base.toCharArray();
+ if ( !Character.isLetter( chars[0] ) ) {
+ for ( int i = 1; i < chars.length; i++ ) {
+ if ( Character.isLetter( chars[i] ) ) {
+ base = base.substring( i );
+ }
+ }
+ }
+
+ if ( Character.isDigit( base.charAt( base.length() - 1 ) ) ) {
+ base = base.substring( 0, base.length() - 1 ) + 'x';
+ }
+
+ return base;
+ }
+
+ private String ensureAliasCapacity(String base, int mappedTableCount) {
+ // we need to consider the max-alias-length reported by the dialect against the
+ // size of the incoming base + the number of mapped tables
+ int nextUniqueingValueSize = Integer.toString( uniqueingValue + 1 ).length();
+ int totalAllowableSizeOfAliasBase = dialect.getMaxAliasLength() -
nextUniqueingValueSize;
+ if ( mappedTableCount > 1 ) {
+ totalAllowableSizeOfAliasBase-= Integer.toString( mappedTableCount ).length();
+ }
+ return buildUniqueAliasBase( truncateAliasBase( base, totalAllowableSizeOfAliasBase )
);
+ }
+
+ private String buildUniqueAliasBase(String base) {
+ return base + uniqueInteger() + '_';
+ }
+
+ private int uniqueInteger() {
+ return uniqueingValue++;
+ }
+}
Added:
core/branches/SQL_GEN_REDESIGN/src/main/java/org/hibernate/sql/ast/alias/DefaultTableAliasGenerator.java
===================================================================
---
core/branches/SQL_GEN_REDESIGN/src/main/java/org/hibernate/sql/ast/alias/DefaultTableAliasGenerator.java
(rev 0)
+++
core/branches/SQL_GEN_REDESIGN/src/main/java/org/hibernate/sql/ast/alias/DefaultTableAliasGenerator.java 2009-02-02
20:24:25 UTC (rev 15857)
@@ -0,0 +1,47 @@
+/*
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * Copyright (c) 2009, Red Hat Middleware LLC or third-party contributors as
+ * indicated by the @author tags or express copyright attribution
+ * statements applied by the authors. All third-party contributions are
+ * distributed under license by Red Hat Middleware LLC.
+ *
+ * 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.sql.ast.alias;
+
+import org.hibernate.dialect.Dialect;
+import org.hibernate.util.StringHelper;
+
+/**
+ * The default implementation of the templating provided by {@link
AbstractTableAliasGeneratorTemplate}.
+ * <p/>
+ * Here we simply truncate the base down to the allowable size.
+ *
+ * @author Steve Ebersole
+ */
+public class DefaultTableAliasGenerator extends AbstractTableAliasGeneratorTemplate {
+ public DefaultTableAliasGenerator(Dialect dialect) {
+ super( dialect );
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ protected String truncateAliasBase(String base, int totalAllowableSizeOfAliasBase) {
+ return StringHelper.truncate( base, totalAllowableSizeOfAliasBase );
+ }
+}
\ No newline at end of file
Copied:
core/branches/SQL_GEN_REDESIGN/src/main/java/org/hibernate/sql/ast/alias/ImplicitAliasGenerator.java
(from rev 15747,
core/branches/SQL_GEN_REDESIGN/src/main/java/org/hibernate/sql/ast/phase/hql/normalize/AliasBuilder.java)
===================================================================
---
core/branches/SQL_GEN_REDESIGN/src/main/java/org/hibernate/sql/ast/alias/ImplicitAliasGenerator.java
(rev 0)
+++
core/branches/SQL_GEN_REDESIGN/src/main/java/org/hibernate/sql/ast/alias/ImplicitAliasGenerator.java 2009-02-02
20:24:25 UTC (rev 15857)
@@ -0,0 +1,52 @@
+/*
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as
+ * indicated by the @author tags or express copyright attribution
+ * statements applied by the authors. All third-party contributions are
+ * distributed under license by Red Hat Middleware LLC.
+ *
+ * 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.sql.ast.alias;
+
+/**
+ * Handles generating implicit (or synthetic) aliases.
+ *
+ * @author Steve Ebersole
+ */
+public class ImplicitAliasGenerator {
+ private int unaliasedCount = 0;
+
+ /**
+ * Builds a unique implicit alias.
+ *
+ * @return The generated alias.
+ */
+ public synchronized String buildUniqueImplicitAlias() {
+ return "<gen:" + unaliasedCount++ + ">";
+ }
+
+ /**
+ * Determine if the given alias is implicit.
+ *
+ * @param alias The alias to check
+ * @return True/false.
+ */
+ public static boolean isImplicitAlias(String alias) {
+ return alias == null || ( alias.startsWith( "<gen:" ) &&
alias.endsWith( ">" ) );
+ }
+}
Added:
core/branches/SQL_GEN_REDESIGN/src/main/java/org/hibernate/sql/ast/alias/TableAliasGenerator.java
===================================================================
---
core/branches/SQL_GEN_REDESIGN/src/main/java/org/hibernate/sql/ast/alias/TableAliasGenerator.java
(rev 0)
+++
core/branches/SQL_GEN_REDESIGN/src/main/java/org/hibernate/sql/ast/alias/TableAliasGenerator.java 2009-02-02
20:24:25 UTC (rev 15857)
@@ -0,0 +1,111 @@
+/*
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * Copyright (c) 2009, Red Hat Middleware LLC or third-party contributors as
+ * indicated by the @author tags or express copyright attribution
+ * statements applied by the authors. All third-party contributions are
+ * distributed under license by Red Hat Middleware LLC.
+ *
+ * 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.sql.ast.alias;
+
+import org.hibernate.persister.collection.QueryableCollection;
+import org.hibernate.persister.entity.Queryable;
+
+/**
+ * Contract for generating table alias roots. An alias root is the base used to create
aliases for a whole series
+ * of related tables (e.g., for all the tables in a joined-subclass hierarchy).
+ *
+ * @author Steve Ebersole
+ */
+public interface TableAliasGenerator {
+ /**
+ * Encapsulation of the alias root.
+ */
+ public static class TableAliasRoot {
+ private final String base;
+
+ public TableAliasRoot(String base) {
+ this.base = base;
+ }
+
+ /**
+ * Generate the sql alias based on the given suffix which is the <i>subclass
table number</i>.
+ *
+ * @param suffix The alias suffix.
+ *
+ * @return The generated alias.
+ */
+ public String generate(int suffix) {
+ return base + Integer.toString( suffix ) + '_';
+ }
+
+ /**
+ * Generate an alias for the <i>collection table</i>.
+ * <p/>
+ * For basic collections and many-to-many mappings the <i>collection
table</i> is a distinct ERD entity, and we
+ * must generate a reference to that table (in contrast, the <i>collection
table</i> for a one-to-many
+ * association is actually the entity table of the many side).
+ *
+ * @return The generated alias.
+ */
+ public String generateCollectionTableAlias() {
+ return base + "_";
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public boolean equals(Object o) {
+ if ( this == o ) {
+ return true;
+ }
+ if ( o == null || getClass() != o.getClass() ) {
+ return false;
+ }
+ TableAliasRoot aliasRoot = ( TableAliasRoot ) o;
+ return base.equals( aliasRoot.base );
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public int hashCode() {
+ return base.hashCode();
+ }
+ }
+
+ /**
+ * Generate the alias root for the given persister reference.
+ *
+ * @param persister The entity persister.
+ * @param sourceAlias The alias attached to the persister in the source query.
+ *
+ * @return The alias root.
+ */
+ public TableAliasRoot generateSqlAliasRoot(Queryable persister, String sourceAlias);
+
+ /**
+ * Generate the alias root for the given persister reference.
+ *
+ * @param persister The collection persister
+ * @param sourceAlias The alias attached to the persister in the source query.
+ *
+ * @return The alias root.
+ */
+ public TableAliasRoot generateSqlAliasRoot(QueryableCollection persister, String
sourceAlias);
+}
Deleted:
core/branches/SQL_GEN_REDESIGN/src/main/java/org/hibernate/sql/ast/phase/hql/normalize/AliasBuilder.java
===================================================================
---
core/branches/SQL_GEN_REDESIGN/src/main/java/org/hibernate/sql/ast/phase/hql/normalize/AliasBuilder.java 2009-02-02
17:15:35 UTC (rev 15856)
+++
core/branches/SQL_GEN_REDESIGN/src/main/java/org/hibernate/sql/ast/phase/hql/normalize/AliasBuilder.java 2009-02-02
20:24:25 UTC (rev 15857)
@@ -1,52 +0,0 @@
-/*
- * Hibernate, Relational Persistence for Idiomatic Java
- *
- * Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as
- * indicated by the @author tags or express copyright attribution
- * statements applied by the authors. All third-party contributions are
- * distributed under license by Red Hat Middleware LLC.
- *
- * 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.sql.ast.phase.hql.normalize;
-
-/**
- * Handles generating synthetic or implicit aliases.
- *
- * @author Steve Ebersole
- */
-public class AliasBuilder {
- private int unaliasedCount = 0;
-
- /**
- * Builds a unique implicit alias.
- *
- * @return The generated alias.
- */
- public synchronized String buildUniqueImplicitAlias() {
- return "<gen:" + unaliasedCount++ + ">";
- }
-
- /**
- * Determine if the given alias is implicit.
- *
- * @param alias The alias to check
- * @return True/false.
- */
- public static boolean isImplicitAlias(String alias) {
- return alias == null || ( alias.startsWith( "<gen:" ) &&
alias.endsWith( ">" ) );
- }
-}