gorm/clause/joins.go

48 lines
906 B
Go
Raw Normal View History

2020-03-08 14:12:33 +03:00
package clause
2020-03-12 03:39:42 +03:00
type JoinType string
const (
CrossJoin JoinType = "CROSS"
2020-07-16 06:27:04 +03:00
InnerJoin JoinType = "INNER"
LeftJoin JoinType = "LEFT"
RightJoin JoinType = "RIGHT"
2020-03-12 03:39:42 +03:00
)
// Join join clause for from
type Join struct {
2020-04-15 04:14:24 +03:00
Type JoinType
Table Table
ON Where
Using []string
Expression Expression
2020-03-12 03:39:42 +03:00
}
func (join Join) Build(builder Builder) {
2020-04-15 04:14:24 +03:00
if join.Expression != nil {
join.Expression.Build(builder)
} else {
if join.Type != "" {
builder.WriteString(string(join.Type))
builder.WriteByte(' ')
}
2020-03-12 03:39:42 +03:00
2020-04-15 04:14:24 +03:00
builder.WriteString("JOIN ")
builder.WriteQuoted(join.Table)
2020-03-12 03:39:42 +03:00
2020-04-15 04:14:24 +03:00
if len(join.ON.Exprs) > 0 {
builder.WriteString(" ON ")
join.ON.Build(builder)
} else if len(join.Using) > 0 {
builder.WriteString(" USING (")
for idx, c := range join.Using {
if idx > 0 {
builder.WriteByte(',')
}
builder.WriteQuoted(c)
2020-03-12 03:39:42 +03:00
}
2020-04-15 04:14:24 +03:00
builder.WriteByte(')')
2020-03-12 03:39:42 +03:00
}
}
2020-03-08 14:12:33 +03:00
}