gitee.com/eden-framework/sqlx@v0.0.3/builder/functions.go (about)

     1  package builder
     2  
     3  import (
     4  	"context"
     5  )
     6  
     7  func Count(sqlExprs ...SqlExpr) *Function {
     8  	if len(sqlExprs) == 0 {
     9  		return Func("COUNT", Expr("1"))
    10  	}
    11  	return Func("COUNT", sqlExprs...)
    12  }
    13  
    14  func Avg(sqlExprs ...SqlExpr) *Function {
    15  	return Func("AVG", sqlExprs...)
    16  }
    17  
    18  func Distinct(sqlExprs ...SqlExpr) *Function {
    19  	return Func("DISTINCT", sqlExprs...)
    20  }
    21  
    22  func Min(sqlExprs ...SqlExpr) *Function {
    23  	return Func("MIN", sqlExprs...)
    24  }
    25  
    26  func Max(sqlExprs ...SqlExpr) *Function {
    27  	return Func("MAX", sqlExprs...)
    28  }
    29  
    30  func First(sqlExprs ...SqlExpr) *Function {
    31  	return Func("FIRST", sqlExprs...)
    32  }
    33  
    34  func Last(sqlExprs ...SqlExpr) *Function {
    35  	return Func("LAST", sqlExprs...)
    36  }
    37  
    38  func Sum(sqlExprs ...SqlExpr) *Function {
    39  	return Func("SUM", sqlExprs...)
    40  }
    41  
    42  func Func(name string, sqlExprs ...SqlExpr) *Function {
    43  	if name == "" {
    44  		return nil
    45  	}
    46  	return &Function{
    47  		name:  name,
    48  		exprs: sqlExprs,
    49  	}
    50  }
    51  
    52  type Function struct {
    53  	name  string
    54  	exprs []SqlExpr
    55  }
    56  
    57  func (f *Function) IsNil() bool {
    58  	return f == nil || f.name == ""
    59  }
    60  
    61  func (f *Function) Ex(ctx context.Context) *Ex {
    62  	e := Expr(f.name)
    63  
    64  	e.WriteGroup(func(e *Ex) {
    65  		if len(f.exprs) == 0 {
    66  			e.WriteByte('*')
    67  		}
    68  
    69  		for i := range f.exprs {
    70  			if i > 0 {
    71  				e.WriteByte(',')
    72  			}
    73  			e.WriteExpr(f.exprs[i])
    74  		}
    75  	})
    76  
    77  	return e.Ex(ctx)
    78  }