<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          Slick-pgSlick 的 PostgreSQL 擴(kuò)展

          聯(lián)合創(chuàng)作 · 2023-09-30 20:30

          Slick-pg 是一些針對(duì) PostgreSQL 的 Slick 擴(kuò)展,用于支持 PostgreSQL 的(特有)類型及/或相關(guān)函數(shù)。如果你對(duì)使用 Slick 來開發(fā)基于 PostgreSQL 的程序感興趣,那么你會(huì)發(fā)現(xiàn) slick-pg 非常有用。

          有了它,我們就可以在 Scala/Slick 項(xiàng)目里自由的使用那些 PostgreSQL 特有的、因而 Slick 肯定不會(huì)內(nèi)置支持的那些數(shù)據(jù)類型及其操作/函數(shù)來構(gòu)造 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


          瀏覽 20
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          編輯 分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          編輯 分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  国产18禁黄网站禁片免费视频 | 天堂网8| 亚洲婷婷丁香五月 | 毛片4级中文字幕无码 | 成人网不用播放器的 |