✏️ 优化代码结构

This commit is contained in:
fumiama
2021-12-21 22:09:43 +08:00
parent b953385bc5
commit 1d3a61386d
13 changed files with 136 additions and 364 deletions

View File

@@ -1,43 +0,0 @@
package main
import (
"os"
"github.com/FloatTech/ZeroBot-Plugin/utils/sql"
)
type book struct {
Id uint64 `db:"id"`
BookReview string `db:"bookreview"`
}
func main() {
db, err := Open(os.Args[1])
if err != nil {
panic(err)
}
newdb := &sql.Sqlite{DBPath: os.Args[2]}
err = newdb.Create("bookreview", &book{})
if err != nil {
panic(err)
}
rs, err := db.Table("book_review").Select("book_review", "").Rows()
if err != nil {
panic(err)
}
var d string
var i uint64
for rs.Next() {
err := rs.Scan(&d)
if err != nil {
panic(err)
}
i++
err = newdb.Insert("bookreview", &book{i, d})
if err != nil {
panic(err)
}
}
db.Close()
newdb.Close()
}

View File

@@ -1,46 +0,0 @@
package main
import (
"os"
"github.com/jinzhu/gorm"
_ "github.com/logoove/sqlite"
)
type BrDB = gorm.DB
func Initialize(dbpath string) *BrDB {
var err error
if _, err = os.Stat(dbpath); err != nil || os.IsNotExist(err) {
// 生成文件
f, err := os.Create(dbpath)
if err != nil {
return nil
}
defer f.Close()
}
gdb, err := gorm.Open("sqlite3", dbpath)
if err != nil {
panic(err)
}
gdb.AutoMigrate(&BookReview{})
return (*BrDB)(gdb)
}
func Open(dbpath string) (*BrDB, error) {
db, err := gorm.Open("sqlite3", dbpath)
if err != nil {
return nil, err
} else {
return (*BrDB)(db), nil
}
}
type BookReview struct {
gorm.Model
BookReview string `gorm:"column:book_review"`
}
func (BookReview) TableName() string {
return "book_review"
}