github.com/artisanhe/tools@v1.0.1-0.20210607022958-19a8fef2eb04/sqlx/builder/stmt_common.go (about)

     1  package builder
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  )
     7  
     8  type StmtType int16
     9  
    10  const (
    11  	STMT_UNKNOWN StmtType = iota
    12  	STMT_INSERT
    13  	STMT_DELETE
    14  	STMT_UPDATE
    15  	STMT_SELECT
    16  	STMT_RAW
    17  )
    18  
    19  type Stmt Expression
    20  
    21  func (s *Stmt) Type() StmtType {
    22  	return STMT_RAW
    23  }
    24  
    25  func (s *Stmt) Expr() *Expression {
    26  	return (*Expression)(s)
    27  }
    28  
    29  type Statement interface {
    30  	CanExpr
    31  	Type() StmtType
    32  }
    33  
    34  func comment(c string) string {
    35  	if c == "" {
    36  		return ""
    37  	}
    38  	return fmt.Sprintf("/* %s */ ", c)
    39  }
    40  
    41  func statement(c string, tpe string, modifiers ...string) string {
    42  	if modifiers == nil {
    43  		return comment(c) + tpe
    44  	}
    45  	return comment(c) + tpe + " " + strings.Join(modifiers, " ")
    46  }
    47  
    48  type where Condition
    49  
    50  func (w *where) Expr() *Expression {
    51  	if w == nil || w.Query == "" {
    52  		return nil
    53  	}
    54  	return Expr("WHERE "+w.Query, w.Args...)
    55  }
    56  
    57  type OrderType string
    58  
    59  const (
    60  	ORDER_NO   OrderType = ""
    61  	ORDER_ASC  OrderType = "ASC"
    62  	ORDER_DESC OrderType = "DESC"
    63  )
    64  
    65  type order struct {
    66  	expr CanExpr
    67  	tpe  OrderType
    68  }
    69  
    70  func (o order) Expr() *Expression {
    71  	if o.expr == nil {
    72  		return nil
    73  	}
    74  	expr := o.expr.Expr()
    75  	if expr == nil {
    76  		return nil
    77  	}
    78  	suffix := ""
    79  	if o.tpe != "" {
    80  		suffix = " " + string(o.tpe)
    81  	}
    82  	return Expr(
    83  		fmt.Sprintf("(%s)%s", expr.Query, suffix),
    84  		expr.Args...,
    85  	)
    86  }
    87  
    88  type groupBy struct {
    89  	groups     []order
    90  	withRollup bool
    91  	havingCond *Condition
    92  }
    93  
    94  func (g groupBy) setBy(expr CanExpr, orderType OrderType) *groupBy {
    95  	g.groups = []order{{
    96  		expr: expr,
    97  		tpe:  orderType,
    98  	}}
    99  	return &g
   100  }
   101  
   102  func (g groupBy) addBy(expr CanExpr, orderType OrderType) *groupBy {
   103  	g.groups = append(g.groups, order{
   104  		expr: expr,
   105  		tpe:  orderType,
   106  	})
   107  	return &g
   108  }
   109  
   110  func (g groupBy) rollup() *groupBy {
   111  	g.withRollup = true
   112  	return &g
   113  }
   114  
   115  func (g groupBy) having(cond *Condition) *groupBy {
   116  	g.havingCond = cond
   117  	return &g
   118  }
   119  
   120  func (g *groupBy) Expr() *Expression {
   121  	if g == nil {
   122  		return nil
   123  	}
   124  	if len(g.groups) > 0 {
   125  		expr := Expr("GROUP BY")
   126  		for i, order := range g.groups {
   127  			if i == 0 {
   128  				expr = expr.ConcatBy(" ", order)
   129  			} else {
   130  				expr = expr.ConcatBy(", ", order)
   131  			}
   132  		}
   133  
   134  		if g.withRollup {
   135  			expr = expr.ConcatBy(" ", Expr("WITH ROLLUP"))
   136  		}
   137  
   138  		expr = expr.ConcatBy(" HAVING ", g.havingCond)
   139  		return expr
   140  	}
   141  	return nil
   142  }
   143  
   144  type orderBy struct {
   145  	orders []order
   146  }
   147  
   148  func (o orderBy) setBy(expr CanExpr, orderType OrderType) *orderBy {
   149  	o.orders = []order{{
   150  		expr: expr,
   151  		tpe:  orderType,
   152  	}}
   153  	return &o
   154  }
   155  
   156  func (o orderBy) addBy(expr CanExpr, orderType OrderType) *orderBy {
   157  	o.orders = append(o.orders, order{
   158  		expr: expr,
   159  		tpe:  orderType,
   160  	})
   161  	return &o
   162  }
   163  
   164  func (o *orderBy) Expr() *Expression {
   165  	if o == nil {
   166  		return nil
   167  	}
   168  	if len(o.orders) > 0 {
   169  		expr := Expr("ORDER BY")
   170  		for i, order := range o.orders {
   171  			if i == 0 {
   172  				expr = expr.ConcatBy(" ", order)
   173  			} else {
   174  				expr = expr.ConcatBy(", ", order)
   175  			}
   176  		}
   177  		return expr
   178  	}
   179  	return nil
   180  }
   181  
   182  type limit struct {
   183  	// limit
   184  	rowCount int32
   185  	// offset
   186  	offsetCount int32
   187  }
   188  
   189  func (l limit) limit(size int32) *limit {
   190  	l.rowCount = size
   191  	return &l
   192  }
   193  
   194  func (l limit) offset(offset int32) *limit {
   195  	l.offsetCount = offset
   196  	return &l
   197  }
   198  
   199  func (l *limit) Expr() *Expression {
   200  	if l == nil {
   201  		return nil
   202  	}
   203  	if l.rowCount > 0 {
   204  		if l.offsetCount > 0 {
   205  			return Expr(fmt.Sprintf("LIMIT %d OFFSET %d", l.rowCount, l.offsetCount))
   206  		}
   207  		return Expr(fmt.Sprintf(fmt.Sprintf("LIMIT %d", l.rowCount)))
   208  	}
   209  	return nil
   210  }