github.com/gogf/gf/v2@v2.7.4/database/gdb/gdb_model_delete.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/gogf/gf.
     6  
     7  package gdb
     8  
     9  import (
    10  	"database/sql"
    11  
    12  	"github.com/gogf/gf/v2/errors/gcode"
    13  	"github.com/gogf/gf/v2/errors/gerror"
    14  	"github.com/gogf/gf/v2/internal/intlog"
    15  	"github.com/gogf/gf/v2/text/gstr"
    16  )
    17  
    18  // Delete does "DELETE FROM ... " statement for the model.
    19  // The optional parameter `where` is the same as the parameter of Model.Where function,
    20  // see Model.Where.
    21  func (m *Model) Delete(where ...interface{}) (result sql.Result, err error) {
    22  	var ctx = m.GetCtx()
    23  	if len(where) > 0 {
    24  		return m.Where(where[0], where[1:]...).Delete()
    25  	}
    26  	defer func() {
    27  		if err == nil {
    28  			m.checkAndRemoveSelectCache(ctx)
    29  		}
    30  	}()
    31  	var (
    32  		conditionWhere, conditionExtra, conditionArgs = m.formatCondition(ctx, false, false)
    33  		conditionStr                                  = conditionWhere + conditionExtra
    34  		fieldNameDelete, fieldTypeDelete              = m.softTimeMaintainer().GetFieldNameAndTypeForDelete(
    35  			ctx, "", m.tablesInit,
    36  		)
    37  	)
    38  	if m.unscoped {
    39  		fieldNameDelete = ""
    40  	}
    41  	if !gstr.ContainsI(conditionStr, " WHERE ") || (fieldNameDelete != "" && !gstr.ContainsI(conditionStr, " AND ")) {
    42  		intlog.Printf(
    43  			ctx,
    44  			`sql condition string "%s" has no WHERE for DELETE operation, fieldNameDelete: %s`,
    45  			conditionStr, fieldNameDelete,
    46  		)
    47  		return nil, gerror.NewCode(
    48  			gcode.CodeMissingParameter,
    49  			"there should be WHERE condition statement for DELETE operation",
    50  		)
    51  	}
    52  
    53  	// Soft deleting.
    54  	if fieldNameDelete != "" {
    55  		dataHolder, dataValue := m.softTimeMaintainer().GetDataByFieldNameAndTypeForDelete(
    56  			ctx, "", fieldNameDelete, fieldTypeDelete,
    57  		)
    58  		in := &HookUpdateInput{
    59  			internalParamHookUpdate: internalParamHookUpdate{
    60  				internalParamHook: internalParamHook{
    61  					link: m.getLink(true),
    62  				},
    63  				handler: m.hookHandler.Update,
    64  			},
    65  			Model:     m,
    66  			Table:     m.tables,
    67  			Data:      dataHolder,
    68  			Condition: conditionStr,
    69  			Args:      append([]interface{}{dataValue}, conditionArgs...),
    70  		}
    71  		return in.Next(ctx)
    72  	}
    73  
    74  	in := &HookDeleteInput{
    75  		internalParamHookDelete: internalParamHookDelete{
    76  			internalParamHook: internalParamHook{
    77  				link: m.getLink(true),
    78  			},
    79  			handler: m.hookHandler.Delete,
    80  		},
    81  		Model:     m,
    82  		Table:     m.tables,
    83  		Condition: conditionStr,
    84  		Args:      conditionArgs,
    85  	}
    86  	return in.Next(ctx)
    87  }