Skip to content

Commit

Permalink
Adding type handler for hstore / Map.class
Browse files Browse the repository at this point in the history
Added bug version level
  • Loading branch information
Daniel-J-Mason committed Oct 24, 2023
1 parent b2e44b0 commit 6c025f6
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
4 changes: 3 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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}"
}


Expand Down
Original file line number Diff line number Diff line change
@@ -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<Map<String, Double>> {

@Override
public void setNonNullParameter(PreparedStatement ps, int i, Map<String, Double> parameter, JdbcType jdbcType) throws SQLException {
ps.setString(i, HStoreConverter.toString(parameter));
}

@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;
}
}

1 change: 1 addition & 0 deletions src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
requires java.sql;
requires org.mybatis.spring;
requires org.mybatis;
requires org.postgresql.jdbc;

exports io.edpn.backend.mybatisutil;
}

0 comments on commit 6c025f6

Please sign in to comment.