mirror of
https://github.com/Li4n0/revsuit.git
synced 2026-09-21 22:30:46 +08:00
feat(database): support postgres (#43)
Co-authored-by: Li4n0 <[email protected]>
This commit is contained in:
+31
-6
@@ -1,6 +1,11 @@
|
||||
package database
|
||||
|
||||
import "gorm.io/gorm"
|
||||
import (
|
||||
"errors"
|
||||
"strings"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
var (
|
||||
DB *gorm.DB
|
||||
@@ -11,14 +16,34 @@ type DriverType = string
|
||||
|
||||
const Sqlite = "sqlite"
|
||||
const Mysql = "mysql"
|
||||
const Postgres = "postgres"
|
||||
const UnknowDatabase = "unknown database"
|
||||
|
||||
func InitDB(driver DriverType, dsn string) (err error) {
|
||||
Driver = driver
|
||||
switch driver {
|
||||
func InitDB(dsn string) (err error) {
|
||||
dbName, dbDsn := dbType(dsn)
|
||||
|
||||
switch dbName {
|
||||
case Sqlite:
|
||||
DB, err = NewSqlite3(dsn)
|
||||
DB, err = NewSqlite3(dbDsn)
|
||||
case Mysql:
|
||||
DB, err = NewMysql(dsn)
|
||||
DB, err = NewMysql(dbDsn)
|
||||
case Postgres:
|
||||
DB, err = NewPostgres(dbDsn)
|
||||
default:
|
||||
err = errors.New("unsupported database")
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func dbType(dsn string) (DriverType, string) {
|
||||
if strings.HasSuffix(dsn, ".db") || strings.HasPrefix(dsn, "sqlite3://") {
|
||||
return Sqlite, strings.TrimPrefix(dsn, "sqlite3://")
|
||||
} else if strings.HasPrefix(dsn, "postgres://") {
|
||||
return Postgres, strings.TrimPrefix(dsn, "postgres://")
|
||||
} else if strings.HasPrefix(dsn, "mysql://") {
|
||||
return Mysql, strings.TrimPrefix(dsn, "mysql://")
|
||||
} else if strings.Contains(dsn, "@tcp") { //兼容上个版本的写法
|
||||
return Mysql, dsn
|
||||
}
|
||||
return UnknowDatabase, dsn
|
||||
}
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"gorm.io/driver/postgres"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func NewPostgres(dsn string) (*gorm.DB, error) {
|
||||
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return db, nil
|
||||
}
|
||||
+11
-10
@@ -14,16 +14,17 @@ type Rule interface {
|
||||
}
|
||||
|
||||
type BaseRule struct {
|
||||
Rule `gorm:"-" json:"-" yaml:"-"`
|
||||
ID uint `gorm:"primarykey" form:"id" json:"id" yaml:"-"`
|
||||
CreatedAt time.Time `json:"created_at" yaml:"-"`
|
||||
UpdatedAt time.Time `json:"updated_at" yaml:"-"`
|
||||
Name string `gorm:"index;unique;not null;" form:"name" json:"name"`
|
||||
FlagFormat string `gorm:"unique;not null;" form:"flag_format" json:"flag_format" yaml:"flag_format"`
|
||||
flagCatcher *regexp.Regexp `gorm:"-" json:"-"`
|
||||
Rank int `gorm:"default:0" json:"rank" form:"rank"`
|
||||
PushToClient bool `gorm:"default:false;not null;" form:"push_to_client" json:"push_to_client" yaml:"push_to_client"`
|
||||
Notice bool `gorm:"default:false;not null;" form:"notice" json:"notice"`
|
||||
Rule `gorm:"-" json:"-" yaml:"-"`
|
||||
ID uint `gorm:"primarykey" form:"id" json:"id" yaml:"-"`
|
||||
CreatedAt time.Time `json:"created_at" yaml:"-"`
|
||||
UpdatedAt time.Time `json:"updated_at" yaml:"-"`
|
||||
Name string `gorm:"index;unique;not null;" form:"name" json:"name"`
|
||||
FlagFormat string `gorm:"unique;not null;" form:"flag_format" json:"flag_format" yaml:"flag_format"`
|
||||
flagCatcher *regexp.Regexp `gorm:"-" json:"-"`
|
||||
// base_bank 解决 mysql 中关键字 rank 冲突和 pg 下不能使用 ``
|
||||
Rank int `gorm:"default:0;column:base_rank" json:"rank" form:"rank"`
|
||||
PushToClient bool `gorm:"default:false;not null;" form:"push_to_client" json:"push_to_client" yaml:"push_to_client"`
|
||||
Notice bool `gorm:"default:false;not null;" form:"notice" json:"notice"`
|
||||
}
|
||||
|
||||
func (br BaseRule) Match(s string) (flag, flagGroup string, vars map[string]string) {
|
||||
|
||||
Reference in New Issue
Block a user