From 9f096ec16a59d2341f29e29dcc4dfb20afe006b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=92=8B=E5=90=89=E5=85=86?= <303176530@qq.com> Date: Mon, 15 Nov 2021 23:00:14 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0pgsql=E9=80=82=E9=85=8D?= =?UTF-8?q?=EF=BC=88todo=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/config.yaml | 15 +++++++ server/config/config.go | 1 + server/config/gorm.go | 14 +++++++ server/initialize/gorm.go | 39 ++----------------- server/initialize/gorm_mysql.go | 39 +++++++++++++++++++ server/initialize/gorm_pgsql.go | 36 +++++++++++++++++ server/model/system/request/sys_init.go | 1 + .../resource/template/server/service.go.tpl | 10 ++--- server/service/system/sys_initdb.go | 10 +++++ 9 files changed, 125 insertions(+), 40 deletions(-) create mode 100644 server/initialize/gorm_mysql.go create mode 100644 server/initialize/gorm_pgsql.go diff --git a/server/config.yaml b/server/config.yaml index 119e69c9..a258fc10 100644 --- a/server/config.yaml +++ b/server/config.yaml @@ -59,6 +59,21 @@ captcha: # 未初始化之前请勿手动修改数据库信息!!!如果一定要手动初始化请看(https://www.github.com/flipped-aurora/gin-vue-admin/server.com/docs/first) mysql: path: '' + port: '' + config: '' + db-name: '' + username: '' + password: '' + max-idle-conns: 10 + max-open-conns: 100 + log-mode: "" + log-zap: false + +# pgsql connect configuration +# 未初始化之前请勿手动修改数据库信息!!!如果一定要手动初始化请看(https://www.github.com/flipped-aurora/gin-vue-admin/server.com/docs/first) +pgsql: + path: '' + port: '' config: '' db-name: '' username: '' diff --git a/server/config/config.go b/server/config/config.go index 83d256f4..b3267cce 100644 --- a/server/config/config.go +++ b/server/config/config.go @@ -12,6 +12,7 @@ type Server struct { AutoCode Autocode `mapstructure:"autoCode" json:"autoCode" yaml:"autoCode"` // gorm Mysql Mysql `mapstructure:"mysql" json:"mysql" yaml:"mysql"` + Pgsql Pgsql `mapstructure:"pgsql" json:"pgsql" yaml:"pgsql"` // oss Local Local `mapstructure:"local" json:"local" yaml:"local"` Qiniu Qiniu `mapstructure:"qiniu" json:"qiniu" yaml:"qiniu"` diff --git a/server/config/gorm.go b/server/config/gorm.go index ae6c7384..535d83b0 100644 --- a/server/config/gorm.go +++ b/server/config/gorm.go @@ -1,7 +1,21 @@ package config type Mysql struct { + Path string `mapstructure:"path" json:"path" yaml:"path"` // 服务器地址 + Port string `mapstructure:"port" json:"port" yaml:"port"` //:端口 + Config string `mapstructure:"config" json:"config" yaml:"config"` // 高级配置 + Dbname string `mapstructure:"db-name" json:"dbname" yaml:"db-name"` // 数据库名 + Username string `mapstructure:"username" json:"username" yaml:"username"` // 数据库用户名 + Password string `mapstructure:"password" json:"password" yaml:"password"` // 数据库密码 + MaxIdleConns int `mapstructure:"max-idle-conns" json:"maxIdleConns" yaml:"max-idle-conns"` // 空闲中的最大连接数 + MaxOpenConns int `mapstructure:"max-open-conns" json:"maxOpenConns" yaml:"max-open-conns"` // 打开到数据库的最大连接数 + LogMode string `mapstructure:"log-mode" json:"logMode" yaml:"log-mode"` // 是否开启Gorm全局日志 + LogZap bool `mapstructure:"log-zap" json:"logZap" yaml:"log-zap"` // 是否通过zap写入日志文件 +} + +type Pgsql struct { Path string `mapstructure:"path" json:"path" yaml:"path"` // 服务器地址:端口 + Port string `mapstructure:"port" json:"port" yaml:"port"` //:端口 Config string `mapstructure:"config" json:"config" yaml:"config"` // 高级配置 Dbname string `mapstructure:"db-name" json:"dbname" yaml:"db-name"` // 数据库名 Username string `mapstructure:"username" json:"username" yaml:"username"` // 数据库用户名 diff --git a/server/initialize/gorm.go b/server/initialize/gorm.go index fbbab797..62694486 100644 --- a/server/initialize/gorm.go +++ b/server/initialize/gorm.go @@ -1,18 +1,17 @@ package initialize import ( + "github.com/flipped-aurora/gin-vue-admin/server/initialize/internal" + "gorm.io/gorm/logger" "os" "github.com/flipped-aurora/gin-vue-admin/server/global" - "github.com/flipped-aurora/gin-vue-admin/server/initialize/internal" "github.com/flipped-aurora/gin-vue-admin/server/model/autocode" "github.com/flipped-aurora/gin-vue-admin/server/model/example" "github.com/flipped-aurora/gin-vue-admin/server/model/system" "go.uber.org/zap" - "gorm.io/driver/mysql" "gorm.io/gorm" - "gorm.io/gorm/logger" ) //@author: SliverHorn @@ -24,6 +23,8 @@ func Gorm() *gorm.DB { switch global.GVA_CONFIG.System.DbType { case "mysql": return GormMysql() + case "pgsql": + return GormPgSql() default: return GormMysql() } @@ -62,38 +63,6 @@ func MysqlTables(db *gorm.DB) { global.GVA_LOG.Info("register table success") } -//@author: SliverHorn -//@function: GormMysql -//@description: 初始化Mysql数据库 -//@return: *gorm.DB - -func GormMysql() *gorm.DB { - m := global.GVA_CONFIG.Mysql - if m.Dbname == "" { - return nil - } - dsn := m.Username + ":" + m.Password + "@tcp(" + m.Path + ")/" + m.Dbname + "?" + m.Config - mysqlConfig := mysql.Config{ - DSN: dsn, // DSN data source name - DefaultStringSize: 191, // string 类型字段的默认长度 - DisableDatetimePrecision: true, // 禁用 datetime 精度,MySQL 5.6 之前的数据库不支持 - DontSupportRenameIndex: true, // 重命名索引时采用删除并新建的方式,MySQL 5.7 之前的数据库和 MariaDB 不支持重命名索引 - DontSupportRenameColumn: true, // 用 `change` 重命名列,MySQL 8 之前的数据库和 MariaDB 不支持重命名列 - SkipInitializeWithVersion: false, // 根据版本自动配置 - } - if db, err := gorm.Open(mysql.New(mysqlConfig), gormConfig()); err != nil { - //global.GVA_LOG.Error("MySQL启动异常", zap.Any("err", err)) - //os.Exit(0) - //return nil - return nil - } else { - sqlDB, _ := db.DB() - sqlDB.SetMaxIdleConns(m.MaxIdleConns) - sqlDB.SetMaxOpenConns(m.MaxOpenConns) - return db - } -} - //@author: SliverHorn //@function: gormConfig //@description: 根据配置决定是否开启日志 diff --git a/server/initialize/gorm_mysql.go b/server/initialize/gorm_mysql.go new file mode 100644 index 00000000..7f21b4b7 --- /dev/null +++ b/server/initialize/gorm_mysql.go @@ -0,0 +1,39 @@ +package initialize + +import ( + "github.com/flipped-aurora/gin-vue-admin/server/global" + "gorm.io/driver/mysql" + "gorm.io/gorm" +) + +//@author: SliverHorn +//@function: GormMysql +//@description: 初始化Mysql数据库 +//@return: *gorm.DB + +func GormMysql() *gorm.DB { + m := global.GVA_CONFIG.Mysql + if m.Dbname == "" { + return nil + } + dsn := m.Username + ":" + m.Password + "@tcp(" + m.Path + ")/" + m.Dbname + "?" + m.Config + mysqlConfig := mysql.Config{ + DSN: dsn, // DSN data source name + DefaultStringSize: 191, // string 类型字段的默认长度 + DisableDatetimePrecision: true, // 禁用 datetime 精度,MySQL 5.6 之前的数据库不支持 + DontSupportRenameIndex: true, // 重命名索引时采用删除并新建的方式,MySQL 5.7 之前的数据库和 MariaDB 不支持重命名索引 + DontSupportRenameColumn: true, // 用 `change` 重命名列,MySQL 8 之前的数据库和 MariaDB 不支持重命名列 + SkipInitializeWithVersion: false, // 根据版本自动配置 + } + if db, err := gorm.Open(mysql.New(mysqlConfig), gormConfig()); err != nil { + //global.GVA_LOG.Error("MySQL启动异常", zap.Any("err", err)) + //os.Exit(0) + //return nil + return nil + } else { + sqlDB, _ := db.DB() + sqlDB.SetMaxIdleConns(m.MaxIdleConns) + sqlDB.SetMaxOpenConns(m.MaxOpenConns) + return db + } +} diff --git a/server/initialize/gorm_pgsql.go b/server/initialize/gorm_pgsql.go new file mode 100644 index 00000000..0d4016b7 --- /dev/null +++ b/server/initialize/gorm_pgsql.go @@ -0,0 +1,36 @@ +package initialize + +import ( + "github.com/flipped-aurora/gin-vue-admin/server/global" + "gorm.io/driver/postgres" + "gorm.io/gorm" +) + +//@author: SliverHorn +//@function: GormMysql +//@description: 初始化Mysql数据库 +//@return: *gorm.DB + +func GormPgSql() *gorm.DB { + p := global.GVA_CONFIG.Pgsql + if p.Dbname == "" { + return nil + } + + dsn := "host=" + p.Path + " user=" + p.Username + " password=" + p.Password + " dbname=" + p.Dbname + " port=" + p.Port + " " + p.Config + pgsqlConfig := postgres.Config{ + DSN: dsn, // DSN data source name + PreferSimpleProtocol: false, + } + if db, err := gorm.Open(postgres.New(pgsqlConfig), gormConfig()); err != nil { + //global.GVA_LOG.Error("MySQL启动异常", zap.Any("err", err)) + //os.Exit(0) + //return nil + return nil + } else { + sqlDB, _ := db.DB() + sqlDB.SetMaxIdleConns(p.MaxIdleConns) + sqlDB.SetMaxOpenConns(p.MaxOpenConns) + return db + } +} diff --git a/server/model/system/request/sys_init.go b/server/model/system/request/sys_init.go index 3f994cf3..90b19abb 100644 --- a/server/model/system/request/sys_init.go +++ b/server/model/system/request/sys_init.go @@ -1,6 +1,7 @@ package request type InitDB struct { + DBType string `json:"DBType"` // 数据库类型 Host string `json:"host"` // 服务器地址 Port string `json:"port"` // 数据库连接端口 UserName string `json:"userName" binding:"required"` // 数据库用户名 diff --git a/server/resource/template/server/service.go.tpl b/server/resource/template/server/service.go.tpl index 2fc99f19..f944e6e8 100644 --- a/server/resource/template/server/service.go.tpl +++ b/server/resource/template/server/service.go.tpl @@ -58,23 +58,23 @@ func ({{.Abbreviation}}Service *{{.StructName}}Service)Get{{.StructName}}InfoLis {{- if .FieldSearchType}} {{- if eq .FieldType "string" }} if info.{{.FieldName}} != "" { - db = db.Where("`{{.ColumnName}}` {{.FieldSearchType}} ?",{{if eq .FieldSearchType "LIKE"}}"%"+ {{ end }}info.{{.FieldName}}{{if eq .FieldSearchType "LIKE"}}+"%"{{ end }}) + db = db.Where("{{.ColumnName}} {{.FieldSearchType}} ?",{{if eq .FieldSearchType "LIKE"}}"%"+ {{ end }}info.{{.FieldName}}{{if eq .FieldSearchType "LIKE"}}+"%"{{ end }}) } {{- else if eq .FieldType "bool" }} if info.{{.FieldName}} != nil { - db = db.Where("`{{.ColumnName}}` {{.FieldSearchType}} ?",{{if eq .FieldSearchType "LIKE"}}"%"+{{ end }}info.{{.FieldName}}{{if eq .FieldSearchType "LIKE"}}+"%"{{ end }}) + db = db.Where("{{.ColumnName}} {{.FieldSearchType}} ?",{{if eq .FieldSearchType "LIKE"}}"%"+{{ end }}info.{{.FieldName}}{{if eq .FieldSearchType "LIKE"}}+"%"{{ end }}) } {{- else if eq .FieldType "int" }} if info.{{.FieldName}} != nil { - db = db.Where("`{{.ColumnName}}` {{.FieldSearchType}} ?",{{if eq .FieldSearchType "LIKE"}}"%"+{{ end }}info.{{.FieldName}}{{if eq .FieldSearchType "LIKE"}}+"%"{{ end }}) + db = db.Where("{{.ColumnName}} {{.FieldSearchType}} ?",{{if eq .FieldSearchType "LIKE"}}"%"+{{ end }}info.{{.FieldName}}{{if eq .FieldSearchType "LIKE"}}+"%"{{ end }}) } {{- else if eq .FieldType "float64" }} if info.{{.FieldName}} != nil { - db = db.Where("`{{.ColumnName}}` {{.FieldSearchType}} ?",{{if eq .FieldSearchType "LIKE"}}"%"+{{ end }}info.{{.FieldName}}{{if eq .FieldSearchType "LIKE"}}+"%"{{ end }}) + db = db.Where("{{.ColumnName}} {{.FieldSearchType}} ?",{{if eq .FieldSearchType "LIKE"}}"%"+{{ end }}info.{{.FieldName}}{{if eq .FieldSearchType "LIKE"}}+"%"{{ end }}) } {{- else if eq .FieldType "time.Time" }} if info.{{.FieldName}} != nil { - db = db.Where("`{{.ColumnName}}` {{.FieldSearchType}} ?",{{if eq .FieldSearchType "LIKE"}}"%"+{{ end }}info.{{.FieldName}}{{if eq .FieldSearchType "LIKE"}}+"%"{{ end }}) + db = db.Where("{{.ColumnName}} {{.FieldSearchType}} ?",{{if eq .FieldSearchType "LIKE"}}"%"+{{ end }}info.{{.FieldName}}{{if eq .FieldSearchType "LIKE"}}+"%"{{ end }}) } {{- end }} {{- end }} diff --git a/server/service/system/sys_initdb.go b/server/service/system/sys_initdb.go index 25b03fbd..afd15e5e 100644 --- a/server/service/system/sys_initdb.go +++ b/server/service/system/sys_initdb.go @@ -80,7 +80,17 @@ func (initDBService *InitDBService) initDB(InitDBFunctions ...system.InitDBFunc) //@return: error func (initDBService *InitDBService) InitDB(conf request.InitDB) error { + switch conf.DBType { + case "mysql": + return initDBService.initMsqlDB(conf) + case "pgsql": + return nil + default: + return initDBService.initMsqlDB(conf) + } +} +func (initDBService *InitDBService) initMsqlDB(conf request.InitDB) error { if conf.Host == "" { conf.Host = "127.0.0.1" }