Skip to content

Commit 73b2589

Browse files
Take byte overhead into account for the max sizes in the DefaultMapper
1 parent 5f88da9 commit 73b2589

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

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

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

1111
public class DefaultMapper implements TypeMapper {
1212

13+
// SOURCES:
14+
// TEXT Datatypes: https://www.mysqltutorial.org/mysql-text/
15+
private static final int BYTES_OVERHEAD_VARCHAR = 4;
16+
private static final int BYTES_OVERHEAD_TINYTEXT = 1;
17+
private static final int BYTES_OVERHEAD_TEXT = 2;
18+
private static final int BYTES_OVERHEAD_MEDIUMTEXT = 3;
19+
private static final int BYTES_OVERHEAD_LONGTEXT = 4;
20+
21+
// The max sizes given in the manual are in bytes. There are overheads which need to be subtracted.
1322
// 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;
23+
// further quarters the maximum column length accordingly.
24+
private static final long MAX_SIZE_VARCHAR = (long) Math.floor((65535 - BYTES_OVERHEAD_VARCHAR) / 4);
25+
private static final long MAX_SIZE_MEDIUMTEXT = (long) Math.floor((16777215 - BYTES_OVERHEAD_MEDIUMTEXT) / 4);
26+
private static final long MAX_SIZE_LONGTEXT = (long) Math.floor((4294967295L - BYTES_OVERHEAD_LONGTEXT) / 4);
1827

1928

2029
public Object mapToSQL(Object source, Class<?> type) {

0 commit comments

Comments
 (0)