-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from ed-pilots-network/string-double-map-typeha…
…ndler add-string-double-map-typehandler
- Loading branch information
Showing
2 changed files
with
58 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
src/main/java/io/edpn/backend/mybatisutil/StringDoubleMapTypeHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package io.edpn.backend.mybatisutil; | ||
|
||
import org.apache.ibatis.type.BaseTypeHandler; | ||
import org.apache.ibatis.type.JdbcType; | ||
import org.apache.ibatis.type.MappedJdbcTypes; | ||
import org.apache.ibatis.type.MappedTypes; | ||
import org.postgresql.util.HStoreConverter; | ||
import org.postgresql.util.PGobject; | ||
|
||
import java.sql.CallableStatement; | ||
import java.sql.PreparedStatement; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
import java.util.LinkedHashMap; | ||
import java.util.Map; | ||
|
||
@MappedTypes(Map.class) | ||
@MappedJdbcTypes(JdbcType.OTHER) | ||
public class StringDoubleMapTypeHandler extends BaseTypeHandler<Map<String, Double>> { | ||
|
||
@Override | ||
public void setNonNullParameter(PreparedStatement ps, int i, Map<String, Double> parameter, JdbcType jdbcType) throws SQLException { | ||
PGobject pGobject = new PGobject(); | ||
pGobject.setType("hstore"); | ||
pGobject.setValue(HStoreConverter.toString(parameter)); | ||
ps.setObject(i, pGobject); | ||
} | ||
|
||
@Override | ||
public Map<String, Double> getNullableResult(ResultSet rs, String columnName) throws SQLException { | ||
return readMap(rs.getString(columnName)); | ||
} | ||
|
||
@Override | ||
public Map<String, Double> getNullableResult(ResultSet rs, int columnIndex) throws SQLException { | ||
return readMap(rs.getString(columnIndex)); | ||
} | ||
|
||
@Override | ||
public Map<String, Double> getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { | ||
return readMap(cs.getString(columnIndex)); | ||
} | ||
|
||
private Map<String, Double> readMap(String hstring) throws SQLException { | ||
if (hstring != null) { | ||
Map<String, Double> map = new LinkedHashMap<>(); | ||
Map<String, String> rawMap = HStoreConverter.fromString(hstring); | ||
for (Map.Entry<String, String> entry : rawMap.entrySet()) { | ||
map.put(entry.getKey(), Double.parseDouble(entry.getValue())); // convert from <String, String> to <String,Double> | ||
} | ||
|
||
return map; | ||
} | ||
return null; | ||
} | ||
} | ||
|