github.com/yongjacky/phoenix-go-orm-builder@v0.3.5/cond_like.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  // Like defines like condition
    10  type Like [2]string
    11  
    12  var _ Cond = Like{"", ""}
    13  
    14  // WriteTo write SQL to Writer
    15  func (like Like) WriteTo(w Writer) error {
    16  	if _, err := fmt.Fprintf(w, "%s LIKE ?", like[0]); err != nil {
    17  		return err
    18  	}
    19  	// FIXME: if use other regular express, this will be failed. but for compatible, keep this
    20  	if like[1][0] == '%' || like[1][len(like[1])-1] == '%' {
    21  		w.Append(like[1])
    22  	} else {
    23  		w.Append("%" + like[1] + "%")
    24  	}
    25  	return nil
    26  }
    27  
    28  // And implements And with other conditions
    29  func (like Like) And(conds ...Cond) Cond {
    30  	return And(like, And(conds...))
    31  }
    32  
    33  // Or implements Or with other conditions
    34  func (like Like) Or(conds ...Cond) Cond {
    35  	return Or(like, Or(conds...))
    36  }
    37  
    38  // IsValid tests if this condition is valid
    39  func (like Like) IsValid() bool {
    40  	return len(like[0]) > 0 && len(like[1]) > 0
    41  }