Adding support for PostGIS databases

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Adding support for PostGIS databases

Sebastien Arbogast
I'm trying to implement a custom DataTypeFactory for PostGIS geometry
datatype. The problem is that the same sql datatype (geometry) can be
mapped onto several org.postgis.Geometry implementations.

So when I try to use my custom datatype, I get the following exception:

org.postgresql.util.PSQLException: Impossible de déduire le type SQL à
utiliser pour une instance de org.postgis.Point. Utilisez setObject()
avec une valeur de type explicite pour spécifier le type à utiliser.
        at org.postgresql.jdbc2.AbstractJdbc2Statement.setObject(AbstractJdbc2Statement.java:1735)
        at org.epseelon.tagspot.business.integration.GeometryDataType.setSqlValue(GeometryDataType.java:60)
...

I guess I'm doing something wrong in my datatype implementation:

import org.dbunit.dataset.ITable;
import org.dbunit.dataset.datatype.AbstractDataType;
import org.dbunit.dataset.datatype.TypeCastException;
import org.postgis.Geometry;
import org.postgis.Point;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;

/**
 * @author sarbogast
 * @version 23 déc. 2007, 15:22:03
 */
public class GeometryDataType extends AbstractDataType {

    public GeometryDataType() {
        super("GEOMETRY", Types.OTHER, Geometry.class, false);
    }

    public Object typeCast(Object value) throws TypeCastException {
        if (value == null || value == ITable.NO_VALUE) {
            return null;
        }

        if (value instanceof String) {
            String geometryString = (String) value;

            try {
                if (geometryString.startsWith("POINT")) {
                    return new Point(geometryString);
                }
            } catch (SQLException e) {
                throw new TypeCastException(value, this);
            }
        }

        throw new TypeCastException(value, this);
    }

    public Object getSqlValue(int column, ResultSet resultSet)
            throws SQLException, TypeCastException {
        Geometry value = (Geometry) resultSet.getObject(column);
        if (resultSet.wasNull()) {
            return null;
        }
        return value.toString();
    }

    public void setSqlValue(Object value, int column,
PreparedStatement statement)
            throws SQLException, TypeCastException {
        Geometry geometry = (Geometry) typeCast(value);
        statement.setObject(column, geometry);
    }
}

Can anybody help me with that?

--
Sébastien Arbogast

http://www.sebastien-arbogast.com

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2005.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
dbunit-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/dbunit-user