Browse Source
update:
update:
- pgsql 获取表名, 获取表字段(todo), 获取数据库名 refactor: - 自动化代码与自动化代码历史 代码规范化 - mysql 获取表名, 获取表字段, 获取数据库名 - enter 代码优化main
SliverHorn
3 years ago
28 changed files with 444 additions and 245 deletions
-
0server/api/v1/autocode/auto_code_example.go
-
2server/api/v1/enter.go
-
6server/api/v1/example/enter.go
-
33server/api/v1/system/enter.go
-
130server/api/v1/system/sys_auto_code.go
-
98server/api/v1/system/sys_auto_code_history.go
-
5server/api/v1/system/sys_initdb.go
-
8server/config/gorm_pgsql.go
-
26server/initialize/router.go
-
10server/model/common/request/common.go
-
7server/model/system/request/sys_auto_history.go
-
22server/model/system/response/sys_auto_code.go
-
13server/model/system/response/sys_auto_code_history.go
-
2server/model/system/sys_auto_code.go
-
0server/router/autocode/auto_code_example.go
-
2server/router/example/enter.go
-
16server/router/system/enter.go
-
19server/router/system/sys_auto_code.go
-
19server/router/system/sys_auto_code_history.go
-
0server/service/autocode/auto_code_example.go
-
2server/service/enter.go
-
4server/service/example/enter.go
-
16server/service/system/enter.go
-
27server/service/system/sys_auto_code.go
-
23server/service/system/sys_auto_code_interface.go
-
55server/service/system/sys_auto_code_mysql.go
-
76server/service/system/sys_auto_code_pgsql.go
-
68server/service/system/sys_autocode_history.go
@ -0,0 +1,98 @@ |
|||
package system |
|||
|
|||
import ( |
|||
"github.com/flipped-aurora/gin-vue-admin/server/global" |
|||
"github.com/flipped-aurora/gin-vue-admin/server/model/common/request" |
|||
"github.com/flipped-aurora/gin-vue-admin/server/model/common/response" |
|||
systemReq "github.com/flipped-aurora/gin-vue-admin/server/model/system/request" |
|||
"github.com/gin-gonic/gin" |
|||
"go.uber.org/zap" |
|||
) |
|||
|
|||
type AutoCodeHistoryApi struct{} |
|||
|
|||
// First
|
|||
// @Tags AutoCode
|
|||
// @Summary 获取meta信息
|
|||
// @Security ApiKeyAuth
|
|||
// @accept application/json
|
|||
// @Produce application/json
|
|||
// @Param data body request.GetById true "请求参数"
|
|||
// @Success 200 {object} response.Response{} "获取成功!"
|
|||
// @Router /autoCode/getMeta [post]
|
|||
func (a *AutoCodeHistoryApi) First(c *gin.Context) { |
|||
var info request.GetById |
|||
_ = c.ShouldBindJSON(&info) |
|||
data, err := autoCodeHistoryService.First(&info) |
|||
if err != nil { |
|||
response.FailWithMessage(err.Error(), c) |
|||
return |
|||
} |
|||
response.OkWithDetailed(gin.H{"meta": data}, "获取成功", c) |
|||
} |
|||
|
|||
// Delete
|
|||
// @Tags AutoCode
|
|||
// @Summary 删除回滚记录
|
|||
// @Security ApiKeyAuth
|
|||
// @accept application/json
|
|||
// @Produce application/json
|
|||
// @Param data body request.GetById true "请求参数"
|
|||
// @Success 200 {object} response.Response{} "删除成功!"
|
|||
// @Router /autoCode/delSysHistory [post]
|
|||
func (a *AutoCodeHistoryApi) Delete(c *gin.Context) { |
|||
var info request.GetById |
|||
_ = c.ShouldBindJSON(&info) |
|||
err := autoCodeHistoryService.Delete(&info) |
|||
if err != nil { |
|||
global.GVA_LOG.Error("删除失败!", zap.Error(err)) |
|||
response.FailWithMessage("删除失败", c) |
|||
return |
|||
} |
|||
response.OkWithMessage("删除成功", c) |
|||
} |
|||
|
|||
// RollBack
|
|||
// @Tags AutoCode
|
|||
// @Summary 回滚自动生成代码
|
|||
// @Security ApiKeyAuth
|
|||
// @accept application/json
|
|||
// @Produce application/json
|
|||
// @Param data body request.GetById true "请求参数"
|
|||
// @Success 200 {object} response.Response{} "回滚成功!"
|
|||
// @Router /autoCode/rollback [post]
|
|||
func (a *AutoCodeHistoryApi) RollBack(c *gin.Context) { |
|||
var info request.GetById |
|||
_ = c.ShouldBindJSON(&info) |
|||
if err := autoCodeHistoryService.RollBack(&info); err != nil { |
|||
response.FailWithMessage(err.Error(), c) |
|||
return |
|||
} |
|||
response.OkWithMessage("回滚成功", c) |
|||
} |
|||
|
|||
// GetList
|
|||
// @Tags AutoCode
|
|||
// @Summary 查询回滚记录
|
|||
// @Security ApiKeyAuth
|
|||
// @accept application/json
|
|||
// @Produce application/json
|
|||
// @Param data body systemReq.SysAutoHistory true "请求参数"
|
|||
// @Success 200 {object} response.Response{} "获取成功!"
|
|||
// @Router /autoCode/getSysHistory [post]
|
|||
func (a *AutoCodeHistoryApi) GetList(c *gin.Context) { |
|||
var search systemReq.SysAutoHistory |
|||
_ = c.ShouldBindJSON(&search) |
|||
list, total, err := autoCodeHistoryService.GetList(search.PageInfo) |
|||
if err != nil { |
|||
global.GVA_LOG.Error("获取失败!", zap.Error(err)) |
|||
response.FailWithMessage("获取失败", c) |
|||
return |
|||
} |
|||
response.OkWithDetailed(response.PageResult{ |
|||
List: list, |
|||
Total: total, |
|||
Page: search.Page, |
|||
PageSize: search.PageSize, |
|||
}, "获取成功", c) |
|||
} |
@ -0,0 +1,7 @@ |
|||
package request |
|||
|
|||
import "github.com/flipped-aurora/gin-vue-admin/server/model/common/request" |
|||
|
|||
type SysAutoHistory struct { |
|||
request.PageInfo |
|||
} |
@ -1,26 +1,16 @@ |
|||
package request |
|||
package response |
|||
|
|||
import "github.com/flipped-aurora/gin-vue-admin/server/model/common/request" |
|||
|
|||
type SysAutoHistory struct { |
|||
request.PageInfo |
|||
} |
|||
|
|||
type AutoHistoryByID struct { |
|||
ID uint `json:"id"` |
|||
} |
|||
|
|||
type DBReq struct { |
|||
type Db struct { |
|||
Database string `json:"database" gorm:"column:database"` |
|||
} |
|||
|
|||
type TableReq struct { |
|||
TableName string `json:"tableName"` |
|||
type Table struct { |
|||
TableName string `json:"tableName" gorm:"column:table_name"` |
|||
} |
|||
|
|||
type ColumnReq struct { |
|||
ColumnName string `json:"columnName" gorm:"column:column_name"` |
|||
type Column struct { |
|||
DataType string `json:"dataType" gorm:"column:data_type"` |
|||
ColumnName string `json:"columnName" gorm:"column:column_name"` |
|||
DataTypeLong string `json:"dataTypeLong" gorm:"column:data_type_long"` |
|||
ColumnComment string `json:"columnComment" gorm:"column:column_comment"` |
|||
} |
@ -0,0 +1,13 @@ |
|||
package response |
|||
|
|||
import "time" |
|||
|
|||
type AutoCodeHistory struct { |
|||
ID uint `json:"ID" gorm:"column:id"` |
|||
CreatedAt time.Time `json:"CreatedAt" gorm:"column:created_at"` |
|||
UpdatedAt time.Time `json:"UpdatedAt" gorm:"column:updated_at"` |
|||
TableName string `json:"tableName" gorm:"column:table_name"` |
|||
StructName string `json:"structName" gorm:"column:struct_name"` |
|||
StructCNName string `json:"structCNName" gorm:"column:struct_cn_name"` |
|||
Flag int `json:"flag" gorm:"column:flag"` |
|||
} |
@ -1,7 +1,7 @@ |
|||
package example |
|||
|
|||
type RouterGroup struct { |
|||
CustomerRouter |
|||
ExcelRouter |
|||
CustomerRouter |
|||
FileUploadAndDownloadRouter |
|||
} |
@ -0,0 +1,19 @@ |
|||
package system |
|||
|
|||
import ( |
|||
v1 "github.com/flipped-aurora/gin-vue-admin/server/api/v1" |
|||
"github.com/gin-gonic/gin" |
|||
) |
|||
|
|||
type AutoCodeHistoryRouter struct{} |
|||
|
|||
func (s *AutoCodeRouter) InitAutoCodeHistoryRouter(Router *gin.RouterGroup) { |
|||
autoCodeHistoryRouter := Router.Group("autoCode") |
|||
autoCodeHistoryApi := v1.ApiGroupApp.SystemApiGroup.AutoCodeHistoryApi |
|||
{ |
|||
autoCodeHistoryRouter.POST("getMeta", autoCodeHistoryApi.First) // 根据id获取meta信息
|
|||
autoCodeHistoryRouter.POST("rollback", autoCodeHistoryApi.RollBack) // 回滚
|
|||
autoCodeHistoryRouter.POST("delSysHistory", autoCodeHistoryApi.Delete) // 删除回滚记录
|
|||
autoCodeHistoryRouter.POST("getSysHistory", autoCodeHistoryApi.GetList) // 获取回滚记录分页
|
|||
} |
|||
} |
@ -1,7 +1,7 @@ |
|||
package example |
|||
|
|||
type ServiceGroup struct { |
|||
FileUploadAndDownloadService |
|||
CustomerService |
|||
ExcelService |
|||
CustomerService |
|||
FileUploadAndDownloadService |
|||
} |
@ -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,76 @@ |
|||
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" |
|||
) |
|||
|
|||
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 |
|||
FROM INFORMATION_SCHEMA.COLUMNS columns |
|||
WHERE table_catalog = ? |
|||
and table_schema = ? |
|||
and 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) |
|||
} |
|||
err = db.Raw(sql, dbName, "public", tableName).Scan(&entities).Error |
|||
return entities, err |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue