github.com/artisanhe/tools@v1.0.1-0.20210607022958-19a8fef2eb04/sqlx/builder/stmt_delete.go (about) 1 package builder 2 3 import ( 4 "fmt" 5 ) 6 7 var ( 8 DeleteNeedLimitByWhere = fmt.Errorf("no where limit for delete") 9 ) 10 11 func Delete(table *Table) *StmtDelete { 12 return &StmtDelete{ 13 table: table, 14 } 15 } 16 17 type StmtDelete struct { 18 table *Table 19 modifiers []string 20 *where 21 *orderBy 22 *limit 23 comment string 24 } 25 26 func (s StmtDelete) Comment(comment string) *StmtDelete { 27 s.comment = comment 28 return &s 29 } 30 31 func (s *StmtDelete) Type() StmtType { 32 return STMT_DELETE 33 } 34 35 func (s StmtDelete) Modifier(modifiers ...string) *StmtDelete { 36 s.modifiers = append(s.modifiers, modifiers...) 37 return &s 38 } 39 40 func (s StmtDelete) Where(cond *Condition) *StmtDelete { 41 s.where = (*where)(cond) 42 return &s 43 } 44 45 func (s StmtDelete) OrderBy(col *Column, orderType OrderType) *StmtDelete { 46 if s.orderBy == nil { 47 s.orderBy = &orderBy{} 48 } 49 s.orderBy = s.orderBy.setBy(col, orderType) 50 return &s 51 } 52 53 func (s StmtDelete) AscendBy(col *Column) *StmtDelete { 54 return s.OrderBy(col, ORDER_ASC) 55 } 56 57 func (s StmtDelete) DescendBy(col *Column) *StmtDelete { 58 return s.OrderBy(col, ORDER_DESC) 59 } 60 61 func (s StmtDelete) Limit(size int32) *StmtDelete { 62 if s.limit == nil { 63 s.limit = &limit{} 64 } 65 s.limit = s.limit.limit(size) 66 return &s 67 } 68 69 func (s *StmtDelete) Expr() *Expression { 70 if s.where == nil { 71 return ExprErr(DeleteNeedLimitByWhere) 72 } 73 74 expr := Expr(fmt.Sprintf("%s FROM %s", statement(s.comment, "DELETE", s.modifiers...), s.table.FullName())) 75 76 expr = expr.ConcatBy(" ", s.where) 77 expr = expr.ConcatBy(" ", s.orderBy) 78 expr = expr.ConcatBy(" ", s.limit) 79 80 return expr 81 }