Dynamic OR filtering - Slick

前端 未结 2 1332
执笔经年
执笔经年 2021-01-15 14:12

Ok, I\'ve got a method with multiple optional arguments like this

def(username: Option[String], petname: Option[String], favouritefood: Option[String])
         


        
2条回答
  •  清酒与你
    2021-01-15 14:41

    Something like this should work. I had to use a similiar fragment in my own code and it is also close to what cvogt proposes in above comment (I think).

    val username = Option("")
    val petname = Option("")
    val ff:Option[String] = None
    
    val default = LiteralColumn(1) === LiteralColumn(1) 
    
    yourTable.filter { it => 
      List(
          username.map(it.username === _),
          petname.map(it.petname === _),
          ff.map(it.ff === _)
      ).collect({case Some(it)  => it}).reduceLeftOption(_ || _).getOrElse(default)
    }
    

提交回复
热议问题