2017-01-03 13:50:28 +05:30
|
|
|
// Copyright 2016 The Xorm Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package xorm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
|
2020-03-22 20:42:55 +05:30
|
|
|
"xorm.io/xorm/caches"
|
|
|
|
"xorm.io/xorm/schemas"
|
2017-01-03 13:50:28 +05:30
|
|
|
)
|
|
|
|
|
2020-03-22 20:42:55 +05:30
|
|
|
var (
|
|
|
|
// ErrNeedDeletedCond delete needs less one condition error
|
|
|
|
ErrNeedDeletedCond = errors.New("Delete action needs at least one condition")
|
|
|
|
|
|
|
|
// ErrNotImplemented not implemented
|
|
|
|
ErrNotImplemented = errors.New("Not implemented")
|
|
|
|
)
|
|
|
|
|
|
|
|
func (session *Session) cacheDelete(table *schemas.Table, tableName, sqlStr string, args ...interface{}) error {
|
2017-09-13 10:48:22 +05:30
|
|
|
if table == nil ||
|
2017-08-22 17:09:52 +05:30
|
|
|
session.tx != nil {
|
2017-01-03 13:50:28 +05:30
|
|
|
return ErrCacheFailed
|
|
|
|
}
|
|
|
|
|
2017-08-22 17:09:52 +05:30
|
|
|
for _, filter := range session.engine.dialect.Filters() {
|
2020-03-22 20:42:55 +05:30
|
|
|
sqlStr = filter.Do(sqlStr)
|
2017-01-03 13:50:28 +05:30
|
|
|
}
|
|
|
|
|
2020-03-22 20:42:55 +05:30
|
|
|
newsql := session.statement.ConvertIDSQL(sqlStr)
|
2017-01-03 13:50:28 +05:30
|
|
|
if newsql == "" {
|
|
|
|
return ErrCacheFailed
|
|
|
|
}
|
|
|
|
|
2020-03-22 20:42:55 +05:30
|
|
|
cacher := session.engine.cacherMgr.GetCacher(tableName)
|
2017-09-13 10:48:22 +05:30
|
|
|
pkColumns := table.PKColumns()
|
2020-03-22 20:42:55 +05:30
|
|
|
ids, err := caches.GetCacheSql(cacher, tableName, newsql, args)
|
2017-01-03 13:50:28 +05:30
|
|
|
if err != nil {
|
2017-09-13 10:48:22 +05:30
|
|
|
resultsSlice, err := session.queryBytes(newsql, args...)
|
2017-01-03 13:50:28 +05:30
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-03-22 20:42:55 +05:30
|
|
|
ids = make([]schemas.PK, 0)
|
2017-01-03 13:50:28 +05:30
|
|
|
if len(resultsSlice) > 0 {
|
|
|
|
for _, data := range resultsSlice {
|
|
|
|
var id int64
|
2020-03-22 20:42:55 +05:30
|
|
|
var pk schemas.PK = make([]interface{}, 0)
|
2017-09-13 10:48:22 +05:30
|
|
|
for _, col := range pkColumns {
|
2017-01-03 13:50:28 +05:30
|
|
|
if v, ok := data[col.Name]; !ok {
|
|
|
|
return errors.New("no id")
|
|
|
|
} else if col.SQLType.IsText() {
|
|
|
|
pk = append(pk, string(v))
|
|
|
|
} else if col.SQLType.IsNumeric() {
|
|
|
|
id, err = strconv.ParseInt(string(v), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
pk = append(pk, id)
|
|
|
|
} else {
|
|
|
|
return errors.New("not supported primary key type")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ids = append(ids, pk)
|
|
|
|
}
|
|
|
|
}
|
2017-09-13 10:48:22 +05:30
|
|
|
}
|
2017-01-03 13:50:28 +05:30
|
|
|
|
|
|
|
for _, id := range ids {
|
2020-03-22 20:42:55 +05:30
|
|
|
session.engine.logger.Debugf("[cache] delete cache obj: %v, %v", tableName, id)
|
2017-01-03 13:50:28 +05:30
|
|
|
sid, err := id.ToString()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
cacher.DelBean(tableName, sid)
|
|
|
|
}
|
2020-03-22 20:42:55 +05:30
|
|
|
session.engine.logger.Debugf("[cache] clear cache table: %v", tableName)
|
2017-01-03 13:50:28 +05:30
|
|
|
cacher.ClearIds(tableName)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete records, bean's non-empty fields are conditions
|
2021-08-13 04:41:42 +05:30
|
|
|
func (session *Session) Delete(beans ...interface{}) (int64, error) {
|
2017-08-22 17:09:52 +05:30
|
|
|
if session.isAutoClose {
|
2017-01-03 13:50:28 +05:30
|
|
|
defer session.Close()
|
|
|
|
}
|
|
|
|
|
2020-03-22 20:42:55 +05:30
|
|
|
if session.statement.LastError != nil {
|
|
|
|
return 0, session.statement.LastError
|
2019-06-23 20:52:43 +05:30
|
|
|
}
|
|
|
|
|
2021-08-13 04:41:42 +05:30
|
|
|
var (
|
|
|
|
condSQL string
|
|
|
|
condArgs []interface{}
|
|
|
|
err error
|
|
|
|
bean interface{}
|
|
|
|
)
|
|
|
|
if len(beans) > 0 {
|
|
|
|
bean = beans[0]
|
|
|
|
if err = session.statement.SetRefBean(bean); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2017-01-03 13:50:28 +05:30
|
|
|
|
2021-08-13 04:41:42 +05:30
|
|
|
executeBeforeClosures(session, bean)
|
2017-01-03 13:50:28 +05:30
|
|
|
|
2021-08-13 04:41:42 +05:30
|
|
|
if processor, ok := interface{}(bean).(BeforeDeleteProcessor); ok {
|
|
|
|
processor.BeforeDelete()
|
|
|
|
}
|
2017-01-03 13:50:28 +05:30
|
|
|
|
2021-08-13 04:41:42 +05:30
|
|
|
condSQL, condArgs, err = session.statement.GenConds(bean)
|
|
|
|
} else {
|
|
|
|
condSQL, condArgs, err = session.statement.GenCondSQL(session.statement.Conds())
|
|
|
|
}
|
2017-08-22 17:09:52 +05:30
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2021-08-13 04:41:42 +05:30
|
|
|
|
2020-03-22 20:42:55 +05:30
|
|
|
pLimitN := session.statement.LimitN
|
|
|
|
if len(condSQL) == 0 && (pLimitN == nil || *pLimitN == 0) {
|
2017-01-03 13:50:28 +05:30
|
|
|
return 0, ErrNeedDeletedCond
|
|
|
|
}
|
|
|
|
|
2017-09-13 10:48:22 +05:30
|
|
|
var tableNameNoQuote = session.statement.TableName()
|
|
|
|
var tableName = session.engine.Quote(tableNameNoQuote)
|
|
|
|
var table = session.statement.RefTable
|
2017-01-03 13:50:28 +05:30
|
|
|
var deleteSQL string
|
|
|
|
if len(condSQL) > 0 {
|
|
|
|
deleteSQL = fmt.Sprintf("DELETE FROM %v WHERE %v", tableName, condSQL)
|
|
|
|
} else {
|
|
|
|
deleteSQL = fmt.Sprintf("DELETE FROM %v", tableName)
|
|
|
|
}
|
|
|
|
|
|
|
|
var orderSQL string
|
2017-08-22 17:09:52 +05:30
|
|
|
if len(session.statement.OrderStr) > 0 {
|
|
|
|
orderSQL += fmt.Sprintf(" ORDER BY %s", session.statement.OrderStr)
|
2017-01-03 13:50:28 +05:30
|
|
|
}
|
2020-03-22 20:42:55 +05:30
|
|
|
if pLimitN != nil && *pLimitN > 0 {
|
|
|
|
limitNValue := *pLimitN
|
|
|
|
orderSQL += fmt.Sprintf(" LIMIT %d", limitNValue)
|
2017-01-03 13:50:28 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if len(orderSQL) > 0 {
|
2020-03-22 20:42:55 +05:30
|
|
|
switch session.engine.dialect.URI().DBType {
|
|
|
|
case schemas.POSTGRES:
|
2017-01-03 13:50:28 +05:30
|
|
|
inSQL := fmt.Sprintf("ctid IN (SELECT ctid FROM %s%s)", tableName, orderSQL)
|
|
|
|
if len(condSQL) > 0 {
|
|
|
|
deleteSQL += " AND " + inSQL
|
|
|
|
} else {
|
|
|
|
deleteSQL += " WHERE " + inSQL
|
|
|
|
}
|
2020-03-22 20:42:55 +05:30
|
|
|
case schemas.SQLITE:
|
2017-01-03 13:50:28 +05:30
|
|
|
inSQL := fmt.Sprintf("rowid IN (SELECT rowid FROM %s%s)", tableName, orderSQL)
|
|
|
|
if len(condSQL) > 0 {
|
|
|
|
deleteSQL += " AND " + inSQL
|
|
|
|
} else {
|
|
|
|
deleteSQL += " WHERE " + inSQL
|
|
|
|
}
|
2020-03-22 20:42:55 +05:30
|
|
|
// TODO: how to handle delete limit on mssql?
|
|
|
|
case schemas.MSSQL:
|
2017-01-03 13:50:28 +05:30
|
|
|
return 0, ErrNotImplemented
|
|
|
|
default:
|
|
|
|
deleteSQL += orderSQL
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var realSQL string
|
|
|
|
argsForCache := make([]interface{}, 0, len(condArgs)*2)
|
2021-08-13 04:41:42 +05:30
|
|
|
if session.statement.GetUnscoped() || table == nil || table.DeletedColumn() == nil { // tag "deleted" is disabled
|
2017-01-03 13:50:28 +05:30
|
|
|
realSQL = deleteSQL
|
|
|
|
copy(argsForCache, condArgs)
|
|
|
|
argsForCache = append(condArgs, argsForCache...)
|
|
|
|
} else {
|
2020-03-22 20:42:55 +05:30
|
|
|
// !oinume! sqlStrForCache and argsForCache is needed to behave as executing "DELETE FROM ..." for caches.
|
2017-01-03 13:50:28 +05:30
|
|
|
copy(argsForCache, condArgs)
|
|
|
|
argsForCache = append(condArgs, argsForCache...)
|
|
|
|
|
|
|
|
deletedColumn := table.DeletedColumn()
|
|
|
|
realSQL = fmt.Sprintf("UPDATE %v SET %v = ? WHERE %v",
|
2017-08-22 17:09:52 +05:30
|
|
|
session.engine.Quote(session.statement.TableName()),
|
|
|
|
session.engine.Quote(deletedColumn.Name),
|
2017-01-03 13:50:28 +05:30
|
|
|
condSQL)
|
|
|
|
|
|
|
|
if len(orderSQL) > 0 {
|
2020-03-22 20:42:55 +05:30
|
|
|
switch session.engine.dialect.URI().DBType {
|
|
|
|
case schemas.POSTGRES:
|
2017-01-03 13:50:28 +05:30
|
|
|
inSQL := fmt.Sprintf("ctid IN (SELECT ctid FROM %s%s)", tableName, orderSQL)
|
|
|
|
if len(condSQL) > 0 {
|
|
|
|
realSQL += " AND " + inSQL
|
|
|
|
} else {
|
|
|
|
realSQL += " WHERE " + inSQL
|
|
|
|
}
|
2020-03-22 20:42:55 +05:30
|
|
|
case schemas.SQLITE:
|
2017-01-03 13:50:28 +05:30
|
|
|
inSQL := fmt.Sprintf("rowid IN (SELECT rowid FROM %s%s)", tableName, orderSQL)
|
|
|
|
if len(condSQL) > 0 {
|
|
|
|
realSQL += " AND " + inSQL
|
|
|
|
} else {
|
|
|
|
realSQL += " WHERE " + inSQL
|
|
|
|
}
|
2020-03-22 20:42:55 +05:30
|
|
|
// TODO: how to handle delete limit on mssql?
|
|
|
|
case schemas.MSSQL:
|
2017-01-03 13:50:28 +05:30
|
|
|
return 0, ErrNotImplemented
|
|
|
|
default:
|
|
|
|
realSQL += orderSQL
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-01 22:22:35 +05:30
|
|
|
// !oinume! Insert nowTime to the head of session.statement.Params
|
2017-01-03 13:50:28 +05:30
|
|
|
condArgs = append(condArgs, "")
|
|
|
|
paramsLen := len(condArgs)
|
|
|
|
copy(condArgs[1:paramsLen], condArgs[0:paramsLen-1])
|
|
|
|
|
2021-08-13 04:41:42 +05:30
|
|
|
val, t, err := session.engine.nowTime(deletedColumn)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2017-01-03 13:50:28 +05:30
|
|
|
condArgs[0] = val
|
|
|
|
|
|
|
|
var colName = deletedColumn.Name
|
|
|
|
session.afterClosures = append(session.afterClosures, func(bean interface{}) {
|
|
|
|
col := table.GetColumn(colName)
|
|
|
|
setColumnTime(bean, col, t)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-03-22 20:42:55 +05:30
|
|
|
if cacher := session.engine.GetCacher(tableNameNoQuote); cacher != nil && session.statement.UseCache {
|
2017-09-13 10:48:22 +05:30
|
|
|
session.cacheDelete(table, tableNameNoQuote, deleteSQL, argsForCache...)
|
2017-01-03 13:50:28 +05:30
|
|
|
}
|
|
|
|
|
2017-09-13 10:48:22 +05:30
|
|
|
session.statement.RefTable = table
|
2017-01-03 13:50:28 +05:30
|
|
|
res, err := session.exec(realSQL, condArgs...)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2021-08-13 04:41:42 +05:30
|
|
|
if bean != nil {
|
|
|
|
// handle after delete processors
|
|
|
|
if session.isAutoCommit {
|
|
|
|
for _, closure := range session.afterClosures {
|
|
|
|
closure(bean)
|
|
|
|
}
|
|
|
|
if processor, ok := interface{}(bean).(AfterDeleteProcessor); ok {
|
|
|
|
processor.AfterDelete()
|
2017-01-03 13:50:28 +05:30
|
|
|
}
|
|
|
|
} else {
|
2021-08-13 04:41:42 +05:30
|
|
|
lenAfterClosures := len(session.afterClosures)
|
|
|
|
if lenAfterClosures > 0 && len(beans) > 0 {
|
|
|
|
if value, has := session.afterDeleteBeans[beans[0]]; has && value != nil {
|
|
|
|
*value = append(*value, session.afterClosures...)
|
|
|
|
} else {
|
|
|
|
afterClosures := make([]func(interface{}), lenAfterClosures)
|
|
|
|
copy(afterClosures, session.afterClosures)
|
|
|
|
session.afterDeleteBeans[bean] = &afterClosures
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if _, ok := interface{}(bean).(AfterDeleteProcessor); ok {
|
|
|
|
session.afterDeleteBeans[bean] = nil
|
|
|
|
}
|
2017-01-03 13:50:28 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cleanupProcessorsClosures(&session.afterClosures)
|
|
|
|
// --
|
|
|
|
|
|
|
|
return res.RowsAffected()
|
|
|
|
}
|