1 package org.dbunit.ext.postgresql;
2
3 import java.lang.reflect.Constructor;
4 import java.lang.reflect.InvocationTargetException;
5 import java.sql.Connection;
6 import java.sql.PreparedStatement;
7 import java.sql.ResultSet;
8 import java.sql.SQLException;
9 import java.sql.Types;
10
11 import org.dbunit.dataset.datatype.AbstractDataType;
12 import org.dbunit.dataset.datatype.TypeCastException;
13
14 public class GeometryType extends AbstractDataType {
15 public GeometryType() {
16 super("geometry", Types.OTHER, String.class, false);
17 }
18
19 public Object getSqlValue(int column, ResultSet resultSet)
20 throws SQLException, TypeCastException {
21 return resultSet.getString(column);
22 }
23
24 public void setSqlValue(Object geom, int column, PreparedStatement statement)
25 throws SQLException, TypeCastException {
26 statement.setObject(column,
27 getGeometry(geom, statement.getConnection()));
28 }
29
30 public Object typeCast(Object arg0) throws TypeCastException {
31 return arg0.toString();
32 }
33
34 private Object getGeometry(Object value, Connection connection)
35 throws TypeCastException {
36 Object tempgeom = null;
37
38 try {
39 Class aPGIntervalClass = super.loadClass("net.postgis.jdbc.PGgeometry",
40 connection);
41 Constructor ct = aPGIntervalClass
42 .getConstructor(new Class[] { String.class });
43
44 tempgeom = ct.newInstance(new Object[] { value });
45 } catch (ClassNotFoundException e) {
46 throw new TypeCastException(value, this, e);
47 } catch (InvocationTargetException e) {
48 throw new TypeCastException(value, this, e);
49 } catch (NoSuchMethodException e) {
50 throw new TypeCastException(value, this, e);
51 } catch (IllegalAccessException e) {
52 throw new TypeCastException(value, this, e);
53 } catch (InstantiationException e) {
54 throw new TypeCastException(value, this, e);
55 }
56
57 return tempgeom;
58 }
59 }