forked from go-gorm/gen
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add new field type Asterisk (go-gorm#609)
* feat: add new field type Asterisk * feat: fix asterisk sql build * feat: update template for asterisk
- Loading branch information
Showing
6 changed files
with
111 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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}} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters