Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: additional select column #6658

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)) //nolint:typecheck,errcheck
}

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
Loading