github.com/RevenueMonster/sqlike@v1.0.6/sql/expr/common.go (about)

     1  package expr
     2  
     3  import (
     4  	"database/sql"
     5  	"strings"
     6  
     7  	"github.com/RevenueMonster/sqlike/sqlike/primitive"
     8  )
     9  
    10  // Raw :
    11  func Raw(value string) (r primitive.Raw) {
    12  	r.Value = value
    13  	return
    14  }
    15  
    16  // As :
    17  func As(field interface{}, alias string) (as primitive.As) {
    18  	as.Field = wrapColumn(field)
    19  	as.Name = alias
    20  	return
    21  }
    22  
    23  // Column :
    24  func Column(name string, alias ...string) (c primitive.Column) {
    25  	if len(alias) > 0 {
    26  		c.Table = name
    27  		c.Name = alias[0]
    28  		return
    29  	}
    30  	c.Name = name
    31  	return
    32  }
    33  
    34  // Func :
    35  func Func(name string, value interface{}, others ...interface{}) (f primitive.Func) {
    36  	f.Name = strings.ToUpper(strings.TrimSpace(name))
    37  	f.Args = append(f.Args, wrapRaw(value))
    38  	if len(others) > 0 {
    39  		for _, arg := range others {
    40  			f.Args = append(f.Args, wrapRaw(arg))
    41  		}
    42  	}
    43  	return
    44  }
    45  
    46  func wrapRaw(v interface{}) (it interface{}) {
    47  	vv := primitive.Value{}
    48  	switch vi := v.(type) {
    49  	case sql.RawBytes:
    50  		vv.Raw = vi
    51  		return vv
    52  	case nil:
    53  		vv.Raw = vi
    54  		return vv
    55  	case string:
    56  		vv.Raw = vi
    57  		return vv
    58  	case []byte:
    59  		vv.Raw = vi
    60  		return vv
    61  	case float32, float64:
    62  		vv.Raw = vi
    63  		return vv
    64  	case int, int8, int16, int32, int64:
    65  		vv.Raw = vi
    66  		return vv
    67  	case uint, uint8, uint16, uint32, uint64:
    68  		vv.Raw = vi
    69  		return vv
    70  	default:
    71  		return v
    72  	}
    73  }