Hi,

here's the relevant code:

public class Inet4AddressType implements ExtendedType {

    @Override
    public String getClassName() {
        return Inet4Address.class.getName();
    }

    @Override
public void setJdbcObject(PreparedStatement statement, Object value, int pos, int type, int scale) throws Exception {
        if (value == null) {
            statement.setNull(pos, type);
        } else {
            if (type == Types.BIGINT) {
                byte[] ip = ((Inet4Address) value).getAddress();
                long ipNum = 0;
ipNum += (long) (ip[0] & 0xFF) << 24; //TODO this needs to be long-cast or it will overflow
                ipNum += (ip[1] & 0xFF) << 16;
                ipNum += (ip[2] & 0xFF) << 8;
                ipNum += (ip[3] & 0xFF);
                statement.setLong(pos, ipNum);
            } else {
throw new IllegalArgumentException("Only BIGINT can be mapped as '" + getClassName() + "', got " + TypesMapping.getSqlNameByType(type));
            }
        }
    }

    @Override
public Object materializeObject(ResultSet rs, int index, int type) throws Exception {
        if (type == Types.BIGINT) {
            long l = rs.getLong(index);
return InetAddress.getByAddress(new byte[]{(byte) ((l >> 24) & 0xFF),
                        (byte) ((l >> 16) & 0xFF),
                        (byte) ((l >> 8) & 0xFF),
                        (byte) (l)});
        } else {
throw new IllegalArgumentException(getClassName() + "' can only be constructed from BIGINT, got " + TypesMapping.getSqlNameByType(type));
        }
    }
}

Reply via email to