mirror of
https://github.com/flipped-aurora/gin-vue-admin.git
synced 2026-09-21 20:35:16 +00:00
Merge pull request #811 from flipped-aurora/pgsqlDevelop
2.4.6 优化 && 修复
This commit is contained in:
@@ -7,8 +7,8 @@ import (
|
||||
)
|
||||
|
||||
type ServiceGroup struct {
|
||||
ExampleServiceGroup example.ServiceGroup
|
||||
SystemServiceGroup system.ServiceGroup
|
||||
ExampleServiceGroup example.ServiceGroup
|
||||
AutoCodeServiceGroup autocode.ServiceGroup
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package example
|
||||
|
||||
type ServiceGroup struct {
|
||||
FileUploadAndDownloadService
|
||||
CustomerService
|
||||
ExcelService
|
||||
CustomerService
|
||||
FileUploadAndDownloadService
|
||||
}
|
||||
|
||||
@@ -3,16 +3,16 @@ package system
|
||||
type ServiceGroup struct {
|
||||
JwtService
|
||||
ApiService
|
||||
AuthorityService
|
||||
AutoCodeService
|
||||
AutoCodeHistoryService
|
||||
BaseMenuService
|
||||
CasbinService
|
||||
DictionaryService
|
||||
DictionaryDetailService
|
||||
InitDBService
|
||||
MenuService
|
||||
OperationRecordService
|
||||
SystemConfigService
|
||||
UserService
|
||||
CasbinService
|
||||
InitDBService
|
||||
AutoCodeService
|
||||
BaseMenuService
|
||||
AuthorityService
|
||||
DictionaryService
|
||||
SystemConfigService
|
||||
AutoCodeHistoryService
|
||||
OperationRecordService
|
||||
DictionaryDetailService
|
||||
}
|
||||
|
||||
@@ -76,11 +76,22 @@ func (apiService *ApiService) GetAPIInfoList(api system.SysApi, info request.Pag
|
||||
db = db.Limit(limit).Offset(offset)
|
||||
if order != "" {
|
||||
var OrderStr string
|
||||
if desc {
|
||||
OrderStr = order + " desc"
|
||||
} else {
|
||||
OrderStr = order
|
||||
// 设置有效排序key 防止sql注入
|
||||
// 感谢 Tom4t0 提交漏洞信息
|
||||
orderMap := make(map[string]bool, 5)
|
||||
orderMap["id"] = true
|
||||
orderMap["path"] = true
|
||||
orderMap["api_group"] = true
|
||||
orderMap["description"] = true
|
||||
orderMap["method"] = true
|
||||
if orderMap[order] {
|
||||
if desc {
|
||||
OrderStr = order + " desc"
|
||||
} else {
|
||||
OrderStr = order
|
||||
}
|
||||
}
|
||||
|
||||
err = db.Order(OrderStr).Find(&apiList).Error
|
||||
} else {
|
||||
err = db.Order("api_group").Find(&apiList).Error
|
||||
|
||||
@@ -13,7 +13,6 @@ import (
|
||||
|
||||
"github.com/flipped-aurora/gin-vue-admin/server/global"
|
||||
"github.com/flipped-aurora/gin-vue-admin/server/model/system"
|
||||
"github.com/flipped-aurora/gin-vue-admin/server/model/system/request"
|
||||
"github.com/flipped-aurora/gin-vue-admin/server/utils"
|
||||
|
||||
"gorm.io/gorm"
|
||||
@@ -301,38 +300,12 @@ func (autoCodeService *AutoCodeService) GetAllTplFile(pathName string, fileList
|
||||
return fileList, err
|
||||
}
|
||||
|
||||
//@author: [piexlmax](https://github.com/piexlmax)
|
||||
//@function: GetTables
|
||||
//@description: 获取数据库的所有表名
|
||||
//@param: dbName string
|
||||
//@return: err error, TableNames []request.TableReq
|
||||
|
||||
func (autoCodeService *AutoCodeService) GetTables(dbName string) (err error, TableNames []request.TableReq) {
|
||||
err = global.GVA_DB.Raw("select table_name as table_name from information_schema.tables where table_schema = ?", dbName).Scan(&TableNames).Error
|
||||
return err, TableNames
|
||||
}
|
||||
|
||||
//@author: [piexlmax](https://github.com/piexlmax)
|
||||
//@function: GetDB
|
||||
//@description: 获取数据库的所有数据库名
|
||||
//@return: err error, DBNames []request.DBReq
|
||||
|
||||
func (autoCodeService *AutoCodeService) GetDB() (err error, DBNames []request.DBReq) {
|
||||
err = global.GVA_DB.Raw("SELECT SCHEMA_NAME AS `database` FROM INFORMATION_SCHEMA.SCHEMATA;").Scan(&DBNames).Error
|
||||
return err, DBNames
|
||||
}
|
||||
|
||||
//@author: [piexlmax](https://github.com/piexlmax)
|
||||
//@function: GetDB
|
||||
//@description: 获取指定数据库和指定数据表的所有字段名,类型值等
|
||||
//@param: tableName string, dbName string
|
||||
//@return: err error, Columns []request.ColumnReq
|
||||
|
||||
func (autoCodeService *AutoCodeService) GetColumn(tableName string, dbName string) (err error, Columns []request.ColumnReq) {
|
||||
err = global.GVA_DB.Raw("SELECT COLUMN_NAME column_name,DATA_TYPE data_type,CASE DATA_TYPE WHEN 'longtext' THEN c.CHARACTER_MAXIMUM_LENGTH WHEN 'varchar' THEN c.CHARACTER_MAXIMUM_LENGTH WHEN 'double' THEN CONCAT_WS( ',', c.NUMERIC_PRECISION, c.NUMERIC_SCALE ) WHEN 'decimal' THEN CONCAT_WS( ',', c.NUMERIC_PRECISION, c.NUMERIC_SCALE ) WHEN 'int' THEN c.NUMERIC_PRECISION WHEN 'bigint' THEN c.NUMERIC_PRECISION ELSE '' END AS data_type_long,COLUMN_COMMENT column_comment FROM INFORMATION_SCHEMA.COLUMNS c WHERE table_name = ? AND table_schema = ?", tableName, dbName).Scan(&Columns).Error
|
||||
return err, Columns
|
||||
}
|
||||
|
||||
func (autoCodeService *AutoCodeService) DropTable(tableName string) error {
|
||||
return global.GVA_DB.Exec("DROP TABLE " + tableName).Error
|
||||
}
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
package system
|
||||
|
||||
import (
|
||||
"github.com/flipped-aurora/gin-vue-admin/server/global"
|
||||
"github.com/flipped-aurora/gin-vue-admin/server/model/system/response"
|
||||
)
|
||||
|
||||
type Database interface {
|
||||
GetDB() (data []response.Db, err error)
|
||||
GetTables(dbName string) (data []response.Table, err error)
|
||||
GetColumn(tableName string, dbName string) (data []response.Column, err error)
|
||||
}
|
||||
|
||||
func (autoCodeService *AutoCodeService) Database() Database {
|
||||
switch global.GVA_CONFIG.System.DbType {
|
||||
case "mysql":
|
||||
return AutoCodeMysql
|
||||
case "pgsql":
|
||||
return AutoCodePgsql
|
||||
default:
|
||||
return AutoCodeMysql
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package system
|
||||
|
||||
import (
|
||||
"github.com/flipped-aurora/gin-vue-admin/server/global"
|
||||
"github.com/flipped-aurora/gin-vue-admin/server/model/system/response"
|
||||
)
|
||||
|
||||
var AutoCodeMysql = new(autoCodeMysql)
|
||||
|
||||
type autoCodeMysql struct{}
|
||||
|
||||
// GetDB 获取数据库的所有数据库名
|
||||
// Author [piexlmax](https://github.com/piexlmax)
|
||||
// Author [SliverHorn](https://github.com/SliverHorn)
|
||||
func (s *autoCodeMysql) GetDB() (data []response.Db, err error) {
|
||||
var entities []response.Db
|
||||
sql := "SELECT SCHEMA_NAME AS `database` FROM INFORMATION_SCHEMA.SCHEMATA;"
|
||||
err = global.GVA_DB.Raw(sql).Scan(&entities).Error
|
||||
return entities, err
|
||||
}
|
||||
|
||||
// GetTables 获取数据库的所有表名
|
||||
// Author [piexlmax](https://github.com/piexlmax)
|
||||
// Author [SliverHorn](https://github.com/SliverHorn)
|
||||
func (s *autoCodeMysql) GetTables(dbName string) (data []response.Table, err error) {
|
||||
var entities []response.Table
|
||||
sql := `select table_name as table_name from information_schema.tables where table_schema = ?`
|
||||
err = global.GVA_DB.Raw(sql, dbName).Scan(&entities).Error
|
||||
return entities, err
|
||||
}
|
||||
|
||||
// GetColumn 获取指定数据库和指定数据表的所有字段名,类型值等
|
||||
// Author [piexlmax](https://github.com/piexlmax)
|
||||
// Author [SliverHorn](https://github.com/SliverHorn)
|
||||
func (s *autoCodeMysql) GetColumn(tableName string, dbName string) (data []response.Column, err error) {
|
||||
var entities []response.Column
|
||||
sql := `
|
||||
SELECT COLUMN_NAME column_name,
|
||||
DATA_TYPE data_type,
|
||||
CASE DATA_TYPE
|
||||
WHEN 'longtext' THEN c.CHARACTER_MAXIMUM_LENGTH
|
||||
WHEN 'varchar' THEN c.CHARACTER_MAXIMUM_LENGTH
|
||||
WHEN 'double' THEN CONCAT_WS(',', c.NUMERIC_PRECISION, c.NUMERIC_SCALE)
|
||||
WHEN 'decimal' THEN CONCAT_WS(',', c.NUMERIC_PRECISION, c.NUMERIC_SCALE)
|
||||
WHEN 'int' THEN c.NUMERIC_PRECISION
|
||||
WHEN 'bigint' THEN c.NUMERIC_PRECISION
|
||||
ELSE '' END AS data_type_long,
|
||||
COLUMN_COMMENT column_comment
|
||||
FROM INFORMATION_SCHEMA.COLUMNS c
|
||||
WHERE table_name = ?
|
||||
AND table_schema = ?
|
||||
`
|
||||
err = global.GVA_DB.Raw(sql, tableName, dbName).Scan(&entities).Error
|
||||
return entities, err
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
package system
|
||||
|
||||
import (
|
||||
"github.com/flipped-aurora/gin-vue-admin/server/global"
|
||||
"github.com/flipped-aurora/gin-vue-admin/server/model/system/response"
|
||||
"github.com/pkg/errors"
|
||||
"gorm.io/driver/postgres"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/logger"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var AutoCodePgsql = new(autoCodePgsql)
|
||||
|
||||
type autoCodePgsql struct{}
|
||||
|
||||
// GetDB 获取数据库的所有数据库名
|
||||
// Author [piexlmax](https://github.com/piexlmax)
|
||||
// Author [SliverHorn](https://github.com/SliverHorn)
|
||||
func (a *autoCodePgsql) GetDB() (data []response.Db, err error) {
|
||||
var entities []response.Db
|
||||
sql := `SELECT datname as database FROM pg_database WHERE datistemplate = false`
|
||||
err = global.GVA_DB.Raw(sql).Scan(&entities).Error
|
||||
return entities, err
|
||||
}
|
||||
|
||||
// GetTables 获取数据库的所有表名
|
||||
// Author [piexlmax](https://github.com/piexlmax)
|
||||
// Author [SliverHorn](https://github.com/SliverHorn)
|
||||
func (a *autoCodePgsql) GetTables(dbName string) (data []response.Table, err error) {
|
||||
var entities []response.Table
|
||||
sql := `select table_name as table_name from information_schema.tables where table_catalog = ? and table_schema = ?`
|
||||
db, _err := gorm.Open(postgres.Open(global.GVA_CONFIG.Pgsql.LinkDsn(dbName)), &gorm.Config{Logger: logger.Default.LogMode(logger.Info)})
|
||||
if _err != nil {
|
||||
return nil, errors.Wrapf(err, "[pgsql] 连接 数据库(%s)的表失败!", dbName)
|
||||
}
|
||||
err = db.Raw(sql, dbName, "public").Scan(&entities).Error
|
||||
return entities, err
|
||||
}
|
||||
|
||||
// GetColumn 获取指定数据库和指定数据表的所有字段名,类型值等
|
||||
// Author [piexlmax](https://github.com/piexlmax)
|
||||
// Author [SliverHorn](https://github.com/SliverHorn)
|
||||
func (a *autoCodePgsql) GetColumn(tableName string, dbName string) (data []response.Column, err error) {
|
||||
// todo 数据获取不全, 待完善sql
|
||||
sql := `
|
||||
SELECT columns.COLUMN_NAME as column_name,
|
||||
columns.DATA_TYPE as data_type,
|
||||
CASE
|
||||
columns.DATA_TYPE
|
||||
WHEN 'text' THEN
|
||||
concat_ws('', '', columns.CHARACTER_MAXIMUM_LENGTH)
|
||||
WHEN 'varchar' THEN
|
||||
concat_ws('', '', columns.CHARACTER_MAXIMUM_LENGTH)
|
||||
WHEN 'smallint' THEN
|
||||
concat_ws(',', columns.NUMERIC_PRECISION, columns.NUMERIC_SCALE)
|
||||
WHEN 'decimal' THEN
|
||||
concat_ws(',', columns.NUMERIC_PRECISION, columns.NUMERIC_SCALE)
|
||||
WHEN 'integer' THEN
|
||||
concat_ws('', '', columns.NUMERIC_PRECISION)
|
||||
WHEN 'bigint' THEN
|
||||
concat_ws('', '', columns.NUMERIC_PRECISION)
|
||||
ELSE ''
|
||||
END AS data_type_long,
|
||||
(select description.description
|
||||
from pg_description description
|
||||
where description.objoid = (select attribute.attrelid
|
||||
from pg_attribute attribute
|
||||
where attribute.attrelid =
|
||||
(select oid from pg_class class where class.relname = '@table_name') and attname =columns.COLUMN_NAME )
|
||||
and description.objsubid = (select attribute.attnum
|
||||
from pg_attribute attribute
|
||||
where attribute.attrelid =
|
||||
(select oid from pg_class class where class.relname = '@table_name') and attname =columns.COLUMN_NAME )) as column_comment
|
||||
FROM INFORMATION_SCHEMA.COLUMNS columns
|
||||
WHERE table_catalog = '@table_catalog'
|
||||
and table_schema = 'public'
|
||||
and table_name = '@table_name';
|
||||
`
|
||||
var entities []response.Column
|
||||
db, _err := gorm.Open(postgres.Open(global.GVA_CONFIG.Pgsql.LinkDsn(dbName)), &gorm.Config{Logger: logger.Default.LogMode(logger.Info)})
|
||||
if _err != nil {
|
||||
return nil, errors.Wrapf(err, "[pgsql] 连接 数据库(%s)的表(%s)失败!", dbName, tableName)
|
||||
}
|
||||
sql = strings.ReplaceAll(sql, "@table_catalog", dbName)
|
||||
sql = strings.ReplaceAll(sql, "@table_name", tableName)
|
||||
err = db.Raw(sql).Scan(&entities).Error
|
||||
return entities, err
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package system
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/flipped-aurora/gin-vue-admin/server/model/system/response"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -17,19 +18,14 @@ import (
|
||||
|
||||
var RepeatErr = errors.New("重复创建")
|
||||
|
||||
type AutoCodeHistoryService struct {
|
||||
}
|
||||
type AutoCodeHistoryService struct{}
|
||||
|
||||
var AutoCodeHistoryServiceApp = new(AutoCodeHistoryService)
|
||||
|
||||
func (autoCodeHistoryService *AutoCodeHistoryService) Repeat(structName string) bool {
|
||||
|
||||
var count int64
|
||||
global.GVA_DB.Model(&system.SysAutoCodeHistory{}).Where("struct_name = ? and flag = 0", structName).Count(&count)
|
||||
return count > 0
|
||||
}
|
||||
|
||||
// CreateAutoCodeHistory RouterPath : RouterPath@RouterString;RouterPath2@RouterString2
|
||||
// CreateAutoCodeHistory 创建代码生成器历史记录
|
||||
// RouterPath : RouterPath@RouterString;RouterPath2@RouterString2
|
||||
// Author [SliverHorn](https://github.com/SliverHorn)
|
||||
// Author [songzhibin97](https://github.com/songzhibin97)
|
||||
func (autoCodeHistoryService *AutoCodeHistoryService) CreateAutoCodeHistory(meta, structName, structCNName, autoCodePath string, injectionMeta string, tableName string, apiIds string) error {
|
||||
return global.GVA_DB.Create(&system.SysAutoCodeHistory{
|
||||
RequestMeta: meta,
|
||||
@@ -42,10 +38,29 @@ func (autoCodeHistoryService *AutoCodeHistoryService) CreateAutoCodeHistory(meta
|
||||
}).Error
|
||||
}
|
||||
|
||||
// First 根据id获取代码生成器历史的数据
|
||||
// Author [SliverHorn](https://github.com/SliverHorn)
|
||||
// Author [songzhibin97](https://github.com/songzhibin97)
|
||||
func (autoCodeHistoryService *AutoCodeHistoryService) First(info *request.GetById) (string, error) {
|
||||
var meta string
|
||||
return meta, global.GVA_DB.Model(system.SysAutoCodeHistory{}).Select("request_meta").Where("id = ?", info.Uint()).First(&meta).Error
|
||||
}
|
||||
|
||||
// Repeat 检测重复
|
||||
// Author [SliverHorn](https://github.com/SliverHorn)
|
||||
// Author [songzhibin97](https://github.com/songzhibin97)
|
||||
func (autoCodeHistoryService *AutoCodeHistoryService) Repeat(structName string) bool {
|
||||
var count int64
|
||||
global.GVA_DB.Model(&system.SysAutoCodeHistory{}).Where("struct_name = ? and flag = 0", structName).Count(&count)
|
||||
return count > 0
|
||||
}
|
||||
|
||||
// RollBack 回滚
|
||||
func (autoCodeHistoryService *AutoCodeHistoryService) RollBack(id uint) error {
|
||||
// Author [SliverHorn](https://github.com/SliverHorn)
|
||||
// Author [songzhibin97](https://github.com/songzhibin97)
|
||||
func (autoCodeHistoryService *AutoCodeHistoryService) RollBack(info *request.GetById) error {
|
||||
md := system.SysAutoCodeHistory{}
|
||||
if err := global.GVA_DB.First(&md, id).Error; err != nil {
|
||||
if err := global.GVA_DB.Where("id = ?", info.Uint()).First(&md).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
// 清除API表
|
||||
@@ -54,7 +69,7 @@ func (autoCodeHistoryService *AutoCodeHistoryService) RollBack(id uint) error {
|
||||
global.GVA_LOG.Error("ClearTag DeleteApiByIds:", zap.Error(err))
|
||||
}
|
||||
// 获取全部表名
|
||||
err, dbNames := AutoCodeServiceApp.GetTables(global.GVA_CONFIG.Mysql.Dbname)
|
||||
dbNames, err := AutoCodeServiceApp.Database().GetTables(global.GVA_CONFIG.Mysql.Dbname)
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("ClearTag GetTables:", zap.Error(err))
|
||||
}
|
||||
@@ -104,26 +119,25 @@ func (autoCodeHistoryService *AutoCodeHistoryService) RollBack(id uint) error {
|
||||
return global.GVA_DB.Save(&md).Error
|
||||
}
|
||||
|
||||
func (autoCodeHistoryService *AutoCodeHistoryService) GetMeta(id uint) (string, error) {
|
||||
var meta string
|
||||
return meta, global.GVA_DB.Model(system.SysAutoCodeHistory{}).Select("request_meta").First(&meta, id).Error
|
||||
// Delete 删除历史数据
|
||||
// Author [SliverHorn](https://github.com/SliverHorn)
|
||||
// Author [songzhibin97](https://github.com/songzhibin97)
|
||||
func (autoCodeHistoryService *AutoCodeHistoryService) Delete(info *request.GetById) error {
|
||||
return global.GVA_DB.Where("id = ?", info.Uint()).Delete(&system.SysAutoCodeHistory{}).Error
|
||||
}
|
||||
|
||||
// GetSysHistoryPage 获取系统历史数据
|
||||
func (autoCodeHistoryService *AutoCodeHistoryService) GetSysHistoryPage(info request.PageInfo) (err error, list interface{}, total int64) {
|
||||
// GetList 获取系统历史数据
|
||||
// Author [SliverHorn](https://github.com/SliverHorn)
|
||||
// Author [songzhibin97](https://github.com/songzhibin97)
|
||||
func (autoCodeHistoryService *AutoCodeHistoryService) GetList(info request.PageInfo) (list []response.AutoCodeHistory, total int64, err error) {
|
||||
limit := info.PageSize
|
||||
offset := info.PageSize * (info.Page - 1)
|
||||
db := global.GVA_DB.Model(&system.SysAutoCodeHistory{})
|
||||
var fileLists []system.SysAutoCodeHistory
|
||||
var entities []response.AutoCodeHistory
|
||||
err = db.Count(&total).Error
|
||||
if err != nil {
|
||||
return
|
||||
return nil, total, err
|
||||
}
|
||||
err = db.Limit(limit).Offset(offset).Order("updated_at desc").Select("id,created_at,updated_at,struct_name,struct_cn_name,flag,table_name").Find(&fileLists).Error
|
||||
return err, fileLists, total
|
||||
}
|
||||
|
||||
// DeletePage 删除历史数据
|
||||
func (autoCodeHistoryService *AutoCodeHistoryService) DeletePage(id uint) error {
|
||||
return global.GVA_DB.Delete(&system.SysAutoCodeHistory{}, id).Error
|
||||
err = db.Limit(limit).Offset(offset).Order("updated_at desc").Find(&entities).Error
|
||||
return entities, total, err
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@ func (initDBService *InitDBService) writeMysqlConfig(mysql config.Mysql) error {
|
||||
for k, v := range cs {
|
||||
global.GVA_VP.Set(k, v)
|
||||
}
|
||||
global.GVA_VP.Set("jwt.signing-key", uuid.NewV4())
|
||||
global.GVA_VP.Set("jwt.signing-key", uuid.NewV4().String())
|
||||
return global.GVA_VP.WriteConfig()
|
||||
}
|
||||
|
||||
|
||||
@@ -23,7 +23,7 @@ func (initDBService *InitDBService) writePgsqlConfig(pgsql config.Pgsql) error {
|
||||
for k, v := range cs {
|
||||
global.GVA_VP.Set(k, v)
|
||||
}
|
||||
global.GVA_VP.Set("jwt.signing-key", uuid.NewV4())
|
||||
global.GVA_VP.Set("jwt.signing-key", uuid.NewV4().String())
|
||||
return global.GVA_VP.WriteConfig()
|
||||
}
|
||||
|
||||
@@ -69,7 +69,7 @@ func (initDBService *InitDBService) initPgsqlDB(conf request.InitDB) error {
|
||||
// initPgsqlData pgsql 初始化数据
|
||||
// Author [SliverHorn](https://github.com/SliverHorn)
|
||||
func (initDBService *InitDBService) initPgsqlData() error {
|
||||
return model.MysqlDataInitialize(
|
||||
return model.PgsqlDataInitialize(
|
||||
system.Api,
|
||||
system.User,
|
||||
system.Casbin,
|
||||
|
||||
Reference in New Issue
Block a user