2014-05-05 31 views
5

怎麼辦例如,在連接多個領域,如下面?Scala Slick 2加入多個字段?

val ownerId = 1 
val contactType = 1 
... 
val contact = for { 
    (t, c) <- ContactTypes leftJoin Contacts on (_.id === _.typeId && _.ownerId === ownerId) 
    if t.id === contactType 
} yield (c.?, t) 

我該如何使用Slick 2.0.1來實現? Idelly我需要光滑生成這種查詢

SELECT 
    x2."contact_id", 
    x2."type_id", 
    x2."owner_id", 
    x2."value", 
    x2."created_on", 
    x2."updated_on", 
    x3."id", 
    x3."type", 
    x3."model" 
FROM 
    (
     SELECT 
      x4."id" AS "id", 
      x4."type" AS "type", 
      x4."model" AS "model" 
     FROM 
      "contact_types" x4 
    )x3 
LEFT OUTER JOIN(
    SELECT 
     x5."created_on" AS "created_on", 
     x5."value" AS "value", 
     x5."contact_id" AS "contact_id", 
     x5."updated_on" AS "updated_on", 
     x5."type_id" AS "type_id", 
     x5."owner_id" AS "owner_id" 
    FROM 
     "contacts" x5 
)x2 ON x3."id" = x2."type_id" AND x2.owner_id = 1 
WHERE 
    (x3."id" = 3) 

請注意ON X3。 「ID」= X2。 「TYPE_ID」 AND x2.owner_id = 16

回答

3

好了,通過網站挖掘後和源代碼我認爲終於找到了溶液

leftJoin上()方法接受以下預解碼參數:(E1,E2)=> T,所以我們簡單地可以這樣做

val contacts = for { 
    (t, c) <- ContactTypes leftJoin Contacts on ((type, contact) => { 
    type.id === contact.typeId && contact.ownerId === ownerId 
    }) 
} yield (c.?, t) 

按照需要生成的SQL查詢。

+1

對子句的權利是:{case(type,contact)=> type.id === contact.typeId && contact.ownerId === ownerId } Vlad的內括號沒有任何意義。 – Epicurist

+0

@Epicurist爲什麼?你能解釋一下嗎? –

+0

你不同意,type.id === contact.typeId && contact.ownerId === OWNERID不需要括號呢? – Epicurist