github.com/yongjacky/phoenix-go-orm-builder@v0.3.5/cond_not.go (about) 1 // Copyright 2016 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 "fmt" 8 9 // Not defines NOT condition 10 type Not [1]Cond 11 12 var _ Cond = Not{} 13 14 // WriteTo writes SQL to Writer 15 func (not Not) WriteTo(w Writer) error { 16 if _, err := fmt.Fprint(w, "NOT "); err != nil { 17 return err 18 } 19 switch not[0].(type) { 20 case condAnd, condOr: 21 if _, err := fmt.Fprint(w, "("); err != nil { 22 return err 23 } 24 case Eq: 25 if len(not[0].(Eq)) > 1 { 26 if _, err := fmt.Fprint(w, "("); err != nil { 27 return err 28 } 29 } 30 case Neq: 31 if len(not[0].(Neq)) > 1 { 32 if _, err := fmt.Fprint(w, "("); err != nil { 33 return err 34 } 35 } 36 } 37 38 if err := not[0].WriteTo(w); err != nil { 39 return err 40 } 41 42 switch not[0].(type) { 43 case condAnd, condOr: 44 if _, err := fmt.Fprint(w, ")"); err != nil { 45 return err 46 } 47 case Eq: 48 if len(not[0].(Eq)) > 1 { 49 if _, err := fmt.Fprint(w, ")"); err != nil { 50 return err 51 } 52 } 53 case Neq: 54 if len(not[0].(Neq)) > 1 { 55 if _, err := fmt.Fprint(w, ")"); err != nil { 56 return err 57 } 58 } 59 } 60 61 return nil 62 } 63 64 // And implements And with other conditions 65 func (not Not) And(conds ...Cond) Cond { 66 return And(not, And(conds...)) 67 } 68 69 // Or implements Or with other conditions 70 func (not Not) Or(conds ...Cond) Cond { 71 return Or(not, Or(conds...)) 72 } 73 74 // IsValid tests if this condition is valid 75 func (not Not) IsValid() bool { 76 return not[0] != nil && not[0].IsValid() 77 }