mirror of https://github.com/go-gorm/gorm.git
feat: add MapColumns method (#6901)
* add MapColumns method * fix MapColumns desc * add TestMapColumns
This commit is contained in:
parent
8a0af58cc5
commit
11c4331058
|
@ -185,6 +185,13 @@ func (db *DB) Omit(columns ...string) (tx *DB) {
|
||||||
return
|
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
|
// Where add conditions
|
||||||
//
|
//
|
||||||
// See the [docs] for details on the various formats that where clauses can take. By default, where clauses chain with AND.
|
// See the [docs] for details on the various formats that where clauses can take. By default, where clauses chain with AND.
|
||||||
|
|
9
scan.go
9
scan.go
|
@ -131,6 +131,15 @@ func Scan(rows Rows, db *DB, mode ScanMode) {
|
||||||
onConflictDonothing = mode&ScanOnConflictDoNothing != 0
|
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
|
db.RowsAffected = 0
|
||||||
|
|
||||||
switch dest := db.Statement.Dest.(type) {
|
switch dest := db.Statement.Dest.(type) {
|
||||||
|
|
|
@ -32,6 +32,7 @@ type Statement struct {
|
||||||
Distinct bool
|
Distinct bool
|
||||||
Selects []string // selected columns
|
Selects []string // selected columns
|
||||||
Omits []string // omit columns
|
Omits []string // omit columns
|
||||||
|
ColumnMapping map[string]string // map columns
|
||||||
Joins []join
|
Joins []join
|
||||||
Preloads map[string][]interface{}
|
Preloads map[string][]interface{}
|
||||||
Settings sync.Map
|
Settings sync.Map
|
||||||
|
@ -513,6 +514,7 @@ func (stmt *Statement) clone() *Statement {
|
||||||
Distinct: stmt.Distinct,
|
Distinct: stmt.Distinct,
|
||||||
Selects: stmt.Selects,
|
Selects: stmt.Selects,
|
||||||
Omits: stmt.Omits,
|
Omits: stmt.Omits,
|
||||||
|
ColumnMapping: stmt.ColumnMapping,
|
||||||
Preloads: map[string][]interface{}{},
|
Preloads: map[string][]interface{}{},
|
||||||
ConnPool: stmt.ConnPool,
|
ConnPool: stmt.ConnPool,
|
||||||
Schema: stmt.Schema,
|
Schema: stmt.Schema,
|
||||||
|
|
|
@ -860,6 +860,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) {
|
func TestPluckWithSelect(t *testing.T) {
|
||||||
users := []User{
|
users := []User{
|
||||||
{Name: "pluck_with_select_1", Age: 25},
|
{Name: "pluck_with_select_1", Age: 25},
|
||||||
|
@ -1194,7 +1216,6 @@ func TestSubQueryWithRaw(t *testing.T) {
|
||||||
Where("age >= ? and name in (?)", 20, []string{"subquery_raw_1", "subquery_raw_3"}).
|
Where("age >= ? and name in (?)", 20, []string{"subquery_raw_1", "subquery_raw_3"}).
|
||||||
Group("name"),
|
Group("name"),
|
||||||
).Count(&count).Error
|
).Count(&count).Error
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Expected to get no errors, but got %v", err)
|
t.Errorf("Expected to get no errors, but got %v", err)
|
||||||
}
|
}
|
||||||
|
@ -1210,7 +1231,6 @@ func TestSubQueryWithRaw(t *testing.T) {
|
||||||
Not("age <= ?", 10).Not("name IN (?)", []string{"subquery_raw_1", "subquery_raw_3"}).
|
Not("age <= ?", 10).Not("name IN (?)", []string{"subquery_raw_1", "subquery_raw_3"}).
|
||||||
Group("name"),
|
Group("name"),
|
||||||
).Count(&count).Error
|
).Count(&count).Error
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Expected to get no errors, but got %v", err)
|
t.Errorf("Expected to get no errors, but got %v", err)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue