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: add MapColumns method #6901

Merged
merged 3 commits into from
Jun 24, 2024
Merged
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
7 changes: 7 additions & 0 deletions chainable_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,13 @@ func (db *DB) Omit(columns ...string) (tx *DB) {
return
}

// MapColumns modify the column names in the query results to facilitate align to the corresponding structural fields
func (db *DB) MapColumns(m map[string]string) (tx *DB) {
tx = db.getInstance()
tx.Statement.ColumnMapping = m
return
}

// Where add conditions
//
// See the [docs] for details on the various formats that where clauses can take. By default, where clauses chain with AND.
Expand Down
9 changes: 9 additions & 0 deletions scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,15 @@ func Scan(rows Rows, db *DB, mode ScanMode) {
onConflictDonothing = mode&ScanOnConflictDoNothing != 0
)

if len(db.Statement.ColumnMapping) > 0 {
for i, column := range columns {
v, ok := db.Statement.ColumnMapping[column]
if ok {
columns[i] = v
}
}
}

db.RowsAffected = 0

switch dest := db.Statement.Dest.(type) {
Expand Down
6 changes: 4 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
Omits []string // omit columns
ColumnMapping map[string]string // map columns
Joins []join
Preloads map[string][]interface{}
Settings sync.Map
Expand Down Expand Up @@ -513,6 +514,7 @@ func (stmt *Statement) clone() *Statement {
Distinct: stmt.Distinct,
Selects: stmt.Selects,
Omits: stmt.Omits,
ColumnMapping: stmt.ColumnMapping,
Preloads: map[string][]interface{}{},
ConnPool: stmt.ConnPool,
Schema: stmt.Schema,
Expand Down
24 changes: 22 additions & 2 deletions tests/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -855,6 +855,28 @@ func TestOmitWithAllFields(t *testing.T) {
}
}

func TestMapColumns(t *testing.T) {
user := User{Name: "MapColumnsUser", Age: 12}
DB.Save(&user)

type result struct {
Name string
Nickname string
Age uint
}
var res result
DB.Table("users").Where("name = ?", user.Name).MapColumns(map[string]string{"name": "nickname"}).Scan(&res)
if res.Nickname != user.Name {
t.Errorf("Expected res.Nickname to be %s, but got %s", user.Name, res.Nickname)
}
if res.Name != "" {
t.Errorf("Expected res.Name to be empty, but got %s", res.Name)
}
if res.Age != user.Age {
t.Errorf("Expected res.Age to be %d, but got %d", user.Age, res.Age)
}
}

func TestPluckWithSelect(t *testing.T) {
users := []User{
{Name: "pluck_with_select_1", Age: 25},
Expand Down Expand Up @@ -1189,7 +1211,6 @@ func TestSubQueryWithRaw(t *testing.T) {
Where("age >= ? and name in (?)", 20, []string{"subquery_raw_1", "subquery_raw_3"}).
Group("name"),
).Count(&count).Error

if err != nil {
t.Errorf("Expected to get no errors, but got %v", err)
}
Expand All @@ -1205,7 +1226,6 @@ func TestSubQueryWithRaw(t *testing.T) {
Not("age <= ?", 10).Not("name IN (?)", []string{"subquery_raw_1", "subquery_raw_3"}).
Group("name"),
).Count(&count).Error

if err != nil {
t.Errorf("Expected to get no errors, but got %v", err)
}
Expand Down
Loading