2020-05-29 02:35:45 +03:00
|
|
|
package gorm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"database/sql/driver"
|
2020-10-13 09:12:03 +03:00
|
|
|
"encoding/json"
|
2020-05-29 02:35:45 +03:00
|
|
|
"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-10-13 09:12:03 +03:00
|
|
|
func (n DeletedAt) MarshalJSON() ([]byte, error) {
|
2020-10-30 14:08:20 +03:00
|
|
|
if n.Valid {
|
|
|
|
return json.Marshal(n.Time)
|
|
|
|
}
|
|
|
|
return json.Marshal(nil)
|
2020-10-13 09:12:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (n *DeletedAt) UnmarshalJSON(b []byte) error {
|
2020-11-02 05:03:39 +03:00
|
|
|
if string(b) == "null" {
|
|
|
|
n.Valid = false
|
|
|
|
return nil
|
|
|
|
}
|
2020-10-13 09:12:03 +03:00
|
|
|
err := json.Unmarshal(b, &n.Time)
|
2020-11-02 05:03:39 +03:00
|
|
|
if err == nil {
|
2020-10-13 09:12:03 +03:00
|
|
|
n.Valid = true
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
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) {
|
2021-12-16 05:41:34 +03:00
|
|
|
if _, ok := stmt.Clauses["soft_delete_enabled"]; !ok && !stmt.Statement.Unscoped {
|
2020-10-19 09:49:42 +03:00
|
|
|
if c, ok := stmt.Clauses["WHERE"]; ok {
|
2021-12-30 06:47:14 +03:00
|
|
|
if where, ok := c.Expression.(clause.Where); ok && len(where.Exprs) >= 1 {
|
2020-10-19 09:49:42 +03:00
|
|
|
for _, expr := range where.Exprs {
|
|
|
|
if orCond, ok := expr.(clause.OrConditions); ok && len(orCond.Exprs) == 1 {
|
|
|
|
where.Exprs = []clause.Expression{clause.And(where.Exprs...)}
|
|
|
|
c.Expression = where
|
|
|
|
stmt.Clauses["WHERE"] = c
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-17 11:31:09 +03:00
|
|
|
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{}
|
|
|
|
}
|
2022-03-07 09:39:56 +03:00
|
|
|
|
|
|
|
// Modify for Joins[i].ON exprs
|
2022-03-07 15:33:12 +03:00
|
|
|
if c, ok := stmt.Clauses["FROM"]; ok && len(stmt.Joins) > 0 && sd.Field.Schema != nil {
|
|
|
|
joinedRelationName := "soft_delete_join_enabled" + sd.Field.Schema.Table
|
|
|
|
if _, ok := stmt.Clauses[joinedRelationName]; !ok && !stmt.Statement.Unscoped {
|
2022-03-07 09:39:56 +03:00
|
|
|
if fromClause, ok := c.Expression.(clause.From); ok && len(fromClause.Joins) > 0 {
|
|
|
|
for i, j := range fromClause.Joins {
|
2022-03-07 15:33:12 +03:00
|
|
|
if j.Table.Name == sd.Field.Schema.Table {
|
2022-03-07 09:39:56 +03:00
|
|
|
j.ON.Exprs = append(j.ON.Exprs, clause.Eq{
|
|
|
|
Column: clause.Column{Table: j.Table.Alias, Name: sd.Field.DBName}, Value: nil,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
fromClause.Joins[i] = j
|
|
|
|
}
|
|
|
|
}
|
|
|
|
stmt.Clauses["FROM"] = c
|
2022-03-07 15:33:12 +03:00
|
|
|
stmt.Clauses[joinedRelationName] = clause.Clause{}
|
2022-03-07 09:39:56 +03:00
|
|
|
}
|
|
|
|
}
|
2020-08-17 11:31:09 +03:00
|
|
|
}
|
|
|
|
|
2021-06-01 13:34:38 +03:00
|
|
|
func (DeletedAt) UpdateClauses(f *schema.Field) []clause.Interface {
|
|
|
|
return []clause.Interface{SoftDeleteUpdateClause{Field: f}}
|
|
|
|
}
|
|
|
|
|
|
|
|
type SoftDeleteUpdateClause struct {
|
|
|
|
Field *schema.Field
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sd SoftDeleteUpdateClause) Name() string {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sd SoftDeleteUpdateClause) Build(clause.Builder) {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sd SoftDeleteUpdateClause) MergeClause(*clause.Clause) {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sd SoftDeleteUpdateClause) ModifyStatement(stmt *Statement) {
|
2021-12-08 08:58:06 +03:00
|
|
|
if stmt.SQL.Len() == 0 && !stmt.Statement.Unscoped {
|
2022-02-25 05:48:23 +03:00
|
|
|
SoftDeleteQueryClause(sd).ModifyStatement(stmt)
|
2021-06-01 13:34:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-17 11:31:09 +03:00
|
|
|
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) {
|
2021-12-08 08:58:06 +03:00
|
|
|
if stmt.SQL.Len() == 0 && !stmt.Statement.Unscoped {
|
2020-12-15 05:39:20 +03:00
|
|
|
curTime := stmt.DB.NowFunc()
|
|
|
|
stmt.AddClause(clause.Set{{Column: clause.Column{Name: sd.Field.DBName}, Value: curTime}})
|
|
|
|
stmt.SetColumn(sd.Field.DBName, curTime, true)
|
2020-05-29 02:35:45 +03:00
|
|
|
|
|
|
|
if stmt.Schema != nil {
|
2022-02-16 10:30:43 +03:00
|
|
|
_, queryValues := schema.GetIdentityFieldValuesMap(stmt.Context, 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 {
|
2022-02-16 10:30:43 +03:00
|
|
|
_, queryValues = schema.GetIdentityFieldValuesMap(stmt.Context, 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}}})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-25 05:48:23 +03:00
|
|
|
SoftDeleteQueryClause(sd).ModifyStatement(stmt)
|
2020-05-29 02:35:45 +03:00
|
|
|
stmt.AddClauseIfNotExists(clause.Update{})
|
2021-10-28 03:03:23 +03:00
|
|
|
stmt.Build(stmt.DB.Callback().Update().Clauses...)
|
2020-05-29 02:35:45 +03:00
|
|
|
}
|
|
|
|
}
|