[jOOQ/jOOQ#2092] Add support for the Postgres INET data type

(java.net.InetAddress)
This commit is contained in:
Lukas Eder 2022-02-28 13:17:23 +01:00
parent 44bb4aa7c3
commit f264904485
13 changed files with 713 additions and 18 deletions

View File

@ -0,0 +1,81 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Other licenses:
* -----------------------------------------------------------------------------
* Commercial licenses for this work are available. These replace the above
* ASL 2.0 and offer limited warranties, support, maintenance, and commercial
* database integrations.
*
* For more information, please visit: http://www.jooq.org/licenses
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
package org.jooq.postgres.extensions.bindings;
import java.sql.SQLException;
import java.sql.Types;
import org.jooq.BindingGetResultSetContext;
import org.jooq.BindingGetStatementContext;
import org.jooq.BindingRegisterContext;
import org.jooq.BindingSetStatementContext;
import org.jooq.Converter;
import org.jooq.postgres.extensions.converters.InetConverter;
import org.jooq.postgres.extensions.types.AbstractInet;
import org.jooq.postgres.extensions.types.Inet;
/**
* A binding for the PostgreSQL <code>inet</code> or <code>cidr</code> data
* type.
*
* @author Lukas Eder
*/
abstract class AbstractInetBinding<U extends AbstractInet> extends AbstractPostgresBinding<Object, U> {
@Override
public void register(final BindingRegisterContext<U> ctx) throws SQLException {
ctx.statement().registerOutParameter(ctx.index(), Types.VARCHAR);
}
@Override
public void set(final BindingSetStatementContext<U> ctx) throws SQLException {
Object value = ctx.convert(converter()).value();
ctx.statement().setString(ctx.index(), value == null ? null : "" + value);
}
@Override
public void get(final BindingGetResultSetContext<U> ctx) throws SQLException {
ctx.convert(converter()).value(ctx.resultSet().getString(ctx.index()));
}
@Override
public void get(final BindingGetStatementContext<U> ctx) throws SQLException {
ctx.convert(converter()).value(ctx.statement().getString(ctx.index()));
}
}

View File

@ -39,7 +39,6 @@ package org.jooq.postgres.extensions.bindings;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.Map;
import org.jooq.BindingGetSQLInputContext;
import org.jooq.BindingSQLContext;
@ -53,11 +52,6 @@ import org.jooq.impl.AbstractBinding;
*/
public abstract class AbstractPostgresBinding<T, U> extends AbstractBinding<T, U> {
/**
* Generated UID
*/
private static final long serialVersionUID = 689952685043626697L;
/**
* Provide the data type name for casts.
* <p>

View File

@ -0,0 +1,62 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Other licenses:
* -----------------------------------------------------------------------------
* Commercial licenses for this work are available. These replace the above
* ASL 2.0 and offer limited warranties, support, maintenance, and commercial
* database integrations.
*
* For more information, please visit: http://www.jooq.org/licenses
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
package org.jooq.postgres.extensions.bindings;
import org.jooq.Converter;
import org.jooq.postgres.extensions.converters.CidrConverter;
import org.jooq.postgres.extensions.types.Cidr;
/**
* A binding for the PostgreSQL <code>cidr</code> data type.
*
* @author Lukas Eder
*/
public class CidrBinding extends AbstractInetBinding<Cidr> {
private static final Converter<Object, Cidr> CONVERTER = new CidrConverter();
@Override
public Converter<Object, Cidr> converter() {
return CONVERTER;
}
@Override
protected String castType() {
return "cidr";
}
}

View File

@ -56,11 +56,7 @@ import org.jooq.postgres.extensions.types.Hstore;
*/
public class HstoreBinding extends AbstractPostgresBinding<Object, Hstore> {
/**
* Generated UID
*/
private static final long serialVersionUID = 5809336497608771915L;
private static final Converter<Object, Hstore> CONVERTER = new HstoreConverter();
private static final Converter<Object, Hstore> CONVERTER = new HstoreConverter();
@Override
public Converter<Object, Hstore> converter() {

View File

@ -0,0 +1,62 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Other licenses:
* -----------------------------------------------------------------------------
* Commercial licenses for this work are available. These replace the above
* ASL 2.0 and offer limited warranties, support, maintenance, and commercial
* database integrations.
*
* For more information, please visit: http://www.jooq.org/licenses
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
package org.jooq.postgres.extensions.bindings;
import org.jooq.Converter;
import org.jooq.postgres.extensions.converters.InetConverter;
import org.jooq.postgres.extensions.types.Inet;
/**
* A binding for the PostgreSQL <code>inet</code> data type.
*
* @author Lukas Eder
*/
public class InetBinding extends AbstractInetBinding<Inet> {
private static final Converter<Object, Inet> CONVERTER = new InetConverter();
@Override
public Converter<Object, Inet> converter() {
return CONVERTER;
}
@Override
protected String castType() {
return "inet";
}
}

View File

@ -0,0 +1,88 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Other licenses:
* -----------------------------------------------------------------------------
* Commercial licenses for this work are available. These replace the above
* ASL 2.0 and offer limited warranties, support, maintenance, and commercial
* database integrations.
*
* For more information, please visit: http://www.jooq.org/licenses
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
package org.jooq.postgres.extensions.converters;
import java.net.InetAddress;
import java.net.UnknownHostException;
import org.jooq.exception.DataTypeException;
import org.jooq.impl.AbstractConverter;
import org.jooq.postgres.extensions.types.AbstractInet;
/**
* A converter for the PostgreSQL <code>inet</code> or <code>cidr</code> data
* type.
*
* @author Lukas Eder
*/
abstract class AbstractInetConverter<U extends AbstractInet> extends AbstractConverter<Object, U> {
public AbstractInetConverter(Class<U> uType) {
super(Object.class, uType);
}
abstract U construct(InetAddress address, Integer prefix);
@Override
public U from(Object t) {
if (t == null)
return null;
String[] s = t.toString().split("/");
try {
InetAddress a = InetAddress.getByName(s[0]);
if (s.length == 1)
return construct(a, null);
else
return construct(a, Integer.valueOf(s[1]));
}
catch (UnknownHostException e) {
throw new DataTypeException("Cannot parse InetAddress", e);
}
}
@Override
public Object to(AbstractInet u) {
return u == null
? null
: u.prefix() == null
? u.address().getHostAddress()
: u.address().getHostAddress() + "/" + u.prefix();
}
}

View File

@ -0,0 +1,61 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Other licenses:
* -----------------------------------------------------------------------------
* Commercial licenses for this work are available. These replace the above
* ASL 2.0 and offer limited warranties, support, maintenance, and commercial
* database integrations.
*
* For more information, please visit: http://www.jooq.org/licenses
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
package org.jooq.postgres.extensions.converters;
import static org.jooq.postgres.extensions.types.Cidr.cidr;
import java.net.InetAddress;
import org.jooq.postgres.extensions.types.Cidr;
/**
* A converter for the PostgreSQL <code>cidr</code> data type.
*
* @author Lukas Eder
*/
public class CidrConverter extends AbstractInetConverter<Cidr> {
public CidrConverter() {
super(Cidr.class);
}
@Override
final Cidr construct(InetAddress address, Integer prefix) {
return cidr(address, prefix);
}
}

View File

@ -43,18 +43,13 @@ import org.jooq.impl.AbstractConverter;
import org.jooq.postgres.extensions.types.Hstore;
/**
* A binding for the PostgreSQL <code>hstore</code> data type.
* A converter for the PostgreSQL <code>hstore</code> data type.
*
* @author Dmitry Baev
* @author Lukas Eder
*/
public class HstoreConverter extends AbstractConverter<Object, Hstore> {
/**
* Generated UID
*/
private static final long serialVersionUID = -2852275331366617696L;
public HstoreConverter() {
super(Object.class, Hstore.class);
}

View File

@ -0,0 +1,61 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Other licenses:
* -----------------------------------------------------------------------------
* Commercial licenses for this work are available. These replace the above
* ASL 2.0 and offer limited warranties, support, maintenance, and commercial
* database integrations.
*
* For more information, please visit: http://www.jooq.org/licenses
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
package org.jooq.postgres.extensions.converters;
import static org.jooq.postgres.extensions.types.Inet.inet;
import java.net.InetAddress;
import org.jooq.postgres.extensions.types.Inet;
/**
* A converter for the PostgreSQL <code>inet</code> data type.
*
* @author Lukas Eder
*/
public class InetConverter extends AbstractInetConverter<Inet> {
public InetConverter() {
super(Inet.class);
}
@Override
final Inet construct(InetAddress address, Integer prefix) {
return inet(address, prefix);
}
}

View File

@ -0,0 +1,90 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Other licenses:
* -----------------------------------------------------------------------------
* Commercial licenses for this work are available. These replace the above
* ASL 2.0 and offer limited warranties, support, maintenance, and commercial
* database integrations.
*
* For more information, please visit: http://www.jooq.org/licenses
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
package org.jooq.postgres.extensions.types;
import java.io.Serializable;
import java.net.InetAddress;
import java.util.Objects;
import org.jetbrains.annotations.NotNull;
/**
* A data type representing the PostgreSQL <code>inet</code> or
* <code>cidr</code> type.
*
* @author Lukas Eder
*/
public abstract class AbstractInet implements Serializable {
private final InetAddress address;
private final Integer prefix;
AbstractInet(InetAddress address, Integer prefix) {
this.address = address;
this.prefix = prefix;
}
@NotNull
public final InetAddress address() {
return address;
}
public /* non-final */ Integer prefix() {
return prefix;
}
@Override
public int hashCode() {
return address.hashCode();
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj instanceof AbstractInet)
return address.equals(((AbstractInet) obj).address) && Objects.equals(prefix, ((AbstractInet) obj).prefix);
return false;
}
@Override
public String toString() {
return prefix == null ? address.getHostAddress() : address.getHostAddress() + "/" + prefix;
}
}

View File

@ -0,0 +1,89 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Other licenses:
* -----------------------------------------------------------------------------
* Commercial licenses for this work are available. These replace the above
* ASL 2.0 and offer limited warranties, support, maintenance, and commercial
* database integrations.
*
* For more information, please visit: http://www.jooq.org/licenses
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
package org.jooq.postgres.extensions.types;
import java.net.InetAddress;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* A data type representing the PostgreSQL <code>cidr</code> type.
*
* @author Lukas Eder
*/
public final class Cidr extends AbstractInet {
private Cidr(InetAddress address, Integer prefix) {
super(address, prefix);
}
@Override
@NotNull
public final Integer prefix() {
return super.prefix();
}
/**
* Create a new {@link Cidr} instance.
*/
@NotNull
public static final Cidr valueOf(InetAddress address, Integer prefix) {
return new Cidr(address, prefix);
}
/**
* Create a new {@link Cidr} instance.
* <p>
* This is the same as {@link #valueOf(InetAddress, Integer)}, but it can be
* static imported.
*/
@NotNull
public static final Cidr cidr(InetAddress address, Integer prefix) {
return new Cidr(address, prefix);
}
/**
* Create a new {@link Cidr} instance, or <code>null</code> if the input
* address is <code>null</code>.
*/
@Nullable
public static final Cidr cidrOrNull(InetAddress address, Integer prefix) {
return address == null ? null : new Cidr(address, prefix);
}
}

View File

@ -51,7 +51,6 @@ import org.jetbrains.annotations.Nullable;
*/
public final class Hstore implements Serializable {
private static final long serialVersionUID = 860591239448066408L;
private final Map<String, String> data;
private Hstore(Map<String, String> data) {

View File

@ -0,0 +1,117 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Other licenses:
* -----------------------------------------------------------------------------
* Commercial licenses for this work are available. These replace the above
* ASL 2.0 and offer limited warranties, support, maintenance, and commercial
* database integrations.
*
* For more information, please visit: http://www.jooq.org/licenses
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
package org.jooq.postgres.extensions.types;
import java.net.InetAddress;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* A data type representing the PostgreSQL <code>inet</code> type.
*
* @author Lukas Eder
*/
public final class Inet extends AbstractInet {
private Inet(InetAddress address, Integer prefix) {
super(address, prefix);
}
@Override
@Nullable
public final Integer prefix() {
return super.prefix();
}
/**
* Create a new {@link Inet} instance.
*/
@NotNull
public static final Inet valueOf(InetAddress address) {
return new Inet(address, null);
}
/**
* Create a new {@link Inet} instance.
*/
@NotNull
public static final Inet valueOf(InetAddress address, Integer prefix) {
return new Inet(address, prefix);
}
/**
* Create a new {@link Inet} instance.
* <p>
* This is the same as {@link #valueOf(InetAddress)}, but it can be static
* imported.
*/
@NotNull
public static final Inet inet(InetAddress address) {
return new Inet(address, null);
}
/**
* Create a new {@link Inet} instance.
* <p>
* This is the same as {@link #valueOf(InetAddress, Integer)}, but it can be
* static imported.
*/
@NotNull
public static final Inet inet(InetAddress address, Integer prefix) {
return new Inet(address, prefix);
}
/**
* Create a new {@link Inet} instance, or <code>null</code> if the input
* address is <code>null</code>.
*/
@Nullable
public static final Inet inetOrNull(InetAddress address) {
return address == null ? null : new Inet(address, null);
}
/**
* Create a new {@link Inet} instance, or <code>null</code> if the input
* address is <code>null</code>.
*/
@Nullable
public static final Inet inetOrNull(InetAddress address, Integer prefix) {
return address == null ? null : new Inet(address, prefix);
}
}