Skip to content

Commit

Permalink
recover previous workaround for problem with MySQL char(n) type
Browse files Browse the repository at this point in the history
This is needed to remain compatible with schemas we produced in the past,
even though we're nor preferring varchar(1) for storing Java char.

Signed-off-by: Gavin King <[email protected]>
  • Loading branch information
gavinking committed Oct 31, 2024
1 parent a389f77 commit 633f101
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,15 @@ protected String columnType(int sqlTypeCode) {
};
}

/**
* Does this dialect strip trailing spaces from values stored
* in columns of type {@code char(n)}?
* MySQL is the main offender here.
*/
public boolean stripsTrailingSpacesFromChar() {
return false;
}

/**
* The SQL type to use in {@code cast( ... as ... )} expressions when
* casting to the target type represented by the given JDBC type code.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,16 @@ protected String columnType(int sqlTypeCode) {
};
}

/**
* MySQL strips any trailing space character from a
* value stored in a column of type {@code char(n)}.
* @return {@code true}
*/
@Override
public boolean stripsTrailingSpacesFromChar() {

This comment has been minimized.

Copy link
@Selaron

Selaron Jan 31, 2025

Contributor

@gavinking In my opinion stripsTrailingSpacesFromChar -> true should also be implemented by MySQLLegacyDialect as we are observing the new CoercionException( "value does not contain a character: '" + string + "'" ); when using Hibernate 7 Beta3, MySQLLegacyDialect, MySQL 5.

Let me know if you need an issue or any additional input regarding this.

This comment has been minimized.

Copy link
@beikov

beikov Feb 11, 2025

Member

Please create a PR for this change.

This comment has been minimized.

Copy link
@Selaron

Selaron Feb 17, 2025

Contributor

I created PR #9761

return true;
}

@Override
public boolean useMaterializedLobWhenCapacityExceeded() {
// MySQL has no real concept of LOBs, so we can just use longtext/longblob with the materialized JDBC APIs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,19 +63,32 @@ public <X> Character wrap(X value, WrapperOptions options) {
if ( value == null ) {
return null;
}
if (value instanceof Character character) {
else if (value instanceof Character character) {
return character;
}
if (value instanceof String string) {
if ( string.length() != 1 ) {
throw new CoercionException( "value must contain exactly one character: '" + string + "'" );
else if (value instanceof String string) {
switch ( string.length() ) {
case 1:
return string.charAt( 0 );
case 0:
if ( options.getDialect().stripsTrailingSpacesFromChar() ) {
// we previously stored char values in char(1) columns on MySQL
// but MySQL strips trailing spaces from the value when read
return ' ';
}
else {
throw new CoercionException( "value does not contain a character: '" + string + "'" );
}
default:
throw new CoercionException( "value contains more than one character: '" + string + "'" );
}
return string.charAt( 0 );
}
if (value instanceof Number number) {
else if (value instanceof Number number) {
return (char) number.shortValue();
}
throw unknownWrap( value.getClass() );
else {
throw unknownWrap( value.getClass() );
}
}

@Override
Expand Down

0 comments on commit 633f101

Please sign in to comment.