github.com/unionj-cloud/go-doudou/v2@v2.3.5/toolkit/gormgen/field/asterisk.go (about) 1 package field 2 3 import ( 4 "gorm.io/gorm" 5 "gorm.io/gorm/clause" 6 ) 7 8 // Asterisk a type of xxx.* 9 type Asterisk struct{ asteriskExpr } 10 11 // Count count 12 func (a Asterisk) Count() Asterisk { 13 var expr *clause.Expr 14 switch { 15 case a.e != nil: 16 expr = &clause.Expr{ 17 SQL: "COUNT(?)", 18 Vars: []interface{}{a.e}, 19 } 20 case a.col.Table == "": 21 expr = &clause.Expr{SQL: "COUNT(*)"} 22 default: 23 expr = &clause.Expr{ 24 SQL: "COUNT(?.*)", 25 Vars: []interface{}{clause.Table{Name: a.col.Table}}, 26 } 27 } 28 return Asterisk{asteriskExpr{expr: a.setE(expr)}} 29 } 30 31 // Distinct distinct 32 func (a Asterisk) Distinct() Asterisk { 33 var expr *clause.Expr 34 if a.col.Table == "" { 35 expr = &clause.Expr{SQL: "DISTINCT *"} 36 } else { 37 expr = &clause.Expr{ 38 SQL: "DISTINCT ?.*", 39 Vars: []interface{}{clause.Table{Name: a.col.Table}}, 40 } 41 } 42 return Asterisk{asteriskExpr{expr: a.setE(expr)}} 43 } 44 45 type asteriskExpr struct{ expr } 46 47 func (e asteriskExpr) BuildWithArgs(*gorm.Statement) (query sql, args []interface{}) { 48 // if e.expr has no expression it must be directly calling for "*" or "xxx.*" 49 if e.e != nil { 50 return "?", []interface{}{e.e} 51 } 52 if e.col.Table == "" { 53 return "*", nil 54 } 55 return "?.*", []interface{}{clause.Table{Name: e.col.Table}} 56 }