package org.skyve.impl.persistence.hibernate.dialect;

import org.geolatte.geom.jts.JTS;
import org.hibernate.mapping.Column;
import org.hibernate.spatial.JTSGeometryType;
import org.hibernate.spatial.dialect.h2geodb.GeoDBDialect;
import org.hibernate.spatial.dialect.h2geodb.GeoDBGeometryTypeDescriptor;
import org.hibernate.spatial.dialect.h2geodb.GeoDbWkb;
import org.hibernate.tool.schema.extract.spi.ColumnInformation;

import com.vividsolutions.jts.geom.Geometry;

public class H2SpatialDialect extends GeoDBDialect implements SkyveDialect {
	private static final long serialVersionUID = 2491505869930720627L;

	private JTSGeometryType geometryType = new JTSGeometryType(GeoDBGeometryTypeDescriptor.INSTANCE);

	@Override
	public int getGeometrySqlType() {
		return GeoDBGeometryTypeDescriptor.INSTANCE.getSqlType();
	}

	@Override
	public final JTSGeometryType getGeometryType() {
		return geometryType;
	}

	// From GeoDBGeometryTypeDescriptor
	@Override
	public Object convertToPersistedValue(Geometry geometry) {
		return GeoDbWkb.to(JTS.from(geometry));
	}
	
	// From GeoDBGeometryTypeDescriptor
	@Override
	public Geometry convertFromPersistedValue(Object geometry) {
		return JTS.to((org.geolatte.geom.Geometry<?>) GeoDbWkb.from(geometry));
	}

	@Override
	public boolean isAlterTableColumnChangeRequired(Column column, ColumnInformation columnInfo) {
		return DDLDelegate.isAlterTableColumnChangeRequired(column, columnInfo);
	}
	
	@Override
	public String getModifyColumnString() {
		return "alter column";
	}
}

@@ -48,16 +48,40 @@
 alect;%0A%0A
+import java.sql.Types;%0A%0A
 import o
@@ -1384,22 +1384,32 @@
 fo) %7B%0A%09%09
-return
+boolean result =
  DDLDele
@@ -1467,16 +1467,943 @@
 nInfo);%0A
+%09%09%0A%09%09// Do additional check for varchar(Integer.MAX_VALUE) false positive.%0A%09%09if (result) %7B%0A/*%0A%09%09%09System.out.println(%22%22 + column.getSqlType() + %22 : %22 + %0A%09%09%09%09%09%09%09%09column.getSqlTypeCode() + %22 : %22 + %0A%09%09%09%09%09%09%09%09column.getLength() + %22 : %22 + %0A%09%09%09%09%09%09%09%09column.getPrecision() + %22 : %22 +%0A%09%09%09%09%09%09%09%09column.getScale() + %22 : %22 + %0A%09%09%09%09%09%09%09%09column.getTypeIndex() + %22 = %22 +%0A%09%09%09%09%09%09%09%09columnInfo.getColumnSize() + %22 : %22 +%0A%09%09%09%09%09%09%09%09columnInfo.getDecimalDigits() + %22 : %22 + %0A%09%09%09%09%09%09%09%09columnInfo.getTypeCode() + %22 : %22 + %0A%09%09%09%09%09%09%09%09columnInfo.getTypeName() + %22 : %22 + %0A%09%09%09%09%09%09%09%09columnInfo.getColumnIdentifier());%0A*/%0A%09%09%09if ((column.getLength() == 255) && %0A%09%09%09%09%09(column.getPrecision() == 19) && %0A%09%09%09%09%09(column.getScale() == 2) &&%0A%09%09%09%09%09(column.getTypeIndex() == 0) &&%0A%09%09%09%09%09(columnInfo.getColumnSize() == Integer.MAX_VALUE) &&%0A%09%09%09%09%09(columnInfo.getDecimalDigits() == 0) &&%0A%09%09%09%09%09(columnInfo.getTypeCode() == Types.VARCHAR)) %7B%0A%09%09%09%09result = false;%0A%09%09%09%7D%0A%09%09%7D%0A%09%09return result;%0A
 %09%7D%0A%09%0A%09@O

Add check for varchar(Integer.MAX_VALUE) false positive when updating a H2 database schema.