github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/go-xorm/builder/cond_notin.go (about)

     1  package builder
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  )
     7  
     8  type condNotIn condIn
     9  
    10  var _ Cond = condNotIn{}
    11  
    12  func NotIn(col string, values ...interface{}) Cond {
    13  	return condNotIn{col, values}
    14  }
    15  
    16  func (condNotIn condNotIn) WriteTo(w Writer) error {
    17  	if len(condNotIn.vals) <= 0 {
    18  		return ErrNoNotInConditions
    19  	}
    20  
    21  	switch condNotIn.vals[0].(type) {
    22  	case []int8:
    23  		vals := condNotIn.vals[0].([]int8)
    24  		if len(vals) <= 0 {
    25  			return ErrNoNotInConditions
    26  		}
    27  		questionMark := strings.Repeat("?,", len(vals))
    28  		if _, err := fmt.Fprintf(w, "%s NOT IN (%s)", condNotIn.col, questionMark[:len(questionMark)-1]); err != nil {
    29  			return err
    30  		}
    31  		for _, val := range vals {
    32  			w.Append(val)
    33  		}
    34  	case []int16:
    35  		vals := condNotIn.vals[0].([]int16)
    36  		if len(vals) <= 0 {
    37  			return ErrNoNotInConditions
    38  		}
    39  		questionMark := strings.Repeat("?,", len(vals))
    40  		if _, err := fmt.Fprintf(w, "%s NOT IN (%s)", condNotIn.col, questionMark[:len(questionMark)-1]); err != nil {
    41  			return err
    42  		}
    43  		for _, val := range vals {
    44  			w.Append(val)
    45  		}
    46  	case []int:
    47  		vals := condNotIn.vals[0].([]int)
    48  		if len(vals) <= 0 {
    49  			return ErrNoNotInConditions
    50  		}
    51  		questionMark := strings.Repeat("?,", len(vals))
    52  		if _, err := fmt.Fprintf(w, "%s NOT IN (%s)", condNotIn.col, questionMark[:len(questionMark)-1]); err != nil {
    53  			return err
    54  		}
    55  		for _, val := range vals {
    56  			w.Append(val)
    57  		}
    58  	case []int32:
    59  		vals := condNotIn.vals[0].([]int32)
    60  		if len(vals) <= 0 {
    61  			return ErrNoNotInConditions
    62  		}
    63  		questionMark := strings.Repeat("?,", len(vals))
    64  		if _, err := fmt.Fprintf(w, "%s NOT IN (%s)", condNotIn.col, questionMark[:len(questionMark)-1]); err != nil {
    65  			return err
    66  		}
    67  		for _, val := range vals {
    68  			w.Append(val)
    69  		}
    70  	case []int64:
    71  		vals := condNotIn.vals[0].([]int64)
    72  		if len(vals) <= 0 {
    73  			return ErrNoNotInConditions
    74  		}
    75  		questionMark := strings.Repeat("?,", len(vals))
    76  		if _, err := fmt.Fprintf(w, "%s NOT IN (%s)", condNotIn.col, questionMark[:len(questionMark)-1]); err != nil {
    77  			return err
    78  		}
    79  		for _, val := range vals {
    80  			w.Append(val)
    81  		}
    82  	case []uint8:
    83  		vals := condNotIn.vals[0].([]uint8)
    84  		if len(vals) <= 0 {
    85  			return ErrNoNotInConditions
    86  		}
    87  		questionMark := strings.Repeat("?,", len(vals))
    88  		if _, err := fmt.Fprintf(w, "%s NOT IN (%s)", condNotIn.col, questionMark[:len(questionMark)-1]); err != nil {
    89  			return err
    90  		}
    91  		for _, val := range vals {
    92  			w.Append(val)
    93  		}
    94  	case []uint16:
    95  		vals := condNotIn.vals[0].([]uint16)
    96  		if len(vals) <= 0 {
    97  			return ErrNoNotInConditions
    98  		}
    99  		questionMark := strings.Repeat("?,", len(vals))
   100  		if _, err := fmt.Fprintf(w, "%s NOT IN (%s)", condNotIn.col, questionMark[:len(questionMark)-1]); err != nil {
   101  			return err
   102  		}
   103  		for _, val := range vals {
   104  			w.Append(val)
   105  		}
   106  	case []uint:
   107  		vals := condNotIn.vals[0].([]uint)
   108  		if len(vals) <= 0 {
   109  			return ErrNoNotInConditions
   110  		}
   111  		questionMark := strings.Repeat("?,", len(vals))
   112  		if _, err := fmt.Fprintf(w, "%s NOT IN (%s)", condNotIn.col, questionMark[:len(questionMark)-1]); err != nil {
   113  			return err
   114  		}
   115  		for _, val := range vals {
   116  			w.Append(val)
   117  		}
   118  	case []uint32:
   119  		vals := condNotIn.vals[0].([]uint32)
   120  		if len(vals) <= 0 {
   121  			return ErrNoNotInConditions
   122  		}
   123  		questionMark := strings.Repeat("?,", len(vals))
   124  		if _, err := fmt.Fprintf(w, "%s NOT IN (%s)", condNotIn.col, questionMark[:len(questionMark)-1]); err != nil {
   125  			return err
   126  		}
   127  		for _, val := range vals {
   128  			w.Append(val)
   129  		}
   130  	case []uint64:
   131  		vals := condNotIn.vals[0].([]uint64)
   132  		if len(vals) <= 0 {
   133  			return ErrNoNotInConditions
   134  		}
   135  		questionMark := strings.Repeat("?,", len(vals))
   136  		if _, err := fmt.Fprintf(w, "%s NOT IN (%s)", condNotIn.col, questionMark[:len(questionMark)-1]); err != nil {
   137  			return err
   138  		}
   139  		for _, val := range vals {
   140  			w.Append(val)
   141  		}
   142  	case []string:
   143  		vals := condNotIn.vals[0].([]string)
   144  		if len(vals) <= 0 {
   145  			return ErrNoNotInConditions
   146  		}
   147  		questionMark := strings.Repeat("?,", len(vals))
   148  		if _, err := fmt.Fprintf(w, "%s NOT IN (%s)", condNotIn.col, questionMark[:len(questionMark)-1]); err != nil {
   149  			return err
   150  		}
   151  		for _, val := range vals {
   152  			w.Append(val)
   153  		}
   154  	case []interface{}:
   155  		vals := condNotIn.vals[0].([]interface{})
   156  		if len(vals) <= 0 {
   157  			return ErrNoNotInConditions
   158  		}
   159  		questionMark := strings.Repeat("?,", len(vals))
   160  		if _, err := fmt.Fprintf(w, "%s NOT IN (%s)", condNotIn.col, questionMark[:len(questionMark)-1]); err != nil {
   161  			return err
   162  		}
   163  		w.Append(vals...)
   164  	case expr:
   165  		val := condNotIn.vals[0].(expr)
   166  		if _, err := fmt.Fprintf(w, "%s NOT IN (", condNotIn.col); err != nil {
   167  			return err
   168  		}
   169  		if err := val.WriteTo(w); err != nil {
   170  			return err
   171  		}
   172  		if _, err := fmt.Fprintf(w, ")"); err != nil {
   173  			return err
   174  		}
   175  	case *Builder:
   176  		val := condNotIn.vals[0].(*Builder)
   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  	default:
   187  		questionMark := strings.Repeat("?,", len(condNotIn.vals))
   188  		if _, err := fmt.Fprintf(w, "%s NOT IN (%s)", condNotIn.col, questionMark[:len(questionMark)-1]); err != nil {
   189  			return err
   190  		}
   191  		w.Append(condNotIn.vals...)
   192  	}
   193  	return nil
   194  }
   195  
   196  func (condNotIn condNotIn) And(conds ...Cond) Cond {
   197  	return And(condNotIn, And(conds...))
   198  }
   199  
   200  func (condNotIn condNotIn) Or(conds ...Cond) Cond {
   201  	return Or(condNotIn, Or(conds...))
   202  }
   203  
   204  func (condNotIn condNotIn) IsValid() bool {
   205  	return len(condNotIn.col) > 0 && len(condNotIn.vals) > 0
   206  }