微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Slick-pg Slick 的 PostgreSQL 扩展

程序名称:Slick-pg

授权协议: BSD

操作系统: 跨平台

开发语言: Scala

Slick-pg 介绍

Slick-pg 是一些针对 PostgresqlSlick 扩展,用于支持
Postgresql 的(特有)类型及/或相关函数。如果你对使用 Slick 来开发基于
Postgresql 的程序感兴趣,那么你会发现 slick-pg 非常有用。

有了它,我们就可以在 Scala/Slick 项目里自由的使用那些 Postgresql 特有的、因而
Slick 肯定不会内置支持的那些数据类型及其操作/函数来构造 sql 查询了:

import MyPostgresDriver.simple._

class TestTable(tag: Tag) extends Table[Test](tag, Some("xxx"), "Test") {
  def id = column[Long]("id", O.AutoInc, O.PrimaryKey)
  def during = column[Range[Timestamp]]("during")
  def location = column[Point]("location")
  def text = column[String]("text", O.DBType("varchar(4000)"))
  def props = column[Map[String,String]]("props_hstore")
  def tags = column[List[String]]("tags_arr")

  def * = (id, during, location, text, props, tags) <> (Test.tupled, Test.unapply)
}

object tests extends TableQuery(new TestTable(_)) {
  ///
  def byId(ids: Long*) = tests.filter(_.id inSetBind ids).map(t => t)
  // will generate sql like: select * from test where tags && ?
  def byTag(tags: String*) = tests.filter(_.tags @& tags.toList.bind).map(t => t)
  // will generate sql like: select * from test where during && ?
  def byTsRange(tsRange: Range[Timestamp]) = tests.filter(_.during @& tsRange.bind).map(t => t)
  // will generate sql like: select * from test where case(props -> ? as [T]) == ?
  def byProperty[T](key: String, value: T) = tests.filter(_.props.>>[T](key.bind) === value.bind).map(t => t)
  // will generate sql like: select * from test where ST_DWithin(location, ?, ?)
  def bydistance(point: Point, distance: Int) = tests.filter(r => r.location.dWithin(point.bind, distance.bind)).map(t => t)
  // will generate sql like: 
  //   select id, text, ts_rank(to_tsvector(text), to_tsquery(?)) 
  //   from test 
  //   where to_tsvector(text) @@ to_tsquery(?) 
  //   order by ts_rank(to_tsvector(text), to_tsquery(?))
  def search(queryStr: String) = tests.filter(tsvector(_.text) @@ tsQuery(queryStr.bind))
        .map(r => (r.id, r.text, tsRank(tsvector(r.text), tsQuery(queryStr.bind))))
        .sortBy(_._3)
}

...

目前支持的 Postgresql 类型有:

  • ARRAY

  • JSON

  • Date/Time

  • Enum

  • Range

  • HStore

  • LTree

  • Inet/MacAddr

  • text Search

  • postgis Geometry

目前支持的 Postgresql 特性有:

  • inherits
  • Composite type (basic)
  • aggregate functions
  • window functions

Slick-pg 官网

https://github.com/tminglei/slick-pg

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐