Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 22 additions & 23 deletions hibernate-core/src/main/java/org/hibernate/mapping/Table.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import org.hibernate.dialect.Dialect;

import org.hibernate.resource.transaction.spi.DdlTransactionIsolator;
import org.jboss.logging.Logger;

import static java.util.Collections.emptyList;
import static java.util.Collections.singletonList;
Expand All @@ -46,7 +45,6 @@
* @author Gavin King
*/
public class Table implements Serializable, ContributableDatabaseObject {
private static final Logger LOG = Logger.getLogger( Table.class );
private static final Column[] EMPTY_COLUMN_ARRAY = new Column[0];

private final String contributor;
Expand Down Expand Up @@ -110,17 +108,19 @@ public Table(
String subselect,
boolean isAbstract) {
this.contributor = contributor;
this.catalog = namespace.getPhysicalName().catalog();
this.schema = namespace.getPhysicalName().schema();
final var physicalName = namespace.getPhysicalName();
this.catalog = physicalName.catalog();
this.schema = physicalName.schema();
this.name = physicalTableName;
this.subselect = subselect;
this.isAbstract = isAbstract;
}

public Table(String contributor, Namespace namespace, String subselect, boolean isAbstract) {
this.contributor = contributor;
this.catalog = namespace.getPhysicalName().catalog();
this.schema = namespace.getPhysicalName().schema();
final var physicalName = namespace.getPhysicalName();
this.catalog = physicalName.catalog();
this.schema = physicalName.schema();
this.subselect = subselect;
this.isAbstract = isAbstract;
}
Expand Down Expand Up @@ -247,7 +247,7 @@ public Column getColumn(Column column) {
return null;
}
else {
final Column existing = columns.get( column.getCanonicalName() );
final var existing = columns.get( column.getCanonicalName() );
return column.equals( existing ) ? existing : null;
}
}
Expand Down Expand Up @@ -276,15 +276,11 @@ public void addColumn(Column column) {
if ( oldColumn == null ) {
if ( primaryKey != null ) {
for ( var primaryKeyColumn : primaryKey.getColumns() ) {
if ( primaryKeyColumn.getCanonicalName().equals( column.getCanonicalName() ) ) {
if ( Objects.equals( column.getCanonicalName(),
primaryKeyColumn.getCanonicalName() ) ) {
// Force the column to be non-null
// as it is part of the primary key
column.setNullable( false );
if ( LOG.isTraceEnabled() ) {
LOG.tracef(
"Forcing column [%s] to be non-null as it is part of the primary key for table [%s]",
column.getCanonicalName(),
getNameIdentifier().getCanonicalName()
);
}
}
}
}
Expand Down Expand Up @@ -452,18 +448,21 @@ public void setPrimaryKey(PrimaryKey primaryKey) {
}

public Index getOrCreateIndex(String indexName) {
Index index = indexes.get( indexName );
if ( index == null ) {
index = new Index();
index.setName( indexName );
index.setTable( this );
indexes.put( indexName, index );
final var index = indexes.get( indexName );
if ( index != null ) {
return index;
}
else {
final var newIndex = new Index();
newIndex.setName( indexName );
newIndex.setTable( this );
indexes.put( indexName, newIndex );
return newIndex;
}
return index;
}

public Index getIndex(String indexName) {
return indexes.get( indexName );
return indexes.get( indexName );
}

public Index addIndex(Index index) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,29 @@ public final boolean next(RowProcessingState rowProcessingState) {

@Override
public boolean previous(RowProcessingState rowProcessingState) {
// NOTE : we do not even bother interacting with the query-cache put manager because
// this method is implicitly related to scrolling and caching of scrolled results
// is not supported
// NOTE: we do not even bother interacting with the query-cache put manager because
// this method is implicitly related to scrolling and caching of scrolled results
// is not supported
return processPrevious( rowProcessingState );
}

protected abstract boolean processPrevious(RowProcessingState rowProcessingState);

@Override
public boolean scroll(int numberOfRows, RowProcessingState rowProcessingState) {
// NOTE : we do not even bother interacting with the query-cache put manager because
// this method is implicitly related to scrolling and caching of scrolled results
// is not supported
// NOTE: we do not even bother interacting with the query-cache put manager because
// this method is implicitly related to scrolling and caching of scrolled results
// is not supported
return processScroll( numberOfRows, rowProcessingState );
}

protected abstract boolean processScroll(int numberOfRows, RowProcessingState rowProcessingState);

@Override
public boolean position(int position, RowProcessingState rowProcessingState) {
// NOTE : we do not even bother interacting with the query-cache put manager because
// this method is implicitly related to scrolling and caching of scrolled results
// is not supported
// NOTE: we do not even bother interacting with the query-cache put manager because
// this method is implicitly related to scrolling and caching of scrolled results
// is not supported
return processPosition( position, rowProcessingState );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import org.hibernate.type.descriptor.java.JavaType;
import org.hibernate.type.descriptor.jdbc.JdbcType;
import org.hibernate.type.descriptor.jdbc.JdbcTypeIndicators;
import org.hibernate.type.descriptor.jdbc.spi.JdbcTypeRegistry;
import org.hibernate.type.spi.TypeConfiguration;

/**
Expand Down Expand Up @@ -108,18 +107,18 @@ public int getResultCountEstimate() {
@Override
public <J> BasicType<J> resolveType(int position, JavaType<J> explicitJavaType, TypeConfiguration typeConfiguration) {
try {
final ResultSetMetaData metaData = getResultSetMetaData();
final JdbcTypeRegistry registry = typeConfiguration.getJdbcTypeRegistry();
final var metaData = getResultSetMetaData();
final var registry = typeConfiguration.getJdbcTypeRegistry();
final String columnTypeName = metaData.getColumnTypeName( position );
final int columnType = metaData.getColumnType( position );
final int scale = metaData.getScale( position );
final int precision = metaData.getPrecision( position );
final int displaySize = metaData.getColumnDisplaySize( position );
final Dialect dialect = getDialect();
final var dialect = getDialect();
final int length = dialect.resolveSqlTypeLength( columnTypeName, columnType, precision, scale, displaySize );
final JdbcType resolvedJdbcType =
final var resolvedJdbcType =
dialect.resolveSqlTypeDescriptor( columnTypeName, columnType, length, scale, registry );
final JdbcType jdbcType =
final var jdbcType =
explicitJavaType == null
? resolvedJdbcType
: jdbcType( explicitJavaType, resolvedJdbcType, length, precision, scale, typeConfiguration );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@

import java.io.Serializable;

import org.hibernate.internal.util.collections.ArrayHelper;
import org.hibernate.sql.results.jdbc.spi.JdbcValuesMetadata;
import org.hibernate.type.BasicType;
import org.hibernate.type.descriptor.java.JavaType;
import org.hibernate.type.spi.TypeConfiguration;

import static org.hibernate.internal.util.collections.ArrayHelper.indexOf;

public final class CachedJdbcValuesMetadata implements JdbcValuesMetadata, Serializable {
private final String[] columnNames;
private final BasicType<?>[] types;
Expand All @@ -28,7 +29,7 @@ public int getColumnCount() {

@Override
public int resolveColumnPosition(String columnName) {
final int position = ArrayHelper.indexOf( columnNames, columnName ) + 1;
final int position = indexOf( columnNames, columnName ) + 1;
if ( position == 0 ) {
throw new IllegalStateException( "Unexpected resolving of unavailable column: " + columnName );
}
Expand All @@ -49,7 +50,7 @@ public <J> BasicType<J> resolveType(
int position,
JavaType<J> explicitJavaType,
TypeConfiguration typeConfiguration) {
final BasicType<?> type = types[position - 1];
final var type = types[position - 1];
if ( type == null ) {
throw new IllegalStateException( "Unexpected resolving of unavailable column at position: " + position );
}
Expand All @@ -58,10 +59,8 @@ public <J> BasicType<J> resolveType(
return (BasicType<J>) type;
}
else {
return typeConfiguration.getBasicTypeRegistry().resolve(
explicitJavaType,
type.getJdbcType()
);
return typeConfiguration.getBasicTypeRegistry()
.resolve( explicitJavaType, type.getJdbcType() );
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,10 @@
import org.hibernate.dialect.Dialect;
import org.hibernate.dialect.pagination.LimitHandler;
import org.hibernate.dialect.pagination.NoopLimitHandler;
import org.hibernate.engine.jdbc.spi.JdbcCoordinator;
import org.hibernate.engine.jdbc.spi.JdbcServices;
import org.hibernate.engine.jdbc.spi.SqlStatementLogger;
import org.hibernate.engine.spi.SessionEventListenerManager;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.event.monitor.spi.DiagnosticEvent;
import org.hibernate.event.monitor.spi.EventMonitor;
import org.hibernate.query.spi.Limit;
import org.hibernate.query.spi.QueryOptions;
import org.hibernate.resource.jdbc.spi.JdbcSessionContext;
Expand Down Expand Up @@ -62,7 +58,7 @@ public DeferredResultSetAccess(
JdbcSelectExecutor.StatementCreator statementCreator,
int resultCountEstimate) {
super( executionContext.getSession() );
final JdbcServices jdbcServices = executionContext.getSession().getJdbcServices();
final var jdbcServices = executionContext.getSession().getJdbcServices();

this.jdbcParameterBindings = jdbcParameterBindings;
this.executionContext = executionContext;
Expand All @@ -71,7 +67,7 @@ public DeferredResultSetAccess(
this.sqlStatementLogger = jdbcServices.getSqlStatementLogger();
this.resultCountEstimate = resultCountEstimate;

final QueryOptions queryOptions = executionContext.getQueryOptions();
final var queryOptions = executionContext.getQueryOptions();
if ( queryOptions == null ) {
finalSql = jdbcSelect.getSqlString();
limit = null;
Expand All @@ -80,41 +76,54 @@ public DeferredResultSetAccess(
else {
// Note that limit and lock aren't set for SQM as that is applied during SQL rendering
// But for native queries, we have to adapt the SQL string
final Dialect dialect = jdbcServices.getDialect();
final var dialect = jdbcServices.getDialect();

final String sql = jdbcSelect.getSqlString();

limit = queryOptions.getLimit();
final boolean needsLimitHandler = needsLimitHandler( jdbcSelect );
limitHandler = needsLimitHandler ? dialect.getLimitHandler() : NoopLimitHandler.NO_LIMIT;
final String sqlWithLimit = !needsLimitHandler ? sql : limitHandler.processSql(
sql,
jdbcParameterBindings.getBindings().size(),
jdbcServices.getParameterMarkerStrategy(),
queryOptions
);
final String sqlWithLimit =
needsLimitHandler
? sqlWithLimit( jdbcParameterBindings, sql, jdbcServices, queryOptions )
: sql;

final String sqlWithLocking =
sqlWithLocking( jdbcSelect.getLockStrategy(), sqlWithLimit, queryOptions, dialect );

finalSql =
dialect.addSqlHintOrComment( sqlWithLocking, queryOptions,
executionContext.getSession().getFactory()
.getSessionFactoryOptions().isCommentsEnabled() );
}
}

final var lockOptions = queryOptions.getLockOptions();
final var jdbcLockStrategy = jdbcSelect.getLockStrategy();
final String sqlWithLocking;
if ( hasLocking( jdbcLockStrategy, lockOptions ) ) {
final boolean usesFollowOnLocking = useFollowOnLocking( jdbcLockStrategy, sqlWithLimit, queryOptions, lockOptions, dialect );
if ( usesFollowOnLocking ) {
sqlWithLocking = sqlWithLimit;
}
else {
sqlWithLocking = dialect.applyLocksToSql( sqlWithLimit, lockOptions, emptyMap() );
}
}
else {
sqlWithLocking = sqlWithLimit;
}
private String sqlWithLimit(
JdbcParameterBindings jdbcParameterBindings,
String sql,
JdbcServices jdbcServices,
QueryOptions queryOptions) {
return limitHandler.processSql(
sql,
jdbcParameterBindings.getBindings().size(),
jdbcServices.getParameterMarkerStrategy(),
queryOptions
);
}

final boolean commentsEnabled = executionContext.getSession()
.getFactory()
.getSessionFactoryOptions()
.isCommentsEnabled();
finalSql = dialect.addSqlHintOrComment( sqlWithLocking, queryOptions, commentsEnabled );
private static String sqlWithLocking(
JdbcLockStrategy jdbcLockStrategy,
String sqlWithLimit,
QueryOptions queryOptions,
Dialect dialect) {
final var lockOptions = queryOptions.getLockOptions();
if ( hasLocking( jdbcLockStrategy, lockOptions ) ) {
return useFollowOnLocking( jdbcLockStrategy, sqlWithLimit, queryOptions, lockOptions, dialect )
? sqlWithLimit
: dialect.applyLocksToSql( sqlWithLimit, lockOptions, emptyMap() );
}
else {
return sqlWithLimit;
}
}

Expand Down Expand Up @@ -221,7 +230,7 @@ protected void bindParameters(PreparedStatement preparedStatement) throws SQLExc
}

private void setQueryOptions(PreparedStatement preparedStatement) throws SQLException {
final QueryOptions queryOptions = executionContext.getQueryOptions();
final var queryOptions = executionContext.getQueryOptions();
// set options
if ( queryOptions != null ) {
final Integer fetchSize = queryOptions.getFetchSize();
Expand All @@ -238,10 +247,10 @@ private void setQueryOptions(PreparedStatement preparedStatement) throws SQLExce
}

private void executeQuery() {
final LogicalConnectionImplementor logicalConnection =
final var logicalConnection =
getPersistenceContext().getJdbcCoordinator().getLogicalConnection();

final SharedSessionContractImplementor session = executionContext.getSession();
final var session = executionContext.getSession();
try {
CORE_LOGGER.tracef( "Executing query to retrieve ResultSet: %s", finalSql );
// prepare the query
Expand All @@ -254,8 +263,8 @@ private void executeQuery() {
if ( sqlStatementLogger.getLogSlowQuery() > 0 ) {
executeStartNanos = System.nanoTime();
}
final EventMonitor eventMonitor = session.getEventMonitor();
final DiagnosticEvent jdbcPreparedStatementExecutionEvent =
final var eventMonitor = session.getEventMonitor();
final var jdbcPreparedStatementExecutionEvent =
eventMonitor.beginJdbcPreparedStatementExecutionEvent();
try {
eventListenerManager.jdbcExecuteStatementStart();
Expand Down Expand Up @@ -326,8 +335,7 @@ protected LockMode determineFollowOnLockMode(LockOptions lockOptions) {

@Override
public void release() {
final JdbcCoordinator jdbcCoordinator =
getPersistenceContext().getJdbcCoordinator();
final var jdbcCoordinator = getPersistenceContext().getJdbcCoordinator();
final LogicalConnectionImplementor logicalConnection = jdbcCoordinator.getLogicalConnection();
if ( resultSet != null ) {
logicalConnection.getResourceRegistry().release( resultSet, preparedStatement );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ public DirectResultSetAccess(
this.resultSetSource = resultSetSource;
this.resultSet = resultSet;

persistenceContext.getJdbcCoordinator().getLogicalConnection().getResourceRegistry()
persistenceContext.getJdbcCoordinator()
.getLogicalConnection()
.getResourceRegistry()
.register( resultSet, resultSetSource );
}

Expand Down
Loading
Loading