Skip to content
This repository has been archived by the owner on Jun 22, 2023. It is now read-only.

Commit

Permalink
add 内置数据库模块
Browse files Browse the repository at this point in the history
  • Loading branch information
mcoo committed Jul 21, 2021
1 parent 232540f commit d1fea21
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 0 deletions.
10 changes: 10 additions & 0 deletions Config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ type CoreConfigStruct struct {
Url string
QQ int64
}
DBConfig struct{
DBType string
DBUserName string
DBPassword string
DBIP string
DBPort string
DBName string
}
Debug bool
BiliLive bool
YiQing bool
ReverseProxy string
Expand Down Expand Up @@ -160,4 +169,5 @@ func init() {
if err != nil {
log.Fatal(err)
}
dbInit()
}
140 changes: 140 additions & 0 deletions Config/dbConfig.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
package Config

import (
"database/sql"
"errors"
"fmt"
"gorm.io/driver/mysql"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)

var (
DB *gorm.DB
sqlDb *sql.DB
//WithUps = SqlArg{ArgId: 1}
//WithJobs = SqlArg{ArgId: 2}
//WithFanjus = SqlArg{ArgId: 3}
//WithGroups = SqlArg{ArgId: 4}
)

func dbInit() {
var err error
conn := ""
if CoreConfig.DBConfig.DBType == "mysql" {
conn = fmt.Sprintf("%v:%v@tcp(%v:%v)/%v?parseTime=True&loc=Local", CoreConfig.DBConfig.DBUserName, CoreConfig.DBConfig.DBPassword,CoreConfig.DBConfig.DBIP,CoreConfig.DBConfig.DBPort, CoreConfig.DBConfig.DBName)
DB, err = gorm.Open(mysql.Open(conn), &gorm.Config{})
} else if CoreConfig.DBConfig.DBType == "sqlite3" {
conn = fmt.Sprintf("./%v.DB", CoreConfig.DBConfig.DBName)
DB, err = gorm.Open(sqlite.Open(conn), &gorm.Config{})
} else {
panic(errors.New("not supported database adapter"))
}
if err != nil {
panic(err)
}
sqlDb, err = DB.DB()
if err == nil {
sqlDb.SetMaxIdleConns(10)
sqlDb.SetMaxOpenConns(100)
}
//err = DB.AutoMigrate(&QQGroup{}, &SQLUp{}, &SQLFanju{},&SQLJob{})
//if err != nil {
// log.Println(err)
//}
}

//type QQGroup struct {
// GroupID int64 `gorm:"primaryKey"`
// Enable bool
// AdminUin int64
// Menu string
// MenuKeyWord string
// ShutUpWord string
// ShutUpTime int
// JoinVerifyTime int
// JoinAutoShutUpTime int
// Zan bool
// SignIn bool
// Bili bool
// BiliUps []*SQLUp `gorm:"many2many:up_groups;foreignKey:GroupID"`
// Fanjus []*SQLFanju `gorm:"many2many:fanju_groups;foreignKey:GroupID"`
// Welcome string
// JoinVerifyType int
// Job []*SQLJob `gorm:"many2many:job_groups;foreignKey:GroupID"`
//}
//type SQLUp struct {
// Name string
// Created int64
// UserId int64 `gorm:"primaryKey"`
// Groups []*QQGroup `gorm:"many2many:up_groups;foreignKey:UserId"`
//}
//type SQLJob struct {
// gorm.Model
// Cron string
// Type int
// Title string
// Content string
// Groups []*QQGroup `gorm:"many2many:job_groups;"`
//}
//type SQLFanju struct {
// Title string
// Id int64
// UserId int64
// Groups []*QQGroup `gorm:"many2many:fanju_groups;foreignKey:Id"`
//}
//type SqlArg struct {
// ArgId int
//}
//func SubUp(groupId,upId int64) {
//
//}
//func DelUpSub(groupId int64,upId int64) error {
// if upId == 0 || groupId == 0 {
// return errors.New("参数非法")
// }
// c := DB
// if CoreConfig.Debug {
// c = c.Debug()
// }
// err := c.Model(&QQGroup{GroupID: groupId}).Association("BiliUps").Delete(SQLUp{UserId: upId})
// if err != nil {
// return err
// }
// if DB.Model(&SQLUp{UserId: upId}).Association("Groups").Count() == 0 {
// err = DB.Delete(&SQLUp{},upId).Error
// }
// return err
//}
//func GetGroupConfig(groupId int64,args ... SqlArg) (g QQGroup,e error) {
// c := DB
// if CoreConfig.Debug {
// c = c.Debug()
// }
// for _,v := range args {
// switch v.ArgId {
// case 1:
// c = c.Preload("BiliUps")
// case 2:
// c = c.Preload("Job")
// case 3:
// c = c.Preload("Fanjus")
// }
// }
// e = c.Where("group_id = ?",groupId).First(&g).Error
// return
//}
//func GetBiliUps(userId int64,args ... SqlArg) (g SQLUp,e error) {
// c := DB
// if CoreConfig.Debug {
// c = c.Debug()
// }
// for _,v := range args {
// switch v.ArgId {
// case 4:
// c = c.Preload("Groups")
// }
// }
// e = c.Where("user_id = ?",userId).First(&g).Error
// return
//}
2 changes: 2 additions & 0 deletions Core/Core.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
nested "github.com/antonfisher/nested-logrus-formatter"
"github.com/mcoo/OPQBot"
"github.com/sirupsen/logrus"
"gorm.io/gorm"
)

var log = logrus.New()
Expand Down Expand Up @@ -49,6 +50,7 @@ type Bot struct {
OPQBot.BotManager
BotCronManager utils.BotCron
Modules map[string]*Module
DB *gorm.DB
}
type ModuleInfo struct {
Name string
Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,7 @@ require (
golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8 // indirect
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
gorm.io/driver/mysql v1.1.1
gorm.io/driver/sqlite v1.1.4
gorm.io/gorm v1.21.11
)
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func main() {
}
b.BotCronManager = utils.NewBotCronManager()
b.BotCronManager.Start()
b.DB = Config.DB
err = b.AddEvent(OPQBot.EventNameOnConnected, func() {
log.Println("连接服务器成功")
})
Expand Down

0 comments on commit d1fea21

Please sign in to comment.