Skip to content

Commit 95919cd

Browse files
authored
Merge pull request #133 from gpc/post-grails-7-cleanup
Post Grails 7 cleanup
2 parents 9527d51 + 72fe9d5 commit 95919cd

File tree

8 files changed

+38
-43
lines changed

8 files changed

+38
-43
lines changed

plugin/src/main/groovy/gpc/pgext/hibernate/criterion/array/PgArrayExpression.groovy

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,17 @@ class PgArrayExpression implements Criterion {
2121
private final Object value
2222
private final String op
2323

24+
private static final PgArrayUtils.MapFunction MAP_TO_ENUM = new PgArrayUtils.MapFunction() {
25+
@Override
26+
Object map(Object o) {
27+
try {
28+
return ((Enum) o).ordinal()
29+
} catch (ClassCastException e) {
30+
throw new HibernateException("Unable to cast object $o to Enum", e)
31+
}
32+
}
33+
}
34+
2435
PgArrayExpression(String propertyName, Object value, String op) {
2536
this.propertyName = propertyName
2637
this.value = value
@@ -29,35 +40,25 @@ class PgArrayExpression implements Criterion {
2940

3041
@Override
3142
String toSqlString(Criteria criteria, CriteriaQuery criteriaQuery) throws HibernateException {
32-
def arrayType = checkAndGetArrayType(criteria, criteriaQuery)
33-
def postgresArrayType = PgArrayUtils.getNativeSqlType(arrayType.getTypeClass()) + '[]'
43+
def arrayType = checkAndGetArrayType(criteria, criteriaQuery, propertyName)
44+
def postgresArrayType = PgArrayUtils.getNativeSqlType(arrayType.typeClass) + '[]'
3445
criteriaQuery.findColumns(propertyName, criteria)
35-
.collect {"$it $op CAST(? as $postgresArrayType)" }
46+
.collect { "$it $op CAST(? as $postgresArrayType)" }
3647
.join(' and ')
3748
}
3849

3950
@Override
4051
TypedValue[] getTypedValues(Criteria criteria, CriteriaQuery criteriaQuery) throws HibernateException {
41-
def arrayType = checkAndGetArrayType(criteria, criteriaQuery)
52+
def arrayType = checkAndGetArrayType(criteria, criteriaQuery, propertyName)
4253
def arrValue = arrayType.typeClass.isEnum() ?
43-
PgArrayUtils.getValueAsArrayOfType(value, Integer, mapValueToEnumOrdinal()) :
54+
PgArrayUtils.getValueAsArrayOfType(value, Integer, MAP_TO_ENUM) :
4455
PgArrayUtils.getValueAsArrayOfType(value, arrayType.typeClass)
4556
criteriaQuery.getTypedValue(criteria, propertyName, arrValue) as TypedValue[]
4657
}
4758

48-
private PgArrayUtils.MapFunction mapValueToEnumOrdinal() {
49-
return { Object o ->
50-
try {
51-
return (o as Enum).ordinal()
52-
} catch (ClassCastException e) {
53-
throw new HibernateException("Unable to cast object $o to Enum", e)
54-
}
55-
} as PgArrayUtils.MapFunction
56-
}
57-
58-
private ArrayType checkAndGetArrayType(Criteria criteria, CriteriaQuery criteriaQuery) {
59+
private static ArrayType checkAndGetArrayType(Criteria criteria, CriteriaQuery criteriaQuery, String propertyName) {
5960
def propertyType = criteriaQuery.getType(criteria, propertyName)
60-
if (!(propertyType instanceof CustomType) || !((propertyType as CustomType).userType instanceof ArrayType)) {
61+
if (!(propertyType instanceof CustomType) || !(((CustomType) propertyType).userType instanceof ArrayType)) {
6162
throw new HibernateException("Property is not an instance of the postgres type ArrayType. Type is: $propertyType.class")
6263
}
6364
(propertyType as CustomType).userType as ArrayType

plugin/src/main/groovy/gpc/pgext/hibernate/criterion/hstore/PgHstoreOperatorExpression.groovy

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,8 @@ class PgHstoreOperatorExpression implements Criterion {
2121
private static final TypedValue[] NO_VALUES = new TypedValue[0]
2222

2323
PgHstoreOperatorExpression(String propertyName, Object value, String operator) {
24-
this(propertyName, value as Map<Object, String>, operator)
25-
}
26-
27-
PgHstoreOperatorExpression(String propertyName, Map<Object, String> value, String operator) {
2824
this.propertyName = propertyName
29-
this.value = value
25+
this.value = (Map<Object, String>) value
3026
this.operator = operator
3127
}
3228

plugin/src/main/groovy/gpc/pgext/hibernate/postgresql/criteria/ArrayCriterias.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ class ArrayCriterias {
143143
self,
144144
new PgArrayILikeFunction(
145145
calculatePropertyName(self, propertyName),
146-
calculatePropertyValue(self, propertyValue) as String
146+
(String) calculatePropertyValue(self, propertyValue)
147147
)
148148
)
149149
}

plugin/src/main/groovy/gpc/pgext/hibernate/postgresql/criteria/JsonCriterias.groovy

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class JsonCriterias {
3939
'->>',
4040
jsonAttribute,
4141
'=',
42-
calculatePropertyValue(self, propertyValue) as String
42+
calculatePropertyValue(self, propertyValue)
4343
)
4444
)
4545
}
@@ -87,7 +87,7 @@ class JsonCriterias {
8787
jsonOp,
8888
jsonAttribute,
8989
sqlOp,
90-
calculatePropertyValue(self, propertyValue) as String
90+
calculatePropertyValue(self, propertyValue)
9191
)
9292
)
9393
}

plugin/src/main/groovy/gpc/pgext/hibernate/usertype/ArrayType.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,16 +137,13 @@ public Class<?> returnedClass() {
137137

138138
@Override
139139
public int[] sqlTypes() {
140-
141140
Integer type = CLASS_TO_SQL_CODE.get(typeClass);
142141
if (type != null) {
143142
return new int[]{type};
144143
}
145-
146144
if (typeClass.isEnum()) {
147145
return new int[]{ENUM_INTEGER_ARRAY};
148146
}
149-
150147
throw new RuntimeException("The type " + typeClass + " is not a valid type");
151148
}
152149

plugin/src/main/groovy/gpc/pgext/hibernate/utils/CriteriaUtils.groovy

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,17 @@ class CriteriaUtils {
5555
}
5656

5757
static Criterion addToCriteria(HibernateCriteriaBuilder target, Criterion criterion) {
58-
makeMethodAccessible(AbstractHibernateCriteriaBuilder, 'addToCriteria', Criterion).invoke(
58+
(Criterion) makeMethodAccessible(AbstractHibernateCriteriaBuilder, 'addToCriteria', Criterion).invoke(
5959
target,
6060
criterion
61-
) as Criterion
61+
)
6262
}
6363

6464
static String calculatePropertyName(HibernateCriteriaBuilder target, String propertyName) {
65-
makeMethodAccessible(AbstractHibernateCriteriaBuilder, 'calculatePropertyName', String).invoke(
65+
(String) makeMethodAccessible(AbstractHibernateCriteriaBuilder, 'calculatePropertyName', String).invoke(
6666
target,
6767
propertyName
68-
) as String
68+
)
6969
}
7070

7171
static Object calculatePropertyValue(HibernateCriteriaBuilder target, Object propertyValue) {

plugin/src/main/groovy/gpc/pgext/hibernate/utils/PgArrayUtils.groovy

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,22 @@ class PgArrayUtils {
3131
* @return an array wrapping the parameter value
3232
*/
3333
static Object[] getValueAsArrayOfType(Object targetValue, Class expectedType, MapFunction mapFunction) {
34-
if (targetValue instanceof Object[]) return (Object[]) targetValue
34+
if (targetValue instanceof Object[]) {
35+
return (Object[]) targetValue
36+
}
3537

3638
def items = (targetValue instanceof Collection) ? (targetValue as List) : [targetValue]
3739
def converted = items.collect { o ->
38-
if (expectedType.isInstance(o)) return o
39-
if (mapFunction) return mapFunction.map(o)
40+
if (expectedType.isInstance(o)) {
41+
return o
42+
}
43+
if (mapFunction) {
44+
return mapFunction.map(o)
45+
}
4046
throw new HibernateException("criteria doesn't support values of type: ${o?.class?.name}. Try: $expectedType or List<$expectedType> instead")
4147
}
4248

43-
def arr = Array.newInstance(expectedType, converted.size())
44-
converted.eachWithIndex { v, i -> Array.set(arr, i, expectedType.cast(v)) }
45-
(Object[]) arr
49+
converted.toArray((Object[]) Array.newInstance(expectedType, converted.size()))
4650
}
4751

4852
/**

test-apps/app1/src/integration-test/groovy/test/hstore/PostgresqlHstoreMapDomainIntegrationSpec.groovy

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package test.hstore
22

33
import app.hstore.TestHstoreMap
4-
import spock.lang.Ignore
4+
import spock.lang.PendingFeature
55
import spock.lang.Specification
66
import spock.lang.Unroll
77

@@ -100,7 +100,6 @@ class PostgresqlHstoreMapDomainIntegrationSpec extends Specification {
100100
['foo,bar': 'baz,qux'] | 'foo,bar' | 'baz,qux'
101101
}
102102

103-
@Unroll
104103
void 'save a domain class with a empty map and validate that is not dirty right after retrieval'() {
105104
setup:
106105
def testHstoreMap = new TestHstoreMap(testAttributes: [:])
@@ -118,9 +117,7 @@ class PostgresqlHstoreMapDomainIntegrationSpec extends Specification {
118117
!retrievedTestHstoreMap.isDirty()
119118
}
120119

121-
// TODO seems dirty check doesn't work for Grails 3.3 with Hibernate 5.2 and GORM 6.1.9
122-
@Ignore
123-
@Unroll
120+
@PendingFeature(reason = 'seems dirty check does not work for Grails 3.3 with Hibernate 5.2 and GORM 6.1.9')
124121
void 'save a domain class, modify it and validate that it is dirty'() {
125122
setup:
126123
def testHstoreMap = new TestHstoreMap(testAttributes: [:])

0 commit comments

Comments
 (0)