【feat】 添加postgresql数据库支持,【修复】pg数据库的代码生成功能

This commit is contained in:
PandaX
2023-11-10 10:07:38 +08:00
parent a37dace97c
commit 72ffbf38c3
15 changed files with 1323 additions and 724 deletions

View File

@@ -19,6 +19,7 @@ type DevGenTable struct {
Remark string `gorm:"remark" json:"remark"` // 备注
PkColumn string `gorm:"pk_column;" json:"pkColumn"`
PkGoField string `gorm:"pk_go_field" json:"pkGoField"`
PkGoType string `gorm:"pk_go_type" json:"pkGoType"`
PkJsonField string `gorm:"pk_json_field" json:"pkJsonField"`
Columns []DevGenTableColumn `gorm:"-" json:"columns"` // 字段信息
model.BaseModel

View File

@@ -194,20 +194,19 @@ func (s *toolsGenTableColumn) GenTableInit(tableName string) entity.DevGenTable
data.FunctionAuthor = "panda"
wg := sync.WaitGroup{}
dcs := *dbColumn
for x := 0; x < len(dcs); x++ {
for x := 0; x < len(dbColumn); x++ {
index := x
wg.Add(1)
go func(wg *sync.WaitGroup, y int) {
defer wg.Done()
var column entity.DevGenTableColumn
column.ColumnComment = dcs[y].ColumnComment
column.ColumnName = dcs[y].ColumnName
column.ColumnType = dcs[y].ColumnType
column.ColumnComment = dbColumn[y].ColumnComment
column.ColumnName = dbColumn[y].ColumnName
column.ColumnType = dbColumn[y].ColumnType
column.Sort = y + 1
column.IsPk = "0"
nameList := strings.Split(dcs[y].ColumnName, "_")
nameList := strings.Split(dbColumn[y].ColumnName, "_")
for i := 0; i < len(nameList); i++ {
strStart := string([]byte(nameList[i])[:1])
strend := string([]byte(nameList[i])[1:])
@@ -218,21 +217,13 @@ func (s *toolsGenTableColumn) GenTableInit(tableName string) entity.DevGenTable
column.JsonField += strings.ToUpper(strStart) + strend
}
}
if strings.Contains(dcs[y].ColumnKey, "PR") {
column.IsPk = "1"
data.PkColumn = dcs[y].ColumnName
data.PkGoField = column.GoField
data.PkJsonField = column.JsonField
if dcs[y].Extra == "auto_increment" {
column.IsIncrement = "1"
}
}
if column.ColumnComment == "" {
column.ColumnComment = column.GoField
}
dataType := s.GetDbType(column.ColumnType)
//dataType := s.GetDbType(column.ColumnType)
dataType := strings.ToLower(column.ColumnType)
if s.IsStringObject(dataType) {
//字段为字符串类型
column.GoType = "string"
@@ -249,7 +240,6 @@ func (s *toolsGenTableColumn) GenTableInit(tableName string) entity.DevGenTable
} else if s.IsNumberObject(dataType) {
//字段为数字类型
column.HtmlType = "input"
column.HtmlType = "input"
t := ""
if global.Conf.Server.DbType == "postgresql" {
t = column.ColumnType
@@ -282,6 +272,17 @@ func (s *toolsGenTableColumn) GenTableInit(tableName string) entity.DevGenTable
column.HtmlType = "switch"
}
}
// 是主键的时候
if strings.Contains(dbColumn[y].ColumnKey, "PR") {
column.IsPk = "1"
data.PkColumn = dbColumn[y].ColumnName
data.PkGoField = column.GoField
data.PkGoType = column.GoType
data.PkJsonField = column.JsonField
if dbColumn[y].Extra == "auto_increment" {
column.IsIncrement = "1"
}
}
//新增字段
if s.IsNotEdit(column.ColumnName) {
column.IsRequired = "0"
@@ -346,7 +347,7 @@ func Preview(tableId int64) map[string]any {
biz.ErrIsNil(err, "service模版读取失败")
t3, err := template.ParseFiles("resource/template/go/api.template")
biz.ErrIsNil(err, "api模版读取失败")
biz.ErrIsNilAppendErr(err, "api模版读取失败")
t4, err := template.ParseFiles("resource/template/go/router.template")
biz.ErrIsNil(err, "router模版读取失败")

View File

@@ -16,7 +16,7 @@ import (
type (
SysGenTableColumnModel interface {
FindDbTablesColumnListPage(page, pageSize int, data entity.DBColumns) (*[]entity.DBColumns, int64)
FindDbTableColumnList(tableName string) *[]entity.DBColumns
FindDbTableColumnList(tableName string) []entity.DBColumns
Insert(data entity.DevGenTableColumn) *entity.DevGenTableColumn
FindList(data entity.DevGenTableColumn, exclude bool) *[]entity.DevGenTableColumn
@@ -58,8 +58,8 @@ func (m *devTableColumnModelImpl) FindDbTablesColumnListPage(page, pageSize int,
return &list, total
}
func (m *devTableColumnModelImpl) FindDbTableColumnList(tableName string) *[]entity.DBColumns {
resData := make([]entity.DBColumns, 0)
func (m *devTableColumnModelImpl) FindDbTableColumnList(tableName string) []entity.DBColumns {
if global.Conf.Server.DbType != "mysql" && global.Conf.Server.DbType != "postgresql" {
biz.ErrIsNil(errors.New("只支持mysql和postgresql数据库"), "只支持mysql和postgresql数据库")
}
@@ -73,9 +73,58 @@ func (m *devTableColumnModelImpl) FindDbTableColumnList(tableName string) *[]ent
biz.IsTrue(tableName != "", "table name cannot be empty")
db = db.Where("table_name = ?", tableName)
err := db.Find(&resData).Error
biz.ErrIsNil(err, "查询表字段失败")
return &resData
resData := make([]entity.DBColumns, 0)
if global.Conf.Server.DbType == "mysql" {
err := db.Find(&resData).Error
biz.ErrIsNil(err, "查询表字段失败")
return resData
}
if global.Conf.Server.DbType == "postgresql" {
pr, err := getPgPR(tableName)
biz.ErrIsNil(err, "查询PG表主键字段失败")
resDataP := make([]entity.DBColumnsP, 0)
err = db.Find(&resDataP).Error
biz.ErrIsNil(err, "查询表字段失败")
for _, data := range resDataP {
dbc := entity.DBColumns{
TableSchema: data.TableSchema,
TableName: data.TableName,
ColumnName: data.ColumnName,
ColumnDefault: data.ColumnDefault,
IsNullable: data.IsNullable,
DataType: data.DataType,
CharacterMaximumLength: data.CharacterMaximumLength,
CharacterSetName: data.CharacterSetName,
ColumnType: data.ColumnType,
ColumnKey: data.ColumnKey,
Extra: data.Extra,
ColumnComment: data.ColumnComment,
}
// 设置为主键
if pr == data.ColumnName {
dbc.ColumnKey = "PRIMARY KEY"
}
resData = append(resData, dbc)
}
return resData
}
return resData
}
func getPgPR(tableName string) (string, error) {
sql := `SELECT
kcu.column_name
FROM
information_schema.table_constraints AS tc
JOIN information_schema.key_column_usage AS kcu ON tc.constraint_name = kcu.constraint_name
WHERE
tc.constraint_type = 'PRIMARY KEY'
AND tc.table_schema = 'public'
AND tc.table_name = ?;`
var pkname string
err := global.Db.Raw(sql, tableName).Scan(&pkname).Error
return pkname, err
}
func (m *devTableColumnModelImpl) Insert(dgt entity.DevGenTableColumn) *entity.DevGenTableColumn {

View File

@@ -237,6 +237,9 @@ func GetDeviceToken(data *entity.Device) *model.DeviceAuth {
// 获取设备数量统计
func (m *deviceModelImpl) FindDeviceCount() (result entity.DeviceCount) {
sql := `SELECT COUNT(*) AS total, (SELECT COUNT(*) FROM devices WHERE DATE(create_time) = CURDATE()) AS today FROM devices`
if global.Conf.Server.DbType == "postgresql" {
sql = `SELECT COUNT(*) AS total, (SELECT COUNT(*) FROM devices WHERE DATE(create_time) = current_date) AS today FROM devices`
}
err := global.Db.Raw(sql).Scan(&result).Error
biz.ErrIsNil(err, "获取设备统计总数失败")
return result

View File

@@ -101,12 +101,18 @@ func (m *alarmModelImpl) Delete(id []string) {
// 获取告警数量统计
func (m *alarmModelImpl) FindAlarmCount() (count entity.DeviceCount) {
sql := `SELECT COUNT(*) AS total, (SELECT COUNT(*) FROM device_alarms WHERE DATE(time) = CURDATE()) AS today FROM device_alarms`
if global.Conf.Server.DbType == "postgresql" {
sql = `SELECT COUNT(*) AS total, (SELECT COUNT(*) FROM device_alarms WHERE DATE(time) = current_date) AS today FROM device_alarms`
}
err := global.Db.Raw(sql).Scan(&count).Error
biz.ErrIsNil(err, "获取告警统计总数失败")
return
}
func (m *alarmModelImpl) FindAlarmPanel() (count []entity.DeviceAlarmCount) {
sql := `SELECT CAST(time AS DATE) AS date, COUNT(*) AS count FROM device_alarms WHERE time >= DATE_SUB(CURDATE(), INTERVAL 30 DAY) GROUP BY CAST(time AS DATE)`
if global.Conf.Server.DbType == "postgresql" {
sql = `SELECT CAST(time AS DATE) AS date, COUNT(*) AS count FROM device_alarms WHERE time >= CURRENT_DATE - INTERVAL '30 day' GROUP BY CAST(time AS DATE);`
}
err := global.Db.Raw(sql).Scan(&count).Error
biz.ErrIsNil(err, "获取告警统计列表失败")
return

View File

@@ -151,6 +151,9 @@ func deleteDeviceStable(productId string) error {
// 获取产品数量统计
func (m *productModelImpl) FindProductCount() (count entity.DeviceCount) {
sql := `SELECT COUNT(*) AS total, (SELECT COUNT(*) FROM products WHERE DATE(create_time) = CURDATE()) AS today FROM products`
if global.Conf.Server.DbType == "postgresql" {
sql = `SELECT COUNT(*) AS total, (SELECT COUNT(*) FROM products WHERE DATE(create_time) = current_date) AS today FROM products`
}
err := global.Db.Raw(sql).Scan(&count).Error
biz.ErrIsNil(err, "获取产品统计总数失败")
return

View File

@@ -134,8 +134,13 @@ func (m *sysRoleModel) GetRoleOrganizationId(data entity.SysRole) []int64 {
func (m *sysRoleModel) FindOrganizationsByRoleId(roleId int64) (entity.SysRole, error) {
var roleData entity.SysRole
err := global.Db.Table(m.table).
Select("sys_roles.data_scope, GROUP_CONCAT(sys_role_organizations.organization_id) as org").
db := global.Db.Table(m.table)
if global.Conf.Server.DbType == "mysql" {
db.Select("sys_roles.data_scope, GROUP_CONCAT(sys_role_organizations.organization_id) as org")
} else {
db.Select("sys_roles.data_scope, STRING_AGG(sys_role_organizations.organization_id::text,',') as org")
}
err := db.
Joins("LEFT JOIN sys_role_organizations ON sys_roles.role_id = sys_role_organizations.role_id").
Where("sys_roles.role_id = ?", roleId).
Group("sys_roles.role_id").