xorm-删除和软删除实例

  • 删除数据Delete方法,参数为struct的指针并且成为查询条件。注意:当删除时,如果user中包含有bool,float64或者float32类型,有可能会使删除失败
package main

import (
	"fmt"
	_ "github.com/go-sql-driver/mysql"
	"github.com/go-xorm/xorm"
	"log"
)

var engine *xorm.Engine

type User struct {
	Name      string    `xorm:"varchar(25) 'name'"`
	Id        int       `xorm:"pk 'id' autoincr"`
}

func main() {
	var err error
	engine, err = xorm.NewEngine("mysql", "root:123456@/test")
	if err != nil {
		log.Fatal(err)
		return
	}
	//创建表和插入数据
	/*err = engine.CreateTables(User{})
	if err != nil {
		log.Fatal(err)
		return
	}

	u := make([]User, 3)
	u[0].Name = "u0"
	u[1].Name = "u1"
	u[2].Name = "u2"

	_, err = engine.Insert(u)
	if err != nil {
		log.Fatal(err)
		return
	}*/

	uu := new(User)
	has ,err :=engine.Id(2).Get(uu)
	if err !=nil {
		log.Fatal(err)
		return
	}
	if has {
		fmt.Println("删除Deleted,用户名:",uu.Name)
	}else {
		fmt.Println("该数据现在已经不存在")
	}

	affected, err := engine.Id(2).Delete(uu)
	if err != nil {
		log.Fatal(err)
		return
	}
	fmt.Println("删除Delete,受影响的行数:", affected)
	fmt.Println()


	has ,err =engine.Id(2).Get(uu)
	if err !=nil {
		log.Fatal(err)
		return
	}
	if has {
		fmt.Println("删除Deleted,用户名:",uu.Name,"deleteAt:")
	}else {
		fmt.Println("该数据现在已经不存在")
	}


	affected, err = engine.Id(2).Delete(uu)
	if err != nil {
		log.Fatal(err)
		return
	}
	fmt.Println("删除Delete,受影响的行数:", affected)
	fmt.Println()


/*
  输出:
	删除Deleted,用户名: u1
	删除Delete,受影响的行数: 1

	该数据现在已经不存在
	删除Delete,受影响的行数: 0
*/

}
  • 软删除Deleted, Deleted可以让您不真正的删除数据,而是标记一个删除时间。使用此特性需要在xorm标记中使用deleted标记,对应的字段必须为time.Time类型。在Delete()时,deleted标记的字段将会被自动更新为当前时间而不是去删除该条记录

    package main
    
    import (
    	"fmt"
    	_ "github.com/go-sql-driver/mysql"
    	"github.com/go-xorm/xorm"
    	"log"
    	"time"
    )
    
    var engine *xorm.Engine
    
    type User struct {
    	Name      string    `xorm:"varchar(25) 'name'"`
    	Id        int       `xorm:"pk 'id' autoincr"`
    	Version   int       `xorm:"version"`
    	UpdatedAt time.Time `xorm:"updated"`
    	DeletedAt time.Time `xorm:"deleted"`
    }
    
    func main() {
    	var err error
    	engine, err = xorm.NewEngine("mysql", "root:123456@/test")
    	if err != nil {
    		log.Fatal(err)
    		return
    	}
    	//创建表和插入数据
    /*	err = engine.CreateTables(User{})
    	if err != nil {
    		log.Fatal(err)
    		return
    	}
    
    	u := make([]User, 3)
    	u[0].Name = "u0"
    	u[1].Name = "u1"
    	u[2].Name = "u2"
    
    	_, err = engine.Insert(u)
    	if err != nil {
    		log.Fatal(err)
    		return
    	}*/
    
    	uu := new(User)
    	has ,err :=engine.Id(2).Get(uu)
    	if err !=nil {
    		log.Fatal(err)
    		return
    	}
    	if has {
    		fmt.Println("软删除Deleted,用户名:",uu.Name,"deleteAt:",uu.DeletedAt)
    	}else {
    		fmt.Println("该数据现在已经不存在")
    	}
    
    	affected, err := engine.Id(2).Delete(uu)
    	if err != nil {
    		log.Fatal(err)
    		return
    	}
    	fmt.Println("软删除Delete,受影响的行数:", affected)
    	fmt.Println()
    
    
    	has ,err =engine.Id(2).Get(uu)
    	if err !=nil {
    		log.Fatal(err)
    		return
    	}
    	if has {
    		fmt.Println("软删除Deleted,用户名:",uu.Name,"deleteAt:",uu.DeletedAt)
    	}else {
    		fmt.Println("该数据现在已经不存在")
    	}
    
    
    	affected, err = engine.Id(2).Delete(uu)
    	if err != nil {
    		log.Fatal(err)
    		return
    	}
    	fmt.Println("软删除Delete,受影响的行数:", affected)
    	fmt.Println()
    
    
    	//那么如果记录已经被标记为删除后,要真正的获得该条记录或者真正的删除该条记录,需要启用Unscoped,如下所示:
    	// 此时将可以获得记录,此时将可以真正的删除记录
    
    	has ,err =engine.Id(2).Unscoped().Get(uu)
    	if err !=nil {
    		log.Fatal(err)
    		return
    	}
    	if has {
    		fmt.Println("启用Unscoped,用户名:",uu.Name,"deleteAt:",uu.DeletedAt)
    	}else {
    		fmt.Println("该数据现在已经不存在")
    	}
    
    	affected, err = engine.Id(2).Unscoped().Delete(uu)
    	if err != nil {
    		log.Fatal(err)
    		return
    	}
    	fmt.Println("启用Unscoped,受影响的行数:", affected)
    
    /*
    输出:
    	软删除Deleted,用户名: u1 deleteAt: 0001-01-01 00:00:00 +0000 UTC
    	软删除Deleted,受影响的行数: 1
    
    	该数据现在已经不存在
    	软删除Deleted,受影响的行数: 0
    
    	启用Unscoped,用户名: u1 deleteAt: 2019-05-28 15:12:38 +0800 CST
    	启用Unscoped,受影响的行数: 1
    */
    
    }
    

posted on 2019-05-28 15:30  j_x_x  阅读(1229)  评论(0编辑  收藏  举报

导航