Slick-pg是一些针对PostgreSQL的Slick扩展,用于支持PostgreSQL的(特有)类型及/或相关函数。如果你对使用 Slick 来开发基于PostgreSQL的程序感兴趣,那么你会发现slick-pg非常有用。
有了它,我们就可以在Scala/Slick项目里自由的使用那些PostgreSQL特有的、因而 Slick 肯定不会内置支持的那些数据类型及其操作/函数来构造SQL查询了:
importMyPostgresDriver.simple._classTestTable(tag:Tag)extendsTable[Test](tag,Some("xxx"),"Test"){defid=column[Long]("id",O.AutoInc,O.PrimaryKey)defduring=column[Range[Timestamp]]("during")deflocation=column[Point]("location")deftext=column[String]("text",O.DBType("varchar(4000)"))defprops=column[Map[String,String]]("props_hstore")deftags=column[List[String]]("tags_arr")def*=(id,during,location,text,props,tags)<>(Test.tupled,Test.unapply)}objecttestsextendsTableQuery(newTestTable(_)){///defbyId(ids:Long*)=tests.filter(_.idinSetBindids).map(t=>t)//willgeneratesqllike:select*fromtestwheretags&&?defbyTag(tags:String*)=tests.filter(_.tags@&tags.toList.bind).map(t=>t)//willgeneratesqllike:select*fromtestwhereduring&&?defbyTsRange(tsRange:Range[Timestamp])=tests.filter(_.during@&tsRange.bind).map(t=>t)//willgeneratesqllike:select*fromtestwherecase(props->?as[T])==?defbyProperty[T](key:String,value:T)=tests.filter(_.props.>>[T](key.bind)===value.bind).map(t=>t)//willgeneratesqllike:select*fromtestwhereST_DWithin(location,?,?)defbyDistance(point:Point,distance:Int)=tests.filter(r=>r.location.dWithin(point.bind,distance.bind)).map(t=>t)//willgeneratesqllike://selectid,text,ts_rank(to_tsvector(text),to_tsquery(?))//fromtest//whereto_tsvector(text)@@to_tsquery(?)//orderbyts_rank(to_tsvector(text),to_tsquery(?))defsearch(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/MacAddrtextSearch
postgisGeometry
目前支持的PostgreSQL特性有:
inheritsCompositetype(basic)aggregatefunctionswindowfunctions
评论