Slick plain SQL How to pass List[int] Parameters

Source: Internet
Author: User

Recently used slick as a database access framework, some functions are not good implementation, with plain Sql,sql is based on query conditions generated, so the parameters are not fixed number. But Staticquery[t, Entity] inside the T, the default only supports the type of database support such as int,double, as well as the corresponding optional, can also support TUPLE1--TUPLE22, as long as the type is The type supported by the database. but because I have to pass the parameters in the compilation period can not know how many, so only with List[int] to pass, the results of the error said that the corresponding implicit conversion is not found.

[ERROR] D:\xx.scala:49:could Notfind implicit value for parameter Pconv1:scala.slick.jdbc.setparameter[list[int]][error]     val query = Q[list[int], Entity] + sql[error]                  ^[error] One error found[error] (compile:compile) compilation Faile D



The first thought is there is no way to list[int], into a tuple, carefully think, found that the wrong, List[int], which contains parameters is not fixed, but Tuplex, need to specify in the compilation period, in the end is a few parameters. Google, and found that it is not good enough.

then just want to see what the Staticquery this class specifically did, point in to see the source code, found that it needs a implicit parameter

Object Staticquery {...  .... def Apply[p, R] (implicit pconv1:setparameter[p],  rconv:getresult[r]) = Query[p,r] ("") ...}



And then continue to see Setparameter,

Trait Setparameter[-t] Extends ((T, positionedparameters) = Unit) {self =  def applied (value:t): Setparameter [Unit] = new Setparameter[unit] {    def apply (U:unit, pp:positionedparameters) {      self.apply (value, pp)    }  }}



And in Object setparameter a bunch of just said default supported parameter types, including int, Optional[int], tuple2[int, int] and so on, looked at the implementation

Implicit object Setint extends Setparameter[int] {def apply (V:int, pp:positionedparameters) {pp.setint (v)}}



It is very simple to think of the implementation of List[int], so in their own class defined a list[int] implicit transformation

Implicit object Setlistint extends Setparameter[list[int]] {    def apply (Vlist:list[int], pp:positionedparameters) { For      (v <-vList)        pp.setint (v)    }  }



Because often use, so put in Basedao inside, fix.

Summary

After encountering the problem, first think about whether it can be converted into a future implementation (List[int]-tuplex[int]); If not, Google, StackOverflow and other search have no other solution; Finally, you can see the source by yourself. Code to master the principle, the realization of their own.

Slick plain SQL How to pass List[int] Parameters

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.