diff --git a/plugin/src/main/groovy/gpc/pgext/hibernate/criterion/array/PgArrayExpression.groovy b/plugin/src/main/groovy/gpc/pgext/hibernate/criterion/array/PgArrayExpression.groovy index f2a1832..d9c38df 100644 --- a/plugin/src/main/groovy/gpc/pgext/hibernate/criterion/array/PgArrayExpression.groovy +++ b/plugin/src/main/groovy/gpc/pgext/hibernate/criterion/array/PgArrayExpression.groovy @@ -21,6 +21,17 @@ class PgArrayExpression implements Criterion { private final Object value private final String op + private static final PgArrayUtils.MapFunction MAP_TO_ENUM = new PgArrayUtils.MapFunction() { + @Override + Object map(Object o) { + try { + return ((Enum) o).ordinal() + } catch (ClassCastException e) { + throw new HibernateException("Unable to cast object $o to Enum", e) + } + } + } + PgArrayExpression(String propertyName, Object value, String op) { this.propertyName = propertyName this.value = value @@ -29,35 +40,25 @@ class PgArrayExpression implements Criterion { @Override String toSqlString(Criteria criteria, CriteriaQuery criteriaQuery) throws HibernateException { - def arrayType = checkAndGetArrayType(criteria, criteriaQuery) - def postgresArrayType = PgArrayUtils.getNativeSqlType(arrayType.getTypeClass()) + '[]' + def arrayType = checkAndGetArrayType(criteria, criteriaQuery, propertyName) + def postgresArrayType = PgArrayUtils.getNativeSqlType(arrayType.typeClass) + '[]' criteriaQuery.findColumns(propertyName, criteria) - .collect {"$it $op CAST(? as $postgresArrayType)" } + .collect { "$it $op CAST(? as $postgresArrayType)" } .join(' and ') } @Override TypedValue[] getTypedValues(Criteria criteria, CriteriaQuery criteriaQuery) throws HibernateException { - def arrayType = checkAndGetArrayType(criteria, criteriaQuery) + def arrayType = checkAndGetArrayType(criteria, criteriaQuery, propertyName) def arrValue = arrayType.typeClass.isEnum() ? - PgArrayUtils.getValueAsArrayOfType(value, Integer, mapValueToEnumOrdinal()) : + PgArrayUtils.getValueAsArrayOfType(value, Integer, MAP_TO_ENUM) : PgArrayUtils.getValueAsArrayOfType(value, arrayType.typeClass) criteriaQuery.getTypedValue(criteria, propertyName, arrValue) as TypedValue[] } - private PgArrayUtils.MapFunction mapValueToEnumOrdinal() { - return { Object o -> - try { - return (o as Enum).ordinal() - } catch (ClassCastException e) { - throw new HibernateException("Unable to cast object $o to Enum", e) - } - } as PgArrayUtils.MapFunction - } - - private ArrayType checkAndGetArrayType(Criteria criteria, CriteriaQuery criteriaQuery) { + private static ArrayType checkAndGetArrayType(Criteria criteria, CriteriaQuery criteriaQuery, String propertyName) { def propertyType = criteriaQuery.getType(criteria, propertyName) - if (!(propertyType instanceof CustomType) || !((propertyType as CustomType).userType instanceof ArrayType)) { + if (!(propertyType instanceof CustomType) || !(((CustomType) propertyType).userType instanceof ArrayType)) { throw new HibernateException("Property is not an instance of the postgres type ArrayType. Type is: $propertyType.class") } (propertyType as CustomType).userType as ArrayType diff --git a/plugin/src/main/groovy/gpc/pgext/hibernate/criterion/hstore/PgHstoreOperatorExpression.groovy b/plugin/src/main/groovy/gpc/pgext/hibernate/criterion/hstore/PgHstoreOperatorExpression.groovy index 19e4fef..6680989 100644 --- a/plugin/src/main/groovy/gpc/pgext/hibernate/criterion/hstore/PgHstoreOperatorExpression.groovy +++ b/plugin/src/main/groovy/gpc/pgext/hibernate/criterion/hstore/PgHstoreOperatorExpression.groovy @@ -21,12 +21,8 @@ class PgHstoreOperatorExpression implements Criterion { private static final TypedValue[] NO_VALUES = new TypedValue[0] PgHstoreOperatorExpression(String propertyName, Object value, String operator) { - this(propertyName, value as Map, operator) - } - - PgHstoreOperatorExpression(String propertyName, Map value, String operator) { this.propertyName = propertyName - this.value = value + this.value = (Map) value this.operator = operator } diff --git a/plugin/src/main/groovy/gpc/pgext/hibernate/postgresql/criteria/ArrayCriterias.groovy b/plugin/src/main/groovy/gpc/pgext/hibernate/postgresql/criteria/ArrayCriterias.groovy index f3fa5b9..cab806b 100644 --- a/plugin/src/main/groovy/gpc/pgext/hibernate/postgresql/criteria/ArrayCriterias.groovy +++ b/plugin/src/main/groovy/gpc/pgext/hibernate/postgresql/criteria/ArrayCriterias.groovy @@ -143,7 +143,7 @@ class ArrayCriterias { self, new PgArrayILikeFunction( calculatePropertyName(self, propertyName), - calculatePropertyValue(self, propertyValue) as String + (String) calculatePropertyValue(self, propertyValue) ) ) } diff --git a/plugin/src/main/groovy/gpc/pgext/hibernate/postgresql/criteria/JsonCriterias.groovy b/plugin/src/main/groovy/gpc/pgext/hibernate/postgresql/criteria/JsonCriterias.groovy index ef79003..46308a9 100644 --- a/plugin/src/main/groovy/gpc/pgext/hibernate/postgresql/criteria/JsonCriterias.groovy +++ b/plugin/src/main/groovy/gpc/pgext/hibernate/postgresql/criteria/JsonCriterias.groovy @@ -39,7 +39,7 @@ class JsonCriterias { '->>', jsonAttribute, '=', - calculatePropertyValue(self, propertyValue) as String + calculatePropertyValue(self, propertyValue) ) ) } @@ -87,7 +87,7 @@ class JsonCriterias { jsonOp, jsonAttribute, sqlOp, - calculatePropertyValue(self, propertyValue) as String + calculatePropertyValue(self, propertyValue) ) ) } diff --git a/plugin/src/main/groovy/gpc/pgext/hibernate/usertype/ArrayType.java b/plugin/src/main/groovy/gpc/pgext/hibernate/usertype/ArrayType.java index 7d0c7e8..7472ca1 100644 --- a/plugin/src/main/groovy/gpc/pgext/hibernate/usertype/ArrayType.java +++ b/plugin/src/main/groovy/gpc/pgext/hibernate/usertype/ArrayType.java @@ -137,16 +137,13 @@ public Class returnedClass() { @Override public int[] sqlTypes() { - Integer type = CLASS_TO_SQL_CODE.get(typeClass); if (type != null) { return new int[]{type}; } - if (typeClass.isEnum()) { return new int[]{ENUM_INTEGER_ARRAY}; } - throw new RuntimeException("The type " + typeClass + " is not a valid type"); } diff --git a/plugin/src/main/groovy/gpc/pgext/hibernate/utils/CriteriaUtils.groovy b/plugin/src/main/groovy/gpc/pgext/hibernate/utils/CriteriaUtils.groovy index 5afd6a7..70eca21 100644 --- a/plugin/src/main/groovy/gpc/pgext/hibernate/utils/CriteriaUtils.groovy +++ b/plugin/src/main/groovy/gpc/pgext/hibernate/utils/CriteriaUtils.groovy @@ -55,17 +55,17 @@ class CriteriaUtils { } static Criterion addToCriteria(HibernateCriteriaBuilder target, Criterion criterion) { - makeMethodAccessible(AbstractHibernateCriteriaBuilder, 'addToCriteria', Criterion).invoke( + (Criterion) makeMethodAccessible(AbstractHibernateCriteriaBuilder, 'addToCriteria', Criterion).invoke( target, criterion - ) as Criterion + ) } static String calculatePropertyName(HibernateCriteriaBuilder target, String propertyName) { - makeMethodAccessible(AbstractHibernateCriteriaBuilder, 'calculatePropertyName', String).invoke( + (String) makeMethodAccessible(AbstractHibernateCriteriaBuilder, 'calculatePropertyName', String).invoke( target, propertyName - ) as String + ) } static Object calculatePropertyValue(HibernateCriteriaBuilder target, Object propertyValue) { diff --git a/plugin/src/main/groovy/gpc/pgext/hibernate/utils/PgArrayUtils.groovy b/plugin/src/main/groovy/gpc/pgext/hibernate/utils/PgArrayUtils.groovy index ccd9f5c..930c6a5 100644 --- a/plugin/src/main/groovy/gpc/pgext/hibernate/utils/PgArrayUtils.groovy +++ b/plugin/src/main/groovy/gpc/pgext/hibernate/utils/PgArrayUtils.groovy @@ -31,18 +31,22 @@ class PgArrayUtils { * @return an array wrapping the parameter value */ static Object[] getValueAsArrayOfType(Object targetValue, Class expectedType, MapFunction mapFunction) { - if (targetValue instanceof Object[]) return (Object[]) targetValue + if (targetValue instanceof Object[]) { + return (Object[]) targetValue + } def items = (targetValue instanceof Collection) ? (targetValue as List) : [targetValue] def converted = items.collect { o -> - if (expectedType.isInstance(o)) return o - if (mapFunction) return mapFunction.map(o) + if (expectedType.isInstance(o)) { + return o + } + if (mapFunction) { + return mapFunction.map(o) + } throw new HibernateException("criteria doesn't support values of type: ${o?.class?.name}. Try: $expectedType or List<$expectedType> instead") } - def arr = Array.newInstance(expectedType, converted.size()) - converted.eachWithIndex { v, i -> Array.set(arr, i, expectedType.cast(v)) } - (Object[]) arr + converted.toArray((Object[]) Array.newInstance(expectedType, converted.size())) } /** diff --git a/test-apps/app1/src/integration-test/groovy/test/hstore/PostgresqlHstoreMapDomainIntegrationSpec.groovy b/test-apps/app1/src/integration-test/groovy/test/hstore/PostgresqlHstoreMapDomainIntegrationSpec.groovy index 7702efb..c350214 100644 --- a/test-apps/app1/src/integration-test/groovy/test/hstore/PostgresqlHstoreMapDomainIntegrationSpec.groovy +++ b/test-apps/app1/src/integration-test/groovy/test/hstore/PostgresqlHstoreMapDomainIntegrationSpec.groovy @@ -1,7 +1,7 @@ package test.hstore import app.hstore.TestHstoreMap -import spock.lang.Ignore +import spock.lang.PendingFeature import spock.lang.Specification import spock.lang.Unroll @@ -100,7 +100,6 @@ class PostgresqlHstoreMapDomainIntegrationSpec extends Specification { ['foo,bar': 'baz,qux'] | 'foo,bar' | 'baz,qux' } - @Unroll void 'save a domain class with a empty map and validate that is not dirty right after retrieval'() { setup: def testHstoreMap = new TestHstoreMap(testAttributes: [:]) @@ -118,9 +117,7 @@ class PostgresqlHstoreMapDomainIntegrationSpec extends Specification { !retrievedTestHstoreMap.isDirty() } - // TODO seems dirty check doesn't work for Grails 3.3 with Hibernate 5.2 and GORM 6.1.9 - @Ignore - @Unroll + @PendingFeature(reason = 'seems dirty check does not work for Grails 3.3 with Hibernate 5.2 and GORM 6.1.9') void 'save a domain class, modify it and validate that it is dirty'() { setup: def testHstoreMap = new TestHstoreMap(testAttributes: [:])