scala - How can I reuse Slick query -


my table definitions are

class ipv4tocountries(tag: tag) extends table[(long, string)](tag, "ip2countries") {   def ip = column[long]("ip")   def country = column[string]("country")    def * = (ip, country) }  class ipv6tocountries(tag: tag) extends table[(bigdecimal, string)](tag, "ipv6_2countries") {   def ip = column[bigdecimal]("ip")   def country = column[string]("country")    def * = (ip, country) }  class country2languages(tag: tag) extends table[(string, string, string)](tag, "country2languages") {   def code = column[string]("code")   def lang_code = column[string]("lang_code")   def iso_country = column[string]("iso_country")    def * = (code, lang_code, iso_country) } 

note difference between ipv4tocountries , ipv6tocountries type of ip column. here query function:

  def getlangcode(ip: string): future[string] = {     inetaddress.getbyname(ip) match {       case ipv4: inet4address =>         val q = (for {           <- ipv4s.sortby(_.ip.desc) if i.ip < ipv4tolong(ipv4)           c <- ip2nationcountries if i.country === c.code         } yield c.lang_code).take(1)         db.run(q.result.head)       case ipv6: inet6address =>         val q = (for {           <- ipv6s.sortby(_.ip.desc) if i.ip < ipv6todecimal(ipv6)           c <- ip2nationcountries if i.country === c.code         } yield c.lang_code).take(1)         db.run(q.result.head)     }   } 

the query ipv4 , ipv6 identical condition if i.ip < addrtonumeric.

is there way reuse query?

you can have common parametrized class like

class iptocontries[a: typemapper](t: tag, s: string) extends table[(a, string)](t, s) {   def ip = column[a]("ip")   def country = column[string]("country")    def * = (ip, country) } 

and use like

class ipv4countries(tag: tag) extends iptocontries[long](tag, "ip2countries") class ipv6countries(tag: tag) extends iptocontries[bigdecimal](tag, "ipv6_2countries") 

i haven't tested it, typemapper context bound on a should specify generic type enough used in fashion.


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -