|
| 1 | +/* Hibernate, Relational Persistence for Idiomatic Java |
| 2 | + * |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + * Copyright: Red Hat Inc. and Hibernate Authors |
| 5 | + */ |
| 6 | +package org.hibernate.reactive.type.descriptor.jdbc; |
| 7 | + |
| 8 | +import java.sql.CallableStatement; |
| 9 | +import java.sql.PreparedStatement; |
| 10 | +import java.sql.ResultSet; |
| 11 | +import java.sql.SQLException; |
| 12 | + |
| 13 | +import org.hibernate.metamodel.mapping.EmbeddableMappingType; |
| 14 | +import org.hibernate.metamodel.spi.RuntimeModelCreationContext; |
| 15 | +import org.hibernate.type.descriptor.ValueBinder; |
| 16 | +import org.hibernate.type.descriptor.ValueExtractor; |
| 17 | +import org.hibernate.type.descriptor.WrapperOptions; |
| 18 | +import org.hibernate.type.descriptor.java.JavaType; |
| 19 | +import org.hibernate.type.descriptor.jdbc.AggregateJdbcType; |
| 20 | +import org.hibernate.type.descriptor.jdbc.BasicBinder; |
| 21 | +import org.hibernate.type.descriptor.jdbc.BasicExtractor; |
| 22 | +import org.hibernate.type.descriptor.jdbc.JsonJdbcType; |
| 23 | + |
| 24 | +import io.vertx.core.json.JsonObject; |
| 25 | + |
| 26 | +/** |
| 27 | + * Map a JSON column as {@link JsonObject} |
| 28 | + */ |
| 29 | +public class ReactiveJsonJdbcType extends JsonJdbcType { |
| 30 | + |
| 31 | + public static final ReactiveJsonJdbcType INSTANCE = new ReactiveJsonJdbcType( null ); |
| 32 | + |
| 33 | + protected ReactiveJsonJdbcType(EmbeddableMappingType embeddableMappingType) { |
| 34 | + super( embeddableMappingType ); |
| 35 | + } |
| 36 | + |
| 37 | + @Override |
| 38 | + public AggregateJdbcType resolveAggregateJdbcType( |
| 39 | + EmbeddableMappingType mappingType, String sqlType, RuntimeModelCreationContext creationContext) { |
| 40 | + return new ReactiveJsonJdbcType( mappingType ); |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public <X> ValueBinder<X> getBinder(JavaType<X> javaType) { |
| 45 | + return new BasicBinder<>( javaType, this ) { |
| 46 | + @Override |
| 47 | + protected void doBind(PreparedStatement st, X value, int index, WrapperOptions options) |
| 48 | + throws SQLException { |
| 49 | + st.setObject( index, toJsonObject( value, javaType, options ) ); |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + protected void doBind(CallableStatement st, X value, String name, WrapperOptions options) |
| 54 | + throws SQLException { |
| 55 | + st.setObject( name, toJsonObject( value, javaType, options ) ); |
| 56 | + } |
| 57 | + }; |
| 58 | + } |
| 59 | + |
| 60 | + protected <X> JsonObject toJsonObject(X value, JavaType<X> javaType, WrapperOptions options) { |
| 61 | + return new JsonObject( this.toString( value, javaType, options ) ); |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public <X> ValueExtractor<X> getExtractor(JavaType<X> javaType) { |
| 66 | + return new BasicExtractor<>( javaType, this ) { |
| 67 | + @Override |
| 68 | + protected X doExtract(ResultSet rs, int paramIndex, WrapperOptions options) throws SQLException { |
| 69 | + return fromString( toJsonString( rs.getObject( paramIndex ) ), getJavaType(), options ); |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + protected X doExtract(CallableStatement statement, int index, WrapperOptions options) throws SQLException { |
| 74 | + return fromString( toJsonString( statement.getObject( index ) ), getJavaType(), options ); |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + protected X doExtract(CallableStatement statement, String name, WrapperOptions options) |
| 79 | + throws SQLException { |
| 80 | + return fromString( toJsonString( statement.getObject( name ) ), getJavaType(), options ); |
| 81 | + } |
| 82 | + }; |
| 83 | + } |
| 84 | + |
| 85 | + private static String toJsonString(Object value) { |
| 86 | + if ( value == null ) { |
| 87 | + return null; |
| 88 | + } |
| 89 | + // Value should be a JsonObject |
| 90 | + return value.toString(); |
| 91 | + } |
| 92 | +} |
0 commit comments