mirror of
https://gitee.com/XM-GO/PandaX.git
synced 2026-04-23 02:48:34 +08:00
【feat】 添加postgresql数据库支持,【修复】pg数据库的代码生成功能
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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模版读取失败")
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user