diff --git a/build.gradle b/build.gradle index 01d2470..0df27a3 100644 --- a/build.gradle +++ b/build.gradle @@ -7,7 +7,7 @@ plugins { } group = 'io.edpn.backend' -version = '0.0.1-SNAPSHOT' +version = '0.0.1.8-SNAPSHOT'//TODO REMOVE DEBUGGING VERSION AND BUMP TO LATEST compileJava.options.encoding = 'UTF-8' repositories { @@ -24,10 +24,12 @@ repositories { ext { mybatisSpringBootVersion = '3.0.2' + postgresqlVersion = '42.6.0' } dependencies { implementation "org.mybatis.spring.boot:mybatis-spring-boot-starter:${mybatisSpringBootVersion}" + implementation "org.postgresql:postgresql:${postgresqlVersion}" } diff --git a/src/main/java/io/edpn/backend/mybatisutil/StringDoubleMapTypeHandler.java b/src/main/java/io/edpn/backend/mybatisutil/StringDoubleMapTypeHandler.java new file mode 100644 index 0000000..47e5bd7 --- /dev/null +++ b/src/main/java/io/edpn/backend/mybatisutil/StringDoubleMapTypeHandler.java @@ -0,0 +1,52 @@ +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 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) +public class StringDoubleMapTypeHandler extends BaseTypeHandler> { + + @Override + public void setNonNullParameter(PreparedStatement ps, int i, Map parameter, JdbcType jdbcType) throws SQLException { + ps.setString(i, HStoreConverter.toString(parameter)); + } + + @Override + public Map getNullableResult(ResultSet rs, String columnName) throws SQLException { + return readMap(rs.getString(columnName)); + } + + @Override + public Map getNullableResult(ResultSet rs, int columnIndex) throws SQLException { + return readMap(rs.getString(columnIndex)); + } + + @Override + public Map getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { + return readMap(cs.getString(columnIndex)); + } + + private Map readMap(String hstring) throws SQLException { + if (hstring != null) { + Map map = new LinkedHashMap<>(); + Map rawMap = HStoreConverter.fromString(hstring); + for (Map.Entry entry : rawMap.entrySet()) { + map.put(entry.getKey(), Double.parseDouble(entry.getValue())); // convert from to + } + + return map; + } + return null; + } +} + diff --git a/src/main/java/module-info.java b/src/main/java/module-info.java index 265d4ef..6eb3562 100644 --- a/src/main/java/module-info.java +++ b/src/main/java/module-info.java @@ -2,6 +2,7 @@ requires java.sql; requires org.mybatis.spring; requires org.mybatis; + requires org.postgresql.jdbc; exports io.edpn.backend.mybatisutil; }