Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

misc minor code changes #9654

Merged
merged 1 commit into from
Jan 21, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -1568,8 +1568,7 @@ private void bindSynchronize() {
if ( synchronize != null ) {
final JdbcEnvironment jdbcEnvironment = getMetadataCollector().getDatabase().getJdbcEnvironment();
final boolean logical = synchronize.logical();
final String[] tableNames = synchronize.value();
for ( String tableName : tableNames ) {
for ( String tableName : synchronize.value() ) {
final String physicalName = logical ? toPhysicalName( jdbcEnvironment, tableName ) : tableName;
persistentClass.addSynchronizedTable( physicalName );
}
Expand Down
51 changes: 24 additions & 27 deletions hibernate-core/src/main/java/org/hibernate/sql/Template.java
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,8 @@ else if ( quotedIdentifier && dialect.closeQuote()==token.charAt(0) ) {
isOpenQuote = false;
}
if ( isOpenQuote
&& !inFromClause // don't want to append alias to tokens inside the from clause
&& notEndsWithDot( previousToken ) ) {
&& !inFromClause // don't want to append alias to tokens inside the FROM clause
&& !endsWithDot( previousToken ) ) {
result.append( alias ).append( '.' );
}
}
Expand Down Expand Up @@ -237,7 +237,7 @@ else if ( FUNCTION_WITH_FROM_KEYWORDS.contains(lcToken) && "(".equals( nextToken
result.append(token);
inExtractOrTrim = true;
}
else if ( !inFromClause // don't want to append alias to tokens inside the from clause
else if ( !inFromClause // don't want to append alias to tokens inside the FROM clause
&& isIdentifier( token )
&& !isFunctionOrKeyword( lcToken, nextToken, dialect, typeConfiguration )
&& !isLiteral( lcToken, nextToken, sql, symbols, tokens ) ) {
Expand Down Expand Up @@ -274,31 +274,29 @@ else if ( inFromClause && ",".equals(lcToken) ) {
return result.toString();
}

private static boolean notEndsWithDot(String token) {
return token == null || !token.endsWith( "." );
private static boolean endsWithDot(String token) {
return token != null && token.endsWith( "." );
}

private static boolean isLiteral(
String lcToken, String next,
String sqlWhereString, String symbols, StringTokenizer tokens) {
if ( LITERAL_PREFIXES.contains( lcToken ) && next != null ) {
// easy cases first
if ( "'".equals(next) ) {
return true;
}
else if ( !next.isBlank() ) {
return false;
}
else {
if ( next == null ) {
return false;
}
else if ( LITERAL_PREFIXES.contains( lcToken ) ) {
if ( next.isBlank() ) {
// we need to look ahead in the token stream
// to find the first non-blank token
return lookPastBlankTokens( sqlWhereString, symbols, tokens, 1,
(String nextToken)
-> "'".equals(nextToken)
(nextToken) -> "'".equals(nextToken)
|| lcToken.equals("time") && "with".equals(nextToken)
|| lcToken.equals("timestamp") && "with".equals(nextToken)
|| lcToken.equals("time") && "zone".equals(nextToken) );
}
else {
return "'".equals(next);
}
}
else {
return false;
Expand Down Expand Up @@ -351,15 +349,15 @@ public static List<String> collectColumnNames(String template) {
int begin = 0;
int match;
while ( ( match = template.indexOf(TEMPLATE, begin) ) >= 0 ) {
int start = match + TEMPLATE.length() + 1;
final int start = match + TEMPLATE.length() + 1;
for ( int loc = start;; loc++ ) {
if ( loc == template.length() - 1 ) {
names.add( template.substring( start ) );
begin = template.length();
break;
}
else {
char ch = template.charAt( loc );
final char ch = template.charAt( loc );
if ( PUNCTUATION.indexOf(ch) >= 0 || WHITESPACE.indexOf(ch) >= 0 ) {
names.add( template.substring( start, loc ) );
begin = loc;
Expand Down Expand Up @@ -401,17 +399,16 @@ private static boolean isType(String lcToken, TypeConfiguration typeConfiguratio
}

private static boolean isIdentifier(String token) {
if ( isBoolean( token ) ) {
return false;
}
return token.charAt( 0 ) == '`'
|| ( //allow any identifier quoted with backtick
isLetter( token.charAt( 0 ) ) && //only recognizes identifiers beginning with a letter
token.indexOf( '.' ) < 0
);
return token.charAt( 0 ) == '`' // allow any identifier quoted with backtick
|| isLetter( token.charAt( 0 ) ) // only recognizes identifiers beginning with a letter
&& token.indexOf( '.' ) < 0
&& !isBoolean( token );
}

private static boolean isBoolean(String token) {
return "true".equals( token ) || "false".equals( token );
return switch ( token.toLowerCase( Locale.ROOT ) ) {
case "true", "false" -> true;
default -> false;
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.Locale;
import java.util.TimeZone;

import org.hibernate.Internal;
import org.hibernate.dialect.Dialect;
import org.hibernate.sql.ast.spi.SqlAppender;

Expand All @@ -32,6 +33,7 @@
* @author Steve Ebersole
* @author Gavin King
*/
@Internal
public final class DateTimeUtils {
private DateTimeUtils() {
}
Expand Down Expand Up @@ -435,8 +437,8 @@ public static <T extends Temporal> T adjustToDefaultPrecision(T temporal, Dialec
return adjustToPrecision( temporal, d.getDefaultTimestampPrecision(), d );
}

public static <T extends Temporal> T adjustToPrecision(T temporal, int precision, Dialect d) {
return d.doesRoundTemporalOnOverflow()
public static <T extends Temporal> T adjustToPrecision(T temporal, int precision, Dialect dialect) {
return dialect.doesRoundTemporalOnOverflow()
? roundToSecondPrecision( temporal, precision )
: truncateToPrecision( temporal, precision );
}
Expand Down Expand Up @@ -484,6 +486,7 @@ public static <T extends Temporal> T roundToSecondPrecision(T temporal, int prec
}
final long nanos = roundToPrecision( temporal.get( ChronoField.NANO_OF_SECOND ), precision );
if ( nanos == 1000000000L ) {
//noinspection unchecked
return (T) temporal.plus( 1L, ChronoUnit.SECONDS ).with( ChronoField.NANO_OF_SECOND, 0L );
}
//noinspection unchecked
Expand All @@ -501,27 +504,17 @@ public static long roundToPrecision(int nano, int precision) {
}

private static int pow10(int exponent) {
switch ( exponent ) {
case 0:
return 1;
case 1:
return 10;
case 2:
return 100;
case 3:
return 1_000;
case 4:
return 10_000;
case 5:
return 100_000;
case 6:
return 1_000_000;
case 7:
return 10_000_000;
case 8:
return 100_000_000;
default:
return (int) Math.pow( 10, exponent );
}
return switch ( exponent ) {
case 0 -> 1;
case 1 -> 10;
case 2 -> 100;
case 3 -> 1_000;
case 4 -> 10_000;
case 5 -> 100_000;
case 6 -> 1_000_000;
case 7 -> 10_000_000;
case 8 -> 100_000_000;
default -> (int) Math.pow( 10, exponent );
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,6 @@ public boolean isTypeNameRegistered(final String typeName) {
return true;
}
}

return false;
}
}
Loading