Skip to content

Commit e869fce

Browse files
Merge branch 'TimothyGillespie/adjustTypeMapping' into TimothyGillespie/automigrationTests
2 parents 05877de + 5f88da9 commit e869fce

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

src/main/java/org/javawebstack/orm/mapper/DefaultMapper.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@
1010

1111
public class DefaultMapper implements TypeMapper {
1212

13+
// The following values assume utf8mb4 encoding which uses 4 bytes per character and
14+
// quarters the maximum column length accordingly
15+
private static final long MAX_SIZE_VARCHAR = (65535 + 1) / 4;
16+
private static final long MAX_SIZE_MEDIUMTEXT = (16777215 + 1) / 4;
17+
private static final long MAX_SIZE_LONGTEXT = (4294967295L + 1) / 4;
18+
19+
1320
public Object mapToSQL(Object source, Class<?> type) {
1421
if (source == null)
1522
return null;
@@ -79,13 +86,13 @@ public Object mapToJava(Object source, Class<?> type) {
7986
public SQLType getType(Class<?> type, int size) {
8087
if (type.equals(String.class) || type.equals(char[].class))
8188
// Upper limit of 4294967295 exceeds the int boundaries
82-
if (size > 16777215)
89+
if (size > MAX_SIZE_MEDIUMTEXT)
8390
return SQLType.LONGTEXT;
84-
if (size > 65535)
91+
if (size > MAX_SIZE_VARCHAR)
8592
return SQLType.MEDIUMTEXT;
8693
if (size > 1)
8794
return SQLType.VARCHAR;
88-
if (size == 0)
95+
if (size == 1)
8996
return SQLType.CHAR;
9097
if (type.equals(UUID.class))
9198
return SQLType.VARCHAR;
@@ -118,7 +125,7 @@ public String getTypeParameters(Class<?> type, int size) {
118125
if (type.isEnum())
119126
return Arrays.stream(((Class<? extends Enum<?>>) type).getEnumConstants()).map(c -> "'" + c.name() + "'").collect(Collectors.joining(","));
120127
if (type.equals(String.class) || type.equals(char[].class))
121-
return size > 65535 || size < 1 ? null : String.valueOf(size);
128+
return size > MAX_SIZE_VARCHAR || size < 1 ? null : String.valueOf(size);
122129
if (type.equals(byte[].class))
123130
return String.valueOf(size > 0 ? size : 255);
124131
if (type.equals(UUID.class))

0 commit comments

Comments
 (0)