joins.go 906 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package clause
  2. type JoinType string
  3. const (
  4. CrossJoin JoinType = "CROSS"
  5. InnerJoin JoinType = "INNER"
  6. LeftJoin JoinType = "LEFT"
  7. RightJoin JoinType = "RIGHT"
  8. )
  9. // Join join clause for from
  10. type Join struct {
  11. Type JoinType
  12. Table Table
  13. ON Where
  14. Using []string
  15. Expression Expression
  16. }
  17. func (join Join) Build(builder Builder) {
  18. if join.Expression != nil {
  19. join.Expression.Build(builder)
  20. } else {
  21. if join.Type != "" {
  22. builder.WriteString(string(join.Type))
  23. builder.WriteByte(' ')
  24. }
  25. builder.WriteString("JOIN ")
  26. builder.WriteQuoted(join.Table)
  27. if len(join.ON.Exprs) > 0 {
  28. builder.WriteString(" ON ")
  29. join.ON.Build(builder)
  30. } else if len(join.Using) > 0 {
  31. builder.WriteString(" USING (")
  32. for idx, c := range join.Using {
  33. if idx > 0 {
  34. builder.WriteByte(',')
  35. }
  36. builder.WriteQuoted(c)
  37. }
  38. builder.WriteByte(')')
  39. }
  40. }
  41. }