HHH-15461 Replace uses of method references for SqlAppender with proper implementation

This commit is contained in:
Christian Beikov 2022-08-23 14:47:31 +02:00
parent 84c276b3cc
commit 271cba0603
16 changed files with 101 additions and 50 deletions

View File

@ -162,7 +162,7 @@ public class InformixDialect extends Dialect {
ddlTypeRegistry.addDescriptor(
CapacityDependentDdlType.builder( NVARCHAR, columnType( LONG32NVARCHAR ), this )
.withTypeCapacity( 255, "varchar($l)" )
.withTypeCapacity( getMaxVarcharLength(), columnType( NVARCHAR ) )
.withTypeCapacity( getMaxNVarcharLength(), columnType( NVARCHAR ) )
.build()
);
}

View File

@ -10,7 +10,6 @@ import java.util.Collections;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.engine.query.internal.NativeQueryInterpreterStandardImpl;
import org.hibernate.engine.query.spi.NativeQueryInterpreter;
import org.hibernate.metamodel.model.domain.internal.JpaMetamodelImpl;
import org.hibernate.orm.test.jpa.JpaComplianceStub;
import org.hibernate.query.criteria.ValueHandlingMode;
@ -20,6 +19,7 @@ import org.hibernate.query.sqm.function.SelfRenderingSqmFunction;
import org.hibernate.query.sqm.function.SqmFunctionDescriptor;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.sql.ast.spi.SqlAppender;
import org.hibernate.sql.ast.spi.StringBuilderSqlAppender;
import org.hibernate.type.BasicType;
import org.hibernate.type.descriptor.java.JdbcDateJavaType;
import org.hibernate.type.descriptor.java.JdbcTimestampJavaType;
@ -121,26 +121,4 @@ public class InformixDialectTestCase extends BaseUnitTestCase {
assertEquals( "today", appender.toString() );
}
private static class StringBuilderSqlAppender implements SqlAppender {
private final StringBuilder sb;
public StringBuilderSqlAppender() {
this.sb = new StringBuilder();
}
@Override
public void appendSql(String fragment) {
sb.append( fragment );
}
@Override
public void appendSql(char fragment) {
sb.append( fragment );
}
@Override
public String toString() {
return sb.toString();
}
}
}

View File

@ -133,6 +133,7 @@ import org.hibernate.sql.ast.SqlAstNodeRenderingMode;
import org.hibernate.sql.ast.SqlAstTranslatorFactory;
import org.hibernate.sql.ast.spi.SqlAppender;
import org.hibernate.sql.ast.spi.StandardSqlAstTranslatorFactory;
import org.hibernate.sql.ast.spi.StringBuilderSqlAppender;
import org.hibernate.tool.schema.extract.internal.SequenceInformationExtractorLegacyImpl;
import org.hibernate.tool.schema.extract.internal.SequenceInformationExtractorNoOpImpl;
import org.hibernate.tool.schema.extract.spi.SequenceInformationExtractor;
@ -2265,7 +2266,7 @@ public abstract class Dialect implements ConversionContext {
*/
public String toBooleanValueString(boolean bool) {
final StringBuilder sb = new StringBuilder();
appendBooleanValueString( sb::append, bool );
appendBooleanValueString( new StringBuilderSqlAppender( sb ), bool );
return sb.toString();
}
@ -3499,7 +3500,7 @@ public abstract class Dialect implements ConversionContext {
*/
public String inlineLiteral(String literal) {
final StringBuilder sb = new StringBuilder( literal.length() + 2 );
appendLiteral( sb::append, literal );
appendLiteral( new StringBuilderSqlAppender( sb ), literal );
return sb.toString();
}

View File

@ -341,7 +341,7 @@ public class SybaseASESqlAstTranslator<T extends JdbcOperation> extends Abstract
if ( currentQuerySpec != null && !currentQuerySpec.isRoot()
&& (roots = currentQuerySpec.getFromClause().getRoots()).size() == 1
&& roots.get( 0 ).getPrimaryTableReference() instanceof UnionTableReference ) {
appendSql( columnReference.getExpressionText() );
columnReference.appendReadExpression( this );
}
// for now, use the unqualified form
else if ( columnReference.isColumnExpressionFormula() ) {
@ -359,7 +359,7 @@ public class SybaseASESqlAstTranslator<T extends JdbcOperation> extends Abstract
}
}
else {
appendSql( columnReference.getExpressionText() );
columnReference.appendReadExpression( this );
}
}

View File

@ -34,6 +34,7 @@ import org.hibernate.query.sqm.sql.SqmToSqlAstConverter;
import org.hibernate.query.sqm.tree.SqmTypedNode;
import org.hibernate.sql.ast.SqlAstTranslator;
import org.hibernate.sql.ast.spi.SqlAppender;
import org.hibernate.sql.ast.spi.StringBuilderSqlAppender;
import org.hibernate.sql.ast.tree.SqlAstNode;
import org.hibernate.sql.ast.tree.expression.BinaryArithmeticExpression;
import org.hibernate.sql.ast.tree.expression.CaseSearchedExpression;
@ -207,9 +208,10 @@ public class FormatFunction extends AbstractSqmFunctionDescriptor implements Fun
.getDialect();
Expression formatExpression = null;
final StringBuilder sb = new StringBuilder();
final StringBuilderSqlAppender sqlAppender = new StringBuilderSqlAppender( sb );
final String delimiter;
if ( supportsPatternLiterals ) {
dialect.appendDatetimeFormat( sb::append, "'a'" );
dialect.appendDatetimeFormat( sqlAppender, "'a'" );
delimiter = sb.substring( 0, sb.indexOf( "a" ) ).replace( "''", "'" );
}
else {
@ -241,7 +243,7 @@ public class FormatFunction extends AbstractSqmFunctionDescriptor implements Fun
continue;
}
sb.setLength( 0 );
dialect.appendDatetimeFormat( sb::append, smallParts[l] );
dialect.appendDatetimeFormat( sqlAppender, smallParts[l] );
final String formatPart = sb.toString();
if ( supportsPatternLiterals ) {
formatExpression = concat(
@ -333,7 +335,7 @@ public class FormatFunction extends AbstractSqmFunctionDescriptor implements Fun
final String formatLiteralPart;
if ( supportsPatternLiterals ) {
sb.setLength( 0 );
dialect.appendDatetimeFormat( sb::append, "'" + chunks[i + 1] + "'" );
dialect.appendDatetimeFormat( sqlAppender, "'" + chunks[i + 1] + "'" );
formatLiteralPart = sb.toString().replace( "''", "'" );
}
else {

View File

@ -309,11 +309,7 @@ public abstract class AbstractNaturalIdLoader<T> implements NaturalIdLoader<T> {
SqlExpressionResolver.createColumnReferenceKey( tableReference, selectableMapping.getSelectionExpression() ),
(processingState) -> new ColumnReference(
tableReference,
selectableMapping.getSelectionExpression(),
selectableMapping.isFormula(),
selectableMapping.getCustomReadExpression(),
selectableMapping.getCustomWriteExpression(),
selectableMapping.getJdbcMapping(),
selectableMapping,
sessionFactory
)
);

View File

@ -76,7 +76,7 @@ public class LoaderSqlAstCreationState
this.sf = sf;
this.processingState = new SqlAstQueryPartProcessingStateImpl(
queryPart,
this,
null,
this,
() -> Clause.IRRELEVANT,
true

View File

@ -51,6 +51,7 @@ import org.hibernate.sql.ast.spi.FromClauseAccess;
import org.hibernate.sql.ast.spi.SqlAliasBase;
import org.hibernate.sql.ast.spi.SqlAstCreationContext;
import org.hibernate.sql.ast.spi.SqlExpressionResolver;
import org.hibernate.sql.ast.spi.StringBuilderSqlAppender;
import org.hibernate.sql.ast.tree.expression.ColumnReference;
import org.hibernate.sql.ast.tree.from.NamedTableReference;
import org.hibernate.sql.ast.tree.from.TableGroup;
@ -520,6 +521,7 @@ public class UnionSubclassEntityPersister extends AbstractEntityPersister {
// Create a union sub-query for the table names, like generateSubquery(PersistentClass model, Mapping mapping)
final StringBuilder buf = new StringBuilder( subquery.length() )
.append( "( " );
final StringBuilderSqlAppender sqlAppender = new StringBuilderSqlAppender( buf );
for ( String name : getSubclassEntityNames() ) {
final AbstractEntityPersister persister = (AbstractEntityPersister) metamodel.findEntityDescriptor( name );
@ -536,9 +538,7 @@ public class UnionSubclassEntityPersister extends AbstractEntityPersister {
buf.append( dialect.getSelectClauseNullString( sqlType, getFactory().getTypeConfiguration() ) )
.append( " as " );
}
buf.append(
new ColumnReference( (String) null, selectableMapping, getFactory() ).getExpressionText()
);
new ColumnReference( (String) null, selectableMapping, getFactory() ).appendReadExpression( sqlAppender );
buf.append( ", " );
}
buf.append( persister.getDiscriminatorSQLValue() ).append( " as clazz_" );

View File

@ -4285,7 +4285,7 @@ public abstract class AbstractSqlAstTranslator<T extends JdbcOperation> implemen
}
}
else {
appendSql( columnReference.getExpressionText() );
columnReference.appendReadExpression( this );
}
}

View File

@ -0,0 +1,68 @@
/*
* 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;
/**
* Access to appending SQL fragments to a StringBuilder
*/
public class StringBuilderSqlAppender implements SqlAppender {
private final StringBuilder sb;
public StringBuilderSqlAppender() {
this(new StringBuilder());
}
public StringBuilderSqlAppender(StringBuilder sb) {
this.sb = sb;
}
@Override
public void appendSql(String fragment) {
sb.append( fragment );
}
@Override
public void appendSql(char fragment) {
sb.append( fragment );
}
@Override
public void appendSql(int value) {
sb.append( value );
}
@Override
public void appendSql(long value) {
sb.append( value );
}
@Override
public void appendSql(boolean value) {
sb.append( value );
}
@Override
public Appendable append(CharSequence csq) {
return sb.append( csq );
}
@Override
public Appendable append(CharSequence csq, int start, int end) {
return sb.append( csq, start, end );
}
@Override
public Appendable append(char c) {
return sb.append( c );
}
@Override
public String toString() {
return sb.toString();
}
}

View File

@ -18,6 +18,7 @@ import org.hibernate.metamodel.mapping.JdbcMapping;
import org.hibernate.metamodel.mapping.MappingModelExpressible;
import org.hibernate.sql.Template;
import org.hibernate.sql.ast.SqlAstWalker;
import org.hibernate.sql.ast.spi.SqlAppender;
import org.hibernate.sql.ast.tree.from.TableReference;
import org.hibernate.sql.ast.tree.update.Assignable;
@ -190,6 +191,10 @@ public class ColumnReference implements Expression, Assignable {
return readExpression;
}
public void appendReadExpression(SqlAppender appender) {
appender.append( getExpressionText() );
}
public String renderSqlFragment(SessionFactoryImplementor sessionFactory) {
return getExpressionText();
}

View File

@ -12,9 +12,7 @@ import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.hibernate.dialect.Dialect;
import org.hibernate.metamodel.model.convert.spi.BasicValueConverter;
import org.hibernate.sql.ast.spi.SqlAppender;
import org.hibernate.type.descriptor.ValueBinder;
import org.hibernate.type.descriptor.ValueExtractor;
import org.hibernate.type.descriptor.WrapperOptions;

View File

@ -18,6 +18,7 @@ import org.hibernate.engine.jdbc.internal.BinaryStreamImpl;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.internal.util.compare.RowVersionComparator;
import org.hibernate.sql.ast.spi.SqlAppender;
import org.hibernate.sql.ast.spi.StringBuilderSqlAppender;
import org.hibernate.type.descriptor.WrapperOptions;
/**
@ -50,9 +51,9 @@ public class PrimitiveByteArrayJavaType extends AbstractClassJavaType<byte[]>
}
public String toString(byte[] bytes) {
final StringBuilder buf = new StringBuilder( bytes.length * 2 );
appendString( buf::append, bytes );
return buf.toString();
final StringBuilder sb = new StringBuilder( bytes.length * 2 );
appendString( new StringBuilderSqlAppender( sb ), bytes );
return sb.toString();
}
public void appendString(SqlAppender appender, byte[] bytes) {

View File

@ -10,6 +10,7 @@ import java.io.Serializable;
import org.hibernate.dialect.Dialect;
import org.hibernate.sql.ast.spi.SqlAppender;
import org.hibernate.sql.ast.spi.StringBuilderSqlAppender;
import org.hibernate.type.descriptor.WrapperOptions;
import org.hibernate.type.descriptor.java.JavaType;
@ -33,7 +34,7 @@ public interface JdbcLiteralFormatter<T> extends Serializable {
*/
default String toJdbcLiteral(T value, Dialect dialect, WrapperOptions wrapperOptions) {
final StringBuilder sb = new StringBuilder();
appendJdbcLiteral( sb::append, value, dialect, wrapperOptions );
appendJdbcLiteral( new StringBuilderSqlAppender( sb ), value, dialect, wrapperOptions );
return sb.toString();
}

View File

@ -184,7 +184,7 @@ public class SmokeTests {
final Expression selectedExpression = sqlSelection.getExpression();
assertThat( selectedExpression, instanceOf( ColumnReference.class ) );
final ColumnReference columnReference = (ColumnReference) selectedExpression;
assertThat( columnReference.renderSqlFragment( scope.getSessionFactory() ), is( "s1_0.gender" ) );
assertThat( columnReference.getExpressionText(), is( "s1_0.gender" ) );
final JdbcMappingContainer selectedExpressible = selectedExpression.getExpressionType();
assertThat( selectedExpressible, instanceOf( CustomType.class ) );

View File

@ -24,6 +24,7 @@ import org.hibernate.dialect.SybaseDialect;
import org.hibernate.dialect.TimeZoneSupport;
import org.hibernate.dialect.TiDBDialect;
import org.hibernate.query.sqm.FetchClauseType;
import org.hibernate.sql.ast.spi.StringBuilderSqlAppender;
import org.hibernate.testing.DialectCheck;
@ -330,7 +331,7 @@ abstract public class DialectFeatureChecks {
public static class SupportsFormat implements DialectFeatureCheck {
public boolean apply(Dialect dialect) {
try {
dialect.appendDatetimeFormat( new StringBuilder()::append, "" );
dialect.appendDatetimeFormat( new StringBuilderSqlAppender(), "" );
return true;
}
catch (Exception ex) {
@ -342,7 +343,7 @@ abstract public class DialectFeatureChecks {
public static class SupportsTruncateThroughCast implements DialectFeatureCheck {
public boolean apply(Dialect dialect) {
try {
dialect.appendDatetimeFormat( new StringBuilder()::append, "" );
dialect.appendDatetimeFormat( new StringBuilderSqlAppender(), "" );
return true;
}
catch (Exception ex) {