forked from go-rel/rel
/
join_query.go
90 lines (75 loc) · 2.82 KB
/
join_query.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package rel
// JoinQuery defines join clause in query.
type JoinQuery struct {
Mode string
Table string
From string
To string
Filter FilterQuery
Arguments []interface{}
}
// Build query.
func (jq JoinQuery) Build(query *Query) {
query.JoinQuery = append(query.JoinQuery, jq)
}
// NewJoinWith query with custom join mode, table, field and additional filters with AND condition.
func NewJoinWith(mode string, table string, from string, to string, filter ...FilterQuery) JoinQuery {
return JoinQuery{
Mode: mode,
Table: table,
From: from,
To: to,
Filter: And(filter...),
}
}
// NewJoinFragment defines a join clause using raw query.
func NewJoinFragment(expr string, args ...interface{}) JoinQuery {
if args == nil {
// prevent buildJoin to populate From and To variable.
args = []interface{}{}
}
return JoinQuery{
Mode: expr,
Arguments: args,
}
}
// NewJoin with given table.
func NewJoin(table string, filter ...FilterQuery) JoinQuery {
return NewJoinWith("JOIN", table, "", "", filter...)
}
// NewJoinOn table with given field and optional additional filter.
func NewJoinOn(table string, from string, to string, filter ...FilterQuery) JoinQuery {
return NewJoinWith("JOIN", table, from, to, filter...)
}
// NewInnerJoin with given table and optional filter.
func NewInnerJoin(table string, filter ...FilterQuery) JoinQuery {
return NewInnerJoinOn(table, "", "", filter...)
}
// NewInnerJoinOn table with given field and optional additional filter.
func NewInnerJoinOn(table string, from string, to string, filter ...FilterQuery) JoinQuery {
return NewJoinWith("INNER JOIN", table, from, to, filter...)
}
// NewLeftJoin with given table and optional filter.
func NewLeftJoin(table string, filter ...FilterQuery) JoinQuery {
return NewLeftJoinOn(table, "", "", filter...)
}
// NewLeftJoinOn table with given field and optional additional filter.
func NewLeftJoinOn(table string, from string, to string, filter ...FilterQuery) JoinQuery {
return NewJoinWith("LEFT JOIN", table, from, to, filter...)
}
// NewRightJoin with given table and optional filter.
func NewRightJoin(table string, filter ...FilterQuery) JoinQuery {
return NewRightJoinOn(table, "", "", filter...)
}
// NewRightJoinOn table with given field and optional additional filter.
func NewRightJoinOn(table string, from string, to string, filter ...FilterQuery) JoinQuery {
return NewJoinWith("RIGHT JOIN", table, from, to, filter...)
}
// NewFullJoin with given table and optional filter.
func NewFullJoin(table string, filter ...FilterQuery) JoinQuery {
return NewFullJoinOn(table, "", "", filter...)
}
// NewFullJoinOn table with given field and optional additional filter.
func NewFullJoinOn(table string, from string, to string, filter ...FilterQuery) JoinQuery {
return NewJoinWith("FULL JOIN", table, from, to, filter...)
}