mirror of https://github.com/go-gorm/gorm.git
Fix panic when using invalid data, close #3193
This commit is contained in:
parent
c3f52cee8b
commit
69d8111893
|
@ -51,7 +51,7 @@ func Create(config *Config) func(db *gorm.DB) {
|
|||
db.Statement.Build("INSERT", "VALUES", "ON CONFLICT")
|
||||
}
|
||||
|
||||
if !db.DryRun {
|
||||
if !db.DryRun && db.Error == nil {
|
||||
result, err := db.Statement.ConnPool.ExecContext(db.Statement.Context, db.Statement.SQL.String(), db.Statement.Vars...)
|
||||
|
||||
if err == nil {
|
||||
|
@ -130,7 +130,7 @@ func CreateWithReturning(db *gorm.DB) {
|
|||
db.Statement.WriteQuoted(field.DBName)
|
||||
}
|
||||
|
||||
if !db.DryRun {
|
||||
if !db.DryRun && db.Error == nil {
|
||||
rows, err := db.Statement.ConnPool.QueryContext(db.Statement.Context, db.Statement.SQL.String(), db.Statement.Vars...)
|
||||
|
||||
if err == nil {
|
||||
|
@ -179,7 +179,7 @@ func CreateWithReturning(db *gorm.DB) {
|
|||
db.AddError(err)
|
||||
}
|
||||
}
|
||||
} else if !db.DryRun {
|
||||
} else if !db.DryRun && db.Error == nil {
|
||||
if result, err := db.Statement.ConnPool.ExecContext(db.Statement.Context, db.Statement.SQL.String(), db.Statement.Vars...); err == nil {
|
||||
db.RowsAffected, _ = result.RowsAffected()
|
||||
} else {
|
||||
|
|
|
@ -60,7 +60,7 @@ func Delete(db *gorm.DB) {
|
|||
db.Statement.Build("DELETE", "FROM", "WHERE")
|
||||
}
|
||||
|
||||
if !db.DryRun {
|
||||
if !db.DryRun && db.Error == nil {
|
||||
result, err := db.Statement.ConnPool.ExecContext(db.Statement.Context, db.Statement.SQL.String(), db.Statement.Vars...)
|
||||
|
||||
if err == nil {
|
||||
|
|
|
@ -23,7 +23,7 @@ func Query(db *gorm.DB) {
|
|||
BuildQuerySQL(db)
|
||||
}
|
||||
|
||||
if !db.DryRun {
|
||||
if !db.DryRun && db.Error == nil {
|
||||
rows, err := db.Statement.ConnPool.QueryContext(db.Statement.Context, db.Statement.SQL.String(), db.Statement.Vars...)
|
||||
if err != nil {
|
||||
db.AddError(err)
|
||||
|
|
|
@ -74,7 +74,7 @@ func Update(db *gorm.DB) {
|
|||
return
|
||||
}
|
||||
|
||||
if !db.DryRun {
|
||||
if !db.DryRun && db.Error == nil {
|
||||
result, err := db.Statement.ConnPool.ExecContext(db.Statement.Context, db.Statement.SQL.String(), db.Statement.Vars...)
|
||||
|
||||
if err == nil {
|
||||
|
|
|
@ -7,20 +7,14 @@ import (
|
|||
var (
|
||||
// ErrRecordNotFound record not found error
|
||||
ErrRecordNotFound = errors.New("record not found")
|
||||
// ErrInvalidSQL invalid SQL error, happens when you passed invalid SQL
|
||||
ErrInvalidSQL = errors.New("invalid SQL")
|
||||
// ErrInvalidTransaction invalid transaction when you are trying to `Commit` or `Rollback`
|
||||
ErrInvalidTransaction = errors.New("no valid transaction")
|
||||
// ErrUnaddressable unaddressable value
|
||||
ErrUnaddressable = errors.New("using unaddressable value")
|
||||
// ErrNotImplemented not implemented
|
||||
ErrNotImplemented = errors.New("not implemented")
|
||||
// ErrMissingWhereClause missing where clause
|
||||
ErrMissingWhereClause = errors.New("WHERE conditions required")
|
||||
// ErrUnsupportedRelation unsupported relations
|
||||
ErrUnsupportedRelation = errors.New("unsupported relations")
|
||||
// ErrPtrStructSupported only ptr of struct supported
|
||||
ErrPtrStructSupported = errors.New("only ptr of struct supported")
|
||||
// ErrorPrimaryKeyRequired primary keys required
|
||||
ErrorPrimaryKeyRequired = errors.New("primary key required")
|
||||
// ErrorModelValueRequired model value required
|
||||
|
|
|
@ -95,7 +95,9 @@ func (stmt *Statement) QuoteTo(writer clause.Writer, field interface{}) {
|
|||
}
|
||||
|
||||
if v.Name == clause.PrimaryKey {
|
||||
if stmt.Schema != nil && stmt.Schema.PrioritizedPrimaryField != nil {
|
||||
if stmt.Schema == nil {
|
||||
stmt.DB.AddError(ErrorModelValueRequired)
|
||||
} else if stmt.Schema.PrioritizedPrimaryField != nil {
|
||||
stmt.DB.Dialector.QuoteTo(writer, stmt.Schema.PrioritizedPrimaryField.DBName)
|
||||
} else if len(stmt.Schema.DBNames) > 0 {
|
||||
stmt.DB.Dialector.QuoteTo(writer, stmt.Schema.DBNames[0])
|
||||
|
|
Loading…
Reference in New Issue