github.com/yongjacky/phoenix-go-orm-builder@v0.3.5/builder_set_operations.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) setOpWriteTo(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, o := range b.setOps {
    19  		current := o.builder
    20  		if current.optype != selectType {
    21  			return ErrUnsupportedUnionMembers
    22  		}
    23  
    24  		if len(b.setOps) == 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  				if o.distinctType == "" {
    35  					fmt.Fprint(w, fmt.Sprintf(" %s ", strings.ToUpper(o.opType)))
    36  				} else {
    37  					fmt.Fprint(w, fmt.Sprintf(" %s %s ", strings.ToUpper(o.opType), strings.ToUpper(o.distinctType)))
    38  				}
    39  			}
    40  			fmt.Fprint(w, "(")
    41  
    42  			if err := current.selectWriteTo(w); err != nil {
    43  				return err
    44  			}
    45  
    46  			fmt.Fprint(w, ")")
    47  		}
    48  	}
    49  
    50  	return nil
    51  }