/
JsonBytesSqlTypeDescriptor.java
98 lines (79 loc) · 3.04 KB
/
JsonBytesSqlTypeDescriptor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package com.vladmihalcea.hibernate.type.json.internal;
import org.hibernate.dialect.Database;
import org.hibernate.type.descriptor.ValueBinder;
import org.hibernate.type.descriptor.WrapperOptions;
import org.hibernate.type.descriptor.java.JavaTypeDescriptor;
import org.hibernate.type.descriptor.sql.BasicBinder;
import java.io.UnsupportedEncodingException;
import java.sql.*;
import java.util.HashMap;
import java.util.Map;
/**
* @author Vlad Mihalcea
*/
public class JsonBytesSqlTypeDescriptor extends AbstractJsonSqlTypeDescriptor {
public static final JsonBytesSqlTypeDescriptor INSTANCE = new JsonBytesSqlTypeDescriptor();
private static final Map<Database, JsonBytesSqlTypeDescriptor> INSTANCE_MAP = new HashMap<>();
static {
INSTANCE_MAP.put(Database.H2, INSTANCE);
INSTANCE_MAP.put(Database.ORACLE, new JsonBytesSqlTypeDescriptor(2016));
}
public static JsonBytesSqlTypeDescriptor of(Database database) {
return INSTANCE_MAP.get(database);
}
public static final String CHARSET = "UTF8";
private final int jdbcType;
public JsonBytesSqlTypeDescriptor() {
this.jdbcType = Types.BINARY;
}
public JsonBytesSqlTypeDescriptor(int jdbcType) {
this.jdbcType = jdbcType;
}
@Override
public int getSqlType() {
return jdbcType;
}
@Override
public <X> ValueBinder<X> getBinder(final JavaTypeDescriptor<X> javaTypeDescriptor) {
return new BasicBinder<X>(javaTypeDescriptor, this) {
@Override
protected void doBind(PreparedStatement st, X value, int index, WrapperOptions options) throws SQLException {
st.setBytes(index, toJsonBytes(javaTypeDescriptor.unwrap(value, String.class, options)));
}
@Override
protected void doBind(CallableStatement st, X value, String name, WrapperOptions options)
throws SQLException {
st.setBytes(name, toJsonBytes(javaTypeDescriptor.unwrap(value, String.class, options)));
}
};
}
@Override
protected Object extractJson(ResultSet rs, String name) throws SQLException {
return fromJsonBytes(rs.getBytes(name));
}
@Override
protected Object extractJson(CallableStatement statement, int index) throws SQLException {
return fromJsonBytes(statement.getBytes(index));
}
@Override
protected Object extractJson(CallableStatement statement, String name) throws SQLException {
return fromJsonBytes(statement.getBytes(name));
}
protected byte[] toJsonBytes(String jsonValue) {
try {
return jsonValue.getBytes(CHARSET);
} catch (UnsupportedEncodingException e) {
throw new IllegalStateException(e);
}
}
protected String fromJsonBytes(byte[] jsonBytes) {
if (jsonBytes == null) {
return null;
}
try {
return new String(jsonBytes, CHARSET);
} catch (UnsupportedEncodingException e) {
throw new IllegalStateException(e);
}
}
}