add MapColumns method
This commit is contained in:
parent
e4e23d26d2
commit
ef066ff4ed
@ -185,6 +185,13 @@ func (db *DB) Omit(columns ...string) (tx *DB) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// MapColumns specify column-to-field for customizing how database columns are assigned to struct fields during querying
|
||||||
|
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
|
||||||
|
Loading…
x
Reference in New Issue
Block a user