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