[jOOQ/jOOQ#5507] Added Hstore wrapper type

This commit is contained in:
Lukas Eder 2020-10-21 13:59:24 +02:00
parent 7d1f95adf6
commit ef0af31f15
4 changed files with 125 additions and 17 deletions

View File

@ -53,6 +53,13 @@
<artifactId>postgresql</artifactId>
</dependency>
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<scope>provided</scope>
<optional>true</optional>
</dependency>

View File

@ -39,7 +39,6 @@ package org.jooq.postgres.extensions.bindings;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Map;
import org.jooq.BindingGetResultSetContext;
import org.jooq.BindingGetStatementContext;
@ -47,6 +46,7 @@ import org.jooq.BindingRegisterContext;
import org.jooq.BindingSetStatementContext;
import org.jooq.Converter;
import org.jooq.postgres.extensions.converters.HstoreConverter;
import org.jooq.postgres.extensions.types.Hstore;
/**
* A binding for the PostgreSQL <code>hstore</code> data type.
@ -54,16 +54,16 @@ import org.jooq.postgres.extensions.converters.HstoreConverter;
* @author Dmitry Baev
* @author Lukas Eder
*/
public class HstoreBinding extends AbstractPostgresBinding<Object, Map<String, String>> {
public class HstoreBinding extends AbstractPostgresBinding<Object, Hstore> {
/**
* Generated UID
*/
private static final long serialVersionUID = 5809336497608771915L;
private static final Converter<Object, Map<String, String>> CONVERTER = new HstoreConverter();
private static final long serialVersionUID = 5809336497608771915L;
private static final Converter<Object, Hstore> CONVERTER = new HstoreConverter();
@Override
public Converter<Object, Map<String, String>> converter() {
public Converter<Object, Hstore> converter() {
return CONVERTER;
}
@ -73,12 +73,12 @@ public class HstoreBinding extends AbstractPostgresBinding<Object, Map<String, S
}
@Override
public void register(final BindingRegisterContext<Map<String, String>> ctx) throws SQLException {
public void register(final BindingRegisterContext<Hstore> ctx) throws SQLException {
ctx.statement().registerOutParameter(ctx.index(), Types.VARCHAR);
}
@Override
public void set(final BindingSetStatementContext<Map<String, String>> ctx) throws SQLException {
public void set(final BindingSetStatementContext<Hstore> ctx) throws SQLException {
Object value = ctx.convert(converter()).value();
ctx.statement().setString(ctx.index(), value == null ? null : "" + value);
@ -86,12 +86,12 @@ public class HstoreBinding extends AbstractPostgresBinding<Object, Map<String, S
@Override
public void get(final BindingGetResultSetContext<Map<String, String>> ctx) throws SQLException {
public void get(final BindingGetResultSetContext<Hstore> ctx) throws SQLException {
ctx.convert(converter()).value(ctx.resultSet().getString(ctx.index()));
}
@Override
public void get(final BindingGetStatementContext<Map<String, String>> ctx) throws SQLException {
public void get(final BindingGetStatementContext<Hstore> ctx) throws SQLException {
ctx.convert(converter()).value(ctx.statement().getString(ctx.index()));
}
}

View File

@ -37,9 +37,10 @@
*/
package org.jooq.postgres.extensions.converters;
import java.util.Map;
import static org.jooq.postgres.extensions.types.Hstore.hstore;
import org.jooq.impl.AbstractConverter;
import org.jooq.postgres.extensions.types.Hstore;
/**
* A binding for the PostgreSQL <code>hstore</code> data type.
@ -47,25 +48,24 @@ import org.jooq.impl.AbstractConverter;
* @author Dmitry Baev
* @author Lukas Eder
*/
public class HstoreConverter extends AbstractConverter<Object, Map<String, String>> {
public class HstoreConverter extends AbstractConverter<Object, Hstore> {
/**
* Generated UID
*/
private static final long serialVersionUID = -2852275331366617696L;
@SuppressWarnings({ "unchecked", "rawtypes" })
public HstoreConverter() {
super(Object.class, (Class<Map<String, String>>) (Class) Map.class);
super(Object.class, Hstore.class);
}
@Override
public Map<String, String> from(Object t) {
return t == null ? null : org.postgresql.util.HStoreConverter.fromString("" + t);
public Hstore from(Object t) {
return t == null ? null : hstore(org.postgresql.util.HStoreConverter.fromString("" + t));
}
@Override
public Object to(Map<String, String> u) {
return u == null ? null : org.postgresql.util.HStoreConverter.toString(u);
public Object to(Hstore u) {
return u == null ? null : org.postgresql.util.HStoreConverter.toString(u.data());
}
}

View File

@ -0,0 +1,101 @@
/*
* 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.util.HashMap;
import java.util.Map;
import org.jetbrains.annotations.NotNull;
/**
* A data type representing the PostgreSQL <code>hstore</code> type.
*
* @author Lukas Eder
*/
public final class Hstore implements Serializable {
private static final long serialVersionUID = 860591239448066408L;
private final Map<String, String> data;
private Hstore(Map<String, String> data) {
this.data = data == null ? new HashMap<>() : data;
}
@NotNull
public final Map<String, String> data() {
return data;
}
/**
* Create a new {@link Hstore} instance from string data input.
*/
@NotNull
public static final Hstore valueOf(Map<String, String> data) {
return new Hstore(data);
}
/**
* Create a new {@link Hstore} instance from map data input.
* <p>
* This is the same as {@link #valueOf(Map)}, but it can be static imported.
*/
@NotNull
public static final Hstore hstore(Map<String, String> data) {
return new Hstore(data);
}
@Override
public int hashCode() {
return data.hashCode();
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj instanceof Hstore)
return data.equals(((Hstore) obj).data);
return false;
}
@Override
public String toString() {
return String.valueOf(data);
}
}