github.com/yongjacky/phoenix-go-orm-builder@v0.3.5/cond_notin.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 (
     8  	"fmt"
     9  	"reflect"
    10  	"strings"
    11  )
    12  
    13  type condNotIn condIn
    14  
    15  var _ Cond = condNotIn{}
    16  
    17  // NotIn generate NOT IN condition
    18  func NotIn(col string, values ...interface{}) Cond {
    19  	return condNotIn{col, values}
    20  }
    21  
    22  func (condNotIn condNotIn) handleBlank(w Writer) error {
    23  	_, err := fmt.Fprint(w, "0=0")
    24  	return err
    25  }
    26  
    27  func (condNotIn condNotIn) WriteTo(w Writer) error {
    28  	if len(condNotIn.vals) <= 0 {
    29  		return condNotIn.handleBlank(w)
    30  	}
    31  
    32  	switch condNotIn.vals[0].(type) {
    33  	case []int8:
    34  		vals := condNotIn.vals[0].([]int8)
    35  		if len(vals) <= 0 {
    36  			return condNotIn.handleBlank(w)
    37  		}
    38  		questionMark := strings.Repeat("?,", len(vals))
    39  		if _, err := fmt.Fprintf(w, "%s NOT IN (%s)", condNotIn.col, questionMark[:len(questionMark)-1]); err != nil {
    40  			return err
    41  		}
    42  		for _, val := range vals {
    43  			w.Append(val)
    44  		}
    45  	case []int16:
    46  		vals := condNotIn.vals[0].([]int16)
    47  		if len(vals) <= 0 {
    48  			return condNotIn.handleBlank(w)
    49  		}
    50  		questionMark := strings.Repeat("?,", len(vals))
    51  		if _, err := fmt.Fprintf(w, "%s NOT IN (%s)", condNotIn.col, questionMark[:len(questionMark)-1]); err != nil {
    52  			return err
    53  		}
    54  		for _, val := range vals {
    55  			w.Append(val)
    56  		}
    57  	case []int:
    58  		vals := condNotIn.vals[0].([]int)
    59  		if len(vals) <= 0 {
    60  			return condNotIn.handleBlank(w)
    61  		}
    62  		questionMark := strings.Repeat("?,", len(vals))
    63  		if _, err := fmt.Fprintf(w, "%s NOT IN (%s)", condNotIn.col, questionMark[:len(questionMark)-1]); err != nil {
    64  			return err
    65  		}
    66  		for _, val := range vals {
    67  			w.Append(val)
    68  		}
    69  	case []int32:
    70  		vals := condNotIn.vals[0].([]int32)
    71  		if len(vals) <= 0 {
    72  			return condNotIn.handleBlank(w)
    73  		}
    74  		questionMark := strings.Repeat("?,", len(vals))
    75  		if _, err := fmt.Fprintf(w, "%s NOT IN (%s)", condNotIn.col, questionMark[:len(questionMark)-1]); err != nil {
    76  			return err
    77  		}
    78  		for _, val := range vals {
    79  			w.Append(val)
    80  		}
    81  	case []int64:
    82  		vals := condNotIn.vals[0].([]int64)
    83  		if len(vals) <= 0 {
    84  			return condNotIn.handleBlank(w)
    85  		}
    86  		questionMark := strings.Repeat("?,", len(vals))
    87  		if _, err := fmt.Fprintf(w, "%s NOT IN (%s)", condNotIn.col, questionMark[:len(questionMark)-1]); err != nil {
    88  			return err
    89  		}
    90  		for _, val := range vals {
    91  			w.Append(val)
    92  		}
    93  	case []uint8:
    94  		vals := condNotIn.vals[0].([]uint8)
    95  		if len(vals) <= 0 {
    96  			return condNotIn.handleBlank(w)
    97  		}
    98  		questionMark := strings.Repeat("?,", len(vals))
    99  		if _, err := fmt.Fprintf(w, "%s NOT IN (%s)", condNotIn.col, questionMark[:len(questionMark)-1]); err != nil {
   100  			return err
   101  		}
   102  		for _, val := range vals {
   103  			w.Append(val)
   104  		}
   105  	case []uint16:
   106  		vals := condNotIn.vals[0].([]uint16)
   107  		if len(vals) <= 0 {
   108  			return condNotIn.handleBlank(w)
   109  		}
   110  		questionMark := strings.Repeat("?,", len(vals))
   111  		if _, err := fmt.Fprintf(w, "%s NOT IN (%s)", condNotIn.col, questionMark[:len(questionMark)-1]); err != nil {
   112  			return err
   113  		}
   114  		for _, val := range vals {
   115  			w.Append(val)
   116  		}
   117  	case []uint:
   118  		vals := condNotIn.vals[0].([]uint)
   119  		if len(vals) <= 0 {
   120  			return condNotIn.handleBlank(w)
   121  		}
   122  		questionMark := strings.Repeat("?,", len(vals))
   123  		if _, err := fmt.Fprintf(w, "%s NOT IN (%s)", condNotIn.col, questionMark[:len(questionMark)-1]); err != nil {
   124  			return err
   125  		}
   126  		for _, val := range vals {
   127  			w.Append(val)
   128  		}
   129  	case []uint32:
   130  		vals := condNotIn.vals[0].([]uint32)
   131  		if len(vals) <= 0 {
   132  			return condNotIn.handleBlank(w)
   133  		}
   134  		questionMark := strings.Repeat("?,", len(vals))
   135  		if _, err := fmt.Fprintf(w, "%s NOT IN (%s)", condNotIn.col, questionMark[:len(questionMark)-1]); err != nil {
   136  			return err
   137  		}
   138  		for _, val := range vals {
   139  			w.Append(val)
   140  		}
   141  	case []uint64:
   142  		vals := condNotIn.vals[0].([]uint64)
   143  		if len(vals) <= 0 {
   144  			return condNotIn.handleBlank(w)
   145  		}
   146  		questionMark := strings.Repeat("?,", len(vals))
   147  		if _, err := fmt.Fprintf(w, "%s NOT IN (%s)", condNotIn.col, questionMark[:len(questionMark)-1]); err != nil {
   148  			return err
   149  		}
   150  		for _, val := range vals {
   151  			w.Append(val)
   152  		}
   153  	case []string:
   154  		vals := condNotIn.vals[0].([]string)
   155  		if len(vals) <= 0 {
   156  			return condNotIn.handleBlank(w)
   157  		}
   158  		questionMark := strings.Repeat("?,", len(vals))
   159  		if _, err := fmt.Fprintf(w, "%s NOT IN (%s)", condNotIn.col, questionMark[:len(questionMark)-1]); err != nil {
   160  			return err
   161  		}
   162  		for _, val := range vals {
   163  			w.Append(val)
   164  		}
   165  	case []interface{}:
   166  		vals := condNotIn.vals[0].([]interface{})
   167  		if len(vals) <= 0 {
   168  			return condNotIn.handleBlank(w)
   169  		}
   170  		questionMark := strings.Repeat("?,", len(vals))
   171  		if _, err := fmt.Fprintf(w, "%s NOT IN (%s)", condNotIn.col, questionMark[:len(questionMark)-1]); err != nil {
   172  			return err
   173  		}
   174  		w.Append(vals...)
   175  	case expr:
   176  		val := condNotIn.vals[0].(expr)
   177  		if _, err := fmt.Fprintf(w, "%s NOT IN (", condNotIn.col); err != nil {
   178  			return err
   179  		}
   180  		if err := val.WriteTo(w); err != nil {
   181  			return err
   182  		}
   183  		if _, err := fmt.Fprintf(w, ")"); err != nil {
   184  			return err
   185  		}
   186  	case *Builder:
   187  		val := condNotIn.vals[0].(*Builder)
   188  		if _, err := fmt.Fprintf(w, "%s NOT IN (", condNotIn.col); err != nil {
   189  			return err
   190  		}
   191  		if err := val.WriteTo(w); err != nil {
   192  			return err
   193  		}
   194  		if _, err := fmt.Fprintf(w, ")"); err != nil {
   195  			return err
   196  		}
   197  	default:
   198  		v := reflect.ValueOf(condNotIn.vals[0])
   199  		if v.Kind() == reflect.Slice {
   200  			l := v.Len()
   201  			if l == 0 {
   202  				return condNotIn.handleBlank(w)
   203  			}
   204  
   205  			questionMark := strings.Repeat("?,", l)
   206  			if _, err := fmt.Fprintf(w, "%s NOT IN (%s)", condNotIn.col, questionMark[:len(questionMark)-1]); err != nil {
   207  				return err
   208  			}
   209  
   210  			for i := 0; i < l; i++ {
   211  				w.Append(v.Index(i).Interface())
   212  			}
   213  		} else {
   214  			questionMark := strings.Repeat("?,", len(condNotIn.vals))
   215  			if _, err := fmt.Fprintf(w, "%s NOT IN (%s)", condNotIn.col, questionMark[:len(questionMark)-1]); err != nil {
   216  				return err
   217  			}
   218  			w.Append(condNotIn.vals...)
   219  		}
   220  	}
   221  	return nil
   222  }
   223  
   224  func (condNotIn condNotIn) And(conds ...Cond) Cond {
   225  	return And(condNotIn, And(conds...))
   226  }
   227  
   228  func (condNotIn condNotIn) Or(conds ...Cond) Cond {
   229  	return Or(condNotIn, Or(conds...))
   230  }
   231  
   232  func (condNotIn condNotIn) IsValid() bool {
   233  	return len(condNotIn.col) > 0 && len(condNotIn.vals) > 0
   234  }