Skip to content

Commit 2444fc0

Browse files
committed
Using ColumnMapping instead of a new map
1 parent a4270cc commit 2444fc0

File tree

4 files changed

+9
-22
lines changed

4 files changed

+9
-22
lines changed

callbacks/query.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,12 @@ func BuildQuerySQL(db *gorm.DB) {
4040
}
4141
}
4242

43-
if db.Statement.TruncatedAliases == nil {
44-
db.Statement.TruncatedAliases = make(map[string]string)
43+
truncatedTableAliases := make(map[string]string)
44+
45+
if db.Statement.ColumnMapping == nil {
46+
db.Statement.ColumnMapping = make(map[string]string)
4547
}
48+
4649
if db.Statement.SQL.Len() == 0 {
4750
db.Statement.SQL.Grow(100)
4851
clauseSelect := clause.Select{Distinct: db.Statement.Distinct}
@@ -168,10 +171,10 @@ func BuildQuerySQL(db *gorm.DB) {
168171
Alias: aliasName,
169172
})
170173
origTableAliasName := tableAliasName
171-
if alias, ok := db.Statement.TruncatedAliases[tableAliasName]; ok {
174+
if alias, ok := truncatedTableAliases[tableAliasName]; ok {
172175
origTableAliasName = alias
173176
}
174-
db.Statement.TruncatedAliases[aliasName] = utils.NestedRelationName(origTableAliasName, s)
177+
db.Statement.ColumnMapping[aliasName] = utils.NestedRelationName(origTableAliasName, s)
175178
}
176179
}
177180

@@ -256,7 +259,7 @@ func BuildQuerySQL(db *gorm.DB) {
256259
fullName = curAliasName
257260
}
258261
aliasName := db.NamingStrategy.JoinNestedRelationNames(nameParts)
259-
db.Statement.TruncatedAliases[aliasName] = fullName
262+
truncatedTableAliases[aliasName] = fullName
260263
curAliasName = aliasName
261264

262265
if _, ok := specifiedRelationsName[curAliasName]; !ok {

scan.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,9 +227,6 @@ func Scan(rows Rows, db *DB, mode ScanMode) {
227227
if sch != nil {
228228
matchedFieldCount := make(map[string]int, len(columns))
229229
for idx, column := range columns {
230-
if origName, ok := db.Statement.TruncatedAliases[column]; ok {
231-
column = origName
232-
}
233230
if field := sch.LookUpField(column); field != nil && field.Readable {
234231
fields[idx] = field
235232
if count, ok := matchedFieldCount[column]; ok {

schema/naming.go

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package schema
22

33
import (
4-
"crypto/sha1"
54
"crypto/sha256"
65
"encoding/hex"
76
"regexp"
@@ -156,18 +155,7 @@ func (ns NamingStrategy) formatName(prefix, table, name string) string {
156155
prefix, table, name,
157156
}, "_"), ".", "_")
158157

159-
if ns.IdentifierMaxLength == 0 {
160-
ns.IdentifierMaxLength = 64
161-
}
162-
163-
if utf8.RuneCountInString(formattedName) > ns.IdentifierMaxLength {
164-
h := sha1.New()
165-
h.Write([]byte(formattedName))
166-
bs := h.Sum(nil)
167-
168-
formattedName = formattedName[0:ns.IdentifierMaxLength-8] + hex.EncodeToString(bs)[:8]
169-
}
170-
return formattedName
158+
return ns.truncateName(formattedName)
171159
}
172160

173161
var (

statement.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ type Statement struct {
4747
attrs []interface{}
4848
assigns []interface{}
4949
scopes []func(*DB) *DB
50-
TruncatedAliases map[string]string
5150
Result *result
5251
}
5352

0 commit comments

Comments
 (0)