/
PostgreSQLRangeType.java
146 lines (125 loc) · 4.77 KB
/
PostgreSQLRangeType.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package com.vladmihalcea.hibernate.type.range;
import com.vladmihalcea.hibernate.type.ImmutableType;
import com.vladmihalcea.hibernate.type.util.Configuration;
import org.hibernate.HibernateException;
import org.hibernate.annotations.common.reflection.XProperty;
import org.hibernate.annotations.common.reflection.java.JavaXMember;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.usertype.DynamicParameterizedType;
import org.postgresql.util.PGobject;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.util.Properties;
/**
* Maps a {@link Range} object type to a PostgreSQL <a href="https://www.postgresql.org/docs/current/rangetypes.html">range</a>
* column type.
* <p>
* Supported range types:
* <ul>
* <li>int4range</li>
* <li>int8range</li>
* <li>numrange</li>
* <li>tsrange</li>
* <li>tstzrange</li>
* <li>daterange</li>
* </ul>
* <p>
* For more details about how to use it,
* check out <a href="https://vladmihalcea.com/map-postgresql-range-column-type-jpa-hibernate/">this article</a>
* on <a href="https://vladmihalcea.com/">vladmihalcea.com</a>.
*
* @author Edgar Asatryan
* @author Vlad Mihalcea
*/
public class PostgreSQLRangeType extends ImmutableType<Range> implements DynamicParameterizedType {
public static final PostgreSQLRangeType INSTANCE = new PostgreSQLRangeType();
private Type type;
public PostgreSQLRangeType() {
super(Range.class);
}
public PostgreSQLRangeType(org.hibernate.type.spi.TypeBootstrapContext typeBootstrapContext) {
super(Range.class, new Configuration(typeBootstrapContext.getConfigurationSettings()));
}
@Override
public int getSqlType() {
return Types.OTHER;
}
@Override
protected Range get(ResultSet rs, int position, SharedSessionContractImplementor session, Object owner) throws SQLException {
PGobject pgObject = (PGobject) rs.getObject(position);
if (pgObject == null) {
return null;
}
String type = pgObject.getType();
String value = pgObject.getValue();
switch (type) {
case "int4range":
return Range.integerRange(value);
case "int8range":
return Range.longRange(value);
case "numrange":
return Range.bigDecimalRange(value);
case "tsrange":
return Range.localDateTimeRange(value);
case "tstzrange":
return Range.zonedDateTimeRange(value);
case "daterange":
return Range.localDateRange(value);
default:
throw new HibernateException(
new IllegalStateException("The range type [" + type + "] is not supported!")
);
}
}
@Override
protected void set(PreparedStatement st, Range range, int index, SharedSessionContractImplementor session) throws SQLException {
if (range == null) {
st.setNull(index, Types.OTHER);
} else {
PGobject object = new PGobject();
object.setType(determineRangeType(range));
object.setValue(range.asString());
st.setObject(index, object);
}
}
private static String determineRangeType(Range<?> range) {
Class<?> clazz = range.getClazz();
if (clazz.equals(Integer.class)) {
return "int4range";
} else if (clazz.equals(Long.class)) {
return "int8range";
} else if (clazz.equals(BigDecimal.class)) {
return "numrange";
} else if (clazz.equals(LocalDateTime.class)) {
return "tsrange";
} else if (clazz.equals(ZonedDateTime.class)) {
return "tstzrange";
} else if (clazz.equals(LocalDate.class)) {
return "daterange";
}
throw new HibernateException(
new IllegalStateException("The class [" + clazz.getName() + "] is not supported!")
);
}
@Override
public void setParameterValues(Properties parameters) {
final XProperty xProperty = (XProperty) parameters.get(DynamicParameterizedType.XPROPERTY);
if (xProperty instanceof JavaXMember) {
type = ((JavaXMember) xProperty).getJavaType();
} else {
type = ((ParameterType) parameters.get(PARAMETER_TYPE)).getReturnedClass();
}
}
public Class<?> getElementType() {
return type instanceof ParameterizedType ?
(Class<?>) ((ParameterizedType) type).getActualTypeArguments()[0] : null;
}
}