You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

56 lines
1.5 KiB

2 years ago
  1. // Copyright 2019 The Xorm Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package builder
  5. // InnerJoin sets inner join
  6. func (b *Builder) InnerJoin(joinTable, joinCond interface{}) *Builder {
  7. return b.Join("INNER", joinTable, joinCond)
  8. }
  9. // LeftJoin sets left join SQL
  10. func (b *Builder) LeftJoin(joinTable, joinCond interface{}) *Builder {
  11. return b.Join("LEFT", joinTable, joinCond)
  12. }
  13. // RightJoin sets right join SQL
  14. func (b *Builder) RightJoin(joinTable, joinCond interface{}) *Builder {
  15. return b.Join("RIGHT", joinTable, joinCond)
  16. }
  17. // CrossJoin sets cross join SQL
  18. func (b *Builder) CrossJoin(joinTable, joinCond interface{}) *Builder {
  19. return b.Join("CROSS", joinTable, joinCond)
  20. }
  21. // FullJoin sets full join SQL
  22. func (b *Builder) FullJoin(joinTable, joinCond interface{}) *Builder {
  23. return b.Join("FULL", joinTable, joinCond)
  24. }
  25. // Join sets join table and conditions
  26. func (b *Builder) Join(joinType string, joinTable, joinCond interface{}, alias ...string) *Builder {
  27. aliasName := ""
  28. if len(alias) > 0 {
  29. aliasName = alias[0]
  30. }
  31. switch joinCond.(type) {
  32. case Cond:
  33. b.joins = append(b.joins, join{
  34. joinType: joinType,
  35. joinTable: joinTable,
  36. joinCond: joinCond.(Cond),
  37. joinAlias: aliasName,
  38. })
  39. case string:
  40. b.joins = append(b.joins, join{
  41. joinType: joinType,
  42. joinTable: joinTable,
  43. joinCond: Expr(joinCond.(string)),
  44. joinAlias: aliasName,
  45. })
  46. }
  47. return b
  48. }