1 package com.vladmihalcea.hibernate.type.basic;
2
3 import com.vladmihalcea.hibernate.type.ImmutableType;
4 import com.vladmihalcea.hibernate.util.ReflectionUtils;
5 import org.hibernate.engine.spi.SharedSessionContractImplementor;
6
7 import java.sql.PreparedStatement;
8 import java.sql.ResultSet;
9 import java.sql.SQLException;
10 import java.sql.Types;
11
12
21 public class PostgreSQLInetType extends ImmutableType<Inet> {
22
23 public static final PostgreSQLInetType INSTANCE = new PostgreSQLInetType();
24
25 public PostgreSQLInetType() {
26 super(Inet.class);
27 }
28
29 @Override
30 public int[] sqlTypes() {
31 return new int[]{Types.OTHER};
32 }
33
34 @Override
35 public Inet get(ResultSet rs, String[] names, SharedSessionContractImplementor session, Object owner) throws SQLException {
36 String ip = rs.getString(names[0]);
37 return (ip != null) ? new Inet(ip) : null;
38 }
39
40 @Override
41 public void set(PreparedStatement st, Inet value, int index, SharedSessionContractImplementor session) throws SQLException {
42 if (value == null) {
43 st.setNull(index, Types.OTHER);
44 } else {
45 Object holder = ReflectionUtils.newInstance("org.postgresql.util.PGobject");
46 ReflectionUtils.invokeSetter(holder, "type", "inet");
47 ReflectionUtils.invokeSetter(holder, "value", value.getAddress());
48 st.setObject(index, holder);
49 }
50 }
51 }