Search in sources :

Example 1 with WrapperOptions

use of org.hibernate.type.descriptor.WrapperOptions in project hibernate-orm by hibernate.

the class PGGeometryTypeDescriptor method getBinder.

@Override
public <X> ValueBinder<X> getBinder(final JavaTypeDescriptor<X> javaTypeDescriptor) {
    return new BasicBinder<X>(javaTypeDescriptor, this) {

        @Override
        protected void doBind(PreparedStatement st, X value, int index, WrapperOptions options) throws SQLException {
            final WkbEncoder encoder = Wkb.newEncoder(Wkb.Dialect.POSTGIS_EWKB_1);
            final Geometry geometry = getJavaDescriptor().unwrap(value, Geometry.class, options);
            final byte[] bytes = encoder.encode(geometry, ByteOrder.NDR).toByteArray();
            st.setBytes(index, bytes);
        }

        @Override
        protected void doBind(CallableStatement st, X value, String name, WrapperOptions options) throws SQLException {
            final WkbEncoder encoder = Wkb.newEncoder(Wkb.Dialect.POSTGIS_EWKB_1);
            final Geometry geometry = getJavaDescriptor().unwrap(value, Geometry.class, options);
            final byte[] bytes = encoder.encode(geometry, ByteOrder.NDR).toByteArray();
            st.setBytes(name, bytes);
        }
    };
}
Also used : Geometry(org.geolatte.geom.Geometry) WkbEncoder(org.geolatte.geom.codec.WkbEncoder) CallableStatement(java.sql.CallableStatement) WrapperOptions(org.hibernate.type.descriptor.WrapperOptions) PreparedStatement(java.sql.PreparedStatement) BasicBinder(org.hibernate.type.descriptor.sql.BasicBinder)

Example 2 with WrapperOptions

use of org.hibernate.type.descriptor.WrapperOptions in project hibernate-orm by hibernate.

the class SqlServer2008GeometryTypeDescriptor method getBinder.

@Override
public <X> ValueBinder<X> getBinder(final JavaTypeDescriptor<X> javaTypeDescriptor) {
    return new BasicBinder<X>(javaTypeDescriptor, this) {

        @Override
        protected void doBind(PreparedStatement st, X value, int index, WrapperOptions options) throws SQLException {
            final Geometry geometry = getJavaDescriptor().unwrap(value, Geometry.class, options);
            final byte[] bytes = Encoders.encode(geometry);
            st.setObject(index, bytes);
        }

        @Override
        protected void doBind(CallableStatement st, X value, String name, WrapperOptions options) throws SQLException {
            final Geometry geometry = getJavaDescriptor().unwrap(value, Geometry.class, options);
            final byte[] bytes = Encoders.encode(geometry);
            st.setObject(name, bytes);
        }
    };
}
Also used : Geometry(org.geolatte.geom.Geometry) CallableStatement(java.sql.CallableStatement) WrapperOptions(org.hibernate.type.descriptor.WrapperOptions) PreparedStatement(java.sql.PreparedStatement) BasicBinder(org.hibernate.type.descriptor.sql.BasicBinder)

Example 3 with WrapperOptions

use of org.hibernate.type.descriptor.WrapperOptions in project hibernate-orm by hibernate.

the class AttributeConverterSqlTypeDescriptorAdapter method getBinder.

// Binding ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@Override
@SuppressWarnings("unchecked")
public <X> ValueBinder<X> getBinder(JavaTypeDescriptor<X> javaTypeDescriptor) {
    // Get the binder for the intermediate type representation
    final ValueBinder realBinder = delegate.getBinder(intermediateJavaTypeDescriptor);
    return new ValueBinder<X>() {

        @Override
        public void bind(PreparedStatement st, X value, int index, WrapperOptions options) throws SQLException {
            final Object convertedValue;
            try {
                convertedValue = converter.convertToDatabaseColumn(value);
            } catch (PersistenceException pe) {
                throw pe;
            } catch (RuntimeException re) {
                throw new PersistenceException("Error attempting to apply AttributeConverter", re);
            }
            log.debugf("Converted value on binding : %s -> %s", value, convertedValue);
            realBinder.bind(st, convertedValue, index, options);
        }

        @Override
        public void bind(CallableStatement st, X value, String name, WrapperOptions options) throws SQLException {
            final Object convertedValue;
            try {
                convertedValue = converter.convertToDatabaseColumn(value);
            } catch (PersistenceException pe) {
                throw pe;
            } catch (RuntimeException re) {
                throw new PersistenceException("Error attempting to apply AttributeConverter", re);
            }
            log.debugf("Converted value on binding : %s -> %s", value, convertedValue);
            realBinder.bind(st, convertedValue, name, options);
        }
    };
}
Also used : CallableStatement(java.sql.CallableStatement) WrapperOptions(org.hibernate.type.descriptor.WrapperOptions) PersistenceException(javax.persistence.PersistenceException) PreparedStatement(java.sql.PreparedStatement) ValueBinder(org.hibernate.type.descriptor.ValueBinder)

Example 4 with WrapperOptions

use of org.hibernate.type.descriptor.WrapperOptions in project hibernate-orm by hibernate.

the class AttributeConverterSqlTypeDescriptorAdapter method getExtractor.

// Extraction ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@Override
public <X> ValueExtractor<X> getExtractor(JavaTypeDescriptor<X> javaTypeDescriptor) {
    // Get the extractor for the intermediate type representation
    final ValueExtractor realExtractor = delegate.getExtractor(intermediateJavaTypeDescriptor);
    return new ValueExtractor<X>() {

        @Override
        public X extract(ResultSet rs, String name, WrapperOptions options) throws SQLException {
            return doConversion(realExtractor.extract(rs, name, options));
        }

        @Override
        public X extract(CallableStatement statement, int index, WrapperOptions options) throws SQLException {
            return doConversion(realExtractor.extract(statement, index, options));
        }

        @Override
        public X extract(CallableStatement statement, String[] paramNames, WrapperOptions options) throws SQLException {
            if (paramNames.length > 1) {
                throw new IllegalArgumentException("Basic value extraction cannot handle multiple output parameters");
            }
            return doConversion(realExtractor.extract(statement, paramNames, options));
        }

        @SuppressWarnings("unchecked")
        private X doConversion(Object extractedValue) {
            try {
                X convertedValue = (X) converter.convertToEntityAttribute(extractedValue);
                log.debugf("Converted value on extraction: %s -> %s", extractedValue, convertedValue);
                return convertedValue;
            } catch (PersistenceException pe) {
                throw pe;
            } catch (RuntimeException re) {
                throw new PersistenceException("Error attempting to apply AttributeConverter", re);
            }
        }
    };
}
Also used : CallableStatement(java.sql.CallableStatement) WrapperOptions(org.hibernate.type.descriptor.WrapperOptions) ResultSet(java.sql.ResultSet) PersistenceException(javax.persistence.PersistenceException) ValueExtractor(org.hibernate.type.descriptor.ValueExtractor)

Example 5 with WrapperOptions

use of org.hibernate.type.descriptor.WrapperOptions in project hibernate-orm by hibernate.

the class GeoDBGeometryTypeDescriptor method getBinder.

@Override
public <X> ValueBinder<X> getBinder(final JavaTypeDescriptor<X> javaTypeDescriptor) {
    return new BasicBinder<X>(javaTypeDescriptor, this) {

        @Override
        protected void doBind(PreparedStatement st, X value, int index, WrapperOptions options) throws SQLException {
            final Geometry geometry = getJavaDescriptor().unwrap(value, Geometry.class, options);
            st.setBytes(index, GeoDbWkb.to(geometry));
        }

        @Override
        protected void doBind(CallableStatement st, X value, String name, WrapperOptions options) throws SQLException {
            final Geometry geometry = getJavaDescriptor().unwrap(value, Geometry.class, options);
            st.setBytes(name, GeoDbWkb.to(geometry));
        }
    };
}
Also used : Geometry(org.geolatte.geom.Geometry) CallableStatement(java.sql.CallableStatement) WrapperOptions(org.hibernate.type.descriptor.WrapperOptions) PreparedStatement(java.sql.PreparedStatement) BasicBinder(org.hibernate.type.descriptor.sql.BasicBinder)

Aggregations

CallableStatement (java.sql.CallableStatement)6 WrapperOptions (org.hibernate.type.descriptor.WrapperOptions)6 PreparedStatement (java.sql.PreparedStatement)5 Geometry (org.geolatte.geom.Geometry)4 BasicBinder (org.hibernate.type.descriptor.sql.BasicBinder)4 PersistenceException (javax.persistence.PersistenceException)2 WkbEncoder (org.geolatte.geom.codec.WkbEncoder)2 ResultSet (java.sql.ResultSet)1 ByteBuffer (org.geolatte.geom.ByteBuffer)1 ValueBinder (org.hibernate.type.descriptor.ValueBinder)1 ValueExtractor (org.hibernate.type.descriptor.ValueExtractor)1