-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_test.go
53 lines (41 loc) · 958 Bytes
/
example_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package greasel
import "fmt"
// Schemas
type userTable struct {
*Table
Id *IntField
}
func NewUserAlias() *userTable {
table := NewTable("users")
return &userTable{
Table: table,
Id: NewIntField(table, "id"),
}
}
// Generate relation condition from bind
func (u *userTable) PhotoAssoc(p *photoTable) (*Table, Filter) {
return p.Table, p.UserId.IsEq(u.Id)
}
type photoTable struct {
*Table
Id *IntField
UserId *IntField
}
func NewPhotoAlias() *photoTable {
table := NewTable("posts")
return &photoTable{
Table: table,
Id: NewIntField(table, "id"),
UserId: NewIntField(table, "user_id"),
}
}
// Examples
func Example() {
u, p := NewUserAlias(), NewPhotoAlias()
q := From(u.Table). // fixme -> interface
Where(Or(u.Id.Eq(1), u.Id.Eq(2))).
InnerJoin(u.PhotoAssoc(p))
fmt.Println(q.SQL())
// Output:
// SELECT u0.id FROM users AS u0 INNER JOIN posts AS p1 ON (p1.user_id = u0.id ) WHERE ((u0.id = ? OR u0.id = ? ) )
}