From 9a3ea037a9c9372ba6e892560181ed744db8f620 Mon Sep 17 00:00:00 2001 From: "Niklas J. Nett" Date: Tue, 6 Aug 2024 20:04:24 +0200 Subject: [PATCH] Add parse methods with central log instance --- association.go | 2 +- callbacks.go | 2 +- callbacks/preload.go | 2 +- callbacks/query.go | 2 +- callbacks/update.go | 2 +- finisher_api.go | 8 ++++---- gorm.go | 4 ++-- migrator/migrator.go | 8 ++++---- schema/schema.go | 14 +++++++++++--- statement.go | 12 ++++++++++-- 10 files changed, 36 insertions(+), 20 deletions(-) diff --git a/association.go b/association.go index e3f51d173b..d716ba2921 100644 --- a/association.go +++ b/association.go @@ -22,7 +22,7 @@ func (db *DB) Association(column string) *Association { association := &Association{DB: db} table := db.Statement.Table - if err := db.Statement.Parse(db.Statement.Model); err == nil { + if err := db.Statement.ParseWithLogger(db.Statement.Model, db.Logger); err == nil { db.Statement.Table = table association.Relationship = db.Statement.Schema.Relationships.Relations[column] diff --git a/callbacks.go b/callbacks.go index 50b5b0e937..6fd028ed58 100644 --- a/callbacks.go +++ b/callbacks.go @@ -102,7 +102,7 @@ func (p *processor) Execute(db *DB) *DB { // parse model values if stmt.Model != nil { - if err := stmt.Parse(stmt.Model); err != nil && (!errors.Is(err, schema.ErrUnsupportedDataType) || (stmt.Table == "" && stmt.TableExpr == nil && stmt.SQL.Len() == 0)) { + if err := stmt.ParseWithLogger(stmt.Model, p.db.Logger); err != nil && (!errors.Is(err, schema.ErrUnsupportedDataType) || (stmt.Table == "" && stmt.TableExpr == nil && stmt.SQL.Len() == 0)) { if errors.Is(err, schema.ErrUnsupportedDataType) && stmt.Table == "" && stmt.TableExpr == nil { db.AddError(fmt.Errorf("%w: Table not set, please set it like: db.Model(&user) or db.Table(\"users\")", err)) } else { diff --git a/callbacks/preload.go b/callbacks/preload.go index fd8214bb26..03c40c73c1 100644 --- a/callbacks/preload.go +++ b/callbacks/preload.go @@ -173,7 +173,7 @@ func preloadDB(db *gorm.DB, reflectValue reflect.Value, dest interface{}) *gorm. return true }) - if err := tx.Statement.Parse(dest); err != nil { + if err := tx.Statement.ParseWithLogger(dest, db.Logger); err != nil { tx.AddError(err) return tx } diff --git a/callbacks/query.go b/callbacks/query.go index 9b2b17ea94..ccd1603680 100644 --- a/callbacks/query.go +++ b/callbacks/query.go @@ -86,7 +86,7 @@ func BuildQuerySQL(db *gorm.DB) { if queryFields { stmt := gorm.Statement{DB: db} // smaller struct - if err := stmt.Parse(db.Statement.Dest); err == nil && (db.QueryFields || stmt.Schema.ModelType != db.Statement.Schema.ModelType) { + if err := stmt.ParseWithLogger(db.Statement.Dest, db.Logger); err == nil && (db.QueryFields || stmt.Schema.ModelType != db.Statement.Schema.ModelType) { clauseSelect.Columns = make([]clause.Column, len(stmt.Schema.DBNames)) for idx, dbName := range stmt.Schema.DBNames { diff --git a/callbacks/update.go b/callbacks/update.go index 7cde7f6196..ea6f9efab9 100644 --- a/callbacks/update.go +++ b/callbacks/update.go @@ -250,7 +250,7 @@ func ConvertToAssignments(stmt *gorm.Statement) (set clause.Set) { if !updatingValue.CanAddr() || stmt.Dest != stmt.Model { // different schema updatingStmt := &gorm.Statement{DB: stmt.DB} - if err := updatingStmt.Parse(stmt.Dest); err == nil { + if err := updatingStmt.ParseWithLogger(stmt.Dest, stmt.Logger); err == nil { updatingSchema = updatingStmt.Schema isDiffSchema = true } diff --git a/finisher_api.go b/finisher_api.go index f97571ed04..42f121283c 100644 --- a/finisher_api.go +++ b/finisher_api.go @@ -86,7 +86,7 @@ func (db *DB) Save(value interface{}) (tx *DB) { } tx = tx.callbacks.Create().Execute(tx.Set("gorm:update_track_time", true)) case reflect.Struct: - if err := tx.Statement.Parse(value); err == nil && tx.Statement.Schema != nil { + if err := tx.Statement.ParseWithLogger(value, db.Logger); err == nil && tx.Statement.Schema != nil { for _, pf := range tx.Statement.Schema.PrimaryFields { if _, isZero := pf.ValueOf(tx.Statement.Context, reflectValue); isZero { return tx.callbacks.Create().Execute(tx) @@ -465,7 +465,7 @@ func (db *DB) Count(count *int64) (tx *DB) { dbName := tx.Statement.Selects[0] fields := strings.FieldsFunc(dbName, utils.IsValidDBNameChar) if len(fields) == 1 || (len(fields) == 3 && (strings.ToUpper(fields[1]) == "AS" || fields[1] == ".")) { - if tx.Statement.Parse(tx.Statement.Model) == nil { + if tx.Statement.ParseWithLogger(tx.Statement.Model, db.Logger) == nil { if f := tx.Statement.Schema.LookUpField(dbName); f != nil { dbName = f.DBName } @@ -554,7 +554,7 @@ func (db *DB) Scan(dest interface{}) (tx *DB) { func (db *DB) Pluck(column string, dest interface{}) (tx *DB) { tx = db.getInstance() if tx.Statement.Model != nil { - if tx.Statement.Parse(tx.Statement.Model) == nil { + if tx.Statement.ParseWithLogger(tx.Statement.Model, db.Logger) == nil { if f := tx.Statement.Schema.LookUpField(column); f != nil { column = f.DBName } @@ -574,7 +574,7 @@ func (db *DB) Pluck(column string, dest interface{}) (tx *DB) { func (db *DB) ScanRows(rows *sql.Rows, dest interface{}) error { tx := db.getInstance() - if err := tx.Statement.Parse(dest); !errors.Is(err, schema.ErrUnsupportedDataType) { + if err := tx.Statement.ParseWithLogger(dest, db.Logger); !errors.Is(err, schema.ErrUnsupportedDataType) { tx.AddError(err) } tx.Statement.Dest = dest diff --git a/gorm.go b/gorm.go index 117d2fd074..d4c6c28782 100644 --- a/gorm.go +++ b/gorm.go @@ -444,13 +444,13 @@ func (db *DB) SetupJoinTable(model interface{}, field string, joinTable interfac modelSchema, joinSchema *schema.Schema ) - err := stmt.Parse(model) + err := stmt.ParseWithLogger(model, db.Logger) if err != nil { return err } modelSchema = stmt.Schema - err = stmt.Parse(joinTable) + err = stmt.ParseWithLogger(joinTable, db.Logger) if err != nil { return err } diff --git a/migrator/migrator.go b/migrator/migrator.go index 189a141f56..4b2af5223e 100644 --- a/migrator/migrator.go +++ b/migrator/migrator.go @@ -67,7 +67,7 @@ func (m Migrator) RunWithValue(value interface{}, fc func(*gorm.Statement) error if table, ok := value.(string); ok { stmt.Table = table - } else if err := stmt.ParseWithSpecialTableName(value, stmt.Table); err != nil { + } else if err := stmt.ParseWithSpecialTableNameWithLogger(value, stmt.Table, m.DB.Logger); err != nil { return err } @@ -348,7 +348,7 @@ func (m Migrator) RenameTable(oldName, newName interface{}) error { oldTable = clause.Table{Name: v} } else { stmt := &gorm.Statement{DB: m.DB} - if err := stmt.Parse(oldName); err == nil { + if err := stmt.ParseWithLogger(oldName, m.DB.Logger); err == nil { oldTable = m.CurrentTable(stmt) } else { return err @@ -359,7 +359,7 @@ func (m Migrator) RenameTable(oldName, newName interface{}) error { newTable = clause.Table{Name: v} } else { stmt := &gorm.Statement{DB: m.DB} - if err := stmt.Parse(newName); err == nil { + if err := stmt.ParseWithLogger(newName, m.DB.Logger); err == nil { newTable = m.CurrentTable(stmt) } else { return err @@ -918,7 +918,7 @@ func (m Migrator) ReorderModels(values []interface{}, autoAdd bool) (results []i } beDependedOn := map[*schema.Schema]bool{} // support for special table name - if err := dep.ParseWithSpecialTableName(value, m.DB.Statement.Table); err != nil { + if err := dep.ParseWithSpecialTableNameWithLogger(value, m.DB.Statement.Table, m.Config.DB.Logger); err != nil { m.DB.Logger.Error(context.Background(), "failed to parse value %#v, got error %v", value, err) } if _, ok := parsedSchemas[dep.Statement.Schema]; ok { diff --git a/schema/schema.go b/schema/schema.go index db2367975d..54875d46be 100644 --- a/schema/schema.go +++ b/schema/schema.go @@ -115,11 +115,19 @@ type TablerWithNamer interface { // Parse get data type from dialector func Parse(dest interface{}, cacheStore *sync.Map, namer Namer) (*Schema, error) { - return ParseWithSpecialTableName(dest, cacheStore, namer, "") + return ParseWithSpecialTableNameWithLogger(dest, cacheStore, namer, "", logger.Default) +} + +func ParseWithLogger(dest interface{}, cacheStore *sync.Map, namer Namer, log logger.Interface) (*Schema, error) { + return ParseWithSpecialTableNameWithLogger(dest, cacheStore, namer, "", log) } // ParseWithSpecialTableName get data type from dialector with extra schema table func ParseWithSpecialTableName(dest interface{}, cacheStore *sync.Map, namer Namer, specialTableName string) (*Schema, error) { + return ParseWithSpecialTableNameWithLogger(dest, cacheStore, namer, specialTableName, logger.Default) +} + +func ParseWithSpecialTableNameWithLogger(dest interface{}, cacheStore *sync.Map, namer Namer, specialTableName string, log logger.Interface) (*Schema, error) { if dest == nil { return nil, fmt.Errorf("%w: %+v", ErrUnsupportedDataType, dest) } @@ -316,7 +324,7 @@ func ParseWithSpecialTableName(dest interface{}, cacheStore *sync.Map, namer Nam case "func(*gorm.DB) error": // TODO hack reflect.Indirect(reflect.ValueOf(schema)).FieldByName(string(cbName)).SetBool(true) default: - logger.Default.Warn(context.Background(), "Model %v don't match %vInterface, should be `%v(*gorm.DB) error`. Please see https://gorm.io/docs/hooks.html", schema, cbName, cbName) + log.Warn(context.Background(), "Model %v don't match %vInterface, should be `%v(*gorm.DB) error`. Please see https://gorm.io/docs/hooks.html", schema, cbName, cbName) } } } @@ -331,7 +339,7 @@ func ParseWithSpecialTableName(dest interface{}, cacheStore *sync.Map, namer Nam defer func() { if schema.err != nil { - logger.Default.Error(context.Background(), schema.err.Error()) + log.Error(context.Background(), schema.err.Error()) cacheStore.Delete(modelType) } }() diff --git a/statement.go b/statement.go index 39e05d093b..5d16d41bbe 100644 --- a/statement.go +++ b/statement.go @@ -486,11 +486,19 @@ func (stmt *Statement) Build(clauses ...string) { } func (stmt *Statement) Parse(value interface{}) (err error) { - return stmt.ParseWithSpecialTableName(value, "") + return stmt.ParseWithSpecialTableNameWithLogger(value, "", logger.Default) +} + +func (stmt *Statement) ParseWithLogger(value interface{}, log logger.Interface) (err error) { + return stmt.ParseWithSpecialTableNameWithLogger(value, "", log) } func (stmt *Statement) ParseWithSpecialTableName(value interface{}, specialTableName string) (err error) { - if stmt.Schema, err = schema.ParseWithSpecialTableName(value, stmt.DB.cacheStore, stmt.DB.NamingStrategy, specialTableName); err == nil && stmt.Table == "" { + return stmt.ParseWithSpecialTableNameWithLogger(value, specialTableName, logger.Default) +} + +func (stmt *Statement) ParseWithSpecialTableNameWithLogger(value interface{}, specialTableName string, log logger.Interface) (err error) { + if stmt.Schema, err = schema.ParseWithSpecialTableNameWithLogger(value, stmt.DB.cacheStore, stmt.DB.NamingStrategy, specialTableName, log); err == nil && stmt.Table == "" { if tables := strings.Split(stmt.Schema.Table, "."); len(tables) == 2 { stmt.TableExpr = &clause.Expr{SQL: stmt.Quote(stmt.Schema.Table)} stmt.Table = tables[1]