/
AbstractJsonSqlTypeDescriptor.java
60 lines (49 loc) · 2.01 KB
/
AbstractJsonSqlTypeDescriptor.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
package com.vladmihalcea.hibernate.type.json.internal;
import org.hibernate.type.descriptor.ValueExtractor;
import org.hibernate.type.descriptor.WrapperOptions;
import org.hibernate.type.descriptor.java.JavaTypeDescriptor;
import org.hibernate.type.descriptor.sql.BasicExtractor;
import org.hibernate.type.descriptor.sql.SqlTypeDescriptor;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
/**
* @author Vlad Mihalcea
*/
public abstract class AbstractJsonSqlTypeDescriptor implements SqlTypeDescriptor {
@Override
public int getSqlType() {
return Types.OTHER;
}
@Override
public boolean canBeRemapped() {
return true;
}
@Override
public <X> ValueExtractor<X> getExtractor(final JavaTypeDescriptor<X> javaTypeDescriptor) {
return new BasicExtractor<X>(javaTypeDescriptor, this) {
@Override
protected X doExtract(ResultSet rs, int paramIndex, WrapperOptions options) throws SQLException {
return javaTypeDescriptor.wrap(extractJson(rs, name), options);
}
@Override
protected X doExtract(CallableStatement statement, int index, WrapperOptions options) throws SQLException {
return javaTypeDescriptor.wrap(extractJson(statement, index), options);
}
@Override
protected X doExtract(CallableStatement statement, String name, WrapperOptions options) throws SQLException {
return javaTypeDescriptor.wrap(extractJson(statement, name), options);
}
};
}
protected Object extractJson(ResultSet rs, String name) throws SQLException {
return rs.getObject(name);
}
protected Object extractJson(CallableStatement statement, int index) throws SQLException {
return statement.getObject(index);
}
protected Object extractJson(CallableStatement statement, String name) throws SQLException {
return statement.getObject(name);
}
}