2020-05-29 02:35:45 +03:00
|
|
|
package gorm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"database/sql/driver"
|
|
|
|
"reflect"
|
|
|
|
|
2020-06-02 04:16:07 +03:00
|
|
|
"gorm.io/gorm/clause"
|
|
|
|
"gorm.io/gorm/schema"
|
2020-05-29 02:35:45 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type DeletedAt sql.NullTime
|
|
|
|
|
|
|
|
// Scan implements the Scanner interface.
|
|
|
|
func (n *DeletedAt) Scan(value interface{}) error {
|
|
|
|
return (*sql.NullTime)(n).Scan(value)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Value implements the driver Valuer interface.
|
|
|
|
func (n DeletedAt) Value() (driver.Value, error) {
|
|
|
|
if !n.Valid {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return n.Time, nil
|
|
|
|
}
|
|
|
|
|
2020-08-17 11:31:09 +03:00
|
|
|
func (DeletedAt) QueryClauses(f *schema.Field) []clause.Interface {
|
2020-09-01 13:05:26 +03:00
|
|
|
return []clause.Interface{SoftDeleteQueryClause{Field: f}}
|
2020-05-29 02:35:45 +03:00
|
|
|
}
|
|
|
|
|
2020-08-17 11:31:09 +03:00
|
|
|
type SoftDeleteQueryClause struct {
|
|
|
|
Field *schema.Field
|
2020-05-29 02:35:45 +03:00
|
|
|
}
|
|
|
|
|
2020-08-17 11:31:09 +03:00
|
|
|
func (sd SoftDeleteQueryClause) Name() string {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sd SoftDeleteQueryClause) Build(clause.Builder) {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sd SoftDeleteQueryClause) MergeClause(*clause.Clause) {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sd SoftDeleteQueryClause) ModifyStatement(stmt *Statement) {
|
|
|
|
if _, ok := stmt.Clauses["soft_delete_enabled"]; !ok {
|
|
|
|
stmt.AddClause(clause.Where{Exprs: []clause.Expression{
|
|
|
|
clause.Eq{Column: clause.Column{Table: clause.CurrentTable, Name: sd.Field.DBName}, Value: nil},
|
|
|
|
}})
|
|
|
|
stmt.Clauses["soft_delete_enabled"] = clause.Clause{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (DeletedAt) DeleteClauses(f *schema.Field) []clause.Interface {
|
|
|
|
return []clause.Interface{SoftDeleteDeleteClause{Field: f}}
|
|
|
|
}
|
|
|
|
|
|
|
|
type SoftDeleteDeleteClause struct {
|
|
|
|
Field *schema.Field
|
2020-05-29 02:35:45 +03:00
|
|
|
}
|
|
|
|
|
2020-08-17 11:31:09 +03:00
|
|
|
func (sd SoftDeleteDeleteClause) Name() string {
|
2020-05-29 02:35:45 +03:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2020-08-17 11:31:09 +03:00
|
|
|
func (sd SoftDeleteDeleteClause) Build(clause.Builder) {
|
2020-05-29 02:35:45 +03:00
|
|
|
}
|
|
|
|
|
2020-08-17 11:31:09 +03:00
|
|
|
func (sd SoftDeleteDeleteClause) MergeClause(*clause.Clause) {
|
2020-05-29 02:35:45 +03:00
|
|
|
}
|
|
|
|
|
2020-08-17 11:31:09 +03:00
|
|
|
func (sd SoftDeleteDeleteClause) ModifyStatement(stmt *Statement) {
|
2020-05-29 02:35:45 +03:00
|
|
|
if stmt.SQL.String() == "" {
|
2020-08-17 11:31:09 +03:00
|
|
|
stmt.AddClause(clause.Set{{Column: clause.Column{Name: sd.Field.DBName}, Value: stmt.DB.NowFunc()}})
|
2020-05-29 02:35:45 +03:00
|
|
|
|
|
|
|
if stmt.Schema != nil {
|
|
|
|
_, queryValues := schema.GetIdentityFieldValuesMap(stmt.ReflectValue, stmt.Schema.PrimaryFields)
|
2020-07-10 08:08:15 +03:00
|
|
|
column, values := schema.ToQueryValues(stmt.Table, stmt.Schema.PrimaryFieldDBNames, queryValues)
|
2020-05-29 02:35:45 +03:00
|
|
|
|
|
|
|
if len(values) > 0 {
|
|
|
|
stmt.AddClause(clause.Where{Exprs: []clause.Expression{clause.IN{Column: column, Values: values}}})
|
|
|
|
}
|
|
|
|
|
2020-08-06 12:48:46 +03:00
|
|
|
if stmt.ReflectValue.CanAddr() && stmt.Dest != stmt.Model && stmt.Model != nil {
|
2020-05-29 02:35:45 +03:00
|
|
|
_, queryValues = schema.GetIdentityFieldValuesMap(reflect.ValueOf(stmt.Model), stmt.Schema.PrimaryFields)
|
2020-07-10 08:08:15 +03:00
|
|
|
column, values = schema.ToQueryValues(stmt.Table, stmt.Schema.PrimaryFieldDBNames, queryValues)
|
2020-05-29 02:35:45 +03:00
|
|
|
|
|
|
|
if len(values) > 0 {
|
|
|
|
stmt.AddClause(clause.Where{Exprs: []clause.Expression{clause.IN{Column: column, Values: values}}})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
stmt.AddClauseIfNotExists(clause.Update{})
|
|
|
|
stmt.Build("UPDATE", "SET", "WHERE")
|
|
|
|
}
|
|
|
|
}
|