github.com/yongjacky/phoenix-go-orm-builder@v0.3.5/cond_compare.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  // WriteMap writes conditions' SQL to Writer, op could be =, <>, >, <, <=, >= and etc.
    10  func WriteMap(w Writer, data map[string]interface{}, op string) error {
    11  	var args = make([]interface{}, 0, len(data))
    12  	var i = 0
    13  	keys := make([]string, 0, len(data))
    14  	for k := range data {
    15  		keys = append(keys, k)
    16  	}
    17  
    18  	for _, k := range keys {
    19  		v := data[k]
    20  		switch v.(type) {
    21  		case expr:
    22  			if _, err := fmt.Fprintf(w, "%s%s(", k, op); err != nil {
    23  				return err
    24  			}
    25  
    26  			if err := v.(expr).WriteTo(w); err != nil {
    27  				return err
    28  			}
    29  
    30  			if _, err := fmt.Fprintf(w, ")"); err != nil {
    31  				return err
    32  			}
    33  		case *Builder:
    34  			if _, err := fmt.Fprintf(w, "%s%s(", k, op); err != nil {
    35  				return err
    36  			}
    37  
    38  			if err := v.(*Builder).WriteTo(w); err != nil {
    39  				return err
    40  			}
    41  
    42  			if _, err := fmt.Fprintf(w, ")"); err != nil {
    43  				return err
    44  			}
    45  		default:
    46  			if _, err := fmt.Fprintf(w, "%s%s?", k, op); err != nil {
    47  				return err
    48  			}
    49  			args = append(args, v)
    50  		}
    51  		if i != len(data)-1 {
    52  			if _, err := fmt.Fprint(w, " AND "); err != nil {
    53  				return err
    54  			}
    55  		}
    56  		i = i + 1
    57  	}
    58  	w.Append(args...)
    59  	return nil
    60  }
    61  
    62  // Lt defines < condition
    63  type Lt map[string]interface{}
    64  
    65  var _ Cond = Lt{}
    66  
    67  // WriteTo write SQL to Writer
    68  func (lt Lt) WriteTo(w Writer) error {
    69  	return WriteMap(w, lt, "<")
    70  }
    71  
    72  // And implements And with other conditions
    73  func (lt Lt) And(conds ...Cond) Cond {
    74  	return condAnd{lt, And(conds...)}
    75  }
    76  
    77  // Or implements Or with other conditions
    78  func (lt Lt) Or(conds ...Cond) Cond {
    79  	return condOr{lt, Or(conds...)}
    80  }
    81  
    82  // IsValid tests if this Eq is valid
    83  func (lt Lt) IsValid() bool {
    84  	return len(lt) > 0
    85  }
    86  
    87  // Lte defines <= condition
    88  type Lte map[string]interface{}
    89  
    90  var _ Cond = Lte{}
    91  
    92  // WriteTo write SQL to Writer
    93  func (lte Lte) WriteTo(w Writer) error {
    94  	return WriteMap(w, lte, "<=")
    95  }
    96  
    97  // And implements And with other conditions
    98  func (lte Lte) And(conds ...Cond) Cond {
    99  	return And(lte, And(conds...))
   100  }
   101  
   102  // Or implements Or with other conditions
   103  func (lte Lte) Or(conds ...Cond) Cond {
   104  	return Or(lte, Or(conds...))
   105  }
   106  
   107  // IsValid tests if this Eq is valid
   108  func (lte Lte) IsValid() bool {
   109  	return len(lte) > 0
   110  }
   111  
   112  // Gt defines > condition
   113  type Gt map[string]interface{}
   114  
   115  var _ Cond = Gt{}
   116  
   117  // WriteTo write SQL to Writer
   118  func (gt Gt) WriteTo(w Writer) error {
   119  	return WriteMap(w, gt, ">")
   120  }
   121  
   122  // And implements And with other conditions
   123  func (gt Gt) And(conds ...Cond) Cond {
   124  	return And(gt, And(conds...))
   125  }
   126  
   127  // Or implements Or with other conditions
   128  func (gt Gt) Or(conds ...Cond) Cond {
   129  	return Or(gt, Or(conds...))
   130  }
   131  
   132  // IsValid tests if this Eq is valid
   133  func (gt Gt) IsValid() bool {
   134  	return len(gt) > 0
   135  }
   136  
   137  // Gte defines >= condition
   138  type Gte map[string]interface{}
   139  
   140  var _ Cond = Gte{}
   141  
   142  // WriteTo write SQL to Writer
   143  func (gte Gte) WriteTo(w Writer) error {
   144  	return WriteMap(w, gte, ">=")
   145  }
   146  
   147  // And implements And with other conditions
   148  func (gte Gte) And(conds ...Cond) Cond {
   149  	return And(gte, And(conds...))
   150  }
   151  
   152  // Or implements Or with other conditions
   153  func (gte Gte) Or(conds ...Cond) Cond {
   154  	return Or(gte, Or(conds...))
   155  }
   156  
   157  // IsValid tests if this Eq is valid
   158  func (gte Gte) IsValid() bool {
   159  	return len(gte) > 0
   160  }