Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package org.hibernate.metamodel.mapping;

import org.checkerframework.checker.nullness.qual.Nullable;
import org.hibernate.engine.jdbc.Size;

import java.util.function.BiConsumer;
import java.util.function.IntFunction;
Expand All @@ -27,7 +28,7 @@ public interface SelectableConsumer {

/**
* Simple form of visitation over a number of columns by name, using
* a separate {@link SelectableMappings} as a base for additional details.
* a separate {@link JdbcMappingContainer} as a base for additional details.
* <p>
* Intended for use in visiting table keys, where we know JdbcMappings, etc.
* from the identifier.
Expand All @@ -46,6 +47,149 @@ default void accept(String tableName, JdbcMappingContainer base, String[] column
mutableSelectableMapping.forEach( this::accept );
}

/**
* Simple form of visitation over a number of columns by name, using
* a separate {@link SelectableMappings} as a base for additional details.
* <p>
* Intended for use in visiting table keys, where we know JdbcMappings, etc.
* from the identifier.
* <p>
* The expectation here is for the following details to be available:<ul>
* <li>{@link SelectableMapping#getContainingTableExpression()}</li>
* <li>{@link SelectableMapping#getSelectionExpression()} (the column name)</li>
* <li>{@link SelectableMapping#getWriteExpression()}</li>
* <li>{@link SelectableMapping#getJdbcMapping()}</li>
* </ul>
*/
default void accept(SelectableMappings base, String tableName, String[] columnNames) {
class SelectableMappingIterator implements SelectableMapping {
private final String tableName;
private final SelectableMappings delegate;
private final String[] columnNames;

private int index;

public SelectableMappingIterator(String tableName, SelectableMappings delegate, String[] columnNames) {
this.tableName = tableName;
this.delegate = delegate;
this.columnNames = columnNames;
assert delegate.getJdbcTypeCount() == columnNames.length;
}

private void forEach(BiConsumer<Integer,SelectableMapping> consumer) {
for ( index = 0; index < columnNames.length; index++ ) {
consumer.accept( index, this );
}
}

@Override
public String getContainingTableExpression() {
return tableName;
}

@Override
public String getSelectionExpression() {
return columnNames[index];
}

@Override
public @Nullable String getCustomReadExpression() {
return null;
}

@Override
public @Nullable String getCustomWriteExpression() {
return null;
}

private SelectableMapping getDelegate() {
return delegate.getSelectable( index );
}

@Override
public String getSelectableName() {
return getDelegate().getSelectableName();
}

@Override
public SelectablePath getSelectablePath() {
return getDelegate().getSelectablePath();
}

@Override
public boolean isFormula() {
return getDelegate().isFormula();
}

@Override
public boolean isNullable() {
return getDelegate().isNullable();
}

@Override
public boolean isInsertable() {
return getDelegate().isInsertable();
}

@Override
public boolean isUpdateable() {
return getDelegate().isUpdateable();
}

@Override
public boolean isPartitioned() {
return getDelegate().isPartitioned();
}

@Override
public @Nullable String getColumnDefinition() {
return getDelegate().getColumnDefinition();
}

@Override
public @Nullable Long getLength() {
return getDelegate().getLength();
}

@Override
public @Nullable Integer getArrayLength() {
return getDelegate().getArrayLength();
}

@Override
public @Nullable Integer getPrecision() {
return getDelegate().getPrecision();
}

@Override
public @Nullable Integer getScale() {
return getDelegate().getScale();
}

@Override
public @Nullable Integer getTemporalPrecision() {
return getDelegate().getTemporalPrecision();
}

@Override
public boolean isLob() {
return getDelegate().isLob();
}

@Override
public JdbcMapping getJdbcMapping() {
return getDelegate().getJdbcMapping();
}

@Override
public Size toSize() {
return getDelegate().toSize();
}
}

final SelectableMappingIterator mutableSelectableMapping = new SelectableMappingIterator( tableName, base, columnNames );
mutableSelectableMapping.forEach( this::accept );
}

class MutableSelectableMapping implements SelectableMapping {
private final String tableName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ public class SelectableMappingImpl extends SqlTypedMappingImpl implements Select
private final String containingTableExpression;
private final String selectionExpression;
private final SelectablePath selectablePath;
private final String customReadExpression;
private final String customWriteExpression;
private final @Nullable String customReadExpression;
private final @Nullable String customWriteExpression;
private final boolean isLob;
private final boolean nullable;
private final boolean insertable;
Expand All @@ -37,14 +37,14 @@ public class SelectableMappingImpl extends SqlTypedMappingImpl implements Select
public SelectableMappingImpl(
String containingTableExpression,
String selectionExpression,
SelectablePath selectablePath,
String customReadExpression,
String customWriteExpression,
String columnDefinition,
Long length,
Integer precision,
Integer scale,
Integer temporalPrecision,
@Nullable SelectablePath selectablePath,
@Nullable String customReadExpression,
@Nullable String customWriteExpression,
@Nullable String columnDefinition,
@Nullable Long length,
@Nullable Integer precision,
@Nullable Integer scale,
@Nullable Integer temporalPrecision,
boolean isLob,
boolean nullable,
boolean insertable,
Expand Down Expand Up @@ -77,15 +77,15 @@ public SelectableMappingImpl(
public SelectableMappingImpl(
String containingTableExpression,
String selectionExpression,
SelectablePath selectablePath,
String customReadExpression,
String customWriteExpression,
String columnDefinition,
Long length,
Integer arrayLength,
Integer precision,
Integer scale,
Integer temporalPrecision,
@Nullable SelectablePath selectablePath,
@Nullable String customReadExpression,
@Nullable String customWriteExpression,
@Nullable String columnDefinition,
@Nullable Long length,
@Nullable Integer arrayLength,
@Nullable Integer precision,
@Nullable Integer scale,
@Nullable Integer temporalPrecision,
boolean isLob,
boolean nullable,
boolean insertable,
Expand All @@ -96,7 +96,7 @@ public SelectableMappingImpl(
super( columnDefinition, length, arrayLength, precision, scale, temporalPrecision, jdbcMapping );
assert selectionExpression != null;
// Save memory by using interned strings. Probability is high that we have multiple duplicate strings
this.containingTableExpression = containingTableExpression == null ? null : containingTableExpression.intern();
this.containingTableExpression = containingTableExpression.intern();
this.selectionExpression = selectionExpression.intern();
this.selectablePath = selectablePath == null ? new SelectablePath( selectionExpression ) : selectablePath;
this.customReadExpression = customReadExpression == null ? null : customReadExpression.intern();
Expand Down Expand Up @@ -286,7 +286,7 @@ public String getSelectionExpression() {

@Override
public String getSelectableName() {
return selectablePath == null ? null : selectablePath.getSelectableName();
return selectablePath.getSelectableName();
}

@Override
Expand All @@ -295,17 +295,12 @@ public SelectablePath getSelectablePath() {
}

@Override
public String getCustomReadExpression() {
public @Nullable String getCustomReadExpression() {
return customReadExpression;
}

@Override
public String getCustomWriteExpression() {
return customWriteExpression;
}

@Override
public String getWriteExpression() {
public @Nullable String getCustomWriteExpression() {
return customWriteExpression;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,17 +210,17 @@ public Predicate createNonDeletedRestriction(TableReference tableReference, SqlE
public ColumnValueBinding createNonDeletedValueBinding(ColumnReference softDeleteColumnReference) {
final var nonDeletedFragment =
strategy == SoftDeleteType.TIMESTAMP
? new ColumnWriteFragment( null, emptyList(), jdbcMapping )
: new ColumnWriteFragment( nonDeletedLiteralText, emptyList(), jdbcMapping );
? new ColumnWriteFragment( null, emptyList(), this )
: new ColumnWriteFragment( nonDeletedLiteralText, emptyList(), this );
return new ColumnValueBinding( softDeleteColumnReference, nonDeletedFragment );
}

@Override
public ColumnValueBinding createDeletedValueBinding(ColumnReference softDeleteColumnReference) {
final ColumnWriteFragment deletedFragment =
strategy == SoftDeleteType.TIMESTAMP
? new ColumnWriteFragment( currentTimestampFunctionName, emptyList(), getJdbcMapping() )
: new ColumnWriteFragment( deletedLiteralText, emptyList(), jdbcMapping );
? new ColumnWriteFragment( currentTimestampFunctionName, emptyList(), this )
: new ColumnWriteFragment( deletedLiteralText, emptyList(), this );
return new ColumnValueBinding( softDeleteColumnReference, deletedFragment );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1739,20 +1739,20 @@ protected void applyKeyRestrictions(
List<ColumnValueBinding> restrictionBindings) {
final var foreignKeyDescriptor = getAttributeMapping().getKeyDescriptor();
assert foreignKeyDescriptor != null;
foreignKeyDescriptor.getKeyPart().forEachSelectable( parameterList );
for ( var columnValueParameter : parameterList ) {
foreignKeyDescriptor.getKeyPart().forEachSelectable( (selectionIndex, selectableMapping) -> {
final var columnValueParameter = parameterList.addColumValueParameter( selectableMapping );
final var columnReference = columnValueParameter.getColumnReference();
restrictionBindings.add(
new ColumnValueBinding(
columnReference,
new ColumnWriteFragment(
"?",
columnValueParameter,
columnReference.getJdbcMapping()
selectableMapping
)
)
);
}
});
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,34 +296,34 @@ public RestrictedTableMutation<JdbcMutationOperation> generateDeleteAllAst(Mutat
new ColumnValueParameterList( tableReference, ParameterUsage.RESTRICT, keyColumnCount );
final List<ColumnValueBinding> keyRestrictionBindings = arrayList( keyColumnCount );
final List<ColumnValueBinding> valueBindings = arrayList( valuesCount );
foreignKeyDescriptor.getKeyPart().forEachSelectable( parameterBinders );
for ( var columnValueParameter : parameterBinders ) {
foreignKeyDescriptor.getKeyPart().forEachSelectable( (selectionIndex, selectableMapping) -> {
final var columnValueParameter = parameterBinders.addColumValueParameter( selectableMapping );
final var columnReference = columnValueParameter.getColumnReference();
keyRestrictionBindings.add(
new ColumnValueBinding(
columnReference,
new ColumnWriteFragment(
"?",
columnValueParameter,
columnReference.getJdbcMapping()
selectableMapping
)
)
);
valueBindings.add(
new ColumnValueBinding(
columnReference,
new ColumnWriteFragment( "null", columnReference.getJdbcMapping() )
new ColumnWriteFragment( "null", selectableMapping )
)
);
}
} );

if ( hasIndex() && !indexContainsFormula ) {
attributeMapping.getIndexDescriptor().forEachSelectable( (selectionIndex, selectableMapping) -> {
if ( selectableMapping.isUpdateable() ) {
valueBindings.add(
new ColumnValueBinding(
new ColumnReference( tableReference, selectableMapping ),
new ColumnWriteFragment( "null", selectableMapping.getJdbcMapping() )
new ColumnWriteFragment( "null", selectableMapping )
)
);
}
Expand Down Expand Up @@ -484,10 +484,8 @@ public RestrictedTableMutation<JdbcMutationOperation> generateDeleteRowAst(Mutat
if ( selectable.isUpdateable() ) {
// set null
updateBuilder.addValueColumn(
selectable.getSelectionExpression(),
NULL,
selectable.getJdbcMapping(),
selectable.isLob()
selectable
);
}
// restrict
Expand All @@ -504,10 +502,8 @@ public RestrictedTableMutation<JdbcMutationOperation> generateDeleteRowAst(Mutat
final var selectable = indexDescriptor.getSelectable( i );
if ( selectable.isUpdateable() ) {
updateBuilder.addValueColumn(
selectable.getSelectionExpression(),
NULL,
selectable.getJdbcMapping(),
selectable.isLob()
selectable
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3356,10 +3356,7 @@ protected EntityTableMapping[] buildTableMappings() {
.accept( (selectionIndex, selectableMapping) -> {
keyColumns.add( new EntityTableMapping.KeyColumn(
tableExpression,
selectableMapping.getSelectionExpression(),
selectableMapping.getWriteExpression(),
selectableMapping.isFormula(),
selectableMapping.getJdbcMapping()
selectableMapping
) );
} );

Expand Down
Loading