-
Notifications
You must be signed in to change notification settings - Fork 2
/
join.go
46 lines (38 loc) · 1.08 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
package sol
import (
"fmt"
"github.com/aodin/sol/dialect"
)
// JoinClause implements a variety of joins
type JoinClause struct {
ArrayClause
method string
table Tabular // interface, since there are dialect-specific tables
}
// String returns a default string representation of the JoinClause
func (j JoinClause) String() string {
compiled, _ := j.Compile(&defaultDialect{}, Params())
return compiled
}
// Compile compiles a JoinClause
func (j JoinClause) Compile(d dialect.Dialect, ps *Parameters) (string, error) {
// Ignore clauses if CROSS
if j.method == CROSSJOIN {
return fmt.Sprintf(`%s %s`, CROSSJOIN, j.table.Name()), nil
}
// If no clauses were given, assume the join is NATURAL
if len(j.ArrayClause.clauses) == 0 {
return fmt.Sprintf(
`NATURAL %s %s`, j.method, j.table.Name(),
), nil
}
// TODO Pass the joining table and auto-create the condition?
// Compile the clauses of the join statement
clauses, err := j.ArrayClause.Compile(d, ps)
if err != nil {
return "", err
}
return fmt.Sprintf(
`%s %s ON %s`, j.method, j.table.Name(), clauses,
), nil
}