1010
1111public 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.
22+ // The following values assume utf8mb4 encoding which uses 4 bytes per character and
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 );
27+
28+
1329 public Object mapToSQL (Object source , Class <?> type ) {
1430 if (source == null )
1531 return null ;
@@ -19,6 +35,10 @@ public Object mapToSQL(Object source, Class<?> type) {
1935 return Integer .valueOf (((Boolean ) source ) ? 1 : 0 );
2036 if (type .equals (boolean .class ))
2137 return Integer .valueOf (((boolean ) source ) ? 1 : 0 );
38+ if (type .equals (byte .class ))
39+ return Byte .valueOf ((byte ) source );
40+ if (type .equals (short .class ))
41+ return Short .valueOf ((short ) source );
2242 if (type .equals (int .class ))
2343 return Integer .valueOf ((int ) source );
2444 if (type .equals (long .class ))
@@ -27,8 +47,15 @@ public Object mapToSQL(Object source, Class<?> type) {
2747 return Double .valueOf ((double ) source );
2848 if (type .equals (float .class ))
2949 return Float .valueOf ((float ) source );
50+ if (type .equals (char [].class ))
51+ return String .valueOf ((char []) source );
3052 if (type .equals (UUID .class ))
3153 return source .toString ();
54+ if (type .equals (Timestamp .class ))
55+ return ((Timestamp ) source ).toString ();
56+ if (type .equals (char .class ))
57+ return String .valueOf ((char ) source );
58+
3259 return source ;
3360 }
3461
@@ -41,26 +68,51 @@ public Object mapToJava(Object source, Class<?> type) {
4168 return UUID .fromString ((String ) source );
4269 if (type .equals (boolean .class ))
4370 return ((Boolean ) source ).booleanValue ();
44- if (type .equals (long .class ))
45- return ((Long ) source ).longValue ();
71+ if (type .equals (byte .class ))
72+ return ((Byte ) source ).byteValue ();
73+ if (type .equals (short .class ))
74+ return ((Short ) source ).shortValue ();
4675 if (type .equals (int .class ))
4776 return ((Integer ) source ).intValue ();
77+ if (type .equals (long .class ))
78+ return ((Long ) source ).longValue ();
4879 if (type .equals (double .class ))
4980 return ((Double ) source ).doubleValue ();
5081 if (type .equals (float .class ))
5182 return ((Float ) source ).floatValue ();
83+ if (type .equals (char [].class ))
84+ return ((String ) source ).toCharArray ();
85+ if (type .equals (char .class )) {
86+ String stringSource = (String ) source ;
87+ if (stringSource .length () != 1 )
88+ return ' ' ;
89+ else
90+ return stringSource .charAt (0 );
91+ }
5292 return source ;
5393 }
5494
5595 public SQLType getType (Class <?> type , int size ) {
56- if (type .equals (String .class ))
57- return size > 255 || size < 1 ? SQLType .TEXT : SQLType .VARCHAR ;
96+ if (type .equals (String .class ) || type .equals (char [].class ))
97+ // Upper limit of 4294967295 exceeds the int boundaries
98+ if (size > MAX_SIZE_MEDIUMTEXT )
99+ return SQLType .LONGTEXT ;
100+ if (size > MAX_SIZE_VARCHAR )
101+ return SQLType .MEDIUMTEXT ;
102+ if (size > 1 )
103+ return SQLType .VARCHAR ;
104+ if (size == 1 )
105+ return SQLType .CHAR ;
58106 if (type .equals (UUID .class ))
59107 return SQLType .VARCHAR ;
108+ if (type .equals (char .class ))
109+ return SQLType .CHAR ;
60110 if (type .isEnum ())
61111 return SQLType .ENUM ;
62- if (type .equals (Boolean .class ) || type .equals (boolean .class ))
112+ if (type .equals (boolean . class ) || type . equals ( Boolean .class ) || type .equals (byte . class ) || type . equals ( Byte .class ))
63113 return SQLType .TINYINT ;
114+ if (type .equals (short .class ) || type .equals (Short .class ))
115+ return SQLType .SMALLINT ;
64116 if (type .equals (int .class ) || type .equals (Integer .class ))
65117 return SQLType .INT ;
66118 if (type .equals (double .class ) || type .equals (Double .class ))
@@ -81,12 +133,14 @@ public SQLType getType(Class<?> type, int size) {
81133 public String getTypeParameters (Class <?> type , int size ) {
82134 if (type .isEnum ())
83135 return Arrays .stream (((Class <? extends Enum <?>>) type ).getEnumConstants ()).map (c -> "'" + c .name () + "'" ).collect (Collectors .joining ("," ));
84- if (type .equals (String .class ))
85- return size > 255 || size < 1 ? null : String .valueOf (size );
136+ if (type .equals (String .class ) || type . equals ( char []. class ) )
137+ return size > MAX_SIZE_VARCHAR || size < 1 ? null : String .valueOf (size );
86138 if (type .equals (byte [].class ))
87139 return String .valueOf (size > 0 ? size : 255 );
88140 if (type .equals (UUID .class ))
89141 return "36" ;
142+ if (type .equals (boolean .class ) || type .equals (Boolean .class ) || type .equals (char .class ))
143+ return "1" ;
90144 return null ;
91145 }
92146
0 commit comments