Skip to content

Commit 05877de

Browse files
Merge branch 'TimothyGillespie/adjustTypeMapping' into TimothyGillespie/automigrationTests
2 parents 9d9734f + 028b38c commit 05877de

File tree

5 files changed

+44
-53
lines changed

5 files changed

+44
-53
lines changed

src/main/java/org/javawebstack/orm/SQLMapper.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ private static Object getValue(ResultSet rs, Class<?> sqlType, String tableName,
9898
}
9999
if (sqlType.equals(String.class))
100100
return rs.getString(columnName);
101+
if (sqlType.equals(Short.class))
102+
return rs.getShort(columnName);
101103
if (sqlType.equals(Integer.class))
102104
return rs.getInt(columnName);
103105
if (sqlType.equals(Boolean.class))

src/main/java/org/javawebstack/orm/SQLType.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,27 @@
33
import java.sql.Date;
44
import java.sql.Timestamp;
55

6+
// The class passed as constructor determines
67
public enum SQLType {
78

89
// Divided by category and ordered by byte size (MySQL sizes)
910
TINYINT(Boolean.class),
1011
SMALLINT(Short.class),
11-
// Not in use for any java data type
12-
//MEDIUMINT(),
12+
MEDIUMINT(Integer.class),
1313
INT(Integer.class),
1414
BIGINT(Long.class),
1515

1616
FLOAT(Float.class),
1717
DOUBLE(Double.class),
1818

19+
// No native char method except for a char stream
20+
CHAR(String.class),
21+
1922
VARCHAR(String.class),
23+
TINYTEXT(String.class),
2024
TEXT(String.class),
25+
MEDIUMTEXT(String.class),
26+
LONGTEXT(String.class),
2127

2228
DATE(Date.class),
2329
TIMESTAMP(Timestamp.class),

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

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,14 @@ public Object mapToSQL(Object source, Class<?> type) {
3232
if (type.equals(float.class))
3333
return Float.valueOf((float) source);
3434
if (type.equals(char[].class))
35-
return String.valueOf(source);
35+
return String.valueOf((char[]) source);
3636
if (type.equals(UUID.class))
3737
return source.toString();
3838
if (type.equals(Timestamp.class))
3939
return ((Timestamp) source).toString();
40+
if (type.equals(char.class))
41+
return String.valueOf((char) source);
42+
4043
return source;
4144
}
4245

@@ -63,14 +66,31 @@ public Object mapToJava(Object source, Class<?> type) {
6366
return ((Float) source).floatValue();
6467
if (type.equals(char[].class))
6568
return ((String) source).toCharArray();
69+
if (type.equals(char.class)) {
70+
String stringSource = (String) source;
71+
if (stringSource.length() != 1)
72+
return ' ';
73+
else
74+
return stringSource.charAt(0);
75+
}
6676
return source;
6777
}
6878

6979
public SQLType getType(Class<?> type, int size) {
7080
if (type.equals(String.class) || type.equals(char[].class))
71-
return size > 65535 || size <= 1 ? SQLType.TEXT : SQLType.VARCHAR;
72-
if (type.equals(UUID.class) || type.equals(char.class))
81+
// Upper limit of 4294967295 exceeds the int boundaries
82+
if (size > 16777215)
83+
return SQLType.LONGTEXT;
84+
if (size > 65535)
85+
return SQLType.MEDIUMTEXT;
86+
if (size > 1)
87+
return SQLType.VARCHAR;
88+
if (size == 0)
89+
return SQLType.CHAR;
90+
if (type.equals(UUID.class))
7391
return SQLType.VARCHAR;
92+
if (type.equals(char.class))
93+
return SQLType.CHAR;
7494
if (type.isEnum())
7595
return SQLType.ENUM;
7696
if (type.equals(boolean.class) || type.equals(Boolean.class) || type.equals(byte.class) || type.equals(Byte.class))
@@ -105,8 +125,6 @@ public String getTypeParameters(Class<?> type, int size) {
105125
return "36";
106126
if (type.equals(boolean.class) || type.equals(Boolean.class) || type.equals(char.class))
107127
return "1";
108-
if (type.equals(Timestamp.class))
109-
return "6";
110128
return null;
111129
}
112130

src/test/java/org/javawebstack/orm/test/TypesTest.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,9 @@
88
import org.javawebstack.orm.exception.ORMConfigurationException;
99
import org.junit.jupiter.api.Test;
1010

11-
import javax.swing.text.TabSet;
1211
import java.sql.Timestamp;
1312
import java.time.Instant;
1413

15-
import static org.javawebstack.orm.test.shared.util.TimestampUtil.diffInNanoseconds;
1614
import static org.junit.jupiter.api.Assertions.*;
1715

1816
public class TypesTest extends ORMTestCase {
@@ -42,6 +40,8 @@ public void testFields() throws ORMConfigurationException {
4240
model.exampleBoolean = true;
4341

4442
model.timestampTest = timestamp;
43+
44+
model.exampleCharPrimitive = 'C';
4545
model.save();
4646

4747
int id = model.id;
@@ -60,7 +60,12 @@ public void testFields() throws ORMConfigurationException {
6060
assertEquals(model.exampleDouble, 123456789.1234567890D);
6161
assertEquals(model.exampleLong, 999999999999999999L);
6262
assertEquals(model.exampleLongPrimitive, 999999999999999999L);
63-
assertTrue(diffInNanoseconds(timestamp, model.timestampTest) < 1);
63+
64+
65+
assertEquals(model.timestampTest.getTime() / 1000, timestamp.getTime() / 1000);
66+
67+
assertEquals(model.exampleCharPrimitive, 'C');
68+
6469
model.exampleNull = "Text";
6570
model.save();
6671
model = Repo.get(ExampleModel.class).get(id);
@@ -108,6 +113,9 @@ public static class ExampleModel extends Model {
108113
@Column
109114
public Timestamp timestampTest;
110115

116+
@Column
117+
public char exampleCharPrimitive;
118+
111119
public enum Type {
112120
ADMIN, USER, GUEST
113121
}

src/test/java/org/javawebstack/orm/test/shared/util/TimestampUtil.java

Lines changed: 0 additions & 43 deletions
This file was deleted.

0 commit comments

Comments
 (0)