3

私の趣味のプロジェクトでは、次のような構造体があります。

type Resource struct {
    Id int
    ParentIds []int
    Title string
    Contents []byte
    Resources []Resource
}

各リソースには、いくつかのサブリソース ([]Resource) がある可能性があります。gorpのようなクエリから構造体へのマッパーを使用して開始したいのですが、次のようなクエリをマップする方法がわかりません

SELECT r.Id, r.Title, r.Contents
FROM Resources r
LEFT OUTER JOIN Resources sub ON sub.ParentIds @> ARRAY[r.Id]::integer[]

誰でも最小限の作業例を作成したり、関連するドキュメントを教えてもらえますか? おそらく、gorp はその仕事に適したツールではないでしょうか? より良い代替案がある場合は、提案も受け付けています。ありがとうございました。

4

1 に答える 1

2

https://github.com/go-gorp/gorpの gorp readme に参加の例があります。あなたがやっているように、別々のテーブルを配列に入れる組み込みの方法はないと思います。

InvoicePersonView は、クエリの結果を保持する構造体です。

// Run your query
query := "select i.Id InvoiceId, p.Id PersonId, i.Memo, p.FName " +
    "from invoice_test i, person_test p " +
    "where i.PersonId = p.Id"

// pass a slice to Select()
var list []InvoicePersonView
_, err := dbmap.Select(&list, query)

// this should test true
expected := InvoicePersonView{inv1.Id, p1.Id, inv1.Memo, p1.FName}
if reflect.DeepEqual(list[0], expected) {
    fmt.Println("Woot! My join worked!")
}
于 2014-07-24T06:56:43.977 に答える