mirror of https://github.com/go-gorm/gorm.git
Check rows.Close error, close #4891
This commit is contained in:
parent
8627634959
commit
300a23fc31
|
@ -83,7 +83,7 @@ func Create(config *Config) func(db *gorm.DB) {
|
||||||
)
|
)
|
||||||
if db.AddError(err) == nil {
|
if db.AddError(err) == nil {
|
||||||
gorm.Scan(rows, db, mode)
|
gorm.Scan(rows, db, mode)
|
||||||
rows.Close()
|
db.AddError(rows.Close())
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
|
|
|
@ -168,7 +168,7 @@ func Delete(config *Config) func(db *gorm.DB) {
|
||||||
|
|
||||||
if rows, err := db.Statement.ConnPool.QueryContext(db.Statement.Context, db.Statement.SQL.String(), db.Statement.Vars...); db.AddError(err) == nil {
|
if rows, err := db.Statement.ConnPool.QueryContext(db.Statement.Context, db.Statement.SQL.String(), db.Statement.Vars...); db.AddError(err) == nil {
|
||||||
gorm.Scan(rows, db, mode)
|
gorm.Scan(rows, db, mode)
|
||||||
rows.Close()
|
db.AddError(rows.Close())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,9 +20,8 @@ func Query(db *gorm.DB) {
|
||||||
db.AddError(err)
|
db.AddError(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer rows.Close()
|
|
||||||
|
|
||||||
gorm.Scan(rows, db, 0)
|
gorm.Scan(rows, db, 0)
|
||||||
|
db.AddError(rows.Close())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -88,7 +88,7 @@ func Update(config *Config) func(db *gorm.DB) {
|
||||||
db.Statement.Dest = db.Statement.ReflectValue.Addr().Interface()
|
db.Statement.Dest = db.Statement.ReflectValue.Addr().Interface()
|
||||||
gorm.Scan(rows, db, mode)
|
gorm.Scan(rows, db, mode)
|
||||||
db.Statement.Dest = dest
|
db.Statement.Dest = dest
|
||||||
rows.Close()
|
db.AddError(rows.Close())
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
result, err := db.Statement.ConnPool.ExecContext(db.Statement.Context, db.Statement.SQL.String(), db.Statement.Vars...)
|
result, err := db.Statement.ConnPool.ExecContext(db.Statement.Context, db.Statement.SQL.String(), db.Statement.Vars...)
|
||||||
|
|
|
@ -457,12 +457,12 @@ func (db *DB) Scan(dest interface{}) (tx *DB) {
|
||||||
tx.Config = &config
|
tx.Config = &config
|
||||||
|
|
||||||
if rows, err := tx.Rows(); err == nil {
|
if rows, err := tx.Rows(); err == nil {
|
||||||
defer rows.Close()
|
|
||||||
if rows.Next() {
|
if rows.Next() {
|
||||||
tx.ScanRows(rows, dest)
|
tx.ScanRows(rows, dest)
|
||||||
} else {
|
} else {
|
||||||
tx.RowsAffected = 0
|
tx.RowsAffected = 0
|
||||||
}
|
}
|
||||||
|
tx.AddError(rows.Close())
|
||||||
}
|
}
|
||||||
|
|
||||||
currentLogger.Trace(tx.Statement.Context, newLogger.BeginAt, func() (string, int64) {
|
currentLogger.Trace(tx.Statement.Context, newLogger.BeginAt, func() (string, int64) {
|
||||||
|
|
|
@ -430,13 +430,15 @@ func (m Migrator) MigrateColumn(value interface{}, field *schema.Field, columnTy
|
||||||
// ColumnTypes return columnTypes []gorm.ColumnType and execErr error
|
// ColumnTypes return columnTypes []gorm.ColumnType and execErr error
|
||||||
func (m Migrator) ColumnTypes(value interface{}) ([]gorm.ColumnType, error) {
|
func (m Migrator) ColumnTypes(value interface{}) ([]gorm.ColumnType, error) {
|
||||||
columnTypes := make([]gorm.ColumnType, 0)
|
columnTypes := make([]gorm.ColumnType, 0)
|
||||||
execErr := m.RunWithValue(value, func(stmt *gorm.Statement) error {
|
execErr := m.RunWithValue(value, func(stmt *gorm.Statement) (err error) {
|
||||||
rows, err := m.DB.Session(&gorm.Session{}).Table(stmt.Table).Limit(1).Rows()
|
rows, err := m.DB.Session(&gorm.Session{}).Table(stmt.Table).Limit(1).Rows()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
defer rows.Close()
|
defer func() {
|
||||||
|
err = rows.Close()
|
||||||
|
}()
|
||||||
|
|
||||||
var rawColumnTypes []*sql.ColumnType
|
var rawColumnTypes []*sql.ColumnType
|
||||||
rawColumnTypes, err = rows.ColumnTypes()
|
rawColumnTypes, err = rows.ColumnTypes()
|
||||||
|
@ -448,7 +450,7 @@ func (m Migrator) ColumnTypes(value interface{}) ([]gorm.ColumnType, error) {
|
||||||
columnTypes = append(columnTypes, c)
|
columnTypes = append(columnTypes, c)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return
|
||||||
})
|
})
|
||||||
|
|
||||||
return columnTypes, execErr
|
return columnTypes, execErr
|
||||||
|
|
|
@ -132,6 +132,13 @@ func TestBelongsToAssociation(t *testing.T) {
|
||||||
|
|
||||||
AssertAssociationCount(t, user2, "Company", 0, "after clear")
|
AssertAssociationCount(t, user2, "Company", 0, "after clear")
|
||||||
AssertAssociationCount(t, user2, "Manager", 0, "after clear")
|
AssertAssociationCount(t, user2, "Manager", 0, "after clear")
|
||||||
|
|
||||||
|
// unexist company id
|
||||||
|
unexistCompanyID := company.ID + 9999999
|
||||||
|
user = User{Name: "invalid-user-with-invalid-belongs-to-foreign-key", CompanyID: &unexistCompanyID}
|
||||||
|
if err := DB.Create(&user).Error; err == nil {
|
||||||
|
t.Errorf("should have gotten foreign key violation error")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBelongsToAssociationForSlice(t *testing.T) {
|
func TestBelongsToAssociationForSlice(t *testing.T) {
|
||||||
|
|
Loading…
Reference in New Issue