https://kotlinlang.org logo
#exposed
Title
# exposed
t

tmseiler

03/13/2018, 10:15 PM
Is there something like Django's "Q" object?
t

tapac

03/14/2018, 6:53 AM
I'm not aware of Django, so could you provide use-case where and how you want to use optional parameters?
t

tmseiler

03/14/2018, 12:45 PM
Let's say you have a REST resource that might be filtered via get parameters, in any arbitrary combination. What's the preferred exposed approach to building this where clause/select?
Django has a "Q" object to dynamically build these logical conditions https://docs.djangoproject.com/en/1.7/ref/models/queries/#q-objects
t

tapac

03/14/2018, 2:11 PM
Copy code
val baseCondition = Op.build { FooTable.type eq type }
val extendedCondition = baseCondition and when(type) {
    type1 -> Op.build { FooTable.col1 eq "bar" }
   else -> Op.build {} 
}
t

tmseiler

03/14/2018, 2:14 PM
Cool, I like that. Thanks
t

tapac

03/14/2018, 2:14 PM
Another way is to use
adjustWhere
on Query.
Copy code
val baseQuery = FooTable.select { FooTable.type eq type } 
if (cond()) {
   baseQuery.adjustWhere { 
      this!!.and FooTable.col1 eq "bar"
   }
}