Allow to use sql function in Group, Pluck

This commit is contained in:
Jinzhu 2020-06-30 23:06:48 +08:00
parent 929c0c576c
commit ee1f46e3a1
2 changed files with 6 additions and 2 deletions

View File

@ -162,8 +162,10 @@ func (db *DB) Joins(query string, args ...interface{}) (tx *DB) {
// Group specify the group method on the find // Group specify the group method on the find
func (db *DB) Group(name string) (tx *DB) { func (db *DB) Group(name string) (tx *DB) {
tx = db.getInstance() tx = db.getInstance()
fields := strings.FieldsFunc(name, utils.IsChar)
tx.Statement.AddClause(clause.GroupBy{ tx.Statement.AddClause(clause.GroupBy{
Columns: []clause.Column{{Name: name}}, Columns: []clause.Column{{Name: name, Raw: len(fields) != 1}},
}) })
return return
} }

View File

@ -8,6 +8,7 @@ import (
"strings" "strings"
"gorm.io/gorm/clause" "gorm.io/gorm/clause"
"gorm.io/gorm/utils"
) )
// Create insert the value into database // Create insert the value into database
@ -325,9 +326,10 @@ func (db *DB) Pluck(column string, dest interface{}) (tx *DB) {
tx.AddError(ErrorModelValueRequired) tx.AddError(ErrorModelValueRequired)
} }
fields := strings.FieldsFunc(column, utils.IsChar)
tx.Statement.AddClauseIfNotExists(clause.Select{ tx.Statement.AddClauseIfNotExists(clause.Select{
Distinct: tx.Statement.Distinct, Distinct: tx.Statement.Distinct,
Columns: []clause.Column{{Name: column}}, Columns: []clause.Column{{Name: column, Raw: len(fields) != 1}},
}) })
tx.Statement.Dest = dest tx.Statement.Dest = dest
tx.callbacks.Query().Execute(tx) tx.callbacks.Query().Execute(tx)