Skip to content
Browse files

ok now with 3 lines 40 to 42 we can do an ArrayList criteria

  • Loading branch information...
1 parent a48d30c commit f68b7943ce4c2743c9b698d41f9758d4c931eeb4 @fabiooshiro committed Feb 20, 2013
View
2 PlasticCriteriaGrailsPlugin.groovy
@@ -1,6 +1,6 @@
class PlasticCriteriaGrailsPlugin {
// the plugin version
- def version = "0.5"
+ def version = "0.6"
// the version or versions of Grails the plugin is designed for
def grailsVersion = "1.3 > *"
// the other plugins this plugin depends on
View
50 src/groovy/plastic/criteria/CriteriaDocTests.groovy
@@ -1,5 +1,6 @@
package plastic.criteria;
+import static org.junit.Assert.*;
import org.hibernate.FetchMode
public class CriteriaDocTests {
@@ -423,6 +424,7 @@ public class CriteriaDocTests {
assert [[monet, 1.1]] == rs
}
+ // next release 0.6
void test_fetch_mode(){
def monet = new Artist(name: 'Monet').save()
new Portrait(artist: monet, name: 'Soleil levant 1').save()
@@ -432,4 +434,52 @@ public class CriteriaDocTests {
}
assert 1 == rs.size()
}
+
+ void test_unique_result(){
+ def monet = new Artist(name: 'Monet').save()
+ def portrait = new Portrait(artist: monet, name: 'Soleil levant 1').save()
+ def result = Portrait.withCriteria {
+ eq('artist', monet)
+ uniqueResult = true
+ }
+ assert result == portrait
+ }
+
+ void test_unique_result_exception(){
+ def monet = new Artist(name: 'Monet').save()
+ new Portrait(artist: monet, name: 'Soleil levant 1').save()
+ new Portrait(artist: monet, name: 'Soleil levant 1').save()
+ try{
+ Portrait.withCriteria {
+ eq('artist', monet)
+ uniqueResult = true
+ }
+ fail("should throw an exception")
+ }catch(org.hibernate.NonUniqueResultException e){
+ // ok
+ }
+ }
+
+ void test_unique_result_null(){
+ def monet = new Artist(name: 'Monet').save()
+ def res = Portrait.withCriteria {
+ eq('artist', monet)
+ uniqueResult = true
+ }
+ assert res == null
+ }
+
+ void test_plastic_criteria_over_arrayList(){
+ def ls = [
+ [name: 'monet', bestPlace: [name: 'Japanese Bridge']],
+ [name: 'salvador', bestPlace: [name: 'Catalunya']],
+ ]
+ def rs = new PlasticCriteria(ls).list{
+ bestPlace{
+ eq('name', 'Japanese Bridge')
+ }
+ }
+ assert 1 == rs.size()
+ assert 'Japanese Bridge' == rs[0].bestPlace.name
+ }
}
View
21 src/groovy/plastic/criteria/PlasticCriteria.groovy
@@ -14,6 +14,9 @@ public class PlasticCriteria {
def _instanceValue
def _criteriaValue
def _critOptions
+
+ def uniqueResult
+
def theImplementations = [
"le":{ _instanceValue <= _criteriaValue },
"lt":{ _instanceValue < _criteriaValue },
@@ -34,6 +37,10 @@ public class PlasticCriteria {
"ltProperty":{ _instanceValue < _criteriaValue }
]
+ public PlasticCriteria(List list){
+ this._clazz = [list: {list}]
+ }
+
public PlasticCriteria(clazz){
this._clazz = clazz
}
@@ -218,7 +225,19 @@ public class PlasticCriteria {
}
ls = rs
}
- return ls
+ return _handleUniqueResult(ls)
+ }
+
+ def _handleUniqueResult(ls){
+ if(uniqueResult){
+ if(ls.size() > 1){
+ throw new org.hibernate.NonUniqueResultException(ls.size())
+ }else if(ls.size() == 1){
+ return ls[0]
+ }
+ }else{
+ return ls
+ }
}
def _runCriteria(cri, obj){

0 comments on commit f68b794

Please sign in to comment.
Something went wrong with that request. Please try again.