GeometryType.java

  1. package org.dbunit.ext.postgresql;

  2. import java.lang.reflect.Constructor;
  3. import java.lang.reflect.InvocationTargetException;
  4. import java.sql.Connection;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.sql.Types;

  9. import org.dbunit.dataset.datatype.AbstractDataType;
  10. import org.dbunit.dataset.datatype.TypeCastException;

  11. public class GeometryType extends AbstractDataType {
  12.     public GeometryType() {
  13.         super("geometry", Types.OTHER, String.class, false);
  14.     }

  15.     public Object getSqlValue(int column, ResultSet resultSet)
  16.             throws SQLException, TypeCastException {
  17.         return resultSet.getString(column);
  18.     }

  19.     public void setSqlValue(Object geom, int column, PreparedStatement statement)
  20.             throws SQLException, TypeCastException {
  21.         statement.setObject(column,
  22.                 getGeometry(geom, statement.getConnection()));
  23.     }

  24.     public Object typeCast(Object arg0) throws TypeCastException {
  25.         return arg0.toString();
  26.     }

  27.     private Object getGeometry(Object value, Connection connection)
  28.             throws TypeCastException {
  29.         Object tempgeom = null;

  30.         try {
  31.             Class aPGIntervalClass = super.loadClass("net.postgis.jdbc.PGgeometry",
  32.                     connection);
  33.             Constructor ct = aPGIntervalClass
  34.                     .getConstructor(new Class[] { String.class });

  35.             tempgeom = ct.newInstance(new Object[] { value });
  36.         } catch (ClassNotFoundException e) {
  37.             throw new TypeCastException(value, this, e);
  38.         } catch (InvocationTargetException e) {
  39.             throw new TypeCastException(value, this, e);
  40.         } catch (NoSuchMethodException e) {
  41.             throw new TypeCastException(value, this, e);
  42.         } catch (IllegalAccessException e) {
  43.             throw new TypeCastException(value, this, e);
  44.         } catch (InstantiationException e) {
  45.             throw new TypeCastException(value, this, e);
  46.         }

  47.         return tempgeom;
  48.     }
  49. }