How to make Dbunit recognize the POLYGON data type of Postgresql?

Asked

Viewed 236 times

10

I have the following dominance of the type polygon in the database:

CREATE DOMAIN "global".polygon AS pg_catalog.polygon;

The following table using this domain:

CREATE TABLE user.area_geom
(
  id_area_geom serial NOT NULL,
  polygon global.polygon
);

And the following class extending DBTestCase setting up the dataset before the tests are run:

public abstract class MyDBTestCase extends DBTestCase {

public MyDBTestCase () {
    // Configura url, user, password e driver do banco de dados.
}

@Override
protected IDataSet getDataSet() throws Exception {
    // Meu arquivo .xml com os dados de teste.
    return new FlatXmlDataSetBuilder()
            .build(new FileInputStream(MyTestDB.FLAT_XML_DATASET));
}

@Override
protected void setUpDatabaseConfig(DatabaseConfig config) {

    /* Habilita case sensitive para os nomes de tabelas do banco */
    config.setProperty(DatabaseConfig.FEATURE_CASE_SENSITIVE_TABLE_NAMES,
            true);

    /* Habilita a utilização de fully qualified names para tabelas */
    config.setProperty(DatabaseConfig.FEATURE_QUALIFIED_TABLE_NAMES, true);

    /* Configura o tipo de dado para o PostgreSQL */
    config.setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY,
            new PostgresqlDataTypeFactory());
}
}

This setup works perfectly for all commonly used data types, but when I checked the table area_geom containing a domain of the type polygon, the console started generating the following output:

9081 [main] WARN org.dbunit.util.SQLHelper - area_geom.polygon data type (1111, 'polygon') not recognized and will be ignored. See FAQ for more information.

In the FAQ proposed by the error message, there is nothing that indicates me how to implement a "custom" data type on top of an existing data type, nor do Dbunit recognize my polygon, I’ve already spent a few hours of Google and found nothing.

Someone has some light for this problem?

  • Have you tried using the Postgis?

  • Peter, I already use Postgis at the database level, but no jar on my project. The implementation for the recognition of Postgis data types can also be done according to the Aelia response.

1 answer

4


The interesting item in the FAQ is this: How to replace the default data type Factory?, which explains how to set up a custom data type Factory Dbunit pro -- basically, a class that implements Idatatypefactory.

You will have to add one Datatype for Polygon (can be inspired by Intervaltype), getting something like this:

public class PolygonType extends AbstractDataType {
      public PolygonType() {
          super("polygon", Types.OTHER, String.class, false);
      }
      /* ... implemente aqui getSqlValue(..) e setSqlValue(..) para o tipo POLYGON */
}

Then create a class by extending the Postgresqldatatypefactory (code here) implementing the special logic for the type POLYGON and delegate to the other guys, something like:

public class PolygonPostgresDataFactory extends PostgresqlDataTypeFactory {
    public DataType createDataType(int sqlType, String sqlTypeName)
                                         throws DataTypeException {
        logger.debug("createDataType(sqlType={}, sqlTypeName={})",
                       String.valueOf(sqlType), sqlTypeName);
        if (sqlType == Types.OTHER && "polygon".equals(sqlTypeName))
             return new PolygonType();
        }      
        return super.createDataType(sqlType, sqlTypeName);
    }
}

Finally, you change your method there MyDBTestCase::setUpDatabaseConfig to use custom Datatypefactory:

/* Configura o tipo de dado para o PostgreSQL */
config.setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY,
                           new PostgresqlDataTypeFactory());
  • Perfect Elias, I implemented as proposed and the type POLYGON has been recognized. I really appreciate your help!

  • 1

    Ball show, Bruno! Thanks for the edition. =)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.