Skip to content

Commit 2967694

Browse files
authored
Merge pull request #3247 from mawen12/encapsulation-method
encapsulation code to method level for reuse
2 parents 45953df + 28af7e9 commit 2967694

File tree

6 files changed

+43
-83
lines changed

6 files changed

+43
-83
lines changed

src/main/java/org/apache/ibatis/type/BlobTypeHandler.java

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -36,31 +36,20 @@ public void setNonNullParameter(PreparedStatement ps, int i, byte[] parameter, J
3636

3737
@Override
3838
public byte[] getNullableResult(ResultSet rs, String columnName) throws SQLException {
39-
Blob blob = rs.getBlob(columnName);
40-
byte[] returnValue = null;
41-
if (null != blob) {
42-
returnValue = blob.getBytes(1, (int) blob.length());
43-
}
44-
return returnValue;
39+
return toPrimitiveBytes(rs.getBlob(columnName));
4540
}
4641

4742
@Override
4843
public byte[] getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
49-
Blob blob = rs.getBlob(columnIndex);
50-
byte[] returnValue = null;
51-
if (null != blob) {
52-
returnValue = blob.getBytes(1, (int) blob.length());
53-
}
54-
return returnValue;
44+
return toPrimitiveBytes(rs.getBlob(columnIndex));
5545
}
5646

5747
@Override
5848
public byte[] getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
59-
Blob blob = cs.getBlob(columnIndex);
60-
byte[] returnValue = null;
61-
if (null != blob) {
62-
returnValue = blob.getBytes(1, (int) blob.length());
63-
}
64-
return returnValue;
49+
return toPrimitiveBytes(cs.getBlob(columnIndex));
50+
}
51+
52+
private byte[] toPrimitiveBytes(Blob blob) throws SQLException {
53+
return blob == null ? null : blob.getBytes(1, (int) blob.length());
6554
}
6655
}

src/main/java/org/apache/ibatis/type/CharacterTypeHandler.java

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,28 +33,20 @@ public void setNonNullParameter(PreparedStatement ps, int i, Character parameter
3333

3434
@Override
3535
public Character getNullableResult(ResultSet rs, String columnName) throws SQLException {
36-
String columnValue = rs.getString(columnName);
37-
if (columnValue != null && !columnValue.isEmpty()) {
38-
return columnValue.charAt(0);
39-
}
40-
return null;
36+
return toCharacter(rs.getString(columnName));
4137
}
4238

4339
@Override
4440
public Character getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
45-
String columnValue = rs.getString(columnIndex);
46-
if (columnValue != null && !columnValue.isEmpty()) {
47-
return columnValue.charAt(0);
48-
}
49-
return null;
41+
return toCharacter(rs.getString(columnIndex));
5042
}
5143

5244
@Override
5345
public Character getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
54-
String columnValue = cs.getString(columnIndex);
55-
if (columnValue != null && !columnValue.isEmpty()) {
56-
return columnValue.charAt(0);
57-
}
58-
return null;
46+
return toCharacter(cs.getString(columnIndex));
47+
}
48+
49+
private Character toCharacter(String value) {
50+
return value == null || value.isEmpty() ? null : value.charAt(0);
5951
}
6052
}

src/main/java/org/apache/ibatis/type/DateOnlyTypeHandler.java

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,29 +33,21 @@ public void setNonNullParameter(PreparedStatement ps, int i, Date parameter, Jdb
3333

3434
@Override
3535
public Date getNullableResult(ResultSet rs, String columnName) throws SQLException {
36-
java.sql.Date sqlDate = rs.getDate(columnName);
37-
if (sqlDate != null) {
38-
return new Date(sqlDate.getTime());
39-
}
40-
return null;
36+
return toSqlDate(rs.getDate(columnName));
4137
}
4238

4339
@Override
4440
public Date getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
45-
java.sql.Date sqlDate = rs.getDate(columnIndex);
46-
if (sqlDate != null) {
47-
return new Date(sqlDate.getTime());
48-
}
49-
return null;
41+
return toSqlDate(rs.getDate(columnIndex));
5042
}
5143

5244
@Override
5345
public Date getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
54-
java.sql.Date sqlDate = cs.getDate(columnIndex);
55-
if (sqlDate != null) {
56-
return new Date(sqlDate.getTime());
57-
}
58-
return null;
46+
return toSqlDate(cs.getDate(columnIndex));
47+
}
48+
49+
private java.sql.Date toSqlDate(Date date) {
50+
return date == null ? null : new java.sql.Date(date.getTime());
5951
}
6052

6153
}

src/main/java/org/apache/ibatis/type/DateTypeHandler.java

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,28 +34,21 @@ public void setNonNullParameter(PreparedStatement ps, int i, Date parameter, Jdb
3434

3535
@Override
3636
public Date getNullableResult(ResultSet rs, String columnName) throws SQLException {
37-
Timestamp sqlTimestamp = rs.getTimestamp(columnName);
38-
if (sqlTimestamp != null) {
39-
return new Date(sqlTimestamp.getTime());
40-
}
41-
return null;
37+
return toDate(rs.getTimestamp(columnName));
4238
}
4339

4440
@Override
4541
public Date getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
46-
Timestamp sqlTimestamp = rs.getTimestamp(columnIndex);
47-
if (sqlTimestamp != null) {
48-
return new Date(sqlTimestamp.getTime());
49-
}
50-
return null;
42+
return toDate(rs.getTimestamp(columnIndex));
5143
}
5244

5345
@Override
5446
public Date getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
55-
Timestamp sqlTimestamp = cs.getTimestamp(columnIndex);
56-
if (sqlTimestamp != null) {
57-
return new Date(sqlTimestamp.getTime());
58-
}
59-
return null;
47+
return toDate(cs.getTimestamp(columnIndex));
6048
}
49+
50+
private Date toDate(Timestamp timestamp) {
51+
return timestamp == null ? null : new Date(timestamp.getTime());
52+
}
53+
6154
}

src/main/java/org/apache/ibatis/type/TimeOnlyTypeHandler.java

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,28 +34,21 @@ public void setNonNullParameter(PreparedStatement ps, int i, Date parameter, Jdb
3434

3535
@Override
3636
public Date getNullableResult(ResultSet rs, String columnName) throws SQLException {
37-
Time sqlTime = rs.getTime(columnName);
38-
if (sqlTime != null) {
39-
return new Date(sqlTime.getTime());
40-
}
41-
return null;
37+
return toDate(rs.getTime(columnName));
4238
}
4339

4440
@Override
4541
public Date getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
46-
Time sqlTime = rs.getTime(columnIndex);
47-
if (sqlTime != null) {
48-
return new Date(sqlTime.getTime());
49-
}
50-
return null;
42+
return toDate(rs.getTime(columnIndex));
5143
}
5244

5345
@Override
5446
public Date getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
55-
Time sqlTime = cs.getTime(columnIndex);
56-
if (sqlTime != null) {
57-
return new Date(sqlTime.getTime());
58-
}
59-
return null;
47+
return toDate(cs.getTime(columnIndex));
6048
}
49+
50+
private Date toDate(Time time) {
51+
return time == null ? null : new Date(time.getTime());
52+
}
53+
6154
}

src/main/java/org/apache/ibatis/type/YearMonthTypeHandler.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,20 @@ public void setNonNullParameter(PreparedStatement ps, int i, YearMonth yearMonth
4040

4141
@Override
4242
public YearMonth getNullableResult(ResultSet rs, String columnName) throws SQLException {
43-
String value = rs.getString(columnName);
44-
return value == null ? null : YearMonth.parse(value);
43+
return toYearMonth(rs.getString(columnName));
4544
}
4645

4746
@Override
4847
public YearMonth getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
49-
String value = rs.getString(columnIndex);
50-
return value == null ? null : YearMonth.parse(value);
48+
return toYearMonth(rs.getString(columnIndex));
5149
}
5250

5351
@Override
5452
public YearMonth getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
55-
String value = cs.getString(columnIndex);
53+
return toYearMonth(cs.getString(columnIndex));
54+
}
55+
56+
private YearMonth toYearMonth(String value) {
5657
return value == null ? null : YearMonth.parse(value);
5758
}
5859

0 commit comments

Comments
 (0)