github.com/yongjacky/phoenix-go-orm-builder@v0.3.5/builder_union.go (about)

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