HHH-6509 - Simplifies SqlServer2008 ValueBinder and ValueExtractor

implementations.
This commit is contained in:
Karel Maesen 2012-10-12 22:15:56 +02:00 committed by Steve Ebersole
parent 0e17f9adf8
commit ea833fcdbb
4 changed files with 67 additions and 123 deletions

View File

@ -76,6 +76,12 @@ public class JTSGeometryJavaTypeDescriptor extends AbstractTypeDescriptor<Geomet
@Override
public <X> Geometry wrap(X value, WrapperOptions options) {
if ( value == null ) {
return null;
}
if ( Geometry.class.isInstance( value ) ) {
return (Geometry) value;
}
if ( String.class.isInstance( value ) ) {
return fromString( (String) value );
}

View File

@ -21,12 +21,18 @@
package org.hibernate.spatial.dialect.sqlserver;
import java.sql.Types;
import com.vividsolutions.jts.geom.Geometry;
import org.hibernate.spatial.GeometrySqlTypeDescriptor;
import org.hibernate.spatial.dialect.sqlserver.convertors.Decoders;
import org.hibernate.spatial.dialect.sqlserver.convertors.Encoders;
import org.hibernate.type.descriptor.ValueBinder;
import org.hibernate.type.descriptor.ValueExtractor;
import org.hibernate.type.descriptor.WrapperOptions;
import org.hibernate.type.descriptor.java.JavaTypeDescriptor;
import org.hibernate.type.descriptor.sql.BasicBinder;
import org.hibernate.type.descriptor.sql.BasicExtractor;
import java.sql.*;
/**
* @author Karel Maesen, Geovise BVBA
@ -53,12 +59,63 @@ public class SqlServer2008GeometryTypeDescriptor extends GeometrySqlTypeDescript
@Override
public <X> ValueBinder<X> getBinder(final JavaTypeDescriptor<X> javaTypeDescriptor) {
return (ValueBinder<X>) new SqlServer2008GeometryValueBinder( javaTypeDescriptor );
return new BasicBinder<X>(javaTypeDescriptor, this){
@Override
protected void doBind(PreparedStatement st, X value, int index, WrapperOptions options) throws SQLException {
Geometry jtsGeom = getJavaDescriptor().unwrap( value, Geometry.class, options );
byte[] bytes = Encoders.encode( jtsGeom );
st.setObject( index, bytes );
}
};
}
@Override
public <X> ValueExtractor<X> getExtractor(final JavaTypeDescriptor<X> javaTypeDescriptor) {
return (ValueExtractor<X>) new SqlServer2008GeometryValueExtractor( javaTypeDescriptor );
return new BasicExtractor<X>( javaTypeDescriptor, this ) {
@Override
protected X doExtract(ResultSet rs, String name, WrapperOptions options) throws SQLException {
return getJavaDescriptor().wrap( toJTS( rs.getObject( name ) ), options );
}
@Override
protected X doExtract(CallableStatement statement, int index, WrapperOptions options) throws SQLException {
return getJavaDescriptor().wrap( toJTS( statement.getObject( index ) ), options );
}
@Override
protected X doExtract(CallableStatement statement, String name, WrapperOptions options)
throws SQLException {
return getJavaDescriptor().wrap( toJTS( statement.getObject( name ) ), options );
}
};
}
private Geometry toJTS(Object obj) {
byte[] raw = null;
if ( obj == null ) {
return null;
}
if ( ( obj instanceof byte[] ) ) {
raw = (byte[]) obj;
}
else if ( obj instanceof Blob ) {
raw = toByteArray( (Blob) obj );
}
else {
throw new IllegalArgumentException( "Expected byte array or BLOB" );
}
return Decoders.decode( raw );
}
private byte[] toByteArray(Blob blob) {
try {
return blob.getBytes( 1, (int) blob.length() );
}
catch ( SQLException e ) {
throw new RuntimeException( "Error on transforming blob into array.", e );
}
}
}

View File

@ -1,50 +0,0 @@
/*
* This file is part of Hibernate Spatial, an extension to the
* hibernate ORM solution for spatial (geographic) data.
*
* Copyright © 2007-2012 Geovise BVBA
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.hibernate.spatial.dialect.sqlserver;
import java.sql.Connection;
import com.vividsolutions.jts.geom.Geometry;
import org.hibernate.spatial.dialect.AbstractGeometryValueBinder;
import org.hibernate.spatial.dialect.sqlserver.convertors.Encoders;
import org.hibernate.type.descriptor.java.JavaTypeDescriptor;
/**
* @author Karel Maesen, Geovise BVBA
* creation-date: 8/23/11
*/
public class SqlServer2008GeometryValueBinder<X> extends AbstractGeometryValueBinder {
public SqlServer2008GeometryValueBinder(JavaTypeDescriptor<X> javaDescriptor) {
super( javaDescriptor, SqlServer2008GeometryTypeDescriptor.INSTANCE );
}
public Object toNative(Geometry geom, Connection connection) {
if ( geom == null ) {
throw new IllegalArgumentException( "Null geometry passed." );
}
return Encoders.encode( geom );
}
}

View File

@ -1,69 +0,0 @@
/*
* This file is part of Hibernate Spatial, an extension to the
* hibernate ORM solution for spatial (geographic) data.
*
* Copyright © 2007-2012 Geovise BVBA
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.hibernate.spatial.dialect.sqlserver;
import java.sql.Blob;
import java.sql.SQLException;
import com.vividsolutions.jts.geom.Geometry;
import org.hibernate.spatial.dialect.AbstractGeometryValueExtractor;
import org.hibernate.spatial.dialect.sqlserver.convertors.Decoders;
import org.hibernate.type.descriptor.java.JavaTypeDescriptor;
/**
* @author Karel Maesen, Geovise BVBA
* creation-date: 8/23/11
*/
public class SqlServer2008GeometryValueExtractor<X> extends AbstractGeometryValueExtractor<X> {
public SqlServer2008GeometryValueExtractor(JavaTypeDescriptor<X> javaDescriptor) {
super( javaDescriptor, SqlServer2008GeometryTypeDescriptor.INSTANCE );
}
public Geometry toJTS(Object obj) {
byte[] raw = null;
if ( obj == null ) {
return null;
}
if ( ( obj instanceof byte[] ) ) {
raw = (byte[]) obj;
}
else if ( obj instanceof Blob ) {
raw = toByteArray( (Blob) obj );
}
else {
throw new IllegalArgumentException( "Expected byte array." );
}
return Decoders.decode( raw );
}
private byte[] toByteArray(Blob blob) {
try {
return blob.getBytes( 1, (int) blob.length() );
}
catch ( SQLException e ) {
throw new RuntimeException( "Error on transforming blob into array.", e );
}
}
}