change database scheema

This commit is contained in:
Jay 2019-07-02 09:02:31 +00:00
parent dd09bfda6e
commit 462804ed07
5 changed files with 150 additions and 78 deletions

26
main.go
View File

@ -2,14 +2,11 @@ package main
import (
"dorisbot/models"
discordmodel "dorisbot/models/discord"
"dorisbot/pkg/config"
"dorisbot/pkg/options"
"dorisbot/pkg/svc/discord"
"fmt"
"log"
"git.trj.tw/golang/utils"
)
func init() {
@ -34,6 +31,15 @@ func main() {
conf := config.GetConfig()
_, err = models.NewDB(conf.Database)
if err != nil {
log.Fatal(err)
}
err = models.Ping()
if err != nil {
log.Fatal(err)
}
client, err := discord.NewClient(conf.Discord.BotToken)
if err != nil {
log.Fatal(err)
@ -45,19 +51,5 @@ func main() {
}
defer client.Close()
_, err = models.NewDB(conf.Database)
if err != nil {
log.Fatal(err)
}
list, err := discordmodel.GetDiscordServerList()
if err != nil {
log.Fatal(err)
}
log.Println("server list ::: ", list)
for _, it := range list {
log.Println(utils.ToMap(it))
}
<-cc
}

View File

@ -1,49 +0,0 @@
package commands
import (
"dorisbot/models"
"errors"
"time"
)
// CommandsModel -
type CommandsModel struct {
CMD string `db:"cmd"`
Server string `db:"server"`
RequireManage bool `db:"require_manage"`
RequireInit bool `db:"require_init"`
Value string `db:"value"`
Ctime time.Time `db:"ctime"`
Mtime time.Time `db:"mtime"`
}
// NewCommand -
func NewCommand(cmd, server, value string, requireManage, requireInit bool) (c *CommandsModel, err error) {
if len(cmd) == 0 || len(server) == 0 || len(value) == 0 {
return nil, errors.New("cmd, server or value is empty")
}
c = &CommandsModel{
CMD: cmd,
Server: server,
Value: value,
RequireInit: requireInit,
RequireManage: requireManage,
}
return
}
// Write -
func (p *CommandsModel) Write() (err error) {
db := models.GetConn()
query := `insert into "commands" ("cmd", "server", "value", "require_init", "require_manage") values
(:cmd, :server, :value, :require_init, :require_manage) returning *`
row, err := db.NamedQuery(query, p)
if err != nil {
return err
}
err = row.StructScan(p)
return
}

View File

@ -2,7 +2,7 @@ package discord
import (
"dorisbot/models"
cmdmodel "dorisbot/models/commands"
pubmodel "dorisbot/models/public"
"errors"
"time"
)
@ -52,7 +52,7 @@ func NewDiscordServer(id, name string, permission int) (ds *DiscordServerModel,
// GetDiscordServerList -
func GetDiscordServerList() (list []*DiscordServerModel, err error) {
db := models.GetConn()
query := `select "id", "name", "permission", "owner", "ctime", "mtime" from discord_server`
query := `select "id", "name", "permission", "owner", "ctime", "mtime" from discord.server`
err = db.Select(&list, query)
if err != nil {
return nil, err
@ -67,7 +67,7 @@ func GetDiscordServerByID(id string) (ds *DiscordServerModel, err error) {
}
db := models.GetConn()
ds = &DiscordServerModel{}
query := `select "id", "name", "permission", "owner", "ctime", "mtime" from discord_server where "id" = $1`
query := `select "id", "name", "permission", "owner", "ctime", "mtime" from discord.server where "id" = $1`
err = db.Get(ds, query, id)
return
}
@ -75,7 +75,7 @@ func GetDiscordServerByID(id string) (ds *DiscordServerModel, err error) {
// Write DiscordServer data to database
func (p *DiscordServerModel) Write() error {
db := models.GetConn()
query := `insert into discord_server ("id", "name", "permission", "owner") values (:id, :name, :permission, :owner) returning *`
query := `insert into discord.server ("id", "name", "permission", "owner") values (:id, :name, :permission, :owner) returning *`
row, err := db.NamedQuery(query, p)
if err != nil {
return err
@ -93,7 +93,7 @@ func (p *DiscordServerModel) ChangeName(name string) (err error) {
return errors.New("name is empty")
}
db := models.GetConn()
query := `update discord_server set "name" = $1, "mtime" = now() where "id" = $2`
query := `update discord.server set "name" = $1, "mtime" = now() where "id" = $2`
_, err = db.Exec(query, name, p.ID)
if err != nil {
return err
@ -105,7 +105,7 @@ func (p *DiscordServerModel) ChangeName(name string) (err error) {
// GetChannelList -
func (p *DiscordServerModel) GetChannelList() (list []*DiscordChannelModel, err error) {
db := models.GetConn()
query := `select "id", "server", "enable_cmd", "enable_notify", "ctime", "mtime" from discord_channel where "server" = $1`
query := `select "id", "server", "enable_cmd", "enable_notify", "ctime", "mtime" from discord.channel where "server" = $1`
err = db.Select(&list, query, p.ID)
if err != nil {
return nil, err
@ -120,15 +120,15 @@ func (p *DiscordServerModel) GetChannel(id string) (ch *DiscordChannelModel, err
}
db := models.GetConn()
ch = &DiscordChannelModel{}
query := `select "id", "server", "enable_cmd", "enable_notify", "ctime", "mtime" from discord_channel where "id" = $1 and "server" = $2`
query := `select "id", "server", "enable_cmd", "enable_notify", "ctime", "mtime" from discord.channel where "id" = $1 and "server" = $2`
err = db.Get(ch, query, id, p.ID)
return
}
// GetCommandList -
func (p *DiscordServerModel) GetCommandList() (cmds []*cmdmodel.CommandsModel, err error) {
func (p *DiscordServerModel) GetCommandList() (cmds []*pubmodel.CommandModel, err error) {
db := models.GetConn()
query := `select "cmd", "server", "value", "require_init", "require_manage", "ctime", "mtime" from "commands" where "server" = $1`
query := `select "cmd", "server", "value", "require_init", "require_manage", "ctime", "mtime" from public."command" where "server" = $1`
err = db.Select(&cmds, query, p.ID)
if err != nil {
return nil, err
@ -139,7 +139,7 @@ func (p *DiscordServerModel) GetCommandList() (cmds []*cmdmodel.CommandsModel, e
// GetRoleList -
func (p *DiscordServerModel) GetRoleList() (list []*DiscordManageRoleModel, err error) {
db := models.GetConn()
query := `select "server", "role_id", "role_name", "ctime", "mtime" from "discord_manage_role" where "server" = $1`
query := `select "server", "role_id", "role_name", "ctime", "mtime" from "discord"."manage_role" where "server" = $1`
err = db.Select(&list, query, p.ID)
if err != nil {
return nil, err
@ -154,7 +154,7 @@ func (p *DiscordServerModel) GetRole(roleID string) (role *DiscordManageRoleMode
}
db := models.GetConn()
role = &DiscordManageRoleModel{}
query := `select "server", "role_id", "role_name", "ctime", "mtime" from "discord_manage_role" where "server" = $1 and "rold_id" = $2`
query := `select "server", "role_id", "role_name", "ctime", "mtime" from "discord"."manage_role" where "server" = $1 and "rold_id" = $2`
err = db.Get(role, query, p.ID, roleID)
if err != nil {
return nil, err
@ -165,7 +165,7 @@ func (p *DiscordServerModel) GetRole(roleID string) (role *DiscordManageRoleMode
// AddChannel -
func (p *DiscordServerModel) AddChannel(id string, enableCMD, enableNotify bool) (err error) {
db := models.GetConn()
query := `insert into discord_channel ("id", "server", "enable_cmd", "enable_notify", "ctime", "mtime") values
query := `insert into discord.channel ("id", "server", "enable_cmd", "enable_notify", "ctime", "mtime") values
($1, $2, $3, $4, now(), now())`
_, err = db.Exec(query, id, p.ID, enableCMD, enableNotify)
if err != nil {

128
models/public/command.go Normal file
View File

@ -0,0 +1,128 @@
package public
import (
"dorisbot/models"
"errors"
"time"
)
// CommandsModel -
type CommandModel struct {
Key string `db:"key"`
Platform string `db:"platform"`
Binding string `db:"binding"`
RequireManage bool `db:"require_manage"`
RequireInit bool `db:"require_init"`
Value string `db:"value"`
Ctime time.Time `db:"ctime"`
Mtime time.Time `db:"mtime"`
}
// NewCommand -
func NewCommand(key, value, platform, binding string, requireManage, requireInit bool) (c *CommandModel, err error) {
if len(key) == 0 || len(value) == 0 {
return nil, errors.New("cmd, server or value is empty")
}
c = &CommandModel{
Key: key,
Value: value,
Platform: platform,
Binding: binding,
RequireInit: requireInit,
RequireManage: requireManage,
}
return
}
// GetCommandListByPlatformBinding -
func GetCommandListByPlatformBinding(binding, platform string) (list []*CommandModel, err error) {
if len(binding) == 0 || len(platform) == 0 {
return nil, errors.New("server id or platform is empty")
}
db := models.GetConn()
query := `select "key", "platform", "binding", "value", "require_manage", "require_init", "ctime", "mtime"
from "command"
where
"binding" = $1
and "platform" = $2`
err = db.Select(&list, query, binding, platform)
if err != nil {
return nil, err
}
return
}
// GetCommandByPlatformBinding -
func GetCommandByPlatformBinding(key, binding, platform string) (list []*CommandModel, err error) {
if len(key) == 0 || len(binding) == 0 || len(platform) == 0 {
return nil, errors.New("key, server id or platform is empty")
}
db := models.GetConn()
query := `select "key", "platform", "binding", "value", "require_manage", "require_init", "ctime", "mtime"
from "command"
where
"key" = $1
and (
("binding" = $2 and "platform" = $3)
or ("binding" = '' and "platform" = '')
)
order by "binding" desc`
err = db.Select(&list, query, key, binding, platform)
if err != nil {
return nil, err
}
return
}
// GetCommandByKey -
func GetCommandByKey(key string) (list []*CommandModel, err error) {
if len(key) == 0 {
return nil, errors.New("key is empty")
}
db := models.GetConn()
query := `select "key", "platform", "binding", "value", "require_manage", "require_init", "ctime", "mtime"
from "command"
where
"key" = $1`
err = db.Select(&list, query, key)
if err != nil {
return nil, err
}
return
}
// GetGlobalCommand -
func GetGlobalCommand(key string) (list []*CommandModel, err error) {
if len(key) == 0 {
return nil, errors.New("key is empty")
}
db := models.GetConn()
query := `select "cmd", "server", "value", "require_manage", "require_init", "ctime", "mtime"
from "command"
where
"cmd" = $1
and "platform" = ''
and "binding" = ''`
err = db.Select(&list, query, key)
if err != nil {
return nil, err
}
return
}
// Write -
func (p *CommandModel) Write() (err error) {
db := models.GetConn()
query := `insert into "command" ("key", "paltform", "binding", "value", "require_init", "require_manage") values
(:key, :platform, :binding, :value, :require_init, :require_manage) returning *`
row, err := db.NamedQuery(query, p)
if err != nil {
return err
}
err = row.StructScan(p)
return
}

View File

@ -34,6 +34,7 @@ func messageCreateEvt(s *discordgo.Session, evt *discordgo.MessageCreate) {
if !keyword && server == nil {
return
}
}
func guildMemberAddEvt(s *discordgo.Session, evt *discordgo.GuildMemberAdd) {}