Fix panic when using invalid data, close #3193

This commit is contained in:
Jinzhu 2020-07-24 08:32:50 +08:00
parent c3f52cee8b
commit 69d8111893
6 changed files with 9 additions and 13 deletions

View File

@ -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 {

View File

@ -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 {

View File

@ -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)

View File

@ -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 {

View File

@ -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

View File

@ -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])