forked from goal-web/querybuilder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjoin.go
91 lines (76 loc) · 2.37 KB
/
join.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
91
package querybuilder
import (
"fmt"
"github.com/goal-web/contracts"
)
type Join struct {
table string
join contracts.JoinType
conditions *Wheres
}
func (this Join) String() (result string) {
result = fmt.Sprintf("%s join %s", this.join, this.table)
if this.conditions.IsEmpty() {
return
}
result = fmt.Sprintf("%s on (%s)", result, this.conditions.String())
return
}
type Joins []Join
func (this Joins) IsEmpty() bool {
return len(this) == 0
}
func (this Joins) String() (result string) {
if this.IsEmpty() {
return
}
for index, join := range this {
if index == 0 {
result = join.String()
} else {
result = fmt.Sprintf("%s %s", result, join.String())
}
}
return
}
func (this *Builder) Join(table string, first, condition, second string, joins ...contracts.JoinType) contracts.QueryBuilder {
join := contracts.InnerJoin
if len(joins) > 0 {
join = joins[0]
}
this.joins = append(this.joins, Join{table, join, &Wheres{wheres: map[contracts.WhereJoinType][]*Where{
contracts.And: {&Where{
field: first,
condition: condition,
arg: second,
}},
}}})
return this
}
func (this *Builder) JoinSub(provider contracts.QueryProvider, as, first, condition, second string, joins ...contracts.JoinType) contracts.QueryBuilder {
join := contracts.InnerJoin
if len(joins) > 0 {
join = joins[0]
}
subBuilder := provider()
this.joins = append(this.joins, Join{fmt.Sprintf("(%s) as %s", subBuilder.ToSql(), as), join, &Wheres{wheres: map[contracts.WhereJoinType][]*Where{
contracts.And: {&Where{
field: first,
condition: condition,
arg: second,
}},
}}})
return this.addBinding(joinBinding, subBuilder.GetBindings()...)
}
func (this *Builder) FullJoin(table string, first, condition, second string) contracts.QueryBuilder {
return this.Join(table, first, condition, second, contracts.FullJoin)
}
func (this *Builder) FullOutJoin(table string, first, condition, second string) contracts.QueryBuilder {
return this.Join(table, first, condition, second, contracts.FullOutJoin)
}
func (this *Builder) LeftJoin(table string, first, condition, second string) contracts.QueryBuilder {
return this.Join(table, first, condition, second, contracts.LeftJoin)
}
func (this *Builder) RightJoin(table string, first, condition, second string) contracts.QueryBuilder {
return this.Join(table, first, condition, second, contracts.RightJoin)
}