2023-09-02 11:47:53 +08:00
|
|
|
package database
|
|
|
|
|
|
|
|
import (
|
2023-09-04 18:00:09 +08:00
|
|
|
_ "github.com/mattn/go-sqlite3"
|
2023-09-02 11:47:53 +08:00
|
|
|
"xorm.io/xorm"
|
|
|
|
)
|
|
|
|
|
|
|
|
var engine *xorm.Engine
|
|
|
|
|
2023-09-04 18:00:09 +08:00
|
|
|
type Wol struct {
|
2023-09-13 14:43:05 +08:00
|
|
|
ID int64
|
2023-09-04 18:00:09 +08:00
|
|
|
Name string `xorm:"varchar(64)"`
|
|
|
|
Mac string `xorm:"varchar(64)"`
|
|
|
|
}
|
|
|
|
|
2023-09-02 16:05:31 +08:00
|
|
|
func init() {
|
2023-09-02 11:47:53 +08:00
|
|
|
var err error
|
2023-09-02 16:05:31 +08:00
|
|
|
file := "./wol.db"
|
2023-09-02 11:47:53 +08:00
|
|
|
engine, err = xorm.NewEngine("sqlite3", file)
|
|
|
|
if err != nil {
|
2023-09-13 14:43:05 +08:00
|
|
|
println(err.Error())
|
2023-09-02 11:47:53 +08:00
|
|
|
|
|
|
|
}
|
2023-09-13 17:32:27 +08:00
|
|
|
res, err := engine.IsTableExist("wol")
|
|
|
|
if !res {
|
|
|
|
CreateTable()
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
println(err.Error())
|
|
|
|
|
|
|
|
}
|
2023-09-04 18:00:09 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
func CreateTable() int {
|
|
|
|
err := engine.Sync2(new(Wol))
|
|
|
|
if err != nil {
|
2023-09-13 14:43:05 +08:00
|
|
|
println(err.Error())
|
2023-09-04 18:00:09 +08:00
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
func Getallwol(everyone *[]Wol) {
|
2023-09-05 17:53:38 +08:00
|
|
|
err := engine.Find(&(*everyone))
|
|
|
|
if err != nil {
|
2023-09-13 14:43:05 +08:00
|
|
|
println("Getallwol err", err.Error())
|
2023-09-05 17:53:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Getallwoltext() {
|
|
|
|
everyone := make([]Wol, 0)
|
2023-09-04 18:00:09 +08:00
|
|
|
err := engine.Find(&everyone)
|
|
|
|
if err != nil {
|
2023-09-13 14:43:05 +08:00
|
|
|
println("Getallwoltext", err.Error())
|
2023-09-05 17:53:38 +08:00
|
|
|
}
|
|
|
|
println(len(everyone))
|
|
|
|
}
|
|
|
|
|
2023-09-15 10:57:29 +08:00
|
|
|
// 返回插入的数量
|
2023-09-05 17:53:38 +08:00
|
|
|
func Insert(wol *Wol) {
|
2023-09-15 10:57:29 +08:00
|
|
|
_, err := engine.Insert(wol)
|
2023-09-05 17:53:38 +08:00
|
|
|
if err != nil {
|
2023-09-13 14:43:05 +08:00
|
|
|
println(err.Error())
|
2023-09-04 18:00:09 +08:00
|
|
|
}
|
2023-09-15 10:57:29 +08:00
|
|
|
|
2023-09-02 11:47:53 +08:00
|
|
|
}
|
2023-09-13 14:43:05 +08:00
|
|
|
func Del(id int64) {
|
2023-09-15 10:57:29 +08:00
|
|
|
_, err := engine.Where("i_d = ?", id).Delete(&Wol{})
|
|
|
|
if err != nil {
|
|
|
|
println(err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func Edi(wol *Wol, id int64) {
|
|
|
|
_, err := engine.Where("i_d = ?", id).Update(wol)
|
2023-09-08 10:34:03 +08:00
|
|
|
if err != nil {
|
2023-09-15 10:57:29 +08:00
|
|
|
println(err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 根据id查找
|
|
|
|
func GetWolById(id int64, wol *Wol) {
|
|
|
|
_, err := engine.Where("i_d = ?", id).Get(wol)
|
|
|
|
if err != nil {
|
|
|
|
println(err.Error())
|
2023-09-08 10:34:03 +08:00
|
|
|
}
|
|
|
|
}
|