From 4bbbb6f2422f13763ce601f334b6d03d2381291d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20G=C3=B3mez?= Date: Sat, 7 Nov 2020 18:38:25 -0500 Subject: [PATCH] Select all fields in SQL queries avoiding the SELECT * FROM --- callbacks/query.go | 22 ++++++---------------- 1 file changed, 6 insertions(+), 16 deletions(-) diff --git a/callbacks/query.go b/callbacks/query.go index 8613e46d..abf81077 100644 --- a/callbacks/query.go +++ b/callbacks/query.go @@ -72,23 +72,13 @@ func BuildQuerySQL(db *gorm.DB) { } } } else if db.Statement.Schema != nil && db.Statement.ReflectValue.IsValid() { - smallerStruct := false - switch db.Statement.ReflectValue.Kind() { - case reflect.Struct: - smallerStruct = db.Statement.ReflectValue.Type() != db.Statement.Schema.ModelType - case reflect.Slice: - smallerStruct = db.Statement.ReflectValue.Type().Elem() != db.Statement.Schema.ModelType - } + stmt := gorm.Statement{DB: db} + // smaller struct + if err := stmt.Parse(db.Statement.Dest); err == nil { + clauseSelect.Columns = make([]clause.Column, len(stmt.Schema.DBNames)) - if smallerStruct { - stmt := gorm.Statement{DB: db} - // 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} - } + for idx, dbName := range stmt.Schema.DBNames { + clauseSelect.Columns[idx] = clause.Column{Name: dbName} } } }