mirror of https://github.com/go-gitea/gitea
Git with a cup of tea, painless self-hosted git service
Mirror for internal git.with.parts use
https://git.with.parts
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
245 lines
6.6 KiB
245 lines
6.6 KiB
8 years ago
|
// 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"
|
||
|
|
||
6 years ago
|
"xorm.io/core"
|
||
8 years ago
|
)
|
||
|
|
||
7 years ago
|
func (session *Session) cacheDelete(table *core.Table, tableName, sqlStr string, args ...interface{}) error {
|
||
|
if table == nil ||
|
||
7 years ago
|
session.tx != nil {
|
||
8 years ago
|
return ErrCacheFailed
|
||
|
}
|
||
|
|
||
7 years ago
|
for _, filter := range session.engine.dialect.Filters() {
|
||
7 years ago
|
sqlStr = filter.Do(sqlStr, session.engine.dialect, table)
|
||
8 years ago
|
}
|
||
|
|
||
7 years ago
|
newsql := session.statement.convertIDSQL(sqlStr)
|
||
8 years ago
|
if newsql == "" {
|
||
|
return ErrCacheFailed
|
||
|
}
|
||
|
|
||
6 years ago
|
cacher := session.engine.getCacher(tableName)
|
||
7 years ago
|
pkColumns := table.PKColumns()
|
||
8 years ago
|
ids, err := core.GetCacheSql(cacher, tableName, newsql, args)
|
||
|
if err != nil {
|
||
7 years ago
|
resultsSlice, err := session.queryBytes(newsql, args...)
|
||
8 years ago
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
ids = make([]core.PK, 0)
|
||
|
if len(resultsSlice) > 0 {
|
||
|
for _, data := range resultsSlice {
|
||
|
var id int64
|
||
|
var pk core.PK = make([]interface{}, 0)
|
||
7 years ago
|
for _, col := range pkColumns {
|
||
8 years ago
|
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)
|
||
|
}
|
||
|
}
|
||
7 years ago
|
}
|
||
8 years ago
|
|
||
|
for _, id := range ids {
|
||
7 years ago
|
session.engine.logger.Debug("[cacheDelete] delete cache obj:", tableName, id)
|
||
8 years ago
|
sid, err := id.ToString()
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
cacher.DelBean(tableName, sid)
|
||
|
}
|
||
7 years ago
|
session.engine.logger.Debug("[cacheDelete] clear cache table:", tableName)
|
||
8 years ago
|
cacher.ClearIds(tableName)
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
// Delete records, bean's non-empty fields are conditions
|
||
|
func (session *Session) Delete(bean interface{}) (int64, error) {
|
||
7 years ago
|
if session.isAutoClose {
|
||
8 years ago
|
defer session.Close()
|
||
|
}
|
||
|
|
||
6 years ago
|
if session.statement.lastError != nil {
|
||
|
return 0, session.statement.lastError
|
||
|
}
|
||
|
|
||
6 years ago
|
if err := session.statement.setRefBean(bean); err != nil {
|
||
8 years ago
|
return 0, err
|
||
|
}
|
||
8 years ago
|
|
||
|
// handle before delete processors
|
||
|
for _, closure := range session.beforeClosures {
|
||
|
closure(bean)
|
||
|
}
|
||
|
cleanupProcessorsClosures(&session.beforeClosures)
|
||
|
|
||
|
if processor, ok := interface{}(bean).(BeforeDeleteProcessor); ok {
|
||
|
processor.BeforeDelete()
|
||
|
}
|
||
|
|
||
7 years ago
|
condSQL, condArgs, err := session.statement.genConds(bean)
|
||
|
if err != nil {
|
||
|
return 0, err
|
||
|
}
|
||
|
if len(condSQL) == 0 && session.statement.LimitN == 0 {
|
||
8 years ago
|
return 0, ErrNeedDeletedCond
|
||
|
}
|
||
|
|
||
7 years ago
|
var tableNameNoQuote = session.statement.TableName()
|
||
|
var tableName = session.engine.Quote(tableNameNoQuote)
|
||
|
var table = session.statement.RefTable
|
||
8 years ago
|
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
|
||
7 years ago
|
if len(session.statement.OrderStr) > 0 {
|
||
|
orderSQL += fmt.Sprintf(" ORDER BY %s", session.statement.OrderStr)
|
||
8 years ago
|
}
|
||
7 years ago
|
if session.statement.LimitN > 0 {
|
||
|
orderSQL += fmt.Sprintf(" LIMIT %d", session.statement.LimitN)
|
||
8 years ago
|
}
|
||
|
|
||
|
if len(orderSQL) > 0 {
|
||
7 years ago
|
switch session.engine.dialect.DBType() {
|
||
8 years ago
|
case core.POSTGRES:
|
||
|
inSQL := fmt.Sprintf("ctid IN (SELECT ctid FROM %s%s)", tableName, orderSQL)
|
||
|
if len(condSQL) > 0 {
|
||
|
deleteSQL += " AND " + inSQL
|
||
|
} else {
|
||
|
deleteSQL += " WHERE " + inSQL
|
||
|
}
|
||
|
case core.SQLITE:
|
||
|
inSQL := fmt.Sprintf("rowid IN (SELECT rowid FROM %s%s)", tableName, orderSQL)
|
||
|
if len(condSQL) > 0 {
|
||
|
deleteSQL += " AND " + inSQL
|
||
|
} else {
|
||
|
deleteSQL += " WHERE " + inSQL
|
||
|
}
|
||
|
// TODO: how to handle delete limit on mssql?
|
||
|
case core.MSSQL:
|
||
|
return 0, ErrNotImplemented
|
||
|
default:
|
||
|
deleteSQL += orderSQL
|
||
|
}
|
||
|
}
|
||
|
|
||
|
var realSQL string
|
||
|
argsForCache := make([]interface{}, 0, len(condArgs)*2)
|
||
7 years ago
|
if session.statement.unscoped || table.DeletedColumn() == nil { // tag "deleted" is disabled
|
||
8 years ago
|
realSQL = deleteSQL
|
||
|
copy(argsForCache, condArgs)
|
||
|
argsForCache = append(condArgs, argsForCache...)
|
||
|
} else {
|
||
|
// !oinume! sqlStrForCache and argsForCache is needed to behave as executing "DELETE FROM ..." for cache.
|
||
|
copy(argsForCache, condArgs)
|
||
|
argsForCache = append(condArgs, argsForCache...)
|
||
|
|
||
|
deletedColumn := table.DeletedColumn()
|
||
|
realSQL = fmt.Sprintf("UPDATE %v SET %v = ? WHERE %v",
|
||
7 years ago
|
session.engine.Quote(session.statement.TableName()),
|
||
|
session.engine.Quote(deletedColumn.Name),
|
||
8 years ago
|
condSQL)
|
||
|
|
||
|
if len(orderSQL) > 0 {
|
||
7 years ago
|
switch session.engine.dialect.DBType() {
|
||
8 years ago
|
case core.POSTGRES:
|
||
|
inSQL := fmt.Sprintf("ctid IN (SELECT ctid FROM %s%s)", tableName, orderSQL)
|
||
|
if len(condSQL) > 0 {
|
||
|
realSQL += " AND " + inSQL
|
||
|
} else {
|
||
|
realSQL += " WHERE " + inSQL
|
||
|
}
|
||
|
case core.SQLITE:
|
||
|
inSQL := fmt.Sprintf("rowid IN (SELECT rowid FROM %s%s)", tableName, orderSQL)
|
||
|
if len(condSQL) > 0 {
|
||
|
realSQL += " AND " + inSQL
|
||
|
} else {
|
||
|
realSQL += " WHERE " + inSQL
|
||
|
}
|
||
|
// TODO: how to handle delete limit on mssql?
|
||
|
case core.MSSQL:
|
||
|
return 0, ErrNotImplemented
|
||
|
default:
|
||
|
realSQL += orderSQL
|
||
|
}
|
||
|
}
|
||
|
|
||
7 years ago
|
// !oinume! Insert nowTime to the head of session.statement.Params
|
||
8 years ago
|
condArgs = append(condArgs, "")
|
||
|
paramsLen := len(condArgs)
|
||
|
copy(condArgs[1:paramsLen], condArgs[0:paramsLen-1])
|
||
|
|
||
7 years ago
|
val, t := session.engine.nowTime(deletedColumn)
|
||
8 years ago
|
condArgs[0] = val
|
||
|
|
||
|
var colName = deletedColumn.Name
|
||
|
session.afterClosures = append(session.afterClosures, func(bean interface{}) {
|
||
|
col := table.GetColumn(colName)
|
||
|
setColumnTime(bean, col, t)
|
||
|
})
|
||
|
}
|
||
|
|
||
6 years ago
|
if cacher := session.engine.getCacher(tableNameNoQuote); cacher != nil && session.statement.UseCache {
|
||
7 years ago
|
session.cacheDelete(table, tableNameNoQuote, deleteSQL, argsForCache...)
|
||
8 years ago
|
}
|
||
|
|
||
7 years ago
|
session.statement.RefTable = table
|
||
8 years ago
|
res, err := session.exec(realSQL, condArgs...)
|
||
|
if err != nil {
|
||
|
return 0, err
|
||
|
}
|
||
|
|
||
|
// handle after delete processors
|
||
7 years ago
|
if session.isAutoCommit {
|
||
8 years ago
|
for _, closure := range session.afterClosures {
|
||
|
closure(bean)
|
||
|
}
|
||
|
if processor, ok := interface{}(bean).(AfterDeleteProcessor); ok {
|
||
|
processor.AfterDelete()
|
||
|
}
|
||
|
} else {
|
||
|
lenAfterClosures := len(session.afterClosures)
|
||
|
if lenAfterClosures > 0 {
|
||
|
if value, has := session.afterDeleteBeans[bean]; has && value != nil {
|
||
|
*value = append(*value, session.afterClosures...)
|
||
|
} else {
|
||
|
afterClosures := make([]func(interface{}), lenAfterClosures)
|
||
|
copy(afterClosures, session.afterClosures)
|
||
|
session.afterDeleteBeans[bean] = &afterClosures
|
||
|
}
|
||
|
} else {
|
||
8 years ago
|
if _, ok := interface{}(bean).(AfterDeleteProcessor); ok {
|
||
8 years ago
|
session.afterDeleteBeans[bean] = nil
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
cleanupProcessorsClosures(&session.afterClosures)
|
||
|
// --
|
||
|
|
||
|
return res.RowsAffected()
|
||
|
}
|