7676import org .hibernate .persister .entity .EntityPersister ;
7777import org .hibernate .persister .entity .Joinable ;
7878import org .hibernate .persister .internal .SqlFragmentPredicate ;
79- import org .hibernate .pretty .MessageHelper ;
8079import org .hibernate .query .named .NamedQueryMemento ;
8180import org .hibernate .query .spi .QueryOptions ;
8281import org .hibernate .spi .NavigablePath ;
8887import org .hibernate .sql .ast .spi .SqlAliasBaseConstant ;
8988import org .hibernate .sql .ast .spi .SqlAliasBaseManager ;
9089import org .hibernate .sql .ast .spi .SqlAstCreationState ;
90+ import org .hibernate .sql .ast .spi .SqlSelection ;
9191import org .hibernate .sql .ast .tree .expression .AliasedExpression ;
9292import org .hibernate .sql .ast .tree .from .TableGroup ;
9393import org .hibernate .sql .ast .tree .from .TableReference ;
134134import static org .hibernate .internal .util .collections .CollectionHelper .arrayList ;
135135import static org .hibernate .jdbc .Expectations .createExpectation ;
136136import static org .hibernate .metamodel .mapping .internal .MappingModelCreationHelper .getTableIdentifierExpression ;
137+ import static org .hibernate .pretty .MessageHelper .collectionInfoString ;
137138import static org .hibernate .sql .Template .renderWhereStringTemplate ;
138139import static org .hibernate .sql .model .ModelMutationLogging .MODEL_MUTATION_LOGGER ;
139140
@@ -938,7 +939,7 @@ public String selectFragment(String alias, String columnSuffix) {
938939 rootQuerySpec ,
939940 new SqlAliasBaseManager (),
940941 new SimpleFromClauseAccessImpl (),
941- new LockOptions () ,
942+ LockOptions . NONE ,
942943 (fetchParent , creationState ) -> ImmutableFetchList .EMPTY ,
943944 true ,
944945 new LoadQueryInfluencers ( factory ),
@@ -964,52 +965,27 @@ public String selectFragment(String alias, String columnSuffix) {
964965 final var sqlSelections = rootQuerySpec .getSelectClause ().getSqlSelections ();
965966 int i = 0 ;
966967 for ( String keyAlias : keyColumnAliases ) {
967- sqlSelections .set (
968- i ,
969- new SqlSelectionImpl (
970- i ,
971- new AliasedExpression ( sqlSelections .get ( i ).getExpression (),
972- keyAlias + columnSuffix )
973- )
974- );
968+ sqlSelections .set ( i ,
969+ sqlSelection ( columnSuffix , keyAlias , i , sqlSelections ) );
975970 i ++;
976971 }
977972
978973 if ( hasIndex () ) {
979974 for ( String indexAlias : indexColumnAliases ) {
980- sqlSelections .set (
981- i ,
982- new SqlSelectionImpl (
983- i ,
984- new AliasedExpression ( sqlSelections .get ( i ).getExpression (),
985- indexAlias + columnSuffix )
986- )
987- );
975+ sqlSelections .set ( i ,
976+ sqlSelection ( columnSuffix , indexAlias , i , sqlSelections ) );
988977 i ++;
989978 }
990979 }
991980 if ( hasId () ) {
992- sqlSelections .set (
993- i ,
994- new SqlSelectionImpl (
995- i ,
996- new AliasedExpression ( sqlSelections .get ( i ).getExpression (),
997- identifierColumnAlias + columnSuffix )
998- )
999- );
981+ sqlSelections .set ( i ,
982+ sqlSelection ( columnSuffix , identifierColumnAlias , i , sqlSelections ) );
1000983 i ++;
1001984 }
1002985
1003986 for ( int columnIndex = 0 ; i < sqlSelections .size (); i ++, columnIndex ++ ) {
1004- final var sqlSelection = sqlSelections .get ( i );
1005- sqlSelections .set (
1006- i ,
1007- new SqlSelectionImpl (
1008- sqlSelection .getValuesArrayPosition (),
1009- new AliasedExpression ( sqlSelection .getExpression (),
1010- elementColumnAliases [columnIndex ] + columnSuffix )
1011- )
1012- );
987+ sqlSelections .set ( i ,
988+ sqlSelection ( columnSuffix , elementColumnAliases [columnIndex ], i , sqlSelections ) );
1013989 }
1014990
1015991 final String sql =
@@ -1018,9 +994,16 @@ public String selectFragment(String alias, String columnSuffix) {
1018994 .translate ( null , QueryOptions .NONE )
1019995 .getSqlString ();
1020996 final int fromIndex = sql .lastIndexOf ( " from" );
1021- return fromIndex != -1
1022- ? sql .substring ( "select " .length (), fromIndex )
1023- : sql .substring ( "select " .length () );
997+ final int selectLength = "select " .length ();
998+ return fromIndex < 0
999+ ? sql .substring ( selectLength )
1000+ : sql .substring ( selectLength , fromIndex );
1001+ }
1002+
1003+ private static SqlSelectionImpl sqlSelection (String columnSuffix , String keyAlias , int i , List <SqlSelection > sqlSelections ) {
1004+ return new SqlSelectionImpl ( sqlSelections .get ( i ).getValuesArrayPosition (),
1005+ new AliasedExpression ( sqlSelections .get ( i ).getExpression (),
1006+ keyAlias + columnSuffix ) );
10241007 }
10251008
10261009 protected String generateSelectSizeString (boolean isIntegerIndexed ) {
@@ -1378,7 +1361,7 @@ private void initCollectionPropertyMap(String aliasName, Type type, String[] col
13781361
13791362 //TODO: this code is almost certainly obsolete and can be removed
13801363 if ( type instanceof ComponentType || type instanceof AnyType ) {
1381- var compositeType = (CompositeType ) type ;
1364+ final var compositeType = (CompositeType ) type ;
13821365 final String [] propertyNames = compositeType .getPropertyNames ();
13831366 for ( int i = 0 ; i < propertyNames .length ; i ++ ) {
13841367 final String name = propertyNames [i ];
@@ -1387,6 +1370,12 @@ private void initCollectionPropertyMap(String aliasName, Type type, String[] col
13871370 }
13881371 }
13891372
1373+ int baseIndex () {
1374+ final int listIndexBase = getAttributeMapping ().getIndexMetadata ().getListIndexBase ();
1375+ //noinspection ManualMinMaxCalculation
1376+ return listIndexBase < 0 ? 0 : listIndexBase ;
1377+ }
1378+
13901379 @ Override
13911380 public int getSize (Object key , SharedSessionContractImplementor session ) {
13921381 try {
@@ -1397,8 +1386,7 @@ public int getSize(Object key, SharedSessionContractImplementor session) {
13971386 getKeyType ().nullSafeSet ( statement , key , 1 , session );
13981387 final var resultSet = jdbcCoordinator .getResultSetReturn ().extract ( statement , sqlSelectSizeString );
13991388 try {
1400- final int baseIndex = Math .max ( attributeMapping .getIndexMetadata ().getListIndexBase (), 0 );
1401- return resultSet .next () ? resultSet .getInt ( 1 ) - baseIndex : 0 ;
1389+ return resultSet .next () ? resultSet .getInt ( 1 ) - baseIndex () : 0 ;
14021390 }
14031391 finally {
14041392 resourceRegistry .release ( resultSet , statement );
@@ -1413,7 +1401,7 @@ public int getSize(Object key, SharedSessionContractImplementor session) {
14131401 throw getSQLExceptionHelper ().convert (
14141402 sqle ,
14151403 "could not retrieve collection size: " +
1416- MessageHelper . collectionInfoString ( this , key , getFactory () ),
1404+ collectionInfoString ( this , key , getFactory () ),
14171405 sqlSelectSizeString
14181406 );
14191407 }
@@ -1457,7 +1445,7 @@ private boolean exists(Object key, Object indexOrElement, Type indexOrElementTyp
14571445 throw getSQLExceptionHelper ().convert (
14581446 sqle ,
14591447 "could not check row existence: " +
1460- MessageHelper . collectionInfoString ( this , key , getFactory () ),
1448+ collectionInfoString ( this , key , getFactory () ),
14611449 sqlSelectSizeString
14621450 );
14631451 }
@@ -1652,37 +1640,57 @@ private static CollectionTableMapping buildCollectionTableMapping(
16521640 spaces ,
16531641 !collectionBootDescriptor .isOneToMany (),
16541642 collectionBootDescriptor .isInverse (),
1655- new MutationDetails (
1656- MutationType .INSERT ,
1657- createExpectation ( collectionBootDescriptor .getInsertExpectation (),
1658- collectionBootDescriptor .isCustomInsertCallable ()),
1659- collectionBootDescriptor .getCustomSQLInsert (),
1660- collectionBootDescriptor .isCustomInsertCallable ()
1661- ),
1662- new MutationDetails (
1663- MutationType .UPDATE ,
1664- createExpectation ( collectionBootDescriptor .getUpdateExpectation (),
1665- collectionBootDescriptor .isCustomUpdateCallable ()),
1666- collectionBootDescriptor .getCustomSQLUpdate (),
1667- collectionBootDescriptor .isCustomUpdateCallable ()
1668- ),
1643+ buildInsertMutationDetails ( collectionBootDescriptor ),
1644+ buildUpdateMutationDetails ( collectionBootDescriptor ),
16691645 collectionBootDescriptor .getKey ().isCascadeDeleteEnabled (),
1670- new MutationDetails (
1671- MutationType .DELETE ,
1672- collectionBootDescriptor .isCustomDeleteAllCallable () || collectionBootDescriptor .getDeleteAllExpectation () != null
1673- ? createExpectation ( collectionBootDescriptor .getDeleteAllExpectation (),
1674- collectionBootDescriptor .isCustomDeleteAllCallable () )
1675- : new Expectation .None (),
1676- collectionBootDescriptor .getCustomSQLDeleteAll (),
1677- collectionBootDescriptor .isCustomDeleteAllCallable ()
1678- ),
1679- new MutationDetails (
1680- MutationType .DELETE ,
1681- createExpectation ( collectionBootDescriptor .getDeleteExpectation (),
1682- collectionBootDescriptor .isCustomDeleteCallable ()),
1683- collectionBootDescriptor .getCustomSQLDelete (),
1684- collectionBootDescriptor .isCustomDeleteCallable ()
1685- )
1646+ buildDeleteAllMutationDetails ( collectionBootDescriptor ),
1647+ buildDeleteMutationDetails ( collectionBootDescriptor )
1648+ );
1649+ }
1650+
1651+ private static MutationDetails buildUpdateMutationDetails (Collection collectionBootDescriptor ) {
1652+ final boolean customUpdateCallable = collectionBootDescriptor .isCustomUpdateCallable ();
1653+ return new MutationDetails (
1654+ MutationType .UPDATE ,
1655+ createExpectation ( collectionBootDescriptor .getUpdateExpectation (),
1656+ customUpdateCallable ),
1657+ collectionBootDescriptor .getCustomSQLUpdate (),
1658+ customUpdateCallable
1659+ );
1660+ }
1661+
1662+ private static MutationDetails buildInsertMutationDetails (Collection collectionBootDescriptor ) {
1663+ return new MutationDetails (
1664+ MutationType .INSERT ,
1665+ createExpectation ( collectionBootDescriptor .getInsertExpectation (),
1666+ collectionBootDescriptor .isCustomInsertCallable () ),
1667+ collectionBootDescriptor .getCustomSQLInsert (),
1668+ collectionBootDescriptor .isCustomInsertCallable ()
1669+ );
1670+ }
1671+
1672+ private static MutationDetails buildDeleteMutationDetails (Collection collectionBootDescriptor ) {
1673+ final boolean customDeleteCallable = collectionBootDescriptor .isCustomDeleteCallable ();
1674+ return new MutationDetails (
1675+ MutationType .DELETE ,
1676+ createExpectation ( collectionBootDescriptor .getDeleteExpectation (),
1677+ customDeleteCallable ),
1678+ collectionBootDescriptor .getCustomSQLDelete (),
1679+ customDeleteCallable
1680+ );
1681+ }
1682+
1683+ private static MutationDetails buildDeleteAllMutationDetails (Collection collectionBootDescriptor ) {
1684+ final boolean customDeleteAllCallable = collectionBootDescriptor .isCustomDeleteAllCallable ();
1685+ final var deleteAllExpectation = collectionBootDescriptor .getDeleteAllExpectation ();
1686+ return new MutationDetails (
1687+ MutationType .DELETE ,
1688+ customDeleteAllCallable || deleteAllExpectation != null
1689+ ? createExpectation ( deleteAllExpectation ,
1690+ customDeleteAllCallable )
1691+ : new Expectation .None (),
1692+ collectionBootDescriptor .getCustomSQLDeleteAll (),
1693+ customDeleteAllCallable
16861694 );
16871695 }
16881696
@@ -1699,12 +1707,13 @@ private JdbcDeleteMutation buildCustomSqlDeleteAllOperation(MutatingTableReferen
16991707 new ColumnValueParameterList ( tableReference , ParameterUsage .RESTRICT , keyDescriptor .getJdbcTypeCount () );
17001708 keyDescriptor .getKeyPart ().forEachSelectable ( parameterBinders );
17011709 final var tableMapping = tableReference .getTableMapping ();
1710+ final var deleteDetails = tableMapping .getDeleteDetails ();
17021711 return new JdbcDeleteMutation (
17031712 tableMapping ,
17041713 this ,
1705- tableMapping . getDeleteDetails () .getCustomSql (),
1706- tableMapping . getDeleteDetails () .isCallable (),
1707- tableMapping . getDeleteDetails () .getExpectation (),
1714+ deleteDetails .getCustomSql (),
1715+ deleteDetails .isCallable (),
1716+ deleteDetails .getExpectation (),
17081717 parameterBinders
17091718 );
17101719 }
@@ -1743,10 +1752,9 @@ protected void applyKeyRestrictions(
17431752 assert foreignKeyDescriptor != null ;
17441753 foreignKeyDescriptor .getKeyPart ().forEachSelectable ( (selectionIndex , selectableMapping ) -> {
17451754 final var columnValueParameter = parameterList .addColumValueParameter ( selectableMapping );
1746- final var columnReference = columnValueParameter .getColumnReference ();
17471755 restrictionBindings .add (
17481756 new ColumnValueBinding (
1749- columnReference ,
1757+ columnValueParameter . getColumnReference () ,
17501758 new ColumnWriteFragment (
17511759 "?" ,
17521760 columnValueParameter ,
0 commit comments