光滑映射postgres的记录数组

时间:2013-11-26 19:17:08

标签: postgresql orm slick

有人可以通过向我展示如何映射这两种类型来帮助我:

case class forumQuote(
            index:          Int,
            startOffset:    Int,
            endOffset:      Int,
            isDirect:       Boolean,
            quotedId:       Int)
case class forumQuotes(quotes: List[forumQuote])

代表postgres As:

CREATE TYPE forum_quote AS
(
    index           INTEGER,
    q_start_offset  INTEGER,
    q_end_offset    INTEGER,
    is_direct       BOOLEAN,
    quoted_id       INTEGER
);

用作

中的数组字段
CREATE TABLE forum_posts
(
...
quotes      forum_quote [],
...
)

在我自己的提升表中用作:

object ForumPosts extends Table[...] {
...
def quotes = Column[forumQuotes]("forum_quotes")
...
}

注意:我不希望看到任何JDBC数组的使用,因为我需要稍后使用hstore做一些时髦的东西(Key [String] =&gt; Value [Array [T]])其中T是postgresql记录。< / p>

1 个答案:

答案 0 :(得分:2)

FYI slick-pg将很快完全支持记录和数组。


我从光滑的家伙中调整了postgres扩展插件的代码。下面显示了如何获取记录和记录数组的类型映射器。

  object ForumQuoteMapper extends RecordMapper[ForumQuote] {
    val name = "forum_quote"
    val extractorRegex = "\\((\\d+),(\\d+),(\\d+),([t|f]),(\\d+)\\)".r
    def fnFromString = (lit: String) =>  {
      def toBool(str: String) = { str match { case "t" => true; case "f" => false}}

      lit match {
        case extractorRegex(index,startOffset,endOffset,bol,quotedId) =>
          ForumQuote(index.toInt,startOffset.toInt,endOffset.toInt,toBool(bol),quotedId.toInt)
      }}
    def fnToString = (v: ForumQuote)=>s"(${v.index}, ${v.startOffset}, ${v.endOffset}, ${v.isDirect},${v.quotedId})"
  }
  implicit val forumQuoteRecordMapper = ForumQuoteMapper.getRecordMapper
  implicit val forumQuoteArrayMapper = ForumQuoteMapper.getRecordArrayMapper

可以找到改编的代码here。一旦我开始将它集成到我的代码中,我可能会发现并修复大量的错误:D但是它会按原样传递基本的测试用例。