Hoping you might be able to give me some pointers on whether it is possible to dynamically construct these queries in a type safe manner using the query DSL, or whether I'm wasting my time and should just dynamically construct the query using string manipulation. I have done this for hibernate using plain old string manipulation and that works fine. My use case is where the UI has a list screen and 1-n search criteria fields which, if not blank, should form the where clause of the query. This scenario is very common so I want to avoid boiler plate code as much as possible. e.g.,
ignoring search fields with no criteria, converting to lower case, etc. Here is my current function showing working code for a simplistic single search criteria scenario. It also shows code that looks like what I want but doesn't compile. Is it possible to dynamically construct the query from a list of supplied search criteria using the query DSL? def getSearchQuery[T](entity: Entity[_, Persisted, T], criteriaList: List[(ColumnInfo[T, String], TextField)]) = { // This works for the simple case where we just look at the list head. val criteria = criteriaList(0) ( select from entity where (lower(criteria._1) like s"${criteria._2.value.get.toString.toLowerCase}%")) // But what we really want is to dynamically construct the where clause in a // type-safe manner using the query DSL. // ( // select from entity where // (for { // (ci, v) <- criteriaList // if (v.value.get.toString.length > 0) // } yield (lower(ci) like s"${v.value.get.toString.toLowerCase}%")) // ) } The application then runs the query as follows:
val p = SportZmanInternetUI.personEntity val persons = SportZmanInternetUI.queryDao.query(getSearchQuery(p, List( (p.surname, txtSearchSurname), (p.firstName, txtSearchFirstName)))) REGARDS
Peter
--
You received this message because you are subscribed to the Google Groups "mapperdao" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mapperdao+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
def getSearchQuery[ID, T](entity: Entity[ID, Persisted, T], criteriaList: List[(ColumnInfo[T, String], TextField)]) = {val filteredCriteria = criteriaList filter (_._2.value.get.toString.length > 0)val selectClause = select from entityfilteredCriteria.length match {case 0 => selectClausecase 1 => selectClause where (lower(filteredCriteria(0)._1) like s"${filteredCriteria(0)._2.value.get.toString.toLowerCase}%")case _ => selectClause where (lower(filteredCriteria(0)._1) like s"${filteredCriteria(0)._2.value.get.toString.toLowerCase}%")}}
overloaded method value query with alternatives:[ID(in method query)(in method query)(in method query)(in method query)(in method query)(in method query)(in method query)(in method query), PC(in method query)(in method query)(in method query)(in method query)(in method query)(in method query)(in method query)(in method query) <: com.googlecode.mapperdao.Persisted, T(in method query)(in method query)(in method query)(in method query)(in method query)(in method query)(in method query)(in method query)](qe: com.googlecode.mapperdao.Query.Builder[ID(in method query)(in method query)(in method query)(in method query)(in method query)(in method query)(in method query)(in method query),PC(in method query)(in method query)(in method query)(in method query)(in method query)(in method query)(in method query)(in method query),T(in method query)(in method query)(in method query)(in method query)(in method query)(in method query)(in method query)(in method query)])List[T(in method query)(in method query)(in method query)(in method query)(in method query)(in method query)(in method query)(in method query) with PC(in method query)(in method query)(in method query)(in method query)(in method query)(in method query)(in method query)(in method query)]<and>[ID(in method query)(in method query)(in method query)(in method query)(in method query)(in method query)(in method query)(in method query), PC(in method query)(in method query)(in method query)(in method query)(in method query)(in method query)(in method query)(in method query) <: com.googlecode.mapperdao.Persisted, T(in method query)(in method query)(in method query)(in method query)(in method query)(in method query)(in method query)(in method query)](qe: com.googlecode.mapperdao.Query.Where[ID(in method query)(in method query)(in method query)(in method query)(in method query)(in method query)(in method query)(in method query),PC(in method query)(in method query)(in method query)(in method query)(in method query)(in method query)(in method query)(in method query),T(in method query)(in method query)(in method query)(in method query)(in method query)(in method query)(in method query)(in method query)])List[T(in method query)(in method query)(in method query)(in method query)(in method query)(in method query)(in method query)(in method query) with PC(in method query)(in method query)(in method query)(in method query)(in method query)(in method query)(in method query)(in method query)]cannot be applied to (com.googlecode.mapperdao.Query.OrderBy[_12])
def getSearchQuery[ID, T](entity: Entity[ID, Persisted, T], criteriaList: List[(ColumnInfo[T, String], TextField)]): Builder[ID, Persisted, T] = {
val s = suburbEntityval txtSearchField = new TextFieldval query = getSearchQuery(s, List(
info(query.toString)val results = queryDao.query(query)
def getSearchQuery[ID, T](entity: Entity[ID, Persisted, T], criteriaList: List[(ColumnInfo[T, String], TextField)]) = {
val criteria = criteriaList(0)val test1 = (
select from entity where(lower(criteria._1) like s"${criteria._2.value.get.toString.toLowerCase}%"))
test1}
def getSearchQuery[ID, T](entity: Entity[ID, Persisted, T], criteriaList: List[(ColumnInfo[T, String], TextField)]) = {val filteredCriteria = criteriaList filter (_._2.value.get.toString.length > 0)val selectClause = select from entity
val test2 = filteredCriteria.length match {
case 0 => selectClausecase 1 => selectClause where (lower(filteredCriteria(0)._1) like s"${filteredCriteria(0)._2.value.get.toString.toLowerCase}%")case _ => selectClause where (lower(filteredCriteria(0)._1) like s"${filteredCriteria(0)._2.value.get.toString.toLowerCase}%")}
test2}
def getSearchQuery[ID, T](entity: Entity[ID, Persisted, T], criteriaList: List[(ColumnInfo[T, String], TextField)]): Where[ID, Persisted, T] = {
val filteredCriteria = criteriaList filter (_._2.value.get.toString.length > 0)val selectClause = select from entity
val test2 = filteredCriteria.length match {
case 0 => selectClausecase 1 => selectClause where (lower(filteredCriteria(0)._1) like s"${filteredCriteria(0)._2.value.get.toString.toLowerCase}%")case _ => selectClause where (lower(filteredCriteria(0)._1) like s"${filteredCriteria(0)._2.value.get.toString.toLowerCase}%")}
test2}
def getSearchQuery[ID, T](entity: Entity[ID, Persisted, T], criteriaList: List[(ColumnInfo[T, String], TextField)]) = {val filteredCriteria = criteriaList filter (_._2.value.get.toString.length > 0)val selectClause = select from entityval test2 = filteredCriteria.length match {case 0 => selectClausecase 1 => selectClause where (lower(filteredCriteria(0)._1) like s"${filteredCriteria(0)._2.value.get.toString.toLowerCase}%")case _ => selectClause where (lower(filteredCriteria(0)._1) like s"${filteredCriteria(0)._2.value.get.toString.toLowerCase}%")}test2}
def getSearchQuery[ID, T](entity: Entity[ID, Persisted, T], criteriaList: List[(ColumnInfo[T, String], String)]) = {val filteredCriteria = criteriaList filter (_._2.length > 0)
val selectClause = select from entity
val test = filteredCriteria.length match {case 0 => new Where(selectClause)case 1 => selectClause where (lower(filteredCriteria(0)._1) like s"${filteredCriteria(0)._2.toLowerCase}%")case _ => selectClause where (lower(filteredCriteria(0)._1) like s"${filteredCriteria(0)._2.toLowerCase}%")}test}
def getSearchQuery[ID, T](entity: Entity[ID, Persisted, T], criteriaList: List[(ColumnInfo[T, String], String)]) = {val filteredCriteria = criteriaList filter (_._2.length > 0)val selectClause = select from entity
filteredCriteria foreach (whereClause => {debug("before: " + selectClause)selectClause where (lower(whereClause._1) like s"${whereClause._2.toLowerCase}%")debug(" after: " + selectClause)})selectClause}
val selectClause = select from entity
criteriaList.filter(_._2.length > 0).foldLeft(selectClause)((q, w) => q where (lower(w._1) like s"${w._2.toLowerCase}%"))
type CriteriaListElement[T] = (ColumnInfo[T, String], String)def getSearchQuery[ID, T](entity: Entity[ID, Persisted, T], criteriaList: List[CriteriaListElement[T]]) = {
val selectClause = select from entity
val filteredCriteria = criteriaList filter (e => e._2 != null && e._2.length > 0)if (filteredCriteria.length > 0) {def criteriaExpression(e: CriteriaListElement[T]) = { (lower(e._1) like s"${e._2.toLowerCase}%") }val whereClause = selectClause where criteriaExpression(filteredCriteria(0))filteredCriteria drop (1) foreach (criteria => { whereClause and criteriaExpression(criteria) })}selectClause}