Skip to content

Commit

Permalink
feat: additional select column
Browse files Browse the repository at this point in the history
  • Loading branch information
misyuari committed Oct 26, 2023
1 parent 5adc0ce commit 0e24da6
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
2 changes: 2 additions & 0 deletions callbacks/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,8 @@ func BuildQuerySQL(db *gorm.DB) {
db.Statement.AddClauseIfNotExists(clause.From{})
}

clauseSelect.Columns = append(clauseSelect.Columns, db.Statement.AdditionalSelects...)

db.Statement.AddClauseIfNotExists(clauseSelect)

db.Statement.Build(db.Statement.BuildClauses...)
Expand Down
44 changes: 44 additions & 0 deletions chainable_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,50 @@ func (db *DB) Select(query interface{}, args ...interface{}) (tx *DB) {
return
}

// AdditionalSelect specify fields that you want when querying
//
// Use AdditionalSelect when you want add a subset of the fields. By default, GORM will select all fields.
// AdditionalSelect accepts both string arguments and arrays.
//
// // Select name and age of user using multiple arguments
// db.AdditionalSelect(clause.Column{
// Table: "users",
// Name: "id",
// Alias: "user_id",
// }, clause.Column{
// Table: "users",
// Name: "name",
// Alias: "user_name",
// }).Find(&users)
// // Select name and age of user using an array
// db.AdditionalSelect([]clause.Column{
// {
// Table: "users",
// Name: "id",
// Alias: "user_id",
// },
// {
// Table: "users",
// Name: "name",
// Alias: "user_name",
// },
// }).Find(&users)
func (db *DB) AdditionalSelect(selects interface{}) (tx *DB) {
tx = db.getInstance()

switch v := selects.(type) {
case []clause.Column:
tx.Statement.AdditionalSelects = v

case clause.Column:
tx.Statement.AdditionalSelects = []clause.Column{v}
default:
tx.AddError(fmt.Errorf("unsupported additional select args %v", selects))

Check failure on line 214 in chainable_api.go

View workflow job for this annotation

GitHub Actions / runner / golangci-lint

[golangci] reported by reviewdog 🐶 Error return value of `tx.AddError` is not checked (errcheck) Raw Output: chainable_api.go:214:14: Error return value of `tx.AddError` is not checked (errcheck) tx.AddError(fmt.Errorf("unsupported additional select args %v", selects)) ^
}

return
}

// Omit specify fields that you want to ignore when creating, updating and querying
func (db *DB) Omit(columns ...string) (tx *DB) {
tx = db.getInstance()
Expand Down
5 changes: 3 additions & 2 deletions statement.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ type Statement struct {
Clauses map[string]clause.Clause
BuildClauses []string
Distinct bool
Selects []string // selected columns
Omits []string // omit columns
Selects []string // selected columns
AdditionalSelects []clause.Column // additional selected columns
Omits []string // omit columns
Joins []join
Preloads map[string][]interface{}
Settings sync.Map
Expand Down

0 comments on commit 0e24da6

Please sign in to comment.