Select all fields in SQL queries avoiding the SELECT * FROM

This commit is contained in:
Luis Gómez 2020-11-07 18:38:25 -05:00
parent 85e9f66d26
commit 4bbbb6f242

View File

@ -72,23 +72,13 @@ func BuildQuerySQL(db *gorm.DB) {
} }
} }
} else if db.Statement.Schema != nil && db.Statement.ReflectValue.IsValid() { } else if db.Statement.Schema != nil && db.Statement.ReflectValue.IsValid() {
smallerStruct := false stmt := gorm.Statement{DB: db}
switch db.Statement.ReflectValue.Kind() { // smaller struct
case reflect.Struct: if err := stmt.Parse(db.Statement.Dest); err == nil {
smallerStruct = db.Statement.ReflectValue.Type() != db.Statement.Schema.ModelType clauseSelect.Columns = make([]clause.Column, len(stmt.Schema.DBNames))
case reflect.Slice:
smallerStruct = db.Statement.ReflectValue.Type().Elem() != db.Statement.Schema.ModelType
}
if smallerStruct { for idx, dbName := range stmt.Schema.DBNames {
stmt := gorm.Statement{DB: db} clauseSelect.Columns[idx] = clause.Column{Name: dbName}
// smaller struct
if err := stmt.Parse(db.Statement.Dest); err == nil && stmt.Schema.ModelType != db.Statement.Schema.ModelType {
clauseSelect.Columns = make([]clause.Column, len(stmt.Schema.DBNames))
for idx, dbName := range stmt.Schema.DBNames {
clauseSelect.Columns[idx] = clause.Column{Name: dbName}
}
} }
} }
} }