MCPcopy Create free account
hub / github.com/FloatTech/ZeroBot-Plugin-Playground / initialize

Function initialize

plugin/chinesebqb/model.go:35–71  ·  view source on GitHub ↗

initialize 初始化

(dbpath string)

Source from the content-addressed store, hash-verified

33
34// initialize 初始化
35func initialize(dbpath string) *bqbdb {
36 var err error
37 if _, err = os.Stat(dbpath); err != nil || os.IsNotExist(err) {
38 // 生成文件
39 f, err := os.Create(dbpath)
40 if err != nil {
41 return nil
42 }
43 defer f.Close()
44 }
45 db, err := gorm.Open(sqlite.Open(dbpath), &gorm.Config{})
46 if err != nil {
47 panic(err)
48 }
49 bdb := (*bqbdb)(db)
50 err = db.AutoMigrate(&bqb{})
51 if err != nil {
52 panic(err)
53 }
54 var count int64
55 err = db.Model(&bqb{}).Count(&count).Error
56 if err != nil {
57 panic(err)
58 }
59 if count == 0 {
60 err = bdb.truncateAndInsert()
61 if err != nil {
62 panic(err)
63 }
64 }
65 err = db.Model(&bqb{}).Count(&count).Error
66 if err != nil {
67 panic(err)
68 }
69 logrus.Infoln("[chinesebqb]加载", count, "条表情包")
70 return (*bqbdb)(db)
71}
72
73// bqb 表情包数据
74type bqb struct {

Callers 1

initFunction · 0.70

Calls 1

truncateAndInsertMethod · 0.80

Tested by

no test coverage detected