builder_union.go 982 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. // Copyright 2018 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. import (
  6. "fmt"
  7. "strings"
  8. )
  9. func (b *Builder) unionWriteTo(w Writer) error {
  10. if b.limitation != nil || b.cond.IsValid() ||
  11. b.orderBy != "" || b.having != "" || b.groupBy != "" {
  12. return ErrNotUnexpectedUnionConditions
  13. }
  14. for idx, u := range b.unions {
  15. current := u.builder
  16. if current.optype != selectType {
  17. return ErrUnsupportedUnionMembers
  18. }
  19. if len(b.unions) == 1 {
  20. if err := current.selectWriteTo(w); err != nil {
  21. return err
  22. }
  23. } else {
  24. if b.dialect != "" && b.dialect != current.dialect {
  25. return ErrInconsistentDialect
  26. }
  27. if idx != 0 {
  28. fmt.Fprint(w, fmt.Sprintf(" UNION %v ", strings.ToUpper(u.unionType)))
  29. }
  30. fmt.Fprint(w, "(")
  31. if err := current.selectWriteTo(w); err != nil {
  32. return err
  33. }
  34. fmt.Fprint(w, ")")
  35. }
  36. }
  37. return nil
  38. }