Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Object, String>, operator)
}

PgHstoreOperatorExpression(String propertyName, Map<Object, String> value, String operator) {
this.propertyName = propertyName
this.value = value
this.value = (Map<Object, String>) value
this.operator = operator
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ class ArrayCriterias {
self,
new PgArrayILikeFunction(
calculatePropertyName(self, propertyName),
calculatePropertyValue(self, propertyValue) as String
(String) calculatePropertyValue(self, propertyValue)
)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class JsonCriterias {
'->>',
jsonAttribute,
'=',
calculatePropertyValue(self, propertyValue) as String
calculatePropertyValue(self, propertyValue)
)
)
}
Expand Down Expand Up @@ -87,7 +87,7 @@ class JsonCriterias {
jsonOp,
jsonAttribute,
sqlOp,
calculatePropertyValue(self, propertyValue) as String
calculatePropertyValue(self, propertyValue)
)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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: [:])
Expand All @@ -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: [:])
Expand Down
Loading