Hi Xiam,
I would really like some support for (MySQL) joins. And I guess to would be nice for Postgres to.
For example:
var Order struct {
Id int64 db:"id"
CustomerId int64 db:"customer_id"
Date time.Time db:"date"
}
var Customer struct {
Id int64 db:"id"
Name stringdb:"name"
}
var Result struct {
Order Order db:",inline"
Customer Customer db:",inline"
}
// Join type proposal ?
type Join struct {
Join interface{} // db.Raw{LEFT JOIN
}
Table interface{} // db.Raw{customers
}
Alias interface{} // db.Raw{Customer
}
Key interface{} // db.Raw{Customer.Id
}
ForeignKey interface{} // db.Raw{Order.customer_id
}
}
myJoin := Join{
db.Raw{LEFT JOIN
},
db.Raw{'customers'},
db.Raw{'Customer'},
db.Raw{'Customer.id'},
db.Raw{'Order.customer_id'},
}
col, err := sess.Collection(orders AS Order
)
res := col.Find(db.Cond{"Order.id":1}).Join(myJoin).Select('Order.id, Customer.name')
var result []Result
err = res.All(&result)
....
The query output should then be something like this:
SELECT Order.id , Customer.id, Customer.name
FROM orders
AS Order
LEFT JOIN customers AS Customer ON customers.id = Order.customer_id
WHERE (Order
.id
= ?)
I suspect that the .All() and .One needs to be modified to, a possible solution might be the using the alias instead of ,inline as the db struct tag.
This way u can easily wrap multiple "structs e.a Order,Customer" into a single result, which in my case can I can marshall to JSON and use it for an API.
But then, if you have a better solution then I probably missed so please let me know the better approach.
Best regards,
Jeroen