Skip to content

Commit 74a2ab8

Browse files
committed
use switch expressions in Dialects
1 parent 3cb9867 commit 74a2ab8

File tree

10 files changed

+97
-170
lines changed

10 files changed

+97
-170
lines changed

hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/CockroachLegacyDialect.java

Lines changed: 12 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -991,38 +991,21 @@ public String getForUpdateString(String aliases, LockOptions lockOptions) {
991991
if (lockMode == null ) {
992992
lockMode = lockOptions.getLockMode();
993993
}
994-
switch ( lockMode ) {
995-
case PESSIMISTIC_READ: {
996-
return getReadLockString( aliases, lockOptions.getTimeOut() );
997-
}
998-
case PESSIMISTIC_WRITE: {
999-
return getWriteLockString( aliases, lockOptions.getTimeOut() );
1000-
}
1001-
case UPGRADE_NOWAIT:
1002-
case PESSIMISTIC_FORCE_INCREMENT: {
1003-
return getForUpdateNowaitString(aliases);
1004-
}
1005-
case UPGRADE_SKIPLOCKED: {
1006-
return getForUpdateSkipLockedString(aliases);
1007-
}
1008-
default: {
1009-
return "";
1010-
}
1011-
}
994+
return switch ( lockMode ) {
995+
case PESSIMISTIC_READ -> getReadLockString( aliases, lockOptions.getTimeOut() );
996+
case PESSIMISTIC_WRITE -> getWriteLockString( aliases, lockOptions.getTimeOut() );
997+
case UPGRADE_NOWAIT, PESSIMISTIC_FORCE_INCREMENT -> getForUpdateNowaitString( aliases );
998+
case UPGRADE_SKIPLOCKED -> getForUpdateSkipLockedString( aliases );
999+
default -> "";
1000+
};
10121001
}
10131002

10141003
private String withTimeout(String lockString, int timeout) {
1015-
switch (timeout) {
1016-
case LockOptions.NO_WAIT: {
1017-
return supportsNoWait() ? lockString + " nowait" : lockString;
1018-
}
1019-
case LockOptions.SKIP_LOCKED: {
1020-
return supportsSkipLocked() ? lockString + " skip locked" : lockString;
1021-
}
1022-
default: {
1023-
return lockString;
1024-
}
1025-
}
1004+
return switch ( timeout ) {
1005+
case LockOptions.NO_WAIT -> supportsNoWait() ? lockString + " nowait" : lockString;
1006+
case LockOptions.SKIP_LOCKED -> supportsSkipLocked() ? lockString + " skip locked" : lockString;
1007+
default -> lockString;
1008+
};
10261009
}
10271010

10281011
@Override

hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/HANALegacyDialect.java

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -684,26 +684,14 @@ public String getForUpdateString(final String aliases, final LockOptions lockOpt
684684
return getForUpdateString( aliases, lockMode, lockOptions.getTimeOut() );
685685
}
686686

687-
@SuppressWarnings({ "deprecation" })
688687
private String getForUpdateString(String aliases, LockMode lockMode, int timeout) {
689-
switch ( lockMode ) {
690-
case PESSIMISTIC_READ: {
691-
return getReadLockString( aliases, timeout );
692-
}
693-
case PESSIMISTIC_WRITE: {
694-
return getWriteLockString( aliases, timeout );
695-
}
696-
case UPGRADE_NOWAIT:
697-
case PESSIMISTIC_FORCE_INCREMENT: {
698-
return getForUpdateNowaitString( aliases );
699-
}
700-
case UPGRADE_SKIPLOCKED: {
701-
return getForUpdateSkipLockedString( aliases );
702-
}
703-
default: {
704-
return "";
705-
}
706-
}
688+
return switch ( lockMode ) {
689+
case PESSIMISTIC_READ -> getReadLockString( aliases, timeout );
690+
case PESSIMISTIC_WRITE -> getWriteLockString( aliases, timeout );
691+
case UPGRADE_NOWAIT, PESSIMISTIC_FORCE_INCREMENT -> getForUpdateNowaitString( aliases );
692+
case UPGRADE_SKIPLOCKED -> getForUpdateSkipLockedString( aliases );
693+
default -> "";
694+
};
707695
}
708696

709697
@Override

hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/MySQLLegacyDialect.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1325,16 +1325,12 @@ public static Replacer datetimeFormat(String format) {
13251325
}
13261326

13271327
private String withTimeout(String lockString, int timeout) {
1328-
switch (timeout) {
1329-
case LockOptions.NO_WAIT:
1330-
return supportsNoWait() ? lockString + " nowait" : lockString;
1331-
case LockOptions.SKIP_LOCKED:
1332-
return supportsSkipLocked() ? lockString + " skip locked" : lockString;
1333-
case LockOptions.WAIT_FOREVER:
1334-
return lockString;
1335-
default:
1336-
return supportsWait() ? lockString + " wait " + getTimeoutInSeconds( timeout ) : lockString;
1337-
}
1328+
return switch ( timeout ) {
1329+
case LockOptions.NO_WAIT -> supportsNoWait() ? lockString + " nowait" : lockString;
1330+
case LockOptions.SKIP_LOCKED -> supportsSkipLocked() ? lockString + " skip locked" : lockString;
1331+
case LockOptions.WAIT_FOREVER -> lockString;
1332+
default -> supportsWait() ? lockString + " wait " + getTimeoutInSeconds( timeout ) : lockString;
1333+
};
13381334
}
13391335

13401336
@Override

hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/OracleLegacyDialect.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1411,16 +1411,12 @@ public String getForUpdateSkipLockedString(String aliases) {
14111411
}
14121412

14131413
private String withTimeout(String lockString, int timeout) {
1414-
switch (timeout) {
1415-
case LockOptions.NO_WAIT:
1416-
return supportsNoWait() ? lockString + " nowait" : lockString;
1417-
case LockOptions.SKIP_LOCKED:
1418-
return supportsSkipLocked() ? lockString + " skip locked" : lockString;
1419-
case LockOptions.WAIT_FOREVER:
1420-
return lockString;
1421-
default:
1422-
return supportsWait() ? lockString + " wait " + getTimeoutInSeconds( timeout ) : lockString;
1423-
}
1414+
return switch ( timeout ) {
1415+
case LockOptions.NO_WAIT -> supportsNoWait() ? lockString + " nowait" : lockString;
1416+
case LockOptions.SKIP_LOCKED -> supportsSkipLocked() ? lockString + " skip locked" : lockString;
1417+
case LockOptions.WAIT_FOREVER -> lockString;
1418+
default -> supportsWait() ? lockString + " wait " + getTimeoutInSeconds( timeout ) : lockString;
1419+
};
14241420
}
14251421

14261422
@Override

hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/PostgreSQLLegacyDialect.java

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -886,24 +886,13 @@ public String getForUpdateString(String aliases, LockOptions lockOptions) {
886886
if (lockMode == null ) {
887887
lockMode = lockOptions.getLockMode();
888888
}
889-
switch ( lockMode ) {
890-
case PESSIMISTIC_READ: {
891-
return getReadLockString( aliases, lockOptions.getTimeOut() );
892-
}
893-
case PESSIMISTIC_WRITE: {
894-
return getWriteLockString( aliases, lockOptions.getTimeOut() );
895-
}
896-
case UPGRADE_NOWAIT:
897-
case PESSIMISTIC_FORCE_INCREMENT: {
898-
return getForUpdateNowaitString(aliases);
899-
}
900-
case UPGRADE_SKIPLOCKED: {
901-
return getForUpdateSkipLockedString(aliases);
902-
}
903-
default: {
904-
return "";
905-
}
906-
}
889+
return switch ( lockMode ) {
890+
case PESSIMISTIC_READ -> getReadLockString( aliases, lockOptions.getTimeOut() );
891+
case PESSIMISTIC_WRITE -> getWriteLockString( aliases, lockOptions.getTimeOut() );
892+
case UPGRADE_NOWAIT, PESSIMISTIC_FORCE_INCREMENT -> getForUpdateNowaitString( aliases );
893+
case UPGRADE_SKIPLOCKED -> getForUpdateSkipLockedString( aliases );
894+
default -> "";
895+
};
907896
}
908897

909898
@Override
@@ -1319,14 +1308,11 @@ public void appendDateTimeLiteral(
13191308
}
13201309

13211310
private String withTimeout(String lockString, int timeout) {
1322-
switch (timeout) {
1323-
case LockOptions.NO_WAIT:
1324-
return supportsNoWait() ? lockString + " nowait" : lockString;
1325-
case LockOptions.SKIP_LOCKED:
1326-
return supportsSkipLocked() ? lockString + " skip locked" : lockString;
1327-
default:
1328-
return lockString;
1329-
}
1311+
return switch ( timeout ) {
1312+
case LockOptions.NO_WAIT -> supportsNoWait() ? lockString + " nowait" : lockString;
1313+
case LockOptions.SKIP_LOCKED -> supportsSkipLocked() ? lockString + " skip locked" : lockString;
1314+
default -> lockString;
1315+
};
13301316
}
13311317

13321318
@Override

hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/SQLServerLegacyDialect.java

Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -649,39 +649,31 @@ public String appendLockHint(LockOptions lockOptions, String tableName) {
649649
lockMode = lockOptions.getLockMode();
650650
}
651651

652-
final String writeLockStr = lockOptions.getTimeOut() == LockOptions.SKIP_LOCKED ? "updlock" : "updlock,holdlock";
653-
final String readLockStr = lockOptions.getTimeOut() == LockOptions.SKIP_LOCKED ? "updlock" : "holdlock";
654-
655-
final String noWaitStr = lockOptions.getTimeOut() == LockOptions.NO_WAIT ? ",nowait" : "";
656-
final String skipLockStr = lockOptions.getTimeOut() == LockOptions.SKIP_LOCKED ? ",readpast" : "";
657-
658-
switch ( lockMode ) {
659-
case PESSIMISTIC_WRITE:
660-
case WRITE:
661-
return tableName + " with (" + writeLockStr + ",rowlock" + noWaitStr + skipLockStr + ")";
662-
case PESSIMISTIC_READ:
663-
return tableName + " with (" + readLockStr + ",rowlock" + noWaitStr + skipLockStr + ")";
664-
case UPGRADE_SKIPLOCKED:
665-
return tableName + " with (updlock,rowlock,readpast" + noWaitStr + ")";
666-
case UPGRADE_NOWAIT:
667-
return tableName + " with (updlock,holdlock,rowlock,nowait)";
668-
default:
669-
return tableName;
670-
}
652+
final int timeOut = lockOptions.getTimeOut();
653+
654+
final String writeLockStr = timeOut == LockOptions.SKIP_LOCKED ? "updlock" : "updlock,holdlock";
655+
final String readLockStr = timeOut == LockOptions.SKIP_LOCKED ? "updlock" : "holdlock";
656+
657+
final String noWaitStr = timeOut == LockOptions.NO_WAIT ? ",nowait" : "";
658+
final String skipLockStr = timeOut == LockOptions.SKIP_LOCKED ? ",readpast" : "";
659+
660+
return switch ( lockMode ) {
661+
case PESSIMISTIC_WRITE, WRITE ->
662+
tableName + " with (" + writeLockStr + ",rowlock" + noWaitStr + skipLockStr + ")";
663+
case PESSIMISTIC_READ ->
664+
tableName + " with (" + readLockStr + ",rowlock" + noWaitStr + skipLockStr + ")";
665+
case UPGRADE_SKIPLOCKED -> tableName + " with (updlock,rowlock,readpast" + noWaitStr + ")";
666+
case UPGRADE_NOWAIT -> tableName + " with (updlock,holdlock,rowlock,nowait)";
667+
default -> tableName;
668+
};
671669
}
672670
else {
673-
switch ( lockOptions.getLockMode() ) {
674-
case UPGRADE_NOWAIT:
675-
case PESSIMISTIC_WRITE:
676-
case WRITE:
677-
return tableName + " with (updlock,rowlock)";
678-
case PESSIMISTIC_READ:
679-
return tableName + " with (holdlock,rowlock)";
680-
case UPGRADE_SKIPLOCKED:
681-
return tableName + " with (updlock,rowlock,readpast)";
682-
default:
683-
return tableName;
684-
}
671+
return switch ( lockOptions.getLockMode() ) {
672+
case UPGRADE_NOWAIT, PESSIMISTIC_WRITE, WRITE -> tableName + " with (updlock,rowlock)";
673+
case PESSIMISTIC_READ -> tableName + " with (holdlock,rowlock)";
674+
case UPGRADE_SKIPLOCKED -> tableName + " with (updlock,rowlock,readpast)";
675+
default -> tableName;
676+
};
685677
}
686678
}
687679

hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/TimesTenDialect.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -281,15 +281,11 @@ public String getReadLockString(String aliases, int timeout) {
281281

282282

283283
private String withTimeout(String lockString, int timeout) {
284-
switch (timeout) {
285-
case LockOptions.NO_WAIT:
286-
return supportsNoWait() ? lockString + " nowait" : lockString;
287-
case LockOptions.SKIP_LOCKED:
288-
case LockOptions.WAIT_FOREVER:
289-
return lockString;
290-
default:
291-
return supportsWait() ? lockString + " wait " + getTimeoutInSeconds( timeout ) : lockString;
292-
}
284+
return switch ( timeout ) {
285+
case LockOptions.NO_WAIT -> supportsNoWait() ? lockString + " nowait" : lockString;
286+
case LockOptions.SKIP_LOCKED, LockOptions.WAIT_FOREVER -> lockString;
287+
default -> supportsWait() ? lockString + " wait " + getTimeoutInSeconds( timeout ) : lockString;
288+
};
293289
}
294290

295291
@Override

hibernate-core/src/main/java/org/hibernate/dialect/Dialect.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2270,8 +2270,8 @@ public String getWriteLockString(int timeout) {
22702270
* @return The appropriate {@code LOCK} clause string.
22712271
*/
22722272
public String getWriteLockString(String aliases, int timeout) {
2273-
// by default we simply return the getWriteLockString(timeout) result since
2274-
// the default is to say no support for "FOR UPDATE OF ..."
2273+
// by default, we simply return getWriteLockString(timeout),
2274+
// since the default is no support for "FOR UPDATE OF ..."
22752275
return getWriteLockString( timeout );
22762276
}
22772277

hibernate-core/src/main/java/org/hibernate/dialect/HANADialect.java

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -693,26 +693,14 @@ public String getForUpdateString(final String aliases, final LockOptions lockOpt
693693
return getForUpdateString( aliases, lockMode, lockOptions.getTimeOut() );
694694
}
695695

696-
@SuppressWarnings({ "deprecation" })
697696
private String getForUpdateString(String aliases, LockMode lockMode, int timeout) {
698-
switch ( lockMode ) {
699-
case PESSIMISTIC_READ: {
700-
return getReadLockString( aliases, timeout );
701-
}
702-
case PESSIMISTIC_WRITE: {
703-
return getWriteLockString( aliases, timeout );
704-
}
705-
case UPGRADE_NOWAIT:
706-
case PESSIMISTIC_FORCE_INCREMENT: {
707-
return getForUpdateNowaitString( aliases );
708-
}
709-
case UPGRADE_SKIPLOCKED: {
710-
return getForUpdateSkipLockedString( aliases );
711-
}
712-
default: {
713-
return "";
714-
}
715-
}
697+
return switch ( lockMode ) {
698+
case PESSIMISTIC_READ -> getReadLockString( aliases, timeout );
699+
case PESSIMISTIC_WRITE -> getWriteLockString( aliases, timeout );
700+
case UPGRADE_NOWAIT, PESSIMISTIC_FORCE_INCREMENT -> getForUpdateNowaitString( aliases );
701+
case UPGRADE_SKIPLOCKED -> getForUpdateSkipLockedString( aliases );
702+
default -> "";
703+
};
716704
}
717705

718706
@Override

hibernate-core/src/main/java/org/hibernate/dialect/SQLServerDialect.java

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -643,27 +643,29 @@ public char openQuote() {
643643

644644
@Override
645645
public String appendLockHint(LockOptions lockOptions, String tableName) {
646-
LockMode lockMode = lockOptions.getAliasSpecificLockMode( tableName );
647-
if ( lockMode == null ) {
648-
lockMode = lockOptions.getLockMode();
649-
}
646+
final LockMode lockMode = lockModeForAlias( lockOptions, tableName );
647+
final int timeOut = lockOptions.getTimeOut();
650648

651-
final String writeLockStr = lockOptions.getTimeOut() == LockOptions.SKIP_LOCKED ? "updlock" : "updlock,holdlock";
652-
final String readLockStr = lockOptions.getTimeOut() == LockOptions.SKIP_LOCKED ? "updlock" : "holdlock";
649+
final String writeLockStr = timeOut == LockOptions.SKIP_LOCKED ? "updlock" : "updlock,holdlock";
650+
final String readLockStr = timeOut == LockOptions.SKIP_LOCKED ? "updlock" : "holdlock";
653651

654-
final String noWaitStr = lockOptions.getTimeOut() == LockOptions.NO_WAIT ? ",nowait" : "";
655-
final String skipLockStr = lockOptions.getTimeOut() == LockOptions.SKIP_LOCKED ? ",readpast" : "";
652+
final String noWaitStr = timeOut == LockOptions.NO_WAIT ? ",nowait" : "";
653+
final String skipLockStr = timeOut == LockOptions.SKIP_LOCKED ? ",readpast" : "";
656654

657-
return switch (lockMode) {
658-
case PESSIMISTIC_WRITE, WRITE ->
659-
tableName + " with (" + writeLockStr + ",rowlock" + noWaitStr + skipLockStr + ")";
660-
case PESSIMISTIC_READ -> tableName + " with (" + readLockStr + ",rowlock" + noWaitStr + skipLockStr + ")";
661-
case UPGRADE_SKIPLOCKED -> tableName + " with (updlock,rowlock,readpast" + noWaitStr + ")";
662-
case UPGRADE_NOWAIT -> tableName + " with (updlock,holdlock,rowlock,nowait)";
663-
default -> tableName;
655+
return tableName + switch (lockMode) {
656+
case PESSIMISTIC_WRITE, WRITE -> " with (" + writeLockStr + ",rowlock" + noWaitStr + skipLockStr + ")";
657+
case PESSIMISTIC_READ -> " with (" + readLockStr + ",rowlock" + noWaitStr + skipLockStr + ")";
658+
case UPGRADE_SKIPLOCKED -> " with (updlock,rowlock,readpast" + noWaitStr + ")";
659+
case UPGRADE_NOWAIT -> " with (updlock,holdlock,rowlock,nowait)";
660+
default -> "";
664661
};
665662
}
666663

664+
private static LockMode lockModeForAlias(LockOptions lockOptions, String tableName) {
665+
final LockMode lockMode = lockOptions.getAliasSpecificLockMode( tableName );
666+
return lockMode == null ? lockOptions.getLockMode() : lockMode;
667+
}
668+
667669

668670
/**
669671
* The current_timestamp is more accurate, but only known to be supported in SQL Server 7.0 and later and

0 commit comments

Comments
 (0)