Using ColumnMapping instead of a new map

This commit is contained in:
Igor Stepanov 2025-08-10 02:29:33 +03:00
parent a4270cc29d
commit 2444fc0d82
4 changed files with 9 additions and 22 deletions

View File

@ -40,9 +40,12 @@ func BuildQuerySQL(db *gorm.DB) {
} }
} }
if db.Statement.TruncatedAliases == nil { truncatedTableAliases := make(map[string]string)
db.Statement.TruncatedAliases = make(map[string]string)
if db.Statement.ColumnMapping == nil {
db.Statement.ColumnMapping = make(map[string]string)
} }
if db.Statement.SQL.Len() == 0 { if db.Statement.SQL.Len() == 0 {
db.Statement.SQL.Grow(100) db.Statement.SQL.Grow(100)
clauseSelect := clause.Select{Distinct: db.Statement.Distinct} clauseSelect := clause.Select{Distinct: db.Statement.Distinct}
@ -168,10 +171,10 @@ func BuildQuerySQL(db *gorm.DB) {
Alias: aliasName, Alias: aliasName,
}) })
origTableAliasName := tableAliasName origTableAliasName := tableAliasName
if alias, ok := db.Statement.TruncatedAliases[tableAliasName]; ok { if alias, ok := truncatedTableAliases[tableAliasName]; ok {
origTableAliasName = alias origTableAliasName = alias
} }
db.Statement.TruncatedAliases[aliasName] = utils.NestedRelationName(origTableAliasName, s) db.Statement.ColumnMapping[aliasName] = utils.NestedRelationName(origTableAliasName, s)
} }
} }
@ -256,7 +259,7 @@ func BuildQuerySQL(db *gorm.DB) {
fullName = curAliasName fullName = curAliasName
} }
aliasName := db.NamingStrategy.JoinNestedRelationNames(nameParts) aliasName := db.NamingStrategy.JoinNestedRelationNames(nameParts)
db.Statement.TruncatedAliases[aliasName] = fullName truncatedTableAliases[aliasName] = fullName
curAliasName = aliasName curAliasName = aliasName
if _, ok := specifiedRelationsName[curAliasName]; !ok { if _, ok := specifiedRelationsName[curAliasName]; !ok {

View File

@ -227,9 +227,6 @@ func Scan(rows Rows, db *DB, mode ScanMode) {
if sch != nil { if sch != nil {
matchedFieldCount := make(map[string]int, len(columns)) matchedFieldCount := make(map[string]int, len(columns))
for idx, column := range columns { for idx, column := range columns {
if origName, ok := db.Statement.TruncatedAliases[column]; ok {
column = origName
}
if field := sch.LookUpField(column); field != nil && field.Readable { if field := sch.LookUpField(column); field != nil && field.Readable {
fields[idx] = field fields[idx] = field
if count, ok := matchedFieldCount[column]; ok { if count, ok := matchedFieldCount[column]; ok {

View File

@ -1,7 +1,6 @@
package schema package schema
import ( import (
"crypto/sha1"
"crypto/sha256" "crypto/sha256"
"encoding/hex" "encoding/hex"
"regexp" "regexp"
@ -156,18 +155,7 @@ func (ns NamingStrategy) formatName(prefix, table, name string) string {
prefix, table, name, prefix, table, name,
}, "_"), ".", "_") }, "_"), ".", "_")
if ns.IdentifierMaxLength == 0 { return ns.truncateName(formattedName)
ns.IdentifierMaxLength = 64
}
if utf8.RuneCountInString(formattedName) > ns.IdentifierMaxLength {
h := sha1.New()
h.Write([]byte(formattedName))
bs := h.Sum(nil)
formattedName = formattedName[0:ns.IdentifierMaxLength-8] + hex.EncodeToString(bs)[:8]
}
return formattedName
} }
var ( var (

View File

@ -47,7 +47,6 @@ type Statement struct {
attrs []interface{} attrs []interface{}
assigns []interface{} assigns []interface{}
scopes []func(*DB) *DB scopes []func(*DB) *DB
TruncatedAliases map[string]string
Result *result Result *result
} }